crc 0.3 → 0.3.1.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 +4 -4
- data/HISTORY.ja.md +18 -0
- data/README.md +84 -16
- data/bin/rbcrc +282 -0
- data/gemstub.rb +26 -6
- data/lib/crc.rb +39 -15
- data/lib/crc/_byruby.rb +89 -31
- data/lib/crc/_modules.rb +2 -1
- data/lib/crc/acrc.rb +2 -2
- data/lib/crc/codegen.rb +891 -0
- data/lib/crc/version.rb +1 -1
- metadata +13 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6d8b8a3e68b73d5b4d330c90908ab880e67f72c2
|
4
|
+
data.tar.gz: 2b035adbace1aad20b824c9611a1a9db5e06ed98
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 52f0617c4756a65178e2c66c155a5b5df96c619b67ffe50e823a89b7476c6c1d9f8ae75faf65053e7cd565bc71371f12ddb07952557f20d311ab146a0fa5c876
|
7
|
+
data.tar.gz: 6e8e41eb6ef956c1a2ec26a1c97895aebe3c0d2e3f074bf8a3bfd6a0e0bc1250e26a7cc5e34a3e71ddbe8b2868fa57463414ce2fc315314484026a904f064914
|
data/HISTORY.ja.md
CHANGED
@@ -2,6 +2,24 @@ This document is written in Japanese.
|
|
2
2
|
|
3
3
|
# crc for ruby の更新履歴
|
4
4
|
|
5
|
+
## crc-0.3.1.1 (平成28年11月10日 木曜日)
|
6
|
+
|
7
|
+
* rbcrc において reflect-input/output が既定値となっていなかったため修正
|
8
|
+
|
9
|
+
|
10
|
+
## crc-0.3.1 (平成28年11月10日 木曜日)
|
11
|
+
|
12
|
+
* メソッド名 CRC#update\_with\_slice\_by\_eight を CRC#update\_with\_slice\_by\_16 に変更
|
13
|
+
* 非 reflect-input も slicing-by-16 となるように修正
|
14
|
+
* CRC#update\_with\_slice\_by\_16 の 16 ビット以下の crc に対する最適化
|
15
|
+
* crc 計算におけるルックアップテーブルの作成の高速化
|
16
|
+
* 実装が C か ruby かを判別しやすくするための定数 CRC::IMPLEMENT を追加
|
17
|
+
* CRC-64-JONES を追加
|
18
|
+
* 特定の CRC を計算するソースコード出力機能を追加
|
19
|
+
* c、ruby、javascript 向けのソースコードを出力するための機能を追加しました。
|
20
|
+
* 今のところアルゴリズムは c を除き slicing-by-16 に固定となります。
|
21
|
+
|
22
|
+
|
5
23
|
## crc-0.3 (平成28年7月31日 日曜日)
|
6
24
|
|
7
25
|
互換性を損なう変更があります。
|
data/README.md
CHANGED
@@ -3,11 +3,11 @@
|
|
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 (slice-by-16 algorithm
|
6
|
+
It is written by pure ruby with based on slice-by-eight algorithm (slice-by-16 algorithm with byte-order free).
|
7
7
|
|
8
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
|
-
Customization is posible for 1 to 64 bit width, any
|
10
|
+
Customization is posible for 1 to 64 bit width, any polynomials, and with/without bit reflection input/output.
|
11
11
|
|
12
12
|
This library is slower than ×85+ of zlib/crc32, and slower than ×120+ of extlzma/crc32 on FreeBSD 10.3R amd64.
|
13
13
|
|
@@ -20,12 +20,14 @@ If you need more speed, please use [crc-turbo](https://rubygems/gems/crc-turbo).
|
|
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.3
|
23
|
+
* version: 0.3.1.1
|
24
24
|
* release quality: technical preview
|
25
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
|
+
* installed executable file:
|
30
|
+
* rbcrc: CRC calcuration source code generator for c, ruby and javascript
|
29
31
|
|
30
32
|
|
31
33
|
## API Guide
|
@@ -45,13 +47,13 @@ This examples are used CRC-32 module. Please see CRC for more details.
|
|
45
47
|
|
46
48
|
* ``CRC.crc32[init = 0, current_length = 0] => crc-32 generator``
|
47
49
|
* ``CRC.crc32.new(init = 0, current_length = 0) => crc-32 generator``
|
48
|
-
* ``CRC
|
49
|
-
* ``CRC
|
50
|
-
* ``CRC
|
51
|
-
* ``CRC
|
52
|
-
* ``CRC
|
50
|
+
* ``CRC::CRC32#update(seq) => self`` (likely as ``Digest::XXXX.update``)
|
51
|
+
* ``CRC::CRC32#finish => crc-32 integer`` (likely as ``Digest::XXXX.finish``)
|
52
|
+
* ``CRC::CRC32#crc => crc-32 integer`` (same as ``CRC::CRC32#finish``)
|
53
|
+
* ``CRC::CRC32#digest => crc-32 digest`` (likely as ``Digest::XXXX.digest``)
|
54
|
+
* ``CRC::CRC32#hexdigest => crc-32 hex-digest`` (likely as ``Digest::XXXX.hexdigest``)
|
53
55
|
|
54
|
-
Example
|
56
|
+
Example::
|
55
57
|
|
56
58
|
``` ruby:ruby
|
57
59
|
x = CRC.crc32.new # => #<CRC::CRC32:00000000>
|
@@ -67,13 +69,13 @@ This examples are used CRC-32 module. Please see CRC for more details.
|
|
67
69
|
* ``CRC.combine(crc1, crc2, len2) => combined crc integer`` (likely as ``Zlib.crc32_comibne``)
|
68
70
|
* ``CRC#+(right_crc) => combined crc generator``
|
69
71
|
|
70
|
-
Example-1
|
72
|
+
Example-1::
|
71
73
|
|
72
74
|
``` ruby:ruby
|
73
75
|
CRC.crc32.combine(CRC.crc32("123"), CRC.crc32("456789"), 6) # => 3421780262
|
74
76
|
```
|
75
77
|
|
76
|
-
Example-2
|
78
|
+
Example-2::
|
77
79
|
|
78
80
|
``` ruby:ruby
|
79
81
|
CRC.crc32["123"] + CRC.crc32["456"] + CRC.crc32["789"] # => #<CRC::CRC32:CBF43926>
|
@@ -92,26 +94,26 @@ This examples are used CRC-32 module. Please see CRC for more details.
|
|
92
94
|
|
93
95
|
* Calcurate arc-crc (***EXPERIMENTAL***)
|
94
96
|
|
95
|
-
* ``CRC.acrc(pre, post = nil, want_crc = 0) => arc-crc byte string``
|
97
|
+
* ``CRC::XXX.acrc(pre, post = nil, want_crc = 0) => arc-crc byte string``
|
96
98
|
|
97
99
|
``` ruby:ruby
|
98
100
|
a = "12"
|
99
101
|
c = "789"
|
100
|
-
wantcrc =
|
102
|
+
wantcrc = CRC.crc32("123456789")
|
101
103
|
b = CRC.crc32.acrc(a, c, wantcrc) # => "3456"
|
102
104
|
CRC.crc32[a + b + c] # => #<CRC::CRC32:CBF43926>
|
103
105
|
```
|
104
106
|
|
105
|
-
See CRC::ModuleClass.acrc for more detail.
|
107
|
+
See CRC::ModuleClass.acrc or below for more detail.
|
106
108
|
|
107
109
|
|
108
110
|
## Built-in CRC modules
|
109
111
|
|
110
112
|
``` shell:shell
|
111
|
-
|
113
|
+
$ ruby -rcrc -e 'puts CRC::MODULE_TABLE.values.uniq.map { |m| m.name }.join(", ")'
|
112
114
|
```
|
113
115
|
|
114
|
-
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-
|
116
|
+
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-ROHC, CRC-7-UMTS, 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, CRC-16, 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-16-A, CRC-16-KERMIT, CRC-16-MODBUS, CRC-16-X-25, CRC-16-XMODEM, 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-CDMA, CRC-31-PHILIPS, CRC-32, CRC-32-BZIP2, CRC-32C, CRC-32D, CRC-32-MPEG-2, CRC-32-POSIX, CRC-32Q, CRC-32-JAMCRC, CRC-32-XFER, CRC-40-GSM, CRC-64-XZ, CRC-64-JONES, CRC-64-ECMA, CRC-64-WE, CRC-64-ISO
|
115
117
|
|
116
118
|
|
117
119
|
## Environment variables for behavior
|
@@ -127,6 +129,72 @@ CRC-1, CRC-3-ROHC, CRC-4-INTERLAKEN, CRC-4-ITU, CRC-5-EPC, CRC-5-ITU, CRC-5-USB,
|
|
127
129
|
CRC.combine is ported from Mark Adler's crccomb.c in <https://stackoverflow.com/questions/29915764/generic-crc-8-16-32-64-combine-implementation#29928573>.
|
128
130
|
|
129
131
|
|
132
|
+
## Source code generator for the specific CRC calcurator (from crc-0.3.1)
|
133
|
+
|
134
|
+
Add source code generator for the specific CRC calcurator to c, ruby, and javascript.
|
135
|
+
|
136
|
+
Algorithm is slicing-by-16 only for ruby and javascript.
|
137
|
+
|
138
|
+
For C, be able choose into bit-by-bit, bit-by-bit-fast, halfbyte-table,
|
139
|
+
standard-table, slicing-by-4, slicing-by-8, and slicing-by-16.
|
140
|
+
|
141
|
+
``` shell:shell
|
142
|
+
$ rbcrc --help
|
143
|
+
usage: rbcrc [options] output-filename...
|
144
|
+
-M crcname choose included crc name in library (``-l'' to print list)
|
145
|
+
-N crcname declare function name or class name [DEFAULT is filename]
|
146
|
+
-s bitsize declare crc bit size [REQUIRED for customized crc]
|
147
|
+
-p polynom declare crc polynomial [REQUIRED for customized crc]
|
148
|
+
-c initcrc declare initial crc (not internal state) [DEFAULT: 0]
|
149
|
+
-S initstate declare initial state (internal state) [DEFAULT: unset]
|
150
|
+
-x xormask declare xor bit mask for when output [DEFAULT: ~0]
|
151
|
+
-i reflect input [DEFAULT]
|
152
|
+
-I normal input (not reflect)
|
153
|
+
-o reflect output [DEFAULT]
|
154
|
+
-O normal output (not reflect)
|
155
|
+
-a algorithm switch algorithm (see below) (C file type only)
|
156
|
+
|
157
|
+
-l print crc names
|
158
|
+
-f force overwrite
|
159
|
+
-v increment verbosery level
|
160
|
+
-q quiet mode (reset verbosery level to zero)
|
161
|
+
|
162
|
+
About LICENSE for generated source code:
|
163
|
+
Generated code is under Creative Commons License Zero (CC0 / Public Domain).
|
164
|
+
See https://creativecommons.org/publicdomain/zero/1.0/
|
165
|
+
|
166
|
+
Algorithms (C file type only):
|
167
|
+
bit-by-bit, bit-by-bit-fast, halfbyte-table, standard-table,
|
168
|
+
slicing-by-4, slicing-by-8, slicing-by-16
|
169
|
+
|
170
|
+
Support export file types:
|
171
|
+
* .c for C (support C89, but required ``stdint.h'')
|
172
|
+
* .js for javascript (required ECMAScript 6th edition)
|
173
|
+
* .rb for ruby (for ruby-2.1+, jruby, and rubinius)
|
174
|
+
(executable for ruby-1.8, ruby-1.9 and ruby-2.0)
|
175
|
+
(executable for mruby and limitation bitsize by fixnum)
|
176
|
+
|
177
|
+
examples:
|
178
|
+
* create crc-32 generator to c source (and header file)
|
179
|
+
$ rbcrc crc32.c
|
180
|
+
|
181
|
+
* create crc-32c generator to ruby source
|
182
|
+
$ rbcrc crc32c.rb
|
183
|
+
|
184
|
+
* create crc-30-cdma generator to javascript source
|
185
|
+
$ rbcrc crc30cdma.js
|
186
|
+
|
187
|
+
* create crc-32 generator to ``crc.c'', ``crc.rb'' and ``crc.js''
|
188
|
+
$ rbcrc -Mcrc32 crc.c crc.rb crc.js
|
189
|
+
|
190
|
+
* create customized crc generator (as mycrc function) to ``mycrc.c''
|
191
|
+
$ rbcrc -s15 -p0x6789 -io -r0 -x~0 mycrc.c
|
192
|
+
|
193
|
+
* create customized crc generator (as MyCRC class) to ``mycrc_1.rb''
|
194
|
+
$ rbcrc -s39 -p0x987654321 -IO -r0 -x1 -NMyCRC mycrc_1.rb
|
195
|
+
```
|
196
|
+
|
197
|
+
|
130
198
|
## arc-crc (***EXPERIMENTAL***)
|
131
199
|
|
132
200
|
(Written in japanese from here)
|
data/bin/rbcrc
ADDED
@@ -0,0 +1,282 @@
|
|
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 -r0 -x~0 mycrc.c
|
123
|
+
|
124
|
+
* create customized crc generator (as MyCRC class) to ``mycrc_1.rb''
|
125
|
+
$ rbcrc -s39 -p0x987654321 -IO -r0 -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, 1
|
187
|
+
puts CRC::LIST.map {|e| e[7 .. -1] }.join(", ")
|
188
|
+
when 2
|
189
|
+
puts CRC::LIST.map {|e| e[7 .. -1].join(", ") }.join("\n")
|
190
|
+
else
|
191
|
+
puts "## This is YAML format\n"
|
192
|
+
CRC::LIST.each do |e|
|
193
|
+
bytesize = (e[0] + 7) / 8
|
194
|
+
bitmask = ~(~0 << e[0])
|
195
|
+
hex = ->(n) { "0x%0*X" % [bytesize * 2, bitmask & n] }
|
196
|
+
names = e[8 .. -1]
|
197
|
+
if names.empty?
|
198
|
+
names = nil
|
199
|
+
else
|
200
|
+
names = <<-YAML_FORMAT.chomp!
|
201
|
+
|
202
|
+
another names:
|
203
|
+
- "#{names.join(%("\n - "))}"
|
204
|
+
YAML_FORMAT
|
205
|
+
end
|
206
|
+
puts <<-YAML_FORMAT
|
207
|
+
"#{e[7]}":
|
208
|
+
bitsize: #{e[0]}
|
209
|
+
polynomial: #{hex[e[1]]}
|
210
|
+
reflect input: #{e[2].inspect}
|
211
|
+
reflect output: #{e[3].inspect}
|
212
|
+
initial crc: #{hex[e[4]]}
|
213
|
+
xor output: #{hex[e[5]]}#{names}
|
214
|
+
YAML_FORMAT
|
215
|
+
end
|
216
|
+
end
|
217
|
+
else
|
218
|
+
ARGV.each do |path|
|
219
|
+
begin
|
220
|
+
basename = File.basename path, ".*"
|
221
|
+
type = File.extname path
|
222
|
+
|
223
|
+
if bitsize
|
224
|
+
wxor = xormask || ~0
|
225
|
+
wcrc = crc || CRC.new(bitsize, polynomial,
|
226
|
+
initcrc || (initstate ? (initstate ^ wxor) : 0),
|
227
|
+
!normalin, !normalout, wxor)
|
228
|
+
wcrcname = crcname || basename
|
229
|
+
else
|
230
|
+
begin
|
231
|
+
wcrc = crc || CRC.lookup(basename)
|
232
|
+
rescue
|
233
|
+
raise "not matched crc name from filename - #{basename} (if you want check name, use ``-l'' switch)"
|
234
|
+
end
|
235
|
+
|
236
|
+
if initcrc || initstate || xormask || !normalin.nil? || !normalout.nil?
|
237
|
+
wxor = xormask || wcrc.xor_output
|
238
|
+
wcrc = CRC.new(wcrc.bitsize, wcrc.polynomial,
|
239
|
+
initcrc || (initstate ? (initstate ^ wxor) : wcrc.initial_crc),
|
240
|
+
normalin.nil? ? wcrc.reflect_input? : !normalin,
|
241
|
+
normalout.nil? ? wcrc.reflect_output? : !normalout,
|
242
|
+
wxor)
|
243
|
+
wcrcname = crcname || basename
|
244
|
+
end
|
245
|
+
end
|
246
|
+
|
247
|
+
case type
|
248
|
+
when ".c"
|
249
|
+
write_to_file(forceoverwrite, path.sub(/\.c$/i, ".h"), path) do |cheader, csource|
|
250
|
+
code = wcrc.dump_to_c(wcrcname, File.basename(cheader.path), File.basename(csource.path),
|
251
|
+
indent: 4, algorithm: algorithm)
|
252
|
+
cheader << code[:header]
|
253
|
+
csource << code[:source]
|
254
|
+
end
|
255
|
+
when ".js"
|
256
|
+
if wcrc.bitsize > 32
|
257
|
+
raise "bitsize is too big for javascript (given #{wcrc.bitsize}, expect 1..32)"
|
258
|
+
end
|
259
|
+
|
260
|
+
write_to_file(forceoverwrite, path) do |jssource|
|
261
|
+
code = wcrc.dump_to_javascript(wcrcname)
|
262
|
+
jssource << code[:source]
|
263
|
+
end
|
264
|
+
when ".rb"
|
265
|
+
write_to_file(forceoverwrite, path) do |rbsource|
|
266
|
+
code = wcrc.dump_to_ruby(wcrcname)
|
267
|
+
rbsource << code[:source]
|
268
|
+
end
|
269
|
+
else
|
270
|
+
raise "not supported for ``#{type}'' file type"
|
271
|
+
end
|
272
|
+
rescue
|
273
|
+
if $-d
|
274
|
+
$stderr.puts $@.join("\n\t").sub(/$/, ": #$! (#{$!.class})")
|
275
|
+
else
|
276
|
+
$stderr.puts "#$0: #$! - #{path}\n"
|
277
|
+
end
|
278
|
+
end
|
279
|
+
end
|
280
|
+
end
|
281
|
+
rescue Errno::EPIPE
|
282
|
+
end
|