File indexing completed on 2024-04-28 16:08:40

0001 /***************************************************************************
0002  *   Copyright (C) 2013 by Linuxstopmotion contributors;                   *
0003  *   see the AUTHORS file for details.                                     *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
0019  ***************************************************************************/
0020 
0021 #ifndef HASH_H_
0022 #define HASH_H_
0023 
0024 #include <stdint.h>
0025 #include <stdio.h>
0026 #include <string>
0027 
0028 /**
0029  * Very simple hashing function by Professor Daniel J Bernstein.
0030  * @par
0031  * Use this to calculate hashes for @ref ModelTestHelper::HashModel.
0032  * @note
0033  * If your model has some sort of hierarchical structure, don't just add all
0034  * the leaf parts to the hash as this will fail to hash the structure. Instead,
0035  * hash each part and combine these parts into the whole's hash. For each
0036  * part's hash, hash each sub-part and combine the hashes together into the
0037  * part's hash. Using hashes recursively in this way will allow the structure
0038  * as well as its contents to be hashed.
0039  */
0040 class Hash {
0041     uint64_t h;
0042 public:
0043     Hash();
0044     Hash(const Hash&);
0045     Hash& operator=(const Hash&);
0046     void add(uint64_t n);
0047     void addS(int64_t n);
0048     void add(const char* string);
0049     void add(Hash h)
0050     /**
0051      * Add the contents of a file to the hash.
0052      * @param fh The file handle to the file (which must be open for reading).
0053      * The seek position will be reset afterwards if there is no error reading.
0054      */;
0055     void add(FILE* fh);
0056     bool equals(const Hash& other) const;
0057     /**
0058      * Appends the 16-character hex representation of the value of this hash to
0059      * the string.
0060      * @param out The string to be appended.
0061      */
0062     void appendTo(std::string& out);
0063 };
0064 
0065 bool operator==(const Hash& a, const Hash& b);
0066 bool operator!=(const Hash& a, const Hash& b);
0067 
0068 #endif /* HASH_H_ */