Module: CRC
- Extended by:
- Utils
- Defined in:
- lib/crc/_byruby.rb,
lib/crc.rb,
lib/crc/finder.rb,
lib/crc/_modules.rb
Overview
Ruby implemented CRC generator. It’s Based on the Intel’s slice-by-eight algorithm (but byte-order-free).
It’s faster than about 50% (CRC-32) and about 30% (CRC-64) of lookup-table algorithm. But need more memory.
reference:
-
www.intel.com/technology/comms/perfnet/download/CRC_generators.pdf
-
xz-utils (src/liblzma/check/crc32_fast.c, src/liblzma/check/crc32_tablegen.c)
If defined “RUBY_CRC_NOFAST=2” enviroment variable, switch to lookup-table algorithm.
If defined “RUBY_CRC_NOFAST=3” enviroment variable, switch to reference algorithm.
Defined Under Namespace
Modules: Aux, Utils Classes: BasicCRC, Generator
Constant Summary collapse
- CRC =
self- MODULE_TABLE =
{}
- SELF_TEST =
($0 == __FILE__) ? true : false
- CRC64 =
CRC64_ECMA
Class Method Summary collapse
- .crc(modulename, seq, state = nil) ⇒ Object
- .create_module(bitsize, polynomial, initial_state = 0, refin = true, refout = true, xor = ~0,, name = nil) ⇒ Object
- .digest(modulename, seq, state = nil) ⇒ Object
- .find(crc, seq, bitsize, polynomial, initstate = [0, ~0, 1], xor = [0, ~0, 1]) ⇒ Object
- .hexdigest(modulename, seq, state = nil) ⇒ Object
- .lookup(modulename) ⇒ Object (also: [])
Methods included from Utils
bitreflect, bitreflect128, bitreflect16, bitreflect32, bitreflect64, bitreflect8, bitreflect_reference, build_table, build_table!, build_table8, build_table8!, export_table
Class Method Details
.crc(modulename, seq, state = nil) ⇒ Object
382 383 384 |
# File 'lib/crc.rb', line 382 def crc(modulename, seq, state = nil) lookup(modulename).crc(seq, state) end |
.create_module(bitsize, polynomial, initial_state = 0, refin = true, refout = true, xor = ~0,, name = nil) ⇒ Object
394 395 396 397 398 399 |
# File 'lib/crc.rb', line 394 def create_module(bitsize, polynomial, initial_state = 0, refin = true, refout = true, xor = ~0, name = nil) generator = Generator.new(bitsize, polynomial, initial_state, refin, refout, xor, name) crc = Class.new(BasicCRC) crc.const_set :GENERATOR, generator crc end |
.digest(modulename, seq, state = nil) ⇒ Object
386 387 388 |
# File 'lib/crc.rb', line 386 def digest(modulename, seq, state = nil) lookup(modulename).digest(seq, state) end |
.find(crc, seq, bitsize, polynomial, initstate = [0, ~0, 1], xor = [0, ~0, 1]) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/crc/finder.rb', line 4 def self.find(crc, seq, bitsize, polynomial, initstate = [0, ~0, 1], xor = [0, ~0, 1]) bitsize0 = bitsize.to_i if bitsize0 < 1 || bitsize0 > 128 raise ArgumentError, "wrong bitsize (expect 1..128, but given #{bitsize})" end bitmask = ~(~0 << bitsize0) crc &= bitmask [polynomial, Utils.bitreflect(polynomial, bitsize0)].each do |poly| poly &= bitmask [false, true].each do |refin| [false, true].each do |refout| Array(xor).each do |xormask| xormask &= bitmask Array(initstate).each do |init| init &= bitmask mod = CRC.create_module(bitsize0, poly, init, refin, refout, xormask) return mod if mod.crc(seq) == crc end end end end end nil end |
.hexdigest(modulename, seq, state = nil) ⇒ Object
390 391 392 |
# File 'lib/crc.rb', line 390 def hexdigest(modulename, seq, state = nil) lookup(modulename).hexdigest(seq, state) end |
.lookup(modulename) ⇒ Object Also known as: []
374 375 376 377 378 |
# File 'lib/crc.rb', line 374 def lookup(modulename) modulename1 = modulename.to_s.gsub(/[\W_]+/, "") modulename1.downcase! MODULE_TABLE[modulename1] or raise NameError, "modulename is not matched (for #{modulename})" end |