18 #include <boost/shared_ptr.hpp>
93 position_(0),
data_(static_cast<const uint8_t*>(data)),
len_(len) {}
116 if (position >
len_) {
117 throwError(
"position is too large");
119 position_ = position;
131 if (position_ +
sizeof(uint8_t) >
len_) {
132 throwError(
"read beyond end of buffer");
135 return (
data_[position_++]);
146 if (position_ +
sizeof(data) >
len_) {
147 throwError(
"read beyond end of buffer");
150 cp = &
data_[position_];
151 data = ((
unsigned int)(cp[0])) << 8;
152 data |= ((
unsigned int)(cp[1]));
153 position_ +=
sizeof(data);
166 if (position_ +
sizeof(data) >
len_) {
167 throwError(
"read beyond end of buffer");
170 cp = &
data_[position_];
171 data = ((
unsigned int)(cp[0])) << 24;
172 data |= ((
unsigned int)(cp[1])) << 16;
173 data |= ((
unsigned int)(cp[2])) << 8;
174 data |= ((
unsigned int)(cp[3]));
175 position_ +=
sizeof(data);
187 if (position_ + len >
len_) {
188 throwError(
"read beyond end of buffer");
191 static_cast<void>(std::memmove(data, &
data_[position_], len));
205 if (position_ + len >
len_) {
206 throwError(
"read beyond end of buffer");
219 static void throwError(
const char* msg) {
310 if (allocated_ != 0) {
311 buffer_ =
static_cast<uint8_t*
>(malloc(allocated_));
312 if (buffer_ == NULL) {
313 throw std::bad_alloc();
328 allocated_(other.allocated_)
330 if (allocated_ != 0) {
331 buffer_ =
static_cast<uint8_t*
>(malloc(allocated_));
332 if (buffer_ == NULL) {
333 throw std::bad_alloc();
335 static_cast<void>(std::memmove(buffer_, other.buffer_, other.size_));
353 if (
this != &other) {
355 if (other.allocated_ != 0) {
362 uint8_t* newbuff =
static_cast<uint8_t*
>(malloc(other.allocated_));
363 if (newbuff == NULL) {
364 throw std::bad_alloc();
371 static_cast<void>(std::memmove(buffer_, other.buffer_, other.size_));
383 allocated_ = other.allocated_;
392 size_t getCapacity()
const {
return (allocated_); }
401 const void*
getData()
const {
return (buffer_); }
413 "[]: pos (" << pos <<
") >= size (" << size_ <<
")");
415 return (buffer_[pos]);
423 void skip(
size_t len) {
430 ensureAllocated(size_ + len);
457 if (buffer_ != NULL) {
458 static_cast<void>(std::memset(buffer_, 0, allocated_));
467 ensureAllocated(size_ + 1);
468 buffer_[size_ ++] = data;
480 if (pos +
sizeof(data) > size_) {
491 ensureAllocated(size_ +
sizeof(data));
492 buffer_[size_ ++] =
static_cast<uint8_t
>((data & 0xff00U) >> 8);
493 buffer_[size_ ++] =
static_cast<uint8_t
>(data & 0x00ffU);
508 if (pos +
sizeof(data) > size_) {
512 buffer_[pos] =
static_cast<uint8_t
>((data & 0xff00U) >> 8);
513 buffer_[pos + 1] =
static_cast<uint8_t
>(data & 0x00ffU);
521 ensureAllocated(size_ +
sizeof(data));
522 buffer_[size_ ++] =
static_cast<uint8_t
>((data & 0xff000000) >> 24);
523 buffer_[size_ ++] =
static_cast<uint8_t
>((data & 0x00ff0000) >> 16);
524 buffer_[size_ ++] =
static_cast<uint8_t
>((data & 0x0000ff00) >> 8);
525 buffer_[size_ ++] =
static_cast<uint8_t
>(data & 0x000000ff);
533 ensureAllocated(size_ +
sizeof(data));
534 buffer_[size_ ++] =
static_cast<uint8_t
>((data & 0xff00000000000000) >> 56);
535 buffer_[size_ ++] =
static_cast<uint8_t
>((data & 0x00ff000000000000) >> 48);
536 buffer_[size_ ++] =
static_cast<uint8_t
>((data & 0x0000ff0000000000) >> 40);
537 buffer_[size_ ++] =
static_cast<uint8_t
>((data & 0x000000ff00000000) >> 32);
538 buffer_[size_ ++] =
static_cast<uint8_t
>((data & 0x00000000ff000000) >> 24);
539 buffer_[size_ ++] =
static_cast<uint8_t
>((data & 0x0000000000ff0000) >> 16);
540 buffer_[size_ ++] =
static_cast<uint8_t
>((data & 0x000000000000ff00) >> 8);
541 buffer_[size_ ++] =
static_cast<uint8_t
>(data & 0x00000000000000ff);
551 ensureAllocated(size_ + len);
552 static_cast<void>(std::memmove(buffer_ + size_, data, len));
571 void ensureAllocated(
size_t needed_size) {
572 if (allocated_ < needed_size) {
574 size_t new_size = (allocated_ == 0) ? 1024 : allocated_;
575 while (new_size < needed_size) {
580 uint8_t* new_buffer_(static_cast<uint8_t*>(realloc(buffer_,
582 if (new_buffer_ == NULL) {
584 throw std::bad_alloc();
586 buffer_ = new_buffer_;
587 allocated_ = new_size;
const void * getData() const
Return a pointer to the head of the data stored in the buffer.
size_t getCapacity() const
Return the current capacity of the buffer.
void skip(size_t len)
Insert a specified length of gap at the end of the buffer.
void writeUint64(uint64_t data)
Write an unsigned 64-bit integer in host byte order into the buffer in network byte order...
void trim(size_t len)
Trim the specified length of data from the end of the buffer.
void writeData(const void *data, size_t len)
Copy an arbitrary length of data into the buffer.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
void writeUint16At(uint16_t data, size_t pos)
Write an unsigned 16-bit integer in host byte order at the specified position of the buffer in networ...
void wipe()
Wipe buffer content.
OutputBuffer(size_t len)
Constructor from the initial size of the buffer.
boost::shared_ptr< InputBuffer > InputBufferPtr
Pointer-like types pointing to InputBuffer or OutputBuffer.
void clear()
Clear buffer content.
A standard DNS module exception that is thrown if an out-of-range buffer operation is being performed...
uint8_t operator[](size_t pos) const
Return the value of the buffer at the specified position.
OutputBuffer(const OutputBuffer &other)
Copy constructor.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
void writeUint32(uint32_t data)
Write an unsigned 32-bit integer in host byte order into the buffer in network byte order...
This is a base class for exceptions thrown from the DNS library module.
Defines the logger used by the top-level component of kea-dhcp-ddns.
~OutputBuffer()
Destructor.
void writeUint8(uint8_t data)
Write an unsigned 8-bit integer into the buffer.
InvalidBufferPosition(const char *file, size_t line, const char *what)
void writeUint16(uint16_t data)
Write an unsigned 16-bit integer in host byte order into the buffer in network byte order...
A generic exception that is thrown if a parameter given to a method would refer to or modify out-of-r...
boost::shared_ptr< OutputBuffer > OutputBufferPtr
OutputBuffer & operator=(const OutputBuffer &other)
Assignment operator.
void writeUint8At(uint8_t data, size_t pos)
Write an unsigned 8-bit integer into the buffer.
size_t getLength() const
Return the length of data written in the buffer.