Skip to content

Instantly share code, notes, and snippets.

@xjdrew
Created January 25, 2014 11:44
Show Gist options
  • Save xjdrew/8615151 to your computer and use it in GitHub Desktop.
Save xjdrew/8615151 to your computer and use it in GitHub Desktop.
生成crc32表
--[[
-- polynomial: 0x104c11db7
-- 根据MSB-first的crc polynomial,生成单字节表
-- http://en.wikipedia.org/wiki/Cyclic_redundancy_check
-- http://www.cnblogs.com/esestt/archive/2007/08/09/848856.html
-- 这个算法对于crc64应该也是一样的
--]]
local polynomial = 0x04c11db7
local length = 32
local t = {}
for i=0, length-1 do
t[i] = bit32.lshift(polynomial, i)
end
local t1 = {}
for i=length-1, 0, -1 do
t1[length-i] = bit32.extract(polynomial, i, length - i)
end
local ret = {}
for i=0, 255 do
local crc = 0
local g = i
for j=7,0,-1 do
if g == 0 then
break
end
if bit32.extract(g, j, 1) == 1 then
g = bit32.replace(g, 0, j, 1)
if j > 0 then
g = bit32.bxor(g, t1[j])
end
crc = bit32.bxor(crc, t[j])
end
end
table.insert(ret, crc)
end
for i=1,#ret do
print(i, string.format("0x%08x", ret[i]))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment