lwbuffer.c revision c1ecf4ace2a59be40c68f3f9f58370e124f68763
1633838b8255282d10af15c5c84cee5a51466712Bob Halley/*
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * Copyright (C) 2000 Internet Software Consortium.
1633838b8255282d10af15c5c84cee5a51466712Bob Halley *
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * Permission to use, copy, modify, and distribute this software for any
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * purpose with or without fee is hereby granted, provided that the above
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * copyright notice and this permission notice appear in all copies.
1633838b8255282d10af15c5c84cee5a51466712Bob Halley *
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * SOFTWARE.
1633838b8255282d10af15c5c84cee5a51466712Bob Halley */
d25afd60ee2286cb171c4960a790f3d7041b6f85Bob Halley
d25afd60ee2286cb171c4960a790f3d7041b6f85Bob Halley/* $Id: lwbuffer.c,v 1.7 2000/06/26 23:35:00 bwelling Exp $ */
d25afd60ee2286cb171c4960a790f3d7041b6f85Bob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#include <config.h>
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#include <string.h>
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#include <lwres/lwbuffer.h>
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#include "assert_p.h"
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleyvoid
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleylwres_buffer_init(lwres_buffer_t *b, void *base, unsigned int length)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley{
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Make 'b' refer to the 'length'-byte region starting at base.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley REQUIRE(b != NULL);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley b->magic = LWRES_BUFFER_MAGIC;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley b->base = base;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley b->length = length;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley b->used = 0;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley b->current = 0;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley b->active = 0;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleyvoid
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleylwres_buffer_invalidate(lwres_buffer_t *b)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley{
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Make 'b' an invalid buffer.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley REQUIRE(LWRES_BUFFER_VALID(b));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley b->magic = 0;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley b->base = NULL;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley b->length = 0;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley b->used = 0;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley b->current = 0;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley b->active = 0;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleyvoid
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleylwres_buffer_add(lwres_buffer_t *b, unsigned int n)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley{
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Increase the 'used' region of 'b' by 'n' bytes.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley REQUIRE(LWRES_BUFFER_VALID(b));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley REQUIRE(b->used + n <= b->length);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley b->used += n;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleyvoid
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleylwres_buffer_subtract(lwres_buffer_t *b, unsigned int n)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley{
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Decrease the 'used' region of 'b' by 'n' bytes.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley REQUIRE(LWRES_BUFFER_VALID(b));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley REQUIRE(b->used >= n);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley b->used -= n;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (b->current > b->used)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley b->current = b->used;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (b->active > b->used)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley b->active = b->used;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleyvoid
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleylwres_buffer_clear(lwres_buffer_t *b)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley{
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Make the used region empty.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley REQUIRE(LWRES_BUFFER_VALID(b));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley b->used = 0;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley b->current = 0;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley b->active = 0;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleyvoid
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleylwres_buffer_first(lwres_buffer_t *b)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley{
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Make the consumed region empty.
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley */
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley REQUIRE(LWRES_BUFFER_VALID(b));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley b->current = 0;
3740b569ae76295b941d57a724a43beb75b533baBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halleyvoid
3740b569ae76295b941d57a724a43beb75b533baBob Halleylwres_buffer_forward(lwres_buffer_t *b, unsigned int n)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley{
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley /*
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley * Increase the 'consumed' region of 'b' by 'n' bytes.
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley */
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley REQUIRE(LWRES_BUFFER_VALID(b));
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley REQUIRE(b->current + n <= b->used);
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley b->current += n;
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley}
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley
577179503f2eb7695ec668d8eeb41889a150e28fBob Halleyvoid
577179503f2eb7695ec668d8eeb41889a150e28fBob Halleylwres_buffer_back(lwres_buffer_t *b, unsigned int n)
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley{
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley /*
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley * Decrease the 'consumed' region of 'b' by 'n' bytes.
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley */
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley REQUIRE(LWRES_BUFFER_VALID(b));
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley REQUIRE(n <= b->current);
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley b->current -= n;
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley}
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley
577179503f2eb7695ec668d8eeb41889a150e28fBob Halleylwres_uint8_t
577179503f2eb7695ec668d8eeb41889a150e28fBob Halleylwres_buffer_getuint8(lwres_buffer_t *b)
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley{
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley unsigned char *cp;
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley lwres_uint8_t result;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /*
3740b569ae76295b941d57a724a43beb75b533baBob Halley * Read an unsigned 8-bit integer from 'b' and return it.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley REQUIRE(LWRES_BUFFER_VALID(b));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley REQUIRE(b->used - b->current >= 1);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley cp = b->base;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley cp += b->current;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley b->current += 1;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley result = ((unsigned int)(cp[0]));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (result);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleyvoid
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleylwres_buffer_putuint8(lwres_buffer_t *b, lwres_uint8_t val)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley{
3740b569ae76295b941d57a724a43beb75b533baBob Halley unsigned char *cp;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley REQUIRE(LWRES_BUFFER_VALID(b));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley REQUIRE(b->used + 1 <= b->length);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley cp = b->base;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley cp += b->used;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley b->used += 1;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley cp[0] = (val & 0x00ff);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleylwres_uint16_t
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleylwres_buffer_getuint16(lwres_buffer_t *b)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley{
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley unsigned char *cp;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley lwres_uint16_t result;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Read an unsigned 16-bit integer in network byte order from 'b',
3740b569ae76295b941d57a724a43beb75b533baBob Halley * convert it to host byte order, and return it.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley REQUIRE(LWRES_BUFFER_VALID(b));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley REQUIRE(b->used - b->current >= 2);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley cp = b->base;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley cp += b->current;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley b->current += 2;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley result = ((unsigned int)(cp[0])) << 8;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley result |= ((unsigned int)(cp[1]));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (result);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleyvoid
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleylwres_buffer_putuint16(lwres_buffer_t *b, lwres_uint16_t val)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley{
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley unsigned char *cp;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley REQUIRE(LWRES_BUFFER_VALID(b));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley REQUIRE(b->used + 2 <= b->length);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley cp = b->base;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley cp += b->used;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley b->used += 2;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley cp[0] = (val & 0xff00) >> 8;
3740b569ae76295b941d57a724a43beb75b533baBob Halley cp[1] = (val & 0x00ff);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleylwres_uint32_t
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleylwres_buffer_getuint32(lwres_buffer_t *b)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley{
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley unsigned char *cp;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley lwres_uint32_t result;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /*
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley * Read an unsigned 32-bit integer in network byte order from 'b',
3740b569ae76295b941d57a724a43beb75b533baBob Halley * convert it to host byte order, and return it.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley REQUIRE(LWRES_BUFFER_VALID(b));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley REQUIRE(b->used - b->current >= 4);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley cp = b->base;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley cp += b->current;
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley b->current += 4;
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley result = ((unsigned int)(cp[0])) << 24;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley result |= ((unsigned int)(cp[1])) << 16;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley result |= ((unsigned int)(cp[2])) << 8;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley result |= ((unsigned int)(cp[3]));
3740b569ae76295b941d57a724a43beb75b533baBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (result);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleyvoid
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleylwres_buffer_putuint32(lwres_buffer_t *b, lwres_uint32_t val)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley{
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley unsigned char *cp;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley REQUIRE(LWRES_BUFFER_VALID(b));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley REQUIRE(b->used + 4 <= b->length);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley cp = b->base;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley cp += b->used;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley b->used += 4;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley cp[0] = (unsigned char)((val & 0xff000000) >> 24);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley cp[1] = (unsigned char)((val & 0x00ff0000) >> 16);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley cp[2] = (unsigned char)((val & 0x0000ff00) >> 8);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley cp[3] = (unsigned char)(val & 0x000000ff);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleyvoid
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleylwres_buffer_putmem(lwres_buffer_t *b, const unsigned char *base,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley unsigned int length)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley{
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley unsigned char *cp;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley REQUIRE(LWRES_BUFFER_VALID(b));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley REQUIRE(b->used + length <= b->length);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley cp = (unsigned char *)b->base + b->used;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley memcpy(cp, base, length);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley b->used += length;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleyvoid
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleylwres_buffer_getmem(lwres_buffer_t *b, unsigned char *base,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley unsigned int length)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley{
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley unsigned char *cp;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley REQUIRE(LWRES_BUFFER_VALID(b));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley REQUIRE(b->used - b->current >= length);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley cp = b->base;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley cp += b->current;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley b->current += length;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley memcpy(base, cp, length);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley