]> git.ozlabs.org Git - ccan/blob - ccan/crc/crc.h
tdb2: implement tdb_chainlock_read/tdb_chainunlock_read.
[ccan] / ccan / crc / crc.h
1 #ifndef CCAN_CRC_H
2 #define CCAN_CRC_H
3 #include <stdint.h>
4 #include <stdlib.h>
5
6 /**
7  * crc32c - Castagnoli 32 bit crc of string of bytes
8  * @start_crc: the initial crc (usually 0)
9  * @buf: pointer to bytes
10  * @size: length of buffer
11  *
12  * If you don't know what crc32 to use, use this one: it's the best.
13  *
14  * @Article{castagnoli-crc,
15  * author =       { Guy Castagnoli and Stefan Braeuer and Martin Herrman},
16  * title =        {{Optimization of Cyclic Redundancy-Check Codes with 24
17  *                 and 32 Parity Bits}},
18  * journal =      IEEE Transactions on Communication,
19  * year =         {1993},
20  * volume =       {41},
21  * number =       {6},
22  * pages =        {},
23  * month =        {June},
24  *}
25  * 32 bit CRC checksum using polynomial
26  * X^32+X^28+X^27+X^26+X^25+X^23+X^22+X^20+X^19+X^18+X^14+X^13+X^11+X^10+X^9+X^8+X^6+X^0.
27  *
28  * You can calculate the CRC of non-contiguous arrays by passing @start_crc
29  * as 0 the first time, and the current crc result from then on.
30  *
31  * Example:
32  *      #include <sys/uio.h>
33  *      ...
34  *      // Check that iovec has the crc we expect (Castagnoli version)
35  *      static bool check_crc(uint32_t expected, const struct iovec *iov, int l)
36  *      {
37  *              uint32_t crc = 0;
38  *              while (l >= 0) {
39  *                      crc = crc32c(crc, iov->iov_base, iov->iov_len);
40  *                      iov++;
41  *              }
42  *              return crc == expected;
43  *      }
44  */
45 uint32_t crc32c(uint32_t start_crc, const void *buf, size_t size);
46
47 /**
48  * crc32c_table - Get the Castagnoli CRC table
49  *
50  * For special effects, you might want direct access to the table; this is
51  * the standard 256-entry table for this algorithm.
52  *
53  * In theory, this might need to malloc(), and thus return NULL.
54  *
55  * Example:
56  *      // This dumb code only handles Castagnoli, so assert that here.
57  *      static void check_user_crc_table(const uint32_t *usertab)
58  *      {
59  *              const uint32_t *ctab = crc32c_table();
60  *              if (!ctab || memcmp(ctab, usertab, 1024) != 0)
61  *                      abort();
62  *      }
63  */
64 const uint32_t *crc32c_table(void);
65
66 /**
67  * crc32_ieee - IEEE 802.3 32 bit crc of string of bytes
68  * @start_crc: the initial crc (usually 0)
69  * @buf: pointer to bytes
70  * @size: length of buffer
71  *
72  * 32 bit CRC checksum using polynomial
73  * 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+X^0.
74  *
75  * See crc32c() for details.
76  */
77 uint32_t crc32_ieee(uint32_t start_crc, const void *buf, size_t size);
78
79 /**
80  * crc32_ieee_table - Get the IEEE 802.3 CRC table
81  *
82  * See crc32c_table() for details.
83  */
84 const uint32_t *crc32_ieee_table(void);
85
86 /**
87  * crc64_iso - ISO 3309
88  * @start_crc: the initial crc (usually 0)
89  * @buf: pointer to bytes
90  * @size: length of buffer
91  *
92  * 64 bit CRC checksum using polynomial
93  * X^64 + X^4 + X^3 + X^1 + X^0
94  *
95  * See crc32c() for details.
96  */
97 uint64_t crc64_iso(uint64_t start_crc, const void *buf, size_t size);
98
99 /**
100  * crc64_iso_table - Get the ISO 3309 CRC table
101  *
102  * See crc32c_table() for details.
103  */
104 const uint64_t *crc64_iso_table(void);
105
106 #endif /* CCAN_CRC_H */