Motr  M0
crc.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2013-2020 Seagate Technology LLC and/or its Affiliates
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * For any questions about this software or licensing,
18  * please email opensource@seagate.com or cortx-questions@seagate.com.
19  *
20  */
21 
22 
23 #include "lib/misc.h"
24 
31 static uint64_t md_crc32_cksum(void *data, uint64_t len, uint64_t *cksum);
32 
40 #define CRC_POLY 0x04C11DB7
41 #define CRC_WIDTH 32
42 #define CRC_SLICE_SIZE 8
43 #define CRC_TABLE_SIZE 256
44 
46 static bool is_table = false;
47 
48 static void crc_mktable(void)
49 {
50  int i;
51  int j;
52  uint32_t hibit = M0_BITS(CRC_WIDTH - 1);
53  uint32_t crc;
54 
55  for (i = 0; i < CRC_TABLE_SIZE; i++) {
56  crc = (uint32_t)i << (CRC_WIDTH - CRC_SLICE_SIZE);
57  for(j = 0; j < CRC_SLICE_SIZE; j++) {
58  crc <<= 1;
59  if (crc & hibit)
60  crc ^= CRC_POLY;
61  }
62  crc_table[i] = crc;
63  }
64 }
65 
66 static uint32_t crc32(uint32_t crc, unsigned char const *data, m0_bcount_t len)
67 {
68  M0_PRE(data != NULL);
69  M0_PRE(len > 0);
70 
71  if (!is_table) {
72  crc_mktable();
73  is_table = true;
74  }
75 
76  while (len--)
77  crc = ((crc << CRC_SLICE_SIZE) | *data++) ^
78  crc_table[crc >> (CRC_WIDTH - CRC_SLICE_SIZE) & 0xFF];
79 
80  return crc;
81 }
82 
83 M0_INTERNAL void m0_crc32(const void *data, uint64_t len,
84  uint64_t *cksum)
85 {
86  M0_PRE(data != NULL);
87  M0_PRE(len > 0);
88  M0_PRE(cksum != NULL);
89 
90  cksum[0] = crc32(~0, data, len);
91 }
92 
93 M0_INTERNAL bool m0_crc32_chk(const void *data, uint64_t len,
94  const uint64_t *cksum)
95 {
96  M0_PRE(data != NULL);
97  M0_PRE(len > 0);
98  M0_PRE(cksum != NULL);
99 
100  return cksum[0] == (uint64_t) crc32(~0, data, len);
101 }
102 
103 static void md_crc32_cksum_set(void *data, uint64_t len, uint64_t *cksum)
104 {
105  *cksum = md_crc32_cksum(data, len, cksum);
106 }
107 
108 static uint64_t md_crc32_cksum(void *data, uint64_t len, uint64_t *cksum)
109 {
110  uint64_t crc = ~0;
111  uint64_t old_cksum = *cksum;
112 
113  *cksum = 0;
114  crc = crc32(crc, data, len);
115  *cksum = old_cksum;
116 
117  return crc;
118 }
119 
120 static bool md_crc32_cksum_check(void *data, uint64_t len, uint64_t *cksum)
121 {
122  return *cksum == md_crc32_cksum(data, len, cksum);
123 }
124 
127 /*
128  * Local variables:
129  * c-indentation-style: "K&R"
130  * c-basic-offset: 8
131  * tab-width: 8
132  * fill-column: 79
133  * scroll-step: 1
134  * End:
135  */
#define M0_PRE(cond)
#define NULL
Definition: misc.h:38
struct m0_bufvec data
Definition: di.c:40
#define CRC_WIDTH
Definition: crc.c:41
static uint64_t md_crc32_cksum(void *data, uint64_t len, uint64_t *cksum)
Definition: crc.c:108
#define M0_BITS(...)
Definition: misc.h:236
uint64_t m0_bcount_t
Definition: types.h:77
M0_INTERNAL bool m0_crc32_chk(const void *data, uint64_t len, const uint64_t *cksum)
Definition: crc.c:93
int i
Definition: dir.c:1033
#define CRC_SLICE_SIZE
Definition: crc.c:42
M0_INTERNAL void m0_crc32(const void *data, uint64_t len, uint64_t *cksum)
Definition: crc.c:83
#define CRC_POLY
Definition: crc.c:40
static void md_crc32_cksum_set(void *data, uint64_t len, uint64_t *cksum)
Definition: crc.c:103
static uint32_t crc32(uint32_t crc, unsigned char const *data, m0_bcount_t len)
Definition: crc.c:66
static bool is_table
Definition: crc.c:46
static void crc_mktable(void)
Definition: crc.c:48
#define CRC_TABLE_SIZE
Definition: crc.c:43
static bool md_crc32_cksum_check(void *data, uint64_t len, uint64_t *cksum)
Definition: crc.c:120
uint32_t crc_table[CRC_TABLE_SIZE]
Definition: crc.c:45