00001 /*************************************************************************** 00002 LOW_helper_crc.h - description 00003 ------------------- 00004 begin : Sat Jul 6 2002 00005 copyright : (C) 2002 by Harald Roelle 00006 email : roelle@informatik.uni-muenchen.de 00007 ***************************************************************************/ 00008 00009 /*************************************************************************** 00010 * * 00011 * This program is free software; you can redistribute it and/or modify * 00012 * it under the terms of the GNU General Public License as published by * 00013 * the Free Software Foundation; either version 2 of the License, or * 00014 * (at your option) any later version. * 00015 * * 00016 ***************************************************************************/ 00017 00018 #ifndef LOW_HELPER_CRC_H 00019 #define LOW_HELPER_CRC_H 00020 00021 00022 00023 #include "LOW_types.h" 00024 #include "LOW_exception.h" 00025 00026 00027 /** Static functions for CRC calculation. 00028 00029 This class is thread-safe. 00030 00031 @author Harald Roelle 00032 */ 00033 class LOW_helper_CRC { 00034 00035 //======================================================================================= 00036 public: 00037 00038 //===================================================================================== 00039 // 00040 // exceptions 00041 // 00042 00043 /** Exception base class to indicate CRC errors. */ 00044 class_DERIVE_FROM_EXCEPTION( crc_error, LOW_exception); 00045 00046 00047 //===================================================================================== 00048 // 00049 // static methods 00050 // 00051 00052 /** Calculate 8 bit CRC from piece of memory. 00053 @param inBuf Pointer to memoty segment. 00054 @param inLen Length of memory segment. 00055 @param inPreloadCRC Initial CRC value. 00056 */ 00057 static const uint8_t calcCRC8( const uint8_t *inBuf, const unsigned int inLen, const uint8_t inPreloadCRC = 0); 00058 00059 /** Calculate 8 bit CRC from byte vector. 00060 @param inVec Reference to byte vector. 00061 @param inPreloadCRC Initial CRC value. 00062 */ 00063 static const uint8_t calcCRC8( const byteVec_t &inVec, const uint8_t inPreloadCRC = 0); 00064 00065 00066 /** Calculate 16 bit CRC from piece of memory. 00067 @param inBuf Pointer to memoty segment. 00068 @param inLen Length of memory segment. 00069 @param inPreloadCRC Initial CRC value. 00070 */ 00071 static const uint16_t calcCRC16( const uint8_t *inBuf, const unsigned int inLen, const uint16_t inPreloadCRC = 0); 00072 00073 /** Calculate 16 bit CRC from byte vector. 00074 @param inVec Reference to byte vector. 00075 @param inPreloadCRC Initial CRC value. 00076 */ 00077 static const uint16_t calcCRC16( const byteVec_t &inVec, const uint16_t inPreloadCRC = 0); 00078 00079 00080 //======================================================================================= 00081 private: 00082 00083 //===================================================================================== 00084 // 00085 // constructors 00086 // 00087 00088 /** Default constructor. 00089 It is private to prevent creating objects from this class as 00090 this is a static helper class. 00091 */ 00092 LOW_helper_CRC(); 00093 00094 /** Destructor. 00095 It is private to prevent creating objects from this class as 00096 this is a static helper class. 00097 */ 00098 ~LOW_helper_CRC(); 00099 00100 00101 //===================================================================================== 00102 // 00103 // constants 00104 // 00105 00106 static const uint8_t crc8Table[256]; /**< Preinitialize CRC-8 table. */ 00107 static const uint16_t crc16Table[256]; /**< Preinitialize CRC-16 table. */ 00108 00109 }; 00110 00111 #endif