blob: 89877b981f5d201e1c9582ffe17e1039ac437282 [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// +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
Nigel Tao3385efa2020-04-02 12:10:10 +110027// | | | | | | VALUE_EXTENSION |
28// | POS | LEN | LP| LN|EXT|VALUE_MAJOR| VALUE_MINOR |
29// | | | | | | 0 |VBC| VBD |
Nigel Taod1c928a2020-02-28 12:43:53 +110030// +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
31//
Nigel Tao3385efa2020-04-02 12:10:10 +110032// - POS (4 bytes) is the position: the sum of all previous tokens' lengths,
33// including elided tokens.
34// - LEN (2 bytes) is the length.
35// - LP (1 bytes) is the link_prev bit.
36// - LN (1 bytes) is the link_next bit
37// - EXT (1 bytes) is 1 for extended and 0 for simple tokens.
Nigel Taod1c928a2020-02-28 12:43:53 +110038//
Nigel Tao3385efa2020-04-02 12:10:10 +110039// Extended tokens have a VALUE_EXTENSION (7 bytes).
40//
41// Simple tokens have a VALUE_MAJOR (3 bytes) and then either 4 bytes
42// VALUE_MINOR (when VALUE_MAJOR is non-zero) or (1 + 3) bytes
43// VALUE_BASE_CATEGORY and VALUE_BASE_DETAIL (when VALUE_MAJOR is zero).
44//
45// ----
Nigel Taod1c928a2020-02-28 12:43:53 +110046//
Nigel Tao193527b2020-02-26 21:55:01 +110047// Together with the hexadecimal WUFFS_BASE__TOKEN__ETC constants defined in
48// token-public.h, this format is somewhat human-readable when piped through a
Nigel Taod1c928a2020-02-28 12:43:53 +110049// hex-dump program (such as /usr/bin/hd), printing one token per line.
Nigel Tao6b29f322020-02-29 17:05:24 +110050// Alternatively, pass the -h (--human-readable) flag to this program.
51//
52// Pass -a (--all-tokens) to print all tokens, including whitespace.
Nigel Tao193527b2020-02-26 21:55:01 +110053//
54// If the input or output is larger than the program's buffers (64 MiB and
55// 131072 tokens by default), there may be multiple valid tokenizations of any
56// given input. For example, if a source string "abcde" straddles an I/O
Nigel Taod1c928a2020-02-28 12:43:53 +110057// boundary, it may be tokenized as single (no-link) 5-length string or as a
58// 3-length link_next'ed string followed by a 2-length link_prev'ed string.
Nigel Tao193527b2020-02-26 21:55:01 +110059//
Nigel Taod1c928a2020-02-28 12:43:53 +110060// A Wuffs token stream, in general, can support inputs more than 0xFFFF_FFFF
Nigel Tao193527b2020-02-26 21:55:01 +110061// bytes long, but this program can not, as it tracks the tokens' cumulative
62// position as a uint32.
63
64#include <inttypes.h>
65#include <stdio.h>
66#include <string.h>
67#include <unistd.h>
68
69// Wuffs ships as a "single file C library" or "header file library" as per
70// https://github.com/nothings/stb/blob/master/docs/stb_howto.txt
71//
72// To use that single file as a "foo.c"-like implementation, instead of a
73// "foo.h"-like header, #define WUFFS_IMPLEMENTATION before #include'ing or
74// compiling it.
75#define WUFFS_IMPLEMENTATION
76
77// Defining the WUFFS_CONFIG__MODULE* macros are optional, but it lets users of
78// release/c/etc.c whitelist which parts of Wuffs to build. That file contains
79// the entire Wuffs standard library, implementing a variety of codecs and file
80// formats. Without this macro definition, an optimizing compiler or linker may
81// very well discard Wuffs code for unused codecs, but listing the Wuffs
82// modules we use makes that process explicit. Preprocessing means that such
83// code simply isn't compiled.
84#define WUFFS_CONFIG__MODULES
85#define WUFFS_CONFIG__MODULE__BASE
86#define WUFFS_CONFIG__MODULE__JSON
87
88// If building this program in an environment that doesn't easily accommodate
89// relative includes, you can use the script/inline-c-relative-includes.go
90// program to generate a stand-alone C++ file.
91#include "../release/c/wuffs-unsupported-snapshot.c"
92
Nigel Taof3146c22020-03-26 08:47:42 +110093// Wuffs allows either statically or dynamically allocated work buffers. This
94// program exercises static allocation.
95#define WORK_BUFFER_ARRAY_SIZE \
96 WUFFS_JSON__DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE
97#if WORK_BUFFER_ARRAY_SIZE > 0
Nigel Taoc0bd9df2020-03-26 21:55:44 +110098uint8_t g_work_buffer_array[WORK_BUFFER_ARRAY_SIZE];
Nigel Taof3146c22020-03-26 08:47:42 +110099#else
100// Not all C/C++ compilers support 0-length arrays.
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100101uint8_t g_work_buffer_array[1];
Nigel Taof3146c22020-03-26 08:47:42 +1100102#endif
103
Nigel Taofdac24a2020-03-06 21:53:08 +1100104#ifndef SRC_BUFFER_ARRAY_SIZE
105#define SRC_BUFFER_ARRAY_SIZE (64 * 1024 * 1024)
Nigel Tao193527b2020-02-26 21:55:01 +1100106#endif
Nigel Taofdac24a2020-03-06 21:53:08 +1100107#ifndef TOKEN_BUFFER_ARRAY_SIZE
108#define TOKEN_BUFFER_ARRAY_SIZE (128 * 1024)
Nigel Tao193527b2020-02-26 21:55:01 +1100109#endif
110
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100111uint8_t g_src_buffer_array[SRC_BUFFER_ARRAY_SIZE];
112wuffs_base__token g_tok_buffer_array[TOKEN_BUFFER_ARRAY_SIZE];
Nigel Tao193527b2020-02-26 21:55:01 +1100113
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100114wuffs_base__io_buffer g_src;
115wuffs_base__token_buffer g_tok;
Nigel Tao193527b2020-02-26 21:55:01 +1100116
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100117wuffs_json__decoder g_dec;
118wuffs_base__status g_dec_status;
Nigel Tao193527b2020-02-26 21:55:01 +1100119
Nigel Tao193527b2020-02-26 21:55:01 +1100120#define TRY(error_msg) \
121 do { \
122 const char* z = error_msg; \
123 if (z) { \
124 return z; \
125 } \
126 } while (false)
127
Nigel Tao68920952020-03-03 11:25:18 +1100128// ignore_return_value suppresses errors from -Wall -Werror.
129static void //
130ignore_return_value(int ignored) {}
131
Nigel Tao193527b2020-02-26 21:55:01 +1100132const char* //
133read_src() {
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100134 if (g_src.meta.closed) {
Nigel Tao193527b2020-02-26 21:55:01 +1100135 return "main: internal error: read requested on a closed source";
136 }
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100137 wuffs_base__io_buffer__compact(&g_src);
138 if (g_src.meta.wi >= g_src.data.len) {
139 return "main: g_src buffer is full";
Nigel Tao193527b2020-02-26 21:55:01 +1100140 }
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100141 size_t n = fread(g_src.data.ptr + g_src.meta.wi, sizeof(uint8_t),
142 g_src.data.len - g_src.meta.wi, stdin);
143 g_src.meta.wi += n;
144 g_src.meta.closed = feof(stdin);
145 if ((n == 0) && !g_src.meta.closed) {
Nigel Tao193527b2020-02-26 21:55:01 +1100146 return "main: read error";
147 }
148 return NULL;
149}
150
151// ----
152
Nigel Tao68920952020-03-03 11:25:18 +1100153struct {
154 int remaining_argc;
155 char** remaining_argv;
156
157 bool all_tokens;
158 bool human_readable;
Nigel Tao5d5a17c2020-03-24 16:38:43 +1100159 bool quirks;
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100160} g_flags = {0};
Nigel Tao68920952020-03-03 11:25:18 +1100161
162const char* //
163parse_flags(int argc, char** argv) {
164 int c = (argc > 0) ? 1 : 0; // Skip argv[0], the program name.
165 for (; c < argc; c++) {
166 char* arg = argv[c];
167 if (*arg++ != '-') {
168 break;
169 }
170
171 // A double-dash "--foo" is equivalent to a single-dash "-foo". As special
172 // cases, a bare "-" is not a flag (some programs may interpret it as
173 // stdin) and a bare "--" means to stop parsing flags.
174 if (*arg == '\x00') {
175 break;
176 } else if (*arg == '-') {
177 arg++;
178 if (*arg == '\x00') {
179 c++;
180 break;
181 }
182 }
183
184 if (!strcmp(arg, "a") || !strcmp(arg, "all-tokens")) {
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100185 g_flags.all_tokens = true;
Nigel Tao68920952020-03-03 11:25:18 +1100186 continue;
187 }
188 if (!strcmp(arg, "h") || !strcmp(arg, "human-readable")) {
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100189 g_flags.human_readable = true;
Nigel Tao68920952020-03-03 11:25:18 +1100190 continue;
191 }
Nigel Tao5d5a17c2020-03-24 16:38:43 +1100192 if (!strcmp(arg, "q") || !strcmp(arg, "quirks")) {
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100193 g_flags.quirks = true;
Nigel Tao5d5a17c2020-03-24 16:38:43 +1100194 continue;
195 }
Nigel Tao68920952020-03-03 11:25:18 +1100196
197 return "main: unrecognized flag argument";
198 }
199
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100200 g_flags.remaining_argc = argc - c;
201 g_flags.remaining_argv = argv + c;
Nigel Tao68920952020-03-03 11:25:18 +1100202 return NULL;
203}
204
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100205const char* g_vbc_names[8] = {
Nigel Tao67f00c12020-02-29 12:48:59 +1100206 "0:Filler..........", //
Nigel Tao85fba7f2020-02-29 16:28:06 +1100207 "1:Structure.......", //
208 "2:String..........", //
209 "3:UnicodeCodePoint", //
210 "4:Literal.........", //
211 "5:Number..........", //
Nigel Tao67f00c12020-02-29 12:48:59 +1100212 "6:Reserved........", //
213 "7:Reserved........", //
214};
215
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100216const int g_base38_decode[38] = {
Nigel Tao67f00c12020-02-29 12:48:59 +1100217 ' ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '?', //
218 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', //
219 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', //
220};
221
Nigel Tao193527b2020-02-26 21:55:01 +1100222const char* //
223main1(int argc, char** argv) {
Nigel Tao68920952020-03-03 11:25:18 +1100224 TRY(parse_flags(argc, argv));
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100225 if (g_flags.remaining_argc > 0) {
Nigel Tao68920952020-03-03 11:25:18 +1100226 return "main: bad argument: use \"program < input\", not \"program input\"";
Nigel Tao67f00c12020-02-29 12:48:59 +1100227 }
228
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100229 g_src = wuffs_base__make_io_buffer(
230 wuffs_base__make_slice_u8(g_src_buffer_array, SRC_BUFFER_ARRAY_SIZE),
Nigel Tao193527b2020-02-26 21:55:01 +1100231 wuffs_base__empty_io_buffer_meta());
232
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100233 g_tok = wuffs_base__make_token_buffer(
234 wuffs_base__make_slice_token(g_tok_buffer_array, TOKEN_BUFFER_ARRAY_SIZE),
Nigel Tao193527b2020-02-26 21:55:01 +1100235 wuffs_base__empty_token_buffer_meta());
236
237 wuffs_base__status init_status = wuffs_json__decoder__initialize(
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100238 &g_dec, sizeof__wuffs_json__decoder(), WUFFS_VERSION, 0);
Nigel Tao193527b2020-02-26 21:55:01 +1100239 if (!wuffs_base__status__is_ok(&init_status)) {
240 return wuffs_base__status__message(&init_status);
241 }
242
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100243 if (g_flags.quirks) {
Nigel Tao5d5a17c2020-03-24 16:38:43 +1100244 uint32_t quirks[] = {
245 WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_A,
246 WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_CAPITAL_U,
247 WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_E,
248 WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_QUESTION_MARK,
249 WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_SINGLE_QUOTE,
250 WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_V,
251 WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_X,
252 WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_ZERO,
253 WUFFS_JSON__QUIRK_ALLOW_COMMENT_BLOCK,
254 WUFFS_JSON__QUIRK_ALLOW_COMMENT_LINE,
255 WUFFS_JSON__QUIRK_ALLOW_EXTRA_COMMA,
256 WUFFS_JSON__QUIRK_ALLOW_INF_NAN_NUMBERS,
257 WUFFS_JSON__QUIRK_ALLOW_LEADING_ASCII_RECORD_SEPARATOR,
258 WUFFS_JSON__QUIRK_ALLOW_LEADING_UNICODE_BYTE_ORDER_MARK,
259 WUFFS_JSON__QUIRK_ALLOW_TRAILING_NEW_LINE,
Nigel Tao3bfed6d2020-03-24 17:44:47 +1100260 WUFFS_JSON__QUIRK_REPLACE_INVALID_UNICODE,
Nigel Tao5d5a17c2020-03-24 16:38:43 +1100261 0,
262 };
263 uint32_t i;
264 for (i = 0; quirks[i]; i++) {
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100265 wuffs_json__decoder__set_quirk_enabled(&g_dec, quirks[i], true);
Nigel Tao5d5a17c2020-03-24 16:38:43 +1100266 }
267 }
268
Nigel Tao193527b2020-02-26 21:55:01 +1100269 uint64_t pos = 0;
270 while (true) {
Nigel Taof3146c22020-03-26 08:47:42 +1100271 wuffs_base__status status = wuffs_json__decoder__decode_tokens(
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100272 &g_dec, &g_tok, &g_src,
273 wuffs_base__make_slice_u8(g_work_buffer_array, WORK_BUFFER_ARRAY_SIZE));
Nigel Tao193527b2020-02-26 21:55:01 +1100274
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100275 while (g_tok.meta.ri < g_tok.meta.wi) {
276 wuffs_base__token* t = &g_tok.data.ptr[g_tok.meta.ri++];
Nigel Taod1c928a2020-02-28 12:43:53 +1100277 uint16_t len = wuffs_base__token__length(t);
Nigel Tao193527b2020-02-26 21:55:01 +1100278
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100279 if (g_flags.all_tokens || (wuffs_base__token__value(t) != 0)) {
Nigel Taod1c928a2020-02-28 12:43:53 +1100280 uint8_t lp = wuffs_base__token__link_prev(t) ? 1 : 0;
281 uint8_t ln = wuffs_base__token__link_next(t) ? 1 : 0;
Nigel Tao3385efa2020-04-02 12:10:10 +1100282 int32_t vmajor = wuffs_base__token__value_major(t);
Nigel Tao67f00c12020-02-29 12:48:59 +1100283 uint32_t vminor = wuffs_base__token__value_minor(t);
284 uint8_t vbc = wuffs_base__token__value_base_category(t);
285 uint32_t vbd = wuffs_base__token__value_base_detail(t);
Nigel Tao193527b2020-02-26 21:55:01 +1100286
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100287 if (g_flags.human_readable) {
Nigel Tao67f00c12020-02-29 12:48:59 +1100288 printf("pos=0x%08" PRIX32 " len=0x%04" PRIX16 " link=0b%d%d ",
289 (uint32_t)(pos), len, (int)(lp), (int)(ln));
290
Nigel Tao3385efa2020-04-02 12:10:10 +1100291 if (vmajor > 0) {
Nigel Tao67f00c12020-02-29 12:48:59 +1100292 uint32_t m = vmajor;
293 uint32_t m0 = m / (38 * 38 * 38);
294 m -= m0 * (38 * 38 * 38);
295 uint32_t m1 = m / (38 * 38);
296 m -= m1 * (38 * 38);
297 uint32_t m2 = m / (38);
298 m -= m2 * (38);
299 uint32_t m3 = m;
300
301 printf("vmajor=0x%06" PRIX32 ":%c%c%c%c vminor=0x%06" PRIX32 "\n",
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100302 vmajor, g_base38_decode[m0], g_base38_decode[m1],
303 g_base38_decode[m2], g_base38_decode[m3], vminor);
Nigel Tao3385efa2020-04-02 12:10:10 +1100304 } else if (vmajor == 0) {
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100305 printf("vbc=%s. vbd=0x%06" PRIX32 "\n", g_vbc_names[vbc & 7], vbd);
Nigel Tao3385efa2020-04-02 12:10:10 +1100306 } else {
307 printf("extended... vextension=0x%012" PRIX64 "\n",
308 wuffs_base__token__value_extension(t));
Nigel Tao67f00c12020-02-29 12:48:59 +1100309 }
310
Nigel Taod1c928a2020-02-28 12:43:53 +1100311 } else {
Nigel Tao67f00c12020-02-29 12:48:59 +1100312 uint8_t buf[16];
313 wuffs_base__store_u32be__no_bounds_check(&buf[0x0], (uint32_t)(pos));
314 wuffs_base__store_u16be__no_bounds_check(&buf[0x4], len);
315 wuffs_base__store_u8__no_bounds_check(&buf[0x0006], lp);
316 wuffs_base__store_u8__no_bounds_check(&buf[0x0007], ln);
Nigel Tao3385efa2020-04-02 12:10:10 +1100317 if (vmajor > 0) {
318 wuffs_base__store_u32be__no_bounds_check(&buf[0x8], vmajor);
Nigel Tao67f00c12020-02-29 12:48:59 +1100319 wuffs_base__store_u32be__no_bounds_check(&buf[0xC], vminor);
Nigel Tao3385efa2020-04-02 12:10:10 +1100320 } else if (vmajor == 0) {
321 wuffs_base__store_u32be__no_bounds_check(&buf[0x8], 0);
Nigel Tao67f00c12020-02-29 12:48:59 +1100322 wuffs_base__store_u8__no_bounds_check(&buf[0x000C], vbc);
323 wuffs_base__store_u24be__no_bounds_check(&buf[0xD], vbd);
Nigel Tao3385efa2020-04-02 12:10:10 +1100324 } else {
325 wuffs_base__store_u8__no_bounds_check(&buf[0x0008], 0x01);
326 wuffs_base__store_u56be__no_bounds_check(
327 &buf[0x9], wuffs_base__token__value_extension(t));
Nigel Tao67f00c12020-02-29 12:48:59 +1100328 }
329 const int stdout_fd = 1;
Nigel Tao68920952020-03-03 11:25:18 +1100330 ignore_return_value(write(stdout_fd, &buf[0], 16));
Nigel Taod1c928a2020-02-28 12:43:53 +1100331 }
Nigel Tao193527b2020-02-26 21:55:01 +1100332 }
333
334 pos += len;
335 if (pos > 0xFFFFFFFF) {
336 return "main: input is too long";
337 }
338 }
339
340 if (status.repr == NULL) {
341 return NULL;
342 } else if (status.repr == wuffs_base__suspension__short_read) {
343 TRY(read_src());
344 } else if (status.repr == wuffs_base__suspension__short_write) {
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100345 wuffs_base__token_buffer__compact(&g_tok);
Nigel Tao193527b2020-02-26 21:55:01 +1100346 } else {
347 return wuffs_base__status__message(&status);
348 }
349 }
350}
351
352// ----
353
354int //
355compute_exit_code(const char* status_msg) {
356 if (!status_msg) {
357 return 0;
358 }
359 size_t n = strnlen(status_msg, 2047);
360 if (n >= 2047) {
361 status_msg = "main: internal error: error message is too long";
362 n = strnlen(status_msg, 2047);
363 }
364 fprintf(stderr, "%s\n", status_msg);
365 // Return an exit code of 1 for regular (forseen) errors, e.g. badly
366 // formatted or unsupported input.
367 //
368 // Return an exit code of 2 for internal (exceptional) errors, e.g. defensive
369 // run-time checks found that an internal invariant did not hold.
370 //
371 // Automated testing, including badly formatted inputs, can therefore
372 // discriminate between expected failure (exit code 1) and unexpected failure
373 // (other non-zero exit codes). Specifically, exit code 2 for internal
374 // invariant violation, exit code 139 (which is 128 + SIGSEGV on x86_64
375 // linux) for a segmentation fault (e.g. null pointer dereference).
376 return strstr(status_msg, "internal error:") ? 2 : 1;
377}
378
379int //
380main(int argc, char** argv) {
381 const char* z = main1(argc, argv);
382 int exit_code = compute_exit_code(z);
383 return exit_code;
384}