Lines Matching full:buffer

2  * buffer.h -- generic memory buffer.
9 * The buffer module implements a generic buffer. The API is based on
10 * the java.nio.Buffer interface.
113 * to the current end of a buffer, read from the current position, and
124 /** The amount of data the buffer can contain */
127 /** The data contained in the buffer */
130 /** If the buffer is fixed it cannot be resized */
133 /** The current state of the buffer. If writing to the buffer fails
142 sldns_buffer_invariant(sldns_buffer *ATTR_UNUSED(buffer)) in sldns_buffer_invariant() argument
147 sldns_buffer_invariant(sldns_buffer *buffer) in sldns_buffer_invariant() argument
149 assert(buffer != NULL); in sldns_buffer_invariant()
150 assert(buffer->_position <= buffer->_limit); in sldns_buffer_invariant()
151 assert(buffer->_limit <= buffer->_capacity); in sldns_buffer_invariant()
152 assert(buffer->_data != NULL); in sldns_buffer_invariant()
157 * creates a new buffer with the specified capacity.
159 * \param[in] capacity the size (in bytes) to allocate for the buffer
160 * \return the created buffer
165 * creates a buffer with the specified data. The data IS copied
166 * and MEMORY allocations are done. The buffer is not fixed and can
169 * \param[in] buffer pointer to the buffer to put the data in
170 * \param[in] data the data to encapsulate in the buffer
173 void sldns_buffer_new_frm_data(sldns_buffer *buffer, void *data, size_t size);
176 * Setup a buffer with the data pointed to. No data copied, no memory allocs.
177 * The buffer is fixed.
178 * \param[in] buffer pointer to the buffer to put the data in
179 * \param[in] data the data to encapsulate in the buffer
182 void sldns_buffer_init_frm_data(sldns_buffer *buffer, void *data, size_t size);
185 * clears the buffer and make it ready for writing. The buffer's limit
187 * \param[in] buffer the buffer to clear
189 INLINE void sldns_buffer_clear(sldns_buffer *buffer) in sldns_buffer_clear() argument
191 sldns_buffer_invariant(buffer); in sldns_buffer_clear()
195 buffer->_position = 0; in sldns_buffer_clear()
196 buffer->_limit = buffer->_capacity; in sldns_buffer_clear()
200 * makes the buffer ready for reading the data that has been written to
201 * the buffer. The buffer's limit is set to the current position and
204 * \param[in] buffer the buffer to flip
206 INLINE void sldns_buffer_flip(sldns_buffer *buffer) in sldns_buffer_flip() argument
208 sldns_buffer_invariant(buffer); in sldns_buffer_flip()
210 buffer->_limit = buffer->_position; in sldns_buffer_flip()
211 buffer->_position = 0; in sldns_buffer_flip()
215 * make the buffer ready for re-reading the data. The buffer's
217 * \param[in] buffer the buffer to rewind
219 INLINE void sldns_buffer_rewind(sldns_buffer *buffer) in sldns_buffer_rewind() argument
221 sldns_buffer_invariant(buffer); in sldns_buffer_rewind()
223 buffer->_position = 0; in sldns_buffer_rewind()
227 * returns the current position in the buffer (as a number of bytes)
228 * \param[in] buffer the buffer
232 sldns_buffer_position(sldns_buffer *buffer) in sldns_buffer_position() argument
234 return buffer->_position; in sldns_buffer_position()
238 * sets the buffer's position to MARK. The position must be less than
239 * or equal to the buffer's limit.
240 * \param[in] buffer the buffer
244 sldns_buffer_set_position(sldns_buffer *buffer, size_t mark) in sldns_buffer_set_position() argument
246 assert(mark <= buffer->_limit); in sldns_buffer_set_position()
247 buffer->_position = mark; in sldns_buffer_set_position()
251 * changes the buffer's position by COUNT bytes. The position must not
252 * be moved behind the buffer's limit or before the beginning of the
253 * buffer.
254 * \param[in] buffer the buffer
258 sldns_buffer_skip(sldns_buffer *buffer, ssize_t count) in sldns_buffer_skip() argument
260 assert(buffer->_position + count <= buffer->_limit); in sldns_buffer_skip()
261 buffer->_position += count; in sldns_buffer_skip()
265 * returns the maximum size of the buffer
266 * \param[in] buffer
270 sldns_buffer_limit(sldns_buffer *buffer) in sldns_buffer_limit() argument
272 return buffer->_limit; in sldns_buffer_limit()
276 * changes the buffer's limit. If the buffer's position is greater
278 * \param[in] buffer the buffer
282 sldns_buffer_set_limit(sldns_buffer *buffer, size_t limit) in sldns_buffer_set_limit() argument
284 assert(limit <= buffer->_capacity); in sldns_buffer_set_limit()
285 buffer->_limit = limit; in sldns_buffer_set_limit()
286 if (buffer->_position > buffer->_limit) in sldns_buffer_set_limit()
287 buffer->_position = buffer->_limit; in sldns_buffer_set_limit()
291 * returns the number of bytes the buffer can hold.
292 * \param[in] buffer the buffer
296 sldns_buffer_capacity(sldns_buffer *buffer) in sldns_buffer_capacity() argument
298 return buffer->_capacity; in sldns_buffer_capacity()
302 * changes the buffer's capacity. The data is reallocated so any
303 * pointers to the data may become invalid. The buffer's limit is set
304 * to the buffer's new capacity.
305 * \param[in] buffer the buffer
309 int sldns_buffer_set_capacity(sldns_buffer *buffer, size_t capacity);
312 * ensures BUFFER can contain at least AMOUNT more bytes. The buffer's
315 * The buffer's limit is always set to the (possibly increased)
317 * \param[in] buffer the buffer
321 int sldns_buffer_reserve(sldns_buffer *buffer, size_t amount);
325 * \param[in] buffer the buffer
330 sldns_buffer_at(const sldns_buffer *buffer, size_t at) in sldns_buffer_at() argument
332 assert(at <= buffer->_limit); in sldns_buffer_at()
333 return buffer->_data + at; in sldns_buffer_at()
337 * returns a pointer to the beginning of the buffer (the data at
339 * \param[in] buffer the buffer
343 sldns_buffer_begin(const sldns_buffer *buffer) in sldns_buffer_begin() argument
345 return sldns_buffer_at(buffer, 0); in sldns_buffer_begin()
349 * returns a pointer to the end of the buffer (the data at the buffer's
351 * \param[in] buffer the buffer
355 sldns_buffer_end(sldns_buffer *buffer) in sldns_buffer_end() argument
357 return sldns_buffer_at(buffer, buffer->_limit); in sldns_buffer_end()
361 * returns a pointer to the data at the buffer's current position.
362 * \param[in] buffer the buffer
366 sldns_buffer_current(sldns_buffer *buffer) in sldns_buffer_current() argument
368 return sldns_buffer_at(buffer, buffer->_position); in sldns_buffer_current()
374 * \param[in] buffer the buffer
379 sldns_buffer_remaining_at(sldns_buffer *buffer, size_t at) in sldns_buffer_remaining_at() argument
381 sldns_buffer_invariant(buffer); in sldns_buffer_remaining_at()
382 assert(at <= buffer->_limit); in sldns_buffer_remaining_at()
383 return at < buffer->_limit ? buffer->_limit - at : 0; in sldns_buffer_remaining_at()
387 * returns the number of bytes remaining between the buffer's position and
389 * \param[in] buffer the buffer
393 sldns_buffer_remaining(sldns_buffer *buffer) in sldns_buffer_remaining() argument
395 return sldns_buffer_remaining_at(buffer, buffer->_position); in sldns_buffer_remaining()
399 * checks if the buffer has at least COUNT more bytes available.
402 * \param[in] buffer the buffer
408 sldns_buffer_available_at(sldns_buffer *buffer, size_t at, size_t count) in sldns_buffer_available_at() argument
410 return count <= sldns_buffer_remaining_at(buffer, at); in sldns_buffer_available_at()
414 * checks if the buffer has count bytes available at the current position
415 * \param[in] buffer the buffer
420 sldns_buffer_available(sldns_buffer *buffer, size_t count) in sldns_buffer_available() argument
422 return sldns_buffer_available_at(buffer, buffer->_position, count); in sldns_buffer_available()
426 * writes the given data to the buffer at the specified position
427 * \param[in] buffer the buffer
429 * \param[in] data pointer to the data to write to the buffer
433 sldns_buffer_write_at(sldns_buffer *buffer, size_t at, const void *data, size_t count) in sldns_buffer_write_at() argument
435 assert(sldns_buffer_available_at(buffer, at, count)); in sldns_buffer_write_at()
436 memcpy(buffer->_data + at, data, count); in sldns_buffer_write_at()
440 * set the given byte to the buffer at the specified position
441 * \param[in] buffer the buffer
443 * \param[in] c the byte to set to the buffer
448 sldns_buffer_set_at(sldns_buffer *buffer, size_t at, int c, size_t count) in sldns_buffer_set_at() argument
450 assert(sldns_buffer_available_at(buffer, at, count)); in sldns_buffer_set_at()
451 memset(buffer->_data + at, c, count); in sldns_buffer_set_at()
456 * writes count bytes of data to the current position of the buffer
457 * \param[in] buffer the buffer
462 sldns_buffer_write(sldns_buffer *buffer, const void *data, size_t count) in sldns_buffer_write() argument
464 sldns_buffer_write_at(buffer, buffer->_position, data, count); in sldns_buffer_write()
465 buffer->_position += count; in sldns_buffer_write()
469 * copies the given (null-delimited) string to the specified position at the buffer
470 * \param[in] buffer the buffer
471 * \param[in] at the position in the buffer
475 sldns_buffer_write_string_at(sldns_buffer *buffer, size_t at, const char *str) in sldns_buffer_write_string_at() argument
477 sldns_buffer_write_at(buffer, at, str, strlen(str)); in sldns_buffer_write_string_at()
481 * copies the given (null-delimited) string to the current position at the buffer
482 * \param[in] buffer the buffer
486 sldns_buffer_write_string(sldns_buffer *buffer, const char *str) in sldns_buffer_write_string() argument
488 sldns_buffer_write(buffer, str, strlen(str)); in sldns_buffer_write_string()
492 * writes the given byte of data at the given position in the buffer
493 * \param[in] buffer the buffer
494 * \param[in] at the position in the buffer
498 sldns_buffer_write_u8_at(sldns_buffer *buffer, size_t at, uint8_t data) in sldns_buffer_write_u8_at() argument
500 assert(sldns_buffer_available_at(buffer, at, sizeof(data))); in sldns_buffer_write_u8_at()
501 buffer->_data[at] = data; in sldns_buffer_write_u8_at()
505 * writes the given byte of data at the current position in the buffer
506 * \param[in] buffer the buffer
510 sldns_buffer_write_u8(sldns_buffer *buffer, uint8_t data) in sldns_buffer_write_u8() argument
512 sldns_buffer_write_u8_at(buffer, buffer->_position, data); in sldns_buffer_write_u8()
513 buffer->_position += sizeof(data); in sldns_buffer_write_u8()
517 * writes the given 2 byte integer at the given position in the buffer
518 * \param[in] buffer the buffer
519 * \param[in] at the position in the buffer
523 sldns_buffer_write_u16_at(sldns_buffer *buffer, size_t at, uint16_t data) in sldns_buffer_write_u16_at() argument
525 assert(sldns_buffer_available_at(buffer, at, sizeof(data))); in sldns_buffer_write_u16_at()
526 sldns_write_uint16(buffer->_data + at, data); in sldns_buffer_write_u16_at()
530 * writes the given 2 byte integer at the current position in the buffer
531 * \param[in] buffer the buffer
535 sldns_buffer_write_u16(sldns_buffer *buffer, uint16_t data) in sldns_buffer_write_u16() argument
537 sldns_buffer_write_u16_at(buffer, buffer->_position, data); in sldns_buffer_write_u16()
538 buffer->_position += sizeof(data); in sldns_buffer_write_u16()
542 * writes the given 4 byte integer at the given position in the buffer
543 * \param[in] buffer the buffer
544 * \param[in] at the position in the buffer
548 sldns_buffer_write_u32_at(sldns_buffer *buffer, size_t at, uint32_t data) in sldns_buffer_write_u32_at() argument
550 assert(sldns_buffer_available_at(buffer, at, sizeof(data))); in sldns_buffer_write_u32_at()
551 sldns_write_uint32(buffer->_data + at, data); in sldns_buffer_write_u32_at()
555 * writes the given 6 byte integer at the given position in the buffer
556 * \param[in] buffer the buffer
557 * \param[in] at the position in the buffer
561 sldns_buffer_write_u48_at(sldns_buffer *buffer, size_t at, uint64_t data) in sldns_buffer_write_u48_at() argument
563 assert(sldns_buffer_available_at(buffer, at, 6)); in sldns_buffer_write_u48_at()
564 sldns_write_uint48(buffer->_data + at, data); in sldns_buffer_write_u48_at()
568 * writes the given 4 byte integer at the current position in the buffer
569 * \param[in] buffer the buffer
573 sldns_buffer_write_u32(sldns_buffer *buffer, uint32_t data) in sldns_buffer_write_u32() argument
575 sldns_buffer_write_u32_at(buffer, buffer->_position, data); in sldns_buffer_write_u32()
576 buffer->_position += sizeof(data); in sldns_buffer_write_u32()
580 * writes the given 6 byte integer at the current position in the buffer
581 * \param[in] buffer the buffer
585 sldns_buffer_write_u48(sldns_buffer *buffer, uint64_t data) in sldns_buffer_write_u48() argument
587 sldns_buffer_write_u48_at(buffer, buffer->_position, data); in sldns_buffer_write_u48()
588 buffer->_position += 6; in sldns_buffer_write_u48()
593 * \param[in] buffer the buffer
594 * \param[in] at the position in the buffer to start
595 * \param[out] data buffer to copy to
599 sldns_buffer_read_at(sldns_buffer *buffer, size_t at, void *data, size_t count) in sldns_buffer_read_at() argument
601 assert(sldns_buffer_available_at(buffer, at, count)); in sldns_buffer_read_at()
602 memcpy(data, buffer->_data + at, count); in sldns_buffer_read_at()
607 * \param[in] buffer the buffer
608 * \param[out] data buffer to copy to
612 sldns_buffer_read(sldns_buffer *buffer, void *data, size_t count) in sldns_buffer_read() argument
614 sldns_buffer_read_at(buffer, buffer->_position, data, count); in sldns_buffer_read()
615 buffer->_position += count; in sldns_buffer_read()
619 * returns the byte value at the given position in the buffer
620 * \param[in] buffer the buffer
621 * \param[in] at the position in the buffer
625 sldns_buffer_read_u8_at(sldns_buffer *buffer, size_t at) in sldns_buffer_read_u8_at() argument
627 assert(sldns_buffer_available_at(buffer, at, sizeof(uint8_t))); in sldns_buffer_read_u8_at()
628 return buffer->_data[at]; in sldns_buffer_read_u8_at()
632 * returns the byte value at the current position in the buffer
633 * \param[in] buffer the buffer
637 sldns_buffer_read_u8(sldns_buffer *buffer) in sldns_buffer_read_u8() argument
639 uint8_t result = sldns_buffer_read_u8_at(buffer, buffer->_position); in sldns_buffer_read_u8()
640 buffer->_position += sizeof(uint8_t); in sldns_buffer_read_u8()
645 * returns the 2-byte integer value at the given position in the buffer
646 * \param[in] buffer the buffer
647 * \param[in] at position in the buffer
651 sldns_buffer_read_u16_at(sldns_buffer *buffer, size_t at) in sldns_buffer_read_u16_at() argument
653 assert(sldns_buffer_available_at(buffer, at, sizeof(uint16_t))); in sldns_buffer_read_u16_at()
654 return sldns_read_uint16(buffer->_data + at); in sldns_buffer_read_u16_at()
658 * returns the 2-byte integer value at the current position in the buffer
659 * \param[in] buffer the buffer
663 sldns_buffer_read_u16(sldns_buffer *buffer) in sldns_buffer_read_u16() argument
665 uint16_t result = sldns_buffer_read_u16_at(buffer, buffer->_position); in sldns_buffer_read_u16()
666 buffer->_position += sizeof(uint16_t); in sldns_buffer_read_u16()
671 * returns the 4-byte integer value at the given position in the buffer
672 * \param[in] buffer the buffer
673 * \param[in] at position in the buffer
677 sldns_buffer_read_u32_at(sldns_buffer *buffer, size_t at) in sldns_buffer_read_u32_at() argument
679 assert(sldns_buffer_available_at(buffer, at, sizeof(uint32_t))); in sldns_buffer_read_u32_at()
680 return sldns_read_uint32(buffer->_data + at); in sldns_buffer_read_u32_at()
684 * returns the 4-byte integer value at the current position in the buffer
685 * \param[in] buffer the buffer
689 sldns_buffer_read_u32(sldns_buffer *buffer) in sldns_buffer_read_u32() argument
691 uint32_t result = sldns_buffer_read_u32_at(buffer, buffer->_position); in sldns_buffer_read_u32()
692 buffer->_position += sizeof(uint32_t); in sldns_buffer_read_u32()
697 * returns the status of the buffer
698 * \param[in] buffer
702 sldns_buffer_status(sldns_buffer *buffer) in sldns_buffer_status() argument
704 return (int)buffer->_status_err; in sldns_buffer_status()
708 * returns true if the status of the buffer is LDNS_STATUS_OK, false otherwise
709 * \param[in] buffer the buffer
713 sldns_buffer_status_ok(sldns_buffer *buffer) in sldns_buffer_status_ok() argument
715 if (buffer) { in sldns_buffer_status_ok()
716 return sldns_buffer_status(buffer) == 0; in sldns_buffer_status_ok()
723 * prints to the buffer, increasing the capacity if required using
724 * buffer_reserve(). The buffer's position is set to the terminating '\\0'
728 int sldns_buffer_printf(sldns_buffer *buffer, const char *format, ...)
732 * frees the buffer.
733 * \param[in] *buffer the buffer to be freed
735 void sldns_buffer_free(sldns_buffer *buffer);
738 * Copy contents of the from buffer to the result buffer and then flips
739 * the result buffer. Data will be silently truncated if the result buffer is
741 * \param[out] *result resulting buffer which is copied to.