QGpCoreTools/FletcherChecksum.h
Go to the documentation of this file.
00001 /***************************************************************************
00002 **
00003 **  This file is part of QGpCoreTools.
00004 **
00005 **  This library is free software; you can redistribute it and/or
00006 **  modify it under the terms of the GNU Lesser General Public
00007 **  License as published by the Free Software Foundation; either
00008 **  version 2.1 of the License, or (at your option) any later version.
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 Lesser General Public
00013 **  License for more details.
00014 **
00015 **  You should have received a copy of the GNU Lesser General Public
00016 **  License along with this library; if not, write to the Free Software
00017 **  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00018 **
00019 **  
00020 **
00021 **  Created: 2012-02-08
00022 **  Authors:
00023 **    Marc Wathelet (ISTerre, Grenoble, France)
00024 **
00025 ***************************************************************************/
00026 
00027 #ifndef FLETCHERCHECKSUM_H
00028 #define FLETCHERCHECKSUM_H
00029 
00030 #include "QGpCoreToolsDLLExport.h"
00031 
00032 namespace QGpCoreTools {
00033 
00034   class QGPCORETOOLS_EXPORT FletcherChecksum
00035   {
00036   public:
00037     FletcherChecksum() {_values.a=0; _values.b=0;}
00038     FletcherChecksum(unsigned char a, unsigned char b) {_values.a=a; _values.b=b;}
00039     FletcherChecksum(const FletcherChecksum& o) {_raw=o._raw;}
00040 
00041     bool operator==(const FletcherChecksum& o) const {return _raw==o._raw;}
00042 
00043     void reset() {_values.a=0; _values.b=0;}
00044     inline void add(const char * buffer, int length);
00045 
00046     unsigned char a() const {return _values.a;}
00047     unsigned char b() const {return _values.b;}
00048   private:
00049     union {
00050       struct {
00051         unsigned char a, b;
00052       } _values;
00053       unsigned char _bytes[2];
00054       unsigned short _raw;
00055     };
00056   };
00057 
00058   inline void FletcherChecksum::add(const char * buffer, int length)
00059   {
00060     for(int i=0; i<length; i++) {
00061       _values.a+=buffer[i];
00062       _values.b+=_values.a;
00063     }
00064   }
00065 
00066 } // namespace QGpCoreTools
00067 
00068 #endif // FLETCHERCHECKSUM_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines