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).
|
-- Never throws: bad data and bad arguments return nil, Err (TRUNCATED / CORRUPT / LIMIT / UNSUPPORTED / ARG).
|
||||||
local Err = require("@formats/core/Err")
|
local Err = require("@formats/core/Err")
|
||||||
local Crc32 = require("@formats/core/Crc32")
|
local Crc32 = require("@formats/core/Crc32")
|
||||||
|
local Base64 = require("@formats/core/Base64")
|
||||||
local Deflate = require("@formats/codecs/Deflate")
|
local Deflate = require("@formats/codecs/Deflate")
|
||||||
local Decode = require("@formats/codecs/Brotli/Decode")
|
local Decode = require("@formats/codecs/Brotli/Decode")
|
||||||
local StaticData = require("@formats/codecs/Brotli/Static")
|
local StaticData = require("@formats/codecs/Brotli/Static")
|
||||||
@@ -25,37 +26,9 @@ export type Options = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
-- ------------------------------------------------------------------------------------------------ static data
|
-- ------------------------------------------------------------------------------------------------ static data
|
||||||
local B64 = {}
|
-- the tables are base64 text (trusted, generated by tools/brotli_static.py)
|
||||||
do
|
|
||||||
local alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
|
|
||||||
for i = 1, 64 do
|
|
||||||
B64[string.byte(alphabet, i)] = i - 1
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local function unbase64(s: string): buffer
|
local function unbase64(s: string): buffer
|
||||||
local n = #s
|
return Base64.decode(s) or buffer.create(0)
|
||||||
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
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local static: Decode.Static? = nil
|
local static: Decode.Static? = nil
|
||||||
|
|||||||
File renamed without changes.
File renamed without changes.
+139
-61
@@ -1,89 +1,167 @@
|
|||||||
--!strict
|
--!strict
|
||||||
--!native
|
--!native
|
||||||
-- core/Base64: RFC 4648 base64 (standard alphabet, and the URL-safe one on decode). Tiled map data, data: URIs, ...
|
-- core/Base64: RFC 4648 base64 (standard alphabet, and base64url with opts.url) encode / decode, and data: URIs.
|
||||||
-- decode skips whitespace and accepts missing padding; other bad characters fail. Never throws.
|
-- 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 s = Base64.encode(buf) -- buffer | string -> string ("" for empty input)
|
||||||
-- local b, err = Base64.decode(s) -- string -> buffer | nil, "message"
|
-- 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 Base64 = {}
|
||||||
|
|
||||||
local ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
|
export type EncodeOptions = {
|
||||||
local ENC = buffer.create(64)
|
url: boolean?, -- the URL-safe alphabet (- and _); default false
|
||||||
buffer.writestring(ENC, 0, ALPHABET)
|
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)
|
local DEC = buffer.create(256)
|
||||||
buffer.fill(DEC, 0, 255)
|
buffer.fill(DEC, 0, 255)
|
||||||
for i = 0, 63 do
|
for i = 1, 64 do
|
||||||
buffer.writeu8(DEC, string.byte(ALPHABET, i + 1), i)
|
buffer.writeu8(DEC, string.byte(ALPHA, i), i - 1)
|
||||||
end
|
end
|
||||||
buffer.writeu8(DEC, string.byte("-"), 62)
|
buffer.writeu8(DEC, string.byte("-"), 62)
|
||||||
buffer.writeu8(DEC, string.byte("_"), 63)
|
buffer.writeu8(DEC, string.byte("_"), 63)
|
||||||
for _, c in { 9, 10, 13, 32 } do
|
for _, c in { 9, 10, 11, 12, 13, 32 } do
|
||||||
buffer.writeu8(DEC, c, 254) -- whitespace: skipped
|
buffer.writeu8(DEC, c, 64)
|
||||||
end
|
end
|
||||||
|
buffer.writeu8(DEC, string.byte("="), 65)
|
||||||
|
|
||||||
local readu8, writeu8 = buffer.readu8, buffer.writeu8
|
local readu8, writeu8 = buffer.readu8, buffer.writeu8
|
||||||
|
|
||||||
-- encode(data) -> base64 text with padding
|
-- encode(data, opts?, offset?, length?) -> base64 text of data[offset, offset + length)
|
||||||
function Base64.encode(data: buffer | string): string
|
function Base64.encode(data: buffer | string, opts: EncodeOptions?, offset: number?, length: number?): string
|
||||||
local src: buffer = if type(data) == "string" then buffer.fromstring(data) else data
|
local buf: buffer = if type(data) == "string" then buffer.fromstring(data) else data
|
||||||
local n = buffer.len(src)
|
local total = buffer.len(buf)
|
||||||
local out = buffer.create((n + 2) // 3 * 4)
|
local o = math.clamp(offset or 0, 0, total)
|
||||||
local o = 0
|
local n = math.clamp(length or (total - o), 0, total - o)
|
||||||
local i = 0
|
local enc = if opts and opts.url then ENC_URL else ENC
|
||||||
while i + 2 < n do
|
local pad = not (opts and opts.pad == false)
|
||||||
local v = readu8(src, i) * 65536 + readu8(src, i + 1) * 256 + readu8(src, i + 2)
|
local wrap = if opts and opts.wrap and opts.wrap >= 4 then opts.wrap // 4 else 0 -- groups per line
|
||||||
writeu8(out, o, readu8(ENC, v // 262144))
|
local full = n // 3
|
||||||
writeu8(out, o + 1, readu8(ENC, v // 4096 % 64))
|
local rest = n - full * 3
|
||||||
writeu8(out, o + 2, readu8(ENC, v // 64 % 64))
|
local textLen = full * 4 + (if rest == 0 then 0 elseif pad then 4 else rest + 1)
|
||||||
writeu8(out, o + 3, readu8(ENC, v % 64))
|
local groups = full + (if rest > 0 then 1 else 0)
|
||||||
i += 3
|
local breaks = if wrap > 0 and groups > 0 then (groups - 1) // wrap else 0
|
||||||
o += 4
|
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
|
end
|
||||||
local rest = n - i
|
|
||||||
if rest > 0 then
|
if rest > 0 then
|
||||||
local v = readu8(src, i) * 65536 + (if rest == 2 then readu8(src, i + 1) * 256 else 0)
|
if wrap > 0 and col == wrap then
|
||||||
writeu8(out, o, readu8(ENC, v // 262144))
|
writeu8(out, p, 10)
|
||||||
writeu8(out, o + 1, readu8(ENC, v // 4096 % 64))
|
p += 1
|
||||||
writeu8(out, o + 2, if rest == 2 then readu8(ENC, v // 64 % 64) else 61)
|
end
|
||||||
writeu8(out, o + 3, 61)
|
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
|
end
|
||||||
return buffer.tostring(out)
|
return buffer.tostring(out)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- decode(text) -> buffer | nil, message
|
-- decode(s) -> buffer | nil, message
|
||||||
function Base64.decode(text: string | buffer): (buffer?, string?)
|
function Base64.decode(s: string | buffer): (buffer?, string?)
|
||||||
local src: buffer = if type(text) == "string" then buffer.fromstring(text) else text
|
local src: buffer = if type(s) == "string" then buffer.fromstring(s) else s
|
||||||
local n = buffer.len(src)
|
local n = buffer.len(src)
|
||||||
local out = buffer.create(n // 4 * 3 + 3)
|
local out = buffer.create((n // 4 + 1) * 3)
|
||||||
local o = 0
|
local acc, bits, p = 0, 0, 0
|
||||||
local acc, bits = 0, 0
|
local padded = false
|
||||||
local pad = false
|
|
||||||
for i = 0, n - 1 do
|
for i = 0, n - 1 do
|
||||||
local c = readu8(src, i)
|
local d = readu8(DEC, readu8(src, i))
|
||||||
local v = readu8(DEC, c)
|
if d < 64 then
|
||||||
if v == 254 then
|
if padded then
|
||||||
continue
|
return nil, "data after padding"
|
||||||
end
|
end
|
||||||
if c == 61 then -- '='
|
acc = acc * 64 + d
|
||||||
pad = true
|
bits += 6
|
||||||
continue
|
if bits >= 8 then
|
||||||
end
|
bits -= 8
|
||||||
if v == 255 or pad then
|
local scale = 2 ^ bits
|
||||||
return nil, string.format("bad base64 character at %d", i)
|
local byte = acc // scale
|
||||||
end
|
writeu8(out, p, byte % 256)
|
||||||
acc = acc * 64 + v
|
p += 1
|
||||||
bits += 6
|
acc -= byte * scale
|
||||||
if bits >= 8 then
|
end
|
||||||
bits -= 8
|
elseif d == 65 then
|
||||||
local p = 2 ^ bits
|
padded = true
|
||||||
writeu8(out, o, acc // p)
|
elseif d ~= 64 then
|
||||||
acc %= p
|
return nil, string.format("bad base64 character 0x%02x at %d", readu8(src, i), i)
|
||||||
o += 1
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
local res = buffer.create(o)
|
if bits >= 6 then
|
||||||
buffer.copy(res, 0, out, 0, o)
|
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
|
return res, nil
|
||||||
end
|
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
|
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 Types = require("@formats/Types")
|
||||||
local Xml = require("@formats/codecs/Xml")
|
local Xml = require("@formats/codecs/Xml")
|
||||||
local Json = require("@formats/codecs/Json")
|
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")
|
local Binary = require("@formats/data/plist/Binary")
|
||||||
|
|
||||||
-- ------------------------------------------------------------------------------------------------ contract
|
-- ------------------------------------------------------------------------------------------------ contract
|
||||||
@@ -20,7 +20,7 @@ local Plist = {
|
|||||||
category = "data",
|
category = "data",
|
||||||
extensions = { "plist" },
|
extensions = { "plist" },
|
||||||
mime = { "application/x-plist", "application/x-bplist" },
|
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 },
|
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
|
elseif tv == "buffer" then
|
||||||
line(level, "<data>")
|
line(level, "<data>")
|
||||||
local maxLine = math.max(16, 76 - 8 * level)
|
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)
|
line(level, l)
|
||||||
end
|
end
|
||||||
line(level, "</data>")
|
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
|
-- !!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.
|
-- values (cycles, functions, collection keys) return nil, message.
|
||||||
local Types = require("@formats/Types")
|
local Types = require("@formats/Types")
|
||||||
|
local Base64 = require("@formats/core/Base64")
|
||||||
local Parse = require("@formats/data/yaml/Parse")
|
local Parse = require("@formats/data/yaml/Parse")
|
||||||
|
|
||||||
local Emit = {}
|
local Emit = {}
|
||||||
@@ -119,22 +120,6 @@ local function str(s: string, ind: string, key: boolean): string
|
|||||||
return dquote(s)
|
return dquote(s)
|
||||||
end
|
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
|
local function isArray(t: { [any]: any }): boolean
|
||||||
if Types.isObject(t) then
|
if Types.isObject(t) then
|
||||||
return false
|
return false
|
||||||
@@ -202,7 +187,7 @@ local function scalar(st: St, v: any, ind: string, key: boolean): string?
|
|||||||
elseif tv == "string" then
|
elseif tv == "string" then
|
||||||
return str(v, ind, key)
|
return str(v, ind, key)
|
||||||
elseif tv == "buffer" then
|
elseif tv == "buffer" then
|
||||||
return "!!binary " .. b64(v)
|
return "!!binary " .. Base64.encode(v)
|
||||||
elseif tv == "table" and Types.Date.is(v) then
|
elseif tv == "table" and Types.Date.is(v) then
|
||||||
return tostring(v)
|
return tostring(v)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
-- schema. Not supported: complex keys ("? "), collections as keys, recursive aliases, merge keys "<<" (kept as a
|
-- 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.
|
-- normal key). Internally raises { yamlErr } tables caught by run(); never throws past it.
|
||||||
local Types = require("@formats/Types")
|
local Types = require("@formats/Types")
|
||||||
|
local Base64 = require("@formats/core/Base64")
|
||||||
|
|
||||||
local Parse = {}
|
local Parse = {}
|
||||||
|
|
||||||
@@ -171,39 +172,8 @@ function Parse.resolve(t: string, nullv: any): any
|
|||||||
return t
|
return t
|
||||||
end
|
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?
|
local function base64(t: string): buffer?
|
||||||
t = string.gsub(t, "[%s]", "")
|
return (Base64.decode(t))
|
||||||
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
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- a scalar's value given its tag (nil = untagged, "!" = non-specific)
|
-- a scalar's value given its tag (nil = untagged, "!" = non-specific)
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ local Yaml = {
|
|||||||
category = "data",
|
category = "data",
|
||||||
extensions = { "yaml", "yml" },
|
extensions = { "yaml", "yml" },
|
||||||
mime = { "application/yaml", "application/x-yaml", "text/yaml" },
|
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 },
|
caps = { decode = true, encode = true, ordered = true, multiDocument = true, anchors = true },
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,7 +11,7 @@
|
|||||||
local Err = require("@formats/core/Err")
|
local Err = require("@formats/core/Err")
|
||||||
local Types = require("@formats/Types")
|
local Types = require("@formats/Types")
|
||||||
local Xml = require("@formats/codecs/Xml")
|
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 M = require("@formats/model/dae/Math")
|
||||||
|
|
||||||
local Read = {}
|
local Read = {}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
-- lights. Not written: skins, morph targets, animations.
|
-- lights. Not written: skins, morph targets, animations.
|
||||||
local Types = require("@formats/Types")
|
local Types = require("@formats/Types")
|
||||||
local Xml = require("@formats/codecs/Xml")
|
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 M = require("@formats/model/dae/Math")
|
||||||
|
|
||||||
local Write = {}
|
local Write = {}
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ local Dae = {
|
|||||||
category = "model",
|
category = "model",
|
||||||
extensions = { "dae", "zae" },
|
extensions = { "dae", "zae" },
|
||||||
mime = { "model/vnd.collada+xml", "model/vnd.collada+zip" },
|
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 },
|
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 }.
|
-- scenes. glTF extras and unhandled extensions are kept in <object>.extras.gltf = { extras, extensions }.
|
||||||
local Err = require("@formats/core/Err")
|
local Err = require("@formats/core/Err")
|
||||||
local Types = require("@formats/Types")
|
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 Pack = require("@formats/model/gltf/Pack")
|
||||||
|
|
||||||
local Read = {}
|
local Read = {}
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ local Err = require("@formats/core/Err")
|
|||||||
local Bytes = require("@formats/core/Bytes")
|
local Bytes = require("@formats/core/Bytes")
|
||||||
local Types = require("@formats/Types")
|
local Types = require("@formats/Types")
|
||||||
local Json = require("@formats/codecs/Json")
|
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 Read = require("@formats/model/gltf/Read")
|
||||||
local Write = require("@formats/model/gltf/Write")
|
local Write = require("@formats/model/gltf/Write")
|
||||||
|
|
||||||
@@ -30,7 +30,7 @@ local Gltf = {
|
|||||||
category = "model",
|
category = "model",
|
||||||
extensions = { "gltf", "glb" },
|
extensions = { "gltf", "glb" },
|
||||||
mime = { "model/gltf+json", "model/gltf-binary" },
|
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 },
|
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
|
-- core/Base64 tests: the RFC 4648 vectors, encode / decode against Python's base64 (standard, URL-safe, unpadded) for every length 0..70
|
||||||
-- round trips.
|
-- 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 T = require("@tests/T")
|
||||||
local Base64 = require("@formats/core/Base64")
|
local Base64 = require("@formats/core/Base64")
|
||||||
|
local Cases = require("@tests/gen/base64_cases")
|
||||||
|
|
||||||
local t = T.suite("base64")
|
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
|
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(Base64.encode(v[1]), v[2], "encode " .. v[1])
|
||||||
t:eq(buffer.tostring(Base64.decode(v[2]) :: buffer), v[1], "decode " .. v[2])
|
t:eq(buffer.tostring(Base64.decode(v[2]) :: buffer), v[1], "decode " .. v[2])
|
||||||
@@ -22,4 +39,30 @@ for n = 0, 40 do
|
|||||||
end
|
end
|
||||||
t:eq(buffer.tostring(Base64.decode(Base64.encode(s)) :: buffer), buffer.tostring(s), "round trip " .. n)
|
t:eq(buffer.tostring(Base64.decode(Base64.encode(s)) :: buffer), buffer.tostring(s), "round trip " .. n)
|
||||||
end
|
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()
|
t:done()
|
||||||
@@ -8,7 +8,7 @@
|
|||||||
# by assimp and trimesh: { name, case, assimp, trimesh }
|
# by assimp and trimesh: { name, case, assimp, trimesh }
|
||||||
import importlib.util, json, os, struct
|
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"]
|
"tools/fixtures/_model_ref.py", "tools/fixtures/_gltf_encode.luau"]
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user