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 | 168f60a | 2020-07-14 13:19:33 +1000 | [diff] [blame] | 19 | (RFC 6901) query syntax. It reads CBOR or UTF-8 JSON from stdin and writes CBOR |
| 20 | or 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 | 168f60a | 2020-07-14 13:19:33 +1000 | [diff] [blame] | 30 | One benefit of simplicity is that this program's CBOR, 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 | 168f60a | 2020-07-14 13:19:33 +1000 | [diff] [blame] | 36 | The CBOR and JSON implementations are also written in the Wuffs programming |
| 37 | language (and then transpiled to C/C++), which is memory-safe (e.g. array |
| 38 | indexing is bounds-checked) but also prevents 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 | 168f60a | 2020-07-14 13:19:33 +1000 | [diff] [blame] | 45 | All together, this program aims to safely handle untrusted CBOR or JSON files |
| 46 | without 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 | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 89 | This example program differs from most other example Wuffs programs in that it |
| 90 | is written in C++, not C. |
| 91 | |
| 92 | $CXX jsonptr.cc && ./a.out < ../../test/data/github-tags.json; rm -f a.out |
| 93 | |
| 94 | for a C++ compiler $CXX, such as clang++ or g++. |
| 95 | */ |
| 96 | |
Nigel Tao | 721190a | 2020-04-03 22:25:21 +1100 | [diff] [blame] | 97 | #if defined(__cplusplus) && (__cplusplus < 201103L) |
| 98 | #error "This C++ program requires -std=c++11 or later" |
| 99 | #endif |
| 100 | |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 101 | #include <errno.h> |
Nigel Tao | 01abc84 | 2020-03-06 21:42:33 +1100 | [diff] [blame] | 102 | #include <fcntl.h> |
| 103 | #include <stdio.h> |
Nigel Tao | 9cc2c25 | 2020-02-23 17:05:49 +1100 | [diff] [blame] | 104 | #include <string.h> |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 105 | #include <unistd.h> |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 106 | |
| 107 | // Wuffs ships as a "single file C library" or "header file library" as per |
| 108 | // https://github.com/nothings/stb/blob/master/docs/stb_howto.txt |
| 109 | // |
| 110 | // To use that single file as a "foo.c"-like implementation, instead of a |
| 111 | // "foo.h"-like header, #define WUFFS_IMPLEMENTATION before #include'ing or |
| 112 | // compiling it. |
| 113 | #define WUFFS_IMPLEMENTATION |
| 114 | |
| 115 | // Defining the WUFFS_CONFIG__MODULE* macros are optional, but it lets users of |
| 116 | // release/c/etc.c whitelist which parts of Wuffs to build. That file contains |
| 117 | // the entire Wuffs standard library, implementing a variety of codecs and file |
| 118 | // formats. Without this macro definition, an optimizing compiler or linker may |
| 119 | // very well discard Wuffs code for unused codecs, but listing the Wuffs |
| 120 | // modules we use makes that process explicit. Preprocessing means that such |
| 121 | // code simply isn't compiled. |
| 122 | #define WUFFS_CONFIG__MODULES |
| 123 | #define WUFFS_CONFIG__MODULE__BASE |
Nigel Tao | 4e19359 | 2020-07-15 12:48:57 +1000 | [diff] [blame] | 124 | #define WUFFS_CONFIG__MODULE__CBOR |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 125 | #define WUFFS_CONFIG__MODULE__JSON |
| 126 | |
| 127 | // If building this program in an environment that doesn't easily accommodate |
| 128 | // relative includes, you can use the script/inline-c-relative-includes.go |
| 129 | // program to generate a stand-alone C++ file. |
| 130 | #include "../../release/c/wuffs-unsupported-snapshot.c" |
| 131 | |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 132 | #if defined(__linux__) |
| 133 | #include <linux/prctl.h> |
| 134 | #include <linux/seccomp.h> |
| 135 | #include <sys/prctl.h> |
| 136 | #include <sys/syscall.h> |
| 137 | #define WUFFS_EXAMPLE_USE_SECCOMP |
| 138 | #endif |
| 139 | |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 140 | #define TRY(error_msg) \ |
| 141 | do { \ |
| 142 | const char* z = error_msg; \ |
| 143 | if (z) { \ |
| 144 | return z; \ |
| 145 | } \ |
| 146 | } while (false) |
| 147 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 148 | static const char* g_eod = "main: end of data"; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 149 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 150 | static const char* g_usage = |
Nigel Tao | 01abc84 | 2020-03-06 21:42:33 +1100 | [diff] [blame] | 151 | "Usage: jsonptr -flags input.json\n" |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 152 | "\n" |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 153 | "Flags:\n" |
Nigel Tao | 3690e83 | 2020-03-12 16:52:26 +1100 | [diff] [blame] | 154 | " -c -compact-output\n" |
Nigel Tao | 94440cf | 2020-04-02 22:28:24 +1100 | [diff] [blame] | 155 | " -d=NUM -max-output-depth=NUM\n" |
Nigel Tao | 4e19359 | 2020-07-15 12:48:57 +1000 | [diff] [blame] | 156 | " -i=FMT -input-format={json,cbor}\n" |
Nigel Tao | 168f60a | 2020-07-14 13:19:33 +1000 | [diff] [blame] | 157 | " -o=FMT -output-format={json,cbor}\n" |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 158 | " -q=STR -query=STR\n" |
Nigel Tao | ecadf72 | 2020-07-13 08:22:34 +1000 | [diff] [blame] | 159 | " -s=NUM -spaces=NUM\n" |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 160 | " -t -tabs\n" |
| 161 | " -fail-if-unsandboxed\n" |
Nigel Tao | c766bb7 | 2020-07-09 12:59:32 +1000 | [diff] [blame] | 162 | " -input-json-extra-comma\n" |
| 163 | " -output-json-extra-comma\n" |
Nigel Tao | ecadf72 | 2020-07-13 08:22:34 +1000 | [diff] [blame] | 164 | " -strict-json-pointer-syntax\n" |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 165 | "\n" |
Nigel Tao | 01abc84 | 2020-03-06 21:42:33 +1100 | [diff] [blame] | 166 | "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] | 167 | "\n" |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 168 | "----\n" |
| 169 | "\n" |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 170 | "jsonptr is a JSON formatter (pretty-printer) that supports the JSON\n" |
Nigel Tao | 168f60a | 2020-07-14 13:19:33 +1000 | [diff] [blame] | 171 | "Pointer (RFC 6901) query syntax. It reads CBOR or UTF-8 JSON from stdin\n" |
| 172 | "and writes CBOR or canonicalized, formatted UTF-8 JSON to stdout. The\n" |
| 173 | "input and output formats do not have to match, but conversion between\n" |
| 174 | "formats may be lossy.\n" |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 175 | "\n" |
| 176 | "Canonicalized means that e.g. \"abc\\u000A\\tx\\u0177z\" is re-written\n" |
| 177 | "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] | 178 | "duplicate keys. Canonicalization does not imply Unicode normalization.\n" |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 179 | "\n" |
| 180 | "Formatted means that arrays' and objects' elements are indented, each\n" |
Nigel Tao | ecadf72 | 2020-07-13 08:22:34 +1000 | [diff] [blame] | 181 | "on its own line. Configure this with the -c / -compact-output, -s=NUM /\n" |
Nigel Tao | 168f60a | 2020-07-14 13:19:33 +1000 | [diff] [blame] | 182 | "-spaces=NUM (for NUM ranging from 0 to 8) and -t / -tabs flags. Those\n" |
| 183 | "flags only apply to JSON (not CBOR) output.\n" |
| 184 | "\n" |
| 185 | "The -input-format and -output-format flags select between reading and\n" |
| 186 | "writing JSON (the default, a textual format) or CBOR (a binary format).\n" |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 187 | "\n" |
Nigel Tao | c766bb7 | 2020-07-09 12:59:32 +1000 | [diff] [blame] | 188 | "The -input-json-extra-comma flag allows input like \"[1,2,]\", with a\n" |
| 189 | "comma after the final element of a JSON list or dictionary.\n" |
| 190 | "\n" |
| 191 | "The -output-json-extra-comma flag writes extra commas, regardless of\n" |
| 192 | "whether the input had it. Extra commas are non-compliant with the JSON\n" |
| 193 | "specification but many parsers accept it and it can produce simpler\n" |
| 194 | "line-based diffs. This flag is ignored when -compact-output is set.\n" |
| 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 | 168f60a | 2020-07-14 13:19:33 +1000 | [diff] [blame] | 211 | "If the query found a valid JSON|CBOR value, this program will return a\n" |
| 212 | "zero exit code even if the rest of the input isn't valid. 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 | 168f60a | 2020-07-14 13:19:33 +1000 | [diff] [blame] | 216 | "The JSON and CBOR specifications (https://json.org/ or RFC 8259; RFC\n" |
| 217 | "7049) permit implementations to allow duplicate keys, as this one does.\n" |
| 218 | "This JSON Pointer implementation is also greedy, following the first\n" |
| 219 | "match for each fragment without back-tracking. For example, the\n" |
| 220 | "\"/foo/bar\" query will fail if the root object has multiple \"foo\"\n" |
| 221 | "children but the first one doesn't have a \"bar\" child, even if later\n" |
| 222 | "ones do.\n" |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 223 | "\n" |
Nigel Tao | ecadf72 | 2020-07-13 08:22:34 +1000 | [diff] [blame] | 224 | "The -strict-json-pointer-syntax flag restricts the -query=STR string to\n" |
| 225 | "exactly RFC 6901, with only two escape sequences: \"~0\" and \"~1\" for\n" |
| 226 | "\"~\" and \"/\". Without this flag, this program also lets \"~n\" and\n" |
| 227 | "\"~r\" escape the New Line and Carriage Return ASCII control characters,\n" |
| 228 | "which can work better with line oriented Unix tools that assume exactly\n" |
| 229 | "one value (i.e. one JSON Pointer string) per line.\n" |
Nigel Tao | d6fdfb1 | 2020-03-11 12:24:14 +1100 | [diff] [blame] | 230 | "\n" |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 231 | "----\n" |
| 232 | "\n" |
Nigel Tao | 94440cf | 2020-04-02 22:28:24 +1100 | [diff] [blame] | 233 | "The -d=NUM or -max-output-depth=NUM flag gives the maximum (inclusive)\n" |
Nigel Tao | 168f60a | 2020-07-14 13:19:33 +1000 | [diff] [blame] | 234 | "output depth. JSON|CBOR containers ([] arrays and {} objects) can hold\n" |
| 235 | "other containers. When this flag is set, containers at depth NUM are\n" |
| 236 | "replaced with \"[…]\" or \"{…}\". A bare -d or -max-output-depth is\n" |
| 237 | "equivalent to -d=1. The flag's absence means an unlimited output depth.\n" |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 238 | "\n" |
| 239 | "The -max-output-depth flag only affects the program's output. It doesn't\n" |
Nigel Tao | 168f60a | 2020-07-14 13:19:33 +1000 | [diff] [blame] | 240 | "affect whether or not the input is considered valid JSON|CBOR. The\n" |
| 241 | "format specifications permit implementations to set their own maximum\n" |
| 242 | "input depth. This JSON|CBOR implementation sets it to 1024.\n" |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 243 | "\n" |
| 244 | "Depth is measured in terms of nested containers. It is unaffected by the\n" |
| 245 | "number of spaces or tabs used to indent.\n" |
| 246 | "\n" |
| 247 | "When both -max-output-depth and -query are set, the output depth is\n" |
| 248 | "measured from when the query resolves, not from the input root. The\n" |
| 249 | "input depth (measured from the root) is still limited to 1024.\n" |
| 250 | "\n" |
| 251 | "----\n" |
| 252 | "\n" |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 253 | "The -fail-if-unsandboxed flag causes the program to exit if it does not\n" |
| 254 | "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] | 255 | "sandbox, regardless of whether this flag was set."; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 256 | |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 257 | // ---- |
| 258 | |
Nigel Tao | f3146c2 | 2020-03-26 08:47:42 +1100 | [diff] [blame] | 259 | // Wuffs allows either statically or dynamically allocated work buffers. This |
| 260 | // program exercises static allocation. |
| 261 | #define WORK_BUFFER_ARRAY_SIZE \ |
| 262 | WUFFS_JSON__DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE |
| 263 | #if WORK_BUFFER_ARRAY_SIZE > 0 |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 264 | uint8_t g_work_buffer_array[WORK_BUFFER_ARRAY_SIZE]; |
Nigel Tao | f3146c2 | 2020-03-26 08:47:42 +1100 | [diff] [blame] | 265 | #else |
| 266 | // Not all C/C++ compilers support 0-length arrays. |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 267 | uint8_t g_work_buffer_array[1]; |
Nigel Tao | f3146c2 | 2020-03-26 08:47:42 +1100 | [diff] [blame] | 268 | #endif |
| 269 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 270 | bool g_sandboxed = false; |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 271 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 272 | int g_input_file_descriptor = 0; // A 0 default means stdin. |
Nigel Tao | 01abc84 | 2020-03-06 21:42:33 +1100 | [diff] [blame] | 273 | |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 274 | #define MAX_INDENT 8 |
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 | 4e19359 | 2020-07-15 12:48:57 +1000 | [diff] [blame] | 321 | wuffs_cbor__decoder g_cbor_decoder; |
| 322 | wuffs_json__decoder g_json_decoder; |
| 323 | wuffs_base__token_decoder* g_dec; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 324 | |
Nigel Tao | 168f60a | 2020-07-14 13:19:33 +1000 | [diff] [blame] | 325 | // cbor_output_string_array is a 4 KiB buffer. For -output-format=cbor, strings |
| 326 | // whose length are 4096 or less are written as a single definite-length |
| 327 | // string. Longer strings are written as an indefinite-length string containing |
| 328 | // multiple definite-length chunks, each of length up to 4 KiB. See the CBOR |
| 329 | // RFC (RFC 7049) section 2.2.2 "Indefinite-Length Byte Strings and Text |
| 330 | // Strings". The output is determinate even when the input is streamed. |
| 331 | // |
| 332 | // If raising CBOR_OUTPUT_STRING_ARRAY_SIZE above 0xFFFF then you will also |
| 333 | // have to update flush_cbor_output_string. |
| 334 | #define CBOR_OUTPUT_STRING_ARRAY_SIZE 4096 |
| 335 | uint8_t g_cbor_output_string_array[CBOR_OUTPUT_STRING_ARRAY_SIZE]; |
| 336 | |
| 337 | uint32_t g_cbor_output_string_length; |
| 338 | bool g_cbor_output_string_is_multiple_chunks; |
| 339 | bool g_cbor_output_string_is_utf_8; |
| 340 | |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 341 | // ---- |
| 342 | |
| 343 | // Query is a JSON Pointer query. After initializing with a NUL-terminated C |
| 344 | // string, its multiple fragments are consumed as the program walks the JSON |
| 345 | // data from stdin. For example, letting "$" denote a NUL, suppose that we |
| 346 | // 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] | 347 | // 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] | 348 | // |
| 349 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 350 | // / a p p l e / b a n a n a / 1 2 / d u r i a n $ |
| 351 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 352 | // ^ ^ |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 353 | // m_frag_i m_frag_k |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 354 | // |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 355 | // The two pointers m_frag_i and m_frag_k (abbreviated as mfi and mfk) are the |
| 356 | // start (inclusive) and end (exclusive) of the query fragment. They satisfy |
| 357 | // (mfi <= mfk) and may be equal if the fragment empty (note that "" is a valid |
| 358 | // JSON object key). |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 359 | // |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 360 | // The m_frag_j (mfj) pointer moves between these two, or is nullptr. An |
| 361 | // invariant is that (((mfi <= mfj) && (mfj <= mfk)) || (mfj == nullptr)). |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 362 | // |
| 363 | // Wuffs' JSON tokenizer can portray a single JSON string as multiple Wuffs |
| 364 | // tokens, as backslash-escaped values within that JSON string may each get |
| 365 | // their own token. |
| 366 | // |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 367 | // 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] | 368 | // |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 369 | // While mfj remains non-nullptr, each token's unescaped contents are then |
| 370 | // compared to that part of the fragment from mfj to mfk. If it is a prefix |
| 371 | // (including the case of an exact match), then mfj is advanced by the |
| 372 | // unescaped length. Otherwise, mfj is set to nullptr. |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 373 | // |
| 374 | // Comparison accounts for JSON Pointer's escaping notation: "~0" and "~1" in |
| 375 | // the query (not the JSON value) are unescaped to "~" and "/" respectively. |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 376 | // "~n" and "~r" are also unescaped to "\n" and "\r". The program is |
| 377 | // responsible for calling Query::validate (with a strict_json_pointer_syntax |
| 378 | // argument) before otherwise using this class. |
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 | // The mfj pointer therefore advances from mfi to mfk, or drops out, as we |
| 381 | // incrementally match the object key with the query fragment. For example, if |
| 382 | // we have already matched the "ban" of "banana", then we would accept any of |
| 383 | // an "ana" token, an "a" token or a "\u0061" token, amongst others. They would |
| 384 | // advance mfj by 3, 1 or 1 bytes respectively. |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 385 | // |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 386 | // mfj |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 387 | // v |
| 388 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 389 | // / a p p l e / b a n a n a / 1 2 / d u r i a n $ |
| 390 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 391 | // ^ ^ |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 392 | // mfi mfk |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 393 | // |
| 394 | // 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] | 395 | // value), if mfj is non-nullptr and equal to (but not less than) mfk then we |
| 396 | // have a fragment match: the query fragment equals the object key. If there is |
| 397 | // a next fragment (in this example, "12") we move the frag_etc pointers to its |
| 398 | // start and end and increment Query::m_depth. Otherwise, we have matched the |
| 399 | // 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] | 400 | // |
| 401 | // The discussion above centers on object keys. If the query fragment is |
| 402 | // numeric then it can also match as an array index: the string fragment "12" |
| 403 | // will match an array's 13th element (starting counting from zero). See RFC |
| 404 | // 6901 for its precise definition of an "array index" number. |
| 405 | // |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 406 | // 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] | 407 | // whose type (wuffs_base__result_u64) is a result type. An error result means |
| 408 | // that the fragment is not an array index. A value result holds the number of |
| 409 | // list elements remaining. When matching a query fragment in an array (instead |
| 410 | // of in an object), each element ticks this number down towards zero. At zero, |
| 411 | // the upcoming JSON value is the one that matches the query fragment. |
| 412 | class Query { |
| 413 | private: |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 414 | uint8_t* m_frag_i; |
| 415 | uint8_t* m_frag_j; |
| 416 | uint8_t* m_frag_k; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 417 | |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 418 | uint32_t m_depth; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 419 | |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 420 | wuffs_base__result_u64 m_array_index; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 421 | |
| 422 | public: |
| 423 | void reset(char* query_c_string) { |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 424 | m_frag_i = (uint8_t*)query_c_string; |
| 425 | m_frag_j = (uint8_t*)query_c_string; |
| 426 | m_frag_k = (uint8_t*)query_c_string; |
| 427 | m_depth = 0; |
| 428 | m_array_index.status.repr = "#main: not an array index query fragment"; |
| 429 | m_array_index.value = 0; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 430 | } |
| 431 | |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 432 | 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] | 433 | |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 434 | bool is_at(uint32_t depth) { return m_depth == depth; } |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 435 | |
| 436 | // tick returns whether the fragment is a valid array index whose value is |
| 437 | // zero. If valid but non-zero, it decrements it and returns false. |
| 438 | bool tick() { |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 439 | if (m_array_index.status.is_ok()) { |
| 440 | if (m_array_index.value == 0) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 441 | return true; |
| 442 | } |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 443 | m_array_index.value--; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 444 | } |
| 445 | return false; |
| 446 | } |
| 447 | |
| 448 | // next_fragment moves to the next fragment, returning whether it existed. |
| 449 | bool next_fragment() { |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 450 | uint8_t* k = m_frag_k; |
| 451 | uint32_t d = m_depth; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 452 | |
| 453 | this->reset(nullptr); |
| 454 | |
| 455 | if (!k || (*k != '/')) { |
| 456 | return false; |
| 457 | } |
| 458 | k++; |
| 459 | |
| 460 | bool all_digits = true; |
| 461 | uint8_t* i = k; |
| 462 | while ((*k != '\x00') && (*k != '/')) { |
| 463 | all_digits = all_digits && ('0' <= *k) && (*k <= '9'); |
| 464 | k++; |
| 465 | } |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 466 | m_frag_i = i; |
| 467 | m_frag_j = i; |
| 468 | m_frag_k = k; |
| 469 | m_depth = d + 1; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 470 | if (all_digits) { |
| 471 | // wuffs_base__parse_number_u64 rejects leading zeroes, e.g. "00", "07". |
Nigel Tao | 6b7ce30 | 2020-07-07 16:19:46 +1000 | [diff] [blame] | 472 | m_array_index = wuffs_base__parse_number_u64( |
| 473 | wuffs_base__make_slice_u8(i, k - i), |
| 474 | WUFFS_BASE__PARSE_NUMBER_XXX__DEFAULT_OPTIONS); |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 475 | } |
| 476 | return true; |
| 477 | } |
| 478 | |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 479 | bool matched_all() { return m_frag_k == nullptr; } |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 480 | |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 481 | 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] | 482 | |
| 483 | void incremental_match_slice(uint8_t* ptr, size_t len) { |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 484 | if (!m_frag_j) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 485 | return; |
| 486 | } |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 487 | uint8_t* j = m_frag_j; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 488 | while (true) { |
| 489 | if (len == 0) { |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 490 | m_frag_j = j; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 491 | return; |
| 492 | } |
| 493 | |
| 494 | if (*j == '\x00') { |
| 495 | break; |
| 496 | |
| 497 | } else if (*j == '~') { |
| 498 | j++; |
| 499 | if (*j == '0') { |
| 500 | if (*ptr != '~') { |
| 501 | break; |
| 502 | } |
| 503 | } else if (*j == '1') { |
| 504 | if (*ptr != '/') { |
| 505 | break; |
| 506 | } |
Nigel Tao | d6fdfb1 | 2020-03-11 12:24:14 +1100 | [diff] [blame] | 507 | } else if (*j == 'n') { |
| 508 | if (*ptr != '\n') { |
| 509 | break; |
| 510 | } |
| 511 | } else if (*j == 'r') { |
| 512 | if (*ptr != '\r') { |
| 513 | break; |
| 514 | } |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 515 | } else { |
| 516 | break; |
| 517 | } |
| 518 | |
| 519 | } else if (*j != *ptr) { |
| 520 | break; |
| 521 | } |
| 522 | |
| 523 | j++; |
| 524 | ptr++; |
| 525 | len--; |
| 526 | } |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 527 | m_frag_j = nullptr; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | void incremental_match_code_point(uint32_t code_point) { |
Nigel Tao | b48ee75 | 2020-03-13 09:27:33 +1100 | [diff] [blame] | 531 | if (!m_frag_j) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 532 | return; |
| 533 | } |
| 534 | uint8_t u[WUFFS_BASE__UTF_8__BYTE_LENGTH__MAX_INCL]; |
| 535 | size_t n = wuffs_base__utf_8__encode( |
| 536 | wuffs_base__make_slice_u8(&u[0], |
| 537 | WUFFS_BASE__UTF_8__BYTE_LENGTH__MAX_INCL), |
| 538 | code_point); |
| 539 | if (n > 0) { |
| 540 | this->incremental_match_slice(&u[0], n); |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | // validate returns whether the (ptr, len) arguments form a valid JSON |
| 545 | // Pointer. In particular, it must be valid UTF-8, and either be empty or |
| 546 | // start with a '/'. Any '~' within must immediately be followed by either |
Nigel Tao | d6fdfb1 | 2020-03-11 12:24:14 +1100 | [diff] [blame] | 547 | // '0' or '1'. If strict_json_pointer_syntax is false, a '~' may also be |
| 548 | // followed by either 'n' or 'r'. |
| 549 | static bool validate(char* query_c_string, |
| 550 | size_t length, |
| 551 | bool strict_json_pointer_syntax) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 552 | if (length <= 0) { |
| 553 | return true; |
| 554 | } |
| 555 | if (query_c_string[0] != '/') { |
| 556 | return false; |
| 557 | } |
| 558 | wuffs_base__slice_u8 s = |
| 559 | wuffs_base__make_slice_u8((uint8_t*)query_c_string, length); |
| 560 | bool previous_was_tilde = false; |
| 561 | while (s.len > 0) { |
| 562 | wuffs_base__utf_8__next__output o = wuffs_base__utf_8__next(s); |
| 563 | if (!o.is_valid()) { |
| 564 | return false; |
| 565 | } |
Nigel Tao | d6fdfb1 | 2020-03-11 12:24:14 +1100 | [diff] [blame] | 566 | |
| 567 | if (previous_was_tilde) { |
| 568 | switch (o.code_point) { |
| 569 | case '0': |
| 570 | case '1': |
| 571 | break; |
| 572 | case 'n': |
| 573 | case 'r': |
| 574 | if (strict_json_pointer_syntax) { |
| 575 | return false; |
| 576 | } |
| 577 | break; |
| 578 | default: |
| 579 | return false; |
| 580 | } |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 581 | } |
| 582 | previous_was_tilde = o.code_point == '~'; |
Nigel Tao | d6fdfb1 | 2020-03-11 12:24:14 +1100 | [diff] [blame] | 583 | |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 584 | s.ptr += o.byte_length; |
| 585 | s.len -= o.byte_length; |
| 586 | } |
| 587 | return !previous_was_tilde; |
| 588 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 589 | } g_query; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 590 | |
| 591 | // ---- |
| 592 | |
Nigel Tao | 168f60a | 2020-07-14 13:19:33 +1000 | [diff] [blame] | 593 | enum class file_format { |
| 594 | json, |
| 595 | cbor, |
| 596 | }; |
| 597 | |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 598 | struct { |
| 599 | int remaining_argc; |
| 600 | char** remaining_argv; |
| 601 | |
Nigel Tao | 3690e83 | 2020-03-12 16:52:26 +1100 | [diff] [blame] | 602 | bool compact_output; |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 603 | bool fail_if_unsandboxed; |
Nigel Tao | 4e19359 | 2020-07-15 12:48:57 +1000 | [diff] [blame] | 604 | file_format input_format; |
Nigel Tao | c766bb7 | 2020-07-09 12:59:32 +1000 | [diff] [blame] | 605 | bool input_json_extra_comma; |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 606 | uint32_t max_output_depth; |
Nigel Tao | 168f60a | 2020-07-14 13:19:33 +1000 | [diff] [blame] | 607 | file_format output_format; |
Nigel Tao | c766bb7 | 2020-07-09 12:59:32 +1000 | [diff] [blame] | 608 | bool output_json_extra_comma; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 609 | char* query_c_string; |
Nigel Tao | ecadf72 | 2020-07-13 08:22:34 +1000 | [diff] [blame] | 610 | size_t spaces; |
Nigel Tao | d6fdfb1 | 2020-03-11 12:24:14 +1100 | [diff] [blame] | 611 | bool strict_json_pointer_syntax; |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 612 | bool tabs; |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 613 | } g_flags = {0}; |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 614 | |
| 615 | const char* // |
| 616 | parse_flags(int argc, char** argv) { |
Nigel Tao | ecadf72 | 2020-07-13 08:22:34 +1000 | [diff] [blame] | 617 | g_flags.spaces = 4; |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 618 | g_flags.max_output_depth = 0xFFFFFFFF; |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 619 | |
| 620 | int c = (argc > 0) ? 1 : 0; // Skip argv[0], the program name. |
| 621 | for (; c < argc; c++) { |
| 622 | char* arg = argv[c]; |
| 623 | if (*arg++ != '-') { |
| 624 | break; |
| 625 | } |
| 626 | |
| 627 | // A double-dash "--foo" is equivalent to a single-dash "-foo". As special |
| 628 | // cases, a bare "-" is not a flag (some programs may interpret it as |
| 629 | // stdin) and a bare "--" means to stop parsing flags. |
| 630 | if (*arg == '\x00') { |
| 631 | break; |
| 632 | } else if (*arg == '-') { |
| 633 | arg++; |
| 634 | if (*arg == '\x00') { |
| 635 | c++; |
| 636 | break; |
| 637 | } |
| 638 | } |
| 639 | |
Nigel Tao | 3690e83 | 2020-03-12 16:52:26 +1100 | [diff] [blame] | 640 | if (!strcmp(arg, "c") || !strcmp(arg, "compact-output")) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 641 | g_flags.compact_output = true; |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 642 | continue; |
| 643 | } |
Nigel Tao | 94440cf | 2020-04-02 22:28:24 +1100 | [diff] [blame] | 644 | if (!strcmp(arg, "d") || !strcmp(arg, "max-output-depth")) { |
| 645 | g_flags.max_output_depth = 1; |
| 646 | continue; |
| 647 | } else if (!strncmp(arg, "d=", 2) || |
| 648 | !strncmp(arg, "max-output-depth=", 16)) { |
| 649 | while (*arg++ != '=') { |
| 650 | } |
| 651 | wuffs_base__result_u64 u = wuffs_base__parse_number_u64( |
Nigel Tao | 6b7ce30 | 2020-07-07 16:19:46 +1000 | [diff] [blame] | 652 | wuffs_base__make_slice_u8((uint8_t*)arg, strlen(arg)), |
| 653 | WUFFS_BASE__PARSE_NUMBER_XXX__DEFAULT_OPTIONS); |
Nigel Tao | af75772 | 2020-07-18 17:27:11 +1000 | [diff] [blame^] | 654 | if (u.status.is_ok() && (u.value <= 0xFFFFFFFF)) { |
Nigel Tao | 94440cf | 2020-04-02 22:28:24 +1100 | [diff] [blame] | 655 | g_flags.max_output_depth = (uint32_t)(u.value); |
| 656 | continue; |
| 657 | } |
| 658 | return g_usage; |
| 659 | } |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 660 | if (!strcmp(arg, "fail-if-unsandboxed")) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 661 | g_flags.fail_if_unsandboxed = true; |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 662 | continue; |
| 663 | } |
Nigel Tao | 4e19359 | 2020-07-15 12:48:57 +1000 | [diff] [blame] | 664 | if (!strcmp(arg, "i=cbor") || !strcmp(arg, "input-format=cbor")) { |
| 665 | g_flags.input_format = file_format::cbor; |
| 666 | continue; |
| 667 | } |
| 668 | if (!strcmp(arg, "i=json") || !strcmp(arg, "input-format=json")) { |
| 669 | g_flags.input_format = file_format::json; |
| 670 | continue; |
| 671 | } |
Nigel Tao | c766bb7 | 2020-07-09 12:59:32 +1000 | [diff] [blame] | 672 | if (!strcmp(arg, "input-json-extra-comma")) { |
| 673 | g_flags.input_json_extra_comma = true; |
| 674 | continue; |
| 675 | } |
Nigel Tao | 168f60a | 2020-07-14 13:19:33 +1000 | [diff] [blame] | 676 | if (!strcmp(arg, "o=cbor") || !strcmp(arg, "output-format=cbor")) { |
| 677 | g_flags.output_format = file_format::cbor; |
| 678 | continue; |
| 679 | } |
| 680 | if (!strcmp(arg, "o=json") || !strcmp(arg, "output-format=json")) { |
| 681 | g_flags.output_format = file_format::json; |
| 682 | continue; |
| 683 | } |
Nigel Tao | c766bb7 | 2020-07-09 12:59:32 +1000 | [diff] [blame] | 684 | if (!strcmp(arg, "output-json-extra-comma")) { |
| 685 | g_flags.output_json_extra_comma = true; |
| 686 | continue; |
| 687 | } |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 688 | if (!strncmp(arg, "q=", 2) || !strncmp(arg, "query=", 6)) { |
| 689 | while (*arg++ != '=') { |
| 690 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 691 | g_flags.query_c_string = arg; |
Nigel Tao | d6fdfb1 | 2020-03-11 12:24:14 +1100 | [diff] [blame] | 692 | continue; |
| 693 | } |
Nigel Tao | ecadf72 | 2020-07-13 08:22:34 +1000 | [diff] [blame] | 694 | if (!strncmp(arg, "s=", 2) || !strncmp(arg, "spaces=", 7)) { |
| 695 | while (*arg++ != '=') { |
| 696 | } |
| 697 | if (('0' <= arg[0]) && (arg[0] <= '8') && (arg[1] == '\x00')) { |
| 698 | g_flags.spaces = arg[0] - '0'; |
| 699 | continue; |
| 700 | } |
| 701 | return g_usage; |
| 702 | } |
| 703 | if (!strcmp(arg, "strict-json-pointer-syntax")) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 704 | g_flags.strict_json_pointer_syntax = true; |
Nigel Tao | d6fdfb1 | 2020-03-11 12:24:14 +1100 | [diff] [blame] | 705 | continue; |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 706 | } |
| 707 | if (!strcmp(arg, "t") || !strcmp(arg, "tabs")) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 708 | g_flags.tabs = true; |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 709 | continue; |
| 710 | } |
| 711 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 712 | return g_usage; |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 713 | } |
| 714 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 715 | if (g_flags.query_c_string && |
| 716 | !Query::validate(g_flags.query_c_string, strlen(g_flags.query_c_string), |
| 717 | g_flags.strict_json_pointer_syntax)) { |
Nigel Tao | d6fdfb1 | 2020-03-11 12:24:14 +1100 | [diff] [blame] | 718 | return "main: bad JSON Pointer (RFC 6901) syntax for the -query=STR flag"; |
| 719 | } |
| 720 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 721 | g_flags.remaining_argc = argc - c; |
| 722 | g_flags.remaining_argv = argv + c; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 723 | return nullptr; |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 724 | } |
| 725 | |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 726 | const char* // |
| 727 | initialize_globals(int argc, char** argv) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 728 | g_dst = wuffs_base__make_io_buffer( |
| 729 | wuffs_base__make_slice_u8(g_dst_array, DST_BUFFER_ARRAY_SIZE), |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 730 | wuffs_base__empty_io_buffer_meta()); |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 731 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 732 | g_src = wuffs_base__make_io_buffer( |
| 733 | wuffs_base__make_slice_u8(g_src_array, SRC_BUFFER_ARRAY_SIZE), |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 734 | wuffs_base__empty_io_buffer_meta()); |
| 735 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 736 | g_tok = wuffs_base__make_token_buffer( |
| 737 | wuffs_base__make_slice_token(g_tok_array, TOKEN_BUFFER_ARRAY_SIZE), |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 738 | wuffs_base__empty_token_buffer_meta()); |
| 739 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 740 | g_curr_token_end_src_index = 0; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 741 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 742 | g_depth = 0; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 743 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 744 | g_ctx = context::none; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 745 | |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 746 | TRY(parse_flags(argc, argv)); |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 747 | if (g_flags.fail_if_unsandboxed && !g_sandboxed) { |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 748 | return "main: unsandboxed"; |
| 749 | } |
Nigel Tao | 01abc84 | 2020-03-06 21:42:33 +1100 | [diff] [blame] | 750 | const int stdin_fd = 0; |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 751 | if (g_flags.remaining_argc > |
| 752 | ((g_input_file_descriptor != stdin_fd) ? 1 : 0)) { |
| 753 | return g_usage; |
Nigel Tao | 107f0ef | 2020-03-01 21:35:02 +1100 | [diff] [blame] | 754 | } |
| 755 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 756 | g_query.reset(g_flags.query_c_string); |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 757 | |
| 758 | // If the query is non-empty, suprress writing to stdout until we've |
| 759 | // completed the query. |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 760 | g_suppress_write_dst = g_query.next_fragment() ? 1 : 0; |
| 761 | g_wrote_to_dst = false; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 762 | |
Nigel Tao | 4e19359 | 2020-07-15 12:48:57 +1000 | [diff] [blame] | 763 | if (g_flags.input_format == file_format::json) { |
| 764 | TRY(g_json_decoder |
| 765 | .initialize(sizeof__wuffs_json__decoder(), WUFFS_VERSION, 0) |
| 766 | .message()); |
| 767 | g_dec = g_json_decoder.upcast_as__wuffs_base__token_decoder(); |
| 768 | } else { |
| 769 | TRY(g_cbor_decoder |
| 770 | .initialize(sizeof__wuffs_cbor__decoder(), WUFFS_VERSION, 0) |
| 771 | .message()); |
| 772 | g_dec = g_cbor_decoder.upcast_as__wuffs_base__token_decoder(); |
| 773 | } |
Nigel Tao | 4b186b0 | 2020-03-18 14:25:21 +1100 | [diff] [blame] | 774 | |
Nigel Tao | c766bb7 | 2020-07-09 12:59:32 +1000 | [diff] [blame] | 775 | if (g_flags.input_json_extra_comma) { |
Nigel Tao | 4e19359 | 2020-07-15 12:48:57 +1000 | [diff] [blame] | 776 | g_dec->set_quirk_enabled(WUFFS_JSON__QUIRK_ALLOW_EXTRA_COMMA, true); |
Nigel Tao | c766bb7 | 2020-07-09 12:59:32 +1000 | [diff] [blame] | 777 | } |
| 778 | |
Nigel Tao | 4b186b0 | 2020-03-18 14:25:21 +1100 | [diff] [blame] | 779 | // Consume an optional whitespace trailer. This isn't part of the JSON spec, |
| 780 | // but it works better with line oriented Unix tools (such as "echo 123 | |
| 781 | // jsonptr" where it's "echo", not "echo -n") or hand-edited JSON files which |
| 782 | // can accidentally contain trailing whitespace. |
Nigel Tao | 4e19359 | 2020-07-15 12:48:57 +1000 | [diff] [blame] | 783 | 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] | 784 | |
| 785 | return nullptr; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 786 | } |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 787 | |
| 788 | // ---- |
| 789 | |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 790 | // ignore_return_value suppresses errors from -Wall -Werror. |
| 791 | static void // |
| 792 | ignore_return_value(int ignored) {} |
| 793 | |
Nigel Tao | 2914bae | 2020-02-26 09:40:30 +1100 | [diff] [blame] | 794 | const char* // |
| 795 | read_src() { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 796 | if (g_src.meta.closed) { |
Nigel Tao | 9cc2c25 | 2020-02-23 17:05:49 +1100 | [diff] [blame] | 797 | return "main: internal error: read requested on a closed source"; |
Nigel Tao | a840692 | 2020-02-19 12:22:00 +1100 | [diff] [blame] | 798 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 799 | g_src.compact(); |
| 800 | if (g_src.meta.wi >= g_src.data.len) { |
| 801 | return "main: g_src buffer is full"; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 802 | } |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 803 | while (true) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 804 | ssize_t n = read(g_input_file_descriptor, g_src.data.ptr + g_src.meta.wi, |
| 805 | g_src.data.len - g_src.meta.wi); |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 806 | if (n >= 0) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 807 | g_src.meta.wi += n; |
| 808 | g_src.meta.closed = n == 0; |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 809 | break; |
| 810 | } else if (errno != EINTR) { |
| 811 | return strerror(errno); |
| 812 | } |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 813 | } |
| 814 | return nullptr; |
| 815 | } |
| 816 | |
Nigel Tao | 2914bae | 2020-02-26 09:40:30 +1100 | [diff] [blame] | 817 | const char* // |
| 818 | flush_dst() { |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 819 | while (true) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 820 | size_t n = g_dst.meta.wi - g_dst.meta.ri; |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 821 | if (n == 0) { |
| 822 | break; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 823 | } |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 824 | const int stdout_fd = 1; |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 825 | ssize_t i = write(stdout_fd, g_dst.data.ptr + g_dst.meta.ri, n); |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 826 | if (i >= 0) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 827 | g_dst.meta.ri += i; |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 828 | } else if (errno != EINTR) { |
| 829 | return strerror(errno); |
| 830 | } |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 831 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 832 | g_dst.compact(); |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 833 | return nullptr; |
| 834 | } |
| 835 | |
Nigel Tao | 2914bae | 2020-02-26 09:40:30 +1100 | [diff] [blame] | 836 | const char* // |
| 837 | write_dst(const void* s, size_t n) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 838 | if (g_suppress_write_dst > 0) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 839 | return nullptr; |
| 840 | } |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 841 | const uint8_t* p = static_cast<const uint8_t*>(s); |
| 842 | while (n > 0) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 843 | size_t i = g_dst.writer_available(); |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 844 | if (i == 0) { |
| 845 | const char* z = flush_dst(); |
| 846 | if (z) { |
| 847 | return z; |
| 848 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 849 | i = g_dst.writer_available(); |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 850 | if (i == 0) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 851 | return "main: g_dst buffer is full"; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 852 | } |
| 853 | } |
| 854 | |
| 855 | if (i > n) { |
| 856 | i = n; |
| 857 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 858 | memcpy(g_dst.data.ptr + g_dst.meta.wi, p, i); |
| 859 | g_dst.meta.wi += i; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 860 | p += i; |
| 861 | n -= i; |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 862 | g_wrote_to_dst = true; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 863 | } |
| 864 | return nullptr; |
| 865 | } |
| 866 | |
| 867 | // ---- |
| 868 | |
Nigel Tao | 168f60a | 2020-07-14 13:19:33 +1000 | [diff] [blame] | 869 | const char* // |
| 870 | write_literal(uint64_t vbd) { |
| 871 | const char* ptr = nullptr; |
| 872 | size_t len = 0; |
| 873 | if (vbd & WUFFS_BASE__TOKEN__VBD__LITERAL__UNDEFINED) { |
| 874 | if (g_flags.output_format == file_format::json) { |
| 875 | ptr = "null"; // JSON's closest approximation to "undefined". |
| 876 | len = 4; |
| 877 | } else { |
| 878 | ptr = "\xF7"; |
| 879 | len = 1; |
| 880 | } |
| 881 | } else if (vbd & WUFFS_BASE__TOKEN__VBD__LITERAL__NULL) { |
| 882 | if (g_flags.output_format == file_format::json) { |
| 883 | ptr = "null"; |
| 884 | len = 4; |
| 885 | } else { |
| 886 | ptr = "\xF6"; |
| 887 | len = 1; |
| 888 | } |
| 889 | } else if (vbd & WUFFS_BASE__TOKEN__VBD__LITERAL__FALSE) { |
| 890 | if (g_flags.output_format == file_format::json) { |
| 891 | ptr = "false"; |
| 892 | len = 5; |
| 893 | } else { |
| 894 | ptr = "\xF4"; |
| 895 | len = 1; |
| 896 | } |
| 897 | } else if (vbd & WUFFS_BASE__TOKEN__VBD__LITERAL__TRUE) { |
| 898 | if (g_flags.output_format == file_format::json) { |
| 899 | ptr = "true"; |
| 900 | len = 4; |
| 901 | } else { |
| 902 | ptr = "\xF5"; |
| 903 | len = 1; |
| 904 | } |
| 905 | } else { |
| 906 | return "main: internal error: unexpected write_literal argument"; |
| 907 | } |
| 908 | return write_dst(ptr, len); |
| 909 | } |
| 910 | |
| 911 | // ---- |
| 912 | |
| 913 | const char* // |
Nigel Tao | 664f843 | 2020-07-16 21:25:14 +1000 | [diff] [blame] | 914 | write_number_as_cbor_f64(double f) { |
Nigel Tao | 168f60a | 2020-07-14 13:19:33 +1000 | [diff] [blame] | 915 | uint8_t buf[9]; |
| 916 | wuffs_base__lossy_value_u16 lv16 = |
| 917 | wuffs_base__ieee_754_bit_representation__from_f64_to_u16_truncate(f); |
| 918 | if (!lv16.lossy) { |
| 919 | buf[0] = 0xF9; |
| 920 | wuffs_base__store_u16be__no_bounds_check(&buf[1], lv16.value); |
| 921 | return write_dst(&buf[0], 3); |
| 922 | } |
| 923 | wuffs_base__lossy_value_u32 lv32 = |
| 924 | wuffs_base__ieee_754_bit_representation__from_f64_to_u32_truncate(f); |
| 925 | if (!lv32.lossy) { |
| 926 | buf[0] = 0xFA; |
| 927 | wuffs_base__store_u32be__no_bounds_check(&buf[1], lv32.value); |
| 928 | return write_dst(&buf[0], 5); |
| 929 | } |
| 930 | buf[0] = 0xFB; |
| 931 | wuffs_base__store_u64be__no_bounds_check( |
| 932 | &buf[1], wuffs_base__ieee_754_bit_representation__from_f64_to_u64(f)); |
| 933 | return write_dst(&buf[0], 9); |
| 934 | } |
| 935 | |
| 936 | const char* // |
Nigel Tao | 664f843 | 2020-07-16 21:25:14 +1000 | [diff] [blame] | 937 | write_number_as_cbor_u64(uint8_t base, uint64_t u) { |
Nigel Tao | 168f60a | 2020-07-14 13:19:33 +1000 | [diff] [blame] | 938 | uint8_t buf[9]; |
| 939 | if (u < 0x18) { |
| 940 | buf[0] = base | ((uint8_t)u); |
| 941 | return write_dst(&buf[0], 1); |
| 942 | } else if ((u >> 8) == 0) { |
| 943 | buf[0] = base | 0x18; |
| 944 | buf[1] = ((uint8_t)u); |
| 945 | return write_dst(&buf[0], 2); |
| 946 | } else if ((u >> 16) == 0) { |
| 947 | buf[0] = base | 0x19; |
| 948 | wuffs_base__store_u16be__no_bounds_check(&buf[1], ((uint16_t)u)); |
| 949 | return write_dst(&buf[0], 3); |
| 950 | } else if ((u >> 32) == 0) { |
| 951 | buf[0] = base | 0x1A; |
| 952 | wuffs_base__store_u32be__no_bounds_check(&buf[1], ((uint32_t)u)); |
| 953 | return write_dst(&buf[0], 5); |
| 954 | } |
| 955 | buf[0] = base | 0x1B; |
| 956 | wuffs_base__store_u64be__no_bounds_check(&buf[1], u); |
| 957 | return write_dst(&buf[0], 9); |
| 958 | } |
| 959 | |
| 960 | const char* // |
Nigel Tao | 664f843 | 2020-07-16 21:25:14 +1000 | [diff] [blame] | 961 | write_cbor_number_as_json(uint8_t* ptr, |
| 962 | size_t len, |
| 963 | bool ignore_first_byte, |
| 964 | bool minus_1_minus_x) { |
| 965 | if (ignore_first_byte) { |
| 966 | if (len == 0) { |
| 967 | return "main: internal error: ignore_first_byte with no bytes"; |
| 968 | } |
| 969 | ptr++; |
| 970 | len--; |
| 971 | } |
| 972 | uint64_t u; |
| 973 | switch (len) { |
| 974 | case 1: |
| 975 | u = wuffs_base__load_u8__no_bounds_check(ptr); |
| 976 | break; |
| 977 | case 2: |
| 978 | u = wuffs_base__load_u16be__no_bounds_check(ptr); |
| 979 | break; |
| 980 | case 4: |
| 981 | u = wuffs_base__load_u32be__no_bounds_check(ptr); |
| 982 | break; |
| 983 | case 8: |
| 984 | u = wuffs_base__load_u64be__no_bounds_check(ptr); |
| 985 | break; |
| 986 | default: |
| 987 | return "main: internal error: unexpected cbor number byte length"; |
| 988 | } |
| 989 | uint8_t buf[1 + WUFFS_BASE__U64__BYTE_LENGTH__MAX_INCL]; |
| 990 | uint8_t* b = &buf[0]; |
| 991 | if (minus_1_minus_x) { |
| 992 | u++; |
| 993 | if (u == 0) { |
| 994 | // See the cbor.TOKEN_VALUE_MINOR__MINUS_1_MINUS_X comment re overflow. |
| 995 | return write_dst("-18446744073709551616", 21); |
| 996 | } |
| 997 | *b++ = '-'; |
| 998 | } |
| 999 | size_t n = wuffs_base__render_number_u64( |
| 1000 | wuffs_base__make_slice_u8(b, WUFFS_BASE__U64__BYTE_LENGTH__MAX_INCL), u, |
| 1001 | WUFFS_BASE__RENDER_NUMBER_XXX__DEFAULT_OPTIONS); |
| 1002 | return write_dst(&buf[0], n + (minus_1_minus_x ? 1 : 0)); |
| 1003 | } |
| 1004 | |
| 1005 | const char* // |
Nigel Tao | 168f60a | 2020-07-14 13:19:33 +1000 | [diff] [blame] | 1006 | write_number(uint64_t vbd, uint8_t* ptr, size_t len) { |
Nigel Tao | 4e19359 | 2020-07-15 12:48:57 +1000 | [diff] [blame] | 1007 | if (g_flags.output_format == file_format::json) { |
| 1008 | if (vbd & WUFFS_BASE__TOKEN__VBD__NUMBER__FORMAT_TEXT) { |
Nigel Tao | 168f60a | 2020-07-14 13:19:33 +1000 | [diff] [blame] | 1009 | return write_dst(ptr, len); |
Nigel Tao | 4e19359 | 2020-07-15 12:48:57 +1000 | [diff] [blame] | 1010 | } else if ((vbd & |
| 1011 | WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_INTEGER_UNSIGNED) && |
| 1012 | (vbd & |
| 1013 | WUFFS_BASE__TOKEN__VBD__NUMBER__FORMAT_BINARY_BIG_ENDIAN)) { |
Nigel Tao | 664f843 | 2020-07-16 21:25:14 +1000 | [diff] [blame] | 1014 | return write_cbor_number_as_json( |
| 1015 | ptr, len, |
| 1016 | vbd & WUFFS_BASE__TOKEN__VBD__NUMBER__FORMAT_IGNORE_FIRST_BYTE, |
| 1017 | false); |
Nigel Tao | 168f60a | 2020-07-14 13:19:33 +1000 | [diff] [blame] | 1018 | } |
| 1019 | |
Nigel Tao | 4e19359 | 2020-07-15 12:48:57 +1000 | [diff] [blame] | 1020 | // From here on, (g_flags.output_format == file_format::cbor). |
| 1021 | } else if (vbd & WUFFS_BASE__TOKEN__VBD__NUMBER__FORMAT_BINARY_BIG_ENDIAN) { |
| 1022 | return write_dst(ptr, len); |
| 1023 | } else if (vbd & WUFFS_BASE__TOKEN__VBD__NUMBER__FORMAT_TEXT) { |
Nigel Tao | 168f60a | 2020-07-14 13:19:33 +1000 | [diff] [blame] | 1024 | // First try to parse (ptr, len) as an integer. Something like |
| 1025 | // "1180591620717411303424" is a valid number (in the JSON sense) but will |
| 1026 | // overflow int64_t or uint64_t, so fall back to parsing it as a float64. |
| 1027 | if (vbd & WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_INTEGER_SIGNED) { |
| 1028 | if ((len > 0) && (ptr[0] == '-')) { |
| 1029 | wuffs_base__result_i64 ri = wuffs_base__parse_number_i64( |
| 1030 | wuffs_base__make_slice_u8(ptr, len), |
| 1031 | WUFFS_BASE__PARSE_NUMBER_XXX__DEFAULT_OPTIONS); |
| 1032 | if (ri.status.is_ok()) { |
Nigel Tao | 664f843 | 2020-07-16 21:25:14 +1000 | [diff] [blame] | 1033 | return write_number_as_cbor_u64(0x20, ~ri.value); |
Nigel Tao | 168f60a | 2020-07-14 13:19:33 +1000 | [diff] [blame] | 1034 | } |
| 1035 | } else { |
| 1036 | wuffs_base__result_u64 ru = wuffs_base__parse_number_u64( |
| 1037 | wuffs_base__make_slice_u8(ptr, len), |
| 1038 | WUFFS_BASE__PARSE_NUMBER_XXX__DEFAULT_OPTIONS); |
| 1039 | if (ru.status.is_ok()) { |
Nigel Tao | 664f843 | 2020-07-16 21:25:14 +1000 | [diff] [blame] | 1040 | return write_number_as_cbor_u64(0x00, ru.value); |
Nigel Tao | 168f60a | 2020-07-14 13:19:33 +1000 | [diff] [blame] | 1041 | } |
| 1042 | } |
| 1043 | } |
| 1044 | |
| 1045 | if (vbd & WUFFS_BASE__TOKEN__VBD__NUMBER__CONTENT_FLOATING_POINT) { |
| 1046 | wuffs_base__result_f64 rf = wuffs_base__parse_number_f64( |
| 1047 | wuffs_base__make_slice_u8(ptr, len), |
| 1048 | WUFFS_BASE__PARSE_NUMBER_XXX__DEFAULT_OPTIONS); |
| 1049 | if (rf.status.is_ok()) { |
Nigel Tao | 664f843 | 2020-07-16 21:25:14 +1000 | [diff] [blame] | 1050 | return write_number_as_cbor_f64(rf.value); |
Nigel Tao | 168f60a | 2020-07-14 13:19:33 +1000 | [diff] [blame] | 1051 | } |
| 1052 | } |
| 1053 | } |
| 1054 | |
Nigel Tao | 4e19359 | 2020-07-15 12:48:57 +1000 | [diff] [blame] | 1055 | fail: |
Nigel Tao | 168f60a | 2020-07-14 13:19:33 +1000 | [diff] [blame] | 1056 | return "main: internal error: unexpected write_number argument"; |
| 1057 | } |
| 1058 | |
Nigel Tao | 4e19359 | 2020-07-15 12:48:57 +1000 | [diff] [blame] | 1059 | const char* // |
| 1060 | write_inline_integer(uint64_t vbd, uint8_t* ptr, size_t len) { |
| 1061 | if (g_flags.output_format == file_format::cbor) { |
| 1062 | return write_dst(ptr, len); |
| 1063 | } |
| 1064 | |
| 1065 | uint8_t buf[WUFFS_BASE__I64__BYTE_LENGTH__MAX_INCL]; |
| 1066 | size_t n = wuffs_base__render_number_i64( |
| 1067 | wuffs_base__make_slice_u8(&buf[0], sizeof buf), (int16_t)vbd, |
| 1068 | WUFFS_BASE__RENDER_NUMBER_XXX__DEFAULT_OPTIONS); |
| 1069 | return write_dst(&buf[0], n); |
| 1070 | } |
| 1071 | |
Nigel Tao | 168f60a | 2020-07-14 13:19:33 +1000 | [diff] [blame] | 1072 | // ---- |
| 1073 | |
Nigel Tao | 2914bae | 2020-02-26 09:40:30 +1100 | [diff] [blame] | 1074 | uint8_t // |
| 1075 | hex_digit(uint8_t nibble) { |
Nigel Tao | b5461bd | 2020-02-21 14:13:37 +1100 | [diff] [blame] | 1076 | nibble &= 0x0F; |
| 1077 | if (nibble <= 9) { |
| 1078 | return '0' + nibble; |
| 1079 | } |
| 1080 | return ('A' - 10) + nibble; |
| 1081 | } |
| 1082 | |
Nigel Tao | 2914bae | 2020-02-26 09:40:30 +1100 | [diff] [blame] | 1083 | const char* // |
Nigel Tao | 168f60a | 2020-07-14 13:19:33 +1000 | [diff] [blame] | 1084 | flush_cbor_output_string() { |
| 1085 | uint8_t prefix[3]; |
| 1086 | prefix[0] = g_cbor_output_string_is_utf_8 ? 0x60 : 0x40; |
| 1087 | if (g_cbor_output_string_length < 0x18) { |
| 1088 | prefix[0] |= g_cbor_output_string_length; |
| 1089 | TRY(write_dst(&prefix[0], 1)); |
| 1090 | } else if (g_cbor_output_string_length <= 0xFF) { |
| 1091 | prefix[0] |= 0x18; |
| 1092 | prefix[1] = g_cbor_output_string_length; |
| 1093 | TRY(write_dst(&prefix[0], 2)); |
| 1094 | } else if (g_cbor_output_string_length <= 0xFFFF) { |
| 1095 | prefix[0] |= 0x19; |
| 1096 | prefix[1] = g_cbor_output_string_length >> 8; |
| 1097 | prefix[2] = g_cbor_output_string_length; |
| 1098 | TRY(write_dst(&prefix[0], 3)); |
| 1099 | } else { |
| 1100 | return "main: internal error: CBOR string output is too long"; |
| 1101 | } |
| 1102 | |
| 1103 | size_t n = g_cbor_output_string_length; |
| 1104 | g_cbor_output_string_length = 0; |
| 1105 | return write_dst(&g_cbor_output_string_array[0], n); |
| 1106 | } |
| 1107 | |
| 1108 | const char* // |
| 1109 | write_cbor_output_string(uint8_t* ptr, size_t len, bool finish) { |
| 1110 | // Check that g_cbor_output_string_array can hold any UTF-8 code point. |
| 1111 | if (CBOR_OUTPUT_STRING_ARRAY_SIZE < 4) { |
| 1112 | return "main: internal error: CBOR_OUTPUT_STRING_ARRAY_SIZE is too short"; |
| 1113 | } |
| 1114 | |
| 1115 | while (len > 0) { |
| 1116 | size_t available = |
| 1117 | CBOR_OUTPUT_STRING_ARRAY_SIZE - g_cbor_output_string_length; |
| 1118 | if (available >= len) { |
| 1119 | memcpy(&g_cbor_output_string_array[g_cbor_output_string_length], ptr, |
| 1120 | len); |
| 1121 | g_cbor_output_string_length += len; |
| 1122 | ptr += len; |
| 1123 | len = 0; |
| 1124 | break; |
| 1125 | |
| 1126 | } else if (available > 0) { |
| 1127 | if (!g_cbor_output_string_is_multiple_chunks) { |
| 1128 | g_cbor_output_string_is_multiple_chunks = true; |
| 1129 | TRY(write_dst(g_cbor_output_string_is_utf_8 ? "\x7F" : "\x5F", 1)); |
Nigel Tao | 3b48698 | 2020-02-27 15:05:59 +1100 | [diff] [blame] | 1130 | } |
Nigel Tao | 168f60a | 2020-07-14 13:19:33 +1000 | [diff] [blame] | 1131 | |
| 1132 | if (g_cbor_output_string_is_utf_8) { |
| 1133 | // Walk the end backwards to a UTF-8 boundary, so that each chunk of |
| 1134 | // the multi-chunk string is also valid UTF-8. |
| 1135 | while (available > 0) { |
| 1136 | wuffs_base__utf_8__next__output o = wuffs_base__utf_8__next_from_end( |
| 1137 | wuffs_base__make_slice_u8(ptr, available)); |
| 1138 | if ((o.code_point != WUFFS_BASE__UNICODE_REPLACEMENT_CHARACTER) || |
| 1139 | (o.byte_length != 1)) { |
| 1140 | break; |
| 1141 | } |
| 1142 | available--; |
| 1143 | } |
| 1144 | } |
| 1145 | |
| 1146 | memcpy(&g_cbor_output_string_array[g_cbor_output_string_length], ptr, |
| 1147 | available); |
| 1148 | g_cbor_output_string_length += available; |
| 1149 | ptr += available; |
| 1150 | len -= available; |
Nigel Tao | 3b48698 | 2020-02-27 15:05:59 +1100 | [diff] [blame] | 1151 | } |
| 1152 | |
Nigel Tao | 168f60a | 2020-07-14 13:19:33 +1000 | [diff] [blame] | 1153 | TRY(flush_cbor_output_string()); |
| 1154 | } |
Nigel Tao | b9ad34f | 2020-03-03 12:44:01 +1100 | [diff] [blame] | 1155 | |
Nigel Tao | 168f60a | 2020-07-14 13:19:33 +1000 | [diff] [blame] | 1156 | if (finish) { |
| 1157 | TRY(flush_cbor_output_string()); |
| 1158 | if (g_cbor_output_string_is_multiple_chunks) { |
| 1159 | TRY(write_dst("\xFF", 1)); |
| 1160 | } |
| 1161 | } |
| 1162 | return nullptr; |
| 1163 | } |
Nigel Tao | b9ad34f | 2020-03-03 12:44:01 +1100 | [diff] [blame] | 1164 | |
Nigel Tao | 168f60a | 2020-07-14 13:19:33 +1000 | [diff] [blame] | 1165 | const char* // |
| 1166 | handle_string(uint64_t vbd, |
| 1167 | uint64_t len, |
| 1168 | bool start_of_token_chain, |
| 1169 | bool continued) { |
| 1170 | if (start_of_token_chain) { |
| 1171 | if (g_flags.output_format == file_format::json) { |
| 1172 | TRY(write_dst("\"", 1)); |
| 1173 | } else { |
| 1174 | g_cbor_output_string_length = 0; |
| 1175 | g_cbor_output_string_is_multiple_chunks = false; |
| 1176 | g_cbor_output_string_is_utf_8 = |
| 1177 | vbd & WUFFS_BASE__TOKEN__VBD__STRING__CHAIN_MUST_BE_UTF_8; |
| 1178 | } |
| 1179 | g_query.restart_fragment(in_dict_before_key() && g_query.is_at(g_depth)); |
| 1180 | } |
| 1181 | |
| 1182 | if (vbd & WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_0_DST_1_SRC_DROP) { |
| 1183 | // No-op. |
| 1184 | } else if (vbd & WUFFS_BASE__TOKEN__VBD__STRING__CONVERT_1_DST_1_SRC_COPY) { |
| 1185 | uint8_t* ptr = g_src.data.ptr + g_curr_token_end_src_index - len; |
| 1186 | if (g_flags.output_format == file_format::json) { |
Nigel Tao | af75772 | 2020-07-18 17:27:11 +1000 | [diff] [blame^] | 1187 | if (g_flags.input_format == file_format::json) { |
| 1188 | TRY(write_dst(ptr, len)); |
| 1189 | } else if (vbd & WUFFS_BASE__TOKEN__VBD__STRING__CHAIN_MUST_BE_UTF_8) { |
| 1190 | // TODO: escape '\n', '\"', etc. |
| 1191 | TRY(write_dst(ptr, len)); |
| 1192 | } else { |
| 1193 | uint8_t as_hex[512]; |
| 1194 | uint8_t* p = ptr; |
| 1195 | size_t n = len; |
| 1196 | while (n > 0) { |
| 1197 | wuffs_base__transform__output o = wuffs_base__base_16__encode2( |
| 1198 | wuffs_base__make_slice_u8(&as_hex[0], sizeof as_hex), |
| 1199 | wuffs_base__make_slice_u8(p, n), true, |
| 1200 | WUFFS_BASE__BASE_16__DEFAULT_OPTIONS); |
| 1201 | TRY(write_dst(&as_hex[0], o.num_dst)); |
| 1202 | p += o.num_src; |
| 1203 | n -= o.num_src; |
| 1204 | if (!o.status.is_ok()) { |
| 1205 | return o.status.message(); |
| 1206 | } |
| 1207 | } |
| 1208 | } |
Nigel Tao | 168f60a | 2020-07-14 13:19:33 +1000 | [diff] [blame] | 1209 | } else { |
| 1210 | TRY(write_cbor_output_string(ptr, len, false)); |
| 1211 | } |
| 1212 | g_query.incremental_match_slice(ptr, len); |
Nigel Tao | b9ad34f | 2020-03-03 12:44:01 +1100 | [diff] [blame] | 1213 | } else { |
Nigel Tao | 168f60a | 2020-07-14 13:19:33 +1000 | [diff] [blame] | 1214 | return "main: internal error: unexpected string-token conversion"; |
| 1215 | } |
| 1216 | |
| 1217 | if (continued) { |
| 1218 | return nullptr; |
| 1219 | } |
| 1220 | |
| 1221 | if (g_flags.output_format == file_format::json) { |
| 1222 | TRY(write_dst("\"", 1)); |
| 1223 | } else { |
| 1224 | TRY(write_cbor_output_string(nullptr, 0, true)); |
| 1225 | } |
| 1226 | return nullptr; |
| 1227 | } |
| 1228 | |
| 1229 | const char* // |
| 1230 | handle_unicode_code_point(uint32_t ucp) { |
| 1231 | if (g_flags.output_format == file_format::json) { |
| 1232 | if (ucp < 0x0020) { |
| 1233 | switch (ucp) { |
| 1234 | case '\b': |
| 1235 | return write_dst("\\b", 2); |
| 1236 | case '\f': |
| 1237 | return write_dst("\\f", 2); |
| 1238 | case '\n': |
| 1239 | return write_dst("\\n", 2); |
| 1240 | case '\r': |
| 1241 | return write_dst("\\r", 2); |
| 1242 | case '\t': |
| 1243 | return write_dst("\\t", 2); |
| 1244 | } |
| 1245 | |
| 1246 | // Other bytes less than 0x0020 are valid UTF-8 but not valid in a |
| 1247 | // JSON string. They need to remain escaped. |
| 1248 | uint8_t esc6[6]; |
| 1249 | esc6[0] = '\\'; |
| 1250 | esc6[1] = 'u'; |
| 1251 | esc6[2] = '0'; |
| 1252 | esc6[3] = '0'; |
| 1253 | esc6[4] = hex_digit(ucp >> 4); |
| 1254 | esc6[5] = hex_digit(ucp >> 0); |
| 1255 | return write_dst(&esc6[0], 6); |
| 1256 | |
| 1257 | } else if (ucp == '\"') { |
| 1258 | return write_dst("\\\"", 2); |
| 1259 | |
| 1260 | } else if (ucp == '\\') { |
| 1261 | return write_dst("\\\\", 2); |
Nigel Tao | 3b48698 | 2020-02-27 15:05:59 +1100 | [diff] [blame] | 1262 | } |
Nigel Tao | 3b48698 | 2020-02-27 15:05:59 +1100 | [diff] [blame] | 1263 | } |
| 1264 | |
Nigel Tao | 168f60a | 2020-07-14 13:19:33 +1000 | [diff] [blame] | 1265 | uint8_t u[WUFFS_BASE__UTF_8__BYTE_LENGTH__MAX_INCL]; |
| 1266 | size_t n = wuffs_base__utf_8__encode( |
| 1267 | wuffs_base__make_slice_u8(&u[0], |
| 1268 | WUFFS_BASE__UTF_8__BYTE_LENGTH__MAX_INCL), |
| 1269 | ucp); |
| 1270 | if (n == 0) { |
| 1271 | return "main: internal error: unexpected Unicode code point"; |
| 1272 | } |
| 1273 | |
| 1274 | if (g_flags.output_format == file_format::json) { |
| 1275 | return write_dst(&u[0], n); |
| 1276 | } |
| 1277 | return write_cbor_output_string(&u[0], n, false); |
Nigel Tao | 3b48698 | 2020-02-27 15:05:59 +1100 | [diff] [blame] | 1278 | } |
| 1279 | |
| 1280 | const char* // |
Nigel Tao | 2ef3999 | 2020-04-09 17:24:39 +1000 | [diff] [blame] | 1281 | handle_token(wuffs_base__token t, bool start_of_token_chain) { |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1282 | do { |
Nigel Tao | 462f866 | 2020-04-01 23:01:51 +1100 | [diff] [blame] | 1283 | int64_t vbc = t.value_base_category(); |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1284 | uint64_t vbd = t.value_base_detail(); |
| 1285 | uint64_t len = t.length(); |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1286 | |
| 1287 | // Handle ']' or '}'. |
Nigel Tao | 9f7a250 | 2020-02-23 09:42:02 +1100 | [diff] [blame] | 1288 | if ((vbc == WUFFS_BASE__TOKEN__VBC__STRUCTURE) && |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1289 | (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__POP)) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1290 | if (g_query.is_at(g_depth)) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1291 | return "main: no match for query"; |
| 1292 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1293 | if (g_depth <= 0) { |
| 1294 | return "main: internal error: inconsistent g_depth"; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1295 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1296 | g_depth--; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1297 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1298 | if (g_query.matched_all() && (g_depth >= g_flags.max_output_depth)) { |
| 1299 | g_suppress_write_dst--; |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 1300 | // '…' is U+2026 HORIZONTAL ELLIPSIS, which is 3 UTF-8 bytes. |
Nigel Tao | 168f60a | 2020-07-14 13:19:33 +1000 | [diff] [blame] | 1301 | if (g_flags.output_format == file_format::json) { |
| 1302 | TRY(write_dst((vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__FROM_LIST) |
| 1303 | ? "\"[…]\"" |
| 1304 | : "\"{…}\"", |
| 1305 | 7)); |
| 1306 | } else { |
| 1307 | TRY(write_dst((vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__FROM_LIST) |
| 1308 | ? "\x65[…]" |
| 1309 | : "\x65{…}", |
| 1310 | 6)); |
| 1311 | } |
| 1312 | } else if (g_flags.output_format == file_format::json) { |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 1313 | // Write preceding whitespace. |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1314 | if ((g_ctx != context::in_list_after_bracket) && |
| 1315 | (g_ctx != context::in_dict_after_brace) && |
| 1316 | !g_flags.compact_output) { |
Nigel Tao | c766bb7 | 2020-07-09 12:59:32 +1000 | [diff] [blame] | 1317 | if (g_flags.output_json_extra_comma) { |
| 1318 | TRY(write_dst(",\n", 2)); |
| 1319 | } else { |
| 1320 | TRY(write_dst("\n", 1)); |
| 1321 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1322 | for (uint32_t i = 0; i < g_depth; i++) { |
| 1323 | TRY(write_dst( |
| 1324 | g_flags.tabs ? INDENT_TAB_STRING : INDENT_SPACES_STRING, |
Nigel Tao | ecadf72 | 2020-07-13 08:22:34 +1000 | [diff] [blame] | 1325 | g_flags.tabs ? 1 : g_flags.spaces)); |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 1326 | } |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1327 | } |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 1328 | |
| 1329 | TRY(write_dst( |
| 1330 | (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__FROM_LIST) ? "]" : "}", |
| 1331 | 1)); |
Nigel Tao | 168f60a | 2020-07-14 13:19:33 +1000 | [diff] [blame] | 1332 | } else { |
| 1333 | TRY(write_dst("\xFF", 1)); |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1334 | } |
| 1335 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1336 | g_ctx = (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_LIST) |
| 1337 | ? context::in_list_after_value |
| 1338 | : context::in_dict_after_key; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1339 | goto after_value; |
| 1340 | } |
| 1341 | |
Nigel Tao | d1c928a | 2020-02-28 12:43:53 +1100 | [diff] [blame] | 1342 | // Write preceding whitespace and punctuation, if it wasn't ']', '}' or a |
| 1343 | // continuation of a multi-token chain. |
Nigel Tao | 2ef3999 | 2020-04-09 17:24:39 +1000 | [diff] [blame] | 1344 | if (start_of_token_chain) { |
Nigel Tao | 168f60a | 2020-07-14 13:19:33 +1000 | [diff] [blame] | 1345 | if (g_flags.output_format != file_format::json) { |
| 1346 | // No-op. |
| 1347 | } else if (g_ctx == context::in_dict_after_key) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1348 | TRY(write_dst(": ", g_flags.compact_output ? 1 : 2)); |
| 1349 | } else if (g_ctx != context::none) { |
| 1350 | if ((g_ctx != context::in_list_after_bracket) && |
| 1351 | (g_ctx != context::in_dict_after_brace)) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1352 | TRY(write_dst(",", 1)); |
Nigel Tao | 107f0ef | 2020-03-01 21:35:02 +1100 | [diff] [blame] | 1353 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1354 | if (!g_flags.compact_output) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1355 | TRY(write_dst("\n", 1)); |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1356 | for (size_t i = 0; i < g_depth; i++) { |
| 1357 | TRY(write_dst( |
| 1358 | g_flags.tabs ? INDENT_TAB_STRING : INDENT_SPACES_STRING, |
Nigel Tao | ecadf72 | 2020-07-13 08:22:34 +1000 | [diff] [blame] | 1359 | g_flags.tabs ? 1 : g_flags.spaces)); |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1360 | } |
| 1361 | } |
| 1362 | } |
| 1363 | |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 1364 | bool query_matched_fragment = false; |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1365 | if (g_query.is_at(g_depth)) { |
| 1366 | switch (g_ctx) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1367 | case context::in_list_after_bracket: |
| 1368 | case context::in_list_after_value: |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1369 | query_matched_fragment = g_query.tick(); |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1370 | break; |
| 1371 | case context::in_dict_after_key: |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1372 | query_matched_fragment = g_query.matched_fragment(); |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1373 | break; |
Nigel Tao | 18ef5b4 | 2020-03-16 10:37:47 +1100 | [diff] [blame] | 1374 | default: |
| 1375 | break; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1376 | } |
| 1377 | } |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 1378 | if (!query_matched_fragment) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1379 | // No-op. |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1380 | } else if (!g_query.next_fragment()) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1381 | // There is no next fragment. We have matched the complete query, and |
| 1382 | // the upcoming JSON value is the result of that query. |
| 1383 | // |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1384 | // Un-suppress writing to stdout and reset the g_ctx and g_depth as if |
| 1385 | // we were about to decode a top-level value. This makes any subsequent |
| 1386 | // indentation be relative to this point, and we will return g_eod |
| 1387 | // after the upcoming JSON value is complete. |
| 1388 | if (g_suppress_write_dst != 1) { |
| 1389 | return "main: internal error: inconsistent g_suppress_write_dst"; |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 1390 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1391 | g_suppress_write_dst = 0; |
| 1392 | g_ctx = context::none; |
| 1393 | g_depth = 0; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1394 | } else if ((vbc != WUFFS_BASE__TOKEN__VBC__STRUCTURE) || |
| 1395 | !(vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__PUSH)) { |
| 1396 | // The query has moved on to the next fragment but the upcoming JSON |
| 1397 | // value is not a container. |
| 1398 | return "main: no match for query"; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1399 | } |
| 1400 | } |
| 1401 | |
| 1402 | // Handle the token itself: either a container ('[' or '{') or a simple |
Nigel Tao | 85fba7f | 2020-02-29 16:28:06 +1100 | [diff] [blame] | 1403 | // value: string (a chain of raw or escaped parts), literal or number. |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1404 | switch (vbc) { |
Nigel Tao | 85fba7f | 2020-02-29 16:28:06 +1100 | [diff] [blame] | 1405 | case WUFFS_BASE__TOKEN__VBC__STRUCTURE: |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1406 | if (g_query.matched_all() && (g_depth >= g_flags.max_output_depth)) { |
| 1407 | g_suppress_write_dst++; |
Nigel Tao | 168f60a | 2020-07-14 13:19:33 +1000 | [diff] [blame] | 1408 | } else if (g_flags.output_format == file_format::json) { |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 1409 | TRY(write_dst( |
| 1410 | (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_LIST) ? "[" : "{", |
| 1411 | 1)); |
Nigel Tao | 168f60a | 2020-07-14 13:19:33 +1000 | [diff] [blame] | 1412 | } else { |
| 1413 | TRY(write_dst((vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_LIST) |
| 1414 | ? "\x9F" |
| 1415 | : "\xBF", |
| 1416 | 1)); |
Nigel Tao | 52c4d6a | 2020-03-08 21:12:38 +1100 | [diff] [blame] | 1417 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1418 | g_depth++; |
| 1419 | g_ctx = (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_LIST) |
| 1420 | ? context::in_list_after_bracket |
| 1421 | : context::in_dict_after_brace; |
Nigel Tao | 85fba7f | 2020-02-29 16:28:06 +1100 | [diff] [blame] | 1422 | return nullptr; |
| 1423 | |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1424 | case WUFFS_BASE__TOKEN__VBC__STRING: |
Nigel Tao | 168f60a | 2020-07-14 13:19:33 +1000 | [diff] [blame] | 1425 | TRY(handle_string(vbd, len, start_of_token_chain, t.continued())); |
Nigel Tao | 496e88b | 2020-04-09 22:10:08 +1000 | [diff] [blame] | 1426 | if (t.continued()) { |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1427 | return nullptr; |
| 1428 | } |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1429 | goto after_value; |
| 1430 | |
| 1431 | case WUFFS_BASE__TOKEN__VBC__UNICODE_CODE_POINT: |
Nigel Tao | 496e88b | 2020-04-09 22:10:08 +1000 | [diff] [blame] | 1432 | if (!t.continued()) { |
| 1433 | return "main: internal error: unexpected non-continued UCP token"; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1434 | } |
| 1435 | TRY(handle_unicode_code_point(vbd)); |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1436 | g_query.incremental_match_code_point(vbd); |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1437 | return nullptr; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1438 | |
Nigel Tao | 85fba7f | 2020-02-29 16:28:06 +1100 | [diff] [blame] | 1439 | case WUFFS_BASE__TOKEN__VBC__LITERAL: |
Nigel Tao | 168f60a | 2020-07-14 13:19:33 +1000 | [diff] [blame] | 1440 | TRY(write_literal(vbd)); |
| 1441 | goto after_value; |
| 1442 | |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1443 | case WUFFS_BASE__TOKEN__VBC__NUMBER: |
Nigel Tao | 168f60a | 2020-07-14 13:19:33 +1000 | [diff] [blame] | 1444 | TRY(write_number(vbd, g_src.data.ptr + g_curr_token_end_src_index - len, |
| 1445 | len)); |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1446 | goto after_value; |
Nigel Tao | 4e19359 | 2020-07-15 12:48:57 +1000 | [diff] [blame] | 1447 | |
| 1448 | case WUFFS_BASE__TOKEN__VBC__INLINE_INTEGER: |
| 1449 | TRY(write_inline_integer( |
| 1450 | vbd, g_src.data.ptr + g_curr_token_end_src_index - len, len)); |
| 1451 | goto after_value; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1452 | } |
| 1453 | |
Nigel Tao | 664f843 | 2020-07-16 21:25:14 +1000 | [diff] [blame] | 1454 | if (t.value_major() == WUFFS_CBOR__TOKEN_VALUE_MAJOR) { |
| 1455 | uint64_t value_minor = t.value_minor(); |
| 1456 | if (value_minor & WUFFS_CBOR__TOKEN_VALUE_MINOR__TAG) { |
| 1457 | // TODO: CBOR tags. |
| 1458 | } else if (value_minor & WUFFS_CBOR__TOKEN_VALUE_MINOR__MINUS_1_MINUS_X) { |
| 1459 | TRY(write_cbor_number_as_json( |
| 1460 | g_src.data.ptr + g_curr_token_end_src_index - len, len, true, |
| 1461 | true)); |
| 1462 | goto after_value; |
| 1463 | } |
| 1464 | } |
| 1465 | |
| 1466 | // Return an error if we didn't match the (value_major, value_minor) or |
| 1467 | // (vbc, vbd) pair. |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1468 | return "main: internal error: unexpected token"; |
| 1469 | } while (0); |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1470 | |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1471 | // Book-keeping after completing a value (whether a container value or a |
| 1472 | // simple value). Empty parent containers are no longer empty. If the parent |
| 1473 | // container is a "{...}" object, toggle between keys and values. |
| 1474 | after_value: |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1475 | if (g_depth == 0) { |
| 1476 | return g_eod; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1477 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1478 | switch (g_ctx) { |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1479 | case context::in_list_after_bracket: |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1480 | g_ctx = context::in_list_after_value; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1481 | break; |
| 1482 | case context::in_dict_after_brace: |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1483 | g_ctx = context::in_dict_after_key; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1484 | break; |
| 1485 | case context::in_dict_after_key: |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1486 | g_ctx = context::in_dict_after_value; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1487 | break; |
| 1488 | case context::in_dict_after_value: |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1489 | g_ctx = context::in_dict_after_key; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1490 | break; |
Nigel Tao | 18ef5b4 | 2020-03-16 10:37:47 +1100 | [diff] [blame] | 1491 | default: |
| 1492 | break; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1493 | } |
| 1494 | return nullptr; |
| 1495 | } |
| 1496 | |
| 1497 | const char* // |
| 1498 | main1(int argc, char** argv) { |
| 1499 | TRY(initialize_globals(argc, argv)); |
| 1500 | |
Nigel Tao | cd183f9 | 2020-07-14 12:11:05 +1000 | [diff] [blame] | 1501 | bool start_of_token_chain = true; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1502 | while (true) { |
Nigel Tao | 4e19359 | 2020-07-15 12:48:57 +1000 | [diff] [blame] | 1503 | wuffs_base__status status = g_dec->decode_tokens( |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1504 | &g_tok, &g_src, |
| 1505 | 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] | 1506 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1507 | while (g_tok.meta.ri < g_tok.meta.wi) { |
| 1508 | wuffs_base__token t = g_tok.data.ptr[g_tok.meta.ri++]; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1509 | uint64_t n = t.length(); |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1510 | if ((g_src.meta.ri - g_curr_token_end_src_index) < n) { |
| 1511 | return "main: internal error: inconsistent g_src indexes"; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1512 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1513 | g_curr_token_end_src_index += n; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1514 | |
Nigel Tao | d0b16cb | 2020-03-14 10:15:54 +1100 | [diff] [blame] | 1515 | // Skip filler tokens (e.g. whitespace). |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1516 | if (t.value() == 0) { |
Nigel Tao | 496e88b | 2020-04-09 22:10:08 +1000 | [diff] [blame] | 1517 | start_of_token_chain = !t.continued(); |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1518 | continue; |
| 1519 | } |
| 1520 | |
Nigel Tao | 2ef3999 | 2020-04-09 17:24:39 +1000 | [diff] [blame] | 1521 | const char* z = handle_token(t, start_of_token_chain); |
Nigel Tao | 496e88b | 2020-04-09 22:10:08 +1000 | [diff] [blame] | 1522 | start_of_token_chain = !t.continued(); |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1523 | if (z == nullptr) { |
| 1524 | continue; |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1525 | } else if (z == g_eod) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1526 | goto end_of_data; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1527 | } |
| 1528 | return z; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1529 | } |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1530 | |
| 1531 | if (status.repr == nullptr) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1532 | return "main: internal error: unexpected end of token stream"; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1533 | } else if (status.repr == wuffs_base__suspension__short_read) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1534 | if (g_curr_token_end_src_index != g_src.meta.ri) { |
| 1535 | return "main: internal error: inconsistent g_src indexes"; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1536 | } |
| 1537 | TRY(read_src()); |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1538 | g_curr_token_end_src_index = g_src.meta.ri; |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1539 | } else if (status.repr == wuffs_base__suspension__short_write) { |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1540 | g_tok.compact(); |
Nigel Tao | 2cf76db | 2020-02-27 22:42:01 +1100 | [diff] [blame] | 1541 | } else { |
| 1542 | return status.message(); |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1543 | } |
| 1544 | } |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1545 | end_of_data: |
| 1546 | |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1547 | // 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] | 1548 | // confirm that we've processed all the tokens. |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1549 | if (g_flags.query_c_string && *g_flags.query_c_string) { |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1550 | return nullptr; |
| 1551 | } |
Nigel Tao | 6b161af | 2020-02-24 11:01:48 +1100 | [diff] [blame] | 1552 | |
Nigel Tao | 6b161af | 2020-02-24 11:01:48 +1100 | [diff] [blame] | 1553 | // Check that we've exhausted the input. |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1554 | 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] | 1555 | TRY(read_src()); |
| 1556 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1557 | if ((g_src.meta.ri < g_src.meta.wi) || !g_src.meta.closed) { |
Nigel Tao | 6b161af | 2020-02-24 11:01:48 +1100 | [diff] [blame] | 1558 | return "main: valid JSON followed by further (unexpected) data"; |
| 1559 | } |
| 1560 | |
| 1561 | // 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] | 1562 | // filler tokens. For example, "true\n" is valid JSON (and fully consumed |
| 1563 | // with WUFFS_JSON__QUIRK_ALLOW_TRAILING_NEW_LINE enabled) with a trailing |
| 1564 | // filler token for the "\n". |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1565 | for (; g_tok.meta.ri < g_tok.meta.wi; g_tok.meta.ri++) { |
| 1566 | if (g_tok.data.ptr[g_tok.meta.ri].value_base_category() != |
Nigel Tao | 6b161af | 2020-02-24 11:01:48 +1100 | [diff] [blame] | 1567 | WUFFS_BASE__TOKEN__VBC__FILLER) { |
| 1568 | return "main: internal error: decoded OK but unprocessed tokens remain"; |
| 1569 | } |
| 1570 | } |
| 1571 | |
| 1572 | return nullptr; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1573 | } |
| 1574 | |
Nigel Tao | 2914bae | 2020-02-26 09:40:30 +1100 | [diff] [blame] | 1575 | int // |
| 1576 | compute_exit_code(const char* status_msg) { |
Nigel Tao | 9cc2c25 | 2020-02-23 17:05:49 +1100 | [diff] [blame] | 1577 | if (!status_msg) { |
| 1578 | return 0; |
| 1579 | } |
Nigel Tao | 01abc84 | 2020-03-06 21:42:33 +1100 | [diff] [blame] | 1580 | size_t n; |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1581 | if (status_msg == g_usage) { |
Nigel Tao | 01abc84 | 2020-03-06 21:42:33 +1100 | [diff] [blame] | 1582 | n = strlen(status_msg); |
| 1583 | } else { |
Nigel Tao | 9cc2c25 | 2020-02-23 17:05:49 +1100 | [diff] [blame] | 1584 | n = strnlen(status_msg, 2047); |
Nigel Tao | 01abc84 | 2020-03-06 21:42:33 +1100 | [diff] [blame] | 1585 | if (n >= 2047) { |
| 1586 | status_msg = "main: internal error: error message is too long"; |
| 1587 | n = strnlen(status_msg, 2047); |
| 1588 | } |
Nigel Tao | 9cc2c25 | 2020-02-23 17:05:49 +1100 | [diff] [blame] | 1589 | } |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 1590 | const int stderr_fd = 2; |
| 1591 | ignore_return_value(write(stderr_fd, status_msg, n)); |
| 1592 | ignore_return_value(write(stderr_fd, "\n", 1)); |
Nigel Tao | 9cc2c25 | 2020-02-23 17:05:49 +1100 | [diff] [blame] | 1593 | // Return an exit code of 1 for regular (forseen) errors, e.g. badly |
| 1594 | // formatted or unsupported input. |
| 1595 | // |
| 1596 | // Return an exit code of 2 for internal (exceptional) errors, e.g. defensive |
| 1597 | // run-time checks found that an internal invariant did not hold. |
| 1598 | // |
| 1599 | // Automated testing, including badly formatted inputs, can therefore |
| 1600 | // discriminate between expected failure (exit code 1) and unexpected failure |
| 1601 | // (other non-zero exit codes). Specifically, exit code 2 for internal |
| 1602 | // invariant violation, exit code 139 (which is 128 + SIGSEGV on x86_64 |
| 1603 | // linux) for a segmentation fault (e.g. null pointer dereference). |
| 1604 | return strstr(status_msg, "internal error:") ? 2 : 1; |
| 1605 | } |
| 1606 | |
Nigel Tao | 2914bae | 2020-02-26 09:40:30 +1100 | [diff] [blame] | 1607 | int // |
| 1608 | main(int argc, char** argv) { |
Nigel Tao | 01abc84 | 2020-03-06 21:42:33 +1100 | [diff] [blame] | 1609 | // Look for an input filename (the first non-flag argument) in argv. If there |
| 1610 | // is one, open it (but do not read from it) before we self-impose a sandbox. |
| 1611 | // |
| 1612 | // Flags start with "-", unless it comes after a bare "--" arg. |
| 1613 | { |
| 1614 | bool dash_dash = false; |
| 1615 | int a; |
| 1616 | for (a = 1; a < argc; a++) { |
| 1617 | char* arg = argv[a]; |
| 1618 | if ((arg[0] == '-') && !dash_dash) { |
| 1619 | dash_dash = (arg[1] == '-') && (arg[2] == '\x00'); |
| 1620 | continue; |
| 1621 | } |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1622 | g_input_file_descriptor = open(arg, O_RDONLY); |
| 1623 | if (g_input_file_descriptor < 0) { |
Nigel Tao | 01abc84 | 2020-03-06 21:42:33 +1100 | [diff] [blame] | 1624 | fprintf(stderr, "%s: %s\n", arg, strerror(errno)); |
| 1625 | return 1; |
| 1626 | } |
| 1627 | break; |
| 1628 | } |
| 1629 | } |
| 1630 | |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 1631 | #if defined(WUFFS_EXAMPLE_USE_SECCOMP) |
| 1632 | prctl(PR_SET_SECCOMP, SECCOMP_MODE_STRICT); |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1633 | g_sandboxed = true; |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 1634 | #endif |
| 1635 | |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1636 | const char* z = main1(argc, argv); |
Nigel Tao | d60815c | 2020-03-26 14:32:35 +1100 | [diff] [blame] | 1637 | if (g_wrote_to_dst) { |
Nigel Tao | 168f60a | 2020-07-14 13:19:33 +1000 | [diff] [blame] | 1638 | const char* z1 = (g_flags.output_format == file_format::json) |
| 1639 | ? write_dst("\n", 1) |
| 1640 | : nullptr; |
Nigel Tao | 0cd2f98 | 2020-03-03 23:03:02 +1100 | [diff] [blame] | 1641 | const char* z2 = flush_dst(); |
| 1642 | z = z ? z : (z1 ? z1 : z2); |
| 1643 | } |
| 1644 | int exit_code = compute_exit_code(z); |
Nigel Tao | fe0cbbd | 2020-03-05 22:01:30 +1100 | [diff] [blame] | 1645 | |
| 1646 | #if defined(WUFFS_EXAMPLE_USE_SECCOMP) |
| 1647 | // Call SYS_exit explicitly, instead of calling SYS_exit_group implicitly by |
| 1648 | // either calling _exit or returning from main. SECCOMP_MODE_STRICT allows |
| 1649 | // only SYS_exit. |
| 1650 | syscall(SYS_exit, exit_code); |
| 1651 | #endif |
Nigel Tao | 9cc2c25 | 2020-02-23 17:05:49 +1100 | [diff] [blame] | 1652 | return exit_code; |
Nigel Tao | 1b07349 | 2020-02-16 22:11:36 +1100 | [diff] [blame] | 1653 | } |