Last active
September 29, 2020 01:40
-
-
Save Anaminus/e25a32f3f8e5214de5f4 to your computer and use it in GitHub Desktop.
Lua - Base64 Fast Encode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| local encodeByte = { | |
| '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','-','_', | |
| } | |
| local byte = string.byte | |
| local concat = table.concat | |
| local function b64fastenc(data) | |
| local out = {} | |
| local nout = 1 | |
| local ndata = #data | |
| for i = 1, ndata-ndata%3, 3 do | |
| local b1,b2,b3 = byte(data, i, i+2) | |
| local b1r = b1/4 | |
| local b2r = b2/16 | |
| local b3r = b3/64 | |
| local x = b1%4*16 | |
| local v = b2%16*4 | |
| local y = b2r%256 - b2r%1 | |
| local w = b3r%256 - b3r%1 | |
| local x0,x1,x2,x3,x4,x5,x6,x7,x8 = x%1,x%2,x%4,x%8,x%16,x%32,x%64,x%128,x%256 | |
| local y0,y1,y2,y3,y4,y5,y6,y7,y8 = y%1,y%2,y%4,y%8,y%16,y%32,y%64,y%128,y%256 | |
| local v0,v1,v2,v3,v4,v5,v6,v7,v8 = v%1,v%2,v%4,v%8,v%16,v%32,v%64,v%128,v%256 | |
| local w0,w1,w2,w3,w4,w5,w6,w7,w8 = w%1,w%2,w%4,w%8,w%16,w%32,w%64,w%128,w%256 | |
| out[nout] = encodeByte[1 + b1r%256 - b1r%1] | |
| out[nout+1] = encodeByte[1 | |
| + ((x1 - x0 ~= 0 or y1 - y0 ~= 0) and 1 or 0) | |
| + ((x2 - x1 ~= 0 or y2 - y1 ~= 0) and 2 or 0) | |
| + ((x3 - x2 ~= 0 or y3 - y2 ~= 0) and 4 or 0) | |
| + ((x4 - x3 ~= 0 or y4 - y3 ~= 0) and 8 or 0) | |
| + ((x5 - x4 ~= 0 or y5 - y4 ~= 0) and 16 or 0) | |
| + ((x6 - x5 ~= 0 or y6 - y5 ~= 0) and 32 or 0) | |
| + ((x7 - x6 ~= 0 or y7 - y6 ~= 0) and 64 or 0) | |
| + ((x8 - x7 ~= 0 or y8 - y7 ~= 0) and 128 or 0)] | |
| out[nout+2] = encodeByte[1 | |
| + ((v1 - v0 ~= 0 or w1 - w0 ~= 0) and 1 or 0) | |
| + ((v2 - v1 ~= 0 or w2 - w1 ~= 0) and 2 or 0) | |
| + ((v3 - v2 ~= 0 or w3 - w2 ~= 0) and 4 or 0) | |
| + ((v4 - v3 ~= 0 or w4 - w3 ~= 0) and 8 or 0) | |
| + ((v5 - v4 ~= 0 or w5 - w4 ~= 0) and 16 or 0) | |
| + ((v6 - v5 ~= 0 or w6 - w5 ~= 0) and 32 or 0) | |
| + ((v7 - v6 ~= 0 or w7 - w6 ~= 0) and 64 or 0) | |
| + ((v8 - v7 ~= 0 or w8 - w7 ~= 0) and 128 or 0)] | |
| out[nout+3] = encodeByte[1 + b3 % 64] | |
| nout = nout + 4 | |
| end | |
| if -ndata%3 > 0 then | |
| local i = ndata-ndata%3 | |
| local b1 = byte(data, i+1) or 0 | |
| local b2 = byte(data, i+2) or 0 | |
| local b3 = byte(data, i+3) or 0 | |
| local b1r = b1/4 | |
| local b2r = b2/16 | |
| local b3r = b3/64 | |
| local x = b1%4*16 | |
| local v = b2%16*4 | |
| local y = b2r%256 - b2r%1 | |
| local w = b3r%256 - b3r%1 | |
| local x0,x1,x2,x3,x4,x5,x6,x7,x8 = x%1,x%2,x%4,x%8,x%16,x%32,x%64,x%128,x%256 | |
| local y0,y1,y2,y3,y4,y5,y6,y7,y8 = y%1,y%2,y%4,y%8,y%16,y%32,y%64,y%128,y%256 | |
| local v0,v1,v2,v3,v4,v5,v6,v7,v8 = v%1,v%2,v%4,v%8,v%16,v%32,v%64,v%128,v%256 | |
| local w0,w1,w2,w3,w4,w5,w6,w7,w8 = w%1,w%2,w%4,w%8,w%16,w%32,w%64,w%128,w%256 | |
| out[nout] = encodeByte[1 + b1r%256 - b1r%1] | |
| out[nout+1] = encodeByte[1 | |
| + ((x1 - x0 ~= 0 or y1 - y0 ~= 0) and 1 or 0) | |
| + ((x2 - x1 ~= 0 or y2 - y1 ~= 0) and 2 or 0) | |
| + ((x3 - x2 ~= 0 or y3 - y2 ~= 0) and 4 or 0) | |
| + ((x4 - x3 ~= 0 or y4 - y3 ~= 0) and 8 or 0) | |
| + ((x5 - x4 ~= 0 or y5 - y4 ~= 0) and 16 or 0) | |
| + ((x6 - x5 ~= 0 or y6 - y5 ~= 0) and 32 or 0) | |
| + ((x7 - x6 ~= 0 or y7 - y6 ~= 0) and 64 or 0) | |
| + ((x8 - x7 ~= 0 or y8 - y7 ~= 0) and 128 or 0)] or '=' | |
| out[nout+2] = ndata-i > 1 and encodeByte[1 | |
| + ((v1 - v0 ~= 0 or w1 - w0 ~= 0) and 1 or 0) | |
| + ((v2 - v1 ~= 0 or w2 - w1 ~= 0) and 2 or 0) | |
| + ((v3 - v2 ~= 0 or w3 - w2 ~= 0) and 4 or 0) | |
| + ((v4 - v3 ~= 0 or w4 - w3 ~= 0) and 8 or 0) | |
| + ((v5 - v4 ~= 0 or w5 - w4 ~= 0) and 16 or 0) | |
| + ((v6 - v5 ~= 0 or w6 - w5 ~= 0) and 32 or 0) | |
| + ((v7 - v6 ~= 0 or w7 - w6 ~= 0) and 64 or 0) | |
| + ((v8 - v7 ~= 0 or w8 - w7 ~= 0) and 128 or 0)] or '=' | |
| out[nout+3] = ndata-i > 2 and encodeByte[1 + b3 % 64] or '=' | |
| nout = nout + 4 | |
| end | |
| return concat(out) | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment