2 * Portions Copyright (C) 2004, 2005, 2007, 2014, 2015 Internet Systems Consortium, Inc. ("ISC")
3 * Portions Copyright (C) 2001 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NOMINUM DISCLAIMS ALL
10 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
11 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY
12 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 * Portions Copyright (C) 2001 Nominum, Inc.
19 * Permission to use, copy, modify, and/or distribute this software for any
20 * purpose with or without fee is hereby granted, provided that the above
21 * copyright notice and this permission notice appear in all copies.
23 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NOMINUM DISCLAIMS ALL
24 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY
26 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
27 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
28 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
29 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
40 #include <isc/assertions.h>
41 #include <isc/print.h>
42 #include <isccc/sexpr.h>
43 #include <isccc/util.h>
45 static isccc_sexpr_t sexpr_t = { ISCCC_SEXPRTYPE_T, { NULL } };
47 #define CAR(s) (s)->value.as_dottedpair.car
48 #define CDR(s) (s)->value.as_dottedpair.cdr
51 isccc_sexpr_cons(isccc_sexpr_t *car, isccc_sexpr_t *cdr) {
54 sexpr = malloc(sizeof(*sexpr));
57 sexpr->type = ISCCC_SEXPRTYPE_DOTTEDPAIR;
65 isccc_sexpr_tconst(void) {
70 isccc_sexpr_fromstring(const char *str) {
73 sexpr = malloc(sizeof(*sexpr));
76 sexpr->type = ISCCC_SEXPRTYPE_STRING;
77 sexpr->value.as_string = strdup(str);
78 if (sexpr->value.as_string == NULL) {
87 isccc_sexpr_frombinary(const isccc_region_t *region) {
89 unsigned int region_size;
91 sexpr = malloc(sizeof(*sexpr));
94 sexpr->type = ISCCC_SEXPRTYPE_BINARY;
95 region_size = REGION_SIZE(*region);
97 * We add an extra byte when we malloc so we can NUL terminate
98 * the binary data. This allows the caller to use it as a C
99 * string. It's up to the caller to ensure this is safe. We don't
100 * add 1 to the length of the binary region, because the NUL is
101 * not part of the binary data.
103 sexpr->value.as_region.rstart = malloc(region_size + 1);
104 if (sexpr->value.as_region.rstart == NULL) {
108 sexpr->value.as_region.rend = sexpr->value.as_region.rstart +
110 memmove(sexpr->value.as_region.rstart, region->rstart, region_size);
114 sexpr->value.as_region.rstart[region_size] = '\0';
120 isccc_sexpr_free(isccc_sexpr_t **sexprp) {
121 isccc_sexpr_t *sexpr;
127 switch (sexpr->type) {
128 case ISCCC_SEXPRTYPE_STRING:
129 free(sexpr->value.as_string);
131 case ISCCC_SEXPRTYPE_DOTTEDPAIR:
134 isccc_sexpr_free(&item);
137 isccc_sexpr_free(&item);
139 case ISCCC_SEXPRTYPE_BINARY:
140 free(sexpr->value.as_region.rstart);
149 printable(isccc_region_t *r) {
153 while (curr != r->rend) {
163 isccc_sexpr_print(isccc_sexpr_t *sexpr, FILE *stream) {
165 unsigned int size, i;
169 fprintf(stream, "nil");
173 switch (sexpr->type) {
174 case ISCCC_SEXPRTYPE_T:
175 fprintf(stream, "t");
177 case ISCCC_SEXPRTYPE_STRING:
178 fprintf(stream, "\"%s\"", sexpr->value.as_string);
180 case ISCCC_SEXPRTYPE_DOTTEDPAIR:
181 fprintf(stream, "(");
183 isccc_sexpr_print(CAR(sexpr), stream);
186 fprintf(stream, " ");
187 if (cdr->type != ISCCC_SEXPRTYPE_DOTTEDPAIR) {
188 fprintf(stream, ". ");
189 isccc_sexpr_print(cdr, stream);
194 } while (sexpr != NULL);
195 fprintf(stream, ")");
197 case ISCCC_SEXPRTYPE_BINARY:
198 size = REGION_SIZE(sexpr->value.as_region);
199 curr = sexpr->value.as_region.rstart;
200 if (printable(&sexpr->value.as_region)) {
201 fprintf(stream, "'%.*s'", (int)size, curr);
203 fprintf(stream, "0x");
204 for (i = 0; i < size; i++)
205 fprintf(stream, "%02x", *curr++);
214 isccc_sexpr_car(isccc_sexpr_t *list) {
215 REQUIRE(list->type == ISCCC_SEXPRTYPE_DOTTEDPAIR);
221 isccc_sexpr_cdr(isccc_sexpr_t *list) {
222 REQUIRE(list->type == ISCCC_SEXPRTYPE_DOTTEDPAIR);
228 isccc_sexpr_setcar(isccc_sexpr_t *pair, isccc_sexpr_t *car) {
229 REQUIRE(pair->type == ISCCC_SEXPRTYPE_DOTTEDPAIR);
235 isccc_sexpr_setcdr(isccc_sexpr_t *pair, isccc_sexpr_t *cdr) {
236 REQUIRE(pair->type == ISCCC_SEXPRTYPE_DOTTEDPAIR);
242 isccc_sexpr_addtolist(isccc_sexpr_t **l1p, isccc_sexpr_t *l2) {
243 isccc_sexpr_t *last, *elt, *l1;
245 REQUIRE(l1p != NULL);
247 REQUIRE(l1 == NULL || l1->type == ISCCC_SEXPRTYPE_DOTTEDPAIR);
249 elt = isccc_sexpr_cons(l2, NULL);
256 for (last = l1; CDR(last) != NULL; last = CDR(last))
264 isccc_sexpr_listp(isccc_sexpr_t *sexpr) {
265 if (sexpr == NULL || sexpr->type == ISCCC_SEXPRTYPE_DOTTEDPAIR)
271 isccc_sexpr_emptyp(isccc_sexpr_t *sexpr) {
278 isccc_sexpr_stringp(isccc_sexpr_t *sexpr) {
279 if (sexpr != NULL && sexpr->type == ISCCC_SEXPRTYPE_STRING)
285 isccc_sexpr_binaryp(isccc_sexpr_t *sexpr) {
286 if (sexpr != NULL && sexpr->type == ISCCC_SEXPRTYPE_BINARY)
292 isccc_sexpr_tostring(isccc_sexpr_t *sexpr) {
293 REQUIRE(sexpr != NULL &&
294 (sexpr->type == ISCCC_SEXPRTYPE_STRING ||
295 sexpr->type == ISCCC_SEXPRTYPE_BINARY));
297 if (sexpr->type == ISCCC_SEXPRTYPE_BINARY)
298 return ((char *)sexpr->value.as_region.rstart);
299 return (sexpr->value.as_string);
303 isccc_sexpr_tobinary(isccc_sexpr_t *sexpr) {
304 REQUIRE(sexpr != NULL && sexpr->type == ISCCC_SEXPRTYPE_BINARY);
305 return (&sexpr->value.as_region);