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