3131#include <string.h>
3232#include <unistd.h>
3333
34- enum Mode {
35- PrintCborDump = 0 ,
36- PrintJson = 0x80000000
37- };
38-
3934void * xrealloc (void * old , size_t size , const char * fname )
4035{
4136 old = realloc (old , size );
@@ -52,7 +47,7 @@ void printerror(CborError err, const char *fname)
5247 exit (EXIT_FAILURE );
5348}
5449
55- void dumpFile (FILE * in , const char * fname , int mode )
50+ void dumpFile (FILE * in , const char * fname , bool printJosn , int flags )
5651{
5752 static const size_t chunklen = 16 * 1024 ;
5853 static size_t bufsize = 0 ;
@@ -77,10 +72,10 @@ void dumpFile(FILE *in, const char *fname, int mode)
7772 CborValue value ;
7873 CborError err = cbor_parser_init (buffer , buflen , 0 , & parser , & value );
7974 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 );
8277 else
83- err = cbor_value_to_pretty_advance (stdout , & value );
78+ err = cbor_value_to_pretty_advance_flags (stdout , & value , flags );
8479 if (!err )
8580 puts ("" );
8681 }
@@ -92,29 +87,37 @@ void dumpFile(FILE *in, const char *fname, int mode)
9287
9388int main (int argc , char * * argv )
9489{
95- int mode = PrintCborDump ;
90+ bool printJson = false;
91+ int json_flags = CborConvertDefaultFlags ;
92+ int cbor_flags = CborPrettyDefaultFlags ;
9693 int c ;
97- while ((c = getopt (argc , argv , "MOSUcjh " )) != -1 ) {
94+ while ((c = getopt (argc , argv , "MOSUcjhfn " )) != -1 ) {
9895 switch (c ) {
9996 case 'c' :
100- mode = PrintCborDump ;
97+ printJson = false ;
10198 break ;
10299 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 ;
105108 break ;
106109
107110 case 'M' :
108- mode |= CborConvertAddMetadata ;
111+ json_flags |= CborConvertAddMetadata ;
109112 break ;
110113 case 'O' :
111- mode |= CborConvertTagsToObjects ;
114+ json_flags |= CborConvertTagsToObjects ;
112115 break ;
113116 case 'S' :
114- mode |= CborConvertStringifyMapKeys ;
117+ json_flags |= CborConvertStringifyMapKeys ;
115118 break ;
116119 case 'U' :
117- mode |= CborConvertByteStringsToBase64Url ;
120+ json_flags |= CborConvertByteStringsToBase64Url ;
118121 break ;
119122
120123 case '?' :
@@ -132,15 +135,18 @@ int main(int argc, char **argv)
132135 " -M Add metadata so converting back to CBOR is possible\n"
133136 " -O Convert CBOR tags to JSON objects\n"
134137 " -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"
136142 "" );
137143 return c == '?' ? EXIT_FAILURE : EXIT_SUCCESS ;
138144 }
139145 }
140146
141147 char * * fname = argv + optind ;
142148 if (!* fname ) {
143- dumpFile (stdin , "-" , mode );
149+ dumpFile (stdin , "-" , printJson , printJson ? json_flags : cbor_flags );
144150 } else {
145151 for ( ; * fname ; ++ fname ) {
146152 FILE * in = fopen (* fname , "rb" );
@@ -149,7 +155,7 @@ int main(int argc, char **argv)
149155 return EXIT_FAILURE ;
150156 }
151157
152- dumpFile (in , * fname , mode );
158+ dumpFile (in , * fname , printJson , printJson ? json_flags : cbor_flags );
153159 fclose (in );
154160 }
155161 }
0 commit comments