2012-02-20 23:29:31 +01:00
|
|
|
// Copyright (C) 2009 Internet Systems Consortium, Inc. ("ISC")
|
|
|
|
//
|
|
|
|
// 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>
|
|
|
|
#include <exceptions/exceptions.h>
|
|
|
|
|
|
|
|
namespace isc {
|
|
|
|
namespace dns {
|
|
|
|
|
|
|
|
LabelSequence::LabelSequence(const Name& name) : name_(name),
|
|
|
|
first_label_(0) {
|
|
|
|
size_t label_count_ = name.getLabelCount();
|
|
|
|
last_label_ = label_count_ - 1;
|
|
|
|
offsets_ = new size_t[label_count_];
|
|
|
|
offsets_[0] = 0;
|
|
|
|
for (size_t i = 1; i < label_count_; ++i) {
|
2012-02-21 12:37:07 +01:00
|
|
|
offsets_[i] = offsets_[i - 1] + name.at(offsets_[i - 1]) + 1;
|
2012-02-20 23:29:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LabelSequence::~LabelSequence() {
|
|
|
|
delete[] offsets_;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char*
|
|
|
|
LabelSequence::getData(size_t *len) const {
|
|
|
|
*len = offsets_[last_label_] - offsets_[first_label_];
|
2012-02-21 12:37:07 +01:00
|
|
|
return (name_.at_p(offsets_[first_label_]));
|
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 (strncasecmp(data, other_data, len) == 0);
|
2012-02-20 23:29:31 +01:00
|
|
|
} else {
|
2012-02-21 12:37:07 +01:00
|
|
|
return (strncmp(data, other_data, len) == 0);
|
2012-02-20 23:29:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
LabelSequence::split(int i) {
|
|
|
|
if (i > 0) {
|
2012-02-21 12:37:07 +01:00
|
|
|
if (i > getLabelCount()) {
|
|
|
|
isc_throw(OutOfRange, "Label " << i << " out of range (" <<
|
|
|
|
getLabelCount() << ")");
|
2012-02-20 23:29:31 +01:00
|
|
|
} else {
|
|
|
|
first_label_ += i;
|
|
|
|
}
|
|
|
|
} else if (i < 0) {
|
2012-02-21 12:37:07 +01:00
|
|
|
if (-i > getLabelCount()) {
|
|
|
|
isc_throw(OutOfRange, "Label " << i << " out of range (" <<
|
|
|
|
getLabelCount() << ")");
|
2012-02-20 23:29:31 +01:00
|
|
|
} else {
|
|
|
|
last_label_ += i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end namespace dns
|
|
|
|
} // end namespace isc
|
|
|
|
|