module CRC::Aux

Internal using module.

Public Class Methods

DIGEST(num, bitsize) { |num >> i & 0xff| ... } click to toggle source
# File lib/crc/_aux.rb, line 6
def self.DIGEST(num, bitsize)
  bits = (bitsize + 7) / 8 * 8
  seq = ""
  (bits - 8).step(0, -8) { |i| seq << yield((num >> i) & 0xff) }
  seq
end
combine(crc1, crc2, len2, bitsize, polynomial, initial_crc, reflect_input, reflect_output, xor_output) click to toggle source
# File lib/crc/_combine.rb, line 57
def self.combine(crc1, crc2, len2,
                 bitsize, polynomial, initial_crc,
                 reflect_input, reflect_output, xor_output)
  return crc1 unless len2 > 1

  crc1 ^= initial_crc

  odd = []
  even = []
  if reflect_output
    odd << Utils.bitreflect(polynomial, bitsize)
    col = 1
    (bitsize - 1).times do
      odd << col
      col <<= 1
    end
  else
    col = 2
    (bitsize - 1).times do
      odd << col
      col <<= 1
    end
    odd << polynomial
  end

  Aux.gf2_matrix_square(bitsize, even, odd)
  Aux.gf2_matrix_square(bitsize, odd, even)

  while true
    Aux.gf2_matrix_square(bitsize, even, odd)
    if len2[0] == 1
      crc1 = Aux.gf2_matrix_times(even, crc1)
    end
    len2 >>= 1
    break unless len2 > 0

    Aux.gf2_matrix_square(bitsize, odd, even)
    if len2[0] == 1
      crc1 = Aux.gf2_matrix_times(odd, crc1)
    end
    len2 >>= 1;
    break unless len2 > 0
  end

  crc1 ^ crc2
end
digest(num, bitsize) click to toggle source
# File lib/crc/_aux.rb, line 13
def self.digest(num, bitsize)
  DIGEST(num, bitsize) { |n| n.chr(Encoding::BINARY) }
end
dump_banner(line_prefix, crcname, algorithm) click to toggle source
# File lib/crc/codegen.rb, line 831
    def self.dump_banner(line_prefix, crcname, algorithm)
      case algorithm
      when ALGORITHM_BITBYBIT
        algorithm = "bit-by-bit"
      when ALGORITHM_BITBYBIT_FAST
        algorithm = "bit-by-bit-fast"
      when ALGORITHM_HALFBYTE_TABLE
        algorithm = "halfbyte-table"
      when ALGORITHM_STANDARD_TABLE
        algorithm = "standard-table"
      when ALGORITHM_SLICING_BY_4, ALGORITHM_SLICING_BY_8, ALGORITHM_SLICING_BY_16, 2..999
        algorithm = "slicing-by-#{algorithm} (with byte-order free), based Intel's slicing-by-8"
      else
        raise ArgumentError, "out of algorithm code (given #{algorithm})"
      end
      (<<-EOS).gsub!(/^(?!$)/, " ").gsub!(/^/) { line_prefix }.chomp!
A CRC calculator for #{crcname}.

This code is auto generated by <https://rubygems.org/gems/crc>.

* License:: Creative Commons License Zero (CC0 / Public Domain)
            See <https://creativecommons.org/publicdomain/zero/1.0/>
* Version:: crc-#{CRC::VERSION} (powered by #{RUBY_DESCRIPTION})
* Generated at:: #{Time.now.strftime "%Y-%m-%d"}
* Algorithm:: #{algorithm}
* Need available memory:: about 1 MiB
      EOS
    end
export_slicing_table(table, baseindent, indentsize, tableprefix, tablesuffix, elementprefix, elementsuffix, lineinelements, &hexnum) click to toggle source
# File lib/crc/codegen.rb, line 860
    def self.export_slicing_table(table, baseindent, indentsize, tableprefix, tablesuffix, elementprefix, elementsuffix, lineinelements, &hexnum)
      indent = ->(n = 0) { baseindent + (" " * (indentsize * n)) }
      tableprefix &&= "#{indent[0]}#{tableprefix}\n"
      tablesuffix &&= "\n#{indent[0]}#{tablesuffix}"
      if table[0].kind_of?(Array)
        (<<-TABLE2).chomp!
#{tableprefix}#{indent[1]}#{elementprefix}
#{
    table = table.map { |tt|
      tt = tt.map(&hexnum)
      %(#{indent[2]}#{tt.each_slice(lineinelements).map { |ttt| ttt.join(", ") }.join(",\n#{indent[2]}")}\n)
    }.join("#{indent[1]}#{elementsuffix},\n#{indent[1]}#{elementprefix}\n").chomp!
}
#{indent[1]}#{elementsuffix}#{tablesuffix}
        TABLE2
      else
        table = table.map(&hexnum)
        %(#{indent[1]}#{table.each_slice(lineinelements).map { |ttt| ttt.join(", ") }.join(",\n#{indent[1]}")}\n).chomp!
      end
    end
gf2_matrix_square(bitsize, square, matrix) click to toggle source
# File lib/crc/_combine.rb, line 49
def self.gf2_matrix_square(bitsize, square, matrix)
  bitsize.times do |n|
    square[n] = gf2_matrix_times(matrix, matrix[n])
  end

  nil
end
gf2_matrix_times(matrix, vector) click to toggle source
# File lib/crc/_combine.rb, line 38
def self.gf2_matrix_times(matrix, vector)
  sum = 0
  matrix.each do |m|
    break unless vector > 0
    sum ^= m unless vector[0] == 0
    vector >>= 1
  end

  sum
end
hexdigest(num, bitsize) click to toggle source
# File lib/crc/_aux.rb, line 17
def self.hexdigest(num, bitsize)
  DIGEST(num, bitsize) { |n| "%02X" % n }
end
slide_to_head(bitsize, state, polynomial, bitmask) { |padded_state, padded_polynomial, shift_input, off_msb, carries_mask, padding_size| padded_new_state } → new_state click to toggle source

YIELD(padded_state, padded_polynomial, shift_input, off_msb, carries_mask, padding_size) -> padded_new_state

# File lib/crc/_aux.rb, line 27
def self.slide_to_head(bitsize, state, polynomial, bitmask)
  pad = bitsize & 0x07
  if pad == 0
    yield(state, polynomial, bitsize - 8, bitsize - 1, bitmask >> 1, 0)
  else
    pad = 8 - pad
    yield(state << pad, polynomial << pad, bitsize - 8 + pad, bitsize - 1 + pad, (bitmask << pad >> 1) | 0x7f, pad) >> pad
  end
end