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