source: mainline/uspace/app/bithenge/blob.c@ 04a7435f

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 04a7435f was 04a7435f, checked in by Sean Bartell <wingedtachikoma@…>, 13 years ago

Bithenge: add the struct transform

  • Property mode set to 100644
File size: 12.7 KB
Line 
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 */
51int bithenge_new_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);
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
66static 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
87static inline bithenge_sequential_blob_t *sequential_from_blob(
88 bithenge_blob_t *base)
89{
90 return (bithenge_sequential_blob_t *)base;
91}
92
93static inline bithenge_blob_t *blob_from_sequential(
94 bithenge_sequential_blob_t *blob)
95{
96 return &blob->base;
97}
98
99static int sequential_size(bithenge_blob_t *base, aoff64_t *size)
100{
101 bithenge_sequential_blob_t *blob = sequential_from_blob(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
120static int sequential_read(bithenge_blob_t *base, aoff64_t offset,
121 char *buffer, aoff64_t *size)
122{
123 bithenge_sequential_blob_t *blob = sequential_from_blob(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
137static int sequential_destroy(bithenge_blob_t *base)
138{
139 bithenge_sequential_blob_t *blob = sequential_from_blob(base);
140 free(blob->buffer);
141 return blob->ops->destroy(blob);
142}
143
144static 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 */
157int bithenge_new_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_new_random_access_blob(blob_from_sequential(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
177typedef struct {
178 bithenge_blob_t base;
179 const char *buffer;
180 size_t size;
181 bool needs_free;
182} memory_blob_t;
183
184static inline memory_blob_t *memory_from_blob(bithenge_blob_t *base)
185{
186 return (memory_blob_t *)base;
187}
188
189static inline bithenge_blob_t *blob_from_memory(memory_blob_t *blob)
190{
191 return &blob->base;
192}
193
194static int memory_size(bithenge_blob_t *base, aoff64_t *size)
195{
196 memory_blob_t *blob = memory_from_blob(base);
197 *size = blob->size;
198 return EOK;
199}
200
201static int memory_read(bithenge_blob_t *base, aoff64_t offset, char *buffer,
202 aoff64_t *size)
203{
204 memory_blob_t *blob = memory_from_blob(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
212static int memory_destroy(bithenge_blob_t *base)
213{
214 memory_blob_t *blob = memory_from_blob(base);
215 if (blob->needs_free)
216 free((void *)blob->buffer);
217 free(blob);
218 return EOK;
219}
220
221static const bithenge_random_access_blob_ops_t memory_ops = {
222 .size = memory_size,
223 .read = memory_read,
224 .destroy = memory_destroy,
225};
226
227/** Create a blob node from data. Unlike with @a
228 * bithenge_blob_t::bithenge_new_blob_from_buffer, the data is copied into a
229 * new buffer and the original data can be changed after this call. The blob
230 * must be freed with @a bithenge_node_t::bithenge_node_destroy after it is
231 * used.
232 * @memberof bithenge_blob_t
233 * @param[out] out Stores the created blob node.
234 * @param[in] data The data.
235 * @param len The length of the data.
236 * @return EOK on success or an error code from errno.h. */
237int bithenge_new_blob_from_data(bithenge_node_t **out, const void *data,
238 size_t len)
239{
240 int rc;
241 assert(data || !len);
242
243 memory_blob_t *blob = malloc(sizeof(*blob));
244 if (!blob)
245 return ENOMEM;
246 rc = bithenge_new_random_access_blob(blob_from_memory(blob),
247 &memory_ops);
248 if (rc != EOK) {
249 free(blob);
250 return rc;
251 }
252 char *buffer = malloc(len);
253 if (!buffer) {
254 free(blob);
255 return rc;
256 }
257 memcpy(buffer, data, len);
258 blob->buffer = buffer;
259 blob->size = len;
260 blob->needs_free = true;
261 *out = bithenge_blob_as_node(blob_from_memory(blob));
262 return EOK;
263}
264
265/** Create a blob node from a buffer. The buffer must exist as long as the blob
266 * does. The blob must be freed with @a bithenge_node_t::bithenge_node_destroy
267 * after it is used.
268 * @memberof bithenge_blob_t
269 * @param[out] out Stores the created blob node.
270 * @param[in] buffer The buffer, which must not be changed until the blob is
271 * destroyed.
272 * @param len The length of the data.
273 * @param needs_free If true, the buffer will be freed with free() if this
274 * function fails or the blob is destroyed.
275 * @return EOK on success or an error code from errno.h. */
276int bithenge_new_blob_from_buffer(bithenge_node_t **out, const void *buffer,
277 size_t len, bool needs_free)
278{
279 int rc;
280 assert(buffer || !len);
281
282 memory_blob_t *blob = malloc(sizeof(*blob));
283 if (!blob)
284 return ENOMEM;
285 rc = bithenge_new_random_access_blob(blob_from_memory(blob),
286 &memory_ops);
287 if (rc != EOK) {
288 free(blob);
289 return rc;
290 }
291 blob->buffer = buffer;
292 blob->size = len;
293 blob->needs_free = needs_free;
294 *out = bithenge_blob_as_node(blob_from_memory(blob));
295 return EOK;
296}
297
298typedef struct {
299 bithenge_blob_t base;
300 bithenge_blob_t *source;
301 aoff64_t offset;
302 aoff64_t size;
303 bool size_matters;
304} subblob_t;
305
306static inline subblob_t *blob_as_subblob(bithenge_blob_t *base)
307{
308 return (subblob_t *)base;
309}
310
311static inline bithenge_blob_t *subblob_as_blob(subblob_t *blob)
312{
313 return &blob->base;
314}
315
316static int subblob_size(bithenge_blob_t *base, aoff64_t *size)
317{
318 subblob_t *blob = blob_as_subblob(base);
319 if (blob->size_matters) {
320 *size = blob->size;
321 return EOK;
322 } else {
323 int rc = bithenge_blob_size(blob->source, size);
324 *size -= blob->offset;
325 return rc;
326 }
327}
328
329static int subblob_read(bithenge_blob_t *base, aoff64_t offset,
330 char *buffer, aoff64_t *size)
331{
332 subblob_t *blob = blob_as_subblob(base);
333 if (blob->size_matters) {
334 if (offset > blob->size)
335 return EINVAL;
336 *size = min(*size, blob->size - offset);
337 }
338 offset += blob->offset;
339 return bithenge_blob_read(blob->source, offset, buffer, size);
340}
341
342static int subblob_destroy(bithenge_blob_t *base)
343{
344 subblob_t *blob = blob_as_subblob(base);
345 bithenge_blob_dec_ref(blob->source);
346 free(blob);
347 return EOK;
348}
349
350static const bithenge_random_access_blob_ops_t subblob_ops = {
351 .size = subblob_size,
352 .read = subblob_read,
353 .destroy = subblob_destroy,
354};
355
356static bool is_subblob(bithenge_blob_t *blob)
357{
358 return blob->base.blob_ops == &subblob_ops;
359}
360
361static int new_subblob(bithenge_node_t **out, bithenge_blob_t *source,
362 aoff64_t offset, aoff64_t size, bool size_matters)
363{
364 assert(out);
365 assert(source);
366 int rc;
367 subblob_t *blob = 0;
368
369 if (is_subblob(source)) {
370 /* We can do some optimizations this way */
371 if (!size_matters)
372 size = 0;
373 subblob_t *source_subblob = blob_as_subblob(source);
374 if (source_subblob->size_matters &&
375 offset + size > source_subblob->size) {
376 rc = EINVAL;
377 goto error;
378 }
379
380 if (source->base.refs == 1) {
381 source_subblob->offset += offset;
382 source_subblob->size -= offset;
383 if (size_matters) {
384 source_subblob->size_matters = true;
385 source_subblob->size = size;
386 }
387 *out = bithenge_blob_as_node(source);
388 return EOK;
389 }
390
391 if (!size_matters && source_subblob->size_matters) {
392 size_matters = true;
393 size = source_subblob->size - offset;
394 }
395 offset += source_subblob->offset;
396 source = source_subblob->source;
397 bithenge_blob_inc_ref(source);
398 bithenge_blob_dec_ref(subblob_as_blob(source_subblob));
399 }
400
401 blob = malloc(sizeof(*blob));
402 if (!blob) {
403 rc = ENOMEM;
404 goto error;
405 }
406 rc = bithenge_new_random_access_blob(subblob_as_blob(blob),
407 &subblob_ops);
408 if (rc != EOK)
409 goto error;
410 blob->source = source;
411 blob->offset = offset;
412 blob->size = size;
413 blob->size_matters = size_matters;
414 *out = bithenge_blob_as_node(subblob_as_blob(blob));
415 return EOK;
416
417error:
418 bithenge_blob_dec_ref(source);
419 free(blob);
420 return rc;
421}
422
423/** Create a blob from data offset within another blob. This function takes
424 * ownership of a reference to @a blob.
425 * @param[out] out Stores the created blob node. On error, this is unchanged.
426 * @param[in] source The input blob.
427 * @param offset The offset within the input blob at which the new blob will start.
428 * @return EOK on success or an error code from errno.h. */
429int bithenge_new_offset_blob(bithenge_node_t **out, bithenge_blob_t *source,
430 aoff64_t offset)
431{
432 return new_subblob(out, source, offset, 0, false);
433}
434
435/** Create a blob from part of another blob. This function takes ownership of a
436 * reference to @a blob.
437 * @param[out] out Stores the created blob node. On error, this is unchanged.
438 * @param[in] source The input blob.
439 * @param offset The offset within the input blob at which the new blob will start.
440 * @param size The size of the new blob.
441 * @return EOK on success or an error code from errno.h. */
442int bithenge_new_subblob(bithenge_node_t **out, bithenge_blob_t *source,
443 aoff64_t offset, aoff64_t size)
444{
445 return new_subblob(out, source, offset, size, true);
446}
447
448/** Check whether the contents of two blobs are equal.
449 * @memberof bithenge_blob_t
450 * @param a, b Blobs to compare.
451 * @return Whether the blobs are equal. If an error occurs, returns false.
452 */
453bool bithenge_blob_equal(bithenge_blob_t *a, bithenge_blob_t *b)
454{
455 assert(a);
456 assert(a->base.blob_ops);
457 assert(b);
458 assert(b->base.blob_ops);
459 int rc;
460 char buffer_a[4096], buffer_b[4096];
461 aoff64_t offset = 0, size_a = sizeof(buffer_a), size_b = sizeof(buffer_b);
462 do {
463 rc = bithenge_blob_read(a, offset, buffer_a, &size_a);
464 if (rc != EOK)
465 return false;
466 rc = bithenge_blob_read(b, offset, buffer_b, &size_b);
467 if (rc != EOK)
468 return false;
469 if (size_a != size_b || bcmp(buffer_a, buffer_b, size_a))
470 return false;
471 offset += size_a;
472 } while (size_a == sizeof(buffer_a));
473 return true;
474}
475
476/** @}
477 */
Note: See TracBrowser for help on using the repository browser.