XRootD
Loading...
Searching...
No Matches
XrdCksCalcadler32.hh
Go to the documentation of this file.
1#ifndef __XRDCKSCALCADLER32_HH__
2#define __XRDCKSCALCADLER32_HH__
3/******************************************************************************/
4/* */
5/* X r d C k s C a l c a d l e r 3 2 . h h */
6/* */
7/* (c) 2011 by the Board of Trustees of the Leland Stanford, Jr., University */
8/* All Rights Reserved */
9/* Produced by Andrew Hanushevsky for Stanford University under contract */
10/* DE-AC02-76-SFO0515 with the Department of Energy */
11/* */
12/* This file is part of the XRootD software suite. */
13/* */
14/* XRootD is free software: you can redistribute it and/or modify it under */
15/* the terms of the GNU Lesser General Public License as published by the */
16/* Free Software Foundation, either version 3 of the License, or (at your */
17/* option) any later version. */
18/* */
19/* XRootD is distributed in the hope that it will be useful, but WITHOUT */
20/* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
21/* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
22/* License for more details. */
23/* */
24/* You should have received a copy of the GNU Lesser General Public License */
25/* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
26/* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
27/* */
28/* The copyright holder's institutional names and contributor's names may not */
29/* be used to endorse or promote products derived from this software without */
30/* specific prior written permission of the institution or contributor. */
31/******************************************************************************/
32
33#include <sys/types.h>
34#include <netinet/in.h>
35#include <cinttypes>
36#include <zlib.h>
37
38#include "XrdCks/XrdCksCalc.hh"
40
41/* The following implementation of adler32 was derived from zlib and is
42 * Copyright (C) 1995-1998 Mark Adler
43 Below are the zlib license terms for this implementation.
44*/
45
46/* zlib.h -- interface of the 'zlib' general purpose compression library
47 version 1.1.4, March 11th, 2002
48
49 Copyright (C) 1995-2002 Jean-loup Gailly and Mark Adler
50
51 This software is provided 'as-is', without any express or implied
52 warranty. In no event will the authors be held liable for any damages
53 arising from the use of this software.
54
55 Permission is granted to anyone to use this software for any purpose,
56 including commercial applications, and to alter it and redistribute it
57 freely, subject to the following restrictions:
58
59 1. The origin of this software must not be misrepresented; you must not
60 claim that you wrote the original software. If you use this software
61 in a product, an acknowledgment in the product documentation would be
62 appreciated but is not required.
63 2. Altered source versions must be plainly marked as such, and must not be
64 misrepresented as being the original software.
65 3. This notice may not be removed or altered from any source distribution.
66
67 Jean-loup Gailly Mark Adler
68 jloup@gzip.org madler@alumni.caltech.edu
69
70
71 The data format used by the zlib library is described by RFCs (Request for
72 Comments) 1950 to 1952 in the files ftp://ds.internic.net/rfc/rfc1950.txt
73 (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format).
74*/
75
76#define DO1(buf) {unSum1 += *buf++; unSum2 += unSum1;}
77#define DO2(buf) DO1(buf); DO1(buf);
78#define DO4(buf) DO2(buf); DO2(buf);
79#define DO8(buf) DO4(buf); DO4(buf);
80#define DO16(buf) DO8(buf); DO8(buf);
81
83{
84public:
85
86bool Combinable() override {return true;}
87
88const char* Combine(const char *Cksum, int DLen) override
89 {uint32_t adler2 = getCS(Cksum);
90 uint32_t adler1 = (unSum2 << 16) | unSum1;
91 uLong newcs = adler32_combine(adler1, adler2, DLen);
92 unSum1 = newcs & 0xffff;
93 unSum2 = (newcs >> 16) & 0xffff;
94 return Final();
95 }
96
97const char* Combine(const char* Cksum1, const char* Cksum2, int DLen) override
98 {uint32_t adler1 = getCS(Cksum1);
99 uint32_t adler2 = getCS(Cksum2);
100 AdlerValue = (uint32_t)adler32_combine(adler1, adler2, DLen);
101#ifndef Xrd_Big_Endian
102 AdlerValue = htonl(AdlerValue);
103#endif
104 return (const char *)&AdlerValue;
105 }
106
107char *Final() override
108 {AdlerValue = (unSum2 << 16) | unSum1;
109#ifndef Xrd_Big_Endian
110 AdlerValue = htonl(AdlerValue);
111#endif
112 return (char *)&AdlerValue;
113 }
114
115void Init() override {unSum1 = AdlerStart; unSum2 = 0;}
116
117XrdCksCalc *New() override {return (XrdCksCalc *)new XrdCksCalcadler32;}
118
119void Update(const char *Buff, int BLen) override
120 {int k;
121 unsigned char *buff = (unsigned char *)Buff;
122 while(BLen > 0)
123 {k = (BLen < AdlerNMax ? BLen : AdlerNMax);
124 BLen -= k;
125 while(k >= 16) {DO16(buff); k -= 16;}
126 if (k != 0) do {DO1(buff);} while (--k);
127 unSum1 %= AdlerBase; unSum2 %= AdlerBase;
128 }
129 }
130
131const char *Type(int &csSize) override
132 {csSize = sizeof(AdlerValue); return "adler32";}
133
136
137private:
138
139uint32_t getCS(const char* csVal)
140 {uint32_t aVal;
141 memcpy(&aVal, csVal, sizeof(aVal));
142#ifndef Xrd_Big_Endian
143 aVal = ntohl(aVal);
144#endif
145 return aVal;
146 }
147
148static const uint32_t AdlerBase = 0xFFF1;
149static const uint32_t AdlerStart = 0x0001;
150static const int AdlerNMax = 5552;
151
152/* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
153
154 uint32_t AdlerValue;
155 uint32_t unSum1;
156 uint32_t unSum2;
157};
158#endif
#define DO16(buf)
#define DO1(buf)
XrdCksCalc()
Constructor.
const char * Combine(const char *Cksum1, const char *Cksum2, int DLen) override
bool Combinable() override
const char * Combine(const char *Cksum, int DLen) override
const char * Type(int &csSize) override
char * Final() override
void Update(const char *Buff, int BLen) override
XrdCksCalc * New() override