From 2bc036f4c9976009468282d497f13a7dbd95f417 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Thu, 6 Oct 2016 23:01:44 -0300 Subject: [PATCH] Use love's sized int typedefs instead of C's. --HG-- branch : minor --- src/modules/math/HashFunction.cpp | 166 +++++++++++++++--------------- src/modules/math/HashFunction.h | 6 +- 2 files changed, 85 insertions(+), 87 deletions(-) diff --git a/src/modules/math/HashFunction.cpp b/src/modules/math/HashFunction.cpp index 21f2d08a3..d7848625c 100644 --- a/src/modules/math/HashFunction.cpp +++ b/src/modules/math/HashFunction.cpp @@ -35,17 +35,17 @@ namespace namespace impl { -inline uint32_t leftrot(uint32_t x, uint8_t amount) +inline uint32 leftrot(uint32 x, uint8 amount) { return (x << amount) | (x >> (32 - amount)); } -inline uint32_t rightrot(uint32_t x, uint8_t amount) +inline uint32 rightrot(uint32 x, uint8 amount) { return (x >> amount) | (x << (32 - amount)); } -inline uint64_t rightrot(uint64_t x, uint8_t amount) +inline uint64 rightrot(uint64 x, uint8 amount) { return (x >> amount) | (x << (64 - amount)); } @@ -60,8 +60,8 @@ inline uint64_t rightrot(uint64_t x, uint8_t amount) class MD5 : public HashFunction { private: - static const uint8_t shifts[64]; - static const uint32_t constants[64]; + static const uint8 shifts[64]; + static const uint32 constants[64]; public: bool isSupported(Function function) const override @@ -69,39 +69,39 @@ public: return function == FUNCTION_MD5; } - std::string hash(Function function, const char *input, uint64_t length) const override + std::string hash(Function function, const char *input, uint64 length) const override { if (function != FUNCTION_MD5) throw love::Exception("Hash function not supported by MD5 implementation"); - uint32_t a0 = 0x67452301; - uint32_t b0 = 0xefcdab89; - uint32_t c0 = 0x98badcfe; - uint32_t d0 = 0x10325476; + uint32 a0 = 0x67452301; + uint32 b0 = 0xefcdab89; + uint32 c0 = 0x98badcfe; + uint32 d0 = 0x10325476; // Do the required padding - uint64_t paddedLength = length; + uint64 paddedLength = length; if (length % 64 < 56) paddedLength += 56-length%64; if (length % 64 > 56) paddedLength += 120-length%64; - uint8_t *padded = new uint8_t[paddedLength+8]; + uint8 *padded = new uint8[paddedLength+8]; memcpy(padded, input, length); memset(padded+length, 0, paddedLength-length); padded[length] = 0x80; - *((uint64_t*) &padded[paddedLength]) = length*8; + *((uint64*) &padded[paddedLength]) = length*8; paddedLength += 8; - for (uint64_t i = 0; i < paddedLength; i += 64) + for (uint64 i = 0; i < paddedLength; i += 64) { - uint32_t *chunk = (uint32_t*) &padded[i]; + uint32 *chunk = (uint32*) &padded[i]; - uint32_t A = a0; - uint32_t B = b0; - uint32_t C = c0; - uint32_t D = d0; - uint32_t F; - uint32_t g; + uint32 A = a0; + uint32 B = b0; + uint32 C = c0; + uint32 D = d0; + uint32 F; + uint32 g; for (int j = 0; j < 64; j++) { @@ -126,7 +126,7 @@ public: g = (7*j) % 16; } - uint32_t temp = D; + uint32 temp = D; D = C; C = B; B += leftrot(A + F + constants[j] + chunk[g], shifts[j]); @@ -150,14 +150,14 @@ public: } } md5; -const uint8_t MD5::shifts[64] = { +const uint8 MD5::shifts[64] = { 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, }; -const uint32_t MD5::constants[64] = { +const uint32 MD5::constants[64] = { 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501, 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, @@ -189,22 +189,22 @@ public: return function == FUNCTION_SHA1; } - std::string hash(Function function, const char *input, uint64_t length) const override + std::string hash(Function function, const char *input, uint64 length) const override { if (function != FUNCTION_SHA1) throw love::Exception("Hash function not supported by SHA1 implementation"); - uint32_t intermediate[5] = { + uint32 intermediate[5] = { 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0 }; // Same padding as for md5, but then big-endian - uint64_t paddedLength = length; + uint64 paddedLength = length; if (length % 64 < 56) paddedLength += 56-length%64; if (length % 64 > 56) paddedLength += 120-length%64; - uint8_t *padded = new uint8_t[paddedLength+8]; + uint8 *padded = new uint8[paddedLength+8]; memcpy(padded, input, length); memset(padded+length, 0, paddedLength-length); padded[length] = 0x80; @@ -215,11 +215,11 @@ public: padded[paddedLength] = (length >> (56-i*8)) & 0xFF; // Allocate our extended words - uint32_t *words = new uint32_t[80]; + uint32 *words = new uint32[80]; - for (uint64_t i = 0; i < paddedLength; i += 64) + for (uint64 i = 0; i < paddedLength; i += 64) { - uint32_t *chunk = (uint32_t*) &padded[i]; + uint32 *chunk = (uint32*) &padded[i]; for (int j = 0; j < 16; j++) { char *c = (char*) &words[j]; @@ -231,15 +231,15 @@ public: for (int j = 16; j < 80; j++) words[j] = leftrot(words[j-3] ^ words[j-8] ^ words[j-14] ^ words[j-16], 1); - uint32_t A = intermediate[0]; - uint32_t B = intermediate[1]; - uint32_t C = intermediate[2]; - uint32_t D = intermediate[3]; - uint32_t E = intermediate[4]; + uint32 A = intermediate[0]; + uint32 B = intermediate[1]; + uint32 C = intermediate[2]; + uint32 D = intermediate[3]; + uint32 E = intermediate[4]; for (int j = 0; j < 80; j++) { - uint32_t temp = leftrot(A, 5) + E + words[j]; + uint32 temp = leftrot(A, 5) + E + words[j]; if (j < 20) temp += 0x5A827999 + ((B & C) | (~B & D)); @@ -287,9 +287,9 @@ public: class SHA256 : public HashFunction { private: - static const uint32_t initial224[8]; - static const uint32_t initial256[8]; - static const uint32_t constants[64]; + static const uint32 initial224[8]; + static const uint32 initial256[8]; + static const uint32 constants[64]; public: bool isSupported(Function function) const override @@ -297,18 +297,18 @@ public: return function == FUNCTION_SHA224 || function == FUNCTION_SHA256; } - std::string hash(Function function, const char *input, uint64_t length) const override + std::string hash(Function function, const char *input, uint64 length) const override { if (!isSupported(function)) throw love::Exception("Hash function not supported by SHA-224/SHA-256 implementation"); // Same padding as for sha1 - uint64_t paddedLength = length; + uint64 paddedLength = length; if (length % 64 < 56) paddedLength += 56-length%64; if (length % 64 > 56) paddedLength += 120-length%64; - uint8_t *padded = new uint8_t[paddedLength+8]; + uint8 *padded = new uint8[paddedLength+8]; memcpy(padded, input, length); memset(padded+length, 0, paddedLength-length); padded[length] = 0x80; @@ -318,18 +318,18 @@ public: for (int i = 0; i < 8; ++i, ++paddedLength) padded[paddedLength] = (length >> (56-i*8)) & 0xFF; - uint32_t intermediate[8]; + uint32 intermediate[8]; if (function == FUNCTION_SHA224) memcpy(intermediate, initial224, sizeof(intermediate)); else memcpy(intermediate, initial256, sizeof(intermediate)); // Allocate our extended words - uint32_t *words = new uint32_t[64]; + uint32 *words = new uint32[64]; - for (uint64_t i = 0; i < paddedLength; i += 64) + for (uint64 i = 0; i < paddedLength; i += 64) { - uint32_t *chunk = (uint32_t*) &padded[i]; + uint32 *chunk = (uint32*) &padded[i]; for (int j = 0; j < 16; j++) { char *c = (char*) &words[j]; @@ -345,21 +345,21 @@ public: words[j] += words[j-7] + words[j-16]; } - uint32_t A = intermediate[0]; - uint32_t B = intermediate[1]; - uint32_t C = intermediate[2]; - uint32_t D = intermediate[3]; - uint32_t E = intermediate[4]; - uint32_t F = intermediate[5]; - uint32_t G = intermediate[6]; - uint32_t H = intermediate[7]; + uint32 A = intermediate[0]; + uint32 B = intermediate[1]; + uint32 C = intermediate[2]; + uint32 D = intermediate[3]; + uint32 E = intermediate[4]; + uint32 F = intermediate[5]; + uint32 G = intermediate[6]; + uint32 H = intermediate[7]; for (int j = 0; j < 64; j++) { - uint32_t temp1 = H + constants[j] + words[j]; + uint32 temp1 = H + constants[j] + words[j]; temp1 += rightrot(E, 6) ^ rightrot(E, 11) ^ rightrot(E, 25); temp1 += (E & F) ^ (~E & G); - uint32_t temp2 = rightrot(A, 2) ^ rightrot(A, 13) ^ rightrot(A, 22); + uint32 temp2 = rightrot(A, 2) ^ rightrot(A, 13) ^ rightrot(A, 22); temp2 += (A & B) ^ (A & C) ^ (B & C); H = G; @@ -403,17 +403,17 @@ public: } } sha256; -const uint32_t SHA256::initial224[8] = { +const uint32 SHA256::initial224[8] = { 0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939, 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4, }; -const uint32_t SHA256::initial256[8] = { +const uint32 SHA256::initial256[8] = { 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19, }; -const uint32_t SHA256::constants[64] = { +const uint32 SHA256::constants[64] = { 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, @@ -439,9 +439,9 @@ const uint32_t SHA256::constants[64] = { class SHA512 : public HashFunction { private: - static const uint64_t initial384[8]; - static const uint64_t initial512[8]; - static const uint64_t constants[80]; + static const uint64 initial384[8]; + static const uint64 initial512[8]; + static const uint64 constants[80]; public: bool isSupported(Function function) const override @@ -449,23 +449,23 @@ public: return function == FUNCTION_SHA384 || function == FUNCTION_SHA512; } - std::string hash(Function function, const char *input, uint64_t length) const override + std::string hash(Function function, const char *input, uint64 length) const override { if (!isSupported(function)) throw love::Exception("Hash function not supported by SHA-384/SHA-512 implementation"); - uint64_t intermediates[8]; + uint64 intermediates[8]; if (function == FUNCTION_SHA384) memcpy(intermediates, initial384, sizeof(intermediates)); else memcpy(intermediates, initial512, sizeof(intermediates)); - uint64_t paddedLength = length; + uint64 paddedLength = length; if (length % 128 < 112) paddedLength += 112-length%128; if (length % 128 > 112) paddedLength += 240-length%128; - uint8_t *padded = new uint8_t[paddedLength+16]; + uint8 *padded = new uint8[paddedLength+16]; paddedLength += 8; memcpy(padded, input, length); memset(padded+length, 0, paddedLength-length); @@ -478,11 +478,11 @@ public: padded[paddedLength] = (length >> (56-i*8)) & 0xFF; // Allocate our extended words - uint64_t *words = new uint64_t[80]; + uint64 *words = new uint64[80]; - for (uint64_t i = 0; i < paddedLength; i += 128) + for (uint64 i = 0; i < paddedLength; i += 128) { - uint64_t *chunk = (uint64_t*) &padded[i]; + uint64 *chunk = (uint64*) &padded[i]; for (int j = 0; j < 16; ++j) { char *c = (char*) &words[j]; @@ -502,21 +502,21 @@ public: words[j] += rightrot(words[j-15], 1) ^ rightrot(words[j-15], 8) ^ (words[j-15] >> 7); } - uint64_t A = intermediates[0]; - uint64_t B = intermediates[1]; - uint64_t C = intermediates[2]; - uint64_t D = intermediates[3]; - uint64_t E = intermediates[4]; - uint64_t F = intermediates[5]; - uint64_t G = intermediates[6]; - uint64_t H = intermediates[7]; + uint64 A = intermediates[0]; + uint64 B = intermediates[1]; + uint64 C = intermediates[2]; + uint64 D = intermediates[3]; + uint64 E = intermediates[4]; + uint64 F = intermediates[5]; + uint64 G = intermediates[6]; + uint64 H = intermediates[7]; for (int j = 0; j < 80; ++j) { - uint64_t temp1 = H + constants[j] + words[j]; + uint64 temp1 = H + constants[j] + words[j]; temp1 += rightrot(E, 14) ^ rightrot(E, 18) ^ rightrot(E, 41); temp1 += (E & F) ^ (~E & G); - uint64_t temp2 = rightrot(A, 28) ^ rightrot(A, 34) ^ rightrot(A, 39); + uint64 temp2 = rightrot(A, 28) ^ rightrot(A, 34) ^ rightrot(A, 39); temp2 += (A & B) ^ (A & C) ^ (B & C); H = G; G = F; @@ -562,17 +562,17 @@ public: } } sha512; -const uint64_t SHA512::initial384[8] = { +const uint64 SHA512::initial384[8] = { 0xcbbb9d5dc1059ed8, 0x629a292a367cd507, 0x9159015a3070dd17, 0x152fecd8f70e5939, 0x67332667ffc00b31, 0x8eb44a8768581511, 0xdb0c2e0d64f98fa7, 0x47b5481dbefa4fa4, }; -const uint64_t SHA512::initial512[8] = { +const uint64 SHA512::initial512[8] = { 0x6a09e667f3bcc908, 0xbb67ae8584caa73b, 0x3c6ef372fe94f82b, 0xa54ff53a5f1d36f1, 0x510e527fade682d1, 0x9b05688c2b3e6c1f, 0x1f83d9abfb41bd6b, 0x5be0cd19137e2179, }; -const uint64_t SHA512::constants[80] = { +const uint64 SHA512::constants[80] = { 0x428a2f98d728ae22, 0x7137449123ef65cd, 0xb5c0fbcfec4d3b2f, 0xe9b5dba58189dbbc, 0x3956c25bf348b538, 0x59f111f1b605d019, 0x923f82a4af194f9b, 0xab1c5ed5da6d8118, 0xd807aa98a3030242, 0x12835b0145706fbe, 0x243185be4ee4b28c, 0x550c7dc3d5ffb4e2, diff --git a/src/modules/math/HashFunction.h b/src/modules/math/HashFunction.h index 3322253fe..fc5d2e9e7 100644 --- a/src/modules/math/HashFunction.h +++ b/src/modules/math/HashFunction.h @@ -24,9 +24,7 @@ // LOVE #include "common/Data.h" #include "common/StringMap.h" - -// STL -#include +#include "common/int.h" namespace love { @@ -66,7 +64,7 @@ public: * @param[in] length The length of the input data. * @return A string of bytes, representing the result of the hash function. **/ - virtual std::string hash(Function function, const char *input, uint64_t length) const = 0; + virtual std::string hash(Function function, const char *input, uint64 length) const = 0; /** * @param[in] function The requested hash function.