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 
00013 #include <sys/types.h>
00014 #include <netinet/in.h>
00015 #include <inttypes.h>
00016 
00017 #include "XrdCks/XrdCksCalc.hh"
00018 #include "XrdSys/XrdSysPlatform.hh"
00019 
00020 /* The following implementation of adler32 was derived from zlib and is
00021                    * Copyright (C) 1995-1998 Mark Adler
00022    Below are the zlib license terms for this implementation.
00023 */
00024   
00025 /* zlib.h -- interface of the 'zlib' general purpose compression library
00026   version 1.1.4, March 11th, 2002
00027 
00028   Copyright (C) 1995-2002 Jean-loup Gailly and Mark Adler
00029 
00030   This software is provided 'as-is', without any express or implied
00031   warranty.  In no event will the authors be held liable for any damages
00032   arising from the use of this software.
00033 
00034   Permission is granted to anyone to use this software for any purpose,
00035   including commercial applications, and to alter it and redistribute it
00036   freely, subject to the following restrictions:
00037 
00038   1. The origin of this software must not be misrepresented; you must not
00039      claim that you wrote the original software. If you use this software
00040      in a product, an acknowledgment in the product documentation would be
00041      appreciated but is not required.
00042   2. Altered source versions must be plainly marked as such, and must not be
00043      misrepresented as being the original software.
00044   3. This notice may not be removed or altered from any source distribution.
00045 
00046   Jean-loup Gailly        Mark Adler
00047   jloup@gzip.org          madler@alumni.caltech.edu
00048 
00049 
00050   The data format used by the zlib library is described by RFCs (Request for
00051   Comments) 1950 to 1952 in the files ftp://ds.internic.net/rfc/rfc1950.txt
00052   (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format).
00053 */
00054 
00055 #define DO1(buf)  {unSum1 += *buf++; unSum2 += unSum1;}
00056 #define DO2(buf)  DO1(buf); DO1(buf);
00057 #define DO4(buf)  DO2(buf); DO2(buf);
00058 #define DO8(buf)  DO4(buf); DO4(buf);
00059 #define DO16(buf) DO8(buf); DO8(buf);
00060 
00061 class XrdCksCalcadler32 : public XrdCksCalc
00062 {
00063 public:
00064 
00065 char *Final()
00066             {AdlerValue = (unSum2 << 16) | unSum1;
00067 #ifndef Xrd_Big_Endian
00068              AdlerValue = htonl(AdlerValue);
00069 #endif
00070              return (char *)&AdlerValue;
00071             }
00072 
00073 void        Init() {unSum1 = AdlerStart; unSum2 = 0;}
00074 
00075 XrdCksCalc *New() {return (XrdCksCalc *)new XrdCksCalcadler32;}
00076 
00077 void        Update(const char *Buff, int BLen)
00078                   {int k;
00079                    unsigned char *buff = (unsigned char *)Buff;
00080                    while(BLen > 0)
00081                         {k = (BLen < AdlerNMax ? BLen : AdlerNMax);
00082                          BLen -= k;
00083                          while(k >= 16) {DO16(buff); k -= 16;}
00084                          if (k != 0) do {DO1(buff);} while (--k);
00085                          unSum1 %= AdlerBase; unSum2 %= AdlerBase;
00086                         }
00087                   }
00088 
00089 const char *Type(int &csSize) {csSize = sizeof(AdlerResult); return "adler32";}
00090 
00091             XrdCksCalcadler32() {Init();}
00092 virtual    ~XrdCksCalcadler32() {}
00093 
00094 private:
00095 
00096 static const unsigned int AdlerBase  = 0xFFF1;
00097 static const unsigned int AdlerStart = 0x0001;
00098 static const          int AdlerNMax  = 5552;
00099 
00100 /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
00101 
00102              unsigned int AdlerResult;
00103              unsigned int AdlerValue;
00104              unsigned int unSum1;
00105              unsigned int unSum2;
00106                       int n;
00107 };
00108 #endif