f9fdb43a912a53c44627449ace57921b143eef60Michael Graff/*
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Copyright (C) 2000, 2001, 2004, 2005, 2007, 2014, 2016 Internet Systems Consortium, Inc. ("ISC")
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence *
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/.
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff */
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
70e5a7403f0e0a3bd292b8287c5fed5772c15270Automatic Updater/* $Id: lwbuffer.c,v 1.15 2007/06/19 23:47:22 tbox Exp $ */
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/*! \file */
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/**
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.
431a83fb29482c5170b3e4026e59bb14849a6707Tinderbox User *
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.
431a83fb29482c5170b3e4026e59bb14849a6707Tinderbox User *
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.
431a83fb29482c5170b3e4026e59bb14849a6707Tinderbox User *
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 * empty.
431a83fb29482c5170b3e4026e59bb14849a6707Tinderbox User *
431a83fb29482c5170b3e4026e59bb14849a6707Tinderbox User *
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * \verbatim
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * /------------entire length---------------\\
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * /----- used region -----\\/-- available --\\
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * +----------------------------------------+
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * | consumed | remaining | |
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * +----------------------------------------+
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * a b c d e
431a83fb29482c5170b3e4026e59bb14849a6707Tinderbox User *
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.
431a83fb29482c5170b3e4026e59bb14849a6707Tinderbox User *
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
431a83fb29482c5170b3e4026e59bb14849a6707Tinderbox User *
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.
431a83fb29482c5170b3e4026e59bb14849a6707Tinderbox User *
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.
431a83fb29482c5170b3e4026e59bb14849a6707Tinderbox User *
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.
431a83fb29482c5170b3e4026e59bb14849a6707Tinderbox User *
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * A buffer is re-initialised by lwres_buffer_clear(). The function sets
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * used , current and active to zero.
431a83fb29482c5170b3e4026e59bb14849a6707Tinderbox User *
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).
431a83fb29482c5170b3e4026e59bb14849a6707Tinderbox User *
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.
431a83fb29482c5170b3e4026e59bb14849a6707Tinderbox User *
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.
431a83fb29482c5170b3e4026e59bb14849a6707Tinderbox User *
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 * order.
431a83fb29482c5170b3e4026e59bb14849a6707Tinderbox User *
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.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein */
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff#include <config.h>
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff#include <string.h>
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff#include <lwres/lwbuffer.h>
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
c1ecf4ace2a59be40c68f3f9f58370e124f68763Brian Wellington#include "assert_p.h"
c1ecf4ace2a59be40c68f3f9f58370e124f68763Brian Wellington
f9fdb43a912a53c44627449ace57921b143eef60Michael Graffvoid
f9fdb43a912a53c44627449ace57921b143eef60Michael Grafflwres_buffer_init(lwres_buffer_t *b, void *base, unsigned int length)
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff{
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff /*
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff * Make 'b' refer to the 'length'-byte region starting at base.
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff */
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff REQUIRE(b != NULL);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff b->magic = LWRES_BUFFER_MAGIC;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff b->base = base;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff b->length = length;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff b->used = 0;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff b->current = 0;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff b->active = 0;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff}
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/* Make 'b' an invalid buffer. */
f9fdb43a912a53c44627449ace57921b143eef60Michael Graffvoid
f9fdb43a912a53c44627449ace57921b143eef60Michael Grafflwres_buffer_invalidate(lwres_buffer_t *b)
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff{
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff REQUIRE(LWRES_BUFFER_VALID(b));
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff b->magic = 0;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff b->base = NULL;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff b->length = 0;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff b->used = 0;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff b->current = 0;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff b->active = 0;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff}
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/* Increase the 'used' region of 'b' by 'n' bytes. */
f9fdb43a912a53c44627449ace57921b143eef60Michael Graffvoid
f9fdb43a912a53c44627449ace57921b143eef60Michael Grafflwres_buffer_add(lwres_buffer_t *b, unsigned int n)
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff{
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff REQUIRE(LWRES_BUFFER_VALID(b));
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff REQUIRE(b->used + n <= b->length);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff b->used += n;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff}
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/* Decrease the 'used' region of 'b' by 'n' bytes. */
f9fdb43a912a53c44627449ace57921b143eef60Michael Graffvoid
f9fdb43a912a53c44627449ace57921b143eef60Michael Grafflwres_buffer_subtract(lwres_buffer_t *b, unsigned int n)
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff{
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff REQUIRE(LWRES_BUFFER_VALID(b));
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff REQUIRE(b->used >= n);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff b->used -= n;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff if (b->current > b->used)
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff b->current = b->used;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff if (b->active > b->used)
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff b->active = b->used;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff}
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/* Make the used region empty. */
f9fdb43a912a53c44627449ace57921b143eef60Michael Graffvoid
f9fdb43a912a53c44627449ace57921b143eef60Michael Grafflwres_buffer_clear(lwres_buffer_t *b)
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff{
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff REQUIRE(LWRES_BUFFER_VALID(b));
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff b->used = 0;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff b->current = 0;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff b->active = 0;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff}
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/* Make the consumed region empty. */
f9fdb43a912a53c44627449ace57921b143eef60Michael Graffvoid
f9fdb43a912a53c44627449ace57921b143eef60Michael Grafflwres_buffer_first(lwres_buffer_t *b)
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff{
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff REQUIRE(LWRES_BUFFER_VALID(b));
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff b->current = 0;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff}
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/* Increase the 'consumed' region of 'b' by 'n' bytes. */
f9fdb43a912a53c44627449ace57921b143eef60Michael Graffvoid
f9fdb43a912a53c44627449ace57921b143eef60Michael Grafflwres_buffer_forward(lwres_buffer_t *b, unsigned int n)
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff{
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff REQUIRE(LWRES_BUFFER_VALID(b));
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff REQUIRE(b->current + n <= b->used);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff b->current += n;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff}
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/* Decrease the 'consumed' region of 'b' by 'n' bytes. */
f9fdb43a912a53c44627449ace57921b143eef60Michael Graffvoid
f9fdb43a912a53c44627449ace57921b143eef60Michael Grafflwres_buffer_back(lwres_buffer_t *b, unsigned int n)
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff{
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff REQUIRE(LWRES_BUFFER_VALID(b));
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff REQUIRE(n <= b->current);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff b->current -= n;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff}
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/* Read an unsigned 8-bit integer from 'b' and return it. */
7a166c5c61a5aaa6eeb929bed152dc0a6b128e3dMichael Grafflwres_uint8_t
f9fdb43a912a53c44627449ace57921b143eef60Michael Grafflwres_buffer_getuint8(lwres_buffer_t *b)
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff{
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff unsigned char *cp;
7a166c5c61a5aaa6eeb929bed152dc0a6b128e3dMichael Graff lwres_uint8_t result;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff REQUIRE(LWRES_BUFFER_VALID(b));
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff REQUIRE(b->used - b->current >= 1);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff cp = b->base;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff cp += b->current;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff b->current += 1;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff result = ((unsigned int)(cp[0]));
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff return (result);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff}
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/* Put an unsigned 8-bit integer */
f9fdb43a912a53c44627449ace57921b143eef60Michael Graffvoid
7a166c5c61a5aaa6eeb929bed152dc0a6b128e3dMichael Grafflwres_buffer_putuint8(lwres_buffer_t *b, lwres_uint8_t val)
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff{
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff unsigned char *cp;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff REQUIRE(LWRES_BUFFER_VALID(b));
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff REQUIRE(b->used + 1 <= b->length);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff cp = b->base;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff cp += b->used;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff b->used += 1;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff cp[0] = (val & 0x00ff);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff}
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/* Read an unsigned 16-bit integer in network byte order from 'b', convert it to host byte order, and return it. */
7a166c5c61a5aaa6eeb929bed152dc0a6b128e3dMichael Grafflwres_uint16_t
f9fdb43a912a53c44627449ace57921b143eef60Michael Grafflwres_buffer_getuint16(lwres_buffer_t *b)
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff{
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff unsigned char *cp;
7a166c5c61a5aaa6eeb929bed152dc0a6b128e3dMichael Graff lwres_uint16_t result;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff REQUIRE(LWRES_BUFFER_VALID(b));
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff REQUIRE(b->used - b->current >= 2);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff cp = b->base;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff cp += b->current;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff b->current += 2;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff result = ((unsigned int)(cp[0])) << 8;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff result |= ((unsigned int)(cp[1]));
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff return (result);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff}
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/* Put an unsigned 16-bit integer. */
f9fdb43a912a53c44627449ace57921b143eef60Michael Graffvoid
7a166c5c61a5aaa6eeb929bed152dc0a6b128e3dMichael Grafflwres_buffer_putuint16(lwres_buffer_t *b, lwres_uint16_t val)
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff{
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff unsigned char *cp;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff REQUIRE(LWRES_BUFFER_VALID(b));
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff REQUIRE(b->used + 2 <= b->length);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff cp = b->base;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff cp += b->used;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff b->used += 2;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff cp[0] = (val & 0xff00) >> 8;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff cp[1] = (val & 0x00ff);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff}
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/* Read an unsigned 32-bit integer in network byte order from 'b', convert it to host byte order, and return it. */
7a166c5c61a5aaa6eeb929bed152dc0a6b128e3dMichael Grafflwres_uint32_t
f9fdb43a912a53c44627449ace57921b143eef60Michael Grafflwres_buffer_getuint32(lwres_buffer_t *b)
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff{
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff unsigned char *cp;
7a166c5c61a5aaa6eeb929bed152dc0a6b128e3dMichael Graff lwres_uint32_t result;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff REQUIRE(LWRES_BUFFER_VALID(b));
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff REQUIRE(b->used - b->current >= 4);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff cp = b->base;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff cp += b->current;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff b->current += 4;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff result = ((unsigned int)(cp[0])) << 24;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff result |= ((unsigned int)(cp[1])) << 16;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff result |= ((unsigned int)(cp[2])) << 8;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff result |= ((unsigned int)(cp[3]));
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff return (result);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff}
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/* Put an unsigned 32-bit integer. */
f9fdb43a912a53c44627449ace57921b143eef60Michael Graffvoid
7a166c5c61a5aaa6eeb929bed152dc0a6b128e3dMichael Grafflwres_buffer_putuint32(lwres_buffer_t *b, lwres_uint32_t val)
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff{
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff unsigned char *cp;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff REQUIRE(LWRES_BUFFER_VALID(b));
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff REQUIRE(b->used + 4 <= b->length);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff cp = b->base;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff cp += b->used;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff b->used += 4;
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);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff cp[3] = (unsigned char)(val & 0x000000ff);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff}
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/* copies length bytes of memory at base to b */
f9fdb43a912a53c44627449ace57921b143eef60Michael Graffvoid
915723e4007e177b10c0e1c9d1bfe77ac2bfe853Michael Grafflwres_buffer_putmem(lwres_buffer_t *b, const unsigned char *base,
915723e4007e177b10c0e1c9d1bfe77ac2bfe853Michael Graff unsigned int length)
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff{
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff unsigned char *cp;
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff REQUIRE(LWRES_BUFFER_VALID(b));
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff REQUIRE(b->used + length <= b->length);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff cp = (unsigned char *)b->base + b->used;
e851ea826066ac5a5b01c2c23218faa0273a12e8Evan Hunt memmove(cp, base, length);
f9fdb43a912a53c44627449ace57921b143eef60Michael Graff b->used += length;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence}
915723e4007e177b10c0e1c9d1bfe77ac2bfe853Michael Graff
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/* copies length bytes of memory at b to base */
915723e4007e177b10c0e1c9d1bfe77ac2bfe853Michael Graffvoid
915723e4007e177b10c0e1c9d1bfe77ac2bfe853Michael Grafflwres_buffer_getmem(lwres_buffer_t *b, unsigned char *base,
915723e4007e177b10c0e1c9d1bfe77ac2bfe853Michael Graff unsigned int length)
915723e4007e177b10c0e1c9d1bfe77ac2bfe853Michael Graff{
915723e4007e177b10c0e1c9d1bfe77ac2bfe853Michael Graff unsigned char *cp;
915723e4007e177b10c0e1c9d1bfe77ac2bfe853Michael Graff
915723e4007e177b10c0e1c9d1bfe77ac2bfe853Michael Graff REQUIRE(LWRES_BUFFER_VALID(b));
915723e4007e177b10c0e1c9d1bfe77ac2bfe853Michael Graff REQUIRE(b->used - b->current >= length);
915723e4007e177b10c0e1c9d1bfe77ac2bfe853Michael Graff
915723e4007e177b10c0e1c9d1bfe77ac2bfe853Michael Graff cp = b->base;
915723e4007e177b10c0e1c9d1bfe77ac2bfe853Michael Graff cp += b->current;
915723e4007e177b10c0e1c9d1bfe77ac2bfe853Michael Graff b->current += length;
915723e4007e177b10c0e1c9d1bfe77ac2bfe853Michael Graff
e851ea826066ac5a5b01c2c23218faa0273a12e8Evan Hunt memmove(base, cp, length);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence}