basexx.lua 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. --------------------------------------------------------------------------------
  2. -- util functions
  3. --------------------------------------------------------------------------------
  4. local function divide_string( str, max, fillChar )
  5. fillChar = fillChar or ""
  6. local result = {}
  7. local start = 1
  8. for i = 1, #str do
  9. if i % max == 0 then
  10. table.insert( result, str:sub( start, i ) )
  11. start = i + 1
  12. elseif i == #str then
  13. table.insert( result, str:sub( start, i ) )
  14. end
  15. end
  16. return result
  17. end
  18. local function number_to_bit( num, length )
  19. local bits = {}
  20. while num > 0 do
  21. local rest = math.floor( math.fmod( num, 2 ) )
  22. table.insert( bits, rest )
  23. num = ( num - rest ) / 2
  24. end
  25. while #bits < length do
  26. table.insert( bits, "0" )
  27. end
  28. return string.reverse( table.concat( bits ) )
  29. end
  30. local function ignore_set( str, set )
  31. if set then
  32. str = str:gsub( "["..set.."]", "" )
  33. end
  34. return str
  35. end
  36. local function pure_from_bit( str )
  37. return ( str:gsub( '........', function ( cc )
  38. return string.char( tonumber( cc, 2 ) )
  39. end ) )
  40. end
  41. --------------------------------------------------------------------------------
  42. local basexx = {}
  43. --------------------------------------------------------------------------------
  44. -- base2(bitfield) decode and encode function
  45. --------------------------------------------------------------------------------
  46. local bitMap = { o = "0", i = "1", l = "1" }
  47. function basexx.from_bit( str, ignore )
  48. str = ignore_set( str, ignore )
  49. str = string.lower( str )
  50. str = str:gsub( '[ilo]', function( c ) return bitMap[ c ] end )
  51. local wrong = str:match( "[^01]" )
  52. if wrong then return nil, wrong end
  53. return pure_from_bit( str )
  54. end
  55. function basexx.to_bit( str )
  56. return ( str:gsub( '.', function ( c )
  57. local byte = string.byte( c )
  58. local bits = {}
  59. for i = 1,8 do
  60. table.insert( bits, byte % 2 )
  61. byte = math.floor( byte / 2 )
  62. end
  63. return table.concat( bits ):reverse()
  64. end ) )
  65. end
  66. --------------------------------------------------------------------------------
  67. -- base16(hex) decode and encode function
  68. --------------------------------------------------------------------------------
  69. function basexx.from_hex( str, ignore )
  70. str = ignore_set( str, ignore )
  71. local wrong = str:match( "[^%x]" )
  72. if wrong then return nil, wrong end
  73. return ( str:gsub( '..', function ( cc )
  74. return string.char( tonumber( cc, 16 ) )
  75. end ) )
  76. end
  77. function basexx.to_hex( str )
  78. return ( str:gsub( '.', function ( c )
  79. return string.format('%02X', string.byte( c ) )
  80. end ) )
  81. end
  82. --------------------------------------------------------------------------------
  83. -- generic function to decode and encode base32/base64
  84. --------------------------------------------------------------------------------
  85. local function from_basexx( str, alphabet, bits )
  86. local result = {}
  87. for i = 1, #str do
  88. local c = string.sub( str, i, i )
  89. if c ~= '=' then
  90. local index = string.find( alphabet, c, 1, true )
  91. if not index then
  92. return nil, c
  93. end
  94. table.insert( result, number_to_bit( index - 1, bits ) )
  95. end
  96. end
  97. local value = table.concat( result )
  98. local pad = #value % 8
  99. return pure_from_bit( string.sub( value, 1, #value - pad ) )
  100. end
  101. local function to_basexx( str, alphabet, bits, pad )
  102. local bitString = basexx.to_bit( str )
  103. local chunks = divide_string( bitString, bits )
  104. local result = {}
  105. for key,value in ipairs( chunks ) do
  106. if ( #value < bits ) then
  107. value = value .. string.rep( '0', bits - #value )
  108. end
  109. local pos = tonumber( value, 2 ) + 1
  110. table.insert( result, alphabet:sub( pos, pos ) )
  111. end
  112. table.insert( result, pad )
  113. return table.concat( result )
  114. end
  115. --------------------------------------------------------------------------------
  116. -- rfc 3548: http://www.rfc-editor.org/rfc/rfc3548.txt
  117. --------------------------------------------------------------------------------
  118. local base32Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"
  119. local base32PadMap = { "", "======", "====", "===", "=" }
  120. function basexx.from_base32( str, ignore )
  121. str = ignore_set( str, ignore )
  122. return from_basexx( string.upper( str ), base32Alphabet, 5 )
  123. end
  124. function basexx.to_base32( str )
  125. return to_basexx( str, base32Alphabet, 5, base32PadMap[ #str % 5 + 1 ] )
  126. end
  127. --------------------------------------------------------------------------------
  128. -- crockford: http://www.crockford.com/wrmg/base32.html
  129. --------------------------------------------------------------------------------
  130. local crockfordAlphabet = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"
  131. local crockfordMap = { O = "0", I = "1", L = "1" }
  132. function basexx.from_crockford( str, ignore )
  133. str = ignore_set( str, ignore )
  134. str = string.upper( str )
  135. str = str:gsub( '[ILOU]', function( c ) return crockfordMap[ c ] end )
  136. return from_basexx( str, crockfordAlphabet, 5 )
  137. end
  138. function basexx.to_crockford( str )
  139. return to_basexx( str, crockfordAlphabet, 5, "" )
  140. end
  141. --------------------------------------------------------------------------------
  142. -- base64 decode and encode function
  143. --------------------------------------------------------------------------------
  144. local base64Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"..
  145. "abcdefghijklmnopqrstuvwxyz"..
  146. "0123456789+/"
  147. local base64PadMap = { "", "==", "=" }
  148. function basexx.from_base64( str, ignore )
  149. str = ignore_set( str, ignore )
  150. return from_basexx( str, base64Alphabet, 6 )
  151. end
  152. function basexx.to_base64( str )
  153. return to_basexx( str, base64Alphabet, 6, base64PadMap[ #str % 3 + 1 ] )
  154. end
  155. --------------------------------------------------------------------------------
  156. -- URL safe base64 decode and encode function
  157. --------------------------------------------------------------------------------
  158. local url64Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"..
  159. "abcdefghijklmnopqrstuvwxyz"..
  160. "0123456789-_"
  161. function basexx.from_url64( str, ignore )
  162. str = ignore_set( str, ignore )
  163. return from_basexx( str, url64Alphabet, 6 )
  164. end
  165. function basexx.to_url64( str )
  166. return to_basexx( str, url64Alphabet, 6, "" )
  167. end
  168. --------------------------------------------------------------------------------
  169. --
  170. --------------------------------------------------------------------------------
  171. local z85Decoder = { 0x00, 0x44, 0x00, 0x54, 0x53, 0x52, 0x48, 0x00,
  172. 0x4B, 0x4C, 0x46, 0x41, 0x00, 0x3F, 0x3E, 0x45,
  173. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  174. 0x08, 0x09, 0x40, 0x00, 0x49, 0x42, 0x4A, 0x47,
  175. 0x51, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A,
  176. 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32,
  177. 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A,
  178. 0x3B, 0x3C, 0x3D, 0x4D, 0x00, 0x4E, 0x43, 0x00,
  179. 0x00, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10,
  180. 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
  181. 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20,
  182. 0x21, 0x22, 0x23, 0x4F, 0x00, 0x50, 0x00, 0x00 }
  183. function basexx.from_z85( str, ignore )
  184. str = ignore_set( str, ignore )
  185. if ( #str % 5 ) ~= 0 then return nil, #str % 5 end
  186. local result = {}
  187. local value = 0
  188. for i = 1, #str do
  189. local index = string.byte( str, i ) - 31
  190. if index < 1 or index >= #z85Decoder then return nil, index end
  191. value = ( value * 85 ) + z85Decoder[ index ]
  192. if ( i % 5 ) == 0 then
  193. local divisor = 256 * 256 * 256
  194. while divisor ~= 0 do
  195. local b = math.floor( value / divisor ) % 256
  196. table.insert( result, string.char( b ) )
  197. divisor = math.floor( divisor / 256 )
  198. end
  199. value = 0
  200. end
  201. end
  202. return table.concat( result )
  203. end
  204. local z85Encoder = "0123456789"..
  205. "abcdefghijklmnopqrstuvwxyz"..
  206. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"..
  207. ".-:+=^!/*?&<>()[]{}@%$#"
  208. function basexx.to_z85( str )
  209. if ( #str % 4 ) ~= 0 then return nil, #str, 4 end
  210. local result = {}
  211. local value = 0
  212. for i = 1, #str do
  213. local b = string.byte( str, i )
  214. value = ( value * 256 ) + b
  215. if ( i % 4 ) == 0 then
  216. local divisor = 85 * 85 * 85 * 85
  217. while divisor ~= 0 do
  218. local index = ( math.floor( value / divisor ) % 85 ) + 1
  219. table.insert( result, z85Encoder:sub( index, index ) )
  220. divisor = math.floor( divisor / 85 )
  221. end
  222. value = 0
  223. end
  224. end
  225. return table.concat( result )
  226. end
  227. --------------------------------------------------------------------------------
  228. return basexx