Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1 | // 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 | |
Nigel Tao | b55d539 | 2020-09-11 08:11:02 +1000 | [diff] [blame] | 17 | // jsonptr is discussed extensively at |
| 18 | // https://nigeltao.github.io/blog/2020/jsonptr.html |
| 19 | |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 20 | /* |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 21 | jsonptr is a JSON formatter (pretty-printer) that supports the JSON Pointer |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 22 | (RFC 6901) query syntax. It reads UTF-8 JSON from stdin and writes |
| 23 | canonicalized, formatted UTF-8 JSON to stdout. |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 24 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 25 | See the "const char* g_usage" string below for details. |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 26 | |
| 27 | ---- |
| 28 | |
| 29 | JSON Pointer (and this program's implementation) is one of many JSON query |
| 30 | languages and JSON tools, such as jq, jql and JMESPath. This one is relatively |
| 31 | simple and fewer-featured compared to those others. |
| 32 | |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 33 | One benefit of simplicity is that this program's JSON and JSON Pointer |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 34 | implementations do not dynamically allocate or free memory (yet it does not |
| 35 | require that the entire input fits in memory at once). They are therefore |
| 36 | trivially protected against certain bug classes: memory leaks, double-frees and |
| 37 | use-after-frees. |
| 38 | |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 39 | The core JSON implementation is also written in the Wuffs programming language |
| 40 | (and then transpiled to C/C++), which is memory-safe (e.g. array indexing is |
| 41 | bounds-checked) but also guards against integer arithmetic overflows. |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 42 | |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 43 | For defense in depth, on Linux, this program also self-imposes a |
| 44 | SECCOMP_MODE_STRICT sandbox before reading (or otherwise processing) its input |
| 45 | or writing its output. Under this sandbox, the only permitted system calls are |
| 46 | read, write, exit and sigreturn. |
| 47 | |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 48 | All together, this program aims to safely handle untrusted JSON files without |
| 49 | fear of security bugs such as remote code execution. |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 50 | |
| 51 | ---- |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 52 | |
Nigel Tao | c5b3a9e | 2020-02-24 11:54:35 +1100 | [diff] [blame] | 53 | As of 2020-02-24, this program passes all 318 "test_parsing" cases from the |
| 54 | JSON test suite (https://github.com/nst/JSONTestSuite), an appendix to the |
| 55 | "Parsing JSON is a Minefield" article (http://seriot.ch/parsing_json.php) that |
| 56 | was first published on 2016-10-26 and updated on 2018-03-30. |
| 57 | |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 58 | After modifying this program, run "build-example.sh example/jsonptr/" and then |
| 59 | "script/run-json-test-suite.sh" to catch correctness regressions. |
| 60 | |
| 61 | ---- |
| 62 | |
Nigel Tao | d0b16cb | 2020-03-14 10:15:54 +1100 | [diff] [blame] | 63 | This program uses Wuffs' JSON decoder at a relatively low level, processing the |
| 64 | decoder's token-stream output individually. The core loop, in pseudo-code, is |
| 65 | "for_each_token { handle_token(etc); }", where the handle_token function |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 66 | changes global state (e.g. the `g_depth` and `g_ctx` variables) and prints |
Nigel Tao | d0b16cb | 2020-03-14 10:15:54 +1100 | [diff] [blame] | 67 | output text based on that state and the token's source text. Notably, |
| 68 | handle_token is not recursive, even though JSON values can nest. |
| 69 | |
| 70 | This approach is centered around JSON tokens. Each JSON 'thing' (e.g. number, |
| 71 | string, object) comprises one or more JSON tokens. |
| 72 | |
| 73 | An alternative, higher-level approach is in the sibling example/jsonfindptrs |
| 74 | program. Neither approach is better or worse per se, but when studying this |
| 75 | program, be aware that there are multiple ways to use Wuffs' JSON decoder. |
| 76 | |
| 77 | The two programs, jsonfindptrs and jsonptr, also demonstrate different |
| 78 | trade-offs with regard to JSON object duplicate keys. The JSON spec permits |
| 79 | different implementations to allow or reject duplicate keys. It is not always |
| 80 | clear which approach is safer. Rejecting them is certainly unambiguous, and |
| 81 | security bugs can lurk in ambiguous corners of a file format, if two different |
| 82 | implementations both silently accept a file but differ on how to interpret it. |
| 83 | On the other hand, in the worst case, detecting duplicate keys requires O(N) |
| 84 | memory, where N is the size of the (potentially untrusted) input. |
| 85 | |
| 86 | This program (jsonptr) allows duplicate keys and requires only O(1) memory. As |
| 87 | mentioned above, it doesn't dynamically allocate memory at all, and on Linux, |
| 88 | it runs in a SECCOMP_MODE_STRICT sandbox. |
| 89 | |
| 90 | ---- |
| 91 | |
Nigel Tao | 50bfab9 | 2020-08-05 11:39:09 +1000 | [diff] [blame] | 92 | To run: |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 93 | |
| 94 | $CXX jsonptr.cc && ./a.out < ../../test/data/github-tags.json; rm -f a.out |
| 95 | |
| 96 | for a C++ compiler $CXX, such as clang++ or g++. |
| 97 | */ |
| 98 | |
Nigel Tao | 721190a | 2020-04-03 22:25:21 +1100 | [diff] [blame] | 99 | #if defined(__cplusplus) && (__cplusplus < 201103L) |
| 100 | #error "This C++ program requires -std=c++11 or later" |
| 101 | #endif |
| 102 | |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 103 | #include <errno.h> |
Nigel Tao | 01abc84 | 2020-03-06 21:42:33 +1100 | [diff] [blame] | 104 | #include <fcntl.h> |
| 105 | #include <stdio.h> |
Nigel Tao | 9cc2c25 | 2020-02-23 17:05:49 +1100 | [diff] [blame] | 106 | #include <string.h> |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 107 | #include <unistd.h> |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 108 | |
| 109 | // Wuffs ships as a "single file C library" or "header file library" as per |
| 110 | // https://github.com/nothings/stb/blob/master/docs/stb_howto.txt |
| 111 | // |
| 112 | // To use that single file as a "foo.c"-like implementation, instead of a |
| 113 | // "foo.h"-like header, #define WUFFS_IMPLEMENTATION before #include'ing or |
| 114 | // compiling it. |
| 115 | #define WUFFS_IMPLEMENTATION |
| 116 | |
| 117 | // Defining the WUFFS_CONFIG__MODULE* macros are optional, but it lets users of |
Nigel Tao | 2f78804 | 2021-01-23 19:29:19 +1100 | [diff] [blame] | 118 | // release/c/etc.c choose which parts of Wuffs to build. That file contains the |
| 119 | // entire Wuffs standard library, implementing a variety of codecs and file |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 120 | // formats. Without this macro definition, an optimizing compiler or linker may |
| 121 | // very well discard Wuffs code for unused codecs, but listing the Wuffs |
| 122 | // modules we use makes that process explicit. Preprocessing means that such |
| 123 | // code simply isn't compiled. |
| 124 | #define WUFFS_CONFIG__MODULES |
| 125 | #define WUFFS_CONFIG__MODULE__BASE |
| 126 | #define WUFFS_CONFIG__MODULE__JSON |
| 127 | |
| 128 | // If building this program in an environment that doesn't easily accommodate |
| 129 | // relative includes, you can use the script/inline-c-relative-includes.go |
| 130 | // program to generate a stand-alone C++ file. |
| 131 | #include "../../release/c/wuffs-unsupported-snapshot.c" |
| 132 | |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 133 | #if defined(__linux__) |
| 134 | #include <linux/prctl.h> |
| 135 | #include <linux/seccomp.h> |
| 136 | #include <sys/prctl.h> |
| 137 | #include <sys/syscall.h> |
| 138 | #define WUFFS_EXAMPLE_USE_SECCOMP |
| 139 | #endif |
| 140 | |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 141 | #define TRY(error_msg) \ |
| 142 | do { \ |
| 143 | const char* z = error_msg; \ |
| 144 | if (z) { \ |
| 145 | return z; \ |
| 146 | } \ |
| 147 | } while (false) |
| 148 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 149 | static const char* g_eod = "main: end of data"; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 150 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 151 | static const char* g_usage = |
Nigel Tao | 01abc84 | 2020-03-06 21:42:33 +1100 | [diff] [blame] | 152 | "Usage: jsonptr -flags input.json\n" |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 153 | "\n" |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 154 | "Flags:\n" |
Nigel Tao | 3690e83 | 2020-03-12 16:52:26 +1100 | [diff] [blame] | 155 | " -c -compact-output\n" |
Nigel Tao | 94440cf | 2020-04-02 22:28:24 +1100 | [diff] [blame] | 156 | " -d=NUM -max-output-depth=NUM\n" |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 157 | " -q=STR -query=STR\n" |
Nigel Tao | ecadf72 | 2020-07-13 08:22:34 +1000 | [diff] [blame] | 158 | " -s=NUM -spaces=NUM\n" |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 159 | " -t -tabs\n" |
| 160 | " -fail-if-unsandboxed\n" |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 161 | " -input-allow-comments\n" |
| 162 | " -input-allow-extra-comma\n" |
| 163 | " -input-allow-inf-nan-numbers\n" |
Nigel Tao | 2104205 | 2020-08-19 23:13:54 +1000 | [diff] [blame] | 164 | " -jwcc\n" |
| 165 | " -output-comments\n" |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 166 | " -output-extra-comma\n" |
Nigel Tao | 7568254 | 2020-08-22 21:40:18 +1000 | [diff] [blame] | 167 | " -output-inf-nan-numbers\n" |
Nigel Tao | ecadf72 | 2020-07-13 08:22:34 +1000 | [diff] [blame] | 168 | " -strict-json-pointer-syntax\n" |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 169 | "\n" |
Nigel Tao | 01abc84 | 2020-03-06 21:42:33 +1100 | [diff] [blame] | 170 | "The input.json filename is optional. If absent, it reads from stdin.\n" |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 171 | "\n" |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 172 | "----\n" |
| 173 | "\n" |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 174 | "jsonptr is a JSON formatter (pretty-printer) that supports the JSON\n" |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 175 | "Pointer (RFC 6901) query syntax. It reads UTF-8 JSON from stdin and\n" |
| 176 | "writes canonicalized, formatted UTF-8 JSON to stdout.\n" |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 177 | "\n" |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 178 | "Canonicalized means that e.g. \"abc\\u000A\\tx\\u0177z\" is re-written\n" |
| 179 | "as \"abc\\n\\txÅ·z\". It does not sort object keys, nor does it reject\n" |
Nigel Tao | 01abc84 | 2020-03-06 21:42:33 +1100 | [diff] [blame] | 180 | "duplicate keys. Canonicalization does not imply Unicode normalization.\n" |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 181 | "\n" |
| 182 | "Formatted means that arrays' and objects' elements are indented, each\n" |
Nigel Tao | ecadf72 | 2020-07-13 08:22:34 +1000 | [diff] [blame] | 183 | "on its own line. Configure this with the -c / -compact-output, -s=NUM /\n" |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 184 | "-spaces=NUM (for NUM ranging from 0 to 8) and -t / -tabs flags.\n" |
Nigel Tao | 168f60a | 2020-07-14 13:19:33 +1000 | [diff] [blame] | 185 | "\n" |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 186 | "The -input-allow-comments flag allows \"/*slash-star*/\" and\n" |
| 187 | "\"//slash-slash\" C-style comments within JSON input. Such comments are\n" |
Nigel Tao | 2104205 | 2020-08-19 23:13:54 +1000 | [diff] [blame] | 188 | "stripped from the output unless -output-comments was also set.\n" |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 189 | "\n" |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 190 | "The -input-allow-extra-comma flag allows input like \"[1,2,]\", with a\n" |
| 191 | "comma after the final element of a JSON list or dictionary.\n" |
Nigel Tao | 3c8589b | 2020-07-19 21:49:00 +1000 | [diff] [blame] | 192 | "\n" |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 193 | "The -input-allow-inf-nan-numbers flag allows non-finite floating point\n" |
Nigel Tao | 7568254 | 2020-08-22 21:40:18 +1000 | [diff] [blame] | 194 | "numbers (infinities and not-a-numbers) within JSON input. This flag\n" |
| 195 | "requires that -output-inf-nan-numbers also be set.\n" |
Nigel Tao | 3c8589b | 2020-07-19 21:49:00 +1000 | [diff] [blame] | 196 | "\n" |
Nigel Tao | 2104205 | 2020-08-19 23:13:54 +1000 | [diff] [blame] | 197 | "The -output-comments flag copies any input comments to the output. It\n" |
| 198 | "has no effect unless -input-allow-comments was also set. Comments look\n" |
| 199 | "better after commas than before them, but a closing \"]\" or \"}\" can\n" |
| 200 | "occur after arbitrarily many comments, so -output-comments also requires\n" |
| 201 | "that one or both of -compact-output and -output-extra-comma be set.\n" |
| 202 | "\n" |
Nigel Tao | 773994c | 2021-02-22 10:50:08 +1100 | [diff] [blame^] | 203 | "With -output-comments, consecutive blank lines collapse to a single\n" |
| 204 | "blank line. Without that flag, all blank lines are removed.\n" |
| 205 | "\n" |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 206 | "The -output-extra-comma flag writes output like \"[1,2,]\", with a comma\n" |
| 207 | "after the final element of a JSON list or dictionary. Such commas are\n" |
| 208 | "non-compliant with the JSON specification but many parsers accept them\n" |
| 209 | "and they can produce simpler line-based diffs. This flag is ignored when\n" |
| 210 | "-compact-output is set.\n" |
Nigel Tao | f8dfc76 | 2020-07-23 23:35:44 +1000 | [diff] [blame] | 211 | "\n" |
Nigel Tao | 2104205 | 2020-08-19 23:13:54 +1000 | [diff] [blame] | 212 | "The -jwcc flag (JSON With Commas and Comments) enables all of:\n" |
| 213 | " -input-allow-comments\n" |
| 214 | " -input-allow-extra-comma\n" |
| 215 | " -output-comments\n" |
| 216 | " -output-extra-comma\n" |
| 217 | "\n" |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 218 | "----\n" |
| 219 | "\n" |
| 220 | "The -q=STR or -query=STR flag gives an optional JSON Pointer query, to\n" |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 221 | "print a subset of the input. For example, given RFC 6901 section 5's\n" |
Nigel Tao | 01abc84 | 2020-03-06 21:42:33 +1100 | [diff] [blame] | 222 | "sample input (https://tools.ietf.org/rfc/rfc6901.txt), this command:\n" |
| 223 | " jsonptr -query=/foo/1 rfc-6901-json-pointer.json\n" |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 224 | "will print:\n" |
| 225 | " \"baz\"\n" |
| 226 | "\n" |
| 227 | "An absent query is equivalent to the empty query, which identifies the\n" |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 228 | "entire input (the root value). Unlike a file system, the \"/\" query\n" |
Nigel Tao | d0b16cb | 2020-03-14 10:15:54 +1100 | [diff] [blame] | 229 | "does not identify the root. Instead, \"\" is the root and \"/\" is the\n" |
| 230 | "child (the value in a key-value pair) of the root whose key is the empty\n" |
| 231 | "string. Similarly, \"/xyz\" and \"/xyz/\" are two different nodes.\n" |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 232 | "\n" |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 233 | "If the query found a valid JSON value, this program will return a zero\n" |
| 234 | "exit code even if the rest of the input isn't valid JSON. If the query\n" |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 235 | "did not find a value, or found an invalid one, this program returns a\n" |
| 236 | "non-zero exit code, but may still print partial output to stdout.\n" |
| 237 | "\n" |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 238 | "The JSON specification (https://json.org/) permits implementations that\n" |
| 239 | "allow duplicate keys, as this one does. This JSON Pointer implementation\n" |
| 240 | "is also greedy, following the first match for each fragment without\n" |
| 241 | "back-tracking. For example, the \"/foo/bar\" query will fail if the root\n" |
| 242 | "object has multiple \"foo\" children but the first one doesn't have a\n" |
| 243 | "\"bar\" child, even if later ones do.\n" |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 244 | "\n" |
Nigel Tao | ecadf72 | 2020-07-13 08:22:34 +1000 | [diff] [blame] | 245 | "The -strict-json-pointer-syntax flag restricts the -query=STR string to\n" |
| 246 | "exactly RFC 6901, with only two escape sequences: \"~0\" and \"~1\" for\n" |
Nigel Tao | 904004e | 2020-11-15 20:56:04 +1100 | [diff] [blame] | 247 | "\"~\" and \"/\". Without this flag, this program also lets \"~n\",\n" |
| 248 | "\"~r\" and \"~t\" escape the New Line, Carriage Return and Horizontal\n" |
| 249 | "Tab ASCII control characters, which can work better with line oriented\n" |
| 250 | "(and tab separated) Unix tools that assume exactly one record (e.g. one\n" |
| 251 | "JSON Pointer string) per line.\n" |
Nigel Tao | d6fdfb1 | 2020-03-11 12:24:14 +1100 | [diff] [blame] | 252 | "\n" |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 253 | "----\n" |
| 254 | "\n" |
Nigel Tao | 94440cf | 2020-04-02 22:28:24 +1100 | [diff] [blame] | 255 | "The -d=NUM or -max-output-depth=NUM flag gives the maximum (inclusive)\n" |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 256 | "output depth. JSON containers ([] arrays and {} objects) can hold other\n" |
| 257 | "containers. When this flag is set, containers at depth NUM are replaced\n" |
| 258 | "with \"[…]\" or \"{…}\". A bare -d or -max-output-depth is equivalent to\n" |
| 259 | "-d=1. The flag's absence is equivalent to an unlimited output depth.\n" |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 260 | "\n" |
| 261 | "The -max-output-depth flag only affects the program's output. It doesn't\n" |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 262 | "affect whether or not the input is considered valid JSON. The JSON\n" |
| 263 | "specification permits implementations to set their own maximum input\n" |
| 264 | "depth. This JSON implementation sets it to 1024.\n" |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 265 | "\n" |
| 266 | "Depth is measured in terms of nested containers. It is unaffected by the\n" |
| 267 | "number of spaces or tabs used to indent.\n" |
| 268 | "\n" |
| 269 | "When both -max-output-depth and -query are set, the output depth is\n" |
| 270 | "measured from when the query resolves, not from the input root. The\n" |
| 271 | "input depth (measured from the root) is still limited to 1024.\n" |
| 272 | "\n" |
| 273 | "----\n" |
| 274 | "\n" |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 275 | "The -fail-if-unsandboxed flag causes the program to exit if it does not\n" |
| 276 | "self-impose a sandbox. On Linux, it self-imposes a SECCOMP_MODE_STRICT\n" |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 277 | "sandbox, regardless of whether this flag was set."; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 278 | |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 279 | // ---- |
| 280 | |
Nigel Tao | 6344181 | 2020-08-21 14:05:48 +1000 | [diff] [blame] | 281 | // ascii_escapes was created by script/print-json-ascii-escapes.go. |
| 282 | const uint8_t ascii_escapes[1024] = { |
| 283 | 0x06, 0x5C, 0x75, 0x30, 0x30, 0x30, 0x30, 0x00, // 0x00: "\\u0000" |
| 284 | 0x06, 0x5C, 0x75, 0x30, 0x30, 0x30, 0x31, 0x00, // 0x01: "\\u0001" |
| 285 | 0x06, 0x5C, 0x75, 0x30, 0x30, 0x30, 0x32, 0x00, // 0x02: "\\u0002" |
| 286 | 0x06, 0x5C, 0x75, 0x30, 0x30, 0x30, 0x33, 0x00, // 0x03: "\\u0003" |
| 287 | 0x06, 0x5C, 0x75, 0x30, 0x30, 0x30, 0x34, 0x00, // 0x04: "\\u0004" |
| 288 | 0x06, 0x5C, 0x75, 0x30, 0x30, 0x30, 0x35, 0x00, // 0x05: "\\u0005" |
| 289 | 0x06, 0x5C, 0x75, 0x30, 0x30, 0x30, 0x36, 0x00, // 0x06: "\\u0006" |
| 290 | 0x06, 0x5C, 0x75, 0x30, 0x30, 0x30, 0x37, 0x00, // 0x07: "\\u0007" |
| 291 | 0x02, 0x5C, 0x62, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x08: "\\b" |
| 292 | 0x02, 0x5C, 0x74, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x09: "\\t" |
| 293 | 0x02, 0x5C, 0x6E, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x0A: "\\n" |
| 294 | 0x06, 0x5C, 0x75, 0x30, 0x30, 0x30, 0x42, 0x00, // 0x0B: "\\u000B" |
| 295 | 0x02, 0x5C, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x0C: "\\f" |
| 296 | 0x02, 0x5C, 0x72, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x0D: "\\r" |
| 297 | 0x06, 0x5C, 0x75, 0x30, 0x30, 0x30, 0x45, 0x00, // 0x0E: "\\u000E" |
| 298 | 0x06, 0x5C, 0x75, 0x30, 0x30, 0x30, 0x46, 0x00, // 0x0F: "\\u000F" |
| 299 | 0x06, 0x5C, 0x75, 0x30, 0x30, 0x31, 0x30, 0x00, // 0x10: "\\u0010" |
| 300 | 0x06, 0x5C, 0x75, 0x30, 0x30, 0x31, 0x31, 0x00, // 0x11: "\\u0011" |
| 301 | 0x06, 0x5C, 0x75, 0x30, 0x30, 0x31, 0x32, 0x00, // 0x12: "\\u0012" |
| 302 | 0x06, 0x5C, 0x75, 0x30, 0x30, 0x31, 0x33, 0x00, // 0x13: "\\u0013" |
| 303 | 0x06, 0x5C, 0x75, 0x30, 0x30, 0x31, 0x34, 0x00, // 0x14: "\\u0014" |
| 304 | 0x06, 0x5C, 0x75, 0x30, 0x30, 0x31, 0x35, 0x00, // 0x15: "\\u0015" |
| 305 | 0x06, 0x5C, 0x75, 0x30, 0x30, 0x31, 0x36, 0x00, // 0x16: "\\u0016" |
| 306 | 0x06, 0x5C, 0x75, 0x30, 0x30, 0x31, 0x37, 0x00, // 0x17: "\\u0017" |
| 307 | 0x06, 0x5C, 0x75, 0x30, 0x30, 0x31, 0x38, 0x00, // 0x18: "\\u0018" |
| 308 | 0x06, 0x5C, 0x75, 0x30, 0x30, 0x31, 0x39, 0x00, // 0x19: "\\u0019" |
| 309 | 0x06, 0x5C, 0x75, 0x30, 0x30, 0x31, 0x41, 0x00, // 0x1A: "\\u001A" |
| 310 | 0x06, 0x5C, 0x75, 0x30, 0x30, 0x31, 0x42, 0x00, // 0x1B: "\\u001B" |
| 311 | 0x06, 0x5C, 0x75, 0x30, 0x30, 0x31, 0x43, 0x00, // 0x1C: "\\u001C" |
| 312 | 0x06, 0x5C, 0x75, 0x30, 0x30, 0x31, 0x44, 0x00, // 0x1D: "\\u001D" |
| 313 | 0x06, 0x5C, 0x75, 0x30, 0x30, 0x31, 0x45, 0x00, // 0x1E: "\\u001E" |
| 314 | 0x06, 0x5C, 0x75, 0x30, 0x30, 0x31, 0x46, 0x00, // 0x1F: "\\u001F" |
| 315 | 0x06, 0x5C, 0x75, 0x30, 0x30, 0x32, 0x30, 0x00, // 0x20: "\\u0020" |
| 316 | 0x01, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x21: "!" |
| 317 | 0x02, 0x5C, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x22: "\\\"" |
| 318 | 0x01, 0x23, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x23: "#" |
| 319 | 0x01, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x24: "$" |
| 320 | 0x01, 0x25, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x25: "%" |
| 321 | 0x01, 0x26, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x26: "&" |
| 322 | 0x01, 0x27, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x27: "'" |
| 323 | 0x01, 0x28, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x28: "(" |
| 324 | 0x01, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x29: ")" |
| 325 | 0x01, 0x2A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x2A: "*" |
| 326 | 0x01, 0x2B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x2B: "+" |
| 327 | 0x01, 0x2C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x2C: "," |
| 328 | 0x01, 0x2D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x2D: "-" |
| 329 | 0x01, 0x2E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x2E: "." |
| 330 | 0x01, 0x2F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x2F: "/" |
| 331 | 0x01, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x30: "0" |
| 332 | 0x01, 0x31, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x31: "1" |
| 333 | 0x01, 0x32, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x32: "2" |
| 334 | 0x01, 0x33, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x33: "3" |
| 335 | 0x01, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x34: "4" |
| 336 | 0x01, 0x35, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x35: "5" |
| 337 | 0x01, 0x36, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x36: "6" |
| 338 | 0x01, 0x37, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x37: "7" |
| 339 | 0x01, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x38: "8" |
| 340 | 0x01, 0x39, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x39: "9" |
| 341 | 0x01, 0x3A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x3A: ":" |
| 342 | 0x01, 0x3B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x3B: ";" |
| 343 | 0x01, 0x3C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x3C: "<" |
| 344 | 0x01, 0x3D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x3D: "=" |
| 345 | 0x01, 0x3E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x3E: ">" |
| 346 | 0x01, 0x3F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x3F: "?" |
| 347 | 0x01, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x40: "@" |
| 348 | 0x01, 0x41, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x41: "A" |
| 349 | 0x01, 0x42, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x42: "B" |
| 350 | 0x01, 0x43, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x43: "C" |
| 351 | 0x01, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x44: "D" |
| 352 | 0x01, 0x45, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x45: "E" |
| 353 | 0x01, 0x46, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x46: "F" |
| 354 | 0x01, 0x47, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x47: "G" |
| 355 | 0x01, 0x48, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x48: "H" |
| 356 | 0x01, 0x49, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x49: "I" |
| 357 | 0x01, 0x4A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x4A: "J" |
| 358 | 0x01, 0x4B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x4B: "K" |
| 359 | 0x01, 0x4C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x4C: "L" |
| 360 | 0x01, 0x4D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x4D: "M" |
| 361 | 0x01, 0x4E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x4E: "N" |
| 362 | 0x01, 0x4F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x4F: "O" |
| 363 | 0x01, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x50: "P" |
| 364 | 0x01, 0x51, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x51: "Q" |
| 365 | 0x01, 0x52, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x52: "R" |
| 366 | 0x01, 0x53, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x53: "S" |
| 367 | 0x01, 0x54, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x54: "T" |
| 368 | 0x01, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x55: "U" |
| 369 | 0x01, 0x56, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x56: "V" |
| 370 | 0x01, 0x57, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x57: "W" |
| 371 | 0x01, 0x58, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x58: "X" |
| 372 | 0x01, 0x59, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x59: "Y" |
| 373 | 0x01, 0x5A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x5A: "Z" |
| 374 | 0x01, 0x5B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x5B: "[" |
| 375 | 0x02, 0x5C, 0x5C, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x5C: "\\\\" |
| 376 | 0x01, 0x5D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x5D: "]" |
| 377 | 0x01, 0x5E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x5E: "^" |
| 378 | 0x01, 0x5F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x5F: "_" |
| 379 | 0x01, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x60: "`" |
| 380 | 0x01, 0x61, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x61: "a" |
| 381 | 0x01, 0x62, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x62: "b" |
| 382 | 0x01, 0x63, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x63: "c" |
| 383 | 0x01, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x64: "d" |
| 384 | 0x01, 0x65, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x65: "e" |
| 385 | 0x01, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x66: "f" |
| 386 | 0x01, 0x67, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x67: "g" |
| 387 | 0x01, 0x68, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x68: "h" |
| 388 | 0x01, 0x69, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x69: "i" |
| 389 | 0x01, 0x6A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x6A: "j" |
| 390 | 0x01, 0x6B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x6B: "k" |
| 391 | 0x01, 0x6C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x6C: "l" |
| 392 | 0x01, 0x6D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x6D: "m" |
| 393 | 0x01, 0x6E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x6E: "n" |
| 394 | 0x01, 0x6F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x6F: "o" |
| 395 | 0x01, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x70: "p" |
| 396 | 0x01, 0x71, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x71: "q" |
| 397 | 0x01, 0x72, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x72: "r" |
| 398 | 0x01, 0x73, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x73: "s" |
| 399 | 0x01, 0x74, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x74: "t" |
| 400 | 0x01, 0x75, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x75: "u" |
| 401 | 0x01, 0x76, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x76: "v" |
| 402 | 0x01, 0x77, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x77: "w" |
| 403 | 0x01, 0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x78: "x" |
| 404 | 0x01, 0x79, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x79: "y" |
| 405 | 0x01, 0x7A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x7A: "z" |
| 406 | 0x01, 0x7B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x7B: "{" |
| 407 | 0x01, 0x7C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x7C: "|" |
| 408 | 0x01, 0x7D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x7D: "}" |
| 409 | 0x01, 0x7E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x7E: "~" |
| 410 | 0x01, 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x7F: "<DEL>" |
| 411 | }; |
| 412 | |
Nigel Tao | f3146c2 | 2020-03-26 08:47:42 +1100 | [diff] [blame] | 413 | // Wuffs allows either statically or dynamically allocated work buffers. This |
| 414 | // program exercises static allocation. |
| 415 | #define WORK_BUFFER_ARRAY_SIZE \ |
| 416 | WUFFS_JSON__DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE |
| 417 | #if WORK_BUFFER_ARRAY_SIZE > 0 |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 418 | uint8_t g_work_buffer_array[WORK_BUFFER_ARRAY_SIZE]; |
Nigel Tao | f3146c2 | 2020-03-26 08:47:42 +1100 | [diff] [blame] | 419 | #else |
| 420 | // Not all C/C++ compilers support 0-length arrays. |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 421 | uint8_t g_work_buffer_array[1]; |
Nigel Tao | f3146c2 | 2020-03-26 08:47:42 +1100 | [diff] [blame] | 422 | #endif |
| 423 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 424 | bool g_sandboxed = false; |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 425 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 426 | int g_input_file_descriptor = 0; // A 0 default means stdin. |
Nigel Tao | 01abc84 | 2020-03-06 21:42:33 +1100 | [diff] [blame] | 427 | |
Nigel Tao | 773994c | 2021-02-22 10:50:08 +1100 | [diff] [blame^] | 428 | #define TWO_NEW_LINES_THEN_256_SPACES \ |
| 429 | "\n\n " \ |
Nigel Tao | 0a0c7d6 | 2020-08-18 23:31:27 +1000 | [diff] [blame] | 430 | " " \ |
| 431 | " " \ |
Nigel Tao | 773994c | 2021-02-22 10:50:08 +1100 | [diff] [blame^] | 432 | " " |
| 433 | #define TWO_NEW_LINES_THEN_256_TABS \ |
| 434 | "\n\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" \ |
Nigel Tao | 0a0c7d6 | 2020-08-18 23:31:27 +1000 | [diff] [blame] | 435 | "\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" \ |
| 436 | "\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" \ |
| 437 | "\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" \ |
| 438 | "\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" \ |
| 439 | "\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" \ |
Nigel Tao | 773994c | 2021-02-22 10:50:08 +1100 | [diff] [blame^] | 440 | "\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" |
Nigel Tao | 0a0c7d6 | 2020-08-18 23:31:27 +1000 | [diff] [blame] | 441 | |
Nigel Tao | 773994c | 2021-02-22 10:50:08 +1100 | [diff] [blame^] | 442 | const char* g_two_new_lines_then_256_indent_bytes; |
Nigel Tao | 0a0c7d6 | 2020-08-18 23:31:27 +1000 | [diff] [blame] | 443 | uint32_t g_bytes_per_indent_depth; |
Nigel Tao | 107f0ef | 2020-03-01 21:35:02 +1100 | [diff] [blame] | 444 | |
Nigel Tao | fdac24a | 2020-03-06 21:53:08 +1100 | [diff] [blame] | 445 | #ifndef DST_BUFFER_ARRAY_SIZE |
| 446 | #define DST_BUFFER_ARRAY_SIZE (32 * 1024) |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 447 | #endif |
Nigel Tao | fdac24a | 2020-03-06 21:53:08 +1100 | [diff] [blame] | 448 | #ifndef SRC_BUFFER_ARRAY_SIZE |
| 449 | #define SRC_BUFFER_ARRAY_SIZE (32 * 1024) |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 450 | #endif |
Nigel Tao | 63e6796 | 2020-08-26 00:00:32 +1000 | [diff] [blame] | 451 | // 1 token is 8 bytes. 4Ki tokens is 32KiB. |
Nigel Tao | fdac24a | 2020-03-06 21:53:08 +1100 | [diff] [blame] | 452 | #ifndef TOKEN_BUFFER_ARRAY_SIZE |
| 453 | #define TOKEN_BUFFER_ARRAY_SIZE (4 * 1024) |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 454 | #endif |
| 455 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 456 | uint8_t g_dst_array[DST_BUFFER_ARRAY_SIZE]; |
| 457 | uint8_t g_src_array[SRC_BUFFER_ARRAY_SIZE]; |
| 458 | wuffs_base__token g_tok_array[TOKEN_BUFFER_ARRAY_SIZE]; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 459 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 460 | wuffs_base__io_buffer g_dst; |
| 461 | wuffs_base__io_buffer g_src; |
| 462 | wuffs_base__token_buffer g_tok; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 463 | |
Nigel Tao | 991bd51 | 2020-08-19 09:38:16 +1000 | [diff] [blame] | 464 | // g_cursor_index is the g_src.data.ptr index between the previous and current |
| 465 | // token. An invariant is that (g_cursor_index <= g_src.meta.ri). |
| 466 | size_t g_cursor_index; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 467 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 468 | uint32_t g_depth; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 469 | |
| 470 | enum class context { |
| 471 | none, |
| 472 | in_list_after_bracket, |
| 473 | in_list_after_value, |
| 474 | in_dict_after_brace, |
| 475 | in_dict_after_key, |
| 476 | in_dict_after_value, |
Nigel Tao | cd4cbc9 | 2020-09-22 22:22:15 +1000 | [diff] [blame] | 477 | end_of_data, |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 478 | } g_ctx; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 479 | |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 480 | bool // |
| 481 | in_dict_before_key() { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 482 | return (g_ctx == context::in_dict_after_brace) || |
| 483 | (g_ctx == context::in_dict_after_value); |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 484 | } |
| 485 | |
Nigel Tao | 773994c | 2021-02-22 10:50:08 +1100 | [diff] [blame^] | 486 | uint64_t g_num_input_blank_lines; |
| 487 | |
Nigel Tao | 2104205 | 2020-08-19 23:13:54 +1000 | [diff] [blame] | 488 | bool g_is_after_comment; |
| 489 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 490 | uint32_t g_suppress_write_dst; |
| 491 | bool g_wrote_to_dst; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 492 | |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 493 | wuffs_json__decoder g_dec; |
Nigel Tao | ea53245 | 2020-07-27 00:03:00 +1000 | [diff] [blame] | 494 | |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 495 | // ---- |
| 496 | |
| 497 | // Query is a JSON Pointer query. After initializing with a NUL-terminated C |
| 498 | // string, its multiple fragments are consumed as the program walks the JSON |
| 499 | // data from stdin. For example, letting "$" denote a NUL, suppose that we |
| 500 | // started with a query string of "/apple/banana/12/durian" and are currently |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 501 | // trying to match the second fragment, "banana", so that Query::m_depth is 2: |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 502 | // |
| 503 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 504 | // / a p p l e / b a n a n a / 1 2 / d u r i a n $ |
| 505 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 506 | // ^ ^ |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 507 | // m_frag_i m_frag_k |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 508 | // |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 509 | // The two pointers m_frag_i and m_frag_k (abbreviated as mfi and mfk) are the |
| 510 | // start (inclusive) and end (exclusive) of the query fragment. They satisfy |
| 511 | // (mfi <= mfk) and may be equal if the fragment empty (note that "" is a valid |
| 512 | // JSON object key). |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 513 | // |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 514 | // The m_frag_j (mfj) pointer moves between these two, or is nullptr. An |
| 515 | // invariant is that (((mfi <= mfj) && (mfj <= mfk)) || (mfj == nullptr)). |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 516 | // |
| 517 | // Wuffs' JSON tokenizer can portray a single JSON string as multiple Wuffs |
| 518 | // tokens, as backslash-escaped values within that JSON string may each get |
| 519 | // their own token. |
| 520 | // |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 521 | // At the start of each object key (a JSON string), mfj is set to mfi. |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 522 | // |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 523 | // While mfj remains non-nullptr, each token's unescaped contents are then |
| 524 | // compared to that part of the fragment from mfj to mfk. If it is a prefix |
| 525 | // (including the case of an exact match), then mfj is advanced by the |
| 526 | // unescaped length. Otherwise, mfj is set to nullptr. |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 527 | // |
| 528 | // Comparison accounts for JSON Pointer's escaping notation: "~0" and "~1" in |
| 529 | // the query (not the JSON value) are unescaped to "~" and "/" respectively. |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 530 | // "~n" and "~r" are also unescaped to "\n" and "\r". The program is |
| 531 | // responsible for calling Query::validate (with a strict_json_pointer_syntax |
| 532 | // argument) before otherwise using this class. |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 533 | // |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 534 | // The mfj pointer therefore advances from mfi to mfk, or drops out, as we |
| 535 | // incrementally match the object key with the query fragment. For example, if |
| 536 | // we have already matched the "ban" of "banana", then we would accept any of |
| 537 | // an "ana" token, an "a" token or a "\u0061" token, amongst others. They would |
| 538 | // advance mfj by 3, 1 or 1 bytes respectively. |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 539 | // |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 540 | // mfj |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 541 | // v |
| 542 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 543 | // / a p p l e / b a n a n a / 1 2 / d u r i a n $ |
| 544 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 545 | // ^ ^ |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 546 | // mfi mfk |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 547 | // |
| 548 | // At the end of each object key (or equivalently, at the start of each object |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 549 | // value), if mfj is non-nullptr and equal to (but not less than) mfk then we |
| 550 | // have a fragment match: the query fragment equals the object key. If there is |
| 551 | // a next fragment (in this example, "12") we move the frag_etc pointers to its |
| 552 | // start and end and increment Query::m_depth. Otherwise, we have matched the |
| 553 | // complete query, and the upcoming JSON value is the result of that query. |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 554 | // |
| 555 | // The discussion above centers on object keys. If the query fragment is |
| 556 | // numeric then it can also match as an array index: the string fragment "12" |
| 557 | // will match an array's 13th element (starting counting from zero). See RFC |
| 558 | // 6901 for its precise definition of an "array index" number. |
| 559 | // |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 560 | // Array index fragment match is represented by the Query::m_array_index field, |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 561 | // whose type (wuffs_base__result_u64) is a result type. An error result means |
| 562 | // that the fragment is not an array index. A value result holds the number of |
| 563 | // list elements remaining. When matching a query fragment in an array (instead |
| 564 | // of in an object), each element ticks this number down towards zero. At zero, |
| 565 | // the upcoming JSON value is the one that matches the query fragment. |
| 566 | class Query { |
| 567 | private: |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 568 | uint8_t* m_frag_i; |
| 569 | uint8_t* m_frag_j; |
| 570 | uint8_t* m_frag_k; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 571 | |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 572 | uint32_t m_depth; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 573 | |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 574 | wuffs_base__result_u64 m_array_index; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 575 | |
| 576 | public: |
| 577 | void reset(char* query_c_string) { |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 578 | m_frag_i = (uint8_t*)query_c_string; |
| 579 | m_frag_j = (uint8_t*)query_c_string; |
| 580 | m_frag_k = (uint8_t*)query_c_string; |
| 581 | m_depth = 0; |
| 582 | m_array_index.status.repr = "#main: not an array index query fragment"; |
| 583 | m_array_index.value = 0; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 584 | } |
| 585 | |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 586 | void restart_fragment(bool enable) { m_frag_j = enable ? m_frag_i : nullptr; } |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 587 | |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 588 | bool is_at(uint32_t depth) { return m_depth == depth; } |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 589 | |
| 590 | // tick returns whether the fragment is a valid array index whose value is |
| 591 | // zero. If valid but non-zero, it decrements it and returns false. |
| 592 | bool tick() { |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 593 | if (m_array_index.status.is_ok()) { |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 594 | if (m_array_index.value == 0) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 595 | return true; |
| 596 | } |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 597 | m_array_index.value--; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 598 | } |
| 599 | return false; |
| 600 | } |
| 601 | |
| 602 | // next_fragment moves to the next fragment, returning whether it existed. |
| 603 | bool next_fragment() { |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 604 | uint8_t* k = m_frag_k; |
| 605 | uint32_t d = m_depth; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 606 | |
| 607 | this->reset(nullptr); |
| 608 | |
| 609 | if (!k || (*k != '/')) { |
| 610 | return false; |
| 611 | } |
| 612 | k++; |
| 613 | |
| 614 | bool all_digits = true; |
| 615 | uint8_t* i = k; |
| 616 | while ((*k != '\x00') && (*k != '/')) { |
| 617 | all_digits = all_digits && ('0' <= *k) && (*k <= '9'); |
| 618 | k++; |
| 619 | } |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 620 | m_frag_i = i; |
| 621 | m_frag_j = i; |
| 622 | m_frag_k = k; |
| 623 | m_depth = d + 1; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 624 | if (all_digits) { |
| 625 | // wuffs_base__parse_number_u64 rejects leading zeroes, e.g. "00", "07". |
Nigel Tao | 6b7ce30 | 2020-07-07 16:19:46 +1000 | [diff] [blame] | 626 | m_array_index = wuffs_base__parse_number_u64( |
| 627 | wuffs_base__make_slice_u8(i, k - i), |
| 628 | WUFFS_BASE__PARSE_NUMBER_XXX__DEFAULT_OPTIONS); |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 629 | } |
| 630 | return true; |
| 631 | } |
| 632 | |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 633 | bool matched_all() { return m_frag_k == nullptr; } |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 634 | |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 635 | bool matched_fragment() { return m_frag_j && (m_frag_j == m_frag_k); } |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 636 | |
| 637 | void incremental_match_slice(uint8_t* ptr, size_t len) { |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 638 | if (!m_frag_j) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 639 | return; |
| 640 | } |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 641 | uint8_t* j = m_frag_j; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 642 | while (true) { |
| 643 | if (len == 0) { |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 644 | m_frag_j = j; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 645 | return; |
| 646 | } |
| 647 | |
| 648 | if (*j == '\x00') { |
| 649 | break; |
| 650 | |
| 651 | } else if (*j == '~') { |
| 652 | j++; |
| 653 | if (*j == '0') { |
| 654 | if (*ptr != '~') { |
| 655 | break; |
| 656 | } |
| 657 | } else if (*j == '1') { |
| 658 | if (*ptr != '/') { |
| 659 | break; |
| 660 | } |
Nigel Tao | d6fdfb1 | 2020-03-11 12:24:14 +1100 | [diff] [blame] | 661 | } else if (*j == 'n') { |
| 662 | if (*ptr != '\n') { |
| 663 | break; |
| 664 | } |
| 665 | } else if (*j == 'r') { |
| 666 | if (*ptr != '\r') { |
| 667 | break; |
| 668 | } |
Nigel Tao | 904004e | 2020-11-15 20:56:04 +1100 | [diff] [blame] | 669 | } else if (*j == 't') { |
| 670 | if (*ptr != '\t') { |
| 671 | break; |
| 672 | } |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 673 | } else { |
| 674 | break; |
| 675 | } |
| 676 | |
| 677 | } else if (*j != *ptr) { |
| 678 | break; |
| 679 | } |
| 680 | |
| 681 | j++; |
| 682 | ptr++; |
| 683 | len--; |
| 684 | } |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 685 | m_frag_j = nullptr; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | void incremental_match_code_point(uint32_t code_point) { |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 689 | if (!m_frag_j) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 690 | return; |
| 691 | } |
| 692 | uint8_t u[WUFFS_BASE__UTF_8__BYTE_LENGTH__MAX_INCL]; |
| 693 | size_t n = wuffs_base__utf_8__encode( |
| 694 | wuffs_base__make_slice_u8(&u[0], |
| 695 | WUFFS_BASE__UTF_8__BYTE_LENGTH__MAX_INCL), |
| 696 | code_point); |
| 697 | if (n > 0) { |
| 698 | this->incremental_match_slice(&u[0], n); |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | // validate returns whether the (ptr, len) arguments form a valid JSON |
| 703 | // Pointer. In particular, it must be valid UTF-8, and either be empty or |
| 704 | // start with a '/'. Any '~' within must immediately be followed by either |
Nigel Tao | d6fdfb1 | 2020-03-11 12:24:14 +1100 | [diff] [blame] | 705 | // '0' or '1'. If strict_json_pointer_syntax is false, a '~' may also be |
Nigel Tao | 904004e | 2020-11-15 20:56:04 +1100 | [diff] [blame] | 706 | // followed by either 'n', 'r' or 't'. |
Nigel Tao | d6fdfb1 | 2020-03-11 12:24:14 +1100 | [diff] [blame] | 707 | static bool validate(char* query_c_string, |
| 708 | size_t length, |
| 709 | bool strict_json_pointer_syntax) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 710 | if (length <= 0) { |
| 711 | return true; |
| 712 | } |
| 713 | if (query_c_string[0] != '/') { |
| 714 | return false; |
| 715 | } |
| 716 | wuffs_base__slice_u8 s = |
| 717 | wuffs_base__make_slice_u8((uint8_t*)query_c_string, length); |
| 718 | bool previous_was_tilde = false; |
| 719 | while (s.len > 0) { |
Nigel Tao | 702c7b2 | 2020-07-22 15:42:54 +1000 | [diff] [blame] | 720 | wuffs_base__utf_8__next__output o = wuffs_base__utf_8__next(s.ptr, s.len); |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 721 | if (!o.is_valid()) { |
| 722 | return false; |
| 723 | } |
Nigel Tao | d6fdfb1 | 2020-03-11 12:24:14 +1100 | [diff] [blame] | 724 | |
| 725 | if (previous_was_tilde) { |
| 726 | switch (o.code_point) { |
| 727 | case '0': |
| 728 | case '1': |
| 729 | break; |
| 730 | case 'n': |
| 731 | case 'r': |
Nigel Tao | 904004e | 2020-11-15 20:56:04 +1100 | [diff] [blame] | 732 | case 't': |
Nigel Tao | d6fdfb1 | 2020-03-11 12:24:14 +1100 | [diff] [blame] | 733 | if (strict_json_pointer_syntax) { |
| 734 | return false; |
| 735 | } |
| 736 | break; |
| 737 | default: |
| 738 | return false; |
| 739 | } |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 740 | } |
| 741 | previous_was_tilde = o.code_point == '~'; |
Nigel Tao | d6fdfb1 | 2020-03-11 12:24:14 +1100 | [diff] [blame] | 742 | |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 743 | s.ptr += o.byte_length; |
| 744 | s.len -= o.byte_length; |
| 745 | } |
| 746 | return !previous_was_tilde; |
| 747 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 748 | } g_query; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 749 | |
| 750 | // ---- |
| 751 | |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 752 | struct { |
| 753 | int remaining_argc; |
| 754 | char** remaining_argv; |
| 755 | |
Nigel Tao | 3690e83 | 2020-03-12 16:52:26 +1100 | [diff] [blame] | 756 | bool compact_output; |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 757 | bool fail_if_unsandboxed; |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 758 | bool input_allow_comments; |
| 759 | bool input_allow_extra_comma; |
| 760 | bool input_allow_inf_nan_numbers; |
Nigel Tao | 2104205 | 2020-08-19 23:13:54 +1000 | [diff] [blame] | 761 | bool output_comments; |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 762 | bool output_extra_comma; |
Nigel Tao | 7568254 | 2020-08-22 21:40:18 +1000 | [diff] [blame] | 763 | bool output_inf_nan_numbers; |
Nigel Tao | d6fdfb1 | 2020-03-11 12:24:14 +1100 | [diff] [blame] | 764 | bool strict_json_pointer_syntax; |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 765 | bool tabs; |
Nigel Tao | 0a0c7d6 | 2020-08-18 23:31:27 +1000 | [diff] [blame] | 766 | |
| 767 | uint32_t max_output_depth; |
| 768 | uint32_t spaces; |
| 769 | |
| 770 | char* query_c_string; |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 771 | } g_flags = {0}; |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 772 | |
| 773 | const char* // |
| 774 | parse_flags(int argc, char** argv) { |
Nigel Tao | ecadf72 | 2020-07-13 08:22:34 +1000 | [diff] [blame] | 775 | g_flags.spaces = 4; |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 776 | g_flags.max_output_depth = 0xFFFFFFFF; |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 777 | |
| 778 | int c = (argc > 0) ? 1 : 0; // Skip argv[0], the program name. |
| 779 | for (; c < argc; c++) { |
| 780 | char* arg = argv[c]; |
| 781 | if (*arg++ != '-') { |
| 782 | break; |
| 783 | } |
| 784 | |
| 785 | // A double-dash "--foo" is equivalent to a single-dash "-foo". As special |
| 786 | // cases, a bare "-" is not a flag (some programs may interpret it as |
| 787 | // stdin) and a bare "--" means to stop parsing flags. |
| 788 | if (*arg == '\x00') { |
| 789 | break; |
| 790 | } else if (*arg == '-') { |
| 791 | arg++; |
| 792 | if (*arg == '\x00') { |
| 793 | c++; |
| 794 | break; |
| 795 | } |
| 796 | } |
| 797 | |
Nigel Tao | 3690e83 | 2020-03-12 16:52:26 +1100 | [diff] [blame] | 798 | if (!strcmp(arg, "c") || !strcmp(arg, "compact-output")) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 799 | g_flags.compact_output = true; |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 800 | continue; |
| 801 | } |
Nigel Tao | 94440cf | 2020-04-02 22:28:24 +1100 | [diff] [blame] | 802 | if (!strcmp(arg, "d") || !strcmp(arg, "max-output-depth")) { |
| 803 | g_flags.max_output_depth = 1; |
| 804 | continue; |
| 805 | } else if (!strncmp(arg, "d=", 2) || |
| 806 | !strncmp(arg, "max-output-depth=", 16)) { |
| 807 | while (*arg++ != '=') { |
| 808 | } |
| 809 | wuffs_base__result_u64 u = wuffs_base__parse_number_u64( |
Nigel Tao | 6b7ce30 | 2020-07-07 16:19:46 +1000 | [diff] [blame] | 810 | wuffs_base__make_slice_u8((uint8_t*)arg, strlen(arg)), |
| 811 | WUFFS_BASE__PARSE_NUMBER_XXX__DEFAULT_OPTIONS); |
Nigel Tao | af75772 | 2020-07-18 17:27:11 +1000 | [diff] [blame] | 812 | if (u.status.is_ok() && (u.value <= 0xFFFFFFFF)) { |
Nigel Tao | 94440cf | 2020-04-02 22:28:24 +1100 | [diff] [blame] | 813 | g_flags.max_output_depth = (uint32_t)(u.value); |
| 814 | continue; |
| 815 | } |
| 816 | return g_usage; |
| 817 | } |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 818 | if (!strcmp(arg, "fail-if-unsandboxed")) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 819 | g_flags.fail_if_unsandboxed = true; |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 820 | continue; |
| 821 | } |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 822 | if (!strcmp(arg, "input-allow-comments")) { |
| 823 | g_flags.input_allow_comments = true; |
Nigel Tao | 4e19359 | 2020-07-15 12:48:57 +1000 | [diff] [blame] | 824 | continue; |
| 825 | } |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 826 | if (!strcmp(arg, "input-allow-extra-comma")) { |
| 827 | g_flags.input_allow_extra_comma = true; |
Nigel Tao | 4e19359 | 2020-07-15 12:48:57 +1000 | [diff] [blame] | 828 | continue; |
| 829 | } |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 830 | if (!strcmp(arg, "input-allow-inf-nan-numbers")) { |
| 831 | g_flags.input_allow_inf_nan_numbers = true; |
Nigel Tao | 3c8589b | 2020-07-19 21:49:00 +1000 | [diff] [blame] | 832 | continue; |
| 833 | } |
Nigel Tao | 2104205 | 2020-08-19 23:13:54 +1000 | [diff] [blame] | 834 | if (!strcmp(arg, "jwcc")) { |
| 835 | g_flags.input_allow_comments = true; |
| 836 | g_flags.input_allow_extra_comma = true; |
| 837 | g_flags.output_comments = true; |
| 838 | g_flags.output_extra_comma = true; |
| 839 | continue; |
| 840 | } |
| 841 | if (!strcmp(arg, "output-comments")) { |
| 842 | g_flags.output_comments = true; |
| 843 | continue; |
| 844 | } |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 845 | if (!strcmp(arg, "output-extra-comma")) { |
| 846 | g_flags.output_extra_comma = true; |
Nigel Tao | dd11469 | 2020-07-25 21:54:12 +1000 | [diff] [blame] | 847 | continue; |
| 848 | } |
Nigel Tao | 7568254 | 2020-08-22 21:40:18 +1000 | [diff] [blame] | 849 | if (!strcmp(arg, "output-inf-nan-numbers")) { |
| 850 | g_flags.output_inf_nan_numbers = true; |
| 851 | continue; |
| 852 | } |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 853 | if (!strncmp(arg, "q=", 2) || !strncmp(arg, "query=", 6)) { |
| 854 | while (*arg++ != '=') { |
| 855 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 856 | g_flags.query_c_string = arg; |
Nigel Tao | d6fdfb1 | 2020-03-11 12:24:14 +1100 | [diff] [blame] | 857 | continue; |
| 858 | } |
Nigel Tao | ecadf72 | 2020-07-13 08:22:34 +1000 | [diff] [blame] | 859 | if (!strncmp(arg, "s=", 2) || !strncmp(arg, "spaces=", 7)) { |
| 860 | while (*arg++ != '=') { |
| 861 | } |
| 862 | if (('0' <= arg[0]) && (arg[0] <= '8') && (arg[1] == '\x00')) { |
| 863 | g_flags.spaces = arg[0] - '0'; |
| 864 | continue; |
| 865 | } |
| 866 | return g_usage; |
| 867 | } |
| 868 | if (!strcmp(arg, "strict-json-pointer-syntax")) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 869 | g_flags.strict_json_pointer_syntax = true; |
Nigel Tao | d6fdfb1 | 2020-03-11 12:24:14 +1100 | [diff] [blame] | 870 | continue; |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 871 | } |
| 872 | if (!strcmp(arg, "t") || !strcmp(arg, "tabs")) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 873 | g_flags.tabs = true; |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 874 | continue; |
| 875 | } |
| 876 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 877 | return g_usage; |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 878 | } |
| 879 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 880 | if (g_flags.query_c_string && |
| 881 | !Query::validate(g_flags.query_c_string, strlen(g_flags.query_c_string), |
| 882 | g_flags.strict_json_pointer_syntax)) { |
Nigel Tao | d6fdfb1 | 2020-03-11 12:24:14 +1100 | [diff] [blame] | 883 | return "main: bad JSON Pointer (RFC 6901) syntax for the -query=STR flag"; |
| 884 | } |
| 885 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 886 | g_flags.remaining_argc = argc - c; |
| 887 | g_flags.remaining_argv = argv + c; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 888 | return nullptr; |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 889 | } |
| 890 | |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 891 | const char* // |
| 892 | initialize_globals(int argc, char** argv) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 893 | g_dst = wuffs_base__make_io_buffer( |
| 894 | wuffs_base__make_slice_u8(g_dst_array, DST_BUFFER_ARRAY_SIZE), |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 895 | wuffs_base__empty_io_buffer_meta()); |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 896 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 897 | g_src = wuffs_base__make_io_buffer( |
| 898 | wuffs_base__make_slice_u8(g_src_array, SRC_BUFFER_ARRAY_SIZE), |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 899 | wuffs_base__empty_io_buffer_meta()); |
| 900 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 901 | g_tok = wuffs_base__make_token_buffer( |
| 902 | wuffs_base__make_slice_token(g_tok_array, TOKEN_BUFFER_ARRAY_SIZE), |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 903 | wuffs_base__empty_token_buffer_meta()); |
| 904 | |
Nigel Tao | 991bd51 | 2020-08-19 09:38:16 +1000 | [diff] [blame] | 905 | g_cursor_index = 0; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 906 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 907 | g_depth = 0; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 908 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 909 | g_ctx = context::none; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 910 | |
Nigel Tao | 773994c | 2021-02-22 10:50:08 +1100 | [diff] [blame^] | 911 | g_num_input_blank_lines = 0; |
| 912 | |
Nigel Tao | 2104205 | 2020-08-19 23:13:54 +1000 | [diff] [blame] | 913 | g_is_after_comment = false; |
| 914 | |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 915 | TRY(parse_flags(argc, argv)); |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 916 | if (g_flags.fail_if_unsandboxed && !g_sandboxed) { |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 917 | return "main: unsandboxed"; |
| 918 | } |
Nigel Tao | 2104205 | 2020-08-19 23:13:54 +1000 | [diff] [blame] | 919 | if (g_flags.output_comments && !g_flags.compact_output && |
| 920 | !g_flags.output_extra_comma) { |
| 921 | return "main: -output-comments requires one or both of -compact-output and " |
| 922 | "-output-extra-comma"; |
| 923 | } |
Nigel Tao | 7568254 | 2020-08-22 21:40:18 +1000 | [diff] [blame] | 924 | if (g_flags.input_allow_inf_nan_numbers && !g_flags.output_inf_nan_numbers) { |
| 925 | return "main: -input-allow-inf-nan-numbers requires " |
| 926 | "-output-inf-nan-numbers"; |
| 927 | } |
Nigel Tao | 01abc84 | 2020-03-06 21:42:33 +1100 | [diff] [blame] | 928 | const int stdin_fd = 0; |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 929 | if (g_flags.remaining_argc > |
| 930 | ((g_input_file_descriptor != stdin_fd) ? 1 : 0)) { |
| 931 | return g_usage; |
Nigel Tao | 107f0ef | 2020-03-01 21:35:02 +1100 | [diff] [blame] | 932 | } |
| 933 | |
Nigel Tao | 773994c | 2021-02-22 10:50:08 +1100 | [diff] [blame^] | 934 | g_two_new_lines_then_256_indent_bytes = g_flags.tabs |
| 935 | ? TWO_NEW_LINES_THEN_256_TABS |
| 936 | : TWO_NEW_LINES_THEN_256_SPACES; |
Nigel Tao | 0a0c7d6 | 2020-08-18 23:31:27 +1000 | [diff] [blame] | 937 | g_bytes_per_indent_depth = g_flags.tabs ? 1 : g_flags.spaces; |
| 938 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 939 | g_query.reset(g_flags.query_c_string); |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 940 | |
Nigel Tao | c96b31c | 2020-07-27 22:37:23 +1000 | [diff] [blame] | 941 | // If the query is non-empty, suppress writing to stdout until we've |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 942 | // completed the query. |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 943 | g_suppress_write_dst = g_query.next_fragment() ? 1 : 0; |
| 944 | g_wrote_to_dst = false; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 945 | |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 946 | TRY(g_dec.initialize(sizeof__wuffs_json__decoder(), WUFFS_VERSION, 0) |
| 947 | .message()); |
Nigel Tao | 4b186b0 | 2020-03-18 14:25:21 +1100 | [diff] [blame] | 948 | |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 949 | if (g_flags.input_allow_comments) { |
| 950 | g_dec.set_quirk_enabled(WUFFS_JSON__QUIRK_ALLOW_COMMENT_BLOCK, true); |
| 951 | g_dec.set_quirk_enabled(WUFFS_JSON__QUIRK_ALLOW_COMMENT_LINE, true); |
Nigel Tao | 3c8589b | 2020-07-19 21:49:00 +1000 | [diff] [blame] | 952 | } |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 953 | if (g_flags.input_allow_extra_comma) { |
| 954 | g_dec.set_quirk_enabled(WUFFS_JSON__QUIRK_ALLOW_EXTRA_COMMA, true); |
Nigel Tao | c766bb7 | 2020-07-09 12:59:32 +1000 | [diff] [blame] | 955 | } |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 956 | if (g_flags.input_allow_inf_nan_numbers) { |
| 957 | g_dec.set_quirk_enabled(WUFFS_JSON__QUIRK_ALLOW_INF_NAN_NUMBERS, true); |
Nigel Tao | 51a3829 | 2020-07-19 22:43:17 +1000 | [diff] [blame] | 958 | } |
Nigel Tao | c766bb7 | 2020-07-09 12:59:32 +1000 | [diff] [blame] | 959 | |
Nigel Tao | cd4cbc9 | 2020-09-22 22:22:15 +1000 | [diff] [blame] | 960 | // Consume any optional trailing whitespace and comments. This isn't part of |
| 961 | // the JSON spec, but it works better with line oriented Unix tools (such as |
| 962 | // "echo 123 | jsonptr" where it's "echo", not "echo -n") or hand-edited JSON |
| 963 | // files which can accidentally contain trailing whitespace. |
| 964 | g_dec.set_quirk_enabled(WUFFS_JSON__QUIRK_ALLOW_TRAILING_FILLER, true); |
Nigel Tao | 4b186b0 | 2020-03-18 14:25:21 +1100 | [diff] [blame] | 965 | |
| 966 | return nullptr; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 967 | } |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 968 | |
| 969 | // ---- |
| 970 | |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 971 | // ignore_return_value suppresses errors from -Wall -Werror. |
| 972 | static void // |
| 973 | ignore_return_value(int ignored) {} |
| 974 | |
Nigel Tao | 2914bae | 2020-02-26 09:40:30 +1100 | [diff] [blame] | 975 | const char* // |
| 976 | read_src() { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 977 | if (g_src.meta.closed) { |
Nigel Tao | 9cc2c25 | 2020-02-23 17:05:49 +1100 | [diff] [blame] | 978 | return "main: internal error: read requested on a closed source"; |
Nigel Tao | a840692 | 2020-02-19 12:22:00 +1100 | [diff] [blame] | 979 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 980 | g_src.compact(); |
| 981 | if (g_src.meta.wi >= g_src.data.len) { |
| 982 | return "main: g_src buffer is full"; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 983 | } |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 984 | while (true) { |
Nigel Tao | d6a10df | 2020-07-27 11:47:47 +1000 | [diff] [blame] | 985 | ssize_t n = read(g_input_file_descriptor, g_src.writer_pointer(), |
| 986 | g_src.writer_length()); |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 987 | if (n >= 0) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 988 | g_src.meta.wi += n; |
| 989 | g_src.meta.closed = n == 0; |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 990 | break; |
| 991 | } else if (errno != EINTR) { |
| 992 | return strerror(errno); |
| 993 | } |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 994 | } |
| 995 | return nullptr; |
| 996 | } |
| 997 | |
Nigel Tao | 2914bae | 2020-02-26 09:40:30 +1100 | [diff] [blame] | 998 | const char* // |
| 999 | flush_dst() { |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 1000 | while (true) { |
Nigel Tao | d6a10df | 2020-07-27 11:47:47 +1000 | [diff] [blame] | 1001 | size_t n = g_dst.reader_length(); |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 1002 | if (n == 0) { |
| 1003 | break; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1004 | } |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 1005 | const int stdout_fd = 1; |
Nigel Tao | d6a10df | 2020-07-27 11:47:47 +1000 | [diff] [blame] | 1006 | ssize_t i = write(stdout_fd, g_dst.reader_pointer(), n); |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 1007 | if (i >= 0) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1008 | g_dst.meta.ri += i; |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 1009 | } else if (errno != EINTR) { |
| 1010 | return strerror(errno); |
| 1011 | } |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1012 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1013 | g_dst.compact(); |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1014 | return nullptr; |
| 1015 | } |
| 1016 | |
Nigel Tao | 2914bae | 2020-02-26 09:40:30 +1100 | [diff] [blame] | 1017 | const char* // |
Nigel Tao | 6b86cbc | 2020-08-19 11:39:56 +1000 | [diff] [blame] | 1018 | write_dst_slow(const void* s, size_t n) { |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1019 | const uint8_t* p = static_cast<const uint8_t*>(s); |
| 1020 | while (n > 0) { |
Nigel Tao | d6a10df | 2020-07-27 11:47:47 +1000 | [diff] [blame] | 1021 | size_t i = g_dst.writer_length(); |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1022 | if (i == 0) { |
| 1023 | const char* z = flush_dst(); |
| 1024 | if (z) { |
| 1025 | return z; |
| 1026 | } |
Nigel Tao | d6a10df | 2020-07-27 11:47:47 +1000 | [diff] [blame] | 1027 | i = g_dst.writer_length(); |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1028 | if (i == 0) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1029 | return "main: g_dst buffer is full"; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1030 | } |
| 1031 | } |
| 1032 | |
| 1033 | if (i > n) { |
| 1034 | i = n; |
| 1035 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1036 | memcpy(g_dst.data.ptr + g_dst.meta.wi, p, i); |
| 1037 | g_dst.meta.wi += i; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1038 | p += i; |
| 1039 | n -= i; |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1040 | g_wrote_to_dst = true; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1041 | } |
| 1042 | return nullptr; |
| 1043 | } |
| 1044 | |
Nigel Tao | 6b86cbc | 2020-08-19 11:39:56 +1000 | [diff] [blame] | 1045 | inline const char* // |
| 1046 | write_dst(const void* s, size_t n) { |
| 1047 | if (g_suppress_write_dst > 0) { |
| 1048 | return nullptr; |
| 1049 | } else if (n <= (DST_BUFFER_ARRAY_SIZE - g_dst.meta.wi)) { |
| 1050 | memcpy(g_dst.data.ptr + g_dst.meta.wi, s, n); |
| 1051 | g_dst.meta.wi += n; |
| 1052 | g_wrote_to_dst = true; |
| 1053 | return nullptr; |
| 1054 | } |
| 1055 | return write_dst_slow(s, n); |
| 1056 | } |
| 1057 | |
Nigel Tao | 773994c | 2021-02-22 10:50:08 +1100 | [diff] [blame^] | 1058 | #define TRY_INDENT_WITH_LEADING_NEW_LINE \ |
| 1059 | do { \ |
| 1060 | uint32_t adj = (g_num_input_blank_lines > 1) ? 1 : 0; \ |
| 1061 | g_num_input_blank_lines = 0; \ |
| 1062 | uint32_t indent = g_depth * g_bytes_per_indent_depth; \ |
| 1063 | TRY(write_dst(g_two_new_lines_then_256_indent_bytes + 1 - adj, \ |
| 1064 | 1 + adj + (indent & 0xFF))); \ |
| 1065 | for (indent >>= 8; indent > 0; indent--) { \ |
| 1066 | TRY(write_dst(g_two_new_lines_then_256_indent_bytes + 2, 0x100)); \ |
| 1067 | } \ |
Nigel Tao | 2104205 | 2020-08-19 23:13:54 +1000 | [diff] [blame] | 1068 | } while (false) |
| 1069 | |
| 1070 | // TRY_INDENT_SANS_LEADING_NEW_LINE is used after comments, which print their |
| 1071 | // own "\n". |
Nigel Tao | 773994c | 2021-02-22 10:50:08 +1100 | [diff] [blame^] | 1072 | #define TRY_INDENT_SANS_LEADING_NEW_LINE \ |
| 1073 | do { \ |
| 1074 | uint32_t adj = (g_num_input_blank_lines > 1) ? 1 : 0; \ |
| 1075 | g_num_input_blank_lines = 0; \ |
| 1076 | uint32_t indent = g_depth * g_bytes_per_indent_depth; \ |
| 1077 | TRY(write_dst(g_two_new_lines_then_256_indent_bytes + 2 - adj, \ |
| 1078 | adj + (indent & 0xFF))); \ |
| 1079 | for (indent >>= 8; indent > 0; indent--) { \ |
| 1080 | TRY(write_dst(g_two_new_lines_then_256_indent_bytes + 2, 0x100)); \ |
| 1081 | } \ |
Nigel Tao | 2104205 | 2020-08-19 23:13:54 +1000 | [diff] [blame] | 1082 | } while (false) |
| 1083 | |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1084 | // ---- |
| 1085 | |
Nigel Tao | 2914bae | 2020-02-26 09:40:30 +1100 | [diff] [blame] | 1086 | const char* // |
Nigel Tao | 7cb7654 | 2020-07-19 22:19:04 +1000 | [diff] [blame] | 1087 | handle_unicode_code_point(uint32_t ucp) { |
Nigel Tao | 6344181 | 2020-08-21 14:05:48 +1000 | [diff] [blame] | 1088 | if (ucp < 0x80) { |
| 1089 | return write_dst(&ascii_escapes[8 * ucp + 1], ascii_escapes[8 * ucp]); |
Nigel Tao | 7cb7654 | 2020-07-19 22:19:04 +1000 | [diff] [blame] | 1090 | } |
Nigel Tao | 7cb7654 | 2020-07-19 22:19:04 +1000 | [diff] [blame] | 1091 | uint8_t u[WUFFS_BASE__UTF_8__BYTE_LENGTH__MAX_INCL]; |
| 1092 | size_t n = wuffs_base__utf_8__encode( |
| 1093 | wuffs_base__make_slice_u8(&u[0], |
| 1094 | WUFFS_BASE__UTF_8__BYTE_LENGTH__MAX_INCL), |
| 1095 | ucp); |
| 1096 | if (n == 0) { |
| 1097 | return "main: internal error: unexpected Unicode code point"; |
| 1098 | } |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 1099 | return write_dst(&u[0], n); |
Nigel Tao | 168f60a | 2020-07-14 13:19:33 +1000 | [diff] [blame] | 1100 | } |
| 1101 | |
Nigel Tao | d191a3f | 2020-07-19 22:14:54 +1000 | [diff] [blame] | 1102 | // ---- |
| 1103 | |
Nigel Tao | 50db4a4 | 2020-08-20 11:31:28 +1000 | [diff] [blame] | 1104 | inline const char* // |
Nigel Tao | 2ef3999 | 2020-04-09 17:24:39 +1000 | [diff] [blame] | 1105 | handle_token(wuffs_base__token t, bool start_of_token_chain) { |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1106 | do { |
Nigel Tao | 462f866 | 2020-04-01 23:01:51 +1100 | [diff] [blame] | 1107 | int64_t vbc = t.value_base_category(); |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1108 | uint64_t vbd = t.value_base_detail(); |
Nigel Tao | ee6927f | 2020-07-27 12:08:33 +1000 | [diff] [blame] | 1109 | uint64_t token_length = t.length(); |
Nigel Tao | 991bd51 | 2020-08-19 09:38:16 +1000 | [diff] [blame] | 1110 | // The "- token_length" is because we incremented g_cursor_index before |
| 1111 | // calling handle_token. |
Nigel Tao | ee6927f | 2020-07-27 12:08:33 +1000 | [diff] [blame] | 1112 | wuffs_base__slice_u8 tok = wuffs_base__make_slice_u8( |
Nigel Tao | 991bd51 | 2020-08-19 09:38:16 +1000 | [diff] [blame] | 1113 | g_src.data.ptr + g_cursor_index - token_length, token_length); |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1114 | |
| 1115 | // Handle ']' or '}'. |
Nigel Tao | 9f7a250 | 2020-02-23 09:42:02 +1100 | [diff] [blame] | 1116 | if ((vbc == WUFFS_BASE__TOKEN__VBC__STRUCTURE) && |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1117 | (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__POP)) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1118 | if (g_query.is_at(g_depth)) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1119 | return "main: no match for query"; |
| 1120 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1121 | if (g_depth <= 0) { |
| 1122 | return "main: internal error: inconsistent g_depth"; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1123 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1124 | g_depth--; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1125 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1126 | if (g_query.matched_all() && (g_depth >= g_flags.max_output_depth)) { |
| 1127 | g_suppress_write_dst--; |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 1128 | // '…' is U+2026 HORIZONTAL ELLIPSIS, which is 3 UTF-8 bytes. |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 1129 | TRY(write_dst((vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__FROM_LIST) |
| 1130 | ? "\"[…]\"" |
| 1131 | : "\"{…}\"", |
| 1132 | 7)); |
| 1133 | } else { |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 1134 | // Write preceding whitespace. |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1135 | if ((g_ctx != context::in_list_after_bracket) && |
| 1136 | (g_ctx != context::in_dict_after_brace) && |
| 1137 | !g_flags.compact_output) { |
Nigel Tao | 2104205 | 2020-08-19 23:13:54 +1000 | [diff] [blame] | 1138 | if (g_is_after_comment) { |
| 1139 | TRY_INDENT_SANS_LEADING_NEW_LINE; |
| 1140 | } else { |
| 1141 | if (g_flags.output_extra_comma) { |
| 1142 | TRY(write_dst(",", 1)); |
| 1143 | } |
| 1144 | TRY_INDENT_WITH_LEADING_NEW_LINE; |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 1145 | } |
Nigel Tao | 773994c | 2021-02-22 10:50:08 +1100 | [diff] [blame^] | 1146 | } else { |
| 1147 | g_num_input_blank_lines = 0; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1148 | } |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 1149 | |
| 1150 | TRY(write_dst( |
| 1151 | (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__FROM_LIST) ? "]" : "}", |
| 1152 | 1)); |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1153 | } |
| 1154 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1155 | g_ctx = (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_LIST) |
| 1156 | ? context::in_list_after_value |
| 1157 | : context::in_dict_after_key; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1158 | goto after_value; |
| 1159 | } |
| 1160 | |
Nigel Tao | d1c928a | 2020-02-28 12:43:53 +1100 | [diff] [blame] | 1161 | // Write preceding whitespace and punctuation, if it wasn't ']', '}' or a |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 1162 | // continuation of a multi-token chain. |
| 1163 | if (start_of_token_chain) { |
Nigel Tao | 2104205 | 2020-08-19 23:13:54 +1000 | [diff] [blame] | 1164 | if (g_is_after_comment) { |
| 1165 | TRY_INDENT_SANS_LEADING_NEW_LINE; |
| 1166 | } else if (g_ctx == context::in_dict_after_key) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1167 | TRY(write_dst(": ", g_flags.compact_output ? 1 : 2)); |
| 1168 | } else if (g_ctx != context::none) { |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 1169 | if ((g_ctx != context::in_list_after_bracket) && |
| 1170 | (g_ctx != context::in_dict_after_brace)) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1171 | TRY(write_dst(",", 1)); |
Nigel Tao | 107f0ef | 2020-03-01 21:35:02 +1100 | [diff] [blame] | 1172 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1173 | if (!g_flags.compact_output) { |
Nigel Tao | 2104205 | 2020-08-19 23:13:54 +1000 | [diff] [blame] | 1174 | TRY_INDENT_WITH_LEADING_NEW_LINE; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1175 | } |
| 1176 | } |
| 1177 | |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 1178 | bool query_matched_fragment = false; |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1179 | if (g_query.is_at(g_depth)) { |
| 1180 | switch (g_ctx) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1181 | case context::in_list_after_bracket: |
| 1182 | case context::in_list_after_value: |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1183 | query_matched_fragment = g_query.tick(); |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1184 | break; |
| 1185 | case context::in_dict_after_key: |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1186 | query_matched_fragment = g_query.matched_fragment(); |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1187 | break; |
Nigel Tao | 18ef5b4 | 2020-03-16 10:37:47 +1100 | [diff] [blame] | 1188 | default: |
| 1189 | break; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1190 | } |
| 1191 | } |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 1192 | if (!query_matched_fragment) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1193 | // No-op. |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1194 | } else if (!g_query.next_fragment()) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1195 | // There is no next fragment. We have matched the complete query, and |
| 1196 | // the upcoming JSON value is the result of that query. |
| 1197 | // |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1198 | // Un-suppress writing to stdout and reset the g_ctx and g_depth as if |
| 1199 | // we were about to decode a top-level value. This makes any subsequent |
| 1200 | // indentation be relative to this point, and we will return g_eod |
| 1201 | // after the upcoming JSON value is complete. |
| 1202 | if (g_suppress_write_dst != 1) { |
| 1203 | return "main: internal error: inconsistent g_suppress_write_dst"; |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 1204 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1205 | g_suppress_write_dst = 0; |
| 1206 | g_ctx = context::none; |
| 1207 | g_depth = 0; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1208 | } else if ((vbc != WUFFS_BASE__TOKEN__VBC__STRUCTURE) || |
| 1209 | !(vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__PUSH)) { |
| 1210 | // The query has moved on to the next fragment but the upcoming JSON |
| 1211 | // value is not a container. |
| 1212 | return "main: no match for query"; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1213 | } |
| 1214 | } |
| 1215 | |
| 1216 | // Handle the token itself: either a container ('[' or '{') or a simple |
Nigel Tao | 85fba7f | 2020-02-29 16:28:06 +1100 | [diff] [blame] | 1217 | // value: string (a chain of raw or escaped parts), literal or number. |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1218 | switch (vbc) { |
Nigel Tao | 85fba7f | 2020-02-29 16:28:06 +1100 | [diff] [blame] | 1219 | case WUFFS_BASE__TOKEN__VBC__STRUCTURE: |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1220 | if (g_query.matched_all() && (g_depth >= g_flags.max_output_depth)) { |
| 1221 | g_suppress_write_dst++; |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 1222 | } else { |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 1223 | TRY(write_dst( |
| 1224 | (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_LIST) ? "[" : "{", |
| 1225 | 1)); |
| 1226 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1227 | g_depth++; |
| 1228 | g_ctx = (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_LIST) |
| 1229 | ? context::in_list_after_bracket |
| 1230 | : context::in_dict_after_brace; |
Nigel Tao | 773994c | 2021-02-22 10:50:08 +1100 | [diff] [blame^] | 1231 | g_num_input_blank_lines = 0; |
Nigel Tao | 85fba7f | 2020-02-29 16:28:06 +1100 | [diff] [blame] | 1232 | return nullptr; |
| 1233 | |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1234 | case WUFFS_BASE__TOKEN__VBC__STRING: |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 1235 | if (start_of_token_chain) { |
| 1236 | TRY(write_dst("\"", 1)); |
| 1237 | g_query.restart_fragment(in_dict_before_key() && |
| 1238 | g_query.is_at(g_depth)); |
| 1239 | } |
| 1240 | |
Nigel Tao | ade0165 | 2020-08-21 15:57:51 +1000 | [diff] [blame] | 1241 | if (vbd & WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_1_DST_1_SRC_COPY) { |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 1242 | TRY(write_dst(tok.ptr, tok.len)); |
| 1243 | g_query.incremental_match_slice(tok.ptr, tok.len); |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 1244 | } |
| 1245 | |
Nigel Tao | 496e88b | 2020-04-09 22:10:08 +1000 | [diff] [blame] | 1246 | if (t.continued()) { |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1247 | return nullptr; |
| 1248 | } |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 1249 | TRY(write_dst("\"", 1)); |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1250 | goto after_value; |
| 1251 | |
| 1252 | case WUFFS_BASE__TOKEN__VBC__UNICODE_CODE_POINT: |
Nigel Tao | 496e88b | 2020-04-09 22:10:08 +1000 | [diff] [blame] | 1253 | if (!t.continued()) { |
| 1254 | return "main: internal error: unexpected non-continued UCP token"; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1255 | } |
| 1256 | TRY(handle_unicode_code_point(vbd)); |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1257 | g_query.incremental_match_code_point(vbd); |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1258 | return nullptr; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1259 | } |
| 1260 | |
Nigel Tao | 3f688b2 | 2020-08-21 15:51:48 +1000 | [diff] [blame] | 1261 | // We have a literal or a number. |
| 1262 | TRY(write_dst(tok.ptr, tok.len)); |
| 1263 | goto after_value; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1264 | } while (0); |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1265 | |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1266 | // Book-keeping after completing a value (whether a container value or a |
| 1267 | // simple value). Empty parent containers are no longer empty. If the parent |
| 1268 | // container is a "{...}" object, toggle between keys and values. |
| 1269 | after_value: |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1270 | if (g_depth == 0) { |
| 1271 | return g_eod; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1272 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1273 | switch (g_ctx) { |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1274 | case context::in_list_after_bracket: |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1275 | g_ctx = context::in_list_after_value; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1276 | break; |
| 1277 | case context::in_dict_after_brace: |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1278 | g_ctx = context::in_dict_after_key; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1279 | break; |
| 1280 | case context::in_dict_after_key: |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1281 | g_ctx = context::in_dict_after_value; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1282 | break; |
| 1283 | case context::in_dict_after_value: |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1284 | g_ctx = context::in_dict_after_key; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1285 | break; |
Nigel Tao | 18ef5b4 | 2020-03-16 10:37:47 +1100 | [diff] [blame] | 1286 | default: |
| 1287 | break; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1288 | } |
| 1289 | return nullptr; |
| 1290 | } |
| 1291 | |
| 1292 | const char* // |
| 1293 | main1(int argc, char** argv) { |
| 1294 | TRY(initialize_globals(argc, argv)); |
| 1295 | |
Nigel Tao | cd183f9 | 2020-07-14 12:11:05 +1000 | [diff] [blame] | 1296 | bool start_of_token_chain = true; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1297 | while (true) { |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 1298 | wuffs_base__status status = g_dec.decode_tokens( |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1299 | &g_tok, &g_src, |
| 1300 | wuffs_base__make_slice_u8(g_work_buffer_array, WORK_BUFFER_ARRAY_SIZE)); |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1301 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1302 | while (g_tok.meta.ri < g_tok.meta.wi) { |
| 1303 | wuffs_base__token t = g_tok.data.ptr[g_tok.meta.ri++]; |
Nigel Tao | 991bd51 | 2020-08-19 09:38:16 +1000 | [diff] [blame] | 1304 | uint64_t token_length = t.length(); |
| 1305 | if ((g_src.meta.ri - g_cursor_index) < token_length) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1306 | return "main: internal error: inconsistent g_src indexes"; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1307 | } |
Nigel Tao | 991bd51 | 2020-08-19 09:38:16 +1000 | [diff] [blame] | 1308 | g_cursor_index += token_length; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1309 | |
Nigel Tao | 2104205 | 2020-08-19 23:13:54 +1000 | [diff] [blame] | 1310 | // Handle filler tokens (e.g. whitespace, punctuation and comments). |
| 1311 | // These are skipped, unless -output-comments is enabled. |
Nigel Tao | 3c8589b | 2020-07-19 21:49:00 +1000 | [diff] [blame] | 1312 | if (t.value_base_category() == WUFFS_BASE__TOKEN__VBC__FILLER) { |
Nigel Tao | 773994c | 2021-02-22 10:50:08 +1100 | [diff] [blame^] | 1313 | if (!g_flags.output_comments) { |
| 1314 | // No-op. |
| 1315 | } else if (t.value_base_detail() & |
| 1316 | WUFFS_BASE__TOKEN__VBD__FILLER__COMMENT_ANY) { |
Nigel Tao | 2104205 | 2020-08-19 23:13:54 +1000 | [diff] [blame] | 1317 | if (g_flags.compact_output) { |
| 1318 | TRY(write_dst(g_src.data.ptr + g_cursor_index - token_length, |
| 1319 | token_length)); |
Nigel Tao | 773994c | 2021-02-22 10:50:08 +1100 | [diff] [blame^] | 1320 | |
Nigel Tao | 2104205 | 2020-08-19 23:13:54 +1000 | [diff] [blame] | 1321 | } else { |
| 1322 | if (start_of_token_chain) { |
| 1323 | if (g_is_after_comment) { |
| 1324 | TRY_INDENT_SANS_LEADING_NEW_LINE; |
| 1325 | } else if (g_ctx != context::none) { |
| 1326 | if (g_ctx == context::in_dict_after_key) { |
| 1327 | TRY(write_dst(":", 1)); |
| 1328 | } else if ((g_ctx != context::in_list_after_bracket) && |
Nigel Tao | cd4cbc9 | 2020-09-22 22:22:15 +1000 | [diff] [blame] | 1329 | (g_ctx != context::in_dict_after_brace) && |
| 1330 | (g_ctx != context::end_of_data)) { |
Nigel Tao | 2104205 | 2020-08-19 23:13:54 +1000 | [diff] [blame] | 1331 | TRY(write_dst(",", 1)); |
| 1332 | } |
Nigel Tao | 773994c | 2021-02-22 10:50:08 +1100 | [diff] [blame^] | 1333 | TRY_INDENT_WITH_LEADING_NEW_LINE; |
Nigel Tao | 2104205 | 2020-08-19 23:13:54 +1000 | [diff] [blame] | 1334 | } |
| 1335 | } |
| 1336 | TRY(write_dst(g_src.data.ptr + g_cursor_index - token_length, |
| 1337 | token_length)); |
| 1338 | if (!t.continued() && |
| 1339 | (t.value_base_detail() & |
| 1340 | WUFFS_BASE__TOKEN__VBD__FILLER__COMMENT_BLOCK)) { |
| 1341 | TRY(write_dst("\n", 1)); |
| 1342 | } |
| 1343 | g_is_after_comment = true; |
| 1344 | } |
Nigel Tao | 773994c | 2021-02-22 10:50:08 +1100 | [diff] [blame^] | 1345 | if (g_ctx == context::in_list_after_bracket) { |
| 1346 | g_ctx = context::in_list_after_value; |
| 1347 | } else if (g_ctx == context::in_dict_after_brace) { |
| 1348 | g_ctx = context::in_dict_after_value; |
| 1349 | } |
| 1350 | g_num_input_blank_lines = |
| 1351 | (t.value_base_detail() & |
| 1352 | WUFFS_BASE__TOKEN__VBD__FILLER__COMMENT_LINE) |
| 1353 | ? 1 |
| 1354 | : 0; |
| 1355 | |
| 1356 | } else { |
| 1357 | uint8_t* p = g_src.data.ptr + g_cursor_index - token_length; |
| 1358 | uint8_t* q = g_src.data.ptr + g_cursor_index; |
| 1359 | for (; p < q; p++) { |
| 1360 | if (*p == '\n') { |
| 1361 | g_num_input_blank_lines++; |
| 1362 | } |
| 1363 | } |
Nigel Tao | 2104205 | 2020-08-19 23:13:54 +1000 | [diff] [blame] | 1364 | } |
Nigel Tao | 773994c | 2021-02-22 10:50:08 +1100 | [diff] [blame^] | 1365 | |
Nigel Tao | 496e88b | 2020-04-09 22:10:08 +1000 | [diff] [blame] | 1366 | start_of_token_chain = !t.continued(); |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1367 | continue; |
| 1368 | } |
| 1369 | |
Nigel Tao | 2ef3999 | 2020-04-09 17:24:39 +1000 | [diff] [blame] | 1370 | const char* z = handle_token(t, start_of_token_chain); |
Nigel Tao | 2104205 | 2020-08-19 23:13:54 +1000 | [diff] [blame] | 1371 | g_is_after_comment = false; |
Nigel Tao | 496e88b | 2020-04-09 22:10:08 +1000 | [diff] [blame] | 1372 | start_of_token_chain = !t.continued(); |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1373 | if (z == nullptr) { |
| 1374 | continue; |
Nigel Tao | cd4cbc9 | 2020-09-22 22:22:15 +1000 | [diff] [blame] | 1375 | } else if (z != g_eod) { |
| 1376 | return z; |
| 1377 | } else if (g_flags.query_c_string && *g_flags.query_c_string) { |
| 1378 | // With a non-empty g_query, don't try to consume trailing filler or |
| 1379 | // confirm that we've processed all the tokens. |
| 1380 | return nullptr; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1381 | } |
Nigel Tao | cd4cbc9 | 2020-09-22 22:22:15 +1000 | [diff] [blame] | 1382 | g_ctx = context::end_of_data; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1383 | } |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1384 | |
| 1385 | if (status.repr == nullptr) { |
Nigel Tao | cd4cbc9 | 2020-09-22 22:22:15 +1000 | [diff] [blame] | 1386 | if (g_ctx != context::end_of_data) { |
| 1387 | return "main: internal error: unexpected end of token stream"; |
| 1388 | } |
| 1389 | // Check that we've exhausted the input. |
| 1390 | if ((g_src.meta.ri == g_src.meta.wi) && !g_src.meta.closed) { |
| 1391 | TRY(read_src()); |
| 1392 | } |
| 1393 | if ((g_src.meta.ri < g_src.meta.wi) || !g_src.meta.closed) { |
| 1394 | return "main: valid JSON followed by further (unexpected) data"; |
| 1395 | } |
| 1396 | // All done. |
| 1397 | return nullptr; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1398 | } else if (status.repr == wuffs_base__suspension__short_read) { |
Nigel Tao | 991bd51 | 2020-08-19 09:38:16 +1000 | [diff] [blame] | 1399 | if (g_cursor_index != g_src.meta.ri) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1400 | return "main: internal error: inconsistent g_src indexes"; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1401 | } |
| 1402 | TRY(read_src()); |
Nigel Tao | 991bd51 | 2020-08-19 09:38:16 +1000 | [diff] [blame] | 1403 | g_cursor_index = g_src.meta.ri; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1404 | } else if (status.repr == wuffs_base__suspension__short_write) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1405 | g_tok.compact(); |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1406 | } else { |
| 1407 | return status.message(); |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1408 | } |
| 1409 | } |
| 1410 | } |
| 1411 | |
Nigel Tao | 2914bae | 2020-02-26 09:40:30 +1100 | [diff] [blame] | 1412 | int // |
| 1413 | compute_exit_code(const char* status_msg) { |
Nigel Tao | 9cc2c25 | 2020-02-23 17:05:49 +1100 | [diff] [blame] | 1414 | if (!status_msg) { |
| 1415 | return 0; |
| 1416 | } |
Nigel Tao | 01abc84 | 2020-03-06 21:42:33 +1100 | [diff] [blame] | 1417 | size_t n; |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1418 | if (status_msg == g_usage) { |
Nigel Tao | 01abc84 | 2020-03-06 21:42:33 +1100 | [diff] [blame] | 1419 | n = strlen(status_msg); |
| 1420 | } else { |
Nigel Tao | 9cc2c25 | 2020-02-23 17:05:49 +1100 | [diff] [blame] | 1421 | n = strnlen(status_msg, 2047); |
Nigel Tao | 01abc84 | 2020-03-06 21:42:33 +1100 | [diff] [blame] | 1422 | if (n >= 2047) { |
| 1423 | status_msg = "main: internal error: error message is too long"; |
| 1424 | n = strnlen(status_msg, 2047); |
| 1425 | } |
Nigel Tao | 9cc2c25 | 2020-02-23 17:05:49 +1100 | [diff] [blame] | 1426 | } |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 1427 | const int stderr_fd = 2; |
| 1428 | ignore_return_value(write(stderr_fd, status_msg, n)); |
| 1429 | ignore_return_value(write(stderr_fd, "\n", 1)); |
Nigel Tao | 9cc2c25 | 2020-02-23 17:05:49 +1100 | [diff] [blame] | 1430 | // Return an exit code of 1 for regular (forseen) errors, e.g. badly |
| 1431 | // formatted or unsupported input. |
| 1432 | // |
| 1433 | // Return an exit code of 2 for internal (exceptional) errors, e.g. defensive |
| 1434 | // run-time checks found that an internal invariant did not hold. |
| 1435 | // |
| 1436 | // Automated testing, including badly formatted inputs, can therefore |
| 1437 | // discriminate between expected failure (exit code 1) and unexpected failure |
| 1438 | // (other non-zero exit codes). Specifically, exit code 2 for internal |
| 1439 | // invariant violation, exit code 139 (which is 128 + SIGSEGV on x86_64 |
| 1440 | // linux) for a segmentation fault (e.g. null pointer dereference). |
| 1441 | return strstr(status_msg, "internal error:") ? 2 : 1; |
| 1442 | } |
| 1443 | |
Nigel Tao | 2914bae | 2020-02-26 09:40:30 +1100 | [diff] [blame] | 1444 | int // |
| 1445 | main(int argc, char** argv) { |
Nigel Tao | 01abc84 | 2020-03-06 21:42:33 +1100 | [diff] [blame] | 1446 | // Look for an input filename (the first non-flag argument) in argv. If there |
| 1447 | // is one, open it (but do not read from it) before we self-impose a sandbox. |
| 1448 | // |
| 1449 | // Flags start with "-", unless it comes after a bare "--" arg. |
| 1450 | { |
| 1451 | bool dash_dash = false; |
| 1452 | int a; |
| 1453 | for (a = 1; a < argc; a++) { |
| 1454 | char* arg = argv[a]; |
| 1455 | if ((arg[0] == '-') && !dash_dash) { |
| 1456 | dash_dash = (arg[1] == '-') && (arg[2] == '\x00'); |
| 1457 | continue; |
| 1458 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1459 | g_input_file_descriptor = open(arg, O_RDONLY); |
| 1460 | if (g_input_file_descriptor < 0) { |
Nigel Tao | 01abc84 | 2020-03-06 21:42:33 +1100 | [diff] [blame] | 1461 | fprintf(stderr, "%s: %s\n", arg, strerror(errno)); |
| 1462 | return 1; |
| 1463 | } |
| 1464 | break; |
| 1465 | } |
| 1466 | } |
| 1467 | |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 1468 | #if defined(WUFFS_EXAMPLE_USE_SECCOMP) |
| 1469 | prctl(PR_SET_SECCOMP, SECCOMP_MODE_STRICT); |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1470 | g_sandboxed = true; |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 1471 | #endif |
| 1472 | |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1473 | const char* z = main1(argc, argv); |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1474 | if (g_wrote_to_dst) { |
Nigel Tao | cd4cbc9 | 2020-09-22 22:22:15 +1000 | [diff] [blame] | 1475 | const char* z1 = g_is_after_comment ? nullptr : write_dst("\n", 1); |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1476 | const char* z2 = flush_dst(); |
| 1477 | z = z ? z : (z1 ? z1 : z2); |
| 1478 | } |
| 1479 | int exit_code = compute_exit_code(z); |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 1480 | |
| 1481 | #if defined(WUFFS_EXAMPLE_USE_SECCOMP) |
| 1482 | // Call SYS_exit explicitly, instead of calling SYS_exit_group implicitly by |
| 1483 | // either calling _exit or returning from main. SECCOMP_MODE_STRICT allows |
| 1484 | // only SYS_exit. |
| 1485 | syscall(SYS_exit, exit_code); |
| 1486 | #endif |
Nigel Tao | 9cc2c25 | 2020-02-23 17:05:49 +1100 | [diff] [blame] | 1487 | return exit_code; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1488 | } |