1998-12-12 20:48:14 +00:00
|
|
|
/*
|
1999-01-06 05:37:54 +00:00
|
|
|
* Copyright (C) 1998, 1999 Internet Software Consortium.
|
1998-12-12 20:48:14 +00:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
|
|
|
|
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
|
|
|
|
* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
|
|
|
|
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
|
|
|
|
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
1998-10-29 02:01:29 +00:00
|
|
|
|
1998-12-12 19:25:20 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
1998-10-29 02:01:29 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <isc/assertions.h>
|
|
|
|
#include <isc/buffer.h>
|
|
|
|
|
1999-01-06 05:37:54 +00:00
|
|
|
#define VALID_BUFFER(b) ((b) != NULL && \
|
|
|
|
(b)->magic == BUFFER_MAGIC)
|
|
|
|
#define BUFFER_MAGIC 0x42756621U /* Buf!. */
|
1998-10-29 02:01:29 +00:00
|
|
|
|
|
|
|
void
|
1999-01-06 05:37:54 +00:00
|
|
|
isc_buffer_init(isc_buffer_t *b, unsigned char *base, unsigned int length,
|
|
|
|
unsigned int type) {
|
1998-10-29 02:01:29 +00:00
|
|
|
/*
|
|
|
|
* Make 'b' refer to the 'length'-byte region starting at base.
|
|
|
|
*/
|
|
|
|
|
1999-01-06 05:37:54 +00:00
|
|
|
REQUIRE(b != NULL);
|
1998-10-29 02:01:29 +00:00
|
|
|
|
1999-01-06 05:37:54 +00:00
|
|
|
b->magic = BUFFER_MAGIC;
|
|
|
|
b->type = type;
|
1998-10-29 02:01:29 +00:00
|
|
|
b->base = base;
|
|
|
|
b->length = length;
|
|
|
|
b->used = 0;
|
1999-01-06 05:37:54 +00:00
|
|
|
b->current = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
isc_buffer_invalidate(isc_buffer_t *b) {
|
|
|
|
/*
|
|
|
|
* Make 'b' an invalid buffer.
|
|
|
|
*/
|
|
|
|
|
|
|
|
REQUIRE(VALID_BUFFER(b));
|
|
|
|
|
|
|
|
b->magic = 0;
|
|
|
|
b->type = 0;
|
|
|
|
b->base = NULL;
|
|
|
|
b->length = 0;
|
|
|
|
b->used = 0;
|
|
|
|
b->current = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int
|
|
|
|
isc_buffer_type(isc_buffer_t *b) {
|
|
|
|
/*
|
|
|
|
* The type of 'b'.
|
|
|
|
*/
|
|
|
|
|
|
|
|
REQUIRE(VALID_BUFFER(b));
|
|
|
|
|
|
|
|
return (b->type);
|
1998-10-29 02:01:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_buffer_region(isc_buffer_t *b, isc_region_t *r) {
|
1998-10-29 02:01:29 +00:00
|
|
|
/*
|
|
|
|
* Make 'r' refer to the region of 'b'.
|
|
|
|
*/
|
|
|
|
|
|
|
|
REQUIRE(VALID_BUFFER(b));
|
|
|
|
REQUIRE(r != NULL);
|
|
|
|
|
|
|
|
r->base = b->base;
|
|
|
|
r->length = b->length;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_buffer_used(isc_buffer_t *b, isc_region_t *r) {
|
1998-10-29 02:01:29 +00:00
|
|
|
/*
|
|
|
|
* Make 'r' refer to the used region of 'b'.
|
|
|
|
*/
|
|
|
|
|
|
|
|
REQUIRE(VALID_BUFFER(b));
|
|
|
|
REQUIRE(r != NULL);
|
|
|
|
|
|
|
|
r->base = b->base;
|
|
|
|
r->length = b->used;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_buffer_available(isc_buffer_t *b, isc_region_t *r) {
|
1998-10-29 02:01:29 +00:00
|
|
|
/*
|
|
|
|
* Make 'r' refer to the available region of 'b'.
|
|
|
|
*/
|
|
|
|
|
|
|
|
REQUIRE(VALID_BUFFER(b));
|
|
|
|
REQUIRE(r != NULL);
|
|
|
|
|
|
|
|
r->base = b->base + b->used;
|
|
|
|
r->length = b->length - b->used;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_buffer_add(isc_buffer_t *b, unsigned int n) {
|
1998-10-29 02:01:29 +00:00
|
|
|
/*
|
|
|
|
* Increase the 'used' region of 'b' by 'n' bytes.
|
|
|
|
*/
|
|
|
|
|
|
|
|
REQUIRE(VALID_BUFFER(b));
|
|
|
|
REQUIRE(b->used + n <= b->length);
|
|
|
|
|
|
|
|
b->used += n;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_buffer_subtract(isc_buffer_t *b, unsigned int n) {
|
1998-10-29 02:01:29 +00:00
|
|
|
/*
|
|
|
|
* Decrease the 'used' region of 'b' by 'n' bytes.
|
|
|
|
*/
|
|
|
|
|
|
|
|
REQUIRE(VALID_BUFFER(b));
|
|
|
|
REQUIRE(b->used >= n);
|
|
|
|
|
|
|
|
b->used -= n;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_buffer_clear(isc_buffer_t *b) {
|
1998-10-29 02:01:29 +00:00
|
|
|
/*
|
|
|
|
* Make the used region empty.
|
|
|
|
*/
|
|
|
|
|
|
|
|
REQUIRE(VALID_BUFFER(b));
|
1999-01-06 05:37:54 +00:00
|
|
|
|
1998-10-29 02:01:29 +00:00
|
|
|
b->used = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_buffer_consumed(isc_buffer_t *b, isc_region_t *r) {
|
1998-10-29 02:01:29 +00:00
|
|
|
/*
|
|
|
|
* Make 'r' refer to the consumed region of 'b'.
|
|
|
|
*/
|
|
|
|
|
|
|
|
REQUIRE(VALID_BUFFER(b));
|
|
|
|
REQUIRE(r != NULL);
|
|
|
|
|
|
|
|
r->base = b->base;
|
1999-01-06 05:37:54 +00:00
|
|
|
r->length = b->current;
|
1998-10-29 02:01:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_buffer_remaining(isc_buffer_t *b, isc_region_t *r) {
|
1998-10-29 02:01:29 +00:00
|
|
|
/*
|
|
|
|
* Make 'r' refer to the remaining region of 'b'.
|
|
|
|
*/
|
|
|
|
|
|
|
|
REQUIRE(VALID_BUFFER(b));
|
|
|
|
REQUIRE(r != NULL);
|
|
|
|
|
1999-01-06 05:37:54 +00:00
|
|
|
r->base = (unsigned char *)b->base + b->current;
|
|
|
|
r->length = b->used - b->current;
|
1998-10-29 02:01:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_buffer_first(isc_buffer_t *b) {
|
1998-10-29 02:01:29 +00:00
|
|
|
/*
|
|
|
|
* Make the consumed region empty.
|
|
|
|
*/
|
|
|
|
|
|
|
|
REQUIRE(VALID_BUFFER(b));
|
|
|
|
|
1999-01-06 05:37:54 +00:00
|
|
|
b->current = 0;
|
1998-10-29 02:01:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_buffer_forward(isc_buffer_t *b, unsigned int n) {
|
1998-10-29 02:01:29 +00:00
|
|
|
/*
|
1999-01-06 05:37:54 +00:00
|
|
|
* Increase the 'consumed' region of 'b' by 'n' bytes.
|
1998-10-29 02:01:29 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
REQUIRE(VALID_BUFFER(b));
|
1999-01-06 05:37:54 +00:00
|
|
|
REQUIRE(b->current + n <= b->used);
|
1998-10-29 02:01:29 +00:00
|
|
|
|
|
|
|
b->current += n;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_buffer_back(isc_buffer_t *b, unsigned int n) {
|
1998-10-29 02:01:29 +00:00
|
|
|
/*
|
|
|
|
* Decrease the 'consumed' region of 'b' by 'n' bytes.
|
|
|
|
*/
|
|
|
|
|
|
|
|
REQUIRE(VALID_BUFFER(b));
|
1999-01-06 05:37:54 +00:00
|
|
|
REQUIRE(n <= b->current);
|
1998-10-29 02:01:29 +00:00
|
|
|
|
|
|
|
b->current -= n;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_buffer_compact(isc_buffer_t *b) {
|
1998-10-29 02:01:29 +00:00
|
|
|
unsigned int length;
|
1999-01-06 05:37:54 +00:00
|
|
|
void *src;
|
1998-10-29 02:01:29 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Compact the used region by moving the remaining region so it occurs
|
|
|
|
* at the start of the buffer. The used region is shrunk by the size
|
|
|
|
* of the consumed region, and the consumed region is then made empty.
|
|
|
|
*/
|
|
|
|
|
|
|
|
REQUIRE(VALID_BUFFER(b));
|
|
|
|
|
1999-01-06 05:37:54 +00:00
|
|
|
src = (unsigned char *)b->base + b->current;
|
|
|
|
length = b->used - b->current;
|
|
|
|
(void)memmove(b->base, src, (size_t)length);
|
1998-10-29 02:01:29 +00:00
|
|
|
|
1999-01-06 05:37:54 +00:00
|
|
|
b->current = 0;
|
1998-10-29 02:01:29 +00:00
|
|
|
b->used = length;
|
|
|
|
}
|