|
| 1 | +/* vim: set sw=4 ts=4 et tw=78: */ |
| 2 | + |
| 3 | +/** |
| 4 | + * \brief An example of a buffered CBOR file reader using low-level |
| 5 | + * POSIX file I/O as might be implemented in a microcontroller |
| 6 | + * RTOS. |
| 7 | + * |
| 8 | + * \author Stuart Longland <stuartl@vrt.com.au> |
| 9 | + * |
| 10 | + * \copyright tinycbor project contributors |
| 11 | + * |
| 12 | + * \file bufferedwriter.c |
| 13 | + */ |
| 14 | + |
| 15 | +/* Includes for POSIX low-level file I/O */ |
| 16 | +#include <sys/types.h> |
| 17 | +#include <sys/stat.h> |
| 18 | +#include <unistd.h> |
| 19 | +#include <fcntl.h> |
| 20 | + |
| 21 | +/* Sanity check routine */ |
| 22 | +#include <assert.h> |
| 23 | + |
| 24 | +/* Pull in "standard" integer types */ |
| 25 | +#include <stdint.h> |
| 26 | + |
| 27 | +/* Pull in definitions for printf and errno */ |
| 28 | +#include <stdio.h> |
| 29 | +#include <string.h> |
| 30 | +#include <errno.h> |
| 31 | + |
| 32 | +/* For example usage */ |
| 33 | +#include <stdlib.h> |
| 34 | + |
| 35 | +#include "../src/cbor.h" |
| 36 | + |
| 37 | +/** |
| 38 | + * Context for the file reader. This stores the file descriptor, a pointer to |
| 39 | + * the read buffer, and context pointers. The assumption here is that the |
| 40 | + * CBOR document being read is less than 64KiB (65536 bytes) in size. |
| 41 | + */ |
| 42 | +struct filereader |
| 43 | +{ |
| 44 | + /** |
| 45 | + * Read buffer. This must be allocated by the caller, and sized |
| 46 | + * appropriately since the buffer must be big enough to accommodate |
| 47 | + * entire string chunks embedded in the CBOR document. |
| 48 | + */ |
| 49 | + uint8_t* buffer; |
| 50 | + |
| 51 | + /** |
| 52 | + * File descriptor, returned by the `open` system call. |
| 53 | + */ |
| 54 | + int fd; |
| 55 | + |
| 56 | + /** |
| 57 | + * Size of the file in bytes. |
| 58 | + */ |
| 59 | + uint16_t file_sz; |
| 60 | + |
| 61 | + /** |
| 62 | + * Size of the read buffer in bytes. |
| 63 | + */ |
| 64 | + uint16_t buffer_sz; |
| 65 | + |
| 66 | + /** |
| 67 | + * Read position within the file. This basically describes where |
| 68 | + * `buffer[0]` came from in the source file. |
| 69 | + */ |
| 70 | + uint16_t pos; |
| 71 | + |
| 72 | + /** |
| 73 | + * Number of bytes stored in the buffer presently. |
| 74 | + */ |
| 75 | + uint16_t used_sz; |
| 76 | + |
| 77 | + /** |
| 78 | + * Block size. When reading from the file, we round up to whole multiples |
| 79 | + * of this block size to improve I/O efficiency. |
| 80 | + */ |
| 81 | + uint16_t block_sz; |
| 82 | +}; |
| 83 | + |
| 84 | +/* Implementation routines */ |
| 85 | + |
| 86 | +/** |
| 87 | + * Return the nearest (earlier) position that is on a block boundary. |
| 88 | + */ |
| 89 | +static uint16_t filereader_get_block_pos( |
| 90 | + const struct filereader * const context, |
| 91 | + uint16_t pos |
| 92 | +) { |
| 93 | + return context->block_sz * (pos / context->block_sz); |
| 94 | +} |
| 95 | + |
| 96 | +/** |
| 97 | + * Retrieve a pointer to the region defined by \a pos and \a sz. |
| 98 | + * |
| 99 | + * \retval NULL The region is not contained in the buffer. |
| 100 | + */ |
| 101 | +static uint8_t *filereader_get_ptr( |
| 102 | + struct filereader *context, uint16_t pos, uint16_t sz |
| 103 | +) { |
| 104 | + /* Sanity check, disallow `sz` bigger than buffer content */ |
| 105 | + if (sz > context->used_sz) { |
| 106 | + return NULL; |
| 107 | + } |
| 108 | + |
| 109 | + /* Is `pos` off the start of the buffer? */ |
| 110 | + if (pos < context->pos) { |
| 111 | + return NULL; |
| 112 | + } |
| 113 | + |
| 114 | + /* Is `pos + sz` off the end of the buffer? */ |
| 115 | + if ((pos + sz) > (context->pos + context->used_sz)) { |
| 116 | + return NULL; |
| 117 | + } |
| 118 | + |
| 119 | + /* We should be good */ |
| 120 | + return &(context->buffer[pos - context->pos]); |
| 121 | +} |
| 122 | + |
| 123 | +/** |
| 124 | + * Copy the data from the requested position in the file to the buffer. |
| 125 | + */ |
| 126 | +static int filereader_read( |
| 127 | + struct filereader *context, uint16_t pos, uint16_t sz, uint8_t *wptr |
| 128 | +) { |
| 129 | + /* Seek to the required file position */ |
| 130 | + off_t seek_res = lseek(context->fd, pos, SEEK_SET); |
| 131 | + if (seek_res != pos) { |
| 132 | + /* We failed */ |
| 133 | + return -errno; |
| 134 | + } |
| 135 | + |
| 136 | + /* Perform the read */ |
| 137 | + ssize_t read_res = read(context->fd, wptr, sz); |
| 138 | + if (read_res != sz) { |
| 139 | + /* Truncated read */ |
| 140 | + return -errno; |
| 141 | + } |
| 142 | + |
| 143 | + return sz; |
| 144 | +} |
| 145 | + |
| 146 | +/** |
| 147 | + * Prepend \arg sz bytes from the file to the buffer, shifting the |
| 148 | + * read window back \arg sz bytes. |
| 149 | + * |
| 150 | + * \param context Reader context |
| 151 | + * \param sz Number of bytes to read |
| 152 | + * |
| 153 | + * \retval ≥0 Number of bytes read into the buffer. |
| 154 | + * \retval <0 Read failure, value is `errno` negated. |
| 155 | + */ |
| 156 | +static int filereader_prepend_buffer(struct filereader *context, uint16_t sz) { |
| 157 | + /* Compute read position */ |
| 158 | + const uint16_t pos = context->pos - sz; |
| 159 | + |
| 160 | + /* Shuffle existing data forward by sz bytes */ |
| 161 | + memmove( |
| 162 | + &(context->buffer[sz]), context->buffer, |
| 163 | + context->buffer_sz - sz |
| 164 | + ); |
| 165 | + |
| 166 | + /* Copy the data in */ |
| 167 | + int read_res = filereader_read(context, pos, sz, context->buffer); |
| 168 | + if (read_res >= 0) { |
| 169 | + context->pos = pos; |
| 170 | + if ((context->used_sz + sz) < context->buffer_sz) { |
| 171 | + context->used_sz += sz; |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + return read_res; |
| 176 | +} |
| 177 | + |
| 178 | +/** |
| 179 | + * Append \arg sz bytes from the file to the buffer, shifting the |
| 180 | + * read window forward \arg sz bytes. |
| 181 | + * |
| 182 | + * \param context Reader context |
| 183 | + * \param sz Number of bytes to read |
| 184 | + * |
| 185 | + * \retval ≥0 Number of bytes read into the buffer. |
| 186 | + * \retval <0 Read failure, value is `errno` negated. |
| 187 | + */ |
| 188 | +static int filereader_append_buffer(struct filereader *context, uint16_t sz) { |
| 189 | + /* Compute read position */ |
| 190 | + const uint16_t pos = context->pos + context->used_sz; |
| 191 | + |
| 192 | + /* Is there room? */ |
| 193 | + if ((context->buffer_sz - context->used_sz) < sz) { |
| 194 | + /* Shuffle existing data forward by sz bytes */ |
| 195 | + memmove( |
| 196 | + context->buffer, &(context->buffer[sz]), |
| 197 | + context->buffer_sz - sz |
| 198 | + ); |
| 199 | + context->pos += sz; |
| 200 | + context->used_sz -= sz; |
| 201 | + } |
| 202 | + |
| 203 | + /* Copy the data in */ |
| 204 | + int read_res = filereader_read( |
| 205 | + context, pos, sz, |
| 206 | + &(context->buffer[context->used_sz]) |
| 207 | + ); |
| 208 | + if (read_res >= 0) { |
| 209 | + context->used_sz += sz; |
| 210 | + } |
| 211 | + |
| 212 | + return read_res; |
| 213 | +} |
| 214 | + |
| 215 | +/** |
| 216 | + * Read data from the file and place it in the buffer, shuffling |
| 217 | + * existing data around as required. |
| 218 | + * |
| 219 | + * \param context Reader context |
| 220 | + * \param pos Position in the file to start reading |
| 221 | + * \param sz Number of bytes to read |
| 222 | + * |
| 223 | + * \retval ≥0 Number of bytes read into the buffer. |
| 224 | + * \retval <0 Read failure, value is `errno` negated. |
| 225 | + */ |
| 226 | +static int filereader_load_buffer( |
| 227 | + struct filereader *context, uint16_t pos, uint16_t sz |
| 228 | +) { |
| 229 | + /* Compute the end position (not-inclusive) */ |
| 230 | + uint16_t end = pos + sz; |
| 231 | + |
| 232 | + /* Is this in the buffer already? */ |
| 233 | + if ( |
| 234 | + (pos < context->pos) |
| 235 | + || (end > (context->pos + context->used_sz)) |
| 236 | + ) { |
| 237 | + /* Make a note of the current buffer state */ |
| 238 | + uint16_t buffer_end = context->pos + context->used_sz; |
| 239 | + uint16_t buffer_rem = context->buffer_sz - context->used_sz; |
| 240 | + |
| 241 | + /* Our buffer write position */ |
| 242 | + uint8_t* wptr = context->buffer; |
| 243 | + |
| 244 | + /* |
| 245 | + * Dumb approach for now, replace the entire buffer. Round |
| 246 | + * the start and end points to block boundaries for efficiency. |
| 247 | + */ |
| 248 | + pos = filereader_get_block_pos(context, pos); |
| 249 | + end = filereader_get_block_pos(context, end + (context->block_sz - 1)); |
| 250 | + |
| 251 | + /* Clamp the end position to the file size */ |
| 252 | + if (end > context->file_sz) { |
| 253 | + end = context->file_sz; |
| 254 | + } |
| 255 | + |
| 256 | + /* Compute new rounded size, then clamp to buffer size */ |
| 257 | + sz = end - pos; |
| 258 | + if (sz > context->buffer_sz) { |
| 259 | + sz = context->buffer_sz; |
| 260 | + } |
| 261 | + |
| 262 | + /* Can we re-use existing data? */ |
| 263 | + if ( |
| 264 | + (pos >= context->pos) |
| 265 | + && (pos < buffer_end) |
| 266 | + && (end > buffer_end) |
| 267 | + ) { |
| 268 | + return filereader_append_buffer(context, end - buffer_end); |
| 269 | + } else if ( |
| 270 | + (pos < context->pos) |
| 271 | + && (end >= context->pos) |
| 272 | + && (end <= buffer_end) |
| 273 | + ) { |
| 274 | + return filereader_prepend_buffer(context, context->pos - pos); |
| 275 | + } else { |
| 276 | + /* Nope, read the lot in */ |
| 277 | + const uint16_t file_rem = context->file_sz - pos; |
| 278 | + if (file_rem < context->buffer_sz) { |
| 279 | + sz = file_rem; |
| 280 | + } else { |
| 281 | + sz = context->buffer_sz; |
| 282 | + } |
| 283 | + |
| 284 | + int read_res = filereader_read( |
| 285 | + context, pos, sz, |
| 286 | + context->buffer |
| 287 | + ); |
| 288 | + if (read_res >= 0) { |
| 289 | + context->pos = pos; |
| 290 | + context->used_sz = sz; |
| 291 | + } |
| 292 | + return read_res; |
| 293 | + } |
| 294 | + } else { |
| 295 | + /* Nothing to do, we have the required data already */ |
| 296 | + return 0; |
| 297 | + } |
| 298 | +} |
| 299 | + |
| 300 | +/** |
| 301 | + * Try to read the data into the buffer, then return a pointer to it. |
| 302 | + * |
| 303 | + * \retval NULL The region could not be loaded into the buffer. |
| 304 | + */ |
| 305 | +static uint8_t *filereader_fetch_ptr( |
| 306 | + struct filereader *context, uint16_t pos, uint16_t sz |
| 307 | +) { |
| 308 | + /* Ensure the data we need is present */ |
| 309 | + if (filereader_load_buffer(context, pos, sz) < 0) { |
| 310 | + /* We failed */ |
| 311 | + return NULL; |
| 312 | + } else { |
| 313 | + return filereader_get_ptr(context, pos, sz); |
| 314 | + } |
| 315 | +} |
| 316 | + |
| 317 | +/** |
| 318 | + * Fetch the reader context from the CborValue |
| 319 | + */ |
| 320 | +static struct filereader *filereader_get_context(const CborValue * const value) { |
| 321 | + return (struct filereader*)(value->parser->data.ctx); |
| 322 | +} |
| 323 | + |
| 324 | +/** |
| 325 | + * Fetch the CborValue read position |
| 326 | + */ |
| 327 | +static uint16_t filereader_get_pos(const CborValue * const value) { |
| 328 | + return (uint16_t)(uintptr_t)(value->source.token); |
| 329 | +} |
| 330 | + |
| 331 | +/** |
| 332 | + * Set the CborValue read position |
| 333 | + */ |
| 334 | +static void filereader_set_pos(CborValue * const value, uint16_t new_pos) { |
| 335 | + value->source.token = (void*)(uintptr_t)new_pos; |
| 336 | +} |
| 337 | + |
| 338 | +/** |
| 339 | + * Return `true` if there is at least \a len bytes that can be read from |
| 340 | + * the file at this moment in time. |
| 341 | + */ |
| 342 | +static bool filereader_impl_can_read_bytes( |
| 343 | + const struct CborValue *value, |
| 344 | + size_t len |
| 345 | +) { |
| 346 | + const struct filereader *context = filereader_get_context(value); |
| 347 | + const uint16_t pos = filereader_get_pos(value); |
| 348 | + |
| 349 | + return ((size_t)pos + len) <= context->file_sz; |
| 350 | +} |
| 351 | + |
| 352 | +/** |
| 353 | + * Read the bytes from the buffer without advancing the read pointer. |
| 354 | + */ |
| 355 | +static void* filereader_impl_read_bytes( |
| 356 | + const struct CborValue *value, |
| 357 | + void* dst, size_t offset, size_t len |
| 358 | +) { |
| 359 | + struct filereader *context = filereader_get_context(value); |
| 360 | + |
| 361 | + /* Determine read position factoring in offset */ |
| 362 | + const uint16_t pos = filereader_get_pos(value) + offset; |
| 363 | + |
| 364 | + /* Fetch the data from the file */ |
| 365 | + const uint8_t* ptr = filereader_fetch_ptr(context, pos, (uint16_t)len); |
| 366 | + if (ptr != NULL) { |
| 367 | + return memcpy(dst, ptr, len); |
| 368 | + } else { |
| 369 | + /* We could not read the data */ |
| 370 | + return NULL; |
| 371 | + } |
| 372 | +} |
| 373 | + |
| 374 | +/** |
| 375 | + * Advance the pointer by the requested amount. |
| 376 | + */ |
| 377 | +static void filereader_impl_advance_bytes(struct CborValue *value, size_t len) { |
| 378 | + filereader_set_pos(value, filereader_get_pos(value) + (uint16_t)len); |
| 379 | +} |
| 380 | + |
| 381 | +/** |
| 382 | + * Retrieve a pointer to the string defined by the given offset and length. |
| 383 | + */ |
| 384 | +CborError filereader_impl_transfer_string( |
| 385 | + struct CborValue *value, |
| 386 | + const void **userptr, size_t offset, size_t len |
| 387 | +) { |
| 388 | + struct filereader *context = filereader_get_context(value); |
| 389 | + |
| 390 | + /* Determine read position factoring in offset */ |
| 391 | + const uint16_t pos = filereader_get_pos(value) + offset; |
| 392 | + |
| 393 | + /* Fetch the data from the file */ |
| 394 | + const uint8_t* ptr = filereader_fetch_ptr(context, pos, (uint16_t)len); |
| 395 | + if (ptr != NULL) { |
| 396 | + /* All good, advance the cursor past the data and return the pointer */ |
| 397 | + filereader_set_pos(value, pos + len); |
| 398 | + *userptr = (void*)ptr; |
| 399 | + return CborNoError; |
| 400 | + } else { |
| 401 | + /* We could not read the data */ |
| 402 | + return CborErrorIO; |
| 403 | + } |
| 404 | +} |
| 405 | + |
| 406 | +/** |
| 407 | + * Implementation of the CBOR File Reader operations. |
| 408 | + */ |
| 409 | +static const struct CborParserOperations filereader_ops = { |
| 410 | + .can_read_bytes = filereader_impl_can_read_bytes, |
| 411 | + .read_bytes = filereader_impl_read_bytes, |
| 412 | + .advance_bytes = filereader_impl_advance_bytes, |
| 413 | + .transfer_string = filereader_impl_transfer_string |
| 414 | +}; |
| 415 | + |
| 416 | +/** |
| 417 | + * Open a CBOR file for reading. |
| 418 | + * |
| 419 | + * \param[inout] parser CBOR parser object to initialise. |
| 420 | + * \param[inout] value Root CBOR cursor object to initialise. |
| 421 | + * |
| 422 | + * \param[inout] context The file reader context. This must exist |
| 423 | + * for the duration the file is open. |
| 424 | + * |
| 425 | + * \param[inout] buffer Read buffer allocated by the caller where |
| 426 | + * the read data will be stored. |
| 427 | + * |
| 428 | + * \param[in] buffer_sz Size of the read buffer. |
| 429 | + * |
| 430 | + * \param[in] path The path to the file being read. |
| 431 | + * |
| 432 | + * \param[in] flags `open` flags. `O_RDONLY` is logic-ORed |
| 433 | + * with this value, but the user may provide |
| 434 | + * other options here. |
| 435 | + * |
| 436 | + * \param[in] block_sz Size of read blocks. Where possible, |
| 437 | + * reads will be rounded up and aligned with |
| 438 | + * blocks of this size for efficiency. Set |
| 439 | + * to 0 to default to `buffer_sz / 2`. |
| 440 | + * |
| 441 | + * \retval CborErrorIO The `open` call failed for some reason, |
| 442 | + * see the POSIX standard `errno` variable |
| 443 | + * for why. |
| 444 | + * |
| 445 | + * \retval CborErrorDataTooLarge The CBOR document is too big to be |
| 446 | + * handled by this reader. |
| 447 | + * |
| 448 | + * \retval CborNoError CBOR encoder initialised successfully. |
| 449 | + */ |
| 450 | +CborError filereader_open( |
| 451 | + CborParser * const parser, |
| 452 | + CborValue * const value, |
| 453 | + struct filereader * const context, |
| 454 | + uint8_t *buffer, |
| 455 | + uint16_t buffer_sz, |
| 456 | + const char* path, |
| 457 | + int flags, |
| 458 | + uint16_t block_sz |
| 459 | +) |
| 460 | +{ |
| 461 | + CborError error = CborNoError; |
| 462 | + struct stat path_stat; |
| 463 | + |
| 464 | + /* Determine the file size */ |
| 465 | + if (stat(path, &path_stat) < 0) { |
| 466 | + /* stat fails */ |
| 467 | + error = CborErrorIO; |
| 468 | + } else { |
| 469 | + context->fd = open(path, O_RDONLY | flags); |
| 470 | + if (context->fd < 0) { |
| 471 | + /* Open fails */ |
| 472 | + error = CborErrorIO; |
| 473 | + } else { |
| 474 | + /* Sanity check document size */ |
| 475 | + if (path_stat.st_size > UINT16_MAX) { |
| 476 | + error = CborErrorDataTooLarge; |
| 477 | + } else { |
| 478 | + /* Initialise structure */ |
| 479 | + context->pos = 0; |
| 480 | + context->used_sz = 0; |
| 481 | + context->buffer = buffer; |
| 482 | + context->buffer_sz = buffer_sz; |
| 483 | + context->file_sz = (uint16_t)path_stat.st_size; |
| 484 | + |
| 485 | + if (block_sz == 0) { |
| 486 | + block_sz = buffer_sz / 2; |
| 487 | + } |
| 488 | + context->block_sz = block_sz; |
| 489 | + |
| 490 | + /* Fill the initial buffer */ |
| 491 | + if (filereader_load_buffer(context, 0, buffer_sz) >= 0) { |
| 492 | + /* Initialise the CBOR parser */ |
| 493 | + error = cbor_parser_init_reader( |
| 494 | + &filereader_ops, parser, value, (void*)context |
| 495 | + ); |
| 496 | + } |
| 497 | + } |
| 498 | + |
| 499 | + if (error != CborNoError) { |
| 500 | + /* Close the file, if we can */ |
| 501 | + assert(close(context->fd) == 0); |
| 502 | + context->fd = -1; |
| 503 | + } |
| 504 | + } |
| 505 | + } |
| 506 | + |
| 507 | + return error; |
| 508 | +} |
| 509 | + |
| 510 | +/** |
| 511 | + * Close the file reader. |
| 512 | + * |
| 513 | + * \param[inout] context File reader context to close. |
| 514 | + * |
| 515 | + * \retval CborErrorIO The `close` call failed for some |
| 516 | + * reason, see the POSIX standard |
| 517 | + * `errno` variable for why. |
| 518 | + * |
| 519 | + * \retval CborNoError File closed, `fd` should be set to -1. |
| 520 | + */ |
| 521 | +CborError filereader_close(struct filereader * const context) |
| 522 | +{ |
| 523 | + CborError error = CborNoError; |
| 524 | + |
| 525 | + /* Try to close the file */ |
| 526 | + if (close(context->fd) < 0) { |
| 527 | + /* Close fails! */ |
| 528 | + error = CborErrorIO; |
| 529 | + } else { |
| 530 | + context->fd = -1; |
| 531 | + } |
| 532 | + |
| 533 | + return error; |
| 534 | +} |
| 535 | + |
| 536 | +/* --- Example usage of the above reader --- */ |
| 537 | + |
| 538 | +/** |
| 539 | + * Indent the output text to the level specified. Taken from `simplereader.c` |
| 540 | + */ |
| 541 | +static void indent(int nestingLevel) |
| 542 | +{ |
| 543 | + while (nestingLevel--) |
| 544 | + printf(" "); |
| 545 | +} |
| 546 | + |
| 547 | +/** |
| 548 | + * Dump the raw bytes given. Taken from `simplereader.c` |
| 549 | + */ |
| 550 | +static void dumpbytes(const uint8_t *buf, size_t len) |
| 551 | +{ |
| 552 | + while (len--) |
| 553 | + printf("%02X ", *buf++); |
| 554 | +} |
| 555 | + |
| 556 | +/** |
| 557 | + * Recursively dump the CBOR data structure. Taken from `simplereader.c` |
| 558 | + */ |
| 559 | +static CborError dumprecursive(CborValue *it, int nestingLevel) |
| 560 | +{ |
| 561 | + while (!cbor_value_at_end(it)) { |
| 562 | + CborError err; |
| 563 | + CborType type = cbor_value_get_type(it); |
| 564 | + |
| 565 | + indent(nestingLevel); |
| 566 | + switch (type) { |
| 567 | + case CborArrayType: |
| 568 | + case CborMapType: { |
| 569 | + // recursive type |
| 570 | + CborValue recursed; |
| 571 | + assert(cbor_value_is_container(it)); |
| 572 | + puts(type == CborArrayType ? "Array[" : "Map["); |
| 573 | + err = cbor_value_enter_container(it, &recursed); |
| 574 | + if (err) |
| 575 | + return err; // parse error |
| 576 | + err = dumprecursive(&recursed, nestingLevel + 1); |
| 577 | + if (err) |
| 578 | + return err; // parse error |
| 579 | + err = cbor_value_leave_container(it, &recursed); |
| 580 | + if (err) |
| 581 | + return err; // parse error |
| 582 | + indent(nestingLevel); |
| 583 | + puts("]"); |
| 584 | + continue; |
| 585 | + } |
| 586 | + |
| 587 | + case CborIntegerType: { |
| 588 | + int64_t val; |
| 589 | + cbor_value_get_int64(it, &val); // can't fail |
| 590 | + printf("%lld\n", (long long)val); |
| 591 | + break; |
| 592 | + } |
| 593 | + |
| 594 | + case CborByteStringType: { |
| 595 | + uint8_t *buf; |
| 596 | + size_t n; |
| 597 | + err = cbor_value_dup_byte_string(it, &buf, &n, it); |
| 598 | + if (err) |
| 599 | + return err; // parse error |
| 600 | + dumpbytes(buf, n); |
| 601 | + printf("\n"); |
| 602 | + free(buf); |
| 603 | + continue; |
| 604 | + } |
| 605 | + |
| 606 | + case CborTextStringType: { |
| 607 | + char *buf; |
| 608 | + size_t n; |
| 609 | + err = cbor_value_dup_text_string(it, &buf, &n, it); |
| 610 | + if (err) |
| 611 | + return err; // parse error |
| 612 | + puts(buf); |
| 613 | + free(buf); |
| 614 | + continue; |
| 615 | + } |
| 616 | + |
| 617 | + case CborTagType: { |
| 618 | + CborTag tag; |
| 619 | + cbor_value_get_tag(it, &tag); // can't fail |
| 620 | + printf("Tag(%lld)\n", (long long)tag); |
| 621 | + break; |
| 622 | + } |
| 623 | + |
| 624 | + case CborSimpleType: { |
| 625 | + uint8_t type; |
| 626 | + cbor_value_get_simple_type(it, &type); // can't fail |
| 627 | + printf("simple(%u)\n", type); |
| 628 | + break; |
| 629 | + } |
| 630 | + |
| 631 | + case CborNullType: |
| 632 | + puts("null"); |
| 633 | + break; |
| 634 | + |
| 635 | + case CborUndefinedType: |
| 636 | + puts("undefined"); |
| 637 | + break; |
| 638 | + |
| 639 | + case CborBooleanType: { |
| 640 | + bool val; |
| 641 | + cbor_value_get_boolean(it, &val); // can't fail |
| 642 | + puts(val ? "true" : "false"); |
| 643 | + break; |
| 644 | + } |
| 645 | + |
| 646 | + case CborDoubleType: { |
| 647 | + double val; |
| 648 | + if (false) { |
| 649 | + float f; |
| 650 | + case CborFloatType: |
| 651 | + cbor_value_get_float(it, &f); |
| 652 | + val = f; |
| 653 | + } else { |
| 654 | + cbor_value_get_double(it, &val); |
| 655 | + } |
| 656 | + printf("%g\n", val); |
| 657 | + break; |
| 658 | + } |
| 659 | + case CborHalfFloatType: { |
| 660 | + uint16_t val; |
| 661 | + cbor_value_get_half_float(it, &val); |
| 662 | + printf("__f16(%04x)\n", val); |
| 663 | + break; |
| 664 | + } |
| 665 | + |
| 666 | + case CborInvalidType: |
| 667 | + assert(false); // can't happen |
| 668 | + break; |
| 669 | + } |
| 670 | + |
| 671 | + err = cbor_value_advance_fixed(it); |
| 672 | + if (err) |
| 673 | + return err; |
| 674 | + } |
| 675 | + return CborNoError; |
| 676 | +} |
| 677 | + |
| 678 | +/** |
| 679 | + * Print the error encountered. If the error is `CborErrorIO`, also check |
| 680 | + * the global `errno` variable and print the resultant error seen. |
| 681 | + * |
| 682 | + * \param[in] error CBORError constant |
| 683 | + */ |
| 684 | +void print_err(CborError error) |
| 685 | +{ |
| 686 | + if (error == CborErrorIO) { |
| 687 | + printf("IO: %s\n", strerror(errno)); |
| 688 | + } else { |
| 689 | + printf("%s\n", cbor_error_string(error)); |
| 690 | + } |
| 691 | +} |
| 692 | + |
| 693 | +int main(int argc, char **argv) |
| 694 | +{ |
| 695 | + if (argc < 2) { |
| 696 | + printf( |
| 697 | + "Usage: %s <filename> [buffer_sz [block_sz]]\n", |
| 698 | + argv[0] |
| 699 | + ); |
| 700 | + return 1; |
| 701 | + } else { |
| 702 | + struct filereader context; |
| 703 | + CborParser parser; |
| 704 | + CborValue value; |
| 705 | + CborError error; |
| 706 | + |
| 707 | + uint16_t buffer_sz = 64; |
| 708 | + uint16_t block_sz = 0; |
| 709 | + |
| 710 | + if (argc > 2) { |
| 711 | + /* buffer_sz given */ |
| 712 | + char *endptr = NULL; |
| 713 | + unsigned long long_buffer_sz = strtoul(argv[2], &endptr, 0); |
| 714 | + |
| 715 | + if (!endptr || *endptr) { |
| 716 | + printf("Invalid buffer size %s\n", argv[2]); |
| 717 | + return 1; |
| 718 | + } |
| 719 | + |
| 720 | + if (long_buffer_sz > UINT16_MAX) { |
| 721 | + printf("Buffer size (%lu bytes) too big\n", long_buffer_sz); |
| 722 | + return 1; |
| 723 | + } |
| 724 | + |
| 725 | + buffer_sz = (uint16_t)long_buffer_sz; |
| 726 | + |
| 727 | + if (argc > 3) { |
| 728 | + /* block_sz given */ |
| 729 | + char *endptr = NULL; |
| 730 | + unsigned long long_block_sz = strtoul(argv[3], &endptr, 0); |
| 731 | + |
| 732 | + if (!endptr || *endptr) { |
| 733 | + printf("Invalid block size %s\n", argv[3]); |
| 734 | + return 1; |
| 735 | + } |
| 736 | + |
| 737 | + if (long_block_sz > buffer_sz) { |
| 738 | + printf("Block size (%lu bytes) too big\n", long_block_sz); |
| 739 | + return 1; |
| 740 | + } |
| 741 | + |
| 742 | + block_sz = (uint16_t)long_block_sz; |
| 743 | + } |
| 744 | + } |
| 745 | + |
| 746 | + /* Allocate the buffer on the stack */ |
| 747 | + uint8_t buffer[buffer_sz]; |
| 748 | + |
| 749 | + /* Open the file for writing, create if needed */ |
| 750 | + error = filereader_open( |
| 751 | + &parser, /* CBOR context */ |
| 752 | + &value, /* CBOR cursor */ |
| 753 | + &context, /* Reader context */ |
| 754 | + buffer, buffer_sz, /* Reader buffer & size */ |
| 755 | + argv[1], /* File name */ |
| 756 | + 0, /* Open flags */ |
| 757 | + block_sz /* Block size */ |
| 758 | + ); |
| 759 | + |
| 760 | + if (error != CborNoError) { |
| 761 | + printf("Failed to open %s for reading: ", argv[1]); |
| 762 | + print_err(error); |
| 763 | + } else { |
| 764 | + error = dumprecursive(&value, 0); |
| 765 | + if (error != CborNoError) { |
| 766 | + printf("Failed to read file: "); |
| 767 | + print_err(error); |
| 768 | + } |
| 769 | + |
| 770 | + error = filereader_close(&context); |
| 771 | + if (error != CborNoError) { |
| 772 | + printf("Failed to close file: "); |
| 773 | + print_err(error); |
| 774 | + } |
| 775 | + } |
| 776 | + |
| 777 | + if (error != CborNoError) { |
| 778 | + return 2; |
| 779 | + } else { |
| 780 | + return 0; |
| 781 | + } |
| 782 | + } |
| 783 | +} |
0 commit comments