xref: /freebsd/contrib/libcbor/examples/cbor_sequence.c (revision b5b9517bfe394e55088f5a05882eabae7e9b7b29)
1 /*
2  * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
3  *
4  * libcbor is free software; you can redistribute it and/or modify
5  * it under the terms of the MIT license. See LICENSE for details.
6  */
7 
8 #include <stdio.h>
9 #include <string.h>
10 
11 #include "cbor.h"
12 
13 void write_cbor_sequence(const char* filename) {
14   FILE* file = fopen(filename, "wb");
15   if (!file) {
16     fprintf(stderr, "Error: Could not open file %s for writing\n", filename);
17     return;
18   }
19 
20   // Create example CBOR items
21   cbor_item_t* int_item = cbor_build_uint32(42);
22   cbor_item_t* string_item = cbor_build_string("Hello, CBOR!");
23   cbor_item_t* array_item = cbor_new_definite_array(2);
24   assert(cbor_array_push(array_item, cbor_build_uint8(1)));
25   assert(cbor_array_push(array_item, cbor_build_uint8(2)));
26 
27   // Serialize and write items to the file
28   unsigned char* buffer;
29   size_t buffer_size;
30 
31   cbor_serialize_alloc(int_item, &buffer, &buffer_size);
32   fwrite(buffer, 1, buffer_size, file);
33   free(buffer);
34   cbor_decref(&int_item);
35 
36   cbor_serialize_alloc(string_item, &buffer, &buffer_size);
37   fwrite(buffer, 1, buffer_size, file);
38   free(buffer);
39   cbor_decref(&string_item);
40 
41   cbor_serialize_alloc(array_item, &buffer, &buffer_size);
42   fwrite(buffer, 1, buffer_size, file);
43   free(buffer);
44   cbor_decref(&array_item);
45 
46   fclose(file);
47   printf("CBOR sequence written to %s\n", filename);
48 }
49 
50 void read_cbor_sequence(const char* filename) {
51   FILE* file = fopen(filename, "rb");
52   if (!file) {
53     fprintf(stderr, "Error: Could not open file %s\n", filename);
54     return;
55   }
56 
57   fseek(file, 0, SEEK_END);
58   size_t file_size = ftell(file);
59   fseek(file, 0, SEEK_SET);
60 
61   unsigned char* buffer = malloc(file_size);
62   if (!buffer) {
63     fprintf(stderr, "Error: Could not allocate memory\n");
64     fclose(file);
65     return;
66   }
67 
68   fread(buffer, 1, file_size, file);
69   fclose(file);
70 
71   struct cbor_load_result result;
72   size_t offset = 0;
73 
74   while (offset < file_size) {
75     cbor_item_t* item = cbor_load(buffer + offset, file_size - offset, &result);
76     if (result.error.code != CBOR_ERR_NONE) {
77       fprintf(stderr, "Error: Failed to parse CBOR item at offset %zu\n",
78               offset);
79       break;
80     }
81 
82     cbor_describe(item, stdout);
83     printf("\n");
84 
85     offset += result.read;
86     cbor_decref(&item);
87   }
88 
89   free(buffer);
90 }
91 
92 int main(int argc, char* argv[]) {
93   if (argc != 3) {
94     fprintf(stderr, "Usage: %s <r|w> <file>\n", argv[0]);
95     return 1;
96   }
97 
98   if (strcmp(argv[1], "w") == 0) {
99     write_cbor_sequence(argv[2]);
100   } else if (strcmp(argv[1], "r") == 0) {
101     read_cbor_sequence(argv[2]);
102   } else {
103     fprintf(stderr,
104             "Error: First argument must be 'r' (read) or 'w' (write)\n");
105     return 1;
106   }
107 
108   return 0;
109 }
110