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.

379 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. #define S11 7
  165. #define S12 12
  166. #define S13 17
  167. #define S14 22
  168. #define S21 5
  169. #define S22 9
  170. #define S23 14
  171. #define S24 20
  172. #define S31 4
  173. #define S32 11
  174. #define S33 16
  175. #define S34 23
  176. #define S41 6
  177. #define S42 10
  178. #define S43 15
  179. #define S44 21
  180. static inline uint32 F (const uint32 x, const uint32 y, const uint32 z) { return (x & y) | (~x & z); }
  181. static inline uint32 G (const uint32 x, const uint32 y, const uint32 z) { return (x & z) | (y & ~z); }
  182. static inline uint32 H (const uint32 x, const uint32 y, const uint32 z) { return x ^ y ^ z; }
  183. static inline uint32 I (const uint32 x, const uint32 y, const uint32 z) { return y ^ (x | ~z); }
  184. static inline uint32 rotateLeft (const uint32 x, const uint32 n) { return (x << n) | (x >> (32 - n)); }
  185. static inline void FF (uint32& a, const uint32 b, const uint32 c, const uint32 d, const uint32 x, const uint32 s, const uint32 ac)
  186. {
  187. a += F (b, c, d) + x + ac;
  188. a = rotateLeft (a, s) + b;
  189. }
  190. static inline void GG (uint32& a, const uint32 b, const uint32 c, const uint32 d, const uint32 x, const uint32 s, const uint32 ac)
  191. {
  192. a += G (b, c, d) + x + ac;
  193. a = rotateLeft (a, s) + b;
  194. }
  195. static inline void HH (uint32& a, const uint32 b, const uint32 c, const uint32 d, const uint32 x, const uint32 s, const uint32 ac)
  196. {
  197. a += H (b, c, d) + x + ac;
  198. a = rotateLeft (a, s) + b;
  199. }
  200. static inline void II (uint32& a, const uint32 b, const uint32 c, const uint32 d, const uint32 x, const uint32 s, const uint32 ac)
  201. {
  202. a += I (b, c, d) + x + ac;
  203. a = rotateLeft (a, s) + b;
  204. }
  205. void MD5::ProcessContext::transform (const uint8* const bufferToTransform)
  206. {
  207. uint32 a = state[0];
  208. uint32 b = state[1];
  209. uint32 c = state[2];
  210. uint32 d = state[3];
  211. uint32 x[16];
  212. decode (x, bufferToTransform, 64);
  213. FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
  214. FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
  215. FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
  216. FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
  217. FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
  218. FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
  219. FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
  220. FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
  221. FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
  222. FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
  223. FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
  224. FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
  225. FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
  226. FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
  227. FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
  228. FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
  229. GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
  230. GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
  231. GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
  232. GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
  233. GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
  234. GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */
  235. GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
  236. GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
  237. GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
  238. GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
  239. GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
  240. GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
  241. GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
  242. GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
  243. GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
  244. GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
  245. HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
  246. HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
  247. HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
  248. HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
  249. HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
  250. HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
  251. HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
  252. HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
  253. HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
  254. HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
  255. HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
  256. HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
  257. HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
  258. HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
  259. HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
  260. HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
  261. II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
  262. II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
  263. II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
  264. II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
  265. II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
  266. II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
  267. II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
  268. II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
  269. II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
  270. II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
  271. II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
  272. II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
  273. II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
  274. II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
  275. II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
  276. II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
  277. state[0] += a;
  278. state[1] += b;
  279. state[2] += c;
  280. state[3] += d;
  281. zeromem (x, sizeof (x));
  282. }
  283. //==============================================================================
  284. const MemoryBlock MD5::getRawChecksumData() const
  285. {
  286. return MemoryBlock (result, 16);
  287. }
  288. const String MD5::toHexString() const
  289. {
  290. return String::toHexString (result, 16, 0);
  291. }
  292. //==============================================================================
  293. bool MD5::operator== (const MD5& other) const
  294. {
  295. return memcmp (result, other.result, 16) == 0;
  296. }
  297. bool MD5::operator!= (const MD5& other) const
  298. {
  299. return ! operator== (other);
  300. }
  301. END_JUCE_NAMESPACE