xrootd
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 
13 #include <sys/types.h>
14 #include <netinet/in.h>
15 #include <inttypes.h>
16 
17 #include "XrdCks/XrdCksCalc.hh"
18 #include "XrdSys/XrdSysPlatform.hh"
19 
20 /* The following implementation of adler32 was derived from zlib and is
21  * Copyright (C) 1995-1998 Mark Adler
22  Below are the zlib license terms for this implementation.
23 */
24 
25 /* zlib.h -- interface of the 'zlib' general purpose compression library
26  version 1.1.4, March 11th, 2002
27 
28  Copyright (C) 1995-2002 Jean-loup Gailly and Mark Adler
29 
30  This software is provided 'as-is', without any express or implied
31  warranty. In no event will the authors be held liable for any damages
32  arising from the use of this software.
33 
34  Permission is granted to anyone to use this software for any purpose,
35  including commercial applications, and to alter it and redistribute it
36  freely, subject to the following restrictions:
37 
38  1. The origin of this software must not be misrepresented; you must not
39  claim that you wrote the original software. If you use this software
40  in a product, an acknowledgment in the product documentation would be
41  appreciated but is not required.
42  2. Altered source versions must be plainly marked as such, and must not be
43  misrepresented as being the original software.
44  3. This notice may not be removed or altered from any source distribution.
45 
46  Jean-loup Gailly Mark Adler
47  jloup@gzip.org madler@alumni.caltech.edu
48 
49 
50  The data format used by the zlib library is described by RFCs (Request for
51  Comments) 1950 to 1952 in the files ftp://ds.internic.net/rfc/rfc1950.txt
52  (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format).
53 */
54 
55 #define DO1(buf) {unSum1 += *buf++; unSum2 += unSum1;}
56 #define DO2(buf) DO1(buf); DO1(buf);
57 #define DO4(buf) DO2(buf); DO2(buf);
58 #define DO8(buf) DO4(buf); DO4(buf);
59 #define DO16(buf) DO8(buf); DO8(buf);
60 
62 {
63 public:
64 
65 char *Final()
66  {AdlerValue = (unSum2 << 16) | unSum1;
67 #ifndef Xrd_Big_Endian
68  AdlerValue = htonl(AdlerValue);
69 #endif
70  return (char *)&AdlerValue;
71  }
72 
73 void Init() {unSum1 = AdlerStart; unSum2 = 0;}
74 
76 
77 void Update(const char *Buff, int BLen)
78  {int k;
79  unsigned char *buff = (unsigned char *)Buff;
80  while(BLen > 0)
81  {k = (BLen < AdlerNMax ? BLen : AdlerNMax);
82  BLen -= k;
83  while(k >= 16) {DO16(buff); k -= 16;}
84  if (k != 0) do {DO1(buff);} while (--k);
86  }
87  }
88 
89 const char *Type(int &csSize) {csSize = sizeof(AdlerResult); return "adler32";}
90 
92 virtual ~XrdCksCalcadler32() {}
93 
94 private:
95 
96 static const unsigned int AdlerBase = 0xFFF1;
97 static const unsigned int AdlerStart = 0x0001;
98 static const int AdlerNMax = 5552;
99 
100 /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
101 
102  unsigned int AdlerResult;
103  unsigned int AdlerValue;
104  unsigned int unSum1;
105  unsigned int unSum2;
106  int n;
107 };
108 #endif