author | Claus Gittinger <cg@exept.de> |
Mon, 09 Jan 2012 22:30:11 +0100 | |
changeset 2703 | 00e8619a241b |
parent 2702 | bbfd7a7031a6 |
child 2704 | 9027649ca910 |
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 |
|
2570
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
45 |
You can also create an instace perforing the Castagnioli CRC-32C |
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 |
|
2310 | 52 |
Use CRC to protect against communication errors; |
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: |
2307 | 59 |
150000 Kb/s on 2Ghz Duo |
1192 | 60 |
|
61 |
[author:] |
|
62 |
Stefan Vogel (stefan@zwerg) |
|
63 |
||
64 |
[instance variables:] |
|
65 |
||
66 |
[class variables:] |
|
67 |
||
68 |
[see also:] |
|
69 |
SHA1Stream |
|
70 |
MD5Stream |
|
71 |
||
72 |
" |
|
73 |
! |
|
74 |
||
75 |
examples |
|
76 |
" |
|
77 |
||
2306 | 78 |
expect 60C1D0A0 |
1192 | 79 |
[exBegin] |
1977 | 80 |
self information:(CRC32Stream hashValueOf:'resume') hexPrintString |
1976 | 81 |
[exEnd] |
1192 | 82 |
|
2306 | 83 |
expect 16r60C1D0A0 |
1192 | 84 |
[exBegin] |
1978
f7330488b12c
changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents:
1977
diff
changeset
|
85 |
self information:(CRC32Stream new |
f7330488b12c
changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents:
1977
diff
changeset
|
86 |
nextPut:$r; |
f7330488b12c
changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents:
1977
diff
changeset
|
87 |
nextPut:$e; |
f7330488b12c
changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents:
1977
diff
changeset
|
88 |
nextPut:$s; |
f7330488b12c
changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents:
1977
diff
changeset
|
89 |
nextPut:$u; |
f7330488b12c
changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents:
1977
diff
changeset
|
90 |
nextPut:$m; |
f7330488b12c
changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents:
1977
diff
changeset
|
91 |
nextPut:$e; |
f7330488b12c
changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents:
1977
diff
changeset
|
92 |
hashValue) hexPrintString |
f7330488b12c
changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents:
1977
diff
changeset
|
93 |
[exEnd] |
f7330488b12c
changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents:
1977
diff
changeset
|
94 |
|
2306 | 95 |
expect 16r70E46888: |
1192 | 96 |
[exBegin] |
2702
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
97 |
self information:(CRC32Stream hashValueOf:#[1 2 3 4 5 6 7]) hexPrintString |
1192 | 98 |
[exEnd] |
99 |
||
2306 | 100 |
expect 16r8CD04C73: |
1192 | 101 |
[exBegin] |
102 |
self information:((CRC32Stream hashValueOf:#[16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF |
|
103 |
16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF |
|
104 |
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
|
105 |
16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF]) hexPrintString) |
1192 | 106 |
[exEnd] |
2306 | 107 |
|
108 |
expect 16r86D7D79A: |
|
109 |
timing throughput: |
|
110 |
[exBegin] |
|
111 |
|hashStream n t| |
|
112 |
||
113 |
hashStream := CRC32Stream new. |
|
114 |
n := 100000. |
|
115 |
t := Time millisecondsToRun:[ |
|
116 |
n timesRepeat:[ |
|
117 |
hashStream nextPutAll:'12345678901234567890123456789012345678901234567890'. |
|
118 |
]. |
|
119 |
]. |
|
120 |
t := (t / 1000) asFloat. |
|
2702
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
121 |
Transcript show:'crc32:'; showCR: hashStream hashValue hexPrintString. |
2306 | 122 |
Transcript show:t; show:' seconds for '; show:(50*n/1024) asFloat; showCR:' Kb'. |
123 |
Transcript show:(n*50/1024 / t); showCR:' Kb/s' |
|
124 |
[exEnd] |
|
125 |
||
1192 | 126 |
" |
127 |
! ! |
|
128 |
||
129 |
!CRC32Stream class methodsFor:'initialization'! |
|
130 |
||
2570
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
131 |
crcTableFor:generatorPolynomInteger |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
132 |
|crcTable| |
1192 | 133 |
|
2570
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
134 |
crcTable := (CrcTables at:generatorPolynomInteger ifAbsent:nil). |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
135 |
crcTable isNil ifTrue:[ |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
136 |
crcTable := IntegerArray new:256. |
1192 | 137 |
|
2570
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
138 |
0 to:255 do:[:count| |i| |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
139 |
i := count. |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
140 |
8 timesRepeat:[ |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
141 |
(i bitTest:1) ifTrue:[ |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
142 |
i := generatorPolynomInteger bitXor:(i bitShift:-1) |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
143 |
] ifFalse:[ |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
144 |
i := i bitShift:-1 |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
145 |
] |
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 |
crcTable at:count+1 put:i. |
1192 | 148 |
]. |
2570
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
149 |
CrcTables at:generatorPolynomInteger put:crcTable. |
1192 | 150 |
]. |
2570
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
151 |
^ crcTable. |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
152 |
! |
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 |
initialize |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
155 |
CrcTables := Dictionary new. |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
156 |
! ! |
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 |
!CRC32Stream class methodsFor:'instance creation'! |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
159 |
|
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
160 |
generatorPolynom:anInteger |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
161 |
^ self basicNew generatorPolynom:anInteger |
1192 | 162 |
|
163 |
" |
|
2570
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
164 |
self assert:((self generatorPolynom:16r82F63B78) |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
165 |
nextPut:'123456789'; |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
166 |
hashValue) = 16rE3069283 |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
167 |
" |
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 |
|
2572 | 170 |
newCrc32c |
2570
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
171 |
"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
|
172 |
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
|
173 |
(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
|
174 |
|
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
175 |
^ self basicNew generatorPolynom:16r82F63B78 |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
176 |
|
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
177 |
" |
2572 | 178 |
self assert:((self newCrc32c) |
2570
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
179 |
nextPut:'123456789'; |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
180 |
hashValue) = 16rE3069283 |
1192 | 181 |
" |
182 |
! ! |
|
183 |
||
184 |
!CRC32Stream class methodsFor:'testing'! |
|
185 |
||
186 |
testVector |
|
2703 | 187 |
"/ obsolete - moved to TestHashAlgorithms unit test. |
1192 | 188 |
^ #( |
2569 | 189 |
('' 0) |
190 |
||
191 |
('123456789' 16rCBF43926) |
|
192 |
||
1192 | 193 |
(#[16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF |
194 |
16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF |
|
195 |
16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF |
|
196 |
16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF] |
|
197 |
2362461299) |
|
198 |
||
199 |
(#[0 0 0 0 0 0 0 0 0 0 |
|
200 |
0 0 0 0 0 0 0 0 0 0 |
|
201 |
0 0 0 0 0 0 0 0 0 0 |
|
202 |
0 0 0 0 0 0 0 0 0 0] |
|
203 |
3924573617) |
|
204 |
||
205 |
(#[0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
206 |
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39] |
|
207 |
228994620 ) |
|
208 |
) |
|
209 |
||
210 |
" |
|
211 |
self test |
|
212 |
" |
|
2703 | 213 |
|
214 |
"Modified (comment): / 09-01-2012 / 21:49:57 / cg" |
|
1192 | 215 |
! ! |
216 |
||
2570
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
217 |
!CRC32Stream methodsFor:'accessing'! |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
218 |
|
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
219 |
generatorPolynom |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
220 |
"answer the generator polynom" |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
221 |
|
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
222 |
^ generatorPolynom |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
223 |
! |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
224 |
|
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
225 |
generatorPolynom:anInteger |
2571
12d5e280eea1
comment/format in: #generatorPolynom:
Stefan Vogel <sv@exept.de>
parents:
2570
diff
changeset
|
226 |
"set the generator polynom for this instance. |
12d5e280eea1
comment/format in: #generatorPolynom:
Stefan Vogel <sv@exept.de>
parents:
2570
diff
changeset
|
227 |
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
|
228 |
|
2570
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
229 |
generatorPolynom := anInteger. |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
230 |
crc := 16rFFFFFFFF. |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
231 |
crcTable := self class crcTableFor:generatorPolynom. |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
232 |
! ! |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
233 |
|
1192 | 234 |
!CRC32Stream methodsFor:'initialization'! |
235 |
||
236 |
initialize |
|
2570
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
237 |
"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
|
238 |
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 | 239 |
|
2570
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
240 |
self generatorPolynom:16rEDB88320 |
1192 | 241 |
! ! |
242 |
||
243 |
!CRC32Stream methodsFor:'queries'! |
|
244 |
||
245 |
hashValue |
|
246 |
"return the computed CRC" |
|
247 |
||
248 |
^ crc bitXor:16rFFFFFFFF |
|
249 |
! ! |
|
250 |
||
251 |
!CRC32Stream methodsFor:'writing'! |
|
252 |
||
2702
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
253 |
nextPutBytes:count from:anObject startingAt:start |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
254 |
"add the hash of anObject to the computed hash so far." |
1192 | 255 |
|
1977 | 256 |
| argSize "{ Class:SmallInteger }" |
257 |
byte "{ Class:SmallInteger }" | |
|
258 |
||
2306 | 259 |
%{ |
2702
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
260 |
if (__bothSmallInteger(count, start)) { |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
261 |
int len, offs; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
262 |
int objSize, nInstVars, nInstBytes; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
263 |
unsigned char *extPtr; |
2306 | 264 |
|
2702
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
265 |
len = __intVal(count); |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
266 |
offs = __intVal(start) - 1; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
267 |
|
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
268 |
if (__isExternalBytesLike(anObject)) { |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
269 |
OBJ sz; |
2306 | 270 |
|
2702
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
271 |
nInstBytes = 0; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
272 |
extPtr = (char *)__externalBytesAddress(anObject); |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
273 |
sz = __externalBytesSize(anObject); |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
274 |
if (__isSmallInteger(sz)) { |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
275 |
objSize = __intVal(sz); |
2306 | 276 |
} else { |
2702
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
277 |
objSize = 0; /* unknown */ |
2306 | 278 |
} |
2702
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
279 |
} else { |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
280 |
OBJ oClass; |
2307 | 281 |
|
2702
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
282 |
oClass = __Class(anObject); |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
283 |
switch (__intVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) { |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
284 |
case BYTEARRAY: |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
285 |
case WORDARRAY: |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
286 |
case LONGARRAY: |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
287 |
case SWORDARRAY: |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
288 |
case SLONGARRAY: |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
289 |
case FLOATARRAY: |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
290 |
case DOUBLEARRAY: |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
291 |
break; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
292 |
default: |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
293 |
goto bad; |
2307 | 294 |
} |
2702
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
295 |
nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars); |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
296 |
nInstBytes = __OBJS2BYTES__(nInstVars); |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
297 |
// 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
|
298 |
// 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
|
299 |
objSize = __Size(anObject) - OHDR_SIZE - nInstBytes; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
300 |
extPtr = (char *)__byteArrayVal(anObject)+nInstBytes; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
301 |
} |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
302 |
|
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
303 |
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
|
304 |
{ |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
305 |
unsigned int _crc; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
306 |
unsigned int *_crcTable; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
307 |
unsigned char *cp = extPtr+offs; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
308 |
unsigned n = len; |
2306 | 309 |
|
2702
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
310 |
if (__isSmallInteger(__INST(crc)) ) { |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
311 |
_crc = (unsigned) (__intVal( __INST(crc) )); |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
312 |
} else { |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
313 |
_crc = __unsignedLongIntVal( __INST(crc) ); |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
314 |
} |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
315 |
|
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
316 |
_crcTable = __integerArrayVal( __INST(crcTable) ); |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
317 |
|
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
318 |
while (n > 3) { |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
319 |
unsigned _idx; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
320 |
|
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
321 |
_idx = (_crc ^ cp[0]) & 0xFF; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
322 |
_crc = _crcTable[_idx] ^ (_crc >> 8); |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
323 |
_idx = (_crc ^ cp[1]) & 0xFF; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
324 |
_crc = _crcTable[_idx] ^ (_crc >> 8); |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
325 |
_idx = (_crc ^ cp[2]) & 0xFF; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
326 |
_crc = _crcTable[_idx] ^ (_crc >> 8); |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
327 |
_idx = (_crc ^ cp[3]) & 0xFF; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
328 |
_crc = _crcTable[_idx] ^ (_crc >> 8); |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
329 |
cp += 4; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
330 |
n -= 4; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
331 |
} |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
332 |
while (n > 0) { |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
333 |
unsigned _byte = *cp++; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
334 |
unsigned _idx; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
335 |
|
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
336 |
_idx = (_crc ^ _byte) & 0xFF; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
337 |
_crc = _crcTable[_idx] ^ (_crc >> 8); |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
338 |
n--; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
339 |
} |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
340 |
__INST(crc) = __MKUINT(_crc); |
2306 | 341 |
} |
2702
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
342 |
RETURN (count); |
2306 | 343 |
} |
344 |
} |
|
2702
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
345 |
bad: ; |
2306 | 346 |
%}. |
2702
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
347 |
self error:'invalid argument' |
1977 | 348 |
|
2702
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
349 |
"Created: / 09-01-2012 / 16:48:35 / cg" |
1192 | 350 |
! ! |
351 |
||
352 |
!CRC32Stream class methodsFor:'documentation'! |
|
353 |
||
354 |
version |
|
2703 | 355 |
^ '$Header: /cvs/stx/stx/libbasic2/CRC32Stream.st,v 1.19 2012-01-09 21:30:11 cg Exp $' |
2306 | 356 |
! |
357 |
||
358 |
version_CVS |
|
2703 | 359 |
^ '$Header: /cvs/stx/stx/libbasic2/CRC32Stream.st,v 1.19 2012-01-09 21:30:11 cg Exp $' |
1192 | 360 |
! ! |
361 |
||
362 |
CRC32Stream initialize! |