Files
rosetta/src/Formats/codecs/Bzip2/init.luau
T
4DBugandClaude Opus 5.5 fb2dd98db2 ws-cleanup: consistency (lower-case tags, WAVE channel order, loops = play count, Err messages, caps)
- Audio tags keys are lower case everywhere (Types.Tags): FLAC no longer returns TITLE / ARTIST (the raw fields stay
  in meta.source.comments; also for Vorbis and Opus); Vorbis / Opus / FLAC read and write tags through
  codecs/VorbisComment (TRACKNUMBER -> track, DISCNUMBER -> disc, DESCRIPTION -> comment); MP3 albumArtist ->
  albumartist and year -> date (ID3 TYER / TDRC, ID3v1); RIFF INFO / CAF / MP4 freeform names are lower-cased (MP4
  writes iTunSMPB / iTunNORM back in their usual spelling; m4a reads tags.itunsmpb).
- Channel order: Vorbis (and Opus mapping family 1) decode to WAVE order like FLAC / WAV / AAC (both used to return
  the Vorbis order); the Vorbis encoder takes WAVE order and writes Vorbis order. Pcm.WAVE_FROM_VORBIS / Pcm.reorder;
  Vorbis.packetDecoder gives pd.order. Tests reorder oggdec's / opusdec's raw (Vorbis-order) references.
- img.loops is the play count in GIF, APNG and WebP (they already agreed; Types / WebP docs said "loop count");
  tests/api round-trips loops 0 / 1 / 3 through all three.
- Err: messages without the format name (png / midi encoders said "png encode: ..."); codecs/Bzip2 and codecs/Zstd
  check arguments through Bytes.span.
- caps: mp3 encodeLayers = { 2, 3 }, opus encodeChannels = 2.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-31 11:01:22 -05:00

65 lines
2.4 KiB
Luau

--!strict
-- codecs/Bzip2: bzip2 decompressor (BWT + MTF + Huffman, the .bz2 stream format of bzip2 0.9.5 / 1.0.x). A codec
-- (layer 3): archive/bz2 and tar.bz2 build on it. Written from the format description (no third-party code).
-- Supports: block sizes 1..9, every Huffman group layout, block and stream CRCs, concatenated streams (as bzip2 -d).
-- Not supported: randomised blocks (bzip2 0.9.0 and older; Err.UNSUPPORTED), compression.
--
-- local Bzip2 = require("@formats/codecs/Bzip2")
-- local out, consumedEnd = Bzip2.decompress(buf) -- | nil, Err
-- local out = Bzip2.decompress(buf, offset, length, { maxBytes = 2^24 })
--
-- Never throws: bad data and bad arguments return nil, Err (TRUNCATED / CORRUPT / CHECKSUM / LIMIT / ARG).
local Err = require("@formats/core/Err")
local Bytes = require("@formats/core/Bytes")
local Decode = require("@formats/codecs/Bzip2/Decode")
local Bzip2 = {}
export type Options = {
maxBytes: number?, -- refuse output larger than this (default 2^30)
}
local KINDS = {
truncated = Err.TRUNCATED,
corrupt = Err.CORRUPT,
checksum = Err.CHECKSUM,
limit = Err.LIMIT,
unsupported = Err.UNSUPPORTED,
}
-- decompress(buf, offset?, length?, opts?) -> buffer, consumedEnd | nil, Err
function Bzip2.decompress(buf: buffer | string, offset: number?, length: number?, opts: Options?): (buffer?, any)
local b, o, l, aerr = Bytes.span("Bzip2.decompress", buf, offset, length)
if not b then
return nil, aerr
end
local maxBytes = if opts and opts.maxBytes ~= nil then opts.maxBytes else 2 ^ 30
if type(maxBytes) ~= "number" or maxBytes < 0 then
return nil, Err.new(Err.ARG, "Bzip2.decompress: bad maxBytes")
end
local out, nOut, consumed = Decode.decode(b, o, o + l, maxBytes)
if not out then
return nil, Err.new(KINDS[consumed] or Err.CORRUPT, "bzip2: " .. tostring(nOut), o)
end
local res = out
if buffer.len(out) ~= nOut then
res = buffer.create(nOut)
buffer.copy(res, 0, out, 0, nOut)
end
return res, consumed
end
-- crc(buf, offset?, length?) -> the CRC-32/BZIP2 of a range (the block CRC bzip2 stores)
function Bzip2.crc(buf: buffer, offset: number?, length: number?): number
local CT = Decode.crcTable
local o = offset or 0
local e = o + (length or (buffer.len(buf) - o))
local c = 0xFFFFFFFF
for i = o, e - 1 do
c = bit32.bxor(bit32.lshift(c, 8), CT[bit32.bxor(bit32.rshift(c, 24), buffer.readu8(buf, i)) + 1])
end
return bit32.bxor(c, 0xFFFFFFFF)
end
return Bzip2