Module: CRC::Aux

Defined in:
lib/crc/_aux.rb,
lib/crc/codegen.rb,
lib/crc/_combine.rb

Overview

Internal using module.

Class Method Summary collapse

Class Method Details

.combine(crc1, crc2, len2, bitsize, polynomial, initial_crc, reflect_input, reflect_output, xor_output) ⇒ Object



57
58
59
60
61
62
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
97
98
99
100
101
102
# 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) ⇒ Object



13
14
15
# File 'lib/crc/_aux.rb', line 13

def self.digest(num, bitsize)
  DIGEST(num, bitsize) { |n| n.chr(Encoding::BINARY) }
end

.DIGEST(num, bitsize) ⇒ Object



6
7
8
9
10
11
# 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

.dump_banner(line_prefix, crcname, algorithm) ⇒ Object



841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
# File 'lib/crc/codegen.rb', line 841

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
    algorithm = "slicing-by-#{algorithm} (with byte-order free), based Intel's slicing-by-8"
  else
    raise ArgumentError, "out of algorithm code (given #{algorithm})"
  end
  ("A CRC calculator for \#{crcname}.\n\nThis code is auto generated by <https://rubygems.org/gems/crc>.\n\n* License:: Creative Commons License Zero (CC0 / Public Domain)\n        See <https://creativecommons.org/publicdomain/zero/1.0/>\n* Version:: crc-\#{CRC::VERSION} (powered by \#{RUBY_DESCRIPTION})\n* Generated at:: \#{Time.now.strftime \"%Y-%m-%d\"}\n* Algorithm:: \#{algorithm}\n* Need available memory:: about 1 MiB\n  EOS\nend\n").gsub!(/^(?!$)/, " ").gsub!(/^/) { line_prefix }.chomp!

.export_slicing_table(table, baseindent, indentsize, tableprefix, tablesuffix, elementprefix, elementsuffix, lineinelements, &hexnum) ⇒ Object



870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
# File 'lib/crc/codegen.rb', line 870

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)
    ("\#{tableprefix}\#{indent[1]}\#{elementprefix}\n\#{\ntable = table.map { |tt|\n  tt = tt.map(&hexnum)\n  %(\#{indent[2]}\#{tt.each_slice(lineinelements).map { |ttt| ttt.join(\", \") }.join(\",\\n\#{indent[2]}\")}\\n)\n}.join(\"\#{indent[1]}\#{elementsuffix},\\n\#{indent[1]}\#{elementprefix}\\n\").chomp!\n}\n\#{indent[1]}\#{elementsuffix}\#{tablesuffix}\n    TABLE2\n  else\n    table = table.map(&hexnum)\n    %(\#{indent[1]}\#{table.each_slice(lineinelements).map { |ttt| ttt.join(\", \") }.join(\",\\n\#{indent[1]}\")}\\n).chomp!\n  end\nend\n").chomp!

.gf2_matrix_square(bitsize, square, matrix) ⇒ Object



49
50
51
52
53
54
55
# 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) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# 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) ⇒ Object



17
18
19
# 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) ⇒ Object

call-seq:

slide_to_head(bitsize, state, polynomial, bitmask) { |padded_state, padded_polynomial, shift_input, off_msb, carries_mask, padding_size| padded_new_state } -> new_state

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



27
28
29
30
31
32
33
34
35
# 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