Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local lookup_V2C = {}
- for ixChar = 65, 90 do lookup_V2C[ixChar - 65] = string.char(ixChar) end
- for ixChar = 97, 122 do lookup_V2C[ixChar - 71] = string.char(ixChar) end
- for ixChar = 48, 57 do lookup_V2C[ixChar + 4] = string.char(ixChar) end
- lookup_V2C[62] = "+"
- lookup_V2C[63] = "/"
- local lookup_C2V = {}
- for key, value in pairs(lookup_V2C) do lookup_C2V[value] = key end
- function encode(data)
- local result = ""
- for ix = 1, #data, 3 do
- local all24 = data:sub(ix, ix):byte() * 65536 + (data:sub(ix + 1, ix + 1):byte() or 0) * 256 + (data:sub(ix + 2, ix + 2):byte() or 0)
- result = result .. lookup_V2C[bit.band(all24, 16515072) / 262144] .. lookup_V2C[bit.band(all24, 258048) / 4096] .. lookup_V2C[bit.band(all24, 4032) / 64] .. lookup_V2C[bit.band(all24, 63)]
- end
- local padding = (3 - data:len() % 3) % 3
- return result:sub(1, result:len() - padding) .. string.rep("=", padding)
- end
- function decode(data)
- local result = ""
- for ix = 1, #data, 4 do
- local all24 = lookup_C2V[data:sub(ix, ix)] * 262144 + (lookup_C2V[data:sub(ix + 1, ix + 1)] or 0) * 4096 + (lookup_C2V[data:sub(ix + 2, ix + 2)] or 0) * 64 + (lookup_C2V[data:sub(ix + 3, ix + 3)] or 0)
- result = result .. string.char(bit.band(all24, 16711680) / 65536) .. string.char(bit.band(all24, 65280) / 256) .. string.char(bit.band(all24, 255))
- end
- return result:sub(1, result:len() - #data:match("^.-(=*)$"))
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement