xref: /isa-l/include/crc.h (revision fa5b8baf84e6a18dbaad48a3fa0d1fa062ae2fe8)
1 /**********************************************************************
2   Copyright(c) 2011-2015 Intel Corporation All rights reserved.
3 
4   Redistribution and use in source and binary forms, with or without
5   modification, are permitted provided that the following conditions
6   are met:
7     * Redistributions of source code must retain the above copyright
8       notice, this list of conditions and the following disclaimer.
9     * Redistributions in binary form must reproduce the above copyright
10       notice, this list of conditions and the following disclaimer in
11       the documentation and/or other materials provided with the
12       distribution.
13     * Neither the name of Intel Corporation nor the names of its
14       contributors may be used to endorse or promote products derived
15       from this software without specific prior written permission.
16 
17   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 **********************************************************************/
29 
30 /**
31  *  @file  crc.h
32  *  @brief CRC functions.
33  */
34 
35 #ifndef _CRC_H_
36 #define _CRC_H_
37 
38 #include <stdint.h>
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 
44 /* Multi-binary functions */
45 
46 /**
47  * @brief Generate CRC from the T10 standard, runs appropriate version.
48  *
49  * This function determines what instruction sets are enabled and
50  * selects the appropriate version at runtime.
51  *
52  * @returns 16 bit CRC
53  */
54 uint16_t
55 crc16_t10dif(uint16_t init_crc,        //!< initial CRC value, 16 bits
56              const unsigned char *buf, //!< buffer to calculate CRC on
57              uint64_t len              //!< buffer length in bytes (64-bit data)
58 );
59 
60 /**
61  * @brief Generate CRC and copy T10 standard, runs appropriate version.
62  *
63  * Stitched CRC + copy function.
64  *
65  * @returns 16 bit CRC
66  */
67 uint16_t
68 crc16_t10dif_copy(uint16_t init_crc, //!< initial CRC value, 16 bits
69                   uint8_t *dst,      //!< buffer destination for copy
70                   uint8_t *src,      //!< buffer source to crc + copy
71                   uint64_t len       //!< buffer length in bytes (64-bit data)
72 );
73 
74 /**
75  * @brief Generate CRC from the IEEE standard, runs appropriate version.
76  *
77  * This function determines what instruction sets are enabled and
78  * selects the appropriate version at runtime.
79  * Note: CRC32 IEEE standard is widely used in HDLC, Ethernet, Gzip and
80  * many others. Its polynomial is 0x04C11DB7 in normal and 0xEDB88320
81  * in reflection (or reverse). In ISA-L CRC, function crc32_ieee is
82  * actually designed for normal CRC32 IEEE version. And function
83  * crc32_gzip_refl is actually designed for reflected CRC32 IEEE.
84  * These two versions of CRC32 IEEE are not compatible with each other.
85  * Users who want to replace their not optimized crc32 ieee with ISA-L's
86  * crc32 function should be careful of that.
87  * Since many applications use CRC32 IEEE reflected version, Please have
88  * a check whether crc32_gzip_refl is right one for you instead of
89  * crc32_ieee.
90  *
91  * @returns 32 bit CRC
92  */
93 
94 uint32_t
95 crc32_ieee(uint32_t init_crc,        //!< initial CRC value, 32 bits
96            const unsigned char *buf, //!< buffer to calculate CRC on
97            uint64_t len              //!< buffer length in bytes (64-bit data)
98 );
99 
100 /**
101  * @brief Generate the customized CRC
102  * based on RFC 1952 CRC (http://www.ietf.org/rfc/rfc1952.txt) standard,
103  * runs appropriate version.
104  *
105  * This function determines what instruction sets are enabled and
106  * selects the appropriate version at runtime.
107  *
108  * Note: CRC32 IEEE standard is widely used in HDLC, Ethernet, Gzip and
109  * many others. Its polynomial is 0x04C11DB7 in normal and 0xEDB88320
110  * in reflection (or reverse). In ISA-L CRC, function crc32_ieee is
111  * actually designed for normal CRC32 IEEE version. And function
112  * crc32_gzip_refl is actually designed for reflected CRC32 IEEE.
113  * These two versions of CRC32 IEEE are not compatible with each other.
114  * Users who want to replace their not optimized crc32 ieee with ISA-L's
115  * crc32 function should be careful of that.
116  * Since many applications use CRC32 IEEE reflected version, Please have
117  * a check whether crc32_gzip_refl is right one for you instead of
118  * crc32_ieee.
119  *
120  * @returns 32 bit CRC
121  */
122 uint32_t
123 crc32_gzip_refl(uint32_t init_crc,        //!< initial CRC value, 32 bits
124                 const unsigned char *buf, //!< buffer to calculate CRC on
125                 uint64_t len              //!< buffer length in bytes (64-bit data)
126 );
127 
128 /**
129  * @brief ISCSI CRC function, runs appropriate version.
130  *
131  * This function determines what instruction sets are enabled and
132  * selects the appropriate version at runtime.
133  *
134  * @returns 32 bit CRC
135  */
136 unsigned int
137 crc32_iscsi(unsigned char *buffer, //!< buffer to calculate CRC on
138             int len,               //!< buffer length in bytes
139             unsigned int init_crc  //!< initial CRC value
140 );
141 
142 /* Base functions */
143 
144 /**
145  * @brief ISCSI CRC function, baseline version
146  * @returns 32 bit CRC
147  */
148 unsigned int
149 crc32_iscsi_base(unsigned char *buffer, //!< buffer to calculate CRC on
150                  int len,               //!< buffer length in bytes
151                  unsigned int crc_init  //!< initial CRC value
152 );
153 
154 /**
155  * @brief Generate CRC from the T10 standard, runs baseline version
156  * @returns 16 bit CRC
157  */
158 uint16_t
159 crc16_t10dif_base(uint16_t seed, //!< initial CRC value, 16 bits
160                   uint8_t *buf,  //!< buffer to calculate CRC on
161                   uint64_t len   //!< buffer length in bytes (64-bit data)
162 );
163 
164 /**
165  * @brief Generate CRC and copy T10 standard, runs baseline version.
166  * @returns 16 bit CRC
167  */
168 uint16_t
169 crc16_t10dif_copy_base(uint16_t init_crc, //!< initial CRC value, 16 bits
170                        uint8_t *dst,      //!< buffer destination for copy
171                        uint8_t *src,      //!< buffer source to crc + copy
172                        uint64_t len       //!< buffer length in bytes (64-bit data)
173 );
174 
175 /**
176  * @brief Generate CRC from the IEEE standard, runs baseline version
177  * @returns 32 bit CRC
178  */
179 uint32_t
180 crc32_ieee_base(uint32_t seed, //!< initial CRC value, 32 bits
181                 uint8_t *buf,  //!< buffer to calculate CRC on
182                 uint64_t len   //!< buffer length in bytes (64-bit data)
183 );
184 
185 /**
186  * @brief Generate the customized CRC
187  * based on RFC 1952 CRC (http://www.ietf.org/rfc/rfc1952.txt) standard,
188  * runs baseline version
189  * @returns 32 bit CRC
190  */
191 uint32_t
192 crc32_gzip_refl_base(uint32_t seed, //!< initial CRC value, 32 bits
193                      uint8_t *buf,  //!< buffer to calculate CRC on
194                      uint64_t len   //!< buffer length in bytes (64-bit data)
195 );
196 
197 #ifdef __cplusplus
198 }
199 #endif
200 
201 #endif // _CRC_H_
202