17#include <boost/array.hpp>
40 OffsetItem(
size_t hash,
size_t pos,
size_t len) :
41 hash_(hash), pos_(pos), len_(len) {
64template <
bool CASE_SENSITIVE>
72 NameCompare(
const OutputBuffer& buffer, InputBuffer& name_buf,
74 buffer_(&buffer), name_buf_(&name_buf), hash_(hash) {
77 bool operator()(
const OffsetItem& item)
const {
80 if (item.hash_ != hash_ || item.len_ != name_buf_->getLength()) {
92 name_buf_->setPosition(0);
93 uint16_t item_pos = item.pos_;
94 uint16_t item_label_len = 0;
95 for (
size_t i = 0; i < item.len_; ++i, ++item_pos) {
96 item_pos = nextPosition(*buffer_, item_pos, item_label_len);
97 const uint8_t ch1 = (*buffer_)[item_pos];
98 const uint8_t ch2 = name_buf_->readUint8();
114 static uint16_t nextPosition(
const OutputBuffer& buffer,
115 uint16_t pos, uint16_t& llen) {
121 pos = (buffer[pos] & ~Name::COMPRESS_POINTER_MARK8) *
122 256 + buffer[pos + 1];
138 const OutputBuffer* buffer_;
139 InputBuffer* name_buf_;
168 for (
size_t i = 0; i <
BUCKETS; ++i) {
174 size_t hash,
bool case_sensitive)
const {
180 const size_t bucket_id = hash %
BUCKETS;
182 if (case_sensitive) {
183 found = find_if(
table_[bucket_id].rbegin(),
185 NameCompare<true>(buffer, name_buf, hash));
187 found = find_if(
table_[bucket_id].rbegin(),
189 NameCompare<false>(buffer, name_buf, hash));
191 if (found !=
table_[bucket_id].rend()) {
192 return (found->pos_);
197 void addOffset(
size_t hash,
size_t offset,
size_t len) {
198 table_[hash %
BUCKETS].push_back(OffsetItem(hash, offset, len));
229 impl_->msglength_limit_ = 512;
230 impl_->truncated_ =
false;
239 vector<OffsetItem> new_table;
241 new_table.swap(impl_->table_[i]);
243 impl_->table_[i].clear();
249 return (impl_->msglength_limit_);
254 impl_->msglength_limit_ = len;
259 return (impl_->truncated_);
264 impl_->truncated_ =
true;
269 return (impl_->compress_mode_);
276 "compress mode cannot be changed during rendering");
278 impl_->compress_mode_ = mode;
290 size_t nlabels_uncomp;
292 const bool case_sensitive = (impl_->compress_mode_ ==
294 for (nlabels_uncomp = 0; nlabels_uncomp < nlabels; ++nlabels_uncomp) {
295 if (nlabels_uncomp > 0) {
305 impl_->seq_hashes_.at(nlabels_uncomp) =
306 sequence.
getHash(impl_->compress_mode_);
308 ptr_offset = impl_->findOffset(
getBuffer(), name_buf,
309 impl_->seq_hashes_[nlabels_uncomp],
319 if (nlabels_uncomp > 0 || !compress) {
321 if (compress && nlabels > nlabels_uncomp) {
323 uncomp_sequence.
stripRight(nlabels - nlabels_uncomp);
339 for (
size_t i = 0; i < nlabels_uncomp; ++i) {
340 const uint8_t label_len =
getBuffer()[offset];
341 if (label_len == 0) {
349 impl_->addOffset(impl_->seq_hashes_[i], offset, seqlen);
350 offset += (label_len + 1);
351 seqlen -= (label_len + 1);
362 local_buffer_(0), buffer_(&local_buffer_) {
367 if (buffer && buffer_->
getLength() != 0) {
369 "MessageRenderer buffer cannot be set when in use");
371 if (!buffer && buffer_ == &local_buffer_) {
373 "Default MessageRenderer buffer cannot be reset");
379 buffer_ = &local_buffer_;
A generic exception that is thrown if a parameter given to a method or function is considered invalid...
AbstractMessageRenderer()
The default constructor.
size_t getLength() const
Return the length of data written in the internal buffer.
virtual void clear()
Clear the internal buffer and other internal resources.
const isc::util::OutputBuffer & getBuffer() const
Return the output buffer we render into.
void writeUint16(uint16_t data)
Write an unsigned 16-bit integer in host byte order into the internal buffer in network byte order.
CompressMode
Compression mode constants.
void setBuffer(isc::util::OutputBuffer *buffer)
Set or reset a temporary output buffer.
void writeData(const void *data, size_t len)
Copy an arbitrary length of data into the internal buffer of the renderer object.
Light-weight Accessor to Name data.
size_t getHash(bool case_sensitive) const
Calculate a simple hash for the label sequence.
size_t getLabelCount() const
Returns the current number of labels for this LabelSequence.
void stripLeft(size_t i)
Remove labels from the front of this LabelSequence.
const uint8_t * getData(size_t *len) const
Return the wire-format data for this LabelSequence.
void stripRight(size_t i)
Remove labels from the end of this LabelSequence.
size_t getDataLength() const
Return the length of the wire-format data of this LabelSequence.
@ CASE_SENSITIVE
Compress names case-sensitive manner.
virtual CompressMode getCompressMode() const
Return the compression mode of the renderer class object.
virtual void setLengthLimit(size_t len)
Set the maximum length of rendered data that can fit in the corresponding DNS message without truncat...
virtual void clear()
Clear the internal buffer and other internal resources.
virtual ~MessageRenderer()
virtual void setTruncated()
Mark the renderer to indicate truncation has occurred while rendering.
virtual bool isTruncated() const
Return whether truncation has occurred while rendering.
virtual void writeName(const Name &name, bool compress=true)
Write a Name object into the internal buffer in wire format, with or without name compression.
virtual size_t getLengthLimit() const
Return the maximum length of rendered data that can fit in the corresponding DNS message without trun...
virtual void setCompressMode(CompressMode mode)
This implementation does not allow this call in the middle of rendering (i.e.
@ CASE_INSENSITIVE
Compress names case-insensitive manner (default).
The Name class encapsulates DNS names.
static const uint16_t MAX_COMPRESS_POINTER
Max possible pointer value for name compression.
static const uint16_t COMPRESS_POINTER_MARK16
A 16-bit masked value indicating a start of compression pointer.
static const size_t MAX_WIRE
Max allowable length of domain names.
static const uint16_t COMPRESS_POINTER_MARK8
A 8-bit masked value indicating a start of compression pointer.
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
size_t getLength() const
Return the length of data written in the buffer.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
#define isc_throw_assert(expr)
Replacement for assert() that throws if the expression is false.
const uint8_t maptolower[]
const uint8_t maptolower[]
Defines the logger used by the top-level component of kea-lfc.
The MessageRendererImpl class is the actual implementation of MessageRenderer.
boost::array< size_t, Name::MAX_LABELS > seq_hashes_
void addOffset(size_t hash, size_t offset, size_t len)
static const size_t RESERVED_ITEMS
static const size_t BUCKETS
uint16_t findOffset(const OutputBuffer &buffer, InputBuffer &name_buf, size_t hash, bool case_sensitive) const
static const uint16_t NO_OFFSET
vector< OffsetItem > table_[BUCKETS]
CompressMode compress_mode_
The name compression mode.
bool truncated_
A boolean flag that indicates truncation has occurred while rendering the data.
MessageRendererImpl()
Constructor.
uint16_t msglength_limit_
The maximum length of rendered data that can fit without truncation.