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