ws-cleanup: one base64 (core/Base64)

core/Base64 takes over codecs/Base64's API (encode(buf | string, { url, pad, wrap }, offset, length), decode of both
alphabets, decodeDataUri, dataUri) plus line wrapping (opts.wrap) for plist; codecs/Base64 is removed and glTF /
DAE require core/Base64. The private copies in data/plist (Base64.luau), data/yaml (Parse / Emit) and codecs/Brotli
(the static tables) are gone. tests/codecs/base64 merged into tests/core/base64.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
4DBugandClaude Opus 5.5 committed 2026-10-31 10:50:40 -05:00
1 parent a289eba8af
commit a25b7c0caa
18 files changed
+202 -410

No files matched your search

-46
View File
@@ -1,46 +0,0 @@
-- codecs/Base64 tests: encode / decode against Python's base64 (standard, URL-safe, unpadded) for every length 0..70
-- and some large inputs, whitespace and missing padding on decode, data: URIs, bad input, and the fuzz no-throw pass.
-- Fixtures: tools/fixtures/base64.py.
local T = require("@tests/T")
local Base64 = require("@formats/codecs/Base64")
local Cases = require("@tests/gen/base64_cases")
local t = T.suite("base64")
for _, c in Cases do
local b = buffer.fromstring(c.data)
t:eq(Base64.encode(b), c.std, "encode " .. #c.data)
t:eq(Base64.encode(b, { url = true }), c.url, "encode url " .. #c.data)
t:eq(Base64.encode(b, { pad = false }), c.nopad, "encode unpadded " .. #c.data)
for _, s in { c.std, c.url, c.nopad } do
local d, err = Base64.decode(s)
t:check(d and buffer.tostring(d) == c.data, "decode " .. #c.data .. ": " .. tostring(err))
end
-- with line breaks every 76 characters (MIME)
local wrapped = string.gsub(c.std, "(" .. string.rep(".", 76) .. ")", "%1\r\n")
local d = Base64.decode(wrapped)
t:check(d and buffer.tostring(d) == c.data, "decode wrapped " .. #c.data)
end
-- a sub-range
t:eq(Base64.encode(buffer.fromstring("xxhelloyy"), nil, 2, 5), "aGVsbG8=", "encode a range")
-- data URIs
local b, mime = Base64.decodeDataUri("data:application/gltf-buffer;base64,AAEC")
t:check(b and buffer.len(b) == 3 and buffer.readu8(b, 2) == 2 and mime == "application/gltf-buffer", "data URI")
local b2, m2 = Base64.decodeDataUri("data:,a%20b")
t:check(b2 and buffer.tostring(b2) == "a b" and m2 == "text/plain", "percent-encoded data URI")
t:eq(Base64.dataUri(buffer.fromstring("hi"), "text/plain"), "data:text/plain;base64,aGk=", "dataUri")
-- bad input
t:check(Base64.decode("ab$d") == nil, "bad character")
t:check(Base64.decode("abcde") == nil, "a lone last character")
t:check(Base64.decode("ab==cd") == nil, "data after padding")
t:check(Base64.decodeDataUri("http://x") == nil, "not a data URI")
local samples = {}
for i, c in Cases do
if i % 10 == 0 then
table.insert(samples, c.std)
end
end
t:fuzz("decode", function(x: buffer)
return Base64.decode(x)
end, samples)
t:done()
+45 -2
View File
@@ -1,9 +1,26 @@
-- core/Base64 tests: the RFC 4648 vectors, whitespace / missing padding / URL-safe input, bad characters, random
-- round trips.
-- core/Base64 tests: the RFC 4648 vectors, encode / decode against Python's base64 (standard, URL-safe, unpadded) for every length 0..70
-- and some large inputs, whitespace and missing padding on decode, data: URIs, bad input, and the fuzz no-throw pass.
-- Fixtures: tools/fixtures/base64.py.
local T = require("@tests/T")
local Base64 = require("@formats/core/Base64")
local Cases = require("@tests/gen/base64_cases")
local t = T.suite("base64")
for _, c in Cases do
local b = buffer.fromstring(c.data)
t:eq(Base64.encode(b), c.std, "encode " .. #c.data)
t:eq(Base64.encode(b, { url = true }), c.url, "encode url " .. #c.data)
t:eq(Base64.encode(b, { pad = false }), c.nopad, "encode unpadded " .. #c.data)
for _, s in { c.std, c.url, c.nopad } do
local d, err = Base64.decode(s)
t:check(d and buffer.tostring(d) == c.data, "decode " .. #c.data .. ": " .. tostring(err))
end
-- with line breaks every 76 characters (MIME)
local wrapped = string.gsub(c.std, "(" .. string.rep(".", 76) .. ")", "%1\r\n")
local d = Base64.decode(wrapped)
t:check(d and buffer.tostring(d) == c.data, "decode wrapped " .. #c.data)
end
for _, v in { { "", "" }, { "f", "Zg==" }, { "fo", "Zm8=" }, { "foo", "Zm9v" }, { "foob", "Zm9vYg==" }, { "fooba", "Zm9vYmE=" }, { "foobar", "Zm9vYmFy" } } do
t:eq(Base64.encode(v[1]), v[2], "encode " .. v[1])
t:eq(buffer.tostring(Base64.decode(v[2]) :: buffer), v[1], "decode " .. v[2])
@@ -22,4 +39,30 @@ for n = 0, 40 do
end
t:eq(buffer.tostring(Base64.decode(Base64.encode(s)) :: buffer), buffer.tostring(s), "round trip " .. n)
end
-- line wrapping (plist / MIME)
t:eq(Base64.encode("foobarfoobarfo", { wrap = 8 }), "Zm9vYmFy\nZm9vYmFy\nZm8=", "wrap 8")
t:eq(Base64.encode("foobarfoobar", { wrap = 8 }), "Zm9vYmFy\nZm9vYmFy", "wrap at the end")
t:eq(Base64.encode("foobarfoobar", { wrap = 9 }), "Zm9vYmFy\nZm9vYmFy", "wrap rounds to groups")
-- a sub-range
t:eq(Base64.encode(buffer.fromstring("xxhelloyy"), nil, 2, 5), "aGVsbG8=", "encode a range")
-- data URIs
local b, mime = Base64.decodeDataUri("data:application/gltf-buffer;base64,AAEC")
t:check(b and buffer.len(b) == 3 and buffer.readu8(b, 2) == 2 and mime == "application/gltf-buffer", "data URI")
local b2, m2 = Base64.decodeDataUri("data:,a%20b")
t:check(b2 and buffer.tostring(b2) == "a b" and m2 == "text/plain", "percent-encoded data URI")
t:eq(Base64.dataUri(buffer.fromstring("hi"), "text/plain"), "data:text/plain;base64,aGk=", "dataUri")
-- bad input
t:check(Base64.decode("ab$d") == nil, "bad character")
t:check(Base64.decode("abcde") == nil, "a lone last character")
t:check(Base64.decode("ab==cd") == nil, "data after padding")
t:check(Base64.decodeDataUri("http://x") == nil, "not a data URI")
local samples = {}
for i, c in Cases do
if i % 10 == 0 then
table.insert(samples, c.std)
end
end
t:fuzz("decode", function(x: buffer)
return Base64.decode(x)
end, samples)
t:done()