0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Copyright (C) 2000, 2001, 2004, 2005, 2007, 2014, 2016 Internet Systems Consortium, Inc. ("ISC")
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * This Source Code Form is subject to the terms of the Mozilla Public
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * License, v. 2.0. If a copy of the MPL was not distributed with this
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * file, You can obtain one at http://mozilla.org/MPL/2.0/.
70e5a7403f0e0a3bd292b8287c5fed5772c15270Automatic Updater/* $Id: lwbuffer.c,v 1.15 2007/06/19 23:47:22 tbox Exp $ */
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * These functions provide bounds checked access to a region of memory
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * where data is being read or written. They are based on, and similar
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * to, the isc_buffer_ functions in the ISC library.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * A buffer is a region of memory, together with a set of related
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * subregions. The used region and the available region are disjoint, and
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * their union is the buffer's region. The used region extends from the
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * beginning of the buffer region to the last used byte. The available
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * region extends from one byte greater than the last used byte to the
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * end of the buffer's region. The size of the used region can be changed
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * using various buffer commands. Initially, the used region is empty.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * The used region is further subdivided into two disjoint regions: the
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * consumed region and the remaining region. The union of these two
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * regions is the used region. The consumed region extends from the
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * beginning of the used region to the byte before the current offset (if
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * any). The remaining region the current pointer to the end of the used
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * region. The size of the consumed region can be changed using various
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * buffer commands. Initially, the consumed region is empty.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * The active region is an (optional) subregion of the remaining region.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * It extends from the current offset to an offset in the remaining
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * region. Initially, the active region is empty. If the current offset
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * advances beyond the chosen offset, the active region will also be
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * /------------entire length---------------\\
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * /----- used region -----\\/-- available --\\
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * +----------------------------------------+
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * | consumed | remaining | |
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * +----------------------------------------+
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * a == base of buffer.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * b == current pointer. Can be anywhere between a and d.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * c == active pointer. Meaningful between b and d.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * d == used pointer.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * e == length of buffer.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * a-e == entire length of buffer.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * a-d == used region.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * a-b == consumed region.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * b-d == remaining region.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * b-c == optional active region.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * \endverbatim
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * lwres_buffer_init() initializes the lwres_buffer_t *b and assocates it
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * with the memory region of size length bytes starting at location base.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * lwres_buffer_invalidate() marks the buffer *b as invalid. Invalidating
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * a buffer after use is not required, but makes it possible to catch its
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * possible accidental use.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * The functions lwres_buffer_add() and lwres_buffer_subtract()
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * respectively increase and decrease the used space in buffer *b by n
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * bytes. lwres_buffer_add() checks for buffer overflow and
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * lwres_buffer_subtract() checks for underflow. These functions do not
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * allocate or deallocate memory. They just change the value of used.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * A buffer is re-initialised by lwres_buffer_clear(). The function sets
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * used , current and active to zero.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * lwres_buffer_first() makes the consumed region of buffer *p empty by
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * setting current to zero (the start of the buffer).
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * lwres_buffer_forward() increases the consumed region of buffer *b by n
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * bytes, checking for overflow. Similarly, lwres_buffer_back() decreases
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * buffer b's consumed region by n bytes and checks for underflow.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * lwres_buffer_getuint8() reads an unsigned 8-bit integer from *b and
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * returns it. lwres_buffer_putuint8() writes the unsigned 8-bit integer
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * val to buffer *b.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * lwres_buffer_getuint16() and lwres_buffer_getuint32() are identical to
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * lwres_buffer_putuint8() except that they respectively read an unsigned
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * 16-bit or 32-bit integer in network byte order from b. Similarly,
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * lwres_buffer_putuint16() and lwres_buffer_putuint32() writes the
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * unsigned 16-bit or 32-bit integer val to buffer b, in network byte
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * Arbitrary amounts of data are read or written from a lightweight
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * resolver buffer with lwres_buffer_getmem() and lwres_buffer_putmem()
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * respectively. lwres_buffer_putmem() copies length bytes of memory at
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * base to b. Conversely, lwres_buffer_getmem() copies length bytes of
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * memory from b to base.
f9fdb43a912a53c44627449ace57921b143eef60Michael Grafflwres_buffer_init(lwres_buffer_t *b, void *base, unsigned int length)
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff * Make 'b' refer to the 'length'-byte region starting at base.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/* Make 'b' an invalid buffer. */
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/* Increase the 'used' region of 'b' by 'n' bytes. */
f9fdb43a912a53c44627449ace57921b143eef60Michael Grafflwres_buffer_add(lwres_buffer_t *b, unsigned int n)
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/* Decrease the 'used' region of 'b' by 'n' bytes. */
f9fdb43a912a53c44627449ace57921b143eef60Michael Grafflwres_buffer_subtract(lwres_buffer_t *b, unsigned int n)
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/* Make the used region empty. */
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/* Make the consumed region empty. */
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/* Increase the 'consumed' region of 'b' by 'n' bytes. */
f9fdb43a912a53c44627449ace57921b143eef60Michael Grafflwres_buffer_forward(lwres_buffer_t *b, unsigned int n)
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/* Decrease the 'consumed' region of 'b' by 'n' bytes. */
f9fdb43a912a53c44627449ace57921b143eef60Michael Grafflwres_buffer_back(lwres_buffer_t *b, unsigned int n)
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/* Read an unsigned 8-bit integer from 'b' and return it. */
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/* Put an unsigned 8-bit integer */
7a166c5c61a5aaa6eeb929bed152dc0a6b128e3dMichael Grafflwres_buffer_putuint8(lwres_buffer_t *b, lwres_uint8_t val)
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/* Read an unsigned 16-bit integer in network byte order from 'b', convert it to host byte order, and return it. */
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/* Put an unsigned 16-bit integer. */
7a166c5c61a5aaa6eeb929bed152dc0a6b128e3dMichael Grafflwres_buffer_putuint16(lwres_buffer_t *b, lwres_uint16_t val)
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/* Read an unsigned 32-bit integer in network byte order from 'b', convert it to host byte order, and return it. */
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/* Put an unsigned 32-bit integer. */
7a166c5c61a5aaa6eeb929bed152dc0a6b128e3dMichael Grafflwres_buffer_putuint32(lwres_buffer_t *b, lwres_uint32_t val)
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff cp[0] = (unsigned char)((val & 0xff000000) >> 24);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff cp[1] = (unsigned char)((val & 0x00ff0000) >> 16);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff cp[2] = (unsigned char)((val & 0x0000ff00) >> 8);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/* copies length bytes of memory at base to b */
915723e4007e177b10c0e1c9d1bfe77ac2bfe853Michael Grafflwres_buffer_putmem(lwres_buffer_t *b, const unsigned char *base,
915723e4007e177b10c0e1c9d1bfe77ac2bfe853Michael Graff unsigned int length)
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/* copies length bytes of memory at b to base */
915723e4007e177b10c0e1c9d1bfe77ac2bfe853Michael Grafflwres_buffer_getmem(lwres_buffer_t *b, unsigned char *base,
915723e4007e177b10c0e1c9d1bfe77ac2bfe853Michael Graff unsigned int length)