* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
* See the COPYRIGHT file distributed with this work for additional
* information regarding copyright ownership.
/*! \file lwres/lwbuffer.h
* A buffer is a region of memory, together with a set of related subregions.
* Buffers are used for parsing and I/O operations.
* The 'used region' and the 'available' region are disjoint, and their
* union is the buffer's region. The used region extends from the beginning
* of the buffer region to the last used byte. The available region
* extends from one byte greater than the last used byte to the end of the
* buffer's region. The size of the used region can be changed using various
* buffer commands. Initially, the used region is empty.
* The used region is further subdivided into two disjoint regions: the
* 'consumed region' and the 'remaining region'. The union of these two
* regions is the used region. The consumed region extends from the beginning
* of the used region to the byte before the 'current' offset (if any). The
* 'remaining' region the current pointer to the end of the used
* region. The size of the consumed region can be changed using various
* buffer commands. Initially, the consumed region is empty.
* The 'active region' is an (optional) subregion of the remaining region.
* It extends from the current offset to an offset in the remaining region
* that is selected with lwres_buffer_setactive(). Initially, the active
* region is empty. If the current offset advances beyond the chosen offset,
* the active region will also be empty.
* /----- used region -----\/-- available --\
* +----------------------------------------+
* | consumed | remaining | |
* +----------------------------------------+
* b == current pointer. Can be anywhere between a and d.
* c == active pointer. Meaningful between b and d.
* a-e == entire (length) of buffer.
* a-b == consumed region.
* b-d == remaining region.
* b-c == optional active region.
* The following invariants are maintained by all routines:
* base is a valid pointer to length bytes of memory
* (although active < current implies empty active region)
* Buffers have no synchronization. Clients must ensure exclusive
* Memory: 1 pointer + 6 unsigned integers per buffer.
#define LWRES_LWBUFFER_H 1
#define LWRES_BUFFER_MAGIC 0x4275663fU /* Buf?. */
#define LWRES_BUFFER_VALID(b) ((b) != NULL && \
(b)->magic == LWRES_BUFFER_MAGIC)
* The following macros MUST be used only on valid buffers. It is the
* caller's responsibility to ensure this by using the LWRES_BUFFER_VALID
* check above, or by calling another lwres_buffer_*() function (rather than
* Get the length of the used region of buffer "b"
#define LWRES_BUFFER_USEDCOUNT(b) ((b)->used)
* Get the length of the available region of buffer "b"
#define LWRES_BUFFER_AVAILABLECOUNT(b) ((b)->length - (b)->used)
#define LWRES_BUFFER_REMAINING(b) ((b)->used - (b)->current)
* Note that the buffer structure is public. This is principally so buffer
* operations can be implemented using macros. Applications are strongly
* discouraged from directly manipulating the structure.
typedef struct lwres_buffer lwres_buffer_t;
/* The following integers are byte offsets from 'base'. */
lwres_buffer_init(lwres_buffer_t *b, void *base, unsigned int length);
* Make 'b' refer to the 'length'-byte region starting at base.
* 'base' is a pointer to a sequence of 'length' bytes.
lwres_buffer_invalidate(lwres_buffer_t *b);
* Make 'b' an invalid buffer.
* If assertion checking is enabled, future attempts to use 'b' without
* calling lwres_buffer_init() on it will cause an assertion failure.
lwres_buffer_add(lwres_buffer_t *b, unsigned int n);
* Increase the 'used' region of 'b' by 'n' bytes.
lwres_buffer_subtract(lwres_buffer_t *b, unsigned int n);
* Decrease the 'used' region of 'b' by 'n' bytes.
lwres_buffer_clear(lwres_buffer_t *b);
* Make the used region empty.
lwres_buffer_first(lwres_buffer_t *b);
* Make the consumed region empty.
lwres_buffer_forward(lwres_buffer_t *b, unsigned int n);
* Increase the 'consumed' region of 'b' by 'n' bytes.
lwres_buffer_back(lwres_buffer_t *b, unsigned int n);
* Decrease the 'consumed' region of 'b' by 'n' bytes.
lwres_buffer_getuint8(lwres_buffer_t *b);
* Read an unsigned 8-bit integer from 'b' and return it.
* The length of the available region of 'b' is at least 1.
* The current pointer in 'b' is advanced by 1.
* A 8-bit unsigned integer.
lwres_buffer_putuint8(lwres_buffer_t *b, uint8_t val);
* Store an unsigned 8-bit integer from 'val' into 'b'.
* The length of the unused region of 'b' is at least 1.
* The used pointer in 'b' is advanced by 1.
lwres_buffer_getuint16(lwres_buffer_t *b);
* Read an unsigned 16-bit integer in network byte order from 'b', convert
* it to host byte order, and return it.
* The length of the available region of 'b' is at least 2.
* The current pointer in 'b' is advanced by 2.
* A 16-bit unsigned integer.
lwres_buffer_putuint16(lwres_buffer_t *b, uint16_t val);
* Store an unsigned 16-bit integer in host byte order from 'val'
* into 'b' in network byte order.
* The length of the unused region of 'b' is at least 2.
* The used pointer in 'b' is advanced by 2.
lwres_buffer_getuint32(lwres_buffer_t *b);
* Read an unsigned 32-bit integer in network byte order from 'b', convert
* it to host byte order, and return it.
* The length of the available region of 'b' is at least 2.
* The current pointer in 'b' is advanced by 2.
* A 32-bit unsigned integer.
lwres_buffer_putuint32(lwres_buffer_t *b, uint32_t val);
* Store an unsigned 32-bit integer in host byte order from 'val'
* into 'b' in network byte order.
* The length of the unused region of 'b' is at least 4.
* The used pointer in 'b' is advanced by 4.
lwres_buffer_putmem(lwres_buffer_t *b, const unsigned char *base,
* Copy 'length' bytes of memory at 'base' into 'b'.
* 'base' points to 'length' bytes of valid memory.
lwres_buffer_getmem(lwres_buffer_t *b, unsigned char *base,
* Copy 'length' bytes of memory from 'b' into 'base'.
* 'base' points to at least 'length' bytes of valid memory.
* 'b' have at least 'length' bytes remaining.
#endif /* LWRES_LWBUFFER_H */