GpCoreTools/FletcherChecksum.h
Go to the documentation of this file.
00001 /***************************************************************************
00002 **
00003 **  This file is part of GpCoreTools.
00004 **
00005 **  This file may be distributed and/or modified under the terms of the
00006 **  GNU General Public License version 2 or 3 as published by the Free
00007 **  Software Foundation and appearing in the file LICENSE.GPL included
00008 **  in the packaging of this file.
00009 **
00010 **  This file is distributed in the hope that it will be useful, but WITHOUT
00011 **  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 **  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
00013 **  more details.
00014 **
00015 **  You should have received a copy of the GNU General Public License
00016 **  along with this program. If not, see <http://www.gnu.org/licenses/>.
00017 **
00018 **  See http://www.geopsy.org for more information.
00019 **
00020 **  Created: 2009-06-26
00021 **  Authors:
00022 **    Marc Wathelet
00023 **    Marc Wathelet (LGIT, Grenoble, France)
00024 **
00025 ***************************************************************************/
00026 
00027 #ifndef FLETCHERCHECKSUM_H
00028 #define FLETCHERCHECKSUM_H
00029 
00030 #include <stdio.h>
00031 
00032 #include "GpCoreToolsDLLExport.h"
00033 
00034 namespace GpCoreTools {
00035 
00036   class GPCORETOOLS_EXPORT FletcherChecksum
00037   {
00038   public:
00039     FletcherChecksum() {_values.a=0; _values.b=0;}
00040     FletcherChecksum(unsigned char a, unsigned char b) {_values.a=a; _values.b=b;}
00041     FletcherChecksum(const FletcherChecksum& o) {_raw=o._raw;}
00042 
00043     bool operator==(const FletcherChecksum& o) const {return _raw==o._raw;}
00044 
00045     void reset() {_values.a=0; _values.b=0;}
00046     inline void add(const char * buffer, int length);
00047 
00048     unsigned char a() const {return _values.a;}
00049     unsigned char b() const {return _values.b;}
00050   private:
00051     union {
00052       struct {
00053         unsigned char a, b;
00054       } _values;
00055       unsigned char _bytes[2];
00056       unsigned short _raw;
00057     };
00058   };
00059 
00060   inline void FletcherChecksum::add(const char * buffer, int length)
00061   {
00062     for(int i=0; i<length; i++) {
00063       _values.a+=buffer[i];
00064       _values.b+=_values.a;
00065     }
00066   }
00067 
00068 } // namespace GpCoreTools
00069 
00070 #endif // FLETCHERCHECKSUM_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines