CRCStream.st
author Claus Gittinger <cg@exept.de>
Tue, 25 Jun 2019 14:28:51 +0200
changeset 5050 44fa8672d102
parent 4995 17232c18b66c
child 5381 91f4a33eddd5
permissions -rw-r--r--
#DOCUMENTATION by cg class: SharedQueue comment/format in: #next #nextWithTimeout:
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4907
6885f756ddce #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4897
diff changeset
     1
"{ Encoding: utf8 }"
6885f756ddce #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4897
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
4907
6885f756ddce #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4897
diff changeset
    59
    "construct the polynom-specific lookup table"
6885f756ddce #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4897
diff changeset
    60
    
4844
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    61
    |crcTable|
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    62
4907
6885f756ddce #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4897
diff changeset
    63
    "/ CRC8Stream flushCrcTables
6885f756ddce #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4897
diff changeset
    64
    "/ CRC8Stream crcTableFor:(16r1D bitReversed8)
6885f756ddce #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4897
diff changeset
    65
    
4844
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    66
    crcTables isNil ifTrue:[
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    67
        crcTables := Dictionary new
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    68
    ].    
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    69
    crcTable := (crcTables at:generatorPolynomInteger ifAbsent:nil).
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    70
    crcTable isNil ifTrue:[
4907
6885f756ddce #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4897
diff changeset
    71
        "/ for crc16 and crc8, we waste some memory 
6885f756ddce #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4897
diff changeset
    72
        "/ (could use WordArray or ByteArray);
6885f756ddce #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4897
diff changeset
    73
        "/ but that would need three writeBytes implementations.
6885f756ddce #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4897
diff changeset
    74
        "/ Not worth the saving.
4844
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    75
        crcTable := IntegerArray new:256.
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    76
4907
6885f756ddce #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4897
diff changeset
    77
        "/ least-sign. bit towards most significant bit...
4844
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    78
        0 to:255 do:[:count| |i|
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    79
            i := count.
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    80
            8 timesRepeat:[
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    81
                (i bitTest:1) ifTrue:[
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    82
                    i := generatorPolynomInteger bitXor:(i bitShift:-1)
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    83
                ] ifFalse:[
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    84
                    i := i bitShift:-1
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    85
                ]
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    86
            ].
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    87
            crcTable at:count+1 put:i.
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    88
        ].
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    89
        crcTables at:generatorPolynomInteger put:crcTable.
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    90
    ].
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    91
    ^ crcTable.
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    92
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    93
    "Created: / 16-03-2019 / 20:53:51 / Claus Gittinger"
4907
6885f756ddce #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4897
diff changeset
    94
    "Modified: / 24-03-2019 / 12:38:12 / Claus Gittinger"
4859
24002e2a232a #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4856
diff changeset
    95
!
24002e2a232a #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4856
diff changeset
    96
24002e2a232a #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4856
diff changeset
    97
flushCrcTables
24002e2a232a #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4856
diff changeset
    98
    crcTables := nil.
24002e2a232a #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4856
diff changeset
    99
24002e2a232a #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4856
diff changeset
   100
    "
24002e2a232a #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4856
diff changeset
   101
     CRC16Stream flushCrcTables
24002e2a232a #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4856
diff changeset
   102
     CRC32Stream flushCrcTables
24002e2a232a #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4856
diff changeset
   103
    "
24002e2a232a #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4856
diff changeset
   104
24002e2a232a #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4856
diff changeset
   105
    "Created: / 16-03-2019 / 23:41:19 / Claus Gittinger"
4844
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   106
! !
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   107
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   108
!CRCStream class methodsFor:'instance creation'!
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   109
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   110
generatorPolynom:anLSBInteger
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   111
    "notice, in literature, the generator polynom is usually specified as an MSB number"
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   112
4863
41d689a13dda #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4860
diff changeset
   113
    ^ self basicNew 
41d689a13dda #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4860
diff changeset
   114
        generatorPolynom:anLSBInteger
4844
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   115
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   116
    "
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   117
       self assert:((self generatorPolynom:16r82F63B78)
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   118
                                nextPut:'123456789';
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   119
                                hashValue)    = 16rE3069283
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   120
    "
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   121
4863
41d689a13dda #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4860
diff changeset
   122
    "Modified (format): / 17-03-2019 / 14:03:42 / Claus Gittinger"
4844
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   123
!
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   124
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   125
generatorPolynom:anLSBInteger initValue:initValue
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   126
    "notice, in literature, the generator polynom is usually specified as an MSB number"
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   127
4863
41d689a13dda #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4860
diff changeset
   128
    ^ self basicNew 
41d689a13dda #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4860
diff changeset
   129
        generatorPolynom:anLSBInteger 
41d689a13dda #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4860
diff changeset
   130
        initValue:initValue
4844
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   131
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   132
    "
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   133
       self assert:((self generatorPolynom:16r82F63B78)
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   134
                                nextPut:'123456789';
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   135
                                hashValue)    = 16rE3069283
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   136
    "
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   137
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   138
    "Created: / 16-03-2019 / 21:15:54 / Claus Gittinger"
4863
41d689a13dda #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4860
diff changeset
   139
    "Modified (format): / 17-03-2019 / 14:03:37 / Claus Gittinger"
4844
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   140
!
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   141
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   142
generatorPolynom:anLSBInteger initValue:initValue xorOut:xorOut
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   143
    "notice, in literature, the generator polynom is usually specified as an MSB number"
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   144
4863
41d689a13dda #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4860
diff changeset
   145
    ^ self basicNew 
41d689a13dda #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4860
diff changeset
   146
        generatorPolynom:anLSBInteger 
41d689a13dda #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4860
diff changeset
   147
        initValue:initValue xorOut:xorOut
4844
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   148
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   149
    "
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   150
       self assert:((self generatorPolynom:16r82F63B78)
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   151
                                nextPut:'123456789';
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   152
                                hashValue)    = 16rE3069283
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   153
    "
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   154
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   155
    "Created: / 16-03-2019 / 21:16:05 / Claus Gittinger"
4863
41d689a13dda #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4860
diff changeset
   156
    "Modified (format): / 17-03-2019 / 14:03:30 / Claus Gittinger"
4844
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   157
! !
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   158
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   159
!CRCStream methodsFor:'accessing'!
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   160
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   161
generatorPolynom
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   162
    "answer the generator polynom (LSB)"
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   163
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   164
    ^ generatorPolynom
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   165
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   166
    "Modified (comment): / 16-03-2019 / 20:58:10 / Claus Gittinger"
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   167
!
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   168
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   169
initValue
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   170
    "answer the init value"
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   171
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   172
    ^ initValue ? 0
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   173
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   174
    "Created: / 16-03-2019 / 21:05:04 / Claus Gittinger"
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   175
!
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   176
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   177
xorOut
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   178
    "answer the xorOut value"
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   179
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   180
    ^ xorOut ? 16rFFFFFFFF
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   181
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   182
    "Created: / 16-03-2019 / 21:16:23 / Claus Gittinger"
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   183
! !
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   184
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   185
!CRCStream methodsFor:'initialization'!
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   186
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   187
generatorPolynom:polyLSB initValue:initValueArg xorOut:xorOutArg
4863
41d689a13dda #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4860
diff changeset
   188
    "set the generator polynom for this instance.
41d689a13dda #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4860
diff changeset
   189
     And set start and xorOut.
41d689a13dda #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4860
diff changeset
   190
     Computes the crcTable for this polynom.
41d689a13dda #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4860
diff changeset
   191
     Notice the bit order is LSB"
4844
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   192
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   193
    generatorPolynom := polyLSB.
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   194
    crc := initValue := initValueArg.
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   195
    xorOut := xorOutArg.
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   196
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   197
    crcTable := self class crcTableFor:generatorPolynom.
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   198
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   199
    "Created: / 16-03-2019 / 21:17:12 / Claus Gittinger"
4863
41d689a13dda #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4860
diff changeset
   200
    "Modified (comment): / 17-03-2019 / 12:25:11 / Claus Gittinger"
4844
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   201
!
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   202
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   203
reset
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   204
    "reset the current crc value"
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   205
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   206
    crc := initValue.
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   207
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   208
    "Created: / 16-03-2019 / 21:25:27 / Claus Gittinger"
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   209
! !
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   210
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   211
!CRCStream methodsFor:'queries'!
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   212
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   213
hashValue
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   214
    "return the computed CRC"
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   215
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   216
    ^ crc bitXor:xorOut.
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   217
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   218
    "Modified: / 16-03-2019 / 21:27:59 / Claus Gittinger"
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   219
! !
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   220
4846
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   221
!CRCStream methodsFor:'writing'!
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   222
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   223
nextPutBytes:count from:anObject startingAt:start
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   224
    "add the hash of anObject to the computed hash so far."
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   225
4916
f278ff8e7e1b #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 4907
diff changeset
   226
%{
4896
17bbef385be1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4872
diff changeset
   227
    int len, offs;
17bbef385be1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4872
diff changeset
   228
17bbef385be1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4872
diff changeset
   229
    // fetch first - check later
17bbef385be1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4872
diff changeset
   230
    len = __intVal(count);
17bbef385be1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4872
diff changeset
   231
    offs = __intVal(start) - 1;
17bbef385be1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4872
diff changeset
   232
4846
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   233
    if (__bothSmallInteger(count, start)) {
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   234
        int objSize;
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   235
        unsigned char *extPtr;
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   236
4896
17bbef385be1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4872
diff changeset
   237
        // the most common first
17bbef385be1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4872
diff changeset
   238
        if (__isStringLike(anObject)) {
17bbef385be1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4872
diff changeset
   239
            objSize = __stringSize(anObject);
17bbef385be1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4872
diff changeset
   240
            extPtr = (unsigned char *)__stringVal(anObject);
17bbef385be1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4872
diff changeset
   241
        } else if (__isByteArray(anObject)) {
17bbef385be1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4872
diff changeset
   242
            objSize = __byteArraySize(anObject);
17bbef385be1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4872
diff changeset
   243
            extPtr = (unsigned char *)__byteArrayVal(anObject);
17bbef385be1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4872
diff changeset
   244
        } else if (__isExternalBytesLike(anObject)) {
4846
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   245
            OBJ sz = __externalBytesSize(anObject);
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
            extPtr = (unsigned char *)__externalBytesAddress(anObject);
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   248
            if (__isSmallInteger(sz)) {
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   249
                objSize = __intVal(sz);
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   250
            } else {
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   251
                objSize = 0; /* unknown */
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   252
            }
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   253
        } else {
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   254
            int nInstVars, nInstBytes;
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   255
            OBJ oClass = __Class(anObject);
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
            switch (__intVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) {
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   258
                case BYTEARRAY:
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   259
                case WORDARRAY:
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   260
                case LONGARRAY:
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   261
                case SWORDARRAY:
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   262
                case SLONGARRAY:
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   263
                case FLOATARRAY:
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   264
                case DOUBLEARRAY:
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   265
                    break;
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   266
                default:
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   267
                    goto bad;
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
            nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars);
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   270
            nInstBytes = __OBJS2BYTES__(nInstVars);
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   271
            // nInstBytes is the number of bytes occupied by pointer instance variables
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   272
            // subtract from size and add to byte-pointer
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   273
            objSize = __qSize(anObject) - OHDR_SIZE - nInstBytes;
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   274
            extPtr = (unsigned char *)__byteArrayVal(anObject)+nInstBytes;
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   275
        }
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
        if ((offs >= 0) && (len >= 0) && (objSize >= (len + offs))) {
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   278
            unsigned int _crc;
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   279
            unsigned int *_crcTable = __integerArrayVal( __INST(crcTable) );
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   280
            unsigned char *cp = extPtr+offs;
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   281
            unsigned int n = len;
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   282
4896
17bbef385be1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4872
diff changeset
   283
            _crc = (unsigned int) (__intVal( __INST(crc) ));
4907
6885f756ddce #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4897
diff changeset
   284
#if __POINTER_SIZE__ != 8
4896
17bbef385be1 #TUNING by cg
Claus Gittinger <cg@exept.de>
parents: 4872
diff changeset
   285
            if (!__isSmallInteger(__INST(crc))) {
4846
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   286
                _crc = __unsignedLongIntVal( __INST(crc) );
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   287
            }
4907
6885f756ddce #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4897
diff changeset
   288
#endif
4846
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   289
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   290
#ifdef __LSBFIRST__
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   291
# if __POINTER_SIZE__ == 8
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   292
            if (((unsigned INT)cp & 7) == 0) {
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   293
                // longword aligned
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   294
                for ( ; n >= 8 ; n -= 8, cp += 8) {
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   295
                    unsigned INT lWord;
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   296
                    unsigned char _idx;
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   297
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   298
                    lWord = ((unsigned INT *)cp)[0];
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   299
                    _idx = (_crc ^ lWord) & 0xFF;
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   300
                    _crc = _crcTable[_idx] ^ (_crc >> 8);
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   301
                    _idx = (_crc ^ (lWord>>8)) & 0xFF;
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   302
                    _crc = _crcTable[_idx] ^ (_crc >> 8);
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   303
                    _idx = (_crc ^ (lWord>>16)) & 0xFF;
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   304
                    _crc = _crcTable[_idx] ^ (_crc >> 8);
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   305
                    _idx = (_crc ^ (lWord>>24)) & 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
                    
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   308
                    _idx = (_crc ^ (lWord>>32)) & 0xFF;
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   309
                    _crc = _crcTable[_idx] ^ (_crc >> 8);
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   310
                    _idx = (_crc ^ (lWord>>40)) & 0xFF;
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   311
                    _crc = _crcTable[_idx] ^ (_crc >> 8);
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   312
                    _idx = (_crc ^ (lWord>>48)) & 0xFF;
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   313
                    _crc = _crcTable[_idx] ^ (_crc >> 8);
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   314
                    _idx = (_crc ^ (lWord>>56)) & 0xFF;
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   315
                    _crc = _crcTable[_idx] ^ (_crc >> 8);
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   316
                }
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   317
            }
4863
41d689a13dda #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4860
diff changeset
   318
# endif // __POINTER_SIZE__ == 8          
4846
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   319
            if (((unsigned INT)cp & 3) == 0) {
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   320
                // word aligned
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   321
                for ( ; n >= 4 ; n -= 4, cp += 4) {
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   322
                    unsigned int word;
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   323
                    unsigned char _idx;
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   324
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   325
                    word = ((unsigned int *)cp)[0];
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   326
                    _idx = (_crc ^ word) & 0xFF;
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   327
                    _crc = _crcTable[_idx] ^ (_crc >> 8);
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   328
                    _idx = (_crc ^ (word>>8)) & 0xFF;
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   329
                    _crc = _crcTable[_idx] ^ (_crc >> 8);
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   330
                    _idx = (_crc ^ (word>>16)) & 0xFF;
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   331
                    _crc = _crcTable[_idx] ^ (_crc >> 8);
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   332
                    _idx = (_crc ^ (word>>24)) & 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
                }
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   335
            }
4863
41d689a13dda #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 4860
diff changeset
   336
#endif // LSBFIRST
4846
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   337
            for ( ; n >= 4 ; n -= 4, cp += 4) {
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   338
                unsigned char _idx;
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   339
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   340
                _idx = (_crc ^ cp[0]) & 0xFF;
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   341
                _crc = _crcTable[_idx] ^ (_crc >> 8);
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   342
                _idx = (_crc ^ cp[1]) & 0xFF;
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   343
                _crc = _crcTable[_idx] ^ (_crc >> 8);
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   344
                _idx = (_crc ^ cp[2]) & 0xFF;
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   345
                _crc = _crcTable[_idx] ^ (_crc >> 8);
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   346
                _idx = (_crc ^ cp[3]) & 0xFF;
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   347
                _crc = _crcTable[_idx] ^ (_crc >> 8);
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
            while (n-- > 0) {
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   350
                unsigned char _idx = (_crc ^ *cp++) & 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
            }
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   353
4907
6885f756ddce #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4897
diff changeset
   354
#if __POINTER_SIZE__ == 8
6885f756ddce #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4897
diff changeset
   355
            __INST(crc) = __MKSMALLINT(_crc);
6885f756ddce #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4897
diff changeset
   356
#else
4860
aba42206dea6 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4859
diff changeset
   357
            if (_crc <= _MAX_INT) {
4846
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   358
                __INST(crc) = __MKSMALLINT(_crc);
4860
aba42206dea6 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4859
diff changeset
   359
            } else {
4846
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   360
                // this code fails with gcc 4.7.2:
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   361
                // __INST(crc) = __MKUINT(_crc); __STORESELF(crc);
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   362
                OBJ temp = __MKUINT(_crc); 
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   363
                __INST(crc) = temp; __STORESELF(crc);
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   364
            }
4907
6885f756ddce #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4897
diff changeset
   365
#endif
4846
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   366
            RETURN (count);
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   367
        }
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   368
    }
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   369
bad: ;
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   370
%}.
4995
17232c18b66c #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4916
diff changeset
   371
    ArgumentError raise
4846
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
    "Created: / 09-01-2012 / 16:48:35 / cg"
4995
17232c18b66c #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4916
diff changeset
   374
    "Modified: / 06-06-2019 / 23:16:48 / Claus Gittinger"
4846
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   375
! !
f5b34b8c44e3 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4844
diff changeset
   376
4844
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   377
!CRCStream class methodsFor:'documentation'!
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   378
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   379
version_CVS
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   380
    ^ '$Header$'
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   381
! !
3b189f2e8b7a initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   382