blob: 866723b63ba8b953ebb6ff479bec3f45d5f73e37 [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 Tao496e88b2020-04-09 22:10:08 +100027// | | | | | VALUE_EXTENSION |
28// | POS | LEN | CON |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.
Nigel Tao496e88b2020-04-09 22:10:08 +100035// - CON (2 bytes) is the continued bit
Nigel Tao3385efa2020-04-02 12:10:10 +110036// - EXT (1 bytes) is 1 for extended and 0 for simple tokens.
Nigel Taod1c928a2020-02-28 12:43:53 +110037//
Nigel Tao3385efa2020-04-02 12:10:10 +110038// Extended tokens have a VALUE_EXTENSION (7 bytes).
39//
40// Simple tokens have a VALUE_MAJOR (3 bytes) and then either 4 bytes
41// VALUE_MINOR (when VALUE_MAJOR is non-zero) or (1 + 3) bytes
42// VALUE_BASE_CATEGORY and VALUE_BASE_DETAIL (when VALUE_MAJOR is zero).
43//
44// ----
Nigel Taod1c928a2020-02-28 12:43:53 +110045//
Nigel Tao193527b2020-02-26 21:55:01 +110046// Together with the hexadecimal WUFFS_BASE__TOKEN__ETC constants defined in
47// token-public.h, this format is somewhat human-readable when piped through a
Nigel Taod1c928a2020-02-28 12:43:53 +110048// hex-dump program (such as /usr/bin/hd), printing one token per line.
Nigel Tao6b29f322020-02-29 17:05:24 +110049// Alternatively, pass the -h (--human-readable) flag to this program.
50//
51// Pass -a (--all-tokens) to print all tokens, including whitespace.
Nigel Tao193527b2020-02-26 21:55:01 +110052//
53// If the input or output is larger than the program's buffers (64 MiB and
54// 131072 tokens by default), there may be multiple valid tokenizations of any
55// given input. For example, if a source string "abcde" straddles an I/O
Nigel Tao496e88b2020-04-09 22:10:08 +100056// boundary, it may be tokenized as single (not continued) 5-length string or
57// as a 3-length continued string followed by a 2-length string.
Nigel Tao193527b2020-02-26 21:55:01 +110058//
Nigel Taod1c928a2020-02-28 12:43:53 +110059// A Wuffs token stream, in general, can support inputs more than 0xFFFF_FFFF
Nigel Tao193527b2020-02-26 21:55:01 +110060// bytes long, but this program can not, as it tracks the tokens' cumulative
61// position as a uint32.
62
63#include <inttypes.h>
64#include <stdio.h>
65#include <string.h>
66#include <unistd.h>
67
68// Wuffs ships as a "single file C library" or "header file library" as per
69// https://github.com/nothings/stb/blob/master/docs/stb_howto.txt
70//
71// To use that single file as a "foo.c"-like implementation, instead of a
72// "foo.h"-like header, #define WUFFS_IMPLEMENTATION before #include'ing or
73// compiling it.
74#define WUFFS_IMPLEMENTATION
75
Nigel Tao6fb76972021-10-07 14:36:35 +110076// Defining the WUFFS_CONFIG__STATIC_FUNCTIONS macro is optional, but when
77// combined with WUFFS_IMPLEMENTATION, it demonstrates making all of Wuffs'
78// functions have static storage.
79//
80// This can help the compiler ignore or discard unused code, which can produce
81// faster compiles and smaller binaries. Other motivations are discussed in the
82// "ALLOW STATIC IMPLEMENTATION" section of
83// https://raw.githubusercontent.com/nothings/stb/master/docs/stb_howto.txt
84#define WUFFS_CONFIG__STATIC_FUNCTIONS
85
Nigel Tao193527b2020-02-26 21:55:01 +110086// Defining the WUFFS_CONFIG__MODULE* macros are optional, but it lets users of
Nigel Tao2f788042021-01-23 19:29:19 +110087// release/c/etc.c choose which parts of Wuffs to build. That file contains the
88// entire Wuffs standard library, implementing a variety of codecs and file
Nigel Tao193527b2020-02-26 21:55:01 +110089// formats. Without this macro definition, an optimizing compiler or linker may
90// very well discard Wuffs code for unused codecs, but listing the Wuffs
91// modules we use makes that process explicit. Preprocessing means that such
92// code simply isn't compiled.
93#define WUFFS_CONFIG__MODULES
94#define WUFFS_CONFIG__MODULE__BASE
Nigel Tao4e193592020-07-15 12:48:57 +100095#define WUFFS_CONFIG__MODULE__CBOR
Nigel Tao193527b2020-02-26 21:55:01 +110096#define WUFFS_CONFIG__MODULE__JSON
97
98// If building this program in an environment that doesn't easily accommodate
99// relative includes, you can use the script/inline-c-relative-includes.go
Nigel Taoa20a2bb2020-09-07 21:06:58 +1000100// program to generate a stand-alone C file.
Nigel Tao193527b2020-02-26 21:55:01 +1100101#include "../release/c/wuffs-unsupported-snapshot.c"
102
Nigel Taof3146c22020-03-26 08:47:42 +1100103// Wuffs allows either statically or dynamically allocated work buffers. This
104// program exercises static allocation.
Nigel Tao4e193592020-07-15 12:48:57 +1000105#if WUFFS_CBOR__DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE > \
106 WUFFS_JSON__DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE
107#define WORK_BUFFER_ARRAY_SIZE \
108 WUFFS_CBOR__DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE
109#else
Nigel Taof3146c22020-03-26 08:47:42 +1100110#define WORK_BUFFER_ARRAY_SIZE \
111 WUFFS_JSON__DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE
Nigel Tao4e193592020-07-15 12:48:57 +1000112#endif
Nigel Taof3146c22020-03-26 08:47:42 +1100113#if WORK_BUFFER_ARRAY_SIZE > 0
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100114uint8_t g_work_buffer_array[WORK_BUFFER_ARRAY_SIZE];
Nigel Taof3146c22020-03-26 08:47:42 +1100115#else
116// Not all C/C++ compilers support 0-length arrays.
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100117uint8_t g_work_buffer_array[1];
Nigel Taof3146c22020-03-26 08:47:42 +1100118#endif
119
Nigel Taofdac24a2020-03-06 21:53:08 +1100120#ifndef SRC_BUFFER_ARRAY_SIZE
121#define SRC_BUFFER_ARRAY_SIZE (64 * 1024 * 1024)
Nigel Tao193527b2020-02-26 21:55:01 +1100122#endif
Nigel Taofdac24a2020-03-06 21:53:08 +1100123#ifndef TOKEN_BUFFER_ARRAY_SIZE
124#define TOKEN_BUFFER_ARRAY_SIZE (128 * 1024)
Nigel Tao193527b2020-02-26 21:55:01 +1100125#endif
126
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100127uint8_t g_src_buffer_array[SRC_BUFFER_ARRAY_SIZE];
128wuffs_base__token g_tok_buffer_array[TOKEN_BUFFER_ARRAY_SIZE];
Nigel Tao193527b2020-02-26 21:55:01 +1100129
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100130wuffs_base__io_buffer g_src;
131wuffs_base__token_buffer g_tok;
Nigel Tao193527b2020-02-26 21:55:01 +1100132
Nigel Tao4e193592020-07-15 12:48:57 +1000133wuffs_cbor__decoder g_cbor_decoder;
134wuffs_json__decoder g_json_decoder;
135wuffs_base__token_decoder* g_dec;
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100136wuffs_base__status g_dec_status;
Nigel Tao193527b2020-02-26 21:55:01 +1100137
Nigel Tao193527b2020-02-26 21:55:01 +1100138#define TRY(error_msg) \
139 do { \
140 const char* z = error_msg; \
141 if (z) { \
142 return z; \
143 } \
144 } while (false)
145
Nigel Tao68920952020-03-03 11:25:18 +1100146// ignore_return_value suppresses errors from -Wall -Werror.
147static void //
148ignore_return_value(int ignored) {}
149
Nigel Tao193527b2020-02-26 21:55:01 +1100150const char* //
151read_src() {
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100152 if (g_src.meta.closed) {
Nigel Tao193527b2020-02-26 21:55:01 +1100153 return "main: internal error: read requested on a closed source";
154 }
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100155 wuffs_base__io_buffer__compact(&g_src);
156 if (g_src.meta.wi >= g_src.data.len) {
157 return "main: g_src buffer is full";
Nigel Tao193527b2020-02-26 21:55:01 +1100158 }
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100159 size_t n = fread(g_src.data.ptr + g_src.meta.wi, sizeof(uint8_t),
160 g_src.data.len - g_src.meta.wi, stdin);
161 g_src.meta.wi += n;
162 g_src.meta.closed = feof(stdin);
163 if ((n == 0) && !g_src.meta.closed) {
Nigel Tao193527b2020-02-26 21:55:01 +1100164 return "main: read error";
165 }
166 return NULL;
167}
168
169// ----
170
Nigel Tao4e193592020-07-15 12:48:57 +1000171typedef enum file_format_enum {
172 FILE_FORMAT_JSON,
173 FILE_FORMAT_CBOR,
174} file_format;
175
Nigel Tao68920952020-03-03 11:25:18 +1100176struct {
177 int remaining_argc;
178 char** remaining_argv;
179
180 bool all_tokens;
181 bool human_readable;
Nigel Tao4e193592020-07-15 12:48:57 +1000182 file_format input_format;
Nigel Tao5d5a17c2020-03-24 16:38:43 +1100183 bool quirks;
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100184} g_flags = {0};
Nigel Tao68920952020-03-03 11:25:18 +1100185
186const char* //
187parse_flags(int argc, char** argv) {
188 int c = (argc > 0) ? 1 : 0; // Skip argv[0], the program name.
189 for (; c < argc; c++) {
190 char* arg = argv[c];
191 if (*arg++ != '-') {
192 break;
193 }
194
195 // A double-dash "--foo" is equivalent to a single-dash "-foo". As special
196 // cases, a bare "-" is not a flag (some programs may interpret it as
197 // stdin) and a bare "--" means to stop parsing flags.
198 if (*arg == '\x00') {
199 break;
200 } else if (*arg == '-') {
201 arg++;
202 if (*arg == '\x00') {
203 c++;
204 break;
205 }
206 }
207
208 if (!strcmp(arg, "a") || !strcmp(arg, "all-tokens")) {
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100209 g_flags.all_tokens = true;
Nigel Tao68920952020-03-03 11:25:18 +1100210 continue;
211 }
212 if (!strcmp(arg, "h") || !strcmp(arg, "human-readable")) {
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100213 g_flags.human_readable = true;
Nigel Tao68920952020-03-03 11:25:18 +1100214 continue;
215 }
Nigel Tao4e193592020-07-15 12:48:57 +1000216 if (!strcmp(arg, "i=cbor") || !strcmp(arg, "input-format=cbor")) {
217 g_flags.input_format = FILE_FORMAT_CBOR;
218 continue;
219 }
220 if (!strcmp(arg, "i=json") || !strcmp(arg, "input-format=json")) {
221 g_flags.input_format = FILE_FORMAT_JSON;
222 continue;
223 }
Nigel Tao5d5a17c2020-03-24 16:38:43 +1100224 if (!strcmp(arg, "q") || !strcmp(arg, "quirks")) {
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100225 g_flags.quirks = true;
Nigel Tao5d5a17c2020-03-24 16:38:43 +1100226 continue;
227 }
Nigel Tao68920952020-03-03 11:25:18 +1100228
229 return "main: unrecognized flag argument";
230 }
231
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100232 g_flags.remaining_argc = argc - c;
233 g_flags.remaining_argv = argv + c;
Nigel Tao68920952020-03-03 11:25:18 +1100234 return NULL;
235}
236
Nigel Tao496e88b2020-04-09 22:10:08 +1000237const char* g_vbc_names[16] = {
Nigel Taoc9d4e342020-07-21 15:20:34 +1000238 "0:Filler...........", //
239 "1:Structure........", //
240 "2:String...........", //
241 "3:UnicodeCodePoint.", //
242 "4:Literal..........", //
243 "5:Number...........", //
244 "6:InlineIntSigned..", //
245 "7:InlineIntUnsigned", //
246 "8:Reserved.........", //
247 "9:Reserved.........", //
248 "A:Reserved.........", //
249 "B:Reserved.........", //
250 "C:Reserved.........", //
251 "D:Reserved.........", //
252 "E:Reserved.........", //
253 "F:Reserved.........", //
Nigel Tao67f00c12020-02-29 12:48:59 +1100254};
255
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100256const int g_base38_decode[38] = {
Nigel Tao67f00c12020-02-29 12:48:59 +1100257 ' ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '?', //
258 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', //
259 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', //
260};
261
Nigel Tao193527b2020-02-26 21:55:01 +1100262const char* //
263main1(int argc, char** argv) {
Nigel Tao68920952020-03-03 11:25:18 +1100264 TRY(parse_flags(argc, argv));
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100265 if (g_flags.remaining_argc > 0) {
Nigel Tao68920952020-03-03 11:25:18 +1100266 return "main: bad argument: use \"program < input\", not \"program input\"";
Nigel Tao67f00c12020-02-29 12:48:59 +1100267 }
268
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100269 g_src = wuffs_base__make_io_buffer(
270 wuffs_base__make_slice_u8(g_src_buffer_array, SRC_BUFFER_ARRAY_SIZE),
Nigel Tao193527b2020-02-26 21:55:01 +1100271 wuffs_base__empty_io_buffer_meta());
272
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100273 g_tok = wuffs_base__make_token_buffer(
274 wuffs_base__make_slice_token(g_tok_buffer_array, TOKEN_BUFFER_ARRAY_SIZE),
Nigel Tao193527b2020-02-26 21:55:01 +1100275 wuffs_base__empty_token_buffer_meta());
276
Nigel Tao4e193592020-07-15 12:48:57 +1000277 if (g_flags.input_format == FILE_FORMAT_JSON) {
278 wuffs_base__status init_status = wuffs_json__decoder__initialize(
279 &g_json_decoder, sizeof__wuffs_json__decoder(), WUFFS_VERSION, 0);
280 if (!wuffs_base__status__is_ok(&init_status)) {
281 return wuffs_base__status__message(&init_status);
282 }
283 g_dec = wuffs_json__decoder__upcast_as__wuffs_base__token_decoder(
284 &g_json_decoder);
285 } else {
286 wuffs_base__status init_status = wuffs_cbor__decoder__initialize(
287 &g_cbor_decoder, sizeof__wuffs_cbor__decoder(), WUFFS_VERSION, 0);
288 if (!wuffs_base__status__is_ok(&init_status)) {
289 return wuffs_base__status__message(&init_status);
290 }
291 g_dec = wuffs_cbor__decoder__upcast_as__wuffs_base__token_decoder(
292 &g_cbor_decoder);
Nigel Tao193527b2020-02-26 21:55:01 +1100293 }
294
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100295 if (g_flags.quirks) {
Nigel Tao5d5a17c2020-03-24 16:38:43 +1100296 uint32_t quirks[] = {
297 WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_A,
298 WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_CAPITAL_U,
299 WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_E,
300 WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_QUESTION_MARK,
301 WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_SINGLE_QUOTE,
302 WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_V,
Nigel Tao1adbc952020-08-06 23:28:07 +1000303 WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_X_AS_CODE_POINTS,
Nigel Tao5d5a17c2020-03-24 16:38:43 +1100304 WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_ZERO,
305 WUFFS_JSON__QUIRK_ALLOW_COMMENT_BLOCK,
306 WUFFS_JSON__QUIRK_ALLOW_COMMENT_LINE,
307 WUFFS_JSON__QUIRK_ALLOW_EXTRA_COMMA,
308 WUFFS_JSON__QUIRK_ALLOW_INF_NAN_NUMBERS,
309 WUFFS_JSON__QUIRK_ALLOW_LEADING_ASCII_RECORD_SEPARATOR,
310 WUFFS_JSON__QUIRK_ALLOW_LEADING_UNICODE_BYTE_ORDER_MARK,
Nigel Taocd4cbc92020-09-22 22:22:15 +1000311 WUFFS_JSON__QUIRK_ALLOW_TRAILING_FILLER,
Nigel Tao3bfed6d2020-03-24 17:44:47 +1100312 WUFFS_JSON__QUIRK_REPLACE_INVALID_UNICODE,
Nigel Tao5d5a17c2020-03-24 16:38:43 +1100313 0,
314 };
315 uint32_t i;
316 for (i = 0; quirks[i]; i++) {
Nigel Tao4e193592020-07-15 12:48:57 +1000317 wuffs_base__token_decoder__set_quirk_enabled(g_dec, quirks[i], true);
Nigel Tao5d5a17c2020-03-24 16:38:43 +1100318 }
319 }
320
Nigel Tao193527b2020-02-26 21:55:01 +1100321 uint64_t pos = 0;
322 while (true) {
Nigel Tao4e193592020-07-15 12:48:57 +1000323 wuffs_base__status status = wuffs_base__token_decoder__decode_tokens(
324 g_dec, &g_tok, &g_src,
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100325 wuffs_base__make_slice_u8(g_work_buffer_array, WORK_BUFFER_ARRAY_SIZE));
Nigel Tao193527b2020-02-26 21:55:01 +1100326
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100327 while (g_tok.meta.ri < g_tok.meta.wi) {
328 wuffs_base__token* t = &g_tok.data.ptr[g_tok.meta.ri++];
Nigel Taod1c928a2020-02-28 12:43:53 +1100329 uint16_t len = wuffs_base__token__length(t);
Nigel Tao193527b2020-02-26 21:55:01 +1100330
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100331 if (g_flags.all_tokens || (wuffs_base__token__value(t) != 0)) {
Nigel Tao496e88b2020-04-09 22:10:08 +1000332 uint16_t con = wuffs_base__token__continued(t) ? 1 : 0;
Nigel Tao3385efa2020-04-02 12:10:10 +1100333 int32_t vmajor = wuffs_base__token__value_major(t);
Nigel Tao67f00c12020-02-29 12:48:59 +1100334 uint32_t vminor = wuffs_base__token__value_minor(t);
335 uint8_t vbc = wuffs_base__token__value_base_category(t);
336 uint32_t vbd = wuffs_base__token__value_base_detail(t);
Nigel Tao193527b2020-02-26 21:55:01 +1100337
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100338 if (g_flags.human_readable) {
Nigel Tao496e88b2020-04-09 22:10:08 +1000339 printf("pos=0x%08" PRIX32 " len=0x%04" PRIX16 " con=%" PRId16 " ",
340 (uint32_t)(pos), len, con);
Nigel Tao67f00c12020-02-29 12:48:59 +1100341
Nigel Tao3385efa2020-04-02 12:10:10 +1100342 if (vmajor > 0) {
Nigel Taof7ea4622020-04-10 22:44:13 +1000343 char vmajor_name[5];
344 vmajor_name[0] = '*';
345 vmajor_name[1] = '*';
346 vmajor_name[2] = '*';
347 vmajor_name[3] = '*';
348 vmajor_name[4] = '\x00';
Nigel Tao67f00c12020-02-29 12:48:59 +1100349 uint32_t m = vmajor;
Nigel Taof7ea4622020-04-10 22:44:13 +1000350 if (m < 38 * 38 * 38 * 38) {
351 uint32_t m0 = m / (38 * 38 * 38);
352 m -= m0 * (38 * 38 * 38);
353 vmajor_name[0] = g_base38_decode[m0];
Nigel Tao67f00c12020-02-29 12:48:59 +1100354
Nigel Taof7ea4622020-04-10 22:44:13 +1000355 uint32_t m1 = m / (38 * 38);
356 m -= m1 * (38 * 38);
357 vmajor_name[1] = g_base38_decode[m1];
358
359 uint32_t m2 = m / (38);
360 m -= m2 * (38);
361 vmajor_name[2] = g_base38_decode[m2];
362
363 uint32_t m3 = m;
364 vmajor_name[3] = g_base38_decode[m3];
365 }
366
Nigel Tao27168032020-07-24 13:05:05 +1000367 printf("vmajor=0x%06" PRIX32 ":%s vminor=0x%07" PRIX32 "\n", vmajor,
368 vmajor_name, vminor);
Nigel Tao3385efa2020-04-02 12:10:10 +1100369 } else if (vmajor == 0) {
Nigel Taoc9d4e342020-07-21 15:20:34 +1000370 printf("vbc=%s vbd=0x%06" PRIX32 "\n", g_vbc_names[vbc & 15], vbd);
Nigel Tao3385efa2020-04-02 12:10:10 +1100371 } else {
372 printf("extended... vextension=0x%012" PRIX64 "\n",
373 wuffs_base__token__value_extension(t));
Nigel Tao67f00c12020-02-29 12:48:59 +1100374 }
375
Nigel Taod1c928a2020-02-28 12:43:53 +1100376 } else {
Nigel Tao67f00c12020-02-29 12:48:59 +1100377 uint8_t buf[16];
Nigel Taoa1c22ca2021-01-17 22:22:49 +1100378 wuffs_base__poke_u32be__no_bounds_check(&buf[0x0], (uint32_t)(pos));
379 wuffs_base__poke_u16be__no_bounds_check(&buf[0x4], len);
380 wuffs_base__poke_u16be__no_bounds_check(&buf[0x6], con);
Nigel Tao3385efa2020-04-02 12:10:10 +1100381 if (vmajor > 0) {
Nigel Taoa1c22ca2021-01-17 22:22:49 +1100382 wuffs_base__poke_u32be__no_bounds_check(&buf[0x8], vmajor);
383 wuffs_base__poke_u32be__no_bounds_check(&buf[0xC], vminor);
Nigel Tao3385efa2020-04-02 12:10:10 +1100384 } else if (vmajor == 0) {
Nigel Taoa1c22ca2021-01-17 22:22:49 +1100385 wuffs_base__poke_u32be__no_bounds_check(&buf[0x8], 0);
386 wuffs_base__poke_u8__no_bounds_check(&buf[0x000C], vbc);
387 wuffs_base__poke_u24be__no_bounds_check(&buf[0xD], vbd);
Nigel Tao3385efa2020-04-02 12:10:10 +1100388 } else {
Nigel Taoa1c22ca2021-01-17 22:22:49 +1100389 wuffs_base__poke_u8__no_bounds_check(&buf[0x0008], 0x01);
390 wuffs_base__poke_u56be__no_bounds_check(
Nigel Tao3385efa2020-04-02 12:10:10 +1100391 &buf[0x9], wuffs_base__token__value_extension(t));
Nigel Tao67f00c12020-02-29 12:48:59 +1100392 }
393 const int stdout_fd = 1;
Nigel Tao68920952020-03-03 11:25:18 +1100394 ignore_return_value(write(stdout_fd, &buf[0], 16));
Nigel Taod1c928a2020-02-28 12:43:53 +1100395 }
Nigel Tao193527b2020-02-26 21:55:01 +1100396 }
397
398 pos += len;
399 if (pos > 0xFFFFFFFF) {
400 return "main: input is too long";
401 }
402 }
403
404 if (status.repr == NULL) {
405 return NULL;
406 } else if (status.repr == wuffs_base__suspension__short_read) {
407 TRY(read_src());
408 } else if (status.repr == wuffs_base__suspension__short_write) {
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100409 wuffs_base__token_buffer__compact(&g_tok);
Nigel Tao193527b2020-02-26 21:55:01 +1100410 } else {
411 return wuffs_base__status__message(&status);
412 }
413 }
414}
415
416// ----
417
418int //
419compute_exit_code(const char* status_msg) {
420 if (!status_msg) {
421 return 0;
422 }
423 size_t n = strnlen(status_msg, 2047);
424 if (n >= 2047) {
425 status_msg = "main: internal error: error message is too long";
426 n = strnlen(status_msg, 2047);
427 }
428 fprintf(stderr, "%s\n", status_msg);
Nigel Taoa51867d2021-05-19 21:34:09 +1000429 // Return an exit code of 1 for regular (foreseen) errors, e.g. badly
Nigel Tao193527b2020-02-26 21:55:01 +1100430 // formatted or unsupported input.
431 //
432 // Return an exit code of 2 for internal (exceptional) errors, e.g. defensive
433 // run-time checks found that an internal invariant did not hold.
434 //
435 // Automated testing, including badly formatted inputs, can therefore
436 // discriminate between expected failure (exit code 1) and unexpected failure
437 // (other non-zero exit codes). Specifically, exit code 2 for internal
438 // invariant violation, exit code 139 (which is 128 + SIGSEGV on x86_64
439 // linux) for a segmentation fault (e.g. null pointer dereference).
440 return strstr(status_msg, "internal error:") ? 2 : 1;
441}
442
443int //
444main(int argc, char** argv) {
445 const char* z = main1(argc, argv);
446 int exit_code = compute_exit_code(z);
447 return exit_code;
448}