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