codecs/compress: comments to specs only
This commit is contained in:
1 parent
f1ad32105b
commit
f626fb4295
23 files changed
+123
-498
No files matched your search
@@ -1,19 +1,6 @@
|
||||
--!strict
|
||||
-- codecs/compress/Deflate: DEFLATE (RFC 1951) decoder and compressor, with the zlib (RFC 1950) and gzip (RFC 1952) wrappers.
|
||||
-- A codec (layer 1), not a format: png, zip, gzip, woff, tiff, ... build on it. Absorbed from deflate-luau 1.0.0
|
||||
-- (same author; no third-party code); the compressor is new.
|
||||
--
|
||||
-- local Deflate = require("@rosetta/codecs/compress/Deflate")
|
||||
-- local out, err = Deflate.inflate(buf) -- raw deflate -> buffer, consumedEnd | nil, Err
|
||||
-- local out, err = Deflate.inflate(buf, off, len, size) -- a range, with the exact output size if known
|
||||
-- local out, err = Deflate.inflate(buf, off, len, size, dict) -- with a preset dictionary
|
||||
-- local out, err = Deflate.mszip(buf, { { offset, length, size }, ... }) -- MSZIP blocks ("CK" + deflate, chained)
|
||||
-- local out, err = Deflate.zlib(buf) -- checks the Adler-32
|
||||
-- local out, err = Deflate.gunzip(buf) -- checks CRC-32 + ISIZE, concatenates members
|
||||
-- local z = Deflate.compress(buf, { level = 6 }) -- raw deflate; also zlibCompress / gzip
|
||||
-- local head = Deflate.peek(buf, nil, nil, 512) -- the first 512 inflated bytes of gzip / zlib / raw
|
||||
--
|
||||
-- Never throws: bad data and bad arguments return nil, Err (core/Err codes TRUNCATED / CORRUPT / CHECKSUM / ARG).
|
||||
-- codecs/compress/Deflate: DEFLATE (RFC 1951) decoder and compressor, with zlib (RFC 1950) and gzip (RFC 1952).
|
||||
-- Absorbed from deflate-luau 1.0.0 (same author; no third-party code); the compressor is new.
|
||||
local Err = require("@rosetta/core/Err")
|
||||
local Bytes = require("@rosetta/core/Bytes")
|
||||
local Crc32 = require("@rosetta/core/Crc32")
|
||||
@@ -42,11 +29,6 @@ local function inflateErr(msg: string, at: number?): Err.Err
|
||||
return Err.new(code, "inflate: " .. msg, at)
|
||||
end
|
||||
|
||||
-- inflate(buf, offset?, length?, expectedSize?) -> buffer, consumedEnd | nil, Err. Raw deflate.
|
||||
-- expectedSize: the exact uncompressed size when known (a zip entry): the output is allocated once, and a stream
|
||||
-- that inflates to a different size is an error. consumedEnd is the input offset just past the final block.
|
||||
-- dict: a preset dictionary (up to 32 KB of data before the stream, as MSZIP's previous block): back references may
|
||||
-- reach into it; the result holds only the new bytes.
|
||||
function Deflate.inflate(buf: buffer | string, offset: number?, length: number?, expectedSize: number?, dict: buffer?): (buffer?, any)
|
||||
local b, o, l, aerr = args("Deflate.inflate", buf, offset, length)
|
||||
if not b then
|
||||
@@ -82,7 +64,6 @@ function Deflate.inflate(buf: buffer | string, offset: number?, length: number?,
|
||||
return shrink(out, n), consumed
|
||||
end
|
||||
|
||||
-- zlib(buf, offset?, length?, expectedSize?) -> buffer, consumedEnd | nil, Err
|
||||
function Deflate.zlib(buf: buffer | string, offset: number?, length: number?, expectedSize: number?): (buffer?, any)
|
||||
local b, o, l, aerr = args("Deflate.zlib", buf, offset, length)
|
||||
if not b then
|
||||
@@ -116,7 +97,6 @@ function Deflate.zlib(buf: buffer | string, offset: number?, length: number?, ex
|
||||
return out, consumed + 4
|
||||
end
|
||||
|
||||
-- gzipBody(b, p, e) -> the offset of a gzip member's deflate data (past FEXTRA / FNAME / FCOMMENT / FHCRC) | nil, Err
|
||||
local function gzipBody(b: buffer, p: number, e: number): (number?, Err.Err?)
|
||||
local flg = buffer.readu8(b, p + 3)
|
||||
if flg >= 0x20 then
|
||||
@@ -146,8 +126,6 @@ local function gzipBody(b: buffer, p: number, e: number): (number?, Err.Err?)
|
||||
return q, nil
|
||||
end
|
||||
|
||||
-- gunzip(buf, offset?, length?) -> buffer | nil, Err. Members are concatenated; trailing data after a member that is
|
||||
-- not another gzip member (zero padding, garbage) is ignored, as gzip -d does.
|
||||
function Deflate.gunzip(buf: buffer | string, offset: number?, length: number?): (buffer?, Err.Err?)
|
||||
local b, o, l, aerr = args("Deflate.gunzip", buf, offset, length)
|
||||
if not b then
|
||||
@@ -194,7 +172,6 @@ function Deflate.gunzip(buf: buffer | string, offset: number?, length: number?):
|
||||
table.insert(parts, out)
|
||||
total += buffer.len(out)
|
||||
p = consumed + 8
|
||||
-- skip zero padding
|
||||
local z = p
|
||||
while z < e and buffer.readu8(b, z) == 0 do
|
||||
z += 1
|
||||
@@ -215,9 +192,6 @@ function Deflate.gunzip(buf: buffer | string, offset: number?, length: number?):
|
||||
return res, nil
|
||||
end
|
||||
|
||||
-- peek(buf, offset?, length?, n) -> buffer | nil, Err: the first n bytes (fewer when the stream is shorter, or up to
|
||||
-- 258 fewer at a match boundary) a gzip member, zlib stream or raw deflate stream inflates to, without inflating
|
||||
-- the rest or checking checksums: for sniffing compressed files cheaply.
|
||||
function Deflate.peek(buf: buffer | string, offset: number?, length: number?, n: number): (buffer?, Err.Err?)
|
||||
local b, o, l, aerr = args("Deflate.peek", buf, offset, length)
|
||||
if not b then
|
||||
@@ -248,7 +222,7 @@ function Deflate.peek(buf: buffer | string, offset: number?, length: number?, n:
|
||||
end
|
||||
|
||||
export type CompressOptions = {
|
||||
level: number?, -- 0 (stored) .. 9 (best); default 6
|
||||
level: number?,
|
||||
}
|
||||
|
||||
local function level(opts: CompressOptions?): number?
|
||||
@@ -259,7 +233,6 @@ local function level(opts: CompressOptions?): number?
|
||||
return lv
|
||||
end
|
||||
|
||||
-- compress(buf, opts?) -> raw deflate buffer | nil, Err
|
||||
function Deflate.compress(buf: buffer | string, opts: CompressOptions?): (buffer?, Err.Err?)
|
||||
local b = toBuffer(buf)
|
||||
local lv = level(opts)
|
||||
@@ -269,7 +242,6 @@ function Deflate.compress(buf: buffer | string, opts: CompressOptions?): (buffer
|
||||
return Compress.raw(b, 0, buffer.len(b), lv), nil
|
||||
end
|
||||
|
||||
-- zlibCompress(buf, opts?) -> zlib stream | nil, Err
|
||||
function Deflate.zlibCompress(buf: buffer | string, opts: CompressOptions?): (buffer?, Err.Err?)
|
||||
local b = toBuffer(buf)
|
||||
local lv = level(opts)
|
||||
@@ -290,7 +262,6 @@ function Deflate.zlibCompress(buf: buffer | string, opts: CompressOptions?): (bu
|
||||
return out, nil
|
||||
end
|
||||
|
||||
-- gzip(buf, opts?) -> gzip member (no name, mtime 0) | nil, Err
|
||||
function Deflate.gzip(buf: buffer | string, opts: CompressOptions?): (buffer?, Err.Err?)
|
||||
local b = toBuffer(buf)
|
||||
local lv = level(opts)
|
||||
@@ -309,10 +280,8 @@ function Deflate.gzip(buf: buffer | string, opts: CompressOptions?): (buffer?, E
|
||||
return out, nil
|
||||
end
|
||||
|
||||
-- mszip(buf, blocks) -> buffer | nil, Err: MSZIP data (Microsoft's blocked deflate, as in CAB folders and DirectX .x
|
||||
-- files): blocks = { { offset, length, size } }, each buf[offset, offset + length) = "CK" + a raw deflate stream that
|
||||
-- inflates to exactly size bytes using the previous block's output (up to 32 KB) as its dictionary. Returns the
|
||||
-- blocks' output concatenated.
|
||||
-- MSZIP (CAB folders, DirectX .x): each block is "CK" + a raw deflate stream using the previous block's output (up to
|
||||
-- 32 KB) as its dictionary.
|
||||
function Deflate.mszip(buf: buffer, blocks: { { offset: number, length: number, size: number } }): (buffer?, Err.Err?)
|
||||
local total = 0
|
||||
for _, b in blocks do
|
||||
@@ -343,7 +312,6 @@ function Deflate.mszip(buf: buffer, blocks: { { offset: number, length: number,
|
||||
return out, nil
|
||||
end
|
||||
|
||||
-- crc32 / adler32 (kept from deflate-luau's API; the same as core/Crc32.of / core/Adler32.of)
|
||||
Deflate.crc32 = function(buf: buffer | string, offset: number?, length: number?, crc: number?): number
|
||||
local b = toBuffer(buf) or buffer.create(0)
|
||||
return Crc32.of(b, offset, length, crc)
|
||||
|
||||
Reference in new issue
Block a user