Class: Omnizip::Checksums::CrcBase Abstract
- Inherits:
-
Object
- Object
- Omnizip::Checksums::CrcBase
- Defined in:
- lib/omnizip/checksums/crc_base.rb
Overview
Subclass and override polynomial-specific methods
Abstract base class for CRC (Cyclic Redundancy Check) algorithms.
This class provides the common framework for implementing CRC checksums using lookup tables for efficient computation. Subclasses must define their specific polynomial and bit width.
The lookup table approach provides O(1) per-byte processing time, making it significantly faster than bit-by-bit calculation.
Architecture:
- Pre-computed lookup tables stored as class constants
- Incremental computation support via update method
- Initial and final XOR values for standard CRC variants
Instance Attribute Summary collapse
-
#value ⇒ Integer
readonly
Current CRC value.
Class Method Summary collapse
-
.calculate(data) ⇒ Integer
Calculate CRC for data in one operation.
-
.generate_table(polynomial, bits) ⇒ Array<Integer>
Generate a CRC lookup table for given polynomial.
-
.lookup_table ⇒ Array<Integer>
abstract
Get the lookup table for this CRC variant.
Instance Method Summary collapse
-
#finalize ⇒ Integer
Get the finalized CRC value.
-
#initialize ⇒ CrcBase
constructor
Initialize a new CRC calculator with default initial value.
-
#reset ⇒ self
Reset the CRC calculator to initial state.
-
#update(data) ⇒ self
Update the CRC value with new data.
Constructor Details
#initialize ⇒ CrcBase
Initialize a new CRC calculator with default initial value.
The initial value is typically all 1s (inverted 0) to detect leading zeros in the input data.
44 45 46 |
# File 'lib/omnizip/checksums/crc_base.rb', line 44 def initialize @value = initial_value end |
Instance Attribute Details
#value ⇒ Integer (readonly)
Returns current CRC value.
38 39 40 |
# File 'lib/omnizip/checksums/crc_base.rb', line 38 def value @value end |
Class Method Details
.calculate(data) ⇒ Integer
Calculate CRC for data in one operation.
This is a convenience method that combines initialize, update, and finalize into a single call.
87 88 89 |
# File 'lib/omnizip/checksums/crc_base.rb', line 87 def self.calculate(data) new.update(data).finalize end |
.generate_table(polynomial, bits) ⇒ Array<Integer>
Generate a CRC lookup table for given polynomial.
This method pre-computes all possible CRC values for single-byte inputs, enabling efficient O(1) per-byte processing.
142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/omnizip/checksums/crc_base.rb', line 142 def self.generate_table(polynomial, bits) mask = (1 << bits) - 1 (0..255).map do |i| crc = i 8.times do if crc.anybits?(1) crc = (crc >> 1) ^ polynomial else crc >>= 1 end end crc & mask end end |
.lookup_table ⇒ Array<Integer>
Get the lookup table for this CRC variant.
129 130 131 132 |
# File 'lib/omnizip/checksums/crc_base.rb', line 129 def self.lookup_table raise NotImplementedError, "#{self} must implement .lookup_table" end |
Instance Method Details
#finalize ⇒ Integer
Get the finalized CRC value.
Applies the final XOR operation required by most CRC standards to produce the final checksum value.
76 77 78 |
# File 'lib/omnizip/checksums/crc_base.rb', line 76 def finalize @value ^ final_xor end |
#reset ⇒ self
Reset the CRC calculator to initial state.
65 66 67 68 |
# File 'lib/omnizip/checksums/crc_base.rb', line 65 def reset @value = initial_value self end |
#update(data) ⇒ self
Update the CRC value with new data.
This method processes the input data byte-by-byte using the lookup table, allowing incremental CRC calculation.
55 56 57 58 59 60 |
# File 'lib/omnizip/checksums/crc_base.rb', line 55 def update(data) data.each_byte do |byte| @value = process_byte(@value, byte) end self end |