blob: 07a280e2c2c70827f6d5436c1989ae180ab115bf [file] [log] [blame]
Nigel Tao1b073492020-02-16 22:11:36 +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/*
18jsonptr is a JSON formatter (pretty-printer).
19
20This example program differs from most other example Wuffs programs in that it
21is written in C++, not C.
22
23$CXX jsonptr.cc && ./a.out < ../../test/data/github-tags.json; rm -f a.out
24
25for a C++ compiler $CXX, such as clang++ or g++.
Nigel Tao569a2942020-02-23 23:13:51 +110026
27After modifying this program, run "build-example.sh example/jsonptr/" and then
28"script/run-json-test-suite.sh" to catch correctness regressions.
Nigel Tao1b073492020-02-16 22:11:36 +110029*/
30
31#include <inttypes.h>
32#include <stdio.h>
Nigel Tao9cc2c252020-02-23 17:05:49 +110033#include <string.h>
Nigel Tao1b073492020-02-16 22:11:36 +110034
35// Wuffs ships as a "single file C library" or "header file library" as per
36// https://github.com/nothings/stb/blob/master/docs/stb_howto.txt
37//
38// To use that single file as a "foo.c"-like implementation, instead of a
39// "foo.h"-like header, #define WUFFS_IMPLEMENTATION before #include'ing or
40// compiling it.
41#define WUFFS_IMPLEMENTATION
42
43// Defining the WUFFS_CONFIG__MODULE* macros are optional, but it lets users of
44// release/c/etc.c whitelist which parts of Wuffs to build. That file contains
45// the entire Wuffs standard library, implementing a variety of codecs and file
46// formats. Without this macro definition, an optimizing compiler or linker may
47// very well discard Wuffs code for unused codecs, but listing the Wuffs
48// modules we use makes that process explicit. Preprocessing means that such
49// code simply isn't compiled.
50#define WUFFS_CONFIG__MODULES
51#define WUFFS_CONFIG__MODULE__BASE
52#define WUFFS_CONFIG__MODULE__JSON
53
54// If building this program in an environment that doesn't easily accommodate
55// relative includes, you can use the script/inline-c-relative-includes.go
56// program to generate a stand-alone C++ file.
57#include "../../release/c/wuffs-unsupported-snapshot.c"
58
59#ifndef DST_BUFFER_SIZE
60#define DST_BUFFER_SIZE (32 * 1024)
61#endif
62#ifndef SRC_BUFFER_SIZE
63#define SRC_BUFFER_SIZE (32 * 1024)
64#endif
65#ifndef TOKEN_BUFFER_SIZE
66#define TOKEN_BUFFER_SIZE (4 * 1024)
67#endif
68
69uint8_t dst_buffer[DST_BUFFER_SIZE];
70uint8_t src_buffer[SRC_BUFFER_SIZE];
71wuffs_base__token tok_buffer[TOKEN_BUFFER_SIZE];
72
73wuffs_base__io_buffer dst;
74wuffs_base__io_buffer src;
75wuffs_base__token_buffer tok;
76
77wuffs_json__decoder dec;
78wuffs_base__status dec_status;
79
80// dec_current_token_end_src_index is the src.data.ptr index of the end of the
81// current token. An invariant is that (dec_current_token_end_src_index <=
82// src.meta.ri).
83size_t dec_current_token_end_src_index;
84
85#define MAX_INDENT 8
86#define INDENT_STRING " "
87size_t indent;
88
89#define TRY(error_msg) \
90 do { \
91 const char* z = error_msg; \
92 if (z) { \
93 return z; \
94 } \
95 } while (false)
96
97// ----
98
99const char* read_src() {
Nigel Taoa8406922020-02-19 12:22:00 +1100100 if (src.meta.closed) {
Nigel Tao9cc2c252020-02-23 17:05:49 +1100101 return "main: internal error: read requested on a closed source";
Nigel Taoa8406922020-02-19 12:22:00 +1100102 }
Nigel Tao1b073492020-02-16 22:11:36 +1100103 src.compact();
104 if (src.meta.wi >= src.data.len) {
105 return "main: src buffer is full";
106 }
107 size_t n = fread(src.data.ptr + src.meta.wi, sizeof(uint8_t),
108 src.data.len - src.meta.wi, stdin);
109 src.meta.wi += n;
Nigel Tao67306562020-02-19 14:04:49 +1100110 src.meta.closed = feof(stdin);
111 if ((n == 0) && !src.meta.closed) {
Nigel Taoa8406922020-02-19 12:22:00 +1100112 return "main: read error";
Nigel Tao1b073492020-02-16 22:11:36 +1100113 }
114 return nullptr;
115}
116
117const char* flush_dst() {
118 size_t n = dst.meta.wi - dst.meta.ri;
119 if (n > 0) {
120 size_t i = fwrite(dst.data.ptr + dst.meta.ri, sizeof(uint8_t), n, stdout);
121 dst.meta.ri += i;
122 if (i != n) {
123 return "main: write error";
124 }
125 dst.compact();
126 }
127 return nullptr;
128}
129
130const char* write_dst(const void* s, size_t n) {
131 const uint8_t* p = static_cast<const uint8_t*>(s);
132 while (n > 0) {
133 size_t i = dst.writer_available();
134 if (i == 0) {
135 const char* z = flush_dst();
136 if (z) {
137 return z;
138 }
139 i = dst.writer_available();
140 if (i == 0) {
141 return "main: dst buffer is full";
142 }
143 }
144
145 if (i > n) {
146 i = n;
147 }
148 memcpy(dst.data.ptr + dst.meta.wi, p, i);
149 dst.meta.wi += i;
150 p += i;
151 n -= i;
152 }
153 return nullptr;
154}
155
156// ----
157
158enum class context {
159 none,
160 in_list_after_bracket,
161 in_list_after_value,
162 in_dict_after_brace,
163 in_dict_after_key,
164 in_dict_after_value,
165};
166
167// parsed_token is a result type, combining a wuffs_base_token and an error.
168// For the parsed_token returned by make_parsed_token, it also contains the src
169// data bytes for the token. This slice is just a view into the src_buffer
170// array, and its contents may change on the next call to parse_next_token.
171//
172// An invariant is that (token.length() == data.len).
173typedef struct {
174 const char* error_msg;
175 wuffs_base__token token;
176 wuffs_base__slice_u8 data;
177} parsed_token;
178
179parsed_token make_pt_error(const char* error_msg) {
180 parsed_token p;
181 p.error_msg = error_msg;
182 p.token = wuffs_base__make_token(0);
183 p.data = wuffs_base__make_slice_u8(nullptr, 0);
184 return p;
185}
186
187parsed_token make_pt_token(uint64_t token_repr,
188 uint8_t* data_ptr,
189 size_t data_len) {
190 parsed_token p;
191 p.error_msg = nullptr;
192 p.token = wuffs_base__make_token(token_repr);
193 p.data = wuffs_base__make_slice_u8(data_ptr, data_len);
194 return p;
195}
196
197parsed_token parse_next_token() {
198 while (true) {
199 // Return a previously produced token, if one exists.
200 //
201 // We do this before checking dec_status. This is analogous to Go's
202 // io.Reader's documented idiom, when processing io.Reader.Read's returned
203 // (n int, err error), to "process the n > 0 bytes returned before
204 // considering the error err. Doing so correctly handles I/O errors that
205 // happen after reading some bytes".
206 if (tok.meta.ri < tok.meta.wi) {
207 wuffs_base__token t = tok.data.ptr[tok.meta.ri++];
208
209 uint64_t n = t.length();
210 if ((src.meta.ri - dec_current_token_end_src_index) < n) {
211 return make_pt_error("main: internal error: inconsistent src indexes");
212 }
213 dec_current_token_end_src_index += n;
214
215 // Filter out any filler tokens (e.g. whitespace).
Nigel Tao6b161af2020-02-24 11:01:48 +1100216 if (t.value_base_category() == WUFFS_BASE__TOKEN__VBC__FILLER) {
Nigel Tao1b073492020-02-16 22:11:36 +1100217 continue;
218 }
219
220 return make_pt_token(
221 t.repr, src.data.ptr + dec_current_token_end_src_index - n, n);
222 }
223
224 // Now consider dec_status.
225 if (dec_status.repr == nullptr) {
226 return make_pt_error("main: internal error: parser stopped");
227
228 } else if (dec_status.repr == wuffs_base__suspension__short_read) {
229 if (dec_current_token_end_src_index != src.meta.ri) {
230 return make_pt_error("main: internal error: inconsistent src indexes");
231 }
232 const char* z = read_src();
233 if (z) {
234 return make_pt_error(z);
235 }
236 dec_current_token_end_src_index = src.meta.ri;
237
238 } else if (dec_status.repr == wuffs_base__suspension__short_write) {
239 tok.compact();
240
241 } else {
242 return make_pt_error(dec_status.message());
243 }
244
245 // Retry a "short read" or "short write" suspension.
246 dec_status = dec.decode_tokens(&tok, &src);
247 }
248}
249
250// ----
251
Nigel Taob5461bd2020-02-21 14:13:37 +1100252uint8_t hex_digit(uint8_t nibble) {
253 nibble &= 0x0F;
254 if (nibble <= 9) {
255 return '0' + nibble;
256 }
257 return ('A' - 10) + nibble;
258}
259
Nigel Tao1b073492020-02-16 22:11:36 +1100260const char* handle_string(parsed_token pt) {
Nigel Tao0711f232020-02-17 13:17:06 +1100261 TRY(write_dst("\"", 1));
Nigel Tao1b073492020-02-16 22:11:36 +1100262 while (true) {
Nigel Taob5461bd2020-02-21 14:13:37 +1100263 uint64_t vbc = pt.token.value_base_category();
264 uint64_t vbd = pt.token.value_base_detail();
265
Nigel Tao9f7a2502020-02-23 09:42:02 +1100266 if (vbc == WUFFS_BASE__TOKEN__VBC__STRING) {
Nigel Taob5461bd2020-02-21 14:13:37 +1100267 TRY(write_dst(pt.data.ptr, pt.data.len));
Nigel Tao9f7a2502020-02-23 09:42:02 +1100268 if ((vbd & WUFFS_BASE__TOKEN__VBD__STRING__INCOMPLETE) == 0) {
Nigel Taob5461bd2020-02-21 14:13:37 +1100269 break;
270 }
271
Nigel Tao9f7a2502020-02-23 09:42:02 +1100272 } else if (vbc != WUFFS_BASE__TOKEN__VBC__UNICODE_CODE_POINT) {
273 return "main: unexpected token";
274
Nigel Taob5461bd2020-02-21 14:13:37 +1100275 } else if (vbd < 0x0020) {
276 switch (vbd) {
277 case '\b':
278 TRY(write_dst("\\b", 2));
279 break;
280 case '\f':
281 TRY(write_dst("\\f", 2));
282 break;
283 case '\n':
284 TRY(write_dst("\\n", 2));
285 break;
286 case '\r':
287 TRY(write_dst("\\r", 2));
288 break;
289 case '\t':
290 TRY(write_dst("\\t", 2));
291 break;
292 default: {
293 // Other bytes less than 0x0020 are valid UTF-8 but not valid in a
294 // JSON string. They need to remain escaped.
295 uint8_t esc6[6];
296 esc6[0] = '\\';
297 esc6[1] = 'u';
298 esc6[2] = '0';
299 esc6[3] = '0';
300 esc6[4] = hex_digit(vbd >> 4);
301 esc6[5] = hex_digit(vbd >> 0);
302 TRY(write_dst(&esc6[0], 6));
303 break;
304 }
305 }
306
307 } else if (vbd <= 0x007F) {
308 switch (vbd) {
309 case '\"':
310 TRY(write_dst("\\\"", 2));
311 break;
312 case '\\':
313 TRY(write_dst("\\\\", 2));
314 break;
315 default: {
316 // The UTF-8 encoding takes 1 byte.
317 uint8_t esc0 = (uint8_t)(vbd);
318 TRY(write_dst(&esc0, 1));
319 break;
320 }
321 }
322
323 } else if (vbd <= 0x07FF) {
324 // The UTF-8 encoding takes 2 bytes.
325 uint8_t esc2[6];
326 esc2[0] = 0xC0 | (uint8_t)((vbd >> 6));
327 esc2[1] = 0x80 | (uint8_t)((vbd >> 0) & 0x3F);
328 TRY(write_dst(&esc2[0], 2));
329
330 } else if (vbd <= 0xFFFF) {
331 // The UTF-8 encoding takes 3 bytes.
332 uint8_t esc3[6];
333 esc3[0] = 0xE0 | (uint8_t)((vbd >> 12));
334 esc3[1] = 0x80 | (uint8_t)((vbd >> 6) & 0x3F);
335 esc3[2] = 0x80 | (uint8_t)((vbd >> 0) & 0x3F);
336 TRY(write_dst(&esc3[0], 3));
337
338 } else {
339 return "main: unexpected Unicode code point";
Nigel Tao1b073492020-02-16 22:11:36 +1100340 }
Nigel Taob5461bd2020-02-21 14:13:37 +1100341
Nigel Tao1b073492020-02-16 22:11:36 +1100342 pt = parse_next_token();
343 if (pt.error_msg) {
344 return pt.error_msg;
345 }
346 }
347 TRY(write_dst("\"", 1));
348 return nullptr;
349}
350
351const char* main2() {
352 dec_status = dec.initialize(sizeof__wuffs_json__decoder(), WUFFS_VERSION, 0);
353 if (!dec_status.is_ok()) {
354 return dec_status.message();
355 }
356 dec_status = dec.decode_tokens(&tok, &src);
357 dec_current_token_end_src_index = 0;
358
359 uint64_t depth = 0;
360 context ctx = context::none;
361
362continue_loop:
363 while (true) {
364 parsed_token pt = parse_next_token();
365 if (pt.error_msg) {
366 return pt.error_msg;
367 }
368 uint64_t vbc = pt.token.value_base_category();
369 uint64_t vbd = pt.token.value_base_detail();
370
371 // Handle ']' or '}'.
Nigel Tao9f7a2502020-02-23 09:42:02 +1100372 if ((vbc == WUFFS_BASE__TOKEN__VBC__STRUCTURE) &&
373 ((vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__POP) != 0)) {
Nigel Tao1b073492020-02-16 22:11:36 +1100374 if (depth <= 0) {
375 return "main: internal error: inconsistent depth";
376 }
377 depth--;
378
379 // Write preceding whitespace.
380 if ((ctx != context::in_list_after_bracket) &&
381 (ctx != context::in_dict_after_brace)) {
382 TRY(write_dst("\n", 1));
383 for (size_t i = 0; i < depth; i++) {
384 TRY(write_dst(INDENT_STRING, indent));
385 }
386 }
387
Nigel Tao9f7a2502020-02-23 09:42:02 +1100388 TRY(write_dst(
389 (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__FROM_LIST) ? "]" : "}", 1));
390 ctx = (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_LIST)
391 ? context::in_list_after_value
392 : context::in_dict_after_key;
Nigel Tao1b073492020-02-16 22:11:36 +1100393 goto after_value;
394 }
395
396 // Write preceding whitespace and punctuation, if it wasn't ']' or '}'.
397 if (ctx == context::in_dict_after_key) {
398 TRY(write_dst(": ", 2));
399 } else if (ctx != context::none) {
400 if ((ctx != context::in_list_after_bracket) &&
401 (ctx != context::in_dict_after_brace)) {
402 TRY(write_dst(",", 1));
403 }
404 TRY(write_dst("\n", 1));
405 for (size_t i = 0; i < depth; i++) {
406 TRY(write_dst(INDENT_STRING, indent));
407 }
408 }
409
410 // Handle the token itself: either a container ('[' or '{') or a simple
411 // value (number, string or literal).
412 switch (vbc) {
Nigel Tao9f7a2502020-02-23 09:42:02 +1100413 case WUFFS_BASE__TOKEN__VBC__STRUCTURE:
414 TRY(write_dst(
415 (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_LIST) ? "[" : "{", 1));
Nigel Tao1b073492020-02-16 22:11:36 +1100416 depth++;
Nigel Tao9f7a2502020-02-23 09:42:02 +1100417 ctx = (vbd & WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_LIST)
418 ? context::in_list_after_bracket
419 : context::in_dict_after_brace;
Nigel Tao1b073492020-02-16 22:11:36 +1100420 goto continue_loop;
421
Nigel Tao9f7a2502020-02-23 09:42:02 +1100422 case WUFFS_BASE__TOKEN__VBC__NUMBER:
Nigel Tao8850d382020-02-19 12:25:00 +1100423 TRY(write_dst(pt.data.ptr, pt.data.len));
424 goto after_value;
425
Nigel Tao9f7a2502020-02-23 09:42:02 +1100426 case WUFFS_BASE__TOKEN__VBC__STRING:
Nigel Tao1b073492020-02-16 22:11:36 +1100427 TRY(handle_string(pt));
428 goto after_value;
429 }
430
431 // Return an error if we didn't match the (vbc, vbd) pair.
432 return "main: unexpected token";
433
434 // Book-keeping after completing a value (whether a container value or a
435 // simple value). Empty parent containers are no longer empty. If the
436 // parent container is a "{...}" object, toggle between keys and values.
437 after_value:
438 if (depth <= 0) {
Nigel Tao6b161af2020-02-24 11:01:48 +1100439 goto break_loop;
Nigel Tao1b073492020-02-16 22:11:36 +1100440 }
441 switch (ctx) {
442 case context::in_list_after_bracket:
443 ctx = context::in_list_after_value;
444 break;
445 case context::in_dict_after_brace:
446 ctx = context::in_dict_after_key;
447 break;
448 case context::in_dict_after_key:
449 ctx = context::in_dict_after_value;
450 break;
451 case context::in_dict_after_value:
452 ctx = context::in_dict_after_key;
453 break;
454 }
455 }
Nigel Tao6b161af2020-02-24 11:01:48 +1100456
457break_loop:
458 // Consume an optional whitespace trailer. This isn't part of the JSON spec,
459 // but it works better with line oriented Unix tools (such as "echo 123 |
460 // jsonptr" where it's "echo", not "echo -n") or hand-edited JSON files which
461 // can accidentally contain trailing whitespace.
462 //
463 // A whitespace trailer is zero or more ' ' and then zero or one '\n'.
464 while (true) {
465 if (src.meta.ri < src.meta.wi) {
466 uint8_t c = src.data.ptr[src.meta.ri];
467 if (c == ' ') {
468 src.meta.ri++;
469 continue;
470 } else if (c == '\n') {
471 src.meta.ri++;
472 break;
473 }
474 // The "exhausted the input" check below will fail.
475 break;
476 } else if (src.meta.closed) {
477 break;
478 }
479 TRY(read_src());
480 }
481
482 // Check that we've exhausted the input.
483 if ((src.meta.ri < src.meta.wi) || !src.meta.closed) {
484 return "main: valid JSON followed by further (unexpected) data";
485 }
486
487 // Check that we've used all of the decoded tokens, other than trailing
488 // filler tokens. For example, a bare `"foo"` string is valid JSON, but even
489 // without a trailing '\n', the Wuffs JSON parser emits a filler token for
490 // the final '\"'.
491 for (; tok.meta.ri < tok.meta.wi; tok.meta.ri++) {
492 if (tok.data.ptr[tok.meta.ri].value_base_category() !=
493 WUFFS_BASE__TOKEN__VBC__FILLER) {
494 return "main: internal error: decoded OK but unprocessed tokens remain";
495 }
496 }
497
498 return nullptr;
Nigel Tao1b073492020-02-16 22:11:36 +1100499}
500
501const char* main1(int argc, char** argv) {
502 dst = wuffs_base__make_io_buffer(
503 wuffs_base__make_slice_u8(dst_buffer, DST_BUFFER_SIZE),
504 wuffs_base__empty_io_buffer_meta());
505
506 src = wuffs_base__make_io_buffer(
507 wuffs_base__make_slice_u8(src_buffer, SRC_BUFFER_SIZE),
508 wuffs_base__empty_io_buffer_meta());
509
510 tok = wuffs_base__make_token_buffer(
511 wuffs_base__make_slice_token(tok_buffer, TOKEN_BUFFER_SIZE),
512 wuffs_base__empty_token_buffer_meta());
513
514 indent = 4;
515
516 TRY(main2());
517 TRY(write_dst("\n", 1));
518 return nullptr;
519}
520
Nigel Tao9cc2c252020-02-23 17:05:49 +1100521int compute_exit_code(const char* status_msg) {
522 if (!status_msg) {
523 return 0;
524 }
525 size_t n = strnlen(status_msg, 2047);
526 if (n >= 2047) {
527 status_msg = "main: internal error: error message is too long";
528 n = strnlen(status_msg, 2047);
529 }
530 fprintf(stderr, "%s\n", status_msg);
531 // Return an exit code of 1 for regular (forseen) errors, e.g. badly
532 // formatted or unsupported input.
533 //
534 // Return an exit code of 2 for internal (exceptional) errors, e.g. defensive
535 // run-time checks found that an internal invariant did not hold.
536 //
537 // Automated testing, including badly formatted inputs, can therefore
538 // discriminate between expected failure (exit code 1) and unexpected failure
539 // (other non-zero exit codes). Specifically, exit code 2 for internal
540 // invariant violation, exit code 139 (which is 128 + SIGSEGV on x86_64
541 // linux) for a segmentation fault (e.g. null pointer dereference).
542 return strstr(status_msg, "internal error:") ? 2 : 1;
543}
544
Nigel Tao1b073492020-02-16 22:11:36 +1100545int main(int argc, char** argv) {
546 const char* z0 = main1(argc, argv);
547 const char* z1 = flush_dst();
Nigel Tao9cc2c252020-02-23 17:05:49 +1100548 int exit_code = compute_exit_code(z0 ? z0 : z1);
549 return exit_code;
Nigel Tao1b073492020-02-16 22:11:36 +1100550}