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,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 },
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user