Add std/json quirk_allow_backslash_capital_u

This commit is contained in:
Nigel Tao committed 2020-03-19 23:20:45 +11:00
1 parent 97f7f28218
commit 83e7da0a6a
4 files changed
+175 -13

No files matched your search

+71
View File
@@ -122,6 +122,10 @@ pub func decoder.decode_tokens?(dst: base.token_writer, src: base.io_reader) {
var uni4_value : base.u32[..= 0xFFFF]
var uni4_high_surrogate : base.u32[..= 0x10_FC00]
var uni8_ok : base.u8
var uni8_string : base.u64
var uni8_value : base.u32[..= 0xFFFF_FFFF]
// expect is a bitmask of what the next character class can be.
//
// expect_after_value is what to expect after seeing a value (a literal,
@@ -483,6 +487,73 @@ pub func decoder.decode_tokens?(dst: base.token_writer, src: base.io_reader) {
continue.string_loop_outer
}
// -------- END backslash-u.
} else if (c == 0x55) and
this.quirk_enabled_allow_backslash_capital_u { // 0x55 is 'U'.
// -------- BEGIN backslash-capital-u.
if args.src.available() < 10 {
if args.src.is_closed() {
return "#bad backslash-escape"
}
yield? base."$short read"
string_length = 0
char = 0
continue.string_loop_outer
}
uni8_string = args.src.peek_u64le_at(offset: 2)
uni8_value = 0
uni8_ok = 0x80
c = lut_hexadecimal_digits[0xFF & (uni8_string >> 0)]
uni8_ok &= c
uni8_value |= ((c & 0x0F) as base.u32) << 28
c = lut_hexadecimal_digits[0xFF & (uni8_string >> 8)]
uni8_ok &= c
uni8_value |= ((c & 0x0F) as base.u32) << 24
c = lut_hexadecimal_digits[0xFF & (uni8_string >> 16)]
uni8_ok &= c
uni8_value |= ((c & 0x0F) as base.u32) << 20
c = lut_hexadecimal_digits[0xFF & (uni8_string >> 24)]
uni8_ok &= c
uni8_value |= ((c & 0x0F) as base.u32) << 16
c = lut_hexadecimal_digits[0xFF & (uni8_string >> 32)]
uni8_ok &= c
uni8_value |= ((c & 0x0F) as base.u32) << 12
c = lut_hexadecimal_digits[0xFF & (uni8_string >> 40)]
uni8_ok &= c
uni8_value |= ((c & 0x0F) as base.u32) << 8
c = lut_hexadecimal_digits[0xFF & (uni8_string >> 48)]
uni8_ok &= c
uni8_value |= ((c & 0x0F) as base.u32) << 4
c = lut_hexadecimal_digits[0xFF & (uni8_string >> 56)]
uni8_ok &= c
uni8_value |= ((c & 0x0F) as base.u32) << 0
if uni8_ok == 0 {
// It wasn't 8 hexadecimal digits. No-op
// (and fall through to "#bad
// backslash-escape").
} else if (uni8_value < 0xD800) or (
(0xDFFF < uni8_value) and (uni8_value <= 0x10_FFFF)) {
// Not a Unicode surrogate. We're good.
args.src.skip32_fast!(actual: 10, worst_case: 10)
args.dst.write_fast_token!(
value_major: 0,
value_minor: 0x60_0000 | (uni8_value & 0x1F_FFFF),
link: 0x3,
length: 10)
continue.string_loop_outer
} else if this.quirk_enabled_replace_invalid_utf_8 {
args.src.skip32_fast!(actual: 10, worst_case: 10)
args.dst.write_fast_token!(
value_major: 0,
value_minor: 0x60_FFFD,
link: 0x3,
length: 6)
continue.string_loop_outer
}
// -------- END backslash-capital-u.
}
return "#bad backslash-escape"
+12 -2
View File
@@ -28,6 +28,8 @@ pub const quirk_allow_backslash_a base.u32 = 0x4909_9400 | 0x00
// When this quirk is enabled, e.g. "abc\U0001F4A9z" is accepted as a JSON
// string, equivalent to "abc\uD83D\uDCA9z", containing the U+0001F4A9 PILE OF
// POO Unicode code point. There are exactly 8 encoded bytes after each "\U".
//
// This quirk can combine with quirk_replace_invalid_utf_8.
pub const quirk_allow_backslash_capital_u base.u32 = 0x4909_9400 | 0x01
// When this quirk is enabled, e.g. "abc\ez" is accepted as a JSON string,
@@ -51,7 +53,9 @@ pub const quirk_allow_backslash_v base.u32 = 0x4909_9400 | 0x05
// decoding to 5 bytes: 0x61, 0x62, 0x63, 0xEF and 0x7A. There are exactly 2
// encoded bytes after each "\x".
//
// Decoded strings are byte strings, no longer guaranteed to be valid UTF-8.
// Decoded strings are byte strings, no longer guaranteed to be valid UTF-8 and
// even if the overall byte string is valid UTF-8, tokens are also no longer
// guaranteed to split on UTF-8 boundaries.
//
// "\x", "\x9", "\x9$" and "\X99" are all still rejected.
pub const quirk_allow_backslash_x base.u32 = 0x4909_9400 | 0x06
@@ -149,10 +153,16 @@ pub const quirk_allow_trailing_new_line base.u32 = 0x4909_9400 | 0x0E
// When this quirk is enabled, invalid UTF-8 inside a JSON string is accepted.
// Each byte of invalid UTF-8 is equivalent to "\uFFFD", the Unicode
// Replacement Character. Invalid UTF-8 outside a JSON string remains an error.
// Replacement Character. The UTF-8 encoding of U+FFFD is "\xEF\xBF\xBD".
//
// Invalid UTF-8 outside a JSON string remains an error.
//
// Similarly, for backslash-u escapes featuring incorrectly paired Unicode
// surrogates, each backslash-u 6-byte unit is replaced. For example,
// "abc\uDC00z" and "ijk\uD800\uDBFFz" are equivalent to "abc\uFFFDz" and
// "ijk\uFFFD\uFFFDz".
//
// When combined with quirk_allow_backslash_capital_u, a "\U12345678" 10-byte
// unit that is an invalid Unicode code point (i.e. in the range U+D800 ..=
// U+DFFF or above U+10FFFF) is similarly replaced with U+FFFD.
pub const quirk_replace_invalid_utf_8 base.u32 = 0x4909_9400 | 0x0F