blob: eda3514473c69f1b2ecc08d36281806ca21b5028 [file] [log] [blame]
Nigel Tao1b073492020-02-16 22:11:36 +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/*
Nigel Tao0cd2f982020-03-03 23:03:02 +110018jsonptr is a JSON formatter (pretty-printer) that supports the JSON Pointer
Nigel Tao0291a472020-08-13 22:40:10 +100019(RFC 6901) query syntax. It reads UTF-8 JSON from stdin and writes
20canonicalized, formatted UTF-8 JSON to stdout.
Nigel Tao0cd2f982020-03-03 23:03:02 +110021
Nigel Taod60815c2020-03-26 14:32:35 +110022See the "const char* g_usage" string below for details.
Nigel Tao0cd2f982020-03-03 23:03:02 +110023
24----
25
26JSON Pointer (and this program's implementation) is one of many JSON query
27languages and JSON tools, such as jq, jql and JMESPath. This one is relatively
28simple and fewer-featured compared to those others.
29
Nigel Tao0291a472020-08-13 22:40:10 +100030One benefit of simplicity is that this program's JSON and JSON Pointer
Nigel Tao0cd2f982020-03-03 23:03:02 +110031implementations do not dynamically allocate or free memory (yet it does not
32require that the entire input fits in memory at once). They are therefore
33trivially protected against certain bug classes: memory leaks, double-frees and
34use-after-frees.
35
Nigel Tao0291a472020-08-13 22:40:10 +100036The core JSON implementation is also written in the Wuffs programming language
37(and then transpiled to C/C++), which is memory-safe (e.g. array indexing is
38bounds-checked) but also guards against integer arithmetic overflows.
Nigel Tao0cd2f982020-03-03 23:03:02 +110039
Nigel Taofe0cbbd2020-03-05 22:01:30 +110040For defense in depth, on Linux, this program also self-imposes a
41SECCOMP_MODE_STRICT sandbox before reading (or otherwise processing) its input
42or writing its output. Under this sandbox, the only permitted system calls are
43read, write, exit and sigreturn.
44
Nigel Tao0291a472020-08-13 22:40:10 +100045All together, this program aims to safely handle untrusted JSON files without
46fear of security bugs such as remote code execution.
Nigel Tao0cd2f982020-03-03 23:03:02 +110047
48----
Nigel Tao1b073492020-02-16 22:11:36 +110049
Nigel Taoc5b3a9e2020-02-24 11:54:35 +110050As of 2020-02-24, this program passes all 318 "test_parsing" cases from the
51JSON test suite (https://github.com/nst/JSONTestSuite), an appendix to the
52"Parsing JSON is a Minefield" article (http://seriot.ch/parsing_json.php) that
53was first published on 2016-10-26 and updated on 2018-03-30.
54
Nigel Tao0cd2f982020-03-03 23:03:02 +110055After modifying this program, run "build-example.sh example/jsonptr/" and then
56"script/run-json-test-suite.sh" to catch correctness regressions.
57
58----
59
Nigel Taod0b16cb2020-03-14 10:15:54 +110060This program uses Wuffs' JSON decoder at a relatively low level, processing the
61decoder's token-stream output individually. The core loop, in pseudo-code, is
62"for_each_token { handle_token(etc); }", where the handle_token function
Nigel Taod60815c2020-03-26 14:32:35 +110063changes global state (e.g. the `g_depth` and `g_ctx` variables) and prints
Nigel Taod0b16cb2020-03-14 10:15:54 +110064output text based on that state and the token's source text. Notably,
65handle_token is not recursive, even though JSON values can nest.
66
67This approach is centered around JSON tokens. Each JSON 'thing' (e.g. number,
68string, object) comprises one or more JSON tokens.
69
70An alternative, higher-level approach is in the sibling example/jsonfindptrs
71program. Neither approach is better or worse per se, but when studying this
72program, be aware that there are multiple ways to use Wuffs' JSON decoder.
73
74The two programs, jsonfindptrs and jsonptr, also demonstrate different
75trade-offs with regard to JSON object duplicate keys. The JSON spec permits
76different implementations to allow or reject duplicate keys. It is not always
77clear which approach is safer. Rejecting them is certainly unambiguous, and
78security bugs can lurk in ambiguous corners of a file format, if two different
79implementations both silently accept a file but differ on how to interpret it.
80On the other hand, in the worst case, detecting duplicate keys requires O(N)
81memory, where N is the size of the (potentially untrusted) input.
82
83This program (jsonptr) allows duplicate keys and requires only O(1) memory. As
84mentioned above, it doesn't dynamically allocate memory at all, and on Linux,
85it runs in a SECCOMP_MODE_STRICT sandbox.
86
87----
88
Nigel Tao50bfab92020-08-05 11:39:09 +100089To run:
Nigel Tao1b073492020-02-16 22:11:36 +110090
91$CXX jsonptr.cc && ./a.out < ../../test/data/github-tags.json; rm -f a.out
92
93for a C++ compiler $CXX, such as clang++ or g++.
94*/
95
Nigel Tao721190a2020-04-03 22:25:21 +110096#if defined(__cplusplus) && (__cplusplus < 201103L)
97#error "This C++ program requires -std=c++11 or later"
98#endif
99
Nigel Taofe0cbbd2020-03-05 22:01:30 +1100100#include <errno.h>
Nigel Tao01abc842020-03-06 21:42:33 +1100101#include <fcntl.h>
102#include <stdio.h>
Nigel Tao9cc2c252020-02-23 17:05:49 +1100103#include <string.h>
Nigel Taofe0cbbd2020-03-05 22:01:30 +1100104#include <unistd.h>
Nigel Tao1b073492020-02-16 22:11:36 +1100105
106// Wuffs ships as a "single file C library" or "header file library" as per
107// https://github.com/nothings/stb/blob/master/docs/stb_howto.txt
108//
109// To use that single file as a "foo.c"-like implementation, instead of a
110// "foo.h"-like header, #define WUFFS_IMPLEMENTATION before #include'ing or
111// compiling it.
112#define WUFFS_IMPLEMENTATION
113
114// Defining the WUFFS_CONFIG__MODULE* macros are optional, but it lets users of
115// release/c/etc.c whitelist which parts of Wuffs to build. That file contains
116// the entire Wuffs standard library, implementing a variety of codecs and file
117// formats. Without this macro definition, an optimizing compiler or linker may
118// very well discard Wuffs code for unused codecs, but listing the Wuffs
119// modules we use makes that process explicit. Preprocessing means that such
120// code simply isn't compiled.
121#define WUFFS_CONFIG__MODULES
122#define WUFFS_CONFIG__MODULE__BASE
123#define WUFFS_CONFIG__MODULE__JSON
124
125// If building this program in an environment that doesn't easily accommodate
126// relative includes, you can use the script/inline-c-relative-includes.go
127// program to generate a stand-alone C++ file.
128#include "../../release/c/wuffs-unsupported-snapshot.c"
129
Nigel Taofe0cbbd2020-03-05 22:01:30 +1100130#if defined(__linux__)
131#include <linux/prctl.h>
132#include <linux/seccomp.h>
133#include <sys/prctl.h>
134#include <sys/syscall.h>
135#define WUFFS_EXAMPLE_USE_SECCOMP
136#endif
137
Nigel Tao2cf76db2020-02-27 22:42: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 Taod60815c2020-03-26 14:32:35 +1100146static const char* g_eod = "main: end of data";
Nigel Tao2cf76db2020-02-27 22:42:01 +1100147
Nigel Taod60815c2020-03-26 14:32:35 +1100148static const char* g_usage =
Nigel Tao01abc842020-03-06 21:42:33 +1100149 "Usage: jsonptr -flags input.json\n"
Nigel Tao0cd2f982020-03-03 23:03:02 +1100150 "\n"
Nigel Tao52c4d6a2020-03-08 21:12:38 +1100151 "Flags:\n"
Nigel Tao3690e832020-03-12 16:52:26 +1100152 " -c -compact-output\n"
Nigel Tao94440cf2020-04-02 22:28:24 +1100153 " -d=NUM -max-output-depth=NUM\n"
Nigel Tao52c4d6a2020-03-08 21:12:38 +1100154 " -q=STR -query=STR\n"
Nigel Taoecadf722020-07-13 08:22:34 +1000155 " -s=NUM -spaces=NUM\n"
Nigel Tao52c4d6a2020-03-08 21:12:38 +1100156 " -t -tabs\n"
157 " -fail-if-unsandboxed\n"
Nigel Tao0291a472020-08-13 22:40:10 +1000158 " -input-allow-comments\n"
159 " -input-allow-extra-comma\n"
160 " -input-allow-inf-nan-numbers\n"
Nigel Tao21042052020-08-19 23:13:54 +1000161 " -jwcc\n"
162 " -output-comments\n"
Nigel Tao0291a472020-08-13 22:40:10 +1000163 " -output-extra-comma\n"
Nigel Tao75682542020-08-22 21:40:18 +1000164 " -output-inf-nan-numbers\n"
Nigel Taoecadf722020-07-13 08:22:34 +1000165 " -strict-json-pointer-syntax\n"
Nigel Tao52c4d6a2020-03-08 21:12:38 +1100166 "\n"
Nigel Tao01abc842020-03-06 21:42:33 +1100167 "The input.json filename is optional. If absent, it reads from stdin.\n"
Nigel Tao0cd2f982020-03-03 23:03:02 +1100168 "\n"
Nigel Tao52c4d6a2020-03-08 21:12:38 +1100169 "----\n"
170 "\n"
Nigel Tao0cd2f982020-03-03 23:03:02 +1100171 "jsonptr is a JSON formatter (pretty-printer) that supports the JSON\n"
Nigel Tao0291a472020-08-13 22:40:10 +1000172 "Pointer (RFC 6901) query syntax. It reads UTF-8 JSON from stdin and\n"
173 "writes canonicalized, formatted UTF-8 JSON to stdout.\n"
Nigel Tao0cd2f982020-03-03 23:03:02 +1100174 "\n"
Nigel Tao0291a472020-08-13 22:40:10 +1000175 "Canonicalized means that e.g. \"abc\\u000A\\tx\\u0177z\" is re-written\n"
176 "as \"abc\\n\\txÅ·z\". It does not sort object keys, nor does it reject\n"
Nigel Tao01abc842020-03-06 21:42:33 +1100177 "duplicate keys. Canonicalization does not imply Unicode normalization.\n"
Nigel Tao0cd2f982020-03-03 23:03:02 +1100178 "\n"
179 "Formatted means that arrays' and objects' elements are indented, each\n"
Nigel Taoecadf722020-07-13 08:22:34 +1000180 "on its own line. Configure this with the -c / -compact-output, -s=NUM /\n"
Nigel Tao0291a472020-08-13 22:40:10 +1000181 "-spaces=NUM (for NUM ranging from 0 to 8) and -t / -tabs flags.\n"
Nigel Tao168f60a2020-07-14 13:19:33 +1000182 "\n"
Nigel Tao0291a472020-08-13 22:40:10 +1000183 "The -input-allow-comments flag allows \"/*slash-star*/\" and\n"
184 "\"//slash-slash\" C-style comments within JSON input. Such comments are\n"
Nigel Tao21042052020-08-19 23:13:54 +1000185 "stripped from the output unless -output-comments was also set.\n"
Nigel Tao0cd2f982020-03-03 23:03:02 +1100186 "\n"
Nigel Tao0291a472020-08-13 22:40:10 +1000187 "The -input-allow-extra-comma flag allows input like \"[1,2,]\", with a\n"
188 "comma after the final element of a JSON list or dictionary.\n"
Nigel Tao3c8589b2020-07-19 21:49:00 +1000189 "\n"
Nigel Tao0291a472020-08-13 22:40:10 +1000190 "The -input-allow-inf-nan-numbers flag allows non-finite floating point\n"
Nigel Tao75682542020-08-22 21:40:18 +1000191 "numbers (infinities and not-a-numbers) within JSON input. This flag\n"
192 "requires that -output-inf-nan-numbers also be set.\n"
Nigel Tao3c8589b2020-07-19 21:49:00 +1000193 "\n"
Nigel Tao21042052020-08-19 23:13:54 +1000194 "The -output-comments flag copies any input comments to the output. It\n"
195 "has no effect unless -input-allow-comments was also set. Comments look\n"
196 "better after commas than before them, but a closing \"]\" or \"}\" can\n"
197 "occur after arbitrarily many comments, so -output-comments also requires\n"
198 "that one or both of -compact-output and -output-extra-comma be set.\n"
199 "\n"
Nigel Tao0291a472020-08-13 22:40:10 +1000200 "The -output-extra-comma flag writes output like \"[1,2,]\", with a comma\n"
201 "after the final element of a JSON list or dictionary. Such commas are\n"
202 "non-compliant with the JSON specification but many parsers accept them\n"
203 "and they can produce simpler line-based diffs. This flag is ignored when\n"
204 "-compact-output is set.\n"
Nigel Taof8dfc762020-07-23 23:35:44 +1000205 "\n"
Nigel Tao21042052020-08-19 23:13:54 +1000206 "The -jwcc flag (JSON With Commas and Comments) enables all of:\n"
207 " -input-allow-comments\n"
208 " -input-allow-extra-comma\n"
209 " -output-comments\n"
210 " -output-extra-comma\n"
211 "\n"
Nigel Tao52c4d6a2020-03-08 21:12:38 +1100212 "----\n"
213 "\n"
214 "The -q=STR or -query=STR flag gives an optional JSON Pointer query, to\n"
Nigel Taofe0cbbd2020-03-05 22:01:30 +1100215 "print a subset of the input. For example, given RFC 6901 section 5's\n"
Nigel Tao01abc842020-03-06 21:42:33 +1100216 "sample input (https://tools.ietf.org/rfc/rfc6901.txt), this command:\n"
217 " jsonptr -query=/foo/1 rfc-6901-json-pointer.json\n"
Nigel Tao0cd2f982020-03-03 23:03:02 +1100218 "will print:\n"
219 " \"baz\"\n"
220 "\n"
221 "An absent query is equivalent to the empty query, which identifies the\n"
Nigel Tao52c4d6a2020-03-08 21:12:38 +1100222 "entire input (the root value). Unlike a file system, the \"/\" query\n"
Nigel Taod0b16cb2020-03-14 10:15:54 +1100223 "does not identify the root. Instead, \"\" is the root and \"/\" is the\n"
224 "child (the value in a key-value pair) of the root whose key is the empty\n"
225 "string. Similarly, \"/xyz\" and \"/xyz/\" are two different nodes.\n"
Nigel Tao0cd2f982020-03-03 23:03:02 +1100226 "\n"
Nigel Tao0291a472020-08-13 22:40:10 +1000227 "If the query found a valid JSON value, this program will return a zero\n"
228 "exit code even if the rest of the input isn't valid JSON. If the query\n"
Nigel Tao0cd2f982020-03-03 23:03:02 +1100229 "did not find a value, or found an invalid one, this program returns a\n"
230 "non-zero exit code, but may still print partial output to stdout.\n"
231 "\n"
Nigel Tao0291a472020-08-13 22:40:10 +1000232 "The JSON specification (https://json.org/) permits implementations that\n"
233 "allow duplicate keys, as this one does. This JSON Pointer implementation\n"
234 "is also greedy, following the first match for each fragment without\n"
235 "back-tracking. For example, the \"/foo/bar\" query will fail if the root\n"
236 "object has multiple \"foo\" children but the first one doesn't have a\n"
237 "\"bar\" child, even if later ones do.\n"
Nigel Taofe0cbbd2020-03-05 22:01:30 +1100238 "\n"
Nigel Taoecadf722020-07-13 08:22:34 +1000239 "The -strict-json-pointer-syntax flag restricts the -query=STR string to\n"
240 "exactly RFC 6901, with only two escape sequences: \"~0\" and \"~1\" for\n"
241 "\"~\" and \"/\". Without this flag, this program also lets \"~n\" and\n"
242 "\"~r\" escape the New Line and Carriage Return ASCII control characters,\n"
243 "which can work better with line oriented Unix tools that assume exactly\n"
244 "one value (i.e. one JSON Pointer string) per line.\n"
Nigel Taod6fdfb12020-03-11 12:24:14 +1100245 "\n"
Nigel Tao52c4d6a2020-03-08 21:12:38 +1100246 "----\n"
247 "\n"
Nigel Tao94440cf2020-04-02 22:28:24 +1100248 "The -d=NUM or -max-output-depth=NUM flag gives the maximum (inclusive)\n"
Nigel Tao0291a472020-08-13 22:40:10 +1000249 "output depth. JSON containers ([] arrays and {} objects) can hold other\n"
250 "containers. When this flag is set, containers at depth NUM are replaced\n"
251 "with \"[…]\" or \"{…}\". A bare -d or -max-output-depth is equivalent to\n"
252 "-d=1. The flag's absence is equivalent to an unlimited output depth.\n"
Nigel Tao52c4d6a2020-03-08 21:12:38 +1100253 "\n"
254 "The -max-output-depth flag only affects the program's output. It doesn't\n"
Nigel Tao0291a472020-08-13 22:40:10 +1000255 "affect whether or not the input is considered valid JSON. The JSON\n"
256 "specification permits implementations to set their own maximum input\n"
257 "depth. This JSON implementation sets it to 1024.\n"
Nigel Tao52c4d6a2020-03-08 21:12:38 +1100258 "\n"
259 "Depth is measured in terms of nested containers. It is unaffected by the\n"
260 "number of spaces or tabs used to indent.\n"
261 "\n"
262 "When both -max-output-depth and -query are set, the output depth is\n"
263 "measured from when the query resolves, not from the input root. The\n"
264 "input depth (measured from the root) is still limited to 1024.\n"
265 "\n"
266 "----\n"
267 "\n"
Nigel Taofe0cbbd2020-03-05 22:01:30 +1100268 "The -fail-if-unsandboxed flag causes the program to exit if it does not\n"
269 "self-impose a sandbox. On Linux, it self-imposes a SECCOMP_MODE_STRICT\n"
Nigel Tao52c4d6a2020-03-08 21:12:38 +1100270 "sandbox, regardless of whether this flag was set.";
Nigel Tao0cd2f982020-03-03 23:03:02 +1100271
Nigel Tao2cf76db2020-02-27 22:42:01 +1100272// ----
273
Nigel Tao63441812020-08-21 14:05:48 +1000274// ascii_escapes was created by script/print-json-ascii-escapes.go.
275const uint8_t ascii_escapes[1024] = {
276 0x06, 0x5C, 0x75, 0x30, 0x30, 0x30, 0x30, 0x00, // 0x00: "\\u0000"
277 0x06, 0x5C, 0x75, 0x30, 0x30, 0x30, 0x31, 0x00, // 0x01: "\\u0001"
278 0x06, 0x5C, 0x75, 0x30, 0x30, 0x30, 0x32, 0x00, // 0x02: "\\u0002"
279 0x06, 0x5C, 0x75, 0x30, 0x30, 0x30, 0x33, 0x00, // 0x03: "\\u0003"
280 0x06, 0x5C, 0x75, 0x30, 0x30, 0x30, 0x34, 0x00, // 0x04: "\\u0004"
281 0x06, 0x5C, 0x75, 0x30, 0x30, 0x30, 0x35, 0x00, // 0x05: "\\u0005"
282 0x06, 0x5C, 0x75, 0x30, 0x30, 0x30, 0x36, 0x00, // 0x06: "\\u0006"
283 0x06, 0x5C, 0x75, 0x30, 0x30, 0x30, 0x37, 0x00, // 0x07: "\\u0007"
284 0x02, 0x5C, 0x62, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x08: "\\b"
285 0x02, 0x5C, 0x74, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x09: "\\t"
286 0x02, 0x5C, 0x6E, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x0A: "\\n"
287 0x06, 0x5C, 0x75, 0x30, 0x30, 0x30, 0x42, 0x00, // 0x0B: "\\u000B"
288 0x02, 0x5C, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x0C: "\\f"
289 0x02, 0x5C, 0x72, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x0D: "\\r"
290 0x06, 0x5C, 0x75, 0x30, 0x30, 0x30, 0x45, 0x00, // 0x0E: "\\u000E"
291 0x06, 0x5C, 0x75, 0x30, 0x30, 0x30, 0x46, 0x00, // 0x0F: "\\u000F"
292 0x06, 0x5C, 0x75, 0x30, 0x30, 0x31, 0x30, 0x00, // 0x10: "\\u0010"
293 0x06, 0x5C, 0x75, 0x30, 0x30, 0x31, 0x31, 0x00, // 0x11: "\\u0011"
294 0x06, 0x5C, 0x75, 0x30, 0x30, 0x31, 0x32, 0x00, // 0x12: "\\u0012"
295 0x06, 0x5C, 0x75, 0x30, 0x30, 0x31, 0x33, 0x00, // 0x13: "\\u0013"
296 0x06, 0x5C, 0x75, 0x30, 0x30, 0x31, 0x34, 0x00, // 0x14: "\\u0014"
297 0x06, 0x5C, 0x75, 0x30, 0x30, 0x31, 0x35, 0x00, // 0x15: "\\u0015"
298 0x06, 0x5C, 0x75, 0x30, 0x30, 0x31, 0x36, 0x00, // 0x16: "\\u0016"
299 0x06, 0x5C, 0x75, 0x30, 0x30, 0x31, 0x37, 0x00, // 0x17: "\\u0017"
300 0x06, 0x5C, 0x75, 0x30, 0x30, 0x31, 0x38, 0x00, // 0x18: "\\u0018"
301 0x06, 0x5C, 0x75, 0x30, 0x30, 0x31, 0x39, 0x00, // 0x19: "\\u0019"
302 0x06, 0x5C, 0x75, 0x30, 0x30, 0x31, 0x41, 0x00, // 0x1A: "\\u001A"
303 0x06, 0x5C, 0x75, 0x30, 0x30, 0x31, 0x42, 0x00, // 0x1B: "\\u001B"
304 0x06, 0x5C, 0x75, 0x30, 0x30, 0x31, 0x43, 0x00, // 0x1C: "\\u001C"
305 0x06, 0x5C, 0x75, 0x30, 0x30, 0x31, 0x44, 0x00, // 0x1D: "\\u001D"
306 0x06, 0x5C, 0x75, 0x30, 0x30, 0x31, 0x45, 0x00, // 0x1E: "\\u001E"
307 0x06, 0x5C, 0x75, 0x30, 0x30, 0x31, 0x46, 0x00, // 0x1F: "\\u001F"
308 0x06, 0x5C, 0x75, 0x30, 0x30, 0x32, 0x30, 0x00, // 0x20: "\\u0020"
309 0x01, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x21: "!"
310 0x02, 0x5C, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x22: "\\\""
311 0x01, 0x23, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x23: "#"
312 0x01, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x24: "$"
313 0x01, 0x25, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x25: "%"
314 0x01, 0x26, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x26: "&"
315 0x01, 0x27, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x27: "'"
316 0x01, 0x28, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x28: "("
317 0x01, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x29: ")"
318 0x01, 0x2A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x2A: "*"
319 0x01, 0x2B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x2B: "+"
320 0x01, 0x2C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x2C: ","
321 0x01, 0x2D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x2D: "-"
322 0x01, 0x2E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x2E: "."
323 0x01, 0x2F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x2F: "/"
324 0x01, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x30: "0"
325 0x01, 0x31, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x31: "1"
326 0x01, 0x32, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x32: "2"
327 0x01, 0x33, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x33: "3"
328 0x01, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x34: "4"
329 0x01, 0x35, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x35: "5"
330 0x01, 0x36, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x36: "6"
331 0x01, 0x37, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x37: "7"
332 0x01, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x38: "8"
333 0x01, 0x39, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x39: "9"
334 0x01, 0x3A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x3A: ":"
335 0x01, 0x3B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x3B: ";"
336 0x01, 0x3C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x3C: "<"
337 0x01, 0x3D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x3D: "="
338 0x01, 0x3E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x3E: ">"
339 0x01, 0x3F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x3F: "?"
340 0x01, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x40: "@"
341 0x01, 0x41, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x41: "A"
342 0x01, 0x42, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x42: "B"
343 0x01, 0x43, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x43: "C"
344 0x01, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x44: "D"
345 0x01, 0x45, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x45: "E"
346 0x01, 0x46, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x46: "F"
347 0x01, 0x47, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x47: "G"
348 0x01, 0x48, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x48: "H"
349 0x01, 0x49, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x49: "I"
350 0x01, 0x4A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x4A: "J"
351 0x01, 0x4B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x4B: "K"
352 0x01, 0x4C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x4C: "L"
353 0x01, 0x4D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x4D: "M"
354 0x01, 0x4E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x4E: "N"
355 0x01, 0x4F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x4F: "O"
356 0x01, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x50: "P"
357 0x01, 0x51, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x51: "Q"
358 0x01, 0x52, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x52: "R"
359 0x01, 0x53, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x53: "S"
360 0x01, 0x54, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x54: "T"
361 0x01, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x55: "U"
362 0x01, 0x56, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x56: "V"
363 0x01, 0x57, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x57: "W"
364 0x01, 0x58, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x58: "X"
365 0x01, 0x59, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x59: "Y"
366 0x01, 0x5A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x5A: "Z"
367 0x01, 0x5B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x5B: "["
368 0x02, 0x5C, 0x5C, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x5C: "\\\\"
369 0x01, 0x5D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x5D: "]"
370 0x01, 0x5E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x5E: "^"
371 0x01, 0x5F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x5F: "_"
372 0x01, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x60: "`"
373 0x01, 0x61, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x61: "a"
374 0x01, 0x62, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x62: "b"
375 0x01, 0x63, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x63: "c"
376 0x01, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x64: "d"
377 0x01, 0x65, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x65: "e"
378 0x01, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x66: "f"
379 0x01, 0x67, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x67: "g"
380 0x01, 0x68, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x68: "h"
381 0x01, 0x69, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x69: "i"
382 0x01, 0x6A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x6A: "j"
383 0x01, 0x6B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x6B: "k"
384 0x01, 0x6C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x6C: "l"
385 0x01, 0x6D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x6D: "m"
386 0x01, 0x6E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x6E: "n"
387 0x01, 0x6F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x6F: "o"
388 0x01, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x70: "p"
389 0x01, 0x71, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x71: "q"
390 0x01, 0x72, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x72: "r"
391 0x01, 0x73, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x73: "s"
392 0x01, 0x74, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x74: "t"
393 0x01, 0x75, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x75: "u"
394 0x01, 0x76, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x76: "v"
395 0x01, 0x77, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x77: "w"
396 0x01, 0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x78: "x"
397 0x01, 0x79, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x79: "y"
398 0x01, 0x7A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x7A: "z"
399 0x01, 0x7B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x7B: "{"
400 0x01, 0x7C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x7C: "|"
401 0x01, 0x7D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x7D: "}"
402 0x01, 0x7E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x7E: "~"
403 0x01, 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x7F: "<DEL>"
404};
405
Nigel Taof3146c22020-03-26 08:47:42 +1100406// Wuffs allows either statically or dynamically allocated work buffers. This
407// program exercises static allocation.
408#define WORK_BUFFER_ARRAY_SIZE \
409 WUFFS_JSON__DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE
410#if WORK_BUFFER_ARRAY_SIZE > 0
Nigel Taod60815c2020-03-26 14:32:35 +1100411uint8_t g_work_buffer_array[WORK_BUFFER_ARRAY_SIZE];
Nigel Taof3146c22020-03-26 08:47:42 +1100412#else
413// Not all C/C++ compilers support 0-length arrays.
Nigel Taod60815c2020-03-26 14:32:35 +1100414uint8_t g_work_buffer_array[1];
Nigel Taof3146c22020-03-26 08:47:42 +1100415#endif
416
Nigel Taod60815c2020-03-26 14:32:35 +1100417bool g_sandboxed = false;
Nigel Taofe0cbbd2020-03-05 22:01:30 +1100418
Nigel Taod60815c2020-03-26 14:32:35 +1100419int g_input_file_descriptor = 0; // A 0 default means stdin.
Nigel Tao01abc842020-03-06 21:42:33 +1100420
Nigel Tao0a0c7d62020-08-18 23:31:27 +1000421#define NEW_LINE_THEN_256_SPACES \
422 "\n " \
423 " " \
424 " " \
425 " "
426#define NEW_LINE_THEN_256_TABS \
427 "\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" \
428 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" \
429 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" \
430 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" \
431 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" \
432 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" \
433 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
434
435const char* g_new_line_then_256_indent_bytes;
436uint32_t g_bytes_per_indent_depth;
Nigel Tao107f0ef2020-03-01 21:35:02 +1100437
Nigel Taofdac24a2020-03-06 21:53:08 +1100438#ifndef DST_BUFFER_ARRAY_SIZE
439#define DST_BUFFER_ARRAY_SIZE (32 * 1024)
Nigel Tao1b073492020-02-16 22:11:36 +1100440#endif
Nigel Taofdac24a2020-03-06 21:53:08 +1100441#ifndef SRC_BUFFER_ARRAY_SIZE
442#define SRC_BUFFER_ARRAY_SIZE (32 * 1024)
Nigel Tao1b073492020-02-16 22:11:36 +1100443#endif
Nigel Tao63e67962020-08-26 00:00:32 +1000444// 1 token is 8 bytes. 4Ki tokens is 32KiB.
Nigel Taofdac24a2020-03-06 21:53:08 +1100445#ifndef TOKEN_BUFFER_ARRAY_SIZE
446#define TOKEN_BUFFER_ARRAY_SIZE (4 * 1024)
Nigel Tao1b073492020-02-16 22:11:36 +1100447#endif
448
Nigel Taod60815c2020-03-26 14:32:35 +1100449uint8_t g_dst_array[DST_BUFFER_ARRAY_SIZE];
450uint8_t g_src_array[SRC_BUFFER_ARRAY_SIZE];
451wuffs_base__token g_tok_array[TOKEN_BUFFER_ARRAY_SIZE];
Nigel Tao1b073492020-02-16 22:11:36 +1100452
Nigel Taod60815c2020-03-26 14:32:35 +1100453wuffs_base__io_buffer g_dst;
454wuffs_base__io_buffer g_src;
455wuffs_base__token_buffer g_tok;
Nigel Tao1b073492020-02-16 22:11:36 +1100456
Nigel Tao991bd512020-08-19 09:38:16 +1000457// g_cursor_index is the g_src.data.ptr index between the previous and current
458// token. An invariant is that (g_cursor_index <= g_src.meta.ri).
459size_t g_cursor_index;
Nigel Tao2cf76db2020-02-27 22:42:01 +1100460
Nigel Taod60815c2020-03-26 14:32:35 +1100461uint32_t g_depth;
Nigel Tao2cf76db2020-02-27 22:42:01 +1100462
463enum class context {
464 none,
465 in_list_after_bracket,
466 in_list_after_value,
467 in_dict_after_brace,
468 in_dict_after_key,
469 in_dict_after_value,
Nigel Taod60815c2020-03-26 14:32:35 +1100470} g_ctx;
Nigel Tao2cf76db2020-02-27 22:42:01 +1100471
Nigel Tao0cd2f982020-03-03 23:03:02 +1100472bool //
473in_dict_before_key() {
Nigel Taod60815c2020-03-26 14:32:35 +1100474 return (g_ctx == context::in_dict_after_brace) ||
475 (g_ctx == context::in_dict_after_value);
Nigel Tao0cd2f982020-03-03 23:03:02 +1100476}
477
Nigel Tao21042052020-08-19 23:13:54 +1000478bool g_is_after_comment;
479
Nigel Taod60815c2020-03-26 14:32:35 +1100480uint32_t g_suppress_write_dst;
481bool g_wrote_to_dst;
Nigel Tao0cd2f982020-03-03 23:03:02 +1100482
Nigel Tao0291a472020-08-13 22:40:10 +1000483wuffs_json__decoder g_dec;
Nigel Taoea532452020-07-27 00:03:00 +1000484
Nigel Tao0cd2f982020-03-03 23:03:02 +1100485// ----
486
487// Query is a JSON Pointer query. After initializing with a NUL-terminated C
488// string, its multiple fragments are consumed as the program walks the JSON
489// data from stdin. For example, letting "$" denote a NUL, suppose that we
490// started with a query string of "/apple/banana/12/durian" and are currently
Nigel Taob48ee752020-03-13 09:27:33 +1100491// trying to match the second fragment, "banana", so that Query::m_depth is 2:
Nigel Tao0cd2f982020-03-03 23:03:02 +1100492//
493// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
494// / a p p l e / b a n a n a / 1 2 / d u r i a n $
495// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
496// ^ ^
Nigel Taob48ee752020-03-13 09:27:33 +1100497// m_frag_i m_frag_k
Nigel Tao0cd2f982020-03-03 23:03:02 +1100498//
Nigel Taob48ee752020-03-13 09:27:33 +1100499// The two pointers m_frag_i and m_frag_k (abbreviated as mfi and mfk) are the
500// start (inclusive) and end (exclusive) of the query fragment. They satisfy
501// (mfi <= mfk) and may be equal if the fragment empty (note that "" is a valid
502// JSON object key).
Nigel Tao0cd2f982020-03-03 23:03:02 +1100503//
Nigel Taob48ee752020-03-13 09:27:33 +1100504// The m_frag_j (mfj) pointer moves between these two, or is nullptr. An
505// invariant is that (((mfi <= mfj) && (mfj <= mfk)) || (mfj == nullptr)).
Nigel Tao0cd2f982020-03-03 23:03:02 +1100506//
507// Wuffs' JSON tokenizer can portray a single JSON string as multiple Wuffs
508// tokens, as backslash-escaped values within that JSON string may each get
509// their own token.
510//
Nigel Taob48ee752020-03-13 09:27:33 +1100511// At the start of each object key (a JSON string), mfj is set to mfi.
Nigel Tao0cd2f982020-03-03 23:03:02 +1100512//
Nigel Taob48ee752020-03-13 09:27:33 +1100513// While mfj remains non-nullptr, each token's unescaped contents are then
514// compared to that part of the fragment from mfj to mfk. If it is a prefix
515// (including the case of an exact match), then mfj is advanced by the
516// unescaped length. Otherwise, mfj is set to nullptr.
Nigel Tao0cd2f982020-03-03 23:03:02 +1100517//
518// Comparison accounts for JSON Pointer's escaping notation: "~0" and "~1" in
519// the query (not the JSON value) are unescaped to "~" and "/" respectively.
Nigel Taob48ee752020-03-13 09:27:33 +1100520// "~n" and "~r" are also unescaped to "\n" and "\r". The program is
521// responsible for calling Query::validate (with a strict_json_pointer_syntax
522// argument) before otherwise using this class.
Nigel Tao0cd2f982020-03-03 23:03:02 +1100523//
Nigel Taob48ee752020-03-13 09:27:33 +1100524// The mfj pointer therefore advances from mfi to mfk, or drops out, as we
525// incrementally match the object key with the query fragment. For example, if
526// we have already matched the "ban" of "banana", then we would accept any of
527// an "ana" token, an "a" token or a "\u0061" token, amongst others. They would
528// advance mfj by 3, 1 or 1 bytes respectively.
Nigel Tao0cd2f982020-03-03 23:03:02 +1100529//
Nigel Taob48ee752020-03-13 09:27:33 +1100530// mfj
Nigel Tao0cd2f982020-03-03 23:03:02 +1100531// v
532// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
533// / a p p l e / b a n a n a / 1 2 / d u r i a n $
534// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
535// ^ ^
Nigel Taob48ee752020-03-13 09:27:33 +1100536// mfi mfk
Nigel Tao0cd2f982020-03-03 23:03:02 +1100537//
538// At the end of each object key (or equivalently, at the start of each object
Nigel Taob48ee752020-03-13 09:27:33 +1100539// value), if mfj is non-nullptr and equal to (but not less than) mfk then we
540// have a fragment match: the query fragment equals the object key. If there is
541// a next fragment (in this example, "12") we move the frag_etc pointers to its
542// start and end and increment Query::m_depth. Otherwise, we have matched the
543// complete query, and the upcoming JSON value is the result of that query.
Nigel Tao0cd2f982020-03-03 23:03:02 +1100544//
545// The discussion above centers on object keys. If the query fragment is
546// numeric then it can also match as an array index: the string fragment "12"
547// will match an array's 13th element (starting counting from zero). See RFC
548// 6901 for its precise definition of an "array index" number.
549//
Nigel Taob48ee752020-03-13 09:27:33 +1100550// Array index fragment match is represented by the Query::m_array_index field,
Nigel Tao0cd2f982020-03-03 23:03:02 +1100551// whose type (wuffs_base__result_u64) is a result type. An error result means
552// that the fragment is not an array index. A value result holds the number of
553// list elements remaining. When matching a query fragment in an array (instead
554// of in an object), each element ticks this number down towards zero. At zero,
555// the upcoming JSON value is the one that matches the query fragment.
556class Query {
557 private:
Nigel Taob48ee752020-03-13 09:27:33 +1100558 uint8_t* m_frag_i;
559 uint8_t* m_frag_j;
560 uint8_t* m_frag_k;
Nigel Tao0cd2f982020-03-03 23:03:02 +1100561
Nigel Taob48ee752020-03-13 09:27:33 +1100562 uint32_t m_depth;
Nigel Tao0cd2f982020-03-03 23:03:02 +1100563
Nigel Taob48ee752020-03-13 09:27:33 +1100564 wuffs_base__result_u64 m_array_index;
Nigel Tao0cd2f982020-03-03 23:03:02 +1100565
566 public:
567 void reset(char* query_c_string) {
Nigel Taob48ee752020-03-13 09:27:33 +1100568 m_frag_i = (uint8_t*)query_c_string;
569 m_frag_j = (uint8_t*)query_c_string;
570 m_frag_k = (uint8_t*)query_c_string;
571 m_depth = 0;
572 m_array_index.status.repr = "#main: not an array index query fragment";
573 m_array_index.value = 0;
Nigel Tao0cd2f982020-03-03 23:03:02 +1100574 }
575
Nigel Taob48ee752020-03-13 09:27:33 +1100576 void restart_fragment(bool enable) { m_frag_j = enable ? m_frag_i : nullptr; }
Nigel Tao0cd2f982020-03-03 23:03:02 +1100577
Nigel Taob48ee752020-03-13 09:27:33 +1100578 bool is_at(uint32_t depth) { return m_depth == depth; }
Nigel Tao0cd2f982020-03-03 23:03:02 +1100579
580 // tick returns whether the fragment is a valid array index whose value is
581 // zero. If valid but non-zero, it decrements it and returns false.
582 bool tick() {
Nigel Taob48ee752020-03-13 09:27:33 +1100583 if (m_array_index.status.is_ok()) {
Nigel Tao0291a472020-08-13 22:40:10 +1000584 if (m_array_index.value == 0) {
Nigel Tao0cd2f982020-03-03 23:03:02 +1100585 return true;
586 }
Nigel Tao0291a472020-08-13 22:40:10 +1000587 m_array_index.value--;
Nigel Tao0cd2f982020-03-03 23:03:02 +1100588 }
589 return false;
590 }
591
592 // next_fragment moves to the next fragment, returning whether it existed.
593 bool next_fragment() {
Nigel Taob48ee752020-03-13 09:27:33 +1100594 uint8_t* k = m_frag_k;
595 uint32_t d = m_depth;
Nigel Tao0cd2f982020-03-03 23:03:02 +1100596
597 this->reset(nullptr);
598
599 if (!k || (*k != '/')) {
600 return false;
601 }
602 k++;
603
604 bool all_digits = true;
605 uint8_t* i = k;
606 while ((*k != '\x00') && (*k != '/')) {
607 all_digits = all_digits && ('0' <= *k) && (*k <= '9');
608 k++;
609 }
Nigel Taob48ee752020-03-13 09:27:33 +1100610 m_frag_i = i;
611 m_frag_j = i;
612 m_frag_k = k;
613 m_depth = d + 1;
Nigel Tao0cd2f982020-03-03 23:03:02 +1100614 if (all_digits) {
615 // wuffs_base__parse_number_u64 rejects leading zeroes, e.g. "00", "07".
Nigel Tao6b7ce302020-07-07 16:19:46 +1000616 m_array_index = wuffs_base__parse_number_u64(
617 wuffs_base__make_slice_u8(i, k - i),
618 WUFFS_BASE__PARSE_NUMBER_XXX__DEFAULT_OPTIONS);
Nigel Tao0cd2f982020-03-03 23:03:02 +1100619 }
620 return true;
621 }
622
Nigel Taob48ee752020-03-13 09:27:33 +1100623 bool matched_all() { return m_frag_k == nullptr; }
Nigel Tao52c4d6a2020-03-08 21:12:38 +1100624
Nigel Taob48ee752020-03-13 09:27:33 +1100625 bool matched_fragment() { return m_frag_j && (m_frag_j == m_frag_k); }
Nigel Tao0cd2f982020-03-03 23:03:02 +1100626
627 void incremental_match_slice(uint8_t* ptr, size_t len) {
Nigel Taob48ee752020-03-13 09:27:33 +1100628 if (!m_frag_j) {
Nigel Tao0cd2f982020-03-03 23:03:02 +1100629 return;
630 }
Nigel Taob48ee752020-03-13 09:27:33 +1100631 uint8_t* j = m_frag_j;
Nigel Tao0cd2f982020-03-03 23:03:02 +1100632 while (true) {
633 if (len == 0) {
Nigel Taob48ee752020-03-13 09:27:33 +1100634 m_frag_j = j;
Nigel Tao0cd2f982020-03-03 23:03:02 +1100635 return;
636 }
637
638 if (*j == '\x00') {
639 break;
640
641 } else if (*j == '~') {
642 j++;
643 if (*j == '0') {
644 if (*ptr != '~') {
645 break;
646 }
647 } else if (*j == '1') {
648 if (*ptr != '/') {
649 break;
650 }
Nigel Taod6fdfb12020-03-11 12:24:14 +1100651 } else if (*j == 'n') {
652 if (*ptr != '\n') {
653 break;
654 }
655 } else if (*j == 'r') {
656 if (*ptr != '\r') {
657 break;
658 }
Nigel Tao0cd2f982020-03-03 23:03:02 +1100659 } else {
660 break;
661 }
662
663 } else if (*j != *ptr) {
664 break;
665 }
666
667 j++;
668 ptr++;
669 len--;
670 }
Nigel Taob48ee752020-03-13 09:27:33 +1100671 m_frag_j = nullptr;
Nigel Tao0cd2f982020-03-03 23:03:02 +1100672 }
673
674 void incremental_match_code_point(uint32_t code_point) {
Nigel Taob48ee752020-03-13 09:27:33 +1100675 if (!m_frag_j) {
Nigel Tao0cd2f982020-03-03 23:03:02 +1100676 return;
677 }
678 uint8_t u[WUFFS_BASE__UTF_8__BYTE_LENGTH__MAX_INCL];
679 size_t n = wuffs_base__utf_8__encode(
680 wuffs_base__make_slice_u8(&u[0],
681 WUFFS_BASE__UTF_8__BYTE_LENGTH__MAX_INCL),
682 code_point);
683 if (n > 0) {
684 this->incremental_match_slice(&u[0], n);
685 }
686 }
687
688 // validate returns whether the (ptr, len) arguments form a valid JSON
689 // Pointer. In particular, it must be valid UTF-8, and either be empty or
690 // start with a '/'. Any '~' within must immediately be followed by either
Nigel Taod6fdfb12020-03-11 12:24:14 +1100691 // '0' or '1'. If strict_json_pointer_syntax is false, a '~' may also be
692 // followed by either 'n' or 'r'.
693 static bool validate(char* query_c_string,
694 size_t length,
695 bool strict_json_pointer_syntax) {
Nigel Tao0cd2f982020-03-03 23:03:02 +1100696 if (length <= 0) {
697 return true;
698 }
699 if (query_c_string[0] != '/') {
700 return false;
701 }
702 wuffs_base__slice_u8 s =
703 wuffs_base__make_slice_u8((uint8_t*)query_c_string, length);
704 bool previous_was_tilde = false;
705 while (s.len > 0) {
Nigel Tao702c7b22020-07-22 15:42:54 +1000706 wuffs_base__utf_8__next__output o = wuffs_base__utf_8__next(s.ptr, s.len);
Nigel Tao0cd2f982020-03-03 23:03:02 +1100707 if (!o.is_valid()) {
708 return false;
709 }
Nigel Taod6fdfb12020-03-11 12:24:14 +1100710
711 if (previous_was_tilde) {
712 switch (o.code_point) {
713 case '0':
714 case '1':
715 break;
716 case 'n':
717 case 'r':
718 if (strict_json_pointer_syntax) {
719 return false;
720 }
721 break;
722 default:
723 return false;
724 }
Nigel Tao0cd2f982020-03-03 23:03:02 +1100725 }
726 previous_was_tilde = o.code_point == '~';
Nigel Taod6fdfb12020-03-11 12:24:14 +1100727
Nigel Tao0cd2f982020-03-03 23:03:02 +1100728 s.ptr += o.byte_length;
729 s.len -= o.byte_length;
730 }
731 return !previous_was_tilde;
732 }
Nigel Taod60815c2020-03-26 14:32:35 +1100733} g_query;
Nigel Tao0cd2f982020-03-03 23:03:02 +1100734
735// ----
736
Nigel Tao68920952020-03-03 11:25:18 +1100737struct {
738 int remaining_argc;
739 char** remaining_argv;
740
Nigel Tao3690e832020-03-12 16:52:26 +1100741 bool compact_output;
Nigel Taofe0cbbd2020-03-05 22:01:30 +1100742 bool fail_if_unsandboxed;
Nigel Tao0291a472020-08-13 22:40:10 +1000743 bool input_allow_comments;
744 bool input_allow_extra_comma;
745 bool input_allow_inf_nan_numbers;
Nigel Tao21042052020-08-19 23:13:54 +1000746 bool output_comments;
Nigel Tao0291a472020-08-13 22:40:10 +1000747 bool output_extra_comma;
Nigel Tao75682542020-08-22 21:40:18 +1000748 bool output_inf_nan_numbers;
Nigel Taod6fdfb12020-03-11 12:24:14 +1100749 bool strict_json_pointer_syntax;
Nigel Tao68920952020-03-03 11:25:18 +1100750 bool tabs;
Nigel Tao0a0c7d62020-08-18 23:31:27 +1000751
752 uint32_t max_output_depth;
753 uint32_t spaces;
754
755 char* query_c_string;
Nigel Taod60815c2020-03-26 14:32:35 +1100756} g_flags = {0};
Nigel Tao68920952020-03-03 11:25:18 +1100757
758const char* //
759parse_flags(int argc, char** argv) {
Nigel Taoecadf722020-07-13 08:22:34 +1000760 g_flags.spaces = 4;
Nigel Taod60815c2020-03-26 14:32:35 +1100761 g_flags.max_output_depth = 0xFFFFFFFF;
Nigel Tao68920952020-03-03 11:25:18 +1100762
763 int c = (argc > 0) ? 1 : 0; // Skip argv[0], the program name.
764 for (; c < argc; c++) {
765 char* arg = argv[c];
766 if (*arg++ != '-') {
767 break;
768 }
769
770 // A double-dash "--foo" is equivalent to a single-dash "-foo". As special
771 // cases, a bare "-" is not a flag (some programs may interpret it as
772 // stdin) and a bare "--" means to stop parsing flags.
773 if (*arg == '\x00') {
774 break;
775 } else if (*arg == '-') {
776 arg++;
777 if (*arg == '\x00') {
778 c++;
779 break;
780 }
781 }
782
Nigel Tao3690e832020-03-12 16:52:26 +1100783 if (!strcmp(arg, "c") || !strcmp(arg, "compact-output")) {
Nigel Taod60815c2020-03-26 14:32:35 +1100784 g_flags.compact_output = true;
Nigel Tao68920952020-03-03 11:25:18 +1100785 continue;
786 }
Nigel Tao94440cf2020-04-02 22:28:24 +1100787 if (!strcmp(arg, "d") || !strcmp(arg, "max-output-depth")) {
788 g_flags.max_output_depth = 1;
789 continue;
790 } else if (!strncmp(arg, "d=", 2) ||
791 !strncmp(arg, "max-output-depth=", 16)) {
792 while (*arg++ != '=') {
793 }
794 wuffs_base__result_u64 u = wuffs_base__parse_number_u64(
Nigel Tao6b7ce302020-07-07 16:19:46 +1000795 wuffs_base__make_slice_u8((uint8_t*)arg, strlen(arg)),
796 WUFFS_BASE__PARSE_NUMBER_XXX__DEFAULT_OPTIONS);
Nigel Taoaf757722020-07-18 17:27:11 +1000797 if (u.status.is_ok() && (u.value <= 0xFFFFFFFF)) {
Nigel Tao94440cf2020-04-02 22:28:24 +1100798 g_flags.max_output_depth = (uint32_t)(u.value);
799 continue;
800 }
801 return g_usage;
802 }
Nigel Taofe0cbbd2020-03-05 22:01:30 +1100803 if (!strcmp(arg, "fail-if-unsandboxed")) {
Nigel Taod60815c2020-03-26 14:32:35 +1100804 g_flags.fail_if_unsandboxed = true;
Nigel Taofe0cbbd2020-03-05 22:01:30 +1100805 continue;
806 }
Nigel Tao0291a472020-08-13 22:40:10 +1000807 if (!strcmp(arg, "input-allow-comments")) {
808 g_flags.input_allow_comments = true;
Nigel Tao4e193592020-07-15 12:48:57 +1000809 continue;
810 }
Nigel Tao0291a472020-08-13 22:40:10 +1000811 if (!strcmp(arg, "input-allow-extra-comma")) {
812 g_flags.input_allow_extra_comma = true;
Nigel Tao4e193592020-07-15 12:48:57 +1000813 continue;
814 }
Nigel Tao0291a472020-08-13 22:40:10 +1000815 if (!strcmp(arg, "input-allow-inf-nan-numbers")) {
816 g_flags.input_allow_inf_nan_numbers = true;
Nigel Tao3c8589b2020-07-19 21:49:00 +1000817 continue;
818 }
Nigel Tao21042052020-08-19 23:13:54 +1000819 if (!strcmp(arg, "jwcc")) {
820 g_flags.input_allow_comments = true;
821 g_flags.input_allow_extra_comma = true;
822 g_flags.output_comments = true;
823 g_flags.output_extra_comma = true;
824 continue;
825 }
826 if (!strcmp(arg, "output-comments")) {
827 g_flags.output_comments = true;
828 continue;
829 }
Nigel Tao0291a472020-08-13 22:40:10 +1000830 if (!strcmp(arg, "output-extra-comma")) {
831 g_flags.output_extra_comma = true;
Nigel Taodd114692020-07-25 21:54:12 +1000832 continue;
833 }
Nigel Tao75682542020-08-22 21:40:18 +1000834 if (!strcmp(arg, "output-inf-nan-numbers")) {
835 g_flags.output_inf_nan_numbers = true;
836 continue;
837 }
Nigel Tao0cd2f982020-03-03 23:03:02 +1100838 if (!strncmp(arg, "q=", 2) || !strncmp(arg, "query=", 6)) {
839 while (*arg++ != '=') {
840 }
Nigel Taod60815c2020-03-26 14:32:35 +1100841 g_flags.query_c_string = arg;
Nigel Taod6fdfb12020-03-11 12:24:14 +1100842 continue;
843 }
Nigel Taoecadf722020-07-13 08:22:34 +1000844 if (!strncmp(arg, "s=", 2) || !strncmp(arg, "spaces=", 7)) {
845 while (*arg++ != '=') {
846 }
847 if (('0' <= arg[0]) && (arg[0] <= '8') && (arg[1] == '\x00')) {
848 g_flags.spaces = arg[0] - '0';
849 continue;
850 }
851 return g_usage;
852 }
853 if (!strcmp(arg, "strict-json-pointer-syntax")) {
Nigel Taod60815c2020-03-26 14:32:35 +1100854 g_flags.strict_json_pointer_syntax = true;
Nigel Taod6fdfb12020-03-11 12:24:14 +1100855 continue;
Nigel Tao68920952020-03-03 11:25:18 +1100856 }
857 if (!strcmp(arg, "t") || !strcmp(arg, "tabs")) {
Nigel Taod60815c2020-03-26 14:32:35 +1100858 g_flags.tabs = true;
Nigel Tao68920952020-03-03 11:25:18 +1100859 continue;
860 }
861
Nigel Taod60815c2020-03-26 14:32:35 +1100862 return g_usage;
Nigel Tao68920952020-03-03 11:25:18 +1100863 }
864
Nigel Taod60815c2020-03-26 14:32:35 +1100865 if (g_flags.query_c_string &&
866 !Query::validate(g_flags.query_c_string, strlen(g_flags.query_c_string),
867 g_flags.strict_json_pointer_syntax)) {
Nigel Taod6fdfb12020-03-11 12:24:14 +1100868 return "main: bad JSON Pointer (RFC 6901) syntax for the -query=STR flag";
869 }
870
Nigel Taod60815c2020-03-26 14:32:35 +1100871 g_flags.remaining_argc = argc - c;
872 g_flags.remaining_argv = argv + c;
Nigel Tao0cd2f982020-03-03 23:03:02 +1100873 return nullptr;
Nigel Tao68920952020-03-03 11:25:18 +1100874}
875
Nigel Tao2cf76db2020-02-27 22:42:01 +1100876const char* //
877initialize_globals(int argc, char** argv) {
Nigel Taod60815c2020-03-26 14:32:35 +1100878 g_dst = wuffs_base__make_io_buffer(
879 wuffs_base__make_slice_u8(g_dst_array, DST_BUFFER_ARRAY_SIZE),
Nigel Tao2cf76db2020-02-27 22:42:01 +1100880 wuffs_base__empty_io_buffer_meta());
Nigel Tao1b073492020-02-16 22:11:36 +1100881
Nigel Taod60815c2020-03-26 14:32:35 +1100882 g_src = wuffs_base__make_io_buffer(
883 wuffs_base__make_slice_u8(g_src_array, SRC_BUFFER_ARRAY_SIZE),
Nigel Tao2cf76db2020-02-27 22:42:01 +1100884 wuffs_base__empty_io_buffer_meta());
885
Nigel Taod60815c2020-03-26 14:32:35 +1100886 g_tok = wuffs_base__make_token_buffer(
887 wuffs_base__make_slice_token(g_tok_array, TOKEN_BUFFER_ARRAY_SIZE),
Nigel Tao2cf76db2020-02-27 22:42:01 +1100888 wuffs_base__empty_token_buffer_meta());
889
Nigel Tao991bd512020-08-19 09:38:16 +1000890 g_cursor_index = 0;
Nigel Tao2cf76db2020-02-27 22:42:01 +1100891
Nigel Taod60815c2020-03-26 14:32:35 +1100892 g_depth = 0;
Nigel Tao2cf76db2020-02-27 22:42:01 +1100893
Nigel Taod60815c2020-03-26 14:32:35 +1100894 g_ctx = context::none;
Nigel Tao2cf76db2020-02-27 22:42:01 +1100895
Nigel Tao21042052020-08-19 23:13:54 +1000896 g_is_after_comment = false;
897
Nigel Tao68920952020-03-03 11:25:18 +1100898 TRY(parse_flags(argc, argv));
Nigel Taod60815c2020-03-26 14:32:35 +1100899 if (g_flags.fail_if_unsandboxed && !g_sandboxed) {
Nigel Taofe0cbbd2020-03-05 22:01:30 +1100900 return "main: unsandboxed";
901 }
Nigel Tao21042052020-08-19 23:13:54 +1000902 if (g_flags.output_comments && !g_flags.compact_output &&
903 !g_flags.output_extra_comma) {
904 return "main: -output-comments requires one or both of -compact-output and "
905 "-output-extra-comma";
906 }
Nigel Tao75682542020-08-22 21:40:18 +1000907 if (g_flags.input_allow_inf_nan_numbers && !g_flags.output_inf_nan_numbers) {
908 return "main: -input-allow-inf-nan-numbers requires "
909 "-output-inf-nan-numbers";
910 }
Nigel Tao01abc842020-03-06 21:42:33 +1100911 const int stdin_fd = 0;
Nigel Taod60815c2020-03-26 14:32:35 +1100912 if (g_flags.remaining_argc >
913 ((g_input_file_descriptor != stdin_fd) ? 1 : 0)) {
914 return g_usage;
Nigel Tao107f0ef2020-03-01 21:35:02 +1100915 }
916
Nigel Tao0a0c7d62020-08-18 23:31:27 +1000917 g_new_line_then_256_indent_bytes =
918 g_flags.tabs ? NEW_LINE_THEN_256_TABS : NEW_LINE_THEN_256_SPACES;
919 g_bytes_per_indent_depth = g_flags.tabs ? 1 : g_flags.spaces;
920
Nigel Taod60815c2020-03-26 14:32:35 +1100921 g_query.reset(g_flags.query_c_string);
Nigel Tao0cd2f982020-03-03 23:03:02 +1100922
Nigel Taoc96b31c2020-07-27 22:37:23 +1000923 // If the query is non-empty, suppress writing to stdout until we've
Nigel Tao0cd2f982020-03-03 23:03:02 +1100924 // completed the query.
Nigel Taod60815c2020-03-26 14:32:35 +1100925 g_suppress_write_dst = g_query.next_fragment() ? 1 : 0;
926 g_wrote_to_dst = false;
Nigel Tao0cd2f982020-03-03 23:03:02 +1100927
Nigel Tao0291a472020-08-13 22:40:10 +1000928 TRY(g_dec.initialize(sizeof__wuffs_json__decoder(), WUFFS_VERSION, 0)
929 .message());
Nigel Tao4b186b02020-03-18 14:25:21 +1100930
Nigel Tao0291a472020-08-13 22:40:10 +1000931 if (g_flags.input_allow_comments) {
932 g_dec.set_quirk_enabled(WUFFS_JSON__QUIRK_ALLOW_COMMENT_BLOCK, true);
933 g_dec.set_quirk_enabled(WUFFS_JSON__QUIRK_ALLOW_COMMENT_LINE, true);
Nigel Tao3c8589b2020-07-19 21:49:00 +1000934 }
Nigel Tao0291a472020-08-13 22:40:10 +1000935 if (g_flags.input_allow_extra_comma) {
936 g_dec.set_quirk_enabled(WUFFS_JSON__QUIRK_ALLOW_EXTRA_COMMA, true);
Nigel Taoc766bb72020-07-09 12:59:32 +1000937 }
Nigel Tao0291a472020-08-13 22:40:10 +1000938 if (g_flags.input_allow_inf_nan_numbers) {
939 g_dec.set_quirk_enabled(WUFFS_JSON__QUIRK_ALLOW_INF_NAN_NUMBERS, true);
Nigel Tao51a38292020-07-19 22:43:17 +1000940 }
Nigel Taoc766bb72020-07-09 12:59:32 +1000941
Nigel Tao4b186b02020-03-18 14:25:21 +1100942 // Consume an optional whitespace trailer. This isn't part of the JSON spec,
943 // but it works better with line oriented Unix tools (such as "echo 123 |
944 // jsonptr" where it's "echo", not "echo -n") or hand-edited JSON files which
945 // can accidentally contain trailing whitespace.
Nigel Tao0291a472020-08-13 22:40:10 +1000946 g_dec.set_quirk_enabled(WUFFS_JSON__QUIRK_ALLOW_TRAILING_NEW_LINE, true);
Nigel Tao4b186b02020-03-18 14:25:21 +1100947
948 return nullptr;
Nigel Tao2cf76db2020-02-27 22:42:01 +1100949}
Nigel Tao1b073492020-02-16 22:11:36 +1100950
951// ----
952
Nigel Taofe0cbbd2020-03-05 22:01:30 +1100953// ignore_return_value suppresses errors from -Wall -Werror.
954static void //
955ignore_return_value(int ignored) {}
956
Nigel Tao2914bae2020-02-26 09:40:30 +1100957const char* //
958read_src() {
Nigel Taod60815c2020-03-26 14:32:35 +1100959 if (g_src.meta.closed) {
Nigel Tao9cc2c252020-02-23 17:05:49 +1100960 return "main: internal error: read requested on a closed source";
Nigel Taoa8406922020-02-19 12:22:00 +1100961 }
Nigel Taod60815c2020-03-26 14:32:35 +1100962 g_src.compact();
963 if (g_src.meta.wi >= g_src.data.len) {
964 return "main: g_src buffer is full";
Nigel Tao1b073492020-02-16 22:11:36 +1100965 }
Nigel Taofe0cbbd2020-03-05 22:01:30 +1100966 while (true) {
Nigel Taod6a10df2020-07-27 11:47:47 +1000967 ssize_t n = read(g_input_file_descriptor, g_src.writer_pointer(),
968 g_src.writer_length());
Nigel Taofe0cbbd2020-03-05 22:01:30 +1100969 if (n >= 0) {
Nigel Taod60815c2020-03-26 14:32:35 +1100970 g_src.meta.wi += n;
971 g_src.meta.closed = n == 0;
Nigel Taofe0cbbd2020-03-05 22:01:30 +1100972 break;
973 } else if (errno != EINTR) {
974 return strerror(errno);
975 }
Nigel Tao1b073492020-02-16 22:11:36 +1100976 }
977 return nullptr;
978}
979
Nigel Tao2914bae2020-02-26 09:40:30 +1100980const char* //
981flush_dst() {
Nigel Taofe0cbbd2020-03-05 22:01:30 +1100982 while (true) {
Nigel Taod6a10df2020-07-27 11:47:47 +1000983 size_t n = g_dst.reader_length();
Nigel Taofe0cbbd2020-03-05 22:01:30 +1100984 if (n == 0) {
985 break;
Nigel Tao1b073492020-02-16 22:11:36 +1100986 }
Nigel Taofe0cbbd2020-03-05 22:01:30 +1100987 const int stdout_fd = 1;
Nigel Taod6a10df2020-07-27 11:47:47 +1000988 ssize_t i = write(stdout_fd, g_dst.reader_pointer(), n);
Nigel Taofe0cbbd2020-03-05 22:01:30 +1100989 if (i >= 0) {
Nigel Taod60815c2020-03-26 14:32:35 +1100990 g_dst.meta.ri += i;
Nigel Taofe0cbbd2020-03-05 22:01:30 +1100991 } else if (errno != EINTR) {
992 return strerror(errno);
993 }
Nigel Tao1b073492020-02-16 22:11:36 +1100994 }
Nigel Taod60815c2020-03-26 14:32:35 +1100995 g_dst.compact();
Nigel Tao1b073492020-02-16 22:11:36 +1100996 return nullptr;
997}
998
Nigel Tao2914bae2020-02-26 09:40:30 +1100999const char* //
Nigel Tao6b86cbc2020-08-19 11:39:56 +10001000write_dst_slow(const void* s, size_t n) {
Nigel Tao1b073492020-02-16 22:11:36 +11001001 const uint8_t* p = static_cast<const uint8_t*>(s);
1002 while (n > 0) {
Nigel Taod6a10df2020-07-27 11:47:47 +10001003 size_t i = g_dst.writer_length();
Nigel Tao1b073492020-02-16 22:11:36 +11001004 if (i == 0) {
1005 const char* z = flush_dst();
1006 if (z) {
1007 return z;
1008 }
Nigel Taod6a10df2020-07-27 11:47:47 +10001009 i = g_dst.writer_length();
Nigel Tao1b073492020-02-16 22:11:36 +11001010 if (i == 0) {
Nigel Taod60815c2020-03-26 14:32:35 +11001011 return "main: g_dst buffer is full";
Nigel Tao1b073492020-02-16 22:11:36 +11001012 }
1013 }
1014
1015 if (i > n) {
1016 i = n;
1017 }
Nigel Taod60815c2020-03-26 14:32:35 +11001018 memcpy(g_dst.data.ptr + g_dst.meta.wi, p, i);
1019 g_dst.meta.wi += i;
Nigel Tao1b073492020-02-16 22:11:36 +11001020 p += i;
1021 n -= i;
Nigel Taod60815c2020-03-26 14:32:35 +11001022 g_wrote_to_dst = true;
Nigel Tao1b073492020-02-16 22:11:36 +11001023 }
1024 return nullptr;
1025}
1026
Nigel Tao6b86cbc2020-08-19 11:39:56 +10001027inline const char* //
1028write_dst(const void* s, size_t n) {
1029 if (g_suppress_write_dst > 0) {
1030 return nullptr;
1031 } else if (n <= (DST_BUFFER_ARRAY_SIZE - g_dst.meta.wi)) {
1032 memcpy(g_dst.data.ptr + g_dst.meta.wi, s, n);
1033 g_dst.meta.wi += n;
1034 g_wrote_to_dst = true;
1035 return nullptr;
1036 }
1037 return write_dst_slow(s, n);
1038}
1039
Nigel Tao21042052020-08-19 23:13:54 +10001040#define TRY_INDENT_WITH_LEADING_NEW_LINE \
1041 do { \
1042 uint32_t indent = g_depth * g_bytes_per_indent_depth; \
1043 TRY(write_dst(g_new_line_then_256_indent_bytes, 1 + (indent & 0xFF))); \
1044 for (indent >>= 8; indent > 0; indent--) { \
1045 TRY(write_dst(g_new_line_then_256_indent_bytes + 1, 0x100)); \
1046 } \
1047 } while (false)
1048
1049// TRY_INDENT_SANS_LEADING_NEW_LINE is used after comments, which print their
1050// own "\n".
1051#define TRY_INDENT_SANS_LEADING_NEW_LINE \
1052 do { \
1053 uint32_t indent = g_depth * g_bytes_per_indent_depth; \
1054 TRY(write_dst(g_new_line_then_256_indent_bytes + 1, (indent & 0xFF))); \
1055 for (indent >>= 8; indent > 0; indent--) { \
1056 TRY(write_dst(g_new_line_then_256_indent_bytes + 1, 0x100)); \
1057 } \
1058 } while (false)
1059
Nigel Tao1b073492020-02-16 22:11:36 +11001060// ----
1061
Nigel Tao2914bae2020-02-26 09:40:30 +11001062const char* //
Nigel Tao7cb76542020-07-19 22:19:04 +10001063handle_unicode_code_point(uint32_t ucp) {
Nigel Tao63441812020-08-21 14:05:48 +10001064 if (ucp < 0x80) {
1065 return write_dst(&ascii_escapes[8 * ucp + 1], ascii_escapes[8 * ucp]);
Nigel Tao7cb76542020-07-19 22:19:04 +10001066 }
Nigel Tao7cb76542020-07-19 22:19:04 +10001067 uint8_t u[WUFFS_BASE__UTF_8__BYTE_LENGTH__MAX_INCL];
1068 size_t n = wuffs_base__utf_8__encode(
1069 wuffs_base__make_slice_u8(&u[0],
1070 WUFFS_BASE__UTF_8__BYTE_LENGTH__MAX_INCL),
1071 ucp);
1072 if (n == 0) {
1073 return "main: internal error: unexpected Unicode code point";
1074 }
Nigel Tao0291a472020-08-13 22:40:10 +10001075 return write_dst(&u[0], n);
Nigel Tao168f60a2020-07-14 13:19:33 +10001076}
1077
Nigel Taod191a3f2020-07-19 22:14:54 +10001078// ----
1079
Nigel Tao50db4a42020-08-20 11:31:28 +10001080inline const char* //
Nigel Tao2ef39992020-04-09 17:24:39 +10001081handle_token(wuffs_base__token t, bool start_of_token_chain) {
Nigel Tao2cf76db2020-02-27 22:42:01 +11001082 do {
Nigel Tao462f8662020-04-01 23:01:51 +11001083 int64_t vbc = t.value_base_category();
Nigel Tao2cf76db2020-02-27 22:42:01 +11001084 uint64_t vbd = t.value_base_detail();
Nigel Taoee6927f2020-07-27 12:08:33 +10001085 uint64_t token_length = t.length();
Nigel Tao991bd512020-08-19 09:38:16 +10001086 // The "- token_length" is because we incremented g_cursor_index before
1087 // calling handle_token.
Nigel Taoee6927f2020-07-27 12:08:33 +10001088 wuffs_base__slice_u8 tok = wuffs_base__make_slice_u8(
Nigel Tao991bd512020-08-19 09:38:16 +10001089 g_src.data.ptr + g_cursor_index - token_length, token_length);
Nigel Tao1b073492020-02-16 22:11:36 +11001090
1091 // Handle ']' or '}'.
Nigel Tao9f7a2502020-02-23 09:42:02 +11001092 if ((vbc == WUFFS_BASE__TOKEN__VBC__STRUCTURE) &&
Nigel Tao2cf76db2020-02-27 22:42:01 +11001093 (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__POP)) {
Nigel Taod60815c2020-03-26 14:32:35 +11001094 if (g_query.is_at(g_depth)) {
Nigel Tao0cd2f982020-03-03 23:03:02 +11001095 return "main: no match for query";
1096 }
Nigel Taod60815c2020-03-26 14:32:35 +11001097 if (g_depth <= 0) {
1098 return "main: internal error: inconsistent g_depth";
Nigel Tao1b073492020-02-16 22:11:36 +11001099 }
Nigel Taod60815c2020-03-26 14:32:35 +11001100 g_depth--;
Nigel Tao1b073492020-02-16 22:11:36 +11001101
Nigel Taod60815c2020-03-26 14:32:35 +11001102 if (g_query.matched_all() && (g_depth >= g_flags.max_output_depth)) {
1103 g_suppress_write_dst--;
Nigel Tao52c4d6a2020-03-08 21:12:38 +11001104 // '…' is U+2026 HORIZONTAL ELLIPSIS, which is 3 UTF-8 bytes.
Nigel Tao0291a472020-08-13 22:40:10 +10001105 TRY(write_dst((vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__FROM_LIST)
1106 ? "\"[…]\""
1107 : "\"{…}\"",
1108 7));
1109 } else {
Nigel Tao52c4d6a2020-03-08 21:12:38 +11001110 // Write preceding whitespace.
Nigel Taod60815c2020-03-26 14:32:35 +11001111 if ((g_ctx != context::in_list_after_bracket) &&
1112 (g_ctx != context::in_dict_after_brace) &&
1113 !g_flags.compact_output) {
Nigel Tao21042052020-08-19 23:13:54 +10001114 if (g_is_after_comment) {
1115 TRY_INDENT_SANS_LEADING_NEW_LINE;
1116 } else {
1117 if (g_flags.output_extra_comma) {
1118 TRY(write_dst(",", 1));
1119 }
1120 TRY_INDENT_WITH_LEADING_NEW_LINE;
Nigel Tao52c4d6a2020-03-08 21:12:38 +11001121 }
Nigel Tao1b073492020-02-16 22:11:36 +11001122 }
Nigel Tao52c4d6a2020-03-08 21:12:38 +11001123
1124 TRY(write_dst(
1125 (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__FROM_LIST) ? "]" : "}",
1126 1));
Nigel Tao1b073492020-02-16 22:11:36 +11001127 }
1128
Nigel Taod60815c2020-03-26 14:32:35 +11001129 g_ctx = (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_LIST)
1130 ? context::in_list_after_value
1131 : context::in_dict_after_key;
Nigel Tao1b073492020-02-16 22:11:36 +11001132 goto after_value;
1133 }
1134
Nigel Taod1c928a2020-02-28 12:43:53 +11001135 // Write preceding whitespace and punctuation, if it wasn't ']', '}' or a
Nigel Tao0291a472020-08-13 22:40:10 +10001136 // continuation of a multi-token chain.
1137 if (start_of_token_chain) {
Nigel Tao21042052020-08-19 23:13:54 +10001138 if (g_is_after_comment) {
1139 TRY_INDENT_SANS_LEADING_NEW_LINE;
1140 } else if (g_ctx == context::in_dict_after_key) {
Nigel Taod60815c2020-03-26 14:32:35 +11001141 TRY(write_dst(": ", g_flags.compact_output ? 1 : 2));
1142 } else if (g_ctx != context::none) {
Nigel Tao0291a472020-08-13 22:40:10 +10001143 if ((g_ctx != context::in_list_after_bracket) &&
1144 (g_ctx != context::in_dict_after_brace)) {
Nigel Tao0cd2f982020-03-03 23:03:02 +11001145 TRY(write_dst(",", 1));
Nigel Tao107f0ef2020-03-01 21:35:02 +11001146 }
Nigel Taod60815c2020-03-26 14:32:35 +11001147 if (!g_flags.compact_output) {
Nigel Tao21042052020-08-19 23:13:54 +10001148 TRY_INDENT_WITH_LEADING_NEW_LINE;
Nigel Tao0cd2f982020-03-03 23:03:02 +11001149 }
1150 }
1151
Nigel Tao52c4d6a2020-03-08 21:12:38 +11001152 bool query_matched_fragment = false;
Nigel Taod60815c2020-03-26 14:32:35 +11001153 if (g_query.is_at(g_depth)) {
1154 switch (g_ctx) {
Nigel Tao0cd2f982020-03-03 23:03:02 +11001155 case context::in_list_after_bracket:
1156 case context::in_list_after_value:
Nigel Taod60815c2020-03-26 14:32:35 +11001157 query_matched_fragment = g_query.tick();
Nigel Tao0cd2f982020-03-03 23:03:02 +11001158 break;
1159 case context::in_dict_after_key:
Nigel Taod60815c2020-03-26 14:32:35 +11001160 query_matched_fragment = g_query.matched_fragment();
Nigel Tao0cd2f982020-03-03 23:03:02 +11001161 break;
Nigel Tao18ef5b42020-03-16 10:37:47 +11001162 default:
1163 break;
Nigel Tao0cd2f982020-03-03 23:03:02 +11001164 }
1165 }
Nigel Tao52c4d6a2020-03-08 21:12:38 +11001166 if (!query_matched_fragment) {
Nigel Tao0cd2f982020-03-03 23:03:02 +11001167 // No-op.
Nigel Taod60815c2020-03-26 14:32:35 +11001168 } else if (!g_query.next_fragment()) {
Nigel Tao0cd2f982020-03-03 23:03:02 +11001169 // There is no next fragment. We have matched the complete query, and
1170 // the upcoming JSON value is the result of that query.
1171 //
Nigel Taod60815c2020-03-26 14:32:35 +11001172 // Un-suppress writing to stdout and reset the g_ctx and g_depth as if
1173 // we were about to decode a top-level value. This makes any subsequent
1174 // indentation be relative to this point, and we will return g_eod
1175 // after the upcoming JSON value is complete.
1176 if (g_suppress_write_dst != 1) {
1177 return "main: internal error: inconsistent g_suppress_write_dst";
Nigel Tao52c4d6a2020-03-08 21:12:38 +11001178 }
Nigel Taod60815c2020-03-26 14:32:35 +11001179 g_suppress_write_dst = 0;
1180 g_ctx = context::none;
1181 g_depth = 0;
Nigel Tao0cd2f982020-03-03 23:03:02 +11001182 } else if ((vbc != WUFFS_BASE__TOKEN__VBC__STRUCTURE) ||
1183 !(vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__PUSH)) {
1184 // The query has moved on to the next fragment but the upcoming JSON
1185 // value is not a container.
1186 return "main: no match for query";
Nigel Tao1b073492020-02-16 22:11:36 +11001187 }
1188 }
1189
1190 // Handle the token itself: either a container ('[' or '{') or a simple
Nigel Tao85fba7f2020-02-29 16:28:06 +11001191 // value: string (a chain of raw or escaped parts), literal or number.
Nigel Tao1b073492020-02-16 22:11:36 +11001192 switch (vbc) {
Nigel Tao85fba7f2020-02-29 16:28:06 +11001193 case WUFFS_BASE__TOKEN__VBC__STRUCTURE:
Nigel Taod60815c2020-03-26 14:32:35 +11001194 if (g_query.matched_all() && (g_depth >= g_flags.max_output_depth)) {
1195 g_suppress_write_dst++;
Nigel Tao0291a472020-08-13 22:40:10 +10001196 } else {
Nigel Tao52c4d6a2020-03-08 21:12:38 +11001197 TRY(write_dst(
1198 (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_LIST) ? "[" : "{",
1199 1));
1200 }
Nigel Taod60815c2020-03-26 14:32:35 +11001201 g_depth++;
1202 g_ctx = (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_LIST)
1203 ? context::in_list_after_bracket
1204 : context::in_dict_after_brace;
Nigel Tao85fba7f2020-02-29 16:28:06 +11001205 return nullptr;
1206
Nigel Tao2cf76db2020-02-27 22:42:01 +11001207 case WUFFS_BASE__TOKEN__VBC__STRING:
Nigel Tao0291a472020-08-13 22:40:10 +10001208 if (start_of_token_chain) {
1209 TRY(write_dst("\"", 1));
1210 g_query.restart_fragment(in_dict_before_key() &&
1211 g_query.is_at(g_depth));
1212 }
1213
Nigel Taoade01652020-08-21 15:57:51 +10001214 if (vbd & WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_1_DST_1_SRC_COPY) {
Nigel Tao0291a472020-08-13 22:40:10 +10001215 TRY(write_dst(tok.ptr, tok.len));
1216 g_query.incremental_match_slice(tok.ptr, tok.len);
Nigel Tao0291a472020-08-13 22:40:10 +10001217 }
1218
Nigel Tao496e88b2020-04-09 22:10:08 +10001219 if (t.continued()) {
Nigel Tao2cf76db2020-02-27 22:42:01 +11001220 return nullptr;
1221 }
Nigel Tao0291a472020-08-13 22:40:10 +10001222 TRY(write_dst("\"", 1));
Nigel Tao2cf76db2020-02-27 22:42:01 +11001223 goto after_value;
1224
1225 case WUFFS_BASE__TOKEN__VBC__UNICODE_CODE_POINT:
Nigel Tao496e88b2020-04-09 22:10:08 +10001226 if (!t.continued()) {
1227 return "main: internal error: unexpected non-continued UCP token";
Nigel Tao0cd2f982020-03-03 23:03:02 +11001228 }
1229 TRY(handle_unicode_code_point(vbd));
Nigel Taod60815c2020-03-26 14:32:35 +11001230 g_query.incremental_match_code_point(vbd);
Nigel Tao0cd2f982020-03-03 23:03:02 +11001231 return nullptr;
Nigel Tao1b073492020-02-16 22:11:36 +11001232 }
1233
Nigel Tao3f688b22020-08-21 15:51:48 +10001234 // We have a literal or a number.
1235 TRY(write_dst(tok.ptr, tok.len));
1236 goto after_value;
Nigel Tao2cf76db2020-02-27 22:42:01 +11001237 } while (0);
Nigel Tao1b073492020-02-16 22:11:36 +11001238
Nigel Tao2cf76db2020-02-27 22:42:01 +11001239 // Book-keeping after completing a value (whether a container value or a
1240 // simple value). Empty parent containers are no longer empty. If the parent
1241 // container is a "{...}" object, toggle between keys and values.
1242after_value:
Nigel Taod60815c2020-03-26 14:32:35 +11001243 if (g_depth == 0) {
1244 return g_eod;
Nigel Tao2cf76db2020-02-27 22:42:01 +11001245 }
Nigel Taod60815c2020-03-26 14:32:35 +11001246 switch (g_ctx) {
Nigel Tao2cf76db2020-02-27 22:42:01 +11001247 case context::in_list_after_bracket:
Nigel Taod60815c2020-03-26 14:32:35 +11001248 g_ctx = context::in_list_after_value;
Nigel Tao2cf76db2020-02-27 22:42:01 +11001249 break;
1250 case context::in_dict_after_brace:
Nigel Taod60815c2020-03-26 14:32:35 +11001251 g_ctx = context::in_dict_after_key;
Nigel Tao2cf76db2020-02-27 22:42:01 +11001252 break;
1253 case context::in_dict_after_key:
Nigel Taod60815c2020-03-26 14:32:35 +11001254 g_ctx = context::in_dict_after_value;
Nigel Tao2cf76db2020-02-27 22:42:01 +11001255 break;
1256 case context::in_dict_after_value:
Nigel Taod60815c2020-03-26 14:32:35 +11001257 g_ctx = context::in_dict_after_key;
Nigel Tao2cf76db2020-02-27 22:42:01 +11001258 break;
Nigel Tao18ef5b42020-03-16 10:37:47 +11001259 default:
1260 break;
Nigel Tao2cf76db2020-02-27 22:42:01 +11001261 }
1262 return nullptr;
1263}
1264
1265const char* //
1266main1(int argc, char** argv) {
1267 TRY(initialize_globals(argc, argv));
1268
Nigel Taocd183f92020-07-14 12:11:05 +10001269 bool start_of_token_chain = true;
Nigel Tao2cf76db2020-02-27 22:42:01 +11001270 while (true) {
Nigel Tao0291a472020-08-13 22:40:10 +10001271 wuffs_base__status status = g_dec.decode_tokens(
Nigel Taod60815c2020-03-26 14:32:35 +11001272 &g_tok, &g_src,
1273 wuffs_base__make_slice_u8(g_work_buffer_array, WORK_BUFFER_ARRAY_SIZE));
Nigel Tao2cf76db2020-02-27 22:42:01 +11001274
Nigel Taod60815c2020-03-26 14:32:35 +11001275 while (g_tok.meta.ri < g_tok.meta.wi) {
1276 wuffs_base__token t = g_tok.data.ptr[g_tok.meta.ri++];
Nigel Tao991bd512020-08-19 09:38:16 +10001277 uint64_t token_length = t.length();
1278 if ((g_src.meta.ri - g_cursor_index) < token_length) {
Nigel Taod60815c2020-03-26 14:32:35 +11001279 return "main: internal error: inconsistent g_src indexes";
Nigel Tao2cf76db2020-02-27 22:42:01 +11001280 }
Nigel Tao991bd512020-08-19 09:38:16 +10001281 g_cursor_index += token_length;
Nigel Tao2cf76db2020-02-27 22:42:01 +11001282
Nigel Tao21042052020-08-19 23:13:54 +10001283 // Handle filler tokens (e.g. whitespace, punctuation and comments).
1284 // These are skipped, unless -output-comments is enabled.
Nigel Tao3c8589b2020-07-19 21:49:00 +10001285 if (t.value_base_category() == WUFFS_BASE__TOKEN__VBC__FILLER) {
Nigel Tao21042052020-08-19 23:13:54 +10001286 if (g_flags.output_comments &&
1287 (t.value_base_detail() &
1288 WUFFS_BASE__TOKEN__VBD__FILLER__COMMENT_ANY)) {
1289 if (g_flags.compact_output) {
1290 TRY(write_dst(g_src.data.ptr + g_cursor_index - token_length,
1291 token_length));
1292 } else {
1293 if (start_of_token_chain) {
1294 if (g_is_after_comment) {
1295 TRY_INDENT_SANS_LEADING_NEW_LINE;
1296 } else if (g_ctx != context::none) {
1297 if (g_ctx == context::in_dict_after_key) {
1298 TRY(write_dst(":", 1));
1299 } else if ((g_ctx != context::in_list_after_bracket) &&
1300 (g_ctx != context::in_dict_after_brace)) {
1301 TRY(write_dst(",", 1));
1302 }
1303 if (!g_flags.compact_output) {
1304 TRY_INDENT_WITH_LEADING_NEW_LINE;
1305 }
1306 }
1307 }
1308 TRY(write_dst(g_src.data.ptr + g_cursor_index - token_length,
1309 token_length));
1310 if (!t.continued() &&
1311 (t.value_base_detail() &
1312 WUFFS_BASE__TOKEN__VBD__FILLER__COMMENT_BLOCK)) {
1313 TRY(write_dst("\n", 1));
1314 }
1315 g_is_after_comment = true;
1316 }
1317 }
Nigel Tao496e88b2020-04-09 22:10:08 +10001318 start_of_token_chain = !t.continued();
Nigel Tao2cf76db2020-02-27 22:42:01 +11001319 continue;
1320 }
1321
Nigel Tao2ef39992020-04-09 17:24:39 +10001322 const char* z = handle_token(t, start_of_token_chain);
Nigel Tao21042052020-08-19 23:13:54 +10001323 g_is_after_comment = false;
Nigel Tao496e88b2020-04-09 22:10:08 +10001324 start_of_token_chain = !t.continued();
Nigel Tao2cf76db2020-02-27 22:42:01 +11001325 if (z == nullptr) {
1326 continue;
Nigel Taod60815c2020-03-26 14:32:35 +11001327 } else if (z == g_eod) {
Nigel Tao0cd2f982020-03-03 23:03:02 +11001328 goto end_of_data;
Nigel Tao2cf76db2020-02-27 22:42:01 +11001329 }
1330 return z;
Nigel Tao1b073492020-02-16 22:11:36 +11001331 }
Nigel Tao2cf76db2020-02-27 22:42:01 +11001332
1333 if (status.repr == nullptr) {
Nigel Tao0cd2f982020-03-03 23:03:02 +11001334 return "main: internal error: unexpected end of token stream";
Nigel Tao2cf76db2020-02-27 22:42:01 +11001335 } else if (status.repr == wuffs_base__suspension__short_read) {
Nigel Tao991bd512020-08-19 09:38:16 +10001336 if (g_cursor_index != g_src.meta.ri) {
Nigel Taod60815c2020-03-26 14:32:35 +11001337 return "main: internal error: inconsistent g_src indexes";
Nigel Tao2cf76db2020-02-27 22:42:01 +11001338 }
1339 TRY(read_src());
Nigel Tao991bd512020-08-19 09:38:16 +10001340 g_cursor_index = g_src.meta.ri;
Nigel Tao2cf76db2020-02-27 22:42:01 +11001341 } else if (status.repr == wuffs_base__suspension__short_write) {
Nigel Taod60815c2020-03-26 14:32:35 +11001342 g_tok.compact();
Nigel Tao2cf76db2020-02-27 22:42:01 +11001343 } else {
1344 return status.message();
Nigel Tao1b073492020-02-16 22:11:36 +11001345 }
1346 }
Nigel Tao0cd2f982020-03-03 23:03:02 +11001347end_of_data:
1348
Nigel Taod60815c2020-03-26 14:32:35 +11001349 // With a non-empty g_query, don't try to consume trailing whitespace or
Nigel Tao0cd2f982020-03-03 23:03:02 +11001350 // confirm that we've processed all the tokens.
Nigel Taod60815c2020-03-26 14:32:35 +11001351 if (g_flags.query_c_string && *g_flags.query_c_string) {
Nigel Tao0cd2f982020-03-03 23:03:02 +11001352 return nullptr;
1353 }
Nigel Tao6b161af2020-02-24 11:01:48 +11001354
Nigel Tao6b161af2020-02-24 11:01:48 +11001355 // Check that we've exhausted the input.
Nigel Taod60815c2020-03-26 14:32:35 +11001356 if ((g_src.meta.ri == g_src.meta.wi) && !g_src.meta.closed) {
Nigel Taofe0cbbd2020-03-05 22:01:30 +11001357 TRY(read_src());
1358 }
Nigel Taod60815c2020-03-26 14:32:35 +11001359 if ((g_src.meta.ri < g_src.meta.wi) || !g_src.meta.closed) {
Nigel Tao0291a472020-08-13 22:40:10 +10001360 return "main: valid JSON followed by further (unexpected) data";
Nigel Tao6b161af2020-02-24 11:01:48 +11001361 }
1362
1363 // Check that we've used all of the decoded tokens, other than trailing
Nigel Tao4b186b02020-03-18 14:25:21 +11001364 // filler tokens. For example, "true\n" is valid JSON (and fully consumed
1365 // with WUFFS_JSON__QUIRK_ALLOW_TRAILING_NEW_LINE enabled) with a trailing
1366 // filler token for the "\n".
Nigel Taod60815c2020-03-26 14:32:35 +11001367 for (; g_tok.meta.ri < g_tok.meta.wi; g_tok.meta.ri++) {
1368 if (g_tok.data.ptr[g_tok.meta.ri].value_base_category() !=
Nigel Tao6b161af2020-02-24 11:01:48 +11001369 WUFFS_BASE__TOKEN__VBC__FILLER) {
1370 return "main: internal error: decoded OK but unprocessed tokens remain";
1371 }
1372 }
1373
1374 return nullptr;
Nigel Tao1b073492020-02-16 22:11:36 +11001375}
1376
Nigel Tao2914bae2020-02-26 09:40:30 +11001377int //
1378compute_exit_code(const char* status_msg) {
Nigel Tao9cc2c252020-02-23 17:05:49 +11001379 if (!status_msg) {
1380 return 0;
1381 }
Nigel Tao01abc842020-03-06 21:42:33 +11001382 size_t n;
Nigel Taod60815c2020-03-26 14:32:35 +11001383 if (status_msg == g_usage) {
Nigel Tao01abc842020-03-06 21:42:33 +11001384 n = strlen(status_msg);
1385 } else {
Nigel Tao9cc2c252020-02-23 17:05:49 +11001386 n = strnlen(status_msg, 2047);
Nigel Tao01abc842020-03-06 21:42:33 +11001387 if (n >= 2047) {
1388 status_msg = "main: internal error: error message is too long";
1389 n = strnlen(status_msg, 2047);
1390 }
Nigel Tao9cc2c252020-02-23 17:05:49 +11001391 }
Nigel Taofe0cbbd2020-03-05 22:01:30 +11001392 const int stderr_fd = 2;
1393 ignore_return_value(write(stderr_fd, status_msg, n));
1394 ignore_return_value(write(stderr_fd, "\n", 1));
Nigel Tao9cc2c252020-02-23 17:05:49 +11001395 // Return an exit code of 1 for regular (forseen) errors, e.g. badly
1396 // formatted or unsupported input.
1397 //
1398 // Return an exit code of 2 for internal (exceptional) errors, e.g. defensive
1399 // run-time checks found that an internal invariant did not hold.
1400 //
1401 // Automated testing, including badly formatted inputs, can therefore
1402 // discriminate between expected failure (exit code 1) and unexpected failure
1403 // (other non-zero exit codes). Specifically, exit code 2 for internal
1404 // invariant violation, exit code 139 (which is 128 + SIGSEGV on x86_64
1405 // linux) for a segmentation fault (e.g. null pointer dereference).
1406 return strstr(status_msg, "internal error:") ? 2 : 1;
1407}
1408
Nigel Tao2914bae2020-02-26 09:40:30 +11001409int //
1410main(int argc, char** argv) {
Nigel Tao01abc842020-03-06 21:42:33 +11001411 // Look for an input filename (the first non-flag argument) in argv. If there
1412 // is one, open it (but do not read from it) before we self-impose a sandbox.
1413 //
1414 // Flags start with "-", unless it comes after a bare "--" arg.
1415 {
1416 bool dash_dash = false;
1417 int a;
1418 for (a = 1; a < argc; a++) {
1419 char* arg = argv[a];
1420 if ((arg[0] == '-') && !dash_dash) {
1421 dash_dash = (arg[1] == '-') && (arg[2] == '\x00');
1422 continue;
1423 }
Nigel Taod60815c2020-03-26 14:32:35 +11001424 g_input_file_descriptor = open(arg, O_RDONLY);
1425 if (g_input_file_descriptor < 0) {
Nigel Tao01abc842020-03-06 21:42:33 +11001426 fprintf(stderr, "%s: %s\n", arg, strerror(errno));
1427 return 1;
1428 }
1429 break;
1430 }
1431 }
1432
Nigel Taofe0cbbd2020-03-05 22:01:30 +11001433#if defined(WUFFS_EXAMPLE_USE_SECCOMP)
1434 prctl(PR_SET_SECCOMP, SECCOMP_MODE_STRICT);
Nigel Taod60815c2020-03-26 14:32:35 +11001435 g_sandboxed = true;
Nigel Taofe0cbbd2020-03-05 22:01:30 +11001436#endif
1437
Nigel Tao0cd2f982020-03-03 23:03:02 +11001438 const char* z = main1(argc, argv);
Nigel Taod60815c2020-03-26 14:32:35 +11001439 if (g_wrote_to_dst) {
Nigel Tao0291a472020-08-13 22:40:10 +10001440 const char* z1 = write_dst("\n", 1);
Nigel Tao0cd2f982020-03-03 23:03:02 +11001441 const char* z2 = flush_dst();
1442 z = z ? z : (z1 ? z1 : z2);
1443 }
1444 int exit_code = compute_exit_code(z);
Nigel Taofe0cbbd2020-03-05 22:01:30 +11001445
1446#if defined(WUFFS_EXAMPLE_USE_SECCOMP)
1447 // Call SYS_exit explicitly, instead of calling SYS_exit_group implicitly by
1448 // either calling _exit or returning from main. SECCOMP_MODE_STRICT allows
1449 // only SYS_exit.
1450 syscall(SYS_exit, exit_code);
1451#endif
Nigel Tao9cc2c252020-02-23 17:05:49 +11001452 return exit_code;
Nigel Tao1b073492020-02-16 22:11:36 +11001453}