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 <prop/prop_data.h>
33 #include "prop_object_impl.h"
36 #include <sys/systm.h>
37 #elif defined(_STANDALONE)
38 #include <sys/param.h>
39 #include <lib/libkern/libkern.h>
47 struct _prop_object pd_obj
;
50 const void * pdu_immutable
;
52 #define pd_mutable pd_un.pdu_mutable
53 #define pd_immutable pd_un.pdu_immutable
58 #define PD_F_NOCOPY 0x01
60 _PROP_POOL_INIT(_prop_data_pool
, sizeof(struct _prop_data
), "propdata")
62 _PROP_MALLOC_DEFINE(M_PROP_DATA
, "prop data",
63 "property data container object")
65 static _prop_object_free_rv_t
66 _prop_data_free(prop_stack_t
, prop_object_t
*);
67 static bool _prop_data_externalize(
68 struct _prop_object_externalize_context
*,
70 static _prop_object_equals_rv_t
71 _prop_data_equals(prop_object_t
, prop_object_t
,
73 prop_object_t
*, prop_object_t
*);
75 static const struct _prop_object_type _prop_object_type_data
= {
76 .pot_type
= PROP_TYPE_DATA
,
77 .pot_free
= _prop_data_free
,
78 .pot_extern
= _prop_data_externalize
,
79 .pot_equals
= _prop_data_equals
,
82 #define prop_object_is_data(x) \
83 ((x) != NULL && (x)->pd_obj.po_type == &_prop_object_type_data)
86 static _prop_object_free_rv_t
87 _prop_data_free(prop_stack_t stack
, prop_object_t
*obj
)
89 prop_data_t pd
= *obj
;
91 if ((pd
->pd_flags
& PD_F_NOCOPY
) == 0 && pd
->pd_mutable
!= NULL
)
92 _PROP_FREE(pd
->pd_mutable
, M_PROP_DATA
);
93 _PROP_POOL_PUT(_prop_data_pool
, pd
);
95 return (_PROP_OBJECT_FREE_DONE
);
98 static const char _prop_data_base64
[] =
99 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
100 static const char _prop_data_pad64
= '=';
103 _prop_data_externalize(struct _prop_object_externalize_context
*ctx
, void *v
)
111 if (pd
->pd_size
== 0)
112 return (_prop_object_externalize_empty_tag(ctx
, "data"));
114 if (_prop_object_externalize_start_tag(ctx
, "data") == false)
117 for (src
= pd
->pd_immutable
, srclen
= pd
->pd_size
;
118 srclen
> 2; srclen
-= 3) {
123 output
[0] = (uint32_t)input
[0] >> 2;
124 output
[1] = ((uint32_t)(input
[0] & 0x03) << 4) +
125 ((uint32_t)input
[1] >> 4);
126 output
[2] = ((uint32_t)(input
[1] & 0x0f) << 2) +
127 ((uint32_t)input
[2] >> 6);
128 output
[3] = input
[2] & 0x3f;
129 _PROP_ASSERT(output
[0] < 64);
130 _PROP_ASSERT(output
[1] < 64);
131 _PROP_ASSERT(output
[2] < 64);
132 _PROP_ASSERT(output
[3] < 64);
134 if (_prop_object_externalize_append_char(ctx
,
135 _prop_data_base64
[output
[0]]) == false ||
136 _prop_object_externalize_append_char(ctx
,
137 _prop_data_base64
[output
[1]]) == false ||
138 _prop_object_externalize_append_char(ctx
,
139 _prop_data_base64
[output
[2]]) == false ||
140 _prop_object_externalize_append_char(ctx
,
141 _prop_data_base64
[output
[3]]) == false)
146 input
[0] = input
[1] = input
[2] = '\0';
147 for (i
= 0; i
< srclen
; i
++)
150 output
[0] = (uint32_t)input
[0] >> 2;
151 output
[1] = ((uint32_t)(input
[0] & 0x03) << 4) +
152 ((uint32_t)input
[1] >> 4);
153 output
[2] = ((uint32_t)(input
[1] & 0x0f) << 2) +
154 ((uint32_t)input
[2] >> 6);
155 _PROP_ASSERT(output
[0] < 64);
156 _PROP_ASSERT(output
[1] < 64);
157 _PROP_ASSERT(output
[2] < 64);
159 if (_prop_object_externalize_append_char(ctx
,
160 _prop_data_base64
[output
[0]]) == false ||
161 _prop_object_externalize_append_char(ctx
,
162 _prop_data_base64
[output
[1]]) == false ||
163 _prop_object_externalize_append_char(ctx
,
164 srclen
== 1 ? _prop_data_pad64
165 : _prop_data_base64
[output
[2]]) == false ||
166 _prop_object_externalize_append_char(ctx
,
167 _prop_data_pad64
) == false)
171 if (_prop_object_externalize_end_tag(ctx
, "data") == false)
178 static _prop_object_equals_rv_t
179 _prop_data_equals(prop_object_t v1
, prop_object_t v2
,
180 void **stored_pointer1
, void **stored_pointer2
,
181 prop_object_t
*next_obj1
, prop_object_t
*next_obj2
)
183 prop_data_t pd1
= v1
;
184 prop_data_t pd2
= v2
;
187 return (_PROP_OBJECT_EQUALS_TRUE
);
188 if (pd1
->pd_size
!= pd2
->pd_size
)
189 return (_PROP_OBJECT_EQUALS_FALSE
);
190 if (pd1
->pd_size
== 0) {
191 _PROP_ASSERT(pd1
->pd_immutable
== NULL
);
192 _PROP_ASSERT(pd2
->pd_immutable
== NULL
);
193 return (_PROP_OBJECT_EQUALS_TRUE
);
195 if (memcmp(pd1
->pd_immutable
, pd2
->pd_immutable
, pd1
->pd_size
) == 0)
196 return _PROP_OBJECT_EQUALS_TRUE
;
198 return _PROP_OBJECT_EQUALS_FALSE
;
202 _prop_data_alloc(void)
206 pd
= _PROP_POOL_GET(_prop_data_pool
);
208 _prop_object_init(&pd
->pd_obj
, &_prop_object_type_data
);
210 pd
->pd_mutable
= NULL
;
219 * prop_data_create_data --
220 * Create a data container that contains a copy of the data.
223 prop_data_create_data(const void *v
, size_t size
)
228 pd
= _prop_data_alloc();
229 if (pd
!= NULL
&& size
!= 0) {
230 nv
= _PROP_MALLOC(size
, M_PROP_DATA
);
232 prop_object_release(pd
);
243 * prop_data_create_data_nocopy --
244 * Create an immutable data container that contains a refrence to the
245 * provided external data.
248 prop_data_create_data_nocopy(const void *v
, size_t size
)
252 pd
= _prop_data_alloc();
254 pd
->pd_immutable
= v
;
256 pd
->pd_flags
|= PD_F_NOCOPY
;
263 * Copy a data container. If the original data is external, then
264 * the copy is also references the same external data.
267 prop_data_copy(prop_data_t opd
)
271 if (! prop_object_is_data(opd
))
274 pd
= _prop_data_alloc();
276 pd
->pd_size
= opd
->pd_size
;
277 pd
->pd_flags
= opd
->pd_flags
;
278 if (opd
->pd_flags
& PD_F_NOCOPY
)
279 pd
->pd_immutable
= opd
->pd_immutable
;
280 else if (opd
->pd_size
!= 0) {
281 void *nv
= _PROP_MALLOC(pd
->pd_size
, M_PROP_DATA
);
283 prop_object_release(pd
);
286 memcpy(nv
, opd
->pd_immutable
, opd
->pd_size
);
295 * Return the size of the data.
298 prop_data_size(prop_data_t pd
)
301 if (! prop_object_is_data(pd
))
304 return (pd
->pd_size
);
309 * Return a copy of the contents of the data container.
310 * The data is allocated with the M_TEMP malloc type.
311 * If the data container is empty, NULL is returned.
314 prop_data_data(prop_data_t pd
)
318 if (! prop_object_is_data(pd
))
321 if (pd
->pd_size
== 0) {
322 _PROP_ASSERT(pd
->pd_immutable
== NULL
);
326 _PROP_ASSERT(pd
->pd_immutable
!= NULL
);
328 v
= _PROP_MALLOC(pd
->pd_size
, M_TEMP
);
330 memcpy(v
, pd
->pd_immutable
, pd
->pd_size
);
336 * prop_data_data_nocopy --
337 * Return an immutable reference to the contents of the data
341 prop_data_data_nocopy(prop_data_t pd
)
344 if (! prop_object_is_data(pd
))
347 _PROP_ASSERT((pd
->pd_size
== 0 && pd
->pd_immutable
== NULL
) ||
348 (pd
->pd_size
!= 0 && pd
->pd_immutable
!= NULL
));
350 return (pd
->pd_immutable
);
354 * prop_data_equals --
355 * Return true if two strings are equivalent.
358 prop_data_equals(prop_data_t pd1
, prop_data_t pd2
)
360 if (!prop_object_is_data(pd1
) || !prop_object_is_data(pd2
))
363 return (prop_object_equals(pd1
, pd2
));
367 * prop_data_equals_data --
368 * Return true if the contained data is equivalent to the specified
372 prop_data_equals_data(prop_data_t pd
, const void *v
, size_t size
)
375 if (! prop_object_is_data(pd
))
378 if (pd
->pd_size
!= size
)
380 return (memcmp(pd
->pd_immutable
, v
, size
) == 0);
384 _prop_data_internalize_decode(struct _prop_object_internalize_context
*ctx
,
385 uint8_t *target
, size_t targsize
, size_t *sizep
,
398 ch
= (unsigned char) *src
++;
401 if (_PROP_ISSPACE(ch
))
407 if (ch
== _prop_data_pad64
)
410 pos
= strchr(_prop_data_base64
, ch
);
417 if (tarindex
>= targsize
)
420 (uint8_t)((pos
- _prop_data_base64
) << 2);
427 if (tarindex
+ 1 >= targsize
)
430 (uint32_t)(pos
- _prop_data_base64
) >> 4;
431 target
[tarindex
+ 1] =
432 (uint8_t)(((pos
- _prop_data_base64
) & 0xf)
441 if (tarindex
+ 1 >= targsize
)
444 (uint32_t)(pos
- _prop_data_base64
) >> 2;
445 target
[tarindex
+ 1] =
446 (uint8_t)(((pos
- _prop_data_base64
)
455 if (tarindex
>= targsize
)
457 target
[tarindex
] |= (uint8_t)
458 (pos
- _prop_data_base64
);
465 _PROP_ASSERT(/*CONSTCOND*/0);
470 * We are done decoding the Base64 characters. Let's see if we
471 * ended up on a byte boundary and/or with unrecognized trailing
474 if (ch
== _prop_data_pad64
) {
475 ch
= (unsigned char) *src
; /* src already advanced */
479 case 0: /* Invalid = in first position */
480 case 1: /* Invalid = in second position */
483 case 2: /* Valid, one byte of info */
484 /* Skip whitespace */
485 for (ch
= (unsigned char) *src
++;
486 ch
!= '<'; ch
= (unsigned char) *src
++) {
489 if (!_PROP_ISSPACE(ch
))
492 /* Make sure there is another trailing = */
493 if (ch
!= _prop_data_pad64
)
495 ch
= (unsigned char) *src
;
498 case 3: /* Valid, two bytes of info */
500 * We know this char is a =. Is there anything but
501 * whitespace after it?
503 for (ch
= (unsigned char) *src
++;
504 ch
!= '<'; ch
= (unsigned char) *src
++) {
507 if (!_PROP_ISSPACE(ch
))
515 * We ended by seeing the end of the Base64 string. Make
516 * sure there are no partial bytes lying around.
522 _PROP_ASSERT(*src
== '<');
532 * _prop_data_internalize --
533 * Parse a <data>...</data> and return the object created from the
534 * external representation.
537 /* strtoul is used for parsing, enforce. */
538 typedef int PROP_DATA_ASSERT
[/* CONSTCOND */sizeof(size_t) == sizeof(unsigned long) ? 1 : -1];
542 _prop_data_internalize(prop_stack_t stack
, prop_object_t
*obj
,
543 struct _prop_object_internalize_context
*ctx
)
550 * We don't accept empty elements.
551 * This actually only checks for the node to be <data/>
552 * (Which actually causes another error if found.)
554 if (ctx
->poic_is_empty_element
)
558 * If we got a "size" attribute, get the size of the data blob
559 * from that. Otherwise, we have to figure it out from the base64.
561 if (ctx
->poic_tagattr
!= NULL
) {
564 if (!_PROP_TAGATTR_MATCH(ctx
, "size") ||
565 ctx
->poic_tagattrval_len
== 0)
571 len
= strtoul(ctx
->poic_tagattrval
, &cp
, 0);
572 #ifndef _KERNEL /* XXX can't check for ERANGE in the kernel */
573 if (len
== ULONG_MAX
&& errno
== ERANGE
)
576 if (cp
!= ctx
->poic_tagattrval
+ ctx
->poic_tagattrval_len
)
578 _PROP_ASSERT(*cp
== '\"');
579 } else if (_prop_data_internalize_decode(ctx
, NULL
, 0, &len
,
584 * Always allocate one extra in case we don't land on an even byte
585 * boundary during the decode.
587 buf
= _PROP_MALLOC(len
+ 1, M_PROP_DATA
);
591 if (_prop_data_internalize_decode(ctx
, buf
, len
+ 1, &alen
,
592 &ctx
->poic_cp
) == false) {
593 _PROP_FREE(buf
, M_PROP_DATA
);
597 _PROP_FREE(buf
, M_PROP_DATA
);
601 if (_prop_object_internalize_find_tag(ctx
, "data",
602 _PROP_TAG_TYPE_END
) == false) {
603 _PROP_FREE(buf
, M_PROP_DATA
);
607 data
= _prop_data_alloc();
609 _PROP_FREE(buf
, M_PROP_DATA
);
614 * Handle alternate type of empty node.
615 * XML document could contain open/close tags, yet still be empty.
618 _PROP_FREE(buf
, M_PROP_DATA
);
619 data
->pd_mutable
= NULL
;
621 data
->pd_mutable
= buf
;