Class: Omnizip::Checksums::CrcBase Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/omnizip/checksums/crc_base.rb

Overview

This class is abstract.

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

Direct Known Subclasses

Crc32, Crc64

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCrcBase

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

#valueInteger (readonly)

Returns current CRC value.

Returns:

  • (Integer)

    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.

Parameters:

  • data (String)

    binary string to checksum

Returns:

  • (Integer)

    final CRC checksum



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.

Parameters:

  • polynomial (Integer)

    CRC polynomial

  • bits (Integer)

    bit width (32 or 64)

Returns:

  • (Array<Integer>)

    256-entry lookup table



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_tableArray<Integer>

This method is abstract.

Get the lookup table for this CRC variant.

Returns:

  • (Array<Integer>)

    256-entry lookup table

Raises:

  • (NotImplementedError)


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

#finalizeInteger

Get the finalized CRC value.

Applies the final XOR operation required by most CRC standards to produce the final checksum value.

Returns:

  • (Integer)

    final CRC checksum



76
77
78
# File 'lib/omnizip/checksums/crc_base.rb', line 76

def finalize
  @value ^ final_xor
end

#resetself

Reset the CRC calculator to initial state.

Returns:

  • (self)

    for method chaining



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.

Parameters:

  • data (String)

    binary string to process

Returns:

  • (self)

    for method chaining



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