CRC32Stream.st
author Stefan Vogel <sv@exept.de>
Fri, 13 May 2011 17:21:46 +0200
changeset 2570 55ce3e8bd7dc
parent 2569 abfbf6f1682b
child 2571 12d5e280eea1
permissions -rw-r--r--
Support additional generator polynoms (e.g. CRC-32C) class definition added:5 methods changed: #documentation #nextPut:
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2416
df3f9f02c8d8 added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2334
diff changeset
     1
"
df3f9f02c8d8 added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2334
diff changeset
     2
 COPYRIGHT (c) 2003 by eXept Software AG
df3f9f02c8d8 added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2334
diff changeset
     3
              All Rights Reserved
df3f9f02c8d8 added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2334
diff changeset
     4
df3f9f02c8d8 added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2334
diff changeset
     5
 This software is furnished under a license and may be used
df3f9f02c8d8 added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2334
diff changeset
     6
 only in accordance with the terms of that license and with the
df3f9f02c8d8 added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2334
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
df3f9f02c8d8 added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2334
diff changeset
     8
 be provided or otherwise made available to, or used by, any
df3f9f02c8d8 added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2334
diff changeset
     9
 other person.  No title to or ownership of the software is
df3f9f02c8d8 added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2334
diff changeset
    10
 hereby transferred.
df3f9f02c8d8 added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2334
diff changeset
    11
"
1975
0850a3e0d484 Fix examples
Stefan Vogel <sv@exept.de>
parents: 1192
diff changeset
    12
"{ Package: 'stx:libbasic2' }"
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    13
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    14
HashStream subclass:#CRC32Stream
2570
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
    15
	instanceVariableNames:'crc generatorPolynom crcTable'
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
    16
	classVariableNames:'CrcTables'
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    17
	poolDictionaries:''
2324
1f5b2b5500e4 category changes
Claus Gittinger <cg@exept.de>
parents: 2310
diff changeset
    18
	category:'System-Crypt-Hashing'
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    19
!
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    20
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    21
!CRC32Stream class methodsFor:'documentation'!
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    22
2416
df3f9f02c8d8 added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2334
diff changeset
    23
copyright
df3f9f02c8d8 added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2334
diff changeset
    24
"
df3f9f02c8d8 added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2334
diff changeset
    25
 COPYRIGHT (c) 2003 by eXept Software AG
df3f9f02c8d8 added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2334
diff changeset
    26
              All Rights Reserved
df3f9f02c8d8 added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2334
diff changeset
    27
df3f9f02c8d8 added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2334
diff changeset
    28
 This software is furnished under a license and may be used
df3f9f02c8d8 added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2334
diff changeset
    29
 only in accordance with the terms of that license and with the
df3f9f02c8d8 added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2334
diff changeset
    30
 inclusion of the above copyright notice.   This software may not
df3f9f02c8d8 added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2334
diff changeset
    31
 be provided or otherwise made available to, or used by, any
df3f9f02c8d8 added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2334
diff changeset
    32
 other person.  No title to or ownership of the software is
df3f9f02c8d8 added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2334
diff changeset
    33
 hereby transferred.
df3f9f02c8d8 added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2334
diff changeset
    34
"
df3f9f02c8d8 added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2334
diff changeset
    35
!
df3f9f02c8d8 added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2334
diff changeset
    36
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    37
documentation
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    38
"
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    39
    Standard CRC method as defined by ISO 3309 [ISO-3309] or ITU-T V.42 [ITU-T-V42]. 
2570
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
    40
    The default CRC polynomial employed is
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    41
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    42
        x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x+1
2569
abfbf6f1682b changed:
Stefan Vogel <sv@exept.de>
parents: 2564
diff changeset
    43
        (or 16r04C11DB7)
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    44
2570
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
    45
    You can also create an instace perforing the Castagnioli CRC-32C 
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
    46
    (used in iSCSI & SCTP, G.hn payload, SSE4.2):
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
    47
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
    48
        self newCrc32c
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
    49
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
    50
        x32 + x28 + x27 + x26 + x25 + x23 + x22 + x20 + x19 + x18 + x14 + x13 + x11 + x10 + x9 + x8 + x6 + 1 
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
    51
2310
c6f63285e4c8 changed: #documentation
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
    52
    Use CRC to protect against communication errors;
c6f63285e4c8 changed: #documentation
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
    53
    do NOT use CRC for cryptography - use SHA1Stream or MD5Stream instead.
c6f63285e4c8 changed: #documentation
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
    54
c6f63285e4c8 changed: #documentation
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
    55
    Notice that this CRC is also used with PNG images - therefore, its performance 
c6f63285e4c8 changed: #documentation
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
    56
    directly affects png image processing.
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    57
2306
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
    58
    throughput:
2307
213162f0a57e changed:
Claus Gittinger <cg@exept.de>
parents: 2306
diff changeset
    59
        150000 Kb/s on 2Ghz Duo
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    60
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    61
    [author:]
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    62
        Stefan Vogel (stefan@zwerg)
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    63
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    64
    [instance variables:]
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    65
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    66
    [class variables:]
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    67
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    68
    [see also:]
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    69
        SHA1Stream
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    70
        MD5Stream
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    71
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    72
"
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    73
!
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    74
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    75
examples
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    76
"
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    77
2306
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
    78
  expect 60C1D0A0
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    79
                                                                [exBegin]
1977
d4a04a73f200 Speed up ByteArray computation
Stefan Vogel <sv@exept.de>
parents: 1976
diff changeset
    80
    self information:(CRC32Stream hashValueOf:'resume') hexPrintString
1976
0f16805e4067 Fix examples
Stefan Vogel <sv@exept.de>
parents: 1975
diff changeset
    81
                                                                [exEnd]
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    82
2306
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
    83
  expect 16r60C1D0A0
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    84
                                                                [exBegin]
1978
f7330488b12c changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents: 1977
diff changeset
    85
    self information:(CRC32Stream new
f7330488b12c changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents: 1977
diff changeset
    86
                            nextPut:$r;
f7330488b12c changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents: 1977
diff changeset
    87
                            nextPut:$e;
f7330488b12c changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents: 1977
diff changeset
    88
                            nextPut:$s;
f7330488b12c changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents: 1977
diff changeset
    89
                            nextPut:$u;
f7330488b12c changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents: 1977
diff changeset
    90
                            nextPut:$m;
f7330488b12c changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents: 1977
diff changeset
    91
                            nextPut:$e;
f7330488b12c changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents: 1977
diff changeset
    92
                            hashValue) hexPrintString
f7330488b12c changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents: 1977
diff changeset
    93
                                                                [exEnd]
f7330488b12c changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents: 1977
diff changeset
    94
2306
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
    95
  expect 16r98DC9ED7:
1978
f7330488b12c changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents: 1977
diff changeset
    96
                                                                [exBegin]
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    97
    self information:(CRC32Stream new nextPut:4711; hashValue) printString
1976
0f16805e4067 Fix examples
Stefan Vogel <sv@exept.de>
parents: 1975
diff changeset
    98
                                                                [exEnd]
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    99
2306
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   100
  expect 16r70E46888:
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   101
                                                                [exBegin]
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   102
    self information:(CRC32Stream hashValueOf:#[1 2 3 4 5 6 7]) printString
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   103
                                                                [exEnd]
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   104
2306
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   105
  expect 16r8CD04C73:
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   106
                                                                [exBegin]
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   107
    self information:((CRC32Stream hashValueOf:#[16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   108
             16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   109
             16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF
1975
0850a3e0d484 Fix examples
Stefan Vogel <sv@exept.de>
parents: 1192
diff changeset
   110
             16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF]) digitBytes hexPrintString)
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   111
                                                                [exEnd]
2306
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   112
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   113
  expect 16r86D7D79A:
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   114
  timing throughput:
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   115
                                                                [exBegin]
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   116
    |hashStream n t|
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   117
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   118
    hashStream := CRC32Stream new.
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   119
    n := 100000.
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   120
    t := Time millisecondsToRun:[
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   121
            n timesRepeat:[
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   122
                hashStream nextPutAll:'12345678901234567890123456789012345678901234567890'.
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   123
            ].
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   124
         ].
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   125
    t := (t / 1000) asFloat.
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   126
    Transcript show:'crc32:'; showCR: hashStream hashValue.
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   127
    Transcript show:t; show:' seconds for '; show:(50*n/1024) asFloat; showCR:' Kb'.
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   128
    Transcript show:(n*50/1024 / t); showCR:' Kb/s'
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   129
                                                                [exEnd]
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   130
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   131
"
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   132
! !
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   133
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   134
!CRC32Stream class methodsFor:'initialization'!
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   135
2570
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   136
crcTableFor:generatorPolynomInteger
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   137
    |crcTable|
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   138
2570
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   139
    crcTable := (CrcTables at:generatorPolynomInteger ifAbsent:nil). 
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   140
    crcTable isNil ifTrue:[
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   141
        crcTable := IntegerArray new:256.
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   142
2570
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   143
        0 to:255 do:[:count| |i|
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   144
            i := count.
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   145
            8 timesRepeat:[
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   146
                (i bitTest:1) ifTrue:[
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   147
                    i := generatorPolynomInteger bitXor:(i bitShift:-1)
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   148
                ] ifFalse:[
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   149
                    i := i bitShift:-1
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   150
                ]
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   151
            ].
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   152
            crcTable at:count+1 put:i.
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   153
        ].
2570
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   154
        CrcTables at:generatorPolynomInteger put:crcTable.
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   155
    ].
2570
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   156
    ^ crcTable.
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   157
!
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   158
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   159
initialize
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   160
    CrcTables := Dictionary new.
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   161
! !
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   162
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   163
!CRC32Stream class methodsFor:'instance creation'!
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   164
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   165
generatorPolynom:anInteger
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   166
    ^ self basicNew generatorPolynom:anInteger
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   167
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   168
    "
2570
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   169
       self assert:((self generatorPolynom:16r82F63B78)
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   170
                                nextPut:'123456789';
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   171
                                hashValue)    = 16rE3069283
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   172
    "
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   173
!
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   174
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   175
newCrc32C
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   176
    "return an instance of the Castagnoli CRC-32
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   177
        x32 + x28 + x27 + x26 + x25 + x23 + x22 + x20 + x19 + x18 + x14 + x13 + x11 + x10 + x9 + x8 + x6 + 1 
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   178
     (used in iSCSI & SCTP, G.hn payload, SSE4.2)"
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   179
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   180
    ^ self basicNew generatorPolynom:16r82F63B78
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   181
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   182
    "
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   183
       self assert:((self newCrc32C)
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   184
                                nextPut:'123456789';
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   185
                                hashValue)    = 16rE3069283
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   186
    "
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   187
! !
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   188
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   189
!CRC32Stream class methodsFor:'testing'!
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   190
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   191
testVector
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   192
    "define a testvector to test the implementation"
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   193
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   194
    ^ #(
2569
abfbf6f1682b changed:
Stefan Vogel <sv@exept.de>
parents: 2564
diff changeset
   195
          ('' 0)
abfbf6f1682b changed:
Stefan Vogel <sv@exept.de>
parents: 2564
diff changeset
   196
abfbf6f1682b changed:
Stefan Vogel <sv@exept.de>
parents: 2564
diff changeset
   197
          ('123456789' 16rCBF43926)
abfbf6f1682b changed:
Stefan Vogel <sv@exept.de>
parents: 2564
diff changeset
   198
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   199
          (#[16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   200
             16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   201
             16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   202
             16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF]
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   203
           2362461299)
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   204
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   205
          (#[0 0 0 0 0 0 0 0 0 0 
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   206
             0 0 0 0 0 0 0 0 0 0
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   207
             0 0 0 0 0 0 0 0 0 0
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   208
             0 0 0 0 0 0 0 0 0 0]
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   209
           3924573617)
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   210
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   211
          (#[0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   212
             20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39]
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   213
           228994620 )
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   214
       )
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   215
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   216
    "
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   217
     self test
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   218
    "
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   219
! !
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   220
2570
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   221
!CRC32Stream methodsFor:'accessing'!
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   222
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   223
generatorPolynom
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   224
    "answer the generator polynom"
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   225
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   226
    ^ generatorPolynom
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   227
!
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   228
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   229
generatorPolynom:anInteger
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   230
    generatorPolynom := anInteger.
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   231
    crc := 16rFFFFFFFF.
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   232
    crcTable := self class crcTableFor:generatorPolynom.
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   233
! !
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   234
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   235
!CRC32Stream methodsFor:'initialization'!
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   236
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   237
initialize
2570
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   238
    "initialize the CRC to CRC-32 ITU-T:
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   239
        x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x+1"
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   240
2570
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   241
    self generatorPolynom:16rEDB88320
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   242
! !
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   243
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   244
!CRC32Stream methodsFor:'queries'!
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   245
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   246
hashValue
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   247
    "return the computed CRC"
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   248
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   249
    ^ crc bitXor:16rFFFFFFFF
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   250
! !
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   251
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   252
!CRC32Stream methodsFor:'writing'!
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   253
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   254
nextPut:arg 
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   255
    "add the hash of anObject to the computed hash so far.
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   256
     anObject can be a Character, SmallInteger, or Character-, Byte-
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   257
     Integer-Array"
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   258
1977
d4a04a73f200 Speed up ByteArray computation
Stefan Vogel <sv@exept.de>
parents: 1976
diff changeset
   259
    | argSize "{ Class:SmallInteger }"
d4a04a73f200 Speed up ByteArray computation
Stefan Vogel <sv@exept.de>
parents: 1976
diff changeset
   260
      byte    "{ Class:SmallInteger }" |
d4a04a73f200 Speed up ByteArray computation
Stefan Vogel <sv@exept.de>
parents: 1976
diff changeset
   261
2306
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   262
%{
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   263
#if 1
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   264
    unsigned char *cp;
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   265
    int n;
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   266
    unsigned char c;
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   267
2334
1a245a5b81ab change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents: 2332
diff changeset
   268
    if (__isStringLike(arg)) {
2306
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   269
        cp = __stringVal(arg);
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   270
        n = __stringSize(arg);
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   271
        goto doBytes;
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   272
    }
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   273
2332
c3876696e7c9 __isByteArray() to __isByteArrayLike() in primitive code
Stefan Vogel <sv@exept.de>
parents: 2324
diff changeset
   274
    if (__isByteArrayLike(arg)) {
2306
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   275
        cp = __byteArrayVal(arg);
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   276
        n = __byteArraySize(arg);
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   277
    doBytes:
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   278
        {
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   279
            unsigned int _crc;
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   280
            unsigned int *_crcTable;
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   281
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   282
            if (__isSmallInteger(__INST(crc)) ) {
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   283
                _crc = (unsigned) (__intVal( __INST(crc) ));
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   284
            } else {
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   285
                _crc = __unsignedLongIntVal( __INST(crc) );
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   286
            }
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   287
2570
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   288
            _crcTable = __integerArrayVal( __INST(crcTable) );
2306
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   289
2307
213162f0a57e changed:
Claus Gittinger <cg@exept.de>
parents: 2306
diff changeset
   290
            while (n > 3) {
213162f0a57e changed:
Claus Gittinger <cg@exept.de>
parents: 2306
diff changeset
   291
                unsigned _idx;
213162f0a57e changed:
Claus Gittinger <cg@exept.de>
parents: 2306
diff changeset
   292
213162f0a57e changed:
Claus Gittinger <cg@exept.de>
parents: 2306
diff changeset
   293
                _idx = (_crc ^ cp[0]) & 0xFF;
213162f0a57e changed:
Claus Gittinger <cg@exept.de>
parents: 2306
diff changeset
   294
                _crc = _crcTable[_idx] ^ (_crc >> 8);
213162f0a57e changed:
Claus Gittinger <cg@exept.de>
parents: 2306
diff changeset
   295
                _idx = (_crc ^ cp[1]) & 0xFF;
213162f0a57e changed:
Claus Gittinger <cg@exept.de>
parents: 2306
diff changeset
   296
                _crc = _crcTable[_idx] ^ (_crc >> 8);
213162f0a57e changed:
Claus Gittinger <cg@exept.de>
parents: 2306
diff changeset
   297
                _idx = (_crc ^ cp[2]) & 0xFF;
213162f0a57e changed:
Claus Gittinger <cg@exept.de>
parents: 2306
diff changeset
   298
                _crc = _crcTable[_idx] ^ (_crc >> 8);
213162f0a57e changed:
Claus Gittinger <cg@exept.de>
parents: 2306
diff changeset
   299
                _idx = (_crc ^ cp[3]) & 0xFF;
213162f0a57e changed:
Claus Gittinger <cg@exept.de>
parents: 2306
diff changeset
   300
                _crc = _crcTable[_idx] ^ (_crc >> 8);
213162f0a57e changed:
Claus Gittinger <cg@exept.de>
parents: 2306
diff changeset
   301
                cp += 4;
213162f0a57e changed:
Claus Gittinger <cg@exept.de>
parents: 2306
diff changeset
   302
                n -= 4;
213162f0a57e changed:
Claus Gittinger <cg@exept.de>
parents: 2306
diff changeset
   303
            }
2306
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   304
            while (n > 0) {
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   305
                unsigned _byte = *cp++;
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   306
                unsigned _idx;
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   307
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   308
                _idx = (_crc ^ _byte) & 0xFF;
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   309
                _crc = _crcTable[_idx] ^ (_crc >> 8);
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   310
                n--;
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   311
            }
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   312
            __INST(crc) = __MKUINT(_crc);
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   313
        }
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   314
        RETURN (self );
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   315
    }
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   316
    if (__isCharacter(arg)) {
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   317
        unsigned int value;
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   318
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   319
        value = __intVal(_characterVal(arg));
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   320
        if ((unsigned)value <= 0xFF) {
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   321
            c = value;
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   322
            cp = &c;
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   323
            n = 1;
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   324
            goto doBytes;
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   325
        }
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   326
    }
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   327
#endif
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   328
%}.
1977
d4a04a73f200 Speed up ByteArray computation
Stefan Vogel <sv@exept.de>
parents: 1976
diff changeset
   329
    arg isByteCollection ifTrue:[
d4a04a73f200 Speed up ByteArray computation
Stefan Vogel <sv@exept.de>
parents: 1976
diff changeset
   330
        argSize := arg size.
d4a04a73f200 Speed up ByteArray computation
Stefan Vogel <sv@exept.de>
parents: 1976
diff changeset
   331
        1 to:argSize do:[:n|
d4a04a73f200 Speed up ByteArray computation
Stefan Vogel <sv@exept.de>
parents: 1976
diff changeset
   332
            byte := arg byteAt:n.
2570
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   333
            crc := (crcTable at:((crc bitXor:byte) bitAnd:16rFF)+1) 
1977
d4a04a73f200 Speed up ByteArray computation
Stefan Vogel <sv@exept.de>
parents: 1976
diff changeset
   334
                   bitXor:(crc bitShift:-8).
d4a04a73f200 Speed up ByteArray computation
Stefan Vogel <sv@exept.de>
parents: 1976
diff changeset
   335
        ].
d4a04a73f200 Speed up ByteArray computation
Stefan Vogel <sv@exept.de>
parents: 1976
diff changeset
   336
        ^ self.
d4a04a73f200 Speed up ByteArray computation
Stefan Vogel <sv@exept.de>
parents: 1976
diff changeset
   337
    ].
d4a04a73f200 Speed up ByteArray computation
Stefan Vogel <sv@exept.de>
parents: 1976
diff changeset
   338
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   339
    arg isCharacter ifTrue:[
1978
f7330488b12c changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents: 1977
diff changeset
   340
        byte := arg codePoint.
f7330488b12c changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents: 1977
diff changeset
   341
        byte < 256 ifTrue:[
2570
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   342
            crc := (crcTable at:((crc bitXor:byte) bitAnd:16rFF)+1) 
1978
f7330488b12c changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents: 1977
diff changeset
   343
                   bitXor:(crc bitShift:-8).
f7330488b12c changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents: 1977
diff changeset
   344
            ^ self.
f7330488b12c changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents: 1977
diff changeset
   345
        ].
f7330488b12c changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents: 1977
diff changeset
   346
        byte digitBytes do:[:eachByte|
2570
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   347
            crc := (crcTable at:((crc bitXor:eachByte) bitAnd:16rFF)+1) 
1978
f7330488b12c changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents: 1977
diff changeset
   348
                   bitXor:(crc bitShift:-8).
f7330488b12c changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents: 1977
diff changeset
   349
        ].
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   350
        ^ self.
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   351
    ].
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   352
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   353
    arg isInteger ifTrue:[
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   354
        arg < 256 ifTrue:[
2570
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   355
            crc := (crcTable at:((crc bitXor:arg) bitAnd:16rFF)+1) 
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   356
                   bitXor:(crc bitShift:-8).
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   357
            ^ self.
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   358
        ].
1977
d4a04a73f200 Speed up ByteArray computation
Stefan Vogel <sv@exept.de>
parents: 1976
diff changeset
   359
        arg digitBytes do:[:eachByte|
2570
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   360
            crc := (crcTable at:((crc bitXor:eachByte) bitAnd:16rFF)+1) 
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   361
                   bitXor:(crc bitShift:-8).
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   362
        ].
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   363
        ^ self.
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   364
    ].
1977
d4a04a73f200 Speed up ByteArray computation
Stefan Vogel <sv@exept.de>
parents: 1976
diff changeset
   365
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   366
    arg isCollection ifTrue:[
1977
d4a04a73f200 Speed up ByteArray computation
Stefan Vogel <sv@exept.de>
parents: 1976
diff changeset
   367
        arg do:[:eachElement|
d4a04a73f200 Speed up ByteArray computation
Stefan Vogel <sv@exept.de>
parents: 1976
diff changeset
   368
            self nextPut:eachElement
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   369
        ].
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   370
        ^ self.
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   371
    ].
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   372
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   373
    self error:'unsupported argument'.
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   374
! !
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   375
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   376
!CRC32Stream class methodsFor:'documentation'!
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   377
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   378
version
2570
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   379
    ^ '$Header: /cvs/stx/stx/libbasic2/CRC32Stream.st,v 1.15 2011-05-13 15:21:46 stefan Exp $'
2306
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   380
!
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   381
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   382
version_CVS
2570
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   383
    ^ '$Header: /cvs/stx/stx/libbasic2/CRC32Stream.st,v 1.15 2011-05-13 15:21:46 stefan Exp $'
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   384
! !
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   385
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   386
CRC32Stream initialize!