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