A set of windows utf8 patches fromJanne Hyvärinen <cse@sci.fi>.
[platform/upstream/flac.git] / src / libFLAC / md5.c
index 85330f9..e251d50 100644 (file)
@@ -1,3 +1,13 @@
+#if HAVE_CONFIG_H
+#  include <config.h>
+#endif
+
+#include <stdlib.h>            /* for malloc() */
+#include <string.h>            /* for memcpy() */
+
+#include "private/md5.h"
+#include "share/alloc.h"
+
 /*
  * This code implements the MD5 message-digest algorithm.
  * The algorithm is due to Ron Rivest.  This code was
  * Still in the public domain.
  */
 
-#include <assert.h>            /* for assert() */
-#include <stdlib.h>            /* for malloc() */
-#include <string.h>            /* for memcpy() */
-
-#include "private/md5.h"
-
-static bool is_big_endian_host_;
-
-void
-byteSwap(uint32 *buf, unsigned words)
-{
-       md5byte *p = (md5byte *)buf;
-
-       if(!is_big_endian_host_)
-               return;
-       do {
-               *buf++ = (uint32)((unsigned)p[3] << 8 | p[2]) << 16 | ((unsigned)p[1] << 8 | p[0]);
-               p += 4;
-       } while (--words);
-}
-
-/*
- * Start MD5 accumulation.  Set bit count to 0 and buffer to mysterious
- * initialization constants.
- */
-void
-MD5Init(struct MD5Context *ctx)
-{
-    uint32 test = 1;
-
-    is_big_endian_host_ = (*((byte*)(&test)))? false : true;
-
-       ctx->buf[0] = 0x67452301;
-       ctx->buf[1] = 0xefcdab89;
-       ctx->buf[2] = 0x98badcfe;
-       ctx->buf[3] = 0x10325476;
-
-       ctx->bytes[0] = 0;
-       ctx->bytes[1] = 0;
-
-       ctx->internal_buf = 0;
-       ctx->capacity = 0;
-}
-
-/*
- * Update context to reflect the concatenation of another buffer full
- * of bytes.
- */
-void
-MD5Update(struct MD5Context *ctx, md5byte const *buf, unsigned len)
-{
-       uint32 t;
-
-       /* Update byte count */
-
-       t = ctx->bytes[0];
-       if ((ctx->bytes[0] = t + len) < t)
-               ctx->bytes[1]++;        /* Carry from low to high */
-
-       t = 64 - (t & 0x3f);    /* Space available in ctx->in (at least 1) */
-       if (t > len) {
-               memcpy((md5byte *)ctx->in + 64 - t, buf, len);
-               return;
-       }
-       /* First chunk is an odd size */
-       memcpy((md5byte *)ctx->in + 64 - t, buf, t);
-       byteSwap(ctx->in, 16);
-       MD5Transform(ctx->buf, ctx->in);
-       buf += t;
-       len -= t;
-
-       /* Process data in 64-byte chunks */
-       while (len >= 64) {
-               memcpy(ctx->in, buf, 64);
-               byteSwap(ctx->in, 16);
-               MD5Transform(ctx->buf, ctx->in);
-               buf += 64;
-               len -= 64;
-       }
-
-       /* Handle any remaining bytes of data. */
-       memcpy(ctx->in, buf, len);
-}
-
-/*
- * Convert the incoming audio signal to a byte stream and MD5Update it.
- */
-bool
-FLAC__MD5Accumulate(struct MD5Context *ctx, const int32 *signal[], unsigned channels, unsigned samples, unsigned bytes_per_sample)
-{
-       unsigned channel, sample, a_byte;
-       int32 a_word;
-       byte *buf_;
-       const unsigned bytes_needed = channels * samples * bytes_per_sample;
-
-       if(ctx->capacity < bytes_needed) {
-               byte *tmp = realloc(ctx->internal_buf, bytes_needed);
-               if(0 == tmp) {
-                       free(ctx->internal_buf);
-                       if(0 == (ctx->internal_buf = malloc(bytes_needed)))
-                               return false;
-               }
-               ctx->internal_buf = tmp;
-               ctx->capacity = bytes_needed;
-       }
-
-       buf_ = ctx->internal_buf;
-
-       for(sample = 0; sample < samples; sample++) {
-               for(channel = 0; channel < channels; channel++) {
-                       a_word = signal[channel][sample];
-                       for(a_byte = 0; a_byte < bytes_per_sample; a_byte++) {
-                               *buf_++ = (byte)(a_word & 0xff);
-                               a_word >>= 8;
-                       }
-               }
-       }
-
-       MD5Update(ctx, ctx->internal_buf, bytes_needed);
-
-       return true;
-}
-
-/*
- * Final wrapup - pad to 64-byte boundary with the bit pattern
- * 1 0* (64-bit count of bits processed, MSB-first)
- */
-void
-MD5Final(md5byte digest[16], struct MD5Context *ctx)
-{
-       int count = ctx->bytes[0] & 0x3f;       /* Number of bytes in ctx->in */
-       md5byte *p = (md5byte *)ctx->in + count;
-
-       /* Set the first char of padding to 0x80.  There is always room. */
-       *p++ = 0x80;
-
-       /* Bytes of padding needed to make 56 bytes (-8..55) */
-       count = 56 - 1 - count;
-
-       if (count < 0) {        /* Padding forces an extra block */
-               memset(p, 0, count + 8);
-               byteSwap(ctx->in, 16);
-               MD5Transform(ctx->buf, ctx->in);
-               p = (md5byte *)ctx->in;
-               count = 56;
-       }
-       memset(p, 0, count);
-       byteSwap(ctx->in, 14);
-
-       /* Append length in bits and transform */
-       ctx->in[14] = ctx->bytes[0] << 3;
-       ctx->in[15] = ctx->bytes[1] << 3 | ctx->bytes[0] >> 29;
-       MD5Transform(ctx->buf, ctx->in);
-
-       byteSwap(ctx->buf, 4);
-       memcpy(digest, ctx->buf, 16);
-       memset(ctx, 0, sizeof(ctx));    /* In case it's sensitive */
-       if(0 != ctx->internal_buf) {
-               free(ctx->internal_buf);
-               ctx->internal_buf = 0;
-               ctx->capacity = 0;
-       }
-}
-
-#ifndef ASM_MD5
-
 /* The four core functions - F1 is optimized somewhat */
 
 /* #define F1(x, y, z) (x & y | ~x & z) */
@@ -206,10 +50,9 @@ MD5Final(md5byte digest[16], struct MD5Context *ctx)
  * reflect the addition of 16 longwords of new data.  MD5Update blocks
  * the data and converts bytes into longwords for this routine.
  */
-void
-MD5Transform(uint32 buf[4], uint32 const in[16])
+static void FLAC__MD5Transform(FLAC__uint32 buf[4], FLAC__uint32 const in[16])
 {
-       register uint32 a, b, c, d;
+       register FLAC__uint32 a, b, c, d;
 
        a = buf[0];
        b = buf[1];
@@ -290,4 +133,289 @@ MD5Transform(uint32 buf[4], uint32 const in[16])
        buf[3] += d;
 }
 
+#if WORDS_BIGENDIAN
+//@@@@@@ OPT: use bswap/intrinsics
+static void byteSwap(FLAC__uint32 *buf, unsigned words)
+{
+       register FLAC__uint32 x;
+       do {
+               x = *buf;
+               x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff);
+               *buf++ = (x >> 16) | (x << 16);
+       } while (--words);
+}
+static void byteSwapX16(FLAC__uint32 *buf)
+{
+       register FLAC__uint32 x;
+
+       x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
+       x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
+       x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
+       x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
+       x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
+       x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
+       x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
+       x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
+       x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
+       x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
+       x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
+       x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
+       x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
+       x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
+       x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf++ = (x >> 16) | (x << 16);
+       x = *buf; x = ((x << 8) & 0xff00ff00) | ((x >> 8) & 0x00ff00ff); *buf   = (x >> 16) | (x << 16);
+}
+#else
+#define byteSwap(buf, words)
+#define byteSwapX16(buf)
+#endif
+
+/*
+ * Update context to reflect the concatenation of another buffer full
+ * of bytes.
+ */
+static void FLAC__MD5Update(FLAC__MD5Context *ctx, FLAC__byte const *buf, unsigned len)
+{
+       FLAC__uint32 t;
+
+       /* Update byte count */
+
+       t = ctx->bytes[0];
+       if ((ctx->bytes[0] = t + len) < t)
+               ctx->bytes[1]++;        /* Carry from low to high */
+
+       t = 64 - (t & 0x3f);    /* Space available in ctx->in (at least 1) */
+       if (t > len) {
+               memcpy((FLAC__byte *)ctx->in + 64 - t, buf, len);
+               return;
+       }
+       /* First chunk is an odd size */
+       memcpy((FLAC__byte *)ctx->in + 64 - t, buf, t);
+       byteSwapX16(ctx->in);
+       FLAC__MD5Transform(ctx->buf, ctx->in);
+       buf += t;
+       len -= t;
+
+       /* Process data in 64-byte chunks */
+       while (len >= 64) {
+               memcpy(ctx->in, buf, 64);
+               byteSwapX16(ctx->in);
+               FLAC__MD5Transform(ctx->buf, ctx->in);
+               buf += 64;
+               len -= 64;
+       }
+
+       /* Handle any remaining bytes of data. */
+       memcpy(ctx->in, buf, len);
+}
+
+/*
+ * Start MD5 accumulation.  Set bit count to 0 and buffer to mysterious
+ * initialization constants.
+ */
+void FLAC__MD5Init(FLAC__MD5Context *ctx)
+{
+       ctx->buf[0] = 0x67452301;
+       ctx->buf[1] = 0xefcdab89;
+       ctx->buf[2] = 0x98badcfe;
+       ctx->buf[3] = 0x10325476;
+
+       ctx->bytes[0] = 0;
+       ctx->bytes[1] = 0;
+
+       ctx->internal_buf = 0;
+       ctx->capacity = 0;
+}
+
+/*
+ * Final wrapup - pad to 64-byte boundary with the bit pattern
+ * 1 0* (64-bit count of bits processed, MSB-first)
+ */
+void FLAC__MD5Final(FLAC__byte digest[16], FLAC__MD5Context *ctx)
+{
+       int count = ctx->bytes[0] & 0x3f;       /* Number of bytes in ctx->in */
+       FLAC__byte *p = (FLAC__byte *)ctx->in + count;
+
+       /* Set the first char of padding to 0x80.  There is always room. */
+       *p++ = 0x80;
+
+       /* Bytes of padding needed to make 56 bytes (-8..55) */
+       count = 56 - 1 - count;
+
+       if (count < 0) {        /* Padding forces an extra block */
+               memset(p, 0, count + 8);
+               byteSwapX16(ctx->in);
+               FLAC__MD5Transform(ctx->buf, ctx->in);
+               p = (FLAC__byte *)ctx->in;
+               count = 56;
+       }
+       memset(p, 0, count);
+       byteSwap(ctx->in, 14);
+
+       /* Append length in bits and transform */
+       ctx->in[14] = ctx->bytes[0] << 3;
+       ctx->in[15] = ctx->bytes[1] << 3 | ctx->bytes[0] >> 29;
+       FLAC__MD5Transform(ctx->buf, ctx->in);
+
+       byteSwap(ctx->buf, 4);
+       memcpy(digest, ctx->buf, 16);
+       if(0 != ctx->internal_buf) {
+               free(ctx->internal_buf);
+               ctx->internal_buf = 0;
+               ctx->capacity = 0;
+       }
+       memset(ctx, 0, sizeof(*ctx));   /* In case it's sensitive */
+}
+
+/*
+ * Convert the incoming audio signal to a byte stream
+ */
+static void format_input_(FLAC__byte *buf, const FLAC__int32 * const signal[], unsigned channels, unsigned samples, unsigned bytes_per_sample)
+{
+       unsigned channel, sample;
+       register FLAC__int32 a_word;
+       register FLAC__byte *buf_ = buf;
+
+#if WORDS_BIGENDIAN
+#else
+       if(channels == 2 && bytes_per_sample == 2) {
+               FLAC__int16 *buf1_ = ((FLAC__int16*)buf_) + 1;
+               memcpy(buf_, signal[0], sizeof(FLAC__int32) * samples);
+               for(sample = 0; sample < samples; sample++, buf1_+=2)
+                       *buf1_ = (FLAC__int16)signal[1][sample];
+       }
+       else if(channels == 1 && bytes_per_sample == 2) {
+               FLAC__int16 *buf1_ = (FLAC__int16*)buf_;
+               for(sample = 0; sample < samples; sample++)
+                       *buf1_++ = (FLAC__int16)signal[0][sample];
+       }
+       else
 #endif
+       if(bytes_per_sample == 2) {
+               if(channels == 2) {
+                       for(sample = 0; sample < samples; sample++) {
+                               a_word = signal[0][sample];
+                               *buf_++ = (FLAC__byte)a_word; a_word >>= 8;
+                               *buf_++ = (FLAC__byte)a_word;
+                               a_word = signal[1][sample];
+                               *buf_++ = (FLAC__byte)a_word; a_word >>= 8;
+                               *buf_++ = (FLAC__byte)a_word;
+                       }
+               }
+               else if(channels == 1) {
+                       for(sample = 0; sample < samples; sample++) {
+                               a_word = signal[0][sample];
+                               *buf_++ = (FLAC__byte)a_word; a_word >>= 8;
+                               *buf_++ = (FLAC__byte)a_word;
+                       }
+               }
+               else {
+                       for(sample = 0; sample < samples; sample++) {
+                               for(channel = 0; channel < channels; channel++) {
+                                       a_word = signal[channel][sample];
+                                       *buf_++ = (FLAC__byte)a_word; a_word >>= 8;
+                                       *buf_++ = (FLAC__byte)a_word;
+                               }
+                       }
+               }
+       }
+       else if(bytes_per_sample == 3) {
+               if(channels == 2) {
+                       for(sample = 0; sample < samples; sample++) {
+                               a_word = signal[0][sample];
+                               *buf_++ = (FLAC__byte)a_word; a_word >>= 8;
+                               *buf_++ = (FLAC__byte)a_word; a_word >>= 8;
+                               *buf_++ = (FLAC__byte)a_word;
+                               a_word = signal[1][sample];
+                               *buf_++ = (FLAC__byte)a_word; a_word >>= 8;
+                               *buf_++ = (FLAC__byte)a_word; a_word >>= 8;
+                               *buf_++ = (FLAC__byte)a_word;
+                       }
+               }
+               else if(channels == 1) {
+                       for(sample = 0; sample < samples; sample++) {
+                               a_word = signal[0][sample];
+                               *buf_++ = (FLAC__byte)a_word; a_word >>= 8;
+                               *buf_++ = (FLAC__byte)a_word; a_word >>= 8;
+                               *buf_++ = (FLAC__byte)a_word;
+                       }
+               }
+               else {
+                       for(sample = 0; sample < samples; sample++) {
+                               for(channel = 0; channel < channels; channel++) {
+                                       a_word = signal[channel][sample];
+                                       *buf_++ = (FLAC__byte)a_word; a_word >>= 8;
+                                       *buf_++ = (FLAC__byte)a_word; a_word >>= 8;
+                                       *buf_++ = (FLAC__byte)a_word;
+                               }
+                       }
+               }
+       }
+       else if(bytes_per_sample == 1) {
+               if(channels == 2) {
+                       for(sample = 0; sample < samples; sample++) {
+                               a_word = signal[0][sample];
+                               *buf_++ = (FLAC__byte)a_word;
+                               a_word = signal[1][sample];
+                               *buf_++ = (FLAC__byte)a_word;
+                       }
+               }
+               else if(channels == 1) {
+                       for(sample = 0; sample < samples; sample++) {
+                               a_word = signal[0][sample];
+                               *buf_++ = (FLAC__byte)a_word;
+                       }
+               }
+               else {
+                       for(sample = 0; sample < samples; sample++) {
+                               for(channel = 0; channel < channels; channel++) {
+                                       a_word = signal[channel][sample];
+                                       *buf_++ = (FLAC__byte)a_word;
+                               }
+                       }
+               }
+       }
+       else { /* bytes_per_sample == 4, maybe optimize more later */
+               for(sample = 0; sample < samples; sample++) {
+                       for(channel = 0; channel < channels; channel++) {
+                               a_word = signal[channel][sample];
+                               *buf_++ = (FLAC__byte)a_word; a_word >>= 8;
+                               *buf_++ = (FLAC__byte)a_word; a_word >>= 8;
+                               *buf_++ = (FLAC__byte)a_word; a_word >>= 8;
+                               *buf_++ = (FLAC__byte)a_word;
+                       }
+               }
+       }
+}
+
+/*
+ * Convert the incoming audio signal to a byte stream and FLAC__MD5Update it.
+ */
+FLAC__bool FLAC__MD5Accumulate(FLAC__MD5Context *ctx, const FLAC__int32 * const signal[], unsigned channels, unsigned samples, unsigned bytes_per_sample)
+{
+       const size_t bytes_needed = (size_t)channels * (size_t)samples * (size_t)bytes_per_sample;
+
+       /* overflow check */
+       if((size_t)channels > SIZE_MAX / (size_t)bytes_per_sample)
+               return false;
+       if((size_t)channels * (size_t)bytes_per_sample > SIZE_MAX / (size_t)samples)
+               return false;
+
+       if(ctx->capacity < bytes_needed) {
+               FLAC__byte *tmp = realloc(ctx->internal_buf, bytes_needed);
+               if(0 == tmp) {
+                       free(ctx->internal_buf);
+                       if(0 == (ctx->internal_buf = safe_malloc_(bytes_needed)))
+                               return false;
+               }
+               else
+                       ctx->internal_buf = tmp;
+               ctx->capacity = bytes_needed;
+       }
+
+       format_input_(ctx->internal_buf, signal, channels, samples, bytes_per_sample);
+
+       FLAC__MD5Update(ctx, ctx->internal_buf, bytes_needed);
+
+       return true;
+}