]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - contrib/bind9/lib/isccc/sexpr.c
MFV r306384:
[FreeBSD/stable/9.git] / contrib / bind9 / lib / isccc / sexpr.c
1 /*
2  * Portions Copyright (C) 2004, 2005, 2007, 2014, 2015  Internet Systems Consortium, Inc. ("ISC")
3  * Portions Copyright (C) 2001  Internet Software Consortium.
4  *
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.
8  *
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.
16  *
17  * Portions Copyright (C) 2001  Nominum, Inc.
18  *
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.
22  *
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.
30  */
31
32 /*! \file */
33
34 #include <config.h>
35
36 #include <ctype.h>
37 #include <stdlib.h>
38 #include <string.h>
39
40 #include <isc/assertions.h>
41 #include <isc/print.h>
42 #include <isccc/sexpr.h>
43 #include <isccc/util.h>
44
45 static isccc_sexpr_t sexpr_t = { ISCCC_SEXPRTYPE_T, { NULL } };
46
47 #define CAR(s)                  (s)->value.as_dottedpair.car
48 #define CDR(s)                  (s)->value.as_dottedpair.cdr
49
50 isccc_sexpr_t *
51 isccc_sexpr_cons(isccc_sexpr_t *car, isccc_sexpr_t *cdr) {
52         isccc_sexpr_t *sexpr;
53
54         sexpr = malloc(sizeof(*sexpr));
55         if (sexpr == NULL)
56                 return (NULL);
57         sexpr->type = ISCCC_SEXPRTYPE_DOTTEDPAIR;
58         CAR(sexpr) = car;
59         CDR(sexpr) = cdr;
60
61         return (sexpr);
62 }
63
64 isccc_sexpr_t *
65 isccc_sexpr_tconst(void) {
66         return (&sexpr_t);
67 }
68
69 isccc_sexpr_t *
70 isccc_sexpr_fromstring(const char *str) {
71         isccc_sexpr_t *sexpr;
72
73         sexpr = malloc(sizeof(*sexpr));
74         if (sexpr == NULL)
75                 return (NULL);
76         sexpr->type = ISCCC_SEXPRTYPE_STRING;
77         sexpr->value.as_string = strdup(str);
78         if (sexpr->value.as_string == NULL) {
79                 free(sexpr);
80                 return (NULL);
81         }
82
83         return (sexpr);
84 }
85
86 isccc_sexpr_t *
87 isccc_sexpr_frombinary(const isccc_region_t *region) {
88         isccc_sexpr_t *sexpr;
89         unsigned int region_size;
90
91         sexpr = malloc(sizeof(*sexpr));
92         if (sexpr == NULL)
93                 return (NULL);
94         sexpr->type = ISCCC_SEXPRTYPE_BINARY;
95         region_size = REGION_SIZE(*region);
96         /*
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.
102          */
103         sexpr->value.as_region.rstart = malloc(region_size + 1);
104         if (sexpr->value.as_region.rstart == NULL) {
105                 free(sexpr);
106                 return (NULL);
107         }
108         sexpr->value.as_region.rend = sexpr->value.as_region.rstart +
109                 region_size;
110         memmove(sexpr->value.as_region.rstart, region->rstart, region_size);
111         /*
112          * NUL terminate.
113          */
114         sexpr->value.as_region.rstart[region_size] = '\0';
115
116         return (sexpr);
117 }
118
119 void
120 isccc_sexpr_free(isccc_sexpr_t **sexprp) {
121         isccc_sexpr_t *sexpr;
122         isccc_sexpr_t *item;
123
124         sexpr = *sexprp;
125         if (sexpr == NULL)
126                 return;
127         switch (sexpr->type) {
128         case ISCCC_SEXPRTYPE_STRING:
129                 free(sexpr->value.as_string);
130                 break;
131         case ISCCC_SEXPRTYPE_DOTTEDPAIR:
132                 item = CAR(sexpr);
133                 if (item != NULL)
134                         isccc_sexpr_free(&item);
135                 item = CDR(sexpr);
136                 if (item != NULL)
137                         isccc_sexpr_free(&item);
138                 break;
139         case ISCCC_SEXPRTYPE_BINARY:
140                 free(sexpr->value.as_region.rstart);
141                 break;
142         }
143         free(sexpr);
144
145         *sexprp = NULL;
146 }
147
148 static isc_boolean_t
149 printable(isccc_region_t *r) {
150         unsigned char *curr;
151
152         curr = r->rstart;
153         while (curr != r->rend) {
154                 if (!isprint(*curr))
155                         return (ISC_FALSE);
156                 curr++;
157         }
158
159         return (ISC_TRUE);
160 }
161
162 void
163 isccc_sexpr_print(isccc_sexpr_t *sexpr, FILE *stream) {
164         isccc_sexpr_t *cdr;
165         unsigned int size, i;
166         unsigned char *curr;
167
168         if (sexpr == NULL) {
169                 fprintf(stream, "nil");
170                 return;
171         }
172
173         switch (sexpr->type) {
174         case ISCCC_SEXPRTYPE_T:
175                 fprintf(stream, "t");
176                 break;
177         case ISCCC_SEXPRTYPE_STRING:
178                 fprintf(stream, "\"%s\"", sexpr->value.as_string);
179                 break;
180         case ISCCC_SEXPRTYPE_DOTTEDPAIR:
181                 fprintf(stream, "(");
182                 do {
183                         isccc_sexpr_print(CAR(sexpr), stream);
184                         cdr = CDR(sexpr);
185                         if (cdr != NULL) {
186                                 fprintf(stream, " ");
187                                 if (cdr->type != ISCCC_SEXPRTYPE_DOTTEDPAIR) {
188                                         fprintf(stream, ". ");
189                                         isccc_sexpr_print(cdr, stream);
190                                         cdr = NULL;
191                                 }
192                         }
193                         sexpr = cdr;
194                 } while (sexpr != NULL);
195                 fprintf(stream, ")");
196                 break;
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);
202                 } else {
203                         fprintf(stream, "0x");
204                         for (i = 0; i < size; i++)
205                                 fprintf(stream, "%02x", *curr++);
206                 }
207                 break;
208         default:
209                 INSIST(0);
210         }
211 }
212
213 isccc_sexpr_t *
214 isccc_sexpr_car(isccc_sexpr_t *list) {
215         REQUIRE(list->type == ISCCC_SEXPRTYPE_DOTTEDPAIR);
216
217         return (CAR(list));
218 }
219
220 isccc_sexpr_t *
221 isccc_sexpr_cdr(isccc_sexpr_t *list) {
222         REQUIRE(list->type == ISCCC_SEXPRTYPE_DOTTEDPAIR);
223
224         return (CDR(list));
225 }
226
227 void
228 isccc_sexpr_setcar(isccc_sexpr_t *pair, isccc_sexpr_t *car) {
229         REQUIRE(pair->type == ISCCC_SEXPRTYPE_DOTTEDPAIR);
230
231         CAR(pair) = car;
232 }
233
234 void
235 isccc_sexpr_setcdr(isccc_sexpr_t *pair, isccc_sexpr_t *cdr) {
236         REQUIRE(pair->type == ISCCC_SEXPRTYPE_DOTTEDPAIR);
237
238         CDR(pair) = cdr;
239 }
240
241 isccc_sexpr_t *
242 isccc_sexpr_addtolist(isccc_sexpr_t **l1p, isccc_sexpr_t *l2) {
243         isccc_sexpr_t *last, *elt, *l1;
244
245         REQUIRE(l1p != NULL);
246         l1 = *l1p;
247         REQUIRE(l1 == NULL || l1->type == ISCCC_SEXPRTYPE_DOTTEDPAIR);
248
249         elt = isccc_sexpr_cons(l2, NULL);
250         if (elt == NULL)
251                 return (NULL);
252         if (l1 == NULL) {
253                 *l1p = elt;
254                 return (elt);
255         }
256         for (last = l1; CDR(last) != NULL; last = CDR(last))
257                 /* Nothing */;
258         CDR(last) = elt;
259
260         return (elt);
261 }
262
263 isc_boolean_t
264 isccc_sexpr_listp(isccc_sexpr_t *sexpr) {
265         if (sexpr == NULL || sexpr->type == ISCCC_SEXPRTYPE_DOTTEDPAIR)
266                 return (ISC_TRUE);
267         return (ISC_FALSE);
268 }
269
270 isc_boolean_t
271 isccc_sexpr_emptyp(isccc_sexpr_t *sexpr) {
272         if (sexpr == NULL)
273                 return (ISC_TRUE);
274         return (ISC_FALSE);
275 }
276
277 isc_boolean_t
278 isccc_sexpr_stringp(isccc_sexpr_t *sexpr) {
279         if (sexpr != NULL && sexpr->type == ISCCC_SEXPRTYPE_STRING)
280                 return (ISC_TRUE);
281         return (ISC_FALSE);
282 }
283
284 isc_boolean_t
285 isccc_sexpr_binaryp(isccc_sexpr_t *sexpr) {
286         if (sexpr != NULL && sexpr->type == ISCCC_SEXPRTYPE_BINARY)
287                 return (ISC_TRUE);
288         return (ISC_FALSE);
289 }
290
291 char *
292 isccc_sexpr_tostring(isccc_sexpr_t *sexpr) {
293         REQUIRE(sexpr != NULL &&
294                 (sexpr->type == ISCCC_SEXPRTYPE_STRING ||
295                  sexpr->type == ISCCC_SEXPRTYPE_BINARY));
296
297         if (sexpr->type == ISCCC_SEXPRTYPE_BINARY)
298                 return ((char *)sexpr->value.as_region.rstart);
299         return (sexpr->value.as_string);
300 }
301
302 isccc_region_t *
303 isccc_sexpr_tobinary(isccc_sexpr_t *sexpr) {
304         REQUIRE(sexpr != NULL && sexpr->type == ISCCC_SEXPRTYPE_BINARY);
305         return (&sexpr->value.as_region);
306 }