1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
|
/*
* Copyright (c) 2018 Vasile Vilvoiu <vasi.vilvoiu@gmail.com>
*
* libecbor is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <string.h>
#include <malloc.h>
#include <ecbor.h>
/*
* Command line arguments
*/
static struct option long_options[] = {
{ "help", no_argument, 0, 'h' },
{ 0, 0, 0, 0 }
};
void
print_help (void);
void
print_ecbor_error (ecbor_error_t err);
ecbor_error_t
print_ecbor_item (ecbor_item_t *item, unsigned int level, char *prefix);
/*
* Print help
*/
void
print_help (void)
{
printf ("Usage: ecbor-describe [options] <filename>\n");
printf (" options:\n");
printf (" -h, --help Display this help message\n");
}
void
print_ecbor_error (ecbor_error_t err)
{
printf ("ECBOR error %d\n", err);
}
ecbor_error_t
print_ecbor_item (ecbor_item_t *item, unsigned int level, char *prefix)
{
static const unsigned int max_str_print_len = 64;
static const char *msg_too_large = "<too_large>";
ecbor_major_type_t mt;
ecbor_error_t rc;
unsigned int i;
for(i = 0; i < level * 2; i ++) putchar(' ');
printf (prefix);
mt = ecbor_get_type (item);
switch (mt) {
case ECBOR_MT_NINT:
{
int64_t val;
rc = ecbor_get_value (item, (void *) &val);
if (rc != ECBOR_OK) {
return rc;
}
printf ("[NINT] value %lld\n", (long long int)val);
}
break;
case ECBOR_MT_UINT:
{
uint64_t val;
rc = ecbor_get_value (item, (void *) &val);
if (rc != ECBOR_OK) {
return rc;
}
printf ("[UINT] value %llu\n", (unsigned long long int) val);
}
break;
case ECBOR_MT_STR:
{
uint64_t len;
char *val;
rc = ecbor_get_length (item, &len);
if (rc != ECBOR_OK) {
return rc;
}
rc = ecbor_get_value (item, (char *) &val);
if (rc != ECBOR_OK) {
return rc;
}
printf ("[STR] len %u %s value '%.*s'\n", (unsigned int) len,
(ECBOR_IS_INDEFINITE (*item) ? "(indefinite)" : ""),
(int)(len <= max_str_print_len ? len : strlen(msg_too_large)),
(len <= max_str_print_len ? val : msg_too_large));
}
break;
case ECBOR_MT_BSTR:
{
uint64_t len;
char *val;
rc = ecbor_get_length (item, &len);
if (rc != ECBOR_OK) {
return rc;
}
rc = ecbor_get_value (item, (char *) &val);
if (rc != ECBOR_OK) {
return rc;
}
printf ("[BSTR] len %u %s value ", (unsigned int) len,
(ECBOR_IS_INDEFINITE (*item) ? "(indefinite)" : ""));
if (len > max_str_print_len) {
printf ("'%s'\n", msg_too_large);
} else {
printf ("'");
for (i = 0; i < len; i ++) {
printf("%02x", val[i]);
}
printf ("'\n");
}
}
break;
case ECBOR_MT_ARRAY:
{
uint64_t len, i;
rc = ecbor_get_length (item, &len);
if (rc != ECBOR_OK) {
return rc;
}
printf ("[ARRAY] len %u %s\n", (unsigned int) len,
(ECBOR_IS_INDEFINITE (*item) ? "(indefinite)" : ""));
for (i = 0; i < len; i ++) {
ecbor_item_t child;
rc = ecbor_get_array_item (item, i, &child);
if (rc != ECBOR_OK) {
return rc;
}
rc = print_ecbor_item (&child, level+1, "");
if (rc != ECBOR_OK) {
return rc;
}
}
}
break;
case ECBOR_MT_MAP:
{
uint64_t len, i;
char kv_msg[100] = { 0 };
rc = ecbor_get_length (item, &len);
if (rc != ECBOR_OK) {
return rc;
}
printf ("[MAP] len %u %s\n", (unsigned int) len,
(ECBOR_IS_INDEFINITE (*item) ? "(indefinite)" : ""));
for (i = 0; i < len; i ++) {
ecbor_item_t k, v;
rc = ecbor_get_map_item (item, i, &k, &v);
if (rc != ECBOR_OK) {
return rc;
}
sprintf (kv_msg, "key[%u]: ", (unsigned int) i);
rc = print_ecbor_item (&k, level+2, kv_msg);
if (rc != ECBOR_OK) {
return rc;
}
sprintf (kv_msg, "val[%u]: ", (unsigned int) i);
rc = print_ecbor_item (&v, level+2, kv_msg);
if (rc != ECBOR_OK) {
return rc;
}
}
}
break;
case ECBOR_MT_TAG:
{
int64_t val;
ecbor_item_t child;
rc = ecbor_get_value (item, (void *) &val);
if (rc != ECBOR_OK) {
return rc;
}
printf ("[TAG] value %lld\n", (long long int)val);
rc = ecbor_get_tag_item (item, &child);
if (rc != ECBOR_OK) {
return rc;
}
rc = print_ecbor_item (&child, level+1, "");
if (rc != ECBOR_OK) {
return rc;
}
}
break;
case ECBOR_MT_FP32:
{
float val;
rc = ecbor_get_value (item, (void *) &val);
if (rc != ECBOR_OK) {
return rc;
}
printf ("[FP32] value %f\n", val);
}
break;
case ECBOR_MT_FP64:
{
double val;
rc = ecbor_get_value (item, (void *) &val);
if (rc != ECBOR_OK) {
return rc;
}
printf ("[FP64] value %f\n", val);
}
break;
default:
printf ("[UNKNOWN]\n");
break;
}
return ECBOR_OK;
}
/*
* Program entry
*/
int
main(int argc, char **argv)
{
char *filename = NULL;
unsigned char *cbor = NULL;
long int cbor_length = 0;
/* parse arguments */
while (1) {
int option_index, c;
c = getopt_long (argc, argv, "h", long_options, &option_index);
if (c == -1) {
break;
}
switch (c) {
default:
print_help ();
return 0;
}
}
if (optind != (argc-1)) {
fprintf (stderr, "Expecting exactly one file name!\n");
print_help ();
return -1;
} else {
filename = strdup (argv[optind]);
}
/* load CBOR data from file */
{
FILE *fp;
fprintf (stderr, "Reading CBOR from file '%s'\n", filename);
fp = fopen (filename, "rb");
if (!fp) {
fprintf (stderr, "Error opening file!\n");
return -1;
}
if (fseek (fp, 0L, SEEK_END)) {
fprintf (stderr, "Error seeking end of file!\n");
fclose (fp);
return -1;
}
cbor_length = ftell(fp);
if (cbor_length < 0) {
fprintf (stderr, "Error determining input size!\n");
fclose (fp);
return -1;
}
if (fseek (fp, 0L, SEEK_SET)) {
fprintf (stderr, "Error seeking beginning of file!\n");
fclose (fp);
return -1;
}
cbor = (unsigned char *) malloc (cbor_length);
if (!cbor) {
fprintf (stderr, "Error allocating %d bytes!\n", (int) cbor_length);
fclose (fp);
return -1;
}
if (fread (cbor, 1, cbor_length, fp) != (size_t) cbor_length) {
fprintf (stderr, "Error reading %d bytes!\n", (int) cbor_length);
fclose (fp);
return -1;
}
fclose (fp);
free (filename);
}
/* parse CBOR data */
{
ecbor_decode_context_t context;
ecbor_item_t item;
ecbor_error_t rc;
rc = ecbor_initialize_decode (&context, cbor, cbor_length);
if (rc != ECBOR_OK) {
print_ecbor_error (rc);
return -1;
}
fprintf (stderr, "CBOR objects:\n");
while (1) {
rc = ecbor_decode (&context, &item);
if (rc != ECBOR_OK) {
if (rc == ECBOR_END_OF_BUFFER) {
break;
}
print_ecbor_error (rc);
return -1;
}
rc = print_ecbor_item (&item, 0, "");
if (rc != ECBOR_OK) {
print_ecbor_error (rc);
return -1;
}
}
}
free (cbor);
/* all ok */
return 0;
}
|