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
|
4868
|
28 |
|
4872
|
29 |
Only use CRC to protect against communication errors;
|
|
30 |
DO NOT use CRC for cryptography, authentication, security, etc.
|
|
31 |
- use secure hashes for those instead.
|
|
32 |
|
4868
|
33 |
[parameters:]
|
|
34 |
polynomials:
|
|
35 |
CRC_POLY_16 0xA001
|
|
36 |
CRC_POLY_32 0xEDB88320L
|
|
37 |
CRC_POLY_CCITT 0x1021
|
|
38 |
CRC_POLY_DNP 0xA6BC
|
|
39 |
CRC_POLY_KERMIT 0x8408
|
|
40 |
|
|
41 |
start values:
|
|
42 |
CRC_START_8 0x00
|
|
43 |
CRC_START_16 0x0000
|
|
44 |
CRC_START_MODBUS 0xFFFF
|
|
45 |
CRC_START_XMODEM 0x0000
|
|
46 |
CRC_START_CCITT_1D0F 0x1D0F
|
|
47 |
CRC_START_CCITT_FFFF 0xFFFF
|
|
48 |
CRC_START_KERMIT 0x0000
|
|
49 |
CRC_START_SICK 0x0000
|
|
50 |
CRC_START_DNP 0x0000
|
|
51 |
CRC_START_32 0xFFFFFFFF
|
|
52 |
|
4844
|
53 |
"
|
|
54 |
! !
|
|
55 |
|
|
56 |
!CRCStream class methodsFor:'initialization'!
|
|
57 |
|
|
58 |
crcTableFor:generatorPolynomInteger
|
|
59 |
|crcTable|
|
|
60 |
|
|
61 |
crcTables isNil ifTrue:[
|
|
62 |
crcTables := Dictionary new
|
|
63 |
].
|
|
64 |
crcTable := (crcTables at:generatorPolynomInteger ifAbsent:nil).
|
|
65 |
crcTable isNil ifTrue:[
|
|
66 |
crcTable := IntegerArray new:256.
|
|
67 |
|
|
68 |
0 to:255 do:[:count| |i|
|
|
69 |
i := count.
|
|
70 |
8 timesRepeat:[
|
|
71 |
(i bitTest:1) ifTrue:[
|
|
72 |
i := generatorPolynomInteger bitXor:(i bitShift:-1)
|
|
73 |
] ifFalse:[
|
|
74 |
i := i bitShift:-1
|
|
75 |
]
|
|
76 |
].
|
|
77 |
crcTable at:count+1 put:i.
|
|
78 |
].
|
|
79 |
crcTables at:generatorPolynomInteger put:crcTable.
|
|
80 |
].
|
|
81 |
^ crcTable.
|
|
82 |
|
|
83 |
"Created: / 16-03-2019 / 20:53:51 / Claus Gittinger"
|
4859
|
84 |
!
|
|
85 |
|
|
86 |
flushCrcTables
|
|
87 |
crcTables := nil.
|
|
88 |
|
|
89 |
"
|
|
90 |
CRC16Stream flushCrcTables
|
|
91 |
CRC32Stream flushCrcTables
|
|
92 |
"
|
|
93 |
|
|
94 |
"Created: / 16-03-2019 / 23:41:19 / Claus Gittinger"
|
4844
|
95 |
! !
|
|
96 |
|
|
97 |
!CRCStream class methodsFor:'instance creation'!
|
|
98 |
|
|
99 |
generatorPolynom:anLSBInteger
|
|
100 |
"notice, in literature, the generator polynom is usually specified as an MSB number"
|
|
101 |
|
4863
|
102 |
^ self basicNew
|
|
103 |
generatorPolynom:anLSBInteger
|
4844
|
104 |
|
|
105 |
"
|
|
106 |
self assert:((self generatorPolynom:16r82F63B78)
|
|
107 |
nextPut:'123456789';
|
|
108 |
hashValue) = 16rE3069283
|
|
109 |
"
|
|
110 |
|
4863
|
111 |
"Modified (format): / 17-03-2019 / 14:03:42 / Claus Gittinger"
|
4844
|
112 |
!
|
|
113 |
|
|
114 |
generatorPolynom:anLSBInteger initValue:initValue
|
|
115 |
"notice, in literature, the generator polynom is usually specified as an MSB number"
|
|
116 |
|
4863
|
117 |
^ self basicNew
|
|
118 |
generatorPolynom:anLSBInteger
|
|
119 |
initValue:initValue
|
4844
|
120 |
|
|
121 |
"
|
|
122 |
self assert:((self generatorPolynom:16r82F63B78)
|
|
123 |
nextPut:'123456789';
|
|
124 |
hashValue) = 16rE3069283
|
|
125 |
"
|
|
126 |
|
|
127 |
"Created: / 16-03-2019 / 21:15:54 / Claus Gittinger"
|
4863
|
128 |
"Modified (format): / 17-03-2019 / 14:03:37 / Claus Gittinger"
|
4844
|
129 |
!
|
|
130 |
|
|
131 |
generatorPolynom:anLSBInteger initValue:initValue xorOut:xorOut
|
|
132 |
"notice, in literature, the generator polynom is usually specified as an MSB number"
|
|
133 |
|
4863
|
134 |
^ self basicNew
|
|
135 |
generatorPolynom:anLSBInteger
|
|
136 |
initValue:initValue xorOut:xorOut
|
4844
|
137 |
|
|
138 |
"
|
|
139 |
self assert:((self generatorPolynom:16r82F63B78)
|
|
140 |
nextPut:'123456789';
|
|
141 |
hashValue) = 16rE3069283
|
|
142 |
"
|
|
143 |
|
|
144 |
"Created: / 16-03-2019 / 21:16:05 / Claus Gittinger"
|
4863
|
145 |
"Modified (format): / 17-03-2019 / 14:03:30 / Claus Gittinger"
|
4844
|
146 |
! !
|
|
147 |
|
|
148 |
!CRCStream methodsFor:'accessing'!
|
|
149 |
|
|
150 |
generatorPolynom
|
|
151 |
"answer the generator polynom (LSB)"
|
|
152 |
|
|
153 |
^ generatorPolynom
|
|
154 |
|
|
155 |
"Modified (comment): / 16-03-2019 / 20:58:10 / Claus Gittinger"
|
|
156 |
!
|
|
157 |
|
|
158 |
initValue
|
|
159 |
"answer the init value"
|
|
160 |
|
|
161 |
^ initValue ? 0
|
|
162 |
|
|
163 |
"Created: / 16-03-2019 / 21:05:04 / Claus Gittinger"
|
|
164 |
!
|
|
165 |
|
|
166 |
xorOut
|
|
167 |
"answer the xorOut value"
|
|
168 |
|
|
169 |
^ xorOut ? 16rFFFFFFFF
|
|
170 |
|
|
171 |
"Created: / 16-03-2019 / 21:16:23 / Claus Gittinger"
|
|
172 |
! !
|
|
173 |
|
|
174 |
!CRCStream methodsFor:'initialization'!
|
|
175 |
|
|
176 |
generatorPolynom:polyLSB initValue:initValueArg xorOut:xorOutArg
|
4863
|
177 |
"set the generator polynom for this instance.
|
|
178 |
And set start and xorOut.
|
|
179 |
Computes the crcTable for this polynom.
|
|
180 |
Notice the bit order is LSB"
|
4844
|
181 |
|
|
182 |
generatorPolynom := polyLSB.
|
|
183 |
crc := initValue := initValueArg.
|
|
184 |
xorOut := xorOutArg.
|
|
185 |
|
|
186 |
crcTable := self class crcTableFor:generatorPolynom.
|
|
187 |
|
|
188 |
"Created: / 16-03-2019 / 21:17:12 / Claus Gittinger"
|
4863
|
189 |
"Modified (comment): / 17-03-2019 / 12:25:11 / Claus Gittinger"
|
4844
|
190 |
!
|
|
191 |
|
|
192 |
reset
|
|
193 |
"reset the current crc value"
|
|
194 |
|
|
195 |
crc := initValue.
|
|
196 |
|
|
197 |
"Created: / 16-03-2019 / 21:25:27 / Claus Gittinger"
|
|
198 |
! !
|
|
199 |
|
|
200 |
!CRCStream methodsFor:'queries'!
|
|
201 |
|
|
202 |
hashValue
|
|
203 |
"return the computed CRC"
|
|
204 |
|
|
205 |
^ crc bitXor:xorOut.
|
|
206 |
|
|
207 |
"Modified: / 16-03-2019 / 21:27:59 / Claus Gittinger"
|
|
208 |
! !
|
|
209 |
|
4846
|
210 |
!CRCStream methodsFor:'writing'!
|
|
211 |
|
|
212 |
nextPutBytes:count from:anObject startingAt:start
|
|
213 |
"add the hash of anObject to the computed hash so far."
|
|
214 |
|
|
215 |
%{
|
|
216 |
if (__bothSmallInteger(count, start)) {
|
|
217 |
int len, offs;
|
|
218 |
int objSize;
|
|
219 |
unsigned char *extPtr;
|
|
220 |
|
|
221 |
len = __intVal(count);
|
|
222 |
offs = __intVal(start) - 1;
|
|
223 |
|
|
224 |
if (__isExternalBytesLike(anObject)) {
|
|
225 |
OBJ sz = __externalBytesSize(anObject);
|
|
226 |
|
|
227 |
extPtr = (unsigned char *)__externalBytesAddress(anObject);
|
|
228 |
if (__isSmallInteger(sz)) {
|
|
229 |
objSize = __intVal(sz);
|
|
230 |
} else {
|
|
231 |
objSize = 0; /* unknown */
|
|
232 |
}
|
|
233 |
} else {
|
|
234 |
int nInstVars, nInstBytes;
|
|
235 |
OBJ oClass = __Class(anObject);
|
|
236 |
|
|
237 |
switch (__intVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) {
|
|
238 |
case BYTEARRAY:
|
|
239 |
case WORDARRAY:
|
|
240 |
case LONGARRAY:
|
|
241 |
case SWORDARRAY:
|
|
242 |
case SLONGARRAY:
|
|
243 |
case FLOATARRAY:
|
|
244 |
case DOUBLEARRAY:
|
|
245 |
break;
|
|
246 |
default:
|
|
247 |
goto bad;
|
|
248 |
}
|
|
249 |
nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars);
|
|
250 |
nInstBytes = __OBJS2BYTES__(nInstVars);
|
|
251 |
// nInstBytes is the number of bytes occupied by pointer instance variables
|
|
252 |
// subtract from size and add to byte-pointer
|
|
253 |
objSize = __qSize(anObject) - OHDR_SIZE - nInstBytes;
|
|
254 |
extPtr = (unsigned char *)__byteArrayVal(anObject)+nInstBytes;
|
|
255 |
}
|
|
256 |
|
|
257 |
if ((offs >= 0) && (len >= 0) && (objSize >= (len + offs))) {
|
|
258 |
unsigned int _crc;
|
|
259 |
unsigned int *_crcTable = __integerArrayVal( __INST(crcTable) );
|
|
260 |
unsigned char *cp = extPtr+offs;
|
|
261 |
unsigned int n = len;
|
|
262 |
|
4856
|
263 |
if (__isSmallInteger(__INST(crc))) {
|
4846
|
264 |
_crc = (unsigned int) (__intVal( __INST(crc) ));
|
|
265 |
} else {
|
|
266 |
_crc = __unsignedLongIntVal( __INST(crc) );
|
|
267 |
}
|
|
268 |
|
|
269 |
#ifdef __LSBFIRST__
|
|
270 |
# if __POINTER_SIZE__ == 8
|
|
271 |
if (((unsigned INT)cp & 7) == 0) {
|
|
272 |
// longword aligned
|
|
273 |
for ( ; n >= 8 ; n -= 8, cp += 8) {
|
|
274 |
unsigned INT lWord;
|
|
275 |
unsigned char _idx;
|
|
276 |
|
|
277 |
lWord = ((unsigned INT *)cp)[0];
|
|
278 |
_idx = (_crc ^ lWord) & 0xFF;
|
|
279 |
_crc = _crcTable[_idx] ^ (_crc >> 8);
|
|
280 |
_idx = (_crc ^ (lWord>>8)) & 0xFF;
|
|
281 |
_crc = _crcTable[_idx] ^ (_crc >> 8);
|
|
282 |
_idx = (_crc ^ (lWord>>16)) & 0xFF;
|
|
283 |
_crc = _crcTable[_idx] ^ (_crc >> 8);
|
|
284 |
_idx = (_crc ^ (lWord>>24)) & 0xFF;
|
|
285 |
_crc = _crcTable[_idx] ^ (_crc >> 8);
|
|
286 |
|
|
287 |
_idx = (_crc ^ (lWord>>32)) & 0xFF;
|
|
288 |
_crc = _crcTable[_idx] ^ (_crc >> 8);
|
|
289 |
_idx = (_crc ^ (lWord>>40)) & 0xFF;
|
|
290 |
_crc = _crcTable[_idx] ^ (_crc >> 8);
|
|
291 |
_idx = (_crc ^ (lWord>>48)) & 0xFF;
|
|
292 |
_crc = _crcTable[_idx] ^ (_crc >> 8);
|
|
293 |
_idx = (_crc ^ (lWord>>56)) & 0xFF;
|
|
294 |
_crc = _crcTable[_idx] ^ (_crc >> 8);
|
|
295 |
}
|
|
296 |
}
|
4863
|
297 |
# endif // __POINTER_SIZE__ == 8
|
4846
|
298 |
if (((unsigned INT)cp & 3) == 0) {
|
|
299 |
// word aligned
|
|
300 |
for ( ; n >= 4 ; n -= 4, cp += 4) {
|
|
301 |
unsigned int word;
|
|
302 |
unsigned char _idx;
|
|
303 |
|
|
304 |
word = ((unsigned int *)cp)[0];
|
|
305 |
_idx = (_crc ^ word) & 0xFF;
|
|
306 |
_crc = _crcTable[_idx] ^ (_crc >> 8);
|
|
307 |
_idx = (_crc ^ (word>>8)) & 0xFF;
|
|
308 |
_crc = _crcTable[_idx] ^ (_crc >> 8);
|
|
309 |
_idx = (_crc ^ (word>>16)) & 0xFF;
|
|
310 |
_crc = _crcTable[_idx] ^ (_crc >> 8);
|
|
311 |
_idx = (_crc ^ (word>>24)) & 0xFF;
|
|
312 |
_crc = _crcTable[_idx] ^ (_crc >> 8);
|
|
313 |
}
|
|
314 |
}
|
4863
|
315 |
#endif // LSBFIRST
|
4846
|
316 |
for ( ; n >= 4 ; n -= 4, cp += 4) {
|
|
317 |
unsigned char _idx;
|
|
318 |
|
|
319 |
_idx = (_crc ^ cp[0]) & 0xFF;
|
|
320 |
_crc = _crcTable[_idx] ^ (_crc >> 8);
|
|
321 |
_idx = (_crc ^ cp[1]) & 0xFF;
|
|
322 |
_crc = _crcTable[_idx] ^ (_crc >> 8);
|
|
323 |
_idx = (_crc ^ cp[2]) & 0xFF;
|
|
324 |
_crc = _crcTable[_idx] ^ (_crc >> 8);
|
|
325 |
_idx = (_crc ^ cp[3]) & 0xFF;
|
|
326 |
_crc = _crcTable[_idx] ^ (_crc >> 8);
|
|
327 |
}
|
|
328 |
while (n-- > 0) {
|
|
329 |
unsigned char _idx = (_crc ^ *cp++) & 0xFF;
|
|
330 |
_crc = _crcTable[_idx] ^ (_crc >> 8);
|
|
331 |
}
|
|
332 |
|
4860
|
333 |
if (_crc <= _MAX_INT) {
|
4846
|
334 |
__INST(crc) = __MKSMALLINT(_crc);
|
4860
|
335 |
} else {
|
4846
|
336 |
// this code fails with gcc 4.7.2:
|
|
337 |
// __INST(crc) = __MKUINT(_crc); __STORESELF(crc);
|
|
338 |
OBJ temp = __MKUINT(_crc);
|
|
339 |
__INST(crc) = temp; __STORESELF(crc);
|
|
340 |
}
|
|
341 |
|
|
342 |
RETURN (count);
|
|
343 |
}
|
|
344 |
}
|
|
345 |
bad: ;
|
|
346 |
%}.
|
|
347 |
self error:'invalid argument'
|
|
348 |
|
|
349 |
"Created: / 09-01-2012 / 16:48:35 / cg"
|
4860
|
350 |
"Modified: / 16-03-2019 / 23:58:21 / Claus Gittinger"
|
4846
|
351 |
! !
|
|
352 |
|
4844
|
353 |
!CRCStream class methodsFor:'documentation'!
|
|
354 |
|
|
355 |
version_CVS
|
|
356 |
^ '$Header$'
|
|
357 |
! !
|
|
358 |
|