Stefan Scholz / ETL
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers checksum.h Source File

checksum.h

Go to the documentation of this file.
00001 
00002 ///\file
00003 
00004 /******************************************************************************
00005 The MIT License(MIT)
00006 Embedded Template Library.
00007 https://github.com/ETLCPP/etl
00008 http://www.etlcpp.com
00009 Copyright(c) 2014 jwellbelove
00010 Permission is hereby granted, free of charge, to any person obtaining a copy
00011 of this software and associated documentation files(the "Software"), to deal
00012 in the Software without restriction, including without limitation the rights
00013 to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
00014 copies of the Software, and to permit persons to whom the Software is
00015 furnished to do so, subject to the following conditions :
00016 The above copyright notice and this permission notice shall be included in all
00017 copies or substantial portions of the Software.
00018 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00019 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00020 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
00021 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00022 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00023 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00024 SOFTWARE.
00025 ******************************************************************************/
00026 
00027 #ifndef __ETL_CHECKSUM__
00028 #define __ETL_CHECKSUM__
00029 
00030 #include <stdint.h>
00031 
00032 #include "platform.h "
00033 #include "binary.h "
00034 #include "frame_check_sequence.h "
00035 
00036 ///\defgroup checksum Checksum calculation
00037 ///\ingroup maths
00038 
00039 namespace etl
00040 {
00041   //***************************************************************************
00042   /// Standard addition checksum policy.
00043   //***************************************************************************
00044   template <typename T>
00045   struct checksum_policy_sum
00046   {
00047     typedef T value_type;
00048 
00049     inline T initial() const
00050     {
00051       return 0;
00052     }
00053 
00054     inline T add(T sum, uint8_t value) const
00055     {
00056       return sum + value;
00057     }
00058 
00059     inline T final(T sum) const
00060     {
00061       return sum;
00062     }
00063   };
00064 
00065   //***************************************************************************
00066   /// BSD checksum policy.
00067   //***************************************************************************
00068   template <typename T>
00069   struct checksum_policy_bsd
00070   {
00071     typedef T value_type;
00072 
00073     inline T initial() const
00074     {
00075       return 0;
00076     }
00077 
00078     inline T add(T sum, uint8_t value) const
00079     {
00080       return etl::rotate_right(sum) + value;
00081     }
00082 
00083     inline T final(T sum) const
00084     {
00085       return sum;
00086     }
00087   };
00088 
00089   //***************************************************************************
00090   /// Standard XOR checksum policy.
00091   //***************************************************************************
00092   template <typename T>
00093   struct checksum_policy_xor
00094   {
00095     typedef T value_type;
00096 
00097     inline T initial() const
00098     {
00099       return 0;
00100     }
00101 
00102     inline T add(T sum, uint8_t value) const
00103     {
00104       return sum ^ value;
00105     }
00106 
00107     inline T final(T sum) const
00108     {
00109       return sum;
00110     }
00111   };
00112 
00113   //***************************************************************************
00114   /// XOR-rotate checksum policy.
00115   //***************************************************************************
00116   template <typename T>
00117   struct checksum_policy_xor_rotate
00118   {
00119     typedef T value_type;
00120 
00121     inline T initial() const
00122     {
00123       return 0;
00124     }
00125 
00126     inline T add(T sum, uint8_t value) const
00127     {
00128       return etl::rotate_left(sum) ^ value;
00129     }
00130 
00131     inline T final(T sum) const
00132     {
00133       return sum;
00134     }
00135   };
00136 
00137   //*************************************************************************
00138   /// Standard Checksum.
00139   //*************************************************************************
00140   template <typename T>
00141   class checksum : public etl::frame_check_sequence<etl::checksum_policy_sum<T> >
00142   {
00143   public:
00144 
00145     //*************************************************************************
00146     /// Default constructor.
00147     //*************************************************************************
00148     checksum()
00149     {
00150       this->reset();
00151     }
00152 
00153     //*************************************************************************
00154     /// Constructor from range.
00155     /// \param begin Start of the range.
00156     /// \param end   End of the range.
00157     //*************************************************************************
00158     template<typename TIterator>
00159     checksum(TIterator begin, const TIterator end)
00160     {
00161       this->reset();
00162       this->add(begin, end);
00163     }
00164   };
00165 
00166   //*************************************************************************
00167   /// BSD Checksum.
00168   //*************************************************************************
00169   template <typename T>
00170   class bsd_checksum : public etl::frame_check_sequence<etl::checksum_policy_bsd<T> >
00171   {
00172   public:
00173 
00174     //*************************************************************************
00175     /// Default constructor.
00176     //*************************************************************************
00177     bsd_checksum()
00178     {
00179       this->reset();
00180     }
00181 
00182     //*************************************************************************
00183     /// Constructor from range.
00184     /// \param begin Start of the range.
00185     /// \param end   End of the range.
00186     //*************************************************************************
00187     template<typename TIterator>
00188     bsd_checksum(TIterator begin, const TIterator end)
00189     {
00190       this->reset();
00191       this->add(begin, end);
00192     }
00193   };
00194 
00195   //*************************************************************************
00196   /// XOR Checksum.
00197   //*************************************************************************
00198   template <typename T>
00199   class xor_checksum : public etl::frame_check_sequence<etl::checksum_policy_xor<T> >
00200   {
00201   public:
00202 
00203     //*************************************************************************
00204     /// Default constructor.
00205     //*************************************************************************
00206     xor_checksum()
00207     {
00208       this->reset();
00209     }
00210 
00211     //*************************************************************************
00212     /// Constructor from range.
00213     /// \param begin Start of the range.
00214     /// \param end   End of the range.
00215     //*************************************************************************
00216     template<typename TIterator>
00217     xor_checksum(TIterator begin, const TIterator end)
00218     {
00219       this->reset();
00220       this->add(begin, end);
00221     }
00222   };
00223 
00224   //*************************************************************************
00225   /// XOR-shift Checksum.
00226   //*************************************************************************
00227   template <typename T>
00228   class xor_rotate_checksum : public etl::frame_check_sequence<etl::checksum_policy_xor_rotate<T> >
00229   {
00230   public:
00231 
00232     //*************************************************************************
00233     /// Default constructor.
00234     //*************************************************************************
00235     xor_rotate_checksum()
00236     {
00237       this->reset();
00238     }
00239 
00240     //*************************************************************************
00241     /// Constructor from range.
00242     /// \param begin Start of the range.
00243     /// \param end   End of the range.
00244     //*************************************************************************
00245     template<typename TIterator>
00246     xor_rotate_checksum(TIterator begin, const TIterator end)
00247     {
00248       this->reset();
00249       this->add(begin, end);
00250     }
00251   };
00252 }
00253 
00254 #endif
00255