Module:Base64: Difference between revisions

From Celeste Wiki
Jump to navigation Jump to search
Content added Content deleted
No edit summary
(WHY IS THIS SO COMPLICATED OMFG)
Line 20: Line 20:
yesno(encode, false)
yesno(encode, false)


local index_table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'


function to_binary(integer)
local remaining = tonumber(integer)
local bin_bits = ''


for i = 7, 0, -1 do
local current_power = 2 ^ i


-- encoding
if remaining >= current_power then
local function enc(data)
bin_bits = bin_bits .. '1'
return ((data:gsub('.', function(x)
remaining = remaining - current_power
else
local r,b='',x:byte()
bin_bits = bin_bits .. '0'
for i=8,1,-1 do r=r..(b%2^i-b%2^(i-1)>0 and '1' or '0') end
end
return r;
end)..'0000'):gsub('%d%d%d?%d?%d?%d?', function(x)
end
if (#x < 6) then return '' end

return bin_bits
local c=0
for i=1,6 do c=c+(x:sub(i,i)=='1' and 2^(6-i) or 0) end
return b:sub(c+1,c+1)
end)..({ '', '==', '=' })[#data%3+1])
end
end


-- decoding
function from_binary(bin_bits)
local function dec(data)
return tonumber(bin_bits, 2)
data = string.gsub(data, '[^'..b..'=]', '')
return (data:gsub('.', function(x)
if (x == '=') then return '' end
local r,f='',(b:find(x)-1)
for i=6,1,-1 do r=r..(f%2^i-f%2^(i-1)>0 and '1' or '0') end
return r;
end):gsub('%d%d%d?%d?%d?%d?%d?%d?', function(x)
if (#x ~= 8) then return '' end
local c=0
for i=1,8 do c=c+(x:sub(i,i)=='1' and 2^(8-i) or 0) end
return string.char(c)
end))
end
end


function to_base64(to_encode)
local bit_pattern = ''
local encoded = ''
local trailing = ''

for i = 1, string.len(to_encode) do
bit_pattern = bit_pattern .. to_binary(string.byte(string.sub(to_encode, i, i)))
end

-- Check the number of bytes. If it's not evenly divisible by three,
-- zero-pad the ending & append on the correct number of ``=``s.
if string.len(bit_pattern) % 3 == 2 then
trailing = '=='
bit_pattern = bit_pattern .. '0000000000000000'
elseif string.len(bit_pattern) % 3 == 1 then
trailing = '='
bit_pattern = bit_pattern .. '00000000'
end

for i = 1, string.len(bit_pattern), 6 do
local byte = string.sub(bit_pattern, i, i+5)
local offset = tonumber(from_binary(byte))
encoded = encoded .. string.sub(index_table, offset+1, offset+1)
end

local result = string.sub(encoded, 1, -1 - string.len(trailing)) .. trailing
return(result)
end


function from_base64(to_decode)
local padded = to_decode:gsub("%s", "")
local unpadded = padded:gsub("=", "")
local bit_pattern = ''
local decoded = ''

for i = 1, string.len(unpadded) do
local char = string.sub(to_decode, i, i)
local offset, _ = string.find(index_table, char)
if offset == nil then
error("Invalid character '" .. char .. "' found.")
end

bit_pattern = bit_pattern .. string.sub(to_binary(offset-1), 3)
end

for i = 1, string.len(bit_pattern), 8 do
local byte = string.sub(bit_pattern, i, i+7)
decoded = decoded .. string.char(from_binary(byte))
end

local padding_length = padded:len()-unpadded:len()

if (padding_length == 1 or padding_length == 2) then
decoded = decoded:sub(1,-2)
end
return decoded
end



if (encode == true) then
if (encode == true) then
to_base64(data)
enc(data)
return r
else
else
from_base64(data)
dec(data)
return r
local result = decoded
return(result)
end
end
end
end

Revision as of 15:10, 14 January 2021

Documentation for this module may be created at Module:Base64/doc

-- Made by [[User:Celeste]] - Licensed under GPLv3

local p = {}

local yesno = require('Module:Yesno') -- invoke Yesno

local b='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' -- You will need this for encoding/decoding

function p.base64(frame)
	local args
	local data
	local encode
	if frame == mw.getCurrentFrame() then
    	args = frame.args
	else
    	args = frame
	end
	data = args.data
	encode = args.encode
	yesno(encode, false)




-- encoding
local function enc(data)
    return ((data:gsub('.', function(x) 
        local r,b='',x:byte()
        for i=8,1,-1 do r=r..(b%2^i-b%2^(i-1)>0 and '1' or '0') end
        return r;
    end)..'0000'):gsub('%d%d%d?%d?%d?%d?', function(x)
        if (#x < 6) then return '' end
        local c=0
        for i=1,6 do c=c+(x:sub(i,i)=='1' and 2^(6-i) or 0) end
        return b:sub(c+1,c+1)
    end)..({ '', '==', '=' })[#data%3+1])
end

-- decoding
local function dec(data)
    data = string.gsub(data, '[^'..b..'=]', '')
    return (data:gsub('.', function(x)
        if (x == '=') then return '' end
        local r,f='',(b:find(x)-1)
        for i=6,1,-1 do r=r..(f%2^i-f%2^(i-1)>0 and '1' or '0') end
        return r;
    end):gsub('%d%d%d?%d?%d?%d?%d?%d?', function(x)
        if (#x ~= 8) then return '' end
        local c=0
        for i=1,8 do c=c+(x:sub(i,i)=='1' and 2^(8-i) or 0) end
            return string.char(c)
    end))
end

if (encode == true) then
		enc(data)
		return r
	else
		dec(data)
		return r
end
end

return p