Refactor std/json expected-character-class check

This commit is contained in:
Nigel Tao committed 2020-02-22 09:03:07 +11:00
1 parent f6843719b5
commit d1a4abe321
3 files changed
+103 -161

No files matched your search

+25 -65
View File
@@ -5189,8 +5189,8 @@ struct wuffs_json__decoder__struct {
struct {
uint32_t v_depth;
uint8_t v_expect;
uint8_t v_expect_after_value;
uint32_t v_expect;
uint32_t v_expect_after_value;
} s_decode_tokens[1];
} private_data;
@@ -18610,8 +18610,8 @@ wuffs_json__decoder__decode_tokens(wuffs_json__decoder* self,
uint8_t v_uni4_ok = 0;
uint32_t v_uni4_string = 0;
uint32_t v_uni4_value = 0;
uint8_t v_expect = 0;
uint8_t v_expect_after_value = 0;
uint32_t v_expect = 0;
uint32_t v_expect_after_value = 0;
wuffs_base__token* iop_a_dst = NULL;
wuffs_base__token* io0_a_dst WUFFS_BASE__POTENTIALLY_UNUSED = NULL;
@@ -18647,7 +18647,7 @@ wuffs_json__decoder__decode_tokens(wuffs_json__decoder* self,
switch (coro_susp_point) {
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
v_expect = 3;
v_expect = 3762;
label_0_continue:;
while (true) {
while (true) {
@@ -18704,11 +18704,11 @@ wuffs_json__decoder__decode_tokens(wuffs_json__decoder* self,
goto label_0_continue;
}
}
if (0 == (v_expect & (((uint32_t)(1)) << v_class))) {
status = wuffs_base__make_status(wuffs_json__error__bad_input);
goto exit;
}
if (v_class == 1) {
if (0 == (v_expect & 2)) {
status = wuffs_base__make_status(wuffs_json__error__bad_input);
goto exit;
}
*iop_a_dst++ = wuffs_base__make_token(
(((uint64_t)(0)) << WUFFS_BASE__TOKEN__VALUE__SHIFT) |
(((uint64_t)(1)) << WUFFS_BASE__TOKEN__LENGTH__SHIFT));
@@ -18891,42 +18891,30 @@ wuffs_json__decoder__decode_tokens(wuffs_json__decoder* self,
goto label_4_break;
}
label_4_break:;
if (0 == (v_expect & 1)) {
if (0 == (v_expect & 16)) {
v_expect = 8;
goto label_0_continue;
}
goto label_5_break;
} else if (v_class == 2) {
if (0 == (v_expect & 4)) {
status = wuffs_base__make_status(wuffs_json__error__bad_input);
goto exit;
}
(iop_a_src += 1, wuffs_base__make_empty_struct());
*iop_a_dst++ = wuffs_base__make_token(
(((uint64_t)(0)) << WUFFS_BASE__TOKEN__VALUE__SHIFT) |
(((uint64_t)(1)) << WUFFS_BASE__TOKEN__LENGTH__SHIFT));
if (0 == (v_expect & 16)) {
if (0 == (v_expect & 256)) {
v_expect = 2;
} else {
v_expect = 3;
v_expect = 3762;
}
goto label_0_continue;
} else if (v_class == 3) {
if (0 == (v_expect & 8)) {
status = wuffs_base__make_status(wuffs_json__error__bad_input);
goto exit;
}
(iop_a_src += 1, wuffs_base__make_empty_struct());
*iop_a_dst++ = wuffs_base__make_token(
(((uint64_t)(0)) << WUFFS_BASE__TOKEN__VALUE__SHIFT) |
(((uint64_t)(1)) << WUFFS_BASE__TOKEN__LENGTH__SHIFT));
v_expect = 3;
v_expect = 3762;
goto label_0_continue;
} else if (v_class == 4) {
if (0 == (v_expect & 1)) {
status = wuffs_base__make_status(wuffs_json__error__bad_input);
goto exit;
}
while (true) {
if (a_src) {
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
@@ -18980,10 +18968,6 @@ wuffs_json__decoder__decode_tokens(wuffs_json__decoder* self,
label_6_break:;
goto label_5_break;
} else if (v_class == 5) {
if (0 == (v_expect & 1)) {
status = wuffs_base__make_status(wuffs_json__error__bad_input);
goto exit;
}
if (v_depth >= 1024) {
status = wuffs_base__make_status(
wuffs_json__error__unsupported_recursion_depth);
@@ -18998,14 +18982,10 @@ wuffs_json__decoder__decode_tokens(wuffs_json__decoder* self,
*iop_a_dst++ = wuffs_base__make_token(
(((uint64_t)(2097185)) << WUFFS_BASE__TOKEN__VALUE__SHIFT) |
(((uint64_t)(1)) << WUFFS_BASE__TOKEN__LENGTH__SHIFT));
v_expect = 34;
v_expect_after_value = 36;
v_expect = 66;
v_expect_after_value = 68;
goto label_0_continue;
} else if (v_class == 6) {
if (0 == (v_expect & 32)) {
status = wuffs_base__make_status(wuffs_json__error__bad_input);
goto exit;
}
(iop_a_src += 1, wuffs_base__make_empty_struct());
if (v_depth <= 1) {
*iop_a_dst++ = wuffs_base__make_token(
@@ -19021,21 +19001,17 @@ wuffs_json__decoder__decode_tokens(wuffs_json__decoder* self,
*iop_a_dst++ = wuffs_base__make_token(
(((uint64_t)(2101282)) << WUFFS_BASE__TOKEN__VALUE__SHIFT) |
(((uint64_t)(1)) << WUFFS_BASE__TOKEN__LENGTH__SHIFT));
v_expect = 20;
v_expect_after_value = 20;
v_expect = 260;
v_expect_after_value = 260;
} else {
*iop_a_dst++ = wuffs_base__make_token(
(((uint64_t)(2105378)) << WUFFS_BASE__TOKEN__VALUE__SHIFT) |
(((uint64_t)(1)) << WUFFS_BASE__TOKEN__LENGTH__SHIFT));
v_expect = 36;
v_expect_after_value = 36;
v_expect = 68;
v_expect_after_value = 68;
}
goto label_0_continue;
} else if (v_class == 7) {
if (0 == (v_expect & 1)) {
status = wuffs_base__make_status(wuffs_json__error__bad_input);
goto exit;
}
if (v_depth >= 1024) {
status = wuffs_base__make_status(
wuffs_json__error__unsupported_recursion_depth);
@@ -19050,14 +19026,10 @@ wuffs_json__decoder__decode_tokens(wuffs_json__decoder* self,
*iop_a_dst++ = wuffs_base__make_token(
(((uint64_t)(2097169)) << WUFFS_BASE__TOKEN__VALUE__SHIFT) |
(((uint64_t)(1)) << WUFFS_BASE__TOKEN__LENGTH__SHIFT));
v_expect = 19;
v_expect_after_value = 20;
v_expect = 4018;
v_expect_after_value = 260;
goto label_0_continue;
} else if (v_class == 8) {
if (0 == (v_expect & 16)) {
status = wuffs_base__make_status(wuffs_json__error__bad_input);
goto exit;
}
(iop_a_src += 1, wuffs_base__make_empty_struct());
if (v_depth <= 1) {
*iop_a_dst++ = wuffs_base__make_token(
@@ -19073,21 +19045,17 @@ wuffs_json__decoder__decode_tokens(wuffs_json__decoder* self,
*iop_a_dst++ = wuffs_base__make_token(
(((uint64_t)(2101266)) << WUFFS_BASE__TOKEN__VALUE__SHIFT) |
(((uint64_t)(1)) << WUFFS_BASE__TOKEN__LENGTH__SHIFT));
v_expect = 20;
v_expect_after_value = 20;
v_expect = 260;
v_expect_after_value = 260;
} else {
*iop_a_dst++ = wuffs_base__make_token(
(((uint64_t)(2105362)) << WUFFS_BASE__TOKEN__VALUE__SHIFT) |
(((uint64_t)(1)) << WUFFS_BASE__TOKEN__LENGTH__SHIFT));
v_expect = 36;
v_expect_after_value = 36;
v_expect = 68;
v_expect_after_value = 68;
}
goto label_0_continue;
} else if (v_class == 9) {
if (0 == (v_expect & 1)) {
status = wuffs_base__make_status(wuffs_json__error__bad_input);
goto exit;
}
v_match = wuffs_base__io_reader__match7(iop_a_src, io2_a_src,
111546413966853);
if (v_match == 0) {
@@ -19108,10 +19076,6 @@ wuffs_json__decoder__decode_tokens(wuffs_json__decoder* self,
goto label_0_continue;
}
} else if (v_class == 10) {
if (0 == (v_expect & 1)) {
status = wuffs_base__make_status(wuffs_json__error__bad_input);
goto exit;
}
v_match =
wuffs_base__io_reader__match7(iop_a_src, io2_a_src, 435762131972);
if (v_match == 0) {
@@ -19132,10 +19096,6 @@ wuffs_json__decoder__decode_tokens(wuffs_json__decoder* self,
goto label_0_continue;
}
} else if (v_class == 11) {
if (0 == (v_expect & 1)) {
status = wuffs_base__make_status(wuffs_json__error__bad_input);
goto exit;
}
v_match =
wuffs_base__io_reader__match7(iop_a_src, io2_a_src, 465676103172);
if (v_match == 0) {
+40 -16
View File
@@ -60,22 +60,46 @@ pri const backslashes array[256] base.u8 = [
// 8 9 A B C D E F
]
// - 0x00 is CLASS_WHITESPACE.
// - 0x01 is CLASS_STRING.
// - 0x02 is CLASS_COMMA.
// - 0x03 is CLASS_COLON.
// - 0x04 is CLASS_NUMBER.
// - 0x05 is CLASS_OPEN_CURLY_BRACE.
// - 0x06 is CLASS_CLOSE_CURLY_BRACE.
// - 0x07 is CLASS_OPEN_SQUARE_BRACKET.
// - 0x08 is CLASS_CLOSE_SQUARE_BRACKET.
// - 0x09 is CLASS_FALSE.
// - 0x0A is CLASS_TRUE.
// - 0x0B is CLASS_NULL.
// - 0x0C is reserved.
// - 0x0D is reserved.
// - 0x0E is reserved.
// - 0x0F is CLASS_BAD_INPUT.
// - 0x00 (bitmask 0x0001) is CLASS_WHITESPACE.
// - 0x01 (bitmask 0x0002) is CLASS_STRING.
// - 0x02 (bitmask 0x0004) is CLASS_COMMA.
// - 0x03 (bitmask 0x0008) is CLASS_COLON.
// - 0x04 (bitmask 0x0010) is CLASS_NUMBER.
// - 0x05 (bitmask 0x0020) is CLASS_OPEN_CURLY_BRACE.
// - 0x06 (bitmask 0x0040) is CLASS_CLOSE_CURLY_BRACE.
// - 0x07 (bitmask 0x0080) is CLASS_OPEN_SQUARE_BRACKET.
// - 0x08 (bitmask 0x0100) is CLASS_CLOSE_SQUARE_BRACKET.
// - 0x09 (bitmask 0x0200) is CLASS_FALSE.
// - 0x0A (bitmask 0x0400) is CLASS_TRUE.
// - 0x0B (bitmask 0x0800) is CLASS_NULL.
// - 0x0C (bitmask 0x1000) is reserved.
// - 0x0D (bitmask 0x2000) is reserved.
// - 0x0E (bitmask 0x4000) is reserved.
// - 0x0F (bitmask 0x8000) is CLASS_BAD_INPUT.
//
// The bitmasks are used by the "expect" variable: what the next character
// class can be.
//
// - 0x0002 is EXPECT_STRING.
// - 0x0EB0 is EXPECT_NON_STRING_VALUE.
// - 0x0004 is EXPECT_COMMA.
// - 0x0008 is EXPECT_COLON.
// - 0x0040 is EXPECT_CLOSE_CURLY_BRACE.
// - 0x0100 is EXPECT_CLOSE_SQUARE_BRACKET.
//
// Bitwise or'ing these together gives 0x0FFE. Whitespace is never expected, as
// it is handled separately.
//
// EXPECT_VALUE is also defined to be 0x0EB2, equivalent to (EXPECT_STRING |
// EXPECT_NON_STRING_VALUE).
//
// EXPECT_NUMBER is also defined to be 0x0010. Testing for EXPECT_NUMBER's
// presence is equivalent to testing for EXPECT_NON_STRING_VALUE.
//
// "Non-string value" includes literals (false, true, null), numbers, arrays
// and objects.
//
// "String value" includes "this" and "th\u0061t".
pri const classes array[256] base.u8[..= 0x0F] = [
// 0 1 2 3 4 5 6 7
// 8 9 A B C D E F
+38 -80
View File
@@ -63,29 +63,16 @@ pub func decoder.decode_tokens?(dst: base.token_writer, src: base.io_reader) {
var uni4_string : base.u32
var uni4_value : base.u32[..= 0xFFFF]
// expect is a bitmask of what the next token can be:
// - 0x01 EXPECT_NON_STRING_VALUE.
// - 0x02 EXPECT_STRING_VALUE.
// - 0x04 EXPECT_COMMA ','.
// - 0x08 EXPECT_COLON ':'.
// - 0x10 EXPECT_CLOSE_BRACKET ']'.
// - 0x20 EXPECT_CLOSE_BRACE '}'.
// expect is a bitmask of what the next character class can be.
//
// EXPECT_VALUE is also defined to be 0x03, equivalent to
// (EXPECT_NON_STRING_VALUE | EXPECT_STRING_VALUE).
//
// "Non-string value" includes numbers, literals (false, true, null),
// arrays ("[etc]") and objects ("{etc}").
//
// "String value" includes "this" and "th\u0061t".
var expect : base.u8
// expect_after_value is what to expect after seeing a value (a literal,
// number, string, array or object). For depth 0, this is ignored.
// Otherwise, it should be (EXPECT_CLOSE_FOO | EXPECT_COMMA), for some
// value of FOO.
var expect : base.u32
var expect_after_value : base.u32
// expect_after_value is what to expect after seeing a value. For depth 0,
// this is ignored. Otherwise, it should be (EXPECT_CLOSE_FOO |
// EXPECT_COMMA), for some value of FOO.
var expect_after_value : base.u8
expect = 0x03 // EXPECT_VALUE
expect = 0x0EB2 // EXPECT_VALUE
while.outer true {
while.goto_parsed_a_leaf_value true {
@@ -145,6 +132,11 @@ pub func decoder.decode_tokens?(dst: base.token_writer, src: base.io_reader) {
}
}
// Check expected character classes.
if 0 == (expect & ((1 as base.u32) << class)) {
return "#bad input"
}
// These assertions are redundant (the Wuffs compiler should
// already know these facts; deleting these assertions should still
// compile) but are listed explicitly to guard against future edits
@@ -154,10 +146,6 @@ pub func decoder.decode_tokens?(dst: base.token_writer, src: base.io_reader) {
if class == 0x01 { // 0x01 is CLASS_STRING.
// -------- BEGIN parse strings.
if 0 == (expect & 0x02) { // 0x02 is EXPECT_STRING_VALUE.
return "#bad input"
}
// The leading '"' is filler.
args.dst.write_fast_token!(
value: 0,
@@ -317,20 +305,17 @@ pub func decoder.decode_tokens?(dst: base.token_writer, src: base.io_reader) {
break
}
// As above, expect must have contained EXPECT_STRING_VALUE. If
// it didn't also contain EXPECT_NON_STRING_VALUE then we were
// parsing an object key and the next token should be ':'.
if 0 == (expect & 0x01) { // 0x01 is EXPECT_NON_STRING_VALUE.
expect = 0x08 // 0x08 is EXPECT_COLON.
// As above, expect must have contained EXPECT_STRING. If it
// didn't also contain EXPECT_NUMBER then we were parsing an
// object key and the next token should be ':'.
if 0 == (expect & 0x0010) { // 0x0010 is EXPECT_NUMBER.
expect = 0x0008 // 0x0008 is EXPECT_COLON.
continue.outer
}
break.goto_parsed_a_leaf_value
// -------- END parse strings.
} else if class == 0x02 { // 0x02 is CLASS_COMMA.
if 0 == (expect & 0x04) { // 0x01 is EXPECT_COMMA.
return "#bad input"
}
args.src.skip32_fast!(actual: 1, worst_case: 1)
// The ',' is filler.
args.dst.write_fast_token!(
@@ -338,30 +323,24 @@ pub func decoder.decode_tokens?(dst: base.token_writer, src: base.io_reader) {
length: 1)
// What's valid after a comma depends on whether or not we're
// in an array or an object.
if 0 == (expect & 0x10) { // 0x10 is EXPECT_CLOSE_BRACKET
expect = 0x02 // 0x02 is EXPECT_STRING_VALUE.
if 0 == (expect & 0x0100) { // 0x0100 is EXPECT_CLOSE_SQUARE_BRACKET
expect = 0x0002 // 0x0002 is EXPECT_STRING.
} else {
expect = 0x03 // 0x03 is EXPECT_VALUE.
expect = 0x0EB2 // 0x0EB2 is EXPECT_VALUE.
}
continue.outer
} else if class == 0x03 { // 0x03 is CLASS_COLON.
if 0 == (expect & 0x08) { // 0x08 is EXPECT_COLON.
return "#bad input"
}
args.src.skip32_fast!(actual: 1, worst_case: 1)
// The ':' is filler.
args.dst.write_fast_token!(
value: 0,
length: 1)
expect = 0x03 // 0x03 is EXPECT_VALUE.
expect = 0x0EB2 // 0x0EB2 is EXPECT_VALUE.
continue.outer
} else if class == 0x04 { // 0x04 is CLASS_NUMBER.
// -------- BEGIN parse numbers.
if 0 == (expect & 0x01) { // 0x02 is EXPECT_NON_STRING_VALUE.
return "#bad input"
}
while true,
pre args.dst.available() > 0,
{
@@ -405,9 +384,6 @@ pub func decoder.decode_tokens?(dst: base.token_writer, src: base.io_reader) {
// -------- END parse numbers.
} else if class == 0x05 { // 0x05 is CLASS_OPEN_CURLY_BRACE.
if 0 == (expect & 0x01) { // 0x01 is EXPECT_NON_STRING_VALUE.
return "#bad input"
}
if depth >= 1024 {
return "#unsupported recursion depth"
}
@@ -420,14 +396,11 @@ pub func decoder.decode_tokens?(dst: base.token_writer, src: base.io_reader) {
args.dst.write_fast_token!(
value: 0x20_0021,
length: 1)
expect = 0x22 // 0x22 is (EXPECT_CLOSE_BRACE | EXPECT_STRING_VALUE).
expect_after_value = 0x24 // 0x24 is (EXPECT_CLOSE_BRACE | EXPECT_COMMA).
expect = 0x0042 // 0x0042 is (EXPECT_CLOSE_CURLY_BRACE | EXPECT_STRING).
expect_after_value = 0x0044 // 0x0044 is (EXPECT_CURLY_CLOSE_BRACE | EXPECT_COMMA).
continue.outer
} else if class == 0x06 { // 0x06 is CLASS_CLOSE_CURLY_BRACE.
if 0 == (expect & 0x20) { // 0x20 is EXPECT_CLOSE_BRACE.
return "#bad input"
}
args.src.skip32_fast!(actual: 1, worst_case: 1)
if depth <= 1 {
args.dst.write_fast_token!(
@@ -442,23 +415,20 @@ pub func decoder.decode_tokens?(dst: base.token_writer, src: base.io_reader) {
args.dst.write_fast_token!(
value: 0x20_1022,
length: 1)
// 0x14 is (EXPECT_CLOSE_BRACKET | EXPECT_COMMA).
expect = 0x14
expect_after_value = 0x14
// 0x0104 is (EXPECT_SQUARE_CLOSE_BRACKET | EXPECT_COMMA).
expect = 0x0104
expect_after_value = 0x0104
} else {
args.dst.write_fast_token!(
value: 0x20_2022,
length: 1)
// 0x24 is (EXPECT_CLOSE_BRACE | EXPECT_COMMA).
expect = 0x24
expect_after_value = 0x24
// 0x0044 is (EXPECT_CURLY_CLOSE_BRACE | EXPECT_COMMA).
expect = 0x0044
expect_after_value = 0x0044
}
continue.outer
} else if class == 0x07 { // 0x07 is CLASS_OPEN_SQUARE_BRACKET.
if 0 == (expect & 0x01) { // 0x01 is EXPECT_NON_STRING_VALUE.
return "#bad input"
}
if depth >= 1024 {
return "#unsupported recursion depth"
}
@@ -471,14 +441,11 @@ pub func decoder.decode_tokens?(dst: base.token_writer, src: base.io_reader) {
args.dst.write_fast_token!(
value: 0x20_0011,
length: 1)
expect = 0x13 // 0x13 is (EXPECT_CLOSE_BRACKET | EXPECT_VALUE).
expect_after_value = 0x14 // 0x14 is (EXPECT_CLOSE_BRACKET | EXPECT_COMMA).
expect = 0x0FB2 // 0x0FB2 is (EXPECT_CLOSE_SQUARE_BRACKET | EXPECT_VALUE).
expect_after_value = 0x0104 // 0x0104 is (EXPECT_CLOSE_SQUARE_BRACKET | EXPECT_COMMA).
continue.outer
} else if class == 0x08 { // 0x08 is CLASS_CLOSE_SQUARE_BRACKET.
if 0 == (expect & 0x10) { // 0x10 is EXPECT_CLOSE_BRACKET
return "#bad input"
}
args.src.skip32_fast!(actual: 1, worst_case: 1)
if depth <= 1 {
args.dst.write_fast_token!(
@@ -493,23 +460,20 @@ pub func decoder.decode_tokens?(dst: base.token_writer, src: base.io_reader) {
args.dst.write_fast_token!(
value: 0x20_1012,
length: 1)
// 0x14 is (EXPECT_CLOSE_BRACKET | EXPECT_COMMA).
expect = 0x14
expect_after_value = 0x14
// 0x0104 is (EXPECT_CLOSE_SQUARE_BRACKET | EXPECT_COMMA).
expect = 0x0104
expect_after_value = 0x0104
} else {
args.dst.write_fast_token!(
value: 0x20_2012,
length: 1)
// 0x24 is (EXPECT_CLOSE_BRACE | EXPECT_COMMA).
expect = 0x24
expect_after_value = 0x24
// 0x0044 is (EXPECT_CLOSE_CURLY_BRACE | EXPECT_COMMA).
expect = 0x0044
expect_after_value = 0x0044
}
continue.outer
} else if class == 0x09 { // 0x09 is CLASS_FALSE.
if 0 == (expect & 0x01) { // 0x01 is EXPECT_NON_STRING_VALUE.
return "#bad input"
}
match = args.src.match7(a: 0x6573_6C61_6605) // 5 bytes "false".
if match == 0 {
args.dst.write_fast_token!(
@@ -526,9 +490,6 @@ pub func decoder.decode_tokens?(dst: base.token_writer, src: base.io_reader) {
}
} else if class == 0x0A { // 0x0A is CLASS_TRUE.
if 0 == (expect & 0x01) { // 0x01 is EXPECT_NON_STRING_VALUE.
return "#bad input"
}
match = args.src.match7(a: 0x65_7572_7404) // 4 bytes "true".
if match == 0 {
args.dst.write_fast_token!(
@@ -545,9 +506,6 @@ pub func decoder.decode_tokens?(dst: base.token_writer, src: base.io_reader) {
}
} else if class == 0x0B { // 0x0B is CLASS_NULL.
if 0 == (expect & 0x01) { // 0x01 is EXPECT_NON_STRING_VALUE.
return "#bad input"
}
match = args.src.match7(a: 0x6C_6C75_6E04) // 4 bytes "null".
if match == 0 {
args.dst.write_fast_token!(