Module: CRC::Utils

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

Instance Method Summary collapse

Instance Method Details

#bitreflect(num, bitsize) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/crc.rb', line 27

def bitreflect(num, bitsize)
  case
  when bitsize > 64
    bitreflect_reference(num, 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



57
58
59
60
61
62
63
64
65
66
# File 'lib/crc/_byruby.rb', line 57

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



30
31
32
33
34
35
36
# File 'lib/crc/_byruby.rb', line 30

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



38
39
40
41
42
43
44
45
# File 'lib/crc/_byruby.rb', line 38

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



47
48
49
50
51
52
53
54
55
# File 'lib/crc/_byruby.rb', line 47

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



23
24
25
26
27
28
# File 'lib/crc/_byruby.rb', line 23

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



21
22
23
24
25
# File 'lib/crc.rb', line 21

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

#build_table(bitsize, polynomial) ⇒ Object



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

def build_table(bitsize, polynomial)
  bitmask = ~(~0 << bitsize)
  carrydown = bitmask >> 1
  polynomial = bitmask & polynomial
  table = []
  head = 7
  256.times do |i|
    8.times { i = (i[head] == 0) ? (i << 1) : (((i & carrydown) << 1) ^ polynomial) }
    table << i
  end

  table.freeze
end

#build_table!(bitsize, polynomial) ⇒ Object



105
106
107
108
109
110
111
112
113
114
# File 'lib/crc.rb', line 105

def build_table!(bitsize, polynomial)
  polynomial = bitreflect(polynomial, bitsize)
  table = []
  256.times do |i|
    8.times { i = (i[0] == 0) ? (i >> 1) : ((i >> 1) ^ polynomial) }
    table << i
  end

  table.freeze
end

#build_table8(bitsize, polynomial, unfreeze = false) ⇒ Object



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

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

#build_table8!(bitsize, polynomial, unfreeze = false) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/crc.rb', line 116

def build_table8!(bitsize, polynomial, unfreeze = false)
  polynomial = bitreflect(polynomial, bitsize)
  table = []
  16.times do |s|
    table << (t = [])
    256.times do |b|
      r = (s == 0) ? b : table[-2][b]
      8.times { r = (r[0] == 0) ? (r >> 1) : ((r >> 1) ^ polynomial) }
      t << r
    end
    t.freeze unless unfreeze
    t
  end

  table.freeze unless unfreeze
  table
end

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



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

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