xrootd
XrdCksCalcadler32.hh
Go to the documentation of this file.
00001 #ifndef __XRDCKSCALCADLER32_HH__
00002 #define __XRDCKSCALCADLER32_HH__
00003 /******************************************************************************/
00004 /*                                                                            */
00005 /*                  X r d C k s C a l c a d l e r 3 2 . h h                   */
00006 /*                                                                            */
00007 /* (c) 2011 by the Board of Trustees of the Leland Stanford, Jr., University  */
00008 /*                            All Rights Reserved                             */
00009 /*   Produced by Andrew Hanushevsky for Stanford University under contract    */
00010 /*              DE-AC02-76-SFO0515 with the Department of Energy              */
00011 /*                                                                            */
00012 /* This file is part of the XRootD software suite.                            */
00013 /*                                                                            */
00014 /* XRootD is free software: you can redistribute it and/or modify it under    */
00015 /* the terms of the GNU Lesser General Public License as published by the     */
00016 /* Free Software Foundation, either version 3 of the License, or (at your     */
00017 /* option) any later version.                                                 */
00018 /*                                                                            */
00019 /* XRootD is distributed in the hope that it will be useful, but WITHOUT      */
00020 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or      */
00021 /* FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public       */
00022 /* License for more details.                                                  */
00023 /*                                                                            */
00024 /* You should have received a copy of the GNU Lesser General Public License   */
00025 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file  */
00026 /* COPYING (GPL license).  If not, see <http://www.gnu.org/licenses/>.        */
00027 /*                                                                            */
00028 /* The copyright holder's institutional names and contributor's names may not */
00029 /* be used to endorse or promote products derived from this software without  */
00030 /* specific prior written permission of the institution or contributor.       */
00031 /******************************************************************************/
00032 
00033 #include <sys/types.h>
00034 #include <netinet/in.h>
00035 #include <inttypes.h>
00036 
00037 #include "XrdCks/XrdCksCalc.hh"
00038 #include "XrdSys/XrdSysPlatform.hh"
00039 
00040 /* The following implementation of adler32 was derived from zlib and is
00041                    * Copyright (C) 1995-1998 Mark Adler
00042    Below are the zlib license terms for this implementation.
00043 */
00044   
00045 /* zlib.h -- interface of the 'zlib' general purpose compression library
00046   version 1.1.4, March 11th, 2002
00047 
00048   Copyright (C) 1995-2002 Jean-loup Gailly and Mark Adler
00049 
00050   This software is provided 'as-is', without any express or implied
00051   warranty.  In no event will the authors be held liable for any damages
00052   arising from the use of this software.
00053 
00054   Permission is granted to anyone to use this software for any purpose,
00055   including commercial applications, and to alter it and redistribute it
00056   freely, subject to the following restrictions:
00057 
00058   1. The origin of this software must not be misrepresented; you must not
00059      claim that you wrote the original software. If you use this software
00060      in a product, an acknowledgment in the product documentation would be
00061      appreciated but is not required.
00062   2. Altered source versions must be plainly marked as such, and must not be
00063      misrepresented as being the original software.
00064   3. This notice may not be removed or altered from any source distribution.
00065 
00066   Jean-loup Gailly        Mark Adler
00067   jloup@gzip.org          madler@alumni.caltech.edu
00068 
00069 
00070   The data format used by the zlib library is described by RFCs (Request for
00071   Comments) 1950 to 1952 in the files ftp://ds.internic.net/rfc/rfc1950.txt
00072   (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format).
00073 */
00074 
00075 #define DO1(buf)  {unSum1 += *buf++; unSum2 += unSum1;}
00076 #define DO2(buf)  DO1(buf); DO1(buf);
00077 #define DO4(buf)  DO2(buf); DO2(buf);
00078 #define DO8(buf)  DO4(buf); DO4(buf);
00079 #define DO16(buf) DO8(buf); DO8(buf);
00080 
00081 class XrdCksCalcadler32 : public XrdCksCalc
00082 {
00083 public:
00084 
00085 char *Final()
00086             {AdlerValue = (unSum2 << 16) | unSum1;
00087 #ifndef Xrd_Big_Endian
00088              AdlerValue = htonl(AdlerValue);
00089 #endif
00090              return (char *)&AdlerValue;
00091             }
00092 
00093 void        Init() {unSum1 = AdlerStart; unSum2 = 0;}
00094 
00095 XrdCksCalc *New() {return (XrdCksCalc *)new XrdCksCalcadler32;}
00096 
00097 void        Update(const char *Buff, int BLen)
00098                   {int k;
00099                    unsigned char *buff = (unsigned char *)Buff;
00100                    while(BLen > 0)
00101                         {k = (BLen < AdlerNMax ? BLen : AdlerNMax);
00102                          BLen -= k;
00103                          while(k >= 16) {DO16(buff); k -= 16;}
00104                          if (k != 0) do {DO1(buff);} while (--k);
00105                          unSum1 %= AdlerBase; unSum2 %= AdlerBase;
00106                         }
00107                   }
00108 
00109 const char *Type(int &csSize) {csSize = sizeof(AdlerValue); return "adler32";}
00110 
00111             XrdCksCalcadler32() {Init();}
00112 virtual    ~XrdCksCalcadler32() {}
00113 
00114 private:
00115 
00116 static const unsigned int AdlerBase  = 0xFFF1;
00117 static const unsigned int AdlerStart = 0x0001;
00118 static const          int AdlerNMax  = 5552;
00119 
00120 /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
00121 
00122              unsigned int AdlerValue;
00123              unsigned int unSum1;
00124              unsigned int unSum2;
00125 };
00126 #endif