diff --git a/include/Nazara/Core/AbstractHash.hpp b/include/Nazara/Core/AbstractHash.hpp index 7314b1b59..bc0a1a4a9 100644 --- a/include/Nazara/Core/AbstractHash.hpp +++ b/include/Nazara/Core/AbstractHash.hpp @@ -8,10 +8,12 @@ #define NAZARA_ABSTRACTHASH_HPP #include +#include +#include namespace Nz { - class HashDigest; + class ByteArray; class NAZARA_CORE_API AbstractHash { @@ -23,10 +25,15 @@ namespace Nz virtual void Append(const UInt8* data, unsigned int len) = 0; virtual void Begin() = 0; - virtual HashDigest End() = 0; + virtual ByteArray End() = 0; + + virtual unsigned int GetDigestLength() const = 0; + virtual const char* GetHashName() const = 0; AbstractHash& operator=(const AbstractHash&) = delete; AbstractHash& operator=(AbstractHash&&) = default; + + static std::unique_ptr Get(HashType hash); }; } diff --git a/include/Nazara/Core/Algorithm.hpp b/include/Nazara/Core/Algorithm.hpp index b91250049..5670b57d6 100644 --- a/include/Nazara/Core/Algorithm.hpp +++ b/include/Nazara/Core/Algorithm.hpp @@ -8,15 +8,24 @@ #define NAZARA_ALGORITHM_CORE_HPP #include +#include #include #include namespace Nz { + class AbstractHash; + class ByteArray; + template auto Apply(F&& fn, Tuple&& t); template auto Apply(O& object, F&& fn, Tuple&& t); + template ByteArray ComputeHash(HashType hash, const T& v); + template ByteArray ComputeHash(AbstractHash* hash, const T& v); template void HashCombine(std::size_t& seed, const T& v); + template + struct Hashable; + template struct TypeTag {}; } diff --git a/include/Nazara/Core/Algorithm.inl b/include/Nazara/Core/Algorithm.inl index b01c1e176..25e4ecdb2 100644 --- a/include/Nazara/Core/Algorithm.inl +++ b/include/Nazara/Core/Algorithm.inl @@ -6,6 +6,7 @@ // Merci à Ryan "FullMetal Alchemist" Lahfa // Merci aussi à Freedom de siteduzero.com +#include #include namespace Nz @@ -41,6 +42,21 @@ namespace Nz return Detail::ApplyImplMethod(object, std::forward(fn), std::forward(t), std::make_index_sequence()); } + + template + ByteArray ComputeHash(HashType hash, const T& v) + { + return ComputeHash(AbstractHash::Get(hash).get(), v); + } + + template + ByteArray ComputeHash(AbstractHash* hash, const T& v) + { + hash->Begin(); + Hashable()(v, hash); + return hash->End(); + } + // Algorithme venant de CityHash par Google // http://stackoverflow.com/questions/8513911/how-to-create-a-good-hash-combine-with-64-bit-output-inspired-by-boosthash-co template diff --git a/include/Nazara/Core/ByteArray.hpp b/include/Nazara/Core/ByteArray.hpp index 926c53fd1..8be45779b 100644 --- a/include/Nazara/Core/ByteArray.hpp +++ b/include/Nazara/Core/ByteArray.hpp @@ -8,8 +8,6 @@ #define NAZARA_BYTEARRAY_HPP #include -#include -#include #include #include @@ -17,7 +15,7 @@ namespace Nz { class AbstractHash; - class NAZARA_CORE_API ByteArray : public Hashable + class NAZARA_CORE_API ByteArray { using Container = std::vector; @@ -90,6 +88,7 @@ namespace Nz inline void ShrinkToFit(); inline void Swap(ByteArray& other); + inline String ToHex() const; inline String ToString() const; // STL interface @@ -109,6 +108,8 @@ namespace Nz inline size_type size() const noexcept; // Operators + NAZARA_CORE_API friend std::ostream& operator<<(std::ostream& out, const Nz::ByteArray& byteArray); + inline reference operator[](size_type pos); inline const_reference operator[](size_type pos) const; inline ByteArray& operator=(const ByteArray& array) = default; @@ -124,13 +125,12 @@ namespace Nz inline bool operator>=(const ByteArray& rhs) const; private: - bool FillHash(AbstractHash* hash) const; - Container m_array; }; -} -NAZARA_CORE_API std::ostream& operator<<(std::ostream& out, const Nz::ByteArray& byteArray); + template<> + struct Hashable; +} namespace std { diff --git a/include/Nazara/Core/ByteArray.inl b/include/Nazara/Core/ByteArray.inl index 25d90c7eb..1542ce991 100644 --- a/include/Nazara/Core/ByteArray.inl +++ b/include/Nazara/Core/ByteArray.inl @@ -193,6 +193,17 @@ namespace Nz m_array.swap(other.m_array); } + inline String ByteArray::ToHex() const + { + unsigned int length = m_array.size() * 2; + + String hexOutput(length, '\0'); + for (unsigned int i = 0; i < m_array.size(); ++i) + std::sprintf(&hexOutput[i * 2], "%02x", m_array[i]); + + return hexOutput; + } + inline String ByteArray::ToString() const { return String(reinterpret_cast(GetConstBuffer()), GetSize()); @@ -321,6 +332,16 @@ namespace Nz { return m_array >= rhs.m_array; } + + template<> + struct Hashable + { + bool operator()(const ByteArray& byteArray, AbstractHash* hash) const + { + hash->Append(byteArray.GetConstBuffer(), byteArray.GetSize()); + return true; + } + }; } namespace std diff --git a/include/Nazara/Core/File.hpp b/include/Nazara/Core/File.hpp index d73c27823..8f8f6bc94 100644 --- a/include/Nazara/Core/File.hpp +++ b/include/Nazara/Core/File.hpp @@ -11,8 +11,6 @@ #include #include #include -#include -#include #include #include @@ -28,7 +26,7 @@ namespace Nz { class FileImpl; - class NAZARA_CORE_API File : public Hashable, public InputStream + class NAZARA_CORE_API File : public InputStream { public: File(); @@ -83,6 +81,8 @@ namespace Nz File& operator=(File&& file) noexcept; static String AbsolutePath(const String& filePath); + static inline ByteArray ComputeHash(HashType hash, const String& filePath); + static inline ByteArray ComputeHash(AbstractHash* hash, const String& filePath); static bool Copy(const String& sourcePath, const String& targetPath); static bool Delete(const String& filePath); static bool Exists(const String& filePath); @@ -90,8 +90,6 @@ namespace Nz static String GetDirectory(const String& filePath); static time_t GetLastAccessTime(const String& filePath); static time_t GetLastWriteTime(const String& filePath); - static HashDigest GetHash(const String& filePath, HashType hash); - static HashDigest GetHash(const String& filePath, AbstractHash* hash); static UInt64 GetSize(const String& filePath); static bool IsAbsolute(const String& filePath); static String NormalizePath(const String& filePath); @@ -99,8 +97,6 @@ namespace Nz static bool Rename(const String& sourcePath, const String& targetPath); private: - bool FillHash(AbstractHash* hash) const; - NazaraMutexAttrib(m_mutex, mutable) Endianness m_endianness; @@ -108,6 +104,11 @@ namespace Nz FileImpl* m_impl; unsigned int m_openMode; }; + + template<> + struct Hashable; } +#include + #endif // NAZARA_FILE_HPP diff --git a/include/Nazara/Core/File.inl b/include/Nazara/Core/File.inl new file mode 100644 index 000000000..c5603c320 --- /dev/null +++ b/include/Nazara/Core/File.inl @@ -0,0 +1,56 @@ +// Copyright (C) 2015 Jérôme Leclercq +// This file is part of the "Nazara Engine - Core module" +// For conditions of distribution and use, see copyright notice in Config.hpp + +#include +#include +#include +#include + +namespace Nz +{ + inline ByteArray File::ComputeHash(HashType hash, const String& filePath) + { + return ComputeHash(AbstractHash::Get(hash).get(), filePath); + } + + inline ByteArray File::ComputeHash(AbstractHash* hash, const String& filePath) + { + return Nz::ComputeHash(hash, File(filePath)); + } + + template<> + struct Hashable + { + bool operator()(const Nz::File& originalFile, AbstractHash* hash) const + { + + File file(originalFile.GetPath()); + if (!file.Open(OpenMode_ReadOnly)) + { + NazaraError("Unable to open file"); + return false; + } + + UInt64 remainingSize = file.GetSize(); + + char buffer[NAZARA_CORE_FILE_BUFFERSIZE]; + while (remainingSize > 0) + { + unsigned int size = static_cast(std::min(remainingSize, static_cast(NAZARA_CORE_FILE_BUFFERSIZE))); + if (file.Read(&buffer[0], sizeof(char), size) != sizeof(char)*size) + { + NazaraError("Unable to read file"); + return false; + } + + remainingSize -= size; + hash->Append(reinterpret_cast(&buffer[0]), size); + } + + return true; + } + }; +} + +#include diff --git a/include/Nazara/Core/Hash.hpp b/include/Nazara/Core/Hash.hpp deleted file mode 100644 index a9a152a2b..000000000 --- a/include/Nazara/Core/Hash.hpp +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright (C) 2015 Jérôme Leclercq -// This file is part of the "Nazara Engine - Core module" -// For conditions of distribution and use, see copyright notice in Config.hpp - -#pragma once - -#ifndef NAZARA_HASH_HPP -#define NAZARA_HASH_HPP - -#include -#include -#include -#include - -namespace Nz -{ - class NAZARA_CORE_API Hash - { - public: - Hash(HashType hash); - Hash(AbstractHash* hashImpl); - Hash(const Hash&) = delete; - Hash(Hash&&) = delete; ///TODO - ~Hash(); - - HashDigest Process(const Hashable& hashable); - - Hash& operator=(const Hash&) = delete; - Hash& operator=(Hash&&) = delete; ///TODO - - private: - AbstractHash* m_impl; - }; -} - -#endif // NAZARA_HASH_HPP diff --git a/include/Nazara/Core/Hash/CRC32.hpp b/include/Nazara/Core/Hash/CRC32.hpp index 602432897..1898ffae4 100644 --- a/include/Nazara/Core/Hash/CRC32.hpp +++ b/include/Nazara/Core/Hash/CRC32.hpp @@ -9,7 +9,7 @@ #include #include -#include +#include namespace Nz { @@ -21,12 +21,12 @@ namespace Nz HashCRC32(UInt32 polynomial = 0x04c11db7); virtual ~HashCRC32(); - void Append(const UInt8* data, unsigned int len); - void Begin(); - HashDigest End(); + void Append(const UInt8* data, unsigned int len) override; + void Begin() override; + ByteArray End() override; - static unsigned int GetDigestLength(); - static String GetHashName(); + unsigned int GetDigestLength() const override; + const char* GetHashName() const override; private: HashCRC32_state* m_state; diff --git a/include/Nazara/Core/Hash/Fletcher16.hpp b/include/Nazara/Core/Hash/Fletcher16.hpp index de84ecfeb..a4e296f38 100644 --- a/include/Nazara/Core/Hash/Fletcher16.hpp +++ b/include/Nazara/Core/Hash/Fletcher16.hpp @@ -9,7 +9,7 @@ #include #include -#include +#include #include namespace Nz @@ -22,12 +22,12 @@ namespace Nz HashFletcher16(); virtual ~HashFletcher16(); - void Append(const UInt8* data, unsigned int len); - void Begin(); - HashDigest End(); + void Append(const UInt8* data, unsigned int len) override; + void Begin() override; + ByteArray End() override; - static unsigned int GetDigestLength(); - static String GetHashName(); + unsigned int GetDigestLength() const override; + const char* GetHashName() const override; private: HashFletcher16_state* m_state; diff --git a/include/Nazara/Core/Hash/MD5.hpp b/include/Nazara/Core/Hash/MD5.hpp index b3723c7ed..20fee5e38 100644 --- a/include/Nazara/Core/Hash/MD5.hpp +++ b/include/Nazara/Core/Hash/MD5.hpp @@ -9,7 +9,7 @@ #include #include -#include +#include namespace Nz { @@ -21,12 +21,12 @@ namespace Nz HashMD5(); virtual ~HashMD5(); - void Append(const UInt8* data, unsigned int len); - void Begin(); - HashDigest End(); + void Append(const UInt8* data, unsigned int len) override; + void Begin() override; + ByteArray End() override; - static unsigned int GetDigestLength(); - static String GetHashName(); + unsigned int GetDigestLength() const override; + const char* GetHashName() const override; private: HashMD5_state* m_state; diff --git a/include/Nazara/Core/Hash/SHA1.hpp b/include/Nazara/Core/Hash/SHA1.hpp index abce7daee..4f80d3ca1 100644 --- a/include/Nazara/Core/Hash/SHA1.hpp +++ b/include/Nazara/Core/Hash/SHA1.hpp @@ -9,7 +9,7 @@ #include #include -#include +#include namespace Nz { @@ -21,12 +21,12 @@ namespace Nz HashSHA1(); virtual ~HashSHA1(); - void Append(const UInt8* data, unsigned int len); - void Begin(); - HashDigest End(); + void Append(const UInt8* data, unsigned int len) override; + void Begin() override; + ByteArray End() override; - static unsigned int GetDigestLength(); - static String GetHashName(); + unsigned int GetDigestLength() const override; + const char* GetHashName() const override; private: SHA_CTX* m_state; diff --git a/include/Nazara/Core/Hash/SHA224.hpp b/include/Nazara/Core/Hash/SHA224.hpp index f6f30da1c..9d97e3193 100644 --- a/include/Nazara/Core/Hash/SHA224.hpp +++ b/include/Nazara/Core/Hash/SHA224.hpp @@ -9,7 +9,7 @@ #include #include -#include +#include namespace Nz { @@ -21,12 +21,12 @@ namespace Nz HashSHA224(); virtual ~HashSHA224(); - void Append(const UInt8* data, unsigned int len); - void Begin(); - HashDigest End(); + void Append(const UInt8* data, unsigned int len) override; + void Begin() override; + ByteArray End() override; - static unsigned int GetDigestLength(); - static String GetHashName(); + unsigned int GetDigestLength() const override; + const char* GetHashName() const override; private: SHA_CTX* m_state; diff --git a/include/Nazara/Core/Hash/SHA256.hpp b/include/Nazara/Core/Hash/SHA256.hpp index 7d7b672af..5398f363b 100644 --- a/include/Nazara/Core/Hash/SHA256.hpp +++ b/include/Nazara/Core/Hash/SHA256.hpp @@ -9,7 +9,7 @@ #include #include -#include +#include namespace Nz { @@ -21,12 +21,12 @@ namespace Nz HashSHA256(); virtual ~HashSHA256(); - void Append(const UInt8* data, unsigned int len); - void Begin(); - HashDigest End(); + void Append(const UInt8* data, unsigned int len) override; + void Begin() override; + ByteArray End() override; - static unsigned int GetDigestLength(); - static String GetHashName(); + unsigned int GetDigestLength() const override; + const char* GetHashName() const override; private: SHA_CTX* m_state; diff --git a/include/Nazara/Core/Hash/SHA384.hpp b/include/Nazara/Core/Hash/SHA384.hpp index 8c03cac90..a84fc8a4e 100644 --- a/include/Nazara/Core/Hash/SHA384.hpp +++ b/include/Nazara/Core/Hash/SHA384.hpp @@ -9,7 +9,7 @@ #include #include -#include +#include namespace Nz { @@ -21,12 +21,12 @@ namespace Nz HashSHA384(); virtual ~HashSHA384(); - void Append(const UInt8* data, unsigned int len); - void Begin(); - HashDigest End(); + void Append(const UInt8* data, unsigned int len) override; + void Begin() override; + ByteArray End() override; - static unsigned int GetDigestLength(); - static String GetHashName(); + unsigned int GetDigestLength() const override; + const char* GetHashName() const override; private: SHA_CTX* m_state; diff --git a/include/Nazara/Core/Hash/SHA512.hpp b/include/Nazara/Core/Hash/SHA512.hpp index 799022266..7a49aaf72 100644 --- a/include/Nazara/Core/Hash/SHA512.hpp +++ b/include/Nazara/Core/Hash/SHA512.hpp @@ -9,7 +9,7 @@ #include #include -#include +#include namespace Nz { @@ -21,12 +21,12 @@ namespace Nz HashSHA512(); virtual ~HashSHA512(); - void Append(const UInt8* data, unsigned int len); - void Begin(); - HashDigest End(); + void Append(const UInt8* data, unsigned int len) override; + void Begin() override; + ByteArray End() override; - static unsigned int GetDigestLength(); - static String GetHashName(); + unsigned int GetDigestLength() const override; + const char* GetHashName() const override; private: SHA_CTX* m_state; diff --git a/include/Nazara/Core/Hash/Whirlpool.hpp b/include/Nazara/Core/Hash/Whirlpool.hpp index 9d49a5ecf..0da68015a 100644 --- a/include/Nazara/Core/Hash/Whirlpool.hpp +++ b/include/Nazara/Core/Hash/Whirlpool.hpp @@ -7,7 +7,7 @@ #include #include -#include +#include namespace Nz { @@ -19,12 +19,12 @@ namespace Nz HashWhirlpool(); virtual ~HashWhirlpool(); - void Append(const UInt8* data, unsigned int len); - void Begin(); - HashDigest End(); + void Append(const UInt8* data, unsigned int len) override; + void Begin() override; + ByteArray End() override; - static unsigned int GetDigestLength(); - static String GetHashName(); + unsigned int GetDigestLength() const; + const char* GetHashName() const; private: HashWhirlpool_state* m_state; diff --git a/include/Nazara/Core/HashDigest.hpp b/include/Nazara/Core/HashDigest.hpp deleted file mode 100644 index 902104de1..000000000 --- a/include/Nazara/Core/HashDigest.hpp +++ /dev/null @@ -1,56 +0,0 @@ -// Copyright (C) 2015 Jérôme Leclercq -// This file is part of the "Nazara Engine - Core module" -// For conditions of distribution and use, see copyright notice in Config.hpp - -#pragma once - -#ifndef NAZARA_HASHDIGEST_HPP -#define NAZARA_HASHDIGEST_HPP - -///TODO: Remplacer par ByteArray - -#include -#include -#include - -namespace Nz -{ - class NAZARA_CORE_API HashDigest - { - public: - HashDigest(); - HashDigest(const String& hashName, const UInt8* digest, unsigned int length); - HashDigest(const HashDigest& rhs); - HashDigest(HashDigest&& rhs) noexcept; - ~HashDigest(); - - bool IsValid() const; - - const UInt8* GetDigest() const; - unsigned int GetDigestLength() const; - String GetHashName() const; - - String ToHex() const; - - UInt8 operator[](unsigned int pos) const; - - HashDigest& operator=(const HashDigest& rhs); - HashDigest& operator=(HashDigest&& rhs) noexcept; - - bool operator==(const HashDigest& rhs) const; - bool operator!=(const HashDigest& rhs) const; - bool operator<(const HashDigest& rhs) const; - bool operator<=(const HashDigest& rhs) const; - bool operator>(const HashDigest& rhs) const; - bool operator>=(const HashDigest& rhs) const; - - NAZARA_CORE_API friend std::ostream& operator<<(std::ostream& out, const HashDigest& string); - - private: - String m_hashName; - UInt8* m_digest; - unsigned int m_digestLength; - }; -} - -#endif // NAZARA_HASHDIGEST_HPP diff --git a/include/Nazara/Core/Hashable.hpp b/include/Nazara/Core/Hashable.hpp deleted file mode 100644 index 3c79b319f..000000000 --- a/include/Nazara/Core/Hashable.hpp +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright (C) 2015 Jérôme Leclercq -// This file is part of the "Nazara Engine - Core module" -// For conditions of distribution and use, see copyright notice in Config.hpp - -#pragma once - -#ifndef HASHABLE_HPP_INCLUDED -#define HASHABLE_HPP_INCLUDED - -#include -#include - -namespace Nz -{ - class AbstractHash; - class HashDigest; - - class NAZARA_CORE_API Hashable - { - friend class Hash; - - public: - Hashable() = default; - Hashable(const Hashable&) = default; - Hashable(Hashable&&) = default; - virtual ~Hashable(); - - HashDigest GetHash(HashType hash) const; - HashDigest GetHash(AbstractHash* impl) const; - - Hashable& operator=(const Hashable&) = default; - Hashable& operator=(Hashable&&) = default; - - private: - virtual bool FillHash(AbstractHash* impl) const = 0; - }; -} - -#endif // HASHABLE_HPP_INCLUDED diff --git a/include/Nazara/Core/String.hpp b/include/Nazara/Core/String.hpp index c7cc5418a..61af938d1 100644 --- a/include/Nazara/Core/String.hpp +++ b/include/Nazara/Core/String.hpp @@ -8,7 +8,7 @@ #define NAZARA_STRING_HPP #include -#include +#include #include #include #include @@ -20,7 +20,7 @@ namespace Nz class AbstractHash; class HashDigest; - class NAZARA_CORE_API String : public Hashable + class NAZARA_CORE_API String { public: enum Flags @@ -306,7 +306,6 @@ namespace Nz String(std::shared_ptr&& sharedString); void EnsureOwnership(bool discardContent = false); - bool FillHash(AbstractHash* hash) const; inline void ReleaseString(); static const std::shared_ptr& GetEmptyString(); @@ -324,6 +323,9 @@ namespace Nz std::unique_ptr string; }; }; + + template<> + struct Hashable; } namespace std @@ -331,6 +333,9 @@ namespace std NAZARA_CORE_API istream& getline(istream& is, Nz::String& str); NAZARA_CORE_API istream& getline(istream& is, Nz::String& str, char delim); NAZARA_CORE_API void swap(Nz::String& lhs, Nz::String& rhs); + + template<> + struct hash; } #include diff --git a/include/Nazara/Core/String.inl b/include/Nazara/Core/String.inl index 49157777f..cc323d697 100644 --- a/include/Nazara/Core/String.inl +++ b/include/Nazara/Core/String.inl @@ -2,6 +2,7 @@ // This file is part of the "Nazara Engine - Core module" // For conditions of distribution and use, see copyright notice in Config.hpp +#include #include namespace Nz @@ -37,6 +38,16 @@ namespace Nz { string[strSize] = '\0'; } + + template<> + struct Hashable + { + bool operator()(const String& str, AbstractHash* hash) const + { + hash->Append(reinterpret_cast(str.GetConstBuffer()), str.GetSize()); + return true; + } + }; } namespace std diff --git a/src/Nazara/Core/AbstractHash.cpp b/src/Nazara/Core/AbstractHash.cpp index c9d6fa517..f0e82bb19 100644 --- a/src/Nazara/Core/AbstractHash.cpp +++ b/src/Nazara/Core/AbstractHash.cpp @@ -3,9 +3,57 @@ // For conditions of distribution and use, see copyright notice in Config.hpp #include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include #include namespace Nz { AbstractHash::~AbstractHash() = default; + + std::unique_ptr AbstractHash::Get(HashType type) + { + NazaraAssert(type <= HashType_Max, "Hash type value out of enum"); + + switch (type) + { + case HashType_Fletcher16: + return std::unique_ptr(new HashFletcher16); + + case HashType_CRC32: + return std::unique_ptr(new HashCRC32); + + case HashType_MD5: + return std::unique_ptr(new HashMD5); + + case HashType_SHA1: + return std::unique_ptr(new HashSHA1); + + case HashType_SHA224: + return std::unique_ptr(new HashSHA224); + + case HashType_SHA256: + return std::unique_ptr(new HashSHA256); + + case HashType_SHA384: + return std::unique_ptr(new HashSHA384); + + case HashType_SHA512: + return std::unique_ptr(new HashSHA512); + + case HashType_Whirlpool: + return std::unique_ptr(new HashWhirlpool); + } + + NazaraInternalError("Hash type not handled (0x" + String::Number(type, 16) + ')'); + return std::unique_ptr(); + } } diff --git a/src/Nazara/Core/ByteArray.cpp b/src/Nazara/Core/ByteArray.cpp index 33b53913b..cc5734822 100644 --- a/src/Nazara/Core/ByteArray.cpp +++ b/src/Nazara/Core/ByteArray.cpp @@ -9,16 +9,9 @@ namespace Nz { - std::ostream& operator<<(std::ostream& out, const ByteArray& byteArray) + std::ostream& operator<<(std::ostream& out, const Nz::ByteArray& byteArray) { - out << byteArray.ToString(); + out << byteArray.ToHex(); return out; } - - bool ByteArray::FillHash(AbstractHash* hash) const - { - hash->Append(GetConstBuffer(), GetSize()); - - return true; - } } diff --git a/src/Nazara/Core/File.cpp b/src/Nazara/Core/File.cpp index 2a72262bb..9c83b708d 100644 --- a/src/Nazara/Core/File.cpp +++ b/src/Nazara/Core/File.cpp @@ -7,7 +7,6 @@ #include #include #include -#include #include #include #include @@ -631,22 +630,6 @@ namespace Nz return FileImpl::GetLastWriteTime(NormalizePath(filePath)); } - HashDigest File::GetHash(const String& filePath, HashType hash) - { - File file(filePath); - - Hash h(hash); - return h.Process(file); - } - - HashDigest File::GetHash(const String& filePath, AbstractHash* hash) - { - File file(filePath); - - Hash h(hash); - return h.Process(file); - } - UInt64 File::GetSize(const String& filePath) { if (filePath.IsEmpty()) @@ -714,32 +697,4 @@ namespace Nz return FileImpl::Rename(NormalizePath(sourcePath), NormalizePath(targetPath)); } - - bool File::FillHash(AbstractHash* hash) const - { - File file(m_filePath); - if (!file.Open(OpenMode_ReadOnly)) - { - NazaraError("Unable to open file"); - return false; - } - - UInt64 remainingSize = file.GetSize(); - - char buffer[NAZARA_CORE_FILE_BUFFERSIZE]; - while (remainingSize > 0) - { - unsigned int size = static_cast(std::min(remainingSize, static_cast(NAZARA_CORE_FILE_BUFFERSIZE))); - if (file.Read(&buffer[0], sizeof(char), size) != sizeof(char)*size) - { - NazaraError("Unable to read file"); - return false; - } - - remainingSize -= size; - hash->Append(reinterpret_cast(&buffer[0]), size); - } - - return true; - } // Fermeture automatique du fichier } diff --git a/src/Nazara/Core/Hash.cpp b/src/Nazara/Core/Hash.cpp deleted file mode 100644 index 04043589a..000000000 --- a/src/Nazara/Core/Hash.cpp +++ /dev/null @@ -1,83 +0,0 @@ -// Copyright (C) 2015 Jérôme Leclercq -// This file is part of the "Nazara Engine - Core module" -// For conditions of distribution and use, see copyright notice in Config.hpp - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -namespace Nz -{ - Hash::Hash(HashType hash) - { - switch (hash) - { - case HashType_Fletcher16: - m_impl = new HashFletcher16; - break; - - case HashType_CRC32: - m_impl = new HashCRC32; - break; - - case HashType_MD5: - m_impl = new HashMD5; - break; - - case HashType_SHA1: - m_impl = new HashSHA1; - break; - - case HashType_SHA224: - m_impl = new HashSHA224; - break; - - case HashType_SHA256: - m_impl = new HashSHA256; - break; - - case HashType_SHA384: - m_impl = new HashSHA384; - break; - - case HashType_SHA512: - m_impl = new HashSHA512; - break; - - case HashType_Whirlpool: - m_impl = new HashWhirlpool; - break; - } - } - - Hash::Hash(AbstractHash* hashImpl) : - m_impl(hashImpl) - { - } - - Hash::~Hash() - { - delete m_impl; - } - - HashDigest Hash::Process(const Hashable& hashable) - { - m_impl->Begin(); - if (hashable.FillHash(m_impl)) - return m_impl->End(); - else // Erreur - { - m_impl->End(); - - return HashDigest(); - } - } -} diff --git a/src/Nazara/Core/Hash/CRC32.cpp b/src/Nazara/Core/Hash/CRC32.cpp index 9459be37e..edf3ba8c2 100644 --- a/src/Nazara/Core/Hash/CRC32.cpp +++ b/src/Nazara/Core/Hash/CRC32.cpp @@ -109,7 +109,7 @@ namespace Nz m_state->crc = 0xFFFFFFFF; } - HashDigest HashCRC32::End() + ByteArray HashCRC32::End() { m_state->crc ^= 0xFFFFFFFF; @@ -117,17 +117,16 @@ namespace Nz SwapBytes(&m_state->crc, sizeof(UInt32)); #endif - return HashDigest(GetHashName(), reinterpret_cast(&m_state->crc), 4); + return ByteArray(reinterpret_cast(&m_state->crc), 4); } - unsigned int HashCRC32::GetDigestLength() + unsigned int HashCRC32::GetDigestLength() const { return 4; } - String HashCRC32::GetHashName() + const char* HashCRC32::GetHashName() const { - static String hashName = "CRC32"; - return hashName; + return "CRC32"; } } diff --git a/src/Nazara/Core/Hash/Fletcher16.cpp b/src/Nazara/Core/Hash/Fletcher16.cpp index 66fbae2af..c4ab978e8 100644 --- a/src/Nazara/Core/Hash/Fletcher16.cpp +++ b/src/Nazara/Core/Hash/Fletcher16.cpp @@ -48,7 +48,7 @@ namespace Nz m_state->sum2 = 0xff; } - HashDigest HashFletcher16::End() + ByteArray HashFletcher16::End() { m_state->sum1 = (m_state->sum1 & 0xff) + (m_state->sum1 >> 8); m_state->sum2 = (m_state->sum2 & 0xff) + (m_state->sum2 >> 8); @@ -59,17 +59,16 @@ namespace Nz SwapBytes(&fletcher, sizeof(UInt32)); #endif - return HashDigest(GetHashName(), reinterpret_cast(&fletcher), 2); + return ByteArray(reinterpret_cast(&fletcher), 2); } - unsigned int HashFletcher16::GetDigestLength() + unsigned int HashFletcher16::GetDigestLength() const { return 2; } - String HashFletcher16::GetHashName() + const char* HashFletcher16::GetHashName() const { - static String hashName = "Fletcher16"; - return hashName; + return "Fletcher16"; } } diff --git a/src/Nazara/Core/Hash/MD5.cpp b/src/Nazara/Core/Hash/MD5.cpp index 93856c79d..127a1f8cb 100644 --- a/src/Nazara/Core/Hash/MD5.cpp +++ b/src/Nazara/Core/Hash/MD5.cpp @@ -325,7 +325,7 @@ namespace Nz m_state->abcd[3] = 0x10325476; } - HashDigest HashMD5::End() + ByteArray HashMD5::End() { static const unsigned char pad[64] = { 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, @@ -349,17 +349,16 @@ namespace Nz for (i = 0; i < 16; ++i) digest[i] = static_cast(m_state->abcd[i >> 2] >> ((i & 3) << 3)); - return HashDigest(GetHashName(), &digest[0], 16); + return ByteArray(&digest[0], 16); } - unsigned int HashMD5::GetDigestLength() + unsigned int HashMD5::GetDigestLength() const { return 16; } - String HashMD5::GetHashName() + const char* HashMD5::GetHashName() const { - static String hashName = "MD5"; - return hashName; + return "MD5"; } } diff --git a/src/Nazara/Core/Hash/SHA1.cpp b/src/Nazara/Core/Hash/SHA1.cpp index 8b1725fcb..1ed49e69c 100644 --- a/src/Nazara/Core/Hash/SHA1.cpp +++ b/src/Nazara/Core/Hash/SHA1.cpp @@ -28,23 +28,22 @@ namespace Nz SHA1_Init(m_state); } - HashDigest HashSHA1::End() + ByteArray HashSHA1::End() { UInt8 digest[SHA1_DIGEST_LENGTH]; SHA1_End(m_state, digest); - return HashDigest(GetHashName(), digest, SHA1_DIGEST_LENGTH); + return ByteArray(digest, SHA1_DIGEST_LENGTH); } - unsigned int HashSHA1::GetDigestLength() + unsigned int HashSHA1::GetDigestLength() const { return SHA1_DIGEST_LENGTH; } - String HashSHA1::GetHashName() + const char* HashSHA1::GetHashName() const { - static String hashName = "SHA1"; - return hashName; + return "SHA1"; } } diff --git a/src/Nazara/Core/Hash/SHA224.cpp b/src/Nazara/Core/Hash/SHA224.cpp index ab836e597..57b95e965 100644 --- a/src/Nazara/Core/Hash/SHA224.cpp +++ b/src/Nazara/Core/Hash/SHA224.cpp @@ -28,23 +28,22 @@ namespace Nz SHA224_Init(m_state); } - HashDigest HashSHA224::End() + ByteArray HashSHA224::End() { UInt8 digest[SHA224_DIGEST_LENGTH]; SHA224_End(m_state, digest); - return HashDigest(GetHashName(), digest, SHA224_DIGEST_LENGTH); + return ByteArray(digest, SHA224_DIGEST_LENGTH); } - unsigned int HashSHA224::GetDigestLength() + unsigned int HashSHA224::GetDigestLength() const { return SHA224_DIGEST_LENGTH; } - String HashSHA224::GetHashName() + const char* HashSHA224::GetHashName() const { - static String hashName = "SHA224"; - return hashName; + return "SHA224"; } } diff --git a/src/Nazara/Core/Hash/SHA256.cpp b/src/Nazara/Core/Hash/SHA256.cpp index a42764e94..202820594 100644 --- a/src/Nazara/Core/Hash/SHA256.cpp +++ b/src/Nazara/Core/Hash/SHA256.cpp @@ -28,23 +28,22 @@ namespace Nz SHA256_Init(m_state); } - HashDigest HashSHA256::End() + ByteArray HashSHA256::End() { UInt8 digest[SHA256_DIGEST_LENGTH]; SHA256_End(m_state, digest); - return HashDigest(GetHashName(), digest, SHA256_DIGEST_LENGTH); + return ByteArray(digest, SHA256_DIGEST_LENGTH); } - unsigned int HashSHA256::GetDigestLength() + unsigned int HashSHA256::GetDigestLength() const { return SHA256_DIGEST_LENGTH; } - String HashSHA256::GetHashName() + const char* HashSHA256::GetHashName() const { - static String hashName = "SHA256"; - return hashName; + return "SHA256"; } } diff --git a/src/Nazara/Core/Hash/SHA384.cpp b/src/Nazara/Core/Hash/SHA384.cpp index a4aa25961..fe1498cbe 100644 --- a/src/Nazara/Core/Hash/SHA384.cpp +++ b/src/Nazara/Core/Hash/SHA384.cpp @@ -28,23 +28,22 @@ namespace Nz SHA384_Init(m_state); } - HashDigest HashSHA384::End() + ByteArray HashSHA384::End() { UInt8 digest[SHA384_DIGEST_LENGTH]; SHA384_End(m_state, digest); - return HashDigest(GetHashName(), digest, SHA384_DIGEST_LENGTH); + return ByteArray(digest, SHA384_DIGEST_LENGTH); } - unsigned int HashSHA384::GetDigestLength() + unsigned int HashSHA384::GetDigestLength() const { return SHA384_DIGEST_LENGTH; } - String HashSHA384::GetHashName() + const char* HashSHA384::GetHashName() const { - static String hashName = "SHA384"; - return hashName; + return "SHA384"; } } diff --git a/src/Nazara/Core/Hash/SHA512.cpp b/src/Nazara/Core/Hash/SHA512.cpp index f246ab0e8..562836998 100644 --- a/src/Nazara/Core/Hash/SHA512.cpp +++ b/src/Nazara/Core/Hash/SHA512.cpp @@ -28,23 +28,22 @@ namespace Nz SHA512_Init(m_state); } - HashDigest HashSHA512::End() + ByteArray HashSHA512::End() { UInt8 digest[SHA512_DIGEST_LENGTH]; SHA512_End(m_state, digest); - return HashDigest(GetHashName(), digest, SHA512_DIGEST_LENGTH); + return ByteArray(digest, SHA512_DIGEST_LENGTH); } - unsigned int HashSHA512::GetDigestLength() + unsigned int HashSHA512::GetDigestLength() const { return SHA512_DIGEST_LENGTH; } - String HashSHA512::GetHashName() + const char* HashSHA512::GetHashName() const { - static String hashName = "SHA512"; - return hashName; + return "SHA512"; } } diff --git a/src/Nazara/Core/Hash/Whirlpool.cpp b/src/Nazara/Core/Hash/Whirlpool.cpp index b734fdec9..9be283775 100644 --- a/src/Nazara/Core/Hash/Whirlpool.cpp +++ b/src/Nazara/Core/Hash/Whirlpool.cpp @@ -962,7 +962,7 @@ namespace Nz m_state->hash[i] = 0L; // initial value } - HashDigest HashWhirlpool::End() + ByteArray HashWhirlpool::End() { UInt8 result[64]; @@ -1012,17 +1012,16 @@ namespace Nz m_state->bufferBits = bufferBits; m_state->bufferPos = bufferPos; - return HashDigest(GetHashName(), &result[0], 64); + return ByteArray(&result[0], 64); } - unsigned int HashWhirlpool::GetDigestLength() + unsigned int HashWhirlpool::GetDigestLength() const { return 64; } - String HashWhirlpool::GetHashName() + const char* HashWhirlpool::GetHashName() const { - static String hashName = "Whirlpool"; - return hashName; + return "Whirlpool"; } } diff --git a/src/Nazara/Core/HashDigest.cpp b/src/Nazara/Core/HashDigest.cpp deleted file mode 100644 index 93c23f13c..000000000 --- a/src/Nazara/Core/HashDigest.cpp +++ /dev/null @@ -1,194 +0,0 @@ -// Copyright (C) 2015 Jérôme Leclercq -// This file is part of the "Nazara Engine - Core module" -// For conditions of distribution and use, see copyright notice in Config.hpp - -#include -#include -#include -#include -#include -#include -#include - -namespace Nz -{ - HashDigest::HashDigest() : - m_digest(nullptr), - m_digestLength(0) - { - } - - HashDigest::HashDigest(const String& hashName, const UInt8* digest, unsigned int length) : - m_hashName(hashName), - m_digestLength(length) - { - if (m_digestLength > 0) - { - m_digest = new UInt8[length]; - std::memcpy(m_digest, digest, length); - } - else - m_digest = nullptr; - } - - HashDigest::HashDigest(const HashDigest& rhs) : - m_hashName(rhs.m_hashName), - m_digestLength(rhs.m_digestLength) - { - if (m_digestLength > 0) - { - m_digest = new UInt8[m_digestLength]; - std::memcpy(m_digest, rhs.m_digest, m_digestLength); - } - else - m_digest = nullptr; - } - - HashDigest::HashDigest(HashDigest&& rhs) noexcept : - m_hashName(std::move(rhs.m_hashName)), - m_digest(rhs.m_digest), - m_digestLength(rhs.m_digestLength) - { - rhs.m_digest = nullptr; - rhs.m_digestLength = 0; - } - - HashDigest::~HashDigest() - { - delete[] m_digest; - } - - bool HashDigest::IsValid() const - { - return m_digestLength > 0; - } - - const UInt8* HashDigest::GetDigest() const - { - return m_digest; - } - - unsigned int HashDigest::GetDigestLength() const - { - return m_digestLength; - } - - String HashDigest::GetHashName() const - { - return m_hashName; - } - - String HashDigest::ToHex() const - { - if (m_digestLength == 0) - return String(); - - unsigned int length = m_digestLength*2; - - String hexOutput(length); - for (unsigned int i = 0; i < m_digestLength; ++i) - std::sprintf(&hexOutput[i*2], "%02x", m_digest[i]); - - return hexOutput; - } - - UInt8 HashDigest::operator[](unsigned int pos) const - { - #if NAZARA_CORE_SAFE - if (pos >= m_digestLength) - { - NazaraError("Position out of range"); - return 0; - } - #endif - - return m_digest[pos]; - } - - HashDigest& HashDigest::operator=(const HashDigest& rhs) - { - if (this == &rhs) - return *this; - - m_hashName = rhs.m_hashName; - - m_digestLength = rhs.m_digestLength; - if (m_digestLength > 0) - { - m_digest = new UInt8[m_digestLength]; - std::memcpy(m_digest, rhs.m_digest, m_digestLength); - } - else - m_digest = nullptr; - - return *this; - } - - HashDigest& HashDigest::operator=(HashDigest&& rhs) noexcept - { - std::swap(m_hashName, rhs.m_hashName); - std::swap(m_digest, rhs.m_digest); - std::swap(m_digestLength, rhs.m_digestLength); - - return *this; - } - - bool HashDigest::operator==(const HashDigest& rhs) const - { - if (m_digest == nullptr || rhs.m_digest == nullptr) - return m_digest == rhs.m_digest; - - if (m_digestLength != rhs.m_digestLength) - return false; - - return m_hashName == rhs.m_hashName && std::memcmp(m_digest, rhs.m_digest, m_digestLength) == 0; - } - - bool HashDigest::operator!=(const HashDigest& rhs) const - { - return !operator==(rhs); - } - - bool HashDigest::operator<(const HashDigest& rhs) const - { - if (rhs.m_digest == nullptr) - return false; - - if (m_digest == nullptr) - return true; - - int cmp = String::Compare(m_hashName, rhs.m_hashName); - if (cmp == 0) - { - cmp = std::memcmp(m_digest, rhs.m_digest, std::min(m_digestLength, rhs.m_digestLength)); - - if (cmp == 0) - return m_digestLength < rhs.m_digestLength; - else - return cmp < 0; - } - else - return cmp < 0; - } - - bool HashDigest::operator<=(const HashDigest& rhs) const - { - return !rhs.operator<(*this); - } - - bool HashDigest::operator>(const HashDigest& rhs) const - { - return rhs.operator<(*this); - } - - bool HashDigest::operator>=(const HashDigest& rhs) const - { - return !operator<(rhs); - } -} - -std::ostream& operator<<(std::ostream& out, const Nz::HashDigest& hashstring) -{ - out << hashstring.ToHex(); - return out; -} diff --git a/src/Nazara/Core/Hashable.cpp b/src/Nazara/Core/Hashable.cpp deleted file mode 100644 index b3d594f10..000000000 --- a/src/Nazara/Core/Hashable.cpp +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright (C) 2015 Jérôme Leclercq -// This file is part of the "Nazara Engine - Core module" -// For conditions of distribution and use, see copyright notice in Config.hpp - -#include -#include -#include - -namespace Nz -{ - Hashable::~Hashable() = default; - - HashDigest Hashable::GetHash(HashType hash) const - { - Hash h(hash); - return h.Process(*this); - } - - HashDigest Hashable::GetHash(AbstractHash* impl) const - { - Hash h(impl); - return h.Process(*this); - } -} diff --git a/src/Nazara/Core/String.cpp b/src/Nazara/Core/String.cpp index d39f9adb1..cfddcf49d 100644 --- a/src/Nazara/Core/String.cpp +++ b/src/Nazara/Core/String.cpp @@ -5,7 +5,6 @@ ///TODO: Réécrire une bonne partie des algorithmes employés (Relu jusqu'à 3538) #include -#include #include #include #include @@ -4203,13 +4202,6 @@ namespace Nz } } - bool String::FillHash(AbstractHash* hash) const - { - hash->Append(reinterpret_cast(GetConstBuffer()), GetSize()); - - return true; - } - const std::shared_ptr& String::GetEmptyString() { static auto emptyString = std::make_shared();