From 0eff56e521c925d309e60285e8bfc86fbb415f61 Mon Sep 17 00:00:00 2001 From: Martin Herkt Date: Fri, 8 Mar 2013 13:58:02 +0100 Subject: [PATCH] website utils: add base64 decoder Signed-off-by: Martin Herkt --- share/lua/website/quvi/util.lua | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/share/lua/website/quvi/util.lua b/share/lua/website/quvi/util.lua index 5927d1c..b6ec2f1 100644 --- a/share/lua/website/quvi/util.lua +++ b/share/lua/website/quvi/util.lua @@ -46,6 +46,36 @@ function M.slash_unescape (s) return s end +function M.base64_decode(s) + local itbl='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' + + s = s:gsub('[^' .. itbl .. '=]', '') + + return (s:gsub('.', function(x) + local r = '' + + if (x ~= '=') then + local f = (itbl:find(x) - 1) + + for i = 6, 1, -1 do + r = r .. (f % 2 ^ i - f % 2 ^ (i - 1) > 0 and '1' or '0') + end + end + + return r; + end):gsub('%d%d%d?%d?%d?%d?%d?%d?', function(x) + local c = 0 + + if (#x == 8) then + for i = 1, 8 do + c = c + (x:sub(i, i) == '1' and 2 ^ (8 - i) or 0) + end + end + + return string.char(c) + end)) +end + -- handles -- Check whether a website script can "handle" the specified URL -- Params: -- 2.11.4.GIT