nrelease - fix/improve livecd
[dragonfly.git] / sys / libprop / prop_data.c
CommitLineData
349fb218
AH
1/* $NetBSD: prop_data.c,v 1.14 2009/01/25 06:59:35 cyber Exp $ */
2
3/*-
4 * Copyright (c) 2006 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
9f95f3e0 32#include <libprop/prop_data.h>
349fb218
AH
33#include "prop_object_impl.h"
34
35#if defined(_KERNEL)
36#include <sys/systm.h>
9f95f3e0 37#include <sys/libkern.h>
349fb218
AH
38#elif defined(_STANDALONE)
39#include <sys/param.h>
40#include <lib/libkern/libkern.h>
41#else
42#include <errno.h>
43#include <limits.h>
44#include <stdlib.h>
45#endif
46
47struct _prop_data {
48 struct _prop_object pd_obj;
49 union {
50 void * pdu_mutable;
51 const void * pdu_immutable;
52 } pd_un;
53#define pd_mutable pd_un.pdu_mutable
54#define pd_immutable pd_un.pdu_immutable
55 size_t pd_size;
56 int pd_flags;
57};
58
59#define PD_F_NOCOPY 0x01
60
f3f3eadb 61_PROP_POOL_INIT(_prop_data_pool, sizeof(struct _prop_data), "propdata");
349fb218
AH
62
63_PROP_MALLOC_DEFINE(M_PROP_DATA, "prop data",
64 "property data container object")
65
66static _prop_object_free_rv_t
67 _prop_data_free(prop_stack_t, prop_object_t *);
68static bool _prop_data_externalize(
69 struct _prop_object_externalize_context *,
70 void *);
71static _prop_object_equals_rv_t
72 _prop_data_equals(prop_object_t, prop_object_t,
73 void **, void **,
74 prop_object_t *, prop_object_t *);
75
76static const struct _prop_object_type _prop_object_type_data = {
77 .pot_type = PROP_TYPE_DATA,
78 .pot_free = _prop_data_free,
79 .pot_extern = _prop_data_externalize,
80 .pot_equals = _prop_data_equals,
81};
82
83#define prop_object_is_data(x) \
84 ((x) != NULL && (x)->pd_obj.po_type == &_prop_object_type_data)
85
86/* ARGSUSED */
87static _prop_object_free_rv_t
88_prop_data_free(prop_stack_t stack, prop_object_t *obj)
89{
90 prop_data_t pd = *obj;
91
92 if ((pd->pd_flags & PD_F_NOCOPY) == 0 && pd->pd_mutable != NULL)
9f95f3e0 93 _PROP_FREE(pd->pd_mutable, M_PROP_DATA);
349fb218
AH
94 _PROP_POOL_PUT(_prop_data_pool, pd);
95
96 return (_PROP_OBJECT_FREE_DONE);
97}
98
99static const char _prop_data_base64[] =
100 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
101static const char _prop_data_pad64 = '=';
102
103static bool
104_prop_data_externalize(struct _prop_object_externalize_context *ctx, void *v)
105{
106 prop_data_t pd = v;
107 size_t i, srclen;
108 const uint8_t *src;
109 uint8_t output[4];
110 uint8_t input[3];
111
112 if (pd->pd_size == 0)
113 return (_prop_object_externalize_empty_tag(ctx, "data"));
114
115 if (_prop_object_externalize_start_tag(ctx, "data") == false)
116 return (false);
117
118 for (src = pd->pd_immutable, srclen = pd->pd_size;
119 srclen > 2; srclen -= 3) {
120 input[0] = *src++;
121 input[1] = *src++;
122 input[2] = *src++;
123
124 output[0] = (uint32_t)input[0] >> 2;
125 output[1] = ((uint32_t)(input[0] & 0x03) << 4) +
126 ((uint32_t)input[1] >> 4);
127 output[2] = ((uint32_t)(input[1] & 0x0f) << 2) +
128 ((uint32_t)input[2] >> 6);
129 output[3] = input[2] & 0x3f;
130 _PROP_ASSERT(output[0] < 64);
131 _PROP_ASSERT(output[1] < 64);
132 _PROP_ASSERT(output[2] < 64);
133 _PROP_ASSERT(output[3] < 64);
134
135 if (_prop_object_externalize_append_char(ctx,
136 _prop_data_base64[output[0]]) == false ||
137 _prop_object_externalize_append_char(ctx,
9f95f3e0 138 _prop_data_base64[output[1]]) == false ||
349fb218 139 _prop_object_externalize_append_char(ctx,
9f95f3e0 140 _prop_data_base64[output[2]]) == false ||
349fb218 141 _prop_object_externalize_append_char(ctx,
9f95f3e0 142 _prop_data_base64[output[3]]) == false)
349fb218
AH
143 return (false);
144 }
145
146 if (srclen != 0) {
147 input[0] = input[1] = input[2] = '\0';
148 for (i = 0; i < srclen; i++)
149 input[i] = *src++;
150
151 output[0] = (uint32_t)input[0] >> 2;
152 output[1] = ((uint32_t)(input[0] & 0x03) << 4) +
153 ((uint32_t)input[1] >> 4);
154 output[2] = ((uint32_t)(input[1] & 0x0f) << 2) +
155 ((uint32_t)input[2] >> 6);
156 _PROP_ASSERT(output[0] < 64);
157 _PROP_ASSERT(output[1] < 64);
158 _PROP_ASSERT(output[2] < 64);
159
160 if (_prop_object_externalize_append_char(ctx,
161 _prop_data_base64[output[0]]) == false ||
162 _prop_object_externalize_append_char(ctx,
9f95f3e0 163 _prop_data_base64[output[1]]) == false ||
349fb218 164 _prop_object_externalize_append_char(ctx,
9f95f3e0 165 srclen == 1 ? _prop_data_pad64
349fb218
AH
166 : _prop_data_base64[output[2]]) == false ||
167 _prop_object_externalize_append_char(ctx,
9f95f3e0 168 _prop_data_pad64) == false)
349fb218
AH
169 return (false);
170 }
171
172 if (_prop_object_externalize_end_tag(ctx, "data") == false)
173 return (false);
9f95f3e0 174
349fb218
AH
175 return (true);
176}
177
178/* ARGSUSED */
179static _prop_object_equals_rv_t
180_prop_data_equals(prop_object_t v1, prop_object_t v2,
181 void **stored_pointer1, void **stored_pointer2,
182 prop_object_t *next_obj1, prop_object_t *next_obj2)
183{
184 prop_data_t pd1 = v1;
185 prop_data_t pd2 = v2;
186
187 if (pd1 == pd2)
188 return (_PROP_OBJECT_EQUALS_TRUE);
189 if (pd1->pd_size != pd2->pd_size)
190 return (_PROP_OBJECT_EQUALS_FALSE);
191 if (pd1->pd_size == 0) {
192 _PROP_ASSERT(pd1->pd_immutable == NULL);
193 _PROP_ASSERT(pd2->pd_immutable == NULL);
194 return (_PROP_OBJECT_EQUALS_TRUE);
195 }
196 if (memcmp(pd1->pd_immutable, pd2->pd_immutable, pd1->pd_size) == 0)
197 return _PROP_OBJECT_EQUALS_TRUE;
198 else
199 return _PROP_OBJECT_EQUALS_FALSE;
200}
201
202static prop_data_t
203_prop_data_alloc(void)
204{
205 prop_data_t pd;
206
207 pd = _PROP_POOL_GET(_prop_data_pool);
208 if (pd != NULL) {
209 _prop_object_init(&pd->pd_obj, &_prop_object_type_data);
210
211 pd->pd_mutable = NULL;
212 pd->pd_size = 0;
213 pd->pd_flags = 0;
214 }
215
216 return (pd);
217}
218
219/*
220 * prop_data_create_data --
221 * Create a data container that contains a copy of the data.
222 */
223prop_data_t
224prop_data_create_data(const void *v, size_t size)
225{
226 prop_data_t pd;
227 void *nv;
228
229 pd = _prop_data_alloc();
230 if (pd != NULL && size != 0) {
231 nv = _PROP_MALLOC(size, M_PROP_DATA);
232 if (nv == NULL) {
233 prop_object_release(pd);
234 return (NULL);
235 }
236 memcpy(nv, v, size);
237 pd->pd_mutable = nv;
238 pd->pd_size = size;
239 }
240 return (pd);
241}
242
243/*
244 * prop_data_create_data_nocopy --
245 * Create an immutable data container that contains a refrence to the
246 * provided external data.
247 */
248prop_data_t
249prop_data_create_data_nocopy(const void *v, size_t size)
250{
251 prop_data_t pd;
9f95f3e0 252
349fb218
AH
253 pd = _prop_data_alloc();
254 if (pd != NULL) {
255 pd->pd_immutable = v;
256 pd->pd_size = size;
257 pd->pd_flags |= PD_F_NOCOPY;
258 }
259 return (pd);
260}
261
262/*
263 * prop_data_copy --
264 * Copy a data container. If the original data is external, then
265 * the copy is also references the same external data.
266 */
267prop_data_t
268prop_data_copy(prop_data_t opd)
269{
270 prop_data_t pd;
271
272 if (! prop_object_is_data(opd))
273 return (NULL);
274
275 pd = _prop_data_alloc();
276 if (pd != NULL) {
277 pd->pd_size = opd->pd_size;
278 pd->pd_flags = opd->pd_flags;
279 if (opd->pd_flags & PD_F_NOCOPY)
280 pd->pd_immutable = opd->pd_immutable;
281 else if (opd->pd_size != 0) {
282 void *nv = _PROP_MALLOC(pd->pd_size, M_PROP_DATA);
283 if (nv == NULL) {
284 prop_object_release(pd);
285 return (NULL);
286 }
287 memcpy(nv, opd->pd_immutable, opd->pd_size);
288 pd->pd_mutable = nv;
289 }
290 }
291 return (pd);
292}
293
294/*
295 * prop_data_size --
296 * Return the size of the data.
297 */
298size_t
299prop_data_size(prop_data_t pd)
300{
301
302 if (! prop_object_is_data(pd))
303 return (0);
304
305 return (pd->pd_size);
306}
307
308/*
309 * prop_data_data --
310 * Return a copy of the contents of the data container.
311 * The data is allocated with the M_TEMP malloc type.
312 * If the data container is empty, NULL is returned.
313 */
314void *
315prop_data_data(prop_data_t pd)
316{
317 void *v;
318
319 if (! prop_object_is_data(pd))
320 return (NULL);
321
322 if (pd->pd_size == 0) {
323 _PROP_ASSERT(pd->pd_immutable == NULL);
324 return (NULL);
325 }
326
327 _PROP_ASSERT(pd->pd_immutable != NULL);
328
329 v = _PROP_MALLOC(pd->pd_size, M_TEMP);
330 if (v != NULL)
331 memcpy(v, pd->pd_immutable, pd->pd_size);
9f95f3e0 332
349fb218
AH
333 return (v);
334}
335
336/*
337 * prop_data_data_nocopy --
338 * Return an immutable reference to the contents of the data
339 * container.
340 */
341const void *
342prop_data_data_nocopy(prop_data_t pd)
343{
344
345 if (! prop_object_is_data(pd))
346 return (NULL);
347
348 _PROP_ASSERT((pd->pd_size == 0 && pd->pd_immutable == NULL) ||
349 (pd->pd_size != 0 && pd->pd_immutable != NULL));
350
351 return (pd->pd_immutable);
352}
353
354/*
355 * prop_data_equals --
356 * Return true if two strings are equivalent.
357 */
358bool
359prop_data_equals(prop_data_t pd1, prop_data_t pd2)
360{
361 if (!prop_object_is_data(pd1) || !prop_object_is_data(pd2))
362 return (false);
363
364 return (prop_object_equals(pd1, pd2));
365}
366
367/*
368 * prop_data_equals_data --
369 * Return true if the contained data is equivalent to the specified
370 * external data.
371 */
372bool
373prop_data_equals_data(prop_data_t pd, const void *v, size_t size)
374{
375
376 if (! prop_object_is_data(pd))
377 return (false);
378
379 if (pd->pd_size != size)
380 return (false);
381 return (memcmp(pd->pd_immutable, v, size) == 0);
382}
383
384static bool
385_prop_data_internalize_decode(struct _prop_object_internalize_context *ctx,
386 uint8_t *target, size_t targsize, size_t *sizep,
387 const char **cpp)
388{
389 const char *src;
390 size_t tarindex;
391 int state, ch;
392 const char *pos;
393
394 state = 0;
395 tarindex = 0;
396 src = ctx->poic_cp;
397
398 for (;;) {
399 ch = (unsigned char) *src++;
400 if (_PROP_EOF(ch))
401 return (false);
402 if (_PROP_ISSPACE(ch))
403 continue;
404 if (ch == '<') {
405 src--;
406 break;
407 }
408 if (ch == _prop_data_pad64)
409 break;
410
411 pos = strchr(_prop_data_base64, ch);
412 if (pos == NULL)
413 return (false);
414
415 switch (state) {
416 case 0:
417 if (target) {
418 if (tarindex >= targsize)
419 return (false);
420 target[tarindex] =
421 (uint8_t)((pos - _prop_data_base64) << 2);
422 }
423 state = 1;
424 break;
425
426 case 1:
427 if (target) {
428 if (tarindex + 1 >= targsize)
429 return (false);
430 target[tarindex] |=
431 (uint32_t)(pos - _prop_data_base64) >> 4;
432 target[tarindex + 1] =
433 (uint8_t)(((pos - _prop_data_base64) & 0xf)
434 << 4);
435 }
436 tarindex++;
437 state = 2;
438 break;
439
440 case 2:
441 if (target) {
442 if (tarindex + 1 >= targsize)
443 return (false);
444 target[tarindex] |=
445 (uint32_t)(pos - _prop_data_base64) >> 2;
446 target[tarindex + 1] =
447 (uint8_t)(((pos - _prop_data_base64)
448 & 0x3) << 6);
449 }
450 tarindex++;
451 state = 3;
452 break;
453
454 case 3:
455 if (target) {
456 if (tarindex >= targsize)
457 return (false);
458 target[tarindex] |= (uint8_t)
459 (pos - _prop_data_base64);
460 }
461 tarindex++;
462 state = 0;
463 break;
464
465 default:
466 _PROP_ASSERT(/*CONSTCOND*/0);
467 }
468 }
469
470 /*
471 * We are done decoding the Base64 characters. Let's see if we
472 * ended up on a byte boundary and/or with unrecognized trailing
473 * characters.
474 */
475 if (ch == _prop_data_pad64) {
476 ch = (unsigned char) *src; /* src already advanced */
477 if (_PROP_EOF(ch))
478 return (false);
479 switch (state) {
480 case 0: /* Invalid = in first position */
481 case 1: /* Invalid = in second position */
482 return (false);
483
484 case 2: /* Valid, one byte of info */
485 /* Skip whitespace */
486 for (ch = (unsigned char) *src++;
487 ch != '<'; ch = (unsigned char) *src++) {
488 if (_PROP_EOF(ch))
489 return (false);
490 if (!_PROP_ISSPACE(ch))
491 break;
492 }
493 /* Make sure there is another trailing = */
494 if (ch != _prop_data_pad64)
495 return (false);
496 ch = (unsigned char) *src;
497 /* FALLTHROUGH */
9f95f3e0 498
349fb218
AH
499 case 3: /* Valid, two bytes of info */
500 /*
501 * We know this char is a =. Is there anything but
502 * whitespace after it?
503 */
504 for (ch = (unsigned char) *src++;
505 ch != '<'; ch = (unsigned char) *src++) {
506 if (_PROP_EOF(ch))
507 return (false);
508 if (!_PROP_ISSPACE(ch))
509 return (false);
510 }
511 /* back up to '<' */
512 src--;
513 }
514 } else {
515 /*
516 * We ended by seeing the end of the Base64 string. Make
517 * sure there are no partial bytes lying around.
518 */
519 if (state != 0)
520 return (false);
521 }
522
523 _PROP_ASSERT(*src == '<');
524 if (sizep != NULL)
525 *sizep = tarindex;
526 if (cpp != NULL)
527 *cpp = src;
528
529 return (true);
530}
531
532/*
533 * _prop_data_internalize --
534 * Parse a <data>...</data> and return the object created from the
535 * external representation.
536 */
537
538/* strtoul is used for parsing, enforce. */
539typedef int PROP_DATA_ASSERT[/* CONSTCOND */sizeof(size_t) == sizeof(unsigned long) ? 1 : -1];
540
541/* ARGSUSED */
542bool
543_prop_data_internalize(prop_stack_t stack, prop_object_t *obj,
544 struct _prop_object_internalize_context *ctx)
545{
546 prop_data_t data;
547 uint8_t *buf;
548 size_t len, alen;
549
550 /*
551 * We don't accept empty elements.
552 * This actually only checks for the node to be <data/>
553 * (Which actually causes another error if found.)
554 */
555 if (ctx->poic_is_empty_element)
556 return (true);
557
558 /*
559 * If we got a "size" attribute, get the size of the data blob
560 * from that. Otherwise, we have to figure it out from the base64.
561 */
562 if (ctx->poic_tagattr != NULL) {
563 char *cp;
564
565 if (!_PROP_TAGATTR_MATCH(ctx, "size") ||
566 ctx->poic_tagattrval_len == 0)
567 return (true);
568
569#ifndef _KERNEL
570 errno = 0;
571#endif
572 len = strtoul(ctx->poic_tagattrval, &cp, 0);
573#ifndef _KERNEL /* XXX can't check for ERANGE in the kernel */
574 if (len == ULONG_MAX && errno == ERANGE)
575 return (true);
576#endif
577 if (cp != ctx->poic_tagattrval + ctx->poic_tagattrval_len)
578 return (true);
579 _PROP_ASSERT(*cp == '\"');
580 } else if (_prop_data_internalize_decode(ctx, NULL, 0, &len,
581 NULL) == false)
582 return (true);
583
584 /*
585 * Always allocate one extra in case we don't land on an even byte
586 * boundary during the decode.
587 */
588 buf = _PROP_MALLOC(len + 1, M_PROP_DATA);
589 if (buf == NULL)
590 return (true);
9f95f3e0 591
349fb218
AH
592 if (_prop_data_internalize_decode(ctx, buf, len + 1, &alen,
593 &ctx->poic_cp) == false) {
594 _PROP_FREE(buf, M_PROP_DATA);
595 return (true);
596 }
597 if (alen != len) {
598 _PROP_FREE(buf, M_PROP_DATA);
599 return (true);
600 }
601
602 if (_prop_object_internalize_find_tag(ctx, "data",
603 _PROP_TAG_TYPE_END) == false) {
604 _PROP_FREE(buf, M_PROP_DATA);
605 return (true);
606 }
607
608 data = _prop_data_alloc();
609 if (data == NULL) {
610 _PROP_FREE(buf, M_PROP_DATA);
611 return (true);
612 }
613
614 /*
615 * Handle alternate type of empty node.
616 * XML document could contain open/close tags, yet still be empty.
617 */
618 if (alen == 0) {
619 _PROP_FREE(buf, M_PROP_DATA);
620 data->pd_mutable = NULL;
621 } else {
622 data->pd_mutable = buf;
623 }
624 data->pd_size = len;
625
626 *obj = data;
627 return (true);
628}