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 #ifndef _CRC_REF_H
31 #define _CRC_REF_H
32
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36
37 #include "crc.h"
38
39 #ifdef _MSC_VER
40 #define inline __inline
41 #endif
42
43 #define MAX_ITER 8
44
45 // iSCSI CRC reference function
46 static inline unsigned int
crc32_iscsi_ref(unsigned char * buffer,int len,unsigned int crc_init)47 crc32_iscsi_ref(unsigned char *buffer, int len, unsigned int crc_init)
48 {
49 uint64_t rem = crc_init;
50 int i, j;
51
52 uint32_t poly = 0x82F63B78;
53
54 for (i = 0; i < len; i++) {
55 rem = rem ^ (buffer[i]);
56 for (j = 0; j < MAX_ITER; j++) {
57 rem = (rem & 0x1ULL) ? (rem >> 1) ^ poly : (rem >> 1);
58 }
59 }
60 return rem;
61 }
62
63 // crc16_t10dif reference function, slow crc16 from the definition.
64 static inline uint16_t
crc16_t10dif_ref(uint16_t seed,uint8_t * buf,uint64_t len)65 crc16_t10dif_ref(uint16_t seed, uint8_t *buf, uint64_t len)
66 {
67 size_t rem = seed;
68 unsigned int i, j;
69
70 uint16_t poly = 0x8bb7; // t10dif standard
71
72 for (i = 0; i < len; i++) {
73 rem = rem ^ (buf[i] << 8);
74 for (j = 0; j < MAX_ITER; j++) {
75 rem = rem << 1;
76 rem = (rem & 0x10000) ? rem ^ poly : rem;
77 }
78 }
79 return rem;
80 }
81
82 // crc16_t10dif reference function, slow crc16 from the definition.
83 static inline uint16_t
crc16_t10dif_copy_ref(uint16_t seed,uint8_t * dst,uint8_t * src,uint64_t len)84 crc16_t10dif_copy_ref(uint16_t seed, uint8_t *dst, uint8_t *src, uint64_t len)
85 {
86 size_t rem = seed;
87 unsigned int i, j;
88
89 uint16_t poly = 0x8bb7; // t10dif standard
90
91 for (i = 0; i < len; i++) {
92 rem = rem ^ (src[i] << 8);
93 dst[i] = src[i];
94 for (j = 0; j < MAX_ITER; j++) {
95 rem = rem << 1;
96 rem = (rem & 0x10000) ? rem ^ poly : rem;
97 }
98 }
99 return rem;
100 }
101
102 // crc32_ieee reference function, slow crc32 from the definition.
103 static inline uint32_t
crc32_ieee_ref(uint32_t seed,uint8_t * buf,uint64_t len)104 crc32_ieee_ref(uint32_t seed, uint8_t *buf, uint64_t len)
105 {
106 uint64_t rem = ~seed;
107 unsigned int i, j;
108
109 uint32_t poly = 0x04C11DB7; // IEEE standard
110
111 for (i = 0; i < len; i++) {
112 rem = rem ^ ((uint64_t) buf[i] << 24);
113 for (j = 0; j < MAX_ITER; j++) {
114 rem = rem << 1;
115 rem = (rem & 0x100000000ULL) ? rem ^ poly : rem;
116 }
117 }
118 return ~rem;
119 }
120
121 // crc32_gzip_refl reference function, slow crc32 from the definition.
122 // Please get difference details between crc32_gzip_ref and crc32_ieee
123 // from crc.h.
124 static inline uint32_t
crc32_gzip_refl_ref(uint32_t seed,uint8_t * buf,uint64_t len)125 crc32_gzip_refl_ref(uint32_t seed, uint8_t *buf, uint64_t len)
126 {
127 uint64_t rem = ~seed;
128 int i, j;
129
130 uint32_t poly = 0xEDB88320; // IEEE standard
131
132 for (i = 0; i < len; i++) {
133 rem = rem ^ (buf[i]);
134 for (j = 0; j < MAX_ITER; j++) {
135 rem = (rem & 0x1ULL) ? (rem >> 1) ^ poly : (rem >> 1);
136 }
137 }
138 return ~rem;
139 }
140
141 #ifdef __cplusplus
142 }
143 #endif
144
145 #endif
146