From bab662ff0cc7b834972deb7287ea74905e5116ef Mon Sep 17 00:00:00 2001 From: Michael Graff Date: Mon, 17 Jan 2000 23:12:02 +0000 Subject: [PATCH] all utility function file --- lib/lwres/lwresutil.c | 88 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 lib/lwres/lwresutil.c diff --git a/lib/lwres/lwresutil.c b/lib/lwres/lwresutil.c new file mode 100644 index 0000000000..97fb0e97f5 --- /dev/null +++ b/lib/lwres/lwresutil.c @@ -0,0 +1,88 @@ +/* + * Copyright (C) 2000 Internet Software Consortium. + * + * 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. + */ + +#include + +#include +#include +#include +#include + +#include +#include + +#include "assert_p.h" + +/* + * Requires: + * + * The "current" pointer in "b" point to an encoded string. + * + * Ensures: + * + * The address of the first byte of the string is returned via "c", + * and the length is returned via "len". If NULL, they are not + * set. + * + * On return, the current pointer of "b" will point to the character + * following the string length, the stirng, and the trailing NUL. + * + */ +int +lwres_string_parse(lwres_buffer_t *b, char **c, isc_uint16_t *len) +{ + isc_uint16_t datalen; + char *string; + + REQUIRE(b != NULL); + + if (!SPACE_REMAINING(b, sizeof(isc_uint16_t))) + return (-1); + datalen = lwres_buffer_getuint16(b); + datalen++; + if (!SPACE_REMAINING(b, datalen)) + return (-1); + + string = b->base + b->current; + + lwres_buffer_forward(b, datalen); + + if (len != NULL) + *len = datalen - 1; + if (c != NULL) + *c = string; + + return (0); +} + +int +lwres_addr_parse(lwres_buffer_t *b, lwres_addr_t *addr) +{ + REQUIRE(addr != NULL); + + if (!SPACE_REMAINING(b, sizeof(isc_uint32_t) + sizeof(isc_uint16_t))) + return (-1); + addr->family = lwres_buffer_getuint32(b); + addr->length = lwres_buffer_getuint16(b); + if (!SPACE_REMAINING(b, addr->length)) + return (-1); + addr->address = b->base + b->current; + lwres_buffer_forward(b, addr->length); + + return (0); +} +