123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- #include <dns/labelsequence.h>
- #include <dns/name_internal.h>
- #include <exceptions/exceptions.h>
- #include <boost/functional/hash.hpp>
- #include <cstring>
- namespace isc {
- namespace dns {
- LabelSequence::LabelSequence(const uint8_t* data,
- const uint8_t* offsets,
- size_t offsets_size) :
- data_(data),
- offsets_(offsets),
- first_label_(0),
- last_label_(offsets_size - 1)
- {
- if (data == NULL || offsets == NULL) {
- isc_throw(BadValue,
- "Null pointer passed to LabelSequence constructor");
- }
- if (offsets_size == 0) {
- isc_throw(BadValue, "Zero offsets to LabelSequence constructor");
- }
- if (offsets_size > Name::MAX_LABELS) {
- isc_throw(BadValue, "MAX_LABELS exceeded");
- }
- for (size_t cur_offset = 0; cur_offset < offsets_size; ++cur_offset) {
- if (offsets[cur_offset] > Name::MAX_LABELLEN) {
- isc_throw(BadValue, "MAX_LABEL_LEN exceeded");
- }
- if (cur_offset > 0 && offsets[cur_offset] <= offsets[cur_offset - 1]) {
- isc_throw(BadValue, "Offset smaller than previous offset");
- }
- }
- }
- const uint8_t*
- LabelSequence::getData(size_t *len) const {
- *len = getDataLength();
- return (&data_[offsets_[first_label_]]);
- }
- void
- LabelSequence::getOffsetData(size_t* len,
- uint8_t placeholder[Name::MAX_LABELS]) const
- {
- *len = getLabelCount();
- for (size_t i = 0; i < *len; ++i) {
- placeholder[i] = offsets_[first_label_ + i] - offsets_[first_label_];
- }
- }
- size_t
- LabelSequence::getDataLength() const {
- const size_t last_label_len = data_[offsets_[last_label_]] + 1;
- return (offsets_[last_label_] - offsets_[first_label_] + last_label_len);
- }
- bool
- LabelSequence::equals(const LabelSequence& other, bool case_sensitive) const {
- size_t len, other_len;
- const uint8_t* data = getData(&len);
- const uint8_t* other_data = other.getData(&other_len);
- if (len != other_len) {
- return (false);
- }
- if (case_sensitive) {
- return (std::memcmp(data, other_data, len) == 0);
- }
-
-
-
- for (size_t i = 0; i < len; ++i) {
- const uint8_t ch = data[i];
- const uint8_t other_ch = other_data[i];
- if (isc::dns::name::internal::maptolower[ch] !=
- isc::dns::name::internal::maptolower[other_ch]) {
- return (false);
- }
- }
- return (true);
- }
- NameComparisonResult
- LabelSequence::compare(const LabelSequence& other,
- bool case_sensitive) const
- {
-
-
-
- unsigned int nlabels = 0;
- int l1 = getLabelCount();
- int l2 = other.getLabelCount();
- const int ldiff = static_cast<int>(l1) - static_cast<int>(l2);
- unsigned int l = (ldiff < 0) ? l1 : l2;
- while (l > 0) {
- --l;
- --l1;
- --l2;
- size_t pos1 = offsets_[l1 + first_label_];
- size_t pos2 = other.offsets_[l2 + other.first_label_];
- unsigned int count1 = data_[pos1++];
- unsigned int count2 = other.data_[pos2++];
-
-
- assert(count1 <= Name::MAX_LABELLEN && count2 <= Name::MAX_LABELLEN);
- const int cdiff = static_cast<int>(count1) - static_cast<int>(count2);
- unsigned int count = (cdiff < 0) ? count1 : count2;
- while (count > 0) {
- const uint8_t label1 = data_[pos1];
- const uint8_t label2 = other.data_[pos2];
- int chdiff;
- if (case_sensitive) {
- chdiff = static_cast<int>(label1) - static_cast<int>(label2);
- } else {
- chdiff = static_cast<int>(
- isc::dns::name::internal::maptolower[label1]) -
- static_cast<int>(
- isc::dns::name::internal::maptolower[label2]);
- }
- if (chdiff != 0) {
- return (NameComparisonResult(
- chdiff, nlabels,
- nlabels == 0 ? NameComparisonResult::NONE :
- NameComparisonResult::COMMONANCESTOR));
- }
- --count;
- ++pos1;
- ++pos2;
- }
- if (cdiff != 0) {
- return (NameComparisonResult(
- cdiff, nlabels,
- nlabels == 0 ? NameComparisonResult::NONE :
- NameComparisonResult::COMMONANCESTOR));
- }
- ++nlabels;
- }
- if (ldiff < 0) {
- return (NameComparisonResult(ldiff, nlabels,
- NameComparisonResult::SUPERDOMAIN));
- } else if (ldiff > 0) {
- return (NameComparisonResult(ldiff, nlabels,
- NameComparisonResult::SUBDOMAIN));
- }
- return (NameComparisonResult(ldiff, nlabels, NameComparisonResult::EQUAL));
- }
- void
- LabelSequence::stripLeft(size_t i) {
- if (i >= getLabelCount()) {
- isc_throw(OutOfRange, "Cannot strip to zero or less labels; " << i <<
- " (labelcount: " << getLabelCount() << ")");
- }
- first_label_ += i;
- }
- void
- LabelSequence::stripRight(size_t i) {
- if (i >= getLabelCount()) {
- isc_throw(OutOfRange, "Cannot strip to zero or less labels; " << i <<
- " (labelcount: " << getLabelCount() << ")");
- }
- last_label_ -= i;
- }
- bool
- LabelSequence::isAbsolute() const {
- return (data_[offsets_[last_label_]] == 0);
- }
- size_t
- LabelSequence::getHash(bool case_sensitive) const {
- size_t length;
- const uint8_t* s = getData(&length);
- if (length > 16) {
- length = 16;
- }
- size_t hash_val = 0;
- while (length > 0) {
- const uint8_t c = *s++;
- boost::hash_combine(hash_val, case_sensitive ? c :
- isc::dns::name::internal::maptolower[c]);
- --length;
- }
- return (hash_val);
- }
- std::string
- LabelSequence::toText(bool omit_final_dot) const {
- const uint8_t* np = &data_[offsets_[first_label_]];
- const uint8_t* np_end = np + getDataLength();
-
- unsigned int labels = getLabelCount();
-
- unsigned int count = Name::MAX_LABELLEN + 1;
-
-
- std::string result;
- result.reserve(getDataLength());
- while (np != np_end) {
- labels--;
- count = *np++;
- if (count == 0) {
-
-
-
- if (!omit_final_dot || result.empty()) {
- result.push_back('.');
- }
- break;
- }
- if (count <= Name::MAX_LABELLEN) {
- assert(np_end - np >= count);
- if (!result.empty()) {
-
- result.push_back('.');
- }
- while (count-- > 0) {
- const uint8_t c = *np++;
- switch (c) {
- case 0x22:
- case 0x28:
- case 0x29:
- case 0x2E:
- case 0x3B:
- case 0x5C:
-
- case 0x40:
- case 0x24:
- result.push_back('\\');
- result.push_back(c);
- break;
- default:
- if (c > 0x20 && c < 0x7f) {
-
- result.push_back(c);
- } else {
-
- result.push_back(0x5c);
- result.push_back(0x30 + ((c / 100) % 10));
- result.push_back(0x30 + ((c / 10) % 10));
- result.push_back(0x30 + (c % 10));
- }
- }
- }
- } else {
- isc_throw(BadLabelType, "unknown label type in name data");
- }
- }
-
- assert(np == np_end);
- assert(labels == 0);
- return (result);
- }
- std::string
- LabelSequence::toText() const {
- return (toText(!isAbsolute()));
- }
- std::ostream&
- operator<<(std::ostream& os, const LabelSequence& label_sequence) {
- os << label_sequence.toText();
- return (os);
- }
- }
- }
|