5381
|
1 |
"
|
|
2 |
COPYRIGHT (c) 2018 by eXept Software AG
|
|
3 |
All Rights Reserved
|
4907
|
4 |
|
5381
|
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 |
"
|
4844
|
12 |
"{ Package: 'stx:libbasic2' }"
|
|
13 |
|
|
14 |
"{ NameSpace: Smalltalk }"
|
|
15 |
|
|
16 |
HashStream subclass:#CRCStream
|
|
17 |
instanceVariableNames:'crc generatorPolynom crcTable initValue xorOut'
|
|
18 |
classVariableNames:''
|
|
19 |
poolDictionaries:''
|
|
20 |
category:'System-Crypt-Hashing'
|
|
21 |
!
|
|
22 |
|
|
23 |
CRCStream class instanceVariableNames:'crcTables'
|
|
24 |
|
|
25 |
"
|
|
26 |
No other class instance variables are inherited by this class.
|
|
27 |
"
|
|
28 |
!
|
|
29 |
|
|
30 |
!CRCStream class methodsFor:'documentation'!
|
|
31 |
|
5381
|
32 |
copyright
|
|
33 |
"
|
|
34 |
COPYRIGHT (c) 2018 by eXept Software AG
|
|
35 |
All Rights Reserved
|
|
36 |
|
|
37 |
This software is furnished under a license and may be used
|
|
38 |
only in accordance with the terms of that license and with the
|
|
39 |
inclusion of the above copyright notice. This software may not
|
|
40 |
be provided or otherwise made available to, or used by, any
|
|
41 |
other person. No title to or ownership of the software is
|
|
42 |
hereby transferred.
|
|
43 |
|
|
44 |
"
|
|
45 |
!
|
|
46 |
|
4844
|
47 |
documentation
|
|
48 |
"
|
|
49 |
abstract superclass of crc streams;
|
|
50 |
refactored from original crc32 stream.
|
4856
|
51 |
For details, see https://en.wikipedia.org/wiki/Cyclic_redundancy_check
|
4868
|
52 |
|
4872
|
53 |
Only use CRC to protect against communication errors;
|
|
54 |
DO NOT use CRC for cryptography, authentication, security, etc.
|
|
55 |
- use secure hashes for those instead.
|
|
56 |
|
4868
|
57 |
[parameters:]
|
|
58 |
polynomials:
|
|
59 |
CRC_POLY_16 0xA001
|
|
60 |
CRC_POLY_32 0xEDB88320L
|
|
61 |
CRC_POLY_CCITT 0x1021
|
|
62 |
CRC_POLY_DNP 0xA6BC
|
|
63 |
CRC_POLY_KERMIT 0x8408
|
|
64 |
|
|
65 |
start values:
|
|
66 |
CRC_START_8 0x00
|
|
67 |
CRC_START_16 0x0000
|
|
68 |
CRC_START_MODBUS 0xFFFF
|
|
69 |
CRC_START_XMODEM 0x0000
|
|
70 |
CRC_START_CCITT_1D0F 0x1D0F
|
|
71 |
CRC_START_CCITT_FFFF 0xFFFF
|
|
72 |
CRC_START_KERMIT 0x0000
|
|
73 |
CRC_START_SICK 0x0000
|
|
74 |
CRC_START_DNP 0x0000
|
|
75 |
CRC_START_32 0xFFFFFFFF
|
|
76 |
|
4844
|
77 |
"
|
|
78 |
! !
|
|
79 |
|
|
80 |
!CRCStream class methodsFor:'initialization'!
|
|
81 |
|
|
82 |
crcTableFor:generatorPolynomInteger
|
4907
|
83 |
"construct the polynom-specific lookup table"
|
|
84 |
|
4844
|
85 |
|crcTable|
|
|
86 |
|
4907
|
87 |
"/ CRC8Stream flushCrcTables
|
|
88 |
"/ CRC8Stream crcTableFor:(16r1D bitReversed8)
|
|
89 |
|
4844
|
90 |
crcTables isNil ifTrue:[
|
|
91 |
crcTables := Dictionary new
|
|
92 |
].
|
|
93 |
crcTable := (crcTables at:generatorPolynomInteger ifAbsent:nil).
|
|
94 |
crcTable isNil ifTrue:[
|
4907
|
95 |
"/ for crc16 and crc8, we waste some memory
|
|
96 |
"/ (could use WordArray or ByteArray);
|
|
97 |
"/ but that would need three writeBytes implementations.
|
|
98 |
"/ Not worth the saving.
|
4844
|
99 |
crcTable := IntegerArray new:256.
|
|
100 |
|
4907
|
101 |
"/ least-sign. bit towards most significant bit...
|
4844
|
102 |
0 to:255 do:[:count| |i|
|
|
103 |
i := count.
|
|
104 |
8 timesRepeat:[
|
|
105 |
(i bitTest:1) ifTrue:[
|
|
106 |
i := generatorPolynomInteger bitXor:(i bitShift:-1)
|
|
107 |
] ifFalse:[
|
|
108 |
i := i bitShift:-1
|
|
109 |
]
|
|
110 |
].
|
|
111 |
crcTable at:count+1 put:i.
|
|
112 |
].
|
|
113 |
crcTables at:generatorPolynomInteger put:crcTable.
|
|
114 |
].
|
|
115 |
^ crcTable.
|
|
116 |
|
|
117 |
"Created: / 16-03-2019 / 20:53:51 / Claus Gittinger"
|
4907
|
118 |
"Modified: / 24-03-2019 / 12:38:12 / Claus Gittinger"
|
4859
|
119 |
!
|
|
120 |
|
|
121 |
flushCrcTables
|
|
122 |
crcTables := nil.
|
|
123 |
|
|
124 |
"
|
|
125 |
CRC16Stream flushCrcTables
|
|
126 |
CRC32Stream flushCrcTables
|
|
127 |
"
|
|
128 |
|
|
129 |
"Created: / 16-03-2019 / 23:41:19 / Claus Gittinger"
|
4844
|
130 |
! !
|
|
131 |
|
|
132 |
!CRCStream class methodsFor:'instance creation'!
|
|
133 |
|
|
134 |
generatorPolynom:anLSBInteger
|
|
135 |
"notice, in literature, the generator polynom is usually specified as an MSB number"
|
|
136 |
|
4863
|
137 |
^ self basicNew
|
|
138 |
generatorPolynom:anLSBInteger
|
4844
|
139 |
|
|
140 |
"
|
|
141 |
self assert:((self generatorPolynom:16r82F63B78)
|
|
142 |
nextPut:'123456789';
|
|
143 |
hashValue) = 16rE3069283
|
|
144 |
"
|
|
145 |
|
4863
|
146 |
"Modified (format): / 17-03-2019 / 14:03:42 / Claus Gittinger"
|
4844
|
147 |
!
|
|
148 |
|
|
149 |
generatorPolynom:anLSBInteger initValue:initValue
|
|
150 |
"notice, in literature, the generator polynom is usually specified as an MSB number"
|
|
151 |
|
4863
|
152 |
^ self basicNew
|
|
153 |
generatorPolynom:anLSBInteger
|
|
154 |
initValue:initValue
|
4844
|
155 |
|
|
156 |
"
|
|
157 |
self assert:((self generatorPolynom:16r82F63B78)
|
|
158 |
nextPut:'123456789';
|
|
159 |
hashValue) = 16rE3069283
|
|
160 |
"
|
|
161 |
|
|
162 |
"Created: / 16-03-2019 / 21:15:54 / Claus Gittinger"
|
4863
|
163 |
"Modified (format): / 17-03-2019 / 14:03:37 / Claus Gittinger"
|
4844
|
164 |
!
|
|
165 |
|
|
166 |
generatorPolynom:anLSBInteger initValue:initValue xorOut:xorOut
|
|
167 |
"notice, in literature, the generator polynom is usually specified as an MSB number"
|
|
168 |
|
4863
|
169 |
^ self basicNew
|
|
170 |
generatorPolynom:anLSBInteger
|
|
171 |
initValue:initValue xorOut:xorOut
|
4844
|
172 |
|
|
173 |
"
|
|
174 |
self assert:((self generatorPolynom:16r82F63B78)
|
|
175 |
nextPut:'123456789';
|
|
176 |
hashValue) = 16rE3069283
|
|
177 |
"
|
|
178 |
|
|
179 |
"Created: / 16-03-2019 / 21:16:05 / Claus Gittinger"
|
4863
|
180 |
"Modified (format): / 17-03-2019 / 14:03:30 / Claus Gittinger"
|
4844
|
181 |
! !
|
|
182 |
|
|
183 |
!CRCStream methodsFor:'accessing'!
|
|
184 |
|
|
185 |
generatorPolynom
|
|
186 |
"answer the generator polynom (LSB)"
|
|
187 |
|
|
188 |
^ generatorPolynom
|
|
189 |
|
|
190 |
"Modified (comment): / 16-03-2019 / 20:58:10 / Claus Gittinger"
|
|
191 |
!
|
|
192 |
|
|
193 |
initValue
|
|
194 |
"answer the init value"
|
|
195 |
|
|
196 |
^ initValue ? 0
|
|
197 |
|
|
198 |
"Created: / 16-03-2019 / 21:05:04 / Claus Gittinger"
|
|
199 |
!
|
|
200 |
|
|
201 |
xorOut
|
|
202 |
"answer the xorOut value"
|
|
203 |
|
|
204 |
^ xorOut ? 16rFFFFFFFF
|
|
205 |
|
|
206 |
"Created: / 16-03-2019 / 21:16:23 / Claus Gittinger"
|
|
207 |
! !
|
|
208 |
|
|
209 |
!CRCStream methodsFor:'initialization'!
|
|
210 |
|
|
211 |
generatorPolynom:polyLSB initValue:initValueArg xorOut:xorOutArg
|
4863
|
212 |
"set the generator polynom for this instance.
|
|
213 |
And set start and xorOut.
|
|
214 |
Computes the crcTable for this polynom.
|
|
215 |
Notice the bit order is LSB"
|
4844
|
216 |
|
|
217 |
generatorPolynom := polyLSB.
|
|
218 |
crc := initValue := initValueArg.
|
|
219 |
xorOut := xorOutArg.
|
|
220 |
|
|
221 |
crcTable := self class crcTableFor:generatorPolynom.
|
|
222 |
|
|
223 |
"Created: / 16-03-2019 / 21:17:12 / Claus Gittinger"
|
4863
|
224 |
"Modified (comment): / 17-03-2019 / 12:25:11 / Claus Gittinger"
|
4844
|
225 |
!
|
|
226 |
|
|
227 |
reset
|
|
228 |
"reset the current crc value"
|
|
229 |
|
|
230 |
crc := initValue.
|
|
231 |
|
|
232 |
"Created: / 16-03-2019 / 21:25:27 / Claus Gittinger"
|
|
233 |
! !
|
|
234 |
|
|
235 |
!CRCStream methodsFor:'queries'!
|
|
236 |
|
|
237 |
hashValue
|
|
238 |
"return the computed CRC"
|
|
239 |
|
|
240 |
^ crc bitXor:xorOut.
|
|
241 |
|
|
242 |
"Modified: / 16-03-2019 / 21:27:59 / Claus Gittinger"
|
|
243 |
! !
|
|
244 |
|
4846
|
245 |
!CRCStream methodsFor:'writing'!
|
|
246 |
|
|
247 |
nextPutBytes:count from:anObject startingAt:start
|
|
248 |
"add the hash of anObject to the computed hash so far."
|
|
249 |
|
4916
|
250 |
%{
|
4896
|
251 |
int len, offs;
|
|
252 |
|
|
253 |
// fetch first - check later
|
|
254 |
len = __intVal(count);
|
|
255 |
offs = __intVal(start) - 1;
|
|
256 |
|
4846
|
257 |
if (__bothSmallInteger(count, start)) {
|
|
258 |
int objSize;
|
|
259 |
unsigned char *extPtr;
|
|
260 |
|
4896
|
261 |
// the most common first
|
|
262 |
if (__isStringLike(anObject)) {
|
|
263 |
objSize = __stringSize(anObject);
|
|
264 |
extPtr = (unsigned char *)__stringVal(anObject);
|
|
265 |
} else if (__isByteArray(anObject)) {
|
|
266 |
objSize = __byteArraySize(anObject);
|
|
267 |
extPtr = (unsigned char *)__byteArrayVal(anObject);
|
|
268 |
} else if (__isExternalBytesLike(anObject)) {
|
4846
|
269 |
OBJ sz = __externalBytesSize(anObject);
|
|
270 |
|
|
271 |
extPtr = (unsigned char *)__externalBytesAddress(anObject);
|
|
272 |
if (__isSmallInteger(sz)) {
|
|
273 |
objSize = __intVal(sz);
|
|
274 |
} else {
|
|
275 |
objSize = 0; /* unknown */
|
|
276 |
}
|
|
277 |
} else {
|
|
278 |
int nInstVars, nInstBytes;
|
|
279 |
OBJ oClass = __Class(anObject);
|
|
280 |
|
|
281 |
switch (__intVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) {
|
|
282 |
case BYTEARRAY:
|
|
283 |
case WORDARRAY:
|
|
284 |
case LONGARRAY:
|
|
285 |
case SWORDARRAY:
|
|
286 |
case SLONGARRAY:
|
|
287 |
case FLOATARRAY:
|
|
288 |
case DOUBLEARRAY:
|
|
289 |
break;
|
|
290 |
default:
|
|
291 |
goto bad;
|
|
292 |
}
|
|
293 |
nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars);
|
|
294 |
nInstBytes = __OBJS2BYTES__(nInstVars);
|
|
295 |
// nInstBytes is the number of bytes occupied by pointer instance variables
|
|
296 |
// subtract from size and add to byte-pointer
|
|
297 |
objSize = __qSize(anObject) - OHDR_SIZE - nInstBytes;
|
|
298 |
extPtr = (unsigned char *)__byteArrayVal(anObject)+nInstBytes;
|
|
299 |
}
|
|
300 |
|
|
301 |
if ((offs >= 0) && (len >= 0) && (objSize >= (len + offs))) {
|
|
302 |
unsigned int _crc;
|
|
303 |
unsigned int *_crcTable = __integerArrayVal( __INST(crcTable) );
|
|
304 |
unsigned char *cp = extPtr+offs;
|
|
305 |
unsigned int n = len;
|
|
306 |
|
4896
|
307 |
_crc = (unsigned int) (__intVal( __INST(crc) ));
|
4907
|
308 |
#if __POINTER_SIZE__ != 8
|
4896
|
309 |
if (!__isSmallInteger(__INST(crc))) {
|
4846
|
310 |
_crc = __unsignedLongIntVal( __INST(crc) );
|
|
311 |
}
|
4907
|
312 |
#endif
|
4846
|
313 |
|
|
314 |
#ifdef __LSBFIRST__
|
|
315 |
# if __POINTER_SIZE__ == 8
|
|
316 |
if (((unsigned INT)cp & 7) == 0) {
|
|
317 |
// longword aligned
|
|
318 |
for ( ; n >= 8 ; n -= 8, cp += 8) {
|
|
319 |
unsigned INT lWord;
|
|
320 |
unsigned char _idx;
|
|
321 |
|
|
322 |
lWord = ((unsigned INT *)cp)[0];
|
|
323 |
_idx = (_crc ^ lWord) & 0xFF;
|
|
324 |
_crc = _crcTable[_idx] ^ (_crc >> 8);
|
|
325 |
_idx = (_crc ^ (lWord>>8)) & 0xFF;
|
|
326 |
_crc = _crcTable[_idx] ^ (_crc >> 8);
|
|
327 |
_idx = (_crc ^ (lWord>>16)) & 0xFF;
|
|
328 |
_crc = _crcTable[_idx] ^ (_crc >> 8);
|
|
329 |
_idx = (_crc ^ (lWord>>24)) & 0xFF;
|
|
330 |
_crc = _crcTable[_idx] ^ (_crc >> 8);
|
|
331 |
|
|
332 |
_idx = (_crc ^ (lWord>>32)) & 0xFF;
|
|
333 |
_crc = _crcTable[_idx] ^ (_crc >> 8);
|
|
334 |
_idx = (_crc ^ (lWord>>40)) & 0xFF;
|
|
335 |
_crc = _crcTable[_idx] ^ (_crc >> 8);
|
|
336 |
_idx = (_crc ^ (lWord>>48)) & 0xFF;
|
|
337 |
_crc = _crcTable[_idx] ^ (_crc >> 8);
|
|
338 |
_idx = (_crc ^ (lWord>>56)) & 0xFF;
|
|
339 |
_crc = _crcTable[_idx] ^ (_crc >> 8);
|
|
340 |
}
|
|
341 |
}
|
4863
|
342 |
# endif // __POINTER_SIZE__ == 8
|
4846
|
343 |
if (((unsigned INT)cp & 3) == 0) {
|
|
344 |
// word aligned
|
|
345 |
for ( ; n >= 4 ; n -= 4, cp += 4) {
|
|
346 |
unsigned int word;
|
|
347 |
unsigned char _idx;
|
|
348 |
|
|
349 |
word = ((unsigned int *)cp)[0];
|
|
350 |
_idx = (_crc ^ word) & 0xFF;
|
|
351 |
_crc = _crcTable[_idx] ^ (_crc >> 8);
|
|
352 |
_idx = (_crc ^ (word>>8)) & 0xFF;
|
|
353 |
_crc = _crcTable[_idx] ^ (_crc >> 8);
|
|
354 |
_idx = (_crc ^ (word>>16)) & 0xFF;
|
|
355 |
_crc = _crcTable[_idx] ^ (_crc >> 8);
|
|
356 |
_idx = (_crc ^ (word>>24)) & 0xFF;
|
|
357 |
_crc = _crcTable[_idx] ^ (_crc >> 8);
|
|
358 |
}
|
|
359 |
}
|
4863
|
360 |
#endif // LSBFIRST
|
4846
|
361 |
for ( ; n >= 4 ; n -= 4, cp += 4) {
|
|
362 |
unsigned char _idx;
|
|
363 |
|
|
364 |
_idx = (_crc ^ cp[0]) & 0xFF;
|
|
365 |
_crc = _crcTable[_idx] ^ (_crc >> 8);
|
|
366 |
_idx = (_crc ^ cp[1]) & 0xFF;
|
|
367 |
_crc = _crcTable[_idx] ^ (_crc >> 8);
|
|
368 |
_idx = (_crc ^ cp[2]) & 0xFF;
|
|
369 |
_crc = _crcTable[_idx] ^ (_crc >> 8);
|
|
370 |
_idx = (_crc ^ cp[3]) & 0xFF;
|
|
371 |
_crc = _crcTable[_idx] ^ (_crc >> 8);
|
|
372 |
}
|
|
373 |
while (n-- > 0) {
|
|
374 |
unsigned char _idx = (_crc ^ *cp++) & 0xFF;
|
|
375 |
_crc = _crcTable[_idx] ^ (_crc >> 8);
|
|
376 |
}
|
|
377 |
|
4907
|
378 |
#if __POINTER_SIZE__ == 8
|
|
379 |
__INST(crc) = __MKSMALLINT(_crc);
|
|
380 |
#else
|
4860
|
381 |
if (_crc <= _MAX_INT) {
|
4846
|
382 |
__INST(crc) = __MKSMALLINT(_crc);
|
4860
|
383 |
} else {
|
4846
|
384 |
// this code fails with gcc 4.7.2:
|
|
385 |
// __INST(crc) = __MKUINT(_crc); __STORESELF(crc);
|
|
386 |
OBJ temp = __MKUINT(_crc);
|
|
387 |
__INST(crc) = temp; __STORESELF(crc);
|
|
388 |
}
|
4907
|
389 |
#endif
|
4846
|
390 |
RETURN (count);
|
|
391 |
}
|
|
392 |
}
|
|
393 |
bad: ;
|
|
394 |
%}.
|
4995
|
395 |
ArgumentError raise
|
4846
|
396 |
|
|
397 |
"Created: / 09-01-2012 / 16:48:35 / cg"
|
4995
|
398 |
"Modified: / 06-06-2019 / 23:16:48 / Claus Gittinger"
|
4846
|
399 |
! !
|
|
400 |
|
4844
|
401 |
!CRCStream class methodsFor:'documentation'!
|
|
402 |
|
|
403 |
version_CVS
|
|
404 |
^ '$Header$'
|
|
405 |
! !
|
|
406 |
|