Class: CRC::Generator

Inherits:
Struct
  • Object
show all
Defined in:
lib/crc.rb,
lib/crc/_byruby.rb

Defined Under Namespace

Classes: BasicStruct

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bitsize, polynomial, initial_state = 0, reflect_input = true, reflect_output = true, xor_output = ~0,, name = nil) ⇒ Generator

Returns a new instance of Generator.



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/crc/_byruby.rb', line 72

def initialize(bitsize, polynomial, initial_state = 0, reflect_input = true, reflect_output = true, xor_output = ~0, name = nil)
  bitsize = bitsize.to_i
  if bitsize < 1 || bitsize > 64
    raise ArgumentError, "wrong bitsize (except 1..64, but given #{bitsize})"
  end
  bitmask = ~(~0 << bitsize)
  polynomial = bitmask & polynomial
  initial_state = bitmask & initial_state
  xor_output = bitmask & xor_output
  name = (name.nil? || ((name = String(name)).empty?)) ? nil : name
  super(bitsize, bitmask, polynomial, initial_state, nil,
        !!reflect_input, !!reflect_output, xor_output, name)
end

Instance Attribute Details

#bitmaskObject

Returns the value of attribute bitmask

Returns:

  • (Object)

    the current value of bitmask



69
70
71
# File 'lib/crc/_byruby.rb', line 69

def bitmask
  @bitmask
end

#bitsizeObject

Returns the value of attribute bitsize

Returns:

  • (Object)

    the current value of bitsize



69
70
71
# File 'lib/crc/_byruby.rb', line 69

def bitsize
  @bitsize
end

#initial_stateObject

Returns the value of attribute initial_state

Returns:

  • (Object)

    the current value of initial_state



69
70
71
# File 'lib/crc/_byruby.rb', line 69

def initial_state
  @initial_state
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



69
70
71
# File 'lib/crc/_byruby.rb', line 69

def name
  @name
end

#polynomialObject

Returns the value of attribute polynomial

Returns:

  • (Object)

    the current value of polynomial



69
70
71
# File 'lib/crc/_byruby.rb', line 69

def polynomial
  @polynomial
end

#reflect_inputObject

Returns the value of attribute reflect_input

Returns:

  • (Object)

    the current value of reflect_input



69
70
71
# File 'lib/crc/_byruby.rb', line 69

def reflect_input
  @reflect_input
end

#reflect_outputObject

Returns the value of attribute reflect_output

Returns:

  • (Object)

    the current value of reflect_output



69
70
71
# File 'lib/crc/_byruby.rb', line 69

def reflect_output
  @reflect_output
end

#table8Object

Returns the value of attribute table8

Returns:

  • (Object)

    the current value of table8



69
70
71
# File 'lib/crc/_byruby.rb', line 69

def table8
  @table8
end

#xor_outputObject

Returns the value of attribute xor_output

Returns:

  • (Object)

    the current value of xor_output



69
70
71
# File 'lib/crc/_byruby.rb', line 69

def xor_output
  @xor_output
end

Instance Method Details

#crc(seq, state = nil) ⇒ Object



206
207
208
# File 'lib/crc.rb', line 206

def crc(seq, state = nil)
  finish(update(seq, setup(state)))
end

#digest(seq, state = nil) ⇒ Object



222
223
224
# File 'lib/crc.rb', line 222

def digest(seq, state = nil)
  Aux.digest(crc(seq, state), bitsize)
end

#finish(state) ⇒ Object



217
218
219
220
# File 'lib/crc.rb', line 217

def finish(state)
  state = CRC.bitreflect(state, bitsize) if reflect_input ^ reflect_output
  state ^ xor_output
end

#hexdigest(seq, state = nil) ⇒ Object



226
227
228
# File 'lib/crc.rb', line 226

def hexdigest(seq, state = nil)
  Aux.hexdigest(crc(seq, state), bitsize)
end

#inspectObject



266
267
268
# File 'lib/crc.rb', line 266

def inspect
  "\#<#{self.class} #{to_s}>"
end

#pretty_inspect(q) ⇒ Object



270
271
272
# File 'lib/crc.rb', line 270

def pretty_inspect(q)
  q.text inspect
end

#setup(state = nil) ⇒ Object



210
211
212
213
214
215
# File 'lib/crc.rb', line 210

def setup(state = nil)
  state ||= initial_state
  state ^= xor_output
  state = CRC.bitreflect(state, bitsize) if reflect_input ^ reflect_output
  state
end

#to_sObject



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/crc.rb', line 230

def to_s
  case
  when bitsize > 64 then width = 20
  when bitsize > 32 then width = 16
  when bitsize > 16 then width =  8
  when bitsize >  8 then width =  4
  else                   width =  2
  end

  if reflect_input
    ref = ", reflect-in#{reflect_output ? "/out" : ""}"
  else
    ref = reflect_output ? ", reflect-out" : ""
  end

  case initial_state
  when 0        then init = "0"
  when bitmask  then init = "~0"
  when 1        then init = "1"
  else               init = "0x%0#{width}X" % initial_state
  end

  case xor_output
  when 0        then xor = "0"
  when bitmask  then xor = "~0"
  when 1        then xor = "1"
  else               xor = "0x%0#{width}X" % xor_output
  end

  if nm = name
    "#{nm}(CRC-%d-0x%0#{width}X init=%s%s, xor=%s)" % [bitsize, polynomial, init, ref, xor]
  else
    "(CRC-%d-0x%0#{width}X init=%s%s, xor=%s)" % [bitsize, polynomial, init, ref, xor]
  end
end

#update_with_lookup_table(seq, state) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/crc/_byruby.rb', line 110

def update_with_lookup_table(seq, state)
  t = table8[0]

  if reflect_input
    String(seq).each_byte do |ch|
      state = t[state & 0xff ^ ch] ^ (state >> 8)
    end
    state
  else
    Aux.slide_to_head(bitsize, state, polynomial, bitmask) do |s, poly, csh, head, carries|
      carries8 = carries >> 7
      String(seq).each_byte do |ch|
        s = t[(s >> csh) ^ ch] ^ ((carries8 & s) << 8)
      end
      s
    end
  end
end

#update_with_reference(seq, state) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/crc/_byruby.rb', line 86

def update_with_reference(seq, state)
  if reflect_input
    poly = CRC.bitreflect(polynomial, bitsize)
    seq.each_byte do |ch|
      state ^= ch
      8.times { state = (state[0] == 0) ? (state >> 1) : ((state >> 1) ^ poly) }

      # 8.times { state = (state >> 1) ^ (poly & -state[0]) }
      # NOTE: ruby だと、分岐したほうが2割くらい高速
    end

    state
  else
    Aux.slide_to_head(bitsize, state, polynomial, bitmask) do |s, poly, csh, head, carries|
      seq.each_byte do |ch|
        s ^= ch << csh
        8.times { s = (s[head] == 0) ? (s << 1) : (((carries & s) << 1) ^ poly) }
      end

      s
    end
  end
end

#update_with_slice_by_eight(seq, s) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/crc/_byruby.rb', line 129

def update_with_slice_by_eight(seq, s)
  tX = table8
  t0 = tX[ 0]; t1 = tX[ 1]; t2 = tX[ 2]; t3 = tX[ 3]
  t4 = tX[ 4]; t5 = tX[ 5]; t6 = tX[ 6]; t7 = tX[ 7]
  t8 = tX[ 8]; t9 = tX[ 9]; tA = tX[10]; tB = tX[11]
  tC = tX[12]; tD = tX[13]; tE = tX[14]; tF = tX[15]

  i = 0
  ii = seq.bytesize
  iii = ii & ~15

  if reflect_input
    if bitsize <= 32
      # speed improvement for 32-bits CRC
      while i < iii
        s = tF[seq.getbyte(i     ) ^ (s      ) & 0xff] ^ tE[seq.getbyte(i +  1) ^ (s >>  8) & 0xff] ^
            tD[seq.getbyte(i +  2) ^ (s >> 16) & 0xff] ^ tC[seq.getbyte(i +  3) ^ (s >> 24) & 0xff] ^
            tB[seq.getbyte(i +  4)                   ] ^ tA[seq.getbyte(i +  5)                   ] ^
            t9[seq.getbyte(i +  6)                   ] ^ t8[seq.getbyte(i +  7)                   ] ^
            t7[seq.getbyte(i +  8)                   ] ^ t6[seq.getbyte(i +  9)                   ] ^
            t5[seq.getbyte(i + 10)                   ] ^ t4[seq.getbyte(i + 11)                   ] ^
            t3[seq.getbyte(i + 12)                   ] ^ t2[seq.getbyte(i + 13)                   ] ^
            t1[seq.getbyte(i + 14)                   ] ^ t0[seq.getbyte(i + 15)                   ]
        i += 16
      end
    else
      while i < iii
        s = tF[seq.getbyte(i     ) ^ (s      ) & 0xff] ^ tE[seq.getbyte(i +  1) ^ (s >>  8) & 0xff] ^
            tD[seq.getbyte(i +  2) ^ (s >> 16) & 0xff] ^ tC[seq.getbyte(i +  3) ^ (s >> 24) & 0xff] ^
            tB[seq.getbyte(i +  4) ^ (s >> 32) & 0xff] ^ tA[seq.getbyte(i +  5) ^ (s >> 40) & 0xff] ^
            t9[seq.getbyte(i +  6) ^ (s >> 48) & 0xff] ^ t8[seq.getbyte(i +  7) ^ (s >> 56) & 0xff] ^
            t7[seq.getbyte(i +  8)                   ] ^ t6[seq.getbyte(i +  9)                   ] ^
            t5[seq.getbyte(i + 10)                   ] ^ t4[seq.getbyte(i + 11)                   ] ^
            t3[seq.getbyte(i + 12)                   ] ^ t2[seq.getbyte(i + 13)                   ] ^
            t1[seq.getbyte(i + 14)                   ] ^ t0[seq.getbyte(i + 15)                   ]
        i += 16
      end
    end

    (iii...ii).each do |n|
      s = t0[seq.getbyte(n) ^ s & 0xff] ^ (s >> 8)
    end

    s
  else
    Aux.slide_to_head(bitsize, s, polynomial, bitmask) do |s, poly, csh, head, carries|
      sh = 64 - (head + 1)

      while i < iii
        s <<= sh
        s = t7[seq.getbyte(i    ) ^ (s >> 56) & 0xff] ^ t6[seq.getbyte(i + 1) ^ (s >> 48) & 0xff] ^
            t5[seq.getbyte(i + 2) ^ (s >> 40) & 0xff] ^ t4[seq.getbyte(i + 3) ^ (s >> 32) & 0xff] ^
            t3[seq.getbyte(i + 4) ^ (s >> 24) & 0xff] ^ t2[seq.getbyte(i + 5) ^ (s >> 16) & 0xff] ^
            t1[seq.getbyte(i + 6) ^ (s >>  8) & 0xff] ^ t0[seq.getbyte(i + 7) ^ (s >>  0) & 0xff]
        i += 8
      end

      carries8 = carries >> 7
      (iii...ii).each do |n|
        s = t0[(s >> csh) ^ seq.getbyte(n)] ^ ((carries8 & s) << 8)
      end
      s
    end
  end
end