Class: CRC

Inherits:
Object
  • Object
show all
Defined in:
lib/futurocube/crc.rb

Instance Method Summary collapse

Constructor Details

#initializeCRC

Returns a new instance of CRC.



2
3
4
5
# File 'lib/futurocube/crc.rb', line 2

def initialize
  @table = make_table(0x04C11DB7)
  reset
end

Instance Method Details

#crcObject



29
30
31
# File 'lib/futurocube/crc.rb', line 29

def crc
  @crc
end

#resetObject



7
8
9
# File 'lib/futurocube/crc.rb', line 7

def reset
  @crc = 0xFFFFFFFF
end

#update(input) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 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