Coverage Report

Created: 2026-06-09 06:28

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