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.

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