Method: Cosmos::Crc#initialize

Defined in:
lib/cosmos/utilities/crc.rb

#initialize(poly, seed, xor, reflect) ⇒ Crc

Creates a CRC algorithm instance.

Parameters:

  • poly (Integer)

    Polynomial to use when calculating the CRC

  • seed (Integer)

    Seed value to start the calculation

  • xor (Boolean)

    Whether to XOR the CRC result with 0xFFFF

  • reflect (Boolean)

    Whether to bit reverse each byte of data before calculating the CRC



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/cosmos/utilities/crc.rb', line 63

def initialize(poly, seed, xor, reflect)
  @poly = poly
  @seed = seed
  @xor = xor
  @reflect = reflect
  if RUBY_ENGINE == 'ruby' and !ENV['COSMOS_NO_EXT']
    @table = ''
  else
    @table = []
  end

  # Determine which class we're using: Crc16, Crc32, Crc64
  @bit_size = self.class.name[-2..-1].to_i
  case @bit_size
  when 16
    pack = 'S'
    filter_mask = 0xFFFF
  when 32
    pack = 'I'
    filter_mask = 0xFFFFFFFF
  when 64
    pack = 'Q'
    filter_mask = 0xFFFFFFFFFFFFFFFF
  end
  if RUBY_ENGINE == 'ruby' and !ENV['COSMOS_NO_EXT']
    (0..255).each do |index|
      @table << [compute_table_entry(index, @bit_size)].pack(pack)
    end
  else
    (0..255).each do |index|
      @table << (compute_table_entry(index, @bit_size) & filter_mask)
    end
  end
end