prop_object.c revision 1.36 1 1.36 thorpej /* $NetBSD: prop_object.c,v 1.36 2025/04/23 02:58:52 thorpej Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*-
4 1.36 thorpej * Copyright (c) 2006, 2007, 2025 The NetBSD Foundation, Inc.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.1 thorpej * by Jason R. Thorpe.
9 1.1 thorpej *
10 1.1 thorpej * Redistribution and use in source and binary forms, with or without
11 1.1 thorpej * modification, are permitted provided that the following conditions
12 1.1 thorpej * are met:
13 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
14 1.1 thorpej * notice, this list of conditions and the following disclaimer.
15 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
17 1.1 thorpej * documentation and/or other materials provided with the distribution.
18 1.1 thorpej *
19 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
30 1.1 thorpej */
31 1.1 thorpej
32 1.28 pooka #include "prop_object_impl.h"
33 1.1 thorpej #include <prop/prop_object.h>
34 1.28 pooka
35 1.28 pooka #ifdef _PROP_NEED_REFCNT_MTX
36 1.28 pooka static pthread_mutex_t _prop_refcnt_mtx = PTHREAD_MUTEX_INITIALIZER;
37 1.28 pooka #endif /* _PROP_NEED_REFCNT_MTX */
38 1.1 thorpej
39 1.4 thorpej #if !defined(_KERNEL) && !defined(_STANDALONE)
40 1.4 thorpej #include <sys/mman.h>
41 1.4 thorpej #include <sys/stat.h>
42 1.4 thorpej #include <errno.h>
43 1.4 thorpej #include <fcntl.h>
44 1.4 thorpej #include <limits.h>
45 1.4 thorpej #include <unistd.h>
46 1.4 thorpej #endif
47 1.4 thorpej
48 1.1 thorpej #ifdef _STANDALONE
49 1.1 thorpej void *
50 1.1 thorpej _prop_standalone_calloc(size_t size)
51 1.1 thorpej {
52 1.1 thorpej void *rv;
53 1.1 thorpej
54 1.1 thorpej rv = alloc(size);
55 1.1 thorpej if (rv != NULL)
56 1.1 thorpej memset(rv, 0, size);
57 1.1 thorpej
58 1.1 thorpej return (rv);
59 1.1 thorpej }
60 1.2 thorpej
61 1.2 thorpej void *
62 1.2 thorpej _prop_standalone_realloc(void *v, size_t size)
63 1.2 thorpej {
64 1.2 thorpej void *rv;
65 1.2 thorpej
66 1.2 thorpej rv = alloc(size);
67 1.2 thorpej if (rv != NULL) {
68 1.2 thorpej memcpy(rv, v, size); /* XXX */
69 1.2 thorpej dealloc(v, 0); /* XXX */
70 1.2 thorpej }
71 1.32 riastrad
72 1.2 thorpej return (rv);
73 1.2 thorpej }
74 1.1 thorpej #endif /* _STANDALONE */
75 1.1 thorpej
76 1.1 thorpej /*
77 1.1 thorpej * _prop_object_init --
78 1.1 thorpej * Initialize an object. Called when sub-classes create
79 1.1 thorpej * an instance.
80 1.1 thorpej */
81 1.1 thorpej void
82 1.2 thorpej _prop_object_init(struct _prop_object *po, const struct _prop_object_type *pot)
83 1.1 thorpej {
84 1.1 thorpej
85 1.2 thorpej po->po_type = pot;
86 1.1 thorpej po->po_refcnt = 1;
87 1.1 thorpej }
88 1.1 thorpej
89 1.1 thorpej /*
90 1.1 thorpej * _prop_object_fini --
91 1.1 thorpej * Finalize an object. Called when sub-classes destroy
92 1.1 thorpej * an instance.
93 1.1 thorpej */
94 1.4 thorpej /*ARGSUSED*/
95 1.1 thorpej void
96 1.9 thorpej _prop_object_fini(struct _prop_object *po _PROP_ARG_UNUSED)
97 1.1 thorpej {
98 1.1 thorpej /* Nothing to do, currently. */
99 1.1 thorpej }
100 1.1 thorpej
101 1.1 thorpej /*
102 1.36 thorpej * _prop_object_externalize_start_line --
103 1.36 thorpej * Append the start-of-line character sequence.
104 1.1 thorpej */
105 1.14 thorpej bool
106 1.36 thorpej _prop_object_externalize_start_line(
107 1.36 thorpej struct _prop_object_externalize_context *ctx)
108 1.1 thorpej {
109 1.1 thorpej unsigned int i;
110 1.1 thorpej
111 1.1 thorpej for (i = 0; i < ctx->poec_depth; i++) {
112 1.36 thorpej if (_prop_object_externalize_append_char(ctx, '\t') == false) {
113 1.36 thorpej return false;
114 1.36 thorpej }
115 1.36 thorpej }
116 1.36 thorpej return true;
117 1.36 thorpej }
118 1.36 thorpej
119 1.36 thorpej /*
120 1.36 thorpej * _prop_object_externalize_end_line --
121 1.36 thorpej * Append the end-of-line character sequence.
122 1.36 thorpej */
123 1.36 thorpej bool
124 1.36 thorpej _prop_object_externalize_end_line(
125 1.36 thorpej struct _prop_object_externalize_context *ctx, const char *trailer)
126 1.36 thorpej {
127 1.36 thorpej if (trailer != NULL &&
128 1.36 thorpej _prop_object_externalize_append_cstring(ctx, trailer) == false) {
129 1.36 thorpej return false;
130 1.36 thorpej }
131 1.36 thorpej return _prop_object_externalize_append_char(ctx, '\n');
132 1.36 thorpej }
133 1.36 thorpej
134 1.36 thorpej /*
135 1.36 thorpej * _prop_object_externalize_start_tag --
136 1.36 thorpej * Append an item's start tag to the externalize buffer.
137 1.36 thorpej */
138 1.36 thorpej bool
139 1.36 thorpej _prop_object_externalize_start_tag(
140 1.36 thorpej struct _prop_object_externalize_context *ctx,
141 1.36 thorpej const struct _prop_object_type_tags *tags,
142 1.36 thorpej const char *tagattrs)
143 1.36 thorpej {
144 1.36 thorpej bool rv;
145 1.36 thorpej
146 1.36 thorpej _PROP_ASSERT(ctx->poec_format == PROP_FORMAT_XML ||
147 1.36 thorpej ctx->poec_format == PROP_FORMAT_JSON);
148 1.36 thorpej
149 1.36 thorpej switch (ctx->poec_format) {
150 1.36 thorpej case PROP_FORMAT_JSON:
151 1.36 thorpej rv = tags->json_open_tag == NULL ||
152 1.36 thorpej _prop_object_externalize_append_cstring(ctx,
153 1.36 thorpej tags->json_open_tag);
154 1.36 thorpej break;
155 1.36 thorpej
156 1.36 thorpej default: /* XML */
157 1.36 thorpej rv = _prop_object_externalize_append_char(ctx, '<') &&
158 1.36 thorpej _prop_object_externalize_append_cstring(ctx,
159 1.36 thorpej tags->xml_tag) &&
160 1.36 thorpej (tagattrs == NULL ||
161 1.36 thorpej (_prop_object_externalize_append_char(ctx, ' ') &&
162 1.36 thorpej _prop_object_externalize_append_cstring(ctx,
163 1.36 thorpej tagattrs))) &&
164 1.36 thorpej _prop_object_externalize_append_char(ctx, '>');
165 1.36 thorpej break;
166 1.1 thorpej }
167 1.32 riastrad
168 1.36 thorpej return rv;
169 1.1 thorpej }
170 1.1 thorpej
171 1.1 thorpej /*
172 1.1 thorpej * _prop_object_externalize_end_tag --
173 1.36 thorpej * Append an item's end tag to the externalize buffer.
174 1.1 thorpej */
175 1.14 thorpej bool
176 1.1 thorpej _prop_object_externalize_end_tag(
177 1.36 thorpej struct _prop_object_externalize_context *ctx,
178 1.36 thorpej const struct _prop_object_type_tags *tags)
179 1.1 thorpej {
180 1.36 thorpej bool rv;
181 1.36 thorpej
182 1.36 thorpej _PROP_ASSERT(ctx->poec_format == PROP_FORMAT_XML ||
183 1.36 thorpej ctx->poec_format == PROP_FORMAT_JSON);
184 1.1 thorpej
185 1.36 thorpej switch (ctx->poec_format) {
186 1.36 thorpej case PROP_FORMAT_JSON:
187 1.36 thorpej rv = tags->json_close_tag == NULL ||
188 1.36 thorpej _prop_object_externalize_append_cstring(ctx,
189 1.36 thorpej tags->json_close_tag);
190 1.36 thorpej break;
191 1.36 thorpej
192 1.36 thorpej default: /* XML */
193 1.36 thorpej rv = _prop_object_externalize_append_char(ctx, '<') &&
194 1.36 thorpej _prop_object_externalize_append_char(ctx, '/') &&
195 1.36 thorpej _prop_object_externalize_append_cstring(ctx,
196 1.36 thorpej tags->xml_tag) &&
197 1.36 thorpej _prop_object_externalize_append_char(ctx, '>');
198 1.36 thorpej break;
199 1.36 thorpej }
200 1.1 thorpej
201 1.36 thorpej return rv;
202 1.1 thorpej }
203 1.1 thorpej
204 1.1 thorpej /*
205 1.1 thorpej * _prop_object_externalize_empty_tag --
206 1.36 thorpej * Append an item's empty tag to the externalize buffer.
207 1.1 thorpej */
208 1.14 thorpej bool
209 1.1 thorpej _prop_object_externalize_empty_tag(
210 1.36 thorpej struct _prop_object_externalize_context *ctx,
211 1.36 thorpej const struct _prop_object_type_tags *tags)
212 1.1 thorpej {
213 1.36 thorpej bool rv;
214 1.36 thorpej
215 1.36 thorpej _PROP_ASSERT(ctx->poec_format == PROP_FORMAT_XML ||
216 1.36 thorpej ctx->poec_format == PROP_FORMAT_JSON);
217 1.36 thorpej
218 1.36 thorpej switch (ctx->poec_format) {
219 1.36 thorpej case PROP_FORMAT_JSON:
220 1.36 thorpej if (tags->json_open_tag == NULL ||
221 1.36 thorpej _prop_object_externalize_append_cstring(ctx,
222 1.36 thorpej tags->json_open_tag) == false) {
223 1.36 thorpej return false;
224 1.36 thorpej }
225 1.36 thorpej if (tags->json_empty_sep != NULL &&
226 1.36 thorpej _prop_object_externalize_append_cstring(ctx,
227 1.36 thorpej tags->json_empty_sep) == false) {
228 1.36 thorpej return false;
229 1.36 thorpej }
230 1.36 thorpej if (tags->json_close_tag != NULL) {
231 1.36 thorpej rv = _prop_object_externalize_append_cstring(ctx,
232 1.36 thorpej tags->json_close_tag);
233 1.36 thorpej } else {
234 1.36 thorpej rv = true;
235 1.36 thorpej }
236 1.36 thorpej break;
237 1.1 thorpej
238 1.36 thorpej default: /* XML */
239 1.36 thorpej rv = _prop_object_externalize_append_char(ctx, '<') &&
240 1.36 thorpej _prop_object_externalize_append_cstring(ctx,
241 1.36 thorpej tags->xml_tag) &&
242 1.36 thorpej _prop_object_externalize_append_char(ctx, '/') &&
243 1.36 thorpej _prop_object_externalize_append_char(ctx, '>');
244 1.36 thorpej break;
245 1.1 thorpej }
246 1.1 thorpej
247 1.36 thorpej return rv;
248 1.1 thorpej }
249 1.1 thorpej
250 1.1 thorpej /*
251 1.1 thorpej * _prop_object_externalize_append_cstring --
252 1.1 thorpej * Append a C string to the externalize buffer.
253 1.1 thorpej */
254 1.14 thorpej bool
255 1.1 thorpej _prop_object_externalize_append_cstring(
256 1.1 thorpej struct _prop_object_externalize_context *ctx, const char *cp)
257 1.1 thorpej {
258 1.1 thorpej
259 1.1 thorpej while (*cp != '\0') {
260 1.1 thorpej if (_prop_object_externalize_append_char(ctx,
261 1.14 thorpej (unsigned char) *cp) == false)
262 1.14 thorpej return (false);
263 1.1 thorpej cp++;
264 1.1 thorpej }
265 1.1 thorpej
266 1.14 thorpej return (true);
267 1.1 thorpej }
268 1.1 thorpej
269 1.1 thorpej /*
270 1.1 thorpej * _prop_object_externalize_append_encoded_cstring --
271 1.1 thorpej * Append an encoded C string to the externalize buffer.
272 1.1 thorpej */
273 1.36 thorpej static bool
274 1.36 thorpej _prop_object_externalize_append_encoded_cstring_xml(
275 1.1 thorpej struct _prop_object_externalize_context *ctx, const char *cp)
276 1.1 thorpej {
277 1.1 thorpej
278 1.1 thorpej while (*cp != '\0') {
279 1.1 thorpej switch (*cp) {
280 1.1 thorpej case '<':
281 1.1 thorpej if (_prop_object_externalize_append_cstring(ctx,
282 1.14 thorpej "<") == false)
283 1.14 thorpej return (false);
284 1.1 thorpej break;
285 1.1 thorpej case '>':
286 1.1 thorpej if (_prop_object_externalize_append_cstring(ctx,
287 1.14 thorpej ">") == false)
288 1.14 thorpej return (false);
289 1.1 thorpej break;
290 1.1 thorpej case '&':
291 1.1 thorpej if (_prop_object_externalize_append_cstring(ctx,
292 1.14 thorpej "&") == false)
293 1.14 thorpej return (false);
294 1.1 thorpej break;
295 1.1 thorpej default:
296 1.1 thorpej if (_prop_object_externalize_append_char(ctx,
297 1.14 thorpej (unsigned char) *cp) == false)
298 1.14 thorpej return (false);
299 1.1 thorpej break;
300 1.1 thorpej }
301 1.1 thorpej cp++;
302 1.1 thorpej }
303 1.1 thorpej
304 1.14 thorpej return (true);
305 1.1 thorpej }
306 1.1 thorpej
307 1.36 thorpej static bool
308 1.36 thorpej _prop_object_externalize_append_escu(
309 1.36 thorpej struct _prop_object_externalize_context *ctx, uint16_t val)
310 1.36 thorpej {
311 1.36 thorpej char tmpstr[sizeof("\\uXXXX")];
312 1.36 thorpej
313 1.36 thorpej snprintf(tmpstr, sizeof(tmpstr), "\\u%04X", val);
314 1.36 thorpej return _prop_object_externalize_append_cstring(ctx, tmpstr);
315 1.36 thorpej }
316 1.36 thorpej
317 1.36 thorpej static bool
318 1.36 thorpej _prop_object_externalize_append_encoded_cstring_json(
319 1.36 thorpej struct _prop_object_externalize_context *ctx, const char *cp)
320 1.36 thorpej {
321 1.36 thorpej bool esc;
322 1.36 thorpej unsigned char ch;
323 1.36 thorpej
324 1.36 thorpej while ((ch = *cp) != '\0') {
325 1.36 thorpej esc = true;
326 1.36 thorpej switch (ch) {
327 1.36 thorpej /*
328 1.36 thorpej * First, the two explicit exclusions. They must be
329 1.36 thorpej * escaped.
330 1.36 thorpej */
331 1.36 thorpej case '"': /* U+0022 quotation mark */
332 1.36 thorpej goto emit;
333 1.36 thorpej
334 1.36 thorpej case '\\': /* U+005C reverse solidus */
335 1.36 thorpej goto emit;
336 1.36 thorpej
337 1.36 thorpej /*
338 1.36 thorpej * And some special cases that are explcit in the grammar.
339 1.36 thorpej */
340 1.36 thorpej case '/': /* U+002F solidus (XXX this one seems silly) */
341 1.36 thorpej goto emit;
342 1.36 thorpej
343 1.36 thorpej case 0x08: /* U+0008 backspace */
344 1.36 thorpej ch = 'b';
345 1.36 thorpej goto emit;
346 1.36 thorpej
347 1.36 thorpej case 0x0c: /* U+000C form feed */
348 1.36 thorpej ch = 'f';
349 1.36 thorpej goto emit;
350 1.36 thorpej
351 1.36 thorpej case 0x0a: /* U+000A line feed */
352 1.36 thorpej ch = 'n';
353 1.36 thorpej goto emit;
354 1.36 thorpej
355 1.36 thorpej case 0x0d: /* U+000D carraige return */
356 1.36 thorpej ch = 'r';
357 1.36 thorpej goto emit;
358 1.36 thorpej
359 1.36 thorpej case 0x09: /* U+0009 tab */
360 1.36 thorpej ch = 't';
361 1.36 thorpej goto emit;
362 1.36 thorpej
363 1.36 thorpej default:
364 1.36 thorpej /*
365 1.36 thorpej * \u-escape all other single-byte ASCII control
366 1.36 thorpej * characters, per RFC 8259:
367 1.36 thorpej *
368 1.36 thorpej * <quote>
369 1.36 thorpej * All Unicode characters may be placed within the
370 1.36 thorpej * quotation marks, except for the characters that
371 1.36 thorpej * MUST be escaped: quotation mark, reverse solidus,
372 1.36 thorpej * and the control characters (U+0000 through U+001F).
373 1.36 thorpej * </quote>
374 1.36 thorpej */
375 1.36 thorpej if (ch < 0x20) {
376 1.36 thorpej if (_prop_object_externalize_append_escu(ctx,
377 1.36 thorpej ch) == false) {
378 1.36 thorpej return false;
379 1.36 thorpej }
380 1.36 thorpej break;
381 1.36 thorpej }
382 1.36 thorpej /*
383 1.36 thorpej * We're going to just treat everything else like
384 1.36 thorpej * UTF-8 (we've been handed a C-string, after all)
385 1.36 thorpej * and pretend it will all be OK.
386 1.36 thorpej */
387 1.36 thorpej esc = false;
388 1.36 thorpej emit:
389 1.36 thorpej if ((esc && _prop_object_externalize_append_char(ctx,
390 1.36 thorpej '\\') == false) ||
391 1.36 thorpej _prop_object_externalize_append_char(ctx,
392 1.36 thorpej ch) == false) {
393 1.36 thorpej return false;
394 1.36 thorpej }
395 1.36 thorpej break;
396 1.36 thorpej }
397 1.36 thorpej cp++;
398 1.36 thorpej }
399 1.36 thorpej
400 1.36 thorpej return true;
401 1.36 thorpej }
402 1.36 thorpej
403 1.36 thorpej bool
404 1.36 thorpej _prop_object_externalize_append_encoded_cstring(
405 1.36 thorpej struct _prop_object_externalize_context *ctx, const char *cp)
406 1.36 thorpej {
407 1.36 thorpej _PROP_ASSERT(ctx->poec_format == PROP_FORMAT_XML ||
408 1.36 thorpej ctx->poec_format == PROP_FORMAT_JSON);
409 1.36 thorpej
410 1.36 thorpej switch (ctx->poec_format) {
411 1.36 thorpej case PROP_FORMAT_JSON:
412 1.36 thorpej return _prop_object_externalize_append_encoded_cstring_json(ctx,
413 1.36 thorpej cp);
414 1.36 thorpej default:
415 1.36 thorpej return _prop_object_externalize_append_encoded_cstring_xml(ctx,
416 1.36 thorpej cp);
417 1.36 thorpej }
418 1.36 thorpej }
419 1.36 thorpej
420 1.11 martin #define BUF_EXPAND 256
421 1.1 thorpej
422 1.1 thorpej /*
423 1.1 thorpej * _prop_object_externalize_append_char --
424 1.1 thorpej * Append a single character to the externalize buffer.
425 1.1 thorpej */
426 1.14 thorpej bool
427 1.1 thorpej _prop_object_externalize_append_char(
428 1.1 thorpej struct _prop_object_externalize_context *ctx, unsigned char c)
429 1.1 thorpej {
430 1.1 thorpej
431 1.1 thorpej _PROP_ASSERT(ctx->poec_capacity != 0);
432 1.1 thorpej _PROP_ASSERT(ctx->poec_buf != NULL);
433 1.1 thorpej _PROP_ASSERT(ctx->poec_len <= ctx->poec_capacity);
434 1.1 thorpej
435 1.1 thorpej if (ctx->poec_len == ctx->poec_capacity) {
436 1.2 thorpej char *cp = _PROP_REALLOC(ctx->poec_buf,
437 1.2 thorpej ctx->poec_capacity + BUF_EXPAND,
438 1.2 thorpej M_TEMP);
439 1.1 thorpej if (cp == NULL)
440 1.14 thorpej return (false);
441 1.1 thorpej ctx->poec_capacity = ctx->poec_capacity + BUF_EXPAND;
442 1.1 thorpej ctx->poec_buf = cp;
443 1.1 thorpej }
444 1.1 thorpej
445 1.1 thorpej ctx->poec_buf[ctx->poec_len++] = c;
446 1.1 thorpej
447 1.14 thorpej return (true);
448 1.1 thorpej }
449 1.1 thorpej
450 1.36 thorpej static const struct _prop_object_type_tags _plist_type_tags = {
451 1.36 thorpej .xml_tag = "plist",
452 1.36 thorpej };
453 1.36 thorpej
454 1.1 thorpej /*
455 1.4 thorpej * _prop_object_externalize_header --
456 1.4 thorpej * Append the standard XML header to the externalize buffer.
457 1.4 thorpej */
458 1.14 thorpej bool
459 1.4 thorpej _prop_object_externalize_header(struct _prop_object_externalize_context *ctx)
460 1.4 thorpej {
461 1.4 thorpej static const char _plist_xml_header[] =
462 1.4 thorpej "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
463 1.4 thorpej "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n";
464 1.4 thorpej
465 1.36 thorpej if (ctx->poec_format != PROP_FORMAT_XML) {
466 1.36 thorpej return true;
467 1.36 thorpej }
468 1.36 thorpej
469 1.4 thorpej if (_prop_object_externalize_append_cstring(ctx,
470 1.14 thorpej _plist_xml_header) == false ||
471 1.4 thorpej _prop_object_externalize_start_tag(ctx,
472 1.36 thorpej &_plist_type_tags,
473 1.36 thorpej "version=\"1.0\"") == false ||
474 1.14 thorpej _prop_object_externalize_append_char(ctx, '\n') == false)
475 1.14 thorpej return (false);
476 1.4 thorpej
477 1.14 thorpej return (true);
478 1.4 thorpej }
479 1.4 thorpej
480 1.4 thorpej /*
481 1.4 thorpej * _prop_object_externalize_footer --
482 1.4 thorpej * Append the standard XML footer to the externalize buffer. This
483 1.4 thorpej * also NUL-terminates the buffer.
484 1.4 thorpej */
485 1.14 thorpej bool
486 1.4 thorpej _prop_object_externalize_footer(struct _prop_object_externalize_context *ctx)
487 1.4 thorpej {
488 1.36 thorpej if (_prop_object_externalize_end_line(ctx, NULL) == false) {
489 1.36 thorpej return false;
490 1.36 thorpej }
491 1.4 thorpej
492 1.36 thorpej if (ctx->poec_format == PROP_FORMAT_XML) {
493 1.36 thorpej if (_prop_object_externalize_end_tag(ctx,
494 1.36 thorpej &_plist_type_tags) == false ||
495 1.36 thorpej _prop_object_externalize_end_line(ctx, NULL) == false) {
496 1.36 thorpej return false;
497 1.36 thorpej }
498 1.36 thorpej }
499 1.4 thorpej
500 1.36 thorpej return _prop_object_externalize_append_char(ctx, '\0');
501 1.4 thorpej }
502 1.4 thorpej
503 1.4 thorpej /*
504 1.1 thorpej * _prop_object_externalize_context_alloc --
505 1.1 thorpej * Allocate an externalize context.
506 1.1 thorpej */
507 1.1 thorpej struct _prop_object_externalize_context *
508 1.36 thorpej _prop_object_externalize_context_alloc(prop_format_t fmt)
509 1.1 thorpej {
510 1.1 thorpej struct _prop_object_externalize_context *ctx;
511 1.1 thorpej
512 1.1 thorpej ctx = _PROP_MALLOC(sizeof(*ctx), M_TEMP);
513 1.1 thorpej if (ctx != NULL) {
514 1.1 thorpej ctx->poec_buf = _PROP_MALLOC(BUF_EXPAND, M_TEMP);
515 1.1 thorpej if (ctx->poec_buf == NULL) {
516 1.1 thorpej _PROP_FREE(ctx, M_TEMP);
517 1.1 thorpej return (NULL);
518 1.1 thorpej }
519 1.1 thorpej ctx->poec_len = 0;
520 1.1 thorpej ctx->poec_capacity = BUF_EXPAND;
521 1.1 thorpej ctx->poec_depth = 0;
522 1.36 thorpej ctx->poec_format = fmt;
523 1.1 thorpej }
524 1.1 thorpej return (ctx);
525 1.1 thorpej }
526 1.1 thorpej
527 1.1 thorpej /*
528 1.1 thorpej * _prop_object_externalize_context_free --
529 1.1 thorpej * Free an externalize context.
530 1.1 thorpej */
531 1.1 thorpej void
532 1.1 thorpej _prop_object_externalize_context_free(
533 1.1 thorpej struct _prop_object_externalize_context *ctx)
534 1.1 thorpej {
535 1.1 thorpej
536 1.1 thorpej /* Buffer is always freed by the caller. */
537 1.1 thorpej _PROP_FREE(ctx, M_TEMP);
538 1.1 thorpej }
539 1.1 thorpej
540 1.1 thorpej /*
541 1.36 thorpej * _prop_object_externalize --
542 1.36 thorpej * Externalize an object, returning a NUL-terminated buffer
543 1.36 thorpej * containing the serialized data in either XML or JSON format.
544 1.36 thorpej * The buffer is allocated with the M_TEMP memory type.
545 1.36 thorpej */
546 1.36 thorpej char *
547 1.36 thorpej _prop_object_externalize(struct _prop_object *obj, prop_format_t fmt)
548 1.36 thorpej {
549 1.36 thorpej struct _prop_object_externalize_context *ctx;
550 1.36 thorpej char *cp = NULL;
551 1.36 thorpej
552 1.36 thorpej if (obj == NULL || obj->po_type->pot_extern == NULL) {
553 1.36 thorpej return NULL;
554 1.36 thorpej }
555 1.36 thorpej if (fmt != PROP_FORMAT_XML && fmt != PROP_FORMAT_JSON) {
556 1.36 thorpej return NULL;
557 1.36 thorpej }
558 1.36 thorpej
559 1.36 thorpej ctx = _prop_object_externalize_context_alloc(fmt);
560 1.36 thorpej if (ctx == NULL) {
561 1.36 thorpej return NULL;
562 1.36 thorpej }
563 1.36 thorpej
564 1.36 thorpej if (_prop_object_externalize_header(ctx) == false ||
565 1.36 thorpej obj->po_type->pot_extern(ctx, obj) == false ||
566 1.36 thorpej _prop_object_externalize_footer(ctx) == false) {
567 1.36 thorpej /* We are responsible for releasing the buffer. */
568 1.36 thorpej _PROP_FREE(ctx->poec_buf, M_TEMP);
569 1.36 thorpej goto bad;
570 1.36 thorpej }
571 1.36 thorpej
572 1.36 thorpej cp = ctx->poec_buf;
573 1.36 thorpej bad:
574 1.36 thorpej _prop_object_externalize_context_free(ctx);
575 1.36 thorpej return cp;
576 1.36 thorpej }
577 1.36 thorpej
578 1.36 thorpej /*
579 1.1 thorpej * _prop_object_internalize_skip_comment --
580 1.1 thorpej * Skip the body and end tag of a comment.
581 1.1 thorpej */
582 1.14 thorpej static bool
583 1.1 thorpej _prop_object_internalize_skip_comment(
584 1.1 thorpej struct _prop_object_internalize_context *ctx)
585 1.1 thorpej {
586 1.1 thorpej const char *cp = ctx->poic_cp;
587 1.1 thorpej
588 1.1 thorpej while (!_PROP_EOF(*cp)) {
589 1.1 thorpej if (cp[0] == '-' &&
590 1.1 thorpej cp[1] == '-' &&
591 1.1 thorpej cp[2] == '>') {
592 1.1 thorpej ctx->poic_cp = cp + 3;
593 1.14 thorpej return (true);
594 1.1 thorpej }
595 1.1 thorpej cp++;
596 1.1 thorpej }
597 1.1 thorpej
598 1.14 thorpej return (false); /* ran out of buffer */
599 1.1 thorpej }
600 1.1 thorpej
601 1.1 thorpej /*
602 1.1 thorpej * _prop_object_internalize_find_tag --
603 1.1 thorpej * Find the next tag in an XML stream. Optionally compare the found
604 1.1 thorpej * tag to an expected tag name. State of the context is undefined
605 1.14 thorpej * if this routine returns false. Upon success, the context points
606 1.1 thorpej * to the first octet after the tag.
607 1.1 thorpej */
608 1.14 thorpej bool
609 1.1 thorpej _prop_object_internalize_find_tag(struct _prop_object_internalize_context *ctx,
610 1.1 thorpej const char *tag, _prop_tag_type_t type)
611 1.1 thorpej {
612 1.1 thorpej const char *cp;
613 1.1 thorpej size_t taglen;
614 1.1 thorpej
615 1.1 thorpej if (tag != NULL)
616 1.1 thorpej taglen = strlen(tag);
617 1.1 thorpej else
618 1.1 thorpej taglen = 0;
619 1.1 thorpej
620 1.1 thorpej start_over:
621 1.1 thorpej cp = ctx->poic_cp;
622 1.1 thorpej
623 1.1 thorpej /*
624 1.1 thorpej * Find the start of the tag.
625 1.1 thorpej */
626 1.36 thorpej cp = _prop_object_internalize_skip_whitespace(cp);
627 1.1 thorpej if (*cp != '<')
628 1.14 thorpej return (false);
629 1.1 thorpej
630 1.1 thorpej ctx->poic_tag_start = cp++;
631 1.1 thorpej if (_PROP_EOF(*cp))
632 1.14 thorpej return (false);
633 1.1 thorpej
634 1.1 thorpej if (*cp == '!') {
635 1.1 thorpej if (cp[1] != '-' || cp[2] != '-')
636 1.14 thorpej return (false);
637 1.1 thorpej /*
638 1.1 thorpej * Comment block -- only allowed if we are allowed to
639 1.1 thorpej * return a start tag.
640 1.1 thorpej */
641 1.1 thorpej if (type == _PROP_TAG_TYPE_END)
642 1.14 thorpej return (false);
643 1.1 thorpej ctx->poic_cp = cp + 3;
644 1.14 thorpej if (_prop_object_internalize_skip_comment(ctx) == false)
645 1.14 thorpej return (false);
646 1.1 thorpej goto start_over;
647 1.1 thorpej }
648 1.1 thorpej
649 1.1 thorpej if (*cp == '/') {
650 1.1 thorpej if (type != _PROP_TAG_TYPE_END &&
651 1.1 thorpej type != _PROP_TAG_TYPE_EITHER)
652 1.14 thorpej return (false);
653 1.1 thorpej cp++;
654 1.1 thorpej if (_PROP_EOF(*cp))
655 1.14 thorpej return (false);
656 1.1 thorpej ctx->poic_tag_type = _PROP_TAG_TYPE_END;
657 1.1 thorpej } else {
658 1.1 thorpej if (type != _PROP_TAG_TYPE_START &&
659 1.1 thorpej type != _PROP_TAG_TYPE_EITHER)
660 1.14 thorpej return (false);
661 1.1 thorpej ctx->poic_tag_type = _PROP_TAG_TYPE_START;
662 1.1 thorpej }
663 1.1 thorpej
664 1.1 thorpej ctx->poic_tagname = cp;
665 1.1 thorpej
666 1.30 christos while (!_PROP_ISSPACE(*cp) && *cp != '/' && *cp != '>') {
667 1.30 christos if (_PROP_EOF(*cp))
668 1.30 christos return (false);
669 1.1 thorpej cp++;
670 1.30 christos }
671 1.1 thorpej
672 1.1 thorpej ctx->poic_tagname_len = cp - ctx->poic_tagname;
673 1.1 thorpej
674 1.1 thorpej /* Make sure this is the tag we're looking for. */
675 1.1 thorpej if (tag != NULL &&
676 1.1 thorpej (taglen != ctx->poic_tagname_len ||
677 1.1 thorpej memcmp(tag, ctx->poic_tagname, taglen) != 0))
678 1.14 thorpej return (false);
679 1.32 riastrad
680 1.1 thorpej /* Check for empty tag. */
681 1.1 thorpej if (*cp == '/') {
682 1.1 thorpej if (ctx->poic_tag_type != _PROP_TAG_TYPE_START)
683 1.14 thorpej return(false); /* only valid on start tags */
684 1.14 thorpej ctx->poic_is_empty_element = true;
685 1.1 thorpej cp++;
686 1.1 thorpej if (_PROP_EOF(*cp) || *cp != '>')
687 1.14 thorpej return (false);
688 1.1 thorpej } else
689 1.14 thorpej ctx->poic_is_empty_element = false;
690 1.1 thorpej
691 1.1 thorpej /* Easy case of no arguments. */
692 1.1 thorpej if (*cp == '>') {
693 1.1 thorpej ctx->poic_tagattr = NULL;
694 1.1 thorpej ctx->poic_tagattr_len = 0;
695 1.1 thorpej ctx->poic_tagattrval = NULL;
696 1.1 thorpej ctx->poic_tagattrval_len = 0;
697 1.1 thorpej ctx->poic_cp = cp + 1;
698 1.14 thorpej return (true);
699 1.1 thorpej }
700 1.1 thorpej
701 1.1 thorpej _PROP_ASSERT(!_PROP_EOF(*cp));
702 1.1 thorpej cp++;
703 1.1 thorpej if (_PROP_EOF(*cp))
704 1.14 thorpej return (false);
705 1.1 thorpej
706 1.36 thorpej cp = _prop_object_internalize_skip_whitespace(cp);
707 1.1 thorpej if (_PROP_EOF(*cp))
708 1.14 thorpej return (false);
709 1.1 thorpej
710 1.1 thorpej ctx->poic_tagattr = cp;
711 1.1 thorpej
712 1.30 christos while (!_PROP_ISSPACE(*cp) && *cp != '=') {
713 1.30 christos if (_PROP_EOF(*cp))
714 1.30 christos return (false);
715 1.1 thorpej cp++;
716 1.30 christos }
717 1.1 thorpej
718 1.1 thorpej ctx->poic_tagattr_len = cp - ctx->poic_tagattr;
719 1.32 riastrad
720 1.1 thorpej cp++;
721 1.1 thorpej if (*cp != '\"')
722 1.14 thorpej return (false);
723 1.1 thorpej cp++;
724 1.1 thorpej if (_PROP_EOF(*cp))
725 1.14 thorpej return (false);
726 1.32 riastrad
727 1.1 thorpej ctx->poic_tagattrval = cp;
728 1.30 christos while (*cp != '\"') {
729 1.30 christos if (_PROP_EOF(*cp))
730 1.30 christos return (false);
731 1.1 thorpej cp++;
732 1.30 christos }
733 1.1 thorpej ctx->poic_tagattrval_len = cp - ctx->poic_tagattrval;
734 1.32 riastrad
735 1.1 thorpej cp++;
736 1.1 thorpej if (*cp != '>')
737 1.14 thorpej return (false);
738 1.1 thorpej
739 1.1 thorpej ctx->poic_cp = cp + 1;
740 1.14 thorpej return (true);
741 1.1 thorpej }
742 1.1 thorpej
743 1.1 thorpej /*
744 1.1 thorpej * _prop_object_internalize_decode_string --
745 1.1 thorpej * Decode an encoded string.
746 1.1 thorpej */
747 1.36 thorpej
748 1.36 thorpej #define ADDCHAR(x) \
749 1.36 thorpej do { \
750 1.36 thorpej if (target) { \
751 1.36 thorpej if (tarindex >= targsize) { \
752 1.36 thorpej return false; \
753 1.36 thorpej } \
754 1.36 thorpej target[tarindex] = (x); \
755 1.36 thorpej } \
756 1.36 thorpej tarindex++; \
757 1.36 thorpej } while (/*CONSTCOND*/0)
758 1.36 thorpej
759 1.36 thorpej static bool
760 1.36 thorpej _prop_object_internalize_decode_string_xml(
761 1.1 thorpej struct _prop_object_internalize_context *ctx,
762 1.1 thorpej char *target, size_t targsize, size_t *sizep,
763 1.1 thorpej const char **cpp)
764 1.1 thorpej {
765 1.1 thorpej const char *src;
766 1.1 thorpej size_t tarindex;
767 1.1 thorpej char c;
768 1.32 riastrad
769 1.1 thorpej tarindex = 0;
770 1.1 thorpej src = ctx->poic_cp;
771 1.1 thorpej
772 1.1 thorpej for (;;) {
773 1.1 thorpej if (_PROP_EOF(*src))
774 1.14 thorpej return (false);
775 1.1 thorpej if (*src == '<') {
776 1.1 thorpej break;
777 1.1 thorpej }
778 1.1 thorpej
779 1.1 thorpej if ((c = *src) == '&') {
780 1.1 thorpej if (src[1] == 'a' &&
781 1.1 thorpej src[2] == 'm' &&
782 1.1 thorpej src[3] == 'p' &&
783 1.1 thorpej src[4] == ';') {
784 1.1 thorpej c = '&';
785 1.1 thorpej src += 5;
786 1.1 thorpej } else if (src[1] == 'l' &&
787 1.1 thorpej src[2] == 't' &&
788 1.1 thorpej src[3] == ';') {
789 1.1 thorpej c = '<';
790 1.1 thorpej src += 4;
791 1.1 thorpej } else if (src[1] == 'g' &&
792 1.1 thorpej src[2] == 't' &&
793 1.1 thorpej src[3] == ';') {
794 1.1 thorpej c = '>';
795 1.1 thorpej src += 4;
796 1.1 thorpej } else if (src[1] == 'a' &&
797 1.1 thorpej src[2] == 'p' &&
798 1.1 thorpej src[3] == 'o' &&
799 1.1 thorpej src[4] == 's' &&
800 1.1 thorpej src[5] == ';') {
801 1.1 thorpej c = '\'';
802 1.1 thorpej src += 6;
803 1.1 thorpej } else if (src[1] == 'q' &&
804 1.1 thorpej src[2] == 'u' &&
805 1.1 thorpej src[3] == 'o' &&
806 1.1 thorpej src[4] == 't' &&
807 1.1 thorpej src[5] == ';') {
808 1.1 thorpej c = '\"';
809 1.1 thorpej src += 6;
810 1.1 thorpej } else
811 1.14 thorpej return (false);
812 1.1 thorpej } else
813 1.1 thorpej src++;
814 1.36 thorpej ADDCHAR(c);
815 1.1 thorpej }
816 1.1 thorpej
817 1.1 thorpej _PROP_ASSERT(*src == '<');
818 1.1 thorpej if (sizep != NULL)
819 1.1 thorpej *sizep = tarindex;
820 1.1 thorpej if (cpp != NULL)
821 1.1 thorpej *cpp = src;
822 1.32 riastrad
823 1.14 thorpej return (true);
824 1.1 thorpej }
825 1.1 thorpej
826 1.36 thorpej static unsigned int
827 1.36 thorpej _prop_object_decode_string_uesc_getu16(const char *src, unsigned int idx,
828 1.36 thorpej uint16_t *valp)
829 1.36 thorpej {
830 1.36 thorpej unsigned int i;
831 1.36 thorpej uint16_t val;
832 1.36 thorpej unsigned char c;
833 1.36 thorpej
834 1.36 thorpej if (src[idx] != '\\' || src[idx + 1] != 'u') {
835 1.36 thorpej return 0;
836 1.36 thorpej }
837 1.36 thorpej
838 1.36 thorpej for (val = 0, i = 2; i < 6; i++) {
839 1.36 thorpej val <<= 4;
840 1.36 thorpej c = src[idx + i];
841 1.36 thorpej if (c >= 'A' && c <= 'F') {
842 1.36 thorpej val |= 10 + (c - 'A');
843 1.36 thorpej } else if (c >= 'a' && c <= 'f') {
844 1.36 thorpej val |= 10 + (c - 'a');
845 1.36 thorpej } else if (c >= '0' && c <= '9') {
846 1.36 thorpej val |= c - '0';
847 1.36 thorpej } else {
848 1.36 thorpej return 0;
849 1.36 thorpej }
850 1.36 thorpej }
851 1.36 thorpej
852 1.36 thorpej *valp = val;
853 1.36 thorpej return idx + i;
854 1.36 thorpej }
855 1.36 thorpej
856 1.36 thorpej #define HS_FIRST 0xd800
857 1.36 thorpej #define HS_LAST 0xdbff
858 1.36 thorpej #define HS_SHIFT 10
859 1.36 thorpej #define LS_FIRST 0xdc00
860 1.36 thorpej #define LS_LAST 0xdfff
861 1.36 thorpej
862 1.36 thorpej #define HIGH_SURROGAGE_P(x) \
863 1.36 thorpej ((x) >= HS_FIRST && (x) <= HS_LAST)
864 1.36 thorpej #define LOW_SURROGATE_P(x) \
865 1.36 thorpej ((x) >= LS_FIRST && (x) <= LS_LAST)
866 1.36 thorpej #define SURROGATE_P(x) \
867 1.36 thorpej (HIGH_SURROGAGE_P(x) || LOW_SURROGATE_P(x))
868 1.36 thorpej
869 1.36 thorpej static int
870 1.36 thorpej _prop_object_decode_string_uesc(const char *src, char *c,
871 1.36 thorpej unsigned int *cszp)
872 1.36 thorpej {
873 1.36 thorpej unsigned int idx = 0;
874 1.36 thorpej uint32_t code;
875 1.36 thorpej uint16_t code16[2] = { 0, 0 };
876 1.36 thorpej
877 1.36 thorpej idx = _prop_object_decode_string_uesc_getu16(src, idx, &code16[0]);
878 1.36 thorpej if (idx == 0) {
879 1.36 thorpej return 0;
880 1.36 thorpej }
881 1.36 thorpej if (! SURROGATE_P(code16[0])) {
882 1.36 thorpej /* Simple case: not a surrogate pair */
883 1.36 thorpej code = code16[0];
884 1.36 thorpej } else if (HIGH_SURROGAGE_P(code16[0])) {
885 1.36 thorpej idx = _prop_object_decode_string_uesc_getu16(src, idx,
886 1.36 thorpej &code16[1]);
887 1.36 thorpej if (idx == 0) {
888 1.36 thorpej return 0;
889 1.36 thorpej }
890 1.36 thorpej /* Next code must be the low surrogate. */
891 1.36 thorpej if (! LOW_SURROGATE_P(code16[1])) {
892 1.36 thorpej return 0;
893 1.36 thorpej }
894 1.36 thorpej code = (((uint32_t)code16[0] - HS_FIRST) << HS_SHIFT) +
895 1.36 thorpej ( code16[1] - LS_FIRST) +
896 1.36 thorpej 0x10000;
897 1.36 thorpej } else {
898 1.36 thorpej /* Got the low surrogate first; this is an error. */
899 1.36 thorpej return 0;
900 1.36 thorpej }
901 1.36 thorpej
902 1.36 thorpej /*
903 1.36 thorpej * Ok, we have the code point. Now convert it to UTF-8.
904 1.36 thorpej * First we'll just split into nybbles.
905 1.36 thorpej */
906 1.36 thorpej uint8_t u = (code >> 20) & 0xf;
907 1.36 thorpej uint8_t v = (code >> 16) & 0xf;
908 1.36 thorpej uint8_t w = (code >> 12) & 0xf;
909 1.36 thorpej uint8_t x = (code >> 8) & 0xf;
910 1.36 thorpej uint8_t y = (code >> 4) & 0xf;
911 1.36 thorpej uint8_t z = (code ) & 0xf;
912 1.36 thorpej
913 1.36 thorpej /*
914 1.36 thorpej * ...and swizzle the nybbles accordingly.
915 1.36 thorpej *
916 1.36 thorpej * N.B. we expcitly disallow inserting a NUL into the string
917 1.36 thorpej * by way of a \uXXXX escape.
918 1.36 thorpej */
919 1.36 thorpej if (code == 0) {
920 1.36 thorpej /* Not allowed. */
921 1.36 thorpej return 0;
922 1.36 thorpej } else if (/*code >= 0x0000 &&*/ code <= 0x007f) {
923 1.36 thorpej c[0] = (char)code; /* == (y << 4) | z */
924 1.36 thorpej *cszp = 1;
925 1.36 thorpej } else if (/*code >= 0x0080 &&*/ code <= 0x07ff) {
926 1.36 thorpej c[0] = 0xc0 | (x << 2) | (y >> 2);
927 1.36 thorpej c[1] = 0x80 | ((y & 3) << 4) | z;
928 1.36 thorpej *cszp = 2;
929 1.36 thorpej } else if (/*code >= 0x0800 &&*/ code <= 0xffff) {
930 1.36 thorpej c[0] = 0xe0 | w;
931 1.36 thorpej c[1] = 0x80 | (x << 2) | (y >> 2);
932 1.36 thorpej c[2] = 0x80 | ((y & 3) << 4) | z;
933 1.36 thorpej *cszp = 3;
934 1.36 thorpej } else if (/*code >= 0x010000 &&*/ code <= 0x10ffff) {
935 1.36 thorpej c[0] = 0xf0 | ((u & 1) << 2) | (v >> 2);
936 1.36 thorpej c[1] = 0x80 | ((v & 3) << 4) | w;
937 1.36 thorpej c[2] = 0x80 | (x << 2) | (y >> 2);
938 1.36 thorpej c[3] = 0x80 | ((y & 3) << 4) | z;
939 1.36 thorpej *cszp = 4;
940 1.36 thorpej } else {
941 1.36 thorpej /* Invalid code. */
942 1.36 thorpej return 0;
943 1.36 thorpej }
944 1.36 thorpej
945 1.36 thorpej return idx; /* advance input by this much */
946 1.36 thorpej }
947 1.36 thorpej
948 1.36 thorpej #undef HS_FIRST
949 1.36 thorpej #undef HS_LAST
950 1.36 thorpej #undef LS_FIRST
951 1.36 thorpej #undef LS_LAST
952 1.36 thorpej #undef HIGH_SURROGAGE_P
953 1.36 thorpej #undef LOW_SURROGATE_P
954 1.36 thorpej #undef SURROGATE_P
955 1.36 thorpej
956 1.36 thorpej static bool
957 1.36 thorpej _prop_object_internalize_decode_string_json(
958 1.36 thorpej struct _prop_object_internalize_context *ctx,
959 1.36 thorpej char *target, size_t targsize, size_t *sizep,
960 1.36 thorpej const char **cpp)
961 1.36 thorpej {
962 1.36 thorpej const char *src;
963 1.36 thorpej size_t tarindex;
964 1.36 thorpej char c[4];
965 1.36 thorpej unsigned int csz;
966 1.36 thorpej
967 1.36 thorpej tarindex = 0;
968 1.36 thorpej src = ctx->poic_cp;
969 1.36 thorpej
970 1.36 thorpej for (;;) {
971 1.36 thorpej if (_PROP_EOF(*src)) {
972 1.36 thorpej return false;
973 1.36 thorpej }
974 1.36 thorpej if (*src == '"') {
975 1.36 thorpej break;
976 1.36 thorpej }
977 1.36 thorpej
978 1.36 thorpej csz = 1;
979 1.36 thorpej if ((c[0] = *src) == '\\') {
980 1.36 thorpej int advance = 2;
981 1.36 thorpej
982 1.36 thorpej switch ((c[0] = src[1])) {
983 1.36 thorpej case '"': /* quotation mark */
984 1.36 thorpej case '\\': /* reverse solidus */
985 1.36 thorpej case '/': /* solidus */
986 1.36 thorpej /* identity mapping */
987 1.36 thorpej break;
988 1.36 thorpej
989 1.36 thorpej case 'b': /* backspace */
990 1.36 thorpej c[0] = 0x08;
991 1.36 thorpej break;
992 1.36 thorpej
993 1.36 thorpej case 'f': /* form feed */
994 1.36 thorpej c[0] = 0x0c;
995 1.36 thorpej break;
996 1.36 thorpej
997 1.36 thorpej case 'n': /* line feed */
998 1.36 thorpej c[0] = 0x0a;
999 1.36 thorpej break;
1000 1.36 thorpej
1001 1.36 thorpej case 'r': /* carraige return */
1002 1.36 thorpej c[0] = 0x0d;
1003 1.36 thorpej break;
1004 1.36 thorpej
1005 1.36 thorpej case 't': /* tab */
1006 1.36 thorpej c[0] = 0x09;
1007 1.36 thorpej break;
1008 1.36 thorpej
1009 1.36 thorpej case 'u':
1010 1.36 thorpej advance = _prop_object_decode_string_uesc(
1011 1.36 thorpej src, c, &csz);
1012 1.36 thorpej if (advance == 0) {
1013 1.36 thorpej return false;
1014 1.36 thorpej }
1015 1.36 thorpej break;
1016 1.36 thorpej
1017 1.36 thorpej default:
1018 1.36 thorpej /* invalid escape */
1019 1.36 thorpej return false;
1020 1.36 thorpej }
1021 1.36 thorpej src += advance;
1022 1.36 thorpej } else {
1023 1.36 thorpej src++;
1024 1.36 thorpej }
1025 1.36 thorpej for (unsigned int i = 0; i < csz; i++) {
1026 1.36 thorpej ADDCHAR(c[i]);
1027 1.36 thorpej }
1028 1.36 thorpej }
1029 1.36 thorpej
1030 1.36 thorpej _PROP_ASSERT(*src == '"');
1031 1.36 thorpej if (sizep != NULL) {
1032 1.36 thorpej *sizep = tarindex;
1033 1.36 thorpej }
1034 1.36 thorpej if (cpp != NULL) {
1035 1.36 thorpej *cpp = src;
1036 1.36 thorpej }
1037 1.36 thorpej
1038 1.36 thorpej return true;
1039 1.36 thorpej }
1040 1.36 thorpej
1041 1.36 thorpej #undef ADDCHAR
1042 1.36 thorpej
1043 1.36 thorpej bool
1044 1.36 thorpej _prop_object_internalize_decode_string(
1045 1.36 thorpej struct _prop_object_internalize_context *ctx,
1046 1.36 thorpej char *target, size_t targsize, size_t *sizep,
1047 1.36 thorpej const char **cpp)
1048 1.36 thorpej {
1049 1.36 thorpej _PROP_ASSERT(ctx->poic_format == PROP_FORMAT_XML ||
1050 1.36 thorpej ctx->poic_format == PROP_FORMAT_JSON);
1051 1.36 thorpej
1052 1.36 thorpej switch (ctx->poic_format) {
1053 1.36 thorpej case PROP_FORMAT_JSON:
1054 1.36 thorpej return _prop_object_internalize_decode_string_json(ctx,
1055 1.36 thorpej target, targsize, sizep, cpp);
1056 1.36 thorpej
1057 1.36 thorpej default: /* XML */
1058 1.36 thorpej return _prop_object_internalize_decode_string_xml(ctx,
1059 1.36 thorpej target, targsize, sizep, cpp);
1060 1.36 thorpej }
1061 1.36 thorpej }
1062 1.36 thorpej
1063 1.36 thorpej /*
1064 1.36 thorpej * _prop_object_internalize_skip_whitespace --
1065 1.36 thorpej * Skip a span of whitespace.
1066 1.36 thorpej */
1067 1.36 thorpej const char *
1068 1.36 thorpej _prop_object_internalize_skip_whitespace(const char *cp)
1069 1.36 thorpej {
1070 1.36 thorpej while (_PROP_ISSPACE(*cp)) {
1071 1.36 thorpej cp++;
1072 1.36 thorpej }
1073 1.36 thorpej return cp;
1074 1.36 thorpej }
1075 1.36 thorpej
1076 1.1 thorpej /*
1077 1.1 thorpej * _prop_object_internalize_match --
1078 1.1 thorpej * Returns true if the two character streams match.
1079 1.1 thorpej */
1080 1.14 thorpej bool
1081 1.1 thorpej _prop_object_internalize_match(const char *str1, size_t len1,
1082 1.1 thorpej const char *str2, size_t len2)
1083 1.1 thorpej {
1084 1.1 thorpej
1085 1.1 thorpej return (len1 == len2 && memcmp(str1, str2, len1) == 0);
1086 1.1 thorpej }
1087 1.1 thorpej
1088 1.1 thorpej #define INTERNALIZER(t, f) \
1089 1.1 thorpej { t, sizeof(t) - 1, f }
1090 1.1 thorpej
1091 1.1 thorpej static const struct _prop_object_internalizer {
1092 1.15 joerg const char *poi_tag;
1093 1.15 joerg size_t poi_taglen;
1094 1.15 joerg prop_object_internalizer_t poi_intern;
1095 1.1 thorpej } _prop_object_internalizer_table[] = {
1096 1.1 thorpej INTERNALIZER("array", _prop_array_internalize),
1097 1.1 thorpej
1098 1.1 thorpej INTERNALIZER("true", _prop_bool_internalize),
1099 1.1 thorpej INTERNALIZER("false", _prop_bool_internalize),
1100 1.1 thorpej
1101 1.1 thorpej INTERNALIZER("data", _prop_data_internalize),
1102 1.1 thorpej
1103 1.1 thorpej INTERNALIZER("dict", _prop_dictionary_internalize),
1104 1.1 thorpej
1105 1.1 thorpej INTERNALIZER("integer", _prop_number_internalize),
1106 1.1 thorpej
1107 1.1 thorpej INTERNALIZER("string", _prop_string_internalize),
1108 1.1 thorpej
1109 1.5 christos { 0, 0, NULL }
1110 1.1 thorpej };
1111 1.1 thorpej
1112 1.1 thorpej #undef INTERNALIZER
1113 1.1 thorpej
1114 1.1 thorpej /*
1115 1.1 thorpej * _prop_object_internalize_by_tag --
1116 1.1 thorpej * Determine the object type from the tag in the context and
1117 1.1 thorpej * internalize it.
1118 1.1 thorpej */
1119 1.36 thorpej static prop_object_t
1120 1.1 thorpej _prop_object_internalize_by_tag(struct _prop_object_internalize_context *ctx)
1121 1.1 thorpej {
1122 1.1 thorpej const struct _prop_object_internalizer *poi;
1123 1.15 joerg prop_object_t obj, parent_obj;
1124 1.15 joerg void *data, *iter;
1125 1.15 joerg prop_object_internalizer_continue_t iter_func;
1126 1.15 joerg struct _prop_stack stack;
1127 1.1 thorpej
1128 1.15 joerg _prop_stack_init(&stack);
1129 1.15 joerg
1130 1.36 thorpej match_start:
1131 1.1 thorpej for (poi = _prop_object_internalizer_table;
1132 1.1 thorpej poi->poi_tag != NULL; poi++) {
1133 1.1 thorpej if (_prop_object_internalize_match(ctx->poic_tagname,
1134 1.1 thorpej ctx->poic_tagname_len,
1135 1.1 thorpej poi->poi_tag,
1136 1.1 thorpej poi->poi_taglen))
1137 1.15 joerg break;
1138 1.15 joerg }
1139 1.26 haad if ((poi == NULL) || (poi->poi_tag == NULL)) {
1140 1.16 joerg while (_prop_stack_pop(&stack, &obj, &iter, &data, NULL)) {
1141 1.15 joerg iter_func = (prop_object_internalizer_continue_t)iter;
1142 1.15 joerg (*iter_func)(&stack, &obj, ctx, data, NULL);
1143 1.15 joerg }
1144 1.15 joerg
1145 1.15 joerg return (NULL);
1146 1.15 joerg }
1147 1.15 joerg
1148 1.15 joerg obj = NULL;
1149 1.15 joerg if (!(*poi->poi_intern)(&stack, &obj, ctx))
1150 1.15 joerg goto match_start;
1151 1.15 joerg
1152 1.15 joerg parent_obj = obj;
1153 1.16 joerg while (_prop_stack_pop(&stack, &parent_obj, &iter, &data, NULL)) {
1154 1.15 joerg iter_func = (prop_object_internalizer_continue_t)iter;
1155 1.15 joerg if (!(*iter_func)(&stack, &parent_obj, ctx, data, obj))
1156 1.15 joerg goto match_start;
1157 1.15 joerg obj = parent_obj;
1158 1.1 thorpej }
1159 1.1 thorpej
1160 1.15 joerg return (parent_obj);
1161 1.1 thorpej }
1162 1.1 thorpej
1163 1.36 thorpej /*
1164 1.36 thorpej * _prop_object_internalize_xml --
1165 1.36 thorpej * Internalize a property list from XML data.
1166 1.36 thorpej */
1167 1.36 thorpej static prop_object_t
1168 1.36 thorpej _prop_object_internalize_xml(struct _prop_object_internalize_context *ctx,
1169 1.36 thorpej const struct _prop_object_type_tags *initial_tag)
1170 1.13 joerg {
1171 1.13 joerg prop_object_t obj = NULL;
1172 1.13 joerg
1173 1.13 joerg /* We start with a <plist> tag. */
1174 1.13 joerg if (_prop_object_internalize_find_tag(ctx, "plist",
1175 1.15 joerg _PROP_TAG_TYPE_START) == false)
1176 1.13 joerg goto out;
1177 1.13 joerg
1178 1.13 joerg /* Plist elements cannot be empty. */
1179 1.13 joerg if (ctx->poic_is_empty_element)
1180 1.13 joerg goto out;
1181 1.13 joerg
1182 1.13 joerg /*
1183 1.13 joerg * We don't understand any plist attributes, but Apple XML
1184 1.13 joerg * property lists often have a "version" attribute. If we
1185 1.13 joerg * see that one, we simply ignore it.
1186 1.13 joerg */
1187 1.13 joerg if (ctx->poic_tagattr != NULL &&
1188 1.13 joerg !_PROP_TAGATTR_MATCH(ctx, "version"))
1189 1.13 joerg goto out;
1190 1.13 joerg
1191 1.13 joerg /* Next we expect to see opening master_tag. */
1192 1.36 thorpej if (_prop_object_internalize_find_tag(ctx,
1193 1.36 thorpej initial_tag != NULL ? initial_tag->xml_tag
1194 1.36 thorpej : NULL,
1195 1.36 thorpej _PROP_TAG_TYPE_START) == false)
1196 1.13 joerg goto out;
1197 1.13 joerg
1198 1.13 joerg obj = _prop_object_internalize_by_tag(ctx);
1199 1.13 joerg if (obj == NULL)
1200 1.13 joerg goto out;
1201 1.13 joerg
1202 1.13 joerg /*
1203 1.36 thorpej * We've advanced past the closing main tag.
1204 1.13 joerg * Now we want </plist>.
1205 1.13 joerg */
1206 1.13 joerg if (_prop_object_internalize_find_tag(ctx, "plist",
1207 1.15 joerg _PROP_TAG_TYPE_END) == false) {
1208 1.13 joerg prop_object_release(obj);
1209 1.13 joerg obj = NULL;
1210 1.13 joerg }
1211 1.13 joerg
1212 1.13 joerg out:
1213 1.36 thorpej return (obj);
1214 1.36 thorpej }
1215 1.36 thorpej
1216 1.36 thorpej /*
1217 1.36 thorpej * _prop_object_internalize_json --
1218 1.36 thorpej * Internalize a property list from JSON data.
1219 1.36 thorpej */
1220 1.36 thorpej static prop_object_t
1221 1.36 thorpej _prop_object_internalize_json(struct _prop_object_internalize_context *ctx,
1222 1.36 thorpej const struct _prop_object_type_tags *initial_tag)
1223 1.36 thorpej {
1224 1.36 thorpej prop_object_t obj, parent_obj;
1225 1.36 thorpej void *data, *iter;
1226 1.36 thorpej prop_object_internalizer_continue_t iter_func;
1227 1.36 thorpej struct _prop_stack stack;
1228 1.36 thorpej bool (*intern)(prop_stack_t, prop_object_t *,
1229 1.36 thorpej struct _prop_object_internalize_context *);
1230 1.36 thorpej
1231 1.36 thorpej _prop_stack_init(&stack);
1232 1.36 thorpej
1233 1.36 thorpej match_start:
1234 1.36 thorpej intern = NULL;
1235 1.36 thorpej ctx->poic_tagname = ctx->poic_tagattr = ctx->poic_tagattrval = NULL;
1236 1.36 thorpej ctx->poic_tagname_len = ctx->poic_tagattr_len =
1237 1.36 thorpej ctx->poic_tagattrval_len = 0;
1238 1.36 thorpej ctx->poic_is_empty_element = false;
1239 1.36 thorpej ctx->poic_cp = _prop_object_internalize_skip_whitespace(ctx->poic_cp);
1240 1.36 thorpej switch (ctx->poic_cp[0]) {
1241 1.36 thorpej case '{':
1242 1.36 thorpej ctx->poic_cp++;
1243 1.36 thorpej intern = _prop_dictionary_internalize;
1244 1.36 thorpej break;
1245 1.36 thorpej
1246 1.36 thorpej case '[':
1247 1.36 thorpej ctx->poic_cp++;
1248 1.36 thorpej intern = _prop_array_internalize;
1249 1.36 thorpej break;
1250 1.36 thorpej
1251 1.36 thorpej case '"':
1252 1.36 thorpej ctx->poic_cp++;
1253 1.36 thorpej /* XXX Slightly gross. */
1254 1.36 thorpej if (*ctx->poic_cp == '"') {
1255 1.36 thorpej ctx->poic_cp++;
1256 1.36 thorpej ctx->poic_is_empty_element = true;
1257 1.36 thorpej }
1258 1.36 thorpej intern = _prop_string_internalize;
1259 1.36 thorpej break;
1260 1.36 thorpej
1261 1.36 thorpej case 't':
1262 1.36 thorpej if (ctx->poic_cp[1] == 'r' &&
1263 1.36 thorpej ctx->poic_cp[2] == 'u' &&
1264 1.36 thorpej ctx->poic_cp[3] == 'e') {
1265 1.36 thorpej /* XXX Slightly gross. */
1266 1.36 thorpej ctx->poic_tagname = ctx->poic_cp;
1267 1.36 thorpej ctx->poic_tagname_len = 4;
1268 1.36 thorpej ctx->poic_is_empty_element = true;
1269 1.36 thorpej intern = _prop_bool_internalize;
1270 1.36 thorpej ctx->poic_cp += 4;
1271 1.36 thorpej }
1272 1.36 thorpej break;
1273 1.36 thorpej
1274 1.36 thorpej case 'f':
1275 1.36 thorpej if (ctx->poic_cp[1] == 'a' &&
1276 1.36 thorpej ctx->poic_cp[2] == 'l' &&
1277 1.36 thorpej ctx->poic_cp[3] == 's' &&
1278 1.36 thorpej ctx->poic_cp[4] == 'e') {
1279 1.36 thorpej /* XXX Slightly gross. */
1280 1.36 thorpej ctx->poic_tagname = ctx->poic_cp;
1281 1.36 thorpej ctx->poic_tagname_len = 5;
1282 1.36 thorpej ctx->poic_is_empty_element = true;
1283 1.36 thorpej intern = _prop_bool_internalize;
1284 1.36 thorpej ctx->poic_cp += 5;
1285 1.36 thorpej }
1286 1.36 thorpej break;
1287 1.36 thorpej
1288 1.36 thorpej default:
1289 1.36 thorpej if (ctx->poic_cp[0] == '+' ||
1290 1.36 thorpej ctx->poic_cp[0] == '-' ||
1291 1.36 thorpej (ctx->poic_cp[0] >= '0' && ctx->poic_cp[0] <= '9')) {
1292 1.36 thorpej intern = _prop_number_internalize;
1293 1.36 thorpej }
1294 1.36 thorpej break;
1295 1.36 thorpej }
1296 1.36 thorpej
1297 1.36 thorpej if (intern == NULL) {
1298 1.36 thorpej while (_prop_stack_pop(&stack, &obj, &iter, &data, NULL)) {
1299 1.36 thorpej iter_func = (prop_object_internalizer_continue_t)iter;
1300 1.36 thorpej (*iter_func)(&stack, &obj, ctx, data, NULL);
1301 1.36 thorpej }
1302 1.36 thorpej return NULL;
1303 1.36 thorpej }
1304 1.36 thorpej
1305 1.36 thorpej obj = NULL;
1306 1.36 thorpej if ((*intern)(&stack, &obj, ctx) == false) {
1307 1.36 thorpej goto match_start;
1308 1.36 thorpej }
1309 1.36 thorpej
1310 1.36 thorpej parent_obj = obj;
1311 1.36 thorpej while (_prop_stack_pop(&stack, &parent_obj, &iter, &data, NULL)) {
1312 1.36 thorpej iter_func = (prop_object_internalizer_continue_t)iter;
1313 1.36 thorpej if ((*iter_func)(&stack, &parent_obj, ctx, data,
1314 1.36 thorpej obj) == false) {
1315 1.36 thorpej goto match_start;
1316 1.36 thorpej }
1317 1.36 thorpej obj = parent_obj;
1318 1.36 thorpej }
1319 1.36 thorpej
1320 1.36 thorpej /* Ensure there's no trailing junk. */
1321 1.36 thorpej if (parent_obj != NULL) {
1322 1.36 thorpej ctx->poic_cp =
1323 1.36 thorpej _prop_object_internalize_skip_whitespace(ctx->poic_cp);
1324 1.36 thorpej if (!_PROP_EOF(*ctx->poic_cp)) {
1325 1.36 thorpej prop_object_release(parent_obj);
1326 1.36 thorpej parent_obj = NULL;
1327 1.36 thorpej }
1328 1.36 thorpej }
1329 1.36 thorpej return parent_obj;
1330 1.36 thorpej }
1331 1.36 thorpej
1332 1.36 thorpej /*
1333 1.36 thorpej * _prop_object_internalize --
1334 1.36 thorpej * Internalize a property list from a NUL-terminated data blob.
1335 1.36 thorpej */
1336 1.36 thorpej prop_object_t
1337 1.36 thorpej _prop_object_internalize(const char *data,
1338 1.36 thorpej const struct _prop_object_type_tags *initial_tag)
1339 1.36 thorpej {
1340 1.36 thorpej struct _prop_object_internalize_context *ctx;
1341 1.36 thorpej prop_object_t obj;
1342 1.36 thorpej prop_format_t fmt;
1343 1.36 thorpej
1344 1.36 thorpej /*
1345 1.36 thorpej * Skip all whitespace until and look at the first
1346 1.36 thorpej * non-whitespace character to determine the format:
1347 1.36 thorpej * An XML plist will always have '<' as the first non-ws
1348 1.36 thorpej * character. If we encounter something else, we assume
1349 1.36 thorpej * it is JSON.
1350 1.36 thorpej */
1351 1.36 thorpej data = _prop_object_internalize_skip_whitespace(data);
1352 1.36 thorpej if (_PROP_EOF(*data)) {
1353 1.36 thorpej return NULL;
1354 1.36 thorpej }
1355 1.36 thorpej
1356 1.36 thorpej fmt = *data == '<' ? PROP_FORMAT_XML : PROP_FORMAT_JSON;
1357 1.36 thorpej
1358 1.36 thorpej ctx = _prop_object_internalize_context_alloc(data, fmt);
1359 1.36 thorpej if (ctx == NULL) {
1360 1.36 thorpej return NULL;
1361 1.36 thorpej }
1362 1.36 thorpej
1363 1.36 thorpej if (fmt == PROP_FORMAT_XML) {
1364 1.36 thorpej obj = _prop_object_internalize_xml(ctx, initial_tag);
1365 1.36 thorpej } else {
1366 1.36 thorpej obj = _prop_object_internalize_json(ctx, initial_tag);
1367 1.36 thorpej }
1368 1.36 thorpej
1369 1.13 joerg _prop_object_internalize_context_free(ctx);
1370 1.13 joerg return (obj);
1371 1.13 joerg }
1372 1.13 joerg
1373 1.36 thorpej prop_object_t
1374 1.36 thorpej prop_object_internalize(const char *data)
1375 1.36 thorpej {
1376 1.36 thorpej return _prop_object_internalize(data, NULL);
1377 1.36 thorpej }
1378 1.36 thorpej
1379 1.1 thorpej /*
1380 1.1 thorpej * _prop_object_internalize_context_alloc --
1381 1.1 thorpej * Allocate an internalize context.
1382 1.1 thorpej */
1383 1.1 thorpej struct _prop_object_internalize_context *
1384 1.36 thorpej _prop_object_internalize_context_alloc(const char *data, prop_format_t fmt)
1385 1.1 thorpej {
1386 1.1 thorpej struct _prop_object_internalize_context *ctx;
1387 1.1 thorpej
1388 1.35 riastrad ctx = _PROP_MALLOC(sizeof(*ctx), M_TEMP);
1389 1.1 thorpej if (ctx == NULL)
1390 1.1 thorpej return (NULL);
1391 1.32 riastrad
1392 1.36 thorpej ctx->poic_format = fmt;
1393 1.36 thorpej ctx->poic_data = ctx->poic_cp = data;
1394 1.36 thorpej
1395 1.36 thorpej /*
1396 1.36 thorpej * If we're digesting JSON, check for a byte order mark and
1397 1.36 thorpej * skip it, if present. We should never see one, but we're
1398 1.36 thorpej * allowed to detect and ignore it. (RFC 8259 section 8.1)
1399 1.36 thorpej */
1400 1.36 thorpej if (fmt == PROP_FORMAT_JSON) {
1401 1.36 thorpej if (((unsigned char)data[0] == 0xff &&
1402 1.36 thorpej (unsigned char)data[1] == 0xfe) ||
1403 1.36 thorpej ((unsigned char)data[0] == 0xfe &&
1404 1.36 thorpej (unsigned char)data[1] == 0xff)) {
1405 1.36 thorpej ctx->poic_cp = data + 2;
1406 1.36 thorpej }
1407 1.36 thorpej
1408 1.36 thorpej /* No additional processing work to do for JSON. */
1409 1.36 thorpej return ctx;
1410 1.36 thorpej }
1411 1.1 thorpej
1412 1.1 thorpej /*
1413 1.1 thorpej * Skip any whitespace and XML preamble stuff that we don't
1414 1.1 thorpej * know about / care about.
1415 1.1 thorpej */
1416 1.1 thorpej for (;;) {
1417 1.36 thorpej data = _prop_object_internalize_skip_whitespace(data);
1418 1.36 thorpej if (_PROP_EOF(*data) || *data != '<')
1419 1.1 thorpej goto bad;
1420 1.1 thorpej
1421 1.36 thorpej #define MATCH(str) (strncmp(&data[1], str, strlen(str)) == 0)
1422 1.1 thorpej
1423 1.1 thorpej /*
1424 1.1 thorpej * Skip over the XML preamble that Apple XML property
1425 1.1 thorpej * lists usually include at the top of the file.
1426 1.1 thorpej */
1427 1.1 thorpej if (MATCH("?xml ") ||
1428 1.1 thorpej MATCH("!DOCTYPE plist")) {
1429 1.36 thorpej while (*data != '>' && !_PROP_EOF(*data))
1430 1.36 thorpej data++;
1431 1.36 thorpej if (_PROP_EOF(*data))
1432 1.1 thorpej goto bad;
1433 1.36 thorpej data++; /* advance past the '>' */
1434 1.1 thorpej continue;
1435 1.1 thorpej }
1436 1.1 thorpej
1437 1.1 thorpej if (MATCH("<!--")) {
1438 1.36 thorpej ctx->poic_cp = data + 4;
1439 1.14 thorpej if (_prop_object_internalize_skip_comment(ctx) == false)
1440 1.1 thorpej goto bad;
1441 1.36 thorpej data = ctx->poic_cp;
1442 1.1 thorpej continue;
1443 1.1 thorpej }
1444 1.1 thorpej
1445 1.1 thorpej #undef MATCH
1446 1.1 thorpej
1447 1.1 thorpej /*
1448 1.1 thorpej * We don't think we should skip it, so let's hope we can
1449 1.1 thorpej * parse it.
1450 1.1 thorpej */
1451 1.1 thorpej break;
1452 1.1 thorpej }
1453 1.1 thorpej
1454 1.36 thorpej ctx->poic_cp = data;
1455 1.1 thorpej return (ctx);
1456 1.1 thorpej bad:
1457 1.1 thorpej _PROP_FREE(ctx, M_TEMP);
1458 1.1 thorpej return (NULL);
1459 1.1 thorpej }
1460 1.1 thorpej
1461 1.1 thorpej /*
1462 1.1 thorpej * _prop_object_internalize_context_free --
1463 1.1 thorpej * Free an internalize context.
1464 1.1 thorpej */
1465 1.1 thorpej void
1466 1.1 thorpej _prop_object_internalize_context_free(
1467 1.1 thorpej struct _prop_object_internalize_context *ctx)
1468 1.1 thorpej {
1469 1.1 thorpej
1470 1.1 thorpej _PROP_FREE(ctx, M_TEMP);
1471 1.1 thorpej }
1472 1.1 thorpej
1473 1.4 thorpej #if !defined(_KERNEL) && !defined(_STANDALONE)
1474 1.4 thorpej /*
1475 1.4 thorpej * _prop_object_externalize_file_dirname --
1476 1.4 thorpej * dirname(3), basically. We have to roll our own because the
1477 1.4 thorpej * system dirname(3) isn't reentrant.
1478 1.4 thorpej */
1479 1.4 thorpej static void
1480 1.4 thorpej _prop_object_externalize_file_dirname(const char *path, char *result)
1481 1.4 thorpej {
1482 1.4 thorpej const char *lastp;
1483 1.4 thorpej size_t len;
1484 1.4 thorpej
1485 1.4 thorpej /*
1486 1.4 thorpej * If `path' is a NULL pointer or points to an empty string,
1487 1.4 thorpej * return ".".
1488 1.4 thorpej */
1489 1.4 thorpej if (path == NULL || *path == '\0')
1490 1.4 thorpej goto singledot;
1491 1.32 riastrad
1492 1.4 thorpej /* String trailing slashes, if any. */
1493 1.4 thorpej lastp = path + strlen(path) - 1;
1494 1.4 thorpej while (lastp != path && *lastp == '/')
1495 1.4 thorpej lastp--;
1496 1.32 riastrad
1497 1.4 thorpej /* Terminate path at the last occurrence of '/'. */
1498 1.4 thorpej do {
1499 1.4 thorpej if (*lastp == '/') {
1500 1.4 thorpej /* Strip trailing slashes, if any. */
1501 1.4 thorpej while (lastp != path && *lastp == '/')
1502 1.4 thorpej lastp--;
1503 1.4 thorpej
1504 1.4 thorpej /* ...and copy the result into the result buffer. */
1505 1.4 thorpej len = (lastp - path) + 1 /* last char */;
1506 1.4 thorpej if (len > (PATH_MAX - 1))
1507 1.4 thorpej len = PATH_MAX - 1;
1508 1.4 thorpej
1509 1.4 thorpej memcpy(result, path, len);
1510 1.4 thorpej result[len] = '\0';
1511 1.4 thorpej return;
1512 1.4 thorpej }
1513 1.4 thorpej } while (--lastp >= path);
1514 1.4 thorpej
1515 1.4 thorpej /* No /'s found, return ".". */
1516 1.4 thorpej singledot:
1517 1.4 thorpej strcpy(result, ".");
1518 1.4 thorpej }
1519 1.4 thorpej
1520 1.4 thorpej /*
1521 1.4 thorpej * _prop_object_externalize_write_file --
1522 1.4 thorpej * Write an externalized dictionary to the specified file.
1523 1.4 thorpej * The file is written atomically from the caller's perspective,
1524 1.4 thorpej * and the mode set to 0666 modified by the caller's umask.
1525 1.4 thorpej */
1526 1.36 thorpej static bool
1527 1.36 thorpej _prop_object_externalize_write_file(const char *fname, const char *data,
1528 1.4 thorpej size_t len)
1529 1.4 thorpej {
1530 1.36 thorpej char tname_store[PATH_MAX];
1531 1.36 thorpej char *tname = NULL;
1532 1.36 thorpej int fd = -1;
1533 1.4 thorpej int save_errno;
1534 1.20 lukem mode_t myumask;
1535 1.36 thorpej bool rv = false;
1536 1.4 thorpej
1537 1.4 thorpej if (len > SSIZE_MAX) {
1538 1.4 thorpej errno = EFBIG;
1539 1.36 thorpej return false;
1540 1.4 thorpej }
1541 1.4 thorpej
1542 1.4 thorpej /*
1543 1.4 thorpej * Get the directory name where the file is to be written
1544 1.4 thorpej * and create the temporary file.
1545 1.4 thorpej */
1546 1.36 thorpej _prop_object_externalize_file_dirname(fname, tname_store);
1547 1.28 pooka #define PLISTTMP "/.plistXXXXXX"
1548 1.36 thorpej if (strlen(tname_store) + strlen(PLISTTMP) >= sizeof(tname_store)) {
1549 1.4 thorpej errno = ENAMETOOLONG;
1550 1.36 thorpej return false;
1551 1.4 thorpej }
1552 1.36 thorpej strcat(tname_store, PLISTTMP);
1553 1.28 pooka #undef PLISTTMP
1554 1.28 pooka
1555 1.36 thorpej if ((fd = mkstemp(tname_store)) == -1) {
1556 1.14 thorpej return (false);
1557 1.36 thorpej }
1558 1.36 thorpej tname = tname_store;
1559 1.4 thorpej
1560 1.36 thorpej if (write(fd, data, len) != (ssize_t)len) {
1561 1.4 thorpej goto bad;
1562 1.36 thorpej }
1563 1.4 thorpej
1564 1.36 thorpej if (fsync(fd) == -1) {
1565 1.4 thorpej goto bad;
1566 1.36 thorpej }
1567 1.4 thorpej
1568 1.20 lukem myumask = umask(0);
1569 1.20 lukem (void)umask(myumask);
1570 1.36 thorpej if (fchmod(fd, 0666 & ~myumask) == -1) {
1571 1.20 lukem goto bad;
1572 1.36 thorpej }
1573 1.20 lukem
1574 1.36 thorpej if (rename(tname, fname) == -1) {
1575 1.4 thorpej goto bad;
1576 1.36 thorpej }
1577 1.36 thorpej tname = NULL;
1578 1.4 thorpej
1579 1.36 thorpej rv = true;
1580 1.4 thorpej
1581 1.4 thorpej bad:
1582 1.4 thorpej save_errno = errno;
1583 1.36 thorpej if (fd != -1) {
1584 1.4 thorpej (void) close(fd);
1585 1.36 thorpej }
1586 1.36 thorpej if (tname != NULL) {
1587 1.36 thorpej (void) unlink(tname);
1588 1.36 thorpej }
1589 1.36 thorpej errno = save_errno;
1590 1.36 thorpej return rv;
1591 1.36 thorpej }
1592 1.36 thorpej
1593 1.36 thorpej /*
1594 1.36 thorpej * _prop_object_externalize_to_file --
1595 1.36 thorpej * Externalize an object to the specified file.
1596 1.36 thorpej */
1597 1.36 thorpej bool
1598 1.36 thorpej _prop_object_externalize_to_file(struct _prop_object *obj, const char *fname,
1599 1.36 thorpej prop_format_t fmt)
1600 1.36 thorpej {
1601 1.36 thorpej char *data = _prop_object_externalize(obj, fmt);
1602 1.36 thorpej if (data == NULL) {
1603 1.36 thorpej return false;
1604 1.36 thorpej }
1605 1.36 thorpej bool rv = _prop_object_externalize_write_file(fname, data,
1606 1.36 thorpej strlen(data));
1607 1.36 thorpej int save_errno = errno;
1608 1.36 thorpej _PROP_FREE(data, M_TEMP);
1609 1.4 thorpej errno = save_errno;
1610 1.36 thorpej
1611 1.36 thorpej return rv;
1612 1.4 thorpej }
1613 1.4 thorpej
1614 1.36 thorpej struct _prop_object_internalize_mapped_file {
1615 1.36 thorpej char * poimf_xml;
1616 1.36 thorpej size_t poimf_mapsize;
1617 1.36 thorpej };
1618 1.36 thorpej
1619 1.4 thorpej /*
1620 1.4 thorpej * _prop_object_internalize_map_file --
1621 1.4 thorpej * Map a file for the purpose of internalizing it.
1622 1.4 thorpej */
1623 1.36 thorpej static struct _prop_object_internalize_mapped_file *
1624 1.4 thorpej _prop_object_internalize_map_file(const char *fname)
1625 1.4 thorpej {
1626 1.4 thorpej struct stat sb;
1627 1.4 thorpej struct _prop_object_internalize_mapped_file *mf;
1628 1.12 he size_t pgsize = (size_t)sysconf(_SC_PAGESIZE);
1629 1.4 thorpej size_t pgmask = pgsize - 1;
1630 1.14 thorpej bool need_guard = false;
1631 1.4 thorpej int fd;
1632 1.4 thorpej
1633 1.4 thorpej mf = _PROP_MALLOC(sizeof(*mf), M_TEMP);
1634 1.4 thorpej if (mf == NULL)
1635 1.4 thorpej return (NULL);
1636 1.32 riastrad
1637 1.4 thorpej fd = open(fname, O_RDONLY, 0400);
1638 1.4 thorpej if (fd == -1) {
1639 1.4 thorpej _PROP_FREE(mf, M_TEMP);
1640 1.4 thorpej return (NULL);
1641 1.4 thorpej }
1642 1.4 thorpej
1643 1.4 thorpej if (fstat(fd, &sb) == -1) {
1644 1.4 thorpej (void) close(fd);
1645 1.4 thorpej _PROP_FREE(mf, M_TEMP);
1646 1.4 thorpej return (NULL);
1647 1.4 thorpej }
1648 1.4 thorpej mf->poimf_mapsize = ((size_t)sb.st_size + pgmask) & ~pgmask;
1649 1.25 lukem if (mf->poimf_mapsize < (size_t)sb.st_size) {
1650 1.4 thorpej (void) close(fd);
1651 1.4 thorpej _PROP_FREE(mf, M_TEMP);
1652 1.4 thorpej return (NULL);
1653 1.4 thorpej }
1654 1.4 thorpej
1655 1.4 thorpej /*
1656 1.4 thorpej * If the file length is an integral number of pages, then we
1657 1.4 thorpej * need to map a guard page at the end in order to provide the
1658 1.4 thorpej * necessary NUL-termination of the buffer.
1659 1.4 thorpej */
1660 1.4 thorpej if ((sb.st_size & pgmask) == 0)
1661 1.14 thorpej need_guard = true;
1662 1.4 thorpej
1663 1.4 thorpej mf->poimf_xml = mmap(NULL, need_guard ? mf->poimf_mapsize + pgsize
1664 1.4 thorpej : mf->poimf_mapsize,
1665 1.4 thorpej PROT_READ, MAP_FILE|MAP_SHARED, fd, (off_t)0);
1666 1.4 thorpej (void) close(fd);
1667 1.4 thorpej if (mf->poimf_xml == MAP_FAILED) {
1668 1.4 thorpej _PROP_FREE(mf, M_TEMP);
1669 1.4 thorpej return (NULL);
1670 1.4 thorpej }
1671 1.31 thorpej #ifdef POSIX_MADV_SEQUENTIAL
1672 1.31 thorpej (void) posix_madvise(mf->poimf_xml, mf->poimf_mapsize,
1673 1.31 thorpej POSIX_MADV_SEQUENTIAL);
1674 1.31 thorpej #endif
1675 1.4 thorpej
1676 1.4 thorpej if (need_guard) {
1677 1.4 thorpej if (mmap(mf->poimf_xml + mf->poimf_mapsize,
1678 1.4 thorpej pgsize, PROT_READ,
1679 1.4 thorpej MAP_ANON|MAP_PRIVATE|MAP_FIXED, -1,
1680 1.4 thorpej (off_t)0) == MAP_FAILED) {
1681 1.4 thorpej (void) munmap(mf->poimf_xml, mf->poimf_mapsize);
1682 1.4 thorpej _PROP_FREE(mf, M_TEMP);
1683 1.4 thorpej return (NULL);
1684 1.4 thorpej }
1685 1.4 thorpej mf->poimf_mapsize += pgsize;
1686 1.4 thorpej }
1687 1.4 thorpej
1688 1.4 thorpej return (mf);
1689 1.4 thorpej }
1690 1.4 thorpej
1691 1.4 thorpej /*
1692 1.4 thorpej * _prop_object_internalize_unmap_file --
1693 1.4 thorpej * Unmap a file previously mapped for internalizing.
1694 1.4 thorpej */
1695 1.36 thorpej static void
1696 1.4 thorpej _prop_object_internalize_unmap_file(
1697 1.4 thorpej struct _prop_object_internalize_mapped_file *mf)
1698 1.4 thorpej {
1699 1.4 thorpej
1700 1.31 thorpej #ifdef POSIX_MADV_DONTNEED
1701 1.31 thorpej (void) posix_madvise(mf->poimf_xml, mf->poimf_mapsize,
1702 1.31 thorpej POSIX_MADV_DONTNEED);
1703 1.31 thorpej #endif
1704 1.4 thorpej (void) munmap(mf->poimf_xml, mf->poimf_mapsize);
1705 1.4 thorpej _PROP_FREE(mf, M_TEMP);
1706 1.4 thorpej }
1707 1.36 thorpej
1708 1.36 thorpej /*
1709 1.36 thorpej * _prop_object_internalize_from_file --
1710 1.36 thorpej * Internalize a property list from a file.
1711 1.36 thorpej */
1712 1.36 thorpej prop_object_t
1713 1.36 thorpej _prop_object_internalize_from_file(const char *fname,
1714 1.36 thorpej const struct _prop_object_type_tags *initial_tag)
1715 1.36 thorpej {
1716 1.36 thorpej struct _prop_object_internalize_mapped_file *mf;
1717 1.36 thorpej prop_object_t obj;
1718 1.36 thorpej
1719 1.36 thorpej mf = _prop_object_internalize_map_file(fname);
1720 1.36 thorpej if (mf == NULL) {
1721 1.36 thorpej return NULL;
1722 1.36 thorpej }
1723 1.36 thorpej obj = _prop_object_internalize(mf->poimf_xml, initial_tag);
1724 1.36 thorpej _prop_object_internalize_unmap_file(mf);
1725 1.36 thorpej
1726 1.36 thorpej return obj;
1727 1.36 thorpej }
1728 1.36 thorpej
1729 1.36 thorpej prop_object_t
1730 1.36 thorpej prop_object_internalize_from_file(const char *fname)
1731 1.36 thorpej {
1732 1.36 thorpej return _prop_object_internalize_from_file(fname, NULL);
1733 1.36 thorpej }
1734 1.36 thorpej #endif /* !_KERNEL && !_STANDALONE */
1735 1.36 thorpej
1736 1.36 thorpej prop_format_t _prop_format_default = PROP_FORMAT_XML;
1737 1.36 thorpej
1738 1.36 thorpej /*
1739 1.36 thorpej * prop_object_externalize --
1740 1.36 thorpej * Externalize an object in the default format.
1741 1.36 thorpej */
1742 1.36 thorpej char *
1743 1.36 thorpej prop_object_externalize(prop_object_t po)
1744 1.36 thorpej {
1745 1.36 thorpej return _prop_object_externalize((struct _prop_object *)po,
1746 1.36 thorpej _prop_format_default);
1747 1.36 thorpej }
1748 1.36 thorpej
1749 1.36 thorpej /*
1750 1.36 thorpej * prop_object_externalize_with_format --
1751 1.36 thorpej * Externalize an object in the specified format.
1752 1.36 thorpej */
1753 1.36 thorpej char *
1754 1.36 thorpej prop_object_externalize_with_format(prop_object_t po, prop_format_t fmt)
1755 1.36 thorpej {
1756 1.36 thorpej return _prop_object_externalize((struct _prop_object *)po, fmt);
1757 1.36 thorpej }
1758 1.36 thorpej
1759 1.36 thorpej #if !defined(_KERNEL) && !defined(_STANDALONE)
1760 1.36 thorpej /*
1761 1.36 thorpej * prop_object_externalize_to_file --
1762 1.36 thorpej * Externalize an object to the specifed file in the default format.
1763 1.36 thorpej */
1764 1.36 thorpej bool
1765 1.36 thorpej prop_object_externalize_to_file(prop_object_t po, const char *fname)
1766 1.36 thorpej {
1767 1.36 thorpej return _prop_object_externalize_to_file((struct _prop_object *)po,
1768 1.36 thorpej fname, _prop_format_default);
1769 1.36 thorpej }
1770 1.36 thorpej
1771 1.36 thorpej /*
1772 1.36 thorpej * prop_object_externalize_to_file_with_format --
1773 1.36 thorpej * Externalize an object to the specifed file in the specified format.
1774 1.36 thorpej */
1775 1.36 thorpej bool
1776 1.36 thorpej prop_object_externalize_to_file_with_format(prop_object_t po,
1777 1.36 thorpej const char *fname, prop_format_t fmt)
1778 1.36 thorpej {
1779 1.36 thorpej return _prop_object_externalize_to_file((struct _prop_object *)po,
1780 1.36 thorpej fname, fmt);
1781 1.36 thorpej }
1782 1.4 thorpej #endif /* !_KERNEL && !_STANDALONE */
1783 1.4 thorpej
1784 1.1 thorpej /*
1785 1.1 thorpej * prop_object_retain --
1786 1.1 thorpej * Increment the reference count on an object.
1787 1.1 thorpej */
1788 1.1 thorpej void
1789 1.1 thorpej prop_object_retain(prop_object_t obj)
1790 1.1 thorpej {
1791 1.1 thorpej struct _prop_object *po = obj;
1792 1.29 martin uint32_t ncnt __unused;
1793 1.1 thorpej
1794 1.28 pooka _PROP_ATOMIC_INC32_NV(&po->po_refcnt, ncnt);
1795 1.24 pooka _PROP_ASSERT(ncnt != 0);
1796 1.1 thorpej }
1797 1.1 thorpej
1798 1.1 thorpej /*
1799 1.15 joerg * prop_object_release_emergency
1800 1.15 joerg * A direct free with prop_object_release failed.
1801 1.15 joerg * Walk down the tree until a leaf is found and
1802 1.15 joerg * free that. Do not recurse to avoid stack overflows.
1803 1.15 joerg *
1804 1.15 joerg * This is a slow edge condition, but necessary to
1805 1.17 xtraeme * guarantee that an object can always be freed.
1806 1.15 joerg */
1807 1.15 joerg static void
1808 1.15 joerg prop_object_release_emergency(prop_object_t obj)
1809 1.15 joerg {
1810 1.15 joerg struct _prop_object *po;
1811 1.23 haad void (*unlock)(void);
1812 1.15 joerg prop_object_t parent = NULL;
1813 1.15 joerg uint32_t ocnt;
1814 1.15 joerg
1815 1.15 joerg for (;;) {
1816 1.15 joerg po = obj;
1817 1.15 joerg _PROP_ASSERT(obj);
1818 1.15 joerg
1819 1.23 haad if (po->po_type->pot_lock != NULL)
1820 1.23 haad po->po_type->pot_lock();
1821 1.23 haad
1822 1.23 haad /* Save pointerto unlock function */
1823 1.23 haad unlock = po->po_type->pot_unlock;
1824 1.32 riastrad
1825 1.24 pooka /* Dance a bit to make sure we always get the non-racy ocnt */
1826 1.28 pooka _PROP_ATOMIC_DEC32_NV(&po->po_refcnt, ocnt);
1827 1.24 pooka ocnt++;
1828 1.24 pooka _PROP_ASSERT(ocnt != 0);
1829 1.15 joerg
1830 1.23 haad if (ocnt != 1) {
1831 1.23 haad if (unlock != NULL)
1832 1.23 haad unlock();
1833 1.15 joerg break;
1834 1.23 haad }
1835 1.32 riastrad
1836 1.32 riastrad _PROP_ASSERT(po->po_type);
1837 1.22 thorpej if ((po->po_type->pot_free)(NULL, &obj) ==
1838 1.23 haad _PROP_OBJECT_FREE_DONE) {
1839 1.23 haad if (unlock != NULL)
1840 1.23 haad unlock();
1841 1.15 joerg break;
1842 1.23 haad }
1843 1.15 joerg
1844 1.23 haad if (unlock != NULL)
1845 1.23 haad unlock();
1846 1.32 riastrad
1847 1.15 joerg parent = po;
1848 1.28 pooka _PROP_ATOMIC_INC32(&po->po_refcnt);
1849 1.15 joerg }
1850 1.15 joerg _PROP_ASSERT(parent);
1851 1.15 joerg /* One object was just freed. */
1852 1.15 joerg po = parent;
1853 1.15 joerg (*po->po_type->pot_emergency_free)(parent);
1854 1.15 joerg }
1855 1.15 joerg
1856 1.15 joerg /*
1857 1.1 thorpej * prop_object_release --
1858 1.1 thorpej * Decrement the reference count on an object.
1859 1.1 thorpej *
1860 1.1 thorpej * Free the object if we are releasing the final
1861 1.1 thorpej * reference.
1862 1.1 thorpej */
1863 1.1 thorpej void
1864 1.1 thorpej prop_object_release(prop_object_t obj)
1865 1.1 thorpej {
1866 1.15 joerg struct _prop_object *po;
1867 1.15 joerg struct _prop_stack stack;
1868 1.32 riastrad void (*unlock)(void);
1869 1.15 joerg int ret;
1870 1.1 thorpej uint32_t ocnt;
1871 1.1 thorpej
1872 1.15 joerg _prop_stack_init(&stack);
1873 1.1 thorpej
1874 1.15 joerg do {
1875 1.15 joerg do {
1876 1.15 joerg po = obj;
1877 1.15 joerg _PROP_ASSERT(obj);
1878 1.15 joerg
1879 1.23 haad if (po->po_type->pot_lock != NULL)
1880 1.23 haad po->po_type->pot_lock();
1881 1.23 haad
1882 1.23 haad /* Save pointer to object unlock function */
1883 1.23 haad unlock = po->po_type->pot_unlock;
1884 1.32 riastrad
1885 1.28 pooka _PROP_ATOMIC_DEC32_NV(&po->po_refcnt, ocnt);
1886 1.24 pooka ocnt++;
1887 1.24 pooka _PROP_ASSERT(ocnt != 0);
1888 1.15 joerg
1889 1.15 joerg if (ocnt != 1) {
1890 1.15 joerg ret = 0;
1891 1.23 haad if (unlock != NULL)
1892 1.23 haad unlock();
1893 1.15 joerg break;
1894 1.15 joerg }
1895 1.32 riastrad
1896 1.23 haad ret = (po->po_type->pot_free)(&stack, &obj);
1897 1.15 joerg
1898 1.23 haad if (unlock != NULL)
1899 1.23 haad unlock();
1900 1.15 joerg
1901 1.15 joerg if (ret == _PROP_OBJECT_FREE_DONE)
1902 1.15 joerg break;
1903 1.32 riastrad
1904 1.28 pooka _PROP_ATOMIC_INC32(&po->po_refcnt);
1905 1.15 joerg } while (ret == _PROP_OBJECT_FREE_RECURSE);
1906 1.15 joerg if (ret == _PROP_OBJECT_FREE_FAILED)
1907 1.15 joerg prop_object_release_emergency(obj);
1908 1.16 joerg } while (_prop_stack_pop(&stack, &obj, NULL, NULL, NULL));
1909 1.1 thorpej }
1910 1.1 thorpej
1911 1.1 thorpej /*
1912 1.1 thorpej * prop_object_type --
1913 1.1 thorpej * Return the type of an object.
1914 1.1 thorpej */
1915 1.1 thorpej prop_type_t
1916 1.1 thorpej prop_object_type(prop_object_t obj)
1917 1.1 thorpej {
1918 1.1 thorpej struct _prop_object *po = obj;
1919 1.1 thorpej
1920 1.4 thorpej if (obj == NULL)
1921 1.4 thorpej return (PROP_TYPE_UNKNOWN);
1922 1.4 thorpej
1923 1.2 thorpej return (po->po_type->pot_type);
1924 1.2 thorpej }
1925 1.2 thorpej
1926 1.2 thorpej /*
1927 1.2 thorpej * prop_object_equals --
1928 1.14 thorpej * Returns true if thw two objects are equivalent.
1929 1.2 thorpej */
1930 1.14 thorpej bool
1931 1.2 thorpej prop_object_equals(prop_object_t obj1, prop_object_t obj2)
1932 1.2 thorpej {
1933 1.16 joerg return (prop_object_equals_with_error(obj1, obj2, NULL));
1934 1.16 joerg }
1935 1.16 joerg
1936 1.16 joerg bool
1937 1.16 joerg prop_object_equals_with_error(prop_object_t obj1, prop_object_t obj2,
1938 1.16 joerg bool *error_flag)
1939 1.16 joerg {
1940 1.16 joerg struct _prop_object *po1;
1941 1.16 joerg struct _prop_object *po2;
1942 1.16 joerg void *stored_pointer1, *stored_pointer2;
1943 1.16 joerg prop_object_t next_obj1, next_obj2;
1944 1.16 joerg struct _prop_stack stack;
1945 1.22 thorpej _prop_object_equals_rv_t ret;
1946 1.16 joerg
1947 1.16 joerg _prop_stack_init(&stack);
1948 1.16 joerg if (error_flag)
1949 1.16 joerg *error_flag = false;
1950 1.16 joerg
1951 1.16 joerg start_subtree:
1952 1.16 joerg stored_pointer1 = NULL;
1953 1.16 joerg stored_pointer2 = NULL;
1954 1.16 joerg po1 = obj1;
1955 1.16 joerg po2 = obj2;
1956 1.2 thorpej
1957 1.2 thorpej if (po1->po_type != po2->po_type)
1958 1.14 thorpej return (false);
1959 1.32 riastrad
1960 1.16 joerg continue_subtree:
1961 1.22 thorpej ret = (*po1->po_type->pot_equals)(obj1, obj2,
1962 1.22 thorpej &stored_pointer1, &stored_pointer2,
1963 1.22 thorpej &next_obj1, &next_obj2);
1964 1.16 joerg if (ret == _PROP_OBJECT_EQUALS_FALSE)
1965 1.16 joerg goto finish;
1966 1.16 joerg if (ret == _PROP_OBJECT_EQUALS_TRUE) {
1967 1.16 joerg if (!_prop_stack_pop(&stack, &obj1, &obj2,
1968 1.16 joerg &stored_pointer1, &stored_pointer2))
1969 1.16 joerg return true;
1970 1.27 martin po1 = obj1;
1971 1.27 martin po2 = obj2;
1972 1.16 joerg goto continue_subtree;
1973 1.16 joerg }
1974 1.16 joerg _PROP_ASSERT(ret == _PROP_OBJECT_EQUALS_RECURSE);
1975 1.16 joerg
1976 1.16 joerg if (!_prop_stack_push(&stack, obj1, obj2,
1977 1.16 joerg stored_pointer1, stored_pointer2)) {
1978 1.16 joerg if (error_flag)
1979 1.16 joerg *error_flag = true;
1980 1.16 joerg goto finish;
1981 1.16 joerg }
1982 1.16 joerg obj1 = next_obj1;
1983 1.16 joerg obj2 = next_obj2;
1984 1.16 joerg goto start_subtree;
1985 1.16 joerg
1986 1.16 joerg finish:
1987 1.16 joerg while (_prop_stack_pop(&stack, &obj1, &obj2, NULL, NULL)) {
1988 1.16 joerg po1 = obj1;
1989 1.16 joerg (*po1->po_type->pot_equals_finish)(obj1, obj2);
1990 1.16 joerg }
1991 1.32 riastrad return (false);
1992 1.1 thorpej }
1993 1.1 thorpej
1994 1.1 thorpej /*
1995 1.1 thorpej * prop_object_iterator_next --
1996 1.1 thorpej * Return the next item during an iteration.
1997 1.1 thorpej */
1998 1.1 thorpej prop_object_t
1999 1.1 thorpej prop_object_iterator_next(prop_object_iterator_t pi)
2000 1.1 thorpej {
2001 1.1 thorpej
2002 1.1 thorpej return ((*pi->pi_next_object)(pi));
2003 1.1 thorpej }
2004 1.1 thorpej
2005 1.1 thorpej /*
2006 1.1 thorpej * prop_object_iterator_reset --
2007 1.1 thorpej * Reset the iterator to the first object so as to restart
2008 1.1 thorpej * iteration.
2009 1.1 thorpej */
2010 1.1 thorpej void
2011 1.1 thorpej prop_object_iterator_reset(prop_object_iterator_t pi)
2012 1.1 thorpej {
2013 1.1 thorpej
2014 1.1 thorpej (*pi->pi_reset)(pi);
2015 1.1 thorpej }
2016 1.1 thorpej
2017 1.1 thorpej /*
2018 1.1 thorpej * prop_object_iterator_release --
2019 1.1 thorpej * Release the object iterator.
2020 1.1 thorpej */
2021 1.1 thorpej void
2022 1.1 thorpej prop_object_iterator_release(prop_object_iterator_t pi)
2023 1.1 thorpej {
2024 1.1 thorpej
2025 1.1 thorpej prop_object_release(pi->pi_obj);
2026 1.1 thorpej _PROP_FREE(pi, M_TEMP);
2027 1.1 thorpej }
2028