From d7e703d72e473653185d0e28cbe6c33ee723aa93 Mon Sep 17 00:00:00 2001
From: Ben Allison <benski@winamp.com>
Date: Sun, 3 Mar 2013 15:32:46 -0500
Subject: [PATCH] * MSVC compatibility fixes

---
 src/libFLAC/bitreader.c               | 37 ++++++++++++++++++-----------------
 src/libFLAC/bitwriter.c               | 14 ++++++-------
 src/libFLAC/include/private/bitmath.h |  4 ++++
 src/libFLAC/lpc.c                     |  2 +-
 src/libFLAC/metadata_object.c         |  5 +++++
 src/libFLAC/stream_decoder.c          |  4 ++++
 6 files changed, 40 insertions(+), 26 deletions(-)

diff --git a/src/libFLAC/bitreader.c b/src/libFLAC/bitreader.c
index 792d6dd..1698376 100644
--- a/src/libFLAC/bitreader.c
+++ b/src/libFLAC/bitreader.c
@@ -40,17 +40,18 @@
 #include "private/crc.h"
 #include "private/macros.h"
 #include "FLAC/assert.h"
+#include "FLAC/ordinals.h"
 #include "share/compat.h"
 #include "share/endswap.h"
 
 /* Things should be fastest when this matches the machine word size */
 /* WATCHOUT: if you change this you must also change the following #defines down to FLAC__clz_uint32 below to match */
-/* WATCHOUT: there are a few places where the code will not work unless uint32_t is >= 32 bits wide */
+/* WATCHOUT: there are a few places where the code will not work unless FLAC__uint32 is >= 32 bits wide */
 /*           also, some sections currently only have fast versions for 4 or 8 bytes per word */
-#define FLAC__BYTES_PER_WORD 4		/* sizeof uint32_t */
+#define FLAC__BYTES_PER_WORD 4		/* sizeof FLAC__uint32 */
 #define FLAC__BITS_PER_WORD (8 * FLAC__BYTES_PER_WORD)
 #define FLAC__WORD_ALL_ONES ((FLAC__uint32)0xffffffff)
-/* SWAP_BE_WORD_TO_HOST swaps bytes in a uint32_t (which is always big-endian) if necessary to match host byte order */
+/* SWAP_BE_WORD_TO_HOST swaps bytes in a FLAC__uint32 (which is always big-endian) if necessary to match host byte order */
 #if WORDS_BIGENDIAN
 #define SWAP_BE_WORD_TO_HOST(x) (x)
 #else
@@ -77,7 +78,7 @@ static const unsigned FLAC__BITREADER_DEFAULT_CAPACITY = 65536u / FLAC__BITS_PER
 struct FLAC__BitReader {
 	/* any partially-consumed word at the head will stay right-justified as bits are consumed from the left */
 	/* any incomplete word at the tail will be left-justified, and bytes from the read callback are added on the right */
-	uint32_t *buffer;
+	FLAC__uint32 *buffer;
 	unsigned capacity; /* in words */
 	unsigned words; /* # of completed words in buffer */
 	unsigned bytes; /* # of bytes in incomplete word at buffer[words] */
@@ -90,7 +91,7 @@ struct FLAC__BitReader {
 	FLAC__CPUInfo cpu_info;
 };
 
-static inline void crc16_update_word_(FLAC__BitReader *br, uint32_t word)
+static __inline void crc16_update_word_(FLAC__BitReader *br, FLAC__uint32 word)
 {
 	register unsigned crc = br->read_crc16;
 #if FLAC__BYTES_PER_WORD == 4
@@ -144,7 +145,7 @@ FLAC__bool bitreader_read_from_client_(FLAC__BitReader *br)
 		return false; /* no space left, buffer is too small; see note for FLAC__BITREADER_DEFAULT_CAPACITY  */
 	target = ((FLAC__byte*)(br->buffer+br->words)) + br->bytes;
 
-	/* before reading, if the existing reader looks like this (say uint32_t is 32 bits wide)
+	/* before reading, if the existing reader looks like this (say FLAC__uint32 is 32 bits wide)
 	 *   bitstream :  11 22 33 44 55            br->words=1 br->bytes=1 (partial tail word is left-justified)
 	 *   buffer[BE]:  11 22 33 44 55 ?? ?? ??   (shown layed out as bytes sequentially in memory)
 	 *   buffer[LE]:  44 33 22 11 ?? ?? ?? 55   (?? being don't-care)
@@ -238,7 +239,7 @@ FLAC__bool FLAC__bitreader_init(FLAC__BitReader *br, FLAC__CPUInfo cpu, FLAC__Bi
 	br->words = br->bytes = 0;
 	br->consumed_words = br->consumed_bits = 0;
 	br->capacity = FLAC__BITREADER_DEFAULT_CAPACITY;
-	br->buffer = malloc(sizeof(uint32_t) * br->capacity);
+	br->buffer = malloc(sizeof(FLAC__uint32) * br->capacity);
 	if(br->buffer == 0)
 		return false;
 	br->read_callback = rcb;
@@ -318,24 +319,24 @@ FLAC__uint16 FLAC__bitreader_get_read_crc16(FLAC__BitReader *br)
 
 	/* CRC any tail bytes in a partially-consumed word */
 	if(br->consumed_bits) {
-		const uint32_t tail = br->buffer[br->consumed_words];
+		const FLAC__uint32 tail = br->buffer[br->consumed_words];
 		for( ; br->crc16_align < br->consumed_bits; br->crc16_align += 8)
 			br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)((tail >> (FLAC__BITS_PER_WORD-8-br->crc16_align)) & 0xff), br->read_crc16);
 	}
 	return br->read_crc16;
 }
 
-inline FLAC__bool FLAC__bitreader_is_consumed_byte_aligned(const FLAC__BitReader *br)
+FLAC__bool FLAC__bitreader_is_consumed_byte_aligned(const FLAC__BitReader *br)
 {
 	return ((br->consumed_bits & 7) == 0);
 }
 
-inline unsigned FLAC__bitreader_bits_left_for_byte_alignment(const FLAC__BitReader *br)
+unsigned FLAC__bitreader_bits_left_for_byte_alignment(const FLAC__BitReader *br)
 {
 	return 8 - (br->consumed_bits & 7);
 }
 
-inline unsigned FLAC__bitreader_get_input_bits_unconsumed(const FLAC__BitReader *br)
+unsigned FLAC__bitreader_get_input_bits_unconsumed(const FLAC__BitReader *br)
 {
 	return (br->words-br->consumed_words)*FLAC__BITS_PER_WORD + br->bytes*8 - br->consumed_bits;
 }
@@ -366,7 +367,7 @@ FLAC__bool FLAC__bitreader_read_raw_uint32(FLAC__BitReader *br, FLAC__uint32 *va
 		if(br->consumed_bits) {
 			/* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
 			const unsigned n = FLAC__BITS_PER_WORD - br->consumed_bits;
-			const uint32_t word = br->buffer[br->consumed_words];
+			const FLAC__uint32 word = br->buffer[br->consumed_words];
 			if(bits < n) {
 				*val = (word & (FLAC__WORD_ALL_ONES >> br->consumed_bits)) >> (n-bits);
 				br->consumed_bits += bits;
@@ -385,7 +386,7 @@ FLAC__bool FLAC__bitreader_read_raw_uint32(FLAC__BitReader *br, FLAC__uint32 *va
 			return true;
 		}
 		else {
-			const uint32_t word = br->buffer[br->consumed_words];
+			const FLAC__uint32 word = br->buffer[br->consumed_words];
 			if(bits < FLAC__BITS_PER_WORD) {
 				*val = word >> (FLAC__BITS_PER_WORD-bits);
 				br->consumed_bits = bits;
@@ -451,7 +452,7 @@ FLAC__bool FLAC__bitreader_read_raw_uint64(FLAC__BitReader *br, FLAC__uint64 *va
 	return true;
 }
 
-inline FLAC__bool FLAC__bitreader_read_uint32_little_endian(FLAC__BitReader *br, FLAC__uint32 *val)
+FLAC__bool FLAC__bitreader_read_uint32_little_endian(FLAC__BitReader *br, FLAC__uint32 *val)
 {
 	FLAC__uint32 x8, x32 = 0;
 
@@ -566,7 +567,7 @@ FLAC__bool FLAC__bitreader_read_byte_block_aligned_no_crc(FLAC__BitReader *br, F
 	/* step 2: read whole words in chunks */
 	while(nvals >= FLAC__BYTES_PER_WORD) {
 		if(br->consumed_words < br->words) {
-			const uint32_t word = br->buffer[br->consumed_words++];
+			const FLAC__uint32 word = br->buffer[br->consumed_words++];
 #if FLAC__BYTES_PER_WORD == 4
 			val[0] = (FLAC__byte)(word >> 24);
 			val[1] = (FLAC__byte)(word >> 16);
@@ -631,7 +632,7 @@ FLAC__bool FLAC__bitreader_read_unary_unsigned(FLAC__BitReader *br, unsigned *va
 	*val = 0;
 	while(1) {
 		while(br->consumed_words < br->words) { /* if we've not consumed up to a partial tail word... */
-			uint32_t b = br->buffer[br->consumed_words] << br->consumed_bits;
+			FLAC__uint32 b = br->buffer[br->consumed_words] << br->consumed_bits;
 			if(b) {
 				i = FLAC__clz_uint32(b);
 				*val += i;
@@ -661,7 +662,7 @@ FLAC__bool FLAC__bitreader_read_unary_unsigned(FLAC__BitReader *br, unsigned *va
 		 */
 		if(br->bytes*8 > br->consumed_bits) {
 			const unsigned end = br->bytes * 8;
-			uint32_t b = (br->buffer[br->consumed_words] & (FLAC__WORD_ALL_ONES << (FLAC__BITS_PER_WORD-end))) << br->consumed_bits;
+			FLAC__uint32 b = (br->buffer[br->consumed_words] & (FLAC__WORD_ALL_ONES << (FLAC__BITS_PER_WORD-end))) << br->consumed_bits;
 			if(b) {
 				i = FLAC__clz_uint32(b);
 				*val += i;
@@ -718,7 +719,7 @@ FLAC__bool FLAC__bitreader_read_rice_signed_block(FLAC__BitReader *br, int vals[
 	 * bitreader functions that use them, and before returning */
 	unsigned cwords, words, lsbs, msbs, x, y;
 	unsigned ucbits; /* keep track of the number of unconsumed bits in word */
-	uint32_t b;
+	FLAC__uint32 b;
 	int *val, *end;
 
 	FLAC__ASSERT(0 != br);
diff --git a/src/libFLAC/bitwriter.c b/src/libFLAC/bitwriter.c
index 7d61d73..e3f8093 100644
--- a/src/libFLAC/bitwriter.c
+++ b/src/libFLAC/bitwriter.c
@@ -62,16 +62,16 @@
  * a frame or metadata block, then write that out and clear the buffer for the
  * next one.
  */
-static const unsigned FLAC__BITWRITER_DEFAULT_CAPACITY = 32768u / sizeof(uint32_t); /* size in words */
+static const unsigned FLAC__BITWRITER_DEFAULT_CAPACITY = 32768u / sizeof(FLAC__uint32); /* size in words */
 /* When growing, increment 4K at a time */
-static const unsigned FLAC__BITWRITER_DEFAULT_INCREMENT = 4096u / sizeof(uint32_t); /* size in words */
+static const unsigned FLAC__BITWRITER_DEFAULT_INCREMENT = 4096u / sizeof(FLAC__uint32); /* size in words */
 
 #define FLAC__WORDS_TO_BITS(words) ((words) * FLAC__BITS_PER_WORD)
 #define FLAC__TOTAL_BITS(bw) (FLAC__WORDS_TO_BITS((bw)->words) + (bw)->bits)
 
 struct FLAC__BitWriter {
-	uint32_t *buffer;
-	uint32_t accum; /* accumulator; bits are right-justified; when full, accum is appended to buffer */
+	FLAC__uint32 *buffer;
+	FLAC__uint32 accum; /* accumulator; bits are right-justified; when full, accum is appended to buffer */
 	unsigned capacity; /* capacity of buffer in words */
 	unsigned words; /* # of complete words in buffer */
 	unsigned bits; /* # of used bits in accum */
@@ -81,7 +81,7 @@ struct FLAC__BitWriter {
 static FLAC__bool bitwriter_grow_(FLAC__BitWriter *bw, unsigned bits_to_add)
 {
 	unsigned new_capacity;
-	uint32_t *new_buffer;
+	FLAC__uint32 *new_buffer;
 
 	FLAC__ASSERT(0 != bw);
 	FLAC__ASSERT(0 != bw->buffer);
@@ -103,7 +103,7 @@ static FLAC__bool bitwriter_grow_(FLAC__BitWriter *bw, unsigned bits_to_add)
 	FLAC__ASSERT(new_capacity > bw->capacity);
 	FLAC__ASSERT(new_capacity >= bw->words + ((bw->bits + bits_to_add + FLAC__BITS_PER_WORD - 1) / FLAC__BITS_PER_WORD));
 
-	new_buffer = safe_realloc_mul_2op_(bw->buffer, sizeof(uint32_t), /*times*/new_capacity);
+	new_buffer = safe_realloc_mul_2op_(bw->buffer, sizeof(FLAC__uint32), /*times*/new_capacity);
 	if(new_buffer == 0)
 		return false;
 	bw->buffer = new_buffer;
@@ -145,7 +145,7 @@ FLAC__bool FLAC__bitwriter_init(FLAC__BitWriter *bw)
 
 	bw->words = bw->bits = 0;
 	bw->capacity = FLAC__BITWRITER_DEFAULT_CAPACITY;
-	bw->buffer = malloc(sizeof(uint32_t) * bw->capacity);
+	bw->buffer = malloc(sizeof(FLAC__uint32) * bw->capacity);
 	if(bw->buffer == 0)
 		return false;
 
diff --git a/src/libFLAC/include/private/bitmath.h b/src/libFLAC/include/private/bitmath.h
index 4e60f78..ed35364 100644
--- a/src/libFLAC/include/private/bitmath.h
+++ b/src/libFLAC/include/private/bitmath.h
@@ -41,6 +41,10 @@
 #include <intrin.h> /* for _BitScanReverse* */
 #endif
 
+#if defined(_MSC_VER)
+#define inline __inline
+#endif
+
 /* Will never be emitted for MSVC, GCC, Intel compilers */
 static inline unsigned int FLAC__clz_soft_uint32(unsigned int word)
 {
diff --git a/src/libFLAC/lpc.c b/src/libFLAC/lpc.c
index 66a6899..f70e90b 100644
--- a/src/libFLAC/lpc.c
+++ b/src/libFLAC/lpc.c
@@ -34,7 +34,7 @@
 #endif
 
 #include <math.h>
-#include <inttypes.h>
+#include "FLAC/ordinals.h"
 #include "FLAC/assert.h"
 #include "FLAC/format.h"
 #include "private/bitmath.h"
diff --git a/src/libFLAC/metadata_object.c b/src/libFLAC/metadata_object.c
index 22d39d4..44be093 100644
--- a/src/libFLAC/metadata_object.c
+++ b/src/libFLAC/metadata_object.c
@@ -43,6 +43,11 @@
 #include "share/alloc.h"
 #include "share/compat.h"
 
+#if defined(_MSC_VER) && !defined(UINT32_MAX)
+#include <limits.h>
+#define UINT32_MAX _UI32_MAX
+#endif
+
 /* Alias the first (in share/alloc.h) to the second (in src/libFLAC/memory.c). */
 #define safe_malloc_mul_2op_ safe_malloc_mul_2op_p
 
diff --git a/src/libFLAC/stream_decoder.c b/src/libFLAC/stream_decoder.c
index 789db1b..33aa66e 100644
--- a/src/libFLAC/stream_decoder.c
+++ b/src/libFLAC/stream_decoder.c
@@ -55,7 +55,11 @@
 
 
 /* technically this should be in an "export.c" but this is convenient enough */
+#if defined(FLAC__HAS_OGG)
 FLAC_API int FLAC_API_SUPPORTS_OGG_FLAC = FLAC__HAS_OGG ;
+#else
+FLAC_API int FLAC_API_SUPPORTS_OGG_FLAC = 0 ;
+#endif
 
 
 /***********************************************************************
-- 
1.7.11.msysgit.1

