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.

518 lines
17KB

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