33 #define LDNS_MIN_BUFLEN 512
84 assert(buffer != NULL);
87 assert(buffer->
_data != NULL);
117 ldns_buffer_invariant(buffer);
135 ldns_buffer_invariant(buffer);
148 ldns_buffer_invariant(buffer);
171 ldns_buffer_set_position(
ldns_buffer *buffer,
size_t mark)
173 assert(mark <= buffer->
_limit);
185 ldns_buffer_skip(
ldns_buffer *buffer, ssize_t count)
209 ldns_buffer_set_limit(
ldns_buffer *buffer,
size_t limit)
257 ldns_buffer_at(
const ldns_buffer *buffer,
size_t at)
259 assert(at <= buffer->
_limit);
260 return buffer->
_data + at;
272 return ldns_buffer_at(buffer, 0);
284 return ldns_buffer_at(buffer, buffer->
_limit);
295 return ldns_buffer_at(buffer, buffer->
_position);
306 ldns_buffer_remaining_at(
const ldns_buffer *buffer,
size_t at)
308 ldns_buffer_invariant(buffer);
309 assert(at <= buffer->
_limit);
310 return buffer->
_limit - at;
322 return ldns_buffer_remaining_at(buffer, buffer->
_position);
335 ldns_buffer_available_at(
const ldns_buffer *buffer,
size_t at,
size_t count)
337 return count <= ldns_buffer_remaining_at(buffer, at);
347 ldns_buffer_available(
const ldns_buffer *buffer,
size_t count)
349 return ldns_buffer_available_at(buffer, buffer->
_position, count);
360 ldns_buffer_write_at(
ldns_buffer *buffer,
size_t at,
const void *data,
size_t count)
362 assert(ldns_buffer_available_at(buffer, at, count));
363 memcpy(buffer->
_data + at, data, count);
373 ldns_buffer_write(
ldns_buffer *buffer,
const void *data,
size_t count)
375 ldns_buffer_write_at(buffer, buffer->
_position, data, count);
386 ldns_buffer_write_string_at(
ldns_buffer *buffer,
size_t at,
const char *str)
388 ldns_buffer_write_at(buffer, at, str, strlen(str));
397 ldns_buffer_write_string(
ldns_buffer *buffer,
const char *str)
399 ldns_buffer_write(buffer, str, strlen(str));
409 ldns_buffer_write_chars(
ldns_buffer *buffer,
const char *str)
414 ldns_buffer_write_string(buffer, str);
425 ldns_buffer_write_u8_at(
ldns_buffer *buffer,
size_t at, uint8_t data)
427 assert(ldns_buffer_available_at(buffer, at,
sizeof(data)));
428 buffer->
_data[at] = data;
437 ldns_buffer_write_u8(
ldns_buffer *buffer, uint8_t data)
439 ldns_buffer_write_u8_at(buffer, buffer->
_position, data);
450 ldns_buffer_write_char(
ldns_buffer *buffer, uint8_t data)
455 ldns_buffer_write_u8(buffer, data);
465 ldns_buffer_write_u16_at(
ldns_buffer *buffer,
size_t at, uint16_t data)
467 assert(ldns_buffer_available_at(buffer, at,
sizeof(data)));
468 ldns_write_uint16(buffer->
_data + at, data);
477 ldns_buffer_write_u16(
ldns_buffer *buffer, uint16_t data)
479 ldns_buffer_write_u16_at(buffer, buffer->
_position, data);
490 ldns_buffer_write_u32_at(
ldns_buffer *buffer,
size_t at, uint32_t data)
492 assert(ldns_buffer_available_at(buffer, at,
sizeof(data)));
493 ldns_write_uint32(buffer->
_data + at, data);
502 ldns_buffer_write_u32(
ldns_buffer *buffer, uint32_t data)
504 ldns_buffer_write_u32_at(buffer, buffer->
_position, data);
516 ldns_buffer_read_at(
const ldns_buffer *buffer,
size_t at,
void *data,
size_t count)
518 assert(ldns_buffer_available_at(buffer, at, count));
519 memcpy(data, buffer->
_data + at, count);
529 ldns_buffer_read(
ldns_buffer *buffer,
void *data,
size_t count)
531 ldns_buffer_read_at(buffer, buffer->
_position, data, count);
542 ldns_buffer_read_u8_at(
const ldns_buffer *buffer,
size_t at)
544 assert(ldns_buffer_available_at(buffer, at,
sizeof(uint8_t)));
545 return buffer->
_data[at];
556 uint8_t result = ldns_buffer_read_u8_at(buffer, buffer->
_position);
568 ldns_buffer_read_u16_at(
ldns_buffer *buffer,
size_t at)
570 assert(ldns_buffer_available_at(buffer, at,
sizeof(uint16_t)));
571 return ldns_read_uint16(buffer->
_data + at);
582 uint16_t result = ldns_buffer_read_u16_at(buffer, buffer->
_position);
594 ldns_buffer_read_u32_at(
ldns_buffer *buffer,
size_t at)
596 assert(ldns_buffer_available_at(buffer, at,
sizeof(uint32_t)));
597 return ldns_read_uint32(buffer->
_data + at);
608 uint32_t result = ldns_buffer_read_u32_at(buffer, buffer->
_position);
void ldns_buffer_free(ldns_buffer *buffer)
frees the buffer.
void ldns_buffer_copy(ldns_buffer *result, const ldns_buffer *from)
Copy contents of the from buffer to the result buffer and then flips the result buffer.
signed char ldns_buffer_set_capacity(ldns_buffer *buffer, size_t capacity)
changes the buffer's capacity.
signed char ldns_buffer_reserve(ldns_buffer *buffer, size_t amount)
ensures BUFFER can contain at least AMOUNT more bytes.
ldns_buffer * ldns_buffer_new(size_t capacity)
creates a new buffer with the specified capacity.
int ldns_buffer_printf(ldns_buffer *buffer, const char *format,...)
prints to the buffer, increasing the capacity if required using buffer_reserve().
void ldns_buffer_new_frm_data(ldns_buffer *buffer, const void *data, size_t size)
creates a buffer with the specified data.
void * ldns_buffer_export(ldns_buffer *buffer)
Makes the buffer fixed and returns a pointer to the data.
Common definitions for LDNS.
Defines error numbers and functions to translate those to a readable string.
enum ldns_enum_status ldns_status
implementation of buffers to ease operations
size_t _capacity
The amount of data the buffer can contain.
unsigned _fixed
If the buffer is fixed it cannot be resized.
size_t _position
The current position used for reading/writing.
ldns_status _status
The current state of the buffer.
uint8_t * _data
The data contained in the buffer.
size_t _limit
The read/write limit.
#define INLINE
splint static inline workaround