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).
This commit is contained in:
1 parent
7169f2cc2c
commit
f784b4eb9b
22 files changed
+152
-311
No files matched your search
@@ -691,6 +691,7 @@ function Types.set(o: { [any]: any }, k: any, v: any)
|
||||
end
|
||||
o[k] = v
|
||||
end
|
||||
-- keyLess(a, b): a total order for table keys (by type name, then value; numbers before strings)
|
||||
local function keyLess(a: any, b: any): boolean
|
||||
local ta, tb = type(a), type(b)
|
||||
if ta ~= tb then
|
||||
@@ -701,6 +702,27 @@ local function keyLess(a: any, b: any): boolean
|
||||
end
|
||||
return tostring(a) < tostring(b)
|
||||
end
|
||||
Types.keyLess = keyLess
|
||||
|
||||
-- isArray(t, emptyIsMap?) -> t is a plain array 1 .. #t (no other keys, not an ordered object); an empty table is an
|
||||
-- array unless emptyIsMap
|
||||
function Types.isArray(t: { [any]: any }, emptyIsMap: boolean?): boolean
|
||||
if Types.isObject(t) then
|
||||
return false
|
||||
end
|
||||
local n = #t
|
||||
if n == 0 then
|
||||
return next(t) == nil and not emptyIsMap
|
||||
end
|
||||
if next(t, n) ~= nil then
|
||||
return false
|
||||
end
|
||||
local c = 0
|
||||
for _ in next, t do
|
||||
c += 1
|
||||
end
|
||||
return c == n
|
||||
end
|
||||
-- keys(t) -> the keys in order: an object's recorded order (keys since removed are skipped), then any other keys
|
||||
-- sorted (numbers before strings)
|
||||
function Types.keys(t: { [any]: any }): { any }
|
||||
|
||||
@@ -41,6 +41,15 @@ local function crc16(b: buffer, from: number, to: number): number
|
||||
end
|
||||
Frame.crc16 = crc16
|
||||
|
||||
local function crc8(b: buffer, from: number, to: number): number
|
||||
local c = 0
|
||||
for i = from, to - 1 do
|
||||
c = CRC8[bxor(c, readu8(b, i)) + 1]
|
||||
end
|
||||
return c
|
||||
end
|
||||
Frame.crc8 = crc8
|
||||
|
||||
local POW2 = table.create(64, 0)
|
||||
for i = 0, 63 do
|
||||
POW2[i + 1] = 2 ^ i
|
||||
@@ -147,11 +156,7 @@ function Frame.header(b: buffer, pos: number, len: number, si: any): Header?
|
||||
return nil
|
||||
end
|
||||
-- CRC-8 over the header
|
||||
local c = 0
|
||||
for i = pos, p - 1 do
|
||||
c = CRC8[bxor(c, readu8(b, i)) + 1]
|
||||
end
|
||||
if c ~= readu8(b, p) then
|
||||
if crc8(b, pos, p) ~= readu8(b, p) then
|
||||
return nil
|
||||
end
|
||||
return {
|
||||
|
||||
@@ -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 } = {
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
--
|
||||
-- Native code must never throw (an error unwinding through native code crashes Roblox Studio), so every read is
|
||||
-- bounds-checked by construction and bad input is skipped and counted in `stats`, never raised.
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
|
||||
local MpegTs = {}
|
||||
|
||||
@@ -103,25 +104,8 @@ local CODECS: { [number]: { string } } = {
|
||||
}
|
||||
|
||||
-- CRC-32/MPEG-2 (poly 0x04C11DB7, init 0xFFFFFFFF, MSB first, no final xor): a section including its CRC sums to 0
|
||||
local CRC = buffer.create(256 * 4)
|
||||
for i = 0, 255 do
|
||||
local c = lshift(i, 24)
|
||||
for _ = 1, 8 do
|
||||
if band(c, 0x80000000) ~= 0 then
|
||||
c = bit32.bxor(lshift(c, 1), 0x04C11DB7)
|
||||
else
|
||||
c = lshift(c, 1)
|
||||
end
|
||||
end
|
||||
buffer.writeu32(CRC, i * 4, c)
|
||||
end
|
||||
|
||||
local function crc32(b: buffer, off: number, n: number): number
|
||||
local c = 0xFFFFFFFF
|
||||
for i = off, off + n - 1 do
|
||||
c = bit32.bxor(lshift(c, 8), buffer.readu32(CRC, band(bit32.bxor(rshift(c, 24), readu8(b, i)), 255) * 4))
|
||||
end
|
||||
return c
|
||||
return Crc32.msb(b, off, n, 0xFFFFFFFF)
|
||||
end
|
||||
MpegTs.crc32 = crc32
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
-- A page header (27 bytes + segment table):
|
||||
-- 0 "OggS" | 4 version (0) | 5 flags (1 continued, 2 bos, 4 eos) | 6 granule (i64 LE) | 14 serial (u32)
|
||||
-- 18 sequence (u32) | 22 crc (u32) | 26 segment count | 27 lacing values | data
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local readu8, readu32, readi32 = buffer.readu8, buffer.readu32, buffer.readi32
|
||||
local band, bxor, lshift, rshift = bit32.band, bit32.bxor, bit32.lshift, bit32.rshift
|
||||
|
||||
@@ -27,19 +28,8 @@ export type Info = {
|
||||
sequence: number,
|
||||
}
|
||||
|
||||
-- CRC table, MSB-first
|
||||
local CRC = table.create(256, 0)
|
||||
for i = 0, 255 do
|
||||
local r = lshift(i, 24)
|
||||
for _ = 1, 8 do
|
||||
if band(r, 0x80000000) ~= 0 then
|
||||
r = bxor(lshift(r, 1), 0x04C11DB7)
|
||||
else
|
||||
r = lshift(r, 1)
|
||||
end
|
||||
end
|
||||
CRC[i + 1] = r
|
||||
end
|
||||
-- CRC table, MSB-first (core/Crc32)
|
||||
local CRC = Crc32.MSB_TABLE
|
||||
Page.CRC_TABLE = CRC
|
||||
|
||||
-- CRC of bytes [from, to) (caller guarantees bounds), continuing from crc
|
||||
|
||||
@@ -13,6 +13,8 @@
|
||||
--
|
||||
-- local w = Bytes.writer() -- or Bytes.writer(initialCapacity)
|
||||
-- w:string("abcd"); w:u32be(width); local out = w:finish()
|
||||
--
|
||||
-- local b, o, n, err = Bytes.span("Deflate.inflate", input, offset?, length?) -- codec argument checking
|
||||
local Err = require("@formats/core/Err")
|
||||
|
||||
local readu8, readi8, readu16, readi16, readu32, readi32 = buffer.readu8, buffer.readi8, buffer.readu16, buffer.readi16, buffer.readu32, buffer.readi32
|
||||
@@ -534,4 +536,32 @@ end
|
||||
Bytes.Reader = Reader
|
||||
Bytes.Writer = Writer
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ arguments
|
||||
-- toBuffer(v) -> v as a buffer (strings are copied) | nil for anything else
|
||||
function Bytes.toBuffer(v: any): buffer?
|
||||
if type(v) == "string" then
|
||||
return buffer.fromstring(v)
|
||||
end
|
||||
return if type(v) == "buffer" then v else nil
|
||||
end
|
||||
|
||||
-- span(what, input, offset?, length?) -> buffer, offset, length | nil, 0, 0, Err.ARG: a codec's (buffer | string,
|
||||
-- offset, length) arguments checked (whole numbers, offset inside the data; the length is clamped to the end)
|
||||
function Bytes.span(what: string, input: any, offset: number?, length: number?): (buffer?, number, number, Err.Err?)
|
||||
local b = Bytes.toBuffer(input)
|
||||
if not b then
|
||||
return nil, 0, 0, Err.new(Err.ARG, what .. ": expected a buffer or a string, got " .. typeof(input))
|
||||
end
|
||||
local n = buffer.len(b)
|
||||
local o = offset or 0
|
||||
local l = length or (n - (if type(o) == "number" then o else 0))
|
||||
if type(o) ~= "number" or o < 0 or o > n or o % 1 ~= 0 or type(l) ~= "number" or l < 0 or l % 1 ~= 0 then
|
||||
return nil, 0, 0, Err.new(Err.ARG, what .. ": bad offset / length " .. tostring(offset) .. ", " .. tostring(length))
|
||||
end
|
||||
if o + l > n then
|
||||
l = n - o
|
||||
end
|
||||
return b, o, l, nil
|
||||
end
|
||||
|
||||
return Bytes
|
||||
@@ -6,7 +6,10 @@
|
||||
--
|
||||
-- Crc32.of(buf, offset?, length?, crc?) -> u32 (pass a previous result as crc to continue it)
|
||||
-- Crc32.string(s, crc?) -> u32
|
||||
local band, bxor, rshift = bit32.band, bit32.bxor, bit32.rshift
|
||||
-- Crc32.msb(buf, offset, length, init?) -> u32 the MSB-first (unreflected) 0x04C11DB7 CRC, raw register: no
|
||||
-- inversions (Ogg: init 0; MPEG-2 sections / bzip2: 0xFFFFFFFF)
|
||||
-- Crc32.MSB_TABLE its 1-based byte table, for codecs that inline it (bzip2)
|
||||
local band, bxor, rshift, lshift = bit32.band, bit32.bxor, bit32.rshift, bit32.lshift
|
||||
local readu8, readu32 = buffer.readu8, buffer.readu32
|
||||
|
||||
local T0, T1, T2, T3 = table.create(256, 0), table.create(256, 0), table.create(256, 0), table.create(256, 0)
|
||||
@@ -95,4 +98,24 @@ end
|
||||
-- raw(buf, off, len, crc?): no clamping, for callers that already checked the range (hot paths)
|
||||
Crc32.raw = crc32
|
||||
|
||||
-- the MSB-first CRC-32 (polynomial 0x04C11DB7, not reflected): Ogg pages, MPEG-2 PSI sections, bzip2 blocks
|
||||
local MSB = table.create(256, 0)
|
||||
for n = 0, 255 do
|
||||
local c = lshift(n, 24)
|
||||
for _ = 1, 8 do
|
||||
c = if band(c, 0x80000000) ~= 0 then bxor(lshift(c, 1), 0x04C11DB7) else lshift(c, 1)
|
||||
end
|
||||
MSB[n + 1] = c
|
||||
end
|
||||
Crc32.MSB_TABLE = MSB
|
||||
|
||||
-- msb(buf, off, len, init?) -> the register after buf[off, off + len) (no inversions; the caller checked the range)
|
||||
function Crc32.msb(buf: buffer, off: number, len: number, init: number?): number
|
||||
local c = init or 0
|
||||
for i = off, off + len - 1 do
|
||||
c = bxor(lshift(c, 8), MSB[bxor(rshift(c, 24), readu8(buf, i)) + 1])
|
||||
end
|
||||
return c
|
||||
end
|
||||
|
||||
return Crc32
|
||||
@@ -453,31 +453,9 @@ local function toHalf(x: number): number?
|
||||
end
|
||||
Codec.toHalf = toHalf
|
||||
|
||||
local function keyLess(a: any, b: any): boolean
|
||||
local ta, tb = type(a), type(b)
|
||||
if ta ~= tb then
|
||||
return ta < tb
|
||||
end
|
||||
if ta == "number" or ta == "string" then
|
||||
return a < b
|
||||
end
|
||||
return tostring(a) < tostring(b)
|
||||
end
|
||||
local keyLess = Types.keyLess
|
||||
|
||||
local function isArray(t: any, emptyMap: boolean): boolean
|
||||
if Types.isObject(t) then
|
||||
return false
|
||||
end
|
||||
local n = #t
|
||||
if n == 0 then
|
||||
return next(t) == nil and not emptyMap
|
||||
end
|
||||
local c = 0
|
||||
for _ in next, t do
|
||||
c += 1
|
||||
end
|
||||
return c == n
|
||||
end
|
||||
local isArray = Types.isArray
|
||||
|
||||
local scratch = buffer.create(8)
|
||||
|
||||
|
||||
@@ -322,31 +322,9 @@ local function lenHeader(w: Bytes.Writer, n: number, fix: number?, fixMax: numbe
|
||||
end
|
||||
end
|
||||
|
||||
local function keyLess(a: any, b: any): boolean
|
||||
local ta, tb = type(a), type(b)
|
||||
if ta ~= tb then
|
||||
return ta < tb
|
||||
end
|
||||
if ta == "number" or ta == "string" then
|
||||
return a < b
|
||||
end
|
||||
return tostring(a) < tostring(b)
|
||||
end
|
||||
local keyLess = Types.keyLess
|
||||
|
||||
local function isArray(t: any, emptyMap: boolean): boolean
|
||||
if Types.isObject(t) then
|
||||
return false
|
||||
end
|
||||
local n = #t
|
||||
if n == 0 then
|
||||
return next(t) == nil and not emptyMap
|
||||
end
|
||||
local c = 0
|
||||
for _ in next, t do
|
||||
c += 1
|
||||
end
|
||||
return c == n
|
||||
end
|
||||
local isArray = Types.isArray
|
||||
Codec.isArray = isArray
|
||||
|
||||
local function enc(st: ESt, v: any, depth: number): boolean
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
-- their key order; arrays are arrays; datetimes are Types.Date values. Checks the 1.0 validity rules: UTF-8, control
|
||||
-- characters, table / key redefinition (explicit, implicit, dotted, inline and array-of-tables), number grammar and
|
||||
-- 64-bit range, escapes, datetimes. Never throws: failures return nil, message, offset (0-based).
|
||||
local Text = require("@formats/codecs/Text")
|
||||
local Types = require("@formats/Types")
|
||||
|
||||
local Decode = {}
|
||||
@@ -13,16 +14,7 @@ export type Options = {
|
||||
bigInts: ("number" | "string")?, -- integers beyond +-2^53: "string" keeps their digits (default "number")
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
local ESC: { [number]: string } = { [98] = "\b", [116] = "\t", [110] = "\n", [102] = "\f", [114] = "\r", [34] = '"', [92] = "\\" }
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ local Toml = {
|
||||
category = "data",
|
||||
extensions = { "toml" },
|
||||
mime = { "application/toml" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/Json" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/Json", "codecs/Text" },
|
||||
caps = { decode = true, encode = true, ordered = true, dates = true },
|
||||
}
|
||||
|
||||
|
||||
@@ -120,34 +120,9 @@ local function str(s: string, ind: string, key: boolean): string
|
||||
return dquote(s)
|
||||
end
|
||||
|
||||
local function isArray(t: { [any]: any }): boolean
|
||||
if Types.isObject(t) then
|
||||
return false
|
||||
end
|
||||
local n = #t
|
||||
if n == 0 then
|
||||
return next(t) == nil
|
||||
end
|
||||
if next(t, n) ~= nil then
|
||||
return false
|
||||
end
|
||||
local c = 0
|
||||
for _ in next, t do
|
||||
c += 1
|
||||
end
|
||||
return c == n
|
||||
end
|
||||
local isArray = Types.isArray
|
||||
|
||||
local function keyLess(a: any, b: any): boolean
|
||||
local ta, tb = type(a), type(b)
|
||||
if ta ~= tb then
|
||||
return ta < tb
|
||||
end
|
||||
if ta == "number" or ta == "string" then
|
||||
return a < b
|
||||
end
|
||||
return tostring(a) < tostring(b)
|
||||
end
|
||||
local keyLess = Types.keyLess
|
||||
|
||||
type St = { out: { string }, step: number, sort: boolean, null: any, refs: { [any]: number }, anchors: { [any]: string }, nextId: number, err: string? }
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
-- opts.tags = "ignore"), %YAML / %TAG directives, multi-document streams, comments. Scalars resolve with the core
|
||||
-- schema. Not supported: complex keys ("? "), collections as keys, recursive aliases, merge keys "<<" (kept as a
|
||||
-- normal key). Internally raises { yamlErr } tables caught by run(); never throws past it.
|
||||
local Text = require("@formats/codecs/Text")
|
||||
local Types = require("@formats/Types")
|
||||
local Base64 = require("@formats/core/Base64")
|
||||
|
||||
@@ -116,16 +117,7 @@ local function endOfLine(s: string, p: number): boolean
|
||||
return c == nil or c == 10 or (c == 35 and q > p)
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ resolution
|
||||
-- the core schema: a plain scalar's value
|
||||
|
||||
@@ -20,7 +20,7 @@ local Yaml = {
|
||||
category = "data",
|
||||
extensions = { "yaml", "yml" },
|
||||
mime = { "application/yaml", "application/x-yaml", "text/yaml" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "core/Base64" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "core/Base64", "codecs/Text" },
|
||||
caps = { decode = true, encode = true, ordered = true, multiDocument = true, anchors = true },
|
||||
}
|
||||
|
||||
|
||||
@@ -73,56 +73,6 @@ local function duration(s: string?): number?
|
||||
return d
|
||||
end
|
||||
|
||||
-- RFC 8216 attribute list: KEY=value pairs, quoted strings may hold commas
|
||||
local function attributes(s: string): { [string]: string }
|
||||
local out = {}
|
||||
local i, n = 1, #s
|
||||
while i <= n do
|
||||
local key, eq = string.match(s, "^%s*([%w%-_]+)%s*()=", i)
|
||||
if not key then
|
||||
break
|
||||
end
|
||||
i = (eq :: number) + 1
|
||||
local value
|
||||
if string.sub(s, i, i) == '"' then
|
||||
local j = string.find(s, '"', i + 1, true) or (n + 1)
|
||||
value = string.sub(s, i + 1, j - 1)
|
||||
i = j + 1
|
||||
else
|
||||
local j = string.find(s, ",", i, true) or (n + 1)
|
||||
value = Text.trim(string.sub(s, i, j - 1))
|
||||
i = j
|
||||
end
|
||||
out[string.upper(key)] = value
|
||||
local c = string.find(s, ",", i, true)
|
||||
if not c then
|
||||
break
|
||||
end
|
||||
i = c + 1
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
-- #EXTINF:<duration>[ attr="v" ...],<title>
|
||||
local function extinf(rest: string): (number?, string?)
|
||||
-- the first comma outside quoted attribute values
|
||||
local comma, quoted = nil, false
|
||||
for i = 1, #rest do
|
||||
local c = string.byte(rest, i)
|
||||
if c == 34 then
|
||||
quoted = not quoted
|
||||
elseif c == 44 and not quoted then
|
||||
comma = i
|
||||
break
|
||||
end
|
||||
end
|
||||
local head = if comma then string.sub(rest, 1, comma - 1) else rest
|
||||
local title = if comma then Text.trim(string.sub(rest, comma + 1)) else nil
|
||||
local d = string.match(head, "^%s*([%-%+]?[%d%.]+)")
|
||||
return duration(d), if title and title ~= "" then title else nil
|
||||
end
|
||||
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
function Pls.sniff(r: Bytes.Reader): number
|
||||
local s = r:peek(64)
|
||||
|
||||
+20
-1
@@ -1,5 +1,6 @@
|
||||
-- core/Bytes, core/Err, core/Crc32, core/Adler32, core/Input tests: every Reader / Writer method both endians, the
|
||||
-- sticky TRUNCATED error (never a throw), sub-readers without copies, Writer growth and patching.
|
||||
-- sticky TRUNCATED error (never a throw), sub-readers without copies, Writer growth and patching, Bytes.span, the
|
||||
-- MSB-first CRC-32 (MPEG-2 / bzip2 check values).
|
||||
local T = require("@tests/T")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/core/Err")
|
||||
@@ -181,4 +182,22 @@ do
|
||||
t:check(w.code == Err.CORRUPT and w.format == "gif", "wrap a string")
|
||||
end
|
||||
|
||||
-- Bytes.span: codec argument checking
|
||||
do
|
||||
local b, o, n = Bytes.span("x", "hello", 1, 10)
|
||||
t:check(type(b) == "buffer" and o == 1 and n == 4, "span clamps the length")
|
||||
local _, _, _, e = Bytes.span("x", 5)
|
||||
t:check(e and e.code == Err.ARG, "span: not a buffer")
|
||||
local _, _, _, e2 = Bytes.span("x", "abc", 4)
|
||||
t:check(e2 and e2.code == Err.ARG, "span: offset past the end")
|
||||
local _, _, _, e3 = Bytes.span("x", "abc", 0.5)
|
||||
t:check(e3 and e3.code == Err.ARG, "span: fractional offset")
|
||||
end
|
||||
-- the MSB-first CRC-32: CRC-32/MPEG-2 and CRC-32/BZIP2 of "123456789"
|
||||
do
|
||||
local nine = buffer.fromstring("123456789")
|
||||
t:eq(Crc32.msb(nine, 0, 9, 0xFFFFFFFF), 0x0376E6E7, "CRC-32/MPEG-2")
|
||||
t:eq(bit32.bxor(Crc32.msb(nine, 0, 9, 0xFFFFFFFF), 0xFFFFFFFF), 0xFC891918, "CRC-32/BZIP2")
|
||||
end
|
||||
|
||||
t:done()
|
||||
Reference in new issue
Block a user