1 /* $NetBSD: prop_data.c,v 1.13 2008/08/03 04:00:12 thorpej 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"
40 struct _prop_object pd_obj
;
43 const void * pdu_immutable
;
45 #define pd_mutable pd_un.pdu_mutable
46 #define pd_immutable pd_un.pdu_immutable
51 #define PD_F_NOCOPY 0x01
53 _PROP_POOL_INIT(_prop_data_pool
, sizeof(struct _prop_data
), "propdata")
55 _PROP_MALLOC_DEFINE(M_PROP_DATA
, "prop data",
56 "property data container object")
58 static _prop_object_free_rv_t
59 _prop_data_free(prop_stack_t
, prop_object_t
*);
60 static bool _prop_data_externalize(
61 struct _prop_object_externalize_context
*,
63 static _prop_object_equals_rv_t
64 _prop_data_equals(prop_object_t
, prop_object_t
,
66 prop_object_t
*, prop_object_t
*);
68 static const struct _prop_object_type _prop_object_type_data
= {
69 .pot_type
= PROP_TYPE_DATA
,
70 .pot_free
= _prop_data_free
,
71 .pot_extern
= _prop_data_externalize
,
72 .pot_equals
= _prop_data_equals
,
75 #define prop_object_is_data(x) \
76 ((x) != NULL && (x)->pd_obj.po_type == &_prop_object_type_data)
79 static _prop_object_free_rv_t
80 _prop_data_free(prop_stack_t stack
, prop_object_t
*obj
)
82 prop_data_t pd
= *obj
;
84 if ((pd
->pd_flags
& PD_F_NOCOPY
) == 0 && pd
->pd_mutable
!= NULL
)
85 _PROP_FREE(pd
->pd_mutable
, M_PROP_DATA
);
86 _PROP_POOL_PUT(_prop_data_pool
, pd
);
88 return (_PROP_OBJECT_FREE_DONE
);
91 static const char _prop_data_base64
[] =
92 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
93 static const char _prop_data_pad64
= '=';
96 _prop_data_externalize(struct _prop_object_externalize_context
*ctx
, void *v
)
104 if (pd
->pd_size
== 0)
105 return (_prop_object_externalize_empty_tag(ctx
, "data"));
107 if (_prop_object_externalize_start_tag(ctx
, "data") == false)
110 for (src
= pd
->pd_immutable
, srclen
= pd
->pd_size
;
111 srclen
> 2; srclen
-= 3) {
116 output
[0] = (uint32_t)input
[0] >> 2;
117 output
[1] = ((uint32_t)(input
[0] & 0x03) << 4) +
118 ((uint32_t)input
[1] >> 4);
119 output
[2] = ((uint32_t)(input
[1] & 0x0f) << 2) +
120 ((uint32_t)input
[2] >> 6);
121 output
[3] = input
[2] & 0x3f;
122 _PROP_ASSERT(output
[0] < 64);
123 _PROP_ASSERT(output
[1] < 64);
124 _PROP_ASSERT(output
[2] < 64);
125 _PROP_ASSERT(output
[3] < 64);
127 if (_prop_object_externalize_append_char(ctx
,
128 _prop_data_base64
[output
[0]]) == false ||
129 _prop_object_externalize_append_char(ctx
,
130 _prop_data_base64
[output
[1]]) == false ||
131 _prop_object_externalize_append_char(ctx
,
132 _prop_data_base64
[output
[2]]) == false ||
133 _prop_object_externalize_append_char(ctx
,
134 _prop_data_base64
[output
[3]]) == false)
139 input
[0] = input
[1] = input
[2] = '\0';
140 for (i
= 0; i
< srclen
; i
++)
143 output
[0] = (uint32_t)input
[0] >> 2;
144 output
[1] = ((uint32_t)(input
[0] & 0x03) << 4) +
145 ((uint32_t)input
[1] >> 4);
146 output
[2] = ((uint32_t)(input
[1] & 0x0f) << 2) +
147 ((uint32_t)input
[2] >> 6);
148 _PROP_ASSERT(output
[0] < 64);
149 _PROP_ASSERT(output
[1] < 64);
150 _PROP_ASSERT(output
[2] < 64);
152 if (_prop_object_externalize_append_char(ctx
,
153 _prop_data_base64
[output
[0]]) == false ||
154 _prop_object_externalize_append_char(ctx
,
155 _prop_data_base64
[output
[1]]) == false ||
156 _prop_object_externalize_append_char(ctx
,
157 srclen
== 1 ? _prop_data_pad64
158 : _prop_data_base64
[output
[2]]) == false ||
159 _prop_object_externalize_append_char(ctx
,
160 _prop_data_pad64
) == false)
164 if (_prop_object_externalize_end_tag(ctx
, "data") == false)
171 static _prop_object_equals_rv_t
172 _prop_data_equals(prop_object_t v1
, prop_object_t v2
,
173 void **stored_pointer1
, void **stored_pointer2
,
174 prop_object_t
*next_obj1
, prop_object_t
*next_obj2
)
176 prop_data_t pd1
= v1
;
177 prop_data_t pd2
= v2
;
180 return (_PROP_OBJECT_EQUALS_TRUE
);
181 if (pd1
->pd_size
!= pd2
->pd_size
)
182 return (_PROP_OBJECT_EQUALS_FALSE
);
183 if (pd1
->pd_size
== 0) {
184 _PROP_ASSERT(pd1
->pd_immutable
== NULL
);
185 _PROP_ASSERT(pd2
->pd_immutable
== NULL
);
186 return (_PROP_OBJECT_EQUALS_TRUE
);
188 if (memcmp(pd1
->pd_immutable
, pd2
->pd_immutable
, pd1
->pd_size
) == 0)
189 return _PROP_OBJECT_EQUALS_TRUE
;
191 return _PROP_OBJECT_EQUALS_FALSE
;
195 _prop_data_alloc(void)
199 pd
= _PROP_POOL_GET(_prop_data_pool
);
201 _prop_object_init(&pd
->pd_obj
, &_prop_object_type_data
);
203 pd
->pd_mutable
= NULL
;
212 * prop_data_create_data --
213 * Create a data container that contains a copy of the data.
216 prop_data_create_data(const void *v
, size_t size
)
221 pd
= _prop_data_alloc();
222 if (pd
!= NULL
&& size
!= 0) {
223 nv
= _PROP_MALLOC(size
, M_PROP_DATA
);
225 prop_object_release(pd
);
236 * prop_data_create_data_nocopy --
237 * Create an immutable data container that contains a refrence to the
238 * provided external data.
241 prop_data_create_data_nocopy(const void *v
, size_t size
)
245 pd
= _prop_data_alloc();
247 pd
->pd_immutable
= v
;
249 pd
->pd_flags
|= PD_F_NOCOPY
;
256 * Copy a data container. If the original data is external, then
257 * the copy is also references the same external data.
260 prop_data_copy(prop_data_t opd
)
264 if (! prop_object_is_data(opd
))
267 pd
= _prop_data_alloc();
269 pd
->pd_size
= opd
->pd_size
;
270 pd
->pd_flags
= opd
->pd_flags
;
271 if (opd
->pd_flags
& PD_F_NOCOPY
)
272 pd
->pd_immutable
= opd
->pd_immutable
;
273 else if (opd
->pd_size
!= 0) {
274 void *nv
= _PROP_MALLOC(pd
->pd_size
, M_PROP_DATA
);
276 prop_object_release(pd
);
279 memcpy(nv
, opd
->pd_immutable
, opd
->pd_size
);
288 * Return the size of the data.
291 prop_data_size(prop_data_t pd
)
294 if (! prop_object_is_data(pd
))
297 return (pd
->pd_size
);
302 * Return a copy of the contents of the data container.
303 * The data is allocated with the M_TEMP malloc type.
304 * If the data container is empty, NULL is returned.
307 prop_data_data(prop_data_t pd
)
311 if (! prop_object_is_data(pd
))
314 if (pd
->pd_size
== 0) {
315 _PROP_ASSERT(pd
->pd_immutable
== NULL
);
319 _PROP_ASSERT(pd
->pd_immutable
!= NULL
);
321 v
= _PROP_MALLOC(pd
->pd_size
, M_TEMP
);
323 memcpy(v
, pd
->pd_immutable
, pd
->pd_size
);
329 * prop_data_data_nocopy --
330 * Return an immutable reference to the contents of the data
334 prop_data_data_nocopy(prop_data_t pd
)
337 if (! prop_object_is_data(pd
))
340 _PROP_ASSERT((pd
->pd_size
== 0 && pd
->pd_immutable
== NULL
) ||
341 (pd
->pd_size
!= 0 && pd
->pd_immutable
!= NULL
));
343 return (pd
->pd_immutable
);
347 * prop_data_equals --
348 * Return true if two strings are equivalent.
351 prop_data_equals(prop_data_t pd1
, prop_data_t pd2
)
353 if (!prop_object_is_data(pd1
) || !prop_object_is_data(pd2
))
356 return (prop_object_equals(pd1
, pd2
));
360 * prop_data_equals_data --
361 * Return true if the contained data is equivalent to the specified
365 prop_data_equals_data(prop_data_t pd
, const void *v
, size_t size
)
368 if (! prop_object_is_data(pd
))
371 if (pd
->pd_size
!= size
)
373 return (memcmp(pd
->pd_immutable
, v
, size
) == 0);
377 _prop_data_internalize_decode(struct _prop_object_internalize_context
*ctx
,
378 uint8_t *target
, size_t targsize
, size_t *sizep
,
391 ch
= (unsigned char) *src
++;
394 if (_PROP_ISSPACE(ch
))
400 if (ch
== _prop_data_pad64
)
403 pos
= strchr(_prop_data_base64
, ch
);
410 if (tarindex
>= targsize
)
413 (uint8_t)((pos
- _prop_data_base64
) << 2);
420 if (tarindex
+ 1 >= targsize
)
423 (uint32_t)(pos
- _prop_data_base64
) >> 4;
424 target
[tarindex
+ 1] =
425 (uint8_t)(((pos
- _prop_data_base64
) & 0xf)
434 if (tarindex
+ 1 >= targsize
)
437 (uint32_t)(pos
- _prop_data_base64
) >> 2;
438 target
[tarindex
+ 1] =
439 (uint8_t)(((pos
- _prop_data_base64
)
448 if (tarindex
>= targsize
)
450 target
[tarindex
] |= (uint8_t)
451 (pos
- _prop_data_base64
);
458 _PROP_ASSERT(/*CONSTCOND*/0);
463 * We are done decoding the Base64 characters. Let's see if we
464 * ended up on a byte boundary and/or with unrecognized trailing
467 if (ch
== _prop_data_pad64
) {
468 ch
= (unsigned char) *src
; /* src already advanced */
472 case 0: /* Invalid = in first position */
473 case 1: /* Invalid = in second position */
476 case 2: /* Valid, one byte of info */
477 /* Skip whitespace */
478 for (ch
= (unsigned char) *src
++;
479 ch
!= '<'; ch
= (unsigned char) *src
++) {
482 if (!_PROP_ISSPACE(ch
))
485 /* Make sure there is another trailing = */
486 if (ch
!= _prop_data_pad64
)
488 ch
= (unsigned char) *src
;
491 case 3: /* Valid, two bytes of info */
493 * We know this char is a =. Is there anything but
494 * whitespace after it?
496 for (ch
= (unsigned char) *src
++;
497 ch
!= '<'; ch
= (unsigned char) *src
++) {
500 if (!_PROP_ISSPACE(ch
))
508 * We ended by seeing the end of the Base64 string. Make
509 * sure there are no partial bytes lying around.
515 _PROP_ASSERT(*src
== '<');
525 * _prop_data_internalize --
526 * Parse a <data>...</data> and return the object created from the
527 * external representation.
530 /* strtoul is used for parsing, enforce. */
531 typedef int PROP_DATA_ASSERT
[/* CONSTCOND */sizeof(size_t) == sizeof(unsigned long) ? 1 : -1];
535 _prop_data_internalize(prop_stack_t stack
, prop_object_t
*obj
,
536 struct _prop_object_internalize_context
*ctx
)
543 * We don't accept empty elements.
544 * This actually only checks for the node to be <data/>
545 * (Which actually causes another error if found.)
547 if (ctx
->poic_is_empty_element
)
551 * If we got a "size" attribute, get the size of the data blob
552 * from that. Otherwise, we have to figure it out from the base64.
554 if (ctx
->poic_tagattr
!= NULL
) {
557 if (!_PROP_TAGATTR_MATCH(ctx
, "size") ||
558 ctx
->poic_tagattrval_len
== 0)
562 len
= strtoul(ctx
->poic_tagattrval
, &cp
, 0);
563 if (len
== ULONG_MAX
&& errno
== ERANGE
)
565 if (cp
!= ctx
->poic_tagattrval
+ ctx
->poic_tagattrval_len
)
567 _PROP_ASSERT(*cp
== '\"');
568 } else if (_prop_data_internalize_decode(ctx
, NULL
, 0, &len
,
573 * Always allocate one extra in case we don't land on an even byte
574 * boundary during the decode.
576 buf
= _PROP_MALLOC(len
+ 1, M_PROP_DATA
);
580 if (_prop_data_internalize_decode(ctx
, buf
, len
+ 1, &alen
,
581 &ctx
->poic_cp
) == false) {
582 _PROP_FREE(buf
, M_PROP_DATA
);
586 _PROP_FREE(buf
, M_PROP_DATA
);
590 if (_prop_object_internalize_find_tag(ctx
, "data",
591 _PROP_TAG_TYPE_END
) == false) {
592 _PROP_FREE(buf
, M_PROP_DATA
);
596 data
= _prop_data_alloc();
598 _PROP_FREE(buf
, M_PROP_DATA
);
603 * Handle alternate type of empty node.
604 * XML document could contain open/close tags, yet still be empty.
607 _PROP_FREE(buf
, M_PROP_DATA
);
608 data
->pd_mutable
= NULL
;
610 data
->pd_mutable
= buf
;