buffer.c revision f8d63d12dd6fe15de6a1339722c61b030d1e5bad
1ccbfca64ae86ace521053773001cb995352f96fBob Halley/*
1ccbfca64ae86ace521053773001cb995352f96fBob Halley * Copyright (C) 1998, 1999 Internet Software Consortium.
1ccbfca64ae86ace521053773001cb995352f96fBob Halley *
1ccbfca64ae86ace521053773001cb995352f96fBob Halley * Permission to use, copy, modify, and distribute this software for any
1ccbfca64ae86ace521053773001cb995352f96fBob Halley * purpose with or without fee is hereby granted, provided that the above
1ccbfca64ae86ace521053773001cb995352f96fBob Halley * copyright notice and this permission notice appear in all copies.
1ccbfca64ae86ace521053773001cb995352f96fBob Halley *
1ccbfca64ae86ace521053773001cb995352f96fBob Halley * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
1ccbfca64ae86ace521053773001cb995352f96fBob Halley * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
1ccbfca64ae86ace521053773001cb995352f96fBob Halley * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
1ccbfca64ae86ace521053773001cb995352f96fBob Halley * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
1ccbfca64ae86ace521053773001cb995352f96fBob Halley * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
1ccbfca64ae86ace521053773001cb995352f96fBob Halley * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
1ccbfca64ae86ace521053773001cb995352f96fBob Halley * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
1ccbfca64ae86ace521053773001cb995352f96fBob Halley * SOFTWARE.
1ccbfca64ae86ace521053773001cb995352f96fBob Halley */
1ccbfca64ae86ace521053773001cb995352f96fBob Halley
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley#include <config.h>
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley#include <string.h>
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley
1ccbfca64ae86ace521053773001cb995352f96fBob Halley#include <isc/assertions.h>
1ccbfca64ae86ace521053773001cb995352f96fBob Halley#include <isc/buffer.h>
1ccbfca64ae86ace521053773001cb995352f96fBob Halley
1ccbfca64ae86ace521053773001cb995352f96fBob Halleyvoid
1ccbfca64ae86ace521053773001cb995352f96fBob Halleyisc_buffer_init(isc_buffer_t *b, void *base, unsigned int length,
1ccbfca64ae86ace521053773001cb995352f96fBob Halley unsigned int type)
1ccbfca64ae86ace521053773001cb995352f96fBob Halley{
1ccbfca64ae86ace521053773001cb995352f96fBob Halley /*
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley * Make 'b' refer to the 'length'-byte region starting at base.
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley */
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley REQUIRE(b != NULL);
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley b->magic = ISC_BUFFER_MAGIC;
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley b->type = type;
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley b->base = base;
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley b->length = length;
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley b->used = 0;
0e3ad060581e366d3e682caf2dd5d9ea2e0f4893Bob Halley b->current = 0;
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley b->active = 0;
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley b->mctx = NULL;
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley ISC_LINK_INIT(b, link);
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley}
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halleyvoid
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halleyisc_buffer_invalidate(isc_buffer_t *b) {
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley /*
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley * Make 'b' an invalid buffer.
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley */
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley REQUIRE(ISC_BUFFER_VALID(b));
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley REQUIRE(!ISC_LINK_LINKED(b, link));
1ccbfca64ae86ace521053773001cb995352f96fBob Halley REQUIRE(b->mctx == NULL);
1ccbfca64ae86ace521053773001cb995352f96fBob Halley
0e3ad060581e366d3e682caf2dd5d9ea2e0f4893Bob Halley b->magic = 0;
1ccbfca64ae86ace521053773001cb995352f96fBob Halley b->type = 0;
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley b->base = NULL;
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley b->length = 0;
0e3ad060581e366d3e682caf2dd5d9ea2e0f4893Bob Halley b->used = 0;
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley b->current = 0;
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley b->active = 0;
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley}
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halleyunsigned int
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halleyisc_buffer_type(isc_buffer_t *b) {
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley /*
1ccbfca64ae86ace521053773001cb995352f96fBob Halley * The type of 'b'.
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley */
0e3ad060581e366d3e682caf2dd5d9ea2e0f4893Bob Halley
1ccbfca64ae86ace521053773001cb995352f96fBob Halley REQUIRE(ISC_BUFFER_VALID(b));
1ccbfca64ae86ace521053773001cb995352f96fBob Halley
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley return (b->type);
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley}
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halleyvoid
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halleyisc_buffer_region(isc_buffer_t *b, isc_region_t *r) {
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley /*
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley * Make 'r' refer to the region of 'b'.
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley */
1ccbfca64ae86ace521053773001cb995352f96fBob Halley
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley REQUIRE(ISC_BUFFER_VALID(b));
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley REQUIRE(r != NULL);
0e3ad060581e366d3e682caf2dd5d9ea2e0f4893Bob Halley
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley r->base = b->base;
1ccbfca64ae86ace521053773001cb995352f96fBob Halley r->length = b->length;
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley}
1ccbfca64ae86ace521053773001cb995352f96fBob Halley
1ccbfca64ae86ace521053773001cb995352f96fBob Halleyvoid
1ccbfca64ae86ace521053773001cb995352f96fBob Halleyisc_buffer_used(isc_buffer_t *b, isc_region_t *r) {
1ccbfca64ae86ace521053773001cb995352f96fBob Halley /*
1ccbfca64ae86ace521053773001cb995352f96fBob Halley * Make 'r' refer to the used region of 'b'.
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley */
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley REQUIRE(ISC_BUFFER_VALID(b));
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley REQUIRE(r != NULL);
1ccbfca64ae86ace521053773001cb995352f96fBob Halley
1ccbfca64ae86ace521053773001cb995352f96fBob Halley r->base = b->base;
1ccbfca64ae86ace521053773001cb995352f96fBob Halley r->length = b->used;
1ccbfca64ae86ace521053773001cb995352f96fBob Halley}
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halleyvoid
1ccbfca64ae86ace521053773001cb995352f96fBob Halleyisc_buffer_available(isc_buffer_t *b, isc_region_t *r) {
1ccbfca64ae86ace521053773001cb995352f96fBob Halley /*
1ccbfca64ae86ace521053773001cb995352f96fBob Halley * Make 'r' refer to the available region of 'b'.
1ccbfca64ae86ace521053773001cb995352f96fBob Halley */
1ccbfca64ae86ace521053773001cb995352f96fBob Halley
1ccbfca64ae86ace521053773001cb995352f96fBob Halley REQUIRE(ISC_BUFFER_VALID(b));
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley REQUIRE(r != NULL);
1ccbfca64ae86ace521053773001cb995352f96fBob Halley
1ccbfca64ae86ace521053773001cb995352f96fBob Halley r->base = (unsigned char *)b->base + b->used;
1ccbfca64ae86ace521053773001cb995352f96fBob Halley r->length = b->length - b->used;
1ccbfca64ae86ace521053773001cb995352f96fBob Halley}
1ccbfca64ae86ace521053773001cb995352f96fBob Halley
1ccbfca64ae86ace521053773001cb995352f96fBob Halleyvoid
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halleyisc_buffer_add(isc_buffer_t *b, unsigned int n) {
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley /*
1ccbfca64ae86ace521053773001cb995352f96fBob Halley * Increase the 'used' region of 'b' by 'n' bytes.
1ccbfca64ae86ace521053773001cb995352f96fBob Halley */
1ccbfca64ae86ace521053773001cb995352f96fBob Halley
1ccbfca64ae86ace521053773001cb995352f96fBob Halley REQUIRE(ISC_BUFFER_VALID(b));
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley REQUIRE(b->used + n <= b->length);
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley b->used += n;
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley}
1ccbfca64ae86ace521053773001cb995352f96fBob Halley
1ccbfca64ae86ace521053773001cb995352f96fBob Halleyvoid
1ccbfca64ae86ace521053773001cb995352f96fBob Halleyisc_buffer_subtract(isc_buffer_t *b, unsigned int n) {
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley /*
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley * Decrease the 'used' region of 'b' by 'n' bytes.
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley */
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley
1ccbfca64ae86ace521053773001cb995352f96fBob Halley REQUIRE(ISC_BUFFER_VALID(b));
1ccbfca64ae86ace521053773001cb995352f96fBob Halley REQUIRE(b->used >= n);
1ccbfca64ae86ace521053773001cb995352f96fBob Halley
1ccbfca64ae86ace521053773001cb995352f96fBob Halley b->used -= n;
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley if (b->current > b->used)
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley b->current = b->used;
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley if (b->active > b->used)
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley b->active = b->used;
1ccbfca64ae86ace521053773001cb995352f96fBob Halley}
1ccbfca64ae86ace521053773001cb995352f96fBob Halley
1ccbfca64ae86ace521053773001cb995352f96fBob Halleyvoid
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halleyisc_buffer_clear(isc_buffer_t *b) {
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley /*
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley * Make the used region empty.
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley */
1ccbfca64ae86ace521053773001cb995352f96fBob Halley
1ccbfca64ae86ace521053773001cb995352f96fBob Halley REQUIRE(ISC_BUFFER_VALID(b));
a99979c686ae04efd55ba8f0aacf32493b4aa7faBob Halley
a99979c686ae04efd55ba8f0aacf32493b4aa7faBob Halley b->used = 0;
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley b->current = 0;
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley b->active = 0;
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley}
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley
a99979c686ae04efd55ba8f0aacf32493b4aa7faBob Halleyvoid
a99979c686ae04efd55ba8f0aacf32493b4aa7faBob Halleyisc_buffer_consumed(isc_buffer_t *b, isc_region_t *r) {
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley /*
a99979c686ae04efd55ba8f0aacf32493b4aa7faBob Halley * Make 'r' refer to the consumed region of 'b'.
a99979c686ae04efd55ba8f0aacf32493b4aa7faBob Halley */
48481c9b6e19501457bcbc2995555412f352b99fBob Halley
48481c9b6e19501457bcbc2995555412f352b99fBob Halley REQUIRE(ISC_BUFFER_VALID(b));
48481c9b6e19501457bcbc2995555412f352b99fBob Halley REQUIRE(r != NULL);
48481c9b6e19501457bcbc2995555412f352b99fBob Halley
48481c9b6e19501457bcbc2995555412f352b99fBob Halley r->base = b->base;
48481c9b6e19501457bcbc2995555412f352b99fBob Halley r->length = b->current;
48481c9b6e19501457bcbc2995555412f352b99fBob Halley}
48481c9b6e19501457bcbc2995555412f352b99fBob Halley
48481c9b6e19501457bcbc2995555412f352b99fBob Halleyvoid
48481c9b6e19501457bcbc2995555412f352b99fBob Halleyisc_buffer_remaining(isc_buffer_t *b, isc_region_t *r) {
48481c9b6e19501457bcbc2995555412f352b99fBob Halley /*
b03b67a6f1ea2966367e7beb2ef276ed6a1d3f92Bob Halley * Make 'r' refer to the remaining region of 'b'.
b03b67a6f1ea2966367e7beb2ef276ed6a1d3f92Bob Halley */
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley REQUIRE(ISC_BUFFER_VALID(b));
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley REQUIRE(r != NULL);
b03b67a6f1ea2966367e7beb2ef276ed6a1d3f92Bob Halley
b03b67a6f1ea2966367e7beb2ef276ed6a1d3f92Bob Halley r->base = (unsigned char *)b->base + b->current;
b03b67a6f1ea2966367e7beb2ef276ed6a1d3f92Bob Halley r->length = b->used - b->current;
b03b67a6f1ea2966367e7beb2ef276ed6a1d3f92Bob Halley}
b03b67a6f1ea2966367e7beb2ef276ed6a1d3f92Bob Halley
b03b67a6f1ea2966367e7beb2ef276ed6a1d3f92Bob Halleyvoid
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halleyisc_buffer_active(isc_buffer_t *b, isc_region_t *r) {
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley /*
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley * Make 'r' refer to the active region of 'b'.
1ccbfca64ae86ace521053773001cb995352f96fBob Halley */
1ccbfca64ae86ace521053773001cb995352f96fBob Halley
1ccbfca64ae86ace521053773001cb995352f96fBob Halley REQUIRE(ISC_BUFFER_VALID(b));
1ccbfca64ae86ace521053773001cb995352f96fBob Halley REQUIRE(r != NULL);
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley if (b->current < b->active) {
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley r->base = (unsigned char *)b->base + b->current;
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley r->length = b->active - b->current;
1ccbfca64ae86ace521053773001cb995352f96fBob Halley } else {
0e3ad060581e366d3e682caf2dd5d9ea2e0f4893Bob Halley r->base = NULL;
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley r->length = 0;
1ccbfca64ae86ace521053773001cb995352f96fBob Halley }
1ccbfca64ae86ace521053773001cb995352f96fBob Halley}
1ccbfca64ae86ace521053773001cb995352f96fBob Halley
1ccbfca64ae86ace521053773001cb995352f96fBob Halleyvoid
1ccbfca64ae86ace521053773001cb995352f96fBob Halleyisc_buffer_setactive(isc_buffer_t *b, unsigned int n) {
1ccbfca64ae86ace521053773001cb995352f96fBob Halley unsigned int active;
1ccbfca64ae86ace521053773001cb995352f96fBob Halley
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley /*
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley * Sets the end of the active region 'n' bytes after current.
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley */
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley
1ccbfca64ae86ace521053773001cb995352f96fBob Halley REQUIRE(ISC_BUFFER_VALID(b));
0e3ad060581e366d3e682caf2dd5d9ea2e0f4893Bob Halley active = b->current + n;
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley REQUIRE(active <= b->used);
1ccbfca64ae86ace521053773001cb995352f96fBob Halley
1ccbfca64ae86ace521053773001cb995352f96fBob Halley b->active = active;
1ccbfca64ae86ace521053773001cb995352f96fBob Halley}
1ccbfca64ae86ace521053773001cb995352f96fBob Halley
b8862d5130b88e7b1a257997d7909f769716d51cBob Halleyvoid
b8862d5130b88e7b1a257997d7909f769716d51cBob Halleyisc_buffer_first(isc_buffer_t *b) {
b8862d5130b88e7b1a257997d7909f769716d51cBob Halley /*
b8862d5130b88e7b1a257997d7909f769716d51cBob Halley * Make the consumed region empty.
b8862d5130b88e7b1a257997d7909f769716d51cBob Halley */
b8862d5130b88e7b1a257997d7909f769716d51cBob Halley
b8862d5130b88e7b1a257997d7909f769716d51cBob Halley REQUIRE(ISC_BUFFER_VALID(b));
b8862d5130b88e7b1a257997d7909f769716d51cBob Halley
b8862d5130b88e7b1a257997d7909f769716d51cBob Halley b->current = 0;
b8862d5130b88e7b1a257997d7909f769716d51cBob Halley}
b8862d5130b88e7b1a257997d7909f769716d51cBob Halley
b8862d5130b88e7b1a257997d7909f769716d51cBob Halleyvoid
b8862d5130b88e7b1a257997d7909f769716d51cBob Halleyisc_buffer_forward(isc_buffer_t *b, unsigned int n) {
b8862d5130b88e7b1a257997d7909f769716d51cBob Halley /*
b8862d5130b88e7b1a257997d7909f769716d51cBob Halley * Increase the 'consumed' region of 'b' by 'n' bytes.
b8862d5130b88e7b1a257997d7909f769716d51cBob Halley */
b8862d5130b88e7b1a257997d7909f769716d51cBob Halley
b8862d5130b88e7b1a257997d7909f769716d51cBob Halley REQUIRE(ISC_BUFFER_VALID(b));
1ccbfca64ae86ace521053773001cb995352f96fBob Halley REQUIRE(b->current + n <= b->used);
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley b->current += n;
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley}
1ccbfca64ae86ace521053773001cb995352f96fBob Halley
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halleyvoid
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halleyisc_buffer_back(isc_buffer_t *b, unsigned int n) {
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley /*
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley * Decrease the 'consumed' region of 'b' by 'n' bytes.
1ccbfca64ae86ace521053773001cb995352f96fBob Halley */
0e3ad060581e366d3e682caf2dd5d9ea2e0f4893Bob Halley
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley REQUIRE(ISC_BUFFER_VALID(b));
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley REQUIRE(n <= b->current);
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley
1ccbfca64ae86ace521053773001cb995352f96fBob Halley b->current -= n;
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley}
1ccbfca64ae86ace521053773001cb995352f96fBob Halley
1ccbfca64ae86ace521053773001cb995352f96fBob Halleyvoid
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halleyisc_buffer_compact(isc_buffer_t *b) {
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley unsigned int length;
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley void *src;
1ccbfca64ae86ace521053773001cb995352f96fBob Halley
1ccbfca64ae86ace521053773001cb995352f96fBob Halley /*
1ccbfca64ae86ace521053773001cb995352f96fBob Halley * Compact the used region by moving the remaining region so it occurs
1ccbfca64ae86ace521053773001cb995352f96fBob Halley * at the start of the buffer. The used region is shrunk by the size
1ccbfca64ae86ace521053773001cb995352f96fBob Halley * of the consumed region, and the consumed region is then made empty.
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley */
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley REQUIRE(ISC_BUFFER_VALID(b));
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley src = (unsigned char *)b->base + b->current;
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley length = b->used - b->current;
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley (void)memmove(b->base, src, (size_t)length);
1ccbfca64ae86ace521053773001cb995352f96fBob Halley
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley if (b->active > b->current)
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley b->active -= b->current;
1ccbfca64ae86ace521053773001cb995352f96fBob Halley else
1ccbfca64ae86ace521053773001cb995352f96fBob Halley b->active = 0;
1ccbfca64ae86ace521053773001cb995352f96fBob Halley b->current = 0;
1ccbfca64ae86ace521053773001cb995352f96fBob Halley b->used = length;
ced5499494f8afba75c056eb2f3933de24a5f360Bob Halley}
ced5499494f8afba75c056eb2f3933de24a5f360Bob Halley
63e6086ef99eca768a4cd69871038181251905bbBob Halleyisc_uint8_t
ced5499494f8afba75c056eb2f3933de24a5f360Bob Halleyisc_buffer_getuint8(isc_buffer_t *b) {
ced5499494f8afba75c056eb2f3933de24a5f360Bob Halley unsigned char *cp;
ced5499494f8afba75c056eb2f3933de24a5f360Bob Halley isc_uint8_t result;
e7e2e948e37a0eeb93b0d4f2390f38ed2d9dcd82Bob Halley
e7e2e948e37a0eeb93b0d4f2390f38ed2d9dcd82Bob Halley /*
e7e2e948e37a0eeb93b0d4f2390f38ed2d9dcd82Bob Halley * Read an unsigned 8-bit integer from 'b' and return it.
e7e2e948e37a0eeb93b0d4f2390f38ed2d9dcd82Bob Halley */
e7e2e948e37a0eeb93b0d4f2390f38ed2d9dcd82Bob Halley
e7e2e948e37a0eeb93b0d4f2390f38ed2d9dcd82Bob Halley REQUIRE(ISC_BUFFER_VALID(b));
e7e2e948e37a0eeb93b0d4f2390f38ed2d9dcd82Bob Halley REQUIRE(b->used - b->current >= 1);
e7e2e948e37a0eeb93b0d4f2390f38ed2d9dcd82Bob Halley
e7e2e948e37a0eeb93b0d4f2390f38ed2d9dcd82Bob Halley cp = b->base;
e7e2e948e37a0eeb93b0d4f2390f38ed2d9dcd82Bob Halley cp += b->current;
e7e2e948e37a0eeb93b0d4f2390f38ed2d9dcd82Bob Halley b->current += 1;
e7e2e948e37a0eeb93b0d4f2390f38ed2d9dcd82Bob Halley result = ((unsigned int)(cp[0]));
63e6086ef99eca768a4cd69871038181251905bbBob Halley
ced5499494f8afba75c056eb2f3933de24a5f360Bob Halley return (result);
ced5499494f8afba75c056eb2f3933de24a5f360Bob Halley}
ced5499494f8afba75c056eb2f3933de24a5f360Bob Halley
1ccbfca64ae86ace521053773001cb995352f96fBob Halleyvoid
1ccbfca64ae86ace521053773001cb995352f96fBob Halleyisc_buffer_putuint8(isc_buffer_t *b, isc_uint8_t val)
1ccbfca64ae86ace521053773001cb995352f96fBob Halley{
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley unsigned char *cp;
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley REQUIRE(ISC_BUFFER_VALID(b));
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley REQUIRE(b->used + 1 <= b->length);
1ccbfca64ae86ace521053773001cb995352f96fBob Halley
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley cp = b->base;
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley cp += b->used;
1ccbfca64ae86ace521053773001cb995352f96fBob Halley b->used += 1;
1ccbfca64ae86ace521053773001cb995352f96fBob Halley cp[0] = (val & 0x00ff);
1ccbfca64ae86ace521053773001cb995352f96fBob Halley}
1ccbfca64ae86ace521053773001cb995352f96fBob Halley
1ccbfca64ae86ace521053773001cb995352f96fBob Halleyisc_uint16_t
1ccbfca64ae86ace521053773001cb995352f96fBob Halleyisc_buffer_getuint16(isc_buffer_t *b) {
1ccbfca64ae86ace521053773001cb995352f96fBob Halley unsigned char *cp;
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley isc_uint16_t result;
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley /*
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley * Read an unsigned 16-bit integer in network byte order from 'b',
1ccbfca64ae86ace521053773001cb995352f96fBob Halley * convert it to host byte order, and return it.
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley */
1ccbfca64ae86ace521053773001cb995352f96fBob Halley
1ccbfca64ae86ace521053773001cb995352f96fBob Halley REQUIRE(ISC_BUFFER_VALID(b));
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley REQUIRE(b->used - b->current >= 2);
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley
1ccbfca64ae86ace521053773001cb995352f96fBob Halley cp = b->base;
1ccbfca64ae86ace521053773001cb995352f96fBob Halley cp += b->current;
097c31fdea44383b7ce95345a66489040ad5e333Bob Halley b->current += 2;
097c31fdea44383b7ce95345a66489040ad5e333Bob Halley result = ((unsigned int)(cp[0])) << 8;
097c31fdea44383b7ce95345a66489040ad5e333Bob Halley result |= ((unsigned int)(cp[1]));
097c31fdea44383b7ce95345a66489040ad5e333Bob Halley
097c31fdea44383b7ce95345a66489040ad5e333Bob Halley return (result);
097c31fdea44383b7ce95345a66489040ad5e333Bob Halley}
097c31fdea44383b7ce95345a66489040ad5e333Bob Halley
097c31fdea44383b7ce95345a66489040ad5e333Bob Halleyvoid
097c31fdea44383b7ce95345a66489040ad5e333Bob Halleyisc_buffer_putuint16(isc_buffer_t *b, isc_uint16_t val)
097c31fdea44383b7ce95345a66489040ad5e333Bob Halley{
097c31fdea44383b7ce95345a66489040ad5e333Bob Halley unsigned char *cp;
097c31fdea44383b7ce95345a66489040ad5e333Bob Halley
097c31fdea44383b7ce95345a66489040ad5e333Bob Halley REQUIRE(ISC_BUFFER_VALID(b));
8c65ae482a50bed3184026301c6f99f32a683dbfBob Halley REQUIRE(b->used + 2 <= b->length);
8c65ae482a50bed3184026301c6f99f32a683dbfBob Halley
8c65ae482a50bed3184026301c6f99f32a683dbfBob Halley cp = b->base;
8c65ae482a50bed3184026301c6f99f32a683dbfBob Halley cp += b->used;
8c65ae482a50bed3184026301c6f99f32a683dbfBob Halley b->used += 2;
6bb7b678f50a61dccad0bb3db4c26f73b59d8c16Bob Halley cp[0] = (val & 0xff00) >> 8;
8c65ae482a50bed3184026301c6f99f32a683dbfBob Halley cp[1] = (val & 0x00ff);
8c65ae482a50bed3184026301c6f99f32a683dbfBob Halley}
8c65ae482a50bed3184026301c6f99f32a683dbfBob Halley
8c65ae482a50bed3184026301c6f99f32a683dbfBob Halleyisc_uint32_t
8c65ae482a50bed3184026301c6f99f32a683dbfBob Halleyisc_buffer_getuint32(isc_buffer_t *b) {
8c65ae482a50bed3184026301c6f99f32a683dbfBob Halley unsigned char *cp;
8c65ae482a50bed3184026301c6f99f32a683dbfBob Halley isc_uint32_t result;
8c65ae482a50bed3184026301c6f99f32a683dbfBob Halley
8c65ae482a50bed3184026301c6f99f32a683dbfBob Halley /*
6bb7b678f50a61dccad0bb3db4c26f73b59d8c16Bob Halley * Read an unsigned 32-bit integer in network byte order from 'b',
8c65ae482a50bed3184026301c6f99f32a683dbfBob Halley * convert it to host byte order, and return it.
8c65ae482a50bed3184026301c6f99f32a683dbfBob Halley */
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley REQUIRE(ISC_BUFFER_VALID(b));
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley REQUIRE(b->used - b->current >= 4);
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley
1ccbfca64ae86ace521053773001cb995352f96fBob Halley cp = b->base;
1ccbfca64ae86ace521053773001cb995352f96fBob Halley cp += b->current;
63e6086ef99eca768a4cd69871038181251905bbBob Halley b->current += 4;
63e6086ef99eca768a4cd69871038181251905bbBob Halley result = ((unsigned int)(cp[0])) << 24;
1ccbfca64ae86ace521053773001cb995352f96fBob Halley result |= ((unsigned int)(cp[1])) << 16;
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley result |= ((unsigned int)(cp[2])) << 8;
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley result |= ((unsigned int)(cp[3]));
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley return (result);
1ccbfca64ae86ace521053773001cb995352f96fBob Halley}
1ccbfca64ae86ace521053773001cb995352f96fBob Halley
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halleyvoid
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halleyisc_buffer_putuint32(isc_buffer_t *b, isc_uint32_t val)
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley{
63e6086ef99eca768a4cd69871038181251905bbBob Halley unsigned char *cp;
1ccbfca64ae86ace521053773001cb995352f96fBob Halley
63e6086ef99eca768a4cd69871038181251905bbBob Halley REQUIRE(ISC_BUFFER_VALID(b));
1ccbfca64ae86ace521053773001cb995352f96fBob Halley REQUIRE(b->used + 4 <= b->length);
1ccbfca64ae86ace521053773001cb995352f96fBob Halley
1ccbfca64ae86ace521053773001cb995352f96fBob Halley cp = b->base;
efe6d8f0665b466052910e8efd4b031dc048f196Bob Halley cp += b->used;
efe6d8f0665b466052910e8efd4b031dc048f196Bob Halley b->used += 4;
63e6086ef99eca768a4cd69871038181251905bbBob Halley cp[0] = (unsigned char)((val & 0xff000000) >> 24);
efe6d8f0665b466052910e8efd4b031dc048f196Bob Halley cp[1] = (unsigned char)((val & 0x00ff0000) >> 16);
efe6d8f0665b466052910e8efd4b031dc048f196Bob Halley cp[2] = (unsigned char)((val & 0x0000ff00) >> 8);
efe6d8f0665b466052910e8efd4b031dc048f196Bob Halley cp[3] = (unsigned char)(val & 0x000000ff);
efe6d8f0665b466052910e8efd4b031dc048f196Bob Halley}
efe6d8f0665b466052910e8efd4b031dc048f196Bob Halley
efe6d8f0665b466052910e8efd4b031dc048f196Bob Halleyvoid
efe6d8f0665b466052910e8efd4b031dc048f196Bob Halleyisc_buffer_putmem(isc_buffer_t *b, unsigned char *base, unsigned int length)
efe6d8f0665b466052910e8efd4b031dc048f196Bob Halley{
efe6d8f0665b466052910e8efd4b031dc048f196Bob Halley unsigned char *cp;
63e6086ef99eca768a4cd69871038181251905bbBob Halley
63e6086ef99eca768a4cd69871038181251905bbBob Halley REQUIRE(ISC_BUFFER_VALID(b));
efe6d8f0665b466052910e8efd4b031dc048f196Bob Halley REQUIRE(b->used + length <= b->length);
efe6d8f0665b466052910e8efd4b031dc048f196Bob Halley
1ccbfca64ae86ace521053773001cb995352f96fBob Halley cp = (unsigned char *)b->base + b->used;
1ccbfca64ae86ace521053773001cb995352f96fBob Halley memcpy(cp, base, length);
63e6086ef99eca768a4cd69871038181251905bbBob Halley b->used += length;
1ccbfca64ae86ace521053773001cb995352f96fBob Halley}
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halleyisc_result_t
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halleyisc_buffer_putstr(isc_buffer_t *b, const char *source) {
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley unsigned int l;
1ccbfca64ae86ace521053773001cb995352f96fBob Halley unsigned char *cp;
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley
5c6d49484bf5600e3c2bcb3165e168bd058d167bBob Halley REQUIRE(ISC_BUFFER_VALID(b));
5c6d49484bf5600e3c2bcb3165e168bd058d167bBob Halley REQUIRE(source != NULL);
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley l = strlen(source);
1ccbfca64ae86ace521053773001cb995352f96fBob Halley if (l > (b->length - b->used))
63e6086ef99eca768a4cd69871038181251905bbBob Halley return (ISC_R_NOSPACE);
1ccbfca64ae86ace521053773001cb995352f96fBob Halley
1ccbfca64ae86ace521053773001cb995352f96fBob Halley cp = (unsigned char *)b->base + b->used;
1ccbfca64ae86ace521053773001cb995352f96fBob Halley memcpy(cp, source, l);
1ccbfca64ae86ace521053773001cb995352f96fBob Halley b->used += l;
1ccbfca64ae86ace521053773001cb995352f96fBob Halley
1ccbfca64ae86ace521053773001cb995352f96fBob Halley return (ISC_R_SUCCESS);
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley}
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halleyisc_result_t
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halleyisc_buffer_copyregion(isc_buffer_t *b, isc_region_t *r) {
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley unsigned char *base;
1ccbfca64ae86ace521053773001cb995352f96fBob Halley unsigned int available;
62837b832f6a9999976d607eb0a9125bbbbb138bBob Halley
a3e2ec2dfec1a8171eaf584bb957b80c04de1e0bBob Halley REQUIRE(ISC_BUFFER_VALID(b));
a3e2ec2dfec1a8171eaf584bb957b80c04de1e0bBob Halley REQUIRE(r != NULL);
1ccbfca64ae86ace521053773001cb995352f96fBob Halley
1ccbfca64ae86ace521053773001cb995352f96fBob Halley base = (unsigned char *)b->base + b->used;
1ccbfca64ae86ace521053773001cb995352f96fBob Halley available = b->length - b->used;
if (r->length > available)
return (ISC_R_NOSPACE);
memcpy(base, r->base, r->length);
b->used += r->length;
return (ISC_R_SUCCESS);
}
isc_result_t
isc_buffer_allocate(isc_mem_t *mctx, isc_buffer_t **dynbuffer,
unsigned int length, unsigned int type)
{
isc_buffer_t *dbuf;
REQUIRE(dynbuffer != NULL);
REQUIRE(*dynbuffer == NULL);
dbuf = isc_mem_get(mctx, length + sizeof(isc_buffer_t));
if (dbuf == NULL)
return (ISC_R_NOMEMORY);
isc_buffer_init(dbuf, ((unsigned char *)dbuf) + sizeof(isc_buffer_t),
length, type);
dbuf->mctx = mctx;
*dynbuffer = dbuf;
return (ISC_R_SUCCESS);
}
void
isc_buffer_free(isc_buffer_t **dynbuffer)
{
unsigned int real_length;
isc_buffer_t *dbuf;
isc_mem_t *mctx;
REQUIRE(dynbuffer != NULL);
REQUIRE(ISC_BUFFER_VALID(*dynbuffer));
REQUIRE((*dynbuffer)->mctx != NULL);
dbuf = *dynbuffer;
*dynbuffer = NULL; /* destroy external reference */
real_length = dbuf->length + sizeof(isc_buffer_t);
mctx = dbuf->mctx;
dbuf->mctx = NULL;
isc_buffer_invalidate(dbuf);
isc_mem_put(mctx, dbuf, real_length);
}