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.

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