crc16(3tcl)

πŸ“¦
version 2.0 Β· 3 versions
, in debian trixie, ubuntu questing, ubuntu resolute, ubuntu stonking

πŸ’– crc16 β€” Perform a 16bit Cyclic Redundancy Check πŸ’–

okay bestie, gather round 🫢 this is the crc16 page from the 3tcl manual, and honestly? it's giving checksum royalty. Let's get into it.


✨ SYNOPSIS ✨

First, the vibes check β€” what you need loaded before any of this works:

package require Tcl 8.5 9

package require crc16 ?1.1.5?

and then, the commands themselves. There are a lot of them, because CRC has so many permutations, and this package said "we're doing all of them, no cap":

::crc::crc16 ?-format format? ?-seed value? ?-implementation procname? -- message

::crc::crc16 ?-format format? ?-seed value? ?-implementation procname? -filename file

::crc::crc-ccitt ?-format format? ?-seed value? ?-implementation procname? -- message

::crc::crc-ccitt ?-format format? ?-seed value? ?-implementation procname? -filename file

::crc::xmodem ?-format format? ?-seed value? ?-implementation procname? -- message

::crc::xmodem ?-format format? ?-seed value? ?-implementation procname? -filename file

::crc::kermit ?-format format? ?-seed value? ?-implementation procname? -- message

::crc::kermit ?-format format? ?-seed value? ?-implementation procname? -filename file

::crc::modbus ?-format format? ?-seed value? ?-implementation procname? -- message

::crc::modbus ?-format format? ?-seed value? ?-implementation procname? -filename file

::crc::mcrf4xx ?-format format? ?-seed value? ?-implementation procname? -- message

::crc::mcrf4xx ?-format format? ?-seed value? ?-implementation procname? -filename file

::crc::genibus ?-format format? ?-seed value? ?-implementation procname? -- message

::crc::genibus ?-format format? ?-seed value? ?-implementation procname? -filename file

::crc::crc-x25 ?-format format? ?-seed value? ?-implementation procname? -- message

::crc::crc-x25 ?-format format? ?-seed value? ?-implementation procname? -filename file

::crc::crc-sdlc ?-format format? ?-seed value? ?-implementation procname? -- message

::crc::crc-sdlc ?-format format? ?-seed value? ?-implementation procname? -filename file

::crc::crc-usb ?-format format? ?-seed value? ?-implementation procname? -- message

::crc::crc-usb ?-format format? ?-seed value? ?-implementation procname? -filename file

::crc::buypass ?-format format? ?-seed value? ?-implementation procname? -- message

::crc::buypass ?-format format? ?-seed value? ?-implementation procname? -filename file

::crc::umts ?-format format? ?-seed value? ?-implementation procname? -- message

::crc::umts ?-format format? ?-seed value? ?-implementation procname? -filename file

::crc::gsm ?-format format? ?-seed value? ?-implementation procname? -- message

::crc::gsm ?-format format? ?-seed value? ?-implementation procname? -filename file

::crc::unknown2 ?-format format? ?-seed value? ?-implementation procname? -- message

::crc::unknown2 ?-format format? ?-seed value? ?-implementation procname? -filename file

::crc::maxim ?-format format? ?-seed value? ?-implementation procname? -- message

::crc::maxim ?-format format? ?-seed value? ?-implementation procname? -filename file

::crc::unknown3 ?-format format? ?-seed value? ?-implementation procname? -- message

::crc::unknown3 ?-format format? ?-seed value? ?-implementation procname? -filename file

::crc::unknown4 ?-format format? ?-seed value? ?-implementation procname? -- message

::crc::unknown4 ?-format format? ?-seed value? ?-implementation procname? -filename file

::crc::cms ?-format format? ?-seed value? ?-implementation procname? -- message

::crc::cms ?-format format? ?-seed value? ?-implementation procname? -filename file

like… pick a flavor, any flavor πŸ’…


🌸 DESCRIPTION 🌸

So this package is a Tcl-only implementation of the CRC algorithms, built on the info from http://www.microconsultants.com/tips/crc/crc.txt ⭐ There are literally so many permutations for calculating CRC checksums, and this package handles all of them. The defaults are already set up for the most common cases, so you don't have to think about it if you don't wanna. Periodt.


πŸŽ€ COMMANDS πŸŽ€

okay so every single one of these takes either string data or a file name, and hands you back a checksum value. Which command you call sets up the CRC polynomial, the initial value and the bit ordering for the checksum standard you want. πŸ’–

Each command comes in two shapes β€” the -- message one and the -filename file one:

  • ::crc::crc16 β€” ?-format format? ?-seed value? ?-implementation procname? -- message or ?-format format? ?-seed value? ?-implementation procname? -filename file
  • ::crc::crc-ccitt β€” ?-format format? ?-seed value? ?-implementation procname? -- message or ?-format format? ?-seed value? ?-implementation procname? -filename file
  • ::crc::xmodem β€” ?-format format? ?-seed value? ?-implementation procname? -- message or ?-format format? ?-seed value? ?-implementation procname? -filename file
  • ::crc::kermit β€” ?-format format? ?-seed value? ?-implementation procname? -- message or ?-format format? ?-seed value? ?-implementation procname? -filename file
  • ::crc::modbus β€” ?-format format? ?-seed value? ?-implementation procname? -- message or ?-format format? ?-seed value? ?-implementation procname? -filename file
  • ::crc::mcrf4xx β€” ?-format format? ?-seed value? ?-implementation procname? -- message or ?-format format? ?-seed value? ?-implementation procname? -filename file
  • ::crc::genibus β€” ?-format format? ?-seed value? ?-implementation procname? -- message or ?-format format? ?-seed value? ?-implementation procname? -filename file
  • ::crc::crc-x25 β€” ?-format format? ?-seed value? ?-implementation procname? -- message or ?-format format? ?-seed value? ?-implementation procname? -filename file
  • ::crc::crc-sdlc β€” ?-format format? ?-seed value? ?-implementation procname? -- message or ?-format format? ?-seed value? ?-implementation procname? -filename file
  • ::crc::crc-usb β€” ?-format format? ?-seed value? ?-implementation procname? -- message or ?-format format? ?-seed value? ?-implementation procname? -filename file
  • ::crc::buypass β€” ?-format format? ?-seed value? ?-implementation procname? -- message or ?-format format? ?-seed value? ?-implementation procname? -filename file
  • ::crc::umts β€” ?-format format? ?-seed value? ?-implementation procname? -- message or ?-format format? ?-seed value? ?-implementation procname? -filename file
  • ::crc::gsm β€” ?-format format? ?-seed value? ?-implementation procname? -- message or ?-format format? ?-seed value? ?-implementation procname? -filename file
  • ::crc::unknown2 β€” ?-format format? ?-seed value? ?-implementation procname? -- message or ?-format format? ?-seed value? ?-implementation procname? -filename file
  • ::crc::maxim β€” ?-format format? ?-seed value? ?-implementation procname? -- message or ?-format format? ?-seed value? ?-implementation procname? -filename file
  • ::crc::unknown3 β€” ?-format format? ?-seed value? ?-implementation procname? -- message or ?-format format? ?-seed value? ?-implementation procname? -filename file
  • ::crc::unknown4 β€” ?-format format? ?-seed value? ?-implementation procname? -- message or ?-format format? ?-seed value? ?-implementation procname? -filename file
  • ::crc::cms β€” ?-format format? ?-seed value? ?-implementation procname? -- message or ?-format format? ?-seed value? ?-implementation procname? -filename file

so what actually happens: the command takes either string data or a file name and returns a checksum value calculated using the CRC algorithm 🧚 The command you picked sets up the CRC polynomial, the initial value and the bit ordering for whatever standard checksum calculation you're going for. The result gets formatted using the format(3tcl) specifier you hand it, or as an unsigned integer (%u) if you don't say anything.

A bunch of common polynomials are in use with CRC, and the most commonly used ones are all bundled up in here πŸŽ€ For convenience, every single one of them gets a command alias in the crc namespace. Cute, right?

Also β€” lowkey iconic β€” you can implement the CRC-32 checksum with this crc16 package, because the implementation is generic enough to stretch to 32 bit checksums. Someone already did it as an example. But heads up bestie: it is not the fastest way to do that algorithm in Tcl, and there's a separate crc32 package available if speed is your thing. πŸƒβ€β™€οΈπŸ’¨


πŸ’… OPTIONS πŸ’…

  • -filename name β€” return a checksum for the file contents instead of for the parameter data. file girlies rise πŸ“
  • -format string β€” return the checksum using an alternative format template. dress it up however you want ✨
  • -seed value β€” pick a different seed value for the CRC calculation. Default is 0 for the CRC16 calculation and 0xFFFF for the CCITT version. This is so useful for calculating the CRC of data structures without first squishing the whole structure into a string β€” you can take the CRC of the previous member and use it as the seed for the next member. It's also the move for accumulating a checksum from fragments of a large message (or file). πŸ’œ
  • -implementation procname β€” this is a hook so you can bring your own implementation (maybe a C compiled extension, main character energy). The procedure you name gets called with two parameters: first the data to be checksummed, second the seed value. It's expected to hand back an integer as the result.

The package already ships implementations of standard CRC polynomials for XMODEM, CCITT and the usual CRC-16 checksum. And for convenience, extra commands exist that make use of those implementations. πŸ¦‹

  • -- β€” terminate option processing. ⚠️ And listen, this one matters: using the option termination flag is important when you're processing data from parameters. If your binary data happens to look like one of the options above, it'll get read as an option if you leave this marker out. So always use the -- option termination flag before giving the data argument. always always always πŸ™ˆ

πŸ”₯ EXAMPLES πŸ”₯

% crc::crc16 -- "Hello, World!"
64077

% crc::crc-ccitt -- "Hello, World!"
26586

% crc::crc16 -format 0x%X -- "Hello, World!"
0xFA4D

% crc::crc16 -file crc16.tcl
51675

look at those numbers go πŸ’«


πŸ‘‘ AUTHORS πŸ‘‘

Pat Thoyts

πŸ’– BUGS, IDEAS, FEEDBACK πŸ’–

Okay bestie, real talk for a sec. This doc? And the whole package it's describing? It absolutely, definitely, one-hundred-percent has bugs. Like, other problems too. It's software, it's giving human-made, it's not that girl who's perfect at everything. πŸ™ˆ

So if you find one, please report it β€” drop it in the crc category of the Tcllib Trackers at:

http://core.tcl.tk/tcllib/reportlist

And listen, if you've got ideas β€” glow-ups, enhancements, little dreams for either the package or the documentation β€” send those along too. Both count. Both are welcome. 🫢


Now, when you're proposing actual code changes, here's the vibe check: hand over unified diffs. That means the output of:

diff -u
⚠️ Also super important: attachments are strongly preferred over inlined patches. Like, do not paste your whole patch into the message body, bestie, please. 😭

So how do you even attach? Easy. Make the ticket first, then immediately go to the Edit form of that ticket, and use the left-most button in the secondary navigation bar. That's the one. That's the door. ✨


🌈 SEE ALSO

Go read these too, they're all part of the same little family:


⭐ KEYWORDS

For the search bar girlies and the index gremlins, here's the tag cloud:

checksum, cksum, crc, crc16, crc32, cyclic redundancy check, data integrity, security


πŸ‘‘ CATEGORY

Hashes, checksums, and encryption


Copyright (c) 2002, 2017, Pat Thoyts

And that's a wrap on this page, my love πŸ’… β€” brought to you by tcllib, version 1.1.5, and filed under crc16(3tcl). Go check your checksums, stay safe out there, and remember: integrity is the whole point. Periodt. πŸŒΈπŸ¦‹

πŸ“¦ where this page comes from