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 | 0a0c7d6 | 2020-08-18 23:31:27 +1000 | [diff] [blame^] | 273 | #define NEW_LINE_THEN_256_SPACES \ |
| 274 | "\n " \ |
| 275 | " " \ |
| 276 | " " \ |
| 277 | " " |
| 278 | #define NEW_LINE_THEN_256_TABS \ |
| 279 | "\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" \ |
| 280 | "\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" \ |
| 281 | "\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" \ |
| 282 | "\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" \ |
| 283 | "\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" \ |
| 284 | "\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" \ |
| 285 | "\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" |
| 286 | |
| 287 | const char* g_new_line_then_256_indent_bytes; |
| 288 | uint32_t g_bytes_per_indent_depth; |
Nigel Tao | 107f0ef | 2020-03-01 21:35:02 +1100 | [diff] [blame] | 289 | |
Nigel Tao | fdac24a | 2020-03-06 21:53:08 +1100 | [diff] [blame] | 290 | #ifndef DST_BUFFER_ARRAY_SIZE |
| 291 | #define DST_BUFFER_ARRAY_SIZE (32 * 1024) |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 292 | #endif |
Nigel Tao | fdac24a | 2020-03-06 21:53:08 +1100 | [diff] [blame] | 293 | #ifndef SRC_BUFFER_ARRAY_SIZE |
| 294 | #define SRC_BUFFER_ARRAY_SIZE (32 * 1024) |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 295 | #endif |
Nigel Tao | fdac24a | 2020-03-06 21:53:08 +1100 | [diff] [blame] | 296 | #ifndef TOKEN_BUFFER_ARRAY_SIZE |
| 297 | #define TOKEN_BUFFER_ARRAY_SIZE (4 * 1024) |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 298 | #endif |
| 299 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 300 | uint8_t g_dst_array[DST_BUFFER_ARRAY_SIZE]; |
| 301 | uint8_t g_src_array[SRC_BUFFER_ARRAY_SIZE]; |
| 302 | wuffs_base__token g_tok_array[TOKEN_BUFFER_ARRAY_SIZE]; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 303 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 304 | wuffs_base__io_buffer g_dst; |
| 305 | wuffs_base__io_buffer g_src; |
| 306 | wuffs_base__token_buffer g_tok; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 307 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 308 | // g_curr_token_end_src_index is the g_src.data.ptr index of the end of the |
| 309 | // current token. An invariant is that (g_curr_token_end_src_index <= |
| 310 | // g_src.meta.ri). |
| 311 | size_t g_curr_token_end_src_index; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 312 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 313 | uint32_t g_depth; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 314 | |
| 315 | enum class context { |
| 316 | none, |
| 317 | in_list_after_bracket, |
| 318 | in_list_after_value, |
| 319 | in_dict_after_brace, |
| 320 | in_dict_after_key, |
| 321 | in_dict_after_value, |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 322 | } g_ctx; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 323 | |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 324 | bool // |
| 325 | in_dict_before_key() { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 326 | return (g_ctx == context::in_dict_after_brace) || |
| 327 | (g_ctx == context::in_dict_after_value); |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 328 | } |
| 329 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 330 | uint32_t g_suppress_write_dst; |
| 331 | bool g_wrote_to_dst; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 332 | |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 333 | wuffs_json__decoder g_dec; |
Nigel Tao | ea53245 | 2020-07-27 00:03:00 +1000 | [diff] [blame] | 334 | |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 335 | // ---- |
| 336 | |
| 337 | // Query is a JSON Pointer query. After initializing with a NUL-terminated C |
| 338 | // string, its multiple fragments are consumed as the program walks the JSON |
| 339 | // data from stdin. For example, letting "$" denote a NUL, suppose that we |
| 340 | // 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] | 341 | // 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] | 342 | // |
| 343 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 344 | // / a p p l e / b a n a n a / 1 2 / d u r i a n $ |
| 345 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 346 | // ^ ^ |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 347 | // m_frag_i m_frag_k |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 348 | // |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 349 | // The two pointers m_frag_i and m_frag_k (abbreviated as mfi and mfk) are the |
| 350 | // start (inclusive) and end (exclusive) of the query fragment. They satisfy |
| 351 | // (mfi <= mfk) and may be equal if the fragment empty (note that "" is a valid |
| 352 | // JSON object key). |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 353 | // |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 354 | // The m_frag_j (mfj) pointer moves between these two, or is nullptr. An |
| 355 | // invariant is that (((mfi <= mfj) && (mfj <= mfk)) || (mfj == nullptr)). |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 356 | // |
| 357 | // Wuffs' JSON tokenizer can portray a single JSON string as multiple Wuffs |
| 358 | // tokens, as backslash-escaped values within that JSON string may each get |
| 359 | // their own token. |
| 360 | // |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 361 | // 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] | 362 | // |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 363 | // While mfj remains non-nullptr, each token's unescaped contents are then |
| 364 | // compared to that part of the fragment from mfj to mfk. If it is a prefix |
| 365 | // (including the case of an exact match), then mfj is advanced by the |
| 366 | // unescaped length. Otherwise, mfj is set to nullptr. |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 367 | // |
| 368 | // Comparison accounts for JSON Pointer's escaping notation: "~0" and "~1" in |
| 369 | // the query (not the JSON value) are unescaped to "~" and "/" respectively. |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 370 | // "~n" and "~r" are also unescaped to "\n" and "\r". The program is |
| 371 | // responsible for calling Query::validate (with a strict_json_pointer_syntax |
| 372 | // argument) before otherwise using this class. |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 373 | // |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 374 | // The mfj pointer therefore advances from mfi to mfk, or drops out, as we |
| 375 | // incrementally match the object key with the query fragment. For example, if |
| 376 | // we have already matched the "ban" of "banana", then we would accept any of |
| 377 | // an "ana" token, an "a" token or a "\u0061" token, amongst others. They would |
| 378 | // advance mfj by 3, 1 or 1 bytes respectively. |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 379 | // |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 380 | // mfj |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 381 | // v |
| 382 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 383 | // / a p p l e / b a n a n a / 1 2 / d u r i a n $ |
| 384 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 385 | // ^ ^ |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 386 | // mfi mfk |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 387 | // |
| 388 | // 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] | 389 | // value), if mfj is non-nullptr and equal to (but not less than) mfk then we |
| 390 | // have a fragment match: the query fragment equals the object key. If there is |
| 391 | // a next fragment (in this example, "12") we move the frag_etc pointers to its |
| 392 | // start and end and increment Query::m_depth. Otherwise, we have matched the |
| 393 | // 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] | 394 | // |
| 395 | // The discussion above centers on object keys. If the query fragment is |
| 396 | // numeric then it can also match as an array index: the string fragment "12" |
| 397 | // will match an array's 13th element (starting counting from zero). See RFC |
| 398 | // 6901 for its precise definition of an "array index" number. |
| 399 | // |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 400 | // 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] | 401 | // whose type (wuffs_base__result_u64) is a result type. An error result means |
| 402 | // that the fragment is not an array index. A value result holds the number of |
| 403 | // list elements remaining. When matching a query fragment in an array (instead |
| 404 | // of in an object), each element ticks this number down towards zero. At zero, |
| 405 | // the upcoming JSON value is the one that matches the query fragment. |
| 406 | class Query { |
| 407 | private: |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 408 | uint8_t* m_frag_i; |
| 409 | uint8_t* m_frag_j; |
| 410 | uint8_t* m_frag_k; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 411 | |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 412 | uint32_t m_depth; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 413 | |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 414 | wuffs_base__result_u64 m_array_index; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 415 | |
| 416 | public: |
| 417 | void reset(char* query_c_string) { |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 418 | m_frag_i = (uint8_t*)query_c_string; |
| 419 | m_frag_j = (uint8_t*)query_c_string; |
| 420 | m_frag_k = (uint8_t*)query_c_string; |
| 421 | m_depth = 0; |
| 422 | m_array_index.status.repr = "#main: not an array index query fragment"; |
| 423 | m_array_index.value = 0; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 424 | } |
| 425 | |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 426 | 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] | 427 | |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 428 | bool is_at(uint32_t depth) { return m_depth == depth; } |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 429 | |
| 430 | // tick returns whether the fragment is a valid array index whose value is |
| 431 | // zero. If valid but non-zero, it decrements it and returns false. |
| 432 | bool tick() { |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 433 | if (m_array_index.status.is_ok()) { |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 434 | if (m_array_index.value == 0) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 435 | return true; |
| 436 | } |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 437 | m_array_index.value--; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 438 | } |
| 439 | return false; |
| 440 | } |
| 441 | |
| 442 | // next_fragment moves to the next fragment, returning whether it existed. |
| 443 | bool next_fragment() { |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 444 | uint8_t* k = m_frag_k; |
| 445 | uint32_t d = m_depth; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 446 | |
| 447 | this->reset(nullptr); |
| 448 | |
| 449 | if (!k || (*k != '/')) { |
| 450 | return false; |
| 451 | } |
| 452 | k++; |
| 453 | |
| 454 | bool all_digits = true; |
| 455 | uint8_t* i = k; |
| 456 | while ((*k != '\x00') && (*k != '/')) { |
| 457 | all_digits = all_digits && ('0' <= *k) && (*k <= '9'); |
| 458 | k++; |
| 459 | } |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 460 | m_frag_i = i; |
| 461 | m_frag_j = i; |
| 462 | m_frag_k = k; |
| 463 | m_depth = d + 1; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 464 | if (all_digits) { |
| 465 | // wuffs_base__parse_number_u64 rejects leading zeroes, e.g. "00", "07". |
Nigel Tao | 6b7ce30 | 2020-07-07 16:19:46 +1000 | [diff] [blame] | 466 | m_array_index = wuffs_base__parse_number_u64( |
| 467 | wuffs_base__make_slice_u8(i, k - i), |
| 468 | WUFFS_BASE__PARSE_NUMBER_XXX__DEFAULT_OPTIONS); |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 469 | } |
| 470 | return true; |
| 471 | } |
| 472 | |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 473 | bool matched_all() { return m_frag_k == nullptr; } |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 474 | |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 475 | 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] | 476 | |
| 477 | void incremental_match_slice(uint8_t* ptr, size_t len) { |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 478 | if (!m_frag_j) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 479 | return; |
| 480 | } |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 481 | uint8_t* j = m_frag_j; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 482 | while (true) { |
| 483 | if (len == 0) { |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 484 | m_frag_j = j; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 485 | return; |
| 486 | } |
| 487 | |
| 488 | if (*j == '\x00') { |
| 489 | break; |
| 490 | |
| 491 | } else if (*j == '~') { |
| 492 | j++; |
| 493 | if (*j == '0') { |
| 494 | if (*ptr != '~') { |
| 495 | break; |
| 496 | } |
| 497 | } else if (*j == '1') { |
| 498 | if (*ptr != '/') { |
| 499 | break; |
| 500 | } |
Nigel Tao | d6fdfb1 | 2020-03-11 12:24:14 +1100 | [diff] [blame] | 501 | } else if (*j == 'n') { |
| 502 | if (*ptr != '\n') { |
| 503 | break; |
| 504 | } |
| 505 | } else if (*j == 'r') { |
| 506 | if (*ptr != '\r') { |
| 507 | break; |
| 508 | } |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 509 | } else { |
| 510 | break; |
| 511 | } |
| 512 | |
| 513 | } else if (*j != *ptr) { |
| 514 | break; |
| 515 | } |
| 516 | |
| 517 | j++; |
| 518 | ptr++; |
| 519 | len--; |
| 520 | } |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 521 | m_frag_j = nullptr; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | void incremental_match_code_point(uint32_t code_point) { |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 525 | if (!m_frag_j) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 526 | return; |
| 527 | } |
| 528 | uint8_t u[WUFFS_BASE__UTF_8__BYTE_LENGTH__MAX_INCL]; |
| 529 | size_t n = wuffs_base__utf_8__encode( |
| 530 | wuffs_base__make_slice_u8(&u[0], |
| 531 | WUFFS_BASE__UTF_8__BYTE_LENGTH__MAX_INCL), |
| 532 | code_point); |
| 533 | if (n > 0) { |
| 534 | this->incremental_match_slice(&u[0], n); |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | // validate returns whether the (ptr, len) arguments form a valid JSON |
| 539 | // Pointer. In particular, it must be valid UTF-8, and either be empty or |
| 540 | // start with a '/'. Any '~' within must immediately be followed by either |
Nigel Tao | d6fdfb1 | 2020-03-11 12:24:14 +1100 | [diff] [blame] | 541 | // '0' or '1'. If strict_json_pointer_syntax is false, a '~' may also be |
| 542 | // followed by either 'n' or 'r'. |
| 543 | static bool validate(char* query_c_string, |
| 544 | size_t length, |
| 545 | bool strict_json_pointer_syntax) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 546 | if (length <= 0) { |
| 547 | return true; |
| 548 | } |
| 549 | if (query_c_string[0] != '/') { |
| 550 | return false; |
| 551 | } |
| 552 | wuffs_base__slice_u8 s = |
| 553 | wuffs_base__make_slice_u8((uint8_t*)query_c_string, length); |
| 554 | bool previous_was_tilde = false; |
| 555 | while (s.len > 0) { |
Nigel Tao | 702c7b2 | 2020-07-22 15:42:54 +1000 | [diff] [blame] | 556 | 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] | 557 | if (!o.is_valid()) { |
| 558 | return false; |
| 559 | } |
Nigel Tao | d6fdfb1 | 2020-03-11 12:24:14 +1100 | [diff] [blame] | 560 | |
| 561 | if (previous_was_tilde) { |
| 562 | switch (o.code_point) { |
| 563 | case '0': |
| 564 | case '1': |
| 565 | break; |
| 566 | case 'n': |
| 567 | case 'r': |
| 568 | if (strict_json_pointer_syntax) { |
| 569 | return false; |
| 570 | } |
| 571 | break; |
| 572 | default: |
| 573 | return false; |
| 574 | } |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 575 | } |
| 576 | previous_was_tilde = o.code_point == '~'; |
Nigel Tao | d6fdfb1 | 2020-03-11 12:24:14 +1100 | [diff] [blame] | 577 | |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 578 | s.ptr += o.byte_length; |
| 579 | s.len -= o.byte_length; |
| 580 | } |
| 581 | return !previous_was_tilde; |
| 582 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 583 | } g_query; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 584 | |
| 585 | // ---- |
| 586 | |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 587 | struct { |
| 588 | int remaining_argc; |
| 589 | char** remaining_argv; |
| 590 | |
Nigel Tao | 3690e83 | 2020-03-12 16:52:26 +1100 | [diff] [blame] | 591 | bool compact_output; |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 592 | bool fail_if_unsandboxed; |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 593 | bool input_allow_comments; |
| 594 | bool input_allow_extra_comma; |
| 595 | bool input_allow_inf_nan_numbers; |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 596 | bool output_extra_comma; |
Nigel Tao | d6fdfb1 | 2020-03-11 12:24:14 +1100 | [diff] [blame] | 597 | bool strict_json_pointer_syntax; |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 598 | bool tabs; |
Nigel Tao | 0a0c7d6 | 2020-08-18 23:31:27 +1000 | [diff] [blame^] | 599 | |
| 600 | uint32_t max_output_depth; |
| 601 | uint32_t spaces; |
| 602 | |
| 603 | char* query_c_string; |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 604 | } g_flags = {0}; |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 605 | |
| 606 | const char* // |
| 607 | parse_flags(int argc, char** argv) { |
Nigel Tao | ecadf72 | 2020-07-13 08:22:34 +1000 | [diff] [blame] | 608 | g_flags.spaces = 4; |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 609 | g_flags.max_output_depth = 0xFFFFFFFF; |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 610 | |
| 611 | int c = (argc > 0) ? 1 : 0; // Skip argv[0], the program name. |
| 612 | for (; c < argc; c++) { |
| 613 | char* arg = argv[c]; |
| 614 | if (*arg++ != '-') { |
| 615 | break; |
| 616 | } |
| 617 | |
| 618 | // A double-dash "--foo" is equivalent to a single-dash "-foo". As special |
| 619 | // cases, a bare "-" is not a flag (some programs may interpret it as |
| 620 | // stdin) and a bare "--" means to stop parsing flags. |
| 621 | if (*arg == '\x00') { |
| 622 | break; |
| 623 | } else if (*arg == '-') { |
| 624 | arg++; |
| 625 | if (*arg == '\x00') { |
| 626 | c++; |
| 627 | break; |
| 628 | } |
| 629 | } |
| 630 | |
Nigel Tao | 3690e83 | 2020-03-12 16:52:26 +1100 | [diff] [blame] | 631 | if (!strcmp(arg, "c") || !strcmp(arg, "compact-output")) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 632 | g_flags.compact_output = true; |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 633 | continue; |
| 634 | } |
Nigel Tao | 94440cf | 2020-04-02 22:28:24 +1100 | [diff] [blame] | 635 | if (!strcmp(arg, "d") || !strcmp(arg, "max-output-depth")) { |
| 636 | g_flags.max_output_depth = 1; |
| 637 | continue; |
| 638 | } else if (!strncmp(arg, "d=", 2) || |
| 639 | !strncmp(arg, "max-output-depth=", 16)) { |
| 640 | while (*arg++ != '=') { |
| 641 | } |
| 642 | wuffs_base__result_u64 u = wuffs_base__parse_number_u64( |
Nigel Tao | 6b7ce30 | 2020-07-07 16:19:46 +1000 | [diff] [blame] | 643 | wuffs_base__make_slice_u8((uint8_t*)arg, strlen(arg)), |
| 644 | WUFFS_BASE__PARSE_NUMBER_XXX__DEFAULT_OPTIONS); |
Nigel Tao | af75772 | 2020-07-18 17:27:11 +1000 | [diff] [blame] | 645 | if (u.status.is_ok() && (u.value <= 0xFFFFFFFF)) { |
Nigel Tao | 94440cf | 2020-04-02 22:28:24 +1100 | [diff] [blame] | 646 | g_flags.max_output_depth = (uint32_t)(u.value); |
| 647 | continue; |
| 648 | } |
| 649 | return g_usage; |
| 650 | } |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 651 | if (!strcmp(arg, "fail-if-unsandboxed")) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 652 | g_flags.fail_if_unsandboxed = true; |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 653 | continue; |
| 654 | } |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 655 | if (!strcmp(arg, "input-allow-comments")) { |
| 656 | g_flags.input_allow_comments = true; |
Nigel Tao | 4e19359 | 2020-07-15 12:48:57 +1000 | [diff] [blame] | 657 | continue; |
| 658 | } |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 659 | if (!strcmp(arg, "input-allow-extra-comma")) { |
| 660 | g_flags.input_allow_extra_comma = true; |
Nigel Tao | 4e19359 | 2020-07-15 12:48:57 +1000 | [diff] [blame] | 661 | continue; |
| 662 | } |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 663 | if (!strcmp(arg, "input-allow-inf-nan-numbers")) { |
| 664 | g_flags.input_allow_inf_nan_numbers = true; |
Nigel Tao | 3c8589b | 2020-07-19 21:49:00 +1000 | [diff] [blame] | 665 | continue; |
| 666 | } |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 667 | if (!strcmp(arg, "output-extra-comma")) { |
| 668 | g_flags.output_extra_comma = true; |
Nigel Tao | dd11469 | 2020-07-25 21:54:12 +1000 | [diff] [blame] | 669 | continue; |
| 670 | } |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 671 | if (!strncmp(arg, "q=", 2) || !strncmp(arg, "query=", 6)) { |
| 672 | while (*arg++ != '=') { |
| 673 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 674 | g_flags.query_c_string = arg; |
Nigel Tao | d6fdfb1 | 2020-03-11 12:24:14 +1100 | [diff] [blame] | 675 | continue; |
| 676 | } |
Nigel Tao | ecadf72 | 2020-07-13 08:22:34 +1000 | [diff] [blame] | 677 | if (!strncmp(arg, "s=", 2) || !strncmp(arg, "spaces=", 7)) { |
| 678 | while (*arg++ != '=') { |
| 679 | } |
| 680 | if (('0' <= arg[0]) && (arg[0] <= '8') && (arg[1] == '\x00')) { |
| 681 | g_flags.spaces = arg[0] - '0'; |
| 682 | continue; |
| 683 | } |
| 684 | return g_usage; |
| 685 | } |
| 686 | if (!strcmp(arg, "strict-json-pointer-syntax")) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 687 | g_flags.strict_json_pointer_syntax = true; |
Nigel Tao | d6fdfb1 | 2020-03-11 12:24:14 +1100 | [diff] [blame] | 688 | continue; |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 689 | } |
| 690 | if (!strcmp(arg, "t") || !strcmp(arg, "tabs")) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 691 | g_flags.tabs = true; |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 692 | continue; |
| 693 | } |
| 694 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 695 | return g_usage; |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 696 | } |
| 697 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 698 | if (g_flags.query_c_string && |
| 699 | !Query::validate(g_flags.query_c_string, strlen(g_flags.query_c_string), |
| 700 | g_flags.strict_json_pointer_syntax)) { |
Nigel Tao | d6fdfb1 | 2020-03-11 12:24:14 +1100 | [diff] [blame] | 701 | return "main: bad JSON Pointer (RFC 6901) syntax for the -query=STR flag"; |
| 702 | } |
| 703 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 704 | g_flags.remaining_argc = argc - c; |
| 705 | g_flags.remaining_argv = argv + c; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 706 | return nullptr; |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 707 | } |
| 708 | |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 709 | const char* // |
| 710 | initialize_globals(int argc, char** argv) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 711 | g_dst = wuffs_base__make_io_buffer( |
| 712 | wuffs_base__make_slice_u8(g_dst_array, DST_BUFFER_ARRAY_SIZE), |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 713 | wuffs_base__empty_io_buffer_meta()); |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 714 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 715 | g_src = wuffs_base__make_io_buffer( |
| 716 | wuffs_base__make_slice_u8(g_src_array, SRC_BUFFER_ARRAY_SIZE), |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 717 | wuffs_base__empty_io_buffer_meta()); |
| 718 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 719 | g_tok = wuffs_base__make_token_buffer( |
| 720 | wuffs_base__make_slice_token(g_tok_array, TOKEN_BUFFER_ARRAY_SIZE), |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 721 | wuffs_base__empty_token_buffer_meta()); |
| 722 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 723 | g_curr_token_end_src_index = 0; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 724 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 725 | g_depth = 0; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 726 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 727 | g_ctx = context::none; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 728 | |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 729 | TRY(parse_flags(argc, argv)); |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 730 | if (g_flags.fail_if_unsandboxed && !g_sandboxed) { |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 731 | return "main: unsandboxed"; |
| 732 | } |
Nigel Tao | 01abc84 | 2020-03-06 21:42:33 +1100 | [diff] [blame] | 733 | const int stdin_fd = 0; |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 734 | if (g_flags.remaining_argc > |
| 735 | ((g_input_file_descriptor != stdin_fd) ? 1 : 0)) { |
| 736 | return g_usage; |
Nigel Tao | 107f0ef | 2020-03-01 21:35:02 +1100 | [diff] [blame] | 737 | } |
| 738 | |
Nigel Tao | 0a0c7d6 | 2020-08-18 23:31:27 +1000 | [diff] [blame^] | 739 | g_new_line_then_256_indent_bytes = |
| 740 | g_flags.tabs ? NEW_LINE_THEN_256_TABS : NEW_LINE_THEN_256_SPACES; |
| 741 | g_bytes_per_indent_depth = g_flags.tabs ? 1 : g_flags.spaces; |
| 742 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 743 | g_query.reset(g_flags.query_c_string); |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 744 | |
Nigel Tao | c96b31c | 2020-07-27 22:37:23 +1000 | [diff] [blame] | 745 | // 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] | 746 | // completed the query. |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 747 | g_suppress_write_dst = g_query.next_fragment() ? 1 : 0; |
| 748 | g_wrote_to_dst = false; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 749 | |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 750 | TRY(g_dec.initialize(sizeof__wuffs_json__decoder(), WUFFS_VERSION, 0) |
| 751 | .message()); |
Nigel Tao | 4b186b0 | 2020-03-18 14:25:21 +1100 | [diff] [blame] | 752 | |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 753 | if (g_flags.input_allow_comments) { |
| 754 | g_dec.set_quirk_enabled(WUFFS_JSON__QUIRK_ALLOW_COMMENT_BLOCK, true); |
| 755 | g_dec.set_quirk_enabled(WUFFS_JSON__QUIRK_ALLOW_COMMENT_LINE, true); |
Nigel Tao | 3c8589b | 2020-07-19 21:49:00 +1000 | [diff] [blame] | 756 | } |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 757 | if (g_flags.input_allow_extra_comma) { |
| 758 | g_dec.set_quirk_enabled(WUFFS_JSON__QUIRK_ALLOW_EXTRA_COMMA, true); |
Nigel Tao | c766bb7 | 2020-07-09 12:59:32 +1000 | [diff] [blame] | 759 | } |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 760 | if (g_flags.input_allow_inf_nan_numbers) { |
| 761 | 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] | 762 | } |
Nigel Tao | c766bb7 | 2020-07-09 12:59:32 +1000 | [diff] [blame] | 763 | |
Nigel Tao | 4b186b0 | 2020-03-18 14:25:21 +1100 | [diff] [blame] | 764 | // Consume an optional whitespace trailer. This isn't part of the JSON spec, |
| 765 | // but it works better with line oriented Unix tools (such as "echo 123 | |
| 766 | // jsonptr" where it's "echo", not "echo -n") or hand-edited JSON files which |
| 767 | // can accidentally contain trailing whitespace. |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 768 | 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] | 769 | |
| 770 | return nullptr; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 771 | } |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 772 | |
| 773 | // ---- |
| 774 | |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 775 | // ignore_return_value suppresses errors from -Wall -Werror. |
| 776 | static void // |
| 777 | ignore_return_value(int ignored) {} |
| 778 | |
Nigel Tao | 2914bae | 2020-02-26 09:40:30 +1100 | [diff] [blame] | 779 | const char* // |
| 780 | read_src() { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 781 | if (g_src.meta.closed) { |
Nigel Tao | 9cc2c25 | 2020-02-23 17:05:49 +1100 | [diff] [blame] | 782 | return "main: internal error: read requested on a closed source"; |
Nigel Tao | a840692 | 2020-02-19 12:22:00 +1100 | [diff] [blame] | 783 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 784 | g_src.compact(); |
| 785 | if (g_src.meta.wi >= g_src.data.len) { |
| 786 | return "main: g_src buffer is full"; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 787 | } |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 788 | while (true) { |
Nigel Tao | d6a10df | 2020-07-27 11:47:47 +1000 | [diff] [blame] | 789 | ssize_t n = read(g_input_file_descriptor, g_src.writer_pointer(), |
| 790 | g_src.writer_length()); |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 791 | if (n >= 0) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 792 | g_src.meta.wi += n; |
| 793 | g_src.meta.closed = n == 0; |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 794 | break; |
| 795 | } else if (errno != EINTR) { |
| 796 | return strerror(errno); |
| 797 | } |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 798 | } |
| 799 | return nullptr; |
| 800 | } |
| 801 | |
Nigel Tao | 2914bae | 2020-02-26 09:40:30 +1100 | [diff] [blame] | 802 | const char* // |
| 803 | flush_dst() { |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 804 | while (true) { |
Nigel Tao | d6a10df | 2020-07-27 11:47:47 +1000 | [diff] [blame] | 805 | size_t n = g_dst.reader_length(); |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 806 | if (n == 0) { |
| 807 | break; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 808 | } |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 809 | const int stdout_fd = 1; |
Nigel Tao | d6a10df | 2020-07-27 11:47:47 +1000 | [diff] [blame] | 810 | ssize_t i = write(stdout_fd, g_dst.reader_pointer(), n); |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 811 | if (i >= 0) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 812 | g_dst.meta.ri += i; |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 813 | } else if (errno != EINTR) { |
| 814 | return strerror(errno); |
| 815 | } |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 816 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 817 | g_dst.compact(); |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 818 | return nullptr; |
| 819 | } |
| 820 | |
Nigel Tao | 2914bae | 2020-02-26 09:40:30 +1100 | [diff] [blame] | 821 | const char* // |
| 822 | write_dst(const void* s, size_t n) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 823 | if (g_suppress_write_dst > 0) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 824 | return nullptr; |
| 825 | } |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 826 | const uint8_t* p = static_cast<const uint8_t*>(s); |
| 827 | while (n > 0) { |
Nigel Tao | d6a10df | 2020-07-27 11:47:47 +1000 | [diff] [blame] | 828 | size_t i = g_dst.writer_length(); |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 829 | if (i == 0) { |
| 830 | const char* z = flush_dst(); |
| 831 | if (z) { |
| 832 | return z; |
| 833 | } |
Nigel Tao | d6a10df | 2020-07-27 11:47:47 +1000 | [diff] [blame] | 834 | i = g_dst.writer_length(); |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 835 | if (i == 0) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 836 | return "main: g_dst buffer is full"; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 837 | } |
| 838 | } |
| 839 | |
| 840 | if (i > n) { |
| 841 | i = n; |
| 842 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 843 | memcpy(g_dst.data.ptr + g_dst.meta.wi, p, i); |
| 844 | g_dst.meta.wi += i; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 845 | p += i; |
| 846 | n -= i; |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 847 | g_wrote_to_dst = true; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 848 | } |
| 849 | return nullptr; |
| 850 | } |
| 851 | |
| 852 | // ---- |
| 853 | |
Nigel Tao | 2914bae | 2020-02-26 09:40:30 +1100 | [diff] [blame] | 854 | uint8_t // |
| 855 | hex_digit(uint8_t nibble) { |
Nigel Tao | b5461bd | 2020-02-21 14:13:37 +1100 | [diff] [blame] | 856 | nibble &= 0x0F; |
| 857 | if (nibble <= 9) { |
| 858 | return '0' + nibble; |
| 859 | } |
| 860 | return ('A' - 10) + nibble; |
| 861 | } |
| 862 | |
Nigel Tao | 2914bae | 2020-02-26 09:40:30 +1100 | [diff] [blame] | 863 | const char* // |
Nigel Tao | 7cb7654 | 2020-07-19 22:19:04 +1000 | [diff] [blame] | 864 | handle_unicode_code_point(uint32_t ucp) { |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 865 | if (ucp < 0x0020) { |
| 866 | switch (ucp) { |
| 867 | case '\b': |
| 868 | return write_dst("\\b", 2); |
| 869 | case '\f': |
| 870 | return write_dst("\\f", 2); |
| 871 | case '\n': |
| 872 | return write_dst("\\n", 2); |
| 873 | case '\r': |
| 874 | return write_dst("\\r", 2); |
| 875 | case '\t': |
| 876 | return write_dst("\\t", 2); |
Nigel Tao | 7cb7654 | 2020-07-19 22:19:04 +1000 | [diff] [blame] | 877 | } |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 878 | |
| 879 | // Other bytes less than 0x0020 are valid UTF-8 but not valid in a |
| 880 | // JSON string. They need to remain escaped. |
| 881 | uint8_t esc6[6]; |
| 882 | esc6[0] = '\\'; |
| 883 | esc6[1] = 'u'; |
| 884 | esc6[2] = '0'; |
| 885 | esc6[3] = '0'; |
| 886 | esc6[4] = hex_digit(ucp >> 4); |
| 887 | esc6[5] = hex_digit(ucp >> 0); |
| 888 | return write_dst(&esc6[0], 6); |
| 889 | |
| 890 | } else if (ucp == '\"') { |
| 891 | return write_dst("\\\"", 2); |
| 892 | |
| 893 | } else if (ucp == '\\') { |
| 894 | return write_dst("\\\\", 2); |
Nigel Tao | 7cb7654 | 2020-07-19 22:19:04 +1000 | [diff] [blame] | 895 | } |
| 896 | |
| 897 | uint8_t u[WUFFS_BASE__UTF_8__BYTE_LENGTH__MAX_INCL]; |
| 898 | size_t n = wuffs_base__utf_8__encode( |
| 899 | wuffs_base__make_slice_u8(&u[0], |
| 900 | WUFFS_BASE__UTF_8__BYTE_LENGTH__MAX_INCL), |
| 901 | ucp); |
| 902 | if (n == 0) { |
| 903 | return "main: internal error: unexpected Unicode code point"; |
| 904 | } |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 905 | return write_dst(&u[0], n); |
Nigel Tao | 168f60a | 2020-07-14 13:19:33 +1000 | [diff] [blame] | 906 | } |
| 907 | |
Nigel Tao | d191a3f | 2020-07-19 22:14:54 +1000 | [diff] [blame] | 908 | // ---- |
| 909 | |
Nigel Tao | 3b48698 | 2020-02-27 15:05:59 +1100 | [diff] [blame] | 910 | const char* // |
Nigel Tao | 2ef3999 | 2020-04-09 17:24:39 +1000 | [diff] [blame] | 911 | handle_token(wuffs_base__token t, bool start_of_token_chain) { |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 912 | do { |
Nigel Tao | 462f866 | 2020-04-01 23:01:51 +1100 | [diff] [blame] | 913 | int64_t vbc = t.value_base_category(); |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 914 | uint64_t vbd = t.value_base_detail(); |
Nigel Tao | ee6927f | 2020-07-27 12:08:33 +1000 | [diff] [blame] | 915 | uint64_t token_length = t.length(); |
| 916 | wuffs_base__slice_u8 tok = wuffs_base__make_slice_u8( |
| 917 | g_src.data.ptr + g_curr_token_end_src_index - token_length, |
| 918 | token_length); |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 919 | |
| 920 | // Handle ']' or '}'. |
Nigel Tao | 9f7a250 | 2020-02-23 09:42:02 +1100 | [diff] [blame] | 921 | if ((vbc == WUFFS_BASE__TOKEN__VBC__STRUCTURE) && |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 922 | (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__POP)) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 923 | if (g_query.is_at(g_depth)) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 924 | return "main: no match for query"; |
| 925 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 926 | if (g_depth <= 0) { |
| 927 | return "main: internal error: inconsistent g_depth"; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 928 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 929 | g_depth--; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 930 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 931 | if (g_query.matched_all() && (g_depth >= g_flags.max_output_depth)) { |
| 932 | g_suppress_write_dst--; |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 933 | // '…' is U+2026 HORIZONTAL ELLIPSIS, which is 3 UTF-8 bytes. |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 934 | TRY(write_dst((vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__FROM_LIST) |
| 935 | ? "\"[…]\"" |
| 936 | : "\"{…}\"", |
| 937 | 7)); |
| 938 | } else { |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 939 | // Write preceding whitespace. |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 940 | if ((g_ctx != context::in_list_after_bracket) && |
| 941 | (g_ctx != context::in_dict_after_brace) && |
| 942 | !g_flags.compact_output) { |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 943 | if (g_flags.output_extra_comma) { |
Nigel Tao | 0a0c7d6 | 2020-08-18 23:31:27 +1000 | [diff] [blame^] | 944 | TRY(write_dst(",", 1)); |
Nigel Tao | c766bb7 | 2020-07-09 12:59:32 +1000 | [diff] [blame] | 945 | } |
Nigel Tao | 0a0c7d6 | 2020-08-18 23:31:27 +1000 | [diff] [blame^] | 946 | uint32_t indent = g_depth * g_bytes_per_indent_depth; |
| 947 | TRY(write_dst(g_new_line_then_256_indent_bytes, 1 + (indent & 0xFF))); |
| 948 | for (indent >>= 8; indent > 0; indent--) { |
| 949 | TRY(write_dst(g_new_line_then_256_indent_bytes + 1, 0x100)); |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 950 | } |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 951 | } |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 952 | |
| 953 | TRY(write_dst( |
| 954 | (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__FROM_LIST) ? "]" : "}", |
| 955 | 1)); |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 956 | } |
| 957 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 958 | g_ctx = (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_LIST) |
| 959 | ? context::in_list_after_value |
| 960 | : context::in_dict_after_key; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 961 | goto after_value; |
| 962 | } |
| 963 | |
Nigel Tao | d1c928a | 2020-02-28 12:43:53 +1100 | [diff] [blame] | 964 | // Write preceding whitespace and punctuation, if it wasn't ']', '}' or a |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 965 | // continuation of a multi-token chain. |
| 966 | if (start_of_token_chain) { |
| 967 | if (g_ctx == context::in_dict_after_key) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 968 | TRY(write_dst(": ", g_flags.compact_output ? 1 : 2)); |
| 969 | } else if (g_ctx != context::none) { |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 970 | if ((g_ctx != context::in_list_after_bracket) && |
| 971 | (g_ctx != context::in_dict_after_brace)) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 972 | TRY(write_dst(",", 1)); |
Nigel Tao | 107f0ef | 2020-03-01 21:35:02 +1100 | [diff] [blame] | 973 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 974 | if (!g_flags.compact_output) { |
Nigel Tao | 0a0c7d6 | 2020-08-18 23:31:27 +1000 | [diff] [blame^] | 975 | uint32_t indent = g_depth * g_bytes_per_indent_depth; |
| 976 | TRY(write_dst(g_new_line_then_256_indent_bytes, 1 + (indent & 0xFF))); |
| 977 | for (indent >>= 8; indent > 0; indent--) { |
| 978 | TRY(write_dst(g_new_line_then_256_indent_bytes + 1, 0x100)); |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 979 | } |
| 980 | } |
| 981 | } |
| 982 | |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 983 | bool query_matched_fragment = false; |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 984 | if (g_query.is_at(g_depth)) { |
| 985 | switch (g_ctx) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 986 | case context::in_list_after_bracket: |
| 987 | case context::in_list_after_value: |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 988 | query_matched_fragment = g_query.tick(); |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 989 | break; |
| 990 | case context::in_dict_after_key: |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 991 | query_matched_fragment = g_query.matched_fragment(); |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 992 | break; |
Nigel Tao | 18ef5b4 | 2020-03-16 10:37:47 +1100 | [diff] [blame] | 993 | default: |
| 994 | break; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 995 | } |
| 996 | } |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 997 | if (!query_matched_fragment) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 998 | // No-op. |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 999 | } else if (!g_query.next_fragment()) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1000 | // There is no next fragment. We have matched the complete query, and |
| 1001 | // the upcoming JSON value is the result of that query. |
| 1002 | // |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1003 | // Un-suppress writing to stdout and reset the g_ctx and g_depth as if |
| 1004 | // we were about to decode a top-level value. This makes any subsequent |
| 1005 | // indentation be relative to this point, and we will return g_eod |
| 1006 | // after the upcoming JSON value is complete. |
| 1007 | if (g_suppress_write_dst != 1) { |
| 1008 | return "main: internal error: inconsistent g_suppress_write_dst"; |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 1009 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1010 | g_suppress_write_dst = 0; |
| 1011 | g_ctx = context::none; |
| 1012 | g_depth = 0; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1013 | } else if ((vbc != WUFFS_BASE__TOKEN__VBC__STRUCTURE) || |
| 1014 | !(vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__PUSH)) { |
| 1015 | // The query has moved on to the next fragment but the upcoming JSON |
| 1016 | // value is not a container. |
| 1017 | return "main: no match for query"; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1018 | } |
| 1019 | } |
| 1020 | |
| 1021 | // Handle the token itself: either a container ('[' or '{') or a simple |
Nigel Tao | 85fba7f | 2020-02-29 16:28:06 +1100 | [diff] [blame] | 1022 | // value: string (a chain of raw or escaped parts), literal or number. |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1023 | switch (vbc) { |
Nigel Tao | 85fba7f | 2020-02-29 16:28:06 +1100 | [diff] [blame] | 1024 | case WUFFS_BASE__TOKEN__VBC__STRUCTURE: |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1025 | if (g_query.matched_all() && (g_depth >= g_flags.max_output_depth)) { |
| 1026 | g_suppress_write_dst++; |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 1027 | } else { |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 1028 | TRY(write_dst( |
| 1029 | (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_LIST) ? "[" : "{", |
| 1030 | 1)); |
| 1031 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1032 | g_depth++; |
| 1033 | g_ctx = (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_LIST) |
| 1034 | ? context::in_list_after_bracket |
| 1035 | : context::in_dict_after_brace; |
Nigel Tao | 85fba7f | 2020-02-29 16:28:06 +1100 | [diff] [blame] | 1036 | return nullptr; |
| 1037 | |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1038 | case WUFFS_BASE__TOKEN__VBC__STRING: |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 1039 | if (start_of_token_chain) { |
| 1040 | TRY(write_dst("\"", 1)); |
| 1041 | g_query.restart_fragment(in_dict_before_key() && |
| 1042 | g_query.is_at(g_depth)); |
| 1043 | } |
| 1044 | |
| 1045 | if (vbd & WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_0_DST_1_SRC_DROP) { |
| 1046 | // No-op. |
| 1047 | } else if (vbd & |
| 1048 | WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_1_DST_1_SRC_COPY) { |
| 1049 | TRY(write_dst(tok.ptr, tok.len)); |
| 1050 | g_query.incremental_match_slice(tok.ptr, tok.len); |
| 1051 | } else { |
| 1052 | return "main: internal error: unexpected string-token conversion"; |
| 1053 | } |
| 1054 | |
Nigel Tao | 496e88b | 2020-04-09 22:10:08 +1000 | [diff] [blame] | 1055 | if (t.continued()) { |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1056 | return nullptr; |
| 1057 | } |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 1058 | TRY(write_dst("\"", 1)); |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1059 | goto after_value; |
| 1060 | |
| 1061 | case WUFFS_BASE__TOKEN__VBC__UNICODE_CODE_POINT: |
Nigel Tao | 496e88b | 2020-04-09 22:10:08 +1000 | [diff] [blame] | 1062 | if (!t.continued()) { |
| 1063 | return "main: internal error: unexpected non-continued UCP token"; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1064 | } |
| 1065 | TRY(handle_unicode_code_point(vbd)); |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1066 | g_query.incremental_match_code_point(vbd); |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1067 | return nullptr; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1068 | |
Nigel Tao | 85fba7f | 2020-02-29 16:28:06 +1100 | [diff] [blame] | 1069 | case WUFFS_BASE__TOKEN__VBC__LITERAL: |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1070 | case WUFFS_BASE__TOKEN__VBC__NUMBER: |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 1071 | TRY(write_dst(tok.ptr, tok.len)); |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1072 | goto after_value; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1073 | } |
| 1074 | |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 1075 | // Return an error if we didn't match the (vbc, vbd) pair. |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1076 | return "main: internal error: unexpected token"; |
| 1077 | } while (0); |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1078 | |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1079 | // Book-keeping after completing a value (whether a container value or a |
| 1080 | // simple value). Empty parent containers are no longer empty. If the parent |
| 1081 | // container is a "{...}" object, toggle between keys and values. |
| 1082 | after_value: |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1083 | if (g_depth == 0) { |
| 1084 | return g_eod; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1085 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1086 | switch (g_ctx) { |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1087 | case context::in_list_after_bracket: |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1088 | g_ctx = context::in_list_after_value; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1089 | break; |
| 1090 | case context::in_dict_after_brace: |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1091 | g_ctx = context::in_dict_after_key; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1092 | break; |
| 1093 | case context::in_dict_after_key: |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1094 | g_ctx = context::in_dict_after_value; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1095 | break; |
| 1096 | case context::in_dict_after_value: |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1097 | g_ctx = context::in_dict_after_key; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1098 | break; |
Nigel Tao | 18ef5b4 | 2020-03-16 10:37:47 +1100 | [diff] [blame] | 1099 | default: |
| 1100 | break; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1101 | } |
| 1102 | return nullptr; |
| 1103 | } |
| 1104 | |
| 1105 | const char* // |
| 1106 | main1(int argc, char** argv) { |
| 1107 | TRY(initialize_globals(argc, argv)); |
| 1108 | |
Nigel Tao | cd183f9 | 2020-07-14 12:11:05 +1000 | [diff] [blame] | 1109 | bool start_of_token_chain = true; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1110 | while (true) { |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 1111 | wuffs_base__status status = g_dec.decode_tokens( |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1112 | &g_tok, &g_src, |
| 1113 | 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] | 1114 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1115 | while (g_tok.meta.ri < g_tok.meta.wi) { |
| 1116 | wuffs_base__token t = g_tok.data.ptr[g_tok.meta.ri++]; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1117 | uint64_t n = t.length(); |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1118 | if ((g_src.meta.ri - g_curr_token_end_src_index) < n) { |
| 1119 | return "main: internal error: inconsistent g_src indexes"; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1120 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1121 | g_curr_token_end_src_index += n; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1122 | |
Nigel Tao | d0b16cb | 2020-03-14 10:15:54 +1100 | [diff] [blame] | 1123 | // Skip filler tokens (e.g. whitespace). |
Nigel Tao | 3c8589b | 2020-07-19 21:49:00 +1000 | [diff] [blame] | 1124 | if (t.value_base_category() == WUFFS_BASE__TOKEN__VBC__FILLER) { |
Nigel Tao | 496e88b | 2020-04-09 22:10:08 +1000 | [diff] [blame] | 1125 | start_of_token_chain = !t.continued(); |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1126 | continue; |
| 1127 | } |
| 1128 | |
Nigel Tao | 2ef3999 | 2020-04-09 17:24:39 +1000 | [diff] [blame] | 1129 | const char* z = handle_token(t, start_of_token_chain); |
Nigel Tao | 496e88b | 2020-04-09 22:10:08 +1000 | [diff] [blame] | 1130 | start_of_token_chain = !t.continued(); |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1131 | if (z == nullptr) { |
| 1132 | continue; |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1133 | } else if (z == g_eod) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1134 | goto end_of_data; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1135 | } |
| 1136 | return z; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1137 | } |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1138 | |
| 1139 | if (status.repr == nullptr) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1140 | return "main: internal error: unexpected end of token stream"; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1141 | } else if (status.repr == wuffs_base__suspension__short_read) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1142 | if (g_curr_token_end_src_index != g_src.meta.ri) { |
| 1143 | return "main: internal error: inconsistent g_src indexes"; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1144 | } |
| 1145 | TRY(read_src()); |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1146 | g_curr_token_end_src_index = g_src.meta.ri; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1147 | } else if (status.repr == wuffs_base__suspension__short_write) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1148 | g_tok.compact(); |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1149 | } else { |
| 1150 | return status.message(); |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1151 | } |
| 1152 | } |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1153 | end_of_data: |
| 1154 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1155 | // 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] | 1156 | // confirm that we've processed all the tokens. |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1157 | if (g_flags.query_c_string && *g_flags.query_c_string) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1158 | return nullptr; |
| 1159 | } |
Nigel Tao | 6b161af | 2020-02-24 11:01:48 +1100 | [diff] [blame] | 1160 | |
Nigel Tao | 6b161af | 2020-02-24 11:01:48 +1100 | [diff] [blame] | 1161 | // Check that we've exhausted the input. |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1162 | 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] | 1163 | TRY(read_src()); |
| 1164 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1165 | 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] | 1166 | return "main: valid JSON followed by further (unexpected) data"; |
Nigel Tao | 6b161af | 2020-02-24 11:01:48 +1100 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | // 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] | 1170 | // filler tokens. For example, "true\n" is valid JSON (and fully consumed |
| 1171 | // with WUFFS_JSON__QUIRK_ALLOW_TRAILING_NEW_LINE enabled) with a trailing |
| 1172 | // filler token for the "\n". |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1173 | for (; g_tok.meta.ri < g_tok.meta.wi; g_tok.meta.ri++) { |
| 1174 | if (g_tok.data.ptr[g_tok.meta.ri].value_base_category() != |
Nigel Tao | 6b161af | 2020-02-24 11:01:48 +1100 | [diff] [blame] | 1175 | WUFFS_BASE__TOKEN__VBC__FILLER) { |
| 1176 | return "main: internal error: decoded OK but unprocessed tokens remain"; |
| 1177 | } |
| 1178 | } |
| 1179 | |
| 1180 | return nullptr; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1181 | } |
| 1182 | |
Nigel Tao | 2914bae | 2020-02-26 09:40:30 +1100 | [diff] [blame] | 1183 | int // |
| 1184 | compute_exit_code(const char* status_msg) { |
Nigel Tao | 9cc2c25 | 2020-02-23 17:05:49 +1100 | [diff] [blame] | 1185 | if (!status_msg) { |
| 1186 | return 0; |
| 1187 | } |
Nigel Tao | 01abc84 | 2020-03-06 21:42:33 +1100 | [diff] [blame] | 1188 | size_t n; |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1189 | if (status_msg == g_usage) { |
Nigel Tao | 01abc84 | 2020-03-06 21:42:33 +1100 | [diff] [blame] | 1190 | n = strlen(status_msg); |
| 1191 | } else { |
Nigel Tao | 9cc2c25 | 2020-02-23 17:05:49 +1100 | [diff] [blame] | 1192 | n = strnlen(status_msg, 2047); |
Nigel Tao | 01abc84 | 2020-03-06 21:42:33 +1100 | [diff] [blame] | 1193 | if (n >= 2047) { |
| 1194 | status_msg = "main: internal error: error message is too long"; |
| 1195 | n = strnlen(status_msg, 2047); |
| 1196 | } |
Nigel Tao | 9cc2c25 | 2020-02-23 17:05:49 +1100 | [diff] [blame] | 1197 | } |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 1198 | const int stderr_fd = 2; |
| 1199 | ignore_return_value(write(stderr_fd, status_msg, n)); |
| 1200 | ignore_return_value(write(stderr_fd, "\n", 1)); |
Nigel Tao | 9cc2c25 | 2020-02-23 17:05:49 +1100 | [diff] [blame] | 1201 | // Return an exit code of 1 for regular (forseen) errors, e.g. badly |
| 1202 | // formatted or unsupported input. |
| 1203 | // |
| 1204 | // Return an exit code of 2 for internal (exceptional) errors, e.g. defensive |
| 1205 | // run-time checks found that an internal invariant did not hold. |
| 1206 | // |
| 1207 | // Automated testing, including badly formatted inputs, can therefore |
| 1208 | // discriminate between expected failure (exit code 1) and unexpected failure |
| 1209 | // (other non-zero exit codes). Specifically, exit code 2 for internal |
| 1210 | // invariant violation, exit code 139 (which is 128 + SIGSEGV on x86_64 |
| 1211 | // linux) for a segmentation fault (e.g. null pointer dereference). |
| 1212 | return strstr(status_msg, "internal error:") ? 2 : 1; |
| 1213 | } |
| 1214 | |
Nigel Tao | 2914bae | 2020-02-26 09:40:30 +1100 | [diff] [blame] | 1215 | int // |
| 1216 | main(int argc, char** argv) { |
Nigel Tao | 01abc84 | 2020-03-06 21:42:33 +1100 | [diff] [blame] | 1217 | // Look for an input filename (the first non-flag argument) in argv. If there |
| 1218 | // is one, open it (but do not read from it) before we self-impose a sandbox. |
| 1219 | // |
| 1220 | // Flags start with "-", unless it comes after a bare "--" arg. |
| 1221 | { |
| 1222 | bool dash_dash = false; |
| 1223 | int a; |
| 1224 | for (a = 1; a < argc; a++) { |
| 1225 | char* arg = argv[a]; |
| 1226 | if ((arg[0] == '-') && !dash_dash) { |
| 1227 | dash_dash = (arg[1] == '-') && (arg[2] == '\x00'); |
| 1228 | continue; |
| 1229 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1230 | g_input_file_descriptor = open(arg, O_RDONLY); |
| 1231 | if (g_input_file_descriptor < 0) { |
Nigel Tao | 01abc84 | 2020-03-06 21:42:33 +1100 | [diff] [blame] | 1232 | fprintf(stderr, "%s: %s\n", arg, strerror(errno)); |
| 1233 | return 1; |
| 1234 | } |
| 1235 | break; |
| 1236 | } |
| 1237 | } |
| 1238 | |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 1239 | #if defined(WUFFS_EXAMPLE_USE_SECCOMP) |
| 1240 | prctl(PR_SET_SECCOMP, SECCOMP_MODE_STRICT); |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1241 | g_sandboxed = true; |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 1242 | #endif |
| 1243 | |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1244 | const char* z = main1(argc, argv); |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1245 | if (g_wrote_to_dst) { |
Nigel Tao | 0291a47 | 2020-08-13 22:40:10 +1000 | [diff] [blame] | 1246 | const char* z1 = write_dst("\n", 1); |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1247 | const char* z2 = flush_dst(); |
| 1248 | z = z ? z : (z1 ? z1 : z2); |
| 1249 | } |
| 1250 | int exit_code = compute_exit_code(z); |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 1251 | |
| 1252 | #if defined(WUFFS_EXAMPLE_USE_SECCOMP) |
| 1253 | // Call SYS_exit explicitly, instead of calling SYS_exit_group implicitly by |
| 1254 | // either calling _exit or returning from main. SECCOMP_MODE_STRICT allows |
| 1255 | // only SYS_exit. |
| 1256 | syscall(SYS_exit, exit_code); |
| 1257 | #endif |
Nigel Tao | 9cc2c25 | 2020-02-23 17:05:49 +1100 | [diff] [blame] | 1258 | return exit_code; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1259 | } |