#FEATURE by cg
authorClaus Gittinger <cg@exept.de>
Sun, 24 Mar 2019 21:23:45 +0100
changeset 4905 dc9731862841
parent 4904 9131ab1c257b
child 4906 50fae51f281d
#FEATURE by cg class: CRC16Stream class definition added: #byteSwapped #byteSwapped: changed: #hashValue class: CRC16Stream class added: #hashSize #newCCITT_1D0F #newXMODEM removed: #newModbus comment/format in: #documentation #newCCITT #newDNP changed: #newKERMIT #newMODBUS
CRC16Stream.st
--- a/CRC16Stream.st	Sun Mar 24 21:22:45 2019 +0100
+++ b/CRC16Stream.st	Sun Mar 24 21:23:45 2019 +0100
@@ -16,7 +16,7 @@
 "{ NameSpace: Smalltalk }"
 
 CRCStream subclass:#CRC16Stream
-	instanceVariableNames:''
+	instanceVariableNames:'byteSwapped'
 	classVariableNames:''
 	poolDictionaries:''
 	category:'System-Crypt-Hashing'
@@ -45,21 +45,37 @@
      - use secure hashes for those instead.
 
     ATTENTION: CRC16Stream is currently BROKEN (in development)
+
+    crc16 is a full zoo of different implementations;
+    they differ in the polynom, the initial value and the final xor value.
+    Also, some return an MSB, others an LSB first value (i.e. byteswapped).
+    Make sure to use the correct one:
     
     newCRC_CCITT:
-      Standard CRC method as defined by CCITT 
+      Standard CRC method as defined by CCITT with FFFF as start value 
       (ITU-T-T30, ITU-T V42, ITU-T X25, RFC1331, RFC1662, ISO/IEC FCD14443-3).
       This is used in data link protocols such as HDLC, SS7, and ISDN.
     
-      The default polynomial is
+      The polynomial is
         x^16 + x^12 + x^5 + 1
 
     newCRC_DNP:
       Standard CRC method as defined by DNP.
       This is used in data link protocols such as HDLC, SS7, and ISDN.
+      DNP 3.0, or distributed network protocol is a communication protocol 
+      designed for use between substation computers, RTUs remote terminal units, 
+      IEDs intelligent electronic devices 
+      and master stations for the electric utility industry. 
+      It is now also used in familiar industries like waste water treatment, 
+      transportation and the oil and gas industry.
+      The polynomial is 0x3d65
 
-      The default polynomial is 0x3d65
+    newKERMIT:
+      CRC as used in kermit protocol
 
+    newMODBUS:
+      CRC as used in the modbus protocol
+      
     [author:]
         Claus Gittinger
 
@@ -162,14 +178,25 @@
 !
 
 newCCITT
-    "return an instance of the CCITT CRC-16
+    "return an instance of the CCITT CRC-16 with FFFF as initial value
         x16 + x12 + x5 + 1 "
 
     "/ 16r1021 bitReversed16 => 16r8408
-    ^ self generatorPolynomMSB:16r1021 initValue:0 xorOut:16rFFFF
+    ^ self generatorPolynomMSB:16r1021 initValue:16rFFFF xorOut:0
 
     "Created: / 16-03-2019 / 21:13:11 / Claus Gittinger"
-    "Modified (comment): / 17-03-2019 / 14:00:33 / Claus Gittinger"
+    "Modified: / 24-03-2019 / 11:10:18 / Claus Gittinger"
+    "Modified (comment): / 24-03-2019 / 20:57:44 / Claus Gittinger"
+!
+
+newCCITT_1D0F
+    "return an instance of the CCITT CRC-16 with 1D0F as initial value
+        x16 + x12 + x5 + 1 "
+
+    "/ 16r1021 bitReversed16 => 16r8408
+    ^ self generatorPolynomMSB:16r1021 initValue:16r1D0F xorOut:0
+
+    "Created: / 24-03-2019 / 20:57:32 / Claus Gittinger"
 !
 
 newCRC_16
@@ -184,12 +211,20 @@
 
 newDNP
     "return an instance of the DNP CRC-16
-        x16 + x13 + x12 + x11 + x10 + x8 + x6 + x5 + x2 + 1 "
+        x16 + x13 + x12 + x11 + x10 + x8 + x6 + x5 + x2 + 1
 
-    ^ self generatorPolynomMSB:16r3d65 initValue:0 xorOut:16rFFFF
+     DNP 3.0, or distributed network protocol is a communication protocol 
+     designed for use between substation computers, RTUs remote terminal units, 
+     IEDs intelligent electronic devices 
+     and master stations for the electric utility industry. 
+     It is now also used in familiar industries like waste water treatment, 
+     transportation and the oil and gas industry."
+
+    "/ 16r3d65 bitReversed16 hexPrintString -> 'A6BC'
+    ^ (self generatorPolynom:16rA6BC initValue:0 xorOut:16rFFFF) byteSwapped:true
 
     "Created: / 16-03-2019 / 20:50:03 / Claus Gittinger"
-    "Modified (comment): / 17-03-2019 / 14:06:53 / Claus Gittinger"
+    "Modified: / 24-03-2019 / 21:17:58 / Claus Gittinger"
 !
 
 newKERMIT
@@ -198,9 +233,10 @@
         x16 + x13 + x12 + x11 + x10 + x8 + x6 + x5 + x2 + 1 "
 
     "/ 16r1021 bitReversed16 => 16r8408
-    ^ self generatorPolynomMSB:16r1021 initValue:0 xorOut:0
+    ^ (self generatorPolynomMSB:16r1021 initValue:0 xorOut:0) byteSwapped:true
 
     "Created: / 17-03-2019 / 13:51:20 / Claus Gittinger"
+    "Modified: / 24-03-2019 / 20:10:31 / Claus Gittinger"
 !
 
 newMODBUS
@@ -208,24 +244,47 @@
         x16 + x15 + x2 + 1 "
 
     "/ 16r8005 bitReversed16 => 16rA001
-    ^ self generatorPolynomMSB:16rA001 initValue:16rFFFF xorOut:16rFFFF
+    ^ self generatorPolynom:16rA001 initValue:16rFFFF xorOut:0
 
     "Created: / 17-03-2019 / 14:22:41 / Claus Gittinger"
+    "Modified: / 24-03-2019 / 20:08:41 / Claus Gittinger"
 !
 
-newModbus
-    "return an instance of the Modbus CRC-16
-        2r11110101100101
-        x16 + x13 + x12 + x11 + x10 + x8 + x6 + x5 + x2 + 1 "
+newXMODEM
+    "return an instance of the XMODEM protocol
+        x16 + x12 + x5 + 1
+        1 2r0001000000100001 "
+    ^ self generatorPolynom:16r1021 initValue:0 xorOut:0
+    "/ ^ self generatorPolynomMSB:16r1021 initValue:0 xorOut:0
 
-    ^ self generatorPolynomMSB:16r8005 initValue:16rFFFF xorOut:0
+    "Created: / 24-03-2019 / 20:16:08 / Claus Gittinger"
+    "Modified: / 24-03-2019 / 21:20:08 / Claus Gittinger"
+! !
+
+!CRC16Stream class methodsFor:'queries'!
 
-    "Created: / 16-03-2019 / 21:15:26 / Claus Gittinger"
-    "Modified (comment): / 17-03-2019 / 00:00:23 / Claus Gittinger"
+hashSize
+    "return the size of the hashvalue returned by instances of this class (in bytes)"
+
+    ^ 2
+
+    "Created: / 24-03-2019 / 12:53:05 / Claus Gittinger"
 ! !
 
 !CRC16Stream methodsFor:'initialization'!
 
+byteSwapped
+    ^ byteSwapped ? false
+
+    "Created: / 24-03-2019 / 20:10:12 / Claus Gittinger"
+!
+
+byteSwapped:aBoolean
+    byteSwapped := aBoolean
+
+    "Created: / 24-03-2019 / 20:10:01 / Claus Gittinger"
+!
+
 generatorPolynom:anLSBInteger
     "set the generator polynom for this instance,
      set start and xorOut to 16rFFFF.
@@ -259,12 +318,13 @@
     |hv|
 
     hv := super hashValue.
-    UninterpretedBytes isBigEndian ifFalse:[
+    (byteSwapped == true) ifTrue:[
         ^ hv byteSwapped16
     ].    
     ^ hv.
 
     "Created: / 17-03-2019 / 13:45:29 / Claus Gittinger"
+    "Modified: / 24-03-2019 / 20:11:49 / Claus Gittinger"
 ! !
 
 !CRC16Stream class methodsFor:'documentation'!