ws-cleanup: one base64 (core/Base64)

core/Base64 takes over codecs/Base64's API (encode(buf | string, { url, pad, wrap }, offset, length), decode of both
alphabets, decodeDataUri, dataUri) plus line wrapping (opts.wrap) for plist; codecs/Base64 is removed and glTF /
DAE require core/Base64. The private copies in data/plist (Base64.luau), data/yaml (Parse / Emit) and codecs/Brotli
(the static tables) are gone. tests/codecs/base64 merged into tests/core/base64.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
4DBugandClaude Opus 5.5 committed 2026-10-31 10:50:40 -05:00
1 parent a289eba8af
commit a25b7c0caa
18 files changed
+202 -410

No files matched your search

-140
View File
@@ -1,140 +0,0 @@
--!strict
--!native
-- codecs/Base64: RFC 4648 base64 (standard alphabet, and base64url with opts.url) encode / decode, for data: URIs
-- (glTF, SVG, CSS), PEM-like text, YAML !!binary, ... New code. decode skips whitespace, accepts missing padding and
-- both alphabets; any other character is an error. Never throws.
--
-- local Base64 = require("@formats/codecs/Base64")
-- local s = Base64.encode(buf) -- string ("" for an empty buffer)
-- local b, err = Base64.decode(s) -- buffer | nil, "message"
-- local b2 = Base64.decodeDataUri("data:application/octet-stream;base64,AAEC") -- buffer, mime | nil, message
local Base64 = {}
local ALPHA = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
local ALPHA_URL = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
local ENC = buffer.fromstring(ALPHA)
local ENC_URL = buffer.fromstring(ALPHA_URL)
-- the decode table: 0..63, 64 = skip (whitespace), 65 = padding, 255 = bad
local DEC = buffer.create(256)
buffer.fill(DEC, 0, 255)
for i = 1, 64 do
buffer.writeu8(DEC, string.byte(ALPHA, i), i - 1)
end
buffer.writeu8(DEC, string.byte("-"), 62)
buffer.writeu8(DEC, string.byte("_"), 63)
for _, c in { 9, 10, 13, 32 } do
buffer.writeu8(DEC, c, 64)
end
buffer.writeu8(DEC, string.byte("="), 65)
local readu8, writeu8 = buffer.readu8, buffer.writeu8
-- encode(buf, opts?, offset?, length?) -> string. opts.url: the URL-safe alphabet; opts.pad = false: no "=" padding.
function Base64.encode(buf: buffer, opts: { url: boolean?, pad: boolean? }?, offset: number?, length: number?): string
local o = math.clamp(offset or 0, 0, buffer.len(buf))
local n = math.clamp(length or (buffer.len(buf) - o), 0, buffer.len(buf) - o)
local enc = if opts and opts.url then ENC_URL else ENC
local pad = not (opts and opts.pad == false)
local full = n // 3
local rest = n - full * 3
local outLen = full * 4 + (if rest == 0 then 0 elseif pad then 4 else rest + 1)
local out = buffer.create(outLen)
local p = 0
for i = 0, full - 1 do
local q = o + i * 3
local v = readu8(buf, q) * 65536 + readu8(buf, q + 1) * 256 + readu8(buf, q + 2)
writeu8(out, p, readu8(enc, v // 262144))
writeu8(out, p + 1, readu8(enc, (v // 4096) % 64))
writeu8(out, p + 2, readu8(enc, (v // 64) % 64))
writeu8(out, p + 3, readu8(enc, v % 64))
p += 4
end
if rest > 0 then
local q = o + full * 3
local v = readu8(buf, q) * 65536 + (if rest == 2 then readu8(buf, q + 1) * 256 else 0)
writeu8(out, p, readu8(enc, v // 262144))
writeu8(out, p + 1, readu8(enc, (v // 4096) % 64))
if rest == 2 then
writeu8(out, p + 2, readu8(enc, (v // 64) % 64))
elseif pad then
writeu8(out, p + 2, 61)
end
if pad then
writeu8(out, p + 3, 61)
end
end
return buffer.tostring(out)
end
-- decode(s) -> buffer | nil, message
function Base64.decode(s: string | buffer): (buffer?, string?)
local src = if type(s) == "string" then buffer.fromstring(s) else s
local n = buffer.len(src)
local out = buffer.create((n // 4 + 1) * 3)
local acc, bits, p = 0, 0, 0
local padded = false
for i = 0, n - 1 do
local d = readu8(DEC, readu8(src, i))
if d < 64 then
if padded then
return nil, "data after padding"
end
acc = acc * 64 + d
bits += 6
if bits >= 8 then
bits -= 8
local byte = acc // (2 ^ bits)
writeu8(out, p, byte % 256)
p += 1
acc = acc % (2 ^ bits)
end
elseif d == 65 then
padded = true
elseif d ~= 64 then
return nil, string.format("bad base64 character 0x%02x at %d", readu8(src, i), i)
end
end
if bits >= 6 then
return nil, "truncated base64 (a single character in the last group)"
end
local res = buffer.create(p)
buffer.copy(res, 0, out, 0, p)
return res, nil
end
-- decodeDataUri(uri) -> buffer, mime | nil, message: "data:[<mime>][;params][;base64],<data>" (percent-encoded data
-- when not base64)
function Base64.decodeDataUri(uri: string): (buffer?, string?)
if string.sub(uri, 1, 5) ~= "data:" then
return nil, "not a data: URI"
end
local comma = string.find(uri, ",", 6, true)
if not comma then
return nil, "data: URI without a comma"
end
local head = string.sub(uri, 6, comma - 1)
local isB64 = string.sub(head, -7) == ";base64"
local mime = string.match(head, "^([^;]*)") or ""
if mime == "" then
mime = "text/plain"
end
local body = string.sub(uri, comma + 1)
if isB64 then
local b, err = Base64.decode(body)
if not b then
return nil, err
end
return b, mime
end
local decoded = string.gsub(body, "%%(%x%x)", function(h: string): string
return string.char(tonumber(h, 16) :: number)
end)
return buffer.fromstring(decoded), mime
end
-- dataUri(buf, mime) -> "data:<mime>;base64,..."
function Base64.dataUri(buf: buffer, mime: string?): string
return "data:" .. (mime or "application/octet-stream") .. ";base64," .. Base64.encode(buf)
end
return Base64
+3 -30
View File
@@ -13,6 +13,7 @@
-- Never throws: bad data and bad arguments return nil, Err (TRUNCATED / CORRUPT / LIMIT / UNSUPPORTED / ARG).
local Err = require("@formats/core/Err")
local Crc32 = require("@formats/core/Crc32")
local Base64 = require("@formats/core/Base64")
local Deflate = require("@formats/codecs/Deflate")
local Decode = require("@formats/codecs/Brotli/Decode")
local StaticData = require("@formats/codecs/Brotli/Static")
@@ -25,37 +26,9 @@ export type Options = {
}
-- ------------------------------------------------------------------------------------------------ static data
local B64 = {}
do
local alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
for i = 1, 64 do
B64[string.byte(alphabet, i)] = i - 1
end
end
-- the tables are base64 text (trusted, generated by tools/brotli_static.py)
local function unbase64(s: string): buffer
local n = #s
local pad = 0
if string.sub(s, -2) == "==" then
pad = 2
elseif string.sub(s, -1) == "=" then
pad = 1
end
local out = buffer.create(n // 4 * 3 - pad)
local o = 0
for i = 1, n, 4 do
local a, b, c, d = string.byte(s, i, i + 3)
local v = B64[a] * 262144 + B64[b] * 4096 + (B64[c] or 0) * 64 + (B64[d] or 0)
buffer.writeu8(out, o, v // 65536)
if o + 1 < buffer.len(out) then
buffer.writeu8(out, o + 1, (v // 256) % 256)
end
if o + 2 < buffer.len(out) then
buffer.writeu8(out, o + 2, v % 256)
end
o += 3
end
return out
return Base64.decode(s) or buffer.create(0)
end
local static: Decode.Static? = nil
+214
View File
@@ -0,0 +1,214 @@
--!strict
--!native
--!optimize 2
-- audio/opus/RangeDecoder: Range decoder (RFC 6716 section 4.1, libopus celt/entdec.c). Reads one Opus frame from a `buffer` slice
-- [base, base + storage). Raw bits (ec_dec_bits) are read from the end of the slice backwards.
-- All values stay below 2^32, so plain Luau numbers hold them exactly; floor division is exact below 2^52.
local RangeDecoder = {}
export type Dec = {
buf: buffer,
base: number,
storage: number,
offs: number,
end_offs: number,
end_window: number,
nend_bits: number,
nbits_total: number,
rng: number,
val: number,
rem: number,
ext: number,
error: number,
}
local SYM_BITS = 8
local CODE_BITS = 32
local CODE_TOP = 2147483648 -- 1 << 31
local CODE_BOT = 8388608 -- CODE_TOP >> 8
local CODE_EXTRA = 7 -- (CODE_BITS - 2) % SYM_BITS + 1
local SYM_MAX = 255
local UINT_BITS = 8
local WINDOW_SIZE = 32
local countlz = bit32.countlz
local band, bor, lshift, rshift = bit32.band, bit32.bor, bit32.lshift, bit32.rshift
local readu8 = buffer.readu8
local function ilog(x: number): number
return 32 - countlz(x)
end
RangeDecoder.ilog = ilog
local function normalize(d: Dec)
local rng, val, rem = d.rng, d.val, d.rem
while rng <= CODE_BOT do
d.nbits_total += SYM_BITS
rng = rng * 256
local sym = rem
local offs = d.offs
if offs < d.storage then
rem = readu8(d.buf, d.base + offs)
d.offs = offs + 1
else
rem = 0
end
sym = rshift(bor(lshift(sym, 8), rem), SYM_BITS - CODE_EXTRA) -- (sym << 8 | rem) >> 1
val = band(val * 256 + band(SYM_MAX, bit32.bnot(sym)), CODE_TOP - 1)
end
d.rng, d.val, d.rem = rng, val, rem
end
function RangeDecoder.new(buf: buffer, base: number, storage: number): Dec
local d: Dec = {
buf = buf,
base = base,
storage = storage,
offs = 0,
end_offs = 0,
end_window = 0,
nend_bits = 0,
nbits_total = CODE_BITS + 1 - ((CODE_BITS - CODE_EXTRA) // SYM_BITS) * SYM_BITS,
rng = 128, -- 1 << CODE_EXTRA
val = 0,
rem = 0,
ext = 0,
error = 0,
}
local rem = 0
if storage > 0 then
rem = readu8(buf, base)
d.offs = 1
end
d.rem = rem
d.val = d.rng - 1 - rshift(rem, SYM_BITS - CODE_EXTRA)
normalize(d)
return d
end
-- ec_decode: the cumulative frequency of the next symbol, for a total of ft
function RangeDecoder.decode(d: Dec, ft: number): number
local ext = d.rng // ft
d.ext = ext
local s = d.val // ext
return ft - math.min(s + 1, ft)
end
function RangeDecoder.decodeBin(d: Dec, bits: number): number
local ext = rshift(d.rng, bits)
d.ext = ext
local s = d.val // ext
local ft = lshift(1, bits)
return ft - math.min(s + 1, ft)
end
function RangeDecoder.update(d: Dec, fl: number, fh: number, ft: number)
local s = d.ext * (ft - fh)
d.val = band(d.val - s, 0xFFFFFFFF) -- uint32 wrap (corrupt streams only)
if fl > 0 then
d.rng = d.ext * (fh - fl)
else
d.rng -= s
end
normalize(d)
end
function RangeDecoder.bitLogp(d: Dec, logp: number): boolean
local r = d.rng
local v = d.val
local s = rshift(r, logp)
local ret = v < s
if ret then
d.rng = s
else
d.val = v - s
d.rng = r - s
end
normalize(d)
return ret
end
-- ec_dec_icdf over a Luau array (1-based) starting at index `off` (icdf[off] is entry 0)
function RangeDecoder.icdf(d: Dec, icdf: { number }, off: number, ftb: number): number
local s = d.rng
local v = d.val
local r = rshift(s, ftb)
local ret = -1
local t
repeat
t = s
ret += 1
s = r * icdf[off + ret]
until v >= s
d.val = v - s
d.rng = t - s
normalize(d)
return ret
end
function RangeDecoder.bits(d: Dec, bits: number): number
local window = d.end_window
local available = d.nend_bits
if available < bits then
repeat
local b = 0
local eo = d.end_offs
if eo < d.storage then
eo += 1
d.end_offs = eo
b = readu8(d.buf, d.base + d.storage - eo)
end
window = bor(window, lshift(b, available))
available += SYM_BITS
until available > WINDOW_SIZE - SYM_BITS
end
local ret = band(window, lshift(1, bits) - 1)
if bits == 32 then
ret = window
end
window = rshift(window, bits)
available -= bits
d.end_window = window
d.nend_bits = available
d.nbits_total += bits
return ret
end
function RangeDecoder.uint(d: Dec, ft0: number): number
local ft = ft0 - 1
local ftb = ilog(ft)
if ftb > UINT_BITS then
ftb -= UINT_BITS
local ft1 = rshift(ft, ftb) + 1
local s = RangeDecoder.decode(d, ft1)
RangeDecoder.update(d, s, s + 1, ft1)
local t = bor(lshift(s, ftb), RangeDecoder.bits(d, ftb))
if t <= ft then
return t
end
d.error = 1
return ft
else
local s = RangeDecoder.decode(d, ft0)
RangeDecoder.update(d, s, s + 1, ft0)
return s
end
end
function RangeDecoder.tell(d: Dec): number
return d.nbits_total - ilog(d.rng)
end
local correction = { 35733, 38967, 42495, 46340, 50535, 55109, 60097, 65535 }
function RangeDecoder.tellFrac(d: Dec): number
local nbits = d.nbits_total * 8
local l = ilog(d.rng)
local r = rshift(d.rng, l - 16)
local b = rshift(r, 12) - 8
if r > correction[b + 1] then
b += 1
end
return nbits - (l * 8 + b)
end
return RangeDecoder
+252
View File
@@ -0,0 +1,252 @@
--!strict
--!native
--!optimize 2
-- audio/opus/RangeEncoder: the range encoder (RFC 6716 section 5.1), a port of libopus celt/entenc.c (BSD:
-- licenses/libopus-BSD.txt), the mirror of audio/opus/RangeDecoder. Writes one fixed-size frame: the range-coded
-- symbols from the front, the raw bits (ec_enc_bits) from the back, zeros between. Values stay below 2^32 as plain
-- numbers. Never throws: running out of room sets `error`.
local RangeEncoder = {}
export type Enc = {
buf: buffer,
storage: number,
offs: number,
end_offs: number,
end_window: number,
nend_bits: number,
nbits_total: number,
rng: number,
val: number,
rem: number,
ext: number,
error: number,
}
local CODE_TOP = 2147483648 -- 1 << 31
local CODE_BOT = 8388608 -- CODE_TOP >> 8
local CODE_SHIFT = 23
local SYM_MAX = 255
local countlz = bit32.countlz
local band = bit32.band
local readu8, writeu8 = buffer.readu8, buffer.writeu8
local floor = math.floor
local function ilog(x: number): number
return 32 - countlz(x)
end
RangeEncoder.ilog = ilog
-- new(storage) -> Enc over a fresh zeroed buffer of storage bytes
function RangeEncoder.new(storage: number): Enc
return {
buf = buffer.create(math.max(storage, 1)),
storage = storage,
offs = 0,
end_offs = 0,
end_window = 0,
nend_bits = 0,
nbits_total = 33, -- EC_CODE_BITS + 1
rng = CODE_TOP,
val = 0,
rem = -1,
ext = 0,
error = 0,
}
end
local function writeByte(e: Enc, v: number)
if e.offs + e.end_offs >= e.storage then
e.error = -1
return
end
writeu8(e.buf, e.offs, band(v, 255))
e.offs += 1
end
local function writeByteAtEnd(e: Enc, v: number)
if e.offs + e.end_offs >= e.storage then
e.error = -1
return
end
e.end_offs += 1
writeu8(e.buf, e.storage - e.end_offs, band(v, 255))
end
local function carryOut(e: Enc, c: number)
if c ~= SYM_MAX then
local carry = if c >= 256 then 1 else 0
if e.rem >= 0 then
writeByte(e, e.rem + carry)
end
if e.ext > 0 then
local sym = band(SYM_MAX + carry, 255)
repeat
writeByte(e, sym)
e.ext -= 1
until e.ext <= 0
end
e.rem = band(c, 255)
else
e.ext += 1
end
end
local function normalize(e: Enc)
while e.rng <= CODE_BOT do
carryOut(e, floor(e.val / 8388608))
e.val = (e.val * 256) % CODE_TOP
e.rng *= 256
e.nbits_total += 8
end
end
-- encode(fl, fh, ft): the symbol [fl, fh) of ft
function RangeEncoder.encode(e: Enc, fl: number, fh: number, ft: number)
local r = floor(e.rng / ft)
if fl > 0 then
e.val += e.rng - r * (ft - fl)
e.rng = r * (fh - fl)
else
e.rng -= r * (ft - fh)
end
normalize(e)
end
-- encodeBin(fl, fh, bits): ft = 2^bits
function RangeEncoder.encodeBin(e: Enc, fl: number, fh: number, bits: number)
local ft = 2 ^ bits
local r = floor(e.rng / ft)
if fl > 0 then
e.val += e.rng - r * (ft - fl)
e.rng = r * (fh - fl)
else
e.rng -= r * (ft - fh)
end
normalize(e)
end
-- bitLogp(v, logp): a bit whose probability of being 1 is 2^-logp
function RangeEncoder.bitLogp(e: Enc, v: boolean, logp: number)
local r = e.rng
local s = floor(r / 2 ^ logp)
r -= s
if v then
e.val += r
e.rng = s
else
e.rng = r
end
normalize(e)
end
-- icdf(s, icdf, ftb): symbol s (0-based) of an inverse CDF table (1-based Luau array) with 2^ftb total
function RangeEncoder.icdf(e: Enc, s: number, icdf: { number }, ftb: number)
local r = floor(e.rng / 2 ^ ftb)
if s > 0 then
e.val += e.rng - r * icdf[s]
e.rng = r * (icdf[s] - icdf[s + 1])
else
e.rng -= r * icdf[s + 1]
end
normalize(e)
end
-- bits(fl, n): n raw bits (n <= 25), written from the end of the frame
function RangeEncoder.bits(e: Enc, fl: number, n: number)
local window = e.end_window
local used = e.nend_bits
if used + n > 32 then
repeat
writeByteAtEnd(e, window % 256)
window = floor(window / 256)
used -= 8
until used < 8
end
window += fl * 2 ^ used
used += n
e.end_window = window
e.nend_bits = used
e.nbits_total += n
end
-- uint(fl, ft): fl uniformly in [0, ft), ft up to 2^32
function RangeEncoder.uint(e: Enc, fl: number, ft: number)
ft -= 1
local ftb = ilog(ft)
if ftb > 8 then
ftb -= 8
local sh = 2 ^ ftb
local ft1 = floor(ft / sh) + 1
local hi = floor(fl / sh)
RangeEncoder.encode(e, hi, hi + 1, ft1)
RangeEncoder.bits(e, fl - hi * sh, ftb)
else
RangeEncoder.encode(e, fl, fl + 1, ft + 1)
end
end
function RangeEncoder.tell(e: Enc): number
return e.nbits_total - ilog(e.rng)
end
local CORRECTION = { 35733, 38967, 42495, 46340, 50535, 55109, 60097, 65535 }
function RangeEncoder.tellFrac(e: Enc): number
local nbits = e.nbits_total * 8
local l = ilog(e.rng)
local r = floor(e.rng / 2 ^ (l - 16))
local b = floor(r / 4096) - 8
if r > CORRECTION[b + 1] then
b += 1
end
return nbits - (l * 8 + b)
end
-- done(): flush; the frame is e.buf (storage bytes)
function RangeEncoder.done(e: Enc)
local l = 32 - ilog(e.rng)
local msk = floor((CODE_TOP - 1) / 2 ^ l)
local stop = floor((e.val + msk) / (msk + 1)) * (msk + 1)
if stop + msk >= e.val + e.rng then
l += 1
msk = floor(msk / 2)
stop = floor((e.val + msk) / (msk + 1)) * (msk + 1)
end
while l > 0 do
carryOut(e, floor(stop / 8388608))
stop = (stop * 256) % CODE_TOP
l -= 8
end
if e.rem >= 0 or e.ext > 0 then
carryOut(e, 0)
end
local window = e.end_window
local used = e.nend_bits
while used >= 8 do
writeByteAtEnd(e, window % 256)
window = floor(window / 256)
used -= 8
end
if e.error == 0 then
local a, b = e.offs, e.storage - e.end_offs
if b > a then
buffer.fill(e.buf, a, 0, b - a)
end
if used > 0 then
if e.end_offs >= e.storage then
e.error = -1
else
l = -l
if e.offs + e.end_offs >= e.storage and l < used then
window = window % 2 ^ l
e.error = -1
end
local p = e.storage - e.end_offs - 1
writeu8(e.buf, p, bit32.bor(readu8(e.buf, p), window))
end
end
end
end
local _ = CODE_SHIFT
return RangeEncoder