crc32.cc
Go to the documentation of this file.
1 // ======================================================================
2 //
3 // crc32: Calculate a CRC32 checksum
4 //
5 // ======================================================================
6 //
7 // The following comments are provided by way of attribution in order to
8 // reflect the origin of the algorithm embedded herein. They are copied
9 // verbatim, but have been lightly edited for stylistic consistency and
10 // to remove details not relevant to this implementation:
11 //
12 // ----------------------------------------------------------------------
13 //
14 // C implementation of CRC-32 checksums for NAACCR records. Code is
15 // based upon and utilizes algorithm published by Ross Williams.
16 //
17 // Provided by: Eric Durbin, Kentucky Cancer Registry, University of
18 // Kentucky, October 14, 1998
19 //
20 // Status: Public Domain
21 //
22 // CRC LOOKUP TABLE
23 // ================
24 // The following CRC lookup table was generated automagically by the
25 // Rocksoft^tm Model CRC Algorithm Table Generation Program V1.0 using
26 // the following model parameters:
27 //
28 // Width : 4 bytes.
29 // Poly : 0x04C11DB7L
30 // Reverse : TRUE.
31 //
32 // For more information on the Rocksoft^tm Model CRC Algorithm, see the
33 // document titled "A Painless Guide to CRC Error Detection Algorithms"
34 // by Ross Williams (ross@guest.adelaide.edu.au.). This document is
35 // likely to be in the FTP archive "ftp.adelaide.edu.au/pub/rocksoft".
36 //
37 // ======================================================================
38 
39 #include "cetlib/crc32.h"
40 
41 using cet::crc32;
42 
43 static_assert(crc32{""}.digest() == 0,
44  "Digest for CRC32 of empty string is not 0!");
45 
46 // ----------------------------------------------------------------------
47 
48 constexpr std::uint32_t cet::crc32::crctable[];
49 
50 crc32::crc32(std::string const& mesg)
51 {
52  operator<<(mesg);
53 }
54 
55 crc32&
57 {
58  for (uchar const ch : mesg) {
59  context = crctable[(context ^ ch) & 0xFFL] ^ (context >> 8);
60  }
61  return *this;
62 }
63 
64 // ======================================================================
unsigned char uchar
Definition: crc32.h:21
std::string string
Definition: nybbler.cc:12
static constexpr std::uint32_t crctable[256]
Definition: crc32.h:47
digest_t context
Definition: crc32.h:39
constexpr crc32 & operator<<(char const *mesg)
Definition: crc32.h:119
constexpr digest_t digest() const
Definition: crc32.h:138