Advertisement
Treyzania

Base64 for CC

Feb 18th, 2015
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.65 KB | None | 0 0
  1. -- Base64-encoding
  2. -- Sourced from http://en.wikipedia.org/wiki/Base64
  3. --require('math')
  4.  
  5.  
  6. local __author__ = 'Daniel Lindsley'
  7. local __version__ = 'scm-1'
  8. local __license__ = 'BSD'
  9.  
  10. local __forker__ = 'Treyzania'
  11. local __fork_reason__ = 'To work on ComputerCraft'
  12.  
  13. local index_table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
  14.  
  15.  
  16. function to_binary(integer)
  17.     local remaining = tonumber(integer)
  18.     local bin_bits = ''
  19.  
  20.     for i = 7, 0, -1 do
  21.         local current_power = math.pow(2, i)
  22.  
  23.         if remaining >= current_power then
  24.             bin_bits = bin_bits .. '1'
  25.             remaining = remaining - current_power
  26.         else
  27.             bin_bits = bin_bits .. '0'
  28.         end
  29.     end
  30.  
  31.     return bin_bits
  32. end
  33.  
  34. function from_binary(bin_bits)
  35.     return tonumber(bin_bits, 2)
  36. end
  37.  
  38.  
  39. function to_base64(to_encode)
  40.     local bit_pattern = ''
  41.     local encoded = ''
  42.     local trailing = ''
  43.  
  44.     for i = 1, string.len(to_encode) do
  45.         bit_pattern = bit_pattern .. to_binary(string.byte(string.sub(to_encode, i, i)))
  46.     end
  47.  
  48.     -- Check the number of bytes. If it's not evenly divisible by three,
  49.     -- zero-pad the ending & append on the correct number of ``=``s.
  50.     if (string.len(bit_pattern) % 3) == 2 then
  51.         trailing = '=='
  52.         bit_pattern = bit_pattern .. '0000000000000000'
  53.     elseif (string.len(bit_pattern) % 3) == 1 then
  54.         trailing = '='
  55.         bit_pattern = bit_pattern .. '00000000'
  56.     end
  57.  
  58.     for i = 1, string.len(bit_pattern), 6 do
  59.         local byte = string.sub(bit_pattern, i, i+5)
  60.         local offset = tonumber(from_binary(byte))
  61.         encoded = encoded .. string.sub(index_table, offset+1, offset+1)
  62.     end
  63.  
  64.     return string.sub(encoded, 1, -1 - string.len(trailing)) .. trailing
  65. end
  66.  
  67.  
  68. function from_base64(to_decode)
  69.     local padded = to_decode:gsub("%s", "")
  70.     local unpadded = padded:gsub("=", "")
  71.     local bit_pattern = ''
  72.     local decoded = ''
  73.  
  74.     for i = 1, string.len(unpadded) do
  75.         local char = string.sub(to_decode, i, i)
  76.         local offset, _ = string.find(index_table, char)
  77.         if offset == nil then
  78.              error("Invalid character '" .. char .. "' found.")
  79.         end
  80.  
  81.         bit_pattern = bit_pattern .. string.sub(to_binary(offset-1), 3)
  82.     end
  83.  
  84.     for i = 1, string.len(bit_pattern), 8 do
  85.         local byte = string.sub(bit_pattern, i, i+7)
  86.         decoded = decoded .. string.char(from_binary(byte))
  87.     end
  88.  
  89.     local padding_length = padded:len()-unpadded:len()
  90.  
  91.     if (padding_length == 1 or padding_length == 2) then
  92.         decoded = decoded:sub(1,-2)
  93.     end
  94.     return decoded
  95. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement