1 /* $NetBSD: prop_data.c,v 1.14 2009/01/25 06:59:35 cyber Exp $ */
4 * Copyright (c) 2006 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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"
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>
48 struct _prop_object pd_obj
;
51 const void * pdu_immutable
;
53 #define pd_mutable pd_un.pdu_mutable
54 #define pd_immutable pd_un.pdu_immutable
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
*,
71 static _prop_object_equals_rv_t
72 _prop_data_equals(prop_object_t
, prop_object_t
,
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)
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
= '=';
104 _prop_data_externalize(struct _prop_object_externalize_context
*ctx
, void *v
)
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)
118 for (src
= pd
->pd_immutable
, srclen
= pd
->pd_size
;
119 srclen
> 2; srclen
-= 3) {
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)
147 input
[0] = input
[1] = input
[2] = '\0';
148 for (i
= 0; i
< srclen
; i
++)
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)
172 if (_prop_object_externalize_end_tag(ctx
, "data") == false)
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
;
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
;
199 return _PROP_OBJECT_EQUALS_FALSE
;
203 _prop_data_alloc(void)
207 pd
= _PROP_POOL_GET(_prop_data_pool
);
209 _prop_object_init(&pd
->pd_obj
, &_prop_object_type_data
);
211 pd
->pd_mutable
= NULL
;
220 * prop_data_create_data --
221 * Create a data container that contains a copy of the data.
224 prop_data_create_data(const void *v
, size_t size
)
229 pd
= _prop_data_alloc();
230 if (pd
!= NULL
&& size
!= 0) {
231 nv
= _PROP_MALLOC(size
, M_PROP_DATA
);
233 prop_object_release(pd
);
244 * prop_data_create_data_nocopy --
245 * Create an immutable data container that contains a refrence to the
246 * provided external data.
249 prop_data_create_data_nocopy(const void *v
, size_t size
)
253 pd
= _prop_data_alloc();
255 pd
->pd_immutable
= v
;
257 pd
->pd_flags
|= PD_F_NOCOPY
;
264 * Copy a data container. If the original data is external, then
265 * the copy is also references the same external data.
268 prop_data_copy(prop_data_t opd
)
272 if (! prop_object_is_data(opd
))
275 pd
= _prop_data_alloc();
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
);
284 prop_object_release(pd
);
287 memcpy(nv
, opd
->pd_immutable
, opd
->pd_size
);
296 * Return the size of the data.
299 prop_data_size(prop_data_t pd
)
302 if (! prop_object_is_data(pd
))
305 return (pd
->pd_size
);
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.
315 prop_data_data(prop_data_t pd
)
319 if (! prop_object_is_data(pd
))
322 if (pd
->pd_size
== 0) {
323 _PROP_ASSERT(pd
->pd_immutable
== NULL
);
327 _PROP_ASSERT(pd
->pd_immutable
!= NULL
);
329 v
= _PROP_MALLOC(pd
->pd_size
, M_TEMP
);
331 memcpy(v
, pd
->pd_immutable
, pd
->pd_size
);
337 * prop_data_data_nocopy --
338 * Return an immutable reference to the contents of the data
342 prop_data_data_nocopy(prop_data_t pd
)
345 if (! prop_object_is_data(pd
))
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.
359 prop_data_equals(prop_data_t pd1
, prop_data_t pd2
)
361 if (!prop_object_is_data(pd1
) || !prop_object_is_data(pd2
))
364 return (prop_object_equals(pd1
, pd2
));
368 * prop_data_equals_data --
369 * Return true if the contained data is equivalent to the specified
373 prop_data_equals_data(prop_data_t pd
, const void *v
, size_t size
)
376 if (! prop_object_is_data(pd
))
379 if (pd
->pd_size
!= size
)
381 return (memcmp(pd
->pd_immutable
, v
, size
) == 0);
385 _prop_data_internalize_decode(struct _prop_object_internalize_context
*ctx
,
386 uint8_t *target
, size_t targsize
, size_t *sizep
,
399 ch
= (unsigned char) *src
++;
402 if (_PROP_ISSPACE(ch
))
408 if (ch
== _prop_data_pad64
)
411 pos
= strchr(_prop_data_base64
, ch
);
418 if (tarindex
>= targsize
)
421 (uint8_t)((pos
- _prop_data_base64
) << 2);
428 if (tarindex
+ 1 >= targsize
)
431 (uint32_t)(pos
- _prop_data_base64
) >> 4;
432 target
[tarindex
+ 1] =
433 (uint8_t)(((pos
- _prop_data_base64
) & 0xf)
442 if (tarindex
+ 1 >= targsize
)
445 (uint32_t)(pos
- _prop_data_base64
) >> 2;
446 target
[tarindex
+ 1] =
447 (uint8_t)(((pos
- _prop_data_base64
)
456 if (tarindex
>= targsize
)
458 target
[tarindex
] |= (uint8_t)
459 (pos
- _prop_data_base64
);
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
475 if (ch
== _prop_data_pad64
) {
476 ch
= (unsigned char) *src
; /* src already advanced */
480 case 0: /* Invalid = in first position */
481 case 1: /* Invalid = in second position */
484 case 2: /* Valid, one byte of info */
485 /* Skip whitespace */
486 for (ch
= (unsigned char) *src
++;
487 ch
!= '<'; ch
= (unsigned char) *src
++) {
490 if (!_PROP_ISSPACE(ch
))
493 /* Make sure there is another trailing = */
494 if (ch
!= _prop_data_pad64
)
496 ch
= (unsigned char) *src
;
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
++) {
508 if (!_PROP_ISSPACE(ch
))
516 * We ended by seeing the end of the Base64 string. Make
517 * sure there are no partial bytes lying around.
523 _PROP_ASSERT(*src
== '<');
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];
543 _prop_data_internalize(prop_stack_t stack
, prop_object_t
*obj
,
544 struct _prop_object_internalize_context
*ctx
)
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
)
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
) {
565 if (!_PROP_TAGATTR_MATCH(ctx
, "size") ||
566 ctx
->poic_tagattrval_len
== 0)
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
)
577 if (cp
!= ctx
->poic_tagattrval
+ ctx
->poic_tagattrval_len
)
579 _PROP_ASSERT(*cp
== '\"');
580 } else if (_prop_data_internalize_decode(ctx
, NULL
, 0, &len
,
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
);
592 if (_prop_data_internalize_decode(ctx
, buf
, len
+ 1, &alen
,
593 &ctx
->poic_cp
) == false) {
594 _PROP_FREE(buf
, M_PROP_DATA
);
598 _PROP_FREE(buf
, M_PROP_DATA
);
602 if (_prop_object_internalize_find_tag(ctx
, "data",
603 _PROP_TAG_TYPE_END
) == false) {
604 _PROP_FREE(buf
, M_PROP_DATA
);
608 data
= _prop_data_alloc();
610 _PROP_FREE(buf
, M_PROP_DATA
);
615 * Handle alternate type of empty node.
616 * XML document could contain open/close tags, yet still be empty.
619 _PROP_FREE(buf
, M_PROP_DATA
);
620 data
->pd_mutable
= NULL
;
622 data
->pd_mutable
= buf
;