Tcl Library Source Code

Check-in [a061c12325]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:crc -- crc16 Applied crc pseudo-patch provided in mail on the tcllib-devel mailing list. Created the missing docs and test cases. Bumped to version 1.1.5. Thanks to [email protected]
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: a061c1232563121637413e428c1083b553d77d336ead3e5b7214d5741e571ae6
User & Date: aku 2023-11-08 20:37:25
Context
2023-11-14
08:20
Integrated Rolf's work to make Tcllib ready for Tcl 9+ into the trunk. Thank you very much. check-in: 758418619e user: aku tags: trunk
2023-11-12
12:15
Merged from trunk. Closed-Leaf check-in: fa334d36e4 user: aku tags: tcl9-testarea-rde
2023-11-08
20:37
crc -- crc16 Applied crc pseudo-patch provided in mail on the tcllib-devel mailing list. Created the missing docs and test cases. Bumped to version 1.1.5. Thanks to [email protected] check-in: a061c12325 user: aku tags: trunk
20:37
Merged the doctools html work provided by Gustaf Neumann check-in: 84249cfc51 user: aku tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to modules/crc/crc16.man.

1
2
3
4
5
6
7
8
[vset VERSION 1.1.4]
[manpage_begin crc16 n [vset VERSION]]
[see_also cksum(n)]
[see_also crc32(n)]
[see_also sum(n)]
[keywords checksum]
[keywords cksum]
[keywords crc]
|







1
2
3
4
5
6
7
8
[vset VERSION 1.1.5]
[manpage_begin crc16 n [vset VERSION]]
[see_also cksum(n)]
[see_also crc32(n)]
[see_also sum(n)]
[keywords checksum]
[keywords cksum]
[keywords crc]
39
40
41
42
43
44
45




























































46
47
48
49
50
51
52
[call [cmd ::crc::crc-ccitt] [opt "-format [arg format]"] \
  [opt "-seed [arg value]"] [opt "-implementation [arg procname]"] [const --] [arg message]]
[call [cmd ::crc::crc-ccitt] [opt "-format [arg format]"] \
  [opt "-seed [arg value]"] [opt "-implementation [arg procname]"] "-filename [arg file]"]
[call [cmd ::crc::xmodem] [opt "-format [arg format]"] \
  [opt "-seed [arg value]"] [opt "-implementation [arg procname]"] [const --] [arg message]]
[call [cmd ::crc::xmodem] [opt "-format [arg format]"] \




























































  [opt "-seed [arg value]"] [opt "-implementation [arg procname]"] "-filename [arg file]"]

The command takes either string data or a file name and returns a checksum
value calculated using the CRC algorithm. The command used sets up the
CRC polynomial, initial value and bit ordering for the desired
standard checksum calculation. The result is formatted
using the [arg format](n) specifier provided or as an unsigned integer







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
[call [cmd ::crc::crc-ccitt] [opt "-format [arg format]"] \
  [opt "-seed [arg value]"] [opt "-implementation [arg procname]"] [const --] [arg message]]
[call [cmd ::crc::crc-ccitt] [opt "-format [arg format]"] \
  [opt "-seed [arg value]"] [opt "-implementation [arg procname]"] "-filename [arg file]"]
[call [cmd ::crc::xmodem] [opt "-format [arg format]"] \
  [opt "-seed [arg value]"] [opt "-implementation [arg procname]"] [const --] [arg message]]
[call [cmd ::crc::xmodem] [opt "-format [arg format]"] \
  [opt "-seed [arg value]"] [opt "-implementation [arg procname]"] "-filename [arg file]"]
[call [cmd ::crc::kermit] [opt "-format [arg format]"] \
  [opt "-seed [arg value]"] [opt "-implementation [arg procname]"] [const --] [arg message]]
[call [cmd ::crc::kermit] [opt "-format [arg format]"] \
  [opt "-seed [arg value]"] [opt "-implementation [arg procname]"] "-filename [arg file]"]
[call [cmd ::crc::modbus] [opt "-format [arg format]"] \
  [opt "-seed [arg value]"] [opt "-implementation [arg procname]"] [const --] [arg message]]
[call [cmd ::crc::modbus] [opt "-format [arg format]"] \
  [opt "-seed [arg value]"] [opt "-implementation [arg procname]"] "-filename [arg file]"]
[call [cmd ::crc::mcrf4xx] [opt "-format [arg format]"] \
  [opt "-seed [arg value]"] [opt "-implementation [arg procname]"] [const --] [arg message]]
[call [cmd ::crc::mcrf4xx] [opt "-format [arg format]"] \
  [opt "-seed [arg value]"] [opt "-implementation [arg procname]"] "-filename [arg file]"]
[call [cmd ::crc::genibus] [opt "-format [arg format]"] \
  [opt "-seed [arg value]"] [opt "-implementation [arg procname]"] [const --] [arg message]]
[call [cmd ::crc::genibus] [opt "-format [arg format]"] \
  [opt "-seed [arg value]"] [opt "-implementation [arg procname]"] "-filename [arg file]"]
[call [cmd ::crc::crc-x25] [opt "-format [arg format]"] \
  [opt "-seed [arg value]"] [opt "-implementation [arg procname]"] [const --] [arg message]]
[call [cmd ::crc::crc-x25] [opt "-format [arg format]"] \
  [opt "-seed [arg value]"] [opt "-implementation [arg procname]"] "-filename [arg file]"]
[call [cmd ::crc::crc-sdlc] [opt "-format [arg format]"] \
  [opt "-seed [arg value]"] [opt "-implementation [arg procname]"] [const --] [arg message]]
[call [cmd ::crc::crc-sdlc] [opt "-format [arg format]"] \
  [opt "-seed [arg value]"] [opt "-implementation [arg procname]"] "-filename [arg file]"]
[call [cmd ::crc::crc-usb] [opt "-format [arg format]"] \
  [opt "-seed [arg value]"] [opt "-implementation [arg procname]"] [const --] [arg message]]
[call [cmd ::crc::crc-usb] [opt "-format [arg format]"] \
  [opt "-seed [arg value]"] [opt "-implementation [arg procname]"] "-filename [arg file]"]
[call [cmd ::crc::buypass] [opt "-format [arg format]"] \
  [opt "-seed [arg value]"] [opt "-implementation [arg procname]"] [const --] [arg message]]
[call [cmd ::crc::buypass] [opt "-format [arg format]"] \
  [opt "-seed [arg value]"] [opt "-implementation [arg procname]"] "-filename [arg file]"]
[call [cmd ::crc::umts] [opt "-format [arg format]"] \
  [opt "-seed [arg value]"] [opt "-implementation [arg procname]"] [const --] [arg message]]
[call [cmd ::crc::umts] [opt "-format [arg format]"] \
  [opt "-seed [arg value]"] [opt "-implementation [arg procname]"] "-filename [arg file]"]
[call [cmd ::crc::gsm] [opt "-format [arg format]"] \
  [opt "-seed [arg value]"] [opt "-implementation [arg procname]"] [const --] [arg message]]
[call [cmd ::crc::gsm] [opt "-format [arg format]"] \
  [opt "-seed [arg value]"] [opt "-implementation [arg procname]"] "-filename [arg file]"]
[call [cmd ::crc::unknown2] [opt "-format [arg format]"] \
  [opt "-seed [arg value]"] [opt "-implementation [arg procname]"] [const --] [arg message]]
[call [cmd ::crc::unknown2] [opt "-format [arg format]"] \
  [opt "-seed [arg value]"] [opt "-implementation [arg procname]"] "-filename [arg file]"]
[call [cmd ::crc::maxim] [opt "-format [arg format]"] \
  [opt "-seed [arg value]"] [opt "-implementation [arg procname]"] [const --] [arg message]]
[call [cmd ::crc::maxim] [opt "-format [arg format]"] \
  [opt "-seed [arg value]"] [opt "-implementation [arg procname]"] "-filename [arg file]"]
[call [cmd ::crc::unknown3] [opt "-format [arg format]"] \
  [opt "-seed [arg value]"] [opt "-implementation [arg procname]"] [const --] [arg message]]
[call [cmd ::crc::unknown3] [opt "-format [arg format]"] \
  [opt "-seed [arg value]"] [opt "-implementation [arg procname]"] "-filename [arg file]"]
[call [cmd ::crc::unknown4] [opt "-format [arg format]"] \
  [opt "-seed [arg value]"] [opt "-implementation [arg procname]"] [const --] [arg message]]
[call [cmd ::crc::unknown4] [opt "-format [arg format]"] \
  [opt "-seed [arg value]"] [opt "-implementation [arg procname]"] "-filename [arg file]"]
[call [cmd ::crc::cms] [opt "-format [arg format]"] \
  [opt "-seed [arg value]"] [opt "-implementation [arg procname]"] [const --] [arg message]]
[call [cmd ::crc::cms] [opt "-format [arg format]"] \
  [opt "-seed [arg value]"] [opt "-implementation [arg procname]"] "-filename [arg file]"]

The command takes either string data or a file name and returns a checksum
value calculated using the CRC algorithm. The command used sets up the
CRC polynomial, initial value and bit ordering for the desired
standard checksum calculation. The result is formatted
using the [arg format](n) specifier provided or as an unsigned integer

Changes to modules/crc/crc16.tcl.

14
15
16
17
18
19
20



















21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# [3] "Numerical Recipes in C", Press WH et al. Chapter 20.
#
# Checks: a crc for the string "123456789" should give:
#   CRC16:     0xBB3D
#   CRC-CCITT: 0x29B1
#   XMODEM:    0x31C3
#   CRC-32:    0xCBF43926



















#
# eg: crc::crc16 "123456789"
#     crc::crc-ccitt "123456789"
# or  crc::crc16 -file tclsh.exe
#
# Note:
#  The CCITT CRC can very easily be checked for the accuracy of transmission
#  as the CRC of the message plus the CRC values will be 0. That is:
#   % set msg {123456789]
#   % set crc [crc::crc-ccitt $msg]
#   % crc::crc-ccitt $msg[binary format S $crc]
#   0
#
#  The same is true of other CRCs but some operate in reverse bit order:
#   % crc::crc16 $msg[binary format s [crc::crc16 $msg]]
#   0







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>








|







14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# [3] "Numerical Recipes in C", Press WH et al. Chapter 20.
#
# Checks: a crc for the string "123456789" should give:
#   CRC16:     0xBB3D
#   CRC-CCITT: 0x29B1
#   XMODEM:    0x31C3
#   CRC-32:    0xCBF43926
#
# Additional CRCs from the bottom of
# http://reveng.sourceforge.net/crc-catalogue/all.htm
#
#   KERMIT:    0x2189
#   MODBUS:    0x4B37
#   MCRF4XX:   0x6F91
#   GENIBUS:   0xD64E
#   X.25:      0x906E
#   SDLC:      0x906E
#   USB:       0xB4C8
#   BUYPASS:   0xFEE8
#   UMTS:      0xFEE8		::crc::umts
#   GSM:       0xCE3C
#   UNKNOWN2:  0xDE76
#   MAXIM:     0x44C2
#   UNKNOWN3:  0x0117
#   UNKNOWN4:  0x5118
#   CMS:       0xAEE7
#
# eg: crc::crc16 "123456789"
#     crc::crc-ccitt "123456789"
# or  crc::crc16 -file tclsh.exe
#
# Note:
#  The CCITT CRC can very easily be checked for the accuracy of transmission
#  as the CRC of the message plus the CRC values will be 0. That is:
#   % set msg {123456789}
#   % set crc [crc::crc-ccitt $msg]
#   % crc::crc-ccitt $msg[binary format S $crc]
#   0
#
#  The same is true of other CRCs but some operate in reverse bit order:
#   % crc::crc16 $msg[binary format s [crc::crc16 $msg]]
#   0
45
46
47
48
49
50
51
52
53
54
55
56
57













58
59
60
61
62
63
64
package require Tcl 8.2;                # tcl minimum version

namespace eval ::crc {
    namespace export crc16 crc-ccitt crc-32

    # Standard CRC generator polynomials.
    variable polynomial
    set polynomial(crc16) [expr {(1<<16) | (1<<15) | (1<<2) | 1}]
    set polynomial(ccitt) [expr {(1<<16) | (1<<12) | (1<<5) | 1}]
    set polynomial(crc32) [expr {(1<<32) | (1<<26) | (1<<23) | (1<<22)
                                 | (1<<16) | (1<<12) | (1<<11) | (1<<10)
                                 | (1<<8) | (1<<7) | (1<<5) | (1<<4)
                                 | (1<<2) | (1<<1) | 1}]














    # Array to hold the generated tables
    variable table
    if {![info exists table]} { array set table {}}

    # calculate the sign bit for the current platform.
    variable signbit







|
|
|
|
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>







64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package require Tcl 8.2;                # tcl minimum version

namespace eval ::crc {
    namespace export crc16 crc-ccitt crc-32

    # Standard CRC generator polynomials.
    variable polynomial
    set polynomial(crc16)     [expr {(1<<16) | (1<<15) | (1<<2) | 1}]
    set polynomial(ccitt)     [expr {(1<<16) | (1<<12) | (1<<5) | 1}]
    set polynomial(crc32)     [expr {(1<<32) | (1<<26) | (1<<23) | (1<<22)
                                     | (1<<16) | (1<<12) | (1<<11) | (1<<10)
                                     | (1<<8) | (1<<7) | (1<<5) | (1<<4)
                                     | (1<<2) | (1<<1) | 1}]
    set polynomial(kermit)    [expr {(1<<16) | (1<<12) | (1<<5) | 1}]
    set polynomial(modbus)    [expr {(1<<16) | (1<<15) | (1<<2) | 1}]
    set polynomial(mcrf4xx)   [expr {(1<<16) | (1<<12) | (1<<5) | 1}]
    set polynomial(genibus)   [expr {(1<<16) | (1<<12) | (1<<5) | 1}]
    set polynomial(x25)       [expr {(1<<16) | (1<<12) | (1<<5) | 1}]
    set polynomial(usb)       [expr {(1<<16) | (1<<15) | (1<<2) | 1}]
    set polynomial(buypass)   [expr {(1<<16) | (1<<15) | (1<<2) | 1}]
    set polynomial(gsm)       [expr {(1<<16) | (1<<12) | (1<<5) | 1}]
    set polynomial(unknown2)  [expr {(1<<16) | (1<<12) | (1<<5) | 1}]
    set polynomial(maxim)     [expr {(1<<16) | (1<<15) | (1<<2) | 1}]
    set polynomial(unknown3)  [expr {(1<<16) | (1<<15) | (1<<2) | 1}]
    set polynomial(unknown4)  [expr {(1<<16) | (1<<15) | (1<<2) | 1}]
    set polynomial(cms)       [expr {(1<<16) | (1<<15) | (1<<2) | 1}]

    # Array to hold the generated tables
    variable table
    if {![info exists table]} { array set table {}}

    # calculate the sign bit for the current platform.
    variable signbit
78
79
80
81
82
83
84
85
86
87













88
89
90
91
92
93
94
# -------------------------------------------------------------------------
# Generate a CRC lookup table.
# This creates a CRC algorithm lookup table for a 'width' bits checksum
# using the 'poly' polynomial for all values of an input byte.
# Setting 'reflected' changes the bit order for input bytes.
# Returns a list or 255 elements.
#
# CRC-32:      Crc_table 32 $crc::polynomial(crc32) 1
# CRC-16:      Crc_table 16 $crc::polynomial(crc16) 1
# CRC16/CCITT: Crc_table 16 $crc::polynomial(ccitt) 0













#
proc ::crc::Crc_table {width poly reflected} {
    set tbl {}
    if {$width < 32} {
        set mask   [expr {(1 << $width) - 1}]
        set topbit [expr {1 << ($width - 1)}]
    } else {







|
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>







110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# -------------------------------------------------------------------------
# Generate a CRC lookup table.
# This creates a CRC algorithm lookup table for a 'width' bits checksum
# using the 'poly' polynomial for all values of an input byte.
# Setting 'reflected' changes the bit order for input bytes.
# Returns a list or 255 elements.
#
# CRC-32:      Crc_table 32 $crc::polynomial(crc32)    1
# CRC-16:      Crc_table 16 $crc::polynomial(crc16)    1
# CRC16/CCITT: Crc_table 16 $crc::polynomial(ccitt)    0
# KERMIT:      Crc_table 16 $crc::polynomial(kermit)   1
# MODBUS:      Crc_table 16 $crc::polynomial(modbus)   1
# MCRF4XX:     Crc_table 16 $crc::polynomial(mcrf4xx)  1
# GENIBUS:     Crc_table 16 $crc::polynomial(genibus)  0
# X.25:        Crc_table 16 $crc::polynomial(x25)      1
# USB:         Crc_table 16 $crc::polynomial(usb)      1
# BUYPASS:     Crc_table 16 $crc::polynomial(buypass)  0
# GSM:         Crc_table 16 $crc::polynomial(gsm)      0
# UNKNOWN2:    Crc_table 16 $crc::polynomial(unknown2) 1
# MAXIM:       Crc_table 16 $crc::polynomial(maxim)    1
# UNKNOWN3:    Crc_table 16 $crc::polynomial(unknown3) 0
# UNKNOWN4:    Crc_table 16 $crc::polynomial(unknown4) 0
# CMS:         Crc_table 16 $crc::polynomial(cms)      0
#
proc ::crc::Crc_table {width poly reflected} {
    set tbl {}
    if {$width < 32} {
        set mask   [expr {(1 << $width) - 1}]
        set topbit [expr {1 << ($width - 1)}]
    } else {
204
205
206
207
208
209
210



































































































































































































211
212
213
214
215
216
217
218
        set table(ccitt) [Crc_table 16 $polynomial(ccitt) 0]
    }

    return [Crc $s 16 [namespace current]::table(ccitt) $seed $xor 0]
}

# -------------------------------------------------------------------------



































































































































































































# Demostrates the parameters used for the 32 bit checksum CRC-32.
# This can be used to show the algorithm is working right by comparison with
# other crc32 implementations
proc ::crc::CRC-32 {s {seed 0xFFFFFFFF}} {
    variable table
    if {![info exists table(crc32)]} {
        variable polynomial
        set table(crc32) [Crc_table 32 $polynomial(crc32) 1]







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|







249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
        set table(ccitt) [Crc_table 16 $polynomial(ccitt) 0]
    }

    return [Crc $s 16 [namespace current]::table(ccitt) $seed $xor 0]
}

# -------------------------------------------------------------------------
# Specialisation of the general crc procedure to perform the KERMIT
# flavour of the CRC16 checksum
proc ::crc::CRC-KERMIT {s {seed 0} {xor 0}} {
    variable table
    if {![info exists table(kermit)]} {
        variable polynomial
        # ::crc::Crc_table width poly reflected
        set table(kermit) [Crc_table 16 $polynomial(kermit) 1]
    }

    # ::crc::Crc s width table init xorout reflected
    return [Crc $s 16 [namespace current]::table(kermit) $seed $xor 1]
}

# -------------------------------------------------------------------------
# Specialisation of the general crc procedure to perform the MODBUS
# flavour of the CRC16 checksum
proc ::crc::CRC-MODBUS {s {seed 0xFFFF} {xor 0}} {
    variable table
    if {![info exists table(modbus)]} {
        variable polynomial
        # ::crc::Crc_table width poly reflected
        set table(modbus) [Crc_table 16 $polynomial(modbus) 1]
    }

    # ::crc::Crc s width table init xorout reflected
    return [Crc $s 16 [namespace current]::table(modbus) $seed $xor 1]
}

# -------------------------------------------------------------------------
# Specialisation of the general crc procedure to perform the MCRF4XX
# flavour of the CRC16 checksum
proc ::crc::CRC-MCRF4XX {s {seed 0xFFFF} {xor 0}} {
    variable table
    if {![info exists table(mcrf4xx)]} {
        variable polynomial
        # ::crc::Crc_table width poly reflected
        set table(mcrf4xx) [Crc_table 16 $polynomial(mcrf4xx) 1]
    }

    # ::crc::Crc s width table init xorout reflected
    return [Crc $s 16 [namespace current]::table(mcrf4xx) $seed $xor 1]
}

# -------------------------------------------------------------------------
# Specialisation of the general crc procedure to perform the GENIBUS
# flavour of the CRC16 checksum
proc ::crc::CRC-GENIBUS {s {seed 0xFFFF} {xor 0xFFFF}} {
    variable table
    if {![info exists table(genibus)]} {
        variable polynomial
        # ::crc::Crc_table width poly reflected
        set table(genibus) [Crc_table 16 $polynomial(genibus) 0]
    }

    # ::crc::Crc s width table init xorout reflected
    return [Crc $s 16 [namespace current]::table(genibus) $seed $xor 0]
}

# -------------------------------------------------------------------------
# Specialisation of the general crc procedure to perform the X25
# flavour of the CRC16 checksum
proc ::crc::CRC-X25 {s {seed 0xFFFF} {xor 0xFFFF}} {
    variable table
    if {![info exists table(x25)]} {
        variable polynomial
        # ::crc::Crc_table width poly reflected
        set table(x25) [Crc_table 16 $polynomial(x25) 1]
    }

    # ::crc::Crc s width table init xorout reflected
    return [Crc $s 16 [namespace current]::table(x25) $seed $xor 1]
}

# -------------------------------------------------------------------------
# Specialisation of the general crc procedure to perform the USB
# flavour of the CRC16 checksum
proc ::crc::CRC-USB {s {seed 0xFFFF} {xor 0xFFFF}} {
    variable table
    if {![info exists table(usb)]} {
        variable polynomial
        # ::crc::Crc_table width poly reflected
        set table(usb) [Crc_table 16 $polynomial(usb) 1]
    }

    # ::crc::Crc s width table init xorout reflected
    return [Crc $s 16 [namespace current]::table(usb) $seed $xor 1]
}

# -------------------------------------------------------------------------
# Specialisation of the general crc procedure to perform the BUYPASS
# flavour of the CRC16 checksum
proc ::crc::CRC-BUYPASS {s {seed 0} {xor 0}} {
    variable table
    if {![info exists table(buypass)]} {
        variable polynomial
        # ::crc::Crc_table width poly reflected
        set table(buypass) [Crc_table 16 $polynomial(buypass) 0]
    }

    # ::crc::Crc s width table init xorout reflected
    return [Crc $s 16 [namespace current]::table(buypass) $seed $xor 0]
}

# -------------------------------------------------------------------------
# Specialisation of the general crc procedure to perform the GSM
# flavour of the CRC16 checksum
proc ::crc::CRC-GSM {s {seed 0} {xor 0xFFFF}} {
    variable table
    if {![info exists table(gsm)]} {
        variable polynomial
        # ::crc::Crc_table width poly reflected
        set table(gsm) [Crc_table 16 $polynomial(gsm) 0]
    }

    # ::crc::Crc s width table init xorout reflected
    return [Crc $s 16 [namespace current]::table(gsm) $seed $xor 0]
}

# -------------------------------------------------------------------------
# Specialisation of the general crc procedure to perform the UNKNOWN-2
# flavour of the CRC16 checksum
proc ::crc::CRC-UNKNOWN2 {s {seed 0} {xor 0xFFFF}} {
    variable table
    if {![info exists table(unknown2)]} {
        variable polynomial
        # ::crc::Crc_table width poly reflected
        set table(unknown2) [Crc_table 16 $polynomial(unknown2) 1]
    }

    # ::crc::Crc s width table init xorout reflected
    return [Crc $s 16 [namespace current]::table(unknown2) $seed $xor 1]
}

# -------------------------------------------------------------------------
# Specialisation of the general crc procedure to perform the MAXIM
# flavour of the CRC16 checksum
proc ::crc::CRC-MAXIM {s {seed 0} {xor 0xFFFF}} {
    variable table
    if {![info exists table(maxim)]} {
        variable polynomial
        # ::crc::Crc_table width poly reflected
        set table(maxim) [Crc_table 16 $polynomial(maxim) 1]
    }

    # ::crc::Crc s width table init xorout reflected
    return [Crc $s 16 [namespace current]::table(maxim) $seed $xor 1]
}

# -------------------------------------------------------------------------
# Specialisation of the general crc procedure to perform the UNKNOWN-3
# flavour of the CRC16 checksum
proc ::crc::CRC-UNKNOWN3 {s {seed 0} {xor 0xFFFF}} {
    variable table
    if {![info exists table(unknown3)]} {
        variable polynomial
        # ::crc::Crc_table width poly reflected
        set table(unknown3) [Crc_table 16 $polynomial(unknown3) 0]
    }

    # ::crc::Crc s width table init xorout reflected
    return [Crc $s 16 [namespace current]::table(unknown3) $seed $xor 0]
}

# -------------------------------------------------------------------------
# Specialisation of the general crc procedure to perform the UNKNOWN-4
# flavour of the CRC16 checksum
proc ::crc::CRC-UNKNOWN4 {s {seed 0xFFFF} {xor 0xFFFF}} {
    variable table
    if {![info exists table(unknown4)]} {
        variable polynomial
        # ::crc::Crc_table width poly reflected
        set table(unknown4) [Crc_table 16 $polynomial(unknown4) 0]
    }

    # ::crc::Crc s width table init xorout reflected
    return [Crc $s 16 [namespace current]::table(unknown4) $seed $xor 0]
}

# -------------------------------------------------------------------------
# Specialisation of the general crc procedure to perform the CMS
# flavour of the CRC16 checksum
proc ::crc::CRC-CMS {s {seed 0xFFFF} {xor 0}} {
    variable table
    if {![info exists table(cms)]} {
        variable polynomial
        # ::crc::Crc_table width poly reflected
        set table(cms) [Crc_table 16 $polynomial(cms) 0]
    }

    # ::crc::Crc s width table init xorout reflected
    return [Crc $s 16 [namespace current]::table(cms) $seed $xor 0]
}

# -------------------------------------------------------------------------
# Demonstrates the parameters used for the 32 bit checksum CRC-32.
# This can be used to show the algorithm is working right by comparison with
# other crc32 implementations
proc ::crc::CRC-32 {s {seed 0xFFFFFFFF}} {
    variable table
    if {![info exists table(crc32)]} {
        variable polynomial
        set table(crc32) [Crc_table 32 $polynomial(crc32) 1]
291
292
293
294
295
296
297
298



































































299
300
301
302
303
304
305
306
307
308
    return [eval [list crc -impl [namespace origin CRC-CCITT] -seed 0] $args]
}

proc ::crc::crc-32 {args} {
    return [eval [list crc -impl [namespace origin CRC-32] -seed 0xFFFFFFFF]\
                $args]
}




































































# -------------------------------------------------------------------------

package provide crc16 1.1.4

# -------------------------------------------------------------------------
#
# Local variables:
#   mode: tcl
#   indent-tabs-mode: nil
# End:








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>


|







531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
    return [eval [list crc -impl [namespace origin CRC-CCITT] -seed 0] $args]
}

proc ::crc::crc-32 {args} {
    return [eval [list crc -impl [namespace origin CRC-32] -seed 0xFFFFFFFF]\
                $args]
}

proc ::crc::kermit {args} {
    return [eval [list crc -impl [namespace origin CRC-KERMIT] -seed 0]\
                $args]
}

proc ::crc::modbus {args} {
    return [eval [list crc -impl [namespace origin CRC-MODBUS] -seed 0xFFFF]\
                $args]
}

proc ::crc::mcrf4xx {args} {
    return [eval [list crc -impl [namespace origin CRC-MCRF4XX] -seed 0xFFFF]\
                $args]
}

proc ::crc::genibus {args} {
    return [eval [list crc -impl [namespace origin CRC-GENIBUS] -seed 0xFFFF]\
                $args]
}

proc ::crc::crc-x25 {args} {
    return [eval [list crc -impl [namespace origin CRC-X25] -seed 0xFFFF]\
                $args]
}

proc ::crc::crc-sdlc {args} {
    return [eval [list crc -impl [namespace origin CRC-X25] -seed 0xFFFF]\
                $args]
}

proc ::crc::crc-usb {args} {
    return [eval [list crc -impl [namespace origin CRC-USB] -seed 0xFFFF]\
                $args]
}

proc ::crc::buypass {args} {
    return [eval [list crc -impl [namespace origin CRC-BUYPASS] -seed 0] $args]
}

proc ::crc::umts {args} {
    return [eval [list crc -impl [namespace origin CRC-BUYPASS] -seed 0] $args]
}

proc ::crc::gsm {args} {
    return [eval [list crc -impl [namespace origin CRC-GSM] -seed 0] $args]
}

proc ::crc::unknown2 {args} {
    return [eval [list crc -impl [namespace origin CRC-UNKNOWN2] -seed 0] $args]
}

proc ::crc::maxim {args} {
    return [eval [list crc -impl [namespace origin CRC-MAXIM] -seed 0] $args]
}

proc ::crc::unknown3 {args} {
    return [eval [list crc -impl [namespace origin CRC-UNKNOWN3] -seed 0] $args]
}

proc ::crc::unknown4 {args} {
    return [eval [list crc -impl [namespace origin CRC-UNKNOWN4] -seed 0xFFFF] $args]
}

proc ::crc::cms {args} {
    return [eval [list crc -impl [namespace origin CRC-CMS] -seed 0xFFFF] $args]
}

# -------------------------------------------------------------------------

package provide crc16 1.1.5

# -------------------------------------------------------------------------
#
# Local variables:
#   mode: tcl
#   indent-tabs-mode: nil
# End:

Changes to modules/crc/crc16.test.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# crc16.test - Copyright (C) 2002 Pat Thoyts <[email protected]>
#
# Tests for the crc16 commands
#
# -------------------------------------------------------------------------
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
# -------------------------------------------------------------------------
# RCS: @(#) $Id: crc16.test,v 1.7 2012/01/23 20:28:11 patthoyts Exp $

# -------------------------------------------------------------------------

source [file join \
            [file dirname [file dirname [file join [pwd] [info script]]]] \
            devtools testutilities.tcl]









<







1
2
3
4
5
6
7
8

9
10
11
12
13
14
15
# crc16.test - Copyright (C) 2002 Pat Thoyts <[email protected]>
#
# Tests for the crc16 commands
#
# -------------------------------------------------------------------------
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
# -------------------------------------------------------------------------


# -------------------------------------------------------------------------

source [file join \
            [file dirname [file dirname [file join [pwd] [info script]]]] \
            devtools testutilities.tcl]

218
219
220
221
222
223
224































225
226
227
228
229
230
231
232
233
    7    "\uFFFE\u0000\u0001\u0002"
    "0x2E64"
} {
    test crc16-8.$n {XMODEM CRCs as hexadecimal string} {
        list [catch {::crc::xmodem -format 0x%X $msg} res] $res
    } [list 0 $expected]
}































# -------------------------------------------------------------------------

catch {unset crc::filename}
testsuiteCleanup

# Local Variables:
#  mode: tcl
#  indent-tabs-mode: nil
# End:







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>









217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
    7    "\uFFFE\u0000\u0001\u0002"
    "0x2E64"
} {
    test crc16-8.$n {XMODEM CRCs as hexadecimal string} {
        list [catch {::crc::xmodem -format 0x%X $msg} res] $res
    } [list 0 $expected]
}

# -------------------------------------------------------------------------
# General tests based on the test vectors listed in the implementation
# -------------------------------------------------------------------------

foreach {n cmd expected} {
    1  crc16     0xBB3D
    2  crc-ccitt 0x29B1
    3  xmodem    0x31C3
    4  crc-32    0xCBF43926
    5  kermit    0x2189
    6  modbus    0x4B37
    7  mcrf4xx   0x6F91
    8  genibus   0xD64E
    9  crc-x25   0x906E
    10 crc-sdlc  0x906E
    11 crc-usb   0xB4C8
    12 buypass   0xFEE8
    13 umts      0xFEE8
    14 gsm       0xCE3C
    15 unknown2  0xDE76
    16 maxim     0x44C2
    17 unknown3   0x117
    18 unknown4  0x5118
    19 cms       0xAEE7
} {
    test crc16-9.$n "$cmd CRC as hexadecimal string" {
        list [catch {::crc::$cmd -format 0x%X "123456789"} res] $res
    } [list 0 $expected]
}

# -------------------------------------------------------------------------

catch {unset crc::filename}
testsuiteCleanup

# Local Variables:
#  mode: tcl
#  indent-tabs-mode: nil
# End:

Changes to modules/crc/pkgIndex.tcl.

1
2
3
4
5
if {![package vsatisfies [package provide Tcl] 8.2]} {return}
package ifneeded cksum 1.1.4 [list source [file join $dir cksum.tcl]]
package ifneeded crc16 1.1.4 [list source [file join $dir crc16.tcl]]
package ifneeded crc32 1.3.3 [list source [file join $dir crc32.tcl]]
package ifneeded sum   1.1.2 [list source [file join $dir sum.tcl]]


|


1
2
3
4
5
if {![package vsatisfies [package provide Tcl] 8.2]} {return}
package ifneeded cksum 1.1.4 [list source [file join $dir cksum.tcl]]
package ifneeded crc16 1.1.5 [list source [file join $dir crc16.tcl]]
package ifneeded crc32 1.3.3 [list source [file join $dir crc32.tcl]]
package ifneeded sum   1.1.2 [list source [file join $dir sum.tcl]]