1 #include "../src/cbor.h"
9 static char *readfile(const char *fname, size_t *size)
12 FILE *f = fopen(fname, "rb");
15 if (fstat(fileno(f), &st) == -1)
17 char *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 char *buf, size_t len)
32 printf("%02X ", (unsigned char)*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:
71 case CborTextStringType: {
74 err = cbor_value_dup_string(it, &buf, &n, it);
76 return err; // parse error
77 if (type == CborByteStringType) {
89 cbor_value_get_tag(it, &tag); // can't fail
90 printf("Tag(%lld)\n", (long long)tag);
94 case CborSimpleType: {
96 cbor_value_get_simple_type(it, &type); // can't fail
97 printf("simple(%u)\n", type);
105 case CborUndefinedType:
109 case CborBooleanType: {
111 cbor_value_get_boolean(it, &val); // can't fail
112 puts(val ? "true" : "false");
116 case CborDoubleType: {
121 cbor_value_get_float(it, &f);
124 cbor_value_get_double(it, &val);
129 case CborHalfFloatType: {
131 cbor_value_get_half_float(it, &val);
132 printf("__f16(%04x)\n", val);
136 case CborInvalidType:
137 assert(false); // can't happen
141 err = cbor_value_advance_fixed(it);
148 int main(int argc, char **argv)
151 puts("simplereader <filename>");
156 char *buf = readfile(argv[1], &length);
164 CborError err = cbor_parser_init(buf, length, 0, &parser, &it);
166 err = dumprecursive(&it, 0);
170 fprintf(stderr, "CBOR parsing failure at offset %ld: %s\n",
171 it.ptr - buf, cbor_error_string(err));