buffer.c revision d8d845eb478e6eae13c787787a3921e24284b593
7d32c065c7bb56f281651ae3dd2888f32ce4f1d9Bob Halley * Copyright (C) 1998, 1999, 2000 Internet Software Consortium.
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 * 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 Gustafssonisc__buffer_init(isc_buffer_t *b, void *base, unsigned int length) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Make 'b' refer to the 'length'-byte region starting at base.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Make 'b' an invalid buffer.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonisc__buffer_region(isc_buffer_t *b, isc_region_t *r) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Make 'r' refer to the region of 'b'.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonisc__buffer_usedregion(isc_buffer_t *b, isc_region_t *r) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Make 'r' refer to the used region of 'b'.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonisc__buffer_availableregion(isc_buffer_t *b, isc_region_t *r) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Make 'r' refer to the available region of 'b'.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonisc__buffer_add(isc_buffer_t *b, unsigned int n) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Increase the 'used' region of 'b' by 'n' bytes.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonisc__buffer_subtract(isc_buffer_t *b, unsigned int n) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Decrease the 'used' region of 'b' by 'n' bytes.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Make the used region empty.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonisc__buffer_consumedregion(isc_buffer_t *b, isc_region_t *r) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Make 'r' refer to the consumed region of 'b'.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonisc__buffer_remainingregion(isc_buffer_t *b, isc_region_t *r) {
f80ea74e1984e0b1dbe48dd86ecdd3a2380393cbBob Halley * Make 'r' refer to the remaining region of 'b'.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonisc__buffer_activeregion(isc_buffer_t *b, isc_region_t *r) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Make 'r' refer to the active region of 'b'.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonisc__buffer_setactive(isc_buffer_t *b, unsigned int n) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Sets the end of the active region 'n' bytes after current.
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson * Make the consumed region empty.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonisc__buffer_forward(isc_buffer_t *b, unsigned int n) {
f80ea74e1984e0b1dbe48dd86ecdd3a2380393cbBob Halley * Increase the 'consumed' region of 'b' by 'n' bytes.
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafssonisc__buffer_back(isc_buffer_t *b, unsigned int n) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Decrease the 'consumed' region of 'b' by 'n' bytes.
f80ea74e1984e0b1dbe48dd86ecdd3a2380393cbBob Halley unsigned int length;
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 src = (unsigned char *)b->base + b->current;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson (void)memmove(b->base, src, (size_t)length);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson unsigned char *cp;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Read an unsigned 8-bit integer from 'b' and return it.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonisc__buffer_putuint8(isc_buffer_t *b, isc_uint8_t val)
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson unsigned char *cp;
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 Gustafssonisc__buffer_putuint16(isc_buffer_t *b, isc_uint16_t val)
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson unsigned char *cp;
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.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonisc__buffer_putuint32(isc_buffer_t *b, isc_uint32_t val)
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafssonisc__buffer_putmem(isc_buffer_t *b, unsigned char *base, unsigned int length) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonisc_buffer_putstr(isc_buffer_t *b, const char *source) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson unsigned int l;
f80ea74e1984e0b1dbe48dd86ecdd3a2380393cbBob Halley unsigned char *cp;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson REQUIRE(l <= isc_buffer_availablelength(b));
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafssonisc_buffer_copyregion(isc_buffer_t *b, isc_region_t *r) {
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson unsigned char *base;
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson base = (unsigned char *)b->base + b->used;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonisc_buffer_allocate(isc_mem_t *mctx, isc_buffer_t **dynbuffer,
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson dbuf = isc_mem_get(mctx, length + sizeof(isc_buffer_t));
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson isc_buffer_init(dbuf, ((unsigned char *)dbuf) + sizeof(isc_buffer_t),
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson *dynbuffer = NULL; /* destroy external reference */