author | Claus Gittinger <cg@exept.de> |
Thu, 12 Jan 2012 12:25:06 +0100 | |
changeset 2705 | 47bcf29473ad |
parent 2704 | 9027649ca910 |
child 2746 | d9d60eda74b3 |
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 |
||
2570
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
184 |
!CRC32Stream methodsFor:'accessing'! |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
185 |
|
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
186 |
generatorPolynom |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
187 |
"answer the generator polynom" |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
188 |
|
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
189 |
^ generatorPolynom |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
190 |
! |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
191 |
|
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
192 |
generatorPolynom:anInteger |
2571
12d5e280eea1
comment/format in: #generatorPolynom:
Stefan Vogel <sv@exept.de>
parents:
2570
diff
changeset
|
193 |
"set the generator polynom for this instance. |
12d5e280eea1
comment/format in: #generatorPolynom:
Stefan Vogel <sv@exept.de>
parents:
2570
diff
changeset
|
194 |
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
|
195 |
|
2570
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
196 |
generatorPolynom := anInteger. |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
197 |
crc := 16rFFFFFFFF. |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
198 |
crcTable := self class crcTableFor:generatorPolynom. |
2705 | 199 |
! |
200 |
||
201 |
reset |
|
202 |
"reset the current crc value" |
|
203 |
||
204 |
crc := 16rFFFFFFFF. |
|
205 |
||
206 |
"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
|
207 |
! ! |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
208 |
|
1192 | 209 |
!CRC32Stream methodsFor:'initialization'! |
210 |
||
211 |
initialize |
|
2570
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
212 |
"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
|
213 |
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 | 214 |
|
2570
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
215 |
self generatorPolynom:16rEDB88320 |
1192 | 216 |
! ! |
217 |
||
218 |
!CRC32Stream methodsFor:'queries'! |
|
219 |
||
220 |
hashValue |
|
221 |
"return the computed CRC" |
|
222 |
||
223 |
^ crc bitXor:16rFFFFFFFF |
|
224 |
! ! |
|
225 |
||
226 |
!CRC32Stream methodsFor:'writing'! |
|
227 |
||
2702
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
228 |
nextPutBytes:count from:anObject startingAt:start |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
229 |
"add the hash of anObject to the computed hash so far." |
1192 | 230 |
|
1977 | 231 |
| argSize "{ Class:SmallInteger }" |
232 |
byte "{ Class:SmallInteger }" | |
|
233 |
||
2306 | 234 |
%{ |
2702
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
235 |
if (__bothSmallInteger(count, start)) { |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
236 |
int len, offs; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
237 |
int objSize, nInstVars, nInstBytes; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
238 |
unsigned char *extPtr; |
2306 | 239 |
|
2702
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
240 |
len = __intVal(count); |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
241 |
offs = __intVal(start) - 1; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
242 |
|
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
243 |
if (__isExternalBytesLike(anObject)) { |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
244 |
OBJ sz; |
2306 | 245 |
|
2702
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
246 |
nInstBytes = 0; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
247 |
extPtr = (char *)__externalBytesAddress(anObject); |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
248 |
sz = __externalBytesSize(anObject); |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
249 |
if (__isSmallInteger(sz)) { |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
250 |
objSize = __intVal(sz); |
2306 | 251 |
} else { |
2702
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
252 |
objSize = 0; /* unknown */ |
2306 | 253 |
} |
2702
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
254 |
} else { |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
255 |
OBJ oClass; |
2307 | 256 |
|
2702
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
257 |
oClass = __Class(anObject); |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
258 |
switch (__intVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) { |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
259 |
case BYTEARRAY: |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
260 |
case WORDARRAY: |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
261 |
case LONGARRAY: |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
262 |
case SWORDARRAY: |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
263 |
case SLONGARRAY: |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
264 |
case FLOATARRAY: |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
265 |
case DOUBLEARRAY: |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
266 |
break; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
267 |
default: |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
268 |
goto bad; |
2307 | 269 |
} |
2702
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
270 |
nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars); |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
271 |
nInstBytes = __OBJS2BYTES__(nInstVars); |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
272 |
// 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
|
273 |
// 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
|
274 |
objSize = __Size(anObject) - OHDR_SIZE - nInstBytes; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
275 |
extPtr = (char *)__byteArrayVal(anObject)+nInstBytes; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
276 |
} |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
277 |
|
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
278 |
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
|
279 |
{ |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
280 |
unsigned int _crc; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
281 |
unsigned int *_crcTable; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
282 |
unsigned char *cp = extPtr+offs; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
283 |
unsigned n = len; |
2306 | 284 |
|
2702
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
285 |
if (__isSmallInteger(__INST(crc)) ) { |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
286 |
_crc = (unsigned) (__intVal( __INST(crc) )); |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
287 |
} else { |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
288 |
_crc = __unsignedLongIntVal( __INST(crc) ); |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
289 |
} |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
290 |
|
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
291 |
_crcTable = __integerArrayVal( __INST(crcTable) ); |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
292 |
|
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
293 |
while (n > 3) { |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
294 |
unsigned _idx; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
295 |
|
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
296 |
_idx = (_crc ^ cp[0]) & 0xFF; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
297 |
_crc = _crcTable[_idx] ^ (_crc >> 8); |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
298 |
_idx = (_crc ^ cp[1]) & 0xFF; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
299 |
_crc = _crcTable[_idx] ^ (_crc >> 8); |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
300 |
_idx = (_crc ^ cp[2]) & 0xFF; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
301 |
_crc = _crcTable[_idx] ^ (_crc >> 8); |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
302 |
_idx = (_crc ^ cp[3]) & 0xFF; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
303 |
_crc = _crcTable[_idx] ^ (_crc >> 8); |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
304 |
cp += 4; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
305 |
n -= 4; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
306 |
} |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
307 |
while (n > 0) { |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
308 |
unsigned _byte = *cp++; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
309 |
unsigned _idx; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
310 |
|
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
311 |
_idx = (_crc ^ _byte) & 0xFF; |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
312 |
_crc = _crcTable[_idx] ^ (_crc >> 8); |
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
313 |
n--; |
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 |
__INST(crc) = __MKUINT(_crc); |
2306 | 316 |
} |
2702
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
317 |
RETURN (count); |
2306 | 318 |
} |
319 |
} |
|
2702
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
320 |
bad: ; |
2306 | 321 |
%}. |
2702
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
322 |
self error:'invalid argument' |
1977 | 323 |
|
2702
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
324 |
"Created: / 09-01-2012 / 16:48:35 / cg" |
1192 | 325 |
! ! |
326 |
||
327 |
!CRC32Stream class methodsFor:'documentation'! |
|
328 |
||
329 |
version |
|
2705 | 330 |
^ '$Header: /cvs/stx/stx/libbasic2/CRC32Stream.st,v 1.21 2012-01-12 11:25:06 cg Exp $' |
2306 | 331 |
! |
332 |
||
333 |
version_CVS |
|
2705 | 334 |
^ '$Header: /cvs/stx/stx/libbasic2/CRC32Stream.st,v 1.21 2012-01-12 11:25:06 cg Exp $' |
1192 | 335 |
! ! |
336 |
||
337 |
CRC32Stream initialize! |