diff --git a/src/Formats/model/x/init.luau b/src/Formats/model/x/init.luau index 183b5e7..faddb64 100644 --- a/src/Formats/model/x/init.luau +++ b/src/Formats/model/x/init.luau @@ -44,7 +44,6 @@ export type DecodeOptions = { -- ------------------------------------------------------------------------------------------------ constants local ENCODINGS = { ["txt "] = "text", ["bin "] = "binary", tzip = "text", bzip = "binary" } -local WINDOW = 32768 -- ------------------------------------------------------------------------------------------------ sniff function X.sniff(r: Bytes.Reader): number @@ -55,9 +54,9 @@ function X.sniff(r: Bytes.Reader): number end -- ------------------------------------------------------------------------------------------------ decode --- MSZIP: a u32 total size (with the header), then blocks (u16 uncompressed size, u16 compressed size, "CK", deflate data); each block --- may refer back into the previous block's output. That history is handed to the inflater as a stored block in --- front of the compressed one (both start on a byte boundary), and cut off the output again. +-- MSZIP: a u32 total size (with the 16-byte file header), then blocks (u16 uncompressed size, u16 compressed size, +-- "CK" + deflate data). The blocks are walked here; codecs/compress/Deflate.mszip inflates the chain (each block may +-- refer back into the previous block's output). local function mszip(b: buffer, p: number, limit: number, maxBytes: number): (buffer?, Err.Err?) if p + 4 > limit then return nil, Err.new(Err.TRUNCATED, "no MSZIP header", p) @@ -67,36 +66,27 @@ local function mszip(b: buffer, p: number, limit: number, maxBytes: number): (bu return nil, Err.new(Err.LIMIT, "inflates to " .. total .. " bytes", p) end p += 4 - local out = buffer.create(total) + local spans: { { offset: number, length: number, size: number } } = {} local o = 0 while p + 6 <= limit and o < total do local usize, csize = buffer.readu16(b, p), buffer.readu16(b, p + 2) if buffer.readstring(b, p + 4, 2) ~= "CK" or csize < 2 then return nil, Err.new(Err.CORRUPT, "bad MSZIP block", p) end - local data, dlen = p + 6, math.min(csize - 2, limit - p - 6) - local hist = math.min(o, WINDOW) - local src = buffer.create(5 + hist + dlen) - buffer.writeu8(src, 0, 0) -- BFINAL 0, stored - buffer.writeu16(src, 1, hist) - buffer.writeu16(src, 3, bit32.bxor(hist, 0xFFFF)) - buffer.copy(src, 5, out, o - hist, hist) - buffer.copy(src, 5 + hist, b, data, dlen) - local inflated, err = Deflate.inflate(src) - if not inflated then - return nil, Err.new(err.code, "MSZIP block: " .. err.message, p) - end - local got = buffer.len(inflated) - hist - if got ~= usize or o + got > total then + if o + usize > total then return nil, Err.new(Err.CORRUPT, "MSZIP block size mismatch", p) end - buffer.copy(out, o, inflated, hist, got) - o += got - p = data + csize - 2 + table.insert(spans, { offset = p + 4, length = math.min(csize, limit - p - 4), size = usize }) + o += usize + p += 4 + csize end if o < total then return nil, Err.new(Err.TRUNCATED, "MSZIP data ends early", p) end + local out, err = Deflate.mszip(b, spans) + if not out then + return nil, Err.new((err :: Err.Err).code, "MSZIP: " .. (err :: Err.Err).message, (err :: Err.Err).offset) + end return out, nil end