crc 0.4.0.1 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 69461cc84e80b500fcae459c0b4ce19d99bdb92f
4
- data.tar.gz: 13212d4202a2f3ac678c6e6345226fc3220e391b
3
+ metadata.gz: e8e6fc81bbcef03f0e899ac1938b7535ddaa344a
4
+ data.tar.gz: 254fc51f632a763e49fd0d9483cb28c8265fbda3
5
5
  SHA512:
6
- metadata.gz: 542043f97a8cb06d3afa1487fc7169ccb1d4db5dd6cb0674e76c6548f7fcdac35108a3cbf177f9e19e41286438f20d9c2f7fdd0e646a12a36aac243cbc101567
7
- data.tar.gz: fbaf27266779e6da9b536ae65e3e07c8775bfbd9ea91ad515e831ec29417a92060b7db293229d963643f4e5ad411916fd91f95bb2fa0fabab3a77c2736018f4b
6
+ metadata.gz: c2a19eac87f476a4a3fec5c1646d465d689a36ce5e04782a978d4acd8823823c130e4c26deaa19ac1d78881151c28641f2456f4a41b6c48d8ba21290af0a79e5
7
+ data.tar.gz: ea0a58a84d5dce2c66e2f3ca9a3f2374d249034c64b54f138aa1bc451ece1a1e31b3d7c69f62f1b5f22227bea2801b27b1a32ed51e7af62193892626eed45097
@@ -2,6 +2,12 @@ This document is written in Japanese.
2
2
 
3
3
  # crc for ruby の更新履歴
4
4
 
5
+ ## crc-0.4.1 (平成29年4月1日 土曜日)
6
+
7
+ * rbcrc -lvv の表示に、ビット反転多項式、相反多項式、初期内部状態、ビット反転魔法数を追加
8
+ * ``dump_to_c`` に slicing-by-2 から slicing-by-999 までの出力機能を追加
9
+
10
+
5
11
  ## crc-0.4.0.1 (平成29年3月28日 火曜日)
6
12
 
7
13
  * acrc、shift 系メソッドのバグ修正
data/README.md CHANGED
@@ -20,7 +20,7 @@ If you need more speed, please use with [crc-turbo](https://rubygems/gems/crc-tu
20
20
  * author: dearblue (mailto:dearblue@users.noreply.github.com)
21
21
  * report issue to: <https://github.com/dearblue/ruby-crc/issues>
22
22
  * how to install: ``gem install crc``
23
- * version: 0.4.0.1
23
+ * version: 0.4.1
24
24
  * production quality: TECHNICAL PREVIEW
25
25
  * licensing:
26
26
  * ***BSD-2-Clause : MAIN LICENSE***
@@ -42,8 +42,8 @@ This examples are used CRC-32 module. Please see CRC for more details.
42
42
  * ``CRC.crc32(seq, init = CRC::CRC32.initial_crc) => crc-32 integer`` (likely as ``Zlib.crc32``)
43
43
  * ``CRC.crc32.crc(seq, init = CRC::CRC32.initial_crc) => crc-32 integer`` (likely as ``Zlib.crc32``)
44
44
  * ``CRC.crc32.digest(seq, init = CRC::CRC32.initial_crc) => crc-32 digest`` (likely as ``Digest::XXXX.digest``)
45
- * ``CRC.crc32.hexdigest(seq, init = 0) -> crc-32 hex-digest`` (likely as ``Digest::XXXX.hexdigest``)
46
- * ``CRC.crc32[seq, init = 0, current_length = 0] -> crc-32 calcurator``
45
+ * ``CRC.crc32.hexdigest(seq, init = CRC::CRC32.initial_crc) -> crc-32 hex-digest`` (likely as ``Digest::XXXX.hexdigest``)
46
+ * ``CRC.crc32[seq, init = CRC::CRC32.initial_crc, current_length = 0] -> crc-32 calcurator``
47
47
 
48
48
  ### Calcurate by streaming
49
49
 
@@ -170,7 +170,7 @@ About LICENSE for generated source code:
170
170
 
171
171
  Algorithms (C file type only):
172
172
  bit-by-bit, bit-by-bit-fast, halfbyte-table, standard-table,
173
- slicing-by-4, slicing-by-8, slicing-by-16
173
+ slicing-by-4, slicing-by-8, slicing-by-16, slicing-by-{2..999}
174
174
 
175
175
  Support export file types:
176
176
  * .c for C (support C89, but required ``stdint.h'')
@@ -207,13 +207,14 @@ And, this command has feature too that is print for each CRC specifications as Y
207
207
  $ rbcrc -lvv
208
208
  ...snip...
209
209
  "CRC-32":
210
- bitsize: 32
211
- polynomial: 0x04C11DB7
212
- reflect input: true
213
- reflect output: true
214
- initial crc: 0x00000000
215
- xor output: 0xFFFFFFFF
216
- magic number: 0x2144DF1C
210
+ bitsize: 32
211
+ polynomial: 0x04C11DB7 # 0xEDB88320 (bit reflected)
212
+ reversed reciprocal: 0x82608EDB # 0xDB710641 (bit reflected)
213
+ reflect input: true
214
+ reflect output: true
215
+ initial crc: 0x00000000 # 0xFFFFFFFF (initial state)
216
+ xor output: 0xFFFFFFFF
217
+ magic number: 0x2144DF1C # 0xDEBB20E3 (internal state)
217
218
  another names:
218
219
  - "CRC-32-ADCCP"
219
220
  - "CRC-32-PKZIP"
data/bin/rbcrc CHANGED
@@ -68,16 +68,18 @@ OptionParser.new("usage: #{File.basename $0} [options] output-filename...", 12,
68
68
  "bitbybitfast" => CRC::ALGORITHM_BITBYBIT_FAST,
69
69
  "halfbytetable" => CRC::ALGORITHM_HALFBYTE_TABLE,
70
70
  "standardtable" => CRC::ALGORITHM_STANDARD_TABLE,
71
- "sliceby4" => CRC::ALGORITHM_SLICING_BY_4,
72
- "slicingby4" => CRC::ALGORITHM_SLICING_BY_4,
73
- "sliceby8" => CRC::ALGORITHM_SLICING_BY_8,
74
- "slicingby8" => CRC::ALGORITHM_SLICING_BY_8,
75
- "sliceby16" => CRC::ALGORITHM_SLICING_BY_16,
76
- "slicingby16" => CRC::ALGORITHM_SLICING_BY_16,
77
71
  }[xx]
78
72
  unless algorithm
79
- raise OptionParser::InvalidOption,
80
- "wrong algorithm name - ``#{x}'' (except bit-by-bit, bit-by-bit-fast, halfbyte-table, standard-table, slicing-by-4, slicing-by-8, slicing-by-16)"
73
+ unless xx =~ /\A(?:slic(?:e|ing)by|sb)(\d+)\z/
74
+ raise OptionParser::InvalidOption,
75
+ "wrong algorithm name - ``#{x}'' (except bit-by-bit, bit-by-bit-fast, halfbyte-table, standard-table or slicing-by-{2..999})"
76
+ end
77
+
78
+ algorithm = $1.to_i
79
+ unless algorithm >= 2 && algorithm <= 999
80
+ raise OptionParser::InvalidOption,
81
+ "wrong number for slicing-by-N (given #$1, expect 2..999)"
82
+ end
81
83
  end
82
84
  }
83
85
 
@@ -96,7 +98,7 @@ About LICENSE for generated source code:
96
98
 
97
99
  Algorithms (C file type only):
98
100
  bit-by-bit, bit-by-bit-fast, halfbyte-table, standard-table,
99
- slicing-by-4, slicing-by-8, slicing-by-16
101
+ slicing-by-4, slicing-by-8, slicing-by-16, slicing-by-{2..999}
100
102
 
101
103
  Support export file types:
102
104
  * .c for C (support C89, but required ``stdint.h'')
@@ -106,22 +108,22 @@ Support export file types:
106
108
  (executable for mruby and limitation bitsize by fixnum)
107
109
 
108
110
  examples:
109
- * create crc-32 generator to c source (and header file)
111
+ * create crc-32 calcurator to c source (and header file)
110
112
  $ rbcrc crc32.c
111
113
 
112
- * create crc-32c generator to ruby source
114
+ * create crc-32c calcurator to ruby source
113
115
  $ rbcrc crc32c.rb
114
116
 
115
- * create crc-30-cdma generator to javascript source
117
+ * create crc-30-cdma calcurator to javascript source
116
118
  $ rbcrc crc30cdma.js
117
119
 
118
- * create crc-32 generator to ``crc.c'', ``crc.rb'' and ``crc.js''
120
+ * create crc-32 calcurator to ``crc.c'', ``crc.rb'' and ``crc.js''
119
121
  $ rbcrc -mcrc32 crc.c crc.rb crc.js
120
122
 
121
- * create customized crc generator (as mycrc function) to ``mycrc.c''
123
+ * create customized crc calcurator (as mycrc function) to ``mycrc.c''
122
124
  $ rbcrc -s15 -p0x6789 -io -x~0 mycrc.c
123
125
 
124
- * create customized crc generator (as MyCRC class) to ``mycrc_1.rb''
126
+ * create customized crc calcurator (as MyCRC class) to ``mycrc_1.rb''
125
127
  $ rbcrc -s39 -p0x987654321 -IO -x1 -nMyCRC mycrc_1.rb
126
128
  EOS
127
129
 
@@ -195,26 +197,35 @@ begin
195
197
  bytesize = (e[0] + 7) / 8
196
198
  bitmask = ~(~0 << e[0])
197
199
  hex = ->(n) { "0x%0*X" % [bytesize * 2, bitmask & n] }
198
- names = e[8 .. -1]
199
- if names.empty?
200
- names = nil
201
- else
202
- names = <<-YAML_FORMAT.chomp!
200
+ revrec = (e[1] >> 1) | (1 << (e[0] - 1)) # reversed reciprocal
201
+ if e[2]
202
+ refpoly = " \# #{hex[CRC.bitreflect(e[1], e[0])]} (bit reflected)"
203
+ recpoly = " \# #{hex[CRC.bitreflect(revrec, e[0])]} (bit reflected)"
204
+ end
205
+ unless e[5] == 0
206
+ internalstate = " \# #{hex[e[4] ^ e[5]]} (initial state)"
207
+ internalmagic = " \# #{hex[CRC[e[7]].magicnumber ^ e[5]]} (internal state)"
208
+ end
209
+ spec = <<-YAML
210
+ "#{e[7]}":
211
+ bitsize: #{e[0]}
212
+ polynomial: #{hex[e[1]]}#{refpoly}
213
+ reversed reciprocal: #{hex[revrec]}#{recpoly}
214
+ reflect input: #{e[2].inspect}
215
+ reflect output: #{e[3].inspect}
216
+ initial crc: #{hex[e[4]]}#{internalstate}
217
+ xor output: #{hex[e[5]]}
218
+ magic number: #{hex[CRC[e[7]].magicnumber]}#{internalmagic}
219
+ YAML
203
220
 
221
+ unless (names = e[8 .. -1]).empty?
222
+ spec << <<-YAML
204
223
  another names:
205
224
  - "#{names.join(%("\n - "))}"
206
- YAML_FORMAT
225
+ YAML
207
226
  end
208
- puts <<-YAML_FORMAT
209
- "#{e[7]}":
210
- bitsize: #{e[0]}
211
- polynomial: #{hex[e[1]]}
212
- reflect input: #{e[2].inspect}
213
- reflect output: #{e[3].inspect}
214
- initial crc: #{hex[e[4]]}
215
- xor output: #{hex[e[5]]}
216
- magic number: #{hex[CRC[e[7]].magicnumber]}#{names}
217
- YAML_FORMAT
227
+
228
+ puts spec
218
229
  end
219
230
  end
220
231
  else
@@ -243,8 +254,9 @@ begin
243
254
  normalin.nil? ? wcrc.reflect_input? : !normalin,
244
255
  normalout.nil? ? wcrc.reflect_output? : !normalout,
245
256
  wxor)
246
- wcrcname = crcname || basename
247
257
  end
258
+
259
+ wcrcname = crcname || basename
248
260
  end
249
261
 
250
262
  case type
@@ -0,0 +1,295 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "crc/codegen"
4
+ require "optparse"
5
+
6
+ def parse_number(t)
7
+ case t.strip
8
+ when /^~(\w+)$/
9
+ ~Integer($1)
10
+ else
11
+ Integer(t)
12
+ end
13
+ end
14
+
15
+ mode = nil
16
+ crc = nil
17
+ crcname = nil
18
+ bitsize = nil
19
+ polynomial = nil
20
+ initcrc = nil
21
+ initstate = nil
22
+ xormask = nil
23
+ normalin = nil
24
+ normalout = nil
25
+ algorithm = CRC::ALGORITHM_SLICING_BY_16
26
+ forceoverwrite = false
27
+ verbose = 1
28
+
29
+ OptionParser.new("usage: #{File.basename $0} [options] output-filename...", 12, " ").instance_eval do
30
+ on("-m crcname", "choose included crc name in library (``-l'' to print list)") do |x|
31
+ begin
32
+ crc = CRC.lookup(x)
33
+ rescue
34
+ raise OptionParser::InvalidOption,
35
+ "not matched crc name - #{x.strip} (if check name, use ``-l'' switch)"
36
+ end
37
+ end
38
+ on("-n crcname", "declare function name or class name [DEFAULT is filename]") { |x| crcname = x.strip }
39
+ on("-s bitsize", "declare crc bit size [REQUIRED for customized crc]") do |x|
40
+ bitsize = x.to_i
41
+ unless bitsize >= 1 && bitsize <= 64
42
+ raise OptionParser::InvalidOption,
43
+ "wrong bitsize (given #{bitsize}, expect 1..64)"
44
+ end
45
+ end
46
+ on("-p polynom", "declare crc polynomial [REQUIRED for customized crc]") do |x|
47
+ polynomial = Integer(x)
48
+ end
49
+ on("-c initcrc", "declare initial crc (not internal state) [DEFAULT: 0]") do |x|
50
+ initcrc = parse_number(x)
51
+ initstate = nil
52
+ end
53
+ on("-S initstate", " declare initial state (internal state) [DEFAULT: unset]") do |x|
54
+ initcrc = nil
55
+ initstate = parse_number(x)
56
+ end
57
+ on("-x xormask", "declare xor bit mask for when output [DEFAULT: ~0]") do |x|
58
+ xormask = parse_number(x)
59
+ end
60
+ on("-i", "reflect input [DEFAULT]") { |x| normalin = false }
61
+ on("-I", "normal input (not reflect)") { |x| normalin = true }
62
+ on("-o", "reflect output [DEFAULT]") { |x| normalout = false }
63
+ on("-O", "normal output (not reflect)") { |x| normalout = true }
64
+ on("-a algorithm", " switch algorithm (see below) (C file type only)") { |x|
65
+ xx = x.downcase.gsub(/[^0-9a-z]+/m, "")
66
+ algorithm = {
67
+ "bitbybit" => CRC::ALGORITHM_BITBYBIT,
68
+ "bitbybitfast" => CRC::ALGORITHM_BITBYBIT_FAST,
69
+ "halfbytetable" => CRC::ALGORITHM_HALFBYTE_TABLE,
70
+ "standardtable" => CRC::ALGORITHM_STANDARD_TABLE,
71
+ "sliceby4" => CRC::ALGORITHM_SLICING_BY_4,
72
+ "slicingby4" => CRC::ALGORITHM_SLICING_BY_4,
73
+ "sliceby8" => CRC::ALGORITHM_SLICING_BY_8,
74
+ "slicingby8" => CRC::ALGORITHM_SLICING_BY_8,
75
+ "sliceby16" => CRC::ALGORITHM_SLICING_BY_16,
76
+ "slicingby16" => CRC::ALGORITHM_SLICING_BY_16,
77
+ }[xx]
78
+ unless algorithm
79
+ raise OptionParser::InvalidOption,
80
+ "wrong algorithm name - ``#{x}'' (except bit-by-bit, bit-by-bit-fast, halfbyte-table, standard-table, slicing-by-4, slicing-by-8, slicing-by-16)"
81
+ end
82
+ }
83
+
84
+ separator ""
85
+
86
+ on("-l", "print crc names") { mode = :list }
87
+ on("-f", "force overwrite") { forceoverwrite = true }
88
+ on("-v", "increment verbosery level") { verbose += 1 }
89
+ on("-q", "quiet mode (reset verbosery level to zero)") { verbose = 0 }
90
+
91
+ separator <<-EOS
92
+
93
+ About LICENSE for generated source code:
94
+ Generated code is under Creative Commons License Zero (CC0 / Public Domain).
95
+ See https://creativecommons.org/publicdomain/zero/1.0/
96
+
97
+ Algorithms (C file type only):
98
+ bit-by-bit, bit-by-bit-fast, halfbyte-table, standard-table,
99
+ slicing-by-4, slicing-by-8, slicing-by-16
100
+
101
+ Support export file types:
102
+ * .c for C (support C89, but required ``stdint.h'')
103
+ * .js for javascript (required ECMAScript 6th edition)
104
+ * .rb for ruby (for ruby-2.1+, jruby, and rubinius)
105
+ (executable for ruby-1.8, ruby-1.9 and ruby-2.0)
106
+ (executable for mruby and limitation bitsize by fixnum)
107
+
108
+ examples:
109
+ * create crc-32 generator to c source (and header file)
110
+ $ rbcrc crc32.c
111
+
112
+ * create crc-32c generator to ruby source
113
+ $ rbcrc crc32c.rb
114
+
115
+ * create crc-30-cdma generator to javascript source
116
+ $ rbcrc crc30cdma.js
117
+
118
+ * create crc-32 generator to ``crc.c'', ``crc.rb'' and ``crc.js''
119
+ $ rbcrc -mcrc32 crc.c crc.rb crc.js
120
+
121
+ * create customized crc generator (as mycrc function) to ``mycrc.c''
122
+ $ rbcrc -s15 -p0x6789 -io -x~0 mycrc.c
123
+
124
+ * create customized crc generator (as MyCRC class) to ``mycrc_1.rb''
125
+ $ rbcrc -s39 -p0x987654321 -IO -x1 -nMyCRC mycrc_1.rb
126
+ EOS
127
+
128
+ begin
129
+ parse!
130
+
131
+ if bitsize && polynomial.nil?
132
+ raise OptionParser::InvalidOption,
133
+ "given ``-s'' switch only (need ``-p'' switch too)"
134
+ end
135
+ rescue OptionParser::InvalidOption
136
+ $stderr.puts <<-ERRORTEXT
137
+ #$0: #$! (#{$!.class})
138
+ \tor enter ``#$0 --help'' to print help.
139
+ ERRORTEXT
140
+ exit 1
141
+ end
142
+
143
+ if ARGV.empty? && mode.nil?
144
+ puts help
145
+ exit 1
146
+ end
147
+ end
148
+
149
+ def write_to_file(forceoverwrite, *filenames)
150
+ unless forceoverwrite
151
+ isexist = false
152
+ filenames.each do |nm|
153
+ next unless File.exist?(nm)
154
+ $stderr.puts "#$0: file exist - #{nm} (please use ``-f'' switch, if you want to overwrite)\n"
155
+ isexist = true
156
+ end
157
+
158
+ return nil if isexist
159
+ end
160
+
161
+ begin
162
+ cleanup = true
163
+ files = filenames.map do |nm|
164
+ File.open(nm, "ab") { } # write test
165
+ fd = File.open(nm + "~", "wb")
166
+ fd.define_singleton_method(:path, -> { nm })
167
+ fd
168
+ end
169
+ yield *files
170
+ cleanup = false
171
+ ensure
172
+ if files
173
+ if cleanup
174
+ files.each { |fd| fd.close rescue nil; File.unlink fd.path + "~" rescue nil }
175
+ else
176
+ files.each { |fd| fd.close; File.rename fd.path + "~", fd.path }
177
+ end
178
+ end
179
+ end
180
+ end
181
+
182
+ begin
183
+ case mode
184
+ when :list
185
+ case verbose
186
+ when 0
187
+ puts CRC::LIST.map {|e| e[7] }.join(", ")
188
+ when 1
189
+ puts CRC::LIST.map {|e| e[7 .. -1] }.join(", ")
190
+ when 2
191
+ puts CRC::LIST.map {|e| e[7 .. -1].join(", ") }.join("\n")
192
+ else
193
+ puts "## This is YAML format\n"
194
+ CRC::LIST.each do |e|
195
+ bytesize = (e[0] + 7) / 8
196
+ bitmask = ~(~0 << e[0])
197
+ hex = ->(n) { "0x%0*X" % [bytesize * 2, bitmask & n] }
198
+ revrec = (e[1] >> 1) | (1 << (e[0] - 1)) # reversed reciprocal
199
+ if e[2]
200
+ refpoly = " \# #{hex[CRC.bitreflect(e[1], e[0])]} (bit reflected)"
201
+ recpoly = " \# #{hex[CRC.bitreflect(revrec, e[0])]} (bit reflected)"
202
+ end
203
+ unless e[5] == 0
204
+ internalstate = " \# #{hex[e[4] ^ e[5]]} (initial state)"
205
+ internalmagic = " \# #{hex[CRC[e[7]].magicnumber ^ e[5]]} (internal state)"
206
+ end
207
+ spec = <<-YAML
208
+ "#{e[7]}":
209
+ bitsize: #{e[0]}
210
+ polynomial: #{hex[e[1]]}#{refpoly}
211
+ reversed reciprocal: #{hex[revrec]}#{recpoly}
212
+ reflect input: #{e[2].inspect}
213
+ reflect output: #{e[3].inspect}
214
+ initial crc: #{hex[e[4]]}#{internalstate}
215
+ xor output: #{hex[e[5]]}
216
+ magic number: #{hex[CRC[e[7]].magicnumber]}#{internalmagic}
217
+ YAML
218
+
219
+ unless (names = e[8 .. -1]).empty?
220
+ spec << <<-YAML
221
+ another names:
222
+ - "#{names.join(%("\n - "))}"
223
+ YAML
224
+ end
225
+
226
+ puts spec
227
+ end
228
+ end
229
+ else
230
+ ARGV.each do |path|
231
+ begin
232
+ basename = File.basename path, ".*"
233
+ type = File.extname path
234
+
235
+ if bitsize
236
+ wxor = xormask || ~0
237
+ wcrc = crc || CRC.new(bitsize, polynomial,
238
+ initcrc || (initstate ? (initstate ^ wxor) : 0),
239
+ !normalin, !normalout, wxor)
240
+ wcrcname = crcname || basename
241
+ else
242
+ begin
243
+ wcrc = crc || CRC.lookup(basename)
244
+ rescue
245
+ raise "not matched crc name from filename - #{basename} (if you want check name, use ``-l'' switch)"
246
+ end
247
+
248
+ if initcrc || initstate || xormask || !normalin.nil? || !normalout.nil?
249
+ wxor = xormask || wcrc.xor_output
250
+ wcrc = CRC.new(wcrc.bitsize, wcrc.polynomial,
251
+ initcrc || (initstate ? (initstate ^ wxor) : wcrc.initial_crc),
252
+ normalin.nil? ? wcrc.reflect_input? : !normalin,
253
+ normalout.nil? ? wcrc.reflect_output? : !normalout,
254
+ wxor)
255
+ end
256
+
257
+ wcrcname = crcname || basename
258
+ end
259
+
260
+ case type
261
+ when ".c"
262
+ write_to_file(forceoverwrite, path.sub(/\.c$/i, ".h"), path) do |cheader, csource|
263
+ code = wcrc.dump_to_c(wcrcname, File.basename(cheader.path), File.basename(csource.path),
264
+ indent: 4, algorithm: algorithm)
265
+ cheader << code[:header]
266
+ csource << code[:source]
267
+ end
268
+ when ".js"
269
+ if wcrc.bitsize > 32
270
+ raise "bitsize is too big for javascript (given #{wcrc.bitsize}, expect 1..32)"
271
+ end
272
+
273
+ write_to_file(forceoverwrite, path) do |jssource|
274
+ code = wcrc.dump_to_javascript(wcrcname)
275
+ jssource << code[:source]
276
+ end
277
+ when ".rb"
278
+ write_to_file(forceoverwrite, path) do |rbsource|
279
+ code = wcrc.dump_to_ruby(wcrcname)
280
+ rbsource << code[:source]
281
+ end
282
+ else
283
+ raise "not supported for ``#{type}'' file type"
284
+ end
285
+ rescue
286
+ if $-d
287
+ $stderr.puts $@.join("\n\t").sub(/$/, ": #$! (#{$!.class})")
288
+ else
289
+ $stderr.puts "#$0: #$! - #{path}\n"
290
+ end
291
+ end
292
+ end
293
+ end
294
+ rescue Errno::EPIPE
295
+ end
@@ -158,12 +158,18 @@ class CRC
158
158
  UPDATE_STANDARD_TABLE
159
159
  when ALGORITHM_SLICING_BY_4,
160
160
  ALGORITHM_SLICING_BY_8,
161
- ALGORITHM_SLICING_BY_16
161
+ ALGORITHM_SLICING_BY_16,
162
+ 2 .. 999
163
+ if algorithm > 100
164
+ slicing_format = "table[%3d][p[%3d] %20s]"
165
+ else
166
+ slicing_format = "table[%2d][p[%2d] %20s]"
167
+ end
162
168
  slicing = algorithm.times.map { |off|
163
169
  ioff = (algorithm - 1) - off
164
- "table[%2d][p[%2d] %20s]" % [
165
- off, ioff,
166
- ioff >= alignedbytes ? nil : "^ #{getleadbyte["s", ioff]}"]
170
+ slicing_format % [
171
+ ioff, off,
172
+ off >= alignedbytes ? nil : "^ #{getleadbyte["s", off]}"]
167
173
  }
168
174
  if algorithm < alignedbytes
169
175
  slicing.insert 0, slideslicing["s"]
@@ -318,14 +324,14 @@ extern "C"
318
324
  #
319
325
 
320
326
  # for ruby-1.8
321
- unless "".respond_to?(:getbyte)
327
+ unless String.method_defined?(:getbyte)
322
328
  class String
323
329
  alias getbyte []
324
330
  end
325
331
  end
326
332
 
327
333
  # for mruby
328
- unless [].respond_to?(:freeze)
334
+ unless Array.method_defined?(:freeze)
329
335
  class Array
330
336
  def freeze
331
337
  self
@@ -347,27 +353,9 @@ class #{name.join("::")}
347
353
  #
348
354
  # call-seq:
349
355
  # initialize(prevcrc = nil)
350
- # initialize(seq, prevcrc = nil)
351
356
  #
352
- def initialize(*args)
353
- case args.size
354
- when 0
355
- seq = nil
356
- prevcrc = nil
357
- when 1
358
- if args[0].kind_of?(String)
359
- seq = args[0]
360
- else
361
- prevcrc = args[0]
362
- end
363
- when 2
364
- (seq, prevcrc) = args
365
- else
366
- raise ArgumentError, "wrong number of argument (given \#{args.size}, expect 0..2)"
367
- end
368
-
357
+ def initialize(prevcrc = nil)
369
358
  reset(prevcrc)
370
- update(seq) if seq
371
359
  end
372
360
 
373
361
  def reset(prevcrc = nil)
@@ -406,7 +394,9 @@ class #{name.join("::")}
406
394
 
407
395
  class << self
408
396
  def [](seq, prevcrc = nil)
409
- new(seq, prevcrc)
397
+ crc = new(prevcrc)
398
+ crc << seq
399
+ crc
410
400
  end
411
401
 
412
402
  def setup(crc = INITIAL_CRC)
@@ -848,7 +838,7 @@ end
848
838
  algorithm = "halfbyte-table"
849
839
  when ALGORITHM_STANDARD_TABLE
850
840
  algorithm = "standard-table"
851
- when ALGORITHM_SLICING_BY_4, ALGORITHM_SLICING_BY_8, ALGORITHM_SLICING_BY_16
841
+ when ALGORITHM_SLICING_BY_4, ALGORITHM_SLICING_BY_8, ALGORITHM_SLICING_BY_16, 2..999
852
842
  algorithm = "slicing-by-#{algorithm} (with byte-order free), based Intel's slicing-by-8"
853
843
  else
854
844
  raise ArgumentError, "out of algorithm code (given #{algorithm})"
@@ -1,5 +1,5 @@
1
1
  #!ruby
2
2
 
3
3
  class CRC
4
- VERSION = "0.4.0.1"
4
+ VERSION = "0.4.1"
5
5
  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.4.0.1
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - dearblue
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-03-28 00:00:00.000000000 Z
11
+ date: 2017-03-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -31,6 +31,7 @@ description: |
31
31
  email: dearblue@users.noreply.github.com
32
32
  executables:
33
33
  - rbcrc
34
+ - rbcrc.orig
34
35
  extensions: []
35
36
  extra_rdoc_files:
36
37
  - HISTORY.ja.md
@@ -57,6 +58,7 @@ files:
57
58
  - Rakefile
58
59
  - benchmark.rb
59
60
  - bin/rbcrc
61
+ - bin/rbcrc.orig
60
62
  - gemstub.rb
61
63
  - lib/crc.rb
62
64
  - lib/crc/_aux.rb