From 8e527d670221e4da72bb2061a3c9a259f7fe9d65 Mon Sep 17 00:00:00 2001 From: 4DBug Date: Sat, 31 Oct 2026 08:57:35 -0500 Subject: [PATCH] absorb: playlist/xspf (new: XSPF 1 read / write on codecs/Xml, xml:base, every track field); tests vs ElementTree-written files --- docs/FORMATS.md | 1 + licenses/NOTICE.md | 2 +- src/Formats/Registry.luau | 12 ++ src/Formats/playlist/xspf/init.luau | 214 ++++++++++++++++++++++++++++ tests/playlist/xspf.luau | 89 ++++++++++++ 5 files changed, 317 insertions(+), 1 deletion(-) create mode 100644 src/Formats/playlist/xspf/init.luau create mode 100644 tests/playlist/xspf.luau diff --git a/docs/FORMATS.md b/docs/FORMATS.md index 575f0c3..65b8ad4 100644 --- a/docs/FORMATS.md +++ b/docs/FORMATS.md @@ -14,6 +14,7 @@ is tested under both runtimes (luau `--codegen -O2` and LuaJIT through the build | archive | zip | .zip .wsz | yes | yes | methods: "store", "deflate", zip64: true | `tests/archive/zip.luau` | | playlist | m3u | .m3u .m3u8 | yes | yes | hls: true | `tests/playlist/m3u.luau` | | playlist | pls | .pls | yes | yes | | `tests/playlist/pls.luau` | +| playlist | xspf | .xspf | yes | yes | | `tests/playlist/xspf.luau` | Codecs (layer 1, shared by formats; not loadable on their own): diff --git a/licenses/NOTICE.md b/licenses/NOTICE.md index 28d7172..c4f7b74 100644 --- a/licenses/NOTICE.md +++ b/licenses/NOTICE.md @@ -13,7 +13,7 @@ | `src/Formats/image/bmp/` | bmp-luau b86aed6 (same author), written from the Microsoft BMP documentation | no licence chosen yet; no third-party code | | `tests/fixtures/bmpsuite/` | BMP Suite 2.8 by Jason Summers (generated images) | public domain: [bmpsuite-NOTICE.txt](bmpsuite-NOTICE.txt) | | `src/Formats/containers/Zip/` (the reader), `src/Formats/archive/zip/` | zip-luau 121d4b0 (same author), written from PKWARE's APPNOTE.TXT 6.3 | no licence chosen yet; no third-party code | -| `src/Formats/playlist/` (m3u, pls), `src/Formats/codecs/Url/`, `src/Formats/codecs/Text/` | playlist-luau 3fc7fd3 (same author), written from RFC 3986 / RFC 8216 | no licence chosen yet; no third-party code | +| `src/Formats/playlist/` (m3u, pls; xspf is new), `src/Formats/codecs/Url/`, `src/Formats/codecs/Text/` | playlist-luau 3fc7fd3 (same author), written from RFC 3986 / RFC 8216 | no licence chosen yet; no third-party code | | `tests/fixtures/pngsuite/` | PngSuite by Willem van Schaik | [PngSuite-LICENSE.txt](PngSuite-LICENSE.txt) | Ports of third-party code (minimp3, stb_vorbis, libopus, ...) add a row here and their upstream licence text in this diff --git a/src/Formats/Registry.luau b/src/Formats/Registry.luau index 8adbe99..6b27b87 100644 --- a/src/Formats/Registry.luau +++ b/src/Formats/Registry.luau @@ -122,6 +122,18 @@ local list: { Entry } = { return require("@formats/playlist/pls") end, }, + { + name = "xspf", + category = "playlist", + path = "playlist/xspf", + extensions = { "xspf" }, + mime = { "application/xspf+xml" }, + deps = { "core/Err", "core/Bytes", "Types", "codecs/Url", "codecs/Xml" }, + caps = { decode = true, encode = true }, + load = function() + return require("@formats/playlist/xspf") + end, + }, } return list diff --git a/src/Formats/playlist/xspf/init.luau b/src/Formats/playlist/xspf/init.luau new file mode 100644 index 0000000..b18034c --- /dev/null +++ b/src/Formats/playlist/xspf/init.luau @@ -0,0 +1,214 @@ +--!strict +-- playlist/xspf: XSPF 1 ("spiff", https://xspf.org/spec) playlists on codecs/Xml. New in formats-luau (playlist-luau +-- had no XSPF). Decode: the playlist's title / creator / annotation / info / location / identifier / image / date, +-- every track's location (the first one is the entry url, all of them in entry.locations), title, creator, album, +-- annotation, info, image, trackNum and duration (milliseconds -> seconds), xml:base on the playlist and on tracks; +-- locations resolve against xml:base, then opts.baseUrl (RFC 3986, codecs/Url), and are classified like the other +-- playlist formats (local / relative / non-HTTP locations are unplayable). Encode: XSPF version 1 with those fields. +-- Not supported: extension / link / meta elements (skipped), XSPF version 0 differences. +-- +-- local pl = Xspf.decode(Bytes.reader(buf), { baseUrl = "https://example.com/list.xspf" }) +-- for _, e in pl.entries do print(e.url, e.title, e.creator, e.duration) end +-- local out = Xspf.encode(pl) + +-- ------------------------------------------------------------------------------------------------ requires +local Err = require("@formats/core/Err") +local Bytes = require("@formats/core/Bytes") +local Types = require("@formats/Types") +local Url = require("@formats/codecs/Url") +local Xml = require("@formats/codecs/Xml") + +-- ------------------------------------------------------------------------------------------------ contract +local Xspf = { + name = "xspf", + category = "playlist", + extensions = { "xspf" }, + mime = { "application/xspf+xml" }, + deps = { "core/Err", "core/Bytes", "Types", "codecs/Url", "codecs/Xml" }, + caps = { decode = true, encode = true }, +} + +-- ------------------------------------------------------------------------------------------------ types +export type DecodeOptions = { + baseUrl: string?, -- the playlist's own URL (or Windows path), for relative locations + maxBytes: number?, -- refuse bigger files (default 64 MB) +} + +-- ------------------------------------------------------------------------------------------------ constants +local NS = "http://xspf.org/ns/0/" +local PLAYLIST_FIELDS = { "title", "creator", "annotation", "info", "location", "identifier", "image", "date", "license" } +local TRACK_FIELDS = { "title", "creator", "album", "annotation", "info", "image" } + +local function trim(s: string): string + return (string.match(s, "^%s*(.-)%s*$") :: string) +end + +-- the text of the first child element with this local name, trimmed (nil when missing or empty) +local function field(node: any, name: string): string? + for _, c in node.children do + if type(c) == "table" and Xml.localName(c.name) == name then + local s = trim(Xml.text(c)) + return if s ~= "" then s else nil + end + end + return nil +end + +local function elements(node: any, name: string): { any } + local out = {} + for _, c in node.children do + if type(c) == "table" and Xml.localName(c.name) == name then + table.insert(out, c) + end + end + return out +end + +-- ------------------------------------------------------------------------------------------------ sniff +function Xspf.sniff(r: Bytes.Reader): number + local s = r:peek(1024) + local i = string.find(s, "<%w*:?playlist[%s>]") + if i and string.find(s, NS, 1, true) then + return 0.95 + end + return 0 +end + +-- ------------------------------------------------------------------------------------------------ decode +-- decode(r, opts?) -> Types.Playlist | nil, Err (malformed XML or a root that is not is an error; odd +-- tracks become warnings) +function Xspf.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Playlist?, Err.Err?) + local n = r.limit - r.pos + if n > (opts and opts.maxBytes or 2 ^ 26) then + return nil, Err.new(Err.LIMIT, "playlist over the size limit") + end + local doc, err = Xml.decode(r:string(n)) + if not doc then + return nil, err + end + local root = (doc :: any).root + if not root or Xml.localName(root.name) ~= "playlist" then + return nil, Err.new(Err.CORRUPT, "the root element is not ") + end + local warnings: { string } = {} + local base, baseScheme = Url.base(opts and opts.baseUrl, warnings) + local function withXmlBase(node: any, b: string?, s: string?): (string?, string?) + local xb = node.attrs and node.attrs["xml:base"] + if type(xb) ~= "string" or xb == "" then + return b, s + end + local l = Url.locate(xb, b, s) + if Url.scheme(l.url) then + return l.url, Url.scheme(l.url) + end + return b, s + end + local pbase, pscheme = withXmlBase(root, base, baseScheme) + local pl: any = { + kind = "playlist", + format = "xspf", + entries = {}, + warnings = warnings, + title = field(root, "title"), + meta = { source = { format = "xspf", version = root.attrs and root.attrs.version } }, + } + for _, k in PLAYLIST_FIELDS do + if k ~= "title" then + pl.meta[k] = field(root, k) + end + end + local nLocal = 0 + for _, list in elements(root, "trackList") do + for i, track in elements(list, "track") do + local tbase, tscheme = withXmlBase(track, pbase, pscheme) + local locs = {} + for _, l in elements(track, "location") do + local s = trim(Xml.text(l)) + if s ~= "" then + table.insert(locs, s) + end + end + if #locs == 0 then + table.insert(warnings, string.format("track %d: no location", i)) + else + local e: any = Url.locate(locs[1], tbase, tscheme) + for _, k in TRACK_FIELDS do + e[k] = field(track, k) + end + local d = tonumber(field(track, "duration")) + e.duration = if d and d >= 0 and d == d and d ~= math.huge then d / 1000 else nil + e.trackNum = tonumber(field(track, "trackNum")) + if #locs > 1 then + local all = {} + for _, l in locs do + table.insert(all, Url.locate(l, tbase, tscheme).url) + end + e.locations = all + end + if e.reason == "local path" then + nLocal += 1 + elseif e.reason then + table.insert(warnings, string.format("track %d: %s: %s", i, e.reason, e.url)) + end + table.insert(pl.entries, e) + end + end + end + if nLocal > 0 then + table.insert(warnings, string.format("%d local path%s (not playable)", nLocal, if nLocal == 1 then "" else "s")) + end + return pl, nil +end + +-- ------------------------------------------------------------------------------------------------ encode +-- encode(playlist, opts?) -> buffer | nil, Err: XSPF version 1. playlist: a Types.Playlist (any format's) or a list +-- of entries ({ url, title?, duration?, creator?, album?, ... }). opts.indent (default 2) pretty-prints. +function Xspf.encode(value: any, opts: { indent: number?, title: string? }?): (buffer?, Err.Err?) + if type(value) ~= "table" then + return nil, Err.new(Err.ARG, "encode: needs a Playlist or a list of entries") + end + local entries = if type(value.entries) == "table" then value.entries else value + local root = Xml.element("playlist", { version = "1", xmlns = NS }) + local function add(parent: any, name: string, v: any) + if type(v) == "string" and v ~= "" then + Xml.append(parent, Xml.element(name, nil, { v })) + elseif type(v) == "number" and v == v and v ~= math.huge and v ~= -math.huge then + Xml.append(parent, Xml.element(name, nil, { tostring(math.floor(v + 0.5)) })) + end + end + local title = if opts and opts.title then opts.title else value.title + add(root, "title", title) + local meta = if type(value.meta) == "table" then value.meta else {} + for _, k in PLAYLIST_FIELDS do + if k ~= "title" then + add(root, k, meta[k]) + end + end + local list = Xml.append(root, Xml.element("trackList")) + for _, e in entries do + if type(e) == "table" and type(e.url) == "string" then + local tr = Xml.append(list, Xml.element("track")) + add(tr, "location", e.url) + if type(e.locations) == "table" then + for i = 2, #e.locations do + add(tr, "location", e.locations[i]) + end + end + for _, k in TRACK_FIELDS do + add(tr, k, e[k]) + end + local d = tonumber(e.duration) + if d and d >= 0 then + add(tr, "duration", d * 1000) + end + add(tr, "trackNum", tonumber(e.trackNum)) + end + end + local s = Xml.encode(Xml.document(root), { indent = if opts and opts.indent ~= nil then opts.indent else 2 }) + if type(s) ~= "string" then + return nil, Err.new(Err.ARG, "encode: the XML serializer failed") + end + return buffer.fromstring(s), nil +end + +return Xspf diff --git a/tests/playlist/xspf.luau b/tests/playlist/xspf.luau new file mode 100644 index 0000000..8cb2aef --- /dev/null +++ b/tests/playlist/xspf.luau @@ -0,0 +1,89 @@ +-- playlist/xspf tests: XSPF files written by Python's xml.etree.ElementTree (tools/fixtures/playlist.py) decode to +-- their entries; every track field, multiple locations, xml:base, relative / local locations, a prefixed namespace; +-- the writer round-trips (and escapes; its output was also checked by hand with ElementTree); errors (malformed XML, +-- another root); the api; the fuzz pass. +-- Fixtures: tools/fixtures/playlist.py. +local T = require("@tests/T") +local Xspf = require("@formats/playlist/xspf") +local Bytes = require("@formats/core/Bytes") +local Err = require("@formats/core/Err") +local F = require("@formats") +local Cases = require("@tests/gen/playlist_cases") + +local t = T.suite("xspf") +local function decode(s: string, base: string?): (any, any) + return Xspf.decode(Bytes.reader(buffer.fromstring(s)), { baseUrl = base }) +end + +for _, c in Cases do + local pl, err = decode(c.xspf) + t:check(pl, c.name .. ": decode: " .. tostring(err)) + t:eq(pl.title, c.title, c.name .. ": title") + t:eq(#pl.entries, #c.entries, c.name .. ": entries") + for i, e in c.entries do + local g = pl.entries[i] + t:check(g.url == e.url and g.title == e.title and g.duration == e.duration and g.playable, c.name .. ": entry " .. i) + end + local back = decode(buffer.tostring(Xspf.encode(pl) :: buffer)) + t:eq(back.title, pl.title, c.name .. ": round trip title") + for i, e in pl.entries do + local g = back.entries[i] + t:check(g.url == e.url and g.title == e.title and g.duration == e.duration, c.name .. ": round trip " .. i) + end +end +t:note("%d ElementTree-written XSPF files decode to their entries and round-trip", #Cases) + +do + local text = [[ + + My & list Me + + a%20b.mp3http://mirror.example/a.mp3 + AArtistAlbum1500 + 3cover.jpg + ../b.mp3 + no location + C:\Music\c.mp3 + +]] + local pl = decode(text, "http://ignored.example/") + t:eq(pl.title, "My & list", "title (entity, trimmed)") + t:eq(pl.meta.creator, "Me", "playlist creator") + t:eq(#pl.entries, 3, "a track without a location is skipped") + local a = pl.entries[1] + t:eq(a.url, "http://media.example/base/a%20b.mp3", "xml:base on the playlist") + t:check(a.title == "A" and a.creator == "Artist" and a.album == "Album" and a.duration == 1.5 and a.trackNum == 3 and a.image == "cover.jpg", "track fields") + t:check(a.locations and #a.locations == 2 and a.locations[2] == "http://mirror.example/a.mp3", "multiple locations") + t:eq(pl.entries[2].url, "http://other.example/b.mp3", "xml:base on a track") + t:check(not pl.entries[3].playable and pl.entries[3].reason == "local path", "local path") + t:check(#pl.warnings >= 2, "warnings for the missing location and the local path") + -- writer: escaping, extra fields, a plain entry list + local out = buffer.tostring(Xspf.encode({ { url = "http://x/a?b=1&c=2", title = "", duration = 2.5, creator = "C" } } :: any) :: buffer) + t:check(string.find(out, "http://x/a?b=1&c=2", 1, true) ~= nil and string.find(out, "<A & B>", 1, true) ~= nil, "escaping") + local back = decode(out) + t:check(back.entries[1].url == "http://x/a?b=1&c=2" and back.entries[1].title == "" and back.entries[1].duration == 2.5 and back.entries[1].creator == "C", "writer round trip") + -- errors + local _, e1 = decode("") + t:check(e1 and Err.is(e1), "malformed XML: " .. tostring(e1)) + local _, e2 = decode("") + t:check(e2 and e2.code == Err.CORRUPT, "another root") + t:check(select(2, Xspf.encode(5 :: any)) ~= nil, "encode a number") + t:eq(Xspf.sniff(Bytes.reader(buffer.fromstring(Cases[1].xspf))), 0.95, "sniff") + t:eq(Xspf.sniff(Bytes.reader(buffer.fromstring(""))), 0, "sniff without the namespace") + -- the api + t:eq(F.detect(Cases[1].xspf), "xspf", "F.detect") + local la = F.playlist.load(Cases[1].xspf) + t:check(la and la.format == "xspf" and #la.entries == #Cases[1].entries, "F.playlist.load") + local m = F.convert(Cases[1].m3u, "xspf") + t:check(m and #decode(buffer.tostring(m)).entries == #Cases[1].entries, "F.convert m3u -> xspf") +end + +local samples = {} +for _, c in Cases do + table.insert(samples, c.xspf) +end +t:fuzz("decode", function(b: buffer) + return Xspf.decode(Bytes.reader(b), { baseUrl = "http://h/a/" }) +end, samples, { mutations = 20 }) + +t:done()