Coverage Report

Created: 2025-07-23 06:41

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