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
@@ -1,140 +0,0 @@
|
||||
--!strict
|
||||
--!native
|
||||
-- codecs/Base64: RFC 4648 base64 (standard alphabet, and base64url with opts.url) encode / decode, for data: URIs
|
||||
-- (glTF, SVG, CSS), PEM-like text, YAML !!binary, ... New code. decode skips whitespace, accepts missing padding and
|
||||
-- both alphabets; any other character is an error. Never throws.
|
||||
--
|
||||
-- local Base64 = require("@formats/codecs/Base64")
|
||||
-- local s = Base64.encode(buf) -- string ("" for an empty buffer)
|
||||
-- local b, err = Base64.decode(s) -- buffer | nil, "message"
|
||||
-- local b2 = Base64.decodeDataUri("data:application/octet-stream;base64,AAEC") -- buffer, mime | nil, message
|
||||
local Base64 = {}
|
||||
|
||||
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 = 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, 64)
|
||||
end
|
||||
buffer.writeu8(DEC, string.byte("="), 65)
|
||||
|
||||
local readu8, writeu8 = buffer.readu8, buffer.writeu8
|
||||
|
||||
-- encode(buf, opts?, offset?, length?) -> string. opts.url: the URL-safe alphabet; opts.pad = false: no "=" padding.
|
||||
function Base64.encode(buf: buffer, opts: { url: boolean?, pad: boolean? }?, offset: number?, length: number?): string
|
||||
local o = math.clamp(offset or 0, 0, buffer.len(buf))
|
||||
local n = math.clamp(length or (buffer.len(buf) - o), 0, buffer.len(buf) - o)
|
||||
local enc = if opts and opts.url then ENC_URL else ENC
|
||||
local pad = not (opts and opts.pad == false)
|
||||
local full = n // 3
|
||||
local rest = n - full * 3
|
||||
local outLen = full * 4 + (if rest == 0 then 0 elseif pad then 4 else rest + 1)
|
||||
local out = buffer.create(outLen)
|
||||
local p = 0
|
||||
for i = 0, full - 1 do
|
||||
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
|
||||
if rest > 0 then
|
||||
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(s) -> buffer | nil, message
|
||||
function Base64.decode(s: string | buffer): (buffer?, string?)
|
||||
local src = if type(s) == "string" then buffer.fromstring(s) else s
|
||||
local n = buffer.len(src)
|
||||
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 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 byte = acc // (2 ^ bits)
|
||||
writeu8(out, p, byte % 256)
|
||||
p += 1
|
||||
acc = acc % (2 ^ bits)
|
||||
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
|
||||
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, mime: string?): string
|
||||
return "data:" .. (mime or "application/octet-stream") .. ";base64," .. Base64.encode(buf)
|
||||
end
|
||||
|
||||
return Base64
|
||||
@@ -13,6 +13,7 @@
|
||||
-- Never throws: bad data and bad arguments return nil, Err (TRUNCATED / CORRUPT / LIMIT / UNSUPPORTED / ARG).
|
||||
local Err = require("@formats/core/Err")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Base64 = require("@formats/core/Base64")
|
||||
local Deflate = require("@formats/codecs/Deflate")
|
||||
local Decode = require("@formats/codecs/Brotli/Decode")
|
||||
local StaticData = require("@formats/codecs/Brotli/Static")
|
||||
@@ -25,37 +26,9 @@ export type Options = {
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ static data
|
||||
local B64 = {}
|
||||
do
|
||||
local alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
|
||||
for i = 1, 64 do
|
||||
B64[string.byte(alphabet, i)] = i - 1
|
||||
end
|
||||
end
|
||||
|
||||
-- the tables are base64 text (trusted, generated by tools/brotli_static.py)
|
||||
local function unbase64(s: string): buffer
|
||||
local n = #s
|
||||
local pad = 0
|
||||
if string.sub(s, -2) == "==" then
|
||||
pad = 2
|
||||
elseif string.sub(s, -1) == "=" then
|
||||
pad = 1
|
||||
end
|
||||
local out = buffer.create(n // 4 * 3 - pad)
|
||||
local o = 0
|
||||
for i = 1, n, 4 do
|
||||
local a, b, c, d = string.byte(s, i, i + 3)
|
||||
local v = B64[a] * 262144 + B64[b] * 4096 + (B64[c] or 0) * 64 + (B64[d] or 0)
|
||||
buffer.writeu8(out, o, v // 65536)
|
||||
if o + 1 < buffer.len(out) then
|
||||
buffer.writeu8(out, o + 1, (v // 256) % 256)
|
||||
end
|
||||
if o + 2 < buffer.len(out) then
|
||||
buffer.writeu8(out, o + 2, v % 256)
|
||||
end
|
||||
o += 3
|
||||
end
|
||||
return out
|
||||
return Base64.decode(s) or buffer.create(0)
|
||||
end
|
||||
|
||||
local static: Decode.Static? = nil
|
||||
|
||||
File renamed without changes.
File renamed without changes.
+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
|
||||
@@ -1,71 +0,0 @@
|
||||
--!strict
|
||||
--!native
|
||||
-- data/plist/Base64: RFC 4648 base64 for plist <data> (decode skips whitespace; encode wraps lines). Never throws.
|
||||
local Base64 = {}
|
||||
|
||||
local ALPHA = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
|
||||
local DEC: { [number]: number } = {}
|
||||
for i = 1, 64 do
|
||||
DEC[string.byte(ALPHA, i)] = i - 1
|
||||
end
|
||||
local ENC: { string } = {}
|
||||
for i = 1, 64 do
|
||||
ENC[i - 1] = string.sub(ALPHA, i, i)
|
||||
end
|
||||
|
||||
-- decode(s) -> buffer | nil, message (whitespace ignored; padding optional)
|
||||
function Base64.decode(s: string): (buffer?, string?)
|
||||
local clean = string.gsub(s, "[ \t\r\n]", "")
|
||||
clean = string.gsub(clean, "=+$", "")
|
||||
local n = #clean
|
||||
if n % 4 == 1 then
|
||||
return nil, "bad base64 length"
|
||||
end
|
||||
local out = buffer.create(n * 3 // 4)
|
||||
local o = 0
|
||||
local acc, bits = 0, 0
|
||||
for i = 1, n do
|
||||
local v = DEC[string.byte(clean, i)]
|
||||
if v == nil then
|
||||
return nil, "bad base64 character"
|
||||
end
|
||||
acc = acc * 64 + v
|
||||
bits += 6
|
||||
if bits >= 8 then
|
||||
bits -= 8
|
||||
local b = acc // (2 ^ bits)
|
||||
buffer.writeu8(out, o, b)
|
||||
o += 1
|
||||
acc -= b * 2 ^ bits
|
||||
end
|
||||
end
|
||||
return out, nil
|
||||
end
|
||||
|
||||
-- encode(b, lineBytes?) -> base64 text; lineBytes > 0 puts "\n" after every lineBytes input bytes (and at the end)
|
||||
function Base64.encode(b: buffer, lineBytes: number?): string
|
||||
local n = buffer.len(b)
|
||||
local parts = table.create(n // 3 + 2)
|
||||
local per = lineBytes or 0
|
||||
local k = 0
|
||||
local i = 0
|
||||
while i < n do
|
||||
local a = buffer.readu8(b, i)
|
||||
local c1 = if i + 1 < n then buffer.readu8(b, i + 1) else 0
|
||||
local c2 = if i + 2 < n then buffer.readu8(b, i + 2) else 0
|
||||
local v = a * 65536 + c1 * 256 + c2
|
||||
local q = ENC[v // 262144] .. ENC[(v // 4096) % 64]
|
||||
q ..= if i + 1 < n then ENC[(v // 64) % 64] else "="
|
||||
q ..= if i + 2 < n then ENC[v % 64] else "="
|
||||
k += 1
|
||||
parts[k] = q
|
||||
i += 3
|
||||
if per > 0 and (i % per == 0 or i >= n) then
|
||||
k += 1
|
||||
parts[k] = "\n"
|
||||
end
|
||||
end
|
||||
return table.concat(parts)
|
||||
end
|
||||
|
||||
return Base64
|
||||
@@ -11,7 +11,7 @@ local Bytes = require("@formats/core/Bytes")
|
||||
local Types = require("@formats/Types")
|
||||
local Xml = require("@formats/codecs/Xml")
|
||||
local Json = require("@formats/codecs/Json")
|
||||
local Base64 = require("@formats/data/plist/Base64")
|
||||
local Base64 = require("@formats/core/Base64")
|
||||
local Binary = require("@formats/data/plist/Binary")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
@@ -20,7 +20,7 @@ local Plist = {
|
||||
category = "data",
|
||||
extensions = { "plist" },
|
||||
mime = { "application/x-plist", "application/x-bplist" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/Xml", "codecs/Json" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "core/Base64", "codecs/Xml", "codecs/Json" },
|
||||
caps = { decode = true, encode = true, binary = "read", ordered = true },
|
||||
}
|
||||
|
||||
@@ -217,7 +217,7 @@ function Plist.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
elseif tv == "buffer" then
|
||||
line(level, "<data>")
|
||||
local maxLine = math.max(16, 76 - 8 * level)
|
||||
for l in string.gmatch(Base64.encode(v, maxLine // 4 * 3), "[^\n]+") do
|
||||
for l in string.gmatch(Base64.encode(v, { wrap = maxLine // 4 * 4 }), "[^\n]+") do
|
||||
line(level, l)
|
||||
end
|
||||
line(level, "</data>")
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
-- !!binary; dates as their RFC 3339 text; tables referenced twice get an anchor and aliases. Never throws: bad
|
||||
-- values (cycles, functions, collection keys) return nil, message.
|
||||
local Types = require("@formats/Types")
|
||||
local Base64 = require("@formats/core/Base64")
|
||||
local Parse = require("@formats/data/yaml/Parse")
|
||||
|
||||
local Emit = {}
|
||||
@@ -119,22 +120,6 @@ local function str(s: string, ind: string, key: boolean): string
|
||||
return dquote(s)
|
||||
end
|
||||
|
||||
local function b64(b: buffer): string
|
||||
local A = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
|
||||
local out = {}
|
||||
local n = buffer.len(b)
|
||||
for i = 0, n - 1, 3 do
|
||||
local a1 = buffer.readu8(b, i)
|
||||
local a2 = if i + 1 < n then buffer.readu8(b, i + 1) else 0
|
||||
local a3 = if i + 2 < n then buffer.readu8(b, i + 2) else 0
|
||||
local v = a1 * 65536 + a2 * 256 + a3
|
||||
local c1, c2, c3, c4 = v // 262144, (v // 4096) % 64, (v // 64) % 64, v % 64
|
||||
table.insert(out, string.sub(A, c1 + 1, c1 + 1) .. string.sub(A, c2 + 1, c2 + 1)
|
||||
.. (if i + 1 < n then string.sub(A, c3 + 1, c3 + 1) else "=") .. (if i + 2 < n then string.sub(A, c4 + 1, c4 + 1) else "="))
|
||||
end
|
||||
return table.concat(out)
|
||||
end
|
||||
|
||||
local function isArray(t: { [any]: any }): boolean
|
||||
if Types.isObject(t) then
|
||||
return false
|
||||
@@ -202,7 +187,7 @@ local function scalar(st: St, v: any, ind: string, key: boolean): string?
|
||||
elseif tv == "string" then
|
||||
return str(v, ind, key)
|
||||
elseif tv == "buffer" then
|
||||
return "!!binary " .. b64(v)
|
||||
return "!!binary " .. Base64.encode(v)
|
||||
elseif tv == "table" and Types.Date.is(v) then
|
||||
return tostring(v)
|
||||
end
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
-- schema. Not supported: complex keys ("? "), collections as keys, recursive aliases, merge keys "<<" (kept as a
|
||||
-- normal key). Internally raises { yamlErr } tables caught by run(); never throws past it.
|
||||
local Types = require("@formats/Types")
|
||||
local Base64 = require("@formats/core/Base64")
|
||||
|
||||
local Parse = {}
|
||||
|
||||
@@ -171,39 +172,8 @@ function Parse.resolve(t: string, nullv: any): any
|
||||
return t
|
||||
end
|
||||
|
||||
local B64: { [number]: number } = {}
|
||||
do
|
||||
local alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
|
||||
for i = 1, 64 do
|
||||
B64[byte(alphabet, i)] = i - 1
|
||||
end
|
||||
end
|
||||
local function base64(t: string): buffer?
|
||||
t = string.gsub(t, "[%s]", "")
|
||||
t = string.gsub(t, "=+$", "")
|
||||
local n = #t
|
||||
if n % 4 == 1 then
|
||||
return nil
|
||||
end
|
||||
local out = buffer.create(n * 3 // 4)
|
||||
local o = 0
|
||||
local acc, bits = 0, 0
|
||||
for i = 1, n do
|
||||
local v = B64[byte(t, i)]
|
||||
if v == nil then
|
||||
return nil
|
||||
end
|
||||
acc = acc * 64 + v
|
||||
bits += 6
|
||||
if bits >= 8 then
|
||||
bits -= 8
|
||||
local b = acc // (2 ^ bits)
|
||||
buffer.writeu8(out, o, b)
|
||||
o += 1
|
||||
acc -= b * 2 ^ bits
|
||||
end
|
||||
end
|
||||
return out
|
||||
return (Base64.decode(t))
|
||||
end
|
||||
|
||||
-- a scalar's value given its tag (nil = untagged, "!" = non-specific)
|
||||
|
||||
@@ -20,7 +20,7 @@ local Yaml = {
|
||||
category = "data",
|
||||
extensions = { "yaml", "yml" },
|
||||
mime = { "application/yaml", "application/x-yaml", "text/yaml" },
|
||||
deps = { "core/Err", "core/Bytes", "Types" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "core/Base64" },
|
||||
caps = { decode = true, encode = true, ordered = true, multiDocument = true, anchors = true },
|
||||
}
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
local Err = require("@formats/core/Err")
|
||||
local Types = require("@formats/Types")
|
||||
local Xml = require("@formats/codecs/Xml")
|
||||
local Base64 = require("@formats/codecs/Base64")
|
||||
local Base64 = require("@formats/core/Base64")
|
||||
local M = require("@formats/model/dae/Math")
|
||||
|
||||
local Read = {}
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
-- lights. Not written: skins, morph targets, animations.
|
||||
local Types = require("@formats/Types")
|
||||
local Xml = require("@formats/codecs/Xml")
|
||||
local Base64 = require("@formats/codecs/Base64")
|
||||
local Base64 = require("@formats/core/Base64")
|
||||
local M = require("@formats/model/dae/Math")
|
||||
|
||||
local Write = {}
|
||||
|
||||
@@ -37,7 +37,7 @@ local Dae = {
|
||||
category = "model",
|
||||
extensions = { "dae", "zae" },
|
||||
mime = { "model/vnd.collada+xml", "model/vnd.collada+zip" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/Xml", "codecs/Base64", "containers/Zip" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/Xml", "core/Base64", "containers/Zip" },
|
||||
caps = { decode = true, encode = true, materials = true, polygons = true, skins = true, morphTargets = true, animation = true, cameras = true, lights = true, zae = true },
|
||||
}
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
-- scenes. glTF extras and unhandled extensions are kept in <object>.extras.gltf = { extras, extensions }.
|
||||
local Err = require("@formats/core/Err")
|
||||
local Types = require("@formats/Types")
|
||||
local Base64 = require("@formats/codecs/Base64")
|
||||
local Base64 = require("@formats/core/Base64")
|
||||
local Pack = require("@formats/model/gltf/Pack")
|
||||
|
||||
local Read = {}
|
||||
|
||||
@@ -20,7 +20,7 @@ local Err = require("@formats/core/Err")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Types = require("@formats/Types")
|
||||
local Json = require("@formats/codecs/Json")
|
||||
local Base64 = require("@formats/codecs/Base64")
|
||||
local Base64 = require("@formats/core/Base64")
|
||||
local Read = require("@formats/model/gltf/Read")
|
||||
local Write = require("@formats/model/gltf/Write")
|
||||
|
||||
@@ -30,7 +30,7 @@ local Gltf = {
|
||||
category = "model",
|
||||
extensions = { "gltf", "glb" },
|
||||
mime = { "model/gltf+json", "model/gltf-binary" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/Json", "codecs/Base64" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/Json", "core/Base64" },
|
||||
caps = { decode = true, encode = true, binary = true, materials = true, animation = true, skins = true, morphTargets = true, cameras = true, lights = true },
|
||||
}
|
||||
|
||||
|
||||
@@ -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
@@ -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()
|
||||
@@ -8,7 +8,7 @@
|
||||
# by assimp and trimesh: { name, case, assimp, trimesh }
|
||||
import importlib.util, json, os, struct
|
||||
|
||||
INPUTS = ["tests/fixtures/gltf", "src/Formats/model/gltf", "src/Formats/codecs/Json", "src/Formats/codecs/Base64",
|
||||
INPUTS = ["tests/fixtures/gltf", "src/Formats/model/gltf", "src/Formats/codecs/Json", "src/Formats/core/Base64",
|
||||
"tools/fixtures/_model_ref.py", "tools/fixtures/_gltf_encode.luau"]
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user