Pumunta sa nilalaman

Module:base64

Mula Wiksiyonaryo


local export = {}

-- {{R:ka:GED}}
function export.R_ka_GED(term)
	local data = [[a:2:{s:12:"word_metauri";s:]] .. string.len(term.args[1]) .. [[:"]].. term.args[1] .. [[";s:11:"word_statia";s:0:"";}]]
	return export.encode(data)
end

-- Lua 5.1 has no integer type or bitwise operators; divide-and-floor will have to do.
do local floor, ord, gsub = math.floor, string.byte, string.gsub

local b64t = {
	[0] =
	'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
	'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
	'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
}

function export.encode(data)
	return (gsub(data, '..?.?', function (m)
		local b0, b1, b2 = ord(m, 1, 3)

		if not b1 then
			local c0 = floor(b0 / 4)
			local c1 = (b0 % 4) * 16
			return b64t[c0] .. b64t[c1] .. '=='
		elseif not b2 then
			local c0 = floor(b0 / 4)
			local c1 = (b0 % 4) * 16 + floor(b1 / 16)
			local c2 = (b1 % 16) * 4
			return b64t[c0] .. b64t[c1] .. b64t[c2] .. '='
		else
			local c0 = floor(b0 / 4)
			local c1 = (b0 % 4) * 16 + floor(b1 / 16)
			local c2 = (b1 % 16) * 4 + floor(b2 / 64)
			local c3 = b2 % 64
			return b64t[c0] .. b64t[c1] .. b64t[c2] .. b64t[c3]
		end
	end))
end

end

export.enc = export.encode

return export