blob: 14af81784f48effb2519d29c7439cdb0b36ace6b [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
76// Defining the WUFFS_CONFIG__MODULE* macros are optional, but it lets users of
Nigel Tao2f788042021-01-23 19:29:19 +110077// release/c/etc.c choose which parts of Wuffs to build. That file contains the
78// entire Wuffs standard library, implementing a variety of codecs and file
Nigel Tao193527b2020-02-26 21:55:01 +110079// formats. Without this macro definition, an optimizing compiler or linker may
80// very well discard Wuffs code for unused codecs, but listing the Wuffs
81// modules we use makes that process explicit. Preprocessing means that such
82// code simply isn't compiled.
83#define WUFFS_CONFIG__MODULES
84#define WUFFS_CONFIG__MODULE__BASE
Nigel Tao4e193592020-07-15 12:48:57 +100085#define WUFFS_CONFIG__MODULE__CBOR
Nigel Tao193527b2020-02-26 21:55:01 +110086#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
Nigel Taoa20a2bb2020-09-07 21:06:58 +100090// program to generate a stand-alone C file.
Nigel Tao193527b2020-02-26 21:55:01 +110091#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.
Nigel Tao4e193592020-07-15 12:48:57 +100095#if WUFFS_CBOR__DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE > \
96 WUFFS_JSON__DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE
97#define WORK_BUFFER_ARRAY_SIZE \
98 WUFFS_CBOR__DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE
99#else
Nigel Taof3146c22020-03-26 08:47:42 +1100100#define WORK_BUFFER_ARRAY_SIZE \
101 WUFFS_JSON__DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE
Nigel Tao4e193592020-07-15 12:48:57 +1000102#endif
Nigel Taof3146c22020-03-26 08:47:42 +1100103#if WORK_BUFFER_ARRAY_SIZE > 0
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100104uint8_t g_work_buffer_array[WORK_BUFFER_ARRAY_SIZE];
Nigel Taof3146c22020-03-26 08:47:42 +1100105#else
106// Not all C/C++ compilers support 0-length arrays.
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100107uint8_t g_work_buffer_array[1];
Nigel Taof3146c22020-03-26 08:47:42 +1100108#endif
109
Nigel Taofdac24a2020-03-06 21:53:08 +1100110#ifndef SRC_BUFFER_ARRAY_SIZE
111#define SRC_BUFFER_ARRAY_SIZE (64 * 1024 * 1024)
Nigel Tao193527b2020-02-26 21:55:01 +1100112#endif
Nigel Taofdac24a2020-03-06 21:53:08 +1100113#ifndef TOKEN_BUFFER_ARRAY_SIZE
114#define TOKEN_BUFFER_ARRAY_SIZE (128 * 1024)
Nigel Tao193527b2020-02-26 21:55:01 +1100115#endif
116
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100117uint8_t g_src_buffer_array[SRC_BUFFER_ARRAY_SIZE];
118wuffs_base__token g_tok_buffer_array[TOKEN_BUFFER_ARRAY_SIZE];
Nigel Tao193527b2020-02-26 21:55:01 +1100119
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100120wuffs_base__io_buffer g_src;
121wuffs_base__token_buffer g_tok;
Nigel Tao193527b2020-02-26 21:55:01 +1100122
Nigel Tao4e193592020-07-15 12:48:57 +1000123wuffs_cbor__decoder g_cbor_decoder;
124wuffs_json__decoder g_json_decoder;
125wuffs_base__token_decoder* g_dec;
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100126wuffs_base__status g_dec_status;
Nigel Tao193527b2020-02-26 21:55:01 +1100127
Nigel Tao193527b2020-02-26 21:55:01 +1100128#define TRY(error_msg) \
129 do { \
130 const char* z = error_msg; \
131 if (z) { \
132 return z; \
133 } \
134 } while (false)
135
Nigel Tao68920952020-03-03 11:25:18 +1100136// ignore_return_value suppresses errors from -Wall -Werror.
137static void //
138ignore_return_value(int ignored) {}
139
Nigel Tao193527b2020-02-26 21:55:01 +1100140const char* //
141read_src() {
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100142 if (g_src.meta.closed) {
Nigel Tao193527b2020-02-26 21:55:01 +1100143 return "main: internal error: read requested on a closed source";
144 }
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100145 wuffs_base__io_buffer__compact(&g_src);
146 if (g_src.meta.wi >= g_src.data.len) {
147 return "main: g_src buffer is full";
Nigel Tao193527b2020-02-26 21:55:01 +1100148 }
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100149 size_t n = fread(g_src.data.ptr + g_src.meta.wi, sizeof(uint8_t),
150 g_src.data.len - g_src.meta.wi, stdin);
151 g_src.meta.wi += n;
152 g_src.meta.closed = feof(stdin);
153 if ((n == 0) && !g_src.meta.closed) {
Nigel Tao193527b2020-02-26 21:55:01 +1100154 return "main: read error";
155 }
156 return NULL;
157}
158
159// ----
160
Nigel Tao4e193592020-07-15 12:48:57 +1000161typedef enum file_format_enum {
162 FILE_FORMAT_JSON,
163 FILE_FORMAT_CBOR,
164} file_format;
165
Nigel Tao68920952020-03-03 11:25:18 +1100166struct {
167 int remaining_argc;
168 char** remaining_argv;
169
170 bool all_tokens;
171 bool human_readable;
Nigel Tao4e193592020-07-15 12:48:57 +1000172 file_format input_format;
Nigel Tao5d5a17c2020-03-24 16:38:43 +1100173 bool quirks;
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100174} g_flags = {0};
Nigel Tao68920952020-03-03 11:25:18 +1100175
176const char* //
177parse_flags(int argc, char** argv) {
178 int c = (argc > 0) ? 1 : 0; // Skip argv[0], the program name.
179 for (; c < argc; c++) {
180 char* arg = argv[c];
181 if (*arg++ != '-') {
182 break;
183 }
184
185 // A double-dash "--foo" is equivalent to a single-dash "-foo". As special
186 // cases, a bare "-" is not a flag (some programs may interpret it as
187 // stdin) and a bare "--" means to stop parsing flags.
188 if (*arg == '\x00') {
189 break;
190 } else if (*arg == '-') {
191 arg++;
192 if (*arg == '\x00') {
193 c++;
194 break;
195 }
196 }
197
198 if (!strcmp(arg, "a") || !strcmp(arg, "all-tokens")) {
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100199 g_flags.all_tokens = true;
Nigel Tao68920952020-03-03 11:25:18 +1100200 continue;
201 }
202 if (!strcmp(arg, "h") || !strcmp(arg, "human-readable")) {
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100203 g_flags.human_readable = true;
Nigel Tao68920952020-03-03 11:25:18 +1100204 continue;
205 }
Nigel Tao4e193592020-07-15 12:48:57 +1000206 if (!strcmp(arg, "i=cbor") || !strcmp(arg, "input-format=cbor")) {
207 g_flags.input_format = FILE_FORMAT_CBOR;
208 continue;
209 }
210 if (!strcmp(arg, "i=json") || !strcmp(arg, "input-format=json")) {
211 g_flags.input_format = FILE_FORMAT_JSON;
212 continue;
213 }
Nigel Tao5d5a17c2020-03-24 16:38:43 +1100214 if (!strcmp(arg, "q") || !strcmp(arg, "quirks")) {
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100215 g_flags.quirks = true;
Nigel Tao5d5a17c2020-03-24 16:38:43 +1100216 continue;
217 }
Nigel Tao68920952020-03-03 11:25:18 +1100218
219 return "main: unrecognized flag argument";
220 }
221
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100222 g_flags.remaining_argc = argc - c;
223 g_flags.remaining_argv = argv + c;
Nigel Tao68920952020-03-03 11:25:18 +1100224 return NULL;
225}
226
Nigel Tao496e88b2020-04-09 22:10:08 +1000227const char* g_vbc_names[16] = {
Nigel Taoc9d4e342020-07-21 15:20:34 +1000228 "0:Filler...........", //
229 "1:Structure........", //
230 "2:String...........", //
231 "3:UnicodeCodePoint.", //
232 "4:Literal..........", //
233 "5:Number...........", //
234 "6:InlineIntSigned..", //
235 "7:InlineIntUnsigned", //
236 "8:Reserved.........", //
237 "9:Reserved.........", //
238 "A:Reserved.........", //
239 "B:Reserved.........", //
240 "C:Reserved.........", //
241 "D:Reserved.........", //
242 "E:Reserved.........", //
243 "F:Reserved.........", //
Nigel Tao67f00c12020-02-29 12:48:59 +1100244};
245
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100246const int g_base38_decode[38] = {
Nigel Tao67f00c12020-02-29 12:48:59 +1100247 ' ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '?', //
248 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', //
249 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', //
250};
251
Nigel Tao193527b2020-02-26 21:55:01 +1100252const char* //
253main1(int argc, char** argv) {
Nigel Tao68920952020-03-03 11:25:18 +1100254 TRY(parse_flags(argc, argv));
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100255 if (g_flags.remaining_argc > 0) {
Nigel Tao68920952020-03-03 11:25:18 +1100256 return "main: bad argument: use \"program < input\", not \"program input\"";
Nigel Tao67f00c12020-02-29 12:48:59 +1100257 }
258
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100259 g_src = wuffs_base__make_io_buffer(
260 wuffs_base__make_slice_u8(g_src_buffer_array, SRC_BUFFER_ARRAY_SIZE),
Nigel Tao193527b2020-02-26 21:55:01 +1100261 wuffs_base__empty_io_buffer_meta());
262
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100263 g_tok = wuffs_base__make_token_buffer(
264 wuffs_base__make_slice_token(g_tok_buffer_array, TOKEN_BUFFER_ARRAY_SIZE),
Nigel Tao193527b2020-02-26 21:55:01 +1100265 wuffs_base__empty_token_buffer_meta());
266
Nigel Tao4e193592020-07-15 12:48:57 +1000267 if (g_flags.input_format == FILE_FORMAT_JSON) {
268 wuffs_base__status init_status = wuffs_json__decoder__initialize(
269 &g_json_decoder, sizeof__wuffs_json__decoder(), WUFFS_VERSION, 0);
270 if (!wuffs_base__status__is_ok(&init_status)) {
271 return wuffs_base__status__message(&init_status);
272 }
273 g_dec = wuffs_json__decoder__upcast_as__wuffs_base__token_decoder(
274 &g_json_decoder);
275 } else {
276 wuffs_base__status init_status = wuffs_cbor__decoder__initialize(
277 &g_cbor_decoder, sizeof__wuffs_cbor__decoder(), WUFFS_VERSION, 0);
278 if (!wuffs_base__status__is_ok(&init_status)) {
279 return wuffs_base__status__message(&init_status);
280 }
281 g_dec = wuffs_cbor__decoder__upcast_as__wuffs_base__token_decoder(
282 &g_cbor_decoder);
Nigel Tao193527b2020-02-26 21:55:01 +1100283 }
284
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100285 if (g_flags.quirks) {
Nigel Tao5d5a17c2020-03-24 16:38:43 +1100286 uint32_t quirks[] = {
287 WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_A,
288 WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_CAPITAL_U,
289 WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_E,
290 WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_QUESTION_MARK,
291 WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_SINGLE_QUOTE,
292 WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_V,
Nigel Tao1adbc952020-08-06 23:28:07 +1000293 WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_X_AS_CODE_POINTS,
Nigel Tao5d5a17c2020-03-24 16:38:43 +1100294 WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_ZERO,
295 WUFFS_JSON__QUIRK_ALLOW_COMMENT_BLOCK,
296 WUFFS_JSON__QUIRK_ALLOW_COMMENT_LINE,
297 WUFFS_JSON__QUIRK_ALLOW_EXTRA_COMMA,
298 WUFFS_JSON__QUIRK_ALLOW_INF_NAN_NUMBERS,
299 WUFFS_JSON__QUIRK_ALLOW_LEADING_ASCII_RECORD_SEPARATOR,
300 WUFFS_JSON__QUIRK_ALLOW_LEADING_UNICODE_BYTE_ORDER_MARK,
Nigel Taocd4cbc92020-09-22 22:22:15 +1000301 WUFFS_JSON__QUIRK_ALLOW_TRAILING_FILLER,
Nigel Tao3bfed6d2020-03-24 17:44:47 +1100302 WUFFS_JSON__QUIRK_REPLACE_INVALID_UNICODE,
Nigel Tao5d5a17c2020-03-24 16:38:43 +1100303 0,
304 };
305 uint32_t i;
306 for (i = 0; quirks[i]; i++) {
Nigel Tao4e193592020-07-15 12:48:57 +1000307 wuffs_base__token_decoder__set_quirk_enabled(g_dec, quirks[i], true);
Nigel Tao5d5a17c2020-03-24 16:38:43 +1100308 }
309 }
310
Nigel Tao193527b2020-02-26 21:55:01 +1100311 uint64_t pos = 0;
312 while (true) {
Nigel Tao4e193592020-07-15 12:48:57 +1000313 wuffs_base__status status = wuffs_base__token_decoder__decode_tokens(
314 g_dec, &g_tok, &g_src,
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100315 wuffs_base__make_slice_u8(g_work_buffer_array, WORK_BUFFER_ARRAY_SIZE));
Nigel Tao193527b2020-02-26 21:55:01 +1100316
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100317 while (g_tok.meta.ri < g_tok.meta.wi) {
318 wuffs_base__token* t = &g_tok.data.ptr[g_tok.meta.ri++];
Nigel Taod1c928a2020-02-28 12:43:53 +1100319 uint16_t len = wuffs_base__token__length(t);
Nigel Tao193527b2020-02-26 21:55:01 +1100320
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100321 if (g_flags.all_tokens || (wuffs_base__token__value(t) != 0)) {
Nigel Tao496e88b2020-04-09 22:10:08 +1000322 uint16_t con = wuffs_base__token__continued(t) ? 1 : 0;
Nigel Tao3385efa2020-04-02 12:10:10 +1100323 int32_t vmajor = wuffs_base__token__value_major(t);
Nigel Tao67f00c12020-02-29 12:48:59 +1100324 uint32_t vminor = wuffs_base__token__value_minor(t);
325 uint8_t vbc = wuffs_base__token__value_base_category(t);
326 uint32_t vbd = wuffs_base__token__value_base_detail(t);
Nigel Tao193527b2020-02-26 21:55:01 +1100327
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100328 if (g_flags.human_readable) {
Nigel Tao496e88b2020-04-09 22:10:08 +1000329 printf("pos=0x%08" PRIX32 " len=0x%04" PRIX16 " con=%" PRId16 " ",
330 (uint32_t)(pos), len, con);
Nigel Tao67f00c12020-02-29 12:48:59 +1100331
Nigel Tao3385efa2020-04-02 12:10:10 +1100332 if (vmajor > 0) {
Nigel Taof7ea4622020-04-10 22:44:13 +1000333 char vmajor_name[5];
334 vmajor_name[0] = '*';
335 vmajor_name[1] = '*';
336 vmajor_name[2] = '*';
337 vmajor_name[3] = '*';
338 vmajor_name[4] = '\x00';
Nigel Tao67f00c12020-02-29 12:48:59 +1100339 uint32_t m = vmajor;
Nigel Taof7ea4622020-04-10 22:44:13 +1000340 if (m < 38 * 38 * 38 * 38) {
341 uint32_t m0 = m / (38 * 38 * 38);
342 m -= m0 * (38 * 38 * 38);
343 vmajor_name[0] = g_base38_decode[m0];
Nigel Tao67f00c12020-02-29 12:48:59 +1100344
Nigel Taof7ea4622020-04-10 22:44:13 +1000345 uint32_t m1 = m / (38 * 38);
346 m -= m1 * (38 * 38);
347 vmajor_name[1] = g_base38_decode[m1];
348
349 uint32_t m2 = m / (38);
350 m -= m2 * (38);
351 vmajor_name[2] = g_base38_decode[m2];
352
353 uint32_t m3 = m;
354 vmajor_name[3] = g_base38_decode[m3];
355 }
356
Nigel Tao27168032020-07-24 13:05:05 +1000357 printf("vmajor=0x%06" PRIX32 ":%s vminor=0x%07" PRIX32 "\n", vmajor,
358 vmajor_name, vminor);
Nigel Tao3385efa2020-04-02 12:10:10 +1100359 } else if (vmajor == 0) {
Nigel Taoc9d4e342020-07-21 15:20:34 +1000360 printf("vbc=%s vbd=0x%06" PRIX32 "\n", g_vbc_names[vbc & 15], vbd);
Nigel Tao3385efa2020-04-02 12:10:10 +1100361 } else {
362 printf("extended... vextension=0x%012" PRIX64 "\n",
363 wuffs_base__token__value_extension(t));
Nigel Tao67f00c12020-02-29 12:48:59 +1100364 }
365
Nigel Taod1c928a2020-02-28 12:43:53 +1100366 } else {
Nigel Tao67f00c12020-02-29 12:48:59 +1100367 uint8_t buf[16];
Nigel Taoa1c22ca2021-01-17 22:22:49 +1100368 wuffs_base__poke_u32be__no_bounds_check(&buf[0x0], (uint32_t)(pos));
369 wuffs_base__poke_u16be__no_bounds_check(&buf[0x4], len);
370 wuffs_base__poke_u16be__no_bounds_check(&buf[0x6], con);
Nigel Tao3385efa2020-04-02 12:10:10 +1100371 if (vmajor > 0) {
Nigel Taoa1c22ca2021-01-17 22:22:49 +1100372 wuffs_base__poke_u32be__no_bounds_check(&buf[0x8], vmajor);
373 wuffs_base__poke_u32be__no_bounds_check(&buf[0xC], vminor);
Nigel Tao3385efa2020-04-02 12:10:10 +1100374 } else if (vmajor == 0) {
Nigel Taoa1c22ca2021-01-17 22:22:49 +1100375 wuffs_base__poke_u32be__no_bounds_check(&buf[0x8], 0);
376 wuffs_base__poke_u8__no_bounds_check(&buf[0x000C], vbc);
377 wuffs_base__poke_u24be__no_bounds_check(&buf[0xD], vbd);
Nigel Tao3385efa2020-04-02 12:10:10 +1100378 } else {
Nigel Taoa1c22ca2021-01-17 22:22:49 +1100379 wuffs_base__poke_u8__no_bounds_check(&buf[0x0008], 0x01);
380 wuffs_base__poke_u56be__no_bounds_check(
Nigel Tao3385efa2020-04-02 12:10:10 +1100381 &buf[0x9], wuffs_base__token__value_extension(t));
Nigel Tao67f00c12020-02-29 12:48:59 +1100382 }
383 const int stdout_fd = 1;
Nigel Tao68920952020-03-03 11:25:18 +1100384 ignore_return_value(write(stdout_fd, &buf[0], 16));
Nigel Taod1c928a2020-02-28 12:43:53 +1100385 }
Nigel Tao193527b2020-02-26 21:55:01 +1100386 }
387
388 pos += len;
389 if (pos > 0xFFFFFFFF) {
390 return "main: input is too long";
391 }
392 }
393
394 if (status.repr == NULL) {
395 return NULL;
396 } else if (status.repr == wuffs_base__suspension__short_read) {
397 TRY(read_src());
398 } else if (status.repr == wuffs_base__suspension__short_write) {
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100399 wuffs_base__token_buffer__compact(&g_tok);
Nigel Tao193527b2020-02-26 21:55:01 +1100400 } else {
401 return wuffs_base__status__message(&status);
402 }
403 }
404}
405
406// ----
407
408int //
409compute_exit_code(const char* status_msg) {
410 if (!status_msg) {
411 return 0;
412 }
413 size_t n = strnlen(status_msg, 2047);
414 if (n >= 2047) {
415 status_msg = "main: internal error: error message is too long";
416 n = strnlen(status_msg, 2047);
417 }
418 fprintf(stderr, "%s\n", status_msg);
419 // Return an exit code of 1 for regular (forseen) errors, e.g. badly
420 // formatted or unsupported input.
421 //
422 // Return an exit code of 2 for internal (exceptional) errors, e.g. defensive
423 // run-time checks found that an internal invariant did not hold.
424 //
425 // Automated testing, including badly formatted inputs, can therefore
426 // discriminate between expected failure (exit code 1) and unexpected failure
427 // (other non-zero exit codes). Specifically, exit code 2 for internal
428 // invariant violation, exit code 139 (which is 128 + SIGSEGV on x86_64
429 // linux) for a segmentation fault (e.g. null pointer dereference).
430 return strstr(status_msg, "internal error:") ? 2 : 1;
431}
432
433int //
434main(int argc, char** argv) {
435 const char* z = main1(argc, argv);
436 int exit_code = compute_exit_code(z);
437 return exit_code;
438}