diff --git a/src/Formats/codecs/audio/ApeTag.luau b/src/Formats/codecs/audio/ApeTag.luau new file mode 100644 index 0000000..259bdcf --- /dev/null +++ b/src/Formats/codecs/audio/ApeTag.luau @@ -0,0 +1,110 @@ +--!strict +-- codecs/audio/ApeTag: APEv1 / APEv2 tags (the tag block of Monkey's Audio, WavPack, Musepack, TTA, OptimFROG and +-- some MP3s) -> Types.Tags. Written from the APEv2 specification (wiki.hydrogenaud.io "APEv2 specification"); no +-- third-party code. A tag sits at the end of the file (before an optional ID3v1 tag) as items + a 32-byte footer +-- "APETAGEX", with an optional 32-byte header in front (APEv2). Item keys become lower case tag keys (Year -> date, +-- Track -> track, Album Artist -> albumartist, Disc -> disc, Comment -> comment); text values holding several +-- values (NUL separated) are joined with "; "; binary items (cover art) and external links stay in info.items. +-- Never throws. +-- +-- local tags, info = ApeTag.find(buf, start, limit) -- the tag ending at limit (or before an ID3v1 tag there) +-- -- tags: { title = ..., artist = ... } | nil (no tag / malformed) +-- -- info: { start = first byte (the header when present), stop = past the footer, version = 1000 | 2000, +-- -- items = { { key = "Title", value = "...", kind = "text" | "binary" | "link" } } } +-- local tags, info = ApeTag.parse(buf, footer, start) -- the tag whose footer is at byte `footer` (>= start) +local ApeTag = {} + +export type Item = { key: string, value: string, kind: string } +export type Info = { start: number, stop: number, version: number, items: { Item } } + +local TO_TAG: { [string]: string } = { + year = "date", + ["album artist"] = "albumartist", + albumartist = "albumartist", + tracknumber = "track", + discnumber = "disc", + description = "comment", +} +local KINDS = { [0] = "text", "binary", "link", "text" } + +-- tagKey(itemKey) -> the Types.Tags key +function ApeTag.tagKey(key: string): string + local k = string.lower(key) + return TO_TAG[k] or k +end + +-- parse(buf, footer, start?) -> tags, info | nil: the tag whose 32-byte footer starts at byte `footer`; the items +-- must lie at or after `start` (default 0) +function ApeTag.parse(buf: buffer, footer: number, start: number?): ({ [string]: string }?, Info?) + local lo = start or 0 + local n = buffer.len(buf) + if footer < lo or footer + 32 > n or buffer.readstring(buf, footer, 8) ~= "APETAGEX" then + return nil, nil + end + local version = buffer.readu32(buf, footer + 8) + local size = buffer.readu32(buf, footer + 12) -- items + footer + local count = buffer.readu32(buf, footer + 16) + local flags = buffer.readu32(buf, footer + 20) + if size < 32 or footer + 32 - size < lo then + return nil, nil + end + local p = footer + 32 - size + local first = p + if version >= 2000 and bit32.band(flags, 0x80000000) ~= 0 and p - 32 >= lo and buffer.readstring(buf, p - 32, 8) == "APETAGEX" then + first = p - 32 + end + local stop = footer + local tags: { [string]: string } = {} + local items: { Item } = {} + for _ = 1, math.min(count, 65536) do + if p + 9 > stop then + break + end + local len = buffer.readu32(buf, p) + local iflags = buffer.readu32(buf, p + 4) + local k = p + 8 + while k < stop and buffer.readu8(buf, k) ~= 0 do + k += 1 + end + if k >= stop then + break + end + local key = buffer.readstring(buf, p + 8, k - p - 8) + local v0 = k + 1 + if len > stop - v0 then + break + end + local value = buffer.readstring(buf, v0, len) + local kind = if version >= 2000 then KINDS[bit32.band(bit32.rshift(iflags, 1), 3)] else "text" + table.insert(items, { key = key, value = value, kind = kind }) + if kind == "text" and key ~= "" then + local tk = ApeTag.tagKey(key) + local s = (string.gsub(value, "%z+$", "")) + s = (string.gsub(s, "%z", "; ")) + local old = tags[tk] + tags[tk] = if old then old .. "; " .. s else s + end + p = v0 + len + end + return tags, { start = first, stop = footer + 32, version = version, items = items } +end + +-- find(buf, start, limit) -> tags, info | nil: the tag at the end of [start, limit), or just before a 128-byte +-- ID3v1 tag ("TAG") there +function ApeTag.find(buf: buffer, start: number, limit: number): ({ [string]: string }?, Info?) + if limit > buffer.len(buf) then + limit = buffer.len(buf) + end + if limit - start >= 32 then + local tags, info = ApeTag.parse(buf, limit - 32, start) + if tags then + return tags, info + end + end + if limit - start >= 160 and buffer.readstring(buf, limit - 128, 3) == "TAG" then + return ApeTag.parse(buf, limit - 160, start) + end + return nil, nil +end + +return ApeTag diff --git a/tests/codecs/apetag.luau b/tests/codecs/apetag.luau new file mode 100644 index 0000000..5506acc --- /dev/null +++ b/tests/codecs/apetag.luau @@ -0,0 +1,79 @@ +-- codecs/audio/ApeTag tests: APEv2 tags with and without a header, APEv1, multi-value items, binary items, an ID3v1 +-- tag after the APE tag, a tag inside a sub-range, malformed sizes / counts; the fuzz no-throw pass. The real files +-- (wavpack -w, mac -t, ...) are covered by tests/audio/wavpack.luau and tests/audio/ape.luau. +local T = require("@tests/T") +local ApeTag = require("@formats/codecs/audio/ApeTag") + +local t = T.suite("apetag") + +local function u32(v: number): string + return string.char(v % 256, (v // 256) % 256, (v // 65536) % 256, (v // 16777216) % 256) +end +-- tag(items, version, header) -> the tag bytes; items = { { key, value, flags? } } +local function tag(items: { any }, version: number, header: boolean): string + local body = {} + for _, it in items do + table.insert(body, u32(#it[2]) .. u32(it[3] or 0) .. it[1] .. "\0" .. it[2]) + end + local b = table.concat(body) + local flags = if header then 0x80000000 else 0 + local function block(isHeader: boolean): string + return "APETAGEX" .. u32(version) .. u32(#b + 32) .. u32(#items) .. u32(flags + (if isHeader then 0x20000000 else 0)) .. string.rep("\0", 8) + end + return (if header then block(true) else "") .. b .. block(false) +end + +do + local s = "audio data" .. tag({ { "Title", "Hello" }, { "Artist", "A\0B" }, { "Year", "2026" }, { "Track", "3/9" }, + { "Cover Art (Front)", "f.png\0PNG", 2 }, { "Album Artist", "Band" } }, 2000, true) + local b = buffer.fromstring(s) + local tags, info = ApeTag.find(b, 0, #s) + t:check(tags and info, "v2 found") + t:eq(tags.title, "Hello", "title") + t:eq(tags.artist, "A; B", "multi value") + t:eq(tags.date, "2026", "year -> date") + t:eq(tags.track, "3/9", "track") + t:eq(tags.albumartist, "Band", "album artist") + t:eq(tags["cover art (front)"], nil, "binary item not a tag") + t:eq(info.start, 10, "start at the header") + t:eq(info.stop, #s, "stop") + t:eq(info.version, 2000, "version") + t:eq(#info.items, 6, "items") + t:eq(info.items[5].kind, "binary", "binary kind") + t:eq(info.items[5].value, "f.png\0PNG", "binary value") + -- ID3v1 after it + local s2 = s .. "TAG" .. string.rep("x", 125) + local tags2, info2 = ApeTag.find(buffer.fromstring(s2), 0, #s2) + t:check(tags2 and tags2.title == "Hello" and info2.stop == #s, "before ID3v1") + -- inside a sub-range + local s3 = "zz" .. s .. "yyy" + local tags3, info3 = ApeTag.find(buffer.fromstring(s3), 2, 2 + #s) + t:check(tags3 and tags3.title == "Hello" and info3.start == 12, "sub-range") + -- the start bounds the header + local tags4, info4 = ApeTag.find(b, 50, #s) + t:check(tags4 == nil and info4 == nil, "items before start") +end +do + local s = tag({ { "Title", "Old" } }, 1000, false) + local tags, info = ApeTag.find(buffer.fromstring(s), 0, #s) + t:check(tags and tags.title == "Old" and info.start == 0 and info.version == 1000, "v1") + t:eq(ApeTag.find(buffer.fromstring("no tag here at all, not even close to it"), 0, 40), nil, "none") +end +-- malformed: a huge item length, a huge count, a size larger than the file +do + local s = tag({ { "Title", "Hello" } }, 2000, false) + local bad = string.sub(s, 1, 0) .. u32(0x7FFFFFFF) .. string.sub(s, 5) + local tags = ApeTag.find(buffer.fromstring(bad), 0, #bad) + t:check(tags and tags.title == nil, "bad item length stops") + local big = string.sub(s, 1, #s - 32 + 12) .. u32(1e9) .. string.sub(s, #s - 32 + 17) + t:eq(ApeTag.find(buffer.fromstring(big), 0, #big), nil, "size past the start") +end + +local samples = { + buffer.fromstring("data" .. tag({ { "Title", "Hello" }, { "Artist", "X" } }, 2000, true)), + buffer.fromstring(tag({ { "A", "b" } }, 1000, false) .. "TAG" .. string.rep("\0", 125)), +} +t:fuzz("find", function(b: buffer) + return ApeTag.find(b, 0, buffer.len(b)) +end, samples, { mutations = 400 }) +t:done()