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