1 /* $NetBSD: sexpr.c,v 1.5 2014/12/10 04:38:01 christos Exp $ */
4 * Portions Copyright (C) 2004, 2005, 2007, 2014 Internet Systems Consortium, Inc. ("ISC")
5 * Portions Copyright (C) 2001 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NOMINUM DISCLAIMS ALL
12 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
13 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY
14 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 * Portions Copyright (C) 2001 Nominum, Inc.
21 * Permission to use, copy, modify, and/or distribute this software for any
22 * purpose with or without fee is hereby granted, provided that the above
23 * copyright notice and this permission notice appear in all copies.
25 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NOMINUM DISCLAIMS ALL
26 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY
28 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
29 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
30 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
31 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
34 /* Id: sexpr.c,v 1.9 2007/08/28 07:20:43 tbox Exp */
44 #include <isc/assertions.h>
45 #include <isccc/sexpr.h>
46 #include <isccc/util.h>
48 static isccc_sexpr_t sexpr_t
= { ISCCC_SEXPRTYPE_T
, { NULL
} };
50 #define CAR(s) (s)->value.as_dottedpair.car
51 #define CDR(s) (s)->value.as_dottedpair.cdr
54 isccc_sexpr_cons(isccc_sexpr_t
*car
, isccc_sexpr_t
*cdr
)
58 sexpr
= malloc(sizeof(*sexpr
));
61 sexpr
->type
= ISCCC_SEXPRTYPE_DOTTEDPAIR
;
69 isccc_sexpr_tconst(void)
75 isccc_sexpr_fromstring(const char *str
)
79 sexpr
= malloc(sizeof(*sexpr
));
82 sexpr
->type
= ISCCC_SEXPRTYPE_STRING
;
83 sexpr
->value
.as_string
= strdup(str
);
84 if (sexpr
->value
.as_string
== NULL
) {
93 isccc_sexpr_frombinary(const isccc_region_t
*region
)
96 unsigned int region_size
;
98 sexpr
= malloc(sizeof(*sexpr
));
101 sexpr
->type
= ISCCC_SEXPRTYPE_BINARY
;
102 region_size
= REGION_SIZE(*region
);
104 * We add an extra byte when we malloc so we can NUL terminate
105 * the binary data. This allows the caller to use it as a C
106 * string. It's up to the caller to ensure this is safe. We don't
107 * add 1 to the length of the binary region, because the NUL is
108 * not part of the binary data.
110 sexpr
->value
.as_region
.rstart
= malloc(region_size
+ 1);
111 if (sexpr
->value
.as_region
.rstart
== NULL
) {
115 sexpr
->value
.as_region
.rend
= sexpr
->value
.as_region
.rstart
+
117 memmove(sexpr
->value
.as_region
.rstart
, region
->rstart
, region_size
);
121 sexpr
->value
.as_region
.rstart
[region_size
] = '\0';
127 isccc_sexpr_free(isccc_sexpr_t
**sexprp
)
129 isccc_sexpr_t
*sexpr
;
135 switch (sexpr
->type
) {
136 case ISCCC_SEXPRTYPE_STRING
:
137 free(sexpr
->value
.as_string
);
139 case ISCCC_SEXPRTYPE_DOTTEDPAIR
:
142 isccc_sexpr_free(&item
);
145 isccc_sexpr_free(&item
);
147 case ISCCC_SEXPRTYPE_BINARY
:
148 free(sexpr
->value
.as_region
.rstart
);
157 printable(isccc_region_t
*r
)
162 while (curr
!= r
->rend
) {
172 isccc_sexpr_print(isccc_sexpr_t
*sexpr
, FILE *stream
)
175 unsigned int size
, i
;
179 fprintf(stream
, "nil");
183 switch (sexpr
->type
) {
184 case ISCCC_SEXPRTYPE_T
:
185 fprintf(stream
, "t");
187 case ISCCC_SEXPRTYPE_STRING
:
188 fprintf(stream
, "\"%s\"", sexpr
->value
.as_string
);
190 case ISCCC_SEXPRTYPE_DOTTEDPAIR
:
191 fprintf(stream
, "(");
193 isccc_sexpr_print(CAR(sexpr
), stream
);
196 fprintf(stream
, " ");
197 if (cdr
->type
!= ISCCC_SEXPRTYPE_DOTTEDPAIR
) {
198 fprintf(stream
, ". ");
199 isccc_sexpr_print(cdr
, stream
);
204 } while (sexpr
!= NULL
);
205 fprintf(stream
, ")");
207 case ISCCC_SEXPRTYPE_BINARY
:
208 size
= REGION_SIZE(sexpr
->value
.as_region
);
209 curr
= sexpr
->value
.as_region
.rstart
;
210 if (printable(&sexpr
->value
.as_region
)) {
211 fprintf(stream
, "'%.*s'", (int)size
, curr
);
213 fprintf(stream
, "0x");
214 for (i
= 0; i
< size
; i
++)
215 fprintf(stream
, "%02x", *curr
++);
224 isccc_sexpr_car(isccc_sexpr_t
*list
)
226 REQUIRE(list
->type
== ISCCC_SEXPRTYPE_DOTTEDPAIR
);
232 isccc_sexpr_cdr(isccc_sexpr_t
*list
)
234 REQUIRE(list
->type
== ISCCC_SEXPRTYPE_DOTTEDPAIR
);
240 isccc_sexpr_setcar(isccc_sexpr_t
*pair
, isccc_sexpr_t
*car
)
242 REQUIRE(pair
->type
== ISCCC_SEXPRTYPE_DOTTEDPAIR
);
248 isccc_sexpr_setcdr(isccc_sexpr_t
*pair
, isccc_sexpr_t
*cdr
)
250 REQUIRE(pair
->type
== ISCCC_SEXPRTYPE_DOTTEDPAIR
);
256 isccc_sexpr_addtolist(isccc_sexpr_t
**l1p
, isccc_sexpr_t
*l2
)
258 isccc_sexpr_t
*last
, *elt
, *l1
;
260 REQUIRE(l1p
!= NULL
);
262 REQUIRE(l1
== NULL
|| l1
->type
== ISCCC_SEXPRTYPE_DOTTEDPAIR
);
264 elt
= isccc_sexpr_cons(l2
, NULL
);
271 for (last
= l1
; CDR(last
) != NULL
; last
= CDR(last
))
279 isccc_sexpr_listp(isccc_sexpr_t
*sexpr
)
281 if (sexpr
== NULL
|| sexpr
->type
== ISCCC_SEXPRTYPE_DOTTEDPAIR
)
287 isccc_sexpr_emptyp(isccc_sexpr_t
*sexpr
)
295 isccc_sexpr_stringp(isccc_sexpr_t
*sexpr
)
297 if (sexpr
!= NULL
&& sexpr
->type
== ISCCC_SEXPRTYPE_STRING
)
303 isccc_sexpr_binaryp(isccc_sexpr_t
*sexpr
)
305 if (sexpr
!= NULL
&& sexpr
->type
== ISCCC_SEXPRTYPE_BINARY
)
311 isccc_sexpr_tostring(isccc_sexpr_t
*sexpr
)
313 REQUIRE(sexpr
!= NULL
&&
314 (sexpr
->type
== ISCCC_SEXPRTYPE_STRING
||
315 sexpr
->type
== ISCCC_SEXPRTYPE_BINARY
));
317 if (sexpr
->type
== ISCCC_SEXPRTYPE_BINARY
)
318 return ((char *)sexpr
->value
.as_region
.rstart
);
319 return (sexpr
->value
.as_string
);
323 isccc_sexpr_tobinary(isccc_sexpr_t
*sexpr
)
325 REQUIRE(sexpr
!= NULL
&& sexpr
->type
== ISCCC_SEXPRTYPE_BINARY
);
326 return (&sexpr
->value
.as_region
);