mypal125

Tomass1996's Base64 program with copyright notice at top

May 24th, 2014
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.05 KB | None | 0 0
  1. --[[
  2. Copyright © 2012 Thomas Farr a.k.a tomass1996 [[email protected]]
  3.  
  4. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
  5. associated documentation files (the "Software"), to deal in the Software without restriction,
  6. including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
  7. copies of the Software, and to permit persons to whom the Software is furnished to do so,
  8. subject to the following conditions:
  9.  
  10. -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  11. -Visible credit is given to the original author.
  12. -The software is distributed in a non-profit way.
  13.  
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  15. WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  16. COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  17. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  18. ]]--
  19. function lsh(value,shift)
  20.     return (value*(2^shift)) % 256
  21. end
  22. function rsh(value,shift)
  23.     return math.floor(value/2^shift) % 256
  24. end
  25. function bit(x,b)
  26.     return (x % 2^b - x % 2^(b-1) > 0)
  27. end
  28. function lor(x,y)
  29.     result = 0
  30.     for p=1,8 do result = result + (((bit(x,p) or bit(y,p)) == true) and 2^(p-1) or 0) end
  31.     return result
  32. end
  33.  
  34. -- Encoding
  35. local base64chars = {[0]='A',[1]='B',[2]='C',[3]='D',[4]='E',[5]='F',[6]='G',[7]='H',[8]='I',[9]='J',[10]='K',[11]='L',[12]='M',[13]='N',[14]='O',[15]='P',[16]='Q',[17]='R',[18]='S',[19]='T',[20]='U',[21]='V',[22]='W',[23]='X',[24]='Y',[25]='Z',[26]='a',[27]='b',[28]='c',[29]='d',[30]='e',[31]='f',[32]='g',[33]='h',[34]='i',[35]='j',[36]='k',[37]='l',[38]='m',[39]='n',[40]='o',[41]='p',[42]='q',[43]='r',[44]='s',[45]='t',[46]='u',[47]='v',[48]='w',[49]='x',[50]='y',[51]='z',[52]='0',[53]='1',[54]='2',[55]='3',[56]='4',[57]='5',[58]='6',[59]='7',[60]='8',[61]='9',[62]='-',[63]='_'}
  36. function encode(data)
  37.     local bytes = {}
  38.     local result = ""
  39.     for spos=0,string.len(data)-1,3 do
  40.         for byte=1,3 do bytes[byte] = string.byte(string.sub(data,(spos+byte))) or 0 end
  41.         result = string.format('%s%s%s%s%s',result,base64chars[rsh(bytes[1],2)],base64chars[lor(lsh((bytes[1] % 4),4), rsh(bytes[2],4))] or "=",((#data-spos) > 1) and base64chars[lor(lsh(bytes[2] % 16,2), rsh(bytes[3],6))] or "=",((#data-spos) > 2) and base64chars[(bytes[3] % 64)] or "=")
  42.     end
  43.     return result
  44. end
  45.  
  46. -- Decoding
  47. local base64bytes = {['A']=0,['B']=1,['C']=2,['D']=3,['E']=4,['F']=5,['G']=6,['H']=7,['I']=8,['J']=9,['K']=10,['L']=11,['M']=12,['N']=13,['O']=14,['P']=15,['Q']=16,['R']=17,['S']=18,['T']=19,['U']=20,['V']=21,['W']=22,['X']=23,['Y']=24,['Z']=25,['a']=26,['b']=27,['c']=28,['d']=29,['e']=30,['f']=31,['g']=32,['h']=33,['i']=34,['j']=35,['k']=36,['l']=37,['m']=38,['n']=39,['o']=40,['p']=41,['q']=42,['r']=43,['s']=44,['t']=45,['u']=46,['v']=47,['w']=48,['x']=49,['y']=50,['z']=51,['0']=52,['1']=53,['2']=54,['3']=55,['4']=56,['5']=57,['6']=58,['7']=59,['8']=60,['9']=61,['-']=62,['_']=63,['=']=nil}
  48. function decode(data)
  49.     local chars = {}
  50.     local result=""
  51.     for dpos=0,string.len(data)-1,4 do
  52.         for char=1,4 do chars[char] = base64bytes[(string.sub(data,(dpos+char),(dpos+char)) or "=")] end
  53.         result = string.format('%s%s%s%s',result,string.char(lor(lsh(chars[1],2), rsh(chars[2],4))),(chars[3] ~= nil) and string.char(lor(lsh(chars[2],4), rsh(chars[3],2))) or "",(chars[4] ~= nil) and string.char(lor(lsh(chars[3],6) % 192, (chars[4]))) or "")
  54.     end
  55.     return result
  56. end
  57.  
  58. tArgs = {...}
  59. EorD = tArgs[1]
  60. ForS = tArgs[2]
  61. tBE = tArgs[3]
  62. if not EorD or not ForS or not tBE then
  63.     error("Usage: Base64 <-E:-D> <-F:-S> <FileName:String>")
  64. end
  65.  
  66. if EorD == "-E" or EorD == "-e" then
  67.     if ForS == "-F" or ForS == "-f" then
  68.         if fs.exists(tBE) then
  69.             file = io.open(tBE, "r")
  70.             fileCont = file:read("*a")
  71.             file:close()
  72.             encoded = encode(fileCont)
  73.             encfile = io.open(tBE..".b64", "w")
  74.             encfile:write(encoded)
  75.             encfile:close()
  76.             print("Original File: "..tBE.." | Encoded File: "..tBE..".b64")
  77.         else
  78.             error("File "..tBE.." Doesn't Exist")
  79.         end
  80.     elseif ForS == "-S" or ForS == "-s" then
  81.         encoded = encode(tBE)
  82.         print("Original: "..tBE.." | Encoded: "..encoded)
  83.     else
  84.         error("I don't understand: "..ForS)
  85.     end
  86. elseif EorD == "-D" or EorD == "-d" then
  87.     if ForS == "-F" or ForS == "-f" then
  88.         if fs.exists(tBE) then
  89.             file = io.open(tBE, "r")
  90.             fileCont = file:read("*a")
  91.             file:close()
  92.             decoded = decode(fileCont)
  93.             decfile = io.open(tBE..".DECb64", "w")
  94.             decfile:write(decoded)
  95.             decfile:close()
  96.             print("Original File: "..tBE.." | Decoded File: "..tBE..".DECb64")
  97.         else
  98.             error("File "..tBE.." Doesn't Exist")
  99.         end
  100.     elseif ForS == "-S" or ForS == "-s" then
  101.         decoded = decode(tBE)
  102.         print("Original: "..tBE.." | Decoded: "..decoded)
  103.     else
  104.         error("I don't understand: "..ForS)
  105.     end
  106. else
  107.     error("I don't understand: "..EorD)
  108. end
Advertisement
Add Comment
Please, Sign In to add comment