--!strict --!native --!optimize 2 -- containers/Ogg/Page: Ogg pages (RFC 3533). Absorbed from ogg-luau be9f604 (same author, no third-party code): the CRC-32 (poly 0x04C11DB7, MSB first, init 0, the CRC field read as zero), header -- parsing with bounds checks, and the capture-pattern resync (find the next valid page at or after a byte offset, -- forwards or backwards). Nothing here throws: invalid or truncated data gives nil. -- -- A page header (27 bytes + segment table): -- 0 "OggS" | 4 version (0) | 5 flags (1 continued, 2 bos, 4 eos) | 6 granule (i64 LE) | 14 serial (u32) -- 18 sequence (u32) | 22 crc (u32) | 26 segment count | 27 lacing values | data local Crc32 = require("@formats/core/Crc32") local readu8, readu32, readi32 = buffer.readu8, buffer.readu32, buffer.readi32 local band, bxor, lshift, rshift = bit32.band, bit32.bxor, bit32.lshift, bit32.rshift local Page = {} export type Info = { offset: number, -- byte offset of "OggS" size: number, -- header + segment table + data headerSize: number, -- 27 + segment count nsegs: number, flags: number, continued: boolean, bos: boolean, eos: boolean, granule: number, -- -1 when no packet finishes on this page serial: number, sequence: number, } -- CRC table, MSB-first (core/Crc32) local CRC = Crc32.MSB_TABLE Page.CRC_TABLE = CRC -- CRC of bytes [from, to) (caller guarantees bounds), continuing from crc local function crcRange(b: buffer, from: number, to: number, crc: number): number for i = from, to - 1 do crc = bxor(lshift(crc, 8), CRC[bxor(rshift(crc, 24), readu8(b, i)) + 1]) end return crc end -- CRC-32 of a byte range, the Ogg way (the crc field is not zeroed: use for arbitrary data) function Page.crc32(b: buffer, offset: number?, len: number?): number local o = offset or 0 local n = len or (buffer.len(b) - o) if o < 0 or n < 0 or o + n > buffer.len(b) then return 0 end return crcRange(b, o, o + n, 0) end -- Parse the page at pos. Returns Info, or nil + reason ("short" if the buffer ends inside the page, "magic", -- "version", "crc"). verify = false skips the CRC check. function Page.parse(b: buffer, pos: number, verify: boolean?): (Info?, string?) local size = buffer.len(b) if pos < 0 or pos + 27 > size then return nil, "short" end if readu32(b, pos) ~= 0x5367674F then -- "OggS" return nil, "magic" end if readu8(b, pos + 4) ~= 0 then return nil, "version" end local nsegs = readu8(b, pos + 26) local hsize = 27 + nsegs if pos + hsize > size then return nil, "short" end local dlen = 0 for i = pos + 27, pos + hsize - 1 do dlen += readu8(b, i) end local total = hsize + dlen if pos + total > size then return nil, "short" end if verify ~= false then local c = crcRange(b, pos, pos + 22, 0) for _ = 1, 4 do c = bxor(lshift(c, 8), CRC[rshift(c, 24) + 1]) end c = crcRange(b, pos + 26, pos + total, c) if c ~= readu32(b, pos + 22) then return nil, "crc" end end local flags = readu8(b, pos + 5) return { offset = pos, size = total, headerSize = hsize, nsegs = nsegs, flags = flags, continued = band(flags, 1) ~= 0, bos = band(flags, 2) ~= 0, eos = band(flags, 4) ~= 0, granule = readi32(b, pos + 10) * 4294967296 + readu32(b, pos + 6), serial = readu32(b, pos + 14), sequence = readu32(b, pos + 18), }, nil end -- The first valid page starting at an offset in [pos, limit) (limit defaults to the buffer end). Resyncs on the -- capture pattern: bytes that are not a valid page (garbage, a bad CRC, a truncated page) are skipped. function Page.find(b: buffer, pos: number, limit: number?, verify: boolean?): Info? local size = buffer.len(b) local lim = math.min(limit or size, size - 27 + 1) if pos < 0 then pos = 0 end while pos < lim do if readu8(b, pos) == 0x4F and readu32(b, pos) == 0x5367674F then local p = Page.parse(b, pos, verify) if p then return p end end pos += 1 end return nil end -- The last valid page starting at an offset in [floor, pos] (searching backwards). function Page.findBack(b: buffer, pos: number, floor: number?, verify: boolean?): Info? local size = buffer.len(b) local lo = math.max(floor or 0, 0) if pos > size - 27 then pos = size - 27 end while pos >= lo do if readu8(b, pos) == 0x4F and readu32(b, pos) == 0x5367674F then local p = Page.parse(b, pos, verify) if p then return p end end pos -= 1 end return nil end -- The identification of a codec from a stream's first packet (the bos page's packet) function Page.codec(b: buffer, off: number, len: number): string local function is(s: string, at: number): boolean if at + #s > len then return false end for i = 1, #s do if readu8(b, off + at + i - 1) ~= string.byte(s, i) then return false end end return true end if is("OpusHead", 0) then return "opus" elseif len >= 5 and readu8(b, off) == 0x7F and is("FLAC", 1) then return "flac" elseif len >= 7 and readu8(b, off) == 1 and is("vorbis", 1) then return "vorbis" elseif len >= 7 and readu8(b, off) == 0x80 and is("theora", 1) then return "theora" elseif is("Speex ", 0) then return "speex" elseif is("fishead", 0) then return "skeleton" elseif is("fLaC", 0) then return "flac-old" -- pre-1.1.1 Ogg FLAC mapping end return "unknown" end return Page