2012-03-01 11:46:46 +01:00
|
|
|
// Copyright (C) 2012 Internet Systems Consortium, Inc. ("ISC")
|
2012-02-20 23:29:31 +01:00
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
|
|
|
|
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
|
|
// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
|
|
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
|
|
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
|
|
|
|
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
|
// PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
|
|
|
#include <dns/labelsequence.h>
|
2012-03-06 14:06:40 -08:00
|
|
|
#include <dns/name_internal.h>
|
2012-02-20 23:29:31 +01:00
|
|
|
#include <exceptions/exceptions.h>
|
|
|
|
|
2012-03-06 14:06:40 -08:00
|
|
|
#include <boost/functional/hash.hpp>
|
|
|
|
|
2012-02-20 23:29:31 +01:00
|
|
|
namespace isc {
|
|
|
|
namespace dns {
|
|
|
|
|
|
|
|
const char*
|
|
|
|
LabelSequence::getData(size_t *len) const {
|
2012-03-05 15:51:41 -08:00
|
|
|
*len = getDataLength();
|
|
|
|
return (&name_->ndata_[name_->offsets_[first_label_]]);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
LabelSequence::getDataLength() const {
|
2012-02-28 19:46:53 +01:00
|
|
|
// If the labelsequence is absolute, the current last_label_ falls
|
|
|
|
// out of the vector (since it points to the 'label' after the
|
|
|
|
// root label, which doesn't exist; in that case, return
|
|
|
|
// the length for the 'previous' label (the root label) plus
|
|
|
|
// one (for the root label zero octet)
|
|
|
|
if (isAbsolute()) {
|
2012-03-05 15:51:41 -08:00
|
|
|
return (name_->offsets_[last_label_ - 1] -
|
|
|
|
name_->offsets_[first_label_] + 1);
|
2012-02-28 19:46:53 +01:00
|
|
|
} else {
|
2012-03-05 15:51:41 -08:00
|
|
|
return (name_->offsets_[last_label_] - name_->offsets_[first_label_]);
|
2012-02-28 19:46:53 +01:00
|
|
|
}
|
2012-02-20 23:29:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
LabelSequence::equals(const LabelSequence& other, bool case_sensitive) const {
|
2012-02-21 12:37:07 +01:00
|
|
|
size_t len, other_len;
|
|
|
|
const char* data = getData(&len);
|
|
|
|
const char* other_data = other.getData(&other_len);
|
|
|
|
|
|
|
|
if (len != other_len) {
|
|
|
|
return (false);
|
|
|
|
}
|
2012-02-20 23:29:31 +01:00
|
|
|
if (case_sensitive) {
|
2012-02-21 12:37:07 +01:00
|
|
|
return (strncmp(data, other_data, len) == 0);
|
2012-03-05 15:58:34 -08:00
|
|
|
} else {
|
|
|
|
return (strncasecmp(data, other_data, len) == 0);
|
2012-02-20 23:29:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-03-02 10:31:16 +01:00
|
|
|
LabelSequence::stripLeft(size_t i) {
|
2012-03-01 11:46:46 +01:00
|
|
|
if (i >= getLabelCount()) {
|
2012-03-02 10:31:16 +01:00
|
|
|
isc_throw(OutOfRange, "Cannot strip to zero or less labels; " << i <<
|
2012-02-28 19:46:53 +01:00
|
|
|
" (labelcount: " << getLabelCount() << ")");
|
2012-02-28 12:30:04 +01:00
|
|
|
}
|
2012-03-01 11:46:46 +01:00
|
|
|
first_label_ += i;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-03-02 10:31:16 +01:00
|
|
|
LabelSequence::stripRight(size_t i) {
|
2012-03-01 11:46:46 +01:00
|
|
|
if (i >= getLabelCount()) {
|
2012-03-02 10:31:16 +01:00
|
|
|
isc_throw(OutOfRange, "Cannot strip to zero or less labels; " << i <<
|
2012-03-01 11:46:46 +01:00
|
|
|
" (labelcount: " << getLabelCount() << ")");
|
2012-02-20 23:29:31 +01:00
|
|
|
}
|
2012-03-01 11:46:46 +01:00
|
|
|
last_label_ -= i;
|
2012-02-20 23:29:31 +01:00
|
|
|
}
|
|
|
|
|
2012-02-28 19:46:53 +01:00
|
|
|
bool
|
|
|
|
LabelSequence::isAbsolute() const {
|
2012-03-05 15:51:41 -08:00
|
|
|
return (last_label_ == name_->offsets_.size());
|
2012-02-28 19:46:53 +01:00
|
|
|
}
|
|
|
|
|
2012-03-06 14:06:40 -08:00
|
|
|
size_t
|
|
|
|
LabelSequence::getHash(bool case_sensitive) const {
|
|
|
|
size_t length;
|
|
|
|
const char* s = getData(&length);
|
|
|
|
if (length > 16) {
|
|
|
|
length = 16;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t hash_val = 0;
|
|
|
|
while (length > 0) {
|
|
|
|
const unsigned char c = *s++;
|
|
|
|
boost::hash_combine(hash_val, case_sensitive ? c :
|
|
|
|
isc::dns::name::internal::maptolower[c]);
|
|
|
|
--length;
|
|
|
|
}
|
|
|
|
return (hash_val);
|
|
|
|
}
|
|
|
|
|
2012-02-20 23:29:31 +01:00
|
|
|
} // end namespace dns
|
|
|
|
} // end namespace isc
|