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