blob: af38a9583665bd09e589e28a7c5cb5a0ffcd7be5 [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
Nigel Tao193527b2020-02-26 21:55:01 +110085#define TRY(error_msg) \
86 do { \
87 const char* z = error_msg; \
88 if (z) { \
89 return z; \
90 } \
91 } while (false)
92
93const char* //
94read_src() {
95 if (src.meta.closed) {
96 return "main: internal error: read requested on a closed source";
97 }
98 wuffs_base__io_buffer__compact(&src);
99 if (src.meta.wi >= src.data.len) {
100 return "main: src buffer is full";
101 }
102 size_t n = fread(src.data.ptr + src.meta.wi, sizeof(uint8_t),
103 src.data.len - src.meta.wi, stdin);
104 src.meta.wi += n;
105 src.meta.closed = feof(stdin);
106 if ((n == 0) && !src.meta.closed) {
107 return "main: read error";
108 }
109 return NULL;
110}
111
112// ----
113
114const char* //
115main1(int argc, char** argv) {
116 src = wuffs_base__make_io_buffer(
117 wuffs_base__make_slice_u8(src_buffer, SRC_BUFFER_SIZE),
118 wuffs_base__empty_io_buffer_meta());
119
120 tok = wuffs_base__make_token_buffer(
121 wuffs_base__make_slice_token(tok_buffer, TOKEN_BUFFER_SIZE),
122 wuffs_base__empty_token_buffer_meta());
123
124 wuffs_base__status init_status = wuffs_json__decoder__initialize(
125 &dec, sizeof__wuffs_json__decoder(), WUFFS_VERSION, 0);
126 if (!wuffs_base__status__is_ok(&init_status)) {
127 return wuffs_base__status__message(&init_status);
128 }
129
130 uint64_t pos = 0;
131 while (true) {
132 wuffs_base__status status =
133 wuffs_json__decoder__decode_tokens(&dec, &tok, &src);
134
135 while (tok.meta.ri < tok.meta.wi) {
136 wuffs_base__token* t = &tok.data.ptr[tok.meta.ri++];
137 uint64_t len = wuffs_base__token__length(t);
138
139 if (wuffs_base__token__value(t) != 0) {
Nigel Tao1ad5cfa2020-02-27 10:58:14 +1100140 uint64_t maj = wuffs_base__token__value_major(t);
141 uint64_t min = wuffs_base__token__value_minor(t);
Nigel Tao193527b2020-02-26 21:55:01 +1100142
143 uint8_t buf[16];
Nigel Tao1ad5cfa2020-02-27 10:58:14 +1100144 wuffs_base__store_u32be__no_bounds_check(&buf[0 * 4], (uint32_t)(pos));
145 wuffs_base__store_u32be__no_bounds_check(&buf[1 * 4], (uint32_t)(len));
146 wuffs_base__store_u32be__no_bounds_check(&buf[2 * 4], (uint32_t)(maj));
147 wuffs_base__store_u32be__no_bounds_check(&buf[3 * 4], (uint32_t)(min));
Nigel Tao193527b2020-02-26 21:55:01 +1100148 const int stdout_fd = 1;
149 write(stdout_fd, &buf[0], 16);
150 }
151
152 pos += len;
153 if (pos > 0xFFFFFFFF) {
154 return "main: input is too long";
155 }
156 }
157
158 if (status.repr == NULL) {
159 return NULL;
160 } else if (status.repr == wuffs_base__suspension__short_read) {
161 TRY(read_src());
162 } else if (status.repr == wuffs_base__suspension__short_write) {
163 wuffs_base__token_buffer__compact(&tok);
164 } else {
165 return wuffs_base__status__message(&status);
166 }
167 }
168}
169
170// ----
171
172int //
173compute_exit_code(const char* status_msg) {
174 if (!status_msg) {
175 return 0;
176 }
177 size_t n = strnlen(status_msg, 2047);
178 if (n >= 2047) {
179 status_msg = "main: internal error: error message is too long";
180 n = strnlen(status_msg, 2047);
181 }
182 fprintf(stderr, "%s\n", status_msg);
183 // Return an exit code of 1 for regular (forseen) errors, e.g. badly
184 // formatted or unsupported input.
185 //
186 // Return an exit code of 2 for internal (exceptional) errors, e.g. defensive
187 // run-time checks found that an internal invariant did not hold.
188 //
189 // Automated testing, including badly formatted inputs, can therefore
190 // discriminate between expected failure (exit code 1) and unexpected failure
191 // (other non-zero exit codes). Specifically, exit code 2 for internal
192 // invariant violation, exit code 139 (which is 128 + SIGSEGV on x86_64
193 // linux) for a segmentation fault (e.g. null pointer dereference).
194 return strstr(status_msg, "internal error:") ? 2 : 1;
195}
196
197int //
198main(int argc, char** argv) {
199 const char* z = main1(argc, argv);
200 int exit_code = compute_exit_code(z);
201 return exit_code;
202}