XRootD
XrdOssCsiCrcUtils.hh
Go to the documentation of this file.
1 #ifndef _XRDOSSCSICRCUTILS_H
2 #define _XRDOSSCSICRCUTILS_H
3 /******************************************************************************/
4 /* */
5 /* X r d O s s C s i C r c U t i l s . h h */
6 /* */
7 /* (C) Copyright 2021 CERN. */
8 /* */
9 /* This file is part of the XRootD software suite. */
10 /* */
11 /* XRootD is free software: you can redistribute it and/or modify it under */
12 /* the terms of the GNU Lesser General Public License as published by the */
13 /* Free Software Foundation, either version 3 of the License, or (at your */
14 /* option) any later version. */
15 /* */
16 /* In applying this licence, CERN does not waive the privileges and */
17 /* immunities granted to it by virtue of its status as an Intergovernmental */
18 /* Organization or submit itself to any jurisdiction. */
19 /* */
20 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */
21 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
22 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
23 /* License for more details. */
24 /* */
25 /* You should have received a copy of the GNU Lesser General Public License */
26 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
27 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
28 /* */
29 /* The copyright holder's institutional names and contributor's names may not */
30 /* be used to endorse or promote products derived from this software without */
31 /* specific prior written permission of the institution or contributor. */
32 /******************************************************************************/
33 
34 #include "XrdOuc/XrdOucCRC.hh"
35 #include "XrdSys/XrdSysPageSize.hh"
36 #include "assert.h"
37 
39 public:
40 
41  // crc32c_combine
42  //
43  // crc1: crc32c value of data1
44  // crc2: crc32c value of data2
45  // len2: length of data2
46  //
47  // returns crc of concatenation of data1|data2
48  // note: using Calc32C: some optimisation could be made
49  static uint32_t crc32c_combine(uint32_t crc1, uint32_t crc2, size_t len2)
50  {
51  if (len2==0)
52  return crc1;
53 
54  assert(len2<=XrdSys::PageSize);
55 
56  const uint32_t c1 = XrdOucCRC::Calc32C(g_bz, len2, ~crc1);
57  return ~c1^crc2;
58  }
59 
60  // crc32c_split1
61  //
62  // crctot: crc32c of data1|data2
63  // crc2: crc32c of data2
64  // len2: length of data2
65  //
66  // returns crc of data1
67  // note: crc bitshift to right, significant optimisation likely
68  // possible with intrinsics or a precomputed table
69  static uint32_t crc32c_split1(uint32_t crctot, uint32_t crc2, size_t len2)
70  {
71  if (len2==0)
72  return crctot;
73 
74  assert(len2<=XrdSys::PageSize);
75  uint32_t crc = (crctot ^ crc2);
76  for(size_t i=0;i<8*len2;i++) {
77  crc = (crc<<1)^((crc&0x80000000) ? (CrcPoly << 1 | 0x1) : 0);
78  }
79  return crc;
80  }
81 
82  // crc32c_split2
83  //
84  // crctot: crc32c of data1|data2
85  // crc1: crc32c of data1
86  // len2: length of data2
87  //
88  // returns crc of data2
89  // note: using Calc32C: some optimisation could be made
90  static uint32_t crc32c_split2(uint32_t crctot, uint32_t crc1, size_t len2)
91  {
92  if (len2==0)
93  return 0;
94 
95  assert(len2<=XrdSys::PageSize);
96  uint32_t c1 = XrdOucCRC::Calc32C(g_bz, len2, ~crc1);
97  return ~c1^crctot;
98  }
99 
100  // crc32c_extendwith_zero
101  //
102  // crc: crc32c of data
103  // len: number of zero bytes to append
104  //
105  // returns crc of data|[0x00 x len]
106  // note: using Calc32C: some optimisation could be made
107  static uint32_t crc32c_extendwith_zero(uint32_t crc, size_t len)
108  {
109  if (len==0)
110  return crc;
111 
112  assert(len<=XrdSys::PageSize);
113  return XrdOucCRC::Calc32C(g_bz, len, crc);
114  }
115 
116 private:
117 
118  static const uint8_t g_bz[XrdSys::PageSize];
119 
120  // CRC-32C (iSCSI) polynomial in reversed bit order.
121  static const uint32_t CrcPoly = 0x82F63B78;
122 };
123 
124 #endif
static uint32_t crc32c_extendwith_zero(uint32_t crc, size_t len)
static uint32_t crc32c_combine(uint32_t crc1, uint32_t crc2, size_t len2)
static uint32_t crc32c_split1(uint32_t crctot, uint32_t crc2, size_t len2)
static uint32_t crc32c_split2(uint32_t crctot, uint32_t crc1, size_t len2)
static uint32_t Calc32C(const void *data, size_t count, uint32_t prevcs=0)
Definition: XrdOucCRC.cc:190
static const int PageSize