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