Module: CRC::Aux

Defined in:
lib/crc.rb,
lib/crc/acrc.rb,
lib/crc/_combine.rb

Overview

Internal using module.

Class Method Summary collapse

Class Method Details

.acrc_loop_reflect(target_crc, state, poly, crcbits, bitmask, bits) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/crc/acrc.rb', line 81

def self.acrc_loop_reflect(target_crc, state, poly, crcbits, bitmask, bits)
  head = bits - 1
  bitmask1 = bitmask >> 1
  crchead = crcbits - 1

  #puts "target_crc=0b%016b, state=0b%016b, reversed-polynomial=0b%016b" % [target_crc, state, poly]
  bits.times do |i|
    if target_crc[crchead] == 0
      target_crc <<= 1
    else
      target_crc ^= poly
      target_crc <<= 1
      target_crc |= 0x01
    end

    target_crc ^= state[head]
    #puts "    0_%016b ^ %d" % [target_crc, state[head]]
    state = (state & bitmask1) << 1
  end
  #puts "target_crc=0b%016b" % target_crc

  target_crc
end

.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



160
161
162
# File 'lib/crc.rb', line 160

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

.DIGEST(num, bitsize) ⇒ Object



153
154
155
156
157
158
# File 'lib/crc.rb', line 153

def self.DIGEST(num, bitsize)
  bits = (bitsize + 7) / 8 * 8
  seq = ""
  (bits - 8).step(0, -8) { |i| seq << yield((num >> i) & 0xff) }
  seq
end

.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



164
165
166
# File 'lib/crc.rb', line 164

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



174
175
176
177
178
179
180
181
182
# File 'lib/crc.rb', line 174

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