fishermedders

FishOS /bin/util/str

Nov 6th, 2016
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 33.39 KB | None | 0 0
  1. --[[
  2.  
  3. http://www.computercraft.info/forums2/index.php?/topic/42-cc13string-utils-api/
  4.  
  5. Copyright (C) 2012 Thomas Farr a.k.a tomass1996 [farr.thomas@gmail.com]
  6.  
  7. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
  8. associated documentation files (the "Software"), to deal in the Software without restriction,
  9. including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. copies of the Software, and to permit persons to whom the Software is furnished to do so,
  11. subject to the following conditions:
  12.  
  13. -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  14. -Visible credit is given to the original author.
  15. -The software is distributed in a non-profit way.
  16.  
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  18. WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  19. COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  20. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. --]]
  22.  
  23. local floor,modf, insert = math.floor,math.modf, table.insert
  24. local char,format,rep = string.char,string.format,string.rep
  25.  
  26. local function basen(n,b)
  27. if n < 0 then
  28. n = -n
  29. end
  30. local t = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_abcdefghijklmnopqrstuvwxyz{|}~"
  31. if n < b then
  32. local ret = ""
  33. ret = ret..string.sub(t, (n%b)+1,(n%b)+1)
  34. return ret
  35. else
  36. local tob = tostring(basen(math.floor(n/b), b))
  37. local ret = tob..t:sub((n%b)+1,(n%b)+1)
  38. return ret
  39. end
  40. end
  41.  
  42. local Base64 = {}
  43. Base64["lsh"] = function(value,shift)
  44. return (value*(2^shift)) % 256
  45. end
  46. Base64["rsh"] = function(value,shift)
  47. return math.floor(value/2^shift) % 256
  48. end
  49. Base64["bit"] = function(x,b)
  50. return (x % 2^b - x % 2^(b-1) > 0)
  51. end
  52. Base64["lor"] = function(x,y)
  53. local result = 0
  54. for p=1,8 do result = result + (((Base64.bit(x,p) or Base64.bit(y,p)) == true) and 2^(p-1) or 0) end
  55. return result
  56. end
  57. Base64["base64chars"] = {
  58. [0]='A',[1]='B',[2]='C',[3]='D',[4]='E',[5]='F',[6]='G',[7]='H',[8]='I',[9]='J',[10]='K',
  59. [11]='L',[12]='M',[13]='N',[14]='O',[15]='P',[16]='Q',[17]='R',[18]='S',[19]='T',[20]='U',
  60. [21]='V',[22]='W',[23]='X',[24]='Y',[25]='Z',[26]='a',[27]='b',[28]='c',[29]='d',[30]='e',
  61. [31]='f',[32]='g',[33]='h',[34]='i',[35]='j',[36]='k',[37]='l',[38]='m',[39]='n',[40]='o',
  62. [41]='p',[42]='q',[43]='r',[44]='s',[45]='t',[46]='u',[47]='v',[48]='w',[49]='x',[50]='y',
  63. [51]='z',[52]='0',[53]='1',[54]='2',[55]='3',[56]='4',[57]='5',[58]='6',[59]='7',[60]='8',
  64. [61]='9',[62]='-',[63]='_'}
  65. Base64["base64bytes"] = {
  66. ['A']=0,['B']=1,['C']=2,['D']=3,['E']=4,['F']=5,['G']=6,['H']=7,['I']=8,['J']=9,['K']=10,
  67. ['L']=11,['M']=12,['N']=13,['O']=14,['P']=15,['Q']=16,['R']=17,['S']=18,['T']=19,['U']=20,
  68. ['V']=21,['W']=22,['X']=23,['Y']=24,['Z']=25,['a']=26,['b']=27,['c']=28,['d']=29,['e']=30,
  69. ['f']=31,['g']=32,['h']=33,['i']=34,['j']=35,['k']=36,['l']=37,['m']=38,['n']=39,['o']=40,
  70. ['p']=41,['q']=42,['r']=43,['s']=44,['t']=45,['u']=46,['v']=47,['w']=48,['x']=49,['y']=50,
  71. ['z']=51,['0']=52,['1']=53,['2']=54,['3']=55,['4']=56,['5']=57,['6']=58,['7']=59,['8']=60,
  72. ['9']=61,['-']=62,['_']=63,['=']=nil}
  73.  
  74. local base32 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"
  75.  
  76. local tSHA1 = {}
  77. tSHA1["bytes_to_w32"] = function(a,b,c,d) return a*0x1000000+b*0x10000+c*0x100+d end
  78. tSHA1["w32_to_bytes"] = function(i) return floor(i/0x1000000)%0x100,floor(i/0x10000)%0x100,floor(i/0x100)%0x100,i%0x100 end
  79. tSHA1["w32_rot"] = function(bits,a)
  80. local b2 = 2^(32-bits)
  81. local a,b = modf(a/b2)
  82. return a+b*b2*(2^(bits))
  83. end
  84. tSHA1["byte_to_bits"] = function(b)
  85. local b = function (n)
  86. local b = floor(b/n)
  87. return b%2==1
  88. end
  89. return b(1),b(2),b(4),b(8),b(16),b(32),b(64),b(128)
  90. end
  91. tSHA1["bits_to_byte"] = function(a,b,c,d,e,f,g,h)
  92. local function n(b,x) return b and x or 0 end
  93. return n(a,1)+n(b,2)+n(c,4)+n(d,8)+n(e,16)+n(f,32)+n(g,64)+n(h,128)
  94. end
  95. tSHA1["bits_to_string"] = function(a,b,c,d,e,f,g,h)
  96. local function x(b) return b and "1" or "0" end
  97. return ("%s%s%s%s %s%s%s%s"):format(x(a),x(b),x(c),x(d),x(e),x(f),x(g),x(h))
  98. end
  99. tSHA1["byte_to_bit_string"] = function(b) return tSHA1.bits_to_string(byte_to_bits(b)) end
  100. tSHA1["w32_to_bit_string"] = function(a)
  101. if type(a) == "string" then return a end
  102. local aa,ab,ac,ad = tSHA1.w32_to_bytes(a)
  103. local s = tSHA1.byte_to_bit_string
  104. return ("%s %s %s %s"):format(s(aa):reverse(),s(ab):reverse(),s(ac):reverse(),s(ad):reverse()):reverse()
  105. end
  106. tSHA1["band"] = function(a,b)
  107. local A,B,C,D,E,F,G,H = tSHA1.byte_to_bits(b)
  108. local a,b,c,d,e,f,g,h = tSHA1.byte_to_bits(a)
  109. return tSHA1.bits_to_byte(
  110. A and a, B and b, C and c, D and d,
  111. E and e, F and f, G and g, H and h)
  112. end
  113. tSHA1["bor"] = function(a,b)
  114. local A,B,C,D,E,F,G,H = tSHA1.byte_to_bits(b)
  115. local a,b,c,d,e,f,g,h = tSHA1.byte_to_bits(a)
  116. return tSHA1.bits_to_byte(
  117. A or a, B or b, C or c, D or d,
  118. E or e, F or f, G or g, H or h)
  119. end
  120. tSHA1["bxor"] = function(a,b)
  121. local A,B,C,D,E,F,G,H = tSHA1.byte_to_bits(b)
  122. local a,b,c,d,e,f,g,h = tSHA1.byte_to_bits(a)
  123. return tSHA1.bits_to_byte(
  124. A ~= a, B ~= b, C ~= c, D ~= d,
  125. E ~= e, F ~= f, G ~= g, H ~= h)
  126. end
  127. tSHA1["bnot"] = function(x) return 255-(x % 256) end
  128. tSHA1["w32_comb"] = function(fn)
  129. return function (a,b)
  130. local aa,ab,ac,ad = tSHA1.w32_to_bytes(a)
  131. local ba,bb,bc,bd = tSHA1.w32_to_bytes(b)
  132. return tSHA1.bytes_to_w32(fn(aa,ba),fn(ab,bb),fn(ac,bc),fn(ad,bd))
  133. end
  134. end
  135. tSHA1["w32_xor_n"] = function(a,...)
  136. local aa,ab,ac,ad = tSHA1.w32_to_bytes(a)
  137. for i=1,select('#',...) do
  138. local ba,bb,bc,bd = tSHA1.w32_to_bytes(select(i,...))
  139. aa,ab,ac,ad = tSHA1.bxor(aa,ba),tSHA1.bxor(ab,bb),tSHA1.bxor(ac,bc),tSHA1.bxor(ad,bd)
  140. end
  141. return tSHA1.bytes_to_w32(aa,ab,ac,ad)
  142. end
  143. tSHA1["w32_or3"] = function(a,b,c)
  144. local aa,ab,ac,ad = tSHA1.w32_to_bytes(a)
  145. local ba,bb,bc,bd = tSHA1.w32_to_bytes(b)
  146. local ca,cb,cc,cd = tSHA1.w32_to_bytes(c)
  147. return tSHA1.bytes_to_w32(
  148. tSHA1.bor(aa,tSHA1.bor(ba,ca)), tSHA1.bor(ab,tSHA1.bor(bb,cb)), tSHA1.bor(ac,tSHA1.bor(bc,cc)), tSHA1.bor(ad,tSHA1.bor(bd,cd))
  149. )
  150. end
  151. tSHA1["w32_not"] = function(a) return 4294967295-(a % 4294967296) end
  152. tSHA1["w32_add"] = function(a,b) return (a+b) % 4294967296 end
  153. tSHA1["w32_add_n"] = function(a,...)
  154. for i=1,select('#',...) do
  155. a = (a+select(i,...)) % 4294967296
  156. end
  157. return a
  158. end
  159. tSHA1["w32_to_hexstring"] = function(w) return format("%08x",w) end
  160. tSHA1["w32_and"] = tSHA1.w32_comb(tSHA1.band)
  161. tSHA1["w32_xor"] = tSHA1.w32_comb(tSHA1.bxor)
  162. tSHA1["w32_or"] = tSHA1.w32_comb(tSHA1.bor)
  163.  
  164. local CRC = {}
  165. CRC.crc32 = {
  166. 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F,
  167. 0xE963A535, 0x9E6495A3, 0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988,
  168. 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91, 0x1DB71064, 0x6AB020F2,
  169. 0xF3B97148, 0x84BE41DE, 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7,
  170. 0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC, 0x14015C4F, 0x63066CD9,
  171. 0xFA0F3D63, 0x8D080DF5, 0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172,
  172. 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B, 0x35B5A8FA, 0x42B2986C,
  173. 0xDBBBC9D6, 0xACBCF940, 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59,
  174. 0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116, 0x21B4F4B5, 0x56B3C423,
  175. 0xCFBA9599, 0xB8BDA50F, 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924,
  176. 0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D, 0x76DC4190, 0x01DB7106,
  177. 0x98D220BC, 0xEFD5102A, 0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433,
  178. 0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818, 0x7F6A0DBB, 0x086D3D2D,
  179. 0x91646C97, 0xE6635C01, 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E,
  180. 0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457, 0x65B0D9C6, 0x12B7E950,
  181. 0x8BBEB8EA, 0xFCB9887C, 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65,
  182. 0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2, 0x4ADFA541, 0x3DD895D7,
  183. 0xA4D1C46D, 0xD3D6F4FB, 0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0,
  184. 0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9, 0x5005713C, 0x270241AA,
  185. 0xBE0B1010, 0xC90C2086, 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F,
  186. 0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, 0x59B33D17, 0x2EB40D81,
  187. 0xB7BD5C3B, 0xC0BA6CAD, 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A,
  188. 0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683, 0xE3630B12, 0x94643B84,
  189. 0x0D6D6A3E, 0x7A6A5AA8, 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1,
  190. 0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE, 0xF762575D, 0x806567CB,
  191. 0x196C3671, 0x6E6B06E7, 0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC,
  192. 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5, 0xD6D6A3E8, 0xA1D1937E,
  193. 0x38D8C2C4, 0x4FDFF252, 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B,
  194. 0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60, 0xDF60EFC3, 0xA867DF55,
  195. 0x316E8EEF, 0x4669BE79, 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236,
  196. 0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F, 0xC5BA3BBE, 0xB2BD0B28,
  197. 0x2BB45A92, 0x5CB36A04, 0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D,
  198. 0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A, 0x9C0906A9, 0xEB0E363F,
  199. 0x72076785, 0x05005713, 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38,
  200. 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21, 0x86D3D2D4, 0xF1D4E242,
  201. 0x68DDB3F8, 0x1FDA836E, 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777,
  202. 0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C, 0x8F659EFF, 0xF862AE69,
  203. 0x616BFFD3, 0x166CCF45, 0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2,
  204. 0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB, 0xAED16A4A, 0xD9D65ADC,
  205. 0x40DF0B66, 0x37D83BF0, 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9,
  206. 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, 0xBAD03605, 0xCDD70693,
  207. 0x54DE5729, 0x23D967BF, 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94,
  208. 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D }
  209.  
  210. local bit = {}
  211. bit["bnot"] = function(n)
  212. local tbl = bit.tobits(n)
  213. local size = math.max(table.getn(tbl), 32)
  214. for i = 1, size do
  215. if(tbl[i] == 1) then
  216. tbl[i] = 0
  217. else
  218. tbl[i] = 1
  219. end
  220. end
  221. return bit.tonumb(tbl)
  222. end
  223. bit["band"] = function(m, n)
  224. local tbl_m = bit.tobits(m)
  225. local tbl_n = bit.tobits(n)
  226. bit.expand(tbl_m, tbl_n)
  227. local tbl = {}
  228. local rslt = math.max(table.getn(tbl_m), table.getn(tbl_n))
  229. for i = 1, rslt do
  230. if(tbl_m[i]== 0 or tbl_n[i] == 0) then
  231. tbl[i] = 0
  232. else
  233. tbl[i] = 1
  234. end
  235. end
  236. return bit.tonumb(tbl)
  237. end
  238. bit["bor"] = function(m, n)
  239. local tbl_m = bit.tobits(m)
  240. local tbl_n = bit.tobits(n)
  241. bit.expand(tbl_m, tbl_n)
  242. local tbl = {}
  243. local rslt = math.max(table.getn(tbl_m), table.getn(tbl_n))
  244. for i = 1, rslt do
  245. if(tbl_m[i]== 0 and tbl_n[i] == 0) then
  246. tbl[i] = 0
  247. else
  248. tbl[i] = 1
  249. end
  250. end
  251. return bit.tonumb(tbl)
  252. end
  253. bit["bxor"] = function(m, n)
  254. local tbl_m = bit.tobits(m)
  255. local tbl_n = bit.tobits(n)
  256. bit.expand(tbl_m, tbl_n)
  257. local tbl = {}
  258. local rslt = math.max(table.getn(tbl_m), table.getn(tbl_n))
  259. for i = 1, rslt do
  260. if(tbl_m[i] ~= tbl_n[i]) then
  261. tbl[i] = 1
  262. else
  263. tbl[i] = 0
  264. end
  265. end
  266. return bit.tonumb(tbl)
  267. end
  268. bit["brshift"] = function(n, bits)
  269. bit.checkint(n)
  270. local high_bit = 0
  271. if(n < 0) then
  272. n = bit.bnot(math.abs(n)) + 1
  273. high_bit = 2147483648
  274. end
  275. for i=1, bits do
  276. n = n/2
  277. n = bit.bor(math.floor(n), high_bit)
  278. end
  279. return math.floor(n)
  280. end
  281. bit["blshift"] = function(n, bits)
  282. bit.checkint(n)
  283. if(n < 0) then
  284. n = bit.bnot(math.abs(n)) + 1
  285. end
  286. for i=1, bits do
  287. n = n*2
  288. end
  289. return bit.band(n, 4294967295)
  290. end
  291. bit["bxor2"] = function(m, n)
  292. local rhs = bit.bor(bit.bnot(m), bit.bnot(n))
  293. local lhs = bit.bor(m, n)
  294. local rslt = bit.band(lhs, rhs)
  295. return rslt
  296. end
  297. bit["blogic_rshift"] = function(n, bits)
  298. bit.checkint(n)
  299. if(n < 0) then
  300. n = bit.bnot(math.abs(n)) + 1
  301. end
  302. for i=1, bits do
  303. n = n/2
  304. end
  305. return math.floor(n)
  306. end
  307. bit["tobits"] = function(n)
  308. bit.checkint(n)
  309. if(n < 0) then
  310. return bit.tobits(bit.bnot(math.abs(n)) + 1)
  311. end
  312. local tbl = {}
  313. local cnt = 1
  314. while (n > 0) do
  315. local last = math.fmod(n,2)
  316. if(last == 1) then
  317. tbl[cnt] = 1
  318. else
  319. tbl[cnt] = 0
  320. end
  321. n = (n-last)/2
  322. cnt = cnt + 1
  323. end
  324. return tbl
  325. end
  326. bit["tonumb"] = function(tbl)
  327. local n = table.getn(tbl)
  328. local rslt = 0
  329. local power = 1
  330. for i = 1, n do
  331. rslt = rslt + tbl[i]*power
  332. power = power*2
  333. end
  334. return rslt
  335. end
  336. bit["checkint"] = function(n)
  337. if(n - math.floor(n) > 0) then
  338. error("trying to use bitwise operation on non-integer!")
  339. end
  340. end
  341. bit["expand"] = function(tbl_m, tbl_n)
  342. local big = {}
  343. local small = {}
  344. if(table.getn(tbl_m) > table.getn(tbl_n)) then
  345. big = tbl_m
  346. small = tbl_n
  347. else
  348. big = tbl_n
  349. small = tbl_m
  350. end
  351. for i = table.getn(small) + 1, table.getn(big) do
  352. small[i] = 0
  353. end
  354. end
  355.  
  356. local FCS = {}
  357. FCS["16"] = {
  358. [0]=0, 4489, 8978, 12955, 17956, 22445, 25910, 29887,
  359. 35912, 40385, 44890, 48851, 51820, 56293, 59774, 63735,
  360. 4225, 264, 13203, 8730, 22181, 18220, 30135, 25662,
  361. 40137, 36160, 49115, 44626, 56045, 52068, 63999, 59510,
  362. 8450, 12427, 528, 5017, 26406, 30383, 17460, 21949,
  363. 44362, 48323, 36440, 40913, 60270, 64231, 51324, 55797,
  364. 12675, 8202, 4753, 792, 30631, 26158, 21685, 17724,
  365. 48587, 44098, 40665, 36688, 64495, 60006, 55549, 51572,
  366. 16900, 21389, 24854, 28831, 1056, 5545, 10034, 14011,
  367. 52812, 57285, 60766, 64727, 34920, 39393, 43898, 47859,
  368. 21125, 17164, 29079, 24606, 5281, 1320, 14259, 9786,
  369. 57037, 53060, 64991, 60502, 39145, 35168, 48123, 43634,
  370. 25350, 29327, 16404, 20893, 9506, 13483, 1584, 6073,
  371. 61262, 65223, 52316, 56789, 43370, 47331, 35448, 39921,
  372. 29575, 25102, 20629, 16668, 13731, 9258, 5809, 1848,
  373. 65487, 60998, 56541, 52564, 47595, 43106, 39673, 35696,
  374. 33800, 38273, 42778, 46739, 49708, 54181, 57662, 61623,
  375. 2112, 6601, 11090, 15067, 20068, 24557, 28022, 31999,
  376. 38025, 34048, 47003, 42514, 53933, 49956, 61887, 57398,
  377. 6337, 2376, 15315, 10842, 24293, 20332, 32247, 27774,
  378. 42250, 46211, 34328, 38801, 58158, 62119, 49212, 53685,
  379. 10562, 14539, 2640, 7129, 28518, 32495, 19572, 24061,
  380. 46475, 41986, 38553, 34576, 62383, 57894, 53437, 49460,
  381. 14787, 10314, 6865, 2904, 32743, 28270, 23797, 19836,
  382. 50700, 55173, 58654, 62615, 32808, 37281, 41786, 45747,
  383. 19012, 23501, 26966, 30943, 3168, 7657, 12146, 16123,
  384. 54925, 50948, 62879, 58390, 37033, 33056, 46011, 41522,
  385. 23237, 19276, 31191, 26718, 7393, 3432, 16371, 11898,
  386. 59150, 63111, 50204, 54677, 41258, 45219, 33336, 37809,
  387. 27462, 31439, 18516, 23005, 11618, 15595, 3696, 8185,
  388. 63375, 58886, 54429, 50452, 45483, 40994, 37561, 33584,
  389. 31687, 27214, 22741, 18780, 15843, 11370, 7921, 3960 }
  390. FCS["32"] = {
  391. [0]=0, 1996959894, -301047508, -1727442502, 124634137, 1886057615, -379345611, -1637575261,
  392. 249268274, 2044508324, -522852066, -1747789432, 162941995, 2125561021, -407360249, -1866523247,
  393. 498536548, 1789927666, -205950648, -2067906082, 450548861, 1843258603, -187386543, -2083289657,
  394. 325883990, 1684777152, -43845254, -1973040660, 335633487, 1661365465, -99664541, -1928851979,
  395. 997073096, 1281953886, -715111964, -1570279054, 1006888145, 1258607687, -770865667, -1526024853,
  396. 901097722, 1119000684, -608450090, -1396901568, 853044451, 1172266101, -589951537, -1412350631,
  397. 651767980, 1373503546, -925412992, -1076862698, 565507253, 1454621731, -809855591, -1195530993,
  398. 671266974, 1594198024, -972236366, -1324619484, 795835527, 1483230225, -1050600021, -1234817731,
  399. 1994146192, 31158534, -1731059524, -271249366, 1907459465, 112637215, -1614814043, -390540237,
  400. 2013776290, 251722036, -1777751922, -519137256, 2137656763, 141376813, -1855689577, -429695999,
  401. 1802195444, 476864866, -2056965928, -228458418, 1812370925, 453092731, -2113342271, -183516073,
  402. 1706088902, 314042704, -1950435094, -54949764, 1658658271, 366619977, -1932296973, -69972891,
  403. 1303535960, 984961486, -1547960204, -725929758, 1256170817, 1037604311, -1529756563, -740887301,
  404. 1131014506, 879679996, -1385723834, -631195440, 1141124467, 855842277, -1442165665, -586318647,
  405. 1342533948, 654459306, -1106571248, -921952122, 1466479909, 544179635, -1184443383, -832445281,
  406. 1591671054, 702138776, -1328506846, -942167884, 1504918807, 783551873, -1212326853, -1061524307,
  407. -306674912, -1698712650, 62317068, 1957810842, -355121351, -1647151185, 81470997, 1943803523,
  408. -480048366, -1805370492, 225274430, 2053790376, -468791541, -1828061283, 167816743, 2097651377,
  409. -267414716, -2029476910, 503444072, 1762050814, -144550051, -2140837941, 426522225, 1852507879,
  410. -19653770, -1982649376, 282753626, 1742555852, -105259153, -1900089351, 397917763, 1622183637,
  411. -690576408, -1580100738, 953729732, 1340076626, -776247311, -1497606297, 1068828381, 1219638859,
  412. -670225446, -1358292148, 906185462, 1090812512, -547295293, -1469587627, 829329135, 1181335161,
  413. -882789492, -1134132454, 628085408, 1382605366, -871598187, -1156888829, 570562233, 1426400815,
  414. -977650754, -1296233688, 733239954, 1555261956, -1026031705, -1244606671, 752459403, 1541320221,
  415. -1687895376, -328994266, 1969922972, 40735498, -1677130071, -351390145, 1913087877, 83908371,
  416. -1782625662, -491226604, 2075208622, 213261112, -1831694693, -438977011, 2094854071, 198958881,
  417. -2032938284, -237706686, 1759359992, 534414190, -2118248755, -155638181, 1873836001, 414664567,
  418. -2012718362, -15766928, 1711684554, 285281116, -1889165569, -127750551, 1634467795, 376229701,
  419. -1609899400, -686959890, 1308918612, 956543938, -1486412191, -799009033, 1231636301, 1047427035,
  420. -1362007478, -640263460, 1088359270, 936918000, -1447252397, -558129467, 1202900863, 817233897,
  421. -1111625188, -893730166, 1404277552, 615818150, -1160759803, -841546093, 1423857449, 601450431,
  422. -1285129682, -1000256840, 1567103746, 711928724, -1274298825, -1022587231, 1510334235, 755167117 }
  423.  
  424. --String Utils :
  425.  
  426. function toCharTable(str) --Returns table of @str's chars
  427. if not str then return nil end
  428. str = tostring(str)
  429. local chars = {}
  430. for n=1,#str do
  431. chars[n] = str:sub(n,n)
  432. end
  433. return chars
  434. end
  435.  
  436. function toByteTable(str) --Returns table of @str's bytes
  437. if not str then return nil end
  438. str = tostring(str)
  439. local bytes = {}
  440. for n=1,#str do
  441. bytes[n] = str:byte(n)
  442. end
  443. return bytes
  444. end
  445.  
  446. function fromCharTable(chars) --Returns string made of chracters in @chars
  447. if not chars or type(chars)~="table" then return nil end
  448. return table.concat(chars)
  449. end
  450.  
  451. function fromByteTable(bytes) --Returns string made of bytes in @bytes
  452. if not bytes or type(bytes)~="table" then return nil end
  453. local str = ""
  454. for n=1,#bytes do
  455. str = str..string.char(bytes[n])
  456. end
  457. return str
  458. end
  459.  
  460. function contains(str,find) --Returns true if @str contains @find
  461. if not str then return nil end
  462. str = tostring(str)
  463. for n=1, #str-#find+1 do
  464. if str:sub(n,n+#find-1) == find then return true end
  465. end
  466. return false
  467. end
  468.  
  469. function startsWith(str,Start) --Check if @str starts with @Start
  470. if not str then return nil end
  471. str = tostring(str)
  472. return str:sub(1,Start:len())==Start
  473. end
  474.  
  475. function endsWith(str,End) --Check if @str ends with @End
  476. if not str then return nil end
  477. str = tostring(str)
  478. return End=='' or str:sub(#str-#End+1)==End
  479. end
  480.  
  481. function trim(str) --Trim @str of initial/trailing whitespace
  482. if not str then return nil end
  483. str = tostring(str)
  484. return (str:gsub("^%s*(.-)%s*$", "%1"))
  485. end
  486.  
  487. function firstLetterUpper(str) --Capitilizes first letter of @str
  488. if not str then return nil end
  489. str = tostring(str)
  490. str = str:gsub("%a", string.upper, 1)
  491. return str
  492. end
  493.  
  494. function titleCase(str) --Changes @str to title case
  495. if not str then return nil end
  496. str = tostring(str)
  497. local function tchelper(first, rest)
  498. return first:upper()..rest:lower()
  499. end
  500. str = str:gsub("(%a)([%w_']*)", tchelper)
  501. return str
  502. end
  503.  
  504. function isRepetition(str, pat) --Checks if @str is a repetition of @pat
  505. if not str then return nil end
  506. str = tostring(str)
  507. return "" == str:gsub(pat, "")
  508. end
  509.  
  510. function isRepetitionWS(str, pat) --Checks if @str is a repetition of @pat seperated by whitespaces
  511. if not str then return nil end
  512. str = tostring(str)
  513. return not str:gsub(pat, ""):find"%S"
  514. end
  515.  
  516. function urlDecode(str) --Url decodes @str
  517. if not str then return nil end
  518. str = tostring(str)
  519. str = string.gsub (str, "+", " ")
  520. str = string.gsub (str, "%%(%x%x)", function(h) return string.char(tonumber(h,16)) end)
  521. str = string.gsub (str, "\r\n", "\n")
  522. return str
  523. end
  524.  
  525. function urlEncode(str) --Url encodes @str
  526. if not str then return nil end
  527. str = tostring(str)
  528. if (str) then
  529. str = string.gsub (str, "\n", "\r\n")
  530. str = string.gsub (str, "([^%w ])", function (c) return string.format ("%%%02X", string.byte(c)) end)
  531. str = string.gsub (str, " ", "+")
  532. end
  533. return str
  534. end
  535.  
  536. function isEmailAddress(str) --Checks if @str is a valid email address
  537. if not str then return nil end
  538. str = tostring(str)
  539. if (str:match("[A-Za-z0-9%.%%%+%-]+@[A-Za-z0-9%.%%%+%-]+%.%w%w%w?%w?")) then
  540. return true
  541. else
  542. return false
  543. end
  544. end
  545.  
  546. function chunk(str, size) --Splits @str into chunks of length @size
  547. if not size then return nil end
  548. str = tostring(str)
  549. local num2App = size - (#str%size)
  550. str = str..(rep(char(0), num2App) or "")
  551. assert(#str%size==0)
  552. local chunks = {}
  553. local numChunks = #str / size
  554. local chunk = 0
  555. while chunk < numChunks do
  556. local start = chunk * size + 1
  557. chunk = chunk+1
  558. if start+size-1 > #str-num2App then
  559. if str:sub(start, #str-num2App) ~= (nil or "") then
  560. chunks[chunk] = str:sub(start, #str-num2App)
  561. end
  562. else
  563. chunks[chunk] = str:sub(start, start+size-1)
  564. end
  565. end
  566. return chunks
  567. end
  568.  
  569. function find(str, match, startIndex) --Finds @match in @str optionally after @startIndex
  570. if not match then return nil end
  571. str = tostring(str)
  572. local _ = startIndex or 1
  573. local _s = nil
  574. local _e = nil
  575. local _len = match:len()
  576. while true do
  577. local _t = str:sub( _ , _len + _ - 1)
  578. if _t == match then
  579. _s = _
  580. _e = _ + _len - 1
  581. break
  582. end
  583. _ = _ + 1
  584. if _ > str:len() then break end
  585. end
  586. if _s == nil then return nil else return _s, _e end
  587. end
  588.  
  589. function seperate(str, divider) --Separates @str on @divider
  590. if not divider then return nil end
  591. str = tostring(str)
  592. local start = {}
  593. local endS = {}
  594. local n=1
  595. repeat
  596. if n==1 then
  597. start[n], endS[n] = find(str, divider)
  598. else
  599. start[n], endS[n] = find(str, divider, endS[n-1]+1)
  600. end
  601. n=n+1
  602. until start[n-1]==nil
  603. local subs = {}
  604. for n=1, #start+1 do
  605. if n==1 then
  606. subs[n] = str:sub(1, start[n]-1)
  607. elseif n==#start+1 then
  608. subs[n] = str:sub(endS[n-1]+1)
  609. else
  610. subs[n] = str:sub(endS[n-1]+1, start[n]-1)
  611. end
  612. end
  613. return subs
  614. end
  615.  
  616. function replace(str, from, to) --Replaces @from to @to in @str
  617. if not from then return nil end
  618. str = tostring(str)
  619. local pcs = seperate(str, from)
  620. str = pcs[1]
  621. for n=2,#pcs do
  622. str = str..to..pcs[n]
  623. end
  624. return str
  625. end
  626.  
  627. function jumble(str) --Jumbles @str
  628. if not str then return nil end
  629. str = tostring(str)
  630. local chars = {}
  631. for i = 1, #str do
  632. chars[i] = str:sub(i, i)
  633. end
  634. local usedNums = ":"
  635. local res = ""
  636. local rand = 0
  637. for i=1, #chars do
  638. while true do
  639. rand = math.random(#chars)
  640. if find(usedNums, ":"..rand..":") == nil then break end
  641. end
  642. res = res..chars[rand]
  643. usedNums = usedNums..rand..":"
  644. end
  645. return res
  646. end
  647.  
  648. function toBase(str, base) --Encodes @str in @base
  649. if not base then return nil end
  650. str = tostring(str)
  651. local res = ""
  652. for i = 1, str:len() do
  653. if i == 1 then
  654. res = basen(str:byte(i), base)
  655. else
  656. res = res..":"..basen(str:byte(i), base)
  657. end
  658. end
  659. return res
  660. end
  661.  
  662. function fromBase(str, base) --Decodes @str from @base
  663. if not base then return nil end
  664. str = tostring(str)
  665. local bytes = seperate(str, ":")
  666. local res = ""
  667. for i = 1, #bytes do
  668. res = res..(string.char(basen(tonumber(bytes[i], base), 10)))
  669. end
  670. return res
  671. end
  672.  
  673. function toBinary(str) --Encodes @str in binary
  674. if not str then return nil end
  675. str = tostring(str)
  676. return toBase(str, 2)
  677. end
  678.  
  679. function fromBinary(str) --Decodes @str from binary
  680. if not str then return nil end
  681. str = tostring(str)
  682. return fromBase(str, 2)
  683. end
  684.  
  685. function toOctal(str) --Encodes @str in octal
  686. if not str then return nil end
  687. str = tostring(str)
  688. return toBase(str, 8)
  689. end
  690.  
  691. function fromOctal(str) --Decodes @str from octal
  692. if not str then return nil end
  693. str = tostring(str)
  694. return fromBase(str, 8)
  695. end
  696.  
  697. function toHex(str) --Encodes @str in hex
  698. if not str then return nil end
  699. str = tostring(str)
  700. return toBase(str, 16)
  701. end
  702.  
  703. function fromHex(str) --Decodes @str from hex
  704. if not str then return nil end
  705. str = tostring(str)
  706. return fromBase(str, 16)
  707. end
  708.  
  709. function toBase36(str) --Encodes @str in Base36
  710. if not str then return nil end
  711. str = tostring(str)
  712. return toBase(str, 36)
  713. end
  714.  
  715. function fromBase36(str) --Decodes @str from Base36
  716. if not str then return nil end
  717. str = tostring(str)
  718. return fromBase(str, 36)
  719. end
  720.  
  721. function toBase32(str) --Encodes @str in Base32
  722. if not str then return nil end
  723. str = tostring(str)
  724. local byte=0
  725. local bits=0
  726. local rez=""
  727. local i=0
  728. for i = 1, str:len() do
  729. byte=byte*256+str:byte(i)
  730. bits=bits+8
  731. repeat
  732. bits=bits-5
  733. local mul=(2^(bits))
  734. local b32n=math.floor(byte/mul)
  735. byte=byte-(b32n*mul)
  736. b32n=b32n+1
  737. rez=rez..string.sub(base32,b32n,b32n)
  738. until bits<5
  739. end
  740. if bits>0 then
  741. local b32n= math.fmod(byte*(2^(5-bits)),32)
  742. b32n=b32n+1
  743. rez=rez..string.sub(base32,b32n,b32n)
  744. end
  745. return rez
  746. end
  747.  
  748. function fromBase32(str) --Decodes @str from Base32
  749. if not str then return nil end
  750. str = tostring(str)
  751. local b32n=0
  752. local bits=0
  753. local rez=""
  754. local i=0
  755. string.gsub(str:upper(), "["..base32.."]", function (char)
  756. local num = string.find(base32, char, 1, true)
  757. b32n=b32n*32+(num - 1)
  758. bits=bits+5
  759. while bits>=8 do
  760. bits=bits-8
  761. local mul=(2^(bits))
  762. local byte = math.floor(b32n/mul)
  763. b32n=b32n-(byte*mul)
  764. rez=rez..string.char(byte)
  765. end
  766. end)
  767. return rez
  768. end
  769.  
  770. function toBase64(str) --Encodes @str in Base64
  771. if not str then return nil end
  772. str = tostring(str)
  773. local bytes = {}
  774. local result = ""
  775. for spos=0,str:len()-1,3 do
  776. for byte=1,3 do bytes[byte] = str:byte(spos+byte) or 0 end
  777. result = string.format('%s%s%s%s%s',result,Base64.base64chars[Base64.rsh(bytes[1],2)],Base64.base64chars[Base64.lor(Base64.lsh((bytes[1] % 4),4), Base64.rsh(bytes[2],4))] or "=",((str:len()-spos) > 1) and Base64.base64chars[Base64.lor(Base64.lsh(bytes[2] % 16,2), Base64.rsh(bytes[3],6))] or "=",((str:len()-spos) > 2) and Base64.base64chars[(bytes[3] % 64)] or "=")
  778. end
  779. return result
  780. end
  781.  
  782. function fromBase64(str) --Decodes @str from Base64
  783. if not str then return nil end
  784. str = tostring(str)
  785. local chars = {}
  786. local result=""
  787. for dpos=0,str:len()-1,4 do
  788. for char=1,4 do chars[char] = Base64.base64bytes[(str:sub((dpos+char),(dpos+char)) or "=")] end
  789. result = string.format('%s%s%s%s',result,string.char(Base64.lor(Base64.lsh(chars[1],2), Base64.rsh(chars[2],4))),(chars[3] ~= nil) and string.char(Base64.lor(Base64.lsh(chars[2],4), Base64.rsh(chars[3],2))) or "",(chars[4] ~= nil) and string.char(Base64.lor(Base64.lsh(chars[3],6) % 192, (chars[4]))) or "")
  790. end
  791. return result
  792. end
  793.  
  794. function rot13(str) --Rot13s @str
  795. if not str then return nil end
  796. str = tostring(str)
  797. local rot = ""
  798. local len = str:len()
  799. for i = 1, len do
  800. local k = str:byte(i)
  801. if (k >= 65 and k <= 77) or (k >= 97 and k <=109) then
  802. rot = rot..string.char(k+13)
  803. elseif (k >= 78 and k <= 90) or (k >= 110 and k <= 122) then
  804. rot = rot..string.char(k-13)
  805. else
  806. rot = rot..string.char(k)
  807. end
  808. end
  809. return rot
  810. end
  811.  
  812. function rot47(str) --Rot47s @str
  813. if not str then return nil end
  814. str = tostring(str)
  815. local rot = ""
  816. for i = 1, str:len() do
  817. local p = str:byte(i)
  818. if p >= string.byte('!') and p <= string.byte('O') then
  819. p = ((p + 47) % 127)
  820. elseif p >= string.byte('P') and p <= string.byte('~') then
  821. p = ((p - 47) % 127)
  822. end
  823. rot = rot..string.char(p)
  824. end
  825. return rot
  826. end
  827.  
  828. function SHA1(str) --Returns SHA1 Hash of @str
  829. if not str then return nil end
  830. str = tostring(str)
  831. local H0,H1,H2,H3,H4 = 0x67452301,0xEFCDAB89,0x98BADCFE,0x10325476,0xC3D2E1F0
  832. local msg_len_in_bits = #str * 8
  833. local first_append = char(0x80)
  834. local non_zero_message_bytes = #str +1 +8
  835. local current_mod = non_zero_message_bytes % 64
  836. local second_append = current_mod>0 and rep(char(0), 64 - current_mod) or ""
  837. local B1, R1 = modf(msg_len_in_bits / 0x01000000)
  838. local B2, R2 = modf( 0x01000000 * R1 / 0x00010000)
  839. local B3, R3 = modf( 0x00010000 * R2 / 0x00000100)
  840. local B4 = 0x00000100 * R3
  841. local L64 = char( 0) .. char( 0) .. char( 0) .. char( 0)
  842. .. char(B1) .. char(B2) .. char(B3) .. char(B4)
  843. str = str .. first_append .. second_append .. L64
  844. assert(#str % 64 == 0)
  845. local chunks = #str / 64
  846. local W = { }
  847. local start, A, B, C, D, E, f, K, TEMP
  848. local chunk = 0
  849. while chunk < chunks do
  850. start,chunk = chunk * 64 + 1,chunk + 1
  851. for t = 0, 15 do
  852. W[t] = tSHA1.bytes_to_w32(str:byte(start, start + 3))
  853. start = start + 4
  854. end
  855. for t = 16, 79 do
  856. W[t] = tSHA1.w32_rot(1, tSHA1.w32_xor_n(W[t-3], W[t-8], W[t-14], W[t-16]))
  857. end
  858. A,B,C,D,E = H0,H1,H2,H3,H4
  859. for t = 0, 79 do
  860. if t <= 19 then
  861. f = tSHA1.w32_or(tSHA1.w32_and(B, C), tSHA1.w32_and(tSHA1.w32_not(B), D))
  862. K = 0x5A827999
  863. elseif t <= 39 then
  864. f = tSHA1.w32_xor_n(B, C, D)
  865. K = 0x6ED9EBA1
  866. elseif t <= 59 then
  867. f = tSHA1.w32_or3(tSHA1.w32_and(B, C), tSHA1.w32_and(B, D), tSHA1.w32_and(C, D))
  868. K = 0x8F1BBCDC
  869. else
  870. f = tSHA1.w32_xor_n(B, C, D)
  871. K = 0xCA62C1D6
  872. end
  873. A,B,C,D,E = tSHA1.w32_add_n(tSHA1.w32_rot(5, A), f, E, W[t], K),
  874. A, tSHA1.w32_rot(30, B), C, D
  875. end
  876. H0,H1,H2,H3,H4 = tSHA1.w32_add(H0, A),tSHA1.w32_add(H1, B),tSHA1.w32_add(H2, C),tSHA1.w32_add(H3, D),tSHA1.w32_add(H4, E)
  877. end
  878. local f = tSHA1.w32_to_hexstring
  879. return f(H0) .. f(H1) .. f(H2) .. f(H3) .. f(H4)
  880. end
  881.  
  882. function CRC32(str) --Returns CRC32 Hash of @str
  883. local crc, l, i = 0xFFFFFFFF, string.len(str)
  884. for i = 1, l, 1 do
  885. crc = bit.bxor(bit.brshift(crc, 8), CRC.crc32[bit.band(bit.bxor(crc, string.byte(str, i)), 0xFF) + 1])
  886. end
  887. return bit.bxor(crc, -1)
  888. end
  889.  
  890. function FCS16(str) --Returns FCS16 Hash of @str
  891. local i
  892. local l=string.len(str)
  893. local uFcs16 = 65535
  894. for i = 1,l do
  895. uFcs16 = bit.bxor(bit.brshift(uFcs16,8), FCS["16"][bit.band(bit.bxor(uFcs16, string.byte(str,i)), 255)])
  896. end
  897. return bit.bxor(uFcs16, 65535)
  898. end
  899.  
  900. function FCS32(str) --Returns FCS32 Hash of @str
  901. local i
  902. local l = string.len(str)
  903. local uFcs32 = -1
  904. for i=1,l do
  905. uFcs32 = bit.bxor(bit.brshift(uFcs32,8), FCS["32"][bit.band(bit.bxor(uFcs32, string.byte(str,i)), 255)])
  906. end
  907. return bit.bnot(uFcs32)
  908. end
  909.  
  910. function encrypt(str, key) --Encrypts @str with @key
  911. if not key then return nil end
  912. str = tostring(str)
  913. local alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_abcdefghijklmnopqrstuvwxyz{|}~"
  914. local _rand = math.random(#alphabet-10)
  915. local iv = string.sub(jumble(alphabet), _rand, _rand + 9)
  916. iv = jumble(iv)
  917. str = iv..str
  918. local key = SHA1(key)
  919. local strLen = str:len()
  920. local keyLen = key:len()
  921. local j=1
  922. local result = ""
  923. for i=1, strLen do
  924. local ordStr = string.byte(str:sub(i,i))
  925. if j == keyLen then j=1 end
  926. local ordKey = string.byte(key:sub(j,j))
  927. result = result..string.reverse(basen(ordStr+ordKey, 36))
  928. j = j+1
  929. end
  930. return result
  931. end
  932.  
  933. function decrypt(str, key) --Decrypts @str with @key
  934. if not key then return nil end
  935. str = tostring(str)
  936. local key = SHA1(key)
  937. local strLen = str:len()
  938. local keyLen = key:len()
  939. local j=1
  940. local result = ""
  941. for i=1, strLen, 2 do
  942. local ordStr = basen(tonumber(string.reverse(str:sub(i, i+1)),36),10)
  943. if j==keyLen then j=1 end
  944. local ordKey = string.byte(key:sub(j,j))
  945. result = result..string.char(ordStr-ordKey)
  946. j = j+1
  947. end
  948. return result:sub(11)
  949. end
  950.  
  951. function setRandSeed(seed) --Sets random seed to @seed
  952. math.randomseed(seed)
  953. end
  954.  
  955. setRandSeed(os.time())
Add Comment
Please, Sign In to add comment