module CRC::Utils
Utilities.
Public Instance Methods
bitreflect(num, bitsize)
click to toggle source
# File lib/crc/_utils.rb, line 14 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)
click to toggle source
# File lib/crc/_byruby.rb, line 300 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)
click to toggle source
# File lib/crc/_byruby.rb, line 273 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)
click to toggle source
# File lib/crc/_byruby.rb, line 281 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)
click to toggle source
# File lib/crc/_byruby.rb, line 290 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)
click to toggle source
# File lib/crc/_byruby.rb, line 266 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)
click to toggle source
# File lib/crc/_utils.rb, line 8 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)
click to toggle source
# File lib/crc/_utils.rb, line 58 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)
click to toggle source
# File lib/crc/_utils.rb, line 31 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)
click to toggle source
# File lib/crc/_utils.rb, line 82 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