Module: CRC::Utils

Extended by:
Utils
Included in:
CRC, Utils
Defined in:
lib/crc.rb,
lib/crc/_byruby.rb

Overview

Utilities.

Instance Method Summary collapse

Instance Method Details

#bitreflect(num, bitsize) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/crc.rb', line 59

def bitreflect(num, bitsize)
  case
  when bitsize > 128
    bitreflect_reference(num, bitsize)
  when bitsize > 64
    bitreflect128(num) >> (128 - bitsize)
  when bitsize > 32
    bitreflect64(num) >> (64 - bitsize)
  when bitsize > 16
    bitreflect32(num) >> (32 - bitsize)
  when bitsize > 8
    bitreflect16(num) >> (16 - bitsize)
  else
    bitreflect8(num) >> (8 - bitsize)
  end
end

#bitreflect128(n) ⇒ Object



305
306
307
308
309
310
311
312
313
314
# File 'lib/crc/_byruby.rb', line 305

def bitreflect128(n)
  n    = n.to_i
  n    = ((n & 0x55555555555555555555555555555555) <<  1) | ((n >>  1) & 0x55555555555555555555555555555555)
  n    = ((n & 0x33333333333333333333333333333333) <<  2) | ((n >>  2) & 0x33333333333333333333333333333333)
  n    = ((n & 0x0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f) <<  4) | ((n >>  4) & 0x0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f)
  n    = ((n & 0x00ff00ff00ff00ff00ff00ff00ff00ff) <<  8) | ((n >>  8) & 0x00ff00ff00ff00ff00ff00ff00ff00ff)
  n    = ((n & 0x0000ffff0000ffff0000ffff0000ffff) << 16) | ((n >> 16) & 0x0000ffff0000ffff0000ffff0000ffff)
  n    = ((n & 0x00000000ffffffff00000000ffffffff) << 32) | ((n >> 32) & 0x00000000ffffffff00000000ffffffff)
  return ((n & 0x0000000000000000ffffffffffffffff) << 64) |  (n >> 64) # 0x0000000000000000ffffffffffffffff
end

#bitreflect16(n) ⇒ Object



278
279
280
281
282
283
284
# File 'lib/crc/_byruby.rb', line 278

def bitreflect16(n)
  n    = n.to_i
  n    = ((n & 0x5555) <<  1) | ((n >>  1) & 0x5555)
  n    = ((n & 0x3333) <<  2) | ((n >>  2) & 0x3333)
  n    = ((n & 0x0f0f) <<  4) | ((n >>  4) & 0x0f0f)
  return ((n & 0x00ff) <<  8) |  (n >>  8) # 0x00ff
end

#bitreflect32(n) ⇒ Object



286
287
288
289
290
291
292
293
# File 'lib/crc/_byruby.rb', line 286

def bitreflect32(n)
  n    = n.to_i
  n    = ((n & 0x55555555) <<  1) | ((n >>  1) & 0x55555555)
  n    = ((n & 0x33333333) <<  2) | ((n >>  2) & 0x33333333)
  n    = ((n & 0x0f0f0f0f) <<  4) | ((n >>  4) & 0x0f0f0f0f)
  n    = ((n & 0x00ff00ff) <<  8) | ((n >>  8) & 0x00ff00ff)
  return ((n & 0x0000ffff) << 16) |  (n >> 16) # 0x0000ffff
end

#bitreflect64(n) ⇒ Object



295
296
297
298
299
300
301
302
303
# File 'lib/crc/_byruby.rb', line 295

def bitreflect64(n)
  n    = n.to_i
  n    = ((n & 0x5555555555555555) <<  1) | ((n >>  1) & 0x5555555555555555)
  n    = ((n & 0x3333333333333333) <<  2) | ((n >>  2) & 0x3333333333333333)
  n    = ((n & 0x0f0f0f0f0f0f0f0f) <<  4) | ((n >>  4) & 0x0f0f0f0f0f0f0f0f)
  n    = ((n & 0x00ff00ff00ff00ff) <<  8) | ((n >>  8) & 0x00ff00ff00ff00ff)
  n    = ((n & 0x0000ffff0000ffff) << 16) | ((n >> 16) & 0x0000ffff0000ffff)
  return ((n & 0x00000000ffffffff) << 32) |  (n >> 32) # 0x00000000ffffffff
end

#bitreflect8(n) ⇒ Object



271
272
273
274
275
276
# File 'lib/crc/_byruby.rb', line 271

def bitreflect8(n)
  n    = n.to_i
  n    = ((n & 0x55) <<  1) | ((n >>  1) & 0x55)
  n    = ((n & 0x33) <<  2) | ((n >>  2) & 0x33)
  return ((n & 0x0f) <<  4) |  (n >>  4) # 0x0f
end

#bitreflect_reference(num, bitsize) ⇒ Object



53
54
55
56
57
# File 'lib/crc.rb', line 53

def bitreflect_reference(num, bitsize)
  n = 0
  bitsize.times { n <<= 1; n |= (num & 0x01); num >>= 1 }
  n
end

#build_reflect_table(bitsize, polynomial, unfreeze = false, slice: 16) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/crc.rb', line 103

def build_reflect_table(bitsize, polynomial, unfreeze = false, slice: 16)
  polynomial = bitreflect(polynomial, bitsize)
  table = []

  table << (t = [])
  256.times do |b|
    8.times { b = (b[0] == 0) ? (b >> 1) : ((b >> 1) ^ polynomial) }
    t << b
  end
  t.freeze unless unfreeze

  (1...slice).step do
    tt = table[-1]
    table << (t = [])
    256.times do |b|
      t << (table[0][tt[b] & 0xff] ^ (tt[b] >> 8))
    end
    t.freeze unless unfreeze
  end

  table.freeze unless unfreeze
  table
end

#build_table(bitsize, polynomial, unfreeze = false, slice: 16) ⇒ Object



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
# File 'lib/crc.rb', line 76

def build_table(bitsize, polynomial, unfreeze = false, slice: 16)
  bitmask = ~(~0 << bitsize)
  table = []
  Aux.slide_to_head(bitsize, 0, bitmask & polynomial, bitmask) do |xx, poly, csh, head, carries, pad|
    table << (t = [])
    256.times do |b|
      b <<= csh
      8.times { b = (b[head] == 0) ? (b << 1) : (((carries & b) << 1) ^ poly) }
      t << b
    end
    t.freeze unless unfreeze

    carries8 = carries >> 7
    (1...slice).step do
      tt = table[-1]
      table << (t = [])
      256.times do |b|
        t << (table[0][tt[b] >> csh] ^ ((carries8 & tt[b]) << 8))
      end
      t.freeze unless unfreeze
    end
    0
  end
  table.freeze unless unfreeze
  table
end

#export_table(table, bitsize, linewidth, indentsize = 2) ⇒ Object



127
128
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
# File 'lib/crc.rb', line 127

def export_table(table, bitsize, linewidth, indentsize = 2)
  bitsize0 = bitsize.to_i
  indent = " " * indentsize.to_i
  case
  when bitsize0 > 64 || bitsize0 < 1
    raise "invalid bitsize (expected to 1..64, but given #{bitsize})"
  when bitsize0 > 32
    packformat = "Q>"
    hexwidth = 16
  when bitsize0 > 16
    packformat = "N"
    hexwidth = 8
  when bitsize0 > 8
    packformat = "n"
    hexwidth = 4
  else # when bitsize0 > 0
    packformat = "C"
    hexwidth = 2
  end
  table = table.to_a.pack("#{packformat}*").unpack("H*")[0]
  table.gsub!(/(?<=\w)(?=\w{#{hexwidth}}{#{linewidth}}+$)/, "\n")
  table.gsub!(/(?<=\w)(?=\w{#{hexwidth}}+$)/, " ")
  table.gsub!(/(?<=\w)(?=\s|$)/, ",")
  table.gsub!(/(?:(?<=^)|(?<=\s))(?=\w)/, "0x")
  table.gsub!(/^/, "#{indent}  ")
  <<-EOS
#{indent}TABLE = [
#{table}
#{indent}].freeze
  EOS
end