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