blob: 1f3d65b6a910d630c6f3dea66417e2692eb309f6 [file] [log] [blame]
Nigel Tao193527b2020-02-26 21:55:01 +11001// Copyright 2020 The Wuffs Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15// ----------------
16
17// print-json-token-debug-format.c parses JSON from stdin and prints the
18// resulting token stream, eliding any non-essential (e.g. whitespace) tokens.
19//
Nigel Taod1c928a2020-02-28 12:43:53 +110020// The output format is only for debugging or regression testing, and certainly
21// not for long term storage. It isn't guaranteed to be stable between versions
22// of this program and of the Wuffs standard library.
Nigel Tao193527b2020-02-26 21:55:01 +110023//
Nigel Taod1c928a2020-02-28 12:43:53 +110024// It prints 16 bytes (128 bits) per token, containing big-endian numbers:
25//
26// +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
27// | POS | LEN | LP| LN| MAJOR | MINOR |
28// | | | | | |VBC| VBD |
29// +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
30//
31// - POS (4 bytes) is the position: the sum of all previous tokens' lengths,
32// including elided tokens.
33// - LEN (2 bytes) is the length.
34// - LP (1 bytes) is the link_prev bit.
35// - LN (1 bytes) is the link_next bit
36// - MAJOR (4 bytes) is the value_major.
37//
38// The final 4 bytes are either value_minor (when the value_major is non-zero)
39// or 1 + 3 bytes for value_base_category and value_base_detail (otherwise).
40//
Nigel Tao193527b2020-02-26 21:55:01 +110041// Together with the hexadecimal WUFFS_BASE__TOKEN__ETC constants defined in
42// token-public.h, this format is somewhat human-readable when piped through a
Nigel Taod1c928a2020-02-28 12:43:53 +110043// hex-dump program (such as /usr/bin/hd), printing one token per line.
Nigel Tao6b29f322020-02-29 17:05:24 +110044// Alternatively, pass the -h (--human-readable) flag to this program.
45//
46// Pass -a (--all-tokens) to print all tokens, including whitespace.
Nigel Tao193527b2020-02-26 21:55:01 +110047//
48// If the input or output is larger than the program's buffers (64 MiB and
49// 131072 tokens by default), there may be multiple valid tokenizations of any
50// given input. For example, if a source string "abcde" straddles an I/O
Nigel Taod1c928a2020-02-28 12:43:53 +110051// boundary, it may be tokenized as single (no-link) 5-length string or as a
52// 3-length link_next'ed string followed by a 2-length link_prev'ed string.
Nigel Tao193527b2020-02-26 21:55:01 +110053//
Nigel Taod1c928a2020-02-28 12:43:53 +110054// A Wuffs token stream, in general, can support inputs more than 0xFFFF_FFFF
Nigel Tao193527b2020-02-26 21:55:01 +110055// bytes long, but this program can not, as it tracks the tokens' cumulative
56// position as a uint32.
57
58#include <inttypes.h>
59#include <stdio.h>
60#include <string.h>
61#include <unistd.h>
62
63// Wuffs ships as a "single file C library" or "header file library" as per
64// https://github.com/nothings/stb/blob/master/docs/stb_howto.txt
65//
66// To use that single file as a "foo.c"-like implementation, instead of a
67// "foo.h"-like header, #define WUFFS_IMPLEMENTATION before #include'ing or
68// compiling it.
69#define WUFFS_IMPLEMENTATION
70
71// Defining the WUFFS_CONFIG__MODULE* macros are optional, but it lets users of
72// release/c/etc.c whitelist which parts of Wuffs to build. That file contains
73// the entire Wuffs standard library, implementing a variety of codecs and file
74// formats. Without this macro definition, an optimizing compiler or linker may
75// very well discard Wuffs code for unused codecs, but listing the Wuffs
76// modules we use makes that process explicit. Preprocessing means that such
77// code simply isn't compiled.
78#define WUFFS_CONFIG__MODULES
79#define WUFFS_CONFIG__MODULE__BASE
80#define WUFFS_CONFIG__MODULE__JSON
81
82// If building this program in an environment that doesn't easily accommodate
83// relative includes, you can use the script/inline-c-relative-includes.go
84// program to generate a stand-alone C++ file.
85#include "../release/c/wuffs-unsupported-snapshot.c"
86
87#ifndef SRC_BUFFER_SIZE
88#define SRC_BUFFER_SIZE (64 * 1024 * 1024)
89#endif
90#ifndef TOKEN_BUFFER_SIZE
91#define TOKEN_BUFFER_SIZE (128 * 1024)
92#endif
93
94uint8_t src_buffer[SRC_BUFFER_SIZE];
95wuffs_base__token tok_buffer[TOKEN_BUFFER_SIZE];
96
97wuffs_base__io_buffer src;
98wuffs_base__token_buffer tok;
99
100wuffs_json__decoder dec;
101wuffs_base__status dec_status;
102
Nigel Tao193527b2020-02-26 21:55:01 +1100103#define TRY(error_msg) \
104 do { \
105 const char* z = error_msg; \
106 if (z) { \
107 return z; \
108 } \
109 } while (false)
110
111const char* //
112read_src() {
113 if (src.meta.closed) {
114 return "main: internal error: read requested on a closed source";
115 }
116 wuffs_base__io_buffer__compact(&src);
117 if (src.meta.wi >= src.data.len) {
118 return "main: src buffer is full";
119 }
120 size_t n = fread(src.data.ptr + src.meta.wi, sizeof(uint8_t),
121 src.data.len - src.meta.wi, stdin);
122 src.meta.wi += n;
123 src.meta.closed = feof(stdin);
124 if ((n == 0) && !src.meta.closed) {
125 return "main: read error";
126 }
127 return NULL;
128}
129
130// ----
131
Nigel Tao67f00c12020-02-29 12:48:59 +1100132const char* vbc_names[8] = {
133 "0:Filler..........", //
Nigel Tao85fba7f2020-02-29 16:28:06 +1100134 "1:Structure.......", //
135 "2:String..........", //
136 "3:UnicodeCodePoint", //
137 "4:Literal.........", //
138 "5:Number..........", //
Nigel Tao67f00c12020-02-29 12:48:59 +1100139 "6:Reserved........", //
140 "7:Reserved........", //
141};
142
143const int base38_decode[38] = {
144 ' ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '?', //
145 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', //
146 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', //
147};
148
Nigel Tao193527b2020-02-26 21:55:01 +1100149const char* //
150main1(int argc, char** argv) {
Nigel Tao6b29f322020-02-29 17:05:24 +1100151 bool all_tokens = false;
Nigel Tao67f00c12020-02-29 12:48:59 +1100152 bool human_readable = false;
153 int i;
154 for (i = 1; i < argc; i++) {
Nigel Tao6b29f322020-02-29 17:05:24 +1100155 if ((strcmp(argv[i], "-a") == 0) ||
156 (strcmp(argv[i], "--all-tokens") == 0)) {
157 all_tokens = true;
158 continue;
159 }
Nigel Tao67f00c12020-02-29 12:48:59 +1100160 if ((strcmp(argv[i], "-h") == 0) ||
161 (strcmp(argv[i], "--human-readable") == 0)) {
162 human_readable = true;
Nigel Tao6b29f322020-02-29 17:05:24 +1100163 continue;
Nigel Tao67f00c12020-02-29 12:48:59 +1100164 }
165 }
166
Nigel Tao193527b2020-02-26 21:55:01 +1100167 src = wuffs_base__make_io_buffer(
168 wuffs_base__make_slice_u8(src_buffer, SRC_BUFFER_SIZE),
169 wuffs_base__empty_io_buffer_meta());
170
171 tok = wuffs_base__make_token_buffer(
172 wuffs_base__make_slice_token(tok_buffer, TOKEN_BUFFER_SIZE),
173 wuffs_base__empty_token_buffer_meta());
174
175 wuffs_base__status init_status = wuffs_json__decoder__initialize(
176 &dec, sizeof__wuffs_json__decoder(), WUFFS_VERSION, 0);
177 if (!wuffs_base__status__is_ok(&init_status)) {
178 return wuffs_base__status__message(&init_status);
179 }
180
181 uint64_t pos = 0;
182 while (true) {
183 wuffs_base__status status =
184 wuffs_json__decoder__decode_tokens(&dec, &tok, &src);
185
186 while (tok.meta.ri < tok.meta.wi) {
187 wuffs_base__token* t = &tok.data.ptr[tok.meta.ri++];
Nigel Taod1c928a2020-02-28 12:43:53 +1100188 uint16_t len = wuffs_base__token__length(t);
Nigel Tao193527b2020-02-26 21:55:01 +1100189
Nigel Tao6b29f322020-02-29 17:05:24 +1100190 if (all_tokens || (wuffs_base__token__value(t) != 0)) {
Nigel Taod1c928a2020-02-28 12:43:53 +1100191 uint8_t lp = wuffs_base__token__link_prev(t) ? 1 : 0;
192 uint8_t ln = wuffs_base__token__link_next(t) ? 1 : 0;
193 uint32_t vmajor = wuffs_base__token__value_major(t);
Nigel Tao67f00c12020-02-29 12:48:59 +1100194 uint32_t vminor = wuffs_base__token__value_minor(t);
195 uint8_t vbc = wuffs_base__token__value_base_category(t);
196 uint32_t vbd = wuffs_base__token__value_base_detail(t);
Nigel Tao193527b2020-02-26 21:55:01 +1100197
Nigel Tao67f00c12020-02-29 12:48:59 +1100198 if (human_readable) {
199 printf("pos=0x%08" PRIX32 " len=0x%04" PRIX16 " link=0b%d%d ",
200 (uint32_t)(pos), len, (int)(lp), (int)(ln));
201
202 if (vmajor) {
203 uint32_t m = vmajor;
204 uint32_t m0 = m / (38 * 38 * 38);
205 m -= m0 * (38 * 38 * 38);
206 uint32_t m1 = m / (38 * 38);
207 m -= m1 * (38 * 38);
208 uint32_t m2 = m / (38);
209 m -= m2 * (38);
210 uint32_t m3 = m;
211
212 printf("vmajor=0x%06" PRIX32 ":%c%c%c%c vminor=0x%06" PRIX32 "\n",
213 vmajor, base38_decode[m0], base38_decode[m1],
214 base38_decode[m2], base38_decode[m3], vminor);
215 } else {
216 printf("vbc=%s vbd=0x%06" PRIX32 "\n", vbc_names[vbc & 7], vbd);
217 }
218
Nigel Taod1c928a2020-02-28 12:43:53 +1100219 } else {
Nigel Tao67f00c12020-02-29 12:48:59 +1100220 uint8_t buf[16];
221 wuffs_base__store_u32be__no_bounds_check(&buf[0x0], (uint32_t)(pos));
222 wuffs_base__store_u16be__no_bounds_check(&buf[0x4], len);
223 wuffs_base__store_u8__no_bounds_check(&buf[0x0006], lp);
224 wuffs_base__store_u8__no_bounds_check(&buf[0x0007], ln);
225 wuffs_base__store_u32be__no_bounds_check(&buf[0x8], vmajor);
226 if (vmajor) {
227 wuffs_base__store_u32be__no_bounds_check(&buf[0xC], vminor);
228 } else {
229 wuffs_base__store_u8__no_bounds_check(&buf[0x000C], vbc);
230 wuffs_base__store_u24be__no_bounds_check(&buf[0xD], vbd);
231 }
232 const int stdout_fd = 1;
233 write(stdout_fd, &buf[0], 16);
Nigel Taod1c928a2020-02-28 12:43:53 +1100234 }
Nigel Tao193527b2020-02-26 21:55:01 +1100235 }
236
237 pos += len;
238 if (pos > 0xFFFFFFFF) {
239 return "main: input is too long";
240 }
241 }
242
243 if (status.repr == NULL) {
244 return NULL;
245 } else if (status.repr == wuffs_base__suspension__short_read) {
246 TRY(read_src());
247 } else if (status.repr == wuffs_base__suspension__short_write) {
248 wuffs_base__token_buffer__compact(&tok);
249 } else {
250 return wuffs_base__status__message(&status);
251 }
252 }
253}
254
255// ----
256
257int //
258compute_exit_code(const char* status_msg) {
259 if (!status_msg) {
260 return 0;
261 }
262 size_t n = strnlen(status_msg, 2047);
263 if (n >= 2047) {
264 status_msg = "main: internal error: error message is too long";
265 n = strnlen(status_msg, 2047);
266 }
267 fprintf(stderr, "%s\n", status_msg);
268 // Return an exit code of 1 for regular (forseen) errors, e.g. badly
269 // formatted or unsupported input.
270 //
271 // Return an exit code of 2 for internal (exceptional) errors, e.g. defensive
272 // run-time checks found that an internal invariant did not hold.
273 //
274 // Automated testing, including badly formatted inputs, can therefore
275 // discriminate between expected failure (exit code 1) and unexpected failure
276 // (other non-zero exit codes). Specifically, exit code 2 for internal
277 // invariant violation, exit code 139 (which is 128 + SIGSEGV on x86_64
278 // linux) for a segmentation fault (e.g. null pointer dereference).
279 return strstr(status_msg, "internal error:") ? 2 : 1;
280}
281
282int //
283main(int argc, char** argv) {
284 const char* z = main1(argc, argv);
285 int exit_code = compute_exit_code(z);
286 return exit_code;
287}