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