Files
wuffs/std/brotli/encode_util.wuffs
T

117 lines
3.4 KiB
Plaintext

// Copyright 2026 The Wuffs Authors.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//
// SPDX-License-Identifier: Apache-2.0 OR MIT
// --------
// This file holds small helpers for the quality 2 (and above) code paths,
// which keep most of their state (the ring buffer, the hash tables, the
// commands, the output storage and the metablock split) in the workbuf. Like
// the C code's uint16_t and uint32_t arrays, multi-byte values in the workbuf
// are little-endian. The C code never reads or writes out of bounds, so the
// out of bounds branches here (which do nothing, or return 0) should never be
// taken.
// byte_at returns s[i], or 0 if i is out of bounds.
pri func encoder.byte_at(s: roslice base.u8, i: base.u64) base.u8 {
if args.i < args.s.length() {
return args.s[args.i]
}
return 0
}
// peek_u16 returns the 2 bytes (little-endian) of s starting at i.
pri func encoder.peek_u16(s: roslice base.u8, i: base.u64) base.u16 {
var q : roslice base.u8
if args.i < args.s.length() {
q = args.s[args.i ..]
if q.length() >= 2 {
return q.peek_u16le()
}
}
return 0
}
// poke_u16 sets the 2 bytes (little-endian) of s starting at i.
pri func encoder.poke_u16!(s: slice base.u8, i: base.u64, v: base.u16) {
var q : slice base.u8
if args.i < args.s.length() {
q = args.s[args.i ..]
if q.length() >= 2 {
q.poke_u16le!(a: args.v)
}
}
}
// poke_u32 sets the 4 bytes (little-endian) of s starting at i.
pri func encoder.poke_u32!(s: slice base.u8, i: base.u64, v: base.u32) {
var q : slice base.u8
if args.i < args.s.length() {
q = args.s[args.i ..]
if q.length() >= 4 {
q.poke_u32le!(a: args.v)
}
}
}
// set_byte sets s[i] to v, if i is in bounds.
pri func encoder.set_byte!(s: slice base.u8, i: base.u64, v: base.u8) {
if args.i < args.s.length() {
args.s[args.i] = args.v
}
}
// fill_bytes sets s[i .. i + n] to v (as far as it is in bounds).
pri func encoder.fill_bytes!(s: slice base.u8, i: base.u64, n: base.u64, v: base.u8) {
var q : slice base.u8
var n : base.u64
if args.i <= args.s.length() {
q = args.s[args.i ..]
if args.n <= q.length() {
q[.. args.n].bulk_memset!(byte_value: args.v)
} else {
n = q.length()
assert n <= q.length() via "a <= b: a == b"()
q[.. n].bulk_memset!(byte_value: args.v)
}
}
}
// dist_cache_u64 returns the C code's (size_t)dist_cache[i]: the int value,
// sign-extended to 64 bits.
pri func encoder.dist_cache_u64(i: base.u32) base.u64 {
var v : base.u32
v = this.dist_cache[args.i & 15]
if v >= 0x8000_0000 {
return (v as base.u64) | 0xFFFF_FFFF_0000_0000
}
return v as base.u64
}
// min64 returns the smaller of a and b.
pri func encoder.min64(a: base.u64, b: base.u64) base.u64 {
if args.a < args.b {
return args.a
}
return args.b
}
// max64 returns the larger of a and b.
pri func encoder.max64(a: base.u64, b: base.u64) base.u64 {
if args.a > args.b {
return args.a
}
return args.b
}