lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



On Jun 14, 2006, at 17:05, Paul Hudson wrote:

The sl-exporters project on luaforge has an implementation of CRC32

http://luaforge.net/frs/?group_id=69

Cool :)

Here is the gist of it if someone is interested:

local CRC = {}

function CRC:table()
        if self._table == nil then
                local aTable = {}
                local mod = bit.mod
                local xor = bit.bxor
                local rshift = bit.rshift

                for anIndex = 0, 255 do
                        local aValue = anIndex

                        for _ = 1, 8 do
                                if mod( aValue, 2 ) == 1 then
aValue = xor( 0xEDB88320, rshift( aValue, 1 ) )
                                else
                                        aValue = rshift( aValue, 1 )
                                end
                        end

                        aTable[ anIndex ] = aValue
                end

                self._table = aTable
        end

        return self._table
end

function CRC:hash( aValue, aStart, anEnd )
        local aTable = self:table()
        local anHash = 0xFFFFFFFF
        local band = bit.band
        local xor = bit.bxor
        local rshift = bit.rshift

        for anIndex = ( aStart or 1 ), ( anEnd or aValue:len() ) do
anHash = xor( aTable[ band( xor( anHash, aValue:byte( anIndex ) ), 255 ) ], rshift( anHash, 8 ) )
        end

        return xor( anHash, 0xFFFFFFFF )
end

It requires Reuben Thomas's bitlib module:

http://rrt.sc3d.org/Software/Lua/?page=Software/Lua

Cheers

--
PA, Onnay Equitursay
http://alt.textdrive.com/