aboutsummaryrefslogtreecommitdiff
path: root/contrib/libcbor/src/cbor.c
blob: 76b892c6b7098093ced7c7989637d59f8a0de45b (plain) (blame)
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
/*
 * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
 *
 * libcbor is free software; you can redistribute it and/or modify
 * it under the terms of the MIT license. See LICENSE for details.
 */

#include "cbor.h"
#include "cbor/internal/builder_callbacks.h"
#include "cbor/internal/loaders.h"

cbor_item_t *cbor_load(cbor_data source, size_t source_size,
                       struct cbor_load_result *result) {
  /* Context stack */
  static struct cbor_callbacks callbacks = {
      .uint8 = &cbor_builder_uint8_callback,
      .uint16 = &cbor_builder_uint16_callback,
      .uint32 = &cbor_builder_uint32_callback,
      .uint64 = &cbor_builder_uint64_callback,

      .negint8 = &cbor_builder_negint8_callback,
      .negint16 = &cbor_builder_negint16_callback,
      .negint32 = &cbor_builder_negint32_callback,
      .negint64 = &cbor_builder_negint64_callback,

      .byte_string = &cbor_builder_byte_string_callback,
      .byte_string_start = &cbor_builder_byte_string_start_callback,

      .string = &cbor_builder_string_callback,
      .string_start = &cbor_builder_string_start_callback,

      .array_start = &cbor_builder_array_start_callback,
      .indef_array_start = &cbor_builder_indef_array_start_callback,

      .map_start = &cbor_builder_map_start_callback,
      .indef_map_start = &cbor_builder_indef_map_start_callback,

      .tag = &cbor_builder_tag_callback,

      .null = &cbor_builder_null_callback,
      .undefined = &cbor_builder_undefined_callback,
      .boolean = &cbor_builder_boolean_callback,
      .float2 = &cbor_builder_float2_callback,
      .float4 = &cbor_builder_float4_callback,
      .float8 = &cbor_builder_float8_callback,
      .indef_break = &cbor_builder_indef_break_callback};

  if (source_size == 0) {
    result->error.code = CBOR_ERR_NODATA;
    return NULL;
  }
  struct _cbor_stack stack = _cbor_stack_init();

  /* Target for callbacks */
  struct _cbor_decoder_context context = (struct _cbor_decoder_context){
      .stack = &stack, .creation_failed = false, .syntax_error = false};
  struct cbor_decoder_result decode_result;
  *result =
      (struct cbor_load_result){.read = 0, .error = {.code = CBOR_ERR_NONE}};

  do {
    if (source_size > result->read) { /* Check for overflows */
      decode_result =
          cbor_stream_decode(source + result->read, source_size - result->read,
                             &callbacks, &context);
    } else {
      result->error = (struct cbor_error){.code = CBOR_ERR_NOTENOUGHDATA,
                                          .position = result->read};
      goto error;
    }

    switch (decode_result.status) {
      case CBOR_DECODER_FINISHED:
        /* Everything OK */
        {
          result->read += decode_result.read;
          break;
        }
      case CBOR_DECODER_NEDATA:
        /* Data length doesn't match MTB expectation */
        {
          result->error.code = CBOR_ERR_NOTENOUGHDATA;
          goto error;
        }
      case CBOR_DECODER_ERROR:
        /* Reserved/malformated item */
        {
          result->error.code = CBOR_ERR_MALFORMATED;
          goto error;
        }
    }

    if (context.creation_failed) {
      /* Most likely unsuccessful allocation - our callback has failed */
      result->error.code = CBOR_ERR_MEMERROR;
      goto error;
    } else if (context.syntax_error) {
      result->error.code = CBOR_ERR_SYNTAXERROR;
      goto error;
    }
  } while (stack.size > 0);

  /* Move the result before free */
  cbor_item_t *result_item = context.root;
  return result_item;

error:
  result->error.position = result->read;
  // debug_print("Failed with decoder error %d at %d\n", result->error.code,
  // result->error.position); cbor_describe(stack.top->item, stdout);
  /* Free the stack */
  while (stack.size > 0) {
    cbor_decref(&stack.top->item);
    _cbor_stack_pop(&stack);
  }
  return NULL;
}

static cbor_item_t *_cbor_copy_int(cbor_item_t *item, bool negative) {
  cbor_item_t *res;
  switch (cbor_int_get_width(item)) {
    case CBOR_INT_8:
      res = cbor_build_uint8(cbor_get_uint8(item));
      break;
    case CBOR_INT_16:
      res = cbor_build_uint16(cbor_get_uint16(item));
      break;
    case CBOR_INT_32:
      res = cbor_build_uint32(cbor_get_uint32(item));
      break;
    case CBOR_INT_64:
      res = cbor_build_uint64(cbor_get_uint64(item));
      break;
    default:
      return NULL;
  }

  if (negative) cbor_mark_negint(res);

  return res;
}

static cbor_item_t *_cbor_copy_float_ctrl(cbor_item_t *item) {
  switch (cbor_float_get_width(item)) {
    case CBOR_FLOAT_0:
      return cbor_build_ctrl(cbor_ctrl_value(item));
    case CBOR_FLOAT_16:
      return cbor_build_float2(cbor_float_get_float2(item));
    case CBOR_FLOAT_32:
      return cbor_build_float4(cbor_float_get_float4(item));
    case CBOR_FLOAT_64:
      return cbor_build_float8(cbor_float_get_float8(item));
  }

  return NULL;
}

cbor_item_t *cbor_copy(cbor_item_t *item) {
  switch (cbor_typeof(item)) {
    case CBOR_TYPE_UINT:
      return _cbor_copy_int(item, false);
    case CBOR_TYPE_NEGINT:
      return _cbor_copy_int(item, true);
    case CBOR_TYPE_BYTESTRING:
      if (cbor_bytestring_is_definite(item)) {
        return cbor_build_bytestring(cbor_bytestring_handle(item),
                                     cbor_bytestring_length(item));
      } else {
        cbor_item_t *res = cbor_new_indefinite_bytestring();
        for (size_t i = 0; i < cbor_bytestring_chunk_count(item); i++)
          cbor_bytestring_add_chunk(
              res,
              cbor_move(cbor_copy(cbor_bytestring_chunks_handle(item)[i])));
        return res;
      }
    case CBOR_TYPE_STRING:
      if (cbor_string_is_definite(item)) {
        return cbor_build_stringn((const char *)cbor_string_handle(item),
                                  cbor_string_length(item));
      } else {
        cbor_item_t *res = cbor_new_indefinite_string();
        for (size_t i = 0; i < cbor_string_chunk_count(item); i++)
          cbor_string_add_chunk(
              res, cbor_move(cbor_copy(cbor_string_chunks_handle(item)[i])));
        return res;
      }
    case CBOR_TYPE_ARRAY: {
      cbor_item_t *res;
      if (cbor_array_is_definite(item))
        res = cbor_new_definite_array(cbor_array_size(item));
      else
        res = cbor_new_indefinite_array();

      for (size_t i = 0; i < cbor_array_size(item); i++)
        cbor_array_push(
            res, cbor_move(cbor_copy(cbor_move(cbor_array_get(item, i)))));
      return res;
    }
    case CBOR_TYPE_MAP: {
      cbor_item_t *res;
      if (cbor_map_is_definite(item))
        res = cbor_new_definite_map(cbor_map_size(item));
      else
        res = cbor_new_indefinite_map();

      struct cbor_pair *it = cbor_map_handle(item);
      for (size_t i = 0; i < cbor_map_size(item); i++)
        cbor_map_add(res, (struct cbor_pair){
                              .key = cbor_move(cbor_copy(it[i].key)),
                              .value = cbor_move(cbor_copy(it[i].value))});
      return res;
    }
    case CBOR_TYPE_TAG:
      return cbor_build_tag(
          cbor_tag_value(item),
          cbor_move(cbor_copy(cbor_move(cbor_tag_item(item)))));
    case CBOR_TYPE_FLOAT_CTRL:
      return _cbor_copy_float_ctrl(item);
  }

  return NULL;
}

#if CBOR_PRETTY_PRINTER

#include <inttypes.h>
#include <locale.h>
#include <stdlib.h>
#include <wchar.h>

#define __STDC_FORMAT_MACROS

static int _pow(int b, int ex) {
  if (ex == 0) return 1;
  int res = b;
  while (--ex > 0) res *= b;
  return res;
}

static void _cbor_nested_describe(cbor_item_t *item, FILE *out, int indent) {
  setlocale(LC_ALL, "");
  switch (cbor_typeof(item)) {
    case CBOR_TYPE_UINT: {
      fprintf(out, "%*s[CBOR_TYPE_UINT] ", indent, " ");
      fprintf(out, "Width: %dB, ", _pow(2, cbor_int_get_width(item)));
      fprintf(out, "Value: %" PRIu64 "\n", cbor_get_int(item));
      break;
    };
    case CBOR_TYPE_NEGINT: {
      fprintf(out, "%*s[CBOR_TYPE_NEGINT] ", indent, " ");
      fprintf(out, "Width: %dB, ", _pow(2, cbor_int_get_width(item)));
      fprintf(out, "Value: -%" PRIu64 " -1\n", cbor_get_int(item));
      break;
    };
    case CBOR_TYPE_BYTESTRING: {
      fprintf(out, "%*s[CBOR_TYPE_BYTESTRING] ", indent, " ");
      if (cbor_bytestring_is_indefinite(item)) {
        fprintf(out, "Indefinite, with %zu chunks:\n",
                cbor_bytestring_chunk_count(item));
        for (size_t i = 0; i < cbor_bytestring_chunk_count(item); i++)
          _cbor_nested_describe(cbor_bytestring_chunks_handle(item)[i], out,
                                indent + 4);
      } else {
        fprintf(out, "Definite, length %zuB\n", cbor_bytestring_length(item));
      }
      break;
    };
    case CBOR_TYPE_STRING: {
      fprintf(out, "%*s[CBOR_TYPE_STRING] ", indent, " ");
      if (cbor_string_is_indefinite(item)) {
        fprintf(out, "Indefinite, with %zu chunks:\n",
                cbor_string_chunk_count(item));
        for (size_t i = 0; i < cbor_string_chunk_count(item); i++)
          _cbor_nested_describe(cbor_string_chunks_handle(item)[i], out,
                                indent + 4);
      } else {
        fprintf(out, "Definite, length %zuB, %zu codepoints\n",
                cbor_string_length(item), cbor_string_codepoint_count(item));
        /* Careful - this doesn't support multibyte characters! */
        /* Printing those is out of the scope of this demo :) */
        /* libICU is your friend */
        fprintf(out, "%*s", indent + 4, " ");
        /* XXX: no null at the end -> confused vprintf */
        fwrite(cbor_string_handle(item), (int)cbor_string_length(item), 1, out);
        fprintf(out, "\n");
      }
      break;
    };
    case CBOR_TYPE_ARRAY: {
      fprintf(out, "%*s[CBOR_TYPE_ARRAY] ", indent, " ");
      if (cbor_array_is_definite(item)) {
        fprintf(out, "Definite, size: %zu\n", cbor_array_size(item));
      } else {
        fprintf(out, "Indefinite, size:  %zu\n", cbor_array_size(item));
      }

      for (size_t i = 0; i < cbor_array_size(item); i++)
        _cbor_nested_describe(cbor_array_handle(item)[i], out, indent + 4);
      break;
    };
    case CBOR_TYPE_MAP: {
      fprintf(out, "%*s[CBOR_TYPE_MAP] ", indent, " ");
      if (cbor_map_is_definite(item)) {
        fprintf(out, "Definite, size: %zu\n", cbor_map_size(item));
      } else {
        fprintf(out, "Indefinite, size:  %zu\n", cbor_map_size(item));
      }

      for (size_t i = 0; i < cbor_map_size(item); i++) {
        _cbor_nested_describe(cbor_map_handle(item)[i].key, out, indent + 4);
        _cbor_nested_describe(cbor_map_handle(item)[i].value, out, indent + 4);
      }
      break;
    };
    case CBOR_TYPE_TAG: {
      fprintf(out, "%*s[CBOR_TYPE_TAG] ", indent, " ");
      fprintf(out, "Value: %" PRIu64 "\n", cbor_tag_value(item));
      _cbor_nested_describe(cbor_move(cbor_tag_item(item)), out, indent + 4);
      break;
    };
    case CBOR_TYPE_FLOAT_CTRL: {
      fprintf(out, "%*s[CBOR_TYPE_FLOAT_CTRL] ", indent, " ");
      if (cbor_float_ctrl_is_ctrl(item)) {
        if (cbor_is_bool(item))
          fprintf(out, "Bool: %s\n", cbor_get_bool(item) ? "true" : "false");
        else if (cbor_is_undef(item))
          fprintf(out, "Undefined\n");
        else if (cbor_is_null(item))
          fprintf(out, "Null\n");
        else
          fprintf(out, "Simple value %d\n", cbor_ctrl_value(item));
      } else {
        fprintf(out, "Width: %dB, ", _pow(2, cbor_float_get_width(item)));
        fprintf(out, "value: %lf\n", cbor_float_get_float(item));
      }
      break;
    };
  }
}

void cbor_describe(cbor_item_t *item, FILE *out) {
  _cbor_nested_describe(item, out, 0);
}

#endif