buffer.c revision d8d845eb478e6eae13c787787a3921e24284b593
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson/*
7d32c065c7bb56f281651ae3dd2888f32ce4f1d9Bob Halley * Copyright (C) 1998, 1999, 2000 Internet Software Consortium.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson *
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Permission to use, copy, modify, and distribute this software for any
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * purpose with or without fee is hereby granted, provided that the above
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * copyright notice and this permission notice appear in all copies.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson *
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * SOFTWARE.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
fc8a6561cbb0a1fea849950a3217aacd3b8f1eacDavid Lawrence#include <config.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <string.h>
f80ea74e1984e0b1dbe48dd86ecdd3a2380393cbBob Halley
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <isc/assertions.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <isc/buffer.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <isc/mem.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <isc/region.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <isc/result.h>
fc8a6561cbb0a1fea849950a3217aacd3b8f1eacDavid Lawrence
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafssonvoid
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonisc__buffer_init(isc_buffer_t *b, void *base, unsigned int length) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson /*
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Make 'b' refer to the 'length'-byte region starting at base.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson REQUIRE(b != NULL);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson ISC__BUFFER_INIT(b, base, length);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson}
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonvoid
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonisc__buffer_invalidate(isc_buffer_t *b) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson /*
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Make 'b' an invalid buffer.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson REQUIRE(ISC_BUFFER_VALID(b));
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson REQUIRE(!ISC_LINK_LINKED(b, link));
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson REQUIRE(b->mctx == NULL);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson ISC__BUFFER_INVALIDATE(b);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson}
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonvoid
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonisc__buffer_region(isc_buffer_t *b, isc_region_t *r) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson /*
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Make 'r' refer to the region of 'b'.
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson */
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson REQUIRE(ISC_BUFFER_VALID(b));
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson REQUIRE(r != NULL);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson ISC__BUFFER_REGION(b, r);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson}
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafssonvoid
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonisc__buffer_usedregion(isc_buffer_t *b, isc_region_t *r) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson /*
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Make 'r' refer to the used region of 'b'.
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson REQUIRE(ISC_BUFFER_VALID(b));
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson REQUIRE(r != NULL);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson ISC__BUFFER_USEDREGION(b, r);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson}
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonvoid
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonisc__buffer_availableregion(isc_buffer_t *b, isc_region_t *r) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson /*
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Make 'r' refer to the available region of 'b'.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson REQUIRE(ISC_BUFFER_VALID(b));
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson REQUIRE(r != NULL);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson ISC__BUFFER_AVAILABLEREGION(b, r);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson}
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonvoid
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonisc__buffer_add(isc_buffer_t *b, unsigned int n) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson /*
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Increase the 'used' region of 'b' by 'n' bytes.
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson */
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson REQUIRE(ISC_BUFFER_VALID(b));
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson REQUIRE(b->used + n <= b->length);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson ISC__BUFFER_ADD(b, n);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson}
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonvoid
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonisc__buffer_subtract(isc_buffer_t *b, unsigned int n) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson /*
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Decrease the 'used' region of 'b' by 'n' bytes.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson REQUIRE(ISC_BUFFER_VALID(b));
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson REQUIRE(b->used >= n);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson ISC__BUFFER_SUBTRACT(b, n);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson}
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonvoid
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonisc__buffer_clear(isc_buffer_t *b) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson /*
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Make the used region empty.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson REQUIRE(ISC_BUFFER_VALID(b));
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson ISC__BUFFER_CLEAR(b);
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson}
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonvoid
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonisc__buffer_consumedregion(isc_buffer_t *b, isc_region_t *r) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson /*
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Make 'r' refer to the consumed region of 'b'.
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson REQUIRE(ISC_BUFFER_VALID(b));
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson REQUIRE(r != NULL);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson ISC__BUFFER_CONSUMEDREGION(b, r);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson}
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonvoid
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonisc__buffer_remainingregion(isc_buffer_t *b, isc_region_t *r) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson /*
f80ea74e1984e0b1dbe48dd86ecdd3a2380393cbBob Halley * Make 'r' refer to the remaining region of 'b'.
f80ea74e1984e0b1dbe48dd86ecdd3a2380393cbBob Halley */
f80ea74e1984e0b1dbe48dd86ecdd3a2380393cbBob Halley
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson REQUIRE(ISC_BUFFER_VALID(b));
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson REQUIRE(r != NULL);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson ISC__BUFFER_REMAININGREGION(b, r);
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson}
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafssonvoid
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonisc__buffer_activeregion(isc_buffer_t *b, isc_region_t *r) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson /*
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Make 'r' refer to the active region of 'b'.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson REQUIRE(ISC_BUFFER_VALID(b));
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson REQUIRE(r != NULL);
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson ISC__BUFFER_ACTIVEREGION(b, r);
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson}
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonvoid
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonisc__buffer_setactive(isc_buffer_t *b, unsigned int n) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson /*
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Sets the end of the active region 'n' bytes after current.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson */
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson REQUIRE(ISC_BUFFER_VALID(b));
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff REQUIRE(b->current + n <= b->used);
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson ISC__BUFFER_SETACTIVE(b, n);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson}
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafssonvoid
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafssonisc__buffer_first(isc_buffer_t *b) {
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson /*
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson * Make the consumed region empty.
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson */
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson
f80ea74e1984e0b1dbe48dd86ecdd3a2380393cbBob Halley REQUIRE(ISC_BUFFER_VALID(b));
f80ea74e1984e0b1dbe48dd86ecdd3a2380393cbBob Halley
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson ISC__BUFFER_FIRST(b);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson}
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonvoid
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonisc__buffer_forward(isc_buffer_t *b, unsigned int n) {
f80ea74e1984e0b1dbe48dd86ecdd3a2380393cbBob Halley /*
f80ea74e1984e0b1dbe48dd86ecdd3a2380393cbBob Halley * Increase the 'consumed' region of 'b' by 'n' bytes.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson REQUIRE(ISC_BUFFER_VALID(b));
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson REQUIRE(b->current + n <= b->used);
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson ISC__BUFFER_FORWARD(b, n);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson}
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonvoid
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafssonisc__buffer_back(isc_buffer_t *b, unsigned int n) {
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson /*
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Decrease the 'consumed' region of 'b' by 'n' bytes.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson REQUIRE(ISC_BUFFER_VALID(b));
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson REQUIRE(n <= b->current);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson ISC__BUFFER_BACK(b, n);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson}
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonvoid
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonisc_buffer_compact(isc_buffer_t *b) {
f80ea74e1984e0b1dbe48dd86ecdd3a2380393cbBob Halley unsigned int length;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson void *src;
f80ea74e1984e0b1dbe48dd86ecdd3a2380393cbBob Halley
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson /*
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Compact the used region by moving the remaining region so it occurs
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * at the start of the buffer. The used region is shrunk by the size
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * of the consumed region, and the consumed region is then made empty.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson REQUIRE(ISC_BUFFER_VALID(b));
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson src = (unsigned char *)b->base + b->current;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson length = b->used - b->current;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson (void)memmove(b->base, src, (size_t)length);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (b->active > b->current)
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson b->active -= b->current;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson else
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson b->active = 0;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson b->current = 0;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson b->used = length;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson}
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonisc_uint8_t
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonisc_buffer_getuint8(isc_buffer_t *b) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson unsigned char *cp;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson isc_uint8_t result;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson /*
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Read an unsigned 8-bit integer from 'b' and return it.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson */
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson REQUIRE(ISC_BUFFER_VALID(b));
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson REQUIRE(b->used - b->current >= 1);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson cp = b->base;
5dbf258e2c780189fa30d51a872d8edb372e5589Bob Halley cp += b->current;
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson b->current += 1;
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson result = ((unsigned int)(cp[0]));
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson return (result);
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson}
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonvoid
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonisc__buffer_putuint8(isc_buffer_t *b, isc_uint8_t val)
b120e39deac3e9c323cdc7801ed2313f8ebbb744Andreas Gustafsson{
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson REQUIRE(ISC_BUFFER_VALID(b));
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson REQUIRE(b->used + 1 <= b->length);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson ISC__BUFFER_PUTUINT8(b, val);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson}
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonisc_uint16_t
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonisc_buffer_getuint16(isc_buffer_t *b) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson unsigned char *cp;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson isc_uint16_t result;
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson /*
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Read an unsigned 16-bit integer in network byte order from 'b',
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * convert it to host byte order, and return it.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson REQUIRE(ISC_BUFFER_VALID(b));
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson REQUIRE(b->used - b->current >= 2);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson cp = b->base;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson cp += b->current;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson b->current += 2;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson result = ((unsigned int)(cp[0])) << 8;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson result |= ((unsigned int)(cp[1]));
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson return (result);
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson}
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonvoid
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonisc__buffer_putuint16(isc_buffer_t *b, isc_uint16_t val)
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson{
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson REQUIRE(ISC_BUFFER_VALID(b));
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson REQUIRE(b->used + 2 <= b->length);
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson ISC__BUFFER_PUTUINT16(b, val);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson}
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonisc_uint32_t
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonisc_buffer_getuint32(isc_buffer_t *b) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson unsigned char *cp;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson isc_uint32_t result;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson /*
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Read an unsigned 32-bit integer in network byte order from 'b',
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson * convert it to host byte order, and return it.
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson */
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson REQUIRE(ISC_BUFFER_VALID(b));
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson REQUIRE(b->used - b->current >= 4);
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson cp = b->base;
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson cp += b->current;
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson b->current += 4;
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson result = ((unsigned int)(cp[0])) << 24;
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson result |= ((unsigned int)(cp[1])) << 16;
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson result |= ((unsigned int)(cp[2])) << 8;
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson result |= ((unsigned int)(cp[3]));
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson return (result);
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson}
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonvoid
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonisc__buffer_putuint32(isc_buffer_t *b, isc_uint32_t val)
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson{
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson REQUIRE(ISC_BUFFER_VALID(b));
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson REQUIRE(b->used + 4 <= b->length);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson ISC__BUFFER_PUTUINT32(b, val);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson}
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonvoid
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafssonisc__buffer_putmem(isc_buffer_t *b, unsigned char *base, unsigned int length) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson REQUIRE(ISC_BUFFER_VALID(b));
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson REQUIRE(b->used + length <= b->length);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson ISC__BUFFER_PUTMEM(b, base, length);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson}
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonvoid
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonisc_buffer_putstr(isc_buffer_t *b, const char *source) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson unsigned int l;
f80ea74e1984e0b1dbe48dd86ecdd3a2380393cbBob Halley unsigned char *cp;
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson REQUIRE(ISC_BUFFER_VALID(b));
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson REQUIRE(source != NULL);
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson l = strlen(source);
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson REQUIRE(l <= isc_buffer_availablelength(b));
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
e485d16462b0a66b835107fae04fe9d562e77af0Bob Halley cp = isc_buffer_used(b);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson memcpy(cp, source, l);
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson b->used += l;
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson}
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafssonisc_result_t
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafssonisc_buffer_copyregion(isc_buffer_t *b, isc_region_t *r) {
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson unsigned char *base;
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson unsigned int available;
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson REQUIRE(ISC_BUFFER_VALID(b));
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson REQUIRE(r != NULL);
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson base = (unsigned char *)b->base + b->used;
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson available = b->length - b->used;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (r->length > available)
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson return (ISC_R_NOSPACE);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson memcpy(base, r->base, r->length);
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson b->used += r->length;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson return (ISC_R_SUCCESS);
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson}
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafssonisc_result_t
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonisc_buffer_allocate(isc_mem_t *mctx, isc_buffer_t **dynbuffer,
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson unsigned int length)
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson{
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson isc_buffer_t *dbuf;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson REQUIRE(dynbuffer != NULL);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson REQUIRE(*dynbuffer == NULL);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson dbuf = isc_mem_get(mctx, length + sizeof(isc_buffer_t));
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson if (dbuf == NULL)
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson return (ISC_R_NOMEMORY);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson isc_buffer_init(dbuf, ((unsigned char *)dbuf) + sizeof(isc_buffer_t),
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson length);
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson dbuf->mctx = mctx;
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson *dynbuffer = dbuf;
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson return (ISC_R_SUCCESS);
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson}
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonvoid
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonisc_buffer_free(isc_buffer_t **dynbuffer)
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson{
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson unsigned int real_length;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson isc_buffer_t *dbuf;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson isc_mem_t *mctx;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson REQUIRE(dynbuffer != NULL);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson REQUIRE(ISC_BUFFER_VALID(*dynbuffer));
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson REQUIRE((*dynbuffer)->mctx != NULL);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson dbuf = *dynbuffer;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson *dynbuffer = NULL; /* destroy external reference */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson real_length = dbuf->length + sizeof(isc_buffer_t);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson mctx = dbuf->mctx;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson dbuf->mctx = NULL;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson isc_buffer_invalidate(dbuf);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson isc_mem_put(mctx, dbuf, real_length);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson}
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson