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