CRC32Stream.st
author Claus Gittinger <cg@exept.de>
Sat, 16 Mar 2019 23:30:01 +0100
changeset 4857 42f77137031b
parent 4847 c76689ab85bf
child 4864 847aab873d88
permissions -rw-r--r--
#QUALITY by cg class: CRC32Stream class added: #newCCITT changed: #new
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4845
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
     1
"{ Encoding: utf8 }"
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
     2
2416
df3f9f02c8d8 added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2334
diff changeset
     3
"
df3f9f02c8d8 added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2334
diff changeset
     4
 COPYRIGHT (c) 2003 by eXept Software AG
2963
Claus Gittinger <cg@exept.de>
parents: 2962
diff changeset
     5
	      All Rights Reserved
2416
df3f9f02c8d8 added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2334
diff changeset
     6
df3f9f02c8d8 added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2334
diff changeset
     7
 This software is furnished under a license and may be used
df3f9f02c8d8 added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2334
diff changeset
     8
 only in accordance with the terms of that license and with the
df3f9f02c8d8 added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2334
diff changeset
     9
 inclusion of the above copyright notice.   This software may not
df3f9f02c8d8 added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2334
diff changeset
    10
 be provided or otherwise made available to, or used by, any
df3f9f02c8d8 added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2334
diff changeset
    11
 other person.  No title to or ownership of the software is
df3f9f02c8d8 added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2334
diff changeset
    12
 hereby transferred.
df3f9f02c8d8 added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2334
diff changeset
    13
"
1975
0850a3e0d484 Fix examples
Stefan Vogel <sv@exept.de>
parents: 1192
diff changeset
    14
"{ Package: 'stx:libbasic2' }"
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    15
4238
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
    16
"{ NameSpace: Smalltalk }"
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
    17
4845
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
    18
CRCStream subclass:#CRC32Stream
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
    19
	instanceVariableNames:''
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
    20
	classVariableNames:''
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    21
	poolDictionaries:''
2324
1f5b2b5500e4 category changes
Claus Gittinger <cg@exept.de>
parents: 2310
diff changeset
    22
	category:'System-Crypt-Hashing'
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    23
!
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    24
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    25
!CRC32Stream class methodsFor:'documentation'!
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    26
2416
df3f9f02c8d8 added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2334
diff changeset
    27
copyright
df3f9f02c8d8 added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2334
diff changeset
    28
"
df3f9f02c8d8 added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2334
diff changeset
    29
 COPYRIGHT (c) 2003 by eXept Software AG
2963
Claus Gittinger <cg@exept.de>
parents: 2962
diff changeset
    30
	      All Rights Reserved
2416
df3f9f02c8d8 added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2334
diff changeset
    31
df3f9f02c8d8 added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2334
diff changeset
    32
 This software is furnished under a license and may be used
df3f9f02c8d8 added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2334
diff changeset
    33
 only in accordance with the terms of that license and with the
df3f9f02c8d8 added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2334
diff changeset
    34
 inclusion of the above copyright notice.   This software may not
df3f9f02c8d8 added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2334
diff changeset
    35
 be provided or otherwise made available to, or used by, any
df3f9f02c8d8 added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2334
diff changeset
    36
 other person.  No title to or ownership of the software is
df3f9f02c8d8 added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2334
diff changeset
    37
 hereby transferred.
df3f9f02c8d8 added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2334
diff changeset
    38
"
df3f9f02c8d8 added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2334
diff changeset
    39
!
df3f9f02c8d8 added: #copyright
Claus Gittinger <cg@exept.de>
parents: 2334
diff changeset
    40
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    41
documentation
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    42
"
2963
Claus Gittinger <cg@exept.de>
parents: 2962
diff changeset
    43
    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
    44
    The default CRC polynomial employed is
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    45
4238
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
    46
        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
    47
        (or 16r04C11DB7)
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    48
4845
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
    49
    You can also create an instace performing the Castagnioli CRC-32C 
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
    50
    (used in iSCSI & SCTP [RFC3720], G.hn payload, SSE4.2):
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
        self newCrc32c
2570
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
    53
4845
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
    54
        poly: 16r1edc6f41
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
    55
        = 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
    56
2746
d9d60eda74b3 changed:
Claus Gittinger <cg@exept.de>
parents: 2705
diff changeset
    57
    Only use CRC to protect against communication errors;
2310
c6f63285e4c8 changed: #documentation
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
    58
    do NOT use CRC for cryptography - use SHA1Stream or MD5Stream instead.
c6f63285e4c8 changed: #documentation
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
    59
2963
Claus Gittinger <cg@exept.de>
parents: 2962
diff changeset
    60
    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
    61
    directly affects png image processing.
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    62
2306
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
    63
    throughput:
4238
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
    64
        220 Mb/s on MacBook Pro (2.6Ghz I7)
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
    65
        157000 Kb/s on a 2.5Ghz 64X2 Athlon 4800+ (64bit)
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
    66
        150000 Kb/s on 2Ghz Duo
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
    [author:]
4238
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
    69
        Stefan Vogel (stefan@zwerg)
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    70
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    71
    [instance variables:]
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    72
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    73
    [class variables:]
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    74
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    75
    [see also:]
4238
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
    76
        SHA1Stream
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
    77
        MD5Stream
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    78
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    79
"
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    80
!
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    81
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    82
examples
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    83
"
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    84
2306
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
    85
  expect 60C1D0A0
4238
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
    86
                                                                [exBegin]
1977
d4a04a73f200 Speed up ByteArray computation
Stefan Vogel <sv@exept.de>
parents: 1976
diff changeset
    87
    self information:(CRC32Stream hashValueOf:'resume') hexPrintString
4238
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
    88
                                                                [exEnd]
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
    89
2306
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
    90
  expect 16r60C1D0A0
4238
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
    91
                                                                [exBegin]
1978
f7330488b12c changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents: 1977
diff changeset
    92
    self information:(CRC32Stream new
4238
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
    93
                            nextPut:$r;
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
    94
                            nextPut:$e;
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
    95
                            nextPut:$s;
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
    96
                            nextPut:$u;
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
    97
                            nextPut:$m;
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
    98
                            nextPut:$e;
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
    99
                            hashValue) hexPrintString
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
   100
                                                                [exEnd]
1978
f7330488b12c changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents: 1977
diff changeset
   101
2306
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   102
  expect 16r70E46888:
4238
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
   103
                                                                [exBegin]
2702
bbfd7a7031a6 only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents: 2572
diff changeset
   104
    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
   105
                                                                [exEnd]
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   106
2306
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   107
  expect 16r8CD04C73:
4238
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
   108
                                                                [exBegin]
2963
Claus Gittinger <cg@exept.de>
parents: 2962
diff changeset
   109
    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
   110
             16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
   111
             16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
   112
             16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF]) hexPrintString)
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
   113
                                                                [exEnd]
2306
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   114
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   115
  timing throughput:
4238
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
   116
                                                                [exBegin]
2306
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   117
    |hashStream n t|
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   118
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   119
    hashStream := CRC32Stream new.
2962
dc468681c480 class: CRC32Stream
Claus Gittinger <cg@exept.de>
parents: 2905
diff changeset
   120
    n := 1000000.
2306
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   121
    t := Time millisecondsToRun:[
4238
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
   122
            n timesRepeat:[
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
   123
                hashStream nextPutAll:'12345678901234567890123456789012345678901234567890'.
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
   124
            ].
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
   125
         ].
2306
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   126
    t := (t / 1000) asFloat.
2702
bbfd7a7031a6 only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents: 2572
diff changeset
   127
    Transcript show:'crc32:'; showCR: hashStream hashValue hexPrintString.
2306
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   128
    Transcript show:t; show:' seconds for '; show:(50*n/1024) asFloat; showCR:' Kb'.
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   129
    Transcript show:(n*50/1024 / t); showCR:' Kb/s'
4238
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
   130
                                                                [exEnd]
2306
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   131
4845
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   132
  test vectors from https://tools.ietf.org/html/rfc3720#page-217:
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   133
  
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   134
  expect 0
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   135
                                                                [exBegin]
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   136
    self information:(CRC32Stream newCrc32c hashValueOf:'') hexPrintString
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   137
                                                                [exEnd]
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   138
  expect C1D04330
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   139
                                                                [exBegin]
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   140
    self information:(CRC32Stream newCrc32c hashValueOf:'a') hexPrintString
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   141
                                                                [exEnd]
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   142
  expect E3069283
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   143
                                                                [exBegin]
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   144
    self information:(CRC32Stream newCrc32c hashValueOf:'123456789') hexPrintString
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   145
                                                                [exEnd]
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   146
  expect 8A9136AA
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   147
                                                                [exBegin]
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   148
    self information:(CRC32Stream newCrc32c hashValueOf:(ByteArray new:32 withAll:0)) hexPrintString
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   149
                                                                [exEnd]
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   150
  expect 62a8ab43
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   151
                                                                [exBegin]
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   152
    self information:(CRC32Stream newCrc32c hashValueOf:(ByteArray new:32 withAll:16rFF)) hexPrintString
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   153
                                                                [exEnd]
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   154
  expect 46dd794e
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   155
                                                                [exBegin]
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   156
    self information:(CRC32Stream newCrc32c hashValueOf:(0 to:31) asByteArray) hexPrintString
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   157
                                                                [exEnd]
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   158
4845
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   159
"
2570
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
4845
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   164
generatorPolynomMSB:anInteger
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   165
    "notice, in literature, the generator polynom is usually specified as an MSB number"
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   166
    
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   167
    ^ self generatorPolynom:(anInteger bitReversed32)
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   168
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   169
    "
2570
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   170
       self assert:((self generatorPolynom:16r82F63B78)
4845
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   171
                                nextPut:'123456789';
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   172
                                hashValue)    = 16rE3069283
2570
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   173
    "
4845
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   174
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   175
    "Created: / 16-03-2019 / 20:55:46 / Claus Gittinger"
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   176
!
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   177
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   178
generatorPolynomMSB:anInteger initValue:initValueArg
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   179
    "notice, in literature, the generator polynom is usually specified as an MSB number"
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   180
    
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   181
    ^ self generatorPolynom:(anInteger bitReversed32) initValue:initValueArg
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   182
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   183
    "Created: / 16-03-2019 / 21:11:31 / Claus Gittinger"
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   184
!
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   185
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   186
generatorPolynomMSB:anInteger initValue:initValueArg xorOut:xorOut
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   187
    "notice, in literature, the generator polynom is usually specified as an MSB number"
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   188
    
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   189
    ^ self generatorPolynom:(anInteger bitReversed32) initValue:initValueArg xorOut:xorOut
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   190
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   191
    "Created: / 16-03-2019 / 21:21:27 / Claus Gittinger"
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   192
!
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   193
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   194
new
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   195
    "return an instance of the ITU-T CRC-32
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   196
        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"
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   197
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   198
    "/ 16r4C11DB7 bitReversed32 -> 16rEDB88320
4857
42f77137031b #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4847
diff changeset
   199
    ^ self newCCITT
42f77137031b #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4847
diff changeset
   200
42f77137031b #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4847
diff changeset
   201
    "Created: / 16-03-2019 / 21:09:19 / Claus Gittinger"
42f77137031b #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4847
diff changeset
   202
    "Modified: / 16-03-2019 / 23:29:45 / Claus Gittinger"
42f77137031b #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4847
diff changeset
   203
!
42f77137031b #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4847
diff changeset
   204
42f77137031b #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4847
diff changeset
   205
newCCITT
42f77137031b #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4847
diff changeset
   206
    "return an instance of the ITU-T CRC-32
42f77137031b #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4847
diff changeset
   207
        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"
42f77137031b #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4847
diff changeset
   208
42f77137031b #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4847
diff changeset
   209
    "/ 16r4C11DB7 bitReversed32 -> 16rEDB88320
4845
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   210
    ^ self generatorPolynom:16rEDB88320 initValue:16rFFFFFFFF xorOut:16rFFFFFFFF
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   211
4857
42f77137031b #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4847
diff changeset
   212
    "Created: / 16-03-2019 / 23:29:39 / Claus Gittinger"
2570
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   213
!
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   214
2572
4d6f4419b6b5 added: #newCrc32c
Stefan Vogel <sv@exept.de>
parents: 2571
diff changeset
   215
newCrc32c
2570
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   216
    "return an instance of the Castagnoli CRC-32
4845
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   217
        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
   218
     (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
   219
4845
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   220
    "/ 16r1edc6f41 bitReversed32 -> 16r82F63B78
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   221
    ^ self generatorPolynom:16r82F63B78 initValue:16rFFFFFFFF xorOut:16rFFFFFFFF
2570
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
    "
2747
c6e4b8d6d9e7 comment/format in: #newCrc32c
Claus Gittinger <cg@exept.de>
parents: 2746
diff changeset
   224
     Castagnoli crc:
2572
4d6f4419b6b5 added: #newCrc32c
Stefan Vogel <sv@exept.de>
parents: 2571
diff changeset
   225
       self assert:((self newCrc32c)
4845
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   226
                                nextPut:'123456789';
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   227
                                hashValue) = 3808858755. '16rE3069283'
2747
c6e4b8d6d9e7 comment/format in: #newCrc32c
Claus Gittinger <cg@exept.de>
parents: 2746
diff changeset
   228
c6e4b8d6d9e7 comment/format in: #newCrc32c
Claus Gittinger <cg@exept.de>
parents: 2746
diff changeset
   229
     default crc:
c6e4b8d6d9e7 comment/format in: #newCrc32c
Claus Gittinger <cg@exept.de>
parents: 2746
diff changeset
   230
       self assert:((self new)
4845
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   231
                                nextPut:'123456789';
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   232
                                hashValue) = 3421780262. '16rCBF43926'
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   233
    "
2747
c6e4b8d6d9e7 comment/format in: #newCrc32c
Claus Gittinger <cg@exept.de>
parents: 2746
diff changeset
   234
c6e4b8d6d9e7 comment/format in: #newCrc32c
Claus Gittinger <cg@exept.de>
parents: 2746
diff changeset
   235
    "Modified (comment): / 17-05-2012 / 12:48:53 / cg"
4845
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   236
    "Modified: / 16-03-2019 / 21:21:07 / Claus Gittinger"
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   237
! !
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   238
4845
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   239
!CRC32Stream methodsFor:'initialization'!
2570
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   240
55ce3e8bd7dc Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents: 2569
diff changeset
   241
generatorPolynom:anInteger
2571
12d5e280eea1 comment/format in: #generatorPolynom:
Stefan Vogel <sv@exept.de>
parents: 2570
diff changeset
   242
    "set the generator polynom for this instance.
12d5e280eea1 comment/format in: #generatorPolynom:
Stefan Vogel <sv@exept.de>
parents: 2570
diff changeset
   243
     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
   244
4845
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   245
    self generatorPolynom:anInteger initValue:16rFFFFFFFF xorOut:16rFFFFFFFF.
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   246
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   247
    "Modified: / 16-03-2019 / 21:23:25 / Claus Gittinger"
2705
47bcf29473ad added: #reset
Claus Gittinger <cg@exept.de>
parents: 2704
diff changeset
   248
!
47bcf29473ad added: #reset
Claus Gittinger <cg@exept.de>
parents: 2704
diff changeset
   249
4845
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   250
generatorPolynom:anInteger initValue:initValueArg
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   251
    "set the generator polynom for this instance.
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   252
     Note: you have to set the bit-reversed value, so the LSB must be first"
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   253
4845
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   254
    self generatorPolynom:anInteger initValue:initValueArg xorOut:16rFFFFFFFF
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   255
4845
36770d6f14bf #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4238
diff changeset
   256
    "Created: / 16-03-2019 / 21:06:16 / Claus Gittinger"
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   257
! !
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   258
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   259
!CRC32Stream class methodsFor:'documentation'!
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   260
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   261
version
4238
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
   262
    ^ '$Header$'
2306
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   263
!
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   264
75d9592b9d8a changed:
Claus Gittinger <cg@exept.de>
parents: 1978
diff changeset
   265
version_CVS
4238
ab8390ef187a #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3024
diff changeset
   266
    ^ '$Header$'
1192
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   267
! !
27c5d5ea5969 initial checkin
Stefan Vogel <sv@exept.de>
parents:
diff changeset
   268