Class: Cosmos::Crc

Inherits:
Object show all
Defined in:
lib/cosmos/utilities/crc.rb,
ext/cosmos/ext/crc/crc.c

Overview

Abstract base class which Crc16, Crc32 and Crc64 use. Do NOT use this class directly but instead use one of the subclasses.

Direct Known Subclasses

Crc16, Crc32, Crc64

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#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



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/cosmos/utilities/crc.rb', line 36

def initialize(poly, seed, xor, reflect)
  @poly = poly
  @seed = seed
  @xor = xor
  @reflect = reflect
  @table = ''

  # Determine which class we're using: Crc16, Crc32, Crc64
  digits = self.class.name[-2..-1].to_i
  case digits
  when 16
    pack = 'S'
  when 32
    pack = 'I'
  when 64
    pack = 'Q'
  end
  (0..255).each do |index|
    @table << [compute_table_entry(index, digits)].pack(pack)
  end
end

Instance Attribute Details

#polyInteger (readonly)

Returns The polynomial used when calcuating the CRC.

Returns:

  • (Integer)

    The polynomial used when calcuating the CRC



19
20
21
# File 'lib/cosmos/utilities/crc.rb', line 19

def poly
  @poly
end

#reflectBoolean (readonly)

Returns Whether to bit reverse each byte.

Returns:

  • (Boolean)

    Whether to bit reverse each byte



25
26
27
# File 'lib/cosmos/utilities/crc.rb', line 25

def reflect
  @reflect
end

#seedInteger (readonly)

Returns Seed value used to start the calulation.

Returns:

  • (Integer)

    Seed value used to start the calulation



21
22
23
# File 'lib/cosmos/utilities/crc.rb', line 21

def seed
  @seed
end

#tableString (readonly)

Returns Binary lookup table used to perform the calculation.

Returns:

  • (String)

    Binary lookup table used to perform the calculation



27
28
29
# File 'lib/cosmos/utilities/crc.rb', line 27

def table
  @table
end

#xorBoolean (readonly)

Returns Whether the result is XORed with 0xFFFF.

Returns:

  • (Boolean)

    Whether the result is XORed with 0xFFFF



23
24
25
# File 'lib/cosmos/utilities/crc.rb', line 23

def xor
  @xor
end