xref: /freebsd-src/contrib/libcbor/examples/readfile.c (revision abd872540f24cfc7dbd1ea29b6918c7082a22108)
110ff414cSEd Maste /*
210ff414cSEd Maste  * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
310ff414cSEd Maste  *
410ff414cSEd Maste  * libcbor is free software; you can redistribute it and/or modify
510ff414cSEd Maste  * it under the terms of the MIT license. See LICENSE for details.
610ff414cSEd Maste  */
710ff414cSEd Maste 
810ff414cSEd Maste #include <stdio.h>
910ff414cSEd Maste #include "cbor.h"
1010ff414cSEd Maste 
usage(void)115d3e7166SEd Maste void usage(void) {
1210ff414cSEd Maste   printf("Usage: readfile [input file]\n");
1310ff414cSEd Maste   exit(1);
1410ff414cSEd Maste }
1510ff414cSEd Maste 
1610ff414cSEd Maste /*
1710ff414cSEd Maste  * Reads data from a file. Example usage:
1810ff414cSEd Maste  * $ ./examples/readfile examples/data/nested_array.cbor
1910ff414cSEd Maste  */
2010ff414cSEd Maste 
main(int argc,char * argv[])2110ff414cSEd Maste int main(int argc, char* argv[]) {
2210ff414cSEd Maste   if (argc != 2) usage();
2310ff414cSEd Maste   FILE* f = fopen(argv[1], "rb");
2410ff414cSEd Maste   if (f == NULL) usage();
2510ff414cSEd Maste   fseek(f, 0, SEEK_END);
2610ff414cSEd Maste   size_t length = (size_t)ftell(f);
2710ff414cSEd Maste   fseek(f, 0, SEEK_SET);
2810ff414cSEd Maste   unsigned char* buffer = malloc(length);
2910ff414cSEd Maste   fread(buffer, length, 1, f);
3010ff414cSEd Maste 
3110ff414cSEd Maste   /* Assuming `buffer` contains `length` bytes of input data */
3210ff414cSEd Maste   struct cbor_load_result result;
3310ff414cSEd Maste   cbor_item_t* item = cbor_load(buffer, length, &result);
345d3e7166SEd Maste   free(buffer);
3510ff414cSEd Maste 
3610ff414cSEd Maste   if (result.error.code != CBOR_ERR_NONE) {
3710ff414cSEd Maste     printf(
3810ff414cSEd Maste         "There was an error while reading the input near byte %zu (read %zu "
3910ff414cSEd Maste         "bytes in total): ",
4010ff414cSEd Maste         result.error.position, result.read);
4110ff414cSEd Maste     switch (result.error.code) {
4210ff414cSEd Maste       case CBOR_ERR_MALFORMATED: {
4310ff414cSEd Maste         printf("Malformed data\n");
4410ff414cSEd Maste         break;
4510ff414cSEd Maste       }
4610ff414cSEd Maste       case CBOR_ERR_MEMERROR: {
4710ff414cSEd Maste         printf("Memory error -- perhaps the input is too large?\n");
4810ff414cSEd Maste         break;
4910ff414cSEd Maste       }
5010ff414cSEd Maste       case CBOR_ERR_NODATA: {
5110ff414cSEd Maste         printf("The input is empty\n");
5210ff414cSEd Maste         break;
5310ff414cSEd Maste       }
5410ff414cSEd Maste       case CBOR_ERR_NOTENOUGHDATA: {
5510ff414cSEd Maste         printf("Data seem to be missing -- is the input complete?\n");
5610ff414cSEd Maste         break;
5710ff414cSEd Maste       }
5810ff414cSEd Maste       case CBOR_ERR_SYNTAXERROR: {
5910ff414cSEd Maste         printf(
6010ff414cSEd Maste             "Syntactically malformed data -- see "
61*abd87254SEd Maste             "https://www.rfc-editor.org/info/std94\n");
6210ff414cSEd Maste         break;
6310ff414cSEd Maste       }
6410ff414cSEd Maste       case CBOR_ERR_NONE: {
6510ff414cSEd Maste         // GCC's cheap dataflow analysis gag
6610ff414cSEd Maste         break;
6710ff414cSEd Maste       }
6810ff414cSEd Maste     }
6910ff414cSEd Maste     exit(1);
7010ff414cSEd Maste   }
7110ff414cSEd Maste 
7210ff414cSEd Maste   /* Pretty-print the result */
7310ff414cSEd Maste   cbor_describe(item, stdout);
7410ff414cSEd Maste   fflush(stdout);
7510ff414cSEd Maste   /* Deallocate the result */
7610ff414cSEd Maste   cbor_decref(&item);
7710ff414cSEd Maste 
7810ff414cSEd Maste   fclose(f);
7910ff414cSEd Maste }
80