mirror of
https://gitlab.isc.org/isc-projects/kea
synced 2025-09-06 00:45:23 +00:00
93 lines
2.9 KiB
C++
93 lines
2.9 KiB
C++
![]() |
// 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>
|
||
|
|
||
|
// TODO: REMOVE
|
||
|
#include <iostream>
|
||
|
|
||
|
namespace isc {
|
||
|
namespace dns {
|
||
|
|
||
|
|
||
|
LabelSequence::LabelSequence(const Name& name) : name_(name),
|
||
|
first_label_(0) {
|
||
|
isc::util::OutputBuffer buf(0);
|
||
|
name.toWire(buf);
|
||
|
size_t buflen = buf.getLength();
|
||
|
data_ = new char[buflen];
|
||
|
memcpy(data_, buf.getData(), buflen);
|
||
|
data_[buflen-1] = '\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) {
|
||
|
offsets_[i] = offsets_[i - 1] + data_[offsets_[i - 1]] + 1;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
LabelSequence::~LabelSequence() {
|
||
|
delete[] data_;
|
||
|
delete[] offsets_;
|
||
|
}
|
||
|
|
||
|
const char*
|
||
|
LabelSequence::getData() const {
|
||
|
return &data_[offsets_[first_label_]];
|
||
|
}
|
||
|
|
||
|
const char*
|
||
|
LabelSequence::getData(size_t *len) const {
|
||
|
std::cout << "[XX]" << std::endl;
|
||
|
std::cout << "[XX] at: " << offsets_[first_label_] << std::endl;
|
||
|
std::cout << "[XX] value: " << name_.at(offsets_[first_label_]) << std::endl;
|
||
|
std::cout << "[XX] ptr: " << name_.at_p(offsets_[first_label_]) << std::endl;
|
||
|
*len = offsets_[last_label_] - offsets_[first_label_];
|
||
|
return name_.at_p(offsets_[first_label_]);
|
||
|
}
|
||
|
|
||
|
bool
|
||
|
LabelSequence::equals(const LabelSequence& other, bool case_sensitive) const {
|
||
|
if (case_sensitive) {
|
||
|
return (strcasecmp(getData(), other.getData()) == 0);
|
||
|
} else {
|
||
|
return (strcmp(getData(), other.getData()) == 0);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void
|
||
|
LabelSequence::split(int i) {
|
||
|
if (i > 0) {
|
||
|
if (first_label_ + i > last_label_) {
|
||
|
isc_throw(Exception, "split(" << i <<") but labelcount: " << getLabelCount());
|
||
|
} else {
|
||
|
first_label_ += i;
|
||
|
}
|
||
|
} else if (i < 0) {
|
||
|
if (last_label_ + i < first_label_) {
|
||
|
isc_throw(Exception, "split(" << i <<") but labelcount: " << getLabelCount());
|
||
|
} else {
|
||
|
last_label_ += i;
|
||
|
data_[offsets_[last_label_]] = '\0';
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
} // end namespace dns
|
||
|
} // end namespace isc
|
||
|
|