From 44a599f00c8382c1ba58067080b7bdca535cd7a5 Mon Sep 17 00:00:00 2001 From: 4DBug Date: Sat, 31 Oct 2026 12:28:33 -0500 Subject: [PATCH] ws-cleanup: codecs/compress/PackBits; UTF-16 and half floats from one place - codecs/compress/PackBits (new, from image/tiff/Pixels): decode (lenient count + a clean flag for strict callers) and encode; image/tiff and layered/psd (which carried its own decoder) use it. tests/codecs/packbits. - codecs/text/Text.utf16(buf, offset, length, bigEndian?): the UTF-16 -> UTF-8 conversion codecs/font/TrueType, archive/7z and layered/psd each wrote (lone surrogates -> U+FFFD everywhere now). - image/tiff/Pixels and data/cbor use core/Bytes.half instead of their own float16 decoders. --- docs/FORMATS.md | 1 + src/Formats/Registry.luau | 6 +- src/Formats/archive/7z/Header.luau | 17 +--- src/Formats/archive/7z/init.luau | 2 +- src/Formats/codecs/compress/PackBits.luau | 95 ++++++++++++++++++++ src/Formats/codecs/font/TrueType/Tables.luau | 20 +---- src/Formats/codecs/text/Text/init.luau | 28 ++++++ src/Formats/data/cbor/Codec.luau | 12 +-- src/Formats/image/tiff/Pixels.luau | 80 +---------------- src/Formats/image/tiff/init.luau | 7 +- src/Formats/layered/psd/Channels.luau | 35 +------- src/Formats/layered/psd/init.luau | 21 +---- tests/codecs/packbits.luau | 45 ++++++++++ 13 files changed, 191 insertions(+), 178 deletions(-) create mode 100644 src/Formats/codecs/compress/PackBits.luau create mode 100644 tests/codecs/packbits.luau diff --git a/docs/FORMATS.md b/docs/FORMATS.md index 13962b1..ba8eb03 100644 --- a/docs/FORMATS.md +++ b/docs/FORMATS.md @@ -101,6 +101,7 @@ Codecs (layer 3, shared by formats; not loadable on their own), by domain: - codecs/compress/Deflate: DEFLATE (RFC 1951) decoder and compressor, with the zlib (RFC 1950) and gzip (RFC 1952) wrappers. - codecs/compress/Lzma: LZMA and LZMA2 decoders, the .lzma ("LZMA alone") wrapper and the .xz container. A codec (layer 3): - codecs/compress/Lzw: LZW decoder and encoder in the two variants file formats use. A codec (layer 1): image/gif and + - codecs/compress/PackBits: the Apple PackBits run-length code (TIFF compression 32773, PSD / PSB RLE channels, IFF - codecs/compress/Zstd: Zstandard decompression (RFC 8878). A codec (layer 3): archive/zst and anything else carrying zstd - **entropy** - codecs/entropy/Huffman: canonical Huffman codes (RFC 1951 3.2.2 / JPEG Annex C order) for decoders and encoders. diff --git a/src/Formats/Registry.luau b/src/Formats/Registry.luau index d5cf13f..4e500fb 100644 --- a/src/Formats/Registry.luau +++ b/src/Formats/Registry.luau @@ -128,7 +128,7 @@ local list: { Entry } = { path = "image/tiff", extensions = { "tif", "tiff" }, mime = { "image/tiff" }, - deps = { "core/Err", "core/Bytes", "Types", "codecs/compress/Deflate", "codecs/compress/Lzw" }, + deps = { "core/Err", "core/Bytes", "Types", "codecs/compress/Deflate", "codecs/compress/Lzw", "codecs/compress/PackBits" }, caps = { decode = true, encode = true, alpha = true, pages = true, depths = { 1, 2, 4, 8, 16, 32 }, float = true }, load = function() return require("@formats/image/tiff") @@ -548,7 +548,7 @@ local list: { Entry } = { path = "archive/7z", extensions = { "7z" }, mime = { "application/x-7z-compressed" }, - deps = { "core/Err", "core/Bytes", "core/Crc32", "Types", "codecs/compress/Lzma", "codecs/compress/Deflate", "codecs/compress/Bzip2", "codecs/compress/Zstd", "containers/Archive" }, + deps = { "core/Err", "core/Bytes", "core/Crc32", "Types", "codecs/compress/Lzma", "codecs/compress/Deflate", "codecs/compress/Bzip2", "codecs/compress/Zstd", "containers/Archive", "codecs/text/Text" }, caps = { decode = true, encode = false, methods = { "copy", "lzma", "lzma2", "deflate", "bcj", "delta" }, encrypted = false }, load = function() return require("@formats/archive/7z") @@ -788,7 +788,7 @@ local list: { Entry } = { path = "layered/psd", extensions = { "psd", "psb" }, mime = { "image/vnd.adobe.photoshop" }, - deps = { "core/Err", "core/Bytes", "Types", "codecs/image/Blend", "codecs/compress/Deflate" }, + deps = { "core/Err", "core/Bytes", "Types", "codecs/image/Blend", "codecs/compress/Deflate", "codecs/compress/PackBits", "codecs/text/Text" }, caps = { decode = true, encode = true, layers = true, groups = true, depths = { 1, 8, 16, 32 } }, load = function() return require("@formats/layered/psd") diff --git a/src/Formats/archive/7z/Header.luau b/src/Formats/archive/7z/Header.luau index 6f5ba24..88f0e04 100644 --- a/src/Formats/archive/7z/Header.luau +++ b/src/Formats/archive/7z/Header.luau @@ -3,6 +3,7 @@ -- streams info (pack info, folders with their coders and bind pairs, substreams), files info (names, empty streams / -- files, anti items, times, attributes). Not --!native: a parse failure raises an Err value that parse() catches and -- returns; any other error is a bug and propagates (the tests' fuzz pass sees it). +local Text = require("@formats/codecs/text/Text") local Err = require("@formats/core/Err") local Bytes = require("@formats/core/Bytes") @@ -395,21 +396,7 @@ local function streamsInfo(r: Bytes.Reader): Streams end local function utf16(b: buffer, o: number, n: number): string - local out = {} - local i = 0 - while i + 1 < n do - local u = buffer.readu16(b, o + i) - i += 2 - if u >= 0xD800 and u < 0xDC00 and i + 1 < n then - local lo = buffer.readu16(b, o + i) - if lo >= 0xDC00 and lo < 0xE000 then - u = 0x10000 + (u - 0xD800) * 1024 + (lo - 0xDC00) - i += 2 - end - end - table.insert(out, utf8.char(u)) - end - return table.concat(out) + return Text.utf16(b, o, n, false) end local function filesInfo(r: Bytes.Reader): { File } diff --git a/src/Formats/archive/7z/init.luau b/src/Formats/archive/7z/init.luau index 4691ae7..4121b34 100644 --- a/src/Formats/archive/7z/init.luau +++ b/src/Formats/archive/7z/init.luau @@ -26,7 +26,7 @@ local SevenZip = { category = "archive", extensions = { "7z" }, mime = { "application/x-7z-compressed" }, - deps = { "core/Err", "core/Bytes", "core/Crc32", "Types", "codecs/compress/Lzma", "codecs/compress/Deflate", "codecs/compress/Bzip2", "codecs/compress/Zstd", "containers/Archive" }, + deps = { "core/Err", "core/Bytes", "core/Crc32", "Types", "codecs/compress/Lzma", "codecs/compress/Deflate", "codecs/compress/Bzip2", "codecs/compress/Zstd", "containers/Archive", "codecs/text/Text" }, caps = { decode = true, encode = false, methods = { "copy", "lzma", "lzma2", "deflate", "bcj", "delta" }, encrypted = false }, } diff --git a/src/Formats/codecs/compress/PackBits.luau b/src/Formats/codecs/compress/PackBits.luau new file mode 100644 index 0000000..e3fd12f --- /dev/null +++ b/src/Formats/codecs/compress/PackBits.luau @@ -0,0 +1,95 @@ +--!strict +--!native +--!optimize 2 +-- codecs/compress/PackBits: the Apple PackBits run-length code (TIFF compression 32773, PSD / PSB RLE channels, IFF +-- ByteRun1, ...): a header byte n < 128 copies n + 1 literal bytes, n > 128 repeats the next byte 257 - n times, +-- 128 is a no-op. Decode and encode; moved here from image/tiff (TIFF and PSD both carried a decoder). Never throws. +-- +-- local written, srcEnd, clean = PackBits.decode(src, p, limit, dst, d, n) -- up to n bytes into dst at d +-- local packed = PackBits.encode(src, offset, length) -- a buffer (one row at a time) +local PackBits = {} + +local readu8, writeu8 = buffer.readu8, buffer.writeu8 + +-- decode(src, p, limit, dst, d, n) -> bytes written (<= n), the source offset after the codes used, clean (false when +-- the source ran out before n bytes or a run overflowed n: lenient callers keep what was written, strict ones fail) +function PackBits.decode(src: buffer, p: number, limit: number, dst: buffer, d: number, n: number): (number, number, boolean) + local o, lim = d, d + n + local clean = true + while o < lim do + if p >= limit then + clean = false + break + end + local h = readu8(src, p) + p += 1 + if h < 128 then + local want = h + 1 + local cnt = math.min(want, limit - p, lim - o) + if cnt < want then + clean = false + end + if cnt > 0 then + buffer.copy(dst, o, src, p, cnt) + o += cnt + end + p += want + if p > limit then + p = limit + break + end + elseif h > 128 then + if p >= limit then + clean = false + break + end + local want = 257 - h + local cnt = math.min(want, lim - o) + if cnt < want then + clean = false + end + buffer.fill(dst, o, readu8(src, p), cnt) + p += 1 + o += cnt + end + end + return o - d, p, clean +end + +-- encode(src, offset, length) -> buffer: runs of 3+ equal bytes as repeats, the rest as literals (at most 128 each) +function PackBits.encode(src: buffer, off: number, len: number): buffer + local out = buffer.create(len + (len + 127) // 128 + 8) + local o = 0 + local p, e = off, off + len + while p < e do + local run = 1 + local v = readu8(src, p) + while p + run < e and run < 128 and readu8(src, p + run) == v do + run += 1 + end + if run >= 3 then + writeu8(out, o, 257 - run) + writeu8(out, o + 1, v) + o += 2 + p += run + else + local s = p + local n = 0 + while p < e and n < 128 do + if p + 2 < e and readu8(src, p) == readu8(src, p + 1) and readu8(src, p) == readu8(src, p + 2) then + break + end + p += 1 + n += 1 + end + writeu8(out, o, n - 1) + buffer.copy(out, o + 1, src, s, n) + o += n + 1 + end + end + local r = buffer.create(o) + buffer.copy(r, 0, out, 0, o) + return r +end + +return PackBits diff --git a/src/Formats/codecs/font/TrueType/Tables.luau b/src/Formats/codecs/font/TrueType/Tables.luau index 9086053..c958a6b 100644 --- a/src/Formats/codecs/font/TrueType/Tables.luau +++ b/src/Formats/codecs/font/TrueType/Tables.luau @@ -6,6 +6,7 @@ -- domain / MIT) as a reference for cmap 4 and the GPOS walk. -- -- Never throws: every read is range-checked (out of range reads as 0). +local Text = require("@formats/codecs/text/Text") local Tables = {} local readu8 = buffer.readu8 @@ -423,24 +424,7 @@ local MAC_ROMAN_HIGH = { } local function utf16be(b: buffer, o: number, n: number): string - local out = {} - local k = 0 - while k + 1 < n do - local c = u16(b, o + k) - k += 2 - if c >= 0xD800 and c < 0xDC00 and k + 1 < n then - local lo = u16(b, o + k) - if lo >= 0xDC00 and lo < 0xE000 then - c = 0x10000 + (c - 0xD800) * 1024 + (lo - 0xDC00) - k += 2 - end - end - if c >= 0xD800 and c < 0xE000 then - c = 0xFFFD - end - table.insert(out, utf8.char(c)) - end - return table.concat(out) + return Text.utf16(b, o, n, true) end local function macRoman(b: buffer, o: number, n: number): string diff --git a/src/Formats/codecs/text/Text/init.luau b/src/Formats/codecs/text/Text/init.luau index 6980e96..fe1e2d5 100644 --- a/src/Formats/codecs/text/Text/init.luau +++ b/src/Formats/codecs/text/Text/init.luau @@ -31,6 +31,34 @@ function Text.trim(s: string): string return (string.match(s, "^%s*(.-)%s*$") :: string) end +-- utf16(buf, offset, length, bigEndian?) -> UTF-8 text of UTF-16 code units (surrogate pairs joined, lone surrogates +-- become U+FFFD; an odd last byte is ignored). Font names, 7z / PSD / Windows strings, XML in UTF-16. +function Text.utf16(b: buffer, o: number, n: number, bigEndian: boolean?): string + local out = {} + local k = 0 + local readu16 = buffer.readu16 + local function unit(at: number): number + local v = readu16(b, at) + return if bigEndian then (v % 256) * 256 + v // 256 else v + end + while k + 1 < n do + local c = unit(o + k) + k += 2 + if c >= 0xD800 and c < 0xDC00 and k + 1 < n then + local lo = unit(o + k) + if lo >= 0xDC00 and lo < 0xE000 then + c = 0x10000 + (c - 0xD800) * 1024 + (lo - 0xDC00) + k += 2 + end + end + if c >= 0xD800 and c < 0xE000 then + c = 0xFFFD + end + table.insert(out, Text.utf8char(c)) + end + return table.concat(out) +end + -- toUtf8(text, warnings?) -> text as UTF-8: strips a BOM; text that is not valid UTF-8 is read as Windows-1252 function Text.toUtf8(text: string, warnings: { string }?): string if string.sub(text, 1, 3) == "\239\187\191" then diff --git a/src/Formats/data/cbor/Codec.luau b/src/Formats/data/cbor/Codec.luau index 0bbc2eb..5c2db05 100644 --- a/src/Formats/data/cbor/Codec.luau +++ b/src/Formats/data/cbor/Codec.luau @@ -142,17 +142,7 @@ local function chunks(st: St, major: number, depth: number): (string?, string?) end end -local function half(h: number): number - local s = if h >= 32768 then -1 else 1 - local e = (h // 1024) % 32 - local m = h % 1024 - 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 +local half = Bytes.half local function tag(st: St, t: number, v: any): (any, string?) if t == 0 and st.dates then diff --git a/src/Formats/image/tiff/Pixels.luau b/src/Formats/image/tiff/Pixels.luau index 68a3db9..82453b5 100644 --- a/src/Formats/image/tiff/Pixels.luau +++ b/src/Formats/image/tiff/Pixels.luau @@ -1,10 +1,11 @@ --!strict --!native --!optimize 2 --- image/tiff/Pixels: the hot loops of the TIFF codec (native; never throws): PackBits, the horizontal (2) and +-- image/tiff/Pixels: the hot loops of the TIFF codec (native; never throws): the horizontal (2) and -- floating-point (3) predictors both ways (TIFF 6.0 section 14, Adobe TIFF technote 3; predictor 3 as libtiff's -- fpAcc / fpDiff), fill-order bit reversal, and the sample converter from decoded planes to a Types.Image layout. -- Callers (image/tiff/init) size every buffer before calling: planes hold h rows of rb bytes plus 8 bytes of slack. +local Bytes = require("@formats/core/Bytes") local readu8, writeu8, readu16, writeu16, readu32, writeu32 = buffer.readu8, buffer.writeu8, buffer.readu16, buffer.writeu16, buffer.readu32, buffer.writeu32 local readf32, writef32, readf64 = buffer.readf32, buffer.writef32, buffer.readf64 local band, bor, rshift, lshift, byteswap = bit32.band, bit32.bor, bit32.rshift, bit32.lshift, bit32.byteswap @@ -40,71 +41,6 @@ end Pixels.f32 = f32 Pixels.f64 = f64 --- ------------------------------------------------------------------------------------------------ PackBits --- packbits(src, off, len, out, outOff, max) -> bytes written (stops when out or src is exhausted) -function Pixels.packbits(src: buffer, off: number, len: number, out: buffer, outOff: number, max: number): number - local p, e = off, off + len - local o, lim = outOff, outOff + max - while p < e and o < lim do - local n = readu8(src, p) - p += 1 - if n < 128 then - local cnt = math.min(n + 1, e - p, lim - o) - if cnt <= 0 then - break - end - buffer.copy(out, o, src, p, cnt) - p += n + 1 - o += cnt - elseif n > 128 then - if p >= e then - break - end - local cnt = math.min(257 - n, lim - o) - buffer.fill(out, o, readu8(src, p), cnt) - p += 1 - o += cnt - end - end - return o - outOff -end - --- packbitsEncode(src, off, len) -> buffer (one row at a time is the caller's business) -function Pixels.packbitsEncode(src: buffer, off: number, len: number): buffer - local out = buffer.create(len + (len + 127) // 128 + 8) - local o = 0 - local p, e = off, off + len - while p < e do - local run = 1 - local v = readu8(src, p) - while p + run < e and run < 128 and readu8(src, p + run) == v do - run += 1 - end - if run >= 3 then - writeu8(out, o, 257 - run) - writeu8(out, o + 1, v) - o += 2 - p += run - else - local s = p - local n = 0 - while p < e and n < 128 do - if p + 2 < e and readu8(src, p) == readu8(src, p + 1) and readu8(src, p) == readu8(src, p + 2) then - break - end - p += 1 - n += 1 - end - writeu8(out, o, n - 1) - buffer.copy(out, o + 1, src, s, n) - o += n + 1 - end - end - local r = buffer.create(o) - buffer.copy(r, 0, out, 0, o) - return r -end - -- ------------------------------------------------------------------------------------------------ fill order local REV = buffer.create(256) for i = 0, 255 do @@ -212,17 +148,7 @@ function Pixels.predict3(b: buffer, off: number, rows: number, rb: number, bytes end -- ------------------------------------------------------------------------------------------------ samples -local function half(h: number): number - local s = if h >= 0x8000 then -1 else 1 - local e = band(rshift(h, 10), 31) - local m = 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 +local half = Bytes.half export type Params = { planes: { buffer }, diff --git a/src/Formats/image/tiff/init.luau b/src/Formats/image/tiff/init.luau index 549b166..e26add9 100644 --- a/src/Formats/image/tiff/init.luau +++ b/src/Formats/image/tiff/init.luau @@ -18,6 +18,7 @@ local Types = require("@formats/Types") local Deflate = require("@formats/codecs/compress/Deflate") local Lzw = require("@formats/codecs/compress/Lzw") local Pixels = require("@formats/image/tiff/Pixels") +local PackBits = require("@formats/codecs/compress/PackBits") -- ------------------------------------------------------------------------------------------------ contract local Tiff = { @@ -25,7 +26,7 @@ local Tiff = { category = "image", extensions = { "tif", "tiff" }, mime = { "image/tiff" }, - deps = { "core/Err", "core/Bytes", "Types", "codecs/compress/Deflate", "codecs/compress/Lzw" }, + deps = { "core/Err", "core/Bytes", "Types", "codecs/compress/Deflate", "codecs/compress/Lzw", "codecs/compress/PackBits" }, caps = { decode = true, encode = true, alpha = true, pages = true, depths = { 1, 2, 4, 8, 16, 32 }, float = true }, } @@ -237,7 +238,7 @@ local function inflateChunk(c: Ctx, comp: number, p: number, len: number, out: b buffer.copy(out, o, raw :: buffer, 0, n) return n, nil elseif comp == 32773 then - return Pixels.packbits(src, sp, len, out, o, size), nil + return (PackBits.decode(src, sp, sp + len, out, o, size)), nil end return nil, Err.new(Err.UNSUPPORTED, "compression " .. comp) end @@ -671,7 +672,7 @@ local function encodePage(img: Types.Image, o: EncodeOptions, comp: number): ({ else local parts, total = {}, 0 for r = 0, rows - 1 do - local p = Pixels.packbitsEncode(raw, r * rb, rb) + local p = PackBits.encode(raw, r * rb, rb) table.insert(parts, p) total += buffer.len(p) end diff --git a/src/Formats/layered/psd/Channels.luau b/src/Formats/layered/psd/Channels.luau index 811f076..cedf85b 100644 --- a/src/Formats/layered/psd/Channels.luau +++ b/src/Formats/layered/psd/Channels.luau @@ -5,6 +5,7 @@ -- writer. Adobe Photoshop File Formats Specification (2016), "Channel image data" / "Image data section". -- Never throws: every read is range-checked first; failures return nil, "message". local Deflate = require("@formats/codecs/compress/Deflate") +local PackBits = require("@formats/codecs/compress/PackBits") local Channels = {} @@ -21,36 +22,6 @@ function Channels.rowBytes(w: number, depth: number): number return w * (depth // 8) end --- packbits(src, p, limit, dst, d, n) -> the source offset after decoding n bytes into dst at d | nil -local function packbits(src: buffer, p: number, limit: number, dst: buffer, d: number, n: number): number? - local e = d + n - while d < e do - if p >= limit then - return nil - end - local h = readu8(src, p) - p += 1 - if h < 128 then - local c = h + 1 - if p + c > limit or d + c > e then - return nil - end - bcopy(dst, d, src, p, c) - p += c - d += c - elseif h > 128 then - local c = 257 - h - if p >= limit or d + c > e then - return nil - end - bfill(dst, d, readu8(src, p), c) - p += 1 - d += c - end - end - return p -end - -- rleCounts(src, p, limit, rows, psb) -> { counts }, offset after them | nil function Channels.rleCounts(src: buffer, p: number, limit: number, rows: number, psb: boolean): ({ number }?, number) local size = if psb then 4 else 2 @@ -76,8 +47,8 @@ function Channels.rle(src: buffer, p: number, limit: number, counts: { number }, if p + c > limit then return nil, "RLE row past the end" end - local q = packbits(src, p, p + c, dst, d + i * rowBytes, rowBytes) - if q == nil then + local _, _, clean = PackBits.decode(src, p, p + c, dst, d + i * rowBytes, rowBytes) + if not clean then -- a short or overlong row: keep what decoded, zero the rest (Photoshop is lenient too) return nil, "bad RLE row" end diff --git a/src/Formats/layered/psd/init.luau b/src/Formats/layered/psd/init.luau index af83e5f..4a13349 100644 --- a/src/Formats/layered/psd/init.luau +++ b/src/Formats/layered/psd/init.luau @@ -15,6 +15,7 @@ local Err = require("@formats/core/Err") local Bytes = require("@formats/core/Bytes") local Types = require("@formats/Types") +local Text = require("@formats/codecs/text/Text") local Blend = require("@formats/codecs/image/Blend") local Channels = require("@formats/layered/psd/Channels") @@ -24,7 +25,7 @@ local Psd = { category = "layered", extensions = { "psd", "psb" }, mime = { "image/vnd.adobe.photoshop" }, - deps = { "core/Err", "core/Bytes", "Types", "codecs/image/Blend", "codecs/compress/Deflate" }, + deps = { "core/Err", "core/Bytes", "Types", "codecs/image/Blend", "codecs/compress/Deflate", "codecs/compress/PackBits", "codecs/text/Text" }, caps = { decode = true, encode = true, layers = true, groups = true, depths = { 1, 8, 16, 32 } }, } @@ -97,23 +98,7 @@ local function len(r: Bytes.Reader, psb: boolean): number end local function utf16be(s: string): string - local out = {} - local i = 1 - while i + 1 <= #s do - local c = string.byte(s, i) * 256 + string.byte(s, i + 1) - i += 2 - if c >= 0xD800 and c < 0xDC00 and i + 1 <= #s then - local c2 = string.byte(s, i) * 256 + string.byte(s, i + 1) - if c2 >= 0xDC00 and c2 < 0xE000 then - c = 0x10000 + (c - 0xD800) * 1024 + (c2 - 0xDC00) - i += 2 - end - end - if c ~= 0 then - table.insert(out, utf8.char(c)) - end - end - return table.concat(out) + return (string.gsub(Text.utf16(buffer.fromstring(s), 0, #s, true), "%z", "")) end -- the layer records + channel data at r (a Reader over the layer info, after its length): flat records | nil, Err diff --git a/tests/codecs/packbits.luau b/tests/codecs/packbits.luau new file mode 100644 index 0000000..cc23fb6 --- /dev/null +++ b/tests/codecs/packbits.luau @@ -0,0 +1,45 @@ +-- codecs/compress/PackBits tests: the Apple technote example, encode -> decode round trips of random / runny rows, +-- the clean flag on short input and overflowing runs, and a no-throw pass over garbage. +local T = require("@tests/T") +local PackBits = require("@formats/codecs/compress/PackBits") + +local t = T.suite("packbits") + +-- Apple Technical Note TN1023 +local packed = T.bytes({ 0xFE, 0xAA, 0x02, 0x80, 0x00, 0x2A, 0xFD, 0xAA, 0x03, 0x80, 0x00, 0x2A, 0x22, 0xF7, 0xAA }) +local want = T.bytes({ 0xAA, 0xAA, 0xAA, 0x80, 0x00, 0x2A, 0xAA, 0xAA, 0xAA, 0xAA, 0x80, 0x00, 0x2A, 0x22, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }) +local out = buffer.create(24) +local n, pEnd, clean = PackBits.decode(packed, 0, buffer.len(packed), out, 0, 24) +t:check(n == 24 and pEnd == buffer.len(packed) and clean and buffer.tostring(out) == buffer.tostring(want), "TN1023 example") + +local rand = T.rng(11) +for k = 1, 40 do + local len = rand(600) + local row = buffer.create(len) + local i = 0 + while i < len do + local v, run = rand(256) - 1, (if rand(3) == 1 then rand(200) else 1) + for _ = 1, run do + if i < len then + buffer.writeu8(row, i, v) + i += 1 + end + end + end + local enc = PackBits.encode(row, 0, len) + local dec = buffer.create(len) + local m, _, ok = PackBits.decode(enc, 0, buffer.len(enc), dec, 0, len) + t:check(m == len and ok and buffer.tostring(dec) == buffer.tostring(row), "round trip " .. k) +end + +local short = buffer.create(10) +local m1, _, ok1 = PackBits.decode(T.bytes({ 0x05, 1, 2 }), 0, 3, short, 0, 10) +t:check(m1 == 2 and not ok1, "short literal: what is there, not clean") +local m2, _, ok2 = PackBits.decode(T.bytes({ 0x81, 7 }), 0, 2, short, 0, 10) +t:check(m2 == 10 and not ok2, "a run past n is clipped, not clean") + +t:fuzz("decode", function(b: buffer) + local o = buffer.create(256) + return PackBits.decode(b, 0, buffer.len(b), o, 0, 256) +end, { buffer.tostring(packed) }) +t:done()