class CRC
Public Class Methods
new()
click to toggle source
# File lib/futurocube/crc.rb, line 2 def initialize @table = make_table(0x04C11DB7) reset end
Public Instance Methods
crc()
click to toggle source
# File lib/futurocube/crc.rb, line 29 def crc @crc end
reset()
click to toggle source
# File lib/futurocube/crc.rb, line 7 def reset @crc = 0xFFFFFFFF end
update(input)
click to toggle source
# File lib/futurocube/crc.rb, line 11 def update(input) crc = @crc table = @table # Original algorithm does it little-endian then iterates from the high byte. # I read it big-endian and then iterate from the low byte, saving some bit maths. input.unpack('N*').each do |data| 4.times do table_index = (data & 0xFF) ^ (crc >> 24) crc = (crc << 8) & 0xFFFFFFFF crc = crc ^ table[table_index] data >>= 8 end end @crc = crc end
Private Instance Methods
make_table(poly)
click to toggle source
# File lib/futurocube/crc.rb, line 35 def make_table(poly) (0..255).map do |n| crc = n << 24 8.times do if crc & 0x80000000 != 0 crc = ((crc << 1) ^ poly) & 0xFFFFFFFF else crc <<= 1 end end (crc & 0xFFFFFFFF) end end