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