prop_extern.c revision 1.2.4.2 1 1.2.4.2 perseant /* $NetBSD: prop_extern.c,v 1.2.4.2 2025/08/02 05:18:35 perseant Exp $ */
2 1.2.4.2 perseant
3 1.2.4.2 perseant /*-
4 1.2.4.2 perseant * Copyright (c) 2006, 2007, 2025 The NetBSD Foundation, Inc.
5 1.2.4.2 perseant * All rights reserved.
6 1.2.4.2 perseant *
7 1.2.4.2 perseant * This code is derived from software contributed to The NetBSD Foundation
8 1.2.4.2 perseant * by Jason R. Thorpe.
9 1.2.4.2 perseant *
10 1.2.4.2 perseant * Redistribution and use in source and binary forms, with or without
11 1.2.4.2 perseant * modification, are permitted provided that the following conditions
12 1.2.4.2 perseant * are met:
13 1.2.4.2 perseant * 1. Redistributions of source code must retain the above copyright
14 1.2.4.2 perseant * notice, this list of conditions and the following disclaimer.
15 1.2.4.2 perseant * 2. Redistributions in binary form must reproduce the above copyright
16 1.2.4.2 perseant * notice, this list of conditions and the following disclaimer in the
17 1.2.4.2 perseant * documentation and/or other materials provided with the distribution.
18 1.2.4.2 perseant *
19 1.2.4.2 perseant * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.2.4.2 perseant * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.2.4.2 perseant * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.2.4.2 perseant * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.2.4.2 perseant * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.2.4.2 perseant * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.2.4.2 perseant * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.2.4.2 perseant * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.2.4.2 perseant * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.2.4.2 perseant * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.2.4.2 perseant * POSSIBILITY OF SUCH DAMAGE.
30 1.2.4.2 perseant */
31 1.2.4.2 perseant
32 1.2.4.2 perseant #include "prop_object_impl.h"
33 1.2.4.2 perseant #include <prop/prop_object.h>
34 1.2.4.2 perseant
35 1.2.4.2 perseant #if !defined(_KERNEL) && !defined(_STANDALONE)
36 1.2.4.2 perseant #include <sys/stat.h>
37 1.2.4.2 perseant #include <errno.h>
38 1.2.4.2 perseant #include <fcntl.h>
39 1.2.4.2 perseant #include <limits.h>
40 1.2.4.2 perseant #include <unistd.h>
41 1.2.4.2 perseant #endif /* !_KERNEL && !_STANDALONE */
42 1.2.4.2 perseant
43 1.2.4.2 perseant #define BUF_EXPAND 256
44 1.2.4.2 perseant #define PLISTTMP "/.plistXXXXXX"
45 1.2.4.2 perseant
46 1.2.4.2 perseant static prop_format_t _prop_format_default = PROP_FORMAT_XML;
47 1.2.4.2 perseant
48 1.2.4.2 perseant /*
49 1.2.4.2 perseant * _prop_extern_append_char --
50 1.2.4.2 perseant * Append a single character to the externalize buffer.
51 1.2.4.2 perseant */
52 1.2.4.2 perseant bool
53 1.2.4.2 perseant _prop_extern_append_char(
54 1.2.4.2 perseant struct _prop_object_externalize_context *ctx, unsigned char c)
55 1.2.4.2 perseant {
56 1.2.4.2 perseant
57 1.2.4.2 perseant _PROP_ASSERT(ctx->poec_capacity != 0);
58 1.2.4.2 perseant _PROP_ASSERT(ctx->poec_buf != NULL);
59 1.2.4.2 perseant _PROP_ASSERT(ctx->poec_len <= ctx->poec_capacity);
60 1.2.4.2 perseant
61 1.2.4.2 perseant if (ctx->poec_len == ctx->poec_capacity) {
62 1.2.4.2 perseant char *cp = _PROP_REALLOC(ctx->poec_buf,
63 1.2.4.2 perseant ctx->poec_capacity + BUF_EXPAND,
64 1.2.4.2 perseant M_TEMP);
65 1.2.4.2 perseant if (cp == NULL) {
66 1.2.4.2 perseant return false;
67 1.2.4.2 perseant }
68 1.2.4.2 perseant ctx->poec_capacity = ctx->poec_capacity + BUF_EXPAND;
69 1.2.4.2 perseant ctx->poec_buf = cp;
70 1.2.4.2 perseant }
71 1.2.4.2 perseant
72 1.2.4.2 perseant ctx->poec_buf[ctx->poec_len++] = c;
73 1.2.4.2 perseant
74 1.2.4.2 perseant return true;
75 1.2.4.2 perseant }
76 1.2.4.2 perseant
77 1.2.4.2 perseant /*
78 1.2.4.2 perseant * _prop_extern_append_cstring --
79 1.2.4.2 perseant * Append a C string to the externalize buffer.
80 1.2.4.2 perseant */
81 1.2.4.2 perseant bool
82 1.2.4.2 perseant _prop_extern_append_cstring(
83 1.2.4.2 perseant struct _prop_object_externalize_context *ctx, const char *cp)
84 1.2.4.2 perseant {
85 1.2.4.2 perseant
86 1.2.4.2 perseant while (*cp != '\0') {
87 1.2.4.2 perseant if (_prop_extern_append_char(ctx,
88 1.2.4.2 perseant (unsigned char)*cp) == false) {
89 1.2.4.2 perseant return false;
90 1.2.4.2 perseant }
91 1.2.4.2 perseant cp++;
92 1.2.4.2 perseant }
93 1.2.4.2 perseant return true;
94 1.2.4.2 perseant }
95 1.2.4.2 perseant
96 1.2.4.2 perseant /*
97 1.2.4.2 perseant * _prop_json_extern_append_encoded_cstring --
98 1.2.4.2 perseant * Append a C string to the externalize buffer, JSON-encoded.
99 1.2.4.2 perseant */
100 1.2.4.2 perseant static bool
101 1.2.4.2 perseant _prop_json_extern_append_escu(
102 1.2.4.2 perseant struct _prop_object_externalize_context *ctx, uint16_t val)
103 1.2.4.2 perseant {
104 1.2.4.2 perseant char tmpstr[sizeof("\\uXXXX")];
105 1.2.4.2 perseant
106 1.2.4.2 perseant snprintf(tmpstr, sizeof(tmpstr), "\\u%04X", val);
107 1.2.4.2 perseant return _prop_extern_append_cstring(ctx, tmpstr);
108 1.2.4.2 perseant }
109 1.2.4.2 perseant
110 1.2.4.2 perseant static bool
111 1.2.4.2 perseant _prop_json_extern_append_encoded_cstring(
112 1.2.4.2 perseant struct _prop_object_externalize_context *ctx, const char *cp)
113 1.2.4.2 perseant {
114 1.2.4.2 perseant bool esc;
115 1.2.4.2 perseant unsigned char ch;
116 1.2.4.2 perseant
117 1.2.4.2 perseant for (; (ch = *cp) != '\0'; cp++) {
118 1.2.4.2 perseant esc = true;
119 1.2.4.2 perseant switch (ch) {
120 1.2.4.2 perseant /*
121 1.2.4.2 perseant * First, the two explicit exclusions. They must be
122 1.2.4.2 perseant * escaped.
123 1.2.4.2 perseant */
124 1.2.4.2 perseant case '"': /* U+0022 quotation mark */
125 1.2.4.2 perseant goto emit;
126 1.2.4.2 perseant
127 1.2.4.2 perseant case '\\': /* U+005C reverse solidus */
128 1.2.4.2 perseant goto emit;
129 1.2.4.2 perseant
130 1.2.4.2 perseant /*
131 1.2.4.2 perseant * And some special cases that are explcit in the grammar.
132 1.2.4.2 perseant */
133 1.2.4.2 perseant case '/': /* U+002F solidus (XXX this one seems silly) */
134 1.2.4.2 perseant goto emit;
135 1.2.4.2 perseant
136 1.2.4.2 perseant case 0x08: /* U+0008 backspace */
137 1.2.4.2 perseant ch = 'b';
138 1.2.4.2 perseant goto emit;
139 1.2.4.2 perseant
140 1.2.4.2 perseant case 0x0c: /* U+000C form feed */
141 1.2.4.2 perseant ch = 'f';
142 1.2.4.2 perseant goto emit;
143 1.2.4.2 perseant
144 1.2.4.2 perseant case 0x0a: /* U+000A line feed */
145 1.2.4.2 perseant ch = 'n';
146 1.2.4.2 perseant goto emit;
147 1.2.4.2 perseant
148 1.2.4.2 perseant case 0x0d: /* U+000D carriage return */
149 1.2.4.2 perseant ch = 'r';
150 1.2.4.2 perseant goto emit;
151 1.2.4.2 perseant
152 1.2.4.2 perseant case 0x09: /* U+0009 tab */
153 1.2.4.2 perseant ch = 't';
154 1.2.4.2 perseant goto emit;
155 1.2.4.2 perseant
156 1.2.4.2 perseant default:
157 1.2.4.2 perseant /*
158 1.2.4.2 perseant * \u-escape all other single-byte ASCII control
159 1.2.4.2 perseant * characters, per RFC 8259:
160 1.2.4.2 perseant *
161 1.2.4.2 perseant * <quote>
162 1.2.4.2 perseant * All Unicode characters may be placed within the
163 1.2.4.2 perseant * quotation marks, except for the characters that
164 1.2.4.2 perseant * MUST be escaped: quotation mark, reverse solidus,
165 1.2.4.2 perseant * and the control characters (U+0000 through U+001F).
166 1.2.4.2 perseant * </quote>
167 1.2.4.2 perseant */
168 1.2.4.2 perseant if (ch < 0x20) {
169 1.2.4.2 perseant if (_prop_json_extern_append_escu(ctx,
170 1.2.4.2 perseant ch) == false) {
171 1.2.4.2 perseant return false;
172 1.2.4.2 perseant }
173 1.2.4.2 perseant break;
174 1.2.4.2 perseant }
175 1.2.4.2 perseant /*
176 1.2.4.2 perseant * We're going to just treat everything else like
177 1.2.4.2 perseant * UTF-8 (we've been handed a C-string, after all)
178 1.2.4.2 perseant * and pretend it will be OK.
179 1.2.4.2 perseant */
180 1.2.4.2 perseant esc = false;
181 1.2.4.2 perseant emit:
182 1.2.4.2 perseant if ((esc && _prop_extern_append_char(ctx,
183 1.2.4.2 perseant '\\') == false) ||
184 1.2.4.2 perseant _prop_extern_append_char(ctx, ch) == false) {
185 1.2.4.2 perseant return false;
186 1.2.4.2 perseant }
187 1.2.4.2 perseant break;
188 1.2.4.2 perseant }
189 1.2.4.2 perseant }
190 1.2.4.2 perseant
191 1.2.4.2 perseant return true;
192 1.2.4.2 perseant }
193 1.2.4.2 perseant
194 1.2.4.2 perseant /*
195 1.2.4.2 perseant * _prop_xml_extern_append_encoded_cstring --
196 1.2.4.2 perseant * Append a C string to the externalize buffer, XML-encoded.
197 1.2.4.2 perseant */
198 1.2.4.2 perseant static bool
199 1.2.4.2 perseant _prop_xml_extern_append_encoded_cstring(
200 1.2.4.2 perseant struct _prop_object_externalize_context *ctx, const char *cp)
201 1.2.4.2 perseant {
202 1.2.4.2 perseant bool rv;
203 1.2.4.2 perseant unsigned char ch;
204 1.2.4.2 perseant
205 1.2.4.2 perseant for (rv = true; rv && (ch = *cp) != '\0'; cp++) {
206 1.2.4.2 perseant switch (ch) {
207 1.2.4.2 perseant case '<':
208 1.2.4.2 perseant rv = _prop_extern_append_cstring(ctx, "<");
209 1.2.4.2 perseant break;
210 1.2.4.2 perseant case '>':
211 1.2.4.2 perseant rv = _prop_extern_append_cstring(ctx, ">");
212 1.2.4.2 perseant break;
213 1.2.4.2 perseant case '&':
214 1.2.4.2 perseant rv = _prop_extern_append_cstring(ctx, "&");
215 1.2.4.2 perseant break;
216 1.2.4.2 perseant default:
217 1.2.4.2 perseant rv = _prop_extern_append_char(ctx, ch);
218 1.2.4.2 perseant break;
219 1.2.4.2 perseant }
220 1.2.4.2 perseant }
221 1.2.4.2 perseant
222 1.2.4.2 perseant return rv;
223 1.2.4.2 perseant }
224 1.2.4.2 perseant
225 1.2.4.2 perseant /*
226 1.2.4.2 perseant * _prop_extern_append_encoded_cstring --
227 1.2.4.2 perseant * Append a C string to the externalize buffer, encoding it for
228 1.2.4.2 perseant * the selected format.
229 1.2.4.2 perseant */
230 1.2.4.2 perseant bool
231 1.2.4.2 perseant _prop_extern_append_encoded_cstring(
232 1.2.4.2 perseant struct _prop_object_externalize_context *ctx, const char *cp)
233 1.2.4.2 perseant {
234 1.2.4.2 perseant _PROP_ASSERT(ctx->poec_format == PROP_FORMAT_XML ||
235 1.2.4.2 perseant ctx->poec_format == PROP_FORMAT_JSON);
236 1.2.4.2 perseant
237 1.2.4.2 perseant switch (ctx->poec_format) {
238 1.2.4.2 perseant case PROP_FORMAT_JSON:
239 1.2.4.2 perseant return _prop_json_extern_append_encoded_cstring(ctx, cp);
240 1.2.4.2 perseant
241 1.2.4.2 perseant default: /* PROP_FORMAT_XML */
242 1.2.4.2 perseant return _prop_xml_extern_append_encoded_cstring(ctx, cp);
243 1.2.4.2 perseant }
244 1.2.4.2 perseant }
245 1.2.4.2 perseant
246 1.2.4.2 perseant /*
247 1.2.4.2 perseant * _prop_extern_start_line --
248 1.2.4.2 perseant * Append the start-of-line character sequence.
249 1.2.4.2 perseant */
250 1.2.4.2 perseant bool
251 1.2.4.2 perseant _prop_extern_start_line(
252 1.2.4.2 perseant struct _prop_object_externalize_context *ctx)
253 1.2.4.2 perseant {
254 1.2.4.2 perseant unsigned int i;
255 1.2.4.2 perseant
256 1.2.4.2 perseant for (i = 0; i < ctx->poec_depth; i++) {
257 1.2.4.2 perseant if (_prop_extern_append_char(ctx, '\t') == false) {
258 1.2.4.2 perseant return false;
259 1.2.4.2 perseant }
260 1.2.4.2 perseant }
261 1.2.4.2 perseant return true;
262 1.2.4.2 perseant }
263 1.2.4.2 perseant
264 1.2.4.2 perseant /*
265 1.2.4.2 perseant * _prop_extern_end_line --
266 1.2.4.2 perseant * Append the end-of-line character sequence.
267 1.2.4.2 perseant */
268 1.2.4.2 perseant bool
269 1.2.4.2 perseant _prop_extern_end_line(
270 1.2.4.2 perseant struct _prop_object_externalize_context *ctx, const char *trailer)
271 1.2.4.2 perseant {
272 1.2.4.2 perseant if (trailer != NULL &&
273 1.2.4.2 perseant _prop_extern_append_cstring(ctx, trailer) == false) {
274 1.2.4.2 perseant return false;
275 1.2.4.2 perseant }
276 1.2.4.2 perseant return _prop_extern_append_char(ctx, '\n');
277 1.2.4.2 perseant }
278 1.2.4.2 perseant
279 1.2.4.2 perseant /*
280 1.2.4.2 perseant * _prop_extern_append_start_tag --
281 1.2.4.2 perseant * Append an item's start tag to the externalize buffer.
282 1.2.4.2 perseant */
283 1.2.4.2 perseant bool
284 1.2.4.2 perseant _prop_extern_append_start_tag(
285 1.2.4.2 perseant struct _prop_object_externalize_context *ctx,
286 1.2.4.2 perseant const struct _prop_object_type_tags *tags,
287 1.2.4.2 perseant const char *tagattrs)
288 1.2.4.2 perseant {
289 1.2.4.2 perseant bool rv;
290 1.2.4.2 perseant
291 1.2.4.2 perseant _PROP_ASSERT(ctx->poec_format == PROP_FORMAT_XML ||
292 1.2.4.2 perseant ctx->poec_format == PROP_FORMAT_JSON);
293 1.2.4.2 perseant
294 1.2.4.2 perseant switch (ctx->poec_format) {
295 1.2.4.2 perseant case PROP_FORMAT_JSON:
296 1.2.4.2 perseant rv = tags->json_open_tag == NULL ||
297 1.2.4.2 perseant _prop_extern_append_cstring(ctx, tags->json_open_tag);
298 1.2.4.2 perseant break;
299 1.2.4.2 perseant
300 1.2.4.2 perseant default: /* PROP_FORMAT_XML */
301 1.2.4.2 perseant rv = _prop_extern_append_char(ctx, '<') &&
302 1.2.4.2 perseant _prop_extern_append_cstring(ctx, tags->xml_tag) &&
303 1.2.4.2 perseant (tagattrs == NULL ||
304 1.2.4.2 perseant (_prop_extern_append_char(ctx, ' ') &&
305 1.2.4.2 perseant _prop_extern_append_cstring(ctx, tagattrs))) &&
306 1.2.4.2 perseant _prop_extern_append_char(ctx, '>');
307 1.2.4.2 perseant break;
308 1.2.4.2 perseant }
309 1.2.4.2 perseant
310 1.2.4.2 perseant return rv;
311 1.2.4.2 perseant }
312 1.2.4.2 perseant
313 1.2.4.2 perseant /*
314 1.2.4.2 perseant * _prop_extern_append_end_tag --
315 1.2.4.2 perseant * Append an item's end tag to the externalize buffer.
316 1.2.4.2 perseant */
317 1.2.4.2 perseant bool
318 1.2.4.2 perseant _prop_extern_append_end_tag(
319 1.2.4.2 perseant struct _prop_object_externalize_context *ctx,
320 1.2.4.2 perseant const struct _prop_object_type_tags *tags)
321 1.2.4.2 perseant {
322 1.2.4.2 perseant bool rv;
323 1.2.4.2 perseant
324 1.2.4.2 perseant _PROP_ASSERT(ctx->poec_format == PROP_FORMAT_XML ||
325 1.2.4.2 perseant ctx->poec_format == PROP_FORMAT_JSON);
326 1.2.4.2 perseant
327 1.2.4.2 perseant switch (ctx->poec_format) {
328 1.2.4.2 perseant case PROP_FORMAT_JSON:
329 1.2.4.2 perseant rv = tags->json_close_tag == NULL ||
330 1.2.4.2 perseant _prop_extern_append_cstring(ctx, tags->json_close_tag);
331 1.2.4.2 perseant break;
332 1.2.4.2 perseant
333 1.2.4.2 perseant default: /* PROP_FORMAT_XML */
334 1.2.4.2 perseant rv = _prop_extern_append_char(ctx, '<') &&
335 1.2.4.2 perseant _prop_extern_append_char(ctx, '/') &&
336 1.2.4.2 perseant _prop_extern_append_cstring(ctx, tags->xml_tag) &&
337 1.2.4.2 perseant _prop_extern_append_char(ctx, '>');
338 1.2.4.2 perseant break;
339 1.2.4.2 perseant }
340 1.2.4.2 perseant
341 1.2.4.2 perseant return rv;
342 1.2.4.2 perseant }
343 1.2.4.2 perseant
344 1.2.4.2 perseant /*
345 1.2.4.2 perseant * _prop_extern_append_empty_tag --
346 1.2.4.2 perseant * Append an item's empty tag to the externalize buffer.
347 1.2.4.2 perseant */
348 1.2.4.2 perseant bool
349 1.2.4.2 perseant _prop_extern_append_empty_tag(
350 1.2.4.2 perseant struct _prop_object_externalize_context *ctx,
351 1.2.4.2 perseant const struct _prop_object_type_tags *tags)
352 1.2.4.2 perseant {
353 1.2.4.2 perseant bool rv;
354 1.2.4.2 perseant
355 1.2.4.2 perseant _PROP_ASSERT(ctx->poec_format == PROP_FORMAT_XML ||
356 1.2.4.2 perseant ctx->poec_format == PROP_FORMAT_JSON);
357 1.2.4.2 perseant
358 1.2.4.2 perseant switch (ctx->poec_format) {
359 1.2.4.2 perseant case PROP_FORMAT_JSON:
360 1.2.4.2 perseant if (tags->json_open_tag == NULL ||
361 1.2.4.2 perseant _prop_extern_append_cstring(ctx,
362 1.2.4.2 perseant tags->json_open_tag) == false) {
363 1.2.4.2 perseant return false;
364 1.2.4.2 perseant }
365 1.2.4.2 perseant if (tags->json_empty_sep != NULL &&
366 1.2.4.2 perseant _prop_extern_append_cstring(ctx,
367 1.2.4.2 perseant tags->json_empty_sep) == false) {
368 1.2.4.2 perseant return false;
369 1.2.4.2 perseant }
370 1.2.4.2 perseant if (tags->json_close_tag != NULL) {
371 1.2.4.2 perseant rv = _prop_extern_append_cstring(ctx,
372 1.2.4.2 perseant tags->json_close_tag);
373 1.2.4.2 perseant } else {
374 1.2.4.2 perseant rv = true;
375 1.2.4.2 perseant }
376 1.2.4.2 perseant break;
377 1.2.4.2 perseant
378 1.2.4.2 perseant default: /* PROP_FORMAT_XML */
379 1.2.4.2 perseant rv = _prop_extern_append_char(ctx, '<') &&
380 1.2.4.2 perseant _prop_extern_append_cstring(ctx, tags->xml_tag) &&
381 1.2.4.2 perseant _prop_extern_append_char(ctx, '/') &&
382 1.2.4.2 perseant _prop_extern_append_char(ctx, '>');
383 1.2.4.2 perseant break;
384 1.2.4.2 perseant }
385 1.2.4.2 perseant
386 1.2.4.2 perseant return rv;
387 1.2.4.2 perseant }
388 1.2.4.2 perseant
389 1.2.4.2 perseant static const struct _prop_object_type_tags _plist_type_tags = {
390 1.2.4.2 perseant .xml_tag = "plist",
391 1.2.4.2 perseant };
392 1.2.4.2 perseant
393 1.2.4.2 perseant /*
394 1.2.4.2 perseant * _prop_extern_append_header --
395 1.2.4.2 perseant * Append the header to the externalize buffer.
396 1.2.4.2 perseant */
397 1.2.4.2 perseant static bool
398 1.2.4.2 perseant _prop_extern_append_header(struct _prop_object_externalize_context *ctx)
399 1.2.4.2 perseant {
400 1.2.4.2 perseant static const char _plist_xml_header[] =
401 1.2.4.2 perseant "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
402 1.2.4.2 perseant "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n";
403 1.2.4.2 perseant
404 1.2.4.2 perseant if (ctx->poec_format != PROP_FORMAT_XML) {
405 1.2.4.2 perseant return true;
406 1.2.4.2 perseant }
407 1.2.4.2 perseant
408 1.2.4.2 perseant if (_prop_extern_append_cstring(ctx, _plist_xml_header) == false ||
409 1.2.4.2 perseant _prop_extern_append_start_tag(ctx,
410 1.2.4.2 perseant &_plist_type_tags,
411 1.2.4.2 perseant "version=\"1.0\"") == false ||
412 1.2.4.2 perseant _prop_extern_append_char(ctx, '\n') == false) {
413 1.2.4.2 perseant return false;
414 1.2.4.2 perseant }
415 1.2.4.2 perseant
416 1.2.4.2 perseant return true;
417 1.2.4.2 perseant }
418 1.2.4.2 perseant
419 1.2.4.2 perseant /*
420 1.2.4.2 perseant * _prop_extern_append_footer --
421 1.2.4.2 perseant * Append the footer to the externalize buffer. This also
422 1.2.4.2 perseant * NUL-terminates the buffer.
423 1.2.4.2 perseant */
424 1.2.4.2 perseant static bool
425 1.2.4.2 perseant _prop_extern_append_footer(struct _prop_object_externalize_context *ctx)
426 1.2.4.2 perseant {
427 1.2.4.2 perseant if (_prop_extern_end_line(ctx, NULL) == false) {
428 1.2.4.2 perseant return false;
429 1.2.4.2 perseant }
430 1.2.4.2 perseant
431 1.2.4.2 perseant if (ctx->poec_format == PROP_FORMAT_XML) {
432 1.2.4.2 perseant if (_prop_extern_append_end_tag(ctx,
433 1.2.4.2 perseant &_plist_type_tags) == false ||
434 1.2.4.2 perseant _prop_extern_end_line(ctx, NULL) == false) {
435 1.2.4.2 perseant return false;
436 1.2.4.2 perseant }
437 1.2.4.2 perseant }
438 1.2.4.2 perseant
439 1.2.4.2 perseant return _prop_extern_append_char(ctx, '\0');
440 1.2.4.2 perseant }
441 1.2.4.2 perseant
442 1.2.4.2 perseant /*
443 1.2.4.2 perseant * _prop_extern_context_alloc --
444 1.2.4.2 perseant * Allocate an externalize context.
445 1.2.4.2 perseant */
446 1.2.4.2 perseant static struct _prop_object_externalize_context *
447 1.2.4.2 perseant _prop_extern_context_alloc(prop_format_t fmt)
448 1.2.4.2 perseant {
449 1.2.4.2 perseant struct _prop_object_externalize_context *ctx;
450 1.2.4.2 perseant
451 1.2.4.2 perseant ctx = _PROP_MALLOC(sizeof(*ctx), M_TEMP);
452 1.2.4.2 perseant if (ctx != NULL) {
453 1.2.4.2 perseant ctx->poec_buf = _PROP_MALLOC(BUF_EXPAND, M_TEMP);
454 1.2.4.2 perseant if (ctx->poec_buf == NULL) {
455 1.2.4.2 perseant _PROP_FREE(ctx, M_TEMP);
456 1.2.4.2 perseant return NULL;
457 1.2.4.2 perseant }
458 1.2.4.2 perseant ctx->poec_len = 0;
459 1.2.4.2 perseant ctx->poec_capacity = BUF_EXPAND;
460 1.2.4.2 perseant ctx->poec_depth = 0;
461 1.2.4.2 perseant ctx->poec_format = fmt;
462 1.2.4.2 perseant }
463 1.2.4.2 perseant return ctx;
464 1.2.4.2 perseant }
465 1.2.4.2 perseant
466 1.2.4.2 perseant /*
467 1.2.4.2 perseant * _prop_extern_context_free --
468 1.2.4.2 perseant * Free an externalize context.
469 1.2.4.2 perseant */
470 1.2.4.2 perseant static void
471 1.2.4.2 perseant _prop_extern_context_free(struct _prop_object_externalize_context *ctx)
472 1.2.4.2 perseant {
473 1.2.4.2 perseant /* Buffer is always freed by the caller. */
474 1.2.4.2 perseant _PROP_FREE(ctx, M_TEMP);
475 1.2.4.2 perseant }
476 1.2.4.2 perseant
477 1.2.4.2 perseant /*
478 1.2.4.2 perseant * _prop_object_externalize --
479 1.2.4.2 perseant * Externalize an object, returning a NUL-terminated buffer
480 1.2.4.2 perseant * containing the serialized data in either XML or JSON format.
481 1.2.4.2 perseant * The buffer is allocated with the M_TEMP memory type.
482 1.2.4.2 perseant */
483 1.2.4.2 perseant char *
484 1.2.4.2 perseant _prop_object_externalize(struct _prop_object *obj, prop_format_t fmt)
485 1.2.4.2 perseant {
486 1.2.4.2 perseant struct _prop_object_externalize_context *ctx;
487 1.2.4.2 perseant char *cp = NULL;
488 1.2.4.2 perseant
489 1.2.4.2 perseant if (obj == NULL || obj->po_type->pot_extern == NULL) {
490 1.2.4.2 perseant return NULL;
491 1.2.4.2 perseant }
492 1.2.4.2 perseant if (fmt != PROP_FORMAT_XML && fmt != PROP_FORMAT_JSON) {
493 1.2.4.2 perseant return NULL;
494 1.2.4.2 perseant }
495 1.2.4.2 perseant
496 1.2.4.2 perseant ctx = _prop_extern_context_alloc(fmt);
497 1.2.4.2 perseant if (ctx == NULL) {
498 1.2.4.2 perseant return NULL;
499 1.2.4.2 perseant }
500 1.2.4.2 perseant
501 1.2.4.2 perseant if (_prop_extern_append_header(ctx) == false ||
502 1.2.4.2 perseant obj->po_type->pot_extern(ctx, obj) == false ||
503 1.2.4.2 perseant _prop_extern_append_footer(ctx) == false) {
504 1.2.4.2 perseant /* We are responsible for releasing the buffer. */
505 1.2.4.2 perseant _PROP_FREE(ctx->poec_buf, M_TEMP);
506 1.2.4.2 perseant goto bad;
507 1.2.4.2 perseant }
508 1.2.4.2 perseant
509 1.2.4.2 perseant cp = ctx->poec_buf;
510 1.2.4.2 perseant bad:
511 1.2.4.2 perseant _prop_extern_context_free(ctx);
512 1.2.4.2 perseant return cp;
513 1.2.4.2 perseant }
514 1.2.4.2 perseant
515 1.2.4.2 perseant #if !defined(_KERNEL) && !defined(_STANDALONE)
516 1.2.4.2 perseant /*
517 1.2.4.2 perseant * _prop_extern_file_dirname --
518 1.2.4.2 perseant * dirname(3), basically. We have to roll our own because the
519 1.2.4.2 perseant * system dirname(3) isn't reentrant.
520 1.2.4.2 perseant */
521 1.2.4.2 perseant static void
522 1.2.4.2 perseant _prop_extern_file_dirname(const char *path, char *result)
523 1.2.4.2 perseant {
524 1.2.4.2 perseant const char *lastp;
525 1.2.4.2 perseant size_t len;
526 1.2.4.2 perseant
527 1.2.4.2 perseant /*
528 1.2.4.2 perseant * If `path' is a NULL pointer or points to an empty string,
529 1.2.4.2 perseant * return ".".
530 1.2.4.2 perseant */
531 1.2.4.2 perseant if (path == NULL || *path == '\0') {
532 1.2.4.2 perseant goto singledot;
533 1.2.4.2 perseant }
534 1.2.4.2 perseant
535 1.2.4.2 perseant /* Strip trailing slashes, if any. */
536 1.2.4.2 perseant lastp = path + strlen(path) - 1;
537 1.2.4.2 perseant while (lastp != path && *lastp == '/') {
538 1.2.4.2 perseant lastp--;
539 1.2.4.2 perseant }
540 1.2.4.2 perseant
541 1.2.4.2 perseant /* Terminate path at the last occurrence of '/'. */
542 1.2.4.2 perseant do {
543 1.2.4.2 perseant if (*lastp == '/') {
544 1.2.4.2 perseant /* Strip trailing slashes, if any. */
545 1.2.4.2 perseant while (lastp != path && *lastp == '/') {
546 1.2.4.2 perseant lastp--;
547 1.2.4.2 perseant }
548 1.2.4.2 perseant
549 1.2.4.2 perseant /* ...and copy the result into the result buffer. */
550 1.2.4.2 perseant len = (lastp - path) + 1 /* last char */;
551 1.2.4.2 perseant if (len > (PATH_MAX - 1)) {
552 1.2.4.2 perseant len = PATH_MAX - 1;
553 1.2.4.2 perseant }
554 1.2.4.2 perseant
555 1.2.4.2 perseant memcpy(result, path, len);
556 1.2.4.2 perseant result[len] = '\0';
557 1.2.4.2 perseant return;
558 1.2.4.2 perseant }
559 1.2.4.2 perseant } while (--lastp >= path);
560 1.2.4.2 perseant
561 1.2.4.2 perseant /* No /'s found, return ".". */
562 1.2.4.2 perseant singledot:
563 1.2.4.2 perseant strcpy(result, ".");
564 1.2.4.2 perseant }
565 1.2.4.2 perseant
566 1.2.4.2 perseant /*
567 1.2.4.2 perseant * _prop_extern_write_file --
568 1.2.4.2 perseant * Write an externalized object to the specified file.
569 1.2.4.2 perseant * The file is written atomically from the caller's perspective,
570 1.2.4.2 perseant * and the mode set to 0666 modified by the caller's umask.
571 1.2.4.2 perseant */
572 1.2.4.2 perseant static bool
573 1.2.4.2 perseant _prop_extern_write_file(const char *fname, const char *data, size_t len)
574 1.2.4.2 perseant {
575 1.2.4.2 perseant char tname_store[PATH_MAX];
576 1.2.4.2 perseant char *tname = NULL;
577 1.2.4.2 perseant int fd = -1;
578 1.2.4.2 perseant int save_errno;
579 1.2.4.2 perseant mode_t myumask;
580 1.2.4.2 perseant bool rv = false;
581 1.2.4.2 perseant
582 1.2.4.2 perseant if (len > SSIZE_MAX) {
583 1.2.4.2 perseant errno = EFBIG;
584 1.2.4.2 perseant return false;
585 1.2.4.2 perseant }
586 1.2.4.2 perseant
587 1.2.4.2 perseant /*
588 1.2.4.2 perseant * Get the directory name where the file is to be written
589 1.2.4.2 perseant * and create the temporary file.
590 1.2.4.2 perseant */
591 1.2.4.2 perseant _prop_extern_file_dirname(fname, tname_store);
592 1.2.4.2 perseant if (strlen(tname_store) + strlen(PLISTTMP) >= sizeof(tname_store)) {
593 1.2.4.2 perseant errno = ENAMETOOLONG;
594 1.2.4.2 perseant return false;
595 1.2.4.2 perseant }
596 1.2.4.2 perseant strcat(tname_store, PLISTTMP);
597 1.2.4.2 perseant
598 1.2.4.2 perseant if ((fd = mkstemp(tname_store)) == -1) {
599 1.2.4.2 perseant return false;
600 1.2.4.2 perseant }
601 1.2.4.2 perseant tname = tname_store;
602 1.2.4.2 perseant
603 1.2.4.2 perseant if (write(fd, data, len) != (ssize_t)len) {
604 1.2.4.2 perseant goto bad;
605 1.2.4.2 perseant }
606 1.2.4.2 perseant
607 1.2.4.2 perseant if (fsync(fd) == -1) {
608 1.2.4.2 perseant goto bad;
609 1.2.4.2 perseant }
610 1.2.4.2 perseant
611 1.2.4.2 perseant myumask = umask(0);
612 1.2.4.2 perseant (void)umask(myumask);
613 1.2.4.2 perseant if (fchmod(fd, 0666 & ~myumask) == -1) {
614 1.2.4.2 perseant goto bad;
615 1.2.4.2 perseant }
616 1.2.4.2 perseant
617 1.2.4.2 perseant if (rename(tname, fname) == -1) {
618 1.2.4.2 perseant goto bad;
619 1.2.4.2 perseant }
620 1.2.4.2 perseant tname = NULL;
621 1.2.4.2 perseant
622 1.2.4.2 perseant rv = true;
623 1.2.4.2 perseant
624 1.2.4.2 perseant bad:
625 1.2.4.2 perseant save_errno = errno;
626 1.2.4.2 perseant if (fd != -1) {
627 1.2.4.2 perseant (void) close(fd);
628 1.2.4.2 perseant }
629 1.2.4.2 perseant if (tname != NULL) {
630 1.2.4.2 perseant (void) unlink(tname);
631 1.2.4.2 perseant }
632 1.2.4.2 perseant errno = save_errno;
633 1.2.4.2 perseant return rv;
634 1.2.4.2 perseant }
635 1.2.4.2 perseant
636 1.2.4.2 perseant /*
637 1.2.4.2 perseant * _prop_object_externalize_to_file --
638 1.2.4.2 perseant * Externalize an object to the specified file.
639 1.2.4.2 perseant */
640 1.2.4.2 perseant bool
641 1.2.4.2 perseant _prop_object_externalize_to_file(struct _prop_object *obj, const char *fname,
642 1.2.4.2 perseant prop_format_t fmt)
643 1.2.4.2 perseant {
644 1.2.4.2 perseant char *data = _prop_object_externalize(obj, fmt);
645 1.2.4.2 perseant if (data == NULL) {
646 1.2.4.2 perseant return false;
647 1.2.4.2 perseant }
648 1.2.4.2 perseant bool rv = _prop_extern_write_file(fname, data, strlen(data));
649 1.2.4.2 perseant int save_errno = errno;
650 1.2.4.2 perseant _PROP_FREE(data, M_TEMP);
651 1.2.4.2 perseant errno = save_errno;
652 1.2.4.2 perseant
653 1.2.4.2 perseant return rv;
654 1.2.4.2 perseant }
655 1.2.4.2 perseant
656 1.2.4.2 perseant /*
657 1.2.4.2 perseant * prop_object_externalize_to_file --
658 1.2.4.2 perseant * Externalize an object to the specifed file in the default format.
659 1.2.4.2 perseant */
660 1.2.4.2 perseant _PROP_EXPORT bool
661 1.2.4.2 perseant prop_object_externalize_to_file(prop_object_t po, const char *fname)
662 1.2.4.2 perseant {
663 1.2.4.2 perseant return _prop_object_externalize_to_file((struct _prop_object *)po,
664 1.2.4.2 perseant fname, _prop_format_default);
665 1.2.4.2 perseant }
666 1.2.4.2 perseant
667 1.2.4.2 perseant /*
668 1.2.4.2 perseant * prop_object_externalize_to_file_with_format --
669 1.2.4.2 perseant * Externalize an object to the specifed file in the specified format.
670 1.2.4.2 perseant */
671 1.2.4.2 perseant _PROP_EXPORT bool
672 1.2.4.2 perseant prop_object_externalize_to_file_with_format(prop_object_t po,
673 1.2.4.2 perseant const char *fname, prop_format_t fmt)
674 1.2.4.2 perseant {
675 1.2.4.2 perseant return _prop_object_externalize_to_file((struct _prop_object *)po,
676 1.2.4.2 perseant fname, fmt);
677 1.2.4.2 perseant }
678 1.2.4.2 perseant #endif /* !_KERNEL && !_STANDALONE */
679 1.2.4.2 perseant
680 1.2.4.2 perseant /*
681 1.2.4.2 perseant * prop_object_externalize --
682 1.2.4.2 perseant * Externalize an object in the default format.
683 1.2.4.2 perseant */
684 1.2.4.2 perseant _PROP_EXPORT char *
685 1.2.4.2 perseant prop_object_externalize(prop_object_t po)
686 1.2.4.2 perseant {
687 1.2.4.2 perseant return _prop_object_externalize((struct _prop_object *)po,
688 1.2.4.2 perseant _prop_format_default);
689 1.2.4.2 perseant }
690 1.2.4.2 perseant
691 1.2.4.2 perseant /*
692 1.2.4.2 perseant * prop_object_externalize_with_format --
693 1.2.4.2 perseant * Externalize an object in the specified format.
694 1.2.4.2 perseant */
695 1.2.4.2 perseant _PROP_EXPORT char *
696 1.2.4.2 perseant prop_object_externalize_with_format(prop_object_t po, prop_format_t fmt)
697 1.2.4.2 perseant {
698 1.2.4.2 perseant return _prop_object_externalize((struct _prop_object *)po, fmt);
699 1.2.4.2 perseant }
700