minor comments
[flac.git] / src / libFLAC / bitbuffer.c
index 082871e..f6b1b74 100644 (file)
@@ -1,22 +1,38 @@
 /* libFLAC - Free Lossless Audio Codec library
- * Copyright (C) 2000,2001,2002  Josh Coalson
+ * Copyright (C) 2000,2001,2002,2003,2004,2005,2006  Josh Coalson
  *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Library General Public
- * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
  *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Library General Public License for more details.
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
  *
- * You should have received a copy of the GNU Library General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA  02111-1307, USA.
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * - Neither the name of the Xiph.org Foundation nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+#if HAVE_CONFIG_H
+#  include <config.h>
+#endif
+
 #include <stdlib.h> /* for malloc() */
 #include <string.h> /* for memcpy(), memset() */
 #include "private/bitbuffer.h"
  */
 
 /*
+ * Some optimization strategies are slower with older versions of MSVC
+ */
+#if defined _MSC_VER && _MSC_VER <= 1200
+#define FLAC__OLD_MSVC_FLAVOR
+#endif
+
+/*
  * This should be at least twice as large as the largest number of blurbs
  * required to represent any 'number' (in any encoding) you are going to
  * read.  With FLAC this is on the order of maybe a few hundred bits.
  */
 static const unsigned FLAC__BITBUFFER_DEFAULT_CAPACITY = ((65536 - 64) * 8) / FLAC__BITS_PER_BLURB; /* blurbs */
 
+#ifndef FLAC__OLD_MSVC_FLAVOR
+static const unsigned char byte_to_unary_table[] = {
+       8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
+       3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
+       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
+};
+#endif
+
 #if FLAC__BITS_PER_BLURB == 8
 #define FLAC__BITS_PER_BLURB_LOG2 3
 #define FLAC__BYTES_PER_BLURB 1
@@ -58,6 +102,9 @@ static const unsigned FLAC__BITBUFFER_DEFAULT_CAPACITY = ((65536 - 64) * 8) / FL
 #define FLAC__BLURB_TOP_BIT_ONE ((FLAC__byte)0x80)
 #define BLURB_BIT_TO_MASK(b) (((FLAC__blurb)'\x80') >> (b))
 #define CRC16_UPDATE_BLURB(bb, blurb, crc) FLAC__CRC16_UPDATE((blurb), (crc));
+#ifndef FLAC__OLD_MSVC_FLAVOR
+#define FLAC__ALIGNED_BLURB_UNARY(blurb) (byte_to_unary_table[blurb])
+#endif
 #elif FLAC__BITS_PER_BLURB == 32
 #define FLAC__BITS_PER_BLURB_LOG2 5
 #define FLAC__BYTES_PER_BLURB 4
@@ -65,6 +112,9 @@ static const unsigned FLAC__BITBUFFER_DEFAULT_CAPACITY = ((65536 - 64) * 8) / FL
 #define FLAC__BLURB_TOP_BIT_ONE ((FLAC__uint32)0x80000000)
 #define BLURB_BIT_TO_MASK(b) (((FLAC__blurb)0x80000000) >> (b))
 #define CRC16_UPDATE_BLURB(bb, blurb, crc) crc16_update_blurb((bb), (blurb));
+#ifndef FLAC__OLD_MSVC_FLAVOR
+#define FLAC__ALIGNED_BLURB_UNARY(blurb) ((blurb) <= 0xff ? byte_to_unary_table[blurb] + 24 : ((blurb) <= 0xffff ? byte_to_unary_table[(blurb) >> 8] + 16 : ((blurb) <= 0xffffff ? byte_to_unary_table[(blurb) >> 16] + 8 : byte_to_unary_table[(blurb) >> 24])))
+#endif
 #else
 /* ERROR, only sizes of 8 and 32 are supported */
 #endif
@@ -80,6 +130,13 @@ static const unsigned FLAC__BITBUFFER_DEFAULT_CAPACITY = ((65536 - 64) * 8) / FL
 #endif
 #define max(x,y) ((x)>(y)?(x):(y))
 
+/* adjust for compilers that can't understand using LLU suffix for uint64_t literals */
+#ifdef _MSC_VER
+#define FLAC__U64L(x) x
+#else
+#define FLAC__U64L(x) x##LLU
+#endif
+
 #ifndef FLaC__INLINE
 #define FLaC__INLINE
 #endif
@@ -134,16 +191,15 @@ static FLAC__bool bitbuffer_resize_(FLAC__BitBuffer *bb, unsigned new_capacity)
 {
        FLAC__blurb *new_buffer;
 
-       FLAC__ASSERT(bb != 0);
-       FLAC__ASSERT(bb->buffer != 0);
+       FLAC__ASSERT(0 != bb);
+       FLAC__ASSERT(0 != bb->buffer);
 
        if(bb->capacity == new_capacity)
                return true;
 
-       new_buffer = (FLAC__blurb*)malloc(sizeof(FLAC__blurb) * new_capacity);
+       new_buffer = (FLAC__blurb*)calloc(new_capacity, sizeof(FLAC__blurb));
        if(new_buffer == 0)
                return false;
-       memset(new_buffer, 0, sizeof(FLAC__blurb) * new_capacity);
        memcpy(new_buffer, bb->buffer, sizeof(FLAC__blurb)*min(bb->blurbs+(bb->bits?1:0), new_capacity));
        if(new_capacity < bb->blurbs+(bb->bits?1:0)) {
                bb->blurbs = new_capacity;
@@ -155,7 +211,7 @@ static FLAC__bool bitbuffer_resize_(FLAC__BitBuffer *bb, unsigned new_capacity)
                bb->consumed_bits = 0;
                bb->total_consumed_bits = FLAC__BLURBS_TO_BITS(new_capacity);
        }
-       free(bb->buffer); /* we've already asserted above that (bb->buffer != 0) */
+       free(bb->buffer); /* we've already asserted above that (0 != bb->buffer) */
        bb->buffer = new_buffer;
        bb->capacity = new_capacity;
        return true;
@@ -173,8 +229,8 @@ static FLAC__bool bitbuffer_grow_(FLAC__BitBuffer *bb, unsigned min_blurbs_to_ad
 
 static FLAC__bool bitbuffer_ensure_size_(FLAC__BitBuffer *bb, unsigned bits_to_add)
 {
-       FLAC__ASSERT(bb != 0);
-       FLAC__ASSERT(bb->buffer != 0);
+       FLAC__ASSERT(0 != bb);
+       FLAC__ASSERT(0 != bb->buffer);
 
        if(FLAC__BLURBS_TO_BITS(bb->capacity) < bb->total_bits + bits_to_add)
                return bitbuffer_grow_(bb, (bits_to_add >> FLAC__BITS_PER_BLURB_LOG2) + 2);
@@ -189,11 +245,27 @@ static FLAC__bool bitbuffer_read_from_client_(FLAC__BitBuffer *bb, FLAC__bool (*
 
        /* first shift the unconsumed buffer data toward the front as much as possible */
        if(bb->total_consumed_bits >= FLAC__BITS_PER_BLURB) {
-               unsigned l = 0, r = bb->consumed_blurbs, r_end = bb->blurbs + (bb->bits? 1:0);
+#if FLAC__BITS_PER_BLURB == 8
+               /*
+                * memset and memcpy are usually implemented in assembly language
+                * by the system libc, and they can be much faster
+                */
+               const unsigned r_end = bb->blurbs + (bb->bits? 1:0);
+               const unsigned r = bb->consumed_blurbs, l = r_end - r;
+               memmove(&bb->buffer[0], &bb->buffer[r], l);
+               memset(&bb->buffer[l], 0, r);
+#elif FLAC__BITS_PER_BLURB == 32
+               /* still needs optimization */
+               const unsigned r_end = bb->blurbs + (bb->bits? 1:0);
+               unsigned l = 0, r = bb->consumed_blurbs;
                for( ; r < r_end; l++, r++)
                        bb->buffer[l] = bb->buffer[r];
                for( ; l < r_end; l++)
                        bb->buffer[l] = 0;
+#else
+               FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
+#endif /* FLAC__BITS_PER_BLURB == 32 or 8 */
+
                bb->blurbs -= bb->consumed_blurbs;
                bb->total_bits -= FLAC__BLURBS_TO_BITS(bb->consumed_blurbs);
                bb->consumed_blurbs = 0;
@@ -251,20 +323,21 @@ static FLAC__bool bitbuffer_read_from_client_(FLAC__BitBuffer *bb, FLAC__bool (*
 
 FLAC__BitBuffer *FLAC__bitbuffer_new()
 {
-       FLAC__BitBuffer *bb = (FLAC__BitBuffer*)malloc(sizeof(FLAC__BitBuffer));
+       FLAC__BitBuffer *bb = (FLAC__BitBuffer*)calloc(1, sizeof(FLAC__BitBuffer));
 
-       if(0 != bb) {
+       /* calloc() implies:
+               memset(bb, 0, sizeof(FLAC__BitBuffer));
                bb->buffer = 0;
                bb->capacity = 0;
                bb->blurbs = bb->bits = bb->total_bits = 0;
                bb->consumed_blurbs = bb->consumed_bits = bb->total_consumed_bits = 0;
-       }
+       */
        return bb;
 }
 
 void FLAC__bitbuffer_delete(FLAC__BitBuffer *bb)
 {
-       FLAC__ASSERT(bb != 0);
+       FLAC__ASSERT(0 != bb);
 
        FLAC__bitbuffer_free(bb);
        free(bb);
@@ -278,7 +351,7 @@ void FLAC__bitbuffer_delete(FLAC__BitBuffer *bb)
 
 FLAC__bool FLAC__bitbuffer_init(FLAC__BitBuffer *bb)
 {
-       FLAC__ASSERT(bb != 0);
+       FLAC__ASSERT(0 != bb);
 
        bb->buffer = 0;
        bb->capacity = 0;
@@ -290,7 +363,7 @@ FLAC__bool FLAC__bitbuffer_init(FLAC__BitBuffer *bb)
 
 FLAC__bool FLAC__bitbuffer_init_from(FLAC__BitBuffer *bb, const FLAC__byte buffer[], unsigned bytes)
 {
-       FLAC__ASSERT(bb != 0);
+       FLAC__ASSERT(0 != bb);
        FLAC__ASSERT(bytes > 0);
 
        if(!FLAC__bitbuffer_init(bb))
@@ -299,7 +372,7 @@ FLAC__bool FLAC__bitbuffer_init_from(FLAC__BitBuffer *bb, const FLAC__byte buffe
        if(!bitbuffer_ensure_size_(bb, bytes << 3))
                return false;
 
-       FLAC__ASSERT(buffer != 0);
+       FLAC__ASSERT(0 != buffer);
        /* @@@ WATCHOUT: code currently only works for 8-bits-per-blurb inclusive-or big-endian: */
        memcpy((FLAC__byte*)bb->buffer, buffer, sizeof(FLAC__byte)*bytes);
        bb->blurbs = bytes / FLAC__BYTES_PER_BLURB;
@@ -312,8 +385,8 @@ FLAC__bool FLAC__bitbuffer_concatenate_aligned(FLAC__BitBuffer *dest, const FLAC
 {
        unsigned bits_to_add = src->total_bits - src->total_consumed_bits;
 
-       FLAC__ASSERT(dest != 0);
-       FLAC__ASSERT(src != 0);
+       FLAC__ASSERT(0 != dest);
+       FLAC__ASSERT(0 != src);
 
        if(bits_to_add == 0)
                return true;
@@ -342,9 +415,9 @@ FLAC__bool FLAC__bitbuffer_concatenate_aligned(FLAC__BitBuffer *dest, const FLAC
 
 void FLAC__bitbuffer_free(FLAC__BitBuffer *bb)
 {
-       FLAC__ASSERT(bb != 0);
+       FLAC__ASSERT(0 != bb);
 
-       if(bb->buffer != 0)
+       if(0 != bb->buffer)
                free(bb->buffer);
        bb->buffer = 0;
        bb->capacity = 0;
@@ -356,10 +429,9 @@ FLAC__bool FLAC__bitbuffer_clear(FLAC__BitBuffer *bb)
 {
        if(bb->buffer == 0) {
                bb->capacity = FLAC__BITBUFFER_DEFAULT_CAPACITY;
-               bb->buffer = (FLAC__blurb*)malloc(sizeof(FLAC__blurb) * bb->capacity);
+               bb->buffer = (FLAC__blurb*)calloc(bb->capacity, sizeof(FLAC__blurb));
                if(bb->buffer == 0)
                        return false;
-               memset(bb->buffer, 0, bb->capacity);
        }
        else {
                memset(bb->buffer, 0, bb->blurbs + (bb->bits?1:0));
@@ -371,10 +443,10 @@ FLAC__bool FLAC__bitbuffer_clear(FLAC__BitBuffer *bb)
 
 FLAC__bool FLAC__bitbuffer_clone(FLAC__BitBuffer *dest, const FLAC__BitBuffer *src)
 {
-       FLAC__ASSERT(dest != 0);
-       FLAC__ASSERT(dest->buffer != 0);
-       FLAC__ASSERT(src != 0);
-       FLAC__ASSERT(src->buffer != 0);
+       FLAC__ASSERT(0 != dest);
+       FLAC__ASSERT(0 != dest->buffer);
+       FLAC__ASSERT(0 != src);
+       FLAC__ASSERT(0 != src->buffer);
 
        if(dest->capacity < src->capacity)
                if(!bitbuffer_resize_(dest, src->capacity))
@@ -392,8 +464,8 @@ FLAC__bool FLAC__bitbuffer_clone(FLAC__BitBuffer *dest, const FLAC__BitBuffer *s
 
 void FLAC__bitbuffer_reset_read_crc16(FLAC__BitBuffer *bb, FLAC__uint16 seed)
 {
-       FLAC__ASSERT(bb != 0);
-       FLAC__ASSERT(bb->buffer != 0);
+       FLAC__ASSERT(0 != bb);
+       FLAC__ASSERT(0 != bb->buffer);
        FLAC__ASSERT((bb->consumed_bits & 7) == 0);
 
        bb->read_crc16 = seed;
@@ -408,8 +480,8 @@ void FLAC__bitbuffer_reset_read_crc16(FLAC__BitBuffer *bb, FLAC__uint16 seed)
 
 FLAC__uint16 FLAC__bitbuffer_get_read_crc16(FLAC__BitBuffer *bb)
 {
-       FLAC__ASSERT(bb != 0);
-       FLAC__ASSERT(bb->buffer != 0);
+       FLAC__ASSERT(0 != bb);
+       FLAC__ASSERT(0 != bb->buffer);
        FLAC__ASSERT((bb->bits & 7) == 0);
        FLAC__ASSERT((bb->consumed_bits & 7) == 0);
 
@@ -539,8 +611,8 @@ FLAC__bool FLAC__bitbuffer_write_zeroes(FLAC__BitBuffer *bb, unsigned bits)
 {
        unsigned n;
 
-       FLAC__ASSERT(bb != 0);
-       FLAC__ASSERT(bb->buffer != 0);
+       FLAC__ASSERT(0 != bb);
+       FLAC__ASSERT(0 != bb->buffer);
 
        if(bits == 0)
                return true;
@@ -564,8 +636,8 @@ FLaC__INLINE FLAC__bool FLAC__bitbuffer_write_raw_uint32(FLAC__BitBuffer *bb, FL
 {
        unsigned n, k;
 
-       FLAC__ASSERT(bb != 0);
-       FLAC__ASSERT(bb->buffer != 0);
+       FLAC__ASSERT(0 != bb);
+       FLAC__ASSERT(0 != bb->buffer);
 
        FLAC__ASSERT(bits <= 32);
        if(bits == 0)
@@ -636,27 +708,27 @@ FLAC__bool FLAC__bitbuffer_write_raw_uint64(FLAC__BitBuffer *bb, FLAC__uint64 va
 {
        static const FLAC__uint64 mask[] = {
                0,
-               0x0000000000000001, 0x0000000000000003, 0x0000000000000007, 0x000000000000000F,
-               0x000000000000001F, 0x000000000000003F, 0x000000000000007F, 0x00000000000000FF,
-               0x00000000000001FF, 0x00000000000003FF, 0x00000000000007FF, 0x0000000000000FFF,
-               0x0000000000001FFF, 0x0000000000003FFF, 0x0000000000007FFF, 0x000000000000FFFF,
-               0x000000000001FFFF, 0x000000000003FFFF, 0x000000000007FFFF, 0x00000000000FFFFF,
-               0x00000000001FFFFF, 0x00000000003FFFFF, 0x00000000007FFFFF, 0x0000000000FFFFFF,
-               0x0000000001FFFFFF, 0x0000000003FFFFFF, 0x0000000007FFFFFF, 0x000000000FFFFFFF,
-               0x000000001FFFFFFF, 0x000000003FFFFFFF, 0x000000007FFFFFFF, 0x00000000FFFFFFFF,
-               0x00000001FFFFFFFF, 0x00000003FFFFFFFF, 0x00000007FFFFFFFF, 0x0000000FFFFFFFFF,
-               0x0000001FFFFFFFFF, 0x0000003FFFFFFFFF, 0x0000007FFFFFFFFF, 0x000000FFFFFFFFFF,
-               0x000001FFFFFFFFFF, 0x000003FFFFFFFFFF, 0x000007FFFFFFFFFF, 0x00000FFFFFFFFFFF,
-               0x00001FFFFFFFFFFF, 0x00003FFFFFFFFFFF, 0x00007FFFFFFFFFFF, 0x0000FFFFFFFFFFFF,
-               0x0001FFFFFFFFFFFF, 0x0003FFFFFFFFFFFF, 0x0007FFFFFFFFFFFF, 0x000FFFFFFFFFFFFF,
-               0x001FFFFFFFFFFFFF, 0x003FFFFFFFFFFFFF, 0x007FFFFFFFFFFFFF, 0x00FFFFFFFFFFFFFF,
-               0x01FFFFFFFFFFFFFF, 0x03FFFFFFFFFFFFFF, 0x07FFFFFFFFFFFFFF, 0x0FFFFFFFFFFFFFFF,
-               0x1FFFFFFFFFFFFFFF, 0x3FFFFFFFFFFFFFFF, 0x7FFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF
+               FLAC__U64L(0x0000000000000001), FLAC__U64L(0x0000000000000003), FLAC__U64L(0x0000000000000007), FLAC__U64L(0x000000000000000F),
+               FLAC__U64L(0x000000000000001F), FLAC__U64L(0x000000000000003F), FLAC__U64L(0x000000000000007F), FLAC__U64L(0x00000000000000FF),
+               FLAC__U64L(0x00000000000001FF), FLAC__U64L(0x00000000000003FF), FLAC__U64L(0x00000000000007FF), FLAC__U64L(0x0000000000000FFF),
+               FLAC__U64L(0x0000000000001FFF), FLAC__U64L(0x0000000000003FFF), FLAC__U64L(0x0000000000007FFF), FLAC__U64L(0x000000000000FFFF),
+               FLAC__U64L(0x000000000001FFFF), FLAC__U64L(0x000000000003FFFF), FLAC__U64L(0x000000000007FFFF), FLAC__U64L(0x00000000000FFFFF),
+               FLAC__U64L(0x00000000001FFFFF), FLAC__U64L(0x00000000003FFFFF), FLAC__U64L(0x00000000007FFFFF), FLAC__U64L(0x0000000000FFFFFF),
+               FLAC__U64L(0x0000000001FFFFFF), FLAC__U64L(0x0000000003FFFFFF), FLAC__U64L(0x0000000007FFFFFF), FLAC__U64L(0x000000000FFFFFFF),
+               FLAC__U64L(0x000000001FFFFFFF), FLAC__U64L(0x000000003FFFFFFF), FLAC__U64L(0x000000007FFFFFFF), FLAC__U64L(0x00000000FFFFFFFF),
+               FLAC__U64L(0x00000001FFFFFFFF), FLAC__U64L(0x00000003FFFFFFFF), FLAC__U64L(0x00000007FFFFFFFF), FLAC__U64L(0x0000000FFFFFFFFF),
+               FLAC__U64L(0x0000001FFFFFFFFF), FLAC__U64L(0x0000003FFFFFFFFF), FLAC__U64L(0x0000007FFFFFFFFF), FLAC__U64L(0x000000FFFFFFFFFF),
+               FLAC__U64L(0x000001FFFFFFFFFF), FLAC__U64L(0x000003FFFFFFFFFF), FLAC__U64L(0x000007FFFFFFFFFF), FLAC__U64L(0x00000FFFFFFFFFFF),
+               FLAC__U64L(0x00001FFFFFFFFFFF), FLAC__U64L(0x00003FFFFFFFFFFF), FLAC__U64L(0x00007FFFFFFFFFFF), FLAC__U64L(0x0000FFFFFFFFFFFF),
+               FLAC__U64L(0x0001FFFFFFFFFFFF), FLAC__U64L(0x0003FFFFFFFFFFFF), FLAC__U64L(0x0007FFFFFFFFFFFF), FLAC__U64L(0x000FFFFFFFFFFFFF),
+               FLAC__U64L(0x001FFFFFFFFFFFFF), FLAC__U64L(0x003FFFFFFFFFFFFF), FLAC__U64L(0x007FFFFFFFFFFFFF), FLAC__U64L(0x00FFFFFFFFFFFFFF),
+               FLAC__U64L(0x01FFFFFFFFFFFFFF), FLAC__U64L(0x03FFFFFFFFFFFFFF), FLAC__U64L(0x07FFFFFFFFFFFFFF), FLAC__U64L(0x0FFFFFFFFFFFFFFF),
+               FLAC__U64L(0x1FFFFFFFFFFFFFFF), FLAC__U64L(0x3FFFFFFFFFFFFFFF), FLAC__U64L(0x7FFFFFFFFFFFFFFF), FLAC__U64L(0xFFFFFFFFFFFFFFFF)
        };
        unsigned n, k;
 
-       FLAC__ASSERT(bb != 0);
-       FLAC__ASSERT(bb->buffer != 0);
+       FLAC__ASSERT(0 != bb);
+       FLAC__ASSERT(0 != bb->buffer);
 
        FLAC__ASSERT(bits <= 64);
        if(bits == 0)
@@ -680,7 +752,7 @@ FLAC__bool FLAC__bitbuffer_write_raw_uint64(FLAC__BitBuffer *bb, FLAC__uint64 va
                                k = bits - FLAC__BITS_PER_BLURB;
                                bb->buffer[bb->blurbs++] = (FLAC__blurb)(val >> k);
                                /* we know k < 64 so no need to protect against the gcc bug mentioned above */
-                               val &= (~(0xffffffffffffffff << k));
+                               val &= (~(FLAC__U64L(0xffffffffffffffff) << k));
                                bits -= FLAC__BITS_PER_BLURB;
                        }
                }
@@ -690,7 +762,7 @@ FLAC__bool FLAC__bitbuffer_write_raw_uint64(FLAC__BitBuffer *bb, FLAC__uint64 va
                        bb->buffer[bb->blurbs] <<= n;
                        bb->buffer[bb->blurbs] |= (val >> k);
                        /* we know n > 0 so k < 64 so no need to protect against the gcc bug mentioned above */
-                       val &= (~(0xffffffffffffffff << k));
+                       val &= (~(FLAC__U64L(0xffffffffffffffff) << k));
                        bits -= n;
                        bb->bits += n;
                        if(bb->bits == FLAC__BITS_PER_BLURB) {
@@ -703,10 +775,12 @@ FLAC__bool FLAC__bitbuffer_write_raw_uint64(FLAC__BitBuffer *bb, FLAC__uint64 va
        return true;
 }
 
+#if 0 /* UNUSED */
 FLAC__bool FLAC__bitbuffer_write_raw_int64(FLAC__BitBuffer *bb, FLAC__int64 val, unsigned bits)
 {
        return FLAC__bitbuffer_write_raw_uint64(bb, (FLAC__uint64)val, bits);
 }
+#endif
 
 FLaC__INLINE FLAC__bool FLAC__bitbuffer_write_raw_uint32_little_endian(FLAC__BitBuffer *bb, FLAC__uint32 val)
 {
@@ -838,134 +912,13 @@ unsigned FLAC__bitbuffer_golomb_bits_unsigned(unsigned uval, unsigned parameter)
 }
 #endif /* UNUSED */
 
-#ifdef FLAC__SYMMETRIC_RICE
-FLAC__bool FLAC__bitbuffer_write_symmetric_rice_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
-{
-       unsigned total_bits, interesting_bits, msbs;
-       FLAC__uint32 pattern;
-
-       FLAC__ASSERT(bb != 0);
-       FLAC__ASSERT(bb->buffer != 0);
-       FLAC__ASSERT(parameter <= 31);
-
-       /* init pattern with the unary end bit and the sign bit */
-       if(val < 0) {
-               pattern = 3;
-               val = -val;
-       }
-       else
-               pattern = 2;
-
-       msbs = val >> parameter;
-       interesting_bits = 2 + parameter;
-       total_bits = interesting_bits + msbs;
-       pattern <<= parameter;
-       pattern |= (val & ((1<<parameter)-1)); /* the binary LSBs */
-
-       if(total_bits <= 32) {
-               if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
-                       return false;
-       }
-       else {
-               /* write the unary MSBs */
-               if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
-                       return false;
-               /* write the unary end bit, the sign bit, and binary LSBs */
-               if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
-                       return false;
-       }
-       return true;
-}
-
-#if 0 /* UNUSED */
-FLAC__bool FLAC__bitbuffer_write_symmetric_rice_signed_guarded(FLAC__BitBuffer *bb, int val, unsigned parameter, unsigned max_bits, FLAC__bool *overflow)
-{
-       unsigned total_bits, interesting_bits, msbs;
-       FLAC__uint32 pattern;
-
-       FLAC__ASSERT(bb != 0);
-       FLAC__ASSERT(bb->buffer != 0);
-       FLAC__ASSERT(parameter <= 31);
-
-       *overflow = false;
-
-       /* init pattern with the unary end bit and the sign bit */
-       if(val < 0) {
-               pattern = 3;
-               val = -val;
-       }
-       else
-               pattern = 2;
-
-       msbs = val >> parameter;
-       interesting_bits = 2 + parameter;
-       total_bits = interesting_bits + msbs;
-       pattern <<= parameter;
-       pattern |= (val & ((1<<parameter)-1)); /* the binary LSBs */
-
-       if(total_bits <= 32) {
-               if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
-                       return false;
-       }
-       else if(total_bits > max_bits) {
-               *overflow = true;
-               return true;
-       }
-       else {
-               /* write the unary MSBs */
-               if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
-                       return false;
-               /* write the unary end bit, the sign bit, and binary LSBs */
-               if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
-                       return false;
-       }
-       return true;
-}
-#endif /* UNUSED */
-
-FLAC__bool FLAC__bitbuffer_write_symmetric_rice_signed_escape(FLAC__BitBuffer *bb, int val, unsigned parameter)
-{
-       unsigned total_bits, val_bits;
-       FLAC__uint32 pattern;
-
-       FLAC__ASSERT(bb != 0);
-       FLAC__ASSERT(bb->buffer != 0);
-       FLAC__ASSERT(parameter <= 31);
-
-       val_bits = FLAC__bitmath_silog2(val);
-       total_bits = 2 + parameter + 5 + val_bits;
-
-       if(total_bits <= 32) {
-               pattern = 3;
-               pattern <<= (parameter + 5);
-               pattern |= val_bits;
-               pattern <<= val_bits;
-               pattern |= (val & ((1 << val_bits) - 1));
-               if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
-                       return false;
-       }
-       else {
-               /* write the '-0' escape code first */
-               if(!FLAC__bitbuffer_write_raw_uint32(bb, 3u << parameter, 2+parameter))
-                       return false;
-               /* write the length */
-               if(!FLAC__bitbuffer_write_raw_uint32(bb, val_bits, 5))
-                       return false;
-               /* write the value */
-               if(!FLAC__bitbuffer_write_raw_int32(bb, val, val_bits))
-                       return false;
-       }
-       return true;
-}
-#endif /* ifdef FLAC__SYMMETRIC_RICE */
-
 FLAC__bool FLAC__bitbuffer_write_rice_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
 {
        unsigned total_bits, interesting_bits, msbs, uval;
        FLAC__uint32 pattern;
 
-       FLAC__ASSERT(bb != 0);
-       FLAC__ASSERT(bb->buffer != 0);
+       FLAC__ASSERT(0 != bb);
+       FLAC__ASSERT(0 != bb->buffer);
        FLAC__ASSERT(parameter <= 30);
 
        /* fold signed to unsigned */
@@ -1005,8 +958,8 @@ FLAC__bool FLAC__bitbuffer_write_rice_signed_guarded(FLAC__BitBuffer *bb, int va
        unsigned total_bits, interesting_bits, msbs, uval;
        FLAC__uint32 pattern;
 
-       FLAC__ASSERT(bb != 0);
-       FLAC__ASSERT(bb->buffer != 0);
+       FLAC__ASSERT(0 != bb);
+       FLAC__ASSERT(0 != bb->buffer);
        FLAC__ASSERT(parameter <= 30);
 
        *overflow = false;
@@ -1053,8 +1006,8 @@ FLAC__bool FLAC__bitbuffer_write_golomb_signed(FLAC__BitBuffer *bb, int val, uns
        unsigned total_bits, msbs, uval;
        unsigned k;
 
-       FLAC__ASSERT(bb != 0);
-       FLAC__ASSERT(bb->buffer != 0);
+       FLAC__ASSERT(0 != bb);
+       FLAC__ASSERT(0 != bb->buffer);
        FLAC__ASSERT(parameter > 0);
 
        /* fold signed to unsigned */
@@ -1121,8 +1074,8 @@ FLAC__bool FLAC__bitbuffer_write_golomb_unsigned(FLAC__BitBuffer *bb, unsigned u
        unsigned total_bits, msbs;
        unsigned k;
 
-       FLAC__ASSERT(bb != 0);
-       FLAC__ASSERT(bb->buffer != 0);
+       FLAC__ASSERT(0 != bb);
+       FLAC__ASSERT(0 != bb->buffer);
        FLAC__ASSERT(parameter > 0);
 
        k = FLAC__bitmath_ilog2(parameter);
@@ -1179,8 +1132,8 @@ FLAC__bool FLAC__bitbuffer_write_utf8_uint32(FLAC__BitBuffer *bb, FLAC__uint32 v
 {
        FLAC__bool ok = 1;
 
-       FLAC__ASSERT(bb != 0);
-       FLAC__ASSERT(bb->buffer != 0);
+       FLAC__ASSERT(0 != bb);
+       FLAC__ASSERT(0 != bb->buffer);
 
        FLAC__ASSERT(!(val & 0x80000000)); /* this version only handles 31 bits */
 
@@ -1225,10 +1178,10 @@ FLAC__bool FLAC__bitbuffer_write_utf8_uint64(FLAC__BitBuffer *bb, FLAC__uint64 v
 {
        FLAC__bool ok = 1;
 
-       FLAC__ASSERT(bb != 0);
-       FLAC__ASSERT(bb->buffer != 0);
+       FLAC__ASSERT(0 != bb);
+       FLAC__ASSERT(0 != bb->buffer);
 
-       FLAC__ASSERT(!(val & 0xFFFFFFF000000000)); /* this version only handles 36 bits */
+       FLAC__ASSERT(!(val & FLAC__U64L(0xFFFFFFF000000000))); /* this version only handles 36 bits */
 
        if(val < 0x80) {
                return FLAC__bitbuffer_write_raw_uint32(bb, (FLAC__uint32)val, 8);
@@ -1288,8 +1241,8 @@ FLAC__bool FLAC__bitbuffer_zero_pad_to_byte_boundary(FLAC__BitBuffer *bb)
 FLAC__bool FLAC__bitbuffer_peek_bit(FLAC__BitBuffer *bb, unsigned *val, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data)
 {
        /* to avoid a drastic speed penalty we don't:
-       FLAC__ASSERT(bb != 0);
-       FLAC__ASSERT(bb->buffer != 0);
+       FLAC__ASSERT(0 != bb);
+       FLAC__ASSERT(0 != bb->buffer);
        FLAC__ASSERT(bb->bits == 0);
        */
 
@@ -1308,8 +1261,8 @@ FLAC__bool FLAC__bitbuffer_peek_bit(FLAC__BitBuffer *bb, unsigned *val, FLAC__bo
 FLAC__bool FLAC__bitbuffer_read_bit(FLAC__BitBuffer *bb, unsigned *val, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data)
 {
        /* to avoid a drastic speed penalty we don't:
-       FLAC__ASSERT(bb != 0);
-       FLAC__ASSERT(bb->buffer != 0);
+       FLAC__ASSERT(0 != bb);
+       FLAC__ASSERT(0 != bb->buffer);
        FLAC__ASSERT(bb->bits == 0);
        */
 
@@ -1335,8 +1288,8 @@ FLAC__bool FLAC__bitbuffer_read_bit(FLAC__BitBuffer *bb, unsigned *val, FLAC__bo
 FLAC__bool FLAC__bitbuffer_read_bit_to_uint32(FLAC__BitBuffer *bb, FLAC__uint32 *val, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data)
 {
        /* to avoid a drastic speed penalty we don't:
-       FLAC__ASSERT(bb != 0);
-       FLAC__ASSERT(bb->buffer != 0);
+       FLAC__ASSERT(0 != bb);
+       FLAC__ASSERT(0 != bb->buffer);
        FLAC__ASSERT(bb->bits == 0);
        */
 
@@ -1363,8 +1316,8 @@ FLAC__bool FLAC__bitbuffer_read_bit_to_uint32(FLAC__BitBuffer *bb, FLAC__uint32
 FLAC__bool FLAC__bitbuffer_read_bit_to_uint64(FLAC__BitBuffer *bb, FLAC__uint64 *val, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data)
 {
        /* to avoid a drastic speed penalty we don't:
-       FLAC__ASSERT(bb != 0);
-       FLAC__ASSERT(bb->buffer != 0);
+       FLAC__ASSERT(0 != bb);
+       FLAC__ASSERT(0 != bb->buffer);
        FLAC__ASSERT(bb->bits == 0);
        */
 
@@ -1393,8 +1346,8 @@ FLaC__INLINE FLAC__bool FLAC__bitbuffer_read_raw_uint32(FLAC__BitBuffer *bb, FLA
 {
        unsigned i;
 
-       FLAC__ASSERT(bb != 0);
-       FLAC__ASSERT(bb->buffer != 0);
+       FLAC__ASSERT(0 != bb);
+       FLAC__ASSERT(0 != bb->buffer);
 
        FLAC__ASSERT(bits <= 32);
 
@@ -1410,8 +1363,8 @@ FLaC__INLINE FLAC__bool FLAC__bitbuffer_read_raw_uint32(FLAC__BitBuffer *bb, FLA
        unsigned i, bits_ = bits;
        FLAC__uint32 v = 0;
 
-       FLAC__ASSERT(bb != 0);
-       FLAC__ASSERT(bb->buffer != 0);
+       FLAC__ASSERT(0 != bb);
+       FLAC__ASSERT(0 != bb->buffer);
 
        FLAC__ASSERT(bits <= 32);
        FLAC__ASSERT((bb->capacity*FLAC__BITS_PER_BLURB) * 2 >= bits);
@@ -1495,8 +1448,8 @@ FLAC__bool FLAC__bitbuffer_read_raw_int32(FLAC__BitBuffer *bb, FLAC__int32 *val,
        unsigned i;
        FLAC__uint32 v;
 
-       FLAC__ASSERT(bb != 0);
-       FLAC__ASSERT(bb->buffer != 0);
+       FLAC__ASSERT(0 != bb);
+       FLAC__ASSERT(0 != bb->buffer);
 
        FLAC__ASSERT(bits <= 32);
 
@@ -1528,8 +1481,8 @@ FLAC__bool FLAC__bitbuffer_read_raw_int32(FLAC__BitBuffer *bb, FLAC__int32 *val,
        unsigned i, bits_ = bits;
        FLAC__uint32 v = 0;
 
-       FLAC__ASSERT(bb != 0);
-       FLAC__ASSERT(bb->buffer != 0);
+       FLAC__ASSERT(0 != bb);
+       FLAC__ASSERT(0 != bb->buffer);
 
        FLAC__ASSERT(bits <= 32);
        FLAC__ASSERT((bb->capacity*FLAC__BITS_PER_BLURB) * 2 >= bits);
@@ -1624,8 +1577,8 @@ FLAC__bool FLAC__bitbuffer_read_raw_uint64(FLAC__BitBuffer *bb, FLAC__uint64 *va
 {
        unsigned i;
 
-       FLAC__ASSERT(bb != 0);
-       FLAC__ASSERT(bb->buffer != 0);
+       FLAC__ASSERT(0 != bb);
+       FLAC__ASSERT(0 != bb->buffer);
 
        FLAC__ASSERT(bits <= 64);
 
@@ -1641,8 +1594,8 @@ FLAC__bool FLAC__bitbuffer_read_raw_uint64(FLAC__BitBuffer *bb, FLAC__uint64 *va
        unsigned i, bits_ = bits;
        FLAC__uint64 v = 0;
 
-       FLAC__ASSERT(bb != 0);
-       FLAC__ASSERT(bb->buffer != 0);
+       FLAC__ASSERT(0 != bb);
+       FLAC__ASSERT(0 != bb->buffer);
 
        FLAC__ASSERT(bits <= 64);
        FLAC__ASSERT((bb->capacity*FLAC__BITS_PER_BLURB) * 2 >= bits);
@@ -1707,14 +1660,15 @@ FLAC__bool FLAC__bitbuffer_read_raw_uint64(FLAC__BitBuffer *bb, FLAC__uint64 *va
 }
 #endif
 
+#if 0 /* UNUSED */
 FLAC__bool FLAC__bitbuffer_read_raw_int64(FLAC__BitBuffer *bb, FLAC__int64 *val, const unsigned bits, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data)
 #ifdef FLAC__NO_MANUAL_INLINING
 {
        unsigned i;
        FLAC__uint64 v;
 
-       FLAC__ASSERT(bb != 0);
-       FLAC__ASSERT(bb->buffer != 0);
+       FLAC__ASSERT(0 != bb);
+       FLAC__ASSERT(0 != bb->buffer);
 
        FLAC__ASSERT(bits <= 64);
 
@@ -1740,8 +1694,8 @@ FLAC__bool FLAC__bitbuffer_read_raw_int64(FLAC__BitBuffer *bb, FLAC__int64 *val,
        unsigned i, bits_ = bits;
        FLAC__uint64 v = 0;
 
-       FLAC__ASSERT(bb != 0);
-       FLAC__ASSERT(bb->buffer != 0);
+       FLAC__ASSERT(0 != bb);
+       FLAC__ASSERT(0 != bb->buffer);
 
        FLAC__ASSERT(bits <= 64);
        FLAC__ASSERT((bb->capacity*FLAC__BITS_PER_BLURB) * 2 >= bits);
@@ -1818,6 +1772,7 @@ FLAC__bool FLAC__bitbuffer_read_raw_int64(FLAC__BitBuffer *bb, FLAC__int64 *val,
        return true;
 }
 #endif
+#endif
 
 FLaC__INLINE FLAC__bool FLAC__bitbuffer_read_raw_uint32_little_endian(FLAC__BitBuffer *bb, FLAC__uint32 *val, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data)
 {
@@ -1844,10 +1799,46 @@ FLaC__INLINE FLAC__bool FLAC__bitbuffer_read_raw_uint32_little_endian(FLAC__BitB
        return true;
 }
 
-FLAC__bool FLAC__bitbuffer_read_byte_block_aligned(FLAC__BitBuffer *bb, FLAC__byte *val, unsigned nvals, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data)
+FLAC__bool FLAC__bitbuffer_skip_bits_no_crc(FLAC__BitBuffer *bb, unsigned bits, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data)
 {
-       FLAC__ASSERT(bb != 0);
-       FLAC__ASSERT(bb->buffer != 0);
+       /*
+        * @@@ a slightly faster implementation is possible but
+        * probably not that useful since this is only called a
+        * couple of times in the metadata readers.
+        */
+       FLAC__ASSERT(0 != bb);
+       FLAC__ASSERT(0 != bb->buffer);
+
+       if(bits > 0) {
+               const unsigned n = bb->consumed_bits & 7;
+               unsigned m;
+               FLAC__uint32 x;
+
+               if(n != 0) {
+                       m = min(8-n, bits);
+                       if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, m, read_callback, client_data))
+                               return false;
+                       bits -= m;
+               }
+               m = bits / 8;
+               if(m > 0) {
+                       if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(bb, 0, m, read_callback, client_data))
+                               return false;
+                       bits %= 8;
+               }
+               if(bits > 0) {
+                       if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, bits, read_callback, client_data))
+                               return false;
+               }
+       }
+
+       return true;
+}
+
+FLAC__bool FLAC__bitbuffer_read_byte_block_aligned_no_crc(FLAC__BitBuffer *bb, FLAC__byte *val, unsigned nvals, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data)
+{
+       FLAC__ASSERT(0 != bb);
+       FLAC__ASSERT(0 != bb->buffer);
        FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(bb));
        FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(bb));
 #if FLAC__BITS_PER_BLURB == 8
@@ -1880,8 +1871,8 @@ FLaC__INLINE FLAC__bool FLAC__bitbuffer_read_unary_unsigned(FLAC__BitBuffer *bb,
 {
        unsigned bit, val_ = 0;
 
-       FLAC__ASSERT(bb != 0);
-       FLAC__ASSERT(bb->buffer != 0);
+       FLAC__ASSERT(0 != bb);
+       FLAC__ASSERT(0 != bb->buffer);
 
        while(1) {
                if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
@@ -1900,8 +1891,8 @@ FLaC__INLINE FLAC__bool FLAC__bitbuffer_read_unary_unsigned(FLAC__BitBuffer *bb,
        unsigned total_blurbs_ = (bb->total_bits + (FLAC__BITS_PER_BLURB-1)) / FLAC__BITS_PER_BLURB;
        FLAC__blurb b;
 
-       FLAC__ASSERT(bb != 0);
-       FLAC__ASSERT(bb->buffer != 0);
+       FLAC__ASSERT(0 != bb);
+       FLAC__ASSERT(0 != bb->buffer);
 
 #if FLAC__BITS_PER_BLURB > 8
        if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { /*@@@ comment on why this is here*/
@@ -1978,43 +1969,13 @@ FLaC__INLINE FLAC__bool FLAC__bitbuffer_read_unary_unsigned(FLAC__BitBuffer *bb,
 }
 #endif
 
-#ifdef FLAC__SYMMETRIC_RICE
-FLAC__bool FLAC__bitbuffer_read_symmetric_rice_signed(FLAC__BitBuffer *bb, int *val, unsigned parameter, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data)
-{
-       FLAC__uint32 sign = 0, lsbs = 0, msbs = 0;
-
-       FLAC__ASSERT(bb != 0);
-       FLAC__ASSERT(bb->buffer != 0);
-       FLAC__ASSERT(parameter <= 31);
-
-       /* read the unary MSBs and end bit */
-       if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
-               return false;
-
-       /* read the sign bit */
-       if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &sign, read_callback, client_data))
-               return false;
-
-       /* read the binary LSBs */
-       if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, parameter, read_callback, client_data))
-               return false;
-
-       /* compose the value */
-       *val = (msbs << parameter) | lsbs;
-       if(sign)
-               *val = -(*val);
-
-       return true;
-}
-#endif /* ifdef FLAC__SYMMETRIC_RICE */
-
 FLAC__bool FLAC__bitbuffer_read_rice_signed(FLAC__BitBuffer *bb, int *val, unsigned parameter, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data)
 {
        FLAC__uint32 lsbs = 0, msbs = 0;
        unsigned uval;
 
-       FLAC__ASSERT(bb != 0);
-       FLAC__ASSERT(bb->buffer != 0);
+       FLAC__ASSERT(0 != bb);
+       FLAC__ASSERT(0 != bb->buffer);
        FLAC__ASSERT(parameter <= 31);
 
        /* read the unary MSBs and end bit */
@@ -2036,6 +1997,7 @@ FLAC__bool FLAC__bitbuffer_read_rice_signed(FLAC__BitBuffer *bb, int *val, unsig
 }
 
 FLAC__bool FLAC__bitbuffer_read_rice_signed_block(FLAC__BitBuffer *bb, int vals[], unsigned nvals, unsigned parameter, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data)
+#ifdef FLAC__OLD_MSVC_FLAVOR
 {
        const FLAC__blurb *buffer = bb->buffer;
 
@@ -2044,8 +2006,8 @@ FLAC__bool FLAC__bitbuffer_read_rice_signed_block(FLAC__BitBuffer *bb, int vals[
        FLAC__blurb blurb, save_blurb;
        unsigned state = 0; /* 0 = getting unary MSBs, 1 = getting binary LSBs */
 
-       FLAC__ASSERT(bb != 0);
-       FLAC__ASSERT(bb->buffer != 0);
+       FLAC__ASSERT(0 != bb);
+       FLAC__ASSERT(0 != bb->buffer);
        FLAC__ASSERT(parameter <= 31);
 
        if(nvals == 0)
@@ -2080,7 +2042,7 @@ FLAC__bool FLAC__bitbuffer_read_rice_signed_block(FLAC__BitBuffer *bb, int vals[
                                                CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
                                                break;
                                        }
-                               }       
+                               }
                                else {
                                        msbs += FLAC__BITS_PER_BLURB - cbits;
                                        cbits = 0;
@@ -2170,7 +2132,7 @@ FLAC__bool FLAC__bitbuffer_read_rice_signed_block(FLAC__BitBuffer *bb, int vals[
                                                        CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
                                                        break;
                                                }
-                                       }       
+                                       }
                                        else {
                                                msbs += FLAC__BITS_PER_BLURB - cbits;
                                                cbits = 0;
@@ -2242,6 +2204,115 @@ FLAC__bool FLAC__bitbuffer_read_rice_signed_block(FLAC__BitBuffer *bb, int vals[
 
        return true;
 }
+#else
+{
+       const FLAC__blurb *buffer = bb->buffer;
+
+       unsigned i, j, val_i = nvals;
+       unsigned cbits = 0, uval = 0, msbs = 0, lsbs_left = 0;
+       FLAC__blurb blurb, save_blurb;
+       unsigned state = 0; /* 0 = getting unary MSBs, 1 = getting binary LSBs */
+
+       FLAC__ASSERT(0 != bb);
+       FLAC__ASSERT(0 != bb->buffer);
+       FLAC__ASSERT(parameter <= 31);
+
+       if(nvals == 0)
+               return true;
+
+       cbits = bb->consumed_bits;
+       i = bb->consumed_blurbs;
+       while(val_i != 0) {
+               for( ; i < bb->blurbs; i++) {
+                       blurb = (save_blurb = buffer[i]) << cbits;
+                       while(1) {
+                               if(state == 0) {
+                                       if(blurb) {
+                                               j = FLAC__ALIGNED_BLURB_UNARY(blurb);
+                                               msbs += j;
+                                               j++;
+                                               cbits += j;
+
+                                               uval = 0;
+                                               lsbs_left = parameter;
+                                               state++;
+                                               if(cbits == FLAC__BITS_PER_BLURB) {
+                                                       cbits = 0;
+                                                       CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
+                                                       break;
+                                               }
+                                               blurb <<= j;
+                                       }
+                                       else {
+                                               msbs += FLAC__BITS_PER_BLURB - cbits;
+                                               cbits = 0;
+                                               CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
+                                               break;
+                                       }
+                               }
+                               else {
+                                       const unsigned available_bits = FLAC__BITS_PER_BLURB - cbits;
+                                       if(lsbs_left >= available_bits) {
+                                               uval <<= available_bits;
+                                               uval |= (blurb >> cbits);
+                                               cbits = 0;
+                                               CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
+
+                                               if(lsbs_left == available_bits) {
+                                                       /* compose the value */
+                                                       uval |= (msbs << parameter);
+                                                       *vals = (int)(uval >> 1 ^ -(int)(uval & 1));
+                                                       --val_i;
+                                                       if(val_i == 0) {
+                                                               i++;
+                                                               goto break2;
+                                                       }
+                                                       ++vals;
+
+                                                       msbs = 0;
+                                                       state = 0;
+                                               }
+
+                                               lsbs_left -= available_bits;
+                                               break;
+                                       }
+                                       else {
+                                               cbits += lsbs_left;
+                                               uval <<= lsbs_left;
+                                               uval |= (blurb >> (FLAC__BITS_PER_BLURB - lsbs_left));
+                                               blurb <<= lsbs_left;
+
+                                               /* compose the value */
+                                               uval |= (msbs << parameter);
+                                               *vals = (int)(uval >> 1 ^ -(int)(uval & 1));
+                                               --val_i;
+                                               if(val_i == 0)
+                                                       goto break2;
+                                               ++vals;
+
+                                               msbs = 0;
+                                               state = 0;
+                                       }
+                               }
+                       }
+               }
+break2:
+               bb->consumed_blurbs = i;
+               bb->consumed_bits = cbits;
+               bb->total_consumed_bits = (i << FLAC__BITS_PER_BLURB_LOG2) | cbits;
+               if(val_i != 0) {
+                       if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
+                               return false;
+                       /* these must be zero because we can only get here if we got to the end of the buffer */
+                       FLAC__ASSERT(bb->consumed_blurbs == 0);
+                       FLAC__ASSERT(bb->consumed_bits == 0);
+                       i = 0;
+               }
+       }
+
+       return true;
+}
+#endif
 
 #if 0 /* UNUSED */
 FLAC__bool FLAC__bitbuffer_read_golomb_signed(FLAC__BitBuffer *bb, int *val, unsigned parameter, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data)
@@ -2249,8 +2320,8 @@ FLAC__bool FLAC__bitbuffer_read_golomb_signed(FLAC__BitBuffer *bb, int *val, uns
        FLAC__uint32 lsbs = 0, msbs = 0;
        unsigned bit, uval, k;
 
-       FLAC__ASSERT(bb != 0);
-       FLAC__ASSERT(bb->buffer != 0);
+       FLAC__ASSERT(0 != bb);
+       FLAC__ASSERT(0 != bb->buffer);
 
        k = FLAC__bitmath_ilog2(parameter);
 
@@ -2293,8 +2364,8 @@ FLAC__bool FLAC__bitbuffer_read_golomb_unsigned(FLAC__BitBuffer *bb, unsigned *v
        FLAC__uint32 lsbs, msbs = 0;
        unsigned bit, k;
 
-       FLAC__ASSERT(bb != 0);
-       FLAC__ASSERT(bb->buffer != 0);
+       FLAC__ASSERT(0 != bb);
+       FLAC__ASSERT(0 != bb->buffer);
 
        k = FLAC__bitmath_ilog2(parameter);
 
@@ -2422,7 +2493,7 @@ FLAC__bool FLAC__bitbuffer_read_utf8_uint64(FLAC__BitBuffer *bb, FLAC__uint64 *v
                i = 6;
        }
        else {
-               *val = 0xffffffffffffffff;
+               *val = FLAC__U64L(0xffffffffffffffff);
                return true;
        }
        for( ; i; i--) {
@@ -2431,7 +2502,7 @@ FLAC__bool FLAC__bitbuffer_read_utf8_uint64(FLAC__BitBuffer *bb, FLAC__uint64 *v
                if(raw)
                        raw[(*rawlen)++] = (FLAC__byte)x;
                if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
-                       *val = 0xffffffffffffffff;
+                       *val = FLAC__U64L(0xffffffffffffffff);
                        return true;
                }
                v <<= 6;
@@ -2449,7 +2520,7 @@ void FLAC__bitbuffer_dump(const FLAC__BitBuffer *bb, FILE *out)
        }
        else {
                fprintf(out, "bitbuffer: capacity=%u blurbs=%u bits=%u total_bits=%u consumed: blurbs=%u, bits=%u, total_bits=%u\n", bb->capacity, bb->blurbs, bb->bits, bb->total_bits, bb->consumed_blurbs, bb->consumed_bits, bb->total_consumed_bits);
-return;/*@@@*/
+
                for(i = 0; i < bb->blurbs; i++) {
                        fprintf(out, "%08X: ", i);
                        for(j = 0; j < FLAC__BITS_PER_BLURB; j++)