CRC32Stream.st
author Claus Gittinger <cg@exept.de>
Tue, 20 Dec 2016 22:39:17 +0100
changeset 4238 ab8390ef187a
parent 3024 c7022e1963b0
child 4845 36770d6f14bf
permissions -rw-r--r--
#DOCUMENTATION by cg class: CRC32Stream comment/format in: #documentation #examples
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
2963
Claus Gittinger <cg@exept.de>
parents: 2962
diff changeset
     3
	      All Rights Reserved
2416
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
4238
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
    14
"{ NameSpace: Smalltalk }"
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
    15
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    16
HashStream subclass:#CRC32Stream
2570
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
    17
	instanceVariableNames:'crc generatorPolynom crcTable'
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
    18
	classVariableNames:'CrcTables'
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    19
	poolDictionaries:''
2324
1f5b2b5500e4 category changes
Claus Gittinger <cg@exept.de>
parents: 2310
diff changeset
    20
	category:'System-Crypt-Hashing'
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    21
!
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    22
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    23
!CRC32Stream class methodsFor:'documentation'!
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    24
2416
df3f9f02c8d8 added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2334
diff changeset
    25
copyright
df3f9f02c8d8 added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2334
diff changeset
    26
"
df3f9f02c8d8 added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2334
diff changeset
    27
 COPYRIGHT (c) 2003 by eXept Software AG
2963
Claus Gittinger <cg@exept.de>
parents: 2962
diff changeset
    28
	      All Rights Reserved
2416
df3f9f02c8d8 added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2334
diff changeset
    29
df3f9f02c8d8 added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2334
diff changeset
    30
 This software is furnished under a license and may be used
df3f9f02c8d8 added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2334
diff changeset
    31
 only in accordance with the terms of that license and with the
df3f9f02c8d8 added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2334
diff changeset
    32
 inclusion of the above copyright notice.   This software may not
df3f9f02c8d8 added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2334
diff changeset
    33
 be provided or otherwise made available to, or used by, any
df3f9f02c8d8 added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2334
diff changeset
    34
 other person.  No title to or ownership of the software is
df3f9f02c8d8 added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2334
diff changeset
    35
 hereby transferred.
df3f9f02c8d8 added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2334
diff changeset
    36
"
df3f9f02c8d8 added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2334
diff changeset
    37
!
df3f9f02c8d8 added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2334
diff changeset
    38
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    39
documentation
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    40
"
2963
Claus Gittinger <cg@exept.de>
parents: 2962
diff changeset
    41
    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
    42
    The default CRC polynomial employed is
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    43
4238
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
    44
        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
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
    45
        (or 16r04C11DB7)
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    46
2963
Claus Gittinger <cg@exept.de>
parents: 2962
diff changeset
    47
    You can also create an instace performing the Castagnioli CRC-32C
2570
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
    48
    (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
    49
4238
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
    50
        self newCrc32c
2570
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
    51
4238
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
    52
        x32 + x28 + x27 + x26 + x25 + x23 + x22 + x20 + x19 + x18 + x14 + x13 + x11 + x10 + x9 + x8 + x6 + 1
2570
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
    53
2746
d9d60eda74b3 changed:
Claus Gittinger <cg@exept.de>
parents: 2705
diff changeset
    54
    Only use CRC to protect against communication errors;
2310
c6f63285e4c8 changed: #documentation
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
    55
    do NOT use CRC for cryptography - use SHA1Stream or MD5Stream instead.
c6f63285e4c8 changed: #documentation
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
    56
2963
Claus Gittinger <cg@exept.de>
parents: 2962
diff changeset
    57
    Notice that this CRC is also used with PNG images - therefore, its performance
2310
c6f63285e4c8 changed: #documentation
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
    58
    directly affects png image processing.
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    59
2306
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
    60
    throughput:
4238
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
    61
        220 Mb/s on MacBook Pro (2.6Ghz I7)
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
    62
        157000 Kb/s on a 2.5Ghz 64X2 Athlon 4800+ (64bit)
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
    63
        150000 Kb/s on 2Ghz Duo
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    64
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    65
    [author:]
4238
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
    66
        Stefan Vogel (stefan@zwerg)
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    67
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    68
    [instance variables:]
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    69
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    70
    [class variables:]
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    71
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    72
    [see also:]
4238
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
    73
        SHA1Stream
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
    74
        MD5Stream
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    75
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    76
"
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    77
!
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    78
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    79
examples
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    80
"
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    81
2306
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
    82
  expect 60C1D0A0
4238
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
    83
                                                                [exBegin]
1977
d4a04a73f200 Speed up ByteArray computation
Stefan Vogel <sv@exept.de>
parents: 1976
diff changeset
    84
    self information:(CRC32Stream hashValueOf:'resume') hexPrintString
4238
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
    85
                                                                [exEnd]
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    86
2306
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
    87
  expect 16r60C1D0A0
4238
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
    88
                                                                [exBegin]
1978
f7330488b12c changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents: 1977
diff changeset
    89
    self information:(CRC32Stream new
4238
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
    90
                            nextPut:$r;
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
    91
                            nextPut:$e;
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
    92
                            nextPut:$s;
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
    93
                            nextPut:$u;
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
    94
                            nextPut:$m;
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
    95
                            nextPut:$e;
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
    96
                            hashValue) hexPrintString
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
    97
                                                                [exEnd]
1978
f7330488b12c changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents: 1977
diff changeset
    98
2306
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
    99
  expect 16r70E46888:
4238
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
   100
                                                                [exBegin]
2702
bbfd7a7031a6 only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents: 2572
diff changeset
   101
    self information:(CRC32Stream hashValueOf:#[1 2 3 4 5 6 7]) hexPrintString
4238
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
   102
                                                                [exEnd]
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   103
2306
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   104
  expect 16r8CD04C73:
4238
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
   105
                                                                [exBegin]
2963
Claus Gittinger <cg@exept.de>
parents: 2962
diff changeset
   106
    self information:((CRC32Stream hashValueOf:#[16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF
4238
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
   107
             16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
   108
             16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
   109
             16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF]) hexPrintString)
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
   110
                                                                [exEnd]
2306
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   111
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   112
  expect 16r86D7D79A:
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   113
  timing throughput:
4238
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
   114
                                                                [exBegin]
2306
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   115
    |hashStream n t|
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   116
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   117
    hashStream := CRC32Stream new.
2962
dc468681c480 class: CRC32Stream
Claus Gittinger <cg@exept.de>
parents: 2905
diff changeset
   118
    n := 1000000.
2306
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   119
    t := Time millisecondsToRun:[
4238
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
   120
            n timesRepeat:[
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
   121
                hashStream nextPutAll:'12345678901234567890123456789012345678901234567890'.
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
   122
            ].
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
   123
         ].
2306
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   124
    t := (t / 1000) asFloat.
2702
bbfd7a7031a6 only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents: 2572
diff changeset
   125
    Transcript show:'crc32:'; showCR: hashStream hashValue hexPrintString.
2306
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   126
    Transcript show:t; show:' seconds for '; show:(50*n/1024) asFloat; showCR:' Kb'.
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   127
    Transcript show:(n*50/1024 / t); showCR:' Kb/s'
4238
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
   128
                                                                [exEnd]
2306
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   129
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   130
"
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
!CRC32Stream class methodsFor:'initialization'!
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   134
2570
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   135
crcTableFor:generatorPolynomInteger
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   136
    |crcTable|
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   137
2963
Claus Gittinger <cg@exept.de>
parents: 2962
diff changeset
   138
    crcTable := (CrcTables at:generatorPolynomInteger ifAbsent:nil).
2570
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   139
    crcTable isNil ifTrue:[
2963
Claus Gittinger <cg@exept.de>
parents: 2962
diff changeset
   140
	crcTable := IntegerArray new:256.
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   141
2963
Claus Gittinger <cg@exept.de>
parents: 2962
diff changeset
   142
	0 to:255 do:[:count| |i|
Claus Gittinger <cg@exept.de>
parents: 2962
diff changeset
   143
	    i := count.
Claus Gittinger <cg@exept.de>
parents: 2962
diff changeset
   144
	    8 timesRepeat:[
Claus Gittinger <cg@exept.de>
parents: 2962
diff changeset
   145
		(i bitTest:1) ifTrue:[
Claus Gittinger <cg@exept.de>
parents: 2962
diff changeset
   146
		    i := generatorPolynomInteger bitXor:(i bitShift:-1)
Claus Gittinger <cg@exept.de>
parents: 2962
diff changeset
   147
		] ifFalse:[
Claus Gittinger <cg@exept.de>
parents: 2962
diff changeset
   148
		    i := i bitShift:-1
Claus Gittinger <cg@exept.de>
parents: 2962
diff changeset
   149
		]
Claus Gittinger <cg@exept.de>
parents: 2962
diff changeset
   150
	    ].
Claus Gittinger <cg@exept.de>
parents: 2962
diff changeset
   151
	    crcTable at:count+1 put:i.
Claus Gittinger <cg@exept.de>
parents: 2962
diff changeset
   152
	].
Claus Gittinger <cg@exept.de>
parents: 2962
diff changeset
   153
	CrcTables at:generatorPolynomInteger put:crcTable.
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   154
    ].
2570
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   155
    ^ crcTable.
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   156
!
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
initialize
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   159
    CrcTables := Dictionary new.
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   160
! !
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
!CRC32Stream class methodsFor:'instance creation'!
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   163
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   164
generatorPolynom:anInteger
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   165
    ^ self basicNew generatorPolynom:anInteger
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   166
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   167
    "
2570
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   168
       self assert:((self generatorPolynom:16r82F63B78)
2963
Claus Gittinger <cg@exept.de>
parents: 2962
diff changeset
   169
				nextPut:'123456789';
Claus Gittinger <cg@exept.de>
parents: 2962
diff changeset
   170
				hashValue)    = 16rE3069283
2570
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   171
    "
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
2572
4d6f4419b6b5 added: #newCrc32c
Stefan Vogel <sv@exept.de>
parents: 2571
diff changeset
   174
newCrc32c
2570
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   175
    "return an instance of the Castagnoli CRC-32
2963
Claus Gittinger <cg@exept.de>
parents: 2962
diff changeset
   176
	x32 + x28 + x27 + x26 + x25 + x23 + x22 + x20 + x19 + x18 + x14 + x13 + x11 + x10 + x9 + x8 + x6 + 1
2570
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   177
     (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
   178
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   179
    ^ self basicNew generatorPolynom:16r82F63B78
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   180
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   181
    "
2747
c6e4b8d6d9e7 comment/format in: #newCrc32c
Claus Gittinger <cg@exept.de>
parents: 2746
diff changeset
   182
     Castagnoli crc:
2572
4d6f4419b6b5 added: #newCrc32c
Stefan Vogel <sv@exept.de>
parents: 2571
diff changeset
   183
       self assert:((self newCrc32c)
2963
Claus Gittinger <cg@exept.de>
parents: 2962
diff changeset
   184
				nextPut:'123456789';
Claus Gittinger <cg@exept.de>
parents: 2962
diff changeset
   185
				hashValue) = 3808858755. '16rE3069283'
2747
c6e4b8d6d9e7 comment/format in: #newCrc32c
Claus Gittinger <cg@exept.de>
parents: 2746
diff changeset
   186
c6e4b8d6d9e7 comment/format in: #newCrc32c
Claus Gittinger <cg@exept.de>
parents: 2746
diff changeset
   187
     default crc:
c6e4b8d6d9e7 comment/format in: #newCrc32c
Claus Gittinger <cg@exept.de>
parents: 2746
diff changeset
   188
       self assert:((self new)
2963
Claus Gittinger <cg@exept.de>
parents: 2962
diff changeset
   189
				nextPut:'123456789';
Claus Gittinger <cg@exept.de>
parents: 2962
diff changeset
   190
				hashValue) = 3421780262. '16rCBF43926'
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   191
    "
2747
c6e4b8d6d9e7 comment/format in: #newCrc32c
Claus Gittinger <cg@exept.de>
parents: 2746
diff changeset
   192
c6e4b8d6d9e7 comment/format in: #newCrc32c
Claus Gittinger <cg@exept.de>
parents: 2746
diff changeset
   193
    "Modified (comment): / 17-05-2012 / 12:48:53 / cg"
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   194
! !
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   195
2570
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   196
!CRC32Stream methodsFor:'accessing'!
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   197
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   198
generatorPolynom
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   199
    "answer the generator polynom"
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   200
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   201
    ^ generatorPolynom
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   202
!
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   203
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   204
generatorPolynom:anInteger
2571
12d5e280eea1 comment/format in: #generatorPolynom:
Stefan Vogel <sv@exept.de>
parents: 2570
diff changeset
   205
    "set the generator polynom for this instance.
12d5e280eea1 comment/format in: #generatorPolynom:
Stefan Vogel <sv@exept.de>
parents: 2570
diff changeset
   206
     Note: you have to set the bit-reversed value, so the LSB must be first"
12d5e280eea1 comment/format in: #generatorPolynom:
Stefan Vogel <sv@exept.de>
parents: 2570
diff changeset
   207
2570
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   208
    generatorPolynom := anInteger.
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   209
    crc := 16rFFFFFFFF.
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   210
    crcTable := self class crcTableFor:generatorPolynom.
2705
47bcf29473ad added: #reset
Claus Gittinger <cg@exept.de>
parents: 2704
diff changeset
   211
!
47bcf29473ad added: #reset
Claus Gittinger <cg@exept.de>
parents: 2704
diff changeset
   212
47bcf29473ad added: #reset
Claus Gittinger <cg@exept.de>
parents: 2704
diff changeset
   213
reset
47bcf29473ad added: #reset
Claus Gittinger <cg@exept.de>
parents: 2704
diff changeset
   214
    "reset the current crc value"
47bcf29473ad added: #reset
Claus Gittinger <cg@exept.de>
parents: 2704
diff changeset
   215
47bcf29473ad added: #reset
Claus Gittinger <cg@exept.de>
parents: 2704
diff changeset
   216
    crc := 16rFFFFFFFF.
47bcf29473ad added: #reset
Claus Gittinger <cg@exept.de>
parents: 2704
diff changeset
   217
47bcf29473ad added: #reset
Claus Gittinger <cg@exept.de>
parents: 2704
diff changeset
   218
    "Created: / 12-01-2012 / 12:23:03 / cg"
2570
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   219
! !
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   220
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   221
!CRC32Stream methodsFor:'initialization'!
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   222
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   223
initialize
2570
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   224
    "initialize the CRC to CRC-32 ITU-T:
2963
Claus Gittinger <cg@exept.de>
parents: 2962
diff changeset
   225
	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
   226
2570
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   227
    self generatorPolynom:16rEDB88320
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   228
! !
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   229
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   230
!CRC32Stream methodsFor:'queries'!
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   231
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   232
hashValue
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   233
    "return the computed CRC"
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   234
3024
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   235
    ^ crc bitXor:16rFFFFFFFF.
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   236
! !
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   237
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   238
!CRC32Stream methodsFor:'writing'!
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   239
2702
bbfd7a7031a6 only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents: 2572
diff changeset
   240
nextPutBytes:count from:anObject startingAt:start
bbfd7a7031a6 only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents: 2572
diff changeset
   241
    "add the hash of anObject to the computed hash so far."
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   242
2306
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   243
%{
2702
bbfd7a7031a6 only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents: 2572
diff changeset
   244
    if (__bothSmallInteger(count, start)) {
3024
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   245
        int len, offs;
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   246
        int objSize;
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   247
        unsigned char *extPtr;
2306
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   248
3024
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   249
        len = __intVal(count);
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   250
        offs = __intVal(start) - 1;
2702
bbfd7a7031a6 only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents: 2572
diff changeset
   251
3024
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   252
        if (__isExternalBytesLike(anObject)) {
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   253
            OBJ sz = __externalBytesSize(anObject);
2306
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   254
3024
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   255
            extPtr = (unsigned char *)__externalBytesAddress(anObject);
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   256
            if (__isSmallInteger(sz)) {
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   257
                objSize = __intVal(sz);
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   258
            } else {
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   259
                objSize = 0; /* unknown */
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   260
            }
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   261
        } else {
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   262
            int nInstVars, nInstBytes;
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   263
            OBJ oClass = __Class(anObject);
2307
213162f0a57e changed:
Claus Gittinger <cg@exept.de>
parents: 2306
diff changeset
   264
3024
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   265
            switch (__intVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) {
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   266
                case BYTEARRAY:
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   267
                case WORDARRAY:
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   268
                case LONGARRAY:
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   269
                case SWORDARRAY:
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   270
                case SLONGARRAY:
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   271
                case FLOATARRAY:
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   272
                case DOUBLEARRAY:
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   273
                    break;
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   274
                default:
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   275
                    goto bad;
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   276
            }
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   277
            nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars);
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   278
            nInstBytes = __OBJS2BYTES__(nInstVars);
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   279
            // nInstBytes is the number of bytes occupied by pointer instance variables
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   280
            // subtract from size and add to byte-pointer
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   281
            objSize = __qSize(anObject) - OHDR_SIZE - nInstBytes;
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   282
            extPtr = (unsigned char *)__byteArrayVal(anObject)+nInstBytes;
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   283
        }
2702
bbfd7a7031a6 only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents: 2572
diff changeset
   284
3024
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   285
        if ((offs >= 0) && (len >= 0) && (objSize >= (len + offs))) {
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   286
            unsigned int _crc;
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   287
            unsigned int *_crcTable = __integerArrayVal( __INST(crcTable) );
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   288
            unsigned char *cp = extPtr+offs;
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   289
            unsigned int n = len;
2306
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   290
3024
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   291
            if (sizeof(INT) == 8 || __isSmallInteger(__INST(crc)) ) {
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   292
                _crc = (unsigned int) (__intVal( __INST(crc) ));
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   293
            } else {
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   294
                _crc = __unsignedLongIntVal( __INST(crc) );
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   295
            }
2702
bbfd7a7031a6 only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents: 2572
diff changeset
   296
2746
d9d60eda74b3 changed:
Claus Gittinger <cg@exept.de>
parents: 2705
diff changeset
   297
#ifdef __LSBFIRST__
3024
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   298
            if (((unsigned INT)cp & 3) == 0) {
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   299
                // word aligned
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   300
                for ( ; n >= 4 ; n -= 4, cp += 4) {
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   301
                    unsigned int word;
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   302
                    unsigned char _idx;
2746
d9d60eda74b3 changed:
Claus Gittinger <cg@exept.de>
parents: 2705
diff changeset
   303
3024
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   304
                    word = ((unsigned int *)cp)[0];
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   305
                    _idx = (_crc ^ word) & 0xFF;
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   306
                    _crc = _crcTable[_idx] ^ (_crc >> 8);
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   307
                    _idx = (_crc ^ (word>>8)) & 0xFF;
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   308
                    _crc = _crcTable[_idx] ^ (_crc >> 8);
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   309
                    _idx = (_crc ^ (word>>16)) & 0xFF;
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   310
                    _crc = _crcTable[_idx] ^ (_crc >> 8);
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   311
                    _idx = (_crc ^ (word>>24)) & 0xFF;
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   312
                    _crc = _crcTable[_idx] ^ (_crc >> 8);
2746
d9d60eda74b3 changed:
Claus Gittinger <cg@exept.de>
parents: 2705
diff changeset
   313
                }
3024
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   314
            }
2746
d9d60eda74b3 changed:
Claus Gittinger <cg@exept.de>
parents: 2705
diff changeset
   315
#endif
3024
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   316
            for ( ; n >= 4 ; n -= 4, cp += 4) {
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   317
                unsigned char _idx;
2702
bbfd7a7031a6 only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents: 2572
diff changeset
   318
3024
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   319
                _idx = (_crc ^ cp[0]) & 0xFF;
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   320
                _crc = _crcTable[_idx] ^ (_crc >> 8);
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   321
                _idx = (_crc ^ cp[1]) & 0xFF;
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   322
                _crc = _crcTable[_idx] ^ (_crc >> 8);
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   323
                _idx = (_crc ^ cp[2]) & 0xFF;
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   324
                _crc = _crcTable[_idx] ^ (_crc >> 8);
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   325
                _idx = (_crc ^ cp[3]) & 0xFF;
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   326
                _crc = _crcTable[_idx] ^ (_crc >> 8);
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   327
            }
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   328
            while (n-- > 0) {
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   329
                unsigned char _idx = (_crc ^ *cp++) & 0xFF;
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   330
                _crc = _crcTable[_idx] ^ (_crc >> 8);
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   331
            }
2702
bbfd7a7031a6 only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents: 2572
diff changeset
   332
3024
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   333
            if (sizeof(INT) == 8 || _crc <= _MAX_INT)
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   334
                __INST(crc) = __MKSMALLINT(_crc);
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   335
            else {
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   336
                // this code fails with gcc 4.7.2:
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   337
                // __INST(crc) = __MKUINT(_crc); __STORESELF(crc);
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   338
                OBJ temp = __MKUINT(_crc); 
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   339
                __INST(crc) = temp; __STORESELF(crc);
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   340
            }
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   341
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   342
            // if (__unsignedLongIntVal(__INST(crc)) == 4294967295) printf("inst: %d crc: %d new: %d\n", __unsignedLongIntVal(__INST(crc)), _crc, __unsignedLongIntVal(temp));
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   343
            RETURN (count);
c7022e1963b0 class: CRC32Stream
Stefan Vogel <sv@exept.de>
parents: 2963
diff changeset
   344
        }
2306
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   345
    }
2702
bbfd7a7031a6 only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents: 2572
diff changeset
   346
bad: ;
2306
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   347
%}.
2702
bbfd7a7031a6 only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents: 2572
diff changeset
   348
    self error:'invalid argument'
1977
d4a04a73f200 Speed up ByteArray computation
Stefan Vogel <sv@exept.de>
parents: 1976
diff changeset
   349
2702
bbfd7a7031a6 only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents: 2572
diff changeset
   350
    "Created: / 09-01-2012 / 16:48:35 / cg"
1192
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
!CRC32Stream class methodsFor:'documentation'!
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   354
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   355
version
4238
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
   356
    ^ '$Header$'
2306
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   357
!
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   358
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   359
version_CVS
4238
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
   360
    ^ '$Header$'
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   361
! !
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   362
2905
73332f14d929 class: CRC32Stream
Claus Gittinger <cg@exept.de>
parents: 2747
diff changeset
   363
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   364
CRC32Stream initialize!