libutil - Add HN_FRACTIONAL to humanize_number()
[dragonfly.git] / sys / libprop / prop_data.c
blob472c76ffe2bed9f6b19cba80e9b577912a63fc11
1 /* $NetBSD: prop_data.c,v 1.14 2009/01/25 06:59:35 cyber Exp $ */
3 /*-
4 * Copyright (c) 2006 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe.
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.
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.
32 #include <libprop/prop_data.h>
33 #include "prop_object_impl.h"
35 #if defined(_KERNEL)
36 #include <sys/systm.h>
37 #include <sys/libkern.h>
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
47 struct _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;
59 #define PD_F_NOCOPY 0x01
61 _PROP_POOL_INIT(_prop_data_pool, sizeof(struct _prop_data), "propdata");
63 _PROP_MALLOC_DEFINE(M_PROP_DATA, "prop data",
64 "property data container object")
66 static _prop_object_free_rv_t
67 _prop_data_free(prop_stack_t, prop_object_t *);
68 static bool _prop_data_externalize(
69 struct _prop_object_externalize_context *,
70 void *);
71 static _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 *);
76 static 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,
83 #define prop_object_is_data(x) \
84 ((x) != NULL && (x)->pd_obj.po_type == &_prop_object_type_data)
86 /* ARGSUSED */
87 static _prop_object_free_rv_t
88 _prop_data_free(prop_stack_t stack, prop_object_t *obj)
90 prop_data_t pd = *obj;
92 if ((pd->pd_flags & PD_F_NOCOPY) == 0 && pd->pd_mutable != NULL)
93 _PROP_FREE(pd->pd_mutable, M_PROP_DATA);
94 _PROP_POOL_PUT(_prop_data_pool, pd);
96 return (_PROP_OBJECT_FREE_DONE);
99 static const char _prop_data_base64[] =
100 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
101 static const char _prop_data_pad64 = '=';
103 static bool
104 _prop_data_externalize(struct _prop_object_externalize_context *ctx, void *v)
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];
112 if (pd->pd_size == 0)
113 return (_prop_object_externalize_empty_tag(ctx, "data"));
115 if (_prop_object_externalize_start_tag(ctx, "data") == false)
116 return (false);
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++;
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);
135 if (_prop_object_externalize_append_char(ctx,
136 _prop_data_base64[output[0]]) == false ||
137 _prop_object_externalize_append_char(ctx,
138 _prop_data_base64[output[1]]) == false ||
139 _prop_object_externalize_append_char(ctx,
140 _prop_data_base64[output[2]]) == false ||
141 _prop_object_externalize_append_char(ctx,
142 _prop_data_base64[output[3]]) == false)
143 return (false);
146 if (srclen != 0) {
147 input[0] = input[1] = input[2] = '\0';
148 for (i = 0; i < srclen; i++)
149 input[i] = *src++;
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);
160 if (_prop_object_externalize_append_char(ctx,
161 _prop_data_base64[output[0]]) == false ||
162 _prop_object_externalize_append_char(ctx,
163 _prop_data_base64[output[1]]) == false ||
164 _prop_object_externalize_append_char(ctx,
165 srclen == 1 ? _prop_data_pad64
166 : _prop_data_base64[output[2]]) == false ||
167 _prop_object_externalize_append_char(ctx,
168 _prop_data_pad64) == false)
169 return (false);
172 if (_prop_object_externalize_end_tag(ctx, "data") == false)
173 return (false);
175 return (true);
178 /* ARGSUSED */
179 static _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)
184 prop_data_t pd1 = v1;
185 prop_data_t pd2 = v2;
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);
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;
202 static prop_data_t
203 _prop_data_alloc(void)
205 prop_data_t pd;
207 pd = _PROP_POOL_GET(_prop_data_pool);
208 if (pd != NULL) {
209 _prop_object_init(&pd->pd_obj, &_prop_object_type_data);
211 pd->pd_mutable = NULL;
212 pd->pd_size = 0;
213 pd->pd_flags = 0;
216 return (pd);
220 * prop_data_create_data --
221 * Create a data container that contains a copy of the data.
223 prop_data_t
224 prop_data_create_data(const void *v, size_t size)
226 prop_data_t pd;
227 void *nv;
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);
236 memcpy(nv, v, size);
237 pd->pd_mutable = nv;
238 pd->pd_size = size;
240 return (pd);
244 * prop_data_create_data_nocopy --
245 * Create an immutable data container that contains a refrence to the
246 * provided external data.
248 prop_data_t
249 prop_data_create_data_nocopy(const void *v, size_t size)
251 prop_data_t pd;
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;
259 return (pd);
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.
267 prop_data_t
268 prop_data_copy(prop_data_t opd)
270 prop_data_t pd;
272 if (! prop_object_is_data(opd))
273 return (NULL);
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);
287 memcpy(nv, opd->pd_immutable, opd->pd_size);
288 pd->pd_mutable = nv;
291 return (pd);
295 * prop_data_size --
296 * Return the size of the data.
298 size_t
299 prop_data_size(prop_data_t pd)
302 if (! prop_object_is_data(pd))
303 return (0);
305 return (pd->pd_size);
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.
314 void *
315 prop_data_data(prop_data_t pd)
317 void *v;
319 if (! prop_object_is_data(pd))
320 return (NULL);
322 if (pd->pd_size == 0) {
323 _PROP_ASSERT(pd->pd_immutable == NULL);
324 return (NULL);
327 _PROP_ASSERT(pd->pd_immutable != NULL);
329 v = _PROP_MALLOC(pd->pd_size, M_TEMP);
330 if (v != NULL)
331 memcpy(v, pd->pd_immutable, pd->pd_size);
333 return (v);
337 * prop_data_data_nocopy --
338 * Return an immutable reference to the contents of the data
339 * container.
341 const void *
342 prop_data_data_nocopy(prop_data_t pd)
345 if (! prop_object_is_data(pd))
346 return (NULL);
348 _PROP_ASSERT((pd->pd_size == 0 && pd->pd_immutable == NULL) ||
349 (pd->pd_size != 0 && pd->pd_immutable != NULL));
351 return (pd->pd_immutable);
355 * prop_data_equals --
356 * Return true if two strings are equivalent.
358 bool
359 prop_data_equals(prop_data_t pd1, prop_data_t pd2)
361 if (!prop_object_is_data(pd1) || !prop_object_is_data(pd2))
362 return (false);
364 return (prop_object_equals(pd1, pd2));
368 * prop_data_equals_data --
369 * Return true if the contained data is equivalent to the specified
370 * external data.
372 bool
373 prop_data_equals_data(prop_data_t pd, const void *v, size_t size)
376 if (! prop_object_is_data(pd))
377 return (false);
379 if (pd->pd_size != size)
380 return (false);
381 return (memcmp(pd->pd_immutable, v, size) == 0);
384 static 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)
389 const char *src;
390 size_t tarindex;
391 int state, ch;
392 const char *pos;
394 state = 0;
395 tarindex = 0;
396 src = ctx->poic_cp;
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;
408 if (ch == _prop_data_pad64)
409 break;
411 pos = strchr(_prop_data_base64, ch);
412 if (pos == NULL)
413 return (false);
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);
423 state = 1;
424 break;
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);
436 tarindex++;
437 state = 2;
438 break;
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);
450 tarindex++;
451 state = 3;
452 break;
454 case 3:
455 if (target) {
456 if (tarindex >= targsize)
457 return (false);
458 target[tarindex] |= (uint8_t)
459 (pos - _prop_data_base64);
461 tarindex++;
462 state = 0;
463 break;
465 default:
466 _PROP_ASSERT(/*CONSTCOND*/0);
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.
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);
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;
493 /* Make sure there is another trailing = */
494 if (ch != _prop_data_pad64)
495 return (false);
496 ch = (unsigned char) *src;
497 /* FALLTHROUGH */
499 case 3: /* Valid, two bytes of info */
501 * We know this char is a =. Is there anything but
502 * whitespace after it?
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);
511 /* back up to '<' */
512 src--;
514 } else {
516 * We ended by seeing the end of the Base64 string. Make
517 * sure there are no partial bytes lying around.
519 if (state != 0)
520 return (false);
523 _PROP_ASSERT(*src == '<');
524 if (sizep != NULL)
525 *sizep = tarindex;
526 if (cpp != NULL)
527 *cpp = src;
529 return (true);
533 * _prop_data_internalize --
534 * Parse a <data>...</data> and return the object created from the
535 * external representation.
538 /* strtoul is used for parsing, enforce. */
539 typedef int PROP_DATA_ASSERT[/* CONSTCOND */sizeof(size_t) == sizeof(unsigned long) ? 1 : -1];
541 /* ARGSUSED */
542 bool
543 _prop_data_internalize(prop_stack_t stack, prop_object_t *obj,
544 struct _prop_object_internalize_context *ctx)
546 prop_data_t data;
547 uint8_t *buf;
548 size_t len, alen;
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.)
555 if (ctx->poic_is_empty_element)
556 return (true);
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.
562 if (ctx->poic_tagattr != NULL) {
563 char *cp;
565 if (!_PROP_TAGATTR_MATCH(ctx, "size") ||
566 ctx->poic_tagattrval_len == 0)
567 return (true);
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);
585 * Always allocate one extra in case we don't land on an even byte
586 * boundary during the decode.
588 buf = _PROP_MALLOC(len + 1, M_PROP_DATA);
589 if (buf == NULL)
590 return (true);
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);
597 if (alen != len) {
598 _PROP_FREE(buf, M_PROP_DATA);
599 return (true);
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);
608 data = _prop_data_alloc();
609 if (data == NULL) {
610 _PROP_FREE(buf, M_PROP_DATA);
611 return (true);
615 * Handle alternate type of empty node.
616 * XML document could contain open/close tags, yet still be empty.
618 if (alen == 0) {
619 _PROP_FREE(buf, M_PROP_DATA);
620 data->pd_mutable = NULL;
621 } else {
622 data->pd_mutable = buf;
624 data->pd_size = len;
626 *obj = data;
627 return (true);