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:
1 parent
a289eba8af
commit
a25b7c0caa
18 files changed
+202
-410
No files matched your search
+139
-61
@@ -1,89 +1,167 @@
|
||||
--!strict
|
||||
--!native
|
||||
-- core/Base64: RFC 4648 base64 (standard alphabet, and the URL-safe one on decode). Tiled map data, data: URIs, ...
|
||||
-- decode skips whitespace and accepts missing padding; other bad characters fail. Never throws.
|
||||
-- core/Base64: RFC 4648 base64 (standard alphabet, and base64url with opts.url) encode / decode, and data: URIs.
|
||||
-- Used by tiled map data, glTF / SVG / CSS data: URIs, plist <data>, YAML !!binary, FBX, the Brotli static tables.
|
||||
-- decode skips whitespace, accepts missing padding and both alphabets; any other character, data after padding or a
|
||||
-- lone last character is an error. Never throws.
|
||||
--
|
||||
-- local s = Base64.encode(buf) -- buffer | string -> string
|
||||
-- local b, err = Base64.decode(s) -- string -> buffer | nil, "message"
|
||||
-- local s = Base64.encode(buf) -- buffer | string -> string ("" for empty input)
|
||||
-- local s2 = Base64.encode(buf, { url = true, pad = false, wrap = 76 }, offset, length)
|
||||
-- local b, err = Base64.decode(s) -- string | buffer -> buffer | nil, "message"
|
||||
-- local b2, mime = Base64.decodeDataUri("data:application/octet-stream;base64,AAEC") -- buffer, mime | nil, msg
|
||||
-- local uri = Base64.dataUri(buf, "image/png")
|
||||
local Base64 = {}
|
||||
|
||||
local ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
|
||||
local ENC = buffer.create(64)
|
||||
buffer.writestring(ENC, 0, ALPHABET)
|
||||
export type EncodeOptions = {
|
||||
url: boolean?, -- the URL-safe alphabet (- and _); default false
|
||||
pad: boolean?, -- "=" padding; default true
|
||||
wrap: number?, -- a "\n" after every `wrap` output characters (rounded down to a multiple of 4); default none
|
||||
}
|
||||
|
||||
local ALPHA = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
|
||||
local ALPHA_URL = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
|
||||
local ENC = buffer.fromstring(ALPHA)
|
||||
local ENC_URL = buffer.fromstring(ALPHA_URL)
|
||||
-- the decode table: 0..63, 64 = skip (whitespace), 65 = padding, 255 = bad
|
||||
local DEC = buffer.create(256)
|
||||
buffer.fill(DEC, 0, 255)
|
||||
for i = 0, 63 do
|
||||
buffer.writeu8(DEC, string.byte(ALPHABET, i + 1), i)
|
||||
for i = 1, 64 do
|
||||
buffer.writeu8(DEC, string.byte(ALPHA, i), i - 1)
|
||||
end
|
||||
buffer.writeu8(DEC, string.byte("-"), 62)
|
||||
buffer.writeu8(DEC, string.byte("_"), 63)
|
||||
for _, c in { 9, 10, 13, 32 } do
|
||||
buffer.writeu8(DEC, c, 254) -- whitespace: skipped
|
||||
for _, c in { 9, 10, 11, 12, 13, 32 } do
|
||||
buffer.writeu8(DEC, c, 64)
|
||||
end
|
||||
buffer.writeu8(DEC, string.byte("="), 65)
|
||||
|
||||
local readu8, writeu8 = buffer.readu8, buffer.writeu8
|
||||
|
||||
-- encode(data) -> base64 text with padding
|
||||
function Base64.encode(data: buffer | string): string
|
||||
local src: buffer = if type(data) == "string" then buffer.fromstring(data) else data
|
||||
local n = buffer.len(src)
|
||||
local out = buffer.create((n + 2) // 3 * 4)
|
||||
local o = 0
|
||||
local i = 0
|
||||
while i + 2 < n do
|
||||
local v = readu8(src, i) * 65536 + readu8(src, i + 1) * 256 + readu8(src, i + 2)
|
||||
writeu8(out, o, readu8(ENC, v // 262144))
|
||||
writeu8(out, o + 1, readu8(ENC, v // 4096 % 64))
|
||||
writeu8(out, o + 2, readu8(ENC, v // 64 % 64))
|
||||
writeu8(out, o + 3, readu8(ENC, v % 64))
|
||||
i += 3
|
||||
o += 4
|
||||
-- encode(data, opts?, offset?, length?) -> base64 text of data[offset, offset + length)
|
||||
function Base64.encode(data: buffer | string, opts: EncodeOptions?, offset: number?, length: number?): string
|
||||
local buf: buffer = if type(data) == "string" then buffer.fromstring(data) else data
|
||||
local total = buffer.len(buf)
|
||||
local o = math.clamp(offset or 0, 0, total)
|
||||
local n = math.clamp(length or (total - o), 0, total - o)
|
||||
local enc = if opts and opts.url then ENC_URL else ENC
|
||||
local pad = not (opts and opts.pad == false)
|
||||
local wrap = if opts and opts.wrap and opts.wrap >= 4 then opts.wrap // 4 else 0 -- groups per line
|
||||
local full = n // 3
|
||||
local rest = n - full * 3
|
||||
local textLen = full * 4 + (if rest == 0 then 0 elseif pad then 4 else rest + 1)
|
||||
local groups = full + (if rest > 0 then 1 else 0)
|
||||
local breaks = if wrap > 0 and groups > 0 then (groups - 1) // wrap else 0
|
||||
local out = buffer.create(textLen + breaks)
|
||||
local p = 0
|
||||
local col = 0
|
||||
for i = 0, full - 1 do
|
||||
if wrap > 0 then
|
||||
if col == wrap then
|
||||
writeu8(out, p, 10)
|
||||
p += 1
|
||||
col = 0
|
||||
end
|
||||
col += 1
|
||||
end
|
||||
local q = o + i * 3
|
||||
local v = readu8(buf, q) * 65536 + readu8(buf, q + 1) * 256 + readu8(buf, q + 2)
|
||||
writeu8(out, p, readu8(enc, v // 262144))
|
||||
writeu8(out, p + 1, readu8(enc, (v // 4096) % 64))
|
||||
writeu8(out, p + 2, readu8(enc, (v // 64) % 64))
|
||||
writeu8(out, p + 3, readu8(enc, v % 64))
|
||||
p += 4
|
||||
end
|
||||
local rest = n - i
|
||||
if rest > 0 then
|
||||
local v = readu8(src, i) * 65536 + (if rest == 2 then readu8(src, i + 1) * 256 else 0)
|
||||
writeu8(out, o, readu8(ENC, v // 262144))
|
||||
writeu8(out, o + 1, readu8(ENC, v // 4096 % 64))
|
||||
writeu8(out, o + 2, if rest == 2 then readu8(ENC, v // 64 % 64) else 61)
|
||||
writeu8(out, o + 3, 61)
|
||||
if wrap > 0 and col == wrap then
|
||||
writeu8(out, p, 10)
|
||||
p += 1
|
||||
end
|
||||
local q = o + full * 3
|
||||
local v = readu8(buf, q) * 65536 + (if rest == 2 then readu8(buf, q + 1) * 256 else 0)
|
||||
writeu8(out, p, readu8(enc, v // 262144))
|
||||
writeu8(out, p + 1, readu8(enc, (v // 4096) % 64))
|
||||
if rest == 2 then
|
||||
writeu8(out, p + 2, readu8(enc, (v // 64) % 64))
|
||||
elseif pad then
|
||||
writeu8(out, p + 2, 61)
|
||||
end
|
||||
if pad then
|
||||
writeu8(out, p + 3, 61)
|
||||
end
|
||||
end
|
||||
return buffer.tostring(out)
|
||||
end
|
||||
|
||||
-- decode(text) -> buffer | nil, message
|
||||
function Base64.decode(text: string | buffer): (buffer?, string?)
|
||||
local src: buffer = if type(text) == "string" then buffer.fromstring(text) else text
|
||||
-- decode(s) -> buffer | nil, message
|
||||
function Base64.decode(s: string | buffer): (buffer?, string?)
|
||||
local src: buffer = if type(s) == "string" then buffer.fromstring(s) else s
|
||||
local n = buffer.len(src)
|
||||
local out = buffer.create(n // 4 * 3 + 3)
|
||||
local o = 0
|
||||
local acc, bits = 0, 0
|
||||
local pad = false
|
||||
local out = buffer.create((n // 4 + 1) * 3)
|
||||
local acc, bits, p = 0, 0, 0
|
||||
local padded = false
|
||||
for i = 0, n - 1 do
|
||||
local c = readu8(src, i)
|
||||
local v = readu8(DEC, c)
|
||||
if v == 254 then
|
||||
continue
|
||||
end
|
||||
if c == 61 then -- '='
|
||||
pad = true
|
||||
continue
|
||||
end
|
||||
if v == 255 or pad then
|
||||
return nil, string.format("bad base64 character at %d", i)
|
||||
end
|
||||
acc = acc * 64 + v
|
||||
bits += 6
|
||||
if bits >= 8 then
|
||||
bits -= 8
|
||||
local p = 2 ^ bits
|
||||
writeu8(out, o, acc // p)
|
||||
acc %= p
|
||||
o += 1
|
||||
local d = readu8(DEC, readu8(src, i))
|
||||
if d < 64 then
|
||||
if padded then
|
||||
return nil, "data after padding"
|
||||
end
|
||||
acc = acc * 64 + d
|
||||
bits += 6
|
||||
if bits >= 8 then
|
||||
bits -= 8
|
||||
local scale = 2 ^ bits
|
||||
local byte = acc // scale
|
||||
writeu8(out, p, byte % 256)
|
||||
p += 1
|
||||
acc -= byte * scale
|
||||
end
|
||||
elseif d == 65 then
|
||||
padded = true
|
||||
elseif d ~= 64 then
|
||||
return nil, string.format("bad base64 character 0x%02x at %d", readu8(src, i), i)
|
||||
end
|
||||
end
|
||||
local res = buffer.create(o)
|
||||
buffer.copy(res, 0, out, 0, o)
|
||||
if bits >= 6 then
|
||||
return nil, "truncated base64 (a single character in the last group)"
|
||||
end
|
||||
local res = buffer.create(p)
|
||||
buffer.copy(res, 0, out, 0, p)
|
||||
return res, nil
|
||||
end
|
||||
|
||||
-- decodeDataUri(uri) -> buffer, mime | nil, message: "data:[<mime>][;params][;base64],<data>" (percent-encoded data
|
||||
-- when not base64)
|
||||
function Base64.decodeDataUri(uri: string): (buffer?, string?)
|
||||
if string.sub(uri, 1, 5) ~= "data:" then
|
||||
return nil, "not a data: URI"
|
||||
end
|
||||
local comma = string.find(uri, ",", 6, true)
|
||||
if not comma then
|
||||
return nil, "data: URI without a comma"
|
||||
end
|
||||
local head = string.sub(uri, 6, comma - 1)
|
||||
local isB64 = string.sub(head, -7) == ";base64"
|
||||
local mime = string.match(head, "^([^;]*)") or ""
|
||||
if mime == "" then
|
||||
mime = "text/plain"
|
||||
end
|
||||
local body = string.sub(uri, comma + 1)
|
||||
if isB64 then
|
||||
local b, err = Base64.decode(body)
|
||||
if not b then
|
||||
return nil, err
|
||||
end
|
||||
return b, mime
|
||||
end
|
||||
local decoded = string.gsub(body, "%%(%x%x)", function(h: string): string
|
||||
return string.char(tonumber(h, 16) :: number)
|
||||
end)
|
||||
return buffer.fromstring(decoded), mime
|
||||
end
|
||||
|
||||
-- dataUri(buf, mime?) -> "data:<mime>;base64,..."
|
||||
function Base64.dataUri(buf: buffer | string, mime: string?): string
|
||||
return "data:" .. (mime or "application/octet-stream") .. ";base64," .. Base64.encode(buf)
|
||||
end
|
||||
|
||||
return Base64
|
||||
Reference in new issue
Block a user