The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

317 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-10 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #include "../core/juce_StandardHeader.h"
  19. BEGIN_JUCE_NAMESPACE
  20. #include "juce_MD5.h"
  21. #include "../io/files/juce_FileInputStream.h"
  22. #include "../containers/juce_ScopedPointer.h"
  23. //==============================================================================
  24. MD5::MD5()
  25. {
  26. zerostruct (result);
  27. }
  28. MD5::MD5 (const MD5& other)
  29. {
  30. memcpy (result, other.result, sizeof (result));
  31. }
  32. MD5& MD5::operator= (const MD5& other)
  33. {
  34. memcpy (result, other.result, sizeof (result));
  35. return *this;
  36. }
  37. //==============================================================================
  38. MD5::MD5 (const MemoryBlock& data)
  39. {
  40. ProcessContext context;
  41. context.processBlock (data.getData(), data.getSize());
  42. context.finish (result);
  43. }
  44. MD5::MD5 (const void* data, const size_t numBytes)
  45. {
  46. ProcessContext context;
  47. context.processBlock (data, numBytes);
  48. context.finish (result);
  49. }
  50. MD5::MD5 (const String& text)
  51. {
  52. ProcessContext context;
  53. const int len = text.length();
  54. const juce_wchar* const t = text;
  55. for (int i = 0; i < len; ++i)
  56. {
  57. // force the string into integer-sized unicode characters, to try to make it
  58. // get the same results on all platforms + compilers.
  59. uint32 unicodeChar = ByteOrder::swapIfBigEndian ((uint32) t[i]);
  60. context.processBlock (&unicodeChar, sizeof (unicodeChar));
  61. }
  62. context.finish (result);
  63. }
  64. void MD5::processStream (InputStream& input, int64 numBytesToRead)
  65. {
  66. ProcessContext context;
  67. if (numBytesToRead < 0)
  68. numBytesToRead = std::numeric_limits<int64>::max();
  69. while (numBytesToRead > 0)
  70. {
  71. uint8 tempBuffer [512];
  72. const int bytesRead = input.read (tempBuffer, (int) jmin (numBytesToRead, (int64) sizeof (tempBuffer)));
  73. if (bytesRead <= 0)
  74. break;
  75. numBytesToRead -= bytesRead;
  76. context.processBlock (tempBuffer, bytesRead);
  77. }
  78. context.finish (result);
  79. }
  80. MD5::MD5 (InputStream& input, int64 numBytesToRead)
  81. {
  82. processStream (input, numBytesToRead);
  83. }
  84. MD5::MD5 (const File& file)
  85. {
  86. const ScopedPointer <FileInputStream> fin (file.createInputStream());
  87. if (fin != 0)
  88. processStream (*fin, -1);
  89. else
  90. zerostruct (result);
  91. }
  92. MD5::~MD5()
  93. {
  94. }
  95. //==============================================================================
  96. namespace MD5Functions
  97. {
  98. void encode (void* const output, const void* const input, const int numBytes) throw()
  99. {
  100. for (int i = 0; i < (numBytes >> 2); ++i)
  101. static_cast<uint32*> (output)[i] = ByteOrder::swapIfBigEndian (static_cast<const uint32*> (input) [i]);
  102. }
  103. inline uint32 rotateLeft (const uint32 x, const uint32 n) throw() { return (x << n) | (x >> (32 - n)); }
  104. inline uint32 F (const uint32 x, const uint32 y, const uint32 z) throw() { return (x & y) | (~x & z); }
  105. inline uint32 G (const uint32 x, const uint32 y, const uint32 z) throw() { return (x & z) | (y & ~z); }
  106. inline uint32 H (const uint32 x, const uint32 y, const uint32 z) throw() { return x ^ y ^ z; }
  107. inline uint32 I (const uint32 x, const uint32 y, const uint32 z) throw() { return y ^ (x | ~z); }
  108. void FF (uint32& a, const uint32 b, const uint32 c, const uint32 d, const uint32 x, const uint32 s, const uint32 ac) throw()
  109. {
  110. a += F (b, c, d) + x + ac;
  111. a = rotateLeft (a, s) + b;
  112. }
  113. void GG (uint32& a, const uint32 b, const uint32 c, const uint32 d, const uint32 x, const uint32 s, const uint32 ac) throw()
  114. {
  115. a += G (b, c, d) + x + ac;
  116. a = rotateLeft (a, s) + b;
  117. }
  118. void HH (uint32& a, const uint32 b, const uint32 c, const uint32 d, const uint32 x, const uint32 s, const uint32 ac) throw()
  119. {
  120. a += H (b, c, d) + x + ac;
  121. a = rotateLeft (a, s) + b;
  122. }
  123. void II (uint32& a, const uint32 b, const uint32 c, const uint32 d, const uint32 x, const uint32 s, const uint32 ac) throw()
  124. {
  125. a += I (b, c, d) + x + ac;
  126. a = rotateLeft (a, s) + b;
  127. }
  128. }
  129. //==============================================================================
  130. MD5::ProcessContext::ProcessContext()
  131. {
  132. state[0] = 0x67452301;
  133. state[1] = 0xefcdab89;
  134. state[2] = 0x98badcfe;
  135. state[3] = 0x10325476;
  136. count[0] = 0;
  137. count[1] = 0;
  138. }
  139. void MD5::ProcessContext::processBlock (const void* const data, const size_t dataSize)
  140. {
  141. int bufferPos = ((count[0] >> 3) & 0x3F);
  142. count[0] += (uint32) (dataSize << 3);
  143. if (count[0] < ((uint32) dataSize << 3))
  144. count[1]++;
  145. count[1] += (uint32) (dataSize >> 29);
  146. const size_t spaceLeft = 64 - bufferPos;
  147. size_t i = 0;
  148. if (dataSize >= spaceLeft)
  149. {
  150. memcpy (buffer + bufferPos, data, spaceLeft);
  151. transform (buffer);
  152. for (i = spaceLeft; i + 64 <= dataSize; i += 64)
  153. transform (static_cast <const char*> (data) + i);
  154. bufferPos = 0;
  155. }
  156. memcpy (buffer + bufferPos, static_cast <const char*> (data) + i, dataSize - i);
  157. }
  158. //==============================================================================
  159. void MD5::ProcessContext::finish (void* const result)
  160. {
  161. unsigned char encodedLength[8];
  162. MD5Functions::encode (encodedLength, count, 8);
  163. // Pad out to 56 mod 64.
  164. const int index = (uint32) ((count[0] >> 3) & 0x3f);
  165. const int paddingLength = (index < 56) ? (56 - index)
  166. : (120 - index);
  167. uint8 paddingBuffer [64];
  168. zeromem (paddingBuffer, paddingLength);
  169. paddingBuffer [0] = 0x80;
  170. processBlock (paddingBuffer, paddingLength);
  171. processBlock (encodedLength, 8);
  172. MD5Functions::encode (result, state, 16);
  173. zerostruct (buffer);
  174. }
  175. void MD5::ProcessContext::transform (const void* const bufferToTransform)
  176. {
  177. using namespace MD5Functions;
  178. uint32 a = state[0];
  179. uint32 b = state[1];
  180. uint32 c = state[2];
  181. uint32 d = state[3];
  182. uint32 x[16];
  183. encode (x, bufferToTransform, 64);
  184. enum Constants
  185. {
  186. S11 = 7, S12 = 12, S13 = 17, S14 = 22, S21 = 5, S22 = 9, S23 = 14, S24 = 20,
  187. S31 = 4, S32 = 11, S33 = 16, S34 = 23, S41 = 6, S42 = 10, S43 = 15, S44 = 21
  188. };
  189. FF (a, b, c, d, x[ 0], S11, 0xd76aa478); FF (d, a, b, c, x[ 1], S12, 0xe8c7b756);
  190. FF (c, d, a, b, x[ 2], S13, 0x242070db); FF (b, c, d, a, x[ 3], S14, 0xc1bdceee);
  191. FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); FF (d, a, b, c, x[ 5], S12, 0x4787c62a);
  192. FF (c, d, a, b, x[ 6], S13, 0xa8304613); FF (b, c, d, a, x[ 7], S14, 0xfd469501);
  193. FF (a, b, c, d, x[ 8], S11, 0x698098d8); FF (d, a, b, c, x[ 9], S12, 0x8b44f7af);
  194. FF (c, d, a, b, x[10], S13, 0xffff5bb1); FF (b, c, d, a, x[11], S14, 0x895cd7be);
  195. FF (a, b, c, d, x[12], S11, 0x6b901122); FF (d, a, b, c, x[13], S12, 0xfd987193);
  196. FF (c, d, a, b, x[14], S13, 0xa679438e); FF (b, c, d, a, x[15], S14, 0x49b40821);
  197. GG (a, b, c, d, x[ 1], S21, 0xf61e2562); GG (d, a, b, c, x[ 6], S22, 0xc040b340);
  198. GG (c, d, a, b, x[11], S23, 0x265e5a51); GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa);
  199. GG (a, b, c, d, x[ 5], S21, 0xd62f105d); GG (d, a, b, c, x[10], S22, 0x02441453);
  200. GG (c, d, a, b, x[15], S23, 0xd8a1e681); GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8);
  201. GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); GG (d, a, b, c, x[14], S22, 0xc33707d6);
  202. GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); GG (b, c, d, a, x[ 8], S24, 0x455a14ed);
  203. GG (a, b, c, d, x[13], S21, 0xa9e3e905); GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8);
  204. GG (c, d, a, b, x[ 7], S23, 0x676f02d9); GG (b, c, d, a, x[12], S24, 0x8d2a4c8a);
  205. HH (a, b, c, d, x[ 5], S31, 0xfffa3942); HH (d, a, b, c, x[ 8], S32, 0x8771f681);
  206. HH (c, d, a, b, x[11], S33, 0x6d9d6122); HH (b, c, d, a, x[14], S34, 0xfde5380c);
  207. HH (a, b, c, d, x[ 1], S31, 0xa4beea44); HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9);
  208. HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); HH (b, c, d, a, x[10], S34, 0xbebfbc70);
  209. HH (a, b, c, d, x[13], S31, 0x289b7ec6); HH (d, a, b, c, x[ 0], S32, 0xeaa127fa);
  210. HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); HH (b, c, d, a, x[ 6], S34, 0x04881d05);
  211. HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); HH (d, a, b, c, x[12], S32, 0xe6db99e5);
  212. HH (c, d, a, b, x[15], S33, 0x1fa27cf8); HH (b, c, d, a, x[ 2], S34, 0xc4ac5665);
  213. II (a, b, c, d, x[ 0], S41, 0xf4292244); II (d, a, b, c, x[ 7], S42, 0x432aff97);
  214. II (c, d, a, b, x[14], S43, 0xab9423a7); II (b, c, d, a, x[ 5], S44, 0xfc93a039);
  215. II (a, b, c, d, x[12], S41, 0x655b59c3); II (d, a, b, c, x[ 3], S42, 0x8f0ccc92);
  216. II (c, d, a, b, x[10], S43, 0xffeff47d); II (b, c, d, a, x[ 1], S44, 0x85845dd1);
  217. II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); II (d, a, b, c, x[15], S42, 0xfe2ce6e0);
  218. II (c, d, a, b, x[ 6], S43, 0xa3014314); II (b, c, d, a, x[13], S44, 0x4e0811a1);
  219. II (a, b, c, d, x[ 4], S41, 0xf7537e82); II (d, a, b, c, x[11], S42, 0xbd3af235);
  220. II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); II (b, c, d, a, x[ 9], S44, 0xeb86d391);
  221. state[0] += a;
  222. state[1] += b;
  223. state[2] += c;
  224. state[3] += d;
  225. zerostruct (x);
  226. }
  227. //==============================================================================
  228. const MemoryBlock MD5::getRawChecksumData() const
  229. {
  230. return MemoryBlock (result, sizeof (result));
  231. }
  232. const String MD5::toHexString() const
  233. {
  234. return String::toHexString (result, sizeof (result), 0);
  235. }
  236. //==============================================================================
  237. bool MD5::operator== (const MD5& other) const
  238. {
  239. return memcmp (result, other.result, sizeof (result)) == 0;
  240. }
  241. bool MD5::operator!= (const MD5& other) const
  242. {
  243. return ! operator== (other);
  244. }
  245. END_JUCE_NAMESPACE