hashsetmaker.h

Go to the documentation of this file.
00001 /*
00002  *  Copyright (C) 2004-2005 Alo Sarv <madcat_@users.sourceforge.net>
00003  *
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with this program; if not, write to the Free Software
00016  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017  */
00018 
00019 /**
00020  * \file hashsetmaker.h
00021  * Interface for various HashSet generators used in P2P networks
00022  *
00023  * These classes are used internally by Files Identification Subsystem and
00024  * shouldn't be needed by plugins.
00025  */
00026 
00027 #ifndef __HASHSETMAKER_H__
00028 #define __HASHSETMAKER_H__
00029 
00030 #include <hn/hash.h>
00031 #include <hn/osdep.h>
00032 #include <hn/md4transform.h>
00033 #include <hn/md5transform.h>
00034 #include <hn/sha1transform.h>
00035 
00036              /* --------------------------------------------------- */
00037              /* Second level of FIS - HashSets used in P2P Networks */
00038              /* --------------------------------------------------- */
00039 
00040 /**
00041  * Abstract base for HashSet maker
00042  */
00043 class DLLEXPORT HashSetMaker {
00044 public:
00045         HashSetMaker();
00046         virtual ~HashSetMaker();
00047         virtual void sumUp(const char *data, uint32_t length) = 0;
00048         virtual HashSetBase* getHashSet() = 0;
00049 private:
00050         HashSetMaker(const HashSetMaker&);
00051         HashSetMaker& operator=(const HashSetMaker&);
00052 };
00053 
00054 // Implementation of Concrete HashSet Makers
00055 // -----------------------------------------
00056 
00057 /**
00058  * Generates ED2KHashSet. Uses MD4Hasher internally.
00059  */
00060 class DLLEXPORT ED2KHashMaker : public HashSetMaker {
00061 public:
00062         //! Constructor
00063         ED2KHashMaker();
00064 
00065         //! Destructor
00066         virtual ~ED2KHashMaker();
00067 
00068         /**
00069          * Continue ED2KHashset generation
00070          *
00071          * @param data      Data to be hashed
00072          * @param length    Length of passed data
00073          */
00074         virtual void sumUp(const char *data, uint32_t length);
00075 
00076         /**
00077          * @return      Resulting hashset, downcasted to abstract base class
00078          *
00079          * @return      The resulting hashset
00080          */
00081         virtual HashSetBase* getHashSet();
00082 private:
00083         //! If HashSet generation has been completed.
00084         bool m_completed;
00085 
00086         //! Md4 transformer pointer used for checksumming
00087         Md4Transform *m_transformer;
00088 
00089         //! Temporary counter used to detect chunk size hits
00090         uint32_t m_dataCount;
00091 
00092         //! Gets filled with part hashes
00093         std::vector< Hash<MD4Hash> > m_chunks;
00094 
00095         //! Makes life simpler
00096         typedef std::vector< Hash<MD4Hash> >::iterator Iter;
00097 
00098         //! Receives final file hash
00099         Hash<MD4Hash> m_fileHash;
00100 };
00101 
00102 /**
00103  * Generates BTHashSet, with varying chunksize. Uses Sha1Transformer internally.
00104  * BTHashSet is a tricky one - it doesn't have a file hash at all, to the
00105  * best of my knowledge. And the chunk size varies. As such, we cannot generate
00106  * BTHashSet by default at all - only on specific request.
00107  */
00108 class DLLEXPORT BTHashMaker : public HashSetMaker {
00109 public:
00110         /**
00111          * Constructor
00112          *
00113          * @param chunkSize     Size of one piece/chunk/part
00114          */
00115         BTHashMaker(uint32_t chunkSize);
00116 
00117         //! Destructor
00118         virtual ~BTHashMaker();
00119 
00120         /**
00121          * Continue hash calculation.
00122          *
00123          * @param data      Data to be hashed. May not be 0.
00124          * @param length    Length of data. May not be 0.
00125          */
00126         virtual void sumUp(const char *data, uint32_t length);
00127 
00128         /**
00129          * @return       Resulting hashset, downcasted to abstract base class
00130          *
00131          * @return       Pointer to newly allocated HashSet object
00132          */
00133         virtual HashSetBase* getHashSet();
00134 private:
00135         BTHashMaker();      //!< Default constructor protected - need chunkSize
00136 
00137         //! If hashset generation has been completed
00138         bool m_completed;
00139         //! Transformer used for calculation
00140         Sha1Transform *m_transformer;
00141         //! Receives part hashes
00142         std::vector< Hash<SHA1Hash> > m_partHashes;
00143         //! Makes life simpler
00144         typedef std::vector< Hash<SHA1Hash> >::iterator Iter;
00145         //! Size of one chunk
00146         const uint32_t m_chunkSize;
00147 };
00148 
00149 /**
00150  * Generates MD5 file hash, no part hashes.
00151  */
00152 class DLLEXPORT MD5HashMaker : public HashSetMaker {
00153 public:
00154         //! Constructor
00155         MD5HashMaker();
00156         //! Destructor
00157         virtual ~MD5HashMaker();
00158         //! Add more data for transforming
00159         virtual void sumUp(const char *data, uint32_t length);
00160         // Retrieve results.
00161         virtual HashSetBase* getHashSet();
00162 private:
00163         bool m_completed;
00164         Md5Transform m_transformer;
00165 };
00166 
00167 /**
00168  * Generates SHA-1 file hash, no part hashes.
00169  */
00170 class DLLEXPORT SHA1HashMaker : public HashSetMaker {
00171 public:
00172         //! Constructor
00173         SHA1HashMaker();
00174         //! Destructor
00175         virtual ~SHA1HashMaker();
00176         //! Add more data
00177         virtual void sumUp(const char *data, uint32_t length);
00178         //! Retrieve results
00179         virtual HashSetBase* getHashSet();
00180 private:
00181         bool m_completed;
00182         Sha1Transform m_transformer;
00183 };
00184 
00185 /**
00186  * Generates MD4 file hash, no part hashes
00187  */
00188 class DLLEXPORT MD4HashMaker : public HashSetMaker {
00189 public:
00190         MD4HashMaker();
00191         virtual ~MD4HashMaker();
00192         virtual void sumUp(const char *data, uint32_t length);
00193         virtual HashSetBase* getHashSet();
00194 private:
00195         bool m_completed;
00196         Md4Transform m_transformer;
00197 };
00198 
00199 #endif