1 #include "../src/cbor.h"
9 static uint8_t *readfile(const char *fname, size_t *size)
12 FILE *f = fopen(fname, "rb");
15 if (fstat(fileno(f), &st) == -1)
17 uint8_t *buf = malloc(st.st_size);
18 *size = fread(buf, st.st_size, 1, f);
23 static void indent(int nestingLevel)
25 while (nestingLevel--)
29 static void dumpbytes(const uint8_t *buf, size_t len)
32 printf("%02X ", *buf++);
35 static bool dumprecursive(CborValue *it, int nestingLevel)
37 while (!cbor_value_at_end(it)) {
39 CborType type = cbor_value_get_type(it);
47 assert(cbor_value_is_container(it));
48 puts(type == CborArrayType ? "Array[" : "Map[");
49 err = cbor_value_enter_container(it, &recursed);
51 return err; // parse error
52 err = dumprecursive(&recursed, nestingLevel + 1);
54 return err; // parse error
55 err = cbor_value_leave_container(it, &recursed);
57 return err; // parse error
63 case CborIntegerType: {
65 cbor_value_get_int64(it, &val); // can't fail
66 printf("%lld\n", (long long)val);
70 case CborByteStringType: {
73 err = cbor_value_dup_byte_string(it, &buf, &n, it);
75 return err; // parse error
82 case CborTextStringType: {
85 err = cbor_value_dup_text_string(it, &buf, &n, it);
87 return err; // parse error
95 cbor_value_get_tag(it, &tag); // can't fail
96 printf("Tag(%lld)\n", (long long)tag);
100 case CborSimpleType: {
102 cbor_value_get_simple_type(it, &type); // can't fail
103 printf("simple(%u)\n", type);
111 case CborUndefinedType:
115 case CborBooleanType: {
117 cbor_value_get_boolean(it, &val); // can't fail
118 puts(val ? "true" : "false");
122 case CborDoubleType: {
127 cbor_value_get_float(it, &f);
130 cbor_value_get_double(it, &val);
135 case CborHalfFloatType: {
137 cbor_value_get_half_float(it, &val);
138 printf("__f16(%04x)\n", val);
142 case CborInvalidType:
143 assert(false); // can't happen
147 err = cbor_value_advance_fixed(it);
154 int main(int argc, char **argv)
157 puts("simplereader <filename>");
162 uint8_t *buf = readfile(argv[1], &length);
170 CborError err = cbor_parser_init(buf, length, 0, &parser, &it);
172 err = dumprecursive(&it, 0);
176 fprintf(stderr, "CBOR parsing failure at offset %ld: %s\n",
177 it.ptr - buf, cbor_error_string(err));