author | Claus Gittinger <cg@exept.de> |
Tue, 02 Apr 2013 10:59:01 +0200 | |
changeset 2962 | dc468681c480 |
parent 2905 | 73332f14d929 |
child 2963 | 76da9963f31e |
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 |
|
2570
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
15 |
instanceVariableNames:'crc generatorPolynom crcTable' |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
16 |
classVariableNames:'CrcTables' |
1192 | 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]. |
|
2570
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
40 |
The default CRC polynomial employed is |
1192 | 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 |
|
2569 | 43 |
(or 16r04C11DB7) |
1192 | 44 |
|
2746 | 45 |
You can also create an instace performing the Castagnioli CRC-32C |
2570
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
46 |
(used in iSCSI & SCTP, G.hn payload, SSE4.2): |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
47 |
|
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
48 |
self newCrc32c |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
49 |
|
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
50 |
x32 + x28 + x27 + x26 + x25 + x23 + x22 + x20 + x19 + x18 + x14 + x13 + x11 + x10 + x9 + x8 + x6 + 1 |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
51 |
|
2746 | 52 |
Only use CRC to protect against communication errors; |
2310 | 53 |
do NOT use CRC for cryptography - use SHA1Stream or MD5Stream instead. |
54 |
||
55 |
Notice that this CRC is also used with PNG images - therefore, its performance |
|
56 |
directly affects png image processing. |
|
1192 | 57 |
|
2306 | 58 |
throughput: |
2905 | 59 |
157000 Kb/s on a 2.5Ghz 64X2 Athlon 4800+ (64bit) |
2307 | 60 |
150000 Kb/s on 2Ghz Duo |
1192 | 61 |
|
62 |
[author:] |
|
63 |
Stefan Vogel (stefan@zwerg) |
|
64 |
||
65 |
[instance variables:] |
|
66 |
||
67 |
[class variables:] |
|
68 |
||
69 |
[see also:] |
|
70 |
SHA1Stream |
|
71 |
MD5Stream |
|
72 |
||
73 |
" |
|
74 |
! |
|
75 |
||
76 |
examples |
|
77 |
" |
|
78 |
||
2306 | 79 |
expect 60C1D0A0 |
1192 | 80 |
[exBegin] |
1977 | 81 |
self information:(CRC32Stream hashValueOf:'resume') hexPrintString |
1976 | 82 |
[exEnd] |
1192 | 83 |
|
2306 | 84 |
expect 16r60C1D0A0 |
1192 | 85 |
[exBegin] |
1978
f7330488b12c
changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents:
1977
diff
changeset
|
86 |
self information:(CRC32Stream new |
f7330488b12c
changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents:
1977
diff
changeset
|
87 |
nextPut:$r; |
f7330488b12c
changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents:
1977
diff
changeset
|
88 |
nextPut:$e; |
f7330488b12c
changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents:
1977
diff
changeset
|
89 |
nextPut:$s; |
f7330488b12c
changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents:
1977
diff
changeset
|
90 |
nextPut:$u; |
f7330488b12c
changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents:
1977
diff
changeset
|
91 |
nextPut:$m; |
f7330488b12c
changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents:
1977
diff
changeset
|
92 |
nextPut:$e; |
f7330488b12c
changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents:
1977
diff
changeset
|
93 |
hashValue) hexPrintString |
f7330488b12c
changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents:
1977
diff
changeset
|
94 |
[exEnd] |
f7330488b12c
changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents:
1977
diff
changeset
|
95 |
|
2306 | 96 |
expect 16r70E46888: |
1192 | 97 |
[exBegin] |
2702
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
98 |
self information:(CRC32Stream hashValueOf:#[1 2 3 4 5 6 7]) hexPrintString |
1192 | 99 |
[exEnd] |
100 |
||
2306 | 101 |
expect 16r8CD04C73: |
1192 | 102 |
[exBegin] |
103 |
self information:((CRC32Stream hashValueOf:#[16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF |
|
104 |
16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF |
|
105 |
16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF |
|
2702
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
106 |
16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF]) hexPrintString) |
1192 | 107 |
[exEnd] |
2306 | 108 |
|
109 |
expect 16r86D7D79A: |
|
110 |
timing throughput: |
|
111 |
[exBegin] |
|
112 |
|hashStream n t| |
|
113 |
||
114 |
hashStream := CRC32Stream new. |
|
2962 | 115 |
n := 1000000. |
2306 | 116 |
t := Time millisecondsToRun:[ |
117 |
n timesRepeat:[ |
|
118 |
hashStream nextPutAll:'12345678901234567890123456789012345678901234567890'. |
|
119 |
]. |
|
120 |
]. |
|
121 |
t := (t / 1000) asFloat. |
|
2702
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
122 |
Transcript show:'crc32:'; showCR: hashStream hashValue hexPrintString. |
2306 | 123 |
Transcript show:t; show:' seconds for '; show:(50*n/1024) asFloat; showCR:' Kb'. |
124 |
Transcript show:(n*50/1024 / t); showCR:' Kb/s' |
|
125 |
[exEnd] |
|
126 |
||
1192 | 127 |
" |
128 |
! ! |
|
129 |
||
130 |
!CRC32Stream class methodsFor:'initialization'! |
|
131 |
||
2570
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
132 |
crcTableFor:generatorPolynomInteger |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
133 |
|crcTable| |
1192 | 134 |
|
2570
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
135 |
crcTable := (CrcTables at:generatorPolynomInteger ifAbsent:nil). |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
136 |
crcTable isNil ifTrue:[ |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
137 |
crcTable := IntegerArray new:256. |
1192 | 138 |
|
2570
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
139 |
0 to:255 do:[:count| |i| |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
140 |
i := count. |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
141 |
8 timesRepeat:[ |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
142 |
(i bitTest:1) ifTrue:[ |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
143 |
i := generatorPolynomInteger bitXor:(i bitShift:-1) |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
144 |
] ifFalse:[ |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
145 |
i := i bitShift:-1 |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
146 |
] |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
147 |
]. |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
148 |
crcTable at:count+1 put:i. |
1192 | 149 |
]. |
2570
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
150 |
CrcTables at:generatorPolynomInteger put:crcTable. |
1192 | 151 |
]. |
2570
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
152 |
^ crcTable. |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
153 |
! |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
154 |
|
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
155 |
initialize |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
156 |
CrcTables := Dictionary new. |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
157 |
! ! |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
158 |
|
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
159 |
!CRC32Stream class methodsFor:'instance creation'! |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
160 |
|
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
161 |
generatorPolynom:anInteger |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
162 |
^ self basicNew generatorPolynom:anInteger |
1192 | 163 |
|
164 |
" |
|
2570
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
165 |
self assert:((self generatorPolynom:16r82F63B78) |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
166 |
nextPut:'123456789'; |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
167 |
hashValue) = 16rE3069283 |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
168 |
" |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
169 |
! |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
170 |
|
2572 | 171 |
newCrc32c |
2570
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
172 |
"return an instance of the Castagnoli CRC-32 |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
173 |
x32 + x28 + x27 + x26 + x25 + x23 + x22 + x20 + x19 + x18 + x14 + x13 + x11 + x10 + x9 + x8 + x6 + 1 |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
174 |
(used in iSCSI & SCTP, G.hn payload, SSE4.2)" |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
175 |
|
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
176 |
^ self basicNew generatorPolynom:16r82F63B78 |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
177 |
|
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
178 |
" |
2747
c6e4b8d6d9e7
comment/format in: #newCrc32c
Claus Gittinger <cg@exept.de>
parents:
2746
diff
changeset
|
179 |
Castagnoli crc: |
2572 | 180 |
self assert:((self newCrc32c) |
2570
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
181 |
nextPut:'123456789'; |
2747
c6e4b8d6d9e7
comment/format in: #newCrc32c
Claus Gittinger <cg@exept.de>
parents:
2746
diff
changeset
|
182 |
hashValue) = 3808858755. '16rE3069283' |
c6e4b8d6d9e7
comment/format in: #newCrc32c
Claus Gittinger <cg@exept.de>
parents:
2746
diff
changeset
|
183 |
|
c6e4b8d6d9e7
comment/format in: #newCrc32c
Claus Gittinger <cg@exept.de>
parents:
2746
diff
changeset
|
184 |
default crc: |
c6e4b8d6d9e7
comment/format in: #newCrc32c
Claus Gittinger <cg@exept.de>
parents:
2746
diff
changeset
|
185 |
self assert:((self new) |
c6e4b8d6d9e7
comment/format in: #newCrc32c
Claus Gittinger <cg@exept.de>
parents:
2746
diff
changeset
|
186 |
nextPut:'123456789'; |
c6e4b8d6d9e7
comment/format in: #newCrc32c
Claus Gittinger <cg@exept.de>
parents:
2746
diff
changeset
|
187 |
hashValue) = 3421780262. '16rCBF43926' |
1192 | 188 |
" |
2747
c6e4b8d6d9e7
comment/format in: #newCrc32c
Claus Gittinger <cg@exept.de>
parents:
2746
diff
changeset
|
189 |
|
c6e4b8d6d9e7
comment/format in: #newCrc32c
Claus Gittinger <cg@exept.de>
parents:
2746
diff
changeset
|
190 |
"Modified (comment): / 17-05-2012 / 12:48:53 / cg" |
1192 | 191 |
! ! |
192 |
||
2570
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
193 |
!CRC32Stream methodsFor:'accessing'! |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
194 |
|
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
195 |
generatorPolynom |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
196 |
"answer the generator polynom" |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
197 |
|
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
198 |
^ generatorPolynom |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
199 |
! |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
200 |
|
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
201 |
generatorPolynom:anInteger |
2571
12d5e280eea1
comment/format in: #generatorPolynom:
Stefan Vogel <sv@exept.de>
parents:
2570
diff
changeset
|
202 |
"set the generator polynom for this instance. |
12d5e280eea1
comment/format in: #generatorPolynom:
Stefan Vogel <sv@exept.de>
parents:
2570
diff
changeset
|
203 |
Note: you have to set the bit-reversed value, so the LSB must be first" |
12d5e280eea1
comment/format in: #generatorPolynom:
Stefan Vogel <sv@exept.de>
parents:
2570
diff
changeset
|
204 |
|
2570
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
205 |
generatorPolynom := anInteger. |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
206 |
crc := 16rFFFFFFFF. |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
207 |
crcTable := self class crcTableFor:generatorPolynom. |
2705 | 208 |
! |
209 |
||
210 |
reset |
|
211 |
"reset the current crc value" |
|
212 |
||
213 |
crc := 16rFFFFFFFF. |
|
214 |
||
215 |
"Created: / 12-01-2012 / 12:23:03 / cg" |
|
2570
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
216 |
! ! |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
217 |
|
1192 | 218 |
!CRC32Stream methodsFor:'initialization'! |
219 |
||
220 |
initialize |
|
2570
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
221 |
"initialize the CRC to CRC-32 ITU-T: |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
222 |
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" |
1192 | 223 |
|
2570
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
224 |
self generatorPolynom:16rEDB88320 |
1192 | 225 |
! ! |
226 |
||
227 |
!CRC32Stream methodsFor:'queries'! |
|
228 |
||
229 |
hashValue |
|
230 |
"return the computed CRC" |
|
231 |
||
232 |
^ crc bitXor:16rFFFFFFFF |
|
233 |
! ! |
|
234 |
||
235 |
!CRC32Stream methodsFor:'writing'! |
|
236 |
||
2702
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
237 |
nextPutBytes:count from:anObject startingAt:start |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
238 |
"add the hash of anObject to the computed hash so far." |
1192 | 239 |
|
1977 | 240 |
| argSize "{ Class:SmallInteger }" |
241 |
byte "{ Class:SmallInteger }" | |
|
242 |
||
2306 | 243 |
%{ |
2702
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
244 |
if (__bothSmallInteger(count, start)) { |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
245 |
int len, offs; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
246 |
int objSize, nInstVars, nInstBytes; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
247 |
unsigned char *extPtr; |
2306 | 248 |
|
2702
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
249 |
len = __intVal(count); |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
250 |
offs = __intVal(start) - 1; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
251 |
|
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
252 |
if (__isExternalBytesLike(anObject)) { |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
253 |
OBJ sz; |
2306 | 254 |
|
2702
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
255 |
nInstBytes = 0; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
256 |
extPtr = (char *)__externalBytesAddress(anObject); |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
257 |
sz = __externalBytesSize(anObject); |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
258 |
if (__isSmallInteger(sz)) { |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
259 |
objSize = __intVal(sz); |
2306 | 260 |
} else { |
2702
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
261 |
objSize = 0; /* unknown */ |
2306 | 262 |
} |
2702
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
263 |
} else { |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
264 |
OBJ oClass; |
2307 | 265 |
|
2702
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
266 |
oClass = __Class(anObject); |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
267 |
switch (__intVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) { |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
268 |
case BYTEARRAY: |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
269 |
case WORDARRAY: |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
270 |
case LONGARRAY: |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
271 |
case SWORDARRAY: |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
272 |
case SLONGARRAY: |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
273 |
case FLOATARRAY: |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
274 |
case DOUBLEARRAY: |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
275 |
break; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
276 |
default: |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
277 |
goto bad; |
2307 | 278 |
} |
2702
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
279 |
nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars); |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
280 |
nInstBytes = __OBJS2BYTES__(nInstVars); |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
281 |
// nInstBytes is the number of bytes occupied by pointer instance variables |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
282 |
// subtract from size and add to byte-pointer |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
283 |
objSize = __Size(anObject) - OHDR_SIZE - nInstBytes; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
284 |
extPtr = (char *)__byteArrayVal(anObject)+nInstBytes; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
285 |
} |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
286 |
|
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
287 |
if ((offs >= 0) && (len >= 0) && (objSize >= (len + offs))) { |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
288 |
{ |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
289 |
unsigned int _crc; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
290 |
unsigned int *_crcTable; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
291 |
unsigned char *cp = extPtr+offs; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
292 |
unsigned n = len; |
2306 | 293 |
|
2702
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
294 |
if (__isSmallInteger(__INST(crc)) ) { |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
295 |
_crc = (unsigned) (__intVal( __INST(crc) )); |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
296 |
} else { |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
297 |
_crc = __unsignedLongIntVal( __INST(crc) ); |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
298 |
} |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
299 |
|
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
300 |
_crcTable = __integerArrayVal( __INST(crcTable) ); |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
301 |
|
2746 | 302 |
#ifdef __LSBFIRST__ |
2962 | 303 |
if (((unsigned INT)cp & 3) == 0) { |
2746 | 304 |
// word aligned |
305 |
while (n >= 4) { |
|
306 |
unsigned word; |
|
307 |
unsigned _idx; |
|
308 |
||
309 |
word = ((unsigned int *)cp)[0]; |
|
310 |
_idx = (_crc ^ word) & 0xFF; |
|
311 |
_crc = _crcTable[_idx] ^ (_crc >> 8); |
|
312 |
_idx = (_crc ^ (word>>8)) & 0xFF; |
|
313 |
_crc = _crcTable[_idx] ^ (_crc >> 8); |
|
314 |
_idx = (_crc ^ (word>>16)) & 0xFF; |
|
315 |
_crc = _crcTable[_idx] ^ (_crc >> 8); |
|
316 |
_idx = (_crc ^ (word>>24)) & 0xFF; |
|
317 |
_crc = _crcTable[_idx] ^ (_crc >> 8); |
|
318 |
cp += 4; |
|
319 |
n -= 4; |
|
320 |
} |
|
321 |
} |
|
322 |
#endif |
|
2702
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
323 |
while (n > 3) { |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
324 |
unsigned _idx; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
325 |
|
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
326 |
_idx = (_crc ^ cp[0]) & 0xFF; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
327 |
_crc = _crcTable[_idx] ^ (_crc >> 8); |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
328 |
_idx = (_crc ^ cp[1]) & 0xFF; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
329 |
_crc = _crcTable[_idx] ^ (_crc >> 8); |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
330 |
_idx = (_crc ^ cp[2]) & 0xFF; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
331 |
_crc = _crcTable[_idx] ^ (_crc >> 8); |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
332 |
_idx = (_crc ^ cp[3]) & 0xFF; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
333 |
_crc = _crcTable[_idx] ^ (_crc >> 8); |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
334 |
cp += 4; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
335 |
n -= 4; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
336 |
} |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
337 |
while (n > 0) { |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
338 |
unsigned _byte = *cp++; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
339 |
unsigned _idx; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
340 |
|
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
341 |
_idx = (_crc ^ _byte) & 0xFF; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
342 |
_crc = _crcTable[_idx] ^ (_crc >> 8); |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
343 |
n--; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
344 |
} |
2962 | 345 |
#if POINTER_SIZE == 8 |
346 |
__INST(crc) = __MKSMALLINT(_crc); |
|
347 |
#else |
|
348 |
{ |
|
349 |
OBJ t; |
|
350 |
__INST(crc) = t = __MKUINT(_crc); __STORE(self, t); |
|
351 |
} |
|
352 |
#endif |
|
2306 | 353 |
} |
2702
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
354 |
RETURN (count); |
2306 | 355 |
} |
356 |
} |
|
2702
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
357 |
bad: ; |
2306 | 358 |
%}. |
2702
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
359 |
self error:'invalid argument' |
1977 | 360 |
|
2702
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
361 |
"Created: / 09-01-2012 / 16:48:35 / cg" |
1192 | 362 |
! ! |
363 |
||
364 |
!CRC32Stream class methodsFor:'documentation'! |
|
365 |
||
366 |
version |
|
2962 | 367 |
^ '$Header: /cvs/stx/stx/libbasic2/CRC32Stream.st,v 1.25 2013-04-02 08:59:01 cg Exp $' |
2306 | 368 |
! |
369 |
||
370 |
version_CVS |
|
2962 | 371 |
^ '$Header: /cvs/stx/stx/libbasic2/CRC32Stream.st,v 1.25 2013-04-02 08:59:01 cg Exp $' |
1192 | 372 |
! ! |
373 |
||
2905 | 374 |
|
1192 | 375 |
CRC32Stream initialize! |