[a54bd98] | 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 |
|
---|
[743ce51] | 37 | #include <assert.h>
|
---|
[a54bd98] | 38 | #include <errno.h>
|
---|
| 39 | #include <stdlib.h>
|
---|
[6cd10ac] | 40 | #include "common.h"
|
---|
[8fc0f47c] | 41 | #include <bithenge/blob.h>
|
---|
| 42 | #include <bithenge/tree.h>
|
---|
[a54bd98] | 43 |
|
---|
| 44 | /** Initialize a random access blob.
|
---|
| 45 | * @memberof bithenge_blob_t
|
---|
| 46 | * @param[out] blob The blob to initialize.
|
---|
[743ce51] | 47 | * @param[in] ops Operations providing random access support. This pointer must
|
---|
[a54bd98] | 48 | * be valid until the blob is destroyed.
|
---|
| 49 | * @return EOK on success or an error code from errno.h.
|
---|
| 50 | */
|
---|
[b7fd2a0] | 51 | errno_t bithenge_init_random_access_blob(bithenge_blob_t *blob,
|
---|
[743ce51] | 52 | const bithenge_random_access_blob_ops_t *ops)
|
---|
| 53 | {
|
---|
| 54 | assert(blob);
|
---|
| 55 | assert(ops);
|
---|
| 56 | assert(ops->destroy);
|
---|
[0ce0103] | 57 | assert(ops->read || ops->read_bits);
|
---|
[743ce51] | 58 | assert(ops->size);
|
---|
| 59 |
|
---|
[1a3b953] | 60 | if (bithenge_should_fail())
|
---|
| 61 | return ENOMEM;
|
---|
| 62 |
|
---|
[5c925ce] | 63 | blob->base.type = BITHENGE_NODE_BLOB;
|
---|
[f2da0bb] | 64 | blob->base.refs = 1;
|
---|
[5c925ce] | 65 | blob->base.blob_ops = ops;
|
---|
[a54bd98] | 66 | return EOK;
|
---|
| 67 | }
|
---|
| 68 |
|
---|
[b7fd2a0] | 69 | static errno_t sequential_buffer(bithenge_sequential_blob_t *blob, aoff64_t end)
|
---|
[743ce51] | 70 | {
|
---|
[a54bd98] | 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)
|
---|
[743ce51] | 79 | return ENOMEM;
|
---|
[a54bd98] | 80 | blob->buffer = buffer;
|
---|
| 81 | }
|
---|
[743ce51] | 82 | aoff64_t size = end - blob->data_size;
|
---|
[b7fd2a0] | 83 | errno_t rc = blob->ops->read(blob, blob->buffer + blob->data_size, &size);
|
---|
[a54bd98] | 84 | if (rc != EOK)
|
---|
| 85 | return rc;
|
---|
| 86 | blob->data_size += size;
|
---|
| 87 | return EOK;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
[978ccaf1] | 90 | static inline bithenge_sequential_blob_t *blob_as_sequential(
|
---|
[1923501] | 91 | bithenge_blob_t *base)
|
---|
| 92 | {
|
---|
| 93 | return (bithenge_sequential_blob_t *)base;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
[978ccaf1] | 96 | static inline bithenge_blob_t *sequential_as_blob(
|
---|
[1923501] | 97 | bithenge_sequential_blob_t *blob)
|
---|
| 98 | {
|
---|
| 99 | return &blob->base;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
[b7fd2a0] | 102 | static errno_t sequential_size(bithenge_blob_t *base, aoff64_t *size)
|
---|
[743ce51] | 103 | {
|
---|
[978ccaf1] | 104 | bithenge_sequential_blob_t *blob = blob_as_sequential(base);
|
---|
[b7fd2a0] | 105 | errno_t rc;
|
---|
[743ce51] | 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);
|
---|
[a54bd98] | 116 | if (rc != EOK)
|
---|
| 117 | return rc;
|
---|
| 118 | }
|
---|
[743ce51] | 119 | *size = blob->data_size;
|
---|
[a54bd98] | 120 | return EOK;
|
---|
| 121 | }
|
---|
| 122 |
|
---|
[b7fd2a0] | 123 | static errno_t sequential_read(bithenge_blob_t *base, aoff64_t offset,
|
---|
[743ce51] | 124 | char *buffer, aoff64_t *size)
|
---|
| 125 | {
|
---|
[978ccaf1] | 126 | bithenge_sequential_blob_t *blob = blob_as_sequential(base);
|
---|
[a54bd98] | 127 | aoff64_t end = offset + *size;
|
---|
| 128 | if (end > blob->data_size) {
|
---|
[b7fd2a0] | 129 | errno_t rc = sequential_buffer(blob, end);
|
---|
[a54bd98] | 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 |
|
---|
[978ccaf1] | 140 | static void sequential_destroy(bithenge_blob_t *base)
|
---|
[743ce51] | 141 | {
|
---|
[978ccaf1] | 142 | bithenge_sequential_blob_t *blob = blob_as_sequential(base);
|
---|
[a54bd98] | 143 | free(blob->buffer);
|
---|
[978ccaf1] | 144 | blob->ops->destroy(blob);
|
---|
[a54bd98] | 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 | */
|
---|
[b7fd2a0] | 160 | errno_t bithenge_init_sequential_blob(bithenge_sequential_blob_t *blob,
|
---|
[743ce51] | 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 |
|
---|
[b7fd2a0] | 169 | errno_t rc = bithenge_init_random_access_blob(sequential_as_blob(blob),
|
---|
[1923501] | 170 | &sequential_ops);
|
---|
[a54bd98] | 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 |
|
---|
[ce683ed3] | 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 |
|
---|
[978ccaf1] | 187 | static inline memory_blob_t *blob_as_memory(bithenge_blob_t *base)
|
---|
[ce683ed3] | 188 | {
|
---|
| 189 | return (memory_blob_t *)base;
|
---|
| 190 | }
|
---|
| 191 |
|
---|
[978ccaf1] | 192 | static inline bithenge_blob_t *memory_as_blob(memory_blob_t *blob)
|
---|
[ce683ed3] | 193 | {
|
---|
| 194 | return &blob->base;
|
---|
| 195 | }
|
---|
| 196 |
|
---|
[b7fd2a0] | 197 | static errno_t memory_size(bithenge_blob_t *base, aoff64_t *size)
|
---|
[ce683ed3] | 198 | {
|
---|
[978ccaf1] | 199 | memory_blob_t *blob = blob_as_memory(base);
|
---|
[1a3b953] | 200 | if (bithenge_should_fail())
|
---|
| 201 | return EIO;
|
---|
[ce683ed3] | 202 | *size = blob->size;
|
---|
| 203 | return EOK;
|
---|
| 204 | }
|
---|
| 205 |
|
---|
[b7fd2a0] | 206 | static errno_t memory_read(bithenge_blob_t *base, aoff64_t offset, char *buffer,
|
---|
[ce683ed3] | 207 | aoff64_t *size)
|
---|
| 208 | {
|
---|
[978ccaf1] | 209 | memory_blob_t *blob = blob_as_memory(base);
|
---|
[ce683ed3] | 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 |
|
---|
[978ccaf1] | 217 | static void memory_destroy(bithenge_blob_t *base)
|
---|
[ce683ed3] | 218 | {
|
---|
[978ccaf1] | 219 | memory_blob_t *blob = blob_as_memory(base);
|
---|
[ce683ed3] | 220 | if (blob->needs_free)
|
---|
[da0fef6] | 221 | free((void *)blob->buffer);
|
---|
[ce683ed3] | 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 |
|
---|
[8375d0eb] | 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
|
---|
[ce683ed3] | 233 | * after it is used.
|
---|
| 234 | * @memberof bithenge_blob_t
|
---|
[8375d0eb] | 235 | * @param[out] out Stores the created blob node.
|
---|
[ce683ed3] | 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.
|
---|
[da0fef6] | 239 | * @param needs_free If true, the buffer will be freed with free() if this
|
---|
| 240 | * function fails or the blob is destroyed.
|
---|
[ce683ed3] | 241 | * @return EOK on success or an error code from errno.h. */
|
---|
[b7fd2a0] | 242 | errno_t bithenge_new_blob_from_buffer(bithenge_node_t **out, const void *buffer,
|
---|
[ce683ed3] | 243 | size_t len, bool needs_free)
|
---|
| 244 | {
|
---|
[b7fd2a0] | 245 | errno_t rc;
|
---|
[ce683ed3] | 246 | assert(buffer || !len);
|
---|
| 247 |
|
---|
| 248 | memory_blob_t *blob = malloc(sizeof(*blob));
|
---|
[1a3b953] | 249 | if (!blob) {
|
---|
| 250 | rc = ENOMEM;
|
---|
| 251 | goto error;
|
---|
| 252 | }
|
---|
[978ccaf1] | 253 | rc = bithenge_init_random_access_blob(memory_as_blob(blob),
|
---|
[ce683ed3] | 254 | &memory_ops);
|
---|
[1a3b953] | 255 | if (rc != EOK)
|
---|
| 256 | goto error;
|
---|
[ce683ed3] | 257 | blob->buffer = buffer;
|
---|
| 258 | blob->size = len;
|
---|
| 259 | blob->needs_free = needs_free;
|
---|
[978ccaf1] | 260 | *out = bithenge_blob_as_node(memory_as_blob(blob));
|
---|
[ce683ed3] | 261 | return EOK;
|
---|
[a35b458] | 262 |
|
---|
[1a3b953] | 263 | error:
|
---|
| 264 | if (needs_free)
|
---|
| 265 | free((void *)buffer);
|
---|
| 266 | free(blob);
|
---|
| 267 | return rc;
|
---|
| 268 | }
|
---|
| 269 |
|
---|
| 270 | /** Create a blob node from data. Unlike with @a
|
---|
| 271 | * bithenge_blob_t::bithenge_new_blob_from_buffer, the data is copied into a
|
---|
| 272 | * new buffer and the original data can be changed after this call. The blob
|
---|
| 273 | * must be freed with @a bithenge_node_t::bithenge_node_destroy after it is
|
---|
| 274 | * used.
|
---|
| 275 | * @memberof bithenge_blob_t
|
---|
| 276 | * @param[out] out Stores the created blob node.
|
---|
| 277 | * @param[in] data The data.
|
---|
| 278 | * @param len The length of the data.
|
---|
| 279 | * @return EOK on success or an error code from errno.h. */
|
---|
[b7fd2a0] | 280 | errno_t bithenge_new_blob_from_data(bithenge_node_t **out, const void *data,
|
---|
[1a3b953] | 281 | size_t len)
|
---|
| 282 | {
|
---|
| 283 | char *buffer = malloc(len);
|
---|
| 284 | if (!buffer)
|
---|
| 285 | return ENOMEM;
|
---|
| 286 | memcpy(buffer, data, len);
|
---|
| 287 |
|
---|
| 288 | return bithenge_new_blob_from_buffer(out, buffer, len, true);
|
---|
[ce683ed3] | 289 | }
|
---|
| 290 |
|
---|
[1a3b953] | 291 |
|
---|
| 292 |
|
---|
[04a7435f] | 293 | typedef struct {
|
---|
| 294 | bithenge_blob_t base;
|
---|
| 295 | bithenge_blob_t *source;
|
---|
| 296 | aoff64_t offset;
|
---|
| 297 | aoff64_t size;
|
---|
| 298 | bool size_matters;
|
---|
| 299 | } subblob_t;
|
---|
| 300 |
|
---|
| 301 | static inline subblob_t *blob_as_subblob(bithenge_blob_t *base)
|
---|
| 302 | {
|
---|
| 303 | return (subblob_t *)base;
|
---|
| 304 | }
|
---|
| 305 |
|
---|
| 306 | static inline bithenge_blob_t *subblob_as_blob(subblob_t *blob)
|
---|
| 307 | {
|
---|
| 308 | return &blob->base;
|
---|
| 309 | }
|
---|
| 310 |
|
---|
[b7fd2a0] | 311 | static errno_t subblob_size(bithenge_blob_t *base, aoff64_t *size)
|
---|
[04a7435f] | 312 | {
|
---|
| 313 | subblob_t *blob = blob_as_subblob(base);
|
---|
| 314 | if (blob->size_matters) {
|
---|
| 315 | *size = blob->size;
|
---|
| 316 | return EOK;
|
---|
| 317 | } else {
|
---|
[b7fd2a0] | 318 | errno_t rc = bithenge_blob_size(blob->source, size);
|
---|
[04a7435f] | 319 | *size -= blob->offset;
|
---|
| 320 | return rc;
|
---|
| 321 | }
|
---|
| 322 | }
|
---|
| 323 |
|
---|
[b7fd2a0] | 324 | static errno_t subblob_read(bithenge_blob_t *base, aoff64_t offset,
|
---|
[04a7435f] | 325 | char *buffer, aoff64_t *size)
|
---|
| 326 | {
|
---|
| 327 | subblob_t *blob = blob_as_subblob(base);
|
---|
| 328 | if (blob->size_matters) {
|
---|
| 329 | if (offset > blob->size)
|
---|
| 330 | return EINVAL;
|
---|
| 331 | *size = min(*size, blob->size - offset);
|
---|
| 332 | }
|
---|
| 333 | offset += blob->offset;
|
---|
| 334 | return bithenge_blob_read(blob->source, offset, buffer, size);
|
---|
| 335 | }
|
---|
| 336 |
|
---|
[b7fd2a0] | 337 | static errno_t subblob_read_bits(bithenge_blob_t *base, aoff64_t offset,
|
---|
[0ce0103] | 338 | char *buffer, aoff64_t *size, bool little_endian)
|
---|
| 339 | {
|
---|
| 340 | subblob_t *blob = blob_as_subblob(base);
|
---|
| 341 | if (blob->size_matters) {
|
---|
| 342 | if (offset > blob->size)
|
---|
| 343 | return EINVAL;
|
---|
| 344 | *size = min(*size, blob->size - offset);
|
---|
| 345 | }
|
---|
| 346 | offset += blob->offset;
|
---|
| 347 | return bithenge_blob_read_bits(blob->source, offset, buffer, size,
|
---|
| 348 | little_endian);
|
---|
| 349 | }
|
---|
| 350 |
|
---|
[978ccaf1] | 351 | static void subblob_destroy(bithenge_blob_t *base)
|
---|
[04a7435f] | 352 | {
|
---|
| 353 | subblob_t *blob = blob_as_subblob(base);
|
---|
| 354 | bithenge_blob_dec_ref(blob->source);
|
---|
| 355 | free(blob);
|
---|
| 356 | }
|
---|
| 357 |
|
---|
| 358 | static const bithenge_random_access_blob_ops_t subblob_ops = {
|
---|
| 359 | .size = subblob_size,
|
---|
| 360 | .read = subblob_read,
|
---|
[0ce0103] | 361 | .read_bits = subblob_read_bits,
|
---|
[04a7435f] | 362 | .destroy = subblob_destroy,
|
---|
| 363 | };
|
---|
| 364 |
|
---|
| 365 | static bool is_subblob(bithenge_blob_t *blob)
|
---|
| 366 | {
|
---|
| 367 | return blob->base.blob_ops == &subblob_ops;
|
---|
| 368 | }
|
---|
| 369 |
|
---|
[b7fd2a0] | 370 | static errno_t new_subblob(bithenge_node_t **out, bithenge_blob_t *source,
|
---|
[04a7435f] | 371 | aoff64_t offset, aoff64_t size, bool size_matters)
|
---|
| 372 | {
|
---|
| 373 | assert(out);
|
---|
| 374 | assert(source);
|
---|
[b7fd2a0] | 375 | errno_t rc;
|
---|
[04a7435f] | 376 | subblob_t *blob = 0;
|
---|
| 377 |
|
---|
| 378 | if (is_subblob(source)) {
|
---|
| 379 | /* We can do some optimizations this way */
|
---|
| 380 | if (!size_matters)
|
---|
| 381 | size = 0;
|
---|
| 382 | subblob_t *source_subblob = blob_as_subblob(source);
|
---|
| 383 | if (source_subblob->size_matters &&
|
---|
| 384 | offset + size > source_subblob->size) {
|
---|
| 385 | rc = EINVAL;
|
---|
| 386 | goto error;
|
---|
| 387 | }
|
---|
| 388 |
|
---|
| 389 | if (source->base.refs == 1) {
|
---|
| 390 | source_subblob->offset += offset;
|
---|
| 391 | source_subblob->size -= offset;
|
---|
| 392 | if (size_matters) {
|
---|
| 393 | source_subblob->size_matters = true;
|
---|
| 394 | source_subblob->size = size;
|
---|
| 395 | }
|
---|
| 396 | *out = bithenge_blob_as_node(source);
|
---|
| 397 | return EOK;
|
---|
| 398 | }
|
---|
| 399 |
|
---|
| 400 | if (!size_matters && source_subblob->size_matters) {
|
---|
| 401 | size_matters = true;
|
---|
| 402 | size = source_subblob->size - offset;
|
---|
| 403 | }
|
---|
| 404 | offset += source_subblob->offset;
|
---|
| 405 | source = source_subblob->source;
|
---|
| 406 | bithenge_blob_inc_ref(source);
|
---|
| 407 | bithenge_blob_dec_ref(subblob_as_blob(source_subblob));
|
---|
| 408 | }
|
---|
| 409 |
|
---|
| 410 | blob = malloc(sizeof(*blob));
|
---|
| 411 | if (!blob) {
|
---|
| 412 | rc = ENOMEM;
|
---|
| 413 | goto error;
|
---|
| 414 | }
|
---|
[978ccaf1] | 415 | rc = bithenge_init_random_access_blob(subblob_as_blob(blob),
|
---|
[04a7435f] | 416 | &subblob_ops);
|
---|
| 417 | if (rc != EOK)
|
---|
| 418 | goto error;
|
---|
| 419 | blob->source = source;
|
---|
| 420 | blob->offset = offset;
|
---|
| 421 | blob->size = size;
|
---|
| 422 | blob->size_matters = size_matters;
|
---|
| 423 | *out = bithenge_blob_as_node(subblob_as_blob(blob));
|
---|
| 424 | return EOK;
|
---|
| 425 |
|
---|
| 426 | error:
|
---|
| 427 | bithenge_blob_dec_ref(source);
|
---|
| 428 | free(blob);
|
---|
| 429 | return rc;
|
---|
| 430 | }
|
---|
| 431 |
|
---|
| 432 | /** Create a blob from data offset within another blob. This function takes
|
---|
| 433 | * ownership of a reference to @a blob.
|
---|
| 434 | * @param[out] out Stores the created blob node. On error, this is unchanged.
|
---|
| 435 | * @param[in] source The input blob.
|
---|
| 436 | * @param offset The offset within the input blob at which the new blob will start.
|
---|
| 437 | * @return EOK on success or an error code from errno.h. */
|
---|
[b7fd2a0] | 438 | errno_t bithenge_new_offset_blob(bithenge_node_t **out, bithenge_blob_t *source,
|
---|
[04a7435f] | 439 | aoff64_t offset)
|
---|
| 440 | {
|
---|
| 441 | return new_subblob(out, source, offset, 0, false);
|
---|
| 442 | }
|
---|
| 443 |
|
---|
| 444 | /** Create a blob from part of another blob. This function takes ownership of a
|
---|
| 445 | * reference to @a blob.
|
---|
| 446 | * @param[out] out Stores the created blob node. On error, this is unchanged.
|
---|
| 447 | * @param[in] source The input blob.
|
---|
| 448 | * @param offset The offset within the input blob at which the new blob will start.
|
---|
| 449 | * @param size The size of the new blob.
|
---|
| 450 | * @return EOK on success or an error code from errno.h. */
|
---|
[b7fd2a0] | 451 | errno_t bithenge_new_subblob(bithenge_node_t **out, bithenge_blob_t *source,
|
---|
[04a7435f] | 452 | aoff64_t offset, aoff64_t size)
|
---|
| 453 | {
|
---|
| 454 | return new_subblob(out, source, offset, size, true);
|
---|
| 455 | }
|
---|
| 456 |
|
---|
[d5070ef] | 457 | /** Check whether the contents of two blobs are equal.
|
---|
| 458 | * @memberof bithenge_blob_t
|
---|
[a42d7d8] | 459 | * @param[out] out Holds whether the blobs are equal.
|
---|
[d5070ef] | 460 | * @param a, b Blobs to compare.
|
---|
[a42d7d8] | 461 | * @return EOK on success, or an error code from errno.h.
|
---|
[d5070ef] | 462 | */
|
---|
[b7fd2a0] | 463 | errno_t bithenge_blob_equal(bool *out, bithenge_blob_t *a, bithenge_blob_t *b)
|
---|
[d5070ef] | 464 | {
|
---|
| 465 | assert(a);
|
---|
| 466 | assert(a->base.blob_ops);
|
---|
| 467 | assert(b);
|
---|
| 468 | assert(b->base.blob_ops);
|
---|
[b7fd2a0] | 469 | errno_t rc;
|
---|
[d5070ef] | 470 | char buffer_a[4096], buffer_b[4096];
|
---|
| 471 | aoff64_t offset = 0, size_a = sizeof(buffer_a), size_b = sizeof(buffer_b);
|
---|
| 472 | do {
|
---|
| 473 | rc = bithenge_blob_read(a, offset, buffer_a, &size_a);
|
---|
| 474 | if (rc != EOK)
|
---|
[a42d7d8] | 475 | return rc;
|
---|
[d5070ef] | 476 | rc = bithenge_blob_read(b, offset, buffer_b, &size_b);
|
---|
| 477 | if (rc != EOK)
|
---|
[a42d7d8] | 478 | return rc;
|
---|
[44ecf89] | 479 | if (size_a != size_b || memcmp(buffer_a, buffer_b, size_a) != 0) {
|
---|
[a42d7d8] | 480 | *out = false;
|
---|
| 481 | return EOK;
|
---|
| 482 | }
|
---|
[d5070ef] | 483 | offset += size_a;
|
---|
| 484 | } while (size_a == sizeof(buffer_a));
|
---|
[a42d7d8] | 485 | *out = true;
|
---|
| 486 | return EOK;
|
---|
[d5070ef] | 487 | }
|
---|
| 488 |
|
---|
[a54bd98] | 489 | /** @}
|
---|
| 490 | */
|
---|