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