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 "blob.h"
|
---|
41 | #include "os.h"
|
---|
42 | #include "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 | int 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 | blob->base.type = BITHENGE_NODE_BLOB;
|
---|
61 | blob->base.refs = 1;
|
---|
62 | blob->base.blob_ops = ops;
|
---|
63 | return EOK;
|
---|
64 | }
|
---|
65 |
|
---|
66 | static int sequential_buffer(bithenge_sequential_blob_t *blob, aoff64_t end)
|
---|
67 | {
|
---|
68 | bool need_realloc = false;
|
---|
69 | while (end > blob->buffer_size) {
|
---|
70 | blob->buffer_size = max(4096, 2 * blob->buffer_size);
|
---|
71 | need_realloc = true;
|
---|
72 | }
|
---|
73 | if (need_realloc) {
|
---|
74 | char *buffer = realloc(blob->buffer, blob->buffer_size);
|
---|
75 | if (!buffer)
|
---|
76 | return ENOMEM;
|
---|
77 | blob->buffer = buffer;
|
---|
78 | }
|
---|
79 | aoff64_t size = end - blob->data_size;
|
---|
80 | int rc = blob->ops->read(blob, blob->buffer + blob->data_size, &size);
|
---|
81 | if (rc != EOK)
|
---|
82 | return rc;
|
---|
83 | blob->data_size += size;
|
---|
84 | return EOK;
|
---|
85 | }
|
---|
86 |
|
---|
87 | static inline bithenge_sequential_blob_t *blob_as_sequential(
|
---|
88 | bithenge_blob_t *base)
|
---|
89 | {
|
---|
90 | return (bithenge_sequential_blob_t *)base;
|
---|
91 | }
|
---|
92 |
|
---|
93 | static inline bithenge_blob_t *sequential_as_blob(
|
---|
94 | bithenge_sequential_blob_t *blob)
|
---|
95 | {
|
---|
96 | return &blob->base;
|
---|
97 | }
|
---|
98 |
|
---|
99 | static int sequential_size(bithenge_blob_t *base, aoff64_t *size)
|
---|
100 | {
|
---|
101 | bithenge_sequential_blob_t *blob = blob_as_sequential(base);
|
---|
102 | int rc;
|
---|
103 | if (blob->ops->size) {
|
---|
104 | rc = blob->ops->size(blob, size);
|
---|
105 | if (rc == EOK)
|
---|
106 | return EOK;
|
---|
107 | }
|
---|
108 | rc = sequential_buffer(blob, blob->buffer_size);
|
---|
109 | if (rc != EOK)
|
---|
110 | return rc;
|
---|
111 | while (blob->data_size == blob->buffer_size) {
|
---|
112 | rc = sequential_buffer(blob, 2 * blob->buffer_size);
|
---|
113 | if (rc != EOK)
|
---|
114 | return rc;
|
---|
115 | }
|
---|
116 | *size = blob->data_size;
|
---|
117 | return EOK;
|
---|
118 | }
|
---|
119 |
|
---|
120 | static int sequential_read(bithenge_blob_t *base, aoff64_t offset,
|
---|
121 | char *buffer, aoff64_t *size)
|
---|
122 | {
|
---|
123 | bithenge_sequential_blob_t *blob = blob_as_sequential(base);
|
---|
124 | aoff64_t end = offset + *size;
|
---|
125 | if (end > blob->data_size) {
|
---|
126 | int rc = sequential_buffer(blob, end);
|
---|
127 | if (rc != EOK)
|
---|
128 | return rc;
|
---|
129 | }
|
---|
130 | if (offset > blob->data_size)
|
---|
131 | return EINVAL;
|
---|
132 | *size = min(*size, blob->data_size - end);
|
---|
133 | memcpy(buffer, blob->buffer + offset, *size);
|
---|
134 | return EOK;
|
---|
135 | }
|
---|
136 |
|
---|
137 | static void sequential_destroy(bithenge_blob_t *base)
|
---|
138 | {
|
---|
139 | bithenge_sequential_blob_t *blob = blob_as_sequential(base);
|
---|
140 | free(blob->buffer);
|
---|
141 | blob->ops->destroy(blob);
|
---|
142 | }
|
---|
143 |
|
---|
144 | static const bithenge_random_access_blob_ops_t sequential_ops = {
|
---|
145 | .size = sequential_size,
|
---|
146 | .read = sequential_read,
|
---|
147 | .destroy = sequential_destroy,
|
---|
148 | };
|
---|
149 |
|
---|
150 | /** Initialize a sequential blob.
|
---|
151 | * @memberof bithenge_sequential_blob_t
|
---|
152 | * @param[out] blob The blob to initialize.
|
---|
153 | * @param[in] ops Operations providing sequential access support. This pointer
|
---|
154 | * must be valid until the blob is destroyed.
|
---|
155 | * @return EOK on success or an error code from errno.h.
|
---|
156 | */
|
---|
157 | int bithenge_init_sequential_blob(bithenge_sequential_blob_t *blob,
|
---|
158 | const bithenge_sequential_blob_ops_t *ops)
|
---|
159 | {
|
---|
160 | assert(blob);
|
---|
161 | assert(ops);
|
---|
162 | assert(ops->destroy);
|
---|
163 | assert(ops->read);
|
---|
164 | // ops->size is optional
|
---|
165 |
|
---|
166 | int rc = bithenge_init_random_access_blob(sequential_as_blob(blob),
|
---|
167 | &sequential_ops);
|
---|
168 | if (rc != EOK)
|
---|
169 | return rc;
|
---|
170 | blob->ops = ops;
|
---|
171 | blob->buffer = NULL; // realloc(NULL, ...) works like malloc
|
---|
172 | blob->buffer_size = 0;
|
---|
173 | blob->data_size = 0;
|
---|
174 | return EOK;
|
---|
175 | }
|
---|
176 |
|
---|
177 | typedef struct {
|
---|
178 | bithenge_blob_t base;
|
---|
179 | const char *buffer;
|
---|
180 | size_t size;
|
---|
181 | bool needs_free;
|
---|
182 | } memory_blob_t;
|
---|
183 |
|
---|
184 | static inline memory_blob_t *blob_as_memory(bithenge_blob_t *base)
|
---|
185 | {
|
---|
186 | return (memory_blob_t *)base;
|
---|
187 | }
|
---|
188 |
|
---|
189 | static inline bithenge_blob_t *memory_as_blob(memory_blob_t *blob)
|
---|
190 | {
|
---|
191 | return &blob->base;
|
---|
192 | }
|
---|
193 |
|
---|
194 | static int memory_size(bithenge_blob_t *base, aoff64_t *size)
|
---|
195 | {
|
---|
196 | memory_blob_t *blob = blob_as_memory(base);
|
---|
197 | *size = blob->size;
|
---|
198 | return EOK;
|
---|
199 | }
|
---|
200 |
|
---|
201 | static int memory_read(bithenge_blob_t *base, aoff64_t offset, char *buffer,
|
---|
202 | aoff64_t *size)
|
---|
203 | {
|
---|
204 | memory_blob_t *blob = blob_as_memory(base);
|
---|
205 | if (offset > blob->size)
|
---|
206 | return ELIMIT;
|
---|
207 | *size = min(*size, blob->size - offset);
|
---|
208 | memcpy(buffer, blob->buffer + offset, *size);
|
---|
209 | return EOK;
|
---|
210 | }
|
---|
211 |
|
---|
212 | static void memory_destroy(bithenge_blob_t *base)
|
---|
213 | {
|
---|
214 | memory_blob_t *blob = blob_as_memory(base);
|
---|
215 | if (blob->needs_free)
|
---|
216 | free((void *)blob->buffer);
|
---|
217 | free(blob);
|
---|
218 | }
|
---|
219 |
|
---|
220 | static const bithenge_random_access_blob_ops_t memory_ops = {
|
---|
221 | .size = memory_size,
|
---|
222 | .read = memory_read,
|
---|
223 | .destroy = memory_destroy,
|
---|
224 | };
|
---|
225 |
|
---|
226 | /** Create a blob node from data. Unlike with @a
|
---|
227 | * bithenge_blob_t::bithenge_new_blob_from_buffer, the data is copied into a
|
---|
228 | * new buffer and the original data can be changed after this call. The blob
|
---|
229 | * must be freed with @a bithenge_node_t::bithenge_node_destroy after it is
|
---|
230 | * used.
|
---|
231 | * @memberof bithenge_blob_t
|
---|
232 | * @param[out] out Stores the created blob node.
|
---|
233 | * @param[in] data The data.
|
---|
234 | * @param len The length of the data.
|
---|
235 | * @return EOK on success or an error code from errno.h. */
|
---|
236 | int bithenge_new_blob_from_data(bithenge_node_t **out, const void *data,
|
---|
237 | size_t len)
|
---|
238 | {
|
---|
239 | int rc;
|
---|
240 | assert(data || !len);
|
---|
241 |
|
---|
242 | memory_blob_t *blob = malloc(sizeof(*blob));
|
---|
243 | if (!blob)
|
---|
244 | return ENOMEM;
|
---|
245 | rc = bithenge_init_random_access_blob(memory_as_blob(blob),
|
---|
246 | &memory_ops);
|
---|
247 | if (rc != EOK) {
|
---|
248 | free(blob);
|
---|
249 | return rc;
|
---|
250 | }
|
---|
251 | char *buffer = malloc(len);
|
---|
252 | if (!buffer) {
|
---|
253 | free(blob);
|
---|
254 | return rc;
|
---|
255 | }
|
---|
256 | memcpy(buffer, data, len);
|
---|
257 | blob->buffer = buffer;
|
---|
258 | blob->size = len;
|
---|
259 | blob->needs_free = true;
|
---|
260 | *out = bithenge_blob_as_node(memory_as_blob(blob));
|
---|
261 | return EOK;
|
---|
262 | }
|
---|
263 |
|
---|
264 | /** Create a blob node from a buffer. The buffer must exist as long as the blob
|
---|
265 | * does. The blob must be freed with @a bithenge_node_t::bithenge_node_destroy
|
---|
266 | * after it is used.
|
---|
267 | * @memberof bithenge_blob_t
|
---|
268 | * @param[out] out Stores the created blob node.
|
---|
269 | * @param[in] buffer The buffer, which must not be changed until the blob is
|
---|
270 | * destroyed.
|
---|
271 | * @param len The length of the data.
|
---|
272 | * @param needs_free If true, the buffer will be freed with free() if this
|
---|
273 | * function fails or the blob is destroyed.
|
---|
274 | * @return EOK on success or an error code from errno.h. */
|
---|
275 | int bithenge_new_blob_from_buffer(bithenge_node_t **out, const void *buffer,
|
---|
276 | size_t len, bool needs_free)
|
---|
277 | {
|
---|
278 | int rc;
|
---|
279 | assert(buffer || !len);
|
---|
280 |
|
---|
281 | memory_blob_t *blob = malloc(sizeof(*blob));
|
---|
282 | if (!blob)
|
---|
283 | return ENOMEM;
|
---|
284 | rc = bithenge_init_random_access_blob(memory_as_blob(blob),
|
---|
285 | &memory_ops);
|
---|
286 | if (rc != EOK) {
|
---|
287 | free(blob);
|
---|
288 | return rc;
|
---|
289 | }
|
---|
290 | blob->buffer = buffer;
|
---|
291 | blob->size = len;
|
---|
292 | blob->needs_free = needs_free;
|
---|
293 | *out = bithenge_blob_as_node(memory_as_blob(blob));
|
---|
294 | return EOK;
|
---|
295 | }
|
---|
296 |
|
---|
297 | typedef struct {
|
---|
298 | bithenge_blob_t base;
|
---|
299 | bithenge_blob_t *source;
|
---|
300 | aoff64_t offset;
|
---|
301 | aoff64_t size;
|
---|
302 | bool size_matters;
|
---|
303 | } subblob_t;
|
---|
304 |
|
---|
305 | static inline subblob_t *blob_as_subblob(bithenge_blob_t *base)
|
---|
306 | {
|
---|
307 | return (subblob_t *)base;
|
---|
308 | }
|
---|
309 |
|
---|
310 | static inline bithenge_blob_t *subblob_as_blob(subblob_t *blob)
|
---|
311 | {
|
---|
312 | return &blob->base;
|
---|
313 | }
|
---|
314 |
|
---|
315 | static int subblob_size(bithenge_blob_t *base, aoff64_t *size)
|
---|
316 | {
|
---|
317 | subblob_t *blob = blob_as_subblob(base);
|
---|
318 | if (blob->size_matters) {
|
---|
319 | *size = blob->size;
|
---|
320 | return EOK;
|
---|
321 | } else {
|
---|
322 | int rc = bithenge_blob_size(blob->source, size);
|
---|
323 | *size -= blob->offset;
|
---|
324 | return rc;
|
---|
325 | }
|
---|
326 | }
|
---|
327 |
|
---|
328 | static int subblob_read(bithenge_blob_t *base, aoff64_t offset,
|
---|
329 | char *buffer, aoff64_t *size)
|
---|
330 | {
|
---|
331 | subblob_t *blob = blob_as_subblob(base);
|
---|
332 | if (blob->size_matters) {
|
---|
333 | if (offset > blob->size)
|
---|
334 | return EINVAL;
|
---|
335 | *size = min(*size, blob->size - offset);
|
---|
336 | }
|
---|
337 | offset += blob->offset;
|
---|
338 | return bithenge_blob_read(blob->source, offset, buffer, size);
|
---|
339 | }
|
---|
340 |
|
---|
341 | static int subblob_read_bits(bithenge_blob_t *base, aoff64_t offset,
|
---|
342 | char *buffer, aoff64_t *size, bool little_endian)
|
---|
343 | {
|
---|
344 | subblob_t *blob = blob_as_subblob(base);
|
---|
345 | if (blob->size_matters) {
|
---|
346 | if (offset > blob->size)
|
---|
347 | return EINVAL;
|
---|
348 | *size = min(*size, blob->size - offset);
|
---|
349 | }
|
---|
350 | offset += blob->offset;
|
---|
351 | return bithenge_blob_read_bits(blob->source, offset, buffer, size,
|
---|
352 | little_endian);
|
---|
353 | }
|
---|
354 |
|
---|
355 | static void subblob_destroy(bithenge_blob_t *base)
|
---|
356 | {
|
---|
357 | subblob_t *blob = blob_as_subblob(base);
|
---|
358 | bithenge_blob_dec_ref(blob->source);
|
---|
359 | free(blob);
|
---|
360 | }
|
---|
361 |
|
---|
362 | static const bithenge_random_access_blob_ops_t subblob_ops = {
|
---|
363 | .size = subblob_size,
|
---|
364 | .read = subblob_read,
|
---|
365 | .read_bits = subblob_read_bits,
|
---|
366 | .destroy = subblob_destroy,
|
---|
367 | };
|
---|
368 |
|
---|
369 | static bool is_subblob(bithenge_blob_t *blob)
|
---|
370 | {
|
---|
371 | return blob->base.blob_ops == &subblob_ops;
|
---|
372 | }
|
---|
373 |
|
---|
374 | static int new_subblob(bithenge_node_t **out, bithenge_blob_t *source,
|
---|
375 | aoff64_t offset, aoff64_t size, bool size_matters)
|
---|
376 | {
|
---|
377 | assert(out);
|
---|
378 | assert(source);
|
---|
379 | int rc;
|
---|
380 | subblob_t *blob = 0;
|
---|
381 |
|
---|
382 | if (is_subblob(source)) {
|
---|
383 | /* We can do some optimizations this way */
|
---|
384 | if (!size_matters)
|
---|
385 | size = 0;
|
---|
386 | subblob_t *source_subblob = blob_as_subblob(source);
|
---|
387 | if (source_subblob->size_matters &&
|
---|
388 | offset + size > source_subblob->size) {
|
---|
389 | rc = EINVAL;
|
---|
390 | goto error;
|
---|
391 | }
|
---|
392 |
|
---|
393 | if (source->base.refs == 1) {
|
---|
394 | source_subblob->offset += offset;
|
---|
395 | source_subblob->size -= offset;
|
---|
396 | if (size_matters) {
|
---|
397 | source_subblob->size_matters = true;
|
---|
398 | source_subblob->size = size;
|
---|
399 | }
|
---|
400 | *out = bithenge_blob_as_node(source);
|
---|
401 | return EOK;
|
---|
402 | }
|
---|
403 |
|
---|
404 | if (!size_matters && source_subblob->size_matters) {
|
---|
405 | size_matters = true;
|
---|
406 | size = source_subblob->size - offset;
|
---|
407 | }
|
---|
408 | offset += source_subblob->offset;
|
---|
409 | source = source_subblob->source;
|
---|
410 | bithenge_blob_inc_ref(source);
|
---|
411 | bithenge_blob_dec_ref(subblob_as_blob(source_subblob));
|
---|
412 | }
|
---|
413 |
|
---|
414 | blob = malloc(sizeof(*blob));
|
---|
415 | if (!blob) {
|
---|
416 | rc = ENOMEM;
|
---|
417 | goto error;
|
---|
418 | }
|
---|
419 | rc = bithenge_init_random_access_blob(subblob_as_blob(blob),
|
---|
420 | &subblob_ops);
|
---|
421 | if (rc != EOK)
|
---|
422 | goto error;
|
---|
423 | blob->source = source;
|
---|
424 | blob->offset = offset;
|
---|
425 | blob->size = size;
|
---|
426 | blob->size_matters = size_matters;
|
---|
427 | *out = bithenge_blob_as_node(subblob_as_blob(blob));
|
---|
428 | return EOK;
|
---|
429 |
|
---|
430 | error:
|
---|
431 | bithenge_blob_dec_ref(source);
|
---|
432 | free(blob);
|
---|
433 | return rc;
|
---|
434 | }
|
---|
435 |
|
---|
436 | /** Create a blob from data offset within another blob. This function takes
|
---|
437 | * ownership of a reference to @a blob.
|
---|
438 | * @param[out] out Stores the created blob node. On error, this is unchanged.
|
---|
439 | * @param[in] source The input blob.
|
---|
440 | * @param offset The offset within the input blob at which the new blob will start.
|
---|
441 | * @return EOK on success or an error code from errno.h. */
|
---|
442 | int bithenge_new_offset_blob(bithenge_node_t **out, bithenge_blob_t *source,
|
---|
443 | aoff64_t offset)
|
---|
444 | {
|
---|
445 | return new_subblob(out, source, offset, 0, false);
|
---|
446 | }
|
---|
447 |
|
---|
448 | /** Create a blob from part of another blob. This function takes ownership of a
|
---|
449 | * reference to @a blob.
|
---|
450 | * @param[out] out Stores the created blob node. On error, this is unchanged.
|
---|
451 | * @param[in] source The input blob.
|
---|
452 | * @param offset The offset within the input blob at which the new blob will start.
|
---|
453 | * @param size The size of the new blob.
|
---|
454 | * @return EOK on success or an error code from errno.h. */
|
---|
455 | int 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 a, b Blobs to compare.
|
---|
464 | * @return Whether the blobs are equal. If an error occurs, returns false.
|
---|
465 | */
|
---|
466 | bool bithenge_blob_equal(bithenge_blob_t *a, bithenge_blob_t *b)
|
---|
467 | {
|
---|
468 | assert(a);
|
---|
469 | assert(a->base.blob_ops);
|
---|
470 | assert(b);
|
---|
471 | assert(b->base.blob_ops);
|
---|
472 | int rc;
|
---|
473 | char buffer_a[4096], buffer_b[4096];
|
---|
474 | aoff64_t offset = 0, size_a = sizeof(buffer_a), size_b = sizeof(buffer_b);
|
---|
475 | do {
|
---|
476 | rc = bithenge_blob_read(a, offset, buffer_a, &size_a);
|
---|
477 | if (rc != EOK)
|
---|
478 | return false;
|
---|
479 | rc = bithenge_blob_read(b, offset, buffer_b, &size_b);
|
---|
480 | if (rc != EOK)
|
---|
481 | return false;
|
---|
482 | if (size_a != size_b || bcmp(buffer_a, buffer_b, size_a))
|
---|
483 | return false;
|
---|
484 | offset += size_a;
|
---|
485 | } while (size_a == sizeof(buffer_a));
|
---|
486 | return true;
|
---|
487 | }
|
---|
488 |
|
---|
489 | /** @}
|
---|
490 | */
|
---|