Nigel Tao | 193527b | 2020-02-26 21:55:01 +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 | // print-json-token-debug-format.c parses JSON from stdin and prints the |
| 18 | // resulting token stream, eliding any non-essential (e.g. whitespace) tokens. |
| 19 | // |
Nigel Tao | d1c928a | 2020-02-28 12:43:53 +1100 | [diff] [blame] | 20 | // The output format is only for debugging or regression testing, and certainly |
| 21 | // not for long term storage. It isn't guaranteed to be stable between versions |
| 22 | // of this program and of the Wuffs standard library. |
Nigel Tao | 193527b | 2020-02-26 21:55:01 +1100 | [diff] [blame] | 23 | // |
Nigel Tao | d1c928a | 2020-02-28 12:43:53 +1100 | [diff] [blame] | 24 | // It prints 16 bytes (128 bits) per token, containing big-endian numbers: |
| 25 | // |
| 26 | // +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ |
| 27 | // | POS | LEN | LP| LN| MAJOR | MINOR | |
| 28 | // | | | | | |VBC| VBD | |
| 29 | // +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ |
| 30 | // |
| 31 | // - POS (4 bytes) is the position: the sum of all previous tokens' lengths, |
| 32 | // including elided tokens. |
| 33 | // - LEN (2 bytes) is the length. |
| 34 | // - LP (1 bytes) is the link_prev bit. |
| 35 | // - LN (1 bytes) is the link_next bit |
| 36 | // - MAJOR (4 bytes) is the value_major. |
| 37 | // |
| 38 | // The final 4 bytes are either value_minor (when the value_major is non-zero) |
| 39 | // or 1 + 3 bytes for value_base_category and value_base_detail (otherwise). |
| 40 | // |
Nigel Tao | 193527b | 2020-02-26 21:55:01 +1100 | [diff] [blame] | 41 | // Together with the hexadecimal WUFFS_BASE__TOKEN__ETC constants defined in |
| 42 | // token-public.h, this format is somewhat human-readable when piped through a |
Nigel Tao | d1c928a | 2020-02-28 12:43:53 +1100 | [diff] [blame] | 43 | // hex-dump program (such as /usr/bin/hd), printing one token per line. |
Nigel Tao | 193527b | 2020-02-26 21:55:01 +1100 | [diff] [blame] | 44 | // |
| 45 | // If the input or output is larger than the program's buffers (64 MiB and |
| 46 | // 131072 tokens by default), there may be multiple valid tokenizations of any |
| 47 | // given input. For example, if a source string "abcde" straddles an I/O |
Nigel Tao | d1c928a | 2020-02-28 12:43:53 +1100 | [diff] [blame] | 48 | // boundary, it may be tokenized as single (no-link) 5-length string or as a |
| 49 | // 3-length link_next'ed string followed by a 2-length link_prev'ed string. |
Nigel Tao | 193527b | 2020-02-26 21:55:01 +1100 | [diff] [blame] | 50 | // |
Nigel Tao | d1c928a | 2020-02-28 12:43:53 +1100 | [diff] [blame] | 51 | // A Wuffs token stream, in general, can support inputs more than 0xFFFF_FFFF |
Nigel Tao | 193527b | 2020-02-26 21:55:01 +1100 | [diff] [blame] | 52 | // bytes long, but this program can not, as it tracks the tokens' cumulative |
| 53 | // position as a uint32. |
| 54 | |
| 55 | #include <inttypes.h> |
| 56 | #include <stdio.h> |
| 57 | #include <string.h> |
| 58 | #include <unistd.h> |
| 59 | |
| 60 | // Wuffs ships as a "single file C library" or "header file library" as per |
| 61 | // https://github.com/nothings/stb/blob/master/docs/stb_howto.txt |
| 62 | // |
| 63 | // To use that single file as a "foo.c"-like implementation, instead of a |
| 64 | // "foo.h"-like header, #define WUFFS_IMPLEMENTATION before #include'ing or |
| 65 | // compiling it. |
| 66 | #define WUFFS_IMPLEMENTATION |
| 67 | |
| 68 | // Defining the WUFFS_CONFIG__MODULE* macros are optional, but it lets users of |
| 69 | // release/c/etc.c whitelist which parts of Wuffs to build. That file contains |
| 70 | // the entire Wuffs standard library, implementing a variety of codecs and file |
| 71 | // formats. Without this macro definition, an optimizing compiler or linker may |
| 72 | // very well discard Wuffs code for unused codecs, but listing the Wuffs |
| 73 | // modules we use makes that process explicit. Preprocessing means that such |
| 74 | // code simply isn't compiled. |
| 75 | #define WUFFS_CONFIG__MODULES |
| 76 | #define WUFFS_CONFIG__MODULE__BASE |
| 77 | #define WUFFS_CONFIG__MODULE__JSON |
| 78 | |
| 79 | // If building this program in an environment that doesn't easily accommodate |
| 80 | // relative includes, you can use the script/inline-c-relative-includes.go |
| 81 | // program to generate a stand-alone C++ file. |
| 82 | #include "../release/c/wuffs-unsupported-snapshot.c" |
| 83 | |
| 84 | #ifndef SRC_BUFFER_SIZE |
| 85 | #define SRC_BUFFER_SIZE (64 * 1024 * 1024) |
| 86 | #endif |
| 87 | #ifndef TOKEN_BUFFER_SIZE |
| 88 | #define TOKEN_BUFFER_SIZE (128 * 1024) |
| 89 | #endif |
| 90 | |
| 91 | uint8_t src_buffer[SRC_BUFFER_SIZE]; |
| 92 | wuffs_base__token tok_buffer[TOKEN_BUFFER_SIZE]; |
| 93 | |
| 94 | wuffs_base__io_buffer src; |
| 95 | wuffs_base__token_buffer tok; |
| 96 | |
| 97 | wuffs_json__decoder dec; |
| 98 | wuffs_base__status dec_status; |
| 99 | |
Nigel Tao | 193527b | 2020-02-26 21:55:01 +1100 | [diff] [blame] | 100 | #define TRY(error_msg) \ |
| 101 | do { \ |
| 102 | const char* z = error_msg; \ |
| 103 | if (z) { \ |
| 104 | return z; \ |
| 105 | } \ |
| 106 | } while (false) |
| 107 | |
| 108 | const char* // |
| 109 | read_src() { |
| 110 | if (src.meta.closed) { |
| 111 | return "main: internal error: read requested on a closed source"; |
| 112 | } |
| 113 | wuffs_base__io_buffer__compact(&src); |
| 114 | if (src.meta.wi >= src.data.len) { |
| 115 | return "main: src buffer is full"; |
| 116 | } |
| 117 | size_t n = fread(src.data.ptr + src.meta.wi, sizeof(uint8_t), |
| 118 | src.data.len - src.meta.wi, stdin); |
| 119 | src.meta.wi += n; |
| 120 | src.meta.closed = feof(stdin); |
| 121 | if ((n == 0) && !src.meta.closed) { |
| 122 | return "main: read error"; |
| 123 | } |
| 124 | return NULL; |
| 125 | } |
| 126 | |
| 127 | // ---- |
| 128 | |
| 129 | const char* // |
| 130 | main1(int argc, char** argv) { |
| 131 | src = wuffs_base__make_io_buffer( |
| 132 | wuffs_base__make_slice_u8(src_buffer, SRC_BUFFER_SIZE), |
| 133 | wuffs_base__empty_io_buffer_meta()); |
| 134 | |
| 135 | tok = wuffs_base__make_token_buffer( |
| 136 | wuffs_base__make_slice_token(tok_buffer, TOKEN_BUFFER_SIZE), |
| 137 | wuffs_base__empty_token_buffer_meta()); |
| 138 | |
| 139 | wuffs_base__status init_status = wuffs_json__decoder__initialize( |
| 140 | &dec, sizeof__wuffs_json__decoder(), WUFFS_VERSION, 0); |
| 141 | if (!wuffs_base__status__is_ok(&init_status)) { |
| 142 | return wuffs_base__status__message(&init_status); |
| 143 | } |
| 144 | |
| 145 | uint64_t pos = 0; |
| 146 | while (true) { |
| 147 | wuffs_base__status status = |
| 148 | wuffs_json__decoder__decode_tokens(&dec, &tok, &src); |
| 149 | |
| 150 | while (tok.meta.ri < tok.meta.wi) { |
| 151 | wuffs_base__token* t = &tok.data.ptr[tok.meta.ri++]; |
Nigel Tao | d1c928a | 2020-02-28 12:43:53 +1100 | [diff] [blame] | 152 | uint16_t len = wuffs_base__token__length(t); |
Nigel Tao | 193527b | 2020-02-26 21:55:01 +1100 | [diff] [blame] | 153 | |
| 154 | if (wuffs_base__token__value(t) != 0) { |
Nigel Tao | d1c928a | 2020-02-28 12:43:53 +1100 | [diff] [blame] | 155 | uint8_t lp = wuffs_base__token__link_prev(t) ? 1 : 0; |
| 156 | uint8_t ln = wuffs_base__token__link_next(t) ? 1 : 0; |
| 157 | uint32_t vmajor = wuffs_base__token__value_major(t); |
Nigel Tao | 193527b | 2020-02-26 21:55:01 +1100 | [diff] [blame] | 158 | |
| 159 | uint8_t buf[16]; |
Nigel Tao | d1c928a | 2020-02-28 12:43:53 +1100 | [diff] [blame] | 160 | wuffs_base__store_u32be__no_bounds_check(&buf[0x0], (uint32_t)(pos)); |
| 161 | wuffs_base__store_u16be__no_bounds_check(&buf[0x4], len); |
| 162 | wuffs_base__store_u8__no_bounds_check(&buf[0x0006], lp); |
| 163 | wuffs_base__store_u8__no_bounds_check(&buf[0x0007], ln); |
| 164 | wuffs_base__store_u32be__no_bounds_check(&buf[0x8], vmajor); |
| 165 | if (vmajor) { |
| 166 | uint32_t vminor = wuffs_base__token__value_minor(t); |
| 167 | wuffs_base__store_u32be__no_bounds_check(&buf[0xC], vminor); |
| 168 | } else { |
| 169 | uint8_t vbc = wuffs_base__token__value_base_category(t); |
| 170 | uint32_t vbd = wuffs_base__token__value_base_detail(t); |
| 171 | wuffs_base__store_u8__no_bounds_check(&buf[0x000C], vbc); |
| 172 | wuffs_base__store_u24be__no_bounds_check(&buf[0xD], vbd); |
| 173 | } |
Nigel Tao | 193527b | 2020-02-26 21:55:01 +1100 | [diff] [blame] | 174 | const int stdout_fd = 1; |
| 175 | write(stdout_fd, &buf[0], 16); |
| 176 | } |
| 177 | |
| 178 | pos += len; |
| 179 | if (pos > 0xFFFFFFFF) { |
| 180 | return "main: input is too long"; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | if (status.repr == NULL) { |
| 185 | return NULL; |
| 186 | } else if (status.repr == wuffs_base__suspension__short_read) { |
| 187 | TRY(read_src()); |
| 188 | } else if (status.repr == wuffs_base__suspension__short_write) { |
| 189 | wuffs_base__token_buffer__compact(&tok); |
| 190 | } else { |
| 191 | return wuffs_base__status__message(&status); |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | // ---- |
| 197 | |
| 198 | int // |
| 199 | compute_exit_code(const char* status_msg) { |
| 200 | if (!status_msg) { |
| 201 | return 0; |
| 202 | } |
| 203 | size_t n = strnlen(status_msg, 2047); |
| 204 | if (n >= 2047) { |
| 205 | status_msg = "main: internal error: error message is too long"; |
| 206 | n = strnlen(status_msg, 2047); |
| 207 | } |
| 208 | fprintf(stderr, "%s\n", status_msg); |
| 209 | // Return an exit code of 1 for regular (forseen) errors, e.g. badly |
| 210 | // formatted or unsupported input. |
| 211 | // |
| 212 | // Return an exit code of 2 for internal (exceptional) errors, e.g. defensive |
| 213 | // run-time checks found that an internal invariant did not hold. |
| 214 | // |
| 215 | // Automated testing, including badly formatted inputs, can therefore |
| 216 | // discriminate between expected failure (exit code 1) and unexpected failure |
| 217 | // (other non-zero exit codes). Specifically, exit code 2 for internal |
| 218 | // invariant violation, exit code 139 (which is 128 + SIGSEGV on x86_64 |
| 219 | // linux) for a segmentation fault (e.g. null pointer dereference). |
| 220 | return strstr(status_msg, "internal error:") ? 2 : 1; |
| 221 | } |
| 222 | |
| 223 | int // |
| 224 | main(int argc, char** argv) { |
| 225 | const char* z = main1(argc, argv); |
| 226 | int exit_code = compute_exit_code(z); |
| 227 | return exit_code; |
| 228 | } |