PageRenderTime 26ms CodeModel.GetById 2ms RepoModel.GetById 1ms app.codeStats 0ms

/CRC32.lua

http://luamemcached.googlecode.com/
Lua | 173 lines | 116 code | 21 blank | 36 comment | 9 complexity | 8d83893566c6895730d78f8d26dc3df6 MD5 | raw file
  1. --Copyright (c) 2007-2008 Neil Richardson (nrich@iinet.net.au)
  2. --
  3. --Permission is hereby granted, free of charge, to any person obtaining a copy
  4. --of this software and associated documentation files (the "Software"), to deal
  5. --in the Software without restriction, including without limitation the rights
  6. --to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. --copies of the Software, and to permit persons to whom the Software is
  8. --furnished to do so, subject to the following conditions:
  9. --
  10. --The above copyright notice and this permission notice shall be included in all
  11. --copies or substantial portions of the Software.
  12. --
  13. --THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. --IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. --FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. --AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. --LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. --OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  19. --IN THE SOFTWARE.
  20. module('CRC32', package.seeall)
  21. local max = 2^32 -1
  22. local CRC32 = {
  23. 0,79764919,159529838,222504665,319059676,
  24. 398814059,445009330,507990021,638119352,
  25. 583659535,797628118,726387553,890018660,
  26. 835552979,1015980042,944750013,1276238704,
  27. 1221641927,1167319070,1095957929,1595256236,
  28. 1540665371,1452775106,1381403509,1780037320,
  29. 1859660671,1671105958,1733955601,2031960084,
  30. 2111593891,1889500026,1952343757,2552477408,
  31. 2632100695,2443283854,2506133561,2334638140,
  32. 2414271883,2191915858,2254759653,3190512472,
  33. 3135915759,3081330742,3009969537,2905550212,
  34. 2850959411,2762807018,2691435357,3560074640,
  35. 3505614887,3719321342,3648080713,3342211916,
  36. 3287746299,3467911202,3396681109,4063920168,
  37. 4143685023,4223187782,4286162673,3779000052,
  38. 3858754371,3904687514,3967668269,881225847,
  39. 809987520,1023691545,969234094,662832811,
  40. 591600412,771767749,717299826,311336399,
  41. 374308984,453813921,533576470,25881363,
  42. 88864420,134795389,214552010,2023205639,
  43. 2086057648,1897238633,1976864222,1804852699,
  44. 1867694188,1645340341,1724971778,1587496639,
  45. 1516133128,1461550545,1406951526,1302016099,
  46. 1230646740,1142491917,1087903418,2896545431,
  47. 2825181984,2770861561,2716262478,3215044683,
  48. 3143675388,3055782693,3001194130,2326604591,
  49. 2389456536,2200899649,2280525302,2578013683,
  50. 2640855108,2418763421,2498394922,3769900519,
  51. 3832873040,3912640137,3992402750,4088425275,
  52. 4151408268,4197601365,4277358050,3334271071,
  53. 3263032808,3476998961,3422541446,3585640067,
  54. 3514407732,3694837229,3640369242,1762451694,
  55. 1842216281,1619975040,1682949687,2047383090,
  56. 2127137669,1938468188,2001449195,1325665622,
  57. 1271206113,1183200824,1111960463,1543535498,
  58. 1489069629,1434599652,1363369299,622672798,
  59. 568075817,748617968,677256519,907627842,
  60. 853037301,1067152940,995781531,51762726,
  61. 131386257,177728840,240578815,269590778,
  62. 349224269,429104020,491947555,4046411278,
  63. 4126034873,4172115296,4234965207,3794477266,
  64. 3874110821,3953728444,4016571915,3609705398,
  65. 3555108353,3735388376,3664026991,3290680682,
  66. 3236090077,3449943556,3378572211,3174993278,
  67. 3120533705,3032266256,2961025959,2923101090,
  68. 2868635157,2813903052,2742672763,2604032198,
  69. 2683796849,2461293480,2524268063,2284983834,
  70. 2364738477,2175806836,2238787779,1569362073,
  71. 1498123566,1409854455,1355396672,1317987909,
  72. 1246755826,1192025387,1137557660,2072149281,
  73. 2135122070,1912620623,1992383480,1753615357,
  74. 1816598090,1627664531,1707420964,295390185,
  75. 358241886,404320391,483945776,43990325,
  76. 106832002,186451547,266083308,932423249,
  77. 861060070,1041341759,986742920,613929101,
  78. 542559546,756411363,701822548,3316196985,
  79. 3244833742,3425377559,3370778784,3601682597,
  80. 3530312978,3744426955,3689838204,3819031489,
  81. 3881883254,3928223919,4007849240,4037393693,
  82. 4100235434,4180117107,4259748804,2310601993,
  83. 2373574846,2151335527,2231098320,2596047829,
  84. 2659030626,2470359227,2550115596,2947551409,
  85. 2876312838,2788305887,2733848168,3165939309,
  86. 3094707162,3040238851,2985771188,
  87. }
  88. local function xor(a, b)
  89. local calc = 0
  90. for i = 32, 0, -1 do
  91. local val = 2 ^ i
  92. local aa = false
  93. local bb = false
  94. if a == 0 then
  95. calc = calc + b
  96. break
  97. end
  98. if b == 0 then
  99. calc = calc + a
  100. break
  101. end
  102. if a >= val then
  103. aa = true
  104. a = a - val
  105. end
  106. if b >= val then
  107. bb = true
  108. b = b - val
  109. end
  110. if not (aa and bb) and (aa or bb) then
  111. calc = calc + val
  112. end
  113. end
  114. return calc
  115. end
  116. local function lshift(num, left)
  117. local res = num * (2 ^ left)
  118. return res % (2 ^ 32)
  119. end
  120. local function rshift(num, right)
  121. local res = num / (2 ^ right)
  122. return math.floor(res)
  123. end
  124. function Hash(str)
  125. local count = string.len(tostring(str))
  126. local crc = max
  127. local i = 1
  128. while count > 0 do
  129. local byte = string.byte(str, i)
  130. crc = xor(lshift(crc, 8), CRC32[xor(rshift(crc, 24), byte) + 1])
  131. i = i + 1
  132. count = count - 1
  133. end
  134. return crc
  135. end
  136. --
  137. -- CRC32.lua
  138. --
  139. -- A pure Lua implementation of a CRC32 hashing algorithm. Slower than using a C implemtation,
  140. -- but useful having no other dependancies.
  141. --
  142. --
  143. -- Synopsis
  144. --
  145. -- require('CRC32')
  146. --
  147. -- crchash = CRC32.Hash('a string')
  148. --
  149. -- Methods:
  150. --
  151. -- hashval = CRC32.Hash(val)
  152. -- Calculates and returns (as an integer) the CRC32 hash of the parameter 'val'.