ws-cleanup: shared helpers instead of copies (CRCs, codec arguments, utf8char, keyLess / isArray)
A sweep for duplicated function bodies, CRC tables and base64 across src/Formats: - core/Crc32.msb / MSB_TABLE: the MSB-first 0x04C11DB7 CRC-32 that containers/Ogg (Page + Mux), containers/MpegTs and codecs/Bzip2 each built (test vectors CRC-32/MPEG-2 and /BZIP2 in tests/core/bytes). - audio/flac/Encode uses audio/flac/Frame's CRC-8 / CRC-16 tables (Frame.crc8 added) instead of its own copy. - core/Bytes.toBuffer / Bytes.span: the (buffer | string, offset, length) argument checking codecs/Deflate, codecs/Lzma and codecs/Brotli each carried. - codecs/Text.utf8char: one copy instead of four (codecs/Json, codecs/Xml, data/yaml, data/toml). - Types.keyLess / Types.isArray (small additions to Types, backward compatible): data/msgpack, data/cbor and data/yaml/Emit had identical copies. - playlist/pls: dead copies of m3u's attributes / extinf removed. Left on purpose: per-codec bit readers / writers in hot loops (STYLE.md: inner loops keep their own bit buffers), same-folder helpers (Alac signExtend, Vorbis ilog), Zip's DOS-date civil() (a different epoch helper). Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
6b68c2ffab
commit
8158acf3d5
22 files changed
+152
-311
No files matched your search
@@ -12,6 +12,7 @@
|
||||
--
|
||||
-- Never throws: bad data and bad arguments return nil, Err (TRUNCATED / CORRUPT / LIMIT / UNSUPPORTED / ARG).
|
||||
local Err = require("@formats/core/Err")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Base64 = require("@formats/core/Base64")
|
||||
local Deflate = require("@formats/codecs/Deflate")
|
||||
@@ -63,30 +64,14 @@ function Brotli.dictionary(): buffer?
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decompress
|
||||
local function toBuffer(b: any): buffer?
|
||||
if type(b) == "string" then
|
||||
return buffer.fromstring(b)
|
||||
end
|
||||
return if type(b) == "buffer" then b else nil
|
||||
end
|
||||
|
||||
-- decompress(buf, offset?, length?, opts?) -> buffer, consumedEnd | nil, Err. consumedEnd: the input offset just past
|
||||
-- the stream (trailing bytes are ignored).
|
||||
function Brotli.decompress(buf: buffer | string, offset: number?, length: number?, opts: Options?): (buffer?, any)
|
||||
local b = toBuffer(buf)
|
||||
local b, o, l, aerr = Bytes.span("Brotli.decompress", buf, offset, length)
|
||||
if not b then
|
||||
return nil, Err.new(Err.ARG, "Brotli.decompress: expected a buffer or a string, got " .. typeof(buf))
|
||||
return nil, aerr
|
||||
end
|
||||
local n = buffer.len(b)
|
||||
local o = offset or 0
|
||||
if type(o) ~= "number" or o < 0 or o > n or o % 1 ~= 0 then
|
||||
return nil, Err.new(Err.ARG, "Brotli.decompress: bad offset " .. tostring(offset))
|
||||
end
|
||||
local l = length or (n - o)
|
||||
if type(l) ~= "number" or l < 0 or l % 1 ~= 0 then
|
||||
return nil, Err.new(Err.ARG, "Brotli.decompress: bad length " .. tostring(length))
|
||||
end
|
||||
l = math.min(l, n - o)
|
||||
local maxBytes = if opts and opts.maxBytes then opts.maxBytes else 2 ^ 30
|
||||
if type(maxBytes) ~= "number" or maxBytes < 0 then
|
||||
return nil, Err.new(Err.ARG, "Brotli.decompress: bad maxBytes")
|
||||
|
||||
@@ -11,25 +11,15 @@
|
||||
--
|
||||
-- Never throws: every buffer access is in range by construction; failures return nil, message, kind
|
||||
-- ("truncated" | "corrupt" | "checksum" | "limit" | "unsupported").
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local band, bor, bxor, lshift, rshift = bit32.band, bit32.bor, bit32.bxor, bit32.lshift, bit32.rshift
|
||||
local readu8, readu32, writeu8, writeu32 = buffer.readu8, buffer.readu32, buffer.writeu8, buffer.writeu32
|
||||
local bcreate, bcopy, bfill, blen = buffer.create, buffer.copy, buffer.fill, buffer.len
|
||||
|
||||
local Decode = {}
|
||||
|
||||
-- CRC-32/BZIP2 table (polynomial 0x04C11DB7, not reflected)
|
||||
local CT = table.create(256, 0)
|
||||
for i = 0, 255 do
|
||||
local c = lshift(i, 24)
|
||||
for _ = 1, 8 do
|
||||
if band(c, 0x80000000) ~= 0 then
|
||||
c = bxor(lshift(c, 1), 0x04C11DB7)
|
||||
else
|
||||
c = lshift(c, 1)
|
||||
end
|
||||
end
|
||||
CT[i + 1] = c
|
||||
end
|
||||
-- CRC-32/BZIP2 table (polynomial 0x04C11DB7, not reflected: core/Crc32)
|
||||
local CT = Crc32.MSB_TABLE
|
||||
Decode.crcTable = CT
|
||||
|
||||
local MASK = table.create(33, 0)
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
--
|
||||
-- Never throws: bad data and bad arguments return nil, Err (core/Err codes TRUNCATED / CORRUPT / CHECKSUM / ARG).
|
||||
local Err = require("@formats/core/Err")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Adler32 = require("@formats/core/Adler32")
|
||||
local Inflate = require("@formats/codecs/Deflate/Inflate")
|
||||
@@ -21,44 +22,8 @@ local Deflate = {}
|
||||
|
||||
Deflate.version = "1.1.0"
|
||||
|
||||
local function toBuffer(b: any): buffer?
|
||||
if type(b) == "string" then
|
||||
return buffer.fromstring(b)
|
||||
end
|
||||
if type(b) ~= "buffer" then
|
||||
return nil
|
||||
end
|
||||
return b
|
||||
end
|
||||
|
||||
-- (offset, length) clamped to the buffer; nil for non-numbers / negative / fractional / out-of-range offsets
|
||||
local function range(b: buffer, off: number?, len: number?): (number?, number)
|
||||
local n = buffer.len(b)
|
||||
local o = off or 0
|
||||
if type(o) ~= "number" or o < 0 or o > n or o % 1 ~= 0 then
|
||||
return nil, 0
|
||||
end
|
||||
local l = len or (n - o)
|
||||
if type(l) ~= "number" or l < 0 or l % 1 ~= 0 then
|
||||
return nil, 0
|
||||
end
|
||||
if o + l > n then
|
||||
l = n - o
|
||||
end
|
||||
return o, l
|
||||
end
|
||||
|
||||
local function args(what: string, buf: any, offset: number?, length: number?): (buffer?, number, number, Err.Err?)
|
||||
local b = toBuffer(buf)
|
||||
if not b then
|
||||
return nil, 0, 0, Err.new(Err.ARG, what .. ": expected a buffer or a string, got " .. typeof(buf))
|
||||
end
|
||||
local o, l = range(b, offset, length)
|
||||
if not o then
|
||||
return nil, 0, 0, Err.new(Err.ARG, what .. ": bad offset / length " .. tostring(offset) .. ", " .. tostring(length))
|
||||
end
|
||||
return b, o, l, nil
|
||||
end
|
||||
local toBuffer = Bytes.toBuffer
|
||||
local args = Bytes.span
|
||||
|
||||
local function shrink(out: buffer, n: number): buffer
|
||||
if buffer.len(out) == n then
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
-- is a LIMIT error, never a stack overflow), strict grammar (no comments, trailing commas, leading zeros, NaN, single
|
||||
-- quotes or raw control characters), \u escapes with surrogate pairs (lone surrogates become WTF-8 bytes). Also the
|
||||
-- pull tokenizer behind Json.events. Never throws: failures return nil, message, offset (0-based byte offset).
|
||||
local Text = require("@formats/codecs/Text")
|
||||
local Types = require("@formats/Types")
|
||||
|
||||
local Decode = {}
|
||||
@@ -31,16 +32,7 @@ local ESC: { [number]: string } = {
|
||||
}
|
||||
|
||||
-- utf8 bytes of a code point (surrogates too: WTF-8)
|
||||
local function utf8char(c: number): string
|
||||
if c < 0x80 then
|
||||
return char(c)
|
||||
elseif c < 0x800 then
|
||||
return char(0xC0 + c // 64, 0x80 + c % 64)
|
||||
elseif c < 0x10000 then
|
||||
return char(0xE0 + c // 4096, 0x80 + (c // 64) % 64, 0x80 + c % 64)
|
||||
end
|
||||
return char(0xF0 + c // 262144, 0x80 + (c // 4096) % 64, 0x80 + (c // 64) % 64, 0x80 + c % 64)
|
||||
end
|
||||
local utf8char = Text.utf8char
|
||||
Decode.utf8char = utf8char
|
||||
|
||||
local function hex4(s: string, i: number): number?
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
-- none / CRC-32 / CRC-64 are verified, SHA-256 is skipped (not verified). xz filters: LZMA2, delta, x86, PowerPC,
|
||||
-- ARM, ARM-Thumb, SPARC, ARM64; IA-64 and RISC-V are Err.UNSUPPORTED. Never throws.
|
||||
local Err = require("@formats/core/Err")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Decode = require("@formats/codecs/Lzma/Decode")
|
||||
local Filters = require("@formats/codecs/Lzma/Filters")
|
||||
@@ -33,43 +34,8 @@ export type Options = {
|
||||
}
|
||||
export type Props = { lc: number, lp: number, pb: number, dictSize: number? }
|
||||
|
||||
local function toBuffer(b: any): buffer?
|
||||
if type(b) == "string" then
|
||||
return buffer.fromstring(b)
|
||||
end
|
||||
if type(b) ~= "buffer" then
|
||||
return nil
|
||||
end
|
||||
return b
|
||||
end
|
||||
|
||||
local function range(b: buffer, off: number?, len: number?): (number?, number)
|
||||
local n = buffer.len(b)
|
||||
local o = off or 0
|
||||
if type(o) ~= "number" or o < 0 or o > n or o % 1 ~= 0 then
|
||||
return nil, 0
|
||||
end
|
||||
local l = len or (n - o)
|
||||
if type(l) ~= "number" or l < 0 or l % 1 ~= 0 then
|
||||
return nil, 0
|
||||
end
|
||||
if o + l > n then
|
||||
l = n - o
|
||||
end
|
||||
return o, l
|
||||
end
|
||||
|
||||
local function args(what: string, buf: any, offset: number?, length: number?): (buffer?, number, number, Err.Err?)
|
||||
local b = toBuffer(buf)
|
||||
if not b then
|
||||
return nil, 0, 0, Err.new(Err.ARG, what .. ": expected a buffer or a string, got " .. typeof(buf))
|
||||
end
|
||||
local o, l = range(b, offset, length)
|
||||
if not o then
|
||||
return nil, 0, 0, Err.new(Err.ARG, what .. ": bad offset / length " .. tostring(offset) .. ", " .. tostring(length))
|
||||
end
|
||||
return b, o, l, nil
|
||||
end
|
||||
local toBuffer = Bytes.toBuffer
|
||||
local args = Bytes.span
|
||||
|
||||
local function maxOf(opts: Options?): number
|
||||
local m = opts and opts.maxBytes
|
||||
|
||||
@@ -5,6 +5,19 @@
|
||||
-- playlist-luau 3fc7fd3 (same author, no third-party code). Never throws.
|
||||
local Text = {}
|
||||
|
||||
-- utf8char(codepoint) -> its UTF-8 bytes (no validation: the parsers check ranges first; utf8.char would throw)
|
||||
function Text.utf8char(c: number): string
|
||||
local char = string.char
|
||||
if c < 0x80 then
|
||||
return char(c)
|
||||
elseif c < 0x800 then
|
||||
return char(0xC0 + c // 64, 0x80 + c % 64)
|
||||
elseif c < 0x10000 then
|
||||
return char(0xE0 + c // 4096, 0x80 + (c // 64) % 64, 0x80 + c % 64)
|
||||
end
|
||||
return char(0xF0 + c // 262144, 0x80 + (c // 4096) % 64, 0x80 + (c // 64) % 64, 0x80 + c % 64)
|
||||
end
|
||||
|
||||
-- CP1252 bytes 0x80..0x9F (undefined ones map to U+FFFD)
|
||||
Text.CP1252 = {
|
||||
[0x80] = 0x20AC, [0x82] = 0x201A, [0x83] = 0x0192, [0x84] = 0x201E, [0x85] = 0x2026, [0x86] = 0x2020,
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
-- character references and internal general entities from the DOCTYPE (with an expansion limit). Not supported:
|
||||
-- external entities / DTDs (references to them are errors), entity values containing markup (taken as text),
|
||||
-- validation. Never throws: errors are events.
|
||||
local Text = require("@formats/codecs/Text")
|
||||
local Types = require("@formats/Types")
|
||||
|
||||
local Parse = {}
|
||||
@@ -24,16 +25,7 @@ export type Options = {
|
||||
local NAME = "^[%a_:\128-\255][%w_:%.%-\128-\255]*"
|
||||
local PREDEF: { [string]: string } = { lt = "<", gt = ">", amp = "&", quot = '"', apos = "'" }
|
||||
|
||||
local function utf8char(c: number): string
|
||||
if c < 0x80 then
|
||||
return char(c)
|
||||
elseif c < 0x800 then
|
||||
return char(0xC0 + c // 64, 0x80 + c % 64)
|
||||
elseif c < 0x10000 then
|
||||
return char(0xE0 + c // 4096, 0x80 + (c // 64) % 64, 0x80 + c % 64)
|
||||
end
|
||||
return char(0xF0 + c // 262144, 0x80 + (c // 4096) % 64, 0x80 + (c // 64) % 64, 0x80 + c % 64)
|
||||
end
|
||||
local utf8char = Text.utf8char
|
||||
Parse.utf8char = utf8char
|
||||
|
||||
local CP1252: { [number]: number } = {
|
||||
|
||||
Reference in new issue
Block a user