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.

422 lines
14KB

  1. #if HAVE_CONFIG_H
  2. # include <config.h>
  3. #endif
  4. #include <stdlib.h> /* for malloc() */
  5. #include <string.h> /* for memcpy() */
  6. #include "include/private/md5.h"
  7. #include "../alloc.h"
  8. /*
  9. * This code implements the MD5 message-digest algorithm.
  10. * The algorithm is due to Ron Rivest. This code was
  11. * written by Colin Plumb in 1993, no copyright is claimed.
  12. * This code is in the public domain; do with it what you wish.
  13. *
  14. * Equivalent code is available from RSA Data Security, Inc.
  15. * This code has been tested against that, and is equivalent,
  16. * except that you don't need to include two pages of legalese
  17. * with every copy.
  18. *
  19. * To compute the message digest of a chunk of bytes, declare an
  20. * MD5Context structure, pass it to MD5Init, call MD5Update as
  21. * needed on buffers full of bytes, and then call MD5Final, which
  22. * will fill a supplied 16-byte array with the digest.
  23. *
  24. * Changed so as no longer to depend on Colin Plumb's `usual.h' header
  25. * definitions; now uses stuff from dpkg's config.h.
  26. * - Ian Jackson <ijackson@nyx.cs.du.edu>.
  27. * Still in the public domain.
  28. *
  29. * Josh Coalson: made some changes to integrate with libFLAC.
  30. * Still in the public domain.
  31. */
  32. /* The four core functions - F1 is optimized somewhat */
  33. /* #define F1(x, y, z) (x & y | ~x & z) */
  34. #define F1(x, y, z) (z ^ (x & (y ^ z)))
  35. #define F2(x, y, z) F1(z, x, y)
  36. #define F3(x, y, z) (x ^ y ^ z)
  37. #define F4(x, y, z) (y ^ (x | ~z))
  38. /* This is the central step in the MD5 algorithm. */
  39. #define MD5STEP(f,w,x,y,z,in,s) \
  40. (w += f(x,y,z) + in, w = (w<<s | w>>(32-s)) + x)
  41. /*
  42. * The core of the MD5 algorithm, this alters an existing MD5 hash to
  43. * reflect the addition of 16 longwords of new data. MD5Update blocks
  44. * the data and converts bytes into longwords for this routine.
  45. */
  46. static void FLAC__MD5Transform(FLAC__uint32 buf[4], FLAC__uint32 const in[16])
  47. {
  48. register FLAC__uint32 a, b, c, d;
  49. a = buf[0];
  50. b = buf[1];
  51. c = buf[2];
  52. d = buf[3];
  53. MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
  54. MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
  55. MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
  56. MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
  57. MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
  58. MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
  59. MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
  60. MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
  61. MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
  62. MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
  63. MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
  64. MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
  65. MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
  66. MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
  67. MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
  68. MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
  69. MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
  70. MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
  71. MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
  72. MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
  73. MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
  74. MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
  75. MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
  76. MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
  77. MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
  78. MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
  79. MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
  80. MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
  81. MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
  82. MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
  83. MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
  84. MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
  85. MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
  86. MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
  87. MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
  88. MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
  89. MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
  90. MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
  91. MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
  92. MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
  93. MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
  94. MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
  95. MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
  96. MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
  97. MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
  98. MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
  99. MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
  100. MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
  101. MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
  102. MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
  103. MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
  104. MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
  105. MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
  106. MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
  107. MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
  108. MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
  109. MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
  110. MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
  111. MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
  112. MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
  113. MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
  114. MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
  115. MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
  116. MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
  117. buf[0] += a;
  118. buf[1] += b;
  119. buf[2] += c;
  120. buf[3] += d;
  121. }
  122. #if WORDS_BIGENDIAN
  123. //@@@@@@ OPT: use bswap/intrinsics
  124. static void byteSwap(FLAC__uint32 *buf, unsigned words)
  125. {
  126. register FLAC__uint32 x;
  127. do {
  128. x = *buf;
  129. x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff);
  130. *buf++ = (x >> 16) | (x << 16);
  131. } while (--words);
  132. }
  133. static void byteSwapX16(FLAC__uint32 *buf)
  134. {
  135. register FLAC__uint32 x;
  136. x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
  137. x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
  138. x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
  139. x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
  140. x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
  141. x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
  142. x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
  143. x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
  144. x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
  145. x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
  146. x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
  147. x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
  148. x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
  149. x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
  150. x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
  151. x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf = (x >> 16) | (x << 16);
  152. }
  153. #else
  154. #define byteSwap(buf, words)
  155. #define byteSwapX16(buf)
  156. #endif
  157. /*
  158. * Update context to reflect the concatenation of another buffer full
  159. * of bytes.
  160. */
  161. static void FLAC__MD5Update(FLAC__MD5Context *ctx, FLAC__byte const *buf, unsigned len)
  162. {
  163. FLAC__uint32 t;
  164. /* Update byte count */
  165. t = ctx->bytes[0];
  166. if ((ctx->bytes[0] = t + len) < t)
  167. ctx->bytes[1]++; /* Carry from low to high */
  168. t = 64 - (t & 0x3f); /* Space available in ctx->in (at least 1) */
  169. if (t > len) {
  170. memcpy((FLAC__byte *)ctx->in + 64 - t, buf, len);
  171. return;
  172. }
  173. /* First chunk is an odd size */
  174. memcpy((FLAC__byte *)ctx->in + 64 - t, buf, t);
  175. byteSwapX16(ctx->in);
  176. FLAC__MD5Transform(ctx->buf, ctx->in);
  177. buf += t;
  178. len -= t;
  179. /* Process data in 64-byte chunks */
  180. while (len >= 64) {
  181. memcpy(ctx->in, buf, 64);
  182. byteSwapX16(ctx->in);
  183. FLAC__MD5Transform(ctx->buf, ctx->in);
  184. buf += 64;
  185. len -= 64;
  186. }
  187. /* Handle any remaining bytes of data. */
  188. memcpy(ctx->in, buf, len);
  189. }
  190. /*
  191. * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
  192. * initialization constants.
  193. */
  194. void FLAC__MD5Init(FLAC__MD5Context *ctx)
  195. {
  196. ctx->buf[0] = 0x67452301;
  197. ctx->buf[1] = 0xefcdab89;
  198. ctx->buf[2] = 0x98badcfe;
  199. ctx->buf[3] = 0x10325476;
  200. ctx->bytes[0] = 0;
  201. ctx->bytes[1] = 0;
  202. ctx->internal_buf = 0;
  203. ctx->capacity = 0;
  204. }
  205. /*
  206. * Final wrapup - pad to 64-byte boundary with the bit pattern
  207. * 1 0* (64-bit count of bits processed, MSB-first)
  208. */
  209. void FLAC__MD5Final(FLAC__byte digest[16], FLAC__MD5Context *ctx)
  210. {
  211. int count = ctx->bytes[0] & 0x3f; /* Number of bytes in ctx->in */
  212. FLAC__byte *p = (FLAC__byte *)ctx->in + count;
  213. /* Set the first char of padding to 0x80. There is always room. */
  214. *p++ = 0x80;
  215. /* Bytes of padding needed to make 56 bytes (-8..55) */
  216. count = 56 - 1 - count;
  217. if (count < 0) { /* Padding forces an extra block */
  218. memset(p, 0, count + 8);
  219. byteSwapX16(ctx->in);
  220. FLAC__MD5Transform(ctx->buf, ctx->in);
  221. p = (FLAC__byte *)ctx->in;
  222. count = 56;
  223. }
  224. memset(p, 0, count);
  225. byteSwap(ctx->in, 14);
  226. /* Append length in bits and transform */
  227. ctx->in[14] = ctx->bytes[0] << 3;
  228. ctx->in[15] = ctx->bytes[1] << 3 | ctx->bytes[0] >> 29;
  229. FLAC__MD5Transform(ctx->buf, ctx->in);
  230. byteSwap(ctx->buf, 4);
  231. memcpy(digest, ctx->buf, 16);
  232. if(0 != ctx->internal_buf) {
  233. free(ctx->internal_buf);
  234. ctx->internal_buf = 0;
  235. ctx->capacity = 0;
  236. }
  237. memset(ctx, 0, sizeof(*ctx)); /* In case it's sensitive */
  238. }
  239. /*
  240. * Convert the incoming audio signal to a byte stream
  241. */
  242. static void format_input_(FLAC__byte *buf, const FLAC__int32 * const signal[], unsigned channels, unsigned samples, unsigned bytes_per_sample)
  243. {
  244. unsigned channel, sample;
  245. register FLAC__int32 a_word;
  246. register FLAC__byte *buf_ = buf;
  247. #if WORDS_BIGENDIAN
  248. #else
  249. if(channels == 2 && bytes_per_sample == 2) {
  250. FLAC__int16 *buf1_ = ((FLAC__int16*)buf_) + 1;
  251. memcpy(buf_, signal[0], sizeof(FLAC__int32) * samples);
  252. for(sample = 0; sample < samples; sample++, buf1_+=2)
  253. *buf1_ = (FLAC__int16)signal[1][sample];
  254. }
  255. else if(channels == 1 && bytes_per_sample == 2) {
  256. FLAC__int16 *buf1_ = (FLAC__int16*)buf_;
  257. for(sample = 0; sample < samples; sample++)
  258. *buf1_++ = (FLAC__int16)signal[0][sample];
  259. }
  260. else
  261. #endif
  262. if(bytes_per_sample == 2) {
  263. if(channels == 2) {
  264. for(sample = 0; sample < samples; sample++) {
  265. a_word = signal[0][sample];
  266. *buf_++ = (FLAC__byte)a_word; a_word >>= 8;
  267. *buf_++ = (FLAC__byte)a_word;
  268. a_word = signal[1][sample];
  269. *buf_++ = (FLAC__byte)a_word; a_word >>= 8;
  270. *buf_++ = (FLAC__byte)a_word;
  271. }
  272. }
  273. else if(channels == 1) {
  274. for(sample = 0; sample < samples; sample++) {
  275. a_word = signal[0][sample];
  276. *buf_++ = (FLAC__byte)a_word; a_word >>= 8;
  277. *buf_++ = (FLAC__byte)a_word;
  278. }
  279. }
  280. else {
  281. for(sample = 0; sample < samples; sample++) {
  282. for(channel = 0; channel < channels; channel++) {
  283. a_word = signal[channel][sample];
  284. *buf_++ = (FLAC__byte)a_word; a_word >>= 8;
  285. *buf_++ = (FLAC__byte)a_word;
  286. }
  287. }
  288. }
  289. }
  290. else if(bytes_per_sample == 3) {
  291. if(channels == 2) {
  292. for(sample = 0; sample < samples; sample++) {
  293. a_word = signal[0][sample];
  294. *buf_++ = (FLAC__byte)a_word; a_word >>= 8;
  295. *buf_++ = (FLAC__byte)a_word; a_word >>= 8;
  296. *buf_++ = (FLAC__byte)a_word;
  297. a_word = signal[1][sample];
  298. *buf_++ = (FLAC__byte)a_word; a_word >>= 8;
  299. *buf_++ = (FLAC__byte)a_word; a_word >>= 8;
  300. *buf_++ = (FLAC__byte)a_word;
  301. }
  302. }
  303. else if(channels == 1) {
  304. for(sample = 0; sample < samples; sample++) {
  305. a_word = signal[0][sample];
  306. *buf_++ = (FLAC__byte)a_word; a_word >>= 8;
  307. *buf_++ = (FLAC__byte)a_word; a_word >>= 8;
  308. *buf_++ = (FLAC__byte)a_word;
  309. }
  310. }
  311. else {
  312. for(sample = 0; sample < samples; sample++) {
  313. for(channel = 0; channel < channels; channel++) {
  314. a_word = signal[channel][sample];
  315. *buf_++ = (FLAC__byte)a_word; a_word >>= 8;
  316. *buf_++ = (FLAC__byte)a_word; a_word >>= 8;
  317. *buf_++ = (FLAC__byte)a_word;
  318. }
  319. }
  320. }
  321. }
  322. else if(bytes_per_sample == 1) {
  323. if(channels == 2) {
  324. for(sample = 0; sample < samples; sample++) {
  325. a_word = signal[0][sample];
  326. *buf_++ = (FLAC__byte)a_word;
  327. a_word = signal[1][sample];
  328. *buf_++ = (FLAC__byte)a_word;
  329. }
  330. }
  331. else if(channels == 1) {
  332. for(sample = 0; sample < samples; sample++) {
  333. a_word = signal[0][sample];
  334. *buf_++ = (FLAC__byte)a_word;
  335. }
  336. }
  337. else {
  338. for(sample = 0; sample < samples; sample++) {
  339. for(channel = 0; channel < channels; channel++) {
  340. a_word = signal[channel][sample];
  341. *buf_++ = (FLAC__byte)a_word;
  342. }
  343. }
  344. }
  345. }
  346. else { /* bytes_per_sample == 4, maybe optimize more later */
  347. for(sample = 0; sample < samples; sample++) {
  348. for(channel = 0; channel < channels; channel++) {
  349. a_word = signal[channel][sample];
  350. *buf_++ = (FLAC__byte)a_word; a_word >>= 8;
  351. *buf_++ = (FLAC__byte)a_word; a_word >>= 8;
  352. *buf_++ = (FLAC__byte)a_word; a_word >>= 8;
  353. *buf_++ = (FLAC__byte)a_word;
  354. }
  355. }
  356. }
  357. }
  358. /*
  359. * Convert the incoming audio signal to a byte stream and FLAC__MD5Update it.
  360. */
  361. FLAC__bool FLAC__MD5Accumulate(FLAC__MD5Context *ctx, const FLAC__int32 * const signal[], unsigned channels, unsigned samples, unsigned bytes_per_sample)
  362. {
  363. const size_t bytes_needed = (size_t)channels * (size_t)samples * (size_t)bytes_per_sample;
  364. /* overflow check */
  365. if((size_t)channels > SIZE_MAX / (size_t)bytes_per_sample)
  366. return false;
  367. if((size_t)channels * (size_t)bytes_per_sample > SIZE_MAX / (size_t)samples)
  368. return false;
  369. if(ctx->capacity < bytes_needed) {
  370. FLAC__byte *tmp = (FLAC__byte*) realloc(ctx->internal_buf, bytes_needed);
  371. if(0 == tmp) {
  372. free(ctx->internal_buf);
  373. if(0 == (ctx->internal_buf = (FLAC__byte*) safe_malloc_(bytes_needed)))
  374. return false;
  375. }
  376. else
  377. ctx->internal_buf = tmp;
  378. ctx->capacity = bytes_needed;
  379. }
  380. format_input_(ctx->internal_buf, signal, channels, samples, bytes_per_sample);
  381. FLAC__MD5Update(ctx, ctx->internal_buf, bytes_needed);
  382. return true;
  383. }