buffer->_position = 0;
buffer->_limit = buffer->_capacity = capacity;
buffer->_fixed = 0;
- buffer->_vfixed = 0;
buffer->_status_err = 0;
sldns_buffer_invariant(buffer);
buffer->_position = 0;
buffer->_limit = buffer->_capacity = size;
buffer->_fixed = 0;
- buffer->_vfixed = 0;
if (!buffer->_fixed && buffer->_data)
free(buffer->_data);
buffer->_data = malloc(size);
buffer->_data = data;
buffer->_capacity = buffer->_limit = size;
buffer->_fixed = 1;
- buffer->_vfixed = 0;
-}
-
-void
-sldns_buffer_init_vfixed_frm_data(sldns_buffer *buffer, void *data, size_t size)
-{
- memset(buffer, 0, sizeof(*buffer));
- buffer->_data = data;
- buffer->_capacity = buffer->_limit = size;
- buffer->_fixed = 1;
- buffer->_vfixed = 1;
}
int
if (written == -1) {
buffer->_status_err = 1;
return -1;
- } else if (!buffer->_vfixed && (size_t) written >= remaining) {
- if (!sldns_buffer_reserve(buffer, (size_t) written + 1)) {
- buffer->_status_err = 1;
- return -1;
- }
- va_start(args, format);
- written = vsnprintf((char *) sldns_buffer_current(buffer),
- sldns_buffer_remaining(buffer), format, args);
- va_end(args);
- if (written == -1) {
- buffer->_status_err = 1;
- return -1;
- }
}
buffer->_position += written;
}
free(buffer);
}
-void *
-sldns_buffer_export(sldns_buffer *buffer)
-{
- buffer->_fixed = 1;
- return buffer->_data;
-}
-
void
sldns_buffer_copy(sldns_buffer* result, sldns_buffer* from)
{
/** If the buffer is fixed it cannot be resized */
unsigned _fixed : 1;
- /** If the buffer is vfixed, no more than capacity bytes will be
- * written to _data, however the _position counter will be updated
- * with the amount that would have been written in consecutive
- * writes. This allows for a modus operandi in which a sequence is
- * written on a fixed capacity buffer (perhaps with _data on stack).
- * When everything could be written, then the _data is immediately
- * usable, if not, then a buffer could be allocated sized precisely
- * to fit the data for a second attempt.
- */
- unsigned _vfixed : 1;
-
/** The current state of the buffer. If writing to the buffer fails
* for any reason, this value is changed. This way, you can perform
* multiple writes in sequence and check for success afterwards. */
sldns_buffer_invariant(sldns_buffer *buffer)
{
assert(buffer != NULL);
- assert(buffer->_position <= buffer->_limit || buffer->_vfixed);
+ assert(buffer->_position <= buffer->_limit);
assert(buffer->_limit <= buffer->_capacity);
- assert(buffer->_data != NULL || (buffer->_vfixed && buffer->_capacity == 0 && buffer->_limit == 0));
+ assert(buffer->_data != NULL);
}
#endif
*/
void sldns_buffer_init_frm_data(sldns_buffer *buffer, void *data, size_t size);
-/**
- * Setup a buffer with the data pointed to. No data copied, no memory allocs.
- * The buffer is "virtually" fixed. Writes beyond size (the capacity) will
- * only update position, but no data will be written beyond capacity. This
- * allows to determine how big the buffer should have been to contain all the
- * written data, by looking at the position with sldns_buffer_position(),
- * similarly to the return value of POSIX's snprintf.
- * \param[in] buffer pointer to the buffer to put the data in
- * \param[in] data the data to encapsulate in the buffer
- * \param[in] size the size of the data
- */
-void sldns_buffer_init_vfixed_frm_data(sldns_buffer *buffer, void *data, size_t size);
-
/**
* clears the buffer and make it ready for writing. The buffer's limit
* is set to the capacity and the position is set to 0.
INLINE void
sldns_buffer_set_position(sldns_buffer *buffer, size_t mark)
{
- assert(mark <= buffer->_limit || buffer->_vfixed);
+ assert(mark <= buffer->_limit);
buffer->_position = mark;
}
INLINE void
sldns_buffer_skip(sldns_buffer *buffer, ssize_t count)
{
- assert(buffer->_position + count <= buffer->_limit || buffer->_vfixed);
+ assert(buffer->_position + count <= buffer->_limit);
buffer->_position += count;
}
INLINE uint8_t *
sldns_buffer_at(const sldns_buffer *buffer, size_t at)
{
- assert(at <= buffer->_limit || buffer->_vfixed);
+ assert(at <= buffer->_limit);
return buffer->_data + at;
}
sldns_buffer_remaining_at(sldns_buffer *buffer, size_t at)
{
sldns_buffer_invariant(buffer);
- assert(at <= buffer->_limit || buffer->_vfixed);
+ assert(at <= buffer->_limit);
return at < buffer->_limit ? buffer->_limit - at : 0;
}
INLINE void
sldns_buffer_write_at(sldns_buffer *buffer, size_t at, const void *data, size_t count)
{
- if (!buffer->_vfixed)
- assert(sldns_buffer_available_at(buffer, at, count));
- else if (sldns_buffer_remaining_at(buffer, at) == 0)
- return;
- else if (count > sldns_buffer_remaining_at(buffer, at)) {
- memcpy(buffer->_data + at, data,
- sldns_buffer_remaining_at(buffer, at));
- return;
- }
+ assert(sldns_buffer_available_at(buffer, at, count));
memcpy(buffer->_data + at, data, count);
}
INLINE void
sldns_buffer_set_at(sldns_buffer *buffer, size_t at, int c, size_t count)
{
- if (!buffer->_vfixed)
- assert(sldns_buffer_available_at(buffer, at, count));
- else if (sldns_buffer_remaining_at(buffer, at) == 0)
- return;
- else if (count > sldns_buffer_remaining_at(buffer, at)) {
- memset(buffer->_data + at, c,
- sldns_buffer_remaining_at(buffer, at));
- return;
- }
+ assert(sldns_buffer_available_at(buffer, at, count));
memset(buffer->_data + at, c, count);
}
INLINE void
sldns_buffer_write_u8_at(sldns_buffer *buffer, size_t at, uint8_t data)
{
- if (buffer->_vfixed && at + sizeof(data) > buffer->_limit) return;
assert(sldns_buffer_available_at(buffer, at, sizeof(data)));
buffer->_data[at] = data;
}
INLINE void
sldns_buffer_write_u16_at(sldns_buffer *buffer, size_t at, uint16_t data)
{
- if (buffer->_vfixed && at + sizeof(data) > buffer->_limit) return;
assert(sldns_buffer_available_at(buffer, at, sizeof(data)));
sldns_write_uint16(buffer->_data + at, data);
}
INLINE void
sldns_buffer_write_u32_at(sldns_buffer *buffer, size_t at, uint32_t data)
{
- if (buffer->_vfixed && at + sizeof(data) > buffer->_limit) return;
assert(sldns_buffer_available_at(buffer, at, sizeof(data)));
sldns_write_uint32(buffer->_data + at, data);
}
INLINE void
sldns_buffer_write_u48_at(sldns_buffer *buffer, size_t at, uint64_t data)
{
- if (buffer->_vfixed && at + 6 > buffer->_limit) return;
assert(sldns_buffer_available_at(buffer, at, 6));
sldns_write_uint48(buffer->_data + at, data);
}
*/
void sldns_buffer_free(sldns_buffer *buffer);
-/**
- * Makes the buffer fixed and returns a pointer to the data. The
- * caller is responsible for free'ing the result.
- * \param[in] *buffer the buffer to be exported
- * \return void
- */
-void *sldns_buffer_export(sldns_buffer *buffer);
-
/**
* Copy contents of the from buffer to the result buffer and then flips
* the result buffer. Data will be silently truncated if the result buffer is