31
31
#include <string.h>
32
32
#include <unistd.h>
33
33
34
- enum Mode {
35
- PrintCborDump = 0 ,
36
- PrintJson = 0x80000000
37
- };
38
-
39
34
void * xrealloc (void * old , size_t size , const char * fname )
40
35
{
41
36
old = realloc (old , size );
@@ -52,7 +47,7 @@ void printerror(CborError err, const char *fname)
52
47
exit (EXIT_FAILURE );
53
48
}
54
49
55
- void dumpFile (FILE * in , const char * fname , int mode )
50
+ void dumpFile (FILE * in , const char * fname , bool printJosn , int flags )
56
51
{
57
52
static const size_t chunklen = 16 * 1024 ;
58
53
static size_t bufsize = 0 ;
@@ -77,10 +72,10 @@ void dumpFile(FILE *in, const char *fname, int mode)
77
72
CborValue value ;
78
73
CborError err = cbor_parser_init (buffer , buflen , 0 , & parser , & value );
79
74
if (!err ) {
80
- if (mode )
81
- err = cbor_value_to_json_advance (stdout , & value , mode & ~ PrintJson );
75
+ if (printJosn )
76
+ err = cbor_value_to_json_advance (stdout , & value , flags );
82
77
else
83
- err = cbor_value_to_pretty_advance (stdout , & value );
78
+ err = cbor_value_to_pretty_advance_flags (stdout , & value , flags );
84
79
if (!err )
85
80
puts ("" );
86
81
}
@@ -92,29 +87,37 @@ void dumpFile(FILE *in, const char *fname, int mode)
92
87
93
88
int main (int argc , char * * argv )
94
89
{
95
- int mode = PrintCborDump ;
90
+ bool printJson = false;
91
+ int json_flags = CborConvertDefaultFlags ;
92
+ int cbor_flags = CborPrettyDefaultFlags ;
96
93
int c ;
97
- while ((c = getopt (argc , argv , "MOSUcjh " )) != -1 ) {
94
+ while ((c = getopt (argc , argv , "MOSUcjhfn " )) != -1 ) {
98
95
switch (c ) {
99
96
case 'c' :
100
- mode = PrintCborDump ;
97
+ printJson = false ;
101
98
break ;
102
99
case 'j' :
103
- mode &= ~PrintCborDump ;
104
- mode |= PrintJson ;
100
+ printJson = true;
101
+ break ;
102
+
103
+ case 'f' :
104
+ cbor_flags |= CborPrettyShowStringFragments ;
105
+ break ;
106
+ case 'n' :
107
+ cbor_flags |= CborPrettyIndicateIndetermineLength | CborPrettyNumericEncodingIndicators ;
105
108
break ;
106
109
107
110
case 'M' :
108
- mode |= CborConvertAddMetadata ;
111
+ json_flags |= CborConvertAddMetadata ;
109
112
break ;
110
113
case 'O' :
111
- mode |= CborConvertTagsToObjects ;
114
+ json_flags |= CborConvertTagsToObjects ;
112
115
break ;
113
116
case 'S' :
114
- mode |= CborConvertStringifyMapKeys ;
117
+ json_flags |= CborConvertStringifyMapKeys ;
115
118
break ;
116
119
case 'U' :
117
- mode |= CborConvertByteStringsToBase64Url ;
120
+ json_flags |= CborConvertByteStringsToBase64Url ;
118
121
break ;
119
122
120
123
case '?' :
@@ -132,15 +135,18 @@ int main(int argc, char **argv)
132
135
" -M Add metadata so converting back to CBOR is possible\n"
133
136
" -O Convert CBOR tags to JSON objects\n"
134
137
" -S Stringify non-text string map keys\n"
135
- " -U Convert all CBOR byte strings to Base64url regardless of tags"
138
+ " -U Convert all CBOR byte strings to Base64url regardless of tags\n"
139
+ "When CBOR dump is active, the following options are recognized:\n"
140
+ " -f Show text and byte string fragments\n"
141
+ " -n Show overlong encoding of CBOR numbers and length"
136
142
"" );
137
143
return c == '?' ? EXIT_FAILURE : EXIT_SUCCESS ;
138
144
}
139
145
}
140
146
141
147
char * * fname = argv + optind ;
142
148
if (!* fname ) {
143
- dumpFile (stdin , "-" , mode );
149
+ dumpFile (stdin , "-" , printJson , printJson ? json_flags : cbor_flags );
144
150
} else {
145
151
for ( ; * fname ; ++ fname ) {
146
152
FILE * in = fopen (* fname , "rb" );
@@ -149,7 +155,7 @@ int main(int argc, char **argv)
149
155
return EXIT_FAILURE ;
150
156
}
151
157
152
- dumpFile (in , * fname , mode );
158
+ dumpFile (in , * fname , printJson , printJson ? json_flags : cbor_flags );
153
159
fclose (in );
154
160
}
155
161
}
0 commit comments