1 | /*
|
---|
2 | * Copyright (c) 2012 Sean Bartell
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup bithenge
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file
|
---|
34 | * Raw binary blobs.
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <assert.h>
|
---|
38 | #include <errno.h>
|
---|
39 | #include <stdlib.h>
|
---|
40 | #include "common.h"
|
---|
41 | #include <bithenge/blob.h>
|
---|
42 | #include <bithenge/tree.h>
|
---|
43 |
|
---|
44 | /** Initialize a random access blob.
|
---|
45 | * @memberof bithenge_blob_t
|
---|
46 | * @param[out] blob The blob to initialize.
|
---|
47 | * @param[in] ops Operations providing random access support. This pointer must
|
---|
48 | * be valid until the blob is destroyed.
|
---|
49 | * @return EOK on success or an error code from errno.h.
|
---|
50 | */
|
---|
51 | errno_t bithenge_init_random_access_blob(bithenge_blob_t *blob,
|
---|
52 | const bithenge_random_access_blob_ops_t *ops)
|
---|
53 | {
|
---|
54 | assert(blob);
|
---|
55 | assert(ops);
|
---|
56 | assert(ops->destroy);
|
---|
57 | assert(ops->read || ops->read_bits);
|
---|
58 | assert(ops->size);
|
---|
59 |
|
---|
60 | if (bithenge_should_fail())
|
---|
61 | return ENOMEM;
|
---|
62 |
|
---|
63 | blob->base.type = BITHENGE_NODE_BLOB;
|
---|
64 | blob->base.refs = 1;
|
---|
65 | blob->base.blob_ops = ops;
|
---|
66 | return EOK;
|
---|
67 | }
|
---|
68 |
|
---|
69 | static errno_t sequential_buffer(bithenge_sequential_blob_t *blob, aoff64_t end)
|
---|
70 | {
|
---|
71 | bool need_realloc = false;
|
---|
72 | while (end > blob->buffer_size) {
|
---|
73 | blob->buffer_size = max(4096, 2 * blob->buffer_size);
|
---|
74 | need_realloc = true;
|
---|
75 | }
|
---|
76 | if (need_realloc) {
|
---|
77 | char *buffer = realloc(blob->buffer, blob->buffer_size);
|
---|
78 | if (!buffer)
|
---|
79 | return ENOMEM;
|
---|
80 | blob->buffer = buffer;
|
---|
81 | }
|
---|
82 | aoff64_t size = end - blob->data_size;
|
---|
83 | errno_t rc = blob->ops->read(blob, blob->buffer + blob->data_size, &size);
|
---|
84 | if (rc != EOK)
|
---|
85 | return rc;
|
---|
86 | blob->data_size += size;
|
---|
87 | return EOK;
|
---|
88 | }
|
---|
89 |
|
---|
90 | static inline bithenge_sequential_blob_t *blob_as_sequential(
|
---|
91 | bithenge_blob_t *base)
|
---|
92 | {
|
---|
93 | return (bithenge_sequential_blob_t *)base;
|
---|
94 | }
|
---|
95 |
|
---|
96 | static inline bithenge_blob_t *sequential_as_blob(
|
---|
97 | bithenge_sequential_blob_t *blob)
|
---|
98 | {
|
---|
99 | return &blob->base;
|
---|
100 | }
|
---|
101 |
|
---|
102 | static errno_t sequential_size(bithenge_blob_t *base, aoff64_t *size)
|
---|
103 | {
|
---|
104 | bithenge_sequential_blob_t *blob = blob_as_sequential(base);
|
---|
105 | errno_t rc;
|
---|
106 | if (blob->ops->size) {
|
---|
107 | rc = blob->ops->size(blob, size);
|
---|
108 | if (rc == EOK)
|
---|
109 | return EOK;
|
---|
110 | }
|
---|
111 | rc = sequential_buffer(blob, blob->buffer_size);
|
---|
112 | if (rc != EOK)
|
---|
113 | return rc;
|
---|
114 | while (blob->data_size == blob->buffer_size) {
|
---|
115 | rc = sequential_buffer(blob, 2 * blob->buffer_size);
|
---|
116 | if (rc != EOK)
|
---|
117 | return rc;
|
---|
118 | }
|
---|
119 | *size = blob->data_size;
|
---|
120 | return EOK;
|
---|
121 | }
|
---|
122 |
|
---|
123 | static errno_t sequential_read(bithenge_blob_t *base, aoff64_t offset,
|
---|
124 | char *buffer, aoff64_t *size)
|
---|
125 | {
|
---|
126 | bithenge_sequential_blob_t *blob = blob_as_sequential(base);
|
---|
127 | aoff64_t end = offset + *size;
|
---|
128 | if (end > blob->data_size) {
|
---|
129 | errno_t rc = sequential_buffer(blob, end);
|
---|
130 | if (rc != EOK)
|
---|
131 | return rc;
|
---|
132 | }
|
---|
133 | if (offset > blob->data_size)
|
---|
134 | return EINVAL;
|
---|
135 | *size = min(*size, blob->data_size - end);
|
---|
136 | memcpy(buffer, blob->buffer + offset, *size);
|
---|
137 | return EOK;
|
---|
138 | }
|
---|
139 |
|
---|
140 | static void sequential_destroy(bithenge_blob_t *base)
|
---|
141 | {
|
---|
142 | bithenge_sequential_blob_t *blob = blob_as_sequential(base);
|
---|
143 | free(blob->buffer);
|
---|
144 | blob->ops->destroy(blob);
|
---|
145 | }
|
---|
146 |
|
---|
147 | static const bithenge_random_access_blob_ops_t sequential_ops = {
|
---|
148 | .size = sequential_size,
|
---|
149 | .read = sequential_read,
|
---|
150 | .destroy = sequential_destroy,
|
---|
151 | };
|
---|
152 |
|
---|
153 | /** Initialize a sequential blob.
|
---|
154 | * @memberof bithenge_sequential_blob_t
|
---|
155 | * @param[out] blob The blob to initialize.
|
---|
156 | * @param[in] ops Operations providing sequential access support. This pointer
|
---|
157 | * must be valid until the blob is destroyed.
|
---|
158 | * @return EOK on success or an error code from errno.h.
|
---|
159 | */
|
---|
160 | errno_t bithenge_init_sequential_blob(bithenge_sequential_blob_t *blob,
|
---|
161 | const bithenge_sequential_blob_ops_t *ops)
|
---|
162 | {
|
---|
163 | assert(blob);
|
---|
164 | assert(ops);
|
---|
165 | assert(ops->destroy);
|
---|
166 | assert(ops->read);
|
---|
167 | // ops->size is optional
|
---|
168 |
|
---|
169 | errno_t rc = bithenge_init_random_access_blob(sequential_as_blob(blob),
|
---|
170 | &sequential_ops);
|
---|
171 | if (rc != EOK)
|
---|
172 | return rc;
|
---|
173 | blob->ops = ops;
|
---|
174 | blob->buffer = NULL; // realloc(NULL, ...) works like malloc
|
---|
175 | blob->buffer_size = 0;
|
---|
176 | blob->data_size = 0;
|
---|
177 | return EOK;
|
---|
178 | }
|
---|
179 |
|
---|
180 | typedef struct {
|
---|
181 | bithenge_blob_t base;
|
---|
182 | const char *buffer;
|
---|
183 | size_t size;
|
---|
184 | bool needs_free;
|
---|
185 | } memory_blob_t;
|
---|
186 |
|
---|
187 | static inline memory_blob_t *blob_as_memory(bithenge_blob_t *base)
|
---|
188 | {
|
---|
189 | return (memory_blob_t *)base;
|
---|
190 | }
|
---|
191 |
|
---|
192 | static inline bithenge_blob_t *memory_as_blob(memory_blob_t *blob)
|
---|
193 | {
|
---|
194 | return &blob->base;
|
---|
195 | }
|
---|
196 |
|
---|
197 | static errno_t memory_size(bithenge_blob_t *base, aoff64_t *size)
|
---|
198 | {
|
---|
199 | memory_blob_t *blob = blob_as_memory(base);
|
---|
200 | if (bithenge_should_fail())
|
---|
201 | return EIO;
|
---|
202 | *size = blob->size;
|
---|
203 | return EOK;
|
---|
204 | }
|
---|
205 |
|
---|
206 | static errno_t memory_read(bithenge_blob_t *base, aoff64_t offset, char *buffer,
|
---|
207 | aoff64_t *size)
|
---|
208 | {
|
---|
209 | memory_blob_t *blob = blob_as_memory(base);
|
---|
210 | if (offset > blob->size)
|
---|
211 | return ELIMIT;
|
---|
212 | *size = min(*size, blob->size - offset);
|
---|
213 | memcpy(buffer, blob->buffer + offset, *size);
|
---|
214 | return EOK;
|
---|
215 | }
|
---|
216 |
|
---|
217 | static void memory_destroy(bithenge_blob_t *base)
|
---|
218 | {
|
---|
219 | memory_blob_t *blob = blob_as_memory(base);
|
---|
220 | if (blob->needs_free)
|
---|
221 | free((void *)blob->buffer);
|
---|
222 | free(blob);
|
---|
223 | }
|
---|
224 |
|
---|
225 | static const bithenge_random_access_blob_ops_t memory_ops = {
|
---|
226 | .size = memory_size,
|
---|
227 | .read = memory_read,
|
---|
228 | .destroy = memory_destroy,
|
---|
229 | };
|
---|
230 |
|
---|
231 | /** Create a blob node from a buffer. The buffer must exist as long as the blob
|
---|
232 | * does. The blob must be freed with @a bithenge_node_t::bithenge_node_destroy
|
---|
233 | * after it is used.
|
---|
234 | * @memberof bithenge_blob_t
|
---|
235 | * @param[out] out Stores the created blob node.
|
---|
236 | * @param[in] buffer The buffer, which must not be changed until the blob is
|
---|
237 | * destroyed.
|
---|
238 | * @param len The length of the data.
|
---|
239 | * @param needs_free If true, the buffer will be freed with free() if this
|
---|
240 | * function fails or the blob is destroyed.
|
---|
241 | * @return EOK on success or an error code from errno.h.
|
---|
242 | */
|
---|
243 | errno_t bithenge_new_blob_from_buffer(bithenge_node_t **out, const void *buffer,
|
---|
244 | size_t len, bool needs_free)
|
---|
245 | {
|
---|
246 | errno_t rc;
|
---|
247 | assert(buffer || !len);
|
---|
248 |
|
---|
249 | memory_blob_t *blob = malloc(sizeof(*blob));
|
---|
250 | if (!blob) {
|
---|
251 | rc = ENOMEM;
|
---|
252 | goto error;
|
---|
253 | }
|
---|
254 | rc = bithenge_init_random_access_blob(memory_as_blob(blob),
|
---|
255 | &memory_ops);
|
---|
256 | if (rc != EOK)
|
---|
257 | goto error;
|
---|
258 | blob->buffer = buffer;
|
---|
259 | blob->size = len;
|
---|
260 | blob->needs_free = needs_free;
|
---|
261 | *out = bithenge_blob_as_node(memory_as_blob(blob));
|
---|
262 | return EOK;
|
---|
263 |
|
---|
264 | error:
|
---|
265 | if (needs_free)
|
---|
266 | free((void *)buffer);
|
---|
267 | free(blob);
|
---|
268 | return rc;
|
---|
269 | }
|
---|
270 |
|
---|
271 | /** Create a blob node from data. Unlike with @a
|
---|
272 | * bithenge_blob_t::bithenge_new_blob_from_buffer, the data is copied into a
|
---|
273 | * new buffer and the original data can be changed after this call. The blob
|
---|
274 | * must be freed with @a bithenge_node_t::bithenge_node_destroy after it is
|
---|
275 | * used.
|
---|
276 | * @memberof bithenge_blob_t
|
---|
277 | * @param[out] out Stores the created blob node.
|
---|
278 | * @param[in] data The data.
|
---|
279 | * @param len The length of the data.
|
---|
280 | * @return EOK on success or an error code from errno.h.
|
---|
281 | */
|
---|
282 | errno_t bithenge_new_blob_from_data(bithenge_node_t **out, const void *data,
|
---|
283 | size_t len)
|
---|
284 | {
|
---|
285 | char *buffer = malloc(len);
|
---|
286 | if (!buffer)
|
---|
287 | return ENOMEM;
|
---|
288 | memcpy(buffer, data, len);
|
---|
289 |
|
---|
290 | return bithenge_new_blob_from_buffer(out, buffer, len, true);
|
---|
291 | }
|
---|
292 |
|
---|
293 |
|
---|
294 |
|
---|
295 | typedef struct {
|
---|
296 | bithenge_blob_t base;
|
---|
297 | bithenge_blob_t *source;
|
---|
298 | aoff64_t offset;
|
---|
299 | aoff64_t size;
|
---|
300 | bool size_matters;
|
---|
301 | } subblob_t;
|
---|
302 |
|
---|
303 | static inline subblob_t *blob_as_subblob(bithenge_blob_t *base)
|
---|
304 | {
|
---|
305 | return (subblob_t *)base;
|
---|
306 | }
|
---|
307 |
|
---|
308 | static inline bithenge_blob_t *subblob_as_blob(subblob_t *blob)
|
---|
309 | {
|
---|
310 | return &blob->base;
|
---|
311 | }
|
---|
312 |
|
---|
313 | static errno_t subblob_size(bithenge_blob_t *base, aoff64_t *size)
|
---|
314 | {
|
---|
315 | subblob_t *blob = blob_as_subblob(base);
|
---|
316 | if (blob->size_matters) {
|
---|
317 | *size = blob->size;
|
---|
318 | return EOK;
|
---|
319 | } else {
|
---|
320 | errno_t rc = bithenge_blob_size(blob->source, size);
|
---|
321 | *size -= blob->offset;
|
---|
322 | return rc;
|
---|
323 | }
|
---|
324 | }
|
---|
325 |
|
---|
326 | static errno_t subblob_read(bithenge_blob_t *base, aoff64_t offset,
|
---|
327 | char *buffer, aoff64_t *size)
|
---|
328 | {
|
---|
329 | subblob_t *blob = blob_as_subblob(base);
|
---|
330 | if (blob->size_matters) {
|
---|
331 | if (offset > blob->size)
|
---|
332 | return EINVAL;
|
---|
333 | *size = min(*size, blob->size - offset);
|
---|
334 | }
|
---|
335 | offset += blob->offset;
|
---|
336 | return bithenge_blob_read(blob->source, offset, buffer, size);
|
---|
337 | }
|
---|
338 |
|
---|
339 | static errno_t subblob_read_bits(bithenge_blob_t *base, aoff64_t offset,
|
---|
340 | char *buffer, aoff64_t *size, bool little_endian)
|
---|
341 | {
|
---|
342 | subblob_t *blob = blob_as_subblob(base);
|
---|
343 | if (blob->size_matters) {
|
---|
344 | if (offset > blob->size)
|
---|
345 | return EINVAL;
|
---|
346 | *size = min(*size, blob->size - offset);
|
---|
347 | }
|
---|
348 | offset += blob->offset;
|
---|
349 | return bithenge_blob_read_bits(blob->source, offset, buffer, size,
|
---|
350 | little_endian);
|
---|
351 | }
|
---|
352 |
|
---|
353 | static void subblob_destroy(bithenge_blob_t *base)
|
---|
354 | {
|
---|
355 | subblob_t *blob = blob_as_subblob(base);
|
---|
356 | bithenge_blob_dec_ref(blob->source);
|
---|
357 | free(blob);
|
---|
358 | }
|
---|
359 |
|
---|
360 | static const bithenge_random_access_blob_ops_t subblob_ops = {
|
---|
361 | .size = subblob_size,
|
---|
362 | .read = subblob_read,
|
---|
363 | .read_bits = subblob_read_bits,
|
---|
364 | .destroy = subblob_destroy,
|
---|
365 | };
|
---|
366 |
|
---|
367 | static bool is_subblob(bithenge_blob_t *blob)
|
---|
368 | {
|
---|
369 | return blob->base.blob_ops == &subblob_ops;
|
---|
370 | }
|
---|
371 |
|
---|
372 | static errno_t new_subblob(bithenge_node_t **out, bithenge_blob_t *source,
|
---|
373 | aoff64_t offset, aoff64_t size, bool size_matters)
|
---|
374 | {
|
---|
375 | assert(out);
|
---|
376 | assert(source);
|
---|
377 | errno_t rc;
|
---|
378 | subblob_t *blob = 0;
|
---|
379 |
|
---|
380 | if (is_subblob(source)) {
|
---|
381 | /* We can do some optimizations this way */
|
---|
382 | if (!size_matters)
|
---|
383 | size = 0;
|
---|
384 | subblob_t *source_subblob = blob_as_subblob(source);
|
---|
385 | if (source_subblob->size_matters &&
|
---|
386 | offset + size > source_subblob->size) {
|
---|
387 | rc = EINVAL;
|
---|
388 | goto error;
|
---|
389 | }
|
---|
390 |
|
---|
391 | if (source->base.refs == 1) {
|
---|
392 | source_subblob->offset += offset;
|
---|
393 | source_subblob->size -= offset;
|
---|
394 | if (size_matters) {
|
---|
395 | source_subblob->size_matters = true;
|
---|
396 | source_subblob->size = size;
|
---|
397 | }
|
---|
398 | *out = bithenge_blob_as_node(source);
|
---|
399 | return EOK;
|
---|
400 | }
|
---|
401 |
|
---|
402 | if (!size_matters && source_subblob->size_matters) {
|
---|
403 | size_matters = true;
|
---|
404 | size = source_subblob->size - offset;
|
---|
405 | }
|
---|
406 | offset += source_subblob->offset;
|
---|
407 | source = source_subblob->source;
|
---|
408 | bithenge_blob_inc_ref(source);
|
---|
409 | bithenge_blob_dec_ref(subblob_as_blob(source_subblob));
|
---|
410 | }
|
---|
411 |
|
---|
412 | blob = malloc(sizeof(*blob));
|
---|
413 | if (!blob) {
|
---|
414 | rc = ENOMEM;
|
---|
415 | goto error;
|
---|
416 | }
|
---|
417 | rc = bithenge_init_random_access_blob(subblob_as_blob(blob),
|
---|
418 | &subblob_ops);
|
---|
419 | if (rc != EOK)
|
---|
420 | goto error;
|
---|
421 | blob->source = source;
|
---|
422 | blob->offset = offset;
|
---|
423 | blob->size = size;
|
---|
424 | blob->size_matters = size_matters;
|
---|
425 | *out = bithenge_blob_as_node(subblob_as_blob(blob));
|
---|
426 | return EOK;
|
---|
427 |
|
---|
428 | error:
|
---|
429 | bithenge_blob_dec_ref(source);
|
---|
430 | free(blob);
|
---|
431 | return rc;
|
---|
432 | }
|
---|
433 |
|
---|
434 | /** Create a blob from data offset within another blob. This function takes
|
---|
435 | * ownership of a reference to @a blob.
|
---|
436 | * @param[out] out Stores the created blob node. On error, this is unchanged.
|
---|
437 | * @param[in] source The input blob.
|
---|
438 | * @param offset The offset within the input blob at which the new blob will start.
|
---|
439 | * @return EOK on success or an error code from errno.h.
|
---|
440 | */
|
---|
441 | errno_t bithenge_new_offset_blob(bithenge_node_t **out, bithenge_blob_t *source,
|
---|
442 | aoff64_t offset)
|
---|
443 | {
|
---|
444 | return new_subblob(out, source, offset, 0, false);
|
---|
445 | }
|
---|
446 |
|
---|
447 | /** Create a blob from part of another blob. This function takes ownership of a
|
---|
448 | * reference to @a blob.
|
---|
449 | * @param[out] out Stores the created blob node. On error, this is unchanged.
|
---|
450 | * @param[in] source The input blob.
|
---|
451 | * @param offset The offset within the input blob at which the new blob will start.
|
---|
452 | * @param size The size of the new blob.
|
---|
453 | * @return EOK on success or an error code from errno.h.
|
---|
454 | */
|
---|
455 | errno_t bithenge_new_subblob(bithenge_node_t **out, bithenge_blob_t *source,
|
---|
456 | aoff64_t offset, aoff64_t size)
|
---|
457 | {
|
---|
458 | return new_subblob(out, source, offset, size, true);
|
---|
459 | }
|
---|
460 |
|
---|
461 | /** Check whether the contents of two blobs are equal.
|
---|
462 | * @memberof bithenge_blob_t
|
---|
463 | * @param[out] out Holds whether the blobs are equal.
|
---|
464 | * @param a, b Blobs to compare.
|
---|
465 | * @return EOK on success, or an error code from errno.h.
|
---|
466 | */
|
---|
467 | errno_t bithenge_blob_equal(bool *out, bithenge_blob_t *a, bithenge_blob_t *b)
|
---|
468 | {
|
---|
469 | assert(a);
|
---|
470 | assert(a->base.blob_ops);
|
---|
471 | assert(b);
|
---|
472 | assert(b->base.blob_ops);
|
---|
473 | errno_t rc;
|
---|
474 | char buffer_a[4096], buffer_b[4096];
|
---|
475 | aoff64_t offset = 0, size_a = sizeof(buffer_a), size_b = sizeof(buffer_b);
|
---|
476 | do {
|
---|
477 | rc = bithenge_blob_read(a, offset, buffer_a, &size_a);
|
---|
478 | if (rc != EOK)
|
---|
479 | return rc;
|
---|
480 | rc = bithenge_blob_read(b, offset, buffer_b, &size_b);
|
---|
481 | if (rc != EOK)
|
---|
482 | return rc;
|
---|
483 | if (size_a != size_b || memcmp(buffer_a, buffer_b, size_a) != 0) {
|
---|
484 | *out = false;
|
---|
485 | return EOK;
|
---|
486 | }
|
---|
487 | offset += size_a;
|
---|
488 | } while (size_a == sizeof(buffer_a));
|
---|
489 | *out = true;
|
---|
490 | return EOK;
|
---|
491 | }
|
---|
492 |
|
---|
493 | /** @}
|
---|
494 | */
|
---|