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,23 +1,9 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/compress/Lzh: the LHA family of LZSS decoders, used by archive/lha, archive/arj and archive/zoo:
|
||||
-- lh4 / lh5 / lh6 / lh7 / lhx static Huffman blocks (code-length "temp" table, 510 literal / length codes, offset
|
||||
-- bit-length codes); also ARJ methods 1..3 (= "lh6") and Zoo method 2 (= "lh5")
|
||||
-- lh1 adaptive Huffman (LHarc 1.x: 314 codes, grouped frequency tree) with fixed-length
|
||||
-- offset prefixes, 4 KB window
|
||||
-- lzs / lz5 LArc's plain LZSS (2 KB / 4 KB windows, flag bits / flag bytes)
|
||||
-- arj4 ARJ method 4 (unary-prefixed lengths and offsets, no Huffman)
|
||||
-- Every method decodes into a buffer of the known output size; the windows start filled as the original tools fill
|
||||
-- them (spaces; lz5's pattern), and offsets before the start of the output read that fill.
|
||||
-- Ported from lhasa 0.6.0 (Simon Howard, ISC licence: licenses/lhasa-LICENSE.txt): lh_new_decoder.c, lh1_decoder.c,
|
||||
-- lzs_decoder.c, lz5_decoder.c; the static Huffman tables are canonical codes built with codecs/entropy/Huffman instead
|
||||
-- of lhasa's trees. ARJ method 4 is written from the ARJ technical notes (unarj's decode_f description).
|
||||
--
|
||||
-- local out, err = Lzh.decode(src, off, len, outSize, "lh5") -- buffer | nil, message
|
||||
--
|
||||
-- Never throws: bad codes / truncated input return nil, message (truncated input: "truncated ..." after the bytes
|
||||
-- decoded so far would be useless, so nothing partial is returned). Not supported: pm0 / pm1 / pm2 (PMarc), lk7.
|
||||
-- codecs/compress/Lzh: the LHA family of LZSS decoders (lh1, lh4-lh7, lhx, lzs, lz5, ARJ 1-4, Zoo 2).
|
||||
-- Ported from lhasa 0.6.0 (Simon Howard, ISC: licenses/lhasa-LICENSE.txt); ARJ method 4 from the ARJ technical notes.
|
||||
-- Windows start filled as the original tools fill them (spaces; lz5's pattern); offsets before the output read it.
|
||||
local Huffman = require("@rosetta/codecs/entropy/Huffman")
|
||||
|
||||
local Lzh = {}
|
||||
@@ -26,9 +12,6 @@ local readu8, writeu8, readu32 = buffer.readu8, buffer.writeu8, buffer.readu32
|
||||
local band, rshift, lshift = bit32.band, bit32.rshift, bit32.lshift
|
||||
local floor = math.floor
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ bit reader
|
||||
-- an MSB-first bit reader over src[pos, limit): acc holds cnt bits (cnt <= 32); past the end, zero bits are read and
|
||||
-- over counts them
|
||||
type Reader = { src: buffer, pos: number, limit: number, acc: number, cnt: number, over: number }
|
||||
|
||||
local function reader(src: buffer, off: number, len: number): Reader
|
||||
@@ -73,7 +56,6 @@ local function bits(r: Reader, n: number): number
|
||||
return v
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ static Huffman
|
||||
type Table = { tab: buffer, P: number, S: number, single: number }
|
||||
|
||||
local function newTable(nsym: number, P: number): Table
|
||||
@@ -145,7 +127,7 @@ local function readLengths(r: Reader, t: Table, nbits: number, maxN: number, spe
|
||||
writeu8(lens, i, l)
|
||||
if i == special then
|
||||
local z = bits(r, 2)
|
||||
i += z -- zeros (already zero)
|
||||
i += z
|
||||
end
|
||||
i += 1
|
||||
end
|
||||
@@ -235,7 +217,6 @@ local function decodeNew(src: buffer, off: number, len: number, out: buffer, n:
|
||||
return nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ lh1
|
||||
local NUM_CODES = 314
|
||||
local NUM_NODES = NUM_CODES * 2 - 1
|
||||
local REORDER = 32 * 1024
|
||||
@@ -261,7 +242,6 @@ end
|
||||
|
||||
local function decodeLh1(src: buffer, off: number, len: number, out: buffer, n: number): string?
|
||||
local r = reader(src, off, len)
|
||||
-- node arrays (0-based indexes stored at [i + 1])
|
||||
local leaf = table.create(NUM_NODES, false)
|
||||
local child = table.create(NUM_NODES, 0)
|
||||
local parent = table.create(NUM_NODES, 0)
|
||||
@@ -401,7 +381,7 @@ local function decodeLh1(src: buffer, off: number, len: number, out: buffer, n:
|
||||
end
|
||||
end
|
||||
local code = child[ni + 1]
|
||||
-- increment_for_code
|
||||
-- lhasa's increment_for_code
|
||||
if freq[1] >= REORDER then
|
||||
reconstruct()
|
||||
end
|
||||
@@ -438,8 +418,7 @@ local function decodeLh1(src: buffer, off: number, len: number, out: buffer, n:
|
||||
return nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ LArc lzs / lz5
|
||||
-- the ring of a 2 KB / 4 KB window (positions are absolute ring positions, not distances)
|
||||
-- LArc lzs / lz5: a 2 KB / 4 KB ring (absolute ring positions, not distances)
|
||||
local function decodeLzs(src: buffer, off: number, len: number, out: buffer, n: number): string?
|
||||
local r = reader(src, off, len)
|
||||
local ring = buffer.create(2048)
|
||||
@@ -549,7 +528,6 @@ local function decodeLz5(src: buffer, off: number, len: number, out: buffer, n:
|
||||
return nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ ARJ method 4
|
||||
local function decodeArj4(src: buffer, off: number, len: number, out: buffer, n: number): string?
|
||||
local r = reader(src, off, len)
|
||||
local function prefixed(start: number, stop: number): number
|
||||
@@ -591,10 +569,8 @@ local function decodeArj4(src: buffer, off: number, len: number, out: buffer, n:
|
||||
return nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ api
|
||||
Lzh.METHODS = { "lh1", "lh4", "lh5", "lh6", "lh7", "lhx", "lzs", "lz5", "arj4" }
|
||||
|
||||
-- decode(src, off, len, outSize, method) -> buffer | nil, message
|
||||
function Lzh.decode(src: buffer, off: number, len: number, outSize: number, method: string): (buffer?, string?)
|
||||
if off < 0 or len < 0 or off > buffer.len(src) or outSize < 0 or outSize ~= outSize then
|
||||
return nil, "bad arguments"
|
||||
|
||||
Reference in new issue
Block a user