4846
|
1 |
"{ Encoding: utf8 }"
|
|
2 |
|
4844
|
3 |
"{ Package: 'stx:libbasic2' }"
|
|
4 |
|
|
5 |
"{ NameSpace: Smalltalk }"
|
|
6 |
|
|
7 |
HashStream subclass:#CRCStream
|
|
8 |
instanceVariableNames:'crc generatorPolynom crcTable initValue xorOut'
|
|
9 |
classVariableNames:''
|
|
10 |
poolDictionaries:''
|
|
11 |
category:'System-Crypt-Hashing'
|
|
12 |
!
|
|
13 |
|
|
14 |
CRCStream class instanceVariableNames:'crcTables'
|
|
15 |
|
|
16 |
"
|
|
17 |
No other class instance variables are inherited by this class.
|
|
18 |
"
|
|
19 |
!
|
|
20 |
|
|
21 |
!CRCStream class methodsFor:'documentation'!
|
|
22 |
|
|
23 |
documentation
|
|
24 |
"
|
|
25 |
abstract superclass of crc streams;
|
|
26 |
refactored from original crc32 stream.
|
4856
|
27 |
For details, see https://en.wikipedia.org/wiki/Cyclic_redundancy_check
|
4844
|
28 |
"
|
|
29 |
! !
|
|
30 |
|
|
31 |
!CRCStream class methodsFor:'initialization'!
|
|
32 |
|
|
33 |
crcTableFor:generatorPolynomInteger
|
|
34 |
|crcTable|
|
|
35 |
|
|
36 |
crcTables isNil ifTrue:[
|
|
37 |
crcTables := Dictionary new
|
|
38 |
].
|
|
39 |
crcTable := (crcTables at:generatorPolynomInteger ifAbsent:nil).
|
|
40 |
crcTable isNil ifTrue:[
|
|
41 |
crcTable := IntegerArray new:256.
|
|
42 |
|
|
43 |
0 to:255 do:[:count| |i|
|
|
44 |
i := count.
|
|
45 |
8 timesRepeat:[
|
|
46 |
(i bitTest:1) ifTrue:[
|
|
47 |
i := generatorPolynomInteger bitXor:(i bitShift:-1)
|
|
48 |
] ifFalse:[
|
|
49 |
i := i bitShift:-1
|
|
50 |
]
|
|
51 |
].
|
|
52 |
crcTable at:count+1 put:i.
|
|
53 |
].
|
|
54 |
crcTables at:generatorPolynomInteger put:crcTable.
|
|
55 |
].
|
|
56 |
^ crcTable.
|
|
57 |
|
|
58 |
"Created: / 16-03-2019 / 20:53:51 / Claus Gittinger"
|
|
59 |
! !
|
|
60 |
|
|
61 |
!CRCStream class methodsFor:'instance creation'!
|
|
62 |
|
|
63 |
generatorPolynom:anLSBInteger
|
|
64 |
"notice, in literature, the generator polynom is usually specified as an MSB number"
|
|
65 |
|
|
66 |
^ self basicNew generatorPolynom:anLSBInteger
|
|
67 |
|
|
68 |
"
|
|
69 |
self assert:((self generatorPolynom:16r82F63B78)
|
|
70 |
nextPut:'123456789';
|
|
71 |
hashValue) = 16rE3069283
|
|
72 |
"
|
|
73 |
|
|
74 |
"Modified (comment): / 16-03-2019 / 20:56:23 / Claus Gittinger"
|
|
75 |
!
|
|
76 |
|
|
77 |
generatorPolynom:anLSBInteger initValue:initValue
|
|
78 |
"notice, in literature, the generator polynom is usually specified as an MSB number"
|
|
79 |
|
|
80 |
^ self basicNew generatorPolynom:anLSBInteger initValue:initValue
|
|
81 |
|
|
82 |
"
|
|
83 |
self assert:((self generatorPolynom:16r82F63B78)
|
|
84 |
nextPut:'123456789';
|
|
85 |
hashValue) = 16rE3069283
|
|
86 |
"
|
|
87 |
|
|
88 |
"Created: / 16-03-2019 / 21:15:54 / Claus Gittinger"
|
|
89 |
!
|
|
90 |
|
|
91 |
generatorPolynom:anLSBInteger initValue:initValue xorOut:xorOut
|
|
92 |
"notice, in literature, the generator polynom is usually specified as an MSB number"
|
|
93 |
|
|
94 |
^ self basicNew generatorPolynom:anLSBInteger initValue:initValue xorOut:xorOut
|
|
95 |
|
|
96 |
"
|
|
97 |
self assert:((self generatorPolynom:16r82F63B78)
|
|
98 |
nextPut:'123456789';
|
|
99 |
hashValue) = 16rE3069283
|
|
100 |
"
|
|
101 |
|
|
102 |
"Created: / 16-03-2019 / 21:16:05 / Claus Gittinger"
|
|
103 |
! !
|
|
104 |
|
|
105 |
!CRCStream methodsFor:'accessing'!
|
|
106 |
|
|
107 |
generatorPolynom
|
|
108 |
"answer the generator polynom (LSB)"
|
|
109 |
|
|
110 |
^ generatorPolynom
|
|
111 |
|
|
112 |
"Modified (comment): / 16-03-2019 / 20:58:10 / Claus Gittinger"
|
|
113 |
!
|
|
114 |
|
|
115 |
initValue
|
|
116 |
"answer the init value"
|
|
117 |
|
|
118 |
^ initValue ? 0
|
|
119 |
|
|
120 |
"Created: / 16-03-2019 / 21:05:04 / Claus Gittinger"
|
|
121 |
!
|
|
122 |
|
|
123 |
xorOut
|
|
124 |
"answer the xorOut value"
|
|
125 |
|
|
126 |
^ xorOut ? 16rFFFFFFFF
|
|
127 |
|
|
128 |
"Created: / 16-03-2019 / 21:16:23 / Claus Gittinger"
|
|
129 |
! !
|
|
130 |
|
|
131 |
!CRCStream methodsFor:'initialization'!
|
|
132 |
|
|
133 |
generatorPolynom:polyLSB initValue:initValueArg xorOut:xorOutArg
|
|
134 |
"answer the generator polynom (LSB)"
|
|
135 |
|
|
136 |
generatorPolynom := polyLSB.
|
|
137 |
crc := initValue := initValueArg.
|
|
138 |
xorOut := xorOutArg.
|
|
139 |
|
|
140 |
crcTable := self class crcTableFor:generatorPolynom.
|
|
141 |
|
|
142 |
"Created: / 16-03-2019 / 21:17:12 / Claus Gittinger"
|
|
143 |
!
|
|
144 |
|
|
145 |
reset
|
|
146 |
"reset the current crc value"
|
|
147 |
|
|
148 |
crc := initValue.
|
|
149 |
|
|
150 |
"Created: / 16-03-2019 / 21:25:27 / Claus Gittinger"
|
|
151 |
! !
|
|
152 |
|
|
153 |
!CRCStream methodsFor:'queries'!
|
|
154 |
|
|
155 |
hashValue
|
|
156 |
"return the computed CRC"
|
|
157 |
|
|
158 |
^ crc bitXor:xorOut.
|
|
159 |
|
|
160 |
"Modified: / 16-03-2019 / 21:27:59 / Claus Gittinger"
|
|
161 |
! !
|
|
162 |
|
4846
|
163 |
!CRCStream methodsFor:'writing'!
|
|
164 |
|
|
165 |
nextPutBytes:count from:anObject startingAt:start
|
|
166 |
"add the hash of anObject to the computed hash so far."
|
|
167 |
|
|
168 |
%{
|
|
169 |
if (__bothSmallInteger(count, start)) {
|
|
170 |
int len, offs;
|
|
171 |
int objSize;
|
|
172 |
unsigned char *extPtr;
|
|
173 |
|
|
174 |
len = __intVal(count);
|
|
175 |
offs = __intVal(start) - 1;
|
|
176 |
|
|
177 |
if (__isExternalBytesLike(anObject)) {
|
|
178 |
OBJ sz = __externalBytesSize(anObject);
|
|
179 |
|
|
180 |
extPtr = (unsigned char *)__externalBytesAddress(anObject);
|
|
181 |
if (__isSmallInteger(sz)) {
|
|
182 |
objSize = __intVal(sz);
|
|
183 |
} else {
|
|
184 |
objSize = 0; /* unknown */
|
|
185 |
}
|
|
186 |
} else {
|
|
187 |
int nInstVars, nInstBytes;
|
|
188 |
OBJ oClass = __Class(anObject);
|
|
189 |
|
|
190 |
switch (__intVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) {
|
|
191 |
case BYTEARRAY:
|
|
192 |
case WORDARRAY:
|
|
193 |
case LONGARRAY:
|
|
194 |
case SWORDARRAY:
|
|
195 |
case SLONGARRAY:
|
|
196 |
case FLOATARRAY:
|
|
197 |
case DOUBLEARRAY:
|
|
198 |
break;
|
|
199 |
default:
|
|
200 |
goto bad;
|
|
201 |
}
|
|
202 |
nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars);
|
|
203 |
nInstBytes = __OBJS2BYTES__(nInstVars);
|
|
204 |
// nInstBytes is the number of bytes occupied by pointer instance variables
|
|
205 |
// subtract from size and add to byte-pointer
|
|
206 |
objSize = __qSize(anObject) - OHDR_SIZE - nInstBytes;
|
|
207 |
extPtr = (unsigned char *)__byteArrayVal(anObject)+nInstBytes;
|
|
208 |
}
|
|
209 |
|
|
210 |
if ((offs >= 0) && (len >= 0) && (objSize >= (len + offs))) {
|
|
211 |
unsigned int _crc;
|
|
212 |
unsigned int *_crcTable = __integerArrayVal( __INST(crcTable) );
|
|
213 |
unsigned char *cp = extPtr+offs;
|
|
214 |
unsigned int n = len;
|
|
215 |
|
4856
|
216 |
if (__isSmallInteger(__INST(crc))) {
|
4846
|
217 |
_crc = (unsigned int) (__intVal( __INST(crc) ));
|
|
218 |
} else {
|
|
219 |
_crc = __unsignedLongIntVal( __INST(crc) );
|
|
220 |
}
|
|
221 |
|
|
222 |
#ifdef __LSBFIRST__
|
|
223 |
# if __POINTER_SIZE__ == 8
|
|
224 |
if (((unsigned INT)cp & 7) == 0) {
|
|
225 |
// longword aligned
|
|
226 |
for ( ; n >= 8 ; n -= 8, cp += 8) {
|
|
227 |
unsigned INT lWord;
|
|
228 |
unsigned char _idx;
|
|
229 |
|
|
230 |
lWord = ((unsigned INT *)cp)[0];
|
|
231 |
_idx = (_crc ^ lWord) & 0xFF;
|
|
232 |
_crc = _crcTable[_idx] ^ (_crc >> 8);
|
|
233 |
_idx = (_crc ^ (lWord>>8)) & 0xFF;
|
|
234 |
_crc = _crcTable[_idx] ^ (_crc >> 8);
|
|
235 |
_idx = (_crc ^ (lWord>>16)) & 0xFF;
|
|
236 |
_crc = _crcTable[_idx] ^ (_crc >> 8);
|
|
237 |
_idx = (_crc ^ (lWord>>24)) & 0xFF;
|
|
238 |
_crc = _crcTable[_idx] ^ (_crc >> 8);
|
|
239 |
|
|
240 |
_idx = (_crc ^ (lWord>>32)) & 0xFF;
|
|
241 |
_crc = _crcTable[_idx] ^ (_crc >> 8);
|
|
242 |
_idx = (_crc ^ (lWord>>40)) & 0xFF;
|
|
243 |
_crc = _crcTable[_idx] ^ (_crc >> 8);
|
|
244 |
_idx = (_crc ^ (lWord>>48)) & 0xFF;
|
|
245 |
_crc = _crcTable[_idx] ^ (_crc >> 8);
|
|
246 |
_idx = (_crc ^ (lWord>>56)) & 0xFF;
|
|
247 |
_crc = _crcTable[_idx] ^ (_crc >> 8);
|
|
248 |
}
|
|
249 |
}
|
|
250 |
# endif
|
|
251 |
if (((unsigned INT)cp & 3) == 0) {
|
|
252 |
// word aligned
|
|
253 |
for ( ; n >= 4 ; n -= 4, cp += 4) {
|
|
254 |
unsigned int word;
|
|
255 |
unsigned char _idx;
|
|
256 |
|
|
257 |
word = ((unsigned int *)cp)[0];
|
|
258 |
_idx = (_crc ^ word) & 0xFF;
|
|
259 |
_crc = _crcTable[_idx] ^ (_crc >> 8);
|
|
260 |
_idx = (_crc ^ (word>>8)) & 0xFF;
|
|
261 |
_crc = _crcTable[_idx] ^ (_crc >> 8);
|
|
262 |
_idx = (_crc ^ (word>>16)) & 0xFF;
|
|
263 |
_crc = _crcTable[_idx] ^ (_crc >> 8);
|
|
264 |
_idx = (_crc ^ (word>>24)) & 0xFF;
|
|
265 |
_crc = _crcTable[_idx] ^ (_crc >> 8);
|
|
266 |
}
|
|
267 |
}
|
|
268 |
#endif
|
|
269 |
for ( ; n >= 4 ; n -= 4, cp += 4) {
|
|
270 |
unsigned char _idx;
|
|
271 |
|
|
272 |
_idx = (_crc ^ cp[0]) & 0xFF;
|
|
273 |
_crc = _crcTable[_idx] ^ (_crc >> 8);
|
|
274 |
_idx = (_crc ^ cp[1]) & 0xFF;
|
|
275 |
_crc = _crcTable[_idx] ^ (_crc >> 8);
|
|
276 |
_idx = (_crc ^ cp[2]) & 0xFF;
|
|
277 |
_crc = _crcTable[_idx] ^ (_crc >> 8);
|
|
278 |
_idx = (_crc ^ cp[3]) & 0xFF;
|
|
279 |
_crc = _crcTable[_idx] ^ (_crc >> 8);
|
|
280 |
}
|
|
281 |
while (n-- > 0) {
|
|
282 |
unsigned char _idx = (_crc ^ *cp++) & 0xFF;
|
|
283 |
_crc = _crcTable[_idx] ^ (_crc >> 8);
|
|
284 |
}
|
|
285 |
|
|
286 |
if (sizeof(INT) == 8 || _crc <= _MAX_INT)
|
|
287 |
__INST(crc) = __MKSMALLINT(_crc);
|
|
288 |
else {
|
|
289 |
// this code fails with gcc 4.7.2:
|
|
290 |
// __INST(crc) = __MKUINT(_crc); __STORESELF(crc);
|
|
291 |
OBJ temp = __MKUINT(_crc);
|
|
292 |
__INST(crc) = temp; __STORESELF(crc);
|
|
293 |
}
|
|
294 |
|
|
295 |
// if (__unsignedLongIntVal(__INST(crc)) == 4294967295) printf("inst: %d crc: %d new: %d\n", __unsignedLongIntVal(__INST(crc)), _crc, __unsignedLongIntVal(temp));
|
|
296 |
RETURN (count);
|
|
297 |
}
|
|
298 |
}
|
|
299 |
bad: ;
|
|
300 |
%}.
|
|
301 |
self error:'invalid argument'
|
|
302 |
|
|
303 |
"Created: / 09-01-2012 / 16:48:35 / cg"
|
4856
|
304 |
"Modified: / 16-03-2019 / 23:19:17 / Claus Gittinger"
|
4846
|
305 |
! !
|
|
306 |
|
4844
|
307 |
!CRCStream class methodsFor:'documentation'!
|
|
308 |
|
|
309 |
version_CVS
|
|
310 |
^ '$Header$'
|
|
311 |
! !
|
|
312 |
|