mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-18 08:32:11 +01:00
392 lines
12 KiB
Plaintext
392 lines
12 KiB
Plaintext
// Copyright 2020 The Wuffs Authors.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
pub status "#bad input"
|
|
|
|
pri status "#internal error: inconsistent I/O"
|
|
pri status "#internal error: inconsistent token length"
|
|
|
|
// TOKEN_VALUE_MAJOR is the base-38 encoding of "cbor".
|
|
pub const TOKEN_VALUE_MAJOR : base.u32 = 0x0C_061D
|
|
|
|
// TOKEN_VALUE_MINOR__TAG means that the remaining 24 bits of the token's
|
|
// value_minor is a CBOR tag. That token may be continued, in which case the
|
|
// following token is an extended token (46 bits) and the overall 64-bit CBOR
|
|
// tag is ((first << 46) | ext).
|
|
pub const TOKEN_VALUE_MINOR__TAG : base.u32 = 0x01
|
|
|
|
// TOKEN_VALUE_MINOR__MINUS_1_MINUS_X means that the token's final
|
|
// (token.length - 1) bytes (which must be one of 1, 2, 4 or 8 bytes) contains
|
|
// a big-endian unsigned integer x. The token's overall value is the negative
|
|
// number (-1 - x). Note that this value can underflow an int64_t, and its
|
|
// absolute value (+1 + x) can overflow a uint64_t.
|
|
pub const TOKEN_VALUE_MINOR__MINUS_1_MINUS_X : base.u32 = 0x02
|
|
|
|
pub struct decoder? implements base.token_decoder(
|
|
end_of_data : base.bool,
|
|
|
|
util : base.utility,
|
|
)(
|
|
)
|
|
|
|
pub func decoder.set_quirk_enabled!(quirk: base.u32, enabled: base.bool) {
|
|
}
|
|
|
|
pub func decoder.workbuf_len() base.range_ii_u64 {
|
|
return this.util.empty_range_ii_u64()
|
|
}
|
|
|
|
pub func decoder.decode_tokens?(dst: base.token_writer, src: base.io_reader, workbuf: slice base.u8) {
|
|
var string_length : base.u64
|
|
|
|
var n64 : base.u64
|
|
var depth : base.u32[..= 1024]
|
|
var token_length : base.u32[..= 0xFFFF]
|
|
var value_minor : base.u32[..= 0x1FF_FFFF]
|
|
var continued : base.u32[..= 1]
|
|
var c : base.u8
|
|
var c_major : base.u8
|
|
var c_minor : base.u8
|
|
|
|
// indefinite_string_major_type is 2 or 3 when we are in an
|
|
// indefinite-length byte string or text string. It is 0 otherwise.
|
|
var indefinite_string_major_type : base.u8[..= 3]
|
|
|
|
if this.end_of_data {
|
|
return base."@end of data"
|
|
}
|
|
|
|
while.outer true {
|
|
while.goto_parsed_a_leaf_value true {{
|
|
while.goto_fail true {{
|
|
if args.dst.available() <= 0 {
|
|
yield? base."$short write"
|
|
continue.outer
|
|
}
|
|
if args.src.available() <= 0 {
|
|
if args.src.is_closed() {
|
|
return "#bad input"
|
|
}
|
|
yield? base."$short read"
|
|
continue.outer
|
|
}
|
|
c = args.src.peek_u8()
|
|
|
|
if (indefinite_string_major_type <> 0) and (indefinite_string_major_type <> (c >> 5)) {
|
|
if c <> 0xFF {
|
|
return "#bad input"
|
|
}
|
|
value_minor = (base.TOKEN__VBC__STRING << 21) |
|
|
base.TOKEN__VBD__STRING__CONVERT_0_DST_1_SRC_DROP
|
|
if indefinite_string_major_type == 3 {
|
|
value_minor |= base.TOKEN__VBD__STRING__DEFINITELY_UTF_8 |
|
|
base.TOKEN__VBD__STRING__CHAIN_MUST_BE_UTF_8 |
|
|
base.TOKEN__VBD__STRING__DEFINITELY_ASCII
|
|
}
|
|
indefinite_string_major_type = 0
|
|
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
|
args.dst.write_simple_token_fast!(
|
|
value_major: 0,
|
|
value_minor: value_minor,
|
|
continued: 0,
|
|
length: 1)
|
|
break.goto_parsed_a_leaf_value
|
|
}
|
|
|
|
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
|
c_major = c >> 5
|
|
c_minor = c & 0x1F
|
|
if c_minor < 0x18 {
|
|
string_length = c_minor as base.u64
|
|
} else {
|
|
while.goto_have_string_length true,
|
|
inv args.dst.available() > 0,
|
|
{{
|
|
if c_minor == 0x18 {
|
|
if args.src.available() >= 1 {
|
|
string_length = args.src.peek_u8_as_u64()
|
|
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
|
break.goto_have_string_length
|
|
}
|
|
} else if c_minor == 0x19 {
|
|
if args.src.available() >= 2 {
|
|
string_length = args.src.peek_u16be_as_u64()
|
|
args.src.skip_u32_fast!(actual: 2, worst_case: 2)
|
|
break.goto_have_string_length
|
|
}
|
|
} else if c_minor == 0x1A {
|
|
if args.src.available() >= 4 {
|
|
string_length = args.src.peek_u32be_as_u64()
|
|
args.src.skip_u32_fast!(actual: 4, worst_case: 4)
|
|
break.goto_have_string_length
|
|
}
|
|
} else if c_minor == 0x1B {
|
|
if args.src.available() >= 8 {
|
|
string_length = args.src.peek_u64be()
|
|
args.src.skip_u32_fast!(actual: 8, worst_case: 8)
|
|
break.goto_have_string_length
|
|
}
|
|
} else {
|
|
string_length = 0
|
|
break.goto_have_string_length
|
|
}
|
|
|
|
if args.src.can_undo_byte() {
|
|
args.src.undo_byte!()
|
|
if args.src.is_closed() {
|
|
return "#bad input"
|
|
}
|
|
yield? base."$short read"
|
|
c_major = 0
|
|
c_minor = 0
|
|
continue.outer
|
|
}
|
|
return "#internal error: inconsistent I/O"
|
|
}} endwhile.goto_have_string_length
|
|
}
|
|
|
|
if c_major == 0 {
|
|
// -------- BEGIN Major type 0: an unsigned integer.
|
|
if c_minor < 0x18 {
|
|
args.dst.write_simple_token_fast!(
|
|
value_major: 0,
|
|
value_minor: (base.TOKEN__VBC__INLINE_INTEGER << 21) |
|
|
(c_minor as base.u32),
|
|
continued: 0,
|
|
length: 1)
|
|
break.goto_parsed_a_leaf_value
|
|
} else if c_minor < 0x1C {
|
|
args.dst.write_simple_token_fast!(
|
|
value_major: 0,
|
|
value_minor: (base.TOKEN__VBC__NUMBER << 21) |
|
|
base.TOKEN__VBD__NUMBER__CONTENT_INTEGER_UNSIGNED |
|
|
base.TOKEN__VBD__NUMBER__FORMAT_BINARY_BIG_ENDIAN |
|
|
base.TOKEN__VBD__NUMBER__FORMAT_IGNORE_FIRST_BYTE,
|
|
continued: 0,
|
|
length: 1 + ((1 as base.u32) << (c_minor - 0x18)))
|
|
break.goto_parsed_a_leaf_value
|
|
}
|
|
// -------- END Major type 0: an unsigned integer.
|
|
|
|
} else if c_major == 1 {
|
|
// -------- END Major type 1: a negative integer.
|
|
if c_minor < 0x18 {
|
|
args.dst.write_simple_token_fast!(
|
|
value_major: 0,
|
|
value_minor: (base.TOKEN__VBC__INLINE_INTEGER << 21) |
|
|
(0x1F_FFFF - (c_minor as base.u32)),
|
|
continued: 0,
|
|
length: 1)
|
|
break.goto_parsed_a_leaf_value
|
|
} else if c_minor < 0x1C {
|
|
args.dst.write_simple_token_fast!(
|
|
value_major: TOKEN_VALUE_MAJOR,
|
|
value_minor: TOKEN_VALUE_MINOR__MINUS_1_MINUS_X,
|
|
continued: 0,
|
|
length: 1 + ((1 as base.u32) << (c_minor - 0x18)))
|
|
break.goto_parsed_a_leaf_value
|
|
}
|
|
// -------- END Major type 1: a negative integer.
|
|
|
|
} else if c_major == 2 {
|
|
// -------- BEGIN Major type 2: a byte string.
|
|
if c_minor == 0x00 {
|
|
args.dst.write_simple_token_fast!(
|
|
value_major: 0,
|
|
value_minor: (base.TOKEN__VBC__STRING << 21) |
|
|
base.TOKEN__VBD__STRING__CONVERT_0_DST_1_SRC_DROP,
|
|
continued: 0,
|
|
length: 1)
|
|
break.goto_parsed_a_leaf_value
|
|
} else if c_minor < 0x18 {
|
|
args.dst.write_simple_token_fast!(
|
|
value_major: 0,
|
|
value_minor: (base.TOKEN__VBC__STRING << 21) |
|
|
base.TOKEN__VBD__STRING__CONVERT_0_DST_1_SRC_DROP,
|
|
continued: 1,
|
|
length: 1)
|
|
} else if c_minor < 0x1C {
|
|
args.dst.write_simple_token_fast!(
|
|
value_major: 0,
|
|
value_minor: (base.TOKEN__VBC__STRING << 21) |
|
|
base.TOKEN__VBD__STRING__CONVERT_0_DST_1_SRC_DROP,
|
|
continued: 1,
|
|
length: 1 + ((1 as base.u32) << (c_minor & 3)))
|
|
} else if c_minor == 0x1F {
|
|
indefinite_string_major_type = 2
|
|
args.dst.write_simple_token_fast!(
|
|
value_major: 0,
|
|
value_minor: (base.TOKEN__VBC__STRING << 21) |
|
|
base.TOKEN__VBD__STRING__CONVERT_0_DST_1_SRC_DROP,
|
|
continued: 1,
|
|
length: 1)
|
|
continue.outer
|
|
} else {
|
|
break.goto_fail
|
|
}
|
|
|
|
while true {
|
|
if args.dst.available() <= 0 {
|
|
yield? base."$short write"
|
|
continue
|
|
}
|
|
n64 = string_length.min(a: args.src.available())
|
|
token_length = (n64 & 0xFFFF) as base.u32
|
|
if n64 > 0xFFFF {
|
|
token_length = 0xFFFF
|
|
} else if token_length <= 0 {
|
|
if args.src.is_closed() {
|
|
return "#bad input"
|
|
}
|
|
yield? base."$short read"
|
|
continue
|
|
}
|
|
if args.src.available() < (token_length as base.u64) {
|
|
return "#internal error: inconsistent token length"
|
|
}
|
|
string_length ~mod-= token_length as base.u64
|
|
continued = 0
|
|
if (string_length > 0) or (indefinite_string_major_type > 0) {
|
|
continued = 1
|
|
}
|
|
args.src.skip_u32_fast!(actual: token_length, worst_case: token_length)
|
|
args.dst.write_simple_token_fast!(
|
|
value_major: 0,
|
|
value_minor: (base.TOKEN__VBC__STRING << 21) |
|
|
base.TOKEN__VBD__STRING__CONVERT_1_DST_1_SRC_COPY,
|
|
continued: continued,
|
|
length: token_length)
|
|
if string_length > 0 {
|
|
continue
|
|
} else if indefinite_string_major_type > 0 {
|
|
continue.outer
|
|
}
|
|
break.goto_parsed_a_leaf_value
|
|
} endwhile
|
|
// -------- END Major type 2: a byte string.
|
|
|
|
} else if c_major == 3 {
|
|
// -------- BEGIN Major type 3: a text string.
|
|
if c_minor == 0x00 {
|
|
args.dst.write_simple_token_fast!(
|
|
value_major: 0,
|
|
value_minor: (base.TOKEN__VBC__STRING << 21) |
|
|
base.TOKEN__VBD__STRING__DEFINITELY_UTF_8 |
|
|
base.TOKEN__VBD__STRING__CHAIN_MUST_BE_UTF_8 |
|
|
base.TOKEN__VBD__STRING__DEFINITELY_ASCII |
|
|
base.TOKEN__VBD__STRING__CONVERT_0_DST_1_SRC_DROP,
|
|
continued: 0,
|
|
length: 1)
|
|
break.goto_parsed_a_leaf_value
|
|
} else if c_minor < 0x18 {
|
|
args.dst.write_simple_token_fast!(
|
|
value_major: 0,
|
|
value_minor: (base.TOKEN__VBC__STRING << 21) |
|
|
base.TOKEN__VBD__STRING__DEFINITELY_UTF_8 |
|
|
base.TOKEN__VBD__STRING__CHAIN_MUST_BE_UTF_8 |
|
|
base.TOKEN__VBD__STRING__DEFINITELY_ASCII |
|
|
base.TOKEN__VBD__STRING__CONVERT_0_DST_1_SRC_DROP,
|
|
continued: 1,
|
|
length: 1)
|
|
} else if c_minor < 0x1C {
|
|
args.dst.write_simple_token_fast!(
|
|
value_major: 0,
|
|
value_minor: (base.TOKEN__VBC__STRING << 21) |
|
|
base.TOKEN__VBD__STRING__DEFINITELY_UTF_8 |
|
|
base.TOKEN__VBD__STRING__CHAIN_MUST_BE_UTF_8 |
|
|
base.TOKEN__VBD__STRING__DEFINITELY_ASCII |
|
|
base.TOKEN__VBD__STRING__CONVERT_0_DST_1_SRC_DROP,
|
|
continued: 1,
|
|
length: 1 + ((1 as base.u32) << (c_minor & 3)))
|
|
} else if c_minor == 0x1F {
|
|
indefinite_string_major_type = 3
|
|
args.dst.write_simple_token_fast!(
|
|
value_major: 0,
|
|
value_minor: (base.TOKEN__VBC__STRING << 21) |
|
|
base.TOKEN__VBD__STRING__DEFINITELY_UTF_8 |
|
|
base.TOKEN__VBD__STRING__CHAIN_MUST_BE_UTF_8 |
|
|
base.TOKEN__VBD__STRING__DEFINITELY_ASCII |
|
|
base.TOKEN__VBD__STRING__CONVERT_0_DST_1_SRC_DROP,
|
|
continued: 1,
|
|
length: 1)
|
|
continue.outer
|
|
} else {
|
|
break.goto_fail
|
|
}
|
|
|
|
while true {
|
|
if args.dst.available() <= 0 {
|
|
yield? base."$short write"
|
|
continue
|
|
}
|
|
n64 = string_length.min(a: args.src.available())
|
|
token_length = (n64 & 0xFFFF) as base.u32
|
|
if n64 > 0xFFFF {
|
|
token_length = 0xFFFF
|
|
}
|
|
// TODO: walk token_length back to a UTF-8 boundary.
|
|
if token_length <= 0 {
|
|
if args.src.is_closed() {
|
|
return "#bad input"
|
|
}
|
|
yield? base."$short read"
|
|
continue
|
|
}
|
|
if args.src.available() < (token_length as base.u64) {
|
|
return "#internal error: inconsistent token length"
|
|
}
|
|
string_length ~mod-= token_length as base.u64
|
|
continued = 0
|
|
if (string_length > 0) or (indefinite_string_major_type > 0) {
|
|
continued = 1
|
|
}
|
|
args.src.skip_u32_fast!(actual: token_length, worst_case: token_length)
|
|
args.dst.write_simple_token_fast!(
|
|
value_major: 0,
|
|
value_minor: (base.TOKEN__VBC__STRING << 21) |
|
|
base.TOKEN__VBD__STRING__DEFINITELY_UTF_8 |
|
|
base.TOKEN__VBD__STRING__CHAIN_MUST_BE_UTF_8 |
|
|
base.TOKEN__VBD__STRING__DEFINITELY_ASCII |
|
|
base.TOKEN__VBD__STRING__CONVERT_1_DST_1_SRC_COPY,
|
|
continued: continued,
|
|
length: token_length)
|
|
if string_length > 0 {
|
|
continue
|
|
} else if indefinite_string_major_type > 0 {
|
|
continue.outer
|
|
}
|
|
break.goto_parsed_a_leaf_value
|
|
} endwhile
|
|
// -------- END Major type 3: a text string.
|
|
}
|
|
break.goto_fail
|
|
}} endwhile.goto_fail
|
|
|
|
if args.src.can_undo_byte() {
|
|
args.src.undo_byte!()
|
|
return "#bad input"
|
|
}
|
|
return "#internal error: inconsistent I/O"
|
|
}} endwhile.goto_parsed_a_leaf_value
|
|
|
|
// We've just parsed a leaf (non-container) value: literal (null,
|
|
// false, true), number or string.
|
|
if depth == 0 {
|
|
break.outer
|
|
}
|
|
} endwhile.outer
|
|
|
|
this.end_of_data = true
|
|
}
|