mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-18 00:32:11 +01:00
wuffsfmt: indent with 4 spaces (not a tab)
This commit is contained in:
1 parent
9c03b8c71d
commit
f5eb38d76e
40 files changed
+12586
-12580
No files matched your search
+2
-1
@@ -6,7 +6,8 @@
|
||||
The dot points below probably aren't of interest unless you're _writing_ Wuffs
|
||||
code (instead of writing C/C++ code that _uses_ Wuffs' standard library).
|
||||
|
||||
- `wuffsfmt` double-indents hanging lines.
|
||||
- `wuffsfmt` double-indents hanging lines and each indent is now 4 spaces (not
|
||||
a tab).
|
||||
|
||||
|
||||
## 2023-04-08 version 0.3.3
|
||||
|
||||
+23
-23
@@ -16,34 +16,34 @@ pri status "#not a digit"
|
||||
pri status "#too large"
|
||||
|
||||
pub struct parser?(
|
||||
val : base.u32,
|
||||
val : base.u32,
|
||||
)
|
||||
|
||||
pub func parser.parse?(src: base.io_reader) {
|
||||
var c : base.u8
|
||||
while true {
|
||||
c = args.src.read_u8?()
|
||||
if c == 0 {
|
||||
return ok
|
||||
}
|
||||
if (c < 0x30) or (0x39 < c) { // '0' and '9' are ASCII 0x30 and 0x39.
|
||||
return "#not a digit"
|
||||
}
|
||||
// Rebase from ASCII (0x30 ..= 0x39) to the value (0 ..= 9).
|
||||
c -= 0x30
|
||||
var c : base.u8
|
||||
while true {
|
||||
c = args.src.read_u8?()
|
||||
if c == 0 {
|
||||
return ok
|
||||
}
|
||||
if (c < 0x30) or (0x39 < c) { // '0' and '9' are ASCII 0x30 and 0x39.
|
||||
return "#not a digit"
|
||||
}
|
||||
// Rebase from ASCII (0x30 ..= 0x39) to the value (0 ..= 9).
|
||||
c -= 0x30
|
||||
|
||||
if this.val < 429_496729 {
|
||||
this.val = (10 * this.val) + (c as base.u32)
|
||||
continue
|
||||
} else if (this.val > 429_496729) or (c > 5) {
|
||||
return "#too large"
|
||||
}
|
||||
// Uncomment this assertion to see what facts are known here.
|
||||
// assert false
|
||||
this.val = (10 * this.val) + (c as base.u32)
|
||||
} endwhile
|
||||
if this.val < 429_496729 {
|
||||
this.val = (10 * this.val) + (c as base.u32)
|
||||
continue
|
||||
} else if (this.val > 429_496729) or (c > 5) {
|
||||
return "#too large"
|
||||
}
|
||||
// Uncomment this assertion to see what facts are known here.
|
||||
// assert false
|
||||
this.val = (10 * this.val) + (c as base.u32)
|
||||
} endwhile
|
||||
}
|
||||
|
||||
pub func parser.value() base.u32 {
|
||||
return this.val
|
||||
return this.val
|
||||
}
|
||||
+11
-6
@@ -294,13 +294,18 @@ func appendNum(buf []byte, s string) []byte {
|
||||
return buf
|
||||
}
|
||||
|
||||
func appendTabs(buf []byte, n int) []byte {
|
||||
if n > 0 {
|
||||
const tabs = "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
|
||||
for ; n > len(tabs); n -= len(tabs) {
|
||||
buf = append(buf, tabs...)
|
||||
func appendTabs(buf []byte, nTabs int) []byte {
|
||||
nSpaces := 4 * nTabs // Hard-code 4 spaces per tab.
|
||||
if nSpaces > 0 {
|
||||
const spaces = "" + // 256 spaces.
|
||||
" " +
|
||||
" " +
|
||||
" " +
|
||||
" "
|
||||
for ; nSpaces > len(spaces); nSpaces -= len(spaces) {
|
||||
buf = append(buf, spaces...)
|
||||
}
|
||||
buf = append(buf, tabs[:n]...)
|
||||
buf = append(buf, spaces[:nSpaces]...)
|
||||
}
|
||||
return buf
|
||||
}
|
||||
|
||||
@@ -14,58 +14,58 @@
|
||||
|
||||
// TODO: drop the '?' but still generate wuffs_adler32__hasher__initialize?
|
||||
pub struct hasher? implements base.hasher_u32(
|
||||
state : base.u32,
|
||||
started : base.bool,
|
||||
state : base.u32,
|
||||
started : base.bool,
|
||||
)
|
||||
|
||||
pub func hasher.set_quirk_enabled!(quirk: base.u32, enabled: base.bool) {
|
||||
}
|
||||
|
||||
pub func hasher.update_u32!(x: slice base.u8) base.u32 {
|
||||
if not this.started {
|
||||
this.started = true
|
||||
this.state = 1
|
||||
// There used to be an up_x86_avx2 implementation too, but while it
|
||||
// made the std/adler32 micro-benchmarks better, it also made the
|
||||
// std/zlib and std/png micro-benchmarks worse. See commit baec831f
|
||||
// "Add std/adler32 hasher.up_x86_avx2".
|
||||
choose up = [
|
||||
up_arm_neon,
|
||||
up_x86_sse42]
|
||||
}
|
||||
this.up!(x: args.x)
|
||||
return this.state
|
||||
if not this.started {
|
||||
this.started = true
|
||||
this.state = 1
|
||||
// There used to be an up_x86_avx2 implementation too, but while it
|
||||
// made the std/adler32 micro-benchmarks better, it also made the
|
||||
// std/zlib and std/png micro-benchmarks worse. See commit baec831f
|
||||
// "Add std/adler32 hasher.up_x86_avx2".
|
||||
choose up = [
|
||||
up_arm_neon,
|
||||
up_x86_sse42]
|
||||
}
|
||||
this.up!(x: args.x)
|
||||
return this.state
|
||||
}
|
||||
|
||||
pri func hasher.up!(x: slice base.u8),
|
||||
choosy,
|
||||
choosy,
|
||||
{
|
||||
// The Adler-32 checksum's magic 65521 and 5552 numbers are discussed in
|
||||
// this package's README.md.
|
||||
// The Adler-32 checksum's magic 65521 and 5552 numbers are discussed in
|
||||
// this package's README.md.
|
||||
|
||||
var s1 : base.u32
|
||||
var s2 : base.u32
|
||||
var remaining : slice base.u8
|
||||
var p : slice base.u8
|
||||
var s1 : base.u32
|
||||
var s2 : base.u32
|
||||
var remaining : slice base.u8
|
||||
var p : slice base.u8
|
||||
|
||||
s1 = this.state.low_bits(n: 16)
|
||||
s2 = this.state.high_bits(n: 16)
|
||||
while args.x.length() > 0 {
|
||||
remaining = args.x[.. 0]
|
||||
if args.x.length() > 5552 {
|
||||
remaining = args.x[5552 ..]
|
||||
args.x = args.x[.. 5552]
|
||||
}
|
||||
s1 = this.state.low_bits(n: 16)
|
||||
s2 = this.state.high_bits(n: 16)
|
||||
while args.x.length() > 0 {
|
||||
remaining = args.x[.. 0]
|
||||
if args.x.length() > 5552 {
|
||||
remaining = args.x[5552 ..]
|
||||
args.x = args.x[.. 5552]
|
||||
}
|
||||
|
||||
// The SIMD versions of this function replace this simple iterate loop.
|
||||
iterate (p = args.x)(length: 1, advance: 1, unroll: 8) {
|
||||
s1 ~mod+= p[0] as base.u32
|
||||
s2 ~mod+= s1
|
||||
}
|
||||
// The SIMD versions of this function replace this simple iterate loop.
|
||||
iterate (p = args.x)(length: 1, advance: 1, unroll: 8) {
|
||||
s1 ~mod+= p[0] as base.u32
|
||||
s2 ~mod+= s1
|
||||
}
|
||||
|
||||
s1 %= 65521
|
||||
s2 %= 65521
|
||||
args.x = remaining
|
||||
} endwhile
|
||||
this.state = ((s2 & 0xFFFF) << 16) | (s1 & 0xFFFF)
|
||||
s1 %= 65521
|
||||
s2 %= 65521
|
||||
args.x = remaining
|
||||
} endwhile
|
||||
this.state = ((s2 & 0xFFFF) << 16) | (s1 & 0xFFFF)
|
||||
}
|
||||
@@ -13,183 +13,183 @@
|
||||
// limitations under the License.
|
||||
|
||||
pri func hasher.up_arm_neon!(x: slice base.u8),
|
||||
choose cpu_arch >= arm_neon,
|
||||
choose cpu_arch >= arm_neon,
|
||||
{
|
||||
// These variables are the same as the non-SIMD version.
|
||||
var s1 : base.u32
|
||||
var s2 : base.u32
|
||||
var remaining : slice base.u8
|
||||
var p : slice base.u8
|
||||
// These variables are the same as the non-SIMD version.
|
||||
var s1 : base.u32
|
||||
var s2 : base.u32
|
||||
var remaining : slice base.u8
|
||||
var p : slice base.u8
|
||||
|
||||
// The remaining variables are specific to the SIMD version.
|
||||
// The remaining variables are specific to the SIMD version.
|
||||
|
||||
var util : base.arm_neon_utility
|
||||
var p__left : base.arm_neon_u8x16
|
||||
var p_right : base.arm_neon_u8x16
|
||||
var v1 : base.arm_neon_u32x4
|
||||
var v2 : base.arm_neon_u32x4
|
||||
var col0 : base.arm_neon_u16x8
|
||||
var col1 : base.arm_neon_u16x8
|
||||
var col2 : base.arm_neon_u16x8
|
||||
var col3 : base.arm_neon_u16x8
|
||||
var sum1 : base.arm_neon_u32x2
|
||||
var sum2 : base.arm_neon_u32x2
|
||||
var sum12 : base.arm_neon_u32x2
|
||||
var util : base.arm_neon_utility
|
||||
var p__left : base.arm_neon_u8x16
|
||||
var p_right : base.arm_neon_u8x16
|
||||
var v1 : base.arm_neon_u32x4
|
||||
var v2 : base.arm_neon_u32x4
|
||||
var col0 : base.arm_neon_u16x8
|
||||
var col1 : base.arm_neon_u16x8
|
||||
var col2 : base.arm_neon_u16x8
|
||||
var col3 : base.arm_neon_u16x8
|
||||
var sum1 : base.arm_neon_u32x2
|
||||
var sum2 : base.arm_neon_u32x2
|
||||
var sum12 : base.arm_neon_u32x2
|
||||
|
||||
var num_iterate_bytes : base.u32
|
||||
var tail_index : base.u64
|
||||
var num_iterate_bytes : base.u32
|
||||
var tail_index : base.u64
|
||||
|
||||
// Decompose this.state.
|
||||
s1 = this.state.low_bits(n: 16)
|
||||
s2 = this.state.high_bits(n: 16)
|
||||
// Decompose this.state.
|
||||
s1 = this.state.low_bits(n: 16)
|
||||
s2 = this.state.high_bits(n: 16)
|
||||
|
||||
// Align to a 16-byte boundary.
|
||||
while (args.x.length() > 0) and ((15 & args.x.uintptr_low_12_bits()) <> 0) {
|
||||
s1 ~mod+= args.x[0] as base.u32
|
||||
s2 ~mod+= s1
|
||||
args.x = args.x[1 ..]
|
||||
} endwhile
|
||||
s1 %= 65521
|
||||
s2 %= 65521
|
||||
// Align to a 16-byte boundary.
|
||||
while (args.x.length() > 0) and ((15 & args.x.uintptr_low_12_bits()) <> 0) {
|
||||
s1 ~mod+= args.x[0] as base.u32
|
||||
s2 ~mod+= s1
|
||||
args.x = args.x[1 ..]
|
||||
} endwhile
|
||||
s1 %= 65521
|
||||
s2 %= 65521
|
||||
|
||||
// Just like the non-SIMD version, loop over args.x up to almost-5552 bytes
|
||||
// at a time. The slightly smaller 5536 is the largest multiple of 32 less
|
||||
// than non-SIMD's 5552.
|
||||
while args.x.length() > 0 {
|
||||
remaining = args.x[.. 0]
|
||||
if args.x.length() > 5536 {
|
||||
remaining = args.x[5536 ..]
|
||||
args.x = args.x[.. 5536]
|
||||
}
|
||||
// Just like the non-SIMD version, loop over args.x up to almost-5552 bytes
|
||||
// at a time. The slightly smaller 5536 is the largest multiple of 32 less
|
||||
// than non-SIMD's 5552.
|
||||
while args.x.length() > 0 {
|
||||
remaining = args.x[.. 0]
|
||||
if args.x.length() > 5536 {
|
||||
remaining = args.x[5536 ..]
|
||||
args.x = args.x[.. 5536]
|
||||
}
|
||||
|
||||
// The s1 state is the sum of the input bytes and the s2 state is the
|
||||
// sum of the s1 state at each 1-byte step. Inside the iterate loop
|
||||
// below, but starting fresh at each outer while loop iteration, s1
|
||||
// consists of three parts (called s1i, s1j and s1k):
|
||||
// - s1i: the initial value, before any 32-byte iterations.
|
||||
// - s1j: the total contribution from previous 32-byte iterations.
|
||||
// - s1k: the contribution due to the current 32-byte iteration.
|
||||
//
|
||||
// The upcoming iterate loop (at 32 bytes per iteration) encompasses
|
||||
// num_iterate_bytes 1-byte steps. We hoist the total s1i contribution,
|
||||
// (s1i * num_iterate_bytes) out here.
|
||||
num_iterate_bytes = (args.x.length() & 0xFFFF_FFE0) as base.u32
|
||||
s2 ~mod+= (s1 ~mod* num_iterate_bytes)
|
||||
// The s1 state is the sum of the input bytes and the s2 state is the
|
||||
// sum of the s1 state at each 1-byte step. Inside the iterate loop
|
||||
// below, but starting fresh at each outer while loop iteration, s1
|
||||
// consists of three parts (called s1i, s1j and s1k):
|
||||
// - s1i: the initial value, before any 32-byte iterations.
|
||||
// - s1j: the total contribution from previous 32-byte iterations.
|
||||
// - s1k: the contribution due to the current 32-byte iteration.
|
||||
//
|
||||
// The upcoming iterate loop (at 32 bytes per iteration) encompasses
|
||||
// num_iterate_bytes 1-byte steps. We hoist the total s1i contribution,
|
||||
// (s1i * num_iterate_bytes) out here.
|
||||
num_iterate_bytes = (args.x.length() & 0xFFFF_FFE0) as base.u32
|
||||
s2 ~mod+= (s1 ~mod* num_iterate_bytes)
|
||||
|
||||
// Zero-initialize some u32×4 vectors associated with the two state
|
||||
// variables s1 and s2. The iterate loop accumulates four parallel u32
|
||||
// sums in each vector. A post-iterate step merges the four u32 sums
|
||||
// into a single u32 sum.
|
||||
v1 = util.make_u32x4_repeat(a: 0)
|
||||
v2 = util.make_u32x4_repeat(a: 0)
|
||||
col0 = util.make_u16x8_repeat(a: 0)
|
||||
col1 = util.make_u16x8_repeat(a: 0)
|
||||
col2 = util.make_u16x8_repeat(a: 0)
|
||||
col3 = util.make_u16x8_repeat(a: 0)
|
||||
// Zero-initialize some u32×4 vectors associated with the two state
|
||||
// variables s1 and s2. The iterate loop accumulates four parallel u32
|
||||
// sums in each vector. A post-iterate step merges the four u32 sums
|
||||
// into a single u32 sum.
|
||||
v1 = util.make_u32x4_repeat(a: 0)
|
||||
v2 = util.make_u32x4_repeat(a: 0)
|
||||
col0 = util.make_u16x8_repeat(a: 0)
|
||||
col1 = util.make_u16x8_repeat(a: 0)
|
||||
col2 = util.make_u16x8_repeat(a: 0)
|
||||
col3 = util.make_u16x8_repeat(a: 0)
|
||||
|
||||
// The inner loop.
|
||||
iterate (p = args.x)(length: 32, advance: 32, unroll: 1) {
|
||||
// Split the 32-byte p into left and right halves. NEON works with
|
||||
// 16-byte registers.
|
||||
//
|
||||
// Let p__left = [u8×16: p00, p01, p02, ..., p15]
|
||||
// Let p_right = [u8×16: p16, p17, p18, ..., p31]
|
||||
p__left = util.make_u8x16_slice128(a: p[.. 16])
|
||||
p_right = util.make_u8x16_slice128(a: p[16 .. 32])
|
||||
// The inner loop.
|
||||
iterate (p = args.x)(length: 32, advance: 32, unroll: 1) {
|
||||
// Split the 32-byte p into left and right halves. NEON works with
|
||||
// 16-byte registers.
|
||||
//
|
||||
// Let p__left = [u8×16: p00, p01, p02, ..., p15]
|
||||
// Let p_right = [u8×16: p16, p17, p18, ..., p31]
|
||||
p__left = util.make_u8x16_slice128(a: p[.. 16])
|
||||
p_right = util.make_u8x16_slice128(a: p[16 .. 32])
|
||||
|
||||
// For v2j, we need to calculate the sums of the s1j terms for each
|
||||
// of p's 32 elements. This is simply 32 times the same number,
|
||||
// that number being the sum of v1's four u32 accumulators. We add
|
||||
// v1 now and multiply by 32 later, outside the inner loop.
|
||||
v2 = v2.vaddq_u32(b: v1)
|
||||
// For v2j, we need to calculate the sums of the s1j terms for each
|
||||
// of p's 32 elements. This is simply 32 times the same number,
|
||||
// that number being the sum of v1's four u32 accumulators. We add
|
||||
// v1 now and multiply by 32 later, outside the inner loop.
|
||||
v2 = v2.vaddq_u32(b: v1)
|
||||
|
||||
// For v1, we need to add the elements of p.
|
||||
//
|
||||
// p__left.vpaddlq_u8() is:
|
||||
// [u16×8: p00 + p01, p02 + p03, ..., p14 + p15]
|
||||
//
|
||||
// Combining (vpadalq_u8) that with p_right gives:
|
||||
// [u16×8: p00 + p01 + p16 + p17,
|
||||
// p02 + p03 + p18 + p19,
|
||||
// ...
|
||||
// p14 + p15 + p30 + p31]
|
||||
//
|
||||
// Pair-wise summing (and widening) again sets v1 to:
|
||||
// [u32×4: Σ (p00 + p01 + p02 + p03 + p16 + p17 + p18 + p19),
|
||||
// Σ (p04 + p05 + p06 + p07 + p20 + p21 + p22 + p23),
|
||||
// ...
|
||||
// Σ (p12 + p13 + p14 + p15 + p28 + p29 + p30 + p31)]
|
||||
v1 = v1.vpadalq_u16(b: p__left.vpaddlq_u8().vpadalq_u8(b: p_right))
|
||||
// For v1, we need to add the elements of p.
|
||||
//
|
||||
// p__left.vpaddlq_u8() is:
|
||||
// [u16×8: p00 + p01, p02 + p03, ..., p14 + p15]
|
||||
//
|
||||
// Combining (vpadalq_u8) that with p_right gives:
|
||||
// [u16×8: p00 + p01 + p16 + p17,
|
||||
// p02 + p03 + p18 + p19,
|
||||
// ...
|
||||
// p14 + p15 + p30 + p31]
|
||||
//
|
||||
// Pair-wise summing (and widening) again sets v1 to:
|
||||
// [u32×4: Σ (p00 + p01 + p02 + p03 + p16 + p17 + p18 + p19),
|
||||
// Σ (p04 + p05 + p06 + p07 + p20 + p21 + p22 + p23),
|
||||
// ...
|
||||
// Σ (p12 + p13 + p14 + p15 + p28 + p29 + p30 + p31)]
|
||||
v1 = v1.vpadalq_u16(b: p__left.vpaddlq_u8().vpadalq_u8(b: p_right))
|
||||
|
||||
// Accumulate column sums:
|
||||
// col0 = [u16×8: Σ p00, Σ p01, ..., Σ p07]
|
||||
// col1 = [u16×8: Σ p08, Σ p09, ..., Σ p15]
|
||||
// col2 = [u16×8: Σ p16, Σ p17, ..., Σ p23]
|
||||
// col3 = [u16×8: Σ p24, Σ p25, ..., Σ p31]
|
||||
col0 = col0.vaddw_u8(b: p__left.vget_low_u8())
|
||||
col1 = col1.vaddw_u8(b: p__left.vget_high_u8())
|
||||
col2 = col2.vaddw_u8(b: p_right.vget_low_u8())
|
||||
col3 = col3.vaddw_u8(b: p_right.vget_high_u8())
|
||||
}
|
||||
// Accumulate column sums:
|
||||
// col0 = [u16×8: Σ p00, Σ p01, ..., Σ p07]
|
||||
// col1 = [u16×8: Σ p08, Σ p09, ..., Σ p15]
|
||||
// col2 = [u16×8: Σ p16, Σ p17, ..., Σ p23]
|
||||
// col3 = [u16×8: Σ p24, Σ p25, ..., Σ p31]
|
||||
col0 = col0.vaddw_u8(b: p__left.vget_low_u8())
|
||||
col1 = col1.vaddw_u8(b: p__left.vget_high_u8())
|
||||
col2 = col2.vaddw_u8(b: p_right.vget_low_u8())
|
||||
col3 = col3.vaddw_u8(b: p_right.vget_high_u8())
|
||||
}
|
||||
|
||||
// Multiply v2j's four u32 elements each by 32, alluded to earlier.
|
||||
v2 = v2.vshlq_n_u32(b: 5)
|
||||
// Multiply v2j's four u32 elements each by 32, alluded to earlier.
|
||||
v2 = v2.vshlq_n_u32(b: 5)
|
||||
|
||||
// Add the v2k contributions in eight u32×4 vectors:
|
||||
// [u32×4: 0x20 * (Σ p00), 0x1F * (Σ p01), ..., 0x1D * (Σ p03)]
|
||||
// [u32×4: 0x1C * (Σ p04), 0x1B * (Σ p05), ..., 0x19 * (Σ p07)]
|
||||
// ...
|
||||
// [u32×4: 0x04 * (Σ p28), 0x03 * (Σ p29), ..., 0x01 * (Σ p31)]
|
||||
v2 = v2.vmlal_u16(
|
||||
b: col0.vget_low_u16(),
|
||||
c: util.make_u16x4_multiple(a00: 0x20, a01: 0x1F, a02: 0x1E, a03: 0x1D))
|
||||
v2 = v2.vmlal_u16(
|
||||
b: col0.vget_high_u16(),
|
||||
c: util.make_u16x4_multiple(a00: 0x1C, a01: 0x1B, a02: 0x1A, a03: 0x19))
|
||||
v2 = v2.vmlal_u16(
|
||||
b: col1.vget_low_u16(),
|
||||
c: util.make_u16x4_multiple(a00: 0x18, a01: 0x17, a02: 0x16, a03: 0x15))
|
||||
v2 = v2.vmlal_u16(
|
||||
b: col1.vget_high_u16(),
|
||||
c: util.make_u16x4_multiple(a00: 0x14, a01: 0x13, a02: 0x12, a03: 0x11))
|
||||
v2 = v2.vmlal_u16(
|
||||
b: col2.vget_low_u16(),
|
||||
c: util.make_u16x4_multiple(a00: 0x10, a01: 0x0F, a02: 0x0E, a03: 0x0D))
|
||||
v2 = v2.vmlal_u16(
|
||||
b: col2.vget_high_u16(),
|
||||
c: util.make_u16x4_multiple(a00: 0x0C, a01: 0x0B, a02: 0x0A, a03: 0x09))
|
||||
v2 = v2.vmlal_u16(
|
||||
b: col3.vget_low_u16(),
|
||||
c: util.make_u16x4_multiple(a00: 0x08, a01: 0x07, a02: 0x06, a03: 0x05))
|
||||
v2 = v2.vmlal_u16(
|
||||
b: col3.vget_high_u16(),
|
||||
c: util.make_u16x4_multiple(a00: 0x04, a01: 0x03, a02: 0x02, a03: 0x01))
|
||||
// Add the v2k contributions in eight u32×4 vectors:
|
||||
// [u32×4: 0x20 * (Σ p00), 0x1F * (Σ p01), ..., 0x1D * (Σ p03)]
|
||||
// [u32×4: 0x1C * (Σ p04), 0x1B * (Σ p05), ..., 0x19 * (Σ p07)]
|
||||
// ...
|
||||
// [u32×4: 0x04 * (Σ p28), 0x03 * (Σ p29), ..., 0x01 * (Σ p31)]
|
||||
v2 = v2.vmlal_u16(
|
||||
b: col0.vget_low_u16(),
|
||||
c: util.make_u16x4_multiple(a00: 0x20, a01: 0x1F, a02: 0x1E, a03: 0x1D))
|
||||
v2 = v2.vmlal_u16(
|
||||
b: col0.vget_high_u16(),
|
||||
c: util.make_u16x4_multiple(a00: 0x1C, a01: 0x1B, a02: 0x1A, a03: 0x19))
|
||||
v2 = v2.vmlal_u16(
|
||||
b: col1.vget_low_u16(),
|
||||
c: util.make_u16x4_multiple(a00: 0x18, a01: 0x17, a02: 0x16, a03: 0x15))
|
||||
v2 = v2.vmlal_u16(
|
||||
b: col1.vget_high_u16(),
|
||||
c: util.make_u16x4_multiple(a00: 0x14, a01: 0x13, a02: 0x12, a03: 0x11))
|
||||
v2 = v2.vmlal_u16(
|
||||
b: col2.vget_low_u16(),
|
||||
c: util.make_u16x4_multiple(a00: 0x10, a01: 0x0F, a02: 0x0E, a03: 0x0D))
|
||||
v2 = v2.vmlal_u16(
|
||||
b: col2.vget_high_u16(),
|
||||
c: util.make_u16x4_multiple(a00: 0x0C, a01: 0x0B, a02: 0x0A, a03: 0x09))
|
||||
v2 = v2.vmlal_u16(
|
||||
b: col3.vget_low_u16(),
|
||||
c: util.make_u16x4_multiple(a00: 0x08, a01: 0x07, a02: 0x06, a03: 0x05))
|
||||
v2 = v2.vmlal_u16(
|
||||
b: col3.vget_high_u16(),
|
||||
c: util.make_u16x4_multiple(a00: 0x04, a01: 0x03, a02: 0x02, a03: 0x01))
|
||||
|
||||
// Merge the four parallel u32 sums (v1) into the single u32 sum (s1)
|
||||
// and ditto for v2. Starting with [u32×4: xx_0, xx_1, xx_2, xx_3]:
|
||||
// sum1 = [u32×2: v1_0 + v1_1, v1_2 + v1_3]
|
||||
// sum2 = [u32×2: v2_0 + v2_1, v2_2 + v2_3]
|
||||
// sum12 = [u32×2: v1_0 + v1_1 + v1_2 + v1_3,
|
||||
// v2_0 + v2_1 + v2_2 + v2_3]
|
||||
sum1 = v1.vget_low_u32().vpadd_u32(b: v1.vget_high_u32())
|
||||
sum2 = v2.vget_low_u32().vpadd_u32(b: v2.vget_high_u32())
|
||||
sum12 = sum1.vpadd_u32(b: sum2)
|
||||
s1 ~mod+= sum12.vget_lane_u32(b: 0)
|
||||
s2 ~mod+= sum12.vget_lane_u32(b: 1)
|
||||
// Merge the four parallel u32 sums (v1) into the single u32 sum (s1)
|
||||
// and ditto for v2. Starting with [u32×4: xx_0, xx_1, xx_2, xx_3]:
|
||||
// sum1 = [u32×2: v1_0 + v1_1, v1_2 + v1_3]
|
||||
// sum2 = [u32×2: v2_0 + v2_1, v2_2 + v2_3]
|
||||
// sum12 = [u32×2: v1_0 + v1_1 + v1_2 + v1_3,
|
||||
// v2_0 + v2_1 + v2_2 + v2_3]
|
||||
sum1 = v1.vget_low_u32().vpadd_u32(b: v1.vget_high_u32())
|
||||
sum2 = v2.vget_low_u32().vpadd_u32(b: v2.vget_high_u32())
|
||||
sum12 = sum1.vpadd_u32(b: sum2)
|
||||
s1 ~mod+= sum12.vget_lane_u32(b: 0)
|
||||
s2 ~mod+= sum12.vget_lane_u32(b: 1)
|
||||
|
||||
// Handle the tail of args.x that wasn't a complete 32-byte chunk.
|
||||
tail_index = args.x.length() & 0xFFFF_FFFF_FFFF_FFE0 // And-not 32.
|
||||
if tail_index < args.x.length() {
|
||||
iterate (p = args.x[tail_index ..])(length: 1, advance: 1, unroll: 1) {
|
||||
s1 ~mod+= p[0] as base.u32
|
||||
s2 ~mod+= s1
|
||||
}
|
||||
}
|
||||
// Handle the tail of args.x that wasn't a complete 32-byte chunk.
|
||||
tail_index = args.x.length() & 0xFFFF_FFFF_FFFF_FFE0 // And-not 32.
|
||||
if tail_index < args.x.length() {
|
||||
iterate (p = args.x[tail_index ..])(length: 1, advance: 1, unroll: 1) {
|
||||
s1 ~mod+= p[0] as base.u32
|
||||
s2 ~mod+= s1
|
||||
}
|
||||
}
|
||||
|
||||
// The rest of this function is the same as the non-SIMD version.
|
||||
s1 %= 65521
|
||||
s2 %= 65521
|
||||
args.x = remaining
|
||||
} endwhile
|
||||
this.state = ((s2 & 0xFFFF) << 16) | (s1 & 0xFFFF)
|
||||
// The rest of this function is the same as the non-SIMD version.
|
||||
s1 %= 65521
|
||||
s2 %= 65521
|
||||
args.x = remaining
|
||||
} endwhile
|
||||
this.state = ((s2 & 0xFFFF) << 16) | (s1 & 0xFFFF)
|
||||
}
|
||||
@@ -13,169 +13,169 @@
|
||||
// limitations under the License.
|
||||
|
||||
pri func hasher.up_x86_sse42!(x: slice base.u8),
|
||||
choose cpu_arch >= x86_sse42,
|
||||
choose cpu_arch >= x86_sse42,
|
||||
{
|
||||
// These variables are the same as the non-SIMD version.
|
||||
var s1 : base.u32
|
||||
var s2 : base.u32
|
||||
var remaining : slice base.u8
|
||||
var p : slice base.u8
|
||||
// These variables are the same as the non-SIMD version.
|
||||
var s1 : base.u32
|
||||
var s2 : base.u32
|
||||
var remaining : slice base.u8
|
||||
var p : slice base.u8
|
||||
|
||||
// The remaining variables are specific to the SIMD version.
|
||||
// The remaining variables are specific to the SIMD version.
|
||||
|
||||
var util : base.x86_sse42_utility
|
||||
var zeroes : base.x86_m128i
|
||||
var ones : base.x86_m128i
|
||||
var weights__left : base.x86_m128i
|
||||
var weights_right : base.x86_m128i
|
||||
var q__left : base.x86_m128i
|
||||
var q_right : base.x86_m128i
|
||||
var v1 : base.x86_m128i
|
||||
var v2 : base.x86_m128i
|
||||
var v2j : base.x86_m128i
|
||||
var v2k : base.x86_m128i
|
||||
var util : base.x86_sse42_utility
|
||||
var zeroes : base.x86_m128i
|
||||
var ones : base.x86_m128i
|
||||
var weights__left : base.x86_m128i
|
||||
var weights_right : base.x86_m128i
|
||||
var q__left : base.x86_m128i
|
||||
var q_right : base.x86_m128i
|
||||
var v1 : base.x86_m128i
|
||||
var v2 : base.x86_m128i
|
||||
var v2j : base.x86_m128i
|
||||
var v2k : base.x86_m128i
|
||||
|
||||
var num_iterate_bytes : base.u32
|
||||
var tail_index : base.u64
|
||||
var num_iterate_bytes : base.u32
|
||||
var tail_index : base.u64
|
||||
|
||||
// zeroes and ones are uniform u16×8 vectors.
|
||||
zeroes = util.make_m128i_repeat_u16(a: 0)
|
||||
ones = util.make_m128i_repeat_u16(a: 1)
|
||||
// zeroes and ones are uniform u16×8 vectors.
|
||||
zeroes = util.make_m128i_repeat_u16(a: 0)
|
||||
ones = util.make_m128i_repeat_u16(a: 1)
|
||||
|
||||
// weights__left and weights_right form the sequence 32, 31, 30, ..., 1.
|
||||
weights__left = util.make_m128i_multiple_u8(
|
||||
a00: 0x20, a01: 0x1F, a02: 0x1E, a03: 0x1D,
|
||||
a04: 0x1C, a05: 0x1B, a06: 0x1A, a07: 0x19,
|
||||
a08: 0x18, a09: 0x17, a10: 0x16, a11: 0x15,
|
||||
a12: 0x14, a13: 0x13, a14: 0x12, a15: 0x11)
|
||||
weights_right = util.make_m128i_multiple_u8(
|
||||
a00: 0x10, a01: 0x0F, a02: 0x0E, a03: 0x0D,
|
||||
a04: 0x0C, a05: 0x0B, a06: 0x0A, a07: 0x09,
|
||||
a08: 0x08, a09: 0x07, a10: 0x06, a11: 0x05,
|
||||
a12: 0x04, a13: 0x03, a14: 0x02, a15: 0x01)
|
||||
// weights__left and weights_right form the sequence 32, 31, 30, ..., 1.
|
||||
weights__left = util.make_m128i_multiple_u8(
|
||||
a00: 0x20, a01: 0x1F, a02: 0x1E, a03: 0x1D,
|
||||
a04: 0x1C, a05: 0x1B, a06: 0x1A, a07: 0x19,
|
||||
a08: 0x18, a09: 0x17, a10: 0x16, a11: 0x15,
|
||||
a12: 0x14, a13: 0x13, a14: 0x12, a15: 0x11)
|
||||
weights_right = util.make_m128i_multiple_u8(
|
||||
a00: 0x10, a01: 0x0F, a02: 0x0E, a03: 0x0D,
|
||||
a04: 0x0C, a05: 0x0B, a06: 0x0A, a07: 0x09,
|
||||
a08: 0x08, a09: 0x07, a10: 0x06, a11: 0x05,
|
||||
a12: 0x04, a13: 0x03, a14: 0x02, a15: 0x01)
|
||||
|
||||
// Decompose this.state.
|
||||
s1 = this.state.low_bits(n: 16)
|
||||
s2 = this.state.high_bits(n: 16)
|
||||
// Decompose this.state.
|
||||
s1 = this.state.low_bits(n: 16)
|
||||
s2 = this.state.high_bits(n: 16)
|
||||
|
||||
// Just like the non-SIMD version, loop over args.x up to almost-5552 bytes
|
||||
// at a time. The slightly smaller 5536 is the largest multiple of 32 less
|
||||
// than non-SIMD's 5552.
|
||||
while args.x.length() > 0 {
|
||||
remaining = args.x[.. 0]
|
||||
if args.x.length() > 5536 {
|
||||
remaining = args.x[5536 ..]
|
||||
args.x = args.x[.. 5536]
|
||||
}
|
||||
// Just like the non-SIMD version, loop over args.x up to almost-5552 bytes
|
||||
// at a time. The slightly smaller 5536 is the largest multiple of 32 less
|
||||
// than non-SIMD's 5552.
|
||||
while args.x.length() > 0 {
|
||||
remaining = args.x[.. 0]
|
||||
if args.x.length() > 5536 {
|
||||
remaining = args.x[5536 ..]
|
||||
args.x = args.x[.. 5536]
|
||||
}
|
||||
|
||||
// The s1 state is the sum of the input bytes and the s2 state is the
|
||||
// sum of the s1 state at each 1-byte step. Inside the iterate loop
|
||||
// below, but starting fresh at each outer while loop iteration, s1
|
||||
// consists of three parts (called s1i, s1j and s1k):
|
||||
// - s1i: the initial value, before any 32-byte iterations.
|
||||
// - s1j: the total contribution from previous 32-byte iterations.
|
||||
// - s1k: the contribution due to the current 32-byte iteration.
|
||||
//
|
||||
// The upcoming iterate loop (at 32 bytes per iteration) encompasses
|
||||
// num_iterate_bytes 1-byte steps. We hoist the total s1i contribution,
|
||||
// (s1i * num_iterate_bytes) out here.
|
||||
num_iterate_bytes = (args.x.length() & 0xFFFF_FFE0) as base.u32
|
||||
s2 ~mod+= (s1 ~mod* num_iterate_bytes)
|
||||
// The s1 state is the sum of the input bytes and the s2 state is the
|
||||
// sum of the s1 state at each 1-byte step. Inside the iterate loop
|
||||
// below, but starting fresh at each outer while loop iteration, s1
|
||||
// consists of three parts (called s1i, s1j and s1k):
|
||||
// - s1i: the initial value, before any 32-byte iterations.
|
||||
// - s1j: the total contribution from previous 32-byte iterations.
|
||||
// - s1k: the contribution due to the current 32-byte iteration.
|
||||
//
|
||||
// The upcoming iterate loop (at 32 bytes per iteration) encompasses
|
||||
// num_iterate_bytes 1-byte steps. We hoist the total s1i contribution,
|
||||
// (s1i * num_iterate_bytes) out here.
|
||||
num_iterate_bytes = (args.x.length() & 0xFFFF_FFE0) as base.u32
|
||||
s2 ~mod+= (s1 ~mod* num_iterate_bytes)
|
||||
|
||||
// Zero-initialize some u32×4 vectors associated with the two state
|
||||
// variables s1 and s2. The iterate loop accumulates four parallel u32
|
||||
// sums in each vector. A post-iterate step merges the four u32 sums
|
||||
// into a single u32 sum.
|
||||
v1 = util.make_m128i_zeroes()
|
||||
v2j = util.make_m128i_zeroes()
|
||||
v2k = util.make_m128i_zeroes()
|
||||
// Zero-initialize some u32×4 vectors associated with the two state
|
||||
// variables s1 and s2. The iterate loop accumulates four parallel u32
|
||||
// sums in each vector. A post-iterate step merges the four u32 sums
|
||||
// into a single u32 sum.
|
||||
v1 = util.make_m128i_zeroes()
|
||||
v2j = util.make_m128i_zeroes()
|
||||
v2k = util.make_m128i_zeroes()
|
||||
|
||||
// The inner loop.
|
||||
iterate (p = args.x)(length: 32, advance: 32, unroll: 1) {
|
||||
// SSE4.2 works with 16-byte registers. Split the 32-byte p into
|
||||
// left and right halves.
|
||||
//
|
||||
// Let q__left = [u8×16: p00, p01, p02, ..., p15]
|
||||
// Let q_right = [u8×16: p16, p17, p18, ..., p31]
|
||||
q__left = util.make_m128i_slice128(a: p[.. 16])
|
||||
q_right = util.make_m128i_slice128(a: p[16 .. 32])
|
||||
// The inner loop.
|
||||
iterate (p = args.x)(length: 32, advance: 32, unroll: 1) {
|
||||
// SSE4.2 works with 16-byte registers. Split the 32-byte p into
|
||||
// left and right halves.
|
||||
//
|
||||
// Let q__left = [u8×16: p00, p01, p02, ..., p15]
|
||||
// Let q_right = [u8×16: p16, p17, p18, ..., p31]
|
||||
q__left = util.make_m128i_slice128(a: p[.. 16])
|
||||
q_right = util.make_m128i_slice128(a: p[16 .. 32])
|
||||
|
||||
// For v2j, we need to calculate the sums of the s1j terms for each
|
||||
// of p's 32 elements. This is simply 32 times the same number,
|
||||
// that number being the sum of v1's four u32 accumulators. We add
|
||||
// v1 now and multiply by 32 later, outside the inner loop.
|
||||
v2j = v2j._mm_add_epi32(b: v1)
|
||||
// For v2j, we need to calculate the sums of the s1j terms for each
|
||||
// of p's 32 elements. This is simply 32 times the same number,
|
||||
// that number being the sum of v1's four u32 accumulators. We add
|
||||
// v1 now and multiply by 32 later, outside the inner loop.
|
||||
v2j = v2j._mm_add_epi32(b: v1)
|
||||
|
||||
// For v1, we need to add the elements of p. Computing the sum of
|
||||
// absolute differences (_mm_sad_epu8) with zero just sums the
|
||||
// elements. q__left._mm_sad_epu8(b: zeroes) equals
|
||||
// [u64×2: p00 + p01 + ... + p07, p08 + p09 + ... + p15]
|
||||
// This is equivalent (little-endian) to:
|
||||
// [u32×4: p00 + p01 + ... + p07, 0, p08 + p09 + ... + p15, 0]
|
||||
// We accumulate those "sum of q__left's elements" in v1, and ditto
|
||||
// for q_right's elements.
|
||||
v1 = v1._mm_add_epi32(b: q__left._mm_sad_epu8(b: zeroes))
|
||||
v1 = v1._mm_add_epi32(b: q_right._mm_sad_epu8(b: zeroes))
|
||||
// For v1, we need to add the elements of p. Computing the sum of
|
||||
// absolute differences (_mm_sad_epu8) with zero just sums the
|
||||
// elements. q__left._mm_sad_epu8(b: zeroes) equals
|
||||
// [u64×2: p00 + p01 + ... + p07, p08 + p09 + ... + p15]
|
||||
// This is equivalent (little-endian) to:
|
||||
// [u32×4: p00 + p01 + ... + p07, 0, p08 + p09 + ... + p15, 0]
|
||||
// We accumulate those "sum of q__left's elements" in v1, and ditto
|
||||
// for q_right's elements.
|
||||
v1 = v1._mm_add_epi32(b: q__left._mm_sad_epu8(b: zeroes))
|
||||
v1 = v1._mm_add_epi32(b: q_right._mm_sad_epu8(b: zeroes))
|
||||
|
||||
// For v2k, we need to calculate a weighted sum: ((32 * p00) + (31
|
||||
// * p01) + (30 * p02) + ... + (1 * p31)), which splits naturally
|
||||
// into weighted sums of the left half and of the right half.
|
||||
//
|
||||
// The _mm_maddubs_epi16 call (vertically multiply u8 columns and
|
||||
// then horizontally sum u16 pairs) with the left half produces:
|
||||
// [u16×8: ((32*p00)+(31*p01)),
|
||||
// ((30*p02)+(29*p03)),
|
||||
// ...
|
||||
// ((18*p14)+(17*p15))]
|
||||
//
|
||||
// The ones._mm_madd_epi16(b: etc) call is a multiply-add (note
|
||||
// that it's "madd" not "add"). Multiplying by 1 is a no-op, so
|
||||
// this sums u16 pairs to produce u32 values:
|
||||
// [u32×4: ((32*p00)+(31*p01)+(30*p02)+(29*p03)),
|
||||
// ((28*p04)+(27*p05)+(26*p06)+(25*p07)),
|
||||
// ...
|
||||
// ((20*p12)+(19*p13)+(18*p14)+(17*p15))]
|
||||
//
|
||||
// Ditto again for q_right's elements.
|
||||
v2k = v2k._mm_add_epi32(b: ones._mm_madd_epi16(b:
|
||||
q__left._mm_maddubs_epi16(b: weights__left)))
|
||||
v2k = v2k._mm_add_epi32(b: ones._mm_madd_epi16(b:
|
||||
q_right._mm_maddubs_epi16(b: weights_right)))
|
||||
}
|
||||
// For v2k, we need to calculate a weighted sum: ((32 * p00) + (31
|
||||
// * p01) + (30 * p02) + ... + (1 * p31)), which splits naturally
|
||||
// into weighted sums of the left half and of the right half.
|
||||
//
|
||||
// The _mm_maddubs_epi16 call (vertically multiply u8 columns and
|
||||
// then horizontally sum u16 pairs) with the left half produces:
|
||||
// [u16×8: ((32*p00)+(31*p01)),
|
||||
// ((30*p02)+(29*p03)),
|
||||
// ...
|
||||
// ((18*p14)+(17*p15))]
|
||||
//
|
||||
// The ones._mm_madd_epi16(b: etc) call is a multiply-add (note
|
||||
// that it's "madd" not "add"). Multiplying by 1 is a no-op, so
|
||||
// this sums u16 pairs to produce u32 values:
|
||||
// [u32×4: ((32*p00)+(31*p01)+(30*p02)+(29*p03)),
|
||||
// ((28*p04)+(27*p05)+(26*p06)+(25*p07)),
|
||||
// ...
|
||||
// ((20*p12)+(19*p13)+(18*p14)+(17*p15))]
|
||||
//
|
||||
// Ditto again for q_right's elements.
|
||||
v2k = v2k._mm_add_epi32(b: ones._mm_madd_epi16(b:
|
||||
q__left._mm_maddubs_epi16(b: weights__left)))
|
||||
v2k = v2k._mm_add_epi32(b: ones._mm_madd_epi16(b:
|
||||
q_right._mm_maddubs_epi16(b: weights_right)))
|
||||
}
|
||||
|
||||
// Merge the four parallel u32 sums (v1) into the single u32 sum (s1).
|
||||
// Starting with a u32×4 vector [x0, x1, x2, x3]:
|
||||
// - shuffling with 0b1011_0001 gives [x1, x0, x3, x2].
|
||||
// - adding gives [x0+x1, x0+x1, x2+x3, x2+x3].
|
||||
// - shuffling with 0b0100_1110 gives [x2+x3, x2+x3, x0+x1, x0+x1].
|
||||
// - adding gives [x0+x1+x2+x3, ditto, ditto, ditto].
|
||||
// The truncate_u32 call extracts the first u32: x0+x1+x2+x3.
|
||||
v1 = v1._mm_add_epi32(b: v1._mm_shuffle_epi32(imm8: 0b1011_0001))
|
||||
v1 = v1._mm_add_epi32(b: v1._mm_shuffle_epi32(imm8: 0b0100_1110))
|
||||
s1 ~mod+= v1.truncate_u32()
|
||||
// Merge the four parallel u32 sums (v1) into the single u32 sum (s1).
|
||||
// Starting with a u32×4 vector [x0, x1, x2, x3]:
|
||||
// - shuffling with 0b1011_0001 gives [x1, x0, x3, x2].
|
||||
// - adding gives [x0+x1, x0+x1, x2+x3, x2+x3].
|
||||
// - shuffling with 0b0100_1110 gives [x2+x3, x2+x3, x0+x1, x0+x1].
|
||||
// - adding gives [x0+x1+x2+x3, ditto, ditto, ditto].
|
||||
// The truncate_u32 call extracts the first u32: x0+x1+x2+x3.
|
||||
v1 = v1._mm_add_epi32(b: v1._mm_shuffle_epi32(imm8: 0b1011_0001))
|
||||
v1 = v1._mm_add_epi32(b: v1._mm_shuffle_epi32(imm8: 0b0100_1110))
|
||||
s1 ~mod+= v1.truncate_u32()
|
||||
|
||||
// Combine v2j and v2k. The slli (shift logical left immediate) by 5
|
||||
// multiplies v2j's four u32 elements each by 32, alluded to earlier.
|
||||
v2 = v2k._mm_add_epi32(b: v2j._mm_slli_epi32(imm8: 5))
|
||||
// Combine v2j and v2k. The slli (shift logical left immediate) by 5
|
||||
// multiplies v2j's four u32 elements each by 32, alluded to earlier.
|
||||
v2 = v2k._mm_add_epi32(b: v2j._mm_slli_epi32(imm8: 5))
|
||||
|
||||
// Similarly merge v2 (a u32×4 vector) into s2 (a u32 scalar).
|
||||
v2 = v2._mm_add_epi32(b: v2._mm_shuffle_epi32(imm8: 0b1011_0001))
|
||||
v2 = v2._mm_add_epi32(b: v2._mm_shuffle_epi32(imm8: 0b0100_1110))
|
||||
s2 ~mod+= v2.truncate_u32()
|
||||
// Similarly merge v2 (a u32×4 vector) into s2 (a u32 scalar).
|
||||
v2 = v2._mm_add_epi32(b: v2._mm_shuffle_epi32(imm8: 0b1011_0001))
|
||||
v2 = v2._mm_add_epi32(b: v2._mm_shuffle_epi32(imm8: 0b0100_1110))
|
||||
s2 ~mod+= v2.truncate_u32()
|
||||
|
||||
// Handle the tail of args.x that wasn't a complete 32-byte chunk.
|
||||
tail_index = args.x.length() & 0xFFFF_FFFF_FFFF_FFE0 // And-not 32.
|
||||
if tail_index < args.x.length() {
|
||||
iterate (p = args.x[tail_index ..])(length: 1, advance: 1, unroll: 1) {
|
||||
s1 ~mod+= p[0] as base.u32
|
||||
s2 ~mod+= s1
|
||||
}
|
||||
}
|
||||
// Handle the tail of args.x that wasn't a complete 32-byte chunk.
|
||||
tail_index = args.x.length() & 0xFFFF_FFFF_FFFF_FFE0 // And-not 32.
|
||||
if tail_index < args.x.length() {
|
||||
iterate (p = args.x[tail_index ..])(length: 1, advance: 1, unroll: 1) {
|
||||
s1 ~mod+= p[0] as base.u32
|
||||
s2 ~mod+= s1
|
||||
}
|
||||
}
|
||||
|
||||
// The rest of this function is the same as the non-SIMD version.
|
||||
s1 %= 65521
|
||||
s2 %= 65521
|
||||
args.x = remaining
|
||||
} endwhile
|
||||
this.state = ((s2 & 0xFFFF) << 16) | (s1 & 0xFFFF)
|
||||
// The rest of this function is the same as the non-SIMD version.
|
||||
s1 %= 65521
|
||||
s2 %= 65521
|
||||
args.x = remaining
|
||||
} endwhile
|
||||
this.state = ((s2 & 0xFFFF) << 16) | (s1 & 0xFFFF)
|
||||
}
|
||||
+1106
-1106
@@ -31,569 +31,569 @@ pri const COMPRESSION_ALPHABITFIELDS : base.u32 = 6
|
||||
pri const COMPRESSION_LOW_BIT_DEPTH : base.u32 = 0x100
|
||||
|
||||
pub struct decoder? implements base.image_decoder(
|
||||
width : base.u32[..= 0x7FFF_FFFF],
|
||||
height : base.u32[..= 0x7FFF_FFFF],
|
||||
width : base.u32[..= 0x7FFF_FFFF],
|
||||
height : base.u32[..= 0x7FFF_FFFF],
|
||||
|
||||
// The call sequence state machine is discussed in
|
||||
// (/doc/std/image-decoders-call-sequence.md).
|
||||
call_sequence : base.u8,
|
||||
// The call sequence state machine is discussed in
|
||||
// (/doc/std/image-decoders-call-sequence.md).
|
||||
call_sequence : base.u8,
|
||||
|
||||
top_down : base.bool,
|
||||
pad_per_row : base.u32[..= 3],
|
||||
src_pixfmt : base.u32,
|
||||
top_down : base.bool,
|
||||
pad_per_row : base.u32[..= 3],
|
||||
src_pixfmt : base.u32,
|
||||
|
||||
io_redirect_fourcc : base.u32,
|
||||
io_redirect_pos : base.u64,
|
||||
io_redirect_fourcc : base.u32,
|
||||
io_redirect_pos : base.u64,
|
||||
|
||||
frame_config_io_position : base.u64,
|
||||
frame_config_io_position : base.u64,
|
||||
|
||||
bitmap_info_len : base.u32,
|
||||
padding : base.u32,
|
||||
bitmap_info_len : base.u32,
|
||||
padding : base.u32,
|
||||
|
||||
bits_per_pixel : base.u32[..= 0xFFFF],
|
||||
compression : base.u32,
|
||||
bits_per_pixel : base.u32[..= 0xFFFF],
|
||||
compression : base.u32,
|
||||
|
||||
// channel_etc's indexes are: B, G, R, A. This is in Wuffs' default order,
|
||||
// which isn't the RGBA order they're listed in the wire format.
|
||||
channel_masks : array[4] base.u32,
|
||||
channel_shifts : array[4] base.u8[..= 31],
|
||||
channel_num_bits : array[4] base.u8[..= 32],
|
||||
// channel_etc's indexes are: B, G, R, A. This is in Wuffs' default order,
|
||||
// which isn't the RGBA order they're listed in the wire format.
|
||||
channel_masks : array[4] base.u32,
|
||||
channel_shifts : array[4] base.u8[..= 31],
|
||||
channel_num_bits : array[4] base.u8[..= 32],
|
||||
|
||||
dst_x : base.u32,
|
||||
dst_y : base.u32,
|
||||
dst_y_inc : base.u32,
|
||||
dst_x : base.u32,
|
||||
dst_y : base.u32,
|
||||
dst_y_inc : base.u32,
|
||||
|
||||
pending_pad : base.u32[..= 3],
|
||||
pending_pad : base.u32[..= 3],
|
||||
|
||||
rle_state : base.u32,
|
||||
rle_length : base.u32[..= 0xFF],
|
||||
rle_delta_x : base.u8,
|
||||
rle_padded : base.bool,
|
||||
rle_state : base.u32,
|
||||
rle_length : base.u32[..= 0xFF],
|
||||
rle_delta_x : base.u8,
|
||||
rle_padded : base.bool,
|
||||
|
||||
swizzler : base.pixel_swizzler,
|
||||
util : base.utility,
|
||||
swizzler : base.pixel_swizzler,
|
||||
util : base.utility,
|
||||
)(
|
||||
// scratch is one of:
|
||||
// - 2048 bytes = 256 * (8 bytes per BGRA_NONPREMUL_4X16LE).
|
||||
// - 1024 bytes = 1024 * (1 byte per INDEXED__BGRA_BINARY), plus
|
||||
// 1024 bytes = 256 * (4 bytes per dst_palette entry).
|
||||
scratch : array[2048] base.u8,
|
||||
// scratch is one of:
|
||||
// - 2048 bytes = 256 * (8 bytes per BGRA_NONPREMUL_4X16LE).
|
||||
// - 1024 bytes = 1024 * (1 byte per INDEXED__BGRA_BINARY), plus
|
||||
// 1024 bytes = 256 * (4 bytes per dst_palette entry).
|
||||
scratch : array[2048] base.u8,
|
||||
|
||||
src_palette : array[4 * 256] base.u8,
|
||||
src_palette : array[4 * 256] base.u8,
|
||||
)
|
||||
|
||||
pub func decoder.set_quirk_enabled!(quirk: base.u32, enabled: base.bool) {
|
||||
}
|
||||
|
||||
pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_reader) {
|
||||
var status : base.status
|
||||
var status : base.status
|
||||
|
||||
while true {
|
||||
status =? this.do_decode_image_config?(dst: args.dst, src: args.src)
|
||||
if (status == base."$short read") and args.src.is_closed() {
|
||||
return "#truncated input"
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
while true {
|
||||
status =? this.do_decode_image_config?(dst: args.dst, src: args.src)
|
||||
if (status == base."$short read") and args.src.is_closed() {
|
||||
return "#truncated input"
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
}
|
||||
|
||||
pri func decoder.do_decode_image_config?(dst: nptr base.image_config, src: base.io_reader) {
|
||||
var magic : base.u32
|
||||
var width : base.u32
|
||||
var height : base.u32
|
||||
var planes : base.u32
|
||||
var dst_pixfmt : base.u32
|
||||
var byte_width : base.u32
|
||||
var magic : base.u32
|
||||
var width : base.u32
|
||||
var height : base.u32
|
||||
var planes : base.u32
|
||||
var dst_pixfmt : base.u32
|
||||
var byte_width : base.u32
|
||||
|
||||
if (this.call_sequence <> 0x00) or (this.io_redirect_fourcc == 1) {
|
||||
return base."#bad call sequence"
|
||||
} else if this.io_redirect_fourcc <> 0 {
|
||||
return base."@I/O redirect"
|
||||
}
|
||||
if (this.call_sequence <> 0x00) or (this.io_redirect_fourcc == 1) {
|
||||
return base."#bad call sequence"
|
||||
} else if this.io_redirect_fourcc <> 0 {
|
||||
return base."@I/O redirect"
|
||||
}
|
||||
|
||||
// Read the BITMAPFILEHEADER (14 bytes).
|
||||
// Read the BITMAPFILEHEADER (14 bytes).
|
||||
|
||||
magic = args.src.read_u16le_as_u32?()
|
||||
if magic <> 'BM'le {
|
||||
return "#bad header"
|
||||
}
|
||||
magic = args.src.read_u16le_as_u32?()
|
||||
if magic <> 'BM'le {
|
||||
return "#bad header"
|
||||
}
|
||||
|
||||
args.src.skip_u32?(n: 8)
|
||||
args.src.skip_u32?(n: 8)
|
||||
|
||||
this.padding = args.src.read_u32le?()
|
||||
if this.padding < 14 {
|
||||
return "#bad header"
|
||||
}
|
||||
this.padding -= 14
|
||||
this.io_redirect_pos = (this.padding as base.u64) ~sat+ args.src.position()
|
||||
this.padding = args.src.read_u32le?()
|
||||
if this.padding < 14 {
|
||||
return "#bad header"
|
||||
}
|
||||
this.padding -= 14
|
||||
this.io_redirect_pos = (this.padding as base.u64) ~sat+ args.src.position()
|
||||
|
||||
// Read the BITMAPINFOHEADER:
|
||||
// - OS/2 is 12, 16 or 64 bytes.
|
||||
// - Windows is 40, 108 or 124 bytes.
|
||||
// Read the BITMAPINFOHEADER:
|
||||
// - OS/2 is 12, 16 or 64 bytes.
|
||||
// - Windows is 40, 108 or 124 bytes.
|
||||
|
||||
this.bitmap_info_len = args.src.read_u32le?()
|
||||
if this.padding < this.bitmap_info_len {
|
||||
return "#bad header"
|
||||
}
|
||||
this.padding -= this.bitmap_info_len
|
||||
this.bitmap_info_len = args.src.read_u32le?()
|
||||
if this.padding < this.bitmap_info_len {
|
||||
return "#bad header"
|
||||
}
|
||||
this.padding -= this.bitmap_info_len
|
||||
|
||||
if this.bitmap_info_len == 12 {
|
||||
this.width = args.src.read_u16le_as_u32?()
|
||||
this.height = args.src.read_u16le_as_u32?()
|
||||
planes = args.src.read_u16le_as_u32?()
|
||||
if planes <> 1 {
|
||||
return "#unsupported BMP file"
|
||||
}
|
||||
this.bits_per_pixel = args.src.read_u16le_as_u32?()
|
||||
if this.bitmap_info_len == 12 {
|
||||
this.width = args.src.read_u16le_as_u32?()
|
||||
this.height = args.src.read_u16le_as_u32?()
|
||||
planes = args.src.read_u16le_as_u32?()
|
||||
if planes <> 1 {
|
||||
return "#unsupported BMP file"
|
||||
}
|
||||
this.bits_per_pixel = args.src.read_u16le_as_u32?()
|
||||
|
||||
} else if this.bitmap_info_len == 16 {
|
||||
width = args.src.read_u32le?()
|
||||
if width >= 0x8000_0000 {
|
||||
return "#bad header"
|
||||
}
|
||||
this.width = width
|
||||
height = args.src.read_u32le?()
|
||||
if height >= 0x8000_0000 {
|
||||
return "#bad header"
|
||||
}
|
||||
this.height = height
|
||||
planes = args.src.read_u16le_as_u32?()
|
||||
if planes <> 1 {
|
||||
return "#unsupported BMP file"
|
||||
}
|
||||
this.bits_per_pixel = args.src.read_u16le_as_u32?()
|
||||
} else if this.bitmap_info_len == 16 {
|
||||
width = args.src.read_u32le?()
|
||||
if width >= 0x8000_0000 {
|
||||
return "#bad header"
|
||||
}
|
||||
this.width = width
|
||||
height = args.src.read_u32le?()
|
||||
if height >= 0x8000_0000 {
|
||||
return "#bad header"
|
||||
}
|
||||
this.height = height
|
||||
planes = args.src.read_u16le_as_u32?()
|
||||
if planes <> 1 {
|
||||
return "#unsupported BMP file"
|
||||
}
|
||||
this.bits_per_pixel = args.src.read_u16le_as_u32?()
|
||||
|
||||
} else {
|
||||
width = args.src.read_u32le?()
|
||||
if width >= 0x8000_0000 {
|
||||
return "#bad header"
|
||||
}
|
||||
this.width = width
|
||||
} else {
|
||||
width = args.src.read_u32le?()
|
||||
if width >= 0x8000_0000 {
|
||||
return "#bad header"
|
||||
}
|
||||
this.width = width
|
||||
|
||||
height = args.src.read_u32le?()
|
||||
if height == 0x8000_0000 {
|
||||
return "#bad header"
|
||||
} else if height >= 0x8000_0000 {
|
||||
// The &0x7FFF_FFFF is redundant, but proves to the compiler that
|
||||
// the result is within this.height's refined bounds.
|
||||
this.height = (0 ~mod- height) & 0x7FFF_FFFF
|
||||
this.top_down = true
|
||||
} else {
|
||||
this.height = height
|
||||
}
|
||||
height = args.src.read_u32le?()
|
||||
if height == 0x8000_0000 {
|
||||
return "#bad header"
|
||||
} else if height >= 0x8000_0000 {
|
||||
// The &0x7FFF_FFFF is redundant, but proves to the compiler that
|
||||
// the result is within this.height's refined bounds.
|
||||
this.height = (0 ~mod- height) & 0x7FFF_FFFF
|
||||
this.top_down = true
|
||||
} else {
|
||||
this.height = height
|
||||
}
|
||||
|
||||
planes = args.src.read_u16le_as_u32?()
|
||||
if planes <> 1 {
|
||||
return "#unsupported BMP file"
|
||||
}
|
||||
planes = args.src.read_u16le_as_u32?()
|
||||
if planes <> 1 {
|
||||
return "#unsupported BMP file"
|
||||
}
|
||||
|
||||
this.bits_per_pixel = args.src.read_u16le_as_u32?()
|
||||
this.compression = args.src.read_u32le?()
|
||||
if this.bits_per_pixel == 0 {
|
||||
if this.compression == COMPRESSION_JPEG {
|
||||
this.io_redirect_fourcc = 'JPEG'be
|
||||
return base."@I/O redirect"
|
||||
} else if this.compression == COMPRESSION_PNG {
|
||||
this.io_redirect_fourcc = 'PNG 'be
|
||||
return base."@I/O redirect"
|
||||
}
|
||||
return "#unsupported BMP file"
|
||||
}
|
||||
this.bits_per_pixel = args.src.read_u16le_as_u32?()
|
||||
this.compression = args.src.read_u32le?()
|
||||
if this.bits_per_pixel == 0 {
|
||||
if this.compression == COMPRESSION_JPEG {
|
||||
this.io_redirect_fourcc = 'JPEG'be
|
||||
return base."@I/O redirect"
|
||||
} else if this.compression == COMPRESSION_PNG {
|
||||
this.io_redirect_fourcc = 'PNG 'be
|
||||
return base."@I/O redirect"
|
||||
}
|
||||
return "#unsupported BMP file"
|
||||
}
|
||||
|
||||
// We've already read 20 bytes from the BITMAPINFOHEADER: size (4),
|
||||
// width (4), height (4), planes (2), bpp (2), compression (4). Skip
|
||||
// the rest of the version 3 BITMAPINFOHEADER (whose total size is 40).
|
||||
args.src.skip_u32?(n: 40 - 20)
|
||||
// We've already read 20 bytes from the BITMAPINFOHEADER: size (4),
|
||||
// width (4), height (4), planes (2), bpp (2), compression (4). Skip
|
||||
// the rest of the version 3 BITMAPINFOHEADER (whose total size is 40).
|
||||
args.src.skip_u32?(n: 40 - 20)
|
||||
|
||||
if this.bitmap_info_len == 40 {
|
||||
if this.bits_per_pixel >= 16 {
|
||||
// It's poorly documented, but "length 40" data can be silently
|
||||
// augmented with RGB or RGBA channel_masks (12 or 16 bytes).
|
||||
if this.padding >= 16 {
|
||||
this.bitmap_info_len = 56
|
||||
this.padding -= 16
|
||||
} else if this.padding >= 12 {
|
||||
this.bitmap_info_len = 52
|
||||
this.padding -= 12
|
||||
}
|
||||
}
|
||||
} else if (this.bitmap_info_len <> 52) and
|
||||
(this.bitmap_info_len <> 56) and
|
||||
(this.bitmap_info_len <> 64) and
|
||||
(this.bitmap_info_len <> 108) and
|
||||
(this.bitmap_info_len <> 124) {
|
||||
return "#unsupported BMP file"
|
||||
}
|
||||
if this.bitmap_info_len == 40 {
|
||||
if this.bits_per_pixel >= 16 {
|
||||
// It's poorly documented, but "length 40" data can be silently
|
||||
// augmented with RGB or RGBA channel_masks (12 or 16 bytes).
|
||||
if this.padding >= 16 {
|
||||
this.bitmap_info_len = 56
|
||||
this.padding -= 16
|
||||
} else if this.padding >= 12 {
|
||||
this.bitmap_info_len = 52
|
||||
this.padding -= 12
|
||||
}
|
||||
}
|
||||
} else if (this.bitmap_info_len <> 52) and
|
||||
(this.bitmap_info_len <> 56) and
|
||||
(this.bitmap_info_len <> 64) and
|
||||
(this.bitmap_info_len <> 108) and
|
||||
(this.bitmap_info_len <> 124) {
|
||||
return "#unsupported BMP file"
|
||||
}
|
||||
|
||||
// Treat COMPRESSION_ALPHABITFIELDS the same as COMPRESSION_BITFIELDS.
|
||||
if this.compression == COMPRESSION_ALPHABITFIELDS {
|
||||
this.compression = COMPRESSION_BITFIELDS
|
||||
}
|
||||
// Treat COMPRESSION_ALPHABITFIELDS the same as COMPRESSION_BITFIELDS.
|
||||
if this.compression == COMPRESSION_ALPHABITFIELDS {
|
||||
this.compression = COMPRESSION_BITFIELDS
|
||||
}
|
||||
|
||||
// Read the channel_masks when this.compression is
|
||||
// COMPRESSION_BITFIELDS.
|
||||
if this.compression == COMPRESSION_BITFIELDS {
|
||||
if this.bitmap_info_len >= 52 {
|
||||
this.channel_masks[2] = args.src.read_u32le?()
|
||||
this.channel_masks[1] = args.src.read_u32le?()
|
||||
this.channel_masks[0] = args.src.read_u32le?()
|
||||
if this.bitmap_info_len >= 56 {
|
||||
this.channel_masks[3] = args.src.read_u32le?()
|
||||
// Skip the rest of the BITMAPINFOHEADER.
|
||||
args.src.skip_u32?(n: this.bitmap_info_len ~mod- 56)
|
||||
}
|
||||
// If the explicit channel_masks are what the implicit ones
|
||||
// would be for no compression, treat it as no compression.
|
||||
if (this.channel_masks[0] == 0x0000_00FF) and
|
||||
(this.channel_masks[1] == 0x0000_FF00) and
|
||||
(this.channel_masks[2] == 0x00FF_0000) {
|
||||
if this.bits_per_pixel == 24 {
|
||||
this.compression = COMPRESSION_NONE
|
||||
} else if this.bits_per_pixel == 32 {
|
||||
if (this.channel_masks[3] == 0) or
|
||||
(this.channel_masks[3] == 0xFF00_0000) {
|
||||
this.compression = COMPRESSION_NONE
|
||||
}
|
||||
}
|
||||
}
|
||||
this.process_masks?()
|
||||
}
|
||||
} else if this.bitmap_info_len >= 40 {
|
||||
// Skip the rest of the BITMAPINFOHEADER.
|
||||
args.src.skip_u32?(n: this.bitmap_info_len - 40)
|
||||
} else {
|
||||
return "#unsupported BMP file"
|
||||
}
|
||||
}
|
||||
// Read the channel_masks when this.compression is
|
||||
// COMPRESSION_BITFIELDS.
|
||||
if this.compression == COMPRESSION_BITFIELDS {
|
||||
if this.bitmap_info_len >= 52 {
|
||||
this.channel_masks[2] = args.src.read_u32le?()
|
||||
this.channel_masks[1] = args.src.read_u32le?()
|
||||
this.channel_masks[0] = args.src.read_u32le?()
|
||||
if this.bitmap_info_len >= 56 {
|
||||
this.channel_masks[3] = args.src.read_u32le?()
|
||||
// Skip the rest of the BITMAPINFOHEADER.
|
||||
args.src.skip_u32?(n: this.bitmap_info_len ~mod- 56)
|
||||
}
|
||||
// If the explicit channel_masks are what the implicit ones
|
||||
// would be for no compression, treat it as no compression.
|
||||
if (this.channel_masks[0] == 0x0000_00FF) and
|
||||
(this.channel_masks[1] == 0x0000_FF00) and
|
||||
(this.channel_masks[2] == 0x00FF_0000) {
|
||||
if this.bits_per_pixel == 24 {
|
||||
this.compression = COMPRESSION_NONE
|
||||
} else if this.bits_per_pixel == 32 {
|
||||
if (this.channel_masks[3] == 0) or
|
||||
(this.channel_masks[3] == 0xFF00_0000) {
|
||||
this.compression = COMPRESSION_NONE
|
||||
}
|
||||
}
|
||||
}
|
||||
this.process_masks?()
|
||||
}
|
||||
} else if this.bitmap_info_len >= 40 {
|
||||
// Skip the rest of the BITMAPINFOHEADER.
|
||||
args.src.skip_u32?(n: this.bitmap_info_len - 40)
|
||||
} else {
|
||||
return "#unsupported BMP file"
|
||||
}
|
||||
}
|
||||
|
||||
if this.compression <> COMPRESSION_BITFIELDS {
|
||||
// The palette follows the BITMAPINFOHEADER.
|
||||
if this.bits_per_pixel < 16 {
|
||||
this.read_palette?(src: args.src)
|
||||
}
|
||||
}
|
||||
if this.compression <> COMPRESSION_BITFIELDS {
|
||||
// The palette follows the BITMAPINFOHEADER.
|
||||
if this.bits_per_pixel < 16 {
|
||||
this.read_palette?(src: args.src)
|
||||
}
|
||||
}
|
||||
|
||||
if this.compression == COMPRESSION_NONE {
|
||||
if (this.bits_per_pixel == 1) or
|
||||
(this.bits_per_pixel == 2) or
|
||||
(this.bits_per_pixel == 4) {
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__INDEXED__BGRA_BINARY
|
||||
this.compression = COMPRESSION_LOW_BIT_DEPTH
|
||||
} else if this.bits_per_pixel == 8 {
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__INDEXED__BGRA_BINARY
|
||||
} else if this.bits_per_pixel == 16 {
|
||||
// BMP's 16-bit default is BGRX_5551.
|
||||
this.compression = COMPRESSION_BITFIELDS
|
||||
this.channel_masks[0] = 0x001F
|
||||
this.channel_masks[1] = 0x03E0
|
||||
this.channel_masks[2] = 0x7C00
|
||||
this.channel_masks[3] = 0x0000
|
||||
this.process_masks?()
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE
|
||||
} else if this.bits_per_pixel == 24 {
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__BGR
|
||||
} else if this.bits_per_pixel == 32 {
|
||||
if this.channel_masks[3] == 0 {
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__BGRX
|
||||
} else {
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL
|
||||
}
|
||||
} else {
|
||||
return "#unsupported BMP file"
|
||||
}
|
||||
if this.compression == COMPRESSION_NONE {
|
||||
if (this.bits_per_pixel == 1) or
|
||||
(this.bits_per_pixel == 2) or
|
||||
(this.bits_per_pixel == 4) {
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__INDEXED__BGRA_BINARY
|
||||
this.compression = COMPRESSION_LOW_BIT_DEPTH
|
||||
} else if this.bits_per_pixel == 8 {
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__INDEXED__BGRA_BINARY
|
||||
} else if this.bits_per_pixel == 16 {
|
||||
// BMP's 16-bit default is BGRX_5551.
|
||||
this.compression = COMPRESSION_BITFIELDS
|
||||
this.channel_masks[0] = 0x001F
|
||||
this.channel_masks[1] = 0x03E0
|
||||
this.channel_masks[2] = 0x7C00
|
||||
this.channel_masks[3] = 0x0000
|
||||
this.process_masks?()
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE
|
||||
} else if this.bits_per_pixel == 24 {
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__BGR
|
||||
} else if this.bits_per_pixel == 32 {
|
||||
if this.channel_masks[3] == 0 {
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__BGRX
|
||||
} else {
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL
|
||||
}
|
||||
} else {
|
||||
return "#unsupported BMP file"
|
||||
}
|
||||
|
||||
} else if this.compression == COMPRESSION_RLE8 {
|
||||
if this.bits_per_pixel == 8 {
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__INDEXED__BGRA_BINARY
|
||||
} else {
|
||||
return "#unsupported BMP file"
|
||||
}
|
||||
} else if this.compression == COMPRESSION_RLE8 {
|
||||
if this.bits_per_pixel == 8 {
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__INDEXED__BGRA_BINARY
|
||||
} else {
|
||||
return "#unsupported BMP file"
|
||||
}
|
||||
|
||||
} else if this.compression == COMPRESSION_RLE4 {
|
||||
if this.bits_per_pixel == 4 {
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__INDEXED__BGRA_BINARY
|
||||
} else {
|
||||
return "#unsupported BMP file"
|
||||
}
|
||||
} else if this.compression == COMPRESSION_RLE4 {
|
||||
if this.bits_per_pixel == 4 {
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__INDEXED__BGRA_BINARY
|
||||
} else {
|
||||
return "#unsupported BMP file"
|
||||
}
|
||||
|
||||
} else if this.compression == COMPRESSION_BITFIELDS {
|
||||
if (this.bits_per_pixel == 16) or (this.bits_per_pixel == 32) {
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE
|
||||
} else {
|
||||
return "#unsupported BMP file"
|
||||
}
|
||||
} else if this.compression == COMPRESSION_BITFIELDS {
|
||||
if (this.bits_per_pixel == 16) or (this.bits_per_pixel == 32) {
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE
|
||||
} else {
|
||||
return "#unsupported BMP file"
|
||||
}
|
||||
|
||||
} else {
|
||||
return "#unsupported BMP file"
|
||||
}
|
||||
} else {
|
||||
return "#unsupported BMP file"
|
||||
}
|
||||
|
||||
// OS/2 BMP files have fewer valid bits_per_pixel values.
|
||||
if ((this.bitmap_info_len < 40) or (this.bitmap_info_len == 64)) and
|
||||
(this.bits_per_pixel <> 1) and
|
||||
(this.bits_per_pixel <> 4) and
|
||||
(this.bits_per_pixel <> 8) and
|
||||
(this.bits_per_pixel <> 24) {
|
||||
return "#bad header"
|
||||
}
|
||||
// OS/2 BMP files have fewer valid bits_per_pixel values.
|
||||
if ((this.bitmap_info_len < 40) or (this.bitmap_info_len == 64)) and
|
||||
(this.bits_per_pixel <> 1) and
|
||||
(this.bits_per_pixel <> 4) and
|
||||
(this.bits_per_pixel <> 8) and
|
||||
(this.bits_per_pixel <> 24) {
|
||||
return "#bad header"
|
||||
}
|
||||
|
||||
// The "((x + 3) >> 2) << 2" dance rounds x up to a multiple of 4.
|
||||
if this.bits_per_pixel == 1 {
|
||||
// byte_width is this.width divided by 8, rounding up.
|
||||
byte_width = (this.width >> 3) + (((this.width & 7) + 7) >> 3)
|
||||
this.pad_per_row = (4 - (byte_width & 3)) & 3
|
||||
} else if this.bits_per_pixel == 2 {
|
||||
// byte_width is this.width divided by 4, rounding up.
|
||||
byte_width = (this.width >> 2) + (((this.width & 3) + 3) >> 2)
|
||||
this.pad_per_row = (4 - (byte_width & 3)) & 3
|
||||
} else if this.bits_per_pixel == 4 {
|
||||
// byte_width is this.width divided by 2, rounding up.
|
||||
byte_width = (this.width >> 1) + (this.width & 1)
|
||||
this.pad_per_row = (4 - (byte_width & 3)) & 3
|
||||
} else if this.bits_per_pixel == 8 {
|
||||
this.pad_per_row = (4 - (this.width & 3)) & 3
|
||||
} else if this.bits_per_pixel == 16 {
|
||||
this.pad_per_row = (this.width & 1) * 2
|
||||
} else if this.bits_per_pixel == 24 {
|
||||
this.pad_per_row = this.width & 3
|
||||
} else if this.bits_per_pixel == 32 {
|
||||
this.pad_per_row = 0
|
||||
}
|
||||
// The "((x + 3) >> 2) << 2" dance rounds x up to a multiple of 4.
|
||||
if this.bits_per_pixel == 1 {
|
||||
// byte_width is this.width divided by 8, rounding up.
|
||||
byte_width = (this.width >> 3) + (((this.width & 7) + 7) >> 3)
|
||||
this.pad_per_row = (4 - (byte_width & 3)) & 3
|
||||
} else if this.bits_per_pixel == 2 {
|
||||
// byte_width is this.width divided by 4, rounding up.
|
||||
byte_width = (this.width >> 2) + (((this.width & 3) + 3) >> 2)
|
||||
this.pad_per_row = (4 - (byte_width & 3)) & 3
|
||||
} else if this.bits_per_pixel == 4 {
|
||||
// byte_width is this.width divided by 2, rounding up.
|
||||
byte_width = (this.width >> 1) + (this.width & 1)
|
||||
this.pad_per_row = (4 - (byte_width & 3)) & 3
|
||||
} else if this.bits_per_pixel == 8 {
|
||||
this.pad_per_row = (4 - (this.width & 3)) & 3
|
||||
} else if this.bits_per_pixel == 16 {
|
||||
this.pad_per_row = (this.width & 1) * 2
|
||||
} else if this.bits_per_pixel == 24 {
|
||||
this.pad_per_row = this.width & 3
|
||||
} else if this.bits_per_pixel == 32 {
|
||||
this.pad_per_row = 0
|
||||
}
|
||||
|
||||
this.frame_config_io_position = args.src.position()
|
||||
this.frame_config_io_position = args.src.position()
|
||||
|
||||
if args.dst <> nullptr {
|
||||
dst_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL
|
||||
if (this.channel_num_bits[0] > 8) or
|
||||
(this.channel_num_bits[1] > 8) or
|
||||
(this.channel_num_bits[2] > 8) or
|
||||
(this.channel_num_bits[3] > 8) {
|
||||
dst_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE
|
||||
}
|
||||
if args.dst <> nullptr {
|
||||
dst_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL
|
||||
if (this.channel_num_bits[0] > 8) or
|
||||
(this.channel_num_bits[1] > 8) or
|
||||
(this.channel_num_bits[2] > 8) or
|
||||
(this.channel_num_bits[3] > 8) {
|
||||
dst_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE
|
||||
}
|
||||
|
||||
args.dst.set!(
|
||||
pixfmt: dst_pixfmt,
|
||||
pixsub: 0,
|
||||
width: this.width,
|
||||
height: this.height,
|
||||
first_frame_io_position: this.frame_config_io_position,
|
||||
first_frame_is_opaque: this.channel_masks[3] == 0)
|
||||
}
|
||||
args.dst.set!(
|
||||
pixfmt: dst_pixfmt,
|
||||
pixsub: 0,
|
||||
width: this.width,
|
||||
height: this.height,
|
||||
first_frame_io_position: this.frame_config_io_position,
|
||||
first_frame_is_opaque: this.channel_masks[3] == 0)
|
||||
}
|
||||
|
||||
this.call_sequence = 0x20
|
||||
this.call_sequence = 0x20
|
||||
}
|
||||
|
||||
pub func decoder.decode_frame_config?(dst: nptr base.frame_config, src: base.io_reader) {
|
||||
var status : base.status
|
||||
var status : base.status
|
||||
|
||||
while true {
|
||||
status =? this.do_decode_frame_config?(dst: args.dst, src: args.src)
|
||||
if (status == base."$short read") and args.src.is_closed() {
|
||||
return "#truncated input"
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
while true {
|
||||
status =? this.do_decode_frame_config?(dst: args.dst, src: args.src)
|
||||
if (status == base."$short read") and args.src.is_closed() {
|
||||
return "#truncated input"
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
}
|
||||
|
||||
pri func decoder.do_decode_frame_config?(dst: nptr base.frame_config, src: base.io_reader) {
|
||||
if this.call_sequence == 0x20 {
|
||||
// No-op.
|
||||
} else if this.call_sequence < 0x20 {
|
||||
this.do_decode_image_config?(dst: nullptr, src: args.src)
|
||||
} else if this.call_sequence == 0x28 {
|
||||
if this.frame_config_io_position <> args.src.position() {
|
||||
return base."#bad restart"
|
||||
}
|
||||
} else if this.call_sequence == 0x40 {
|
||||
this.call_sequence = 0x60
|
||||
return base."@end of data"
|
||||
} else {
|
||||
return base."@end of data"
|
||||
}
|
||||
if this.call_sequence == 0x20 {
|
||||
// No-op.
|
||||
} else if this.call_sequence < 0x20 {
|
||||
this.do_decode_image_config?(dst: nullptr, src: args.src)
|
||||
} else if this.call_sequence == 0x28 {
|
||||
if this.frame_config_io_position <> args.src.position() {
|
||||
return base."#bad restart"
|
||||
}
|
||||
} else if this.call_sequence == 0x40 {
|
||||
this.call_sequence = 0x60
|
||||
return base."@end of data"
|
||||
} else {
|
||||
return base."@end of data"
|
||||
}
|
||||
|
||||
if args.dst <> nullptr {
|
||||
args.dst.set!(bounds: this.util.make_rect_ie_u32(
|
||||
min_incl_x: 0,
|
||||
min_incl_y: 0,
|
||||
max_excl_x: this.width,
|
||||
max_excl_y: this.height),
|
||||
duration: 0,
|
||||
index: 0,
|
||||
io_position: this.frame_config_io_position,
|
||||
disposal: 0,
|
||||
opaque_within_bounds: true,
|
||||
overwrite_instead_of_blend: false,
|
||||
background_color: 0xFF00_0000)
|
||||
}
|
||||
if args.dst <> nullptr {
|
||||
args.dst.set!(bounds: this.util.make_rect_ie_u32(
|
||||
min_incl_x: 0,
|
||||
min_incl_y: 0,
|
||||
max_excl_x: this.width,
|
||||
max_excl_y: this.height),
|
||||
duration: 0,
|
||||
index: 0,
|
||||
io_position: this.frame_config_io_position,
|
||||
disposal: 0,
|
||||
opaque_within_bounds: true,
|
||||
overwrite_instead_of_blend: false,
|
||||
background_color: 0xFF00_0000)
|
||||
}
|
||||
|
||||
this.call_sequence = 0x40
|
||||
this.call_sequence = 0x40
|
||||
}
|
||||
|
||||
pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader, blend: base.pixel_blend, workbuf: slice base.u8, opts: nptr base.decode_frame_options) {
|
||||
var status : base.status
|
||||
var status : base.status
|
||||
|
||||
while true {
|
||||
status =? this.do_decode_frame?(dst: args.dst, src: args.src, blend: args.blend, workbuf: args.workbuf, opts: args.opts)
|
||||
if (status == base."$short read") and args.src.is_closed() {
|
||||
return "#truncated input"
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
while true {
|
||||
status =? this.do_decode_frame?(dst: args.dst, src: args.src, blend: args.blend, workbuf: args.workbuf, opts: args.opts)
|
||||
if (status == base."$short read") and args.src.is_closed() {
|
||||
return "#truncated input"
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
}
|
||||
|
||||
pri func decoder.do_decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader, blend: base.pixel_blend, workbuf: slice base.u8, opts: nptr base.decode_frame_options) {
|
||||
var status : base.status
|
||||
var status : base.status
|
||||
|
||||
if this.call_sequence == 0x40 {
|
||||
// No-op.
|
||||
} else if this.call_sequence < 0x40 {
|
||||
this.do_decode_frame_config?(dst: nullptr, src: args.src)
|
||||
} else {
|
||||
return base."@end of data"
|
||||
}
|
||||
if this.call_sequence == 0x40 {
|
||||
// No-op.
|
||||
} else if this.call_sequence < 0x40 {
|
||||
this.do_decode_frame_config?(dst: nullptr, src: args.src)
|
||||
} else {
|
||||
return base."@end of data"
|
||||
}
|
||||
|
||||
args.src.skip_u32?(n: this.padding)
|
||||
args.src.skip_u32?(n: this.padding)
|
||||
|
||||
if (this.width > 0) and (this.height > 0) {
|
||||
this.dst_x = 0
|
||||
if this.top_down {
|
||||
this.dst_y = 0
|
||||
this.dst_y_inc = 1
|
||||
} else {
|
||||
this.dst_y = this.height ~mod- 1
|
||||
this.dst_y_inc = 0xFFFF_FFFF // -1 as a base.u32.
|
||||
}
|
||||
if (this.width > 0) and (this.height > 0) {
|
||||
this.dst_x = 0
|
||||
if this.top_down {
|
||||
this.dst_y = 0
|
||||
this.dst_y_inc = 1
|
||||
} else {
|
||||
this.dst_y = this.height ~mod- 1
|
||||
this.dst_y_inc = 0xFFFF_FFFF // -1 as a base.u32.
|
||||
}
|
||||
|
||||
status = this.swizzler.prepare!(
|
||||
dst_pixfmt: args.dst.pixel_format(),
|
||||
dst_palette: args.dst.palette_or_else(fallback: this.scratch[1024 ..]),
|
||||
src_pixfmt: this.util.make_pixel_format(repr: this.src_pixfmt),
|
||||
src_palette: this.src_palette[..],
|
||||
blend: args.blend)
|
||||
if not status.is_ok() {
|
||||
return status
|
||||
}
|
||||
status = this.swizzler.prepare!(
|
||||
dst_pixfmt: args.dst.pixel_format(),
|
||||
dst_palette: args.dst.palette_or_else(fallback: this.scratch[1024 ..]),
|
||||
src_pixfmt: this.util.make_pixel_format(repr: this.src_pixfmt),
|
||||
src_palette: this.src_palette[..],
|
||||
blend: args.blend)
|
||||
if not status.is_ok() {
|
||||
return status
|
||||
}
|
||||
|
||||
while true {
|
||||
if this.compression == COMPRESSION_NONE {
|
||||
status = this.swizzle_none!(dst: args.dst, src: args.src)
|
||||
} else if this.compression < COMPRESSION_BITFIELDS {
|
||||
status = this.swizzle_rle!(dst: args.dst, src: args.src)
|
||||
} else if this.compression == COMPRESSION_BITFIELDS {
|
||||
status = this.swizzle_bitfields!(dst: args.dst, src: args.src)
|
||||
} else {
|
||||
status = this.swizzle_low_bit_depth!(dst: args.dst, src: args.src)
|
||||
}
|
||||
while true {
|
||||
if this.compression == COMPRESSION_NONE {
|
||||
status = this.swizzle_none!(dst: args.dst, src: args.src)
|
||||
} else if this.compression < COMPRESSION_BITFIELDS {
|
||||
status = this.swizzle_rle!(dst: args.dst, src: args.src)
|
||||
} else if this.compression == COMPRESSION_BITFIELDS {
|
||||
status = this.swizzle_bitfields!(dst: args.dst, src: args.src)
|
||||
} else {
|
||||
status = this.swizzle_low_bit_depth!(dst: args.dst, src: args.src)
|
||||
}
|
||||
|
||||
if status.is_ok() {
|
||||
break
|
||||
} else if status <> "@internal note: short read" {
|
||||
return status
|
||||
}
|
||||
yield? base."$short read"
|
||||
} endwhile
|
||||
if status.is_ok() {
|
||||
break
|
||||
} else if status <> "@internal note: short read" {
|
||||
return status
|
||||
}
|
||||
yield? base."$short read"
|
||||
} endwhile
|
||||
|
||||
args.src.skip_u32?(n: this.pending_pad)
|
||||
this.pending_pad = 0
|
||||
}
|
||||
args.src.skip_u32?(n: this.pending_pad)
|
||||
this.pending_pad = 0
|
||||
}
|
||||
|
||||
this.call_sequence = 0x60
|
||||
this.call_sequence = 0x60
|
||||
}
|
||||
|
||||
pri func decoder.swizzle_none!(dst: ptr base.pixel_buffer, src: base.io_reader) base.status {
|
||||
var dst_pixfmt : base.pixel_format
|
||||
var dst_bits_per_pixel : base.u32[..= 256]
|
||||
var dst_bytes_per_pixel : base.u64[..= 32]
|
||||
var dst_bytes_per_row : base.u64
|
||||
var src_bytes_per_pixel : base.u32[..= 4]
|
||||
var dst_palette : slice base.u8
|
||||
var tab : table base.u8
|
||||
var dst : slice base.u8
|
||||
var i : base.u64
|
||||
var j : base.u64
|
||||
var n : base.u64
|
||||
var dst_pixfmt : base.pixel_format
|
||||
var dst_bits_per_pixel : base.u32[..= 256]
|
||||
var dst_bytes_per_pixel : base.u64[..= 32]
|
||||
var dst_bytes_per_row : base.u64
|
||||
var src_bytes_per_pixel : base.u32[..= 4]
|
||||
var dst_palette : slice base.u8
|
||||
var tab : table base.u8
|
||||
var dst : slice base.u8
|
||||
var i : base.u64
|
||||
var j : base.u64
|
||||
var n : base.u64
|
||||
|
||||
// TODO: the dst_pixfmt variable shouldn't be necessary. We should be able
|
||||
// to chain the two calls: "args.dst.pixel_format().bits_per_pixel()".
|
||||
dst_pixfmt = args.dst.pixel_format()
|
||||
dst_bits_per_pixel = dst_pixfmt.bits_per_pixel()
|
||||
if (dst_bits_per_pixel & 7) <> 0 {
|
||||
return base."#unsupported option"
|
||||
}
|
||||
dst_bytes_per_pixel = (dst_bits_per_pixel / 8) as base.u64
|
||||
dst_bytes_per_row = (this.width as base.u64) * dst_bytes_per_pixel
|
||||
dst_palette = args.dst.palette_or_else(fallback: this.scratch[1024 ..])
|
||||
tab = args.dst.plane(p: 0)
|
||||
// TODO: the dst_pixfmt variable shouldn't be necessary. We should be able
|
||||
// to chain the two calls: "args.dst.pixel_format().bits_per_pixel()".
|
||||
dst_pixfmt = args.dst.pixel_format()
|
||||
dst_bits_per_pixel = dst_pixfmt.bits_per_pixel()
|
||||
if (dst_bits_per_pixel & 7) <> 0 {
|
||||
return base."#unsupported option"
|
||||
}
|
||||
dst_bytes_per_pixel = (dst_bits_per_pixel / 8) as base.u64
|
||||
dst_bytes_per_row = (this.width as base.u64) * dst_bytes_per_pixel
|
||||
dst_palette = args.dst.palette_or_else(fallback: this.scratch[1024 ..])
|
||||
tab = args.dst.plane(p: 0)
|
||||
|
||||
while.outer true {
|
||||
while this.pending_pad > 0 {
|
||||
if args.src.length() <= 0 {
|
||||
return "@internal note: short read"
|
||||
}
|
||||
this.pending_pad -= 1
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
} endwhile
|
||||
while.outer true {
|
||||
while this.pending_pad > 0 {
|
||||
if args.src.length() <= 0 {
|
||||
return "@internal note: short read"
|
||||
}
|
||||
this.pending_pad -= 1
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
} endwhile
|
||||
|
||||
while.inner true {
|
||||
if this.dst_x == this.width {
|
||||
this.dst_x = 0
|
||||
this.dst_y ~mod+= this.dst_y_inc
|
||||
if this.dst_y >= this.height {
|
||||
if this.height > 0 {
|
||||
this.pending_pad = this.pad_per_row
|
||||
}
|
||||
break.outer
|
||||
} else if this.pad_per_row <> 0 {
|
||||
this.pending_pad = this.pad_per_row
|
||||
continue.outer
|
||||
}
|
||||
}
|
||||
while.inner true {
|
||||
if this.dst_x == this.width {
|
||||
this.dst_x = 0
|
||||
this.dst_y ~mod+= this.dst_y_inc
|
||||
if this.dst_y >= this.height {
|
||||
if this.height > 0 {
|
||||
this.pending_pad = this.pad_per_row
|
||||
}
|
||||
break.outer
|
||||
} else if this.pad_per_row <> 0 {
|
||||
this.pending_pad = this.pad_per_row
|
||||
continue.outer
|
||||
}
|
||||
}
|
||||
|
||||
dst = tab.row_u32(y: this.dst_y)
|
||||
if dst_bytes_per_row < dst.length() {
|
||||
dst = dst[.. dst_bytes_per_row]
|
||||
}
|
||||
i = (this.dst_x as base.u64) * dst_bytes_per_pixel
|
||||
if i >= dst.length() {
|
||||
if this.bits_per_pixel > 32 {
|
||||
return "#unsupported BMP file"
|
||||
}
|
||||
src_bytes_per_pixel = this.bits_per_pixel / 8
|
||||
if src_bytes_per_pixel == 0 {
|
||||
return "#unsupported BMP file"
|
||||
}
|
||||
n = args.src.length() / (src_bytes_per_pixel as base.u64)
|
||||
n = n.min(a: (this.width ~mod- this.dst_x) as base.u64)
|
||||
j = n
|
||||
while j >= 8 {
|
||||
if args.src.length() >= ((src_bytes_per_pixel * 8) as base.u64) {
|
||||
args.src.skip_u32_fast!(
|
||||
actual: src_bytes_per_pixel * 8,
|
||||
worst_case: src_bytes_per_pixel * 8)
|
||||
}
|
||||
j -= 8
|
||||
} endwhile
|
||||
while j > 0 {
|
||||
if args.src.length() >= ((src_bytes_per_pixel * 1) as base.u64) {
|
||||
args.src.skip_u32_fast!(
|
||||
actual: src_bytes_per_pixel * 1,
|
||||
worst_case: src_bytes_per_pixel * 1)
|
||||
}
|
||||
j -= 1
|
||||
} endwhile
|
||||
} else {
|
||||
n = this.swizzler.swizzle_interleaved_from_reader!(
|
||||
dst: dst[i ..],
|
||||
dst_palette: dst_palette,
|
||||
src: args.src)
|
||||
}
|
||||
if n == 0 {
|
||||
return "@internal note: short read"
|
||||
}
|
||||
this.dst_x ~sat+= (n & 0xFFFF_FFFF) as base.u32
|
||||
} endwhile.inner
|
||||
} endwhile.outer
|
||||
dst = tab.row_u32(y: this.dst_y)
|
||||
if dst_bytes_per_row < dst.length() {
|
||||
dst = dst[.. dst_bytes_per_row]
|
||||
}
|
||||
i = (this.dst_x as base.u64) * dst_bytes_per_pixel
|
||||
if i >= dst.length() {
|
||||
if this.bits_per_pixel > 32 {
|
||||
return "#unsupported BMP file"
|
||||
}
|
||||
src_bytes_per_pixel = this.bits_per_pixel / 8
|
||||
if src_bytes_per_pixel == 0 {
|
||||
return "#unsupported BMP file"
|
||||
}
|
||||
n = args.src.length() / (src_bytes_per_pixel as base.u64)
|
||||
n = n.min(a: (this.width ~mod- this.dst_x) as base.u64)
|
||||
j = n
|
||||
while j >= 8 {
|
||||
if args.src.length() >= ((src_bytes_per_pixel * 8) as base.u64) {
|
||||
args.src.skip_u32_fast!(
|
||||
actual: src_bytes_per_pixel * 8,
|
||||
worst_case: src_bytes_per_pixel * 8)
|
||||
}
|
||||
j -= 8
|
||||
} endwhile
|
||||
while j > 0 {
|
||||
if args.src.length() >= ((src_bytes_per_pixel * 1) as base.u64) {
|
||||
args.src.skip_u32_fast!(
|
||||
actual: src_bytes_per_pixel * 1,
|
||||
worst_case: src_bytes_per_pixel * 1)
|
||||
}
|
||||
j -= 1
|
||||
} endwhile
|
||||
} else {
|
||||
n = this.swizzler.swizzle_interleaved_from_reader!(
|
||||
dst: dst[i ..],
|
||||
dst_palette: dst_palette,
|
||||
src: args.src)
|
||||
}
|
||||
if n == 0 {
|
||||
return "@internal note: short read"
|
||||
}
|
||||
this.dst_x ~sat+= (n & 0xFFFF_FFFF) as base.u32
|
||||
} endwhile.inner
|
||||
} endwhile.outer
|
||||
|
||||
return ok
|
||||
return ok
|
||||
}
|
||||
|
||||
pri const RLE_STATE_NEUTRAL : base.u32 = 0
|
||||
@@ -604,716 +604,716 @@ pri const RLE_STATE_DELTA_X : base.u32 = 4
|
||||
pri const RLE_STATE_DELTA_Y : base.u32 = 5
|
||||
|
||||
pri func decoder.swizzle_rle!(dst: ptr base.pixel_buffer, src: base.io_reader) base.status {
|
||||
var dst_pixfmt : base.pixel_format
|
||||
var dst_bits_per_pixel : base.u32[..= 256]
|
||||
var dst_bytes_per_pixel : base.u64[..= 32]
|
||||
var dst_bytes_per_row : base.u64
|
||||
var dst_palette : slice base.u8
|
||||
var tab : table base.u8
|
||||
var row : slice base.u8
|
||||
var dst : slice base.u8
|
||||
var i : base.u64
|
||||
var n : base.u64
|
||||
var dst_pixfmt : base.pixel_format
|
||||
var dst_bits_per_pixel : base.u32[..= 256]
|
||||
var dst_bytes_per_pixel : base.u64[..= 32]
|
||||
var dst_bytes_per_row : base.u64
|
||||
var dst_palette : slice base.u8
|
||||
var tab : table base.u8
|
||||
var row : slice base.u8
|
||||
var dst : slice base.u8
|
||||
var i : base.u64
|
||||
var n : base.u64
|
||||
|
||||
var p0 : base.u32[..= 259]
|
||||
var code : base.u8
|
||||
var indexes : array[2] base.u8
|
||||
var p0 : base.u32[..= 259]
|
||||
var code : base.u8
|
||||
var indexes : array[2] base.u8
|
||||
|
||||
var rle_state : base.u32
|
||||
var rle_state : base.u32
|
||||
|
||||
var chunk_bits : base.u32
|
||||
var chunk_count : base.u32[..= 64]
|
||||
var chunk_bits : base.u32
|
||||
var chunk_count : base.u32[..= 64]
|
||||
|
||||
// TODO: the dst_pixfmt variable shouldn't be necessary. We should be able
|
||||
// to chain the two calls: "args.dst.pixel_format().bits_per_pixel()".
|
||||
dst_pixfmt = args.dst.pixel_format()
|
||||
dst_bits_per_pixel = dst_pixfmt.bits_per_pixel()
|
||||
if (dst_bits_per_pixel & 7) <> 0 {
|
||||
return base."#unsupported option"
|
||||
}
|
||||
dst_bytes_per_pixel = (dst_bits_per_pixel / 8) as base.u64
|
||||
dst_bytes_per_row = (this.width as base.u64) * dst_bytes_per_pixel
|
||||
dst_palette = args.dst.palette_or_else(fallback: this.scratch[1024 ..])
|
||||
tab = args.dst.plane(p: 0)
|
||||
// TODO: the dst_pixfmt variable shouldn't be necessary. We should be able
|
||||
// to chain the two calls: "args.dst.pixel_format().bits_per_pixel()".
|
||||
dst_pixfmt = args.dst.pixel_format()
|
||||
dst_bits_per_pixel = dst_pixfmt.bits_per_pixel()
|
||||
if (dst_bits_per_pixel & 7) <> 0 {
|
||||
return base."#unsupported option"
|
||||
}
|
||||
dst_bytes_per_pixel = (dst_bits_per_pixel / 8) as base.u64
|
||||
dst_bytes_per_row = (this.width as base.u64) * dst_bytes_per_pixel
|
||||
dst_palette = args.dst.palette_or_else(fallback: this.scratch[1024 ..])
|
||||
tab = args.dst.plane(p: 0)
|
||||
|
||||
rle_state = this.rle_state
|
||||
rle_state = this.rle_state
|
||||
|
||||
while.outer true {
|
||||
row = tab.row_u32(y: this.dst_y)
|
||||
if dst_bytes_per_row < row.length() {
|
||||
row = row[.. dst_bytes_per_row]
|
||||
}
|
||||
while.outer true {
|
||||
row = tab.row_u32(y: this.dst_y)
|
||||
if dst_bytes_per_row < row.length() {
|
||||
row = row[.. dst_bytes_per_row]
|
||||
}
|
||||
|
||||
while.middle true {
|
||||
i = (this.dst_x as base.u64) * dst_bytes_per_pixel
|
||||
if i <= row.length() {
|
||||
dst = row[i ..]
|
||||
} else {
|
||||
dst = this.util.empty_slice_u8()
|
||||
}
|
||||
while.middle true {
|
||||
i = (this.dst_x as base.u64) * dst_bytes_per_pixel
|
||||
if i <= row.length() {
|
||||
dst = row[i ..]
|
||||
} else {
|
||||
dst = this.util.empty_slice_u8()
|
||||
}
|
||||
|
||||
while.goto_suspend true {{
|
||||
while.inner true {
|
||||
if rle_state == RLE_STATE_NEUTRAL {
|
||||
if args.src.length() < 1 {
|
||||
break.goto_suspend
|
||||
}
|
||||
code = args.src.peek_u8()
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
if code == 0 {
|
||||
rle_state = RLE_STATE_ESCAPE
|
||||
continue.inner
|
||||
}
|
||||
this.rle_length = code as base.u32
|
||||
rle_state = RLE_STATE_RUN
|
||||
continue.inner
|
||||
while.goto_suspend true {{
|
||||
while.inner true {
|
||||
if rle_state == RLE_STATE_NEUTRAL {
|
||||
if args.src.length() < 1 {
|
||||
break.goto_suspend
|
||||
}
|
||||
code = args.src.peek_u8()
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
if code == 0 {
|
||||
rle_state = RLE_STATE_ESCAPE
|
||||
continue.inner
|
||||
}
|
||||
this.rle_length = code as base.u32
|
||||
rle_state = RLE_STATE_RUN
|
||||
continue.inner
|
||||
|
||||
} else if rle_state == RLE_STATE_RUN {
|
||||
if args.src.length() < 1 {
|
||||
break.goto_suspend
|
||||
}
|
||||
code = args.src.peek_u8()
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
if this.bits_per_pixel == 8 {
|
||||
p0 = 0
|
||||
while p0 < this.rle_length {
|
||||
assert p0 < 255 via "a < b: a < c; c <= b"(c: this.rle_length)
|
||||
this.scratch[p0] = code
|
||||
p0 += 1
|
||||
} endwhile
|
||||
} else {
|
||||
indexes[0] = (code >> 4) as base.u8
|
||||
indexes[1] = code & 0x0F
|
||||
p0 = 0
|
||||
while p0 < this.rle_length {
|
||||
assert p0 < 255 via "a < b: a < c; c <= b"(c: this.rle_length)
|
||||
this.scratch[p0 + 0] = indexes[0]
|
||||
this.scratch[p0 + 1] = indexes[1]
|
||||
p0 += 2
|
||||
} endwhile
|
||||
}
|
||||
this.swizzler.swizzle_interleaved_from_slice!(
|
||||
dst: dst,
|
||||
dst_palette: dst_palette,
|
||||
src: this.scratch[.. this.rle_length])
|
||||
this.dst_x ~sat+= this.rle_length
|
||||
rle_state = RLE_STATE_NEUTRAL
|
||||
continue.middle
|
||||
} else if rle_state == RLE_STATE_RUN {
|
||||
if args.src.length() < 1 {
|
||||
break.goto_suspend
|
||||
}
|
||||
code = args.src.peek_u8()
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
if this.bits_per_pixel == 8 {
|
||||
p0 = 0
|
||||
while p0 < this.rle_length {
|
||||
assert p0 < 255 via "a < b: a < c; c <= b"(c: this.rle_length)
|
||||
this.scratch[p0] = code
|
||||
p0 += 1
|
||||
} endwhile
|
||||
} else {
|
||||
indexes[0] = (code >> 4) as base.u8
|
||||
indexes[1] = code & 0x0F
|
||||
p0 = 0
|
||||
while p0 < this.rle_length {
|
||||
assert p0 < 255 via "a < b: a < c; c <= b"(c: this.rle_length)
|
||||
this.scratch[p0 + 0] = indexes[0]
|
||||
this.scratch[p0 + 1] = indexes[1]
|
||||
p0 += 2
|
||||
} endwhile
|
||||
}
|
||||
this.swizzler.swizzle_interleaved_from_slice!(
|
||||
dst: dst,
|
||||
dst_palette: dst_palette,
|
||||
src: this.scratch[.. this.rle_length])
|
||||
this.dst_x ~sat+= this.rle_length
|
||||
rle_state = RLE_STATE_NEUTRAL
|
||||
continue.middle
|
||||
|
||||
} else if rle_state == RLE_STATE_ESCAPE {
|
||||
if args.src.length() < 1 {
|
||||
break.goto_suspend
|
||||
}
|
||||
code = args.src.peek_u8()
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
if code < 2 { // 0=EOL, 1=EOF.
|
||||
if (this.dst_y >= this.height) and (code == 0) {
|
||||
return "#bad RLE compression"
|
||||
}
|
||||
this.swizzler.swizzle_interleaved_transparent_black!(
|
||||
dst: dst,
|
||||
dst_palette: dst_palette,
|
||||
num_pixels: 0xFFFF_FFFF_FFFF_FFFF)
|
||||
this.dst_x = 0
|
||||
this.dst_y ~mod+= this.dst_y_inc
|
||||
if code > 0 {
|
||||
break.outer
|
||||
}
|
||||
rle_state = RLE_STATE_NEUTRAL
|
||||
continue.outer
|
||||
} else if code == 2 { // 2=DELTA.
|
||||
rle_state = RLE_STATE_DELTA_X
|
||||
continue.inner
|
||||
}
|
||||
this.rle_length = code as base.u32
|
||||
this.rle_padded = (this.bits_per_pixel == 8) and ((code & 1) <> 0)
|
||||
rle_state = RLE_STATE_LITERAL
|
||||
continue.inner
|
||||
} else if rle_state == RLE_STATE_ESCAPE {
|
||||
if args.src.length() < 1 {
|
||||
break.goto_suspend
|
||||
}
|
||||
code = args.src.peek_u8()
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
if code < 2 { // 0=EOL, 1=EOF.
|
||||
if (this.dst_y >= this.height) and (code == 0) {
|
||||
return "#bad RLE compression"
|
||||
}
|
||||
this.swizzler.swizzle_interleaved_transparent_black!(
|
||||
dst: dst,
|
||||
dst_palette: dst_palette,
|
||||
num_pixels: 0xFFFF_FFFF_FFFF_FFFF)
|
||||
this.dst_x = 0
|
||||
this.dst_y ~mod+= this.dst_y_inc
|
||||
if code > 0 {
|
||||
break.outer
|
||||
}
|
||||
rle_state = RLE_STATE_NEUTRAL
|
||||
continue.outer
|
||||
} else if code == 2 { // 2=DELTA.
|
||||
rle_state = RLE_STATE_DELTA_X
|
||||
continue.inner
|
||||
}
|
||||
this.rle_length = code as base.u32
|
||||
this.rle_padded = (this.bits_per_pixel == 8) and ((code & 1) <> 0)
|
||||
rle_state = RLE_STATE_LITERAL
|
||||
continue.inner
|
||||
|
||||
} else if rle_state == RLE_STATE_LITERAL {
|
||||
if this.bits_per_pixel == 8 {
|
||||
n = this.swizzler.limited_swizzle_u32_interleaved_from_reader!(
|
||||
up_to_num_pixels: this.rle_length,
|
||||
dst: dst,
|
||||
dst_palette: dst_palette,
|
||||
src: args.src)
|
||||
this.dst_x ~sat+= (n & 0xFFFF_FFFF) as base.u32
|
||||
this.rle_length ~sat-= (n & 0xFFFF_FFFF) as base.u32
|
||||
} else {
|
||||
// Calculate the remaining number of 16-bit chunks. At
|
||||
// 4 bits per pixel there are 4 pixels per chunk.
|
||||
// Division rounds up.
|
||||
chunk_count = (this.rle_length + 3) / 4
|
||||
p0 = 0
|
||||
while (chunk_count > 0) and (args.src.length() >= 2) {
|
||||
chunk_bits = args.src.peek_u16be_as_u32()
|
||||
args.src.skip_u32_fast!(actual: 2, worst_case: 2)
|
||||
this.scratch[p0 + 0x00] = (0x0F & (chunk_bits >> 0x0C)) as base.u8
|
||||
this.scratch[p0 + 0x01] = (0x0F & (chunk_bits >> 0x08)) as base.u8
|
||||
this.scratch[p0 + 0x02] = (0x0F & (chunk_bits >> 0x04)) as base.u8
|
||||
this.scratch[p0 + 0x03] = (0x0F & (chunk_bits >> 0x00)) as base.u8
|
||||
p0 = (p0 & 255) + 0x04
|
||||
chunk_count -= 1
|
||||
} endwhile
|
||||
p0 = p0.min(a: this.rle_length)
|
||||
this.swizzler.swizzle_interleaved_from_slice!(
|
||||
dst: dst,
|
||||
dst_palette: dst_palette,
|
||||
src: this.scratch[.. p0])
|
||||
this.dst_x ~sat+= p0
|
||||
this.rle_length ~sat-= p0
|
||||
}
|
||||
} else if rle_state == RLE_STATE_LITERAL {
|
||||
if this.bits_per_pixel == 8 {
|
||||
n = this.swizzler.limited_swizzle_u32_interleaved_from_reader!(
|
||||
up_to_num_pixels: this.rle_length,
|
||||
dst: dst,
|
||||
dst_palette: dst_palette,
|
||||
src: args.src)
|
||||
this.dst_x ~sat+= (n & 0xFFFF_FFFF) as base.u32
|
||||
this.rle_length ~sat-= (n & 0xFFFF_FFFF) as base.u32
|
||||
} else {
|
||||
// Calculate the remaining number of 16-bit chunks. At
|
||||
// 4 bits per pixel there are 4 pixels per chunk.
|
||||
// Division rounds up.
|
||||
chunk_count = (this.rle_length + 3) / 4
|
||||
p0 = 0
|
||||
while (chunk_count > 0) and (args.src.length() >= 2) {
|
||||
chunk_bits = args.src.peek_u16be_as_u32()
|
||||
args.src.skip_u32_fast!(actual: 2, worst_case: 2)
|
||||
this.scratch[p0 + 0x00] = (0x0F & (chunk_bits >> 0x0C)) as base.u8
|
||||
this.scratch[p0 + 0x01] = (0x0F & (chunk_bits >> 0x08)) as base.u8
|
||||
this.scratch[p0 + 0x02] = (0x0F & (chunk_bits >> 0x04)) as base.u8
|
||||
this.scratch[p0 + 0x03] = (0x0F & (chunk_bits >> 0x00)) as base.u8
|
||||
p0 = (p0 & 255) + 0x04
|
||||
chunk_count -= 1
|
||||
} endwhile
|
||||
p0 = p0.min(a: this.rle_length)
|
||||
this.swizzler.swizzle_interleaved_from_slice!(
|
||||
dst: dst,
|
||||
dst_palette: dst_palette,
|
||||
src: this.scratch[.. p0])
|
||||
this.dst_x ~sat+= p0
|
||||
this.rle_length ~sat-= p0
|
||||
}
|
||||
|
||||
if this.rle_length > 0 {
|
||||
break.goto_suspend
|
||||
}
|
||||
if this.rle_padded {
|
||||
if args.src.length() < 1 {
|
||||
break.goto_suspend
|
||||
}
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
this.rle_padded = false
|
||||
}
|
||||
rle_state = RLE_STATE_NEUTRAL
|
||||
continue.middle
|
||||
if this.rle_length > 0 {
|
||||
break.goto_suspend
|
||||
}
|
||||
if this.rle_padded {
|
||||
if args.src.length() < 1 {
|
||||
break.goto_suspend
|
||||
}
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
this.rle_padded = false
|
||||
}
|
||||
rle_state = RLE_STATE_NEUTRAL
|
||||
continue.middle
|
||||
|
||||
} else if rle_state == RLE_STATE_DELTA_X {
|
||||
if args.src.length() < 1 {
|
||||
break.goto_suspend
|
||||
}
|
||||
this.rle_delta_x = args.src.peek_u8()
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
rle_state = RLE_STATE_DELTA_Y
|
||||
continue.inner
|
||||
} else if rle_state == RLE_STATE_DELTA_X {
|
||||
if args.src.length() < 1 {
|
||||
break.goto_suspend
|
||||
}
|
||||
this.rle_delta_x = args.src.peek_u8()
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
rle_state = RLE_STATE_DELTA_Y
|
||||
continue.inner
|
||||
|
||||
} // else (rle_state == RLE_STATE_DELTA_Y).
|
||||
if args.src.length() < 1 {
|
||||
break.goto_suspend
|
||||
}
|
||||
code = args.src.peek_u8()
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
if this.rle_delta_x > 0 {
|
||||
this.swizzler.swizzle_interleaved_transparent_black!(
|
||||
dst: dst,
|
||||
dst_palette: dst_palette,
|
||||
num_pixels: this.rle_delta_x as base.u64)
|
||||
this.dst_x ~sat+= this.rle_delta_x as base.u32
|
||||
this.rle_delta_x = 0
|
||||
if this.dst_x > this.width {
|
||||
return "#bad RLE compression"
|
||||
}
|
||||
}
|
||||
} // else (rle_state == RLE_STATE_DELTA_Y).
|
||||
if args.src.length() < 1 {
|
||||
break.goto_suspend
|
||||
}
|
||||
code = args.src.peek_u8()
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
if this.rle_delta_x > 0 {
|
||||
this.swizzler.swizzle_interleaved_transparent_black!(
|
||||
dst: dst,
|
||||
dst_palette: dst_palette,
|
||||
num_pixels: this.rle_delta_x as base.u64)
|
||||
this.dst_x ~sat+= this.rle_delta_x as base.u32
|
||||
this.rle_delta_x = 0
|
||||
if this.dst_x > this.width {
|
||||
return "#bad RLE compression"
|
||||
}
|
||||
}
|
||||
|
||||
// Paint the skipped rows (and the partial final row) with
|
||||
// transparent black.
|
||||
if code > 0 {
|
||||
code -= 1
|
||||
while true {
|
||||
this.dst_y ~mod+= this.dst_y_inc
|
||||
if this.dst_y >= this.height {
|
||||
return "#bad RLE compression"
|
||||
}
|
||||
row = tab.row_u32(y: this.dst_y)
|
||||
if dst_bytes_per_row < row.length() {
|
||||
row = row[.. dst_bytes_per_row]
|
||||
}
|
||||
if code <= 0 {
|
||||
this.swizzler.swizzle_interleaved_transparent_black!(
|
||||
dst: row,
|
||||
dst_palette: dst_palette,
|
||||
num_pixels: this.dst_x as base.u64)
|
||||
break
|
||||
}
|
||||
this.swizzler.swizzle_interleaved_transparent_black!(
|
||||
dst: row,
|
||||
dst_palette: dst_palette,
|
||||
num_pixels: 0xFFFF_FFFF_FFFF_FFFF)
|
||||
code -= 1
|
||||
} endwhile
|
||||
}
|
||||
// Paint the skipped rows (and the partial final row) with
|
||||
// transparent black.
|
||||
if code > 0 {
|
||||
code -= 1
|
||||
while true {
|
||||
this.dst_y ~mod+= this.dst_y_inc
|
||||
if this.dst_y >= this.height {
|
||||
return "#bad RLE compression"
|
||||
}
|
||||
row = tab.row_u32(y: this.dst_y)
|
||||
if dst_bytes_per_row < row.length() {
|
||||
row = row[.. dst_bytes_per_row]
|
||||
}
|
||||
if code <= 0 {
|
||||
this.swizzler.swizzle_interleaved_transparent_black!(
|
||||
dst: row,
|
||||
dst_palette: dst_palette,
|
||||
num_pixels: this.dst_x as base.u64)
|
||||
break
|
||||
}
|
||||
this.swizzler.swizzle_interleaved_transparent_black!(
|
||||
dst: row,
|
||||
dst_palette: dst_palette,
|
||||
num_pixels: 0xFFFF_FFFF_FFFF_FFFF)
|
||||
code -= 1
|
||||
} endwhile
|
||||
}
|
||||
|
||||
rle_state = RLE_STATE_NEUTRAL
|
||||
continue.middle
|
||||
rle_state = RLE_STATE_NEUTRAL
|
||||
continue.middle
|
||||
|
||||
} endwhile.inner
|
||||
}} endwhile.goto_suspend
|
||||
this.rle_state = rle_state
|
||||
return "@internal note: short read"
|
||||
} endwhile.middle
|
||||
} endwhile.outer
|
||||
} endwhile.inner
|
||||
}} endwhile.goto_suspend
|
||||
this.rle_state = rle_state
|
||||
return "@internal note: short read"
|
||||
} endwhile.middle
|
||||
} endwhile.outer
|
||||
|
||||
while this.dst_y < this.height {
|
||||
row = tab.row_u32(y: this.dst_y)
|
||||
if dst_bytes_per_row < row.length() {
|
||||
row = row[.. dst_bytes_per_row]
|
||||
}
|
||||
this.swizzler.swizzle_interleaved_transparent_black!(
|
||||
dst: row,
|
||||
dst_palette: dst_palette,
|
||||
num_pixels: 0xFFFF_FFFF_FFFF_FFFF)
|
||||
this.dst_y ~mod+= this.dst_y_inc
|
||||
} endwhile
|
||||
while this.dst_y < this.height {
|
||||
row = tab.row_u32(y: this.dst_y)
|
||||
if dst_bytes_per_row < row.length() {
|
||||
row = row[.. dst_bytes_per_row]
|
||||
}
|
||||
this.swizzler.swizzle_interleaved_transparent_black!(
|
||||
dst: row,
|
||||
dst_palette: dst_palette,
|
||||
num_pixels: 0xFFFF_FFFF_FFFF_FFFF)
|
||||
this.dst_y ~mod+= this.dst_y_inc
|
||||
} endwhile
|
||||
|
||||
return ok
|
||||
return ok
|
||||
}
|
||||
|
||||
pri func decoder.swizzle_bitfields!(dst: ptr base.pixel_buffer, src: base.io_reader) base.status {
|
||||
var dst_pixfmt : base.pixel_format
|
||||
var dst_bits_per_pixel : base.u32[..= 256]
|
||||
var dst_bytes_per_pixel : base.u64[..= 32]
|
||||
var dst_bytes_per_row : base.u64
|
||||
var dst_palette : slice base.u8
|
||||
var tab : table base.u8
|
||||
var dst : slice base.u8
|
||||
var i : base.u64
|
||||
var n : base.u64
|
||||
var dst_pixfmt : base.pixel_format
|
||||
var dst_bits_per_pixel : base.u32[..= 256]
|
||||
var dst_bytes_per_pixel : base.u64[..= 32]
|
||||
var dst_bytes_per_row : base.u64
|
||||
var dst_palette : slice base.u8
|
||||
var tab : table base.u8
|
||||
var dst : slice base.u8
|
||||
var i : base.u64
|
||||
var n : base.u64
|
||||
|
||||
var p0 : base.u32[..= 256]
|
||||
var p1 : base.u32[..= 256]
|
||||
var p1_temp : base.u32
|
||||
var p0 : base.u32[..= 256]
|
||||
var p1 : base.u32[..= 256]
|
||||
var p1_temp : base.u32
|
||||
|
||||
var num_bits : base.u32[..= 32]
|
||||
var c : base.u32
|
||||
var c32 : base.u32
|
||||
var channel : base.u32[..= 4]
|
||||
var num_bits : base.u32[..= 32]
|
||||
var c : base.u32
|
||||
var c32 : base.u32
|
||||
var channel : base.u32[..= 4]
|
||||
|
||||
// TODO: the dst_pixfmt variable shouldn't be necessary. We should be able
|
||||
// to chain the two calls: "args.dst.pixel_format().bits_per_pixel()".
|
||||
dst_pixfmt = args.dst.pixel_format()
|
||||
dst_bits_per_pixel = dst_pixfmt.bits_per_pixel()
|
||||
if (dst_bits_per_pixel & 7) <> 0 {
|
||||
return base."#unsupported option"
|
||||
}
|
||||
dst_bytes_per_pixel = (dst_bits_per_pixel / 8) as base.u64
|
||||
dst_bytes_per_row = (this.width as base.u64) * dst_bytes_per_pixel
|
||||
dst_palette = args.dst.palette_or_else(fallback: this.scratch[1024 ..])
|
||||
tab = args.dst.plane(p: 0)
|
||||
// TODO: the dst_pixfmt variable shouldn't be necessary. We should be able
|
||||
// to chain the two calls: "args.dst.pixel_format().bits_per_pixel()".
|
||||
dst_pixfmt = args.dst.pixel_format()
|
||||
dst_bits_per_pixel = dst_pixfmt.bits_per_pixel()
|
||||
if (dst_bits_per_pixel & 7) <> 0 {
|
||||
return base."#unsupported option"
|
||||
}
|
||||
dst_bytes_per_pixel = (dst_bits_per_pixel / 8) as base.u64
|
||||
dst_bytes_per_row = (this.width as base.u64) * dst_bytes_per_pixel
|
||||
dst_palette = args.dst.palette_or_else(fallback: this.scratch[1024 ..])
|
||||
tab = args.dst.plane(p: 0)
|
||||
|
||||
while.outer true {
|
||||
while this.pending_pad > 0 {
|
||||
if args.src.length() <= 0 {
|
||||
return "@internal note: short read"
|
||||
}
|
||||
this.pending_pad -= 1
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
} endwhile
|
||||
while.outer true {
|
||||
while this.pending_pad > 0 {
|
||||
if args.src.length() <= 0 {
|
||||
return "@internal note: short read"
|
||||
}
|
||||
this.pending_pad -= 1
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
} endwhile
|
||||
|
||||
while.inner true {
|
||||
if this.dst_x == this.width {
|
||||
this.dst_x = 0
|
||||
this.dst_y ~mod+= this.dst_y_inc
|
||||
if this.dst_y >= this.height {
|
||||
if this.height > 0 {
|
||||
this.pending_pad = this.pad_per_row
|
||||
}
|
||||
break.outer
|
||||
} else if this.pad_per_row <> 0 {
|
||||
this.pending_pad = this.pad_per_row
|
||||
continue.outer
|
||||
}
|
||||
}
|
||||
while.inner true {
|
||||
if this.dst_x == this.width {
|
||||
this.dst_x = 0
|
||||
this.dst_y ~mod+= this.dst_y_inc
|
||||
if this.dst_y >= this.height {
|
||||
if this.height > 0 {
|
||||
this.pending_pad = this.pad_per_row
|
||||
}
|
||||
break.outer
|
||||
} else if this.pad_per_row <> 0 {
|
||||
this.pending_pad = this.pad_per_row
|
||||
continue.outer
|
||||
}
|
||||
}
|
||||
|
||||
// -------- BEGIN convert to PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE.
|
||||
p1_temp = this.width ~mod- this.dst_x
|
||||
p1 = p1_temp.min(a: 256)
|
||||
p0 = 0
|
||||
while p0 < p1 {
|
||||
assert p0 < 256 via "a < b: a < c; c <= b"(c: p1)
|
||||
if this.bits_per_pixel == 16 {
|
||||
if args.src.length() < 2 {
|
||||
break
|
||||
}
|
||||
c32 = args.src.peek_u16le_as_u32()
|
||||
args.src.skip_u32_fast!(actual: 2, worst_case: 2)
|
||||
} else {
|
||||
if args.src.length() < 4 {
|
||||
break
|
||||
}
|
||||
c32 = args.src.peek_u32le()
|
||||
args.src.skip_u32_fast!(actual: 4, worst_case: 4)
|
||||
}
|
||||
// -------- BEGIN convert to PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE.
|
||||
p1_temp = this.width ~mod- this.dst_x
|
||||
p1 = p1_temp.min(a: 256)
|
||||
p0 = 0
|
||||
while p0 < p1 {
|
||||
assert p0 < 256 via "a < b: a < c; c <= b"(c: p1)
|
||||
if this.bits_per_pixel == 16 {
|
||||
if args.src.length() < 2 {
|
||||
break
|
||||
}
|
||||
c32 = args.src.peek_u16le_as_u32()
|
||||
args.src.skip_u32_fast!(actual: 2, worst_case: 2)
|
||||
} else {
|
||||
if args.src.length() < 4 {
|
||||
break
|
||||
}
|
||||
c32 = args.src.peek_u32le()
|
||||
args.src.skip_u32_fast!(actual: 4, worst_case: 4)
|
||||
}
|
||||
|
||||
channel = 0
|
||||
while channel < 4,
|
||||
inv p0 < 256,
|
||||
{
|
||||
if this.channel_num_bits[channel] == 0 {
|
||||
this.scratch[(8 * p0) + (2 * channel) + 0] = 0xFF
|
||||
this.scratch[(8 * p0) + (2 * channel) + 1] = 0xFF
|
||||
} else {
|
||||
c = (c32 & this.channel_masks[channel]) >> this.channel_shifts[channel]
|
||||
num_bits = this.channel_num_bits[channel] as base.u32
|
||||
while num_bits < 16,
|
||||
inv p0 < 256,
|
||||
inv channel < 4,
|
||||
post num_bits >= 16,
|
||||
{
|
||||
c |= c ~mod<< num_bits
|
||||
num_bits *= 2
|
||||
} endwhile
|
||||
c >>= num_bits - 16
|
||||
this.scratch[(8 * p0) + (2 * channel) + 0] = (0xFF & (c >> 0)) as base.u8
|
||||
this.scratch[(8 * p0) + (2 * channel) + 1] = (0xFF & (c >> 8)) as base.u8
|
||||
}
|
||||
channel = 0
|
||||
while channel < 4,
|
||||
inv p0 < 256,
|
||||
{
|
||||
if this.channel_num_bits[channel] == 0 {
|
||||
this.scratch[(8 * p0) + (2 * channel) + 0] = 0xFF
|
||||
this.scratch[(8 * p0) + (2 * channel) + 1] = 0xFF
|
||||
} else {
|
||||
c = (c32 & this.channel_masks[channel]) >> this.channel_shifts[channel]
|
||||
num_bits = this.channel_num_bits[channel] as base.u32
|
||||
while num_bits < 16,
|
||||
inv p0 < 256,
|
||||
inv channel < 4,
|
||||
post num_bits >= 16,
|
||||
{
|
||||
c |= c ~mod<< num_bits
|
||||
num_bits *= 2
|
||||
} endwhile
|
||||
c >>= num_bits - 16
|
||||
this.scratch[(8 * p0) + (2 * channel) + 0] = (0xFF & (c >> 0)) as base.u8
|
||||
this.scratch[(8 * p0) + (2 * channel) + 1] = (0xFF & (c >> 8)) as base.u8
|
||||
}
|
||||
|
||||
channel += 1
|
||||
} endwhile
|
||||
channel += 1
|
||||
} endwhile
|
||||
|
||||
p0 += 1
|
||||
} endwhile
|
||||
// -------- END convert to PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE.
|
||||
p0 += 1
|
||||
} endwhile
|
||||
// -------- END convert to PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE.
|
||||
|
||||
dst = tab.row_u32(y: this.dst_y)
|
||||
if dst_bytes_per_row < dst.length() {
|
||||
dst = dst[.. dst_bytes_per_row]
|
||||
}
|
||||
i = (this.dst_x as base.u64) * dst_bytes_per_pixel
|
||||
if i >= dst.length() {
|
||||
n = p0 as base.u64
|
||||
} else {
|
||||
n = this.swizzler.swizzle_interleaved_from_slice!(
|
||||
dst: dst[i ..],
|
||||
dst_palette: dst_palette,
|
||||
src: this.scratch[.. 8 * p0])
|
||||
}
|
||||
if n == 0 {
|
||||
return "@internal note: short read"
|
||||
}
|
||||
this.dst_x ~sat+= (n & 0xFFFF_FFFF) as base.u32
|
||||
} endwhile.inner
|
||||
} endwhile.outer
|
||||
dst = tab.row_u32(y: this.dst_y)
|
||||
if dst_bytes_per_row < dst.length() {
|
||||
dst = dst[.. dst_bytes_per_row]
|
||||
}
|
||||
i = (this.dst_x as base.u64) * dst_bytes_per_pixel
|
||||
if i >= dst.length() {
|
||||
n = p0 as base.u64
|
||||
} else {
|
||||
n = this.swizzler.swizzle_interleaved_from_slice!(
|
||||
dst: dst[i ..],
|
||||
dst_palette: dst_palette,
|
||||
src: this.scratch[.. 8 * p0])
|
||||
}
|
||||
if n == 0 {
|
||||
return "@internal note: short read"
|
||||
}
|
||||
this.dst_x ~sat+= (n & 0xFFFF_FFFF) as base.u32
|
||||
} endwhile.inner
|
||||
} endwhile.outer
|
||||
|
||||
return ok
|
||||
return ok
|
||||
}
|
||||
|
||||
pri func decoder.swizzle_low_bit_depth!(dst: ptr base.pixel_buffer, src: base.io_reader) base.status {
|
||||
var dst_pixfmt : base.pixel_format
|
||||
var dst_bits_per_pixel : base.u32[..= 256]
|
||||
var dst_bytes_per_pixel : base.u64[..= 32]
|
||||
var dst_bytes_per_row : base.u64
|
||||
var dst_palette : slice base.u8
|
||||
var tab : table base.u8
|
||||
var dst : slice base.u8
|
||||
var i : base.u64
|
||||
var n : base.u64
|
||||
var dst_pixfmt : base.pixel_format
|
||||
var dst_bits_per_pixel : base.u32[..= 256]
|
||||
var dst_bytes_per_pixel : base.u64[..= 32]
|
||||
var dst_bytes_per_row : base.u64
|
||||
var dst_palette : slice base.u8
|
||||
var tab : table base.u8
|
||||
var dst : slice base.u8
|
||||
var i : base.u64
|
||||
var n : base.u64
|
||||
|
||||
var p0 : base.u32[..= 543]
|
||||
var p0 : base.u32[..= 543]
|
||||
|
||||
var chunk_bits : base.u32
|
||||
var chunk_count : base.u32
|
||||
var pixels_per_chunk : base.u32[..= 32]
|
||||
var chunk_bits : base.u32
|
||||
var chunk_count : base.u32
|
||||
var pixels_per_chunk : base.u32[..= 32]
|
||||
|
||||
// TODO: the dst_pixfmt variable shouldn't be necessary. We should be able
|
||||
// to chain the two calls: "args.dst.pixel_format().bits_per_pixel()".
|
||||
dst_pixfmt = args.dst.pixel_format()
|
||||
dst_bits_per_pixel = dst_pixfmt.bits_per_pixel()
|
||||
if (dst_bits_per_pixel & 7) <> 0 {
|
||||
return base."#unsupported option"
|
||||
}
|
||||
dst_bytes_per_pixel = (dst_bits_per_pixel / 8) as base.u64
|
||||
dst_bytes_per_row = (this.width as base.u64) * dst_bytes_per_pixel
|
||||
dst_palette = args.dst.palette_or_else(fallback: this.scratch[1024 ..])
|
||||
tab = args.dst.plane(p: 0)
|
||||
// TODO: the dst_pixfmt variable shouldn't be necessary. We should be able
|
||||
// to chain the two calls: "args.dst.pixel_format().bits_per_pixel()".
|
||||
dst_pixfmt = args.dst.pixel_format()
|
||||
dst_bits_per_pixel = dst_pixfmt.bits_per_pixel()
|
||||
if (dst_bits_per_pixel & 7) <> 0 {
|
||||
return base."#unsupported option"
|
||||
}
|
||||
dst_bytes_per_pixel = (dst_bits_per_pixel / 8) as base.u64
|
||||
dst_bytes_per_row = (this.width as base.u64) * dst_bytes_per_pixel
|
||||
dst_palette = args.dst.palette_or_else(fallback: this.scratch[1024 ..])
|
||||
tab = args.dst.plane(p: 0)
|
||||
|
||||
while.loop true {
|
||||
if this.dst_x == this.width {
|
||||
this.dst_x = 0
|
||||
this.dst_y ~mod+= this.dst_y_inc
|
||||
if this.dst_y >= this.height {
|
||||
break.loop
|
||||
}
|
||||
}
|
||||
while.loop true {
|
||||
if this.dst_x == this.width {
|
||||
this.dst_x = 0
|
||||
this.dst_y ~mod+= this.dst_y_inc
|
||||
if this.dst_y >= this.height {
|
||||
break.loop
|
||||
}
|
||||
}
|
||||
|
||||
dst = tab.row_u32(y: this.dst_y)
|
||||
if dst_bytes_per_row < dst.length() {
|
||||
dst = dst[.. dst_bytes_per_row]
|
||||
}
|
||||
i = (this.dst_x as base.u64) * dst_bytes_per_pixel
|
||||
if i >= dst.length() {
|
||||
if this.bits_per_pixel == 1 {
|
||||
chunk_count = ((this.width ~sat- this.dst_x) + 31) / 32
|
||||
pixels_per_chunk = 32
|
||||
} else if this.bits_per_pixel == 2 {
|
||||
chunk_count = ((this.width ~sat- this.dst_x) + 15) / 16
|
||||
pixels_per_chunk = 16
|
||||
} else {
|
||||
chunk_count = ((this.width ~sat- this.dst_x) + 7) / 8
|
||||
pixels_per_chunk = 8
|
||||
}
|
||||
while (chunk_count >= 64) and (args.src.length() >= 256) {
|
||||
args.src.skip_u32_fast!(actual: 256, worst_case: 256)
|
||||
this.dst_x = this.width.min(a: this.dst_x ~mod+ (pixels_per_chunk * 64))
|
||||
chunk_count -= 64
|
||||
} endwhile
|
||||
while (chunk_count >= 8) and (args.src.length() >= 32) {
|
||||
args.src.skip_u32_fast!(actual: 32, worst_case: 32)
|
||||
this.dst_x = this.width.min(a: this.dst_x ~mod+ (pixels_per_chunk * 8))
|
||||
chunk_count -= 8
|
||||
} endwhile
|
||||
while chunk_count > 0 {
|
||||
if args.src.length() < 4 {
|
||||
return "@internal note: short read"
|
||||
}
|
||||
args.src.skip_u32_fast!(actual: 4, worst_case: 4)
|
||||
this.dst_x = this.width.min(a: this.dst_x ~mod+ (pixels_per_chunk * 1))
|
||||
chunk_count -= 1
|
||||
} endwhile
|
||||
continue.loop
|
||||
}
|
||||
dst = dst[i ..]
|
||||
p0 = 0
|
||||
dst = tab.row_u32(y: this.dst_y)
|
||||
if dst_bytes_per_row < dst.length() {
|
||||
dst = dst[.. dst_bytes_per_row]
|
||||
}
|
||||
i = (this.dst_x as base.u64) * dst_bytes_per_pixel
|
||||
if i >= dst.length() {
|
||||
if this.bits_per_pixel == 1 {
|
||||
chunk_count = ((this.width ~sat- this.dst_x) + 31) / 32
|
||||
pixels_per_chunk = 32
|
||||
} else if this.bits_per_pixel == 2 {
|
||||
chunk_count = ((this.width ~sat- this.dst_x) + 15) / 16
|
||||
pixels_per_chunk = 16
|
||||
} else {
|
||||
chunk_count = ((this.width ~sat- this.dst_x) + 7) / 8
|
||||
pixels_per_chunk = 8
|
||||
}
|
||||
while (chunk_count >= 64) and (args.src.length() >= 256) {
|
||||
args.src.skip_u32_fast!(actual: 256, worst_case: 256)
|
||||
this.dst_x = this.width.min(a: this.dst_x ~mod+ (pixels_per_chunk * 64))
|
||||
chunk_count -= 64
|
||||
} endwhile
|
||||
while (chunk_count >= 8) and (args.src.length() >= 32) {
|
||||
args.src.skip_u32_fast!(actual: 32, worst_case: 32)
|
||||
this.dst_x = this.width.min(a: this.dst_x ~mod+ (pixels_per_chunk * 8))
|
||||
chunk_count -= 8
|
||||
} endwhile
|
||||
while chunk_count > 0 {
|
||||
if args.src.length() < 4 {
|
||||
return "@internal note: short read"
|
||||
}
|
||||
args.src.skip_u32_fast!(actual: 4, worst_case: 4)
|
||||
this.dst_x = this.width.min(a: this.dst_x ~mod+ (pixels_per_chunk * 1))
|
||||
chunk_count -= 1
|
||||
} endwhile
|
||||
continue.loop
|
||||
}
|
||||
dst = dst[i ..]
|
||||
p0 = 0
|
||||
|
||||
if this.bits_per_pixel == 1 {
|
||||
// Calculate the remaining number of 32-bit chunks. At 1 bit per
|
||||
// pixel there are 32 pixels per chunk. Division rounds up.
|
||||
chunk_count = ((this.width ~sat- this.dst_x) + 31) / 32
|
||||
chunk_count = chunk_count.min(a: 16) // Keep p0 <= 512.
|
||||
while (chunk_count > 0) and (args.src.length() >= 4) {
|
||||
chunk_bits = args.src.peek_u32be()
|
||||
args.src.skip_u32_fast!(actual: 4, worst_case: 4)
|
||||
this.scratch[p0 + 0x00] = (0x01 & (chunk_bits >> 0x1F)) as base.u8
|
||||
this.scratch[p0 + 0x01] = (0x01 & (chunk_bits >> 0x1E)) as base.u8
|
||||
this.scratch[p0 + 0x02] = (0x01 & (chunk_bits >> 0x1D)) as base.u8
|
||||
this.scratch[p0 + 0x03] = (0x01 & (chunk_bits >> 0x1C)) as base.u8
|
||||
this.scratch[p0 + 0x04] = (0x01 & (chunk_bits >> 0x1B)) as base.u8
|
||||
this.scratch[p0 + 0x05] = (0x01 & (chunk_bits >> 0x1A)) as base.u8
|
||||
this.scratch[p0 + 0x06] = (0x01 & (chunk_bits >> 0x19)) as base.u8
|
||||
this.scratch[p0 + 0x07] = (0x01 & (chunk_bits >> 0x18)) as base.u8
|
||||
this.scratch[p0 + 0x08] = (0x01 & (chunk_bits >> 0x17)) as base.u8
|
||||
this.scratch[p0 + 0x09] = (0x01 & (chunk_bits >> 0x16)) as base.u8
|
||||
this.scratch[p0 + 0x0A] = (0x01 & (chunk_bits >> 0x15)) as base.u8
|
||||
this.scratch[p0 + 0x0B] = (0x01 & (chunk_bits >> 0x14)) as base.u8
|
||||
this.scratch[p0 + 0x0C] = (0x01 & (chunk_bits >> 0x13)) as base.u8
|
||||
this.scratch[p0 + 0x0D] = (0x01 & (chunk_bits >> 0x12)) as base.u8
|
||||
this.scratch[p0 + 0x0E] = (0x01 & (chunk_bits >> 0x11)) as base.u8
|
||||
this.scratch[p0 + 0x0F] = (0x01 & (chunk_bits >> 0x10)) as base.u8
|
||||
this.scratch[p0 + 0x10] = (0x01 & (chunk_bits >> 0x0F)) as base.u8
|
||||
this.scratch[p0 + 0x11] = (0x01 & (chunk_bits >> 0x0E)) as base.u8
|
||||
this.scratch[p0 + 0x12] = (0x01 & (chunk_bits >> 0x0D)) as base.u8
|
||||
this.scratch[p0 + 0x13] = (0x01 & (chunk_bits >> 0x0C)) as base.u8
|
||||
this.scratch[p0 + 0x14] = (0x01 & (chunk_bits >> 0x0B)) as base.u8
|
||||
this.scratch[p0 + 0x15] = (0x01 & (chunk_bits >> 0x0A)) as base.u8
|
||||
this.scratch[p0 + 0x16] = (0x01 & (chunk_bits >> 0x09)) as base.u8
|
||||
this.scratch[p0 + 0x17] = (0x01 & (chunk_bits >> 0x08)) as base.u8
|
||||
this.scratch[p0 + 0x18] = (0x01 & (chunk_bits >> 0x07)) as base.u8
|
||||
this.scratch[p0 + 0x19] = (0x01 & (chunk_bits >> 0x06)) as base.u8
|
||||
this.scratch[p0 + 0x1A] = (0x01 & (chunk_bits >> 0x05)) as base.u8
|
||||
this.scratch[p0 + 0x1B] = (0x01 & (chunk_bits >> 0x04)) as base.u8
|
||||
this.scratch[p0 + 0x1C] = (0x01 & (chunk_bits >> 0x03)) as base.u8
|
||||
this.scratch[p0 + 0x1D] = (0x01 & (chunk_bits >> 0x02)) as base.u8
|
||||
this.scratch[p0 + 0x1E] = (0x01 & (chunk_bits >> 0x01)) as base.u8
|
||||
this.scratch[p0 + 0x1F] = (0x01 & (chunk_bits >> 0x00)) as base.u8
|
||||
p0 = (p0 & 511) + 0x20
|
||||
chunk_count -= 1
|
||||
} endwhile
|
||||
if this.bits_per_pixel == 1 {
|
||||
// Calculate the remaining number of 32-bit chunks. At 1 bit per
|
||||
// pixel there are 32 pixels per chunk. Division rounds up.
|
||||
chunk_count = ((this.width ~sat- this.dst_x) + 31) / 32
|
||||
chunk_count = chunk_count.min(a: 16) // Keep p0 <= 512.
|
||||
while (chunk_count > 0) and (args.src.length() >= 4) {
|
||||
chunk_bits = args.src.peek_u32be()
|
||||
args.src.skip_u32_fast!(actual: 4, worst_case: 4)
|
||||
this.scratch[p0 + 0x00] = (0x01 & (chunk_bits >> 0x1F)) as base.u8
|
||||
this.scratch[p0 + 0x01] = (0x01 & (chunk_bits >> 0x1E)) as base.u8
|
||||
this.scratch[p0 + 0x02] = (0x01 & (chunk_bits >> 0x1D)) as base.u8
|
||||
this.scratch[p0 + 0x03] = (0x01 & (chunk_bits >> 0x1C)) as base.u8
|
||||
this.scratch[p0 + 0x04] = (0x01 & (chunk_bits >> 0x1B)) as base.u8
|
||||
this.scratch[p0 + 0x05] = (0x01 & (chunk_bits >> 0x1A)) as base.u8
|
||||
this.scratch[p0 + 0x06] = (0x01 & (chunk_bits >> 0x19)) as base.u8
|
||||
this.scratch[p0 + 0x07] = (0x01 & (chunk_bits >> 0x18)) as base.u8
|
||||
this.scratch[p0 + 0x08] = (0x01 & (chunk_bits >> 0x17)) as base.u8
|
||||
this.scratch[p0 + 0x09] = (0x01 & (chunk_bits >> 0x16)) as base.u8
|
||||
this.scratch[p0 + 0x0A] = (0x01 & (chunk_bits >> 0x15)) as base.u8
|
||||
this.scratch[p0 + 0x0B] = (0x01 & (chunk_bits >> 0x14)) as base.u8
|
||||
this.scratch[p0 + 0x0C] = (0x01 & (chunk_bits >> 0x13)) as base.u8
|
||||
this.scratch[p0 + 0x0D] = (0x01 & (chunk_bits >> 0x12)) as base.u8
|
||||
this.scratch[p0 + 0x0E] = (0x01 & (chunk_bits >> 0x11)) as base.u8
|
||||
this.scratch[p0 + 0x0F] = (0x01 & (chunk_bits >> 0x10)) as base.u8
|
||||
this.scratch[p0 + 0x10] = (0x01 & (chunk_bits >> 0x0F)) as base.u8
|
||||
this.scratch[p0 + 0x11] = (0x01 & (chunk_bits >> 0x0E)) as base.u8
|
||||
this.scratch[p0 + 0x12] = (0x01 & (chunk_bits >> 0x0D)) as base.u8
|
||||
this.scratch[p0 + 0x13] = (0x01 & (chunk_bits >> 0x0C)) as base.u8
|
||||
this.scratch[p0 + 0x14] = (0x01 & (chunk_bits >> 0x0B)) as base.u8
|
||||
this.scratch[p0 + 0x15] = (0x01 & (chunk_bits >> 0x0A)) as base.u8
|
||||
this.scratch[p0 + 0x16] = (0x01 & (chunk_bits >> 0x09)) as base.u8
|
||||
this.scratch[p0 + 0x17] = (0x01 & (chunk_bits >> 0x08)) as base.u8
|
||||
this.scratch[p0 + 0x18] = (0x01 & (chunk_bits >> 0x07)) as base.u8
|
||||
this.scratch[p0 + 0x19] = (0x01 & (chunk_bits >> 0x06)) as base.u8
|
||||
this.scratch[p0 + 0x1A] = (0x01 & (chunk_bits >> 0x05)) as base.u8
|
||||
this.scratch[p0 + 0x1B] = (0x01 & (chunk_bits >> 0x04)) as base.u8
|
||||
this.scratch[p0 + 0x1C] = (0x01 & (chunk_bits >> 0x03)) as base.u8
|
||||
this.scratch[p0 + 0x1D] = (0x01 & (chunk_bits >> 0x02)) as base.u8
|
||||
this.scratch[p0 + 0x1E] = (0x01 & (chunk_bits >> 0x01)) as base.u8
|
||||
this.scratch[p0 + 0x1F] = (0x01 & (chunk_bits >> 0x00)) as base.u8
|
||||
p0 = (p0 & 511) + 0x20
|
||||
chunk_count -= 1
|
||||
} endwhile
|
||||
|
||||
} else if this.bits_per_pixel == 2 {
|
||||
// Calculate the remaining number of 32-bit chunks. At 2 bits per
|
||||
// pixel there are 16 pixels per chunk. Division rounds up.
|
||||
chunk_count = ((this.width ~sat- this.dst_x) + 15) / 16
|
||||
chunk_count = chunk_count.min(a: 32) // Keep p0 <= 512.
|
||||
while (chunk_count > 0) and (args.src.length() >= 4) {
|
||||
chunk_bits = args.src.peek_u32be()
|
||||
args.src.skip_u32_fast!(actual: 4, worst_case: 4)
|
||||
this.scratch[p0 + 0x00] = (0x03 & (chunk_bits >> 0x1E)) as base.u8
|
||||
this.scratch[p0 + 0x01] = (0x03 & (chunk_bits >> 0x1C)) as base.u8
|
||||
this.scratch[p0 + 0x02] = (0x03 & (chunk_bits >> 0x1A)) as base.u8
|
||||
this.scratch[p0 + 0x03] = (0x03 & (chunk_bits >> 0x18)) as base.u8
|
||||
this.scratch[p0 + 0x04] = (0x03 & (chunk_bits >> 0x16)) as base.u8
|
||||
this.scratch[p0 + 0x05] = (0x03 & (chunk_bits >> 0x14)) as base.u8
|
||||
this.scratch[p0 + 0x06] = (0x03 & (chunk_bits >> 0x12)) as base.u8
|
||||
this.scratch[p0 + 0x07] = (0x03 & (chunk_bits >> 0x10)) as base.u8
|
||||
this.scratch[p0 + 0x08] = (0x03 & (chunk_bits >> 0x0E)) as base.u8
|
||||
this.scratch[p0 + 0x09] = (0x03 & (chunk_bits >> 0x0C)) as base.u8
|
||||
this.scratch[p0 + 0x0A] = (0x03 & (chunk_bits >> 0x0A)) as base.u8
|
||||
this.scratch[p0 + 0x0B] = (0x03 & (chunk_bits >> 0x08)) as base.u8
|
||||
this.scratch[p0 + 0x0C] = (0x03 & (chunk_bits >> 0x06)) as base.u8
|
||||
this.scratch[p0 + 0x0D] = (0x03 & (chunk_bits >> 0x04)) as base.u8
|
||||
this.scratch[p0 + 0x0E] = (0x03 & (chunk_bits >> 0x02)) as base.u8
|
||||
this.scratch[p0 + 0x0F] = (0x03 & (chunk_bits >> 0x00)) as base.u8
|
||||
p0 = (p0 & 511) + 0x10
|
||||
chunk_count -= 1
|
||||
} endwhile
|
||||
} else if this.bits_per_pixel == 2 {
|
||||
// Calculate the remaining number of 32-bit chunks. At 2 bits per
|
||||
// pixel there are 16 pixels per chunk. Division rounds up.
|
||||
chunk_count = ((this.width ~sat- this.dst_x) + 15) / 16
|
||||
chunk_count = chunk_count.min(a: 32) // Keep p0 <= 512.
|
||||
while (chunk_count > 0) and (args.src.length() >= 4) {
|
||||
chunk_bits = args.src.peek_u32be()
|
||||
args.src.skip_u32_fast!(actual: 4, worst_case: 4)
|
||||
this.scratch[p0 + 0x00] = (0x03 & (chunk_bits >> 0x1E)) as base.u8
|
||||
this.scratch[p0 + 0x01] = (0x03 & (chunk_bits >> 0x1C)) as base.u8
|
||||
this.scratch[p0 + 0x02] = (0x03 & (chunk_bits >> 0x1A)) as base.u8
|
||||
this.scratch[p0 + 0x03] = (0x03 & (chunk_bits >> 0x18)) as base.u8
|
||||
this.scratch[p0 + 0x04] = (0x03 & (chunk_bits >> 0x16)) as base.u8
|
||||
this.scratch[p0 + 0x05] = (0x03 & (chunk_bits >> 0x14)) as base.u8
|
||||
this.scratch[p0 + 0x06] = (0x03 & (chunk_bits >> 0x12)) as base.u8
|
||||
this.scratch[p0 + 0x07] = (0x03 & (chunk_bits >> 0x10)) as base.u8
|
||||
this.scratch[p0 + 0x08] = (0x03 & (chunk_bits >> 0x0E)) as base.u8
|
||||
this.scratch[p0 + 0x09] = (0x03 & (chunk_bits >> 0x0C)) as base.u8
|
||||
this.scratch[p0 + 0x0A] = (0x03 & (chunk_bits >> 0x0A)) as base.u8
|
||||
this.scratch[p0 + 0x0B] = (0x03 & (chunk_bits >> 0x08)) as base.u8
|
||||
this.scratch[p0 + 0x0C] = (0x03 & (chunk_bits >> 0x06)) as base.u8
|
||||
this.scratch[p0 + 0x0D] = (0x03 & (chunk_bits >> 0x04)) as base.u8
|
||||
this.scratch[p0 + 0x0E] = (0x03 & (chunk_bits >> 0x02)) as base.u8
|
||||
this.scratch[p0 + 0x0F] = (0x03 & (chunk_bits >> 0x00)) as base.u8
|
||||
p0 = (p0 & 511) + 0x10
|
||||
chunk_count -= 1
|
||||
} endwhile
|
||||
|
||||
} else {
|
||||
// Calculate the remaining number of 32-bit chunks. At 4 bits per
|
||||
// pixel there are 8 pixels per chunk. Division rounds up.
|
||||
chunk_count = ((this.width ~sat- this.dst_x) + 7) / 8
|
||||
chunk_count = chunk_count.min(a: 64) // Keep p0 <= 512.
|
||||
while (chunk_count > 0) and (args.src.length() >= 4) {
|
||||
chunk_bits = args.src.peek_u32be()
|
||||
args.src.skip_u32_fast!(actual: 4, worst_case: 4)
|
||||
this.scratch[p0 + 0x00] = (0x0F & (chunk_bits >> 0x1C)) as base.u8
|
||||
this.scratch[p0 + 0x01] = (0x0F & (chunk_bits >> 0x18)) as base.u8
|
||||
this.scratch[p0 + 0x02] = (0x0F & (chunk_bits >> 0x14)) as base.u8
|
||||
this.scratch[p0 + 0x03] = (0x0F & (chunk_bits >> 0x10)) as base.u8
|
||||
this.scratch[p0 + 0x04] = (0x0F & (chunk_bits >> 0x0C)) as base.u8
|
||||
this.scratch[p0 + 0x05] = (0x0F & (chunk_bits >> 0x08)) as base.u8
|
||||
this.scratch[p0 + 0x06] = (0x0F & (chunk_bits >> 0x04)) as base.u8
|
||||
this.scratch[p0 + 0x07] = (0x0F & (chunk_bits >> 0x00)) as base.u8
|
||||
p0 = (p0 & 511) + 0x08
|
||||
chunk_count -= 1
|
||||
} endwhile
|
||||
}
|
||||
} else {
|
||||
// Calculate the remaining number of 32-bit chunks. At 4 bits per
|
||||
// pixel there are 8 pixels per chunk. Division rounds up.
|
||||
chunk_count = ((this.width ~sat- this.dst_x) + 7) / 8
|
||||
chunk_count = chunk_count.min(a: 64) // Keep p0 <= 512.
|
||||
while (chunk_count > 0) and (args.src.length() >= 4) {
|
||||
chunk_bits = args.src.peek_u32be()
|
||||
args.src.skip_u32_fast!(actual: 4, worst_case: 4)
|
||||
this.scratch[p0 + 0x00] = (0x0F & (chunk_bits >> 0x1C)) as base.u8
|
||||
this.scratch[p0 + 0x01] = (0x0F & (chunk_bits >> 0x18)) as base.u8
|
||||
this.scratch[p0 + 0x02] = (0x0F & (chunk_bits >> 0x14)) as base.u8
|
||||
this.scratch[p0 + 0x03] = (0x0F & (chunk_bits >> 0x10)) as base.u8
|
||||
this.scratch[p0 + 0x04] = (0x0F & (chunk_bits >> 0x0C)) as base.u8
|
||||
this.scratch[p0 + 0x05] = (0x0F & (chunk_bits >> 0x08)) as base.u8
|
||||
this.scratch[p0 + 0x06] = (0x0F & (chunk_bits >> 0x04)) as base.u8
|
||||
this.scratch[p0 + 0x07] = (0x0F & (chunk_bits >> 0x00)) as base.u8
|
||||
p0 = (p0 & 511) + 0x08
|
||||
chunk_count -= 1
|
||||
} endwhile
|
||||
}
|
||||
|
||||
p0 = p0.min(a: this.width ~sat- this.dst_x)
|
||||
n = this.swizzler.swizzle_interleaved_from_slice!(
|
||||
dst: dst,
|
||||
dst_palette: dst_palette,
|
||||
src: this.scratch[.. p0])
|
||||
if n == 0 {
|
||||
return "@internal note: short read"
|
||||
}
|
||||
this.dst_x ~sat+= (n & 0xFFFF_FFFF) as base.u32
|
||||
} endwhile.loop
|
||||
p0 = p0.min(a: this.width ~sat- this.dst_x)
|
||||
n = this.swizzler.swizzle_interleaved_from_slice!(
|
||||
dst: dst,
|
||||
dst_palette: dst_palette,
|
||||
src: this.scratch[.. p0])
|
||||
if n == 0 {
|
||||
return "@internal note: short read"
|
||||
}
|
||||
this.dst_x ~sat+= (n & 0xFFFF_FFFF) as base.u32
|
||||
} endwhile.loop
|
||||
|
||||
return ok
|
||||
return ok
|
||||
}
|
||||
|
||||
pub func decoder.frame_dirty_rect() base.rect_ie_u32 {
|
||||
return this.util.make_rect_ie_u32(
|
||||
min_incl_x: 0,
|
||||
min_incl_y: 0,
|
||||
max_excl_x: this.width,
|
||||
max_excl_y: this.height)
|
||||
return this.util.make_rect_ie_u32(
|
||||
min_incl_x: 0,
|
||||
min_incl_y: 0,
|
||||
max_excl_x: this.width,
|
||||
max_excl_y: this.height)
|
||||
}
|
||||
|
||||
pub func decoder.num_animation_loops() base.u32 {
|
||||
return 0
|
||||
return 0
|
||||
}
|
||||
|
||||
pub func decoder.num_decoded_frame_configs() base.u64 {
|
||||
if this.call_sequence > 0x20 {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
if this.call_sequence > 0x20 {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
pub func decoder.num_decoded_frames() base.u64 {
|
||||
if this.call_sequence > 0x40 {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
if this.call_sequence > 0x40 {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
pub func decoder.restart_frame!(index: base.u64, io_position: base.u64) base.status {
|
||||
if this.call_sequence < 0x20 {
|
||||
return base."#bad call sequence"
|
||||
}
|
||||
if args.index <> 0 {
|
||||
return base."#bad argument"
|
||||
}
|
||||
this.call_sequence = 0x28
|
||||
this.frame_config_io_position = args.io_position
|
||||
return ok
|
||||
if this.call_sequence < 0x20 {
|
||||
return base."#bad call sequence"
|
||||
}
|
||||
if args.index <> 0 {
|
||||
return base."#bad argument"
|
||||
}
|
||||
this.call_sequence = 0x28
|
||||
this.frame_config_io_position = args.io_position
|
||||
return ok
|
||||
}
|
||||
|
||||
pub func decoder.set_report_metadata!(fourcc: base.u32, report: base.bool) {
|
||||
// No-op. BMP doesn't support metadata.
|
||||
// No-op. BMP doesn't support metadata.
|
||||
}
|
||||
|
||||
pub func decoder.tell_me_more?(dst: base.io_writer, minfo: nptr base.more_information, src: base.io_reader) {
|
||||
var status : base.status
|
||||
var status : base.status
|
||||
|
||||
while true {
|
||||
status =? this.do_tell_me_more?(dst: args.dst, minfo: args.minfo, src: args.src)
|
||||
if (status == base."$short read") and args.src.is_closed() {
|
||||
return "#truncated input"
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
while true {
|
||||
status =? this.do_tell_me_more?(dst: args.dst, minfo: args.minfo, src: args.src)
|
||||
if (status == base."$short read") and args.src.is_closed() {
|
||||
return "#truncated input"
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
}
|
||||
|
||||
pri func decoder.do_tell_me_more?(dst: base.io_writer, minfo: nptr base.more_information, src: base.io_reader) {
|
||||
if this.io_redirect_fourcc <= 1 {
|
||||
return base."#no more information"
|
||||
}
|
||||
if args.minfo <> nullptr {
|
||||
args.minfo.set!(
|
||||
flavor: base.MORE_INFORMATION__FLAVOR__IO_REDIRECT,
|
||||
w: this.io_redirect_fourcc,
|
||||
x: 0,
|
||||
y: this.io_redirect_pos,
|
||||
z: 0xFFFF_FFFF_FFFF_FFFF)
|
||||
}
|
||||
// Setting io_redirect_fourcc to a placeholder value of 1 will cause future
|
||||
// calls to return an error.
|
||||
this.io_redirect_fourcc = 1
|
||||
if this.io_redirect_fourcc <= 1 {
|
||||
return base."#no more information"
|
||||
}
|
||||
if args.minfo <> nullptr {
|
||||
args.minfo.set!(
|
||||
flavor: base.MORE_INFORMATION__FLAVOR__IO_REDIRECT,
|
||||
w: this.io_redirect_fourcc,
|
||||
x: 0,
|
||||
y: this.io_redirect_pos,
|
||||
z: 0xFFFF_FFFF_FFFF_FFFF)
|
||||
}
|
||||
// Setting io_redirect_fourcc to a placeholder value of 1 will cause future
|
||||
// calls to return an error.
|
||||
this.io_redirect_fourcc = 1
|
||||
}
|
||||
|
||||
pub func decoder.workbuf_len() base.range_ii_u64 {
|
||||
return this.util.make_range_ii_u64(min_incl: 0, max_incl: 0)
|
||||
return this.util.make_range_ii_u64(min_incl: 0, max_incl: 0)
|
||||
}
|
||||
|
||||
pri func decoder.read_palette?(src: base.io_reader) {
|
||||
var i : base.u32
|
||||
var argb : base.u32
|
||||
var i : base.u32
|
||||
var argb : base.u32
|
||||
|
||||
if this.bitmap_info_len == 12 {
|
||||
while (i < 256) and (this.padding >= 3) {
|
||||
this.padding -= 3
|
||||
argb = args.src.read_u24le_as_u32?()
|
||||
argb |= 0xFF00_0000
|
||||
this.src_palette[(4 * i) + 0] = ((argb >> 0) & 0xFF) as base.u8
|
||||
this.src_palette[(4 * i) + 1] = ((argb >> 8) & 0xFF) as base.u8
|
||||
this.src_palette[(4 * i) + 2] = ((argb >> 16) & 0xFF) as base.u8
|
||||
this.src_palette[(4 * i) + 3] = ((argb >> 24) & 0xFF) as base.u8
|
||||
i += 1
|
||||
} endwhile
|
||||
} else {
|
||||
while (i < 256) and (this.padding >= 4) {
|
||||
this.padding -= 4
|
||||
argb = args.src.read_u32le?()
|
||||
argb |= 0xFF00_0000
|
||||
this.src_palette[(4 * i) + 0] = ((argb >> 0) & 0xFF) as base.u8
|
||||
this.src_palette[(4 * i) + 1] = ((argb >> 8) & 0xFF) as base.u8
|
||||
this.src_palette[(4 * i) + 2] = ((argb >> 16) & 0xFF) as base.u8
|
||||
this.src_palette[(4 * i) + 3] = ((argb >> 24) & 0xFF) as base.u8
|
||||
i += 1
|
||||
} endwhile
|
||||
}
|
||||
if this.bitmap_info_len == 12 {
|
||||
while (i < 256) and (this.padding >= 3) {
|
||||
this.padding -= 3
|
||||
argb = args.src.read_u24le_as_u32?()
|
||||
argb |= 0xFF00_0000
|
||||
this.src_palette[(4 * i) + 0] = ((argb >> 0) & 0xFF) as base.u8
|
||||
this.src_palette[(4 * i) + 1] = ((argb >> 8) & 0xFF) as base.u8
|
||||
this.src_palette[(4 * i) + 2] = ((argb >> 16) & 0xFF) as base.u8
|
||||
this.src_palette[(4 * i) + 3] = ((argb >> 24) & 0xFF) as base.u8
|
||||
i += 1
|
||||
} endwhile
|
||||
} else {
|
||||
while (i < 256) and (this.padding >= 4) {
|
||||
this.padding -= 4
|
||||
argb = args.src.read_u32le?()
|
||||
argb |= 0xFF00_0000
|
||||
this.src_palette[(4 * i) + 0] = ((argb >> 0) & 0xFF) as base.u8
|
||||
this.src_palette[(4 * i) + 1] = ((argb >> 8) & 0xFF) as base.u8
|
||||
this.src_palette[(4 * i) + 2] = ((argb >> 16) & 0xFF) as base.u8
|
||||
this.src_palette[(4 * i) + 3] = ((argb >> 24) & 0xFF) as base.u8
|
||||
i += 1
|
||||
} endwhile
|
||||
}
|
||||
|
||||
while i < 256 {
|
||||
this.src_palette[(4 * i) + 0] = 0x00
|
||||
this.src_palette[(4 * i) + 1] = 0x00
|
||||
this.src_palette[(4 * i) + 2] = 0x00
|
||||
this.src_palette[(4 * i) + 3] = 0xFF
|
||||
i += 1
|
||||
} endwhile
|
||||
while i < 256 {
|
||||
this.src_palette[(4 * i) + 0] = 0x00
|
||||
this.src_palette[(4 * i) + 1] = 0x00
|
||||
this.src_palette[(4 * i) + 2] = 0x00
|
||||
this.src_palette[(4 * i) + 3] = 0xFF
|
||||
i += 1
|
||||
} endwhile
|
||||
}
|
||||
|
||||
pri func decoder.process_masks?() {
|
||||
var i : base.u32
|
||||
var mask : base.u32
|
||||
var n : base.u32
|
||||
var i : base.u32
|
||||
var mask : base.u32
|
||||
var n : base.u32
|
||||
|
||||
while i < 4 {
|
||||
mask = this.channel_masks[i]
|
||||
if mask <> 0 {
|
||||
n = 0
|
||||
while (mask & 1) == 0,
|
||||
inv i < 4,
|
||||
{
|
||||
n ~mod+= 1
|
||||
mask >>= 1
|
||||
} endwhile
|
||||
this.channel_shifts[i] = (n & 31) as base.u8
|
||||
while i < 4 {
|
||||
mask = this.channel_masks[i]
|
||||
if mask <> 0 {
|
||||
n = 0
|
||||
while (mask & 1) == 0,
|
||||
inv i < 4,
|
||||
{
|
||||
n ~mod+= 1
|
||||
mask >>= 1
|
||||
} endwhile
|
||||
this.channel_shifts[i] = (n & 31) as base.u8
|
||||
|
||||
n = 0
|
||||
while (mask & 1) == 1,
|
||||
inv i < 4,
|
||||
{
|
||||
n ~mod+= 1
|
||||
mask >>= 1
|
||||
} endwhile
|
||||
if (mask <> 0) or (n > 32) {
|
||||
return "#bad header"
|
||||
}
|
||||
this.channel_num_bits[i] = n as base.u8
|
||||
} else if i <> 3 {
|
||||
return "#bad header"
|
||||
}
|
||||
n = 0
|
||||
while (mask & 1) == 1,
|
||||
inv i < 4,
|
||||
{
|
||||
n ~mod+= 1
|
||||
mask >>= 1
|
||||
} endwhile
|
||||
if (mask <> 0) or (n > 32) {
|
||||
return "#bad header"
|
||||
}
|
||||
this.channel_num_bits[i] = n as base.u8
|
||||
} else if i <> 3 {
|
||||
return "#bad header"
|
||||
}
|
||||
|
||||
i += 1
|
||||
} endwhile
|
||||
i += 1
|
||||
} endwhile
|
||||
}
|
||||
+619
-619
@@ -29,530 +29,530 @@ pub const DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE : base.u64 = 0
|
||||
pri const CLAMP_TO_5 : array[8] base.u8[..= 5] = [0, 1, 2, 3, 4, 5, 5, 5]
|
||||
|
||||
pub struct decoder? implements base.io_transformer(
|
||||
bits : base.u32,
|
||||
n_bits : base.u32[..= 31],
|
||||
bits : base.u32,
|
||||
n_bits : base.u32[..= 31],
|
||||
|
||||
// Block size is measured prior to the RLE1 step.
|
||||
max_incl_block_size : base.u32[..= 900000],
|
||||
// Block size is measured prior to the RLE1 step.
|
||||
max_incl_block_size : base.u32[..= 900000],
|
||||
|
||||
block_size : base.u32[..= 900000],
|
||||
decode_huffman_finished : base.bool,
|
||||
decode_huffman_which : base.u8[..= 5],
|
||||
decode_huffman_ticks : base.u32[..= 50],
|
||||
decode_huffman_section : base.u32,
|
||||
decode_huffman_run_shift : base.u32[..= 23],
|
||||
block_size : base.u32[..= 900000],
|
||||
decode_huffman_finished : base.bool,
|
||||
decode_huffman_which : base.u8[..= 5],
|
||||
decode_huffman_ticks : base.u32[..= 50],
|
||||
decode_huffman_section : base.u32,
|
||||
decode_huffman_run_shift : base.u32[..= 23],
|
||||
|
||||
flush_pointer : base.u32[..= 1_048575],
|
||||
flush_repeat_count : base.u32[..= 255],
|
||||
flush_prev : base.u8,
|
||||
flush_pointer : base.u32[..= 1_048575],
|
||||
flush_repeat_count : base.u32[..= 255],
|
||||
flush_prev : base.u8,
|
||||
|
||||
ignore_checksum : base.bool,
|
||||
final_checksum_have : base.u32,
|
||||
block_checksum_have : base.u32,
|
||||
block_checksum_want : base.u32,
|
||||
original_pointer : base.u32,
|
||||
num_symbols : base.u32[..= 258],
|
||||
num_huffman_codes : base.u32[..= 6],
|
||||
num_sections : base.u32[..= 18001],
|
||||
code_lengths_bitmask : base.u32,
|
||||
ignore_checksum : base.bool,
|
||||
final_checksum_have : base.u32,
|
||||
block_checksum_have : base.u32,
|
||||
block_checksum_want : base.u32,
|
||||
original_pointer : base.u32,
|
||||
num_symbols : base.u32[..= 258],
|
||||
num_huffman_codes : base.u32[..= 6],
|
||||
num_sections : base.u32[..= 18001],
|
||||
code_lengths_bitmask : base.u32,
|
||||
|
||||
util : base.utility,
|
||||
util : base.utility,
|
||||
)(
|
||||
scratch : base.u32,
|
||||
scratch : base.u32,
|
||||
|
||||
letter_counts : array[256] base.u32,
|
||||
letter_counts : array[256] base.u32,
|
||||
|
||||
// presence[i] being non-zero is whether the byte value i occurs in the
|
||||
// block's decompressed output.
|
||||
presence : array[256] base.u8,
|
||||
// presence[i] being non-zero is whether the byte value i occurs in the
|
||||
// block's decompressed output.
|
||||
presence : array[256] base.u8,
|
||||
|
||||
// mtft is the Move To Front Transform state.
|
||||
mtft : array[256] base.u8,
|
||||
// mtft is the Move To Front Transform state.
|
||||
mtft : array[256] base.u8,
|
||||
|
||||
// huffman_selectors selects which of the huffman_trees to use for each
|
||||
// 50-symbol section.
|
||||
huffman_selectors : array[32768] base.u8,
|
||||
// huffman_selectors selects which of the huffman_trees to use for each
|
||||
// 50-symbol section.
|
||||
huffman_selectors : array[32768] base.u8,
|
||||
|
||||
// Each array[2] base.u16 is a branch node in the Huffman tree. The
|
||||
// build_huffman_tree method comment details what the base.u16 values mean.
|
||||
//
|
||||
// Each Huffman tree has num_symbols leaf nodes and hence (num_symbols - 1)
|
||||
// branch nodes, also called internal nodes, as per
|
||||
// https://stackoverflow.com/questions/69399994/maximum-number-of-nodes-in-a-huffman-tree/69401083#69401083
|
||||
//
|
||||
// This can be proven inductively. The base case Huffman code (two symbols)
|
||||
// has one branch node (the root node). The inductive case, adding one
|
||||
// symbol to a (complete but possibly non-canonical) Huffman code replaces
|
||||
// one leaf node with one branch node (and two leaf nodes). The net gain in
|
||||
// branch nodes is +1.
|
||||
//
|
||||
// Since num_symbols' inclusive maximum is 258 (for RUNA, RUNB, 255 × mXYZ,
|
||||
// EOB), there are at most 257 branch nodes.
|
||||
huffman_trees : array[6] array[257] array[2] base.u16,
|
||||
// Each array[2] base.u16 is a branch node in the Huffman tree. The
|
||||
// build_huffman_tree method comment details what the base.u16 values mean.
|
||||
//
|
||||
// Each Huffman tree has num_symbols leaf nodes and hence (num_symbols - 1)
|
||||
// branch nodes, also called internal nodes, as per
|
||||
// https://stackoverflow.com/questions/69399994/maximum-number-of-nodes-in-a-huffman-tree/69401083#69401083
|
||||
//
|
||||
// This can be proven inductively. The base case Huffman code (two symbols)
|
||||
// has one branch node (the root node). The inductive case, adding one
|
||||
// symbol to a (complete but possibly non-canonical) Huffman code replaces
|
||||
// one leaf node with one branch node (and two leaf nodes). The net gain in
|
||||
// branch nodes is +1.
|
||||
//
|
||||
// Since num_symbols' inclusive maximum is 258 (for RUNA, RUNB, 255 × mXYZ,
|
||||
// EOB), there are at most 257 branch nodes.
|
||||
huffman_trees : array[6] array[257] array[2] base.u16,
|
||||
|
||||
// huffman_tables[which] caches the result of walking huffman_trees[which]
|
||||
// for each possible 8-bit input. The base.u16 value decomposes as:
|
||||
// - The low 10 bits are the final node, where finality comes either from
|
||||
// hitting a leaf node or from spending all 8 bits of the input. As per
|
||||
// the build_huffman_tree method, a value of 0x100 = 256 or less means
|
||||
// a branch node and 0x201 = 513 or more means a leaf node.
|
||||
// - The middle 2 bits are unused.
|
||||
// - The high 4 bits are the number of input bits spent.
|
||||
huffman_tables : array[6] array[256] base.u16,
|
||||
// huffman_tables[which] caches the result of walking huffman_trees[which]
|
||||
// for each possible 8-bit input. The base.u16 value decomposes as:
|
||||
// - The low 10 bits are the final node, where finality comes either from
|
||||
// hitting a leaf node or from spending all 8 bits of the input. As per
|
||||
// the build_huffman_tree method, a value of 0x100 = 256 or less means
|
||||
// a branch node and 0x201 = 513 or more means a leaf node.
|
||||
// - The middle 2 bits are unused.
|
||||
// - The high 4 bits are the number of input bits spent.
|
||||
huffman_tables : array[6] array[256] base.u16,
|
||||
|
||||
// bwt is the Burrows Wheeler Transform state. Per the README.md file, each
|
||||
// entry is a row with the low 8 bits holding the L column and the high 20
|
||||
// bits holding the U column. The middle 4 bits are unused.
|
||||
//
|
||||
// The read_code_lengths and build_huffman_tree methods also re-purpose
|
||||
// this buffer to temporarily hold up to 258 symbols' code lengths.
|
||||
bwt : array[1_048576] base.u32,
|
||||
// bwt is the Burrows Wheeler Transform state. Per the README.md file, each
|
||||
// entry is a row with the low 8 bits holding the L column and the high 20
|
||||
// bits holding the U column. The middle 4 bits are unused.
|
||||
//
|
||||
// The read_code_lengths and build_huffman_tree methods also re-purpose
|
||||
// this buffer to temporarily hold up to 258 symbols' code lengths.
|
||||
bwt : array[1_048576] base.u32,
|
||||
)
|
||||
|
||||
pub func decoder.set_quirk_enabled!(quirk: base.u32, enabled: base.bool) {
|
||||
if args.quirk == base.QUIRK_IGNORE_CHECKSUM {
|
||||
this.ignore_checksum = args.enabled
|
||||
}
|
||||
if args.quirk == base.QUIRK_IGNORE_CHECKSUM {
|
||||
this.ignore_checksum = args.enabled
|
||||
}
|
||||
}
|
||||
|
||||
pub func decoder.workbuf_len() base.range_ii_u64 {
|
||||
return this.util.make_range_ii_u64(
|
||||
min_incl: DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE,
|
||||
max_incl: DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE)
|
||||
return this.util.make_range_ii_u64(
|
||||
min_incl: DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE,
|
||||
max_incl: DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE)
|
||||
}
|
||||
|
||||
pub func decoder.transform_io?(dst: base.io_writer, src: base.io_reader, workbuf: slice base.u8) {
|
||||
var status : base.status
|
||||
var status : base.status
|
||||
|
||||
while true {
|
||||
status =? this.do_transform_io?(dst: args.dst, src: args.src, workbuf: args.workbuf)
|
||||
if (status == base."$short read") and args.src.is_closed() {
|
||||
return "#truncated input"
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
while true {
|
||||
status =? this.do_transform_io?(dst: args.dst, src: args.src, workbuf: args.workbuf)
|
||||
if (status == base."$short read") and args.src.is_closed() {
|
||||
return "#truncated input"
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
}
|
||||
|
||||
pri func decoder.do_transform_io?(dst: base.io_writer, src: base.io_reader, workbuf: slice base.u8) {
|
||||
var c : base.u8
|
||||
var i : base.u32
|
||||
var tag : base.u64
|
||||
var status : base.status
|
||||
var final_checksum_want : base.u32
|
||||
var c : base.u8
|
||||
var i : base.u32
|
||||
var tag : base.u64
|
||||
var status : base.status
|
||||
var final_checksum_want : base.u32
|
||||
|
||||
// Read the header.
|
||||
c = args.src.read_u8?()
|
||||
if c <> 0x42 {
|
||||
return "#bad header"
|
||||
}
|
||||
c = args.src.read_u8?()
|
||||
if c <> 0x5A {
|
||||
return "#bad header"
|
||||
}
|
||||
c = args.src.read_u8?()
|
||||
if c <> 0x68 {
|
||||
return "#bad header"
|
||||
}
|
||||
c = args.src.read_u8?()
|
||||
if (c < '1') or ('9' < c) {
|
||||
return "#bad header"
|
||||
}
|
||||
this.max_incl_block_size = ((c - '0') as base.u32) * 100000
|
||||
// Read the header.
|
||||
c = args.src.read_u8?()
|
||||
if c <> 0x42 {
|
||||
return "#bad header"
|
||||
}
|
||||
c = args.src.read_u8?()
|
||||
if c <> 0x5A {
|
||||
return "#bad header"
|
||||
}
|
||||
c = args.src.read_u8?()
|
||||
if c <> 0x68 {
|
||||
return "#bad header"
|
||||
}
|
||||
c = args.src.read_u8?()
|
||||
if (c < '1') or ('9' < c) {
|
||||
return "#bad header"
|
||||
}
|
||||
this.max_incl_block_size = ((c - '0') as base.u32) * 100000
|
||||
|
||||
while true {
|
||||
// Read the 48-bit tag.
|
||||
tag = 0
|
||||
i = 0
|
||||
while i < 48 {
|
||||
if this.n_bits <= 0 {
|
||||
c = args.src.read_u8?()
|
||||
this.bits = (c as base.u32) << 24
|
||||
this.n_bits = 8
|
||||
assert this.n_bits > 0
|
||||
}
|
||||
tag ~mod<<= 1
|
||||
tag |= (this.bits >> 31) as base.u64
|
||||
this.bits ~mod<<= 1
|
||||
this.n_bits -= 1
|
||||
i += 1
|
||||
} endwhile
|
||||
while true {
|
||||
// Read the 48-bit tag.
|
||||
tag = 0
|
||||
i = 0
|
||||
while i < 48 {
|
||||
if this.n_bits <= 0 {
|
||||
c = args.src.read_u8?()
|
||||
this.bits = (c as base.u32) << 24
|
||||
this.n_bits = 8
|
||||
assert this.n_bits > 0
|
||||
}
|
||||
tag ~mod<<= 1
|
||||
tag |= (this.bits >> 31) as base.u64
|
||||
this.bits ~mod<<= 1
|
||||
this.n_bits -= 1
|
||||
i += 1
|
||||
} endwhile
|
||||
|
||||
if tag == 0x1772_4538_5090 {
|
||||
break
|
||||
} else if tag <> 0x3141_5926_5359 {
|
||||
return "#bad block header"
|
||||
}
|
||||
if tag == 0x1772_4538_5090 {
|
||||
break
|
||||
} else if tag <> 0x3141_5926_5359 {
|
||||
return "#bad block header"
|
||||
}
|
||||
|
||||
this.prepare_block?(src: args.src)
|
||||
this.prepare_block?(src: args.src)
|
||||
|
||||
this.block_size = 0
|
||||
this.decode_huffman_finished = false
|
||||
this.decode_huffman_which = CLAMP_TO_5[this.huffman_selectors[0] & 7]
|
||||
this.decode_huffman_ticks = 50
|
||||
this.decode_huffman_section = 0
|
||||
this.decode_huffman_run_shift = 0
|
||||
while not this.decode_huffman_finished {
|
||||
status = this.decode_huffman_fast!(src: args.src)
|
||||
if status.is_error() {
|
||||
return status
|
||||
} else if this.decode_huffman_finished {
|
||||
break
|
||||
}
|
||||
this.decode_huffman_slow?(src: args.src)
|
||||
} endwhile
|
||||
this.block_size = 0
|
||||
this.decode_huffman_finished = false
|
||||
this.decode_huffman_which = CLAMP_TO_5[this.huffman_selectors[0] & 7]
|
||||
this.decode_huffman_ticks = 50
|
||||
this.decode_huffman_section = 0
|
||||
this.decode_huffman_run_shift = 0
|
||||
while not this.decode_huffman_finished {
|
||||
status = this.decode_huffman_fast!(src: args.src)
|
||||
if status.is_error() {
|
||||
return status
|
||||
} else if this.decode_huffman_finished {
|
||||
break
|
||||
}
|
||||
this.decode_huffman_slow?(src: args.src)
|
||||
} endwhile
|
||||
|
||||
this.invert_bwt!()
|
||||
this.invert_bwt!()
|
||||
|
||||
this.block_checksum_have = 0xFFFF_FFFF
|
||||
if this.original_pointer >= this.block_size {
|
||||
return "#bad block length"
|
||||
}
|
||||
assert this.original_pointer < 900000 via "a < b: a < c; c <= b"(c: this.block_size)
|
||||
this.flush_pointer = this.bwt[this.original_pointer] >> 12
|
||||
this.flush_repeat_count = 0
|
||||
this.flush_prev = 0
|
||||
this.block_checksum_have = 0xFFFF_FFFF
|
||||
if this.original_pointer >= this.block_size {
|
||||
return "#bad block length"
|
||||
}
|
||||
assert this.original_pointer < 900000 via "a < b: a < c; c <= b"(c: this.block_size)
|
||||
this.flush_pointer = this.bwt[this.original_pointer] >> 12
|
||||
this.flush_repeat_count = 0
|
||||
this.flush_prev = 0
|
||||
|
||||
while this.block_size > 0 {
|
||||
this.flush_fast!(dst: args.dst)
|
||||
if this.block_size <= 0 {
|
||||
break
|
||||
}
|
||||
this.flush_slow?(dst: args.dst)
|
||||
} endwhile
|
||||
while this.block_size > 0 {
|
||||
this.flush_fast!(dst: args.dst)
|
||||
if this.block_size <= 0 {
|
||||
break
|
||||
}
|
||||
this.flush_slow?(dst: args.dst)
|
||||
} endwhile
|
||||
|
||||
this.block_checksum_have ^= 0xFFFF_FFFF
|
||||
if (not this.ignore_checksum) and (this.block_checksum_have <> this.block_checksum_want) {
|
||||
return "#bad checksum"
|
||||
}
|
||||
this.final_checksum_have = this.block_checksum_have ^ (
|
||||
(this.final_checksum_have >> 31) |
|
||||
(this.final_checksum_have ~mod<< 1))
|
||||
} endwhile
|
||||
this.block_checksum_have ^= 0xFFFF_FFFF
|
||||
if (not this.ignore_checksum) and (this.block_checksum_have <> this.block_checksum_want) {
|
||||
return "#bad checksum"
|
||||
}
|
||||
this.final_checksum_have = this.block_checksum_have ^ (
|
||||
(this.final_checksum_have >> 31) |
|
||||
(this.final_checksum_have ~mod<< 1))
|
||||
} endwhile
|
||||
|
||||
// Read the 32-bit final checksum.
|
||||
final_checksum_want = 0
|
||||
i = 0
|
||||
while i < 32 {
|
||||
if this.n_bits <= 0 {
|
||||
c = args.src.read_u8?()
|
||||
this.bits = (c as base.u32) << 24
|
||||
this.n_bits = 8
|
||||
assert this.n_bits > 0
|
||||
}
|
||||
final_checksum_want ~mod<<= 1
|
||||
final_checksum_want |= this.bits >> 31
|
||||
this.bits ~mod<<= 1
|
||||
this.n_bits -= 1
|
||||
i += 1
|
||||
} endwhile
|
||||
if (not this.ignore_checksum) and (this.final_checksum_have <> final_checksum_want) {
|
||||
return "#bad checksum"
|
||||
}
|
||||
// Read the 32-bit final checksum.
|
||||
final_checksum_want = 0
|
||||
i = 0
|
||||
while i < 32 {
|
||||
if this.n_bits <= 0 {
|
||||
c = args.src.read_u8?()
|
||||
this.bits = (c as base.u32) << 24
|
||||
this.n_bits = 8
|
||||
assert this.n_bits > 0
|
||||
}
|
||||
final_checksum_want ~mod<<= 1
|
||||
final_checksum_want |= this.bits >> 31
|
||||
this.bits ~mod<<= 1
|
||||
this.n_bits -= 1
|
||||
i += 1
|
||||
} endwhile
|
||||
if (not this.ignore_checksum) and (this.final_checksum_have <> final_checksum_want) {
|
||||
return "#bad checksum"
|
||||
}
|
||||
}
|
||||
|
||||
pri func decoder.prepare_block?(src: base.io_reader) {
|
||||
var c : base.u8
|
||||
var i : base.u32
|
||||
var j : base.u32
|
||||
var selector : base.u32
|
||||
var sel_ff : base.u32
|
||||
var movee : base.u8
|
||||
var status : base.status
|
||||
var c : base.u8
|
||||
var i : base.u32
|
||||
var j : base.u32
|
||||
var selector : base.u32
|
||||
var sel_ff : base.u32
|
||||
var movee : base.u8
|
||||
var status : base.status
|
||||
|
||||
// Read the 32-bit block checksum.
|
||||
this.block_checksum_want = 0
|
||||
i = 0
|
||||
while i < 32 {
|
||||
if this.n_bits <= 0 {
|
||||
c = args.src.read_u8?()
|
||||
this.bits = (c as base.u32) << 24
|
||||
this.n_bits = 8
|
||||
assert this.n_bits > 0
|
||||
}
|
||||
this.block_checksum_want ~mod<<= 1
|
||||
this.block_checksum_want |= this.bits >> 31
|
||||
this.bits ~mod<<= 1
|
||||
this.n_bits -= 1
|
||||
i += 1
|
||||
} endwhile
|
||||
// Read the 32-bit block checksum.
|
||||
this.block_checksum_want = 0
|
||||
i = 0
|
||||
while i < 32 {
|
||||
if this.n_bits <= 0 {
|
||||
c = args.src.read_u8?()
|
||||
this.bits = (c as base.u32) << 24
|
||||
this.n_bits = 8
|
||||
assert this.n_bits > 0
|
||||
}
|
||||
this.block_checksum_want ~mod<<= 1
|
||||
this.block_checksum_want |= this.bits >> 31
|
||||
this.bits ~mod<<= 1
|
||||
this.n_bits -= 1
|
||||
i += 1
|
||||
} endwhile
|
||||
|
||||
// Read the "block randomized" bit.
|
||||
if this.n_bits <= 0 {
|
||||
c = args.src.read_u8?()
|
||||
this.bits = (c as base.u32) << 24
|
||||
this.n_bits = 8
|
||||
assert this.n_bits > 0
|
||||
}
|
||||
if (this.bits >> 31) <> 0 {
|
||||
return "#unsupported block randomization"
|
||||
}
|
||||
this.bits ~mod<<= 1
|
||||
this.n_bits -= 1
|
||||
// Read the "block randomized" bit.
|
||||
if this.n_bits <= 0 {
|
||||
c = args.src.read_u8?()
|
||||
this.bits = (c as base.u32) << 24
|
||||
this.n_bits = 8
|
||||
assert this.n_bits > 0
|
||||
}
|
||||
if (this.bits >> 31) <> 0 {
|
||||
return "#unsupported block randomization"
|
||||
}
|
||||
this.bits ~mod<<= 1
|
||||
this.n_bits -= 1
|
||||
|
||||
// Read the 24-bit original pointer. The BWT technical report calls this I.
|
||||
this.original_pointer = 0
|
||||
i = 0
|
||||
while i < 24 {
|
||||
if this.n_bits <= 0 {
|
||||
c = args.src.read_u8?()
|
||||
this.bits = (c as base.u32) << 24
|
||||
this.n_bits = 8
|
||||
assert this.n_bits > 0
|
||||
}
|
||||
this.original_pointer ~mod<<= 1
|
||||
this.original_pointer |= this.bits >> 31
|
||||
this.bits ~mod<<= 1
|
||||
this.n_bits -= 1
|
||||
i += 1
|
||||
} endwhile
|
||||
// Read the 24-bit original pointer. The BWT technical report calls this I.
|
||||
this.original_pointer = 0
|
||||
i = 0
|
||||
while i < 24 {
|
||||
if this.n_bits <= 0 {
|
||||
c = args.src.read_u8?()
|
||||
this.bits = (c as base.u32) << 24
|
||||
this.n_bits = 8
|
||||
assert this.n_bits > 0
|
||||
}
|
||||
this.original_pointer ~mod<<= 1
|
||||
this.original_pointer |= this.bits >> 31
|
||||
this.bits ~mod<<= 1
|
||||
this.n_bits -= 1
|
||||
i += 1
|
||||
} endwhile
|
||||
|
||||
// Reset the presence bitmap.
|
||||
i = 0
|
||||
while i < 256 {
|
||||
this.presence[i] = 0
|
||||
i += 1
|
||||
} endwhile
|
||||
// Reset the presence bitmap.
|
||||
i = 0
|
||||
while i < 256 {
|
||||
this.presence[i] = 0
|
||||
i += 1
|
||||
} endwhile
|
||||
|
||||
// Read the 16-bit high-level presence bitmap.
|
||||
i = 0
|
||||
while i < 256 {
|
||||
if this.n_bits <= 0 {
|
||||
c = args.src.read_u8?()
|
||||
this.bits = (c as base.u32) << 24
|
||||
this.n_bits = 8
|
||||
assert this.n_bits > 0
|
||||
}
|
||||
if (this.bits >> 31) <> 0 {
|
||||
this.presence[i] = 1
|
||||
}
|
||||
this.bits ~mod<<= 1
|
||||
this.n_bits -= 1
|
||||
i += 16
|
||||
} endwhile
|
||||
// Read the 16-bit high-level presence bitmap.
|
||||
i = 0
|
||||
while i < 256 {
|
||||
if this.n_bits <= 0 {
|
||||
c = args.src.read_u8?()
|
||||
this.bits = (c as base.u32) << 24
|
||||
this.n_bits = 8
|
||||
assert this.n_bits > 0
|
||||
}
|
||||
if (this.bits >> 31) <> 0 {
|
||||
this.presence[i] = 1
|
||||
}
|
||||
this.bits ~mod<<= 1
|
||||
this.n_bits -= 1
|
||||
i += 16
|
||||
} endwhile
|
||||
|
||||
// Read the 16-bit low-level presence bitmaps.
|
||||
this.scratch = 0
|
||||
i = 0
|
||||
while i < 256 {
|
||||
if this.presence[i] == 0 {
|
||||
i += 16
|
||||
continue
|
||||
}
|
||||
while true {
|
||||
if this.n_bits <= 0 {
|
||||
c = args.src.read_u8?()
|
||||
this.bits = (c as base.u32) << 24
|
||||
this.n_bits = 8
|
||||
assert this.n_bits > 0
|
||||
}
|
||||
this.scratch ~mod+= this.bits >> 31
|
||||
this.presence[i & 0xFF] = (this.bits >> 31) as base.u8
|
||||
this.bits ~mod<<= 1
|
||||
this.n_bits -= 1
|
||||
i ~mod+= 1
|
||||
if (i & 15) == 0 {
|
||||
break
|
||||
}
|
||||
} endwhile
|
||||
} endwhile
|
||||
if (this.scratch < 1) or (256 < this.scratch) {
|
||||
return "#bad block header"
|
||||
}
|
||||
this.num_symbols = this.scratch + 2
|
||||
// Read the 16-bit low-level presence bitmaps.
|
||||
this.scratch = 0
|
||||
i = 0
|
||||
while i < 256 {
|
||||
if this.presence[i] == 0 {
|
||||
i += 16
|
||||
continue
|
||||
}
|
||||
while true {
|
||||
if this.n_bits <= 0 {
|
||||
c = args.src.read_u8?()
|
||||
this.bits = (c as base.u32) << 24
|
||||
this.n_bits = 8
|
||||
assert this.n_bits > 0
|
||||
}
|
||||
this.scratch ~mod+= this.bits >> 31
|
||||
this.presence[i & 0xFF] = (this.bits >> 31) as base.u8
|
||||
this.bits ~mod<<= 1
|
||||
this.n_bits -= 1
|
||||
i ~mod+= 1
|
||||
if (i & 15) == 0 {
|
||||
break
|
||||
}
|
||||
} endwhile
|
||||
} endwhile
|
||||
if (this.scratch < 1) or (256 < this.scratch) {
|
||||
return "#bad block header"
|
||||
}
|
||||
this.num_symbols = this.scratch + 2
|
||||
|
||||
// Read the 3-bit number of Huffman codes.
|
||||
this.scratch = 0
|
||||
i = 0
|
||||
while i < 3 {
|
||||
if this.n_bits <= 0 {
|
||||
c = args.src.read_u8?()
|
||||
this.bits = (c as base.u32) << 24
|
||||
this.n_bits = 8
|
||||
assert this.n_bits > 0
|
||||
}
|
||||
this.scratch ~mod<<= 1
|
||||
this.scratch |= this.bits >> 31
|
||||
this.bits ~mod<<= 1
|
||||
this.n_bits -= 1
|
||||
i += 1
|
||||
} endwhile
|
||||
if (this.scratch < 2) or (6 < this.scratch) {
|
||||
return "#bad block header"
|
||||
}
|
||||
this.num_huffman_codes = this.scratch
|
||||
// Read the 3-bit number of Huffman codes.
|
||||
this.scratch = 0
|
||||
i = 0
|
||||
while i < 3 {
|
||||
if this.n_bits <= 0 {
|
||||
c = args.src.read_u8?()
|
||||
this.bits = (c as base.u32) << 24
|
||||
this.n_bits = 8
|
||||
assert this.n_bits > 0
|
||||
}
|
||||
this.scratch ~mod<<= 1
|
||||
this.scratch |= this.bits >> 31
|
||||
this.bits ~mod<<= 1
|
||||
this.n_bits -= 1
|
||||
i += 1
|
||||
} endwhile
|
||||
if (this.scratch < 2) or (6 < this.scratch) {
|
||||
return "#bad block header"
|
||||
}
|
||||
this.num_huffman_codes = this.scratch
|
||||
|
||||
// Read the 15-bit number of sections.
|
||||
this.scratch = 0
|
||||
i = 0
|
||||
while i < 15 {
|
||||
if this.n_bits <= 0 {
|
||||
c = args.src.read_u8?()
|
||||
this.bits = (c as base.u32) << 24
|
||||
this.n_bits = 8
|
||||
assert this.n_bits > 0
|
||||
}
|
||||
this.scratch ~mod<<= 1
|
||||
this.scratch |= this.bits >> 31
|
||||
this.bits ~mod<<= 1
|
||||
this.n_bits -= 1
|
||||
i += 1
|
||||
} endwhile
|
||||
if (this.scratch < 1) or (18001 < this.scratch) {
|
||||
return "#bad block header"
|
||||
}
|
||||
this.num_sections = this.scratch
|
||||
// Read the 15-bit number of sections.
|
||||
this.scratch = 0
|
||||
i = 0
|
||||
while i < 15 {
|
||||
if this.n_bits <= 0 {
|
||||
c = args.src.read_u8?()
|
||||
this.bits = (c as base.u32) << 24
|
||||
this.n_bits = 8
|
||||
assert this.n_bits > 0
|
||||
}
|
||||
this.scratch ~mod<<= 1
|
||||
this.scratch |= this.bits >> 31
|
||||
this.bits ~mod<<= 1
|
||||
this.n_bits -= 1
|
||||
i += 1
|
||||
} endwhile
|
||||
if (this.scratch < 1) or (18001 < this.scratch) {
|
||||
return "#bad block header"
|
||||
}
|
||||
this.num_sections = this.scratch
|
||||
|
||||
// Initialize the MTFT state.
|
||||
i = 0
|
||||
while i < this.num_huffman_codes {
|
||||
assert i < 6 via "a < b: a < c; c <= b"(c: this.num_huffman_codes)
|
||||
this.mtft[i] = i as base.u8
|
||||
i += 1
|
||||
} endwhile
|
||||
// Initialize the MTFT state.
|
||||
i = 0
|
||||
while i < this.num_huffman_codes {
|
||||
assert i < 6 via "a < b: a < c; c <= b"(c: this.num_huffman_codes)
|
||||
this.mtft[i] = i as base.u8
|
||||
i += 1
|
||||
} endwhile
|
||||
|
||||
// Read each section's Huffman code selector, applying the MTFT.
|
||||
i = 0
|
||||
while i < this.num_sections {
|
||||
assert i < 18001 via "a < b: a < c; c <= b"(c: this.num_sections)
|
||||
selector = 0
|
||||
// Read each section's Huffman code selector, applying the MTFT.
|
||||
i = 0
|
||||
while i < this.num_sections {
|
||||
assert i < 18001 via "a < b: a < c; c <= b"(c: this.num_sections)
|
||||
selector = 0
|
||||
|
||||
// Read a unary encoded natural number.
|
||||
while true,
|
||||
inv i < 18001,
|
||||
{
|
||||
if this.n_bits <= 0 {
|
||||
c = args.src.read_u8?()
|
||||
this.bits = (c as base.u32) << 24
|
||||
this.n_bits = 8
|
||||
assert this.n_bits > 0
|
||||
}
|
||||
if (this.bits >> 31) == 0 {
|
||||
this.bits ~mod<<= 1
|
||||
this.n_bits -= 1
|
||||
break
|
||||
}
|
||||
this.bits ~mod<<= 1
|
||||
this.n_bits -= 1
|
||||
selector ~mod+= 1
|
||||
if selector >= this.num_huffman_codes {
|
||||
return "#bad block header"
|
||||
}
|
||||
} endwhile
|
||||
// Read a unary encoded natural number.
|
||||
while true,
|
||||
inv i < 18001,
|
||||
{
|
||||
if this.n_bits <= 0 {
|
||||
c = args.src.read_u8?()
|
||||
this.bits = (c as base.u32) << 24
|
||||
this.n_bits = 8
|
||||
assert this.n_bits > 0
|
||||
}
|
||||
if (this.bits >> 31) == 0 {
|
||||
this.bits ~mod<<= 1
|
||||
this.n_bits -= 1
|
||||
break
|
||||
}
|
||||
this.bits ~mod<<= 1
|
||||
this.n_bits -= 1
|
||||
selector ~mod+= 1
|
||||
if selector >= this.num_huffman_codes {
|
||||
return "#bad block header"
|
||||
}
|
||||
} endwhile
|
||||
|
||||
// Move to front.
|
||||
if selector == 0 {
|
||||
this.huffman_selectors[i] = this.mtft[0]
|
||||
} else {
|
||||
sel_ff = selector & 0xFF
|
||||
movee = this.mtft[sel_ff]
|
||||
this.mtft[1 .. 1 + sel_ff].copy_from_slice!(
|
||||
s: this.mtft[.. sel_ff])
|
||||
this.mtft[0] = movee
|
||||
this.huffman_selectors[i] = movee
|
||||
}
|
||||
// Move to front.
|
||||
if selector == 0 {
|
||||
this.huffman_selectors[i] = this.mtft[0]
|
||||
} else {
|
||||
sel_ff = selector & 0xFF
|
||||
movee = this.mtft[sel_ff]
|
||||
this.mtft[1 .. 1 + sel_ff].copy_from_slice!(
|
||||
s: this.mtft[.. sel_ff])
|
||||
this.mtft[0] = movee
|
||||
this.huffman_selectors[i] = movee
|
||||
}
|
||||
|
||||
i += 1
|
||||
} endwhile
|
||||
i += 1
|
||||
} endwhile
|
||||
|
||||
// Read the Huffman codes.
|
||||
i = 0
|
||||
while i < this.num_huffman_codes {
|
||||
assert i < 6 via "a < b: a < c; c <= b"(c: this.num_huffman_codes)
|
||||
this.read_code_lengths?(src: args.src)
|
||||
status = this.build_huffman_tree!(which: i)
|
||||
if status.is_error() {
|
||||
return status
|
||||
}
|
||||
this.build_huffman_table!(which: i)
|
||||
i += 1
|
||||
} endwhile
|
||||
// Read the Huffman codes.
|
||||
i = 0
|
||||
while i < this.num_huffman_codes {
|
||||
assert i < 6 via "a < b: a < c; c <= b"(c: this.num_huffman_codes)
|
||||
this.read_code_lengths?(src: args.src)
|
||||
status = this.build_huffman_tree!(which: i)
|
||||
if status.is_error() {
|
||||
return status
|
||||
}
|
||||
this.build_huffman_table!(which: i)
|
||||
i += 1
|
||||
} endwhile
|
||||
|
||||
// Initialize the MTFT state.
|
||||
i = 0
|
||||
j = 0
|
||||
while i < 256 {
|
||||
if this.presence[i] <> 0 {
|
||||
this.mtft[j & 0xFF] = i as base.u8
|
||||
j ~mod+= 1
|
||||
}
|
||||
i += 1
|
||||
} endwhile
|
||||
// Initialize the MTFT state.
|
||||
i = 0
|
||||
j = 0
|
||||
while i < 256 {
|
||||
if this.presence[i] <> 0 {
|
||||
this.mtft[j & 0xFF] = i as base.u8
|
||||
j ~mod+= 1
|
||||
}
|
||||
i += 1
|
||||
} endwhile
|
||||
|
||||
// Initialize the letter counts.
|
||||
i = 0
|
||||
while i < 256 {
|
||||
this.letter_counts[i] = 0
|
||||
i += 1
|
||||
} endwhile
|
||||
// Initialize the letter counts.
|
||||
i = 0
|
||||
while i < 256 {
|
||||
this.letter_counts[i] = 0
|
||||
i += 1
|
||||
} endwhile
|
||||
}
|
||||
|
||||
pri func decoder.read_code_lengths?(src: base.io_reader) {
|
||||
var c : base.u8
|
||||
var i : base.u32
|
||||
var code_length : base.u32
|
||||
var c : base.u8
|
||||
var i : base.u32
|
||||
var code_length : base.u32
|
||||
|
||||
this.code_lengths_bitmask = 0
|
||||
this.code_lengths_bitmask = 0
|
||||
|
||||
// Read the 5-bit starting value.
|
||||
i = 0
|
||||
while i < 5 {
|
||||
if this.n_bits <= 0 {
|
||||
c = args.src.read_u8?()
|
||||
this.bits = (c as base.u32) << 24
|
||||
this.n_bits = 8
|
||||
assert this.n_bits > 0
|
||||
}
|
||||
code_length ~mod<<= 1
|
||||
code_length |= this.bits >> 31
|
||||
this.bits ~mod<<= 1
|
||||
this.n_bits -= 1
|
||||
i += 1
|
||||
} endwhile
|
||||
// Read the 5-bit starting value.
|
||||
i = 0
|
||||
while i < 5 {
|
||||
if this.n_bits <= 0 {
|
||||
c = args.src.read_u8?()
|
||||
this.bits = (c as base.u32) << 24
|
||||
this.n_bits = 8
|
||||
assert this.n_bits > 0
|
||||
}
|
||||
code_length ~mod<<= 1
|
||||
code_length |= this.bits >> 31
|
||||
this.bits ~mod<<= 1
|
||||
this.n_bits -= 1
|
||||
i += 1
|
||||
} endwhile
|
||||
|
||||
// Read the code lengths.
|
||||
i = 0
|
||||
while i < this.num_symbols {
|
||||
assert i < 258 via "a < b: a < c; c <= b"(c: this.num_symbols)
|
||||
while true,
|
||||
inv i < 258,
|
||||
{
|
||||
if (code_length < 1) or (20 < code_length) {
|
||||
return "#bad block header"
|
||||
}
|
||||
// Read the code lengths.
|
||||
i = 0
|
||||
while i < this.num_symbols {
|
||||
assert i < 258 via "a < b: a < c; c <= b"(c: this.num_symbols)
|
||||
while true,
|
||||
inv i < 258,
|
||||
{
|
||||
if (code_length < 1) or (20 < code_length) {
|
||||
return "#bad block header"
|
||||
}
|
||||
|
||||
if this.n_bits <= 0 {
|
||||
c = args.src.read_u8?()
|
||||
this.bits = (c as base.u32) << 24
|
||||
this.n_bits = 8
|
||||
assert this.n_bits > 0
|
||||
}
|
||||
if (this.bits >> 31) == 0 {
|
||||
this.bits ~mod<<= 1
|
||||
this.n_bits -= 1
|
||||
break
|
||||
}
|
||||
this.bits ~mod<<= 1
|
||||
this.n_bits -= 1
|
||||
if this.n_bits <= 0 {
|
||||
c = args.src.read_u8?()
|
||||
this.bits = (c as base.u32) << 24
|
||||
this.n_bits = 8
|
||||
assert this.n_bits > 0
|
||||
}
|
||||
if (this.bits >> 31) == 0 {
|
||||
this.bits ~mod<<= 1
|
||||
this.n_bits -= 1
|
||||
break
|
||||
}
|
||||
this.bits ~mod<<= 1
|
||||
this.n_bits -= 1
|
||||
|
||||
if this.n_bits <= 0 {
|
||||
c = args.src.read_u8?()
|
||||
this.bits = (c as base.u32) << 24
|
||||
this.n_bits = 8
|
||||
assert this.n_bits > 0
|
||||
}
|
||||
if (this.bits >> 31) == 0 {
|
||||
code_length ~mod+= 1
|
||||
} else {
|
||||
code_length ~mod-= 1
|
||||
}
|
||||
this.bits ~mod<<= 1
|
||||
this.n_bits -= 1
|
||||
} endwhile
|
||||
if this.n_bits <= 0 {
|
||||
c = args.src.read_u8?()
|
||||
this.bits = (c as base.u32) << 24
|
||||
this.n_bits = 8
|
||||
assert this.n_bits > 0
|
||||
}
|
||||
if (this.bits >> 31) == 0 {
|
||||
code_length ~mod+= 1
|
||||
} else {
|
||||
code_length ~mod-= 1
|
||||
}
|
||||
this.bits ~mod<<= 1
|
||||
this.n_bits -= 1
|
||||
} endwhile
|
||||
|
||||
this.code_lengths_bitmask |= (1 as base.u32) << (code_length & 31)
|
||||
this.bwt[i] = code_length
|
||||
i += 1
|
||||
} endwhile
|
||||
this.code_lengths_bitmask |= (1 as base.u32) << (code_length & 31)
|
||||
this.bwt[i] = code_length
|
||||
i += 1
|
||||
} endwhile
|
||||
}
|
||||
|
||||
// build_huffman_tree builds a canonical Huffman tree given the symbols' code
|
||||
@@ -602,122 +602,122 @@ pri func decoder.read_code_lengths?(src: base.io_reader) {
|
||||
// height (here, 3) then it would pop the stack. Otherwise it would push new
|
||||
// nodes onto the stack until the stack height matched that code length.
|
||||
pri func decoder.build_huffman_tree!(which: base.u32[..= 5]) base.status {
|
||||
var code_length : base.u32
|
||||
var symbol_index : base.u32
|
||||
var num_branch_nodes : base.u32[..= 257]
|
||||
var stack_height : base.u32[..= 21]
|
||||
var stack_values : array[21] base.u32[..= 256]
|
||||
var node_index : base.u32[..= 256]
|
||||
var leaf_value : base.u16
|
||||
var code_length : base.u32
|
||||
var symbol_index : base.u32
|
||||
var num_branch_nodes : base.u32[..= 257]
|
||||
var stack_height : base.u32[..= 21]
|
||||
var stack_values : array[21] base.u32[..= 256]
|
||||
var node_index : base.u32[..= 256]
|
||||
var leaf_value : base.u16
|
||||
|
||||
// Push a root node.
|
||||
this.huffman_trees[args.which][0][0] = 0
|
||||
this.huffman_trees[args.which][0][1] = 0
|
||||
num_branch_nodes = 1
|
||||
stack_height = 1
|
||||
stack_values[0] = 0
|
||||
// Push a root node.
|
||||
this.huffman_trees[args.which][0][0] = 0
|
||||
this.huffman_trees[args.which][0][1] = 0
|
||||
num_branch_nodes = 1
|
||||
stack_height = 1
|
||||
stack_values[0] = 0
|
||||
|
||||
code_length = 1
|
||||
while code_length <= 20 {
|
||||
if (this.code_lengths_bitmask & ((1 as base.u32) << code_length)) == 0 {
|
||||
code_length += 1
|
||||
continue
|
||||
}
|
||||
symbol_index = 0
|
||||
while symbol_index < this.num_symbols,
|
||||
inv code_length <= 20,
|
||||
{
|
||||
assert symbol_index < 258 via "a < b: a < c; c <= b"(c: this.num_symbols)
|
||||
if this.bwt[symbol_index] <> code_length {
|
||||
symbol_index += 1
|
||||
continue
|
||||
}
|
||||
code_length = 1
|
||||
while code_length <= 20 {
|
||||
if (this.code_lengths_bitmask & ((1 as base.u32) << code_length)) == 0 {
|
||||
code_length += 1
|
||||
continue
|
||||
}
|
||||
symbol_index = 0
|
||||
while symbol_index < this.num_symbols,
|
||||
inv code_length <= 20,
|
||||
{
|
||||
assert symbol_index < 258 via "a < b: a < c; c <= b"(c: this.num_symbols)
|
||||
if this.bwt[symbol_index] <> code_length {
|
||||
symbol_index += 1
|
||||
continue
|
||||
}
|
||||
|
||||
while true,
|
||||
inv code_length <= 20,
|
||||
inv symbol_index < 258,
|
||||
post stack_height > 0,
|
||||
{
|
||||
if stack_height <= 0 {
|
||||
return "#bad Huffman code (over-subscribed)"
|
||||
} else if stack_height >= code_length {
|
||||
break
|
||||
}
|
||||
assert stack_height < 20 via "a < b: a < c; c <= b"(c: code_length)
|
||||
while true,
|
||||
inv code_length <= 20,
|
||||
inv symbol_index < 258,
|
||||
post stack_height > 0,
|
||||
{
|
||||
if stack_height <= 0 {
|
||||
return "#bad Huffman code (over-subscribed)"
|
||||
} else if stack_height >= code_length {
|
||||
break
|
||||
}
|
||||
assert stack_height < 20 via "a < b: a < c; c <= b"(c: code_length)
|
||||
|
||||
node_index = stack_values[stack_height - 1]
|
||||
if this.huffman_trees[args.which][node_index][0] == 0 {
|
||||
this.huffman_trees[args.which][node_index][0] = num_branch_nodes as base.u16
|
||||
} else {
|
||||
this.huffman_trees[args.which][node_index][1] = num_branch_nodes as base.u16
|
||||
}
|
||||
if num_branch_nodes >= 257 {
|
||||
return "#bad Huffman code (under-subscribed)"
|
||||
}
|
||||
stack_values[stack_height] = num_branch_nodes
|
||||
this.huffman_trees[args.which][num_branch_nodes][0] = 0
|
||||
this.huffman_trees[args.which][num_branch_nodes][1] = 0
|
||||
num_branch_nodes += 1
|
||||
stack_height += 1
|
||||
} endwhile
|
||||
node_index = stack_values[stack_height - 1]
|
||||
if this.huffman_trees[args.which][node_index][0] == 0 {
|
||||
this.huffman_trees[args.which][node_index][0] = num_branch_nodes as base.u16
|
||||
} else {
|
||||
this.huffman_trees[args.which][node_index][1] = num_branch_nodes as base.u16
|
||||
}
|
||||
if num_branch_nodes >= 257 {
|
||||
return "#bad Huffman code (under-subscribed)"
|
||||
}
|
||||
stack_values[stack_height] = num_branch_nodes
|
||||
this.huffman_trees[args.which][num_branch_nodes][0] = 0
|
||||
this.huffman_trees[args.which][num_branch_nodes][1] = 0
|
||||
num_branch_nodes += 1
|
||||
stack_height += 1
|
||||
} endwhile
|
||||
|
||||
node_index = stack_values[stack_height - 1]
|
||||
if symbol_index < 2 {
|
||||
leaf_value = (0x301 + symbol_index) as base.u16
|
||||
} else if (symbol_index + 1) < this.num_symbols {
|
||||
leaf_value = (0x1FF + symbol_index) as base.u16
|
||||
} else {
|
||||
leaf_value = 0x300
|
||||
}
|
||||
node_index = stack_values[stack_height - 1]
|
||||
if symbol_index < 2 {
|
||||
leaf_value = (0x301 + symbol_index) as base.u16
|
||||
} else if (symbol_index + 1) < this.num_symbols {
|
||||
leaf_value = (0x1FF + symbol_index) as base.u16
|
||||
} else {
|
||||
leaf_value = 0x300
|
||||
}
|
||||
|
||||
if this.huffman_trees[args.which][node_index][0] == 0 {
|
||||
this.huffman_trees[args.which][node_index][0] = leaf_value
|
||||
} else {
|
||||
this.huffman_trees[args.which][node_index][1] = leaf_value
|
||||
stack_height -= 1
|
||||
while stack_height > 0,
|
||||
inv code_length <= 20,
|
||||
inv symbol_index < 258,
|
||||
{
|
||||
node_index = stack_values[stack_height - 1]
|
||||
if this.huffman_trees[args.which][node_index][1] == 0 {
|
||||
break
|
||||
}
|
||||
stack_height -= 1
|
||||
} endwhile
|
||||
}
|
||||
if this.huffman_trees[args.which][node_index][0] == 0 {
|
||||
this.huffman_trees[args.which][node_index][0] = leaf_value
|
||||
} else {
|
||||
this.huffman_trees[args.which][node_index][1] = leaf_value
|
||||
stack_height -= 1
|
||||
while stack_height > 0,
|
||||
inv code_length <= 20,
|
||||
inv symbol_index < 258,
|
||||
{
|
||||
node_index = stack_values[stack_height - 1]
|
||||
if this.huffman_trees[args.which][node_index][1] == 0 {
|
||||
break
|
||||
}
|
||||
stack_height -= 1
|
||||
} endwhile
|
||||
}
|
||||
|
||||
symbol_index += 1
|
||||
} endwhile
|
||||
code_length += 1
|
||||
} endwhile
|
||||
symbol_index += 1
|
||||
} endwhile
|
||||
code_length += 1
|
||||
} endwhile
|
||||
|
||||
if stack_height <> 0 {
|
||||
return "#bad Huffman code (under-subscribed)"
|
||||
}
|
||||
return ok
|
||||
if stack_height <> 0 {
|
||||
return "#bad Huffman code (under-subscribed)"
|
||||
}
|
||||
return ok
|
||||
}
|
||||
|
||||
pri func decoder.build_huffman_table!(which: base.u32[..= 5]) {
|
||||
var i : base.u32
|
||||
var bits : base.u32
|
||||
var n_bits : base.u16[..= 8]
|
||||
var child : base.u16
|
||||
var i : base.u32
|
||||
var bits : base.u32
|
||||
var n_bits : base.u16[..= 8]
|
||||
var child : base.u16
|
||||
|
||||
while i < 256 {
|
||||
bits = i << 24
|
||||
n_bits = 0
|
||||
child = 0
|
||||
while (child < 257) and (n_bits < 8),
|
||||
inv i < 256,
|
||||
{
|
||||
child = this.huffman_trees[args.which][child][bits >> 31]
|
||||
bits ~mod<<= 1
|
||||
n_bits += 1
|
||||
} endwhile
|
||||
this.huffman_tables[args.which][i] = (child | (n_bits << 12)) as base.u16
|
||||
i += 1
|
||||
} endwhile
|
||||
while i < 256 {
|
||||
bits = i << 24
|
||||
n_bits = 0
|
||||
child = 0
|
||||
while (child < 257) and (n_bits < 8),
|
||||
inv i < 256,
|
||||
{
|
||||
child = this.huffman_trees[args.which][child][bits >> 31]
|
||||
bits ~mod<<= 1
|
||||
n_bits += 1
|
||||
} endwhile
|
||||
this.huffman_tables[args.which][i] = (child | (n_bits << 12)) as base.u16
|
||||
i += 1
|
||||
} endwhile
|
||||
}
|
||||
|
||||
// invert_bwt performs "two passes over the data, and one pass over the
|
||||
@@ -725,67 +725,67 @@ pri func decoder.build_huffman_table!(which: base.u32[..= 5]) {
|
||||
// (accumulating this.letter_counts) is integrated into the decode_huffman_etc
|
||||
// methods.
|
||||
pri func decoder.invert_bwt!() {
|
||||
var i : base.u32
|
||||
var letter : base.u32[..= 255]
|
||||
var sum : base.u32
|
||||
var old_sum : base.u32
|
||||
var i : base.u32
|
||||
var letter : base.u32[..= 255]
|
||||
var sum : base.u32
|
||||
var old_sum : base.u32
|
||||
|
||||
// Alphabet pass.
|
||||
sum = 0
|
||||
i = 0
|
||||
while i < 256 {
|
||||
old_sum = sum
|
||||
sum ~mod+= this.letter_counts[i]
|
||||
this.letter_counts[i] = old_sum
|
||||
i += 1
|
||||
} endwhile
|
||||
// Alphabet pass.
|
||||
sum = 0
|
||||
i = 0
|
||||
while i < 256 {
|
||||
old_sum = sum
|
||||
sum ~mod+= this.letter_counts[i]
|
||||
this.letter_counts[i] = old_sum
|
||||
i += 1
|
||||
} endwhile
|
||||
|
||||
// Second data pass, but per the README.md file, calculate the U column
|
||||
// instead of the BWT technical report's T column.
|
||||
i = 0
|
||||
while i < this.block_size {
|
||||
assert i < 900000 via "a < b: a < c; c <= b"(c: this.block_size)
|
||||
letter = this.bwt[i] & 0xFF
|
||||
this.bwt[this.letter_counts[letter] & 1_048575] |= i << 12
|
||||
this.letter_counts[letter] ~mod+= 1
|
||||
i += 1
|
||||
} endwhile
|
||||
// Second data pass, but per the README.md file, calculate the U column
|
||||
// instead of the BWT technical report's T column.
|
||||
i = 0
|
||||
while i < this.block_size {
|
||||
assert i < 900000 via "a < b: a < c; c <= b"(c: this.block_size)
|
||||
letter = this.bwt[i] & 0xFF
|
||||
this.bwt[this.letter_counts[letter] & 1_048575] |= i << 12
|
||||
this.letter_counts[letter] ~mod+= 1
|
||||
i += 1
|
||||
} endwhile
|
||||
}
|
||||
|
||||
// The table below was created by script/print-crc32-magic-numbers.go with the
|
||||
// -reverse flag set.
|
||||
|
||||
pri const REV_CRC32_TABLE : array[256] base.u32 = [
|
||||
0x0000_0000, 0x04C1_1DB7, 0x0982_3B6E, 0x0D43_26D9, 0x1304_76DC, 0x17C5_6B6B, 0x1A86_4DB2, 0x1E47_5005,
|
||||
0x2608_EDB8, 0x22C9_F00F, 0x2F8A_D6D6, 0x2B4B_CB61, 0x350C_9B64, 0x31CD_86D3, 0x3C8E_A00A, 0x384F_BDBD,
|
||||
0x4C11_DB70, 0x48D0_C6C7, 0x4593_E01E, 0x4152_FDA9, 0x5F15_ADAC, 0x5BD4_B01B, 0x5697_96C2, 0x5256_8B75,
|
||||
0x6A19_36C8, 0x6ED8_2B7F, 0x639B_0DA6, 0x675A_1011, 0x791D_4014, 0x7DDC_5DA3, 0x709F_7B7A, 0x745E_66CD,
|
||||
0x9823_B6E0, 0x9CE2_AB57, 0x91A1_8D8E, 0x9560_9039, 0x8B27_C03C, 0x8FE6_DD8B, 0x82A5_FB52, 0x8664_E6E5,
|
||||
0xBE2B_5B58, 0xBAEA_46EF, 0xB7A9_6036, 0xB368_7D81, 0xAD2F_2D84, 0xA9EE_3033, 0xA4AD_16EA, 0xA06C_0B5D,
|
||||
0xD432_6D90, 0xD0F3_7027, 0xDDB0_56FE, 0xD971_4B49, 0xC736_1B4C, 0xC3F7_06FB, 0xCEB4_2022, 0xCA75_3D95,
|
||||
0xF23A_8028, 0xF6FB_9D9F, 0xFBB8_BB46, 0xFF79_A6F1, 0xE13E_F6F4, 0xE5FF_EB43, 0xE8BC_CD9A, 0xEC7D_D02D,
|
||||
0x3486_7077, 0x3047_6DC0, 0x3D04_4B19, 0x39C5_56AE, 0x2782_06AB, 0x2343_1B1C, 0x2E00_3DC5, 0x2AC1_2072,
|
||||
0x128E_9DCF, 0x164F_8078, 0x1B0C_A6A1, 0x1FCD_BB16, 0x018A_EB13, 0x054B_F6A4, 0x0808_D07D, 0x0CC9_CDCA,
|
||||
0x7897_AB07, 0x7C56_B6B0, 0x7115_9069, 0x75D4_8DDE, 0x6B93_DDDB, 0x6F52_C06C, 0x6211_E6B5, 0x66D0_FB02,
|
||||
0x5E9F_46BF, 0x5A5E_5B08, 0x571D_7DD1, 0x53DC_6066, 0x4D9B_3063, 0x495A_2DD4, 0x4419_0B0D, 0x40D8_16BA,
|
||||
0xACA5_C697, 0xA864_DB20, 0xA527_FDF9, 0xA1E6_E04E, 0xBFA1_B04B, 0xBB60_ADFC, 0xB623_8B25, 0xB2E2_9692,
|
||||
0x8AAD_2B2F, 0x8E6C_3698, 0x832F_1041, 0x87EE_0DF6, 0x99A9_5DF3, 0x9D68_4044, 0x902B_669D, 0x94EA_7B2A,
|
||||
0xE0B4_1DE7, 0xE475_0050, 0xE936_2689, 0xEDF7_3B3E, 0xF3B0_6B3B, 0xF771_768C, 0xFA32_5055, 0xFEF3_4DE2,
|
||||
0xC6BC_F05F, 0xC27D_EDE8, 0xCF3E_CB31, 0xCBFF_D686, 0xD5B8_8683, 0xD179_9B34, 0xDC3A_BDED, 0xD8FB_A05A,
|
||||
0x690C_E0EE, 0x6DCD_FD59, 0x608E_DB80, 0x644F_C637, 0x7A08_9632, 0x7EC9_8B85, 0x738A_AD5C, 0x774B_B0EB,
|
||||
0x4F04_0D56, 0x4BC5_10E1, 0x4686_3638, 0x4247_2B8F, 0x5C00_7B8A, 0x58C1_663D, 0x5582_40E4, 0x5143_5D53,
|
||||
0x251D_3B9E, 0x21DC_2629, 0x2C9F_00F0, 0x285E_1D47, 0x3619_4D42, 0x32D8_50F5, 0x3F9B_762C, 0x3B5A_6B9B,
|
||||
0x0315_D626, 0x07D4_CB91, 0x0A97_ED48, 0x0E56_F0FF, 0x1011_A0FA, 0x14D0_BD4D, 0x1993_9B94, 0x1D52_8623,
|
||||
0xF12F_560E, 0xF5EE_4BB9, 0xF8AD_6D60, 0xFC6C_70D7, 0xE22B_20D2, 0xE6EA_3D65, 0xEBA9_1BBC, 0xEF68_060B,
|
||||
0xD727_BBB6, 0xD3E6_A601, 0xDEA5_80D8, 0xDA64_9D6F, 0xC423_CD6A, 0xC0E2_D0DD, 0xCDA1_F604, 0xC960_EBB3,
|
||||
0xBD3E_8D7E, 0xB9FF_90C9, 0xB4BC_B610, 0xB07D_ABA7, 0xAE3A_FBA2, 0xAAFB_E615, 0xA7B8_C0CC, 0xA379_DD7B,
|
||||
0x9B36_60C6, 0x9FF7_7D71, 0x92B4_5BA8, 0x9675_461F, 0x8832_161A, 0x8CF3_0BAD, 0x81B0_2D74, 0x8571_30C3,
|
||||
0x5D8A_9099, 0x594B_8D2E, 0x5408_ABF7, 0x50C9_B640, 0x4E8E_E645, 0x4A4F_FBF2, 0x470C_DD2B, 0x43CD_C09C,
|
||||
0x7B82_7D21, 0x7F43_6096, 0x7200_464F, 0x76C1_5BF8, 0x6886_0BFD, 0x6C47_164A, 0x6104_3093, 0x65C5_2D24,
|
||||
0x119B_4BE9, 0x155A_565E, 0x1819_7087, 0x1CD8_6D30, 0x029F_3D35, 0x065E_2082, 0x0B1D_065B, 0x0FDC_1BEC,
|
||||
0x3793_A651, 0x3352_BBE6, 0x3E11_9D3F, 0x3AD0_8088, 0x2497_D08D, 0x2056_CD3A, 0x2D15_EBE3, 0x29D4_F654,
|
||||
0xC5A9_2679, 0xC168_3BCE, 0xCC2B_1D17, 0xC8EA_00A0, 0xD6AD_50A5, 0xD26C_4D12, 0xDF2F_6BCB, 0xDBEE_767C,
|
||||
0xE3A1_CBC1, 0xE760_D676, 0xEA23_F0AF, 0xEEE2_ED18, 0xF0A5_BD1D, 0xF464_A0AA, 0xF927_8673, 0xFDE6_9BC4,
|
||||
0x89B8_FD09, 0x8D79_E0BE, 0x803A_C667, 0x84FB_DBD0, 0x9ABC_8BD5, 0x9E7D_9662, 0x933E_B0BB, 0x97FF_AD0C,
|
||||
0xAFB0_10B1, 0xAB71_0D06, 0xA632_2BDF, 0xA2F3_3668, 0xBCB4_666D, 0xB875_7BDA, 0xB536_5D03, 0xB1F7_40B4,
|
||||
0x0000_0000, 0x04C1_1DB7, 0x0982_3B6E, 0x0D43_26D9, 0x1304_76DC, 0x17C5_6B6B, 0x1A86_4DB2, 0x1E47_5005,
|
||||
0x2608_EDB8, 0x22C9_F00F, 0x2F8A_D6D6, 0x2B4B_CB61, 0x350C_9B64, 0x31CD_86D3, 0x3C8E_A00A, 0x384F_BDBD,
|
||||
0x4C11_DB70, 0x48D0_C6C7, 0x4593_E01E, 0x4152_FDA9, 0x5F15_ADAC, 0x5BD4_B01B, 0x5697_96C2, 0x5256_8B75,
|
||||
0x6A19_36C8, 0x6ED8_2B7F, 0x639B_0DA6, 0x675A_1011, 0x791D_4014, 0x7DDC_5DA3, 0x709F_7B7A, 0x745E_66CD,
|
||||
0x9823_B6E0, 0x9CE2_AB57, 0x91A1_8D8E, 0x9560_9039, 0x8B27_C03C, 0x8FE6_DD8B, 0x82A5_FB52, 0x8664_E6E5,
|
||||
0xBE2B_5B58, 0xBAEA_46EF, 0xB7A9_6036, 0xB368_7D81, 0xAD2F_2D84, 0xA9EE_3033, 0xA4AD_16EA, 0xA06C_0B5D,
|
||||
0xD432_6D90, 0xD0F3_7027, 0xDDB0_56FE, 0xD971_4B49, 0xC736_1B4C, 0xC3F7_06FB, 0xCEB4_2022, 0xCA75_3D95,
|
||||
0xF23A_8028, 0xF6FB_9D9F, 0xFBB8_BB46, 0xFF79_A6F1, 0xE13E_F6F4, 0xE5FF_EB43, 0xE8BC_CD9A, 0xEC7D_D02D,
|
||||
0x3486_7077, 0x3047_6DC0, 0x3D04_4B19, 0x39C5_56AE, 0x2782_06AB, 0x2343_1B1C, 0x2E00_3DC5, 0x2AC1_2072,
|
||||
0x128E_9DCF, 0x164F_8078, 0x1B0C_A6A1, 0x1FCD_BB16, 0x018A_EB13, 0x054B_F6A4, 0x0808_D07D, 0x0CC9_CDCA,
|
||||
0x7897_AB07, 0x7C56_B6B0, 0x7115_9069, 0x75D4_8DDE, 0x6B93_DDDB, 0x6F52_C06C, 0x6211_E6B5, 0x66D0_FB02,
|
||||
0x5E9F_46BF, 0x5A5E_5B08, 0x571D_7DD1, 0x53DC_6066, 0x4D9B_3063, 0x495A_2DD4, 0x4419_0B0D, 0x40D8_16BA,
|
||||
0xACA5_C697, 0xA864_DB20, 0xA527_FDF9, 0xA1E6_E04E, 0xBFA1_B04B, 0xBB60_ADFC, 0xB623_8B25, 0xB2E2_9692,
|
||||
0x8AAD_2B2F, 0x8E6C_3698, 0x832F_1041, 0x87EE_0DF6, 0x99A9_5DF3, 0x9D68_4044, 0x902B_669D, 0x94EA_7B2A,
|
||||
0xE0B4_1DE7, 0xE475_0050, 0xE936_2689, 0xEDF7_3B3E, 0xF3B0_6B3B, 0xF771_768C, 0xFA32_5055, 0xFEF3_4DE2,
|
||||
0xC6BC_F05F, 0xC27D_EDE8, 0xCF3E_CB31, 0xCBFF_D686, 0xD5B8_8683, 0xD179_9B34, 0xDC3A_BDED, 0xD8FB_A05A,
|
||||
0x690C_E0EE, 0x6DCD_FD59, 0x608E_DB80, 0x644F_C637, 0x7A08_9632, 0x7EC9_8B85, 0x738A_AD5C, 0x774B_B0EB,
|
||||
0x4F04_0D56, 0x4BC5_10E1, 0x4686_3638, 0x4247_2B8F, 0x5C00_7B8A, 0x58C1_663D, 0x5582_40E4, 0x5143_5D53,
|
||||
0x251D_3B9E, 0x21DC_2629, 0x2C9F_00F0, 0x285E_1D47, 0x3619_4D42, 0x32D8_50F5, 0x3F9B_762C, 0x3B5A_6B9B,
|
||||
0x0315_D626, 0x07D4_CB91, 0x0A97_ED48, 0x0E56_F0FF, 0x1011_A0FA, 0x14D0_BD4D, 0x1993_9B94, 0x1D52_8623,
|
||||
0xF12F_560E, 0xF5EE_4BB9, 0xF8AD_6D60, 0xFC6C_70D7, 0xE22B_20D2, 0xE6EA_3D65, 0xEBA9_1BBC, 0xEF68_060B,
|
||||
0xD727_BBB6, 0xD3E6_A601, 0xDEA5_80D8, 0xDA64_9D6F, 0xC423_CD6A, 0xC0E2_D0DD, 0xCDA1_F604, 0xC960_EBB3,
|
||||
0xBD3E_8D7E, 0xB9FF_90C9, 0xB4BC_B610, 0xB07D_ABA7, 0xAE3A_FBA2, 0xAAFB_E615, 0xA7B8_C0CC, 0xA379_DD7B,
|
||||
0x9B36_60C6, 0x9FF7_7D71, 0x92B4_5BA8, 0x9675_461F, 0x8832_161A, 0x8CF3_0BAD, 0x81B0_2D74, 0x8571_30C3,
|
||||
0x5D8A_9099, 0x594B_8D2E, 0x5408_ABF7, 0x50C9_B640, 0x4E8E_E645, 0x4A4F_FBF2, 0x470C_DD2B, 0x43CD_C09C,
|
||||
0x7B82_7D21, 0x7F43_6096, 0x7200_464F, 0x76C1_5BF8, 0x6886_0BFD, 0x6C47_164A, 0x6104_3093, 0x65C5_2D24,
|
||||
0x119B_4BE9, 0x155A_565E, 0x1819_7087, 0x1CD8_6D30, 0x029F_3D35, 0x065E_2082, 0x0B1D_065B, 0x0FDC_1BEC,
|
||||
0x3793_A651, 0x3352_BBE6, 0x3E11_9D3F, 0x3AD0_8088, 0x2497_D08D, 0x2056_CD3A, 0x2D15_EBE3, 0x29D4_F654,
|
||||
0xC5A9_2679, 0xC168_3BCE, 0xCC2B_1D17, 0xC8EA_00A0, 0xD6AD_50A5, 0xD26C_4D12, 0xDF2F_6BCB, 0xDBEE_767C,
|
||||
0xE3A1_CBC1, 0xE760_D676, 0xEA23_F0AF, 0xEEE2_ED18, 0xF0A5_BD1D, 0xF464_A0AA, 0xF927_8673, 0xFDE6_9BC4,
|
||||
0x89B8_FD09, 0x8D79_E0BE, 0x803A_C667, 0x84FB_DBD0, 0x9ABC_8BD5, 0x9E7D_9662, 0x933E_B0BB, 0x97FF_AD0C,
|
||||
0xAFB0_10B1, 0xAB71_0D06, 0xA632_2BDF, 0xA2F3_3668, 0xBCB4_666D, 0xB875_7BDA, 0xB536_5D03, 0xB1F7_40B4,
|
||||
]
|
||||
@@ -13,65 +13,65 @@
|
||||
// limitations under the License.
|
||||
|
||||
pri func decoder.flush_fast!(dst: base.io_writer) {
|
||||
var flush_pointer : base.u32[..= 1_048575]
|
||||
var flush_repeat_count : base.u32[..= 255]
|
||||
var flush_prev : base.u8
|
||||
var block_checksum_have : base.u32
|
||||
var block_size : base.u32
|
||||
var flush_pointer : base.u32[..= 1_048575]
|
||||
var flush_repeat_count : base.u32[..= 255]
|
||||
var flush_prev : base.u8
|
||||
var block_checksum_have : base.u32
|
||||
var block_size : base.u32
|
||||
|
||||
var entry : base.u32
|
||||
var curr : base.u8
|
||||
var entry : base.u32
|
||||
var curr : base.u8
|
||||
|
||||
flush_pointer = this.flush_pointer
|
||||
flush_repeat_count = this.flush_repeat_count
|
||||
flush_prev = this.flush_prev
|
||||
block_checksum_have = this.block_checksum_have
|
||||
block_size = this.block_size
|
||||
flush_pointer = this.flush_pointer
|
||||
flush_repeat_count = this.flush_repeat_count
|
||||
flush_prev = this.flush_prev
|
||||
block_checksum_have = this.block_checksum_have
|
||||
block_size = this.block_size
|
||||
|
||||
while (block_size > 0) and (args.dst.length() >= 255) {
|
||||
if flush_repeat_count < 4 {
|
||||
entry = this.bwt[flush_pointer]
|
||||
curr = (entry & 0xFF) as base.u8
|
||||
flush_pointer = entry >> 12
|
||||
if curr == flush_prev {
|
||||
flush_repeat_count += 1
|
||||
} else {
|
||||
flush_repeat_count = 1
|
||||
}
|
||||
block_checksum_have =
|
||||
REV_CRC32_TABLE[((block_checksum_have >> 24) as base.u8) ^ curr] ^
|
||||
(block_checksum_have ~mod<< 8)
|
||||
args.dst.write_u8_fast!(a: curr)
|
||||
flush_prev = curr
|
||||
block_size -= 1
|
||||
while (block_size > 0) and (args.dst.length() >= 255) {
|
||||
if flush_repeat_count < 4 {
|
||||
entry = this.bwt[flush_pointer]
|
||||
curr = (entry & 0xFF) as base.u8
|
||||
flush_pointer = entry >> 12
|
||||
if curr == flush_prev {
|
||||
flush_repeat_count += 1
|
||||
} else {
|
||||
flush_repeat_count = 1
|
||||
}
|
||||
block_checksum_have =
|
||||
REV_CRC32_TABLE[((block_checksum_have >> 24) as base.u8) ^ curr] ^
|
||||
(block_checksum_have ~mod<< 8)
|
||||
args.dst.write_u8_fast!(a: curr)
|
||||
flush_prev = curr
|
||||
block_size -= 1
|
||||
|
||||
} else {
|
||||
entry = this.bwt[flush_pointer]
|
||||
curr = (entry & 0xFF) as base.u8
|
||||
flush_pointer = entry >> 12
|
||||
flush_repeat_count = curr as base.u32
|
||||
while flush_repeat_count > 0,
|
||||
inv block_size > 0,
|
||||
{
|
||||
block_checksum_have =
|
||||
REV_CRC32_TABLE[((block_checksum_have >> 24) as base.u8) ^ flush_prev] ^
|
||||
(block_checksum_have ~mod<< 8)
|
||||
if args.dst.length() > 0 {
|
||||
args.dst.write_u8_fast!(a: flush_prev)
|
||||
}
|
||||
flush_repeat_count -= 1
|
||||
} endwhile
|
||||
flush_repeat_count = 0
|
||||
flush_prev = curr
|
||||
block_size -= 1
|
||||
}
|
||||
} endwhile
|
||||
} else {
|
||||
entry = this.bwt[flush_pointer]
|
||||
curr = (entry & 0xFF) as base.u8
|
||||
flush_pointer = entry >> 12
|
||||
flush_repeat_count = curr as base.u32
|
||||
while flush_repeat_count > 0,
|
||||
inv block_size > 0,
|
||||
{
|
||||
block_checksum_have =
|
||||
REV_CRC32_TABLE[((block_checksum_have >> 24) as base.u8) ^ flush_prev] ^
|
||||
(block_checksum_have ~mod<< 8)
|
||||
if args.dst.length() > 0 {
|
||||
args.dst.write_u8_fast!(a: flush_prev)
|
||||
}
|
||||
flush_repeat_count -= 1
|
||||
} endwhile
|
||||
flush_repeat_count = 0
|
||||
flush_prev = curr
|
||||
block_size -= 1
|
||||
}
|
||||
} endwhile
|
||||
|
||||
this.flush_pointer = flush_pointer
|
||||
this.flush_repeat_count = flush_repeat_count
|
||||
this.flush_prev = flush_prev
|
||||
this.block_checksum_have = block_checksum_have
|
||||
if block_size <= 900000 {
|
||||
this.block_size = block_size
|
||||
}
|
||||
this.flush_pointer = flush_pointer
|
||||
this.flush_repeat_count = flush_repeat_count
|
||||
this.flush_prev = flush_prev
|
||||
this.block_checksum_have = block_checksum_have
|
||||
if block_size <= 900000 {
|
||||
this.block_size = block_size
|
||||
}
|
||||
}
|
||||
@@ -13,63 +13,63 @@
|
||||
// limitations under the License.
|
||||
|
||||
pri func decoder.flush_slow?(dst: base.io_writer) {
|
||||
var flush_pointer : base.u32[..= 1_048575]
|
||||
var flush_repeat_count : base.u32[..= 255]
|
||||
var flush_prev : base.u8
|
||||
var block_checksum_have : base.u32
|
||||
var block_size : base.u32
|
||||
var flush_pointer : base.u32[..= 1_048575]
|
||||
var flush_repeat_count : base.u32[..= 255]
|
||||
var flush_prev : base.u8
|
||||
var block_checksum_have : base.u32
|
||||
var block_size : base.u32
|
||||
|
||||
var entry : base.u32
|
||||
var curr : base.u8
|
||||
var entry : base.u32
|
||||
var curr : base.u8
|
||||
|
||||
flush_pointer = this.flush_pointer
|
||||
flush_repeat_count = this.flush_repeat_count
|
||||
flush_prev = this.flush_prev
|
||||
block_checksum_have = this.block_checksum_have
|
||||
block_size = this.block_size
|
||||
flush_pointer = this.flush_pointer
|
||||
flush_repeat_count = this.flush_repeat_count
|
||||
flush_prev = this.flush_prev
|
||||
block_checksum_have = this.block_checksum_have
|
||||
block_size = this.block_size
|
||||
|
||||
while (block_size > 0) and (not coroutine_resumed) {
|
||||
if flush_repeat_count < 4 {
|
||||
entry = this.bwt[flush_pointer]
|
||||
curr = (entry & 0xFF) as base.u8
|
||||
flush_pointer = entry >> 12
|
||||
if curr == flush_prev {
|
||||
flush_repeat_count += 1
|
||||
} else {
|
||||
flush_repeat_count = 1
|
||||
}
|
||||
block_checksum_have =
|
||||
REV_CRC32_TABLE[((block_checksum_have >> 24) as base.u8) ^ curr] ^
|
||||
(block_checksum_have ~mod<< 8)
|
||||
args.dst.write_u8?(a: curr)
|
||||
flush_prev = curr
|
||||
block_size -= 1
|
||||
while (block_size > 0) and (not coroutine_resumed) {
|
||||
if flush_repeat_count < 4 {
|
||||
entry = this.bwt[flush_pointer]
|
||||
curr = (entry & 0xFF) as base.u8
|
||||
flush_pointer = entry >> 12
|
||||
if curr == flush_prev {
|
||||
flush_repeat_count += 1
|
||||
} else {
|
||||
flush_repeat_count = 1
|
||||
}
|
||||
block_checksum_have =
|
||||
REV_CRC32_TABLE[((block_checksum_have >> 24) as base.u8) ^ curr] ^
|
||||
(block_checksum_have ~mod<< 8)
|
||||
args.dst.write_u8?(a: curr)
|
||||
flush_prev = curr
|
||||
block_size -= 1
|
||||
|
||||
} else {
|
||||
entry = this.bwt[flush_pointer]
|
||||
curr = (entry & 0xFF) as base.u8
|
||||
flush_pointer = entry >> 12
|
||||
flush_repeat_count = curr as base.u32
|
||||
while flush_repeat_count > 0,
|
||||
inv block_size > 0,
|
||||
{
|
||||
block_checksum_have =
|
||||
REV_CRC32_TABLE[((block_checksum_have >> 24) as base.u8) ^ flush_prev] ^
|
||||
(block_checksum_have ~mod<< 8)
|
||||
args.dst.write_u8?(a: flush_prev)
|
||||
flush_repeat_count -= 1
|
||||
} endwhile
|
||||
flush_repeat_count = 0
|
||||
flush_prev = curr
|
||||
block_size -= 1
|
||||
}
|
||||
} endwhile
|
||||
} else {
|
||||
entry = this.bwt[flush_pointer]
|
||||
curr = (entry & 0xFF) as base.u8
|
||||
flush_pointer = entry >> 12
|
||||
flush_repeat_count = curr as base.u32
|
||||
while flush_repeat_count > 0,
|
||||
inv block_size > 0,
|
||||
{
|
||||
block_checksum_have =
|
||||
REV_CRC32_TABLE[((block_checksum_have >> 24) as base.u8) ^ flush_prev] ^
|
||||
(block_checksum_have ~mod<< 8)
|
||||
args.dst.write_u8?(a: flush_prev)
|
||||
flush_repeat_count -= 1
|
||||
} endwhile
|
||||
flush_repeat_count = 0
|
||||
flush_prev = curr
|
||||
block_size -= 1
|
||||
}
|
||||
} endwhile
|
||||
|
||||
this.flush_pointer = flush_pointer
|
||||
this.flush_repeat_count = flush_repeat_count
|
||||
this.flush_prev = flush_prev
|
||||
this.block_checksum_have = block_checksum_have
|
||||
if block_size <= 900000 {
|
||||
this.block_size = block_size
|
||||
}
|
||||
this.flush_pointer = flush_pointer
|
||||
this.flush_repeat_count = flush_repeat_count
|
||||
this.flush_prev = flush_prev
|
||||
this.block_checksum_have = block_checksum_have
|
||||
if block_size <= 900000 {
|
||||
this.block_size = block_size
|
||||
}
|
||||
}
|
||||
+100
-100
@@ -13,115 +13,115 @@
|
||||
// limitations under the License.
|
||||
|
||||
pri func decoder.decode_huffman_fast!(src: base.io_reader) base.status {
|
||||
var bits : base.u32
|
||||
var n_bits : base.u32[..= 31]
|
||||
var block_size : base.u32[..= 900000]
|
||||
var which : base.u8[..= 5]
|
||||
var ticks : base.u32[..= 50]
|
||||
var section : base.u32
|
||||
var run_shift : base.u32[..= 23]
|
||||
var bits : base.u32
|
||||
var n_bits : base.u32[..= 31]
|
||||
var block_size : base.u32[..= 900000]
|
||||
var which : base.u8[..= 5]
|
||||
var ticks : base.u32[..= 50]
|
||||
var section : base.u32
|
||||
var run_shift : base.u32[..= 23]
|
||||
|
||||
var table_entry : base.u16
|
||||
var child : base.u16
|
||||
var child_ff : base.u32[..= 255]
|
||||
var i : base.u32
|
||||
var j : base.u32
|
||||
var output : base.u32[..= 255]
|
||||
var run : base.u32[..= 12_582912] // 12_582912 = (3 << 22)
|
||||
var mtft0 : base.u32[..= 255]
|
||||
var table_entry : base.u16
|
||||
var child : base.u16
|
||||
var child_ff : base.u32[..= 255]
|
||||
var i : base.u32
|
||||
var j : base.u32
|
||||
var output : base.u32[..= 255]
|
||||
var run : base.u32[..= 12_582912] // 12_582912 = (3 << 22)
|
||||
var mtft0 : base.u32[..= 255]
|
||||
|
||||
bits = this.bits
|
||||
n_bits = this.n_bits
|
||||
block_size = this.block_size
|
||||
which = this.decode_huffman_which
|
||||
ticks = this.decode_huffman_ticks
|
||||
section = this.decode_huffman_section
|
||||
run_shift = this.decode_huffman_run_shift
|
||||
bits = this.bits
|
||||
n_bits = this.n_bits
|
||||
block_size = this.block_size
|
||||
which = this.decode_huffman_which
|
||||
ticks = this.decode_huffman_ticks
|
||||
section = this.decode_huffman_section
|
||||
run_shift = this.decode_huffman_run_shift
|
||||
|
||||
// Apply the Huffman trees.
|
||||
while.outer args.src.length() >= 4 {
|
||||
if ticks > 0 {
|
||||
ticks -= 1
|
||||
} else {
|
||||
ticks = 49
|
||||
section ~mod+= 1
|
||||
if section >= this.num_sections {
|
||||
return "#bad number of sections"
|
||||
}
|
||||
which = CLAMP_TO_5[this.huffman_selectors[section & 32767] & 7]
|
||||
}
|
||||
// Apply the Huffman trees.
|
||||
while.outer args.src.length() >= 4 {
|
||||
if ticks > 0 {
|
||||
ticks -= 1
|
||||
} else {
|
||||
ticks = 49
|
||||
section ~mod+= 1
|
||||
if section >= this.num_sections {
|
||||
return "#bad number of sections"
|
||||
}
|
||||
which = CLAMP_TO_5[this.huffman_selectors[section & 32767] & 7]
|
||||
}
|
||||
|
||||
bits |= args.src.peek_u32be() >> n_bits
|
||||
args.src.skip_u32_fast!(actual: (31 - n_bits) >> 3, worst_case: 4)
|
||||
n_bits |= 24
|
||||
bits |= args.src.peek_u32be() >> n_bits
|
||||
args.src.skip_u32_fast!(actual: (31 - n_bits) >> 3, worst_case: 4)
|
||||
n_bits |= 24
|
||||
|
||||
table_entry = this.huffman_tables[which][bits >> 24]
|
||||
bits ~mod<<= table_entry >> 12
|
||||
n_bits -= (table_entry >> 12) as base.u32
|
||||
child = table_entry & 0x3FF
|
||||
while child < 257 {
|
||||
child = this.huffman_trees[which][child][bits >> 31]
|
||||
bits ~mod<<= 1
|
||||
if n_bits <= 0 {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
n_bits -= 1
|
||||
} endwhile
|
||||
table_entry = this.huffman_tables[which][bits >> 24]
|
||||
bits ~mod<<= table_entry >> 12
|
||||
n_bits -= (table_entry >> 12) as base.u32
|
||||
child = table_entry & 0x3FF
|
||||
while child < 257 {
|
||||
child = this.huffman_trees[which][child][bits >> 31]
|
||||
bits ~mod<<= 1
|
||||
if n_bits <= 0 {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
n_bits -= 1
|
||||
} endwhile
|
||||
|
||||
if child < 0x300 { // mNN symbol.
|
||||
// Move to front.
|
||||
child_ff = (child & 0xFF) as base.u32
|
||||
output = this.mtft[child_ff] as base.u32
|
||||
this.mtft[1 .. 1 + child_ff].copy_from_slice!(
|
||||
s: this.mtft[.. child_ff])
|
||||
this.mtft[0] = output as base.u8
|
||||
if child < 0x300 { // mNN symbol.
|
||||
// Move to front.
|
||||
child_ff = (child & 0xFF) as base.u32
|
||||
output = this.mtft[child_ff] as base.u32
|
||||
this.mtft[1 .. 1 + child_ff].copy_from_slice!(
|
||||
s: this.mtft[.. child_ff])
|
||||
this.mtft[0] = output as base.u8
|
||||
|
||||
this.letter_counts[output] ~mod+= 1
|
||||
this.bwt[block_size] = output
|
||||
if block_size >= this.max_incl_block_size {
|
||||
return "#bad block length"
|
||||
}
|
||||
assert block_size < 900000 via "a < b: a < c; c <= b"(c: this.max_incl_block_size)
|
||||
block_size += 1
|
||||
run_shift = 0
|
||||
continue.outer
|
||||
} else if child == 0x300 { // EOB symbol.
|
||||
this.decode_huffman_finished = true
|
||||
break.outer
|
||||
}
|
||||
this.letter_counts[output] ~mod+= 1
|
||||
this.bwt[block_size] = output
|
||||
if block_size >= this.max_incl_block_size {
|
||||
return "#bad block length"
|
||||
}
|
||||
assert block_size < 900000 via "a < b: a < c; c <= b"(c: this.max_incl_block_size)
|
||||
block_size += 1
|
||||
run_shift = 0
|
||||
continue.outer
|
||||
} else if child == 0x300 { // EOB symbol.
|
||||
this.decode_huffman_finished = true
|
||||
break.outer
|
||||
}
|
||||
|
||||
// RUNA or RUNB symbol.
|
||||
if run_shift >= 23 {
|
||||
return "#bad block length"
|
||||
}
|
||||
run = ((child as base.u32) & 3) << run_shift
|
||||
run_shift += 1
|
||||
i = block_size
|
||||
j = run + block_size
|
||||
if j > this.max_incl_block_size {
|
||||
return "#bad block length"
|
||||
}
|
||||
assert j <= 900000 via "a <= b: a <= c; c <= b"(c: this.max_incl_block_size)
|
||||
block_size = j
|
||||
// RUNA or RUNB symbol.
|
||||
if run_shift >= 23 {
|
||||
return "#bad block length"
|
||||
}
|
||||
run = ((child as base.u32) & 3) << run_shift
|
||||
run_shift += 1
|
||||
i = block_size
|
||||
j = run + block_size
|
||||
if j > this.max_incl_block_size {
|
||||
return "#bad block length"
|
||||
}
|
||||
assert j <= 900000 via "a <= b: a <= c; c <= b"(c: this.max_incl_block_size)
|
||||
block_size = j
|
||||
|
||||
mtft0 = this.mtft[0] as base.u32
|
||||
this.letter_counts[mtft0] ~mod+= run
|
||||
while i < j,
|
||||
pre j <= 900000,
|
||||
{
|
||||
assert i < 900000 via "a < b: a < c; c <= b"(c: j)
|
||||
this.bwt[i] = mtft0
|
||||
i += 1
|
||||
} endwhile
|
||||
} endwhile.outer
|
||||
mtft0 = this.mtft[0] as base.u32
|
||||
this.letter_counts[mtft0] ~mod+= run
|
||||
while i < j,
|
||||
pre j <= 900000,
|
||||
{
|
||||
assert i < 900000 via "a < b: a < c; c <= b"(c: j)
|
||||
this.bwt[i] = mtft0
|
||||
i += 1
|
||||
} endwhile
|
||||
} endwhile.outer
|
||||
|
||||
this.bits = bits
|
||||
this.n_bits = n_bits
|
||||
this.block_size = block_size
|
||||
this.decode_huffman_which = which
|
||||
this.decode_huffman_ticks = ticks
|
||||
this.decode_huffman_section = section
|
||||
this.decode_huffman_run_shift = run_shift
|
||||
this.bits = bits
|
||||
this.n_bits = n_bits
|
||||
this.block_size = block_size
|
||||
this.decode_huffman_which = which
|
||||
this.decode_huffman_ticks = ticks
|
||||
this.decode_huffman_section = section
|
||||
this.decode_huffman_run_shift = run_shift
|
||||
|
||||
return ok
|
||||
return ok
|
||||
}
|
||||
@@ -13,93 +13,93 @@
|
||||
// limitations under the License.
|
||||
|
||||
pri func decoder.decode_huffman_slow?(src: base.io_reader) {
|
||||
var c : base.u8
|
||||
var node_index : base.u32[..= 256]
|
||||
var c : base.u8
|
||||
var node_index : base.u32[..= 256]
|
||||
|
||||
var child : base.u16
|
||||
var child_ff : base.u32[..= 255]
|
||||
var i : base.u32
|
||||
var j : base.u32
|
||||
var output : base.u32[..= 255]
|
||||
var run : base.u32[..= 12_582912] // 12_582912 = (3 << 22)
|
||||
var mtft0 : base.u32[..= 255]
|
||||
var child : base.u16
|
||||
var child_ff : base.u32[..= 255]
|
||||
var i : base.u32
|
||||
var j : base.u32
|
||||
var output : base.u32[..= 255]
|
||||
var run : base.u32[..= 12_582912] // 12_582912 = (3 << 22)
|
||||
var mtft0 : base.u32[..= 255]
|
||||
|
||||
// Apply the Huffman trees.
|
||||
while.outer not coroutine_resumed {
|
||||
if this.decode_huffman_ticks > 0 {
|
||||
this.decode_huffman_ticks -= 1
|
||||
} else {
|
||||
this.decode_huffman_ticks = 49
|
||||
this.decode_huffman_section ~mod+= 1
|
||||
if this.decode_huffman_section >= this.num_sections {
|
||||
return "#bad number of sections"
|
||||
}
|
||||
this.decode_huffman_which = CLAMP_TO_5[this.huffman_selectors[this.decode_huffman_section & 32767] & 7]
|
||||
}
|
||||
// Apply the Huffman trees.
|
||||
while.outer not coroutine_resumed {
|
||||
if this.decode_huffman_ticks > 0 {
|
||||
this.decode_huffman_ticks -= 1
|
||||
} else {
|
||||
this.decode_huffman_ticks = 49
|
||||
this.decode_huffman_section ~mod+= 1
|
||||
if this.decode_huffman_section >= this.num_sections {
|
||||
return "#bad number of sections"
|
||||
}
|
||||
this.decode_huffman_which = CLAMP_TO_5[this.huffman_selectors[this.decode_huffman_section & 32767] & 7]
|
||||
}
|
||||
|
||||
node_index = 0
|
||||
while true {
|
||||
if this.n_bits <= 0 {
|
||||
c = args.src.read_u8?()
|
||||
this.bits = (c as base.u32) << 24
|
||||
this.n_bits = 8
|
||||
assert this.n_bits > 0
|
||||
}
|
||||
child = this.huffman_trees[this.decode_huffman_which][node_index][this.bits >> 31]
|
||||
this.bits ~mod<<= 1
|
||||
this.n_bits -= 1
|
||||
node_index = 0
|
||||
while true {
|
||||
if this.n_bits <= 0 {
|
||||
c = args.src.read_u8?()
|
||||
this.bits = (c as base.u32) << 24
|
||||
this.n_bits = 8
|
||||
assert this.n_bits > 0
|
||||
}
|
||||
child = this.huffman_trees[this.decode_huffman_which][node_index][this.bits >> 31]
|
||||
this.bits ~mod<<= 1
|
||||
this.n_bits -= 1
|
||||
|
||||
if child < 257 { // Branch node.
|
||||
node_index = child as base.u32
|
||||
continue
|
||||
if child < 257 { // Branch node.
|
||||
node_index = child as base.u32
|
||||
continue
|
||||
|
||||
} else if child < 0x300 { // mNN symbol.
|
||||
// Move to front.
|
||||
child_ff = (child & 0xFF) as base.u32
|
||||
output = this.mtft[child_ff] as base.u32
|
||||
this.mtft[1 .. 1 + child_ff].copy_from_slice!(
|
||||
s: this.mtft[.. child_ff])
|
||||
this.mtft[0] = output as base.u8
|
||||
} else if child < 0x300 { // mNN symbol.
|
||||
// Move to front.
|
||||
child_ff = (child & 0xFF) as base.u32
|
||||
output = this.mtft[child_ff] as base.u32
|
||||
this.mtft[1 .. 1 + child_ff].copy_from_slice!(
|
||||
s: this.mtft[.. child_ff])
|
||||
this.mtft[0] = output as base.u8
|
||||
|
||||
this.letter_counts[output] ~mod+= 1
|
||||
this.bwt[this.block_size] = output
|
||||
if this.block_size >= this.max_incl_block_size {
|
||||
return "#bad block length"
|
||||
}
|
||||
assert this.block_size < 900000 via "a < b: a < c; c <= b"(c: this.max_incl_block_size)
|
||||
this.block_size += 1
|
||||
this.decode_huffman_run_shift = 0
|
||||
break
|
||||
this.letter_counts[output] ~mod+= 1
|
||||
this.bwt[this.block_size] = output
|
||||
if this.block_size >= this.max_incl_block_size {
|
||||
return "#bad block length"
|
||||
}
|
||||
assert this.block_size < 900000 via "a < b: a < c; c <= b"(c: this.max_incl_block_size)
|
||||
this.block_size += 1
|
||||
this.decode_huffman_run_shift = 0
|
||||
break
|
||||
|
||||
} else if child == 0x300 { // EOB symbol.
|
||||
this.decode_huffman_finished = true
|
||||
break.outer
|
||||
}
|
||||
} else if child == 0x300 { // EOB symbol.
|
||||
this.decode_huffman_finished = true
|
||||
break.outer
|
||||
}
|
||||
|
||||
// RUNA or RUNB symbol.
|
||||
if this.decode_huffman_run_shift >= 23 {
|
||||
return "#bad block length"
|
||||
}
|
||||
run = ((child as base.u32) & 3) << this.decode_huffman_run_shift
|
||||
this.decode_huffman_run_shift += 1
|
||||
i = this.block_size
|
||||
j = run + this.block_size
|
||||
if j > this.max_incl_block_size {
|
||||
return "#bad block length"
|
||||
}
|
||||
assert j <= 900000 via "a <= b: a <= c; c <= b"(c: this.max_incl_block_size)
|
||||
this.block_size = j
|
||||
// RUNA or RUNB symbol.
|
||||
if this.decode_huffman_run_shift >= 23 {
|
||||
return "#bad block length"
|
||||
}
|
||||
run = ((child as base.u32) & 3) << this.decode_huffman_run_shift
|
||||
this.decode_huffman_run_shift += 1
|
||||
i = this.block_size
|
||||
j = run + this.block_size
|
||||
if j > this.max_incl_block_size {
|
||||
return "#bad block length"
|
||||
}
|
||||
assert j <= 900000 via "a <= b: a <= c; c <= b"(c: this.max_incl_block_size)
|
||||
this.block_size = j
|
||||
|
||||
mtft0 = this.mtft[0] as base.u32
|
||||
this.letter_counts[mtft0] ~mod+= run
|
||||
while i < j,
|
||||
pre j <= 900000,
|
||||
{
|
||||
assert i < 900000 via "a < b: a < c; c <= b"(c: j)
|
||||
this.bwt[i] = mtft0
|
||||
i += 1
|
||||
} endwhile
|
||||
break
|
||||
} endwhile
|
||||
} endwhile.outer
|
||||
mtft0 = this.mtft[0] as base.u32
|
||||
this.letter_counts[mtft0] ~mod+= run
|
||||
while i < j,
|
||||
pre j <= 900000,
|
||||
{
|
||||
assert i < 900000 via "a < b: a < c; c <= b"(c: j)
|
||||
this.bwt[i] = mtft0
|
||||
i += 1
|
||||
} endwhile
|
||||
break
|
||||
} endwhile
|
||||
} endwhile.outer
|
||||
}
|
||||
+660
-660
@@ -77,720 +77,720 @@ pub const TOKEN_VALUE_MINOR__TAG : base.u32 = 0x040_0000
|
||||
// --------
|
||||
|
||||
pri const LITERALS : array[4] base.u32[..= 0x1FF_FFFF] = [
|
||||
(base.TOKEN__VBC__LITERAL << 21) | base.TOKEN__VBD__LITERAL__FALSE,
|
||||
(base.TOKEN__VBC__LITERAL << 21) | base.TOKEN__VBD__LITERAL__TRUE,
|
||||
(base.TOKEN__VBC__LITERAL << 21) | base.TOKEN__VBD__LITERAL__NULL,
|
||||
(base.TOKEN__VBC__LITERAL << 21) | base.TOKEN__VBD__LITERAL__UNDEFINED,
|
||||
(base.TOKEN__VBC__LITERAL << 21) | base.TOKEN__VBD__LITERAL__FALSE,
|
||||
(base.TOKEN__VBC__LITERAL << 21) | base.TOKEN__VBD__LITERAL__TRUE,
|
||||
(base.TOKEN__VBC__LITERAL << 21) | base.TOKEN__VBD__LITERAL__NULL,
|
||||
(base.TOKEN__VBC__LITERAL << 21) | base.TOKEN__VBD__LITERAL__UNDEFINED,
|
||||
]
|
||||
|
||||
pri const TOKEN_LENGTHS : array[32] base.u8[..= 9] = [
|
||||
1, 1, 1, 1, 1, 1, 1, 1,
|
||||
1, 1, 1, 1, 1, 1, 1, 1,
|
||||
1, 1, 1, 1, 1, 1, 1, 1,
|
||||
2, 3, 5, 9, 0, 0, 0, 1,
|
||||
1, 1, 1, 1, 1, 1, 1, 1,
|
||||
1, 1, 1, 1, 1, 1, 1, 1,
|
||||
1, 1, 1, 1, 1, 1, 1, 1,
|
||||
2, 3, 5, 9, 0, 0, 0, 1,
|
||||
]
|
||||
|
||||
pub struct decoder? implements base.token_decoder(
|
||||
end_of_data : base.bool,
|
||||
end_of_data : base.bool,
|
||||
|
||||
util : base.utility,
|
||||
util : base.utility,
|
||||
)(
|
||||
// stack is conceptually an array of 2-bit integers, implemented as an
|
||||
// array of u32. The N'th 2-bit pair is whether we're in an array or
|
||||
// object, where N is the recursion depth:
|
||||
// - 0b00 In an array.
|
||||
// - 0b01 In an object, expecting a value.
|
||||
// - 0b10 In an array.
|
||||
// - 0b11 In an object, expecting a key.
|
||||
//
|
||||
// The maximum recursion depth, 1024, is the same as for std/json.
|
||||
stack : array[1024 / 16] base.u32,
|
||||
// stack is conceptually an array of 2-bit integers, implemented as an
|
||||
// array of u32. The N'th 2-bit pair is whether we're in an array or
|
||||
// object, where N is the recursion depth:
|
||||
// - 0b00 In an array.
|
||||
// - 0b01 In an object, expecting a value.
|
||||
// - 0b10 In an array.
|
||||
// - 0b11 In an object, expecting a key.
|
||||
//
|
||||
// The maximum recursion depth, 1024, is the same as for std/json.
|
||||
stack : array[1024 / 16] base.u32,
|
||||
|
||||
// container_num_remaining[i] is the number of elements remaining in the
|
||||
// open containers, for i ranging in 0 .. depth. If (i < depth) and
|
||||
// (container_num_remaining[i] == 0) then the i'th open container has
|
||||
// indefinite length.
|
||||
container_num_remaining : array[1024] base.u64,
|
||||
// container_num_remaining[i] is the number of elements remaining in the
|
||||
// open containers, for i ranging in 0 .. depth. If (i < depth) and
|
||||
// (container_num_remaining[i] == 0) then the i'th open container has
|
||||
// indefinite length.
|
||||
container_num_remaining : array[1024] base.u64,
|
||||
)
|
||||
|
||||
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()
|
||||
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 string_length : base.u64
|
||||
|
||||
var n64 : base.u64
|
||||
var depth : base.u32[..= 1024]
|
||||
var stack_byte : base.u32[..= (1024 / 16) - 1]
|
||||
var stack_bit : base.u32[..= 30]
|
||||
var stack_val : base.u32[..= 3]
|
||||
var token_length : base.u32[..= 0xFFFF]
|
||||
var vminor : base.u32[..= 0x1FF_FFFF]
|
||||
var vminor_alt : base.u32[..= 0x1FF_FFFF]
|
||||
var continued : base.u32[..= 1]
|
||||
var c : base.u8
|
||||
var c_major : base.u8[..= 0x07]
|
||||
var c_minor : base.u8[..= 0x1F]
|
||||
var tagged : base.bool
|
||||
var n64 : base.u64
|
||||
var depth : base.u32[..= 1024]
|
||||
var stack_byte : base.u32[..= (1024 / 16) - 1]
|
||||
var stack_bit : base.u32[..= 30]
|
||||
var stack_val : base.u32[..= 3]
|
||||
var token_length : base.u32[..= 0xFFFF]
|
||||
var vminor : base.u32[..= 0x1FF_FFFF]
|
||||
var vminor_alt : base.u32[..= 0x1FF_FFFF]
|
||||
var continued : base.u32[..= 1]
|
||||
var c : base.u8
|
||||
var c_major : base.u8[..= 0x07]
|
||||
var c_minor : base.u8[..= 0x1F]
|
||||
var tagged : base.bool
|
||||
|
||||
// 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]
|
||||
// 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"
|
||||
}
|
||||
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.length() <= 1 {
|
||||
yield? base."$short write"
|
||||
continue.outer
|
||||
}
|
||||
if args.src.length() <= 0 {
|
||||
if args.src.is_closed() {
|
||||
return "#bad input"
|
||||
}
|
||||
yield? base."$short read"
|
||||
continue.outer
|
||||
}
|
||||
c = args.src.peek_u8()
|
||||
while.outer true {
|
||||
while.goto_parsed_a_leaf_value true {{
|
||||
while.goto_fail true {{
|
||||
if args.dst.length() <= 1 {
|
||||
yield? base."$short write"
|
||||
continue.outer
|
||||
}
|
||||
if args.src.length() <= 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"
|
||||
}
|
||||
vminor = (base.TOKEN__VBC__STRING << 21) |
|
||||
base.TOKEN__VBD__STRING__CONVERT_0_DST_1_SRC_DROP
|
||||
if indefinite_string_major_type == 3 {
|
||||
vminor |= 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: vminor,
|
||||
continued: 0,
|
||||
length: 1)
|
||||
break.goto_parsed_a_leaf_value
|
||||
}
|
||||
if (indefinite_string_major_type <> 0) and (indefinite_string_major_type <> (c >> 5)) {
|
||||
if c <> 0xFF {
|
||||
return "#bad input"
|
||||
}
|
||||
vminor = (base.TOKEN__VBC__STRING << 21) |
|
||||
base.TOKEN__VBD__STRING__CONVERT_0_DST_1_SRC_DROP
|
||||
if indefinite_string_major_type == 3 {
|
||||
vminor |= 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: vminor,
|
||||
continued: 0,
|
||||
length: 1)
|
||||
break.goto_parsed_a_leaf_value
|
||||
}
|
||||
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
c_major = (c >> 5) as base.u8
|
||||
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.length() > 1,
|
||||
{{
|
||||
if c_minor == 0x18 {
|
||||
if args.src.length() >= 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.length() >= 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.length() >= 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.length() >= 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
|
||||
}
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
c_major = (c >> 5) as base.u8
|
||||
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.length() > 1,
|
||||
{{
|
||||
if c_minor == 0x18 {
|
||||
if args.src.length() >= 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.length() >= 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.length() >= 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.length() >= 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"
|
||||
continue.outer
|
||||
}
|
||||
return "#internal error: inconsistent I/O"
|
||||
}} endwhile.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"
|
||||
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 < 0x1A {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__INLINE_INTEGER_UNSIGNED << 21) |
|
||||
((string_length & 0xFFFF) as base.u32),
|
||||
continued: 0,
|
||||
length: TOKEN_LENGTHS[c_minor] as base.u32)
|
||||
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__INLINE_INTEGER_UNSIGNED << 21) |
|
||||
((string_length >> base.TOKEN__VALUE_EXTENSION__NUM_BITS) as base.u32),
|
||||
continued: 1,
|
||||
length: 0)
|
||||
args.dst.write_extended_token_fast!(
|
||||
value_extension: string_length & 0x3FFF_FFFF_FFFF,
|
||||
continued: 0,
|
||||
length: TOKEN_LENGTHS[c_minor] as base.u32)
|
||||
break.goto_parsed_a_leaf_value
|
||||
}
|
||||
// -------- END Major type 0: an unsigned integer.
|
||||
if c_major == 0 {
|
||||
// -------- BEGIN Major type 0: an unsigned integer.
|
||||
if c_minor < 0x1A {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__INLINE_INTEGER_UNSIGNED << 21) |
|
||||
((string_length & 0xFFFF) as base.u32),
|
||||
continued: 0,
|
||||
length: TOKEN_LENGTHS[c_minor] as base.u32)
|
||||
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__INLINE_INTEGER_UNSIGNED << 21) |
|
||||
((string_length >> base.TOKEN__VALUE_EXTENSION__NUM_BITS) as base.u32),
|
||||
continued: 1,
|
||||
length: 0)
|
||||
args.dst.write_extended_token_fast!(
|
||||
value_extension: string_length & 0x3FFF_FFFF_FFFF,
|
||||
continued: 0,
|
||||
length: TOKEN_LENGTHS[c_minor] as base.u32)
|
||||
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 < 0x1A {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__INLINE_INTEGER_SIGNED << 21) |
|
||||
(0x1F_FFFF - ((string_length & 0xFFFF) as base.u32)),
|
||||
continued: 0,
|
||||
length: TOKEN_LENGTHS[c_minor] as base.u32)
|
||||
break.goto_parsed_a_leaf_value
|
||||
} else if c_minor < 0x1C {
|
||||
if string_length < 0x8000_0000_0000_0000 {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__INLINE_INTEGER_SIGNED << 21) |
|
||||
(0x1F_FFFF - ((string_length >> base.TOKEN__VALUE_EXTENSION__NUM_BITS) as base.u32)),
|
||||
continued: 1,
|
||||
length: 0)
|
||||
args.dst.write_extended_token_fast!(
|
||||
value_extension: (0xFFFF_FFFF_FFFF_FFFF - string_length) & 0x3FFF_FFFF_FFFF,
|
||||
continued: 0,
|
||||
length: TOKEN_LENGTHS[c_minor] as base.u32)
|
||||
} else {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: TOKEN_VALUE_MAJOR,
|
||||
value_minor: TOKEN_VALUE_MINOR__MINUS_1_MINUS_X,
|
||||
continued: 0,
|
||||
length: 9)
|
||||
}
|
||||
break.goto_parsed_a_leaf_value
|
||||
}
|
||||
// -------- END Major type 1: a negative integer.
|
||||
} else if c_major == 1 {
|
||||
// -------- END Major type 1: a negative integer.
|
||||
if c_minor < 0x1A {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__INLINE_INTEGER_SIGNED << 21) |
|
||||
(0x1F_FFFF - ((string_length & 0xFFFF) as base.u32)),
|
||||
continued: 0,
|
||||
length: TOKEN_LENGTHS[c_minor] as base.u32)
|
||||
break.goto_parsed_a_leaf_value
|
||||
} else if c_minor < 0x1C {
|
||||
if string_length < 0x8000_0000_0000_0000 {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__INLINE_INTEGER_SIGNED << 21) |
|
||||
(0x1F_FFFF - ((string_length >> base.TOKEN__VALUE_EXTENSION__NUM_BITS) as base.u32)),
|
||||
continued: 1,
|
||||
length: 0)
|
||||
args.dst.write_extended_token_fast!(
|
||||
value_extension: (0xFFFF_FFFF_FFFF_FFFF - string_length) & 0x3FFF_FFFF_FFFF,
|
||||
continued: 0,
|
||||
length: TOKEN_LENGTHS[c_minor] as base.u32)
|
||||
} else {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: TOKEN_VALUE_MAJOR,
|
||||
value_minor: TOKEN_VALUE_MINOR__MINUS_1_MINUS_X,
|
||||
continued: 0,
|
||||
length: 9)
|
||||
}
|
||||
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 < 0x1C {
|
||||
if string_length == 0 {
|
||||
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: TOKEN_LENGTHS[c_minor] as base.u32)
|
||||
break.goto_parsed_a_leaf_value
|
||||
}
|
||||
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: TOKEN_LENGTHS[c_minor] as base.u32)
|
||||
} else if c_minor == 0x1F {
|
||||
if indefinite_string_major_type <> 0 {
|
||||
break.goto_fail
|
||||
}
|
||||
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
|
||||
}
|
||||
} else if c_major == 2 {
|
||||
// -------- BEGIN Major type 2: a byte string.
|
||||
if c_minor < 0x1C {
|
||||
if string_length == 0 {
|
||||
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: TOKEN_LENGTHS[c_minor] as base.u32)
|
||||
break.goto_parsed_a_leaf_value
|
||||
}
|
||||
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: TOKEN_LENGTHS[c_minor] as base.u32)
|
||||
} else if c_minor == 0x1F {
|
||||
if indefinite_string_major_type <> 0 {
|
||||
break.goto_fail
|
||||
}
|
||||
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.length() <= 0 {
|
||||
yield? base."$short write"
|
||||
continue
|
||||
}
|
||||
n64 = string_length.min(a: args.src.length())
|
||||
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.length() < (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.
|
||||
while true {
|
||||
if args.dst.length() <= 0 {
|
||||
yield? base."$short write"
|
||||
continue
|
||||
}
|
||||
n64 = string_length.min(a: args.src.length())
|
||||
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.length() < (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 < 0x1C {
|
||||
if string_length == 0 {
|
||||
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: TOKEN_LENGTHS[c_minor] as base.u32)
|
||||
break.goto_parsed_a_leaf_value
|
||||
}
|
||||
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: TOKEN_LENGTHS[c_minor] as base.u32)
|
||||
} else if c_minor == 0x1F {
|
||||
if indefinite_string_major_type <> 0 {
|
||||
break.goto_fail
|
||||
}
|
||||
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
|
||||
}
|
||||
} else if c_major == 3 {
|
||||
// -------- BEGIN Major type 3: a text string.
|
||||
if c_minor < 0x1C {
|
||||
if string_length == 0 {
|
||||
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: TOKEN_LENGTHS[c_minor] as base.u32)
|
||||
break.goto_parsed_a_leaf_value
|
||||
}
|
||||
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: TOKEN_LENGTHS[c_minor] as base.u32)
|
||||
} else if c_minor == 0x1F {
|
||||
if indefinite_string_major_type <> 0 {
|
||||
break.goto_fail
|
||||
}
|
||||
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.length() <= 0 {
|
||||
yield? base."$short write"
|
||||
continue
|
||||
}
|
||||
n64 = string_length.min(a: 0xFFFF)
|
||||
n64 = args.src.valid_utf_8_length(up_to: n64)
|
||||
token_length = (n64 & 0xFFFF) as base.u32
|
||||
if token_length <= 0 {
|
||||
// The longest UTF-8 code point is 4 bytes.
|
||||
if args.src.is_closed() or (args.src.length() >= 4) {
|
||||
return "#bad input"
|
||||
}
|
||||
yield? base."$short read"
|
||||
continue
|
||||
}
|
||||
if args.src.length() < (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__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.
|
||||
while true {
|
||||
if args.dst.length() <= 0 {
|
||||
yield? base."$short write"
|
||||
continue
|
||||
}
|
||||
n64 = string_length.min(a: 0xFFFF)
|
||||
n64 = args.src.valid_utf_8_length(up_to: n64)
|
||||
token_length = (n64 & 0xFFFF) as base.u32
|
||||
if token_length <= 0 {
|
||||
// The longest UTF-8 code point is 4 bytes.
|
||||
if args.src.is_closed() or (args.src.length() >= 4) {
|
||||
return "#bad input"
|
||||
}
|
||||
yield? base."$short read"
|
||||
continue
|
||||
}
|
||||
if args.src.length() < (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__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.
|
||||
|
||||
} else if c_major == 4 {
|
||||
// -------- BEGIN Major type 4: an array of data items.
|
||||
if TOKEN_LENGTHS[c_minor] == 0 {
|
||||
break.goto_fail
|
||||
} else if depth >= 1024 {
|
||||
token_length = TOKEN_LENGTHS[c_minor] as base.u32
|
||||
while (token_length > 0) and (args.src.can_undo_byte()) {
|
||||
args.src.undo_byte!()
|
||||
token_length -= 1
|
||||
} endwhile
|
||||
return "#unsupported recursion depth"
|
||||
}
|
||||
} else if c_major == 4 {
|
||||
// -------- BEGIN Major type 4: an array of data items.
|
||||
if TOKEN_LENGTHS[c_minor] == 0 {
|
||||
break.goto_fail
|
||||
} else if depth >= 1024 {
|
||||
token_length = TOKEN_LENGTHS[c_minor] as base.u32
|
||||
while (token_length > 0) and (args.src.can_undo_byte()) {
|
||||
args.src.undo_byte!()
|
||||
token_length -= 1
|
||||
} endwhile
|
||||
return "#unsupported recursion depth"
|
||||
}
|
||||
|
||||
vminor = (base.TOKEN__VBC__STRUCTURE << 21) |
|
||||
base.TOKEN__VBD__STRUCTURE__PUSH |
|
||||
base.TOKEN__VBD__STRUCTURE__FROM_NONE |
|
||||
base.TOKEN__VBD__STRUCTURE__TO_LIST
|
||||
vminor_alt = (base.TOKEN__VBC__STRUCTURE << 21) |
|
||||
base.TOKEN__VBD__STRUCTURE__POP |
|
||||
base.TOKEN__VBD__STRUCTURE__FROM_LIST |
|
||||
base.TOKEN__VBD__STRUCTURE__TO_NONE
|
||||
if depth > 0 {
|
||||
stack_byte = (depth - 1) / 16
|
||||
stack_bit = ((depth - 1) & 15) * 2
|
||||
if 0 == (this.stack[stack_byte] & ((1 as base.u32) << stack_bit)) {
|
||||
vminor = (base.TOKEN__VBC__STRUCTURE << 21) |
|
||||
base.TOKEN__VBD__STRUCTURE__PUSH |
|
||||
base.TOKEN__VBD__STRUCTURE__FROM_LIST |
|
||||
base.TOKEN__VBD__STRUCTURE__TO_LIST
|
||||
vminor_alt = (base.TOKEN__VBC__STRUCTURE << 21) |
|
||||
base.TOKEN__VBD__STRUCTURE__POP |
|
||||
base.TOKEN__VBD__STRUCTURE__FROM_LIST |
|
||||
base.TOKEN__VBD__STRUCTURE__TO_LIST
|
||||
} else {
|
||||
vminor = (base.TOKEN__VBC__STRUCTURE << 21) |
|
||||
base.TOKEN__VBD__STRUCTURE__PUSH |
|
||||
base.TOKEN__VBD__STRUCTURE__FROM_DICT |
|
||||
base.TOKEN__VBD__STRUCTURE__TO_LIST
|
||||
vminor_alt = (base.TOKEN__VBC__STRUCTURE << 21) |
|
||||
base.TOKEN__VBD__STRUCTURE__POP |
|
||||
base.TOKEN__VBD__STRUCTURE__FROM_LIST |
|
||||
base.TOKEN__VBD__STRUCTURE__TO_DICT
|
||||
}
|
||||
}
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: vminor,
|
||||
continued: 0,
|
||||
length: TOKEN_LENGTHS[c_minor] as base.u32)
|
||||
if c_minor == 0x00 {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: vminor_alt,
|
||||
continued: 0,
|
||||
length: 0)
|
||||
break.goto_parsed_a_leaf_value
|
||||
}
|
||||
vminor = (base.TOKEN__VBC__STRUCTURE << 21) |
|
||||
base.TOKEN__VBD__STRUCTURE__PUSH |
|
||||
base.TOKEN__VBD__STRUCTURE__FROM_NONE |
|
||||
base.TOKEN__VBD__STRUCTURE__TO_LIST
|
||||
vminor_alt = (base.TOKEN__VBC__STRUCTURE << 21) |
|
||||
base.TOKEN__VBD__STRUCTURE__POP |
|
||||
base.TOKEN__VBD__STRUCTURE__FROM_LIST |
|
||||
base.TOKEN__VBD__STRUCTURE__TO_NONE
|
||||
if depth > 0 {
|
||||
stack_byte = (depth - 1) / 16
|
||||
stack_bit = ((depth - 1) & 15) * 2
|
||||
if 0 == (this.stack[stack_byte] & ((1 as base.u32) << stack_bit)) {
|
||||
vminor = (base.TOKEN__VBC__STRUCTURE << 21) |
|
||||
base.TOKEN__VBD__STRUCTURE__PUSH |
|
||||
base.TOKEN__VBD__STRUCTURE__FROM_LIST |
|
||||
base.TOKEN__VBD__STRUCTURE__TO_LIST
|
||||
vminor_alt = (base.TOKEN__VBC__STRUCTURE << 21) |
|
||||
base.TOKEN__VBD__STRUCTURE__POP |
|
||||
base.TOKEN__VBD__STRUCTURE__FROM_LIST |
|
||||
base.TOKEN__VBD__STRUCTURE__TO_LIST
|
||||
} else {
|
||||
vminor = (base.TOKEN__VBC__STRUCTURE << 21) |
|
||||
base.TOKEN__VBD__STRUCTURE__PUSH |
|
||||
base.TOKEN__VBD__STRUCTURE__FROM_DICT |
|
||||
base.TOKEN__VBD__STRUCTURE__TO_LIST
|
||||
vminor_alt = (base.TOKEN__VBC__STRUCTURE << 21) |
|
||||
base.TOKEN__VBD__STRUCTURE__POP |
|
||||
base.TOKEN__VBD__STRUCTURE__FROM_LIST |
|
||||
base.TOKEN__VBD__STRUCTURE__TO_DICT
|
||||
}
|
||||
}
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: vminor,
|
||||
continued: 0,
|
||||
length: TOKEN_LENGTHS[c_minor] as base.u32)
|
||||
if c_minor == 0x00 {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: vminor_alt,
|
||||
continued: 0,
|
||||
length: 0)
|
||||
break.goto_parsed_a_leaf_value
|
||||
}
|
||||
|
||||
stack_byte = depth / 16
|
||||
stack_bit = (depth & 15) * 2
|
||||
this.stack[stack_byte] &= 0xFFFF_FFFF ^ ((3 as base.u32) << stack_bit)
|
||||
this.container_num_remaining[depth] = string_length
|
||||
depth += 1
|
||||
tagged = false
|
||||
continue.outer
|
||||
// -------- END Major type 4: an array of data items.
|
||||
stack_byte = depth / 16
|
||||
stack_bit = (depth & 15) * 2
|
||||
this.stack[stack_byte] &= 0xFFFF_FFFF ^ ((3 as base.u32) << stack_bit)
|
||||
this.container_num_remaining[depth] = string_length
|
||||
depth += 1
|
||||
tagged = false
|
||||
continue.outer
|
||||
// -------- END Major type 4: an array of data items.
|
||||
|
||||
} else if c_major == 5 {
|
||||
// -------- BEGIN Major type 5: a map of pairs of data items.
|
||||
if TOKEN_LENGTHS[c_minor] == 0 {
|
||||
break.goto_fail
|
||||
} else if depth >= 1024 {
|
||||
token_length = TOKEN_LENGTHS[c_minor] as base.u32
|
||||
while (token_length > 0) and (args.src.can_undo_byte()) {
|
||||
args.src.undo_byte!()
|
||||
token_length -= 1
|
||||
} endwhile
|
||||
return "#unsupported recursion depth"
|
||||
}
|
||||
} else if c_major == 5 {
|
||||
// -------- BEGIN Major type 5: a map of pairs of data items.
|
||||
if TOKEN_LENGTHS[c_minor] == 0 {
|
||||
break.goto_fail
|
||||
} else if depth >= 1024 {
|
||||
token_length = TOKEN_LENGTHS[c_minor] as base.u32
|
||||
while (token_length > 0) and (args.src.can_undo_byte()) {
|
||||
args.src.undo_byte!()
|
||||
token_length -= 1
|
||||
} endwhile
|
||||
return "#unsupported recursion depth"
|
||||
}
|
||||
|
||||
vminor = (base.TOKEN__VBC__STRUCTURE << 21) |
|
||||
base.TOKEN__VBD__STRUCTURE__PUSH |
|
||||
base.TOKEN__VBD__STRUCTURE__FROM_NONE |
|
||||
base.TOKEN__VBD__STRUCTURE__TO_DICT
|
||||
vminor_alt = (base.TOKEN__VBC__STRUCTURE << 21) |
|
||||
base.TOKEN__VBD__STRUCTURE__POP |
|
||||
base.TOKEN__VBD__STRUCTURE__FROM_DICT |
|
||||
base.TOKEN__VBD__STRUCTURE__TO_NONE
|
||||
if depth > 0 {
|
||||
stack_byte = (depth - 1) / 16
|
||||
stack_bit = ((depth - 1) & 15) * 2
|
||||
if 0 == (this.stack[stack_byte] & ((1 as base.u32) << stack_bit)) {
|
||||
vminor = (base.TOKEN__VBC__STRUCTURE << 21) |
|
||||
base.TOKEN__VBD__STRUCTURE__PUSH |
|
||||
base.TOKEN__VBD__STRUCTURE__FROM_LIST |
|
||||
base.TOKEN__VBD__STRUCTURE__TO_DICT
|
||||
vminor_alt = (base.TOKEN__VBC__STRUCTURE << 21) |
|
||||
base.TOKEN__VBD__STRUCTURE__POP |
|
||||
base.TOKEN__VBD__STRUCTURE__FROM_DICT |
|
||||
base.TOKEN__VBD__STRUCTURE__TO_LIST
|
||||
} else {
|
||||
vminor = (base.TOKEN__VBC__STRUCTURE << 21) |
|
||||
base.TOKEN__VBD__STRUCTURE__PUSH |
|
||||
base.TOKEN__VBD__STRUCTURE__FROM_DICT |
|
||||
base.TOKEN__VBD__STRUCTURE__TO_DICT
|
||||
vminor_alt = (base.TOKEN__VBC__STRUCTURE << 21) |
|
||||
base.TOKEN__VBD__STRUCTURE__POP |
|
||||
base.TOKEN__VBD__STRUCTURE__FROM_DICT |
|
||||
base.TOKEN__VBD__STRUCTURE__TO_DICT
|
||||
}
|
||||
}
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: vminor,
|
||||
continued: 0,
|
||||
length: TOKEN_LENGTHS[c_minor] as base.u32)
|
||||
if c_minor == 0x00 {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: vminor_alt,
|
||||
continued: 0,
|
||||
length: 0)
|
||||
break.goto_parsed_a_leaf_value
|
||||
}
|
||||
vminor = (base.TOKEN__VBC__STRUCTURE << 21) |
|
||||
base.TOKEN__VBD__STRUCTURE__PUSH |
|
||||
base.TOKEN__VBD__STRUCTURE__FROM_NONE |
|
||||
base.TOKEN__VBD__STRUCTURE__TO_DICT
|
||||
vminor_alt = (base.TOKEN__VBC__STRUCTURE << 21) |
|
||||
base.TOKEN__VBD__STRUCTURE__POP |
|
||||
base.TOKEN__VBD__STRUCTURE__FROM_DICT |
|
||||
base.TOKEN__VBD__STRUCTURE__TO_NONE
|
||||
if depth > 0 {
|
||||
stack_byte = (depth - 1) / 16
|
||||
stack_bit = ((depth - 1) & 15) * 2
|
||||
if 0 == (this.stack[stack_byte] & ((1 as base.u32) << stack_bit)) {
|
||||
vminor = (base.TOKEN__VBC__STRUCTURE << 21) |
|
||||
base.TOKEN__VBD__STRUCTURE__PUSH |
|
||||
base.TOKEN__VBD__STRUCTURE__FROM_LIST |
|
||||
base.TOKEN__VBD__STRUCTURE__TO_DICT
|
||||
vminor_alt = (base.TOKEN__VBC__STRUCTURE << 21) |
|
||||
base.TOKEN__VBD__STRUCTURE__POP |
|
||||
base.TOKEN__VBD__STRUCTURE__FROM_DICT |
|
||||
base.TOKEN__VBD__STRUCTURE__TO_LIST
|
||||
} else {
|
||||
vminor = (base.TOKEN__VBC__STRUCTURE << 21) |
|
||||
base.TOKEN__VBD__STRUCTURE__PUSH |
|
||||
base.TOKEN__VBD__STRUCTURE__FROM_DICT |
|
||||
base.TOKEN__VBD__STRUCTURE__TO_DICT
|
||||
vminor_alt = (base.TOKEN__VBC__STRUCTURE << 21) |
|
||||
base.TOKEN__VBD__STRUCTURE__POP |
|
||||
base.TOKEN__VBD__STRUCTURE__FROM_DICT |
|
||||
base.TOKEN__VBD__STRUCTURE__TO_DICT
|
||||
}
|
||||
}
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: vminor,
|
||||
continued: 0,
|
||||
length: TOKEN_LENGTHS[c_minor] as base.u32)
|
||||
if c_minor == 0x00 {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: vminor_alt,
|
||||
continued: 0,
|
||||
length: 0)
|
||||
break.goto_parsed_a_leaf_value
|
||||
}
|
||||
|
||||
stack_byte = depth / 16
|
||||
stack_bit = (depth & 15) * 2
|
||||
this.stack[stack_byte] |= (3 as base.u32) << stack_bit
|
||||
this.container_num_remaining[depth] = string_length
|
||||
depth += 1
|
||||
tagged = false
|
||||
continue.outer
|
||||
// -------- END Major type 5: a map of pairs of data items.
|
||||
stack_byte = depth / 16
|
||||
stack_bit = (depth & 15) * 2
|
||||
this.stack[stack_byte] |= (3 as base.u32) << stack_bit
|
||||
this.container_num_remaining[depth] = string_length
|
||||
depth += 1
|
||||
tagged = false
|
||||
continue.outer
|
||||
// -------- END Major type 5: a map of pairs of data items.
|
||||
|
||||
} else if c_major == 6 {
|
||||
// -------- BEGIN Major type 6: tags.
|
||||
if c_minor >= 0x1C {
|
||||
break.goto_fail
|
||||
}
|
||||
// Write one token (18 bits) or two tokens (18 + 46 bits).
|
||||
if string_length < 0x4_0000 {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: TOKEN_VALUE_MAJOR,
|
||||
value_minor: TOKEN_VALUE_MINOR__TAG |
|
||||
(string_length as base.u32),
|
||||
continued: 0,
|
||||
length: TOKEN_LENGTHS[c_minor] as base.u32)
|
||||
} else {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: TOKEN_VALUE_MAJOR,
|
||||
value_minor: TOKEN_VALUE_MINOR__TAG |
|
||||
((string_length >> base.TOKEN__VALUE_EXTENSION__NUM_BITS) as base.u32),
|
||||
continued: 1,
|
||||
length: 0)
|
||||
args.dst.write_extended_token_fast!(
|
||||
value_extension: string_length & 0x3FFF_FFFF_FFFF,
|
||||
continued: 0,
|
||||
length: TOKEN_LENGTHS[c_minor] as base.u32)
|
||||
}
|
||||
tagged = true
|
||||
continue.outer
|
||||
// -------- END Major type 6: tags.
|
||||
} else if c_major == 6 {
|
||||
// -------- BEGIN Major type 6: tags.
|
||||
if c_minor >= 0x1C {
|
||||
break.goto_fail
|
||||
}
|
||||
// Write one token (18 bits) or two tokens (18 + 46 bits).
|
||||
if string_length < 0x4_0000 {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: TOKEN_VALUE_MAJOR,
|
||||
value_minor: TOKEN_VALUE_MINOR__TAG |
|
||||
(string_length as base.u32),
|
||||
continued: 0,
|
||||
length: TOKEN_LENGTHS[c_minor] as base.u32)
|
||||
} else {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: TOKEN_VALUE_MAJOR,
|
||||
value_minor: TOKEN_VALUE_MINOR__TAG |
|
||||
((string_length >> base.TOKEN__VALUE_EXTENSION__NUM_BITS) as base.u32),
|
||||
continued: 1,
|
||||
length: 0)
|
||||
args.dst.write_extended_token_fast!(
|
||||
value_extension: string_length & 0x3FFF_FFFF_FFFF,
|
||||
continued: 0,
|
||||
length: TOKEN_LENGTHS[c_minor] as base.u32)
|
||||
}
|
||||
tagged = true
|
||||
continue.outer
|
||||
// -------- END Major type 6: tags.
|
||||
|
||||
} else if c_major == 7 {
|
||||
// -------- BEGIN Major type 7: miscellaneous.
|
||||
if c_minor < 0x14 {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: TOKEN_VALUE_MAJOR,
|
||||
value_minor: TOKEN_VALUE_MINOR__SIMPLE_VALUE |
|
||||
((string_length & 0xFF) as base.u32),
|
||||
continued: 0,
|
||||
length: 1)
|
||||
break.goto_parsed_a_leaf_value
|
||||
} else if c_major == 7 {
|
||||
// -------- BEGIN Major type 7: miscellaneous.
|
||||
if c_minor < 0x14 {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: TOKEN_VALUE_MAJOR,
|
||||
value_minor: TOKEN_VALUE_MINOR__SIMPLE_VALUE |
|
||||
((string_length & 0xFF) as base.u32),
|
||||
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: LITERALS[c_minor & 3],
|
||||
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: LITERALS[c_minor & 3],
|
||||
continued: 0,
|
||||
length: 1)
|
||||
break.goto_parsed_a_leaf_value
|
||||
|
||||
} else if c_minor == 0x18 {
|
||||
if string_length < 0x18 {
|
||||
if not args.src.can_undo_byte() {
|
||||
return "#internal error: inconsistent I/O"
|
||||
}
|
||||
args.src.undo_byte!()
|
||||
break.goto_fail
|
||||
}
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: TOKEN_VALUE_MAJOR,
|
||||
value_minor: TOKEN_VALUE_MINOR__SIMPLE_VALUE |
|
||||
((string_length & 0xFF) as base.u32),
|
||||
continued: 0,
|
||||
length: 2)
|
||||
break.goto_parsed_a_leaf_value
|
||||
} else if c_minor == 0x18 {
|
||||
if string_length < 0x18 {
|
||||
if not args.src.can_undo_byte() {
|
||||
return "#internal error: inconsistent I/O"
|
||||
}
|
||||
args.src.undo_byte!()
|
||||
break.goto_fail
|
||||
}
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: TOKEN_VALUE_MAJOR,
|
||||
value_minor: TOKEN_VALUE_MINOR__SIMPLE_VALUE |
|
||||
((string_length & 0xFF) as base.u32),
|
||||
continued: 0,
|
||||
length: 2)
|
||||
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_FLOATING_POINT |
|
||||
base.TOKEN__VBD__NUMBER__FORMAT_BINARY_BIG_ENDIAN |
|
||||
base.TOKEN__VBD__NUMBER__FORMAT_IGNORE_FIRST_BYTE,
|
||||
continued: 0,
|
||||
length: TOKEN_LENGTHS[c_minor] as base.u32)
|
||||
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_FLOATING_POINT |
|
||||
base.TOKEN__VBD__NUMBER__FORMAT_BINARY_BIG_ENDIAN |
|
||||
base.TOKEN__VBD__NUMBER__FORMAT_IGNORE_FIRST_BYTE,
|
||||
continued: 0,
|
||||
length: TOKEN_LENGTHS[c_minor] as base.u32)
|
||||
break.goto_parsed_a_leaf_value
|
||||
|
||||
} else if c_minor == 0x1F {
|
||||
// Indefinite-length strings were dealt with separately, above.
|
||||
// Here, we expect to be in an indefinite-length container.
|
||||
if tagged or (depth <= 0) {
|
||||
break.goto_fail
|
||||
}
|
||||
depth -= 1
|
||||
if this.container_num_remaining[depth] <> 0 {
|
||||
break.goto_fail
|
||||
}
|
||||
} else if c_minor == 0x1F {
|
||||
// Indefinite-length strings were dealt with separately, above.
|
||||
// Here, we expect to be in an indefinite-length container.
|
||||
if tagged or (depth <= 0) {
|
||||
break.goto_fail
|
||||
}
|
||||
depth -= 1
|
||||
if this.container_num_remaining[depth] <> 0 {
|
||||
break.goto_fail
|
||||
}
|
||||
|
||||
// Check that, if we're in a map, the map contains an even
|
||||
// number of elements.
|
||||
stack_byte = depth / 16
|
||||
stack_bit = (depth & 15) * 2
|
||||
stack_val = 3 & (this.stack[stack_byte] >> stack_bit)
|
||||
if stack_val == 1 {
|
||||
break.goto_fail
|
||||
}
|
||||
// Check that, if we're in a map, the map contains an even
|
||||
// number of elements.
|
||||
stack_byte = depth / 16
|
||||
stack_bit = (depth & 15) * 2
|
||||
stack_val = 3 & (this.stack[stack_byte] >> stack_bit)
|
||||
if stack_val == 1 {
|
||||
break.goto_fail
|
||||
}
|
||||
|
||||
// Fill in FROM_ETC.
|
||||
if stack_val <> 3 {
|
||||
vminor_alt = (base.TOKEN__VBC__STRUCTURE << 21) |
|
||||
base.TOKEN__VBD__STRUCTURE__POP |
|
||||
base.TOKEN__VBD__STRUCTURE__FROM_LIST
|
||||
} else {
|
||||
vminor_alt = (base.TOKEN__VBC__STRUCTURE << 21) |
|
||||
base.TOKEN__VBD__STRUCTURE__POP |
|
||||
base.TOKEN__VBD__STRUCTURE__FROM_DICT
|
||||
}
|
||||
// Fill in FROM_ETC.
|
||||
if stack_val <> 3 {
|
||||
vminor_alt = (base.TOKEN__VBC__STRUCTURE << 21) |
|
||||
base.TOKEN__VBD__STRUCTURE__POP |
|
||||
base.TOKEN__VBD__STRUCTURE__FROM_LIST
|
||||
} else {
|
||||
vminor_alt = (base.TOKEN__VBC__STRUCTURE << 21) |
|
||||
base.TOKEN__VBD__STRUCTURE__POP |
|
||||
base.TOKEN__VBD__STRUCTURE__FROM_DICT
|
||||
}
|
||||
|
||||
// Fill in TO_ETC.
|
||||
if depth <= 0 {
|
||||
vminor_alt |= base.TOKEN__VBD__STRUCTURE__TO_NONE
|
||||
} else {
|
||||
stack_byte = (depth - 1) / 16
|
||||
stack_bit = ((depth - 1) & 15) * 2
|
||||
if 0 == (this.stack[stack_byte] & ((1 as base.u32) << stack_bit)) {
|
||||
vminor_alt |= base.TOKEN__VBD__STRUCTURE__TO_LIST
|
||||
} else {
|
||||
vminor_alt |= base.TOKEN__VBD__STRUCTURE__TO_DICT
|
||||
}
|
||||
}
|
||||
// Fill in TO_ETC.
|
||||
if depth <= 0 {
|
||||
vminor_alt |= base.TOKEN__VBD__STRUCTURE__TO_NONE
|
||||
} else {
|
||||
stack_byte = (depth - 1) / 16
|
||||
stack_bit = ((depth - 1) & 15) * 2
|
||||
if 0 == (this.stack[stack_byte] & ((1 as base.u32) << stack_bit)) {
|
||||
vminor_alt |= base.TOKEN__VBD__STRUCTURE__TO_LIST
|
||||
} else {
|
||||
vminor_alt |= base.TOKEN__VBD__STRUCTURE__TO_DICT
|
||||
}
|
||||
}
|
||||
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: vminor_alt,
|
||||
continued: 0,
|
||||
length: 1)
|
||||
break.goto_parsed_a_leaf_value
|
||||
}
|
||||
// -------- END Major type 7: miscellaneous.
|
||||
}
|
||||
break.goto_fail
|
||||
}} endwhile.goto_fail
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: vminor_alt,
|
||||
continued: 0,
|
||||
length: 1)
|
||||
break.goto_parsed_a_leaf_value
|
||||
}
|
||||
// -------- END Major type 7: miscellaneous.
|
||||
}
|
||||
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
|
||||
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, or the (explicit or
|
||||
// implicit) close of a container (array or object).
|
||||
tagged = false
|
||||
while depth > 0 {
|
||||
// Toggle the key/value bit for object containers. This bit is
|
||||
// ignored for array containers.
|
||||
stack_byte = (depth - 1) / 16
|
||||
stack_bit = ((depth - 1) & 15) * 2
|
||||
this.stack[stack_byte] ^= (1 as base.u32) << (stack_bit + 1)
|
||||
if 1 == (3 & (this.stack[stack_byte] >> stack_bit)) {
|
||||
// We just parsed the key of a key-value pair.
|
||||
continue.outer
|
||||
}
|
||||
// We've just parsed a leaf (non-container) value, or the (explicit or
|
||||
// implicit) close of a container (array or object).
|
||||
tagged = false
|
||||
while depth > 0 {
|
||||
// Toggle the key/value bit for object containers. This bit is
|
||||
// ignored for array containers.
|
||||
stack_byte = (depth - 1) / 16
|
||||
stack_bit = ((depth - 1) & 15) * 2
|
||||
this.stack[stack_byte] ^= (1 as base.u32) << (stack_bit + 1)
|
||||
if 1 == (3 & (this.stack[stack_byte] >> stack_bit)) {
|
||||
// We just parsed the key of a key-value pair.
|
||||
continue.outer
|
||||
}
|
||||
|
||||
if this.container_num_remaining[depth - 1] <= 0 {
|
||||
// We're in an indefinite-length container.
|
||||
continue.outer
|
||||
}
|
||||
this.container_num_remaining[depth - 1] -= 1
|
||||
if this.container_num_remaining[depth - 1] > 0 {
|
||||
// We're in a definite-length, non-empty container and have not
|
||||
// seen its final value.
|
||||
continue.outer
|
||||
}
|
||||
if this.container_num_remaining[depth - 1] <= 0 {
|
||||
// We're in an indefinite-length container.
|
||||
continue.outer
|
||||
}
|
||||
this.container_num_remaining[depth - 1] -= 1
|
||||
if this.container_num_remaining[depth - 1] > 0 {
|
||||
// We're in a definite-length, non-empty container and have not
|
||||
// seen its final value.
|
||||
continue.outer
|
||||
}
|
||||
|
||||
while args.dst.length() <= 0,
|
||||
inv depth > 0,
|
||||
post args.dst.length() > 0,
|
||||
{
|
||||
yield? base."$short write"
|
||||
continue
|
||||
} endwhile
|
||||
depth -= 1
|
||||
while args.dst.length() <= 0,
|
||||
inv depth > 0,
|
||||
post args.dst.length() > 0,
|
||||
{
|
||||
yield? base."$short write"
|
||||
continue
|
||||
} endwhile
|
||||
depth -= 1
|
||||
|
||||
// Fill in FROM_ETC.
|
||||
stack_byte = depth / 16
|
||||
stack_bit = (depth & 15) * 2
|
||||
if 0 == (this.stack[stack_byte] & ((1 as base.u32) << stack_bit)) {
|
||||
vminor_alt = (base.TOKEN__VBC__STRUCTURE << 21) |
|
||||
base.TOKEN__VBD__STRUCTURE__POP |
|
||||
base.TOKEN__VBD__STRUCTURE__FROM_LIST
|
||||
} else {
|
||||
vminor_alt = (base.TOKEN__VBC__STRUCTURE << 21) |
|
||||
base.TOKEN__VBD__STRUCTURE__POP |
|
||||
base.TOKEN__VBD__STRUCTURE__FROM_DICT
|
||||
}
|
||||
// Fill in FROM_ETC.
|
||||
stack_byte = depth / 16
|
||||
stack_bit = (depth & 15) * 2
|
||||
if 0 == (this.stack[stack_byte] & ((1 as base.u32) << stack_bit)) {
|
||||
vminor_alt = (base.TOKEN__VBC__STRUCTURE << 21) |
|
||||
base.TOKEN__VBD__STRUCTURE__POP |
|
||||
base.TOKEN__VBD__STRUCTURE__FROM_LIST
|
||||
} else {
|
||||
vminor_alt = (base.TOKEN__VBC__STRUCTURE << 21) |
|
||||
base.TOKEN__VBD__STRUCTURE__POP |
|
||||
base.TOKEN__VBD__STRUCTURE__FROM_DICT
|
||||
}
|
||||
|
||||
// Fill in TO_ETC.
|
||||
if depth <= 0 {
|
||||
vminor_alt |= base.TOKEN__VBD__STRUCTURE__TO_NONE
|
||||
} else {
|
||||
stack_byte = (depth - 1) / 16
|
||||
stack_bit = ((depth - 1) & 15) * 2
|
||||
if 0 == (this.stack[stack_byte] & ((1 as base.u32) << stack_bit)) {
|
||||
vminor_alt |= base.TOKEN__VBD__STRUCTURE__TO_LIST
|
||||
} else {
|
||||
vminor_alt |= base.TOKEN__VBD__STRUCTURE__TO_DICT
|
||||
}
|
||||
}
|
||||
// Fill in TO_ETC.
|
||||
if depth <= 0 {
|
||||
vminor_alt |= base.TOKEN__VBD__STRUCTURE__TO_NONE
|
||||
} else {
|
||||
stack_byte = (depth - 1) / 16
|
||||
stack_bit = ((depth - 1) & 15) * 2
|
||||
if 0 == (this.stack[stack_byte] & ((1 as base.u32) << stack_bit)) {
|
||||
vminor_alt |= base.TOKEN__VBD__STRUCTURE__TO_LIST
|
||||
} else {
|
||||
vminor_alt |= base.TOKEN__VBD__STRUCTURE__TO_DICT
|
||||
}
|
||||
}
|
||||
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: vminor_alt,
|
||||
continued: 0,
|
||||
length: 0)
|
||||
} endwhile
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: vminor_alt,
|
||||
continued: 0,
|
||||
length: 0)
|
||||
} endwhile
|
||||
|
||||
// We've parsed the top-level value and we're therefore done overall.
|
||||
break.outer
|
||||
} endwhile.outer
|
||||
// We've parsed the top-level value and we're therefore done overall.
|
||||
break.outer
|
||||
} endwhile.outer
|
||||
|
||||
this.end_of_data = true
|
||||
this.end_of_data = true
|
||||
}
|
||||
+552
-552
@@ -14,7 +14,7 @@
|
||||
|
||||
// TODO: drop the '?' but still generate wuffs_crc32__ieee_hasher__initialize?
|
||||
pub struct ieee_hasher? implements base.hasher_u32(
|
||||
state : base.u32,
|
||||
state : base.u32,
|
||||
)
|
||||
|
||||
pub func ieee_hasher.set_quirk_enabled!(quirk: base.u32, enabled: base.bool) {
|
||||
@@ -24,582 +24,582 @@ pub func ieee_hasher.set_quirk_enabled!(quirk: base.u32, enabled: base.bool) {
|
||||
// to signal "initializer not called"? Should the return type, in the generated
|
||||
// C code, be "struct{ uint32_t checksum; wuffs_crc32__status status }"?
|
||||
pub func ieee_hasher.update_u32!(x: slice base.u8) base.u32 {
|
||||
if this.state == 0 {
|
||||
choose up = [
|
||||
up_arm_crc32,
|
||||
up_x86_avx2,
|
||||
up_x86_sse42]
|
||||
}
|
||||
this.up!(x: args.x)
|
||||
return this.state
|
||||
if this.state == 0 {
|
||||
choose up = [
|
||||
up_arm_crc32,
|
||||
up_x86_avx2,
|
||||
up_x86_sse42]
|
||||
}
|
||||
this.up!(x: args.x)
|
||||
return this.state
|
||||
}
|
||||
|
||||
pri func ieee_hasher.up!(x: slice base.u8),
|
||||
choosy,
|
||||
choosy,
|
||||
{
|
||||
var s : base.u32
|
||||
var p : slice base.u8
|
||||
var s : base.u32
|
||||
var p : slice base.u8
|
||||
|
||||
s = 0xFFFF_FFFF ^ this.state
|
||||
s = 0xFFFF_FFFF ^ this.state
|
||||
|
||||
// See "Multi-Byte Lookup Tables" in std/crc32/README.md for more detail on
|
||||
// the slicing-by-M algorithm. We use an M of 16.
|
||||
iterate (p = args.x)(length: 16, advance: 16, unroll: 2) {
|
||||
s ^= ((p[0x00] as base.u32) << 0) |
|
||||
((p[0x01] as base.u32) << 8) |
|
||||
((p[0x02] as base.u32) << 16) |
|
||||
((p[0x03] as base.u32) << 24)
|
||||
s = IEEE_TABLE[0x00][p[0x0F]] ^
|
||||
IEEE_TABLE[0x01][p[0x0E]] ^
|
||||
IEEE_TABLE[0x02][p[0x0D]] ^
|
||||
IEEE_TABLE[0x03][p[0x0C]] ^
|
||||
IEEE_TABLE[0x04][p[0x0B]] ^
|
||||
IEEE_TABLE[0x05][p[0x0A]] ^
|
||||
IEEE_TABLE[0x06][p[0x09]] ^
|
||||
IEEE_TABLE[0x07][p[0x08]] ^
|
||||
IEEE_TABLE[0x08][p[0x07]] ^
|
||||
IEEE_TABLE[0x09][p[0x06]] ^
|
||||
IEEE_TABLE[0x0A][p[0x05]] ^
|
||||
IEEE_TABLE[0x0B][p[0x04]] ^
|
||||
IEEE_TABLE[0x0C][0xFF & (s >> 24)] ^
|
||||
IEEE_TABLE[0x0D][0xFF & (s >> 16)] ^
|
||||
IEEE_TABLE[0x0E][0xFF & (s >> 8)] ^
|
||||
IEEE_TABLE[0x0F][0xFF & (s >> 0)]
|
||||
} else (length: 1, advance: 1, unroll: 1) {
|
||||
s = IEEE_TABLE[0][((s & 0xFF) as base.u8) ^ p[0]] ^ (s >> 8)
|
||||
}
|
||||
// See "Multi-Byte Lookup Tables" in std/crc32/README.md for more detail on
|
||||
// the slicing-by-M algorithm. We use an M of 16.
|
||||
iterate (p = args.x)(length: 16, advance: 16, unroll: 2) {
|
||||
s ^= ((p[0x00] as base.u32) << 0) |
|
||||
((p[0x01] as base.u32) << 8) |
|
||||
((p[0x02] as base.u32) << 16) |
|
||||
((p[0x03] as base.u32) << 24)
|
||||
s = IEEE_TABLE[0x00][p[0x0F]] ^
|
||||
IEEE_TABLE[0x01][p[0x0E]] ^
|
||||
IEEE_TABLE[0x02][p[0x0D]] ^
|
||||
IEEE_TABLE[0x03][p[0x0C]] ^
|
||||
IEEE_TABLE[0x04][p[0x0B]] ^
|
||||
IEEE_TABLE[0x05][p[0x0A]] ^
|
||||
IEEE_TABLE[0x06][p[0x09]] ^
|
||||
IEEE_TABLE[0x07][p[0x08]] ^
|
||||
IEEE_TABLE[0x08][p[0x07]] ^
|
||||
IEEE_TABLE[0x09][p[0x06]] ^
|
||||
IEEE_TABLE[0x0A][p[0x05]] ^
|
||||
IEEE_TABLE[0x0B][p[0x04]] ^
|
||||
IEEE_TABLE[0x0C][0xFF & (s >> 24)] ^
|
||||
IEEE_TABLE[0x0D][0xFF & (s >> 16)] ^
|
||||
IEEE_TABLE[0x0E][0xFF & (s >> 8)] ^
|
||||
IEEE_TABLE[0x0F][0xFF & (s >> 0)]
|
||||
} else (length: 1, advance: 1, unroll: 1) {
|
||||
s = IEEE_TABLE[0][((s & 0xFF) as base.u8) ^ p[0]] ^ (s >> 8)
|
||||
}
|
||||
|
||||
this.state = 0xFFFF_FFFF ^ s
|
||||
this.state = 0xFFFF_FFFF ^ s
|
||||
}
|
||||
|
||||
// The table below was created by script/print-crc32-magic-numbers.go.
|
||||
|
||||
pri const IEEE_TABLE : array[16] array[256] base.u32 = [[
|
||||
0x0000_0000, 0x7707_3096, 0xEE0E_612C, 0x9909_51BA, 0x076D_C419, 0x706A_F48F, 0xE963_A535, 0x9E64_95A3,
|
||||
0x0EDB_8832, 0x79DC_B8A4, 0xE0D5_E91E, 0x97D2_D988, 0x09B6_4C2B, 0x7EB1_7CBD, 0xE7B8_2D07, 0x90BF_1D91,
|
||||
0x1DB7_1064, 0x6AB0_20F2, 0xF3B9_7148, 0x84BE_41DE, 0x1ADA_D47D, 0x6DDD_E4EB, 0xF4D4_B551, 0x83D3_85C7,
|
||||
0x136C_9856, 0x646B_A8C0, 0xFD62_F97A, 0x8A65_C9EC, 0x1401_5C4F, 0x6306_6CD9, 0xFA0F_3D63, 0x8D08_0DF5,
|
||||
0x3B6E_20C8, 0x4C69_105E, 0xD560_41E4, 0xA267_7172, 0x3C03_E4D1, 0x4B04_D447, 0xD20D_85FD, 0xA50A_B56B,
|
||||
0x35B5_A8FA, 0x42B2_986C, 0xDBBB_C9D6, 0xACBC_F940, 0x32D8_6CE3, 0x45DF_5C75, 0xDCD6_0DCF, 0xABD1_3D59,
|
||||
0x26D9_30AC, 0x51DE_003A, 0xC8D7_5180, 0xBFD0_6116, 0x21B4_F4B5, 0x56B3_C423, 0xCFBA_9599, 0xB8BD_A50F,
|
||||
0x2802_B89E, 0x5F05_8808, 0xC60C_D9B2, 0xB10B_E924, 0x2F6F_7C87, 0x5868_4C11, 0xC161_1DAB, 0xB666_2D3D,
|
||||
0x76DC_4190, 0x01DB_7106, 0x98D2_20BC, 0xEFD5_102A, 0x71B1_8589, 0x06B6_B51F, 0x9FBF_E4A5, 0xE8B8_D433,
|
||||
0x7807_C9A2, 0x0F00_F934, 0x9609_A88E, 0xE10E_9818, 0x7F6A_0DBB, 0x086D_3D2D, 0x9164_6C97, 0xE663_5C01,
|
||||
0x6B6B_51F4, 0x1C6C_6162, 0x8565_30D8, 0xF262_004E, 0x6C06_95ED, 0x1B01_A57B, 0x8208_F4C1, 0xF50F_C457,
|
||||
0x65B0_D9C6, 0x12B7_E950, 0x8BBE_B8EA, 0xFCB9_887C, 0x62DD_1DDF, 0x15DA_2D49, 0x8CD3_7CF3, 0xFBD4_4C65,
|
||||
0x4DB2_6158, 0x3AB5_51CE, 0xA3BC_0074, 0xD4BB_30E2, 0x4ADF_A541, 0x3DD8_95D7, 0xA4D1_C46D, 0xD3D6_F4FB,
|
||||
0x4369_E96A, 0x346E_D9FC, 0xAD67_8846, 0xDA60_B8D0, 0x4404_2D73, 0x3303_1DE5, 0xAA0A_4C5F, 0xDD0D_7CC9,
|
||||
0x5005_713C, 0x2702_41AA, 0xBE0B_1010, 0xC90C_2086, 0x5768_B525, 0x206F_85B3, 0xB966_D409, 0xCE61_E49F,
|
||||
0x5EDE_F90E, 0x29D9_C998, 0xB0D0_9822, 0xC7D7_A8B4, 0x59B3_3D17, 0x2EB4_0D81, 0xB7BD_5C3B, 0xC0BA_6CAD,
|
||||
0xEDB8_8320, 0x9ABF_B3B6, 0x03B6_E20C, 0x74B1_D29A, 0xEAD5_4739, 0x9DD2_77AF, 0x04DB_2615, 0x73DC_1683,
|
||||
0xE363_0B12, 0x9464_3B84, 0x0D6D_6A3E, 0x7A6A_5AA8, 0xE40E_CF0B, 0x9309_FF9D, 0x0A00_AE27, 0x7D07_9EB1,
|
||||
0xF00F_9344, 0x8708_A3D2, 0x1E01_F268, 0x6906_C2FE, 0xF762_575D, 0x8065_67CB, 0x196C_3671, 0x6E6B_06E7,
|
||||
0xFED4_1B76, 0x89D3_2BE0, 0x10DA_7A5A, 0x67DD_4ACC, 0xF9B9_DF6F, 0x8EBE_EFF9, 0x17B7_BE43, 0x60B0_8ED5,
|
||||
0xD6D6_A3E8, 0xA1D1_937E, 0x38D8_C2C4, 0x4FDF_F252, 0xD1BB_67F1, 0xA6BC_5767, 0x3FB5_06DD, 0x48B2_364B,
|
||||
0xD80D_2BDA, 0xAF0A_1B4C, 0x3603_4AF6, 0x4104_7A60, 0xDF60_EFC3, 0xA867_DF55, 0x316E_8EEF, 0x4669_BE79,
|
||||
0xCB61_B38C, 0xBC66_831A, 0x256F_D2A0, 0x5268_E236, 0xCC0C_7795, 0xBB0B_4703, 0x2202_16B9, 0x5505_262F,
|
||||
0xC5BA_3BBE, 0xB2BD_0B28, 0x2BB4_5A92, 0x5CB3_6A04, 0xC2D7_FFA7, 0xB5D0_CF31, 0x2CD9_9E8B, 0x5BDE_AE1D,
|
||||
0x9B64_C2B0, 0xEC63_F226, 0x756A_A39C, 0x026D_930A, 0x9C09_06A9, 0xEB0E_363F, 0x7207_6785, 0x0500_5713,
|
||||
0x95BF_4A82, 0xE2B8_7A14, 0x7BB1_2BAE, 0x0CB6_1B38, 0x92D2_8E9B, 0xE5D5_BE0D, 0x7CDC_EFB7, 0x0BDB_DF21,
|
||||
0x86D3_D2D4, 0xF1D4_E242, 0x68DD_B3F8, 0x1FDA_836E, 0x81BE_16CD, 0xF6B9_265B, 0x6FB0_77E1, 0x18B7_4777,
|
||||
0x8808_5AE6, 0xFF0F_6A70, 0x6606_3BCA, 0x1101_0B5C, 0x8F65_9EFF, 0xF862_AE69, 0x616B_FFD3, 0x166C_CF45,
|
||||
0xA00A_E278, 0xD70D_D2EE, 0x4E04_8354, 0x3903_B3C2, 0xA767_2661, 0xD060_16F7, 0x4969_474D, 0x3E6E_77DB,
|
||||
0xAED1_6A4A, 0xD9D6_5ADC, 0x40DF_0B66, 0x37D8_3BF0, 0xA9BC_AE53, 0xDEBB_9EC5, 0x47B2_CF7F, 0x30B5_FFE9,
|
||||
0xBDBD_F21C, 0xCABA_C28A, 0x53B3_9330, 0x24B4_A3A6, 0xBAD0_3605, 0xCDD7_0693, 0x54DE_5729, 0x23D9_67BF,
|
||||
0xB366_7A2E, 0xC461_4AB8, 0x5D68_1B02, 0x2A6F_2B94, 0xB40B_BE37, 0xC30C_8EA1, 0x5A05_DF1B, 0x2D02_EF8D,
|
||||
0x0000_0000, 0x7707_3096, 0xEE0E_612C, 0x9909_51BA, 0x076D_C419, 0x706A_F48F, 0xE963_A535, 0x9E64_95A3,
|
||||
0x0EDB_8832, 0x79DC_B8A4, 0xE0D5_E91E, 0x97D2_D988, 0x09B6_4C2B, 0x7EB1_7CBD, 0xE7B8_2D07, 0x90BF_1D91,
|
||||
0x1DB7_1064, 0x6AB0_20F2, 0xF3B9_7148, 0x84BE_41DE, 0x1ADA_D47D, 0x6DDD_E4EB, 0xF4D4_B551, 0x83D3_85C7,
|
||||
0x136C_9856, 0x646B_A8C0, 0xFD62_F97A, 0x8A65_C9EC, 0x1401_5C4F, 0x6306_6CD9, 0xFA0F_3D63, 0x8D08_0DF5,
|
||||
0x3B6E_20C8, 0x4C69_105E, 0xD560_41E4, 0xA267_7172, 0x3C03_E4D1, 0x4B04_D447, 0xD20D_85FD, 0xA50A_B56B,
|
||||
0x35B5_A8FA, 0x42B2_986C, 0xDBBB_C9D6, 0xACBC_F940, 0x32D8_6CE3, 0x45DF_5C75, 0xDCD6_0DCF, 0xABD1_3D59,
|
||||
0x26D9_30AC, 0x51DE_003A, 0xC8D7_5180, 0xBFD0_6116, 0x21B4_F4B5, 0x56B3_C423, 0xCFBA_9599, 0xB8BD_A50F,
|
||||
0x2802_B89E, 0x5F05_8808, 0xC60C_D9B2, 0xB10B_E924, 0x2F6F_7C87, 0x5868_4C11, 0xC161_1DAB, 0xB666_2D3D,
|
||||
0x76DC_4190, 0x01DB_7106, 0x98D2_20BC, 0xEFD5_102A, 0x71B1_8589, 0x06B6_B51F, 0x9FBF_E4A5, 0xE8B8_D433,
|
||||
0x7807_C9A2, 0x0F00_F934, 0x9609_A88E, 0xE10E_9818, 0x7F6A_0DBB, 0x086D_3D2D, 0x9164_6C97, 0xE663_5C01,
|
||||
0x6B6B_51F4, 0x1C6C_6162, 0x8565_30D8, 0xF262_004E, 0x6C06_95ED, 0x1B01_A57B, 0x8208_F4C1, 0xF50F_C457,
|
||||
0x65B0_D9C6, 0x12B7_E950, 0x8BBE_B8EA, 0xFCB9_887C, 0x62DD_1DDF, 0x15DA_2D49, 0x8CD3_7CF3, 0xFBD4_4C65,
|
||||
0x4DB2_6158, 0x3AB5_51CE, 0xA3BC_0074, 0xD4BB_30E2, 0x4ADF_A541, 0x3DD8_95D7, 0xA4D1_C46D, 0xD3D6_F4FB,
|
||||
0x4369_E96A, 0x346E_D9FC, 0xAD67_8846, 0xDA60_B8D0, 0x4404_2D73, 0x3303_1DE5, 0xAA0A_4C5F, 0xDD0D_7CC9,
|
||||
0x5005_713C, 0x2702_41AA, 0xBE0B_1010, 0xC90C_2086, 0x5768_B525, 0x206F_85B3, 0xB966_D409, 0xCE61_E49F,
|
||||
0x5EDE_F90E, 0x29D9_C998, 0xB0D0_9822, 0xC7D7_A8B4, 0x59B3_3D17, 0x2EB4_0D81, 0xB7BD_5C3B, 0xC0BA_6CAD,
|
||||
0xEDB8_8320, 0x9ABF_B3B6, 0x03B6_E20C, 0x74B1_D29A, 0xEAD5_4739, 0x9DD2_77AF, 0x04DB_2615, 0x73DC_1683,
|
||||
0xE363_0B12, 0x9464_3B84, 0x0D6D_6A3E, 0x7A6A_5AA8, 0xE40E_CF0B, 0x9309_FF9D, 0x0A00_AE27, 0x7D07_9EB1,
|
||||
0xF00F_9344, 0x8708_A3D2, 0x1E01_F268, 0x6906_C2FE, 0xF762_575D, 0x8065_67CB, 0x196C_3671, 0x6E6B_06E7,
|
||||
0xFED4_1B76, 0x89D3_2BE0, 0x10DA_7A5A, 0x67DD_4ACC, 0xF9B9_DF6F, 0x8EBE_EFF9, 0x17B7_BE43, 0x60B0_8ED5,
|
||||
0xD6D6_A3E8, 0xA1D1_937E, 0x38D8_C2C4, 0x4FDF_F252, 0xD1BB_67F1, 0xA6BC_5767, 0x3FB5_06DD, 0x48B2_364B,
|
||||
0xD80D_2BDA, 0xAF0A_1B4C, 0x3603_4AF6, 0x4104_7A60, 0xDF60_EFC3, 0xA867_DF55, 0x316E_8EEF, 0x4669_BE79,
|
||||
0xCB61_B38C, 0xBC66_831A, 0x256F_D2A0, 0x5268_E236, 0xCC0C_7795, 0xBB0B_4703, 0x2202_16B9, 0x5505_262F,
|
||||
0xC5BA_3BBE, 0xB2BD_0B28, 0x2BB4_5A92, 0x5CB3_6A04, 0xC2D7_FFA7, 0xB5D0_CF31, 0x2CD9_9E8B, 0x5BDE_AE1D,
|
||||
0x9B64_C2B0, 0xEC63_F226, 0x756A_A39C, 0x026D_930A, 0x9C09_06A9, 0xEB0E_363F, 0x7207_6785, 0x0500_5713,
|
||||
0x95BF_4A82, 0xE2B8_7A14, 0x7BB1_2BAE, 0x0CB6_1B38, 0x92D2_8E9B, 0xE5D5_BE0D, 0x7CDC_EFB7, 0x0BDB_DF21,
|
||||
0x86D3_D2D4, 0xF1D4_E242, 0x68DD_B3F8, 0x1FDA_836E, 0x81BE_16CD, 0xF6B9_265B, 0x6FB0_77E1, 0x18B7_4777,
|
||||
0x8808_5AE6, 0xFF0F_6A70, 0x6606_3BCA, 0x1101_0B5C, 0x8F65_9EFF, 0xF862_AE69, 0x616B_FFD3, 0x166C_CF45,
|
||||
0xA00A_E278, 0xD70D_D2EE, 0x4E04_8354, 0x3903_B3C2, 0xA767_2661, 0xD060_16F7, 0x4969_474D, 0x3E6E_77DB,
|
||||
0xAED1_6A4A, 0xD9D6_5ADC, 0x40DF_0B66, 0x37D8_3BF0, 0xA9BC_AE53, 0xDEBB_9EC5, 0x47B2_CF7F, 0x30B5_FFE9,
|
||||
0xBDBD_F21C, 0xCABA_C28A, 0x53B3_9330, 0x24B4_A3A6, 0xBAD0_3605, 0xCDD7_0693, 0x54DE_5729, 0x23D9_67BF,
|
||||
0xB366_7A2E, 0xC461_4AB8, 0x5D68_1B02, 0x2A6F_2B94, 0xB40B_BE37, 0xC30C_8EA1, 0x5A05_DF1B, 0x2D02_EF8D,
|
||||
],[
|
||||
0x0000_0000, 0x191B_3141, 0x3236_6282, 0x2B2D_53C3, 0x646C_C504, 0x7D77_F445, 0x565A_A786, 0x4F41_96C7,
|
||||
0xC8D9_8A08, 0xD1C2_BB49, 0xFAEF_E88A, 0xE3F4_D9CB, 0xACB5_4F0C, 0xB5AE_7E4D, 0x9E83_2D8E, 0x8798_1CCF,
|
||||
0x4AC2_1251, 0x53D9_2310, 0x78F4_70D3, 0x61EF_4192, 0x2EAE_D755, 0x37B5_E614, 0x1C98_B5D7, 0x0583_8496,
|
||||
0x821B_9859, 0x9B00_A918, 0xB02D_FADB, 0xA936_CB9A, 0xE677_5D5D, 0xFF6C_6C1C, 0xD441_3FDF, 0xCD5A_0E9E,
|
||||
0x9584_24A2, 0x8C9F_15E3, 0xA7B2_4620, 0xBEA9_7761, 0xF1E8_E1A6, 0xE8F3_D0E7, 0xC3DE_8324, 0xDAC5_B265,
|
||||
0x5D5D_AEAA, 0x4446_9FEB, 0x6F6B_CC28, 0x7670_FD69, 0x3931_6BAE, 0x202A_5AEF, 0x0B07_092C, 0x121C_386D,
|
||||
0xDF46_36F3, 0xC65D_07B2, 0xED70_5471, 0xF46B_6530, 0xBB2A_F3F7, 0xA231_C2B6, 0x891C_9175, 0x9007_A034,
|
||||
0x179F_BCFB, 0x0E84_8DBA, 0x25A9_DE79, 0x3CB2_EF38, 0x73F3_79FF, 0x6AE8_48BE, 0x41C5_1B7D, 0x58DE_2A3C,
|
||||
0xF079_4F05, 0xE962_7E44, 0xC24F_2D87, 0xDB54_1CC6, 0x9415_8A01, 0x8D0E_BB40, 0xA623_E883, 0xBF38_D9C2,
|
||||
0x38A0_C50D, 0x21BB_F44C, 0x0A96_A78F, 0x138D_96CE, 0x5CCC_0009, 0x45D7_3148, 0x6EFA_628B, 0x77E1_53CA,
|
||||
0xBABB_5D54, 0xA3A0_6C15, 0x888D_3FD6, 0x9196_0E97, 0xDED7_9850, 0xC7CC_A911, 0xECE1_FAD2, 0xF5FA_CB93,
|
||||
0x7262_D75C, 0x6B79_E61D, 0x4054_B5DE, 0x594F_849F, 0x160E_1258, 0x0F15_2319, 0x2438_70DA, 0x3D23_419B,
|
||||
0x65FD_6BA7, 0x7CE6_5AE6, 0x57CB_0925, 0x4ED0_3864, 0x0191_AEA3, 0x188A_9FE2, 0x33A7_CC21, 0x2ABC_FD60,
|
||||
0xAD24_E1AF, 0xB43F_D0EE, 0x9F12_832D, 0x8609_B26C, 0xC948_24AB, 0xD053_15EA, 0xFB7E_4629, 0xE265_7768,
|
||||
0x2F3F_79F6, 0x3624_48B7, 0x1D09_1B74, 0x0412_2A35, 0x4B53_BCF2, 0x5248_8DB3, 0x7965_DE70, 0x607E_EF31,
|
||||
0xE7E6_F3FE, 0xFEFD_C2BF, 0xD5D0_917C, 0xCCCB_A03D, 0x838A_36FA, 0x9A91_07BB, 0xB1BC_5478, 0xA8A7_6539,
|
||||
0x3B83_984B, 0x2298_A90A, 0x09B5_FAC9, 0x10AE_CB88, 0x5FEF_5D4F, 0x46F4_6C0E, 0x6DD9_3FCD, 0x74C2_0E8C,
|
||||
0xF35A_1243, 0xEA41_2302, 0xC16C_70C1, 0xD877_4180, 0x9736_D747, 0x8E2D_E606, 0xA500_B5C5, 0xBC1B_8484,
|
||||
0x7141_8A1A, 0x685A_BB5B, 0x4377_E898, 0x5A6C_D9D9, 0x152D_4F1E, 0x0C36_7E5F, 0x271B_2D9C, 0x3E00_1CDD,
|
||||
0xB998_0012, 0xA083_3153, 0x8BAE_6290, 0x92B5_53D1, 0xDDF4_C516, 0xC4EF_F457, 0xEFC2_A794, 0xF6D9_96D5,
|
||||
0xAE07_BCE9, 0xB71C_8DA8, 0x9C31_DE6B, 0x852A_EF2A, 0xCA6B_79ED, 0xD370_48AC, 0xF85D_1B6F, 0xE146_2A2E,
|
||||
0x66DE_36E1, 0x7FC5_07A0, 0x54E8_5463, 0x4DF3_6522, 0x02B2_F3E5, 0x1BA9_C2A4, 0x3084_9167, 0x299F_A026,
|
||||
0xE4C5_AEB8, 0xFDDE_9FF9, 0xD6F3_CC3A, 0xCFE8_FD7B, 0x80A9_6BBC, 0x99B2_5AFD, 0xB29F_093E, 0xAB84_387F,
|
||||
0x2C1C_24B0, 0x3507_15F1, 0x1E2A_4632, 0x0731_7773, 0x4870_E1B4, 0x516B_D0F5, 0x7A46_8336, 0x635D_B277,
|
||||
0xCBFA_D74E, 0xD2E1_E60F, 0xF9CC_B5CC, 0xE0D7_848D, 0xAF96_124A, 0xB68D_230B, 0x9DA0_70C8, 0x84BB_4189,
|
||||
0x0323_5D46, 0x1A38_6C07, 0x3115_3FC4, 0x280E_0E85, 0x674F_9842, 0x7E54_A903, 0x5579_FAC0, 0x4C62_CB81,
|
||||
0x8138_C51F, 0x9823_F45E, 0xB30E_A79D, 0xAA15_96DC, 0xE554_001B, 0xFC4F_315A, 0xD762_6299, 0xCE79_53D8,
|
||||
0x49E1_4F17, 0x50FA_7E56, 0x7BD7_2D95, 0x62CC_1CD4, 0x2D8D_8A13, 0x3496_BB52, 0x1FBB_E891, 0x06A0_D9D0,
|
||||
0x5E7E_F3EC, 0x4765_C2AD, 0x6C48_916E, 0x7553_A02F, 0x3A12_36E8, 0x2309_07A9, 0x0824_546A, 0x113F_652B,
|
||||
0x96A7_79E4, 0x8FBC_48A5, 0xA491_1B66, 0xBD8A_2A27, 0xF2CB_BCE0, 0xEBD0_8DA1, 0xC0FD_DE62, 0xD9E6_EF23,
|
||||
0x14BC_E1BD, 0x0DA7_D0FC, 0x268A_833F, 0x3F91_B27E, 0x70D0_24B9, 0x69CB_15F8, 0x42E6_463B, 0x5BFD_777A,
|
||||
0xDC65_6BB5, 0xC57E_5AF4, 0xEE53_0937, 0xF748_3876, 0xB809_AEB1, 0xA112_9FF0, 0x8A3F_CC33, 0x9324_FD72,
|
||||
0x0000_0000, 0x191B_3141, 0x3236_6282, 0x2B2D_53C3, 0x646C_C504, 0x7D77_F445, 0x565A_A786, 0x4F41_96C7,
|
||||
0xC8D9_8A08, 0xD1C2_BB49, 0xFAEF_E88A, 0xE3F4_D9CB, 0xACB5_4F0C, 0xB5AE_7E4D, 0x9E83_2D8E, 0x8798_1CCF,
|
||||
0x4AC2_1251, 0x53D9_2310, 0x78F4_70D3, 0x61EF_4192, 0x2EAE_D755, 0x37B5_E614, 0x1C98_B5D7, 0x0583_8496,
|
||||
0x821B_9859, 0x9B00_A918, 0xB02D_FADB, 0xA936_CB9A, 0xE677_5D5D, 0xFF6C_6C1C, 0xD441_3FDF, 0xCD5A_0E9E,
|
||||
0x9584_24A2, 0x8C9F_15E3, 0xA7B2_4620, 0xBEA9_7761, 0xF1E8_E1A6, 0xE8F3_D0E7, 0xC3DE_8324, 0xDAC5_B265,
|
||||
0x5D5D_AEAA, 0x4446_9FEB, 0x6F6B_CC28, 0x7670_FD69, 0x3931_6BAE, 0x202A_5AEF, 0x0B07_092C, 0x121C_386D,
|
||||
0xDF46_36F3, 0xC65D_07B2, 0xED70_5471, 0xF46B_6530, 0xBB2A_F3F7, 0xA231_C2B6, 0x891C_9175, 0x9007_A034,
|
||||
0x179F_BCFB, 0x0E84_8DBA, 0x25A9_DE79, 0x3CB2_EF38, 0x73F3_79FF, 0x6AE8_48BE, 0x41C5_1B7D, 0x58DE_2A3C,
|
||||
0xF079_4F05, 0xE962_7E44, 0xC24F_2D87, 0xDB54_1CC6, 0x9415_8A01, 0x8D0E_BB40, 0xA623_E883, 0xBF38_D9C2,
|
||||
0x38A0_C50D, 0x21BB_F44C, 0x0A96_A78F, 0x138D_96CE, 0x5CCC_0009, 0x45D7_3148, 0x6EFA_628B, 0x77E1_53CA,
|
||||
0xBABB_5D54, 0xA3A0_6C15, 0x888D_3FD6, 0x9196_0E97, 0xDED7_9850, 0xC7CC_A911, 0xECE1_FAD2, 0xF5FA_CB93,
|
||||
0x7262_D75C, 0x6B79_E61D, 0x4054_B5DE, 0x594F_849F, 0x160E_1258, 0x0F15_2319, 0x2438_70DA, 0x3D23_419B,
|
||||
0x65FD_6BA7, 0x7CE6_5AE6, 0x57CB_0925, 0x4ED0_3864, 0x0191_AEA3, 0x188A_9FE2, 0x33A7_CC21, 0x2ABC_FD60,
|
||||
0xAD24_E1AF, 0xB43F_D0EE, 0x9F12_832D, 0x8609_B26C, 0xC948_24AB, 0xD053_15EA, 0xFB7E_4629, 0xE265_7768,
|
||||
0x2F3F_79F6, 0x3624_48B7, 0x1D09_1B74, 0x0412_2A35, 0x4B53_BCF2, 0x5248_8DB3, 0x7965_DE70, 0x607E_EF31,
|
||||
0xE7E6_F3FE, 0xFEFD_C2BF, 0xD5D0_917C, 0xCCCB_A03D, 0x838A_36FA, 0x9A91_07BB, 0xB1BC_5478, 0xA8A7_6539,
|
||||
0x3B83_984B, 0x2298_A90A, 0x09B5_FAC9, 0x10AE_CB88, 0x5FEF_5D4F, 0x46F4_6C0E, 0x6DD9_3FCD, 0x74C2_0E8C,
|
||||
0xF35A_1243, 0xEA41_2302, 0xC16C_70C1, 0xD877_4180, 0x9736_D747, 0x8E2D_E606, 0xA500_B5C5, 0xBC1B_8484,
|
||||
0x7141_8A1A, 0x685A_BB5B, 0x4377_E898, 0x5A6C_D9D9, 0x152D_4F1E, 0x0C36_7E5F, 0x271B_2D9C, 0x3E00_1CDD,
|
||||
0xB998_0012, 0xA083_3153, 0x8BAE_6290, 0x92B5_53D1, 0xDDF4_C516, 0xC4EF_F457, 0xEFC2_A794, 0xF6D9_96D5,
|
||||
0xAE07_BCE9, 0xB71C_8DA8, 0x9C31_DE6B, 0x852A_EF2A, 0xCA6B_79ED, 0xD370_48AC, 0xF85D_1B6F, 0xE146_2A2E,
|
||||
0x66DE_36E1, 0x7FC5_07A0, 0x54E8_5463, 0x4DF3_6522, 0x02B2_F3E5, 0x1BA9_C2A4, 0x3084_9167, 0x299F_A026,
|
||||
0xE4C5_AEB8, 0xFDDE_9FF9, 0xD6F3_CC3A, 0xCFE8_FD7B, 0x80A9_6BBC, 0x99B2_5AFD, 0xB29F_093E, 0xAB84_387F,
|
||||
0x2C1C_24B0, 0x3507_15F1, 0x1E2A_4632, 0x0731_7773, 0x4870_E1B4, 0x516B_D0F5, 0x7A46_8336, 0x635D_B277,
|
||||
0xCBFA_D74E, 0xD2E1_E60F, 0xF9CC_B5CC, 0xE0D7_848D, 0xAF96_124A, 0xB68D_230B, 0x9DA0_70C8, 0x84BB_4189,
|
||||
0x0323_5D46, 0x1A38_6C07, 0x3115_3FC4, 0x280E_0E85, 0x674F_9842, 0x7E54_A903, 0x5579_FAC0, 0x4C62_CB81,
|
||||
0x8138_C51F, 0x9823_F45E, 0xB30E_A79D, 0xAA15_96DC, 0xE554_001B, 0xFC4F_315A, 0xD762_6299, 0xCE79_53D8,
|
||||
0x49E1_4F17, 0x50FA_7E56, 0x7BD7_2D95, 0x62CC_1CD4, 0x2D8D_8A13, 0x3496_BB52, 0x1FBB_E891, 0x06A0_D9D0,
|
||||
0x5E7E_F3EC, 0x4765_C2AD, 0x6C48_916E, 0x7553_A02F, 0x3A12_36E8, 0x2309_07A9, 0x0824_546A, 0x113F_652B,
|
||||
0x96A7_79E4, 0x8FBC_48A5, 0xA491_1B66, 0xBD8A_2A27, 0xF2CB_BCE0, 0xEBD0_8DA1, 0xC0FD_DE62, 0xD9E6_EF23,
|
||||
0x14BC_E1BD, 0x0DA7_D0FC, 0x268A_833F, 0x3F91_B27E, 0x70D0_24B9, 0x69CB_15F8, 0x42E6_463B, 0x5BFD_777A,
|
||||
0xDC65_6BB5, 0xC57E_5AF4, 0xEE53_0937, 0xF748_3876, 0xB809_AEB1, 0xA112_9FF0, 0x8A3F_CC33, 0x9324_FD72,
|
||||
],[
|
||||
0x0000_0000, 0x01C2_6A37, 0x0384_D46E, 0x0246_BE59, 0x0709_A8DC, 0x06CB_C2EB, 0x048D_7CB2, 0x054F_1685,
|
||||
0x0E13_51B8, 0x0FD1_3B8F, 0x0D97_85D6, 0x0C55_EFE1, 0x091A_F964, 0x08D8_9353, 0x0A9E_2D0A, 0x0B5C_473D,
|
||||
0x1C26_A370, 0x1DE4_C947, 0x1FA2_771E, 0x1E60_1D29, 0x1B2F_0BAC, 0x1AED_619B, 0x18AB_DFC2, 0x1969_B5F5,
|
||||
0x1235_F2C8, 0x13F7_98FF, 0x11B1_26A6, 0x1073_4C91, 0x153C_5A14, 0x14FE_3023, 0x16B8_8E7A, 0x177A_E44D,
|
||||
0x384D_46E0, 0x398F_2CD7, 0x3BC9_928E, 0x3A0B_F8B9, 0x3F44_EE3C, 0x3E86_840B, 0x3CC0_3A52, 0x3D02_5065,
|
||||
0x365E_1758, 0x379C_7D6F, 0x35DA_C336, 0x3418_A901, 0x3157_BF84, 0x3095_D5B3, 0x32D3_6BEA, 0x3311_01DD,
|
||||
0x246B_E590, 0x25A9_8FA7, 0x27EF_31FE, 0x262D_5BC9, 0x2362_4D4C, 0x22A0_277B, 0x20E6_9922, 0x2124_F315,
|
||||
0x2A78_B428, 0x2BBA_DE1F, 0x29FC_6046, 0x283E_0A71, 0x2D71_1CF4, 0x2CB3_76C3, 0x2EF5_C89A, 0x2F37_A2AD,
|
||||
0x709A_8DC0, 0x7158_E7F7, 0x731E_59AE, 0x72DC_3399, 0x7793_251C, 0x7651_4F2B, 0x7417_F172, 0x75D5_9B45,
|
||||
0x7E89_DC78, 0x7F4B_B64F, 0x7D0D_0816, 0x7CCF_6221, 0x7980_74A4, 0x7842_1E93, 0x7A04_A0CA, 0x7BC6_CAFD,
|
||||
0x6CBC_2EB0, 0x6D7E_4487, 0x6F38_FADE, 0x6EFA_90E9, 0x6BB5_866C, 0x6A77_EC5B, 0x6831_5202, 0x69F3_3835,
|
||||
0x62AF_7F08, 0x636D_153F, 0x612B_AB66, 0x60E9_C151, 0x65A6_D7D4, 0x6464_BDE3, 0x6622_03BA, 0x67E0_698D,
|
||||
0x48D7_CB20, 0x4915_A117, 0x4B53_1F4E, 0x4A91_7579, 0x4FDE_63FC, 0x4E1C_09CB, 0x4C5A_B792, 0x4D98_DDA5,
|
||||
0x46C4_9A98, 0x4706_F0AF, 0x4540_4EF6, 0x4482_24C1, 0x41CD_3244, 0x400F_5873, 0x4249_E62A, 0x438B_8C1D,
|
||||
0x54F1_6850, 0x5533_0267, 0x5775_BC3E, 0x56B7_D609, 0x53F8_C08C, 0x523A_AABB, 0x507C_14E2, 0x51BE_7ED5,
|
||||
0x5AE2_39E8, 0x5B20_53DF, 0x5966_ED86, 0x58A4_87B1, 0x5DEB_9134, 0x5C29_FB03, 0x5E6F_455A, 0x5FAD_2F6D,
|
||||
0xE135_1B80, 0xE0F7_71B7, 0xE2B1_CFEE, 0xE373_A5D9, 0xE63C_B35C, 0xE7FE_D96B, 0xE5B8_6732, 0xE47A_0D05,
|
||||
0xEF26_4A38, 0xEEE4_200F, 0xECA2_9E56, 0xED60_F461, 0xE82F_E2E4, 0xE9ED_88D3, 0xEBAB_368A, 0xEA69_5CBD,
|
||||
0xFD13_B8F0, 0xFCD1_D2C7, 0xFE97_6C9E, 0xFF55_06A9, 0xFA1A_102C, 0xFBD8_7A1B, 0xF99E_C442, 0xF85C_AE75,
|
||||
0xF300_E948, 0xF2C2_837F, 0xF084_3D26, 0xF146_5711, 0xF409_4194, 0xF5CB_2BA3, 0xF78D_95FA, 0xF64F_FFCD,
|
||||
0xD978_5D60, 0xD8BA_3757, 0xDAFC_890E, 0xDB3E_E339, 0xDE71_F5BC, 0xDFB3_9F8B, 0xDDF5_21D2, 0xDC37_4BE5,
|
||||
0xD76B_0CD8, 0xD6A9_66EF, 0xD4EF_D8B6, 0xD52D_B281, 0xD062_A404, 0xD1A0_CE33, 0xD3E6_706A, 0xD224_1A5D,
|
||||
0xC55E_FE10, 0xC49C_9427, 0xC6DA_2A7E, 0xC718_4049, 0xC257_56CC, 0xC395_3CFB, 0xC1D3_82A2, 0xC011_E895,
|
||||
0xCB4D_AFA8, 0xCA8F_C59F, 0xC8C9_7BC6, 0xC90B_11F1, 0xCC44_0774, 0xCD86_6D43, 0xCFC0_D31A, 0xCE02_B92D,
|
||||
0x91AF_9640, 0x906D_FC77, 0x922B_422E, 0x93E9_2819, 0x96A6_3E9C, 0x9764_54AB, 0x9522_EAF2, 0x94E0_80C5,
|
||||
0x9FBC_C7F8, 0x9E7E_ADCF, 0x9C38_1396, 0x9DFA_79A1, 0x98B5_6F24, 0x9977_0513, 0x9B31_BB4A, 0x9AF3_D17D,
|
||||
0x8D89_3530, 0x8C4B_5F07, 0x8E0D_E15E, 0x8FCF_8B69, 0x8A80_9DEC, 0x8B42_F7DB, 0x8904_4982, 0x88C6_23B5,
|
||||
0x839A_6488, 0x8258_0EBF, 0x801E_B0E6, 0x81DC_DAD1, 0x8493_CC54, 0x8551_A663, 0x8717_183A, 0x86D5_720D,
|
||||
0xA9E2_D0A0, 0xA820_BA97, 0xAA66_04CE, 0xABA4_6EF9, 0xAEEB_787C, 0xAF29_124B, 0xAD6F_AC12, 0xACAD_C625,
|
||||
0xA7F1_8118, 0xA633_EB2F, 0xA475_5576, 0xA5B7_3F41, 0xA0F8_29C4, 0xA13A_43F3, 0xA37C_FDAA, 0xA2BE_979D,
|
||||
0xB5C4_73D0, 0xB406_19E7, 0xB640_A7BE, 0xB782_CD89, 0xB2CD_DB0C, 0xB30F_B13B, 0xB149_0F62, 0xB08B_6555,
|
||||
0xBBD7_2268, 0xBA15_485F, 0xB853_F606, 0xB991_9C31, 0xBCDE_8AB4, 0xBD1C_E083, 0xBF5A_5EDA, 0xBE98_34ED,
|
||||
0x0000_0000, 0x01C2_6A37, 0x0384_D46E, 0x0246_BE59, 0x0709_A8DC, 0x06CB_C2EB, 0x048D_7CB2, 0x054F_1685,
|
||||
0x0E13_51B8, 0x0FD1_3B8F, 0x0D97_85D6, 0x0C55_EFE1, 0x091A_F964, 0x08D8_9353, 0x0A9E_2D0A, 0x0B5C_473D,
|
||||
0x1C26_A370, 0x1DE4_C947, 0x1FA2_771E, 0x1E60_1D29, 0x1B2F_0BAC, 0x1AED_619B, 0x18AB_DFC2, 0x1969_B5F5,
|
||||
0x1235_F2C8, 0x13F7_98FF, 0x11B1_26A6, 0x1073_4C91, 0x153C_5A14, 0x14FE_3023, 0x16B8_8E7A, 0x177A_E44D,
|
||||
0x384D_46E0, 0x398F_2CD7, 0x3BC9_928E, 0x3A0B_F8B9, 0x3F44_EE3C, 0x3E86_840B, 0x3CC0_3A52, 0x3D02_5065,
|
||||
0x365E_1758, 0x379C_7D6F, 0x35DA_C336, 0x3418_A901, 0x3157_BF84, 0x3095_D5B3, 0x32D3_6BEA, 0x3311_01DD,
|
||||
0x246B_E590, 0x25A9_8FA7, 0x27EF_31FE, 0x262D_5BC9, 0x2362_4D4C, 0x22A0_277B, 0x20E6_9922, 0x2124_F315,
|
||||
0x2A78_B428, 0x2BBA_DE1F, 0x29FC_6046, 0x283E_0A71, 0x2D71_1CF4, 0x2CB3_76C3, 0x2EF5_C89A, 0x2F37_A2AD,
|
||||
0x709A_8DC0, 0x7158_E7F7, 0x731E_59AE, 0x72DC_3399, 0x7793_251C, 0x7651_4F2B, 0x7417_F172, 0x75D5_9B45,
|
||||
0x7E89_DC78, 0x7F4B_B64F, 0x7D0D_0816, 0x7CCF_6221, 0x7980_74A4, 0x7842_1E93, 0x7A04_A0CA, 0x7BC6_CAFD,
|
||||
0x6CBC_2EB0, 0x6D7E_4487, 0x6F38_FADE, 0x6EFA_90E9, 0x6BB5_866C, 0x6A77_EC5B, 0x6831_5202, 0x69F3_3835,
|
||||
0x62AF_7F08, 0x636D_153F, 0x612B_AB66, 0x60E9_C151, 0x65A6_D7D4, 0x6464_BDE3, 0x6622_03BA, 0x67E0_698D,
|
||||
0x48D7_CB20, 0x4915_A117, 0x4B53_1F4E, 0x4A91_7579, 0x4FDE_63FC, 0x4E1C_09CB, 0x4C5A_B792, 0x4D98_DDA5,
|
||||
0x46C4_9A98, 0x4706_F0AF, 0x4540_4EF6, 0x4482_24C1, 0x41CD_3244, 0x400F_5873, 0x4249_E62A, 0x438B_8C1D,
|
||||
0x54F1_6850, 0x5533_0267, 0x5775_BC3E, 0x56B7_D609, 0x53F8_C08C, 0x523A_AABB, 0x507C_14E2, 0x51BE_7ED5,
|
||||
0x5AE2_39E8, 0x5B20_53DF, 0x5966_ED86, 0x58A4_87B1, 0x5DEB_9134, 0x5C29_FB03, 0x5E6F_455A, 0x5FAD_2F6D,
|
||||
0xE135_1B80, 0xE0F7_71B7, 0xE2B1_CFEE, 0xE373_A5D9, 0xE63C_B35C, 0xE7FE_D96B, 0xE5B8_6732, 0xE47A_0D05,
|
||||
0xEF26_4A38, 0xEEE4_200F, 0xECA2_9E56, 0xED60_F461, 0xE82F_E2E4, 0xE9ED_88D3, 0xEBAB_368A, 0xEA69_5CBD,
|
||||
0xFD13_B8F0, 0xFCD1_D2C7, 0xFE97_6C9E, 0xFF55_06A9, 0xFA1A_102C, 0xFBD8_7A1B, 0xF99E_C442, 0xF85C_AE75,
|
||||
0xF300_E948, 0xF2C2_837F, 0xF084_3D26, 0xF146_5711, 0xF409_4194, 0xF5CB_2BA3, 0xF78D_95FA, 0xF64F_FFCD,
|
||||
0xD978_5D60, 0xD8BA_3757, 0xDAFC_890E, 0xDB3E_E339, 0xDE71_F5BC, 0xDFB3_9F8B, 0xDDF5_21D2, 0xDC37_4BE5,
|
||||
0xD76B_0CD8, 0xD6A9_66EF, 0xD4EF_D8B6, 0xD52D_B281, 0xD062_A404, 0xD1A0_CE33, 0xD3E6_706A, 0xD224_1A5D,
|
||||
0xC55E_FE10, 0xC49C_9427, 0xC6DA_2A7E, 0xC718_4049, 0xC257_56CC, 0xC395_3CFB, 0xC1D3_82A2, 0xC011_E895,
|
||||
0xCB4D_AFA8, 0xCA8F_C59F, 0xC8C9_7BC6, 0xC90B_11F1, 0xCC44_0774, 0xCD86_6D43, 0xCFC0_D31A, 0xCE02_B92D,
|
||||
0x91AF_9640, 0x906D_FC77, 0x922B_422E, 0x93E9_2819, 0x96A6_3E9C, 0x9764_54AB, 0x9522_EAF2, 0x94E0_80C5,
|
||||
0x9FBC_C7F8, 0x9E7E_ADCF, 0x9C38_1396, 0x9DFA_79A1, 0x98B5_6F24, 0x9977_0513, 0x9B31_BB4A, 0x9AF3_D17D,
|
||||
0x8D89_3530, 0x8C4B_5F07, 0x8E0D_E15E, 0x8FCF_8B69, 0x8A80_9DEC, 0x8B42_F7DB, 0x8904_4982, 0x88C6_23B5,
|
||||
0x839A_6488, 0x8258_0EBF, 0x801E_B0E6, 0x81DC_DAD1, 0x8493_CC54, 0x8551_A663, 0x8717_183A, 0x86D5_720D,
|
||||
0xA9E2_D0A0, 0xA820_BA97, 0xAA66_04CE, 0xABA4_6EF9, 0xAEEB_787C, 0xAF29_124B, 0xAD6F_AC12, 0xACAD_C625,
|
||||
0xA7F1_8118, 0xA633_EB2F, 0xA475_5576, 0xA5B7_3F41, 0xA0F8_29C4, 0xA13A_43F3, 0xA37C_FDAA, 0xA2BE_979D,
|
||||
0xB5C4_73D0, 0xB406_19E7, 0xB640_A7BE, 0xB782_CD89, 0xB2CD_DB0C, 0xB30F_B13B, 0xB149_0F62, 0xB08B_6555,
|
||||
0xBBD7_2268, 0xBA15_485F, 0xB853_F606, 0xB991_9C31, 0xBCDE_8AB4, 0xBD1C_E083, 0xBF5A_5EDA, 0xBE98_34ED,
|
||||
],[
|
||||
0x0000_0000, 0xB8BC_6765, 0xAA09_C88B, 0x12B5_AFEE, 0x8F62_9757, 0x37DE_F032, 0x256B_5FDC, 0x9DD7_38B9,
|
||||
0xC5B4_28EF, 0x7D08_4F8A, 0x6FBD_E064, 0xD701_8701, 0x4AD6_BFB8, 0xF26A_D8DD, 0xE0DF_7733, 0x5863_1056,
|
||||
0x5019_579F, 0xE8A5_30FA, 0xFA10_9F14, 0x42AC_F871, 0xDF7B_C0C8, 0x67C7_A7AD, 0x7572_0843, 0xCDCE_6F26,
|
||||
0x95AD_7F70, 0x2D11_1815, 0x3FA4_B7FB, 0x8718_D09E, 0x1ACF_E827, 0xA273_8F42, 0xB0C6_20AC, 0x087A_47C9,
|
||||
0xA032_AF3E, 0x188E_C85B, 0x0A3B_67B5, 0xB287_00D0, 0x2F50_3869, 0x97EC_5F0C, 0x8559_F0E2, 0x3DE5_9787,
|
||||
0x6586_87D1, 0xDD3A_E0B4, 0xCF8F_4F5A, 0x7733_283F, 0xEAE4_1086, 0x5258_77E3, 0x40ED_D80D, 0xF851_BF68,
|
||||
0xF02B_F8A1, 0x4897_9FC4, 0x5A22_302A, 0xE29E_574F, 0x7F49_6FF6, 0xC7F5_0893, 0xD540_A77D, 0x6DFC_C018,
|
||||
0x359F_D04E, 0x8D23_B72B, 0x9F96_18C5, 0x272A_7FA0, 0xBAFD_4719, 0x0241_207C, 0x10F4_8F92, 0xA848_E8F7,
|
||||
0x9B14_583D, 0x23A8_3F58, 0x311D_90B6, 0x89A1_F7D3, 0x1476_CF6A, 0xACCA_A80F, 0xBE7F_07E1, 0x06C3_6084,
|
||||
0x5EA0_70D2, 0xE61C_17B7, 0xF4A9_B859, 0x4C15_DF3C, 0xD1C2_E785, 0x697E_80E0, 0x7BCB_2F0E, 0xC377_486B,
|
||||
0xCB0D_0FA2, 0x73B1_68C7, 0x6104_C729, 0xD9B8_A04C, 0x446F_98F5, 0xFCD3_FF90, 0xEE66_507E, 0x56DA_371B,
|
||||
0x0EB9_274D, 0xB605_4028, 0xA4B0_EFC6, 0x1C0C_88A3, 0x81DB_B01A, 0x3967_D77F, 0x2BD2_7891, 0x936E_1FF4,
|
||||
0x3B26_F703, 0x839A_9066, 0x912F_3F88, 0x2993_58ED, 0xB444_6054, 0x0CF8_0731, 0x1E4D_A8DF, 0xA6F1_CFBA,
|
||||
0xFE92_DFEC, 0x462E_B889, 0x549B_1767, 0xEC27_7002, 0x71F0_48BB, 0xC94C_2FDE, 0xDBF9_8030, 0x6345_E755,
|
||||
0x6B3F_A09C, 0xD383_C7F9, 0xC136_6817, 0x798A_0F72, 0xE45D_37CB, 0x5CE1_50AE, 0x4E54_FF40, 0xF6E8_9825,
|
||||
0xAE8B_8873, 0x1637_EF16, 0x0482_40F8, 0xBC3E_279D, 0x21E9_1F24, 0x9955_7841, 0x8BE0_D7AF, 0x335C_B0CA,
|
||||
0xED59_B63B, 0x55E5_D15E, 0x4750_7EB0, 0xFFEC_19D5, 0x623B_216C, 0xDA87_4609, 0xC832_E9E7, 0x708E_8E82,
|
||||
0x28ED_9ED4, 0x9051_F9B1, 0x82E4_565F, 0x3A58_313A, 0xA78F_0983, 0x1F33_6EE6, 0x0D86_C108, 0xB53A_A66D,
|
||||
0xBD40_E1A4, 0x05FC_86C1, 0x1749_292F, 0xAFF5_4E4A, 0x3222_76F3, 0x8A9E_1196, 0x982B_BE78, 0x2097_D91D,
|
||||
0x78F4_C94B, 0xC048_AE2E, 0xD2FD_01C0, 0x6A41_66A5, 0xF796_5E1C, 0x4F2A_3979, 0x5D9F_9697, 0xE523_F1F2,
|
||||
0x4D6B_1905, 0xF5D7_7E60, 0xE762_D18E, 0x5FDE_B6EB, 0xC209_8E52, 0x7AB5_E937, 0x6800_46D9, 0xD0BC_21BC,
|
||||
0x88DF_31EA, 0x3063_568F, 0x22D6_F961, 0x9A6A_9E04, 0x07BD_A6BD, 0xBF01_C1D8, 0xADB4_6E36, 0x1508_0953,
|
||||
0x1D72_4E9A, 0xA5CE_29FF, 0xB77B_8611, 0x0FC7_E174, 0x9210_D9CD, 0x2AAC_BEA8, 0x3819_1146, 0x80A5_7623,
|
||||
0xD8C6_6675, 0x607A_0110, 0x72CF_AEFE, 0xCA73_C99B, 0x57A4_F122, 0xEF18_9647, 0xFDAD_39A9, 0x4511_5ECC,
|
||||
0x764D_EE06, 0xCEF1_8963, 0xDC44_268D, 0x64F8_41E8, 0xF92F_7951, 0x4193_1E34, 0x5326_B1DA, 0xEB9A_D6BF,
|
||||
0xB3F9_C6E9, 0x0B45_A18C, 0x19F0_0E62, 0xA14C_6907, 0x3C9B_51BE, 0x8427_36DB, 0x9692_9935, 0x2E2E_FE50,
|
||||
0x2654_B999, 0x9EE8_DEFC, 0x8C5D_7112, 0x34E1_1677, 0xA936_2ECE, 0x118A_49AB, 0x033F_E645, 0xBB83_8120,
|
||||
0xE3E0_9176, 0x5B5C_F613, 0x49E9_59FD, 0xF155_3E98, 0x6C82_0621, 0xD43E_6144, 0xC68B_CEAA, 0x7E37_A9CF,
|
||||
0xD67F_4138, 0x6EC3_265D, 0x7C76_89B3, 0xC4CA_EED6, 0x591D_D66F, 0xE1A1_B10A, 0xF314_1EE4, 0x4BA8_7981,
|
||||
0x13CB_69D7, 0xAB77_0EB2, 0xB9C2_A15C, 0x017E_C639, 0x9CA9_FE80, 0x2415_99E5, 0x36A0_360B, 0x8E1C_516E,
|
||||
0x8666_16A7, 0x3EDA_71C2, 0x2C6F_DE2C, 0x94D3_B949, 0x0904_81F0, 0xB1B8_E695, 0xA30D_497B, 0x1BB1_2E1E,
|
||||
0x43D2_3E48, 0xFB6E_592D, 0xE9DB_F6C3, 0x5167_91A6, 0xCCB0_A91F, 0x740C_CE7A, 0x66B9_6194, 0xDE05_06F1,
|
||||
0x0000_0000, 0xB8BC_6765, 0xAA09_C88B, 0x12B5_AFEE, 0x8F62_9757, 0x37DE_F032, 0x256B_5FDC, 0x9DD7_38B9,
|
||||
0xC5B4_28EF, 0x7D08_4F8A, 0x6FBD_E064, 0xD701_8701, 0x4AD6_BFB8, 0xF26A_D8DD, 0xE0DF_7733, 0x5863_1056,
|
||||
0x5019_579F, 0xE8A5_30FA, 0xFA10_9F14, 0x42AC_F871, 0xDF7B_C0C8, 0x67C7_A7AD, 0x7572_0843, 0xCDCE_6F26,
|
||||
0x95AD_7F70, 0x2D11_1815, 0x3FA4_B7FB, 0x8718_D09E, 0x1ACF_E827, 0xA273_8F42, 0xB0C6_20AC, 0x087A_47C9,
|
||||
0xA032_AF3E, 0x188E_C85B, 0x0A3B_67B5, 0xB287_00D0, 0x2F50_3869, 0x97EC_5F0C, 0x8559_F0E2, 0x3DE5_9787,
|
||||
0x6586_87D1, 0xDD3A_E0B4, 0xCF8F_4F5A, 0x7733_283F, 0xEAE4_1086, 0x5258_77E3, 0x40ED_D80D, 0xF851_BF68,
|
||||
0xF02B_F8A1, 0x4897_9FC4, 0x5A22_302A, 0xE29E_574F, 0x7F49_6FF6, 0xC7F5_0893, 0xD540_A77D, 0x6DFC_C018,
|
||||
0x359F_D04E, 0x8D23_B72B, 0x9F96_18C5, 0x272A_7FA0, 0xBAFD_4719, 0x0241_207C, 0x10F4_8F92, 0xA848_E8F7,
|
||||
0x9B14_583D, 0x23A8_3F58, 0x311D_90B6, 0x89A1_F7D3, 0x1476_CF6A, 0xACCA_A80F, 0xBE7F_07E1, 0x06C3_6084,
|
||||
0x5EA0_70D2, 0xE61C_17B7, 0xF4A9_B859, 0x4C15_DF3C, 0xD1C2_E785, 0x697E_80E0, 0x7BCB_2F0E, 0xC377_486B,
|
||||
0xCB0D_0FA2, 0x73B1_68C7, 0x6104_C729, 0xD9B8_A04C, 0x446F_98F5, 0xFCD3_FF90, 0xEE66_507E, 0x56DA_371B,
|
||||
0x0EB9_274D, 0xB605_4028, 0xA4B0_EFC6, 0x1C0C_88A3, 0x81DB_B01A, 0x3967_D77F, 0x2BD2_7891, 0x936E_1FF4,
|
||||
0x3B26_F703, 0x839A_9066, 0x912F_3F88, 0x2993_58ED, 0xB444_6054, 0x0CF8_0731, 0x1E4D_A8DF, 0xA6F1_CFBA,
|
||||
0xFE92_DFEC, 0x462E_B889, 0x549B_1767, 0xEC27_7002, 0x71F0_48BB, 0xC94C_2FDE, 0xDBF9_8030, 0x6345_E755,
|
||||
0x6B3F_A09C, 0xD383_C7F9, 0xC136_6817, 0x798A_0F72, 0xE45D_37CB, 0x5CE1_50AE, 0x4E54_FF40, 0xF6E8_9825,
|
||||
0xAE8B_8873, 0x1637_EF16, 0x0482_40F8, 0xBC3E_279D, 0x21E9_1F24, 0x9955_7841, 0x8BE0_D7AF, 0x335C_B0CA,
|
||||
0xED59_B63B, 0x55E5_D15E, 0x4750_7EB0, 0xFFEC_19D5, 0x623B_216C, 0xDA87_4609, 0xC832_E9E7, 0x708E_8E82,
|
||||
0x28ED_9ED4, 0x9051_F9B1, 0x82E4_565F, 0x3A58_313A, 0xA78F_0983, 0x1F33_6EE6, 0x0D86_C108, 0xB53A_A66D,
|
||||
0xBD40_E1A4, 0x05FC_86C1, 0x1749_292F, 0xAFF5_4E4A, 0x3222_76F3, 0x8A9E_1196, 0x982B_BE78, 0x2097_D91D,
|
||||
0x78F4_C94B, 0xC048_AE2E, 0xD2FD_01C0, 0x6A41_66A5, 0xF796_5E1C, 0x4F2A_3979, 0x5D9F_9697, 0xE523_F1F2,
|
||||
0x4D6B_1905, 0xF5D7_7E60, 0xE762_D18E, 0x5FDE_B6EB, 0xC209_8E52, 0x7AB5_E937, 0x6800_46D9, 0xD0BC_21BC,
|
||||
0x88DF_31EA, 0x3063_568F, 0x22D6_F961, 0x9A6A_9E04, 0x07BD_A6BD, 0xBF01_C1D8, 0xADB4_6E36, 0x1508_0953,
|
||||
0x1D72_4E9A, 0xA5CE_29FF, 0xB77B_8611, 0x0FC7_E174, 0x9210_D9CD, 0x2AAC_BEA8, 0x3819_1146, 0x80A5_7623,
|
||||
0xD8C6_6675, 0x607A_0110, 0x72CF_AEFE, 0xCA73_C99B, 0x57A4_F122, 0xEF18_9647, 0xFDAD_39A9, 0x4511_5ECC,
|
||||
0x764D_EE06, 0xCEF1_8963, 0xDC44_268D, 0x64F8_41E8, 0xF92F_7951, 0x4193_1E34, 0x5326_B1DA, 0xEB9A_D6BF,
|
||||
0xB3F9_C6E9, 0x0B45_A18C, 0x19F0_0E62, 0xA14C_6907, 0x3C9B_51BE, 0x8427_36DB, 0x9692_9935, 0x2E2E_FE50,
|
||||
0x2654_B999, 0x9EE8_DEFC, 0x8C5D_7112, 0x34E1_1677, 0xA936_2ECE, 0x118A_49AB, 0x033F_E645, 0xBB83_8120,
|
||||
0xE3E0_9176, 0x5B5C_F613, 0x49E9_59FD, 0xF155_3E98, 0x6C82_0621, 0xD43E_6144, 0xC68B_CEAA, 0x7E37_A9CF,
|
||||
0xD67F_4138, 0x6EC3_265D, 0x7C76_89B3, 0xC4CA_EED6, 0x591D_D66F, 0xE1A1_B10A, 0xF314_1EE4, 0x4BA8_7981,
|
||||
0x13CB_69D7, 0xAB77_0EB2, 0xB9C2_A15C, 0x017E_C639, 0x9CA9_FE80, 0x2415_99E5, 0x36A0_360B, 0x8E1C_516E,
|
||||
0x8666_16A7, 0x3EDA_71C2, 0x2C6F_DE2C, 0x94D3_B949, 0x0904_81F0, 0xB1B8_E695, 0xA30D_497B, 0x1BB1_2E1E,
|
||||
0x43D2_3E48, 0xFB6E_592D, 0xE9DB_F6C3, 0x5167_91A6, 0xCCB0_A91F, 0x740C_CE7A, 0x66B9_6194, 0xDE05_06F1,
|
||||
],[
|
||||
0x0000_0000, 0x3D60_29B0, 0x7AC0_5360, 0x47A0_7AD0, 0xF580_A6C0, 0xC8E0_8F70, 0x8F40_F5A0, 0xB220_DC10,
|
||||
0x3070_4BC1, 0x0D10_6271, 0x4AB0_18A1, 0x77D0_3111, 0xC5F0_ED01, 0xF890_C4B1, 0xBF30_BE61, 0x8250_97D1,
|
||||
0x60E0_9782, 0x5D80_BE32, 0x1A20_C4E2, 0x2740_ED52, 0x9560_3142, 0xA800_18F2, 0xEFA0_6222, 0xD2C0_4B92,
|
||||
0x5090_DC43, 0x6DF0_F5F3, 0x2A50_8F23, 0x1730_A693, 0xA510_7A83, 0x9870_5333, 0xDFD0_29E3, 0xE2B0_0053,
|
||||
0xC1C1_2F04, 0xFCA1_06B4, 0xBB01_7C64, 0x8661_55D4, 0x3441_89C4, 0x0921_A074, 0x4E81_DAA4, 0x73E1_F314,
|
||||
0xF1B1_64C5, 0xCCD1_4D75, 0x8B71_37A5, 0xB611_1E15, 0x0431_C205, 0x3951_EBB5, 0x7EF1_9165, 0x4391_B8D5,
|
||||
0xA121_B886, 0x9C41_9136, 0xDBE1_EBE6, 0xE681_C256, 0x54A1_1E46, 0x69C1_37F6, 0x2E61_4D26, 0x1301_6496,
|
||||
0x9151_F347, 0xAC31_DAF7, 0xEB91_A027, 0xD6F1_8997, 0x64D1_5587, 0x59B1_7C37, 0x1E11_06E7, 0x2371_2F57,
|
||||
0x58F3_5849, 0x6593_71F9, 0x2233_0B29, 0x1F53_2299, 0xAD73_FE89, 0x9013_D739, 0xD7B3_ADE9, 0xEAD3_8459,
|
||||
0x6883_1388, 0x55E3_3A38, 0x1243_40E8, 0x2F23_6958, 0x9D03_B548, 0xA063_9CF8, 0xE7C3_E628, 0xDAA3_CF98,
|
||||
0x3813_CFCB, 0x0573_E67B, 0x42D3_9CAB, 0x7FB3_B51B, 0xCD93_690B, 0xF0F3_40BB, 0xB753_3A6B, 0x8A33_13DB,
|
||||
0x0863_840A, 0x3503_ADBA, 0x72A3_D76A, 0x4FC3_FEDA, 0xFDE3_22CA, 0xC083_0B7A, 0x8723_71AA, 0xBA43_581A,
|
||||
0x9932_774D, 0xA452_5EFD, 0xE3F2_242D, 0xDE92_0D9D, 0x6CB2_D18D, 0x51D2_F83D, 0x1672_82ED, 0x2B12_AB5D,
|
||||
0xA942_3C8C, 0x9422_153C, 0xD382_6FEC, 0xEEE2_465C, 0x5CC2_9A4C, 0x61A2_B3FC, 0x2602_C92C, 0x1B62_E09C,
|
||||
0xF9D2_E0CF, 0xC4B2_C97F, 0x8312_B3AF, 0xBE72_9A1F, 0x0C52_460F, 0x3132_6FBF, 0x7692_156F, 0x4BF2_3CDF,
|
||||
0xC9A2_AB0E, 0xF4C2_82BE, 0xB362_F86E, 0x8E02_D1DE, 0x3C22_0DCE, 0x0142_247E, 0x46E2_5EAE, 0x7B82_771E,
|
||||
0xB1E6_B092, 0x8C86_9922, 0xCB26_E3F2, 0xF646_CA42, 0x4466_1652, 0x7906_3FE2, 0x3EA6_4532, 0x03C6_6C82,
|
||||
0x8196_FB53, 0xBCF6_D2E3, 0xFB56_A833, 0xC636_8183, 0x7416_5D93, 0x4976_7423, 0x0ED6_0EF3, 0x33B6_2743,
|
||||
0xD106_2710, 0xEC66_0EA0, 0xABC6_7470, 0x96A6_5DC0, 0x2486_81D0, 0x19E6_A860, 0x5E46_D2B0, 0x6326_FB00,
|
||||
0xE176_6CD1, 0xDC16_4561, 0x9BB6_3FB1, 0xA6D6_1601, 0x14F6_CA11, 0x2996_E3A1, 0x6E36_9971, 0x5356_B0C1,
|
||||
0x7027_9F96, 0x4D47_B626, 0x0AE7_CCF6, 0x3787_E546, 0x85A7_3956, 0xB8C7_10E6, 0xFF67_6A36, 0xC207_4386,
|
||||
0x4057_D457, 0x7D37_FDE7, 0x3A97_8737, 0x07F7_AE87, 0xB5D7_7297, 0x88B7_5B27, 0xCF17_21F7, 0xF277_0847,
|
||||
0x10C7_0814, 0x2DA7_21A4, 0x6A07_5B74, 0x5767_72C4, 0xE547_AED4, 0xD827_8764, 0x9F87_FDB4, 0xA2E7_D404,
|
||||
0x20B7_43D5, 0x1DD7_6A65, 0x5A77_10B5, 0x6717_3905, 0xD537_E515, 0xE857_CCA5, 0xAFF7_B675, 0x9297_9FC5,
|
||||
0xE915_E8DB, 0xD475_C16B, 0x93D5_BBBB, 0xAEB5_920B, 0x1C95_4E1B, 0x21F5_67AB, 0x6655_1D7B, 0x5B35_34CB,
|
||||
0xD965_A31A, 0xE405_8AAA, 0xA3A5_F07A, 0x9EC5_D9CA, 0x2CE5_05DA, 0x1185_2C6A, 0x5625_56BA, 0x6B45_7F0A,
|
||||
0x89F5_7F59, 0xB495_56E9, 0xF335_2C39, 0xCE55_0589, 0x7C75_D999, 0x4115_F029, 0x06B5_8AF9, 0x3BD5_A349,
|
||||
0xB985_3498, 0x84E5_1D28, 0xC345_67F8, 0xFE25_4E48, 0x4C05_9258, 0x7165_BBE8, 0x36C5_C138, 0x0BA5_E888,
|
||||
0x28D4_C7DF, 0x15B4_EE6F, 0x5214_94BF, 0x6F74_BD0F, 0xDD54_611F, 0xE034_48AF, 0xA794_327F, 0x9AF4_1BCF,
|
||||
0x18A4_8C1E, 0x25C4_A5AE, 0x6264_DF7E, 0x5F04_F6CE, 0xED24_2ADE, 0xD044_036E, 0x97E4_79BE, 0xAA84_500E,
|
||||
0x4834_505D, 0x7554_79ED, 0x32F4_033D, 0x0F94_2A8D, 0xBDB4_F69D, 0x80D4_DF2D, 0xC774_A5FD, 0xFA14_8C4D,
|
||||
0x7844_1B9C, 0x4524_322C, 0x0284_48FC, 0x3FE4_614C, 0x8DC4_BD5C, 0xB0A4_94EC, 0xF704_EE3C, 0xCA64_C78C,
|
||||
0x0000_0000, 0x3D60_29B0, 0x7AC0_5360, 0x47A0_7AD0, 0xF580_A6C0, 0xC8E0_8F70, 0x8F40_F5A0, 0xB220_DC10,
|
||||
0x3070_4BC1, 0x0D10_6271, 0x4AB0_18A1, 0x77D0_3111, 0xC5F0_ED01, 0xF890_C4B1, 0xBF30_BE61, 0x8250_97D1,
|
||||
0x60E0_9782, 0x5D80_BE32, 0x1A20_C4E2, 0x2740_ED52, 0x9560_3142, 0xA800_18F2, 0xEFA0_6222, 0xD2C0_4B92,
|
||||
0x5090_DC43, 0x6DF0_F5F3, 0x2A50_8F23, 0x1730_A693, 0xA510_7A83, 0x9870_5333, 0xDFD0_29E3, 0xE2B0_0053,
|
||||
0xC1C1_2F04, 0xFCA1_06B4, 0xBB01_7C64, 0x8661_55D4, 0x3441_89C4, 0x0921_A074, 0x4E81_DAA4, 0x73E1_F314,
|
||||
0xF1B1_64C5, 0xCCD1_4D75, 0x8B71_37A5, 0xB611_1E15, 0x0431_C205, 0x3951_EBB5, 0x7EF1_9165, 0x4391_B8D5,
|
||||
0xA121_B886, 0x9C41_9136, 0xDBE1_EBE6, 0xE681_C256, 0x54A1_1E46, 0x69C1_37F6, 0x2E61_4D26, 0x1301_6496,
|
||||
0x9151_F347, 0xAC31_DAF7, 0xEB91_A027, 0xD6F1_8997, 0x64D1_5587, 0x59B1_7C37, 0x1E11_06E7, 0x2371_2F57,
|
||||
0x58F3_5849, 0x6593_71F9, 0x2233_0B29, 0x1F53_2299, 0xAD73_FE89, 0x9013_D739, 0xD7B3_ADE9, 0xEAD3_8459,
|
||||
0x6883_1388, 0x55E3_3A38, 0x1243_40E8, 0x2F23_6958, 0x9D03_B548, 0xA063_9CF8, 0xE7C3_E628, 0xDAA3_CF98,
|
||||
0x3813_CFCB, 0x0573_E67B, 0x42D3_9CAB, 0x7FB3_B51B, 0xCD93_690B, 0xF0F3_40BB, 0xB753_3A6B, 0x8A33_13DB,
|
||||
0x0863_840A, 0x3503_ADBA, 0x72A3_D76A, 0x4FC3_FEDA, 0xFDE3_22CA, 0xC083_0B7A, 0x8723_71AA, 0xBA43_581A,
|
||||
0x9932_774D, 0xA452_5EFD, 0xE3F2_242D, 0xDE92_0D9D, 0x6CB2_D18D, 0x51D2_F83D, 0x1672_82ED, 0x2B12_AB5D,
|
||||
0xA942_3C8C, 0x9422_153C, 0xD382_6FEC, 0xEEE2_465C, 0x5CC2_9A4C, 0x61A2_B3FC, 0x2602_C92C, 0x1B62_E09C,
|
||||
0xF9D2_E0CF, 0xC4B2_C97F, 0x8312_B3AF, 0xBE72_9A1F, 0x0C52_460F, 0x3132_6FBF, 0x7692_156F, 0x4BF2_3CDF,
|
||||
0xC9A2_AB0E, 0xF4C2_82BE, 0xB362_F86E, 0x8E02_D1DE, 0x3C22_0DCE, 0x0142_247E, 0x46E2_5EAE, 0x7B82_771E,
|
||||
0xB1E6_B092, 0x8C86_9922, 0xCB26_E3F2, 0xF646_CA42, 0x4466_1652, 0x7906_3FE2, 0x3EA6_4532, 0x03C6_6C82,
|
||||
0x8196_FB53, 0xBCF6_D2E3, 0xFB56_A833, 0xC636_8183, 0x7416_5D93, 0x4976_7423, 0x0ED6_0EF3, 0x33B6_2743,
|
||||
0xD106_2710, 0xEC66_0EA0, 0xABC6_7470, 0x96A6_5DC0, 0x2486_81D0, 0x19E6_A860, 0x5E46_D2B0, 0x6326_FB00,
|
||||
0xE176_6CD1, 0xDC16_4561, 0x9BB6_3FB1, 0xA6D6_1601, 0x14F6_CA11, 0x2996_E3A1, 0x6E36_9971, 0x5356_B0C1,
|
||||
0x7027_9F96, 0x4D47_B626, 0x0AE7_CCF6, 0x3787_E546, 0x85A7_3956, 0xB8C7_10E6, 0xFF67_6A36, 0xC207_4386,
|
||||
0x4057_D457, 0x7D37_FDE7, 0x3A97_8737, 0x07F7_AE87, 0xB5D7_7297, 0x88B7_5B27, 0xCF17_21F7, 0xF277_0847,
|
||||
0x10C7_0814, 0x2DA7_21A4, 0x6A07_5B74, 0x5767_72C4, 0xE547_AED4, 0xD827_8764, 0x9F87_FDB4, 0xA2E7_D404,
|
||||
0x20B7_43D5, 0x1DD7_6A65, 0x5A77_10B5, 0x6717_3905, 0xD537_E515, 0xE857_CCA5, 0xAFF7_B675, 0x9297_9FC5,
|
||||
0xE915_E8DB, 0xD475_C16B, 0x93D5_BBBB, 0xAEB5_920B, 0x1C95_4E1B, 0x21F5_67AB, 0x6655_1D7B, 0x5B35_34CB,
|
||||
0xD965_A31A, 0xE405_8AAA, 0xA3A5_F07A, 0x9EC5_D9CA, 0x2CE5_05DA, 0x1185_2C6A, 0x5625_56BA, 0x6B45_7F0A,
|
||||
0x89F5_7F59, 0xB495_56E9, 0xF335_2C39, 0xCE55_0589, 0x7C75_D999, 0x4115_F029, 0x06B5_8AF9, 0x3BD5_A349,
|
||||
0xB985_3498, 0x84E5_1D28, 0xC345_67F8, 0xFE25_4E48, 0x4C05_9258, 0x7165_BBE8, 0x36C5_C138, 0x0BA5_E888,
|
||||
0x28D4_C7DF, 0x15B4_EE6F, 0x5214_94BF, 0x6F74_BD0F, 0xDD54_611F, 0xE034_48AF, 0xA794_327F, 0x9AF4_1BCF,
|
||||
0x18A4_8C1E, 0x25C4_A5AE, 0x6264_DF7E, 0x5F04_F6CE, 0xED24_2ADE, 0xD044_036E, 0x97E4_79BE, 0xAA84_500E,
|
||||
0x4834_505D, 0x7554_79ED, 0x32F4_033D, 0x0F94_2A8D, 0xBDB4_F69D, 0x80D4_DF2D, 0xC774_A5FD, 0xFA14_8C4D,
|
||||
0x7844_1B9C, 0x4524_322C, 0x0284_48FC, 0x3FE4_614C, 0x8DC4_BD5C, 0xB0A4_94EC, 0xF704_EE3C, 0xCA64_C78C,
|
||||
],[
|
||||
0x0000_0000, 0xCB5C_D3A5, 0x4DC8_A10B, 0x8694_72AE, 0x9B91_4216, 0x50CD_91B3, 0xD659_E31D, 0x1D05_30B8,
|
||||
0xEC53_826D, 0x270F_51C8, 0xA19B_2366, 0x6AC7_F0C3, 0x77C2_C07B, 0xBC9E_13DE, 0x3A0A_6170, 0xF156_B2D5,
|
||||
0x03D6_029B, 0xC88A_D13E, 0x4E1E_A390, 0x8542_7035, 0x9847_408D, 0x531B_9328, 0xD58F_E186, 0x1ED3_3223,
|
||||
0xEF85_80F6, 0x24D9_5353, 0xA24D_21FD, 0x6911_F258, 0x7414_C2E0, 0xBF48_1145, 0x39DC_63EB, 0xF280_B04E,
|
||||
0x07AC_0536, 0xCCF0_D693, 0x4A64_A43D, 0x8138_7798, 0x9C3D_4720, 0x5761_9485, 0xD1F5_E62B, 0x1AA9_358E,
|
||||
0xEBFF_875B, 0x20A3_54FE, 0xA637_2650, 0x6D6B_F5F5, 0x706E_C54D, 0xBB32_16E8, 0x3DA6_6446, 0xF6FA_B7E3,
|
||||
0x047A_07AD, 0xCF26_D408, 0x49B2_A6A6, 0x82EE_7503, 0x9FEB_45BB, 0x54B7_961E, 0xD223_E4B0, 0x197F_3715,
|
||||
0xE829_85C0, 0x2375_5665, 0xA5E1_24CB, 0x6EBD_F76E, 0x73B8_C7D6, 0xB8E4_1473, 0x3E70_66DD, 0xF52C_B578,
|
||||
0x0F58_0A6C, 0xC404_D9C9, 0x4290_AB67, 0x89CC_78C2, 0x94C9_487A, 0x5F95_9BDF, 0xD901_E971, 0x125D_3AD4,
|
||||
0xE30B_8801, 0x2857_5BA4, 0xAEC3_290A, 0x659F_FAAF, 0x789A_CA17, 0xB3C6_19B2, 0x3552_6B1C, 0xFE0E_B8B9,
|
||||
0x0C8E_08F7, 0xC7D2_DB52, 0x4146_A9FC, 0x8A1A_7A59, 0x971F_4AE1, 0x5C43_9944, 0xDAD7_EBEA, 0x118B_384F,
|
||||
0xE0DD_8A9A, 0x2B81_593F, 0xAD15_2B91, 0x6649_F834, 0x7B4C_C88C, 0xB010_1B29, 0x3684_6987, 0xFDD8_BA22,
|
||||
0x08F4_0F5A, 0xC3A8_DCFF, 0x453C_AE51, 0x8E60_7DF4, 0x9365_4D4C, 0x5839_9EE9, 0xDEAD_EC47, 0x15F1_3FE2,
|
||||
0xE4A7_8D37, 0x2FFB_5E92, 0xA96F_2C3C, 0x6233_FF99, 0x7F36_CF21, 0xB46A_1C84, 0x32FE_6E2A, 0xF9A2_BD8F,
|
||||
0x0B22_0DC1, 0xC07E_DE64, 0x46EA_ACCA, 0x8DB6_7F6F, 0x90B3_4FD7, 0x5BEF_9C72, 0xDD7B_EEDC, 0x1627_3D79,
|
||||
0xE771_8FAC, 0x2C2D_5C09, 0xAAB9_2EA7, 0x61E5_FD02, 0x7CE0_CDBA, 0xB7BC_1E1F, 0x3128_6CB1, 0xFA74_BF14,
|
||||
0x1EB0_14D8, 0xD5EC_C77D, 0x5378_B5D3, 0x9824_6676, 0x8521_56CE, 0x4E7D_856B, 0xC8E9_F7C5, 0x03B5_2460,
|
||||
0xF2E3_96B5, 0x39BF_4510, 0xBF2B_37BE, 0x7477_E41B, 0x6972_D4A3, 0xA22E_0706, 0x24BA_75A8, 0xEFE6_A60D,
|
||||
0x1D66_1643, 0xD63A_C5E6, 0x50AE_B748, 0x9BF2_64ED, 0x86F7_5455, 0x4DAB_87F0, 0xCB3F_F55E, 0x0063_26FB,
|
||||
0xF135_942E, 0x3A69_478B, 0xBCFD_3525, 0x77A1_E680, 0x6AA4_D638, 0xA1F8_059D, 0x276C_7733, 0xEC30_A496,
|
||||
0x191C_11EE, 0xD240_C24B, 0x54D4_B0E5, 0x9F88_6340, 0x828D_53F8, 0x49D1_805D, 0xCF45_F2F3, 0x0419_2156,
|
||||
0xF54F_9383, 0x3E13_4026, 0xB887_3288, 0x73DB_E12D, 0x6EDE_D195, 0xA582_0230, 0x2316_709E, 0xE84A_A33B,
|
||||
0x1ACA_1375, 0xD196_C0D0, 0x5702_B27E, 0x9C5E_61DB, 0x815B_5163, 0x4A07_82C6, 0xCC93_F068, 0x07CF_23CD,
|
||||
0xF699_9118, 0x3DC5_42BD, 0xBB51_3013, 0x700D_E3B6, 0x6D08_D30E, 0xA654_00AB, 0x20C0_7205, 0xEB9C_A1A0,
|
||||
0x11E8_1EB4, 0xDAB4_CD11, 0x5C20_BFBF, 0x977C_6C1A, 0x8A79_5CA2, 0x4125_8F07, 0xC7B1_FDA9, 0x0CED_2E0C,
|
||||
0xFDBB_9CD9, 0x36E7_4F7C, 0xB073_3DD2, 0x7B2F_EE77, 0x662A_DECF, 0xAD76_0D6A, 0x2BE2_7FC4, 0xE0BE_AC61,
|
||||
0x123E_1C2F, 0xD962_CF8A, 0x5FF6_BD24, 0x94AA_6E81, 0x89AF_5E39, 0x42F3_8D9C, 0xC467_FF32, 0x0F3B_2C97,
|
||||
0xFE6D_9E42, 0x3531_4DE7, 0xB3A5_3F49, 0x78F9_ECEC, 0x65FC_DC54, 0xAEA0_0FF1, 0x2834_7D5F, 0xE368_AEFA,
|
||||
0x1644_1B82, 0xDD18_C827, 0x5B8C_BA89, 0x90D0_692C, 0x8DD5_5994, 0x4689_8A31, 0xC01D_F89F, 0x0B41_2B3A,
|
||||
0xFA17_99EF, 0x314B_4A4A, 0xB7DF_38E4, 0x7C83_EB41, 0x6186_DBF9, 0xAADA_085C, 0x2C4E_7AF2, 0xE712_A957,
|
||||
0x1592_1919, 0xDECE_CABC, 0x585A_B812, 0x9306_6BB7, 0x8E03_5B0F, 0x455F_88AA, 0xC3CB_FA04, 0x0897_29A1,
|
||||
0xF9C1_9B74, 0x329D_48D1, 0xB409_3A7F, 0x7F55_E9DA, 0x6250_D962, 0xA90C_0AC7, 0x2F98_7869, 0xE4C4_ABCC,
|
||||
0x0000_0000, 0xCB5C_D3A5, 0x4DC8_A10B, 0x8694_72AE, 0x9B91_4216, 0x50CD_91B3, 0xD659_E31D, 0x1D05_30B8,
|
||||
0xEC53_826D, 0x270F_51C8, 0xA19B_2366, 0x6AC7_F0C3, 0x77C2_C07B, 0xBC9E_13DE, 0x3A0A_6170, 0xF156_B2D5,
|
||||
0x03D6_029B, 0xC88A_D13E, 0x4E1E_A390, 0x8542_7035, 0x9847_408D, 0x531B_9328, 0xD58F_E186, 0x1ED3_3223,
|
||||
0xEF85_80F6, 0x24D9_5353, 0xA24D_21FD, 0x6911_F258, 0x7414_C2E0, 0xBF48_1145, 0x39DC_63EB, 0xF280_B04E,
|
||||
0x07AC_0536, 0xCCF0_D693, 0x4A64_A43D, 0x8138_7798, 0x9C3D_4720, 0x5761_9485, 0xD1F5_E62B, 0x1AA9_358E,
|
||||
0xEBFF_875B, 0x20A3_54FE, 0xA637_2650, 0x6D6B_F5F5, 0x706E_C54D, 0xBB32_16E8, 0x3DA6_6446, 0xF6FA_B7E3,
|
||||
0x047A_07AD, 0xCF26_D408, 0x49B2_A6A6, 0x82EE_7503, 0x9FEB_45BB, 0x54B7_961E, 0xD223_E4B0, 0x197F_3715,
|
||||
0xE829_85C0, 0x2375_5665, 0xA5E1_24CB, 0x6EBD_F76E, 0x73B8_C7D6, 0xB8E4_1473, 0x3E70_66DD, 0xF52C_B578,
|
||||
0x0F58_0A6C, 0xC404_D9C9, 0x4290_AB67, 0x89CC_78C2, 0x94C9_487A, 0x5F95_9BDF, 0xD901_E971, 0x125D_3AD4,
|
||||
0xE30B_8801, 0x2857_5BA4, 0xAEC3_290A, 0x659F_FAAF, 0x789A_CA17, 0xB3C6_19B2, 0x3552_6B1C, 0xFE0E_B8B9,
|
||||
0x0C8E_08F7, 0xC7D2_DB52, 0x4146_A9FC, 0x8A1A_7A59, 0x971F_4AE1, 0x5C43_9944, 0xDAD7_EBEA, 0x118B_384F,
|
||||
0xE0DD_8A9A, 0x2B81_593F, 0xAD15_2B91, 0x6649_F834, 0x7B4C_C88C, 0xB010_1B29, 0x3684_6987, 0xFDD8_BA22,
|
||||
0x08F4_0F5A, 0xC3A8_DCFF, 0x453C_AE51, 0x8E60_7DF4, 0x9365_4D4C, 0x5839_9EE9, 0xDEAD_EC47, 0x15F1_3FE2,
|
||||
0xE4A7_8D37, 0x2FFB_5E92, 0xA96F_2C3C, 0x6233_FF99, 0x7F36_CF21, 0xB46A_1C84, 0x32FE_6E2A, 0xF9A2_BD8F,
|
||||
0x0B22_0DC1, 0xC07E_DE64, 0x46EA_ACCA, 0x8DB6_7F6F, 0x90B3_4FD7, 0x5BEF_9C72, 0xDD7B_EEDC, 0x1627_3D79,
|
||||
0xE771_8FAC, 0x2C2D_5C09, 0xAAB9_2EA7, 0x61E5_FD02, 0x7CE0_CDBA, 0xB7BC_1E1F, 0x3128_6CB1, 0xFA74_BF14,
|
||||
0x1EB0_14D8, 0xD5EC_C77D, 0x5378_B5D3, 0x9824_6676, 0x8521_56CE, 0x4E7D_856B, 0xC8E9_F7C5, 0x03B5_2460,
|
||||
0xF2E3_96B5, 0x39BF_4510, 0xBF2B_37BE, 0x7477_E41B, 0x6972_D4A3, 0xA22E_0706, 0x24BA_75A8, 0xEFE6_A60D,
|
||||
0x1D66_1643, 0xD63A_C5E6, 0x50AE_B748, 0x9BF2_64ED, 0x86F7_5455, 0x4DAB_87F0, 0xCB3F_F55E, 0x0063_26FB,
|
||||
0xF135_942E, 0x3A69_478B, 0xBCFD_3525, 0x77A1_E680, 0x6AA4_D638, 0xA1F8_059D, 0x276C_7733, 0xEC30_A496,
|
||||
0x191C_11EE, 0xD240_C24B, 0x54D4_B0E5, 0x9F88_6340, 0x828D_53F8, 0x49D1_805D, 0xCF45_F2F3, 0x0419_2156,
|
||||
0xF54F_9383, 0x3E13_4026, 0xB887_3288, 0x73DB_E12D, 0x6EDE_D195, 0xA582_0230, 0x2316_709E, 0xE84A_A33B,
|
||||
0x1ACA_1375, 0xD196_C0D0, 0x5702_B27E, 0x9C5E_61DB, 0x815B_5163, 0x4A07_82C6, 0xCC93_F068, 0x07CF_23CD,
|
||||
0xF699_9118, 0x3DC5_42BD, 0xBB51_3013, 0x700D_E3B6, 0x6D08_D30E, 0xA654_00AB, 0x20C0_7205, 0xEB9C_A1A0,
|
||||
0x11E8_1EB4, 0xDAB4_CD11, 0x5C20_BFBF, 0x977C_6C1A, 0x8A79_5CA2, 0x4125_8F07, 0xC7B1_FDA9, 0x0CED_2E0C,
|
||||
0xFDBB_9CD9, 0x36E7_4F7C, 0xB073_3DD2, 0x7B2F_EE77, 0x662A_DECF, 0xAD76_0D6A, 0x2BE2_7FC4, 0xE0BE_AC61,
|
||||
0x123E_1C2F, 0xD962_CF8A, 0x5FF6_BD24, 0x94AA_6E81, 0x89AF_5E39, 0x42F3_8D9C, 0xC467_FF32, 0x0F3B_2C97,
|
||||
0xFE6D_9E42, 0x3531_4DE7, 0xB3A5_3F49, 0x78F9_ECEC, 0x65FC_DC54, 0xAEA0_0FF1, 0x2834_7D5F, 0xE368_AEFA,
|
||||
0x1644_1B82, 0xDD18_C827, 0x5B8C_BA89, 0x90D0_692C, 0x8DD5_5994, 0x4689_8A31, 0xC01D_F89F, 0x0B41_2B3A,
|
||||
0xFA17_99EF, 0x314B_4A4A, 0xB7DF_38E4, 0x7C83_EB41, 0x6186_DBF9, 0xAADA_085C, 0x2C4E_7AF2, 0xE712_A957,
|
||||
0x1592_1919, 0xDECE_CABC, 0x585A_B812, 0x9306_6BB7, 0x8E03_5B0F, 0x455F_88AA, 0xC3CB_FA04, 0x0897_29A1,
|
||||
0xF9C1_9B74, 0x329D_48D1, 0xB409_3A7F, 0x7F55_E9DA, 0x6250_D962, 0xA90C_0AC7, 0x2F98_7869, 0xE4C4_ABCC,
|
||||
],[
|
||||
0x0000_0000, 0xA677_0BB4, 0x979F_1129, 0x31E8_1A9D, 0xF44F_2413, 0x5238_2FA7, 0x63D0_353A, 0xC5A7_3E8E,
|
||||
0x33EF_4E67, 0x9598_45D3, 0xA470_5F4E, 0x0207_54FA, 0xC7A0_6A74, 0x61D7_61C0, 0x503F_7B5D, 0xF648_70E9,
|
||||
0x67DE_9CCE, 0xC1A9_977A, 0xF041_8DE7, 0x5636_8653, 0x9391_B8DD, 0x35E6_B369, 0x040E_A9F4, 0xA279_A240,
|
||||
0x5431_D2A9, 0xF246_D91D, 0xC3AE_C380, 0x65D9_C834, 0xA07E_F6BA, 0x0609_FD0E, 0x37E1_E793, 0x9196_EC27,
|
||||
0xCFBD_399C, 0x69CA_3228, 0x5822_28B5, 0xFE55_2301, 0x3BF2_1D8F, 0x9D85_163B, 0xAC6D_0CA6, 0x0A1A_0712,
|
||||
0xFC52_77FB, 0x5A25_7C4F, 0x6BCD_66D2, 0xCDBA_6D66, 0x081D_53E8, 0xAE6A_585C, 0x9F82_42C1, 0x39F5_4975,
|
||||
0xA863_A552, 0x0E14_AEE6, 0x3FFC_B47B, 0x998B_BFCF, 0x5C2C_8141, 0xFA5B_8AF5, 0xCBB3_9068, 0x6DC4_9BDC,
|
||||
0x9B8C_EB35, 0x3DFB_E081, 0x0C13_FA1C, 0xAA64_F1A8, 0x6FC3_CF26, 0xC9B4_C492, 0xF85C_DE0F, 0x5E2B_D5BB,
|
||||
0x440B_7579, 0xE27C_7ECD, 0xD394_6450, 0x75E3_6FE4, 0xB044_516A, 0x1633_5ADE, 0x27DB_4043, 0x81AC_4BF7,
|
||||
0x77E4_3B1E, 0xD193_30AA, 0xE07B_2A37, 0x460C_2183, 0x83AB_1F0D, 0x25DC_14B9, 0x1434_0E24, 0xB243_0590,
|
||||
0x23D5_E9B7, 0x85A2_E203, 0xB44A_F89E, 0x123D_F32A, 0xD79A_CDA4, 0x71ED_C610, 0x4005_DC8D, 0xE672_D739,
|
||||
0x103A_A7D0, 0xB64D_AC64, 0x87A5_B6F9, 0x21D2_BD4D, 0xE475_83C3, 0x4202_8877, 0x73EA_92EA, 0xD59D_995E,
|
||||
0x8BB6_4CE5, 0x2DC1_4751, 0x1C29_5DCC, 0xBA5E_5678, 0x7FF9_68F6, 0xD98E_6342, 0xE866_79DF, 0x4E11_726B,
|
||||
0xB859_0282, 0x1E2E_0936, 0x2FC6_13AB, 0x89B1_181F, 0x4C16_2691, 0xEA61_2D25, 0xDB89_37B8, 0x7DFE_3C0C,
|
||||
0xEC68_D02B, 0x4A1F_DB9F, 0x7BF7_C102, 0xDD80_CAB6, 0x1827_F438, 0xBE50_FF8C, 0x8FB8_E511, 0x29CF_EEA5,
|
||||
0xDF87_9E4C, 0x79F0_95F8, 0x4818_8F65, 0xEE6F_84D1, 0x2BC8_BA5F, 0x8DBF_B1EB, 0xBC57_AB76, 0x1A20_A0C2,
|
||||
0x8816_EAF2, 0x2E61_E146, 0x1F89_FBDB, 0xB9FE_F06F, 0x7C59_CEE1, 0xDA2E_C555, 0xEBC6_DFC8, 0x4DB1_D47C,
|
||||
0xBBF9_A495, 0x1D8E_AF21, 0x2C66_B5BC, 0x8A11_BE08, 0x4FB6_8086, 0xE9C1_8B32, 0xD829_91AF, 0x7E5E_9A1B,
|
||||
0xEFC8_763C, 0x49BF_7D88, 0x7857_6715, 0xDE20_6CA1, 0x1B87_522F, 0xBDF0_599B, 0x8C18_4306, 0x2A6F_48B2,
|
||||
0xDC27_385B, 0x7A50_33EF, 0x4BB8_2972, 0xEDCF_22C6, 0x2868_1C48, 0x8E1F_17FC, 0xBFF7_0D61, 0x1980_06D5,
|
||||
0x47AB_D36E, 0xE1DC_D8DA, 0xD034_C247, 0x7643_C9F3, 0xB3E4_F77D, 0x1593_FCC9, 0x247B_E654, 0x820C_EDE0,
|
||||
0x7444_9D09, 0xD233_96BD, 0xE3DB_8C20, 0x45AC_8794, 0x800B_B91A, 0x267C_B2AE, 0x1794_A833, 0xB1E3_A387,
|
||||
0x2075_4FA0, 0x8602_4414, 0xB7EA_5E89, 0x119D_553D, 0xD43A_6BB3, 0x724D_6007, 0x43A5_7A9A, 0xE5D2_712E,
|
||||
0x139A_01C7, 0xB5ED_0A73, 0x8405_10EE, 0x2272_1B5A, 0xE7D5_25D4, 0x41A2_2E60, 0x704A_34FD, 0xD63D_3F49,
|
||||
0xCC1D_9F8B, 0x6A6A_943F, 0x5B82_8EA2, 0xFDF5_8516, 0x3852_BB98, 0x9E25_B02C, 0xAFCD_AAB1, 0x09BA_A105,
|
||||
0xFFF2_D1EC, 0x5985_DA58, 0x686D_C0C5, 0xCE1A_CB71, 0x0BBD_F5FF, 0xADCA_FE4B, 0x9C22_E4D6, 0x3A55_EF62,
|
||||
0xABC3_0345, 0x0DB4_08F1, 0x3C5C_126C, 0x9A2B_19D8, 0x5F8C_2756, 0xF9FB_2CE2, 0xC813_367F, 0x6E64_3DCB,
|
||||
0x982C_4D22, 0x3E5B_4696, 0x0FB3_5C0B, 0xA9C4_57BF, 0x6C63_6931, 0xCA14_6285, 0xFBFC_7818, 0x5D8B_73AC,
|
||||
0x03A0_A617, 0xA5D7_ADA3, 0x943F_B73E, 0x3248_BC8A, 0xF7EF_8204, 0x5198_89B0, 0x6070_932D, 0xC607_9899,
|
||||
0x304F_E870, 0x9638_E3C4, 0xA7D0_F959, 0x01A7_F2ED, 0xC400_CC63, 0x6277_C7D7, 0x539F_DD4A, 0xF5E8_D6FE,
|
||||
0x647E_3AD9, 0xC209_316D, 0xF3E1_2BF0, 0x5596_2044, 0x9031_1ECA, 0x3646_157E, 0x07AE_0FE3, 0xA1D9_0457,
|
||||
0x5791_74BE, 0xF1E6_7F0A, 0xC00E_6597, 0x6679_6E23, 0xA3DE_50AD, 0x05A9_5B19, 0x3441_4184, 0x9236_4A30,
|
||||
0x0000_0000, 0xA677_0BB4, 0x979F_1129, 0x31E8_1A9D, 0xF44F_2413, 0x5238_2FA7, 0x63D0_353A, 0xC5A7_3E8E,
|
||||
0x33EF_4E67, 0x9598_45D3, 0xA470_5F4E, 0x0207_54FA, 0xC7A0_6A74, 0x61D7_61C0, 0x503F_7B5D, 0xF648_70E9,
|
||||
0x67DE_9CCE, 0xC1A9_977A, 0xF041_8DE7, 0x5636_8653, 0x9391_B8DD, 0x35E6_B369, 0x040E_A9F4, 0xA279_A240,
|
||||
0x5431_D2A9, 0xF246_D91D, 0xC3AE_C380, 0x65D9_C834, 0xA07E_F6BA, 0x0609_FD0E, 0x37E1_E793, 0x9196_EC27,
|
||||
0xCFBD_399C, 0x69CA_3228, 0x5822_28B5, 0xFE55_2301, 0x3BF2_1D8F, 0x9D85_163B, 0xAC6D_0CA6, 0x0A1A_0712,
|
||||
0xFC52_77FB, 0x5A25_7C4F, 0x6BCD_66D2, 0xCDBA_6D66, 0x081D_53E8, 0xAE6A_585C, 0x9F82_42C1, 0x39F5_4975,
|
||||
0xA863_A552, 0x0E14_AEE6, 0x3FFC_B47B, 0x998B_BFCF, 0x5C2C_8141, 0xFA5B_8AF5, 0xCBB3_9068, 0x6DC4_9BDC,
|
||||
0x9B8C_EB35, 0x3DFB_E081, 0x0C13_FA1C, 0xAA64_F1A8, 0x6FC3_CF26, 0xC9B4_C492, 0xF85C_DE0F, 0x5E2B_D5BB,
|
||||
0x440B_7579, 0xE27C_7ECD, 0xD394_6450, 0x75E3_6FE4, 0xB044_516A, 0x1633_5ADE, 0x27DB_4043, 0x81AC_4BF7,
|
||||
0x77E4_3B1E, 0xD193_30AA, 0xE07B_2A37, 0x460C_2183, 0x83AB_1F0D, 0x25DC_14B9, 0x1434_0E24, 0xB243_0590,
|
||||
0x23D5_E9B7, 0x85A2_E203, 0xB44A_F89E, 0x123D_F32A, 0xD79A_CDA4, 0x71ED_C610, 0x4005_DC8D, 0xE672_D739,
|
||||
0x103A_A7D0, 0xB64D_AC64, 0x87A5_B6F9, 0x21D2_BD4D, 0xE475_83C3, 0x4202_8877, 0x73EA_92EA, 0xD59D_995E,
|
||||
0x8BB6_4CE5, 0x2DC1_4751, 0x1C29_5DCC, 0xBA5E_5678, 0x7FF9_68F6, 0xD98E_6342, 0xE866_79DF, 0x4E11_726B,
|
||||
0xB859_0282, 0x1E2E_0936, 0x2FC6_13AB, 0x89B1_181F, 0x4C16_2691, 0xEA61_2D25, 0xDB89_37B8, 0x7DFE_3C0C,
|
||||
0xEC68_D02B, 0x4A1F_DB9F, 0x7BF7_C102, 0xDD80_CAB6, 0x1827_F438, 0xBE50_FF8C, 0x8FB8_E511, 0x29CF_EEA5,
|
||||
0xDF87_9E4C, 0x79F0_95F8, 0x4818_8F65, 0xEE6F_84D1, 0x2BC8_BA5F, 0x8DBF_B1EB, 0xBC57_AB76, 0x1A20_A0C2,
|
||||
0x8816_EAF2, 0x2E61_E146, 0x1F89_FBDB, 0xB9FE_F06F, 0x7C59_CEE1, 0xDA2E_C555, 0xEBC6_DFC8, 0x4DB1_D47C,
|
||||
0xBBF9_A495, 0x1D8E_AF21, 0x2C66_B5BC, 0x8A11_BE08, 0x4FB6_8086, 0xE9C1_8B32, 0xD829_91AF, 0x7E5E_9A1B,
|
||||
0xEFC8_763C, 0x49BF_7D88, 0x7857_6715, 0xDE20_6CA1, 0x1B87_522F, 0xBDF0_599B, 0x8C18_4306, 0x2A6F_48B2,
|
||||
0xDC27_385B, 0x7A50_33EF, 0x4BB8_2972, 0xEDCF_22C6, 0x2868_1C48, 0x8E1F_17FC, 0xBFF7_0D61, 0x1980_06D5,
|
||||
0x47AB_D36E, 0xE1DC_D8DA, 0xD034_C247, 0x7643_C9F3, 0xB3E4_F77D, 0x1593_FCC9, 0x247B_E654, 0x820C_EDE0,
|
||||
0x7444_9D09, 0xD233_96BD, 0xE3DB_8C20, 0x45AC_8794, 0x800B_B91A, 0x267C_B2AE, 0x1794_A833, 0xB1E3_A387,
|
||||
0x2075_4FA0, 0x8602_4414, 0xB7EA_5E89, 0x119D_553D, 0xD43A_6BB3, 0x724D_6007, 0x43A5_7A9A, 0xE5D2_712E,
|
||||
0x139A_01C7, 0xB5ED_0A73, 0x8405_10EE, 0x2272_1B5A, 0xE7D5_25D4, 0x41A2_2E60, 0x704A_34FD, 0xD63D_3F49,
|
||||
0xCC1D_9F8B, 0x6A6A_943F, 0x5B82_8EA2, 0xFDF5_8516, 0x3852_BB98, 0x9E25_B02C, 0xAFCD_AAB1, 0x09BA_A105,
|
||||
0xFFF2_D1EC, 0x5985_DA58, 0x686D_C0C5, 0xCE1A_CB71, 0x0BBD_F5FF, 0xADCA_FE4B, 0x9C22_E4D6, 0x3A55_EF62,
|
||||
0xABC3_0345, 0x0DB4_08F1, 0x3C5C_126C, 0x9A2B_19D8, 0x5F8C_2756, 0xF9FB_2CE2, 0xC813_367F, 0x6E64_3DCB,
|
||||
0x982C_4D22, 0x3E5B_4696, 0x0FB3_5C0B, 0xA9C4_57BF, 0x6C63_6931, 0xCA14_6285, 0xFBFC_7818, 0x5D8B_73AC,
|
||||
0x03A0_A617, 0xA5D7_ADA3, 0x943F_B73E, 0x3248_BC8A, 0xF7EF_8204, 0x5198_89B0, 0x6070_932D, 0xC607_9899,
|
||||
0x304F_E870, 0x9638_E3C4, 0xA7D0_F959, 0x01A7_F2ED, 0xC400_CC63, 0x6277_C7D7, 0x539F_DD4A, 0xF5E8_D6FE,
|
||||
0x647E_3AD9, 0xC209_316D, 0xF3E1_2BF0, 0x5596_2044, 0x9031_1ECA, 0x3646_157E, 0x07AE_0FE3, 0xA1D9_0457,
|
||||
0x5791_74BE, 0xF1E6_7F0A, 0xC00E_6597, 0x6679_6E23, 0xA3DE_50AD, 0x05A9_5B19, 0x3441_4184, 0x9236_4A30,
|
||||
],[
|
||||
0x0000_0000, 0xCCAA_009E, 0x4225_077D, 0x8E8F_07E3, 0x844A_0EFA, 0x48E0_0E64, 0xC66F_0987, 0x0AC5_0919,
|
||||
0xD3E5_1BB5, 0x1F4F_1B2B, 0x91C0_1CC8, 0x5D6A_1C56, 0x57AF_154F, 0x9B05_15D1, 0x158A_1232, 0xD920_12AC,
|
||||
0x7CBB_312B, 0xB011_31B5, 0x3E9E_3656, 0xF234_36C8, 0xF8F1_3FD1, 0x345B_3F4F, 0xBAD4_38AC, 0x767E_3832,
|
||||
0xAF5E_2A9E, 0x63F4_2A00, 0xED7B_2DE3, 0x21D1_2D7D, 0x2B14_2464, 0xE7BE_24FA, 0x6931_2319, 0xA59B_2387,
|
||||
0xF976_6256, 0x35DC_62C8, 0xBB53_652B, 0x77F9_65B5, 0x7D3C_6CAC, 0xB196_6C32, 0x3F19_6BD1, 0xF3B3_6B4F,
|
||||
0x2A93_79E3, 0xE639_797D, 0x68B6_7E9E, 0xA41C_7E00, 0xAED9_7719, 0x6273_7787, 0xECFC_7064, 0x2056_70FA,
|
||||
0x85CD_537D, 0x4967_53E3, 0xC7E8_5400, 0x0B42_549E, 0x0187_5D87, 0xCD2D_5D19, 0x43A2_5AFA, 0x8F08_5A64,
|
||||
0x5628_48C8, 0x9A82_4856, 0x140D_4FB5, 0xD8A7_4F2B, 0xD262_4632, 0x1EC8_46AC, 0x9047_414F, 0x5CED_41D1,
|
||||
0x299D_C2ED, 0xE537_C273, 0x6BB8_C590, 0xA712_C50E, 0xADD7_CC17, 0x617D_CC89, 0xEFF2_CB6A, 0x2358_CBF4,
|
||||
0xFA78_D958, 0x36D2_D9C6, 0xB85D_DE25, 0x74F7_DEBB, 0x7E32_D7A2, 0xB298_D73C, 0x3C17_D0DF, 0xF0BD_D041,
|
||||
0x5526_F3C6, 0x998C_F358, 0x1703_F4BB, 0xDBA9_F425, 0xD16C_FD3C, 0x1DC6_FDA2, 0x9349_FA41, 0x5FE3_FADF,
|
||||
0x86C3_E873, 0x4A69_E8ED, 0xC4E6_EF0E, 0x084C_EF90, 0x0289_E689, 0xCE23_E617, 0x40AC_E1F4, 0x8C06_E16A,
|
||||
0xD0EB_A0BB, 0x1C41_A025, 0x92CE_A7C6, 0x5E64_A758, 0x54A1_AE41, 0x980B_AEDF, 0x1684_A93C, 0xDA2E_A9A2,
|
||||
0x030E_BB0E, 0xCFA4_BB90, 0x412B_BC73, 0x8D81_BCED, 0x8744_B5F4, 0x4BEE_B56A, 0xC561_B289, 0x09CB_B217,
|
||||
0xAC50_9190, 0x60FA_910E, 0xEE75_96ED, 0x22DF_9673, 0x281A_9F6A, 0xE4B0_9FF4, 0x6A3F_9817, 0xA695_9889,
|
||||
0x7FB5_8A25, 0xB31F_8ABB, 0x3D90_8D58, 0xF13A_8DC6, 0xFBFF_84DF, 0x3755_8441, 0xB9DA_83A2, 0x7570_833C,
|
||||
0x533B_85DA, 0x9F91_8544, 0x111E_82A7, 0xDDB4_8239, 0xD771_8B20, 0x1BDB_8BBE, 0x9554_8C5D, 0x59FE_8CC3,
|
||||
0x80DE_9E6F, 0x4C74_9EF1, 0xC2FB_9912, 0x0E51_998C, 0x0494_9095, 0xC83E_900B, 0x46B1_97E8, 0x8A1B_9776,
|
||||
0x2F80_B4F1, 0xE32A_B46F, 0x6DA5_B38C, 0xA10F_B312, 0xABCA_BA0B, 0x6760_BA95, 0xE9EF_BD76, 0x2545_BDE8,
|
||||
0xFC65_AF44, 0x30CF_AFDA, 0xBE40_A839, 0x72EA_A8A7, 0x782F_A1BE, 0xB485_A120, 0x3A0A_A6C3, 0xF6A0_A65D,
|
||||
0xAA4D_E78C, 0x66E7_E712, 0xE868_E0F1, 0x24C2_E06F, 0x2E07_E976, 0xE2AD_E9E8, 0x6C22_EE0B, 0xA088_EE95,
|
||||
0x79A8_FC39, 0xB502_FCA7, 0x3B8D_FB44, 0xF727_FBDA, 0xFDE2_F2C3, 0x3148_F25D, 0xBFC7_F5BE, 0x736D_F520,
|
||||
0xD6F6_D6A7, 0x1A5C_D639, 0x94D3_D1DA, 0x5879_D144, 0x52BC_D85D, 0x9E16_D8C3, 0x1099_DF20, 0xDC33_DFBE,
|
||||
0x0513_CD12, 0xC9B9_CD8C, 0x4736_CA6F, 0x8B9C_CAF1, 0x8159_C3E8, 0x4DF3_C376, 0xC37C_C495, 0x0FD6_C40B,
|
||||
0x7AA6_4737, 0xB60C_47A9, 0x3883_404A, 0xF429_40D4, 0xFEEC_49CD, 0x3246_4953, 0xBCC9_4EB0, 0x7063_4E2E,
|
||||
0xA943_5C82, 0x65E9_5C1C, 0xEB66_5BFF, 0x27CC_5B61, 0x2D09_5278, 0xE1A3_52E6, 0x6F2C_5505, 0xA386_559B,
|
||||
0x061D_761C, 0xCAB7_7682, 0x4438_7161, 0x8892_71FF, 0x8257_78E6, 0x4EFD_7878, 0xC072_7F9B, 0x0CD8_7F05,
|
||||
0xD5F8_6DA9, 0x1952_6D37, 0x97DD_6AD4, 0x5B77_6A4A, 0x51B2_6353, 0x9D18_63CD, 0x1397_642E, 0xDF3D_64B0,
|
||||
0x83D0_2561, 0x4F7A_25FF, 0xC1F5_221C, 0x0D5F_2282, 0x079A_2B9B, 0xCB30_2B05, 0x45BF_2CE6, 0x8915_2C78,
|
||||
0x5035_3ED4, 0x9C9F_3E4A, 0x1210_39A9, 0xDEBA_3937, 0xD47F_302E, 0x18D5_30B0, 0x965A_3753, 0x5AF0_37CD,
|
||||
0xFF6B_144A, 0x33C1_14D4, 0xBD4E_1337, 0x71E4_13A9, 0x7B21_1AB0, 0xB78B_1A2E, 0x3904_1DCD, 0xF5AE_1D53,
|
||||
0x2C8E_0FFF, 0xE024_0F61, 0x6EAB_0882, 0xA201_081C, 0xA8C4_0105, 0x646E_019B, 0xEAE1_0678, 0x264B_06E6,
|
||||
0x0000_0000, 0xCCAA_009E, 0x4225_077D, 0x8E8F_07E3, 0x844A_0EFA, 0x48E0_0E64, 0xC66F_0987, 0x0AC5_0919,
|
||||
0xD3E5_1BB5, 0x1F4F_1B2B, 0x91C0_1CC8, 0x5D6A_1C56, 0x57AF_154F, 0x9B05_15D1, 0x158A_1232, 0xD920_12AC,
|
||||
0x7CBB_312B, 0xB011_31B5, 0x3E9E_3656, 0xF234_36C8, 0xF8F1_3FD1, 0x345B_3F4F, 0xBAD4_38AC, 0x767E_3832,
|
||||
0xAF5E_2A9E, 0x63F4_2A00, 0xED7B_2DE3, 0x21D1_2D7D, 0x2B14_2464, 0xE7BE_24FA, 0x6931_2319, 0xA59B_2387,
|
||||
0xF976_6256, 0x35DC_62C8, 0xBB53_652B, 0x77F9_65B5, 0x7D3C_6CAC, 0xB196_6C32, 0x3F19_6BD1, 0xF3B3_6B4F,
|
||||
0x2A93_79E3, 0xE639_797D, 0x68B6_7E9E, 0xA41C_7E00, 0xAED9_7719, 0x6273_7787, 0xECFC_7064, 0x2056_70FA,
|
||||
0x85CD_537D, 0x4967_53E3, 0xC7E8_5400, 0x0B42_549E, 0x0187_5D87, 0xCD2D_5D19, 0x43A2_5AFA, 0x8F08_5A64,
|
||||
0x5628_48C8, 0x9A82_4856, 0x140D_4FB5, 0xD8A7_4F2B, 0xD262_4632, 0x1EC8_46AC, 0x9047_414F, 0x5CED_41D1,
|
||||
0x299D_C2ED, 0xE537_C273, 0x6BB8_C590, 0xA712_C50E, 0xADD7_CC17, 0x617D_CC89, 0xEFF2_CB6A, 0x2358_CBF4,
|
||||
0xFA78_D958, 0x36D2_D9C6, 0xB85D_DE25, 0x74F7_DEBB, 0x7E32_D7A2, 0xB298_D73C, 0x3C17_D0DF, 0xF0BD_D041,
|
||||
0x5526_F3C6, 0x998C_F358, 0x1703_F4BB, 0xDBA9_F425, 0xD16C_FD3C, 0x1DC6_FDA2, 0x9349_FA41, 0x5FE3_FADF,
|
||||
0x86C3_E873, 0x4A69_E8ED, 0xC4E6_EF0E, 0x084C_EF90, 0x0289_E689, 0xCE23_E617, 0x40AC_E1F4, 0x8C06_E16A,
|
||||
0xD0EB_A0BB, 0x1C41_A025, 0x92CE_A7C6, 0x5E64_A758, 0x54A1_AE41, 0x980B_AEDF, 0x1684_A93C, 0xDA2E_A9A2,
|
||||
0x030E_BB0E, 0xCFA4_BB90, 0x412B_BC73, 0x8D81_BCED, 0x8744_B5F4, 0x4BEE_B56A, 0xC561_B289, 0x09CB_B217,
|
||||
0xAC50_9190, 0x60FA_910E, 0xEE75_96ED, 0x22DF_9673, 0x281A_9F6A, 0xE4B0_9FF4, 0x6A3F_9817, 0xA695_9889,
|
||||
0x7FB5_8A25, 0xB31F_8ABB, 0x3D90_8D58, 0xF13A_8DC6, 0xFBFF_84DF, 0x3755_8441, 0xB9DA_83A2, 0x7570_833C,
|
||||
0x533B_85DA, 0x9F91_8544, 0x111E_82A7, 0xDDB4_8239, 0xD771_8B20, 0x1BDB_8BBE, 0x9554_8C5D, 0x59FE_8CC3,
|
||||
0x80DE_9E6F, 0x4C74_9EF1, 0xC2FB_9912, 0x0E51_998C, 0x0494_9095, 0xC83E_900B, 0x46B1_97E8, 0x8A1B_9776,
|
||||
0x2F80_B4F1, 0xE32A_B46F, 0x6DA5_B38C, 0xA10F_B312, 0xABCA_BA0B, 0x6760_BA95, 0xE9EF_BD76, 0x2545_BDE8,
|
||||
0xFC65_AF44, 0x30CF_AFDA, 0xBE40_A839, 0x72EA_A8A7, 0x782F_A1BE, 0xB485_A120, 0x3A0A_A6C3, 0xF6A0_A65D,
|
||||
0xAA4D_E78C, 0x66E7_E712, 0xE868_E0F1, 0x24C2_E06F, 0x2E07_E976, 0xE2AD_E9E8, 0x6C22_EE0B, 0xA088_EE95,
|
||||
0x79A8_FC39, 0xB502_FCA7, 0x3B8D_FB44, 0xF727_FBDA, 0xFDE2_F2C3, 0x3148_F25D, 0xBFC7_F5BE, 0x736D_F520,
|
||||
0xD6F6_D6A7, 0x1A5C_D639, 0x94D3_D1DA, 0x5879_D144, 0x52BC_D85D, 0x9E16_D8C3, 0x1099_DF20, 0xDC33_DFBE,
|
||||
0x0513_CD12, 0xC9B9_CD8C, 0x4736_CA6F, 0x8B9C_CAF1, 0x8159_C3E8, 0x4DF3_C376, 0xC37C_C495, 0x0FD6_C40B,
|
||||
0x7AA6_4737, 0xB60C_47A9, 0x3883_404A, 0xF429_40D4, 0xFEEC_49CD, 0x3246_4953, 0xBCC9_4EB0, 0x7063_4E2E,
|
||||
0xA943_5C82, 0x65E9_5C1C, 0xEB66_5BFF, 0x27CC_5B61, 0x2D09_5278, 0xE1A3_52E6, 0x6F2C_5505, 0xA386_559B,
|
||||
0x061D_761C, 0xCAB7_7682, 0x4438_7161, 0x8892_71FF, 0x8257_78E6, 0x4EFD_7878, 0xC072_7F9B, 0x0CD8_7F05,
|
||||
0xD5F8_6DA9, 0x1952_6D37, 0x97DD_6AD4, 0x5B77_6A4A, 0x51B2_6353, 0x9D18_63CD, 0x1397_642E, 0xDF3D_64B0,
|
||||
0x83D0_2561, 0x4F7A_25FF, 0xC1F5_221C, 0x0D5F_2282, 0x079A_2B9B, 0xCB30_2B05, 0x45BF_2CE6, 0x8915_2C78,
|
||||
0x5035_3ED4, 0x9C9F_3E4A, 0x1210_39A9, 0xDEBA_3937, 0xD47F_302E, 0x18D5_30B0, 0x965A_3753, 0x5AF0_37CD,
|
||||
0xFF6B_144A, 0x33C1_14D4, 0xBD4E_1337, 0x71E4_13A9, 0x7B21_1AB0, 0xB78B_1A2E, 0x3904_1DCD, 0xF5AE_1D53,
|
||||
0x2C8E_0FFF, 0xE024_0F61, 0x6EAB_0882, 0xA201_081C, 0xA8C4_0105, 0x646E_019B, 0xEAE1_0678, 0x264B_06E6,
|
||||
],[
|
||||
0x0000_0000, 0x177B_1443, 0x2EF6_2886, 0x398D_3CC5, 0x5DEC_510C, 0x4A97_454F, 0x731A_798A, 0x6461_6DC9,
|
||||
0xBBD8_A218, 0xACA3_B65B, 0x952E_8A9E, 0x8255_9EDD, 0xE634_F314, 0xF14F_E757, 0xC8C2_DB92, 0xDFB9_CFD1,
|
||||
0xACC0_4271, 0xBBBB_5632, 0x8236_6AF7, 0x954D_7EB4, 0xF12C_137D, 0xE657_073E, 0xDFDA_3BFB, 0xC8A1_2FB8,
|
||||
0x1718_E069, 0x0063_F42A, 0x39EE_C8EF, 0x2E95_DCAC, 0x4AF4_B165, 0x5D8F_A526, 0x6402_99E3, 0x7379_8DA0,
|
||||
0x82F1_82A3, 0x958A_96E0, 0xAC07_AA25, 0xBB7C_BE66, 0xDF1D_D3AF, 0xC866_C7EC, 0xF1EB_FB29, 0xE690_EF6A,
|
||||
0x3929_20BB, 0x2E52_34F8, 0x17DF_083D, 0x00A4_1C7E, 0x64C5_71B7, 0x73BE_65F4, 0x4A33_5931, 0x5D48_4D72,
|
||||
0x2E31_C0D2, 0x394A_D491, 0x00C7_E854, 0x17BC_FC17, 0x73DD_91DE, 0x64A6_859D, 0x5D2B_B958, 0x4A50_AD1B,
|
||||
0x95E9_62CA, 0x8292_7689, 0xBB1F_4A4C, 0xAC64_5E0F, 0xC805_33C6, 0xDF7E_2785, 0xE6F3_1B40, 0xF188_0F03,
|
||||
0xDE92_0307, 0xC9E9_1744, 0xF064_2B81, 0xE71F_3FC2, 0x837E_520B, 0x9405_4648, 0xAD88_7A8D, 0xBAF3_6ECE,
|
||||
0x654A_A11F, 0x7231_B55C, 0x4BBC_8999, 0x5CC7_9DDA, 0x38A6_F013, 0x2FDD_E450, 0x1650_D895, 0x012B_CCD6,
|
||||
0x7252_4176, 0x6529_5535, 0x5CA4_69F0, 0x4BDF_7DB3, 0x2FBE_107A, 0x38C5_0439, 0x0148_38FC, 0x1633_2CBF,
|
||||
0xC98A_E36E, 0xDEF1_F72D, 0xE77C_CBE8, 0xF007_DFAB, 0x9466_B262, 0x831D_A621, 0xBA90_9AE4, 0xADEB_8EA7,
|
||||
0x5C63_81A4, 0x4B18_95E7, 0x7295_A922, 0x65EE_BD61, 0x018F_D0A8, 0x16F4_C4EB, 0x2F79_F82E, 0x3802_EC6D,
|
||||
0xE7BB_23BC, 0xF0C0_37FF, 0xC94D_0B3A, 0xDE36_1F79, 0xBA57_72B0, 0xAD2C_66F3, 0x94A1_5A36, 0x83DA_4E75,
|
||||
0xF0A3_C3D5, 0xE7D8_D796, 0xDE55_EB53, 0xC92E_FF10, 0xAD4F_92D9, 0xBA34_869A, 0x83B9_BA5F, 0x94C2_AE1C,
|
||||
0x4B7B_61CD, 0x5C00_758E, 0x658D_494B, 0x72F6_5D08, 0x1697_30C1, 0x01EC_2482, 0x3861_1847, 0x2F1A_0C04,
|
||||
0x6655_004F, 0x712E_140C, 0x48A3_28C9, 0x5FD8_3C8A, 0x3BB9_5143, 0x2CC2_4500, 0x154F_79C5, 0x0234_6D86,
|
||||
0xDD8D_A257, 0xCAF6_B614, 0xF37B_8AD1, 0xE400_9E92, 0x8061_F35B, 0x971A_E718, 0xAE97_DBDD, 0xB9EC_CF9E,
|
||||
0xCA95_423E, 0xDDEE_567D, 0xE463_6AB8, 0xF318_7EFB, 0x9779_1332, 0x8002_0771, 0xB98F_3BB4, 0xAEF4_2FF7,
|
||||
0x714D_E026, 0x6636_F465, 0x5FBB_C8A0, 0x48C0_DCE3, 0x2CA1_B12A, 0x3BDA_A569, 0x0257_99AC, 0x152C_8DEF,
|
||||
0xE4A4_82EC, 0xF3DF_96AF, 0xCA52_AA6A, 0xDD29_BE29, 0xB948_D3E0, 0xAE33_C7A3, 0x97BE_FB66, 0x80C5_EF25,
|
||||
0x5F7C_20F4, 0x4807_34B7, 0x718A_0872, 0x66F1_1C31, 0x0290_71F8, 0x15EB_65BB, 0x2C66_597E, 0x3B1D_4D3D,
|
||||
0x4864_C09D, 0x5F1F_D4DE, 0x6692_E81B, 0x71E9_FC58, 0x1588_9191, 0x02F3_85D2, 0x3B7E_B917, 0x2C05_AD54,
|
||||
0xF3BC_6285, 0xE4C7_76C6, 0xDD4A_4A03, 0xCA31_5E40, 0xAE50_3389, 0xB92B_27CA, 0x80A6_1B0F, 0x97DD_0F4C,
|
||||
0xB8C7_0348, 0xAFBC_170B, 0x9631_2BCE, 0x814A_3F8D, 0xE52B_5244, 0xF250_4607, 0xCBDD_7AC2, 0xDCA6_6E81,
|
||||
0x031F_A150, 0x1464_B513, 0x2DE9_89D6, 0x3A92_9D95, 0x5EF3_F05C, 0x4988_E41F, 0x7005_D8DA, 0x677E_CC99,
|
||||
0x1407_4139, 0x037C_557A, 0x3AF1_69BF, 0x2D8A_7DFC, 0x49EB_1035, 0x5E90_0476, 0x671D_38B3, 0x7066_2CF0,
|
||||
0xAFDF_E321, 0xB8A4_F762, 0x8129_CBA7, 0x9652_DFE4, 0xF233_B22D, 0xE548_A66E, 0xDCC5_9AAB, 0xCBBE_8EE8,
|
||||
0x3A36_81EB, 0x2D4D_95A8, 0x14C0_A96D, 0x03BB_BD2E, 0x67DA_D0E7, 0x70A1_C4A4, 0x492C_F861, 0x5E57_EC22,
|
||||
0x81EE_23F3, 0x9695_37B0, 0xAF18_0B75, 0xB863_1F36, 0xDC02_72FF, 0xCB79_66BC, 0xF2F4_5A79, 0xE58F_4E3A,
|
||||
0x96F6_C39A, 0x818D_D7D9, 0xB800_EB1C, 0xAF7B_FF5F, 0xCB1A_9296, 0xDC61_86D5, 0xE5EC_BA10, 0xF297_AE53,
|
||||
0x2D2E_6182, 0x3A55_75C1, 0x03D8_4904, 0x14A3_5D47, 0x70C2_308E, 0x67B9_24CD, 0x5E34_1808, 0x494F_0C4B,
|
||||
0x0000_0000, 0x177B_1443, 0x2EF6_2886, 0x398D_3CC5, 0x5DEC_510C, 0x4A97_454F, 0x731A_798A, 0x6461_6DC9,
|
||||
0xBBD8_A218, 0xACA3_B65B, 0x952E_8A9E, 0x8255_9EDD, 0xE634_F314, 0xF14F_E757, 0xC8C2_DB92, 0xDFB9_CFD1,
|
||||
0xACC0_4271, 0xBBBB_5632, 0x8236_6AF7, 0x954D_7EB4, 0xF12C_137D, 0xE657_073E, 0xDFDA_3BFB, 0xC8A1_2FB8,
|
||||
0x1718_E069, 0x0063_F42A, 0x39EE_C8EF, 0x2E95_DCAC, 0x4AF4_B165, 0x5D8F_A526, 0x6402_99E3, 0x7379_8DA0,
|
||||
0x82F1_82A3, 0x958A_96E0, 0xAC07_AA25, 0xBB7C_BE66, 0xDF1D_D3AF, 0xC866_C7EC, 0xF1EB_FB29, 0xE690_EF6A,
|
||||
0x3929_20BB, 0x2E52_34F8, 0x17DF_083D, 0x00A4_1C7E, 0x64C5_71B7, 0x73BE_65F4, 0x4A33_5931, 0x5D48_4D72,
|
||||
0x2E31_C0D2, 0x394A_D491, 0x00C7_E854, 0x17BC_FC17, 0x73DD_91DE, 0x64A6_859D, 0x5D2B_B958, 0x4A50_AD1B,
|
||||
0x95E9_62CA, 0x8292_7689, 0xBB1F_4A4C, 0xAC64_5E0F, 0xC805_33C6, 0xDF7E_2785, 0xE6F3_1B40, 0xF188_0F03,
|
||||
0xDE92_0307, 0xC9E9_1744, 0xF064_2B81, 0xE71F_3FC2, 0x837E_520B, 0x9405_4648, 0xAD88_7A8D, 0xBAF3_6ECE,
|
||||
0x654A_A11F, 0x7231_B55C, 0x4BBC_8999, 0x5CC7_9DDA, 0x38A6_F013, 0x2FDD_E450, 0x1650_D895, 0x012B_CCD6,
|
||||
0x7252_4176, 0x6529_5535, 0x5CA4_69F0, 0x4BDF_7DB3, 0x2FBE_107A, 0x38C5_0439, 0x0148_38FC, 0x1633_2CBF,
|
||||
0xC98A_E36E, 0xDEF1_F72D, 0xE77C_CBE8, 0xF007_DFAB, 0x9466_B262, 0x831D_A621, 0xBA90_9AE4, 0xADEB_8EA7,
|
||||
0x5C63_81A4, 0x4B18_95E7, 0x7295_A922, 0x65EE_BD61, 0x018F_D0A8, 0x16F4_C4EB, 0x2F79_F82E, 0x3802_EC6D,
|
||||
0xE7BB_23BC, 0xF0C0_37FF, 0xC94D_0B3A, 0xDE36_1F79, 0xBA57_72B0, 0xAD2C_66F3, 0x94A1_5A36, 0x83DA_4E75,
|
||||
0xF0A3_C3D5, 0xE7D8_D796, 0xDE55_EB53, 0xC92E_FF10, 0xAD4F_92D9, 0xBA34_869A, 0x83B9_BA5F, 0x94C2_AE1C,
|
||||
0x4B7B_61CD, 0x5C00_758E, 0x658D_494B, 0x72F6_5D08, 0x1697_30C1, 0x01EC_2482, 0x3861_1847, 0x2F1A_0C04,
|
||||
0x6655_004F, 0x712E_140C, 0x48A3_28C9, 0x5FD8_3C8A, 0x3BB9_5143, 0x2CC2_4500, 0x154F_79C5, 0x0234_6D86,
|
||||
0xDD8D_A257, 0xCAF6_B614, 0xF37B_8AD1, 0xE400_9E92, 0x8061_F35B, 0x971A_E718, 0xAE97_DBDD, 0xB9EC_CF9E,
|
||||
0xCA95_423E, 0xDDEE_567D, 0xE463_6AB8, 0xF318_7EFB, 0x9779_1332, 0x8002_0771, 0xB98F_3BB4, 0xAEF4_2FF7,
|
||||
0x714D_E026, 0x6636_F465, 0x5FBB_C8A0, 0x48C0_DCE3, 0x2CA1_B12A, 0x3BDA_A569, 0x0257_99AC, 0x152C_8DEF,
|
||||
0xE4A4_82EC, 0xF3DF_96AF, 0xCA52_AA6A, 0xDD29_BE29, 0xB948_D3E0, 0xAE33_C7A3, 0x97BE_FB66, 0x80C5_EF25,
|
||||
0x5F7C_20F4, 0x4807_34B7, 0x718A_0872, 0x66F1_1C31, 0x0290_71F8, 0x15EB_65BB, 0x2C66_597E, 0x3B1D_4D3D,
|
||||
0x4864_C09D, 0x5F1F_D4DE, 0x6692_E81B, 0x71E9_FC58, 0x1588_9191, 0x02F3_85D2, 0x3B7E_B917, 0x2C05_AD54,
|
||||
0xF3BC_6285, 0xE4C7_76C6, 0xDD4A_4A03, 0xCA31_5E40, 0xAE50_3389, 0xB92B_27CA, 0x80A6_1B0F, 0x97DD_0F4C,
|
||||
0xB8C7_0348, 0xAFBC_170B, 0x9631_2BCE, 0x814A_3F8D, 0xE52B_5244, 0xF250_4607, 0xCBDD_7AC2, 0xDCA6_6E81,
|
||||
0x031F_A150, 0x1464_B513, 0x2DE9_89D6, 0x3A92_9D95, 0x5EF3_F05C, 0x4988_E41F, 0x7005_D8DA, 0x677E_CC99,
|
||||
0x1407_4139, 0x037C_557A, 0x3AF1_69BF, 0x2D8A_7DFC, 0x49EB_1035, 0x5E90_0476, 0x671D_38B3, 0x7066_2CF0,
|
||||
0xAFDF_E321, 0xB8A4_F762, 0x8129_CBA7, 0x9652_DFE4, 0xF233_B22D, 0xE548_A66E, 0xDCC5_9AAB, 0xCBBE_8EE8,
|
||||
0x3A36_81EB, 0x2D4D_95A8, 0x14C0_A96D, 0x03BB_BD2E, 0x67DA_D0E7, 0x70A1_C4A4, 0x492C_F861, 0x5E57_EC22,
|
||||
0x81EE_23F3, 0x9695_37B0, 0xAF18_0B75, 0xB863_1F36, 0xDC02_72FF, 0xCB79_66BC, 0xF2F4_5A79, 0xE58F_4E3A,
|
||||
0x96F6_C39A, 0x818D_D7D9, 0xB800_EB1C, 0xAF7B_FF5F, 0xCB1A_9296, 0xDC61_86D5, 0xE5EC_BA10, 0xF297_AE53,
|
||||
0x2D2E_6182, 0x3A55_75C1, 0x03D8_4904, 0x14A3_5D47, 0x70C2_308E, 0x67B9_24CD, 0x5E34_1808, 0x494F_0C4B,
|
||||
],[
|
||||
0x0000_0000, 0xEFC2_6B3E, 0x04F5_D03D, 0xEB37_BB03, 0x09EB_A07A, 0xE629_CB44, 0x0D1E_7047, 0xE2DC_1B79,
|
||||
0x13D7_40F4, 0xFC15_2BCA, 0x1722_90C9, 0xF8E0_FBF7, 0x1A3C_E08E, 0xF5FE_8BB0, 0x1EC9_30B3, 0xF10B_5B8D,
|
||||
0x27AE_81E8, 0xC86C_EAD6, 0x235B_51D5, 0xCC99_3AEB, 0x2E45_2192, 0xC187_4AAC, 0x2AB0_F1AF, 0xC572_9A91,
|
||||
0x3479_C11C, 0xDBBB_AA22, 0x308C_1121, 0xDF4E_7A1F, 0x3D92_6166, 0xD250_0A58, 0x3967_B15B, 0xD6A5_DA65,
|
||||
0x4F5D_03D0, 0xA09F_68EE, 0x4BA8_D3ED, 0xA46A_B8D3, 0x46B6_A3AA, 0xA974_C894, 0x4243_7397, 0xAD81_18A9,
|
||||
0x5C8A_4324, 0xB348_281A, 0x587F_9319, 0xB7BD_F827, 0x5561_E35E, 0xBAA3_8860, 0x5194_3363, 0xBE56_585D,
|
||||
0x68F3_8238, 0x8731_E906, 0x6C06_5205, 0x83C4_393B, 0x6118_2242, 0x8EDA_497C, 0x65ED_F27F, 0x8A2F_9941,
|
||||
0x7B24_C2CC, 0x94E6_A9F2, 0x7FD1_12F1, 0x9013_79CF, 0x72CF_62B6, 0x9D0D_0988, 0x763A_B28B, 0x99F8_D9B5,
|
||||
0x9EBA_07A0, 0x7178_6C9E, 0x9A4F_D79D, 0x758D_BCA3, 0x9751_A7DA, 0x7893_CCE4, 0x93A4_77E7, 0x7C66_1CD9,
|
||||
0x8D6D_4754, 0x62AF_2C6A, 0x8998_9769, 0x665A_FC57, 0x8486_E72E, 0x6B44_8C10, 0x8073_3713, 0x6FB1_5C2D,
|
||||
0xB914_8648, 0x56D6_ED76, 0xBDE1_5675, 0x5223_3D4B, 0xB0FF_2632, 0x5F3D_4D0C, 0xB40A_F60F, 0x5BC8_9D31,
|
||||
0xAAC3_C6BC, 0x4501_AD82, 0xAE36_1681, 0x41F4_7DBF, 0xA328_66C6, 0x4CEA_0DF8, 0xA7DD_B6FB, 0x481F_DDC5,
|
||||
0xD1E7_0470, 0x3E25_6F4E, 0xD512_D44D, 0x3AD0_BF73, 0xD80C_A40A, 0x37CE_CF34, 0xDCF9_7437, 0x333B_1F09,
|
||||
0xC230_4484, 0x2DF2_2FBA, 0xC6C5_94B9, 0x2907_FF87, 0xCBDB_E4FE, 0x2419_8FC0, 0xCF2E_34C3, 0x20EC_5FFD,
|
||||
0xF649_8598, 0x198B_EEA6, 0xF2BC_55A5, 0x1D7E_3E9B, 0xFFA2_25E2, 0x1060_4EDC, 0xFB57_F5DF, 0x1495_9EE1,
|
||||
0xE59E_C56C, 0x0A5C_AE52, 0xE16B_1551, 0x0EA9_7E6F, 0xEC75_6516, 0x03B7_0E28, 0xE880_B52B, 0x0742_DE15,
|
||||
0xE605_0901, 0x09C7_623F, 0xE2F0_D93C, 0x0D32_B202, 0xEFEE_A97B, 0x002C_C245, 0xEB1B_7946, 0x04D9_1278,
|
||||
0xF5D2_49F5, 0x1A10_22CB, 0xF127_99C8, 0x1EE5_F2F6, 0xFC39_E98F, 0x13FB_82B1, 0xF8CC_39B2, 0x170E_528C,
|
||||
0xC1AB_88E9, 0x2E69_E3D7, 0xC55E_58D4, 0x2A9C_33EA, 0xC840_2893, 0x2782_43AD, 0xCCB5_F8AE, 0x2377_9390,
|
||||
0xD27C_C81D, 0x3DBE_A323, 0xD689_1820, 0x394B_731E, 0xDB97_6867, 0x3455_0359, 0xDF62_B85A, 0x30A0_D364,
|
||||
0xA958_0AD1, 0x469A_61EF, 0xADAD_DAEC, 0x426F_B1D2, 0xA0B3_AAAB, 0x4F71_C195, 0xA446_7A96, 0x4B84_11A8,
|
||||
0xBA8F_4A25, 0x554D_211B, 0xBE7A_9A18, 0x51B8_F126, 0xB364_EA5F, 0x5CA6_8161, 0xB791_3A62, 0x5853_515C,
|
||||
0x8EF6_8B39, 0x6134_E007, 0x8A03_5B04, 0x65C1_303A, 0x871D_2B43, 0x68DF_407D, 0x83E8_FB7E, 0x6C2A_9040,
|
||||
0x9D21_CBCD, 0x72E3_A0F3, 0x99D4_1BF0, 0x7616_70CE, 0x94CA_6BB7, 0x7B08_0089, 0x903F_BB8A, 0x7FFD_D0B4,
|
||||
0x78BF_0EA1, 0x977D_659F, 0x7C4A_DE9C, 0x9388_B5A2, 0x7154_AEDB, 0x9E96_C5E5, 0x75A1_7EE6, 0x9A63_15D8,
|
||||
0x6B68_4E55, 0x84AA_256B, 0x6F9D_9E68, 0x805F_F556, 0x6283_EE2F, 0x8D41_8511, 0x6676_3E12, 0x89B4_552C,
|
||||
0x5F11_8F49, 0xB0D3_E477, 0x5BE4_5F74, 0xB426_344A, 0x56FA_2F33, 0xB938_440D, 0x520F_FF0E, 0xBDCD_9430,
|
||||
0x4CC6_CFBD, 0xA304_A483, 0x4833_1F80, 0xA7F1_74BE, 0x452D_6FC7, 0xAAEF_04F9, 0x41D8_BFFA, 0xAE1A_D4C4,
|
||||
0x37E2_0D71, 0xD820_664F, 0x3317_DD4C, 0xDCD5_B672, 0x3E09_AD0B, 0xD1CB_C635, 0x3AFC_7D36, 0xD53E_1608,
|
||||
0x2435_4D85, 0xCBF7_26BB, 0x20C0_9DB8, 0xCF02_F686, 0x2DDE_EDFF, 0xC21C_86C1, 0x292B_3DC2, 0xC6E9_56FC,
|
||||
0x104C_8C99, 0xFF8E_E7A7, 0x14B9_5CA4, 0xFB7B_379A, 0x19A7_2CE3, 0xF665_47DD, 0x1D52_FCDE, 0xF290_97E0,
|
||||
0x039B_CC6D, 0xEC59_A753, 0x076E_1C50, 0xE8AC_776E, 0x0A70_6C17, 0xE5B2_0729, 0x0E85_BC2A, 0xE147_D714,
|
||||
0x0000_0000, 0xEFC2_6B3E, 0x04F5_D03D, 0xEB37_BB03, 0x09EB_A07A, 0xE629_CB44, 0x0D1E_7047, 0xE2DC_1B79,
|
||||
0x13D7_40F4, 0xFC15_2BCA, 0x1722_90C9, 0xF8E0_FBF7, 0x1A3C_E08E, 0xF5FE_8BB0, 0x1EC9_30B3, 0xF10B_5B8D,
|
||||
0x27AE_81E8, 0xC86C_EAD6, 0x235B_51D5, 0xCC99_3AEB, 0x2E45_2192, 0xC187_4AAC, 0x2AB0_F1AF, 0xC572_9A91,
|
||||
0x3479_C11C, 0xDBBB_AA22, 0x308C_1121, 0xDF4E_7A1F, 0x3D92_6166, 0xD250_0A58, 0x3967_B15B, 0xD6A5_DA65,
|
||||
0x4F5D_03D0, 0xA09F_68EE, 0x4BA8_D3ED, 0xA46A_B8D3, 0x46B6_A3AA, 0xA974_C894, 0x4243_7397, 0xAD81_18A9,
|
||||
0x5C8A_4324, 0xB348_281A, 0x587F_9319, 0xB7BD_F827, 0x5561_E35E, 0xBAA3_8860, 0x5194_3363, 0xBE56_585D,
|
||||
0x68F3_8238, 0x8731_E906, 0x6C06_5205, 0x83C4_393B, 0x6118_2242, 0x8EDA_497C, 0x65ED_F27F, 0x8A2F_9941,
|
||||
0x7B24_C2CC, 0x94E6_A9F2, 0x7FD1_12F1, 0x9013_79CF, 0x72CF_62B6, 0x9D0D_0988, 0x763A_B28B, 0x99F8_D9B5,
|
||||
0x9EBA_07A0, 0x7178_6C9E, 0x9A4F_D79D, 0x758D_BCA3, 0x9751_A7DA, 0x7893_CCE4, 0x93A4_77E7, 0x7C66_1CD9,
|
||||
0x8D6D_4754, 0x62AF_2C6A, 0x8998_9769, 0x665A_FC57, 0x8486_E72E, 0x6B44_8C10, 0x8073_3713, 0x6FB1_5C2D,
|
||||
0xB914_8648, 0x56D6_ED76, 0xBDE1_5675, 0x5223_3D4B, 0xB0FF_2632, 0x5F3D_4D0C, 0xB40A_F60F, 0x5BC8_9D31,
|
||||
0xAAC3_C6BC, 0x4501_AD82, 0xAE36_1681, 0x41F4_7DBF, 0xA328_66C6, 0x4CEA_0DF8, 0xA7DD_B6FB, 0x481F_DDC5,
|
||||
0xD1E7_0470, 0x3E25_6F4E, 0xD512_D44D, 0x3AD0_BF73, 0xD80C_A40A, 0x37CE_CF34, 0xDCF9_7437, 0x333B_1F09,
|
||||
0xC230_4484, 0x2DF2_2FBA, 0xC6C5_94B9, 0x2907_FF87, 0xCBDB_E4FE, 0x2419_8FC0, 0xCF2E_34C3, 0x20EC_5FFD,
|
||||
0xF649_8598, 0x198B_EEA6, 0xF2BC_55A5, 0x1D7E_3E9B, 0xFFA2_25E2, 0x1060_4EDC, 0xFB57_F5DF, 0x1495_9EE1,
|
||||
0xE59E_C56C, 0x0A5C_AE52, 0xE16B_1551, 0x0EA9_7E6F, 0xEC75_6516, 0x03B7_0E28, 0xE880_B52B, 0x0742_DE15,
|
||||
0xE605_0901, 0x09C7_623F, 0xE2F0_D93C, 0x0D32_B202, 0xEFEE_A97B, 0x002C_C245, 0xEB1B_7946, 0x04D9_1278,
|
||||
0xF5D2_49F5, 0x1A10_22CB, 0xF127_99C8, 0x1EE5_F2F6, 0xFC39_E98F, 0x13FB_82B1, 0xF8CC_39B2, 0x170E_528C,
|
||||
0xC1AB_88E9, 0x2E69_E3D7, 0xC55E_58D4, 0x2A9C_33EA, 0xC840_2893, 0x2782_43AD, 0xCCB5_F8AE, 0x2377_9390,
|
||||
0xD27C_C81D, 0x3DBE_A323, 0xD689_1820, 0x394B_731E, 0xDB97_6867, 0x3455_0359, 0xDF62_B85A, 0x30A0_D364,
|
||||
0xA958_0AD1, 0x469A_61EF, 0xADAD_DAEC, 0x426F_B1D2, 0xA0B3_AAAB, 0x4F71_C195, 0xA446_7A96, 0x4B84_11A8,
|
||||
0xBA8F_4A25, 0x554D_211B, 0xBE7A_9A18, 0x51B8_F126, 0xB364_EA5F, 0x5CA6_8161, 0xB791_3A62, 0x5853_515C,
|
||||
0x8EF6_8B39, 0x6134_E007, 0x8A03_5B04, 0x65C1_303A, 0x871D_2B43, 0x68DF_407D, 0x83E8_FB7E, 0x6C2A_9040,
|
||||
0x9D21_CBCD, 0x72E3_A0F3, 0x99D4_1BF0, 0x7616_70CE, 0x94CA_6BB7, 0x7B08_0089, 0x903F_BB8A, 0x7FFD_D0B4,
|
||||
0x78BF_0EA1, 0x977D_659F, 0x7C4A_DE9C, 0x9388_B5A2, 0x7154_AEDB, 0x9E96_C5E5, 0x75A1_7EE6, 0x9A63_15D8,
|
||||
0x6B68_4E55, 0x84AA_256B, 0x6F9D_9E68, 0x805F_F556, 0x6283_EE2F, 0x8D41_8511, 0x6676_3E12, 0x89B4_552C,
|
||||
0x5F11_8F49, 0xB0D3_E477, 0x5BE4_5F74, 0xB426_344A, 0x56FA_2F33, 0xB938_440D, 0x520F_FF0E, 0xBDCD_9430,
|
||||
0x4CC6_CFBD, 0xA304_A483, 0x4833_1F80, 0xA7F1_74BE, 0x452D_6FC7, 0xAAEF_04F9, 0x41D8_BFFA, 0xAE1A_D4C4,
|
||||
0x37E2_0D71, 0xD820_664F, 0x3317_DD4C, 0xDCD5_B672, 0x3E09_AD0B, 0xD1CB_C635, 0x3AFC_7D36, 0xD53E_1608,
|
||||
0x2435_4D85, 0xCBF7_26BB, 0x20C0_9DB8, 0xCF02_F686, 0x2DDE_EDFF, 0xC21C_86C1, 0x292B_3DC2, 0xC6E9_56FC,
|
||||
0x104C_8C99, 0xFF8E_E7A7, 0x14B9_5CA4, 0xFB7B_379A, 0x19A7_2CE3, 0xF665_47DD, 0x1D52_FCDE, 0xF290_97E0,
|
||||
0x039B_CC6D, 0xEC59_A753, 0x076E_1C50, 0xE8AC_776E, 0x0A70_6C17, 0xE5B2_0729, 0x0E85_BC2A, 0xE147_D714,
|
||||
],[
|
||||
0x0000_0000, 0xC18E_DFC0, 0x586C_B9C1, 0x99E2_6601, 0xB0D9_7382, 0x7157_AC42, 0xE8B5_CA43, 0x293B_1583,
|
||||
0xBAC3_E145, 0x7B4D_3E85, 0xE2AF_5884, 0x2321_8744, 0x0A1A_92C7, 0xCB94_4D07, 0x5276_2B06, 0x93F8_F4C6,
|
||||
0xAEF6_C4CB, 0x6F78_1B0B, 0xF69A_7D0A, 0x3714_A2CA, 0x1E2F_B749, 0xDFA1_6889, 0x4643_0E88, 0x87CD_D148,
|
||||
0x1435_258E, 0xD5BB_FA4E, 0x4C59_9C4F, 0x8DD7_438F, 0xA4EC_560C, 0x6562_89CC, 0xFC80_EFCD, 0x3D0E_300D,
|
||||
0x869C_8FD7, 0x4712_5017, 0xDEF0_3616, 0x1F7E_E9D6, 0x3645_FC55, 0xF7CB_2395, 0x6E29_4594, 0xAFA7_9A54,
|
||||
0x3C5F_6E92, 0xFDD1_B152, 0x6433_D753, 0xA5BD_0893, 0x8C86_1D10, 0x4D08_C2D0, 0xD4EA_A4D1, 0x1564_7B11,
|
||||
0x286A_4B1C, 0xE9E4_94DC, 0x7006_F2DD, 0xB188_2D1D, 0x98B3_389E, 0x593D_E75E, 0xC0DF_815F, 0x0151_5E9F,
|
||||
0x92A9_AA59, 0x5327_7599, 0xCAC5_1398, 0x0B4B_CC58, 0x2270_D9DB, 0xE3FE_061B, 0x7A1C_601A, 0xBB92_BFDA,
|
||||
0xD648_19EF, 0x17C6_C62F, 0x8E24_A02E, 0x4FAA_7FEE, 0x6691_6A6D, 0xA71F_B5AD, 0x3EFD_D3AC, 0xFF73_0C6C,
|
||||
0x6C8B_F8AA, 0xAD05_276A, 0x34E7_416B, 0xF569_9EAB, 0xDC52_8B28, 0x1DDC_54E8, 0x843E_32E9, 0x45B0_ED29,
|
||||
0x78BE_DD24, 0xB930_02E4, 0x20D2_64E5, 0xE15C_BB25, 0xC867_AEA6, 0x09E9_7166, 0x900B_1767, 0x5185_C8A7,
|
||||
0xC27D_3C61, 0x03F3_E3A1, 0x9A11_85A0, 0x5B9F_5A60, 0x72A4_4FE3, 0xB32A_9023, 0x2AC8_F622, 0xEB46_29E2,
|
||||
0x50D4_9638, 0x915A_49F8, 0x08B8_2FF9, 0xC936_F039, 0xE00D_E5BA, 0x2183_3A7A, 0xB861_5C7B, 0x79EF_83BB,
|
||||
0xEA17_777D, 0x2B99_A8BD, 0xB27B_CEBC, 0x73F5_117C, 0x5ACE_04FF, 0x9B40_DB3F, 0x02A2_BD3E, 0xC32C_62FE,
|
||||
0xFE22_52F3, 0x3FAC_8D33, 0xA64E_EB32, 0x67C0_34F2, 0x4EFB_2171, 0x8F75_FEB1, 0x1697_98B0, 0xD719_4770,
|
||||
0x44E1_B3B6, 0x856F_6C76, 0x1C8D_0A77, 0xDD03_D5B7, 0xF438_C034, 0x35B6_1FF4, 0xAC54_79F5, 0x6DDA_A635,
|
||||
0x77E1_359F, 0xB66F_EA5F, 0x2F8D_8C5E, 0xEE03_539E, 0xC738_461D, 0x06B6_99DD, 0x9F54_FFDC, 0x5EDA_201C,
|
||||
0xCD22_D4DA, 0x0CAC_0B1A, 0x954E_6D1B, 0x54C0_B2DB, 0x7DFB_A758, 0xBC75_7898, 0x2597_1E99, 0xE419_C159,
|
||||
0xD917_F154, 0x1899_2E94, 0x817B_4895, 0x40F5_9755, 0x69CE_82D6, 0xA840_5D16, 0x31A2_3B17, 0xF02C_E4D7,
|
||||
0x63D4_1011, 0xA25A_CFD1, 0x3BB8_A9D0, 0xFA36_7610, 0xD30D_6393, 0x1283_BC53, 0x8B61_DA52, 0x4AEF_0592,
|
||||
0xF17D_BA48, 0x30F3_6588, 0xA911_0389, 0x689F_DC49, 0x41A4_C9CA, 0x802A_160A, 0x19C8_700B, 0xD846_AFCB,
|
||||
0x4BBE_5B0D, 0x8A30_84CD, 0x13D2_E2CC, 0xD25C_3D0C, 0xFB67_288F, 0x3AE9_F74F, 0xA30B_914E, 0x6285_4E8E,
|
||||
0x5F8B_7E83, 0x9E05_A143, 0x07E7_C742, 0xC669_1882, 0xEF52_0D01, 0x2EDC_D2C1, 0xB73E_B4C0, 0x76B0_6B00,
|
||||
0xE548_9FC6, 0x24C6_4006, 0xBD24_2607, 0x7CAA_F9C7, 0x5591_EC44, 0x941F_3384, 0x0DFD_5585, 0xCC73_8A45,
|
||||
0xA1A9_2C70, 0x6027_F3B0, 0xF9C5_95B1, 0x384B_4A71, 0x1170_5FF2, 0xD0FE_8032, 0x491C_E633, 0x8892_39F3,
|
||||
0x1B6A_CD35, 0xDAE4_12F5, 0x4306_74F4, 0x8288_AB34, 0xABB3_BEB7, 0x6A3D_6177, 0xF3DF_0776, 0x3251_D8B6,
|
||||
0x0F5F_E8BB, 0xCED1_377B, 0x5733_517A, 0x96BD_8EBA, 0xBF86_9B39, 0x7E08_44F9, 0xE7EA_22F8, 0x2664_FD38,
|
||||
0xB59C_09FE, 0x7412_D63E, 0xEDF0_B03F, 0x2C7E_6FFF, 0x0545_7A7C, 0xC4CB_A5BC, 0x5D29_C3BD, 0x9CA7_1C7D,
|
||||
0x2735_A3A7, 0xE6BB_7C67, 0x7F59_1A66, 0xBED7_C5A6, 0x97EC_D025, 0x5662_0FE5, 0xCF80_69E4, 0x0E0E_B624,
|
||||
0x9DF6_42E2, 0x5C78_9D22, 0xC59A_FB23, 0x0414_24E3, 0x2D2F_3160, 0xECA1_EEA0, 0x7543_88A1, 0xB4CD_5761,
|
||||
0x89C3_676C, 0x484D_B8AC, 0xD1AF_DEAD, 0x1021_016D, 0x391A_14EE, 0xF894_CB2E, 0x6176_AD2F, 0xA0F8_72EF,
|
||||
0x3300_8629, 0xF28E_59E9, 0x6B6C_3FE8, 0xAAE2_E028, 0x83D9_F5AB, 0x4257_2A6B, 0xDBB5_4C6A, 0x1A3B_93AA,
|
||||
0x0000_0000, 0xC18E_DFC0, 0x586C_B9C1, 0x99E2_6601, 0xB0D9_7382, 0x7157_AC42, 0xE8B5_CA43, 0x293B_1583,
|
||||
0xBAC3_E145, 0x7B4D_3E85, 0xE2AF_5884, 0x2321_8744, 0x0A1A_92C7, 0xCB94_4D07, 0x5276_2B06, 0x93F8_F4C6,
|
||||
0xAEF6_C4CB, 0x6F78_1B0B, 0xF69A_7D0A, 0x3714_A2CA, 0x1E2F_B749, 0xDFA1_6889, 0x4643_0E88, 0x87CD_D148,
|
||||
0x1435_258E, 0xD5BB_FA4E, 0x4C59_9C4F, 0x8DD7_438F, 0xA4EC_560C, 0x6562_89CC, 0xFC80_EFCD, 0x3D0E_300D,
|
||||
0x869C_8FD7, 0x4712_5017, 0xDEF0_3616, 0x1F7E_E9D6, 0x3645_FC55, 0xF7CB_2395, 0x6E29_4594, 0xAFA7_9A54,
|
||||
0x3C5F_6E92, 0xFDD1_B152, 0x6433_D753, 0xA5BD_0893, 0x8C86_1D10, 0x4D08_C2D0, 0xD4EA_A4D1, 0x1564_7B11,
|
||||
0x286A_4B1C, 0xE9E4_94DC, 0x7006_F2DD, 0xB188_2D1D, 0x98B3_389E, 0x593D_E75E, 0xC0DF_815F, 0x0151_5E9F,
|
||||
0x92A9_AA59, 0x5327_7599, 0xCAC5_1398, 0x0B4B_CC58, 0x2270_D9DB, 0xE3FE_061B, 0x7A1C_601A, 0xBB92_BFDA,
|
||||
0xD648_19EF, 0x17C6_C62F, 0x8E24_A02E, 0x4FAA_7FEE, 0x6691_6A6D, 0xA71F_B5AD, 0x3EFD_D3AC, 0xFF73_0C6C,
|
||||
0x6C8B_F8AA, 0xAD05_276A, 0x34E7_416B, 0xF569_9EAB, 0xDC52_8B28, 0x1DDC_54E8, 0x843E_32E9, 0x45B0_ED29,
|
||||
0x78BE_DD24, 0xB930_02E4, 0x20D2_64E5, 0xE15C_BB25, 0xC867_AEA6, 0x09E9_7166, 0x900B_1767, 0x5185_C8A7,
|
||||
0xC27D_3C61, 0x03F3_E3A1, 0x9A11_85A0, 0x5B9F_5A60, 0x72A4_4FE3, 0xB32A_9023, 0x2AC8_F622, 0xEB46_29E2,
|
||||
0x50D4_9638, 0x915A_49F8, 0x08B8_2FF9, 0xC936_F039, 0xE00D_E5BA, 0x2183_3A7A, 0xB861_5C7B, 0x79EF_83BB,
|
||||
0xEA17_777D, 0x2B99_A8BD, 0xB27B_CEBC, 0x73F5_117C, 0x5ACE_04FF, 0x9B40_DB3F, 0x02A2_BD3E, 0xC32C_62FE,
|
||||
0xFE22_52F3, 0x3FAC_8D33, 0xA64E_EB32, 0x67C0_34F2, 0x4EFB_2171, 0x8F75_FEB1, 0x1697_98B0, 0xD719_4770,
|
||||
0x44E1_B3B6, 0x856F_6C76, 0x1C8D_0A77, 0xDD03_D5B7, 0xF438_C034, 0x35B6_1FF4, 0xAC54_79F5, 0x6DDA_A635,
|
||||
0x77E1_359F, 0xB66F_EA5F, 0x2F8D_8C5E, 0xEE03_539E, 0xC738_461D, 0x06B6_99DD, 0x9F54_FFDC, 0x5EDA_201C,
|
||||
0xCD22_D4DA, 0x0CAC_0B1A, 0x954E_6D1B, 0x54C0_B2DB, 0x7DFB_A758, 0xBC75_7898, 0x2597_1E99, 0xE419_C159,
|
||||
0xD917_F154, 0x1899_2E94, 0x817B_4895, 0x40F5_9755, 0x69CE_82D6, 0xA840_5D16, 0x31A2_3B17, 0xF02C_E4D7,
|
||||
0x63D4_1011, 0xA25A_CFD1, 0x3BB8_A9D0, 0xFA36_7610, 0xD30D_6393, 0x1283_BC53, 0x8B61_DA52, 0x4AEF_0592,
|
||||
0xF17D_BA48, 0x30F3_6588, 0xA911_0389, 0x689F_DC49, 0x41A4_C9CA, 0x802A_160A, 0x19C8_700B, 0xD846_AFCB,
|
||||
0x4BBE_5B0D, 0x8A30_84CD, 0x13D2_E2CC, 0xD25C_3D0C, 0xFB67_288F, 0x3AE9_F74F, 0xA30B_914E, 0x6285_4E8E,
|
||||
0x5F8B_7E83, 0x9E05_A143, 0x07E7_C742, 0xC669_1882, 0xEF52_0D01, 0x2EDC_D2C1, 0xB73E_B4C0, 0x76B0_6B00,
|
||||
0xE548_9FC6, 0x24C6_4006, 0xBD24_2607, 0x7CAA_F9C7, 0x5591_EC44, 0x941F_3384, 0x0DFD_5585, 0xCC73_8A45,
|
||||
0xA1A9_2C70, 0x6027_F3B0, 0xF9C5_95B1, 0x384B_4A71, 0x1170_5FF2, 0xD0FE_8032, 0x491C_E633, 0x8892_39F3,
|
||||
0x1B6A_CD35, 0xDAE4_12F5, 0x4306_74F4, 0x8288_AB34, 0xABB3_BEB7, 0x6A3D_6177, 0xF3DF_0776, 0x3251_D8B6,
|
||||
0x0F5F_E8BB, 0xCED1_377B, 0x5733_517A, 0x96BD_8EBA, 0xBF86_9B39, 0x7E08_44F9, 0xE7EA_22F8, 0x2664_FD38,
|
||||
0xB59C_09FE, 0x7412_D63E, 0xEDF0_B03F, 0x2C7E_6FFF, 0x0545_7A7C, 0xC4CB_A5BC, 0x5D29_C3BD, 0x9CA7_1C7D,
|
||||
0x2735_A3A7, 0xE6BB_7C67, 0x7F59_1A66, 0xBED7_C5A6, 0x97EC_D025, 0x5662_0FE5, 0xCF80_69E4, 0x0E0E_B624,
|
||||
0x9DF6_42E2, 0x5C78_9D22, 0xC59A_FB23, 0x0414_24E3, 0x2D2F_3160, 0xECA1_EEA0, 0x7543_88A1, 0xB4CD_5761,
|
||||
0x89C3_676C, 0x484D_B8AC, 0xD1AF_DEAD, 0x1021_016D, 0x391A_14EE, 0xF894_CB2E, 0x6176_AD2F, 0xA0F8_72EF,
|
||||
0x3300_8629, 0xF28E_59E9, 0x6B6C_3FE8, 0xAAE2_E028, 0x83D9_F5AB, 0x4257_2A6B, 0xDBB5_4C6A, 0x1A3B_93AA,
|
||||
],[
|
||||
0x0000_0000, 0x9BA5_4C6F, 0xEC3B_9E9F, 0x779E_D2F0, 0x0306_3B7F, 0x98A3_7710, 0xEF3D_A5E0, 0x7498_E98F,
|
||||
0x060C_76FE, 0x9DA9_3A91, 0xEA37_E861, 0x7192_A40E, 0x050A_4D81, 0x9EAF_01EE, 0xE931_D31E, 0x7294_9F71,
|
||||
0x0C18_EDFC, 0x97BD_A193, 0xE023_7363, 0x7B86_3F0C, 0x0F1E_D683, 0x94BB_9AEC, 0xE325_481C, 0x7880_0473,
|
||||
0x0A14_9B02, 0x91B1_D76D, 0xE62F_059D, 0x7D8A_49F2, 0x0912_A07D, 0x92B7_EC12, 0xE529_3EE2, 0x7E8C_728D,
|
||||
0x1831_DBF8, 0x8394_9797, 0xF40A_4567, 0x6FAF_0908, 0x1B37_E087, 0x8092_ACE8, 0xF70C_7E18, 0x6CA9_3277,
|
||||
0x1E3D_AD06, 0x8598_E169, 0xF206_3399, 0x69A3_7FF6, 0x1D3B_9679, 0x869E_DA16, 0xF100_08E6, 0x6AA5_4489,
|
||||
0x1429_3604, 0x8F8C_7A6B, 0xF812_A89B, 0x63B7_E4F4, 0x172F_0D7B, 0x8C8A_4114, 0xFB14_93E4, 0x60B1_DF8B,
|
||||
0x1225_40FA, 0x8980_0C95, 0xFE1E_DE65, 0x65BB_920A, 0x1123_7B85, 0x8A86_37EA, 0xFD18_E51A, 0x66BD_A975,
|
||||
0x3063_B7F0, 0xABC6_FB9F, 0xDC58_296F, 0x47FD_6500, 0x3365_8C8F, 0xA8C0_C0E0, 0xDF5E_1210, 0x44FB_5E7F,
|
||||
0x366F_C10E, 0xADCA_8D61, 0xDA54_5F91, 0x41F1_13FE, 0x3569_FA71, 0xAECC_B61E, 0xD952_64EE, 0x42F7_2881,
|
||||
0x3C7B_5A0C, 0xA7DE_1663, 0xD040_C493, 0x4BE5_88FC, 0x3F7D_6173, 0xA4D8_2D1C, 0xD346_FFEC, 0x48E3_B383,
|
||||
0x3A77_2CF2, 0xA1D2_609D, 0xD64C_B26D, 0x4DE9_FE02, 0x3971_178D, 0xA2D4_5BE2, 0xD54A_8912, 0x4EEF_C57D,
|
||||
0x2852_6C08, 0xB3F7_2067, 0xC469_F297, 0x5FCC_BEF8, 0x2B54_5777, 0xB0F1_1B18, 0xC76F_C9E8, 0x5CCA_8587,
|
||||
0x2E5E_1AF6, 0xB5FB_5699, 0xC265_8469, 0x59C0_C806, 0x2D58_2189, 0xB6FD_6DE6, 0xC163_BF16, 0x5AC6_F379,
|
||||
0x244A_81F4, 0xBFEF_CD9B, 0xC871_1F6B, 0x53D4_5304, 0x274C_BA8B, 0xBCE9_F6E4, 0xCB77_2414, 0x50D2_687B,
|
||||
0x2246_F70A, 0xB9E3_BB65, 0xCE7D_6995, 0x55D8_25FA, 0x2140_CC75, 0xBAE5_801A, 0xCD7B_52EA, 0x56DE_1E85,
|
||||
0x60C7_6FE0, 0xFB62_238F, 0x8CFC_F17F, 0x1759_BD10, 0x63C1_549F, 0xF864_18F0, 0x8FFA_CA00, 0x145F_866F,
|
||||
0x66CB_191E, 0xFD6E_5571, 0x8AF0_8781, 0x1155_CBEE, 0x65CD_2261, 0xFE68_6E0E, 0x89F6_BCFE, 0x1253_F091,
|
||||
0x6CDF_821C, 0xF77A_CE73, 0x80E4_1C83, 0x1B41_50EC, 0x6FD9_B963, 0xF47C_F50C, 0x83E2_27FC, 0x1847_6B93,
|
||||
0x6AD3_F4E2, 0xF176_B88D, 0x86E8_6A7D, 0x1D4D_2612, 0x69D5_CF9D, 0xF270_83F2, 0x85EE_5102, 0x1E4B_1D6D,
|
||||
0x78F6_B418, 0xE353_F877, 0x94CD_2A87, 0x0F68_66E8, 0x7BF0_8F67, 0xE055_C308, 0x97CB_11F8, 0x0C6E_5D97,
|
||||
0x7EFA_C2E6, 0xE55F_8E89, 0x92C1_5C79, 0x0964_1016, 0x7DFC_F999, 0xE659_B5F6, 0x91C7_6706, 0x0A62_2B69,
|
||||
0x74EE_59E4, 0xEF4B_158B, 0x98D5_C77B, 0x0370_8B14, 0x77E8_629B, 0xEC4D_2EF4, 0x9BD3_FC04, 0x0076_B06B,
|
||||
0x72E2_2F1A, 0xE947_6375, 0x9ED9_B185, 0x057C_FDEA, 0x71E4_1465, 0xEA41_580A, 0x9DDF_8AFA, 0x067A_C695,
|
||||
0x50A4_D810, 0xCB01_947F, 0xBC9F_468F, 0x273A_0AE0, 0x53A2_E36F, 0xC807_AF00, 0xBF99_7DF0, 0x243C_319F,
|
||||
0x56A8_AEEE, 0xCD0D_E281, 0xBA93_3071, 0x2136_7C1E, 0x55AE_9591, 0xCE0B_D9FE, 0xB995_0B0E, 0x2230_4761,
|
||||
0x5CBC_35EC, 0xC719_7983, 0xB087_AB73, 0x2B22_E71C, 0x5FBA_0E93, 0xC41F_42FC, 0xB381_900C, 0x2824_DC63,
|
||||
0x5AB0_4312, 0xC115_0F7D, 0xB68B_DD8D, 0x2D2E_91E2, 0x59B6_786D, 0xC213_3402, 0xB58D_E6F2, 0x2E28_AA9D,
|
||||
0x4895_03E8, 0xD330_4F87, 0xA4AE_9D77, 0x3F0B_D118, 0x4B93_3897, 0xD036_74F8, 0xA7A8_A608, 0x3C0D_EA67,
|
||||
0x4E99_7516, 0xD53C_3979, 0xA2A2_EB89, 0x3907_A7E6, 0x4D9F_4E69, 0xD63A_0206, 0xA1A4_D0F6, 0x3A01_9C99,
|
||||
0x448D_EE14, 0xDF28_A27B, 0xA8B6_708B, 0x3313_3CE4, 0x478B_D56B, 0xDC2E_9904, 0xABB0_4BF4, 0x3015_079B,
|
||||
0x4281_98EA, 0xD924_D485, 0xAEBA_0675, 0x351F_4A1A, 0x4187_A395, 0xDA22_EFFA, 0xADBC_3D0A, 0x3619_7165,
|
||||
0x0000_0000, 0x9BA5_4C6F, 0xEC3B_9E9F, 0x779E_D2F0, 0x0306_3B7F, 0x98A3_7710, 0xEF3D_A5E0, 0x7498_E98F,
|
||||
0x060C_76FE, 0x9DA9_3A91, 0xEA37_E861, 0x7192_A40E, 0x050A_4D81, 0x9EAF_01EE, 0xE931_D31E, 0x7294_9F71,
|
||||
0x0C18_EDFC, 0x97BD_A193, 0xE023_7363, 0x7B86_3F0C, 0x0F1E_D683, 0x94BB_9AEC, 0xE325_481C, 0x7880_0473,
|
||||
0x0A14_9B02, 0x91B1_D76D, 0xE62F_059D, 0x7D8A_49F2, 0x0912_A07D, 0x92B7_EC12, 0xE529_3EE2, 0x7E8C_728D,
|
||||
0x1831_DBF8, 0x8394_9797, 0xF40A_4567, 0x6FAF_0908, 0x1B37_E087, 0x8092_ACE8, 0xF70C_7E18, 0x6CA9_3277,
|
||||
0x1E3D_AD06, 0x8598_E169, 0xF206_3399, 0x69A3_7FF6, 0x1D3B_9679, 0x869E_DA16, 0xF100_08E6, 0x6AA5_4489,
|
||||
0x1429_3604, 0x8F8C_7A6B, 0xF812_A89B, 0x63B7_E4F4, 0x172F_0D7B, 0x8C8A_4114, 0xFB14_93E4, 0x60B1_DF8B,
|
||||
0x1225_40FA, 0x8980_0C95, 0xFE1E_DE65, 0x65BB_920A, 0x1123_7B85, 0x8A86_37EA, 0xFD18_E51A, 0x66BD_A975,
|
||||
0x3063_B7F0, 0xABC6_FB9F, 0xDC58_296F, 0x47FD_6500, 0x3365_8C8F, 0xA8C0_C0E0, 0xDF5E_1210, 0x44FB_5E7F,
|
||||
0x366F_C10E, 0xADCA_8D61, 0xDA54_5F91, 0x41F1_13FE, 0x3569_FA71, 0xAECC_B61E, 0xD952_64EE, 0x42F7_2881,
|
||||
0x3C7B_5A0C, 0xA7DE_1663, 0xD040_C493, 0x4BE5_88FC, 0x3F7D_6173, 0xA4D8_2D1C, 0xD346_FFEC, 0x48E3_B383,
|
||||
0x3A77_2CF2, 0xA1D2_609D, 0xD64C_B26D, 0x4DE9_FE02, 0x3971_178D, 0xA2D4_5BE2, 0xD54A_8912, 0x4EEF_C57D,
|
||||
0x2852_6C08, 0xB3F7_2067, 0xC469_F297, 0x5FCC_BEF8, 0x2B54_5777, 0xB0F1_1B18, 0xC76F_C9E8, 0x5CCA_8587,
|
||||
0x2E5E_1AF6, 0xB5FB_5699, 0xC265_8469, 0x59C0_C806, 0x2D58_2189, 0xB6FD_6DE6, 0xC163_BF16, 0x5AC6_F379,
|
||||
0x244A_81F4, 0xBFEF_CD9B, 0xC871_1F6B, 0x53D4_5304, 0x274C_BA8B, 0xBCE9_F6E4, 0xCB77_2414, 0x50D2_687B,
|
||||
0x2246_F70A, 0xB9E3_BB65, 0xCE7D_6995, 0x55D8_25FA, 0x2140_CC75, 0xBAE5_801A, 0xCD7B_52EA, 0x56DE_1E85,
|
||||
0x60C7_6FE0, 0xFB62_238F, 0x8CFC_F17F, 0x1759_BD10, 0x63C1_549F, 0xF864_18F0, 0x8FFA_CA00, 0x145F_866F,
|
||||
0x66CB_191E, 0xFD6E_5571, 0x8AF0_8781, 0x1155_CBEE, 0x65CD_2261, 0xFE68_6E0E, 0x89F6_BCFE, 0x1253_F091,
|
||||
0x6CDF_821C, 0xF77A_CE73, 0x80E4_1C83, 0x1B41_50EC, 0x6FD9_B963, 0xF47C_F50C, 0x83E2_27FC, 0x1847_6B93,
|
||||
0x6AD3_F4E2, 0xF176_B88D, 0x86E8_6A7D, 0x1D4D_2612, 0x69D5_CF9D, 0xF270_83F2, 0x85EE_5102, 0x1E4B_1D6D,
|
||||
0x78F6_B418, 0xE353_F877, 0x94CD_2A87, 0x0F68_66E8, 0x7BF0_8F67, 0xE055_C308, 0x97CB_11F8, 0x0C6E_5D97,
|
||||
0x7EFA_C2E6, 0xE55F_8E89, 0x92C1_5C79, 0x0964_1016, 0x7DFC_F999, 0xE659_B5F6, 0x91C7_6706, 0x0A62_2B69,
|
||||
0x74EE_59E4, 0xEF4B_158B, 0x98D5_C77B, 0x0370_8B14, 0x77E8_629B, 0xEC4D_2EF4, 0x9BD3_FC04, 0x0076_B06B,
|
||||
0x72E2_2F1A, 0xE947_6375, 0x9ED9_B185, 0x057C_FDEA, 0x71E4_1465, 0xEA41_580A, 0x9DDF_8AFA, 0x067A_C695,
|
||||
0x50A4_D810, 0xCB01_947F, 0xBC9F_468F, 0x273A_0AE0, 0x53A2_E36F, 0xC807_AF00, 0xBF99_7DF0, 0x243C_319F,
|
||||
0x56A8_AEEE, 0xCD0D_E281, 0xBA93_3071, 0x2136_7C1E, 0x55AE_9591, 0xCE0B_D9FE, 0xB995_0B0E, 0x2230_4761,
|
||||
0x5CBC_35EC, 0xC719_7983, 0xB087_AB73, 0x2B22_E71C, 0x5FBA_0E93, 0xC41F_42FC, 0xB381_900C, 0x2824_DC63,
|
||||
0x5AB0_4312, 0xC115_0F7D, 0xB68B_DD8D, 0x2D2E_91E2, 0x59B6_786D, 0xC213_3402, 0xB58D_E6F2, 0x2E28_AA9D,
|
||||
0x4895_03E8, 0xD330_4F87, 0xA4AE_9D77, 0x3F0B_D118, 0x4B93_3897, 0xD036_74F8, 0xA7A8_A608, 0x3C0D_EA67,
|
||||
0x4E99_7516, 0xD53C_3979, 0xA2A2_EB89, 0x3907_A7E6, 0x4D9F_4E69, 0xD63A_0206, 0xA1A4_D0F6, 0x3A01_9C99,
|
||||
0x448D_EE14, 0xDF28_A27B, 0xA8B6_708B, 0x3313_3CE4, 0x478B_D56B, 0xDC2E_9904, 0xABB0_4BF4, 0x3015_079B,
|
||||
0x4281_98EA, 0xD924_D485, 0xAEBA_0675, 0x351F_4A1A, 0x4187_A395, 0xDA22_EFFA, 0xADBC_3D0A, 0x3619_7165,
|
||||
],[
|
||||
0x0000_0000, 0xDD96_D985, 0x605C_B54B, 0xBDCA_6CCE, 0xC0B9_6A96, 0x1D2F_B313, 0xA0E5_DFDD, 0x7D73_0658,
|
||||
0x5A03_D36D, 0x8795_0AE8, 0x3A5F_6626, 0xE7C9_BFA3, 0x9ABA_B9FB, 0x472C_607E, 0xFAE6_0CB0, 0x2770_D535,
|
||||
0xB407_A6DA, 0x6991_7F5F, 0xD45B_1391, 0x09CD_CA14, 0x74BE_CC4C, 0xA928_15C9, 0x14E2_7907, 0xC974_A082,
|
||||
0xEE04_75B7, 0x3392_AC32, 0x8E58_C0FC, 0x53CE_1979, 0x2EBD_1F21, 0xF32B_C6A4, 0x4EE1_AA6A, 0x9377_73EF,
|
||||
0xB37E_4BF5, 0x6EE8_9270, 0xD322_FEBE, 0x0EB4_273B, 0x73C7_2163, 0xAE51_F8E6, 0x139B_9428, 0xCE0D_4DAD,
|
||||
0xE97D_9898, 0x34EB_411D, 0x8921_2DD3, 0x54B7_F456, 0x29C4_F20E, 0xF452_2B8B, 0x4998_4745, 0x940E_9EC0,
|
||||
0x0779_ED2F, 0xDAEF_34AA, 0x6725_5864, 0xBAB3_81E1, 0xC7C0_87B9, 0x1A56_5E3C, 0xA79C_32F2, 0x7A0A_EB77,
|
||||
0x5D7A_3E42, 0x80EC_E7C7, 0x3D26_8B09, 0xE0B0_528C, 0x9DC3_54D4, 0x4055_8D51, 0xFD9F_E19F, 0x2009_381A,
|
||||
0xBD8D_91AB, 0x601B_482E, 0xDDD1_24E0, 0x0047_FD65, 0x7D34_FB3D, 0xA0A2_22B8, 0x1D68_4E76, 0xC0FE_97F3,
|
||||
0xE78E_42C6, 0x3A18_9B43, 0x87D2_F78D, 0x5A44_2E08, 0x2737_2850, 0xFAA1_F1D5, 0x476B_9D1B, 0x9AFD_449E,
|
||||
0x098A_3771, 0xD41C_EEF4, 0x69D6_823A, 0xB440_5BBF, 0xC933_5DE7, 0x14A5_8462, 0xA96F_E8AC, 0x74F9_3129,
|
||||
0x5389_E41C, 0x8E1F_3D99, 0x33D5_5157, 0xEE43_88D2, 0x9330_8E8A, 0x4EA6_570F, 0xF36C_3BC1, 0x2EFA_E244,
|
||||
0x0EF3_DA5E, 0xD365_03DB, 0x6EAF_6F15, 0xB339_B690, 0xCE4A_B0C8, 0x13DC_694D, 0xAE16_0583, 0x7380_DC06,
|
||||
0x54F0_0933, 0x8966_D0B6, 0x34AC_BC78, 0xE93A_65FD, 0x9449_63A5, 0x49DF_BA20, 0xF415_D6EE, 0x2983_0F6B,
|
||||
0xBAF4_7C84, 0x6762_A501, 0xDAA8_C9CF, 0x073E_104A, 0x7A4D_1612, 0xA7DB_CF97, 0x1A11_A359, 0xC787_7ADC,
|
||||
0xE0F7_AFE9, 0x3D61_766C, 0x80AB_1AA2, 0x5D3D_C327, 0x204E_C57F, 0xFDD8_1CFA, 0x4012_7034, 0x9D84_A9B1,
|
||||
0xA06A_2517, 0x7DFC_FC92, 0xC036_905C, 0x1DA0_49D9, 0x60D3_4F81, 0xBD45_9604, 0x008F_FACA, 0xDD19_234F,
|
||||
0xFA69_F67A, 0x27FF_2FFF, 0x9A35_4331, 0x47A3_9AB4, 0x3AD0_9CEC, 0xE746_4569, 0x5A8C_29A7, 0x871A_F022,
|
||||
0x146D_83CD, 0xC9FB_5A48, 0x7431_3686, 0xA9A7_EF03, 0xD4D4_E95B, 0x0942_30DE, 0xB488_5C10, 0x691E_8595,
|
||||
0x4E6E_50A0, 0x93F8_8925, 0x2E32_E5EB, 0xF3A4_3C6E, 0x8ED7_3A36, 0x5341_E3B3, 0xEE8B_8F7D, 0x331D_56F8,
|
||||
0x1314_6EE2, 0xCE82_B767, 0x7348_DBA9, 0xAEDE_022C, 0xD3AD_0474, 0x0E3B_DDF1, 0xB3F1_B13F, 0x6E67_68BA,
|
||||
0x4917_BD8F, 0x9481_640A, 0x294B_08C4, 0xF4DD_D141, 0x89AE_D719, 0x5438_0E9C, 0xE9F2_6252, 0x3464_BBD7,
|
||||
0xA713_C838, 0x7A85_11BD, 0xC74F_7D73, 0x1AD9_A4F6, 0x67AA_A2AE, 0xBA3C_7B2B, 0x07F6_17E5, 0xDA60_CE60,
|
||||
0xFD10_1B55, 0x2086_C2D0, 0x9D4C_AE1E, 0x40DA_779B, 0x3DA9_71C3, 0xE03F_A846, 0x5DF5_C488, 0x8063_1D0D,
|
||||
0x1DE7_B4BC, 0xC071_6D39, 0x7DBB_01F7, 0xA02D_D872, 0xDD5E_DE2A, 0x00C8_07AF, 0xBD02_6B61, 0x6094_B2E4,
|
||||
0x47E4_67D1, 0x9A72_BE54, 0x27B8_D29A, 0xFA2E_0B1F, 0x875D_0D47, 0x5ACB_D4C2, 0xE701_B80C, 0x3A97_6189,
|
||||
0xA9E0_1266, 0x7476_CBE3, 0xC9BC_A72D, 0x142A_7EA8, 0x6959_78F0, 0xB4CF_A175, 0x0905_CDBB, 0xD493_143E,
|
||||
0xF3E3_C10B, 0x2E75_188E, 0x93BF_7440, 0x4E29_ADC5, 0x335A_AB9D, 0xEECC_7218, 0x5306_1ED6, 0x8E90_C753,
|
||||
0xAE99_FF49, 0x730F_26CC, 0xCEC5_4A02, 0x1353_9387, 0x6E20_95DF, 0xB3B6_4C5A, 0x0E7C_2094, 0xD3EA_F911,
|
||||
0xF49A_2C24, 0x290C_F5A1, 0x94C6_996F, 0x4950_40EA, 0x3423_46B2, 0xE9B5_9F37, 0x547F_F3F9, 0x89E9_2A7C,
|
||||
0x1A9E_5993, 0xC708_8016, 0x7AC2_ECD8, 0xA754_355D, 0xDA27_3305, 0x07B1_EA80, 0xBA7B_864E, 0x67ED_5FCB,
|
||||
0x409D_8AFE, 0x9D0B_537B, 0x20C1_3FB5, 0xFD57_E630, 0x8024_E068, 0x5DB2_39ED, 0xE078_5523, 0x3DEE_8CA6,
|
||||
0x0000_0000, 0xDD96_D985, 0x605C_B54B, 0xBDCA_6CCE, 0xC0B9_6A96, 0x1D2F_B313, 0xA0E5_DFDD, 0x7D73_0658,
|
||||
0x5A03_D36D, 0x8795_0AE8, 0x3A5F_6626, 0xE7C9_BFA3, 0x9ABA_B9FB, 0x472C_607E, 0xFAE6_0CB0, 0x2770_D535,
|
||||
0xB407_A6DA, 0x6991_7F5F, 0xD45B_1391, 0x09CD_CA14, 0x74BE_CC4C, 0xA928_15C9, 0x14E2_7907, 0xC974_A082,
|
||||
0xEE04_75B7, 0x3392_AC32, 0x8E58_C0FC, 0x53CE_1979, 0x2EBD_1F21, 0xF32B_C6A4, 0x4EE1_AA6A, 0x9377_73EF,
|
||||
0xB37E_4BF5, 0x6EE8_9270, 0xD322_FEBE, 0x0EB4_273B, 0x73C7_2163, 0xAE51_F8E6, 0x139B_9428, 0xCE0D_4DAD,
|
||||
0xE97D_9898, 0x34EB_411D, 0x8921_2DD3, 0x54B7_F456, 0x29C4_F20E, 0xF452_2B8B, 0x4998_4745, 0x940E_9EC0,
|
||||
0x0779_ED2F, 0xDAEF_34AA, 0x6725_5864, 0xBAB3_81E1, 0xC7C0_87B9, 0x1A56_5E3C, 0xA79C_32F2, 0x7A0A_EB77,
|
||||
0x5D7A_3E42, 0x80EC_E7C7, 0x3D26_8B09, 0xE0B0_528C, 0x9DC3_54D4, 0x4055_8D51, 0xFD9F_E19F, 0x2009_381A,
|
||||
0xBD8D_91AB, 0x601B_482E, 0xDDD1_24E0, 0x0047_FD65, 0x7D34_FB3D, 0xA0A2_22B8, 0x1D68_4E76, 0xC0FE_97F3,
|
||||
0xE78E_42C6, 0x3A18_9B43, 0x87D2_F78D, 0x5A44_2E08, 0x2737_2850, 0xFAA1_F1D5, 0x476B_9D1B, 0x9AFD_449E,
|
||||
0x098A_3771, 0xD41C_EEF4, 0x69D6_823A, 0xB440_5BBF, 0xC933_5DE7, 0x14A5_8462, 0xA96F_E8AC, 0x74F9_3129,
|
||||
0x5389_E41C, 0x8E1F_3D99, 0x33D5_5157, 0xEE43_88D2, 0x9330_8E8A, 0x4EA6_570F, 0xF36C_3BC1, 0x2EFA_E244,
|
||||
0x0EF3_DA5E, 0xD365_03DB, 0x6EAF_6F15, 0xB339_B690, 0xCE4A_B0C8, 0x13DC_694D, 0xAE16_0583, 0x7380_DC06,
|
||||
0x54F0_0933, 0x8966_D0B6, 0x34AC_BC78, 0xE93A_65FD, 0x9449_63A5, 0x49DF_BA20, 0xF415_D6EE, 0x2983_0F6B,
|
||||
0xBAF4_7C84, 0x6762_A501, 0xDAA8_C9CF, 0x073E_104A, 0x7A4D_1612, 0xA7DB_CF97, 0x1A11_A359, 0xC787_7ADC,
|
||||
0xE0F7_AFE9, 0x3D61_766C, 0x80AB_1AA2, 0x5D3D_C327, 0x204E_C57F, 0xFDD8_1CFA, 0x4012_7034, 0x9D84_A9B1,
|
||||
0xA06A_2517, 0x7DFC_FC92, 0xC036_905C, 0x1DA0_49D9, 0x60D3_4F81, 0xBD45_9604, 0x008F_FACA, 0xDD19_234F,
|
||||
0xFA69_F67A, 0x27FF_2FFF, 0x9A35_4331, 0x47A3_9AB4, 0x3AD0_9CEC, 0xE746_4569, 0x5A8C_29A7, 0x871A_F022,
|
||||
0x146D_83CD, 0xC9FB_5A48, 0x7431_3686, 0xA9A7_EF03, 0xD4D4_E95B, 0x0942_30DE, 0xB488_5C10, 0x691E_8595,
|
||||
0x4E6E_50A0, 0x93F8_8925, 0x2E32_E5EB, 0xF3A4_3C6E, 0x8ED7_3A36, 0x5341_E3B3, 0xEE8B_8F7D, 0x331D_56F8,
|
||||
0x1314_6EE2, 0xCE82_B767, 0x7348_DBA9, 0xAEDE_022C, 0xD3AD_0474, 0x0E3B_DDF1, 0xB3F1_B13F, 0x6E67_68BA,
|
||||
0x4917_BD8F, 0x9481_640A, 0x294B_08C4, 0xF4DD_D141, 0x89AE_D719, 0x5438_0E9C, 0xE9F2_6252, 0x3464_BBD7,
|
||||
0xA713_C838, 0x7A85_11BD, 0xC74F_7D73, 0x1AD9_A4F6, 0x67AA_A2AE, 0xBA3C_7B2B, 0x07F6_17E5, 0xDA60_CE60,
|
||||
0xFD10_1B55, 0x2086_C2D0, 0x9D4C_AE1E, 0x40DA_779B, 0x3DA9_71C3, 0xE03F_A846, 0x5DF5_C488, 0x8063_1D0D,
|
||||
0x1DE7_B4BC, 0xC071_6D39, 0x7DBB_01F7, 0xA02D_D872, 0xDD5E_DE2A, 0x00C8_07AF, 0xBD02_6B61, 0x6094_B2E4,
|
||||
0x47E4_67D1, 0x9A72_BE54, 0x27B8_D29A, 0xFA2E_0B1F, 0x875D_0D47, 0x5ACB_D4C2, 0xE701_B80C, 0x3A97_6189,
|
||||
0xA9E0_1266, 0x7476_CBE3, 0xC9BC_A72D, 0x142A_7EA8, 0x6959_78F0, 0xB4CF_A175, 0x0905_CDBB, 0xD493_143E,
|
||||
0xF3E3_C10B, 0x2E75_188E, 0x93BF_7440, 0x4E29_ADC5, 0x335A_AB9D, 0xEECC_7218, 0x5306_1ED6, 0x8E90_C753,
|
||||
0xAE99_FF49, 0x730F_26CC, 0xCEC5_4A02, 0x1353_9387, 0x6E20_95DF, 0xB3B6_4C5A, 0x0E7C_2094, 0xD3EA_F911,
|
||||
0xF49A_2C24, 0x290C_F5A1, 0x94C6_996F, 0x4950_40EA, 0x3423_46B2, 0xE9B5_9F37, 0x547F_F3F9, 0x89E9_2A7C,
|
||||
0x1A9E_5993, 0xC708_8016, 0x7AC2_ECD8, 0xA754_355D, 0xDA27_3305, 0x07B1_EA80, 0xBA7B_864E, 0x67ED_5FCB,
|
||||
0x409D_8AFE, 0x9D0B_537B, 0x20C1_3FB5, 0xFD57_E630, 0x8024_E068, 0x5DB2_39ED, 0xE078_5523, 0x3DEE_8CA6,
|
||||
],[
|
||||
0x0000_0000, 0x9D0F_E176, 0xE16E_C4AD, 0x7C61_25DB, 0x19AC_8F1B, 0x84A3_6E6D, 0xF8C2_4BB6, 0x65CD_AAC0,
|
||||
0x3359_1E36, 0xAE56_FF40, 0xD237_DA9B, 0x4F38_3BED, 0x2AF5_912D, 0xB7FA_705B, 0xCB9B_5580, 0x5694_B4F6,
|
||||
0x66B2_3C6C, 0xFBBD_DD1A, 0x87DC_F8C1, 0x1AD3_19B7, 0x7F1E_B377, 0xE211_5201, 0x9E70_77DA, 0x037F_96AC,
|
||||
0x55EB_225A, 0xC8E4_C32C, 0xB485_E6F7, 0x298A_0781, 0x4C47_AD41, 0xD148_4C37, 0xAD29_69EC, 0x3026_889A,
|
||||
0xCD64_78D8, 0x506B_99AE, 0x2C0A_BC75, 0xB105_5D03, 0xD4C8_F7C3, 0x49C7_16B5, 0x35A6_336E, 0xA8A9_D218,
|
||||
0xFE3D_66EE, 0x6332_8798, 0x1F53_A243, 0x825C_4335, 0xE791_E9F5, 0x7A9E_0883, 0x06FF_2D58, 0x9BF0_CC2E,
|
||||
0xABD6_44B4, 0x36D9_A5C2, 0x4AB8_8019, 0xD7B7_616F, 0xB27A_CBAF, 0x2F75_2AD9, 0x5314_0F02, 0xCE1B_EE74,
|
||||
0x988F_5A82, 0x0580_BBF4, 0x79E1_9E2F, 0xE4EE_7F59, 0x8123_D599, 0x1C2C_34EF, 0x604D_1134, 0xFD42_F042,
|
||||
0x41B9_F7F1, 0xDCB6_1687, 0xA0D7_335C, 0x3DD8_D22A, 0x5815_78EA, 0xC51A_999C, 0xB97B_BC47, 0x2474_5D31,
|
||||
0x72E0_E9C7, 0xEFEF_08B1, 0x938E_2D6A, 0x0E81_CC1C, 0x6B4C_66DC, 0xF643_87AA, 0x8A22_A271, 0x172D_4307,
|
||||
0x270B_CB9D, 0xBA04_2AEB, 0xC665_0F30, 0x5B6A_EE46, 0x3EA7_4486, 0xA3A8_A5F0, 0xDFC9_802B, 0x42C6_615D,
|
||||
0x1452_D5AB, 0x895D_34DD, 0xF53C_1106, 0x6833_F070, 0x0DFE_5AB0, 0x90F1_BBC6, 0xEC90_9E1D, 0x719F_7F6B,
|
||||
0x8CDD_8F29, 0x11D2_6E5F, 0x6DB3_4B84, 0xF0BC_AAF2, 0x9571_0032, 0x087E_E144, 0x741F_C49F, 0xE910_25E9,
|
||||
0xBF84_911F, 0x228B_7069, 0x5EEA_55B2, 0xC3E5_B4C4, 0xA628_1E04, 0x3B27_FF72, 0x4746_DAA9, 0xDA49_3BDF,
|
||||
0xEA6F_B345, 0x7760_5233, 0x0B01_77E8, 0x960E_969E, 0xF3C3_3C5E, 0x6ECC_DD28, 0x12AD_F8F3, 0x8FA2_1985,
|
||||
0xD936_AD73, 0x4439_4C05, 0x3858_69DE, 0xA557_88A8, 0xC09A_2268, 0x5D95_C31E, 0x21F4_E6C5, 0xBCFB_07B3,
|
||||
0x8373_EFE2, 0x1E7C_0E94, 0x621D_2B4F, 0xFF12_CA39, 0x9ADF_60F9, 0x07D0_818F, 0x7BB1_A454, 0xE6BE_4522,
|
||||
0xB02A_F1D4, 0x2D25_10A2, 0x5144_3579, 0xCC4B_D40F, 0xA986_7ECF, 0x3489_9FB9, 0x48E8_BA62, 0xD5E7_5B14,
|
||||
0xE5C1_D38E, 0x78CE_32F8, 0x04AF_1723, 0x99A0_F655, 0xFC6D_5C95, 0x6162_BDE3, 0x1D03_9838, 0x800C_794E,
|
||||
0xD698_CDB8, 0x4B97_2CCE, 0x37F6_0915, 0xAAF9_E863, 0xCF34_42A3, 0x523B_A3D5, 0x2E5A_860E, 0xB355_6778,
|
||||
0x4E17_973A, 0xD318_764C, 0xAF79_5397, 0x3276_B2E1, 0x57BB_1821, 0xCAB4_F957, 0xB6D5_DC8C, 0x2BDA_3DFA,
|
||||
0x7D4E_890C, 0xE041_687A, 0x9C20_4DA1, 0x012F_ACD7, 0x64E2_0617, 0xF9ED_E761, 0x858C_C2BA, 0x1883_23CC,
|
||||
0x28A5_AB56, 0xB5AA_4A20, 0xC9CB_6FFB, 0x54C4_8E8D, 0x3109_244D, 0xAC06_C53B, 0xD067_E0E0, 0x4D68_0196,
|
||||
0x1BFC_B560, 0x86F3_5416, 0xFA92_71CD, 0x679D_90BB, 0x0250_3A7B, 0x9F5F_DB0D, 0xE33E_FED6, 0x7E31_1FA0,
|
||||
0xC2CA_1813, 0x5FC5_F965, 0x23A4_DCBE, 0xBEAB_3DC8, 0xDB66_9708, 0x4669_767E, 0x3A08_53A5, 0xA707_B2D3,
|
||||
0xF193_0625, 0x6C9C_E753, 0x10FD_C288, 0x8DF2_23FE, 0xE83F_893E, 0x7530_6848, 0x0951_4D93, 0x945E_ACE5,
|
||||
0xA478_247F, 0x3977_C509, 0x4516_E0D2, 0xD819_01A4, 0xBDD4_AB64, 0x20DB_4A12, 0x5CBA_6FC9, 0xC1B5_8EBF,
|
||||
0x9721_3A49, 0x0A2E_DB3F, 0x764F_FEE4, 0xEB40_1F92, 0x8E8D_B552, 0x1382_5424, 0x6FE3_71FF, 0xF2EC_9089,
|
||||
0x0FAE_60CB, 0x92A1_81BD, 0xEEC0_A466, 0x73CF_4510, 0x1602_EFD0, 0x8B0D_0EA6, 0xF76C_2B7D, 0x6A63_CA0B,
|
||||
0x3CF7_7EFD, 0xA1F8_9F8B, 0xDD99_BA50, 0x4096_5B26, 0x255B_F1E6, 0xB854_1090, 0xC435_354B, 0x593A_D43D,
|
||||
0x691C_5CA7, 0xF413_BDD1, 0x8872_980A, 0x157D_797C, 0x70B0_D3BC, 0xEDBF_32CA, 0x91DE_1711, 0x0CD1_F667,
|
||||
0x5A45_4291, 0xC74A_A3E7, 0xBB2B_863C, 0x2624_674A, 0x43E9_CD8A, 0xDEE6_2CFC, 0xA287_0927, 0x3F88_E851,
|
||||
0x0000_0000, 0x9D0F_E176, 0xE16E_C4AD, 0x7C61_25DB, 0x19AC_8F1B, 0x84A3_6E6D, 0xF8C2_4BB6, 0x65CD_AAC0,
|
||||
0x3359_1E36, 0xAE56_FF40, 0xD237_DA9B, 0x4F38_3BED, 0x2AF5_912D, 0xB7FA_705B, 0xCB9B_5580, 0x5694_B4F6,
|
||||
0x66B2_3C6C, 0xFBBD_DD1A, 0x87DC_F8C1, 0x1AD3_19B7, 0x7F1E_B377, 0xE211_5201, 0x9E70_77DA, 0x037F_96AC,
|
||||
0x55EB_225A, 0xC8E4_C32C, 0xB485_E6F7, 0x298A_0781, 0x4C47_AD41, 0xD148_4C37, 0xAD29_69EC, 0x3026_889A,
|
||||
0xCD64_78D8, 0x506B_99AE, 0x2C0A_BC75, 0xB105_5D03, 0xD4C8_F7C3, 0x49C7_16B5, 0x35A6_336E, 0xA8A9_D218,
|
||||
0xFE3D_66EE, 0x6332_8798, 0x1F53_A243, 0x825C_4335, 0xE791_E9F5, 0x7A9E_0883, 0x06FF_2D58, 0x9BF0_CC2E,
|
||||
0xABD6_44B4, 0x36D9_A5C2, 0x4AB8_8019, 0xD7B7_616F, 0xB27A_CBAF, 0x2F75_2AD9, 0x5314_0F02, 0xCE1B_EE74,
|
||||
0x988F_5A82, 0x0580_BBF4, 0x79E1_9E2F, 0xE4EE_7F59, 0x8123_D599, 0x1C2C_34EF, 0x604D_1134, 0xFD42_F042,
|
||||
0x41B9_F7F1, 0xDCB6_1687, 0xA0D7_335C, 0x3DD8_D22A, 0x5815_78EA, 0xC51A_999C, 0xB97B_BC47, 0x2474_5D31,
|
||||
0x72E0_E9C7, 0xEFEF_08B1, 0x938E_2D6A, 0x0E81_CC1C, 0x6B4C_66DC, 0xF643_87AA, 0x8A22_A271, 0x172D_4307,
|
||||
0x270B_CB9D, 0xBA04_2AEB, 0xC665_0F30, 0x5B6A_EE46, 0x3EA7_4486, 0xA3A8_A5F0, 0xDFC9_802B, 0x42C6_615D,
|
||||
0x1452_D5AB, 0x895D_34DD, 0xF53C_1106, 0x6833_F070, 0x0DFE_5AB0, 0x90F1_BBC6, 0xEC90_9E1D, 0x719F_7F6B,
|
||||
0x8CDD_8F29, 0x11D2_6E5F, 0x6DB3_4B84, 0xF0BC_AAF2, 0x9571_0032, 0x087E_E144, 0x741F_C49F, 0xE910_25E9,
|
||||
0xBF84_911F, 0x228B_7069, 0x5EEA_55B2, 0xC3E5_B4C4, 0xA628_1E04, 0x3B27_FF72, 0x4746_DAA9, 0xDA49_3BDF,
|
||||
0xEA6F_B345, 0x7760_5233, 0x0B01_77E8, 0x960E_969E, 0xF3C3_3C5E, 0x6ECC_DD28, 0x12AD_F8F3, 0x8FA2_1985,
|
||||
0xD936_AD73, 0x4439_4C05, 0x3858_69DE, 0xA557_88A8, 0xC09A_2268, 0x5D95_C31E, 0x21F4_E6C5, 0xBCFB_07B3,
|
||||
0x8373_EFE2, 0x1E7C_0E94, 0x621D_2B4F, 0xFF12_CA39, 0x9ADF_60F9, 0x07D0_818F, 0x7BB1_A454, 0xE6BE_4522,
|
||||
0xB02A_F1D4, 0x2D25_10A2, 0x5144_3579, 0xCC4B_D40F, 0xA986_7ECF, 0x3489_9FB9, 0x48E8_BA62, 0xD5E7_5B14,
|
||||
0xE5C1_D38E, 0x78CE_32F8, 0x04AF_1723, 0x99A0_F655, 0xFC6D_5C95, 0x6162_BDE3, 0x1D03_9838, 0x800C_794E,
|
||||
0xD698_CDB8, 0x4B97_2CCE, 0x37F6_0915, 0xAAF9_E863, 0xCF34_42A3, 0x523B_A3D5, 0x2E5A_860E, 0xB355_6778,
|
||||
0x4E17_973A, 0xD318_764C, 0xAF79_5397, 0x3276_B2E1, 0x57BB_1821, 0xCAB4_F957, 0xB6D5_DC8C, 0x2BDA_3DFA,
|
||||
0x7D4E_890C, 0xE041_687A, 0x9C20_4DA1, 0x012F_ACD7, 0x64E2_0617, 0xF9ED_E761, 0x858C_C2BA, 0x1883_23CC,
|
||||
0x28A5_AB56, 0xB5AA_4A20, 0xC9CB_6FFB, 0x54C4_8E8D, 0x3109_244D, 0xAC06_C53B, 0xD067_E0E0, 0x4D68_0196,
|
||||
0x1BFC_B560, 0x86F3_5416, 0xFA92_71CD, 0x679D_90BB, 0x0250_3A7B, 0x9F5F_DB0D, 0xE33E_FED6, 0x7E31_1FA0,
|
||||
0xC2CA_1813, 0x5FC5_F965, 0x23A4_DCBE, 0xBEAB_3DC8, 0xDB66_9708, 0x4669_767E, 0x3A08_53A5, 0xA707_B2D3,
|
||||
0xF193_0625, 0x6C9C_E753, 0x10FD_C288, 0x8DF2_23FE, 0xE83F_893E, 0x7530_6848, 0x0951_4D93, 0x945E_ACE5,
|
||||
0xA478_247F, 0x3977_C509, 0x4516_E0D2, 0xD819_01A4, 0xBDD4_AB64, 0x20DB_4A12, 0x5CBA_6FC9, 0xC1B5_8EBF,
|
||||
0x9721_3A49, 0x0A2E_DB3F, 0x764F_FEE4, 0xEB40_1F92, 0x8E8D_B552, 0x1382_5424, 0x6FE3_71FF, 0xF2EC_9089,
|
||||
0x0FAE_60CB, 0x92A1_81BD, 0xEEC0_A466, 0x73CF_4510, 0x1602_EFD0, 0x8B0D_0EA6, 0xF76C_2B7D, 0x6A63_CA0B,
|
||||
0x3CF7_7EFD, 0xA1F8_9F8B, 0xDD99_BA50, 0x4096_5B26, 0x255B_F1E6, 0xB854_1090, 0xC435_354B, 0x593A_D43D,
|
||||
0x691C_5CA7, 0xF413_BDD1, 0x8872_980A, 0x157D_797C, 0x70B0_D3BC, 0xEDBF_32CA, 0x91DE_1711, 0x0CD1_F667,
|
||||
0x5A45_4291, 0xC74A_A3E7, 0xBB2B_863C, 0x2624_674A, 0x43E9_CD8A, 0xDEE6_2CFC, 0xA287_0927, 0x3F88_E851,
|
||||
],[
|
||||
0x0000_0000, 0xB9FB_DBE8, 0xA886_B191, 0x117D_6A79, 0x8A7C_6563, 0x3387_BE8B, 0x22FA_D4F2, 0x9B01_0F1A,
|
||||
0xCF89_CC87, 0x7672_176F, 0x670F_7D16, 0xDEF4_A6FE, 0x45F5_A9E4, 0xFC0E_720C, 0xED73_1875, 0x5488_C39D,
|
||||
0x4462_9F4F, 0xFD99_44A7, 0xECE4_2EDE, 0x551F_F536, 0xCE1E_FA2C, 0x77E5_21C4, 0x6698_4BBD, 0xDF63_9055,
|
||||
0x8BEB_53C8, 0x3210_8820, 0x236D_E259, 0x9A96_39B1, 0x0197_36AB, 0xB86C_ED43, 0xA911_873A, 0x10EA_5CD2,
|
||||
0x88C5_3E9E, 0x313E_E576, 0x2043_8F0F, 0x99B8_54E7, 0x02B9_5BFD, 0xBB42_8015, 0xAA3F_EA6C, 0x13C4_3184,
|
||||
0x474C_F219, 0xFEB7_29F1, 0xEFCA_4388, 0x5631_9860, 0xCD30_977A, 0x74CB_4C92, 0x65B6_26EB, 0xDC4D_FD03,
|
||||
0xCCA7_A1D1, 0x755C_7A39, 0x6421_1040, 0xDDDA_CBA8, 0x46DB_C4B2, 0xFF20_1F5A, 0xEE5D_7523, 0x57A6_AECB,
|
||||
0x032E_6D56, 0xBAD5_B6BE, 0xABA8_DCC7, 0x1253_072F, 0x8952_0835, 0x30A9_D3DD, 0x21D4_B9A4, 0x982F_624C,
|
||||
0xCAFB_7B7D, 0x7300_A095, 0x627D_CAEC, 0xDB86_1104, 0x4087_1E1E, 0xF97C_C5F6, 0xE801_AF8F, 0x51FA_7467,
|
||||
0x0572_B7FA, 0xBC89_6C12, 0xADF4_066B, 0x140F_DD83, 0x8F0E_D299, 0x36F5_0971, 0x2788_6308, 0x9E73_B8E0,
|
||||
0x8E99_E432, 0x3762_3FDA, 0x261F_55A3, 0x9FE4_8E4B, 0x04E5_8151, 0xBD1E_5AB9, 0xAC63_30C0, 0x1598_EB28,
|
||||
0x4110_28B5, 0xF8EB_F35D, 0xE996_9924, 0x506D_42CC, 0xCB6C_4DD6, 0x7297_963E, 0x63EA_FC47, 0xDA11_27AF,
|
||||
0x423E_45E3, 0xFBC5_9E0B, 0xEAB8_F472, 0x5343_2F9A, 0xC842_2080, 0x71B9_FB68, 0x60C4_9111, 0xD93F_4AF9,
|
||||
0x8DB7_8964, 0x344C_528C, 0x2531_38F5, 0x9CCA_E31D, 0x07CB_EC07, 0xBE30_37EF, 0xAF4D_5D96, 0x16B6_867E,
|
||||
0x065C_DAAC, 0xBFA7_0144, 0xAEDA_6B3D, 0x1721_B0D5, 0x8C20_BFCF, 0x35DB_6427, 0x24A6_0E5E, 0x9D5D_D5B6,
|
||||
0xC9D5_162B, 0x702E_CDC3, 0x6153_A7BA, 0xD8A8_7C52, 0x43A9_7348, 0xFA52_A8A0, 0xEB2F_C2D9, 0x52D4_1931,
|
||||
0x4E87_F0BB, 0xF77C_2B53, 0xE601_412A, 0x5FFA_9AC2, 0xC4FB_95D8, 0x7D00_4E30, 0x6C7D_2449, 0xD586_FFA1,
|
||||
0x810E_3C3C, 0x38F5_E7D4, 0x2988_8DAD, 0x9073_5645, 0x0B72_595F, 0xB289_82B7, 0xA3F4_E8CE, 0x1A0F_3326,
|
||||
0x0AE5_6FF4, 0xB31E_B41C, 0xA263_DE65, 0x1B98_058D, 0x8099_0A97, 0x3962_D17F, 0x281F_BB06, 0x91E4_60EE,
|
||||
0xC56C_A373, 0x7C97_789B, 0x6DEA_12E2, 0xD411_C90A, 0x4F10_C610, 0xF6EB_1DF8, 0xE796_7781, 0x5E6D_AC69,
|
||||
0xC642_CE25, 0x7FB9_15CD, 0x6EC4_7FB4, 0xD73F_A45C, 0x4C3E_AB46, 0xF5C5_70AE, 0xE4B8_1AD7, 0x5D43_C13F,
|
||||
0x09CB_02A2, 0xB030_D94A, 0xA14D_B333, 0x18B6_68DB, 0x83B7_67C1, 0x3A4C_BC29, 0x2B31_D650, 0x92CA_0DB8,
|
||||
0x8220_516A, 0x3BDB_8A82, 0x2AA6_E0FB, 0x935D_3B13, 0x085C_3409, 0xB1A7_EFE1, 0xA0DA_8598, 0x1921_5E70,
|
||||
0x4DA9_9DED, 0xF452_4605, 0xE52F_2C7C, 0x5CD4_F794, 0xC7D5_F88E, 0x7E2E_2366, 0x6F53_491F, 0xD6A8_92F7,
|
||||
0x847C_8BC6, 0x3D87_502E, 0x2CFA_3A57, 0x9501_E1BF, 0x0E00_EEA5, 0xB7FB_354D, 0xA686_5F34, 0x1F7D_84DC,
|
||||
0x4BF5_4741, 0xF20E_9CA9, 0xE373_F6D0, 0x5A88_2D38, 0xC189_2222, 0x7872_F9CA, 0x690F_93B3, 0xD0F4_485B,
|
||||
0xC01E_1489, 0x79E5_CF61, 0x6898_A518, 0xD163_7EF0, 0x4A62_71EA, 0xF399_AA02, 0xE2E4_C07B, 0x5B1F_1B93,
|
||||
0x0F97_D80E, 0xB66C_03E6, 0xA711_699F, 0x1EEA_B277, 0x85EB_BD6D, 0x3C10_6685, 0x2D6D_0CFC, 0x9496_D714,
|
||||
0x0CB9_B558, 0xB542_6EB0, 0xA43F_04C9, 0x1DC4_DF21, 0x86C5_D03B, 0x3F3E_0BD3, 0x2E43_61AA, 0x97B8_BA42,
|
||||
0xC330_79DF, 0x7ACB_A237, 0x6BB6_C84E, 0xD24D_13A6, 0x494C_1CBC, 0xF0B7_C754, 0xE1CA_AD2D, 0x5831_76C5,
|
||||
0x48DB_2A17, 0xF120_F1FF, 0xE05D_9B86, 0x59A6_406E, 0xC2A7_4F74, 0x7B5C_949C, 0x6A21_FEE5, 0xD3DA_250D,
|
||||
0x8752_E690, 0x3EA9_3D78, 0x2FD4_5701, 0x962F_8CE9, 0x0D2E_83F3, 0xB4D5_581B, 0xA5A8_3262, 0x1C53_E98A,
|
||||
0x0000_0000, 0xB9FB_DBE8, 0xA886_B191, 0x117D_6A79, 0x8A7C_6563, 0x3387_BE8B, 0x22FA_D4F2, 0x9B01_0F1A,
|
||||
0xCF89_CC87, 0x7672_176F, 0x670F_7D16, 0xDEF4_A6FE, 0x45F5_A9E4, 0xFC0E_720C, 0xED73_1875, 0x5488_C39D,
|
||||
0x4462_9F4F, 0xFD99_44A7, 0xECE4_2EDE, 0x551F_F536, 0xCE1E_FA2C, 0x77E5_21C4, 0x6698_4BBD, 0xDF63_9055,
|
||||
0x8BEB_53C8, 0x3210_8820, 0x236D_E259, 0x9A96_39B1, 0x0197_36AB, 0xB86C_ED43, 0xA911_873A, 0x10EA_5CD2,
|
||||
0x88C5_3E9E, 0x313E_E576, 0x2043_8F0F, 0x99B8_54E7, 0x02B9_5BFD, 0xBB42_8015, 0xAA3F_EA6C, 0x13C4_3184,
|
||||
0x474C_F219, 0xFEB7_29F1, 0xEFCA_4388, 0x5631_9860, 0xCD30_977A, 0x74CB_4C92, 0x65B6_26EB, 0xDC4D_FD03,
|
||||
0xCCA7_A1D1, 0x755C_7A39, 0x6421_1040, 0xDDDA_CBA8, 0x46DB_C4B2, 0xFF20_1F5A, 0xEE5D_7523, 0x57A6_AECB,
|
||||
0x032E_6D56, 0xBAD5_B6BE, 0xABA8_DCC7, 0x1253_072F, 0x8952_0835, 0x30A9_D3DD, 0x21D4_B9A4, 0x982F_624C,
|
||||
0xCAFB_7B7D, 0x7300_A095, 0x627D_CAEC, 0xDB86_1104, 0x4087_1E1E, 0xF97C_C5F6, 0xE801_AF8F, 0x51FA_7467,
|
||||
0x0572_B7FA, 0xBC89_6C12, 0xADF4_066B, 0x140F_DD83, 0x8F0E_D299, 0x36F5_0971, 0x2788_6308, 0x9E73_B8E0,
|
||||
0x8E99_E432, 0x3762_3FDA, 0x261F_55A3, 0x9FE4_8E4B, 0x04E5_8151, 0xBD1E_5AB9, 0xAC63_30C0, 0x1598_EB28,
|
||||
0x4110_28B5, 0xF8EB_F35D, 0xE996_9924, 0x506D_42CC, 0xCB6C_4DD6, 0x7297_963E, 0x63EA_FC47, 0xDA11_27AF,
|
||||
0x423E_45E3, 0xFBC5_9E0B, 0xEAB8_F472, 0x5343_2F9A, 0xC842_2080, 0x71B9_FB68, 0x60C4_9111, 0xD93F_4AF9,
|
||||
0x8DB7_8964, 0x344C_528C, 0x2531_38F5, 0x9CCA_E31D, 0x07CB_EC07, 0xBE30_37EF, 0xAF4D_5D96, 0x16B6_867E,
|
||||
0x065C_DAAC, 0xBFA7_0144, 0xAEDA_6B3D, 0x1721_B0D5, 0x8C20_BFCF, 0x35DB_6427, 0x24A6_0E5E, 0x9D5D_D5B6,
|
||||
0xC9D5_162B, 0x702E_CDC3, 0x6153_A7BA, 0xD8A8_7C52, 0x43A9_7348, 0xFA52_A8A0, 0xEB2F_C2D9, 0x52D4_1931,
|
||||
0x4E87_F0BB, 0xF77C_2B53, 0xE601_412A, 0x5FFA_9AC2, 0xC4FB_95D8, 0x7D00_4E30, 0x6C7D_2449, 0xD586_FFA1,
|
||||
0x810E_3C3C, 0x38F5_E7D4, 0x2988_8DAD, 0x9073_5645, 0x0B72_595F, 0xB289_82B7, 0xA3F4_E8CE, 0x1A0F_3326,
|
||||
0x0AE5_6FF4, 0xB31E_B41C, 0xA263_DE65, 0x1B98_058D, 0x8099_0A97, 0x3962_D17F, 0x281F_BB06, 0x91E4_60EE,
|
||||
0xC56C_A373, 0x7C97_789B, 0x6DEA_12E2, 0xD411_C90A, 0x4F10_C610, 0xF6EB_1DF8, 0xE796_7781, 0x5E6D_AC69,
|
||||
0xC642_CE25, 0x7FB9_15CD, 0x6EC4_7FB4, 0xD73F_A45C, 0x4C3E_AB46, 0xF5C5_70AE, 0xE4B8_1AD7, 0x5D43_C13F,
|
||||
0x09CB_02A2, 0xB030_D94A, 0xA14D_B333, 0x18B6_68DB, 0x83B7_67C1, 0x3A4C_BC29, 0x2B31_D650, 0x92CA_0DB8,
|
||||
0x8220_516A, 0x3BDB_8A82, 0x2AA6_E0FB, 0x935D_3B13, 0x085C_3409, 0xB1A7_EFE1, 0xA0DA_8598, 0x1921_5E70,
|
||||
0x4DA9_9DED, 0xF452_4605, 0xE52F_2C7C, 0x5CD4_F794, 0xC7D5_F88E, 0x7E2E_2366, 0x6F53_491F, 0xD6A8_92F7,
|
||||
0x847C_8BC6, 0x3D87_502E, 0x2CFA_3A57, 0x9501_E1BF, 0x0E00_EEA5, 0xB7FB_354D, 0xA686_5F34, 0x1F7D_84DC,
|
||||
0x4BF5_4741, 0xF20E_9CA9, 0xE373_F6D0, 0x5A88_2D38, 0xC189_2222, 0x7872_F9CA, 0x690F_93B3, 0xD0F4_485B,
|
||||
0xC01E_1489, 0x79E5_CF61, 0x6898_A518, 0xD163_7EF0, 0x4A62_71EA, 0xF399_AA02, 0xE2E4_C07B, 0x5B1F_1B93,
|
||||
0x0F97_D80E, 0xB66C_03E6, 0xA711_699F, 0x1EEA_B277, 0x85EB_BD6D, 0x3C10_6685, 0x2D6D_0CFC, 0x9496_D714,
|
||||
0x0CB9_B558, 0xB542_6EB0, 0xA43F_04C9, 0x1DC4_DF21, 0x86C5_D03B, 0x3F3E_0BD3, 0x2E43_61AA, 0x97B8_BA42,
|
||||
0xC330_79DF, 0x7ACB_A237, 0x6BB6_C84E, 0xD24D_13A6, 0x494C_1CBC, 0xF0B7_C754, 0xE1CA_AD2D, 0x5831_76C5,
|
||||
0x48DB_2A17, 0xF120_F1FF, 0xE05D_9B86, 0x59A6_406E, 0xC2A7_4F74, 0x7B5C_949C, 0x6A21_FEE5, 0xD3DA_250D,
|
||||
0x8752_E690, 0x3EA9_3D78, 0x2FD4_5701, 0x962F_8CE9, 0x0D2E_83F3, 0xB4D5_581B, 0xA5A8_3262, 0x1C53_E98A,
|
||||
],[
|
||||
0x0000_0000, 0xAE68_9191, 0x87A0_2563, 0x29C8_B4F2, 0xD431_4C87, 0x7A59_DD16, 0x5391_69E4, 0xFDF9_F875,
|
||||
0x7313_9F4F, 0xDD7B_0EDE, 0xF4B3_BA2C, 0x5ADB_2BBD, 0xA722_D3C8, 0x094A_4259, 0x2082_F6AB, 0x8EEA_673A,
|
||||
0xE627_3E9E, 0x484F_AF0F, 0x6187_1BFD, 0xCFEF_8A6C, 0x3216_7219, 0x9C7E_E388, 0xB5B6_577A, 0x1BDE_C6EB,
|
||||
0x9534_A1D1, 0x3B5C_3040, 0x1294_84B2, 0xBCFC_1523, 0x4105_ED56, 0xEF6D_7CC7, 0xC6A5_C835, 0x68CD_59A4,
|
||||
0x173F_7B7D, 0xB957_EAEC, 0x909F_5E1E, 0x3EF7_CF8F, 0xC30E_37FA, 0x6D66_A66B, 0x44AE_1299, 0xEAC6_8308,
|
||||
0x642C_E432, 0xCA44_75A3, 0xE38C_C151, 0x4DE4_50C0, 0xB01D_A8B5, 0x1E75_3924, 0x37BD_8DD6, 0x99D5_1C47,
|
||||
0xF118_45E3, 0x5F70_D472, 0x76B8_6080, 0xD8D0_F111, 0x2529_0964, 0x8B41_98F5, 0xA289_2C07, 0x0CE1_BD96,
|
||||
0x820B_DAAC, 0x2C63_4B3D, 0x05AB_FFCF, 0xABC3_6E5E, 0x563A_962B, 0xF852_07BA, 0xD19A_B348, 0x7FF2_22D9,
|
||||
0x2E7E_F6FA, 0x8016_676B, 0xA9DE_D399, 0x07B6_4208, 0xFA4F_BA7D, 0x5427_2BEC, 0x7DEF_9F1E, 0xD387_0E8F,
|
||||
0x5D6D_69B5, 0xF305_F824, 0xDACD_4CD6, 0x74A5_DD47, 0x895C_2532, 0x2734_B4A3, 0x0EFC_0051, 0xA094_91C0,
|
||||
0xC859_C864, 0x6631_59F5, 0x4FF9_ED07, 0xE191_7C96, 0x1C68_84E3, 0xB200_1572, 0x9BC8_A180, 0x35A0_3011,
|
||||
0xBB4A_572B, 0x1522_C6BA, 0x3CEA_7248, 0x9282_E3D9, 0x6F7B_1BAC, 0xC113_8A3D, 0xE8DB_3ECF, 0x46B3_AF5E,
|
||||
0x3941_8D87, 0x9729_1C16, 0xBEE1_A8E4, 0x1089_3975, 0xED70_C100, 0x4318_5091, 0x6AD0_E463, 0xC4B8_75F2,
|
||||
0x4A52_12C8, 0xE43A_8359, 0xCDF2_37AB, 0x639A_A63A, 0x9E63_5E4F, 0x300B_CFDE, 0x19C3_7B2C, 0xB7AB_EABD,
|
||||
0xDF66_B319, 0x710E_2288, 0x58C6_967A, 0xF6AE_07EB, 0x0B57_FF9E, 0xA53F_6E0F, 0x8CF7_DAFD, 0x229F_4B6C,
|
||||
0xAC75_2C56, 0x021D_BDC7, 0x2BD5_0935, 0x85BD_98A4, 0x7844_60D1, 0xD62C_F140, 0xFFE4_45B2, 0x518C_D423,
|
||||
0x5CFD_EDF4, 0xF295_7C65, 0xDB5D_C897, 0x7535_5906, 0x88CC_A173, 0x26A4_30E2, 0x0F6C_8410, 0xA104_1581,
|
||||
0x2FEE_72BB, 0x8186_E32A, 0xA84E_57D8, 0x0626_C649, 0xFBDF_3E3C, 0x55B7_AFAD, 0x7C7F_1B5F, 0xD217_8ACE,
|
||||
0xBADA_D36A, 0x14B2_42FB, 0x3D7A_F609, 0x9312_6798, 0x6EEB_9FED, 0xC083_0E7C, 0xE94B_BA8E, 0x4723_2B1F,
|
||||
0xC9C9_4C25, 0x67A1_DDB4, 0x4E69_6946, 0xE001_F8D7, 0x1DF8_00A2, 0xB390_9133, 0x9A58_25C1, 0x3430_B450,
|
||||
0x4BC2_9689, 0xE5AA_0718, 0xCC62_B3EA, 0x620A_227B, 0x9FF3_DA0E, 0x319B_4B9F, 0x1853_FF6D, 0xB63B_6EFC,
|
||||
0x38D1_09C6, 0x96B9_9857, 0xBF71_2CA5, 0x1119_BD34, 0xECE0_4541, 0x4288_D4D0, 0x6B40_6022, 0xC528_F1B3,
|
||||
0xADE5_A817, 0x038D_3986, 0x2A45_8D74, 0x842D_1CE5, 0x79D4_E490, 0xD7BC_7501, 0xFE74_C1F3, 0x501C_5062,
|
||||
0xDEF6_3758, 0x709E_A6C9, 0x5956_123B, 0xF73E_83AA, 0x0AC7_7BDF, 0xA4AF_EA4E, 0x8D67_5EBC, 0x230F_CF2D,
|
||||
0x7283_1B0E, 0xDCEB_8A9F, 0xF523_3E6D, 0x5B4B_AFFC, 0xA6B2_5789, 0x08DA_C618, 0x2112_72EA, 0x8F7A_E37B,
|
||||
0x0190_8441, 0xAFF8_15D0, 0x8630_A122, 0x2858_30B3, 0xD5A1_C8C6, 0x7BC9_5957, 0x5201_EDA5, 0xFC69_7C34,
|
||||
0x94A4_2590, 0x3ACC_B401, 0x1304_00F3, 0xBD6C_9162, 0x4095_6917, 0xEEFD_F886, 0xC735_4C74, 0x695D_DDE5,
|
||||
0xE7B7_BADF, 0x49DF_2B4E, 0x6017_9FBC, 0xCE7F_0E2D, 0x3386_F658, 0x9DEE_67C9, 0xB426_D33B, 0x1A4E_42AA,
|
||||
0x65BC_6073, 0xCBD4_F1E2, 0xE21C_4510, 0x4C74_D481, 0xB18D_2CF4, 0x1FE5_BD65, 0x362D_0997, 0x9845_9806,
|
||||
0x16AF_FF3C, 0xB8C7_6EAD, 0x910F_DA5F, 0x3F67_4BCE, 0xC29E_B3BB, 0x6CF6_222A, 0x453E_96D8, 0xEB56_0749,
|
||||
0x839B_5EED, 0x2DF3_CF7C, 0x043B_7B8E, 0xAA53_EA1F, 0x57AA_126A, 0xF9C2_83FB, 0xD00A_3709, 0x7E62_A698,
|
||||
0xF088_C1A2, 0x5EE0_5033, 0x7728_E4C1, 0xD940_7550, 0x24B9_8D25, 0x8AD1_1CB4, 0xA319_A846, 0x0D71_39D7,
|
||||
0x0000_0000, 0xAE68_9191, 0x87A0_2563, 0x29C8_B4F2, 0xD431_4C87, 0x7A59_DD16, 0x5391_69E4, 0xFDF9_F875,
|
||||
0x7313_9F4F, 0xDD7B_0EDE, 0xF4B3_BA2C, 0x5ADB_2BBD, 0xA722_D3C8, 0x094A_4259, 0x2082_F6AB, 0x8EEA_673A,
|
||||
0xE627_3E9E, 0x484F_AF0F, 0x6187_1BFD, 0xCFEF_8A6C, 0x3216_7219, 0x9C7E_E388, 0xB5B6_577A, 0x1BDE_C6EB,
|
||||
0x9534_A1D1, 0x3B5C_3040, 0x1294_84B2, 0xBCFC_1523, 0x4105_ED56, 0xEF6D_7CC7, 0xC6A5_C835, 0x68CD_59A4,
|
||||
0x173F_7B7D, 0xB957_EAEC, 0x909F_5E1E, 0x3EF7_CF8F, 0xC30E_37FA, 0x6D66_A66B, 0x44AE_1299, 0xEAC6_8308,
|
||||
0x642C_E432, 0xCA44_75A3, 0xE38C_C151, 0x4DE4_50C0, 0xB01D_A8B5, 0x1E75_3924, 0x37BD_8DD6, 0x99D5_1C47,
|
||||
0xF118_45E3, 0x5F70_D472, 0x76B8_6080, 0xD8D0_F111, 0x2529_0964, 0x8B41_98F5, 0xA289_2C07, 0x0CE1_BD96,
|
||||
0x820B_DAAC, 0x2C63_4B3D, 0x05AB_FFCF, 0xABC3_6E5E, 0x563A_962B, 0xF852_07BA, 0xD19A_B348, 0x7FF2_22D9,
|
||||
0x2E7E_F6FA, 0x8016_676B, 0xA9DE_D399, 0x07B6_4208, 0xFA4F_BA7D, 0x5427_2BEC, 0x7DEF_9F1E, 0xD387_0E8F,
|
||||
0x5D6D_69B5, 0xF305_F824, 0xDACD_4CD6, 0x74A5_DD47, 0x895C_2532, 0x2734_B4A3, 0x0EFC_0051, 0xA094_91C0,
|
||||
0xC859_C864, 0x6631_59F5, 0x4FF9_ED07, 0xE191_7C96, 0x1C68_84E3, 0xB200_1572, 0x9BC8_A180, 0x35A0_3011,
|
||||
0xBB4A_572B, 0x1522_C6BA, 0x3CEA_7248, 0x9282_E3D9, 0x6F7B_1BAC, 0xC113_8A3D, 0xE8DB_3ECF, 0x46B3_AF5E,
|
||||
0x3941_8D87, 0x9729_1C16, 0xBEE1_A8E4, 0x1089_3975, 0xED70_C100, 0x4318_5091, 0x6AD0_E463, 0xC4B8_75F2,
|
||||
0x4A52_12C8, 0xE43A_8359, 0xCDF2_37AB, 0x639A_A63A, 0x9E63_5E4F, 0x300B_CFDE, 0x19C3_7B2C, 0xB7AB_EABD,
|
||||
0xDF66_B319, 0x710E_2288, 0x58C6_967A, 0xF6AE_07EB, 0x0B57_FF9E, 0xA53F_6E0F, 0x8CF7_DAFD, 0x229F_4B6C,
|
||||
0xAC75_2C56, 0x021D_BDC7, 0x2BD5_0935, 0x85BD_98A4, 0x7844_60D1, 0xD62C_F140, 0xFFE4_45B2, 0x518C_D423,
|
||||
0x5CFD_EDF4, 0xF295_7C65, 0xDB5D_C897, 0x7535_5906, 0x88CC_A173, 0x26A4_30E2, 0x0F6C_8410, 0xA104_1581,
|
||||
0x2FEE_72BB, 0x8186_E32A, 0xA84E_57D8, 0x0626_C649, 0xFBDF_3E3C, 0x55B7_AFAD, 0x7C7F_1B5F, 0xD217_8ACE,
|
||||
0xBADA_D36A, 0x14B2_42FB, 0x3D7A_F609, 0x9312_6798, 0x6EEB_9FED, 0xC083_0E7C, 0xE94B_BA8E, 0x4723_2B1F,
|
||||
0xC9C9_4C25, 0x67A1_DDB4, 0x4E69_6946, 0xE001_F8D7, 0x1DF8_00A2, 0xB390_9133, 0x9A58_25C1, 0x3430_B450,
|
||||
0x4BC2_9689, 0xE5AA_0718, 0xCC62_B3EA, 0x620A_227B, 0x9FF3_DA0E, 0x319B_4B9F, 0x1853_FF6D, 0xB63B_6EFC,
|
||||
0x38D1_09C6, 0x96B9_9857, 0xBF71_2CA5, 0x1119_BD34, 0xECE0_4541, 0x4288_D4D0, 0x6B40_6022, 0xC528_F1B3,
|
||||
0xADE5_A817, 0x038D_3986, 0x2A45_8D74, 0x842D_1CE5, 0x79D4_E490, 0xD7BC_7501, 0xFE74_C1F3, 0x501C_5062,
|
||||
0xDEF6_3758, 0x709E_A6C9, 0x5956_123B, 0xF73E_83AA, 0x0AC7_7BDF, 0xA4AF_EA4E, 0x8D67_5EBC, 0x230F_CF2D,
|
||||
0x7283_1B0E, 0xDCEB_8A9F, 0xF523_3E6D, 0x5B4B_AFFC, 0xA6B2_5789, 0x08DA_C618, 0x2112_72EA, 0x8F7A_E37B,
|
||||
0x0190_8441, 0xAFF8_15D0, 0x8630_A122, 0x2858_30B3, 0xD5A1_C8C6, 0x7BC9_5957, 0x5201_EDA5, 0xFC69_7C34,
|
||||
0x94A4_2590, 0x3ACC_B401, 0x1304_00F3, 0xBD6C_9162, 0x4095_6917, 0xEEFD_F886, 0xC735_4C74, 0x695D_DDE5,
|
||||
0xE7B7_BADF, 0x49DF_2B4E, 0x6017_9FBC, 0xCE7F_0E2D, 0x3386_F658, 0x9DEE_67C9, 0xB426_D33B, 0x1A4E_42AA,
|
||||
0x65BC_6073, 0xCBD4_F1E2, 0xE21C_4510, 0x4C74_D481, 0xB18D_2CF4, 0x1FE5_BD65, 0x362D_0997, 0x9845_9806,
|
||||
0x16AF_FF3C, 0xB8C7_6EAD, 0x910F_DA5F, 0x3F67_4BCE, 0xC29E_B3BB, 0x6CF6_222A, 0x453E_96D8, 0xEB56_0749,
|
||||
0x839B_5EED, 0x2DF3_CF7C, 0x043B_7B8E, 0xAA53_EA1F, 0x57AA_126A, 0xF9C2_83FB, 0xD00A_3709, 0x7E62_A698,
|
||||
0xF088_C1A2, 0x5EE0_5033, 0x7728_E4C1, 0xD940_7550, 0x24B9_8D25, 0x8AD1_1CB4, 0xA319_A846, 0x0D71_39D7,
|
||||
]]
|
||||
@@ -13,26 +13,26 @@
|
||||
// limitations under the License.
|
||||
|
||||
pri func ieee_hasher.up_arm_crc32!(x: slice base.u8),
|
||||
choose cpu_arch >= arm_crc32,
|
||||
choose cpu_arch >= arm_crc32,
|
||||
{
|
||||
var p : slice base.u8
|
||||
var p : slice base.u8
|
||||
|
||||
var util : base.arm_crc32_utility
|
||||
var s : base.arm_crc32_u32
|
||||
var util : base.arm_crc32_utility
|
||||
var s : base.arm_crc32_u32
|
||||
|
||||
s = util.make_u32(a: 0xFFFF_FFFF ^ this.state)
|
||||
s = util.make_u32(a: 0xFFFF_FFFF ^ this.state)
|
||||
|
||||
// Align to a 16-byte boundary.
|
||||
while (args.x.length() > 0) and ((15 & args.x.uintptr_low_12_bits()) <> 0) {
|
||||
s = s.crc32b(b: args.x[0])
|
||||
args.x = args.x[1 ..]
|
||||
} endwhile
|
||||
// Align to a 16-byte boundary.
|
||||
while (args.x.length() > 0) and ((15 & args.x.uintptr_low_12_bits()) <> 0) {
|
||||
s = s.crc32b(b: args.x[0])
|
||||
args.x = args.x[1 ..]
|
||||
} endwhile
|
||||
|
||||
iterate (p = args.x)(length: 8, advance: 8, unroll: 16) {
|
||||
s = s.crc32d(b: p.peek_u64le())
|
||||
} else (length: 1, advance: 1, unroll: 1) {
|
||||
s = s.crc32b(b: p[0])
|
||||
}
|
||||
iterate (p = args.x)(length: 8, advance: 8, unroll: 16) {
|
||||
s = s.crc32d(b: p.peek_u64le())
|
||||
} else (length: 1, advance: 1, unroll: 1) {
|
||||
s = s.crc32b(b: p[0])
|
||||
}
|
||||
|
||||
this.state = 0xFFFF_FFFF ^ s.value()
|
||||
this.state = 0xFFFF_FFFF ^ s.value()
|
||||
}
|
||||
@@ -21,118 +21,118 @@
|
||||
// cpu_arch >= x86_avx2". With AVX, PCLMULQDQ has a three-operand form, not
|
||||
// just a two-operand form: https://www.felixcloutier.com/x86/pclmulqdq
|
||||
pri func ieee_hasher.up_x86_avx2!(x: slice base.u8),
|
||||
choose cpu_arch >= x86_avx2,
|
||||
choose cpu_arch >= x86_avx2,
|
||||
{
|
||||
var s : base.u32
|
||||
var p : slice base.u8
|
||||
var s : base.u32
|
||||
var p : slice base.u8
|
||||
|
||||
var util : base.x86_sse42_utility
|
||||
var k : base.x86_m128i
|
||||
var x0 : base.x86_m128i
|
||||
var x1 : base.x86_m128i
|
||||
var x2 : base.x86_m128i
|
||||
var x3 : base.x86_m128i
|
||||
var y0 : base.x86_m128i
|
||||
var y1 : base.x86_m128i
|
||||
var y2 : base.x86_m128i
|
||||
var y3 : base.x86_m128i
|
||||
var util : base.x86_sse42_utility
|
||||
var k : base.x86_m128i
|
||||
var x0 : base.x86_m128i
|
||||
var x1 : base.x86_m128i
|
||||
var x2 : base.x86_m128i
|
||||
var x3 : base.x86_m128i
|
||||
var y0 : base.x86_m128i
|
||||
var y1 : base.x86_m128i
|
||||
var y2 : base.x86_m128i
|
||||
var y3 : base.x86_m128i
|
||||
|
||||
var tail_index : base.u64
|
||||
var tail_index : base.u64
|
||||
|
||||
s = 0xFFFF_FFFF ^ this.state
|
||||
s = 0xFFFF_FFFF ^ this.state
|
||||
|
||||
// Align to a 16-byte boundary.
|
||||
while (args.x.length() > 0) and ((15 & args.x.uintptr_low_12_bits()) <> 0) {
|
||||
s = IEEE_TABLE[0][((s & 0xFF) as base.u8) ^ args.x[0]] ^ (s >> 8)
|
||||
args.x = args.x[1 ..]
|
||||
} endwhile
|
||||
// Align to a 16-byte boundary.
|
||||
while (args.x.length() > 0) and ((15 & args.x.uintptr_low_12_bits()) <> 0) {
|
||||
s = IEEE_TABLE[0][((s & 0xFF) as base.u8) ^ args.x[0]] ^ (s >> 8)
|
||||
args.x = args.x[1 ..]
|
||||
} endwhile
|
||||
|
||||
// For short inputs, just do a simple loop.
|
||||
if args.x.length() < 64 {
|
||||
iterate (p = args.x)(length: 1, advance: 1, unroll: 1) {
|
||||
s = IEEE_TABLE[0][((s & 0xFF) as base.u8) ^ p[0]] ^ (s >> 8)
|
||||
}
|
||||
this.state = 0xFFFF_FFFF ^ s
|
||||
return nothing
|
||||
}
|
||||
// For short inputs, just do a simple loop.
|
||||
if args.x.length() < 64 {
|
||||
iterate (p = args.x)(length: 1, advance: 1, unroll: 1) {
|
||||
s = IEEE_TABLE[0][((s & 0xFF) as base.u8) ^ p[0]] ^ (s >> 8)
|
||||
}
|
||||
this.state = 0xFFFF_FFFF ^ s
|
||||
return nothing
|
||||
}
|
||||
|
||||
// Load 128×4 = 512 bits from the first 64-byte chunk.
|
||||
x0 = util.make_m128i_slice128(a: args.x[0x00 .. 0x10])
|
||||
x1 = util.make_m128i_slice128(a: args.x[0x10 .. 0x20])
|
||||
x2 = util.make_m128i_slice128(a: args.x[0x20 .. 0x30])
|
||||
x3 = util.make_m128i_slice128(a: args.x[0x30 .. 0x40])
|
||||
// Load 128×4 = 512 bits from the first 64-byte chunk.
|
||||
x0 = util.make_m128i_slice128(a: args.x[0x00 .. 0x10])
|
||||
x1 = util.make_m128i_slice128(a: args.x[0x10 .. 0x20])
|
||||
x2 = util.make_m128i_slice128(a: args.x[0x20 .. 0x30])
|
||||
x3 = util.make_m128i_slice128(a: args.x[0x30 .. 0x40])
|
||||
|
||||
// Combine with the initial state.
|
||||
x0 = x0._mm_xor_si128(b: util.make_m128i_single_u32(a: s))
|
||||
// Combine with the initial state.
|
||||
x0 = x0._mm_xor_si128(b: util.make_m128i_single_u32(a: s))
|
||||
|
||||
// Process the remaining 64-byte chunks.
|
||||
k = util.make_m128i_slice128(a: IEEE_X86_SSE42_K1K2[.. 16])
|
||||
iterate (p = args.x[64 ..])(length: 64, advance: 64, unroll: 1) {
|
||||
y0 = x0._mm_clmulepi64_si128(b: k, imm8: 0x00)
|
||||
y1 = x1._mm_clmulepi64_si128(b: k, imm8: 0x00)
|
||||
y2 = x2._mm_clmulepi64_si128(b: k, imm8: 0x00)
|
||||
y3 = x3._mm_clmulepi64_si128(b: k, imm8: 0x00)
|
||||
// Process the remaining 64-byte chunks.
|
||||
k = util.make_m128i_slice128(a: IEEE_X86_SSE42_K1K2[.. 16])
|
||||
iterate (p = args.x[64 ..])(length: 64, advance: 64, unroll: 1) {
|
||||
y0 = x0._mm_clmulepi64_si128(b: k, imm8: 0x00)
|
||||
y1 = x1._mm_clmulepi64_si128(b: k, imm8: 0x00)
|
||||
y2 = x2._mm_clmulepi64_si128(b: k, imm8: 0x00)
|
||||
y3 = x3._mm_clmulepi64_si128(b: k, imm8: 0x00)
|
||||
|
||||
x0 = x0._mm_clmulepi64_si128(b: k, imm8: 0x11)
|
||||
x1 = x1._mm_clmulepi64_si128(b: k, imm8: 0x11)
|
||||
x2 = x2._mm_clmulepi64_si128(b: k, imm8: 0x11)
|
||||
x3 = x3._mm_clmulepi64_si128(b: k, imm8: 0x11)
|
||||
x0 = x0._mm_clmulepi64_si128(b: k, imm8: 0x11)
|
||||
x1 = x1._mm_clmulepi64_si128(b: k, imm8: 0x11)
|
||||
x2 = x2._mm_clmulepi64_si128(b: k, imm8: 0x11)
|
||||
x3 = x3._mm_clmulepi64_si128(b: k, imm8: 0x11)
|
||||
|
||||
x0 = x0._mm_xor_si128(b: y0)._mm_xor_si128(b: util.make_m128i_slice128(a: p[0x00 .. 0x10]))
|
||||
x1 = x1._mm_xor_si128(b: y1)._mm_xor_si128(b: util.make_m128i_slice128(a: p[0x10 .. 0x20]))
|
||||
x2 = x2._mm_xor_si128(b: y2)._mm_xor_si128(b: util.make_m128i_slice128(a: p[0x20 .. 0x30]))
|
||||
x3 = x3._mm_xor_si128(b: y3)._mm_xor_si128(b: util.make_m128i_slice128(a: p[0x30 .. 0x40]))
|
||||
}
|
||||
x0 = x0._mm_xor_si128(b: y0)._mm_xor_si128(b: util.make_m128i_slice128(a: p[0x00 .. 0x10]))
|
||||
x1 = x1._mm_xor_si128(b: y1)._mm_xor_si128(b: util.make_m128i_slice128(a: p[0x10 .. 0x20]))
|
||||
x2 = x2._mm_xor_si128(b: y2)._mm_xor_si128(b: util.make_m128i_slice128(a: p[0x20 .. 0x30]))
|
||||
x3 = x3._mm_xor_si128(b: y3)._mm_xor_si128(b: util.make_m128i_slice128(a: p[0x30 .. 0x40]))
|
||||
}
|
||||
|
||||
// Reduce 128×4 = 512 bits to 128 bits.
|
||||
k = util.make_m128i_slice128(a: IEEE_X86_SSE42_K3K4[.. 16])
|
||||
y0 = x0._mm_clmulepi64_si128(b: k, imm8: 0x00)
|
||||
x0 = x0._mm_clmulepi64_si128(b: k, imm8: 0x11)
|
||||
x0 = x0._mm_xor_si128(b: x1)
|
||||
x0 = x0._mm_xor_si128(b: y0)
|
||||
y0 = x0._mm_clmulepi64_si128(b: k, imm8: 0x00)
|
||||
x0 = x0._mm_clmulepi64_si128(b: k, imm8: 0x11)
|
||||
x0 = x0._mm_xor_si128(b: x2)
|
||||
x0 = x0._mm_xor_si128(b: y0)
|
||||
y0 = x0._mm_clmulepi64_si128(b: k, imm8: 0x00)
|
||||
x0 = x0._mm_clmulepi64_si128(b: k, imm8: 0x11)
|
||||
x0 = x0._mm_xor_si128(b: x3)
|
||||
x0 = x0._mm_xor_si128(b: y0)
|
||||
// Reduce 128×4 = 512 bits to 128 bits.
|
||||
k = util.make_m128i_slice128(a: IEEE_X86_SSE42_K3K4[.. 16])
|
||||
y0 = x0._mm_clmulepi64_si128(b: k, imm8: 0x00)
|
||||
x0 = x0._mm_clmulepi64_si128(b: k, imm8: 0x11)
|
||||
x0 = x0._mm_xor_si128(b: x1)
|
||||
x0 = x0._mm_xor_si128(b: y0)
|
||||
y0 = x0._mm_clmulepi64_si128(b: k, imm8: 0x00)
|
||||
x0 = x0._mm_clmulepi64_si128(b: k, imm8: 0x11)
|
||||
x0 = x0._mm_xor_si128(b: x2)
|
||||
x0 = x0._mm_xor_si128(b: y0)
|
||||
y0 = x0._mm_clmulepi64_si128(b: k, imm8: 0x00)
|
||||
x0 = x0._mm_clmulepi64_si128(b: k, imm8: 0x11)
|
||||
x0 = x0._mm_xor_si128(b: x3)
|
||||
x0 = x0._mm_xor_si128(b: y0)
|
||||
|
||||
// Reduce 128 bits to 64 bits.
|
||||
x1 = x0._mm_clmulepi64_si128(b: k, imm8: 0x10)
|
||||
x2 = util.make_m128i_multiple_u32(
|
||||
a00: 0xFFFF_FFFF,
|
||||
a01: 0x0000_0000,
|
||||
a02: 0xFFFF_FFFF,
|
||||
a03: 0x0000_0000)
|
||||
x0 = x0._mm_srli_si128(imm8: 8)
|
||||
x0 = x0._mm_xor_si128(b: x1)
|
||||
k = util.make_m128i_slice128(a: IEEE_X86_SSE42_K5ZZ[.. 16])
|
||||
x1 = x0._mm_srli_si128(imm8: 4)
|
||||
x0 = x0._mm_and_si128(b: x2)
|
||||
x0 = x0._mm_clmulepi64_si128(b: k, imm8: 0x00)
|
||||
x0 = x0._mm_xor_si128(b: x1)
|
||||
// Reduce 128 bits to 64 bits.
|
||||
x1 = x0._mm_clmulepi64_si128(b: k, imm8: 0x10)
|
||||
x2 = util.make_m128i_multiple_u32(
|
||||
a00: 0xFFFF_FFFF,
|
||||
a01: 0x0000_0000,
|
||||
a02: 0xFFFF_FFFF,
|
||||
a03: 0x0000_0000)
|
||||
x0 = x0._mm_srli_si128(imm8: 8)
|
||||
x0 = x0._mm_xor_si128(b: x1)
|
||||
k = util.make_m128i_slice128(a: IEEE_X86_SSE42_K5ZZ[.. 16])
|
||||
x1 = x0._mm_srli_si128(imm8: 4)
|
||||
x0 = x0._mm_and_si128(b: x2)
|
||||
x0 = x0._mm_clmulepi64_si128(b: k, imm8: 0x00)
|
||||
x0 = x0._mm_xor_si128(b: x1)
|
||||
|
||||
// Reduce 64 bits to 32 bits (Barrett Reduction) and extract.
|
||||
//
|
||||
// Barrett Reduction is Algorithm 1 (page 14) of Gopal et al., after
|
||||
// adjusting for bit-reflection as per Figure 12 (page 21).
|
||||
k = util.make_m128i_slice128(a: IEEE_X86_SSE42_PXMU[.. 16])
|
||||
x1 = x0._mm_and_si128(b: x2)
|
||||
x1 = x1._mm_clmulepi64_si128(b: k, imm8: 0x10)
|
||||
x1 = x1._mm_and_si128(b: x2)
|
||||
x1 = x1._mm_clmulepi64_si128(b: k, imm8: 0x00)
|
||||
x0 = x0._mm_xor_si128(b: x1)
|
||||
s = x0._mm_extract_epi32(imm8: 1)
|
||||
// Reduce 64 bits to 32 bits (Barrett Reduction) and extract.
|
||||
//
|
||||
// Barrett Reduction is Algorithm 1 (page 14) of Gopal et al., after
|
||||
// adjusting for bit-reflection as per Figure 12 (page 21).
|
||||
k = util.make_m128i_slice128(a: IEEE_X86_SSE42_PXMU[.. 16])
|
||||
x1 = x0._mm_and_si128(b: x2)
|
||||
x1 = x1._mm_clmulepi64_si128(b: k, imm8: 0x10)
|
||||
x1 = x1._mm_and_si128(b: x2)
|
||||
x1 = x1._mm_clmulepi64_si128(b: k, imm8: 0x00)
|
||||
x0 = x0._mm_xor_si128(b: x1)
|
||||
s = x0._mm_extract_epi32(imm8: 1)
|
||||
|
||||
// Handle the tail of args.x that wasn't a complete 64-byte chunk.
|
||||
tail_index = args.x.length() & 0xFFFF_FFFF_FFFF_FFC0 // And-not 64.
|
||||
if tail_index < args.x.length() {
|
||||
iterate (p = args.x[tail_index ..])(length: 1, advance: 1, unroll: 1) {
|
||||
s = IEEE_TABLE[0][((s & 0xFF) as base.u8) ^ p[0]] ^ (s >> 8)
|
||||
}
|
||||
}
|
||||
// Handle the tail of args.x that wasn't a complete 64-byte chunk.
|
||||
tail_index = args.x.length() & 0xFFFF_FFFF_FFFF_FFC0 // And-not 64.
|
||||
if tail_index < args.x.length() {
|
||||
iterate (p = args.x[tail_index ..])(length: 1, advance: 1, unroll: 1) {
|
||||
s = IEEE_TABLE[0][((s & 0xFF) as base.u8) ^ p[0]] ^ (s >> 8)
|
||||
}
|
||||
}
|
||||
|
||||
this.state = 0xFFFF_FFFF ^ s
|
||||
this.state = 0xFFFF_FFFF ^ s
|
||||
}
|
||||
+106
-106
@@ -18,120 +18,120 @@
|
||||
// Computation for Generic Polynomials Using PCLMULQDQ Instruction".
|
||||
|
||||
pri func ieee_hasher.up_x86_sse42!(x: slice base.u8),
|
||||
choose cpu_arch >= x86_sse42,
|
||||
choose cpu_arch >= x86_sse42,
|
||||
{
|
||||
var s : base.u32
|
||||
var p : slice base.u8
|
||||
var s : base.u32
|
||||
var p : slice base.u8
|
||||
|
||||
var util : base.x86_sse42_utility
|
||||
var k : base.x86_m128i
|
||||
var x0 : base.x86_m128i
|
||||
var x1 : base.x86_m128i
|
||||
var x2 : base.x86_m128i
|
||||
var x3 : base.x86_m128i
|
||||
var y0 : base.x86_m128i
|
||||
var y1 : base.x86_m128i
|
||||
var y2 : base.x86_m128i
|
||||
var y3 : base.x86_m128i
|
||||
var util : base.x86_sse42_utility
|
||||
var k : base.x86_m128i
|
||||
var x0 : base.x86_m128i
|
||||
var x1 : base.x86_m128i
|
||||
var x2 : base.x86_m128i
|
||||
var x3 : base.x86_m128i
|
||||
var y0 : base.x86_m128i
|
||||
var y1 : base.x86_m128i
|
||||
var y2 : base.x86_m128i
|
||||
var y3 : base.x86_m128i
|
||||
|
||||
var tail_index : base.u64
|
||||
var tail_index : base.u64
|
||||
|
||||
s = 0xFFFF_FFFF ^ this.state
|
||||
s = 0xFFFF_FFFF ^ this.state
|
||||
|
||||
// Align to a 16-byte boundary.
|
||||
while (args.x.length() > 0) and ((15 & args.x.uintptr_low_12_bits()) <> 0) {
|
||||
s = IEEE_TABLE[0][((s & 0xFF) as base.u8) ^ args.x[0]] ^ (s >> 8)
|
||||
args.x = args.x[1 ..]
|
||||
} endwhile
|
||||
// Align to a 16-byte boundary.
|
||||
while (args.x.length() > 0) and ((15 & args.x.uintptr_low_12_bits()) <> 0) {
|
||||
s = IEEE_TABLE[0][((s & 0xFF) as base.u8) ^ args.x[0]] ^ (s >> 8)
|
||||
args.x = args.x[1 ..]
|
||||
} endwhile
|
||||
|
||||
// For short inputs, just do a simple loop.
|
||||
if args.x.length() < 64 {
|
||||
iterate (p = args.x)(length: 1, advance: 1, unroll: 1) {
|
||||
s = IEEE_TABLE[0][((s & 0xFF) as base.u8) ^ p[0]] ^ (s >> 8)
|
||||
}
|
||||
this.state = 0xFFFF_FFFF ^ s
|
||||
return nothing
|
||||
}
|
||||
// For short inputs, just do a simple loop.
|
||||
if args.x.length() < 64 {
|
||||
iterate (p = args.x)(length: 1, advance: 1, unroll: 1) {
|
||||
s = IEEE_TABLE[0][((s & 0xFF) as base.u8) ^ p[0]] ^ (s >> 8)
|
||||
}
|
||||
this.state = 0xFFFF_FFFF ^ s
|
||||
return nothing
|
||||
}
|
||||
|
||||
// Load 128×4 = 512 bits from the first 64-byte chunk.
|
||||
x0 = util.make_m128i_slice128(a: args.x[0x00 .. 0x10])
|
||||
x1 = util.make_m128i_slice128(a: args.x[0x10 .. 0x20])
|
||||
x2 = util.make_m128i_slice128(a: args.x[0x20 .. 0x30])
|
||||
x3 = util.make_m128i_slice128(a: args.x[0x30 .. 0x40])
|
||||
// Load 128×4 = 512 bits from the first 64-byte chunk.
|
||||
x0 = util.make_m128i_slice128(a: args.x[0x00 .. 0x10])
|
||||
x1 = util.make_m128i_slice128(a: args.x[0x10 .. 0x20])
|
||||
x2 = util.make_m128i_slice128(a: args.x[0x20 .. 0x30])
|
||||
x3 = util.make_m128i_slice128(a: args.x[0x30 .. 0x40])
|
||||
|
||||
// Combine with the initial state.
|
||||
x0 = x0._mm_xor_si128(b: util.make_m128i_single_u32(a: s))
|
||||
// Combine with the initial state.
|
||||
x0 = x0._mm_xor_si128(b: util.make_m128i_single_u32(a: s))
|
||||
|
||||
// Process the remaining 64-byte chunks.
|
||||
k = util.make_m128i_slice128(a: IEEE_X86_SSE42_K1K2[.. 16])
|
||||
iterate (p = args.x[64 ..])(length: 64, advance: 64, unroll: 1) {
|
||||
y0 = x0._mm_clmulepi64_si128(b: k, imm8: 0x00)
|
||||
y1 = x1._mm_clmulepi64_si128(b: k, imm8: 0x00)
|
||||
y2 = x2._mm_clmulepi64_si128(b: k, imm8: 0x00)
|
||||
y3 = x3._mm_clmulepi64_si128(b: k, imm8: 0x00)
|
||||
// Process the remaining 64-byte chunks.
|
||||
k = util.make_m128i_slice128(a: IEEE_X86_SSE42_K1K2[.. 16])
|
||||
iterate (p = args.x[64 ..])(length: 64, advance: 64, unroll: 1) {
|
||||
y0 = x0._mm_clmulepi64_si128(b: k, imm8: 0x00)
|
||||
y1 = x1._mm_clmulepi64_si128(b: k, imm8: 0x00)
|
||||
y2 = x2._mm_clmulepi64_si128(b: k, imm8: 0x00)
|
||||
y3 = x3._mm_clmulepi64_si128(b: k, imm8: 0x00)
|
||||
|
||||
x0 = x0._mm_clmulepi64_si128(b: k, imm8: 0x11)
|
||||
x1 = x1._mm_clmulepi64_si128(b: k, imm8: 0x11)
|
||||
x2 = x2._mm_clmulepi64_si128(b: k, imm8: 0x11)
|
||||
x3 = x3._mm_clmulepi64_si128(b: k, imm8: 0x11)
|
||||
x0 = x0._mm_clmulepi64_si128(b: k, imm8: 0x11)
|
||||
x1 = x1._mm_clmulepi64_si128(b: k, imm8: 0x11)
|
||||
x2 = x2._mm_clmulepi64_si128(b: k, imm8: 0x11)
|
||||
x3 = x3._mm_clmulepi64_si128(b: k, imm8: 0x11)
|
||||
|
||||
x0 = x0._mm_xor_si128(b: y0)._mm_xor_si128(b: util.make_m128i_slice128(a: p[0x00 .. 0x10]))
|
||||
x1 = x1._mm_xor_si128(b: y1)._mm_xor_si128(b: util.make_m128i_slice128(a: p[0x10 .. 0x20]))
|
||||
x2 = x2._mm_xor_si128(b: y2)._mm_xor_si128(b: util.make_m128i_slice128(a: p[0x20 .. 0x30]))
|
||||
x3 = x3._mm_xor_si128(b: y3)._mm_xor_si128(b: util.make_m128i_slice128(a: p[0x30 .. 0x40]))
|
||||
}
|
||||
x0 = x0._mm_xor_si128(b: y0)._mm_xor_si128(b: util.make_m128i_slice128(a: p[0x00 .. 0x10]))
|
||||
x1 = x1._mm_xor_si128(b: y1)._mm_xor_si128(b: util.make_m128i_slice128(a: p[0x10 .. 0x20]))
|
||||
x2 = x2._mm_xor_si128(b: y2)._mm_xor_si128(b: util.make_m128i_slice128(a: p[0x20 .. 0x30]))
|
||||
x3 = x3._mm_xor_si128(b: y3)._mm_xor_si128(b: util.make_m128i_slice128(a: p[0x30 .. 0x40]))
|
||||
}
|
||||
|
||||
// Reduce 128×4 = 512 bits to 128 bits.
|
||||
k = util.make_m128i_slice128(a: IEEE_X86_SSE42_K3K4[.. 16])
|
||||
y0 = x0._mm_clmulepi64_si128(b: k, imm8: 0x00)
|
||||
x0 = x0._mm_clmulepi64_si128(b: k, imm8: 0x11)
|
||||
x0 = x0._mm_xor_si128(b: x1)
|
||||
x0 = x0._mm_xor_si128(b: y0)
|
||||
y0 = x0._mm_clmulepi64_si128(b: k, imm8: 0x00)
|
||||
x0 = x0._mm_clmulepi64_si128(b: k, imm8: 0x11)
|
||||
x0 = x0._mm_xor_si128(b: x2)
|
||||
x0 = x0._mm_xor_si128(b: y0)
|
||||
y0 = x0._mm_clmulepi64_si128(b: k, imm8: 0x00)
|
||||
x0 = x0._mm_clmulepi64_si128(b: k, imm8: 0x11)
|
||||
x0 = x0._mm_xor_si128(b: x3)
|
||||
x0 = x0._mm_xor_si128(b: y0)
|
||||
// Reduce 128×4 = 512 bits to 128 bits.
|
||||
k = util.make_m128i_slice128(a: IEEE_X86_SSE42_K3K4[.. 16])
|
||||
y0 = x0._mm_clmulepi64_si128(b: k, imm8: 0x00)
|
||||
x0 = x0._mm_clmulepi64_si128(b: k, imm8: 0x11)
|
||||
x0 = x0._mm_xor_si128(b: x1)
|
||||
x0 = x0._mm_xor_si128(b: y0)
|
||||
y0 = x0._mm_clmulepi64_si128(b: k, imm8: 0x00)
|
||||
x0 = x0._mm_clmulepi64_si128(b: k, imm8: 0x11)
|
||||
x0 = x0._mm_xor_si128(b: x2)
|
||||
x0 = x0._mm_xor_si128(b: y0)
|
||||
y0 = x0._mm_clmulepi64_si128(b: k, imm8: 0x00)
|
||||
x0 = x0._mm_clmulepi64_si128(b: k, imm8: 0x11)
|
||||
x0 = x0._mm_xor_si128(b: x3)
|
||||
x0 = x0._mm_xor_si128(b: y0)
|
||||
|
||||
// Reduce 128 bits to 64 bits.
|
||||
x1 = x0._mm_clmulepi64_si128(b: k, imm8: 0x10)
|
||||
x2 = util.make_m128i_multiple_u32(
|
||||
a00: 0xFFFF_FFFF,
|
||||
a01: 0x0000_0000,
|
||||
a02: 0xFFFF_FFFF,
|
||||
a03: 0x0000_0000)
|
||||
x0 = x0._mm_srli_si128(imm8: 8)
|
||||
x0 = x0._mm_xor_si128(b: x1)
|
||||
k = util.make_m128i_slice128(a: IEEE_X86_SSE42_K5ZZ[.. 16])
|
||||
x1 = x0._mm_srli_si128(imm8: 4)
|
||||
x0 = x0._mm_and_si128(b: x2)
|
||||
x0 = x0._mm_clmulepi64_si128(b: k, imm8: 0x00)
|
||||
x0 = x0._mm_xor_si128(b: x1)
|
||||
// Reduce 128 bits to 64 bits.
|
||||
x1 = x0._mm_clmulepi64_si128(b: k, imm8: 0x10)
|
||||
x2 = util.make_m128i_multiple_u32(
|
||||
a00: 0xFFFF_FFFF,
|
||||
a01: 0x0000_0000,
|
||||
a02: 0xFFFF_FFFF,
|
||||
a03: 0x0000_0000)
|
||||
x0 = x0._mm_srli_si128(imm8: 8)
|
||||
x0 = x0._mm_xor_si128(b: x1)
|
||||
k = util.make_m128i_slice128(a: IEEE_X86_SSE42_K5ZZ[.. 16])
|
||||
x1 = x0._mm_srli_si128(imm8: 4)
|
||||
x0 = x0._mm_and_si128(b: x2)
|
||||
x0 = x0._mm_clmulepi64_si128(b: k, imm8: 0x00)
|
||||
x0 = x0._mm_xor_si128(b: x1)
|
||||
|
||||
// Reduce 64 bits to 32 bits (Barrett Reduction) and extract.
|
||||
//
|
||||
// Barrett Reduction is Algorithm 1 (page 14) of Gopal et al., after
|
||||
// adjusting for bit-reflection as per Figure 12 (page 21).
|
||||
k = util.make_m128i_slice128(a: IEEE_X86_SSE42_PXMU[.. 16])
|
||||
x1 = x0._mm_and_si128(b: x2)
|
||||
x1 = x1._mm_clmulepi64_si128(b: k, imm8: 0x10)
|
||||
x1 = x1._mm_and_si128(b: x2)
|
||||
x1 = x1._mm_clmulepi64_si128(b: k, imm8: 0x00)
|
||||
x0 = x0._mm_xor_si128(b: x1)
|
||||
s = x0._mm_extract_epi32(imm8: 1)
|
||||
// Reduce 64 bits to 32 bits (Barrett Reduction) and extract.
|
||||
//
|
||||
// Barrett Reduction is Algorithm 1 (page 14) of Gopal et al., after
|
||||
// adjusting for bit-reflection as per Figure 12 (page 21).
|
||||
k = util.make_m128i_slice128(a: IEEE_X86_SSE42_PXMU[.. 16])
|
||||
x1 = x0._mm_and_si128(b: x2)
|
||||
x1 = x1._mm_clmulepi64_si128(b: k, imm8: 0x10)
|
||||
x1 = x1._mm_and_si128(b: x2)
|
||||
x1 = x1._mm_clmulepi64_si128(b: k, imm8: 0x00)
|
||||
x0 = x0._mm_xor_si128(b: x1)
|
||||
s = x0._mm_extract_epi32(imm8: 1)
|
||||
|
||||
// Handle the tail of args.x that wasn't a complete 64-byte chunk.
|
||||
tail_index = args.x.length() & 0xFFFF_FFFF_FFFF_FFC0 // And-not 64.
|
||||
if tail_index < args.x.length() {
|
||||
iterate (p = args.x[tail_index ..])(length: 1, advance: 1, unroll: 1) {
|
||||
s = IEEE_TABLE[0][((s & 0xFF) as base.u8) ^ p[0]] ^ (s >> 8)
|
||||
}
|
||||
}
|
||||
// Handle the tail of args.x that wasn't a complete 64-byte chunk.
|
||||
tail_index = args.x.length() & 0xFFFF_FFFF_FFFF_FFC0 // And-not 64.
|
||||
if tail_index < args.x.length() {
|
||||
iterate (p = args.x[tail_index ..])(length: 1, advance: 1, unroll: 1) {
|
||||
s = IEEE_TABLE[0][((s & 0xFF) as base.u8) ^ p[0]] ^ (s >> 8)
|
||||
}
|
||||
}
|
||||
|
||||
this.state = 0xFFFF_FFFF ^ s
|
||||
this.state = 0xFFFF_FFFF ^ s
|
||||
}
|
||||
|
||||
// These constants come from page 22 of Gopal et al. They are also reproduced
|
||||
@@ -141,21 +141,21 @@ pri func ieee_hasher.up_x86_sse42!(x: slice base.u8),
|
||||
// The k6' constant from the Gopal paper is unused.
|
||||
|
||||
pri const IEEE_X86_SSE42_K1K2 : array[16] base.u8 = [
|
||||
0xD4, 0x2B, 0x44, 0x54, 0x01, 0x00, 0x00, 0x00, // k1' = 0x1_5444_2BD4
|
||||
0x96, 0x15, 0xE4, 0xC6, 0x01, 0x00, 0x00, 0x00, // k2' = 0x1_C6E4_1596
|
||||
0xD4, 0x2B, 0x44, 0x54, 0x01, 0x00, 0x00, 0x00, // k1' = 0x1_5444_2BD4
|
||||
0x96, 0x15, 0xE4, 0xC6, 0x01, 0x00, 0x00, 0x00, // k2' = 0x1_C6E4_1596
|
||||
]
|
||||
|
||||
pri const IEEE_X86_SSE42_K3K4 : array[16] base.u8 = [
|
||||
0xD0, 0x97, 0x19, 0x75, 0x01, 0x00, 0x00, 0x00, // k3' = 0x1_7519_97D0
|
||||
0x9E, 0x00, 0xAA, 0xCC, 0x00, 0x00, 0x00, 0x00, // k4' = 0x0_CCAA_009E
|
||||
0xD0, 0x97, 0x19, 0x75, 0x01, 0x00, 0x00, 0x00, // k3' = 0x1_7519_97D0
|
||||
0x9E, 0x00, 0xAA, 0xCC, 0x00, 0x00, 0x00, 0x00, // k4' = 0x0_CCAA_009E
|
||||
]
|
||||
|
||||
pri const IEEE_X86_SSE42_K5ZZ : array[16] base.u8 = [
|
||||
0x24, 0x61, 0xCD, 0x63, 0x01, 0x00, 0x00, 0x00, // k5' = 0x1_63CD_6124
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Unused
|
||||
0x24, 0x61, 0xCD, 0x63, 0x01, 0x00, 0x00, 0x00, // k5' = 0x1_63CD_6124
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Unused
|
||||
]
|
||||
|
||||
pri const IEEE_X86_SSE42_PXMU : array[16] base.u8 = [
|
||||
0x41, 0x06, 0x71, 0xDB, 0x01, 0x00, 0x00, 0x00, // Px' = 0x1_DB71_0641
|
||||
0x41, 0x16, 0x01, 0xF7, 0x01, 0x00, 0x00, 0x00, // μ' = 0x1_F701_1641
|
||||
0x41, 0x06, 0x71, 0xDB, 0x01, 0x00, 0x00, 0x00, // Px' = 0x1_DB71_0641
|
||||
0x41, 0x16, 0x01, 0xF7, 0x01, 0x00, 0x00, 0x00, // μ' = 0x1_F701_1641
|
||||
]
|
||||
@@ -14,41 +14,41 @@
|
||||
|
||||
// CODE_ORDER is defined in the RFC section 3.2.7.
|
||||
pri const CODE_ORDER : array[19] base.u8[..= 18] = [
|
||||
16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15,
|
||||
16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15,
|
||||
]
|
||||
|
||||
// REVERSE8 reverses the bits in a byte.
|
||||
pri const REVERSE8 : array[256] base.u8 = [
|
||||
0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0, // 0x00 - 0x07
|
||||
0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0, // 0x08 - 0x0F
|
||||
0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8, // 0x10 - 0x17
|
||||
0x18, 0x98, 0x58, 0xD8, 0x38, 0xB8, 0x78, 0xF8, // 0x18 - 0x1F
|
||||
0x04, 0x84, 0x44, 0xC4, 0x24, 0xA4, 0x64, 0xE4, // 0x20 - 0x27
|
||||
0x14, 0x94, 0x54, 0xD4, 0x34, 0xB4, 0x74, 0xF4, // 0x28 - 0x2F
|
||||
0x0C, 0x8C, 0x4C, 0xCC, 0x2C, 0xAC, 0x6C, 0xEC, // 0x30 - 0x37
|
||||
0x1C, 0x9C, 0x5C, 0xDC, 0x3C, 0xBC, 0x7C, 0xFC, // 0x38 - 0x3F
|
||||
0x02, 0x82, 0x42, 0xC2, 0x22, 0xA2, 0x62, 0xE2, // 0x40 - 0x47
|
||||
0x12, 0x92, 0x52, 0xD2, 0x32, 0xB2, 0x72, 0xF2, // 0x48 - 0x4F
|
||||
0x0A, 0x8A, 0x4A, 0xCA, 0x2A, 0xAA, 0x6A, 0xEA, // 0x50 - 0x57
|
||||
0x1A, 0x9A, 0x5A, 0xDA, 0x3A, 0xBA, 0x7A, 0xFA, // 0x58 - 0x5F
|
||||
0x06, 0x86, 0x46, 0xC6, 0x26, 0xA6, 0x66, 0xE6, // 0x60 - 0x67
|
||||
0x16, 0x96, 0x56, 0xD6, 0x36, 0xB6, 0x76, 0xF6, // 0x68 - 0x6F
|
||||
0x0E, 0x8E, 0x4E, 0xCE, 0x2E, 0xAE, 0x6E, 0xEE, // 0x70 - 0x77
|
||||
0x1E, 0x9E, 0x5E, 0xDE, 0x3E, 0xBE, 0x7E, 0xFE, // 0x78 - 0x7F
|
||||
0x01, 0x81, 0x41, 0xC1, 0x21, 0xA1, 0x61, 0xE1, // 0x80 - 0x87
|
||||
0x11, 0x91, 0x51, 0xD1, 0x31, 0xB1, 0x71, 0xF1, // 0x88 - 0x8F
|
||||
0x09, 0x89, 0x49, 0xC9, 0x29, 0xA9, 0x69, 0xE9, // 0x90 - 0x97
|
||||
0x19, 0x99, 0x59, 0xD9, 0x39, 0xB9, 0x79, 0xF9, // 0x98 - 0x9F
|
||||
0x05, 0x85, 0x45, 0xC5, 0x25, 0xA5, 0x65, 0xE5, // 0xA0 - 0xA7
|
||||
0x15, 0x95, 0x55, 0xD5, 0x35, 0xB5, 0x75, 0xF5, // 0xA8 - 0xAF
|
||||
0x0D, 0x8D, 0x4D, 0xCD, 0x2D, 0xAD, 0x6D, 0xED, // 0xB0 - 0xB7
|
||||
0x1D, 0x9D, 0x5D, 0xDD, 0x3D, 0xBD, 0x7D, 0xFD, // 0xB8 - 0xBF
|
||||
0x03, 0x83, 0x43, 0xC3, 0x23, 0xA3, 0x63, 0xE3, // 0xC0 - 0xC7
|
||||
0x13, 0x93, 0x53, 0xD3, 0x33, 0xB3, 0x73, 0xF3, // 0xC8 - 0xCF
|
||||
0x0B, 0x8B, 0x4B, 0xCB, 0x2B, 0xAB, 0x6B, 0xEB, // 0xD0 - 0xD7
|
||||
0x1B, 0x9B, 0x5B, 0xDB, 0x3B, 0xBB, 0x7B, 0xFB, // 0xD8 - 0xDF
|
||||
0x07, 0x87, 0x47, 0xC7, 0x27, 0xA7, 0x67, 0xE7, // 0xE0 - 0xE7
|
||||
0x17, 0x97, 0x57, 0xD7, 0x37, 0xB7, 0x77, 0xF7, // 0xE8 - 0xEF
|
||||
0x0F, 0x8F, 0x4F, 0xCF, 0x2F, 0xAF, 0x6F, 0xEF, // 0xF0 - 0xF7
|
||||
0x1F, 0x9F, 0x5F, 0xDF, 0x3F, 0xBF, 0x7F, 0xFF, // 0xF8 - 0xFF
|
||||
0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0, // 0x00 - 0x07
|
||||
0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0, // 0x08 - 0x0F
|
||||
0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8, // 0x10 - 0x17
|
||||
0x18, 0x98, 0x58, 0xD8, 0x38, 0xB8, 0x78, 0xF8, // 0x18 - 0x1F
|
||||
0x04, 0x84, 0x44, 0xC4, 0x24, 0xA4, 0x64, 0xE4, // 0x20 - 0x27
|
||||
0x14, 0x94, 0x54, 0xD4, 0x34, 0xB4, 0x74, 0xF4, // 0x28 - 0x2F
|
||||
0x0C, 0x8C, 0x4C, 0xCC, 0x2C, 0xAC, 0x6C, 0xEC, // 0x30 - 0x37
|
||||
0x1C, 0x9C, 0x5C, 0xDC, 0x3C, 0xBC, 0x7C, 0xFC, // 0x38 - 0x3F
|
||||
0x02, 0x82, 0x42, 0xC2, 0x22, 0xA2, 0x62, 0xE2, // 0x40 - 0x47
|
||||
0x12, 0x92, 0x52, 0xD2, 0x32, 0xB2, 0x72, 0xF2, // 0x48 - 0x4F
|
||||
0x0A, 0x8A, 0x4A, 0xCA, 0x2A, 0xAA, 0x6A, 0xEA, // 0x50 - 0x57
|
||||
0x1A, 0x9A, 0x5A, 0xDA, 0x3A, 0xBA, 0x7A, 0xFA, // 0x58 - 0x5F
|
||||
0x06, 0x86, 0x46, 0xC6, 0x26, 0xA6, 0x66, 0xE6, // 0x60 - 0x67
|
||||
0x16, 0x96, 0x56, 0xD6, 0x36, 0xB6, 0x76, 0xF6, // 0x68 - 0x6F
|
||||
0x0E, 0x8E, 0x4E, 0xCE, 0x2E, 0xAE, 0x6E, 0xEE, // 0x70 - 0x77
|
||||
0x1E, 0x9E, 0x5E, 0xDE, 0x3E, 0xBE, 0x7E, 0xFE, // 0x78 - 0x7F
|
||||
0x01, 0x81, 0x41, 0xC1, 0x21, 0xA1, 0x61, 0xE1, // 0x80 - 0x87
|
||||
0x11, 0x91, 0x51, 0xD1, 0x31, 0xB1, 0x71, 0xF1, // 0x88 - 0x8F
|
||||
0x09, 0x89, 0x49, 0xC9, 0x29, 0xA9, 0x69, 0xE9, // 0x90 - 0x97
|
||||
0x19, 0x99, 0x59, 0xD9, 0x39, 0xB9, 0x79, 0xF9, // 0x98 - 0x9F
|
||||
0x05, 0x85, 0x45, 0xC5, 0x25, 0xA5, 0x65, 0xE5, // 0xA0 - 0xA7
|
||||
0x15, 0x95, 0x55, 0xD5, 0x35, 0xB5, 0x75, 0xF5, // 0xA8 - 0xAF
|
||||
0x0D, 0x8D, 0x4D, 0xCD, 0x2D, 0xAD, 0x6D, 0xED, // 0xB0 - 0xB7
|
||||
0x1D, 0x9D, 0x5D, 0xDD, 0x3D, 0xBD, 0x7D, 0xFD, // 0xB8 - 0xBF
|
||||
0x03, 0x83, 0x43, 0xC3, 0x23, 0xA3, 0x63, 0xE3, // 0xC0 - 0xC7
|
||||
0x13, 0x93, 0x53, 0xD3, 0x33, 0xB3, 0x73, 0xF3, // 0xC8 - 0xCF
|
||||
0x0B, 0x8B, 0x4B, 0xCB, 0x2B, 0xAB, 0x6B, 0xEB, // 0xD0 - 0xD7
|
||||
0x1B, 0x9B, 0x5B, 0xDB, 0x3B, 0xBB, 0x7B, 0xFB, // 0xD8 - 0xDF
|
||||
0x07, 0x87, 0x47, 0xC7, 0x27, 0xA7, 0x67, 0xE7, // 0xE0 - 0xE7
|
||||
0x17, 0x97, 0x57, 0xD7, 0x37, 0xB7, 0x77, 0xF7, // 0xE8 - 0xEF
|
||||
0x0F, 0x8F, 0x4F, 0xCF, 0x2F, 0xAF, 0x6F, 0xEF, // 0xF0 - 0xF7
|
||||
0x1F, 0x9F, 0x5F, 0xDF, 0x3F, 0xBF, 0x7F, 0xFF, // 0xF8 - 0xFF
|
||||
]
|
||||
+772
-772
@@ -45,17 +45,17 @@ pub const DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE : base.u64 = 1
|
||||
// Some trailing elements are 0x08000000. Bit 27 indicates an invalid value.
|
||||
|
||||
pri const LCODE_MAGIC_NUMBERS : array[32] base.u32 = [
|
||||
0x4000_0000, 0x4000_0100, 0x4000_0200, 0x4000_0300, 0x4000_0400, 0x4000_0500, 0x4000_0600, 0x4000_0700,
|
||||
0x4000_0810, 0x4000_0A10, 0x4000_0C10, 0x4000_0E10, 0x4000_1020, 0x4000_1420, 0x4000_1820, 0x4000_1C20,
|
||||
0x4000_2030, 0x4000_2830, 0x4000_3030, 0x4000_3830, 0x4000_4040, 0x4000_5040, 0x4000_6040, 0x4000_7040,
|
||||
0x4000_8050, 0x4000_A050, 0x4000_C050, 0x4000_E050, 0x4000_FF00, 0x0800_0000, 0x0800_0000, 0x0800_0000,
|
||||
0x4000_0000, 0x4000_0100, 0x4000_0200, 0x4000_0300, 0x4000_0400, 0x4000_0500, 0x4000_0600, 0x4000_0700,
|
||||
0x4000_0810, 0x4000_0A10, 0x4000_0C10, 0x4000_0E10, 0x4000_1020, 0x4000_1420, 0x4000_1820, 0x4000_1C20,
|
||||
0x4000_2030, 0x4000_2830, 0x4000_3030, 0x4000_3830, 0x4000_4040, 0x4000_5040, 0x4000_6040, 0x4000_7040,
|
||||
0x4000_8050, 0x4000_A050, 0x4000_C050, 0x4000_E050, 0x4000_FF00, 0x0800_0000, 0x0800_0000, 0x0800_0000,
|
||||
]
|
||||
|
||||
pri const DCODE_MAGIC_NUMBERS : array[32] base.u32 = [
|
||||
0x4000_0000, 0x4000_0100, 0x4000_0200, 0x4000_0300, 0x4000_0410, 0x4000_0610, 0x4000_0820, 0x4000_0C20,
|
||||
0x4000_1030, 0x4000_1830, 0x4000_2040, 0x4000_3040, 0x4000_4050, 0x4000_6050, 0x4000_8060, 0x4000_C060,
|
||||
0x4001_0070, 0x4001_8070, 0x4002_0080, 0x4003_0080, 0x4004_0090, 0x4006_0090, 0x4008_00A0, 0x400C_00A0,
|
||||
0x4010_00B0, 0x4018_00B0, 0x4020_00C0, 0x4030_00C0, 0x4040_00D0, 0x4060_00D0, 0x0800_0000, 0x0800_0000,
|
||||
0x4000_0000, 0x4000_0100, 0x4000_0200, 0x4000_0300, 0x4000_0410, 0x4000_0610, 0x4000_0820, 0x4000_0C20,
|
||||
0x4000_1030, 0x4000_1830, 0x4000_2040, 0x4000_3040, 0x4000_4050, 0x4000_6050, 0x4000_8060, 0x4000_C060,
|
||||
0x4001_0070, 0x4001_8070, 0x4002_0080, 0x4003_0080, 0x4004_0090, 0x4006_0090, 0x4008_00A0, 0x400C_00A0,
|
||||
0x4010_00B0, 0x4018_00B0, 0x4020_00C0, 0x4030_00C0, 0x4040_00D0, 0x4060_00D0, 0x0800_0000, 0x0800_0000,
|
||||
]
|
||||
|
||||
// HUFFS_TABLE_SIZE is the smallest power of 2 that is greater than or equal to
|
||||
@@ -67,852 +67,852 @@ pri const HUFFS_TABLE_SIZE : base.u32 = 1024
|
||||
pri const HUFFS_TABLE_MASK : base.u32 = 1023
|
||||
|
||||
pub struct decoder? implements base.io_transformer(
|
||||
// These fields yield src's bits in Least Significant Bits order.
|
||||
bits : base.u32,
|
||||
n_bits : base.u32,
|
||||
// These fields yield src's bits in Least Significant Bits order.
|
||||
bits : base.u32,
|
||||
n_bits : base.u32,
|
||||
|
||||
// transformed_history_count is the number of bytes written to the history
|
||||
// ringbuffer due to calling transform_io. It excludes any pre-history
|
||||
// written by explicit calls to add_history. It also excludes any bytes
|
||||
// decoded from the final transform_io call (the one that doesn't suspend)
|
||||
// as there is no further need for tracking history (for resolving back-
|
||||
// references) once the decoding completes.
|
||||
transformed_history_count : base.u64,
|
||||
// transformed_history_count is the number of bytes written to the history
|
||||
// ringbuffer due to calling transform_io. It excludes any pre-history
|
||||
// written by explicit calls to add_history. It also excludes any bytes
|
||||
// decoded from the final transform_io call (the one that doesn't suspend)
|
||||
// as there is no further need for tracking history (for resolving back-
|
||||
// references) once the decoding completes.
|
||||
transformed_history_count : base.u64,
|
||||
|
||||
// history_index indexes the history array, defined below.
|
||||
history_index : base.u32,
|
||||
// history_index indexes the history array, defined below.
|
||||
history_index : base.u32,
|
||||
|
||||
// n_huffs_bits is discussed in the huffs field comment.
|
||||
n_huffs_bits : array[2] base.u32[..= 9],
|
||||
// n_huffs_bits is discussed in the huffs field comment.
|
||||
n_huffs_bits : array[2] base.u32[..= 9],
|
||||
|
||||
// end_of_block is whether decode_huffman_xxx saw an end-of-block code.
|
||||
//
|
||||
// TODO: can decode_huffman_xxx signal this in band instead of out of band?
|
||||
end_of_block : base.bool,
|
||||
// end_of_block is whether decode_huffman_xxx saw an end-of-block code.
|
||||
//
|
||||
// TODO: can decode_huffman_xxx signal this in band instead of out of band?
|
||||
end_of_block : base.bool,
|
||||
|
||||
util : base.utility,
|
||||
util : base.utility,
|
||||
)(
|
||||
// huffs and n_huffs_bits are the lookup tables for Huffman decodings.
|
||||
//
|
||||
// There are up to 2 Huffman decoders active at any one time. As per this
|
||||
// package's README.md:
|
||||
// - huffs[0] is used for clcode and lcode.
|
||||
// - huffs[1] is used for dcode.
|
||||
//
|
||||
// The initial table key is the low n_huffs_bits of the decoder.bits field.
|
||||
// Keys longer than 9 bits require a two step lookup, the first step
|
||||
// examines the low 9 bits, the second step examines the remaining bits.
|
||||
// Two steps are required at most, as keys are at most 15 bits long.
|
||||
//
|
||||
// Using decoder.bits's low n_huffs_bits as a table key is valid even if
|
||||
// decoder.n_bits is less than n_huffs_bits, because the immediate next
|
||||
// step after indexing the table by the key is to compare decoder.n_bits to
|
||||
// the table value's number of decoder.bits to consume. If it compares
|
||||
// less, then more source bytes are read and the table lookup re-tried.
|
||||
//
|
||||
// The table value's bits:
|
||||
// - bit 31 indicates a literal.
|
||||
// - bit 30 indicates a base number + extra bits.
|
||||
// - bit 29 indicates end-of-block.
|
||||
// - bit 28 indicates a redirect to another part of the table.
|
||||
// - bit 27 indicates an invalid value.
|
||||
// - bits 24 ..= 26 are zero.
|
||||
// - bits 8 ..= 23 are the redirect offset, literal (in bits [8 ..= 15])
|
||||
// or base number.
|
||||
// - bits 4 ..= 7 are the redirected table's size in bits or the number
|
||||
// of extra bits.
|
||||
// - bits 0 ..= 3 are the number of decoder.bits to consume.
|
||||
//
|
||||
// Exactly one of the eight bits [24 ..= 31] should be set.
|
||||
huffs : array[2] array[HUFFS_TABLE_SIZE] base.u32,
|
||||
// huffs and n_huffs_bits are the lookup tables for Huffman decodings.
|
||||
//
|
||||
// There are up to 2 Huffman decoders active at any one time. As per this
|
||||
// package's README.md:
|
||||
// - huffs[0] is used for clcode and lcode.
|
||||
// - huffs[1] is used for dcode.
|
||||
//
|
||||
// The initial table key is the low n_huffs_bits of the decoder.bits field.
|
||||
// Keys longer than 9 bits require a two step lookup, the first step
|
||||
// examines the low 9 bits, the second step examines the remaining bits.
|
||||
// Two steps are required at most, as keys are at most 15 bits long.
|
||||
//
|
||||
// Using decoder.bits's low n_huffs_bits as a table key is valid even if
|
||||
// decoder.n_bits is less than n_huffs_bits, because the immediate next
|
||||
// step after indexing the table by the key is to compare decoder.n_bits to
|
||||
// the table value's number of decoder.bits to consume. If it compares
|
||||
// less, then more source bytes are read and the table lookup re-tried.
|
||||
//
|
||||
// The table value's bits:
|
||||
// - bit 31 indicates a literal.
|
||||
// - bit 30 indicates a base number + extra bits.
|
||||
// - bit 29 indicates end-of-block.
|
||||
// - bit 28 indicates a redirect to another part of the table.
|
||||
// - bit 27 indicates an invalid value.
|
||||
// - bits 24 ..= 26 are zero.
|
||||
// - bits 8 ..= 23 are the redirect offset, literal (in bits [8 ..= 15])
|
||||
// or base number.
|
||||
// - bits 4 ..= 7 are the redirected table's size in bits or the number
|
||||
// of extra bits.
|
||||
// - bits 0 ..= 3 are the number of decoder.bits to consume.
|
||||
//
|
||||
// Exactly one of the eight bits [24 ..= 31] should be set.
|
||||
huffs : array[2] array[HUFFS_TABLE_SIZE] base.u32,
|
||||
|
||||
// history[.. 0x8000] holds up to the last 32KiB of decoded output, if the
|
||||
// decoding was incomplete (e.g. due to a short read or write). RFC 1951
|
||||
// (DEFLATE) gives the maximum distance in a length-distance back-reference
|
||||
// as 32768, or 0x8000. Similarly, the RFC gives the maximum length as 258.
|
||||
//
|
||||
// history[.. 0x8000] is a ringbuffer, so that the most distant byte in
|
||||
// the decoding isn't necessarily history[0]. The ringbuffer is full (i.e.
|
||||
// it holds 32KiB of history) if and only if history_index >= 0x8000.
|
||||
//
|
||||
// history[history_index & 0x7FFF] is where the next byte of decoded output
|
||||
// will be written.
|
||||
//
|
||||
// When suspended in decoder.transform_io, or after an add_history call,
|
||||
// history[0x8000 .. 0x8000 + (ML - 1)] duplicates history[.. (ML - 1)],
|
||||
// where ML is the maximum length (258 as stated above). This simplifies
|
||||
// copying up to ML bytes from the ringbuffer, as there is no need to split
|
||||
// the copy around the 0x8000 index.
|
||||
history : array[0x8000 + (258 - 1)] base.u8, // 32 KiB + (ML - 1) bytes.
|
||||
// history[.. 0x8000] holds up to the last 32KiB of decoded output, if the
|
||||
// decoding was incomplete (e.g. due to a short read or write). RFC 1951
|
||||
// (DEFLATE) gives the maximum distance in a length-distance back-reference
|
||||
// as 32768, or 0x8000. Similarly, the RFC gives the maximum length as 258.
|
||||
//
|
||||
// history[.. 0x8000] is a ringbuffer, so that the most distant byte in
|
||||
// the decoding isn't necessarily history[0]. The ringbuffer is full (i.e.
|
||||
// it holds 32KiB of history) if and only if history_index >= 0x8000.
|
||||
//
|
||||
// history[history_index & 0x7FFF] is where the next byte of decoded output
|
||||
// will be written.
|
||||
//
|
||||
// When suspended in decoder.transform_io, or after an add_history call,
|
||||
// history[0x8000 .. 0x8000 + (ML - 1)] duplicates history[.. (ML - 1)],
|
||||
// where ML is the maximum length (258 as stated above). This simplifies
|
||||
// copying up to ML bytes from the ringbuffer, as there is no need to split
|
||||
// the copy around the 0x8000 index.
|
||||
history : array[0x8000 + (258 - 1)] base.u8, // 32 KiB + (ML - 1) bytes.
|
||||
|
||||
// code_lengths is used to pass out-of-band data to init_huff.
|
||||
//
|
||||
// code_lengths[args.n_codes0 + i] holds the number of bits in the i'th
|
||||
// code.
|
||||
code_lengths : array[320] base.u8,
|
||||
// code_lengths is used to pass out-of-band data to init_huff.
|
||||
//
|
||||
// code_lengths[args.n_codes0 + i] holds the number of bits in the i'th
|
||||
// code.
|
||||
code_lengths : array[320] base.u8,
|
||||
)
|
||||
|
||||
pub func decoder.add_history!(hist: slice base.u8) {
|
||||
var s : slice base.u8
|
||||
var n_copied : base.u64
|
||||
var already_full : base.u32[..= 0x8000]
|
||||
var s : slice base.u8
|
||||
var n_copied : base.u64
|
||||
var already_full : base.u32[..= 0x8000]
|
||||
|
||||
s = args.hist
|
||||
if s.length() >= 0x8000 {
|
||||
// If s is longer than the ringbuffer, we can ignore the previous value
|
||||
// of history_index, as we will overwrite the whole ringbuffer.
|
||||
s = s.suffix(up_to: 0x8000)
|
||||
this.history[.. 0x8000].copy_from_slice!(s: s)
|
||||
this.history_index = 0x8000
|
||||
} else {
|
||||
// Otherwise, append s to the history ringbuffer starting at the
|
||||
// previous history_index (modulo 0x8000).
|
||||
n_copied = this.history[this.history_index & 0x7FFF .. 0x8000].copy_from_slice!(s: s)
|
||||
if n_copied < s.length() {
|
||||
// a_slice.copy_from(s:b_slice) returns the minimum of the two
|
||||
// slice lengths. If that value is less than b_slice.length(), then
|
||||
// not all of b_slice was copied.
|
||||
//
|
||||
// In terms of the history ringbuffer, that means that we have to
|
||||
// wrap around and copy the remainder of s over the start of the
|
||||
// history ringbuffer.
|
||||
s = s[n_copied ..]
|
||||
n_copied = this.history[.. 0x8000].copy_from_slice!(s: s)
|
||||
// Set history_index (modulo 0x8000) to the length of this
|
||||
// remainder. The &0x7FFF is redundant, but proves to the compiler
|
||||
// that the conversion to u32 will not overflow. The +0x8000 is to
|
||||
// maintain that the history ringbuffer is full if and only if
|
||||
// history_index >= 0x8000.
|
||||
this.history_index = ((n_copied & 0x7FFF) as base.u32) + 0x8000
|
||||
} else {
|
||||
// We didn't need to wrap around.
|
||||
already_full = 0
|
||||
if this.history_index >= 0x8000 {
|
||||
already_full = 0x8000
|
||||
}
|
||||
this.history_index = (this.history_index & 0x7FFF) + ((n_copied & 0x7FFF) as base.u32) + already_full
|
||||
}
|
||||
}
|
||||
s = args.hist
|
||||
if s.length() >= 0x8000 {
|
||||
// If s is longer than the ringbuffer, we can ignore the previous value
|
||||
// of history_index, as we will overwrite the whole ringbuffer.
|
||||
s = s.suffix(up_to: 0x8000)
|
||||
this.history[.. 0x8000].copy_from_slice!(s: s)
|
||||
this.history_index = 0x8000
|
||||
} else {
|
||||
// Otherwise, append s to the history ringbuffer starting at the
|
||||
// previous history_index (modulo 0x8000).
|
||||
n_copied = this.history[this.history_index & 0x7FFF .. 0x8000].copy_from_slice!(s: s)
|
||||
if n_copied < s.length() {
|
||||
// a_slice.copy_from(s:b_slice) returns the minimum of the two
|
||||
// slice lengths. If that value is less than b_slice.length(), then
|
||||
// not all of b_slice was copied.
|
||||
//
|
||||
// In terms of the history ringbuffer, that means that we have to
|
||||
// wrap around and copy the remainder of s over the start of the
|
||||
// history ringbuffer.
|
||||
s = s[n_copied ..]
|
||||
n_copied = this.history[.. 0x8000].copy_from_slice!(s: s)
|
||||
// Set history_index (modulo 0x8000) to the length of this
|
||||
// remainder. The &0x7FFF is redundant, but proves to the compiler
|
||||
// that the conversion to u32 will not overflow. The +0x8000 is to
|
||||
// maintain that the history ringbuffer is full if and only if
|
||||
// history_index >= 0x8000.
|
||||
this.history_index = ((n_copied & 0x7FFF) as base.u32) + 0x8000
|
||||
} else {
|
||||
// We didn't need to wrap around.
|
||||
already_full = 0
|
||||
if this.history_index >= 0x8000 {
|
||||
already_full = 0x8000
|
||||
}
|
||||
this.history_index = (this.history_index & 0x7FFF) + ((n_copied & 0x7FFF) as base.u32) + already_full
|
||||
}
|
||||
}
|
||||
|
||||
// Have the tail of this.history duplicate the head. Look for "ML" in the
|
||||
// comments for the history field for more discussion.
|
||||
this.history[0x8000 ..].copy_from_slice!(s: this.history[..])
|
||||
// Have the tail of this.history duplicate the head. Look for "ML" in the
|
||||
// comments for the history field for more discussion.
|
||||
this.history[0x8000 ..].copy_from_slice!(s: this.history[..])
|
||||
}
|
||||
|
||||
pub func decoder.set_quirk_enabled!(quirk: base.u32, enabled: base.bool) {
|
||||
}
|
||||
|
||||
pub func decoder.workbuf_len() base.range_ii_u64 {
|
||||
return this.util.make_range_ii_u64(
|
||||
min_incl: DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE,
|
||||
max_incl: DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE)
|
||||
return this.util.make_range_ii_u64(
|
||||
min_incl: DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE,
|
||||
max_incl: DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE)
|
||||
}
|
||||
|
||||
pub func decoder.transform_io?(dst: base.io_writer, src: base.io_reader, workbuf: slice base.u8) {
|
||||
var status : base.status
|
||||
var status : base.status
|
||||
|
||||
while true {
|
||||
status =? this.do_transform_io?(dst: args.dst, src: args.src, workbuf: args.workbuf)
|
||||
if (status == base."$short read") and args.src.is_closed() {
|
||||
return "#truncated input"
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
while true {
|
||||
status =? this.do_transform_io?(dst: args.dst, src: args.src, workbuf: args.workbuf)
|
||||
if (status == base."$short read") and args.src.is_closed() {
|
||||
return "#truncated input"
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
}
|
||||
|
||||
pri func decoder.do_transform_io?(dst: base.io_writer, src: base.io_reader, workbuf: slice base.u8) {
|
||||
var mark : base.u64
|
||||
var status : base.status
|
||||
var mark : base.u64
|
||||
var status : base.status
|
||||
|
||||
choose decode_huffman_fast64 = [decode_huffman_bmi2]
|
||||
choose decode_huffman_fast64 = [decode_huffman_bmi2]
|
||||
|
||||
while true {
|
||||
mark = args.dst.mark()
|
||||
status =? this.decode_blocks?(dst: args.dst, src: args.src)
|
||||
if not status.is_suspension() {
|
||||
return status
|
||||
}
|
||||
this.transformed_history_count ~sat+= args.dst.count_since(mark: mark)
|
||||
// TODO: should "since" be "since!", as the return value lets you
|
||||
// modify the state of args.dst, so future mutations (via the slice)
|
||||
// can change the veracity of any args.dst assertions?
|
||||
this.add_history!(hist: args.dst.since(mark: mark))
|
||||
yield? status
|
||||
} endwhile
|
||||
while true {
|
||||
mark = args.dst.mark()
|
||||
status =? this.decode_blocks?(dst: args.dst, src: args.src)
|
||||
if not status.is_suspension() {
|
||||
return status
|
||||
}
|
||||
this.transformed_history_count ~sat+= args.dst.count_since(mark: mark)
|
||||
// TODO: should "since" be "since!", as the return value lets you
|
||||
// modify the state of args.dst, so future mutations (via the slice)
|
||||
// can change the veracity of any args.dst assertions?
|
||||
this.add_history!(hist: args.dst.since(mark: mark))
|
||||
yield? status
|
||||
} endwhile
|
||||
}
|
||||
|
||||
pri func decoder.decode_blocks?(dst: base.io_writer, src: base.io_reader) {
|
||||
var final : base.u32
|
||||
var b0 : base.u32[..= 255]
|
||||
var type : base.u32
|
||||
var status : base.status
|
||||
var final : base.u32
|
||||
var b0 : base.u32[..= 255]
|
||||
var type : base.u32
|
||||
var status : base.status
|
||||
|
||||
while.outer final == 0 {
|
||||
while this.n_bits < 3,
|
||||
post this.n_bits >= 3,
|
||||
{
|
||||
b0 = args.src.read_u8_as_u32?()
|
||||
this.bits |= b0 << (this.n_bits & 3)
|
||||
this.n_bits = (this.n_bits & 3) + 8
|
||||
} endwhile
|
||||
final = this.bits & 0x01
|
||||
type = (this.bits >> 1) & 0x03
|
||||
this.bits >>= 3
|
||||
this.n_bits -= 3
|
||||
while.outer final == 0 {
|
||||
while this.n_bits < 3,
|
||||
post this.n_bits >= 3,
|
||||
{
|
||||
b0 = args.src.read_u8_as_u32?()
|
||||
this.bits |= b0 << (this.n_bits & 3)
|
||||
this.n_bits = (this.n_bits & 3) + 8
|
||||
} endwhile
|
||||
final = this.bits & 0x01
|
||||
type = (this.bits >> 1) & 0x03
|
||||
this.bits >>= 3
|
||||
this.n_bits -= 3
|
||||
|
||||
if type == 0 {
|
||||
this.decode_uncompressed?(dst: args.dst, src: args.src)
|
||||
continue.outer
|
||||
} else if type == 1 {
|
||||
status = this.init_fixed_huffman!()
|
||||
// TODO: "if status.is_error()" is probably more idiomatic, but for
|
||||
// some mysterious, idiosyncratic reason, performs noticeably worse
|
||||
// for gcc (but not for clang).
|
||||
//
|
||||
// See git commit 3bf9573 "Work around strange status.is_error
|
||||
// performance".
|
||||
if not status.is_ok() {
|
||||
return status
|
||||
}
|
||||
} else if type == 2 {
|
||||
this.init_dynamic_huffman?(src: args.src)
|
||||
} else {
|
||||
return "#bad block"
|
||||
}
|
||||
if type == 0 {
|
||||
this.decode_uncompressed?(dst: args.dst, src: args.src)
|
||||
continue.outer
|
||||
} else if type == 1 {
|
||||
status = this.init_fixed_huffman!()
|
||||
// TODO: "if status.is_error()" is probably more idiomatic, but for
|
||||
// some mysterious, idiosyncratic reason, performs noticeably worse
|
||||
// for gcc (but not for clang).
|
||||
//
|
||||
// See git commit 3bf9573 "Work around strange status.is_error
|
||||
// performance".
|
||||
if not status.is_ok() {
|
||||
return status
|
||||
}
|
||||
} else if type == 2 {
|
||||
this.init_dynamic_huffman?(src: args.src)
|
||||
} else {
|
||||
return "#bad block"
|
||||
}
|
||||
|
||||
this.end_of_block = false
|
||||
while true {
|
||||
if this.util.cpu_arch_is_32_bit() {
|
||||
status = this.decode_huffman_fast32!(dst: args.dst, src: args.src)
|
||||
} else {
|
||||
status = this.decode_huffman_fast64!(dst: args.dst, src: args.src)
|
||||
}
|
||||
if status.is_error() {
|
||||
return status
|
||||
}
|
||||
if this.end_of_block {
|
||||
continue.outer
|
||||
}
|
||||
this.decode_huffman_slow?(dst: args.dst, src: args.src)
|
||||
if this.end_of_block {
|
||||
continue.outer
|
||||
}
|
||||
} endwhile
|
||||
} endwhile.outer
|
||||
this.end_of_block = false
|
||||
while true {
|
||||
if this.util.cpu_arch_is_32_bit() {
|
||||
status = this.decode_huffman_fast32!(dst: args.dst, src: args.src)
|
||||
} else {
|
||||
status = this.decode_huffman_fast64!(dst: args.dst, src: args.src)
|
||||
}
|
||||
if status.is_error() {
|
||||
return status
|
||||
}
|
||||
if this.end_of_block {
|
||||
continue.outer
|
||||
}
|
||||
this.decode_huffman_slow?(dst: args.dst, src: args.src)
|
||||
if this.end_of_block {
|
||||
continue.outer
|
||||
}
|
||||
} endwhile
|
||||
} endwhile.outer
|
||||
}
|
||||
|
||||
// decode_uncompressed decodes an uncompresed block as per the RFC section
|
||||
// 3.2.4.
|
||||
pri func decoder.decode_uncompressed?(dst: base.io_writer, src: base.io_reader) {
|
||||
var length : base.u32
|
||||
var n_copied : base.u32
|
||||
var length : base.u32
|
||||
var n_copied : base.u32
|
||||
|
||||
// TODO: make this "if" into a function invariant?
|
||||
//
|
||||
// Ditto for decode_huffman_slow and decode_huffman_fastxx.
|
||||
if (this.n_bits >= 8) or ((this.bits >> (this.n_bits & 7)) <> 0) {
|
||||
return "#internal error: inconsistent n_bits"
|
||||
}
|
||||
this.n_bits = 0
|
||||
this.bits = 0
|
||||
// TODO: make this "if" into a function invariant?
|
||||
//
|
||||
// Ditto for decode_huffman_slow and decode_huffman_fastxx.
|
||||
if (this.n_bits >= 8) or ((this.bits >> (this.n_bits & 7)) <> 0) {
|
||||
return "#internal error: inconsistent n_bits"
|
||||
}
|
||||
this.n_bits = 0
|
||||
this.bits = 0
|
||||
|
||||
length = args.src.read_u32le?()
|
||||
if (length.low_bits(n: 16) + length.high_bits(n: 16)) <> 0xFFFF {
|
||||
return "#inconsistent stored block length"
|
||||
}
|
||||
length = length.low_bits(n: 16)
|
||||
while true {
|
||||
n_copied = args.dst.limited_copy_u32_from_reader!(up_to: length, r: args.src)
|
||||
if length <= n_copied {
|
||||
return ok
|
||||
}
|
||||
length -= n_copied
|
||||
if args.dst.length() == 0 {
|
||||
yield? base."$short write"
|
||||
} else {
|
||||
yield? base."$short read"
|
||||
}
|
||||
} endwhile
|
||||
length = args.src.read_u32le?()
|
||||
if (length.low_bits(n: 16) + length.high_bits(n: 16)) <> 0xFFFF {
|
||||
return "#inconsistent stored block length"
|
||||
}
|
||||
length = length.low_bits(n: 16)
|
||||
while true {
|
||||
n_copied = args.dst.limited_copy_u32_from_reader!(up_to: length, r: args.src)
|
||||
if length <= n_copied {
|
||||
return ok
|
||||
}
|
||||
length -= n_copied
|
||||
if args.dst.length() == 0 {
|
||||
yield? base."$short write"
|
||||
} else {
|
||||
yield? base."$short read"
|
||||
}
|
||||
} endwhile
|
||||
}
|
||||
|
||||
// init_fixed_huffman initializes this.huffs as per the RFC section 3.2.6.
|
||||
pri func decoder.init_fixed_huffman!() base.status {
|
||||
var i : base.u32
|
||||
var status : base.status
|
||||
var i : base.u32
|
||||
var status : base.status
|
||||
|
||||
while i < 144 {
|
||||
this.code_lengths[i] = 8
|
||||
i += 1
|
||||
} endwhile
|
||||
while i < 256 {
|
||||
this.code_lengths[i] = 9
|
||||
i += 1
|
||||
} endwhile
|
||||
while i < 280 {
|
||||
this.code_lengths[i] = 7
|
||||
i += 1
|
||||
} endwhile
|
||||
while i < 288 {
|
||||
this.code_lengths[i] = 8
|
||||
i += 1
|
||||
} endwhile
|
||||
while i < 320 {
|
||||
this.code_lengths[i] = 5
|
||||
i += 1
|
||||
} endwhile
|
||||
while i < 144 {
|
||||
this.code_lengths[i] = 8
|
||||
i += 1
|
||||
} endwhile
|
||||
while i < 256 {
|
||||
this.code_lengths[i] = 9
|
||||
i += 1
|
||||
} endwhile
|
||||
while i < 280 {
|
||||
this.code_lengths[i] = 7
|
||||
i += 1
|
||||
} endwhile
|
||||
while i < 288 {
|
||||
this.code_lengths[i] = 8
|
||||
i += 1
|
||||
} endwhile
|
||||
while i < 320 {
|
||||
this.code_lengths[i] = 5
|
||||
i += 1
|
||||
} endwhile
|
||||
|
||||
status = this.init_huff!(which: 0, n_codes0: 0, n_codes1: 288, base_symbol: 257)
|
||||
if status.is_error() {
|
||||
return status
|
||||
}
|
||||
status = this.init_huff!(which: 1, n_codes0: 288, n_codes1: 320, base_symbol: 0)
|
||||
if status.is_error() {
|
||||
return status
|
||||
}
|
||||
return ok
|
||||
status = this.init_huff!(which: 0, n_codes0: 0, n_codes1: 288, base_symbol: 257)
|
||||
if status.is_error() {
|
||||
return status
|
||||
}
|
||||
status = this.init_huff!(which: 1, n_codes0: 288, n_codes1: 320, base_symbol: 0)
|
||||
if status.is_error() {
|
||||
return status
|
||||
}
|
||||
return ok
|
||||
}
|
||||
|
||||
// init_dynamic_huffman initializes this.huffs as per the RFC section 3.2.7.
|
||||
pri func decoder.init_dynamic_huffman?(src: base.io_reader) {
|
||||
var bits : base.u32
|
||||
var n_bits : base.u32
|
||||
var b0 : base.u32[..= 255]
|
||||
var n_lit : base.u32[..= 288]
|
||||
var n_dist : base.u32[..= 32]
|
||||
var n_clen : base.u32[..= 19]
|
||||
var i : base.u32
|
||||
var b1 : base.u32[..= 255]
|
||||
var status : base.status
|
||||
var mask : base.u32[..= 511]
|
||||
var table_entry : base.u32
|
||||
var table_entry_n_bits : base.u32[..= 15]
|
||||
var b2 : base.u32[..= 255]
|
||||
var n_extra_bits : base.u32[..= 7]
|
||||
var rep_symbol : base.u8[..= 15]
|
||||
var rep_count : base.u32
|
||||
var b3 : base.u32[..= 255]
|
||||
var bits : base.u32
|
||||
var n_bits : base.u32
|
||||
var b0 : base.u32[..= 255]
|
||||
var n_lit : base.u32[..= 288]
|
||||
var n_dist : base.u32[..= 32]
|
||||
var n_clen : base.u32[..= 19]
|
||||
var i : base.u32
|
||||
var b1 : base.u32[..= 255]
|
||||
var status : base.status
|
||||
var mask : base.u32[..= 511]
|
||||
var table_entry : base.u32
|
||||
var table_entry_n_bits : base.u32[..= 15]
|
||||
var b2 : base.u32[..= 255]
|
||||
var n_extra_bits : base.u32[..= 7]
|
||||
var rep_symbol : base.u8[..= 15]
|
||||
var rep_count : base.u32
|
||||
var b3 : base.u32[..= 255]
|
||||
|
||||
bits = this.bits
|
||||
n_bits = this.n_bits
|
||||
while n_bits < 14,
|
||||
post n_bits >= 14,
|
||||
{
|
||||
b0 = args.src.read_u8_as_u32?()
|
||||
bits |= b0 << n_bits
|
||||
n_bits += 8
|
||||
} endwhile
|
||||
n_lit = bits.low_bits(n: 5) + 257
|
||||
if n_lit > 286 {
|
||||
return "#bad literal/length code count"
|
||||
}
|
||||
bits >>= 5
|
||||
n_dist = bits.low_bits(n: 5) + 1
|
||||
if n_dist > 30 {
|
||||
return "#bad distance code count"
|
||||
}
|
||||
bits >>= 5
|
||||
n_clen = bits.low_bits(n: 4) + 4
|
||||
bits >>= 4
|
||||
n_bits -= 14
|
||||
bits = this.bits
|
||||
n_bits = this.n_bits
|
||||
while n_bits < 14,
|
||||
post n_bits >= 14,
|
||||
{
|
||||
b0 = args.src.read_u8_as_u32?()
|
||||
bits |= b0 << n_bits
|
||||
n_bits += 8
|
||||
} endwhile
|
||||
n_lit = bits.low_bits(n: 5) + 257
|
||||
if n_lit > 286 {
|
||||
return "#bad literal/length code count"
|
||||
}
|
||||
bits >>= 5
|
||||
n_dist = bits.low_bits(n: 5) + 1
|
||||
if n_dist > 30 {
|
||||
return "#bad distance code count"
|
||||
}
|
||||
bits >>= 5
|
||||
n_clen = bits.low_bits(n: 4) + 4
|
||||
bits >>= 4
|
||||
n_bits -= 14
|
||||
|
||||
// Read the clcode Huffman table: H-CL.
|
||||
i = 0
|
||||
while i < n_clen {
|
||||
while n_bits < 3,
|
||||
inv i < n_clen,
|
||||
post n_bits >= 3,
|
||||
{
|
||||
b1 = args.src.read_u8_as_u32?()
|
||||
bits |= b1 << n_bits
|
||||
n_bits += 8
|
||||
} endwhile
|
||||
assert i < 19 via "a < b: a < c; c <= b"(c: n_clen)
|
||||
this.code_lengths[CODE_ORDER[i]] = (bits & 0x07) as base.u8
|
||||
bits >>= 3
|
||||
n_bits -= 3
|
||||
i += 1
|
||||
} endwhile
|
||||
while i < 19 {
|
||||
this.code_lengths[CODE_ORDER[i]] = 0
|
||||
i += 1
|
||||
} endwhile
|
||||
status = this.init_huff!(which: 0, n_codes0: 0, n_codes1: 19, base_symbol: 0xFFF)
|
||||
if status.is_error() {
|
||||
return status
|
||||
}
|
||||
// Read the clcode Huffman table: H-CL.
|
||||
i = 0
|
||||
while i < n_clen {
|
||||
while n_bits < 3,
|
||||
inv i < n_clen,
|
||||
post n_bits >= 3,
|
||||
{
|
||||
b1 = args.src.read_u8_as_u32?()
|
||||
bits |= b1 << n_bits
|
||||
n_bits += 8
|
||||
} endwhile
|
||||
assert i < 19 via "a < b: a < c; c <= b"(c: n_clen)
|
||||
this.code_lengths[CODE_ORDER[i]] = (bits & 0x07) as base.u8
|
||||
bits >>= 3
|
||||
n_bits -= 3
|
||||
i += 1
|
||||
} endwhile
|
||||
while i < 19 {
|
||||
this.code_lengths[CODE_ORDER[i]] = 0
|
||||
i += 1
|
||||
} endwhile
|
||||
status = this.init_huff!(which: 0, n_codes0: 0, n_codes1: 19, base_symbol: 0xFFF)
|
||||
if status.is_error() {
|
||||
return status
|
||||
}
|
||||
|
||||
// Decode the code lengths for the next two Huffman tables.
|
||||
mask = ((1 as base.u32) << (this.n_huffs_bits[0])) - 1
|
||||
i = 0
|
||||
while i < (n_lit + n_dist) {
|
||||
assert i < (288 + 32) via "a < (b + c): a < (b0 + c0); b0 <= b; c0 <= c"(b0: n_lit, c0: n_dist)
|
||||
// Decode the code lengths for the next two Huffman tables.
|
||||
mask = ((1 as base.u32) << (this.n_huffs_bits[0])) - 1
|
||||
i = 0
|
||||
while i < (n_lit + n_dist) {
|
||||
assert i < (288 + 32) via "a < (b + c): a < (b0 + c0); b0 <= b; c0 <= c"(b0: n_lit, c0: n_dist)
|
||||
|
||||
// Decode a clcode symbol from H-CL.
|
||||
while true,
|
||||
inv i < 320,
|
||||
{
|
||||
table_entry = this.huffs[0][bits & mask]
|
||||
table_entry_n_bits = table_entry & 15
|
||||
if n_bits >= table_entry_n_bits {
|
||||
bits >>= table_entry_n_bits
|
||||
n_bits -= table_entry_n_bits
|
||||
break
|
||||
}
|
||||
assert n_bits < 15 via "a < b: a < c; c <= b"(c: table_entry_n_bits)
|
||||
b2 = args.src.read_u8_as_u32?()
|
||||
bits |= b2 << n_bits
|
||||
n_bits += 8
|
||||
} endwhile
|
||||
// For H-CL, there should be no redirections and all symbols should be
|
||||
// literals.
|
||||
if (table_entry >> 24) <> 0x80 {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
table_entry = (table_entry >> 8) & 0xFF
|
||||
// Decode a clcode symbol from H-CL.
|
||||
while true,
|
||||
inv i < 320,
|
||||
{
|
||||
table_entry = this.huffs[0][bits & mask]
|
||||
table_entry_n_bits = table_entry & 15
|
||||
if n_bits >= table_entry_n_bits {
|
||||
bits >>= table_entry_n_bits
|
||||
n_bits -= table_entry_n_bits
|
||||
break
|
||||
}
|
||||
assert n_bits < 15 via "a < b: a < c; c <= b"(c: table_entry_n_bits)
|
||||
b2 = args.src.read_u8_as_u32?()
|
||||
bits |= b2 << n_bits
|
||||
n_bits += 8
|
||||
} endwhile
|
||||
// For H-CL, there should be no redirections and all symbols should be
|
||||
// literals.
|
||||
if (table_entry >> 24) <> 0x80 {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
table_entry = (table_entry >> 8) & 0xFF
|
||||
|
||||
// Write a literal code length.
|
||||
if table_entry < 16 {
|
||||
this.code_lengths[i] = table_entry as base.u8
|
||||
i += 1
|
||||
continue
|
||||
}
|
||||
// Write a literal code length.
|
||||
if table_entry < 16 {
|
||||
this.code_lengths[i] = table_entry as base.u8
|
||||
i += 1
|
||||
continue
|
||||
}
|
||||
|
||||
// Write a repeated code length.
|
||||
n_extra_bits = 0
|
||||
rep_symbol = 0
|
||||
rep_count = 0
|
||||
if table_entry == 16 {
|
||||
n_extra_bits = 2
|
||||
if i <= 0 {
|
||||
return "#bad Huffman code length repetition"
|
||||
}
|
||||
rep_symbol = this.code_lengths[i - 1] & 15
|
||||
rep_count = 3
|
||||
assert rep_count <= 11
|
||||
} else if table_entry == 17 {
|
||||
n_extra_bits = 3
|
||||
rep_symbol = 0
|
||||
rep_count = 3
|
||||
assert rep_count <= 11
|
||||
} else if table_entry == 18 {
|
||||
n_extra_bits = 7
|
||||
rep_symbol = 0
|
||||
rep_count = 11
|
||||
assert rep_count <= 11
|
||||
} else {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
while n_bits < n_extra_bits,
|
||||
inv i < 320,
|
||||
inv rep_count <= 11,
|
||||
post n_bits >= n_extra_bits,
|
||||
{
|
||||
assert n_bits < 7 via "a < b: a < c; c <= b"(c: n_extra_bits)
|
||||
b3 = args.src.read_u8_as_u32?()
|
||||
bits |= b3 << n_bits
|
||||
n_bits += 8
|
||||
} endwhile
|
||||
rep_count += bits.low_bits(n: n_extra_bits)
|
||||
bits >>= n_extra_bits
|
||||
n_bits -= n_extra_bits
|
||||
// Write a repeated code length.
|
||||
n_extra_bits = 0
|
||||
rep_symbol = 0
|
||||
rep_count = 0
|
||||
if table_entry == 16 {
|
||||
n_extra_bits = 2
|
||||
if i <= 0 {
|
||||
return "#bad Huffman code length repetition"
|
||||
}
|
||||
rep_symbol = this.code_lengths[i - 1] & 15
|
||||
rep_count = 3
|
||||
assert rep_count <= 11
|
||||
} else if table_entry == 17 {
|
||||
n_extra_bits = 3
|
||||
rep_symbol = 0
|
||||
rep_count = 3
|
||||
assert rep_count <= 11
|
||||
} else if table_entry == 18 {
|
||||
n_extra_bits = 7
|
||||
rep_symbol = 0
|
||||
rep_count = 11
|
||||
assert rep_count <= 11
|
||||
} else {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
while n_bits < n_extra_bits,
|
||||
inv i < 320,
|
||||
inv rep_count <= 11,
|
||||
post n_bits >= n_extra_bits,
|
||||
{
|
||||
assert n_bits < 7 via "a < b: a < c; c <= b"(c: n_extra_bits)
|
||||
b3 = args.src.read_u8_as_u32?()
|
||||
bits |= b3 << n_bits
|
||||
n_bits += 8
|
||||
} endwhile
|
||||
rep_count += bits.low_bits(n: n_extra_bits)
|
||||
bits >>= n_extra_bits
|
||||
n_bits -= n_extra_bits
|
||||
|
||||
while rep_count > 0 {
|
||||
// TODO: hoist this check up one level?
|
||||
if i >= (n_lit + n_dist) {
|
||||
return "#bad Huffman code length count"
|
||||
}
|
||||
assert i < (288 + 32) via "a < (b + c): a < (b0 + c0); b0 <= b; c0 <= c"(b0: n_lit, c0: n_dist)
|
||||
this.code_lengths[i] = rep_symbol
|
||||
i += 1
|
||||
rep_count -= 1
|
||||
} endwhile
|
||||
} endwhile
|
||||
while rep_count > 0 {
|
||||
// TODO: hoist this check up one level?
|
||||
if i >= (n_lit + n_dist) {
|
||||
return "#bad Huffman code length count"
|
||||
}
|
||||
assert i < (288 + 32) via "a < (b + c): a < (b0 + c0); b0 <= b; c0 <= c"(b0: n_lit, c0: n_dist)
|
||||
this.code_lengths[i] = rep_symbol
|
||||
i += 1
|
||||
rep_count -= 1
|
||||
} endwhile
|
||||
} endwhile
|
||||
|
||||
if i <> (n_lit + n_dist) {
|
||||
return "#bad Huffman code length count"
|
||||
}
|
||||
if this.code_lengths[256] == 0 {
|
||||
return "#missing end-of-block code"
|
||||
}
|
||||
if i <> (n_lit + n_dist) {
|
||||
return "#bad Huffman code length count"
|
||||
}
|
||||
if this.code_lengths[256] == 0 {
|
||||
return "#missing end-of-block code"
|
||||
}
|
||||
|
||||
status = this.init_huff!(which: 0, n_codes0: 0, n_codes1: n_lit, base_symbol: 257)
|
||||
if status.is_error() {
|
||||
return status
|
||||
}
|
||||
status = this.init_huff!(which: 1, n_codes0: n_lit, n_codes1: n_lit + n_dist, base_symbol: 0)
|
||||
if status.is_error() {
|
||||
return status
|
||||
}
|
||||
status = this.init_huff!(which: 0, n_codes0: 0, n_codes1: n_lit, base_symbol: 257)
|
||||
if status.is_error() {
|
||||
return status
|
||||
}
|
||||
status = this.init_huff!(which: 1, n_codes0: n_lit, n_codes1: n_lit + n_dist, base_symbol: 0)
|
||||
if status.is_error() {
|
||||
return status
|
||||
}
|
||||
|
||||
this.bits = bits
|
||||
this.n_bits = n_bits
|
||||
this.bits = bits
|
||||
this.n_bits = n_bits
|
||||
}
|
||||
|
||||
// TODO: make named constants for 15, 19, 319, etc.
|
||||
|
||||
pri func decoder.init_huff!(which: base.u32[..= 1], n_codes0: base.u32[..= 288], n_codes1: base.u32[..= 320], base_symbol: base.u32) base.status {
|
||||
var counts : array[16] base.u16[..= 320]
|
||||
var i : base.u32
|
||||
var remaining : base.u32
|
||||
var offsets : array[16] base.u16[..= 320]
|
||||
var n_symbols : base.u32[..= 320]
|
||||
var count : base.u32[..= 320]
|
||||
var symbols : array[320] base.u16[..= 319]
|
||||
var min_cl : base.u32[..= 9]
|
||||
var max_cl : base.u32[..= 15]
|
||||
var initial_high_bits : base.u32
|
||||
var prev_cl : base.u32[..= 15]
|
||||
var prev_redirect_key : base.u32
|
||||
var top : base.u32[..= HUFFS_TABLE_SIZE]
|
||||
var next_top : base.u32[..= HUFFS_TABLE_SIZE]
|
||||
var code : base.u32
|
||||
var key : base.u32
|
||||
var value : base.u32
|
||||
var cl : base.u32[..= 15]
|
||||
var redirect_key : base.u32[..= 511]
|
||||
var j : base.u32[..= 16]
|
||||
var reversed_key : base.u32[..= 511]
|
||||
var symbol : base.u32[..= 319]
|
||||
var high_bits : base.u32
|
||||
var delta : base.u32
|
||||
var counts : array[16] base.u16[..= 320]
|
||||
var i : base.u32
|
||||
var remaining : base.u32
|
||||
var offsets : array[16] base.u16[..= 320]
|
||||
var n_symbols : base.u32[..= 320]
|
||||
var count : base.u32[..= 320]
|
||||
var symbols : array[320] base.u16[..= 319]
|
||||
var min_cl : base.u32[..= 9]
|
||||
var max_cl : base.u32[..= 15]
|
||||
var initial_high_bits : base.u32
|
||||
var prev_cl : base.u32[..= 15]
|
||||
var prev_redirect_key : base.u32
|
||||
var top : base.u32[..= HUFFS_TABLE_SIZE]
|
||||
var next_top : base.u32[..= HUFFS_TABLE_SIZE]
|
||||
var code : base.u32
|
||||
var key : base.u32
|
||||
var value : base.u32
|
||||
var cl : base.u32[..= 15]
|
||||
var redirect_key : base.u32[..= 511]
|
||||
var j : base.u32[..= 16]
|
||||
var reversed_key : base.u32[..= 511]
|
||||
var symbol : base.u32[..= 319]
|
||||
var high_bits : base.u32
|
||||
var delta : base.u32
|
||||
|
||||
// For the clcode example in this package's README.md:
|
||||
// - n_codes0 = 0
|
||||
// - n_codes1 = 19
|
||||
// - code_lengths[ 0] = 3
|
||||
// - code_lengths[ 1] = 0
|
||||
// - code_lengths[ 2] = 0
|
||||
// - code_lengths[ 3] = 5
|
||||
// - code_lengths[ 4] = 3
|
||||
// - code_lengths[ 5] = 3
|
||||
// - code_lengths[ 6] = 3
|
||||
// - code_lengths[ 7] = 3
|
||||
// - code_lengths[ 8] = 3
|
||||
// - code_lengths[ 9] = 3
|
||||
// - code_lengths[10] = 0
|
||||
// - code_lengths[11] = 0
|
||||
// - code_lengths[12] = 0
|
||||
// - code_lengths[13] = 0
|
||||
// - code_lengths[14] = 0
|
||||
// - code_lengths[15] = 0
|
||||
// - code_lengths[16] = 0
|
||||
// - code_lengths[17] = 4
|
||||
// - code_lengths[18] = 5
|
||||
// For the clcode example in this package's README.md:
|
||||
// - n_codes0 = 0
|
||||
// - n_codes1 = 19
|
||||
// - code_lengths[ 0] = 3
|
||||
// - code_lengths[ 1] = 0
|
||||
// - code_lengths[ 2] = 0
|
||||
// - code_lengths[ 3] = 5
|
||||
// - code_lengths[ 4] = 3
|
||||
// - code_lengths[ 5] = 3
|
||||
// - code_lengths[ 6] = 3
|
||||
// - code_lengths[ 7] = 3
|
||||
// - code_lengths[ 8] = 3
|
||||
// - code_lengths[ 9] = 3
|
||||
// - code_lengths[10] = 0
|
||||
// - code_lengths[11] = 0
|
||||
// - code_lengths[12] = 0
|
||||
// - code_lengths[13] = 0
|
||||
// - code_lengths[14] = 0
|
||||
// - code_lengths[15] = 0
|
||||
// - code_lengths[16] = 0
|
||||
// - code_lengths[17] = 4
|
||||
// - code_lengths[18] = 5
|
||||
|
||||
// Calculate counts.
|
||||
//
|
||||
// For the clcode example in this package's README.md:
|
||||
// - counts[0] = 9
|
||||
// - counts[1] = 0
|
||||
// - counts[2] = 0
|
||||
// - counts[3] = 7
|
||||
// - counts[4] = 1
|
||||
// - counts[5] = 2
|
||||
// - all other counts elements are 0.
|
||||
i = args.n_codes0
|
||||
while i < args.n_codes1 {
|
||||
assert i < 320 via "a < b: a < c; c <= b"(c: args.n_codes1)
|
||||
// TODO: this if should be unnecessary. Have some way to assert that,
|
||||
// for all j, counts[j] <= i, and thus counts[j]++ will not overflow.
|
||||
if counts[this.code_lengths[i] & 15] >= 320 {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
counts[this.code_lengths[i] & 15] += 1
|
||||
i += 1
|
||||
} endwhile
|
||||
if ((counts[0] as base.u32) + args.n_codes0) == args.n_codes1 {
|
||||
return "#no Huffman codes"
|
||||
}
|
||||
// Calculate counts.
|
||||
//
|
||||
// For the clcode example in this package's README.md:
|
||||
// - counts[0] = 9
|
||||
// - counts[1] = 0
|
||||
// - counts[2] = 0
|
||||
// - counts[3] = 7
|
||||
// - counts[4] = 1
|
||||
// - counts[5] = 2
|
||||
// - all other counts elements are 0.
|
||||
i = args.n_codes0
|
||||
while i < args.n_codes1 {
|
||||
assert i < 320 via "a < b: a < c; c <= b"(c: args.n_codes1)
|
||||
// TODO: this if should be unnecessary. Have some way to assert that,
|
||||
// for all j, counts[j] <= i, and thus counts[j]++ will not overflow.
|
||||
if counts[this.code_lengths[i] & 15] >= 320 {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
counts[this.code_lengths[i] & 15] += 1
|
||||
i += 1
|
||||
} endwhile
|
||||
if ((counts[0] as base.u32) + args.n_codes0) == args.n_codes1 {
|
||||
return "#no Huffman codes"
|
||||
}
|
||||
|
||||
// Check that the Huffman code completely covers all possible input bits.
|
||||
remaining = 1 // There is 1 possible 0-bit code.
|
||||
i = 1
|
||||
while i <= 15 {
|
||||
if remaining > (1 << 30) {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
// Each iteration doubles the number of possible remaining codes.
|
||||
remaining <<= 1
|
||||
if remaining < (counts[i] as base.u32) {
|
||||
return "#bad Huffman code (over-subscribed)"
|
||||
}
|
||||
remaining -= counts[i] as base.u32
|
||||
i += 1
|
||||
} endwhile
|
||||
if remaining <> 0 {
|
||||
// As a special case, allow a degenerate H-D Huffman table, with only
|
||||
// one 1-bit code.
|
||||
if (args.which == 1) and (counts[1] == 1) and
|
||||
(((counts[0] as base.u32) + args.n_codes0 + 1) == args.n_codes1) {
|
||||
// Check that the Huffman code completely covers all possible input bits.
|
||||
remaining = 1 // There is 1 possible 0-bit code.
|
||||
i = 1
|
||||
while i <= 15 {
|
||||
if remaining > (1 << 30) {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
// Each iteration doubles the number of possible remaining codes.
|
||||
remaining <<= 1
|
||||
if remaining < (counts[i] as base.u32) {
|
||||
return "#bad Huffman code (over-subscribed)"
|
||||
}
|
||||
remaining -= counts[i] as base.u32
|
||||
i += 1
|
||||
} endwhile
|
||||
if remaining <> 0 {
|
||||
// As a special case, allow a degenerate H-D Huffman table, with only
|
||||
// one 1-bit code.
|
||||
if (args.which == 1) and (counts[1] == 1) and
|
||||
(((counts[0] as base.u32) + args.n_codes0 + 1) == args.n_codes1) {
|
||||
|
||||
i = 0
|
||||
while i <= 29 {
|
||||
if this.code_lengths[args.n_codes0 + i] == 1 {
|
||||
this.n_huffs_bits[1] = 1
|
||||
this.huffs[1][0] = DCODE_MAGIC_NUMBERS[i] | 1
|
||||
this.huffs[1][1] = DCODE_MAGIC_NUMBERS[31] | 1
|
||||
return ok
|
||||
}
|
||||
i += 1
|
||||
} endwhile
|
||||
}
|
||||
return "#bad Huffman code (under-subscribed)"
|
||||
}
|
||||
i = 0
|
||||
while i <= 29 {
|
||||
if this.code_lengths[args.n_codes0 + i] == 1 {
|
||||
this.n_huffs_bits[1] = 1
|
||||
this.huffs[1][0] = DCODE_MAGIC_NUMBERS[i] | 1
|
||||
this.huffs[1][1] = DCODE_MAGIC_NUMBERS[31] | 1
|
||||
return ok
|
||||
}
|
||||
i += 1
|
||||
} endwhile
|
||||
}
|
||||
return "#bad Huffman code (under-subscribed)"
|
||||
}
|
||||
|
||||
// Calculate offsets and n_symbols.
|
||||
//
|
||||
// For the clcode example in this package's README.md:
|
||||
// - offsets[0] = 0
|
||||
// - offsets[1] = 0
|
||||
// - offsets[2] = 0
|
||||
// - offsets[3] = 0
|
||||
// - offsets[4] = 7
|
||||
// - offsets[5] = 8
|
||||
// - offsets[6] = 10
|
||||
// - all other offsets elements are 10.
|
||||
// - n_symbols = 10
|
||||
i = 1
|
||||
while i <= 15 {
|
||||
offsets[i] = n_symbols as base.u16
|
||||
count = counts[i] as base.u32
|
||||
if n_symbols > (320 - count) {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
assert (n_symbols + count) <= 320 via "(a + b) <= c: a <= (c - b)"()
|
||||
// TODO: change this to n_symbols += count, once the proof engine's
|
||||
// bounds checking can handle it.
|
||||
n_symbols = n_symbols + count
|
||||
i += 1
|
||||
} endwhile
|
||||
if n_symbols > 288 {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
// Calculate offsets and n_symbols.
|
||||
//
|
||||
// For the clcode example in this package's README.md:
|
||||
// - offsets[0] = 0
|
||||
// - offsets[1] = 0
|
||||
// - offsets[2] = 0
|
||||
// - offsets[3] = 0
|
||||
// - offsets[4] = 7
|
||||
// - offsets[5] = 8
|
||||
// - offsets[6] = 10
|
||||
// - all other offsets elements are 10.
|
||||
// - n_symbols = 10
|
||||
i = 1
|
||||
while i <= 15 {
|
||||
offsets[i] = n_symbols as base.u16
|
||||
count = counts[i] as base.u32
|
||||
if n_symbols > (320 - count) {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
assert (n_symbols + count) <= 320 via "(a + b) <= c: a <= (c - b)"()
|
||||
// TODO: change this to n_symbols += count, once the proof engine's
|
||||
// bounds checking can handle it.
|
||||
n_symbols = n_symbols + count
|
||||
i += 1
|
||||
} endwhile
|
||||
if n_symbols > 288 {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
|
||||
// Calculate symbols.
|
||||
//
|
||||
// For the clcode example in this package's README.md:
|
||||
// - symbols[0] = 0
|
||||
// - symbols[1] = 4
|
||||
// - symbols[2] = 5
|
||||
// - symbols[3] = 6
|
||||
// - symbols[4] = 7
|
||||
// - symbols[5] = 8
|
||||
// - symbols[6] = 9
|
||||
// - symbols[7] = 17
|
||||
// - symbols[8] = 3
|
||||
// - symbols[9] = 18
|
||||
//
|
||||
// As a (local variable) side effect, offsets' values will be updated:
|
||||
// - offsets[3] = 7, formerly 0
|
||||
// - offsets[4] = 8, formerly 7
|
||||
// - offsets[5] = 10, formerly 8
|
||||
i = args.n_codes0
|
||||
while i < args.n_codes1,
|
||||
inv n_symbols <= 288,
|
||||
{
|
||||
assert i < 320 via "a < b: a < c; c <= b"(c: args.n_codes1)
|
||||
// TODO: this if check should be unnecessary.
|
||||
if i < args.n_codes0 {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
if this.code_lengths[i] <> 0 {
|
||||
if offsets[this.code_lengths[i] & 15] >= 320 {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
symbols[offsets[this.code_lengths[i] & 15]] = (i - args.n_codes0) as base.u16
|
||||
offsets[this.code_lengths[i] & 15] += 1
|
||||
}
|
||||
i += 1
|
||||
} endwhile
|
||||
// Calculate symbols.
|
||||
//
|
||||
// For the clcode example in this package's README.md:
|
||||
// - symbols[0] = 0
|
||||
// - symbols[1] = 4
|
||||
// - symbols[2] = 5
|
||||
// - symbols[3] = 6
|
||||
// - symbols[4] = 7
|
||||
// - symbols[5] = 8
|
||||
// - symbols[6] = 9
|
||||
// - symbols[7] = 17
|
||||
// - symbols[8] = 3
|
||||
// - symbols[9] = 18
|
||||
//
|
||||
// As a (local variable) side effect, offsets' values will be updated:
|
||||
// - offsets[3] = 7, formerly 0
|
||||
// - offsets[4] = 8, formerly 7
|
||||
// - offsets[5] = 10, formerly 8
|
||||
i = args.n_codes0
|
||||
while i < args.n_codes1,
|
||||
inv n_symbols <= 288,
|
||||
{
|
||||
assert i < 320 via "a < b: a < c; c <= b"(c: args.n_codes1)
|
||||
// TODO: this if check should be unnecessary.
|
||||
if i < args.n_codes0 {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
if this.code_lengths[i] <> 0 {
|
||||
if offsets[this.code_lengths[i] & 15] >= 320 {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
symbols[offsets[this.code_lengths[i] & 15]] = (i - args.n_codes0) as base.u16
|
||||
offsets[this.code_lengths[i] & 15] += 1
|
||||
}
|
||||
i += 1
|
||||
} endwhile
|
||||
|
||||
// Calculate min_cl and max_cl.
|
||||
//
|
||||
// For the clcode example in this package's README.md:
|
||||
// - min_cl = 3
|
||||
// - max_cl = 5
|
||||
min_cl = 1
|
||||
while true,
|
||||
inv n_symbols <= 288,
|
||||
{
|
||||
if counts[min_cl] <> 0 {
|
||||
break
|
||||
}
|
||||
if min_cl >= 9 {
|
||||
return "#bad Huffman minimum code length"
|
||||
}
|
||||
min_cl += 1
|
||||
} endwhile
|
||||
max_cl = 15
|
||||
while true,
|
||||
inv n_symbols <= 288,
|
||||
{
|
||||
if counts[max_cl] <> 0 {
|
||||
break
|
||||
}
|
||||
if max_cl <= 1 {
|
||||
return "#no Huffman codes"
|
||||
}
|
||||
max_cl -= 1
|
||||
} endwhile
|
||||
if max_cl <= 9 {
|
||||
this.n_huffs_bits[args.which] = max_cl
|
||||
} else {
|
||||
this.n_huffs_bits[args.which] = 9
|
||||
}
|
||||
// Calculate min_cl and max_cl.
|
||||
//
|
||||
// For the clcode example in this package's README.md:
|
||||
// - min_cl = 3
|
||||
// - max_cl = 5
|
||||
min_cl = 1
|
||||
while true,
|
||||
inv n_symbols <= 288,
|
||||
{
|
||||
if counts[min_cl] <> 0 {
|
||||
break
|
||||
}
|
||||
if min_cl >= 9 {
|
||||
return "#bad Huffman minimum code length"
|
||||
}
|
||||
min_cl += 1
|
||||
} endwhile
|
||||
max_cl = 15
|
||||
while true,
|
||||
inv n_symbols <= 288,
|
||||
{
|
||||
if counts[max_cl] <> 0 {
|
||||
break
|
||||
}
|
||||
if max_cl <= 1 {
|
||||
return "#no Huffman codes"
|
||||
}
|
||||
max_cl -= 1
|
||||
} endwhile
|
||||
if max_cl <= 9 {
|
||||
this.n_huffs_bits[args.which] = max_cl
|
||||
} else {
|
||||
this.n_huffs_bits[args.which] = 9
|
||||
}
|
||||
|
||||
// Calculate this.huffs[args.which].
|
||||
//
|
||||
// For the clcode example in this package's README.md:
|
||||
// - this.huffs[0][0b..000] = 0x80000003 (literal, symbols[0]=0x00, code_length=3)
|
||||
// - this.huffs[0][0b..100] = 0x80000403 (literal, symbols[1]=0x04, code_length=3)
|
||||
// - this.huffs[0][0b..010] = 0x80000503 (literal, symbols[2]=0x05, code_length=3)
|
||||
// - this.huffs[0][0b..110] = 0x80000603 (literal, symbols[3]=0x06, code_length=3)
|
||||
// - this.huffs[0][0b..001] = 0x80000703 (literal, symbols[4]=0x07, code_length=3)
|
||||
// - this.huffs[0][0b..101] = 0x80000803 (literal, symbols[5]=0x08, code_length=3)
|
||||
// - this.huffs[0][0b..011] = 0x80000903 (literal, symbols[6]=0x09, code_length=3)
|
||||
// - this.huffs[0][0b.0111] = 0x80001104 (literal, symbols[7]=0x11, code_length=4)
|
||||
// - this.huffs[0][0b01111] = 0x80000305 (literal, symbols[8]=0x03, code_length=5)
|
||||
// - this.huffs[0][0b11111] = 0x80001805 (literal, symbols[9]=0x18, code_length=5)
|
||||
i = 0
|
||||
if (n_symbols <> (offsets[max_cl] as base.u32)) or (n_symbols <> (offsets[15] as base.u32)) {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
if (args.n_codes0 + (symbols[0] as base.u32)) >= 320 {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
// Calculate this.huffs[args.which].
|
||||
//
|
||||
// For the clcode example in this package's README.md:
|
||||
// - this.huffs[0][0b..000] = 0x80000003 (literal, symbols[0]=0x00, code_length=3)
|
||||
// - this.huffs[0][0b..100] = 0x80000403 (literal, symbols[1]=0x04, code_length=3)
|
||||
// - this.huffs[0][0b..010] = 0x80000503 (literal, symbols[2]=0x05, code_length=3)
|
||||
// - this.huffs[0][0b..110] = 0x80000603 (literal, symbols[3]=0x06, code_length=3)
|
||||
// - this.huffs[0][0b..001] = 0x80000703 (literal, symbols[4]=0x07, code_length=3)
|
||||
// - this.huffs[0][0b..101] = 0x80000803 (literal, symbols[5]=0x08, code_length=3)
|
||||
// - this.huffs[0][0b..011] = 0x80000903 (literal, symbols[6]=0x09, code_length=3)
|
||||
// - this.huffs[0][0b.0111] = 0x80001104 (literal, symbols[7]=0x11, code_length=4)
|
||||
// - this.huffs[0][0b01111] = 0x80000305 (literal, symbols[8]=0x03, code_length=5)
|
||||
// - this.huffs[0][0b11111] = 0x80001805 (literal, symbols[9]=0x18, code_length=5)
|
||||
i = 0
|
||||
if (n_symbols <> (offsets[max_cl] as base.u32)) or (n_symbols <> (offsets[15] as base.u32)) {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
if (args.n_codes0 + (symbols[0] as base.u32)) >= 320 {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
|
||||
initial_high_bits = 1 << 9
|
||||
if max_cl < 9 {
|
||||
initial_high_bits = (1 as base.u32) << max_cl
|
||||
}
|
||||
prev_cl = (this.code_lengths[args.n_codes0 + (symbols[0] as base.u32)] & 15) as base.u32
|
||||
prev_redirect_key = 0xFFFF_FFFF
|
||||
top = 0
|
||||
next_top = 512
|
||||
code = 0
|
||||
key = 0
|
||||
value = 0
|
||||
while true,
|
||||
pre code < (1 << 15),
|
||||
pre i < 288,
|
||||
inv n_symbols <= 288,
|
||||
{
|
||||
if (args.n_codes0 + (symbols[i] as base.u32)) >= 320 {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
cl = (this.code_lengths[args.n_codes0 + (symbols[i] as base.u32)] & 15) as base.u32
|
||||
if cl > prev_cl {
|
||||
code <<= cl - prev_cl
|
||||
if code >= (1 << 15) {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
}
|
||||
// For the remainder of this loop body, prev_cl is the original code
|
||||
// length, cl is possibly clipped by 9, if in the 2nd-level table.
|
||||
prev_cl = cl
|
||||
initial_high_bits = 1 << 9
|
||||
if max_cl < 9 {
|
||||
initial_high_bits = (1 as base.u32) << max_cl
|
||||
}
|
||||
prev_cl = (this.code_lengths[args.n_codes0 + (symbols[0] as base.u32)] & 15) as base.u32
|
||||
prev_redirect_key = 0xFFFF_FFFF
|
||||
top = 0
|
||||
next_top = 512
|
||||
code = 0
|
||||
key = 0
|
||||
value = 0
|
||||
while true,
|
||||
pre code < (1 << 15),
|
||||
pre i < 288,
|
||||
inv n_symbols <= 288,
|
||||
{
|
||||
if (args.n_codes0 + (symbols[i] as base.u32)) >= 320 {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
cl = (this.code_lengths[args.n_codes0 + (symbols[i] as base.u32)] & 15) as base.u32
|
||||
if cl > prev_cl {
|
||||
code <<= cl - prev_cl
|
||||
if code >= (1 << 15) {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
}
|
||||
// For the remainder of this loop body, prev_cl is the original code
|
||||
// length, cl is possibly clipped by 9, if in the 2nd-level table.
|
||||
prev_cl = cl
|
||||
|
||||
key = code
|
||||
if cl > 9 {
|
||||
cl -= 9
|
||||
assert cl <= 9
|
||||
key = code
|
||||
if cl > 9 {
|
||||
cl -= 9
|
||||
assert cl <= 9
|
||||
|
||||
redirect_key = (key >> cl) & 511
|
||||
key = key.low_bits(n: cl)
|
||||
if prev_redirect_key <> redirect_key {
|
||||
prev_redirect_key = redirect_key
|
||||
redirect_key = (key >> cl) & 511
|
||||
key = key.low_bits(n: cl)
|
||||
if prev_redirect_key <> redirect_key {
|
||||
prev_redirect_key = redirect_key
|
||||
|
||||
// Calculate the number of bits needed for the 2nd level table.
|
||||
// This computation is similar to "check that the Huffman code
|
||||
// completely covers all possible input bits" above.
|
||||
remaining = (1 as base.u32) << cl
|
||||
j = prev_cl
|
||||
while j <= 15,
|
||||
inv cl <= 9,
|
||||
inv code < (1 << 15),
|
||||
inv i < 288,
|
||||
inv n_symbols <= 288,
|
||||
{
|
||||
if remaining <= (counts[j] as base.u32) {
|
||||
break
|
||||
}
|
||||
remaining -= counts[j] as base.u32
|
||||
if remaining > (1 << 30) {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
remaining <<= 1
|
||||
j += 1
|
||||
} endwhile
|
||||
if (j <= 9) or (15 < j) {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
j -= 9
|
||||
initial_high_bits = (1 as base.u32) << j
|
||||
// Calculate the number of bits needed for the 2nd level table.
|
||||
// This computation is similar to "check that the Huffman code
|
||||
// completely covers all possible input bits" above.
|
||||
remaining = (1 as base.u32) << cl
|
||||
j = prev_cl
|
||||
while j <= 15,
|
||||
inv cl <= 9,
|
||||
inv code < (1 << 15),
|
||||
inv i < 288,
|
||||
inv n_symbols <= 288,
|
||||
{
|
||||
if remaining <= (counts[j] as base.u32) {
|
||||
break
|
||||
}
|
||||
remaining -= counts[j] as base.u32
|
||||
if remaining > (1 << 30) {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
remaining <<= 1
|
||||
j += 1
|
||||
} endwhile
|
||||
if (j <= 9) or (15 < j) {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
j -= 9
|
||||
initial_high_bits = (1 as base.u32) << j
|
||||
|
||||
top = next_top
|
||||
if (top + ((1 as base.u32) << j)) > HUFFS_TABLE_SIZE {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
assert (top + ((1 as base.u32) << j)) <= 1024 via "a <= b: a <= c; c <= b"(c: HUFFS_TABLE_SIZE)
|
||||
next_top = top + ((1 as base.u32) << j)
|
||||
top = next_top
|
||||
if (top + ((1 as base.u32) << j)) > HUFFS_TABLE_SIZE {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
assert (top + ((1 as base.u32) << j)) <= 1024 via "a <= b: a <= c; c <= b"(c: HUFFS_TABLE_SIZE)
|
||||
next_top = top + ((1 as base.u32) << j)
|
||||
|
||||
redirect_key = (REVERSE8[redirect_key >> 1] as base.u32) | ((redirect_key & 1) << 8)
|
||||
this.huffs[args.which][redirect_key] = 0x1000_0009 | (top << 8) | (j << 4)
|
||||
}
|
||||
}
|
||||
if (key >= (1 << 9)) or (counts[prev_cl] <= 0) {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
counts[prev_cl] -= 1
|
||||
redirect_key = (REVERSE8[redirect_key >> 1] as base.u32) | ((redirect_key & 1) << 8)
|
||||
this.huffs[args.which][redirect_key] = 0x1000_0009 | (top << 8) | (j << 4)
|
||||
}
|
||||
}
|
||||
if (key >= (1 << 9)) or (counts[prev_cl] <= 0) {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
counts[prev_cl] -= 1
|
||||
|
||||
reversed_key = (REVERSE8[key >> 1] as base.u32) | ((key & 1) << 8)
|
||||
reversed_key >>= 9 - cl
|
||||
reversed_key = (REVERSE8[key >> 1] as base.u32) | ((key & 1) << 8)
|
||||
reversed_key >>= 9 - cl
|
||||
|
||||
symbol = symbols[i] as base.u32
|
||||
if symbol == 256 {
|
||||
// End-of-block.
|
||||
value = 0x2000_0000 | cl
|
||||
} else if (symbol < 256) and (args.which == 0) {
|
||||
// Literal.
|
||||
value = 0x8000_0000 | (symbol << 8) | cl
|
||||
} else if symbol >= args.base_symbol {
|
||||
// Base number + extra bits.
|
||||
symbol -= args.base_symbol
|
||||
if args.which == 0 {
|
||||
value = LCODE_MAGIC_NUMBERS[symbol & 31] | cl
|
||||
} else {
|
||||
value = DCODE_MAGIC_NUMBERS[symbol & 31] | cl
|
||||
}
|
||||
} else {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
symbol = symbols[i] as base.u32
|
||||
if symbol == 256 {
|
||||
// End-of-block.
|
||||
value = 0x2000_0000 | cl
|
||||
} else if (symbol < 256) and (args.which == 0) {
|
||||
// Literal.
|
||||
value = 0x8000_0000 | (symbol << 8) | cl
|
||||
} else if symbol >= args.base_symbol {
|
||||
// Base number + extra bits.
|
||||
symbol -= args.base_symbol
|
||||
if args.which == 0 {
|
||||
value = LCODE_MAGIC_NUMBERS[symbol & 31] | cl
|
||||
} else {
|
||||
value = DCODE_MAGIC_NUMBERS[symbol & 31] | cl
|
||||
}
|
||||
} else {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
|
||||
// The table uses log2(initial_high_bits) bits, but reversed_key only
|
||||
// has cl bits. We duplicate the key-value pair across all possible
|
||||
// values of the high (log2(initial_high_bits) - cl) bits.
|
||||
high_bits = initial_high_bits
|
||||
delta = (1 as base.u32) << cl
|
||||
while high_bits >= delta,
|
||||
inv code < (1 << 15),
|
||||
inv i < 288,
|
||||
inv n_symbols <= 288,
|
||||
{
|
||||
high_bits -= delta
|
||||
if (top + ((high_bits | reversed_key) & 511)) >= HUFFS_TABLE_SIZE {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
assert (top + ((high_bits | reversed_key) & 511)) < 1024 via "a < b: a < c; c <= b"(c: HUFFS_TABLE_SIZE)
|
||||
this.huffs[args.which][top + ((high_bits | reversed_key) & 511)] = value
|
||||
} endwhile
|
||||
// The table uses log2(initial_high_bits) bits, but reversed_key only
|
||||
// has cl bits. We duplicate the key-value pair across all possible
|
||||
// values of the high (log2(initial_high_bits) - cl) bits.
|
||||
high_bits = initial_high_bits
|
||||
delta = (1 as base.u32) << cl
|
||||
while high_bits >= delta,
|
||||
inv code < (1 << 15),
|
||||
inv i < 288,
|
||||
inv n_symbols <= 288,
|
||||
{
|
||||
high_bits -= delta
|
||||
if (top + ((high_bits | reversed_key) & 511)) >= HUFFS_TABLE_SIZE {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
assert (top + ((high_bits | reversed_key) & 511)) < 1024 via "a < b: a < c; c <= b"(c: HUFFS_TABLE_SIZE)
|
||||
this.huffs[args.which][top + ((high_bits | reversed_key) & 511)] = value
|
||||
} endwhile
|
||||
|
||||
i += 1
|
||||
if i >= n_symbols {
|
||||
break
|
||||
}
|
||||
assert i < 288 via "a < b: a < c; c <= b"(c: n_symbols)
|
||||
code += 1
|
||||
if code >= (1 << 15) {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
} endwhile
|
||||
return ok
|
||||
i += 1
|
||||
if i >= n_symbols {
|
||||
break
|
||||
}
|
||||
assert i < 288 via "a < b: a < c; c <= b"(c: n_symbols)
|
||||
code += 1
|
||||
if code >= (1 << 15) {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
} endwhile
|
||||
return ok
|
||||
}
|
||||
@@ -16,287 +16,287 @@
|
||||
// the "choose cpu_arch >= x86_bmi2". Unsurprisingly, having Bit Manipulation
|
||||
// Instructions available to the compiler can help this function's performance.
|
||||
pri func decoder.decode_huffman_bmi2!(dst: base.io_writer, src: base.io_reader) base.status,
|
||||
choose cpu_arch >= x86_bmi2,
|
||||
choose cpu_arch >= x86_bmi2,
|
||||
{
|
||||
// When editing this function, consider making the equivalent change to the
|
||||
// decode_huffman_slow function. Keep the diff between the two
|
||||
// decode_huffman_*.wuffs files as small as possible, while retaining both
|
||||
// correctness and performance.
|
||||
// When editing this function, consider making the equivalent change to the
|
||||
// decode_huffman_slow function. Keep the diff between the two
|
||||
// decode_huffman_*.wuffs files as small as possible, while retaining both
|
||||
// correctness and performance.
|
||||
|
||||
var bits : base.u64
|
||||
var n_bits : base.u32
|
||||
var table_entry : base.u32
|
||||
var table_entry_n_bits : base.u32[..= 15]
|
||||
var lmask : base.u64[..= 511]
|
||||
var dmask : base.u64[..= 511]
|
||||
var redir_top : base.u32[..= 0xFFFF]
|
||||
var redir_mask : base.u32[..= 0x7FFF]
|
||||
var length : base.u32[..= 258]
|
||||
var dist_minus_1 : base.u32[..= 0x7FFF]
|
||||
var hlen : base.u32[..= 0x7FFF]
|
||||
var hdist : base.u32
|
||||
var hdist_adjustment : base.u32
|
||||
var bits : base.u64
|
||||
var n_bits : base.u32
|
||||
var table_entry : base.u32
|
||||
var table_entry_n_bits : base.u32[..= 15]
|
||||
var lmask : base.u64[..= 511]
|
||||
var dmask : base.u64[..= 511]
|
||||
var redir_top : base.u32[..= 0xFFFF]
|
||||
var redir_mask : base.u32[..= 0x7FFF]
|
||||
var length : base.u32[..= 258]
|
||||
var dist_minus_1 : base.u32[..= 0x7FFF]
|
||||
var hlen : base.u32[..= 0x7FFF]
|
||||
var hdist : base.u32
|
||||
var hdist_adjustment : base.u32
|
||||
|
||||
if (this.n_bits >= 8) or ((this.bits >> (this.n_bits & 7)) <> 0) {
|
||||
return "#internal error: inconsistent n_bits"
|
||||
}
|
||||
if (this.n_bits >= 8) or ((this.bits >> (this.n_bits & 7)) <> 0) {
|
||||
return "#internal error: inconsistent n_bits"
|
||||
}
|
||||
|
||||
bits = this.bits as base.u64
|
||||
n_bits = this.n_bits
|
||||
bits = this.bits as base.u64
|
||||
n_bits = this.n_bits
|
||||
|
||||
lmask = ((1 as base.u64) << this.n_huffs_bits[0]) - 1
|
||||
dmask = ((1 as base.u64) << this.n_huffs_bits[1]) - 1
|
||||
lmask = ((1 as base.u64) << this.n_huffs_bits[0]) - 1
|
||||
dmask = ((1 as base.u64) << this.n_huffs_bits[1]) - 1
|
||||
|
||||
if this.transformed_history_count < args.dst.history_position() {
|
||||
return base."#bad I/O position"
|
||||
}
|
||||
hdist_adjustment = ((this.transformed_history_count - args.dst.history_position()) & 0xFFFF_FFFF) as base.u32
|
||||
if this.transformed_history_count < args.dst.history_position() {
|
||||
return base."#bad I/O position"
|
||||
}
|
||||
hdist_adjustment = ((this.transformed_history_count - args.dst.history_position()) & 0xFFFF_FFFF) as base.u32
|
||||
|
||||
// Check up front, on each iteration, that we have enough buffer space to
|
||||
// both read (8 bytes) and write (266 bytes) as much as we need to. Doing
|
||||
// this check once (per iteration), up front, removes the need to check
|
||||
// multiple times inside the loop body, so it's faster overall.
|
||||
//
|
||||
// For writing, a literal code obviously corresponds to writing 1 byte, and
|
||||
// 258 is the maximum length in a length-distance pair, as specified in the
|
||||
// RFC section 3.2.5. Compressed blocks (length and distance codes).
|
||||
//
|
||||
// We adjust 258 up to (258 + 8) = 266 since it can be faster to overshoot
|
||||
// a little and make multiple-of-8-byte copies even when the length in the
|
||||
// length-distance pair isn't an exact multiple-of-8. Strictly speaking,
|
||||
// 264 (an exact multiple-of-8) is the tight bound but (258 + 8) is easier
|
||||
// for the Wuffs proof system, as length's type is refined to [..= 258],
|
||||
//
|
||||
// For reading, strictly speaking, we only need 6 bytes (48 bits) of
|
||||
// available input, because the H-L Literal/Length code is up to 15 bits
|
||||
// plus up to 5 extra bits, the H-D Distance code is up to 15 bits plus up
|
||||
// to 13 extra bits and 15 + 5 + 15 + 13 == 48. However, it's faster to
|
||||
// read 64 bits than 48 bits or (48 - n_bits) bits.
|
||||
while.loop(args.dst.length() >= 266) and (args.src.length() >= 8) {
|
||||
// Ensure that we have at least 56 bits of input.
|
||||
//
|
||||
// This is "Variant 4" of
|
||||
// https://fgiesen.wordpress.com/2018/02/20/reading-bits-in-far-too-many-ways-part-2/
|
||||
//
|
||||
// 56, the number of bits in 7 bytes, is a property of that "Variant 4"
|
||||
// bit-reading technique, and not related to the DEFLATE format per se.
|
||||
//
|
||||
// The "& 63" is a no-op, and not part of the original "Variant 4"
|
||||
// technique, but satisfies Wuffs' overflow/underflow checks.
|
||||
bits |= args.src.peek_u64le() ~mod<< (n_bits & 63)
|
||||
args.src.skip_u32_fast!(actual: (63 - (n_bits & 63)) >> 3, worst_case: 8)
|
||||
n_bits |= 56
|
||||
// Check up front, on each iteration, that we have enough buffer space to
|
||||
// both read (8 bytes) and write (266 bytes) as much as we need to. Doing
|
||||
// this check once (per iteration), up front, removes the need to check
|
||||
// multiple times inside the loop body, so it's faster overall.
|
||||
//
|
||||
// For writing, a literal code obviously corresponds to writing 1 byte, and
|
||||
// 258 is the maximum length in a length-distance pair, as specified in the
|
||||
// RFC section 3.2.5. Compressed blocks (length and distance codes).
|
||||
//
|
||||
// We adjust 258 up to (258 + 8) = 266 since it can be faster to overshoot
|
||||
// a little and make multiple-of-8-byte copies even when the length in the
|
||||
// length-distance pair isn't an exact multiple-of-8. Strictly speaking,
|
||||
// 264 (an exact multiple-of-8) is the tight bound but (258 + 8) is easier
|
||||
// for the Wuffs proof system, as length's type is refined to [..= 258],
|
||||
//
|
||||
// For reading, strictly speaking, we only need 6 bytes (48 bits) of
|
||||
// available input, because the H-L Literal/Length code is up to 15 bits
|
||||
// plus up to 5 extra bits, the H-D Distance code is up to 15 bits plus up
|
||||
// to 13 extra bits and 15 + 5 + 15 + 13 == 48. However, it's faster to
|
||||
// read 64 bits than 48 bits or (48 - n_bits) bits.
|
||||
while.loop(args.dst.length() >= 266) and (args.src.length() >= 8) {
|
||||
// Ensure that we have at least 56 bits of input.
|
||||
//
|
||||
// This is "Variant 4" of
|
||||
// https://fgiesen.wordpress.com/2018/02/20/reading-bits-in-far-too-many-ways-part-2/
|
||||
//
|
||||
// 56, the number of bits in 7 bytes, is a property of that "Variant 4"
|
||||
// bit-reading technique, and not related to the DEFLATE format per se.
|
||||
//
|
||||
// The "& 63" is a no-op, and not part of the original "Variant 4"
|
||||
// technique, but satisfies Wuffs' overflow/underflow checks.
|
||||
bits |= args.src.peek_u64le() ~mod<< (n_bits & 63)
|
||||
args.src.skip_u32_fast!(actual: (63 - (n_bits & 63)) >> 3, worst_case: 8)
|
||||
n_bits |= 56
|
||||
|
||||
// Decode an lcode symbol from H-L.
|
||||
table_entry = this.huffs[0][bits & lmask]
|
||||
table_entry_n_bits = table_entry & 0x0F
|
||||
bits >>= table_entry_n_bits
|
||||
n_bits ~mod-= table_entry_n_bits
|
||||
// Decode an lcode symbol from H-L.
|
||||
table_entry = this.huffs[0][bits & lmask]
|
||||
table_entry_n_bits = table_entry & 0x0F
|
||||
bits >>= table_entry_n_bits
|
||||
n_bits ~mod-= table_entry_n_bits
|
||||
|
||||
if (table_entry >> 31) <> 0 {
|
||||
// Literal.
|
||||
args.dst.write_u8_fast!(a: ((table_entry >> 8) & 0xFF) as base.u8)
|
||||
continue.loop
|
||||
} else if (table_entry >> 30) <> 0 {
|
||||
// No-op; code continues past the if-else chain.
|
||||
} else if (table_entry >> 29) <> 0 {
|
||||
// End of block.
|
||||
this.end_of_block = true
|
||||
break.loop
|
||||
} else if (table_entry >> 28) <> 0 {
|
||||
// Redirect.
|
||||
redir_top = (table_entry >> 8) & 0xFFFF
|
||||
redir_mask = ((1 as base.u32) << ((table_entry >> 4) & 0x0F)) - 1
|
||||
table_entry = this.huffs[0][
|
||||
(redir_top + (((bits & 0xFFFF_FFFF) as base.u32) & redir_mask)) & HUFFS_TABLE_MASK]
|
||||
table_entry_n_bits = table_entry & 0x0F
|
||||
bits >>= table_entry_n_bits
|
||||
n_bits ~mod-= table_entry_n_bits
|
||||
if (table_entry >> 31) <> 0 {
|
||||
// Literal.
|
||||
args.dst.write_u8_fast!(a: ((table_entry >> 8) & 0xFF) as base.u8)
|
||||
continue.loop
|
||||
} else if (table_entry >> 30) <> 0 {
|
||||
// No-op; code continues past the if-else chain.
|
||||
} else if (table_entry >> 29) <> 0 {
|
||||
// End of block.
|
||||
this.end_of_block = true
|
||||
break.loop
|
||||
} else if (table_entry >> 28) <> 0 {
|
||||
// Redirect.
|
||||
redir_top = (table_entry >> 8) & 0xFFFF
|
||||
redir_mask = ((1 as base.u32) << ((table_entry >> 4) & 0x0F)) - 1
|
||||
table_entry = this.huffs[0][
|
||||
(redir_top + (((bits & 0xFFFF_FFFF) as base.u32) & redir_mask)) & HUFFS_TABLE_MASK]
|
||||
table_entry_n_bits = table_entry & 0x0F
|
||||
bits >>= table_entry_n_bits
|
||||
n_bits ~mod-= table_entry_n_bits
|
||||
|
||||
if (table_entry >> 31) <> 0 {
|
||||
// Literal.
|
||||
args.dst.write_u8_fast!(a: ((table_entry >> 8) & 0xFF) as base.u8)
|
||||
continue.loop
|
||||
} else if (table_entry >> 30) <> 0 {
|
||||
// No-op; code continues past the if-else chain.
|
||||
} else if (table_entry >> 29) <> 0 {
|
||||
// End of block.
|
||||
this.end_of_block = true
|
||||
break.loop
|
||||
} else if (table_entry >> 28) <> 0 {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
} else if (table_entry >> 27) <> 0 {
|
||||
return "#bad Huffman code"
|
||||
} else {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
if (table_entry >> 31) <> 0 {
|
||||
// Literal.
|
||||
args.dst.write_u8_fast!(a: ((table_entry >> 8) & 0xFF) as base.u8)
|
||||
continue.loop
|
||||
} else if (table_entry >> 30) <> 0 {
|
||||
// No-op; code continues past the if-else chain.
|
||||
} else if (table_entry >> 29) <> 0 {
|
||||
// End of block.
|
||||
this.end_of_block = true
|
||||
break.loop
|
||||
} else if (table_entry >> 28) <> 0 {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
} else if (table_entry >> 27) <> 0 {
|
||||
return "#bad Huffman code"
|
||||
} else {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
|
||||
} else if (table_entry >> 27) <> 0 {
|
||||
return "#bad Huffman code"
|
||||
} else {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
} else if (table_entry >> 27) <> 0 {
|
||||
return "#bad Huffman code"
|
||||
} else {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
|
||||
// length = base_number_minus_3 + 3 + extra_bits.
|
||||
//
|
||||
// The -3 is from the bias in script/print-deflate-magic-numbers.go.
|
||||
// That bias makes the "& 0xFF" 1 and 15-ish lines below correct.
|
||||
length = ((table_entry >> 8) & 0xFF) + 3
|
||||
table_entry_n_bits = (table_entry >> 4) & 0x0F
|
||||
if table_entry_n_bits > 0 {
|
||||
// The "+ 253" is the same as "- 3", after the "& 0xFF", but the
|
||||
// plus form won't require an underflow check.
|
||||
length = ((length + 253 + (bits.low_bits(n: table_entry_n_bits) as base.u32)) & 0xFF) + 3
|
||||
bits >>= table_entry_n_bits
|
||||
n_bits ~mod-= table_entry_n_bits
|
||||
}
|
||||
// length = base_number_minus_3 + 3 + extra_bits.
|
||||
//
|
||||
// The -3 is from the bias in script/print-deflate-magic-numbers.go.
|
||||
// That bias makes the "& 0xFF" 1 and 15-ish lines below correct.
|
||||
length = ((table_entry >> 8) & 0xFF) + 3
|
||||
table_entry_n_bits = (table_entry >> 4) & 0x0F
|
||||
if table_entry_n_bits > 0 {
|
||||
// The "+ 253" is the same as "- 3", after the "& 0xFF", but the
|
||||
// plus form won't require an underflow check.
|
||||
length = ((length + 253 + (bits.low_bits(n: table_entry_n_bits) as base.u32)) & 0xFF) + 3
|
||||
bits >>= table_entry_n_bits
|
||||
n_bits ~mod-= table_entry_n_bits
|
||||
}
|
||||
|
||||
// Decode a dcode symbol from H-D.
|
||||
table_entry = this.huffs[1][bits & dmask]
|
||||
table_entry_n_bits = table_entry & 15
|
||||
bits >>= table_entry_n_bits
|
||||
n_bits ~mod-= table_entry_n_bits
|
||||
// Decode a dcode symbol from H-D.
|
||||
table_entry = this.huffs[1][bits & dmask]
|
||||
table_entry_n_bits = table_entry & 15
|
||||
bits >>= table_entry_n_bits
|
||||
n_bits ~mod-= table_entry_n_bits
|
||||
|
||||
// Check for a redirect.
|
||||
if (table_entry >> 28) == 1 {
|
||||
redir_top = (table_entry >> 8) & 0xFFFF
|
||||
redir_mask = ((1 as base.u32) << ((table_entry >> 4) & 0x0F)) - 1
|
||||
table_entry = this.huffs[1][
|
||||
(redir_top + (((bits & 0xFFFF_FFFF) as base.u32) & redir_mask)) & HUFFS_TABLE_MASK]
|
||||
table_entry_n_bits = table_entry & 0x0F
|
||||
bits >>= table_entry_n_bits
|
||||
n_bits ~mod-= table_entry_n_bits
|
||||
}
|
||||
// Check for a redirect.
|
||||
if (table_entry >> 28) == 1 {
|
||||
redir_top = (table_entry >> 8) & 0xFFFF
|
||||
redir_mask = ((1 as base.u32) << ((table_entry >> 4) & 0x0F)) - 1
|
||||
table_entry = this.huffs[1][
|
||||
(redir_top + (((bits & 0xFFFF_FFFF) as base.u32) & redir_mask)) & HUFFS_TABLE_MASK]
|
||||
table_entry_n_bits = table_entry & 0x0F
|
||||
bits >>= table_entry_n_bits
|
||||
n_bits ~mod-= table_entry_n_bits
|
||||
}
|
||||
|
||||
// For H-D, all symbols should be base_number + extra_bits.
|
||||
if (table_entry >> 24) <> 0x40 {
|
||||
if (table_entry >> 24) == 0x08 {
|
||||
return "#bad Huffman code"
|
||||
}
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
// For H-D, all symbols should be base_number + extra_bits.
|
||||
if (table_entry >> 24) <> 0x40 {
|
||||
if (table_entry >> 24) == 0x08 {
|
||||
return "#bad Huffman code"
|
||||
}
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
|
||||
// dist_minus_1 = base_number_minus_1 + extra_bits.
|
||||
// distance = dist_minus_1 + 1.
|
||||
//
|
||||
// The -1 is from the bias in script/print-deflate-magic-numbers.go.
|
||||
// That bias makes the "& 0x7FFF" 2 and 15-ish lines below correct and
|
||||
// undoing that bias makes proving (dist_minus_1 + 1) > 0 trivial.
|
||||
dist_minus_1 = (table_entry >> 8) & 0x7FFF
|
||||
table_entry_n_bits = (table_entry >> 4) & 0x0F
|
||||
// dist_minus_1 = base_number_minus_1 + extra_bits.
|
||||
// distance = dist_minus_1 + 1.
|
||||
//
|
||||
// The -1 is from the bias in script/print-deflate-magic-numbers.go.
|
||||
// That bias makes the "& 0x7FFF" 2 and 15-ish lines below correct and
|
||||
// undoing that bias makes proving (dist_minus_1 + 1) > 0 trivial.
|
||||
dist_minus_1 = (table_entry >> 8) & 0x7FFF
|
||||
table_entry_n_bits = (table_entry >> 4) & 0x0F
|
||||
|
||||
dist_minus_1 = (dist_minus_1 + (bits.low_bits(n: table_entry_n_bits) as base.u32)) & 0x7FFF
|
||||
bits >>= table_entry_n_bits
|
||||
n_bits ~mod-= table_entry_n_bits
|
||||
dist_minus_1 = (dist_minus_1 + (bits.low_bits(n: table_entry_n_bits) as base.u32)) & 0x7FFF
|
||||
bits >>= table_entry_n_bits
|
||||
n_bits ~mod-= table_entry_n_bits
|
||||
|
||||
// The "while true { etc; break }" is a redundant version of "etc", but
|
||||
// its presence minimizes the diff between decode_huffman_fastxx and
|
||||
// decode_huffman_slow.
|
||||
while true,
|
||||
pre args.dst.length() >= 266,
|
||||
{
|
||||
// We can therefore prove:
|
||||
assert (length as base.u64) <= args.dst.length() via "a <= b: a <= c; c <= b"(c: 266)
|
||||
assert ((length + 8) as base.u64) <= args.dst.length() via "a <= b: a <= c; c <= b"(c: 266)
|
||||
// The "while true { etc; break }" is a redundant version of "etc", but
|
||||
// its presence minimizes the diff between decode_huffman_fastxx and
|
||||
// decode_huffman_slow.
|
||||
while true,
|
||||
pre args.dst.length() >= 266,
|
||||
{
|
||||
// We can therefore prove:
|
||||
assert (length as base.u64) <= args.dst.length() via "a <= b: a <= c; c <= b"(c: 266)
|
||||
assert ((length + 8) as base.u64) <= args.dst.length() via "a <= b: a <= c; c <= b"(c: 266)
|
||||
|
||||
// Copy from this.history.
|
||||
if ((dist_minus_1 + 1) as base.u64) > args.dst.history_length() {
|
||||
// Set (hlen, hdist) to be the length-distance pair to copy
|
||||
// from this.history, and (length, distance) to be the
|
||||
// remaining length-distance pair to copy from args.dst.
|
||||
hlen = 0
|
||||
hdist = (((dist_minus_1 + 1) as base.u64) - args.dst.history_length()) as base.u32
|
||||
if length > hdist {
|
||||
assert hdist < length via "a < b: b > a"()
|
||||
assert hdist < 0x8000 via "a < b: a < c; c <= b"(c: length)
|
||||
length -= hdist
|
||||
hlen = hdist
|
||||
} else {
|
||||
hlen = length
|
||||
length = 0
|
||||
}
|
||||
hdist ~mod+= hdist_adjustment
|
||||
if this.history_index < hdist {
|
||||
return "#bad distance"
|
||||
}
|
||||
// Copy from this.history.
|
||||
if ((dist_minus_1 + 1) as base.u64) > args.dst.history_length() {
|
||||
// Set (hlen, hdist) to be the length-distance pair to copy
|
||||
// from this.history, and (length, distance) to be the
|
||||
// remaining length-distance pair to copy from args.dst.
|
||||
hlen = 0
|
||||
hdist = (((dist_minus_1 + 1) as base.u64) - args.dst.history_length()) as base.u32
|
||||
if length > hdist {
|
||||
assert hdist < length via "a < b: b > a"()
|
||||
assert hdist < 0x8000 via "a < b: a < c; c <= b"(c: length)
|
||||
length -= hdist
|
||||
hlen = hdist
|
||||
} else {
|
||||
hlen = length
|
||||
length = 0
|
||||
}
|
||||
hdist ~mod+= hdist_adjustment
|
||||
if this.history_index < hdist {
|
||||
return "#bad distance"
|
||||
}
|
||||
|
||||
// Copy from this.history[(this.history_index - hdist) ..].
|
||||
//
|
||||
// This copying is simpler than the decode_huffman_slow version
|
||||
// because it cannot yield. We have already checked that
|
||||
// args.dst.length() is large enough.
|
||||
args.dst.limited_copy_u32_from_slice!(
|
||||
up_to: hlen, s: this.history[(this.history_index - hdist) & 0x7FFF ..])
|
||||
if length == 0 {
|
||||
// No need to copy from args.dst.
|
||||
continue.loop
|
||||
}
|
||||
// Copy from this.history[(this.history_index - hdist) ..].
|
||||
//
|
||||
// This copying is simpler than the decode_huffman_slow version
|
||||
// because it cannot yield. We have already checked that
|
||||
// args.dst.length() is large enough.
|
||||
args.dst.limited_copy_u32_from_slice!(
|
||||
up_to: hlen, s: this.history[(this.history_index - hdist) & 0x7FFF ..])
|
||||
if length == 0 {
|
||||
// No need to copy from args.dst.
|
||||
continue.loop
|
||||
}
|
||||
|
||||
if (((dist_minus_1 + 1) as base.u64) > args.dst.history_length()) or
|
||||
((length as base.u64) > args.dst.length()) or
|
||||
(((length + 8) as base.u64) > args.dst.length()) {
|
||||
return "#internal error: inconsistent distance"
|
||||
}
|
||||
}
|
||||
// Once again, redundant but explicit assertions.
|
||||
assert (dist_minus_1 + 1) >= 1
|
||||
assert ((dist_minus_1 + 1) as base.u64) <= args.dst.history_length()
|
||||
assert (length as base.u64) <= args.dst.length()
|
||||
assert ((length + 8) as base.u64) <= args.dst.length()
|
||||
if (((dist_minus_1 + 1) as base.u64) > args.dst.history_length()) or
|
||||
((length as base.u64) > args.dst.length()) or
|
||||
(((length + 8) as base.u64) > args.dst.length()) {
|
||||
return "#internal error: inconsistent distance"
|
||||
}
|
||||
}
|
||||
// Once again, redundant but explicit assertions.
|
||||
assert (dist_minus_1 + 1) >= 1
|
||||
assert ((dist_minus_1 + 1) as base.u64) <= args.dst.history_length()
|
||||
assert (length as base.u64) <= args.dst.length()
|
||||
assert ((length + 8) as base.u64) <= args.dst.length()
|
||||
|
||||
// Copy from args.dst.
|
||||
//
|
||||
// For short distances, less than 8 bytes, copying atomic 8-byte
|
||||
// chunks can result in incorrect output, so we fall back to a
|
||||
// slower 1-byte-at-a-time copy. Deflate's copy-from-history can
|
||||
// pick up freshly written bytes. A length = 5, distance = 2 copy
|
||||
// starting with "abc" should give "abcbcbcb" but the 8-byte chunk
|
||||
// technique would give "abcbc???", the exact output depending on
|
||||
// what was previously in the writer buffer.
|
||||
if (dist_minus_1 + 1) >= 8 {
|
||||
args.dst.limited_copy_u32_from_history_8_byte_chunks_fast!(
|
||||
up_to: length, distance: (dist_minus_1 + 1))
|
||||
} else if (dist_minus_1 + 1) == 1 {
|
||||
// (distance == 1) is essentially RLE (Run Length Encoding). It
|
||||
// happens often enough that it's worth special-casing.
|
||||
args.dst.limited_copy_u32_from_history_8_byte_chunks_distance_1_fast!(
|
||||
up_to: length, distance: (dist_minus_1 + 1))
|
||||
} else {
|
||||
args.dst.limited_copy_u32_from_history_fast!(
|
||||
up_to: length, distance: (dist_minus_1 + 1))
|
||||
}
|
||||
break
|
||||
} endwhile
|
||||
} endwhile.loop
|
||||
// Copy from args.dst.
|
||||
//
|
||||
// For short distances, less than 8 bytes, copying atomic 8-byte
|
||||
// chunks can result in incorrect output, so we fall back to a
|
||||
// slower 1-byte-at-a-time copy. Deflate's copy-from-history can
|
||||
// pick up freshly written bytes. A length = 5, distance = 2 copy
|
||||
// starting with "abc" should give "abcbcbcb" but the 8-byte chunk
|
||||
// technique would give "abcbc???", the exact output depending on
|
||||
// what was previously in the writer buffer.
|
||||
if (dist_minus_1 + 1) >= 8 {
|
||||
args.dst.limited_copy_u32_from_history_8_byte_chunks_fast!(
|
||||
up_to: length, distance: (dist_minus_1 + 1))
|
||||
} else if (dist_minus_1 + 1) == 1 {
|
||||
// (distance == 1) is essentially RLE (Run Length Encoding). It
|
||||
// happens often enough that it's worth special-casing.
|
||||
args.dst.limited_copy_u32_from_history_8_byte_chunks_distance_1_fast!(
|
||||
up_to: length, distance: (dist_minus_1 + 1))
|
||||
} else {
|
||||
args.dst.limited_copy_u32_from_history_fast!(
|
||||
up_to: length, distance: (dist_minus_1 + 1))
|
||||
}
|
||||
break
|
||||
} endwhile
|
||||
} endwhile.loop
|
||||
|
||||
// Ensure n_bits < 8 by rewindng args.src, if we loaded too many of its
|
||||
// bytes into the bits variable.
|
||||
//
|
||||
// Note that we can unconditionally call undo_read (without resulting in an
|
||||
// "invalid I/O operation" error code) only because this whole function can
|
||||
// never suspend, as all of its I/O operations were checked beforehand for
|
||||
// sufficient buffer space. Otherwise, resuming from the suspension could
|
||||
// mean that the (possibly different) args.src is no longer rewindable,
|
||||
// even if conceptually, this function was responsible for reading the
|
||||
// bytes we want to rewind.
|
||||
if n_bits > 63 {
|
||||
return "#internal error: inconsistent n_bits"
|
||||
}
|
||||
while n_bits >= 8,
|
||||
post n_bits < 8,
|
||||
{
|
||||
n_bits -= 8
|
||||
if args.src.can_undo_byte() {
|
||||
args.src.undo_byte!()
|
||||
} else {
|
||||
return "#internal error: inconsistent I/O"
|
||||
}
|
||||
} endwhile
|
||||
// Ensure n_bits < 8 by rewindng args.src, if we loaded too many of its
|
||||
// bytes into the bits variable.
|
||||
//
|
||||
// Note that we can unconditionally call undo_read (without resulting in an
|
||||
// "invalid I/O operation" error code) only because this whole function can
|
||||
// never suspend, as all of its I/O operations were checked beforehand for
|
||||
// sufficient buffer space. Otherwise, resuming from the suspension could
|
||||
// mean that the (possibly different) args.src is no longer rewindable,
|
||||
// even if conceptually, this function was responsible for reading the
|
||||
// bytes we want to rewind.
|
||||
if n_bits > 63 {
|
||||
return "#internal error: inconsistent n_bits"
|
||||
}
|
||||
while n_bits >= 8,
|
||||
post n_bits < 8,
|
||||
{
|
||||
n_bits -= 8
|
||||
if args.src.can_undo_byte() {
|
||||
args.src.undo_byte!()
|
||||
} else {
|
||||
return "#internal error: inconsistent I/O"
|
||||
}
|
||||
} endwhile
|
||||
|
||||
this.bits = (bits & (((1 as base.u64) << n_bits) - 1)) as base.u32
|
||||
this.n_bits = n_bits
|
||||
this.bits = (bits & (((1 as base.u64) << n_bits) - 1)) as base.u32
|
||||
this.n_bits = n_bits
|
||||
|
||||
if (this.n_bits >= 8) or ((this.bits >> this.n_bits) <> 0) {
|
||||
return "#internal error: inconsistent n_bits"
|
||||
}
|
||||
if (this.n_bits >= 8) or ((this.bits >> this.n_bits) <> 0) {
|
||||
return "#internal error: inconsistent n_bits"
|
||||
}
|
||||
}
|
||||
@@ -15,385 +15,385 @@
|
||||
// TODO: describe how the xxx_fastxx version differs from the xxx_slow one, the
|
||||
// assumptions that xxx_fastxx makes, and how that makes it fast.
|
||||
pri func decoder.decode_huffman_fast32!(dst: base.io_writer, src: base.io_reader) base.status {
|
||||
// When editing this function, consider making the equivalent change to the
|
||||
// decode_huffman_slow function. Keep the diff between the two
|
||||
// decode_huffman_*.wuffs files as small as possible, while retaining both
|
||||
// correctness and performance.
|
||||
// When editing this function, consider making the equivalent change to the
|
||||
// decode_huffman_slow function. Keep the diff between the two
|
||||
// decode_huffman_*.wuffs files as small as possible, while retaining both
|
||||
// correctness and performance.
|
||||
|
||||
var bits : base.u32
|
||||
var n_bits : base.u32
|
||||
var table_entry : base.u32
|
||||
var table_entry_n_bits : base.u32[..= 15]
|
||||
var lmask : base.u32[..= 511]
|
||||
var dmask : base.u32[..= 511]
|
||||
var redir_top : base.u32[..= 0xFFFF]
|
||||
var redir_mask : base.u32[..= 0x7FFF]
|
||||
var length : base.u32[..= 258]
|
||||
var dist_minus_1 : base.u32[..= 0x7FFF]
|
||||
var hlen : base.u32[..= 0x7FFF]
|
||||
var hdist : base.u32
|
||||
var hdist_adjustment : base.u32
|
||||
var bits : base.u32
|
||||
var n_bits : base.u32
|
||||
var table_entry : base.u32
|
||||
var table_entry_n_bits : base.u32[..= 15]
|
||||
var lmask : base.u32[..= 511]
|
||||
var dmask : base.u32[..= 511]
|
||||
var redir_top : base.u32[..= 0xFFFF]
|
||||
var redir_mask : base.u32[..= 0x7FFF]
|
||||
var length : base.u32[..= 258]
|
||||
var dist_minus_1 : base.u32[..= 0x7FFF]
|
||||
var hlen : base.u32[..= 0x7FFF]
|
||||
var hdist : base.u32
|
||||
var hdist_adjustment : base.u32
|
||||
|
||||
if (this.n_bits >= 8) or ((this.bits >> (this.n_bits & 7)) <> 0) {
|
||||
return "#internal error: inconsistent n_bits"
|
||||
}
|
||||
if (this.n_bits >= 8) or ((this.bits >> (this.n_bits & 7)) <> 0) {
|
||||
return "#internal error: inconsistent n_bits"
|
||||
}
|
||||
|
||||
bits = this.bits
|
||||
n_bits = this.n_bits
|
||||
bits = this.bits
|
||||
n_bits = this.n_bits
|
||||
|
||||
lmask = ((1 as base.u32) << this.n_huffs_bits[0]) - 1
|
||||
dmask = ((1 as base.u32) << this.n_huffs_bits[1]) - 1
|
||||
lmask = ((1 as base.u32) << this.n_huffs_bits[0]) - 1
|
||||
dmask = ((1 as base.u32) << this.n_huffs_bits[1]) - 1
|
||||
|
||||
if this.transformed_history_count < args.dst.history_position() {
|
||||
return base."#bad I/O position"
|
||||
}
|
||||
hdist_adjustment = ((this.transformed_history_count - args.dst.history_position()) & 0xFFFF_FFFF) as base.u32
|
||||
if this.transformed_history_count < args.dst.history_position() {
|
||||
return base."#bad I/O position"
|
||||
}
|
||||
hdist_adjustment = ((this.transformed_history_count - args.dst.history_position()) & 0xFFFF_FFFF) as base.u32
|
||||
|
||||
// Check up front, on each iteration, that we have enough buffer space to
|
||||
// both read (12 bytes) and write (266 bytes) as much as we need to. Doing
|
||||
// this check once (per iteration), up front, removes the need to check
|
||||
// multiple times inside the loop body, so it's faster overall.
|
||||
//
|
||||
// For writing, a literal code obviously corresponds to writing 1 byte, and
|
||||
// 258 is the maximum length in a length-distance pair, as specified in the
|
||||
// RFC section 3.2.5. Compressed blocks (length and distance codes).
|
||||
//
|
||||
// We adjust 258 up to (258 + 8) = 266 since it can be faster to overshoot
|
||||
// a little and make multiple-of-8-byte copies even when the length in the
|
||||
// length-distance pair isn't an exact multiple-of-8. Strictly speaking,
|
||||
// 264 (an exact multiple-of-8) is the tight bound but (258 + 8) is easier
|
||||
// for the Wuffs proof system, as length's type is refined to [..= 258],
|
||||
//
|
||||
// For reading, strictly speaking, we only need 6 bytes (48 bits) of
|
||||
// available input, because the H-L Literal/Length code is up to 15 bits
|
||||
// plus up to 5 extra bits, the H-D Distance code is up to 15 bits plus up
|
||||
// to 13 extra bits and 15 + 5 + 15 + 13 == 48. However,
|
||||
// args.src.length() assertions are made in terms of bytes, not bits.
|
||||
//
|
||||
// In theory, we could write assertions that mixed bytes and bits, such as:
|
||||
//
|
||||
// assert ((args.src.length() >= 6) and (n_bits >= 5)) or
|
||||
// ((args.src.length() >= 4) and (n_bits >= 21))
|
||||
//
|
||||
// but that looks clumsy and makes the proofs more complicated. Instead, we
|
||||
// are conservative and work in terms of bytes only. In bytes, each code
|
||||
// requires either one (direct) or two (redirect) lookups, up to 2 bytes
|
||||
// for each. There are also up to 13 extra bits, or 2 bytes. Thus, each
|
||||
// code requires up to 2 + 2 + 2 == 6 bytes, and there are two codes (one
|
||||
// from H-L and one from H-D). Therefore, we check for at least 12 bytes.
|
||||
while.loop(args.dst.length() >= 266) and (args.src.length() >= 12) {
|
||||
// Ensure that we have at least 15 bits of input.
|
||||
if n_bits < 15 {
|
||||
assert n_bits >= 0 // TODO: this shouldn't be necessary.
|
||||
bits |= args.src.peek_u8_as_u32() << n_bits
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
n_bits += 8
|
||||
bits |= args.src.peek_u8_as_u32() << n_bits
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
n_bits += 8
|
||||
assert n_bits >= 15
|
||||
} else {
|
||||
assert args.src.length() >= 10
|
||||
}
|
||||
// These assertions are redundant, but are listed explicitly for
|
||||
// clarity. They must hold regardless of which branch was taken for the
|
||||
// if statement above.
|
||||
assert args.src.length() >= 10
|
||||
assert n_bits >= 15
|
||||
// Check up front, on each iteration, that we have enough buffer space to
|
||||
// both read (12 bytes) and write (266 bytes) as much as we need to. Doing
|
||||
// this check once (per iteration), up front, removes the need to check
|
||||
// multiple times inside the loop body, so it's faster overall.
|
||||
//
|
||||
// For writing, a literal code obviously corresponds to writing 1 byte, and
|
||||
// 258 is the maximum length in a length-distance pair, as specified in the
|
||||
// RFC section 3.2.5. Compressed blocks (length and distance codes).
|
||||
//
|
||||
// We adjust 258 up to (258 + 8) = 266 since it can be faster to overshoot
|
||||
// a little and make multiple-of-8-byte copies even when the length in the
|
||||
// length-distance pair isn't an exact multiple-of-8. Strictly speaking,
|
||||
// 264 (an exact multiple-of-8) is the tight bound but (258 + 8) is easier
|
||||
// for the Wuffs proof system, as length's type is refined to [..= 258],
|
||||
//
|
||||
// For reading, strictly speaking, we only need 6 bytes (48 bits) of
|
||||
// available input, because the H-L Literal/Length code is up to 15 bits
|
||||
// plus up to 5 extra bits, the H-D Distance code is up to 15 bits plus up
|
||||
// to 13 extra bits and 15 + 5 + 15 + 13 == 48. However,
|
||||
// args.src.length() assertions are made in terms of bytes, not bits.
|
||||
//
|
||||
// In theory, we could write assertions that mixed bytes and bits, such as:
|
||||
//
|
||||
// assert ((args.src.length() >= 6) and (n_bits >= 5)) or
|
||||
// ((args.src.length() >= 4) and (n_bits >= 21))
|
||||
//
|
||||
// but that looks clumsy and makes the proofs more complicated. Instead, we
|
||||
// are conservative and work in terms of bytes only. In bytes, each code
|
||||
// requires either one (direct) or two (redirect) lookups, up to 2 bytes
|
||||
// for each. There are also up to 13 extra bits, or 2 bytes. Thus, each
|
||||
// code requires up to 2 + 2 + 2 == 6 bytes, and there are two codes (one
|
||||
// from H-L and one from H-D). Therefore, we check for at least 12 bytes.
|
||||
while.loop(args.dst.length() >= 266) and (args.src.length() >= 12) {
|
||||
// Ensure that we have at least 15 bits of input.
|
||||
if n_bits < 15 {
|
||||
assert n_bits >= 0 // TODO: this shouldn't be necessary.
|
||||
bits |= args.src.peek_u8_as_u32() << n_bits
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
n_bits += 8
|
||||
bits |= args.src.peek_u8_as_u32() << n_bits
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
n_bits += 8
|
||||
assert n_bits >= 15
|
||||
} else {
|
||||
assert args.src.length() >= 10
|
||||
}
|
||||
// These assertions are redundant, but are listed explicitly for
|
||||
// clarity. They must hold regardless of which branch was taken for the
|
||||
// if statement above.
|
||||
assert args.src.length() >= 10
|
||||
assert n_bits >= 15
|
||||
|
||||
// Decode an lcode symbol from H-L.
|
||||
table_entry = this.huffs[0][bits & lmask]
|
||||
table_entry_n_bits = table_entry & 0x0F
|
||||
bits >>= table_entry_n_bits
|
||||
n_bits -= table_entry_n_bits
|
||||
// Decode an lcode symbol from H-L.
|
||||
table_entry = this.huffs[0][bits & lmask]
|
||||
table_entry_n_bits = table_entry & 0x0F
|
||||
bits >>= table_entry_n_bits
|
||||
n_bits -= table_entry_n_bits
|
||||
|
||||
if (table_entry >> 31) <> 0 {
|
||||
// Literal.
|
||||
args.dst.write_u8_fast!(a: ((table_entry >> 8) & 0xFF) as base.u8)
|
||||
continue.loop
|
||||
} else if (table_entry >> 30) <> 0 {
|
||||
// No-op; code continues past the if-else chain.
|
||||
assert args.src.length() >= 8
|
||||
} else if (table_entry >> 29) <> 0 {
|
||||
// End of block.
|
||||
this.end_of_block = true
|
||||
break.loop
|
||||
} else if (table_entry >> 28) <> 0 {
|
||||
// Redirect.
|
||||
if (table_entry >> 31) <> 0 {
|
||||
// Literal.
|
||||
args.dst.write_u8_fast!(a: ((table_entry >> 8) & 0xFF) as base.u8)
|
||||
continue.loop
|
||||
} else if (table_entry >> 30) <> 0 {
|
||||
// No-op; code continues past the if-else chain.
|
||||
assert args.src.length() >= 8
|
||||
} else if (table_entry >> 29) <> 0 {
|
||||
// End of block.
|
||||
this.end_of_block = true
|
||||
break.loop
|
||||
} else if (table_entry >> 28) <> 0 {
|
||||
// Redirect.
|
||||
|
||||
// Ensure that we have at least 15 bits of input.
|
||||
if n_bits < 15 {
|
||||
assert n_bits >= 0 // TODO: this shouldn't be necessary.
|
||||
bits |= args.src.peek_u8_as_u32() << n_bits
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
n_bits += 8
|
||||
bits |= args.src.peek_u8_as_u32() << n_bits
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
n_bits += 8
|
||||
assert n_bits >= 15
|
||||
} else {
|
||||
assert args.src.length() >= 8
|
||||
}
|
||||
// Once again, redundant but explicit assertions.
|
||||
assert args.src.length() >= 8
|
||||
assert n_bits >= 15
|
||||
// Ensure that we have at least 15 bits of input.
|
||||
if n_bits < 15 {
|
||||
assert n_bits >= 0 // TODO: this shouldn't be necessary.
|
||||
bits |= args.src.peek_u8_as_u32() << n_bits
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
n_bits += 8
|
||||
bits |= args.src.peek_u8_as_u32() << n_bits
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
n_bits += 8
|
||||
assert n_bits >= 15
|
||||
} else {
|
||||
assert args.src.length() >= 8
|
||||
}
|
||||
// Once again, redundant but explicit assertions.
|
||||
assert args.src.length() >= 8
|
||||
assert n_bits >= 15
|
||||
|
||||
redir_top = (table_entry >> 8) & 0xFFFF
|
||||
redir_mask = ((1 as base.u32) << ((table_entry >> 4) & 0x0F)) - 1
|
||||
table_entry = this.huffs[0][(redir_top + (bits & redir_mask)) & HUFFS_TABLE_MASK]
|
||||
table_entry_n_bits = table_entry & 0x0F
|
||||
bits >>= table_entry_n_bits
|
||||
n_bits -= table_entry_n_bits
|
||||
redir_top = (table_entry >> 8) & 0xFFFF
|
||||
redir_mask = ((1 as base.u32) << ((table_entry >> 4) & 0x0F)) - 1
|
||||
table_entry = this.huffs[0][(redir_top + (bits & redir_mask)) & HUFFS_TABLE_MASK]
|
||||
table_entry_n_bits = table_entry & 0x0F
|
||||
bits >>= table_entry_n_bits
|
||||
n_bits -= table_entry_n_bits
|
||||
|
||||
if (table_entry >> 31) <> 0 {
|
||||
// Literal.
|
||||
args.dst.write_u8_fast!(a: ((table_entry >> 8) & 0xFF) as base.u8)
|
||||
continue.loop
|
||||
} else if (table_entry >> 30) <> 0 {
|
||||
// No-op; code continues past the if-else chain.
|
||||
} else if (table_entry >> 29) <> 0 {
|
||||
// End of block.
|
||||
this.end_of_block = true
|
||||
break.loop
|
||||
} else if (table_entry >> 28) <> 0 {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
} else if (table_entry >> 27) <> 0 {
|
||||
return "#bad Huffman code"
|
||||
} else {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
if (table_entry >> 31) <> 0 {
|
||||
// Literal.
|
||||
args.dst.write_u8_fast!(a: ((table_entry >> 8) & 0xFF) as base.u8)
|
||||
continue.loop
|
||||
} else if (table_entry >> 30) <> 0 {
|
||||
// No-op; code continues past the if-else chain.
|
||||
} else if (table_entry >> 29) <> 0 {
|
||||
// End of block.
|
||||
this.end_of_block = true
|
||||
break.loop
|
||||
} else if (table_entry >> 28) <> 0 {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
} else if (table_entry >> 27) <> 0 {
|
||||
return "#bad Huffman code"
|
||||
} else {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
|
||||
// Once again, redundant but explicit assertions.
|
||||
assert args.src.length() >= 8
|
||||
// Once again, redundant but explicit assertions.
|
||||
assert args.src.length() >= 8
|
||||
|
||||
} else if (table_entry >> 27) <> 0 {
|
||||
return "#bad Huffman code"
|
||||
} else {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
} else if (table_entry >> 27) <> 0 {
|
||||
return "#bad Huffman code"
|
||||
} else {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
|
||||
// length = base_number_minus_3 + 3 + extra_bits.
|
||||
//
|
||||
// The -3 is from the bias in script/print-deflate-magic-numbers.go.
|
||||
// That bias makes the "& 0xFF" 1 and 15-ish lines below correct.
|
||||
length = ((table_entry >> 8) & 0xFF) + 3
|
||||
table_entry_n_bits = (table_entry >> 4) & 0x0F
|
||||
if table_entry_n_bits > 0 {
|
||||
// Ensure that we have at least 15 bits of input.
|
||||
if n_bits < 15 {
|
||||
assert n_bits >= 0 // TODO: this shouldn't be necessary.
|
||||
bits |= args.src.peek_u8_as_u32() << n_bits
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
n_bits += 8
|
||||
bits |= args.src.peek_u8_as_u32() << n_bits
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
n_bits += 8
|
||||
assert n_bits >= 15
|
||||
} else {
|
||||
assert args.src.length() >= 6
|
||||
}
|
||||
// Once again, redundant but explicit assertions.
|
||||
assert args.src.length() >= 6
|
||||
assert n_bits >= 15
|
||||
// length = base_number_minus_3 + 3 + extra_bits.
|
||||
//
|
||||
// The -3 is from the bias in script/print-deflate-magic-numbers.go.
|
||||
// That bias makes the "& 0xFF" 1 and 15-ish lines below correct.
|
||||
length = ((table_entry >> 8) & 0xFF) + 3
|
||||
table_entry_n_bits = (table_entry >> 4) & 0x0F
|
||||
if table_entry_n_bits > 0 {
|
||||
// Ensure that we have at least 15 bits of input.
|
||||
if n_bits < 15 {
|
||||
assert n_bits >= 0 // TODO: this shouldn't be necessary.
|
||||
bits |= args.src.peek_u8_as_u32() << n_bits
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
n_bits += 8
|
||||
bits |= args.src.peek_u8_as_u32() << n_bits
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
n_bits += 8
|
||||
assert n_bits >= 15
|
||||
} else {
|
||||
assert args.src.length() >= 6
|
||||
}
|
||||
// Once again, redundant but explicit assertions.
|
||||
assert args.src.length() >= 6
|
||||
assert n_bits >= 15
|
||||
|
||||
// The "+ 253" is the same as "- 3", after the "& 0xFF", but the
|
||||
// plus form won't require an underflow check.
|
||||
length = ((length + 253 + bits.low_bits(n: table_entry_n_bits)) & 0xFF) + 3
|
||||
bits >>= table_entry_n_bits
|
||||
n_bits -= table_entry_n_bits
|
||||
// The "+ 253" is the same as "- 3", after the "& 0xFF", but the
|
||||
// plus form won't require an underflow check.
|
||||
length = ((length + 253 + bits.low_bits(n: table_entry_n_bits)) & 0xFF) + 3
|
||||
bits >>= table_entry_n_bits
|
||||
n_bits -= table_entry_n_bits
|
||||
|
||||
} else {
|
||||
assert args.src.length() >= 6
|
||||
}
|
||||
} else {
|
||||
assert args.src.length() >= 6
|
||||
}
|
||||
|
||||
// Ensure that we have at least 15 bits of input.
|
||||
if n_bits < 15 {
|
||||
assert n_bits >= 0 // TODO: this shouldn't be necessary.
|
||||
bits |= args.src.peek_u8_as_u32() << n_bits
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
n_bits += 8
|
||||
bits |= args.src.peek_u8_as_u32() << n_bits
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
n_bits += 8
|
||||
assert n_bits >= 15
|
||||
} else {
|
||||
assert args.src.length() >= 4
|
||||
}
|
||||
// Once again, redundant but explicit assertions.
|
||||
assert args.src.length() >= 4
|
||||
assert n_bits >= 15
|
||||
// Ensure that we have at least 15 bits of input.
|
||||
if n_bits < 15 {
|
||||
assert n_bits >= 0 // TODO: this shouldn't be necessary.
|
||||
bits |= args.src.peek_u8_as_u32() << n_bits
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
n_bits += 8
|
||||
bits |= args.src.peek_u8_as_u32() << n_bits
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
n_bits += 8
|
||||
assert n_bits >= 15
|
||||
} else {
|
||||
assert args.src.length() >= 4
|
||||
}
|
||||
// Once again, redundant but explicit assertions.
|
||||
assert args.src.length() >= 4
|
||||
assert n_bits >= 15
|
||||
|
||||
// Decode a dcode symbol from H-D.
|
||||
table_entry = this.huffs[1][bits & dmask]
|
||||
table_entry_n_bits = table_entry & 15
|
||||
bits >>= table_entry_n_bits
|
||||
n_bits -= table_entry_n_bits
|
||||
// Decode a dcode symbol from H-D.
|
||||
table_entry = this.huffs[1][bits & dmask]
|
||||
table_entry_n_bits = table_entry & 15
|
||||
bits >>= table_entry_n_bits
|
||||
n_bits -= table_entry_n_bits
|
||||
|
||||
// Check for a redirect.
|
||||
if (table_entry >> 28) == 1 {
|
||||
// Ensure that we have at least 15 bits of input.
|
||||
if n_bits < 15 {
|
||||
assert n_bits >= 0 // TODO: this shouldn't be necessary.
|
||||
bits |= args.src.peek_u8_as_u32() << n_bits
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
n_bits += 8
|
||||
bits |= args.src.peek_u8_as_u32() << n_bits
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
n_bits += 8
|
||||
assert n_bits >= 15
|
||||
} else {
|
||||
assert args.src.length() >= 2
|
||||
}
|
||||
// Once again, redundant but explicit assertions.
|
||||
assert args.src.length() >= 2
|
||||
assert n_bits >= 15
|
||||
// Check for a redirect.
|
||||
if (table_entry >> 28) == 1 {
|
||||
// Ensure that we have at least 15 bits of input.
|
||||
if n_bits < 15 {
|
||||
assert n_bits >= 0 // TODO: this shouldn't be necessary.
|
||||
bits |= args.src.peek_u8_as_u32() << n_bits
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
n_bits += 8
|
||||
bits |= args.src.peek_u8_as_u32() << n_bits
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
n_bits += 8
|
||||
assert n_bits >= 15
|
||||
} else {
|
||||
assert args.src.length() >= 2
|
||||
}
|
||||
// Once again, redundant but explicit assertions.
|
||||
assert args.src.length() >= 2
|
||||
assert n_bits >= 15
|
||||
|
||||
redir_top = (table_entry >> 8) & 0xFFFF
|
||||
redir_mask = ((1 as base.u32) << ((table_entry >> 4) & 0x0F)) - 1
|
||||
table_entry = this.huffs[1][(redir_top + (bits & redir_mask)) & HUFFS_TABLE_MASK]
|
||||
table_entry_n_bits = table_entry & 0x0F
|
||||
bits >>= table_entry_n_bits
|
||||
n_bits -= table_entry_n_bits
|
||||
} else {
|
||||
assert args.src.length() >= 2
|
||||
}
|
||||
redir_top = (table_entry >> 8) & 0xFFFF
|
||||
redir_mask = ((1 as base.u32) << ((table_entry >> 4) & 0x0F)) - 1
|
||||
table_entry = this.huffs[1][(redir_top + (bits & redir_mask)) & HUFFS_TABLE_MASK]
|
||||
table_entry_n_bits = table_entry & 0x0F
|
||||
bits >>= table_entry_n_bits
|
||||
n_bits -= table_entry_n_bits
|
||||
} else {
|
||||
assert args.src.length() >= 2
|
||||
}
|
||||
|
||||
// For H-D, all symbols should be base_number + extra_bits.
|
||||
if (table_entry >> 24) <> 0x40 {
|
||||
if (table_entry >> 24) == 0x08 {
|
||||
return "#bad Huffman code"
|
||||
}
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
// For H-D, all symbols should be base_number + extra_bits.
|
||||
if (table_entry >> 24) <> 0x40 {
|
||||
if (table_entry >> 24) == 0x08 {
|
||||
return "#bad Huffman code"
|
||||
}
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
|
||||
// dist_minus_1 = base_number_minus_1 + extra_bits.
|
||||
// distance = dist_minus_1 + 1.
|
||||
//
|
||||
// The -1 is from the bias in script/print-deflate-magic-numbers.go.
|
||||
// That bias makes the "& 0x7FFF" 2 and 15-ish lines below correct and
|
||||
// undoing that bias makes proving (dist_minus_1 + 1) > 0 trivial.
|
||||
dist_minus_1 = (table_entry >> 8) & 0x7FFF
|
||||
table_entry_n_bits = (table_entry >> 4) & 0x0F
|
||||
// Ensure that we have at least table_entry_n_bits bits of input.
|
||||
if n_bits < table_entry_n_bits {
|
||||
assert n_bits < 15 via "a < b: a < c; c <= b"(c: table_entry_n_bits)
|
||||
assert n_bits >= 0 // TODO: this shouldn't be necessary.
|
||||
bits |= args.src.peek_u8_as_u32() << n_bits
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
n_bits += 8
|
||||
bits |= args.src.peek_u8_as_u32() << n_bits
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
n_bits += 8
|
||||
assert table_entry_n_bits <= n_bits via "a <= b: a <= c; c <= b"(c: 16)
|
||||
assert n_bits >= table_entry_n_bits via "a >= b: b <= a"()
|
||||
}
|
||||
// Once again, redundant but explicit assertions.
|
||||
assert n_bits >= table_entry_n_bits
|
||||
// dist_minus_1 = base_number_minus_1 + extra_bits.
|
||||
// distance = dist_minus_1 + 1.
|
||||
//
|
||||
// The -1 is from the bias in script/print-deflate-magic-numbers.go.
|
||||
// That bias makes the "& 0x7FFF" 2 and 15-ish lines below correct and
|
||||
// undoing that bias makes proving (dist_minus_1 + 1) > 0 trivial.
|
||||
dist_minus_1 = (table_entry >> 8) & 0x7FFF
|
||||
table_entry_n_bits = (table_entry >> 4) & 0x0F
|
||||
// Ensure that we have at least table_entry_n_bits bits of input.
|
||||
if n_bits < table_entry_n_bits {
|
||||
assert n_bits < 15 via "a < b: a < c; c <= b"(c: table_entry_n_bits)
|
||||
assert n_bits >= 0 // TODO: this shouldn't be necessary.
|
||||
bits |= args.src.peek_u8_as_u32() << n_bits
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
n_bits += 8
|
||||
bits |= args.src.peek_u8_as_u32() << n_bits
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
n_bits += 8
|
||||
assert table_entry_n_bits <= n_bits via "a <= b: a <= c; c <= b"(c: 16)
|
||||
assert n_bits >= table_entry_n_bits via "a >= b: b <= a"()
|
||||
}
|
||||
// Once again, redundant but explicit assertions.
|
||||
assert n_bits >= table_entry_n_bits
|
||||
|
||||
dist_minus_1 = (dist_minus_1 + bits.low_bits(n: table_entry_n_bits)) & 0x7FFF
|
||||
bits >>= table_entry_n_bits
|
||||
n_bits -= table_entry_n_bits
|
||||
dist_minus_1 = (dist_minus_1 + bits.low_bits(n: table_entry_n_bits)) & 0x7FFF
|
||||
bits >>= table_entry_n_bits
|
||||
n_bits -= table_entry_n_bits
|
||||
|
||||
// The "while true { etc; break }" is a redundant version of "etc", but
|
||||
// its presence minimizes the diff between decode_huffman_fastxx and
|
||||
// decode_huffman_slow.
|
||||
while true,
|
||||
pre args.dst.length() >= 266,
|
||||
{
|
||||
// We can therefore prove:
|
||||
assert (length as base.u64) <= args.dst.length() via "a <= b: a <= c; c <= b"(c: 266)
|
||||
assert ((length + 8) as base.u64) <= args.dst.length() via "a <= b: a <= c; c <= b"(c: 266)
|
||||
// The "while true { etc; break }" is a redundant version of "etc", but
|
||||
// its presence minimizes the diff between decode_huffman_fastxx and
|
||||
// decode_huffman_slow.
|
||||
while true,
|
||||
pre args.dst.length() >= 266,
|
||||
{
|
||||
// We can therefore prove:
|
||||
assert (length as base.u64) <= args.dst.length() via "a <= b: a <= c; c <= b"(c: 266)
|
||||
assert ((length + 8) as base.u64) <= args.dst.length() via "a <= b: a <= c; c <= b"(c: 266)
|
||||
|
||||
// Copy from this.history.
|
||||
if ((dist_minus_1 + 1) as base.u64) > args.dst.history_length() {
|
||||
// Set (hlen, hdist) to be the length-distance pair to copy
|
||||
// from this.history, and (length, distance) to be the
|
||||
// remaining length-distance pair to copy from args.dst.
|
||||
hlen = 0
|
||||
hdist = (((dist_minus_1 + 1) as base.u64) - args.dst.history_length()) as base.u32
|
||||
if length > hdist {
|
||||
assert hdist < length via "a < b: b > a"()
|
||||
assert hdist < 0x8000 via "a < b: a < c; c <= b"(c: length)
|
||||
length -= hdist
|
||||
hlen = hdist
|
||||
} else {
|
||||
hlen = length
|
||||
length = 0
|
||||
}
|
||||
hdist ~mod+= hdist_adjustment
|
||||
if this.history_index < hdist {
|
||||
return "#bad distance"
|
||||
}
|
||||
// Copy from this.history.
|
||||
if ((dist_minus_1 + 1) as base.u64) > args.dst.history_length() {
|
||||
// Set (hlen, hdist) to be the length-distance pair to copy
|
||||
// from this.history, and (length, distance) to be the
|
||||
// remaining length-distance pair to copy from args.dst.
|
||||
hlen = 0
|
||||
hdist = (((dist_minus_1 + 1) as base.u64) - args.dst.history_length()) as base.u32
|
||||
if length > hdist {
|
||||
assert hdist < length via "a < b: b > a"()
|
||||
assert hdist < 0x8000 via "a < b: a < c; c <= b"(c: length)
|
||||
length -= hdist
|
||||
hlen = hdist
|
||||
} else {
|
||||
hlen = length
|
||||
length = 0
|
||||
}
|
||||
hdist ~mod+= hdist_adjustment
|
||||
if this.history_index < hdist {
|
||||
return "#bad distance"
|
||||
}
|
||||
|
||||
// Copy from this.history[(this.history_index - hdist) ..].
|
||||
//
|
||||
// This copying is simpler than the decode_huffman_slow version
|
||||
// because it cannot yield. We have already checked that
|
||||
// args.dst.length() is large enough.
|
||||
args.dst.limited_copy_u32_from_slice!(
|
||||
up_to: hlen, s: this.history[(this.history_index - hdist) & 0x7FFF ..])
|
||||
if length == 0 {
|
||||
// No need to copy from args.dst.
|
||||
continue.loop
|
||||
}
|
||||
// Copy from this.history[(this.history_index - hdist) ..].
|
||||
//
|
||||
// This copying is simpler than the decode_huffman_slow version
|
||||
// because it cannot yield. We have already checked that
|
||||
// args.dst.length() is large enough.
|
||||
args.dst.limited_copy_u32_from_slice!(
|
||||
up_to: hlen, s: this.history[(this.history_index - hdist) & 0x7FFF ..])
|
||||
if length == 0 {
|
||||
// No need to copy from args.dst.
|
||||
continue.loop
|
||||
}
|
||||
|
||||
if (((dist_minus_1 + 1) as base.u64) > args.dst.history_length()) or
|
||||
((length as base.u64) > args.dst.length()) or
|
||||
(((length + 8) as base.u64) > args.dst.length()) {
|
||||
return "#internal error: inconsistent distance"
|
||||
}
|
||||
}
|
||||
// Once again, redundant but explicit assertions.
|
||||
assert (dist_minus_1 + 1) >= 1
|
||||
assert ((dist_minus_1 + 1) as base.u64) <= args.dst.history_length()
|
||||
assert (length as base.u64) <= args.dst.length()
|
||||
assert ((length + 8) as base.u64) <= args.dst.length()
|
||||
if (((dist_minus_1 + 1) as base.u64) > args.dst.history_length()) or
|
||||
((length as base.u64) > args.dst.length()) or
|
||||
(((length + 8) as base.u64) > args.dst.length()) {
|
||||
return "#internal error: inconsistent distance"
|
||||
}
|
||||
}
|
||||
// Once again, redundant but explicit assertions.
|
||||
assert (dist_minus_1 + 1) >= 1
|
||||
assert ((dist_minus_1 + 1) as base.u64) <= args.dst.history_length()
|
||||
assert (length as base.u64) <= args.dst.length()
|
||||
assert ((length + 8) as base.u64) <= args.dst.length()
|
||||
|
||||
// Copy from args.dst.
|
||||
//
|
||||
// For short distances, less than 8 bytes, copying atomic 8-byte
|
||||
// chunks can result in incorrect output, so we fall back to a
|
||||
// slower 1-byte-at-a-time copy. Deflate's copy-from-history can
|
||||
// pick up freshly written bytes. A length = 5, distance = 2 copy
|
||||
// starting with "abc" should give "abcbcbcb" but the 8-byte chunk
|
||||
// technique would give "abcbc???", the exact output depending on
|
||||
// what was previously in the writer buffer.
|
||||
if (dist_minus_1 + 1) >= 8 {
|
||||
args.dst.limited_copy_u32_from_history_8_byte_chunks_fast!(
|
||||
up_to: length, distance: (dist_minus_1 + 1))
|
||||
} else {
|
||||
args.dst.limited_copy_u32_from_history_fast!(
|
||||
up_to: length, distance: (dist_minus_1 + 1))
|
||||
}
|
||||
break
|
||||
} endwhile
|
||||
} endwhile.loop
|
||||
// Copy from args.dst.
|
||||
//
|
||||
// For short distances, less than 8 bytes, copying atomic 8-byte
|
||||
// chunks can result in incorrect output, so we fall back to a
|
||||
// slower 1-byte-at-a-time copy. Deflate's copy-from-history can
|
||||
// pick up freshly written bytes. A length = 5, distance = 2 copy
|
||||
// starting with "abc" should give "abcbcbcb" but the 8-byte chunk
|
||||
// technique would give "abcbc???", the exact output depending on
|
||||
// what was previously in the writer buffer.
|
||||
if (dist_minus_1 + 1) >= 8 {
|
||||
args.dst.limited_copy_u32_from_history_8_byte_chunks_fast!(
|
||||
up_to: length, distance: (dist_minus_1 + 1))
|
||||
} else {
|
||||
args.dst.limited_copy_u32_from_history_fast!(
|
||||
up_to: length, distance: (dist_minus_1 + 1))
|
||||
}
|
||||
break
|
||||
} endwhile
|
||||
} endwhile.loop
|
||||
|
||||
// Ensure n_bits < 8 by rewindng args.src, if we loaded too many of its
|
||||
// bytes into the bits variable.
|
||||
//
|
||||
// Note that we can unconditionally call undo_read (without resulting in an
|
||||
// "invalid I/O operation" error code) only because this whole function can
|
||||
// never suspend, as all of its I/O operations were checked beforehand for
|
||||
// sufficient buffer space. Otherwise, resuming from the suspension could
|
||||
// mean that the (possibly different) args.src is no longer rewindable,
|
||||
// even if conceptually, this function was responsible for reading the
|
||||
// bytes we want to rewind.
|
||||
while n_bits >= 8,
|
||||
post n_bits < 8,
|
||||
{
|
||||
n_bits -= 8
|
||||
if args.src.can_undo_byte() {
|
||||
args.src.undo_byte!()
|
||||
} else {
|
||||
return "#internal error: inconsistent I/O"
|
||||
}
|
||||
} endwhile
|
||||
// Ensure n_bits < 8 by rewindng args.src, if we loaded too many of its
|
||||
// bytes into the bits variable.
|
||||
//
|
||||
// Note that we can unconditionally call undo_read (without resulting in an
|
||||
// "invalid I/O operation" error code) only because this whole function can
|
||||
// never suspend, as all of its I/O operations were checked beforehand for
|
||||
// sufficient buffer space. Otherwise, resuming from the suspension could
|
||||
// mean that the (possibly different) args.src is no longer rewindable,
|
||||
// even if conceptually, this function was responsible for reading the
|
||||
// bytes we want to rewind.
|
||||
while n_bits >= 8,
|
||||
post n_bits < 8,
|
||||
{
|
||||
n_bits -= 8
|
||||
if args.src.can_undo_byte() {
|
||||
args.src.undo_byte!()
|
||||
} else {
|
||||
return "#internal error: inconsistent I/O"
|
||||
}
|
||||
} endwhile
|
||||
|
||||
this.bits = bits & (((1 as base.u32) << n_bits) - 1)
|
||||
this.n_bits = n_bits
|
||||
this.bits = bits & (((1 as base.u32) << n_bits) - 1)
|
||||
this.n_bits = n_bits
|
||||
|
||||
if (this.n_bits >= 8) or ((this.bits >> this.n_bits) <> 0) {
|
||||
return "#internal error: inconsistent n_bits"
|
||||
}
|
||||
if (this.n_bits >= 8) or ((this.bits >> this.n_bits) <> 0) {
|
||||
return "#internal error: inconsistent n_bits"
|
||||
}
|
||||
}
|
||||
@@ -15,287 +15,287 @@
|
||||
// TODO: describe how the xxx_fastxx version differs from the xxx_slow one, the
|
||||
// assumptions that xxx_fastxx makes, and how that makes it fast.
|
||||
pri func decoder.decode_huffman_fast64!(dst: base.io_writer, src: base.io_reader) base.status,
|
||||
choosy,
|
||||
choosy,
|
||||
{
|
||||
// When editing this function, consider making the equivalent change to the
|
||||
// decode_huffman_slow function. Keep the diff between the two
|
||||
// decode_huffman_*.wuffs files as small as possible, while retaining both
|
||||
// correctness and performance.
|
||||
// When editing this function, consider making the equivalent change to the
|
||||
// decode_huffman_slow function. Keep the diff between the two
|
||||
// decode_huffman_*.wuffs files as small as possible, while retaining both
|
||||
// correctness and performance.
|
||||
|
||||
var bits : base.u64
|
||||
var n_bits : base.u32
|
||||
var table_entry : base.u32
|
||||
var table_entry_n_bits : base.u32[..= 15]
|
||||
var lmask : base.u64[..= 511]
|
||||
var dmask : base.u64[..= 511]
|
||||
var redir_top : base.u32[..= 0xFFFF]
|
||||
var redir_mask : base.u32[..= 0x7FFF]
|
||||
var length : base.u32[..= 258]
|
||||
var dist_minus_1 : base.u32[..= 0x7FFF]
|
||||
var hlen : base.u32[..= 0x7FFF]
|
||||
var hdist : base.u32
|
||||
var hdist_adjustment : base.u32
|
||||
var bits : base.u64
|
||||
var n_bits : base.u32
|
||||
var table_entry : base.u32
|
||||
var table_entry_n_bits : base.u32[..= 15]
|
||||
var lmask : base.u64[..= 511]
|
||||
var dmask : base.u64[..= 511]
|
||||
var redir_top : base.u32[..= 0xFFFF]
|
||||
var redir_mask : base.u32[..= 0x7FFF]
|
||||
var length : base.u32[..= 258]
|
||||
var dist_minus_1 : base.u32[..= 0x7FFF]
|
||||
var hlen : base.u32[..= 0x7FFF]
|
||||
var hdist : base.u32
|
||||
var hdist_adjustment : base.u32
|
||||
|
||||
if (this.n_bits >= 8) or ((this.bits >> (this.n_bits & 7)) <> 0) {
|
||||
return "#internal error: inconsistent n_bits"
|
||||
}
|
||||
if (this.n_bits >= 8) or ((this.bits >> (this.n_bits & 7)) <> 0) {
|
||||
return "#internal error: inconsistent n_bits"
|
||||
}
|
||||
|
||||
bits = this.bits as base.u64
|
||||
n_bits = this.n_bits
|
||||
bits = this.bits as base.u64
|
||||
n_bits = this.n_bits
|
||||
|
||||
lmask = ((1 as base.u64) << this.n_huffs_bits[0]) - 1
|
||||
dmask = ((1 as base.u64) << this.n_huffs_bits[1]) - 1
|
||||
lmask = ((1 as base.u64) << this.n_huffs_bits[0]) - 1
|
||||
dmask = ((1 as base.u64) << this.n_huffs_bits[1]) - 1
|
||||
|
||||
if this.transformed_history_count < args.dst.history_position() {
|
||||
return base."#bad I/O position"
|
||||
}
|
||||
hdist_adjustment = ((this.transformed_history_count - args.dst.history_position()) & 0xFFFF_FFFF) as base.u32
|
||||
if this.transformed_history_count < args.dst.history_position() {
|
||||
return base."#bad I/O position"
|
||||
}
|
||||
hdist_adjustment = ((this.transformed_history_count - args.dst.history_position()) & 0xFFFF_FFFF) as base.u32
|
||||
|
||||
// Check up front, on each iteration, that we have enough buffer space to
|
||||
// both read (8 bytes) and write (266 bytes) as much as we need to. Doing
|
||||
// this check once (per iteration), up front, removes the need to check
|
||||
// multiple times inside the loop body, so it's faster overall.
|
||||
//
|
||||
// For writing, a literal code obviously corresponds to writing 1 byte, and
|
||||
// 258 is the maximum length in a length-distance pair, as specified in the
|
||||
// RFC section 3.2.5. Compressed blocks (length and distance codes).
|
||||
//
|
||||
// We adjust 258 up to (258 + 8) = 266 since it can be faster to overshoot
|
||||
// a little and make multiple-of-8-byte copies even when the length in the
|
||||
// length-distance pair isn't an exact multiple-of-8. Strictly speaking,
|
||||
// 264 (an exact multiple-of-8) is the tight bound but (258 + 8) is easier
|
||||
// for the Wuffs proof system, as length's type is refined to [..= 258],
|
||||
//
|
||||
// For reading, strictly speaking, we only need 6 bytes (48 bits) of
|
||||
// available input, because the H-L Literal/Length code is up to 15 bits
|
||||
// plus up to 5 extra bits, the H-D Distance code is up to 15 bits plus up
|
||||
// to 13 extra bits and 15 + 5 + 15 + 13 == 48. However, it's faster to
|
||||
// read 64 bits than 48 bits or (48 - n_bits) bits.
|
||||
while.loop(args.dst.length() >= 266) and (args.src.length() >= 8) {
|
||||
// Ensure that we have at least 56 bits of input.
|
||||
//
|
||||
// This is "Variant 4" of
|
||||
// https://fgiesen.wordpress.com/2018/02/20/reading-bits-in-far-too-many-ways-part-2/
|
||||
//
|
||||
// 56, the number of bits in 7 bytes, is a property of that "Variant 4"
|
||||
// bit-reading technique, and not related to the DEFLATE format per se.
|
||||
//
|
||||
// The "& 63" is a no-op, and not part of the original "Variant 4"
|
||||
// technique, but satisfies Wuffs' overflow/underflow checks.
|
||||
bits |= args.src.peek_u64le() ~mod<< (n_bits & 63)
|
||||
args.src.skip_u32_fast!(actual: (63 - (n_bits & 63)) >> 3, worst_case: 8)
|
||||
n_bits |= 56
|
||||
// Check up front, on each iteration, that we have enough buffer space to
|
||||
// both read (8 bytes) and write (266 bytes) as much as we need to. Doing
|
||||
// this check once (per iteration), up front, removes the need to check
|
||||
// multiple times inside the loop body, so it's faster overall.
|
||||
//
|
||||
// For writing, a literal code obviously corresponds to writing 1 byte, and
|
||||
// 258 is the maximum length in a length-distance pair, as specified in the
|
||||
// RFC section 3.2.5. Compressed blocks (length and distance codes).
|
||||
//
|
||||
// We adjust 258 up to (258 + 8) = 266 since it can be faster to overshoot
|
||||
// a little and make multiple-of-8-byte copies even when the length in the
|
||||
// length-distance pair isn't an exact multiple-of-8. Strictly speaking,
|
||||
// 264 (an exact multiple-of-8) is the tight bound but (258 + 8) is easier
|
||||
// for the Wuffs proof system, as length's type is refined to [..= 258],
|
||||
//
|
||||
// For reading, strictly speaking, we only need 6 bytes (48 bits) of
|
||||
// available input, because the H-L Literal/Length code is up to 15 bits
|
||||
// plus up to 5 extra bits, the H-D Distance code is up to 15 bits plus up
|
||||
// to 13 extra bits and 15 + 5 + 15 + 13 == 48. However, it's faster to
|
||||
// read 64 bits than 48 bits or (48 - n_bits) bits.
|
||||
while.loop(args.dst.length() >= 266) and (args.src.length() >= 8) {
|
||||
// Ensure that we have at least 56 bits of input.
|
||||
//
|
||||
// This is "Variant 4" of
|
||||
// https://fgiesen.wordpress.com/2018/02/20/reading-bits-in-far-too-many-ways-part-2/
|
||||
//
|
||||
// 56, the number of bits in 7 bytes, is a property of that "Variant 4"
|
||||
// bit-reading technique, and not related to the DEFLATE format per se.
|
||||
//
|
||||
// The "& 63" is a no-op, and not part of the original "Variant 4"
|
||||
// technique, but satisfies Wuffs' overflow/underflow checks.
|
||||
bits |= args.src.peek_u64le() ~mod<< (n_bits & 63)
|
||||
args.src.skip_u32_fast!(actual: (63 - (n_bits & 63)) >> 3, worst_case: 8)
|
||||
n_bits |= 56
|
||||
|
||||
// Decode an lcode symbol from H-L.
|
||||
table_entry = this.huffs[0][bits & lmask]
|
||||
table_entry_n_bits = table_entry & 0x0F
|
||||
bits >>= table_entry_n_bits
|
||||
n_bits ~mod-= table_entry_n_bits
|
||||
// Decode an lcode symbol from H-L.
|
||||
table_entry = this.huffs[0][bits & lmask]
|
||||
table_entry_n_bits = table_entry & 0x0F
|
||||
bits >>= table_entry_n_bits
|
||||
n_bits ~mod-= table_entry_n_bits
|
||||
|
||||
if (table_entry >> 31) <> 0 {
|
||||
// Literal.
|
||||
args.dst.write_u8_fast!(a: ((table_entry >> 8) & 0xFF) as base.u8)
|
||||
continue.loop
|
||||
} else if (table_entry >> 30) <> 0 {
|
||||
// No-op; code continues past the if-else chain.
|
||||
} else if (table_entry >> 29) <> 0 {
|
||||
// End of block.
|
||||
this.end_of_block = true
|
||||
break.loop
|
||||
} else if (table_entry >> 28) <> 0 {
|
||||
// Redirect.
|
||||
redir_top = (table_entry >> 8) & 0xFFFF
|
||||
redir_mask = ((1 as base.u32) << ((table_entry >> 4) & 0x0F)) - 1
|
||||
table_entry = this.huffs[0][
|
||||
(redir_top + (((bits & 0xFFFF_FFFF) as base.u32) & redir_mask)) & HUFFS_TABLE_MASK]
|
||||
table_entry_n_bits = table_entry & 0x0F
|
||||
bits >>= table_entry_n_bits
|
||||
n_bits ~mod-= table_entry_n_bits
|
||||
if (table_entry >> 31) <> 0 {
|
||||
// Literal.
|
||||
args.dst.write_u8_fast!(a: ((table_entry >> 8) & 0xFF) as base.u8)
|
||||
continue.loop
|
||||
} else if (table_entry >> 30) <> 0 {
|
||||
// No-op; code continues past the if-else chain.
|
||||
} else if (table_entry >> 29) <> 0 {
|
||||
// End of block.
|
||||
this.end_of_block = true
|
||||
break.loop
|
||||
} else if (table_entry >> 28) <> 0 {
|
||||
// Redirect.
|
||||
redir_top = (table_entry >> 8) & 0xFFFF
|
||||
redir_mask = ((1 as base.u32) << ((table_entry >> 4) & 0x0F)) - 1
|
||||
table_entry = this.huffs[0][
|
||||
(redir_top + (((bits & 0xFFFF_FFFF) as base.u32) & redir_mask)) & HUFFS_TABLE_MASK]
|
||||
table_entry_n_bits = table_entry & 0x0F
|
||||
bits >>= table_entry_n_bits
|
||||
n_bits ~mod-= table_entry_n_bits
|
||||
|
||||
if (table_entry >> 31) <> 0 {
|
||||
// Literal.
|
||||
args.dst.write_u8_fast!(a: ((table_entry >> 8) & 0xFF) as base.u8)
|
||||
continue.loop
|
||||
} else if (table_entry >> 30) <> 0 {
|
||||
// No-op; code continues past the if-else chain.
|
||||
} else if (table_entry >> 29) <> 0 {
|
||||
// End of block.
|
||||
this.end_of_block = true
|
||||
break.loop
|
||||
} else if (table_entry >> 28) <> 0 {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
} else if (table_entry >> 27) <> 0 {
|
||||
return "#bad Huffman code"
|
||||
} else {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
if (table_entry >> 31) <> 0 {
|
||||
// Literal.
|
||||
args.dst.write_u8_fast!(a: ((table_entry >> 8) & 0xFF) as base.u8)
|
||||
continue.loop
|
||||
} else if (table_entry >> 30) <> 0 {
|
||||
// No-op; code continues past the if-else chain.
|
||||
} else if (table_entry >> 29) <> 0 {
|
||||
// End of block.
|
||||
this.end_of_block = true
|
||||
break.loop
|
||||
} else if (table_entry >> 28) <> 0 {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
} else if (table_entry >> 27) <> 0 {
|
||||
return "#bad Huffman code"
|
||||
} else {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
|
||||
} else if (table_entry >> 27) <> 0 {
|
||||
return "#bad Huffman code"
|
||||
} else {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
} else if (table_entry >> 27) <> 0 {
|
||||
return "#bad Huffman code"
|
||||
} else {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
|
||||
// length = base_number_minus_3 + 3 + extra_bits.
|
||||
//
|
||||
// The -3 is from the bias in script/print-deflate-magic-numbers.go.
|
||||
// That bias makes the "& 0xFF" 1 and 15-ish lines below correct.
|
||||
length = ((table_entry >> 8) & 0xFF) + 3
|
||||
table_entry_n_bits = (table_entry >> 4) & 0x0F
|
||||
if table_entry_n_bits > 0 {
|
||||
// The "+ 253" is the same as "- 3", after the "& 0xFF", but the
|
||||
// plus form won't require an underflow check.
|
||||
length = ((length + 253 + (bits.low_bits(n: table_entry_n_bits) as base.u32)) & 0xFF) + 3
|
||||
bits >>= table_entry_n_bits
|
||||
n_bits ~mod-= table_entry_n_bits
|
||||
}
|
||||
// length = base_number_minus_3 + 3 + extra_bits.
|
||||
//
|
||||
// The -3 is from the bias in script/print-deflate-magic-numbers.go.
|
||||
// That bias makes the "& 0xFF" 1 and 15-ish lines below correct.
|
||||
length = ((table_entry >> 8) & 0xFF) + 3
|
||||
table_entry_n_bits = (table_entry >> 4) & 0x0F
|
||||
if table_entry_n_bits > 0 {
|
||||
// The "+ 253" is the same as "- 3", after the "& 0xFF", but the
|
||||
// plus form won't require an underflow check.
|
||||
length = ((length + 253 + (bits.low_bits(n: table_entry_n_bits) as base.u32)) & 0xFF) + 3
|
||||
bits >>= table_entry_n_bits
|
||||
n_bits ~mod-= table_entry_n_bits
|
||||
}
|
||||
|
||||
// Decode a dcode symbol from H-D.
|
||||
table_entry = this.huffs[1][bits & dmask]
|
||||
table_entry_n_bits = table_entry & 15
|
||||
bits >>= table_entry_n_bits
|
||||
n_bits ~mod-= table_entry_n_bits
|
||||
// Decode a dcode symbol from H-D.
|
||||
table_entry = this.huffs[1][bits & dmask]
|
||||
table_entry_n_bits = table_entry & 15
|
||||
bits >>= table_entry_n_bits
|
||||
n_bits ~mod-= table_entry_n_bits
|
||||
|
||||
// Check for a redirect.
|
||||
if (table_entry >> 28) == 1 {
|
||||
redir_top = (table_entry >> 8) & 0xFFFF
|
||||
redir_mask = ((1 as base.u32) << ((table_entry >> 4) & 0x0F)) - 1
|
||||
table_entry = this.huffs[1][
|
||||
(redir_top + (((bits & 0xFFFF_FFFF) as base.u32) & redir_mask)) & HUFFS_TABLE_MASK]
|
||||
table_entry_n_bits = table_entry & 0x0F
|
||||
bits >>= table_entry_n_bits
|
||||
n_bits ~mod-= table_entry_n_bits
|
||||
}
|
||||
// Check for a redirect.
|
||||
if (table_entry >> 28) == 1 {
|
||||
redir_top = (table_entry >> 8) & 0xFFFF
|
||||
redir_mask = ((1 as base.u32) << ((table_entry >> 4) & 0x0F)) - 1
|
||||
table_entry = this.huffs[1][
|
||||
(redir_top + (((bits & 0xFFFF_FFFF) as base.u32) & redir_mask)) & HUFFS_TABLE_MASK]
|
||||
table_entry_n_bits = table_entry & 0x0F
|
||||
bits >>= table_entry_n_bits
|
||||
n_bits ~mod-= table_entry_n_bits
|
||||
}
|
||||
|
||||
// For H-D, all symbols should be base_number + extra_bits.
|
||||
if (table_entry >> 24) <> 0x40 {
|
||||
if (table_entry >> 24) == 0x08 {
|
||||
return "#bad Huffman code"
|
||||
}
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
// For H-D, all symbols should be base_number + extra_bits.
|
||||
if (table_entry >> 24) <> 0x40 {
|
||||
if (table_entry >> 24) == 0x08 {
|
||||
return "#bad Huffman code"
|
||||
}
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
|
||||
// dist_minus_1 = base_number_minus_1 + extra_bits.
|
||||
// distance = dist_minus_1 + 1.
|
||||
//
|
||||
// The -1 is from the bias in script/print-deflate-magic-numbers.go.
|
||||
// That bias makes the "& 0x7FFF" 2 and 15-ish lines below correct and
|
||||
// undoing that bias makes proving (dist_minus_1 + 1) > 0 trivial.
|
||||
dist_minus_1 = (table_entry >> 8) & 0x7FFF
|
||||
table_entry_n_bits = (table_entry >> 4) & 0x0F
|
||||
// dist_minus_1 = base_number_minus_1 + extra_bits.
|
||||
// distance = dist_minus_1 + 1.
|
||||
//
|
||||
// The -1 is from the bias in script/print-deflate-magic-numbers.go.
|
||||
// That bias makes the "& 0x7FFF" 2 and 15-ish lines below correct and
|
||||
// undoing that bias makes proving (dist_minus_1 + 1) > 0 trivial.
|
||||
dist_minus_1 = (table_entry >> 8) & 0x7FFF
|
||||
table_entry_n_bits = (table_entry >> 4) & 0x0F
|
||||
|
||||
dist_minus_1 = (dist_minus_1 + (bits.low_bits(n: table_entry_n_bits) as base.u32)) & 0x7FFF
|
||||
bits >>= table_entry_n_bits
|
||||
n_bits ~mod-= table_entry_n_bits
|
||||
dist_minus_1 = (dist_minus_1 + (bits.low_bits(n: table_entry_n_bits) as base.u32)) & 0x7FFF
|
||||
bits >>= table_entry_n_bits
|
||||
n_bits ~mod-= table_entry_n_bits
|
||||
|
||||
// The "while true { etc; break }" is a redundant version of "etc", but
|
||||
// its presence minimizes the diff between decode_huffman_fastxx and
|
||||
// decode_huffman_slow.
|
||||
while true,
|
||||
pre args.dst.length() >= 266,
|
||||
{
|
||||
// We can therefore prove:
|
||||
assert (length as base.u64) <= args.dst.length() via "a <= b: a <= c; c <= b"(c: 266)
|
||||
assert ((length + 8) as base.u64) <= args.dst.length() via "a <= b: a <= c; c <= b"(c: 266)
|
||||
// The "while true { etc; break }" is a redundant version of "etc", but
|
||||
// its presence minimizes the diff between decode_huffman_fastxx and
|
||||
// decode_huffman_slow.
|
||||
while true,
|
||||
pre args.dst.length() >= 266,
|
||||
{
|
||||
// We can therefore prove:
|
||||
assert (length as base.u64) <= args.dst.length() via "a <= b: a <= c; c <= b"(c: 266)
|
||||
assert ((length + 8) as base.u64) <= args.dst.length() via "a <= b: a <= c; c <= b"(c: 266)
|
||||
|
||||
// Copy from this.history.
|
||||
if ((dist_minus_1 + 1) as base.u64) > args.dst.history_length() {
|
||||
// Set (hlen, hdist) to be the length-distance pair to copy
|
||||
// from this.history, and (length, distance) to be the
|
||||
// remaining length-distance pair to copy from args.dst.
|
||||
hlen = 0
|
||||
hdist = (((dist_minus_1 + 1) as base.u64) - args.dst.history_length()) as base.u32
|
||||
if length > hdist {
|
||||
assert hdist < length via "a < b: b > a"()
|
||||
assert hdist < 0x8000 via "a < b: a < c; c <= b"(c: length)
|
||||
length -= hdist
|
||||
hlen = hdist
|
||||
} else {
|
||||
hlen = length
|
||||
length = 0
|
||||
}
|
||||
hdist ~mod+= hdist_adjustment
|
||||
if this.history_index < hdist {
|
||||
return "#bad distance"
|
||||
}
|
||||
// Copy from this.history.
|
||||
if ((dist_minus_1 + 1) as base.u64) > args.dst.history_length() {
|
||||
// Set (hlen, hdist) to be the length-distance pair to copy
|
||||
// from this.history, and (length, distance) to be the
|
||||
// remaining length-distance pair to copy from args.dst.
|
||||
hlen = 0
|
||||
hdist = (((dist_minus_1 + 1) as base.u64) - args.dst.history_length()) as base.u32
|
||||
if length > hdist {
|
||||
assert hdist < length via "a < b: b > a"()
|
||||
assert hdist < 0x8000 via "a < b: a < c; c <= b"(c: length)
|
||||
length -= hdist
|
||||
hlen = hdist
|
||||
} else {
|
||||
hlen = length
|
||||
length = 0
|
||||
}
|
||||
hdist ~mod+= hdist_adjustment
|
||||
if this.history_index < hdist {
|
||||
return "#bad distance"
|
||||
}
|
||||
|
||||
// Copy from this.history[(this.history_index - hdist) ..].
|
||||
//
|
||||
// This copying is simpler than the decode_huffman_slow version
|
||||
// because it cannot yield. We have already checked that
|
||||
// args.dst.length() is large enough.
|
||||
args.dst.limited_copy_u32_from_slice!(
|
||||
up_to: hlen, s: this.history[(this.history_index - hdist) & 0x7FFF ..])
|
||||
if length == 0 {
|
||||
// No need to copy from args.dst.
|
||||
continue.loop
|
||||
}
|
||||
// Copy from this.history[(this.history_index - hdist) ..].
|
||||
//
|
||||
// This copying is simpler than the decode_huffman_slow version
|
||||
// because it cannot yield. We have already checked that
|
||||
// args.dst.length() is large enough.
|
||||
args.dst.limited_copy_u32_from_slice!(
|
||||
up_to: hlen, s: this.history[(this.history_index - hdist) & 0x7FFF ..])
|
||||
if length == 0 {
|
||||
// No need to copy from args.dst.
|
||||
continue.loop
|
||||
}
|
||||
|
||||
if (((dist_minus_1 + 1) as base.u64) > args.dst.history_length()) or
|
||||
((length as base.u64) > args.dst.length()) or
|
||||
(((length + 8) as base.u64) > args.dst.length()) {
|
||||
return "#internal error: inconsistent distance"
|
||||
}
|
||||
}
|
||||
// Once again, redundant but explicit assertions.
|
||||
assert (dist_minus_1 + 1) >= 1
|
||||
assert ((dist_minus_1 + 1) as base.u64) <= args.dst.history_length()
|
||||
assert (length as base.u64) <= args.dst.length()
|
||||
assert ((length + 8) as base.u64) <= args.dst.length()
|
||||
if (((dist_minus_1 + 1) as base.u64) > args.dst.history_length()) or
|
||||
((length as base.u64) > args.dst.length()) or
|
||||
(((length + 8) as base.u64) > args.dst.length()) {
|
||||
return "#internal error: inconsistent distance"
|
||||
}
|
||||
}
|
||||
// Once again, redundant but explicit assertions.
|
||||
assert (dist_minus_1 + 1) >= 1
|
||||
assert ((dist_minus_1 + 1) as base.u64) <= args.dst.history_length()
|
||||
assert (length as base.u64) <= args.dst.length()
|
||||
assert ((length + 8) as base.u64) <= args.dst.length()
|
||||
|
||||
// Copy from args.dst.
|
||||
//
|
||||
// For short distances, less than 8 bytes, copying atomic 8-byte
|
||||
// chunks can result in incorrect output, so we fall back to a
|
||||
// slower 1-byte-at-a-time copy. Deflate's copy-from-history can
|
||||
// pick up freshly written bytes. A length = 5, distance = 2 copy
|
||||
// starting with "abc" should give "abcbcbcb" but the 8-byte chunk
|
||||
// technique would give "abcbc???", the exact output depending on
|
||||
// what was previously in the writer buffer.
|
||||
if (dist_minus_1 + 1) >= 8 {
|
||||
args.dst.limited_copy_u32_from_history_8_byte_chunks_fast!(
|
||||
up_to: length, distance: (dist_minus_1 + 1))
|
||||
} else if (dist_minus_1 + 1) == 1 {
|
||||
// (distance == 1) is essentially RLE (Run Length Encoding). It
|
||||
// happens often enough that it's worth special-casing.
|
||||
args.dst.limited_copy_u32_from_history_8_byte_chunks_distance_1_fast!(
|
||||
up_to: length, distance: (dist_minus_1 + 1))
|
||||
} else {
|
||||
args.dst.limited_copy_u32_from_history_fast!(
|
||||
up_to: length, distance: (dist_minus_1 + 1))
|
||||
}
|
||||
break
|
||||
} endwhile
|
||||
} endwhile.loop
|
||||
// Copy from args.dst.
|
||||
//
|
||||
// For short distances, less than 8 bytes, copying atomic 8-byte
|
||||
// chunks can result in incorrect output, so we fall back to a
|
||||
// slower 1-byte-at-a-time copy. Deflate's copy-from-history can
|
||||
// pick up freshly written bytes. A length = 5, distance = 2 copy
|
||||
// starting with "abc" should give "abcbcbcb" but the 8-byte chunk
|
||||
// technique would give "abcbc???", the exact output depending on
|
||||
// what was previously in the writer buffer.
|
||||
if (dist_minus_1 + 1) >= 8 {
|
||||
args.dst.limited_copy_u32_from_history_8_byte_chunks_fast!(
|
||||
up_to: length, distance: (dist_minus_1 + 1))
|
||||
} else if (dist_minus_1 + 1) == 1 {
|
||||
// (distance == 1) is essentially RLE (Run Length Encoding). It
|
||||
// happens often enough that it's worth special-casing.
|
||||
args.dst.limited_copy_u32_from_history_8_byte_chunks_distance_1_fast!(
|
||||
up_to: length, distance: (dist_minus_1 + 1))
|
||||
} else {
|
||||
args.dst.limited_copy_u32_from_history_fast!(
|
||||
up_to: length, distance: (dist_minus_1 + 1))
|
||||
}
|
||||
break
|
||||
} endwhile
|
||||
} endwhile.loop
|
||||
|
||||
// Ensure n_bits < 8 by rewindng args.src, if we loaded too many of its
|
||||
// bytes into the bits variable.
|
||||
//
|
||||
// Note that we can unconditionally call undo_read (without resulting in an
|
||||
// "invalid I/O operation" error code) only because this whole function can
|
||||
// never suspend, as all of its I/O operations were checked beforehand for
|
||||
// sufficient buffer space. Otherwise, resuming from the suspension could
|
||||
// mean that the (possibly different) args.src is no longer rewindable,
|
||||
// even if conceptually, this function was responsible for reading the
|
||||
// bytes we want to rewind.
|
||||
if n_bits > 63 {
|
||||
return "#internal error: inconsistent n_bits"
|
||||
}
|
||||
while n_bits >= 8,
|
||||
post n_bits < 8,
|
||||
{
|
||||
n_bits -= 8
|
||||
if args.src.can_undo_byte() {
|
||||
args.src.undo_byte!()
|
||||
} else {
|
||||
return "#internal error: inconsistent I/O"
|
||||
}
|
||||
} endwhile
|
||||
// Ensure n_bits < 8 by rewindng args.src, if we loaded too many of its
|
||||
// bytes into the bits variable.
|
||||
//
|
||||
// Note that we can unconditionally call undo_read (without resulting in an
|
||||
// "invalid I/O operation" error code) only because this whole function can
|
||||
// never suspend, as all of its I/O operations were checked beforehand for
|
||||
// sufficient buffer space. Otherwise, resuming from the suspension could
|
||||
// mean that the (possibly different) args.src is no longer rewindable,
|
||||
// even if conceptually, this function was responsible for reading the
|
||||
// bytes we want to rewind.
|
||||
if n_bits > 63 {
|
||||
return "#internal error: inconsistent n_bits"
|
||||
}
|
||||
while n_bits >= 8,
|
||||
post n_bits < 8,
|
||||
{
|
||||
n_bits -= 8
|
||||
if args.src.can_undo_byte() {
|
||||
args.src.undo_byte!()
|
||||
} else {
|
||||
return "#internal error: inconsistent I/O"
|
||||
}
|
||||
} endwhile
|
||||
|
||||
this.bits = (bits & (((1 as base.u64) << n_bits) - 1)) as base.u32
|
||||
this.n_bits = n_bits
|
||||
this.bits = (bits & (((1 as base.u64) << n_bits) - 1)) as base.u32
|
||||
this.n_bits = n_bits
|
||||
|
||||
if (this.n_bits >= 8) or ((this.bits >> this.n_bits) <> 0) {
|
||||
return "#internal error: inconsistent n_bits"
|
||||
}
|
||||
if (this.n_bits >= 8) or ((this.bits >> this.n_bits) <> 0) {
|
||||
return "#internal error: inconsistent n_bits"
|
||||
}
|
||||
}
|
||||
@@ -13,249 +13,249 @@
|
||||
// limitations under the License.
|
||||
|
||||
pri func decoder.decode_huffman_slow?(dst: base.io_writer, src: base.io_reader) {
|
||||
var bits : base.u32
|
||||
var n_bits : base.u32
|
||||
var table_entry : base.u32
|
||||
var table_entry_n_bits : base.u32[..= 15]
|
||||
var lmask : base.u32[..= 511]
|
||||
var dmask : base.u32[..= 511]
|
||||
var b0 : base.u32[..= 255]
|
||||
var redir_top : base.u32[..= 0xFFFF]
|
||||
var redir_mask : base.u32[..= 0x7FFF]
|
||||
var b1 : base.u32[..= 255]
|
||||
var length : base.u32[..= 258]
|
||||
var b2 : base.u32[..= 255]
|
||||
var b3 : base.u32[..= 255]
|
||||
var b4 : base.u32[..= 255]
|
||||
var dist_minus_1 : base.u32[..= 0x7FFF]
|
||||
var b5 : base.u32[..= 255]
|
||||
var n_copied : base.u32
|
||||
var hlen : base.u32[..= 0x7FFF]
|
||||
var hdist : base.u32
|
||||
var bits : base.u32
|
||||
var n_bits : base.u32
|
||||
var table_entry : base.u32
|
||||
var table_entry_n_bits : base.u32[..= 15]
|
||||
var lmask : base.u32[..= 511]
|
||||
var dmask : base.u32[..= 511]
|
||||
var b0 : base.u32[..= 255]
|
||||
var redir_top : base.u32[..= 0xFFFF]
|
||||
var redir_mask : base.u32[..= 0x7FFF]
|
||||
var b1 : base.u32[..= 255]
|
||||
var length : base.u32[..= 258]
|
||||
var b2 : base.u32[..= 255]
|
||||
var b3 : base.u32[..= 255]
|
||||
var b4 : base.u32[..= 255]
|
||||
var dist_minus_1 : base.u32[..= 0x7FFF]
|
||||
var b5 : base.u32[..= 255]
|
||||
var n_copied : base.u32
|
||||
var hlen : base.u32[..= 0x7FFF]
|
||||
var hdist : base.u32
|
||||
|
||||
// When editing this function, consider making the equivalent change to the
|
||||
// decode_huffman_fastxx functions. Keep the diff between the two
|
||||
// decode_huffman_*.wuffs files as small as possible, while retaining both
|
||||
// correctness and performance.
|
||||
// When editing this function, consider making the equivalent change to the
|
||||
// decode_huffman_fastxx functions. Keep the diff between the two
|
||||
// decode_huffman_*.wuffs files as small as possible, while retaining both
|
||||
// correctness and performance.
|
||||
|
||||
if (this.n_bits >= 8) or ((this.bits >> (this.n_bits & 7)) <> 0) {
|
||||
return "#internal error: inconsistent n_bits"
|
||||
}
|
||||
if (this.n_bits >= 8) or ((this.bits >> (this.n_bits & 7)) <> 0) {
|
||||
return "#internal error: inconsistent n_bits"
|
||||
}
|
||||
|
||||
bits = this.bits
|
||||
n_bits = this.n_bits
|
||||
bits = this.bits
|
||||
n_bits = this.n_bits
|
||||
|
||||
lmask = ((1 as base.u32) << this.n_huffs_bits[0]) - 1
|
||||
dmask = ((1 as base.u32) << this.n_huffs_bits[1]) - 1
|
||||
lmask = ((1 as base.u32) << this.n_huffs_bits[0]) - 1
|
||||
dmask = ((1 as base.u32) << this.n_huffs_bits[1]) - 1
|
||||
|
||||
while.loop not coroutine_resumed {
|
||||
// Decode an lcode symbol from H-L.
|
||||
while true {
|
||||
table_entry = this.huffs[0][bits & lmask]
|
||||
table_entry_n_bits = table_entry & 0x0F
|
||||
if n_bits >= table_entry_n_bits {
|
||||
bits >>= table_entry_n_bits
|
||||
n_bits -= table_entry_n_bits
|
||||
break
|
||||
}
|
||||
assert n_bits < 15 via "a < b: a < c; c <= b"(c: table_entry_n_bits)
|
||||
b0 = args.src.read_u8_as_u32?()
|
||||
bits |= b0 << n_bits
|
||||
n_bits += 8
|
||||
} endwhile
|
||||
while.loop not coroutine_resumed {
|
||||
// Decode an lcode symbol from H-L.
|
||||
while true {
|
||||
table_entry = this.huffs[0][bits & lmask]
|
||||
table_entry_n_bits = table_entry & 0x0F
|
||||
if n_bits >= table_entry_n_bits {
|
||||
bits >>= table_entry_n_bits
|
||||
n_bits -= table_entry_n_bits
|
||||
break
|
||||
}
|
||||
assert n_bits < 15 via "a < b: a < c; c <= b"(c: table_entry_n_bits)
|
||||
b0 = args.src.read_u8_as_u32?()
|
||||
bits |= b0 << n_bits
|
||||
n_bits += 8
|
||||
} endwhile
|
||||
|
||||
if (table_entry >> 31) <> 0 {
|
||||
// Literal.
|
||||
args.dst.write_u8?(a: ((table_entry >> 8) & 0xFF) as base.u8)
|
||||
continue.loop
|
||||
} else if (table_entry >> 30) <> 0 {
|
||||
// No-op; code continues past the if-else chain.
|
||||
} else if (table_entry >> 29) <> 0 {
|
||||
// End of block.
|
||||
this.end_of_block = true
|
||||
break.loop
|
||||
} else if (table_entry >> 28) <> 0 {
|
||||
// Redirect.
|
||||
redir_top = (table_entry >> 8) & 0xFFFF
|
||||
redir_mask = ((1 as base.u32) << ((table_entry >> 4) & 0x0F)) - 1
|
||||
while true {
|
||||
table_entry = this.huffs[0][(redir_top + (bits & redir_mask)) & HUFFS_TABLE_MASK]
|
||||
table_entry_n_bits = table_entry & 0x0F
|
||||
if n_bits >= table_entry_n_bits {
|
||||
bits >>= table_entry_n_bits
|
||||
n_bits -= table_entry_n_bits
|
||||
break
|
||||
}
|
||||
assert n_bits < 15 via "a < b: a < c; c <= b"(c: table_entry_n_bits)
|
||||
b1 = args.src.read_u8_as_u32?()
|
||||
bits |= b1 << n_bits
|
||||
n_bits += 8
|
||||
} endwhile
|
||||
if (table_entry >> 31) <> 0 {
|
||||
// Literal.
|
||||
args.dst.write_u8?(a: ((table_entry >> 8) & 0xFF) as base.u8)
|
||||
continue.loop
|
||||
} else if (table_entry >> 30) <> 0 {
|
||||
// No-op; code continues past the if-else chain.
|
||||
} else if (table_entry >> 29) <> 0 {
|
||||
// End of block.
|
||||
this.end_of_block = true
|
||||
break.loop
|
||||
} else if (table_entry >> 28) <> 0 {
|
||||
// Redirect.
|
||||
redir_top = (table_entry >> 8) & 0xFFFF
|
||||
redir_mask = ((1 as base.u32) << ((table_entry >> 4) & 0x0F)) - 1
|
||||
while true {
|
||||
table_entry = this.huffs[0][(redir_top + (bits & redir_mask)) & HUFFS_TABLE_MASK]
|
||||
table_entry_n_bits = table_entry & 0x0F
|
||||
if n_bits >= table_entry_n_bits {
|
||||
bits >>= table_entry_n_bits
|
||||
n_bits -= table_entry_n_bits
|
||||
break
|
||||
}
|
||||
assert n_bits < 15 via "a < b: a < c; c <= b"(c: table_entry_n_bits)
|
||||
b1 = args.src.read_u8_as_u32?()
|
||||
bits |= b1 << n_bits
|
||||
n_bits += 8
|
||||
} endwhile
|
||||
|
||||
if (table_entry >> 31) <> 0 {
|
||||
// Literal.
|
||||
args.dst.write_u8?(a: ((table_entry >> 8) & 0xFF) as base.u8)
|
||||
continue.loop
|
||||
} else if (table_entry >> 30) <> 0 {
|
||||
// No-op; code continues past the if-else chain.
|
||||
} else if (table_entry >> 29) <> 0 {
|
||||
// End of block.
|
||||
this.end_of_block = true
|
||||
break.loop
|
||||
} else if (table_entry >> 28) <> 0 {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
} else if (table_entry >> 27) <> 0 {
|
||||
return "#bad Huffman code"
|
||||
} else {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
if (table_entry >> 31) <> 0 {
|
||||
// Literal.
|
||||
args.dst.write_u8?(a: ((table_entry >> 8) & 0xFF) as base.u8)
|
||||
continue.loop
|
||||
} else if (table_entry >> 30) <> 0 {
|
||||
// No-op; code continues past the if-else chain.
|
||||
} else if (table_entry >> 29) <> 0 {
|
||||
// End of block.
|
||||
this.end_of_block = true
|
||||
break.loop
|
||||
} else if (table_entry >> 28) <> 0 {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
} else if (table_entry >> 27) <> 0 {
|
||||
return "#bad Huffman code"
|
||||
} else {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
|
||||
} else if (table_entry >> 27) <> 0 {
|
||||
return "#bad Huffman code"
|
||||
} else {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
} else if (table_entry >> 27) <> 0 {
|
||||
return "#bad Huffman code"
|
||||
} else {
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
|
||||
// length = base_number_minus_3 + 3 + extra_bits.
|
||||
//
|
||||
// The -3 is from the bias in script/print-deflate-magic-numbers.go.
|
||||
// That bias makes the "& 0xFF" 1 and 15-ish lines below correct.
|
||||
length = ((table_entry >> 8) & 0xFF) + 3
|
||||
table_entry_n_bits = (table_entry >> 4) & 0x0F
|
||||
if table_entry_n_bits > 0 {
|
||||
while n_bits < table_entry_n_bits,
|
||||
post n_bits >= table_entry_n_bits,
|
||||
{
|
||||
assert n_bits < 15 via "a < b: a < c; c <= b"(c: table_entry_n_bits)
|
||||
b2 = args.src.read_u8_as_u32?()
|
||||
bits |= b2 << n_bits
|
||||
n_bits += 8
|
||||
} endwhile
|
||||
// The "+ 253" is the same as "- 3", after the "& 0xFF", but the
|
||||
// plus form won't require an underflow check.
|
||||
length = ((length + 253 + bits.low_bits(n: table_entry_n_bits)) & 0xFF) + 3
|
||||
bits >>= table_entry_n_bits
|
||||
n_bits -= table_entry_n_bits
|
||||
}
|
||||
// length = base_number_minus_3 + 3 + extra_bits.
|
||||
//
|
||||
// The -3 is from the bias in script/print-deflate-magic-numbers.go.
|
||||
// That bias makes the "& 0xFF" 1 and 15-ish lines below correct.
|
||||
length = ((table_entry >> 8) & 0xFF) + 3
|
||||
table_entry_n_bits = (table_entry >> 4) & 0x0F
|
||||
if table_entry_n_bits > 0 {
|
||||
while n_bits < table_entry_n_bits,
|
||||
post n_bits >= table_entry_n_bits,
|
||||
{
|
||||
assert n_bits < 15 via "a < b: a < c; c <= b"(c: table_entry_n_bits)
|
||||
b2 = args.src.read_u8_as_u32?()
|
||||
bits |= b2 << n_bits
|
||||
n_bits += 8
|
||||
} endwhile
|
||||
// The "+ 253" is the same as "- 3", after the "& 0xFF", but the
|
||||
// plus form won't require an underflow check.
|
||||
length = ((length + 253 + bits.low_bits(n: table_entry_n_bits)) & 0xFF) + 3
|
||||
bits >>= table_entry_n_bits
|
||||
n_bits -= table_entry_n_bits
|
||||
}
|
||||
|
||||
// Decode a dcode symbol from H-D.
|
||||
while true {
|
||||
table_entry = this.huffs[1][bits & dmask]
|
||||
table_entry_n_bits = table_entry & 15
|
||||
if n_bits >= table_entry_n_bits {
|
||||
bits >>= table_entry_n_bits
|
||||
n_bits -= table_entry_n_bits
|
||||
break
|
||||
}
|
||||
assert n_bits < 15 via "a < b: a < c; c <= b"(c: table_entry_n_bits)
|
||||
b3 = args.src.read_u8_as_u32?()
|
||||
bits |= b3 << n_bits
|
||||
n_bits += 8
|
||||
} endwhile
|
||||
// Check for a redirect.
|
||||
if (table_entry >> 28) == 1 {
|
||||
redir_top = (table_entry >> 8) & 0xFFFF
|
||||
redir_mask = ((1 as base.u32) << ((table_entry >> 4) & 0x0F)) - 1
|
||||
while true {
|
||||
table_entry = this.huffs[1][(redir_top + (bits & redir_mask)) & HUFFS_TABLE_MASK]
|
||||
table_entry_n_bits = table_entry & 0x0F
|
||||
if n_bits >= table_entry_n_bits {
|
||||
bits >>= table_entry_n_bits
|
||||
n_bits -= table_entry_n_bits
|
||||
break
|
||||
}
|
||||
assert n_bits < 15 via "a < b: a < c; c <= b"(c: table_entry_n_bits)
|
||||
b4 = args.src.read_u8_as_u32?()
|
||||
bits |= b4 << n_bits
|
||||
n_bits += 8
|
||||
} endwhile
|
||||
}
|
||||
// Decode a dcode symbol from H-D.
|
||||
while true {
|
||||
table_entry = this.huffs[1][bits & dmask]
|
||||
table_entry_n_bits = table_entry & 15
|
||||
if n_bits >= table_entry_n_bits {
|
||||
bits >>= table_entry_n_bits
|
||||
n_bits -= table_entry_n_bits
|
||||
break
|
||||
}
|
||||
assert n_bits < 15 via "a < b: a < c; c <= b"(c: table_entry_n_bits)
|
||||
b3 = args.src.read_u8_as_u32?()
|
||||
bits |= b3 << n_bits
|
||||
n_bits += 8
|
||||
} endwhile
|
||||
// Check for a redirect.
|
||||
if (table_entry >> 28) == 1 {
|
||||
redir_top = (table_entry >> 8) & 0xFFFF
|
||||
redir_mask = ((1 as base.u32) << ((table_entry >> 4) & 0x0F)) - 1
|
||||
while true {
|
||||
table_entry = this.huffs[1][(redir_top + (bits & redir_mask)) & HUFFS_TABLE_MASK]
|
||||
table_entry_n_bits = table_entry & 0x0F
|
||||
if n_bits >= table_entry_n_bits {
|
||||
bits >>= table_entry_n_bits
|
||||
n_bits -= table_entry_n_bits
|
||||
break
|
||||
}
|
||||
assert n_bits < 15 via "a < b: a < c; c <= b"(c: table_entry_n_bits)
|
||||
b4 = args.src.read_u8_as_u32?()
|
||||
bits |= b4 << n_bits
|
||||
n_bits += 8
|
||||
} endwhile
|
||||
}
|
||||
|
||||
// For H-D, all symbols should be base_number + extra_bits.
|
||||
if (table_entry >> 24) <> 0x40 {
|
||||
if (table_entry >> 24) == 0x08 {
|
||||
return "#bad Huffman code"
|
||||
}
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
// For H-D, all symbols should be base_number + extra_bits.
|
||||
if (table_entry >> 24) <> 0x40 {
|
||||
if (table_entry >> 24) == 0x08 {
|
||||
return "#bad Huffman code"
|
||||
}
|
||||
return "#internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
|
||||
// dist_minus_1 = base_number_minus_1 + extra_bits.
|
||||
// distance = dist_minus_1 + 1.
|
||||
//
|
||||
// The -1 is from the bias in script/print-deflate-magic-numbers.go.
|
||||
// That bias makes the "& 0x7FFF" 2 and 15-ish lines below correct and
|
||||
// undoing that bias makes proving (dist_minus_1 + 1) > 0 trivial.
|
||||
dist_minus_1 = (table_entry >> 8) & 0x7FFF
|
||||
table_entry_n_bits = (table_entry >> 4) & 0x0F
|
||||
if table_entry_n_bits > 0 {
|
||||
while n_bits < table_entry_n_bits,
|
||||
post n_bits >= table_entry_n_bits,
|
||||
{
|
||||
assert n_bits < 15 via "a < b: a < c; c <= b"(c: table_entry_n_bits)
|
||||
b5 = args.src.read_u8_as_u32?()
|
||||
bits |= b5 << n_bits
|
||||
n_bits += 8
|
||||
} endwhile
|
||||
dist_minus_1 = (dist_minus_1 + bits.low_bits(n: table_entry_n_bits)) & 0x7FFF
|
||||
bits >>= table_entry_n_bits
|
||||
n_bits -= table_entry_n_bits
|
||||
}
|
||||
// dist_minus_1 = base_number_minus_1 + extra_bits.
|
||||
// distance = dist_minus_1 + 1.
|
||||
//
|
||||
// The -1 is from the bias in script/print-deflate-magic-numbers.go.
|
||||
// That bias makes the "& 0x7FFF" 2 and 15-ish lines below correct and
|
||||
// undoing that bias makes proving (dist_minus_1 + 1) > 0 trivial.
|
||||
dist_minus_1 = (table_entry >> 8) & 0x7FFF
|
||||
table_entry_n_bits = (table_entry >> 4) & 0x0F
|
||||
if table_entry_n_bits > 0 {
|
||||
while n_bits < table_entry_n_bits,
|
||||
post n_bits >= table_entry_n_bits,
|
||||
{
|
||||
assert n_bits < 15 via "a < b: a < c; c <= b"(c: table_entry_n_bits)
|
||||
b5 = args.src.read_u8_as_u32?()
|
||||
bits |= b5 << n_bits
|
||||
n_bits += 8
|
||||
} endwhile
|
||||
dist_minus_1 = (dist_minus_1 + bits.low_bits(n: table_entry_n_bits)) & 0x7FFF
|
||||
bits >>= table_entry_n_bits
|
||||
n_bits -= table_entry_n_bits
|
||||
}
|
||||
|
||||
while.inner true {
|
||||
// Copy from this.history.
|
||||
if ((dist_minus_1 + 1) as base.u64) > args.dst.history_length() {
|
||||
// Set (hlen, hdist) to be the length-distance pair to copy
|
||||
// from this.history.
|
||||
hdist = (((dist_minus_1 + 1) as base.u64) - args.dst.history_length()) as base.u32
|
||||
if hdist < length {
|
||||
assert hdist < 0x8000 via "a < b: a < c; c <= b"(c: length)
|
||||
hlen = hdist
|
||||
assert hlen <= length via "a <= b: a == c; c <= b"(c: hdist)
|
||||
} else {
|
||||
hlen = length
|
||||
assert hlen <= length via "a <= b: a == b"()
|
||||
}
|
||||
while.inner true {
|
||||
// Copy from this.history.
|
||||
if ((dist_minus_1 + 1) as base.u64) > args.dst.history_length() {
|
||||
// Set (hlen, hdist) to be the length-distance pair to copy
|
||||
// from this.history.
|
||||
hdist = (((dist_minus_1 + 1) as base.u64) - args.dst.history_length()) as base.u32
|
||||
if hdist < length {
|
||||
assert hdist < 0x8000 via "a < b: a < c; c <= b"(c: length)
|
||||
hlen = hdist
|
||||
assert hlen <= length via "a <= b: a == c; c <= b"(c: hdist)
|
||||
} else {
|
||||
hlen = length
|
||||
assert hlen <= length via "a <= b: a == b"()
|
||||
}
|
||||
|
||||
hdist ~mod+= ((this.transformed_history_count ~mod- args.dst.history_position()) & 0xFFFF_FFFF) as base.u32
|
||||
if this.history_index < hdist {
|
||||
return "#bad distance"
|
||||
}
|
||||
hdist ~mod+= ((this.transformed_history_count ~mod- args.dst.history_position()) & 0xFFFF_FFFF) as base.u32
|
||||
if this.history_index < hdist {
|
||||
return "#bad distance"
|
||||
}
|
||||
|
||||
// Copy from this.history[(this.history_index - hdist) ..].
|
||||
n_copied = args.dst.limited_copy_u32_from_slice!(
|
||||
up_to: hlen, s: this.history[(this.history_index - hdist) & 0x7FFF ..])
|
||||
if n_copied < hlen {
|
||||
assert n_copied < length via "a < b: a < c; c <= b"(c: hlen)
|
||||
assert length > n_copied via "a > b: b < a"()
|
||||
length -= n_copied
|
||||
yield? base."$short write"
|
||||
continue.inner
|
||||
}
|
||||
assert length >= hlen via "a >= b: b <= a"()
|
||||
length -= hlen
|
||||
if length == 0 {
|
||||
// No need to copy from args.dst.
|
||||
continue.loop
|
||||
}
|
||||
}
|
||||
// Copy from this.history[(this.history_index - hdist) ..].
|
||||
n_copied = args.dst.limited_copy_u32_from_slice!(
|
||||
up_to: hlen, s: this.history[(this.history_index - hdist) & 0x7FFF ..])
|
||||
if n_copied < hlen {
|
||||
assert n_copied < length via "a < b: a < c; c <= b"(c: hlen)
|
||||
assert length > n_copied via "a > b: b < a"()
|
||||
length -= n_copied
|
||||
yield? base."$short write"
|
||||
continue.inner
|
||||
}
|
||||
assert length >= hlen via "a >= b: b <= a"()
|
||||
length -= hlen
|
||||
if length == 0 {
|
||||
// No need to copy from args.dst.
|
||||
continue.loop
|
||||
}
|
||||
}
|
||||
|
||||
// Copy from args.dst.
|
||||
n_copied = args.dst.limited_copy_u32_from_history!(
|
||||
up_to: length, distance: (dist_minus_1 + 1))
|
||||
if length <= n_copied {
|
||||
continue.loop
|
||||
}
|
||||
length -= n_copied
|
||||
yield? base."$short write"
|
||||
} endwhile.inner
|
||||
} endwhile.loop
|
||||
// Copy from args.dst.
|
||||
n_copied = args.dst.limited_copy_u32_from_history!(
|
||||
up_to: length, distance: (dist_minus_1 + 1))
|
||||
if length <= n_copied {
|
||||
continue.loop
|
||||
}
|
||||
length -= n_copied
|
||||
yield? base."$short write"
|
||||
} endwhile.inner
|
||||
} endwhile.loop
|
||||
|
||||
// TODO: "assert n_bits < 8"? What about (bits >> n_bits)?
|
||||
// TODO: "assert n_bits < 8"? What about (bits >> n_bits)?
|
||||
|
||||
this.bits = bits
|
||||
this.n_bits = n_bits
|
||||
this.bits = bits
|
||||
this.n_bits = n_bits
|
||||
|
||||
if (this.n_bits >= 8) or ((this.bits >> (this.n_bits & 7)) <> 0) {
|
||||
return "#internal error: inconsistent n_bits"
|
||||
}
|
||||
if (this.n_bits >= 8) or ((this.bits >> (this.n_bits & 7)) <> 0) {
|
||||
return "#internal error: inconsistent n_bits"
|
||||
}
|
||||
}
|
||||
@@ -45,20 +45,20 @@ pri const INTERLACE_COUNT : array[5] base.u8 = [0, 1, 2, 4, 8]
|
||||
|
||||
// ANIMEXTS1DOT0 is "ANIMEXTS1.0" as bytes.
|
||||
pri const ANIMEXTS1DOT0 : array[11] base.u8 = [
|
||||
0x41, 0x4E, 0x49, 0x4D, 0x45, 0x58, 0x54, 0x53, 0x31, 0x2E, 0x30,
|
||||
0x41, 0x4E, 0x49, 0x4D, 0x45, 0x58, 0x54, 0x53, 0x31, 0x2E, 0x30,
|
||||
]
|
||||
|
||||
// NETSCAPE2DOT0 is "NETSCAPE2.0" as bytes.
|
||||
pri const NETSCAPE2DOT0 : array[11] base.u8 = [
|
||||
0x4E, 0x45, 0x54, 0x53, 0x43, 0x41, 0x50, 0x45, 0x32, 0x2E, 0x30,
|
||||
0x4E, 0x45, 0x54, 0x53, 0x43, 0x41, 0x50, 0x45, 0x32, 0x2E, 0x30,
|
||||
]
|
||||
|
||||
// ICCRGBG1012 Is "ICCRGBG1012" as bytes.
|
||||
pri const ICCRGBG1012 : array[11] base.u8 = [
|
||||
0x49, 0x43, 0x43, 0x52, 0x47, 0x42, 0x47, 0x31, 0x30, 0x31, 0x32,
|
||||
0x49, 0x43, 0x43, 0x52, 0x47, 0x42, 0x47, 0x31, 0x30, 0x31, 0x32,
|
||||
]
|
||||
|
||||
// XMPDATAXMP IS "XMP DataXMP" as bytes.
|
||||
pri const XMPDATAXMP : array[11] base.u8 = [
|
||||
0x58, 0x4D, 0x50, 0x20, 0x44, 0x61, 0x74, 0x61, 0x58, 0x4D, 0x50,
|
||||
0x58, 0x4D, 0x50, 0x20, 0x44, 0x61, 0x74, 0x61, 0x58, 0x4D, 0x50,
|
||||
]
|
||||
+906
-906
@@ -13,590 +13,590 @@
|
||||
// limitations under the License.
|
||||
|
||||
pub struct decoder? implements base.image_decoder(
|
||||
width : base.u32,
|
||||
height : base.u32,
|
||||
width : base.u32,
|
||||
height : base.u32,
|
||||
|
||||
// The call sequence state machine is discussed in
|
||||
// (/doc/std/image-decoders-call-sequence.md).
|
||||
//
|
||||
// A GIF's first frame's bounds affects the overall image bounds (per
|
||||
// test/data/artificial/gif-frame-out-of-bounds.gif.make-artificial.txt).
|
||||
// Decoding the first frame's bounds is part of decode_image_config even
|
||||
// though it's conceptually part of the first decode_frame_config.
|
||||
call_sequence : base.u8,
|
||||
// The call sequence state machine is discussed in
|
||||
// (/doc/std/image-decoders-call-sequence.md).
|
||||
//
|
||||
// A GIF's first frame's bounds affects the overall image bounds (per
|
||||
// test/data/artificial/gif-frame-out-of-bounds.gif.make-artificial.txt).
|
||||
// Decoding the first frame's bounds is part of decode_image_config even
|
||||
// though it's conceptually part of the first decode_frame_config.
|
||||
call_sequence : base.u8,
|
||||
|
||||
report_metadata_iccp : base.bool,
|
||||
report_metadata_xmp : base.bool,
|
||||
metadata_fourcc : base.u32,
|
||||
metadata_io_position : base.u64,
|
||||
report_metadata_iccp : base.bool,
|
||||
report_metadata_xmp : base.bool,
|
||||
metadata_fourcc : base.u32,
|
||||
metadata_io_position : base.u64,
|
||||
|
||||
quirks : array[QUIRKS_COUNT] base.bool,
|
||||
quirks : array[QUIRKS_COUNT] base.bool,
|
||||
|
||||
delayed_num_decoded_frames : base.bool,
|
||||
previous_lzw_decode_ended_abruptly : base.bool,
|
||||
seen_header : base.bool,
|
||||
delayed_num_decoded_frames : base.bool,
|
||||
previous_lzw_decode_ended_abruptly : base.bool,
|
||||
seen_header : base.bool,
|
||||
|
||||
has_global_palette : base.bool,
|
||||
has_global_palette : base.bool,
|
||||
|
||||
// interlace indexes the INTERLACE_START and INTERLACE_DELTA arrays.
|
||||
interlace : base.u8[..= 4],
|
||||
// interlace indexes the INTERLACE_START and INTERLACE_DELTA arrays.
|
||||
interlace : base.u8[..= 4],
|
||||
|
||||
// Absent an ANIMEXTS1.0 or NETSCAPE2.0 extension, the implicit number of
|
||||
// animation loops is 1.
|
||||
seen_num_animation_loops_value : base.bool,
|
||||
num_animation_loops_value : base.u32,
|
||||
// Absent an ANIMEXTS1.0 or NETSCAPE2.0 extension, the implicit number of
|
||||
// animation loops is 1.
|
||||
seen_num_animation_loops_value : base.bool,
|
||||
num_animation_loops_value : base.u32,
|
||||
|
||||
background_color_u32_argb_premul : base.u32,
|
||||
black_color_u32_argb_premul : base.u32,
|
||||
background_color_u32_argb_premul : base.u32,
|
||||
black_color_u32_argb_premul : base.u32,
|
||||
|
||||
gc_has_transparent_index : base.bool,
|
||||
gc_transparent_index : base.u8,
|
||||
gc_disposal : base.u8,
|
||||
// There are 7_056000 flicks per centisecond.
|
||||
gc_duration : base.u64[..= 0xFFFF * 7_056000],
|
||||
gc_has_transparent_index : base.bool,
|
||||
gc_transparent_index : base.u8,
|
||||
gc_disposal : base.u8,
|
||||
// There are 7_056000 flicks per centisecond.
|
||||
gc_duration : base.u64[..= 0xFFFF * 7_056000],
|
||||
|
||||
frame_config_io_position : base.u64,
|
||||
frame_config_io_position : base.u64,
|
||||
|
||||
num_decoded_frame_configs_value : base.u64,
|
||||
num_decoded_frames_value : base.u64,
|
||||
num_decoded_frame_configs_value : base.u64,
|
||||
num_decoded_frames_value : base.u64,
|
||||
|
||||
frame_rect_x0 : base.u32,
|
||||
frame_rect_y0 : base.u32,
|
||||
frame_rect_x1 : base.u32,
|
||||
frame_rect_y1 : base.u32,
|
||||
frame_rect_x0 : base.u32,
|
||||
frame_rect_y0 : base.u32,
|
||||
frame_rect_x1 : base.u32,
|
||||
frame_rect_y1 : base.u32,
|
||||
|
||||
// The dst_etc fields are the output cursor during copy_to_image_buffer.
|
||||
dst_x : base.u32,
|
||||
dst_y : base.u32,
|
||||
dirty_max_excl_y : base.u32,
|
||||
// The dst_etc fields are the output cursor during copy_to_image_buffer.
|
||||
dst_x : base.u32,
|
||||
dst_y : base.u32,
|
||||
dirty_max_excl_y : base.u32,
|
||||
|
||||
// Indexes into the compressed array, defined below.
|
||||
compressed_ri : base.u64,
|
||||
compressed_wi : base.u64,
|
||||
// Indexes into the compressed array, defined below.
|
||||
compressed_ri : base.u64,
|
||||
compressed_wi : base.u64,
|
||||
|
||||
swizzler : base.pixel_swizzler,
|
||||
swizzler : base.pixel_swizzler,
|
||||
|
||||
util : base.utility,
|
||||
util : base.utility,
|
||||
)(
|
||||
compressed : array[4096] base.u8,
|
||||
compressed : array[4096] base.u8,
|
||||
|
||||
// palettes[0] and palettes[1] are the Global and Local Color Table.
|
||||
palettes : array[2] array[4 * 256] base.u8,
|
||||
// dst_palette is the swizzled color table.
|
||||
dst_palette : array[4 * 256] base.u8,
|
||||
// palettes[0] and palettes[1] are the Global and Local Color Table.
|
||||
palettes : array[2] array[4 * 256] base.u8,
|
||||
// dst_palette is the swizzled color table.
|
||||
dst_palette : array[4 * 256] base.u8,
|
||||
|
||||
lzw : lzw.decoder,
|
||||
lzw : lzw.decoder,
|
||||
)
|
||||
|
||||
pub func decoder.set_quirk_enabled!(quirk: base.u32, enabled: base.bool) {
|
||||
if (this.call_sequence == 0x00) and (args.quirk >= QUIRKS_BASE) {
|
||||
args.quirk -= QUIRKS_BASE
|
||||
if args.quirk < QUIRKS_COUNT {
|
||||
this.quirks[args.quirk] = args.enabled
|
||||
}
|
||||
}
|
||||
if (this.call_sequence == 0x00) and (args.quirk >= QUIRKS_BASE) {
|
||||
args.quirk -= QUIRKS_BASE
|
||||
if args.quirk < QUIRKS_COUNT {
|
||||
this.quirks[args.quirk] = args.enabled
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_reader) {
|
||||
var status : base.status
|
||||
var status : base.status
|
||||
|
||||
while true {
|
||||
status =? this.do_decode_image_config?(dst: args.dst, src: args.src)
|
||||
if (status == base."$short read") and args.src.is_closed() {
|
||||
return "#truncated input"
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
while true {
|
||||
status =? this.do_decode_image_config?(dst: args.dst, src: args.src)
|
||||
if (status == base."$short read") and args.src.is_closed() {
|
||||
return "#truncated input"
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
}
|
||||
|
||||
pri func decoder.do_decode_image_config?(dst: nptr base.image_config, src: base.io_reader) {
|
||||
var ffio : base.bool
|
||||
var ffio : base.bool
|
||||
|
||||
if this.call_sequence <> 0x00 {
|
||||
return base."#bad call sequence"
|
||||
} else if not this.seen_header {
|
||||
this.decode_header?(src: args.src)
|
||||
this.decode_lsd?(src: args.src)
|
||||
this.seen_header = true
|
||||
}
|
||||
if this.call_sequence <> 0x00 {
|
||||
return base."#bad call sequence"
|
||||
} else if not this.seen_header {
|
||||
this.decode_header?(src: args.src)
|
||||
this.decode_lsd?(src: args.src)
|
||||
this.seen_header = true
|
||||
}
|
||||
|
||||
this.decode_up_to_id_part1?(src: args.src)
|
||||
this.decode_up_to_id_part1?(src: args.src)
|
||||
|
||||
ffio = not this.gc_has_transparent_index
|
||||
if not this.quirks[QUIRK_HONOR_BACKGROUND_COLOR - QUIRKS_BASE] {
|
||||
ffio = ffio and
|
||||
(this.frame_rect_x0 == 0) and
|
||||
(this.frame_rect_y0 == 0) and
|
||||
(this.frame_rect_x1 == this.width) and
|
||||
(this.frame_rect_y1 == this.height)
|
||||
} else if ffio {
|
||||
// Use opaque black, not transparent black.
|
||||
this.black_color_u32_argb_premul = 0xFF00_0000
|
||||
}
|
||||
ffio = not this.gc_has_transparent_index
|
||||
if not this.quirks[QUIRK_HONOR_BACKGROUND_COLOR - QUIRKS_BASE] {
|
||||
ffio = ffio and
|
||||
(this.frame_rect_x0 == 0) and
|
||||
(this.frame_rect_y0 == 0) and
|
||||
(this.frame_rect_x1 == this.width) and
|
||||
(this.frame_rect_y1 == this.height)
|
||||
} else if ffio {
|
||||
// Use opaque black, not transparent black.
|
||||
this.black_color_u32_argb_premul = 0xFF00_0000
|
||||
}
|
||||
|
||||
if this.background_color_u32_argb_premul == 77 {
|
||||
this.background_color_u32_argb_premul = this.black_color_u32_argb_premul
|
||||
}
|
||||
if this.background_color_u32_argb_premul == 77 {
|
||||
this.background_color_u32_argb_premul = this.black_color_u32_argb_premul
|
||||
}
|
||||
|
||||
if args.dst <> nullptr {
|
||||
args.dst.set!(
|
||||
pixfmt: base.PIXEL_FORMAT__INDEXED__BGRA_BINARY,
|
||||
pixsub: 0,
|
||||
width: this.width,
|
||||
height: this.height,
|
||||
first_frame_io_position: this.frame_config_io_position,
|
||||
first_frame_is_opaque: ffio)
|
||||
}
|
||||
if args.dst <> nullptr {
|
||||
args.dst.set!(
|
||||
pixfmt: base.PIXEL_FORMAT__INDEXED__BGRA_BINARY,
|
||||
pixsub: 0,
|
||||
width: this.width,
|
||||
height: this.height,
|
||||
first_frame_io_position: this.frame_config_io_position,
|
||||
first_frame_is_opaque: ffio)
|
||||
}
|
||||
|
||||
if this.call_sequence == 0x00 {
|
||||
this.call_sequence = 0x20
|
||||
}
|
||||
if this.call_sequence == 0x00 {
|
||||
this.call_sequence = 0x20
|
||||
}
|
||||
}
|
||||
|
||||
pub func decoder.set_report_metadata!(fourcc: base.u32, report: base.bool) {
|
||||
if args.fourcc == 'ICCP'be {
|
||||
this.report_metadata_iccp = args.report
|
||||
} else if args.fourcc == 'XMP 'be {
|
||||
this.report_metadata_xmp = args.report
|
||||
}
|
||||
if args.fourcc == 'ICCP'be {
|
||||
this.report_metadata_iccp = args.report
|
||||
} else if args.fourcc == 'XMP 'be {
|
||||
this.report_metadata_xmp = args.report
|
||||
}
|
||||
}
|
||||
|
||||
pub func decoder.tell_me_more?(dst: base.io_writer, minfo: nptr base.more_information, src: base.io_reader) {
|
||||
var status : base.status
|
||||
var status : base.status
|
||||
|
||||
while true {
|
||||
status =? this.do_tell_me_more?(dst: args.dst, minfo: args.minfo, src: args.src)
|
||||
if (status == base."$short read") and args.src.is_closed() {
|
||||
return "#truncated input"
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
while true {
|
||||
status =? this.do_tell_me_more?(dst: args.dst, minfo: args.minfo, src: args.src)
|
||||
if (status == base."$short read") and args.src.is_closed() {
|
||||
return "#truncated input"
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
}
|
||||
|
||||
pri func decoder.do_tell_me_more?(dst: base.io_writer, minfo: nptr base.more_information, src: base.io_reader) {
|
||||
var chunk_length : base.u64
|
||||
var chunk_length : base.u64
|
||||
|
||||
if (this.call_sequence & 0x10) == 0 {
|
||||
return base."#bad call sequence"
|
||||
}
|
||||
if this.metadata_fourcc == 0 {
|
||||
return base."#no more information"
|
||||
}
|
||||
if (this.call_sequence & 0x10) == 0 {
|
||||
return base."#bad call sequence"
|
||||
}
|
||||
if this.metadata_fourcc == 0 {
|
||||
return base."#no more information"
|
||||
}
|
||||
|
||||
while true {
|
||||
while true,
|
||||
post args.src.length() > 0,
|
||||
{
|
||||
if args.src.position() <> this.metadata_io_position {
|
||||
if args.minfo <> nullptr {
|
||||
args.minfo.set!(
|
||||
flavor: base.MORE_INFORMATION__FLAVOR__IO_SEEK,
|
||||
w: 0,
|
||||
x: this.metadata_io_position,
|
||||
y: 0,
|
||||
z: 0)
|
||||
}
|
||||
yield? base."$mispositioned read"
|
||||
continue
|
||||
}
|
||||
while true {
|
||||
while true,
|
||||
post args.src.length() > 0,
|
||||
{
|
||||
if args.src.position() <> this.metadata_io_position {
|
||||
if args.minfo <> nullptr {
|
||||
args.minfo.set!(
|
||||
flavor: base.MORE_INFORMATION__FLAVOR__IO_SEEK,
|
||||
w: 0,
|
||||
x: this.metadata_io_position,
|
||||
y: 0,
|
||||
z: 0)
|
||||
}
|
||||
yield? base."$mispositioned read"
|
||||
continue
|
||||
}
|
||||
|
||||
if args.src.length() <= 0 {
|
||||
if args.minfo <> nullptr {
|
||||
args.minfo.set!(
|
||||
flavor: 0,
|
||||
w: 0,
|
||||
x: 0,
|
||||
y: 0,
|
||||
z: 0)
|
||||
}
|
||||
yield? base."$short read"
|
||||
continue
|
||||
}
|
||||
if args.src.length() <= 0 {
|
||||
if args.minfo <> nullptr {
|
||||
args.minfo.set!(
|
||||
flavor: 0,
|
||||
w: 0,
|
||||
x: 0,
|
||||
y: 0,
|
||||
z: 0)
|
||||
}
|
||||
yield? base."$short read"
|
||||
continue
|
||||
}
|
||||
|
||||
break
|
||||
} endwhile
|
||||
break
|
||||
} endwhile
|
||||
|
||||
chunk_length = args.src.peek_u8_as_u64()
|
||||
if chunk_length <= 0 {
|
||||
// Consume the '\x00' that means a zero-length block.
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
break
|
||||
}
|
||||
chunk_length = args.src.peek_u8_as_u64()
|
||||
if chunk_length <= 0 {
|
||||
// Consume the '\x00' that means a zero-length block.
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
break
|
||||
}
|
||||
|
||||
if this.metadata_fourcc == 'XMP 'be {
|
||||
// The +1 is because XMP metadata's encoding includes each
|
||||
// block's leading byte (the block size) as part of the
|
||||
// metadata passed to the caller.
|
||||
chunk_length += 1
|
||||
} else {
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
}
|
||||
this.metadata_io_position = args.src.position() ~sat+ chunk_length
|
||||
if this.metadata_fourcc == 'XMP 'be {
|
||||
// The +1 is because XMP metadata's encoding includes each
|
||||
// block's leading byte (the block size) as part of the
|
||||
// metadata passed to the caller.
|
||||
chunk_length += 1
|
||||
} else {
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
}
|
||||
this.metadata_io_position = args.src.position() ~sat+ chunk_length
|
||||
|
||||
if args.minfo <> nullptr {
|
||||
args.minfo.set!(
|
||||
flavor: base.MORE_INFORMATION__FLAVOR__METADATA_RAW_PASSTHROUGH,
|
||||
w: this.metadata_fourcc,
|
||||
x: 0,
|
||||
y: args.src.position(),
|
||||
z: this.metadata_io_position)
|
||||
}
|
||||
if args.minfo <> nullptr {
|
||||
args.minfo.set!(
|
||||
flavor: base.MORE_INFORMATION__FLAVOR__METADATA_RAW_PASSTHROUGH,
|
||||
w: this.metadata_fourcc,
|
||||
x: 0,
|
||||
y: args.src.position(),
|
||||
z: this.metadata_io_position)
|
||||
}
|
||||
|
||||
yield? base."$even more information"
|
||||
} endwhile
|
||||
yield? base."$even more information"
|
||||
} endwhile
|
||||
|
||||
if args.minfo <> nullptr {
|
||||
args.minfo.set!(
|
||||
flavor: base.MORE_INFORMATION__FLAVOR__METADATA_RAW_PASSTHROUGH,
|
||||
w: this.metadata_fourcc,
|
||||
x: 0,
|
||||
y: this.metadata_io_position,
|
||||
z: this.metadata_io_position)
|
||||
}
|
||||
this.call_sequence &= 0xEF
|
||||
this.metadata_fourcc = 0
|
||||
this.metadata_io_position = 0
|
||||
return ok
|
||||
if args.minfo <> nullptr {
|
||||
args.minfo.set!(
|
||||
flavor: base.MORE_INFORMATION__FLAVOR__METADATA_RAW_PASSTHROUGH,
|
||||
w: this.metadata_fourcc,
|
||||
x: 0,
|
||||
y: this.metadata_io_position,
|
||||
z: this.metadata_io_position)
|
||||
}
|
||||
this.call_sequence &= 0xEF
|
||||
this.metadata_fourcc = 0
|
||||
this.metadata_io_position = 0
|
||||
return ok
|
||||
}
|
||||
|
||||
pub func decoder.num_animation_loops() base.u32 {
|
||||
if this.seen_num_animation_loops_value {
|
||||
return this.num_animation_loops_value
|
||||
}
|
||||
// Absent an explicit animation loop count, default to 1 meaning "play each
|
||||
// frame exactly once" for animated GIFs and 0 meaning "loop forever" for
|
||||
// still (non-animated) GIFs.
|
||||
//
|
||||
// In practice, returning 1 or 0 has no difference for still GIFs, in that
|
||||
// either way, the pixels on screen do not change over time. However,
|
||||
// returning 0 here matches other Wuffs image decoders (e.g. BMP, PNG)
|
||||
// returning 0 for their still images.
|
||||
//
|
||||
// As a consequence, if a still GIF is losslessly converted to a still BMP
|
||||
// and both of those are then converted to NIA, the two NIA outputs match.
|
||||
if this.num_decoded_frame_configs_value > 1 {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
if this.seen_num_animation_loops_value {
|
||||
return this.num_animation_loops_value
|
||||
}
|
||||
// Absent an explicit animation loop count, default to 1 meaning "play each
|
||||
// frame exactly once" for animated GIFs and 0 meaning "loop forever" for
|
||||
// still (non-animated) GIFs.
|
||||
//
|
||||
// In practice, returning 1 or 0 has no difference for still GIFs, in that
|
||||
// either way, the pixels on screen do not change over time. However,
|
||||
// returning 0 here matches other Wuffs image decoders (e.g. BMP, PNG)
|
||||
// returning 0 for their still images.
|
||||
//
|
||||
// As a consequence, if a still GIF is losslessly converted to a still BMP
|
||||
// and both of those are then converted to NIA, the two NIA outputs match.
|
||||
if this.num_decoded_frame_configs_value > 1 {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
pub func decoder.num_decoded_frame_configs() base.u64 {
|
||||
return this.num_decoded_frame_configs_value
|
||||
return this.num_decoded_frame_configs_value
|
||||
}
|
||||
|
||||
pub func decoder.num_decoded_frames() base.u64 {
|
||||
return this.num_decoded_frames_value
|
||||
return this.num_decoded_frames_value
|
||||
}
|
||||
|
||||
pub func decoder.frame_dirty_rect() base.rect_ie_u32 {
|
||||
// The "foo.min(a:this.width_or_height)" calls clip the nominal frame_rect
|
||||
// to the image_rect.
|
||||
return this.util.make_rect_ie_u32(
|
||||
min_incl_x: this.frame_rect_x0.min(a: this.width),
|
||||
min_incl_y: this.frame_rect_y0.min(a: this.height),
|
||||
max_excl_x: this.frame_rect_x1.min(a: this.width),
|
||||
max_excl_y: this.dirty_max_excl_y.min(a: this.height))
|
||||
// The "foo.min(a:this.width_or_height)" calls clip the nominal frame_rect
|
||||
// to the image_rect.
|
||||
return this.util.make_rect_ie_u32(
|
||||
min_incl_x: this.frame_rect_x0.min(a: this.width),
|
||||
min_incl_y: this.frame_rect_y0.min(a: this.height),
|
||||
max_excl_x: this.frame_rect_x1.min(a: this.width),
|
||||
max_excl_y: this.dirty_max_excl_y.min(a: this.height))
|
||||
}
|
||||
|
||||
pub func decoder.workbuf_len() base.range_ii_u64 {
|
||||
return this.util.make_range_ii_u64(
|
||||
min_incl: DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE,
|
||||
max_incl: DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE)
|
||||
return this.util.make_range_ii_u64(
|
||||
min_incl: DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE,
|
||||
max_incl: DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE)
|
||||
}
|
||||
|
||||
pub func decoder.restart_frame!(index: base.u64, io_position: base.u64) base.status {
|
||||
if this.call_sequence < 0x20 {
|
||||
return base."#bad call sequence"
|
||||
} else if args.io_position == 0 {
|
||||
return base."#bad argument"
|
||||
}
|
||||
this.delayed_num_decoded_frames = false
|
||||
this.frame_config_io_position = args.io_position
|
||||
this.num_decoded_frame_configs_value = args.index
|
||||
this.num_decoded_frames_value = args.index
|
||||
this.reset_gc!()
|
||||
this.call_sequence = 0x28
|
||||
return ok
|
||||
if this.call_sequence < 0x20 {
|
||||
return base."#bad call sequence"
|
||||
} else if args.io_position == 0 {
|
||||
return base."#bad argument"
|
||||
}
|
||||
this.delayed_num_decoded_frames = false
|
||||
this.frame_config_io_position = args.io_position
|
||||
this.num_decoded_frame_configs_value = args.index
|
||||
this.num_decoded_frames_value = args.index
|
||||
this.reset_gc!()
|
||||
this.call_sequence = 0x28
|
||||
return ok
|
||||
}
|
||||
|
||||
pub func decoder.decode_frame_config?(dst: nptr base.frame_config, src: base.io_reader) {
|
||||
var status : base.status
|
||||
var status : base.status
|
||||
|
||||
while true {
|
||||
status =? this.do_decode_frame_config?(dst: args.dst, src: args.src)
|
||||
if (status == base."$short read") and args.src.is_closed() {
|
||||
return "#truncated input"
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
while true {
|
||||
status =? this.do_decode_frame_config?(dst: args.dst, src: args.src)
|
||||
if (status == base."$short read") and args.src.is_closed() {
|
||||
return "#truncated input"
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
}
|
||||
|
||||
pri func decoder.do_decode_frame_config?(dst: nptr base.frame_config, src: base.io_reader) {
|
||||
var background_color : base.u32
|
||||
var flags : base.u8
|
||||
var background_color : base.u32
|
||||
var flags : base.u8
|
||||
|
||||
this.dirty_max_excl_y = 0
|
||||
this.dirty_max_excl_y = 0
|
||||
|
||||
if (this.call_sequence & 0x10) <> 0 {
|
||||
return base."#bad call sequence"
|
||||
} else if this.call_sequence == 0x20 {
|
||||
// No-op.
|
||||
} else if this.call_sequence < 0x20 {
|
||||
this.do_decode_image_config?(dst: nullptr, src: args.src)
|
||||
} else if this.call_sequence == 0x28 {
|
||||
if this.frame_config_io_position <> args.src.position() {
|
||||
return base."#bad restart"
|
||||
}
|
||||
} else if this.call_sequence == 0x40 {
|
||||
this.skip_frame?(src: args.src)
|
||||
if this.call_sequence >= 0x60 {
|
||||
return base."@end of data"
|
||||
}
|
||||
} else {
|
||||
return base."@end of data"
|
||||
}
|
||||
if (this.call_sequence & 0x10) <> 0 {
|
||||
return base."#bad call sequence"
|
||||
} else if this.call_sequence == 0x20 {
|
||||
// No-op.
|
||||
} else if this.call_sequence < 0x20 {
|
||||
this.do_decode_image_config?(dst: nullptr, src: args.src)
|
||||
} else if this.call_sequence == 0x28 {
|
||||
if this.frame_config_io_position <> args.src.position() {
|
||||
return base."#bad restart"
|
||||
}
|
||||
} else if this.call_sequence == 0x40 {
|
||||
this.skip_frame?(src: args.src)
|
||||
if this.call_sequence >= 0x60 {
|
||||
return base."@end of data"
|
||||
}
|
||||
} else {
|
||||
return base."@end of data"
|
||||
}
|
||||
|
||||
if (this.num_decoded_frame_configs_value > 0) or (this.call_sequence == 0x28) {
|
||||
this.decode_up_to_id_part1?(src: args.src)
|
||||
if this.call_sequence >= 0x60 {
|
||||
return base."@end of data"
|
||||
}
|
||||
}
|
||||
if (this.num_decoded_frame_configs_value > 0) or (this.call_sequence == 0x28) {
|
||||
this.decode_up_to_id_part1?(src: args.src)
|
||||
if this.call_sequence >= 0x60 {
|
||||
return base."@end of data"
|
||||
}
|
||||
}
|
||||
|
||||
background_color = this.black_color_u32_argb_premul
|
||||
if not this.gc_has_transparent_index {
|
||||
background_color = this.background_color_u32_argb_premul
|
||||
background_color = this.black_color_u32_argb_premul
|
||||
if not this.gc_has_transparent_index {
|
||||
background_color = this.background_color_u32_argb_premul
|
||||
|
||||
// If the quirk is enabled and the first frame has a local color
|
||||
// palette, its background color is black.
|
||||
if this.quirks[QUIRK_FIRST_FRAME_LOCAL_PALETTE_MEANS_BLACK_BACKGROUND - QUIRKS_BASE] and
|
||||
(this.num_decoded_frame_configs_value == 0) {
|
||||
// If the quirk is enabled and the first frame has a local color
|
||||
// palette, its background color is black.
|
||||
if this.quirks[QUIRK_FIRST_FRAME_LOCAL_PALETTE_MEANS_BLACK_BACKGROUND - QUIRKS_BASE] and
|
||||
(this.num_decoded_frame_configs_value == 0) {
|
||||
|
||||
while args.src.length() <= 0,
|
||||
post args.src.length() > 0,
|
||||
{
|
||||
yield? base."$short read"
|
||||
} endwhile
|
||||
flags = args.src.peek_u8()
|
||||
if (flags & 0x80) <> 0 {
|
||||
background_color = this.black_color_u32_argb_premul
|
||||
}
|
||||
}
|
||||
}
|
||||
while args.src.length() <= 0,
|
||||
post args.src.length() > 0,
|
||||
{
|
||||
yield? base."$short read"
|
||||
} endwhile
|
||||
flags = args.src.peek_u8()
|
||||
if (flags & 0x80) <> 0 {
|
||||
background_color = this.black_color_u32_argb_premul
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if args.dst <> nullptr {
|
||||
// The "foo.min(a:this.width_or_height)" calls clip the nominal
|
||||
// frame_rect to the image_rect.
|
||||
args.dst.set!(bounds: this.util.make_rect_ie_u32(
|
||||
min_incl_x: this.frame_rect_x0.min(a: this.width),
|
||||
min_incl_y: this.frame_rect_y0.min(a: this.height),
|
||||
max_excl_x: this.frame_rect_x1.min(a: this.width),
|
||||
max_excl_y: this.frame_rect_y1.min(a: this.height)),
|
||||
duration: this.gc_duration,
|
||||
index: this.num_decoded_frame_configs_value,
|
||||
io_position: this.frame_config_io_position,
|
||||
disposal: this.gc_disposal,
|
||||
opaque_within_bounds: not this.gc_has_transparent_index,
|
||||
overwrite_instead_of_blend: false,
|
||||
background_color: background_color)
|
||||
}
|
||||
if args.dst <> nullptr {
|
||||
// The "foo.min(a:this.width_or_height)" calls clip the nominal
|
||||
// frame_rect to the image_rect.
|
||||
args.dst.set!(bounds: this.util.make_rect_ie_u32(
|
||||
min_incl_x: this.frame_rect_x0.min(a: this.width),
|
||||
min_incl_y: this.frame_rect_y0.min(a: this.height),
|
||||
max_excl_x: this.frame_rect_x1.min(a: this.width),
|
||||
max_excl_y: this.frame_rect_y1.min(a: this.height)),
|
||||
duration: this.gc_duration,
|
||||
index: this.num_decoded_frame_configs_value,
|
||||
io_position: this.frame_config_io_position,
|
||||
disposal: this.gc_disposal,
|
||||
opaque_within_bounds: not this.gc_has_transparent_index,
|
||||
overwrite_instead_of_blend: false,
|
||||
background_color: background_color)
|
||||
}
|
||||
|
||||
this.num_decoded_frame_configs_value ~sat+= 1
|
||||
this.call_sequence = 0x40
|
||||
this.num_decoded_frame_configs_value ~sat+= 1
|
||||
this.call_sequence = 0x40
|
||||
}
|
||||
|
||||
pri func decoder.skip_frame?(src: base.io_reader) {
|
||||
var flags : base.u8
|
||||
var lw : base.u8
|
||||
var flags : base.u8
|
||||
var lw : base.u8
|
||||
|
||||
// Skip the optional Local Color Table, 3 bytes (RGB) per entry.
|
||||
flags = args.src.read_u8?()
|
||||
if (flags & 0x80) <> 0 {
|
||||
args.src.skip_u32?(n: (3 as base.u32) << (1 + (flags & 0x07)))
|
||||
}
|
||||
// Skip the optional Local Color Table, 3 bytes (RGB) per entry.
|
||||
flags = args.src.read_u8?()
|
||||
if (flags & 0x80) <> 0 {
|
||||
args.src.skip_u32?(n: (3 as base.u32) << (1 + (flags & 0x07)))
|
||||
}
|
||||
|
||||
// Process the LZW literal width.
|
||||
lw = args.src.read_u8?()
|
||||
if lw > 8 {
|
||||
return "#bad literal width"
|
||||
}
|
||||
// Process the LZW literal width.
|
||||
lw = args.src.read_u8?()
|
||||
if lw > 8 {
|
||||
return "#bad literal width"
|
||||
}
|
||||
|
||||
// Skip the blocks of LZW-compressed data.
|
||||
this.skip_blocks?(src: args.src)
|
||||
// Skip the blocks of LZW-compressed data.
|
||||
this.skip_blocks?(src: args.src)
|
||||
|
||||
if this.quirks[QUIRK_DELAY_NUM_DECODED_FRAMES - QUIRKS_BASE] {
|
||||
this.delayed_num_decoded_frames = true
|
||||
} else {
|
||||
this.num_decoded_frames_value ~sat+= 1
|
||||
}
|
||||
this.reset_gc!()
|
||||
this.call_sequence = 0x20
|
||||
if this.quirks[QUIRK_DELAY_NUM_DECODED_FRAMES - QUIRKS_BASE] {
|
||||
this.delayed_num_decoded_frames = true
|
||||
} else {
|
||||
this.num_decoded_frames_value ~sat+= 1
|
||||
}
|
||||
this.reset_gc!()
|
||||
this.call_sequence = 0x20
|
||||
}
|
||||
|
||||
pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader, blend: base.pixel_blend, workbuf: slice base.u8, opts: nptr base.decode_frame_options) {
|
||||
var status : base.status
|
||||
var status : base.status
|
||||
|
||||
while true {
|
||||
status =? this.do_decode_frame?(dst: args.dst, src: args.src, blend: args.blend, workbuf: args.workbuf, opts: args.opts)
|
||||
if (status == base."$short read") and args.src.is_closed() {
|
||||
return "#truncated input"
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
while true {
|
||||
status =? this.do_decode_frame?(dst: args.dst, src: args.src, blend: args.blend, workbuf: args.workbuf, opts: args.opts)
|
||||
if (status == base."$short read") and args.src.is_closed() {
|
||||
return "#truncated input"
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
}
|
||||
|
||||
// TODO: honor args.opts.
|
||||
pri func decoder.do_decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader, blend: base.pixel_blend, workbuf: slice base.u8, opts: nptr base.decode_frame_options) {
|
||||
if this.call_sequence == 0x40 {
|
||||
// No-op.
|
||||
} else if this.call_sequence < 0x40 {
|
||||
this.do_decode_frame_config?(dst: nullptr, src: args.src)
|
||||
} else {
|
||||
return base."@end of data"
|
||||
}
|
||||
if this.quirks[QUIRK_REJECT_EMPTY_FRAME - QUIRKS_BASE] and
|
||||
((this.frame_rect_x0 == this.frame_rect_x1) or (this.frame_rect_y0 == this.frame_rect_y1)) {
|
||||
return "#bad frame size"
|
||||
}
|
||||
this.decode_id_part1?(dst: args.dst, src: args.src, blend: args.blend)
|
||||
this.decode_id_part2?(dst: args.dst, src: args.src, workbuf: args.workbuf)
|
||||
if this.call_sequence == 0x40 {
|
||||
// No-op.
|
||||
} else if this.call_sequence < 0x40 {
|
||||
this.do_decode_frame_config?(dst: nullptr, src: args.src)
|
||||
} else {
|
||||
return base."@end of data"
|
||||
}
|
||||
if this.quirks[QUIRK_REJECT_EMPTY_FRAME - QUIRKS_BASE] and
|
||||
((this.frame_rect_x0 == this.frame_rect_x1) or (this.frame_rect_y0 == this.frame_rect_y1)) {
|
||||
return "#bad frame size"
|
||||
}
|
||||
this.decode_id_part1?(dst: args.dst, src: args.src, blend: args.blend)
|
||||
this.decode_id_part2?(dst: args.dst, src: args.src, workbuf: args.workbuf)
|
||||
|
||||
this.num_decoded_frames_value ~sat+= 1
|
||||
this.reset_gc!()
|
||||
this.call_sequence = 0x20
|
||||
this.num_decoded_frames_value ~sat+= 1
|
||||
this.reset_gc!()
|
||||
this.call_sequence = 0x20
|
||||
}
|
||||
|
||||
pri func decoder.reset_gc!() {
|
||||
// The Image Descriptor is mandatory, but the Graphic Control extension is
|
||||
// optional. Reset the GC related fields for the next decode_frame call.
|
||||
this.gc_has_transparent_index = false
|
||||
this.gc_transparent_index = 0
|
||||
this.gc_disposal = 0
|
||||
this.gc_duration = 0
|
||||
// The Image Descriptor is mandatory, but the Graphic Control extension is
|
||||
// optional. Reset the GC related fields for the next decode_frame call.
|
||||
this.gc_has_transparent_index = false
|
||||
this.gc_transparent_index = 0
|
||||
this.gc_disposal = 0
|
||||
this.gc_duration = 0
|
||||
}
|
||||
|
||||
pri func decoder.decode_up_to_id_part1?(src: base.io_reader) {
|
||||
var block_type : base.u8
|
||||
var block_type : base.u8
|
||||
|
||||
if (this.frame_config_io_position == 0) or (this.num_decoded_frame_configs_value > 0) {
|
||||
this.frame_config_io_position = args.src.position()
|
||||
}
|
||||
if (this.frame_config_io_position == 0) or (this.num_decoded_frame_configs_value > 0) {
|
||||
this.frame_config_io_position = args.src.position()
|
||||
}
|
||||
|
||||
while true {
|
||||
block_type = args.src.read_u8?()
|
||||
if block_type == 0x21 { // The spec calls 0x21 the "Extension Introducer".
|
||||
this.decode_extension?(src: args.src)
|
||||
} else if block_type == 0x2C { // The spec calls 0x2C the "Image Separator".
|
||||
if this.delayed_num_decoded_frames {
|
||||
this.delayed_num_decoded_frames = false
|
||||
this.num_decoded_frames_value ~sat+= 1
|
||||
}
|
||||
this.decode_id_part0?(src: args.src)
|
||||
break
|
||||
} else {
|
||||
// If we don't have 0x21 or 0x2C then, according to the spec, the
|
||||
// only valid byte is 0x3B, called the "Trailer". In practice, some
|
||||
// other popular decoders allow anything (other than 0x21 or 0x2C)
|
||||
// to be equivalent to 0x3B, which ends the animated GIF image, and
|
||||
// we do likewise here. Some real world GIF files that exhibit this
|
||||
// are at https://github.com/golang/go/issues/38853
|
||||
//
|
||||
// Chromium's decoder
|
||||
// https://skia.googlesource.com/libgifcodec/+/c002ec500aba1e1b0189547629787cb02db78193/SkGifImageReader.cpp#563
|
||||
//
|
||||
// Firefox's decoder
|
||||
// https://dxr.mozilla.org/mozilla-central/rev/93a33cb7f2369ac4f1d1f2ac97ec14ba60e1e7d7/image/decoders/nsGIFDecoder2.cpp#569
|
||||
if this.delayed_num_decoded_frames {
|
||||
this.delayed_num_decoded_frames = false
|
||||
this.num_decoded_frames_value ~sat+= 1
|
||||
}
|
||||
this.call_sequence = 0x60
|
||||
break
|
||||
}
|
||||
} endwhile
|
||||
while true {
|
||||
block_type = args.src.read_u8?()
|
||||
if block_type == 0x21 { // The spec calls 0x21 the "Extension Introducer".
|
||||
this.decode_extension?(src: args.src)
|
||||
} else if block_type == 0x2C { // The spec calls 0x2C the "Image Separator".
|
||||
if this.delayed_num_decoded_frames {
|
||||
this.delayed_num_decoded_frames = false
|
||||
this.num_decoded_frames_value ~sat+= 1
|
||||
}
|
||||
this.decode_id_part0?(src: args.src)
|
||||
break
|
||||
} else {
|
||||
// If we don't have 0x21 or 0x2C then, according to the spec, the
|
||||
// only valid byte is 0x3B, called the "Trailer". In practice, some
|
||||
// other popular decoders allow anything (other than 0x21 or 0x2C)
|
||||
// to be equivalent to 0x3B, which ends the animated GIF image, and
|
||||
// we do likewise here. Some real world GIF files that exhibit this
|
||||
// are at https://github.com/golang/go/issues/38853
|
||||
//
|
||||
// Chromium's decoder
|
||||
// https://skia.googlesource.com/libgifcodec/+/c002ec500aba1e1b0189547629787cb02db78193/SkGifImageReader.cpp#563
|
||||
//
|
||||
// Firefox's decoder
|
||||
// https://dxr.mozilla.org/mozilla-central/rev/93a33cb7f2369ac4f1d1f2ac97ec14ba60e1e7d7/image/decoders/nsGIFDecoder2.cpp#569
|
||||
if this.delayed_num_decoded_frames {
|
||||
this.delayed_num_decoded_frames = false
|
||||
this.num_decoded_frames_value ~sat+= 1
|
||||
}
|
||||
this.call_sequence = 0x60
|
||||
break
|
||||
}
|
||||
} endwhile
|
||||
}
|
||||
|
||||
// decode_header reads either "GIF87a" or "GIF89a".
|
||||
//
|
||||
// See the spec section 17 "Header" on page 7.
|
||||
pri func decoder.decode_header?(src: base.io_reader) {
|
||||
var c : array[6] base.u8
|
||||
var i : base.u32
|
||||
var c : array[6] base.u8
|
||||
var i : base.u32
|
||||
|
||||
while i < 6 {
|
||||
c[i] = args.src.read_u8?()
|
||||
i += 1
|
||||
} endwhile
|
||||
if (c[0] <> 'G') or (c[1] <> 'I') or (c[2] <> 'F') or (c[3] <> '8') or
|
||||
((c[4] <> '7') and (c[4] <> '9')) or (c[5] <> 'a') {
|
||||
return "#bad header"
|
||||
}
|
||||
while i < 6 {
|
||||
c[i] = args.src.read_u8?()
|
||||
i += 1
|
||||
} endwhile
|
||||
if (c[0] <> 'G') or (c[1] <> 'I') or (c[2] <> 'F') or (c[3] <> '8') or
|
||||
((c[4] <> '7') and (c[4] <> '9')) or (c[5] <> 'a') {
|
||||
return "#bad header"
|
||||
}
|
||||
}
|
||||
|
||||
// decode_lsd reads the Logical Screen Descriptor.
|
||||
//
|
||||
// See the spec section 18 "Logical Screen Descriptor" on page 8.
|
||||
pri func decoder.decode_lsd?(src: base.io_reader) {
|
||||
var flags : base.u8
|
||||
var background_color_index : base.u8
|
||||
var num_palette_entries : base.u32[..= 256]
|
||||
var i : base.u32
|
||||
var j : base.u32[..= 1020]
|
||||
var argb : base.u32
|
||||
var flags : base.u8
|
||||
var background_color_index : base.u8
|
||||
var num_palette_entries : base.u32[..= 256]
|
||||
var i : base.u32
|
||||
var j : base.u32[..= 1020]
|
||||
var argb : base.u32
|
||||
|
||||
this.width = args.src.read_u16le_as_u32?()
|
||||
this.height = args.src.read_u16le_as_u32?()
|
||||
flags = args.src.read_u8?()
|
||||
background_color_index = args.src.read_u8?()
|
||||
// Ignore the Pixel Aspect Ratio byte.
|
||||
args.src.skip_u32?(n: 1)
|
||||
this.width = args.src.read_u16le_as_u32?()
|
||||
this.height = args.src.read_u16le_as_u32?()
|
||||
flags = args.src.read_u8?()
|
||||
background_color_index = args.src.read_u8?()
|
||||
// Ignore the Pixel Aspect Ratio byte.
|
||||
args.src.skip_u32?(n: 1)
|
||||
|
||||
// Read the optional Global Color Table.
|
||||
i = 0
|
||||
this.has_global_palette = (flags & 0x80) <> 0
|
||||
if this.has_global_palette {
|
||||
num_palette_entries = (1 as base.u32) << (1 + (flags & 0x07))
|
||||
while i < num_palette_entries {
|
||||
assert i < 256 via "a < b: a < c; c <= b"(c: num_palette_entries)
|
||||
// Convert from RGB (in memory order) to ARGB (in native u32 order)
|
||||
// to BGRA (in memory order).
|
||||
argb = args.src.read_u24be_as_u32?()
|
||||
argb |= 0xFF00_0000
|
||||
this.palettes[0][(4 * i) + 0] = ((argb >> 0) & 0xFF) as base.u8
|
||||
this.palettes[0][(4 * i) + 1] = ((argb >> 8) & 0xFF) as base.u8
|
||||
this.palettes[0][(4 * i) + 2] = ((argb >> 16) & 0xFF) as base.u8
|
||||
this.palettes[0][(4 * i) + 3] = ((argb >> 24) & 0xFF) as base.u8
|
||||
i += 1
|
||||
} endwhile
|
||||
// Read the optional Global Color Table.
|
||||
i = 0
|
||||
this.has_global_palette = (flags & 0x80) <> 0
|
||||
if this.has_global_palette {
|
||||
num_palette_entries = (1 as base.u32) << (1 + (flags & 0x07))
|
||||
while i < num_palette_entries {
|
||||
assert i < 256 via "a < b: a < c; c <= b"(c: num_palette_entries)
|
||||
// Convert from RGB (in memory order) to ARGB (in native u32 order)
|
||||
// to BGRA (in memory order).
|
||||
argb = args.src.read_u24be_as_u32?()
|
||||
argb |= 0xFF00_0000
|
||||
this.palettes[0][(4 * i) + 0] = ((argb >> 0) & 0xFF) as base.u8
|
||||
this.palettes[0][(4 * i) + 1] = ((argb >> 8) & 0xFF) as base.u8
|
||||
this.palettes[0][(4 * i) + 2] = ((argb >> 16) & 0xFF) as base.u8
|
||||
this.palettes[0][(4 * i) + 3] = ((argb >> 24) & 0xFF) as base.u8
|
||||
i += 1
|
||||
} endwhile
|
||||
|
||||
if this.quirks[QUIRK_HONOR_BACKGROUND_COLOR - QUIRKS_BASE] {
|
||||
if (background_color_index <> 0) and
|
||||
((background_color_index as base.u32) < num_palette_entries) {
|
||||
if this.quirks[QUIRK_HONOR_BACKGROUND_COLOR - QUIRKS_BASE] {
|
||||
if (background_color_index <> 0) and
|
||||
((background_color_index as base.u32) < num_palette_entries) {
|
||||
|
||||
j = 4 * (background_color_index as base.u32)
|
||||
this.background_color_u32_argb_premul =
|
||||
((this.palettes[0][j + 0] as base.u32) << 0) |
|
||||
((this.palettes[0][j + 1] as base.u32) << 8) |
|
||||
((this.palettes[0][j + 2] as base.u32) << 16) |
|
||||
((this.palettes[0][j + 3] as base.u32) << 24)
|
||||
} else {
|
||||
// The background color is either opaque black or transparent
|
||||
// black. We set it to an arbitrary nonsense value (77) for
|
||||
// now, and set it to its real value later, once we know
|
||||
// whether the first frame is opaque (the ffio value).
|
||||
this.background_color_u32_argb_premul = 77
|
||||
}
|
||||
}
|
||||
}
|
||||
j = 4 * (background_color_index as base.u32)
|
||||
this.background_color_u32_argb_premul =
|
||||
((this.palettes[0][j + 0] as base.u32) << 0) |
|
||||
((this.palettes[0][j + 1] as base.u32) << 8) |
|
||||
((this.palettes[0][j + 2] as base.u32) << 16) |
|
||||
((this.palettes[0][j + 3] as base.u32) << 24)
|
||||
} else {
|
||||
// The background color is either opaque black or transparent
|
||||
// black. We set it to an arbitrary nonsense value (77) for
|
||||
// now, and set it to its real value later, once we know
|
||||
// whether the first frame is opaque (the ffio value).
|
||||
this.background_color_u32_argb_premul = 77
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Set the remaining palette entries to opaque black.
|
||||
while i < 256 {
|
||||
this.palettes[0][(4 * i) + 0] = 0x00
|
||||
this.palettes[0][(4 * i) + 1] = 0x00
|
||||
this.palettes[0][(4 * i) + 2] = 0x00
|
||||
this.palettes[0][(4 * i) + 3] = 0xFF
|
||||
i += 1
|
||||
} endwhile
|
||||
// Set the remaining palette entries to opaque black.
|
||||
while i < 256 {
|
||||
this.palettes[0][(4 * i) + 0] = 0x00
|
||||
this.palettes[0][(4 * i) + 1] = 0x00
|
||||
this.palettes[0][(4 * i) + 2] = 0x00
|
||||
this.palettes[0][(4 * i) + 3] = 0xFF
|
||||
i += 1
|
||||
} endwhile
|
||||
}
|
||||
|
||||
// decode_extension reads an extension. The Extension Introducer byte has
|
||||
@@ -608,166 +608,166 @@ pri func decoder.decode_lsd?(src: base.io_reader) {
|
||||
// - section 25 "Plain Text Extension" on page 18.
|
||||
// - section 26 "Application Extension" on page 21.
|
||||
pri func decoder.decode_extension?(src: base.io_reader) {
|
||||
var label : base.u8
|
||||
var label : base.u8
|
||||
|
||||
label = args.src.read_u8?()
|
||||
if label == 0xF9 { // The spec calls 0xF9 the "Graphic Control Label".
|
||||
this.decode_gc?(src: args.src)
|
||||
return ok
|
||||
} else if label == 0xFF { // The spec calls 0xFF the "Application Extension Label".
|
||||
this.decode_ae?(src: args.src)
|
||||
return ok
|
||||
}
|
||||
// We skip over all other extensions, including 0x01 "Plain Text Label" and
|
||||
// 0xFE "Comment Label".
|
||||
this.skip_blocks?(src: args.src)
|
||||
label = args.src.read_u8?()
|
||||
if label == 0xF9 { // The spec calls 0xF9 the "Graphic Control Label".
|
||||
this.decode_gc?(src: args.src)
|
||||
return ok
|
||||
} else if label == 0xFF { // The spec calls 0xFF the "Application Extension Label".
|
||||
this.decode_ae?(src: args.src)
|
||||
return ok
|
||||
}
|
||||
// We skip over all other extensions, including 0x01 "Plain Text Label" and
|
||||
// 0xFE "Comment Label".
|
||||
this.skip_blocks?(src: args.src)
|
||||
}
|
||||
|
||||
pri func decoder.skip_blocks?(src: base.io_reader) {
|
||||
var block_size : base.u8
|
||||
var block_size : base.u8
|
||||
|
||||
while true {
|
||||
block_size = args.src.read_u8?()
|
||||
if block_size == 0 {
|
||||
return ok
|
||||
}
|
||||
args.src.skip_u32?(n: block_size as base.u32)
|
||||
} endwhile
|
||||
while true {
|
||||
block_size = args.src.read_u8?()
|
||||
if block_size == 0 {
|
||||
return ok
|
||||
}
|
||||
args.src.skip_u32?(n: block_size as base.u32)
|
||||
} endwhile
|
||||
}
|
||||
|
||||
// decode_ae reads an Application Extension.
|
||||
pri func decoder.decode_ae?(src: base.io_reader) {
|
||||
var c : base.u8
|
||||
var block_size : base.u8
|
||||
var is_animexts : base.bool
|
||||
var is_netscape : base.bool
|
||||
var is_iccp : base.bool
|
||||
var is_xmp : base.bool
|
||||
var c : base.u8
|
||||
var block_size : base.u8
|
||||
var is_animexts : base.bool
|
||||
var is_netscape : base.bool
|
||||
var is_iccp : base.bool
|
||||
var is_xmp : base.bool
|
||||
|
||||
while.goto_done true {{
|
||||
if this.metadata_fourcc <> 0 {
|
||||
return base."@metadata reported"
|
||||
}
|
||||
while.goto_done true {{
|
||||
if this.metadata_fourcc <> 0 {
|
||||
return base."@metadata reported"
|
||||
}
|
||||
|
||||
block_size = args.src.read_u8?()
|
||||
if block_size == 0 {
|
||||
return ok
|
||||
}
|
||||
block_size = args.src.read_u8?()
|
||||
if block_size == 0 {
|
||||
return ok
|
||||
}
|
||||
|
||||
// Look only for an 11 byte "ANIMEXTS1.0", "NETSCAPE2.0" or other
|
||||
// extension, as per:
|
||||
// - http://www.vurdalakov.net/misc/gif/animexts-looping-application-extension
|
||||
// - http://www.vurdalakov.net/misc/gif/netscape-looping-application-extension
|
||||
//
|
||||
// Other extensions include XMP metadata.
|
||||
if block_size <> 11 {
|
||||
args.src.skip_u32?(n: block_size as base.u32)
|
||||
break.goto_done
|
||||
}
|
||||
is_animexts = true
|
||||
is_netscape = true
|
||||
is_iccp = true
|
||||
is_xmp = true
|
||||
block_size = 0 // Re-purpose the block_size variable as a counter.
|
||||
while block_size < 11 {
|
||||
c = args.src.read_u8?()
|
||||
is_animexts = is_animexts and (c == ANIMEXTS1DOT0[block_size])
|
||||
is_netscape = is_netscape and (c == NETSCAPE2DOT0[block_size])
|
||||
is_iccp = is_iccp and (c == ICCRGBG1012[block_size])
|
||||
is_xmp = is_xmp and (c == XMPDATAXMP[block_size])
|
||||
block_size += 1
|
||||
} endwhile
|
||||
// Look only for an 11 byte "ANIMEXTS1.0", "NETSCAPE2.0" or other
|
||||
// extension, as per:
|
||||
// - http://www.vurdalakov.net/misc/gif/animexts-looping-application-extension
|
||||
// - http://www.vurdalakov.net/misc/gif/netscape-looping-application-extension
|
||||
//
|
||||
// Other extensions include XMP metadata.
|
||||
if block_size <> 11 {
|
||||
args.src.skip_u32?(n: block_size as base.u32)
|
||||
break.goto_done
|
||||
}
|
||||
is_animexts = true
|
||||
is_netscape = true
|
||||
is_iccp = true
|
||||
is_xmp = true
|
||||
block_size = 0 // Re-purpose the block_size variable as a counter.
|
||||
while block_size < 11 {
|
||||
c = args.src.read_u8?()
|
||||
is_animexts = is_animexts and (c == ANIMEXTS1DOT0[block_size])
|
||||
is_netscape = is_netscape and (c == NETSCAPE2DOT0[block_size])
|
||||
is_iccp = is_iccp and (c == ICCRGBG1012[block_size])
|
||||
is_xmp = is_xmp and (c == XMPDATAXMP[block_size])
|
||||
block_size += 1
|
||||
} endwhile
|
||||
|
||||
if is_animexts or is_netscape {
|
||||
// Those 11 bytes should be followed by 0x03, 0x01 and then the loop
|
||||
// count.
|
||||
block_size = args.src.read_u8?()
|
||||
if block_size <> 3 {
|
||||
args.src.skip_u32?(n: block_size as base.u32)
|
||||
break.goto_done
|
||||
}
|
||||
c = args.src.read_u8?()
|
||||
if c <> 0x01 {
|
||||
args.src.skip_u32?(n: 2)
|
||||
break.goto_done
|
||||
}
|
||||
this.num_animation_loops_value = args.src.read_u16le_as_u32?()
|
||||
this.seen_num_animation_loops_value = true
|
||||
if is_animexts or is_netscape {
|
||||
// Those 11 bytes should be followed by 0x03, 0x01 and then the loop
|
||||
// count.
|
||||
block_size = args.src.read_u8?()
|
||||
if block_size <> 3 {
|
||||
args.src.skip_u32?(n: block_size as base.u32)
|
||||
break.goto_done
|
||||
}
|
||||
c = args.src.read_u8?()
|
||||
if c <> 0x01 {
|
||||
args.src.skip_u32?(n: 2)
|
||||
break.goto_done
|
||||
}
|
||||
this.num_animation_loops_value = args.src.read_u16le_as_u32?()
|
||||
this.seen_num_animation_loops_value = true
|
||||
|
||||
// A loop count of N, in the wire format, actually means "repeat N
|
||||
// times after the first play", if N is positive. A zero N means to
|
||||
// loop forever. Playing the frames exactly once is denoted by the
|
||||
// *absence* of this NETSCAPE2.0 application extension.
|
||||
//
|
||||
// For example, if there are four frames: A, B, C, D, and N is 2, then
|
||||
// each frame is actually played N+1 or 3 times: ABCDABCDABCD.
|
||||
//
|
||||
// Thus, we increment N if it is positive. The comparison against
|
||||
// 0xFFFF will never fail, but is necessary for the overflow checker.
|
||||
if (0 < this.num_animation_loops_value) and (this.num_animation_loops_value <= 0xFFFF) {
|
||||
this.num_animation_loops_value += 1
|
||||
}
|
||||
// A loop count of N, in the wire format, actually means "repeat N
|
||||
// times after the first play", if N is positive. A zero N means to
|
||||
// loop forever. Playing the frames exactly once is denoted by the
|
||||
// *absence* of this NETSCAPE2.0 application extension.
|
||||
//
|
||||
// For example, if there are four frames: A, B, C, D, and N is 2, then
|
||||
// each frame is actually played N+1 or 3 times: ABCDABCDABCD.
|
||||
//
|
||||
// Thus, we increment N if it is positive. The comparison against
|
||||
// 0xFFFF will never fail, but is necessary for the overflow checker.
|
||||
if (0 < this.num_animation_loops_value) and (this.num_animation_loops_value <= 0xFFFF) {
|
||||
this.num_animation_loops_value += 1
|
||||
}
|
||||
|
||||
} else if this.call_sequence >= 0x20 {
|
||||
// No-op.
|
||||
} else if this.call_sequence >= 0x20 {
|
||||
// No-op.
|
||||
|
||||
} else if is_iccp and this.report_metadata_iccp {
|
||||
this.metadata_fourcc = 'ICCP'be
|
||||
this.metadata_io_position = args.src.position()
|
||||
this.call_sequence = 0x10
|
||||
return base."@metadata reported"
|
||||
} else if is_iccp and this.report_metadata_iccp {
|
||||
this.metadata_fourcc = 'ICCP'be
|
||||
this.metadata_io_position = args.src.position()
|
||||
this.call_sequence = 0x10
|
||||
return base."@metadata reported"
|
||||
|
||||
} else if is_xmp and this.report_metadata_xmp {
|
||||
this.metadata_fourcc = 'XMP 'be
|
||||
this.metadata_io_position = args.src.position()
|
||||
this.call_sequence = 0x10
|
||||
return base."@metadata reported"
|
||||
}
|
||||
} else if is_xmp and this.report_metadata_xmp {
|
||||
this.metadata_fourcc = 'XMP 'be
|
||||
this.metadata_io_position = args.src.position()
|
||||
this.call_sequence = 0x10
|
||||
return base."@metadata reported"
|
||||
}
|
||||
|
||||
break.goto_done
|
||||
}} endwhile.goto_done
|
||||
break.goto_done
|
||||
}} endwhile.goto_done
|
||||
|
||||
this.skip_blocks?(src: args.src)
|
||||
this.skip_blocks?(src: args.src)
|
||||
}
|
||||
|
||||
// decode_gc reads a Graphic Control.
|
||||
pri func decoder.decode_gc?(src: base.io_reader) {
|
||||
var c : base.u8
|
||||
var flags : base.u8
|
||||
var gc_duration_centiseconds : base.u16
|
||||
var c : base.u8
|
||||
var flags : base.u8
|
||||
var gc_duration_centiseconds : base.u16
|
||||
|
||||
c = args.src.read_u8?()
|
||||
if c <> 4 {
|
||||
return "#bad graphic control"
|
||||
}
|
||||
c = args.src.read_u8?()
|
||||
if c <> 4 {
|
||||
return "#bad graphic control"
|
||||
}
|
||||
|
||||
flags = args.src.read_u8?()
|
||||
this.gc_has_transparent_index = (flags & 0x01) <> 0
|
||||
flags = args.src.read_u8?()
|
||||
this.gc_has_transparent_index = (flags & 0x01) <> 0
|
||||
|
||||
// Convert the disposal method from GIF's wire format to Wuffs constants.
|
||||
//
|
||||
// The GIF spec discusses the 3-bit flag value being 0, 1, 2 or 3. Values
|
||||
// in the range [4 ..= 7] are "to be defined". In practice, some encoders also
|
||||
// use 4 for "restore previous". See
|
||||
// https://cs.chromium.org/chromium/src/third_party/blink/renderer/platform/image-decoders/gif/gif_image_reader.cc?rcl=5161173c43324da2b13e1aa45bbe69901daa1279&l=625
|
||||
flags = (flags >> 2) & 0x07
|
||||
if flags == 2 {
|
||||
this.gc_disposal = base.ANIMATION_DISPOSAL__RESTORE_BACKGROUND
|
||||
} else if (flags == 3) or (flags == 4) {
|
||||
this.gc_disposal = base.ANIMATION_DISPOSAL__RESTORE_PREVIOUS
|
||||
} else {
|
||||
this.gc_disposal = base.ANIMATION_DISPOSAL__NONE
|
||||
}
|
||||
// Convert the disposal method from GIF's wire format to Wuffs constants.
|
||||
//
|
||||
// The GIF spec discusses the 3-bit flag value being 0, 1, 2 or 3. Values
|
||||
// in the range [4 ..= 7] are "to be defined". In practice, some encoders also
|
||||
// use 4 for "restore previous". See
|
||||
// https://cs.chromium.org/chromium/src/third_party/blink/renderer/platform/image-decoders/gif/gif_image_reader.cc?rcl=5161173c43324da2b13e1aa45bbe69901daa1279&l=625
|
||||
flags = (flags >> 2) & 0x07
|
||||
if flags == 2 {
|
||||
this.gc_disposal = base.ANIMATION_DISPOSAL__RESTORE_BACKGROUND
|
||||
} else if (flags == 3) or (flags == 4) {
|
||||
this.gc_disposal = base.ANIMATION_DISPOSAL__RESTORE_PREVIOUS
|
||||
} else {
|
||||
this.gc_disposal = base.ANIMATION_DISPOSAL__NONE
|
||||
}
|
||||
|
||||
// There are 7_056000 flicks per centisecond.
|
||||
gc_duration_centiseconds = args.src.read_u16le?()
|
||||
this.gc_duration = (gc_duration_centiseconds as base.u64) * 7_056000
|
||||
this.gc_transparent_index = args.src.read_u8?()
|
||||
// There are 7_056000 flicks per centisecond.
|
||||
gc_duration_centiseconds = args.src.read_u16le?()
|
||||
this.gc_duration = (gc_duration_centiseconds as base.u64) * 7_056000
|
||||
this.gc_transparent_index = args.src.read_u8?()
|
||||
|
||||
c = args.src.read_u8?()
|
||||
if c <> 0 {
|
||||
return "#bad graphic control"
|
||||
}
|
||||
c = args.src.read_u8?()
|
||||
if c <> 0 {
|
||||
return "#bad graphic control"
|
||||
}
|
||||
}
|
||||
|
||||
// decode_id_partX reads an Image Descriptor. The Image Separator byte has
|
||||
@@ -783,368 +783,368 @@ pri func decoder.decode_gc?(src: base.io_reader) {
|
||||
// principle that smaller functions are easier to understand.
|
||||
|
||||
pri func decoder.decode_id_part0?(src: base.io_reader) {
|
||||
this.frame_rect_x0 = args.src.read_u16le_as_u32?()
|
||||
this.frame_rect_y0 = args.src.read_u16le_as_u32?()
|
||||
this.frame_rect_x1 = args.src.read_u16le_as_u32?()
|
||||
this.frame_rect_x1 ~mod+= this.frame_rect_x0
|
||||
this.frame_rect_y1 = args.src.read_u16le_as_u32?()
|
||||
this.frame_rect_y1 ~mod+= this.frame_rect_y0
|
||||
this.frame_rect_x0 = args.src.read_u16le_as_u32?()
|
||||
this.frame_rect_y0 = args.src.read_u16le_as_u32?()
|
||||
this.frame_rect_x1 = args.src.read_u16le_as_u32?()
|
||||
this.frame_rect_x1 ~mod+= this.frame_rect_x0
|
||||
this.frame_rect_y1 = args.src.read_u16le_as_u32?()
|
||||
this.frame_rect_y1 ~mod+= this.frame_rect_y0
|
||||
|
||||
this.dst_x = this.frame_rect_x0
|
||||
this.dst_y = this.frame_rect_y0
|
||||
this.dst_x = this.frame_rect_x0
|
||||
this.dst_y = this.frame_rect_y0
|
||||
|
||||
// Set the image's overall width and height to be the maximum of the
|
||||
// nominal image width and height (given in the Logical Screen Descriptor)
|
||||
// and the bottom right extent of the first frame. See
|
||||
// test/data/artificial/gif-frame-out-of-bounds.gif.make-artificial.txt for
|
||||
// more discussion.
|
||||
if (this.num_decoded_frame_configs_value == 0) and (not this.quirks[QUIRK_IMAGE_BOUNDS_ARE_STRICT - QUIRKS_BASE]) {
|
||||
this.width = this.width.max(a: this.frame_rect_x1)
|
||||
this.height = this.height.max(a: this.frame_rect_y1)
|
||||
}
|
||||
// Set the image's overall width and height to be the maximum of the
|
||||
// nominal image width and height (given in the Logical Screen Descriptor)
|
||||
// and the bottom right extent of the first frame. See
|
||||
// test/data/artificial/gif-frame-out-of-bounds.gif.make-artificial.txt for
|
||||
// more discussion.
|
||||
if (this.num_decoded_frame_configs_value == 0) and (not this.quirks[QUIRK_IMAGE_BOUNDS_ARE_STRICT - QUIRKS_BASE]) {
|
||||
this.width = this.width.max(a: this.frame_rect_x1)
|
||||
this.height = this.height.max(a: this.frame_rect_y1)
|
||||
}
|
||||
}
|
||||
|
||||
pri func decoder.decode_id_part1?(dst: ptr base.pixel_buffer, src: base.io_reader, blend: base.pixel_blend) {
|
||||
var flags : base.u8
|
||||
var which_palette : base.u8[..= 1]
|
||||
var num_palette_entries : base.u32[..= 256]
|
||||
var i : base.u32
|
||||
var argb : base.u32
|
||||
var status : base.status
|
||||
var lw : base.u8
|
||||
var flags : base.u8
|
||||
var which_palette : base.u8[..= 1]
|
||||
var num_palette_entries : base.u32[..= 256]
|
||||
var i : base.u32
|
||||
var argb : base.u32
|
||||
var status : base.status
|
||||
var lw : base.u8
|
||||
|
||||
flags = args.src.read_u8?()
|
||||
if (flags & 0x40) <> 0 {
|
||||
this.interlace = 4
|
||||
} else {
|
||||
this.interlace = 0
|
||||
}
|
||||
flags = args.src.read_u8?()
|
||||
if (flags & 0x40) <> 0 {
|
||||
this.interlace = 4
|
||||
} else {
|
||||
this.interlace = 0
|
||||
}
|
||||
|
||||
// Read the optional Local Color Table.
|
||||
which_palette = 1
|
||||
if (flags & 0x80) <> 0 {
|
||||
num_palette_entries = (1 as base.u32) << (1 + (flags & 0x07))
|
||||
i = 0
|
||||
while i < num_palette_entries {
|
||||
assert i < 256 via "a < b: a < c; c <= b"(c: num_palette_entries)
|
||||
// Convert from RGB (in memory order) to ARGB (in native u32 order)
|
||||
// to BGRA (in memory order).
|
||||
argb = args.src.read_u24be_as_u32?()
|
||||
argb |= 0xFF00_0000
|
||||
this.palettes[1][(4 * i) + 0] = ((argb >> 0) & 0xFF) as base.u8
|
||||
this.palettes[1][(4 * i) + 1] = ((argb >> 8) & 0xFF) as base.u8
|
||||
this.palettes[1][(4 * i) + 2] = ((argb >> 16) & 0xFF) as base.u8
|
||||
this.palettes[1][(4 * i) + 3] = ((argb >> 24) & 0xFF) as base.u8
|
||||
i += 1
|
||||
} endwhile
|
||||
// Set the remaining palette entries to opaque black.
|
||||
while i < 256 {
|
||||
this.palettes[1][(4 * i) + 0] = 0x00
|
||||
this.palettes[1][(4 * i) + 1] = 0x00
|
||||
this.palettes[1][(4 * i) + 2] = 0x00
|
||||
this.palettes[1][(4 * i) + 3] = 0xFF
|
||||
i += 1
|
||||
} endwhile
|
||||
} else if this.quirks[QUIRK_REJECT_EMPTY_PALETTE - QUIRKS_BASE] and (not this.has_global_palette) {
|
||||
return "#bad palette"
|
||||
} else if this.gc_has_transparent_index {
|
||||
this.palettes[1][..].copy_from_slice!(s: this.palettes[0][..])
|
||||
} else {
|
||||
which_palette = 0
|
||||
}
|
||||
// Read the optional Local Color Table.
|
||||
which_palette = 1
|
||||
if (flags & 0x80) <> 0 {
|
||||
num_palette_entries = (1 as base.u32) << (1 + (flags & 0x07))
|
||||
i = 0
|
||||
while i < num_palette_entries {
|
||||
assert i < 256 via "a < b: a < c; c <= b"(c: num_palette_entries)
|
||||
// Convert from RGB (in memory order) to ARGB (in native u32 order)
|
||||
// to BGRA (in memory order).
|
||||
argb = args.src.read_u24be_as_u32?()
|
||||
argb |= 0xFF00_0000
|
||||
this.palettes[1][(4 * i) + 0] = ((argb >> 0) & 0xFF) as base.u8
|
||||
this.palettes[1][(4 * i) + 1] = ((argb >> 8) & 0xFF) as base.u8
|
||||
this.palettes[1][(4 * i) + 2] = ((argb >> 16) & 0xFF) as base.u8
|
||||
this.palettes[1][(4 * i) + 3] = ((argb >> 24) & 0xFF) as base.u8
|
||||
i += 1
|
||||
} endwhile
|
||||
// Set the remaining palette entries to opaque black.
|
||||
while i < 256 {
|
||||
this.palettes[1][(4 * i) + 0] = 0x00
|
||||
this.palettes[1][(4 * i) + 1] = 0x00
|
||||
this.palettes[1][(4 * i) + 2] = 0x00
|
||||
this.palettes[1][(4 * i) + 3] = 0xFF
|
||||
i += 1
|
||||
} endwhile
|
||||
} else if this.quirks[QUIRK_REJECT_EMPTY_PALETTE - QUIRKS_BASE] and (not this.has_global_palette) {
|
||||
return "#bad palette"
|
||||
} else if this.gc_has_transparent_index {
|
||||
this.palettes[1][..].copy_from_slice!(s: this.palettes[0][..])
|
||||
} else {
|
||||
which_palette = 0
|
||||
}
|
||||
|
||||
// Set the gc_transparent_index palette entry to transparent black.
|
||||
if this.gc_has_transparent_index {
|
||||
this.palettes[1][(4 * (this.gc_transparent_index as base.u32)) + 0] = 0x00
|
||||
this.palettes[1][(4 * (this.gc_transparent_index as base.u32)) + 1] = 0x00
|
||||
this.palettes[1][(4 * (this.gc_transparent_index as base.u32)) + 2] = 0x00
|
||||
this.palettes[1][(4 * (this.gc_transparent_index as base.u32)) + 3] = 0x00
|
||||
}
|
||||
// Set the gc_transparent_index palette entry to transparent black.
|
||||
if this.gc_has_transparent_index {
|
||||
this.palettes[1][(4 * (this.gc_transparent_index as base.u32)) + 0] = 0x00
|
||||
this.palettes[1][(4 * (this.gc_transparent_index as base.u32)) + 1] = 0x00
|
||||
this.palettes[1][(4 * (this.gc_transparent_index as base.u32)) + 2] = 0x00
|
||||
this.palettes[1][(4 * (this.gc_transparent_index as base.u32)) + 3] = 0x00
|
||||
}
|
||||
|
||||
status = this.swizzler.prepare!(
|
||||
dst_pixfmt: args.dst.pixel_format(),
|
||||
dst_palette: args.dst.palette_or_else(fallback: this.dst_palette[..]),
|
||||
src_pixfmt: this.util.make_pixel_format(repr: base.PIXEL_FORMAT__INDEXED__BGRA_BINARY),
|
||||
src_palette: this.palettes[which_palette][..],
|
||||
blend: args.blend)
|
||||
if not status.is_ok() {
|
||||
return status
|
||||
}
|
||||
status = this.swizzler.prepare!(
|
||||
dst_pixfmt: args.dst.pixel_format(),
|
||||
dst_palette: args.dst.palette_or_else(fallback: this.dst_palette[..]),
|
||||
src_pixfmt: this.util.make_pixel_format(repr: base.PIXEL_FORMAT__INDEXED__BGRA_BINARY),
|
||||
src_palette: this.palettes[which_palette][..],
|
||||
blend: args.blend)
|
||||
if not status.is_ok() {
|
||||
return status
|
||||
}
|
||||
|
||||
// Other GIF implementations accept GIF files that aren't completely spec
|
||||
// compliant. For example, the test/data/gifplayer-muybridge.gif file
|
||||
// (created by the gifsicle program) is accepted by other GIF decoders.
|
||||
// However, in that file, frame #61's embedded LZW data is truncated,
|
||||
// finishing with only 8 of the 9 bits required of the LZW end code. The
|
||||
// end code itself, 0x81, is representable in only 8 bits, but the number
|
||||
// of bits for the decoder to read has ticked over from 8 to 9 just before
|
||||
// that end code is encountered.
|
||||
//
|
||||
// To accommodate such malformed GIFs, we detect when the previous frame's
|
||||
// LZW decoding ended abruptly. The previous LZW decode 'works', in that it
|
||||
// decodes as much pixel data as is available, but without seeing that end
|
||||
// code (i.e. returning the "ok" status code), the LZW decoder is stuck in
|
||||
// a coroutine-in-progress lzw_decoder.decode call, still waiting for that
|
||||
// end code. To cancel that coroutine, we reset the LZW decoder.
|
||||
if this.previous_lzw_decode_ended_abruptly {
|
||||
this.lzw.reset!()
|
||||
}
|
||||
// Other GIF implementations accept GIF files that aren't completely spec
|
||||
// compliant. For example, the test/data/gifplayer-muybridge.gif file
|
||||
// (created by the gifsicle program) is accepted by other GIF decoders.
|
||||
// However, in that file, frame #61's embedded LZW data is truncated,
|
||||
// finishing with only 8 of the 9 bits required of the LZW end code. The
|
||||
// end code itself, 0x81, is representable in only 8 bits, but the number
|
||||
// of bits for the decoder to read has ticked over from 8 to 9 just before
|
||||
// that end code is encountered.
|
||||
//
|
||||
// To accommodate such malformed GIFs, we detect when the previous frame's
|
||||
// LZW decoding ended abruptly. The previous LZW decode 'works', in that it
|
||||
// decodes as much pixel data as is available, but without seeing that end
|
||||
// code (i.e. returning the "ok" status code), the LZW decoder is stuck in
|
||||
// a coroutine-in-progress lzw_decoder.decode call, still waiting for that
|
||||
// end code. To cancel that coroutine, we reset the LZW decoder.
|
||||
if this.previous_lzw_decode_ended_abruptly {
|
||||
this.lzw.reset!()
|
||||
}
|
||||
|
||||
// Process the LZW literal width. The spec says that "images which have one
|
||||
// color bit must be indicated as having a code size [i.e. literal width]
|
||||
// of 2", but in practice, some encoders use a literal width of 1 or 0.
|
||||
lw = args.src.read_u8?()
|
||||
if lw > 8 {
|
||||
return "#bad literal width"
|
||||
}
|
||||
this.lzw.set_literal_width!(lw: lw as base.u32)
|
||||
// Process the LZW literal width. The spec says that "images which have one
|
||||
// color bit must be indicated as having a code size [i.e. literal width]
|
||||
// of 2", but in practice, some encoders use a literal width of 1 or 0.
|
||||
lw = args.src.read_u8?()
|
||||
if lw > 8 {
|
||||
return "#bad literal width"
|
||||
}
|
||||
this.lzw.set_literal_width!(lw: lw as base.u32)
|
||||
|
||||
this.previous_lzw_decode_ended_abruptly = true
|
||||
this.previous_lzw_decode_ended_abruptly = true
|
||||
}
|
||||
|
||||
pri func decoder.decode_id_part2?(dst: ptr base.pixel_buffer, src: base.io_reader, workbuf: slice base.u8) {
|
||||
var block_size : base.u64[..= 255]
|
||||
var need_block_size : base.bool
|
||||
var n_copied : base.u32
|
||||
var n_compressed : base.u64
|
||||
var r : base.io_reader
|
||||
var mark : base.u64
|
||||
var lzw_status : base.status
|
||||
var copy_status : base.status
|
||||
var uncompressed : slice base.u8
|
||||
var block_size : base.u64[..= 255]
|
||||
var need_block_size : base.bool
|
||||
var n_copied : base.u32
|
||||
var n_compressed : base.u64
|
||||
var r : base.io_reader
|
||||
var mark : base.u64
|
||||
var lzw_status : base.status
|
||||
var copy_status : base.status
|
||||
var uncompressed : slice base.u8
|
||||
|
||||
need_block_size = true
|
||||
while.outer true {
|
||||
if need_block_size {
|
||||
need_block_size = false
|
||||
block_size = args.src.read_u8_as_u64?()
|
||||
}
|
||||
if block_size == 0 {
|
||||
break.outer
|
||||
}
|
||||
while args.src.length() == 0 {
|
||||
yield? base."$short read"
|
||||
} endwhile
|
||||
need_block_size = true
|
||||
while.outer true {
|
||||
if need_block_size {
|
||||
need_block_size = false
|
||||
block_size = args.src.read_u8_as_u64?()
|
||||
}
|
||||
if block_size == 0 {
|
||||
break.outer
|
||||
}
|
||||
while args.src.length() == 0 {
|
||||
yield? base."$short read"
|
||||
} endwhile
|
||||
|
||||
if this.compressed_ri == this.compressed_wi {
|
||||
this.compressed_ri = 0
|
||||
this.compressed_wi = 0
|
||||
}
|
||||
while this.compressed_wi <= (4096 - 255) {
|
||||
n_compressed = block_size.min(a: args.src.length())
|
||||
if n_compressed <= 0 {
|
||||
break
|
||||
}
|
||||
n_copied = args.src.limited_copy_u32_to_slice!(
|
||||
up_to: (n_compressed & 0xFFFF_FFFF) as base.u32,
|
||||
s: this.compressed[this.compressed_wi ..])
|
||||
this.compressed_wi ~sat+= n_copied as base.u64
|
||||
block_size ~sat-= n_copied as base.u64
|
||||
if block_size > 0 {
|
||||
break
|
||||
}
|
||||
if args.src.length() <= 0 {
|
||||
need_block_size = true
|
||||
break
|
||||
}
|
||||
block_size = args.src.peek_u8_as_u64()
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
} endwhile
|
||||
if this.compressed_ri == this.compressed_wi {
|
||||
this.compressed_ri = 0
|
||||
this.compressed_wi = 0
|
||||
}
|
||||
while this.compressed_wi <= (4096 - 255) {
|
||||
n_compressed = block_size.min(a: args.src.length())
|
||||
if n_compressed <= 0 {
|
||||
break
|
||||
}
|
||||
n_copied = args.src.limited_copy_u32_to_slice!(
|
||||
up_to: (n_compressed & 0xFFFF_FFFF) as base.u32,
|
||||
s: this.compressed[this.compressed_wi ..])
|
||||
this.compressed_wi ~sat+= n_copied as base.u64
|
||||
block_size ~sat-= n_copied as base.u64
|
||||
if block_size > 0 {
|
||||
break
|
||||
}
|
||||
if args.src.length() <= 0 {
|
||||
need_block_size = true
|
||||
break
|
||||
}
|
||||
block_size = args.src.peek_u8_as_u64()
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
} endwhile
|
||||
|
||||
while.inner true {
|
||||
if (this.compressed_ri > this.compressed_wi) or (this.compressed_wi > 4096) {
|
||||
return "#internal error: inconsistent ri/wi"
|
||||
}
|
||||
io_bind (io: r, data: this.compressed[this.compressed_ri .. this.compressed_wi], history_position: 0) {
|
||||
mark = r.mark()
|
||||
lzw_status =? this.lzw.transform_io?(
|
||||
dst: this.util.empty_io_writer(), src: r, workbuf: this.util.empty_slice_u8())
|
||||
this.compressed_ri ~sat+= r.count_since(mark: mark)
|
||||
}
|
||||
while.inner true {
|
||||
if (this.compressed_ri > this.compressed_wi) or (this.compressed_wi > 4096) {
|
||||
return "#internal error: inconsistent ri/wi"
|
||||
}
|
||||
io_bind (io: r, data: this.compressed[this.compressed_ri .. this.compressed_wi], history_position: 0) {
|
||||
mark = r.mark()
|
||||
lzw_status =? this.lzw.transform_io?(
|
||||
dst: this.util.empty_io_writer(), src: r, workbuf: this.util.empty_slice_u8())
|
||||
this.compressed_ri ~sat+= r.count_since(mark: mark)
|
||||
}
|
||||
|
||||
uncompressed = this.lzw.flush!()
|
||||
if uncompressed.length() > 0 {
|
||||
copy_status = this.copy_to_image_buffer!(pb: args.dst, src: uncompressed)
|
||||
if copy_status.is_error() {
|
||||
return copy_status
|
||||
}
|
||||
}
|
||||
uncompressed = this.lzw.flush!()
|
||||
if uncompressed.length() > 0 {
|
||||
copy_status = this.copy_to_image_buffer!(pb: args.dst, src: uncompressed)
|
||||
if copy_status.is_error() {
|
||||
return copy_status
|
||||
}
|
||||
}
|
||||
|
||||
if lzw_status.is_ok() {
|
||||
this.previous_lzw_decode_ended_abruptly = false
|
||||
if lzw_status.is_ok() {
|
||||
this.previous_lzw_decode_ended_abruptly = false
|
||||
|
||||
// Skip any trailing blocks.
|
||||
if need_block_size or (block_size > 0) {
|
||||
args.src.skip_u32?(n: block_size as base.u32)
|
||||
this.skip_blocks?(src: args.src)
|
||||
}
|
||||
// Skip any trailing blocks.
|
||||
if need_block_size or (block_size > 0) {
|
||||
args.src.skip_u32?(n: block_size as base.u32)
|
||||
this.skip_blocks?(src: args.src)
|
||||
}
|
||||
|
||||
break.outer
|
||||
} else if lzw_status == base."$short read" {
|
||||
continue.outer
|
||||
} else if lzw_status == base."$short write" {
|
||||
continue.inner
|
||||
} else if this.quirks[QUIRK_IGNORE_TOO_MUCH_PIXEL_DATA - QUIRKS_BASE] and
|
||||
(this.dst_y >= this.frame_rect_y1) and (this.interlace == 0) {
|
||||
// It's invalid LZW-compressed data, but we still have a full
|
||||
// frame and have opted in to QUIRK_IGNORE_TOO_MUCH_PIXEL_DATA,
|
||||
// so treat it like the lzw_status.is_ok() case, other than not
|
||||
// clearing this.previous_lzw_decode_ended_abruptly.
|
||||
if need_block_size or (block_size > 0) {
|
||||
args.src.skip_u32?(n: block_size as base.u32)
|
||||
this.skip_blocks?(src: args.src)
|
||||
}
|
||||
break.outer
|
||||
}
|
||||
return lzw_status
|
||||
} endwhile.inner
|
||||
} endwhile.outer
|
||||
break.outer
|
||||
} else if lzw_status == base."$short read" {
|
||||
continue.outer
|
||||
} else if lzw_status == base."$short write" {
|
||||
continue.inner
|
||||
} else if this.quirks[QUIRK_IGNORE_TOO_MUCH_PIXEL_DATA - QUIRKS_BASE] and
|
||||
(this.dst_y >= this.frame_rect_y1) and (this.interlace == 0) {
|
||||
// It's invalid LZW-compressed data, but we still have a full
|
||||
// frame and have opted in to QUIRK_IGNORE_TOO_MUCH_PIXEL_DATA,
|
||||
// so treat it like the lzw_status.is_ok() case, other than not
|
||||
// clearing this.previous_lzw_decode_ended_abruptly.
|
||||
if need_block_size or (block_size > 0) {
|
||||
args.src.skip_u32?(n: block_size as base.u32)
|
||||
this.skip_blocks?(src: args.src)
|
||||
}
|
||||
break.outer
|
||||
}
|
||||
return lzw_status
|
||||
} endwhile.inner
|
||||
} endwhile.outer
|
||||
|
||||
this.compressed_ri = 0
|
||||
this.compressed_wi = 0
|
||||
this.compressed_ri = 0
|
||||
this.compressed_wi = 0
|
||||
|
||||
if (this.dst_y < this.frame_rect_y1) and
|
||||
(this.frame_rect_x0 <> this.frame_rect_x1) and
|
||||
(this.frame_rect_y0 <> this.frame_rect_y1) {
|
||||
return base."#not enough data"
|
||||
}
|
||||
if (this.dst_y < this.frame_rect_y1) and
|
||||
(this.frame_rect_x0 <> this.frame_rect_x1) and
|
||||
(this.frame_rect_y0 <> this.frame_rect_y1) {
|
||||
return base."#not enough data"
|
||||
}
|
||||
}
|
||||
|
||||
pri func decoder.copy_to_image_buffer!(pb: ptr base.pixel_buffer, src: slice base.u8) base.status {
|
||||
// TODO: don't assume an interleaved pixel format.
|
||||
var dst : slice base.u8
|
||||
var src : slice base.u8
|
||||
var width_in_bytes : base.u64
|
||||
var n : base.u64
|
||||
var src_ri : base.u64
|
||||
var pixfmt : base.pixel_format
|
||||
var bytes_per_pixel : base.u32[..= 32]
|
||||
var bits_per_pixel : base.u32[..= 256]
|
||||
var tab : table base.u8
|
||||
var i : base.u64
|
||||
var j : base.u64
|
||||
var replicate_y0 : base.u32
|
||||
var replicate_y1 : base.u32
|
||||
var replicate_dst : slice base.u8
|
||||
var replicate_src : slice base.u8
|
||||
// TODO: don't assume an interleaved pixel format.
|
||||
var dst : slice base.u8
|
||||
var src : slice base.u8
|
||||
var width_in_bytes : base.u64
|
||||
var n : base.u64
|
||||
var src_ri : base.u64
|
||||
var pixfmt : base.pixel_format
|
||||
var bytes_per_pixel : base.u32[..= 32]
|
||||
var bits_per_pixel : base.u32[..= 256]
|
||||
var tab : table base.u8
|
||||
var i : base.u64
|
||||
var j : base.u64
|
||||
var replicate_y0 : base.u32
|
||||
var replicate_y1 : base.u32
|
||||
var replicate_dst : slice base.u8
|
||||
var replicate_src : slice base.u8
|
||||
|
||||
// TODO: the pixfmt variable shouldn't be necessary. We should be able to
|
||||
// chain the two calls: "args.pb.pixel_format().bits_per_pixel()".
|
||||
pixfmt = args.pb.pixel_format()
|
||||
bits_per_pixel = pixfmt.bits_per_pixel()
|
||||
if (bits_per_pixel & 7) <> 0 {
|
||||
return base."#unsupported option"
|
||||
}
|
||||
bytes_per_pixel = bits_per_pixel >> 3
|
||||
// TODO: the pixfmt variable shouldn't be necessary. We should be able to
|
||||
// chain the two calls: "args.pb.pixel_format().bits_per_pixel()".
|
||||
pixfmt = args.pb.pixel_format()
|
||||
bits_per_pixel = pixfmt.bits_per_pixel()
|
||||
if (bits_per_pixel & 7) <> 0 {
|
||||
return base."#unsupported option"
|
||||
}
|
||||
bytes_per_pixel = bits_per_pixel >> 3
|
||||
|
||||
width_in_bytes = (this.width as base.u64) * (bytes_per_pixel as base.u64)
|
||||
tab = args.pb.plane(p: 0)
|
||||
while src_ri < args.src.length() {
|
||||
src = args.src[src_ri ..]
|
||||
width_in_bytes = (this.width as base.u64) * (bytes_per_pixel as base.u64)
|
||||
tab = args.pb.plane(p: 0)
|
||||
while src_ri < args.src.length() {
|
||||
src = args.src[src_ri ..]
|
||||
|
||||
if this.dst_y >= this.frame_rect_y1 {
|
||||
if this.quirks[QUIRK_IGNORE_TOO_MUCH_PIXEL_DATA - QUIRKS_BASE] {
|
||||
return ok
|
||||
}
|
||||
return base."#too much data"
|
||||
}
|
||||
if this.dst_y >= this.frame_rect_y1 {
|
||||
if this.quirks[QUIRK_IGNORE_TOO_MUCH_PIXEL_DATA - QUIRKS_BASE] {
|
||||
return ok
|
||||
}
|
||||
return base."#too much data"
|
||||
}
|
||||
|
||||
// First, copy from src to that part of the frame rect that is inside
|
||||
// args.pb's bounds (clipped to the image bounds).
|
||||
// First, copy from src to that part of the frame rect that is inside
|
||||
// args.pb's bounds (clipped to the image bounds).
|
||||
|
||||
dst = tab.row_u32(y: this.dst_y)
|
||||
if this.dst_y >= this.height {
|
||||
dst = dst[.. 0]
|
||||
} else if width_in_bytes < dst.length() {
|
||||
dst = dst[.. width_in_bytes]
|
||||
}
|
||||
dst = tab.row_u32(y: this.dst_y)
|
||||
if this.dst_y >= this.height {
|
||||
dst = dst[.. 0]
|
||||
} else if width_in_bytes < dst.length() {
|
||||
dst = dst[.. width_in_bytes]
|
||||
}
|
||||
|
||||
i = (this.dst_x as base.u64) * (bytes_per_pixel as base.u64)
|
||||
if i < dst.length() {
|
||||
j = (this.frame_rect_x1 as base.u64) * (bytes_per_pixel as base.u64)
|
||||
if (i <= j) and (j <= dst.length()) {
|
||||
dst = dst[i .. j]
|
||||
} else {
|
||||
dst = dst[i ..]
|
||||
}
|
||||
n = this.swizzler.swizzle_interleaved_from_slice!(
|
||||
dst: dst, dst_palette: this.dst_palette[..], src: src)
|
||||
i = (this.dst_x as base.u64) * (bytes_per_pixel as base.u64)
|
||||
if i < dst.length() {
|
||||
j = (this.frame_rect_x1 as base.u64) * (bytes_per_pixel as base.u64)
|
||||
if (i <= j) and (j <= dst.length()) {
|
||||
dst = dst[i .. j]
|
||||
} else {
|
||||
dst = dst[i ..]
|
||||
}
|
||||
n = this.swizzler.swizzle_interleaved_from_slice!(
|
||||
dst: dst, dst_palette: this.dst_palette[..], src: src)
|
||||
|
||||
src_ri ~sat+= n
|
||||
this.dst_x ~sat+= (n & 0xFFFF_FFFF) as base.u32
|
||||
src_ri ~sat+= n
|
||||
this.dst_x ~sat+= (n & 0xFFFF_FFFF) as base.u32
|
||||
|
||||
this.dirty_max_excl_y = this.dirty_max_excl_y.max(a: this.dst_y ~sat+ 1)
|
||||
}
|
||||
this.dirty_max_excl_y = this.dirty_max_excl_y.max(a: this.dst_y ~sat+ 1)
|
||||
}
|
||||
|
||||
if this.frame_rect_x1 <= this.dst_x {
|
||||
this.dst_x = this.frame_rect_x0
|
||||
if this.interlace == 0 {
|
||||
this.dst_y ~sat+= 1
|
||||
continue
|
||||
}
|
||||
if this.frame_rect_x1 <= this.dst_x {
|
||||
this.dst_x = this.frame_rect_x0
|
||||
if this.interlace == 0 {
|
||||
this.dst_y ~sat+= 1
|
||||
continue
|
||||
}
|
||||
|
||||
// For the first frame of an interlaced, non-transparent GIF,
|
||||
// replicate the early passes' rows. For example, when such an
|
||||
// image is downloaded over a slow network, this produces a richer
|
||||
// intermediate image while waiting for the complete image.
|
||||
//
|
||||
// Some other GIF implementations call this progressive display or
|
||||
// a "Haeberli inspired" technique.
|
||||
if (this.num_decoded_frames_value == 0) and
|
||||
(not this.gc_has_transparent_index) and
|
||||
(this.interlace > 1) {
|
||||
// For the first frame of an interlaced, non-transparent GIF,
|
||||
// replicate the early passes' rows. For example, when such an
|
||||
// image is downloaded over a slow network, this produces a richer
|
||||
// intermediate image while waiting for the complete image.
|
||||
//
|
||||
// Some other GIF implementations call this progressive display or
|
||||
// a "Haeberli inspired" technique.
|
||||
if (this.num_decoded_frames_value == 0) and
|
||||
(not this.gc_has_transparent_index) and
|
||||
(this.interlace > 1) {
|
||||
|
||||
replicate_src = tab.row_u32(y: this.dst_y)
|
||||
replicate_y0 = this.dst_y ~sat+ 1
|
||||
replicate_y1 = this.dst_y ~sat+ (INTERLACE_COUNT[this.interlace] as base.u32)
|
||||
replicate_y1 = replicate_y1.min(a: this.frame_rect_y1)
|
||||
while replicate_y0 < replicate_y1 {
|
||||
assert replicate_y0 < 0xFFFF_FFFF via "a < b: a < c; c <= b"(c: replicate_y1)
|
||||
replicate_dst = tab.row_u32(y: replicate_y0)
|
||||
replicate_dst.copy_from_slice!(s: replicate_src)
|
||||
replicate_y0 += 1
|
||||
} endwhile
|
||||
this.dirty_max_excl_y = this.dirty_max_excl_y.max(a: replicate_y1)
|
||||
}
|
||||
replicate_src = tab.row_u32(y: this.dst_y)
|
||||
replicate_y0 = this.dst_y ~sat+ 1
|
||||
replicate_y1 = this.dst_y ~sat+ (INTERLACE_COUNT[this.interlace] as base.u32)
|
||||
replicate_y1 = replicate_y1.min(a: this.frame_rect_y1)
|
||||
while replicate_y0 < replicate_y1 {
|
||||
assert replicate_y0 < 0xFFFF_FFFF via "a < b: a < c; c <= b"(c: replicate_y1)
|
||||
replicate_dst = tab.row_u32(y: replicate_y0)
|
||||
replicate_dst.copy_from_slice!(s: replicate_src)
|
||||
replicate_y0 += 1
|
||||
} endwhile
|
||||
this.dirty_max_excl_y = this.dirty_max_excl_y.max(a: replicate_y1)
|
||||
}
|
||||
|
||||
this.dst_y ~sat+= INTERLACE_DELTA[this.interlace] as base.u32
|
||||
while (this.interlace > 0) and (this.dst_y >= this.frame_rect_y1) {
|
||||
this.interlace -= 1
|
||||
this.dst_y = this.frame_rect_y0 ~sat+ INTERLACE_START[this.interlace]
|
||||
} endwhile
|
||||
continue
|
||||
}
|
||||
this.dst_y ~sat+= INTERLACE_DELTA[this.interlace] as base.u32
|
||||
while (this.interlace > 0) and (this.dst_y >= this.frame_rect_y1) {
|
||||
this.interlace -= 1
|
||||
this.dst_y = this.frame_rect_y0 ~sat+ INTERLACE_START[this.interlace]
|
||||
} endwhile
|
||||
continue
|
||||
}
|
||||
|
||||
if args.src.length() == src_ri {
|
||||
break
|
||||
} else if args.src.length() < src_ri {
|
||||
return "#internal error: inconsistent ri/wi"
|
||||
}
|
||||
if args.src.length() == src_ri {
|
||||
break
|
||||
} else if args.src.length() < src_ri {
|
||||
return "#internal error: inconsistent ri/wi"
|
||||
}
|
||||
|
||||
// Second, skip over src for that part of the frame rect that is
|
||||
// outside args.pb's bounds. This second step should be infrequent.
|
||||
// Second, skip over src for that part of the frame rect that is
|
||||
// outside args.pb's bounds. This second step should be infrequent.
|
||||
|
||||
// Set n to the number of pixels (i.e. the number of bytes) to skip.
|
||||
n = (this.frame_rect_x1 - this.dst_x) as base.u64
|
||||
n = n.min(a: args.src.length() - src_ri)
|
||||
// Set n to the number of pixels (i.e. the number of bytes) to skip.
|
||||
n = (this.frame_rect_x1 - this.dst_x) as base.u64
|
||||
n = n.min(a: args.src.length() - src_ri)
|
||||
|
||||
src_ri ~sat+= n
|
||||
this.dst_x ~sat+= (n & 0xFFFF_FFFF) as base.u32
|
||||
src_ri ~sat+= n
|
||||
this.dst_x ~sat+= (n & 0xFFFF_FFFF) as base.u32
|
||||
|
||||
if this.frame_rect_x1 <= this.dst_x {
|
||||
this.dst_x = this.frame_rect_x0
|
||||
this.dst_y ~sat+= INTERLACE_DELTA[this.interlace] as base.u32
|
||||
while (this.interlace > 0) and (this.dst_y >= this.frame_rect_y1) {
|
||||
this.interlace -= 1
|
||||
this.dst_y = this.frame_rect_y0 ~sat+ INTERLACE_START[this.interlace]
|
||||
} endwhile
|
||||
continue
|
||||
}
|
||||
if this.frame_rect_x1 <= this.dst_x {
|
||||
this.dst_x = this.frame_rect_x0
|
||||
this.dst_y ~sat+= INTERLACE_DELTA[this.interlace] as base.u32
|
||||
while (this.interlace > 0) and (this.dst_y >= this.frame_rect_y1) {
|
||||
this.interlace -= 1
|
||||
this.dst_y = this.frame_rect_y0 ~sat+ INTERLACE_START[this.interlace]
|
||||
} endwhile
|
||||
continue
|
||||
}
|
||||
|
||||
if src_ri <> args.src.length() {
|
||||
return "#internal error: inconsistent ri/wi"
|
||||
}
|
||||
break
|
||||
} endwhile
|
||||
return ok
|
||||
if src_ri <> args.src.length() {
|
||||
return "#internal error: inconsistent ri/wi"
|
||||
}
|
||||
break
|
||||
} endwhile
|
||||
return ok
|
||||
}
|
||||
+97
-97
@@ -25,123 +25,123 @@ pub status "#truncated input"
|
||||
pub const DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE : base.u64 = 1
|
||||
|
||||
pub struct decoder? implements base.io_transformer(
|
||||
ignore_checksum : base.bool,
|
||||
checksum : crc32.ieee_hasher,
|
||||
ignore_checksum : base.bool,
|
||||
checksum : crc32.ieee_hasher,
|
||||
|
||||
flate : deflate.decoder,
|
||||
flate : deflate.decoder,
|
||||
|
||||
util : base.utility,
|
||||
util : base.utility,
|
||||
)
|
||||
|
||||
pub func decoder.set_quirk_enabled!(quirk: base.u32, enabled: base.bool) {
|
||||
if args.quirk == base.QUIRK_IGNORE_CHECKSUM {
|
||||
this.ignore_checksum = args.enabled
|
||||
}
|
||||
if args.quirk == base.QUIRK_IGNORE_CHECKSUM {
|
||||
this.ignore_checksum = args.enabled
|
||||
}
|
||||
}
|
||||
|
||||
pub func decoder.workbuf_len() base.range_ii_u64 {
|
||||
return this.util.make_range_ii_u64(
|
||||
min_incl: DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE,
|
||||
max_incl: DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE)
|
||||
return this.util.make_range_ii_u64(
|
||||
min_incl: DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE,
|
||||
max_incl: DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE)
|
||||
}
|
||||
|
||||
pub func decoder.transform_io?(dst: base.io_writer, src: base.io_reader, workbuf: slice base.u8) {
|
||||
var status : base.status
|
||||
var status : base.status
|
||||
|
||||
while true {
|
||||
status =? this.do_transform_io?(dst: args.dst, src: args.src, workbuf: args.workbuf)
|
||||
if (status == base."$short read") and args.src.is_closed() {
|
||||
return "#truncated input"
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
while true {
|
||||
status =? this.do_transform_io?(dst: args.dst, src: args.src, workbuf: args.workbuf)
|
||||
if (status == base."$short read") and args.src.is_closed() {
|
||||
return "#truncated input"
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
}
|
||||
|
||||
pri func decoder.do_transform_io?(dst: base.io_writer, src: base.io_reader, workbuf: slice base.u8) {
|
||||
var c : base.u8
|
||||
var flags : base.u8
|
||||
var xlen : base.u16
|
||||
var mark : base.u64
|
||||
var checksum_got : base.u32
|
||||
var decoded_length_got : base.u32
|
||||
var status : base.status
|
||||
var checksum_want : base.u32
|
||||
var decoded_length_want : base.u32
|
||||
var c : base.u8
|
||||
var flags : base.u8
|
||||
var xlen : base.u16
|
||||
var mark : base.u64
|
||||
var checksum_got : base.u32
|
||||
var decoded_length_got : base.u32
|
||||
var status : base.status
|
||||
var checksum_want : base.u32
|
||||
var decoded_length_want : base.u32
|
||||
|
||||
// Read the header.
|
||||
c = args.src.read_u8?()
|
||||
if c <> 0x1F {
|
||||
return "#bad header"
|
||||
}
|
||||
c = args.src.read_u8?()
|
||||
if c <> 0x8B {
|
||||
return "#bad header"
|
||||
}
|
||||
c = args.src.read_u8?()
|
||||
if c <> 0x08 {
|
||||
return "#bad compression method"
|
||||
}
|
||||
flags = args.src.read_u8?()
|
||||
// TODO: API for returning the header's MTIME field.
|
||||
args.src.skip_u32?(n: 6)
|
||||
// Read the header.
|
||||
c = args.src.read_u8?()
|
||||
if c <> 0x1F {
|
||||
return "#bad header"
|
||||
}
|
||||
c = args.src.read_u8?()
|
||||
if c <> 0x8B {
|
||||
return "#bad header"
|
||||
}
|
||||
c = args.src.read_u8?()
|
||||
if c <> 0x08 {
|
||||
return "#bad compression method"
|
||||
}
|
||||
flags = args.src.read_u8?()
|
||||
// TODO: API for returning the header's MTIME field.
|
||||
args.src.skip_u32?(n: 6)
|
||||
|
||||
// Handle FEXTRA.
|
||||
if (flags & 0x04) <> 0 {
|
||||
xlen = args.src.read_u16le?()
|
||||
args.src.skip_u32?(n: xlen as base.u32)
|
||||
}
|
||||
// Handle FEXTRA.
|
||||
if (flags & 0x04) <> 0 {
|
||||
xlen = args.src.read_u16le?()
|
||||
args.src.skip_u32?(n: xlen as base.u32)
|
||||
}
|
||||
|
||||
// Handle FNAME.
|
||||
//
|
||||
// TODO: API for returning the header's FNAME field. This might require
|
||||
// converting ISO 8859-1 to UTF-8. We may also want to cap the UTF-8
|
||||
// filename length to NAME_MAX, which is 255.
|
||||
if (flags & 0x08) <> 0 {
|
||||
while true {
|
||||
c = args.src.read_u8?()
|
||||
if c == 0 {
|
||||
break
|
||||
}
|
||||
} endwhile
|
||||
}
|
||||
// Handle FNAME.
|
||||
//
|
||||
// TODO: API for returning the header's FNAME field. This might require
|
||||
// converting ISO 8859-1 to UTF-8. We may also want to cap the UTF-8
|
||||
// filename length to NAME_MAX, which is 255.
|
||||
if (flags & 0x08) <> 0 {
|
||||
while true {
|
||||
c = args.src.read_u8?()
|
||||
if c == 0 {
|
||||
break
|
||||
}
|
||||
} endwhile
|
||||
}
|
||||
|
||||
// Handle FCOMMENT.
|
||||
if (flags & 0x10) <> 0 {
|
||||
while true {
|
||||
c = args.src.read_u8?()
|
||||
if c == 0 {
|
||||
break
|
||||
}
|
||||
} endwhile
|
||||
}
|
||||
// Handle FCOMMENT.
|
||||
if (flags & 0x10) <> 0 {
|
||||
while true {
|
||||
c = args.src.read_u8?()
|
||||
if c == 0 {
|
||||
break
|
||||
}
|
||||
} endwhile
|
||||
}
|
||||
|
||||
// Handle FHCRC.
|
||||
if (flags & 0x02) <> 0 {
|
||||
args.src.skip_u32?(n: 2)
|
||||
}
|
||||
// Handle FHCRC.
|
||||
if (flags & 0x02) <> 0 {
|
||||
args.src.skip_u32?(n: 2)
|
||||
}
|
||||
|
||||
// Reserved flags bits must be zero.
|
||||
if (flags & 0xE0) <> 0 {
|
||||
return "#bad encoding flags"
|
||||
}
|
||||
// Reserved flags bits must be zero.
|
||||
if (flags & 0xE0) <> 0 {
|
||||
return "#bad encoding flags"
|
||||
}
|
||||
|
||||
// Decode and checksum the DEFLATE-encoded payload.
|
||||
while true {
|
||||
mark = args.dst.mark()
|
||||
status =? this.flate.transform_io?(dst: args.dst, src: args.src, workbuf: args.workbuf)
|
||||
if not this.ignore_checksum {
|
||||
checksum_got = this.checksum.update_u32!(x: args.dst.since(mark: mark))
|
||||
decoded_length_got ~mod+= (args.dst.count_since(mark: mark) & 0xFFFF_FFFF) as base.u32
|
||||
}
|
||||
if status.is_ok() {
|
||||
break
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
checksum_want = args.src.read_u32le?()
|
||||
decoded_length_want = args.src.read_u32le?()
|
||||
if (not this.ignore_checksum) and
|
||||
((checksum_got <> checksum_want) or (decoded_length_got <> decoded_length_want)) {
|
||||
return "#bad checksum"
|
||||
}
|
||||
// Decode and checksum the DEFLATE-encoded payload.
|
||||
while true {
|
||||
mark = args.dst.mark()
|
||||
status =? this.flate.transform_io?(dst: args.dst, src: args.src, workbuf: args.workbuf)
|
||||
if not this.ignore_checksum {
|
||||
checksum_got = this.checksum.update_u32!(x: args.dst.since(mark: mark))
|
||||
decoded_length_got ~mod+= (args.dst.count_since(mark: mark) & 0xFFFF_FFFF) as base.u32
|
||||
}
|
||||
if status.is_ok() {
|
||||
break
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
checksum_want = args.src.read_u32le?()
|
||||
decoded_length_want = args.src.read_u32le?()
|
||||
if (not this.ignore_checksum) and
|
||||
((checksum_got <> checksum_want) or (decoded_length_got <> decoded_length_want)) {
|
||||
return "#bad checksum"
|
||||
}
|
||||
}
|
||||
+192
-192
@@ -85,69 +85,69 @@ pri const DECODER_NUMBER_LENGTH_MAX_INCL : base.u64 = 99
|
||||
// If the element is zero then "\i" is invalid, or it is a special case, the
|
||||
// start of "\x12", "\u1234" or "\U12345678".
|
||||
pri const LUT_BACKSLASHES : array[256] base.u8 = [
|
||||
// 0 1 2 3 4 5 6 7
|
||||
// 8 9 A B C D E F
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x00 ..= 0x07.
|
||||
0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x08 ..= 0x0F. '\n'.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x10 ..= 0x17.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x18 ..= 0x1F.
|
||||
0x00, 0x00, 0xA2, 0x00, 0x00, 0x00, 0x00, 0x05, // 0x20 ..= 0x27. '"', '\''.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xAF, // 0x28 ..= 0x2F. '/'.
|
||||
0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x30 ..= 0x37. '0'.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, // 0x38 ..= 0x3F. '?'
|
||||
// 0 1 2 3 4 5 6 7
|
||||
// 8 9 A B C D E F
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x00 ..= 0x07.
|
||||
0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x08 ..= 0x0F. '\n'.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x10 ..= 0x17.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x18 ..= 0x1F.
|
||||
0x00, 0x00, 0xA2, 0x00, 0x00, 0x00, 0x00, 0x05, // 0x20 ..= 0x27. '"', '\''.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xAF, // 0x28 ..= 0x2F. '/'.
|
||||
0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x30 ..= 0x37. '0'.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, // 0x38 ..= 0x3F. '?'
|
||||
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x40 ..= 0x47.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x48 ..= 0x4F.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x50 ..= 0x57.
|
||||
0x00, 0x00, 0x00, 0x00, 0xDC, 0x00, 0x00, 0x00, // 0x58 ..= 0x5F. '\\'.
|
||||
0x00, 0x01, 0x88, 0x00, 0x00, 0x02, 0x8C, 0x00, // 0x60 ..= 0x67. 'a', 'b', 'e', 'f'.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8A, 0x00, // 0x68 ..= 0x6F. 'n'.
|
||||
0x00, 0x00, 0x8D, 0x00, 0x89, 0x00, 0x06, 0x00, // 0x70 ..= 0x77. 'r', 't', 'v'.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x78 ..= 0x7F.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x40 ..= 0x47.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x48 ..= 0x4F.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x50 ..= 0x57.
|
||||
0x00, 0x00, 0x00, 0x00, 0xDC, 0x00, 0x00, 0x00, // 0x58 ..= 0x5F. '\\'.
|
||||
0x00, 0x01, 0x88, 0x00, 0x00, 0x02, 0x8C, 0x00, // 0x60 ..= 0x67. 'a', 'b', 'e', 'f'.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8A, 0x00, // 0x68 ..= 0x6F. 'n'.
|
||||
0x00, 0x00, 0x8D, 0x00, 0x89, 0x00, 0x06, 0x00, // 0x70 ..= 0x77. 'r', 't', 'v'.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x78 ..= 0x7F.
|
||||
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x80 ..= 0x87.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x88 ..= 0x8F.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x90 ..= 0x97.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x98 ..= 0x9F.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xA0 ..= 0xA7.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xA8 ..= 0xAF.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xB0 ..= 0xB7.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xB8 ..= 0xBF.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x80 ..= 0x87.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x88 ..= 0x8F.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x90 ..= 0x97.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x98 ..= 0x9F.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xA0 ..= 0xA7.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xA8 ..= 0xAF.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xB0 ..= 0xB7.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xB8 ..= 0xBF.
|
||||
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xC0 ..= 0xC7.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xC8 ..= 0xCF.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xD0 ..= 0xD7.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xD8 ..= 0xDF.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xE0 ..= 0xE7.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xE8 ..= 0xEF.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xF0 ..= 0xF7.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xF8 ..= 0xFF.
|
||||
// 0 1 2 3 4 5 6 7
|
||||
// 8 9 A B C D E F
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xC0 ..= 0xC7.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xC8 ..= 0xCF.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xD0 ..= 0xD7.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xD8 ..= 0xDF.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xE0 ..= 0xE7.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xE8 ..= 0xEF.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xF0 ..= 0xF7.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xF8 ..= 0xFF.
|
||||
// 0 1 2 3 4 5 6 7
|
||||
// 8 9 A B C D E F
|
||||
]
|
||||
|
||||
// LUT_QUIRKY_BACKSLASHES_QUIRKS is discussed in the LUT_BACKSLASHES comment.
|
||||
// The first element (index 0) is not used, but 8 is a round power of 2, so
|
||||
// enforcing index-in-bounds is a simple "&7" operation.
|
||||
pri const LUT_QUIRKY_BACKSLASHES_QUIRKS : array[8] base.u8[..= 0x0A] = [
|
||||
0,
|
||||
// Uncommenting the following lines requires being able to define consts in
|
||||
// terms of other consts, which could be non-trivial to specify and
|
||||
// implement (to avoid infinite loops).
|
||||
0x01, // (QUIRK_ALLOW_BACKSLASH_A - QUIRKS_BASE) as base.u8,
|
||||
0x03, // (QUIRK_ALLOW_BACKSLASH_E - QUIRKS_BASE) as base.u8,
|
||||
0x04, // (QUIRK_ALLOW_BACKSLASH_NEW_LINE - QUIRKS_BASE) as base.u8,
|
||||
0x05, // (QUIRK_ALLOW_BACKSLASH_QUESTION_MARK - QUIRKS_BASE) as base.u8,
|
||||
0x06, // (QUIRK_ALLOW_BACKSLASH_SINGLE_QUOTE - QUIRKS_BASE) as base.u8,
|
||||
0x07, // (QUIRK_ALLOW_BACKSLASH_V - QUIRKS_BASE) as base.u8,
|
||||
0x0A, // (QUIRK_ALLOW_BACKSLASH_ZERO - QUIRKS_BASE) as base.u8,
|
||||
0,
|
||||
// Uncommenting the following lines requires being able to define consts in
|
||||
// terms of other consts, which could be non-trivial to specify and
|
||||
// implement (to avoid infinite loops).
|
||||
0x01, // (QUIRK_ALLOW_BACKSLASH_A - QUIRKS_BASE) as base.u8,
|
||||
0x03, // (QUIRK_ALLOW_BACKSLASH_E - QUIRKS_BASE) as base.u8,
|
||||
0x04, // (QUIRK_ALLOW_BACKSLASH_NEW_LINE - QUIRKS_BASE) as base.u8,
|
||||
0x05, // (QUIRK_ALLOW_BACKSLASH_QUESTION_MARK - QUIRKS_BASE) as base.u8,
|
||||
0x06, // (QUIRK_ALLOW_BACKSLASH_SINGLE_QUOTE - QUIRKS_BASE) as base.u8,
|
||||
0x07, // (QUIRK_ALLOW_BACKSLASH_V - QUIRKS_BASE) as base.u8,
|
||||
0x0A, // (QUIRK_ALLOW_BACKSLASH_ZERO - QUIRKS_BASE) as base.u8,
|
||||
]
|
||||
|
||||
// LUT_QUIRKY_BACKSLASHES_CHARS is discussed in the LUT_BACKSLASHES comment.
|
||||
// The first element (index 0) is not used, but 8 is a round power of 2, so
|
||||
// enforcing index-in-bounds is a simple "&7" operation.
|
||||
pri const LUT_QUIRKY_BACKSLASHES_CHARS : array[8] base.u8 = [
|
||||
0x00, 0x07, 0x1B, 0x0A, 0x3F, 0x27, 0x0B, 0x00,
|
||||
0x00, 0x07, 0x1B, 0x0A, 0x3F, 0x27, 0x0B, 0x00,
|
||||
]
|
||||
|
||||
// LUT_CHARS helps decode bytes within a string:
|
||||
@@ -170,45 +170,45 @@ pri const LUT_QUIRKY_BACKSLASHES_CHARS : array[8] base.u8 = [
|
||||
// %xF4 %x80-8F 2( UTF8-tail )
|
||||
// UTF8-tail = %x80-BF
|
||||
pri const LUT_CHARS : array[256] base.u8 = [
|
||||
// 0 1 2 3 4 5 6 7
|
||||
// 8 9 A B C D E F
|
||||
0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, // 0x00 ..= 0x07. C0 control codes.
|
||||
0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F, // 0x08 ..= 0x0F. C0 control codes.
|
||||
0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, // 0x10 ..= 0x17. C0 control codes.
|
||||
0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F, // 0x18 ..= 0x1F. C0 control codes.
|
||||
0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x20 ..= 0x27. UTF-8-1; '"'.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x28 ..= 0x2F. UTF-8-1.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x30 ..= 0x37. UTF-8-1.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x38 ..= 0x3F. UTF-8-1.
|
||||
// 0 1 2 3 4 5 6 7
|
||||
// 8 9 A B C D E F
|
||||
0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, // 0x00 ..= 0x07. C0 control codes.
|
||||
0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F, // 0x08 ..= 0x0F. C0 control codes.
|
||||
0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, // 0x10 ..= 0x17. C0 control codes.
|
||||
0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F, // 0x18 ..= 0x1F. C0 control codes.
|
||||
0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x20 ..= 0x27. UTF-8-1; '"'.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x28 ..= 0x2F. UTF-8-1.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x30 ..= 0x37. UTF-8-1.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x38 ..= 0x3F. UTF-8-1.
|
||||
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x40 ..= 0x47. UTF-8-1.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x48 ..= 0x4F. UTF-8-1.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x50 ..= 0x57. UTF-8-1.
|
||||
0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, // 0x58 ..= 0x5F. UTF-8-1; '\\'.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x60 ..= 0x67. UTF-8-1.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x68 ..= 0x6F. UTF-8-1.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x70 ..= 0x77. UTF-8-1.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x78 ..= 0x7F. UTF-8-1.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x40 ..= 0x47. UTF-8-1.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x48 ..= 0x4F. UTF-8-1.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x50 ..= 0x57. UTF-8-1.
|
||||
0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, // 0x58 ..= 0x5F. UTF-8-1; '\\'.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x60 ..= 0x67. UTF-8-1.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x68 ..= 0x6F. UTF-8-1.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x70 ..= 0x77. UTF-8-1.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x78 ..= 0x7F. UTF-8-1.
|
||||
|
||||
0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, // 0x80 ..= 0x87. UTF-8 tail.
|
||||
0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, // 0x88 ..= 0x8F. UTF-8 tail.
|
||||
0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, // 0x90 ..= 0x97. UTF-8 tail.
|
||||
0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, // 0x98 ..= 0x9F. UTF-8 tail.
|
||||
0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, // 0xA0 ..= 0xA7. UTF-8 tail.
|
||||
0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, // 0xA8 ..= 0xAF. UTF-8 tail.
|
||||
0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, // 0xB0 ..= 0xB7. UTF-8 tail.
|
||||
0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, // 0xB8 ..= 0xBF. UTF-8 tail.
|
||||
0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, // 0x80 ..= 0x87. UTF-8 tail.
|
||||
0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, // 0x88 ..= 0x8F. UTF-8 tail.
|
||||
0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, // 0x90 ..= 0x97. UTF-8 tail.
|
||||
0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, // 0x98 ..= 0x9F. UTF-8 tail.
|
||||
0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, // 0xA0 ..= 0xA7. UTF-8 tail.
|
||||
0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, // 0xA8 ..= 0xAF. UTF-8 tail.
|
||||
0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, // 0xB0 ..= 0xB7. UTF-8 tail.
|
||||
0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, // 0xB8 ..= 0xBF. UTF-8 tail.
|
||||
|
||||
0x20, 0x20, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, // 0xC0 ..= 0xC7. Invalid UTF-8; UTF-8-2.
|
||||
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, // 0xC8 ..= 0xCF. UTF-8-2.
|
||||
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, // 0xD0 ..= 0xD7. UTF-8-2.
|
||||
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, // 0xD8 ..= 0xDF. UTF-8-2.
|
||||
0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, // 0xE0 ..= 0xE7. UTF-8-3.
|
||||
0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, // 0xE8 ..= 0xEF. UTF-8-3.
|
||||
0x05, 0x05, 0x05, 0x05, 0x05, 0x20, 0x20, 0x20, // 0xF0 ..= 0xF7. UTF-8-4; Invalid UTF-8.
|
||||
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, // 0xF8 ..= 0xFF. Invalid UTF-8.
|
||||
// 0 1 2 3 4 5 6 7
|
||||
// 8 9 A B C D E F
|
||||
0x20, 0x20, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, // 0xC0 ..= 0xC7. Invalid UTF-8; UTF-8-2.
|
||||
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, // 0xC8 ..= 0xCF. UTF-8-2.
|
||||
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, // 0xD0 ..= 0xD7. UTF-8-2.
|
||||
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, // 0xD8 ..= 0xDF. UTF-8-2.
|
||||
0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, // 0xE0 ..= 0xE7. UTF-8-3.
|
||||
0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, // 0xE8 ..= 0xEF. UTF-8-3.
|
||||
0x05, 0x05, 0x05, 0x05, 0x05, 0x20, 0x20, 0x20, // 0xF0 ..= 0xF7. UTF-8-4; Invalid UTF-8.
|
||||
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, // 0xF8 ..= 0xFF. Invalid UTF-8.
|
||||
// 0 1 2 3 4 5 6 7
|
||||
// 8 9 A B C D E F
|
||||
]
|
||||
|
||||
pri const CLASS_WHITESPACE : base.u8 = 0x00
|
||||
@@ -270,127 +270,127 @@ pri const EXPECT_CLOSE_SQUARE_BRACKET : base.u32 = 0x1100
|
||||
// The bitmasks are used by the "expect" variable: what the next character
|
||||
// class can be. See EXPECT_ETC above.
|
||||
pri const LUT_CLASSES : array[256] base.u8[..= 0x0F] = [
|
||||
// 0 1 2 3 4 5 6 7
|
||||
// 8 9 A B C D E F
|
||||
0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, // 0x00 ..= 0x07.
|
||||
0x0F, 0x00, 0x00, 0x0F, 0x0F, 0x00, 0x0F, 0x0F, // 0x08 ..= 0x0F. '\t', '\n', '\r'.
|
||||
0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, // 0x10 ..= 0x17.
|
||||
0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, // 0x18 ..= 0x1F.
|
||||
0x00, 0x0F, 0x01, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, // 0x20 ..= 0x27. ' ', '"'.
|
||||
0x0F, 0x0F, 0x0F, 0x0B, 0x02, 0x04, 0x0F, 0x0C, // 0x28 ..= 0x2F. '+', ',', '-', '/'.
|
||||
0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, // 0x30 ..= 0x37. '0'-'7'.
|
||||
0x04, 0x04, 0x03, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, // 0x38 ..= 0x3F. '8'-'9', ':'.
|
||||
// 0 1 2 3 4 5 6 7
|
||||
// 8 9 A B C D E F
|
||||
0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, // 0x00 ..= 0x07.
|
||||
0x0F, 0x00, 0x00, 0x0F, 0x0F, 0x00, 0x0F, 0x0F, // 0x08 ..= 0x0F. '\t', '\n', '\r'.
|
||||
0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, // 0x10 ..= 0x17.
|
||||
0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, // 0x18 ..= 0x1F.
|
||||
0x00, 0x0F, 0x01, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, // 0x20 ..= 0x27. ' ', '"'.
|
||||
0x0F, 0x0F, 0x0F, 0x0B, 0x02, 0x04, 0x0F, 0x0C, // 0x28 ..= 0x2F. '+', ',', '-', '/'.
|
||||
0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, // 0x30 ..= 0x37. '0'-'7'.
|
||||
0x04, 0x04, 0x03, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, // 0x38 ..= 0x3F. '8'-'9', ':'.
|
||||
|
||||
0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, // 0x40 ..= 0x47.
|
||||
0x0F, 0x0B, 0x0F, 0x0F, 0x0F, 0x0F, 0x0B, 0x0F, // 0x48 ..= 0x4F. 'I', 'N'.
|
||||
0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, // 0x50 ..= 0x57.
|
||||
0x0F, 0x0F, 0x0F, 0x07, 0x0F, 0x08, 0x0F, 0x0F, // 0x58 ..= 0x5F. '[', ']'.
|
||||
0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x09, 0x0F, // 0x60 ..= 0x67. 'f'.
|
||||
0x0F, 0x0B, 0x0F, 0x0F, 0x0F, 0x0F, 0x0B, 0x0F, // 0x68 ..= 0x6F. 'i', 'n'.
|
||||
0x0F, 0x0F, 0x0F, 0x0F, 0x0A, 0x0F, 0x0F, 0x0F, // 0x70 ..= 0x77. 't'.
|
||||
0x0F, 0x0F, 0x0F, 0x05, 0x0F, 0x06, 0x0F, 0x0F, // 0x78 ..= 0x7F. '{', '}'.
|
||||
0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, // 0x40 ..= 0x47.
|
||||
0x0F, 0x0B, 0x0F, 0x0F, 0x0F, 0x0F, 0x0B, 0x0F, // 0x48 ..= 0x4F. 'I', 'N'.
|
||||
0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, // 0x50 ..= 0x57.
|
||||
0x0F, 0x0F, 0x0F, 0x07, 0x0F, 0x08, 0x0F, 0x0F, // 0x58 ..= 0x5F. '[', ']'.
|
||||
0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x09, 0x0F, // 0x60 ..= 0x67. 'f'.
|
||||
0x0F, 0x0B, 0x0F, 0x0F, 0x0F, 0x0F, 0x0B, 0x0F, // 0x68 ..= 0x6F. 'i', 'n'.
|
||||
0x0F, 0x0F, 0x0F, 0x0F, 0x0A, 0x0F, 0x0F, 0x0F, // 0x70 ..= 0x77. 't'.
|
||||
0x0F, 0x0F, 0x0F, 0x05, 0x0F, 0x06, 0x0F, 0x0F, // 0x78 ..= 0x7F. '{', '}'.
|
||||
|
||||
0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, // 0x80 ..= 0x87.
|
||||
0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, // 0x88 ..= 0x8F.
|
||||
0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, // 0x90 ..= 0x97.
|
||||
0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, // 0x98 ..= 0x9F.
|
||||
0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, // 0xA0 ..= 0xA7.
|
||||
0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, // 0xA8 ..= 0xAF.
|
||||
0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, // 0xB0 ..= 0xB7.
|
||||
0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, // 0xB8 ..= 0xBF.
|
||||
0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, // 0x80 ..= 0x87.
|
||||
0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, // 0x88 ..= 0x8F.
|
||||
0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, // 0x90 ..= 0x97.
|
||||
0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, // 0x98 ..= 0x9F.
|
||||
0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, // 0xA0 ..= 0xA7.
|
||||
0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, // 0xA8 ..= 0xAF.
|
||||
0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, // 0xB0 ..= 0xB7.
|
||||
0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, // 0xB8 ..= 0xBF.
|
||||
|
||||
0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, // 0xC0 ..= 0xC7.
|
||||
0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, // 0xC8 ..= 0xCF.
|
||||
0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, // 0xD0 ..= 0xD7.
|
||||
0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, // 0xD8 ..= 0xDF.
|
||||
0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, // 0xE0 ..= 0xE7.
|
||||
0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, // 0xE8 ..= 0xEF.
|
||||
0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, // 0xF0 ..= 0xF7.
|
||||
0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, // 0xF8 ..= 0xFF.
|
||||
// 0 1 2 3 4 5 6 7
|
||||
// 8 9 A B C D E F
|
||||
0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, // 0xC0 ..= 0xC7.
|
||||
0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, // 0xC8 ..= 0xCF.
|
||||
0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, // 0xD0 ..= 0xD7.
|
||||
0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, // 0xD8 ..= 0xDF.
|
||||
0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, // 0xE0 ..= 0xE7.
|
||||
0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, // 0xE8 ..= 0xEF.
|
||||
0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, // 0xF0 ..= 0xF7.
|
||||
0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, // 0xF8 ..= 0xFF.
|
||||
// 0 1 2 3 4 5 6 7
|
||||
// 8 9 A B C D E F
|
||||
]
|
||||
|
||||
pri const LUT_DECIMAL_DIGITS : array[256] base.u8 = [
|
||||
// 0 1 2 3 4 5 6 7
|
||||
// 8 9 A B C D E F
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x00 ..= 0x07.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x08 ..= 0x0F.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x10 ..= 0x17.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x18 ..= 0x1F.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x20 ..= 0x27.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x28 ..= 0x2F.
|
||||
0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, // 0x30 ..= 0x37. '0'-'7'.
|
||||
0x88, 0x89, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x38 ..= 0x3F. '8'-'9'.
|
||||
// 0 1 2 3 4 5 6 7
|
||||
// 8 9 A B C D E F
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x00 ..= 0x07.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x08 ..= 0x0F.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x10 ..= 0x17.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x18 ..= 0x1F.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x20 ..= 0x27.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x28 ..= 0x2F.
|
||||
0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, // 0x30 ..= 0x37. '0'-'7'.
|
||||
0x88, 0x89, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x38 ..= 0x3F. '8'-'9'.
|
||||
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x40 ..= 0x47.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x48 ..= 0x4F.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x50 ..= 0x57.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x58 ..= 0x5F.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x60 ..= 0x67.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x68 ..= 0x6F.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x70 ..= 0x77.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x78 ..= 0x7F.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x40 ..= 0x47.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x48 ..= 0x4F.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x50 ..= 0x57.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x58 ..= 0x5F.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x60 ..= 0x67.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x68 ..= 0x6F.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x70 ..= 0x77.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x78 ..= 0x7F.
|
||||
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x80 ..= 0x87.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x88 ..= 0x8F.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x90 ..= 0x97.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x98 ..= 0x9F.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xA0 ..= 0xA7.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xA8 ..= 0xAF.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xB0 ..= 0xB7.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xB8 ..= 0xBF.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x80 ..= 0x87.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x88 ..= 0x8F.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x90 ..= 0x97.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x98 ..= 0x9F.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xA0 ..= 0xA7.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xA8 ..= 0xAF.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xB0 ..= 0xB7.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xB8 ..= 0xBF.
|
||||
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xC0 ..= 0xC7.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xC8 ..= 0xCF.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xD0 ..= 0xD7.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xD8 ..= 0xDF.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xE0 ..= 0xE7.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xE8 ..= 0xEF.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xF0 ..= 0xF7.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xF8 ..= 0xFF.
|
||||
// 0 1 2 3 4 5 6 7
|
||||
// 8 9 A B C D E F
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xC0 ..= 0xC7.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xC8 ..= 0xCF.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xD0 ..= 0xD7.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xD8 ..= 0xDF.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xE0 ..= 0xE7.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xE8 ..= 0xEF.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xF0 ..= 0xF7.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xF8 ..= 0xFF.
|
||||
// 0 1 2 3 4 5 6 7
|
||||
// 8 9 A B C D E F
|
||||
]
|
||||
|
||||
pri const LUT_HEXADECIMAL_DIGITS : array[256] base.u8 = [
|
||||
// 0 1 2 3 4 5 6 7
|
||||
// 8 9 A B C D E F
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x00 ..= 0x07.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x08 ..= 0x0F.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x10 ..= 0x17.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x18 ..= 0x1F.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x20 ..= 0x27.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x28 ..= 0x2F.
|
||||
0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, // 0x30 ..= 0x37. '0'-'7'.
|
||||
0x88, 0x89, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x38 ..= 0x3F. '8'-'9'.
|
||||
// 0 1 2 3 4 5 6 7
|
||||
// 8 9 A B C D E F
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x00 ..= 0x07.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x08 ..= 0x0F.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x10 ..= 0x17.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x18 ..= 0x1F.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x20 ..= 0x27.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x28 ..= 0x2F.
|
||||
0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, // 0x30 ..= 0x37. '0'-'7'.
|
||||
0x88, 0x89, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x38 ..= 0x3F. '8'-'9'.
|
||||
|
||||
0x00, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F, 0x00, // 0x40 ..= 0x47. 'A'-'F'.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x48 ..= 0x4F.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x50 ..= 0x57.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x58 ..= 0x5F.
|
||||
0x00, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F, 0x00, // 0x60 ..= 0x67. 'a'-'f'.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x68 ..= 0x6F.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x70 ..= 0x77.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x78 ..= 0x7F.
|
||||
0x00, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F, 0x00, // 0x40 ..= 0x47. 'A'-'F'.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x48 ..= 0x4F.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x50 ..= 0x57.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x58 ..= 0x5F.
|
||||
0x00, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F, 0x00, // 0x60 ..= 0x67. 'a'-'f'.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x68 ..= 0x6F.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x70 ..= 0x77.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x78 ..= 0x7F.
|
||||
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x80 ..= 0x87.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x88 ..= 0x8F.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x90 ..= 0x97.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x98 ..= 0x9F.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xA0 ..= 0xA7.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xA8 ..= 0xAF.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xB0 ..= 0xB7.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xB8 ..= 0xBF.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x80 ..= 0x87.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x88 ..= 0x8F.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x90 ..= 0x97.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x98 ..= 0x9F.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xA0 ..= 0xA7.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xA8 ..= 0xAF.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xB0 ..= 0xB7.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xB8 ..= 0xBF.
|
||||
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xC0 ..= 0xC7.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xC8 ..= 0xCF.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xD0 ..= 0xD7.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xD8 ..= 0xDF.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xE0 ..= 0xE7.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xE8 ..= 0xEF.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xF0 ..= 0xF7.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xF8 ..= 0xFF.
|
||||
// 0 1 2 3 4 5 6 7
|
||||
// 8 9 A B C D E F
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xC0 ..= 0xC7.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xC8 ..= 0xCF.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xD0 ..= 0xD7.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xD8 ..= 0xDF.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xE0 ..= 0xE7.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xE8 ..= 0xEF.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xF0 ..= 0xF7.
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xF8 ..= 0xFF.
|
||||
// 0 1 2 3 4 5 6 7
|
||||
// 8 9 A B C D E F
|
||||
]
|
||||
+1648
-1648
@@ -13,1728 +13,1728 @@
|
||||
// limitations under the License.
|
||||
|
||||
pub struct decoder? implements base.token_decoder(
|
||||
quirks : array[QUIRKS_COUNT] base.bool,
|
||||
quirks : array[QUIRKS_COUNT] base.bool,
|
||||
|
||||
allow_leading_ars : base.bool,
|
||||
allow_leading_ubom : base.bool,
|
||||
allow_leading_ars : base.bool,
|
||||
allow_leading_ubom : base.bool,
|
||||
|
||||
end_of_data : base.bool,
|
||||
end_of_data : base.bool,
|
||||
|
||||
trailer_stop : base.u8,
|
||||
trailer_stop : base.u8,
|
||||
|
||||
// comment_type is set as a side-effect of decode_comment?.
|
||||
// - 0 means no comment.
|
||||
// - 1 means a block comment.
|
||||
// - 2 means a line comment.
|
||||
comment_type : base.u8,
|
||||
// comment_type is set as a side-effect of decode_comment?.
|
||||
// - 0 means no comment.
|
||||
// - 1 means a block comment.
|
||||
// - 2 means a line comment.
|
||||
comment_type : base.u8,
|
||||
|
||||
util : base.utility,
|
||||
util : base.utility,
|
||||
)(
|
||||
// stack is conceptually an array of bits, implemented as an array of u32.
|
||||
// The N'th bit being 0 or 1 means that we're in an array or object, where
|
||||
// N is the recursion depth.
|
||||
//
|
||||
// Parsing JSON involves recursion: containers (arrays and objects) can
|
||||
// hold other containers. As child elements are completed, the parser needs
|
||||
// to remember 1 bit of state per recursion depth: whether the parent
|
||||
// container was an array or an object. When continuing to parse the
|
||||
// parent's elements, `, "key": value` is only valid for objects.
|
||||
//
|
||||
// Note that we explicitly track our own stack and depth. We do not use the
|
||||
// call stack to hold this state and the decoder.decode_tokens function is
|
||||
// not recursive per se.
|
||||
//
|
||||
// Wuffs code does not have the capability to dynamically allocate memory,
|
||||
// so the maximum depth is hard-coded at compile time. In this case, the
|
||||
// maximum is 1024 (stack is 1024 bits or 128 bytes), also known as
|
||||
// DECODER_DEPTH_MAX_INCL.
|
||||
//
|
||||
// The [JSON spec](https://www.ietf.org/rfc/rfc8259.txt) clearly states,
|
||||
// "an implementation may set limits on the maximum depth of nesting".
|
||||
//
|
||||
// In comparison, as of February 2020, the Chromium web browser's JSON
|
||||
// parser's maximum recursion depth is 200:
|
||||
// https://source.chromium.org/chromium/chromium/src/+/3dece34cde622faa0daac07156c25d92c9897d1e:base/json/json_common.h;l=18
|
||||
//
|
||||
// Other languages and libraries' maximum depths (determined empirically)
|
||||
// are listed at https://github.com/lovasoa/bad_json_parsers#results
|
||||
stack : array[1024 / 32] base.u32,
|
||||
// stack is conceptually an array of bits, implemented as an array of u32.
|
||||
// The N'th bit being 0 or 1 means that we're in an array or object, where
|
||||
// N is the recursion depth.
|
||||
//
|
||||
// Parsing JSON involves recursion: containers (arrays and objects) can
|
||||
// hold other containers. As child elements are completed, the parser needs
|
||||
// to remember 1 bit of state per recursion depth: whether the parent
|
||||
// container was an array or an object. When continuing to parse the
|
||||
// parent's elements, `, "key": value` is only valid for objects.
|
||||
//
|
||||
// Note that we explicitly track our own stack and depth. We do not use the
|
||||
// call stack to hold this state and the decoder.decode_tokens function is
|
||||
// not recursive per se.
|
||||
//
|
||||
// Wuffs code does not have the capability to dynamically allocate memory,
|
||||
// so the maximum depth is hard-coded at compile time. In this case, the
|
||||
// maximum is 1024 (stack is 1024 bits or 128 bytes), also known as
|
||||
// DECODER_DEPTH_MAX_INCL.
|
||||
//
|
||||
// The [JSON spec](https://www.ietf.org/rfc/rfc8259.txt) clearly states,
|
||||
// "an implementation may set limits on the maximum depth of nesting".
|
||||
//
|
||||
// In comparison, as of February 2020, the Chromium web browser's JSON
|
||||
// parser's maximum recursion depth is 200:
|
||||
// https://source.chromium.org/chromium/chromium/src/+/3dece34cde622faa0daac07156c25d92c9897d1e:base/json/json_common.h;l=18
|
||||
//
|
||||
// Other languages and libraries' maximum depths (determined empirically)
|
||||
// are listed at https://github.com/lovasoa/bad_json_parsers#results
|
||||
stack : array[1024 / 32] base.u32,
|
||||
)
|
||||
|
||||
pub func decoder.set_quirk_enabled!(quirk: base.u32, enabled: base.bool) {
|
||||
if args.quirk >= QUIRKS_BASE {
|
||||
args.quirk -= QUIRKS_BASE
|
||||
if args.quirk < QUIRKS_COUNT {
|
||||
this.quirks[args.quirk] = args.enabled
|
||||
}
|
||||
}
|
||||
if args.quirk >= QUIRKS_BASE {
|
||||
args.quirk -= QUIRKS_BASE
|
||||
if args.quirk < QUIRKS_COUNT {
|
||||
this.quirks[args.quirk] = args.enabled
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub func decoder.workbuf_len() base.range_ii_u64 {
|
||||
return this.util.empty_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) {
|
||||
// This is a very, very long function, and it is tempting to refactor it.
|
||||
// Be careful of performance impacts when doing so. For example, commit
|
||||
// 86d3b89f "Factor out json.decoder.decode_string" pulled out a 500 line
|
||||
// decode_string function, which was certainly cleaner structurally, but
|
||||
// also regressed performance by 1.1x to 1.2x. For details, see
|
||||
// https://github.com/google/wuffs/commit/86d3b89f9a6578d964a4b6d71e21dfc9bb702b44
|
||||
|
||||
var vminor : base.u32[..= 0xFF_FFFF]
|
||||
var number_length : base.u32[..= 0x3FF]
|
||||
var number_status : base.u32[..= 0x3]
|
||||
var string_length : base.u32[..= 0xFFFB]
|
||||
var whitespace_length : base.u32[..= 0xFFFE]
|
||||
var depth : base.u32[..= 1024]
|
||||
var stack_byte : base.u32[..= (1024 / 32) - 1]
|
||||
var stack_bit : base.u32[..= 31]
|
||||
var match : base.u32[..= 2]
|
||||
var c4 : base.u32
|
||||
var c : base.u8
|
||||
var backslash : base.u8
|
||||
var char : base.u8
|
||||
var class : base.u8[..= 0x0F]
|
||||
var multi_byte_utf8 : base.u32
|
||||
|
||||
var backslash_x_ok : base.u8
|
||||
var backslash_x_value : base.u8
|
||||
var backslash_x_string : base.u32
|
||||
|
||||
var uni4_ok : base.u8
|
||||
var uni4_string : base.u64
|
||||
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,
|
||||
// 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
|
||||
|
||||
if this.end_of_data {
|
||||
return base."@end of data"
|
||||
}
|
||||
|
||||
if this.quirks[QUIRK_EXPECT_TRAILING_NEW_LINE_OR_EOF - QUIRKS_BASE] {
|
||||
if this.quirks[QUIRK_ALLOW_COMMENT_BLOCK - QUIRKS_BASE] or
|
||||
this.quirks[QUIRK_ALLOW_COMMENT_LINE - QUIRKS_BASE] or
|
||||
this.quirks[QUIRK_ALLOW_TRAILING_FILLER - QUIRKS_BASE] {
|
||||
return "#bad quirk combination"
|
||||
}
|
||||
}
|
||||
|
||||
if this.quirks[QUIRK_ALLOW_LEADING_ASCII_RECORD_SEPARATOR - QUIRKS_BASE] or
|
||||
this.quirks[QUIRK_ALLOW_LEADING_UNICODE_BYTE_ORDER_MARK - QUIRKS_BASE] {
|
||||
this.decode_leading?(dst: args.dst, src: args.src)
|
||||
}
|
||||
|
||||
expect = EXPECT_VALUE
|
||||
|
||||
while.outer true {
|
||||
while.goto_parsed_a_leaf_value true {{
|
||||
if args.dst.length() <= 0 {
|
||||
yield? base."$short write"
|
||||
continue.outer
|
||||
}
|
||||
|
||||
// Consume whitespace.
|
||||
whitespace_length = 0
|
||||
c = 0
|
||||
class = 0
|
||||
while.ws true,
|
||||
inv args.dst.length() > 0,
|
||||
post args.src.length() > 0,
|
||||
{
|
||||
if args.src.length() <= 0 {
|
||||
if whitespace_length > 0 {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: 0,
|
||||
continued: 0,
|
||||
length: whitespace_length)
|
||||
whitespace_length = 0
|
||||
}
|
||||
if args.src.is_closed() {
|
||||
return "#bad input"
|
||||
}
|
||||
yield? base."$short read"
|
||||
continue.outer
|
||||
}
|
||||
|
||||
c = args.src.peek_u8()
|
||||
class = LUT_CLASSES[c]
|
||||
if class <> CLASS_WHITESPACE {
|
||||
break.ws
|
||||
}
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
|
||||
if whitespace_length >= 0xFFFE {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: 0,
|
||||
continued: 0,
|
||||
length: 0xFFFF)
|
||||
whitespace_length = 0
|
||||
continue.outer
|
||||
}
|
||||
whitespace_length += 1
|
||||
} endwhile.ws
|
||||
|
||||
// Emit whitespace.
|
||||
if whitespace_length > 0 {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: 0,
|
||||
continued: 0,
|
||||
length: whitespace_length)
|
||||
whitespace_length = 0
|
||||
if args.dst.length() <= 0 {
|
||||
continue.outer
|
||||
}
|
||||
}
|
||||
|
||||
// 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 to the code
|
||||
// above inadvertently invalidating these assertions.
|
||||
assert args.dst.length() > 0
|
||||
assert args.src.length() > 0
|
||||
|
||||
if class == CLASS_STRING {
|
||||
// -------- BEGIN parse strings.
|
||||
// Emit the leading '"'.
|
||||
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)
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
|
||||
while.string_loop_outer true {
|
||||
if args.dst.length() <= 0 {
|
||||
yield? base."$short write"
|
||||
continue.string_loop_outer
|
||||
}
|
||||
|
||||
string_length = 0
|
||||
while.string_loop_inner true,
|
||||
pre args.dst.length() > 0,
|
||||
{
|
||||
if args.src.length() <= 0 {
|
||||
if string_length > 0 {
|
||||
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__CONVERT_1_DST_1_SRC_COPY,
|
||||
continued: 1,
|
||||
length: string_length)
|
||||
string_length = 0
|
||||
}
|
||||
if args.src.is_closed() {
|
||||
return "#bad input"
|
||||
}
|
||||
yield? base."$short read"
|
||||
continue.string_loop_outer
|
||||
}
|
||||
|
||||
// As an optimization, consume non-special ASCII 4 bytes at
|
||||
// a time.
|
||||
while args.src.length() > 4,
|
||||
inv args.dst.length() > 0,
|
||||
inv args.src.length() > 0,
|
||||
{
|
||||
c4 = args.src.peek_u32le()
|
||||
if 0x00 <> (LUT_CHARS[0xFF & (c4 >> 0)] |
|
||||
LUT_CHARS[0xFF & (c4 >> 8)] |
|
||||
LUT_CHARS[0xFF & (c4 >> 16)] |
|
||||
LUT_CHARS[0xFF & (c4 >> 24)]) {
|
||||
break
|
||||
}
|
||||
args.src.skip_u32_fast!(actual: 4, worst_case: 4)
|
||||
if string_length > (0xFFFB - 4) {
|
||||
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__CONVERT_1_DST_1_SRC_COPY,
|
||||
continued: 1,
|
||||
length: string_length + 4)
|
||||
string_length = 0
|
||||
continue.string_loop_outer
|
||||
}
|
||||
string_length += 4
|
||||
} endwhile
|
||||
|
||||
c = args.src.peek_u8()
|
||||
char = LUT_CHARS[c]
|
||||
|
||||
if char == 0x00 { // Non-special ASCII.
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
if string_length >= 0xFFFB {
|
||||
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__CONVERT_1_DST_1_SRC_COPY,
|
||||
continued: 1,
|
||||
length: 0xFFFC)
|
||||
string_length = 0
|
||||
continue.string_loop_outer
|
||||
}
|
||||
string_length += 1
|
||||
continue.string_loop_inner
|
||||
|
||||
} else if char == 0x01 { // '"'
|
||||
if string_length <> 0 {
|
||||
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__CONVERT_1_DST_1_SRC_COPY,
|
||||
continued: 1,
|
||||
length: string_length)
|
||||
string_length = 0
|
||||
}
|
||||
break.string_loop_outer
|
||||
|
||||
} else if char == 0x02 { // '\\'.
|
||||
if string_length > 0 {
|
||||
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__CONVERT_1_DST_1_SRC_COPY,
|
||||
continued: 1,
|
||||
length: string_length)
|
||||
string_length = 0
|
||||
if args.dst.length() <= 0 {
|
||||
continue.string_loop_outer
|
||||
}
|
||||
}
|
||||
assert args.dst.length() > 0
|
||||
|
||||
if args.src.length() < 2 {
|
||||
if args.src.is_closed() {
|
||||
return "#bad backslash-escape"
|
||||
}
|
||||
yield? base."$short read"
|
||||
continue.string_loop_outer
|
||||
}
|
||||
c = (args.src.peek_u16le() >> 8) as base.u8
|
||||
backslash = LUT_BACKSLASHES[c]
|
||||
if (backslash & 0x80) <> 0 {
|
||||
args.src.skip_u32_fast!(actual: 2, worst_case: 2)
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__UNICODE_CODE_POINT << 21) |
|
||||
((backslash & 0x7F) as base.u32),
|
||||
continued: 1,
|
||||
length: 2)
|
||||
continue.string_loop_outer
|
||||
|
||||
} else if backslash <> 0 {
|
||||
if this.quirks[LUT_QUIRKY_BACKSLASHES_QUIRKS[backslash & 7]] {
|
||||
args.src.skip_u32_fast!(actual: 2, worst_case: 2)
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__UNICODE_CODE_POINT << 21) |
|
||||
(LUT_QUIRKY_BACKSLASHES_CHARS[backslash & 7] as base.u32),
|
||||
continued: 1,
|
||||
length: 2)
|
||||
continue.string_loop_outer
|
||||
}
|
||||
|
||||
} else if c == 'u' {
|
||||
// -------- BEGIN backslash-u.
|
||||
if args.src.length() < 6 {
|
||||
if args.src.is_closed() {
|
||||
return "#bad backslash-escape"
|
||||
}
|
||||
yield? base."$short read"
|
||||
continue.string_loop_outer
|
||||
}
|
||||
|
||||
uni4_string = args.src.peek_u48le_as_u64() >> 16
|
||||
uni4_value = 0
|
||||
uni4_ok = 0x80
|
||||
|
||||
c = LUT_HEXADECIMAL_DIGITS[0xFF & (uni4_string >> 0)]
|
||||
uni4_ok &= c
|
||||
uni4_value |= ((c & 0x0F) as base.u32) << 12
|
||||
c = LUT_HEXADECIMAL_DIGITS[0xFF & (uni4_string >> 8)]
|
||||
uni4_ok &= c
|
||||
uni4_value |= ((c & 0x0F) as base.u32) << 8
|
||||
c = LUT_HEXADECIMAL_DIGITS[0xFF & (uni4_string >> 16)]
|
||||
uni4_ok &= c
|
||||
uni4_value |= ((c & 0x0F) as base.u32) << 4
|
||||
c = LUT_HEXADECIMAL_DIGITS[0xFF & (uni4_string >> 24)]
|
||||
uni4_ok &= c
|
||||
uni4_value |= ((c & 0x0F) as base.u32) << 0
|
||||
|
||||
if uni4_ok == 0 {
|
||||
// It wasn't 4 hexadecimal digits. No-op (and
|
||||
// fall through to "#bad backslash-escape").
|
||||
|
||||
} else if (uni4_value < 0xD800) or (0xDFFF < uni4_value) {
|
||||
// Not a Unicode surrogate. We're good.
|
||||
args.src.skip_u32_fast!(actual: 6, worst_case: 6)
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__UNICODE_CODE_POINT << 21) |
|
||||
uni4_value,
|
||||
continued: 1,
|
||||
length: 6)
|
||||
continue.string_loop_outer
|
||||
|
||||
} else if uni4_value >= 0xDC00 {
|
||||
// Low surrogate. No-op (and fall through to
|
||||
// "#bad backslash-escape").
|
||||
|
||||
} else {
|
||||
// High surrogate, which needs to be followed
|
||||
// by a "\\u1234" low surrogate. We've already
|
||||
// peeked 6 bytes for the high surrogate. We
|
||||
// need 12 in total: another 8 bytes at an
|
||||
// offset of 4.
|
||||
if args.src.length() < 12 {
|
||||
if args.src.is_closed() {
|
||||
if this.quirks[QUIRK_REPLACE_INVALID_UNICODE - QUIRKS_BASE] {
|
||||
args.src.skip_u32_fast!(actual: 6, worst_case: 6)
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__UNICODE_CODE_POINT << 21) |
|
||||
base.UNICODE__REPLACEMENT_CHARACTER,
|
||||
continued: 1,
|
||||
length: 6)
|
||||
continue.string_loop_outer
|
||||
}
|
||||
return "#bad backslash-escape"
|
||||
}
|
||||
yield? base."$short read"
|
||||
continue.string_loop_outer
|
||||
}
|
||||
uni4_string = args.src.peek_u64le_at(offset: 4) >> 16
|
||||
|
||||
// Look for the low surrogate's "\\u".
|
||||
if ((0xFF & (uni4_string >> 0)) <> '\\') or
|
||||
((0xFF & (uni4_string >> 8)) <> 'u') {
|
||||
uni4_high_surrogate = 0
|
||||
uni4_value = 0
|
||||
uni4_ok = 0
|
||||
} else {
|
||||
uni4_high_surrogate =
|
||||
0x1_0000 + ((uni4_value - 0xD800) << 10)
|
||||
uni4_value = 0
|
||||
uni4_ok = 0x80
|
||||
uni4_string >>= 16
|
||||
|
||||
c = LUT_HEXADECIMAL_DIGITS[0xFF & (uni4_string >> 0)]
|
||||
uni4_ok &= c
|
||||
uni4_value |= ((c & 0x0F) as base.u32) << 12
|
||||
c = LUT_HEXADECIMAL_DIGITS[0xFF & (uni4_string >> 8)]
|
||||
uni4_ok &= c
|
||||
uni4_value |= ((c & 0x0F) as base.u32) << 8
|
||||
c = LUT_HEXADECIMAL_DIGITS[0xFF & (uni4_string >> 16)]
|
||||
uni4_ok &= c
|
||||
uni4_value |= ((c & 0x0F) as base.u32) << 4
|
||||
c = LUT_HEXADECIMAL_DIGITS[0xFF & (uni4_string >> 24)]
|
||||
uni4_ok &= c
|
||||
uni4_value |= ((c & 0x0F) as base.u32) << 0
|
||||
}
|
||||
|
||||
if (uni4_ok <> 0) and
|
||||
(0xDC00 <= uni4_value) and (uni4_value <= 0xDFFF) {
|
||||
|
||||
// Emit a single token for the surrogate
|
||||
// pair.
|
||||
uni4_value -= 0xDC00
|
||||
args.src.skip_u32_fast!(actual: 12, worst_case: 12)
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__UNICODE_CODE_POINT << 21) |
|
||||
uni4_high_surrogate | uni4_value,
|
||||
continued: 1,
|
||||
length: 12)
|
||||
continue.string_loop_outer
|
||||
}
|
||||
}
|
||||
|
||||
if this.quirks[QUIRK_REPLACE_INVALID_UNICODE - QUIRKS_BASE] {
|
||||
if args.src.length() < 6 {
|
||||
return "#internal error: inconsistent I/O"
|
||||
}
|
||||
args.src.skip_u32_fast!(actual: 6, worst_case: 6)
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__UNICODE_CODE_POINT << 21) |
|
||||
base.UNICODE__REPLACEMENT_CHARACTER,
|
||||
continued: 1,
|
||||
length: 6)
|
||||
continue.string_loop_outer
|
||||
}
|
||||
// -------- END backslash-u.
|
||||
|
||||
} else if (c == 'U') and
|
||||
this.quirks[QUIRK_ALLOW_BACKSLASH_CAPITAL_U - QUIRKS_BASE] {
|
||||
// -------- BEGIN backslash-capital-u.
|
||||
if args.src.length() < 10 {
|
||||
if args.src.is_closed() {
|
||||
return "#bad backslash-escape"
|
||||
}
|
||||
yield? base."$short read"
|
||||
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.skip_u32_fast!(actual: 10, worst_case: 10)
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__UNICODE_CODE_POINT << 21) |
|
||||
(uni8_value & 0x1F_FFFF),
|
||||
continued: 1,
|
||||
length: 10)
|
||||
continue.string_loop_outer
|
||||
} else if this.quirks[QUIRK_REPLACE_INVALID_UNICODE - QUIRKS_BASE] {
|
||||
args.src.skip_u32_fast!(actual: 10, worst_case: 10)
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__UNICODE_CODE_POINT << 21) |
|
||||
base.UNICODE__REPLACEMENT_CHARACTER,
|
||||
continued: 1,
|
||||
length: 10)
|
||||
continue.string_loop_outer
|
||||
}
|
||||
// -------- END backslash-capital-u.
|
||||
|
||||
} else if (c == 'x') and
|
||||
this.quirks[QUIRK_ALLOW_BACKSLASH_X_AS_CODE_POINTS - QUIRKS_BASE] {
|
||||
// -------- BEGIN backslash-x
|
||||
if args.src.length() < 4 {
|
||||
if args.src.is_closed() {
|
||||
return "#bad backslash-escape"
|
||||
}
|
||||
yield? base."$short read"
|
||||
continue.string_loop_outer
|
||||
}
|
||||
|
||||
backslash_x_string = args.src.peek_u32le()
|
||||
backslash_x_ok = 0x80
|
||||
|
||||
c = LUT_HEXADECIMAL_DIGITS[0xFF & (backslash_x_string >> 16)]
|
||||
backslash_x_ok &= c
|
||||
backslash_x_value = ((c & 0x0F) << 4) as base.u8
|
||||
c = LUT_HEXADECIMAL_DIGITS[0xFF & (backslash_x_string >> 24)]
|
||||
backslash_x_ok &= c
|
||||
backslash_x_value = (backslash_x_value | (c & 0x0F)) as base.u8
|
||||
|
||||
if (backslash_x_ok == 0) or
|
||||
((backslash_x_string & 0xFFFF) <> 0x785C) {
|
||||
// It wasn't "\\x34", for some hexadecimal
|
||||
// digits "34".
|
||||
return "#bad backslash-escape"
|
||||
}
|
||||
args.src.skip_u32_fast!(actual: 4, worst_case: 4)
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__UNICODE_CODE_POINT << 21) |
|
||||
(backslash_x_value as base.u32),
|
||||
continued: 1,
|
||||
length: 4)
|
||||
continue.string_loop_outer
|
||||
// -------- END backslash-x
|
||||
}
|
||||
|
||||
return "#bad backslash-escape"
|
||||
|
||||
} else if char == 0x03 { // 2-byte UTF-8.
|
||||
if args.src.length() < 2 {
|
||||
if string_length > 0 {
|
||||
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__CONVERT_1_DST_1_SRC_COPY,
|
||||
continued: 1,
|
||||
length: string_length)
|
||||
string_length = 0
|
||||
if args.dst.length() <= 0 {
|
||||
continue.string_loop_outer
|
||||
}
|
||||
}
|
||||
if args.src.is_closed() {
|
||||
if this.quirks[QUIRK_REPLACE_INVALID_UNICODE - QUIRKS_BASE] {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__UNICODE_CODE_POINT << 21) |
|
||||
base.UNICODE__REPLACEMENT_CHARACTER,
|
||||
continued: 1,
|
||||
length: 1)
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
continue.string_loop_outer
|
||||
}
|
||||
return "#bad UTF-8"
|
||||
}
|
||||
yield? base."$short read"
|
||||
continue.string_loop_outer
|
||||
}
|
||||
multi_byte_utf8 = args.src.peek_u16le_as_u32()
|
||||
if (multi_byte_utf8 & 0xC000) == 0x8000 {
|
||||
multi_byte_utf8 = (0x00_07C0 & (multi_byte_utf8 ~mod<< 6)) |
|
||||
(0x00_003F & (multi_byte_utf8 >> 8))
|
||||
args.src.skip_u32_fast!(actual: 2, worst_case: 2)
|
||||
if string_length >= 0xFFF8 {
|
||||
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__CONVERT_1_DST_1_SRC_COPY,
|
||||
continued: 1,
|
||||
length: string_length + 2)
|
||||
string_length = 0
|
||||
continue.string_loop_outer
|
||||
}
|
||||
string_length += 2
|
||||
continue.string_loop_inner
|
||||
}
|
||||
|
||||
} else if char == 0x04 { // 3-byte UTF-8.
|
||||
if args.src.length() < 3 {
|
||||
if string_length > 0 {
|
||||
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__CONVERT_1_DST_1_SRC_COPY,
|
||||
continued: 1,
|
||||
length: string_length)
|
||||
string_length = 0
|
||||
if args.dst.length() <= 0 {
|
||||
continue.string_loop_outer
|
||||
}
|
||||
}
|
||||
if args.src.is_closed() {
|
||||
if this.quirks[QUIRK_REPLACE_INVALID_UNICODE - QUIRKS_BASE] {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__UNICODE_CODE_POINT << 21) |
|
||||
base.UNICODE__REPLACEMENT_CHARACTER,
|
||||
continued: 1,
|
||||
length: 1)
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
continue.string_loop_outer
|
||||
}
|
||||
return "#bad UTF-8"
|
||||
}
|
||||
yield? base."$short read"
|
||||
continue.string_loop_outer
|
||||
}
|
||||
multi_byte_utf8 = args.src.peek_u24le_as_u32()
|
||||
if (multi_byte_utf8 & 0xC0_C000) == 0x80_8000 {
|
||||
multi_byte_utf8 = (0x00_F000 & (multi_byte_utf8 ~mod<< 12)) |
|
||||
(0x00_0FC0 & (multi_byte_utf8 >> 2)) |
|
||||
(0x00_003F & (multi_byte_utf8 >> 16))
|
||||
if (0x07FF < multi_byte_utf8) and
|
||||
((multi_byte_utf8 < 0xD800) or (0xDFFF < multi_byte_utf8)) {
|
||||
|
||||
args.src.skip_u32_fast!(actual: 3, worst_case: 3)
|
||||
if string_length >= 0xFFF8 {
|
||||
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__CONVERT_1_DST_1_SRC_COPY,
|
||||
continued: 1,
|
||||
length: string_length + 3)
|
||||
string_length = 0
|
||||
continue.string_loop_outer
|
||||
}
|
||||
string_length += 3
|
||||
continue.string_loop_inner
|
||||
}
|
||||
}
|
||||
|
||||
} else if char == 0x05 { // 4-byte UTF-8.
|
||||
if args.src.length() < 4 {
|
||||
if string_length > 0 {
|
||||
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__CONVERT_1_DST_1_SRC_COPY,
|
||||
continued: 1,
|
||||
length: string_length)
|
||||
string_length = 0
|
||||
if args.dst.length() <= 0 {
|
||||
continue.string_loop_outer
|
||||
}
|
||||
}
|
||||
if args.src.is_closed() {
|
||||
if this.quirks[QUIRK_REPLACE_INVALID_UNICODE - QUIRKS_BASE] {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__UNICODE_CODE_POINT << 21) |
|
||||
base.UNICODE__REPLACEMENT_CHARACTER,
|
||||
continued: 1,
|
||||
length: 1)
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
continue.string_loop_outer
|
||||
}
|
||||
return "#bad UTF-8"
|
||||
}
|
||||
yield? base."$short read"
|
||||
continue.string_loop_outer
|
||||
}
|
||||
multi_byte_utf8 = args.src.peek_u32le()
|
||||
if (multi_byte_utf8 & 0xC0C0_C000) == 0x8080_8000 {
|
||||
multi_byte_utf8 = (0x1C_0000 & (multi_byte_utf8 ~mod<< 18)) |
|
||||
(0x03_F000 & (multi_byte_utf8 ~mod<< 4)) |
|
||||
(0x00_0FC0 & (multi_byte_utf8 >> 10)) |
|
||||
(0x00_003F & (multi_byte_utf8 >> 24))
|
||||
if (0xFFFF < multi_byte_utf8) and (multi_byte_utf8 <= 0x10_FFFF) {
|
||||
args.src.skip_u32_fast!(actual: 4, worst_case: 4)
|
||||
if string_length >= 0xFFF8 {
|
||||
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__CONVERT_1_DST_1_SRC_COPY,
|
||||
continued: 1,
|
||||
length: string_length + 4)
|
||||
string_length = 0
|
||||
continue.string_loop_outer
|
||||
}
|
||||
string_length += 4
|
||||
continue.string_loop_inner
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if string_length > 0 {
|
||||
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__CONVERT_1_DST_1_SRC_COPY,
|
||||
continued: 1,
|
||||
length: string_length)
|
||||
string_length = 0
|
||||
if args.dst.length() <= 0 {
|
||||
continue.string_loop_outer
|
||||
}
|
||||
}
|
||||
if (char & 0x80) <> 0 {
|
||||
if this.quirks[QUIRK_ALLOW_ASCII_CONTROL_CODES - QUIRKS_BASE] {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__UNICODE_CODE_POINT << 21) |
|
||||
((char & 0x7F) as base.u32),
|
||||
continued: 1,
|
||||
length: 1)
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
continue.string_loop_outer
|
||||
}
|
||||
if char == 0x8A {
|
||||
return "#bad new-line in a string"
|
||||
}
|
||||
return "#bad C0 control code"
|
||||
}
|
||||
if this.quirks[QUIRK_REPLACE_INVALID_UNICODE - QUIRKS_BASE] {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__UNICODE_CODE_POINT << 21) |
|
||||
base.UNICODE__REPLACEMENT_CHARACTER,
|
||||
continued: 1,
|
||||
length: 1)
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
continue.string_loop_outer
|
||||
}
|
||||
return "#bad UTF-8"
|
||||
} endwhile.string_loop_inner
|
||||
} endwhile.string_loop_outer
|
||||
|
||||
// Emit the trailing '"'.
|
||||
while true {
|
||||
if args.src.length() <= 0 {
|
||||
if args.src.is_closed() {
|
||||
return "#bad input"
|
||||
}
|
||||
yield? base."$short read"
|
||||
continue
|
||||
}
|
||||
if args.dst.length() <= 0 {
|
||||
yield? base."$short write"
|
||||
continue
|
||||
}
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
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
|
||||
} endwhile
|
||||
|
||||
// As above, expect must have contained EXPECT_STRING. If it didn't
|
||||
// also contain EXPECT_NUMBER (excluding EXPECT_COMMENT) then we
|
||||
// were parsing an object key and the next token should be ':'.
|
||||
if 0 == (expect & ((1 as base.u32) << CLASS_NUMBER)) {
|
||||
expect = EXPECT_COLON
|
||||
continue.outer
|
||||
}
|
||||
break.goto_parsed_a_leaf_value
|
||||
// -------- END parse strings.
|
||||
|
||||
} else if class == CLASS_COMMA {
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
// The ',' is punctuation (filler).
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__FILLER << 21) |
|
||||
base.TOKEN__VBD__FILLER__PUNCTUATION,
|
||||
continued: 0,
|
||||
length: 1)
|
||||
// What's valid after a comma depends on whether or not we're in an
|
||||
// array or an object.
|
||||
if 0 == (expect & ((1 as base.u32) << CLASS_CLOSE_SQUARE_BRACKET)) {
|
||||
if this.quirks[QUIRK_ALLOW_EXTRA_COMMA - QUIRKS_BASE] {
|
||||
expect = EXPECT_STRING | EXPECT_CLOSE_CURLY_BRACE
|
||||
} else {
|
||||
expect = EXPECT_STRING
|
||||
}
|
||||
} else {
|
||||
if this.quirks[QUIRK_ALLOW_EXTRA_COMMA - QUIRKS_BASE] {
|
||||
expect = EXPECT_VALUE | EXPECT_CLOSE_SQUARE_BRACKET
|
||||
} else {
|
||||
expect = EXPECT_VALUE
|
||||
}
|
||||
}
|
||||
continue.outer
|
||||
|
||||
} else if class == CLASS_COLON {
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
// The ':' is punctuation (filler).
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__FILLER << 21) |
|
||||
base.TOKEN__VBD__FILLER__PUNCTUATION,
|
||||
continued: 0,
|
||||
length: 1)
|
||||
expect = EXPECT_VALUE
|
||||
continue.outer
|
||||
|
||||
} else if class == CLASS_NUMBER {
|
||||
// -------- BEGIN parse numbers.
|
||||
while true,
|
||||
pre args.dst.length() > 0,
|
||||
{
|
||||
number_length = this.decode_number!(src: args.src)
|
||||
number_status = number_length >> 8
|
||||
vminor = (base.TOKEN__VBC__NUMBER << 21) |
|
||||
base.TOKEN__VBD__NUMBER__CONTENT_FLOATING_POINT |
|
||||
base.TOKEN__VBD__NUMBER__CONTENT_INTEGER_SIGNED |
|
||||
base.TOKEN__VBD__NUMBER__FORMAT_TEXT
|
||||
if (number_length & 0x80) <> 0 {
|
||||
vminor = (base.TOKEN__VBC__NUMBER << 21) |
|
||||
base.TOKEN__VBD__NUMBER__CONTENT_FLOATING_POINT |
|
||||
base.TOKEN__VBD__NUMBER__FORMAT_TEXT
|
||||
}
|
||||
number_length = number_length & 0x7F
|
||||
if number_status == 0 {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: vminor,
|
||||
continued: 0,
|
||||
length: number_length)
|
||||
break
|
||||
}
|
||||
|
||||
while number_length > 0 {
|
||||
number_length -= 1
|
||||
if args.src.can_undo_byte() {
|
||||
args.src.undo_byte!()
|
||||
} else {
|
||||
return "#internal error: inconsistent I/O"
|
||||
}
|
||||
} endwhile
|
||||
|
||||
if number_status == 1 {
|
||||
if this.quirks[QUIRK_ALLOW_INF_NAN_NUMBERS - QUIRKS_BASE] {
|
||||
this.decode_inf_nan?(dst: args.dst, src: args.src)
|
||||
break
|
||||
}
|
||||
return "#bad input"
|
||||
} else if number_status == 2 {
|
||||
return "#unsupported number length"
|
||||
} else {
|
||||
yield? base."$short read"
|
||||
while args.dst.length() <= 0,
|
||||
post args.dst.length() > 0,
|
||||
{
|
||||
yield? base."$short write"
|
||||
} endwhile
|
||||
}
|
||||
} endwhile
|
||||
break.goto_parsed_a_leaf_value
|
||||
// -------- END parse numbers.
|
||||
|
||||
} else if class == CLASS_OPEN_CURLY_BRACE {
|
||||
vminor = (base.TOKEN__VBC__STRUCTURE << 21) |
|
||||
base.TOKEN__VBD__STRUCTURE__PUSH |
|
||||
base.TOKEN__VBD__STRUCTURE__FROM_NONE |
|
||||
base.TOKEN__VBD__STRUCTURE__TO_DICT
|
||||
if depth == 0 {
|
||||
// No-op.
|
||||
} else if 0 <> (expect_after_value & ((1 as base.u32) << CLASS_CLOSE_CURLY_BRACE)) {
|
||||
vminor = (base.TOKEN__VBC__STRUCTURE << 21) |
|
||||
base.TOKEN__VBD__STRUCTURE__PUSH |
|
||||
base.TOKEN__VBD__STRUCTURE__FROM_DICT |
|
||||
base.TOKEN__VBD__STRUCTURE__TO_DICT
|
||||
} else {
|
||||
vminor = (base.TOKEN__VBC__STRUCTURE << 21) |
|
||||
base.TOKEN__VBD__STRUCTURE__PUSH |
|
||||
base.TOKEN__VBD__STRUCTURE__FROM_LIST |
|
||||
base.TOKEN__VBD__STRUCTURE__TO_DICT
|
||||
}
|
||||
if depth >= 1024 {
|
||||
return "#unsupported recursion depth"
|
||||
}
|
||||
stack_byte = depth / 32
|
||||
stack_bit = depth & 31
|
||||
this.stack[stack_byte] |= (1 as base.u32) << stack_bit
|
||||
depth += 1
|
||||
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: vminor,
|
||||
continued: 0,
|
||||
length: 1)
|
||||
expect = EXPECT_CLOSE_CURLY_BRACE | EXPECT_STRING
|
||||
expect_after_value = EXPECT_CLOSE_CURLY_BRACE | EXPECT_COMMA
|
||||
continue.outer
|
||||
|
||||
} else if class == CLASS_CLOSE_CURLY_BRACE {
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
if depth <= 1 {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__STRUCTURE << 21) |
|
||||
base.TOKEN__VBD__STRUCTURE__POP |
|
||||
base.TOKEN__VBD__STRUCTURE__FROM_DICT |
|
||||
base.TOKEN__VBD__STRUCTURE__TO_NONE,
|
||||
continued: 0,
|
||||
length: 1)
|
||||
break.outer
|
||||
}
|
||||
depth -= 1
|
||||
stack_byte = (depth - 1) / 32
|
||||
stack_bit = (depth - 1) & 31
|
||||
if 0 == (this.stack[stack_byte] & ((1 as base.u32) << stack_bit)) {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__STRUCTURE << 21) |
|
||||
base.TOKEN__VBD__STRUCTURE__POP |
|
||||
base.TOKEN__VBD__STRUCTURE__FROM_DICT |
|
||||
base.TOKEN__VBD__STRUCTURE__TO_LIST,
|
||||
continued: 0,
|
||||
length: 1)
|
||||
expect = EXPECT_CLOSE_SQUARE_BRACKET | EXPECT_COMMA
|
||||
expect_after_value = EXPECT_CLOSE_SQUARE_BRACKET | EXPECT_COMMA
|
||||
} else {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__STRUCTURE << 21) |
|
||||
base.TOKEN__VBD__STRUCTURE__POP |
|
||||
base.TOKEN__VBD__STRUCTURE__FROM_DICT |
|
||||
base.TOKEN__VBD__STRUCTURE__TO_DICT,
|
||||
continued: 0,
|
||||
length: 1)
|
||||
expect = EXPECT_CLOSE_CURLY_BRACE | EXPECT_COMMA
|
||||
expect_after_value = EXPECT_CLOSE_CURLY_BRACE | EXPECT_COMMA
|
||||
}
|
||||
continue.outer
|
||||
|
||||
} else if class == CLASS_OPEN_SQUARE_BRACKET {
|
||||
vminor = (base.TOKEN__VBC__STRUCTURE << 21) |
|
||||
base.TOKEN__VBD__STRUCTURE__PUSH |
|
||||
base.TOKEN__VBD__STRUCTURE__FROM_NONE |
|
||||
base.TOKEN__VBD__STRUCTURE__TO_LIST
|
||||
if depth == 0 {
|
||||
// No-op.
|
||||
} else if 0 <> (expect_after_value & ((1 as base.u32) << CLASS_CLOSE_CURLY_BRACE)) {
|
||||
vminor = (base.TOKEN__VBC__STRUCTURE << 21) |
|
||||
base.TOKEN__VBD__STRUCTURE__PUSH |
|
||||
base.TOKEN__VBD__STRUCTURE__FROM_DICT |
|
||||
base.TOKEN__VBD__STRUCTURE__TO_LIST
|
||||
} else {
|
||||
vminor = (base.TOKEN__VBC__STRUCTURE << 21) |
|
||||
base.TOKEN__VBD__STRUCTURE__PUSH |
|
||||
base.TOKEN__VBD__STRUCTURE__FROM_LIST |
|
||||
base.TOKEN__VBD__STRUCTURE__TO_LIST
|
||||
}
|
||||
if depth >= 1024 {
|
||||
return "#unsupported recursion depth"
|
||||
}
|
||||
stack_byte = depth / 32
|
||||
stack_bit = depth & 31
|
||||
this.stack[stack_byte] &= 0xFFFF_FFFF ^ ((1 as base.u32) << stack_bit)
|
||||
depth += 1
|
||||
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: vminor,
|
||||
continued: 0,
|
||||
length: 1)
|
||||
expect = EXPECT_CLOSE_SQUARE_BRACKET | EXPECT_VALUE
|
||||
expect_after_value = EXPECT_CLOSE_SQUARE_BRACKET | EXPECT_COMMA
|
||||
continue.outer
|
||||
|
||||
} else if class == CLASS_CLOSE_SQUARE_BRACKET {
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
if depth <= 1 {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__STRUCTURE << 21) |
|
||||
base.TOKEN__VBD__STRUCTURE__POP |
|
||||
base.TOKEN__VBD__STRUCTURE__FROM_LIST |
|
||||
base.TOKEN__VBD__STRUCTURE__TO_NONE,
|
||||
continued: 0,
|
||||
length: 1)
|
||||
break.outer
|
||||
}
|
||||
depth -= 1
|
||||
stack_byte = (depth - 1) / 32
|
||||
stack_bit = (depth - 1) & 31
|
||||
if 0 == (this.stack[stack_byte] & ((1 as base.u32) << stack_bit)) {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__STRUCTURE << 21) |
|
||||
base.TOKEN__VBD__STRUCTURE__POP |
|
||||
base.TOKEN__VBD__STRUCTURE__FROM_LIST |
|
||||
base.TOKEN__VBD__STRUCTURE__TO_LIST,
|
||||
continued: 0,
|
||||
length: 1)
|
||||
expect = EXPECT_CLOSE_SQUARE_BRACKET | EXPECT_COMMA
|
||||
expect_after_value = EXPECT_CLOSE_SQUARE_BRACKET | EXPECT_COMMA
|
||||
} else {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__STRUCTURE << 21) |
|
||||
base.TOKEN__VBD__STRUCTURE__POP |
|
||||
base.TOKEN__VBD__STRUCTURE__FROM_LIST |
|
||||
base.TOKEN__VBD__STRUCTURE__TO_DICT,
|
||||
continued: 0,
|
||||
length: 1)
|
||||
expect = EXPECT_CLOSE_CURLY_BRACE | EXPECT_COMMA
|
||||
expect_after_value = EXPECT_CLOSE_CURLY_BRACE | EXPECT_COMMA
|
||||
}
|
||||
continue.outer
|
||||
|
||||
} else if class == CLASS_FALSE {
|
||||
match = args.src.match7(a: '\x05false'le)
|
||||
if match == 0 {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__LITERAL << 21) |
|
||||
base.TOKEN__VBD__LITERAL__FALSE,
|
||||
continued: 0,
|
||||
length: 5)
|
||||
if args.src.length() < 5 {
|
||||
return "#internal error: inconsistent I/O"
|
||||
}
|
||||
args.src.skip_u32_fast!(actual: 5, worst_case: 5)
|
||||
break.goto_parsed_a_leaf_value
|
||||
} else if match == 1 {
|
||||
yield? base."$short read"
|
||||
continue.outer
|
||||
}
|
||||
|
||||
} else if class == CLASS_TRUE {
|
||||
match = args.src.match7(a: '\x04true'le)
|
||||
if match == 0 {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__LITERAL << 21) |
|
||||
base.TOKEN__VBD__LITERAL__TRUE,
|
||||
continued: 0,
|
||||
length: 4)
|
||||
if args.src.length() < 4 {
|
||||
return "#internal error: inconsistent I/O"
|
||||
}
|
||||
args.src.skip_u32_fast!(actual: 4, worst_case: 4)
|
||||
break.goto_parsed_a_leaf_value
|
||||
} else if match == 1 {
|
||||
yield? base."$short read"
|
||||
continue.outer
|
||||
}
|
||||
|
||||
} else if class == CLASS_NULL_NAN_INF {
|
||||
match = args.src.match7(a: '\x04null'le)
|
||||
if match == 0 {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__LITERAL << 21) |
|
||||
base.TOKEN__VBD__LITERAL__NULL,
|
||||
continued: 0,
|
||||
length: 4)
|
||||
if args.src.length() < 4 {
|
||||
return "#internal error: inconsistent I/O"
|
||||
}
|
||||
args.src.skip_u32_fast!(actual: 4, worst_case: 4)
|
||||
break.goto_parsed_a_leaf_value
|
||||
} else if match == 1 {
|
||||
yield? base."$short read"
|
||||
continue.outer
|
||||
}
|
||||
|
||||
if this.quirks[QUIRK_ALLOW_INF_NAN_NUMBERS - QUIRKS_BASE] {
|
||||
this.decode_inf_nan?(dst: args.dst, src: args.src)
|
||||
break.goto_parsed_a_leaf_value
|
||||
}
|
||||
|
||||
} else if class == CLASS_COMMENT {
|
||||
if this.quirks[QUIRK_ALLOW_COMMENT_BLOCK - QUIRKS_BASE] or
|
||||
this.quirks[QUIRK_ALLOW_COMMENT_LINE - QUIRKS_BASE] {
|
||||
this.decode_comment?(dst: args.dst, src: args.src)
|
||||
if this.comment_type > 0 {
|
||||
continue.outer
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return "#bad input"
|
||||
}} 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
|
||||
}
|
||||
expect = expect_after_value
|
||||
} endwhile.outer
|
||||
|
||||
if this.quirks[QUIRK_ALLOW_TRAILING_FILLER - QUIRKS_BASE] or
|
||||
this.quirks[QUIRK_EXPECT_TRAILING_NEW_LINE_OR_EOF - QUIRKS_BASE] {
|
||||
this.decode_trailer?(dst: args.dst, src: args.src)
|
||||
}
|
||||
|
||||
this.end_of_data = true
|
||||
// This is a very, very long function, and it is tempting to refactor it.
|
||||
// Be careful of performance impacts when doing so. For example, commit
|
||||
// 86d3b89f "Factor out json.decoder.decode_string" pulled out a 500 line
|
||||
// decode_string function, which was certainly cleaner structurally, but
|
||||
// also regressed performance by 1.1x to 1.2x. For details, see
|
||||
// https://github.com/google/wuffs/commit/86d3b89f9a6578d964a4b6d71e21dfc9bb702b44
|
||||
|
||||
var vminor : base.u32[..= 0xFF_FFFF]
|
||||
var number_length : base.u32[..= 0x3FF]
|
||||
var number_status : base.u32[..= 0x3]
|
||||
var string_length : base.u32[..= 0xFFFB]
|
||||
var whitespace_length : base.u32[..= 0xFFFE]
|
||||
var depth : base.u32[..= 1024]
|
||||
var stack_byte : base.u32[..= (1024 / 32) - 1]
|
||||
var stack_bit : base.u32[..= 31]
|
||||
var match : base.u32[..= 2]
|
||||
var c4 : base.u32
|
||||
var c : base.u8
|
||||
var backslash : base.u8
|
||||
var char : base.u8
|
||||
var class : base.u8[..= 0x0F]
|
||||
var multi_byte_utf8 : base.u32
|
||||
|
||||
var backslash_x_ok : base.u8
|
||||
var backslash_x_value : base.u8
|
||||
var backslash_x_string : base.u32
|
||||
|
||||
var uni4_ok : base.u8
|
||||
var uni4_string : base.u64
|
||||
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,
|
||||
// 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
|
||||
|
||||
if this.end_of_data {
|
||||
return base."@end of data"
|
||||
}
|
||||
|
||||
if this.quirks[QUIRK_EXPECT_TRAILING_NEW_LINE_OR_EOF - QUIRKS_BASE] {
|
||||
if this.quirks[QUIRK_ALLOW_COMMENT_BLOCK - QUIRKS_BASE] or
|
||||
this.quirks[QUIRK_ALLOW_COMMENT_LINE - QUIRKS_BASE] or
|
||||
this.quirks[QUIRK_ALLOW_TRAILING_FILLER - QUIRKS_BASE] {
|
||||
return "#bad quirk combination"
|
||||
}
|
||||
}
|
||||
|
||||
if this.quirks[QUIRK_ALLOW_LEADING_ASCII_RECORD_SEPARATOR - QUIRKS_BASE] or
|
||||
this.quirks[QUIRK_ALLOW_LEADING_UNICODE_BYTE_ORDER_MARK - QUIRKS_BASE] {
|
||||
this.decode_leading?(dst: args.dst, src: args.src)
|
||||
}
|
||||
|
||||
expect = EXPECT_VALUE
|
||||
|
||||
while.outer true {
|
||||
while.goto_parsed_a_leaf_value true {{
|
||||
if args.dst.length() <= 0 {
|
||||
yield? base."$short write"
|
||||
continue.outer
|
||||
}
|
||||
|
||||
// Consume whitespace.
|
||||
whitespace_length = 0
|
||||
c = 0
|
||||
class = 0
|
||||
while.ws true,
|
||||
inv args.dst.length() > 0,
|
||||
post args.src.length() > 0,
|
||||
{
|
||||
if args.src.length() <= 0 {
|
||||
if whitespace_length > 0 {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: 0,
|
||||
continued: 0,
|
||||
length: whitespace_length)
|
||||
whitespace_length = 0
|
||||
}
|
||||
if args.src.is_closed() {
|
||||
return "#bad input"
|
||||
}
|
||||
yield? base."$short read"
|
||||
continue.outer
|
||||
}
|
||||
|
||||
c = args.src.peek_u8()
|
||||
class = LUT_CLASSES[c]
|
||||
if class <> CLASS_WHITESPACE {
|
||||
break.ws
|
||||
}
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
|
||||
if whitespace_length >= 0xFFFE {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: 0,
|
||||
continued: 0,
|
||||
length: 0xFFFF)
|
||||
whitespace_length = 0
|
||||
continue.outer
|
||||
}
|
||||
whitespace_length += 1
|
||||
} endwhile.ws
|
||||
|
||||
// Emit whitespace.
|
||||
if whitespace_length > 0 {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: 0,
|
||||
continued: 0,
|
||||
length: whitespace_length)
|
||||
whitespace_length = 0
|
||||
if args.dst.length() <= 0 {
|
||||
continue.outer
|
||||
}
|
||||
}
|
||||
|
||||
// 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 to the code
|
||||
// above inadvertently invalidating these assertions.
|
||||
assert args.dst.length() > 0
|
||||
assert args.src.length() > 0
|
||||
|
||||
if class == CLASS_STRING {
|
||||
// -------- BEGIN parse strings.
|
||||
// Emit the leading '"'.
|
||||
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)
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
|
||||
while.string_loop_outer true {
|
||||
if args.dst.length() <= 0 {
|
||||
yield? base."$short write"
|
||||
continue.string_loop_outer
|
||||
}
|
||||
|
||||
string_length = 0
|
||||
while.string_loop_inner true,
|
||||
pre args.dst.length() > 0,
|
||||
{
|
||||
if args.src.length() <= 0 {
|
||||
if string_length > 0 {
|
||||
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__CONVERT_1_DST_1_SRC_COPY,
|
||||
continued: 1,
|
||||
length: string_length)
|
||||
string_length = 0
|
||||
}
|
||||
if args.src.is_closed() {
|
||||
return "#bad input"
|
||||
}
|
||||
yield? base."$short read"
|
||||
continue.string_loop_outer
|
||||
}
|
||||
|
||||
// As an optimization, consume non-special ASCII 4 bytes at
|
||||
// a time.
|
||||
while args.src.length() > 4,
|
||||
inv args.dst.length() > 0,
|
||||
inv args.src.length() > 0,
|
||||
{
|
||||
c4 = args.src.peek_u32le()
|
||||
if 0x00 <> (LUT_CHARS[0xFF & (c4 >> 0)] |
|
||||
LUT_CHARS[0xFF & (c4 >> 8)] |
|
||||
LUT_CHARS[0xFF & (c4 >> 16)] |
|
||||
LUT_CHARS[0xFF & (c4 >> 24)]) {
|
||||
break
|
||||
}
|
||||
args.src.skip_u32_fast!(actual: 4, worst_case: 4)
|
||||
if string_length > (0xFFFB - 4) {
|
||||
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__CONVERT_1_DST_1_SRC_COPY,
|
||||
continued: 1,
|
||||
length: string_length + 4)
|
||||
string_length = 0
|
||||
continue.string_loop_outer
|
||||
}
|
||||
string_length += 4
|
||||
} endwhile
|
||||
|
||||
c = args.src.peek_u8()
|
||||
char = LUT_CHARS[c]
|
||||
|
||||
if char == 0x00 { // Non-special ASCII.
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
if string_length >= 0xFFFB {
|
||||
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__CONVERT_1_DST_1_SRC_COPY,
|
||||
continued: 1,
|
||||
length: 0xFFFC)
|
||||
string_length = 0
|
||||
continue.string_loop_outer
|
||||
}
|
||||
string_length += 1
|
||||
continue.string_loop_inner
|
||||
|
||||
} else if char == 0x01 { // '"'
|
||||
if string_length <> 0 {
|
||||
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__CONVERT_1_DST_1_SRC_COPY,
|
||||
continued: 1,
|
||||
length: string_length)
|
||||
string_length = 0
|
||||
}
|
||||
break.string_loop_outer
|
||||
|
||||
} else if char == 0x02 { // '\\'.
|
||||
if string_length > 0 {
|
||||
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__CONVERT_1_DST_1_SRC_COPY,
|
||||
continued: 1,
|
||||
length: string_length)
|
||||
string_length = 0
|
||||
if args.dst.length() <= 0 {
|
||||
continue.string_loop_outer
|
||||
}
|
||||
}
|
||||
assert args.dst.length() > 0
|
||||
|
||||
if args.src.length() < 2 {
|
||||
if args.src.is_closed() {
|
||||
return "#bad backslash-escape"
|
||||
}
|
||||
yield? base."$short read"
|
||||
continue.string_loop_outer
|
||||
}
|
||||
c = (args.src.peek_u16le() >> 8) as base.u8
|
||||
backslash = LUT_BACKSLASHES[c]
|
||||
if (backslash & 0x80) <> 0 {
|
||||
args.src.skip_u32_fast!(actual: 2, worst_case: 2)
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__UNICODE_CODE_POINT << 21) |
|
||||
((backslash & 0x7F) as base.u32),
|
||||
continued: 1,
|
||||
length: 2)
|
||||
continue.string_loop_outer
|
||||
|
||||
} else if backslash <> 0 {
|
||||
if this.quirks[LUT_QUIRKY_BACKSLASHES_QUIRKS[backslash & 7]] {
|
||||
args.src.skip_u32_fast!(actual: 2, worst_case: 2)
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__UNICODE_CODE_POINT << 21) |
|
||||
(LUT_QUIRKY_BACKSLASHES_CHARS[backslash & 7] as base.u32),
|
||||
continued: 1,
|
||||
length: 2)
|
||||
continue.string_loop_outer
|
||||
}
|
||||
|
||||
} else if c == 'u' {
|
||||
// -------- BEGIN backslash-u.
|
||||
if args.src.length() < 6 {
|
||||
if args.src.is_closed() {
|
||||
return "#bad backslash-escape"
|
||||
}
|
||||
yield? base."$short read"
|
||||
continue.string_loop_outer
|
||||
}
|
||||
|
||||
uni4_string = args.src.peek_u48le_as_u64() >> 16
|
||||
uni4_value = 0
|
||||
uni4_ok = 0x80
|
||||
|
||||
c = LUT_HEXADECIMAL_DIGITS[0xFF & (uni4_string >> 0)]
|
||||
uni4_ok &= c
|
||||
uni4_value |= ((c & 0x0F) as base.u32) << 12
|
||||
c = LUT_HEXADECIMAL_DIGITS[0xFF & (uni4_string >> 8)]
|
||||
uni4_ok &= c
|
||||
uni4_value |= ((c & 0x0F) as base.u32) << 8
|
||||
c = LUT_HEXADECIMAL_DIGITS[0xFF & (uni4_string >> 16)]
|
||||
uni4_ok &= c
|
||||
uni4_value |= ((c & 0x0F) as base.u32) << 4
|
||||
c = LUT_HEXADECIMAL_DIGITS[0xFF & (uni4_string >> 24)]
|
||||
uni4_ok &= c
|
||||
uni4_value |= ((c & 0x0F) as base.u32) << 0
|
||||
|
||||
if uni4_ok == 0 {
|
||||
// It wasn't 4 hexadecimal digits. No-op (and
|
||||
// fall through to "#bad backslash-escape").
|
||||
|
||||
} else if (uni4_value < 0xD800) or (0xDFFF < uni4_value) {
|
||||
// Not a Unicode surrogate. We're good.
|
||||
args.src.skip_u32_fast!(actual: 6, worst_case: 6)
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__UNICODE_CODE_POINT << 21) |
|
||||
uni4_value,
|
||||
continued: 1,
|
||||
length: 6)
|
||||
continue.string_loop_outer
|
||||
|
||||
} else if uni4_value >= 0xDC00 {
|
||||
// Low surrogate. No-op (and fall through to
|
||||
// "#bad backslash-escape").
|
||||
|
||||
} else {
|
||||
// High surrogate, which needs to be followed
|
||||
// by a "\\u1234" low surrogate. We've already
|
||||
// peeked 6 bytes for the high surrogate. We
|
||||
// need 12 in total: another 8 bytes at an
|
||||
// offset of 4.
|
||||
if args.src.length() < 12 {
|
||||
if args.src.is_closed() {
|
||||
if this.quirks[QUIRK_REPLACE_INVALID_UNICODE - QUIRKS_BASE] {
|
||||
args.src.skip_u32_fast!(actual: 6, worst_case: 6)
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__UNICODE_CODE_POINT << 21) |
|
||||
base.UNICODE__REPLACEMENT_CHARACTER,
|
||||
continued: 1,
|
||||
length: 6)
|
||||
continue.string_loop_outer
|
||||
}
|
||||
return "#bad backslash-escape"
|
||||
}
|
||||
yield? base."$short read"
|
||||
continue.string_loop_outer
|
||||
}
|
||||
uni4_string = args.src.peek_u64le_at(offset: 4) >> 16
|
||||
|
||||
// Look for the low surrogate's "\\u".
|
||||
if ((0xFF & (uni4_string >> 0)) <> '\\') or
|
||||
((0xFF & (uni4_string >> 8)) <> 'u') {
|
||||
uni4_high_surrogate = 0
|
||||
uni4_value = 0
|
||||
uni4_ok = 0
|
||||
} else {
|
||||
uni4_high_surrogate =
|
||||
0x1_0000 + ((uni4_value - 0xD800) << 10)
|
||||
uni4_value = 0
|
||||
uni4_ok = 0x80
|
||||
uni4_string >>= 16
|
||||
|
||||
c = LUT_HEXADECIMAL_DIGITS[0xFF & (uni4_string >> 0)]
|
||||
uni4_ok &= c
|
||||
uni4_value |= ((c & 0x0F) as base.u32) << 12
|
||||
c = LUT_HEXADECIMAL_DIGITS[0xFF & (uni4_string >> 8)]
|
||||
uni4_ok &= c
|
||||
uni4_value |= ((c & 0x0F) as base.u32) << 8
|
||||
c = LUT_HEXADECIMAL_DIGITS[0xFF & (uni4_string >> 16)]
|
||||
uni4_ok &= c
|
||||
uni4_value |= ((c & 0x0F) as base.u32) << 4
|
||||
c = LUT_HEXADECIMAL_DIGITS[0xFF & (uni4_string >> 24)]
|
||||
uni4_ok &= c
|
||||
uni4_value |= ((c & 0x0F) as base.u32) << 0
|
||||
}
|
||||
|
||||
if (uni4_ok <> 0) and
|
||||
(0xDC00 <= uni4_value) and (uni4_value <= 0xDFFF) {
|
||||
|
||||
// Emit a single token for the surrogate
|
||||
// pair.
|
||||
uni4_value -= 0xDC00
|
||||
args.src.skip_u32_fast!(actual: 12, worst_case: 12)
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__UNICODE_CODE_POINT << 21) |
|
||||
uni4_high_surrogate | uni4_value,
|
||||
continued: 1,
|
||||
length: 12)
|
||||
continue.string_loop_outer
|
||||
}
|
||||
}
|
||||
|
||||
if this.quirks[QUIRK_REPLACE_INVALID_UNICODE - QUIRKS_BASE] {
|
||||
if args.src.length() < 6 {
|
||||
return "#internal error: inconsistent I/O"
|
||||
}
|
||||
args.src.skip_u32_fast!(actual: 6, worst_case: 6)
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__UNICODE_CODE_POINT << 21) |
|
||||
base.UNICODE__REPLACEMENT_CHARACTER,
|
||||
continued: 1,
|
||||
length: 6)
|
||||
continue.string_loop_outer
|
||||
}
|
||||
// -------- END backslash-u.
|
||||
|
||||
} else if (c == 'U') and
|
||||
this.quirks[QUIRK_ALLOW_BACKSLASH_CAPITAL_U - QUIRKS_BASE] {
|
||||
// -------- BEGIN backslash-capital-u.
|
||||
if args.src.length() < 10 {
|
||||
if args.src.is_closed() {
|
||||
return "#bad backslash-escape"
|
||||
}
|
||||
yield? base."$short read"
|
||||
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.skip_u32_fast!(actual: 10, worst_case: 10)
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__UNICODE_CODE_POINT << 21) |
|
||||
(uni8_value & 0x1F_FFFF),
|
||||
continued: 1,
|
||||
length: 10)
|
||||
continue.string_loop_outer
|
||||
} else if this.quirks[QUIRK_REPLACE_INVALID_UNICODE - QUIRKS_BASE] {
|
||||
args.src.skip_u32_fast!(actual: 10, worst_case: 10)
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__UNICODE_CODE_POINT << 21) |
|
||||
base.UNICODE__REPLACEMENT_CHARACTER,
|
||||
continued: 1,
|
||||
length: 10)
|
||||
continue.string_loop_outer
|
||||
}
|
||||
// -------- END backslash-capital-u.
|
||||
|
||||
} else if (c == 'x') and
|
||||
this.quirks[QUIRK_ALLOW_BACKSLASH_X_AS_CODE_POINTS - QUIRKS_BASE] {
|
||||
// -------- BEGIN backslash-x
|
||||
if args.src.length() < 4 {
|
||||
if args.src.is_closed() {
|
||||
return "#bad backslash-escape"
|
||||
}
|
||||
yield? base."$short read"
|
||||
continue.string_loop_outer
|
||||
}
|
||||
|
||||
backslash_x_string = args.src.peek_u32le()
|
||||
backslash_x_ok = 0x80
|
||||
|
||||
c = LUT_HEXADECIMAL_DIGITS[0xFF & (backslash_x_string >> 16)]
|
||||
backslash_x_ok &= c
|
||||
backslash_x_value = ((c & 0x0F) << 4) as base.u8
|
||||
c = LUT_HEXADECIMAL_DIGITS[0xFF & (backslash_x_string >> 24)]
|
||||
backslash_x_ok &= c
|
||||
backslash_x_value = (backslash_x_value | (c & 0x0F)) as base.u8
|
||||
|
||||
if (backslash_x_ok == 0) or
|
||||
((backslash_x_string & 0xFFFF) <> 0x785C) {
|
||||
// It wasn't "\\x34", for some hexadecimal
|
||||
// digits "34".
|
||||
return "#bad backslash-escape"
|
||||
}
|
||||
args.src.skip_u32_fast!(actual: 4, worst_case: 4)
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__UNICODE_CODE_POINT << 21) |
|
||||
(backslash_x_value as base.u32),
|
||||
continued: 1,
|
||||
length: 4)
|
||||
continue.string_loop_outer
|
||||
// -------- END backslash-x
|
||||
}
|
||||
|
||||
return "#bad backslash-escape"
|
||||
|
||||
} else if char == 0x03 { // 2-byte UTF-8.
|
||||
if args.src.length() < 2 {
|
||||
if string_length > 0 {
|
||||
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__CONVERT_1_DST_1_SRC_COPY,
|
||||
continued: 1,
|
||||
length: string_length)
|
||||
string_length = 0
|
||||
if args.dst.length() <= 0 {
|
||||
continue.string_loop_outer
|
||||
}
|
||||
}
|
||||
if args.src.is_closed() {
|
||||
if this.quirks[QUIRK_REPLACE_INVALID_UNICODE - QUIRKS_BASE] {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__UNICODE_CODE_POINT << 21) |
|
||||
base.UNICODE__REPLACEMENT_CHARACTER,
|
||||
continued: 1,
|
||||
length: 1)
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
continue.string_loop_outer
|
||||
}
|
||||
return "#bad UTF-8"
|
||||
}
|
||||
yield? base."$short read"
|
||||
continue.string_loop_outer
|
||||
}
|
||||
multi_byte_utf8 = args.src.peek_u16le_as_u32()
|
||||
if (multi_byte_utf8 & 0xC000) == 0x8000 {
|
||||
multi_byte_utf8 = (0x00_07C0 & (multi_byte_utf8 ~mod<< 6)) |
|
||||
(0x00_003F & (multi_byte_utf8 >> 8))
|
||||
args.src.skip_u32_fast!(actual: 2, worst_case: 2)
|
||||
if string_length >= 0xFFF8 {
|
||||
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__CONVERT_1_DST_1_SRC_COPY,
|
||||
continued: 1,
|
||||
length: string_length + 2)
|
||||
string_length = 0
|
||||
continue.string_loop_outer
|
||||
}
|
||||
string_length += 2
|
||||
continue.string_loop_inner
|
||||
}
|
||||
|
||||
} else if char == 0x04 { // 3-byte UTF-8.
|
||||
if args.src.length() < 3 {
|
||||
if string_length > 0 {
|
||||
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__CONVERT_1_DST_1_SRC_COPY,
|
||||
continued: 1,
|
||||
length: string_length)
|
||||
string_length = 0
|
||||
if args.dst.length() <= 0 {
|
||||
continue.string_loop_outer
|
||||
}
|
||||
}
|
||||
if args.src.is_closed() {
|
||||
if this.quirks[QUIRK_REPLACE_INVALID_UNICODE - QUIRKS_BASE] {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__UNICODE_CODE_POINT << 21) |
|
||||
base.UNICODE__REPLACEMENT_CHARACTER,
|
||||
continued: 1,
|
||||
length: 1)
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
continue.string_loop_outer
|
||||
}
|
||||
return "#bad UTF-8"
|
||||
}
|
||||
yield? base."$short read"
|
||||
continue.string_loop_outer
|
||||
}
|
||||
multi_byte_utf8 = args.src.peek_u24le_as_u32()
|
||||
if (multi_byte_utf8 & 0xC0_C000) == 0x80_8000 {
|
||||
multi_byte_utf8 = (0x00_F000 & (multi_byte_utf8 ~mod<< 12)) |
|
||||
(0x00_0FC0 & (multi_byte_utf8 >> 2)) |
|
||||
(0x00_003F & (multi_byte_utf8 >> 16))
|
||||
if (0x07FF < multi_byte_utf8) and
|
||||
((multi_byte_utf8 < 0xD800) or (0xDFFF < multi_byte_utf8)) {
|
||||
|
||||
args.src.skip_u32_fast!(actual: 3, worst_case: 3)
|
||||
if string_length >= 0xFFF8 {
|
||||
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__CONVERT_1_DST_1_SRC_COPY,
|
||||
continued: 1,
|
||||
length: string_length + 3)
|
||||
string_length = 0
|
||||
continue.string_loop_outer
|
||||
}
|
||||
string_length += 3
|
||||
continue.string_loop_inner
|
||||
}
|
||||
}
|
||||
|
||||
} else if char == 0x05 { // 4-byte UTF-8.
|
||||
if args.src.length() < 4 {
|
||||
if string_length > 0 {
|
||||
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__CONVERT_1_DST_1_SRC_COPY,
|
||||
continued: 1,
|
||||
length: string_length)
|
||||
string_length = 0
|
||||
if args.dst.length() <= 0 {
|
||||
continue.string_loop_outer
|
||||
}
|
||||
}
|
||||
if args.src.is_closed() {
|
||||
if this.quirks[QUIRK_REPLACE_INVALID_UNICODE - QUIRKS_BASE] {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__UNICODE_CODE_POINT << 21) |
|
||||
base.UNICODE__REPLACEMENT_CHARACTER,
|
||||
continued: 1,
|
||||
length: 1)
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
continue.string_loop_outer
|
||||
}
|
||||
return "#bad UTF-8"
|
||||
}
|
||||
yield? base."$short read"
|
||||
continue.string_loop_outer
|
||||
}
|
||||
multi_byte_utf8 = args.src.peek_u32le()
|
||||
if (multi_byte_utf8 & 0xC0C0_C000) == 0x8080_8000 {
|
||||
multi_byte_utf8 = (0x1C_0000 & (multi_byte_utf8 ~mod<< 18)) |
|
||||
(0x03_F000 & (multi_byte_utf8 ~mod<< 4)) |
|
||||
(0x00_0FC0 & (multi_byte_utf8 >> 10)) |
|
||||
(0x00_003F & (multi_byte_utf8 >> 24))
|
||||
if (0xFFFF < multi_byte_utf8) and (multi_byte_utf8 <= 0x10_FFFF) {
|
||||
args.src.skip_u32_fast!(actual: 4, worst_case: 4)
|
||||
if string_length >= 0xFFF8 {
|
||||
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__CONVERT_1_DST_1_SRC_COPY,
|
||||
continued: 1,
|
||||
length: string_length + 4)
|
||||
string_length = 0
|
||||
continue.string_loop_outer
|
||||
}
|
||||
string_length += 4
|
||||
continue.string_loop_inner
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if string_length > 0 {
|
||||
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__CONVERT_1_DST_1_SRC_COPY,
|
||||
continued: 1,
|
||||
length: string_length)
|
||||
string_length = 0
|
||||
if args.dst.length() <= 0 {
|
||||
continue.string_loop_outer
|
||||
}
|
||||
}
|
||||
if (char & 0x80) <> 0 {
|
||||
if this.quirks[QUIRK_ALLOW_ASCII_CONTROL_CODES - QUIRKS_BASE] {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__UNICODE_CODE_POINT << 21) |
|
||||
((char & 0x7F) as base.u32),
|
||||
continued: 1,
|
||||
length: 1)
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
continue.string_loop_outer
|
||||
}
|
||||
if char == 0x8A {
|
||||
return "#bad new-line in a string"
|
||||
}
|
||||
return "#bad C0 control code"
|
||||
}
|
||||
if this.quirks[QUIRK_REPLACE_INVALID_UNICODE - QUIRKS_BASE] {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__UNICODE_CODE_POINT << 21) |
|
||||
base.UNICODE__REPLACEMENT_CHARACTER,
|
||||
continued: 1,
|
||||
length: 1)
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
continue.string_loop_outer
|
||||
}
|
||||
return "#bad UTF-8"
|
||||
} endwhile.string_loop_inner
|
||||
} endwhile.string_loop_outer
|
||||
|
||||
// Emit the trailing '"'.
|
||||
while true {
|
||||
if args.src.length() <= 0 {
|
||||
if args.src.is_closed() {
|
||||
return "#bad input"
|
||||
}
|
||||
yield? base."$short read"
|
||||
continue
|
||||
}
|
||||
if args.dst.length() <= 0 {
|
||||
yield? base."$short write"
|
||||
continue
|
||||
}
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
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
|
||||
} endwhile
|
||||
|
||||
// As above, expect must have contained EXPECT_STRING. If it didn't
|
||||
// also contain EXPECT_NUMBER (excluding EXPECT_COMMENT) then we
|
||||
// were parsing an object key and the next token should be ':'.
|
||||
if 0 == (expect & ((1 as base.u32) << CLASS_NUMBER)) {
|
||||
expect = EXPECT_COLON
|
||||
continue.outer
|
||||
}
|
||||
break.goto_parsed_a_leaf_value
|
||||
// -------- END parse strings.
|
||||
|
||||
} else if class == CLASS_COMMA {
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
// The ',' is punctuation (filler).
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__FILLER << 21) |
|
||||
base.TOKEN__VBD__FILLER__PUNCTUATION,
|
||||
continued: 0,
|
||||
length: 1)
|
||||
// What's valid after a comma depends on whether or not we're in an
|
||||
// array or an object.
|
||||
if 0 == (expect & ((1 as base.u32) << CLASS_CLOSE_SQUARE_BRACKET)) {
|
||||
if this.quirks[QUIRK_ALLOW_EXTRA_COMMA - QUIRKS_BASE] {
|
||||
expect = EXPECT_STRING | EXPECT_CLOSE_CURLY_BRACE
|
||||
} else {
|
||||
expect = EXPECT_STRING
|
||||
}
|
||||
} else {
|
||||
if this.quirks[QUIRK_ALLOW_EXTRA_COMMA - QUIRKS_BASE] {
|
||||
expect = EXPECT_VALUE | EXPECT_CLOSE_SQUARE_BRACKET
|
||||
} else {
|
||||
expect = EXPECT_VALUE
|
||||
}
|
||||
}
|
||||
continue.outer
|
||||
|
||||
} else if class == CLASS_COLON {
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
// The ':' is punctuation (filler).
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__FILLER << 21) |
|
||||
base.TOKEN__VBD__FILLER__PUNCTUATION,
|
||||
continued: 0,
|
||||
length: 1)
|
||||
expect = EXPECT_VALUE
|
||||
continue.outer
|
||||
|
||||
} else if class == CLASS_NUMBER {
|
||||
// -------- BEGIN parse numbers.
|
||||
while true,
|
||||
pre args.dst.length() > 0,
|
||||
{
|
||||
number_length = this.decode_number!(src: args.src)
|
||||
number_status = number_length >> 8
|
||||
vminor = (base.TOKEN__VBC__NUMBER << 21) |
|
||||
base.TOKEN__VBD__NUMBER__CONTENT_FLOATING_POINT |
|
||||
base.TOKEN__VBD__NUMBER__CONTENT_INTEGER_SIGNED |
|
||||
base.TOKEN__VBD__NUMBER__FORMAT_TEXT
|
||||
if (number_length & 0x80) <> 0 {
|
||||
vminor = (base.TOKEN__VBC__NUMBER << 21) |
|
||||
base.TOKEN__VBD__NUMBER__CONTENT_FLOATING_POINT |
|
||||
base.TOKEN__VBD__NUMBER__FORMAT_TEXT
|
||||
}
|
||||
number_length = number_length & 0x7F
|
||||
if number_status == 0 {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: vminor,
|
||||
continued: 0,
|
||||
length: number_length)
|
||||
break
|
||||
}
|
||||
|
||||
while number_length > 0 {
|
||||
number_length -= 1
|
||||
if args.src.can_undo_byte() {
|
||||
args.src.undo_byte!()
|
||||
} else {
|
||||
return "#internal error: inconsistent I/O"
|
||||
}
|
||||
} endwhile
|
||||
|
||||
if number_status == 1 {
|
||||
if this.quirks[QUIRK_ALLOW_INF_NAN_NUMBERS - QUIRKS_BASE] {
|
||||
this.decode_inf_nan?(dst: args.dst, src: args.src)
|
||||
break
|
||||
}
|
||||
return "#bad input"
|
||||
} else if number_status == 2 {
|
||||
return "#unsupported number length"
|
||||
} else {
|
||||
yield? base."$short read"
|
||||
while args.dst.length() <= 0,
|
||||
post args.dst.length() > 0,
|
||||
{
|
||||
yield? base."$short write"
|
||||
} endwhile
|
||||
}
|
||||
} endwhile
|
||||
break.goto_parsed_a_leaf_value
|
||||
// -------- END parse numbers.
|
||||
|
||||
} else if class == CLASS_OPEN_CURLY_BRACE {
|
||||
vminor = (base.TOKEN__VBC__STRUCTURE << 21) |
|
||||
base.TOKEN__VBD__STRUCTURE__PUSH |
|
||||
base.TOKEN__VBD__STRUCTURE__FROM_NONE |
|
||||
base.TOKEN__VBD__STRUCTURE__TO_DICT
|
||||
if depth == 0 {
|
||||
// No-op.
|
||||
} else if 0 <> (expect_after_value & ((1 as base.u32) << CLASS_CLOSE_CURLY_BRACE)) {
|
||||
vminor = (base.TOKEN__VBC__STRUCTURE << 21) |
|
||||
base.TOKEN__VBD__STRUCTURE__PUSH |
|
||||
base.TOKEN__VBD__STRUCTURE__FROM_DICT |
|
||||
base.TOKEN__VBD__STRUCTURE__TO_DICT
|
||||
} else {
|
||||
vminor = (base.TOKEN__VBC__STRUCTURE << 21) |
|
||||
base.TOKEN__VBD__STRUCTURE__PUSH |
|
||||
base.TOKEN__VBD__STRUCTURE__FROM_LIST |
|
||||
base.TOKEN__VBD__STRUCTURE__TO_DICT
|
||||
}
|
||||
if depth >= 1024 {
|
||||
return "#unsupported recursion depth"
|
||||
}
|
||||
stack_byte = depth / 32
|
||||
stack_bit = depth & 31
|
||||
this.stack[stack_byte] |= (1 as base.u32) << stack_bit
|
||||
depth += 1
|
||||
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: vminor,
|
||||
continued: 0,
|
||||
length: 1)
|
||||
expect = EXPECT_CLOSE_CURLY_BRACE | EXPECT_STRING
|
||||
expect_after_value = EXPECT_CLOSE_CURLY_BRACE | EXPECT_COMMA
|
||||
continue.outer
|
||||
|
||||
} else if class == CLASS_CLOSE_CURLY_BRACE {
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
if depth <= 1 {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__STRUCTURE << 21) |
|
||||
base.TOKEN__VBD__STRUCTURE__POP |
|
||||
base.TOKEN__VBD__STRUCTURE__FROM_DICT |
|
||||
base.TOKEN__VBD__STRUCTURE__TO_NONE,
|
||||
continued: 0,
|
||||
length: 1)
|
||||
break.outer
|
||||
}
|
||||
depth -= 1
|
||||
stack_byte = (depth - 1) / 32
|
||||
stack_bit = (depth - 1) & 31
|
||||
if 0 == (this.stack[stack_byte] & ((1 as base.u32) << stack_bit)) {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__STRUCTURE << 21) |
|
||||
base.TOKEN__VBD__STRUCTURE__POP |
|
||||
base.TOKEN__VBD__STRUCTURE__FROM_DICT |
|
||||
base.TOKEN__VBD__STRUCTURE__TO_LIST,
|
||||
continued: 0,
|
||||
length: 1)
|
||||
expect = EXPECT_CLOSE_SQUARE_BRACKET | EXPECT_COMMA
|
||||
expect_after_value = EXPECT_CLOSE_SQUARE_BRACKET | EXPECT_COMMA
|
||||
} else {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__STRUCTURE << 21) |
|
||||
base.TOKEN__VBD__STRUCTURE__POP |
|
||||
base.TOKEN__VBD__STRUCTURE__FROM_DICT |
|
||||
base.TOKEN__VBD__STRUCTURE__TO_DICT,
|
||||
continued: 0,
|
||||
length: 1)
|
||||
expect = EXPECT_CLOSE_CURLY_BRACE | EXPECT_COMMA
|
||||
expect_after_value = EXPECT_CLOSE_CURLY_BRACE | EXPECT_COMMA
|
||||
}
|
||||
continue.outer
|
||||
|
||||
} else if class == CLASS_OPEN_SQUARE_BRACKET {
|
||||
vminor = (base.TOKEN__VBC__STRUCTURE << 21) |
|
||||
base.TOKEN__VBD__STRUCTURE__PUSH |
|
||||
base.TOKEN__VBD__STRUCTURE__FROM_NONE |
|
||||
base.TOKEN__VBD__STRUCTURE__TO_LIST
|
||||
if depth == 0 {
|
||||
// No-op.
|
||||
} else if 0 <> (expect_after_value & ((1 as base.u32) << CLASS_CLOSE_CURLY_BRACE)) {
|
||||
vminor = (base.TOKEN__VBC__STRUCTURE << 21) |
|
||||
base.TOKEN__VBD__STRUCTURE__PUSH |
|
||||
base.TOKEN__VBD__STRUCTURE__FROM_DICT |
|
||||
base.TOKEN__VBD__STRUCTURE__TO_LIST
|
||||
} else {
|
||||
vminor = (base.TOKEN__VBC__STRUCTURE << 21) |
|
||||
base.TOKEN__VBD__STRUCTURE__PUSH |
|
||||
base.TOKEN__VBD__STRUCTURE__FROM_LIST |
|
||||
base.TOKEN__VBD__STRUCTURE__TO_LIST
|
||||
}
|
||||
if depth >= 1024 {
|
||||
return "#unsupported recursion depth"
|
||||
}
|
||||
stack_byte = depth / 32
|
||||
stack_bit = depth & 31
|
||||
this.stack[stack_byte] &= 0xFFFF_FFFF ^ ((1 as base.u32) << stack_bit)
|
||||
depth += 1
|
||||
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: vminor,
|
||||
continued: 0,
|
||||
length: 1)
|
||||
expect = EXPECT_CLOSE_SQUARE_BRACKET | EXPECT_VALUE
|
||||
expect_after_value = EXPECT_CLOSE_SQUARE_BRACKET | EXPECT_COMMA
|
||||
continue.outer
|
||||
|
||||
} else if class == CLASS_CLOSE_SQUARE_BRACKET {
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
if depth <= 1 {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__STRUCTURE << 21) |
|
||||
base.TOKEN__VBD__STRUCTURE__POP |
|
||||
base.TOKEN__VBD__STRUCTURE__FROM_LIST |
|
||||
base.TOKEN__VBD__STRUCTURE__TO_NONE,
|
||||
continued: 0,
|
||||
length: 1)
|
||||
break.outer
|
||||
}
|
||||
depth -= 1
|
||||
stack_byte = (depth - 1) / 32
|
||||
stack_bit = (depth - 1) & 31
|
||||
if 0 == (this.stack[stack_byte] & ((1 as base.u32) << stack_bit)) {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__STRUCTURE << 21) |
|
||||
base.TOKEN__VBD__STRUCTURE__POP |
|
||||
base.TOKEN__VBD__STRUCTURE__FROM_LIST |
|
||||
base.TOKEN__VBD__STRUCTURE__TO_LIST,
|
||||
continued: 0,
|
||||
length: 1)
|
||||
expect = EXPECT_CLOSE_SQUARE_BRACKET | EXPECT_COMMA
|
||||
expect_after_value = EXPECT_CLOSE_SQUARE_BRACKET | EXPECT_COMMA
|
||||
} else {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__STRUCTURE << 21) |
|
||||
base.TOKEN__VBD__STRUCTURE__POP |
|
||||
base.TOKEN__VBD__STRUCTURE__FROM_LIST |
|
||||
base.TOKEN__VBD__STRUCTURE__TO_DICT,
|
||||
continued: 0,
|
||||
length: 1)
|
||||
expect = EXPECT_CLOSE_CURLY_BRACE | EXPECT_COMMA
|
||||
expect_after_value = EXPECT_CLOSE_CURLY_BRACE | EXPECT_COMMA
|
||||
}
|
||||
continue.outer
|
||||
|
||||
} else if class == CLASS_FALSE {
|
||||
match = args.src.match7(a: '\x05false'le)
|
||||
if match == 0 {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__LITERAL << 21) |
|
||||
base.TOKEN__VBD__LITERAL__FALSE,
|
||||
continued: 0,
|
||||
length: 5)
|
||||
if args.src.length() < 5 {
|
||||
return "#internal error: inconsistent I/O"
|
||||
}
|
||||
args.src.skip_u32_fast!(actual: 5, worst_case: 5)
|
||||
break.goto_parsed_a_leaf_value
|
||||
} else if match == 1 {
|
||||
yield? base."$short read"
|
||||
continue.outer
|
||||
}
|
||||
|
||||
} else if class == CLASS_TRUE {
|
||||
match = args.src.match7(a: '\x04true'le)
|
||||
if match == 0 {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__LITERAL << 21) |
|
||||
base.TOKEN__VBD__LITERAL__TRUE,
|
||||
continued: 0,
|
||||
length: 4)
|
||||
if args.src.length() < 4 {
|
||||
return "#internal error: inconsistent I/O"
|
||||
}
|
||||
args.src.skip_u32_fast!(actual: 4, worst_case: 4)
|
||||
break.goto_parsed_a_leaf_value
|
||||
} else if match == 1 {
|
||||
yield? base."$short read"
|
||||
continue.outer
|
||||
}
|
||||
|
||||
} else if class == CLASS_NULL_NAN_INF {
|
||||
match = args.src.match7(a: '\x04null'le)
|
||||
if match == 0 {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__LITERAL << 21) |
|
||||
base.TOKEN__VBD__LITERAL__NULL,
|
||||
continued: 0,
|
||||
length: 4)
|
||||
if args.src.length() < 4 {
|
||||
return "#internal error: inconsistent I/O"
|
||||
}
|
||||
args.src.skip_u32_fast!(actual: 4, worst_case: 4)
|
||||
break.goto_parsed_a_leaf_value
|
||||
} else if match == 1 {
|
||||
yield? base."$short read"
|
||||
continue.outer
|
||||
}
|
||||
|
||||
if this.quirks[QUIRK_ALLOW_INF_NAN_NUMBERS - QUIRKS_BASE] {
|
||||
this.decode_inf_nan?(dst: args.dst, src: args.src)
|
||||
break.goto_parsed_a_leaf_value
|
||||
}
|
||||
|
||||
} else if class == CLASS_COMMENT {
|
||||
if this.quirks[QUIRK_ALLOW_COMMENT_BLOCK - QUIRKS_BASE] or
|
||||
this.quirks[QUIRK_ALLOW_COMMENT_LINE - QUIRKS_BASE] {
|
||||
this.decode_comment?(dst: args.dst, src: args.src)
|
||||
if this.comment_type > 0 {
|
||||
continue.outer
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return "#bad input"
|
||||
}} 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
|
||||
}
|
||||
expect = expect_after_value
|
||||
} endwhile.outer
|
||||
|
||||
if this.quirks[QUIRK_ALLOW_TRAILING_FILLER - QUIRKS_BASE] or
|
||||
this.quirks[QUIRK_EXPECT_TRAILING_NEW_LINE_OR_EOF - QUIRKS_BASE] {
|
||||
this.decode_trailer?(dst: args.dst, src: args.src)
|
||||
}
|
||||
|
||||
this.end_of_data = true
|
||||
}
|
||||
|
||||
pri func decoder.decode_number!(src: base.io_reader) base.u32[..= 0x3FF] {
|
||||
var c : base.u8
|
||||
var n : base.u32[..= 0x3FF]
|
||||
var floating_point : base.u32[..= 0x80]
|
||||
var c : base.u8
|
||||
var n : base.u32[..= 0x3FF]
|
||||
var floating_point : base.u32[..= 0x80]
|
||||
|
||||
while.goto_done true {{
|
||||
n = 0
|
||||
while.goto_done true {{
|
||||
n = 0
|
||||
|
||||
// Peek.
|
||||
if args.src.length() <= 0 {
|
||||
if not args.src.is_closed() {
|
||||
n |= 0x300
|
||||
}
|
||||
break.goto_done
|
||||
}
|
||||
c = args.src.peek_u8()
|
||||
// Peek.
|
||||
if args.src.length() <= 0 {
|
||||
if not args.src.is_closed() {
|
||||
n |= 0x300
|
||||
}
|
||||
break.goto_done
|
||||
}
|
||||
c = args.src.peek_u8()
|
||||
|
||||
// Scan the optional minus sign.
|
||||
if c <> '-' {
|
||||
assert args.src.length() > 0
|
||||
assert n <= 1
|
||||
} else {
|
||||
n += 1
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
// Scan the optional minus sign.
|
||||
if c <> '-' {
|
||||
assert args.src.length() > 0
|
||||
assert n <= 1
|
||||
} else {
|
||||
n += 1
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
|
||||
// Peek.
|
||||
if args.src.length() <= 0 {
|
||||
if not args.src.is_closed() {
|
||||
n |= 0x300
|
||||
}
|
||||
n |= 0x100 // A '-' without digits is invalid.
|
||||
break.goto_done
|
||||
}
|
||||
c = args.src.peek_u8()
|
||||
// Peek.
|
||||
if args.src.length() <= 0 {
|
||||
if not args.src.is_closed() {
|
||||
n |= 0x300
|
||||
}
|
||||
n |= 0x100 // A '-' without digits is invalid.
|
||||
break.goto_done
|
||||
}
|
||||
c = args.src.peek_u8()
|
||||
|
||||
assert args.src.length() > 0
|
||||
assert n <= 1
|
||||
}
|
||||
assert args.src.length() > 0
|
||||
assert n <= 1
|
||||
}
|
||||
|
||||
// Scan the opening digits.
|
||||
if c == '0' {
|
||||
n += 1
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
assert n <= 99
|
||||
} else {
|
||||
n = this.decode_digits!(src: args.src, n: n)
|
||||
if n > 99 {
|
||||
break.goto_done
|
||||
}
|
||||
assert n <= 99
|
||||
}
|
||||
// Scan the opening digits.
|
||||
if c == '0' {
|
||||
n += 1
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
assert n <= 99
|
||||
} else {
|
||||
n = this.decode_digits!(src: args.src, n: n)
|
||||
if n > 99 {
|
||||
break.goto_done
|
||||
}
|
||||
assert n <= 99
|
||||
}
|
||||
|
||||
// Peek.
|
||||
if args.src.length() <= 0 {
|
||||
if not args.src.is_closed() {
|
||||
n |= 0x300
|
||||
}
|
||||
break.goto_done
|
||||
}
|
||||
c = args.src.peek_u8()
|
||||
// Peek.
|
||||
if args.src.length() <= 0 {
|
||||
if not args.src.is_closed() {
|
||||
n |= 0x300
|
||||
}
|
||||
break.goto_done
|
||||
}
|
||||
c = args.src.peek_u8()
|
||||
|
||||
// Scan the optional fraction.
|
||||
if c <> '.' {
|
||||
assert args.src.length() > 0
|
||||
assert n <= 99
|
||||
} else {
|
||||
if n >= 99 {
|
||||
n |= 0x200
|
||||
break.goto_done
|
||||
}
|
||||
n += 1
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
floating_point = 0x80
|
||||
// Scan the optional fraction.
|
||||
if c <> '.' {
|
||||
assert args.src.length() > 0
|
||||
assert n <= 99
|
||||
} else {
|
||||
if n >= 99 {
|
||||
n |= 0x200
|
||||
break.goto_done
|
||||
}
|
||||
n += 1
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
floating_point = 0x80
|
||||
|
||||
n = this.decode_digits!(src: args.src, n: n)
|
||||
if n > 99 {
|
||||
break.goto_done
|
||||
}
|
||||
n = this.decode_digits!(src: args.src, n: n)
|
||||
if n > 99 {
|
||||
break.goto_done
|
||||
}
|
||||
|
||||
// Peek.
|
||||
if args.src.length() <= 0 {
|
||||
if not args.src.is_closed() {
|
||||
n |= 0x300
|
||||
}
|
||||
break.goto_done
|
||||
}
|
||||
c = args.src.peek_u8()
|
||||
// Peek.
|
||||
if args.src.length() <= 0 {
|
||||
if not args.src.is_closed() {
|
||||
n |= 0x300
|
||||
}
|
||||
break.goto_done
|
||||
}
|
||||
c = args.src.peek_u8()
|
||||
|
||||
assert args.src.length() > 0
|
||||
assert n <= 99
|
||||
}
|
||||
assert args.src.length() > 0
|
||||
assert n <= 99
|
||||
}
|
||||
|
||||
// Scan the optional 'E' or 'e'.
|
||||
if (c <> 'E') and (c <> 'e') {
|
||||
break.goto_done
|
||||
}
|
||||
if n >= 99 {
|
||||
n |= 0x200
|
||||
break.goto_done
|
||||
}
|
||||
n += 1
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
floating_point = 0x80
|
||||
assert n <= 99
|
||||
// Scan the optional 'E' or 'e'.
|
||||
if (c <> 'E') and (c <> 'e') {
|
||||
break.goto_done
|
||||
}
|
||||
if n >= 99 {
|
||||
n |= 0x200
|
||||
break.goto_done
|
||||
}
|
||||
n += 1
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
floating_point = 0x80
|
||||
assert n <= 99
|
||||
|
||||
// Peek.
|
||||
if args.src.length() <= 0 {
|
||||
if not args.src.is_closed() {
|
||||
n |= 0x300
|
||||
}
|
||||
n |= 0x100 // An 'E' or 'e' without digits is invalid.
|
||||
break.goto_done
|
||||
}
|
||||
c = args.src.peek_u8()
|
||||
// Peek.
|
||||
if args.src.length() <= 0 {
|
||||
if not args.src.is_closed() {
|
||||
n |= 0x300
|
||||
}
|
||||
n |= 0x100 // An 'E' or 'e' without digits is invalid.
|
||||
break.goto_done
|
||||
}
|
||||
c = args.src.peek_u8()
|
||||
|
||||
// Scan the optional '+' or '-'.
|
||||
if (c <> '+') and (c <> '-') {
|
||||
assert n <= 99
|
||||
} else {
|
||||
if n >= 99 {
|
||||
n |= 0x200
|
||||
break.goto_done
|
||||
}
|
||||
n += 1
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
assert n <= 99
|
||||
}
|
||||
// Scan the optional '+' or '-'.
|
||||
if (c <> '+') and (c <> '-') {
|
||||
assert n <= 99
|
||||
} else {
|
||||
if n >= 99 {
|
||||
n |= 0x200
|
||||
break.goto_done
|
||||
}
|
||||
n += 1
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
assert n <= 99
|
||||
}
|
||||
|
||||
// Scan the exponent digits.
|
||||
n = this.decode_digits!(src: args.src, n: n)
|
||||
// Scan the exponent digits.
|
||||
n = this.decode_digits!(src: args.src, n: n)
|
||||
|
||||
break.goto_done
|
||||
}} endwhile.goto_done
|
||||
break.goto_done
|
||||
}} endwhile.goto_done
|
||||
|
||||
return n | floating_point
|
||||
return n | floating_point
|
||||
}
|
||||
|
||||
pri func decoder.decode_digits!(src: base.io_reader, n: base.u32[..= 99]) base.u32[..= 0x3FF] {
|
||||
var c : base.u8
|
||||
var n : base.u32[..= 0x3FF]
|
||||
var c : base.u8
|
||||
var n : base.u32[..= 0x3FF]
|
||||
|
||||
n = args.n
|
||||
while true {
|
||||
if args.src.length() <= 0 {
|
||||
if not args.src.is_closed() {
|
||||
n |= 0x300
|
||||
}
|
||||
break
|
||||
}
|
||||
c = args.src.peek_u8()
|
||||
if 0x00 == LUT_DECIMAL_DIGITS[c] {
|
||||
break
|
||||
}
|
||||
// Cap DECODER_NUMBER_LENGTH_MAX_INCL at an arbitrary value, 99. The
|
||||
// caller's src.data.len should therefore be at least 100, also known
|
||||
// as DECODER_SRC_IO_BUFFER_LENGTH_MIN_INCL.
|
||||
//
|
||||
// An example of a JSON number that is 81 bytes long is:
|
||||
// https://github.com/nst/JSONTestSuite/blob/master/test_parsing/y_number_double_close_to_zero.json
|
||||
//
|
||||
// Note that 99 (in hex, 0x63) is less than 0x80, so we can use 0x80 as
|
||||
// a flag bit in func decoder.decode_number.
|
||||
if n >= 99 {
|
||||
n |= 0x200
|
||||
break
|
||||
}
|
||||
n += 1
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
} endwhile
|
||||
if n == args.n {
|
||||
n |= 0x100
|
||||
}
|
||||
return n
|
||||
n = args.n
|
||||
while true {
|
||||
if args.src.length() <= 0 {
|
||||
if not args.src.is_closed() {
|
||||
n |= 0x300
|
||||
}
|
||||
break
|
||||
}
|
||||
c = args.src.peek_u8()
|
||||
if 0x00 == LUT_DECIMAL_DIGITS[c] {
|
||||
break
|
||||
}
|
||||
// Cap DECODER_NUMBER_LENGTH_MAX_INCL at an arbitrary value, 99. The
|
||||
// caller's src.data.len should therefore be at least 100, also known
|
||||
// as DECODER_SRC_IO_BUFFER_LENGTH_MIN_INCL.
|
||||
//
|
||||
// An example of a JSON number that is 81 bytes long is:
|
||||
// https://github.com/nst/JSONTestSuite/blob/master/test_parsing/y_number_double_close_to_zero.json
|
||||
//
|
||||
// Note that 99 (in hex, 0x63) is less than 0x80, so we can use 0x80 as
|
||||
// a flag bit in func decoder.decode_number.
|
||||
if n >= 99 {
|
||||
n |= 0x200
|
||||
break
|
||||
}
|
||||
n += 1
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
} endwhile
|
||||
if n == args.n {
|
||||
n |= 0x100
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
pri func decoder.decode_leading?(dst: base.token_writer, src: base.io_reader) {
|
||||
var c : base.u8
|
||||
var u : base.u32
|
||||
var c : base.u8
|
||||
var u : base.u32
|
||||
|
||||
this.allow_leading_ars =
|
||||
this.quirks[QUIRK_ALLOW_LEADING_ASCII_RECORD_SEPARATOR - QUIRKS_BASE]
|
||||
this.allow_leading_ubom =
|
||||
this.quirks[QUIRK_ALLOW_LEADING_UNICODE_BYTE_ORDER_MARK - QUIRKS_BASE]
|
||||
this.allow_leading_ars =
|
||||
this.quirks[QUIRK_ALLOW_LEADING_ASCII_RECORD_SEPARATOR - QUIRKS_BASE]
|
||||
this.allow_leading_ubom =
|
||||
this.quirks[QUIRK_ALLOW_LEADING_UNICODE_BYTE_ORDER_MARK - QUIRKS_BASE]
|
||||
|
||||
while this.allow_leading_ars or this.allow_leading_ubom {
|
||||
if args.dst.length() <= 0 {
|
||||
yield? base."$short write"
|
||||
continue
|
||||
}
|
||||
if args.src.length() <= 0 {
|
||||
if args.src.is_closed() {
|
||||
break
|
||||
}
|
||||
yield? base."$short read"
|
||||
continue
|
||||
}
|
||||
c = args.src.peek_u8()
|
||||
if (c == 0x1E) and this.allow_leading_ars {
|
||||
this.allow_leading_ars = false
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0, value_minor: 0, continued: 0, length: 1)
|
||||
continue
|
||||
} else if (c == 0xEF) and this.allow_leading_ubom {
|
||||
if args.src.length() < 3 {
|
||||
if args.src.is_closed() {
|
||||
break
|
||||
}
|
||||
yield? base."$short read"
|
||||
continue
|
||||
}
|
||||
u = args.src.peek_u24le_as_u32()
|
||||
if u == 0xBF_BBEF {
|
||||
this.allow_leading_ubom = false
|
||||
args.src.skip_u32_fast!(actual: 3, worst_case: 3)
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0, value_minor: 0, continued: 0, length: 3)
|
||||
continue
|
||||
}
|
||||
}
|
||||
break
|
||||
} endwhile
|
||||
while this.allow_leading_ars or this.allow_leading_ubom {
|
||||
if args.dst.length() <= 0 {
|
||||
yield? base."$short write"
|
||||
continue
|
||||
}
|
||||
if args.src.length() <= 0 {
|
||||
if args.src.is_closed() {
|
||||
break
|
||||
}
|
||||
yield? base."$short read"
|
||||
continue
|
||||
}
|
||||
c = args.src.peek_u8()
|
||||
if (c == 0x1E) and this.allow_leading_ars {
|
||||
this.allow_leading_ars = false
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0, value_minor: 0, continued: 0, length: 1)
|
||||
continue
|
||||
} else if (c == 0xEF) and this.allow_leading_ubom {
|
||||
if args.src.length() < 3 {
|
||||
if args.src.is_closed() {
|
||||
break
|
||||
}
|
||||
yield? base."$short read"
|
||||
continue
|
||||
}
|
||||
u = args.src.peek_u24le_as_u32()
|
||||
if u == 0xBF_BBEF {
|
||||
this.allow_leading_ubom = false
|
||||
args.src.skip_u32_fast!(actual: 3, worst_case: 3)
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0, value_minor: 0, continued: 0, length: 3)
|
||||
continue
|
||||
}
|
||||
}
|
||||
break
|
||||
} endwhile
|
||||
}
|
||||
|
||||
pri func decoder.decode_comment?(dst: base.token_writer, src: base.io_reader) {
|
||||
var c : base.u8
|
||||
var c2 : base.u16
|
||||
var length : base.u32[..= 0xFFFD]
|
||||
var c : base.u8
|
||||
var c2 : base.u16
|
||||
var length : base.u32[..= 0xFFFD]
|
||||
|
||||
this.comment_type = 0
|
||||
this.comment_type = 0
|
||||
|
||||
while (args.dst.length() <= 0) or (args.src.length() <= 1),
|
||||
post args.dst.length() > 0,
|
||||
post args.src.length() > 1,
|
||||
{
|
||||
if args.dst.length() <= 0 {
|
||||
yield? base."$short write"
|
||||
continue
|
||||
}
|
||||
if args.src.is_closed() {
|
||||
return ok
|
||||
}
|
||||
yield? base."$short read"
|
||||
} endwhile
|
||||
c2 = args.src.peek_u16le()
|
||||
while (args.dst.length() <= 0) or (args.src.length() <= 1),
|
||||
post args.dst.length() > 0,
|
||||
post args.src.length() > 1,
|
||||
{
|
||||
if args.dst.length() <= 0 {
|
||||
yield? base."$short write"
|
||||
continue
|
||||
}
|
||||
if args.src.is_closed() {
|
||||
return ok
|
||||
}
|
||||
yield? base."$short read"
|
||||
} endwhile
|
||||
c2 = args.src.peek_u16le()
|
||||
|
||||
if (c2 == '/*'le) and this.quirks[QUIRK_ALLOW_COMMENT_BLOCK - QUIRKS_BASE] {
|
||||
args.src.skip_u32_fast!(actual: 2, worst_case: 2)
|
||||
length = 2
|
||||
if (c2 == '/*'le) and this.quirks[QUIRK_ALLOW_COMMENT_BLOCK - QUIRKS_BASE] {
|
||||
args.src.skip_u32_fast!(actual: 2, worst_case: 2)
|
||||
length = 2
|
||||
|
||||
while.comment_block true,
|
||||
pre args.dst.length() > 0,
|
||||
{
|
||||
if args.src.length() <= 1 {
|
||||
if length > 0 {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__FILLER << 21) |
|
||||
base.TOKEN__VBD__FILLER__COMMENT_BLOCK,
|
||||
continued: 1,
|
||||
length: length)
|
||||
}
|
||||
if args.src.is_closed() {
|
||||
return "#bad input"
|
||||
}
|
||||
yield? base."$short read"
|
||||
while args.dst.length() <= 0,
|
||||
post args.dst.length() > 0,
|
||||
{
|
||||
yield? base."$short write"
|
||||
} endwhile
|
||||
length = 0
|
||||
continue.comment_block
|
||||
}
|
||||
while.comment_block true,
|
||||
pre args.dst.length() > 0,
|
||||
{
|
||||
if args.src.length() <= 1 {
|
||||
if length > 0 {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__FILLER << 21) |
|
||||
base.TOKEN__VBD__FILLER__COMMENT_BLOCK,
|
||||
continued: 1,
|
||||
length: length)
|
||||
}
|
||||
if args.src.is_closed() {
|
||||
return "#bad input"
|
||||
}
|
||||
yield? base."$short read"
|
||||
while args.dst.length() <= 0,
|
||||
post args.dst.length() > 0,
|
||||
{
|
||||
yield? base."$short write"
|
||||
} endwhile
|
||||
length = 0
|
||||
continue.comment_block
|
||||
}
|
||||
|
||||
c2 = args.src.peek_u16le()
|
||||
if c2 == '*/'le {
|
||||
args.src.skip_u32_fast!(actual: 2, worst_case: 2)
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__FILLER << 21) |
|
||||
base.TOKEN__VBD__FILLER__COMMENT_BLOCK,
|
||||
continued: 0,
|
||||
length: length + 2)
|
||||
this.comment_type = 1
|
||||
return ok
|
||||
}
|
||||
c2 = args.src.peek_u16le()
|
||||
if c2 == '*/'le {
|
||||
args.src.skip_u32_fast!(actual: 2, worst_case: 2)
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__FILLER << 21) |
|
||||
base.TOKEN__VBD__FILLER__COMMENT_BLOCK,
|
||||
continued: 0,
|
||||
length: length + 2)
|
||||
this.comment_type = 1
|
||||
return ok
|
||||
}
|
||||
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
if length >= 0xFFFD {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__FILLER << 21) |
|
||||
base.TOKEN__VBD__FILLER__COMMENT_BLOCK,
|
||||
continued: 1,
|
||||
length: length + 1)
|
||||
while args.dst.length() <= 0,
|
||||
post args.dst.length() > 0,
|
||||
{
|
||||
yield? base."$short write"
|
||||
} endwhile
|
||||
length = 0
|
||||
continue.comment_block
|
||||
}
|
||||
length += 1
|
||||
} endwhile.comment_block
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
if length >= 0xFFFD {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__FILLER << 21) |
|
||||
base.TOKEN__VBD__FILLER__COMMENT_BLOCK,
|
||||
continued: 1,
|
||||
length: length + 1)
|
||||
while args.dst.length() <= 0,
|
||||
post args.dst.length() > 0,
|
||||
{
|
||||
yield? base."$short write"
|
||||
} endwhile
|
||||
length = 0
|
||||
continue.comment_block
|
||||
}
|
||||
length += 1
|
||||
} endwhile.comment_block
|
||||
|
||||
} else if (c2 == '//'le) and this.quirks[QUIRK_ALLOW_COMMENT_LINE - QUIRKS_BASE] {
|
||||
args.src.skip_u32_fast!(actual: 2, worst_case: 2)
|
||||
length = 2
|
||||
} else if (c2 == '//'le) and this.quirks[QUIRK_ALLOW_COMMENT_LINE - QUIRKS_BASE] {
|
||||
args.src.skip_u32_fast!(actual: 2, worst_case: 2)
|
||||
length = 2
|
||||
|
||||
while.comment_line true,
|
||||
pre args.dst.length() > 0,
|
||||
{
|
||||
if args.src.length() <= 0 {
|
||||
if args.src.is_closed() {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__FILLER << 21) |
|
||||
base.TOKEN__VBD__FILLER__COMMENT_LINE,
|
||||
continued: 0,
|
||||
length: length)
|
||||
this.comment_type = 2
|
||||
return ok
|
||||
} else if length > 0 {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__FILLER << 21) |
|
||||
base.TOKEN__VBD__FILLER__COMMENT_LINE,
|
||||
continued: 1,
|
||||
length: length)
|
||||
}
|
||||
yield? base."$short read"
|
||||
while args.dst.length() <= 0,
|
||||
post args.dst.length() > 0,
|
||||
{
|
||||
yield? base."$short write"
|
||||
} endwhile
|
||||
length = 0
|
||||
continue.comment_line
|
||||
}
|
||||
while.comment_line true,
|
||||
pre args.dst.length() > 0,
|
||||
{
|
||||
if args.src.length() <= 0 {
|
||||
if args.src.is_closed() {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__FILLER << 21) |
|
||||
base.TOKEN__VBD__FILLER__COMMENT_LINE,
|
||||
continued: 0,
|
||||
length: length)
|
||||
this.comment_type = 2
|
||||
return ok
|
||||
} else if length > 0 {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__FILLER << 21) |
|
||||
base.TOKEN__VBD__FILLER__COMMENT_LINE,
|
||||
continued: 1,
|
||||
length: length)
|
||||
}
|
||||
yield? base."$short read"
|
||||
while args.dst.length() <= 0,
|
||||
post args.dst.length() > 0,
|
||||
{
|
||||
yield? base."$short write"
|
||||
} endwhile
|
||||
length = 0
|
||||
continue.comment_line
|
||||
}
|
||||
|
||||
c = args.src.peek_u8()
|
||||
if c == '\n' {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__FILLER << 21) |
|
||||
base.TOKEN__VBD__FILLER__COMMENT_LINE,
|
||||
continued: 0,
|
||||
length: length)
|
||||
this.comment_type = 2
|
||||
return ok
|
||||
}
|
||||
c = args.src.peek_u8()
|
||||
if c == '\n' {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__FILLER << 21) |
|
||||
base.TOKEN__VBD__FILLER__COMMENT_LINE,
|
||||
continued: 0,
|
||||
length: length)
|
||||
this.comment_type = 2
|
||||
return ok
|
||||
}
|
||||
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
if length >= 0xFFFD {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__FILLER << 21) |
|
||||
base.TOKEN__VBD__FILLER__COMMENT_LINE,
|
||||
continued: 1,
|
||||
length: length + 1)
|
||||
while args.dst.length() <= 0,
|
||||
post args.dst.length() > 0,
|
||||
{
|
||||
yield? base."$short write"
|
||||
} endwhile
|
||||
length = 0
|
||||
continue.comment_line
|
||||
}
|
||||
length += 1
|
||||
} endwhile.comment_line
|
||||
}
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
if length >= 0xFFFD {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__FILLER << 21) |
|
||||
base.TOKEN__VBD__FILLER__COMMENT_LINE,
|
||||
continued: 1,
|
||||
length: length + 1)
|
||||
while args.dst.length() <= 0,
|
||||
post args.dst.length() > 0,
|
||||
{
|
||||
yield? base."$short write"
|
||||
} endwhile
|
||||
length = 0
|
||||
continue.comment_line
|
||||
}
|
||||
length += 1
|
||||
} endwhile.comment_line
|
||||
}
|
||||
}
|
||||
|
||||
pri func decoder.decode_inf_nan?(dst: base.token_writer, src: base.io_reader) {
|
||||
var c4 : base.u32
|
||||
var neg : base.u32[..= 1]
|
||||
var c4 : base.u32
|
||||
var neg : base.u32[..= 1]
|
||||
|
||||
while true {
|
||||
if args.dst.length() <= 0 {
|
||||
yield? base."$short write"
|
||||
continue
|
||||
}
|
||||
if args.src.length() <= 2 {
|
||||
if args.src.is_closed() {
|
||||
return "#bad input"
|
||||
}
|
||||
yield? base."$short read"
|
||||
continue
|
||||
}
|
||||
while true {
|
||||
if args.dst.length() <= 0 {
|
||||
yield? base."$short write"
|
||||
continue
|
||||
}
|
||||
if args.src.length() <= 2 {
|
||||
if args.src.is_closed() {
|
||||
return "#bad input"
|
||||
}
|
||||
yield? base."$short read"
|
||||
continue
|
||||
}
|
||||
|
||||
// Bitwise or'ing with 0x20 converts upper case ASCII to lower case.
|
||||
// Bitwise or'ing with 0x20 converts upper case ASCII to lower case.
|
||||
|
||||
c4 = args.src.peek_u24le_as_u32()
|
||||
if (c4 | 0x20_2020) == 'inf'le {
|
||||
if args.src.length() > 7 {
|
||||
if (args.src.peek_u64le() | 0x2020_2020_2020_2020) == 'infinity'le {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__NUMBER << 21) |
|
||||
base.TOKEN__VBD__NUMBER__CONTENT_POS_INF,
|
||||
continued: 0,
|
||||
length: 8)
|
||||
args.src.skip_u32_fast!(actual: 8, worst_case: 8)
|
||||
return ok
|
||||
}
|
||||
} else if not args.src.is_closed() {
|
||||
yield? base."$short read"
|
||||
continue
|
||||
}
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__NUMBER << 21) |
|
||||
base.TOKEN__VBD__NUMBER__CONTENT_POS_INF,
|
||||
continued: 0,
|
||||
length: 3)
|
||||
args.src.skip_u32_fast!(actual: 3, worst_case: 3)
|
||||
return ok
|
||||
c4 = args.src.peek_u24le_as_u32()
|
||||
if (c4 | 0x20_2020) == 'inf'le {
|
||||
if args.src.length() > 7 {
|
||||
if (args.src.peek_u64le() | 0x2020_2020_2020_2020) == 'infinity'le {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__NUMBER << 21) |
|
||||
base.TOKEN__VBD__NUMBER__CONTENT_POS_INF,
|
||||
continued: 0,
|
||||
length: 8)
|
||||
args.src.skip_u32_fast!(actual: 8, worst_case: 8)
|
||||
return ok
|
||||
}
|
||||
} else if not args.src.is_closed() {
|
||||
yield? base."$short read"
|
||||
continue
|
||||
}
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__NUMBER << 21) |
|
||||
base.TOKEN__VBD__NUMBER__CONTENT_POS_INF,
|
||||
continued: 0,
|
||||
length: 3)
|
||||
args.src.skip_u32_fast!(actual: 3, worst_case: 3)
|
||||
return ok
|
||||
|
||||
} else if (c4 | 0x20_2020) == 'nan'le {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__NUMBER << 21) |
|
||||
base.TOKEN__VBD__NUMBER__CONTENT_POS_NAN,
|
||||
continued: 0,
|
||||
length: 3)
|
||||
args.src.skip_u32_fast!(actual: 3, worst_case: 3)
|
||||
return ok
|
||||
} else if (c4 & 0xFF) == '+' {
|
||||
neg = 0
|
||||
} else if (c4 & 0xFF) == '-' {
|
||||
neg = 1
|
||||
} else {
|
||||
return "#bad input"
|
||||
}
|
||||
} else if (c4 | 0x20_2020) == 'nan'le {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__NUMBER << 21) |
|
||||
base.TOKEN__VBD__NUMBER__CONTENT_POS_NAN,
|
||||
continued: 0,
|
||||
length: 3)
|
||||
args.src.skip_u32_fast!(actual: 3, worst_case: 3)
|
||||
return ok
|
||||
} else if (c4 & 0xFF) == '+' {
|
||||
neg = 0
|
||||
} else if (c4 & 0xFF) == '-' {
|
||||
neg = 1
|
||||
} else {
|
||||
return "#bad input"
|
||||
}
|
||||
|
||||
if args.src.length() <= 3 {
|
||||
if args.src.is_closed() {
|
||||
return "#bad input"
|
||||
}
|
||||
yield? base."$short read"
|
||||
continue
|
||||
}
|
||||
if args.src.length() <= 3 {
|
||||
if args.src.is_closed() {
|
||||
return "#bad input"
|
||||
}
|
||||
yield? base."$short read"
|
||||
continue
|
||||
}
|
||||
|
||||
c4 = args.src.peek_u32le() >> 8
|
||||
if (c4 | 0x20_2020) == 'inf'le {
|
||||
if args.src.length() > 8 {
|
||||
if (args.src.peek_u64le_at(offset: 1) | 0x2020_2020_2020_2020) == 'infinity'le {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__NUMBER << 21) |
|
||||
(base.TOKEN__VBD__NUMBER__CONTENT_POS_INF >> neg),
|
||||
continued: 0,
|
||||
length: 9)
|
||||
args.src.skip_u32_fast!(actual: 9, worst_case: 9)
|
||||
return ok
|
||||
}
|
||||
} else if not args.src.is_closed() {
|
||||
yield? base."$short read"
|
||||
continue
|
||||
}
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__NUMBER << 21) |
|
||||
(base.TOKEN__VBD__NUMBER__CONTENT_POS_INF >> neg),
|
||||
continued: 0,
|
||||
length: 4)
|
||||
args.src.skip_u32_fast!(actual: 4, worst_case: 4)
|
||||
return ok
|
||||
c4 = args.src.peek_u32le() >> 8
|
||||
if (c4 | 0x20_2020) == 'inf'le {
|
||||
if args.src.length() > 8 {
|
||||
if (args.src.peek_u64le_at(offset: 1) | 0x2020_2020_2020_2020) == 'infinity'le {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__NUMBER << 21) |
|
||||
(base.TOKEN__VBD__NUMBER__CONTENT_POS_INF >> neg),
|
||||
continued: 0,
|
||||
length: 9)
|
||||
args.src.skip_u32_fast!(actual: 9, worst_case: 9)
|
||||
return ok
|
||||
}
|
||||
} else if not args.src.is_closed() {
|
||||
yield? base."$short read"
|
||||
continue
|
||||
}
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__NUMBER << 21) |
|
||||
(base.TOKEN__VBD__NUMBER__CONTENT_POS_INF >> neg),
|
||||
continued: 0,
|
||||
length: 4)
|
||||
args.src.skip_u32_fast!(actual: 4, worst_case: 4)
|
||||
return ok
|
||||
|
||||
} else if (c4 | 0x20_2020) == 'nan'le {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__NUMBER << 21) |
|
||||
(base.TOKEN__VBD__NUMBER__CONTENT_POS_NAN >> neg),
|
||||
continued: 0,
|
||||
length: 4)
|
||||
args.src.skip_u32_fast!(actual: 4, worst_case: 4)
|
||||
return ok
|
||||
}
|
||||
} else if (c4 | 0x20_2020) == 'nan'le {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0,
|
||||
value_minor: (base.TOKEN__VBC__NUMBER << 21) |
|
||||
(base.TOKEN__VBD__NUMBER__CONTENT_POS_NAN >> neg),
|
||||
continued: 0,
|
||||
length: 4)
|
||||
args.src.skip_u32_fast!(actual: 4, worst_case: 4)
|
||||
return ok
|
||||
}
|
||||
|
||||
return "#bad input"
|
||||
} endwhile
|
||||
return "#bad input"
|
||||
} endwhile
|
||||
}
|
||||
|
||||
pri func decoder.decode_trailer?(dst: base.token_writer, src: base.io_reader) {
|
||||
var c : base.u8
|
||||
var whitespace_length : base.u32[..= 0xFFFE]
|
||||
var c : base.u8
|
||||
var whitespace_length : base.u32[..= 0xFFFE]
|
||||
|
||||
if this.quirks[QUIRK_EXPECT_TRAILING_NEW_LINE_OR_EOF - QUIRKS_BASE] {
|
||||
this.trailer_stop = '\n'
|
||||
} else {
|
||||
this.trailer_stop = 0
|
||||
}
|
||||
if this.quirks[QUIRK_EXPECT_TRAILING_NEW_LINE_OR_EOF - QUIRKS_BASE] {
|
||||
this.trailer_stop = '\n'
|
||||
} else {
|
||||
this.trailer_stop = 0
|
||||
}
|
||||
|
||||
while.outer true {
|
||||
if args.dst.length() <= 0 {
|
||||
yield? base."$short write"
|
||||
continue.outer
|
||||
}
|
||||
while.outer true {
|
||||
if args.dst.length() <= 0 {
|
||||
yield? base."$short write"
|
||||
continue.outer
|
||||
}
|
||||
|
||||
whitespace_length = 0
|
||||
while.inner true,
|
||||
pre args.dst.length() > 0,
|
||||
{
|
||||
if args.src.length() <= 0 {
|
||||
if whitespace_length > 0 {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0, value_minor: 0, continued: 0, length: whitespace_length)
|
||||
}
|
||||
if args.src.is_closed() {
|
||||
break.outer
|
||||
}
|
||||
yield? base."$short read"
|
||||
continue.outer
|
||||
}
|
||||
whitespace_length = 0
|
||||
while.inner true,
|
||||
pre args.dst.length() > 0,
|
||||
{
|
||||
if args.src.length() <= 0 {
|
||||
if whitespace_length > 0 {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0, value_minor: 0, continued: 0, length: whitespace_length)
|
||||
}
|
||||
if args.src.is_closed() {
|
||||
break.outer
|
||||
}
|
||||
yield? base."$short read"
|
||||
continue.outer
|
||||
}
|
||||
|
||||
c = args.src.peek_u8()
|
||||
if LUT_CLASSES[c] <> CLASS_WHITESPACE {
|
||||
if whitespace_length > 0 {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0, value_minor: 0, continued: 0, length: whitespace_length)
|
||||
}
|
||||
if this.trailer_stop > 0 {
|
||||
return "#bad input"
|
||||
}
|
||||
this.decode_comment?(dst: args.dst, src: args.src)
|
||||
if this.comment_type > 0 {
|
||||
continue.outer
|
||||
}
|
||||
return ok
|
||||
}
|
||||
c = args.src.peek_u8()
|
||||
if LUT_CLASSES[c] <> CLASS_WHITESPACE {
|
||||
if whitespace_length > 0 {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0, value_minor: 0, continued: 0, length: whitespace_length)
|
||||
}
|
||||
if this.trailer_stop > 0 {
|
||||
return "#bad input"
|
||||
}
|
||||
this.decode_comment?(dst: args.dst, src: args.src)
|
||||
if this.comment_type > 0 {
|
||||
continue.outer
|
||||
}
|
||||
return ok
|
||||
}
|
||||
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
if (whitespace_length >= 0xFFFE) or (c == this.trailer_stop) {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0, value_minor: 0, continued: 0, length: whitespace_length + 1)
|
||||
if c == this.trailer_stop {
|
||||
return ok
|
||||
}
|
||||
continue.outer
|
||||
}
|
||||
whitespace_length += 1
|
||||
} endwhile.inner
|
||||
} endwhile.outer
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
if (whitespace_length >= 0xFFFE) or (c == this.trailer_stop) {
|
||||
args.dst.write_simple_token_fast!(
|
||||
value_major: 0, value_minor: 0, continued: 0, length: whitespace_length + 1)
|
||||
if c == this.trailer_stop {
|
||||
return ok
|
||||
}
|
||||
continue.outer
|
||||
}
|
||||
whitespace_length += 1
|
||||
} endwhile.inner
|
||||
} endwhile.outer
|
||||
}
|
||||
+286
-286
@@ -28,349 +28,349 @@ pri status "#internal error: inconsistent I/O"
|
||||
pub const DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE : base.u64 = 0
|
||||
|
||||
pub struct decoder? implements base.io_transformer(
|
||||
// set_literal_width_arg is 1 plus the saved argument passed to
|
||||
// set_literal_width. This is assigned to the literal_width field at the
|
||||
// start of transform_io. During that method, calling set_literal_width
|
||||
// will change set_literal_width_arg but not literal_width.
|
||||
set_literal_width_arg : base.u32[..= 9],
|
||||
// set_literal_width_arg is 1 plus the saved argument passed to
|
||||
// set_literal_width. This is assigned to the literal_width field at the
|
||||
// start of transform_io. During that method, calling set_literal_width
|
||||
// will change set_literal_width_arg but not literal_width.
|
||||
set_literal_width_arg : base.u32[..= 9],
|
||||
|
||||
// read_from state that does not change during a decode call.
|
||||
literal_width : base.u32[..= 8],
|
||||
clear_code : base.u32[..= 256],
|
||||
end_code : base.u32[..= 257],
|
||||
// read_from state that does not change during a decode call.
|
||||
literal_width : base.u32[..= 8],
|
||||
clear_code : base.u32[..= 256],
|
||||
end_code : base.u32[..= 257],
|
||||
|
||||
// read_from state that does change during a decode call.
|
||||
save_code : base.u32[..= 4096],
|
||||
prev_code : base.u32[..= 4095],
|
||||
width : base.u32[..= 12],
|
||||
bits : base.u32,
|
||||
n_bits : base.u32[..= 31],
|
||||
output_ri : base.u32[..= 8191],
|
||||
output_wi : base.u32[..= 8191],
|
||||
// read_from state that does change during a decode call.
|
||||
save_code : base.u32[..= 4096],
|
||||
prev_code : base.u32[..= 4095],
|
||||
width : base.u32[..= 12],
|
||||
bits : base.u32,
|
||||
n_bits : base.u32[..= 31],
|
||||
output_ri : base.u32[..= 8191],
|
||||
output_wi : base.u32[..= 8191],
|
||||
|
||||
// read_from return value. The read_from method effectively returns a
|
||||
// base.u32 to show how decode should continue after calling write_to. That
|
||||
// value needs to be saved across write_to's possible suspension, so we
|
||||
// might as well save it explicitly as a decoder field.
|
||||
read_from_return_value : base.u32,
|
||||
// read_from return value. The read_from method effectively returns a
|
||||
// base.u32 to show how decode should continue after calling write_to. That
|
||||
// value needs to be saved across write_to's possible suspension, so we
|
||||
// might as well save it explicitly as a decoder field.
|
||||
read_from_return_value : base.u32,
|
||||
|
||||
// read_from per-code state.
|
||||
prefixes : array[4096] base.u16[..= 4095],
|
||||
// read_from per-code state.
|
||||
prefixes : array[4096] base.u16[..= 4095],
|
||||
|
||||
util : base.utility,
|
||||
util : base.utility,
|
||||
)(
|
||||
// read_from per-code state.
|
||||
suffixes : array[4096] array[8] base.u8,
|
||||
// lm1s is the "length minus 1"s of the values for the implicit key-value
|
||||
// table in this decoder. See std/lzw/README.md for more detail.
|
||||
lm1s : array[4096] base.u16,
|
||||
// read_from per-code state.
|
||||
suffixes : array[4096] array[8] base.u8,
|
||||
// lm1s is the "length minus 1"s of the values for the implicit key-value
|
||||
// table in this decoder. See std/lzw/README.md for more detail.
|
||||
lm1s : array[4096] base.u16,
|
||||
|
||||
// output[output_ri:output_wi] is the buffered output, connecting read_from
|
||||
// with write_to and flush.
|
||||
output : array[8192 + 7] base.u8,
|
||||
// output[output_ri:output_wi] is the buffered output, connecting read_from
|
||||
// with write_to and flush.
|
||||
output : array[8192 + 7] base.u8,
|
||||
)
|
||||
|
||||
pub func decoder.set_quirk_enabled!(quirk: base.u32, enabled: base.bool) {
|
||||
}
|
||||
|
||||
pub func decoder.set_literal_width!(lw: base.u32[..= 8]) {
|
||||
this.set_literal_width_arg = args.lw + 1
|
||||
this.set_literal_width_arg = args.lw + 1
|
||||
}
|
||||
|
||||
pub func decoder.workbuf_len() base.range_ii_u64 {
|
||||
return this.util.make_range_ii_u64(min_incl: 0, max_incl: 0)
|
||||
return this.util.make_range_ii_u64(min_incl: 0, max_incl: 0)
|
||||
}
|
||||
|
||||
pub func decoder.transform_io?(dst: base.io_writer, src: base.io_reader, workbuf: slice base.u8) {
|
||||
var i : base.u32[..= 8191]
|
||||
var i : base.u32[..= 8191]
|
||||
|
||||
// Initialize read_from state.
|
||||
this.literal_width = 8
|
||||
if this.set_literal_width_arg > 0 {
|
||||
this.literal_width = this.set_literal_width_arg - 1
|
||||
}
|
||||
this.clear_code = (1 as base.u32) << this.literal_width
|
||||
this.end_code = this.clear_code + 1
|
||||
this.save_code = this.end_code
|
||||
this.prev_code = this.end_code
|
||||
this.width = this.literal_width + 1
|
||||
this.bits = 0
|
||||
this.n_bits = 0
|
||||
this.output_ri = 0
|
||||
this.output_wi = 0
|
||||
i = 0
|
||||
while i < this.clear_code {
|
||||
assert i < 256 via "a < b: a < c; c <= b"(c: this.clear_code)
|
||||
this.lm1s[i] = 0
|
||||
this.suffixes[i][0] = i as base.u8
|
||||
i += 1
|
||||
} endwhile
|
||||
// Initialize read_from state.
|
||||
this.literal_width = 8
|
||||
if this.set_literal_width_arg > 0 {
|
||||
this.literal_width = this.set_literal_width_arg - 1
|
||||
}
|
||||
this.clear_code = (1 as base.u32) << this.literal_width
|
||||
this.end_code = this.clear_code + 1
|
||||
this.save_code = this.end_code
|
||||
this.prev_code = this.end_code
|
||||
this.width = this.literal_width + 1
|
||||
this.bits = 0
|
||||
this.n_bits = 0
|
||||
this.output_ri = 0
|
||||
this.output_wi = 0
|
||||
i = 0
|
||||
while i < this.clear_code {
|
||||
assert i < 256 via "a < b: a < c; c <= b"(c: this.clear_code)
|
||||
this.lm1s[i] = 0
|
||||
this.suffixes[i][0] = i as base.u8
|
||||
i += 1
|
||||
} endwhile
|
||||
|
||||
while true {
|
||||
this.read_from!(src: args.src)
|
||||
while true {
|
||||
this.read_from!(src: args.src)
|
||||
|
||||
if this.output_wi > 0 {
|
||||
this.write_to?(dst: args.dst)
|
||||
}
|
||||
if this.output_wi > 0 {
|
||||
this.write_to?(dst: args.dst)
|
||||
}
|
||||
|
||||
if this.read_from_return_value == 0 {
|
||||
break
|
||||
} else if this.read_from_return_value == 1 {
|
||||
continue
|
||||
} else if this.read_from_return_value == 2 {
|
||||
yield? base."$short read"
|
||||
} else if this.read_from_return_value == 3 {
|
||||
return "#truncated input"
|
||||
} else if this.read_from_return_value == 4 {
|
||||
return "#bad code"
|
||||
} else {
|
||||
return "#internal error: inconsistent I/O"
|
||||
}
|
||||
} endwhile
|
||||
if this.read_from_return_value == 0 {
|
||||
break
|
||||
} else if this.read_from_return_value == 1 {
|
||||
continue
|
||||
} else if this.read_from_return_value == 2 {
|
||||
yield? base."$short read"
|
||||
} else if this.read_from_return_value == 3 {
|
||||
return "#truncated input"
|
||||
} else if this.read_from_return_value == 4 {
|
||||
return "#bad code"
|
||||
} else {
|
||||
return "#internal error: inconsistent I/O"
|
||||
}
|
||||
} endwhile
|
||||
}
|
||||
|
||||
pri func decoder.read_from!(src: base.io_reader) {
|
||||
var clear_code : base.u32[..= 256]
|
||||
var end_code : base.u32[..= 257]
|
||||
var clear_code : base.u32[..= 256]
|
||||
var end_code : base.u32[..= 257]
|
||||
|
||||
var save_code : base.u32[..= 4096]
|
||||
var prev_code : base.u32[..= 4095]
|
||||
var width : base.u32[..= 12]
|
||||
var bits : base.u32
|
||||
var n_bits : base.u32[..= 31]
|
||||
var output_wi : base.u32[..= 8191]
|
||||
var save_code : base.u32[..= 4096]
|
||||
var prev_code : base.u32[..= 4095]
|
||||
var width : base.u32[..= 12]
|
||||
var bits : base.u32
|
||||
var n_bits : base.u32[..= 31]
|
||||
var output_wi : base.u32[..= 8191]
|
||||
|
||||
var code : base.u32[..= 4095]
|
||||
var c : base.u32[..= 4095]
|
||||
var o : base.u32[..= 8191]
|
||||
var steps : base.u32
|
||||
var first_byte : base.u8
|
||||
var lm1_b : base.u16[..= 4095]
|
||||
var lm1_a : base.u16[..= 4095]
|
||||
var code : base.u32[..= 4095]
|
||||
var c : base.u32[..= 4095]
|
||||
var o : base.u32[..= 8191]
|
||||
var steps : base.u32
|
||||
var first_byte : base.u8
|
||||
var lm1_b : base.u16[..= 4095]
|
||||
var lm1_a : base.u16[..= 4095]
|
||||
|
||||
clear_code = this.clear_code
|
||||
end_code = this.end_code
|
||||
clear_code = this.clear_code
|
||||
end_code = this.end_code
|
||||
|
||||
save_code = this.save_code
|
||||
prev_code = this.prev_code
|
||||
width = this.width
|
||||
bits = this.bits
|
||||
n_bits = this.n_bits
|
||||
output_wi = this.output_wi
|
||||
save_code = this.save_code
|
||||
prev_code = this.prev_code
|
||||
width = this.width
|
||||
bits = this.bits
|
||||
n_bits = this.n_bits
|
||||
output_wi = this.output_wi
|
||||
|
||||
while true {
|
||||
if n_bits < width {
|
||||
assert n_bits < 12 via "a < b: a < c; c <= b"(c: width)
|
||||
if args.src.length() >= 4 {
|
||||
// Read 4 bytes, using the "Variant 4" technique of
|
||||
// https://fgiesen.wordpress.com/2018/02/20/reading-bits-in-far-too-many-ways-part-2/
|
||||
bits |= args.src.peek_u32le() ~mod<< n_bits
|
||||
args.src.skip_u32_fast!(actual: (31 - n_bits) >> 3, worst_case: 3)
|
||||
n_bits |= 24
|
||||
assert width <= n_bits via "a <= b: a <= c; c <= b"(c: 12)
|
||||
assert n_bits >= width via "a >= b: b <= a"()
|
||||
} else if args.src.length() <= 0 {
|
||||
if args.src.is_closed() {
|
||||
this.read_from_return_value = 3
|
||||
} else {
|
||||
this.read_from_return_value = 2
|
||||
}
|
||||
break
|
||||
} else {
|
||||
bits |= args.src.peek_u8_as_u32() << n_bits
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
n_bits += 8
|
||||
if n_bits >= width {
|
||||
// No-op.
|
||||
} else if args.src.length() <= 0 {
|
||||
if args.src.is_closed() {
|
||||
this.read_from_return_value = 3
|
||||
} else {
|
||||
this.read_from_return_value = 2
|
||||
}
|
||||
break
|
||||
} else {
|
||||
bits |= args.src.peek_u8_as_u32() << n_bits
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
n_bits += 8
|
||||
assert width <= n_bits via "a <= b: a <= c; c <= b"(c: 12)
|
||||
assert n_bits >= width via "a >= b: b <= a"()
|
||||
while true {
|
||||
if n_bits < width {
|
||||
assert n_bits < 12 via "a < b: a < c; c <= b"(c: width)
|
||||
if args.src.length() >= 4 {
|
||||
// Read 4 bytes, using the "Variant 4" technique of
|
||||
// https://fgiesen.wordpress.com/2018/02/20/reading-bits-in-far-too-many-ways-part-2/
|
||||
bits |= args.src.peek_u32le() ~mod<< n_bits
|
||||
args.src.skip_u32_fast!(actual: (31 - n_bits) >> 3, worst_case: 3)
|
||||
n_bits |= 24
|
||||
assert width <= n_bits via "a <= b: a <= c; c <= b"(c: 12)
|
||||
assert n_bits >= width via "a >= b: b <= a"()
|
||||
} else if args.src.length() <= 0 {
|
||||
if args.src.is_closed() {
|
||||
this.read_from_return_value = 3
|
||||
} else {
|
||||
this.read_from_return_value = 2
|
||||
}
|
||||
break
|
||||
} else {
|
||||
bits |= args.src.peek_u8_as_u32() << n_bits
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
n_bits += 8
|
||||
if n_bits >= width {
|
||||
// No-op.
|
||||
} else if args.src.length() <= 0 {
|
||||
if args.src.is_closed() {
|
||||
this.read_from_return_value = 3
|
||||
} else {
|
||||
this.read_from_return_value = 2
|
||||
}
|
||||
break
|
||||
} else {
|
||||
bits |= args.src.peek_u8_as_u32() << n_bits
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
n_bits += 8
|
||||
assert width <= n_bits via "a <= b: a <= c; c <= b"(c: 12)
|
||||
assert n_bits >= width via "a >= b: b <= a"()
|
||||
|
||||
// This if condition is always false, but for some unknown
|
||||
// reason, removing it worsens the benchmarks slightly.
|
||||
if n_bits < width {
|
||||
this.read_from_return_value = 5
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// This if condition is always false, but for some unknown
|
||||
// reason, removing it worsens the benchmarks slightly.
|
||||
if n_bits < width {
|
||||
this.read_from_return_value = 5
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
code = bits.low_bits(n: width)
|
||||
bits >>= width
|
||||
n_bits -= width
|
||||
code = bits.low_bits(n: width)
|
||||
bits >>= width
|
||||
n_bits -= width
|
||||
|
||||
if code < clear_code {
|
||||
assert code < 256 via "a < b: a < c; c <= b"(c: clear_code)
|
||||
this.output[output_wi] = code as base.u8
|
||||
output_wi = (output_wi + 1) & 8191
|
||||
if save_code <= 4095 {
|
||||
lm1_a = (this.lm1s[prev_code] ~mod+ 1) & 4095
|
||||
this.lm1s[save_code] = lm1_a
|
||||
if code < clear_code {
|
||||
assert code < 256 via "a < b: a < c; c <= b"(c: clear_code)
|
||||
this.output[output_wi] = code as base.u8
|
||||
output_wi = (output_wi + 1) & 8191
|
||||
if save_code <= 4095 {
|
||||
lm1_a = (this.lm1s[prev_code] ~mod+ 1) & 4095
|
||||
this.lm1s[save_code] = lm1_a
|
||||
|
||||
if (lm1_a % 8) <> 0 {
|
||||
this.prefixes[save_code] = this.prefixes[prev_code]
|
||||
this.suffixes[save_code] = this.suffixes[prev_code]
|
||||
this.suffixes[save_code][lm1_a % 8] = code as base.u8
|
||||
} else {
|
||||
this.prefixes[save_code] = prev_code as base.u16
|
||||
this.suffixes[save_code][0] = code as base.u8
|
||||
}
|
||||
if (lm1_a % 8) <> 0 {
|
||||
this.prefixes[save_code] = this.prefixes[prev_code]
|
||||
this.suffixes[save_code] = this.suffixes[prev_code]
|
||||
this.suffixes[save_code][lm1_a % 8] = code as base.u8
|
||||
} else {
|
||||
this.prefixes[save_code] = prev_code as base.u16
|
||||
this.suffixes[save_code][0] = code as base.u8
|
||||
}
|
||||
|
||||
save_code += 1
|
||||
if width < 12 {
|
||||
width += 1 & (save_code >> width)
|
||||
}
|
||||
prev_code = code
|
||||
}
|
||||
save_code += 1
|
||||
if width < 12 {
|
||||
width += 1 & (save_code >> width)
|
||||
}
|
||||
prev_code = code
|
||||
}
|
||||
|
||||
} else if code <= end_code {
|
||||
if code == end_code {
|
||||
this.read_from_return_value = 0
|
||||
break
|
||||
}
|
||||
save_code = end_code
|
||||
prev_code = end_code
|
||||
width = this.literal_width + 1
|
||||
} else if code <= end_code {
|
||||
if code == end_code {
|
||||
this.read_from_return_value = 0
|
||||
break
|
||||
}
|
||||
save_code = end_code
|
||||
prev_code = end_code
|
||||
width = this.literal_width + 1
|
||||
|
||||
} else if code <= save_code {
|
||||
c = code
|
||||
if code == save_code {
|
||||
c = prev_code
|
||||
}
|
||||
} else if code <= save_code {
|
||||
c = code
|
||||
if code == save_code {
|
||||
c = prev_code
|
||||
}
|
||||
|
||||
// Letting old_wi and new_wi denote the values of output_wi before
|
||||
// and after these two lines of code, the decoded bytes will be
|
||||
// written to output[old_wi:new_wi]. They will be written
|
||||
// back-to-front, 8 bytes at a time, starting by writing
|
||||
// output[o:o + 8], which will contain output[new_wi - 1].
|
||||
//
|
||||
// In the special case that code == save_code, the decoded bytes
|
||||
// contain an extra copy (at the end) of the first byte, and will
|
||||
// be written to output[old_wi:new_wi + 1].
|
||||
o = (output_wi + ((this.lm1s[c] as base.u32) & 0xFFFF_FFF8)) & 8191
|
||||
output_wi = (output_wi + 1 + (this.lm1s[c] as base.u32)) & 8191
|
||||
// Letting old_wi and new_wi denote the values of output_wi before
|
||||
// and after these two lines of code, the decoded bytes will be
|
||||
// written to output[old_wi:new_wi]. They will be written
|
||||
// back-to-front, 8 bytes at a time, starting by writing
|
||||
// output[o:o + 8], which will contain output[new_wi - 1].
|
||||
//
|
||||
// In the special case that code == save_code, the decoded bytes
|
||||
// contain an extra copy (at the end) of the first byte, and will
|
||||
// be written to output[old_wi:new_wi + 1].
|
||||
o = (output_wi + ((this.lm1s[c] as base.u32) & 0xFFFF_FFF8)) & 8191
|
||||
output_wi = (output_wi + 1 + (this.lm1s[c] as base.u32)) & 8191
|
||||
|
||||
steps = (this.lm1s[c] as base.u32) >> 3
|
||||
while true {
|
||||
assert o <= (o + 8) via "a <= (a + b): 0 <= b"(b: 8)
|
||||
steps = (this.lm1s[c] as base.u32) >> 3
|
||||
while true {
|
||||
assert o <= (o + 8) via "a <= (a + b): 0 <= b"(b: 8)
|
||||
|
||||
// The final "8" is redundant semantically, but helps the
|
||||
// wuffs-c code generator recognize that both slices have the
|
||||
// same constant length, and hence produce efficient C code.
|
||||
this.output[o .. o + 8].copy_from_slice!(s: this.suffixes[c][.. 8])
|
||||
// The final "8" is redundant semantically, but helps the
|
||||
// wuffs-c code generator recognize that both slices have the
|
||||
// same constant length, and hence produce efficient C code.
|
||||
this.output[o .. o + 8].copy_from_slice!(s: this.suffixes[c][.. 8])
|
||||
|
||||
if steps <= 0 {
|
||||
break
|
||||
}
|
||||
steps -= 1
|
||||
if steps <= 0 {
|
||||
break
|
||||
}
|
||||
steps -= 1
|
||||
|
||||
// This line is essentially "o -= 8". The "& 8191" is a no-op
|
||||
// in practice, but is necessary for the overflow checker.
|
||||
o = (o ~mod- 8) & 8191
|
||||
c = this.prefixes[c] as base.u32
|
||||
} endwhile
|
||||
first_byte = this.suffixes[c][0]
|
||||
// This line is essentially "o -= 8". The "& 8191" is a no-op
|
||||
// in practice, but is necessary for the overflow checker.
|
||||
o = (o ~mod- 8) & 8191
|
||||
c = this.prefixes[c] as base.u32
|
||||
} endwhile
|
||||
first_byte = this.suffixes[c][0]
|
||||
|
||||
if code == save_code {
|
||||
this.output[output_wi] = first_byte
|
||||
output_wi = (output_wi + 1) & 8191
|
||||
}
|
||||
if code == save_code {
|
||||
this.output[output_wi] = first_byte
|
||||
output_wi = (output_wi + 1) & 8191
|
||||
}
|
||||
|
||||
if save_code <= 4095 {
|
||||
lm1_b = (this.lm1s[prev_code] ~mod+ 1) & 4095
|
||||
this.lm1s[save_code] = lm1_b
|
||||
if save_code <= 4095 {
|
||||
lm1_b = (this.lm1s[prev_code] ~mod+ 1) & 4095
|
||||
this.lm1s[save_code] = lm1_b
|
||||
|
||||
if (lm1_b % 8) <> 0 {
|
||||
this.prefixes[save_code] = this.prefixes[prev_code]
|
||||
this.suffixes[save_code] = this.suffixes[prev_code]
|
||||
this.suffixes[save_code][lm1_b % 8] = first_byte
|
||||
} else {
|
||||
this.prefixes[save_code] = prev_code as base.u16
|
||||
this.suffixes[save_code][0] = first_byte as base.u8
|
||||
}
|
||||
if (lm1_b % 8) <> 0 {
|
||||
this.prefixes[save_code] = this.prefixes[prev_code]
|
||||
this.suffixes[save_code] = this.suffixes[prev_code]
|
||||
this.suffixes[save_code][lm1_b % 8] = first_byte
|
||||
} else {
|
||||
this.prefixes[save_code] = prev_code as base.u16
|
||||
this.suffixes[save_code][0] = first_byte as base.u8
|
||||
}
|
||||
|
||||
save_code += 1
|
||||
if width < 12 {
|
||||
width += 1 & (save_code >> width)
|
||||
}
|
||||
prev_code = code
|
||||
}
|
||||
save_code += 1
|
||||
if width < 12 {
|
||||
width += 1 & (save_code >> width)
|
||||
}
|
||||
prev_code = code
|
||||
}
|
||||
|
||||
} else {
|
||||
this.read_from_return_value = 4
|
||||
break
|
||||
}
|
||||
} else {
|
||||
this.read_from_return_value = 4
|
||||
break
|
||||
}
|
||||
|
||||
// Flush the output if it could be too full to contain the entire
|
||||
// decoding of the next code. The longest possible decoding is slightly
|
||||
// less than 4096 and output's length is 8192, so a conservative
|
||||
// threshold is ensuring that output_wi <= 4095.
|
||||
if output_wi > 4095 {
|
||||
this.read_from_return_value = 1
|
||||
break
|
||||
}
|
||||
} endwhile
|
||||
// Flush the output if it could be too full to contain the entire
|
||||
// decoding of the next code. The longest possible decoding is slightly
|
||||
// less than 4096 and output's length is 8192, so a conservative
|
||||
// threshold is ensuring that output_wi <= 4095.
|
||||
if output_wi > 4095 {
|
||||
this.read_from_return_value = 1
|
||||
break
|
||||
}
|
||||
} endwhile
|
||||
|
||||
// Rewind args.src, if we're not in "$short read" and we've read too many
|
||||
// bits.
|
||||
if this.read_from_return_value <> 2 {
|
||||
while n_bits >= 8 {
|
||||
n_bits -= 8
|
||||
if args.src.can_undo_byte() {
|
||||
args.src.undo_byte!()
|
||||
} else {
|
||||
this.read_from_return_value = 5
|
||||
break
|
||||
}
|
||||
} endwhile
|
||||
}
|
||||
// Rewind args.src, if we're not in "$short read" and we've read too many
|
||||
// bits.
|
||||
if this.read_from_return_value <> 2 {
|
||||
while n_bits >= 8 {
|
||||
n_bits -= 8
|
||||
if args.src.can_undo_byte() {
|
||||
args.src.undo_byte!()
|
||||
} else {
|
||||
this.read_from_return_value = 5
|
||||
break
|
||||
}
|
||||
} endwhile
|
||||
}
|
||||
|
||||
this.save_code = save_code
|
||||
this.prev_code = prev_code
|
||||
this.width = width
|
||||
this.bits = bits
|
||||
this.n_bits = n_bits
|
||||
this.output_wi = output_wi
|
||||
this.save_code = save_code
|
||||
this.prev_code = prev_code
|
||||
this.width = width
|
||||
this.bits = bits
|
||||
this.n_bits = n_bits
|
||||
this.output_wi = output_wi
|
||||
}
|
||||
|
||||
pri func decoder.write_to?(dst: base.io_writer) {
|
||||
var s : slice base.u8
|
||||
var n : base.u64
|
||||
var s : slice base.u8
|
||||
var n : base.u64
|
||||
|
||||
while this.output_wi > 0 {
|
||||
if this.output_ri > this.output_wi {
|
||||
return "#internal error: inconsistent I/O"
|
||||
}
|
||||
s = this.output[this.output_ri .. this.output_wi]
|
||||
n = args.dst.copy_from_slice!(s: s)
|
||||
if n == s.length() {
|
||||
this.output_ri = 0
|
||||
this.output_wi = 0
|
||||
return ok
|
||||
}
|
||||
this.output_ri = (this.output_ri ~mod+ ((n & 0xFFFF_FFFF) as base.u32)) & 8191
|
||||
yield? base."$short write"
|
||||
} endwhile
|
||||
while this.output_wi > 0 {
|
||||
if this.output_ri > this.output_wi {
|
||||
return "#internal error: inconsistent I/O"
|
||||
}
|
||||
s = this.output[this.output_ri .. this.output_wi]
|
||||
n = args.dst.copy_from_slice!(s: s)
|
||||
if n == s.length() {
|
||||
this.output_ri = 0
|
||||
this.output_wi = 0
|
||||
return ok
|
||||
}
|
||||
this.output_ri = (this.output_ri ~mod+ ((n & 0xFFFF_FFFF) as base.u32)) & 8191
|
||||
yield? base."$short write"
|
||||
} endwhile
|
||||
}
|
||||
|
||||
pub func decoder.flush!() slice base.u8 {
|
||||
var s : slice base.u8
|
||||
var s : slice base.u8
|
||||
|
||||
if this.output_ri <= this.output_wi {
|
||||
s = this.output[this.output_ri .. this.output_wi]
|
||||
}
|
||||
this.output_ri = 0
|
||||
this.output_wi = 0
|
||||
return s
|
||||
if this.output_ri <= this.output_wi {
|
||||
s = this.output[this.output_ri .. this.output_wi]
|
||||
}
|
||||
this.output_ri = 0
|
||||
this.output_wi = 0
|
||||
return s
|
||||
}
|
||||
+228
-228
@@ -21,304 +21,304 @@ pri status "@internal note: short read"
|
||||
pub const DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE : base.u64 = 0
|
||||
|
||||
pub struct decoder? implements base.image_decoder(
|
||||
pixfmt : base.u32,
|
||||
width : base.u32[..= 0x7FFF_FFFF],
|
||||
height : base.u32[..= 0x7FFF_FFFF],
|
||||
pixfmt : base.u32,
|
||||
width : base.u32[..= 0x7FFF_FFFF],
|
||||
height : base.u32[..= 0x7FFF_FFFF],
|
||||
|
||||
// The call sequence state machine is discussed in
|
||||
// (/doc/std/image-decoders-call-sequence.md).
|
||||
call_sequence : base.u8,
|
||||
// The call sequence state machine is discussed in
|
||||
// (/doc/std/image-decoders-call-sequence.md).
|
||||
call_sequence : base.u8,
|
||||
|
||||
dst_x : base.u32,
|
||||
dst_y : base.u32,
|
||||
dst_x : base.u32,
|
||||
dst_y : base.u32,
|
||||
|
||||
swizzler : base.pixel_swizzler,
|
||||
util : base.utility,
|
||||
swizzler : base.pixel_swizzler,
|
||||
util : base.utility,
|
||||
)
|
||||
|
||||
pub func decoder.set_quirk_enabled!(quirk: base.u32, enabled: base.bool) {
|
||||
}
|
||||
|
||||
pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_reader) {
|
||||
var status : base.status
|
||||
var status : base.status
|
||||
|
||||
while true {
|
||||
status =? this.do_decode_image_config?(dst: args.dst, src: args.src)
|
||||
if (status == base."$short read") and args.src.is_closed() {
|
||||
return "#truncated input"
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
while true {
|
||||
status =? this.do_decode_image_config?(dst: args.dst, src: args.src)
|
||||
if (status == base."$short read") and args.src.is_closed() {
|
||||
return "#truncated input"
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
}
|
||||
|
||||
pri func decoder.do_decode_image_config?(dst: nptr base.image_config, src: base.io_reader) {
|
||||
var a : base.u32
|
||||
var a : base.u32
|
||||
|
||||
if this.call_sequence <> 0x00 {
|
||||
return base."#bad call sequence"
|
||||
}
|
||||
if this.call_sequence <> 0x00 {
|
||||
return base."#bad call sequence"
|
||||
}
|
||||
|
||||
a = args.src.read_u32le?()
|
||||
if a <> 'nïE'le {
|
||||
return "#bad header"
|
||||
}
|
||||
a = args.src.read_u32le?()
|
||||
if a <> 'nïE'le {
|
||||
return "#bad header"
|
||||
}
|
||||
|
||||
a = args.src.read_u32le?()
|
||||
if a == '\xFFbn4'le {
|
||||
this.pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL
|
||||
} else if a == '\xFFbn8'le {
|
||||
this.pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE
|
||||
} else if a == '\xFFbp4'le {
|
||||
return "#unsupported NIE file"
|
||||
} else if a == '\xFFbp8'le {
|
||||
return "#unsupported NIE file"
|
||||
} else {
|
||||
return "#bad header"
|
||||
}
|
||||
a = args.src.read_u32le?()
|
||||
if a == '\xFFbn4'le {
|
||||
this.pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL
|
||||
} else if a == '\xFFbn8'le {
|
||||
this.pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE
|
||||
} else if a == '\xFFbp4'le {
|
||||
return "#unsupported NIE file"
|
||||
} else if a == '\xFFbp8'le {
|
||||
return "#unsupported NIE file"
|
||||
} else {
|
||||
return "#bad header"
|
||||
}
|
||||
|
||||
a = args.src.read_u32le?()
|
||||
if a >= 0x8000_0000 {
|
||||
return "#bad header"
|
||||
}
|
||||
this.width = a
|
||||
a = args.src.read_u32le?()
|
||||
if a >= 0x8000_0000 {
|
||||
return "#bad header"
|
||||
}
|
||||
this.width = a
|
||||
|
||||
a = args.src.read_u32le?()
|
||||
if a >= 0x8000_0000 {
|
||||
return "#bad header"
|
||||
}
|
||||
this.height = a
|
||||
a = args.src.read_u32le?()
|
||||
if a >= 0x8000_0000 {
|
||||
return "#bad header"
|
||||
}
|
||||
this.height = a
|
||||
|
||||
if args.dst <> nullptr {
|
||||
args.dst.set!(
|
||||
pixfmt: this.pixfmt,
|
||||
pixsub: 0,
|
||||
width: this.width,
|
||||
height: this.height,
|
||||
first_frame_io_position: 16,
|
||||
first_frame_is_opaque: false)
|
||||
}
|
||||
if args.dst <> nullptr {
|
||||
args.dst.set!(
|
||||
pixfmt: this.pixfmt,
|
||||
pixsub: 0,
|
||||
width: this.width,
|
||||
height: this.height,
|
||||
first_frame_io_position: 16,
|
||||
first_frame_is_opaque: false)
|
||||
}
|
||||
|
||||
this.call_sequence = 0x20
|
||||
this.call_sequence = 0x20
|
||||
}
|
||||
|
||||
pub func decoder.decode_frame_config?(dst: nptr base.frame_config, src: base.io_reader) {
|
||||
var status : base.status
|
||||
var status : base.status
|
||||
|
||||
while true {
|
||||
status =? this.do_decode_frame_config?(dst: args.dst, src: args.src)
|
||||
if (status == base."$short read") and args.src.is_closed() {
|
||||
return "#truncated input"
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
while true {
|
||||
status =? this.do_decode_frame_config?(dst: args.dst, src: args.src)
|
||||
if (status == base."$short read") and args.src.is_closed() {
|
||||
return "#truncated input"
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
}
|
||||
|
||||
pri func decoder.do_decode_frame_config?(dst: nptr base.frame_config, src: base.io_reader) {
|
||||
if this.call_sequence == 0x20 {
|
||||
// No-op.
|
||||
} else if this.call_sequence < 0x20 {
|
||||
this.do_decode_image_config?(dst: nullptr, src: args.src)
|
||||
} else if this.call_sequence == 0x28 {
|
||||
if 16 <> args.src.position() {
|
||||
return base."#bad restart"
|
||||
}
|
||||
} else if this.call_sequence == 0x40 {
|
||||
this.call_sequence = 0x60
|
||||
return base."@end of data"
|
||||
} else {
|
||||
return base."@end of data"
|
||||
}
|
||||
if this.call_sequence == 0x20 {
|
||||
// No-op.
|
||||
} else if this.call_sequence < 0x20 {
|
||||
this.do_decode_image_config?(dst: nullptr, src: args.src)
|
||||
} else if this.call_sequence == 0x28 {
|
||||
if 16 <> args.src.position() {
|
||||
return base."#bad restart"
|
||||
}
|
||||
} else if this.call_sequence == 0x40 {
|
||||
this.call_sequence = 0x60
|
||||
return base."@end of data"
|
||||
} else {
|
||||
return base."@end of data"
|
||||
}
|
||||
|
||||
if args.dst <> nullptr {
|
||||
args.dst.set!(bounds: this.util.make_rect_ie_u32(
|
||||
min_incl_x: 0,
|
||||
min_incl_y: 0,
|
||||
max_excl_x: this.width,
|
||||
max_excl_y: this.height),
|
||||
duration: 0,
|
||||
index: 0,
|
||||
io_position: 16,
|
||||
disposal: 0,
|
||||
opaque_within_bounds: false,
|
||||
overwrite_instead_of_blend: false,
|
||||
background_color: 0x0000_0000)
|
||||
}
|
||||
if args.dst <> nullptr {
|
||||
args.dst.set!(bounds: this.util.make_rect_ie_u32(
|
||||
min_incl_x: 0,
|
||||
min_incl_y: 0,
|
||||
max_excl_x: this.width,
|
||||
max_excl_y: this.height),
|
||||
duration: 0,
|
||||
index: 0,
|
||||
io_position: 16,
|
||||
disposal: 0,
|
||||
opaque_within_bounds: false,
|
||||
overwrite_instead_of_blend: false,
|
||||
background_color: 0x0000_0000)
|
||||
}
|
||||
|
||||
this.call_sequence = 0x40
|
||||
this.call_sequence = 0x40
|
||||
}
|
||||
|
||||
pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader, blend: base.pixel_blend, workbuf: slice base.u8, opts: nptr base.decode_frame_options) {
|
||||
var status : base.status
|
||||
var status : base.status
|
||||
|
||||
while true {
|
||||
status =? this.do_decode_frame?(dst: args.dst, src: args.src, blend: args.blend, workbuf: args.workbuf, opts: args.opts)
|
||||
if (status == base."$short read") and args.src.is_closed() {
|
||||
return "#truncated input"
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
while true {
|
||||
status =? this.do_decode_frame?(dst: args.dst, src: args.src, blend: args.blend, workbuf: args.workbuf, opts: args.opts)
|
||||
if (status == base."$short read") and args.src.is_closed() {
|
||||
return "#truncated input"
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
}
|
||||
|
||||
pri func decoder.do_decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader, blend: base.pixel_blend, workbuf: slice base.u8, opts: nptr base.decode_frame_options) {
|
||||
var status : base.status
|
||||
var status : base.status
|
||||
|
||||
if this.call_sequence == 0x40 {
|
||||
// No-op.
|
||||
} else if this.call_sequence < 0x40 {
|
||||
this.do_decode_frame_config?(dst: nullptr, src: args.src)
|
||||
} else {
|
||||
return base."@end of data"
|
||||
}
|
||||
if this.call_sequence == 0x40 {
|
||||
// No-op.
|
||||
} else if this.call_sequence < 0x40 {
|
||||
this.do_decode_frame_config?(dst: nullptr, src: args.src)
|
||||
} else {
|
||||
return base."@end of data"
|
||||
}
|
||||
|
||||
this.dst_x = 0
|
||||
this.dst_y = 0
|
||||
this.dst_x = 0
|
||||
this.dst_y = 0
|
||||
|
||||
status = this.swizzler.prepare!(
|
||||
dst_pixfmt: args.dst.pixel_format(),
|
||||
dst_palette: args.dst.palette(),
|
||||
src_pixfmt: this.util.make_pixel_format(repr: this.pixfmt),
|
||||
src_palette: this.util.empty_slice_u8(),
|
||||
blend: args.blend)
|
||||
if not status.is_ok() {
|
||||
return status
|
||||
}
|
||||
status = this.swizzler.prepare!(
|
||||
dst_pixfmt: args.dst.pixel_format(),
|
||||
dst_palette: args.dst.palette(),
|
||||
src_pixfmt: this.util.make_pixel_format(repr: this.pixfmt),
|
||||
src_palette: this.util.empty_slice_u8(),
|
||||
blend: args.blend)
|
||||
if not status.is_ok() {
|
||||
return status
|
||||
}
|
||||
|
||||
while true {
|
||||
status = this.swizzle!(dst: args.dst, src: args.src)
|
||||
if status.is_ok() {
|
||||
break
|
||||
} else if status <> "@internal note: short read" {
|
||||
return status
|
||||
}
|
||||
yield? base."$short read"
|
||||
} endwhile
|
||||
while true {
|
||||
status = this.swizzle!(dst: args.dst, src: args.src)
|
||||
if status.is_ok() {
|
||||
break
|
||||
} else if status <> "@internal note: short read" {
|
||||
return status
|
||||
}
|
||||
yield? base."$short read"
|
||||
} endwhile
|
||||
|
||||
this.call_sequence = 0x60
|
||||
this.call_sequence = 0x60
|
||||
}
|
||||
|
||||
pri func decoder.swizzle!(dst: ptr base.pixel_buffer, src: base.io_reader) base.status {
|
||||
var dst_pixfmt : base.pixel_format
|
||||
var dst_bits_per_pixel : base.u32[..= 256]
|
||||
var dst_bytes_per_pixel : base.u64[..= 32]
|
||||
var dst_bytes_per_row : base.u64
|
||||
var src_bytes_per_pixel : base.u32[..= 8]
|
||||
var tab : table base.u8
|
||||
var dst : slice base.u8
|
||||
var i : base.u64
|
||||
var j : base.u64
|
||||
var n : base.u64
|
||||
var dst_pixfmt : base.pixel_format
|
||||
var dst_bits_per_pixel : base.u32[..= 256]
|
||||
var dst_bytes_per_pixel : base.u64[..= 32]
|
||||
var dst_bytes_per_row : base.u64
|
||||
var src_bytes_per_pixel : base.u32[..= 8]
|
||||
var tab : table base.u8
|
||||
var dst : slice base.u8
|
||||
var i : base.u64
|
||||
var j : base.u64
|
||||
var n : base.u64
|
||||
|
||||
// TODO: the dst_pixfmt variable shouldn't be necessary. We should be able
|
||||
// to chain the two calls: "args.dst.pixel_format().bits_per_pixel()".
|
||||
dst_pixfmt = args.dst.pixel_format()
|
||||
dst_bits_per_pixel = dst_pixfmt.bits_per_pixel()
|
||||
if (dst_bits_per_pixel & 7) <> 0 {
|
||||
return base."#unsupported option"
|
||||
}
|
||||
dst_bytes_per_pixel = (dst_bits_per_pixel / 8) as base.u64
|
||||
dst_bytes_per_row = (this.width as base.u64) * dst_bytes_per_pixel
|
||||
tab = args.dst.plane(p: 0)
|
||||
// TODO: the dst_pixfmt variable shouldn't be necessary. We should be able
|
||||
// to chain the two calls: "args.dst.pixel_format().bits_per_pixel()".
|
||||
dst_pixfmt = args.dst.pixel_format()
|
||||
dst_bits_per_pixel = dst_pixfmt.bits_per_pixel()
|
||||
if (dst_bits_per_pixel & 7) <> 0 {
|
||||
return base."#unsupported option"
|
||||
}
|
||||
dst_bytes_per_pixel = (dst_bits_per_pixel / 8) as base.u64
|
||||
dst_bytes_per_row = (this.width as base.u64) * dst_bytes_per_pixel
|
||||
tab = args.dst.plane(p: 0)
|
||||
|
||||
while true {
|
||||
if this.dst_x == this.width {
|
||||
this.dst_x = 0
|
||||
this.dst_y ~mod+= 1
|
||||
if this.dst_y >= this.height {
|
||||
break
|
||||
}
|
||||
}
|
||||
while true {
|
||||
if this.dst_x == this.width {
|
||||
this.dst_x = 0
|
||||
this.dst_y ~mod+= 1
|
||||
if this.dst_y >= this.height {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
dst = tab.row_u32(y: this.dst_y)
|
||||
if dst_bytes_per_row < dst.length() {
|
||||
dst = dst[.. dst_bytes_per_row]
|
||||
}
|
||||
i = (this.dst_x as base.u64) * dst_bytes_per_pixel
|
||||
if i >= dst.length() {
|
||||
src_bytes_per_pixel = 4
|
||||
assert src_bytes_per_pixel > 0
|
||||
if this.pixfmt == base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE {
|
||||
src_bytes_per_pixel = 8
|
||||
assert src_bytes_per_pixel > 0
|
||||
}
|
||||
n = args.src.length() / (src_bytes_per_pixel as base.u64)
|
||||
n = n.min(a: (this.width ~mod- this.dst_x) as base.u64)
|
||||
j = n
|
||||
while j >= 8 {
|
||||
if args.src.length() >= ((src_bytes_per_pixel * 8) as base.u64) {
|
||||
args.src.skip_u32_fast!(
|
||||
actual: src_bytes_per_pixel * 8,
|
||||
worst_case: src_bytes_per_pixel * 8)
|
||||
}
|
||||
j -= 8
|
||||
} endwhile
|
||||
while j > 0 {
|
||||
if args.src.length() >= ((src_bytes_per_pixel * 1) as base.u64) {
|
||||
args.src.skip_u32_fast!(
|
||||
actual: src_bytes_per_pixel * 1,
|
||||
worst_case: src_bytes_per_pixel * 1)
|
||||
}
|
||||
j -= 1
|
||||
} endwhile
|
||||
} else {
|
||||
n = this.swizzler.swizzle_interleaved_from_reader!(
|
||||
dst: dst[i ..],
|
||||
dst_palette: args.dst.palette(),
|
||||
src: args.src)
|
||||
}
|
||||
if n == 0 {
|
||||
return "@internal note: short read"
|
||||
}
|
||||
this.dst_x ~sat+= (n & 0xFFFF_FFFF) as base.u32
|
||||
} endwhile
|
||||
dst = tab.row_u32(y: this.dst_y)
|
||||
if dst_bytes_per_row < dst.length() {
|
||||
dst = dst[.. dst_bytes_per_row]
|
||||
}
|
||||
i = (this.dst_x as base.u64) * dst_bytes_per_pixel
|
||||
if i >= dst.length() {
|
||||
src_bytes_per_pixel = 4
|
||||
assert src_bytes_per_pixel > 0
|
||||
if this.pixfmt == base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE {
|
||||
src_bytes_per_pixel = 8
|
||||
assert src_bytes_per_pixel > 0
|
||||
}
|
||||
n = args.src.length() / (src_bytes_per_pixel as base.u64)
|
||||
n = n.min(a: (this.width ~mod- this.dst_x) as base.u64)
|
||||
j = n
|
||||
while j >= 8 {
|
||||
if args.src.length() >= ((src_bytes_per_pixel * 8) as base.u64) {
|
||||
args.src.skip_u32_fast!(
|
||||
actual: src_bytes_per_pixel * 8,
|
||||
worst_case: src_bytes_per_pixel * 8)
|
||||
}
|
||||
j -= 8
|
||||
} endwhile
|
||||
while j > 0 {
|
||||
if args.src.length() >= ((src_bytes_per_pixel * 1) as base.u64) {
|
||||
args.src.skip_u32_fast!(
|
||||
actual: src_bytes_per_pixel * 1,
|
||||
worst_case: src_bytes_per_pixel * 1)
|
||||
}
|
||||
j -= 1
|
||||
} endwhile
|
||||
} else {
|
||||
n = this.swizzler.swizzle_interleaved_from_reader!(
|
||||
dst: dst[i ..],
|
||||
dst_palette: args.dst.palette(),
|
||||
src: args.src)
|
||||
}
|
||||
if n == 0 {
|
||||
return "@internal note: short read"
|
||||
}
|
||||
this.dst_x ~sat+= (n & 0xFFFF_FFFF) as base.u32
|
||||
} endwhile
|
||||
|
||||
return ok
|
||||
return ok
|
||||
}
|
||||
|
||||
pub func decoder.frame_dirty_rect() base.rect_ie_u32 {
|
||||
return this.util.make_rect_ie_u32(
|
||||
min_incl_x: 0,
|
||||
min_incl_y: 0,
|
||||
max_excl_x: this.width,
|
||||
max_excl_y: this.height)
|
||||
return this.util.make_rect_ie_u32(
|
||||
min_incl_x: 0,
|
||||
min_incl_y: 0,
|
||||
max_excl_x: this.width,
|
||||
max_excl_y: this.height)
|
||||
}
|
||||
|
||||
pub func decoder.num_animation_loops() base.u32 {
|
||||
return 0
|
||||
return 0
|
||||
}
|
||||
|
||||
pub func decoder.num_decoded_frame_configs() base.u64 {
|
||||
if this.call_sequence > 0x20 {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
if this.call_sequence > 0x20 {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
pub func decoder.num_decoded_frames() base.u64 {
|
||||
if this.call_sequence > 0x40 {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
if this.call_sequence > 0x40 {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
pub func decoder.restart_frame!(index: base.u64, io_position: base.u64) base.status {
|
||||
if this.call_sequence < 0x20 {
|
||||
return base."#bad call sequence"
|
||||
}
|
||||
if (args.index <> 0) or (args.io_position <> 16) {
|
||||
return base."#bad argument"
|
||||
}
|
||||
this.call_sequence = 0x28
|
||||
return ok
|
||||
if this.call_sequence < 0x20 {
|
||||
return base."#bad call sequence"
|
||||
}
|
||||
if (args.index <> 0) or (args.io_position <> 16) {
|
||||
return base."#bad argument"
|
||||
}
|
||||
this.call_sequence = 0x28
|
||||
return ok
|
||||
}
|
||||
|
||||
pub func decoder.set_report_metadata!(fourcc: base.u32, report: base.bool) {
|
||||
// No-op. NIE doesn't support metadata.
|
||||
// No-op. NIE doesn't support metadata.
|
||||
}
|
||||
|
||||
pub func decoder.tell_me_more?(dst: base.io_writer, minfo: nptr base.more_information, src: base.io_reader) {
|
||||
return base."#no more information"
|
||||
return base."#no more information"
|
||||
}
|
||||
|
||||
pub func decoder.workbuf_len() base.range_ii_u64 {
|
||||
return this.util.make_range_ii_u64(min_incl: 0, max_incl: 0)
|
||||
return this.util.make_range_ii_u64(min_incl: 0, max_incl: 0)
|
||||
}
|
||||
+64
-64
@@ -58,87 +58,87 @@ pri const ANCILLARY_BIT : base.u32 = 0x0000_0020
|
||||
// 4: y_stride - y_offset - 1
|
||||
// 5: y_offset
|
||||
pri const INTERLACING : array[8] array[6] base.u8[..= 7] = [
|
||||
[0, 0, 0, 0, 0, 0], // non-interlaced; xy_stride=1, xy_offset=0
|
||||
[3, 7, 0, 3, 7, 0], // interlace_pass == 1
|
||||
[3, 3, 4, 3, 7, 0], // interlace_pass == 2
|
||||
[2, 3, 0, 3, 3, 4], // interlace_pass == 3
|
||||
[2, 1, 2, 2, 3, 0], // interlace_pass == 4
|
||||
[1, 1, 0, 2, 1, 2], // interlace_pass == 5
|
||||
[1, 0, 1, 1, 1, 0], // interlace_pass == 6
|
||||
[0, 0, 0, 1, 0, 1], // interlace_pass == 7
|
||||
[0, 0, 0, 0, 0, 0], // non-interlaced; xy_stride=1, xy_offset=0
|
||||
[3, 7, 0, 3, 7, 0], // interlace_pass == 1
|
||||
[3, 3, 4, 3, 7, 0], // interlace_pass == 2
|
||||
[2, 3, 0, 3, 3, 4], // interlace_pass == 3
|
||||
[2, 1, 2, 2, 3, 0], // interlace_pass == 4
|
||||
[1, 1, 0, 2, 1, 2], // interlace_pass == 5
|
||||
[1, 0, 1, 1, 1, 0], // interlace_pass == 6
|
||||
[0, 0, 0, 1, 0, 1], // interlace_pass == 7
|
||||
]
|
||||
|
||||
// LOW_BIT_DEPTH_MULTIPLIERS holds multipliers that convert D-bit values into
|
||||
// 8-bit values, for depth D.
|
||||
pri const LOW_BIT_DEPTH_MULTIPLIERS : array[8] base.u8 = [
|
||||
0,
|
||||
0b1111_1111, // depth == 1
|
||||
0b0101_0101, // depth == 2
|
||||
0,
|
||||
0b0001_0001, // depth == 4
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0b1111_1111, // depth == 1
|
||||
0b0101_0101, // depth == 2
|
||||
0,
|
||||
0b0001_0001, // depth == 4
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
]
|
||||
|
||||
// LOW_BIT_DEPTH_NUM_PACKS holds the number of smaller-than-1-byte units that
|
||||
// are packed into each byte.
|
||||
pri const LOW_BIT_DEPTH_NUM_PACKS : array[8] base.u8 = [
|
||||
0,
|
||||
8, // depth == 1
|
||||
4, // depth == 2
|
||||
0,
|
||||
2, // depth == 4
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
8, // depth == 1
|
||||
4, // depth == 2
|
||||
0,
|
||||
2, // depth == 4
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
]
|
||||
|
||||
// NUM_CHANNELS holds the number of channels for each valid color type.
|
||||
pri const NUM_CHANNELS : array[8] base.u8[..= 4] = [
|
||||
1, // color_type == 0: Y
|
||||
0,
|
||||
3, // color_type == 2: RGB
|
||||
1, // color_type == 3: indexed
|
||||
2, // color_type == 4: YA
|
||||
0,
|
||||
4, // color_type == 6: RGBA
|
||||
0,
|
||||
1, // color_type == 0: Y
|
||||
0,
|
||||
3, // color_type == 2: RGB
|
||||
1, // color_type == 3: indexed
|
||||
2, // color_type == 4: YA
|
||||
0,
|
||||
4, // color_type == 6: RGBA
|
||||
0,
|
||||
]
|
||||
|
||||
// LATIN_1 converts from printable Latin-1 (ISO/IEC 8859-1) to UTF-8 (encoded
|
||||
// as little-endian u16). It was created by script/print-png-latin-1.go.
|
||||
pri const LATIN_1 : array[256] base.u16 = [
|
||||
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
|
||||
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
|
||||
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
|
||||
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
|
||||
0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027,
|
||||
0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F,
|
||||
0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037,
|
||||
0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F,
|
||||
0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047,
|
||||
0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F,
|
||||
0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057,
|
||||
0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F,
|
||||
0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067,
|
||||
0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F,
|
||||
0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077,
|
||||
0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x0000,
|
||||
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
|
||||
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
|
||||
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
|
||||
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
|
||||
0x0000, 0xA1C2, 0xA2C2, 0xA3C2, 0xA4C2, 0xA5C2, 0xA6C2, 0xA7C2,
|
||||
0xA8C2, 0xA9C2, 0xAAC2, 0xABC2, 0xACC2, 0xADC2, 0xAEC2, 0xAFC2,
|
||||
0xB0C2, 0xB1C2, 0xB2C2, 0xB3C2, 0xB4C2, 0xB5C2, 0xB6C2, 0xB7C2,
|
||||
0xB8C2, 0xB9C2, 0xBAC2, 0xBBC2, 0xBCC2, 0xBDC2, 0xBEC2, 0xBFC2,
|
||||
0x80C3, 0x81C3, 0x82C3, 0x83C3, 0x84C3, 0x85C3, 0x86C3, 0x87C3,
|
||||
0x88C3, 0x89C3, 0x8AC3, 0x8BC3, 0x8CC3, 0x8DC3, 0x8EC3, 0x8FC3,
|
||||
0x90C3, 0x91C3, 0x92C3, 0x93C3, 0x94C3, 0x95C3, 0x96C3, 0x97C3,
|
||||
0x98C3, 0x99C3, 0x9AC3, 0x9BC3, 0x9CC3, 0x9DC3, 0x9EC3, 0x9FC3,
|
||||
0xA0C3, 0xA1C3, 0xA2C3, 0xA3C3, 0xA4C3, 0xA5C3, 0xA6C3, 0xA7C3,
|
||||
0xA8C3, 0xA9C3, 0xAAC3, 0xABC3, 0xACC3, 0xADC3, 0xAEC3, 0xAFC3,
|
||||
0xB0C3, 0xB1C3, 0xB2C3, 0xB3C3, 0xB4C3, 0xB5C3, 0xB6C3, 0xB7C3,
|
||||
0xB8C3, 0xB9C3, 0xBAC3, 0xBBC3, 0xBCC3, 0xBDC3, 0xBEC3, 0xBFC3,
|
||||
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
|
||||
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
|
||||
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
|
||||
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
|
||||
0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027,
|
||||
0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F,
|
||||
0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037,
|
||||
0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F,
|
||||
0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047,
|
||||
0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F,
|
||||
0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057,
|
||||
0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F,
|
||||
0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067,
|
||||
0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F,
|
||||
0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077,
|
||||
0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x0000,
|
||||
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
|
||||
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
|
||||
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
|
||||
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
|
||||
0x0000, 0xA1C2, 0xA2C2, 0xA3C2, 0xA4C2, 0xA5C2, 0xA6C2, 0xA7C2,
|
||||
0xA8C2, 0xA9C2, 0xAAC2, 0xABC2, 0xACC2, 0xADC2, 0xAEC2, 0xAFC2,
|
||||
0xB0C2, 0xB1C2, 0xB2C2, 0xB3C2, 0xB4C2, 0xB5C2, 0xB6C2, 0xB7C2,
|
||||
0xB8C2, 0xB9C2, 0xBAC2, 0xBBC2, 0xBCC2, 0xBDC2, 0xBEC2, 0xBFC2,
|
||||
0x80C3, 0x81C3, 0x82C3, 0x83C3, 0x84C3, 0x85C3, 0x86C3, 0x87C3,
|
||||
0x88C3, 0x89C3, 0x8AC3, 0x8BC3, 0x8CC3, 0x8DC3, 0x8EC3, 0x8FC3,
|
||||
0x90C3, 0x91C3, 0x92C3, 0x93C3, 0x94C3, 0x95C3, 0x96C3, 0x97C3,
|
||||
0x98C3, 0x99C3, 0x9AC3, 0x9BC3, 0x9CC3, 0x9DC3, 0x9EC3, 0x9FC3,
|
||||
0xA0C3, 0xA1C3, 0xA2C3, 0xA3C3, 0xA4C3, 0xA5C3, 0xA6C3, 0xA7C3,
|
||||
0xA8C3, 0xA9C3, 0xAAC3, 0xABC3, 0xACC3, 0xADC3, 0xAEC3, 0xAFC3,
|
||||
0xB0C3, 0xB1C3, 0xB2C3, 0xB3C3, 0xB4C3, 0xB5C3, 0xB6C3, 0xB7C3,
|
||||
0xB8C3, 0xB9C3, 0xBAC3, 0xBBC3, 0xBCC3, 0xBDC3, 0xBEC3, 0xBFC3,
|
||||
]
|
||||
@@ -29,20 +29,20 @@
|
||||
// Filter 1: Sub.
|
||||
|
||||
pri func decoder.filter_1_distance_4_arm_neon!(curr: slice base.u8),
|
||||
choose cpu_arch >= arm_neon,
|
||||
choose cpu_arch >= arm_neon,
|
||||
{
|
||||
var curr : slice base.u8
|
||||
var curr : slice base.u8
|
||||
|
||||
var util : base.arm_neon_utility
|
||||
var fa : base.arm_neon_u8x8
|
||||
var fx : base.arm_neon_u8x8
|
||||
var util : base.arm_neon_utility
|
||||
var fa : base.arm_neon_u8x8
|
||||
var fx : base.arm_neon_u8x8
|
||||
|
||||
iterate (curr = args.curr)(length: 4, advance: 4, unroll: 2) {
|
||||
fx = util.make_u32x2_repeat(a: curr.peek_u32le()).as_u8x8()
|
||||
fx = fx.vadd_u8(b: fa)
|
||||
curr.poke_u32le!(a: fx.as_u32x2().vget_lane_u32(b: 0))
|
||||
fa = fx
|
||||
}
|
||||
iterate (curr = args.curr)(length: 4, advance: 4, unroll: 2) {
|
||||
fx = util.make_u32x2_repeat(a: curr.peek_u32le()).as_u8x8()
|
||||
fx = fx.vadd_u8(b: fa)
|
||||
curr.poke_u32le!(a: fx.as_u32x2().vget_lane_u32(b: 0))
|
||||
fa = fx
|
||||
}
|
||||
}
|
||||
|
||||
// --------
|
||||
@@ -50,33 +50,33 @@ pri func decoder.filter_1_distance_4_arm_neon!(curr: slice base.u8),
|
||||
// Filter 3: Average.
|
||||
|
||||
pri func decoder.filter_3_distance_4_arm_neon!(curr: slice base.u8, prev: slice base.u8),
|
||||
choose cpu_arch >= arm_neon,
|
||||
choose cpu_arch >= arm_neon,
|
||||
{
|
||||
var curr : slice base.u8
|
||||
var prev : slice base.u8
|
||||
var curr : slice base.u8
|
||||
var prev : slice base.u8
|
||||
|
||||
var util : base.arm_neon_utility
|
||||
var fa : base.arm_neon_u8x8
|
||||
var fb : base.arm_neon_u8x8
|
||||
var fx : base.arm_neon_u8x8
|
||||
var util : base.arm_neon_utility
|
||||
var fa : base.arm_neon_u8x8
|
||||
var fb : base.arm_neon_u8x8
|
||||
var fx : base.arm_neon_u8x8
|
||||
|
||||
if args.prev.length() == 0 {
|
||||
iterate (curr = args.curr)(length: 4, advance: 4, unroll: 2) {
|
||||
fx = util.make_u32x2_repeat(a: curr.peek_u32le()).as_u8x8()
|
||||
fx = fx.vadd_u8(b: fa.vhadd_u8(b: fb))
|
||||
curr.poke_u32le!(a: fx.as_u32x2().vget_lane_u32(b: 0))
|
||||
fa = fx
|
||||
}
|
||||
if args.prev.length() == 0 {
|
||||
iterate (curr = args.curr)(length: 4, advance: 4, unroll: 2) {
|
||||
fx = util.make_u32x2_repeat(a: curr.peek_u32le()).as_u8x8()
|
||||
fx = fx.vadd_u8(b: fa.vhadd_u8(b: fb))
|
||||
curr.poke_u32le!(a: fx.as_u32x2().vget_lane_u32(b: 0))
|
||||
fa = fx
|
||||
}
|
||||
|
||||
} else {
|
||||
iterate (curr = args.curr, prev = args.prev)(length: 4, advance: 4, unroll: 2) {
|
||||
fb = util.make_u32x2_repeat(a: prev.peek_u32le()).as_u8x8()
|
||||
fx = util.make_u32x2_repeat(a: curr.peek_u32le()).as_u8x8()
|
||||
fx = fx.vadd_u8(b: fa.vhadd_u8(b: fb))
|
||||
curr.poke_u32le!(a: fx.as_u32x2().vget_lane_u32(b: 0))
|
||||
fa = fx
|
||||
}
|
||||
}
|
||||
} else {
|
||||
iterate (curr = args.curr, prev = args.prev)(length: 4, advance: 4, unroll: 2) {
|
||||
fb = util.make_u32x2_repeat(a: prev.peek_u32le()).as_u8x8()
|
||||
fx = util.make_u32x2_repeat(a: curr.peek_u32le()).as_u8x8()
|
||||
fx = fx.vadd_u8(b: fa.vhadd_u8(b: fb))
|
||||
curr.poke_u32le!(a: fx.as_u32x2().vget_lane_u32(b: 0))
|
||||
fa = fx
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --------
|
||||
@@ -84,123 +84,123 @@ pri func decoder.filter_3_distance_4_arm_neon!(curr: slice base.u8, prev: slice
|
||||
// Filter 4: Paeth.
|
||||
|
||||
pri func decoder.filter_4_distance_3_arm_neon!(curr: slice base.u8, prev: slice base.u8),
|
||||
choose cpu_arch >= arm_neon,
|
||||
choose cpu_arch >= arm_neon,
|
||||
{
|
||||
// See the comments in filter_4_distance_4_arm_neon for an explanation of
|
||||
// how this works. That function's single loop is copied twice here, once
|
||||
// with "length: 4" and once with "length: 3". It's generally faster to
|
||||
// load 4 bytes at a time instead of 3.
|
||||
//
|
||||
// Differences between that function and this one are marked with a §.
|
||||
// See the comments in filter_4_distance_4_arm_neon for an explanation of
|
||||
// how this works. That function's single loop is copied twice here, once
|
||||
// with "length: 4" and once with "length: 3". It's generally faster to
|
||||
// load 4 bytes at a time instead of 3.
|
||||
//
|
||||
// Differences between that function and this one are marked with a §.
|
||||
|
||||
var curr : slice base.u8
|
||||
var prev : slice base.u8
|
||||
var curr : slice base.u8
|
||||
var prev : slice base.u8
|
||||
|
||||
var util : base.arm_neon_utility
|
||||
var fa : base.arm_neon_u8x8
|
||||
var fb : base.arm_neon_u8x8
|
||||
var fc : base.arm_neon_u8x8
|
||||
var fx : base.arm_neon_u8x8
|
||||
var fafb : base.arm_neon_u16x8
|
||||
var fcfc : base.arm_neon_u16x8
|
||||
var pa : base.arm_neon_u16x8
|
||||
var pb : base.arm_neon_u16x8
|
||||
var pc : base.arm_neon_u16x8
|
||||
var cmpab : base.arm_neon_u16x8
|
||||
var cmpac : base.arm_neon_u16x8
|
||||
var picka : base.arm_neon_u8x8
|
||||
var pickb : base.arm_neon_u8x8
|
||||
var util : base.arm_neon_utility
|
||||
var fa : base.arm_neon_u8x8
|
||||
var fb : base.arm_neon_u8x8
|
||||
var fc : base.arm_neon_u8x8
|
||||
var fx : base.arm_neon_u8x8
|
||||
var fafb : base.arm_neon_u16x8
|
||||
var fcfc : base.arm_neon_u16x8
|
||||
var pa : base.arm_neon_u16x8
|
||||
var pb : base.arm_neon_u16x8
|
||||
var pc : base.arm_neon_u16x8
|
||||
var cmpab : base.arm_neon_u16x8
|
||||
var cmpac : base.arm_neon_u16x8
|
||||
var picka : base.arm_neon_u8x8
|
||||
var pickb : base.arm_neon_u8x8
|
||||
|
||||
// § The advance is 3, not 4.
|
||||
iterate (curr = args.curr, prev = args.prev)(length: 4, advance: 3, unroll: 2) {
|
||||
fb = util.make_u32x2_repeat(a: prev.peek_u32le()).as_u8x8()
|
||||
fx = util.make_u32x2_repeat(a: curr.peek_u32le()).as_u8x8()
|
||||
fafb = fa.vaddl_u8(b: fb)
|
||||
fcfc = fc.vaddl_u8(b: fc)
|
||||
pa = fb.vabdl_u8(b: fc)
|
||||
pb = fa.vabdl_u8(b: fc)
|
||||
pc = fafb.vabdq_u16(b: fcfc)
|
||||
cmpab = pa.vcleq_u16(b: pb)
|
||||
cmpac = pa.vcleq_u16(b: pc)
|
||||
picka = cmpab.vandq_u16(b: cmpac).vmovn_u16()
|
||||
pickb = pb.vcleq_u16(b: pc).vmovn_u16()
|
||||
fx = fx.vadd_u8(
|
||||
b: picka.vbsl_u8(b: fa,
|
||||
c: pickb.vbsl_u8(b: fb, c: fc)))
|
||||
// § poke_u24le replaces poke_u32le.
|
||||
curr.poke_u24le!(a: fx.as_u32x2().vget_lane_u32(b: 0))
|
||||
fc = fb
|
||||
fa = fx
|
||||
} else (length: 3, advance: 3, unroll: 1) {
|
||||
// § peek_u24le_as_u32 replaces peek_u32le.
|
||||
fb = util.make_u32x2_repeat(a: prev.peek_u24le_as_u32()).as_u8x8()
|
||||
// § peek_u24le_as_u32 replaces peek_u32le.
|
||||
fx = util.make_u32x2_repeat(a: curr.peek_u24le_as_u32()).as_u8x8()
|
||||
fafb = fa.vaddl_u8(b: fb)
|
||||
fcfc = fc.vaddl_u8(b: fc)
|
||||
pa = fb.vabdl_u8(b: fc)
|
||||
pb = fa.vabdl_u8(b: fc)
|
||||
pc = fafb.vabdq_u16(b: fcfc)
|
||||
cmpab = pa.vcleq_u16(b: pb)
|
||||
cmpac = pa.vcleq_u16(b: pc)
|
||||
picka = cmpab.vandq_u16(b: cmpac).vmovn_u16()
|
||||
pickb = pb.vcleq_u16(b: pc).vmovn_u16()
|
||||
fx = fx.vadd_u8(
|
||||
b: picka.vbsl_u8(b: fa,
|
||||
c: pickb.vbsl_u8(b: fb, c: fc)))
|
||||
// § poke_u24le replaces poke_u32le.
|
||||
curr.poke_u24le!(a: fx.as_u32x2().vget_lane_u32(b: 0))
|
||||
// § These assignments are unnecessary; this is the last iteration.
|
||||
// fc = fb
|
||||
// fa = fx
|
||||
}
|
||||
// § The advance is 3, not 4.
|
||||
iterate (curr = args.curr, prev = args.prev)(length: 4, advance: 3, unroll: 2) {
|
||||
fb = util.make_u32x2_repeat(a: prev.peek_u32le()).as_u8x8()
|
||||
fx = util.make_u32x2_repeat(a: curr.peek_u32le()).as_u8x8()
|
||||
fafb = fa.vaddl_u8(b: fb)
|
||||
fcfc = fc.vaddl_u8(b: fc)
|
||||
pa = fb.vabdl_u8(b: fc)
|
||||
pb = fa.vabdl_u8(b: fc)
|
||||
pc = fafb.vabdq_u16(b: fcfc)
|
||||
cmpab = pa.vcleq_u16(b: pb)
|
||||
cmpac = pa.vcleq_u16(b: pc)
|
||||
picka = cmpab.vandq_u16(b: cmpac).vmovn_u16()
|
||||
pickb = pb.vcleq_u16(b: pc).vmovn_u16()
|
||||
fx = fx.vadd_u8(
|
||||
b: picka.vbsl_u8(b: fa,
|
||||
c: pickb.vbsl_u8(b: fb, c: fc)))
|
||||
// § poke_u24le replaces poke_u32le.
|
||||
curr.poke_u24le!(a: fx.as_u32x2().vget_lane_u32(b: 0))
|
||||
fc = fb
|
||||
fa = fx
|
||||
} else (length: 3, advance: 3, unroll: 1) {
|
||||
// § peek_u24le_as_u32 replaces peek_u32le.
|
||||
fb = util.make_u32x2_repeat(a: prev.peek_u24le_as_u32()).as_u8x8()
|
||||
// § peek_u24le_as_u32 replaces peek_u32le.
|
||||
fx = util.make_u32x2_repeat(a: curr.peek_u24le_as_u32()).as_u8x8()
|
||||
fafb = fa.vaddl_u8(b: fb)
|
||||
fcfc = fc.vaddl_u8(b: fc)
|
||||
pa = fb.vabdl_u8(b: fc)
|
||||
pb = fa.vabdl_u8(b: fc)
|
||||
pc = fafb.vabdq_u16(b: fcfc)
|
||||
cmpab = pa.vcleq_u16(b: pb)
|
||||
cmpac = pa.vcleq_u16(b: pc)
|
||||
picka = cmpab.vandq_u16(b: cmpac).vmovn_u16()
|
||||
pickb = pb.vcleq_u16(b: pc).vmovn_u16()
|
||||
fx = fx.vadd_u8(
|
||||
b: picka.vbsl_u8(b: fa,
|
||||
c: pickb.vbsl_u8(b: fb, c: fc)))
|
||||
// § poke_u24le replaces poke_u32le.
|
||||
curr.poke_u24le!(a: fx.as_u32x2().vget_lane_u32(b: 0))
|
||||
// § These assignments are unnecessary; this is the last iteration.
|
||||
// fc = fb
|
||||
// fa = fx
|
||||
}
|
||||
}
|
||||
|
||||
pri func decoder.filter_4_distance_4_arm_neon!(curr: slice base.u8, prev: slice base.u8),
|
||||
choose cpu_arch >= arm_neon,
|
||||
choose cpu_arch >= arm_neon,
|
||||
{
|
||||
var curr : slice base.u8
|
||||
var prev : slice base.u8
|
||||
var curr : slice base.u8
|
||||
var prev : slice base.u8
|
||||
|
||||
var util : base.arm_neon_utility
|
||||
var fa : base.arm_neon_u8x8
|
||||
var fb : base.arm_neon_u8x8
|
||||
var fc : base.arm_neon_u8x8
|
||||
var fx : base.arm_neon_u8x8
|
||||
var fafb : base.arm_neon_u16x8
|
||||
var fcfc : base.arm_neon_u16x8
|
||||
var pa : base.arm_neon_u16x8
|
||||
var pb : base.arm_neon_u16x8
|
||||
var pc : base.arm_neon_u16x8
|
||||
var cmpab : base.arm_neon_u16x8
|
||||
var cmpac : base.arm_neon_u16x8
|
||||
var picka : base.arm_neon_u8x8
|
||||
var pickb : base.arm_neon_u8x8
|
||||
var util : base.arm_neon_utility
|
||||
var fa : base.arm_neon_u8x8
|
||||
var fb : base.arm_neon_u8x8
|
||||
var fc : base.arm_neon_u8x8
|
||||
var fx : base.arm_neon_u8x8
|
||||
var fafb : base.arm_neon_u16x8
|
||||
var fcfc : base.arm_neon_u16x8
|
||||
var pa : base.arm_neon_u16x8
|
||||
var pb : base.arm_neon_u16x8
|
||||
var pc : base.arm_neon_u16x8
|
||||
var cmpab : base.arm_neon_u16x8
|
||||
var cmpac : base.arm_neon_u16x8
|
||||
var picka : base.arm_neon_u8x8
|
||||
var pickb : base.arm_neon_u8x8
|
||||
|
||||
iterate (curr = args.curr, prev = args.prev)(length: 4, advance: 4, unroll: 2) {
|
||||
fb = util.make_u32x2_repeat(a: prev.peek_u32le()).as_u8x8()
|
||||
fx = util.make_u32x2_repeat(a: curr.peek_u32le()).as_u8x8()
|
||||
iterate (curr = args.curr, prev = args.prev)(length: 4, advance: 4, unroll: 2) {
|
||||
fb = util.make_u32x2_repeat(a: prev.peek_u32le()).as_u8x8()
|
||||
fx = util.make_u32x2_repeat(a: curr.peek_u32le()).as_u8x8()
|
||||
|
||||
fafb = fa.vaddl_u8(b: fb) // fafb = (fa + fb)
|
||||
fcfc = fc.vaddl_u8(b: fc) // fcfc = (fc + fc)
|
||||
fafb = fa.vaddl_u8(b: fb) // fafb = (fa + fb)
|
||||
fcfc = fc.vaddl_u8(b: fc) // fcfc = (fc + fc)
|
||||
|
||||
pa = fb.vabdl_u8(b: fc) // pa = abs(fa + fb - fc - fa)
|
||||
pb = fa.vabdl_u8(b: fc) // pb = abs(fa + fb - fc - fb)
|
||||
pc = fafb.vabdq_u16(b: fcfc) // pc = abs(fa + fb - fc - fc)
|
||||
pa = fb.vabdl_u8(b: fc) // pa = abs(fa + fb - fc - fa)
|
||||
pb = fa.vabdl_u8(b: fc) // pb = abs(fa + fb - fc - fb)
|
||||
pc = fafb.vabdq_u16(b: fcfc) // pc = abs(fa + fb - fc - fc)
|
||||
|
||||
cmpab = pa.vcleq_u16(b: pb) // cmpab = (pa <= pb)
|
||||
cmpac = pa.vcleq_u16(b: pc) // cmpac = (pa <= pc)
|
||||
cmpab = pa.vcleq_u16(b: pb) // cmpab = (pa <= pb)
|
||||
cmpac = pa.vcleq_u16(b: pc) // cmpac = (pa <= pc)
|
||||
|
||||
picka = cmpab.vandq_u16(b: cmpac).vmovn_u16() // picka = ((pa <= pb) && (pa <= pc))
|
||||
pickb = pb.vcleq_u16(b: pc).vmovn_u16() // pickb = (pb <= pc)
|
||||
picka = cmpab.vandq_u16(b: cmpac).vmovn_u16() // picka = ((pa <= pb) && (pa <= pc))
|
||||
pickb = pb.vcleq_u16(b: pc).vmovn_u16() // pickb = (pb <= pc)
|
||||
|
||||
// Add the predictor to the residual.
|
||||
fx = fx.vadd_u8(
|
||||
b: picka.vbsl_u8(b: fa,
|
||||
c: pickb.vbsl_u8(b: fb, c: fc)))
|
||||
// Add the predictor to the residual.
|
||||
fx = fx.vadd_u8(
|
||||
b: picka.vbsl_u8(b: fa,
|
||||
c: pickb.vbsl_u8(b: fb, c: fc)))
|
||||
|
||||
curr.poke_u32le!(a: fx.as_u32x2().vget_lane_u32(b: 0))
|
||||
fc = fb
|
||||
fa = fx
|
||||
}
|
||||
curr.poke_u32le!(a: fx.as_u32x2().vget_lane_u32(b: 0))
|
||||
fc = fb
|
||||
fa = fx
|
||||
}
|
||||
}
|
||||
@@ -17,517 +17,517 @@
|
||||
// Filter 1: Sub.
|
||||
|
||||
pri func decoder.filter_1!(curr: slice base.u8),
|
||||
choosy,
|
||||
choosy,
|
||||
{
|
||||
var filter_distance : base.u64[..= 8]
|
||||
var fa : base.u8
|
||||
var i_start : base.u64
|
||||
var i : base.u64
|
||||
var filter_distance : base.u64[..= 8]
|
||||
var fa : base.u8
|
||||
var i_start : base.u64
|
||||
var i : base.u64
|
||||
|
||||
filter_distance = this.filter_distance as base.u64
|
||||
i_start = 0
|
||||
while i_start < filter_distance {
|
||||
assert i_start < 8 via "a < b: a < c; c <= b"(c: filter_distance)
|
||||
fa = 0
|
||||
i = i_start
|
||||
while i < args.curr.length(),
|
||||
inv i_start < 8,
|
||||
{
|
||||
args.curr[i] = args.curr[i] ~mod+ fa
|
||||
fa = args.curr[i]
|
||||
i ~mod+= filter_distance
|
||||
} endwhile
|
||||
i_start += 1
|
||||
} endwhile
|
||||
filter_distance = this.filter_distance as base.u64
|
||||
i_start = 0
|
||||
while i_start < filter_distance {
|
||||
assert i_start < 8 via "a < b: a < c; c <= b"(c: filter_distance)
|
||||
fa = 0
|
||||
i = i_start
|
||||
while i < args.curr.length(),
|
||||
inv i_start < 8,
|
||||
{
|
||||
args.curr[i] = args.curr[i] ~mod+ fa
|
||||
fa = args.curr[i]
|
||||
i ~mod+= filter_distance
|
||||
} endwhile
|
||||
i_start += 1
|
||||
} endwhile
|
||||
}
|
||||
|
||||
pri func decoder.filter_1_distance_3_fallback!(curr: slice base.u8) {
|
||||
var curr : slice base.u8
|
||||
var curr : slice base.u8
|
||||
|
||||
var fa0 : base.u8
|
||||
var fa1 : base.u8
|
||||
var fa2 : base.u8
|
||||
var fa0 : base.u8
|
||||
var fa1 : base.u8
|
||||
var fa2 : base.u8
|
||||
|
||||
iterate (curr = args.curr)(length: 3, advance: 3, unroll: 2) {
|
||||
fa0 = fa0 ~mod+ curr[0]
|
||||
curr[0] = fa0
|
||||
fa1 = fa1 ~mod+ curr[1]
|
||||
curr[1] = fa1
|
||||
fa2 = fa2 ~mod+ curr[2]
|
||||
curr[2] = fa2
|
||||
}
|
||||
iterate (curr = args.curr)(length: 3, advance: 3, unroll: 2) {
|
||||
fa0 = fa0 ~mod+ curr[0]
|
||||
curr[0] = fa0
|
||||
fa1 = fa1 ~mod+ curr[1]
|
||||
curr[1] = fa1
|
||||
fa2 = fa2 ~mod+ curr[2]
|
||||
curr[2] = fa2
|
||||
}
|
||||
}
|
||||
|
||||
pri func decoder.filter_1_distance_4_fallback!(curr: slice base.u8) {
|
||||
var curr : slice base.u8
|
||||
var curr : slice base.u8
|
||||
|
||||
var fa0 : base.u8
|
||||
var fa1 : base.u8
|
||||
var fa2 : base.u8
|
||||
var fa3 : base.u8
|
||||
var fa0 : base.u8
|
||||
var fa1 : base.u8
|
||||
var fa2 : base.u8
|
||||
var fa3 : base.u8
|
||||
|
||||
iterate (curr = args.curr)(length: 4, advance: 4, unroll: 1) {
|
||||
fa0 = fa0 ~mod+ curr[0]
|
||||
curr[0] = fa0
|
||||
fa1 = fa1 ~mod+ curr[1]
|
||||
curr[1] = fa1
|
||||
fa2 = fa2 ~mod+ curr[2]
|
||||
curr[2] = fa2
|
||||
fa3 = fa3 ~mod+ curr[3]
|
||||
curr[3] = fa3
|
||||
}
|
||||
iterate (curr = args.curr)(length: 4, advance: 4, unroll: 1) {
|
||||
fa0 = fa0 ~mod+ curr[0]
|
||||
curr[0] = fa0
|
||||
fa1 = fa1 ~mod+ curr[1]
|
||||
curr[1] = fa1
|
||||
fa2 = fa2 ~mod+ curr[2]
|
||||
curr[2] = fa2
|
||||
fa3 = fa3 ~mod+ curr[3]
|
||||
curr[3] = fa3
|
||||
}
|
||||
}
|
||||
|
||||
// Filter 2: Up.
|
||||
|
||||
pri func decoder.filter_2!(curr: slice base.u8, prev: slice base.u8) {
|
||||
var n : base.u64
|
||||
var i : base.u64
|
||||
var n : base.u64
|
||||
var i : base.u64
|
||||
|
||||
n = args.curr.length().min(a: args.prev.length())
|
||||
i = 0
|
||||
while i < n,
|
||||
inv n <= args.curr.length(),
|
||||
inv n <= args.prev.length(),
|
||||
{
|
||||
assert i < 0xFFFF_FFFF_FFFF_FFFF via "a < b: a < c; c <= b"(c: n)
|
||||
assert i < args.curr.length() via "a < b: a < c; c <= b"(c: n)
|
||||
assert i < args.prev.length() via "a < b: a < c; c <= b"(c: n)
|
||||
args.curr[i] = args.curr[i] ~mod+ (args.prev[i])
|
||||
i += 1
|
||||
} endwhile
|
||||
n = args.curr.length().min(a: args.prev.length())
|
||||
i = 0
|
||||
while i < n,
|
||||
inv n <= args.curr.length(),
|
||||
inv n <= args.prev.length(),
|
||||
{
|
||||
assert i < 0xFFFF_FFFF_FFFF_FFFF via "a < b: a < c; c <= b"(c: n)
|
||||
assert i < args.curr.length() via "a < b: a < c; c <= b"(c: n)
|
||||
assert i < args.prev.length() via "a < b: a < c; c <= b"(c: n)
|
||||
args.curr[i] = args.curr[i] ~mod+ (args.prev[i])
|
||||
i += 1
|
||||
} endwhile
|
||||
}
|
||||
|
||||
// Filter 3: Average.
|
||||
|
||||
pri func decoder.filter_3!(curr: slice base.u8, prev: slice base.u8),
|
||||
choosy,
|
||||
choosy,
|
||||
{
|
||||
var filter_distance : base.u64[..= 8]
|
||||
var n : base.u64
|
||||
var i : base.u64
|
||||
var filter_distance : base.u64[..= 8]
|
||||
var n : base.u64
|
||||
var i : base.u64
|
||||
|
||||
filter_distance = this.filter_distance as base.u64
|
||||
if args.prev.length() == 0 {
|
||||
i = filter_distance
|
||||
assert i >= filter_distance via "a >= b: a == b"()
|
||||
while i < args.curr.length(),
|
||||
inv i >= filter_distance,
|
||||
{
|
||||
assert i < 0xFFFF_FFFF_FFFF_FFFF via "a < b: a < c; c <= b"(c: args.curr.length())
|
||||
assert (i - filter_distance) < args.curr.length() via "(a - b) < c: a < c; 0 <= b"()
|
||||
args.curr[i] = args.curr[i] ~mod+ (args.curr[i - filter_distance] / 2)
|
||||
i += 1
|
||||
assert i >= filter_distance via "a >= b: a >= (b + c); 0 <= c"(c: 1)
|
||||
} endwhile
|
||||
filter_distance = this.filter_distance as base.u64
|
||||
if args.prev.length() == 0 {
|
||||
i = filter_distance
|
||||
assert i >= filter_distance via "a >= b: a == b"()
|
||||
while i < args.curr.length(),
|
||||
inv i >= filter_distance,
|
||||
{
|
||||
assert i < 0xFFFF_FFFF_FFFF_FFFF via "a < b: a < c; c <= b"(c: args.curr.length())
|
||||
assert (i - filter_distance) < args.curr.length() via "(a - b) < c: a < c; 0 <= b"()
|
||||
args.curr[i] = args.curr[i] ~mod+ (args.curr[i - filter_distance] / 2)
|
||||
i += 1
|
||||
assert i >= filter_distance via "a >= b: a >= (b + c); 0 <= c"(c: 1)
|
||||
} endwhile
|
||||
|
||||
} else {
|
||||
n = args.curr.length().min(a: args.prev.length())
|
||||
i = 0
|
||||
while (i < n) and (i < filter_distance),
|
||||
inv n <= args.curr.length(),
|
||||
inv n <= args.prev.length(),
|
||||
{
|
||||
assert i < 0xFFFF_FFFF_FFFF_FFFF via "a < b: a < c; c <= b"(c: n)
|
||||
assert i < args.curr.length() via "a < b: a < c; c <= b"(c: n)
|
||||
assert i < args.prev.length() via "a < b: a < c; c <= b"(c: n)
|
||||
args.curr[i] = args.curr[i] ~mod+ (args.prev[i] / 2)
|
||||
i += 1
|
||||
} endwhile
|
||||
} else {
|
||||
n = args.curr.length().min(a: args.prev.length())
|
||||
i = 0
|
||||
while (i < n) and (i < filter_distance),
|
||||
inv n <= args.curr.length(),
|
||||
inv n <= args.prev.length(),
|
||||
{
|
||||
assert i < 0xFFFF_FFFF_FFFF_FFFF via "a < b: a < c; c <= b"(c: n)
|
||||
assert i < args.curr.length() via "a < b: a < c; c <= b"(c: n)
|
||||
assert i < args.prev.length() via "a < b: a < c; c <= b"(c: n)
|
||||
args.curr[i] = args.curr[i] ~mod+ (args.prev[i] / 2)
|
||||
i += 1
|
||||
} endwhile
|
||||
|
||||
i = filter_distance
|
||||
assert i >= filter_distance via "a >= b: a == b"()
|
||||
while i < n,
|
||||
inv i >= filter_distance,
|
||||
inv n <= args.curr.length(),
|
||||
inv n <= args.prev.length(),
|
||||
{
|
||||
assert i < 0xFFFF_FFFF_FFFF_FFFF via "a < b: a < c; c <= b"(c: n)
|
||||
assert i < args.curr.length() via "a < b: a < c; c <= b"(c: n)
|
||||
assert i < args.prev.length() via "a < b: a < c; c <= b"(c: n)
|
||||
assert (i - filter_distance) < args.curr.length() via "(a - b) < c: a < c; 0 <= b"()
|
||||
args.curr[i] = args.curr[i] ~mod+ (((
|
||||
(args.curr[i - filter_distance] as base.u32) +
|
||||
(args.prev[i] as base.u32)) / 2) as base.u8)
|
||||
i += 1
|
||||
assert i >= filter_distance via "a >= b: a >= (b + c); 0 <= c"(c: 1)
|
||||
} endwhile
|
||||
}
|
||||
i = filter_distance
|
||||
assert i >= filter_distance via "a >= b: a == b"()
|
||||
while i < n,
|
||||
inv i >= filter_distance,
|
||||
inv n <= args.curr.length(),
|
||||
inv n <= args.prev.length(),
|
||||
{
|
||||
assert i < 0xFFFF_FFFF_FFFF_FFFF via "a < b: a < c; c <= b"(c: n)
|
||||
assert i < args.curr.length() via "a < b: a < c; c <= b"(c: n)
|
||||
assert i < args.prev.length() via "a < b: a < c; c <= b"(c: n)
|
||||
assert (i - filter_distance) < args.curr.length() via "(a - b) < c: a < c; 0 <= b"()
|
||||
args.curr[i] = args.curr[i] ~mod+ (((
|
||||
(args.curr[i - filter_distance] as base.u32) +
|
||||
(args.prev[i] as base.u32)) / 2) as base.u8)
|
||||
i += 1
|
||||
assert i >= filter_distance via "a >= b: a >= (b + c); 0 <= c"(c: 1)
|
||||
} endwhile
|
||||
}
|
||||
}
|
||||
|
||||
pri func decoder.filter_3_distance_3_fallback!(curr: slice base.u8, prev: slice base.u8) {
|
||||
var curr : slice base.u8
|
||||
var prev : slice base.u8
|
||||
var curr : slice base.u8
|
||||
var prev : slice base.u8
|
||||
|
||||
var fa0 : base.u8
|
||||
var fa1 : base.u8
|
||||
var fa2 : base.u8
|
||||
var fa0 : base.u8
|
||||
var fa1 : base.u8
|
||||
var fa2 : base.u8
|
||||
|
||||
if args.prev.length() == 0 {
|
||||
iterate (curr = args.curr)(length: 3, advance: 3, unroll: 2) {
|
||||
fa0 = (fa0 / 2) ~mod+ curr[0]
|
||||
curr[0] = fa0
|
||||
fa1 = (fa1 / 2) ~mod+ curr[1]
|
||||
curr[1] = fa1
|
||||
fa2 = (fa2 / 2) ~mod+ curr[2]
|
||||
curr[2] = fa2
|
||||
}
|
||||
if args.prev.length() == 0 {
|
||||
iterate (curr = args.curr)(length: 3, advance: 3, unroll: 2) {
|
||||
fa0 = (fa0 / 2) ~mod+ curr[0]
|
||||
curr[0] = fa0
|
||||
fa1 = (fa1 / 2) ~mod+ curr[1]
|
||||
curr[1] = fa1
|
||||
fa2 = (fa2 / 2) ~mod+ curr[2]
|
||||
curr[2] = fa2
|
||||
}
|
||||
|
||||
} else {
|
||||
iterate (curr = args.curr, prev = args.prev)(length: 3, advance: 3, unroll: 2) {
|
||||
fa0 = ((((fa0 as base.u32) + (prev[0] as base.u32)) / 2) as base.u8) ~mod+ curr[0]
|
||||
curr[0] = fa0
|
||||
fa1 = ((((fa1 as base.u32) + (prev[1] as base.u32)) / 2) as base.u8) ~mod+ curr[1]
|
||||
curr[1] = fa1
|
||||
fa2 = ((((fa2 as base.u32) + (prev[2] as base.u32)) / 2) as base.u8) ~mod+ curr[2]
|
||||
curr[2] = fa2
|
||||
}
|
||||
}
|
||||
} else {
|
||||
iterate (curr = args.curr, prev = args.prev)(length: 3, advance: 3, unroll: 2) {
|
||||
fa0 = ((((fa0 as base.u32) + (prev[0] as base.u32)) / 2) as base.u8) ~mod+ curr[0]
|
||||
curr[0] = fa0
|
||||
fa1 = ((((fa1 as base.u32) + (prev[1] as base.u32)) / 2) as base.u8) ~mod+ curr[1]
|
||||
curr[1] = fa1
|
||||
fa2 = ((((fa2 as base.u32) + (prev[2] as base.u32)) / 2) as base.u8) ~mod+ curr[2]
|
||||
curr[2] = fa2
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pri func decoder.filter_3_distance_4_fallback!(curr: slice base.u8, prev: slice base.u8) {
|
||||
var curr : slice base.u8
|
||||
var prev : slice base.u8
|
||||
var curr : slice base.u8
|
||||
var prev : slice base.u8
|
||||
|
||||
var fa0 : base.u8
|
||||
var fa1 : base.u8
|
||||
var fa2 : base.u8
|
||||
var fa3 : base.u8
|
||||
var fa0 : base.u8
|
||||
var fa1 : base.u8
|
||||
var fa2 : base.u8
|
||||
var fa3 : base.u8
|
||||
|
||||
if args.prev.length() == 0 {
|
||||
iterate (curr = args.curr)(length: 4, advance: 4, unroll: 1) {
|
||||
fa0 = (fa0 / 2) ~mod+ curr[0]
|
||||
curr[0] = fa0
|
||||
fa1 = (fa1 / 2) ~mod+ curr[1]
|
||||
curr[1] = fa1
|
||||
fa2 = (fa2 / 2) ~mod+ curr[2]
|
||||
curr[2] = fa2
|
||||
fa3 = (fa3 / 2) ~mod+ curr[3]
|
||||
curr[3] = fa3
|
||||
}
|
||||
if args.prev.length() == 0 {
|
||||
iterate (curr = args.curr)(length: 4, advance: 4, unroll: 1) {
|
||||
fa0 = (fa0 / 2) ~mod+ curr[0]
|
||||
curr[0] = fa0
|
||||
fa1 = (fa1 / 2) ~mod+ curr[1]
|
||||
curr[1] = fa1
|
||||
fa2 = (fa2 / 2) ~mod+ curr[2]
|
||||
curr[2] = fa2
|
||||
fa3 = (fa3 / 2) ~mod+ curr[3]
|
||||
curr[3] = fa3
|
||||
}
|
||||
|
||||
} else {
|
||||
iterate (curr = args.curr, prev = args.prev)(length: 4, advance: 4, unroll: 1) {
|
||||
fa0 = ((((fa0 as base.u32) + (prev[0] as base.u32)) / 2) as base.u8) ~mod+ curr[0]
|
||||
curr[0] = fa0
|
||||
fa1 = ((((fa1 as base.u32) + (prev[1] as base.u32)) / 2) as base.u8) ~mod+ curr[1]
|
||||
curr[1] = fa1
|
||||
fa2 = ((((fa2 as base.u32) + (prev[2] as base.u32)) / 2) as base.u8) ~mod+ curr[2]
|
||||
curr[2] = fa2
|
||||
fa3 = ((((fa3 as base.u32) + (prev[3] as base.u32)) / 2) as base.u8) ~mod+ curr[3]
|
||||
curr[3] = fa3
|
||||
}
|
||||
}
|
||||
} else {
|
||||
iterate (curr = args.curr, prev = args.prev)(length: 4, advance: 4, unroll: 1) {
|
||||
fa0 = ((((fa0 as base.u32) + (prev[0] as base.u32)) / 2) as base.u8) ~mod+ curr[0]
|
||||
curr[0] = fa0
|
||||
fa1 = ((((fa1 as base.u32) + (prev[1] as base.u32)) / 2) as base.u8) ~mod+ curr[1]
|
||||
curr[1] = fa1
|
||||
fa2 = ((((fa2 as base.u32) + (prev[2] as base.u32)) / 2) as base.u8) ~mod+ curr[2]
|
||||
curr[2] = fa2
|
||||
fa3 = ((((fa3 as base.u32) + (prev[3] as base.u32)) / 2) as base.u8) ~mod+ curr[3]
|
||||
curr[3] = fa3
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Filter 4: Paeth.
|
||||
|
||||
pri func decoder.filter_4!(curr: slice base.u8, prev: slice base.u8),
|
||||
choosy,
|
||||
choosy,
|
||||
{
|
||||
var filter_distance : base.u64[..= 8]
|
||||
var n : base.u64
|
||||
var i : base.u64
|
||||
var filter_distance : base.u64[..= 8]
|
||||
var n : base.u64
|
||||
var i : base.u64
|
||||
|
||||
var fa : base.u32
|
||||
var fb : base.u32
|
||||
var fc : base.u32
|
||||
var pp : base.u32
|
||||
var pa : base.u32
|
||||
var pb : base.u32
|
||||
var pc : base.u32
|
||||
var fa : base.u32
|
||||
var fb : base.u32
|
||||
var fc : base.u32
|
||||
var pp : base.u32
|
||||
var pa : base.u32
|
||||
var pb : base.u32
|
||||
var pc : base.u32
|
||||
|
||||
filter_distance = this.filter_distance as base.u64
|
||||
n = args.curr.length().min(a: args.prev.length())
|
||||
i = 0
|
||||
while (i < n) and (i < filter_distance),
|
||||
inv n <= args.curr.length(),
|
||||
inv n <= args.prev.length(),
|
||||
{
|
||||
assert i < 0xFFFF_FFFF_FFFF_FFFF via "a < b: a < c; c <= b"(c: n)
|
||||
assert i < args.curr.length() via "a < b: a < c; c <= b"(c: n)
|
||||
assert i < args.prev.length() via "a < b: a < c; c <= b"(c: n)
|
||||
args.curr[i] = args.curr[i] ~mod+ args.prev[i]
|
||||
i += 1
|
||||
} endwhile
|
||||
filter_distance = this.filter_distance as base.u64
|
||||
n = args.curr.length().min(a: args.prev.length())
|
||||
i = 0
|
||||
while (i < n) and (i < filter_distance),
|
||||
inv n <= args.curr.length(),
|
||||
inv n <= args.prev.length(),
|
||||
{
|
||||
assert i < 0xFFFF_FFFF_FFFF_FFFF via "a < b: a < c; c <= b"(c: n)
|
||||
assert i < args.curr.length() via "a < b: a < c; c <= b"(c: n)
|
||||
assert i < args.prev.length() via "a < b: a < c; c <= b"(c: n)
|
||||
args.curr[i] = args.curr[i] ~mod+ args.prev[i]
|
||||
i += 1
|
||||
} endwhile
|
||||
|
||||
i = filter_distance
|
||||
assert i >= filter_distance via "a >= b: a == b"()
|
||||
while i < n,
|
||||
inv i >= filter_distance,
|
||||
inv n <= args.curr.length(),
|
||||
inv n <= args.prev.length(),
|
||||
{
|
||||
assert i < 0xFFFF_FFFF_FFFF_FFFF via "a < b: a < c; c <= b"(c: n)
|
||||
assert i < args.curr.length() via "a < b: a < c; c <= b"(c: n)
|
||||
assert i < args.prev.length() via "a < b: a < c; c <= b"(c: n)
|
||||
assert (i - filter_distance) < args.curr.length() via "(a - b) < c: a < c; 0 <= b"()
|
||||
assert (i - filter_distance) < args.prev.length() via "(a - b) < c: a < c; 0 <= b"()
|
||||
fa = args.curr[i - filter_distance] as base.u32
|
||||
fb = args.prev[i] as base.u32
|
||||
fc = args.prev[i - filter_distance] as base.u32
|
||||
pp = (fa ~mod+ fb) ~mod- fc
|
||||
pa = pp ~mod- fa
|
||||
if pa >= 0x8000_0000 {
|
||||
pa = 0 ~mod- pa
|
||||
}
|
||||
pb = pp ~mod- fb
|
||||
if pb >= 0x8000_0000 {
|
||||
pb = 0 ~mod- pb
|
||||
}
|
||||
pc = pp ~mod- fc
|
||||
if pc >= 0x8000_0000 {
|
||||
pc = 0 ~mod- pc
|
||||
}
|
||||
if (pa <= pb) and (pa <= pc) {
|
||||
// No-op.
|
||||
} else if pb <= pc {
|
||||
fa = fb
|
||||
} else {
|
||||
fa = fc
|
||||
}
|
||||
args.curr[i] = args.curr[i] ~mod+ ((fa & 0xFF) as base.u8)
|
||||
i += 1
|
||||
assert i >= filter_distance via "a >= b: a >= (b + c); 0 <= c"(c: 1)
|
||||
} endwhile
|
||||
i = filter_distance
|
||||
assert i >= filter_distance via "a >= b: a == b"()
|
||||
while i < n,
|
||||
inv i >= filter_distance,
|
||||
inv n <= args.curr.length(),
|
||||
inv n <= args.prev.length(),
|
||||
{
|
||||
assert i < 0xFFFF_FFFF_FFFF_FFFF via "a < b: a < c; c <= b"(c: n)
|
||||
assert i < args.curr.length() via "a < b: a < c; c <= b"(c: n)
|
||||
assert i < args.prev.length() via "a < b: a < c; c <= b"(c: n)
|
||||
assert (i - filter_distance) < args.curr.length() via "(a - b) < c: a < c; 0 <= b"()
|
||||
assert (i - filter_distance) < args.prev.length() via "(a - b) < c: a < c; 0 <= b"()
|
||||
fa = args.curr[i - filter_distance] as base.u32
|
||||
fb = args.prev[i] as base.u32
|
||||
fc = args.prev[i - filter_distance] as base.u32
|
||||
pp = (fa ~mod+ fb) ~mod- fc
|
||||
pa = pp ~mod- fa
|
||||
if pa >= 0x8000_0000 {
|
||||
pa = 0 ~mod- pa
|
||||
}
|
||||
pb = pp ~mod- fb
|
||||
if pb >= 0x8000_0000 {
|
||||
pb = 0 ~mod- pb
|
||||
}
|
||||
pc = pp ~mod- fc
|
||||
if pc >= 0x8000_0000 {
|
||||
pc = 0 ~mod- pc
|
||||
}
|
||||
if (pa <= pb) and (pa <= pc) {
|
||||
// No-op.
|
||||
} else if pb <= pc {
|
||||
fa = fb
|
||||
} else {
|
||||
fa = fc
|
||||
}
|
||||
args.curr[i] = args.curr[i] ~mod+ ((fa & 0xFF) as base.u8)
|
||||
i += 1
|
||||
assert i >= filter_distance via "a >= b: a >= (b + c); 0 <= c"(c: 1)
|
||||
} endwhile
|
||||
}
|
||||
|
||||
pri func decoder.filter_4_distance_3_fallback!(curr: slice base.u8, prev: slice base.u8) {
|
||||
var curr : slice base.u8
|
||||
var prev : slice base.u8
|
||||
var curr : slice base.u8
|
||||
var prev : slice base.u8
|
||||
|
||||
var fa0 : base.u32
|
||||
var fa1 : base.u32
|
||||
var fa2 : base.u32
|
||||
var fb0 : base.u32
|
||||
var fb1 : base.u32
|
||||
var fb2 : base.u32
|
||||
var fc0 : base.u32
|
||||
var fc1 : base.u32
|
||||
var fc2 : base.u32
|
||||
var pp0 : base.u32
|
||||
var pp1 : base.u32
|
||||
var pp2 : base.u32
|
||||
var pa0 : base.u32
|
||||
var pa1 : base.u32
|
||||
var pa2 : base.u32
|
||||
var pb0 : base.u32
|
||||
var pb1 : base.u32
|
||||
var pb2 : base.u32
|
||||
var pc0 : base.u32
|
||||
var pc1 : base.u32
|
||||
var pc2 : base.u32
|
||||
var fa0 : base.u32
|
||||
var fa1 : base.u32
|
||||
var fa2 : base.u32
|
||||
var fb0 : base.u32
|
||||
var fb1 : base.u32
|
||||
var fb2 : base.u32
|
||||
var fc0 : base.u32
|
||||
var fc1 : base.u32
|
||||
var fc2 : base.u32
|
||||
var pp0 : base.u32
|
||||
var pp1 : base.u32
|
||||
var pp2 : base.u32
|
||||
var pa0 : base.u32
|
||||
var pa1 : base.u32
|
||||
var pa2 : base.u32
|
||||
var pb0 : base.u32
|
||||
var pb1 : base.u32
|
||||
var pb2 : base.u32
|
||||
var pc0 : base.u32
|
||||
var pc1 : base.u32
|
||||
var pc2 : base.u32
|
||||
|
||||
iterate (curr = args.curr, prev = args.prev)(length: 3, advance: 3, unroll: 1) {
|
||||
fb0 = prev[0] as base.u32
|
||||
pp0 = (fa0 ~mod+ fb0) ~mod- fc0
|
||||
pa0 = pp0 ~mod- fa0
|
||||
if pa0 >= 0x8000_0000 {
|
||||
pa0 = 0 ~mod- pa0
|
||||
}
|
||||
pb0 = pp0 ~mod- fb0
|
||||
if pb0 >= 0x8000_0000 {
|
||||
pb0 = 0 ~mod- pb0
|
||||
}
|
||||
pc0 = pp0 ~mod- fc0
|
||||
if pc0 >= 0x8000_0000 {
|
||||
pc0 = 0 ~mod- pc0
|
||||
}
|
||||
if (pa0 <= pb0) and (pa0 <= pc0) {
|
||||
// No-op.
|
||||
} else if pb0 <= pc0 {
|
||||
fa0 = fb0
|
||||
} else {
|
||||
fa0 = fc0
|
||||
}
|
||||
curr[0] = curr[0] ~mod+ ((fa0 & 0xFF) as base.u8)
|
||||
fa0 = curr[0] as base.u32
|
||||
fc0 = fb0
|
||||
iterate (curr = args.curr, prev = args.prev)(length: 3, advance: 3, unroll: 1) {
|
||||
fb0 = prev[0] as base.u32
|
||||
pp0 = (fa0 ~mod+ fb0) ~mod- fc0
|
||||
pa0 = pp0 ~mod- fa0
|
||||
if pa0 >= 0x8000_0000 {
|
||||
pa0 = 0 ~mod- pa0
|
||||
}
|
||||
pb0 = pp0 ~mod- fb0
|
||||
if pb0 >= 0x8000_0000 {
|
||||
pb0 = 0 ~mod- pb0
|
||||
}
|
||||
pc0 = pp0 ~mod- fc0
|
||||
if pc0 >= 0x8000_0000 {
|
||||
pc0 = 0 ~mod- pc0
|
||||
}
|
||||
if (pa0 <= pb0) and (pa0 <= pc0) {
|
||||
// No-op.
|
||||
} else if pb0 <= pc0 {
|
||||
fa0 = fb0
|
||||
} else {
|
||||
fa0 = fc0
|
||||
}
|
||||
curr[0] = curr[0] ~mod+ ((fa0 & 0xFF) as base.u8)
|
||||
fa0 = curr[0] as base.u32
|
||||
fc0 = fb0
|
||||
|
||||
fb1 = prev[1] as base.u32
|
||||
pp1 = (fa1 ~mod+ fb1) ~mod- fc1
|
||||
pa1 = pp1 ~mod- fa1
|
||||
if pa1 >= 0x8000_0000 {
|
||||
pa1 = 0 ~mod- pa1
|
||||
}
|
||||
pb1 = pp1 ~mod- fb1
|
||||
if pb1 >= 0x8000_0000 {
|
||||
pb1 = 0 ~mod- pb1
|
||||
}
|
||||
pc1 = pp1 ~mod- fc1
|
||||
if pc1 >= 0x8000_0000 {
|
||||
pc1 = 0 ~mod- pc1
|
||||
}
|
||||
if (pa1 <= pb1) and (pa1 <= pc1) {
|
||||
// No-op.
|
||||
} else if pb1 <= pc1 {
|
||||
fa1 = fb1
|
||||
} else {
|
||||
fa1 = fc1
|
||||
}
|
||||
curr[1] = curr[1] ~mod+ ((fa1 & 0xFF) as base.u8)
|
||||
fa1 = curr[1] as base.u32
|
||||
fc1 = fb1
|
||||
fb1 = prev[1] as base.u32
|
||||
pp1 = (fa1 ~mod+ fb1) ~mod- fc1
|
||||
pa1 = pp1 ~mod- fa1
|
||||
if pa1 >= 0x8000_0000 {
|
||||
pa1 = 0 ~mod- pa1
|
||||
}
|
||||
pb1 = pp1 ~mod- fb1
|
||||
if pb1 >= 0x8000_0000 {
|
||||
pb1 = 0 ~mod- pb1
|
||||
}
|
||||
pc1 = pp1 ~mod- fc1
|
||||
if pc1 >= 0x8000_0000 {
|
||||
pc1 = 0 ~mod- pc1
|
||||
}
|
||||
if (pa1 <= pb1) and (pa1 <= pc1) {
|
||||
// No-op.
|
||||
} else if pb1 <= pc1 {
|
||||
fa1 = fb1
|
||||
} else {
|
||||
fa1 = fc1
|
||||
}
|
||||
curr[1] = curr[1] ~mod+ ((fa1 & 0xFF) as base.u8)
|
||||
fa1 = curr[1] as base.u32
|
||||
fc1 = fb1
|
||||
|
||||
fb2 = prev[2] as base.u32
|
||||
pp2 = (fa2 ~mod+ fb2) ~mod- fc2
|
||||
pa2 = pp2 ~mod- fa2
|
||||
if pa2 >= 0x8000_0000 {
|
||||
pa2 = 0 ~mod- pa2
|
||||
}
|
||||
pb2 = pp2 ~mod- fb2
|
||||
if pb2 >= 0x8000_0000 {
|
||||
pb2 = 0 ~mod- pb2
|
||||
}
|
||||
pc2 = pp2 ~mod- fc2
|
||||
if pc2 >= 0x8000_0000 {
|
||||
pc2 = 0 ~mod- pc2
|
||||
}
|
||||
if (pa2 <= pb2) and (pa2 <= pc2) {
|
||||
// No-op.
|
||||
} else if pb2 <= pc2 {
|
||||
fa2 = fb2
|
||||
} else {
|
||||
fa2 = fc2
|
||||
}
|
||||
curr[2] = curr[2] ~mod+ ((fa2 & 0xFF) as base.u8)
|
||||
fa2 = curr[2] as base.u32
|
||||
fc2 = fb2
|
||||
}
|
||||
fb2 = prev[2] as base.u32
|
||||
pp2 = (fa2 ~mod+ fb2) ~mod- fc2
|
||||
pa2 = pp2 ~mod- fa2
|
||||
if pa2 >= 0x8000_0000 {
|
||||
pa2 = 0 ~mod- pa2
|
||||
}
|
||||
pb2 = pp2 ~mod- fb2
|
||||
if pb2 >= 0x8000_0000 {
|
||||
pb2 = 0 ~mod- pb2
|
||||
}
|
||||
pc2 = pp2 ~mod- fc2
|
||||
if pc2 >= 0x8000_0000 {
|
||||
pc2 = 0 ~mod- pc2
|
||||
}
|
||||
if (pa2 <= pb2) and (pa2 <= pc2) {
|
||||
// No-op.
|
||||
} else if pb2 <= pc2 {
|
||||
fa2 = fb2
|
||||
} else {
|
||||
fa2 = fc2
|
||||
}
|
||||
curr[2] = curr[2] ~mod+ ((fa2 & 0xFF) as base.u8)
|
||||
fa2 = curr[2] as base.u32
|
||||
fc2 = fb2
|
||||
}
|
||||
}
|
||||
|
||||
pri func decoder.filter_4_distance_4_fallback!(curr: slice base.u8, prev: slice base.u8) {
|
||||
var curr : slice base.u8
|
||||
var prev : slice base.u8
|
||||
var curr : slice base.u8
|
||||
var prev : slice base.u8
|
||||
|
||||
var fa0 : base.u32
|
||||
var fa1 : base.u32
|
||||
var fa2 : base.u32
|
||||
var fa3 : base.u32
|
||||
var fb0 : base.u32
|
||||
var fb1 : base.u32
|
||||
var fb2 : base.u32
|
||||
var fb3 : base.u32
|
||||
var fc0 : base.u32
|
||||
var fc1 : base.u32
|
||||
var fc2 : base.u32
|
||||
var fc3 : base.u32
|
||||
var pp0 : base.u32
|
||||
var pp1 : base.u32
|
||||
var pp2 : base.u32
|
||||
var pp3 : base.u32
|
||||
var pa0 : base.u32
|
||||
var pa1 : base.u32
|
||||
var pa2 : base.u32
|
||||
var pa3 : base.u32
|
||||
var pb0 : base.u32
|
||||
var pb1 : base.u32
|
||||
var pb2 : base.u32
|
||||
var pb3 : base.u32
|
||||
var pc0 : base.u32
|
||||
var pc1 : base.u32
|
||||
var pc2 : base.u32
|
||||
var pc3 : base.u32
|
||||
var fa0 : base.u32
|
||||
var fa1 : base.u32
|
||||
var fa2 : base.u32
|
||||
var fa3 : base.u32
|
||||
var fb0 : base.u32
|
||||
var fb1 : base.u32
|
||||
var fb2 : base.u32
|
||||
var fb3 : base.u32
|
||||
var fc0 : base.u32
|
||||
var fc1 : base.u32
|
||||
var fc2 : base.u32
|
||||
var fc3 : base.u32
|
||||
var pp0 : base.u32
|
||||
var pp1 : base.u32
|
||||
var pp2 : base.u32
|
||||
var pp3 : base.u32
|
||||
var pa0 : base.u32
|
||||
var pa1 : base.u32
|
||||
var pa2 : base.u32
|
||||
var pa3 : base.u32
|
||||
var pb0 : base.u32
|
||||
var pb1 : base.u32
|
||||
var pb2 : base.u32
|
||||
var pb3 : base.u32
|
||||
var pc0 : base.u32
|
||||
var pc1 : base.u32
|
||||
var pc2 : base.u32
|
||||
var pc3 : base.u32
|
||||
|
||||
iterate (curr = args.curr, prev = args.prev)(length: 4, advance: 4, unroll: 1) {
|
||||
fb0 = prev[0] as base.u32
|
||||
pp0 = (fa0 ~mod+ fb0) ~mod- fc0
|
||||
pa0 = pp0 ~mod- fa0
|
||||
if pa0 >= 0x8000_0000 {
|
||||
pa0 = 0 ~mod- pa0
|
||||
}
|
||||
pb0 = pp0 ~mod- fb0
|
||||
if pb0 >= 0x8000_0000 {
|
||||
pb0 = 0 ~mod- pb0
|
||||
}
|
||||
pc0 = pp0 ~mod- fc0
|
||||
if pc0 >= 0x8000_0000 {
|
||||
pc0 = 0 ~mod- pc0
|
||||
}
|
||||
if (pa0 <= pb0) and (pa0 <= pc0) {
|
||||
// No-op.
|
||||
} else if pb0 <= pc0 {
|
||||
fa0 = fb0
|
||||
} else {
|
||||
fa0 = fc0
|
||||
}
|
||||
curr[0] = curr[0] ~mod+ ((fa0 & 0xFF) as base.u8)
|
||||
fa0 = curr[0] as base.u32
|
||||
fc0 = fb0
|
||||
iterate (curr = args.curr, prev = args.prev)(length: 4, advance: 4, unroll: 1) {
|
||||
fb0 = prev[0] as base.u32
|
||||
pp0 = (fa0 ~mod+ fb0) ~mod- fc0
|
||||
pa0 = pp0 ~mod- fa0
|
||||
if pa0 >= 0x8000_0000 {
|
||||
pa0 = 0 ~mod- pa0
|
||||
}
|
||||
pb0 = pp0 ~mod- fb0
|
||||
if pb0 >= 0x8000_0000 {
|
||||
pb0 = 0 ~mod- pb0
|
||||
}
|
||||
pc0 = pp0 ~mod- fc0
|
||||
if pc0 >= 0x8000_0000 {
|
||||
pc0 = 0 ~mod- pc0
|
||||
}
|
||||
if (pa0 <= pb0) and (pa0 <= pc0) {
|
||||
// No-op.
|
||||
} else if pb0 <= pc0 {
|
||||
fa0 = fb0
|
||||
} else {
|
||||
fa0 = fc0
|
||||
}
|
||||
curr[0] = curr[0] ~mod+ ((fa0 & 0xFF) as base.u8)
|
||||
fa0 = curr[0] as base.u32
|
||||
fc0 = fb0
|
||||
|
||||
fb1 = prev[1] as base.u32
|
||||
pp1 = (fa1 ~mod+ fb1) ~mod- fc1
|
||||
pa1 = pp1 ~mod- fa1
|
||||
if pa1 >= 0x8000_0000 {
|
||||
pa1 = 0 ~mod- pa1
|
||||
}
|
||||
pb1 = pp1 ~mod- fb1
|
||||
if pb1 >= 0x8000_0000 {
|
||||
pb1 = 0 ~mod- pb1
|
||||
}
|
||||
pc1 = pp1 ~mod- fc1
|
||||
if pc1 >= 0x8000_0000 {
|
||||
pc1 = 0 ~mod- pc1
|
||||
}
|
||||
if (pa1 <= pb1) and (pa1 <= pc1) {
|
||||
// No-op.
|
||||
} else if pb1 <= pc1 {
|
||||
fa1 = fb1
|
||||
} else {
|
||||
fa1 = fc1
|
||||
}
|
||||
curr[1] = curr[1] ~mod+ ((fa1 & 0xFF) as base.u8)
|
||||
fa1 = curr[1] as base.u32
|
||||
fc1 = fb1
|
||||
fb1 = prev[1] as base.u32
|
||||
pp1 = (fa1 ~mod+ fb1) ~mod- fc1
|
||||
pa1 = pp1 ~mod- fa1
|
||||
if pa1 >= 0x8000_0000 {
|
||||
pa1 = 0 ~mod- pa1
|
||||
}
|
||||
pb1 = pp1 ~mod- fb1
|
||||
if pb1 >= 0x8000_0000 {
|
||||
pb1 = 0 ~mod- pb1
|
||||
}
|
||||
pc1 = pp1 ~mod- fc1
|
||||
if pc1 >= 0x8000_0000 {
|
||||
pc1 = 0 ~mod- pc1
|
||||
}
|
||||
if (pa1 <= pb1) and (pa1 <= pc1) {
|
||||
// No-op.
|
||||
} else if pb1 <= pc1 {
|
||||
fa1 = fb1
|
||||
} else {
|
||||
fa1 = fc1
|
||||
}
|
||||
curr[1] = curr[1] ~mod+ ((fa1 & 0xFF) as base.u8)
|
||||
fa1 = curr[1] as base.u32
|
||||
fc1 = fb1
|
||||
|
||||
fb2 = prev[2] as base.u32
|
||||
pp2 = (fa2 ~mod+ fb2) ~mod- fc2
|
||||
pa2 = pp2 ~mod- fa2
|
||||
if pa2 >= 0x8000_0000 {
|
||||
pa2 = 0 ~mod- pa2
|
||||
}
|
||||
pb2 = pp2 ~mod- fb2
|
||||
if pb2 >= 0x8000_0000 {
|
||||
pb2 = 0 ~mod- pb2
|
||||
}
|
||||
pc2 = pp2 ~mod- fc2
|
||||
if pc2 >= 0x8000_0000 {
|
||||
pc2 = 0 ~mod- pc2
|
||||
}
|
||||
if (pa2 <= pb2) and (pa2 <= pc2) {
|
||||
// No-op.
|
||||
} else if pb2 <= pc2 {
|
||||
fa2 = fb2
|
||||
} else {
|
||||
fa2 = fc2
|
||||
}
|
||||
curr[2] = curr[2] ~mod+ ((fa2 & 0xFF) as base.u8)
|
||||
fa2 = curr[2] as base.u32
|
||||
fc2 = fb2
|
||||
fb2 = prev[2] as base.u32
|
||||
pp2 = (fa2 ~mod+ fb2) ~mod- fc2
|
||||
pa2 = pp2 ~mod- fa2
|
||||
if pa2 >= 0x8000_0000 {
|
||||
pa2 = 0 ~mod- pa2
|
||||
}
|
||||
pb2 = pp2 ~mod- fb2
|
||||
if pb2 >= 0x8000_0000 {
|
||||
pb2 = 0 ~mod- pb2
|
||||
}
|
||||
pc2 = pp2 ~mod- fc2
|
||||
if pc2 >= 0x8000_0000 {
|
||||
pc2 = 0 ~mod- pc2
|
||||
}
|
||||
if (pa2 <= pb2) and (pa2 <= pc2) {
|
||||
// No-op.
|
||||
} else if pb2 <= pc2 {
|
||||
fa2 = fb2
|
||||
} else {
|
||||
fa2 = fc2
|
||||
}
|
||||
curr[2] = curr[2] ~mod+ ((fa2 & 0xFF) as base.u8)
|
||||
fa2 = curr[2] as base.u32
|
||||
fc2 = fb2
|
||||
|
||||
fb3 = prev[3] as base.u32
|
||||
pp3 = (fa3 ~mod+ fb3) ~mod- fc3
|
||||
pa3 = pp3 ~mod- fa3
|
||||
if pa3 >= 0x8000_0000 {
|
||||
pa3 = 0 ~mod- pa3
|
||||
}
|
||||
pb3 = pp3 ~mod- fb3
|
||||
if pb3 >= 0x8000_0000 {
|
||||
pb3 = 0 ~mod- pb3
|
||||
}
|
||||
pc3 = pp3 ~mod- fc3
|
||||
if pc3 >= 0x8000_0000 {
|
||||
pc3 = 0 ~mod- pc3
|
||||
}
|
||||
if (pa3 <= pb3) and (pa3 <= pc3) {
|
||||
// No-op.
|
||||
} else if pb3 <= pc3 {
|
||||
fa3 = fb3
|
||||
} else {
|
||||
fa3 = fc3
|
||||
}
|
||||
curr[3] = curr[3] ~mod+ ((fa3 & 0xFF) as base.u8)
|
||||
fa3 = curr[3] as base.u32
|
||||
fc3 = fb3
|
||||
}
|
||||
fb3 = prev[3] as base.u32
|
||||
pp3 = (fa3 ~mod+ fb3) ~mod- fc3
|
||||
pa3 = pp3 ~mod- fa3
|
||||
if pa3 >= 0x8000_0000 {
|
||||
pa3 = 0 ~mod- pa3
|
||||
}
|
||||
pb3 = pp3 ~mod- fb3
|
||||
if pb3 >= 0x8000_0000 {
|
||||
pb3 = 0 ~mod- pb3
|
||||
}
|
||||
pc3 = pp3 ~mod- fc3
|
||||
if pc3 >= 0x8000_0000 {
|
||||
pc3 = 0 ~mod- pc3
|
||||
}
|
||||
if (pa3 <= pb3) and (pa3 <= pc3) {
|
||||
// No-op.
|
||||
} else if pb3 <= pc3 {
|
||||
fa3 = fb3
|
||||
} else {
|
||||
fa3 = fc3
|
||||
}
|
||||
curr[3] = curr[3] ~mod+ ((fa3 & 0xFF) as base.u8)
|
||||
fa3 = curr[3] as base.u32
|
||||
fc3 = fb3
|
||||
}
|
||||
}
|
||||
@@ -45,20 +45,20 @@
|
||||
// "Have std/png filter_1_distance_? use more SIMD" for a pessimizing example.
|
||||
|
||||
pri func decoder.filter_1_distance_4_x86_sse42!(curr: slice base.u8),
|
||||
choose cpu_arch >= x86_sse42,
|
||||
choose cpu_arch >= x86_sse42,
|
||||
{
|
||||
var curr : slice base.u8
|
||||
var curr : slice base.u8
|
||||
|
||||
var util : base.x86_sse42_utility
|
||||
var x128 : base.x86_m128i
|
||||
var a128 : base.x86_m128i
|
||||
var util : base.x86_sse42_utility
|
||||
var x128 : base.x86_m128i
|
||||
var a128 : base.x86_m128i
|
||||
|
||||
iterate (curr = args.curr)(length: 4, advance: 4, unroll: 2) {
|
||||
x128 = util.make_m128i_single_u32(a: curr.peek_u32le())
|
||||
x128 = x128._mm_add_epi8(b: a128)
|
||||
a128 = x128
|
||||
curr.poke_u32le!(a: x128.truncate_u32())
|
||||
}
|
||||
iterate (curr = args.curr)(length: 4, advance: 4, unroll: 2) {
|
||||
x128 = util.make_m128i_single_u32(a: curr.peek_u32le())
|
||||
x128 = x128._mm_add_epi8(b: a128)
|
||||
a128 = x128
|
||||
curr.poke_u32le!(a: x128.truncate_u32())
|
||||
}
|
||||
}
|
||||
|
||||
// --------
|
||||
@@ -75,58 +75,58 @@ pri func decoder.filter_1_distance_4_x86_sse42!(curr: slice base.u8),
|
||||
// }
|
||||
|
||||
pri func decoder.filter_3_distance_4_x86_sse42!(curr: slice base.u8, prev: slice base.u8),
|
||||
choose cpu_arch >= x86_sse42,
|
||||
choose cpu_arch >= x86_sse42,
|
||||
{
|
||||
var curr : slice base.u8
|
||||
var prev : slice base.u8
|
||||
var curr : slice base.u8
|
||||
var prev : slice base.u8
|
||||
|
||||
var util : base.x86_sse42_utility
|
||||
var x128 : base.x86_m128i
|
||||
var a128 : base.x86_m128i
|
||||
var b128 : base.x86_m128i
|
||||
var p128 : base.x86_m128i
|
||||
var k128 : base.x86_m128i
|
||||
var util : base.x86_sse42_utility
|
||||
var x128 : base.x86_m128i
|
||||
var a128 : base.x86_m128i
|
||||
var b128 : base.x86_m128i
|
||||
var p128 : base.x86_m128i
|
||||
var k128 : base.x86_m128i
|
||||
|
||||
if args.prev.length() == 0 {
|
||||
k128 = util.make_m128i_repeat_u8(a: 0xFE)
|
||||
iterate (curr = args.curr)(length: 4, advance: 4, unroll: 2) {
|
||||
// The predictor, p128, is just half (rounded down) of the previous
|
||||
// pixel, a128. In this branch, b128 stays zero so the average of
|
||||
// a128 and b128 is just half of a128. _mm_avg_epu8 rounds up, but
|
||||
// (a128 & 0xFE_repeated) takes out the low bits of a128's bytes.
|
||||
p128 = a128._mm_and_si128(b: k128)._mm_avg_epu8(b: b128)
|
||||
if args.prev.length() == 0 {
|
||||
k128 = util.make_m128i_repeat_u8(a: 0xFE)
|
||||
iterate (curr = args.curr)(length: 4, advance: 4, unroll: 2) {
|
||||
// The predictor, p128, is just half (rounded down) of the previous
|
||||
// pixel, a128. In this branch, b128 stays zero so the average of
|
||||
// a128 and b128 is just half of a128. _mm_avg_epu8 rounds up, but
|
||||
// (a128 & 0xFE_repeated) takes out the low bits of a128's bytes.
|
||||
p128 = a128._mm_and_si128(b: k128)._mm_avg_epu8(b: b128)
|
||||
|
||||
// Add the predictor to the residual and, for the next iteration,
|
||||
// set its previous pixel, a128, to this one, x128.
|
||||
x128 = util.make_m128i_single_u32(a: curr.peek_u32le())
|
||||
x128 = x128._mm_add_epi8(b: p128)
|
||||
a128 = x128
|
||||
curr.poke_u32le!(a: x128.truncate_u32())
|
||||
}
|
||||
// Add the predictor to the residual and, for the next iteration,
|
||||
// set its previous pixel, a128, to this one, x128.
|
||||
x128 = util.make_m128i_single_u32(a: curr.peek_u32le())
|
||||
x128 = x128._mm_add_epi8(b: p128)
|
||||
a128 = x128
|
||||
curr.poke_u32le!(a: x128.truncate_u32())
|
||||
}
|
||||
|
||||
} else {
|
||||
k128 = util.make_m128i_repeat_u8(a: 0x01)
|
||||
iterate (curr = args.curr, prev = args.prev)(length: 4, advance: 4, unroll: 2) {
|
||||
// Load the pixel from the row above.
|
||||
b128 = util.make_m128i_single_u32(a: prev.peek_u32le())
|
||||
} else {
|
||||
k128 = util.make_m128i_repeat_u8(a: 0x01)
|
||||
iterate (curr = args.curr, prev = args.prev)(length: 4, advance: 4, unroll: 2) {
|
||||
// Load the pixel from the row above.
|
||||
b128 = util.make_m128i_single_u32(a: prev.peek_u32le())
|
||||
|
||||
// The predictor, p128, is the average (rounded down) of the
|
||||
// previous pixel, a128, and the pixel above, b128.
|
||||
p128 = a128._mm_avg_epu8(b: b128)
|
||||
// The predictor, p128, is the average (rounded down) of the
|
||||
// previous pixel, a128, and the pixel above, b128.
|
||||
p128 = a128._mm_avg_epu8(b: b128)
|
||||
|
||||
// Subtract a correction term because _mm_avg_epu8 rounds up but
|
||||
// the PNG filter rounds down. The correction term is the low bit
|
||||
// of each byte of (a128 ^ b128).
|
||||
p128 = p128._mm_sub_epi8(b: k128._mm_and_si128(b: a128._mm_xor_si128(b: b128)))
|
||||
// Subtract a correction term because _mm_avg_epu8 rounds up but
|
||||
// the PNG filter rounds down. The correction term is the low bit
|
||||
// of each byte of (a128 ^ b128).
|
||||
p128 = p128._mm_sub_epi8(b: k128._mm_and_si128(b: a128._mm_xor_si128(b: b128)))
|
||||
|
||||
// Add the predictor to the residual and, for the next iteration,
|
||||
// set its previous pixel, a128, to this one, x128.
|
||||
x128 = util.make_m128i_single_u32(a: curr.peek_u32le())
|
||||
x128 = x128._mm_add_epi8(b: p128)
|
||||
a128 = x128
|
||||
curr.poke_u32le!(a: x128.truncate_u32())
|
||||
}
|
||||
}
|
||||
// Add the predictor to the residual and, for the next iteration,
|
||||
// set its previous pixel, a128, to this one, x128.
|
||||
x128 = util.make_m128i_single_u32(a: curr.peek_u32le())
|
||||
x128 = x128._mm_add_epi8(b: p128)
|
||||
a128 = x128
|
||||
curr.poke_u32le!(a: x128.truncate_u32())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --------
|
||||
@@ -134,146 +134,146 @@ pri func decoder.filter_3_distance_4_x86_sse42!(curr: slice base.u8, prev: slice
|
||||
// Filter 4: Paeth.
|
||||
|
||||
pri func decoder.filter_4_distance_3_x86_sse42!(curr: slice base.u8, prev: slice base.u8),
|
||||
choose cpu_arch >= x86_sse42,
|
||||
choose cpu_arch >= x86_sse42,
|
||||
{
|
||||
// See the comments in filter_4_distance_4_x86_sse42 for an explanation of
|
||||
// how this works. That function's single loop is copied twice here, once
|
||||
// with "length: 4" and once with "length: 3". It's generally faster to
|
||||
// load 4 bytes at a time instead of 3.
|
||||
//
|
||||
// Differences between that function and this one are marked with a §.
|
||||
// See the comments in filter_4_distance_4_x86_sse42 for an explanation of
|
||||
// how this works. That function's single loop is copied twice here, once
|
||||
// with "length: 4" and once with "length: 3". It's generally faster to
|
||||
// load 4 bytes at a time instead of 3.
|
||||
//
|
||||
// Differences between that function and this one are marked with a §.
|
||||
|
||||
var curr : slice base.u8
|
||||
var prev : slice base.u8
|
||||
var curr : slice base.u8
|
||||
var prev : slice base.u8
|
||||
|
||||
var util : base.x86_sse42_utility
|
||||
var x128 : base.x86_m128i
|
||||
var a128 : base.x86_m128i
|
||||
var b128 : base.x86_m128i
|
||||
var c128 : base.x86_m128i
|
||||
var p128 : base.x86_m128i
|
||||
var pa128 : base.x86_m128i
|
||||
var pb128 : base.x86_m128i
|
||||
var pc128 : base.x86_m128i
|
||||
var smallest128 : base.x86_m128i
|
||||
var z128 : base.x86_m128i
|
||||
var util : base.x86_sse42_utility
|
||||
var x128 : base.x86_m128i
|
||||
var a128 : base.x86_m128i
|
||||
var b128 : base.x86_m128i
|
||||
var c128 : base.x86_m128i
|
||||
var p128 : base.x86_m128i
|
||||
var pa128 : base.x86_m128i
|
||||
var pb128 : base.x86_m128i
|
||||
var pc128 : base.x86_m128i
|
||||
var smallest128 : base.x86_m128i
|
||||
var z128 : base.x86_m128i
|
||||
|
||||
// § The advance is 3, not 4.
|
||||
iterate (curr = args.curr, prev = args.prev)(length: 4, advance: 3, unroll: 2) {
|
||||
b128 = util.make_m128i_single_u32(a: prev.peek_u32le())
|
||||
b128 = b128._mm_unpacklo_epi8(b: z128)
|
||||
pa128 = b128._mm_sub_epi16(b: c128)
|
||||
pb128 = a128._mm_sub_epi16(b: c128)
|
||||
pc128 = pa128._mm_add_epi16(b: pb128)
|
||||
pa128 = pa128._mm_abs_epi16()
|
||||
pb128 = pb128._mm_abs_epi16()
|
||||
pc128 = pc128._mm_abs_epi16()
|
||||
smallest128 = pc128._mm_min_epi16(b: pb128._mm_min_epi16(b: pa128))
|
||||
p128 = c128._mm_blendv_epi8(
|
||||
b: b128,
|
||||
mask: smallest128._mm_cmpeq_epi16(b: pb128))._mm_blendv_epi8(
|
||||
b: a128,
|
||||
mask: smallest128._mm_cmpeq_epi16(b: pa128))
|
||||
x128 = util.make_m128i_single_u32(a: curr.peek_u32le())
|
||||
x128 = x128._mm_unpacklo_epi8(b: z128)
|
||||
x128 = x128._mm_add_epi8(b: p128)
|
||||
a128 = x128
|
||||
c128 = b128
|
||||
x128 = x128._mm_packus_epi16(b: x128)
|
||||
// § poke_u24le replaces poke_u32le.
|
||||
curr.poke_u24le!(a: x128.truncate_u32())
|
||||
// § The advance is 3, not 4.
|
||||
iterate (curr = args.curr, prev = args.prev)(length: 4, advance: 3, unroll: 2) {
|
||||
b128 = util.make_m128i_single_u32(a: prev.peek_u32le())
|
||||
b128 = b128._mm_unpacklo_epi8(b: z128)
|
||||
pa128 = b128._mm_sub_epi16(b: c128)
|
||||
pb128 = a128._mm_sub_epi16(b: c128)
|
||||
pc128 = pa128._mm_add_epi16(b: pb128)
|
||||
pa128 = pa128._mm_abs_epi16()
|
||||
pb128 = pb128._mm_abs_epi16()
|
||||
pc128 = pc128._mm_abs_epi16()
|
||||
smallest128 = pc128._mm_min_epi16(b: pb128._mm_min_epi16(b: pa128))
|
||||
p128 = c128._mm_blendv_epi8(
|
||||
b: b128,
|
||||
mask: smallest128._mm_cmpeq_epi16(b: pb128))._mm_blendv_epi8(
|
||||
b: a128,
|
||||
mask: smallest128._mm_cmpeq_epi16(b: pa128))
|
||||
x128 = util.make_m128i_single_u32(a: curr.peek_u32le())
|
||||
x128 = x128._mm_unpacklo_epi8(b: z128)
|
||||
x128 = x128._mm_add_epi8(b: p128)
|
||||
a128 = x128
|
||||
c128 = b128
|
||||
x128 = x128._mm_packus_epi16(b: x128)
|
||||
// § poke_u24le replaces poke_u32le.
|
||||
curr.poke_u24le!(a: x128.truncate_u32())
|
||||
|
||||
// § The length and advance are both 3, not 4.
|
||||
} else (length: 3, advance: 3, unroll: 1) {
|
||||
// § peek_u24le_as_u32 replaces peek_u32le.
|
||||
b128 = util.make_m128i_single_u32(a: prev.peek_u24le_as_u32())
|
||||
b128 = b128._mm_unpacklo_epi8(b: z128)
|
||||
pa128 = b128._mm_sub_epi16(b: c128)
|
||||
pb128 = a128._mm_sub_epi16(b: c128)
|
||||
pc128 = pa128._mm_add_epi16(b: pb128)
|
||||
pa128 = pa128._mm_abs_epi16()
|
||||
pb128 = pb128._mm_abs_epi16()
|
||||
pc128 = pc128._mm_abs_epi16()
|
||||
smallest128 = pc128._mm_min_epi16(b: pb128._mm_min_epi16(b: pa128))
|
||||
p128 = c128._mm_blendv_epi8(
|
||||
b: b128,
|
||||
mask: smallest128._mm_cmpeq_epi16(b: pb128))._mm_blendv_epi8(
|
||||
b: a128,
|
||||
mask: smallest128._mm_cmpeq_epi16(b: pa128))
|
||||
// § peek_u24le_as_u32 replaces peek_u32le.
|
||||
x128 = util.make_m128i_single_u32(a: curr.peek_u24le_as_u32())
|
||||
x128 = x128._mm_unpacklo_epi8(b: z128)
|
||||
x128 = x128._mm_add_epi8(b: p128)
|
||||
// § These assignments are unnecessary; this is the last iteration.
|
||||
// a128 = x128
|
||||
// c128 = b128
|
||||
x128 = x128._mm_packus_epi16(b: x128)
|
||||
// § poke_u24le replaces poke_u32le.
|
||||
curr.poke_u24le!(a: x128.truncate_u32())
|
||||
}
|
||||
// § The length and advance are both 3, not 4.
|
||||
} else (length: 3, advance: 3, unroll: 1) {
|
||||
// § peek_u24le_as_u32 replaces peek_u32le.
|
||||
b128 = util.make_m128i_single_u32(a: prev.peek_u24le_as_u32())
|
||||
b128 = b128._mm_unpacklo_epi8(b: z128)
|
||||
pa128 = b128._mm_sub_epi16(b: c128)
|
||||
pb128 = a128._mm_sub_epi16(b: c128)
|
||||
pc128 = pa128._mm_add_epi16(b: pb128)
|
||||
pa128 = pa128._mm_abs_epi16()
|
||||
pb128 = pb128._mm_abs_epi16()
|
||||
pc128 = pc128._mm_abs_epi16()
|
||||
smallest128 = pc128._mm_min_epi16(b: pb128._mm_min_epi16(b: pa128))
|
||||
p128 = c128._mm_blendv_epi8(
|
||||
b: b128,
|
||||
mask: smallest128._mm_cmpeq_epi16(b: pb128))._mm_blendv_epi8(
|
||||
b: a128,
|
||||
mask: smallest128._mm_cmpeq_epi16(b: pa128))
|
||||
// § peek_u24le_as_u32 replaces peek_u32le.
|
||||
x128 = util.make_m128i_single_u32(a: curr.peek_u24le_as_u32())
|
||||
x128 = x128._mm_unpacklo_epi8(b: z128)
|
||||
x128 = x128._mm_add_epi8(b: p128)
|
||||
// § These assignments are unnecessary; this is the last iteration.
|
||||
// a128 = x128
|
||||
// c128 = b128
|
||||
x128 = x128._mm_packus_epi16(b: x128)
|
||||
// § poke_u24le replaces poke_u32le.
|
||||
curr.poke_u24le!(a: x128.truncate_u32())
|
||||
}
|
||||
}
|
||||
|
||||
pri func decoder.filter_4_distance_4_x86_sse42!(curr: slice base.u8, prev: slice base.u8),
|
||||
choose cpu_arch >= x86_sse42,
|
||||
choose cpu_arch >= x86_sse42,
|
||||
{
|
||||
var curr : slice base.u8
|
||||
var prev : slice base.u8
|
||||
var curr : slice base.u8
|
||||
var prev : slice base.u8
|
||||
|
||||
var util : base.x86_sse42_utility
|
||||
var x128 : base.x86_m128i
|
||||
var a128 : base.x86_m128i
|
||||
var b128 : base.x86_m128i
|
||||
var c128 : base.x86_m128i
|
||||
var p128 : base.x86_m128i
|
||||
var pa128 : base.x86_m128i
|
||||
var pb128 : base.x86_m128i
|
||||
var pc128 : base.x86_m128i
|
||||
var smallest128 : base.x86_m128i
|
||||
var z128 : base.x86_m128i
|
||||
var util : base.x86_sse42_utility
|
||||
var x128 : base.x86_m128i
|
||||
var a128 : base.x86_m128i
|
||||
var b128 : base.x86_m128i
|
||||
var c128 : base.x86_m128i
|
||||
var p128 : base.x86_m128i
|
||||
var pa128 : base.x86_m128i
|
||||
var pb128 : base.x86_m128i
|
||||
var pc128 : base.x86_m128i
|
||||
var smallest128 : base.x86_m128i
|
||||
var z128 : base.x86_m128i
|
||||
|
||||
iterate (curr = args.curr, prev = args.prev)(length: 4, advance: 4, unroll: 2) {
|
||||
// Load the pixel from the row above.
|
||||
b128 = util.make_m128i_single_u32(a: prev.peek_u32le())
|
||||
iterate (curr = args.curr, prev = args.prev)(length: 4, advance: 4, unroll: 2) {
|
||||
// Load the pixel from the row above.
|
||||
b128 = util.make_m128i_single_u32(a: prev.peek_u32le())
|
||||
|
||||
// Convert from u8 to i16 by unpacking it with zeroes.
|
||||
b128 = b128._mm_unpacklo_epi8(b: z128)
|
||||
// Convert from u8 to i16 by unpacking it with zeroes.
|
||||
b128 = b128._mm_unpacklo_epi8(b: z128)
|
||||
|
||||
// Compute:
|
||||
// - pa128 = (m128 - a128)
|
||||
// - pb128 = (m128 - b128)
|
||||
// - pc128 = (m128 - c128)
|
||||
// where m128 = (a128 + b128 - c128).
|
||||
pa128 = b128._mm_sub_epi16(b: c128)
|
||||
pb128 = a128._mm_sub_epi16(b: c128)
|
||||
pc128 = pa128._mm_add_epi16(b: pb128)
|
||||
// Compute:
|
||||
// - pa128 = (m128 - a128)
|
||||
// - pb128 = (m128 - b128)
|
||||
// - pc128 = (m128 - c128)
|
||||
// where m128 = (a128 + b128 - c128).
|
||||
pa128 = b128._mm_sub_epi16(b: c128)
|
||||
pb128 = a128._mm_sub_epi16(b: c128)
|
||||
pc128 = pa128._mm_add_epi16(b: pb128)
|
||||
|
||||
// Compute the smallest absolute value of pa128, pb128 and pc128.
|
||||
pa128 = pa128._mm_abs_epi16()
|
||||
pb128 = pb128._mm_abs_epi16()
|
||||
pc128 = pc128._mm_abs_epi16()
|
||||
smallest128 = pc128._mm_min_epi16(b: pb128._mm_min_epi16(b: pa128))
|
||||
// Compute the smallest absolute value of pa128, pb128 and pc128.
|
||||
pa128 = pa128._mm_abs_epi16()
|
||||
pb128 = pb128._mm_abs_epi16()
|
||||
pc128 = pc128._mm_abs_epi16()
|
||||
smallest128 = pc128._mm_min_epi16(b: pb128._mm_min_epi16(b: pa128))
|
||||
|
||||
// The predictor, p128, is whichever of a128, b128 or c128 such that
|
||||
// pa128, pb128 or pc128 matches this smallest absolute value. Ties are
|
||||
// broken in favor of a128 then b128 then c128.
|
||||
//
|
||||
// The a._mm_blendv_epi8(b, mask) method picks b when mask is true.
|
||||
p128 = c128._mm_blendv_epi8(
|
||||
b: b128,
|
||||
mask: smallest128._mm_cmpeq_epi16(b: pb128))._mm_blendv_epi8(
|
||||
b: a128,
|
||||
mask: smallest128._mm_cmpeq_epi16(b: pa128))
|
||||
// The predictor, p128, is whichever of a128, b128 or c128 such that
|
||||
// pa128, pb128 or pc128 matches this smallest absolute value. Ties are
|
||||
// broken in favor of a128 then b128 then c128.
|
||||
//
|
||||
// The a._mm_blendv_epi8(b, mask) method picks b when mask is true.
|
||||
p128 = c128._mm_blendv_epi8(
|
||||
b: b128,
|
||||
mask: smallest128._mm_cmpeq_epi16(b: pb128))._mm_blendv_epi8(
|
||||
b: a128,
|
||||
mask: smallest128._mm_cmpeq_epi16(b: pa128))
|
||||
|
||||
// Add the predictor to the residual and, for the next iteration, set
|
||||
// its previous pixels, a128 and c128, to x128 and b128.
|
||||
//
|
||||
// Again, convert between u8 and i16 by unpacking and re-packing.
|
||||
x128 = util.make_m128i_single_u32(a: curr.peek_u32le())
|
||||
x128 = x128._mm_unpacklo_epi8(b: z128)
|
||||
x128 = x128._mm_add_epi8(b: p128)
|
||||
a128 = x128
|
||||
c128 = b128
|
||||
x128 = x128._mm_packus_epi16(b: x128)
|
||||
curr.poke_u32le!(a: x128.truncate_u32())
|
||||
}
|
||||
// Add the predictor to the residual and, for the next iteration, set
|
||||
// its previous pixels, a128 and c128, to x128 and b128.
|
||||
//
|
||||
// Again, convert between u8 and i16 by unpacking and re-packing.
|
||||
x128 = util.make_m128i_single_u32(a: curr.peek_u32le())
|
||||
x128 = x128._mm_unpacklo_epi8(b: z128)
|
||||
x128 = x128._mm_add_epi8(b: p128)
|
||||
a128 = x128
|
||||
c128 = b128
|
||||
x128 = x128._mm_packus_epi16(b: x128)
|
||||
curr.poke_u32le!(a: x128.truncate_u32())
|
||||
}
|
||||
}
|
||||
+1463
-1463
@@ -16,1708 +16,1708 @@ use "std/crc32"
|
||||
use "std/zlib"
|
||||
|
||||
pub struct decoder? implements base.image_decoder(
|
||||
// The 0x00FF_FFFF limit is arbitrary (the PNG spec says 0x7FFF_FFFF) but
|
||||
// it means that (width * height * bytes_per_pixel) doesn't overflow a u64.
|
||||
width : base.u32[..= 0x00FF_FFFF],
|
||||
height : base.u32[..= 0x00FF_FFFF],
|
||||
// The 0x00FF_FFFF limit is arbitrary (the PNG spec says 0x7FFF_FFFF) but
|
||||
// it means that (width * height * bytes_per_pixel) doesn't overflow a u64.
|
||||
width : base.u32[..= 0x00FF_FFFF],
|
||||
height : base.u32[..= 0x00FF_FFFF],
|
||||
|
||||
// pass_bytes_per_row doesn't include the 1 byte for the per-row filter.
|
||||
pass_bytes_per_row : base.u64[..= 0x07FF_FFF8],
|
||||
// pass_bytes_per_row doesn't include the 1 byte for the per-row filter.
|
||||
pass_bytes_per_row : base.u64[..= 0x07FF_FFF8],
|
||||
|
||||
workbuf_wi : base.u64,
|
||||
workbuf_hist_pos_base : base.u64,
|
||||
workbuf_wi : base.u64,
|
||||
workbuf_hist_pos_base : base.u64,
|
||||
|
||||
// The inclusive upper bound, DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE, is
|
||||
// derived from the width and height upper bounds at up to 8 bytes per
|
||||
// pixel. It equals (((8 * M) + 1) * M), where M is 0x00FF_FFFF.
|
||||
overall_workbuf_length : base.u64[..= 0x0007_FFFF_F100_0007],
|
||||
pass_workbuf_length : base.u64[..= 0x0007_FFFF_F100_0007],
|
||||
// The inclusive upper bound, DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE, is
|
||||
// derived from the width and height upper bounds at up to 8 bytes per
|
||||
// pixel. It equals (((8 * M) + 1) * M), where M is 0x00FF_FFFF.
|
||||
overall_workbuf_length : base.u64[..= 0x0007_FFFF_F100_0007],
|
||||
pass_workbuf_length : base.u64[..= 0x0007_FFFF_F100_0007],
|
||||
|
||||
// The call sequence state machine is discussed in
|
||||
// (/doc/std/image-decoders-call-sequence.md).
|
||||
call_sequence : base.u8,
|
||||
// The call sequence state machine is discussed in
|
||||
// (/doc/std/image-decoders-call-sequence.md).
|
||||
call_sequence : base.u8,
|
||||
|
||||
report_metadata_chrm : base.bool,
|
||||
report_metadata_exif : base.bool,
|
||||
report_metadata_gama : base.bool,
|
||||
report_metadata_iccp : base.bool,
|
||||
report_metadata_kvp : base.bool,
|
||||
report_metadata_srgb : base.bool,
|
||||
report_metadata_chrm : base.bool,
|
||||
report_metadata_exif : base.bool,
|
||||
report_metadata_gama : base.bool,
|
||||
report_metadata_iccp : base.bool,
|
||||
report_metadata_kvp : base.bool,
|
||||
report_metadata_srgb : base.bool,
|
||||
|
||||
ignore_checksum : base.bool,
|
||||
ignore_checksum : base.bool,
|
||||
|
||||
depth : base.u8[..= 16],
|
||||
color_type : base.u8[..= 6],
|
||||
filter_distance : base.u8[..= 8],
|
||||
interlace_pass : base.u8[..= 7],
|
||||
depth : base.u8[..= 16],
|
||||
color_type : base.u8[..= 6],
|
||||
filter_distance : base.u8[..= 8],
|
||||
interlace_pass : base.u8[..= 7],
|
||||
|
||||
seen_actl : base.bool,
|
||||
seen_chrm : base.bool,
|
||||
seen_fctl : base.bool,
|
||||
seen_exif : base.bool,
|
||||
seen_gama : base.bool,
|
||||
seen_iccp : base.bool,
|
||||
seen_idat : base.bool,
|
||||
seen_ihdr : base.bool,
|
||||
seen_plte : base.bool,
|
||||
seen_srgb : base.bool,
|
||||
seen_trns : base.bool,
|
||||
seen_actl : base.bool,
|
||||
seen_chrm : base.bool,
|
||||
seen_fctl : base.bool,
|
||||
seen_exif : base.bool,
|
||||
seen_gama : base.bool,
|
||||
seen_iccp : base.bool,
|
||||
seen_idat : base.bool,
|
||||
seen_ihdr : base.bool,
|
||||
seen_plte : base.bool,
|
||||
seen_srgb : base.bool,
|
||||
seen_trns : base.bool,
|
||||
|
||||
metadata_is_zlib_compressed : base.bool,
|
||||
metadata_is_zlib_compressed : base.bool,
|
||||
|
||||
zlib_is_dirty : base.bool,
|
||||
zlib_is_dirty : base.bool,
|
||||
|
||||
chunk_type : base.u32,
|
||||
chunk_type_array : array[4] base.u8,
|
||||
chunk_length : base.u32,
|
||||
chunk_type : base.u32,
|
||||
chunk_type_array : array[4] base.u8,
|
||||
chunk_length : base.u32,
|
||||
|
||||
// remap_transparency, if non-zero, is the 32-bit or 64-bit (depending on
|
||||
// this.depth) argb_nonpremul color that is nominally opaque but remapped
|
||||
// to transparent black.
|
||||
//
|
||||
// "Remapped" is an unofficial term. This is where the IHDR color type is 0
|
||||
// (Y) or 2 (RGB) but there's also a tRNS chunk.
|
||||
//
|
||||
// PNG transparency that isn't "remapped" is color type 3 (indexed) with
|
||||
// tRNS, color type 4 (YA) or color type 6 (RGBA).
|
||||
remap_transparency : base.u64,
|
||||
// remap_transparency, if non-zero, is the 32-bit or 64-bit (depending on
|
||||
// this.depth) argb_nonpremul color that is nominally opaque but remapped
|
||||
// to transparent black.
|
||||
//
|
||||
// "Remapped" is an unofficial term. This is where the IHDR color type is 0
|
||||
// (Y) or 2 (RGB) but there's also a tRNS chunk.
|
||||
//
|
||||
// PNG transparency that isn't "remapped" is color type 3 (indexed) with
|
||||
// tRNS, color type 4 (YA) or color type 6 (RGBA).
|
||||
remap_transparency : base.u64,
|
||||
|
||||
dst_pixfmt : base.u32,
|
||||
src_pixfmt : base.u32,
|
||||
dst_pixfmt : base.u32,
|
||||
src_pixfmt : base.u32,
|
||||
|
||||
num_animation_frames_value : base.u32,
|
||||
num_animation_loops_value : base.u32,
|
||||
num_decoded_frame_configs_value : base.u32,
|
||||
num_decoded_frames_value : base.u32,
|
||||
num_animation_frames_value : base.u32,
|
||||
num_animation_loops_value : base.u32,
|
||||
num_decoded_frame_configs_value : base.u32,
|
||||
num_decoded_frames_value : base.u32,
|
||||
|
||||
// The frame_etc fields correspond to the base.frame_config argument passed
|
||||
// to decode_frame_config. For animated APNG, these are explicitly given in
|
||||
// the file's fcTL chunks. For still PNG, these are implicitly given in the
|
||||
// file's IHDR chunk.
|
||||
//
|
||||
// Either way, decode_image_config has to read all the way to the start of
|
||||
// the first IDAT / fdAT chunk (e.g. to see if there's a PLTE chunk). While
|
||||
// later frame's restart io_positions are located just before fcTL chunks,
|
||||
// the first frame's restart io_position is located just before the IDAT /
|
||||
// fdAT chunk, so we also have to cache the first frame's configuration as
|
||||
// we cannot re-read the first fcTL. Hence, there are first_etc fields for
|
||||
// every frame_etc field.
|
||||
//
|
||||
// For the etc_duration fields, there are 705_600000 flicks per second and
|
||||
// the maximum frame duration is 65535 seconds.
|
||||
//
|
||||
// The fields are ordered to minimize alignment wastage.
|
||||
frame_rect_x0 : base.u32[..= 0x00FF_FFFF],
|
||||
frame_rect_y0 : base.u32[..= 0x00FF_FFFF],
|
||||
frame_rect_x1 : base.u32[..= 0x00FF_FFFF],
|
||||
frame_rect_y1 : base.u32[..= 0x00FF_FFFF],
|
||||
first_rect_x0 : base.u32[..= 0x00FF_FFFF],
|
||||
first_rect_y0 : base.u32[..= 0x00FF_FFFF],
|
||||
first_rect_x1 : base.u32[..= 0x00FF_FFFF],
|
||||
first_rect_y1 : base.u32[..= 0x00FF_FFFF],
|
||||
frame_config_io_position : base.u64,
|
||||
first_config_io_position : base.u64,
|
||||
frame_duration : base.u64[..= 0x2A0E_6FF1_6600],
|
||||
first_duration : base.u64[..= 0x2A0E_6FF1_6600],
|
||||
frame_disposal : base.u8,
|
||||
first_disposal : base.u8,
|
||||
frame_overwrite_instead_of_blend : base.bool,
|
||||
first_overwrite_instead_of_blend : base.bool,
|
||||
// The frame_etc fields correspond to the base.frame_config argument passed
|
||||
// to decode_frame_config. For animated APNG, these are explicitly given in
|
||||
// the file's fcTL chunks. For still PNG, these are implicitly given in the
|
||||
// file's IHDR chunk.
|
||||
//
|
||||
// Either way, decode_image_config has to read all the way to the start of
|
||||
// the first IDAT / fdAT chunk (e.g. to see if there's a PLTE chunk). While
|
||||
// later frame's restart io_positions are located just before fcTL chunks,
|
||||
// the first frame's restart io_position is located just before the IDAT /
|
||||
// fdAT chunk, so we also have to cache the first frame's configuration as
|
||||
// we cannot re-read the first fcTL. Hence, there are first_etc fields for
|
||||
// every frame_etc field.
|
||||
//
|
||||
// For the etc_duration fields, there are 705_600000 flicks per second and
|
||||
// the maximum frame duration is 65535 seconds.
|
||||
//
|
||||
// The fields are ordered to minimize alignment wastage.
|
||||
frame_rect_x0 : base.u32[..= 0x00FF_FFFF],
|
||||
frame_rect_y0 : base.u32[..= 0x00FF_FFFF],
|
||||
frame_rect_x1 : base.u32[..= 0x00FF_FFFF],
|
||||
frame_rect_y1 : base.u32[..= 0x00FF_FFFF],
|
||||
first_rect_x0 : base.u32[..= 0x00FF_FFFF],
|
||||
first_rect_y0 : base.u32[..= 0x00FF_FFFF],
|
||||
first_rect_x1 : base.u32[..= 0x00FF_FFFF],
|
||||
first_rect_y1 : base.u32[..= 0x00FF_FFFF],
|
||||
frame_config_io_position : base.u64,
|
||||
first_config_io_position : base.u64,
|
||||
frame_duration : base.u64[..= 0x2A0E_6FF1_6600],
|
||||
first_duration : base.u64[..= 0x2A0E_6FF1_6600],
|
||||
frame_disposal : base.u8,
|
||||
first_disposal : base.u8,
|
||||
frame_overwrite_instead_of_blend : base.bool,
|
||||
first_overwrite_instead_of_blend : base.bool,
|
||||
|
||||
next_animation_seq_num : base.u32,
|
||||
next_animation_seq_num : base.u32,
|
||||
|
||||
metadata_flavor : base.u32,
|
||||
metadata_fourcc : base.u32,
|
||||
metadata_x : base.u64,
|
||||
metadata_y : base.u64,
|
||||
metadata_z : base.u64,
|
||||
metadata_flavor : base.u32,
|
||||
metadata_fourcc : base.u32,
|
||||
metadata_x : base.u64,
|
||||
metadata_y : base.u64,
|
||||
metadata_z : base.u64,
|
||||
|
||||
// ztxt_ri and ztxt_wi are read and write indexes into the dst_palette
|
||||
// buffer, re-purposed as a zlib uncompression buffer for zTXt chunks. The
|
||||
// upper bound, 1024, is the same as the dst_palette length.
|
||||
ztxt_ri : base.u32[..= 1024],
|
||||
ztxt_wi : base.u32[..= 1024],
|
||||
// ztxt_hist_pos is the history position: how many uncompressed bytes have
|
||||
// been generated.
|
||||
ztxt_hist_pos : base.u64,
|
||||
// ztxt_ri and ztxt_wi are read and write indexes into the dst_palette
|
||||
// buffer, re-purposed as a zlib uncompression buffer for zTXt chunks. The
|
||||
// upper bound, 1024, is the same as the dst_palette length.
|
||||
ztxt_ri : base.u32[..= 1024],
|
||||
ztxt_wi : base.u32[..= 1024],
|
||||
// ztxt_hist_pos is the history position: how many uncompressed bytes have
|
||||
// been generated.
|
||||
ztxt_hist_pos : base.u64,
|
||||
|
||||
swizzler : base.pixel_swizzler,
|
||||
util : base.utility,
|
||||
swizzler : base.pixel_swizzler,
|
||||
util : base.utility,
|
||||
)(
|
||||
crc32 : crc32.ieee_hasher,
|
||||
zlib : zlib.decoder,
|
||||
crc32 : crc32.ieee_hasher,
|
||||
zlib : zlib.decoder,
|
||||
|
||||
// dst_palette and src_palette are used by the swizzler, during
|
||||
// decode_frame. src_palette is initialized by processing the PLTE chunk.
|
||||
// dst_palette is also re-purposed as a zlib uncompression buffer for zTXt
|
||||
// chunks, during decode_image_config.
|
||||
dst_palette : array[4 * 256] base.u8,
|
||||
src_palette : array[4 * 256] base.u8,
|
||||
// dst_palette and src_palette are used by the swizzler, during
|
||||
// decode_frame. src_palette is initialized by processing the PLTE chunk.
|
||||
// dst_palette is also re-purposed as a zlib uncompression buffer for zTXt
|
||||
// chunks, during decode_image_config.
|
||||
dst_palette : array[4 * 256] base.u8,
|
||||
src_palette : array[4 * 256] base.u8,
|
||||
)
|
||||
|
||||
pub func decoder.set_quirk_enabled!(quirk: base.u32, enabled: base.bool) {
|
||||
if args.quirk == base.QUIRK_IGNORE_CHECKSUM {
|
||||
this.ignore_checksum = args.enabled
|
||||
this.zlib.set_quirk_enabled!(quirk: args.quirk, enabled: args.enabled)
|
||||
}
|
||||
if args.quirk == base.QUIRK_IGNORE_CHECKSUM {
|
||||
this.ignore_checksum = args.enabled
|
||||
this.zlib.set_quirk_enabled!(quirk: args.quirk, enabled: args.enabled)
|
||||
}
|
||||
}
|
||||
|
||||
pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_reader) {
|
||||
var status : base.status
|
||||
var status : base.status
|
||||
|
||||
while true {
|
||||
status =? this.do_decode_image_config?(dst: args.dst, src: args.src)
|
||||
if (status == base."$short read") and args.src.is_closed() {
|
||||
return "#truncated input"
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
while true {
|
||||
status =? this.do_decode_image_config?(dst: args.dst, src: args.src)
|
||||
if (status == base."$short read") and args.src.is_closed() {
|
||||
return "#truncated input"
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
}
|
||||
|
||||
pri func decoder.do_decode_image_config?(dst: nptr base.image_config, src: base.io_reader) {
|
||||
var magic : base.u64
|
||||
var mark : base.u64
|
||||
var checksum_have : base.u32
|
||||
var checksum_want : base.u32
|
||||
var status : base.status
|
||||
var magic : base.u64
|
||||
var mark : base.u64
|
||||
var checksum_have : base.u32
|
||||
var checksum_want : base.u32
|
||||
var status : base.status
|
||||
|
||||
if this.call_sequence <> 0x00 {
|
||||
return base."#bad call sequence"
|
||||
} else if not this.seen_ihdr {
|
||||
magic = args.src.read_u64le?()
|
||||
if magic <> '\x89PNG\x0D\x0A\x1A\x0A'le {
|
||||
return "#bad header"
|
||||
}
|
||||
magic = args.src.read_u64le?()
|
||||
if magic <> '\x00\x00\x00\x0DIHDR'le {
|
||||
if magic == '\x00\x00\x00\x04CgBI'le {
|
||||
// TODO: add support for Apple's unofficial CgBI extension to
|
||||
// PNG, once there exists good documentation for it. See
|
||||
// https://github.com/w3c/PNG-spec/issues/45
|
||||
return "#unsupported CgBI extension"
|
||||
}
|
||||
return "#bad header"
|
||||
}
|
||||
this.chunk_type_array[0] = 'I'
|
||||
this.chunk_type_array[1] = 'H'
|
||||
this.chunk_type_array[2] = 'D'
|
||||
this.chunk_type_array[3] = 'R'
|
||||
this.crc32.reset!()
|
||||
this.crc32.update_u32!(x: this.chunk_type_array[..])
|
||||
if this.call_sequence <> 0x00 {
|
||||
return base."#bad call sequence"
|
||||
} else if not this.seen_ihdr {
|
||||
magic = args.src.read_u64le?()
|
||||
if magic <> '\x89PNG\x0D\x0A\x1A\x0A'le {
|
||||
return "#bad header"
|
||||
}
|
||||
magic = args.src.read_u64le?()
|
||||
if magic <> '\x00\x00\x00\x0DIHDR'le {
|
||||
if magic == '\x00\x00\x00\x04CgBI'le {
|
||||
// TODO: add support for Apple's unofficial CgBI extension to
|
||||
// PNG, once there exists good documentation for it. See
|
||||
// https://github.com/w3c/PNG-spec/issues/45
|
||||
return "#unsupported CgBI extension"
|
||||
}
|
||||
return "#bad header"
|
||||
}
|
||||
this.chunk_type_array[0] = 'I'
|
||||
this.chunk_type_array[1] = 'H'
|
||||
this.chunk_type_array[2] = 'D'
|
||||
this.chunk_type_array[3] = 'R'
|
||||
this.crc32.reset!()
|
||||
this.crc32.update_u32!(x: this.chunk_type_array[..])
|
||||
|
||||
while true {
|
||||
mark = args.src.mark()
|
||||
status =? this.decode_ihdr?(src: args.src)
|
||||
if not this.ignore_checksum {
|
||||
checksum_have = this.crc32.update_u32!(x: args.src.since(mark: mark))
|
||||
}
|
||||
if status.is_ok() {
|
||||
break
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
while true {
|
||||
mark = args.src.mark()
|
||||
status =? this.decode_ihdr?(src: args.src)
|
||||
if not this.ignore_checksum {
|
||||
checksum_have = this.crc32.update_u32!(x: args.src.since(mark: mark))
|
||||
}
|
||||
if status.is_ok() {
|
||||
break
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
|
||||
// Verify CRC-32 checksum.
|
||||
checksum_want = args.src.read_u32be?()
|
||||
if (not this.ignore_checksum) and (checksum_have <> checksum_want) {
|
||||
return "#bad checksum"
|
||||
}
|
||||
this.seen_ihdr = true
|
||||
} else if this.metadata_fourcc <> 0 {
|
||||
this.call_sequence = 0x10
|
||||
return base."@metadata reported"
|
||||
}
|
||||
// Verify CRC-32 checksum.
|
||||
checksum_want = args.src.read_u32be?()
|
||||
if (not this.ignore_checksum) and (checksum_have <> checksum_want) {
|
||||
return "#bad checksum"
|
||||
}
|
||||
this.seen_ihdr = true
|
||||
} else if this.metadata_fourcc <> 0 {
|
||||
this.call_sequence = 0x10
|
||||
return base."@metadata reported"
|
||||
}
|
||||
|
||||
// Read up until an IDAT or fdAT chunk.
|
||||
//
|
||||
// By default, libpng "warns and discards" when seeing ancillary chunk
|
||||
// checksum failures (as opposed to critical chunk checksum failures) but
|
||||
// it still continues to decode the image. Wuffs' decoder is similar,
|
||||
// simply always ignoring ancillary chunks' CRC-32 checksums.
|
||||
//
|
||||
// https://github.com/glennrp/libpng/blob/dbe3e0c43e549a1602286144d94b0666549b18e6/png.h#L1436
|
||||
//
|
||||
// We've already seen the IHDR chunk. We're not expecting an IEND chunk. An
|
||||
// IDAT chunk breaks the loop. The only other possible critical chunk is a
|
||||
// PLTE chunk. We verify PLTE checksums here but ignore other checksums.
|
||||
while true {
|
||||
if args.src.length() < 8 {
|
||||
yield? base."$short read"
|
||||
continue
|
||||
}
|
||||
// Read up until an IDAT or fdAT chunk.
|
||||
//
|
||||
// By default, libpng "warns and discards" when seeing ancillary chunk
|
||||
// checksum failures (as opposed to critical chunk checksum failures) but
|
||||
// it still continues to decode the image. Wuffs' decoder is similar,
|
||||
// simply always ignoring ancillary chunks' CRC-32 checksums.
|
||||
//
|
||||
// https://github.com/glennrp/libpng/blob/dbe3e0c43e549a1602286144d94b0666549b18e6/png.h#L1436
|
||||
//
|
||||
// We've already seen the IHDR chunk. We're not expecting an IEND chunk. An
|
||||
// IDAT chunk breaks the loop. The only other possible critical chunk is a
|
||||
// PLTE chunk. We verify PLTE checksums here but ignore other checksums.
|
||||
while true {
|
||||
if args.src.length() < 8 {
|
||||
yield? base."$short read"
|
||||
continue
|
||||
}
|
||||
|
||||
this.chunk_length = args.src.peek_u32be()
|
||||
this.chunk_type = (args.src.peek_u64le() >> 32) as base.u32
|
||||
if this.chunk_type == 'IDAT'le {
|
||||
if (not this.seen_actl) or this.seen_fctl {
|
||||
break
|
||||
}
|
||||
this.seen_idat = true
|
||||
} else if this.chunk_type == 'fdAT'le {
|
||||
if this.seen_idat and this.seen_fctl {
|
||||
break
|
||||
}
|
||||
return "#bad chunk"
|
||||
}
|
||||
args.src.skip_u32_fast!(actual: 8, worst_case: 8)
|
||||
this.chunk_length = args.src.peek_u32be()
|
||||
this.chunk_type = (args.src.peek_u64le() >> 32) as base.u32
|
||||
if this.chunk_type == 'IDAT'le {
|
||||
if (not this.seen_actl) or this.seen_fctl {
|
||||
break
|
||||
}
|
||||
this.seen_idat = true
|
||||
} else if this.chunk_type == 'fdAT'le {
|
||||
if this.seen_idat and this.seen_fctl {
|
||||
break
|
||||
}
|
||||
return "#bad chunk"
|
||||
}
|
||||
args.src.skip_u32_fast!(actual: 8, worst_case: 8)
|
||||
|
||||
if (not this.ignore_checksum) and ((this.chunk_type & ANCILLARY_BIT) == 0) {
|
||||
this.chunk_type_array[0] = ((this.chunk_type >> 0) & 0xFF) as base.u8
|
||||
this.chunk_type_array[1] = ((this.chunk_type >> 8) & 0xFF) as base.u8
|
||||
this.chunk_type_array[2] = ((this.chunk_type >> 16) & 0xFF) as base.u8
|
||||
this.chunk_type_array[3] = ((this.chunk_type >> 24) & 0xFF) as base.u8
|
||||
this.crc32.reset!()
|
||||
this.crc32.update_u32!(x: this.chunk_type_array[..])
|
||||
}
|
||||
if (not this.ignore_checksum) and ((this.chunk_type & ANCILLARY_BIT) == 0) {
|
||||
this.chunk_type_array[0] = ((this.chunk_type >> 0) & 0xFF) as base.u8
|
||||
this.chunk_type_array[1] = ((this.chunk_type >> 8) & 0xFF) as base.u8
|
||||
this.chunk_type_array[2] = ((this.chunk_type >> 16) & 0xFF) as base.u8
|
||||
this.chunk_type_array[3] = ((this.chunk_type >> 24) & 0xFF) as base.u8
|
||||
this.crc32.reset!()
|
||||
this.crc32.update_u32!(x: this.chunk_type_array[..])
|
||||
}
|
||||
|
||||
while true {
|
||||
mark = args.src.mark()
|
||||
status =? this.decode_other_chunk?(src: args.src, framy: false)
|
||||
if (not this.ignore_checksum) and ((this.chunk_type & ANCILLARY_BIT) == 0) {
|
||||
checksum_have = this.crc32.update_u32!(x: args.src.since(mark: mark))
|
||||
}
|
||||
if status.is_ok() {
|
||||
break
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
while true {
|
||||
mark = args.src.mark()
|
||||
status =? this.decode_other_chunk?(src: args.src, framy: false)
|
||||
if (not this.ignore_checksum) and ((this.chunk_type & ANCILLARY_BIT) == 0) {
|
||||
checksum_have = this.crc32.update_u32!(x: args.src.since(mark: mark))
|
||||
}
|
||||
if status.is_ok() {
|
||||
break
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
|
||||
// If we have metadata, delay reading (skipping) the ancillary chunk's
|
||||
// CRC-32 checksum until the end of tell_me_more.
|
||||
if this.metadata_fourcc <> 0 {
|
||||
this.call_sequence = 0x10
|
||||
return base."@metadata reported"
|
||||
}
|
||||
// If we have metadata, delay reading (skipping) the ancillary chunk's
|
||||
// CRC-32 checksum until the end of tell_me_more.
|
||||
if this.metadata_fourcc <> 0 {
|
||||
this.call_sequence = 0x10
|
||||
return base."@metadata reported"
|
||||
}
|
||||
|
||||
checksum_want = args.src.read_u32be?()
|
||||
if (not this.ignore_checksum) and ((this.chunk_type & ANCILLARY_BIT) == 0) and
|
||||
(checksum_have <> checksum_want) {
|
||||
return "#bad checksum"
|
||||
}
|
||||
} endwhile
|
||||
checksum_want = args.src.read_u32be?()
|
||||
if (not this.ignore_checksum) and ((this.chunk_type & ANCILLARY_BIT) == 0) and
|
||||
(checksum_have <> checksum_want) {
|
||||
return "#bad checksum"
|
||||
}
|
||||
} endwhile
|
||||
|
||||
if (this.color_type == 3) and (not this.seen_plte) {
|
||||
return "#missing palette"
|
||||
}
|
||||
if (this.color_type == 3) and (not this.seen_plte) {
|
||||
return "#missing palette"
|
||||
}
|
||||
|
||||
this.frame_config_io_position = args.src.position()
|
||||
this.first_config_io_position = this.frame_config_io_position
|
||||
this.frame_config_io_position = args.src.position()
|
||||
this.first_config_io_position = this.frame_config_io_position
|
||||
|
||||
if args.dst <> nullptr {
|
||||
args.dst.set!(
|
||||
pixfmt: this.dst_pixfmt,
|
||||
pixsub: 0,
|
||||
width: this.width,
|
||||
height: this.height,
|
||||
first_frame_io_position: this.first_config_io_position,
|
||||
first_frame_is_opaque: (this.color_type <= 3) and (not this.seen_trns))
|
||||
}
|
||||
if args.dst <> nullptr {
|
||||
args.dst.set!(
|
||||
pixfmt: this.dst_pixfmt,
|
||||
pixsub: 0,
|
||||
width: this.width,
|
||||
height: this.height,
|
||||
first_frame_io_position: this.first_config_io_position,
|
||||
first_frame_is_opaque: (this.color_type <= 3) and (not this.seen_trns))
|
||||
}
|
||||
|
||||
// For still (non-animated) PNGs, the first and only frame's configuration
|
||||
// is implied by the IHDR chunk instead of an explicit fcTL chunk.
|
||||
if not this.seen_actl {
|
||||
this.num_animation_frames_value = 1
|
||||
this.first_rect_x0 = 0
|
||||
this.first_rect_y0 = 0
|
||||
this.first_rect_x1 = this.width
|
||||
this.first_rect_y1 = this.height
|
||||
this.first_duration = 0
|
||||
this.first_disposal = base.ANIMATION_DISPOSAL__NONE
|
||||
this.first_overwrite_instead_of_blend = false
|
||||
}
|
||||
// For still (non-animated) PNGs, the first and only frame's configuration
|
||||
// is implied by the IHDR chunk instead of an explicit fcTL chunk.
|
||||
if not this.seen_actl {
|
||||
this.num_animation_frames_value = 1
|
||||
this.first_rect_x0 = 0
|
||||
this.first_rect_y0 = 0
|
||||
this.first_rect_x1 = this.width
|
||||
this.first_rect_y1 = this.height
|
||||
this.first_duration = 0
|
||||
this.first_disposal = base.ANIMATION_DISPOSAL__NONE
|
||||
this.first_overwrite_instead_of_blend = false
|
||||
}
|
||||
|
||||
this.call_sequence = 0x20
|
||||
this.call_sequence = 0x20
|
||||
}
|
||||
|
||||
pri func decoder.decode_ihdr?(src: base.io_reader) {
|
||||
var a32 : base.u32
|
||||
var a8 : base.u8
|
||||
var a32 : base.u32
|
||||
var a8 : base.u8
|
||||
|
||||
a32 = args.src.read_u32be?()
|
||||
if (a32 == 0) or (a32 >= 0x8000_0000) {
|
||||
return "#bad header"
|
||||
} else if a32 >= 0x0100_0000 {
|
||||
return "#unsupported PNG file"
|
||||
}
|
||||
this.width = a32
|
||||
a32 = args.src.read_u32be?()
|
||||
if (a32 == 0) or (a32 >= 0x8000_0000) {
|
||||
return "#bad header"
|
||||
} else if a32 >= 0x0100_0000 {
|
||||
return "#unsupported PNG file"
|
||||
}
|
||||
this.width = a32
|
||||
|
||||
a32 = args.src.read_u32be?()
|
||||
if (a32 == 0) or (a32 >= 0x8000_0000) {
|
||||
return "#bad header"
|
||||
} else if a32 >= 0x0100_0000 {
|
||||
return "#unsupported PNG file"
|
||||
}
|
||||
this.height = a32
|
||||
a32 = args.src.read_u32be?()
|
||||
if (a32 == 0) or (a32 >= 0x8000_0000) {
|
||||
return "#bad header"
|
||||
} else if a32 >= 0x0100_0000 {
|
||||
return "#unsupported PNG file"
|
||||
}
|
||||
this.height = a32
|
||||
|
||||
// Depth.
|
||||
a8 = args.src.read_u8?()
|
||||
if a8 > 16 {
|
||||
return "#bad header"
|
||||
}
|
||||
this.depth = a8
|
||||
// Depth.
|
||||
a8 = args.src.read_u8?()
|
||||
if a8 > 16 {
|
||||
return "#bad header"
|
||||
}
|
||||
this.depth = a8
|
||||
|
||||
// Color.
|
||||
a8 = args.src.read_u8?()
|
||||
if (a8 == 1) or (a8 == 5) or (a8 > 6) {
|
||||
return "#bad header"
|
||||
}
|
||||
this.color_type = a8
|
||||
// Color.
|
||||
a8 = args.src.read_u8?()
|
||||
if (a8 == 1) or (a8 == 5) or (a8 > 6) {
|
||||
return "#bad header"
|
||||
}
|
||||
this.color_type = a8
|
||||
|
||||
// Compression.
|
||||
a8 = args.src.read_u8?()
|
||||
if a8 <> 0 {
|
||||
return "#unsupported PNG compression method"
|
||||
}
|
||||
// Compression.
|
||||
a8 = args.src.read_u8?()
|
||||
if a8 <> 0 {
|
||||
return "#unsupported PNG compression method"
|
||||
}
|
||||
|
||||
// Filter.
|
||||
a8 = args.src.read_u8?()
|
||||
if a8 <> 0 {
|
||||
return "#bad header"
|
||||
}
|
||||
// Filter.
|
||||
a8 = args.src.read_u8?()
|
||||
if a8 <> 0 {
|
||||
return "#bad header"
|
||||
}
|
||||
|
||||
// Interlace.
|
||||
a8 = args.src.read_u8?()
|
||||
if a8 == 0 {
|
||||
this.interlace_pass = 0
|
||||
} else if a8 == 1 {
|
||||
this.interlace_pass = 1
|
||||
choose filter_and_swizzle = [filter_and_swizzle_tricky]
|
||||
} else {
|
||||
return "#bad header"
|
||||
}
|
||||
// Interlace.
|
||||
a8 = args.src.read_u8?()
|
||||
if a8 == 0 {
|
||||
this.interlace_pass = 0
|
||||
} else if a8 == 1 {
|
||||
this.interlace_pass = 1
|
||||
choose filter_and_swizzle = [filter_and_swizzle_tricky]
|
||||
} else {
|
||||
return "#bad header"
|
||||
}
|
||||
|
||||
// Derived fields.
|
||||
this.filter_distance = 0
|
||||
this.assign_filter_distance!()
|
||||
if this.filter_distance == 0 {
|
||||
return "#bad header"
|
||||
}
|
||||
this.overall_workbuf_length = (this.height as base.u64) *
|
||||
(1 + this.calculate_bytes_per_row(width: this.width))
|
||||
this.choose_filter_implementations!()
|
||||
// Derived fields.
|
||||
this.filter_distance = 0
|
||||
this.assign_filter_distance!()
|
||||
if this.filter_distance == 0 {
|
||||
return "#bad header"
|
||||
}
|
||||
this.overall_workbuf_length = (this.height as base.u64) *
|
||||
(1 + this.calculate_bytes_per_row(width: this.width))
|
||||
this.choose_filter_implementations!()
|
||||
}
|
||||
|
||||
pri func decoder.assign_filter_distance!() {
|
||||
if this.depth < 8 {
|
||||
if (this.depth <> 1) and (this.depth <> 2) and (this.depth <> 4) {
|
||||
return nothing
|
||||
} else if this.color_type == 0 {
|
||||
this.dst_pixfmt = base.PIXEL_FORMAT__Y
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__Y
|
||||
} else if this.color_type == 3 {
|
||||
this.dst_pixfmt = base.PIXEL_FORMAT__INDEXED__BGRA_BINARY
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__INDEXED__BGRA_BINARY
|
||||
} else {
|
||||
return nothing
|
||||
}
|
||||
if this.depth < 8 {
|
||||
if (this.depth <> 1) and (this.depth <> 2) and (this.depth <> 4) {
|
||||
return nothing
|
||||
} else if this.color_type == 0 {
|
||||
this.dst_pixfmt = base.PIXEL_FORMAT__Y
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__Y
|
||||
} else if this.color_type == 3 {
|
||||
this.dst_pixfmt = base.PIXEL_FORMAT__INDEXED__BGRA_BINARY
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__INDEXED__BGRA_BINARY
|
||||
} else {
|
||||
return nothing
|
||||
}
|
||||
|
||||
this.filter_distance = 1
|
||||
choose filter_and_swizzle = [filter_and_swizzle_tricky]
|
||||
this.filter_distance = 1
|
||||
choose filter_and_swizzle = [filter_and_swizzle_tricky]
|
||||
|
||||
} else if this.color_type == 0 {
|
||||
if this.depth == 8 {
|
||||
this.dst_pixfmt = base.PIXEL_FORMAT__Y
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__Y
|
||||
this.filter_distance = 1
|
||||
} else if this.depth == 16 {
|
||||
if this.interlace_pass == 0 {
|
||||
this.dst_pixfmt = base.PIXEL_FORMAT__Y_16LE
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__Y_16BE
|
||||
} else {
|
||||
// Interlaced means choosing filter_and_swizzle_tricky.
|
||||
this.dst_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE
|
||||
}
|
||||
this.filter_distance = 2
|
||||
}
|
||||
} else if this.color_type == 0 {
|
||||
if this.depth == 8 {
|
||||
this.dst_pixfmt = base.PIXEL_FORMAT__Y
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__Y
|
||||
this.filter_distance = 1
|
||||
} else if this.depth == 16 {
|
||||
if this.interlace_pass == 0 {
|
||||
this.dst_pixfmt = base.PIXEL_FORMAT__Y_16LE
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__Y_16BE
|
||||
} else {
|
||||
// Interlaced means choosing filter_and_swizzle_tricky.
|
||||
this.dst_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE
|
||||
}
|
||||
this.filter_distance = 2
|
||||
}
|
||||
|
||||
} else if this.color_type == 2 {
|
||||
if this.depth == 8 {
|
||||
this.dst_pixfmt = base.PIXEL_FORMAT__BGR
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__RGB
|
||||
this.filter_distance = 3
|
||||
} else if this.depth == 16 {
|
||||
this.dst_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE
|
||||
this.filter_distance = 6
|
||||
choose filter_and_swizzle = [filter_and_swizzle_tricky]
|
||||
}
|
||||
} else if this.color_type == 2 {
|
||||
if this.depth == 8 {
|
||||
this.dst_pixfmt = base.PIXEL_FORMAT__BGR
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__RGB
|
||||
this.filter_distance = 3
|
||||
} else if this.depth == 16 {
|
||||
this.dst_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE
|
||||
this.filter_distance = 6
|
||||
choose filter_and_swizzle = [filter_and_swizzle_tricky]
|
||||
}
|
||||
|
||||
} else if this.color_type == 3 {
|
||||
if this.depth == 8 {
|
||||
this.dst_pixfmt = base.PIXEL_FORMAT__INDEXED__BGRA_BINARY
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__INDEXED__BGRA_BINARY
|
||||
this.filter_distance = 1
|
||||
}
|
||||
} else if this.color_type == 3 {
|
||||
if this.depth == 8 {
|
||||
this.dst_pixfmt = base.PIXEL_FORMAT__INDEXED__BGRA_BINARY
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__INDEXED__BGRA_BINARY
|
||||
this.filter_distance = 1
|
||||
}
|
||||
|
||||
} else if this.color_type == 4 {
|
||||
if this.depth == 8 {
|
||||
this.dst_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL
|
||||
this.filter_distance = 2
|
||||
choose filter_and_swizzle = [filter_and_swizzle_tricky]
|
||||
} else if this.depth == 16 {
|
||||
this.dst_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE
|
||||
this.filter_distance = 4
|
||||
choose filter_and_swizzle = [filter_and_swizzle_tricky]
|
||||
}
|
||||
} else if this.color_type == 4 {
|
||||
if this.depth == 8 {
|
||||
this.dst_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL
|
||||
this.filter_distance = 2
|
||||
choose filter_and_swizzle = [filter_and_swizzle_tricky]
|
||||
} else if this.depth == 16 {
|
||||
this.dst_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE
|
||||
this.filter_distance = 4
|
||||
choose filter_and_swizzle = [filter_and_swizzle_tricky]
|
||||
}
|
||||
|
||||
} else if this.color_type == 6 {
|
||||
if this.depth == 8 {
|
||||
this.dst_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__RGBA_NONPREMUL
|
||||
this.filter_distance = 4
|
||||
} else if this.depth == 16 {
|
||||
this.dst_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE
|
||||
this.filter_distance = 8
|
||||
choose filter_and_swizzle = [filter_and_swizzle_tricky]
|
||||
}
|
||||
}
|
||||
} else if this.color_type == 6 {
|
||||
if this.depth == 8 {
|
||||
this.dst_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__RGBA_NONPREMUL
|
||||
this.filter_distance = 4
|
||||
} else if this.depth == 16 {
|
||||
this.dst_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE
|
||||
this.filter_distance = 8
|
||||
choose filter_and_swizzle = [filter_and_swizzle_tricky]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pri func decoder.calculate_bytes_per_row(width: base.u32[..= 0x00FF_FFFF]) base.u64[..= 0x07FF_FFF8] {
|
||||
var bytes_per_channel : base.u64[..= 2]
|
||||
var bytes_per_channel : base.u64[..= 2]
|
||||
|
||||
if this.depth == 1 {
|
||||
return ((args.width + 7) / 8) as base.u64
|
||||
} else if this.depth == 2 {
|
||||
return ((args.width + 3) / 4) as base.u64
|
||||
} else if this.depth == 4 {
|
||||
return ((args.width + 1) / 2) as base.u64
|
||||
}
|
||||
bytes_per_channel = (this.depth >> 3) as base.u64
|
||||
return (args.width as base.u64) * bytes_per_channel *
|
||||
(NUM_CHANNELS[this.color_type] as base.u64)
|
||||
if this.depth == 1 {
|
||||
return ((args.width + 7) / 8) as base.u64
|
||||
} else if this.depth == 2 {
|
||||
return ((args.width + 3) / 4) as base.u64
|
||||
} else if this.depth == 4 {
|
||||
return ((args.width + 1) / 2) as base.u64
|
||||
}
|
||||
bytes_per_channel = (this.depth >> 3) as base.u64
|
||||
return (args.width as base.u64) * bytes_per_channel *
|
||||
(NUM_CHANNELS[this.color_type] as base.u64)
|
||||
}
|
||||
|
||||
pri func decoder.choose_filter_implementations!() {
|
||||
// Filter 0 is a no-op. Filter 2, the up filter, should already vectorize
|
||||
// easily by a good optimizing C compiler.
|
||||
if this.filter_distance == 3 {
|
||||
choose filter_1 = [filter_1_distance_3_fallback]
|
||||
choose filter_3 = [filter_3_distance_3_fallback]
|
||||
choose filter_4 = [
|
||||
filter_4_distance_3_arm_neon,
|
||||
filter_4_distance_3_x86_sse42,
|
||||
filter_4_distance_3_fallback]
|
||||
} else if this.filter_distance == 4 {
|
||||
choose filter_1 = [
|
||||
filter_1_distance_4_arm_neon,
|
||||
filter_1_distance_4_x86_sse42,
|
||||
filter_1_distance_4_fallback]
|
||||
choose filter_3 = [
|
||||
filter_3_distance_4_arm_neon,
|
||||
filter_3_distance_4_x86_sse42,
|
||||
filter_3_distance_4_fallback]
|
||||
choose filter_4 = [
|
||||
filter_4_distance_4_arm_neon,
|
||||
filter_4_distance_4_x86_sse42,
|
||||
filter_4_distance_4_fallback]
|
||||
}
|
||||
// Filter 0 is a no-op. Filter 2, the up filter, should already vectorize
|
||||
// easily by a good optimizing C compiler.
|
||||
if this.filter_distance == 3 {
|
||||
choose filter_1 = [filter_1_distance_3_fallback]
|
||||
choose filter_3 = [filter_3_distance_3_fallback]
|
||||
choose filter_4 = [
|
||||
filter_4_distance_3_arm_neon,
|
||||
filter_4_distance_3_x86_sse42,
|
||||
filter_4_distance_3_fallback]
|
||||
} else if this.filter_distance == 4 {
|
||||
choose filter_1 = [
|
||||
filter_1_distance_4_arm_neon,
|
||||
filter_1_distance_4_x86_sse42,
|
||||
filter_1_distance_4_fallback]
|
||||
choose filter_3 = [
|
||||
filter_3_distance_4_arm_neon,
|
||||
filter_3_distance_4_x86_sse42,
|
||||
filter_3_distance_4_fallback]
|
||||
choose filter_4 = [
|
||||
filter_4_distance_4_arm_neon,
|
||||
filter_4_distance_4_x86_sse42,
|
||||
filter_4_distance_4_fallback]
|
||||
}
|
||||
}
|
||||
|
||||
// framy is:
|
||||
// - false when coming from decode_image_config.
|
||||
// - true when coming from decode_frame_config.
|
||||
pri func decoder.decode_other_chunk?(src: base.io_reader, framy: base.bool) {
|
||||
if (this.chunk_type == 'PLTE'le) and (not args.framy) {
|
||||
if this.seen_plte {
|
||||
return "#bad chunk"
|
||||
} else if this.color_type == 3 {
|
||||
// Color type 3 means paletted.
|
||||
this.decode_plte?(src: args.src)
|
||||
} else if (this.color_type == 2) or (this.color_type == 6) {
|
||||
// Color types 2 and 6 means RGB and RGBA. In these cases, the PLTE
|
||||
// chunk is merely a hint, like a sPLT "suggested palette" chunk.
|
||||
// We ignore it.
|
||||
} else {
|
||||
return "#bad chunk"
|
||||
}
|
||||
this.seen_plte = true
|
||||
if (this.chunk_type == 'PLTE'le) and (not args.framy) {
|
||||
if this.seen_plte {
|
||||
return "#bad chunk"
|
||||
} else if this.color_type == 3 {
|
||||
// Color type 3 means paletted.
|
||||
this.decode_plte?(src: args.src)
|
||||
} else if (this.color_type == 2) or (this.color_type == 6) {
|
||||
// Color types 2 and 6 means RGB and RGBA. In these cases, the PLTE
|
||||
// chunk is merely a hint, like a sPLT "suggested palette" chunk.
|
||||
// We ignore it.
|
||||
} else {
|
||||
return "#bad chunk"
|
||||
}
|
||||
this.seen_plte = true
|
||||
|
||||
} else if (this.chunk_type & ANCILLARY_BIT) == 0 {
|
||||
if this.chunk_type <> 'IDAT'le {
|
||||
return "#bad chunk"
|
||||
}
|
||||
}
|
||||
} else if (this.chunk_type & ANCILLARY_BIT) == 0 {
|
||||
if this.chunk_type <> 'IDAT'le {
|
||||
return "#bad chunk"
|
||||
}
|
||||
}
|
||||
|
||||
if this.chunk_type == 'eXIf'le {
|
||||
if this.report_metadata_exif {
|
||||
if this.seen_exif {
|
||||
return "#bad chunk"
|
||||
}
|
||||
this.decode_exif?(src: args.src)
|
||||
this.seen_exif = true
|
||||
}
|
||||
if this.chunk_type == 'eXIf'le {
|
||||
if this.report_metadata_exif {
|
||||
if this.seen_exif {
|
||||
return "#bad chunk"
|
||||
}
|
||||
this.decode_exif?(src: args.src)
|
||||
this.seen_exif = true
|
||||
}
|
||||
|
||||
} else if (this.chunk_type == 'iTXt'le) or
|
||||
(this.chunk_type == 'tEXt'le) or
|
||||
(this.chunk_type == 'zTXt'le) {
|
||||
if this.report_metadata_kvp {
|
||||
this.metadata_flavor = base.MORE_INFORMATION__FLAVOR__METADATA_RAW_TRANSFORM
|
||||
this.metadata_fourcc = 'KVPK'be
|
||||
this.metadata_x = 0
|
||||
this.metadata_y = 0
|
||||
this.metadata_z = 0
|
||||
}
|
||||
} else if (this.chunk_type == 'iTXt'le) or
|
||||
(this.chunk_type == 'tEXt'le) or
|
||||
(this.chunk_type == 'zTXt'le) {
|
||||
if this.report_metadata_kvp {
|
||||
this.metadata_flavor = base.MORE_INFORMATION__FLAVOR__METADATA_RAW_TRANSFORM
|
||||
this.metadata_fourcc = 'KVPK'be
|
||||
this.metadata_x = 0
|
||||
this.metadata_y = 0
|
||||
this.metadata_z = 0
|
||||
}
|
||||
|
||||
} else if not args.framy {
|
||||
if this.chunk_type == 'acTL'le {
|
||||
if this.seen_actl {
|
||||
return "#bad chunk"
|
||||
}
|
||||
this.decode_actl?(src: args.src)
|
||||
this.seen_actl = true
|
||||
} else if not args.framy {
|
||||
if this.chunk_type == 'acTL'le {
|
||||
if this.seen_actl {
|
||||
return "#bad chunk"
|
||||
}
|
||||
this.decode_actl?(src: args.src)
|
||||
this.seen_actl = true
|
||||
|
||||
} else if this.chunk_type == 'cHRM'le {
|
||||
if this.report_metadata_chrm {
|
||||
if this.seen_chrm {
|
||||
return "#bad chunk"
|
||||
}
|
||||
this.decode_chrm?(src: args.src)
|
||||
this.seen_chrm = true
|
||||
}
|
||||
} else if this.chunk_type == 'cHRM'le {
|
||||
if this.report_metadata_chrm {
|
||||
if this.seen_chrm {
|
||||
return "#bad chunk"
|
||||
}
|
||||
this.decode_chrm?(src: args.src)
|
||||
this.seen_chrm = true
|
||||
}
|
||||
|
||||
} else if this.chunk_type == 'fcTL'le {
|
||||
if this.seen_fctl {
|
||||
return "#bad chunk"
|
||||
}
|
||||
this.decode_fctl?(src: args.src)
|
||||
this.seen_fctl = true
|
||||
} else if this.chunk_type == 'fcTL'le {
|
||||
if this.seen_fctl {
|
||||
return "#bad chunk"
|
||||
}
|
||||
this.decode_fctl?(src: args.src)
|
||||
this.seen_fctl = true
|
||||
|
||||
} else if this.chunk_type == 'gAMA'le {
|
||||
if this.report_metadata_gama {
|
||||
if this.seen_gama {
|
||||
return "#bad chunk"
|
||||
}
|
||||
this.decode_gama?(src: args.src)
|
||||
this.seen_gama = true
|
||||
}
|
||||
} else if this.chunk_type == 'gAMA'le {
|
||||
if this.report_metadata_gama {
|
||||
if this.seen_gama {
|
||||
return "#bad chunk"
|
||||
}
|
||||
this.decode_gama?(src: args.src)
|
||||
this.seen_gama = true
|
||||
}
|
||||
|
||||
} else if this.chunk_type == 'iCCP'le {
|
||||
if this.report_metadata_iccp {
|
||||
if this.seen_iccp {
|
||||
return "#bad chunk"
|
||||
}
|
||||
this.decode_iccp?(src: args.src)
|
||||
this.seen_iccp = true
|
||||
}
|
||||
} else if this.chunk_type == 'iCCP'le {
|
||||
if this.report_metadata_iccp {
|
||||
if this.seen_iccp {
|
||||
return "#bad chunk"
|
||||
}
|
||||
this.decode_iccp?(src: args.src)
|
||||
this.seen_iccp = true
|
||||
}
|
||||
|
||||
} else if this.chunk_type == 'sRGB'le {
|
||||
if this.report_metadata_srgb {
|
||||
if this.seen_srgb {
|
||||
return "#bad chunk"
|
||||
}
|
||||
this.decode_srgb?(src: args.src)
|
||||
this.seen_srgb = true
|
||||
}
|
||||
} else if this.chunk_type == 'sRGB'le {
|
||||
if this.report_metadata_srgb {
|
||||
if this.seen_srgb {
|
||||
return "#bad chunk"
|
||||
}
|
||||
this.decode_srgb?(src: args.src)
|
||||
this.seen_srgb = true
|
||||
}
|
||||
|
||||
} else if this.chunk_type == 'tRNS'le {
|
||||
if this.seen_trns or (this.color_type > 3) or
|
||||
((this.color_type == 3) and (not this.seen_plte)) {
|
||||
return "#bad chunk"
|
||||
}
|
||||
this.decode_trns?(src: args.src)
|
||||
this.seen_trns = true
|
||||
}
|
||||
}
|
||||
} else if this.chunk_type == 'tRNS'le {
|
||||
if this.seen_trns or (this.color_type > 3) or
|
||||
((this.color_type == 3) and (not this.seen_plte)) {
|
||||
return "#bad chunk"
|
||||
}
|
||||
this.decode_trns?(src: args.src)
|
||||
this.seen_trns = true
|
||||
}
|
||||
}
|
||||
|
||||
if this.metadata_fourcc == 0 {
|
||||
args.src.skip_u32?(n: this.chunk_length)
|
||||
}
|
||||
if this.metadata_fourcc == 0 {
|
||||
args.src.skip_u32?(n: this.chunk_length)
|
||||
}
|
||||
}
|
||||
|
||||
pri func decoder.decode_actl?(src: base.io_reader) {
|
||||
if this.chunk_length <> 8 {
|
||||
return "#bad chunk"
|
||||
} else if this.interlace_pass > 0 {
|
||||
// https://wiki.mozilla.org/APNG_Specification doesn't say whether the
|
||||
// interlace pattern starts at the image or frame top-left corner. We
|
||||
// avoid the question by returning "unsupported" for now.
|
||||
return "#unsupported PNG file"
|
||||
}
|
||||
this.chunk_length = 0
|
||||
this.num_animation_frames_value = args.src.read_u32be?()
|
||||
if this.num_animation_frames_value == 0 {
|
||||
return "#bad chunk"
|
||||
}
|
||||
this.num_animation_loops_value = args.src.read_u32be?()
|
||||
if this.chunk_length <> 8 {
|
||||
return "#bad chunk"
|
||||
} else if this.interlace_pass > 0 {
|
||||
// https://wiki.mozilla.org/APNG_Specification doesn't say whether the
|
||||
// interlace pattern starts at the image or frame top-left corner. We
|
||||
// avoid the question by returning "unsupported" for now.
|
||||
return "#unsupported PNG file"
|
||||
}
|
||||
this.chunk_length = 0
|
||||
this.num_animation_frames_value = args.src.read_u32be?()
|
||||
if this.num_animation_frames_value == 0 {
|
||||
return "#bad chunk"
|
||||
}
|
||||
this.num_animation_loops_value = args.src.read_u32be?()
|
||||
}
|
||||
|
||||
pri func decoder.decode_chrm?(src: base.io_reader) {
|
||||
var u : base.u64
|
||||
var u : base.u64
|
||||
|
||||
if this.chunk_length <> 32 {
|
||||
return "#bad chunk"
|
||||
}
|
||||
this.chunk_length = 0
|
||||
this.metadata_flavor = base.MORE_INFORMATION__FLAVOR__METADATA_PARSED
|
||||
this.metadata_fourcc = 'CHRM'be
|
||||
this.metadata_x = 0
|
||||
this.metadata_y = 0
|
||||
this.metadata_z = 0
|
||||
if this.chunk_length <> 32 {
|
||||
return "#bad chunk"
|
||||
}
|
||||
this.chunk_length = 0
|
||||
this.metadata_flavor = base.MORE_INFORMATION__FLAVOR__METADATA_PARSED
|
||||
this.metadata_fourcc = 'CHRM'be
|
||||
this.metadata_x = 0
|
||||
this.metadata_y = 0
|
||||
this.metadata_z = 0
|
||||
|
||||
// See the wuffs_base__more_information__metadata_parsed__chrm comments for
|
||||
// how we pack the eight chromaticity values into three u64 fields. This
|
||||
// admittedly truncates chromaticity values from 32 to 24 bits, but in
|
||||
// practice they only range between 0 and 100000 (which is 0x01_86A0).
|
||||
u = args.src.read_u32be_as_u64?()
|
||||
this.metadata_x |= (0xFF_FFFF & u) << 0
|
||||
u = args.src.read_u32be_as_u64?()
|
||||
this.metadata_x |= (0xFF_FFFF & u) << 24
|
||||
u = args.src.read_u32be_as_u64?()
|
||||
this.metadata_x |= (0xFF_FFFF & u) ~mod<< 48
|
||||
this.metadata_y |= (0xFF_FFFF & u) >> 16
|
||||
u = args.src.read_u32be_as_u64?()
|
||||
this.metadata_y |= (0xFF_FFFF & u) << 8
|
||||
u = args.src.read_u32be_as_u64?()
|
||||
this.metadata_y |= (0xFF_FFFF & u) << 32
|
||||
u = args.src.read_u32be_as_u64?()
|
||||
this.metadata_y |= (0xFF_FFFF & u) ~mod<< 56
|
||||
this.metadata_z |= (0xFF_FFFF & u) >> 8
|
||||
u = args.src.read_u32be_as_u64?()
|
||||
this.metadata_z |= (0xFF_FFFF & u) << 16
|
||||
u = args.src.read_u32be_as_u64?()
|
||||
this.metadata_z |= (0xFF_FFFF & u) << 40
|
||||
// See the wuffs_base__more_information__metadata_parsed__chrm comments for
|
||||
// how we pack the eight chromaticity values into three u64 fields. This
|
||||
// admittedly truncates chromaticity values from 32 to 24 bits, but in
|
||||
// practice they only range between 0 and 100000 (which is 0x01_86A0).
|
||||
u = args.src.read_u32be_as_u64?()
|
||||
this.metadata_x |= (0xFF_FFFF & u) << 0
|
||||
u = args.src.read_u32be_as_u64?()
|
||||
this.metadata_x |= (0xFF_FFFF & u) << 24
|
||||
u = args.src.read_u32be_as_u64?()
|
||||
this.metadata_x |= (0xFF_FFFF & u) ~mod<< 48
|
||||
this.metadata_y |= (0xFF_FFFF & u) >> 16
|
||||
u = args.src.read_u32be_as_u64?()
|
||||
this.metadata_y |= (0xFF_FFFF & u) << 8
|
||||
u = args.src.read_u32be_as_u64?()
|
||||
this.metadata_y |= (0xFF_FFFF & u) << 32
|
||||
u = args.src.read_u32be_as_u64?()
|
||||
this.metadata_y |= (0xFF_FFFF & u) ~mod<< 56
|
||||
this.metadata_z |= (0xFF_FFFF & u) >> 8
|
||||
u = args.src.read_u32be_as_u64?()
|
||||
this.metadata_z |= (0xFF_FFFF & u) << 16
|
||||
u = args.src.read_u32be_as_u64?()
|
||||
this.metadata_z |= (0xFF_FFFF & u) << 40
|
||||
}
|
||||
|
||||
pri func decoder.decode_exif?(src: base.io_reader) {
|
||||
if this.chunk_length < 4 {
|
||||
return "#bad chunk"
|
||||
}
|
||||
this.metadata_flavor = base.MORE_INFORMATION__FLAVOR__METADATA_RAW_PASSTHROUGH
|
||||
this.metadata_fourcc = 'EXIF'be
|
||||
this.metadata_x = 0
|
||||
this.metadata_y = args.src.position()
|
||||
this.metadata_z = this.metadata_y ~sat+ (this.chunk_length as base.u64)
|
||||
this.chunk_length = 0
|
||||
if this.chunk_length < 4 {
|
||||
return "#bad chunk"
|
||||
}
|
||||
this.metadata_flavor = base.MORE_INFORMATION__FLAVOR__METADATA_RAW_PASSTHROUGH
|
||||
this.metadata_fourcc = 'EXIF'be
|
||||
this.metadata_x = 0
|
||||
this.metadata_y = args.src.position()
|
||||
this.metadata_z = this.metadata_y ~sat+ (this.chunk_length as base.u64)
|
||||
this.chunk_length = 0
|
||||
}
|
||||
|
||||
pri func decoder.decode_fctl?(src: base.io_reader) {
|
||||
var x0 : base.u32
|
||||
var y0 : base.u32
|
||||
var x1 : base.u32
|
||||
var y1 : base.u32
|
||||
var x0 : base.u32
|
||||
var y0 : base.u32
|
||||
var x1 : base.u32
|
||||
var y1 : base.u32
|
||||
|
||||
if this.chunk_length <> 26 {
|
||||
return "#bad chunk"
|
||||
}
|
||||
this.chunk_length = 0
|
||||
if this.chunk_length <> 26 {
|
||||
return "#bad chunk"
|
||||
}
|
||||
this.chunk_length = 0
|
||||
|
||||
x0 = args.src.read_u32be?()
|
||||
if x0 <> this.next_animation_seq_num {
|
||||
return "#bad animation sequence number"
|
||||
} else if this.next_animation_seq_num >= 0xFFFF_FFFF {
|
||||
return "#unsupported PNG file"
|
||||
}
|
||||
this.next_animation_seq_num += 1
|
||||
x0 = args.src.read_u32be?()
|
||||
if x0 <> this.next_animation_seq_num {
|
||||
return "#bad animation sequence number"
|
||||
} else if this.next_animation_seq_num >= 0xFFFF_FFFF {
|
||||
return "#unsupported PNG file"
|
||||
}
|
||||
this.next_animation_seq_num += 1
|
||||
|
||||
x1 = args.src.read_u32be?()
|
||||
y1 = args.src.read_u32be?()
|
||||
x0 = args.src.read_u32be?()
|
||||
y0 = args.src.read_u32be?()
|
||||
x1 ~mod+= x0
|
||||
y1 ~mod+= y0
|
||||
if (x0 >= x1) or (x0 > this.width) or (x1 > this.width) or
|
||||
(y0 >= y1) or (y0 > this.height) or (y1 > this.height) {
|
||||
return "#bad chunk"
|
||||
}
|
||||
assert x1 <= 0x00FF_FFFF via "a <= b: a <= c; c <= b"(c: this.width)
|
||||
assert y1 <= 0x00FF_FFFF via "a <= b: a <= c; c <= b"(c: this.height)
|
||||
assert x0 <= 0x00FF_FFFF via "a <= b: a <= c; c <= b"(c: x1)
|
||||
assert y0 <= 0x00FF_FFFF via "a <= b: a <= c; c <= b"(c: y1)
|
||||
this.frame_rect_x0 = x0
|
||||
this.frame_rect_y0 = y0
|
||||
this.frame_rect_x1 = x1
|
||||
this.frame_rect_y1 = y1
|
||||
x1 = args.src.read_u32be?()
|
||||
y1 = args.src.read_u32be?()
|
||||
x0 = args.src.read_u32be?()
|
||||
y0 = args.src.read_u32be?()
|
||||
x1 ~mod+= x0
|
||||
y1 ~mod+= y0
|
||||
if (x0 >= x1) or (x0 > this.width) or (x1 > this.width) or
|
||||
(y0 >= y1) or (y0 > this.height) or (y1 > this.height) {
|
||||
return "#bad chunk"
|
||||
}
|
||||
assert x1 <= 0x00FF_FFFF via "a <= b: a <= c; c <= b"(c: this.width)
|
||||
assert y1 <= 0x00FF_FFFF via "a <= b: a <= c; c <= b"(c: this.height)
|
||||
assert x0 <= 0x00FF_FFFF via "a <= b: a <= c; c <= b"(c: x1)
|
||||
assert y0 <= 0x00FF_FFFF via "a <= b: a <= c; c <= b"(c: y1)
|
||||
this.frame_rect_x0 = x0
|
||||
this.frame_rect_y0 = y0
|
||||
this.frame_rect_x1 = x1
|
||||
this.frame_rect_y1 = y1
|
||||
|
||||
// There are 705_600000 flicks per second. A nominally zero denominator
|
||||
// means to use 100 instead (the units are centiseconds, the same as GIF).
|
||||
x0 = args.src.read_u16be_as_u32?()
|
||||
x1 = args.src.read_u16be_as_u32?()
|
||||
if x1 <= 0 {
|
||||
this.frame_duration = (x0 as base.u64) * 7_056000
|
||||
} else {
|
||||
this.frame_duration = ((x0 as base.u64) * 705_600000) / (x1 as base.u64)
|
||||
}
|
||||
// There are 705_600000 flicks per second. A nominally zero denominator
|
||||
// means to use 100 instead (the units are centiseconds, the same as GIF).
|
||||
x0 = args.src.read_u16be_as_u32?()
|
||||
x1 = args.src.read_u16be_as_u32?()
|
||||
if x1 <= 0 {
|
||||
this.frame_duration = (x0 as base.u64) * 7_056000
|
||||
} else {
|
||||
this.frame_duration = ((x0 as base.u64) * 705_600000) / (x1 as base.u64)
|
||||
}
|
||||
|
||||
x0 = args.src.read_u8_as_u32?()
|
||||
if x0 == 0 {
|
||||
this.frame_disposal = base.ANIMATION_DISPOSAL__NONE
|
||||
} else if x0 == 1 {
|
||||
this.frame_disposal = base.ANIMATION_DISPOSAL__RESTORE_BACKGROUND
|
||||
} else if x0 == 2 {
|
||||
this.frame_disposal = base.ANIMATION_DISPOSAL__RESTORE_PREVIOUS
|
||||
} else {
|
||||
return "#bad chunk"
|
||||
}
|
||||
x0 = args.src.read_u8_as_u32?()
|
||||
if x0 == 0 {
|
||||
this.frame_disposal = base.ANIMATION_DISPOSAL__NONE
|
||||
} else if x0 == 1 {
|
||||
this.frame_disposal = base.ANIMATION_DISPOSAL__RESTORE_BACKGROUND
|
||||
} else if x0 == 2 {
|
||||
this.frame_disposal = base.ANIMATION_DISPOSAL__RESTORE_PREVIOUS
|
||||
} else {
|
||||
return "#bad chunk"
|
||||
}
|
||||
|
||||
x0 = args.src.read_u8_as_u32?()
|
||||
if x0 == 0 {
|
||||
this.frame_overwrite_instead_of_blend = true
|
||||
} else if x0 == 1 {
|
||||
this.frame_overwrite_instead_of_blend = false
|
||||
} else {
|
||||
return "#bad chunk"
|
||||
}
|
||||
x0 = args.src.read_u8_as_u32?()
|
||||
if x0 == 0 {
|
||||
this.frame_overwrite_instead_of_blend = true
|
||||
} else if x0 == 1 {
|
||||
this.frame_overwrite_instead_of_blend = false
|
||||
} else {
|
||||
return "#bad chunk"
|
||||
}
|
||||
|
||||
if this.num_decoded_frame_configs_value == 0 {
|
||||
this.first_rect_x0 = this.frame_rect_x0
|
||||
this.first_rect_y0 = this.frame_rect_y0
|
||||
this.first_rect_x1 = this.frame_rect_x1
|
||||
this.first_rect_y1 = this.frame_rect_y1
|
||||
this.first_duration = this.frame_duration
|
||||
this.first_disposal = this.frame_disposal
|
||||
this.first_overwrite_instead_of_blend = this.frame_overwrite_instead_of_blend
|
||||
}
|
||||
if this.num_decoded_frame_configs_value == 0 {
|
||||
this.first_rect_x0 = this.frame_rect_x0
|
||||
this.first_rect_y0 = this.frame_rect_y0
|
||||
this.first_rect_x1 = this.frame_rect_x1
|
||||
this.first_rect_y1 = this.frame_rect_y1
|
||||
this.first_duration = this.frame_duration
|
||||
this.first_disposal = this.frame_disposal
|
||||
this.first_overwrite_instead_of_blend = this.frame_overwrite_instead_of_blend
|
||||
}
|
||||
}
|
||||
|
||||
pri func decoder.decode_gama?(src: base.io_reader) {
|
||||
if this.chunk_length <> 4 {
|
||||
return "#bad chunk"
|
||||
}
|
||||
this.chunk_length = 0
|
||||
this.metadata_flavor = base.MORE_INFORMATION__FLAVOR__METADATA_PARSED
|
||||
this.metadata_fourcc = 'GAMA'be
|
||||
this.metadata_x = args.src.read_u32be_as_u64?()
|
||||
this.metadata_y = 0
|
||||
this.metadata_z = 0
|
||||
if this.chunk_length <> 4 {
|
||||
return "#bad chunk"
|
||||
}
|
||||
this.chunk_length = 0
|
||||
this.metadata_flavor = base.MORE_INFORMATION__FLAVOR__METADATA_PARSED
|
||||
this.metadata_fourcc = 'GAMA'be
|
||||
this.metadata_x = args.src.read_u32be_as_u64?()
|
||||
this.metadata_y = 0
|
||||
this.metadata_z = 0
|
||||
}
|
||||
|
||||
pri func decoder.decode_iccp?(src: base.io_reader) {
|
||||
var c : base.u8
|
||||
var c : base.u8
|
||||
|
||||
// Skip the NUL-terminated color profile name.
|
||||
while true {
|
||||
if this.chunk_length <= 0 {
|
||||
return "#bad chunk"
|
||||
}
|
||||
this.chunk_length -= 1
|
||||
c = args.src.read_u8?()
|
||||
if c == 0 {
|
||||
break
|
||||
}
|
||||
} endwhile
|
||||
// Skip the NUL-terminated color profile name.
|
||||
while true {
|
||||
if this.chunk_length <= 0 {
|
||||
return "#bad chunk"
|
||||
}
|
||||
this.chunk_length -= 1
|
||||
c = args.src.read_u8?()
|
||||
if c == 0 {
|
||||
break
|
||||
}
|
||||
} endwhile
|
||||
|
||||
// Compression method.
|
||||
if this.chunk_length <= 0 {
|
||||
return "#bad chunk"
|
||||
}
|
||||
this.chunk_length -= 1
|
||||
c = args.src.read_u8?()
|
||||
if c <> 0 {
|
||||
return "#unsupported PNG compression method"
|
||||
}
|
||||
// Compression method.
|
||||
if this.chunk_length <= 0 {
|
||||
return "#bad chunk"
|
||||
}
|
||||
this.chunk_length -= 1
|
||||
c = args.src.read_u8?()
|
||||
if c <> 0 {
|
||||
return "#unsupported PNG compression method"
|
||||
}
|
||||
|
||||
this.metadata_is_zlib_compressed = true
|
||||
this.metadata_flavor = base.MORE_INFORMATION__FLAVOR__METADATA_RAW_TRANSFORM
|
||||
this.metadata_fourcc = 'ICCP'be
|
||||
this.metadata_x = 0
|
||||
this.metadata_y = 0
|
||||
this.metadata_z = 0
|
||||
this.metadata_is_zlib_compressed = true
|
||||
this.metadata_flavor = base.MORE_INFORMATION__FLAVOR__METADATA_RAW_TRANSFORM
|
||||
this.metadata_fourcc = 'ICCP'be
|
||||
this.metadata_x = 0
|
||||
this.metadata_y = 0
|
||||
this.metadata_z = 0
|
||||
}
|
||||
|
||||
pri func decoder.decode_plte?(src: base.io_reader) {
|
||||
var num_entries : base.u32[..= 256]
|
||||
var i : base.u32
|
||||
var argb : base.u32
|
||||
var num_entries : base.u32[..= 256]
|
||||
var i : base.u32
|
||||
var argb : base.u32
|
||||
|
||||
if (this.chunk_length > 768) or ((this.chunk_length % 3) <> 0) {
|
||||
return "#bad chunk"
|
||||
}
|
||||
num_entries = (this.chunk_length as base.u32) / 3
|
||||
this.chunk_length = 0
|
||||
if (this.chunk_length > 768) or ((this.chunk_length % 3) <> 0) {
|
||||
return "#bad chunk"
|
||||
}
|
||||
num_entries = (this.chunk_length as base.u32) / 3
|
||||
this.chunk_length = 0
|
||||
|
||||
while i < num_entries {
|
||||
assert i < 256 via "a < b: a < c; c <= b"(c: num_entries)
|
||||
// Convert from RGB (in memory order) to ARGB (in native u32 order)
|
||||
// to BGRA (in memory order).
|
||||
argb = args.src.read_u24be_as_u32?()
|
||||
argb |= 0xFF00_0000
|
||||
this.src_palette[(4 * i) + 0] = ((argb >> 0) & 0xFF) as base.u8
|
||||
this.src_palette[(4 * i) + 1] = ((argb >> 8) & 0xFF) as base.u8
|
||||
this.src_palette[(4 * i) + 2] = ((argb >> 16) & 0xFF) as base.u8
|
||||
this.src_palette[(4 * i) + 3] = ((argb >> 24) & 0xFF) as base.u8
|
||||
i += 1
|
||||
} endwhile
|
||||
while i < num_entries {
|
||||
assert i < 256 via "a < b: a < c; c <= b"(c: num_entries)
|
||||
// Convert from RGB (in memory order) to ARGB (in native u32 order)
|
||||
// to BGRA (in memory order).
|
||||
argb = args.src.read_u24be_as_u32?()
|
||||
argb |= 0xFF00_0000
|
||||
this.src_palette[(4 * i) + 0] = ((argb >> 0) & 0xFF) as base.u8
|
||||
this.src_palette[(4 * i) + 1] = ((argb >> 8) & 0xFF) as base.u8
|
||||
this.src_palette[(4 * i) + 2] = ((argb >> 16) & 0xFF) as base.u8
|
||||
this.src_palette[(4 * i) + 3] = ((argb >> 24) & 0xFF) as base.u8
|
||||
i += 1
|
||||
} endwhile
|
||||
|
||||
// Set the remaining palette entries to opaque black.
|
||||
while i < 256 {
|
||||
this.src_palette[(4 * i) + 0] = 0x00
|
||||
this.src_palette[(4 * i) + 1] = 0x00
|
||||
this.src_palette[(4 * i) + 2] = 0x00
|
||||
this.src_palette[(4 * i) + 3] = 0xFF
|
||||
i += 1
|
||||
} endwhile
|
||||
// Set the remaining palette entries to opaque black.
|
||||
while i < 256 {
|
||||
this.src_palette[(4 * i) + 0] = 0x00
|
||||
this.src_palette[(4 * i) + 1] = 0x00
|
||||
this.src_palette[(4 * i) + 2] = 0x00
|
||||
this.src_palette[(4 * i) + 3] = 0xFF
|
||||
i += 1
|
||||
} endwhile
|
||||
}
|
||||
|
||||
pri func decoder.decode_srgb?(src: base.io_reader) {
|
||||
if this.chunk_length <> 1 {
|
||||
return "#bad chunk"
|
||||
}
|
||||
this.chunk_length = 0
|
||||
this.metadata_flavor = base.MORE_INFORMATION__FLAVOR__METADATA_PARSED
|
||||
this.metadata_fourcc = 'SRGB'be
|
||||
this.metadata_x = args.src.read_u8_as_u64?()
|
||||
this.metadata_y = 0
|
||||
this.metadata_z = 0
|
||||
if this.chunk_length <> 1 {
|
||||
return "#bad chunk"
|
||||
}
|
||||
this.chunk_length = 0
|
||||
this.metadata_flavor = base.MORE_INFORMATION__FLAVOR__METADATA_PARSED
|
||||
this.metadata_fourcc = 'SRGB'be
|
||||
this.metadata_x = args.src.read_u8_as_u64?()
|
||||
this.metadata_y = 0
|
||||
this.metadata_z = 0
|
||||
}
|
||||
|
||||
pri func decoder.decode_trns?(src: base.io_reader) {
|
||||
var i : base.u32
|
||||
var n : base.u32[..= 256]
|
||||
var u : base.u64
|
||||
var i : base.u32
|
||||
var n : base.u32[..= 256]
|
||||
var u : base.u64
|
||||
|
||||
if this.color_type == 0 {
|
||||
choose filter_and_swizzle = [filter_and_swizzle_tricky]
|
||||
if this.depth <= 8 {
|
||||
this.dst_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL
|
||||
} else {
|
||||
this.dst_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE
|
||||
}
|
||||
if this.chunk_length <> 2 {
|
||||
return "#bad chunk"
|
||||
}
|
||||
this.chunk_length = 0
|
||||
if this.color_type == 0 {
|
||||
choose filter_and_swizzle = [filter_and_swizzle_tricky]
|
||||
if this.depth <= 8 {
|
||||
this.dst_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL
|
||||
} else {
|
||||
this.dst_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE
|
||||
}
|
||||
if this.chunk_length <> 2 {
|
||||
return "#bad chunk"
|
||||
}
|
||||
this.chunk_length = 0
|
||||
|
||||
u = args.src.read_u16be_as_u64?()
|
||||
if this.depth <= 1 {
|
||||
this.remap_transparency = ((u & 0x01) * 0xFF_FFFF) | 0xFF00_0000
|
||||
} else if this.depth <= 2 {
|
||||
this.remap_transparency = ((u & 0x03) * 0x55_5555) | 0xFF00_0000
|
||||
} else if this.depth <= 4 {
|
||||
this.remap_transparency = ((u & 0x0F) * 0x11_1111) | 0xFF00_0000
|
||||
} else if this.depth <= 8 {
|
||||
this.remap_transparency = ((u & 0xFF) * 0x01_0101) | 0xFF00_0000
|
||||
} else {
|
||||
this.remap_transparency = (u * 0x0001_0001_0001) | 0xFFFF_0000_0000_0000
|
||||
}
|
||||
u = args.src.read_u16be_as_u64?()
|
||||
if this.depth <= 1 {
|
||||
this.remap_transparency = ((u & 0x01) * 0xFF_FFFF) | 0xFF00_0000
|
||||
} else if this.depth <= 2 {
|
||||
this.remap_transparency = ((u & 0x03) * 0x55_5555) | 0xFF00_0000
|
||||
} else if this.depth <= 4 {
|
||||
this.remap_transparency = ((u & 0x0F) * 0x11_1111) | 0xFF00_0000
|
||||
} else if this.depth <= 8 {
|
||||
this.remap_transparency = ((u & 0xFF) * 0x01_0101) | 0xFF00_0000
|
||||
} else {
|
||||
this.remap_transparency = (u * 0x0001_0001_0001) | 0xFFFF_0000_0000_0000
|
||||
}
|
||||
|
||||
} else if this.color_type == 2 {
|
||||
choose filter_and_swizzle = [filter_and_swizzle_tricky]
|
||||
if this.depth <= 8 {
|
||||
this.dst_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL
|
||||
} else {
|
||||
this.dst_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE
|
||||
}
|
||||
if this.chunk_length <> 6 {
|
||||
return "#bad chunk"
|
||||
}
|
||||
this.chunk_length = 0
|
||||
} else if this.color_type == 2 {
|
||||
choose filter_and_swizzle = [filter_and_swizzle_tricky]
|
||||
if this.depth <= 8 {
|
||||
this.dst_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL
|
||||
} else {
|
||||
this.dst_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE
|
||||
}
|
||||
if this.chunk_length <> 6 {
|
||||
return "#bad chunk"
|
||||
}
|
||||
this.chunk_length = 0
|
||||
|
||||
u = args.src.read_u48be_as_u64?()
|
||||
if this.depth <= 8 {
|
||||
this.remap_transparency =
|
||||
(0x0000_00FF & (u >> 0)) |
|
||||
(0x0000_FF00 & (u >> 8)) |
|
||||
(0x00FF_0000 & (u >> 16)) |
|
||||
0xFF00_0000
|
||||
} else {
|
||||
this.remap_transparency = u | 0xFFFF_0000_0000_0000
|
||||
}
|
||||
u = args.src.read_u48be_as_u64?()
|
||||
if this.depth <= 8 {
|
||||
this.remap_transparency =
|
||||
(0x0000_00FF & (u >> 0)) |
|
||||
(0x0000_FF00 & (u >> 8)) |
|
||||
(0x00FF_0000 & (u >> 16)) |
|
||||
0xFF00_0000
|
||||
} else {
|
||||
this.remap_transparency = u | 0xFFFF_0000_0000_0000
|
||||
}
|
||||
|
||||
} else if this.color_type == 3 {
|
||||
this.dst_pixfmt = base.PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL
|
||||
if this.chunk_length > 256 {
|
||||
return "#bad chunk"
|
||||
}
|
||||
n = this.chunk_length as base.u32
|
||||
this.chunk_length = 0
|
||||
while i < n {
|
||||
assert i < 256 via "a < b: a < c; c <= b"(c: n)
|
||||
this.src_palette[(4 * i) + 3] = args.src.read_u8?()
|
||||
i += 1
|
||||
} endwhile
|
||||
} else if this.color_type == 3 {
|
||||
this.dst_pixfmt = base.PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL
|
||||
if this.chunk_length > 256 {
|
||||
return "#bad chunk"
|
||||
}
|
||||
n = this.chunk_length as base.u32
|
||||
this.chunk_length = 0
|
||||
while i < n {
|
||||
assert i < 256 via "a < b: a < c; c <= b"(c: n)
|
||||
this.src_palette[(4 * i) + 3] = args.src.read_u8?()
|
||||
i += 1
|
||||
} endwhile
|
||||
|
||||
} else {
|
||||
return "#bad chunk"
|
||||
}
|
||||
} else {
|
||||
return "#bad chunk"
|
||||
}
|
||||
}
|
||||
|
||||
pub func decoder.decode_frame_config?(dst: nptr base.frame_config, src: base.io_reader) {
|
||||
var status : base.status
|
||||
var status : base.status
|
||||
|
||||
while true {
|
||||
status =? this.do_decode_frame_config?(dst: args.dst, src: args.src)
|
||||
if (status == base."$short read") and args.src.is_closed() {
|
||||
return "#truncated input"
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
while true {
|
||||
status =? this.do_decode_frame_config?(dst: args.dst, src: args.src)
|
||||
if (status == base."$short read") and args.src.is_closed() {
|
||||
return "#truncated input"
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
}
|
||||
|
||||
pri func decoder.do_decode_frame_config?(dst: nptr base.frame_config, src: base.io_reader) {
|
||||
var checksum_have : base.u32
|
||||
var checksum_have : base.u32
|
||||
|
||||
if (this.call_sequence & 0x10) <> 0 {
|
||||
return base."#bad call sequence"
|
||||
} else if this.call_sequence == 0x20 {
|
||||
// No-op.
|
||||
} else if this.call_sequence < 0x20 {
|
||||
this.do_decode_image_config?(dst: nullptr, src: args.src)
|
||||
} else if this.call_sequence == 0x28 {
|
||||
if this.frame_config_io_position <> args.src.position() {
|
||||
return base."#bad restart"
|
||||
}
|
||||
} else if this.call_sequence == 0x40 {
|
||||
this.skip_frame?(src: args.src)
|
||||
} else {
|
||||
return base."@end of data"
|
||||
}
|
||||
if (this.call_sequence & 0x10) <> 0 {
|
||||
return base."#bad call sequence"
|
||||
} else if this.call_sequence == 0x20 {
|
||||
// No-op.
|
||||
} else if this.call_sequence < 0x20 {
|
||||
this.do_decode_image_config?(dst: nullptr, src: args.src)
|
||||
} else if this.call_sequence == 0x28 {
|
||||
if this.frame_config_io_position <> args.src.position() {
|
||||
return base."#bad restart"
|
||||
}
|
||||
} else if this.call_sequence == 0x40 {
|
||||
this.skip_frame?(src: args.src)
|
||||
} else {
|
||||
return base."@end of data"
|
||||
}
|
||||
|
||||
if this.metadata_fourcc <> 0 {
|
||||
this.call_sequence = 0x30
|
||||
return base."@metadata reported"
|
||||
}
|
||||
if this.metadata_fourcc <> 0 {
|
||||
this.call_sequence = 0x30
|
||||
return base."@metadata reported"
|
||||
}
|
||||
|
||||
if this.num_decoded_frame_configs_value == 0 {
|
||||
this.frame_rect_x0 = this.first_rect_x0
|
||||
this.frame_rect_y0 = this.first_rect_y0
|
||||
this.frame_rect_x1 = this.first_rect_x1
|
||||
this.frame_rect_y1 = this.first_rect_y1
|
||||
this.frame_config_io_position = this.first_config_io_position
|
||||
this.frame_duration = this.first_duration
|
||||
this.frame_disposal = this.first_disposal
|
||||
this.frame_overwrite_instead_of_blend = this.first_overwrite_instead_of_blend
|
||||
if this.num_decoded_frame_configs_value == 0 {
|
||||
this.frame_rect_x0 = this.first_rect_x0
|
||||
this.frame_rect_y0 = this.first_rect_y0
|
||||
this.frame_rect_x1 = this.first_rect_x1
|
||||
this.frame_rect_y1 = this.first_rect_y1
|
||||
this.frame_config_io_position = this.first_config_io_position
|
||||
this.frame_duration = this.first_duration
|
||||
this.frame_disposal = this.first_disposal
|
||||
this.frame_overwrite_instead_of_blend = this.first_overwrite_instead_of_blend
|
||||
|
||||
} else {
|
||||
// Decode the next IEND or fcTL chunk.
|
||||
while true {
|
||||
this.chunk_length = args.src.read_u32be?()
|
||||
this.chunk_type = args.src.read_u32le?()
|
||||
if this.chunk_type == 'IEND'le {
|
||||
if this.chunk_length <> 0 {
|
||||
return "#bad chunk"
|
||||
}
|
||||
checksum_have = args.src.read_u32le?()
|
||||
if (not this.ignore_checksum) and (checksum_have <> 0x8260_42AE) {
|
||||
return "#bad checksum"
|
||||
}
|
||||
this.call_sequence = 0x60
|
||||
return base."@end of data"
|
||||
} else if this.chunk_type == 'fdAT'le {
|
||||
return "#bad chunk"
|
||||
} else if this.chunk_type == 'fcTL'le {
|
||||
this.frame_config_io_position = args.src.position() ~mod- 8
|
||||
this.decode_fctl?(src: args.src)
|
||||
args.src.skip?(n: 4) // Skip the checksum.
|
||||
break
|
||||
}
|
||||
} else {
|
||||
// Decode the next IEND or fcTL chunk.
|
||||
while true {
|
||||
this.chunk_length = args.src.read_u32be?()
|
||||
this.chunk_type = args.src.read_u32le?()
|
||||
if this.chunk_type == 'IEND'le {
|
||||
if this.chunk_length <> 0 {
|
||||
return "#bad chunk"
|
||||
}
|
||||
checksum_have = args.src.read_u32le?()
|
||||
if (not this.ignore_checksum) and (checksum_have <> 0x8260_42AE) {
|
||||
return "#bad checksum"
|
||||
}
|
||||
this.call_sequence = 0x60
|
||||
return base."@end of data"
|
||||
} else if this.chunk_type == 'fdAT'le {
|
||||
return "#bad chunk"
|
||||
} else if this.chunk_type == 'fcTL'le {
|
||||
this.frame_config_io_position = args.src.position() ~mod- 8
|
||||
this.decode_fctl?(src: args.src)
|
||||
args.src.skip?(n: 4) // Skip the checksum.
|
||||
break
|
||||
}
|
||||
|
||||
this.decode_other_chunk?(src: args.src, framy: true)
|
||||
if this.metadata_fourcc <> 0 {
|
||||
this.call_sequence = 0x30
|
||||
return base."@metadata reported"
|
||||
}
|
||||
args.src.skip_u32?(n: 4) // Skip the checksum.
|
||||
this.chunk_length = 0
|
||||
} endwhile
|
||||
}
|
||||
this.decode_other_chunk?(src: args.src, framy: true)
|
||||
if this.metadata_fourcc <> 0 {
|
||||
this.call_sequence = 0x30
|
||||
return base."@metadata reported"
|
||||
}
|
||||
args.src.skip_u32?(n: 4) // Skip the checksum.
|
||||
this.chunk_length = 0
|
||||
} endwhile
|
||||
}
|
||||
|
||||
if args.dst <> nullptr {
|
||||
args.dst.set!(bounds: this.util.make_rect_ie_u32(
|
||||
min_incl_x: this.frame_rect_x0,
|
||||
min_incl_y: this.frame_rect_y0,
|
||||
max_excl_x: this.frame_rect_x1,
|
||||
max_excl_y: this.frame_rect_y1),
|
||||
duration: this.frame_duration,
|
||||
index: this.num_decoded_frame_configs_value as base.u64,
|
||||
io_position: this.frame_config_io_position,
|
||||
disposal: this.frame_disposal,
|
||||
opaque_within_bounds: (this.color_type <= 3) and (not this.seen_trns),
|
||||
overwrite_instead_of_blend: this.frame_overwrite_instead_of_blend,
|
||||
background_color: 0x0000_0000)
|
||||
}
|
||||
if args.dst <> nullptr {
|
||||
args.dst.set!(bounds: this.util.make_rect_ie_u32(
|
||||
min_incl_x: this.frame_rect_x0,
|
||||
min_incl_y: this.frame_rect_y0,
|
||||
max_excl_x: this.frame_rect_x1,
|
||||
max_excl_y: this.frame_rect_y1),
|
||||
duration: this.frame_duration,
|
||||
index: this.num_decoded_frame_configs_value as base.u64,
|
||||
io_position: this.frame_config_io_position,
|
||||
disposal: this.frame_disposal,
|
||||
opaque_within_bounds: (this.color_type <= 3) and (not this.seen_trns),
|
||||
overwrite_instead_of_blend: this.frame_overwrite_instead_of_blend,
|
||||
background_color: 0x0000_0000)
|
||||
}
|
||||
|
||||
this.num_decoded_frame_configs_value ~sat+= 1
|
||||
this.call_sequence = 0x40
|
||||
this.num_decoded_frame_configs_value ~sat+= 1
|
||||
this.call_sequence = 0x40
|
||||
}
|
||||
|
||||
pri func decoder.skip_frame?(src: base.io_reader) {
|
||||
var seq_num : base.u32
|
||||
var seq_num : base.u32
|
||||
|
||||
this.chunk_type_array[0] = 0
|
||||
this.chunk_type_array[1] = 0
|
||||
this.chunk_type_array[2] = 0
|
||||
this.chunk_type_array[3] = 0
|
||||
this.chunk_type_array[0] = 0
|
||||
this.chunk_type_array[1] = 0
|
||||
this.chunk_type_array[2] = 0
|
||||
this.chunk_type_array[3] = 0
|
||||
|
||||
while true {
|
||||
if args.src.length() < 8 {
|
||||
yield? base."$short read"
|
||||
continue
|
||||
}
|
||||
while true {
|
||||
if args.src.length() < 8 {
|
||||
yield? base."$short read"
|
||||
continue
|
||||
}
|
||||
|
||||
this.chunk_length = args.src.peek_u32be()
|
||||
this.chunk_type = (args.src.peek_u64le() >> 32) as base.u32
|
||||
this.chunk_length = args.src.peek_u32be()
|
||||
this.chunk_type = (args.src.peek_u64le() >> 32) as base.u32
|
||||
|
||||
if this.chunk_type == 'IDAT'le {
|
||||
if this.chunk_type_array[0] == 'f' {
|
||||
return "#bad chunk"
|
||||
}
|
||||
this.chunk_type_array[0] = 'I'
|
||||
this.chunk_type_array[1] = 'D'
|
||||
this.chunk_type_array[2] = 'A'
|
||||
this.chunk_type_array[3] = 'T'
|
||||
if this.chunk_type == 'IDAT'le {
|
||||
if this.chunk_type_array[0] == 'f' {
|
||||
return "#bad chunk"
|
||||
}
|
||||
this.chunk_type_array[0] = 'I'
|
||||
this.chunk_type_array[1] = 'D'
|
||||
this.chunk_type_array[2] = 'A'
|
||||
this.chunk_type_array[3] = 'T'
|
||||
|
||||
} else if this.chunk_type == 'fdAT'le {
|
||||
if this.chunk_type_array[0] == 'I' {
|
||||
return "#bad chunk"
|
||||
}
|
||||
this.chunk_type_array[0] = 'f'
|
||||
this.chunk_type_array[1] = 'd'
|
||||
this.chunk_type_array[2] = 'A'
|
||||
this.chunk_type_array[3] = 'T'
|
||||
if this.chunk_length < 4 {
|
||||
return "#bad chunk"
|
||||
}
|
||||
this.chunk_length -= 4
|
||||
args.src.skip_u32_fast!(actual: 8, worst_case: 8)
|
||||
seq_num = args.src.read_u32be?()
|
||||
if seq_num <> this.next_animation_seq_num {
|
||||
return "#bad animation sequence number"
|
||||
} else if this.next_animation_seq_num >= 0xFFFF_FFFF {
|
||||
return "#unsupported PNG file"
|
||||
}
|
||||
this.next_animation_seq_num += 1
|
||||
args.src.skip?(n: (this.chunk_length as base.u64) + 4) // +4 for the checksum.
|
||||
this.chunk_length = 0
|
||||
continue
|
||||
} else if this.chunk_type == 'fdAT'le {
|
||||
if this.chunk_type_array[0] == 'I' {
|
||||
return "#bad chunk"
|
||||
}
|
||||
this.chunk_type_array[0] = 'f'
|
||||
this.chunk_type_array[1] = 'd'
|
||||
this.chunk_type_array[2] = 'A'
|
||||
this.chunk_type_array[3] = 'T'
|
||||
if this.chunk_length < 4 {
|
||||
return "#bad chunk"
|
||||
}
|
||||
this.chunk_length -= 4
|
||||
args.src.skip_u32_fast!(actual: 8, worst_case: 8)
|
||||
seq_num = args.src.read_u32be?()
|
||||
if seq_num <> this.next_animation_seq_num {
|
||||
return "#bad animation sequence number"
|
||||
} else if this.next_animation_seq_num >= 0xFFFF_FFFF {
|
||||
return "#unsupported PNG file"
|
||||
}
|
||||
this.next_animation_seq_num += 1
|
||||
args.src.skip?(n: (this.chunk_length as base.u64) + 4) // +4 for the checksum.
|
||||
this.chunk_length = 0
|
||||
continue
|
||||
|
||||
} else if this.chunk_type_array[0] <> 0 {
|
||||
break
|
||||
} else if this.chunk_type_array[0] <> 0 {
|
||||
break
|
||||
|
||||
} else if this.chunk_type == 'fcTL'le {
|
||||
return "#bad chunk"
|
||||
}
|
||||
} else if this.chunk_type == 'fcTL'le {
|
||||
return "#bad chunk"
|
||||
}
|
||||
|
||||
// +12 for chunk length, chunk type and checksum.
|
||||
args.src.skip?(n: (this.chunk_length as base.u64) + 12)
|
||||
this.chunk_length = 0
|
||||
} endwhile
|
||||
// +12 for chunk length, chunk type and checksum.
|
||||
args.src.skip?(n: (this.chunk_length as base.u64) + 12)
|
||||
this.chunk_length = 0
|
||||
} endwhile
|
||||
|
||||
this.num_decoded_frames_value ~sat+= 1
|
||||
this.call_sequence = 0x20
|
||||
this.num_decoded_frames_value ~sat+= 1
|
||||
this.call_sequence = 0x20
|
||||
}
|
||||
|
||||
pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader, blend: base.pixel_blend, workbuf: slice base.u8, opts: nptr base.decode_frame_options) {
|
||||
var status : base.status
|
||||
var status : base.status
|
||||
|
||||
while true {
|
||||
status =? this.do_decode_frame?(dst: args.dst, src: args.src, blend: args.blend, workbuf: args.workbuf, opts: args.opts)
|
||||
if (status == base."$short read") and args.src.is_closed() {
|
||||
return "#truncated input"
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
while true {
|
||||
status =? this.do_decode_frame?(dst: args.dst, src: args.src, blend: args.blend, workbuf: args.workbuf, opts: args.opts)
|
||||
if (status == base."$short read") and args.src.is_closed() {
|
||||
return "#truncated input"
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
}
|
||||
|
||||
pri func decoder.do_decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader, blend: base.pixel_blend, workbuf: slice base.u8, opts: nptr base.decode_frame_options) {
|
||||
var seq_num : base.u32
|
||||
var status : base.status
|
||||
var pass_width : base.u32[..= 0x00FF_FFFF]
|
||||
var pass_height : base.u32[..= 0x00FF_FFFF]
|
||||
var seq_num : base.u32
|
||||
var status : base.status
|
||||
var pass_width : base.u32[..= 0x00FF_FFFF]
|
||||
var pass_height : base.u32[..= 0x00FF_FFFF]
|
||||
|
||||
if (this.call_sequence & 0x10) <> 0 {
|
||||
return base."#bad call sequence"
|
||||
} else if this.call_sequence >= 0x60 {
|
||||
return base."@end of data"
|
||||
} else if this.call_sequence <> 0x40 {
|
||||
this.do_decode_frame_config?(dst: nullptr, src: args.src)
|
||||
}
|
||||
if (this.call_sequence & 0x10) <> 0 {
|
||||
return base."#bad call sequence"
|
||||
} else if this.call_sequence >= 0x60 {
|
||||
return base."@end of data"
|
||||
} else if this.call_sequence <> 0x40 {
|
||||
this.do_decode_frame_config?(dst: nullptr, src: args.src)
|
||||
}
|
||||
|
||||
while true {
|
||||
if args.src.length() < 8 {
|
||||
yield? base."$short read"
|
||||
continue
|
||||
}
|
||||
while true {
|
||||
if args.src.length() < 8 {
|
||||
yield? base."$short read"
|
||||
continue
|
||||
}
|
||||
|
||||
this.chunk_length = args.src.peek_u32be()
|
||||
this.chunk_type = (args.src.peek_u64le() >> 32) as base.u32
|
||||
this.chunk_length = args.src.peek_u32be()
|
||||
this.chunk_type = (args.src.peek_u64le() >> 32) as base.u32
|
||||
|
||||
if this.chunk_type == 'IDAT'le {
|
||||
this.chunk_type_array[0] = 'I'
|
||||
this.chunk_type_array[1] = 'D'
|
||||
this.chunk_type_array[2] = 'A'
|
||||
this.chunk_type_array[3] = 'T'
|
||||
args.src.skip_u32_fast!(actual: 8, worst_case: 8)
|
||||
if not this.ignore_checksum {
|
||||
this.crc32.reset!()
|
||||
this.crc32.update_u32!(x: this.chunk_type_array[..])
|
||||
}
|
||||
break
|
||||
if this.chunk_type == 'IDAT'le {
|
||||
this.chunk_type_array[0] = 'I'
|
||||
this.chunk_type_array[1] = 'D'
|
||||
this.chunk_type_array[2] = 'A'
|
||||
this.chunk_type_array[3] = 'T'
|
||||
args.src.skip_u32_fast!(actual: 8, worst_case: 8)
|
||||
if not this.ignore_checksum {
|
||||
this.crc32.reset!()
|
||||
this.crc32.update_u32!(x: this.chunk_type_array[..])
|
||||
}
|
||||
break
|
||||
|
||||
} else if this.chunk_type == 'fdAT'le {
|
||||
this.chunk_type_array[0] = 'f'
|
||||
this.chunk_type_array[1] = 'd'
|
||||
this.chunk_type_array[2] = 'A'
|
||||
this.chunk_type_array[3] = 'T'
|
||||
if this.chunk_length < 4 {
|
||||
return "#bad chunk"
|
||||
}
|
||||
this.chunk_length -= 4
|
||||
args.src.skip_u32_fast!(actual: 8, worst_case: 8)
|
||||
seq_num = args.src.read_u32be?()
|
||||
if seq_num <> this.next_animation_seq_num {
|
||||
return "#bad animation sequence number"
|
||||
} else if this.next_animation_seq_num >= 0xFFFF_FFFF {
|
||||
return "#unsupported PNG file"
|
||||
}
|
||||
this.next_animation_seq_num += 1
|
||||
break
|
||||
} else if this.chunk_type == 'fdAT'le {
|
||||
this.chunk_type_array[0] = 'f'
|
||||
this.chunk_type_array[1] = 'd'
|
||||
this.chunk_type_array[2] = 'A'
|
||||
this.chunk_type_array[3] = 'T'
|
||||
if this.chunk_length < 4 {
|
||||
return "#bad chunk"
|
||||
}
|
||||
this.chunk_length -= 4
|
||||
args.src.skip_u32_fast!(actual: 8, worst_case: 8)
|
||||
seq_num = args.src.read_u32be?()
|
||||
if seq_num <> this.next_animation_seq_num {
|
||||
return "#bad animation sequence number"
|
||||
} else if this.next_animation_seq_num >= 0xFFFF_FFFF {
|
||||
return "#unsupported PNG file"
|
||||
}
|
||||
this.next_animation_seq_num += 1
|
||||
break
|
||||
|
||||
} else if this.chunk_type == 'fcTL'le {
|
||||
return "#bad chunk"
|
||||
}
|
||||
} else if this.chunk_type == 'fcTL'le {
|
||||
return "#bad chunk"
|
||||
}
|
||||
|
||||
// +12 for chunk length, chunk type and checksum.
|
||||
args.src.skip?(n: (this.chunk_length as base.u64) + 12)
|
||||
this.chunk_length = 0
|
||||
} endwhile
|
||||
// +12 for chunk length, chunk type and checksum.
|
||||
args.src.skip?(n: (this.chunk_length as base.u64) + 12)
|
||||
this.chunk_length = 0
|
||||
} endwhile
|
||||
|
||||
if this.zlib_is_dirty {
|
||||
this.zlib.reset!()
|
||||
if this.ignore_checksum {
|
||||
this.zlib.set_quirk_enabled!(quirk: base.QUIRK_IGNORE_CHECKSUM, enabled: true)
|
||||
}
|
||||
}
|
||||
this.zlib_is_dirty = true
|
||||
if this.zlib_is_dirty {
|
||||
this.zlib.reset!()
|
||||
if this.ignore_checksum {
|
||||
this.zlib.set_quirk_enabled!(quirk: base.QUIRK_IGNORE_CHECKSUM, enabled: true)
|
||||
}
|
||||
}
|
||||
this.zlib_is_dirty = true
|
||||
|
||||
status = this.swizzler.prepare!(
|
||||
dst_pixfmt: args.dst.pixel_format(),
|
||||
dst_palette: args.dst.palette_or_else(fallback: this.dst_palette[..]),
|
||||
src_pixfmt: this.util.make_pixel_format(repr: this.src_pixfmt),
|
||||
src_palette: this.src_palette[..],
|
||||
blend: args.blend)
|
||||
if not status.is_ok() {
|
||||
return status
|
||||
}
|
||||
status = this.swizzler.prepare!(
|
||||
dst_pixfmt: args.dst.pixel_format(),
|
||||
dst_palette: args.dst.palette_or_else(fallback: this.dst_palette[..]),
|
||||
src_pixfmt: this.util.make_pixel_format(repr: this.src_pixfmt),
|
||||
src_palette: this.src_palette[..],
|
||||
blend: args.blend)
|
||||
if not status.is_ok() {
|
||||
return status
|
||||
}
|
||||
|
||||
this.workbuf_hist_pos_base = 0
|
||||
while true {
|
||||
if (this.chunk_type_array[0] == 'I') {
|
||||
pass_width = 0x00FF_FFFF &
|
||||
(((INTERLACING[this.interlace_pass][1] as base.u32) + this.width) >>
|
||||
INTERLACING[this.interlace_pass][0])
|
||||
pass_height = 0x00FF_FFFF &
|
||||
(((INTERLACING[this.interlace_pass][4] as base.u32) + this.height) >>
|
||||
INTERLACING[this.interlace_pass][3])
|
||||
} else {
|
||||
pass_width = 0x00FF_FFFF & (this.frame_rect_x1 ~mod- this.frame_rect_x0)
|
||||
pass_height = 0x00FF_FFFF & (this.frame_rect_y1 ~mod- this.frame_rect_y0)
|
||||
}
|
||||
this.workbuf_hist_pos_base = 0
|
||||
while true {
|
||||
if (this.chunk_type_array[0] == 'I') {
|
||||
pass_width = 0x00FF_FFFF &
|
||||
(((INTERLACING[this.interlace_pass][1] as base.u32) + this.width) >>
|
||||
INTERLACING[this.interlace_pass][0])
|
||||
pass_height = 0x00FF_FFFF &
|
||||
(((INTERLACING[this.interlace_pass][4] as base.u32) + this.height) >>
|
||||
INTERLACING[this.interlace_pass][3])
|
||||
} else {
|
||||
pass_width = 0x00FF_FFFF & (this.frame_rect_x1 ~mod- this.frame_rect_x0)
|
||||
pass_height = 0x00FF_FFFF & (this.frame_rect_y1 ~mod- this.frame_rect_y0)
|
||||
}
|
||||
|
||||
if (pass_width > 0) and (pass_height > 0) {
|
||||
this.pass_bytes_per_row = this.calculate_bytes_per_row(width: pass_width)
|
||||
this.pass_workbuf_length = (pass_height as base.u64) * (1 + this.pass_bytes_per_row)
|
||||
while true {
|
||||
status =? this.decode_pass?(src: args.src, workbuf: args.workbuf)
|
||||
if status.is_ok() {
|
||||
break
|
||||
} else if status.is_error() or
|
||||
((status == base."$short read") and args.src.is_closed()) {
|
||||
// The input was invalid or truncated. Produce whatever
|
||||
// pixels we can.
|
||||
if this.workbuf_wi <= args.workbuf.length() {
|
||||
// This might return "#internal error: inconsistent
|
||||
// workbuf length" because of the ".. this.workbuf_wi".
|
||||
// We just ignore the error.
|
||||
this.filter_and_swizzle!(dst: args.dst, workbuf: args.workbuf[.. this.workbuf_wi])
|
||||
}
|
||||
if status == base."$short read" {
|
||||
return "#truncated input"
|
||||
}
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
status = this.filter_and_swizzle!(dst: args.dst, workbuf: args.workbuf)
|
||||
if not status.is_ok() {
|
||||
return status
|
||||
}
|
||||
this.workbuf_hist_pos_base ~mod+= this.pass_workbuf_length
|
||||
}
|
||||
if (pass_width > 0) and (pass_height > 0) {
|
||||
this.pass_bytes_per_row = this.calculate_bytes_per_row(width: pass_width)
|
||||
this.pass_workbuf_length = (pass_height as base.u64) * (1 + this.pass_bytes_per_row)
|
||||
while true {
|
||||
status =? this.decode_pass?(src: args.src, workbuf: args.workbuf)
|
||||
if status.is_ok() {
|
||||
break
|
||||
} else if status.is_error() or
|
||||
((status == base."$short read") and args.src.is_closed()) {
|
||||
// The input was invalid or truncated. Produce whatever
|
||||
// pixels we can.
|
||||
if this.workbuf_wi <= args.workbuf.length() {
|
||||
// This might return "#internal error: inconsistent
|
||||
// workbuf length" because of the ".. this.workbuf_wi".
|
||||
// We just ignore the error.
|
||||
this.filter_and_swizzle!(dst: args.dst, workbuf: args.workbuf[.. this.workbuf_wi])
|
||||
}
|
||||
if status == base."$short read" {
|
||||
return "#truncated input"
|
||||
}
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
status = this.filter_and_swizzle!(dst: args.dst, workbuf: args.workbuf)
|
||||
if not status.is_ok() {
|
||||
return status
|
||||
}
|
||||
this.workbuf_hist_pos_base ~mod+= this.pass_workbuf_length
|
||||
}
|
||||
|
||||
if (this.interlace_pass == 0) or (this.interlace_pass >= 7) {
|
||||
break
|
||||
}
|
||||
this.interlace_pass += 1
|
||||
} endwhile
|
||||
if (this.interlace_pass == 0) or (this.interlace_pass >= 7) {
|
||||
break
|
||||
}
|
||||
this.interlace_pass += 1
|
||||
} endwhile
|
||||
|
||||
this.num_decoded_frames_value ~sat+= 1
|
||||
this.call_sequence = 0x20
|
||||
this.num_decoded_frames_value ~sat+= 1
|
||||
this.call_sequence = 0x20
|
||||
}
|
||||
|
||||
pri func decoder.decode_pass?(src: base.io_reader, workbuf: slice base.u8) {
|
||||
var w : base.io_writer
|
||||
var w_mark : base.u64
|
||||
var r_mark : base.u64
|
||||
var zlib_status : base.status
|
||||
var checksum_have : base.u32
|
||||
var checksum_want : base.u32
|
||||
var seq_num : base.u32
|
||||
var w : base.io_writer
|
||||
var w_mark : base.u64
|
||||
var r_mark : base.u64
|
||||
var zlib_status : base.status
|
||||
var checksum_have : base.u32
|
||||
var checksum_want : base.u32
|
||||
var seq_num : base.u32
|
||||
|
||||
this.workbuf_wi = 0
|
||||
while true {
|
||||
if (this.workbuf_wi > this.pass_workbuf_length) or (
|
||||
this.pass_workbuf_length > args.workbuf.length()) {
|
||||
return base."#bad workbuf length"
|
||||
}
|
||||
io_bind (io: w, data: args.workbuf[this.workbuf_wi .. this.pass_workbuf_length], history_position: this.workbuf_hist_pos_base ~mod+ this.workbuf_wi) {
|
||||
io_limit (io: args.src, limit: (this.chunk_length as base.u64)) {
|
||||
w_mark = w.mark()
|
||||
r_mark = args.src.mark()
|
||||
zlib_status =? this.zlib.transform_io?(
|
||||
dst: w, src: args.src, workbuf: this.util.empty_slice_u8())
|
||||
if not this.ignore_checksum {
|
||||
this.crc32.update_u32!(x: args.src.since(mark: r_mark))
|
||||
}
|
||||
this.chunk_length ~sat-= (args.src.count_since(mark: r_mark) & 0xFFFF_FFFF) as base.u32
|
||||
this.workbuf_wi ~sat+= w.count_since(mark: w_mark)
|
||||
}
|
||||
}
|
||||
this.workbuf_wi = 0
|
||||
while true {
|
||||
if (this.workbuf_wi > this.pass_workbuf_length) or (
|
||||
this.pass_workbuf_length > args.workbuf.length()) {
|
||||
return base."#bad workbuf length"
|
||||
}
|
||||
io_bind (io: w, data: args.workbuf[this.workbuf_wi .. this.pass_workbuf_length], history_position: this.workbuf_hist_pos_base ~mod+ this.workbuf_wi) {
|
||||
io_limit (io: args.src, limit: (this.chunk_length as base.u64)) {
|
||||
w_mark = w.mark()
|
||||
r_mark = args.src.mark()
|
||||
zlib_status =? this.zlib.transform_io?(
|
||||
dst: w, src: args.src, workbuf: this.util.empty_slice_u8())
|
||||
if not this.ignore_checksum {
|
||||
this.crc32.update_u32!(x: args.src.since(mark: r_mark))
|
||||
}
|
||||
this.chunk_length ~sat-= (args.src.count_since(mark: r_mark) & 0xFFFF_FFFF) as base.u32
|
||||
this.workbuf_wi ~sat+= w.count_since(mark: w_mark)
|
||||
}
|
||||
}
|
||||
|
||||
if zlib_status.is_ok() {
|
||||
if this.chunk_length > 0 {
|
||||
// TODO: should this really be a fatal error?
|
||||
return base."#too much data"
|
||||
}
|
||||
checksum_want = args.src.read_u32be?()
|
||||
// Verify the final IDAT chunk's CRC-32 checksum.
|
||||
if (not this.ignore_checksum) and (this.chunk_type_array[0] == 'I') {
|
||||
checksum_have = this.crc32.update_u32!(x: this.util.empty_slice_u8())
|
||||
if checksum_have <> checksum_want {
|
||||
return "#bad checksum"
|
||||
}
|
||||
}
|
||||
break
|
||||
} else if zlib_status == base."$short write" {
|
||||
if (1 <= this.interlace_pass) and (this.interlace_pass <= 6) {
|
||||
break
|
||||
}
|
||||
return base."#too much data"
|
||||
} else if zlib_status <> base."$short read" {
|
||||
return zlib_status
|
||||
} else if this.chunk_length == 0 {
|
||||
// Verify the non-final IDAT chunk's CRC-32 checksum.
|
||||
checksum_want = args.src.read_u32be?()
|
||||
if (not this.ignore_checksum) and (this.chunk_type_array[0] == 'I') {
|
||||
checksum_have = this.crc32.update_u32!(x: this.util.empty_slice_u8())
|
||||
if checksum_have <> checksum_want {
|
||||
return "#bad checksum"
|
||||
}
|
||||
}
|
||||
if zlib_status.is_ok() {
|
||||
if this.chunk_length > 0 {
|
||||
// TODO: should this really be a fatal error?
|
||||
return base."#too much data"
|
||||
}
|
||||
checksum_want = args.src.read_u32be?()
|
||||
// Verify the final IDAT chunk's CRC-32 checksum.
|
||||
if (not this.ignore_checksum) and (this.chunk_type_array[0] == 'I') {
|
||||
checksum_have = this.crc32.update_u32!(x: this.util.empty_slice_u8())
|
||||
if checksum_have <> checksum_want {
|
||||
return "#bad checksum"
|
||||
}
|
||||
}
|
||||
break
|
||||
} else if zlib_status == base."$short write" {
|
||||
if (1 <= this.interlace_pass) and (this.interlace_pass <= 6) {
|
||||
break
|
||||
}
|
||||
return base."#too much data"
|
||||
} else if zlib_status <> base."$short read" {
|
||||
return zlib_status
|
||||
} else if this.chunk_length == 0 {
|
||||
// Verify the non-final IDAT chunk's CRC-32 checksum.
|
||||
checksum_want = args.src.read_u32be?()
|
||||
if (not this.ignore_checksum) and (this.chunk_type_array[0] == 'I') {
|
||||
checksum_have = this.crc32.update_u32!(x: this.util.empty_slice_u8())
|
||||
if checksum_have <> checksum_want {
|
||||
return "#bad checksum"
|
||||
}
|
||||
}
|
||||
|
||||
// The next chunk should be another IDAT or fdAT.
|
||||
this.chunk_length = args.src.read_u32be?()
|
||||
this.chunk_type = args.src.read_u32le?()
|
||||
if (this.chunk_type_array[0] == 'I') {
|
||||
if this.chunk_type <> 'IDAT'le {
|
||||
return "#bad chunk"
|
||||
}
|
||||
// The IDAT is part of the next CRC-32 checksum's input.
|
||||
if not this.ignore_checksum {
|
||||
this.crc32.reset!()
|
||||
this.crc32.update_u32!(x: this.chunk_type_array[..])
|
||||
}
|
||||
} else {
|
||||
if (this.chunk_type <> 'fdAT'le) or (this.chunk_length < 4) {
|
||||
return "#bad chunk"
|
||||
}
|
||||
this.chunk_length -= 4
|
||||
seq_num = args.src.read_u32be?()
|
||||
if seq_num <> this.next_animation_seq_num {
|
||||
return "#bad animation sequence number"
|
||||
} else if this.next_animation_seq_num >= 0xFFFF_FFFF {
|
||||
return "#unsupported PNG file"
|
||||
}
|
||||
this.next_animation_seq_num += 1
|
||||
}
|
||||
continue
|
||||
} else if args.src.length() > 0 {
|
||||
return "#internal error: zlib decoder did not exhaust its input"
|
||||
}
|
||||
yield? base."$short read"
|
||||
} endwhile
|
||||
// The next chunk should be another IDAT or fdAT.
|
||||
this.chunk_length = args.src.read_u32be?()
|
||||
this.chunk_type = args.src.read_u32le?()
|
||||
if (this.chunk_type_array[0] == 'I') {
|
||||
if this.chunk_type <> 'IDAT'le {
|
||||
return "#bad chunk"
|
||||
}
|
||||
// The IDAT is part of the next CRC-32 checksum's input.
|
||||
if not this.ignore_checksum {
|
||||
this.crc32.reset!()
|
||||
this.crc32.update_u32!(x: this.chunk_type_array[..])
|
||||
}
|
||||
} else {
|
||||
if (this.chunk_type <> 'fdAT'le) or (this.chunk_length < 4) {
|
||||
return "#bad chunk"
|
||||
}
|
||||
this.chunk_length -= 4
|
||||
seq_num = args.src.read_u32be?()
|
||||
if seq_num <> this.next_animation_seq_num {
|
||||
return "#bad animation sequence number"
|
||||
} else if this.next_animation_seq_num >= 0xFFFF_FFFF {
|
||||
return "#unsupported PNG file"
|
||||
}
|
||||
this.next_animation_seq_num += 1
|
||||
}
|
||||
continue
|
||||
} else if args.src.length() > 0 {
|
||||
return "#internal error: zlib decoder did not exhaust its input"
|
||||
}
|
||||
yield? base."$short read"
|
||||
} endwhile
|
||||
|
||||
if this.workbuf_wi <> this.pass_workbuf_length {
|
||||
return base."#not enough data"
|
||||
} else if 0 < args.workbuf.length() {
|
||||
// For the top row, the Paeth filter (4) is equivalent to the Sub
|
||||
// filter (1), but the Paeth implementation is simpler if it can assume
|
||||
// that there is a previous row.
|
||||
if args.workbuf[0] == 4 {
|
||||
args.workbuf[0] = 1
|
||||
}
|
||||
}
|
||||
if this.workbuf_wi <> this.pass_workbuf_length {
|
||||
return base."#not enough data"
|
||||
} else if 0 < args.workbuf.length() {
|
||||
// For the top row, the Paeth filter (4) is equivalent to the Sub
|
||||
// filter (1), but the Paeth implementation is simpler if it can assume
|
||||
// that there is a previous row.
|
||||
if args.workbuf[0] == 4 {
|
||||
args.workbuf[0] = 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub func decoder.frame_dirty_rect() base.rect_ie_u32 {
|
||||
return this.util.make_rect_ie_u32(
|
||||
min_incl_x: this.frame_rect_x0,
|
||||
min_incl_y: this.frame_rect_y0,
|
||||
max_excl_x: this.frame_rect_x1,
|
||||
max_excl_y: this.frame_rect_y1)
|
||||
return this.util.make_rect_ie_u32(
|
||||
min_incl_x: this.frame_rect_x0,
|
||||
min_incl_y: this.frame_rect_y0,
|
||||
max_excl_x: this.frame_rect_x1,
|
||||
max_excl_y: this.frame_rect_y1)
|
||||
}
|
||||
|
||||
pub func decoder.num_animation_loops() base.u32 {
|
||||
return this.num_animation_loops_value
|
||||
return this.num_animation_loops_value
|
||||
}
|
||||
|
||||
pub func decoder.num_decoded_frame_configs() base.u64 {
|
||||
return this.num_decoded_frame_configs_value as base.u64
|
||||
return this.num_decoded_frame_configs_value as base.u64
|
||||
}
|
||||
|
||||
pub func decoder.num_decoded_frames() base.u64 {
|
||||
return this.num_decoded_frames_value as base.u64
|
||||
return this.num_decoded_frames_value as base.u64
|
||||
}
|
||||
|
||||
pub func decoder.restart_frame!(index: base.u64, io_position: base.u64) base.status {
|
||||
if this.call_sequence < 0x20 {
|
||||
return base."#bad call sequence"
|
||||
} else if (args.index >= (this.num_animation_frames_value as base.u64)) or
|
||||
((args.index == 0) and (args.io_position <> this.first_config_io_position)) {
|
||||
return base."#bad argument"
|
||||
}
|
||||
this.call_sequence = 0x28
|
||||
if this.interlace_pass >= 1 {
|
||||
this.interlace_pass = 1
|
||||
}
|
||||
this.frame_config_io_position = args.io_position
|
||||
this.num_decoded_frame_configs_value = (args.index & 0xFFFF_FFFF) as base.u32
|
||||
this.num_decoded_frames_value = this.num_decoded_frame_configs_value
|
||||
return ok
|
||||
if this.call_sequence < 0x20 {
|
||||
return base."#bad call sequence"
|
||||
} else if (args.index >= (this.num_animation_frames_value as base.u64)) or
|
||||
((args.index == 0) and (args.io_position <> this.first_config_io_position)) {
|
||||
return base."#bad argument"
|
||||
}
|
||||
this.call_sequence = 0x28
|
||||
if this.interlace_pass >= 1 {
|
||||
this.interlace_pass = 1
|
||||
}
|
||||
this.frame_config_io_position = args.io_position
|
||||
this.num_decoded_frame_configs_value = (args.index & 0xFFFF_FFFF) as base.u32
|
||||
this.num_decoded_frames_value = this.num_decoded_frame_configs_value
|
||||
return ok
|
||||
}
|
||||
|
||||
pub func decoder.set_report_metadata!(fourcc: base.u32, report: base.bool) {
|
||||
if args.fourcc == 'CHRM'be {
|
||||
this.report_metadata_chrm = args.report
|
||||
} else if args.fourcc == 'EXIF'be {
|
||||
this.report_metadata_exif = args.report
|
||||
} else if args.fourcc == 'GAMA'be {
|
||||
this.report_metadata_gama = args.report
|
||||
} else if args.fourcc == 'ICCP'be {
|
||||
this.report_metadata_iccp = args.report
|
||||
} else if args.fourcc == 'KVP 'be {
|
||||
this.report_metadata_kvp = args.report
|
||||
} else if args.fourcc == 'SRGB'be {
|
||||
this.report_metadata_srgb = args.report
|
||||
}
|
||||
if args.fourcc == 'CHRM'be {
|
||||
this.report_metadata_chrm = args.report
|
||||
} else if args.fourcc == 'EXIF'be {
|
||||
this.report_metadata_exif = args.report
|
||||
} else if args.fourcc == 'GAMA'be {
|
||||
this.report_metadata_gama = args.report
|
||||
} else if args.fourcc == 'ICCP'be {
|
||||
this.report_metadata_iccp = args.report
|
||||
} else if args.fourcc == 'KVP 'be {
|
||||
this.report_metadata_kvp = args.report
|
||||
} else if args.fourcc == 'SRGB'be {
|
||||
this.report_metadata_srgb = args.report
|
||||
}
|
||||
}
|
||||
|
||||
pub func decoder.tell_me_more?(dst: base.io_writer, minfo: nptr base.more_information, src: base.io_reader) {
|
||||
var status : base.status
|
||||
var status : base.status
|
||||
|
||||
while true {
|
||||
status =? this.do_tell_me_more?(dst: args.dst, minfo: args.minfo, src: args.src)
|
||||
if (status == base."$short read") and args.src.is_closed() {
|
||||
return "#truncated input"
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
while true {
|
||||
status =? this.do_tell_me_more?(dst: args.dst, minfo: args.minfo, src: args.src)
|
||||
if (status == base."$short read") and args.src.is_closed() {
|
||||
return "#truncated input"
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
}
|
||||
|
||||
pri func decoder.do_tell_me_more?(dst: base.io_writer, minfo: nptr base.more_information, src: base.io_reader) {
|
||||
var c : base.u8
|
||||
var c2 : base.u16
|
||||
var w : base.io_writer
|
||||
var num_written : base.u64
|
||||
var w_mark : base.u64
|
||||
var r_mark : base.u64
|
||||
var zlib_status : base.status
|
||||
var c : base.u8
|
||||
var c2 : base.u16
|
||||
var w : base.io_writer
|
||||
var num_written : base.u64
|
||||
var w_mark : base.u64
|
||||
var r_mark : base.u64
|
||||
var zlib_status : base.status
|
||||
|
||||
if (this.call_sequence & 0x10) == 0 {
|
||||
return base."#bad call sequence"
|
||||
}
|
||||
if this.metadata_fourcc == 0 {
|
||||
return base."#no more information"
|
||||
}
|
||||
if (this.call_sequence & 0x10) == 0 {
|
||||
return base."#bad call sequence"
|
||||
}
|
||||
if this.metadata_fourcc == 0 {
|
||||
return base."#no more information"
|
||||
}
|
||||
|
||||
while.goto_done true {{
|
||||
if this.metadata_flavor == base.MORE_INFORMATION__FLAVOR__METADATA_RAW_PASSTHROUGH {
|
||||
while true {
|
||||
if args.src.position() <> this.metadata_y {
|
||||
return base."#bad I/O position"
|
||||
} else if args.minfo <> nullptr {
|
||||
args.minfo.set!(
|
||||
flavor: this.metadata_flavor,
|
||||
w: this.metadata_fourcc,
|
||||
x: this.metadata_x,
|
||||
y: this.metadata_y,
|
||||
z: this.metadata_z)
|
||||
}
|
||||
if this.metadata_y >= this.metadata_z {
|
||||
break.goto_done
|
||||
}
|
||||
this.metadata_y = this.metadata_z
|
||||
yield? base."$even more information"
|
||||
} endwhile
|
||||
}
|
||||
while.goto_done true {{
|
||||
if this.metadata_flavor == base.MORE_INFORMATION__FLAVOR__METADATA_RAW_PASSTHROUGH {
|
||||
while true {
|
||||
if args.src.position() <> this.metadata_y {
|
||||
return base."#bad I/O position"
|
||||
} else if args.minfo <> nullptr {
|
||||
args.minfo.set!(
|
||||
flavor: this.metadata_flavor,
|
||||
w: this.metadata_fourcc,
|
||||
x: this.metadata_x,
|
||||
y: this.metadata_y,
|
||||
z: this.metadata_z)
|
||||
}
|
||||
if this.metadata_y >= this.metadata_z {
|
||||
break.goto_done
|
||||
}
|
||||
this.metadata_y = this.metadata_z
|
||||
yield? base."$even more information"
|
||||
} endwhile
|
||||
}
|
||||
|
||||
if this.metadata_is_zlib_compressed {
|
||||
if this.zlib_is_dirty {
|
||||
this.zlib.reset!()
|
||||
if this.ignore_checksum {
|
||||
this.zlib.set_quirk_enabled!(quirk: base.QUIRK_IGNORE_CHECKSUM, enabled: true)
|
||||
}
|
||||
}
|
||||
this.zlib_is_dirty = true
|
||||
this.ztxt_hist_pos = 0
|
||||
}
|
||||
if this.metadata_is_zlib_compressed {
|
||||
if this.zlib_is_dirty {
|
||||
this.zlib.reset!()
|
||||
if this.ignore_checksum {
|
||||
this.zlib.set_quirk_enabled!(quirk: base.QUIRK_IGNORE_CHECKSUM, enabled: true)
|
||||
}
|
||||
}
|
||||
this.zlib_is_dirty = true
|
||||
this.ztxt_hist_pos = 0
|
||||
}
|
||||
|
||||
while.loop true {
|
||||
if args.minfo <> nullptr {
|
||||
args.minfo.set!(
|
||||
flavor: this.metadata_flavor,
|
||||
w: this.metadata_fourcc,
|
||||
x: this.metadata_x,
|
||||
y: this.metadata_y,
|
||||
z: this.metadata_z)
|
||||
}
|
||||
while.loop true {
|
||||
if args.minfo <> nullptr {
|
||||
args.minfo.set!(
|
||||
flavor: this.metadata_flavor,
|
||||
w: this.metadata_fourcc,
|
||||
x: this.metadata_x,
|
||||
y: this.metadata_y,
|
||||
z: this.metadata_z)
|
||||
}
|
||||
|
||||
if this.metadata_flavor <> base.MORE_INFORMATION__FLAVOR__METADATA_RAW_TRANSFORM {
|
||||
break.loop
|
||||
}
|
||||
if this.metadata_flavor <> base.MORE_INFORMATION__FLAVOR__METADATA_RAW_TRANSFORM {
|
||||
break.loop
|
||||
}
|
||||
|
||||
if this.metadata_is_zlib_compressed {
|
||||
if this.chunk_type == 'iCCP'le {
|
||||
io_limit (io: args.src, limit: this.chunk_length as base.u64) {
|
||||
r_mark = args.src.mark()
|
||||
zlib_status =? this.zlib.transform_io?(
|
||||
dst: args.dst, src: args.src, workbuf: this.util.empty_slice_u8())
|
||||
this.chunk_length ~sat-=
|
||||
(args.src.count_since(mark: r_mark) & 0xFFFF_FFFF) as base.u32
|
||||
}
|
||||
if this.metadata_is_zlib_compressed {
|
||||
if this.chunk_type == 'iCCP'le {
|
||||
io_limit (io: args.src, limit: this.chunk_length as base.u64) {
|
||||
r_mark = args.src.mark()
|
||||
zlib_status =? this.zlib.transform_io?(
|
||||
dst: args.dst, src: args.src, workbuf: this.util.empty_slice_u8())
|
||||
this.chunk_length ~sat-=
|
||||
(args.src.count_since(mark: r_mark) & 0xFFFF_FFFF) as base.u32
|
||||
}
|
||||
|
||||
if zlib_status.is_ok() {
|
||||
this.metadata_is_zlib_compressed = false
|
||||
break.loop
|
||||
} else if not zlib_status.is_suspension() {
|
||||
return zlib_status
|
||||
}
|
||||
yield? zlib_status
|
||||
if zlib_status.is_ok() {
|
||||
this.metadata_is_zlib_compressed = false
|
||||
break.loop
|
||||
} else if not zlib_status.is_suspension() {
|
||||
return zlib_status
|
||||
}
|
||||
yield? zlib_status
|
||||
|
||||
} else if this.chunk_type == 'iTXt'le {
|
||||
// TODO: verify uncompressed data is UTF-8.
|
||||
io_limit (io: args.src, limit: this.chunk_length as base.u64) {
|
||||
r_mark = args.src.mark()
|
||||
zlib_status =? this.zlib.transform_io?(
|
||||
dst: args.dst, src: args.src, workbuf: this.util.empty_slice_u8())
|
||||
this.chunk_length ~sat-=
|
||||
(args.src.count_since(mark: r_mark) & 0xFFFF_FFFF) as base.u32
|
||||
}
|
||||
} else if this.chunk_type == 'iTXt'le {
|
||||
// TODO: verify uncompressed data is UTF-8.
|
||||
io_limit (io: args.src, limit: this.chunk_length as base.u64) {
|
||||
r_mark = args.src.mark()
|
||||
zlib_status =? this.zlib.transform_io?(
|
||||
dst: args.dst, src: args.src, workbuf: this.util.empty_slice_u8())
|
||||
this.chunk_length ~sat-=
|
||||
(args.src.count_since(mark: r_mark) & 0xFFFF_FFFF) as base.u32
|
||||
}
|
||||
|
||||
if zlib_status.is_ok() {
|
||||
this.metadata_is_zlib_compressed = false
|
||||
break.loop
|
||||
} else if not zlib_status.is_suspension() {
|
||||
return zlib_status
|
||||
}
|
||||
yield? zlib_status
|
||||
if zlib_status.is_ok() {
|
||||
this.metadata_is_zlib_compressed = false
|
||||
break.loop
|
||||
} else if not zlib_status.is_suspension() {
|
||||
return zlib_status
|
||||
}
|
||||
yield? zlib_status
|
||||
|
||||
} else if this.chunk_type == 'zTXt'le {
|
||||
// Fill this.dst_palette, zlib-uncompressing producing Latin-1.
|
||||
if this.ztxt_ri == this.ztxt_wi {
|
||||
io_bind (io: w, data: this.dst_palette[..], history_position: this.ztxt_hist_pos) {
|
||||
io_limit (io: args.src, limit: this.chunk_length as base.u64) {
|
||||
w_mark = w.mark()
|
||||
r_mark = args.src.mark()
|
||||
zlib_status =? this.zlib.transform_io?(
|
||||
dst: w, src: args.src, workbuf: this.util.empty_slice_u8())
|
||||
this.chunk_length ~sat-=
|
||||
(args.src.count_since(mark: r_mark) & 0xFFFF_FFFF) as base.u32
|
||||
num_written = w.count_since(mark: w_mark)
|
||||
}
|
||||
}
|
||||
if num_written > 1024 {
|
||||
return "#internal error: inconsistent I/O"
|
||||
}
|
||||
this.ztxt_ri = 0
|
||||
this.ztxt_wi = num_written as base.u32
|
||||
this.ztxt_hist_pos ~sat+= num_written
|
||||
}
|
||||
} else if this.chunk_type == 'zTXt'le {
|
||||
// Fill this.dst_palette, zlib-uncompressing producing Latin-1.
|
||||
if this.ztxt_ri == this.ztxt_wi {
|
||||
io_bind (io: w, data: this.dst_palette[..], history_position: this.ztxt_hist_pos) {
|
||||
io_limit (io: args.src, limit: this.chunk_length as base.u64) {
|
||||
w_mark = w.mark()
|
||||
r_mark = args.src.mark()
|
||||
zlib_status =? this.zlib.transform_io?(
|
||||
dst: w, src: args.src, workbuf: this.util.empty_slice_u8())
|
||||
this.chunk_length ~sat-=
|
||||
(args.src.count_since(mark: r_mark) & 0xFFFF_FFFF) as base.u32
|
||||
num_written = w.count_since(mark: w_mark)
|
||||
}
|
||||
}
|
||||
if num_written > 1024 {
|
||||
return "#internal error: inconsistent I/O"
|
||||
}
|
||||
this.ztxt_ri = 0
|
||||
this.ztxt_wi = num_written as base.u32
|
||||
this.ztxt_hist_pos ~sat+= num_written
|
||||
}
|
||||
|
||||
// Drain this.dst_palette, converting from Latin-1 to UTF-8.
|
||||
while this.ztxt_ri < this.ztxt_wi {
|
||||
assert this.ztxt_ri < 1024 via "a < b: a < c; c <= b"(c: this.ztxt_wi)
|
||||
c2 = LATIN_1[this.dst_palette[this.ztxt_ri]]
|
||||
if c2 == 0 {
|
||||
return "#bad text chunk (not Latin-1)"
|
||||
} else if c2 <= 0x7F {
|
||||
if args.dst.length() <= 0 {
|
||||
yield? base."$short write"
|
||||
continue.loop
|
||||
}
|
||||
this.ztxt_ri += 1
|
||||
args.dst.write_u8_fast!(a: c2 as base.u8)
|
||||
} else {
|
||||
if args.dst.length() <= 1 {
|
||||
yield? base."$short write"
|
||||
continue.loop
|
||||
}
|
||||
this.ztxt_ri += 1
|
||||
args.dst.write_u16le_fast!(a: c2)
|
||||
}
|
||||
} endwhile
|
||||
// Drain this.dst_palette, converting from Latin-1 to UTF-8.
|
||||
while this.ztxt_ri < this.ztxt_wi {
|
||||
assert this.ztxt_ri < 1024 via "a < b: a < c; c <= b"(c: this.ztxt_wi)
|
||||
c2 = LATIN_1[this.dst_palette[this.ztxt_ri]]
|
||||
if c2 == 0 {
|
||||
return "#bad text chunk (not Latin-1)"
|
||||
} else if c2 <= 0x7F {
|
||||
if args.dst.length() <= 0 {
|
||||
yield? base."$short write"
|
||||
continue.loop
|
||||
}
|
||||
this.ztxt_ri += 1
|
||||
args.dst.write_u8_fast!(a: c2 as base.u8)
|
||||
} else {
|
||||
if args.dst.length() <= 1 {
|
||||
yield? base."$short write"
|
||||
continue.loop
|
||||
}
|
||||
this.ztxt_ri += 1
|
||||
args.dst.write_u16le_fast!(a: c2)
|
||||
}
|
||||
} endwhile
|
||||
|
||||
if zlib_status.is_ok() {
|
||||
this.metadata_is_zlib_compressed = false
|
||||
break.loop
|
||||
} else if not zlib_status.is_suspension() {
|
||||
return zlib_status
|
||||
} else if zlib_status <> base."$short write" {
|
||||
yield? zlib_status
|
||||
}
|
||||
if zlib_status.is_ok() {
|
||||
this.metadata_is_zlib_compressed = false
|
||||
break.loop
|
||||
} else if not zlib_status.is_suspension() {
|
||||
return zlib_status
|
||||
} else if zlib_status <> base."$short write" {
|
||||
yield? zlib_status
|
||||
}
|
||||
|
||||
} else {
|
||||
return "#internal error: inconsistent chunk type"
|
||||
}
|
||||
} else {
|
||||
return "#internal error: inconsistent chunk type"
|
||||
}
|
||||
|
||||
} else if (this.chunk_type == 'iTXt'le) and (this.metadata_fourcc == 'KVPV'be) {
|
||||
// iTXt value is UTF-8.
|
||||
//
|
||||
// TODO: verify data is UTF-8.
|
||||
while true {
|
||||
if this.chunk_length <= 0 {
|
||||
break.loop
|
||||
} else if args.src.length() <= 0 {
|
||||
yield? base."$short read"
|
||||
continue.loop
|
||||
} else if args.dst.length() <= 0 {
|
||||
yield? base."$short write"
|
||||
continue.loop
|
||||
}
|
||||
this.chunk_length -= 1
|
||||
c = args.src.peek_u8()
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
args.dst.write_u8_fast!(a: c)
|
||||
} endwhile
|
||||
} else if (this.chunk_type == 'iTXt'le) and (this.metadata_fourcc == 'KVPV'be) {
|
||||
// iTXt value is UTF-8.
|
||||
//
|
||||
// TODO: verify data is UTF-8.
|
||||
while true {
|
||||
if this.chunk_length <= 0 {
|
||||
break.loop
|
||||
} else if args.src.length() <= 0 {
|
||||
yield? base."$short read"
|
||||
continue.loop
|
||||
} else if args.dst.length() <= 0 {
|
||||
yield? base."$short write"
|
||||
continue.loop
|
||||
}
|
||||
this.chunk_length -= 1
|
||||
c = args.src.peek_u8()
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
args.dst.write_u8_fast!(a: c)
|
||||
} endwhile
|
||||
|
||||
} else {
|
||||
// Other uncompressed keys and values are Latin-1.
|
||||
while true {
|
||||
if this.chunk_length <= 0 {
|
||||
// Keys are NUL-terminated but values are not.
|
||||
if this.metadata_fourcc == 'KVPK'be {
|
||||
return "#bad chunk"
|
||||
}
|
||||
break.loop
|
||||
} else if args.src.length() <= 0 {
|
||||
yield? base."$short read"
|
||||
continue.loop
|
||||
}
|
||||
c = args.src.peek_u8()
|
||||
if c == 0 {
|
||||
this.chunk_length -= 1
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
break.loop
|
||||
}
|
||||
c2 = LATIN_1[c]
|
||||
if c2 == 0 {
|
||||
return "#bad text chunk (not Latin-1)"
|
||||
} else if c2 <= 0x7F {
|
||||
if args.dst.length() <= 0 {
|
||||
yield? base."$short write"
|
||||
continue.loop
|
||||
}
|
||||
this.chunk_length -= 1
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
args.dst.write_u8_fast!(a: c2 as base.u8)
|
||||
} else {
|
||||
if args.dst.length() <= 1 {
|
||||
yield? base."$short write"
|
||||
continue.loop
|
||||
}
|
||||
this.chunk_length -= 1
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
args.dst.write_u16le_fast!(a: c2)
|
||||
}
|
||||
} endwhile
|
||||
}
|
||||
} endwhile.loop
|
||||
} else {
|
||||
// Other uncompressed keys and values are Latin-1.
|
||||
while true {
|
||||
if this.chunk_length <= 0 {
|
||||
// Keys are NUL-terminated but values are not.
|
||||
if this.metadata_fourcc == 'KVPK'be {
|
||||
return "#bad chunk"
|
||||
}
|
||||
break.loop
|
||||
} else if args.src.length() <= 0 {
|
||||
yield? base."$short read"
|
||||
continue.loop
|
||||
}
|
||||
c = args.src.peek_u8()
|
||||
if c == 0 {
|
||||
this.chunk_length -= 1
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
break.loop
|
||||
}
|
||||
c2 = LATIN_1[c]
|
||||
if c2 == 0 {
|
||||
return "#bad text chunk (not Latin-1)"
|
||||
} else if c2 <= 0x7F {
|
||||
if args.dst.length() <= 0 {
|
||||
yield? base."$short write"
|
||||
continue.loop
|
||||
}
|
||||
this.chunk_length -= 1
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
args.dst.write_u8_fast!(a: c2 as base.u8)
|
||||
} else {
|
||||
if args.dst.length() <= 1 {
|
||||
yield? base."$short write"
|
||||
continue.loop
|
||||
}
|
||||
this.chunk_length -= 1
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
args.dst.write_u16le_fast!(a: c2)
|
||||
}
|
||||
} endwhile
|
||||
}
|
||||
} endwhile.loop
|
||||
|
||||
// Key-value pairs come in... pairs.
|
||||
if this.metadata_fourcc == 'KVPK'be {
|
||||
this.metadata_fourcc = 'KVPV'be
|
||||
if this.chunk_type == 'iTXt'le {
|
||||
// Compression flag, compression method.
|
||||
if this.chunk_length <= 1 {
|
||||
return "#bad chunk"
|
||||
}
|
||||
this.chunk_length -= 2
|
||||
c = args.src.read_u8?()
|
||||
if c == 0 {
|
||||
this.metadata_is_zlib_compressed = false
|
||||
} else if c == 1 {
|
||||
this.metadata_is_zlib_compressed = true
|
||||
} else {
|
||||
return "#bad chunk"
|
||||
}
|
||||
c = args.src.read_u8?()
|
||||
if (c <> 0) and this.metadata_is_zlib_compressed {
|
||||
return "#unsupported PNG compression method"
|
||||
}
|
||||
// Key-value pairs come in... pairs.
|
||||
if this.metadata_fourcc == 'KVPK'be {
|
||||
this.metadata_fourcc = 'KVPV'be
|
||||
if this.chunk_type == 'iTXt'le {
|
||||
// Compression flag, compression method.
|
||||
if this.chunk_length <= 1 {
|
||||
return "#bad chunk"
|
||||
}
|
||||
this.chunk_length -= 2
|
||||
c = args.src.read_u8?()
|
||||
if c == 0 {
|
||||
this.metadata_is_zlib_compressed = false
|
||||
} else if c == 1 {
|
||||
this.metadata_is_zlib_compressed = true
|
||||
} else {
|
||||
return "#bad chunk"
|
||||
}
|
||||
c = args.src.read_u8?()
|
||||
if (c <> 0) and this.metadata_is_zlib_compressed {
|
||||
return "#unsupported PNG compression method"
|
||||
}
|
||||
|
||||
// Skip the language tag and translated keyword: two iterations
|
||||
// looking for a NUL terminator.
|
||||
this.metadata_fourcc ~mod-= 2
|
||||
while this.metadata_fourcc <> 'KVPV'be {
|
||||
this.metadata_fourcc ~mod+= 1
|
||||
while true {
|
||||
if this.chunk_length <= 0 {
|
||||
return "#bad chunk"
|
||||
}
|
||||
this.chunk_length -= 1
|
||||
c = args.src.read_u8?()
|
||||
if c == 0 {
|
||||
break
|
||||
}
|
||||
} endwhile
|
||||
} endwhile
|
||||
// Skip the language tag and translated keyword: two iterations
|
||||
// looking for a NUL terminator.
|
||||
this.metadata_fourcc ~mod-= 2
|
||||
while this.metadata_fourcc <> 'KVPV'be {
|
||||
this.metadata_fourcc ~mod+= 1
|
||||
while true {
|
||||
if this.chunk_length <= 0 {
|
||||
return "#bad chunk"
|
||||
}
|
||||
this.chunk_length -= 1
|
||||
c = args.src.read_u8?()
|
||||
if c == 0 {
|
||||
break
|
||||
}
|
||||
} endwhile
|
||||
} endwhile
|
||||
|
||||
} else if this.chunk_type == 'zTXt'le {
|
||||
// Compression method.
|
||||
if this.chunk_length <= 0 {
|
||||
return "#bad chunk"
|
||||
}
|
||||
this.chunk_length -= 1
|
||||
c = args.src.read_u8?()
|
||||
if c <> 0 {
|
||||
return "#unsupported PNG compression method"
|
||||
}
|
||||
this.metadata_is_zlib_compressed = true
|
||||
}
|
||||
} else if this.chunk_type == 'zTXt'le {
|
||||
// Compression method.
|
||||
if this.chunk_length <= 0 {
|
||||
return "#bad chunk"
|
||||
}
|
||||
this.chunk_length -= 1
|
||||
c = args.src.read_u8?()
|
||||
if c <> 0 {
|
||||
return "#unsupported PNG compression method"
|
||||
}
|
||||
this.metadata_is_zlib_compressed = true
|
||||
}
|
||||
|
||||
this.call_sequence &= 0xEF
|
||||
return ok
|
||||
}
|
||||
break.goto_done
|
||||
}} endwhile.goto_done
|
||||
this.call_sequence &= 0xEF
|
||||
return ok
|
||||
}
|
||||
break.goto_done
|
||||
}} endwhile.goto_done
|
||||
|
||||
if this.chunk_length <> 0 {
|
||||
return "#bad chunk"
|
||||
}
|
||||
if this.chunk_length <> 0 {
|
||||
return "#bad chunk"
|
||||
}
|
||||
|
||||
// Skip the ancillary chunk's CRC-32 checksum.
|
||||
args.src.skip?(n: 4)
|
||||
// Skip the ancillary chunk's CRC-32 checksum.
|
||||
args.src.skip?(n: 4)
|
||||
|
||||
this.metadata_flavor = 0
|
||||
this.metadata_fourcc = 0
|
||||
this.metadata_x = 0
|
||||
this.metadata_y = 0
|
||||
this.metadata_z = 0
|
||||
this.metadata_flavor = 0
|
||||
this.metadata_fourcc = 0
|
||||
this.metadata_x = 0
|
||||
this.metadata_y = 0
|
||||
this.metadata_z = 0
|
||||
|
||||
this.call_sequence &= 0xEF
|
||||
return ok
|
||||
this.call_sequence &= 0xEF
|
||||
return ok
|
||||
}
|
||||
|
||||
pub func decoder.workbuf_len() base.range_ii_u64 {
|
||||
return this.util.make_range_ii_u64(
|
||||
min_incl: this.overall_workbuf_length,
|
||||
max_incl: this.overall_workbuf_length)
|
||||
return this.util.make_range_ii_u64(
|
||||
min_incl: this.overall_workbuf_length,
|
||||
max_incl: this.overall_workbuf_length)
|
||||
}
|
||||
@@ -13,95 +13,95 @@
|
||||
// limitations under the License.
|
||||
|
||||
pri func decoder.filter_and_swizzle!(dst: ptr base.pixel_buffer, workbuf: slice base.u8) base.status,
|
||||
choosy,
|
||||
choosy,
|
||||
{
|
||||
var dst_pixfmt : base.pixel_format
|
||||
var dst_bits_per_pixel : base.u32[..= 256]
|
||||
var dst_bytes_per_pixel : base.u64[..= 32]
|
||||
var dst_bytes_per_row0 : base.u64
|
||||
var dst_bytes_per_row1 : base.u64
|
||||
var dst_palette : slice base.u8
|
||||
var tab : table base.u8
|
||||
var dst_pixfmt : base.pixel_format
|
||||
var dst_bits_per_pixel : base.u32[..= 256]
|
||||
var dst_bytes_per_pixel : base.u64[..= 32]
|
||||
var dst_bytes_per_row0 : base.u64
|
||||
var dst_bytes_per_row1 : base.u64
|
||||
var dst_palette : slice base.u8
|
||||
var tab : table base.u8
|
||||
|
||||
var y : base.u32
|
||||
var dst : slice base.u8
|
||||
var filter : base.u8
|
||||
var curr_row : slice base.u8
|
||||
var prev_row : slice base.u8
|
||||
var y : base.u32
|
||||
var dst : slice base.u8
|
||||
var filter : base.u8
|
||||
var curr_row : slice base.u8
|
||||
var prev_row : slice base.u8
|
||||
|
||||
// TODO: the dst_pixfmt variable shouldn't be necessary. We should be able
|
||||
// to chain the two calls: "args.dst.pixel_format().bits_per_pixel()".
|
||||
dst_pixfmt = args.dst.pixel_format()
|
||||
dst_bits_per_pixel = dst_pixfmt.bits_per_pixel()
|
||||
if (dst_bits_per_pixel & 7) <> 0 {
|
||||
return base."#unsupported option"
|
||||
}
|
||||
dst_bytes_per_pixel = (dst_bits_per_pixel / 8) as base.u64
|
||||
dst_bytes_per_row0 = (this.frame_rect_x0 as base.u64) * dst_bytes_per_pixel
|
||||
dst_bytes_per_row1 = (this.frame_rect_x1 as base.u64) * dst_bytes_per_pixel
|
||||
dst_palette = args.dst.palette_or_else(fallback: this.dst_palette[..])
|
||||
tab = args.dst.plane(p: 0)
|
||||
// TODO: the dst_pixfmt variable shouldn't be necessary. We should be able
|
||||
// to chain the two calls: "args.dst.pixel_format().bits_per_pixel()".
|
||||
dst_pixfmt = args.dst.pixel_format()
|
||||
dst_bits_per_pixel = dst_pixfmt.bits_per_pixel()
|
||||
if (dst_bits_per_pixel & 7) <> 0 {
|
||||
return base."#unsupported option"
|
||||
}
|
||||
dst_bytes_per_pixel = (dst_bits_per_pixel / 8) as base.u64
|
||||
dst_bytes_per_row0 = (this.frame_rect_x0 as base.u64) * dst_bytes_per_pixel
|
||||
dst_bytes_per_row1 = (this.frame_rect_x1 as base.u64) * dst_bytes_per_pixel
|
||||
dst_palette = args.dst.palette_or_else(fallback: this.dst_palette[..])
|
||||
tab = args.dst.plane(p: 0)
|
||||
|
||||
if dst_bytes_per_row1 < tab.width() {
|
||||
tab = tab.subtable(
|
||||
min_incl_x: 0,
|
||||
min_incl_y: 0,
|
||||
max_incl_x: dst_bytes_per_row1,
|
||||
max_incl_y: tab.height())
|
||||
}
|
||||
if dst_bytes_per_row1 < tab.width() {
|
||||
tab = tab.subtable(
|
||||
min_incl_x: 0,
|
||||
min_incl_y: 0,
|
||||
max_incl_x: dst_bytes_per_row1,
|
||||
max_incl_y: tab.height())
|
||||
}
|
||||
|
||||
if dst_bytes_per_row0 < tab.width() {
|
||||
tab = tab.subtable(
|
||||
min_incl_x: dst_bytes_per_row0,
|
||||
min_incl_y: 0,
|
||||
max_incl_x: tab.width(),
|
||||
max_incl_y: tab.height())
|
||||
} else {
|
||||
tab = tab.subtable(
|
||||
min_incl_x: 0,
|
||||
min_incl_y: 0,
|
||||
max_incl_x: 0,
|
||||
max_incl_y: 0)
|
||||
}
|
||||
if dst_bytes_per_row0 < tab.width() {
|
||||
tab = tab.subtable(
|
||||
min_incl_x: dst_bytes_per_row0,
|
||||
min_incl_y: 0,
|
||||
max_incl_x: tab.width(),
|
||||
max_incl_y: tab.height())
|
||||
} else {
|
||||
tab = tab.subtable(
|
||||
min_incl_x: 0,
|
||||
min_incl_y: 0,
|
||||
max_incl_x: 0,
|
||||
max_incl_y: 0)
|
||||
}
|
||||
|
||||
y = this.frame_rect_y0
|
||||
while y < this.frame_rect_y1 {
|
||||
assert y < 0x00FF_FFFF via "a < b: a < c; c <= b"(c: this.frame_rect_y1)
|
||||
dst = tab.row_u32(y: y)
|
||||
y = this.frame_rect_y0
|
||||
while y < this.frame_rect_y1 {
|
||||
assert y < 0x00FF_FFFF via "a < b: a < c; c <= b"(c: this.frame_rect_y1)
|
||||
dst = tab.row_u32(y: y)
|
||||
|
||||
if 1 > args.workbuf.length() {
|
||||
return "#internal error: inconsistent workbuf length"
|
||||
}
|
||||
filter = args.workbuf[0]
|
||||
args.workbuf = args.workbuf[1 ..]
|
||||
if this.pass_bytes_per_row > args.workbuf.length() {
|
||||
return "#internal error: inconsistent workbuf length"
|
||||
}
|
||||
curr_row = args.workbuf[.. this.pass_bytes_per_row]
|
||||
args.workbuf = args.workbuf[this.pass_bytes_per_row ..]
|
||||
if 1 > args.workbuf.length() {
|
||||
return "#internal error: inconsistent workbuf length"
|
||||
}
|
||||
filter = args.workbuf[0]
|
||||
args.workbuf = args.workbuf[1 ..]
|
||||
if this.pass_bytes_per_row > args.workbuf.length() {
|
||||
return "#internal error: inconsistent workbuf length"
|
||||
}
|
||||
curr_row = args.workbuf[.. this.pass_bytes_per_row]
|
||||
args.workbuf = args.workbuf[this.pass_bytes_per_row ..]
|
||||
|
||||
if filter == 0 {
|
||||
// No-op.
|
||||
} else if filter == 1 {
|
||||
this.filter_1!(curr: curr_row)
|
||||
} else if filter == 2 {
|
||||
this.filter_2!(curr: curr_row, prev: prev_row)
|
||||
} else if filter == 3 {
|
||||
this.filter_3!(curr: curr_row, prev: prev_row)
|
||||
} else if filter == 4 {
|
||||
this.filter_4!(curr: curr_row, prev: prev_row)
|
||||
} else {
|
||||
return "#bad filter"
|
||||
}
|
||||
if filter == 0 {
|
||||
// No-op.
|
||||
} else if filter == 1 {
|
||||
this.filter_1!(curr: curr_row)
|
||||
} else if filter == 2 {
|
||||
this.filter_2!(curr: curr_row, prev: prev_row)
|
||||
} else if filter == 3 {
|
||||
this.filter_3!(curr: curr_row, prev: prev_row)
|
||||
} else if filter == 4 {
|
||||
this.filter_4!(curr: curr_row, prev: prev_row)
|
||||
} else {
|
||||
return "#bad filter"
|
||||
}
|
||||
|
||||
this.swizzler.swizzle_interleaved_from_slice!(
|
||||
dst: dst,
|
||||
dst_palette: dst_palette,
|
||||
src: curr_row)
|
||||
this.swizzler.swizzle_interleaved_from_slice!(
|
||||
dst: dst,
|
||||
dst_palette: dst_palette,
|
||||
src: curr_row)
|
||||
|
||||
prev_row = curr_row
|
||||
y += 1
|
||||
} endwhile
|
||||
prev_row = curr_row
|
||||
y += 1
|
||||
} endwhile
|
||||
|
||||
return ok
|
||||
return ok
|
||||
}
|
||||
+304
-304
@@ -13,330 +13,330 @@
|
||||
// limitations under the License.
|
||||
|
||||
pri func decoder.filter_and_swizzle_tricky!(dst: ptr base.pixel_buffer, workbuf: slice base.u8) base.status {
|
||||
var dst_pixfmt : base.pixel_format
|
||||
var dst_bits_per_pixel : base.u32[..= 256]
|
||||
var dst_bytes_per_pixel : base.u64[..= 32]
|
||||
var dst_bytes_per_row1 : base.u64
|
||||
var dst_palette : slice base.u8
|
||||
var tab : table base.u8
|
||||
var dst_pixfmt : base.pixel_format
|
||||
var dst_bits_per_pixel : base.u32[..= 256]
|
||||
var dst_bytes_per_pixel : base.u64[..= 32]
|
||||
var dst_bytes_per_row1 : base.u64
|
||||
var dst_palette : slice base.u8
|
||||
var tab : table base.u8
|
||||
|
||||
var src_bytes_per_pixel : base.u64[..= 8]
|
||||
var src_bytes_per_pixel : base.u64[..= 8]
|
||||
|
||||
var x : base.u32
|
||||
var y : base.u32
|
||||
var i : base.u64[..= 0x1FFF_FFC0]
|
||||
var dst : slice base.u8
|
||||
var filter : base.u8
|
||||
var s : slice base.u8
|
||||
var curr_row : slice base.u8
|
||||
var prev_row : slice base.u8
|
||||
var x : base.u32
|
||||
var y : base.u32
|
||||
var i : base.u64[..= 0x1FFF_FFC0]
|
||||
var dst : slice base.u8
|
||||
var filter : base.u8
|
||||
var s : slice base.u8
|
||||
var curr_row : slice base.u8
|
||||
var prev_row : slice base.u8
|
||||
|
||||
var bits_unpacked : array[8] base.u8
|
||||
var bits_packed : base.u8
|
||||
var packs_remaining : base.u8
|
||||
var multiplier : base.u8
|
||||
var shift : base.u8[..= 7]
|
||||
var bits_unpacked : array[8] base.u8
|
||||
var bits_packed : base.u8
|
||||
var packs_remaining : base.u8
|
||||
var multiplier : base.u8
|
||||
var shift : base.u8[..= 7]
|
||||
|
||||
// TODO: the dst_pixfmt variable shouldn't be necessary. We should be able
|
||||
// to chain the two calls: "args.dst.pixel_format().bits_per_pixel()".
|
||||
dst_pixfmt = args.dst.pixel_format()
|
||||
dst_bits_per_pixel = dst_pixfmt.bits_per_pixel()
|
||||
if (dst_bits_per_pixel & 7) <> 0 {
|
||||
return base."#unsupported option"
|
||||
}
|
||||
dst_bytes_per_pixel = (dst_bits_per_pixel / 8) as base.u64
|
||||
dst_bytes_per_row1 = (this.frame_rect_x1 as base.u64) * dst_bytes_per_pixel
|
||||
dst_palette = args.dst.palette_or_else(fallback: this.dst_palette[..])
|
||||
tab = args.dst.plane(p: 0)
|
||||
// TODO: the dst_pixfmt variable shouldn't be necessary. We should be able
|
||||
// to chain the two calls: "args.dst.pixel_format().bits_per_pixel()".
|
||||
dst_pixfmt = args.dst.pixel_format()
|
||||
dst_bits_per_pixel = dst_pixfmt.bits_per_pixel()
|
||||
if (dst_bits_per_pixel & 7) <> 0 {
|
||||
return base."#unsupported option"
|
||||
}
|
||||
dst_bytes_per_pixel = (dst_bits_per_pixel / 8) as base.u64
|
||||
dst_bytes_per_row1 = (this.frame_rect_x1 as base.u64) * dst_bytes_per_pixel
|
||||
dst_palette = args.dst.palette_or_else(fallback: this.dst_palette[..])
|
||||
tab = args.dst.plane(p: 0)
|
||||
|
||||
src_bytes_per_pixel = 1
|
||||
if this.depth >= 8 {
|
||||
src_bytes_per_pixel = (NUM_CHANNELS[this.color_type] as base.u64) *
|
||||
((this.depth >> 3) as base.u64)
|
||||
}
|
||||
src_bytes_per_pixel = 1
|
||||
if this.depth >= 8 {
|
||||
src_bytes_per_pixel = (NUM_CHANNELS[this.color_type] as base.u64) *
|
||||
((this.depth >> 3) as base.u64)
|
||||
}
|
||||
|
||||
if (this.chunk_type_array[0] == 'I') {
|
||||
y = INTERLACING[this.interlace_pass][5] as base.u32
|
||||
} else {
|
||||
y = this.frame_rect_y0
|
||||
}
|
||||
while y < this.frame_rect_y1 {
|
||||
assert y < 0x00FF_FFFF via "a < b: a < c; c <= b"(c: this.frame_rect_y1)
|
||||
dst = tab.row_u32(y: y)
|
||||
if dst_bytes_per_row1 < dst.length() {
|
||||
dst = dst[.. dst_bytes_per_row1]
|
||||
}
|
||||
if (this.chunk_type_array[0] == 'I') {
|
||||
y = INTERLACING[this.interlace_pass][5] as base.u32
|
||||
} else {
|
||||
y = this.frame_rect_y0
|
||||
}
|
||||
while y < this.frame_rect_y1 {
|
||||
assert y < 0x00FF_FFFF via "a < b: a < c; c <= b"(c: this.frame_rect_y1)
|
||||
dst = tab.row_u32(y: y)
|
||||
if dst_bytes_per_row1 < dst.length() {
|
||||
dst = dst[.. dst_bytes_per_row1]
|
||||
}
|
||||
|
||||
if 1 > args.workbuf.length() {
|
||||
return "#internal error: inconsistent workbuf length"
|
||||
}
|
||||
filter = args.workbuf[0]
|
||||
args.workbuf = args.workbuf[1 ..]
|
||||
if this.pass_bytes_per_row > args.workbuf.length() {
|
||||
return "#internal error: inconsistent workbuf length"
|
||||
}
|
||||
curr_row = args.workbuf[.. this.pass_bytes_per_row]
|
||||
args.workbuf = args.workbuf[this.pass_bytes_per_row ..]
|
||||
if 1 > args.workbuf.length() {
|
||||
return "#internal error: inconsistent workbuf length"
|
||||
}
|
||||
filter = args.workbuf[0]
|
||||
args.workbuf = args.workbuf[1 ..]
|
||||
if this.pass_bytes_per_row > args.workbuf.length() {
|
||||
return "#internal error: inconsistent workbuf length"
|
||||
}
|
||||
curr_row = args.workbuf[.. this.pass_bytes_per_row]
|
||||
args.workbuf = args.workbuf[this.pass_bytes_per_row ..]
|
||||
|
||||
if filter == 0 {
|
||||
// No-op.
|
||||
} else if filter == 1 {
|
||||
this.filter_1!(curr: curr_row)
|
||||
} else if filter == 2 {
|
||||
this.filter_2!(curr: curr_row, prev: prev_row)
|
||||
} else if filter == 3 {
|
||||
this.filter_3!(curr: curr_row, prev: prev_row)
|
||||
} else if filter == 4 {
|
||||
this.filter_4!(curr: curr_row, prev: prev_row)
|
||||
} else {
|
||||
return "#bad filter"
|
||||
}
|
||||
if filter == 0 {
|
||||
// No-op.
|
||||
} else if filter == 1 {
|
||||
this.filter_1!(curr: curr_row)
|
||||
} else if filter == 2 {
|
||||
this.filter_2!(curr: curr_row, prev: prev_row)
|
||||
} else if filter == 3 {
|
||||
this.filter_3!(curr: curr_row, prev: prev_row)
|
||||
} else if filter == 4 {
|
||||
this.filter_4!(curr: curr_row, prev: prev_row)
|
||||
} else {
|
||||
return "#bad filter"
|
||||
}
|
||||
|
||||
s = curr_row
|
||||
if (this.chunk_type_array[0] == 'I') {
|
||||
x = INTERLACING[this.interlace_pass][2] as base.u32
|
||||
} else {
|
||||
x = this.frame_rect_x0
|
||||
}
|
||||
if this.depth == 8 {
|
||||
while x < this.frame_rect_x1,
|
||||
inv y < 0x00FF_FFFF,
|
||||
{
|
||||
assert x < 0x00FF_FFFF via "a < b: a < c; c <= b"(c: this.frame_rect_x1)
|
||||
i = (x as base.u64) * dst_bytes_per_pixel
|
||||
if i <= dst.length() {
|
||||
if this.color_type == 4 {
|
||||
if 2 <= s.length() {
|
||||
bits_unpacked[0] = s[0]
|
||||
bits_unpacked[1] = s[0]
|
||||
bits_unpacked[2] = s[0]
|
||||
bits_unpacked[3] = s[1]
|
||||
s = s[2 ..]
|
||||
this.swizzler.swizzle_interleaved_from_slice!(
|
||||
dst: dst[i ..],
|
||||
dst_palette: dst_palette,
|
||||
src: bits_unpacked[.. 4])
|
||||
}
|
||||
s = curr_row
|
||||
if (this.chunk_type_array[0] == 'I') {
|
||||
x = INTERLACING[this.interlace_pass][2] as base.u32
|
||||
} else {
|
||||
x = this.frame_rect_x0
|
||||
}
|
||||
if this.depth == 8 {
|
||||
while x < this.frame_rect_x1,
|
||||
inv y < 0x00FF_FFFF,
|
||||
{
|
||||
assert x < 0x00FF_FFFF via "a < b: a < c; c <= b"(c: this.frame_rect_x1)
|
||||
i = (x as base.u64) * dst_bytes_per_pixel
|
||||
if i <= dst.length() {
|
||||
if this.color_type == 4 {
|
||||
if 2 <= s.length() {
|
||||
bits_unpacked[0] = s[0]
|
||||
bits_unpacked[1] = s[0]
|
||||
bits_unpacked[2] = s[0]
|
||||
bits_unpacked[3] = s[1]
|
||||
s = s[2 ..]
|
||||
this.swizzler.swizzle_interleaved_from_slice!(
|
||||
dst: dst[i ..],
|
||||
dst_palette: dst_palette,
|
||||
src: bits_unpacked[.. 4])
|
||||
}
|
||||
|
||||
} else if ((this.remap_transparency & 0xFFFF_FFFF) as base.u32) <> 0 {
|
||||
if this.color_type == 0 {
|
||||
if 1 <= s.length() {
|
||||
bits_unpacked[0] = s[0]
|
||||
bits_unpacked[1] = s[0]
|
||||
bits_unpacked[2] = s[0]
|
||||
bits_unpacked[3] = 0xFF
|
||||
s = s[1 ..]
|
||||
if ((this.remap_transparency & 0xFFFF_FFFF) as base.u32) == (
|
||||
((bits_unpacked[0] as base.u32) << 0) |
|
||||
((bits_unpacked[1] as base.u32) << 8) |
|
||||
((bits_unpacked[2] as base.u32) << 16) |
|
||||
((bits_unpacked[3] as base.u32) << 24)) {
|
||||
bits_unpacked[0] = 0
|
||||
bits_unpacked[1] = 0
|
||||
bits_unpacked[2] = 0
|
||||
bits_unpacked[3] = 0
|
||||
}
|
||||
this.swizzler.swizzle_interleaved_from_slice!(
|
||||
dst: dst[i ..],
|
||||
dst_palette: dst_palette,
|
||||
src: bits_unpacked[.. 4])
|
||||
}
|
||||
} else {
|
||||
if 3 <= s.length() {
|
||||
bits_unpacked[0] = s[2]
|
||||
bits_unpacked[1] = s[1]
|
||||
bits_unpacked[2] = s[0]
|
||||
bits_unpacked[3] = 0xFF
|
||||
s = s[3 ..]
|
||||
if ((this.remap_transparency & 0xFFFF_FFFF) as base.u32) == (
|
||||
((bits_unpacked[0] as base.u32) << 0) |
|
||||
((bits_unpacked[1] as base.u32) << 8) |
|
||||
((bits_unpacked[2] as base.u32) << 16) |
|
||||
((bits_unpacked[3] as base.u32) << 24)) {
|
||||
bits_unpacked[0] = 0
|
||||
bits_unpacked[1] = 0
|
||||
bits_unpacked[2] = 0
|
||||
bits_unpacked[3] = 0
|
||||
}
|
||||
this.swizzler.swizzle_interleaved_from_slice!(
|
||||
dst: dst[i ..],
|
||||
dst_palette: dst_palette,
|
||||
src: bits_unpacked[.. 4])
|
||||
}
|
||||
}
|
||||
} else if ((this.remap_transparency & 0xFFFF_FFFF) as base.u32) <> 0 {
|
||||
if this.color_type == 0 {
|
||||
if 1 <= s.length() {
|
||||
bits_unpacked[0] = s[0]
|
||||
bits_unpacked[1] = s[0]
|
||||
bits_unpacked[2] = s[0]
|
||||
bits_unpacked[3] = 0xFF
|
||||
s = s[1 ..]
|
||||
if ((this.remap_transparency & 0xFFFF_FFFF) as base.u32) == (
|
||||
((bits_unpacked[0] as base.u32) << 0) |
|
||||
((bits_unpacked[1] as base.u32) << 8) |
|
||||
((bits_unpacked[2] as base.u32) << 16) |
|
||||
((bits_unpacked[3] as base.u32) << 24)) {
|
||||
bits_unpacked[0] = 0
|
||||
bits_unpacked[1] = 0
|
||||
bits_unpacked[2] = 0
|
||||
bits_unpacked[3] = 0
|
||||
}
|
||||
this.swizzler.swizzle_interleaved_from_slice!(
|
||||
dst: dst[i ..],
|
||||
dst_palette: dst_palette,
|
||||
src: bits_unpacked[.. 4])
|
||||
}
|
||||
} else {
|
||||
if 3 <= s.length() {
|
||||
bits_unpacked[0] = s[2]
|
||||
bits_unpacked[1] = s[1]
|
||||
bits_unpacked[2] = s[0]
|
||||
bits_unpacked[3] = 0xFF
|
||||
s = s[3 ..]
|
||||
if ((this.remap_transparency & 0xFFFF_FFFF) as base.u32) == (
|
||||
((bits_unpacked[0] as base.u32) << 0) |
|
||||
((bits_unpacked[1] as base.u32) << 8) |
|
||||
((bits_unpacked[2] as base.u32) << 16) |
|
||||
((bits_unpacked[3] as base.u32) << 24)) {
|
||||
bits_unpacked[0] = 0
|
||||
bits_unpacked[1] = 0
|
||||
bits_unpacked[2] = 0
|
||||
bits_unpacked[3] = 0
|
||||
}
|
||||
this.swizzler.swizzle_interleaved_from_slice!(
|
||||
dst: dst[i ..],
|
||||
dst_palette: dst_palette,
|
||||
src: bits_unpacked[.. 4])
|
||||
}
|
||||
}
|
||||
|
||||
} else if src_bytes_per_pixel <= s.length() {
|
||||
this.swizzler.swizzle_interleaved_from_slice!(
|
||||
dst: dst[i ..],
|
||||
dst_palette: dst_palette,
|
||||
src: s[.. src_bytes_per_pixel])
|
||||
s = s[src_bytes_per_pixel ..]
|
||||
}
|
||||
}
|
||||
x += (1 as base.u32) << INTERLACING[this.interlace_pass][0]
|
||||
} endwhile
|
||||
} else if src_bytes_per_pixel <= s.length() {
|
||||
this.swizzler.swizzle_interleaved_from_slice!(
|
||||
dst: dst[i ..],
|
||||
dst_palette: dst_palette,
|
||||
src: s[.. src_bytes_per_pixel])
|
||||
s = s[src_bytes_per_pixel ..]
|
||||
}
|
||||
}
|
||||
x += (1 as base.u32) << INTERLACING[this.interlace_pass][0]
|
||||
} endwhile
|
||||
|
||||
} else if this.depth < 8 {
|
||||
multiplier = 1
|
||||
if this.color_type == 0 { // Color type 0 means base.PIXEL_FORMAT__Y.
|
||||
multiplier = LOW_BIT_DEPTH_MULTIPLIERS[this.depth]
|
||||
}
|
||||
shift = (8 - this.depth) & 7
|
||||
packs_remaining = 0
|
||||
} else if this.depth < 8 {
|
||||
multiplier = 1
|
||||
if this.color_type == 0 { // Color type 0 means base.PIXEL_FORMAT__Y.
|
||||
multiplier = LOW_BIT_DEPTH_MULTIPLIERS[this.depth]
|
||||
}
|
||||
shift = (8 - this.depth) & 7
|
||||
packs_remaining = 0
|
||||
|
||||
while x < this.frame_rect_x1,
|
||||
inv y < 0x00FF_FFFF,
|
||||
inv this.depth < 8,
|
||||
{
|
||||
assert x < 0x00FF_FFFF via "a < b: a < c; c <= b"(c: this.frame_rect_x1)
|
||||
i = (x as base.u64) * dst_bytes_per_pixel
|
||||
if i <= dst.length() {
|
||||
if (packs_remaining == 0) and (1 <= s.length()) {
|
||||
packs_remaining = LOW_BIT_DEPTH_NUM_PACKS[this.depth]
|
||||
bits_packed = s[0]
|
||||
s = s[1 ..]
|
||||
}
|
||||
bits_unpacked[0] = (bits_packed >> shift) ~mod* multiplier
|
||||
bits_packed = bits_packed ~mod<< this.depth
|
||||
packs_remaining = packs_remaining ~mod- 1
|
||||
while x < this.frame_rect_x1,
|
||||
inv y < 0x00FF_FFFF,
|
||||
inv this.depth < 8,
|
||||
{
|
||||
assert x < 0x00FF_FFFF via "a < b: a < c; c <= b"(c: this.frame_rect_x1)
|
||||
i = (x as base.u64) * dst_bytes_per_pixel
|
||||
if i <= dst.length() {
|
||||
if (packs_remaining == 0) and (1 <= s.length()) {
|
||||
packs_remaining = LOW_BIT_DEPTH_NUM_PACKS[this.depth]
|
||||
bits_packed = s[0]
|
||||
s = s[1 ..]
|
||||
}
|
||||
bits_unpacked[0] = (bits_packed >> shift) ~mod* multiplier
|
||||
bits_packed = bits_packed ~mod<< this.depth
|
||||
packs_remaining = packs_remaining ~mod- 1
|
||||
|
||||
if ((this.remap_transparency & 0xFFFF_FFFF) as base.u32) <> 0 {
|
||||
bits_unpacked[1] = bits_unpacked[0]
|
||||
bits_unpacked[2] = bits_unpacked[0]
|
||||
bits_unpacked[3] = 0xFF
|
||||
if ((this.remap_transparency & 0xFFFF_FFFF) as base.u32) == (
|
||||
((bits_unpacked[0] as base.u32) << 0) |
|
||||
((bits_unpacked[1] as base.u32) << 8) |
|
||||
((bits_unpacked[2] as base.u32) << 16) |
|
||||
((bits_unpacked[3] as base.u32) << 24)) {
|
||||
bits_unpacked[0] = 0
|
||||
bits_unpacked[1] = 0
|
||||
bits_unpacked[2] = 0
|
||||
bits_unpacked[3] = 0
|
||||
}
|
||||
this.swizzler.swizzle_interleaved_from_slice!(
|
||||
dst: dst[i ..],
|
||||
dst_palette: dst_palette,
|
||||
src: bits_unpacked[.. 4])
|
||||
if ((this.remap_transparency & 0xFFFF_FFFF) as base.u32) <> 0 {
|
||||
bits_unpacked[1] = bits_unpacked[0]
|
||||
bits_unpacked[2] = bits_unpacked[0]
|
||||
bits_unpacked[3] = 0xFF
|
||||
if ((this.remap_transparency & 0xFFFF_FFFF) as base.u32) == (
|
||||
((bits_unpacked[0] as base.u32) << 0) |
|
||||
((bits_unpacked[1] as base.u32) << 8) |
|
||||
((bits_unpacked[2] as base.u32) << 16) |
|
||||
((bits_unpacked[3] as base.u32) << 24)) {
|
||||
bits_unpacked[0] = 0
|
||||
bits_unpacked[1] = 0
|
||||
bits_unpacked[2] = 0
|
||||
bits_unpacked[3] = 0
|
||||
}
|
||||
this.swizzler.swizzle_interleaved_from_slice!(
|
||||
dst: dst[i ..],
|
||||
dst_palette: dst_palette,
|
||||
src: bits_unpacked[.. 4])
|
||||
|
||||
} else {
|
||||
this.swizzler.swizzle_interleaved_from_slice!(
|
||||
dst: dst[i ..],
|
||||
dst_palette: dst_palette,
|
||||
src: bits_unpacked[.. 1])
|
||||
}
|
||||
}
|
||||
x += (1 as base.u32) << INTERLACING[this.interlace_pass][0]
|
||||
} endwhile
|
||||
} else {
|
||||
this.swizzler.swizzle_interleaved_from_slice!(
|
||||
dst: dst[i ..],
|
||||
dst_palette: dst_palette,
|
||||
src: bits_unpacked[.. 1])
|
||||
}
|
||||
}
|
||||
x += (1 as base.u32) << INTERLACING[this.interlace_pass][0]
|
||||
} endwhile
|
||||
|
||||
} else {
|
||||
while x < this.frame_rect_x1,
|
||||
inv y < 0x00FF_FFFF,
|
||||
{
|
||||
assert x < 0x00FF_FFFF via "a < b: a < c; c <= b"(c: this.frame_rect_x1)
|
||||
i = (x as base.u64) * dst_bytes_per_pixel
|
||||
if i <= dst.length() {
|
||||
if this.color_type == 0 {
|
||||
if 2 <= s.length() {
|
||||
bits_unpacked[0] = s[1]
|
||||
bits_unpacked[1] = s[0]
|
||||
bits_unpacked[2] = s[1]
|
||||
bits_unpacked[3] = s[0]
|
||||
bits_unpacked[4] = s[1]
|
||||
bits_unpacked[5] = s[0]
|
||||
bits_unpacked[6] = 0xFF
|
||||
bits_unpacked[7] = 0xFF
|
||||
s = s[2 ..]
|
||||
if this.remap_transparency == (
|
||||
((bits_unpacked[0] as base.u64) << 0) |
|
||||
((bits_unpacked[1] as base.u64) << 8) |
|
||||
((bits_unpacked[2] as base.u64) << 16) |
|
||||
((bits_unpacked[3] as base.u64) << 24) |
|
||||
((bits_unpacked[4] as base.u64) << 32) |
|
||||
((bits_unpacked[5] as base.u64) << 40) |
|
||||
((bits_unpacked[6] as base.u64) << 48) |
|
||||
((bits_unpacked[7] as base.u64) << 56)) {
|
||||
bits_unpacked[0] = 0
|
||||
bits_unpacked[1] = 0
|
||||
bits_unpacked[2] = 0
|
||||
bits_unpacked[3] = 0
|
||||
bits_unpacked[4] = 0
|
||||
bits_unpacked[5] = 0
|
||||
bits_unpacked[6] = 0
|
||||
bits_unpacked[7] = 0
|
||||
}
|
||||
}
|
||||
} else {
|
||||
while x < this.frame_rect_x1,
|
||||
inv y < 0x00FF_FFFF,
|
||||
{
|
||||
assert x < 0x00FF_FFFF via "a < b: a < c; c <= b"(c: this.frame_rect_x1)
|
||||
i = (x as base.u64) * dst_bytes_per_pixel
|
||||
if i <= dst.length() {
|
||||
if this.color_type == 0 {
|
||||
if 2 <= s.length() {
|
||||
bits_unpacked[0] = s[1]
|
||||
bits_unpacked[1] = s[0]
|
||||
bits_unpacked[2] = s[1]
|
||||
bits_unpacked[3] = s[0]
|
||||
bits_unpacked[4] = s[1]
|
||||
bits_unpacked[5] = s[0]
|
||||
bits_unpacked[6] = 0xFF
|
||||
bits_unpacked[7] = 0xFF
|
||||
s = s[2 ..]
|
||||
if this.remap_transparency == (
|
||||
((bits_unpacked[0] as base.u64) << 0) |
|
||||
((bits_unpacked[1] as base.u64) << 8) |
|
||||
((bits_unpacked[2] as base.u64) << 16) |
|
||||
((bits_unpacked[3] as base.u64) << 24) |
|
||||
((bits_unpacked[4] as base.u64) << 32) |
|
||||
((bits_unpacked[5] as base.u64) << 40) |
|
||||
((bits_unpacked[6] as base.u64) << 48) |
|
||||
((bits_unpacked[7] as base.u64) << 56)) {
|
||||
bits_unpacked[0] = 0
|
||||
bits_unpacked[1] = 0
|
||||
bits_unpacked[2] = 0
|
||||
bits_unpacked[3] = 0
|
||||
bits_unpacked[4] = 0
|
||||
bits_unpacked[5] = 0
|
||||
bits_unpacked[6] = 0
|
||||
bits_unpacked[7] = 0
|
||||
}
|
||||
}
|
||||
|
||||
} else if this.color_type == 2 {
|
||||
if 6 <= s.length() {
|
||||
bits_unpacked[0] = s[5]
|
||||
bits_unpacked[1] = s[4]
|
||||
bits_unpacked[2] = s[3]
|
||||
bits_unpacked[3] = s[2]
|
||||
bits_unpacked[4] = s[1]
|
||||
bits_unpacked[5] = s[0]
|
||||
bits_unpacked[6] = 0xFF
|
||||
bits_unpacked[7] = 0xFF
|
||||
s = s[6 ..]
|
||||
if this.remap_transparency == (
|
||||
((bits_unpacked[0] as base.u64) << 0) |
|
||||
((bits_unpacked[1] as base.u64) << 8) |
|
||||
((bits_unpacked[2] as base.u64) << 16) |
|
||||
((bits_unpacked[3] as base.u64) << 24) |
|
||||
((bits_unpacked[4] as base.u64) << 32) |
|
||||
((bits_unpacked[5] as base.u64) << 40) |
|
||||
((bits_unpacked[6] as base.u64) << 48) |
|
||||
((bits_unpacked[7] as base.u64) << 56)) {
|
||||
bits_unpacked[0] = 0
|
||||
bits_unpacked[1] = 0
|
||||
bits_unpacked[2] = 0
|
||||
bits_unpacked[3] = 0
|
||||
bits_unpacked[4] = 0
|
||||
bits_unpacked[5] = 0
|
||||
bits_unpacked[6] = 0
|
||||
bits_unpacked[7] = 0
|
||||
}
|
||||
}
|
||||
} else if this.color_type == 2 {
|
||||
if 6 <= s.length() {
|
||||
bits_unpacked[0] = s[5]
|
||||
bits_unpacked[1] = s[4]
|
||||
bits_unpacked[2] = s[3]
|
||||
bits_unpacked[3] = s[2]
|
||||
bits_unpacked[4] = s[1]
|
||||
bits_unpacked[5] = s[0]
|
||||
bits_unpacked[6] = 0xFF
|
||||
bits_unpacked[7] = 0xFF
|
||||
s = s[6 ..]
|
||||
if this.remap_transparency == (
|
||||
((bits_unpacked[0] as base.u64) << 0) |
|
||||
((bits_unpacked[1] as base.u64) << 8) |
|
||||
((bits_unpacked[2] as base.u64) << 16) |
|
||||
((bits_unpacked[3] as base.u64) << 24) |
|
||||
((bits_unpacked[4] as base.u64) << 32) |
|
||||
((bits_unpacked[5] as base.u64) << 40) |
|
||||
((bits_unpacked[6] as base.u64) << 48) |
|
||||
((bits_unpacked[7] as base.u64) << 56)) {
|
||||
bits_unpacked[0] = 0
|
||||
bits_unpacked[1] = 0
|
||||
bits_unpacked[2] = 0
|
||||
bits_unpacked[3] = 0
|
||||
bits_unpacked[4] = 0
|
||||
bits_unpacked[5] = 0
|
||||
bits_unpacked[6] = 0
|
||||
bits_unpacked[7] = 0
|
||||
}
|
||||
}
|
||||
|
||||
} else if this.color_type == 4 {
|
||||
if 4 <= s.length() {
|
||||
bits_unpacked[0] = s[1]
|
||||
bits_unpacked[1] = s[0]
|
||||
bits_unpacked[2] = s[1]
|
||||
bits_unpacked[3] = s[0]
|
||||
bits_unpacked[4] = s[1]
|
||||
bits_unpacked[5] = s[0]
|
||||
bits_unpacked[6] = s[3]
|
||||
bits_unpacked[7] = s[2]
|
||||
s = s[4 ..]
|
||||
}
|
||||
} else if this.color_type == 4 {
|
||||
if 4 <= s.length() {
|
||||
bits_unpacked[0] = s[1]
|
||||
bits_unpacked[1] = s[0]
|
||||
bits_unpacked[2] = s[1]
|
||||
bits_unpacked[3] = s[0]
|
||||
bits_unpacked[4] = s[1]
|
||||
bits_unpacked[5] = s[0]
|
||||
bits_unpacked[6] = s[3]
|
||||
bits_unpacked[7] = s[2]
|
||||
s = s[4 ..]
|
||||
}
|
||||
|
||||
} else {
|
||||
if 8 <= s.length() {
|
||||
bits_unpacked[0] = s[5]
|
||||
bits_unpacked[1] = s[4]
|
||||
bits_unpacked[2] = s[3]
|
||||
bits_unpacked[3] = s[2]
|
||||
bits_unpacked[4] = s[1]
|
||||
bits_unpacked[5] = s[0]
|
||||
bits_unpacked[6] = s[7]
|
||||
bits_unpacked[7] = s[6]
|
||||
s = s[8 ..]
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if 8 <= s.length() {
|
||||
bits_unpacked[0] = s[5]
|
||||
bits_unpacked[1] = s[4]
|
||||
bits_unpacked[2] = s[3]
|
||||
bits_unpacked[3] = s[2]
|
||||
bits_unpacked[4] = s[1]
|
||||
bits_unpacked[5] = s[0]
|
||||
bits_unpacked[6] = s[7]
|
||||
bits_unpacked[7] = s[6]
|
||||
s = s[8 ..]
|
||||
}
|
||||
}
|
||||
|
||||
this.swizzler.swizzle_interleaved_from_slice!(
|
||||
dst: dst[i ..],
|
||||
dst_palette: dst_palette,
|
||||
src: bits_unpacked[.. 8])
|
||||
}
|
||||
x += (1 as base.u32) << INTERLACING[this.interlace_pass][0]
|
||||
} endwhile
|
||||
}
|
||||
this.swizzler.swizzle_interleaved_from_slice!(
|
||||
dst: dst[i ..],
|
||||
dst_palette: dst_palette,
|
||||
src: bits_unpacked[.. 8])
|
||||
}
|
||||
x += (1 as base.u32) << INTERLACING[this.interlace_pass][0]
|
||||
} endwhile
|
||||
}
|
||||
|
||||
prev_row = curr_row
|
||||
y += (1 as base.u32) << INTERLACING[this.interlace_pass][3]
|
||||
} endwhile
|
||||
prev_row = curr_row
|
||||
y += (1 as base.u32) << INTERLACING[this.interlace_pass][3]
|
||||
} endwhile
|
||||
|
||||
return ok
|
||||
return ok
|
||||
}
|
||||
+499
-499
@@ -20,596 +20,596 @@ pub status "#unsupported TGA file"
|
||||
pub const DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE : base.u64 = 0
|
||||
|
||||
pub struct decoder? implements base.image_decoder(
|
||||
width : base.u32[..= 0xFFFF],
|
||||
height : base.u32[..= 0xFFFF],
|
||||
width : base.u32[..= 0xFFFF],
|
||||
height : base.u32[..= 0xFFFF],
|
||||
|
||||
// The call sequence state machine is discussed in
|
||||
// (/doc/std/image-decoders-call-sequence.md).
|
||||
call_sequence : base.u8,
|
||||
// The call sequence state machine is discussed in
|
||||
// (/doc/std/image-decoders-call-sequence.md).
|
||||
call_sequence : base.u8,
|
||||
|
||||
header_id_length : base.u8,
|
||||
header_color_map_type : base.u8,
|
||||
header_image_type : base.u8,
|
||||
header_color_map_first_entry_index : base.u16,
|
||||
header_color_map_length : base.u16,
|
||||
header_color_map_entry_size : base.u8,
|
||||
header_pixel_depth : base.u8,
|
||||
header_image_descriptor : base.u8,
|
||||
header_id_length : base.u8,
|
||||
header_color_map_type : base.u8,
|
||||
header_image_type : base.u8,
|
||||
header_color_map_first_entry_index : base.u16,
|
||||
header_color_map_length : base.u16,
|
||||
header_color_map_entry_size : base.u8,
|
||||
header_pixel_depth : base.u8,
|
||||
header_image_descriptor : base.u8,
|
||||
|
||||
opaque : base.bool,
|
||||
opaque : base.bool,
|
||||
|
||||
scratch_bytes_per_pixel : base.u32[..= 4],
|
||||
src_bytes_per_pixel : base.u32[..= 4],
|
||||
src_pixfmt : base.u32,
|
||||
scratch_bytes_per_pixel : base.u32[..= 4],
|
||||
src_bytes_per_pixel : base.u32[..= 4],
|
||||
src_pixfmt : base.u32,
|
||||
|
||||
frame_config_io_position : base.u64,
|
||||
frame_config_io_position : base.u64,
|
||||
|
||||
swizzler : base.pixel_swizzler,
|
||||
util : base.utility,
|
||||
swizzler : base.pixel_swizzler,
|
||||
util : base.utility,
|
||||
)(
|
||||
dst_palette : array[4 * 256] base.u8,
|
||||
src_palette : array[4 * 256] base.u8,
|
||||
scratch : array[4] base.u8,
|
||||
dst_palette : array[4 * 256] base.u8,
|
||||
src_palette : array[4 * 256] base.u8,
|
||||
scratch : array[4] base.u8,
|
||||
)
|
||||
|
||||
pub func decoder.set_quirk_enabled!(quirk: base.u32, enabled: base.bool) {
|
||||
}
|
||||
|
||||
pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_reader) {
|
||||
var status : base.status
|
||||
var status : base.status
|
||||
|
||||
while true {
|
||||
status =? this.do_decode_image_config?(dst: args.dst, src: args.src)
|
||||
if (status == base."$short read") and args.src.is_closed() {
|
||||
return "#truncated input"
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
while true {
|
||||
status =? this.do_decode_image_config?(dst: args.dst, src: args.src)
|
||||
if (status == base."$short read") and args.src.is_closed() {
|
||||
return "#truncated input"
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
}
|
||||
|
||||
pri func decoder.do_decode_image_config?(dst: nptr base.image_config, src: base.io_reader) {
|
||||
var c : base.u32
|
||||
var c5 : base.u32[..= 0x1F]
|
||||
var i : base.u32
|
||||
var c : base.u32
|
||||
var c5 : base.u32[..= 0x1F]
|
||||
var i : base.u32
|
||||
|
||||
if this.call_sequence <> 0x00 {
|
||||
return base."#bad call sequence"
|
||||
}
|
||||
if this.call_sequence <> 0x00 {
|
||||
return base."#bad call sequence"
|
||||
}
|
||||
|
||||
this.header_id_length = args.src.read_u8?()
|
||||
this.header_color_map_type = args.src.read_u8?()
|
||||
if this.header_color_map_type > 1 {
|
||||
return "#bad header"
|
||||
}
|
||||
this.header_id_length = args.src.read_u8?()
|
||||
this.header_color_map_type = args.src.read_u8?()
|
||||
if this.header_color_map_type > 1 {
|
||||
return "#bad header"
|
||||
}
|
||||
|
||||
this.header_image_type = args.src.read_u8?()
|
||||
if (this.header_image_type == 0x01) or
|
||||
(this.header_image_type == 0x02) or
|
||||
(this.header_image_type == 0x03) or
|
||||
(this.header_image_type == 0x09) or
|
||||
(this.header_image_type == 0x0A) or
|
||||
(this.header_image_type == 0x0B) {
|
||||
// No-op.
|
||||
} else {
|
||||
// TODO: 0x20 and 0x21 are invalid, according to the spec, but are
|
||||
// apparently unofficial extensions.
|
||||
return "#bad header"
|
||||
}
|
||||
this.header_image_type = args.src.read_u8?()
|
||||
if (this.header_image_type == 0x01) or
|
||||
(this.header_image_type == 0x02) or
|
||||
(this.header_image_type == 0x03) or
|
||||
(this.header_image_type == 0x09) or
|
||||
(this.header_image_type == 0x0A) or
|
||||
(this.header_image_type == 0x0B) {
|
||||
// No-op.
|
||||
} else {
|
||||
// TODO: 0x20 and 0x21 are invalid, according to the spec, but are
|
||||
// apparently unofficial extensions.
|
||||
return "#bad header"
|
||||
}
|
||||
|
||||
this.header_color_map_first_entry_index = args.src.read_u16le?()
|
||||
this.header_color_map_length = args.src.read_u16le?()
|
||||
this.header_color_map_entry_size = args.src.read_u8?()
|
||||
if this.header_color_map_type <> 0 {
|
||||
// We have a color-mapped image (in Wuffs, an indexed pixel format).
|
||||
if (this.header_color_map_first_entry_index <> 0) or
|
||||
(this.header_color_map_length > 0x100) {
|
||||
return "#unsupported TGA file"
|
||||
} else if (this.header_color_map_entry_size <> 0x0F) and
|
||||
(this.header_color_map_entry_size <> 0x10) and
|
||||
(this.header_color_map_entry_size <> 0x18) and
|
||||
(this.header_color_map_entry_size <> 0x20) {
|
||||
return "#bad header"
|
||||
}
|
||||
} else {
|
||||
// The color-map fields must be zero.
|
||||
if (this.header_color_map_first_entry_index <> 0) or
|
||||
(this.header_color_map_length <> 0) or
|
||||
(this.header_color_map_entry_size <> 0) {
|
||||
return "#bad header"
|
||||
}
|
||||
}
|
||||
this.header_color_map_first_entry_index = args.src.read_u16le?()
|
||||
this.header_color_map_length = args.src.read_u16le?()
|
||||
this.header_color_map_entry_size = args.src.read_u8?()
|
||||
if this.header_color_map_type <> 0 {
|
||||
// We have a color-mapped image (in Wuffs, an indexed pixel format).
|
||||
if (this.header_color_map_first_entry_index <> 0) or
|
||||
(this.header_color_map_length > 0x100) {
|
||||
return "#unsupported TGA file"
|
||||
} else if (this.header_color_map_entry_size <> 0x0F) and
|
||||
(this.header_color_map_entry_size <> 0x10) and
|
||||
(this.header_color_map_entry_size <> 0x18) and
|
||||
(this.header_color_map_entry_size <> 0x20) {
|
||||
return "#bad header"
|
||||
}
|
||||
} else {
|
||||
// The color-map fields must be zero.
|
||||
if (this.header_color_map_first_entry_index <> 0) or
|
||||
(this.header_color_map_length <> 0) or
|
||||
(this.header_color_map_entry_size <> 0) {
|
||||
return "#bad header"
|
||||
}
|
||||
}
|
||||
|
||||
// Ignore the X-Origin and Y-Origin fields.
|
||||
args.src.skip_u32?(n: 4)
|
||||
// Ignore the X-Origin and Y-Origin fields.
|
||||
args.src.skip_u32?(n: 4)
|
||||
|
||||
this.width = args.src.read_u16le_as_u32?()
|
||||
this.height = args.src.read_u16le_as_u32?()
|
||||
this.width = args.src.read_u16le_as_u32?()
|
||||
this.height = args.src.read_u16le_as_u32?()
|
||||
|
||||
this.header_pixel_depth = args.src.read_u8?()
|
||||
if (this.header_pixel_depth <> 0x01) and
|
||||
(this.header_pixel_depth <> 0x08) and
|
||||
(this.header_pixel_depth <> 0x0F) and
|
||||
(this.header_pixel_depth <> 0x10) and
|
||||
(this.header_pixel_depth <> 0x18) and
|
||||
(this.header_pixel_depth <> 0x20) {
|
||||
return "#bad header"
|
||||
}
|
||||
this.header_pixel_depth = args.src.read_u8?()
|
||||
if (this.header_pixel_depth <> 0x01) and
|
||||
(this.header_pixel_depth <> 0x08) and
|
||||
(this.header_pixel_depth <> 0x0F) and
|
||||
(this.header_pixel_depth <> 0x10) and
|
||||
(this.header_pixel_depth <> 0x18) and
|
||||
(this.header_pixel_depth <> 0x20) {
|
||||
return "#bad header"
|
||||
}
|
||||
|
||||
if (this.header_image_type | 8) == 0x09 {
|
||||
this.scratch_bytes_per_pixel = 1
|
||||
this.src_bytes_per_pixel = 1
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL
|
||||
this.opaque =
|
||||
(this.header_color_map_entry_size == 0x0F) or
|
||||
(this.header_color_map_entry_size == 0x18)
|
||||
if (this.header_image_type | 8) == 0x09 {
|
||||
this.scratch_bytes_per_pixel = 1
|
||||
this.src_bytes_per_pixel = 1
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL
|
||||
this.opaque =
|
||||
(this.header_color_map_entry_size == 0x0F) or
|
||||
(this.header_color_map_entry_size == 0x18)
|
||||
|
||||
} else if (this.header_image_type | 8) == 0x0A {
|
||||
if (this.header_pixel_depth == 0x0F) or
|
||||
(this.header_pixel_depth == 0x10) {
|
||||
// Wuffs' base.pixel_swizzler doesn't support BGRX5551, so
|
||||
// scratch_bytes_per_pixel and src_bytes_per_pixel are different.
|
||||
this.scratch_bytes_per_pixel = 4
|
||||
this.src_bytes_per_pixel = 0
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL
|
||||
} else if this.header_pixel_depth == 0x18 {
|
||||
this.scratch_bytes_per_pixel = 3
|
||||
this.src_bytes_per_pixel = 3
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__BGR
|
||||
this.opaque = true
|
||||
} else if this.header_pixel_depth == 0x20 {
|
||||
this.scratch_bytes_per_pixel = 4
|
||||
this.src_bytes_per_pixel = 4
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL
|
||||
} else {
|
||||
return "#unsupported TGA file"
|
||||
}
|
||||
} else if (this.header_image_type | 8) == 0x0A {
|
||||
if (this.header_pixel_depth == 0x0F) or
|
||||
(this.header_pixel_depth == 0x10) {
|
||||
// Wuffs' base.pixel_swizzler doesn't support BGRX5551, so
|
||||
// scratch_bytes_per_pixel and src_bytes_per_pixel are different.
|
||||
this.scratch_bytes_per_pixel = 4
|
||||
this.src_bytes_per_pixel = 0
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL
|
||||
} else if this.header_pixel_depth == 0x18 {
|
||||
this.scratch_bytes_per_pixel = 3
|
||||
this.src_bytes_per_pixel = 3
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__BGR
|
||||
this.opaque = true
|
||||
} else if this.header_pixel_depth == 0x20 {
|
||||
this.scratch_bytes_per_pixel = 4
|
||||
this.src_bytes_per_pixel = 4
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL
|
||||
} else {
|
||||
return "#unsupported TGA file"
|
||||
}
|
||||
|
||||
} else {
|
||||
if this.header_pixel_depth == 0x08 {
|
||||
this.scratch_bytes_per_pixel = 1
|
||||
this.src_bytes_per_pixel = 1
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__Y
|
||||
this.opaque = true
|
||||
} else {
|
||||
return "#unsupported TGA file"
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if this.header_pixel_depth == 0x08 {
|
||||
this.scratch_bytes_per_pixel = 1
|
||||
this.src_bytes_per_pixel = 1
|
||||
this.src_pixfmt = base.PIXEL_FORMAT__Y
|
||||
this.opaque = true
|
||||
} else {
|
||||
return "#unsupported TGA file"
|
||||
}
|
||||
}
|
||||
|
||||
this.header_image_descriptor = args.src.read_u8?()
|
||||
if (this.header_image_descriptor & 0x10) <> 0 {
|
||||
// We don't support right-to-left, only left-to-right pixel order.
|
||||
return "#unsupported TGA file"
|
||||
}
|
||||
this.header_image_descriptor = args.src.read_u8?()
|
||||
if (this.header_image_descriptor & 0x10) <> 0 {
|
||||
// We don't support right-to-left, only left-to-right pixel order.
|
||||
return "#unsupported TGA file"
|
||||
}
|
||||
|
||||
// Skip the Image ID.
|
||||
args.src.skip_u32?(n: this.header_id_length as base.u32)
|
||||
// Skip the Image ID.
|
||||
args.src.skip_u32?(n: this.header_id_length as base.u32)
|
||||
|
||||
// Read the color map.
|
||||
if this.header_color_map_type <> 0 {
|
||||
while i < (this.header_color_map_length as base.u32) {
|
||||
assert i <= 0xFFFF via "a <= b: a <= c; c <= b"(c: this.header_color_map_length as base.u32)
|
||||
if this.header_color_map_entry_size == 0x18 {
|
||||
c = args.src.read_u24le_as_u32?()
|
||||
this.src_palette[((i & 0xFF) * 4) + 0] = ((c >> 0x00) & 0xFF) as base.u8
|
||||
this.src_palette[((i & 0xFF) * 4) + 1] = ((c >> 0x08) & 0xFF) as base.u8
|
||||
this.src_palette[((i & 0xFF) * 4) + 2] = ((c >> 0x10) & 0xFF) as base.u8
|
||||
this.src_palette[((i & 0xFF) * 4) + 3] = 0xFF
|
||||
} else if this.header_color_map_entry_size == 0x20 {
|
||||
c = args.src.read_u32le?()
|
||||
this.src_palette[((i & 0xFF) * 4) + 0] = ((c >> 0x00) & 0xFF) as base.u8
|
||||
this.src_palette[((i & 0xFF) * 4) + 1] = ((c >> 0x08) & 0xFF) as base.u8
|
||||
this.src_palette[((i & 0xFF) * 4) + 2] = ((c >> 0x10) & 0xFF) as base.u8
|
||||
this.src_palette[((i & 0xFF) * 4) + 3] = ((c >> 0x18) & 0xFF) as base.u8
|
||||
} else {
|
||||
// Expand 15-bit or 16-bit color map entries.
|
||||
c = args.src.read_u16le_as_u32?()
|
||||
c5 = 0x1F & (c >> 0)
|
||||
this.src_palette[((i & 0xFF) * 4) + 0] = ((c5 << 3) | (c5 >> 2)) as base.u8
|
||||
c5 = 0x1F & (c >> 5)
|
||||
this.src_palette[((i & 0xFF) * 4) + 1] = ((c5 << 3) | (c5 >> 2)) as base.u8
|
||||
c5 = 0x1F & (c >> 10)
|
||||
this.src_palette[((i & 0xFF) * 4) + 2] = ((c5 << 3) | (c5 >> 2)) as base.u8
|
||||
// TODO: can the alpha value be zero (BGRA5551 not BGRX5551)?
|
||||
this.src_palette[((i & 0xFF) * 4) + 3] = 0xFF
|
||||
}
|
||||
i += 1
|
||||
} endwhile
|
||||
while i < 0x100 {
|
||||
this.src_palette[(i * 4) + 0] = 0x00
|
||||
this.src_palette[(i * 4) + 1] = 0x00
|
||||
this.src_palette[(i * 4) + 2] = 0x00
|
||||
this.src_palette[(i * 4) + 3] = 0xFF
|
||||
i += 1
|
||||
} endwhile
|
||||
}
|
||||
// Read the color map.
|
||||
if this.header_color_map_type <> 0 {
|
||||
while i < (this.header_color_map_length as base.u32) {
|
||||
assert i <= 0xFFFF via "a <= b: a <= c; c <= b"(c: this.header_color_map_length as base.u32)
|
||||
if this.header_color_map_entry_size == 0x18 {
|
||||
c = args.src.read_u24le_as_u32?()
|
||||
this.src_palette[((i & 0xFF) * 4) + 0] = ((c >> 0x00) & 0xFF) as base.u8
|
||||
this.src_palette[((i & 0xFF) * 4) + 1] = ((c >> 0x08) & 0xFF) as base.u8
|
||||
this.src_palette[((i & 0xFF) * 4) + 2] = ((c >> 0x10) & 0xFF) as base.u8
|
||||
this.src_palette[((i & 0xFF) * 4) + 3] = 0xFF
|
||||
} else if this.header_color_map_entry_size == 0x20 {
|
||||
c = args.src.read_u32le?()
|
||||
this.src_palette[((i & 0xFF) * 4) + 0] = ((c >> 0x00) & 0xFF) as base.u8
|
||||
this.src_palette[((i & 0xFF) * 4) + 1] = ((c >> 0x08) & 0xFF) as base.u8
|
||||
this.src_palette[((i & 0xFF) * 4) + 2] = ((c >> 0x10) & 0xFF) as base.u8
|
||||
this.src_palette[((i & 0xFF) * 4) + 3] = ((c >> 0x18) & 0xFF) as base.u8
|
||||
} else {
|
||||
// Expand 15-bit or 16-bit color map entries.
|
||||
c = args.src.read_u16le_as_u32?()
|
||||
c5 = 0x1F & (c >> 0)
|
||||
this.src_palette[((i & 0xFF) * 4) + 0] = ((c5 << 3) | (c5 >> 2)) as base.u8
|
||||
c5 = 0x1F & (c >> 5)
|
||||
this.src_palette[((i & 0xFF) * 4) + 1] = ((c5 << 3) | (c5 >> 2)) as base.u8
|
||||
c5 = 0x1F & (c >> 10)
|
||||
this.src_palette[((i & 0xFF) * 4) + 2] = ((c5 << 3) | (c5 >> 2)) as base.u8
|
||||
// TODO: can the alpha value be zero (BGRA5551 not BGRX5551)?
|
||||
this.src_palette[((i & 0xFF) * 4) + 3] = 0xFF
|
||||
}
|
||||
i += 1
|
||||
} endwhile
|
||||
while i < 0x100 {
|
||||
this.src_palette[(i * 4) + 0] = 0x00
|
||||
this.src_palette[(i * 4) + 1] = 0x00
|
||||
this.src_palette[(i * 4) + 2] = 0x00
|
||||
this.src_palette[(i * 4) + 3] = 0xFF
|
||||
i += 1
|
||||
} endwhile
|
||||
}
|
||||
|
||||
this.frame_config_io_position = args.src.position()
|
||||
this.frame_config_io_position = args.src.position()
|
||||
|
||||
if args.dst <> nullptr {
|
||||
args.dst.set!(
|
||||
pixfmt: this.src_pixfmt,
|
||||
pixsub: 0,
|
||||
width: this.width,
|
||||
height: this.height,
|
||||
first_frame_io_position: this.frame_config_io_position,
|
||||
first_frame_is_opaque: this.opaque)
|
||||
}
|
||||
if args.dst <> nullptr {
|
||||
args.dst.set!(
|
||||
pixfmt: this.src_pixfmt,
|
||||
pixsub: 0,
|
||||
width: this.width,
|
||||
height: this.height,
|
||||
first_frame_io_position: this.frame_config_io_position,
|
||||
first_frame_is_opaque: this.opaque)
|
||||
}
|
||||
|
||||
this.call_sequence = 0x20
|
||||
this.call_sequence = 0x20
|
||||
}
|
||||
|
||||
pub func decoder.decode_frame_config?(dst: nptr base.frame_config, src: base.io_reader) {
|
||||
var status : base.status
|
||||
var status : base.status
|
||||
|
||||
while true {
|
||||
status =? this.do_decode_frame_config?(dst: args.dst, src: args.src)
|
||||
if (status == base."$short read") and args.src.is_closed() {
|
||||
return "#truncated input"
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
while true {
|
||||
status =? this.do_decode_frame_config?(dst: args.dst, src: args.src)
|
||||
if (status == base."$short read") and args.src.is_closed() {
|
||||
return "#truncated input"
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
}
|
||||
|
||||
pri func decoder.do_decode_frame_config?(dst: nptr base.frame_config, src: base.io_reader) {
|
||||
if this.call_sequence == 0x20 {
|
||||
// No-op.
|
||||
} else if this.call_sequence < 0x20 {
|
||||
this.do_decode_image_config?(dst: nullptr, src: args.src)
|
||||
} else if this.call_sequence == 0x28 {
|
||||
if this.frame_config_io_position <> args.src.position() {
|
||||
return base."#bad restart"
|
||||
}
|
||||
} else if this.call_sequence == 0x40 {
|
||||
this.call_sequence = 0x60
|
||||
return base."@end of data"
|
||||
} else {
|
||||
return base."@end of data"
|
||||
}
|
||||
if this.call_sequence == 0x20 {
|
||||
// No-op.
|
||||
} else if this.call_sequence < 0x20 {
|
||||
this.do_decode_image_config?(dst: nullptr, src: args.src)
|
||||
} else if this.call_sequence == 0x28 {
|
||||
if this.frame_config_io_position <> args.src.position() {
|
||||
return base."#bad restart"
|
||||
}
|
||||
} else if this.call_sequence == 0x40 {
|
||||
this.call_sequence = 0x60
|
||||
return base."@end of data"
|
||||
} else {
|
||||
return base."@end of data"
|
||||
}
|
||||
|
||||
if args.dst <> nullptr {
|
||||
args.dst.set!(bounds: this.util.make_rect_ie_u32(
|
||||
min_incl_x: 0,
|
||||
min_incl_y: 0,
|
||||
max_excl_x: this.width,
|
||||
max_excl_y: this.height),
|
||||
duration: 0,
|
||||
index: 0,
|
||||
io_position: this.frame_config_io_position,
|
||||
disposal: 0,
|
||||
opaque_within_bounds: this.opaque,
|
||||
overwrite_instead_of_blend: false,
|
||||
background_color: 0xFF00_0000)
|
||||
}
|
||||
if args.dst <> nullptr {
|
||||
args.dst.set!(bounds: this.util.make_rect_ie_u32(
|
||||
min_incl_x: 0,
|
||||
min_incl_y: 0,
|
||||
max_excl_x: this.width,
|
||||
max_excl_y: this.height),
|
||||
duration: 0,
|
||||
index: 0,
|
||||
io_position: this.frame_config_io_position,
|
||||
disposal: 0,
|
||||
opaque_within_bounds: this.opaque,
|
||||
overwrite_instead_of_blend: false,
|
||||
background_color: 0xFF00_0000)
|
||||
}
|
||||
|
||||
this.call_sequence = 0x40
|
||||
this.call_sequence = 0x40
|
||||
}
|
||||
|
||||
pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader, blend: base.pixel_blend, workbuf: slice base.u8, opts: nptr base.decode_frame_options) {
|
||||
var status : base.status
|
||||
var status : base.status
|
||||
|
||||
while true {
|
||||
status =? this.do_decode_frame?(dst: args.dst, src: args.src, blend: args.blend, workbuf: args.workbuf, opts: args.opts)
|
||||
if (status == base."$short read") and args.src.is_closed() {
|
||||
return "#truncated input"
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
while true {
|
||||
status =? this.do_decode_frame?(dst: args.dst, src: args.src, blend: args.blend, workbuf: args.workbuf, opts: args.opts)
|
||||
if (status == base."$short read") and args.src.is_closed() {
|
||||
return "#truncated input"
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
}
|
||||
|
||||
pri func decoder.do_decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader, blend: base.pixel_blend, workbuf: slice base.u8, opts: nptr base.decode_frame_options) {
|
||||
var status : base.status
|
||||
var dst_pixfmt : base.pixel_format
|
||||
var dst_bits_per_pixel : base.u32[..= 256]
|
||||
var dst_bytes_per_pixel : base.u64[..= 32]
|
||||
var dst_x : base.u32
|
||||
var dst_y : base.u32
|
||||
var tab : table base.u8
|
||||
var dst_palette : slice base.u8
|
||||
var dst : slice base.u8
|
||||
var dst_start : base.u64
|
||||
var src_palette : slice base.u8
|
||||
var mark : base.u64
|
||||
var num_pixels64 : base.u64
|
||||
var num_pixels32 : base.u32[..= 0xFFFF]
|
||||
var lit_length : base.u32[..= 0xFFFF]
|
||||
var run_length : base.u32[..= 0xFFFF]
|
||||
var num_dst_bytes : base.u64[..= 0x1F_FFE0]
|
||||
var num_src_bytes : base.u32[..= 0x3_FFFC]
|
||||
var c : base.u32
|
||||
var c5 : base.u32[..= 0x1F]
|
||||
var status : base.status
|
||||
var dst_pixfmt : base.pixel_format
|
||||
var dst_bits_per_pixel : base.u32[..= 256]
|
||||
var dst_bytes_per_pixel : base.u64[..= 32]
|
||||
var dst_x : base.u32
|
||||
var dst_y : base.u32
|
||||
var tab : table base.u8
|
||||
var dst_palette : slice base.u8
|
||||
var dst : slice base.u8
|
||||
var dst_start : base.u64
|
||||
var src_palette : slice base.u8
|
||||
var mark : base.u64
|
||||
var num_pixels64 : base.u64
|
||||
var num_pixels32 : base.u32[..= 0xFFFF]
|
||||
var lit_length : base.u32[..= 0xFFFF]
|
||||
var run_length : base.u32[..= 0xFFFF]
|
||||
var num_dst_bytes : base.u64[..= 0x1F_FFE0]
|
||||
var num_src_bytes : base.u32[..= 0x3_FFFC]
|
||||
var c : base.u32
|
||||
var c5 : base.u32[..= 0x1F]
|
||||
|
||||
if this.call_sequence == 0x40 {
|
||||
// No-op.
|
||||
} else if this.call_sequence < 0x40 {
|
||||
this.do_decode_frame_config?(dst: nullptr, src: args.src)
|
||||
} else {
|
||||
return base."@end of data"
|
||||
}
|
||||
if this.call_sequence == 0x40 {
|
||||
// No-op.
|
||||
} else if this.call_sequence < 0x40 {
|
||||
this.do_decode_frame_config?(dst: nullptr, src: args.src)
|
||||
} else {
|
||||
return base."@end of data"
|
||||
}
|
||||
|
||||
if this.header_color_map_type <> 0 {
|
||||
src_palette = this.src_palette[..]
|
||||
}
|
||||
status = this.swizzler.prepare!(
|
||||
dst_pixfmt: args.dst.pixel_format(),
|
||||
dst_palette: args.dst.palette_or_else(fallback: this.dst_palette[..]),
|
||||
src_pixfmt: this.util.make_pixel_format(repr: this.src_pixfmt),
|
||||
src_palette: src_palette,
|
||||
blend: args.blend)
|
||||
if not status.is_ok() {
|
||||
return status
|
||||
}
|
||||
if this.header_color_map_type <> 0 {
|
||||
src_palette = this.src_palette[..]
|
||||
}
|
||||
status = this.swizzler.prepare!(
|
||||
dst_pixfmt: args.dst.pixel_format(),
|
||||
dst_palette: args.dst.palette_or_else(fallback: this.dst_palette[..]),
|
||||
src_pixfmt: this.util.make_pixel_format(repr: this.src_pixfmt),
|
||||
src_palette: src_palette,
|
||||
blend: args.blend)
|
||||
if not status.is_ok() {
|
||||
return status
|
||||
}
|
||||
|
||||
// TODO: the dst_pixfmt variable shouldn't be necessary. We should be able
|
||||
// to chain the two calls: "args.dst.pixel_format().bits_per_pixel()".
|
||||
dst_pixfmt = args.dst.pixel_format()
|
||||
dst_bits_per_pixel = dst_pixfmt.bits_per_pixel()
|
||||
if (dst_bits_per_pixel & 7) <> 0 {
|
||||
return base."#unsupported option"
|
||||
}
|
||||
dst_bytes_per_pixel = (dst_bits_per_pixel / 8) as base.u64
|
||||
// TODO: the dst_pixfmt variable shouldn't be necessary. We should be able
|
||||
// to chain the two calls: "args.dst.pixel_format().bits_per_pixel()".
|
||||
dst_pixfmt = args.dst.pixel_format()
|
||||
dst_bits_per_pixel = dst_pixfmt.bits_per_pixel()
|
||||
if (dst_bits_per_pixel & 7) <> 0 {
|
||||
return base."#unsupported option"
|
||||
}
|
||||
dst_bytes_per_pixel = (dst_bits_per_pixel / 8) as base.u64
|
||||
|
||||
if (this.header_image_descriptor & 0x20) == 0 { // Bottom-to-top.
|
||||
dst_y = this.height ~mod- 1
|
||||
}
|
||||
if (this.header_image_type & 8) == 0 {
|
||||
// No RLE (run length encoding) means that the entire row is
|
||||
// effectively literals.
|
||||
lit_length = this.width
|
||||
}
|
||||
if (this.header_image_descriptor & 0x20) == 0 { // Bottom-to-top.
|
||||
dst_y = this.height ~mod- 1
|
||||
}
|
||||
if (this.header_image_type & 8) == 0 {
|
||||
// No RLE (run length encoding) means that the entire row is
|
||||
// effectively literals.
|
||||
lit_length = this.width
|
||||
}
|
||||
|
||||
while.resume true {
|
||||
tab = args.dst.plane(p: 0)
|
||||
dst_palette = args.dst.palette_or_else(fallback: this.dst_palette[..])
|
||||
while.resume true {
|
||||
tab = args.dst.plane(p: 0)
|
||||
dst_palette = args.dst.palette_or_else(fallback: this.dst_palette[..])
|
||||
|
||||
while dst_y < this.height {
|
||||
dst = tab.row_u32(y: dst_y)
|
||||
dst_start = (dst_x as base.u64) * dst_bytes_per_pixel
|
||||
if dst_start <= dst.length() {
|
||||
dst = dst[dst_start ..]
|
||||
} else {
|
||||
dst = this.util.empty_slice_u8()
|
||||
}
|
||||
while dst_y < this.height {
|
||||
dst = tab.row_u32(y: dst_y)
|
||||
dst_start = (dst_x as base.u64) * dst_bytes_per_pixel
|
||||
if dst_start <= dst.length() {
|
||||
dst = dst[dst_start ..]
|
||||
} else {
|
||||
dst = this.util.empty_slice_u8()
|
||||
}
|
||||
|
||||
while dst_x < this.width {
|
||||
assert dst_x <= 0xFFFF via "a <= b: a <= c; c <= b"(c: this.width)
|
||||
while dst_x < this.width {
|
||||
assert dst_x <= 0xFFFF via "a <= b: a <= c; c <= b"(c: this.width)
|
||||
|
||||
if this.src_bytes_per_pixel > 0 {
|
||||
if lit_length > 0 {
|
||||
mark = args.src.mark()
|
||||
num_pixels64 = args.src.length() / (this.src_bytes_per_pixel as base.u64)
|
||||
num_pixels32 = num_pixels64.min(a: lit_length as base.u64) as base.u32
|
||||
num_dst_bytes = (num_pixels32 as base.u64) * dst_bytes_per_pixel
|
||||
num_src_bytes = num_pixels32 * this.src_bytes_per_pixel
|
||||
args.src.skip_u32?(n: num_src_bytes)
|
||||
this.swizzler.swizzle_interleaved_from_slice!(
|
||||
dst: dst,
|
||||
dst_palette: dst_palette,
|
||||
src: args.src.since(mark: mark))
|
||||
if num_dst_bytes <= dst.length() {
|
||||
dst = dst[num_dst_bytes ..]
|
||||
} else {
|
||||
dst = this.util.empty_slice_u8()
|
||||
}
|
||||
dst_x += num_pixels32
|
||||
lit_length = (lit_length ~mod- num_pixels32) & 0xFFFF
|
||||
if lit_length > 0 {
|
||||
yield? base."$short read"
|
||||
continue.resume
|
||||
}
|
||||
if this.src_bytes_per_pixel > 0 {
|
||||
if lit_length > 0 {
|
||||
mark = args.src.mark()
|
||||
num_pixels64 = args.src.length() / (this.src_bytes_per_pixel as base.u64)
|
||||
num_pixels32 = num_pixels64.min(a: lit_length as base.u64) as base.u32
|
||||
num_dst_bytes = (num_pixels32 as base.u64) * dst_bytes_per_pixel
|
||||
num_src_bytes = num_pixels32 * this.src_bytes_per_pixel
|
||||
args.src.skip_u32?(n: num_src_bytes)
|
||||
this.swizzler.swizzle_interleaved_from_slice!(
|
||||
dst: dst,
|
||||
dst_palette: dst_palette,
|
||||
src: args.src.since(mark: mark))
|
||||
if num_dst_bytes <= dst.length() {
|
||||
dst = dst[num_dst_bytes ..]
|
||||
} else {
|
||||
dst = this.util.empty_slice_u8()
|
||||
}
|
||||
dst_x += num_pixels32
|
||||
lit_length = (lit_length ~mod- num_pixels32) & 0xFFFF
|
||||
if lit_length > 0 {
|
||||
yield? base."$short read"
|
||||
continue.resume
|
||||
}
|
||||
|
||||
} else if run_length > 0 {
|
||||
run_length -= 1
|
||||
this.swizzler.swizzle_interleaved_from_slice!(
|
||||
dst: dst,
|
||||
dst_palette: dst_palette,
|
||||
src: this.scratch[.. this.scratch_bytes_per_pixel])
|
||||
if dst_bytes_per_pixel <= dst.length() {
|
||||
dst = dst[dst_bytes_per_pixel ..]
|
||||
}
|
||||
dst_x += 1
|
||||
} else if run_length > 0 {
|
||||
run_length -= 1
|
||||
this.swizzler.swizzle_interleaved_from_slice!(
|
||||
dst: dst,
|
||||
dst_palette: dst_palette,
|
||||
src: this.scratch[.. this.scratch_bytes_per_pixel])
|
||||
if dst_bytes_per_pixel <= dst.length() {
|
||||
dst = dst[dst_bytes_per_pixel ..]
|
||||
}
|
||||
dst_x += 1
|
||||
|
||||
} else {
|
||||
// Handle Raw vs RLE packets.
|
||||
if args.src.length() <= 0 {
|
||||
yield? base."$short read"
|
||||
continue.resume
|
||||
}
|
||||
if args.src.peek_u8_as_u32() < 0x80 {
|
||||
lit_length = args.src.peek_u8_as_u32() + 1
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
if (lit_length + dst_x) > this.width {
|
||||
return "#bad run length encoding"
|
||||
}
|
||||
} else {
|
||||
// Handle Raw vs RLE packets.
|
||||
if args.src.length() <= 0 {
|
||||
yield? base."$short read"
|
||||
continue.resume
|
||||
}
|
||||
if args.src.peek_u8_as_u32() < 0x80 {
|
||||
lit_length = args.src.peek_u8_as_u32() + 1
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
if (lit_length + dst_x) > this.width {
|
||||
return "#bad run length encoding"
|
||||
}
|
||||
|
||||
} else {
|
||||
if this.src_bytes_per_pixel == 1 {
|
||||
if args.src.length() < 2 {
|
||||
yield? base."$short read"
|
||||
continue.resume
|
||||
}
|
||||
run_length = (args.src.peek_u8_as_u32() & 0x7F) + 1
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
this.scratch[0] = args.src.peek_u8()
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
} else if this.src_bytes_per_pixel == 3 {
|
||||
if args.src.length() < 4 {
|
||||
yield? base."$short read"
|
||||
continue.resume
|
||||
}
|
||||
run_length = (args.src.peek_u8_as_u32() & 0x7F) + 1
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
this.scratch[0] = args.src.peek_u8()
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
this.scratch[1] = args.src.peek_u8()
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
this.scratch[2] = args.src.peek_u8()
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
} else { // this.src_bytes_per_pixel == 4
|
||||
if args.src.length() < 5 {
|
||||
yield? base."$short read"
|
||||
continue.resume
|
||||
}
|
||||
run_length = (args.src.peek_u8_as_u32() & 0x7F) + 1
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
this.scratch[0] = args.src.peek_u8()
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
this.scratch[1] = args.src.peek_u8()
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
this.scratch[2] = args.src.peek_u8()
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
this.scratch[3] = args.src.peek_u8()
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
}
|
||||
} else {
|
||||
if this.src_bytes_per_pixel == 1 {
|
||||
if args.src.length() < 2 {
|
||||
yield? base."$short read"
|
||||
continue.resume
|
||||
}
|
||||
run_length = (args.src.peek_u8_as_u32() & 0x7F) + 1
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
this.scratch[0] = args.src.peek_u8()
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
} else if this.src_bytes_per_pixel == 3 {
|
||||
if args.src.length() < 4 {
|
||||
yield? base."$short read"
|
||||
continue.resume
|
||||
}
|
||||
run_length = (args.src.peek_u8_as_u32() & 0x7F) + 1
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
this.scratch[0] = args.src.peek_u8()
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
this.scratch[1] = args.src.peek_u8()
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
this.scratch[2] = args.src.peek_u8()
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
} else { // this.src_bytes_per_pixel == 4
|
||||
if args.src.length() < 5 {
|
||||
yield? base."$short read"
|
||||
continue.resume
|
||||
}
|
||||
run_length = (args.src.peek_u8_as_u32() & 0x7F) + 1
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
this.scratch[0] = args.src.peek_u8()
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
this.scratch[1] = args.src.peek_u8()
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
this.scratch[2] = args.src.peek_u8()
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
this.scratch[3] = args.src.peek_u8()
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
}
|
||||
|
||||
if (run_length + dst_x) > this.width {
|
||||
return "#bad run length encoding"
|
||||
}
|
||||
}
|
||||
}
|
||||
if (run_length + dst_x) > this.width {
|
||||
return "#bad run length encoding"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
// Wuffs' base.pixel_swizzler doesn't support BGRX5551, so
|
||||
// we manually convert to BGRX8888, one pixel at a time.
|
||||
} else {
|
||||
// Wuffs' base.pixel_swizzler doesn't support BGRX5551, so
|
||||
// we manually convert to BGRX8888, one pixel at a time.
|
||||
|
||||
if lit_length > 0 {
|
||||
if args.src.length() < 2 {
|
||||
yield? base."$short read"
|
||||
continue.resume
|
||||
}
|
||||
c = args.src.peek_u16le_as_u32()
|
||||
args.src.skip_u32_fast!(actual: 2, worst_case: 2)
|
||||
c5 = 0x1F & (c >> 0)
|
||||
this.scratch[0] = ((c5 << 3) | (c5 >> 2)) as base.u8
|
||||
c5 = 0x1F & (c >> 5)
|
||||
this.scratch[1] = ((c5 << 3) | (c5 >> 2)) as base.u8
|
||||
c5 = 0x1F & (c >> 10)
|
||||
this.scratch[2] = ((c5 << 3) | (c5 >> 2)) as base.u8
|
||||
// TODO: can the alpha value be zero (BGRA5551 not BGRX5551)?
|
||||
this.scratch[3] = 0xFF
|
||||
this.swizzler.swizzle_interleaved_from_slice!(
|
||||
dst: dst,
|
||||
dst_palette: dst_palette,
|
||||
src: this.scratch[.. 4])
|
||||
if dst_bytes_per_pixel <= dst.length() {
|
||||
dst = dst[dst_bytes_per_pixel ..]
|
||||
}
|
||||
dst_x += 1
|
||||
lit_length -= 1
|
||||
if lit_length > 0 {
|
||||
if args.src.length() < 2 {
|
||||
yield? base."$short read"
|
||||
continue.resume
|
||||
}
|
||||
c = args.src.peek_u16le_as_u32()
|
||||
args.src.skip_u32_fast!(actual: 2, worst_case: 2)
|
||||
c5 = 0x1F & (c >> 0)
|
||||
this.scratch[0] = ((c5 << 3) | (c5 >> 2)) as base.u8
|
||||
c5 = 0x1F & (c >> 5)
|
||||
this.scratch[1] = ((c5 << 3) | (c5 >> 2)) as base.u8
|
||||
c5 = 0x1F & (c >> 10)
|
||||
this.scratch[2] = ((c5 << 3) | (c5 >> 2)) as base.u8
|
||||
// TODO: can the alpha value be zero (BGRA5551 not BGRX5551)?
|
||||
this.scratch[3] = 0xFF
|
||||
this.swizzler.swizzle_interleaved_from_slice!(
|
||||
dst: dst,
|
||||
dst_palette: dst_palette,
|
||||
src: this.scratch[.. 4])
|
||||
if dst_bytes_per_pixel <= dst.length() {
|
||||
dst = dst[dst_bytes_per_pixel ..]
|
||||
}
|
||||
dst_x += 1
|
||||
lit_length -= 1
|
||||
|
||||
} else if run_length > 0 {
|
||||
run_length -= 1
|
||||
this.swizzler.swizzle_interleaved_from_slice!(
|
||||
dst: dst,
|
||||
dst_palette: dst_palette,
|
||||
src: this.scratch[.. this.scratch_bytes_per_pixel])
|
||||
if dst_bytes_per_pixel <= dst.length() {
|
||||
dst = dst[dst_bytes_per_pixel ..]
|
||||
}
|
||||
dst_x += 1
|
||||
} else if run_length > 0 {
|
||||
run_length -= 1
|
||||
this.swizzler.swizzle_interleaved_from_slice!(
|
||||
dst: dst,
|
||||
dst_palette: dst_palette,
|
||||
src: this.scratch[.. this.scratch_bytes_per_pixel])
|
||||
if dst_bytes_per_pixel <= dst.length() {
|
||||
dst = dst[dst_bytes_per_pixel ..]
|
||||
}
|
||||
dst_x += 1
|
||||
|
||||
} else {
|
||||
// Handle Raw vs RLE packets.
|
||||
if args.src.length() <= 0 {
|
||||
yield? base."$short read"
|
||||
continue.resume
|
||||
}
|
||||
if args.src.peek_u8_as_u32() < 0x80 {
|
||||
lit_length = args.src.peek_u8_as_u32() + 1
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
if (lit_length + dst_x) > this.width {
|
||||
return "#bad run length encoding"
|
||||
}
|
||||
} else {
|
||||
// Handle Raw vs RLE packets.
|
||||
if args.src.length() <= 0 {
|
||||
yield? base."$short read"
|
||||
continue.resume
|
||||
}
|
||||
if args.src.peek_u8_as_u32() < 0x80 {
|
||||
lit_length = args.src.peek_u8_as_u32() + 1
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
if (lit_length + dst_x) > this.width {
|
||||
return "#bad run length encoding"
|
||||
}
|
||||
|
||||
} else {
|
||||
if args.src.length() < 3 {
|
||||
yield? base."$short read"
|
||||
continue.resume
|
||||
}
|
||||
run_length = (args.src.peek_u8_as_u32() & 0x7F) + 1
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
c = args.src.peek_u16le_as_u32()
|
||||
args.src.skip_u32_fast!(actual: 2, worst_case: 2)
|
||||
c5 = 0x1F & (c >> 0)
|
||||
this.scratch[0] = ((c5 << 3) | (c5 >> 2)) as base.u8
|
||||
c5 = 0x1F & (c >> 5)
|
||||
this.scratch[1] = ((c5 << 3) | (c5 >> 2)) as base.u8
|
||||
c5 = 0x1F & (c >> 10)
|
||||
this.scratch[2] = ((c5 << 3) | (c5 >> 2)) as base.u8
|
||||
// TODO: can the alpha value be zero (BGRA5551 not BGRX5551)?
|
||||
this.scratch[3] = 0xFF
|
||||
} else {
|
||||
if args.src.length() < 3 {
|
||||
yield? base."$short read"
|
||||
continue.resume
|
||||
}
|
||||
run_length = (args.src.peek_u8_as_u32() & 0x7F) + 1
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
c = args.src.peek_u16le_as_u32()
|
||||
args.src.skip_u32_fast!(actual: 2, worst_case: 2)
|
||||
c5 = 0x1F & (c >> 0)
|
||||
this.scratch[0] = ((c5 << 3) | (c5 >> 2)) as base.u8
|
||||
c5 = 0x1F & (c >> 5)
|
||||
this.scratch[1] = ((c5 << 3) | (c5 >> 2)) as base.u8
|
||||
c5 = 0x1F & (c >> 10)
|
||||
this.scratch[2] = ((c5 << 3) | (c5 >> 2)) as base.u8
|
||||
// TODO: can the alpha value be zero (BGRA5551 not BGRX5551)?
|
||||
this.scratch[3] = 0xFF
|
||||
|
||||
if (run_length + dst_x) > this.width {
|
||||
return "#bad run length encoding"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} endwhile
|
||||
dst_x = 0
|
||||
if (run_length + dst_x) > this.width {
|
||||
return "#bad run length encoding"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} endwhile
|
||||
dst_x = 0
|
||||
|
||||
if (this.header_image_descriptor & 0x20) == 0 { // Bottom-to-top.
|
||||
dst_y ~mod-= 1
|
||||
} else { // Top-to-bottom.
|
||||
dst_y ~mod+= 1
|
||||
}
|
||||
if (this.header_image_type & 8) == 0 {
|
||||
// No RLE (run length encoding) means that the entire row is
|
||||
// effectively literals.
|
||||
lit_length = this.width
|
||||
}
|
||||
} endwhile
|
||||
break.resume
|
||||
} endwhile.resume
|
||||
if (this.header_image_descriptor & 0x20) == 0 { // Bottom-to-top.
|
||||
dst_y ~mod-= 1
|
||||
} else { // Top-to-bottom.
|
||||
dst_y ~mod+= 1
|
||||
}
|
||||
if (this.header_image_type & 8) == 0 {
|
||||
// No RLE (run length encoding) means that the entire row is
|
||||
// effectively literals.
|
||||
lit_length = this.width
|
||||
}
|
||||
} endwhile
|
||||
break.resume
|
||||
} endwhile.resume
|
||||
|
||||
this.call_sequence = 0x60
|
||||
this.call_sequence = 0x60
|
||||
}
|
||||
|
||||
pub func decoder.frame_dirty_rect() base.rect_ie_u32 {
|
||||
return this.util.make_rect_ie_u32(
|
||||
min_incl_x: 0,
|
||||
min_incl_y: 0,
|
||||
max_excl_x: this.width,
|
||||
max_excl_y: this.height)
|
||||
return this.util.make_rect_ie_u32(
|
||||
min_incl_x: 0,
|
||||
min_incl_y: 0,
|
||||
max_excl_x: this.width,
|
||||
max_excl_y: this.height)
|
||||
}
|
||||
|
||||
pub func decoder.num_animation_loops() base.u32 {
|
||||
return 0
|
||||
return 0
|
||||
}
|
||||
|
||||
pub func decoder.num_decoded_frame_configs() base.u64 {
|
||||
if this.call_sequence > 0x20 {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
if this.call_sequence > 0x20 {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
pub func decoder.num_decoded_frames() base.u64 {
|
||||
if this.call_sequence > 0x40 {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
if this.call_sequence > 0x40 {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
pub func decoder.restart_frame!(index: base.u64, io_position: base.u64) base.status {
|
||||
if this.call_sequence < 0x20 {
|
||||
return base."#bad call sequence"
|
||||
}
|
||||
if args.index <> 0 {
|
||||
return base."#bad argument"
|
||||
}
|
||||
this.call_sequence = 0x28
|
||||
this.frame_config_io_position = args.io_position
|
||||
return ok
|
||||
if this.call_sequence < 0x20 {
|
||||
return base."#bad call sequence"
|
||||
}
|
||||
if args.index <> 0 {
|
||||
return base."#bad argument"
|
||||
}
|
||||
this.call_sequence = 0x28
|
||||
this.frame_config_io_position = args.io_position
|
||||
return ok
|
||||
}
|
||||
|
||||
pub func decoder.set_report_metadata!(fourcc: base.u32, report: base.bool) {
|
||||
// TODO: implement.
|
||||
// TODO: implement.
|
||||
}
|
||||
|
||||
pub func decoder.tell_me_more?(dst: base.io_writer, minfo: nptr base.more_information, src: base.io_reader) {
|
||||
return base."#no more information"
|
||||
return base."#no more information"
|
||||
}
|
||||
|
||||
pub func decoder.workbuf_len() base.range_ii_u64 {
|
||||
return this.util.make_range_ii_u64(min_incl: 0, max_incl: 0)
|
||||
return this.util.make_range_ii_u64(min_incl: 0, max_incl: 0)
|
||||
}
|
||||
+224
-224
@@ -18,299 +18,299 @@ pub status "#truncated input"
|
||||
pub const DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE : base.u64 = 0
|
||||
|
||||
pub struct decoder? implements base.image_decoder(
|
||||
width : base.u32,
|
||||
height : base.u32,
|
||||
width : base.u32,
|
||||
height : base.u32,
|
||||
|
||||
// The call sequence state machine is discussed in
|
||||
// (/doc/std/image-decoders-call-sequence.md).
|
||||
call_sequence : base.u8,
|
||||
// The call sequence state machine is discussed in
|
||||
// (/doc/std/image-decoders-call-sequence.md).
|
||||
call_sequence : base.u8,
|
||||
|
||||
frame_config_io_position : base.u64,
|
||||
frame_config_io_position : base.u64,
|
||||
|
||||
swizzler : base.pixel_swizzler,
|
||||
util : base.utility,
|
||||
swizzler : base.pixel_swizzler,
|
||||
util : base.utility,
|
||||
)
|
||||
|
||||
pub func decoder.set_quirk_enabled!(quirk: base.u32, enabled: base.bool) {
|
||||
}
|
||||
|
||||
pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_reader) {
|
||||
var status : base.status
|
||||
var status : base.status
|
||||
|
||||
while true {
|
||||
status =? this.do_decode_image_config?(dst: args.dst, src: args.src)
|
||||
if (status == base."$short read") and args.src.is_closed() {
|
||||
return "#truncated input"
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
while true {
|
||||
status =? this.do_decode_image_config?(dst: args.dst, src: args.src)
|
||||
if (status == base."$short read") and args.src.is_closed() {
|
||||
return "#truncated input"
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
}
|
||||
|
||||
pri func decoder.do_decode_image_config?(dst: nptr base.image_config, src: base.io_reader) {
|
||||
var c : base.u8
|
||||
var i : base.u32
|
||||
var x32 : base.u32
|
||||
var x64 : base.u64
|
||||
var c : base.u8
|
||||
var i : base.u32
|
||||
var x32 : base.u32
|
||||
var x64 : base.u64
|
||||
|
||||
if this.call_sequence <> 0x00 {
|
||||
return base."#bad call sequence"
|
||||
}
|
||||
if this.call_sequence <> 0x00 {
|
||||
return base."#bad call sequence"
|
||||
}
|
||||
|
||||
// TypeField, FixHeaderField.
|
||||
i = 0
|
||||
while i < 2 {
|
||||
c = args.src.read_u8?()
|
||||
if c <> 0 {
|
||||
return "#bad header"
|
||||
}
|
||||
i += 1
|
||||
} endwhile
|
||||
// TypeField, FixHeaderField.
|
||||
i = 0
|
||||
while i < 2 {
|
||||
c = args.src.read_u8?()
|
||||
if c <> 0 {
|
||||
return "#bad header"
|
||||
}
|
||||
i += 1
|
||||
} endwhile
|
||||
|
||||
// Width, height.
|
||||
i = 0
|
||||
while i < 2 {
|
||||
x32 = 0
|
||||
// Width, height.
|
||||
i = 0
|
||||
while i < 2 {
|
||||
x32 = 0
|
||||
|
||||
while true,
|
||||
inv i < 2,
|
||||
{
|
||||
c = args.src.read_u8?()
|
||||
x32 |= (c & 0x7F) as base.u32
|
||||
if (c >> 7) == 0 {
|
||||
break
|
||||
}
|
||||
x64 = (x32 as base.u64) << 7
|
||||
if x64 > 0xFFFF_FFFF {
|
||||
return "#bad header"
|
||||
}
|
||||
x32 = x64 as base.u32
|
||||
} endwhile
|
||||
while true,
|
||||
inv i < 2,
|
||||
{
|
||||
c = args.src.read_u8?()
|
||||
x32 |= (c & 0x7F) as base.u32
|
||||
if (c >> 7) == 0 {
|
||||
break
|
||||
}
|
||||
x64 = (x32 as base.u64) << 7
|
||||
if x64 > 0xFFFF_FFFF {
|
||||
return "#bad header"
|
||||
}
|
||||
x32 = x64 as base.u32
|
||||
} endwhile
|
||||
|
||||
if i == 0 {
|
||||
this.width = x32
|
||||
} else {
|
||||
this.height = x32
|
||||
}
|
||||
i += 1
|
||||
} endwhile
|
||||
if i == 0 {
|
||||
this.width = x32
|
||||
} else {
|
||||
this.height = x32
|
||||
}
|
||||
i += 1
|
||||
} endwhile
|
||||
|
||||
this.frame_config_io_position = args.src.position()
|
||||
this.frame_config_io_position = args.src.position()
|
||||
|
||||
if args.dst <> nullptr {
|
||||
args.dst.set!(
|
||||
pixfmt: base.PIXEL_FORMAT__INDEXED__BGRA_BINARY,
|
||||
pixsub: 0,
|
||||
width: this.width,
|
||||
height: this.height,
|
||||
first_frame_io_position: this.frame_config_io_position,
|
||||
first_frame_is_opaque: true)
|
||||
}
|
||||
if args.dst <> nullptr {
|
||||
args.dst.set!(
|
||||
pixfmt: base.PIXEL_FORMAT__INDEXED__BGRA_BINARY,
|
||||
pixsub: 0,
|
||||
width: this.width,
|
||||
height: this.height,
|
||||
first_frame_io_position: this.frame_config_io_position,
|
||||
first_frame_is_opaque: true)
|
||||
}
|
||||
|
||||
this.call_sequence = 0x20
|
||||
this.call_sequence = 0x20
|
||||
}
|
||||
|
||||
pub func decoder.decode_frame_config?(dst: nptr base.frame_config, src: base.io_reader) {
|
||||
var status : base.status
|
||||
var status : base.status
|
||||
|
||||
while true {
|
||||
status =? this.do_decode_frame_config?(dst: args.dst, src: args.src)
|
||||
if (status == base."$short read") and args.src.is_closed() {
|
||||
return "#truncated input"
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
while true {
|
||||
status =? this.do_decode_frame_config?(dst: args.dst, src: args.src)
|
||||
if (status == base."$short read") and args.src.is_closed() {
|
||||
return "#truncated input"
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
}
|
||||
|
||||
pri func decoder.do_decode_frame_config?(dst: nptr base.frame_config, src: base.io_reader) {
|
||||
if this.call_sequence == 0x20 {
|
||||
// No-op.
|
||||
} else if this.call_sequence < 0x20 {
|
||||
this.do_decode_image_config?(dst: nullptr, src: args.src)
|
||||
} else if this.call_sequence == 0x28 {
|
||||
if this.frame_config_io_position <> args.src.position() {
|
||||
return base."#bad restart"
|
||||
}
|
||||
} else if this.call_sequence == 0x40 {
|
||||
this.call_sequence = 0x60
|
||||
return base."@end of data"
|
||||
} else {
|
||||
return base."@end of data"
|
||||
}
|
||||
if this.call_sequence == 0x20 {
|
||||
// No-op.
|
||||
} else if this.call_sequence < 0x20 {
|
||||
this.do_decode_image_config?(dst: nullptr, src: args.src)
|
||||
} else if this.call_sequence == 0x28 {
|
||||
if this.frame_config_io_position <> args.src.position() {
|
||||
return base."#bad restart"
|
||||
}
|
||||
} else if this.call_sequence == 0x40 {
|
||||
this.call_sequence = 0x60
|
||||
return base."@end of data"
|
||||
} else {
|
||||
return base."@end of data"
|
||||
}
|
||||
|
||||
if args.dst <> nullptr {
|
||||
args.dst.set!(bounds: this.util.make_rect_ie_u32(
|
||||
min_incl_x: 0,
|
||||
min_incl_y: 0,
|
||||
max_excl_x: this.width,
|
||||
max_excl_y: this.height),
|
||||
duration: 0,
|
||||
index: 0,
|
||||
io_position: this.frame_config_io_position,
|
||||
disposal: 0,
|
||||
opaque_within_bounds: true,
|
||||
overwrite_instead_of_blend: false,
|
||||
background_color: 0xFF00_0000)
|
||||
}
|
||||
if args.dst <> nullptr {
|
||||
args.dst.set!(bounds: this.util.make_rect_ie_u32(
|
||||
min_incl_x: 0,
|
||||
min_incl_y: 0,
|
||||
max_excl_x: this.width,
|
||||
max_excl_y: this.height),
|
||||
duration: 0,
|
||||
index: 0,
|
||||
io_position: this.frame_config_io_position,
|
||||
disposal: 0,
|
||||
opaque_within_bounds: true,
|
||||
overwrite_instead_of_blend: false,
|
||||
background_color: 0xFF00_0000)
|
||||
}
|
||||
|
||||
this.call_sequence = 0x40
|
||||
this.call_sequence = 0x40
|
||||
}
|
||||
|
||||
pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader, blend: base.pixel_blend, workbuf: slice base.u8, opts: nptr base.decode_frame_options) {
|
||||
var status : base.status
|
||||
var status : base.status
|
||||
|
||||
while true {
|
||||
status =? this.do_decode_frame?(dst: args.dst, src: args.src, blend: args.blend, workbuf: args.workbuf, opts: args.opts)
|
||||
if (status == base."$short read") and args.src.is_closed() {
|
||||
return "#truncated input"
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
while true {
|
||||
status =? this.do_decode_frame?(dst: args.dst, src: args.src, blend: args.blend, workbuf: args.workbuf, opts: args.opts)
|
||||
if (status == base."$short read") and args.src.is_closed() {
|
||||
return "#truncated input"
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
}
|
||||
|
||||
pri func decoder.do_decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader, blend: base.pixel_blend, workbuf: slice base.u8, opts: nptr base.decode_frame_options) {
|
||||
var status : base.status
|
||||
var dst_pixfmt : base.pixel_format
|
||||
var dst_bits_per_pixel : base.u32[..= 256]
|
||||
var dst_bytes_per_pixel : base.u64[..= 32]
|
||||
var dst_x_in_bytes : base.u64
|
||||
var dst_x : base.u32
|
||||
var dst_y : base.u32
|
||||
var tab : table base.u8
|
||||
var dst : slice base.u8
|
||||
var src : array[1] base.u8
|
||||
var c : base.u8
|
||||
var status : base.status
|
||||
var dst_pixfmt : base.pixel_format
|
||||
var dst_bits_per_pixel : base.u32[..= 256]
|
||||
var dst_bytes_per_pixel : base.u64[..= 32]
|
||||
var dst_x_in_bytes : base.u64
|
||||
var dst_x : base.u32
|
||||
var dst_y : base.u32
|
||||
var tab : table base.u8
|
||||
var dst : slice base.u8
|
||||
var src : array[1] base.u8
|
||||
var c : base.u8
|
||||
|
||||
if this.call_sequence == 0x40 {
|
||||
// No-op.
|
||||
} else if this.call_sequence < 0x40 {
|
||||
this.do_decode_frame_config?(dst: nullptr, src: args.src)
|
||||
} else {
|
||||
return base."@end of data"
|
||||
}
|
||||
if this.call_sequence == 0x40 {
|
||||
// No-op.
|
||||
} else if this.call_sequence < 0x40 {
|
||||
this.do_decode_frame_config?(dst: nullptr, src: args.src)
|
||||
} else {
|
||||
return base."@end of data"
|
||||
}
|
||||
|
||||
status = this.swizzler.prepare!(
|
||||
dst_pixfmt: args.dst.pixel_format(),
|
||||
dst_palette: args.dst.palette(),
|
||||
src_pixfmt: this.util.make_pixel_format(repr: base.PIXEL_FORMAT__Y),
|
||||
src_palette: this.util.empty_slice_u8(),
|
||||
blend: args.blend)
|
||||
if not status.is_ok() {
|
||||
return status
|
||||
}
|
||||
status = this.swizzler.prepare!(
|
||||
dst_pixfmt: args.dst.pixel_format(),
|
||||
dst_palette: args.dst.palette(),
|
||||
src_pixfmt: this.util.make_pixel_format(repr: base.PIXEL_FORMAT__Y),
|
||||
src_palette: this.util.empty_slice_u8(),
|
||||
blend: args.blend)
|
||||
if not status.is_ok() {
|
||||
return status
|
||||
}
|
||||
|
||||
// TODO: the dst_pixfmt variable shouldn't be necessary. We should be able
|
||||
// to chain the two calls: "args.dst.pixel_format().bits_per_pixel()".
|
||||
dst_pixfmt = args.dst.pixel_format()
|
||||
dst_bits_per_pixel = dst_pixfmt.bits_per_pixel()
|
||||
if (dst_bits_per_pixel & 7) <> 0 {
|
||||
return base."#unsupported option"
|
||||
}
|
||||
dst_bytes_per_pixel = (dst_bits_per_pixel / 8) as base.u64
|
||||
// TODO: the dst_pixfmt variable shouldn't be necessary. We should be able
|
||||
// to chain the two calls: "args.dst.pixel_format().bits_per_pixel()".
|
||||
dst_pixfmt = args.dst.pixel_format()
|
||||
dst_bits_per_pixel = dst_pixfmt.bits_per_pixel()
|
||||
if (dst_bits_per_pixel & 7) <> 0 {
|
||||
return base."#unsupported option"
|
||||
}
|
||||
dst_bytes_per_pixel = (dst_bits_per_pixel / 8) as base.u64
|
||||
|
||||
// TODO: be more efficient than reading one byte at a time.
|
||||
if this.width > 0 {
|
||||
tab = args.dst.plane(p: 0)
|
||||
while dst_y < this.height {
|
||||
assert dst_y < 0xFFFF_FFFF via "a < b: a < c; c <= b"(c: this.height)
|
||||
dst = tab.row_u32(y: dst_y)
|
||||
dst_x = 0
|
||||
// TODO: be more efficient than reading one byte at a time.
|
||||
if this.width > 0 {
|
||||
tab = args.dst.plane(p: 0)
|
||||
while dst_y < this.height {
|
||||
assert dst_y < 0xFFFF_FFFF via "a < b: a < c; c <= b"(c: this.height)
|
||||
dst = tab.row_u32(y: dst_y)
|
||||
dst_x = 0
|
||||
|
||||
while dst_x < this.width,
|
||||
inv dst_y < 0xFFFF_FFFF,
|
||||
{
|
||||
assert dst_x < 0xFFFF_FFFF via "a < b: a < c; c <= b"(c: this.width)
|
||||
while dst_x < this.width,
|
||||
inv dst_y < 0xFFFF_FFFF,
|
||||
{
|
||||
assert dst_x < 0xFFFF_FFFF via "a < b: a < c; c <= b"(c: this.width)
|
||||
|
||||
if (dst_x & 7) == 0 {
|
||||
while args.src.length() <= 0,
|
||||
inv dst_x < 0xFFFF_FFFF,
|
||||
inv dst_y < 0xFFFF_FFFF,
|
||||
post args.src.length() > 0,
|
||||
{
|
||||
yield? base."$short read"
|
||||
tab = args.dst.plane(p: 0)
|
||||
dst = tab.row_u32(y: dst_y)
|
||||
dst_x_in_bytes = (dst_x as base.u64) * dst_bytes_per_pixel
|
||||
if dst_x_in_bytes <= dst.length() {
|
||||
dst = dst[dst_x_in_bytes ..]
|
||||
}
|
||||
} endwhile
|
||||
c = args.src.peek_u8()
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
}
|
||||
if (c & 0x80) == 0 {
|
||||
src[0] = 0x00
|
||||
} else {
|
||||
src[0] = 0xFF
|
||||
}
|
||||
// TODO: this should just be "c ~mod<<= 1", but that generates:
|
||||
//
|
||||
// error: conversion to ‘uint8_t {aka unsigned char}’ from
|
||||
// ‘int’ may alter its value [-Werror=conversion]
|
||||
// v_c <<= 1;
|
||||
c = (((c as base.u32) << 1) & 0xFF) as base.u8
|
||||
if (dst_x & 7) == 0 {
|
||||
while args.src.length() <= 0,
|
||||
inv dst_x < 0xFFFF_FFFF,
|
||||
inv dst_y < 0xFFFF_FFFF,
|
||||
post args.src.length() > 0,
|
||||
{
|
||||
yield? base."$short read"
|
||||
tab = args.dst.plane(p: 0)
|
||||
dst = tab.row_u32(y: dst_y)
|
||||
dst_x_in_bytes = (dst_x as base.u64) * dst_bytes_per_pixel
|
||||
if dst_x_in_bytes <= dst.length() {
|
||||
dst = dst[dst_x_in_bytes ..]
|
||||
}
|
||||
} endwhile
|
||||
c = args.src.peek_u8()
|
||||
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
|
||||
}
|
||||
if (c & 0x80) == 0 {
|
||||
src[0] = 0x00
|
||||
} else {
|
||||
src[0] = 0xFF
|
||||
}
|
||||
// TODO: this should just be "c ~mod<<= 1", but that generates:
|
||||
//
|
||||
// error: conversion to ‘uint8_t {aka unsigned char}’ from
|
||||
// ‘int’ may alter its value [-Werror=conversion]
|
||||
// v_c <<= 1;
|
||||
c = (((c as base.u32) << 1) & 0xFF) as base.u8
|
||||
|
||||
this.swizzler.swizzle_interleaved_from_slice!(
|
||||
dst: dst, dst_palette: this.util.empty_slice_u8(), src: src[..])
|
||||
this.swizzler.swizzle_interleaved_from_slice!(
|
||||
dst: dst, dst_palette: this.util.empty_slice_u8(), src: src[..])
|
||||
|
||||
if dst_bytes_per_pixel <= dst.length() {
|
||||
dst = dst[dst_bytes_per_pixel ..]
|
||||
}
|
||||
if dst_bytes_per_pixel <= dst.length() {
|
||||
dst = dst[dst_bytes_per_pixel ..]
|
||||
}
|
||||
|
||||
dst_x += 1
|
||||
} endwhile
|
||||
dst_y += 1
|
||||
} endwhile
|
||||
}
|
||||
dst_x += 1
|
||||
} endwhile
|
||||
dst_y += 1
|
||||
} endwhile
|
||||
}
|
||||
|
||||
this.call_sequence = 0x60
|
||||
this.call_sequence = 0x60
|
||||
}
|
||||
|
||||
pub func decoder.frame_dirty_rect() base.rect_ie_u32 {
|
||||
return this.util.make_rect_ie_u32(
|
||||
min_incl_x: 0,
|
||||
min_incl_y: 0,
|
||||
max_excl_x: this.width,
|
||||
max_excl_y: this.height)
|
||||
return this.util.make_rect_ie_u32(
|
||||
min_incl_x: 0,
|
||||
min_incl_y: 0,
|
||||
max_excl_x: this.width,
|
||||
max_excl_y: this.height)
|
||||
}
|
||||
|
||||
pub func decoder.num_animation_loops() base.u32 {
|
||||
return 0
|
||||
return 0
|
||||
}
|
||||
|
||||
pub func decoder.num_decoded_frame_configs() base.u64 {
|
||||
if this.call_sequence > 0x20 {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
if this.call_sequence > 0x20 {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
pub func decoder.num_decoded_frames() base.u64 {
|
||||
if this.call_sequence > 0x40 {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
if this.call_sequence > 0x40 {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
pub func decoder.restart_frame!(index: base.u64, io_position: base.u64) base.status {
|
||||
if this.call_sequence < 0x20 {
|
||||
return base."#bad call sequence"
|
||||
}
|
||||
if args.index <> 0 {
|
||||
return base."#bad argument"
|
||||
}
|
||||
this.call_sequence = 0x28
|
||||
this.frame_config_io_position = args.io_position
|
||||
return ok
|
||||
if this.call_sequence < 0x20 {
|
||||
return base."#bad call sequence"
|
||||
}
|
||||
if args.index <> 0 {
|
||||
return base."#bad argument"
|
||||
}
|
||||
this.call_sequence = 0x28
|
||||
this.frame_config_io_position = args.io_position
|
||||
return ok
|
||||
}
|
||||
|
||||
pub func decoder.set_report_metadata!(fourcc: base.u32, report: base.bool) {
|
||||
// No-op. WBMP doesn't support metadata.
|
||||
// No-op. WBMP doesn't support metadata.
|
||||
}
|
||||
|
||||
pub func decoder.tell_me_more?(dst: base.io_writer, minfo: nptr base.more_information, src: base.io_reader) {
|
||||
return base."#no more information"
|
||||
return base."#no more information"
|
||||
}
|
||||
|
||||
pub func decoder.workbuf_len() base.range_ii_u64 {
|
||||
return this.util.make_range_ii_u64(min_incl: 0, max_incl: 0)
|
||||
return this.util.make_range_ii_u64(min_incl: 0, max_incl: 0)
|
||||
}
|
||||
+96
-96
@@ -28,130 +28,130 @@ pub status "#truncated input"
|
||||
pub const DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE : base.u64 = 1
|
||||
|
||||
pub struct decoder? implements base.io_transformer(
|
||||
bad_call_sequence : base.bool,
|
||||
header_complete : base.bool,
|
||||
bad_call_sequence : base.bool,
|
||||
header_complete : base.bool,
|
||||
|
||||
got_dictionary : base.bool,
|
||||
want_dictionary : base.bool,
|
||||
got_dictionary : base.bool,
|
||||
want_dictionary : base.bool,
|
||||
|
||||
quirks : array[QUIRKS_COUNT] base.bool,
|
||||
quirks : array[QUIRKS_COUNT] base.bool,
|
||||
|
||||
ignore_checksum : base.bool,
|
||||
checksum : adler32.hasher,
|
||||
ignore_checksum : base.bool,
|
||||
checksum : adler32.hasher,
|
||||
|
||||
dict_id_hasher : adler32.hasher,
|
||||
dict_id_got : base.u32,
|
||||
dict_id_want : base.u32,
|
||||
dict_id_hasher : adler32.hasher,
|
||||
dict_id_got : base.u32,
|
||||
dict_id_want : base.u32,
|
||||
|
||||
flate : deflate.decoder,
|
||||
flate : deflate.decoder,
|
||||
|
||||
util : base.utility,
|
||||
util : base.utility,
|
||||
)
|
||||
|
||||
pub func decoder.dictionary_id() base.u32 {
|
||||
return this.dict_id_want
|
||||
return this.dict_id_want
|
||||
}
|
||||
|
||||
pub func decoder.add_dictionary!(dict: slice base.u8) {
|
||||
if this.header_complete {
|
||||
this.bad_call_sequence = true
|
||||
} else {
|
||||
this.dict_id_got = this.dict_id_hasher.update_u32!(x: args.dict)
|
||||
this.flate.add_history!(hist: args.dict)
|
||||
}
|
||||
this.got_dictionary = true
|
||||
if this.header_complete {
|
||||
this.bad_call_sequence = true
|
||||
} else {
|
||||
this.dict_id_got = this.dict_id_hasher.update_u32!(x: args.dict)
|
||||
this.flate.add_history!(hist: args.dict)
|
||||
}
|
||||
this.got_dictionary = true
|
||||
}
|
||||
|
||||
pub func decoder.set_quirk_enabled!(quirk: base.u32, enabled: base.bool) {
|
||||
if this.header_complete {
|
||||
this.bad_call_sequence = true
|
||||
} else if args.quirk == base.QUIRK_IGNORE_CHECKSUM {
|
||||
this.ignore_checksum = args.enabled
|
||||
} else if args.quirk >= QUIRKS_BASE {
|
||||
args.quirk -= QUIRKS_BASE
|
||||
if args.quirk < QUIRKS_COUNT {
|
||||
this.quirks[args.quirk] = args.enabled
|
||||
}
|
||||
}
|
||||
if this.header_complete {
|
||||
this.bad_call_sequence = true
|
||||
} else if args.quirk == base.QUIRK_IGNORE_CHECKSUM {
|
||||
this.ignore_checksum = args.enabled
|
||||
} else if args.quirk >= QUIRKS_BASE {
|
||||
args.quirk -= QUIRKS_BASE
|
||||
if args.quirk < QUIRKS_COUNT {
|
||||
this.quirks[args.quirk] = args.enabled
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub func decoder.workbuf_len() base.range_ii_u64 {
|
||||
return this.util.make_range_ii_u64(
|
||||
min_incl: DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE,
|
||||
max_incl: DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE)
|
||||
return this.util.make_range_ii_u64(
|
||||
min_incl: DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE,
|
||||
max_incl: DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE)
|
||||
}
|
||||
|
||||
pub func decoder.transform_io?(dst: base.io_writer, src: base.io_reader, workbuf: slice base.u8) {
|
||||
var status : base.status
|
||||
var status : base.status
|
||||
|
||||
while true {
|
||||
status =? this.do_transform_io?(dst: args.dst, src: args.src, workbuf: args.workbuf)
|
||||
if (status == base."$short read") and args.src.is_closed() {
|
||||
return "#truncated input"
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
while true {
|
||||
status =? this.do_transform_io?(dst: args.dst, src: args.src, workbuf: args.workbuf)
|
||||
if (status == base."$short read") and args.src.is_closed() {
|
||||
return "#truncated input"
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
}
|
||||
|
||||
pri func decoder.do_transform_io?(dst: base.io_writer, src: base.io_reader, workbuf: slice base.u8) {
|
||||
var x : base.u16
|
||||
var checksum_got : base.u32
|
||||
var status : base.status
|
||||
var checksum_want : base.u32
|
||||
var mark : base.u64
|
||||
var x : base.u16
|
||||
var checksum_got : base.u32
|
||||
var status : base.status
|
||||
var checksum_want : base.u32
|
||||
var mark : base.u64
|
||||
|
||||
if this.bad_call_sequence {
|
||||
return base."#bad call sequence"
|
||||
} else if this.quirks[QUIRK_JUST_RAW_DEFLATE - QUIRKS_BASE] {
|
||||
// No-op.
|
||||
} else if not this.want_dictionary {
|
||||
x = args.src.read_u16be?()
|
||||
if ((x >> 8) & 0x0F) <> 0x08 {
|
||||
return "#bad compression method"
|
||||
}
|
||||
if (x >> 12) > 0x07 {
|
||||
return "#bad compression window size"
|
||||
}
|
||||
if (x % 31) <> 0 {
|
||||
return "#bad parity check"
|
||||
}
|
||||
this.want_dictionary = (x & 0x20) <> 0
|
||||
if this.want_dictionary {
|
||||
this.dict_id_got = 1 // Adler-32 initial value.
|
||||
this.dict_id_want = args.src.read_u32be?()
|
||||
return "@dictionary required"
|
||||
} else if this.got_dictionary {
|
||||
return "#incorrect dictionary"
|
||||
}
|
||||
} else if this.dict_id_got <> this.dict_id_want {
|
||||
if this.got_dictionary {
|
||||
return "#incorrect dictionary"
|
||||
}
|
||||
return "@dictionary required"
|
||||
}
|
||||
if this.bad_call_sequence {
|
||||
return base."#bad call sequence"
|
||||
} else if this.quirks[QUIRK_JUST_RAW_DEFLATE - QUIRKS_BASE] {
|
||||
// No-op.
|
||||
} else if not this.want_dictionary {
|
||||
x = args.src.read_u16be?()
|
||||
if ((x >> 8) & 0x0F) <> 0x08 {
|
||||
return "#bad compression method"
|
||||
}
|
||||
if (x >> 12) > 0x07 {
|
||||
return "#bad compression window size"
|
||||
}
|
||||
if (x % 31) <> 0 {
|
||||
return "#bad parity check"
|
||||
}
|
||||
this.want_dictionary = (x & 0x20) <> 0
|
||||
if this.want_dictionary {
|
||||
this.dict_id_got = 1 // Adler-32 initial value.
|
||||
this.dict_id_want = args.src.read_u32be?()
|
||||
return "@dictionary required"
|
||||
} else if this.got_dictionary {
|
||||
return "#incorrect dictionary"
|
||||
}
|
||||
} else if this.dict_id_got <> this.dict_id_want {
|
||||
if this.got_dictionary {
|
||||
return "#incorrect dictionary"
|
||||
}
|
||||
return "@dictionary required"
|
||||
}
|
||||
|
||||
this.header_complete = true
|
||||
this.header_complete = true
|
||||
|
||||
// Decode and checksum the DEFLATE-encoded payload.
|
||||
while true {
|
||||
mark = args.dst.mark()
|
||||
status =? this.flate.transform_io?(dst: args.dst, src: args.src, workbuf: args.workbuf)
|
||||
if (not this.ignore_checksum) and (not this.quirks[QUIRK_JUST_RAW_DEFLATE - QUIRKS_BASE]) {
|
||||
checksum_got = this.checksum.update_u32!(x: args.dst.since(mark: mark))
|
||||
}
|
||||
if status.is_ok() {
|
||||
break
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
// Decode and checksum the DEFLATE-encoded payload.
|
||||
while true {
|
||||
mark = args.dst.mark()
|
||||
status =? this.flate.transform_io?(dst: args.dst, src: args.src, workbuf: args.workbuf)
|
||||
if (not this.ignore_checksum) and (not this.quirks[QUIRK_JUST_RAW_DEFLATE - QUIRKS_BASE]) {
|
||||
checksum_got = this.checksum.update_u32!(x: args.dst.since(mark: mark))
|
||||
}
|
||||
if status.is_ok() {
|
||||
break
|
||||
}
|
||||
yield? status
|
||||
} endwhile
|
||||
|
||||
if not this.quirks[QUIRK_JUST_RAW_DEFLATE - QUIRKS_BASE] {
|
||||
checksum_want = args.src.read_u32be?()
|
||||
if (not this.ignore_checksum) and (checksum_got <> checksum_want) {
|
||||
return "#bad checksum"
|
||||
}
|
||||
}
|
||||
if not this.quirks[QUIRK_JUST_RAW_DEFLATE - QUIRKS_BASE] {
|
||||
checksum_want = args.src.read_u32be?()
|
||||
if (not this.ignore_checksum) and (checksum_got <> checksum_want) {
|
||||
return "#bad checksum"
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: reset state (e.g. want_dictionary), so that we can read concat'ed
|
||||
// zlib streams?
|
||||
// TODO: reset state (e.g. want_dictionary), so that we can read concat'ed
|
||||
// zlib streams?
|
||||
}
|
||||
Reference in new issue
Block a user