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.

372 lines
12KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-9 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. zeromem (result, sizeof (result));
  27. }
  28. MD5::MD5 (const MD5& other)
  29. {
  30. memcpy (result, other.result, sizeof (result));
  31. }
  32. const 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 ((const uint8*) data.getData(), data.getSize());
  42. context.finish (result);
  43. }
  44. MD5::MD5 (const char* data, const size_t numBytes)
  45. {
  46. ProcessContext context;
  47. context.processBlock ((const uint8*) 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 = (uint32) t[i];
  60. ByteOrder::swapIfBigEndian (unicodeChar);
  61. context.processBlock ((const uint8*) &unicodeChar,
  62. sizeof (unicodeChar));
  63. }
  64. context.finish (result);
  65. }
  66. void MD5::processStream (InputStream& input, int64 numBytesToRead)
  67. {
  68. ProcessContext context;
  69. if (numBytesToRead < 0)
  70. numBytesToRead = std::numeric_limits<int64>::max();
  71. while (numBytesToRead > 0)
  72. {
  73. char tempBuffer [512];
  74. const int bytesRead = input.read (tempBuffer, (int) jmin (numBytesToRead, (int64) sizeof (tempBuffer)));
  75. if (bytesRead <= 0)
  76. break;
  77. numBytesToRead -= bytesRead;
  78. context.processBlock ((const uint8*) tempBuffer, bytesRead);
  79. }
  80. context.finish (result);
  81. }
  82. MD5::MD5 (InputStream& input, int64 numBytesToRead)
  83. {
  84. processStream (input, numBytesToRead);
  85. }
  86. MD5::MD5 (const File& file)
  87. {
  88. const ScopedPointer <FileInputStream> fin (file.createInputStream());
  89. if (fin != 0)
  90. processStream (*fin, -1);
  91. else
  92. zeromem (result, sizeof (result));
  93. }
  94. MD5::~MD5()
  95. {
  96. }
  97. //==============================================================================
  98. MD5::ProcessContext::ProcessContext()
  99. {
  100. state[0] = 0x67452301;
  101. state[1] = 0xefcdab89;
  102. state[2] = 0x98badcfe;
  103. state[3] = 0x10325476;
  104. count[0] = 0;
  105. count[1] = 0;
  106. }
  107. void MD5::ProcessContext::processBlock (const uint8* const data, size_t dataSize)
  108. {
  109. int bufferPos = ((count[0] >> 3) & 0x3F);
  110. count[0] += (uint32) (dataSize << 3);
  111. if (count[0] < ((uint32) dataSize << 3))
  112. count[1]++;
  113. count[1] += (uint32) (dataSize >> 29);
  114. const size_t spaceLeft = 64 - bufferPos;
  115. size_t i = 0;
  116. if (dataSize >= spaceLeft)
  117. {
  118. memcpy (buffer + bufferPos, data, spaceLeft);
  119. transform (buffer);
  120. i = spaceLeft;
  121. while (i + 64 <= dataSize)
  122. {
  123. transform (data + i);
  124. i += 64;
  125. }
  126. bufferPos = 0;
  127. }
  128. memcpy (buffer + bufferPos, data + i, dataSize - i);
  129. }
  130. //==============================================================================
  131. static void encode (uint8* const output,
  132. const uint32* const input,
  133. const int numBytes)
  134. {
  135. uint32* const o = (uint32*) output;
  136. for (int i = 0; i < (numBytes >> 2); ++i)
  137. o[i] = ByteOrder::swapIfBigEndian (input [i]);
  138. }
  139. static void decode (uint32* const output,
  140. const uint8* const input,
  141. const int numBytes)
  142. {
  143. for (int i = 0; i < (numBytes >> 2); ++i)
  144. output[i] = ByteOrder::littleEndianInt ((const char*) input + (i << 2));
  145. }
  146. //==============================================================================
  147. void MD5::ProcessContext::finish (uint8* const result)
  148. {
  149. unsigned char encodedLength[8];
  150. encode (encodedLength, count, 8);
  151. // Pad out to 56 mod 64.
  152. const int index = (uint32) ((count[0] >> 3) & 0x3f);
  153. const int paddingLength = (index < 56) ? (56 - index)
  154. : (120 - index);
  155. uint8 paddingBuffer [64];
  156. zeromem (paddingBuffer, paddingLength);
  157. paddingBuffer [0] = 0x80;
  158. processBlock (paddingBuffer, paddingLength);
  159. processBlock (encodedLength, 8);
  160. encode (result, state, 16);
  161. zeromem (buffer, sizeof (buffer));
  162. }
  163. //==============================================================================
  164. namespace MD5Functions
  165. {
  166. static inline uint32 F (const uint32 x, const uint32 y, const uint32 z) throw() { return (x & y) | (~x & z); }
  167. static inline uint32 G (const uint32 x, const uint32 y, const uint32 z) throw() { return (x & z) | (y & ~z); }
  168. static inline uint32 H (const uint32 x, const uint32 y, const uint32 z) throw() { return x ^ y ^ z; }
  169. static inline uint32 I (const uint32 x, const uint32 y, const uint32 z) throw() { return y ^ (x | ~z); }
  170. static inline uint32 rotateLeft (const uint32 x, const uint32 n) throw() { return (x << n) | (x >> (32 - n)); }
  171. static void FF (uint32& a, const uint32 b, const uint32 c, const uint32 d, const uint32 x, const uint32 s, const uint32 ac) throw()
  172. {
  173. a += F (b, c, d) + x + ac;
  174. a = rotateLeft (a, s) + b;
  175. }
  176. static void GG (uint32& a, const uint32 b, const uint32 c, const uint32 d, const uint32 x, const uint32 s, const uint32 ac) throw()
  177. {
  178. a += G (b, c, d) + x + ac;
  179. a = rotateLeft (a, s) + b;
  180. }
  181. static void HH (uint32& a, const uint32 b, const uint32 c, const uint32 d, const uint32 x, const uint32 s, const uint32 ac) throw()
  182. {
  183. a += H (b, c, d) + x + ac;
  184. a = rotateLeft (a, s) + b;
  185. }
  186. static void II (uint32& a, const uint32 b, const uint32 c, const uint32 d, const uint32 x, const uint32 s, const uint32 ac) throw()
  187. {
  188. a += I (b, c, d) + x + ac;
  189. a = rotateLeft (a, s) + b;
  190. }
  191. }
  192. void MD5::ProcessContext::transform (const uint8* const bufferToTransform)
  193. {
  194. uint32 a = state[0];
  195. uint32 b = state[1];
  196. uint32 c = state[2];
  197. uint32 d = state[3];
  198. uint32 x[16];
  199. decode (x, bufferToTransform, 64);
  200. enum Constants
  201. {
  202. S11 = 7, S12 = 12, S13 = 17, S14 = 22, S21 = 5, S22 = 9, S23 = 14, S24 = 20,
  203. S31 = 4, S32 = 11, S33 = 16, S34 = 23, S41 = 6, S42 = 10, S43 = 15, S44 = 21
  204. };
  205. using namespace MD5Functions;
  206. FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
  207. FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
  208. FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
  209. FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
  210. FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
  211. FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
  212. FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
  213. FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
  214. FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
  215. FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
  216. FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
  217. FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
  218. FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
  219. FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
  220. FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
  221. FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
  222. GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
  223. GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
  224. GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
  225. GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
  226. GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
  227. GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */
  228. GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
  229. GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
  230. GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
  231. GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
  232. GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
  233. GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
  234. GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
  235. GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
  236. GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
  237. GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
  238. HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
  239. HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
  240. HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
  241. HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
  242. HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
  243. HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
  244. HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
  245. HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
  246. HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
  247. HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
  248. HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
  249. HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
  250. HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
  251. HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
  252. HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
  253. HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
  254. II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
  255. II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
  256. II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
  257. II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
  258. II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
  259. II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
  260. II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
  261. II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
  262. II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
  263. II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
  264. II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
  265. II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
  266. II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
  267. II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
  268. II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
  269. II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
  270. state[0] += a;
  271. state[1] += b;
  272. state[2] += c;
  273. state[3] += d;
  274. zerostruct (x);
  275. }
  276. //==============================================================================
  277. const MemoryBlock MD5::getRawChecksumData() const
  278. {
  279. return MemoryBlock (result, 16);
  280. }
  281. const String MD5::toHexString() const
  282. {
  283. return String::toHexString (result, 16, 0);
  284. }
  285. //==============================================================================
  286. bool MD5::operator== (const MD5& other) const
  287. {
  288. return memcmp (result, other.result, 16) == 0;
  289. }
  290. bool MD5::operator!= (const MD5& other) const
  291. {
  292. return ! operator== (other);
  293. }
  294. END_JUCE_NAMESPACE