-
Notifications
You must be signed in to change notification settings - Fork 219
Expand file tree
/
Copy pathcbortojson.c
More file actions
823 lines (739 loc) · 28.9 KB
/
cbortojson.c
File metadata and controls
823 lines (739 loc) · 28.9 KB
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
/****************************************************************************
**
** Copyright (C) 2021 Intel Corporation
**
** Permission is hereby granted, free of charge, to any person obtaining a copy
** of this software and associated documentation files (the "Software"), to deal
** in the Software without restriction, including without limitation the rights
** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
** copies of the Software, and to permit persons to whom the Software is
** furnished to do so, subject to the following conditions:
**
** The above copyright notice and this permission notice shall be included in
** all copies or substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
** THE SOFTWARE.
**
****************************************************************************/
#include "cborinternalmacros_p.h"
#include "tinycbor/cbor.h"
#include "cborjson.h"
#include "cborinternal_p.h"
#include "compilersupport_p.h"
#include "cborinternal_p.h"
#include "memory.h"
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/**
* \defgroup CborToJson Converting CBOR to JSON
* \brief Group of functions used to convert CBOR to JSON.
*
* This group contains two functions that can be used to convert a \ref
* CborValue object to an equivalent JSON representation. This module attempts
* to follow the recommendations from RFC 7049 section 4.1 "Converting from
* CBOR to JSON", though it has a few differences. They are noted below.
*
* These functions produce a "minified" JSON output, with no spacing,
* indentation or line breaks. If those are necessary, they need to be applied
* in a post-processing phase.
*
* Note that JSON cannot support all CBOR types with fidelity, so the
* conversion is usually lossy. For that reason, TinyCBOR supports adding a set
* of metadata JSON values that can be used by a JSON-to-CBOR converter to
* restore the original data types.
*
* The TinyCBOR library does not provide a way to convert from JSON
* representation back to encoded form. However, it provides a tool called
* \c json2cbor which can be used for that purpose. That tool supports the
* metadata format that these functions may produce.
*
* Either of the functions in this section will attempt to convert exactly one
* CborValue object to JSON. Those functions may return any error documented
* for the functions for CborParsing. In addition, if the C standard library
* stream functions return with error, the text conversion will return with
* error CborErrorIO.
*
* These functions also perform UTF-8 validation in CBOR text strings. If they
* encounter a sequence of bytes that is not permitted in UTF-8, they will return
* CborErrorInvalidUtf8TextString. That includes encoding of surrogate points
* in UTF-8.
*
* \warning The metadata produced by these functions is not guaranteed to
* remain stable. A future update of TinyCBOR may produce different output for
* the same input and parsers may be unable to handle it.
*
* \sa CborParsing, CborPretty, cbor_parser_init()
*/
/**
* \addtogroup CborToJson
* @{
* <h2 class="groupheader">Conversion limitations</h2>
*
* When converting from CBOR to JSON, there may be information loss. This
* section lists the possible scenarios.
*
* \par Number precision:
* ALL JSON numbers, due to its JavaScript heritage, are IEEE 754
* double-precision floating point. This means JSON is not capable of
* representing all integers numbers outside the range [-(2<sup>53</sup>)+1,
* 2<sup>53</sup>-1] and is not capable of representing NaN or infinite. If the
* CBOR data contains a number outside the valid range, the conversion will
* lose precision. If the input was NaN or infinite, the result of the
* conversion will be the JSON null value. In addition, the distinction between
* half-, single- and double-precision is lost.
*
* \par
* If enabled, the original value and original type are stored in the metadata.
*
* \par Non-native types:
* CBOR's type system is richer than JSON's, which means some data values
* cannot be represented when converted to JSON. The conversion silently turns
* them into strings: CBOR simple types become "simple(nn)" where \c nn is the
* simple type's value, with the exception of CBOR undefined, which becomes
* "undefined", while CBOR byte strings are converted to an Base16, Base64, or
* Base64url encoding
*
* \par
* If enabled, the original type is stored in the metadata.
*
* \par Presence of tags:
* JSON has no support for tagged values, so by default tags are dropped when
* converting to JSON. However, if the CborConvertObeyByteStringTags option is
* active (default), then certain known tags are honored and are used to format
* the conversion of the tagged byte string to JSON.
*
* \par
* If the CborConvertTagsToObjects option is active, then the tag and the
* tagged value are converted to a JSON object. Otherwise, if enabled, the
* last (innermost) tag is stored in the metadata.
*
* \par Non-string keys in maps:
* JSON requires all Object keys to be strings, while CBOR does not. By
* default, if a non-string key is found, the conversion fails with error
* CborErrorJsonObjectKeyNotString. If the CborConvertStringifyMapKeys option
* is active, then the conversion attempts to create a string representation
* using CborPretty. Note that the \c json2cbor tool is not able to parse this
* back to the original form.
*
* \par Duplicate keys in maps:
* Neither JSON nor CBOR allow duplicated keys, but current TinyCBOR does not
* validate that this is the case. If there are duplicated keys in the input,
* they will be repeated in the output, which many JSON tools may flag as
* invalid. In addition to that, if the CborConvertStringifyMapKeys option is
* active, it is possible that a non-string key in a CBOR map will be converted
* to a string form that is identical to another key.
*
* \par
* When metadata support is active, the conversion will add extra key-value
* pairs to the JSON output so it can store the metadata. It is possible that
* the keys for the metadata clash with existing keys in the JSON map.
*/
extern FILE *open_memstream(char **bufptr, size_t *sizeptr);
enum ConversionStatusFlags {
TypeWasNotNative = 0x100, /* anything but strings, boolean, null, arrays and maps */
TypeWasTagged = 0x200,
NumberPrecisionWasLost = 0x400,
NumberWasNaN = 0x800,
NumberWasInfinite = 0x1000,
NumberWasNegative = 0x2000, /* only used with NumberWasInifite or NumberWasTooBig */
FinalTypeMask = 0xff
};
typedef struct ConversionStatus {
CborTag lastTag;
uint64_t originalNumber;
int flags;
} ConversionStatus;
static CborError value_to_json(FILE *out, CborValue *it, int flags, CborType type,
int nestingLevel, ConversionStatus *status);
static void append_hex(void *buffer, uint8_t byte)
{
static const char characters[] = "0123456789abcdef";
char *str = buffer;
str[0] = characters[byte >> 4];
str[1] = characters[byte & 0xf];
}
static CborError dump_bytestring_base16(char **result, CborValue *it)
{
size_t i;
size_t n = 0;
uint8_t *buffer;
CborError err = cbor_value_calculate_string_length(it, &n);
if (err)
return err;
/* a Base16 (hex) output is twice as big as our buffer */
size_t needed;
if (mul_check_overflow(n, 2, &needed) || add_check_overflow(needed, 1, &needed))
return CborErrorDataTooLarge;
buffer = (uint8_t *)cbor_malloc(needed);
if (buffer == NULL)
/* out of memory */
return CborErrorOutOfMemory;
*result = (char *)buffer;
/* let cbor_value_copy_byte_string know we have an extra byte for the terminating NUL */
++n;
err = cbor_value_copy_byte_string(it, buffer + n - 1, &n, it);
cbor_assert(err == CborNoError);
for (i = 0; i < n; ++i) {
uint8_t byte = buffer[n + i];
append_hex(buffer + 2 * i, byte);
}
return CborNoError;
}
static CborError generic_dump_base64(char **result, CborValue *it, const char alphabet[65])
{
size_t n = 0, i;
uint8_t *buffer, *out, *in;
CborError err = cbor_value_calculate_string_length(it, &n);
if (err)
return err;
/* a Base64 output (untruncated) has 4 bytes for every 3 in the input */
size_t len, needed;
if (add_check_overflow(n, 5, &len) || mul_check_overflow(len / 3, 4, &len)
|| add_check_overflow(len, 1, &needed)) {
return CborErrorDataTooLarge;
}
buffer = (uint8_t *)cbor_malloc(needed);
if (buffer == NULL)
/* out of memory */
return CborErrorOutOfMemory;
out = buffer;
*result = (char *)buffer;
/* we read our byte string at the tail end of the buffer
* so we can do an in-place conversion while iterating forwards */
in = buffer + len - n;
/* let cbor_value_copy_byte_string know we have an extra byte for the terminating NUL */
++n;
err = cbor_value_copy_byte_string(it, in, &n, it);
cbor_assert(err == CborNoError);
uint_least32_t val = 0;
for (i = 0; n - i >= 3; i += 3) {
/* read 3 bytes x 8 bits = 24 bits */
if (false) {
#ifdef __GNUC__
} else if (i) {
__builtin_memcpy(&val, in + i - 1, sizeof(val));
val = cbor_ntohl(val);
#endif
} else {
val = (in[i] << 16) | (in[i + 1] << 8) | in[i + 2];
}
/* write 4 chars x 6 bits = 24 bits */
*out++ = alphabet[(val >> 18) & 0x3f];
*out++ = alphabet[(val >> 12) & 0x3f];
*out++ = alphabet[(val >> 6) & 0x3f];
*out++ = alphabet[val & 0x3f];
}
/* maybe 1 or 2 bytes left */
if (n - i) {
/* we can read in[i + 1] even if it's past the end of the string because
* we know (by construction) that it's a NUL byte */
#ifdef __GNUC__
uint16_t val16;
__builtin_memcpy(&val16, in + i, sizeof(val16));
val = cbor_ntohs(val16);
#else
val = (in[i] << 8) | in[i + 1];
#endif
val <<= 8;
/* the 65th character in the alphabet is our filler: either '=' or '\0' */
out[4] = '\0';
out[3] = alphabet[64];
if (n - i == 2) {
/* write the third char in 3 chars x 6 bits = 18 bits */
out[2] = alphabet[(val >> 6) & 0x3f];
} else {
out[2] = alphabet[64]; /* filler */
}
out[1] = alphabet[(val >> 12) & 0x3f];
out[0] = alphabet[(val >> 18) & 0x3f];
} else {
out[0] = '\0';
}
return CborNoError;
}
static CborError dump_bytestring_base64(char **result, CborValue *it)
{
static const char alphabet[] = "ABCDEFGH" "IJKLMNOP" "QRSTUVWX" "YZabcdef"
"ghijklmn" "opqrstuv" "wxyz0123" "456789+/" "=";
return generic_dump_base64(result, it, alphabet);
}
static CborError dump_bytestring_base64url(char **result, CborValue *it)
{
static const char alphabet[] = "ABCDEFGH" "IJKLMNOP" "QRSTUVWX" "YZabcdef"
"ghijklmn" "opqrstuv" "wxyz0123" "456789-_";
return generic_dump_base64(result, it, alphabet);
}
static CborError escape_text_string(char **str, size_t *alloc, size_t *offsetp, const char *input, size_t len)
{
/* JSON requires escaping some characters in strings, so we iterate and
* escape as necessary
* https://www.rfc-editor.org/rfc/rfc8259#section-7:
* All Unicode characters may be placed within the
* quotation marks, except for the characters that MUST be escaped:
* quotation mark, reverse solidus, and the control characters (U+0000
* through U+001F).
* We additionally choose to escape BS, HT, CR, LF and FF.
*/
char *buf = *str;
/* Ensure we have enough space for this chunk. In the worst case, we
* have 6 escaped characters per input character.
*
* The overflow checking here is only practically useful for 32-bit
* machines, as SIZE_MAX/6 for a 64-bit machine is 2.6667 exabytes.
* That is much more than any current architecture can even address and
* cbor_value_get_text_string_chunk() only works for data already
* loaded into memory.
*/
size_t needed;
size_t offset = offsetp ? *offsetp : 0;
if (mul_check_overflow(len, 6, &needed) || add_check_overflow(needed, offset, &needed)
|| add_check_overflow(needed, 1, &needed)) {
return CborErrorDataTooLarge;
}
if (!alloc || needed > *alloc) {
buf = cbor_realloc(buf, needed);
if (!buf)
return CborErrorOutOfMemory;
if (alloc)
*alloc = needed;
}
for (size_t i = 0; i < len; ++i) {
static const char escapeChars[] = "\b\t\n\r\f\"\\";
static const char escapedChars[] = "btnrf\"\\";
unsigned char c = input[i];
char *esc = c > 0 ? strchr(escapeChars, c) : NULL;
if (esc) {
buf[offset++] = '\\';
buf[offset++] = escapedChars[esc - escapeChars];
} else if (c <= 0x1F) {
buf[offset++] = '\\';
buf[offset++] = 'u';
buf[offset++] = '0';
buf[offset++] = '0';
append_hex(buf + offset, c);
offset += 2;
} else {
buf[offset++] = c;
}
}
buf[offset] = '\0';
*str = buf;
if (offsetp)
*offsetp = offset;
return CborNoError;
}
static CborError text_string_to_escaped(char **str, CborValue *it)
{
size_t alloc = 0, offset = 0;
CborError err;
*str = NULL;
err = cbor_value_begin_string_iteration(it);
while (err == CborNoError) {
const char *chunk;
size_t len;
err = cbor_value_get_text_string_chunk(it, &chunk, &len, it);
if (err == CborNoError)
err = escape_text_string(str, &alloc, &offset, chunk, len);
}
if (likely(err == CborErrorNoMoreStringChunks)) {
/* success */
if (!*str)
*str = strdup(""); // wasteful, but very atypical
err = cbor_value_finish_string_iteration(it);
if (likely(err == CborNoError))
return CborNoError;
}
cbor_free(*str);
*str = NULL;
return err;
}
static CborError add_value_metadata(FILE *out, CborType type, const ConversionStatus *status)
{
int flags = status->flags;
if (flags & TypeWasTagged) {
/* extract the tagged type, which may be JSON native */
type = flags & FinalTypeMask;
flags &= ~(FinalTypeMask | TypeWasTagged);
if (fprintf(out, "\"tag\":\"%" PRIu64 "\"%s", status->lastTag,
flags & ~TypeWasTagged ? "," : "") < 0)
return CborErrorIO;
}
if (!flags)
return CborNoError;
/* print at least the type */
if (fprintf(out, "\"t\":%d", type) < 0)
return CborErrorIO;
if (flags & NumberWasNaN)
if (fprintf(out, ",\"v\":\"nan\"") < 0)
return CborErrorIO;
if (flags & NumberWasInfinite)
if (fprintf(out, ",\"v\":\"%sinf\"", flags & NumberWasNegative ? "-" : "") < 0)
return CborErrorIO;
if (flags & NumberPrecisionWasLost)
if (fprintf(out, ",\"v\":\"%c%" PRIx64 "\"", flags & NumberWasNegative ? '-' : '+',
status->originalNumber) < 0)
return CborErrorIO;
if (type == CborSimpleType)
if (fprintf(out, ",\"v\":%d", (int)status->originalNumber) < 0)
return CborErrorIO;
return CborNoError;
}
static CborError find_tagged_type(CborValue *it, CborTag *tag, CborType *type, int nestingLevel)
{
CborError err = CborNoError;
*type = cbor_value_get_type(it);
while (*type == CborTagType) {
if (nestingLevel-- == 0)
return CborErrorNestingTooDeep;
cbor_value_get_tag(it, tag); /* can't fail */
err = cbor_value_advance_fixed(it);
if (err)
return err;
*type = cbor_value_get_type(it);
}
return err;
}
static CborError tagged_value_to_json(FILE *out, CborValue *it, int flags, int nestingLevel, ConversionStatus *status)
{
CborTag tag;
CborError err;
if (flags & CborConvertTagsToObjects) {
cbor_value_get_tag(it, &tag); /* can't fail */
err = cbor_value_advance_fixed(it);
if (err)
return err;
if (fprintf(out, "{\"tag%" PRIu64 "\":", tag) < 0)
return CborErrorIO;
CborType type = cbor_value_get_type(it);
err = value_to_json(out, it, flags, type, nestingLevel, status);
if (err)
return err;
if (flags & CborConvertAddMetadata && status->flags) {
if (fprintf(out, ",\"tag%" PRIu64 "$cbor\":{", tag) < 0 ||
add_value_metadata(out, type, status) != CborNoError ||
fputc('}', out) < 0)
return CborErrorIO;
}
if (fputc('}', out) < 0)
return CborErrorIO;
status->flags = TypeWasNotNative | CborTagType;
return CborNoError;
}
CborType type;
err = find_tagged_type(it, &status->lastTag, &type, nestingLevel);
if (err)
return err;
tag = status->lastTag;
/* special handling of byte strings? */
if (type == CborByteStringType && (flags & CborConvertByteStringsToBase64Url) == 0 &&
(tag == CborNegativeBignumTag || tag == CborExpectedBase16Tag || tag == CborExpectedBase64Tag)) {
char *str;
const char *pre = "";
if (tag == CborNegativeBignumTag) {
pre = "~";
err = dump_bytestring_base64url(&str, it);
} else if (tag == CborExpectedBase64Tag) {
err = dump_bytestring_base64(&str, it);
} else { /* tag == CborExpectedBase16Tag */
err = dump_bytestring_base16(&str, it);
}
if (err)
return err;
err = fprintf(out, "\"%s%s\"", pre, str) < 0 ? CborErrorIO : CborNoError;
cbor_free(str);
status->flags = TypeWasNotNative | TypeWasTagged | CborByteStringType;
return err;
}
/* no special handling */
err = value_to_json(out, it, flags, type, nestingLevel, status);
status->flags |= TypeWasTagged | type;
return err;
}
static CborError stringify_map_key(char **key, CborValue *it, int flags, CborType type)
{
(void)flags; /* unused */
(void)type; /* unused */
#ifdef WITHOUT_OPEN_MEMSTREAM
(void)key; /* unused */
(void)it; /* unused */
return CborErrorJsonNotImplemented;
#else
size_t size;
char *stringified;
FILE *memstream = open_memstream(&stringified, &size);
if (memstream == NULL)
return CborErrorOutOfMemory; /* could also be EMFILE, but it's unlikely */
CborError err = cbor_value_to_pretty_advance(memstream, it);
if (unlikely(fclose(memstream) < 0 || stringified == NULL))
return CborErrorInternalError;
if (err == CborNoError) {
/* escape the stringified CBOR stream */
err = escape_text_string(key, NULL, NULL, stringified, size);
}
cbor_free(stringified);
return err;
#endif
}
static CborError array_to_json(FILE *out, CborValue *it, int flags, int nestingLevel, ConversionStatus *status)
{
const char *comma = "";
while (!cbor_value_at_end(it)) {
if (fprintf(out, "%s", comma) < 0)
return CborErrorIO;
comma = ",";
CborError err = value_to_json(out, it, flags, cbor_value_get_type(it), nestingLevel, status);
if (err)
return err;
}
return CborNoError;
}
static CborError map_to_json(FILE *out, CborValue *it, int flags, int nestingLevel, ConversionStatus *status)
{
const char *comma = "";
CborError err;
while (!cbor_value_at_end(it)) {
char *key = NULL;
if (fprintf(out, "%s", comma) < 0)
return CborErrorIO;
comma = ",";
CborType keyType = cbor_value_get_type(it);
if (likely(keyType == CborTextStringType)) {
err = text_string_to_escaped(&key, it);
} else if (flags & CborConvertStringifyMapKeys) {
err = stringify_map_key(&key, it, flags, keyType);
} else {
return CborErrorJsonObjectKeyNotString;
}
if (err)
return err;
/* first, print the key */
if (fprintf(out, "\"%s\":", key) < 0) {
cbor_free(key);
return CborErrorIO;
}
/* then, print the value */
CborType valueType = cbor_value_get_type(it);
err = value_to_json(out, it, flags, valueType, nestingLevel, status);
/* finally, print any metadata we may have */
if (flags & CborConvertAddMetadata) {
if (!err && keyType != CborTextStringType) {
if (fprintf(out, ",\"%s$keycbordump\":true", key) < 0)
err = CborErrorIO;
}
if (!err && status->flags) {
if (fprintf(out, ",\"%s$cbor\":{", key) < 0 ||
add_value_metadata(out, valueType, status) != CborNoError ||
fputc('}', out) < 0)
err = CborErrorIO;
}
}
cbor_free(key);
if (err)
return err;
}
return CborNoError;
}
static CborError value_to_json(FILE *out, CborValue *it, int flags, CborType type,
int nestingLevel, ConversionStatus *status)
{
CborError err;
status->flags = 0;
if (nestingLevel == 0)
return CborErrorNestingTooDeep;
switch (type) {
case CborArrayType:
case CborMapType: {
/* recursive type */
CborValue recursed;
err = cbor_value_enter_container(it, &recursed);
if (err) {
copy_current_position(it, &recursed);
return err; /* parse error */
}
if (fputc(type == CborArrayType ? '[' : '{', out) < 0)
return CborErrorIO;
err = (type == CborArrayType) ?
array_to_json(out, &recursed, flags, nestingLevel - 1, status) :
map_to_json(out, &recursed, flags, nestingLevel - 1, status);
if (err) {
copy_current_position(it, &recursed);
return err; /* parse error */
}
if (fputc(type == CborArrayType ? ']' : '}', out) < 0)
return CborErrorIO;
err = cbor_value_leave_container(it, &recursed);
if (err)
return err; /* parse error */
status->flags = 0; /* reset, there are never conversion errors for us */
return CborNoError;
}
case CborIntegerType: {
double num; /* JS numbers are IEEE double precision */
uint64_t val;
cbor_value_get_raw_integer(it, &val); /* can't fail */
num = (double)val;
if (cbor_value_is_negative_integer(it)) {
num = -num - 1; /* convert to negative */
if ((uint64_t)(-num - 1) != val) {
status->flags = NumberPrecisionWasLost | NumberWasNegative;
status->originalNumber = val;
}
} else {
if ((uint64_t)num != val) {
status->flags = NumberPrecisionWasLost;
status->originalNumber = val;
}
}
if (fprintf(out, "%.0f", num) < 0) /* this number has no fraction, so no decimal points please */
return CborErrorIO;
break;
}
case CborByteStringType:
case CborTextStringType: {
char *str;
if (type == CborByteStringType) {
err = dump_bytestring_base64url(&str, it);
status->flags = TypeWasNotNative;
} else {
err = text_string_to_escaped(&str, it);
}
if (err)
return err;
err = (fprintf(out, "\"%s\"", str) < 0) ? CborErrorIO : CborNoError;
cbor_free(str);
return err;
}
case CborTagType:
return tagged_value_to_json(out, it, flags, nestingLevel - 1, status);
case CborSimpleType: {
uint8_t simple_type;
cbor_value_get_simple_type(it, &simple_type); /* can't fail */
status->flags = TypeWasNotNative;
status->originalNumber = simple_type;
if (fprintf(out, "\"simple(%" PRIu8 ")\"", simple_type) < 0)
return CborErrorIO;
break;
}
case CborNullType:
if (fprintf(out, "null") < 0)
return CborErrorIO;
break;
case CborUndefinedType:
status->flags = TypeWasNotNative;
if (fprintf(out, "\"undefined\"") < 0)
return CborErrorIO;
break;
case CborBooleanType: {
bool val;
cbor_value_get_boolean(it, &val); /* can't fail */
if (fprintf(out, val ? "true" : "false") < 0)
return CborErrorIO;
break;
}
#ifndef CBOR_NO_FLOATING_POINT
case CborDoubleType: {
double val;
if (false) {
float f;
case CborFloatType:
status->flags = TypeWasNotNative;
cbor_value_get_float(it, &f);
val = f;
} else if (false) {
uint16_t f16;
case CborHalfFloatType:
# ifndef CBOR_NO_HALF_FLOAT_TYPE
status->flags = TypeWasNotNative;
cbor_value_get_half_float(it, &f16);
val = decode_half(f16);
# else
(void)f16;
err = CborErrorUnsupportedType;
break;
# endif
} else {
cbor_value_get_double(it, &val);
}
int r = fpclassify(val);
if (r == FP_NAN || r == FP_INFINITE) {
if (fprintf(out, "null") < 0)
return CborErrorIO;
status->flags |= r == FP_NAN ? NumberWasNaN :
NumberWasInfinite | (val < 0 ? NumberWasNegative : 0);
} else {
const double limit = (UINT32_MAX + 1.0) * (UINT32_MAX + 1.0); /* 2^64 */
uint64_t ival = 0;
double aval = fabs(val);
if (aval < limit && (double)(ival = (uint64_t)aval) == aval) {
/* print as integer so we get the full precision */
r = fprintf(out, "%s%" PRIu64, val < 0 ? "-" : "", ival);
status->flags |= TypeWasNotNative; /* mark this integer number as a double */
} else {
/* this number is definitely not a 64-bit integer */
r = fprintf(out, "%." DBL_DECIMAL_DIG_STR "g", val);
}
if (r < 0)
return CborErrorIO;
}
break;
}
#else
case CborDoubleType:
case CborFloatType:
case CborHalfFloatType:
err = CborErrorUnsupportedType;
break;
#endif /* !CBOR_NO_FLOATING_POINT */
case CborInvalidType:
return CborErrorUnknownType;
}
return cbor_value_advance_fixed(it);
}
/**
* \enum CborToJsonFlags
* The CborToJsonFlags enum contains flags that control the conversion of CBOR to JSON.
*
* \value CborConvertAddMetadata Adds metadata to facilitate restoration of the original CBOR data.
* \value CborConvertTagsToObjects Converts CBOR tags to JSON objects
* \value CborConvertIgnoreTags (default) Ignore CBOR tags, except for byte strings
* \value CborConvertObeyByteStringTags (default) Honor formatting of CBOR byte strings if so tagged
* \value CborConvertByteStringsToBase64Url Force the conversion of all CBOR byte strings to Base64url encoding, despite any tags
* \value CborConvertRequireMapStringKeys (default) Require CBOR map keys to be strings, failing the conversion if they are not
* \value CborConvertStringifyMapKeys Convert non-string keys in CBOR maps to a string form
* \value CborConvertDefaultFlags Default conversion flags.
*/
/**
* \fn CborError cbor_value_to_json(FILE *out, const CborValue *value, int flags)
*
* Converts the current CBOR type pointed to by \a value to JSON and writes that
* to the \a out stream. If an error occurs, this function returns an error
* code similar to CborParsing. The \a flags parameter indicates one or more of
* the flags from CborToJsonFlags that control the conversion.
*
* \sa cbor_value_to_json_advance(), cbor_value_to_pretty()
*/
/**
* Converts the current CBOR type pointed to by \a value to JSON and writes that
* to the \a out stream. If an error occurs, this function returns an error
* code similar to CborParsing. The \a flags parameter indicates one or more of
* the flags from CborToJsonFlags that control the conversion.
*
* If no error ocurred, this function advances \a value to the next element.
*
* \sa cbor_value_to_json(), cbor_value_to_pretty_advance()
*/
CborError cbor_value_to_json_advance(FILE *out, CborValue *value, int flags)
{
ConversionStatus status;
return value_to_json(out, value, flags, cbor_value_get_type(value), CBOR_PARSER_MAX_RECURSIONS,
&status);
}
/** @} */