The "GR-PEACH_Audio_Playback_7InchLCD_Sample" is a sample code that can provides high-resolution audio playback of FLAC format files. It also allows the user to audio-playback control functions such as play, pause, and stop by manipulating key switches.

Dependencies:   GR-PEACH_video R_BSP TLV320_RBSP USBHost_custom

Fork of GR-PEACH_Audio_Playback_Sample by Renesas

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers md5.c Source File

md5.c

00001 #ifdef HAVE_CONFIG_H
00002 #  include <config.h>
00003 #endif
00004 
00005 #include <stdlib.h>     /* for malloc() */
00006 #include <string.h>     /* for memcpy() */
00007 
00008 #include "private/md5.h"
00009 #include "share/alloc.h"
00010 #include "share/endswap.h"
00011 
00012 /*
00013  * This code implements the MD5 message-digest algorithm.
00014  * The algorithm is due to Ron Rivest.  This code was
00015  * written by Colin Plumb in 1993, no copyright is claimed.
00016  * This code is in the public domain; do with it what you wish.
00017  *
00018  * Equivalent code is available from RSA Data Security, Inc.
00019  * This code has been tested against that, and is equivalent,
00020  * except that you don't need to include two pages of legalese
00021  * with every copy.
00022  *
00023  * To compute the message digest of a chunk of bytes, declare an
00024  * MD5Context structure, pass it to MD5Init, call MD5Update as
00025  * needed on buffers full of bytes, and then call MD5Final, which
00026  * will fill a supplied 16-byte array with the digest.
00027  *
00028  * Changed so as no longer to depend on Colin Plumb's `usual.h' header
00029  * definitions; now uses stuff from dpkg's config.h.
00030  *  - Ian Jackson <ijackson@nyx.cs.du.edu>.
00031  * Still in the public domain.
00032  *
00033  * Josh Coalson: made some changes to integrate with libFLAC.
00034  * Still in the public domain.
00035  */
00036 
00037 /* The four core functions - F1 is optimized somewhat */
00038 
00039 /* #define F1(x, y, z) (x & y | ~x & z) */
00040 #define F1(x, y, z) (z ^ (x & (y ^ z)))
00041 #define F2(x, y, z) F1(z, x, y)
00042 #define F3(x, y, z) (x ^ y ^ z)
00043 #define F4(x, y, z) (y ^ (x | ~z))
00044 
00045 /* This is the central step in the MD5 algorithm. */
00046 #define MD5STEP(f,w,x,y,z,in,s) \
00047      (w += f(x,y,z) + in, w = (w<<s | w>>(32-s)) + x)
00048 
00049 /*
00050  * The core of the MD5 algorithm, this alters an existing MD5 hash to
00051  * reflect the addition of 16 longwords of new data.  MD5Update blocks
00052  * the data and converts bytes into longwords for this routine.
00053  */
00054 static void FLAC__MD5Transform(FLAC__uint32 buf[4], FLAC__uint32 const in[16])
00055 {
00056     register FLAC__uint32 a, b, c, d;
00057 
00058     a = buf[0];
00059     b = buf[1];
00060     c = buf[2];
00061     d = buf[3];
00062 
00063     MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
00064     MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
00065     MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
00066     MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
00067     MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
00068     MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
00069     MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
00070     MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
00071     MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
00072     MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
00073     MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
00074     MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
00075     MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
00076     MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
00077     MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
00078     MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
00079 
00080     MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
00081     MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
00082     MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
00083     MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
00084     MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
00085     MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
00086     MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
00087     MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
00088     MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
00089     MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
00090     MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
00091     MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
00092     MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
00093     MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
00094     MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
00095     MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
00096 
00097     MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
00098     MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
00099     MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
00100     MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
00101     MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
00102     MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
00103     MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
00104     MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
00105     MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
00106     MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
00107     MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
00108     MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
00109     MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
00110     MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
00111     MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
00112     MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
00113 
00114     MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
00115     MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
00116     MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
00117     MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
00118     MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
00119     MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
00120     MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
00121     MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
00122     MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
00123     MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
00124     MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
00125     MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
00126     MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
00127     MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
00128     MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
00129     MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
00130 
00131     buf[0] += a;
00132     buf[1] += b;
00133     buf[2] += c;
00134     buf[3] += d;
00135 }
00136 
00137 #if WORDS_BIGENDIAN
00138 //@@@@@@ OPT: use bswap/intrinsics
00139 static void byteSwap(FLAC__uint32 *buf, unsigned words)
00140 {
00141     register FLAC__uint32 x;
00142     do {
00143         x = *buf;
00144         x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff);
00145         *buf++ = (x >> 16) | (x << 16);
00146     } while (--words);
00147 }
00148 static void byteSwapX16(FLAC__uint32 *buf)
00149 {
00150     register FLAC__uint32 x;
00151 
00152     x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
00153     x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
00154     x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
00155     x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
00156     x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
00157     x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
00158     x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
00159     x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
00160     x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
00161     x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
00162     x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
00163     x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
00164     x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
00165     x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
00166     x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
00167     x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf   = (x >> 16) | (x << 16);
00168 }
00169 #else
00170 #define byteSwap(buf, words)
00171 #define byteSwapX16(buf)
00172 #endif
00173 
00174 /*
00175  * Update context to reflect the concatenation of another buffer full
00176  * of bytes.
00177  */
00178 static void FLAC__MD5Update(FLAC__MD5Context *ctx, FLAC__byte const *buf, unsigned len)
00179 {
00180     FLAC__uint32 t;
00181 
00182     /* Update byte count */
00183 
00184     t = ctx->bytes[0];
00185     if ((ctx->bytes[0] = t + len) < t)
00186         ctx->bytes[1]++;    /* Carry from low to high */
00187 
00188     t = 64 - (t & 0x3f);    /* Space available in ctx->in (at least 1) */
00189     if (t > len) {
00190         memcpy((FLAC__byte *)ctx->in + 64 - t, buf, len);
00191         return;
00192     }
00193     /* First chunk is an odd size */
00194     memcpy((FLAC__byte *)ctx->in + 64 - t, buf, t);
00195     byteSwapX16(ctx->in);
00196     FLAC__MD5Transform(ctx->buf, ctx->in);
00197     buf += t;
00198     len -= t;
00199 
00200     /* Process data in 64-byte chunks */
00201     while (len >= 64) {
00202         memcpy(ctx->in, buf, 64);
00203         byteSwapX16(ctx->in);
00204         FLAC__MD5Transform(ctx->buf, ctx->in);
00205         buf += 64;
00206         len -= 64;
00207     }
00208 
00209     /* Handle any remaining bytes of data. */
00210     memcpy(ctx->in, buf, len);
00211 }
00212 
00213 /*
00214  * Start MD5 accumulation.  Set bit count to 0 and buffer to mysterious
00215  * initialization constants.
00216  */
00217 void FLAC__MD5Init(FLAC__MD5Context *ctx)
00218 {
00219     ctx->buf[0] = 0x67452301;
00220     ctx->buf[1] = 0xefcdab89;
00221     ctx->buf[2] = 0x98badcfe;
00222     ctx->buf[3] = 0x10325476;
00223 
00224     ctx->bytes[0] = 0;
00225     ctx->bytes[1] = 0;
00226 
00227     ctx->internal_buf.p8= 0;
00228     ctx->capacity = 0;
00229 }
00230 
00231 /*
00232  * Final wrapup - pad to 64-byte boundary with the bit pattern
00233  * 1 0* (64-bit count of bits processed, MSB-first)
00234  */
00235 void FLAC__MD5Final(FLAC__byte digest[16], FLAC__MD5Context *ctx)
00236 {
00237     int count = ctx->bytes[0] & 0x3f;   /* Number of bytes in ctx->in */
00238     FLAC__byte *p = (FLAC__byte *)ctx->in + count;
00239 
00240     /* Set the first char of padding to 0x80.  There is always room. */
00241     *p++ = 0x80;
00242 
00243     /* Bytes of padding needed to make 56 bytes (-8..55) */
00244     count = 56 - 1 - count;
00245 
00246     if (count < 0) {    /* Padding forces an extra block */
00247         memset(p, 0, count + 8);
00248         byteSwapX16(ctx->in);
00249         FLAC__MD5Transform(ctx->buf, ctx->in);
00250         p = (FLAC__byte *)ctx->in;
00251         count = 56;
00252     }
00253     memset(p, 0, count);
00254     byteSwap(ctx->in, 14);
00255 
00256     /* Append length in bits and transform */
00257     ctx->in[14] = ctx->bytes[0] << 3;
00258     ctx->in[15] = ctx->bytes[1] << 3 | ctx->bytes[0] >> 29;
00259     FLAC__MD5Transform(ctx->buf, ctx->in);
00260 
00261     byteSwap(ctx->buf, 4);
00262     memcpy(digest, ctx->buf, 16);
00263     if (0 != ctx->internal_buf.p8) {
00264         free(ctx->internal_buf.p8);
00265         ctx->internal_buf.p8= 0;
00266         ctx->capacity = 0;
00267     }
00268     memset(ctx, 0, sizeof(*ctx));   /* In case it's sensitive */
00269 }
00270 
00271 /*
00272  * Convert the incoming audio signal to a byte stream
00273  */
00274 static void format_input_(FLAC__multibyte *mbuf, const FLAC__int32 * const signal[], unsigned channels, unsigned samples, unsigned bytes_per_sample)
00275 {
00276     FLAC__byte *buf_ = mbuf->p8;
00277     FLAC__int16 *buf16 = mbuf->p16;
00278     FLAC__int32 *buf32 = mbuf->p32;
00279     FLAC__int32 a_word;
00280     unsigned channel, sample;
00281 
00282     /* Storage in the output buffer, buf, is little endian. */
00283 
00284 #define BYTES_CHANNEL_SELECTOR(bytes, channels)   (bytes * 100 + channels)
00285 
00286     /* First do the most commonly used combinations. */
00287     switch (BYTES_CHANNEL_SELECTOR (bytes_per_sample, channels)) {
00288         /* One byte per sample. */
00289         case (BYTES_CHANNEL_SELECTOR (1, 1)):
00290             for (sample = 0; sample < samples; sample++)
00291                 *buf_++ = signal[0][sample];
00292             return;
00293 
00294         case (BYTES_CHANNEL_SELECTOR (1, 2)):
00295             for (sample = 0; sample < samples; sample++) {
00296                 *buf_++ = signal[0][sample];
00297                 *buf_++ = signal[1][sample];
00298             }
00299             return;
00300 
00301         case (BYTES_CHANNEL_SELECTOR (1, 4)):
00302             for (sample = 0; sample < samples; sample++) {
00303                 *buf_++ = signal[0][sample];
00304                 *buf_++ = signal[1][sample];
00305                 *buf_++ = signal[2][sample];
00306                 *buf_++ = signal[3][sample];
00307             }
00308             return;
00309 
00310         case (BYTES_CHANNEL_SELECTOR (1, 6)):
00311             for (sample = 0; sample < samples; sample++) {
00312                 *buf_++ = signal[0][sample];
00313                 *buf_++ = signal[1][sample];
00314                 *buf_++ = signal[2][sample];
00315                 *buf_++ = signal[3][sample];
00316                 *buf_++ = signal[4][sample];
00317                 *buf_++ = signal[5][sample];
00318             }
00319             return;
00320 
00321         case (BYTES_CHANNEL_SELECTOR (1, 8)):
00322             for (sample = 0; sample < samples; sample++) {
00323                 *buf_++ = signal[0][sample];
00324                 *buf_++ = signal[1][sample];
00325                 *buf_++ = signal[2][sample];
00326                 *buf_++ = signal[3][sample];
00327                 *buf_++ = signal[4][sample];
00328                 *buf_++ = signal[5][sample];
00329                 *buf_++ = signal[6][sample];
00330                 *buf_++ = signal[7][sample];
00331             }
00332             return;
00333 
00334         /* Two bytes per sample. */
00335         case (BYTES_CHANNEL_SELECTOR (2, 1)):
00336             for (sample = 0; sample < samples; sample++)
00337                 *buf16++ = H2LE_16(signal[0][sample]);
00338             return;
00339 
00340         case (BYTES_CHANNEL_SELECTOR (2, 2)):
00341             for (sample = 0; sample < samples; sample++) {
00342                 *buf16++ = H2LE_16(signal[0][sample]);
00343                 *buf16++ = H2LE_16(signal[1][sample]);
00344             }
00345             return;
00346 
00347         case (BYTES_CHANNEL_SELECTOR (2, 4)):
00348             for (sample = 0; sample < samples; sample++) {
00349                 *buf16++ = H2LE_16(signal[0][sample]);
00350                 *buf16++ = H2LE_16(signal[1][sample]);
00351                 *buf16++ = H2LE_16(signal[2][sample]);
00352                 *buf16++ = H2LE_16(signal[3][sample]);
00353             }
00354             return;
00355 
00356         case (BYTES_CHANNEL_SELECTOR (2, 6)):
00357             for (sample = 0; sample < samples; sample++) {
00358                 *buf16++ = H2LE_16(signal[0][sample]);
00359                 *buf16++ = H2LE_16(signal[1][sample]);
00360                 *buf16++ = H2LE_16(signal[2][sample]);
00361                 *buf16++ = H2LE_16(signal[3][sample]);
00362                 *buf16++ = H2LE_16(signal[4][sample]);
00363                 *buf16++ = H2LE_16(signal[5][sample]);
00364             }
00365             return;
00366 
00367         case (BYTES_CHANNEL_SELECTOR (2, 8)):
00368             for (sample = 0; sample < samples; sample++) {
00369                 *buf16++ = H2LE_16(signal[0][sample]);
00370                 *buf16++ = H2LE_16(signal[1][sample]);
00371                 *buf16++ = H2LE_16(signal[2][sample]);
00372                 *buf16++ = H2LE_16(signal[3][sample]);
00373                 *buf16++ = H2LE_16(signal[4][sample]);
00374                 *buf16++ = H2LE_16(signal[5][sample]);
00375                 *buf16++ = H2LE_16(signal[6][sample]);
00376                 *buf16++ = H2LE_16(signal[7][sample]);
00377             }
00378             return;
00379 
00380         /* Three bytes per sample. */
00381         case (BYTES_CHANNEL_SELECTOR (3, 1)):
00382             for (sample = 0; sample < samples; sample++) {
00383                 a_word = signal[0][sample];
00384                 *buf_++ = (FLAC__byte)a_word; a_word >>= 8;
00385                 *buf_++ = (FLAC__byte)a_word; a_word >>= 8;
00386                 *buf_++ = (FLAC__byte)a_word;
00387             }
00388             return;
00389 
00390         case (BYTES_CHANNEL_SELECTOR (3, 2)):
00391             for (sample = 0; sample < samples; sample++) {
00392                 a_word = signal[0][sample];
00393                 *buf_++ = (FLAC__byte)a_word; a_word >>= 8;
00394                 *buf_++ = (FLAC__byte)a_word; a_word >>= 8;
00395                 *buf_++ = (FLAC__byte)a_word;
00396                 a_word = signal[1][sample];
00397                 *buf_++ = (FLAC__byte)a_word; a_word >>= 8;
00398                 *buf_++ = (FLAC__byte)a_word; a_word >>= 8;
00399                 *buf_++ = (FLAC__byte)a_word;
00400             }
00401             return;
00402 
00403         /* Four bytes per sample. */
00404         case (BYTES_CHANNEL_SELECTOR (4, 1)):
00405             for (sample = 0; sample < samples; sample++)
00406                 *buf32++ = H2LE_32(signal[0][sample]);
00407             return;
00408 
00409         case (BYTES_CHANNEL_SELECTOR (4, 2)):
00410             for (sample = 0; sample < samples; sample++) {
00411                 *buf32++ = H2LE_32(signal[0][sample]);
00412                 *buf32++ = H2LE_32(signal[1][sample]);
00413             }
00414             return;
00415 
00416         case (BYTES_CHANNEL_SELECTOR (4, 4)):
00417             for (sample = 0; sample < samples; sample++) {
00418                 *buf32++ = H2LE_32(signal[0][sample]);
00419                 *buf32++ = H2LE_32(signal[1][sample]);
00420                 *buf32++ = H2LE_32(signal[2][sample]);
00421                 *buf32++ = H2LE_32(signal[3][sample]);
00422             }
00423             return;
00424 
00425         case (BYTES_CHANNEL_SELECTOR (4, 6)):
00426             for (sample = 0; sample < samples; sample++) {
00427                 *buf32++ = H2LE_32(signal[0][sample]);
00428                 *buf32++ = H2LE_32(signal[1][sample]);
00429                 *buf32++ = H2LE_32(signal[2][sample]);
00430                 *buf32++ = H2LE_32(signal[3][sample]);
00431                 *buf32++ = H2LE_32(signal[4][sample]);
00432                 *buf32++ = H2LE_32(signal[5][sample]);
00433             }
00434             return;
00435 
00436         case (BYTES_CHANNEL_SELECTOR (4, 8)):
00437             for (sample = 0; sample < samples; sample++) {
00438                 *buf32++ = H2LE_32(signal[0][sample]);
00439                 *buf32++ = H2LE_32(signal[1][sample]);
00440                 *buf32++ = H2LE_32(signal[2][sample]);
00441                 *buf32++ = H2LE_32(signal[3][sample]);
00442                 *buf32++ = H2LE_32(signal[4][sample]);
00443                 *buf32++ = H2LE_32(signal[5][sample]);
00444                 *buf32++ = H2LE_32(signal[6][sample]);
00445                 *buf32++ = H2LE_32(signal[7][sample]);
00446             }
00447             return;
00448 
00449         default:
00450             break;
00451     }
00452 
00453     /* General version. */
00454     switch (bytes_per_sample) {
00455         case 1:
00456             for (sample = 0; sample < samples; sample++)
00457                 for (channel = 0; channel < channels; channel++)
00458                     *buf_++ = signal[channel][sample];
00459             return;
00460 
00461         case 2:
00462             for (sample = 0; sample < samples; sample++)
00463                 for (channel = 0; channel < channels; channel++)
00464                     *buf16++ = H2LE_16(signal[channel][sample]);
00465             return;
00466 
00467         case 3:
00468             for (sample = 0; sample < samples; sample++)
00469                 for (channel = 0; channel < channels; channel++) {
00470                     a_word = signal[channel][sample];
00471                     *buf_++ = (FLAC__byte)a_word; a_word >>= 8;
00472                     *buf_++ = (FLAC__byte)a_word; a_word >>= 8;
00473                     *buf_++ = (FLAC__byte)a_word;
00474                 }
00475             return;
00476 
00477         case 4:
00478             for (sample = 0; sample < samples; sample++)
00479                 for (channel = 0; channel < channels; channel++)
00480                     *buf32++ = H2LE_32(signal[channel][sample]);
00481             return;
00482 
00483         default:
00484             break;
00485     }
00486 }
00487 
00488 /*
00489  * Convert the incoming audio signal to a byte stream and FLAC__MD5Update it.
00490  */
00491 FLAC__bool FLAC__MD5Accumulate(FLAC__MD5Context *ctx, const FLAC__int32 * const signal[], unsigned channels, unsigned samples, unsigned bytes_per_sample)
00492 {
00493     const size_t bytes_needed = (size_t)channels * (size_t)samples * (size_t)bytes_per_sample;
00494 
00495     /* overflow check */
00496     if ((size_t)channels > SIZE_MAX / (size_t)bytes_per_sample)
00497         return false;
00498     if ((size_t)channels * (size_t)bytes_per_sample > SIZE_MAX / (size_t)samples)
00499         return false;
00500 
00501     if (ctx->capacity < bytes_needed) {
00502         FLAC__byte *tmp = realloc(ctx->internal_buf.p8, bytes_needed);
00503         if (0 == tmp) {
00504             free(ctx->internal_buf.p8);
00505             if (0 == (ctx->internal_buf.p8= safe_malloc_(bytes_needed)))
00506                 return false;
00507         }
00508         else
00509             ctx->internal_buf.p8= tmp;
00510         ctx->capacity = bytes_needed;
00511     }
00512 
00513     format_input_(&ctx->internal_buf, signal, channels, samples, bytes_per_sample);
00514 
00515     FLAC__MD5Update(ctx, ctx->internal_buf.p8, bytes_needed);
00516 
00517     return true;
00518 }