author | Claus Gittinger <cg@exept.de> |
Sun, 11 Oct 2009 00:10:50 +0200 | |
changeset 2307 | 213162f0a57e |
parent 2306 | 75d9592b9d8a |
child 2310 | c6f63285e4c8 |
permissions | -rw-r--r-- |
1975 | 1 |
"{ Package: 'stx:libbasic2' }" |
1192 | 2 |
|
3 |
HashStream subclass:#CRC32Stream |
|
4 |
instanceVariableNames:'crc' |
|
5 |
classVariableNames:'CrcTable' |
|
6 |
poolDictionaries:'' |
|
7 |
category:'System-Crypt-Streams' |
|
8 |
! |
|
9 |
||
10 |
!CRC32Stream class methodsFor:'documentation'! |
|
11 |
||
12 |
documentation |
|
13 |
" |
|
14 |
Standard CRC method as defined by ISO 3309 [ISO-3309] or ITU-T V.42 [ITU-T-V42]. |
|
15 |
The CRC polynomial employed is |
|
16 |
||
17 |
x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x+1 |
|
18 |
||
19 |
Use CRC to protect against communication errors. Do NOT use CRC for cryptography. |
|
20 |
Use SHA1Stream or MD5Stream instead. |
|
21 |
||
2306 | 22 |
throughput: |
2307 | 23 |
150000 Kb/s on 2Ghz Duo |
1192 | 24 |
|
25 |
[author:] |
|
26 |
Stefan Vogel (stefan@zwerg) |
|
27 |
||
28 |
[instance variables:] |
|
29 |
||
30 |
[class variables:] |
|
31 |
||
32 |
[see also:] |
|
33 |
SHA1Stream |
|
34 |
MD5Stream |
|
35 |
||
36 |
" |
|
37 |
! |
|
38 |
||
39 |
examples |
|
40 |
" |
|
41 |
||
2306 | 42 |
expect 60C1D0A0 |
1192 | 43 |
[exBegin] |
1977 | 44 |
self information:(CRC32Stream hashValueOf:'resume') hexPrintString |
1976 | 45 |
[exEnd] |
1192 | 46 |
|
2306 | 47 |
expect 16r60C1D0A0 |
1192 | 48 |
[exBegin] |
1978
f7330488b12c
changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents:
1977
diff
changeset
|
49 |
self information:(CRC32Stream new |
f7330488b12c
changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents:
1977
diff
changeset
|
50 |
nextPut:$r; |
f7330488b12c
changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents:
1977
diff
changeset
|
51 |
nextPut:$e; |
f7330488b12c
changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents:
1977
diff
changeset
|
52 |
nextPut:$s; |
f7330488b12c
changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents:
1977
diff
changeset
|
53 |
nextPut:$u; |
f7330488b12c
changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents:
1977
diff
changeset
|
54 |
nextPut:$m; |
f7330488b12c
changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents:
1977
diff
changeset
|
55 |
nextPut:$e; |
f7330488b12c
changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents:
1977
diff
changeset
|
56 |
hashValue) hexPrintString |
f7330488b12c
changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents:
1977
diff
changeset
|
57 |
[exEnd] |
f7330488b12c
changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents:
1977
diff
changeset
|
58 |
|
2306 | 59 |
expect 16r98DC9ED7: |
1978
f7330488b12c
changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents:
1977
diff
changeset
|
60 |
[exBegin] |
1192 | 61 |
self information:(CRC32Stream new nextPut:4711; hashValue) printString |
1976 | 62 |
[exEnd] |
1192 | 63 |
|
2306 | 64 |
expect 16r70E46888: |
1192 | 65 |
[exBegin] |
66 |
self information:(CRC32Stream hashValueOf:#[1 2 3 4 5 6 7]) printString |
|
67 |
[exEnd] |
|
68 |
||
2306 | 69 |
expect 16r8CD04C73: |
1192 | 70 |
[exBegin] |
71 |
self information:((CRC32Stream hashValueOf:#[16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF |
|
72 |
16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF |
|
73 |
16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF |
|
1975 | 74 |
16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF]) digitBytes hexPrintString) |
1192 | 75 |
[exEnd] |
2306 | 76 |
|
77 |
expect 16r86D7D79A: |
|
78 |
timing throughput: |
|
79 |
[exBegin] |
|
80 |
|hashStream n t| |
|
81 |
||
82 |
hashStream := CRC32Stream new. |
|
83 |
n := 100000. |
|
84 |
t := Time millisecondsToRun:[ |
|
85 |
n timesRepeat:[ |
|
86 |
hashStream nextPutAll:'12345678901234567890123456789012345678901234567890'. |
|
87 |
]. |
|
88 |
]. |
|
89 |
t := (t / 1000) asFloat. |
|
90 |
Transcript show:'crc32:'; showCR: hashStream hashValue. |
|
91 |
Transcript show:t; show:' seconds for '; show:(50*n/1024) asFloat; showCR:' Kb'. |
|
92 |
Transcript show:(n*50/1024 / t); showCR:' Kb/s' |
|
93 |
[exEnd] |
|
94 |
||
1192 | 95 |
" |
96 |
! ! |
|
97 |
||
98 |
!CRC32Stream class methodsFor:'initialization'! |
|
99 |
||
100 |
initialize |
|
101 |
||
102 |
CrcTable := IntegerArray new:256. |
|
103 |
||
104 |
0 to:255 do:[:count| |i| |
|
105 |
i := count. |
|
106 |
8 timesRepeat:[ |
|
107 |
(i bitTest:1) ifTrue:[ |
|
108 |
i := 16rEDB88320 bitXor:(i bitShift:-1) |
|
109 |
] ifFalse:[ |
|
110 |
i := i bitShift:-1 |
|
111 |
] |
|
112 |
]. |
|
113 |
CrcTable at:count+1 put:i. |
|
114 |
]. |
|
115 |
||
116 |
" |
|
117 |
self initialize |
|
118 |
" |
|
119 |
! ! |
|
120 |
||
121 |
!CRC32Stream class methodsFor:'testing'! |
|
122 |
||
123 |
testVector |
|
124 |
"define a testvector to test the implementation" |
|
125 |
||
126 |
^ #( |
|
127 |
(#[16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF |
|
128 |
16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF |
|
129 |
16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF |
|
130 |
16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF] |
|
131 |
2362461299) |
|
132 |
||
133 |
(#[0 0 0 0 0 0 0 0 0 0 |
|
134 |
0 0 0 0 0 0 0 0 0 0 |
|
135 |
0 0 0 0 0 0 0 0 0 0 |
|
136 |
0 0 0 0 0 0 0 0 0 0] |
|
137 |
3924573617) |
|
138 |
||
139 |
(#[0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
140 |
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39] |
|
141 |
228994620 ) |
|
142 |
) |
|
143 |
||
144 |
" |
|
145 |
self test |
|
146 |
" |
|
147 |
! ! |
|
148 |
||
149 |
!CRC32Stream methodsFor:'initialization'! |
|
150 |
||
151 |
initialize |
|
152 |
"initialize the CRC" |
|
153 |
||
154 |
crc := 16rFFFFFFFF. |
|
155 |
! ! |
|
156 |
||
157 |
!CRC32Stream methodsFor:'queries'! |
|
158 |
||
159 |
hashValue |
|
160 |
"return the computed CRC" |
|
161 |
||
162 |
^ crc bitXor:16rFFFFFFFF |
|
163 |
! ! |
|
164 |
||
165 |
!CRC32Stream methodsFor:'writing'! |
|
166 |
||
167 |
nextPut:arg |
|
168 |
"add the hash of anObject to the computed hash so far. |
|
169 |
anObject can be a Character, SmallInteger, or Character-, Byte- |
|
170 |
Integer-Array" |
|
171 |
||
1977 | 172 |
| argSize "{ Class:SmallInteger }" |
173 |
byte "{ Class:SmallInteger }" | |
|
174 |
||
2306 | 175 |
%{ |
176 |
#if 1 |
|
177 |
unsigned char *cp; |
|
178 |
int n; |
|
179 |
unsigned char c; |
|
180 |
||
181 |
if (__isString(arg)) { |
|
182 |
cp = __stringVal(arg); |
|
183 |
n = __stringSize(arg); |
|
184 |
goto doBytes; |
|
185 |
} |
|
186 |
||
187 |
if (__isByteArray(arg)) { |
|
188 |
cp = __byteArrayVal(arg); |
|
189 |
n = __byteArraySize(arg); |
|
190 |
doBytes: |
|
191 |
{ |
|
192 |
unsigned int _crc; |
|
193 |
unsigned int *_crcTable; |
|
194 |
||
195 |
if (__isSmallInteger(__INST(crc)) ) { |
|
196 |
_crc = (unsigned) (__intVal( __INST(crc) )); |
|
197 |
} else { |
|
198 |
_crc = __unsignedLongIntVal( __INST(crc) ); |
|
199 |
} |
|
200 |
||
201 |
_crcTable = __integerArrayVal( @global(CrcTable) ); |
|
202 |
||
2307 | 203 |
while (n > 3) { |
204 |
unsigned _idx; |
|
205 |
||
206 |
_idx = (_crc ^ cp[0]) & 0xFF; |
|
207 |
_crc = _crcTable[_idx] ^ (_crc >> 8); |
|
208 |
_idx = (_crc ^ cp[1]) & 0xFF; |
|
209 |
_crc = _crcTable[_idx] ^ (_crc >> 8); |
|
210 |
_idx = (_crc ^ cp[2]) & 0xFF; |
|
211 |
_crc = _crcTable[_idx] ^ (_crc >> 8); |
|
212 |
_idx = (_crc ^ cp[3]) & 0xFF; |
|
213 |
_crc = _crcTable[_idx] ^ (_crc >> 8); |
|
214 |
cp += 4; |
|
215 |
n -= 4; |
|
216 |
} |
|
2306 | 217 |
while (n > 0) { |
218 |
unsigned _byte = *cp++; |
|
219 |
unsigned _idx; |
|
220 |
||
221 |
_idx = (_crc ^ _byte) & 0xFF; |
|
222 |
_crc = _crcTable[_idx] ^ (_crc >> 8); |
|
223 |
n--; |
|
224 |
} |
|
225 |
__INST(crc) = __MKUINT(_crc); |
|
226 |
} |
|
227 |
RETURN (self ); |
|
228 |
} |
|
229 |
if (__isCharacter(arg)) { |
|
230 |
unsigned int value; |
|
231 |
||
232 |
value = __intVal(_characterVal(arg)); |
|
233 |
if ((unsigned)value <= 0xFF) { |
|
234 |
c = value; |
|
235 |
cp = &c; |
|
236 |
n = 1; |
|
237 |
goto doBytes; |
|
238 |
} |
|
239 |
} |
|
240 |
#endif |
|
241 |
%}. |
|
1977 | 242 |
arg isByteCollection ifTrue:[ |
243 |
argSize := arg size. |
|
244 |
1 to:argSize do:[:n| |
|
245 |
byte := arg byteAt:n. |
|
246 |
crc := (CrcTable at:((crc bitXor:byte) bitAnd:16rFF)+1) |
|
247 |
bitXor:(crc bitShift:-8). |
|
248 |
]. |
|
249 |
^ self. |
|
250 |
]. |
|
251 |
||
1192 | 252 |
arg isCharacter ifTrue:[ |
1978
f7330488b12c
changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents:
1977
diff
changeset
|
253 |
byte := arg codePoint. |
f7330488b12c
changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents:
1977
diff
changeset
|
254 |
byte < 256 ifTrue:[ |
f7330488b12c
changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents:
1977
diff
changeset
|
255 |
crc := (CrcTable at:((crc bitXor:byte) bitAnd:16rFF)+1) |
f7330488b12c
changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents:
1977
diff
changeset
|
256 |
bitXor:(crc bitShift:-8). |
f7330488b12c
changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents:
1977
diff
changeset
|
257 |
^ self. |
f7330488b12c
changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents:
1977
diff
changeset
|
258 |
]. |
f7330488b12c
changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents:
1977
diff
changeset
|
259 |
byte digitBytes do:[:eachByte| |
f7330488b12c
changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents:
1977
diff
changeset
|
260 |
crc := (CrcTable at:((crc bitXor:eachByte) bitAnd:16rFF)+1) |
f7330488b12c
changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents:
1977
diff
changeset
|
261 |
bitXor:(crc bitShift:-8). |
f7330488b12c
changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents:
1977
diff
changeset
|
262 |
]. |
1192 | 263 |
^ self. |
264 |
]. |
|
265 |
||
266 |
arg isInteger ifTrue:[ |
|
267 |
arg < 256 ifTrue:[ |
|
268 |
crc := (CrcTable at:((crc bitXor:arg) bitAnd:16rFF)+1) |
|
269 |
bitXor:(crc bitShift:-8). |
|
270 |
^ self. |
|
271 |
]. |
|
1977 | 272 |
arg digitBytes do:[:eachByte| |
273 |
crc := (CrcTable at:((crc bitXor:eachByte) bitAnd:16rFF)+1) |
|
1192 | 274 |
bitXor:(crc bitShift:-8). |
275 |
]. |
|
276 |
^ self. |
|
277 |
]. |
|
1977 | 278 |
|
1192 | 279 |
arg isCollection ifTrue:[ |
1977 | 280 |
arg do:[:eachElement| |
281 |
self nextPut:eachElement |
|
1192 | 282 |
]. |
283 |
^ self. |
|
284 |
]. |
|
285 |
||
286 |
self error:'unsupported argument'. |
|
287 |
! ! |
|
288 |
||
289 |
!CRC32Stream class methodsFor:'documentation'! |
|
290 |
||
291 |
version |
|
2307 | 292 |
^ '$Header: /cvs/stx/stx/libbasic2/CRC32Stream.st,v 1.7 2009-10-10 22:10:50 cg Exp $' |
2306 | 293 |
! |
294 |
||
295 |
version_CVS |
|
2307 | 296 |
^ '$Header: /cvs/stx/stx/libbasic2/CRC32Stream.st,v 1.7 2009-10-10 22:10:50 cg Exp $' |
1192 | 297 |
! ! |
298 |
||
299 |
CRC32Stream initialize! |