sexpr.c revision 431a83fb29482c5170b3e4026e59bb14849a6707
431a83fb29482c5170b3e4026e59bb14849a6707Tinderbox User * Portions Copyright (C) 2004, 2005, 2007, 2014 Internet Systems Consortium, Inc. ("ISC")
cb3e854e181373807f7f011e5050c1a8013b4841Brian Wellington * Portions Copyright (C) 2001 Internet Software Consortium.
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * Permission to use, copy, modify, and/or distribute this software for any
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * purpose with or without fee is hereby granted, provided that the above
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * copyright notice and this permission notice appear in all copies.
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NOMINUM DISCLAIMS ALL
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
cb3e854e181373807f7f011e5050c1a8013b4841Brian Wellington * Portions Copyright (C) 2001 Nominum, Inc.
271c4c7ffafeb0bda21278af6cac4535c0193f18Automatic Updater * Permission to use, copy, modify, and/or distribute this software for any
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington * purpose with or without fee is hereby granted, provided that the above
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington * copyright notice and this permission notice appear in all copies.
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NOMINUM DISCLAIMS ALL
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY
cb3e854e181373807f7f011e5050c1a8013b4841Brian Wellington * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
cb3e854e181373807f7f011e5050c1a8013b4841Brian Wellington * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
271c4c7ffafeb0bda21278af6cac4535c0193f18Automatic Updater/* $Id: sexpr.c,v 1.9 2007/08/28 07:20:43 tbox Exp $ */
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonstatic isccc_sexpr_t sexpr_t = { ISCCC_SEXPRTYPE_T, { NULL } };
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisccc_sexpr_cons(isccc_sexpr_t *car, isccc_sexpr_t *cdr)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisccc_sexpr_frombinary(const isccc_region_t *region)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington * We add an extra byte when we malloc so we can NUL terminate
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington * the binary data. This allows the caller to use it as a C
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington * string. It's up to the caller to ensure this is safe. We don't
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington * add 1 to the length of the binary region, because the NUL is
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington * not part of the binary data.
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington sexpr->value.as_region.rstart = malloc(region_size + 1);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (sexpr->value.as_region.rstart == NULL) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington sexpr->value.as_region.rend = sexpr->value.as_region.rstart +
e851ea826066ac5a5b01c2c23218faa0273a12e8Evan Hunt memmove(sexpr->value.as_region.rstart, region->rstart, region_size);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington * NUL terminate.
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington sexpr->value.as_region.rstart[region_size] = '\0';
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington unsigned char *curr;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisccc_sexpr_print(isccc_sexpr_t *sexpr, FILE *stream)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington unsigned int size, i;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington unsigned char *curr;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington fprintf(stream, "\"%s\"", sexpr->value.as_string);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (cdr->type != ISCCC_SEXPRTYPE_DOTTEDPAIR) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington size = REGION_SIZE(sexpr->value.as_region);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington fprintf(stream, "'%.*s'", (int)size, curr);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington for (i = 0; i < size; i++)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington REQUIRE(list->type == ISCCC_SEXPRTYPE_DOTTEDPAIR);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington REQUIRE(list->type == ISCCC_SEXPRTYPE_DOTTEDPAIR);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisccc_sexpr_setcar(isccc_sexpr_t *pair, isccc_sexpr_t *car)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington REQUIRE(pair->type == ISCCC_SEXPRTYPE_DOTTEDPAIR);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisccc_sexpr_setcdr(isccc_sexpr_t *pair, isccc_sexpr_t *cdr)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington REQUIRE(pair->type == ISCCC_SEXPRTYPE_DOTTEDPAIR);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisccc_sexpr_addtolist(isccc_sexpr_t **l1p, isccc_sexpr_t *l2)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington REQUIRE(l1 == NULL || l1->type == ISCCC_SEXPRTYPE_DOTTEDPAIR);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington for (last = l1; CDR(last) != NULL; last = CDR(last))
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington /* Nothing */;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (sexpr == NULL || sexpr->type == ISCCC_SEXPRTYPE_DOTTEDPAIR)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (sexpr != NULL && sexpr->type == ISCCC_SEXPRTYPE_STRING)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (sexpr != NULL && sexpr->type == ISCCC_SEXPRTYPE_BINARY)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return ((char *)sexpr->value.as_region.rstart);