rename: src/Formats -> src/Rosetta, @formats -> @rosetta requires, build/luajit/rosetta + rosetta._compat, build/roblox/Rosetta, ROSETTA_* env/globals, writer vendor strings
This commit is contained in:
1 parent
2956576d7f
commit
22ddcd78fa
941 files changed
+3364
-3364
No files matched your search
@@ -0,0 +1,50 @@
|
||||
--!strict
|
||||
--!native
|
||||
-- core/Adler32: Adler-32 (RFC 1950, the zlib checksum). Absorbed from deflate-luau src/Deflate/Checksum.luau (same author;
|
||||
-- no third-party code). Never throws: the range is clamped.
|
||||
--
|
||||
-- Adler32.of(buf, offset?, length?, adler?) -> u32 (starts at 1; pass a previous result to continue it)
|
||||
-- Adler32.string(s, adler?) -> u32
|
||||
local readu8 = buffer.readu8
|
||||
|
||||
-- Adler-32 of buf[off, off + len), continuing adler (default 1)
|
||||
local function adler32(buf: buffer, off: number, len: number, adler: number?): number
|
||||
local a0 = adler or 1
|
||||
local a, b = a0 % 65536, a0 // 65536
|
||||
local i, e = off, off + len
|
||||
while i < e do
|
||||
-- 5552 is zlib's NMAX: the largest n with 255 n (n + 1) / 2 + (n + 1) (BASE - 1) < 2^32. Doubles hold far
|
||||
-- more, but keeping the sums small keeps them exact and cheap.
|
||||
local stop = math.min(e, i + 5552)
|
||||
while i < stop do
|
||||
a += readu8(buf, i)
|
||||
b += a
|
||||
i += 1
|
||||
end
|
||||
a %= 65521
|
||||
b %= 65521
|
||||
end
|
||||
return b * 65536 + a
|
||||
end
|
||||
|
||||
-- clamp (offset, length) to the buffer
|
||||
local function range(buf: buffer, offset: number?, length: number?): (number, number)
|
||||
local n = buffer.len(buf)
|
||||
local o = math.clamp(offset or 0, 0, n)
|
||||
return o, math.clamp(length or (n - o), 0, n - o)
|
||||
end
|
||||
|
||||
local Adler32 = {}
|
||||
|
||||
function Adler32.of(buf: buffer, offset: number?, length: number?, adler: number?): number
|
||||
local o, l = range(buf, offset, length)
|
||||
return adler32(buf, o, l, adler)
|
||||
end
|
||||
|
||||
function Adler32.string(s: string, adler: number?): number
|
||||
return adler32(buffer.fromstring(s), 0, #s, adler)
|
||||
end
|
||||
|
||||
Adler32.raw = adler32
|
||||
|
||||
return Adler32
|
||||
@@ -0,0 +1,167 @@
|
||||
--!strict
|
||||
--!native
|
||||
-- core/Base64: RFC 4648 base64 (standard alphabet, and base64url with opts.url) encode / decode, and data: URIs.
|
||||
-- Used by tiled map data, glTF / SVG / CSS data: URIs, plist <data>, YAML !!binary, FBX, the Brotli static tables.
|
||||
-- decode skips whitespace, accepts missing padding and both alphabets; any other character, data after padding or a
|
||||
-- lone last character is an error. Never throws.
|
||||
--
|
||||
-- local s = Base64.encode(buf) -- buffer | string -> string ("" for empty input)
|
||||
-- local s2 = Base64.encode(buf, { url = true, pad = false, wrap = 76 }, offset, length)
|
||||
-- local b, err = Base64.decode(s) -- string | buffer -> buffer | nil, "message"
|
||||
-- local b2, mime = Base64.decodeDataUri("data:application/octet-stream;base64,AAEC") -- buffer, mime | nil, msg
|
||||
-- local uri = Base64.dataUri(buf, "image/png")
|
||||
local Base64 = {}
|
||||
|
||||
export type EncodeOptions = {
|
||||
url: boolean?, -- the URL-safe alphabet (- and _); default false
|
||||
pad: boolean?, -- "=" padding; default true
|
||||
wrap: number?, -- a "\n" after every `wrap` output characters (rounded down to a multiple of 4); default none
|
||||
}
|
||||
|
||||
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, 11, 12, 13, 32 } do
|
||||
buffer.writeu8(DEC, c, 64)
|
||||
end
|
||||
buffer.writeu8(DEC, string.byte("="), 65)
|
||||
|
||||
local readu8, writeu8 = buffer.readu8, buffer.writeu8
|
||||
|
||||
-- encode(data, opts?, offset?, length?) -> base64 text of data[offset, offset + length)
|
||||
function Base64.encode(data: buffer | string, opts: EncodeOptions?, offset: number?, length: number?): string
|
||||
local buf: buffer = if type(data) == "string" then buffer.fromstring(data) else data
|
||||
local total = buffer.len(buf)
|
||||
local o = math.clamp(offset or 0, 0, total)
|
||||
local n = math.clamp(length or (total - o), 0, total - o)
|
||||
local enc = if opts and opts.url then ENC_URL else ENC
|
||||
local pad = not (opts and opts.pad == false)
|
||||
local wrap = if opts and opts.wrap and opts.wrap >= 4 then opts.wrap // 4 else 0 -- groups per line
|
||||
local full = n // 3
|
||||
local rest = n - full * 3
|
||||
local textLen = full * 4 + (if rest == 0 then 0 elseif pad then 4 else rest + 1)
|
||||
local groups = full + (if rest > 0 then 1 else 0)
|
||||
local breaks = if wrap > 0 and groups > 0 then (groups - 1) // wrap else 0
|
||||
local out = buffer.create(textLen + breaks)
|
||||
local p = 0
|
||||
local col = 0
|
||||
for i = 0, full - 1 do
|
||||
if wrap > 0 then
|
||||
if col == wrap then
|
||||
writeu8(out, p, 10)
|
||||
p += 1
|
||||
col = 0
|
||||
end
|
||||
col += 1
|
||||
end
|
||||
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
|
||||
if wrap > 0 and col == wrap then
|
||||
writeu8(out, p, 10)
|
||||
p += 1
|
||||
end
|
||||
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: buffer = 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 scale = 2 ^ bits
|
||||
local byte = acc // scale
|
||||
writeu8(out, p, byte % 256)
|
||||
p += 1
|
||||
acc -= byte * scale
|
||||
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 | string, mime: string?): string
|
||||
return "data:" .. (mime or "application/octet-stream") .. ";base64," .. Base64.encode(buf)
|
||||
end
|
||||
|
||||
return Base64
|
||||
@@ -0,0 +1,310 @@
|
||||
--!strict
|
||||
--!native
|
||||
-- core/Bits: bit readers and writers, LSB-first (DEFLATE, GIF LZW, VP8L, ...) and MSB-first (JPEG entropy data
|
||||
-- without the 0xFF00 unstuffing, MPEG, H.264, FLAC, ...). Up to 32 bits per read / write.
|
||||
--
|
||||
-- Plain arithmetic on an accumulator of at most 31 bits (no bit32 calls), exact on doubles, cheap on both runtimes.
|
||||
-- These are the generic, convenient readers: a codec's innermost loop usually keeps its own local bit buffer (see
|
||||
-- codecs/compress/Deflate/Inflate) and uses these for headers.
|
||||
--
|
||||
-- Never throws: reading past the end returns zero bits and sets the sticky `err` (Err.TRUNCATED).
|
||||
--
|
||||
-- local br = Bits.lsbReader(buf, offset?, length?) -- or Bits.msbReader
|
||||
-- local v = br:read(5); if br.err then return nil, br.err end
|
||||
-- local bw = Bits.msbWriter(); bw:write(0b101, 3); local out = bw:finish()
|
||||
-- local w = { buf = buffer.create(n), pos = 0, acc = 0, cnt = 0 }; Bits.msbPut(w, v, nbits) -- encoders' fast path
|
||||
-- (a plain record the caller sizes / grows itself: MP3, MP2 and FLAC frames)
|
||||
local Err = require("@rosetta/core/Err")
|
||||
local Bytes = require("@rosetta/core/Bytes")
|
||||
|
||||
local readu8, blen = buffer.readu8, buffer.len
|
||||
local floor = math.floor
|
||||
|
||||
-- 2^n for n = 0..32
|
||||
local POW = table.create(33, 0)
|
||||
for i = 0, 32 do
|
||||
POW[i + 1] = 2 ^ i
|
||||
end
|
||||
|
||||
local Bits = {}
|
||||
Bits.POW = POW
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ readers
|
||||
export type BitReader = {
|
||||
buf: buffer,
|
||||
pos: number, -- next byte to load
|
||||
limit: number,
|
||||
acc: number, -- loaded, unread bits
|
||||
cnt: number, -- how many
|
||||
err: Err.Err?,
|
||||
read: (BitReader, number) -> number,
|
||||
peek: (BitReader, number) -> number,
|
||||
skip: (BitReader, number) -> (),
|
||||
align: (BitReader) -> (),
|
||||
bytePos: (BitReader) -> number,
|
||||
bitsLeft: (BitReader) -> number,
|
||||
}
|
||||
|
||||
local function overrun(self: any)
|
||||
if not self.err then
|
||||
self.err = Err.new(Err.TRUNCATED, "bit stream ends early", self.limit)
|
||||
end
|
||||
end
|
||||
|
||||
-- LSB-first: the first bit of the stream is bit 0 of the first byte; a field's first bit is its least significant
|
||||
local Lsb = {}
|
||||
Lsb.__index = Lsb
|
||||
|
||||
local function lsbFill(self: any, n: number)
|
||||
local acc, cnt, pos = self.acc, self.cnt, self.pos
|
||||
while cnt < n do
|
||||
if pos < self.limit then
|
||||
acc += readu8(self.buf, pos) * POW[cnt + 1]
|
||||
pos += 1
|
||||
else
|
||||
overrun(self)
|
||||
end
|
||||
cnt += 8
|
||||
end
|
||||
self.acc, self.cnt, self.pos = acc, cnt, pos
|
||||
end
|
||||
|
||||
function Lsb.read(self: any, n: number): number
|
||||
if n > 24 then
|
||||
local lo = Lsb.read(self, 16)
|
||||
return lo + Lsb.read(self, n - 16) * 65536
|
||||
end
|
||||
if self.cnt < n then
|
||||
lsbFill(self, n)
|
||||
end
|
||||
local m = POW[n + 1]
|
||||
local v = self.acc % m
|
||||
self.acc = (self.acc - v) / m
|
||||
self.cnt -= n
|
||||
return v
|
||||
end
|
||||
function Lsb.peek(self: any, n: number): number
|
||||
if self.cnt < n then
|
||||
local acc, cnt, pos = self.acc, self.cnt, self.pos
|
||||
while cnt < n do -- no error for peeking past the end: the bits are zeros
|
||||
if pos < self.limit then
|
||||
acc += readu8(self.buf, pos) * POW[cnt + 1]
|
||||
pos += 1
|
||||
end
|
||||
cnt += 8
|
||||
end
|
||||
-- keep the loaded bytes only when they are all real (zero padding past the end is not data)
|
||||
if (pos - self.pos) * 8 == cnt - self.cnt then
|
||||
self.acc, self.cnt, self.pos = acc, cnt, pos
|
||||
end
|
||||
return acc % POW[n + 1]
|
||||
end
|
||||
return self.acc % POW[n + 1]
|
||||
end
|
||||
function Lsb.skip(self: any, n: number)
|
||||
while n > 24 do
|
||||
Lsb.read(self, 24)
|
||||
n -= 24
|
||||
end
|
||||
Lsb.read(self, n)
|
||||
end
|
||||
-- align(): drop the bits up to the next byte boundary
|
||||
function Lsb.align(self: any)
|
||||
local k = self.cnt % 8
|
||||
if k > 0 then
|
||||
Lsb.read(self, k)
|
||||
end
|
||||
end
|
||||
-- bytePos(): the offset of the first byte not yet (even partly) consumed
|
||||
function Lsb.bytePos(self: any): number
|
||||
return self.pos - floor(self.cnt / 8)
|
||||
end
|
||||
function Lsb.bitsLeft(self: any): number
|
||||
return (self.limit - self.pos) * 8 + self.cnt
|
||||
end
|
||||
|
||||
-- MSB-first: the first bit of the stream is bit 7 of the first byte; a field's first bit is its most significant
|
||||
local Msb = {}
|
||||
Msb.__index = Msb
|
||||
|
||||
local function msbFill(self: any, n: number)
|
||||
local acc, cnt, pos = self.acc, self.cnt, self.pos
|
||||
while cnt < n do
|
||||
if pos < self.limit then
|
||||
acc = acc * 256 + readu8(self.buf, pos)
|
||||
pos += 1
|
||||
else
|
||||
acc *= 256
|
||||
overrun(self)
|
||||
end
|
||||
cnt += 8
|
||||
end
|
||||
self.acc, self.cnt, self.pos = acc, cnt, pos
|
||||
end
|
||||
|
||||
function Msb.read(self: any, n: number): number
|
||||
if n > 24 then
|
||||
local hi = Msb.read(self, n - 16)
|
||||
return hi * 65536 + Msb.read(self, 16)
|
||||
end
|
||||
if self.cnt < n then
|
||||
msbFill(self, n)
|
||||
end
|
||||
local sh = POW[self.cnt - n + 1]
|
||||
local v = floor(self.acc / sh)
|
||||
self.acc -= v * sh
|
||||
self.cnt -= n
|
||||
return v
|
||||
end
|
||||
function Msb.peek(self: any, n: number): number
|
||||
local acc, cnt, pos = self.acc, self.cnt, self.pos
|
||||
while cnt < n do
|
||||
if pos < self.limit then
|
||||
acc = acc * 256 + readu8(self.buf, pos)
|
||||
pos += 1
|
||||
else
|
||||
acc *= 256
|
||||
end
|
||||
cnt += 8
|
||||
end
|
||||
-- keep the loaded bytes only when they are all real (zero padding past the end is not data)
|
||||
if (pos - self.pos) * 8 == cnt - self.cnt then
|
||||
self.acc, self.cnt, self.pos = acc, cnt, pos
|
||||
end
|
||||
return floor(acc / POW[cnt - n + 1])
|
||||
end
|
||||
function Msb.skip(self: any, n: number)
|
||||
while n > 24 do
|
||||
Msb.read(self, 24)
|
||||
n -= 24
|
||||
end
|
||||
Msb.read(self, n)
|
||||
end
|
||||
function Msb.align(self: any)
|
||||
local k = self.cnt % 8
|
||||
if k > 0 then
|
||||
Msb.read(self, k)
|
||||
end
|
||||
end
|
||||
Msb.bytePos = Lsb.bytePos
|
||||
Msb.bitsLeft = Lsb.bitsLeft
|
||||
|
||||
local function reader(meta: any, buf: buffer, offset: number?, length: number?): BitReader
|
||||
local n = blen(buf)
|
||||
local o = math.clamp(offset or 0, 0, n)
|
||||
local l = math.clamp(length or (n - o), 0, n - o)
|
||||
return setmetatable({ buf = buf, pos = o, limit = o + l, acc = 0, cnt = 0, err = nil }, meta) :: any
|
||||
end
|
||||
|
||||
-- lsbReader(buf, offset?, length?) / msbReader(buf, offset?, length?) -> BitReader
|
||||
function Bits.lsbReader(buf: buffer, offset: number?, length: number?): BitReader
|
||||
return reader(Lsb, buf, offset, length)
|
||||
end
|
||||
function Bits.msbReader(buf: buffer, offset: number?, length: number?): BitReader
|
||||
return reader(Msb, buf, offset, length)
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ writers
|
||||
export type BitWriter = {
|
||||
out: Bytes.Writer, -- the bytes so far (whole bytes only)
|
||||
acc: number,
|
||||
cnt: number,
|
||||
write: (BitWriter, number, number) -> (),
|
||||
align: (BitWriter) -> (),
|
||||
finish: (BitWriter) -> buffer,
|
||||
}
|
||||
|
||||
local LsbW = {}
|
||||
LsbW.__index = LsbW
|
||||
function LsbW.write(self: any, v: number, n: number)
|
||||
if n > 24 then
|
||||
local lo = v % 65536
|
||||
LsbW.write(self, lo, 16)
|
||||
LsbW.write(self, (v - lo) / 65536, n - 16)
|
||||
return
|
||||
end
|
||||
local acc = self.acc + (v % POW[n + 1]) * POW[self.cnt + 1]
|
||||
local cnt = self.cnt + n
|
||||
local out = self.out
|
||||
while cnt >= 8 do
|
||||
local byte = acc % 256
|
||||
out:u8(byte)
|
||||
acc = (acc - byte) / 256
|
||||
cnt -= 8
|
||||
end
|
||||
self.acc, self.cnt = acc, cnt
|
||||
end
|
||||
-- align(): pad with zero bits to a byte boundary
|
||||
function LsbW.align(self: any)
|
||||
if self.cnt > 0 then
|
||||
self.out:u8(self.acc)
|
||||
self.acc, self.cnt = 0, 0
|
||||
end
|
||||
end
|
||||
function LsbW.finish(self: any): buffer
|
||||
LsbW.align(self)
|
||||
return self.out:finish()
|
||||
end
|
||||
|
||||
local MsbW = {}
|
||||
MsbW.__index = MsbW
|
||||
function MsbW.write(self: any, v: number, n: number)
|
||||
if n > 24 then
|
||||
local lo = v % 65536
|
||||
MsbW.write(self, (v - lo) / 65536, n - 16)
|
||||
MsbW.write(self, lo, 16)
|
||||
return
|
||||
end
|
||||
local acc = self.acc * POW[n + 1] + (v % POW[n + 1])
|
||||
local cnt = self.cnt + n
|
||||
local out = self.out
|
||||
while cnt >= 8 do
|
||||
local sh = POW[cnt - 8 + 1]
|
||||
local byte = floor(acc / sh)
|
||||
out:u8(byte)
|
||||
acc -= byte * sh
|
||||
cnt -= 8
|
||||
end
|
||||
self.acc, self.cnt = acc, cnt
|
||||
end
|
||||
function MsbW.align(self: any)
|
||||
if self.cnt > 0 then
|
||||
self.out:u8(self.acc * POW[8 - self.cnt + 1])
|
||||
self.acc, self.cnt = 0, 0
|
||||
end
|
||||
end
|
||||
function MsbW.finish(self: any): buffer
|
||||
MsbW.align(self)
|
||||
return self.out:finish()
|
||||
end
|
||||
|
||||
-- msbPut(w, v, n): append the low n bits (n <= 32) of the non-negative integer v to the record w = { buf, pos, acc,
|
||||
-- cnt }, MSB first. No growth: the caller keeps buf large enough (whole bytes go to buf[pos]; acc holds cnt < 8 bits).
|
||||
export type RawWriter = { buf: buffer, pos: number, acc: number, cnt: number }
|
||||
function Bits.msbPut(w: RawWriter, v: number, n: number)
|
||||
if n == 0 then
|
||||
return
|
||||
end
|
||||
local acc = w.acc * POW[n + 1] + v
|
||||
local cnt = w.cnt + n
|
||||
local b, p = w.buf, w.pos
|
||||
while cnt >= 8 do
|
||||
cnt -= 8
|
||||
local m = POW[cnt + 1]
|
||||
local byte = floor(acc / m)
|
||||
buffer.writeu8(b, p, byte)
|
||||
p += 1
|
||||
acc -= byte * m
|
||||
end
|
||||
w.acc, w.cnt, w.pos = acc, cnt, p
|
||||
end
|
||||
|
||||
-- lsbWriter(capacity?) / msbWriter(capacity?) -> BitWriter
|
||||
function Bits.lsbWriter(capacity: number?): BitWriter
|
||||
return setmetatable({ out = Bytes.writer(capacity), acc = 0, cnt = 0 }, LsbW) :: any
|
||||
end
|
||||
function Bits.msbWriter(capacity: number?): BitWriter
|
||||
return setmetatable({ out = Bytes.writer(capacity), acc = 0, cnt = 0 }, MsbW) :: any
|
||||
end
|
||||
|
||||
return Bits
|
||||
@@ -0,0 +1,567 @@
|
||||
--!strict
|
||||
--!native
|
||||
-- core/Bytes: a cursor Reader over a buffer range and a growable Writer. Little / big endian integers up to u64 (as a
|
||||
-- double: exact to 2^53), f16 / f32 / f64, C strings, fixed strings, and zero-copy sub-readers.
|
||||
--
|
||||
-- Never throws. A read past the end (or a failed seek) sets the reader's sticky `err` (Err.TRUNCATED), moves the cursor
|
||||
-- to the end and returns 0 / "" (a zero-length buffer for bytes()); check `r.err` once after a run of reads:
|
||||
--
|
||||
-- local r = Bytes.reader(buf) -- or Bytes.reader(buf, offset, length)
|
||||
-- local magic, w, h = r:string(4), r:u32be(), r:u32be()
|
||||
-- if r.err then return nil, r.err end
|
||||
-- if magic ~= "abcd" then return r:fail(Err.CORRUPT, "bad magic") end
|
||||
--
|
||||
-- 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("@rosetta/core/Err")
|
||||
|
||||
local readu8, readi8, readu16, readi16, readu32, readi32 = buffer.readu8, buffer.readi8, buffer.readu16, buffer.readi16, buffer.readu32, buffer.readi32
|
||||
local readf32, readf64, readstring = buffer.readf32, buffer.readf64, buffer.readstring
|
||||
local writeu8, writeu16, writeu32, writef32, writef64, writestring = buffer.writeu8, buffer.writeu16, buffer.writeu32, buffer.writef32, buffer.writef64, buffer.writestring
|
||||
local bcreate, bcopy, bfill, blen = buffer.create, buffer.copy, buffer.fill, buffer.len
|
||||
local byteswap = bit32.byteswap
|
||||
|
||||
local TWO32 = 4294967296
|
||||
local scratch = bcreate(8) -- reinterpret casts for big-endian floats
|
||||
|
||||
local Bytes = {}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ Reader
|
||||
local Reader = {}
|
||||
Reader.__index = Reader
|
||||
|
||||
export type Reader = typeof(setmetatable(
|
||||
{} :: {
|
||||
buf: buffer,
|
||||
pos: number, -- absolute offset of the next byte in buf
|
||||
base: number, -- absolute offset of the reader's first byte
|
||||
limit: number, -- absolute offset just past the reader's last byte
|
||||
err: Err.Err?, -- sticky: the first failure
|
||||
},
|
||||
Reader
|
||||
))
|
||||
|
||||
-- reader(buf, offset?, length?) -> Reader over buf[offset, offset + length), clamped to the buffer
|
||||
function Bytes.reader(buf: buffer, offset: number?, length: number?): Reader
|
||||
local n = blen(buf)
|
||||
local o = math.clamp(offset or 0, 0, n)
|
||||
local l = math.clamp(length or (n - o), 0, n - o)
|
||||
return setmetatable({ buf = buf, pos = o, base = o, limit = o + l, err = nil :: Err.Err? }, Reader)
|
||||
end
|
||||
|
||||
-- fail(code, message) -> nil, Err: records the first error (at the cursor) and returns it, for `return r:fail(...)`
|
||||
function Reader.fail(self: Reader, code: string, message: string): (nil, Err.Err)
|
||||
if not self.err then
|
||||
self.err = Err.new(code, message, self.pos)
|
||||
end
|
||||
return nil, self.err :: Err.Err
|
||||
end
|
||||
|
||||
-- need(n) -> true when n more bytes are available; otherwise records TRUNCATED and moves to the end
|
||||
function Reader.need(self: Reader, n: number): boolean
|
||||
if self.pos + n <= self.limit and n >= 0 then
|
||||
return true
|
||||
end
|
||||
if not self.err then
|
||||
self.err = Err.new(Err.TRUNCATED, string.format("need %d bytes, %d left", n, self.limit - self.pos), self.pos)
|
||||
end
|
||||
self.pos = self.limit
|
||||
return false
|
||||
end
|
||||
|
||||
function Reader.tell(self: Reader): number
|
||||
return self.pos - self.base
|
||||
end
|
||||
function Reader.size(self: Reader): number
|
||||
return self.limit - self.base
|
||||
end
|
||||
function Reader.remaining(self: Reader): number
|
||||
return self.limit - self.pos
|
||||
end
|
||||
function Reader.eof(self: Reader): boolean
|
||||
return self.pos >= self.limit
|
||||
end
|
||||
|
||||
-- seek(offset) -> boolean: offset relative to the reader's start; out of range records TRUNCATED
|
||||
function Reader.seek(self: Reader, offset: number): boolean
|
||||
if offset < 0 or self.base + offset > self.limit then
|
||||
self.pos = self.limit
|
||||
if not self.err then
|
||||
self.err = Err.new(Err.TRUNCATED, "seek to " .. offset .. " past the end", self.base + offset)
|
||||
end
|
||||
return false
|
||||
end
|
||||
self.pos = self.base + offset
|
||||
return true
|
||||
end
|
||||
|
||||
function Reader.skip(self: Reader, n: number): boolean
|
||||
if self:need(n) then
|
||||
self.pos += n
|
||||
return true
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
-- align(n): skip to the next multiple of n (relative to the reader's start)
|
||||
function Reader.align(self: Reader, n: number): boolean
|
||||
local r = (self.pos - self.base) % n
|
||||
if r == 0 then
|
||||
return true
|
||||
end
|
||||
return self:skip(n - r)
|
||||
end
|
||||
|
||||
-- integers
|
||||
function Reader.u8(self: Reader): number
|
||||
local p = self.pos
|
||||
if p + 1 > self.limit then
|
||||
self:need(1)
|
||||
return 0
|
||||
end
|
||||
self.pos = p + 1
|
||||
return readu8(self.buf, p)
|
||||
end
|
||||
function Reader.i8(self: Reader): number
|
||||
local p = self.pos
|
||||
if p + 1 > self.limit then
|
||||
self:need(1)
|
||||
return 0
|
||||
end
|
||||
self.pos = p + 1
|
||||
return readi8(self.buf, p)
|
||||
end
|
||||
function Reader.u16le(self: Reader): number
|
||||
local p = self.pos
|
||||
if p + 2 > self.limit then
|
||||
self:need(2)
|
||||
return 0
|
||||
end
|
||||
self.pos = p + 2
|
||||
return readu16(self.buf, p)
|
||||
end
|
||||
function Reader.u16be(self: Reader): number
|
||||
local p = self.pos
|
||||
if p + 2 > self.limit then
|
||||
self:need(2)
|
||||
return 0
|
||||
end
|
||||
self.pos = p + 2
|
||||
return readu8(self.buf, p) * 256 + readu8(self.buf, p + 1)
|
||||
end
|
||||
function Reader.i16le(self: Reader): number
|
||||
local p = self.pos
|
||||
if p + 2 > self.limit then
|
||||
self:need(2)
|
||||
return 0
|
||||
end
|
||||
self.pos = p + 2
|
||||
return readi16(self.buf, p)
|
||||
end
|
||||
function Reader.i16be(self: Reader): number
|
||||
local v = self:u16be()
|
||||
return if v >= 32768 then v - 65536 else v
|
||||
end
|
||||
function Reader.u24le(self: Reader): number
|
||||
local p = self.pos
|
||||
if p + 3 > self.limit then
|
||||
self:need(3)
|
||||
return 0
|
||||
end
|
||||
self.pos = p + 3
|
||||
return readu16(self.buf, p) + readu8(self.buf, p + 2) * 65536
|
||||
end
|
||||
function Reader.u24be(self: Reader): number
|
||||
local p = self.pos
|
||||
if p + 3 > self.limit then
|
||||
self:need(3)
|
||||
return 0
|
||||
end
|
||||
self.pos = p + 3
|
||||
return readu8(self.buf, p) * 65536 + readu8(self.buf, p + 1) * 256 + readu8(self.buf, p + 2)
|
||||
end
|
||||
function Reader.u32le(self: Reader): number
|
||||
local p = self.pos
|
||||
if p + 4 > self.limit then
|
||||
self:need(4)
|
||||
return 0
|
||||
end
|
||||
self.pos = p + 4
|
||||
return readu32(self.buf, p)
|
||||
end
|
||||
function Reader.u32be(self: Reader): number
|
||||
local p = self.pos
|
||||
if p + 4 > self.limit then
|
||||
self:need(4)
|
||||
return 0
|
||||
end
|
||||
self.pos = p + 4
|
||||
return byteswap(readu32(self.buf, p))
|
||||
end
|
||||
function Reader.i32le(self: Reader): number
|
||||
local p = self.pos
|
||||
if p + 4 > self.limit then
|
||||
self:need(4)
|
||||
return 0
|
||||
end
|
||||
self.pos = p + 4
|
||||
return readi32(self.buf, p)
|
||||
end
|
||||
function Reader.i32be(self: Reader): number
|
||||
local v = self:u32be()
|
||||
return if v >= 2147483648 then v - TWO32 else v
|
||||
end
|
||||
-- 64-bit integers as doubles (exact below 2^53)
|
||||
function Reader.u64le(self: Reader): number
|
||||
local lo = self:u32le()
|
||||
return lo + self:u32le() * TWO32
|
||||
end
|
||||
function Reader.u64be(self: Reader): number
|
||||
local hi = self:u32be()
|
||||
return hi * TWO32 + self:u32be()
|
||||
end
|
||||
function Reader.i64le(self: Reader): number
|
||||
local lo = self:u32le()
|
||||
return lo + self:i32le() * TWO32
|
||||
end
|
||||
function Reader.i64be(self: Reader): number
|
||||
local hi = self:i32be()
|
||||
return hi * TWO32 + self:u32be()
|
||||
end
|
||||
|
||||
-- floats
|
||||
local function half(h: number): number
|
||||
local s = if h >= 32768 then -1 else 1
|
||||
local e = bit32.band(bit32.rshift(h, 10), 31)
|
||||
local m = bit32.band(h, 1023)
|
||||
if e == 0 then
|
||||
return s * m * 2 ^ -24
|
||||
elseif e == 31 then
|
||||
return if m == 0 then s * math.huge else 0 / 0
|
||||
end
|
||||
return s * (1 + m / 1024) * 2 ^ (e - 15)
|
||||
end
|
||||
Bytes.half = half
|
||||
|
||||
function Reader.f16le(self: Reader): number
|
||||
return half(self:u16le())
|
||||
end
|
||||
function Reader.f16be(self: Reader): number
|
||||
return half(self:u16be())
|
||||
end
|
||||
function Reader.f32le(self: Reader): number
|
||||
local p = self.pos
|
||||
if p + 4 > self.limit then
|
||||
self:need(4)
|
||||
return 0
|
||||
end
|
||||
self.pos = p + 4
|
||||
return readf32(self.buf, p)
|
||||
end
|
||||
function Reader.f32be(self: Reader): number
|
||||
local v = self:u32be()
|
||||
writeu32(scratch, 0, v)
|
||||
return readf32(scratch, 0)
|
||||
end
|
||||
function Reader.f64le(self: Reader): number
|
||||
local p = self.pos
|
||||
if p + 8 > self.limit then
|
||||
self:need(8)
|
||||
return 0
|
||||
end
|
||||
self.pos = p + 8
|
||||
return readf64(self.buf, p)
|
||||
end
|
||||
function Reader.f64be(self: Reader): number
|
||||
local hi = self:u32be()
|
||||
local lo = self:u32be()
|
||||
writeu32(scratch, 0, lo)
|
||||
writeu32(scratch, 4, hi)
|
||||
return readf64(scratch, 0)
|
||||
end
|
||||
|
||||
-- strings
|
||||
function Reader.string(self: Reader, n: number): string
|
||||
local p = self.pos
|
||||
if n < 0 or p + n > self.limit then
|
||||
self:need(n)
|
||||
return ""
|
||||
end
|
||||
self.pos = p + n
|
||||
return readstring(self.buf, p, n)
|
||||
end
|
||||
|
||||
-- fixed(n): an n-byte field, cut at the first NUL
|
||||
function Reader.fixed(self: Reader, n: number): string
|
||||
local s = self:string(n)
|
||||
local z = string.find(s, "\0", 1, true)
|
||||
return if z then string.sub(s, 1, z - 1) else s
|
||||
end
|
||||
|
||||
-- cstring(max?): bytes up to a NUL (consumed, not returned); no NUL before the end (or max bytes) records TRUNCATED
|
||||
function Reader.cstring(self: Reader, max: number?): string
|
||||
local p = self.pos
|
||||
local e = if max then math.min(self.limit, p + max) else self.limit
|
||||
local q = p
|
||||
local buf = self.buf
|
||||
while q < e and readu8(buf, q) ~= 0 do
|
||||
q += 1
|
||||
end
|
||||
if q >= e then
|
||||
self.pos = e
|
||||
self:need(1)
|
||||
return readstring(buf, p, e - p)
|
||||
end
|
||||
self.pos = q + 1
|
||||
return readstring(buf, p, q - p)
|
||||
end
|
||||
|
||||
-- match(s) -> true (and skips s) when the next bytes are s; otherwise false, nothing consumed, no error
|
||||
function Reader.match(self: Reader, s: string): boolean
|
||||
local p, n = self.pos, #s
|
||||
if p + n > self.limit or readstring(self.buf, p, n) ~= s then
|
||||
return false
|
||||
end
|
||||
self.pos = p + n
|
||||
return true
|
||||
end
|
||||
|
||||
-- peek(n, at?) -> up to n bytes as a string at the cursor (+ at), without consuming; short at the end, never an error
|
||||
function Reader.peek(self: Reader, n: number, at: number?): string
|
||||
local p = self.pos + (at or 0)
|
||||
if p < self.base or p >= self.limit then
|
||||
return ""
|
||||
end
|
||||
return readstring(self.buf, p, math.min(n, self.limit - p))
|
||||
end
|
||||
|
||||
-- peekU8(at?) -> the byte at the cursor (+ at), or -1 past the end
|
||||
function Reader.peekU8(self: Reader, at: number?): number
|
||||
local p = self.pos + (at or 0)
|
||||
if p < self.base or p >= self.limit then
|
||||
return -1
|
||||
end
|
||||
return readu8(self.buf, p)
|
||||
end
|
||||
|
||||
-- bytes(n) -> a new buffer with a copy of the next n bytes
|
||||
function Reader.bytes(self: Reader, n: number): buffer
|
||||
local p = self.pos
|
||||
if n < 0 or p + n > self.limit then
|
||||
self:need(n)
|
||||
return bcreate(0)
|
||||
end
|
||||
local b = bcreate(n)
|
||||
bcopy(b, 0, self.buf, p, n)
|
||||
self.pos = p + n
|
||||
return b
|
||||
end
|
||||
|
||||
-- slice(n) -> a Reader over the next n bytes (no copy); the parent skips them. Short data: a reader over what is
|
||||
-- left, and the parent records TRUNCATED.
|
||||
function Reader.slice(self: Reader, n: number): Reader
|
||||
local p = self.pos
|
||||
local l = math.max(0, math.min(n, self.limit - p))
|
||||
if l < n then
|
||||
self:need(n)
|
||||
else
|
||||
self.pos = p + n
|
||||
end
|
||||
return setmetatable({ buf = self.buf, pos = p, base = p, limit = p + l, err = nil :: Err.Err? }, Reader)
|
||||
end
|
||||
|
||||
-- sub(offset, length?) -> a Reader over [offset, offset + length) relative to this reader's start (clamped), without
|
||||
-- moving the cursor
|
||||
function Reader.sub(self: Reader, offset: number, length: number?): Reader
|
||||
local o = math.clamp(self.base + offset, self.base, self.limit)
|
||||
local l = math.clamp(length or (self.limit - o), 0, self.limit - o)
|
||||
return setmetatable({ buf = self.buf, pos = o, base = o, limit = o + l, err = nil :: Err.Err? }, Reader)
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ Writer
|
||||
local Writer = {}
|
||||
Writer.__index = Writer
|
||||
|
||||
export type Writer = typeof(setmetatable(
|
||||
{} :: {
|
||||
buf: buffer,
|
||||
pos: number, -- bytes written (the next write offset)
|
||||
},
|
||||
Writer
|
||||
))
|
||||
|
||||
-- writer(capacity?) -> Writer
|
||||
function Bytes.writer(capacity: number?): Writer
|
||||
return setmetatable({ buf = bcreate(math.max(16, capacity or 256)), pos = 0 }, Writer)
|
||||
end
|
||||
|
||||
-- reserve(n): make room for n more bytes; returns the write offset (for direct buffer.write* into w.buf)
|
||||
function Writer.reserve(self: Writer, n: number): number
|
||||
local p = self.pos
|
||||
local cap = blen(self.buf)
|
||||
if p + n > cap then
|
||||
local nb = bcreate(math.max(cap * 2, p + n))
|
||||
bcopy(nb, 0, self.buf, 0, p)
|
||||
self.buf = nb
|
||||
end
|
||||
return p
|
||||
end
|
||||
|
||||
function Writer.tell(self: Writer): number
|
||||
return self.pos
|
||||
end
|
||||
|
||||
function Writer.u8(self: Writer, v: number)
|
||||
local p = self:reserve(1)
|
||||
writeu8(self.buf, p, v)
|
||||
self.pos = p + 1
|
||||
end
|
||||
function Writer.u16le(self: Writer, v: number)
|
||||
local p = self:reserve(2)
|
||||
writeu16(self.buf, p, v)
|
||||
self.pos = p + 2
|
||||
end
|
||||
function Writer.u16be(self: Writer, v: number)
|
||||
local p = self:reserve(2)
|
||||
writeu8(self.buf, p, bit32.rshift(v, 8))
|
||||
writeu8(self.buf, p + 1, v)
|
||||
self.pos = p + 2
|
||||
end
|
||||
function Writer.u24le(self: Writer, v: number)
|
||||
local p = self:reserve(3)
|
||||
writeu16(self.buf, p, v)
|
||||
writeu8(self.buf, p + 2, bit32.rshift(v, 16))
|
||||
self.pos = p + 3
|
||||
end
|
||||
function Writer.u24be(self: Writer, v: number)
|
||||
local p = self:reserve(3)
|
||||
writeu8(self.buf, p, bit32.rshift(v, 16))
|
||||
writeu8(self.buf, p + 1, bit32.rshift(v, 8))
|
||||
writeu8(self.buf, p + 2, v)
|
||||
self.pos = p + 3
|
||||
end
|
||||
function Writer.u32le(self: Writer, v: number)
|
||||
local p = self:reserve(4)
|
||||
writeu32(self.buf, p, v)
|
||||
self.pos = p + 4
|
||||
end
|
||||
function Writer.u32be(self: Writer, v: number)
|
||||
local p = self:reserve(4)
|
||||
writeu32(self.buf, p, byteswap(v % TWO32))
|
||||
self.pos = p + 4
|
||||
end
|
||||
Writer.i8 = Writer.u8
|
||||
Writer.i16le = Writer.u16le
|
||||
Writer.i16be = Writer.u16be
|
||||
Writer.i32le = Writer.u32le
|
||||
Writer.i32be = Writer.u32be
|
||||
function Writer.u64le(self: Writer, v: number)
|
||||
self:u32le(v % TWO32)
|
||||
self:u32le(math.floor(v / TWO32) % TWO32)
|
||||
end
|
||||
function Writer.u64be(self: Writer, v: number)
|
||||
self:u32be(math.floor(v / TWO32) % TWO32)
|
||||
self:u32be(v % TWO32)
|
||||
end
|
||||
Writer.i64le = Writer.u64le
|
||||
Writer.i64be = Writer.u64be
|
||||
function Writer.f32le(self: Writer, v: number)
|
||||
local p = self:reserve(4)
|
||||
writef32(self.buf, p, v)
|
||||
self.pos = p + 4
|
||||
end
|
||||
function Writer.f32be(self: Writer, v: number)
|
||||
writef32(scratch, 0, v)
|
||||
self:u32be(readu32(scratch, 0))
|
||||
end
|
||||
function Writer.f64le(self: Writer, v: number)
|
||||
local p = self:reserve(8)
|
||||
writef64(self.buf, p, v)
|
||||
self.pos = p + 8
|
||||
end
|
||||
function Writer.f64be(self: Writer, v: number)
|
||||
writef64(scratch, 0, v)
|
||||
self:u32be(readu32(scratch, 4))
|
||||
self:u32be(readu32(scratch, 0))
|
||||
end
|
||||
|
||||
function Writer.string(self: Writer, s: string)
|
||||
local n = #s
|
||||
local p = self:reserve(n)
|
||||
writestring(self.buf, p, s)
|
||||
self.pos = p + n
|
||||
end
|
||||
-- cstring(s): s and a NUL
|
||||
function Writer.cstring(self: Writer, s: string)
|
||||
self:string(s)
|
||||
self:u8(0)
|
||||
end
|
||||
-- bytes(b, offset?, length?): copy a buffer range
|
||||
function Writer.bytes(self: Writer, b: buffer, offset: number?, length: number?)
|
||||
local o = offset or 0
|
||||
local n = length or (blen(b) - o)
|
||||
local p = self:reserve(n)
|
||||
bcopy(self.buf, p, b, o, n)
|
||||
self.pos = p + n
|
||||
end
|
||||
function Writer.fill(self: Writer, v: number, n: number)
|
||||
local p = self:reserve(n)
|
||||
bfill(self.buf, p, v, n)
|
||||
self.pos = p + n
|
||||
end
|
||||
|
||||
-- patch fields written earlier (lengths, CRCs) at an absolute offset < pos
|
||||
function Writer.setU32le(self: Writer, at: number, v: number)
|
||||
writeu32(self.buf, at, v)
|
||||
end
|
||||
function Writer.setU32be(self: Writer, at: number, v: number)
|
||||
writeu32(self.buf, at, byteswap(v % TWO32))
|
||||
end
|
||||
function Writer.setU16le(self: Writer, at: number, v: number)
|
||||
writeu16(self.buf, at, v)
|
||||
end
|
||||
function Writer.setU8(self: Writer, at: number, v: number)
|
||||
writeu8(self.buf, at, v)
|
||||
end
|
||||
|
||||
-- finish() -> a buffer of exactly the written bytes (the writer can keep going)
|
||||
function Writer.finish(self: Writer): buffer
|
||||
local b = bcreate(self.pos)
|
||||
bcopy(b, 0, self.buf, 0, self.pos)
|
||||
return b
|
||||
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
|
||||
@@ -0,0 +1,52 @@
|
||||
--!strict
|
||||
--!native
|
||||
-- core/Crc16: the two 16-bit CRCs old archive and encoding formats use, table driven. New code.
|
||||
-- Crc16.ccitt(buf, off, len, crc?) CRC-16/XMODEM: polynomial 0x1021, MSB first, init 0, no final XOR (BinHex 4.0,
|
||||
-- MacBinary II / III headers)
|
||||
-- Crc16.arc(buf, off, len, crc?) CRC-16/ARC: polynomial 0x8005 reflected (0xA001), init 0 (LHA, Zoo, ARC)
|
||||
-- Pass a previous result as crc to continue it. Never throws: the range is clamped to the buffer.
|
||||
local band, bxor, rshift, lshift = bit32.band, bit32.bxor, bit32.rshift, bit32.lshift
|
||||
local readu8 = buffer.readu8
|
||||
|
||||
local MSB = table.create(256, 0)
|
||||
local ARC = table.create(256, 0)
|
||||
for n = 0, 255 do
|
||||
local c = lshift(n, 8)
|
||||
for _ = 1, 8 do
|
||||
c = if band(c, 0x8000) ~= 0 then bxor(band(lshift(c, 1), 0xFFFF), 0x1021) else band(lshift(c, 1), 0xFFFF)
|
||||
end
|
||||
MSB[n + 1] = c
|
||||
local r = n
|
||||
for _ = 1, 8 do
|
||||
r = if band(r, 1) ~= 0 then bxor(rshift(r, 1), 0xA001) else rshift(r, 1)
|
||||
end
|
||||
ARC[n + 1] = r
|
||||
end
|
||||
|
||||
local Crc16 = {}
|
||||
|
||||
local function clamp(buf: buffer, off: number, len: number): (number, number)
|
||||
local n = buffer.len(buf)
|
||||
local o = math.clamp(math.floor(off), 0, n)
|
||||
return o, math.clamp(math.floor(len), 0, n - o)
|
||||
end
|
||||
|
||||
function Crc16.ccitt(buf: buffer, off: number, len: number, crc: number?): number
|
||||
local o, l = clamp(buf, off, len)
|
||||
local c = crc or 0
|
||||
for i = o, o + l - 1 do
|
||||
c = bxor(band(lshift(c, 8), 0xFFFF), MSB[bxor(rshift(c, 8), readu8(buf, i)) + 1])
|
||||
end
|
||||
return c
|
||||
end
|
||||
|
||||
function Crc16.arc(buf: buffer, off: number, len: number, crc: number?): number
|
||||
local o, l = clamp(buf, off, len)
|
||||
local c = crc or 0
|
||||
for i = o, o + l - 1 do
|
||||
c = bxor(rshift(c, 8), ARC[bxor(band(c, 0xFF), readu8(buf, i)) + 1])
|
||||
end
|
||||
return c
|
||||
end
|
||||
|
||||
return Crc16
|
||||
@@ -0,0 +1,121 @@
|
||||
--!strict
|
||||
--!native
|
||||
-- core/Crc32: CRC-32 (IEEE 802.3, the zlib / gzip / zip / PNG polynomial 0xEDB88320, reflected), slice-by-8.
|
||||
-- Absorbed from deflate-luau src/Deflate/Checksum.luau (same author;
|
||||
-- no third-party code). Never throws: the range is clamped.
|
||||
--
|
||||
-- Crc32.of(buf, offset?, length?, crc?) -> u32 (pass a previous result as crc to continue it)
|
||||
-- Crc32.string(s, crc?) -> u32
|
||||
-- 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)
|
||||
local T4, T5, T6, T7 = table.create(256, 0), table.create(256, 0), table.create(256, 0), table.create(256, 0)
|
||||
for n = 0, 255 do
|
||||
local c = n
|
||||
for _ = 1, 8 do
|
||||
if band(c, 1) ~= 0 then
|
||||
c = bxor(0xEDB88320, rshift(c, 1))
|
||||
else
|
||||
c = rshift(c, 1)
|
||||
end
|
||||
end
|
||||
T0[n + 1] = c
|
||||
end
|
||||
for n = 0, 255 do
|
||||
local c = T0[n + 1]
|
||||
c = bxor(T0[band(c, 255) + 1], rshift(c, 8))
|
||||
T1[n + 1] = c
|
||||
c = bxor(T0[band(c, 255) + 1], rshift(c, 8))
|
||||
T2[n + 1] = c
|
||||
c = bxor(T0[band(c, 255) + 1], rshift(c, 8))
|
||||
T3[n + 1] = c
|
||||
c = bxor(T0[band(c, 255) + 1], rshift(c, 8))
|
||||
T4[n + 1] = c
|
||||
c = bxor(T0[band(c, 255) + 1], rshift(c, 8))
|
||||
T5[n + 1] = c
|
||||
c = bxor(T0[band(c, 255) + 1], rshift(c, 8))
|
||||
T6[n + 1] = c
|
||||
c = bxor(T0[band(c, 255) + 1], rshift(c, 8))
|
||||
T7[n + 1] = c
|
||||
end
|
||||
|
||||
-- CRC-32 of buf[off, off + len), continuing crc (default 0 = a new CRC). Values are finished CRCs (the inversion
|
||||
-- happens inside), so crc(b, crc(a)) == the CRC of a .. b.
|
||||
local function crc32(buf: buffer, off: number, len: number, crc: number?): number
|
||||
local c = bxor(crc or 0, 0xFFFFFFFF)
|
||||
local i, e = off, off + len
|
||||
local e8 = e - 8
|
||||
while i <= e8 do
|
||||
c = bxor(c, readu32(buf, i))
|
||||
local d = readu32(buf, i + 4)
|
||||
c = bxor(
|
||||
T7[band(c, 255) + 1],
|
||||
T6[band(rshift(c, 8), 255) + 1],
|
||||
T5[band(rshift(c, 16), 255) + 1],
|
||||
T4[rshift(c, 24) + 1],
|
||||
T3[band(d, 255) + 1],
|
||||
T2[band(rshift(d, 8), 255) + 1],
|
||||
T1[band(rshift(d, 16), 255) + 1],
|
||||
T0[rshift(d, 24) + 1]
|
||||
)
|
||||
i += 8
|
||||
end
|
||||
local e4 = e - 4
|
||||
while i <= e4 do
|
||||
c = bxor(c, readu32(buf, i))
|
||||
c = bxor(T3[band(c, 255) + 1], T2[band(rshift(c, 8), 255) + 1], T1[band(rshift(c, 16), 255) + 1], T0[rshift(c, 24) + 1])
|
||||
i += 4
|
||||
end
|
||||
while i < e do
|
||||
c = bxor(T0[band(bxor(c, readu8(buf, i)), 255) + 1], rshift(c, 8))
|
||||
i += 1
|
||||
end
|
||||
return bxor(c, 0xFFFFFFFF)
|
||||
end
|
||||
|
||||
-- clamp (offset, length) to the buffer
|
||||
local function range(buf: buffer, offset: number?, length: number?): (number, number)
|
||||
local n = buffer.len(buf)
|
||||
local o = math.clamp(offset or 0, 0, n)
|
||||
return o, math.clamp(length or (n - o), 0, n - o)
|
||||
end
|
||||
|
||||
local Crc32 = {}
|
||||
|
||||
function Crc32.of(buf: buffer, offset: number?, length: number?, crc: number?): number
|
||||
local o, l = range(buf, offset, length)
|
||||
return crc32(buf, o, l, crc)
|
||||
end
|
||||
|
||||
function Crc32.string(s: string, crc: number?): number
|
||||
return crc32(buffer.fromstring(s), 0, #s, crc)
|
||||
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
|
||||
@@ -0,0 +1,94 @@
|
||||
--!strict
|
||||
-- core/Err: the error values every decoder / encoder returns (`nil, err`; nothing in the library throws on bad data).
|
||||
-- An Err is a table { code, message, offset?, format? } that prints as "png: CORRUPT at 0x1234: bad filter type 7".
|
||||
-- Never throws: --!native modules may require it.
|
||||
--
|
||||
-- return nil, Err.new(Err.TRUNCATED, "IDAT runs past the end", r.pos)
|
||||
-- if err.code == Err.UNSUPPORTED then ... end
|
||||
local Err = {}
|
||||
|
||||
-- codes
|
||||
Err.TRUNCATED = "TRUNCATED" -- the data ends early
|
||||
Err.CORRUPT = "CORRUPT" -- the data is malformed (bad magic inside, bad table, out-of-range field)
|
||||
Err.CHECKSUM = "CHECKSUM" -- a CRC / Adler / hash mismatch
|
||||
Err.UNSUPPORTED = "UNSUPPORTED" -- valid data using a feature this library does not implement, or no encoder
|
||||
Err.UNKNOWN_FORMAT = "UNKNOWN_FORMAT" -- no format recognised the data / name
|
||||
Err.LIMIT = "LIMIT" -- over a size limit (opts.maxPixels, opts.maxBytes, ...)
|
||||
Err.ARG = "ARG" -- a bad argument (wrong type, out-of-range option)
|
||||
Err.IO = "IO" -- reading or writing a file failed, or the host has no file system
|
||||
|
||||
export type Err = {
|
||||
code: string,
|
||||
message: string,
|
||||
offset: number?,
|
||||
format: string?,
|
||||
}
|
||||
|
||||
local Meta = {}
|
||||
Meta.__tostring = function(e: Err): string
|
||||
local s = if e.format then e.format .. ": " .. e.code else e.code
|
||||
if e.offset then
|
||||
s ..= string.format(" at 0x%x", e.offset)
|
||||
end
|
||||
return s .. ": " .. e.message
|
||||
end
|
||||
|
||||
-- new(code, message, offset?) -> Err
|
||||
function Err.new(code: string, message: string, offset: number?): Err
|
||||
return setmetatable({ code = code, message = message, offset = offset, format = nil :: string? }, Meta) :: any
|
||||
end
|
||||
|
||||
-- the kinds a private submodule may report: Err codes in either case ("truncated", "CORRUPT", ...)
|
||||
local KIND: { [string]: string } = {}
|
||||
for _, c in { "TRUNCATED", "CORRUPT", "CHECKSUM", "UNSUPPORTED", "LIMIT", "ARG", "IO", "UNKNOWN_FORMAT" } do
|
||||
KIND[c] = c
|
||||
KIND[string.lower(c)] = c
|
||||
end
|
||||
|
||||
-- from(message, offset?, kind?) -> Err: the error plumbing between a private submodule (which returns
|
||||
-- nil / false, "message"[, kind]) and the contract. kind names the code ("truncated", "LIMIT", ...); without it the
|
||||
-- message decides: a "<code>: ..." prefix ("limit: ...", "unsupported: ...") names the code and is dropped;
|
||||
-- "...truncated..." / "...past the end" / "...runs past..." -> TRUNCATED; "unsupported..." -> UNSUPPORTED;
|
||||
-- "checksum..." -> CHECKSUM; anything else CORRUPT. An Err passes
|
||||
-- through unchanged.
|
||||
function Err.from(message: any, offset: number?, kind: string?): Err
|
||||
if Err.is(message) then
|
||||
return message
|
||||
end
|
||||
local msg = if message == nil then "failed" else tostring(message)
|
||||
local code: string? = kind and KIND[kind]
|
||||
if not code then
|
||||
local head, rest = string.match(msg, "^(%a+):%s*(.*)$")
|
||||
if head and KIND[head] then
|
||||
code, msg = KIND[head], rest :: string
|
||||
elseif string.find(msg, "truncated", 1, true) or string.find(msg, "past the end", 1, true) or string.find(msg, "runs past", 1, true) then
|
||||
code = Err.TRUNCATED
|
||||
elseif string.find(msg, "^unsupported") then
|
||||
code = Err.UNSUPPORTED
|
||||
elseif string.find(msg, "^checksum") then
|
||||
code = Err.CHECKSUM
|
||||
else
|
||||
code = Err.CORRUPT
|
||||
end
|
||||
end
|
||||
return Err.new(code :: string, msg, offset)
|
||||
end
|
||||
|
||||
-- is(v) -> true when v is an Err
|
||||
function Err.is(v: any): boolean
|
||||
return type(v) == "table" and getmetatable(v) == Meta
|
||||
end
|
||||
|
||||
-- wrap(e, format) -> e with its format set (when not set yet); a string becomes a CORRUPT Err. Used by the API to
|
||||
-- tag errors with the format name.
|
||||
function Err.wrap(e: any, format: string?): Err
|
||||
if not Err.is(e) then
|
||||
e = Err.new(Err.CORRUPT, tostring(e))
|
||||
end
|
||||
if e.format == nil then
|
||||
e.format = format
|
||||
end
|
||||
return e
|
||||
end
|
||||
|
||||
return table.freeze(Err)
|
||||
@@ -0,0 +1,82 @@
|
||||
--!strict
|
||||
-- core/Input: turns what the caller passed (a buffer, a string of bytes, or a file path) into a buffer, reading files
|
||||
-- through the host adapter the API passes in (core never requires host itself).
|
||||
--
|
||||
-- local buf, err, name = Input.resolve(src, opts, host)
|
||||
--
|
||||
-- A string is a path when opts.path is true, or when the host has a file system and the string looks like a path
|
||||
-- (under 4 KB, no NUL / newline) and the file exists; otherwise it is the data itself (opts.data = true forces that).
|
||||
-- name: the path (for the extension fallback) or opts.name. Never throws.
|
||||
local Err = require("@rosetta/core/Err")
|
||||
|
||||
local Input = {}
|
||||
|
||||
export type HostLike = {
|
||||
hasFiles: boolean,
|
||||
readFile: (path: string) -> (buffer?, Err.Err?),
|
||||
exists: (path: string) -> boolean,
|
||||
}
|
||||
|
||||
-- looksLikePath(s) -> true for short strings without NUL / newline bytes
|
||||
function Input.looksLikePath(s: string): boolean
|
||||
return #s > 0 and #s < 4096 and not string.find(s, "\0", 1, true) and not string.find(s, "[\n\r]")
|
||||
end
|
||||
|
||||
-- resolve(src, opts?, host?) -> buffer, nil, name? | nil, Err
|
||||
function Input.resolve(src: any, opts: any?, host: HostLike?): (buffer?, Err.Err?, string?)
|
||||
local o = opts or {}
|
||||
local t = type(src)
|
||||
if t == "buffer" then
|
||||
return src, nil, o.name
|
||||
elseif t == "string" then
|
||||
local isPath = false
|
||||
if o.path == true then
|
||||
isPath = true
|
||||
elseif o.data ~= true and host and host.hasFiles and Input.looksLikePath(src) then
|
||||
isPath = host.exists(src)
|
||||
end
|
||||
if isPath then
|
||||
if not host then
|
||||
return nil, Err.new(Err.IO, "no host to read " .. src), nil
|
||||
end
|
||||
local b, err = host.readFile(src)
|
||||
if not b then
|
||||
return nil, err, nil
|
||||
end
|
||||
return b, nil, src
|
||||
end
|
||||
return buffer.fromstring(src), nil, o.name
|
||||
end
|
||||
return nil, Err.new(Err.ARG, "expected a buffer, a string or a path, got " .. typeof(src)), nil
|
||||
end
|
||||
|
||||
-- extension(name) -> the lower-case extension without the dot ("png"), or nil
|
||||
function Input.extension(name: string?): string?
|
||||
if not name then
|
||||
return nil
|
||||
end
|
||||
local ext = string.match(name, "%.([%w_]+)$")
|
||||
return if ext then string.lower(ext) else nil
|
||||
end
|
||||
|
||||
-- sibling(opts, uri) -> buffer | nil: a file the data being decoded refers to (a palette, an animation, an archive
|
||||
-- part, ...): opts.files[uri] (a buffer or a string), else opts.resolve(uri) (errors in it count as not found)
|
||||
function Input.sibling(opts: any?, uri: string): buffer?
|
||||
if type(opts) ~= "table" then
|
||||
return nil
|
||||
end
|
||||
local v: any = nil
|
||||
if type(opts.files) == "table" then
|
||||
v = opts.files[uri]
|
||||
end
|
||||
if v == nil and type(opts.resolve) == "function" then
|
||||
local ok, got = pcall(opts.resolve, uri)
|
||||
v = if ok then got else nil
|
||||
end
|
||||
if type(v) == "string" then
|
||||
return buffer.fromstring(v)
|
||||
end
|
||||
return if type(v) == "buffer" then v else nil
|
||||
end
|
||||
|
||||
return Input
|
||||
@@ -0,0 +1,164 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- core/Md5: MD5 (RFC 1321), incremental. Written from the RFC. Used by the FLAC encoder (STREAMINFO's MD5 of the PCM);
|
||||
-- not for security.
|
||||
--
|
||||
-- local h = Md5.new()
|
||||
-- h:update(buf, offset?, length?) -- any number of times
|
||||
-- local digest = h:digest() -- 16-byte string (the state is finished; make a new one to hash again)
|
||||
-- Md5.hex(digest) / Md5.of(buf) -> hex string
|
||||
-- Never throws (ranges are clamped to the buffer).
|
||||
local Md5 = {}
|
||||
|
||||
local band, bor, bxor, bnot, lrotate = bit32.band, bit32.bor, bit32.bxor, bit32.bnot, bit32.lrotate
|
||||
local readu32, writeu32, writeu8 = buffer.readu32, buffer.writeu32, buffer.writeu8
|
||||
|
||||
local K = table.create(64, 0)
|
||||
for i = 0, 63 do
|
||||
K[i + 1] = math.floor(math.abs(math.sin(i + 1)) * 4294967296) % 4294967296
|
||||
end
|
||||
local S = {
|
||||
7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22,
|
||||
5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20,
|
||||
4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,
|
||||
6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21,
|
||||
}
|
||||
local G = table.create(64, 0)
|
||||
for i = 0, 63 do
|
||||
if i < 16 then
|
||||
G[i + 1] = i
|
||||
elseif i < 32 then
|
||||
G[i + 1] = (5 * i + 1) % 16
|
||||
elseif i < 48 then
|
||||
G[i + 1] = (3 * i + 5) % 16
|
||||
else
|
||||
G[i + 1] = (7 * i) % 16
|
||||
end
|
||||
end
|
||||
|
||||
export type State = {
|
||||
a: number,
|
||||
b: number,
|
||||
c: number,
|
||||
d: number,
|
||||
block: buffer, -- 64-byte pending block
|
||||
fill: number,
|
||||
total: number, -- bytes hashed
|
||||
update: (self: State, buf: buffer, offset: number?, length: number?) -> (),
|
||||
digest: (self: State) -> string,
|
||||
}
|
||||
|
||||
local M = table.create(16, 0)
|
||||
|
||||
local function compress(st: State, blk: buffer, o: number)
|
||||
for i = 0, 15 do
|
||||
M[i + 1] = readu32(blk, o + i * 4)
|
||||
end
|
||||
local a, b, c, d = st.a, st.b, st.c, st.d
|
||||
for i = 0, 63 do
|
||||
local f
|
||||
if i < 16 then
|
||||
f = bor(band(b, c), band(bnot(b), d))
|
||||
elseif i < 32 then
|
||||
f = bor(band(d, b), band(bnot(d), c))
|
||||
elseif i < 48 then
|
||||
f = bxor(b, c, d)
|
||||
else
|
||||
f = bxor(c, bor(b, bnot(d)))
|
||||
end
|
||||
local tmp = d
|
||||
d = c
|
||||
c = b
|
||||
b = (b + lrotate((a + f + K[i + 1] + M[G[i + 1] + 1]) % 4294967296, S[i + 1])) % 4294967296
|
||||
a = tmp
|
||||
end
|
||||
st.a = (st.a + a) % 4294967296
|
||||
st.b = (st.b + b) % 4294967296
|
||||
st.c = (st.c + c) % 4294967296
|
||||
st.d = (st.d + d) % 4294967296
|
||||
end
|
||||
|
||||
local function update(self: State, buf: buffer, offset: number?, length: number?)
|
||||
local n = buffer.len(buf)
|
||||
local o = math.clamp(offset or 0, 0, n)
|
||||
local len = math.clamp(length or (n - o), 0, n - o)
|
||||
self.total += len
|
||||
local blk = self.block
|
||||
if self.fill > 0 then
|
||||
local take = math.min(64 - self.fill, len)
|
||||
buffer.copy(blk, self.fill, buf, o, take)
|
||||
self.fill += take
|
||||
o += take
|
||||
len -= take
|
||||
if self.fill == 64 then
|
||||
compress(self, blk, 0)
|
||||
self.fill = 0
|
||||
end
|
||||
end
|
||||
while len >= 64 do
|
||||
compress(self, buf, o)
|
||||
o += 64
|
||||
len -= 64
|
||||
end
|
||||
if len > 0 then
|
||||
buffer.copy(blk, 0, buf, o, len)
|
||||
self.fill = len
|
||||
end
|
||||
end
|
||||
|
||||
local function digest(self: State): string
|
||||
local bits = self.total * 8
|
||||
local blk = self.block
|
||||
local f = self.fill
|
||||
writeu8(blk, f, 0x80)
|
||||
f += 1
|
||||
if f > 56 then
|
||||
buffer.fill(blk, f, 0, 64 - f)
|
||||
compress(self, blk, 0)
|
||||
f = 0
|
||||
end
|
||||
buffer.fill(blk, f, 0, 56 - f)
|
||||
writeu32(blk, 56, bits % 4294967296)
|
||||
writeu32(blk, 60, math.floor(bits / 4294967296) % 4294967296)
|
||||
compress(self, blk, 0)
|
||||
self.fill = 0
|
||||
local out = buffer.create(16)
|
||||
writeu32(out, 0, self.a)
|
||||
writeu32(out, 4, self.b)
|
||||
writeu32(out, 8, self.c)
|
||||
writeu32(out, 12, self.d)
|
||||
return buffer.tostring(out)
|
||||
end
|
||||
|
||||
function Md5.new(): State
|
||||
return {
|
||||
a = 0x67452301,
|
||||
b = 0xEFCDAB89,
|
||||
c = 0x98BADCFE,
|
||||
d = 0x10325476,
|
||||
block = buffer.create(64),
|
||||
fill = 0,
|
||||
total = 0,
|
||||
update = update,
|
||||
digest = digest,
|
||||
}
|
||||
end
|
||||
|
||||
-- hex(digest) -> 32 lower-case hex digits
|
||||
function Md5.hex(d: string): string
|
||||
local out = table.create(#d)
|
||||
for i = 1, #d do
|
||||
out[i] = string.format("%02x", string.byte(d, i))
|
||||
end
|
||||
return table.concat(out)
|
||||
end
|
||||
|
||||
-- of(buf, offset?, length?) -> hex digest
|
||||
function Md5.of(buf: buffer, offset: number?, length: number?): string
|
||||
local h = Md5.new()
|
||||
h:update(buf, offset, length)
|
||||
return Md5.hex(h:digest())
|
||||
end
|
||||
|
||||
return Md5
|
||||
@@ -0,0 +1,36 @@
|
||||
--!strict
|
||||
-- core/Sniff: helpers for the formats' sniff functions (which return 0..1 from the first bytes, never consuming and
|
||||
-- never throwing): magic comparison and the start of a text file without its UTF-8 byte order mark.
|
||||
--
|
||||
-- function Qoi.sniff(r) return Sniff.magic(r, "qoif") end -- 1 or 0
|
||||
-- function Gz.sniff(r) return Sniff.magic(r, "\31\139\8", 0, 0.9) end -- a custom score
|
||||
-- local head = Sniff.text(r, 64) -- the first 64 bytes, BOM dropped
|
||||
-- local head = Sniff.text(r, 512, true) -- ... and leading whitespace too
|
||||
-- local text = Sniff.dropBom(whole) -- decoders: the BOM off a whole file
|
||||
local Sniff = {}
|
||||
|
||||
type Peeker = { peek: (any, number, number?) -> string }
|
||||
|
||||
-- magic(r, s, at?, score?) -> score (default 1) when the bytes at offset at (default 0) are s, else 0
|
||||
function Sniff.magic(r: Peeker, s: string, at: number?, score: number?): number
|
||||
return if r:peek(#s, at) == s then score or 1 else 0
|
||||
end
|
||||
|
||||
-- dropBom(s) -> s without a leading UTF-8 byte order mark (for decoders reading a whole text file)
|
||||
function Sniff.dropBom(s: string): string
|
||||
return if string.sub(s, 1, 3) == "\239\187\191" then string.sub(s, 4) else s
|
||||
end
|
||||
|
||||
-- text(r, n, trim?) -> up to n bytes from the start without a UTF-8 BOM (and leading whitespace when trim)
|
||||
function Sniff.text(r: Peeker, n: number, trim: boolean?): string
|
||||
local s = r:peek(n)
|
||||
if string.sub(s, 1, 3) == "\239\187\191" then
|
||||
s = string.sub(s, 4)
|
||||
end
|
||||
if trim then
|
||||
s = string.match(s, "^%s*(.*)$") :: string
|
||||
end
|
||||
return s
|
||||
end
|
||||
|
||||
return Sniff
|
||||
Reference in new issue
Block a user