author | Claus Gittinger <cg@exept.de> |
Thu, 09 Jun 2016 18:09:58 +0200 | |
changeset 3934 | 3fc6968232c1 |
parent 3024 | c7022e1963b0 |
child 4238 | ab8390ef187a |
permissions | -rw-r--r-- |
2416 | 1 |
" |
2 |
COPYRIGHT (c) 2003 by eXept Software AG |
|
2963 | 3 |
All Rights Reserved |
2416 | 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 |
|
2963 | 26 |
All Rights Reserved |
2416 | 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 |
" |
|
2963 | 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 |
|
2963 | 42 |
x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x+1 |
43 |
(or 16r04C11DB7) |
|
1192 | 44 |
|
2963 | 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 |
|
2963 | 48 |
self newCrc32c |
2570
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
49 |
|
2963 | 50 |
x32 + x28 + x27 + x26 + x25 + x23 + x22 + x20 + x19 + x18 + x14 + x13 + x11 + x10 + x9 + x8 + x6 + 1 |
2570
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 |
||
2963 | 55 |
Notice that this CRC is also used with PNG images - therefore, its performance |
2310 | 56 |
directly affects png image processing. |
1192 | 57 |
|
2306 | 58 |
throughput: |
2963 | 59 |
157000 Kb/s on a 2.5Ghz 64X2 Athlon 4800+ (64bit) |
60 |
150000 Kb/s on 2Ghz Duo |
|
1192 | 61 |
|
62 |
[author:] |
|
2963 | 63 |
Stefan Vogel (stefan@zwerg) |
1192 | 64 |
|
65 |
[instance variables:] |
|
66 |
||
67 |
[class variables:] |
|
68 |
||
69 |
[see also:] |
|
2963 | 70 |
SHA1Stream |
71 |
MD5Stream |
|
1192 | 72 |
|
73 |
" |
|
74 |
! |
|
75 |
||
76 |
examples |
|
77 |
" |
|
78 |
||
2306 | 79 |
expect 60C1D0A0 |
2963 | 80 |
[exBegin] |
1977 | 81 |
self information:(CRC32Stream hashValueOf:'resume') hexPrintString |
2963 | 82 |
[exEnd] |
1192 | 83 |
|
2306 | 84 |
expect 16r60C1D0A0 |
2963 | 85 |
[exBegin] |
1978
f7330488b12c
changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents:
1977
diff
changeset
|
86 |
self information:(CRC32Stream new |
2963 | 87 |
nextPut:$r; |
88 |
nextPut:$e; |
|
89 |
nextPut:$s; |
|
90 |
nextPut:$u; |
|
91 |
nextPut:$m; |
|
92 |
nextPut:$e; |
|
93 |
hashValue) hexPrintString |
|
94 |
[exEnd] |
|
1978
f7330488b12c
changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents:
1977
diff
changeset
|
95 |
|
2306 | 96 |
expect 16r70E46888: |
2963 | 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 |
2963 | 99 |
[exEnd] |
1192 | 100 |
|
2306 | 101 |
expect 16r8CD04C73: |
2963 | 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 |
|
106 |
16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF]) hexPrintString) |
|
107 |
[exEnd] |
|
2306 | 108 |
|
109 |
expect 16r86D7D79A: |
|
110 |
timing throughput: |
|
2963 | 111 |
[exBegin] |
2306 | 112 |
|hashStream n t| |
113 |
||
114 |
hashStream := CRC32Stream new. |
|
2962 | 115 |
n := 1000000. |
2306 | 116 |
t := Time millisecondsToRun:[ |
2963 | 117 |
n timesRepeat:[ |
118 |
hashStream nextPutAll:'12345678901234567890123456789012345678901234567890'. |
|
119 |
]. |
|
120 |
]. |
|
2306 | 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' |
|
2963 | 125 |
[exEnd] |
2306 | 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 |
|
2963 | 135 |
crcTable := (CrcTables at:generatorPolynomInteger ifAbsent:nil). |
2570
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
136 |
crcTable isNil ifTrue:[ |
2963 | 137 |
crcTable := IntegerArray new:256. |
1192 | 138 |
|
2963 | 139 |
0 to:255 do:[:count| |i| |
140 |
i := count. |
|
141 |
8 timesRepeat:[ |
|
142 |
(i bitTest:1) ifTrue:[ |
|
143 |
i := generatorPolynomInteger bitXor:(i bitShift:-1) |
|
144 |
] ifFalse:[ |
|
145 |
i := i bitShift:-1 |
|
146 |
] |
|
147 |
]. |
|
148 |
crcTable at:count+1 put:i. |
|
149 |
]. |
|
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) |
2963 | 166 |
nextPut:'123456789'; |
167 |
hashValue) = 16rE3069283 |
|
2570
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 |
2963 | 173 |
x32 + x28 + x27 + x26 + x25 + x23 + x22 + x20 + x19 + x18 + x14 + x13 + x11 + x10 + x9 + x8 + x6 + 1 |
2570
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) |
2963 | 181 |
nextPut:'123456789'; |
182 |
hashValue) = 3808858755. '16rE3069283' |
|
2747
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) |
2963 | 186 |
nextPut:'123456789'; |
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: |
2963 | 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 |
||
3024 | 232 |
^ crc bitXor:16rFFFFFFFF. |
1192 | 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 |
|
2306 | 240 |
%{ |
2702
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
241 |
if (__bothSmallInteger(count, start)) { |
3024 | 242 |
int len, offs; |
243 |
int objSize; |
|
244 |
unsigned char *extPtr; |
|
2306 | 245 |
|
3024 | 246 |
len = __intVal(count); |
247 |
offs = __intVal(start) - 1; |
|
2702
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
248 |
|
3024 | 249 |
if (__isExternalBytesLike(anObject)) { |
250 |
OBJ sz = __externalBytesSize(anObject); |
|
2306 | 251 |
|
3024 | 252 |
extPtr = (unsigned char *)__externalBytesAddress(anObject); |
253 |
if (__isSmallInteger(sz)) { |
|
254 |
objSize = __intVal(sz); |
|
255 |
} else { |
|
256 |
objSize = 0; /* unknown */ |
|
257 |
} |
|
258 |
} else { |
|
259 |
int nInstVars, nInstBytes; |
|
260 |
OBJ oClass = __Class(anObject); |
|
2307 | 261 |
|
3024 | 262 |
switch (__intVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) { |
263 |
case BYTEARRAY: |
|
264 |
case WORDARRAY: |
|
265 |
case LONGARRAY: |
|
266 |
case SWORDARRAY: |
|
267 |
case SLONGARRAY: |
|
268 |
case FLOATARRAY: |
|
269 |
case DOUBLEARRAY: |
|
270 |
break; |
|
271 |
default: |
|
272 |
goto bad; |
|
273 |
} |
|
274 |
nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars); |
|
275 |
nInstBytes = __OBJS2BYTES__(nInstVars); |
|
276 |
// nInstBytes is the number of bytes occupied by pointer instance variables |
|
277 |
// subtract from size and add to byte-pointer |
|
278 |
objSize = __qSize(anObject) - OHDR_SIZE - nInstBytes; |
|
279 |
extPtr = (unsigned char *)__byteArrayVal(anObject)+nInstBytes; |
|
280 |
} |
|
2702
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
281 |
|
3024 | 282 |
if ((offs >= 0) && (len >= 0) && (objSize >= (len + offs))) { |
283 |
unsigned int _crc; |
|
284 |
unsigned int *_crcTable = __integerArrayVal( __INST(crcTable) ); |
|
285 |
unsigned char *cp = extPtr+offs; |
|
286 |
unsigned int n = len; |
|
2306 | 287 |
|
3024 | 288 |
if (sizeof(INT) == 8 || __isSmallInteger(__INST(crc)) ) { |
289 |
_crc = (unsigned int) (__intVal( __INST(crc) )); |
|
290 |
} else { |
|
291 |
_crc = __unsignedLongIntVal( __INST(crc) ); |
|
292 |
} |
|
2702
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
293 |
|
2746 | 294 |
#ifdef __LSBFIRST__ |
3024 | 295 |
if (((unsigned INT)cp & 3) == 0) { |
296 |
// word aligned |
|
297 |
for ( ; n >= 4 ; n -= 4, cp += 4) { |
|
298 |
unsigned int word; |
|
299 |
unsigned char _idx; |
|
2746 | 300 |
|
3024 | 301 |
word = ((unsigned int *)cp)[0]; |
302 |
_idx = (_crc ^ word) & 0xFF; |
|
303 |
_crc = _crcTable[_idx] ^ (_crc >> 8); |
|
304 |
_idx = (_crc ^ (word>>8)) & 0xFF; |
|
305 |
_crc = _crcTable[_idx] ^ (_crc >> 8); |
|
306 |
_idx = (_crc ^ (word>>16)) & 0xFF; |
|
307 |
_crc = _crcTable[_idx] ^ (_crc >> 8); |
|
308 |
_idx = (_crc ^ (word>>24)) & 0xFF; |
|
309 |
_crc = _crcTable[_idx] ^ (_crc >> 8); |
|
2746 | 310 |
} |
3024 | 311 |
} |
2746 | 312 |
#endif |
3024 | 313 |
for ( ; n >= 4 ; n -= 4, cp += 4) { |
314 |
unsigned char _idx; |
|
2702
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
315 |
|
3024 | 316 |
_idx = (_crc ^ cp[0]) & 0xFF; |
317 |
_crc = _crcTable[_idx] ^ (_crc >> 8); |
|
318 |
_idx = (_crc ^ cp[1]) & 0xFF; |
|
319 |
_crc = _crcTable[_idx] ^ (_crc >> 8); |
|
320 |
_idx = (_crc ^ cp[2]) & 0xFF; |
|
321 |
_crc = _crcTable[_idx] ^ (_crc >> 8); |
|
322 |
_idx = (_crc ^ cp[3]) & 0xFF; |
|
323 |
_crc = _crcTable[_idx] ^ (_crc >> 8); |
|
324 |
} |
|
325 |
while (n-- > 0) { |
|
326 |
unsigned char _idx = (_crc ^ *cp++) & 0xFF; |
|
327 |
_crc = _crcTable[_idx] ^ (_crc >> 8); |
|
328 |
} |
|
2702
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
329 |
|
3024 | 330 |
if (sizeof(INT) == 8 || _crc <= _MAX_INT) |
331 |
__INST(crc) = __MKSMALLINT(_crc); |
|
332 |
else { |
|
333 |
// this code fails with gcc 4.7.2: |
|
334 |
// __INST(crc) = __MKUINT(_crc); __STORESELF(crc); |
|
335 |
OBJ temp = __MKUINT(_crc); |
|
336 |
__INST(crc) = temp; __STORESELF(crc); |
|
337 |
} |
|
338 |
||
339 |
// if (__unsignedLongIntVal(__INST(crc)) == 4294967295) printf("inst: %d crc: %d new: %d\n", __unsignedLongIntVal(__INST(crc)), _crc, __unsignedLongIntVal(temp)); |
|
340 |
RETURN (count); |
|
341 |
} |
|
2306 | 342 |
} |
2702
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
343 |
bad: ; |
2306 | 344 |
%}. |
2702
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
345 |
self error:'invalid argument' |
1977 | 346 |
|
2702
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
347 |
"Created: / 09-01-2012 / 16:48:35 / cg" |
1192 | 348 |
! ! |
349 |
||
350 |
!CRC32Stream class methodsFor:'documentation'! |
|
351 |
||
352 |
version |
|
3024 | 353 |
^ '$Header: /cvs/stx/stx/libbasic2/CRC32Stream.st,v 1.27 2013-07-02 11:55:39 stefan Exp $' |
2306 | 354 |
! |
355 |
||
356 |
version_CVS |
|
3024 | 357 |
^ '$Header: /cvs/stx/stx/libbasic2/CRC32Stream.st,v 1.27 2013-07-02 11:55:39 stefan Exp $' |
1192 | 358 |
! ! |
359 |
||
2905 | 360 |
|
1192 | 361 |
CRC32Stream initialize! |