crc 0.3 → 0.4.2
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 +5 -5
- data/HISTORY.ja.md +85 -0
- data/README.ja.md +281 -0
- data/README.md +167 -73
- data/bin/rbcrc +297 -0
- data/bin/rbcrc.orig +295 -0
- data/gemstub.rb +30 -10
- data/lib/crc.rb +63 -257
- data/lib/crc/_aux.rb +37 -0
- data/lib/crc/_byruby.rb +94 -41
- data/lib/crc/_combine.rb +1 -1
- data/lib/crc/_extensions.rb +211 -0
- data/lib/crc/_file.rb +15 -0
- data/lib/crc/_magic.rb +93 -0
- data/lib/crc/{_modules.rb → _models.rb} +7 -6
- data/lib/crc/_shift.rb +198 -0
- data/lib/crc/_utils.rb +114 -0
- data/lib/crc/acrc.rb +18 -73
- data/lib/crc/codegen.rb +881 -0
- data/lib/crc/version.rb +1 -1
- data/test/common.rb +23 -0
- data/test/self_check.rb +31 -0
- data/test/test_arc.rb +23 -0
- data/test/test_block.rb +28 -0
- data/test/test_magic.rb +25 -0
- metadata +42 -19
data/lib/crc/acrc.rb
CHANGED
@@ -3,22 +3,22 @@
|
|
3
3
|
require_relative "../crc"
|
4
4
|
|
5
5
|
class CRC
|
6
|
-
|
6
|
+
using CRC::Extensions
|
7
|
+
|
8
|
+
module Calculator
|
7
9
|
#
|
8
10
|
# call-seq:
|
9
|
-
# acrc(
|
11
|
+
# acrc(pre, post = nil, targetcrc = 0) -> byte string as arc-code
|
10
12
|
#
|
11
13
|
# 目的となる crc になるように、指定された crc に続くバイト列を逆算します。
|
12
14
|
#
|
13
15
|
# 出力されるバイト列は、crc のビット数を表現できるバイト数となります。
|
14
16
|
#
|
15
|
-
# 現在のところ、reflect-input/output 限定となっています。
|
16
|
-
#
|
17
17
|
# * crc32("123456789????") の結果が 0 となるような、???? の部分を逆算する
|
18
18
|
#
|
19
19
|
# seq = "123456789"
|
20
|
-
#
|
21
|
-
# p CRC::CRC32[seq] # => #<CRC::CRC32:00000000>
|
20
|
+
# arced_seq = CRC::CRC32.acrc(seq)
|
21
|
+
# p CRC::CRC32[seq + arced_seq] # => #<CRC::CRC32:00000000>
|
22
22
|
#
|
23
23
|
# * crc32("123456789????ABCDEFG") の結果が 0 となるような、???? の部分を逆算する
|
24
24
|
#
|
@@ -31,75 +31,20 @@ class CRC
|
|
31
31
|
#
|
32
32
|
# seq1 = "123456789"
|
33
33
|
# seq2 = "ABCDEFG"
|
34
|
-
#
|
35
|
-
# seq = seq1 + CRC::CRC32.acrc(seq1, seq2,
|
34
|
+
# targetcrc = 0x12345678
|
35
|
+
# seq = seq1 + CRC::CRC32.acrc(seq1, seq2, targetcrc) + seq2
|
36
36
|
# p CRC::CRC32[seq] # => #<CRC::CRC32:12345678>
|
37
37
|
#
|
38
|
-
def acrc(
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
rest_seq.bytesize.downto(1) do |i|
|
49
|
-
target_crc = Aux.acrc_loop_reflect(target_crc, rest_seq.getbyte(i - 1), poly, bitsize, 0xff, 8)
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
bytes = (bitsize + 7) / 8
|
54
|
-
bits = bytes * 8
|
55
|
-
|
56
|
-
case crc
|
57
|
-
when Numeric
|
58
|
-
state = bitmask & crc ^ xor_output
|
59
|
-
when CRC
|
60
|
-
raise "different crc module (given %p(%p), expect %p)" % [crc, crc.class, self] unless variant?(crc)
|
61
|
-
state = crc.state
|
62
|
-
else
|
63
|
-
state = new(crc).state
|
64
|
-
end
|
65
|
-
|
66
|
-
if bits > bitsize
|
67
|
-
# ビット数が 8 の境界にない場合、その隙間分を埋める。
|
68
|
-
# 現在の実装では、最終結果のバイト列における最終バイト値の
|
69
|
-
# 上位ビットが 0 であるようにしている。
|
70
|
-
pad = bits - bitsize
|
71
|
-
target_crc = Aux.acrc_loop_reflect(target_crc, 0, poly, bitsize, 0xff, pad)
|
72
|
-
end
|
73
|
-
|
74
|
-
target_crc = Aux.acrc_loop_reflect(target_crc, state, poly, bitsize, bitmask, bitsize)
|
75
|
-
|
76
|
-
bytes.times.reduce("") { |a, *| a << (target_crc & 0xff).chr(Encoding::BINARY); target_crc >>= 8; a }
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
module Aux
|
81
|
-
def self.acrc_loop_reflect(target_crc, state, poly, crcbits, bitmask, bits)
|
82
|
-
head = bits - 1
|
83
|
-
bitmask1 = bitmask >> 1
|
84
|
-
crchead = crcbits - 1
|
85
|
-
|
86
|
-
#puts "target_crc=0b%016b, state=0b%016b, reversed-polynomial=0b%016b" % [target_crc, state, poly]
|
87
|
-
bits.times do |i|
|
88
|
-
if target_crc[crchead] == 0
|
89
|
-
target_crc <<= 1
|
90
|
-
else
|
91
|
-
target_crc ^= poly
|
92
|
-
target_crc <<= 1
|
93
|
-
target_crc |= 0x01
|
94
|
-
end
|
95
|
-
|
96
|
-
target_crc ^= state[head]
|
97
|
-
#puts " 0_%016b ^ %d" % [target_crc, state[head]]
|
98
|
-
state = (state & bitmask1) << 1
|
99
|
-
end
|
100
|
-
#puts "target_crc=0b%016b" % target_crc
|
101
|
-
|
102
|
-
target_crc
|
38
|
+
def acrc(pre, post = nil, targetcrc = 0)
|
39
|
+
pre = pre.convert_internal_state_for(self)
|
40
|
+
laststate = targetcrc.convert_target_state_for(self)
|
41
|
+
state = unshiftbytes(post, laststate)
|
42
|
+
bytesize = (bitsize + 7) / 8
|
43
|
+
pre <<= (bytesize * 8 - bitsize) unless reflect_input?
|
44
|
+
bytes = pre.splitbytes("".b, bytesize, reflect_input?)
|
45
|
+
state = unshiftbytes(bytes, state)
|
46
|
+
state <<= (bytesize * 8 - bitsize) unless reflect_input?
|
47
|
+
state.splitbytes("".b, bytesize, reflect_input?)
|
103
48
|
end
|
104
49
|
end
|
105
50
|
end
|
data/lib/crc/codegen.rb
ADDED
@@ -0,0 +1,881 @@
|
|
1
|
+
#!ruby
|
2
|
+
|
3
|
+
require_relative "../crc"
|
4
|
+
|
5
|
+
class CRC
|
6
|
+
ALGORITHM_BITBYBIT = -2
|
7
|
+
ALGORITHM_BITBYBIT_FAST = -1
|
8
|
+
ALGORITHM_HALFBYTE_TABLE = 0
|
9
|
+
ALGORITHM_STANDARD_TABLE = 1
|
10
|
+
ALGORITHM_SLICING_BY_4 = 4
|
11
|
+
ALGORITHM_SLICING_BY_8 = 8
|
12
|
+
ALGORITHM_SLICING_BY_16 = 16
|
13
|
+
|
14
|
+
def self.dump_to_c(func_name = nil, header_name = nil, source_name = nil, indent: 4, visible: true, algorithm: ALGORITHM_SLICING_BY_16)
|
15
|
+
func_name ||= self.to_s.slice(/\w+$/).downcase
|
16
|
+
#func_name ||= self.name.gsub(/[\W]+/, "_").gsub(/_{2,}/, "_").gsub(/^_|_$/, "").downcase
|
17
|
+
header_name ||= "#{func_name}.h"
|
18
|
+
source_name ||= "#{func_name}.c"
|
19
|
+
indentunit = " " * Integer(indent.to_i)
|
20
|
+
indent = ->(level) { indentunit * level }
|
21
|
+
visibility = visible ? "" : "static "
|
22
|
+
|
23
|
+
case
|
24
|
+
when bitsize <= 8
|
25
|
+
type = "uint8_t"
|
26
|
+
sizeof = 1
|
27
|
+
suffix = "u"
|
28
|
+
when bitsize <= 16
|
29
|
+
type = "uint16_t"
|
30
|
+
sizeof = 2
|
31
|
+
suffix = "u"
|
32
|
+
when bitsize <= 32
|
33
|
+
type = "uint32_t"
|
34
|
+
sizeof = 4
|
35
|
+
suffix = "ul"
|
36
|
+
else
|
37
|
+
type = "uint64_t"
|
38
|
+
sizeof = 8
|
39
|
+
suffix = "ull"
|
40
|
+
end
|
41
|
+
|
42
|
+
typesize = sizeof * 8
|
43
|
+
alignedbytes = (bitsize + 7) / 8
|
44
|
+
alignedbits = (bitsize + 7) & ~0x07 # 8 bit alignmented bitsize
|
45
|
+
bitreflect = func_name + "_bitreflect"
|
46
|
+
hexnum = ->(n) { "0x%0#{sizeof * 2}X%s" % [n, suffix] }
|
47
|
+
hexbyte = ->(n) { "0x%02Xu" % n }
|
48
|
+
|
49
|
+
if reflect_input?
|
50
|
+
getleadbyte = ->(expr, off = 0) { "(uint8_t)(#{expr} >> %2d)" % (off * 8) }
|
51
|
+
getheadbit_from_byte = ->(expr, off = 0) { "((#{expr} >> #{off}) & 1)" }
|
52
|
+
getleadbit = ->(expr, off = 0, width = 1) { "((%s >> %2d) & 0x%02X)" % [expr, off, ~(~0 << width)] }
|
53
|
+
slideinput = ->(expr) { "#{expr}" }
|
54
|
+
slidestate1 = ->(expr) { "(#{expr} >> 1)" }
|
55
|
+
slidestate4 = ->(expr) { "(#{expr} >> 4)" }
|
56
|
+
slidestate8 = ->(expr) { "(#{expr} >> 8)" }
|
57
|
+
slideslicing = ->(expr) { "(#{expr} >> #{algorithm * 8})" }
|
58
|
+
padding = depadding = nil
|
59
|
+
workpoly = hexnum[Utils.bitreflect(polynomial, bitsize)]
|
60
|
+
if algorithm >= 0
|
61
|
+
table = Utils.build_reflect_table(bitsize, polynomial, true, slice: (algorithm < 1 ? 1 : algorithm))
|
62
|
+
end
|
63
|
+
else
|
64
|
+
getleadbyte = ->(expr, off = 0) { "(uint8_t)(#{expr} >> %2d)" % (alignedbits - (off + 1) * 8) }
|
65
|
+
getheadbit_from_byte = ->(expr, off = 0) { "((#{expr} >> #{8 - (off + 1)}) & 1)" }
|
66
|
+
getleadbit = ->(expr, off = 0, width = 1) { "((%s >> %2d) & 0x%02X)" % [expr, (alignedbits - (off + 1) - (width - 1)), ~(~0 << width)] }
|
67
|
+
slideinput = ->(expr) { "((#{type})#{expr} << #{alignedbits - 8})" }
|
68
|
+
slidestate1 = ->(expr) { "(#{expr} << 1)" }
|
69
|
+
slidestate4 = ->(expr) { "(#{expr} << 4)" }
|
70
|
+
slidestate8 = ->(expr) { "(#{expr} << 8)" }
|
71
|
+
slideslicing = ->(expr) { "(#{expr} << #{algorithm * 8})" }
|
72
|
+
if (pad = (alignedbits - bitsize)) > 0
|
73
|
+
padding = " << #{pad}"
|
74
|
+
depadding = " >> #{pad}"
|
75
|
+
workpoly = "#{func_name.upcase}_POLYNOMIAL << #{pad}"
|
76
|
+
else
|
77
|
+
padding = depadding = nil
|
78
|
+
workpoly = "#{func_name.upcase}_POLYNOMIAL"
|
79
|
+
end
|
80
|
+
if algorithm >= 0
|
81
|
+
table = Utils.build_table(bitsize, polynomial, true, slice: (algorithm < 1 ? 1 : algorithm))
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
case algorithm
|
86
|
+
when ALGORITHM_BITBYBIT
|
87
|
+
comment = " /* bit reflected */" if reflect_input?
|
88
|
+
prepare = <<-PREPARE_BITBYBIT
|
89
|
+
static const #{type} workpoly = #{workpoly};#{comment}
|
90
|
+
PREPARE_BITBYBIT
|
91
|
+
update = <<-UPDATE_BITBYBIT
|
92
|
+
for (; p < pp; p ++) {
|
93
|
+
int i;
|
94
|
+
s ^= #{slideinput["*p"]};
|
95
|
+
for (i = 0; i < 8; i ++) {
|
96
|
+
s = #{slidestate1["s"]} ^ (workpoly & -(#{type})#{getleadbit["s"]});
|
97
|
+
}
|
98
|
+
}
|
99
|
+
UPDATE_BITBYBIT
|
100
|
+
when ALGORITHM_BITBYBIT_FAST
|
101
|
+
comment = " /* bit reflected */" if reflect_input?
|
102
|
+
tspc = " " * type.size
|
103
|
+
prepare = <<-PREPARE_BITBYBIT_FAST
|
104
|
+
static const #{type} workpoly = #{workpoly};#{comment}
|
105
|
+
static const #{type} g0 = workpoly,
|
106
|
+
#{tspc} g1 = #{slidestate1["g0"]} ^ (workpoly & -(#{type})#{getleadbit["g0"]}),
|
107
|
+
#{tspc} g2 = #{slidestate1["g1"]} ^ (workpoly & -(#{type})#{getleadbit["g1"]}),
|
108
|
+
#{tspc} g3 = #{slidestate1["g2"]} ^ (workpoly & -(#{type})#{getleadbit["g2"]}),
|
109
|
+
#{tspc} g4 = #{slidestate1["g3"]} ^ (workpoly & -(#{type})#{getleadbit["g3"]}),
|
110
|
+
#{tspc} g5 = #{slidestate1["g4"]} ^ (workpoly & -(#{type})#{getleadbit["g4"]}),
|
111
|
+
#{tspc} g6 = #{slidestate1["g5"]} ^ (workpoly & -(#{type})#{getleadbit["g5"]}),
|
112
|
+
#{tspc} g7 = #{slidestate1["g6"]} ^ (workpoly & -(#{type})#{getleadbit["g6"]});
|
113
|
+
PREPARE_BITBYBIT_FAST
|
114
|
+
update = <<-UPDATE_BITBYBIT_FAST
|
115
|
+
for (; p < pp; p ++) {
|
116
|
+
const uint8_t s1 = #{getleadbit["s", 0, 8]} ^ *p;
|
117
|
+
s = #{slidestate8["s"]} ^
|
118
|
+
(g7 & -(#{type})#{getheadbit_from_byte["s1", 0]}) ^
|
119
|
+
(g6 & -(#{type})#{getheadbit_from_byte["s1", 1]}) ^
|
120
|
+
(g5 & -(#{type})#{getheadbit_from_byte["s1", 2]}) ^
|
121
|
+
(g4 & -(#{type})#{getheadbit_from_byte["s1", 3]}) ^
|
122
|
+
(g3 & -(#{type})#{getheadbit_from_byte["s1", 4]}) ^
|
123
|
+
(g2 & -(#{type})#{getheadbit_from_byte["s1", 5]}) ^
|
124
|
+
(g1 & -(#{type})#{getheadbit_from_byte["s1", 6]}) ^
|
125
|
+
(g0 & -(#{type})#{getheadbit_from_byte["s1", 7]});
|
126
|
+
}
|
127
|
+
UPDATE_BITBYBIT_FAST
|
128
|
+
when ALGORITHM_HALFBYTE_TABLE
|
129
|
+
table = table[0]
|
130
|
+
if reflect_input?
|
131
|
+
table = 16.times.map { |i| table[i * 16] }
|
132
|
+
else
|
133
|
+
table = 16.times.map { |i| table[i] }
|
134
|
+
end
|
135
|
+
prepare = <<-PREPARE_HALFBYTE_TABLE
|
136
|
+
static const #{type} table[16] = {
|
137
|
+
#{Aux.export_slicing_table(table, " ", 4, nil, nil, nil, nil, sizeof > 2 ? 4 : 8, &hexnum)}
|
138
|
+
};
|
139
|
+
PREPARE_HALFBYTE_TABLE
|
140
|
+
update = <<-UPDATE_HALFBYTE_TABLE
|
141
|
+
for (; p < pp; p ++) {
|
142
|
+
s ^= #{slideinput["(#{type})*p"]};
|
143
|
+
s = table[#{getleadbit["s", 0, 4]}] ^ #{slidestate4["s"]};
|
144
|
+
s = table[#{getleadbit["s", 0, 4]}] ^ #{slidestate4["s"]};
|
145
|
+
}
|
146
|
+
UPDATE_HALFBYTE_TABLE
|
147
|
+
when ALGORITHM_STANDARD_TABLE
|
148
|
+
prepare = <<-PREPARE_STANDARD_TABLE
|
149
|
+
static const #{type} table[256] = {
|
150
|
+
#{Aux.export_slicing_table(table[0], " ", 4, nil, nil, nil, nil, sizeof > 2 ? 4 : 8, &hexnum)}
|
151
|
+
};
|
152
|
+
PREPARE_STANDARD_TABLE
|
153
|
+
update = <<-UPDATE_STANDARD_TABLE
|
154
|
+
for (; p < pp; p ++) {
|
155
|
+
s ^= #{slideinput["(#{type})*p"]};
|
156
|
+
s = table[(uint8_t)#{getleadbit["s", 0, 8]}] ^ #{slidestate8["s"]};
|
157
|
+
}
|
158
|
+
UPDATE_STANDARD_TABLE
|
159
|
+
when ALGORITHM_SLICING_BY_4,
|
160
|
+
ALGORITHM_SLICING_BY_8,
|
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
|
168
|
+
slicing = algorithm.times.map { |off|
|
169
|
+
ioff = (algorithm - 1) - off
|
170
|
+
slicing_format % [
|
171
|
+
ioff, off,
|
172
|
+
off >= alignedbytes ? nil : "^ #{getleadbyte["s", off]}"]
|
173
|
+
}
|
174
|
+
if algorithm < alignedbytes
|
175
|
+
slicing.insert 0, slideslicing["s"]
|
176
|
+
end
|
177
|
+
slicing = slicing.join(" ^\n ")
|
178
|
+
|
179
|
+
prepare = <<-PREPARE_SLICING_TABLE
|
180
|
+
static const #{type} table[#{algorithm}][256] = {
|
181
|
+
#{Aux.export_slicing_table(table, " ", 4, nil, nil, "{", "}", sizeof > 2 ? 4 : 8, &hexnum)}
|
182
|
+
};
|
183
|
+
PREPARE_SLICING_TABLE
|
184
|
+
update = <<-UPDATE_SLICING_TABLE
|
185
|
+
const uint8_t *ppby = p + (len / #{algorithm} * #{algorithm});
|
186
|
+
for (; p < ppby; p += #{algorithm}) {
|
187
|
+
s = #{slicing};
|
188
|
+
}
|
189
|
+
|
190
|
+
for (; p < pp; p ++) {
|
191
|
+
s = table[0][*p ^ #{getleadbyte["s"]}] ^ #{slidestate8["s"]};
|
192
|
+
}
|
193
|
+
UPDATE_SLICING_TABLE
|
194
|
+
else
|
195
|
+
raise ArgumentError, "wrong algorithm code - #{algorithm}"
|
196
|
+
end
|
197
|
+
|
198
|
+
if reflect_output? ^ reflect_input?
|
199
|
+
slideleft = (typesize == bitsize) ? nil : "\n" << <<-SLIDELEFT.chomp!
|
200
|
+
n <<= #{typesize - bitsize};
|
201
|
+
SLIDELEFT
|
202
|
+
swapby32 = (sizeof < 8) ? nil : "\n" << <<-SWAPBY32.chomp!
|
203
|
+
n = ((n & 0X00000000FFFFFFFF#{suffix}) << 32) | (n >> 32);
|
204
|
+
SWAPBY32
|
205
|
+
swapby16 = (sizeof < 4) ? nil : "\n" << <<-SWAPBY16.chomp!
|
206
|
+
n = ((n & 0x#{"0000FFFF" * (sizeof / 4)}#{suffix}) << 16) | ((n >> 16) & 0x#{"0000FFFF" * (sizeof / 4)}#{suffix});
|
207
|
+
SWAPBY16
|
208
|
+
swapby8 = (sizeof < 2) ? nil : "\n" << <<-SWAPBY8.chomp!
|
209
|
+
n = ((n & 0x#{"00FF" * (sizeof / 2)}#{suffix}) << 8) | ((n >> 8) & 0x#{"00FF" * (sizeof / 2)}#{suffix});
|
210
|
+
SWAPBY8
|
211
|
+
|
212
|
+
func_bitreflect = "\n" << <<-BITREFLECT
|
213
|
+
static #{type}
|
214
|
+
#{bitreflect}(#{type} n)
|
215
|
+
{#{slideleft}#{swapby32}#{swapby16}#{swapby8}
|
216
|
+
n = ((n & 0x#{"0F" * sizeof}#{suffix}) << 4) | ((n >> 4) & 0x#{"0F" * sizeof}#{suffix});
|
217
|
+
n = ((n & 0x#{"33" * sizeof}#{suffix}) << 2) | ((n >> 2) & 0x#{"33" * sizeof}#{suffix});
|
218
|
+
n = ((n & 0x#{"55" * sizeof}#{suffix}) << 1) | ((n >> 1) & 0x#{"55" * sizeof}#{suffix});
|
219
|
+
return n;
|
220
|
+
}
|
221
|
+
BITREFLECT
|
222
|
+
else
|
223
|
+
bitreflect = nil
|
224
|
+
end
|
225
|
+
|
226
|
+
{ header: <<-CHEADER, source: <<-CSOURCE }
|
227
|
+
/*
|
228
|
+
#{Aux.dump_banner(" *", "#{self.name}{#{to_str}}", algorithm)}
|
229
|
+
*/
|
230
|
+
|
231
|
+
#ifndef #{func_name.upcase}_H__
|
232
|
+
#define #{func_name.upcase}_H__ 1
|
233
|
+
|
234
|
+
#include <stdlib.h>
|
235
|
+
#include <stdint.h>
|
236
|
+
|
237
|
+
#define #{func_name.upcase}_TYPE #{type}
|
238
|
+
#define #{func_name.upcase}_BITSIZE #{bitsize}
|
239
|
+
#define #{func_name.upcase}_BITMASK #{hexnum[bitmask]}
|
240
|
+
#define #{func_name.upcase}_POLYNOMIAL #{hexnum[polynomial]}
|
241
|
+
#define #{func_name.upcase}_INITIAL_CRC #{hexnum[initial_crc]}
|
242
|
+
#define #{func_name.upcase}_XOR_OUTPUT #{hexnum[xor_output]}
|
243
|
+
#define #{func_name.upcase}_REFLECT_INPUT #{reflect_input? ? 1 : 0}
|
244
|
+
#define #{func_name.upcase}_REFLECT_OUTPUT #{reflect_output? ? 1 : 0}
|
245
|
+
|
246
|
+
#ifdef __cplusplus
|
247
|
+
extern "C"
|
248
|
+
#{visibility}#{type} #{func_name}(const void *ptr, size_t len, #{type} crc = #{func_name.upcase}_INITIAL_CRC);
|
249
|
+
#else
|
250
|
+
#{visibility}#{type} #{func_name}(const void *ptr, size_t len, #{type} crc);
|
251
|
+
#endif
|
252
|
+
|
253
|
+
#endif /* #{func_name.upcase}_H__ */
|
254
|
+
CHEADER
|
255
|
+
/*
|
256
|
+
#{Aux.dump_banner(" *", "#{self.name}{#{to_str}}", algorithm)}
|
257
|
+
*/
|
258
|
+
|
259
|
+
#include "#{header_name}"
|
260
|
+
#{func_bitreflect}
|
261
|
+
#{visibility}#{type}
|
262
|
+
#{func_name}(const void *ptr, size_t len, #{type} crc)
|
263
|
+
{
|
264
|
+
#{prepare}
|
265
|
+
#{type} s = ((#{bitreflect}(crc) & #{func_name.upcase}_BITMASK) ^ #{func_name.upcase}_XOR_OUTPUT)#{padding};
|
266
|
+
const uint8_t *p = (const uint8_t *)ptr;
|
267
|
+
const uint8_t *pp = p + len;
|
268
|
+
|
269
|
+
#{update}
|
270
|
+
return #{bitreflect}((s#{depadding}) & #{func_name.upcase}_BITMASK) ^ #{func_name.upcase}_XOR_OUTPUT;
|
271
|
+
}
|
272
|
+
CSOURCE
|
273
|
+
end
|
274
|
+
|
275
|
+
def self.dump_to_ruby(class_name = nil)
|
276
|
+
class_name ||= self.to_s.slice(/\w+$/)
|
277
|
+
name = class_name.split("::")
|
278
|
+
name.map! { |nm| nm.gsub!(/[\W]+/m, "_"); nm.sub!(/^_+/, ""); nm.sub!(/^[a-z]/) { |m| m.upcase! }; nm }
|
279
|
+
case
|
280
|
+
when bitsize <= 8
|
281
|
+
sizeof = 1
|
282
|
+
pack = "C"
|
283
|
+
when bitsize <= 16
|
284
|
+
sizeof = 2
|
285
|
+
pack = "n"
|
286
|
+
when bitsize <= 32
|
287
|
+
sizeof = 4
|
288
|
+
pack = "N"
|
289
|
+
else
|
290
|
+
sizeof = 8
|
291
|
+
pack = "Q>"
|
292
|
+
end
|
293
|
+
|
294
|
+
typebits = sizeof * 8
|
295
|
+
hexnum = ->(n) { "0x%0*X" % [sizeof * 2, n] }
|
296
|
+
if reflect_input?
|
297
|
+
table1 = Utils.build_reflect_table(bitsize, polynomial, slice: 16)
|
298
|
+
headstate = "(s & 0xFF)"
|
299
|
+
slidestate = "(s >> 8)"
|
300
|
+
slicing = ->(off) {
|
301
|
+
t = "t%X" % (15 - off)
|
302
|
+
getbyte = off > 0 ? "+ %2d" % off : ""
|
303
|
+
shiftstate = off < sizeof ? "^ (s %5s) & 0xFF" % (off > 0 ? ">> %2d" % (off * 8) : "") : ""
|
304
|
+
"%s[seq.getbyte(i %4s) %18s]" % [t, getbyte, shiftstate]
|
305
|
+
}
|
306
|
+
else
|
307
|
+
table1 = Utils.build_table(bitsize, polynomial, true, slice: 16)
|
308
|
+
bitpad = typebits - bitsize
|
309
|
+
headstate = "((s >> #{typebits - 8}) & 0xFF)"
|
310
|
+
slidestate = "((s & #{hexnum[~(~0 << typebits) >> 8]}) << 8)"
|
311
|
+
slicing = ->(off) {
|
312
|
+
t = "t%X" % (15 - off)
|
313
|
+
getbyte = off > 0 ? "+ %2d" % off : ""
|
314
|
+
shiftstate = off < sizeof ? "^ (s %5s) & 0xFF" % (off + 1 < sizeof ? ">> %2d" % ((sizeof - off) * 8 - 8) : "") : ""
|
315
|
+
"%s[seq.getbyte(i %4s) %18s]" % [t, getbyte, shiftstate]
|
316
|
+
}
|
317
|
+
end
|
318
|
+
|
319
|
+
code = <<-EOS
|
320
|
+
#!ruby
|
321
|
+
|
322
|
+
#
|
323
|
+
#{Aux.dump_banner("#", "#{self.name}{#{to_str}}", ALGORITHM_SLICING_BY_16)}
|
324
|
+
#
|
325
|
+
|
326
|
+
# for ruby-1.8
|
327
|
+
unless String.method_defined?(:getbyte)
|
328
|
+
class String
|
329
|
+
alias getbyte []
|
330
|
+
end
|
331
|
+
end
|
332
|
+
|
333
|
+
# for mruby
|
334
|
+
unless Array.method_defined?(:freeze)
|
335
|
+
class Array
|
336
|
+
def freeze
|
337
|
+
self
|
338
|
+
end
|
339
|
+
end
|
340
|
+
end
|
341
|
+
|
342
|
+
class #{name.join("::")}
|
343
|
+
BITSIZE = #{bitsize}
|
344
|
+
BITMASK = #{hexnum[bitmask]}
|
345
|
+
POLYNOMIAL = #{hexnum[polynomial]}
|
346
|
+
INITIAL_CRC = #{hexnum[initial_crc]}
|
347
|
+
REFLECT_INPUT = #{reflect_input?.inspect}
|
348
|
+
REFLECT_OUTPUT = #{reflect_output?.inspect}
|
349
|
+
XOR_OUTPUT = #{hexnum[xor_output]}
|
350
|
+
|
351
|
+
attr_accessor :state
|
352
|
+
|
353
|
+
#
|
354
|
+
# call-seq:
|
355
|
+
# initialize(prevcrc = nil)
|
356
|
+
#
|
357
|
+
def initialize(prevcrc = nil)
|
358
|
+
reset(prevcrc)
|
359
|
+
end
|
360
|
+
|
361
|
+
def reset(prevcrc = nil)
|
362
|
+
@state = self.class.setup(prevcrc || INITIAL_CRC)
|
363
|
+
self
|
364
|
+
end
|
365
|
+
|
366
|
+
def update(seq)
|
367
|
+
@state = self.class.update(seq, state)
|
368
|
+
self
|
369
|
+
end
|
370
|
+
|
371
|
+
alias << update
|
372
|
+
|
373
|
+
def crc
|
374
|
+
self.class.finish(state)
|
375
|
+
end
|
376
|
+
|
377
|
+
alias finish crc
|
378
|
+
|
379
|
+
def digest
|
380
|
+
[crc].pack(#{pack.inspect})
|
381
|
+
end
|
382
|
+
|
383
|
+
def hexdigest
|
384
|
+
"%0#{sizeof * 2}X" % crc
|
385
|
+
end
|
386
|
+
|
387
|
+
def inspect
|
388
|
+
"#<\#{self.class}:\#{hexdigest}>"
|
389
|
+
end
|
390
|
+
|
391
|
+
def pretty_print(q)
|
392
|
+
q.text inspect
|
393
|
+
end
|
394
|
+
|
395
|
+
class << self
|
396
|
+
def [](seq, prevcrc = nil)
|
397
|
+
crc = new(prevcrc)
|
398
|
+
crc << seq
|
399
|
+
crc
|
400
|
+
end
|
401
|
+
|
402
|
+
def setup(crc = INITIAL_CRC)
|
403
|
+
#{
|
404
|
+
s = "(BITMASK & crc ^ XOR_OUTPUT)"
|
405
|
+
|
406
|
+
if reflect_output? ^ reflect_input?
|
407
|
+
s = "bitreflect#{s}"
|
408
|
+
end
|
409
|
+
|
410
|
+
if !reflect_input? && typebits > bitsize
|
411
|
+
s << " << #{typebits - bitsize}"
|
412
|
+
end
|
413
|
+
|
414
|
+
s
|
415
|
+
}
|
416
|
+
end
|
417
|
+
|
418
|
+
alias init setup
|
419
|
+
|
420
|
+
def finish(state)
|
421
|
+
#{
|
422
|
+
if !reflect_input? && typebits > bitsize
|
423
|
+
state = "(state >> #{typebits - bitsize})"
|
424
|
+
else
|
425
|
+
state = "state"
|
426
|
+
end
|
427
|
+
|
428
|
+
if reflect_output? ^ reflect_input?
|
429
|
+
"bitreflect(BITMASK & #{state} ^ XOR_OUTPUT)"
|
430
|
+
else
|
431
|
+
"BITMASK & #{state} ^ XOR_OUTPUT"
|
432
|
+
end
|
433
|
+
}
|
434
|
+
end
|
435
|
+
|
436
|
+
#
|
437
|
+
# call-seq:
|
438
|
+
# update(seq, state) -> state
|
439
|
+
#
|
440
|
+
def update(seq, s)
|
441
|
+
i = 0
|
442
|
+
ii = seq.bytesize
|
443
|
+
ii16 = ii & ~15
|
444
|
+
t0 = TABLE[ 0]; t1 = TABLE[ 1]; t2 = TABLE[ 2]; t3 = TABLE[ 3]
|
445
|
+
t4 = TABLE[ 4]; t5 = TABLE[ 5]; t6 = TABLE[ 6]; t7 = TABLE[ 7]
|
446
|
+
t8 = TABLE[ 8]; t9 = TABLE[ 9]; tA = TABLE[10]; tB = TABLE[11]
|
447
|
+
tC = TABLE[12]; tD = TABLE[13]; tE = TABLE[14]; tF = TABLE[15]
|
448
|
+
while i < ii16
|
449
|
+
s = #{slicing[ 0]} ^
|
450
|
+
#{slicing[ 1]} ^
|
451
|
+
#{slicing[ 2]} ^
|
452
|
+
#{slicing[ 3]} ^
|
453
|
+
#{slicing[ 4]} ^
|
454
|
+
#{slicing[ 5]} ^
|
455
|
+
#{slicing[ 6]} ^
|
456
|
+
#{slicing[ 7]} ^
|
457
|
+
#{slicing[ 8]} ^
|
458
|
+
#{slicing[ 9]} ^
|
459
|
+
#{slicing[10]} ^
|
460
|
+
#{slicing[11]} ^
|
461
|
+
#{slicing[12]} ^
|
462
|
+
#{slicing[13]} ^
|
463
|
+
#{slicing[14]} ^
|
464
|
+
#{slicing[15]}
|
465
|
+
i += 16
|
466
|
+
end
|
467
|
+
|
468
|
+
while i < ii
|
469
|
+
s = t0[seq.getbyte(i) ^ #{headstate}] ^ #{slidestate}
|
470
|
+
i += 1
|
471
|
+
end
|
472
|
+
|
473
|
+
s
|
474
|
+
end
|
475
|
+
|
476
|
+
def crc(seq, initcrc = INITIAL_CRC)
|
477
|
+
finish(update(seq, setup(initcrc)))
|
478
|
+
end
|
479
|
+
|
480
|
+
def digest(seq, initcrc = INITIAL_CRC)
|
481
|
+
[crc(seq, initcrc)].pack(#{pack.inspect})
|
482
|
+
end
|
483
|
+
|
484
|
+
def hexdigest(seq, initcrc = INITIAL_CRC)
|
485
|
+
"%0#{sizeof * 2}X" % crc(seq, initcrc)
|
486
|
+
end
|
487
|
+
EOS
|
488
|
+
|
489
|
+
if reflect_output? ^ reflect_input?
|
490
|
+
code << <<-EOS
|
491
|
+
|
492
|
+
def bitreflect(n)
|
493
|
+
EOS
|
494
|
+
|
495
|
+
if typebits > bitsize
|
496
|
+
code << <<-EOS
|
497
|
+
n <<= #{typebits - bitsize}
|
498
|
+
EOS
|
499
|
+
end
|
500
|
+
|
501
|
+
if typebits > 32
|
502
|
+
code << <<-EOS
|
503
|
+
n = ((n >> 32) & 0x00000000ffffffff) | ((n & 0x00000000ffffffff) << 32)
|
504
|
+
EOS
|
505
|
+
end
|
506
|
+
|
507
|
+
if typebits > 16
|
508
|
+
code << <<-EOS
|
509
|
+
n = ((n >> 16) & 0x#{"0000ffff" * (sizeof / 4)}) | ((n & 0x#{"0000ffff" * (sizeof / 4)}) << 16)
|
510
|
+
EOS
|
511
|
+
end
|
512
|
+
|
513
|
+
if typebits > 8
|
514
|
+
code << <<-EOS
|
515
|
+
n = ((n >> 8) & 0x#{"00ff" * (sizeof / 2)}) | ((n & 0x#{"00ff" * (sizeof / 2)}) << 8)
|
516
|
+
EOS
|
517
|
+
end
|
518
|
+
|
519
|
+
code << <<-EOS
|
520
|
+
n = ((n >> 4) & 0x#{"0f" * sizeof}) | ((n & 0x#{"0f" * sizeof}) << 4)
|
521
|
+
n = ((n >> 2) & 0x#{"33" * sizeof}) | ((n & 0x#{"33" * sizeof}) << 2)
|
522
|
+
n = ((n >> 1) & 0x#{"55" * sizeof}) | ((n & 0x#{"55" * sizeof}) << 1)
|
523
|
+
end
|
524
|
+
EOS
|
525
|
+
end
|
526
|
+
|
527
|
+
code << <<-EOS
|
528
|
+
end
|
529
|
+
|
530
|
+
TABLE = [
|
531
|
+
#{Aux.export_slicing_table(table1, " ", 2, nil, nil, "[", "].freeze", sizeof > 2 ? 4 : 8, &hexnum)}
|
532
|
+
].freeze
|
533
|
+
end
|
534
|
+
EOS
|
535
|
+
|
536
|
+
({ source: code })
|
537
|
+
end
|
538
|
+
|
539
|
+
def self.dump_to_javascript(class_name = nil)
|
540
|
+
class_name ||= self.to_s.slice(/\w+$/).downcase
|
541
|
+
name = class_name.split("::")
|
542
|
+
name.map! { |nm| nm.gsub!(/[\W]+/m, "_"); nm.sub!(/^_+/, ""); nm }
|
543
|
+
case
|
544
|
+
when bitsize <= 8
|
545
|
+
sizeof = 1
|
546
|
+
when bitsize <= 16
|
547
|
+
sizeof = 2
|
548
|
+
when bitsize <= 32
|
549
|
+
sizeof = 4
|
550
|
+
else
|
551
|
+
sizeof = 8
|
552
|
+
end
|
553
|
+
|
554
|
+
pack = ""
|
555
|
+
|
556
|
+
typebits = sizeof * 8
|
557
|
+
hexnum = ->(n) { "0x%0*X" % [sizeof * 2, n] }
|
558
|
+
if reflect_input?
|
559
|
+
table1 = Utils.build_reflect_table(bitsize, polynomial, slice: 16)
|
560
|
+
headstate = "(s & 0xFF)"
|
561
|
+
slidestate = "(s >>> 8)"
|
562
|
+
slicing = ->(off) {
|
563
|
+
t = "t%X" % (15 - off)
|
564
|
+
getbyte = off > 0 ? "+ %2d" % off : ""
|
565
|
+
shiftstate = off < sizeof ? "^ (s %6s) & 0xFF" % (off > 0 ? ">>> %2d" % (off * 8) : "") : ""
|
566
|
+
"%s[(0xFF & seq.charCodeAt(i %4s)) %19s]" % [t, getbyte, shiftstate]
|
567
|
+
}
|
568
|
+
else
|
569
|
+
table1 = Utils.build_table(bitsize, polynomial, true, slice: 16)
|
570
|
+
bitpad = typebits - bitsize
|
571
|
+
#table1.each { |t| t.map! { |tt| tt << bitpad } }
|
572
|
+
headstate = "((s >>> #{typebits - 8}) & 0xFF)"
|
573
|
+
slidestate = "((s & #{hexnum[~(~0 << typebits) >> 8]}) << 8)"
|
574
|
+
slicing = ->(off) {
|
575
|
+
t = "t%X" % (15 - off)
|
576
|
+
getbyte = off > 0 ? "+ %2d" % off : ""
|
577
|
+
shiftstate = off < sizeof ? "^ (s %6s) & 0xFF" % (off + 1 < sizeof ? ">>> %2d" % ((sizeof - off) * 8 - 8) : "") : ""
|
578
|
+
"%s[(0xFF & seq.charCodeAt(i %4s)) %19s]" % [t, getbyte, shiftstate]
|
579
|
+
}
|
580
|
+
end
|
581
|
+
|
582
|
+
typename = name.join(".")
|
583
|
+
|
584
|
+
code = <<-EOS
|
585
|
+
/*
|
586
|
+
#{Aux.dump_banner(" *", "#{self.name}{#{to_str}}", ALGORITHM_SLICING_BY_16)}
|
587
|
+
* * Required ECMASCript version: 6th edition
|
588
|
+
*
|
589
|
+
* *** IMPORTANT BUG! ***
|
590
|
+
*
|
591
|
+
* This can not be calculated correctly,
|
592
|
+
* if string included with 0x100+ codepointed character.
|
593
|
+
*/
|
594
|
+
|
595
|
+
"use strict";
|
596
|
+
|
597
|
+
/*
|
598
|
+
* #{typename}(prevcrc = null)
|
599
|
+
* #{typename}(seq, prevcrc = null)
|
600
|
+
* new #{typename}(prevcrc = null)
|
601
|
+
* new #{typename}(seq, prevcrc = null)
|
602
|
+
*/
|
603
|
+
(function(root, undefined) {
|
604
|
+
if (typeof(module) != "undefined") {
|
605
|
+
// case node-js
|
606
|
+
root = global;
|
607
|
+
}
|
608
|
+
|
609
|
+
var BITSIZE = #{bitsize};
|
610
|
+
var BITMASK = #{hexnum[bitmask]} >>> 0;
|
611
|
+
var POLYNOMIAL = #{hexnum[polynomial]} >>> 0;
|
612
|
+
var INITIAL_CRC = #{hexnum[initial_crc]} >>> 0;
|
613
|
+
var REFLECT_INPUT = #{reflect_input?.inspect};
|
614
|
+
var REFLECT_OUTPUT = #{reflect_output?.inspect};
|
615
|
+
var XOR_OUTPUT = #{hexnum[xor_output]} >>> 0;
|
616
|
+
|
617
|
+
var #{typename} = function() {
|
618
|
+
if(!(this instanceof #{typename})) {
|
619
|
+
return new #{typename}(...arguments).crc;
|
620
|
+
}
|
621
|
+
|
622
|
+
var seq, prevcrc;
|
623
|
+
|
624
|
+
switch (arguments.length) {
|
625
|
+
case 0:
|
626
|
+
seq = null;
|
627
|
+
prevcrc = null;
|
628
|
+
break;
|
629
|
+
case 1:
|
630
|
+
if (typeof(arguments[0]) == "string") {
|
631
|
+
seq = arguments[0];
|
632
|
+
} else {
|
633
|
+
prevcrc = arguments[0];
|
634
|
+
}
|
635
|
+
break;
|
636
|
+
case 2:
|
637
|
+
seq = arguments[0];
|
638
|
+
prevcrc = arguments[1];
|
639
|
+
break;
|
640
|
+
default:
|
641
|
+
throw `wrong number of argument (given ${arguments.size}, expect 0..2)`;
|
642
|
+
}
|
643
|
+
|
644
|
+
this.reset(prevcrc);
|
645
|
+
if (seq) { this.update(seq); }
|
646
|
+
};
|
647
|
+
|
648
|
+
var proto = #{typename}.prototype;
|
649
|
+
|
650
|
+
proto.reset = function(prevcrc = null) {
|
651
|
+
this.state = #{typename}.setup(prevcrc || INITIAL_CRC);
|
652
|
+
return this;
|
653
|
+
};
|
654
|
+
|
655
|
+
proto.update = function(seq) {
|
656
|
+
this.state = #{typename}.update(seq, this.state);
|
657
|
+
return this;
|
658
|
+
};
|
659
|
+
|
660
|
+
// proto.operator << = proto.update;
|
661
|
+
|
662
|
+
proto.finish = function() {
|
663
|
+
return #{typename}.finish(this.state);
|
664
|
+
};
|
665
|
+
|
666
|
+
Object.defineProperty(proto, "crc", {
|
667
|
+
get: proto.finish
|
668
|
+
});
|
669
|
+
|
670
|
+
Object.defineProperty(proto, "hexdigest", {
|
671
|
+
get: function() {
|
672
|
+
var s = this.crc.toString(16);
|
673
|
+
for (var i = s.length; i < #{sizeof * 2}; i ++) {
|
674
|
+
s = "0" + s;
|
675
|
+
}
|
676
|
+
return s;
|
677
|
+
}
|
678
|
+
});
|
679
|
+
|
680
|
+
proto.toString = function() {
|
681
|
+
return `#<#{typename}:${this.hexdigest}>`;
|
682
|
+
};
|
683
|
+
|
684
|
+
#{typename}.setup = function(crc = INITIAL_CRC) {
|
685
|
+
return #{
|
686
|
+
s = "(BITMASK & crc ^ XOR_OUTPUT)"
|
687
|
+
|
688
|
+
if reflect_output? ^ reflect_input?
|
689
|
+
s = "bitreflect#{s}"
|
690
|
+
end
|
691
|
+
|
692
|
+
if !reflect_input? && typebits > bitsize
|
693
|
+
s << " << #{typebits - bitsize}"
|
694
|
+
end
|
695
|
+
|
696
|
+
s
|
697
|
+
};
|
698
|
+
};
|
699
|
+
|
700
|
+
#{typename}.init = #{typename}.setup;
|
701
|
+
|
702
|
+
#{typename}.finish = function(state) {
|
703
|
+
return (#{
|
704
|
+
if !reflect_input? && typebits > bitsize
|
705
|
+
state = "(state >>> #{typebits - bitsize})"
|
706
|
+
else
|
707
|
+
state = "state"
|
708
|
+
end
|
709
|
+
|
710
|
+
if reflect_output? ^ reflect_input?
|
711
|
+
"bitreflect(BITMASK & #{state} ^ XOR_OUTPUT)"
|
712
|
+
else
|
713
|
+
"BITMASK & #{state} ^ XOR_OUTPUT"
|
714
|
+
end
|
715
|
+
}) >>> 0;
|
716
|
+
};
|
717
|
+
|
718
|
+
/*
|
719
|
+
* update(seq, state) -> state
|
720
|
+
*/
|
721
|
+
#{typename}.update = function(seq, s) {
|
722
|
+
var i = 0;
|
723
|
+
var ii = seq.length;
|
724
|
+
var ii16 = ii & ~15;
|
725
|
+
var t0 = TABLE[ 0], t1 = TABLE[ 1], t2 = TABLE[ 2], t3 = TABLE[ 3],
|
726
|
+
t4 = TABLE[ 4], t5 = TABLE[ 5], t6 = TABLE[ 6], t7 = TABLE[ 7],
|
727
|
+
t8 = TABLE[ 8], t9 = TABLE[ 9], tA = TABLE[10], tB = TABLE[11],
|
728
|
+
tC = TABLE[12], tD = TABLE[13], tE = TABLE[14], tF = TABLE[15];
|
729
|
+
for (; i < ii16; i += 16) {
|
730
|
+
s = #{slicing[ 0]} ^
|
731
|
+
#{slicing[ 1]} ^
|
732
|
+
#{slicing[ 2]} ^
|
733
|
+
#{slicing[ 3]} ^
|
734
|
+
#{slicing[ 4]} ^
|
735
|
+
#{slicing[ 5]} ^
|
736
|
+
#{slicing[ 6]} ^
|
737
|
+
#{slicing[ 7]} ^
|
738
|
+
#{slicing[ 8]} ^
|
739
|
+
#{slicing[ 9]} ^
|
740
|
+
#{slicing[10]} ^
|
741
|
+
#{slicing[11]} ^
|
742
|
+
#{slicing[12]} ^
|
743
|
+
#{slicing[13]} ^
|
744
|
+
#{slicing[14]} ^
|
745
|
+
#{slicing[15]};
|
746
|
+
}
|
747
|
+
|
748
|
+
for (; i < ii; i ++) {
|
749
|
+
s = t0[(0xFF & seq.charCodeAt(i)) ^ #{headstate}] ^ #{slidestate}
|
750
|
+
}
|
751
|
+
|
752
|
+
return s;
|
753
|
+
}
|
754
|
+
|
755
|
+
#{typename}.crc = function(seq, initcrc = INITIAL_CRC) {
|
756
|
+
return #{typename}.finish(#{typename}.update(seq, #{typename}.setup(initcrc)));
|
757
|
+
};
|
758
|
+
|
759
|
+
#{typename}.hexdigest = function(seq, initcrc = INITIAL_CRC) {
|
760
|
+
var s = #{typename}.crc(seq, initcrc).toString(16);
|
761
|
+
for (var i = s.length; i < #{sizeof * 2}; i ++) {
|
762
|
+
s = "0" + s;
|
763
|
+
}
|
764
|
+
return s;
|
765
|
+
};
|
766
|
+
EOS
|
767
|
+
|
768
|
+
if reflect_output? ^ reflect_input?
|
769
|
+
code << <<-EOS
|
770
|
+
|
771
|
+
#{typename}.bitreflect = function(n) {
|
772
|
+
EOS
|
773
|
+
|
774
|
+
if typebits > bitsize
|
775
|
+
code << <<-EOS
|
776
|
+
n <<= #{typebits - bitsize}
|
777
|
+
EOS
|
778
|
+
end
|
779
|
+
|
780
|
+
if typebits > 32
|
781
|
+
code << <<-EOS
|
782
|
+
n = ((n >>> 32) & 0x00000000ffffffff) | ((n & 0x00000000ffffffff) << 32)
|
783
|
+
EOS
|
784
|
+
end
|
785
|
+
|
786
|
+
if typebits > 16
|
787
|
+
code << <<-EOS
|
788
|
+
n = ((n >>> 16) & 0x#{"0000ffff" * (sizeof / 4)}) | ((n & 0x#{"0000ffff" * (sizeof / 4)}) << 16)
|
789
|
+
EOS
|
790
|
+
end
|
791
|
+
|
792
|
+
if typebits > 8
|
793
|
+
code << <<-EOS
|
794
|
+
n = ((n >>> 8) & 0x#{"00ff" * (sizeof / 2)}) | ((n & 0x#{"00ff" * (sizeof / 2)}) << 8)
|
795
|
+
EOS
|
796
|
+
end
|
797
|
+
|
798
|
+
code << <<-EOS
|
799
|
+
n = ((n >>> 4) & 0x#{"0f" * sizeof}) | ((n & 0x#{"0f" * sizeof}) << 4)
|
800
|
+
n = ((n >>> 2) & 0x#{"33" * sizeof}) | ((n & 0x#{"33" * sizeof}) << 2)
|
801
|
+
n = ((n >>> 1) & 0x#{"55" * sizeof}) | ((n & 0x#{"55" * sizeof}) << 1)
|
802
|
+
return n;
|
803
|
+
}
|
804
|
+
EOS
|
805
|
+
end
|
806
|
+
|
807
|
+
code << <<-EOS
|
808
|
+
|
809
|
+
var TABLE = [
|
810
|
+
#{Aux.export_slicing_table(table1, " ", 2, nil, nil, "[", "]", sizeof > 2 ? 4 : 8, &hexnum)}
|
811
|
+
];
|
812
|
+
|
813
|
+
root.#{typename} = #{typename};
|
814
|
+
|
815
|
+
Object.defineProperty(#{typename}, "BITSIZE", { get: function() { return BITSIZE } });
|
816
|
+
Object.defineProperty(#{typename}, "BITMASK", { get: function() { return BITMASK } });
|
817
|
+
Object.defineProperty(#{typename}, "POLYNOMIAL", { get: function() { return POLYNOMIAL } });
|
818
|
+
Object.defineProperty(#{typename}, "INITIAL_CRC", { get: function() { return INITIAL_CRC } });
|
819
|
+
Object.defineProperty(#{typename}, "REFLECT_INPUT", { get: function() { return REFLECT_INPUT } });
|
820
|
+
Object.defineProperty(#{typename}, "REFLECT_OUTPUT", { get: function() { return REFLECT_OUTPUT } });
|
821
|
+
Object.defineProperty(#{typename}, "XOR_OUTPUT", { get: function() { return XOR_OUTPUT } });
|
822
|
+
|
823
|
+
return #{typename};
|
824
|
+
})(this);
|
825
|
+
EOS
|
826
|
+
|
827
|
+
({ source: code })
|
828
|
+
end
|
829
|
+
|
830
|
+
module Aux
|
831
|
+
def self.dump_banner(line_prefix, crcname, algorithm)
|
832
|
+
case algorithm
|
833
|
+
when ALGORITHM_BITBYBIT
|
834
|
+
algorithm = "bit-by-bit"
|
835
|
+
when ALGORITHM_BITBYBIT_FAST
|
836
|
+
algorithm = "bit-by-bit-fast"
|
837
|
+
when ALGORITHM_HALFBYTE_TABLE
|
838
|
+
algorithm = "halfbyte-table"
|
839
|
+
when ALGORITHM_STANDARD_TABLE
|
840
|
+
algorithm = "standard-table"
|
841
|
+
when ALGORITHM_SLICING_BY_4, ALGORITHM_SLICING_BY_8, ALGORITHM_SLICING_BY_16, 2..999
|
842
|
+
algorithm = "slicing-by-#{algorithm} (with byte-order free), based Intel's slicing-by-8"
|
843
|
+
else
|
844
|
+
raise ArgumentError, "out of algorithm code (given #{algorithm})"
|
845
|
+
end
|
846
|
+
(<<-EOS).gsub!(/^(?!$)/, " ").gsub!(/^/) { line_prefix }.chomp!
|
847
|
+
A CRC calculator for #{crcname}.
|
848
|
+
|
849
|
+
This code is auto generated by <https://rubygems.org/gems/crc>.
|
850
|
+
|
851
|
+
* License:: Creative Commons License Zero (CC0 / Public Domain)
|
852
|
+
See <https://creativecommons.org/publicdomain/zero/1.0/>
|
853
|
+
* Version:: crc-#{CRC::VERSION} (powered by #{RUBY_DESCRIPTION})
|
854
|
+
* Generated at:: #{Time.now.strftime "%Y-%m-%d"}
|
855
|
+
* Algorithm:: #{algorithm}
|
856
|
+
* Need available memory:: about 1 MiB
|
857
|
+
EOS
|
858
|
+
end
|
859
|
+
|
860
|
+
def self.export_slicing_table(table, baseindent, indentsize, tableprefix, tablesuffix, elementprefix, elementsuffix, lineinelements, &hexnum)
|
861
|
+
indent = ->(n = 0) { baseindent + (" " * (indentsize * n)) }
|
862
|
+
tableprefix &&= "#{indent[0]}#{tableprefix}\n"
|
863
|
+
tablesuffix &&= "\n#{indent[0]}#{tablesuffix}"
|
864
|
+
if table[0].kind_of?(Array)
|
865
|
+
(<<-TABLE2).chomp!
|
866
|
+
#{tableprefix}#{indent[1]}#{elementprefix}
|
867
|
+
#{
|
868
|
+
table = table.map { |tt|
|
869
|
+
tt = tt.map(&hexnum)
|
870
|
+
%(#{indent[2]}#{tt.each_slice(lineinelements).map { |ttt| ttt.join(", ") }.join(",\n#{indent[2]}")}\n)
|
871
|
+
}.join("#{indent[1]}#{elementsuffix},\n#{indent[1]}#{elementprefix}\n").chomp!
|
872
|
+
}
|
873
|
+
#{indent[1]}#{elementsuffix}#{tablesuffix}
|
874
|
+
TABLE2
|
875
|
+
else
|
876
|
+
table = table.map(&hexnum)
|
877
|
+
%(#{indent[1]}#{table.each_slice(lineinelements).map { |ttt| ttt.join(", ") }.join(",\n#{indent[1]}")}\n).chomp!
|
878
|
+
end
|
879
|
+
end
|
880
|
+
end
|
881
|
+
end
|