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