PixelLightAPI
.
|
00001 /*********************************************************\ 00002 * File: Checksum.h * 00003 * 00004 * Copyright (C) 2002-2012 The PixelLight Team (http://www.pixellight.org/) 00005 * 00006 * This file is part of PixelLight. 00007 * 00008 * PixelLight is free software: you can redistribute it and/or modify 00009 * it under the terms of the GNU Lesser General Public License as published by 00010 * the Free Software Foundation, either version 3 of the License, or 00011 * (at your option) any later version. 00012 * 00013 * PixelLight is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 * GNU Lesser General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public License 00019 * along with PixelLight. If not, see <http://www.gnu.org/licenses/>. 00020 \*********************************************************/ 00021 00022 00023 #ifndef __PLCORE_CHECKSUM_H__ 00024 #define __PLCORE_CHECKSUM_H__ 00025 #pragma once 00026 00027 00028 //[-------------------------------------------------------] 00029 //[ Includes ] 00030 //[-------------------------------------------------------] 00031 #include "PLCore/String/String.h" 00032 00033 00034 //[-------------------------------------------------------] 00035 //[ Namespace ] 00036 //[-------------------------------------------------------] 00037 namespace PLCore { 00038 00039 00040 //[-------------------------------------------------------] 00041 //[ Forward declarations ] 00042 //[-------------------------------------------------------] 00043 class File; 00044 00045 00046 //[-------------------------------------------------------] 00047 //[ Classes ] 00048 //[-------------------------------------------------------] 00049 /** 00050 * @brief 00051 * Abstract checksum ("digital fingerprint") base class 00052 * 00053 * @remarks 00054 * Normally a checksum is used to detect errors after transmission or storage. Before transmission 00055 * or storage of data, a checksum of the data is calculated and send/saved. Afterwards the received 00056 * data is verified by using the checksum to confirm that no changes occurred on transit. 00057 * 00058 * @note 00059 * - Each time a checksum is calculated, an instance of the class is created and destroyed at the end 00060 */ 00061 class Checksum { 00062 00063 00064 //[-------------------------------------------------------] 00065 //[ Public functions ] 00066 //[-------------------------------------------------------] 00067 public: 00068 /** 00069 * @brief 00070 * Returns the checksum of a given buffer 00071 * 00072 * @param[in] pnBuffer 00073 * Buffer to create the checksum from (MUST be valid!) 00074 * @param[in] nNumOfBytes 00075 * Number of bytes of the given buffer (MUST be valid!) 00076 * 00077 * @return 00078 * The checksum of the given buffer, empty on error (maybe empty buffer?) 00079 */ 00080 PLCORE_API String Get(const uint8 *pnBuffer, uint32 nNumOfBytes); 00081 00082 /** 00083 * @brief 00084 * Returns the checksum of a given string 00085 * 00086 * @param[in] sString 00087 * String to create the checksum from, the original internal format is used - 00088 * if it's a Unicode string, Unicode will be used to get the checksum 00089 * 00090 * @return 00091 * The checksum of the given string, empty on error (maybe unknown class?) 00092 * 00093 * @note 00094 * - sString: "My text" != L"My text" (ASCII != Unicode even if the text itself is the same!) 00095 */ 00096 PLCORE_API String Get(const String &sString); 00097 00098 /** 00099 * @brief 00100 * Returns the checksum of a given file 00101 * 00102 * @param[in] cFile 00103 * File to create the checksum from (MUST be opened and readable!) 00104 * 00105 * @return 00106 * The checksum of the given file, empty string on error 00107 * (maybe unknown class or the given file is invalid?) 00108 * 00109 * @note 00110 * - The current file offset is not changed 00111 */ 00112 PLCORE_API String Get(File &cFile); 00113 00114 /** 00115 * @brief 00116 * Returns the checksum of a given file 00117 * 00118 * @param[in] sFilename 00119 * Name of the file to create the checksum from 00120 * 00121 * @return 00122 * The checksum of the given file, empty on error (maybe unknown class or invalid file?) 00123 */ 00124 PLCORE_API String GetFile(const String &sFilename); 00125 00126 00127 //[-------------------------------------------------------] 00128 //[ Protected functions ] 00129 //[-------------------------------------------------------] 00130 protected: 00131 /** 00132 * @brief 00133 * Default constructor 00134 */ 00135 PLCORE_API Checksum(); 00136 00137 /** 00138 * @brief 00139 * Destructor 00140 */ 00141 PLCORE_API virtual ~Checksum(); 00142 00143 00144 //[-------------------------------------------------------] 00145 //[ Protected virtual Checksum functions ] 00146 //[-------------------------------------------------------] 00147 protected: 00148 /** 00149 * @brief 00150 * Implementation of main checksum algorithm 00151 * 00152 * @param[in] nInput 00153 * Input block 00154 * @param[in] nInputLen 00155 * Length of input block in bytes 00156 * 00157 * @remarks 00158 * Computes the partial checksum for 'nInputLen' bytes of data in 'Input'. 00159 */ 00160 virtual void Update(const uint8 nInput[], uint32 nInputLen) = 0; 00161 00162 /** 00163 * @brief 00164 * Implementation of main checksum algorithm; ends the checksum calculation. 00165 * 00166 * @return 00167 * The final hexadecimal checksum result 00168 * (without the 0x prefix and with lower case hex characters) 00169 * 00170 * @remarks 00171 * Performs the final checksum calculation. 'Update' does most of the work, 00172 * this function just finishes the calculation. 00173 */ 00174 virtual String Final() = 0; 00175 00176 00177 //[-------------------------------------------------------] 00178 //[ Private functions ] 00179 //[-------------------------------------------------------] 00180 private: 00181 /** 00182 * @brief 00183 * Copy constructor 00184 * 00185 * @param[in] cSource 00186 * Source to copy from 00187 */ 00188 Checksum(const Checksum &cSource); 00189 00190 /** 00191 * @brief 00192 * Copy operator 00193 * 00194 * @param[in] cSource 00195 * Source to copy from 00196 * 00197 * @return 00198 * Reference to this instance 00199 */ 00200 Checksum &operator =(const Checksum &cSource); 00201 00202 00203 }; 00204 00205 00206 //[-------------------------------------------------------] 00207 //[ Namespace ] 00208 //[-------------------------------------------------------] 00209 } // PLCore 00210 00211 00212 #endif // __PLCORE_CHECKSUM_H__
|