crc 0.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bee58adf97d048655d45b3f33a14781b150638c7
4
- data.tar.gz: bd49389a8b03124937b87c7a09deaf1fd03b3f8e
3
+ metadata.gz: ccf4d480da684a319f684082517b687ec6bd9974
4
+ data.tar.gz: c97ed329ce7093405e7db797ede21ff6f8d9acbc
5
5
  SHA512:
6
- metadata.gz: ba1a3e68cef316f5e46db132c8fceeb66bace335427ec49b01987f87bb12b74ab7409649b5240baa4876231fd52da93f999fb6fdb01fa1f56657a9183f3b03fd
7
- data.tar.gz: 707b71a8fcd2dfe0593ce3d94b4fbd3b2265c202e67d6e704c39c758c275e1386fe14626090c8ace254c78edb6e7ff03846b8ea551bf4335e110c1eef78f9a41
6
+ metadata.gz: 2c026e783622209e7fd9ff23783a773a6da6c2514f685c177ade3225b560b9c2db5ebe126e6bedbe98e7a039c24d0974ae73139bf623d771fc47f07cb04b39ce
7
+ data.tar.gz: a4b27b3b7262c4c46e616048d587c3ae9b809dd104d7e76686f611f830c9f761eccbef0a693cf46acede424205a1984247b351e7f9ef78442fc800489577a68d
@@ -0,0 +1,13 @@
1
+ This document is written in Japanese.
2
+
3
+ # crc for ruby の更新履歴
4
+
5
+ ## crc-0.2 (平成28年5月15日 (日))
6
+
7
+ * CRC モジュールの追加と修正
8
+ * CRC::Utils.build\_table、CRC::Utils.build\_reflect\_table メソッドに slice キーワード引数を導入
9
+ * CRC::Generator#combine、CRC::BasicCRC#+ メソッドの実装
10
+
11
+ ## crc-0.1 (平成28年5月8日 (日))
12
+
13
+ 初版
data/README.md CHANGED
@@ -3,46 +3,53 @@
3
3
 
4
4
  This is a general CRC (Cyclic Redundancy Check) generator for ruby.
5
5
 
6
- It is written by pure ruby with based on slice-by-eight algorithm (byte-order-free slice-by-16 algorithm).
6
+ It is written by pure ruby with based on slice-by-eight algorithm (slice-by-16 algorithm as byte-order free and byte-alignment free).
7
7
 
8
- Included built-in CRC modules are CRC-32, CRC-64-ECMA, CRC-64-ISO, CRC-16-CCITT, CRC-16-IBM, CRC-8, CRC-5-USB, CRC-5-EPC and more.
8
+ Included built-in CRC modules are CRC-32, CRC-64-ECMA, CRC-64-ISO, CRC-16-CCITT, CRC-16-IBM, CRC-8, CRC-5-USB, CRC-5-EPC and many more.
9
9
 
10
- And your defined CRC modules to use.
10
+ Additional your customized CRC modules are defined to posible.
11
11
 
12
- This is slower than x95+ of zlib/crc32, and slower than x135+ of extlzma/crc32 on FreeBSD 10.3R amd64.
12
+ This library is slower than ×85+ of zlib/crc32, and slower than ×120+ of extlzma/crc32 on FreeBSD 10.3R amd64.
13
13
 
14
- If you need more speed, please use crc-turbo.
14
+ If you need more speed, please use [crc-turbo](https://rubygems/gems/crc-turbo).
15
15
 
16
16
 
17
- ## SUMMARY
17
+ ## Summary
18
18
 
19
19
  * package name: crc
20
20
  * author: dearblue (mailto:dearblue@users.osdn.me)
21
21
  * report issue to: <https://osdn.jp/projects/rutsubo/ticket/>
22
22
  * how to install: ``gem install crc``
23
- * version: 0.1
23
+ * version: 0.2
24
24
  * release quality: thechnical preview
25
- * licensing: BSD-2-Clause
25
+ * licensing: BSD-2-Clause<br>any parts are under Creative Commons License Zero (CC0 / Public Domain), and zlib-style License.
26
26
  * dependency gems: none
27
27
  * dependency external c libraries: none
28
28
  * bundled external c libraries: none
29
29
 
30
- ## FEATURES
31
30
 
32
- * ``CRC.crc32(seq, init = 0) -> crc-32 integer`` (likely as ``Zlib.crc32``)
33
- * ``CRC::CRC32.crc(seq, init = 0) -> crc-32 integer`` (likely as ``Zlib.crc32``)
34
- * ``CRC::CRC32.digest(seq, init = 0) -> crc-32 digest`` (likely as ``Digest::XXXX.digest``)
35
- * ``CRC::CRC32.hexdigest(seq, init = 0) -> crc-32 hex-digest`` (likely as ``Digest::XXXX.hexdigest``)
36
- * ``CRC::CRC32.new(init = 0) -> crc-32 context`` (likely as ``Digest::XXXX.new``)
37
- * ``CRC::CRC32#update(seq) -> self`` (likely as ``Digest::XXXX#update``)
38
- * ``CRC::CRC32#finish -> crc-32 integer`` (likely as ``Digest::XXXX#finish``)
39
- * ``CRC.crc("crc-32", seq, init = 0) -> crc-32 integer``
40
- * ``CRC.digest("crc-32", seq, init = 0) -> crc-32 digest``
41
- * ``CRC.hexdigest("crc-32", seq, init = 0) -> crc-32 hex-digest``
31
+ ## Features
32
+
33
+ This examples are used CRC-32 module. Please see CRC::BasicCRC for more details.
34
+
35
+ * CRC.crc32(seq, init = 0) -> crc-32 integer (likely as ``Zlib.crc32``)
36
+ * CRC::CRC32.crc(seq, init = 0) -> crc-32 integer (likely as ``Zlib.crc32``)
37
+ * CRC::CRC32.digest(seq, init = 0) -> crc-32 digest (likely as ``Digest::XXXX.digest``)
38
+ * CRC::CRC32.hexdigest(seq, init = 0) -> crc-32 hex-digest (likely as ``Digest::XXXX.hexdigest``)
39
+ * CRC::CRC32.new(init = 0) -> crc-32 context (likely as ``Digest::XXXX.new``)
40
+ * CRC::CRC32#update(seq) -> self (likely as ``Digest::XXXX#update``)
41
+ * CRC::CRC32#state -> crc-32 integer
42
+ * CRC::CRC32#digest -> crc-32 digest (likely as ``Digest::XXXX#digest``)
43
+ * CRC::CRC32#hexdigest -> crc-32 hex-digest (likely as ``Digest::XXXX#hexdigest``)
44
+ * CRC.crc("crc-32", seq, init = 0) -> crc-32 integer
45
+ * CRC.digest("crc-32", seq, init = 0) -> crc-32 digest
46
+ * CRC.hexdigest("crc-32", seq, init = 0) -> crc-32 hex-digest
47
+ * CRC::CRC32.combine(CRC.crc32("123"), CRC.crc32("456789"), 6) -> 3421780262 (likely as ``Zlib.crc32_comibne``)
48
+ * CRC.CRC32("123") + CRC.CRC32("456") + CRC.CRC32("789") -> &#35;&lt;CRC::CRC32:CBF43926&gt;
42
49
 
43
50
  ----
44
51
 
45
- * ``CRC.create_module(bitsize, poly, init_state, refin, refout, xorout) -> new crc module class``
52
+ * CRC.create\_module(bitsize, poly, init\_state, refin, refout, xorout) -> new crc module class
46
53
 
47
54
  ``` ruby:ruby
48
55
  MyCRC32 = CRC.create_module(32, 0x04C11DB7)
@@ -51,17 +58,23 @@ If you need more speed, please use crc-turbo.
51
58
  ```
52
59
 
53
60
 
54
- ## BUILD-IN CRC MODULES
61
+ ## Built-in CRC modules
55
62
 
56
63
  ``` shell:shell
57
- % ruby -rcrc -e 'puts CRC::MODULE_TABLE.values.uniq.map { |m| m::TRAITS.name }.join(", ")'
64
+ % ruby -rcrc -e 'puts CRC::MODULE_TABLE.values.uniq.map { |m| m::GENERATOR.name }.join(", ")'
58
65
  ```
59
66
 
60
- CRC-1, CRC-3-ROHC, CRC-4-ITU, CRC-5-EPC, CRC-5-ITU, CRC-5-USB, CRC-6-CDMA2000-A, CRC-6-CDMA2000-B, CRC-6-DARC, CRC-6-ITU, CRC-7, CRC-7-MVB, CRC-8, CRC-8-CCITT, CRC-8-Dallas/Maxim, CRC-8-DARC, CRC-8-SAE, CRC-8-WCDMA, CRC-10, CRC-10-CDMA2000, CRC-11, CRC-12, CRC-12-CDMA2000, CRC-13-BBC, CRC-14-DARC, CRC-15-CAN, CRC-15-MPT1327, Chakravarty, CRC-16-ARINC, CRC-16-CCITT, CRC-16-CDMA2000, CRC-16-DECT, CRC-16-T10-DIF, CRC-16-DNP, CRC-16-IBM, CRC-16-LZH, CRC-17-CAN, CRC-21-CAN, CRC-24, CRC-24-Radix-64, CRC-30, CRC-32, CRC-32C, CRC-32K, CRC-32K2, CRC-32Q, CRC-40-GSM, CRC-64-ECMA, CRC-64-ISO
67
+ CRC-1, CRC-3-ROHC, CRC-4-INTERLAKEN, CRC-4-ITU, CRC-5-EPC, CRC-5-ITU, CRC-5-USB, CRC-6-CDMA2000-A, CRC-6-CDMA2000-B, CRC-6-DARC, CRC-6-ITU, CRC-7, CRC-7-MVB, CRC-7-ROHC, CRC-7-UMTS, CRC-8, CRC-8-CCITT, CRC-8-MAXIM, CRC-8-DARC, CRC-8-SAE, CRC-8-WCDMA, CRC-8-CDMA2000, CRC-8-DVB-S2, CRC-8-EBU, CRC-8-I-CODE, CRC-8-ITU, CRC-8-LTE, CRC-8-ROHC, CRC-10, CRC-10-CDMA2000, CRC-11, CRC-11-UMTS, CRC-12-CDMA2000, CRC-12-DECT, CRC-12-UMTS, CRC-13-BBC, CRC-14-DARC, CRC-15, CRC-15-MPT1327, Chakravarty, ARC, CRC-16-ARINC, CRC-16-AUG-CCITT, CRC-16-CDMA2000, CRC-16-DECT-R, CRC-16-DECT-X, CRC-16-T10-DIF, CRC-16-DNP, CRC-16-BUYPASS, CRC-16-CCITT-FALSE, CRC-16-DDS-110, CRC-16-EN-13757, CRC-16-GENIBUS, CRC-16-LJ1200, CRC-16-MAXIM, CRC-16-MCRF4XX, CRC-16-RIELLO, CRC-16-TELEDISK, CRC-16-TMS37157, CRC-16-USB, CRC-A, KERMIT, MODBUS, X-25, XMODEM, CRC-17-CAN, CRC-21-CAN, CRC-24, CRC-24-Radix-64, CRC-24-OPENPGP, CRC-24-BLE, CRC-24-FLEXRAY-A, CRC-24-FLEXRAY-B, CRC-24-INTERLAKEN, CRC-24-LTE-A, CRC-24-LTE-B, CRC-30, CRC-30-CDMA, CRC-31-PHILIPS, CRC-32, CRC-32-BZIP2, CRC-32C, CRC-32D, CRC-32-MPEG-2, CRC-32-POSIX, CRC-32K, CRC-32K2, CRC-32Q, JAMCRC, XFER, CRC-40-GSM, CRC-64, CRC-64-ECMA, CRC-64-WE, CRC-64-ISO
61
68
 
62
69
 
63
- ## ENVIRONMENT VARIABLES FOR BEHAVIOR
70
+ ## Environment variables for behavior
64
71
 
72
+ * ``RUBY_CRC_NOFAST=0``: Use "crc-turbo" if posible. When failure required, same as ``RUBY_CRC_NOFAST=1``.
65
73
  * ``RUBY_CRC_NOFAST=1``: Force use ruby implementation with slice-by-16 algorithm. Not used "crc-turbo".
66
- * ``RUBY_CRC_NOFAST=2``: Switch to lookup table algorithm from slice-by-16 algorithm. Slower to about 52% (when CRC-32).
67
- * ``RUBY_CRC_NOFAST=3``: Switch to reference algorithm from slice-by-16 algorithm. Slower to about 7% (when CRC-32).
74
+ * ``RUBY_CRC_NOFAST=2``: Switch to lookup table algorithm from slice-by-16 algorithm. Slower than about 52% (when CRC-32).
75
+ * ``RUBY_CRC_NOFAST=3``: Switch to reference algorithm from slice-by-16 algorithm. Slower than about 7% (when CRC-32).
76
+
77
+
78
+ ## About CRC::Generator#combine
79
+
80
+ CRC::Generator#combine is ported from Mark Adler's crccomb.c in https://stackoverflow.com/questions/29915764/generic-crc-8-16-32-64-combine-implementation#29928573 .
@@ -2,6 +2,7 @@
2
2
  # Only this code to the PUBLIC DOMAIN.
3
3
  #
4
4
 
5
+ require "optparse"
5
6
  require "benchmark"
6
7
  #require "securerandom"
7
8
  require "zlib"
@@ -14,15 +15,25 @@ def measure(generator_name)
14
15
  $stdout.flush
15
16
  realms = 5.times.map do
16
17
  real = (Benchmark.measure { yield }.real * 1000)
17
- print " #{real.ceil} ms."
18
+ print " #{(real * 100).round / 100.0} ms."
18
19
  $stdout.flush
19
20
  real
20
21
  end.min
21
- puts " (#{realms.ceil} ms.)\n"
22
+ puts " (#{(realms * 100).round / 100.0} ms.)\n"
22
23
  [generator_name, realms]
23
24
  end
24
25
 
25
- size = 20
26
+ opt = OptionParser.new
27
+ size = 2
28
+ opt.on("-s size", "set input data size in MiB (default: #{size} MiB)") { |x| size = x.to_i }
29
+ opt.on("--no-digest-crc") { no_digest_crc = true }
30
+ opt.on("--no-extlzma") { no_extlzma = true }
31
+ opt.parse!
32
+
33
+ puts <<"EOS"
34
+ *** Benchmark with #{RUBY_DESCRIPTION}.
35
+ EOS
36
+
26
37
  puts " ** preparing #{size} MiB data...\n"
27
38
  #s = SecureRandom.random_bytes(size << 20)
28
39
  s = "0" * (size << 20)
@@ -43,3 +54,7 @@ EOS
43
54
  comparisons.each do |name, meas|
44
55
  puts "%24s : ruby-crc/crc32 = %10.5f : 1.0\n" % [name, crc / meas]
45
56
  end
57
+
58
+ puts <<'EOS'
59
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
60
+ EOS
data/gemstub.rb CHANGED
@@ -1,16 +1,16 @@
1
1
  GEMSTUB = Gem::Specification.new do |s|
2
2
  s.name = "crc"
3
- s.version = "0.1"
3
+ s.version = "0.2"
4
4
  s.summary = "general CRC generator"
5
5
  s.description = <<EOS
6
6
  This is a general CRC (Cyclic Redundancy Check) generator for ruby.
7
- It is written by pure ruby with based on slice-by-eight algorithm (byte-order-free slice-by-16 algorithm).
8
- Included built-in CRC modules are CRC-32, CRC-64-ECMA, CRC-64-ISO, CRC-16-CCITT, CRC-16-IBM, CRC-8, CRC-5-USB, CRC-5-EPC and more.
9
- And your defined CRC modules to use.
7
+ It is written by pure ruby with based on slice-by-eight algorithm (slice-by-16 algorithm as byte-order free and byte-alignment free).
8
+ Included built-in CRC modules are CRC-32, CRC-64-XZ, CRC-64-ECMA, CRC-64-ISO, CRC-16-CCITT, CRC-16-IBM, CRC-8, CRC-5-USB, CRC-5-EPC and many more.
9
+ Additional your customized CRC modules are defined to posible.
10
10
  If you need more speed, please use crc-turbo.
11
11
  EOS
12
12
  s.homepage = "https://osdn.jp/projects/rutsubo/"
13
- s.license = "BSD-2-Clause"
13
+ s.licenses = ["BSD-2-Clause", "Zlib", "CC0-1.0"]
14
14
  s.author = "dearblue"
15
15
  s.email = "dearblue@users.osdn.me"
16
16
 
data/lib/crc.rb CHANGED
@@ -10,6 +10,30 @@ else
10
10
  end
11
11
  end
12
12
 
13
+ #
14
+ # This is a general CRC generator.
15
+ #
16
+ # When you want to use CRC-32 module, there are following ways:
17
+ #
18
+ # 1. Generate CRC-32'd value at direct:
19
+ #
20
+ # CRC.crc32("123456789") # => 3421780262
21
+ #
22
+ # 2. Generate CRC-32'd hex-digest at direct:
23
+ #
24
+ # CRC::CRC32.hexdigest("123456789") # => "CBF43926"
25
+ #
26
+ # 3. Streaming process:
27
+ #
28
+ # crc32 = CRC::CRC32.new # => #<CRC::CRC32:00000000>
29
+ # IO.foreach("/boot/kernel/kernel", nil, 262144, mode: "rb") do |s|
30
+ # crc32 << s
31
+ # end
32
+ # p crc32 # => #<CRC::CRC32:6A632AA5>
33
+ # p crc32.state # => 1784883877
34
+ # p crc32.digest # => "jc*\xA5"
35
+ # p crc32.hexdigest # => "6A632AA5"
36
+ #
13
37
  module CRC
14
38
  CRC = self
15
39
 
@@ -26,67 +50,26 @@ module CRC
26
50
 
27
51
  def bitreflect(num, bitsize)
28
52
  case
29
- when bitsize > 64
53
+ when bitsize > 128
30
54
  bitreflect_reference(num, bitsize)
55
+ when bitsize > 64
56
+ bitreflect128(num) >> (128 - bitsize)
31
57
  when bitsize > 32
32
58
  bitreflect64(num) >> (64 - bitsize)
33
59
  when bitsize > 16
34
60
  bitreflect32(num) >> (32 - bitsize)
35
- when bitsize > 8
61
+ when bitsize > 8
36
62
  bitreflect16(num) >> (16 - bitsize)
37
63
  else
38
- bitreflect8(num) >> ( 8 - bitsize)
39
- end
40
- end
41
-
42
- if false
43
- 20.times do
44
- n = rand(1 << 62)
45
- bitsize = rand(64) + 1
46
- a = bitreflect_reference(n, bitsize)
47
- b = bitreflect(n, bitsize)
48
- puts "0x%016X (%2d) => 0x%016X, 0x%016X (%s)" % [n, bitsize, a, b, (a == b)]
49
- end
50
- puts
51
- require "benchmark"
52
- Benchmark.bm(24) do |bm|
53
- t = 1 << 16
54
- 4.times do
55
- bm.report("reference(-1, 8)") { t.times { bitreflect_reference(-1, 8) } }
56
- bm.report("reference(-1, 16)") { t.times { bitreflect_reference(-1, 16) } }
57
- bm.report("reference(-1, 24)") { t.times { bitreflect_reference(-1, 24) } }
58
- bm.report("reference(-1, 32)") { t.times { bitreflect_reference(-1, 32) } }
59
- bm.report("reference(-1, 64)") { t.times { bitreflect_reference(-1, 64) } }
60
- bm.report("bitreflect(-1, 8)") { t.times { bitreflect(-1, 8) } }
61
- bm.report("bitreflect(-1, 16)") { t.times { bitreflect(-1, 16) } }
62
- bm.report("bitreflect(-1, 24)") { t.times { bitreflect(-1, 24) } }
63
- bm.report("bitreflect(-1, 32)") { t.times { bitreflect(-1, 32) } }
64
- bm.report("bitreflect(-1, 64)") { t.times { bitreflect(-1, 64) } }
65
- puts
66
- end
67
- end
68
- abort "TEST ABORT"
69
- end
70
-
71
- def build_table(bitsize, polynomial)
72
- bitmask = ~(~0 << bitsize)
73
- carrydown = bitmask >> 1
74
- polynomial = bitmask & polynomial
75
- table = []
76
- head = 7
77
- 256.times do |i|
78
- 8.times { i = (i[head] == 0) ? (i << 1) : (((i & carrydown) << 1) ^ polynomial) }
79
- table << i
64
+ bitreflect8(num) >> (8 - bitsize)
80
65
  end
81
-
82
- table.freeze
83
66
  end
84
67
 
85
- def build_table8(bitsize, polynomial, unfreeze = false)
68
+ def build_table(bitsize, polynomial, unfreeze = false, slice: 16)
86
69
  bitmask = ~(~0 << bitsize)
87
70
  table = []
88
71
  Aux.slide_to_head(bitsize, 0, bitmask & polynomial, bitmask) do |xx, poly, csh, head, carries, pad|
89
- 8.times do |s|
72
+ slice.times do |s|
90
73
  table << (t = [])
91
74
  256.times do |b|
92
75
  r = (s == 0 ? (b << csh) : (table[-2][b]))
@@ -102,21 +85,10 @@ module CRC
102
85
  table
103
86
  end
104
87
 
105
- def build_table!(bitsize, polynomial)
106
- polynomial = bitreflect(polynomial, bitsize)
107
- table = []
108
- 256.times do |i|
109
- 8.times { i = (i[0] == 0) ? (i >> 1) : ((i >> 1) ^ polynomial) }
110
- table << i
111
- end
112
-
113
- table.freeze
114
- end
115
-
116
- def build_table8!(bitsize, polynomial, unfreeze = false)
88
+ def build_reflect_table(bitsize, polynomial, unfreeze = false, slice: 16)
117
89
  polynomial = bitreflect(polynomial, bitsize)
118
90
  table = []
119
- 16.times do |s|
91
+ slice.times do |s|
120
92
  table << (t = [])
121
93
  256.times do |b|
122
94
  r = (s == 0) ? b : table[-2][b]
@@ -209,9 +181,8 @@ module CRC
209
181
 
210
182
  def setup(state = nil)
211
183
  state ||= initial_state
212
- state ^= xor_output
213
184
  state = CRC.bitreflect(state, bitsize) if reflect_input ^ reflect_output
214
- state
185
+ state ^ xor_output
215
186
  end
216
187
 
217
188
  def finish(state)
@@ -219,6 +190,9 @@ module CRC
219
190
  state ^ xor_output
220
191
  end
221
192
 
193
+ alias reflect_input? reflect_input
194
+ alias reflect_output? reflect_output
195
+
222
196
  def digest(seq, state = nil)
223
197
  Aux.digest(crc(seq, state), bitsize)
224
198
  end
@@ -257,9 +231,9 @@ module CRC
257
231
  end
258
232
 
259
233
  if nm = name
260
- "#{nm}(CRC-%d-0x%0#{width}X init=%s%s, xor=%s)" % [bitsize, polynomial, init, ref, xor]
234
+ "#{nm}(CRC-%d-0x%0#{width}X%s init=%s, xor=%s)" % [bitsize, polynomial, ref, init, xor]
261
235
  else
262
- "(CRC-%d-0x%0#{width}X init=%s%s, xor=%s)" % [bitsize, polynomial, init, ref, xor]
236
+ "(CRC-%d-0x%0#{width}X%s init=%s, xor=%s)" % [bitsize, polynomial, ref, init, xor]
263
237
  end
264
238
  end
265
239
 
@@ -272,7 +246,7 @@ module CRC
272
246
  end
273
247
  end
274
248
 
275
- class BasicCRC < Struct.new(:internal_state, :initial_state)
249
+ class BasicCRC < Struct.new(:internal_state, :initial_state, :size)
276
250
  BasicStruct = superclass
277
251
 
278
252
  class BasicStruct
@@ -280,38 +254,93 @@ module CRC
280
254
  alias set_state! internal_state=
281
255
  end
282
256
 
283
- def initialize(initial_state = nil)
284
- generator = self.class::GENERATOR
285
- initial_state ||= generator.initial_state
286
- super generator.setup(initial_state), initial_state
257
+ #
258
+ # call-seq:
259
+ # initialize(initial_state = nil, size = 0)
260
+ # initialize(seq, initial_state = nil, size = 0)
261
+ #
262
+ def initialize(*args)
263
+ initialize_args(args) do |seq, initial_state, size|
264
+ g = self.class::GENERATOR
265
+ initial_state ||= g.initial_state
266
+ super g.setup(initial_state.to_i), initial_state.to_i, size.to_i
267
+ update(seq) if seq
268
+ end
287
269
  end
288
270
 
289
- def reset(initial_state = self.initial_state)
290
- generator = self.class::GENERATOR
291
- initial_state ||= generator.initial_state
292
- set_state! generator.setup(initial_state)
271
+ def reset(initial_state = self.initial_state, size = 0)
272
+ g = self.class::GENERATOR
273
+ initial_state ||= g.initial_state
274
+ set_state! g.setup(initial_state)
293
275
  self.initial_state = initial_state
276
+ self.size = size.to_i
294
277
  self
295
278
  end
296
279
 
297
280
  def update(seq)
298
- set_state! self.class::GENERATOR.update!(seq, state!)
281
+ set_state! self.class::GENERATOR.update(seq, state!)
282
+ self.size += seq.bytesize
299
283
  self
300
284
  end
301
285
 
302
286
  alias << update
303
287
 
304
- def finish
288
+ def state
305
289
  self.class::GENERATOR.finish(state!)
306
290
  end
307
291
 
308
- alias state finish
292
+ def +(crc2)
293
+ raise ArgumentError, "not a CRC instance (#{crc2.inspect})" unless crc2.kind_of?(BasicCRC)
294
+ c1 = self.class
295
+ g1 = c1::GENERATOR
296
+ g2 = crc2.class::GENERATOR
297
+ unless g1.bitsize == g2.bitsize &&
298
+ g1.polynomial == g2.polynomial &&
299
+ g1.reflect_input == g2.reflect_input &&
300
+ g1.reflect_output == g2.reflect_output &&
301
+ # g1.initial_state == g2.initial_state &&
302
+ g1.xor_output == g2.xor_output
303
+ raise ArgumentError, "different CRC module (#{g1.inspect} and #{g2.inspect})"
304
+ end
305
+ c1.new(g1.combine(state, crc2.state, crc2.size), size + crc2.size)
306
+ end
307
+
308
+ def ==(a)
309
+ case a
310
+ when BasicCRC
311
+ c1 = self.class
312
+ g1 = c1::GENERATOR
313
+ g2 = a.class::GENERATOR
314
+ if g1.bitsize == g2.bitsize &&
315
+ g1.polynomial == g2.polynomial &&
316
+ g1.reflect_input == g2.reflect_input &&
317
+ g1.reflect_output == g2.reflect_output &&
318
+ # g1.initial_state == g2.initial_state &&
319
+ g1.xor_output == g2.xor_output &&
320
+ state! == a.state!
321
+ true
322
+ else
323
+ false
324
+ end
325
+ when Integer
326
+ state == a
327
+ else
328
+ super
329
+ end
330
+ end
331
+
332
+ alias to_i state
333
+ alias to_int state
334
+
335
+ def to_a
336
+ [state]
337
+ end
309
338
 
310
339
  def digest
311
340
  Aux.DIGEST(state, self.class::GENERATOR.bitsize) { |n| [n].pack("C") }
312
341
  end
313
342
 
314
- # ビット反転せずに値を返す
343
+ # return digest as internal state
315
344
  def digest!
316
345
  Aux.DIGEST(state!, self.class::GENERATOR.bitsize) { |n| [n].pack("C") }
317
346
  end
@@ -320,7 +349,7 @@ module CRC
320
349
  Aux.DIGEST(state, self.class::GENERATOR.bitsize) { |n| "%02X" % n }
321
350
  end
322
351
 
323
- # ビット反転せずに値を返す
352
+ # return hex-digest as internal state
324
353
  def hexdigest!
325
354
  Aux.DIGEST(state!, self.class::GENERATOR.bitsize) { |n| "%02X" % n }
326
355
  end
@@ -337,8 +366,31 @@ module CRC
337
366
  end
338
367
 
339
368
  class << self
340
- def inspect
369
+ alias [] new
370
+
371
+ #
372
+ # call-seq:
373
+ # combine(crc1, crc2) -> new combined crc
374
+ # combine(crc1_int, crc2_int, crc2_len) -> new combined crc
375
+ #
376
+ def combine(crc1, crc2, len2 = nil)
377
+ return crc1 + crc2 if crc1.kind_of?(BasicCRC) && crc2.kind_of?(BasicCRC)
378
+ self::GENERATOR.combine(crc1.to_i, crc2.to_i, len2)
379
+ end
380
+
381
+ def crc(seq, state = nil)
382
+ self::GENERATOR.crc(seq, state)
383
+ end
341
384
 
385
+ def digest(seq, state = nil)
386
+ Aux.digest(self::GENERATOR.crc(seq, state), self::GENERATOR.bitsize)
387
+ end
388
+
389
+ def hexdigest(seq, state = nil)
390
+ Aux.hexdigest(self::GENERATOR.crc(seq, state), self::GENERATOR.bitsize)
391
+ end
392
+
393
+ def inspect
342
394
  if const_defined?(:GENERATOR)
343
395
  if nm = name
344
396
  "#{nm}(#{self::GENERATOR.to_s})"
@@ -353,17 +405,29 @@ module CRC
353
405
  def pretty_inspect(q)
354
406
  q.text inspect
355
407
  end
408
+ end
356
409
 
357
- def crc(seq, state = nil)
358
- self::GENERATOR.crc(seq, state)
359
- end
360
-
361
- def digest(seq, state = nil)
362
- Aux.digest(self::GENERATOR.crc(seq, state), self::GENERATOR.bitsize)
363
- end
364
-
365
- def hexdigest(seq, state = nil)
366
- Aux.hexdigest(self::GENERATOR.crc(seq, state), self::GENERATOR.bitsize)
410
+ private
411
+ def initialize_args(args)
412
+ case args.size
413
+ when 0
414
+ yield nil, nil, 0
415
+ when 1
416
+ if args[0].kind_of?(String)
417
+ yield args[0], nil, 0
418
+ else
419
+ yield nil, args[0], 0
420
+ end
421
+ when 2
422
+ if args[0].kind_of?(String)
423
+ yield args[0], args[1], 0
424
+ else
425
+ yield nil, args[0], args[1].to_i
426
+ end
427
+ when 3
428
+ yield args[0], args[1], args[2].to_i
429
+ else
430
+ raise ArgumentError, "wrong argument size (given #{args.size}, expect 0..3)"
367
431
  end
368
432
  end
369
433
  end
@@ -392,14 +456,60 @@ module CRC
392
456
  end
393
457
 
394
458
  def create_module(bitsize, polynomial, initial_state = 0, refin = true, refout = true, xor = ~0, name = nil)
395
- generator = Generator.new(bitsize, polynomial, initial_state, refin, refout, xor, name)
459
+ g = Generator.new(bitsize, polynomial, initial_state, refin, refout, xor, name)
396
460
  crc = Class.new(BasicCRC)
397
- crc.const_set :GENERATOR, generator
461
+ crc.const_set :GENERATOR, g
398
462
  crc
399
463
  end
400
464
  end
401
465
 
402
- SELF_TEST = ($0 == __FILE__) ? true : false
403
- end
466
+ require_relative "crc/_modules"
467
+ require_relative "crc/_combine"
468
+
469
+ #
470
+ # Create CRC module classes.
471
+ #
472
+ LIST.each do |bitsize, polynomial, refin, refout, initial_state, xor, check, *names|
473
+ names.map! { |nm| nm.freeze }
474
+
475
+ crc = create_module(bitsize, polynomial, initial_state, refin, refout, xor, names[0])
476
+ crc.const_set :NAME, names
477
+
478
+ names.each do |nm|
479
+ nm1 = nm.downcase.gsub(/[\W_]+/, "")
480
+ if MODULE_TABLE.key?(nm1)
481
+ raise NameError, "collision crc-module name: #{nm} (#{crc::GENERATOR} and #{MODULE_TABLE[nm1]::GENERATOR})"
482
+ end
483
+ MODULE_TABLE[nm1] = crc
484
+ end
485
+ name = names[0].sub(/(?<=\bCRC)-(?=\d+)/, "").gsub(/[\W]+/, "_")
486
+ const_set(name, crc)
487
+
488
+ check = Integer(check.to_i) if check
489
+ crc.const_set :CHECK, check
490
+
491
+ g = crc::GENERATOR
492
+ define_singleton_method(name.upcase, ->(*args) { crc.new(*args) })
493
+ define_singleton_method(name.downcase, ->(*args) { g.crc(*args) })
494
+ end
495
+
496
+ if $0 == __FILE__
497
+ $stderr.puts "#{__FILE__}:#{__LINE__}: SELF CHECK for CRC modules (#{File.basename($".grep(/_(?:byruby|turbo)/)[0]||"")})\n"
498
+ MODULE_TABLE.values.uniq.each do |crc|
499
+ g = crc::GENERATOR
500
+ check = crc::CHECK
501
+ checked = g.crc("123456789")
502
+ case check
503
+ when nil
504
+ $stderr.puts "| %20s(\"123456789\") = %16X (check only)\n" % [g.name, checked]
505
+ when checked
506
+ ;
507
+ else
508
+ $stderr.puts "| %20s(\"123456789\") = %16X (expect to %X)\n" % [g.name, checked, check]
509
+ end
510
+ end
511
+ $stderr.puts "#{__FILE__}:#{__LINE__}: DONE SELF CHECK\n"
404
512
 
405
- require_relative "crc/_modules"
513
+ exit
514
+ end
515
+ end
@@ -1,18 +1,26 @@
1
+ #!ruby
2
+
1
3
  #--
2
4
  # Author:: dearblue <dearblue@users.osdn.me>
3
- # License:: BSD-2-Clause
5
+ # License:: Creative Commons License Zero (CC0 / Public Domain)
4
6
  #++
5
7
 
8
+ #
9
+ # \* \* \* \* \* \* \* \*
6
10
  #
7
11
  # Ruby implemented CRC generator.
8
- # It's Based on the Intel's slice-by-eight algorithm (but byte-order-free).
12
+ # It's used slice-by-16 algorithm with byte-order free and byte-alignment free.
13
+ # This is based on the Intel's slice-by-eight algorithm.
9
14
  #
10
15
  # It's faster than about 50% (CRC-32) and about 30% (CRC-64) of
11
16
  # lookup-table algorithm. But need more memory.
12
17
  #
13
18
  # reference:
14
- # * http://www.intel.com/technology/comms/perfnet/download/CRC_generators.pdf
15
- # * xz-utils (src/liblzma/check/crc32_fast.c, src/liblzma/check/crc32_tablegen.c)
19
+ # * https://sourceforge.net/projects/slicing-by-8/
20
+ # * xz-utils
21
+ # * http://tukaani.org/xz/
22
+ # * xz-5.2.2/src/liblzma/check/crc32_fast.c
23
+ # * xz-5.2.2/src/liblzma/check/crc32_tablegen.c
16
24
  #
17
25
  # If defined "RUBY_CRC_NOFAST=2" enviroment variable, switch to lookup-table algorithm.
18
26
  #
@@ -66,7 +74,7 @@ module CRC
66
74
  end
67
75
  end
68
76
 
69
- class Generator < Struct.new(:bitsize, :bitmask, :polynomial, :initial_state, :table8, :reflect_input, :reflect_output, :xor_output, :name)
77
+ class Generator < Struct.new(:bitsize, :bitmask, :polynomial, :initial_state, :table, :reflect_input, :reflect_output, :xor_output, :name)
70
78
  BasicStruct = superclass
71
79
 
72
80
  def initialize(bitsize, polynomial, initial_state = 0, reflect_input = true, reflect_output = true, xor_output = ~0, name = nil)
@@ -108,7 +116,7 @@ module CRC
108
116
  end
109
117
 
110
118
  def update_with_lookup_table(seq, state)
111
- t = table8[0]
119
+ t = table[0]
112
120
 
113
121
  if reflect_input
114
122
  String(seq).each_byte do |ch|
@@ -127,7 +135,7 @@ module CRC
127
135
  end
128
136
 
129
137
  def update_with_slice_by_eight(seq, s)
130
- tX = table8
138
+ tX = table
131
139
  t0 = tX[ 0]; t1 = tX[ 1]; t2 = tX[ 2]; t3 = tX[ 3]
132
140
  t4 = tX[ 4]; t5 = tX[ 5]; t6 = tX[ 6]; t7 = tX[ 7]
133
141
  t8 = tX[ 8]; t9 = tX[ 9]; tA = tX[10]; tB = tX[11]
@@ -140,6 +148,7 @@ module CRC
140
148
  if reflect_input
141
149
  if bitsize <= 32
142
150
  # speed improvement for 32-bits CRC
151
+ i = 0
143
152
  while i < iii
144
153
  s = tF[seq.getbyte(i ) ^ (s ) & 0xff] ^ tE[seq.getbyte(i + 1) ^ (s >> 8) & 0xff] ^
145
154
  tD[seq.getbyte(i + 2) ^ (s >> 16) & 0xff] ^ tC[seq.getbyte(i + 3) ^ (s >> 24) & 0xff] ^
@@ -152,6 +161,7 @@ module CRC
152
161
  i += 16
153
162
  end
154
163
  else
164
+ i = 0
155
165
  while i < iii
156
166
  s = tF[seq.getbyte(i ) ^ (s ) & 0xff] ^ tE[seq.getbyte(i + 1) ^ (s >> 8) & 0xff] ^
157
167
  tD[seq.getbyte(i + 2) ^ (s >> 16) & 0xff] ^ tC[seq.getbyte(i + 3) ^ (s >> 24) & 0xff] ^
@@ -192,16 +202,14 @@ module CRC
192
202
  end
193
203
  end
194
204
 
195
- def table8
196
- unless t = super
197
- if reflect_input
198
- set_table8 t = CRC.build_table8!(bitsize, polynomial)
199
- else
200
- set_table8 t = CRC.build_table8(bitsize, polynomial)
201
- end
205
+ def table
206
+ if reflect_input
207
+ set_table t = CRC.build_reflect_table(bitsize, polynomial, slice: 16)
208
+ else
209
+ set_table t = CRC.build_table(bitsize, polynomial, slice: 16)
202
210
  end
203
211
 
204
- define_singleton_method :table8, self.class.superclass.instance_method(:table8)
212
+ define_singleton_method :table, self.class.superclass.instance_method(:table)
205
213
 
206
214
  t
207
215
  end
@@ -216,10 +224,10 @@ module CRC
216
224
  end
217
225
 
218
226
  class BasicStruct
219
- alias set_table8 table8=
220
- private :set_table8
227
+ alias set_table table=
228
+ private :set_table
221
229
 
222
- undef :bitsize=, :bitmask=, :polynomial=, :initial_state=, :table8=,
230
+ undef :bitsize=, :bitmask=, :polynomial=, :initial_state=, :table=,
223
231
  :reflect_input=, :reflect_output=, :xor_output=, :name=, :[]=
224
232
  end
225
233
  end
@@ -0,0 +1,104 @@
1
+ #!ruby
2
+
3
+ #--
4
+ # This part is based from http://stackoverflow.com/questions/29915764/generic-crc-8-16-32-64-combine-implementation
5
+ #
6
+ # > /* crccomb.c -- generalized combination of CRCs
7
+ # > * Copyright (C) 2015 Mark Adler
8
+ # > * Version 1.1 29 Apr 2015 Mark Adler
9
+ # > */
10
+ # >
11
+ # > /*
12
+ # > This software is provided 'as-is', without any express or implied
13
+ # > warranty. In no event will the author be held liable for any damages
14
+ # > arising from the use of this software.
15
+ # >
16
+ # > Permission is granted to anyone to use this software for any purpose,
17
+ # > including commercial applications, and to alter it and redistribute it
18
+ # > freely, subject to the following restrictions:
19
+ # >
20
+ # > 1. The origin of this software must not be misrepresented; you must not
21
+ # > claim that you wrote the original software. If you use this software
22
+ # > in a product, an acknowledgment in the product documentation would be
23
+ # > appreciated but is not required.
24
+ # > 2. Altered source versions must be plainly marked as such, and must not be
25
+ # > misrepresented as being the original software.
26
+ # > 3. This notice may not be removed or altered from any source distribution.
27
+ # >
28
+ # > Mark Adler
29
+ # > madler@alumni.caltech.edu
30
+ # > */
31
+ #
32
+ # Ported by:: dearblue <dearblue@users.osdn.me>
33
+ # License:: zlib-style
34
+ #--
35
+
36
+ module CRC
37
+ module Aux
38
+ def self.gf2_matrix_times(matrix, vector)
39
+ sum = 0
40
+ matrix.each do |m|
41
+ break unless vector > 0
42
+ sum ^= m unless vector[0] == 0
43
+ vector >>= 1
44
+ end
45
+
46
+ sum
47
+ end
48
+
49
+ def self.gf2_matrix_square(bitsize, square, matrix)
50
+ bitsize.times do |n|
51
+ square[n] = gf2_matrix_times(matrix, matrix[n])
52
+ end
53
+
54
+ nil
55
+ end
56
+ end
57
+
58
+ class Generator
59
+ def combine(crc1, crc2, len2)
60
+ return crc1 unless len2 > 1
61
+
62
+ crc1 ^= initial_state
63
+
64
+ odd = []
65
+ even = []
66
+ if reflect_output
67
+ odd << Utils.bitreflect(polynomial, bitsize)
68
+ col = 1
69
+ (bitsize - 1).times do
70
+ odd << col
71
+ col <<= 1
72
+ end
73
+ else
74
+ col = 2
75
+ (bitsize - 1).times do
76
+ odd << col
77
+ col <<= 1
78
+ end
79
+ odd << polynomial
80
+ end
81
+
82
+ Aux.gf2_matrix_square(bitsize, even, odd)
83
+ Aux.gf2_matrix_square(bitsize, odd, even)
84
+
85
+ while true
86
+ Aux.gf2_matrix_square(bitsize, even, odd)
87
+ if len2[0] == 1
88
+ crc1 = Aux.gf2_matrix_times(even, crc1)
89
+ end
90
+ len2 >>= 1
91
+ break unless len2 > 0
92
+
93
+ Aux.gf2_matrix_square(bitsize, odd, even)
94
+ if len2[0] == 1
95
+ crc1 = Aux.gf2_matrix_times(odd, crc1)
96
+ end
97
+ len2 >>= 1;
98
+ break unless len2 > 0
99
+ end
100
+
101
+ crc1 ^ crc2
102
+ end
103
+ end
104
+ end
@@ -1,105 +1,121 @@
1
1
  #!ruby
2
2
 
3
- module CRC
4
-
5
- #
6
- # references from:
7
- # * https://en.wikipedia.org/wiki/Cyclic_redundancy_check
8
- # * https://ja.wikipedia.org/wiki/%E5%B7%A1%E5%9B%9E%E5%86%97%E9%95%B7%E6%A4%9C%E6%9F%BB
9
- # * http://reveng.sourceforge.net/crc-catalogue/all.htm
10
- # * http://crcmod.sourceforge.net/crcmod.predefined.html
11
- # * https://users.ece.cmu.edu/~koopman/roses/dsn04/koopman04_crc_poly_embedded.pdf
12
- #
3
+ #--
4
+ # Author:: dearblue <dearblue@users.osdn.me>
5
+ # License:: Creative Commons License Zero (CC0 / Public Domain)
6
+ #
7
+ # references from:
8
+ # * https://en.wikipedia.org/wiki/Cyclic_redundancy_check
9
+ # * https://ja.wikipedia.org/wiki/%E5%B7%A1%E5%9B%9E%E5%86%97%E9%95%B7%E6%A4%9C%E6%9F%BB
10
+ # * http://reveng.sourceforge.net/crc-catalogue/all.htm
11
+ # * http://crcmod.sourceforge.net/crcmod.predefined.html
12
+ # * https://github.com/cluelogic/cluelib/blob/master/src/cl_crc.svh
13
+ # * https://users.ece.cmu.edu/~koopman/crc/hw_data.html
14
+ # * https://users.ece.cmu.edu/~koopman/roses/dsn04/koopman04_crc_poly_embedded.pdf
15
+ #++
13
16
 
14
- list = [
17
+ module CRC
18
+ LIST = [
15
19
  #
16
- # module name, polynomial, refrect input,
17
- # bit size, reflect output,
18
- # initial state, xor external, crc("123456789"), alias names...
20
+ # bit size, polynomial, initial state,
21
+ # refrect input, xor output,
22
+ # reflect output, crc("123456789"), names...
19
23
  #
20
- [:CRC1, 0x01, 1, 0, true, true, ~0, nil, "CRC-1"],
21
- [:CRC3_ROHC, 0x03, 3, ~0, true, true, 0, 0x06, "CRC-3-ROHC"],
22
- [:CRC4_ITU, 0x03, 4, 0, true, true, 0, 0x07, "CRC-4-ITU"],
23
- [:CRC5_EPC, 0x09, 5, 0x09, false, false, 0, 0x00, "CRC-5-EPC"],
24
- [:CRC5_ITU, 0x15, 5, 0, true, true, 0, 0x07, "CRC-5-ITU"],
25
- [:CRC5_USB, 0x05, 5, 0, true, true, ~0, 0x19, "CRC-5-USB"],
26
- [:CRC6_CDMA2000_A, 0x27, 6, ~0, false, false, 0, 0x0D, "CRC-6-CDMA2000-A"],
27
- [:CRC6_CDMA2000_B, 0x07, 6, ~0, false, false, 0, 0x3B, "CRC-6-CDMA2000-B"],
28
- [:CRC6_DARC, 0x19, 6, 0, true, true, 0, 0x26, "CRC-6-DARC"],
29
- [:CRC6_ITU, 0x03, 6, 0, true, true, 0, 0x06, "CRC-6-ITU"],
30
- [:CRC7, 0x09, 7, 0, false, false, 0, 0x75, "CRC-7"],
31
- [:CRC7_MVB, 0x65, 7, 0, true, true, ~0, nil, "CRC-7-MVB"],
32
- [:CRC8, 0xD5, 8, 0, true, true, ~0, nil, "CRC-8"],
33
- [:CRC8_CCITT, 0x07, 8, 0, true, true, ~0, nil, "CRC-8-CCITT"],
34
- [:CRC8_DALLAS_MAXIM, 0x31, 8, 0, true, true, ~0, nil, "CRC-8-Dallas/Maxim"],
35
- [:CRC8_DARC, 0x39, 8, 0, true, true, 0, 0x15, "CRC-8-DARC"],
36
- [:CRC8_SAE, 0x1D, 8, 0, true, true, ~0, nil, "CRC-8-SAE"],
37
- [:CRC8_WCDMA, 0x9B, 8, 0, true, true, 0, 0x25, "CRC-8-WCDMA"],
38
- [:CRC10, 0x0233, 10, 0, false, false, 0, 0x0199, "CRC-10"],
39
- [:CRC10_CDMA2000, 0x03D9, 10, ~0, false, false, 0, 0x0233, "CRC-10-CDMA2000"],
40
- [:CRC11, 0x0385, 11, 0x1a, false, false, 0, 0x05a3, "CRC-11"],
41
- [:CRC12, 0x080F, 12, 0, false, true, 0, 0x0daf, "CRC-12"],
42
- [:CRC12_CDMA2000, 0x0F13, 12, ~0, false, false, 0, 0x0d4d, "CRC-12-CDMA2000"],
43
- [:CRC13_BBC, 0x1CF5, 13, 0, false, false, 0, 0x04fa, "CRC-13-BBC"],
44
- [:CRC14_DARC, 0x0805, 14, 0, true, true, 0, 0x082d, "CRC-14-DARC"],
45
- [:CRC15_CAN, 0x4599, 15, 0, false, false, 0, 0x059e, "CRC-15-CAN"],
46
- [:CRC15_MPT1327, 0x6815, 15, 1, false, false, 1, nil, "CRC-15-MPT1327"],
47
- [:CHAKRAVARTY, 0x2F15, 16, 0, true, true, ~0, nil, "Chakravarty"],
48
- [:CRC16_ARINC, 0xA02B, 16, 0, true, true, ~0, nil, "CRC-16-ARINC"],
49
- [:CRC16_CCITT, 0x1021, 16, 0, true, true, ~0, nil, "CRC-16-CCITT", "CRC-CCITT"],
50
- [:CRC16_CDMA2000, 0xC867, 16, 0, true, true, ~0, nil, "CRC-16-CDMA2000"],
51
- [:CRC16_DECT, 0x0589, 16, 0, true, true, ~0, nil, "CRC-16-DECT"],
52
- [:CRC16_T10_DIF, 0x8BB7, 16, 0, true, true, ~0, nil, "CRC-16-T10-DIF"],
53
- [:CRC16_DNP, 0x3D65, 16, 0, true, true, ~0, nil, "CRC-16-DNP"],
54
- [:CRC16_IBM, 0x8005, 16, 0, true, true, ~0, nil, "CRC-16-IBM", "CRC-16", "CRC-16-ANSI"],
55
- [:CRC16_LZH, 0x8005, 16, 0, true, true, 0, 0xBB3D, "CRC-16-LZH", "CRC-LZH"],
56
- [:CRC17_CAN, 0x0001685B, 17, 0, true, true, ~0, nil, "CRC-17-CAN"],
57
- [:CRC21_CAN, 0x00102899, 21, 0, true, true, ~0, nil, "CRC-21-CAN"],
58
- [:CRC24, 0x005D6DCB, 24, 0, true, true, ~0, nil, "CRC-24"],
59
- [:CRC24_RADIX_64, 0x00864CFB, 24, 0, true, true, ~0, nil, "CRC-24-Radix-64"],
60
- [:CRC30, 0x2030B9C7, 30, 0, true, true, ~0, nil, "CRC-30"],
61
- [:CRC32, 0x04c11db7, 32, 0, true, true, ~0, 0xCBF43926, "CRC-32"],
62
- [:CRC32C, 0x1edc6f41, 32, 0, true, true, ~0, 0xE3069283, "CRC-32C"],
63
- [:CRC32K, 0x741B8CD7, 32, 0, true, true, ~0, nil, "CRC-32K"],
64
- [:CRC32K2, 0x32583499, 32, 0, true, true, ~0, nil, "CRC-32K2"],
65
- [:CRC32Q, 0x814141AB, 32, 0, false, false, 0, 0x3010BF7F, "CRC-32Q"],
66
- [:CRC40_GSM, 0x0000000004820009, 40, ~0, false, false, ~0, 0xD4164FC646, "CRC-40-GSM"],
67
- [:CRC64_ECMA, 0x42F0E1EBA9EA3693, 64, 0, true, true, ~0, 0x995DC9BBDF1939FA, "CRC-64-ECMA", "CRC-64"],
68
- [:CRC64_ISO, 0x000000000000001B, 64, 0, true, true, ~0, nil, "CRC-64-ISO"],
24
+ [ 1, 0x01, true, true, 0, ~0, 0x01, "CRC-1"],
25
+ [ 3, 0x03, true, true, ~0, 0, 0x06, "CRC-3-ROHC", "CRC-3-RFC 3095"],
26
+ [ 4, 0x03, false, false, 0, ~0, 0x0b, "CRC-4-INTERLAKEN"],
27
+ [ 4, 0x03, true, true, 0, 0, 0x07, "CRC-4-ITU"],
28
+ [ 5, 0x09, false, false, 0x09, 0, 0x00, "CRC-5-EPC"],
29
+ [ 5, 0x15, true, true, 0, 0, 0x07, "CRC-5-ITU"],
30
+ [ 5, 0x05, true, true, 0, ~0, 0x19, "CRC-5-USB"],
31
+ [ 6, 0x27, false, false, ~0, 0, 0x0D, "CRC-6-CDMA2000-A"],
32
+ [ 6, 0x07, false, false, ~0, 0, 0x3B, "CRC-6-CDMA2000-B"],
33
+ [ 6, 0x19, true, true, 0, 0, 0x26, "CRC-6-DARC"],
34
+ [ 6, 0x03, true, true, 0, 0, 0x06, "CRC-6-ITU"],
35
+ [ 7, 0x09, false, false, 0, 0, 0x75, "CRC-7", "CRC-7-JESD84-A441"],
36
+ [ 7, 0x65, false, false, 0, 0, nil, "CRC-7-MVB"],
37
+ [ 7, 0x4F, true, true, ~0, 0, 0x53, "CRC-7-ROHC", "CRC-7-RFC 3095"],
38
+ [ 7, 0x45, false, false, 0, 0, 0x61, "CRC-7-UMTS"],
39
+ [ 8, 0xD5, false, false, 0, 0, nil, "CRC-8"],
40
+ [ 8, 0x07, false, false, 0, 0, 0xF4, "CRC-8-CCITT", "CRC-8-SMBus"],
41
+ [ 8, 0x31, true, true, 0, 0, 0xA1, "CRC-8-MAXIM", "CRC-8-Dallas/Maxim", "DOW-CRC"],
42
+ [ 8, 0x39, true, true, 0, 0, 0x15, "CRC-8-DARC"],
43
+ [ 8, 0x1D, false, false, 0, ~0, 0x4B, "CRC-8-SAE", "CRC-8-SAE-J1850"],
44
+ [ 8, 0x9B, true, true, 0, 0, 0x25, "CRC-8-WCDMA"],
45
+ [ 8, 0x9B, false, false, ~0, 0, 0xDA, "CRC-8-CDMA2000"],
46
+ [ 8, 0xD5, false, false, 0, 0, 0xBC, "CRC-8-DVB-S2"],
47
+ [ 8, 0x1D, true, true, ~0, 0, 0x97, "CRC-8-EBU", "CRC-8-AES"],
48
+ [ 8, 0x1D, false, false, 0xFD, 0, 0x7E, "CRC-8-I-CODE"],
49
+ [ 8, 0x07, false, false, 0x55, 0x55, 0xA1, "CRC-8-ITU"],
50
+ [ 8, 0x9B, false, false, 0, 0, 0xEA, "CRC-8-LTE"],
51
+ [ 8, 0x07, true, true, ~0, 0, 0xD0, "CRC-8-ROHC", "CRC-8-RFC 3095"],
52
+ [10, 0x0233, false, false, 0, 0, 0x0199, "CRC-10"],
53
+ [10, 0x03D9, false, false, ~0, 0, 0x0233, "CRC-10-CDMA2000"],
54
+ [11, 0x0385, false, false, 0x001A, 0, 0x05A3, "CRC-11"],
55
+ [11, 0x0307, false, false, 0, 0, 0x0061, "CRC-11-UMTS"],
56
+ [12, 0x0F13, false, false, ~0, 0, 0x0D4D, "CRC-12-CDMA2000"],
57
+ [12, 0x080F, false, false, 0, 0, 0x0F5B, "CRC-12-DECT", "X-CRC-12"],
58
+ [12, 0x080F, false, true, 0, 0, 0x0DAF, "CRC-12-UMTS", "CRC-12-3GPP"],
59
+ [13, 0x1CF5, false, false, 0, 0, 0x04FA, "CRC-13-BBC"],
60
+ [14, 0x0805, true, true, 0, 0, 0x082D, "CRC-14-DARC"],
61
+ [15, 0x4599, false, false, 0, 0, 0x059E, "CRC-15", "CRC-15-CAN"],
62
+ [15, 0x6815, false, false, 1, 1, 0x2566, "CRC-15-MPT1327"],
63
+ [16, 0x2F15, false, false, 0, 0, nil, "Chakravarty"],
64
+ [16, 0x8005, true, true, 0, 0, 0xBB3D, "ARC", "CRC-16", "CRC-IBM", "CRC-16-ARC", "CRC-16-LHA"],
65
+ [16, 0xA02B, false, false, 0, 0, nil, "CRC-16-ARINC"],
66
+ [16, 0x1021, false, false, 0x1D0F, 0, 0xE5CC, "CRC-16-AUG-CCITT", "CRC-16-SPI-FUJITSU"],
67
+ [16, 0xC867, false, false, ~0, 0, 0x4C06, "CRC-16-CDMA2000"],
68
+ [16, 0x0589, false, false, 1, 1, 0x007E, "CRC-16-DECT-R", "R-CRC-16"],
69
+ [16, 0x0589, false, false, 0, 0, 0x007F, "CRC-16-DECT-X", "X-CRC-16"],
70
+ [16, 0x8BB7, false, false, 0, 0, 0xD0DB, "CRC-16-T10-DIF"],
71
+ [16, 0x3D65, true, true, ~0, ~0, 0xEA82, "CRC-16-DNP"],
72
+ [16, 0x8005, false, false, 0, 0, 0xFEE8, "CRC-16-BUYPASS", "CRC-16-VERIFONE", "CRC-16-UMTS"],
73
+ [16, 0x1021, false, false, ~0, 0, 0x29B1, "CRC-16-CCITT-FALSE"],
74
+ [16, 0x8005, false, false, 0x800D, 0, 0x9ECF, "CRC-16-DDS-110"],
75
+ [16, 0x3D65, false, false, ~0, ~0, 0xC2B7, "CRC-16-EN-13757"],
76
+ [16, 0x1021, false, false, 0, ~0, 0xD64E, "CRC-16-GENIBUS", "CRC-16-EPC", "CRC-16-I-CODE", "CRC-16-DARC"],
77
+ [16, 0x6F63, false, false, 0, 0, 0xBDF4, "CRC-16-LJ1200"],
78
+ [16, 0x8005, true, true, ~0, ~0, 0x44C2, "CRC-16-MAXIM"],
79
+ [16, 0x1021, true, true, ~0, 0, 0x6F91, "CRC-16-MCRF4XX"],
80
+ [16, 0x1021, true, true, 0xB2AA, 0, 0x63D0, "CRC-16-RIELLO"],
81
+ [16, 0xA097, false, false, 0, 0, 0x0FB3, "CRC-16-TELEDISK"],
82
+ [16, 0x1021, true, true, 0x89EC, 0, 0x26B1, "CRC-16-TMS37157"],
83
+ [16, 0x8005, true, true, 0, ~0, 0xB4C8, "CRC-16-USB"],
84
+ [16, 0x1021, true, true, 0xC6C6, 0, 0xBF05, "CRC-A", "CRC-16-ISO/IEC FCD 14443-3"],
85
+ [16, 0x1021, true, true, 0, 0, 0x2189, "KERMIT", "CRC-16-CCITT", "CRC-16-CCITT-TRUE", "CRC-CCITT"],
86
+ [16, 0x8005, true, true, ~0, 0, 0x4B37, "MODBUS"],
87
+ [16, 0x1021, true, true, 0, ~0, 0x906E, "X-25", "CRC-16-IBM-SDLC", "CRC-16-ISO-HDLC", "CRC-B"],
88
+ [16, 0x1021, false, false, 0, 0, 0x31C3, "XMODEM", "ZMODEM", "CRC-16-ACORN", "CRC-16-LTE"],
89
+ [17, 0x0001685B, false, false, 0, 0, nil, "CRC-17-CAN"],
90
+ [21, 0x00102899, false, false, 0, 0, nil, "CRC-21-CAN"],
91
+ [24, 0x005D6DCB, false, false, 0, 0, nil, "CRC-24"],
92
+ [24, 0x00864CFB, false, false, 0, 0, 0x00CDE703, "CRC-24-Radix-64"],
93
+ [24, 0x00864CFB, false, false, 0x00B704CE, 0, 0x0021CF02, "CRC-24-OPENPGP"],
94
+ [24, 0x0000065B, true, true, 0x00555555, 0, 0x00C25A56, "CRC-24-BLE"],
95
+ [24, 0x005D6DCB, false, false, 0x00FEDCBA, 0, 0x007979BD, "CRC-24-FLEXRAY-A"],
96
+ [24, 0x005D6DCB, false, false, 0x00ABCDEF, 0, 0x001F23B8, "CRC-24-FLEXRAY-B"],
97
+ [24, 0x00328B63, false, false, 0, ~0, 0x00B4F3E6, "CRC-24-INTERLAKEN"],
98
+ [24, 0x00864CFB, false, false, 0, 0, 0x00CDE703, "CRC-24-LTE-A"],
99
+ [24, 0x00800063, false, false, 0, 0, 0x0023EF52, "CRC-24-LTE-B"],
100
+ [30, 0x2030B9C7, false, false, 0, 0, nil, "CRC-30"],
101
+ [30, 0x2030B9C7, false, false, 0, ~0, 0x04C34ABF, "CRC-30-CDMA"],
102
+ [31, 0x04C11DB7, false, false, 0, ~0, 0x0CE9E46C, "CRC-31-PHILIPS"],
103
+ [32, 0x04C11DB7, true, true, 0, ~0, 0xCBF43926, "CRC-32", "CRC-32-ADCCP", "PKZIP", "CRC-32-PKZIP"],
104
+ [32, 0x04C11DB7, false, false, 0, ~0, 0xFC891918, "CRC-32-BZIP2", "CRC-32-AAL5", "CRC-32-DECT-B", "B-CRC-32"],
105
+ [32, 0x1EDC6F41, true, true, 0, ~0, 0xE3069283, "CRC-32C", "CRC-32-ISCSI", "CRC-32-CASTAGNOLI", "CRC-32-INTERLAKEN"],
106
+ [32, 0xa833982b, true, true, 0, ~0, 0x87315576, "CRC-32D"],
107
+ [32, 0x04C11DB7, false, false, ~0, 0, 0x0376E6E7, "CRC-32-MPEG-2"],
108
+ [32, 0x04C11DB7, false, false, ~0, ~0, 0x765E7680, "CRC-32-POSIX", "CKSUM"],
109
+ [32, 0x741B8CD7, true, true, 0, ~0, nil, "CRC-32K"],
110
+ [32, 0x32583499, true, true, 0, ~0, nil, "CRC-32K2"],
111
+ [32, 0x814141AB, false, false, 0, 0, 0x3010BF7F, "CRC-32Q"],
112
+ [32, 0x04C11DB7, true, true, ~0, 0, 0x340BC6D9, "JAMCRC", "CRC-32-JAMCRC"],
113
+ [32, 0x000000AF, false, false, 0, 0, 0xBD0BE338, "XFER", "CRC-32-XFER"],
114
+ [40, 0x0004820009, false, false, ~0, ~0, 0xD4164FC646, "CRC-40-GSM"],
115
+ [64, 0x42F0E1EBA9EA3693, true, true, 0, ~0, 0x995DC9BBDF1939FA, "CRC-64", "CRC-64-XZ"],
116
+ [64, 0x42F0E1EBA9EA3693, false, false, 0, 0, 0x6C40DF5F0B497347, "CRC-64-ECMA", "CRC-64-ECMA-182"],
117
+ [64, 0x42F0E1EBA9EA3693, false, false, 0, ~0, 0x62EC59E3F1A4F00A, "CRC-64-WE"],
118
+ [64, 0x000000000000001B, false, false, 0, 0, nil, "CRC-64-ISO"],
119
+ # [82, 0x308C0111011401440411, true, true, 0, 0, 0x9EA83F625023801FD612, "CRC-82/DARC"],
69
120
  ]
70
-
71
- $stderr.puts "#{__FILE__}:#{__LINE__}: SELF CHECK for CRC modules (#{File.basename($".grep(/\/crc\/_(?:byruby|turbo)/)[0]||"")})\n" if SELF_TEST
72
- list.each do |name, polynomial, bitsize, initial_state, refin, refout, xor, check, *names|
73
- names.map! { |nm| nm.freeze }
74
-
75
- crc = create_module(bitsize, polynomial, initial_state, refin, refout, xor, names[0])
76
-
77
- const_set(name, crc)
78
- names.each { |nm| MODULE_TABLE[nm.downcase.gsub(/[\W_]+/, "")] = crc }
79
-
80
- check = Integer(check.to_i) if check
81
- crc.const_set :CHECK, check
82
-
83
- generator = crc::GENERATOR
84
- define_singleton_method(name.downcase, ->(*args) { generator.crc(*args) })
85
- define_singleton_method("#{name.downcase}_digest", ->(*args) { generator.digest(*args) })
86
- define_singleton_method("#{name.downcase}_hexdigest", ->(*args) { generator.hexdigest(*args) })
87
-
88
- if SELF_TEST
89
- checked = generator.crc("123456789")
90
- case
91
- when check.nil?
92
- $stderr.puts "| %20s(\"123456789\") = %16X (check only)\n" % [names[0], checked]
93
- when check != checked
94
- $stderr.puts "| %20s(\"123456789\") = %16X (expect to %016X)\n" % [names[0], checked, check]
95
- end
96
- end
97
- end
98
- exit if SELF_TEST
99
-
100
- class << self
101
- alias crc64 crc64_ecma
102
- end
103
-
104
- CRC64 = CRC64_ECMA
105
121
  end
@@ -8,7 +8,11 @@ module CRC
8
8
  end
9
9
  bitmask = ~(~0 << bitsize0)
10
10
  crc &= bitmask
11
- [polynomial, Utils.bitreflect(polynomial, bitsize0)].each do |poly|
11
+ results = []
12
+ poly = Array(polynomial)
13
+ #poly += poly.map { |po| Utils.bitreflect(po, bitsize0) }
14
+ #poly.uniq!
15
+ poly.each do |poly|
12
16
  poly &= bitmask
13
17
  [false, true].each do |refin|
14
18
  [false, true].each do |refout|
@@ -17,13 +21,13 @@ module CRC
17
21
  Array(initstate).each do |init|
18
22
  init &= bitmask
19
23
  mod = CRC.create_module(bitsize0, poly, init, refin, refout, xormask)
20
- return mod if mod.crc(seq) == crc
24
+ results << mod if mod.crc(seq) == crc
21
25
  end
22
26
  end
23
27
  end
24
28
  end
25
29
  end
26
30
 
27
- nil
31
+ results
28
32
  end
29
33
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: crc
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.1'
4
+ version: '0.2'
5
5
  platform: ruby
6
6
  authors:
7
7
  - dearblue
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-08 00:00:00.000000000 Z
11
+ date: 2016-05-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -26,21 +26,24 @@ dependencies:
26
26
  version: '11.0'
27
27
  description: |
28
28
  This is a general CRC (Cyclic Redundancy Check) generator for ruby.
29
- It is written by pure ruby with based on slice-by-eight algorithm (byte-order-free slice-by-16 algorithm).
30
- Included built-in CRC modules are CRC-32, CRC-64-ECMA, CRC-64-ISO, CRC-16-CCITT, CRC-16-IBM, CRC-8, CRC-5-USB, CRC-5-EPC and more.
31
- And your defined CRC modules to use.
29
+ It is written by pure ruby with based on slice-by-eight algorithm (slice-by-16 algorithm as byte-order free and byte-alignment free).
30
+ Included built-in CRC modules are CRC-32, CRC-64-XZ, CRC-64-ECMA, CRC-64-ISO, CRC-16-CCITT, CRC-16-IBM, CRC-8, CRC-5-USB, CRC-5-EPC and many more.
31
+ Additional your customized CRC modules are defined to posible.
32
32
  If you need more speed, please use crc-turbo.
33
33
  email: dearblue@users.osdn.me
34
34
  executables: []
35
35
  extensions: []
36
36
  extra_rdoc_files:
37
+ - HISTORY.ja.md
37
38
  - LICENSE
38
39
  - README.md
39
40
  - lib/crc.rb
40
41
  - lib/crc/_byruby.rb
42
+ - lib/crc/_combine.rb
41
43
  - lib/crc/_modules.rb
42
44
  - lib/crc/finder.rb
43
45
  files:
46
+ - HISTORY.ja.md
44
47
  - LICENSE
45
48
  - README.md
46
49
  - Rakefile
@@ -48,11 +51,14 @@ files:
48
51
  - gemstub.rb
49
52
  - lib/crc.rb
50
53
  - lib/crc/_byruby.rb
54
+ - lib/crc/_combine.rb
51
55
  - lib/crc/_modules.rb
52
56
  - lib/crc/finder.rb
53
57
  homepage: https://osdn.jp/projects/rutsubo/
54
58
  licenses:
55
59
  - BSD-2-Clause
60
+ - Zlib
61
+ - CC0-1.0
56
62
  metadata: {}
57
63
  post_install_message:
58
64
  rdoc_options:
@@ -74,7 +80,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
74
80
  version: '0'
75
81
  requirements: []
76
82
  rubyforge_project:
77
- rubygems_version: 2.6.2
83
+ rubygems_version: 2.6.4
78
84
  signing_key:
79
85
  specification_version: 4
80
86
  summary: general CRC generator