Coverage Report

Created: 2025-12-14 06:19

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/flac/src/libFLAC/md5.c
Line
Count
Source
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
186M
#define F1(x, y, z) (z ^ (x & (y ^ z)))
42
93.0M
#define F2(x, y, z) F1(z, x, y)
43
93.0M
#define F3(x, y, z) (x ^ y ^ z)
44
93.0M
#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
372M
   (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
5.81M
{
62
5.81M
  register FLAC__uint32 a, b, c, d;
63
64
5.81M
  a = buf[0];
65
5.81M
  b = buf[1];
66
5.81M
  c = buf[2];
67
5.81M
  d = buf[3];
68
69
5.81M
  MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
70
5.81M
  MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
71
5.81M
  MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
72
5.81M
  MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
73
5.81M
  MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
74
5.81M
  MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
75
5.81M
  MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
76
5.81M
  MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
77
5.81M
  MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
78
5.81M
  MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
79
5.81M
  MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
80
5.81M
  MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
81
5.81M
  MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
82
5.81M
  MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
83
5.81M
  MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
84
5.81M
  MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
85
86
5.81M
  MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
87
5.81M
  MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
88
5.81M
  MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
89
5.81M
  MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
90
5.81M
  MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
91
5.81M
  MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
92
5.81M
  MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
93
5.81M
  MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
94
5.81M
  MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
95
5.81M
  MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
96
5.81M
  MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
97
5.81M
  MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
98
5.81M
  MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
99
5.81M
  MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
100
5.81M
  MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
101
5.81M
  MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
102
103
5.81M
  MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
104
5.81M
  MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
105
5.81M
  MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
106
5.81M
  MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
107
5.81M
  MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
108
5.81M
  MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
109
5.81M
  MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
110
5.81M
  MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
111
5.81M
  MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
112
5.81M
  MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
113
5.81M
  MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
114
5.81M
  MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
115
5.81M
  MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
116
5.81M
  MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
117
5.81M
  MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
118
5.81M
  MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
119
120
5.81M
  MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
121
5.81M
  MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
122
5.81M
  MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
123
5.81M
  MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
124
5.81M
  MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
125
5.81M
  MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
126
5.81M
  MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
127
5.81M
  MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
128
5.81M
  MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
129
5.81M
  MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
130
5.81M
  MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
131
5.81M
  MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
132
5.81M
  MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
133
5.81M
  MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
134
5.81M
  MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
135
5.81M
  MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
136
137
5.81M
  buf[0] += a;
138
5.81M
  buf[1] += b;
139
5.81M
  buf[2] += c;
140
5.81M
  buf[3] += d;
141
5.81M
}
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
2.15M
{
186
2.15M
  FLAC__uint32 t;
187
188
  /* Update byte count */
189
190
2.15M
  t = ctx->bytes[0];
191
2.15M
  if ((ctx->bytes[0] = t + len) < t)
192
0
    ctx->bytes[1]++; /* Carry from low to high */
193
194
2.15M
  t = 64 - (t & 0x3f);  /* Space available in ctx->in (at least 1) */
195
2.15M
  if (t > len) {
196
282k
    memcpy((FLAC__byte *)ctx->in + 64 - t, buf, len);
197
282k
    return;
198
282k
  }
199
  /* First chunk is an odd size */
200
1.87M
  memcpy((FLAC__byte *)ctx->in + 64 - t, buf, t);
201
1.87M
  byteSwapX16(ctx->in);
202
1.87M
  FLAC__MD5Transform(ctx->buf, ctx->in);
203
1.87M
  buf += t;
204
1.87M
  len -= t;
205
206
  /* Process data in 64-byte chunks */
207
5.79M
  while (len >= 64) {
208
3.91M
    memcpy(ctx->in, buf, 64);
209
3.91M
    byteSwapX16(ctx->in);
210
3.91M
    FLAC__MD5Transform(ctx->buf, ctx->in);
211
3.91M
    buf += 64;
212
3.91M
    len -= 64;
213
3.91M
  }
214
215
  /* Handle any remaining bytes of data. */
216
1.87M
  memcpy(ctx->in, buf, len);
217
1.87M
}
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
19.7k
{
225
19.7k
  ctx->buf[0] = 0x67452301;
226
19.7k
  ctx->buf[1] = 0xefcdab89;
227
19.7k
  ctx->buf[2] = 0x98badcfe;
228
19.7k
  ctx->buf[3] = 0x10325476;
229
230
19.7k
  ctx->bytes[0] = 0;
231
19.7k
  ctx->bytes[1] = 0;
232
233
19.7k
  ctx->internal_buf.p8 = 0;
234
19.7k
  ctx->capacity = 0;
235
19.7k
}
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
20.0k
{
243
20.0k
  int count = ctx->bytes[0] & 0x3f; /* Number of bytes in ctx->in */
244
20.0k
  FLAC__byte *p = (FLAC__byte *)ctx->in + count;
245
246
  /* Set the first char of padding to 0x80.  There is always room. */
247
20.0k
  *p++ = 0x80;
248
249
  /* Bytes of padding needed to make 56 bytes (-8..55) */
250
20.0k
  count = 56 - 1 - count;
251
252
20.0k
  if (count < 0) { /* Padding forces an extra block */
253
870
    memset(p, 0, count + 8);
254
870
    byteSwapX16(ctx->in);
255
870
    FLAC__MD5Transform(ctx->buf, ctx->in);
256
870
    p = (FLAC__byte *)ctx->in;
257
870
    count = 56;
258
870
  }
259
20.0k
  memset(p, 0, count);
260
20.0k
  byteSwap(ctx->in, 14);
261
262
  /* Append length in bits and transform */
263
20.0k
  ctx->in[14] = ctx->bytes[0] << 3;
264
20.0k
  ctx->in[15] = ctx->bytes[1] << 3 | ctx->bytes[0] >> 29;
265
20.0k
  FLAC__MD5Transform(ctx->buf, ctx->in);
266
267
20.0k
  byteSwap(ctx->buf, 4);
268
20.0k
  memcpy(digest, ctx->buf, 16);
269
20.0k
  if (0 != ctx->internal_buf.p8) {
270
10.8k
    free(ctx->internal_buf.p8);
271
10.8k
    ctx->internal_buf.p8 = 0;
272
10.8k
    ctx->capacity = 0;
273
10.8k
  }
274
20.0k
  memset(ctx, 0, sizeof(*ctx)); /* In case it's sensitive */
275
20.0k
}
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
2.15M
{
282
2.15M
  FLAC__byte *buf_ = mbuf->p8;
283
2.15M
  FLAC__int16 *buf16 = mbuf->p16;
284
2.15M
  FLAC__int32 *buf32 = mbuf->p32;
285
2.15M
  FLAC__int32 a_word;
286
2.15M
  uint32_t channel, sample;
287
288
  /* Storage in the output buffer, buf, is little endian. */
289
290
4.31M
#define BYTES_CHANNEL_SELECTOR(bytes, channels)   (bytes * 100 + channels)
291
292
  /* First do the most commonly used combinations. */
293
2.15M
  switch (BYTES_CHANNEL_SELECTOR (bytes_per_sample, channels)) {
294
    /* One byte per sample. */
295
18.4k
    case (BYTES_CHANNEL_SELECTOR (1, 1)):
296
4.98M
      for (sample = 0; sample < samples; sample++)
297
4.96M
        *buf_++ = signal[0][sample];
298
18.4k
      return;
299
300
1.54k
    case (BYTES_CHANNEL_SELECTOR (1, 2)):
301
241k
      for (sample = 0; sample < samples; sample++) {
302
239k
        *buf_++ = signal[0][sample];
303
239k
        *buf_++ = signal[1][sample];
304
239k
      }
305
1.54k
      return;
306
307
624
    case (BYTES_CHANNEL_SELECTOR (1, 4)):
308
107k
      for (sample = 0; sample < samples; sample++) {
309
106k
        *buf_++ = signal[0][sample];
310
106k
        *buf_++ = signal[1][sample];
311
106k
        *buf_++ = signal[2][sample];
312
106k
        *buf_++ = signal[3][sample];
313
106k
      }
314
624
      return;
315
316
226
    case (BYTES_CHANNEL_SELECTOR (1, 6)):
317
3.76k
      for (sample = 0; sample < samples; sample++) {
318
3.53k
        *buf_++ = signal[0][sample];
319
3.53k
        *buf_++ = signal[1][sample];
320
3.53k
        *buf_++ = signal[2][sample];
321
3.53k
        *buf_++ = signal[3][sample];
322
3.53k
        *buf_++ = signal[4][sample];
323
3.53k
        *buf_++ = signal[5][sample];
324
3.53k
      }
325
226
      return;
326
327
633
    case (BYTES_CHANNEL_SELECTOR (1, 8)):
328
10.7k
      for (sample = 0; sample < samples; sample++) {
329
10.0k
        *buf_++ = signal[0][sample];
330
10.0k
        *buf_++ = signal[1][sample];
331
10.0k
        *buf_++ = signal[2][sample];
332
10.0k
        *buf_++ = signal[3][sample];
333
10.0k
        *buf_++ = signal[4][sample];
334
10.0k
        *buf_++ = signal[5][sample];
335
10.0k
        *buf_++ = signal[6][sample];
336
10.0k
        *buf_++ = signal[7][sample];
337
10.0k
      }
338
633
      return;
339
340
    /* Two bytes per sample. */
341
555k
    case (BYTES_CHANNEL_SELECTOR (2, 1)):
342
12.4M
      for (sample = 0; sample < samples; sample++)
343
11.9M
        *buf16++ = H2LE_16(signal[0][sample]);
344
555k
      return;
345
346
4.46k
    case (BYTES_CHANNEL_SELECTOR (2, 2)):
347
607k
      for (sample = 0; sample < samples; sample++) {
348
602k
        *buf16++ = H2LE_16(signal[0][sample]);
349
602k
        *buf16++ = H2LE_16(signal[1][sample]);
350
602k
      }
351
4.46k
      return;
352
353
613
    case (BYTES_CHANNEL_SELECTOR (2, 4)):
354
10.5k
      for (sample = 0; sample < samples; sample++) {
355
9.90k
        *buf16++ = H2LE_16(signal[0][sample]);
356
9.90k
        *buf16++ = H2LE_16(signal[1][sample]);
357
9.90k
        *buf16++ = H2LE_16(signal[2][sample]);
358
9.90k
        *buf16++ = H2LE_16(signal[3][sample]);
359
9.90k
      }
360
613
      return;
361
362
503
    case (BYTES_CHANNEL_SELECTOR (2, 6)):
363
308k
      for (sample = 0; sample < samples; sample++) {
364
308k
        *buf16++ = H2LE_16(signal[0][sample]);
365
308k
        *buf16++ = H2LE_16(signal[1][sample]);
366
308k
        *buf16++ = H2LE_16(signal[2][sample]);
367
308k
        *buf16++ = H2LE_16(signal[3][sample]);
368
308k
        *buf16++ = H2LE_16(signal[4][sample]);
369
308k
        *buf16++ = H2LE_16(signal[5][sample]);
370
308k
      }
371
503
      return;
372
373
1.31k
    case (BYTES_CHANNEL_SELECTOR (2, 8)):
374
58.8k
      for (sample = 0; sample < samples; sample++) {
375
57.5k
        *buf16++ = H2LE_16(signal[0][sample]);
376
57.5k
        *buf16++ = H2LE_16(signal[1][sample]);
377
57.5k
        *buf16++ = H2LE_16(signal[2][sample]);
378
57.5k
        *buf16++ = H2LE_16(signal[3][sample]);
379
57.5k
        *buf16++ = H2LE_16(signal[4][sample]);
380
57.5k
        *buf16++ = H2LE_16(signal[5][sample]);
381
57.5k
        *buf16++ = H2LE_16(signal[6][sample]);
382
57.5k
        *buf16++ = H2LE_16(signal[7][sample]);
383
57.5k
      }
384
1.31k
      return;
385
386
    /* Three bytes per sample. */
387
444k
    case (BYTES_CHANNEL_SELECTOR (3, 1)):
388
18.2M
      for (sample = 0; sample < samples; sample++) {
389
17.7M
        a_word = signal[0][sample];
390
17.7M
        *buf_++ = (FLAC__byte)a_word; a_word >>= 8;
391
17.7M
        *buf_++ = (FLAC__byte)a_word; a_word >>= 8;
392
17.7M
        *buf_++ = (FLAC__byte)a_word;
393
17.7M
      }
394
444k
      return;
395
396
16.8k
    case (BYTES_CHANNEL_SELECTOR (3, 2)):
397
429k
      for (sample = 0; sample < samples; sample++) {
398
412k
        a_word = signal[0][sample];
399
412k
        *buf_++ = (FLAC__byte)a_word; a_word >>= 8;
400
412k
        *buf_++ = (FLAC__byte)a_word; a_word >>= 8;
401
412k
        *buf_++ = (FLAC__byte)a_word;
402
412k
        a_word = signal[1][sample];
403
412k
        *buf_++ = (FLAC__byte)a_word; a_word >>= 8;
404
412k
        *buf_++ = (FLAC__byte)a_word; a_word >>= 8;
405
412k
        *buf_++ = (FLAC__byte)a_word;
406
412k
      }
407
16.8k
      return;
408
409
    /* Four bytes per sample. */
410
1.07M
    case (BYTES_CHANNEL_SELECTOR (4, 1)):
411
37.6M
      for (sample = 0; sample < samples; sample++)
412
36.5M
        *buf32++ = H2LE_32(signal[0][sample]);
413
1.07M
      return;
414
415
32.3k
    case (BYTES_CHANNEL_SELECTOR (4, 2)):
416
11.1M
      for (sample = 0; sample < samples; sample++) {
417
11.0M
        *buf32++ = H2LE_32(signal[0][sample]);
418
11.0M
        *buf32++ = H2LE_32(signal[1][sample]);
419
11.0M
      }
420
32.3k
      return;
421
422
817
    case (BYTES_CHANNEL_SELECTOR (4, 4)):
423
56.6k
      for (sample = 0; sample < samples; sample++) {
424
55.8k
        *buf32++ = H2LE_32(signal[0][sample]);
425
55.8k
        *buf32++ = H2LE_32(signal[1][sample]);
426
55.8k
        *buf32++ = H2LE_32(signal[2][sample]);
427
55.8k
        *buf32++ = H2LE_32(signal[3][sample]);
428
55.8k
      }
429
817
      return;
430
431
837
    case (BYTES_CHANNEL_SELECTOR (4, 6)):
432
71.6k
      for (sample = 0; sample < samples; sample++) {
433
70.8k
        *buf32++ = H2LE_32(signal[0][sample]);
434
70.8k
        *buf32++ = H2LE_32(signal[1][sample]);
435
70.8k
        *buf32++ = H2LE_32(signal[2][sample]);
436
70.8k
        *buf32++ = H2LE_32(signal[3][sample]);
437
70.8k
        *buf32++ = H2LE_32(signal[4][sample]);
438
70.8k
        *buf32++ = H2LE_32(signal[5][sample]);
439
70.8k
      }
440
837
      return;
441
442
786
    case (BYTES_CHANNEL_SELECTOR (4, 8)):
443
1.05M
      for (sample = 0; sample < samples; sample++) {
444
1.05M
        *buf32++ = H2LE_32(signal[0][sample]);
445
1.05M
        *buf32++ = H2LE_32(signal[1][sample]);
446
1.05M
        *buf32++ = H2LE_32(signal[2][sample]);
447
1.05M
        *buf32++ = H2LE_32(signal[3][sample]);
448
1.05M
        *buf32++ = H2LE_32(signal[4][sample]);
449
1.05M
        *buf32++ = H2LE_32(signal[5][sample]);
450
1.05M
        *buf32++ = H2LE_32(signal[6][sample]);
451
1.05M
        *buf32++ = H2LE_32(signal[7][sample]);
452
1.05M
      }
453
786
      return;
454
455
3.20k
    default:
456
3.20k
      break;
457
2.15M
  }
458
459
  /* General version. */
460
3.20k
  switch (bytes_per_sample) {
461
309
    case 1:
462
5.17k
      for (sample = 0; sample < samples; sample++)
463
19.8k
        for (channel = 0; channel < channels; channel++)
464
14.9k
          *buf_++ = signal[channel][sample];
465
309
      return;
466
467
549
    case 2:
468
9.42k
      for (sample = 0; sample < samples; sample++)
469
37.5k
        for (channel = 0; channel < channels; channel++)
470
28.6k
          *buf16++ = H2LE_16(signal[channel][sample]);
471
549
      return;
472
473
394
    case 3:
474
6.69k
      for (sample = 0; sample < samples; sample++)
475
32.7k
        for (channel = 0; channel < channels; channel++) {
476
26.4k
          a_word = signal[channel][sample];
477
26.4k
          *buf_++ = (FLAC__byte)a_word; a_word >>= 8;
478
26.4k
          *buf_++ = (FLAC__byte)a_word; a_word >>= 8;
479
26.4k
          *buf_++ = (FLAC__byte)a_word;
480
26.4k
        }
481
394
      return;
482
483
1.94k
    case 4:
484
373k
      for (sample = 0; sample < samples; sample++)
485
2.15M
        for (channel = 0; channel < channels; channel++)
486
1.78M
          *buf32++ = H2LE_32(signal[channel][sample]);
487
1.94k
      return;
488
489
0
    default:
490
0
      break;
491
3.20k
  }
492
3.20k
}
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
2.15M
{
499
2.15M
  const size_t bytes_needed = (size_t)channels * (size_t)samples * (size_t)bytes_per_sample;
500
501
  /* overflow check */
502
2.15M
  if ((size_t)channels > SIZE_MAX / (size_t)bytes_per_sample)
503
0
    return false;
504
2.15M
  if ((size_t)channels * (size_t)bytes_per_sample > SIZE_MAX / (size_t)samples)
505
0
    return false;
506
507
2.15M
  if (ctx->capacity < bytes_needed) {
508
11.7k
    if (0 == (ctx->internal_buf.p8 = safe_realloc_(ctx->internal_buf.p8, bytes_needed))) {
509
83
      if (0 == (ctx->internal_buf.p8 = safe_malloc_(bytes_needed))) {
510
0
        ctx->capacity = 0;
511
0
        return false;
512
0
      }
513
83
    }
514
11.7k
    ctx->capacity = bytes_needed;
515
11.7k
  }
516
517
2.15M
  format_input_(&ctx->internal_buf, signal, channels, samples, bytes_per_sample);
518
519
2.15M
  FLAC__MD5Update(ctx, ctx->internal_buf.p8, bytes_needed);
520
521
  return true;
522
2.15M
}