ws-audio: merge master (ws/m4a codecs/Mdct already in use since 6535a3c; audio/opus encoder, data, vec, 3d, webp)

This commit is contained in:
4DBug committed 2026-10-31 09:29:36 -05:00
commit 43b8f4b4f5
400 files changed
+82546 -38

No files matched your search

+89
View File
@@ -0,0 +1,89 @@
--!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.
--
-- local s = Base64.encode(buf) -- buffer | string -> string
-- local b, err = Base64.decode(s) -- string -> buffer | nil, "message"
local Base64 = {}
local ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
local ENC = buffer.create(64)
buffer.writestring(ENC, 0, ALPHABET)
local DEC = buffer.create(256)
buffer.fill(DEC, 0, 255)
for i = 0, 63 do
buffer.writeu8(DEC, string.byte(ALPHABET, i + 1), i)
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
end
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
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)
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
local n = buffer.len(src)
local out = buffer.create(n // 4 * 3 + 3)
local o = 0
local acc, bits = 0, 0
local pad = 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
end
end
local res = buffer.create(o)
buffer.copy(res, 0, out, 0, o)
return res, nil
end
return Base64