XRootD
Loading...
Searching...
No Matches
XrdCksCalczcrc32.cc
Go to the documentation of this file.
1/******************************************************************************/
2/* */
3/* X r d C k s C a l c z c r c 3 2 . h h */
4/* */
5/* Copyright (c) 2012 by European Organization of Nuclear Research (CERN) */
6/* Produced by Lukasz Janyst <ljanyst@cern.ch> */
7/* */
8/* This file is part of the XRootD software suite. */
9/* */
10/* XRootD is free software: you can redistribute it and/or modify it under */
11/* the terms of the GNU Lesser General Public License as published by the */
12/* Free Software Foundation, either version 3 of the License, or (at your */
13/* option) any later version. */
14/* */
15/* XRootD is distributed in the hope that it will be useful, but WITHOUT */
16/* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
17/* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
18/* License for more details. */
19/* */
20/* You should have received a copy of the GNU Lesser General Public License */
21/* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
22/* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
23/* */
24/* The copyright holder's institutional names and contributor's names may not */
25/* be used to endorse or promote products derived from this software without */
26/* specific prior written permission of the institution or contributor. */
27/******************************************************************************/
28
29#include <cstdint>
30#include <cstring>
31#include <zlib.h>
32
33#include "XrdVersion.hh"
34#include "XrdCks/XrdCksCalc.hh"
35
36class XrdSysError;
37
38//------------------------------------------------------------------------------
39// CRC32 checkum according to the algorithm implemented in zlib
40// WARNING: zlib always output the result in the endian byte order that is
41// natural to the machine being used. Normally, today this is usually
42// little endian and corresponds to RFC1952 for gzip files which is
43// not particularly useful for other file formats. Use crc32 which
44// consistently outputs big endian format regardless of the hardware.
45//------------------------------------------------------------------------------
46
48{
49 public:
50
51 //--------------------------------------------------------------------------
53 //--------------------------------------------------------------------------
55 {
56 Init();
57 }
58
59 //--------------------------------------------------------------------------
61 //--------------------------------------------------------------------------
63 {
64 }
65
66 //--------------------------------------------------------------------------
68 //--------------------------------------------------------------------------
69 bool Combinable() override {return true;}
70
71 //--------------------------------------------------------------------------
73 //--------------------------------------------------------------------------
74 const char* Combine(const char* Cksum, int DLen) override
75 {
76 uint32_t crc2;
77 memcpy(&crc2, Cksum, sizeof(crc2)); // The crc32 as returned by zlib
78 pCheckSum = crc32_combine(pCheckSum, crc2, (z_off_t)DLen);
79 return (char *)&pCheckSum;
80 }
81
82 //--------------------------------------------------------------------------
84 //--------------------------------------------------------------------------
85 const char* Combine(const char* Cksum1, const char* Cksum2, int DLen)
86 override
87 {
88 uint32_t crc1, crc2;
89 memcpy(&crc1, Cksum1, sizeof(crc1)); // The crc32 as returned by zlib
90 memcpy(&crc2, Cksum2, sizeof(crc2)); // The crc32 as returned by zlib
91 pCheckSumTmp = crc32_combine(crc1, crc2, (z_off_t)DLen);
92 return (char *)&pCheckSumTmp;
93 }
94
95 //--------------------------------------------------------------------------
97 //--------------------------------------------------------------------------
98 char *Final() override
99 {
100 return (char *)&pCheckSum;
101 }
102
103 //--------------------------------------------------------------------------
105 //--------------------------------------------------------------------------
106 void Init() override
107 {
108 pCheckSum = crc32( 0L, Z_NULL, 0 );
109 }
110
111 //--------------------------------------------------------------------------
113 //--------------------------------------------------------------------------
114 XrdCksCalc *New() override
115 {
116 return new XrdCksCalczcrc32();
117 }
118
119 //--------------------------------------------------------------------------
121 //--------------------------------------------------------------------------
122 void Update( const char *Buff, int BLen ) override
123 {
124 pCheckSum = crc32( pCheckSum, (const Bytef*)Buff, BLen );
125 }
126
127 //--------------------------------------------------------------------------
129 //--------------------------------------------------------------------------
130 const char *Type(int &csSz) override
131 {
132 csSz = 4; return "zcrc32";
133 }
134
135 private:
136 uint32_t pCheckSum;
137 uint32_t pCheckSumTmp;
138};
139
140//------------------------------------------------------------------------------
141// Plugin callback
142//------------------------------------------------------------------------------
144 const char *csName,
145 const char *cFN,
146 const char *Parms)
147{
148 return new XrdCksCalczcrc32();
149}
150
XrdVERSIONINFO(XrdClGetPlugIn, XrdClGetPlugIn) extern "C"
XrdCksCalc * XrdCksCalcInit(XrdSysError *eDest, const char *csName, const char *cFN, const char *Parms)
XrdSysError eDest(0, "HttpMon")
XrdCksCalc()
Constructor.
XrdCksCalc * New() override
Virtual constructor.
XrdCksCalczcrc32()
Constructor.
const char * Type(int &csSz) override
Checksum algorithm name.
char * Final() override
Final checksum.
void Init() override
Initialize.
void Update(const char *Buff, int BLen) override
Update current checksum.
bool Combinable() override
Combinable trait.
const char * Combine(const char *Cksum, int DLen) override
Combine checksum into current checksum.
const char * Combine(const char *Cksum1, const char *Cksum2, int DLen) override
Combine two checksums and return result.
virtual ~XrdCksCalczcrc32()
Destructor.