1c74663799493f2b1e6123c18def94295d0afab7Kenny Root/* libFLAC - Free Lossless Audio Codec library
2c74663799493f2b1e6123c18def94295d0afab7Kenny Root * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007  Josh Coalson
3c74663799493f2b1e6123c18def94295d0afab7Kenny Root *
4c74663799493f2b1e6123c18def94295d0afab7Kenny Root * Redistribution and use in source and binary forms, with or without
5c74663799493f2b1e6123c18def94295d0afab7Kenny Root * modification, are permitted provided that the following conditions
6c74663799493f2b1e6123c18def94295d0afab7Kenny Root * are met:
7c74663799493f2b1e6123c18def94295d0afab7Kenny Root *
8c74663799493f2b1e6123c18def94295d0afab7Kenny Root * - Redistributions of source code must retain the above copyright
9c74663799493f2b1e6123c18def94295d0afab7Kenny Root * notice, this list of conditions and the following disclaimer.
10c74663799493f2b1e6123c18def94295d0afab7Kenny Root *
11c74663799493f2b1e6123c18def94295d0afab7Kenny Root * - Redistributions in binary form must reproduce the above copyright
12c74663799493f2b1e6123c18def94295d0afab7Kenny Root * notice, this list of conditions and the following disclaimer in the
13c74663799493f2b1e6123c18def94295d0afab7Kenny Root * documentation and/or other materials provided with the distribution.
14c74663799493f2b1e6123c18def94295d0afab7Kenny Root *
15c74663799493f2b1e6123c18def94295d0afab7Kenny Root * - Neither the name of the Xiph.org Foundation nor the names of its
16c74663799493f2b1e6123c18def94295d0afab7Kenny Root * contributors may be used to endorse or promote products derived from
17c74663799493f2b1e6123c18def94295d0afab7Kenny Root * this software without specific prior written permission.
18c74663799493f2b1e6123c18def94295d0afab7Kenny Root *
19c74663799493f2b1e6123c18def94295d0afab7Kenny Root * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20c74663799493f2b1e6123c18def94295d0afab7Kenny Root * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21c74663799493f2b1e6123c18def94295d0afab7Kenny Root * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22c74663799493f2b1e6123c18def94295d0afab7Kenny Root * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
23c74663799493f2b1e6123c18def94295d0afab7Kenny Root * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24c74663799493f2b1e6123c18def94295d0afab7Kenny Root * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25c74663799493f2b1e6123c18def94295d0afab7Kenny Root * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26c74663799493f2b1e6123c18def94295d0afab7Kenny Root * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27c74663799493f2b1e6123c18def94295d0afab7Kenny Root * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28c74663799493f2b1e6123c18def94295d0afab7Kenny Root * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29c74663799493f2b1e6123c18def94295d0afab7Kenny Root * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30c74663799493f2b1e6123c18def94295d0afab7Kenny Root */
31c74663799493f2b1e6123c18def94295d0afab7Kenny Root
32c74663799493f2b1e6123c18def94295d0afab7Kenny Root#if HAVE_CONFIG_H
33c74663799493f2b1e6123c18def94295d0afab7Kenny Root#  include <config.h>
34c74663799493f2b1e6123c18def94295d0afab7Kenny Root#endif
35c74663799493f2b1e6123c18def94295d0afab7Kenny Root
36c74663799493f2b1e6123c18def94295d0afab7Kenny Root#include <stdlib.h> /* for malloc() */
37c74663799493f2b1e6123c18def94295d0afab7Kenny Root#include <string.h> /* for memcpy(), memset() */
38c74663799493f2b1e6123c18def94295d0afab7Kenny Root#ifdef _MSC_VER
39c74663799493f2b1e6123c18def94295d0afab7Kenny Root#include <winsock.h> /* for ntohl() */
40c74663799493f2b1e6123c18def94295d0afab7Kenny Root#elif defined FLAC__SYS_DARWIN
41c74663799493f2b1e6123c18def94295d0afab7Kenny Root#include <machine/endian.h> /* for ntohl() */
42c74663799493f2b1e6123c18def94295d0afab7Kenny Root#elif defined __MINGW32__
43c74663799493f2b1e6123c18def94295d0afab7Kenny Root#include <winsock.h> /* for ntohl() */
44c74663799493f2b1e6123c18def94295d0afab7Kenny Root#else
45c74663799493f2b1e6123c18def94295d0afab7Kenny Root#include <netinet/in.h> /* for ntohl() */
46c74663799493f2b1e6123c18def94295d0afab7Kenny Root#endif
47c74663799493f2b1e6123c18def94295d0afab7Kenny Root#include "private/bitmath.h"
48c74663799493f2b1e6123c18def94295d0afab7Kenny Root#include "private/bitreader.h"
49c74663799493f2b1e6123c18def94295d0afab7Kenny Root#include "private/crc.h"
50c74663799493f2b1e6123c18def94295d0afab7Kenny Root#include "FLAC/assert.h"
51c74663799493f2b1e6123c18def94295d0afab7Kenny Root
52c74663799493f2b1e6123c18def94295d0afab7Kenny Root/* Things should be fastest when this matches the machine word size */
53c74663799493f2b1e6123c18def94295d0afab7Kenny Root/* WATCHOUT: if you change this you must also change the following #defines down to COUNT_ZERO_MSBS below to match */
54c74663799493f2b1e6123c18def94295d0afab7Kenny Root/* WATCHOUT: there are a few places where the code will not work unless brword is >= 32 bits wide */
55c74663799493f2b1e6123c18def94295d0afab7Kenny Root/*           also, some sections currently only have fast versions for 4 or 8 bytes per word */
56c74663799493f2b1e6123c18def94295d0afab7Kenny Roottypedef FLAC__uint32 brword;
57c74663799493f2b1e6123c18def94295d0afab7Kenny Root#define FLAC__BYTES_PER_WORD 4
58c74663799493f2b1e6123c18def94295d0afab7Kenny Root#define FLAC__BITS_PER_WORD 32
59c74663799493f2b1e6123c18def94295d0afab7Kenny Root#define FLAC__WORD_ALL_ONES ((FLAC__uint32)0xffffffff)
60c74663799493f2b1e6123c18def94295d0afab7Kenny Root/* SWAP_BE_WORD_TO_HOST swaps bytes in a brword (which is always big-endian) if necessary to match host byte order */
61c74663799493f2b1e6123c18def94295d0afab7Kenny Root#if WORDS_BIGENDIAN
62c74663799493f2b1e6123c18def94295d0afab7Kenny Root#define SWAP_BE_WORD_TO_HOST(x) (x)
63c74663799493f2b1e6123c18def94295d0afab7Kenny Root#else
64c74663799493f2b1e6123c18def94295d0afab7Kenny Root#ifdef _MSC_VER
65c74663799493f2b1e6123c18def94295d0afab7Kenny Root#define SWAP_BE_WORD_TO_HOST(x) local_swap32_(x)
66c74663799493f2b1e6123c18def94295d0afab7Kenny Root#else
67c74663799493f2b1e6123c18def94295d0afab7Kenny Root#define SWAP_BE_WORD_TO_HOST(x) ntohl(x)
68c74663799493f2b1e6123c18def94295d0afab7Kenny Root#endif
69c74663799493f2b1e6123c18def94295d0afab7Kenny Root#endif
70c74663799493f2b1e6123c18def94295d0afab7Kenny Root/* counts the # of zero MSBs in a word */
71c74663799493f2b1e6123c18def94295d0afab7Kenny Root#define COUNT_ZERO_MSBS(word) ( \
72c74663799493f2b1e6123c18def94295d0afab7Kenny Root	(word) <= 0xffff ? \
73c74663799493f2b1e6123c18def94295d0afab7Kenny Root		( (word) <= 0xff? byte_to_unary_table[word] + 24 : byte_to_unary_table[(word) >> 8] + 16 ) : \
74c74663799493f2b1e6123c18def94295d0afab7Kenny Root		( (word) <= 0xffffff? byte_to_unary_table[word >> 16] + 8 : byte_to_unary_table[(word) >> 24] ) \
75c74663799493f2b1e6123c18def94295d0afab7Kenny Root)
76c74663799493f2b1e6123c18def94295d0afab7Kenny Root/* this alternate might be slightly faster on some systems/compilers: */
77c74663799493f2b1e6123c18def94295d0afab7Kenny Root#define COUNT_ZERO_MSBS2(word) ( (word) <= 0xff ? byte_to_unary_table[word] + 24 : ((word) <= 0xffff ? byte_to_unary_table[(word) >> 8] + 16 : ((word) <= 0xffffff ? byte_to_unary_table[(word) >> 16] + 8 : byte_to_unary_table[(word) >> 24])) )
78c74663799493f2b1e6123c18def94295d0afab7Kenny Root
79c74663799493f2b1e6123c18def94295d0afab7Kenny Root
80c74663799493f2b1e6123c18def94295d0afab7Kenny Root/*
81c74663799493f2b1e6123c18def94295d0afab7Kenny Root * This should be at least twice as large as the largest number of words
82c74663799493f2b1e6123c18def94295d0afab7Kenny Root * required to represent any 'number' (in any encoding) you are going to
83c74663799493f2b1e6123c18def94295d0afab7Kenny Root * read.  With FLAC this is on the order of maybe a few hundred bits.
84c74663799493f2b1e6123c18def94295d0afab7Kenny Root * If the buffer is smaller than that, the decoder won't be able to read
85c74663799493f2b1e6123c18def94295d0afab7Kenny Root * in a whole number that is in a variable length encoding (e.g. Rice).
86c74663799493f2b1e6123c18def94295d0afab7Kenny Root * But to be practical it should be at least 1K bytes.
87c74663799493f2b1e6123c18def94295d0afab7Kenny Root *
88c74663799493f2b1e6123c18def94295d0afab7Kenny Root * Increase this number to decrease the number of read callbacks, at the
89c74663799493f2b1e6123c18def94295d0afab7Kenny Root * expense of using more memory.  Or decrease for the reverse effect,
90c74663799493f2b1e6123c18def94295d0afab7Kenny Root * keeping in mind the limit from the first paragraph.  The optimal size
91c74663799493f2b1e6123c18def94295d0afab7Kenny Root * also depends on the CPU cache size and other factors; some twiddling
92c74663799493f2b1e6123c18def94295d0afab7Kenny Root * may be necessary to squeeze out the best performance.
93c74663799493f2b1e6123c18def94295d0afab7Kenny Root */
94c74663799493f2b1e6123c18def94295d0afab7Kenny Rootstatic const unsigned FLAC__BITREADER_DEFAULT_CAPACITY = 65536u / FLAC__BITS_PER_WORD; /* in words */
95c74663799493f2b1e6123c18def94295d0afab7Kenny Root
96c74663799493f2b1e6123c18def94295d0afab7Kenny Rootstatic const unsigned char byte_to_unary_table[] = {
97c74663799493f2b1e6123c18def94295d0afab7Kenny Root	8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
98c74663799493f2b1e6123c18def94295d0afab7Kenny Root	3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
99c74663799493f2b1e6123c18def94295d0afab7Kenny Root	2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
100c74663799493f2b1e6123c18def94295d0afab7Kenny Root	2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
101c74663799493f2b1e6123c18def94295d0afab7Kenny Root	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
102c74663799493f2b1e6123c18def94295d0afab7Kenny Root	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
103c74663799493f2b1e6123c18def94295d0afab7Kenny Root	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
104c74663799493f2b1e6123c18def94295d0afab7Kenny Root	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
105c74663799493f2b1e6123c18def94295d0afab7Kenny Root	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
106c74663799493f2b1e6123c18def94295d0afab7Kenny Root	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
107c74663799493f2b1e6123c18def94295d0afab7Kenny Root	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
108c74663799493f2b1e6123c18def94295d0afab7Kenny Root	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
109c74663799493f2b1e6123c18def94295d0afab7Kenny Root	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
110c74663799493f2b1e6123c18def94295d0afab7Kenny Root	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
111c74663799493f2b1e6123c18def94295d0afab7Kenny Root	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
112c74663799493f2b1e6123c18def94295d0afab7Kenny Root	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
113c74663799493f2b1e6123c18def94295d0afab7Kenny Root};
114c74663799493f2b1e6123c18def94295d0afab7Kenny Root
115c74663799493f2b1e6123c18def94295d0afab7Kenny Root#ifdef min
116c74663799493f2b1e6123c18def94295d0afab7Kenny Root#undef min
117c74663799493f2b1e6123c18def94295d0afab7Kenny Root#endif
118c74663799493f2b1e6123c18def94295d0afab7Kenny Root#define min(x,y) ((x)<(y)?(x):(y))
119c74663799493f2b1e6123c18def94295d0afab7Kenny Root#ifdef max
120c74663799493f2b1e6123c18def94295d0afab7Kenny Root#undef max
121c74663799493f2b1e6123c18def94295d0afab7Kenny Root#endif
122c74663799493f2b1e6123c18def94295d0afab7Kenny Root#define max(x,y) ((x)>(y)?(x):(y))
123c74663799493f2b1e6123c18def94295d0afab7Kenny Root
124c74663799493f2b1e6123c18def94295d0afab7Kenny Root/* adjust for compilers that can't understand using LLU suffix for uint64_t literals */
125c74663799493f2b1e6123c18def94295d0afab7Kenny Root#ifdef _MSC_VER
126c74663799493f2b1e6123c18def94295d0afab7Kenny Root#define FLAC__U64L(x) x
127c74663799493f2b1e6123c18def94295d0afab7Kenny Root#else
128c74663799493f2b1e6123c18def94295d0afab7Kenny Root#define FLAC__U64L(x) x##LLU
129c74663799493f2b1e6123c18def94295d0afab7Kenny Root#endif
130c74663799493f2b1e6123c18def94295d0afab7Kenny Root
131c74663799493f2b1e6123c18def94295d0afab7Kenny Root#ifndef FLaC__INLINE
132c74663799493f2b1e6123c18def94295d0afab7Kenny Root#define FLaC__INLINE
133c74663799493f2b1e6123c18def94295d0afab7Kenny Root#endif
134c74663799493f2b1e6123c18def94295d0afab7Kenny Root
135c74663799493f2b1e6123c18def94295d0afab7Kenny Root/* WATCHOUT: assembly routines rely on the order in which these fields are declared */
136c74663799493f2b1e6123c18def94295d0afab7Kenny Rootstruct FLAC__BitReader {
137c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/* any partially-consumed word at the head will stay right-justified as bits are consumed from the left */
138c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/* any incomplete word at the tail will be left-justified, and bytes from the read callback are added on the right */
139c74663799493f2b1e6123c18def94295d0afab7Kenny Root	brword *buffer;
140c74663799493f2b1e6123c18def94295d0afab7Kenny Root	unsigned capacity; /* in words */
141c74663799493f2b1e6123c18def94295d0afab7Kenny Root	unsigned words; /* # of completed words in buffer */
142c74663799493f2b1e6123c18def94295d0afab7Kenny Root	unsigned bytes; /* # of bytes in incomplete word at buffer[words] */
143c74663799493f2b1e6123c18def94295d0afab7Kenny Root	unsigned consumed_words; /* #words ... */
144c74663799493f2b1e6123c18def94295d0afab7Kenny Root	unsigned consumed_bits; /* ... + (#bits of head word) already consumed from the front of buffer */
145c74663799493f2b1e6123c18def94295d0afab7Kenny Root	unsigned read_crc16; /* the running frame CRC */
146c74663799493f2b1e6123c18def94295d0afab7Kenny Root	unsigned crc16_align; /* the number of bits in the current consumed word that should not be CRC'd */
147c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__BitReaderReadCallback read_callback;
148c74663799493f2b1e6123c18def94295d0afab7Kenny Root	void *client_data;
149c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__CPUInfo cpu_info;
150c74663799493f2b1e6123c18def94295d0afab7Kenny Root};
151c74663799493f2b1e6123c18def94295d0afab7Kenny Root
152c74663799493f2b1e6123c18def94295d0afab7Kenny Root#ifdef _MSC_VER
153c74663799493f2b1e6123c18def94295d0afab7Kenny Root/* OPT: an MSVC built-in would be better */
154c74663799493f2b1e6123c18def94295d0afab7Kenny Rootstatic _inline FLAC__uint32 local_swap32_(FLAC__uint32 x)
155c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
156c74663799493f2b1e6123c18def94295d0afab7Kenny Root	x = ((x<<8)&0xFF00FF00) | ((x>>8)&0x00FF00FF);
157c74663799493f2b1e6123c18def94295d0afab7Kenny Root	return (x>>16) | (x<<16);
158c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
159c74663799493f2b1e6123c18def94295d0afab7Kenny Rootstatic void local_swap32_block_(FLAC__uint32 *start, FLAC__uint32 len)
160c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
161c74663799493f2b1e6123c18def94295d0afab7Kenny Root	__asm {
162c74663799493f2b1e6123c18def94295d0afab7Kenny Root		mov edx, start
163c74663799493f2b1e6123c18def94295d0afab7Kenny Root		mov ecx, len
164c74663799493f2b1e6123c18def94295d0afab7Kenny Root		test ecx, ecx
165c74663799493f2b1e6123c18def94295d0afab7Kenny Rootloop1:
166c74663799493f2b1e6123c18def94295d0afab7Kenny Root		jz done1
167c74663799493f2b1e6123c18def94295d0afab7Kenny Root		mov eax, [edx]
168c74663799493f2b1e6123c18def94295d0afab7Kenny Root		bswap eax
169c74663799493f2b1e6123c18def94295d0afab7Kenny Root		mov [edx], eax
170c74663799493f2b1e6123c18def94295d0afab7Kenny Root		add edx, 4
171c74663799493f2b1e6123c18def94295d0afab7Kenny Root		dec ecx
172c74663799493f2b1e6123c18def94295d0afab7Kenny Root		jmp short loop1
173c74663799493f2b1e6123c18def94295d0afab7Kenny Rootdone1:
174c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
175c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
176c74663799493f2b1e6123c18def94295d0afab7Kenny Root#endif
177c74663799493f2b1e6123c18def94295d0afab7Kenny Root
178c74663799493f2b1e6123c18def94295d0afab7Kenny Rootstatic FLaC__INLINE void crc16_update_word_(FLAC__BitReader *br, brword word)
179c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
180c74663799493f2b1e6123c18def94295d0afab7Kenny Root	register unsigned crc = br->read_crc16;
181c74663799493f2b1e6123c18def94295d0afab7Kenny Root#if FLAC__BYTES_PER_WORD == 4
182c74663799493f2b1e6123c18def94295d0afab7Kenny Root	switch(br->crc16_align) {
183c74663799493f2b1e6123c18def94295d0afab7Kenny Root		case  0: crc = FLAC__CRC16_UPDATE((unsigned)(word >> 24), crc);
184c74663799493f2b1e6123c18def94295d0afab7Kenny Root		case  8: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 16) & 0xff), crc);
185c74663799493f2b1e6123c18def94295d0afab7Kenny Root		case 16: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 8) & 0xff), crc);
186c74663799493f2b1e6123c18def94295d0afab7Kenny Root		case 24: br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)(word & 0xff), crc);
187c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
188c74663799493f2b1e6123c18def94295d0afab7Kenny Root#elif FLAC__BYTES_PER_WORD == 8
189c74663799493f2b1e6123c18def94295d0afab7Kenny Root	switch(br->crc16_align) {
190c74663799493f2b1e6123c18def94295d0afab7Kenny Root		case  0: crc = FLAC__CRC16_UPDATE((unsigned)(word >> 56), crc);
191c74663799493f2b1e6123c18def94295d0afab7Kenny Root		case  8: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 48) & 0xff), crc);
192c74663799493f2b1e6123c18def94295d0afab7Kenny Root		case 16: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 40) & 0xff), crc);
193c74663799493f2b1e6123c18def94295d0afab7Kenny Root		case 24: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 32) & 0xff), crc);
194c74663799493f2b1e6123c18def94295d0afab7Kenny Root		case 32: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 24) & 0xff), crc);
195c74663799493f2b1e6123c18def94295d0afab7Kenny Root		case 40: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 16) & 0xff), crc);
196c74663799493f2b1e6123c18def94295d0afab7Kenny Root		case 48: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 8) & 0xff), crc);
197c74663799493f2b1e6123c18def94295d0afab7Kenny Root		case 56: br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)(word & 0xff), crc);
198c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
199c74663799493f2b1e6123c18def94295d0afab7Kenny Root#else
200c74663799493f2b1e6123c18def94295d0afab7Kenny Root	for( ; br->crc16_align < FLAC__BITS_PER_WORD; br->crc16_align += 8)
201c74663799493f2b1e6123c18def94295d0afab7Kenny Root		crc = FLAC__CRC16_UPDATE((unsigned)((word >> (FLAC__BITS_PER_WORD-8-br->crc16_align)) & 0xff), crc);
202c74663799493f2b1e6123c18def94295d0afab7Kenny Root	br->read_crc16 = crc;
203c74663799493f2b1e6123c18def94295d0afab7Kenny Root#endif
204c74663799493f2b1e6123c18def94295d0afab7Kenny Root	br->crc16_align = 0;
205c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
206c74663799493f2b1e6123c18def94295d0afab7Kenny Root
207c74663799493f2b1e6123c18def94295d0afab7Kenny Root/* would be static except it needs to be called by asm routines */
208c74663799493f2b1e6123c18def94295d0afab7Kenny RootFLAC__bool bitreader_read_from_client_(FLAC__BitReader *br)
209c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
210c74663799493f2b1e6123c18def94295d0afab7Kenny Root	unsigned start, end;
211c74663799493f2b1e6123c18def94295d0afab7Kenny Root	size_t bytes;
212c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__byte *target;
213c74663799493f2b1e6123c18def94295d0afab7Kenny Root
214c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/* first shift the unconsumed buffer data toward the front as much as possible */
215c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(br->consumed_words > 0) {
216c74663799493f2b1e6123c18def94295d0afab7Kenny Root		start = br->consumed_words;
217c74663799493f2b1e6123c18def94295d0afab7Kenny Root		end = br->words + (br->bytes? 1:0);
218c74663799493f2b1e6123c18def94295d0afab7Kenny Root		memmove(br->buffer, br->buffer+start, FLAC__BYTES_PER_WORD * (end - start));
219c74663799493f2b1e6123c18def94295d0afab7Kenny Root
220c74663799493f2b1e6123c18def94295d0afab7Kenny Root		br->words -= start;
221c74663799493f2b1e6123c18def94295d0afab7Kenny Root		br->consumed_words = 0;
222c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
223c74663799493f2b1e6123c18def94295d0afab7Kenny Root
224c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/*
225c74663799493f2b1e6123c18def94295d0afab7Kenny Root	 * set the target for reading, taking into account word alignment and endianness
226c74663799493f2b1e6123c18def94295d0afab7Kenny Root	 */
227c74663799493f2b1e6123c18def94295d0afab7Kenny Root	bytes = (br->capacity - br->words) * FLAC__BYTES_PER_WORD - br->bytes;
228c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(bytes == 0)
229c74663799493f2b1e6123c18def94295d0afab7Kenny Root		return false; /* no space left, buffer is too small; see note for FLAC__BITREADER_DEFAULT_CAPACITY  */
230c74663799493f2b1e6123c18def94295d0afab7Kenny Root	target = ((FLAC__byte*)(br->buffer+br->words)) + br->bytes;
231c74663799493f2b1e6123c18def94295d0afab7Kenny Root
232c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/* before reading, if the existing reader looks like this (say brword is 32 bits wide)
233c74663799493f2b1e6123c18def94295d0afab7Kenny Root	 *   bitstream :  11 22 33 44 55            br->words=1 br->bytes=1 (partial tail word is left-justified)
234c74663799493f2b1e6123c18def94295d0afab7Kenny Root	 *   buffer[BE]:  11 22 33 44 55 ?? ?? ??   (shown layed out as bytes sequentially in memory)
235c74663799493f2b1e6123c18def94295d0afab7Kenny Root	 *   buffer[LE]:  44 33 22 11 ?? ?? ?? 55   (?? being don't-care)
236c74663799493f2b1e6123c18def94295d0afab7Kenny Root	 *                               ^^-------target, bytes=3
237c74663799493f2b1e6123c18def94295d0afab7Kenny Root	 * on LE machines, have to byteswap the odd tail word so nothing is
238c74663799493f2b1e6123c18def94295d0afab7Kenny Root	 * overwritten:
239c74663799493f2b1e6123c18def94295d0afab7Kenny Root	 */
240c74663799493f2b1e6123c18def94295d0afab7Kenny Root#if WORDS_BIGENDIAN
241c74663799493f2b1e6123c18def94295d0afab7Kenny Root#else
242c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(br->bytes)
243c74663799493f2b1e6123c18def94295d0afab7Kenny Root		br->buffer[br->words] = SWAP_BE_WORD_TO_HOST(br->buffer[br->words]);
244c74663799493f2b1e6123c18def94295d0afab7Kenny Root#endif
245c74663799493f2b1e6123c18def94295d0afab7Kenny Root
246c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/* now it looks like:
247c74663799493f2b1e6123c18def94295d0afab7Kenny Root	 *   bitstream :  11 22 33 44 55            br->words=1 br->bytes=1
248c74663799493f2b1e6123c18def94295d0afab7Kenny Root	 *   buffer[BE]:  11 22 33 44 55 ?? ?? ??
249c74663799493f2b1e6123c18def94295d0afab7Kenny Root	 *   buffer[LE]:  44 33 22 11 55 ?? ?? ??
250c74663799493f2b1e6123c18def94295d0afab7Kenny Root	 *                               ^^-------target, bytes=3
251c74663799493f2b1e6123c18def94295d0afab7Kenny Root	 */
252c74663799493f2b1e6123c18def94295d0afab7Kenny Root
253c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/* read in the data; note that the callback may return a smaller number of bytes */
254c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(!br->read_callback(target, &bytes, br->client_data))
255c74663799493f2b1e6123c18def94295d0afab7Kenny Root		return false;
256c74663799493f2b1e6123c18def94295d0afab7Kenny Root
257c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/* after reading bytes 66 77 88 99 AA BB CC DD EE FF from the client:
258c74663799493f2b1e6123c18def94295d0afab7Kenny Root	 *   bitstream :  11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF
259c74663799493f2b1e6123c18def94295d0afab7Kenny Root	 *   buffer[BE]:  11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF ??
260c74663799493f2b1e6123c18def94295d0afab7Kenny Root	 *   buffer[LE]:  44 33 22 11 55 66 77 88 99 AA BB CC DD EE FF ??
261c74663799493f2b1e6123c18def94295d0afab7Kenny Root	 * now have to byteswap on LE machines:
262c74663799493f2b1e6123c18def94295d0afab7Kenny Root	 */
263c74663799493f2b1e6123c18def94295d0afab7Kenny Root#if WORDS_BIGENDIAN
264c74663799493f2b1e6123c18def94295d0afab7Kenny Root#else
265c74663799493f2b1e6123c18def94295d0afab7Kenny Root	end = (br->words*FLAC__BYTES_PER_WORD + br->bytes + bytes + (FLAC__BYTES_PER_WORD-1)) / FLAC__BYTES_PER_WORD;
266c74663799493f2b1e6123c18def94295d0afab7Kenny Root# if defined(_MSC_VER) && (FLAC__BYTES_PER_WORD == 4)
267c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(br->cpu_info.type == FLAC__CPUINFO_TYPE_IA32 && br->cpu_info.data.ia32.bswap) {
268c74663799493f2b1e6123c18def94295d0afab7Kenny Root		start = br->words;
269c74663799493f2b1e6123c18def94295d0afab7Kenny Root		local_swap32_block_(br->buffer + start, end - start);
270c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
271c74663799493f2b1e6123c18def94295d0afab7Kenny Root	else
272c74663799493f2b1e6123c18def94295d0afab7Kenny Root# endif
273c74663799493f2b1e6123c18def94295d0afab7Kenny Root	for(start = br->words; start < end; start++)
274c74663799493f2b1e6123c18def94295d0afab7Kenny Root		br->buffer[start] = SWAP_BE_WORD_TO_HOST(br->buffer[start]);
275c74663799493f2b1e6123c18def94295d0afab7Kenny Root#endif
276c74663799493f2b1e6123c18def94295d0afab7Kenny Root
277c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/* now it looks like:
278c74663799493f2b1e6123c18def94295d0afab7Kenny Root	 *   bitstream :  11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF
279c74663799493f2b1e6123c18def94295d0afab7Kenny Root	 *   buffer[BE]:  11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF ??
280c74663799493f2b1e6123c18def94295d0afab7Kenny Root	 *   buffer[LE]:  44 33 22 11 88 77 66 55 CC BB AA 99 ?? FF EE DD
281c74663799493f2b1e6123c18def94295d0afab7Kenny Root	 * finally we'll update the reader values:
282c74663799493f2b1e6123c18def94295d0afab7Kenny Root	 */
283c74663799493f2b1e6123c18def94295d0afab7Kenny Root	end = br->words*FLAC__BYTES_PER_WORD + br->bytes + bytes;
284c74663799493f2b1e6123c18def94295d0afab7Kenny Root	br->words = end / FLAC__BYTES_PER_WORD;
285c74663799493f2b1e6123c18def94295d0afab7Kenny Root	br->bytes = end % FLAC__BYTES_PER_WORD;
286c74663799493f2b1e6123c18def94295d0afab7Kenny Root
287c74663799493f2b1e6123c18def94295d0afab7Kenny Root	return true;
288c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
289c74663799493f2b1e6123c18def94295d0afab7Kenny Root
290c74663799493f2b1e6123c18def94295d0afab7Kenny Root/***********************************************************************
291c74663799493f2b1e6123c18def94295d0afab7Kenny Root *
292c74663799493f2b1e6123c18def94295d0afab7Kenny Root * Class constructor/destructor
293c74663799493f2b1e6123c18def94295d0afab7Kenny Root *
294c74663799493f2b1e6123c18def94295d0afab7Kenny Root ***********************************************************************/
295c74663799493f2b1e6123c18def94295d0afab7Kenny Root
296c74663799493f2b1e6123c18def94295d0afab7Kenny RootFLAC__BitReader *FLAC__bitreader_new(void)
297c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
298c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__BitReader *br = (FLAC__BitReader*)calloc(1, sizeof(FLAC__BitReader));
299c74663799493f2b1e6123c18def94295d0afab7Kenny Root
300c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/* calloc() implies:
301c74663799493f2b1e6123c18def94295d0afab7Kenny Root		memset(br, 0, sizeof(FLAC__BitReader));
302c74663799493f2b1e6123c18def94295d0afab7Kenny Root		br->buffer = 0;
303c74663799493f2b1e6123c18def94295d0afab7Kenny Root		br->capacity = 0;
304c74663799493f2b1e6123c18def94295d0afab7Kenny Root		br->words = br->bytes = 0;
305c74663799493f2b1e6123c18def94295d0afab7Kenny Root		br->consumed_words = br->consumed_bits = 0;
306c74663799493f2b1e6123c18def94295d0afab7Kenny Root		br->read_callback = 0;
307c74663799493f2b1e6123c18def94295d0afab7Kenny Root		br->client_data = 0;
308c74663799493f2b1e6123c18def94295d0afab7Kenny Root	*/
309c74663799493f2b1e6123c18def94295d0afab7Kenny Root	return br;
310c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
311c74663799493f2b1e6123c18def94295d0afab7Kenny Root
312c74663799493f2b1e6123c18def94295d0afab7Kenny Rootvoid FLAC__bitreader_delete(FLAC__BitReader *br)
313c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
314c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(0 != br);
315c74663799493f2b1e6123c18def94295d0afab7Kenny Root
316c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__bitreader_free(br);
317c74663799493f2b1e6123c18def94295d0afab7Kenny Root	free(br);
318c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
319c74663799493f2b1e6123c18def94295d0afab7Kenny Root
320c74663799493f2b1e6123c18def94295d0afab7Kenny Root/***********************************************************************
321c74663799493f2b1e6123c18def94295d0afab7Kenny Root *
322c74663799493f2b1e6123c18def94295d0afab7Kenny Root * Public class methods
323c74663799493f2b1e6123c18def94295d0afab7Kenny Root *
324c74663799493f2b1e6123c18def94295d0afab7Kenny Root ***********************************************************************/
325c74663799493f2b1e6123c18def94295d0afab7Kenny Root
326c74663799493f2b1e6123c18def94295d0afab7Kenny RootFLAC__bool FLAC__bitreader_init(FLAC__BitReader *br, FLAC__CPUInfo cpu, FLAC__BitReaderReadCallback rcb, void *cd)
327c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
328c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(0 != br);
329c74663799493f2b1e6123c18def94295d0afab7Kenny Root
330c74663799493f2b1e6123c18def94295d0afab7Kenny Root	br->words = br->bytes = 0;
331c74663799493f2b1e6123c18def94295d0afab7Kenny Root	br->consumed_words = br->consumed_bits = 0;
332c74663799493f2b1e6123c18def94295d0afab7Kenny Root	br->capacity = FLAC__BITREADER_DEFAULT_CAPACITY;
333c74663799493f2b1e6123c18def94295d0afab7Kenny Root	br->buffer = (brword*)malloc(sizeof(brword) * br->capacity);
334c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(br->buffer == 0)
335c74663799493f2b1e6123c18def94295d0afab7Kenny Root		return false;
336c74663799493f2b1e6123c18def94295d0afab7Kenny Root	br->read_callback = rcb;
337c74663799493f2b1e6123c18def94295d0afab7Kenny Root	br->client_data = cd;
338c74663799493f2b1e6123c18def94295d0afab7Kenny Root	br->cpu_info = cpu;
339c74663799493f2b1e6123c18def94295d0afab7Kenny Root
340c74663799493f2b1e6123c18def94295d0afab7Kenny Root	return true;
341c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
342c74663799493f2b1e6123c18def94295d0afab7Kenny Root
343c74663799493f2b1e6123c18def94295d0afab7Kenny Rootvoid FLAC__bitreader_free(FLAC__BitReader *br)
344c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
345c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(0 != br);
346c74663799493f2b1e6123c18def94295d0afab7Kenny Root
347c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(0 != br->buffer)
348c74663799493f2b1e6123c18def94295d0afab7Kenny Root		free(br->buffer);
349c74663799493f2b1e6123c18def94295d0afab7Kenny Root	br->buffer = 0;
350c74663799493f2b1e6123c18def94295d0afab7Kenny Root	br->capacity = 0;
351c74663799493f2b1e6123c18def94295d0afab7Kenny Root	br->words = br->bytes = 0;
352c74663799493f2b1e6123c18def94295d0afab7Kenny Root	br->consumed_words = br->consumed_bits = 0;
353c74663799493f2b1e6123c18def94295d0afab7Kenny Root	br->read_callback = 0;
354c74663799493f2b1e6123c18def94295d0afab7Kenny Root	br->client_data = 0;
355c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
356c74663799493f2b1e6123c18def94295d0afab7Kenny Root
357c74663799493f2b1e6123c18def94295d0afab7Kenny RootFLAC__bool FLAC__bitreader_clear(FLAC__BitReader *br)
358c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
359c74663799493f2b1e6123c18def94295d0afab7Kenny Root	br->words = br->bytes = 0;
360c74663799493f2b1e6123c18def94295d0afab7Kenny Root	br->consumed_words = br->consumed_bits = 0;
361c74663799493f2b1e6123c18def94295d0afab7Kenny Root	return true;
362c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
363c74663799493f2b1e6123c18def94295d0afab7Kenny Root
364c74663799493f2b1e6123c18def94295d0afab7Kenny Rootvoid FLAC__bitreader_dump(const FLAC__BitReader *br, FILE *out)
365c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
366c74663799493f2b1e6123c18def94295d0afab7Kenny Root	unsigned i, j;
367c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(br == 0) {
368c74663799493f2b1e6123c18def94295d0afab7Kenny Root		fprintf(out, "bitreader is NULL\n");
369c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
370c74663799493f2b1e6123c18def94295d0afab7Kenny Root	else {
371c74663799493f2b1e6123c18def94295d0afab7Kenny Root		fprintf(out, "bitreader: capacity=%u words=%u bytes=%u consumed: words=%u, bits=%u\n", br->capacity, br->words, br->bytes, br->consumed_words, br->consumed_bits);
372c74663799493f2b1e6123c18def94295d0afab7Kenny Root
373c74663799493f2b1e6123c18def94295d0afab7Kenny Root		for(i = 0; i < br->words; i++) {
374c74663799493f2b1e6123c18def94295d0afab7Kenny Root			fprintf(out, "%08X: ", i);
375c74663799493f2b1e6123c18def94295d0afab7Kenny Root			for(j = 0; j < FLAC__BITS_PER_WORD; j++)
376c74663799493f2b1e6123c18def94295d0afab7Kenny Root				if(i < br->consumed_words || (i == br->consumed_words && j < br->consumed_bits))
377c74663799493f2b1e6123c18def94295d0afab7Kenny Root					fprintf(out, ".");
378c74663799493f2b1e6123c18def94295d0afab7Kenny Root				else
379c74663799493f2b1e6123c18def94295d0afab7Kenny Root					fprintf(out, "%01u", br->buffer[i] & (1 << (FLAC__BITS_PER_WORD-j-1)) ? 1:0);
380c74663799493f2b1e6123c18def94295d0afab7Kenny Root			fprintf(out, "\n");
381c74663799493f2b1e6123c18def94295d0afab7Kenny Root		}
382c74663799493f2b1e6123c18def94295d0afab7Kenny Root		if(br->bytes > 0) {
383c74663799493f2b1e6123c18def94295d0afab7Kenny Root			fprintf(out, "%08X: ", i);
384c74663799493f2b1e6123c18def94295d0afab7Kenny Root			for(j = 0; j < br->bytes*8; j++)
385c74663799493f2b1e6123c18def94295d0afab7Kenny Root				if(i < br->consumed_words || (i == br->consumed_words && j < br->consumed_bits))
386c74663799493f2b1e6123c18def94295d0afab7Kenny Root					fprintf(out, ".");
387c74663799493f2b1e6123c18def94295d0afab7Kenny Root				else
388c74663799493f2b1e6123c18def94295d0afab7Kenny Root					fprintf(out, "%01u", br->buffer[i] & (1 << (br->bytes*8-j-1)) ? 1:0);
389c74663799493f2b1e6123c18def94295d0afab7Kenny Root			fprintf(out, "\n");
390c74663799493f2b1e6123c18def94295d0afab7Kenny Root		}
391c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
392c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
393c74663799493f2b1e6123c18def94295d0afab7Kenny Root
394c74663799493f2b1e6123c18def94295d0afab7Kenny Rootvoid FLAC__bitreader_reset_read_crc16(FLAC__BitReader *br, FLAC__uint16 seed)
395c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
396c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(0 != br);
397c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(0 != br->buffer);
398c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT((br->consumed_bits & 7) == 0);
399c74663799493f2b1e6123c18def94295d0afab7Kenny Root
400c74663799493f2b1e6123c18def94295d0afab7Kenny Root	br->read_crc16 = (unsigned)seed;
401c74663799493f2b1e6123c18def94295d0afab7Kenny Root	br->crc16_align = br->consumed_bits;
402c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
403c74663799493f2b1e6123c18def94295d0afab7Kenny Root
404c74663799493f2b1e6123c18def94295d0afab7Kenny RootFLAC__uint16 FLAC__bitreader_get_read_crc16(FLAC__BitReader *br)
405c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
406c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(0 != br);
407c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(0 != br->buffer);
408c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT((br->consumed_bits & 7) == 0);
409c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(br->crc16_align <= br->consumed_bits);
410c74663799493f2b1e6123c18def94295d0afab7Kenny Root
411c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/* CRC any tail bytes in a partially-consumed word */
412c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(br->consumed_bits) {
413c74663799493f2b1e6123c18def94295d0afab7Kenny Root		const brword tail = br->buffer[br->consumed_words];
414c74663799493f2b1e6123c18def94295d0afab7Kenny Root		for( ; br->crc16_align < br->consumed_bits; br->crc16_align += 8)
415c74663799493f2b1e6123c18def94295d0afab7Kenny Root			br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)((tail >> (FLAC__BITS_PER_WORD-8-br->crc16_align)) & 0xff), br->read_crc16);
416c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
417c74663799493f2b1e6123c18def94295d0afab7Kenny Root	return br->read_crc16;
418c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
419c74663799493f2b1e6123c18def94295d0afab7Kenny Root
420c74663799493f2b1e6123c18def94295d0afab7Kenny RootFLaC__INLINE FLAC__bool FLAC__bitreader_is_consumed_byte_aligned(const FLAC__BitReader *br)
421c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
422c74663799493f2b1e6123c18def94295d0afab7Kenny Root	return ((br->consumed_bits & 7) == 0);
423c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
424c74663799493f2b1e6123c18def94295d0afab7Kenny Root
425c74663799493f2b1e6123c18def94295d0afab7Kenny RootFLaC__INLINE unsigned FLAC__bitreader_bits_left_for_byte_alignment(const FLAC__BitReader *br)
426c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
427c74663799493f2b1e6123c18def94295d0afab7Kenny Root	return 8 - (br->consumed_bits & 7);
428c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
429c74663799493f2b1e6123c18def94295d0afab7Kenny Root
430c74663799493f2b1e6123c18def94295d0afab7Kenny RootFLaC__INLINE unsigned FLAC__bitreader_get_input_bits_unconsumed(const FLAC__BitReader *br)
431c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
432c74663799493f2b1e6123c18def94295d0afab7Kenny Root	return (br->words-br->consumed_words)*FLAC__BITS_PER_WORD + br->bytes*8 - br->consumed_bits;
433c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
434c74663799493f2b1e6123c18def94295d0afab7Kenny Root
435c74663799493f2b1e6123c18def94295d0afab7Kenny RootFLaC__INLINE FLAC__bool FLAC__bitreader_read_raw_uint32(FLAC__BitReader *br, FLAC__uint32 *val, unsigned bits)
436c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
437c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(0 != br);
438c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(0 != br->buffer);
439c74663799493f2b1e6123c18def94295d0afab7Kenny Root
440c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(bits <= 32);
441c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT((br->capacity*FLAC__BITS_PER_WORD) * 2 >= bits);
442c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(br->consumed_words <= br->words);
443c74663799493f2b1e6123c18def94295d0afab7Kenny Root
444c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
445c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
446c74663799493f2b1e6123c18def94295d0afab7Kenny Root
447c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(bits == 0) { /* OPT: investigate if this can ever happen, maybe change to assertion */
448c74663799493f2b1e6123c18def94295d0afab7Kenny Root		*val = 0;
449c74663799493f2b1e6123c18def94295d0afab7Kenny Root		return true;
450c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
451c74663799493f2b1e6123c18def94295d0afab7Kenny Root
452c74663799493f2b1e6123c18def94295d0afab7Kenny Root	while((br->words-br->consumed_words)*FLAC__BITS_PER_WORD + br->bytes*8 - br->consumed_bits < bits) {
453c74663799493f2b1e6123c18def94295d0afab7Kenny Root		if(!bitreader_read_from_client_(br))
454c74663799493f2b1e6123c18def94295d0afab7Kenny Root			return false;
455c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
456c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(br->consumed_words < br->words) { /* if we've not consumed up to a partial tail word... */
457c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* OPT: taking out the consumed_bits==0 "else" case below might make things faster if less code allows the compiler to inline this function */
458c74663799493f2b1e6123c18def94295d0afab7Kenny Root		if(br->consumed_bits) {
459c74663799493f2b1e6123c18def94295d0afab7Kenny Root			/* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
460c74663799493f2b1e6123c18def94295d0afab7Kenny Root			const unsigned n = FLAC__BITS_PER_WORD - br->consumed_bits;
461c74663799493f2b1e6123c18def94295d0afab7Kenny Root			const brword word = br->buffer[br->consumed_words];
462c74663799493f2b1e6123c18def94295d0afab7Kenny Root			if(bits < n) {
463c74663799493f2b1e6123c18def94295d0afab7Kenny Root				*val = (word & (FLAC__WORD_ALL_ONES >> br->consumed_bits)) >> (n-bits);
464c74663799493f2b1e6123c18def94295d0afab7Kenny Root				br->consumed_bits += bits;
465c74663799493f2b1e6123c18def94295d0afab7Kenny Root				return true;
466c74663799493f2b1e6123c18def94295d0afab7Kenny Root			}
467c74663799493f2b1e6123c18def94295d0afab7Kenny Root			*val = word & (FLAC__WORD_ALL_ONES >> br->consumed_bits);
468c74663799493f2b1e6123c18def94295d0afab7Kenny Root			bits -= n;
469c74663799493f2b1e6123c18def94295d0afab7Kenny Root			crc16_update_word_(br, word);
470c74663799493f2b1e6123c18def94295d0afab7Kenny Root			br->consumed_words++;
471c74663799493f2b1e6123c18def94295d0afab7Kenny Root			br->consumed_bits = 0;
472c74663799493f2b1e6123c18def94295d0afab7Kenny Root			if(bits) { /* if there are still bits left to read, there have to be less than 32 so they will all be in the next word */
473c74663799493f2b1e6123c18def94295d0afab7Kenny Root				*val <<= bits;
474c74663799493f2b1e6123c18def94295d0afab7Kenny Root				*val |= (br->buffer[br->consumed_words] >> (FLAC__BITS_PER_WORD-bits));
475c74663799493f2b1e6123c18def94295d0afab7Kenny Root				br->consumed_bits = bits;
476c74663799493f2b1e6123c18def94295d0afab7Kenny Root			}
477c74663799493f2b1e6123c18def94295d0afab7Kenny Root			return true;
478c74663799493f2b1e6123c18def94295d0afab7Kenny Root		}
479c74663799493f2b1e6123c18def94295d0afab7Kenny Root		else {
480c74663799493f2b1e6123c18def94295d0afab7Kenny Root			const brword word = br->buffer[br->consumed_words];
481c74663799493f2b1e6123c18def94295d0afab7Kenny Root			if(bits < FLAC__BITS_PER_WORD) {
482c74663799493f2b1e6123c18def94295d0afab7Kenny Root				*val = word >> (FLAC__BITS_PER_WORD-bits);
483c74663799493f2b1e6123c18def94295d0afab7Kenny Root				br->consumed_bits = bits;
484c74663799493f2b1e6123c18def94295d0afab7Kenny Root				return true;
485c74663799493f2b1e6123c18def94295d0afab7Kenny Root			}
486c74663799493f2b1e6123c18def94295d0afab7Kenny Root			/* at this point 'bits' must be == FLAC__BITS_PER_WORD; because of previous assertions, it can't be larger */
487c74663799493f2b1e6123c18def94295d0afab7Kenny Root			*val = word;
488c74663799493f2b1e6123c18def94295d0afab7Kenny Root			crc16_update_word_(br, word);
489c74663799493f2b1e6123c18def94295d0afab7Kenny Root			br->consumed_words++;
490c74663799493f2b1e6123c18def94295d0afab7Kenny Root			return true;
491c74663799493f2b1e6123c18def94295d0afab7Kenny Root		}
492c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
493c74663799493f2b1e6123c18def94295d0afab7Kenny Root	else {
494c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* in this case we're starting our read at a partial tail word;
495c74663799493f2b1e6123c18def94295d0afab7Kenny Root		 * the reader has guaranteed that we have at least 'bits' bits
496c74663799493f2b1e6123c18def94295d0afab7Kenny Root		 * available to read, which makes this case simpler.
497c74663799493f2b1e6123c18def94295d0afab7Kenny Root		 */
498c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* OPT: taking out the consumed_bits==0 "else" case below might make things faster if less code allows the compiler to inline this function */
499c74663799493f2b1e6123c18def94295d0afab7Kenny Root		if(br->consumed_bits) {
500c74663799493f2b1e6123c18def94295d0afab7Kenny Root			/* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
501c74663799493f2b1e6123c18def94295d0afab7Kenny Root			FLAC__ASSERT(br->consumed_bits + bits <= br->bytes*8);
502c74663799493f2b1e6123c18def94295d0afab7Kenny Root			*val = (br->buffer[br->consumed_words] & (FLAC__WORD_ALL_ONES >> br->consumed_bits)) >> (FLAC__BITS_PER_WORD-br->consumed_bits-bits);
503c74663799493f2b1e6123c18def94295d0afab7Kenny Root			br->consumed_bits += bits;
504c74663799493f2b1e6123c18def94295d0afab7Kenny Root			return true;
505c74663799493f2b1e6123c18def94295d0afab7Kenny Root		}
506c74663799493f2b1e6123c18def94295d0afab7Kenny Root		else {
507c74663799493f2b1e6123c18def94295d0afab7Kenny Root			*val = br->buffer[br->consumed_words] >> (FLAC__BITS_PER_WORD-bits);
508c74663799493f2b1e6123c18def94295d0afab7Kenny Root			br->consumed_bits += bits;
509c74663799493f2b1e6123c18def94295d0afab7Kenny Root			return true;
510c74663799493f2b1e6123c18def94295d0afab7Kenny Root		}
511c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
512c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
513c74663799493f2b1e6123c18def94295d0afab7Kenny Root
514c74663799493f2b1e6123c18def94295d0afab7Kenny RootFLAC__bool FLAC__bitreader_read_raw_int32(FLAC__BitReader *br, FLAC__int32 *val, unsigned bits)
515c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
516c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/* OPT: inline raw uint32 code here, or make into a macro if possible in the .h file */
517c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(!FLAC__bitreader_read_raw_uint32(br, (FLAC__uint32*)val, bits))
518c74663799493f2b1e6123c18def94295d0afab7Kenny Root		return false;
519c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/* sign-extend: */
520c74663799493f2b1e6123c18def94295d0afab7Kenny Root	*val <<= (32-bits);
521c74663799493f2b1e6123c18def94295d0afab7Kenny Root	*val >>= (32-bits);
522c74663799493f2b1e6123c18def94295d0afab7Kenny Root	return true;
523c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
524c74663799493f2b1e6123c18def94295d0afab7Kenny Root
525c74663799493f2b1e6123c18def94295d0afab7Kenny RootFLAC__bool FLAC__bitreader_read_raw_uint64(FLAC__BitReader *br, FLAC__uint64 *val, unsigned bits)
526c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
527c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__uint32 hi, lo;
528c74663799493f2b1e6123c18def94295d0afab7Kenny Root
529c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(bits > 32) {
530c74663799493f2b1e6123c18def94295d0afab7Kenny Root		if(!FLAC__bitreader_read_raw_uint32(br, &hi, bits-32))
531c74663799493f2b1e6123c18def94295d0afab7Kenny Root			return false;
532c74663799493f2b1e6123c18def94295d0afab7Kenny Root		if(!FLAC__bitreader_read_raw_uint32(br, &lo, 32))
533c74663799493f2b1e6123c18def94295d0afab7Kenny Root			return false;
534c74663799493f2b1e6123c18def94295d0afab7Kenny Root		*val = hi;
535c74663799493f2b1e6123c18def94295d0afab7Kenny Root		*val <<= 32;
536c74663799493f2b1e6123c18def94295d0afab7Kenny Root		*val |= lo;
537c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
538c74663799493f2b1e6123c18def94295d0afab7Kenny Root	else {
539c74663799493f2b1e6123c18def94295d0afab7Kenny Root		if(!FLAC__bitreader_read_raw_uint32(br, &lo, bits))
540c74663799493f2b1e6123c18def94295d0afab7Kenny Root			return false;
541c74663799493f2b1e6123c18def94295d0afab7Kenny Root		*val = lo;
542c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
543c74663799493f2b1e6123c18def94295d0afab7Kenny Root	return true;
544c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
545c74663799493f2b1e6123c18def94295d0afab7Kenny Root
546c74663799493f2b1e6123c18def94295d0afab7Kenny RootFLaC__INLINE FLAC__bool FLAC__bitreader_read_uint32_little_endian(FLAC__BitReader *br, FLAC__uint32 *val)
547c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
548c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__uint32 x8, x32 = 0;
549c74663799493f2b1e6123c18def94295d0afab7Kenny Root
550c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/* this doesn't need to be that fast as currently it is only used for vorbis comments */
551c74663799493f2b1e6123c18def94295d0afab7Kenny Root
552c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(!FLAC__bitreader_read_raw_uint32(br, &x32, 8))
553c74663799493f2b1e6123c18def94295d0afab7Kenny Root		return false;
554c74663799493f2b1e6123c18def94295d0afab7Kenny Root
555c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8))
556c74663799493f2b1e6123c18def94295d0afab7Kenny Root		return false;
557c74663799493f2b1e6123c18def94295d0afab7Kenny Root	x32 |= (x8 << 8);
558c74663799493f2b1e6123c18def94295d0afab7Kenny Root
559c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8))
560c74663799493f2b1e6123c18def94295d0afab7Kenny Root		return false;
561c74663799493f2b1e6123c18def94295d0afab7Kenny Root	x32 |= (x8 << 16);
562c74663799493f2b1e6123c18def94295d0afab7Kenny Root
563c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8))
564c74663799493f2b1e6123c18def94295d0afab7Kenny Root		return false;
565c74663799493f2b1e6123c18def94295d0afab7Kenny Root	x32 |= (x8 << 24);
566c74663799493f2b1e6123c18def94295d0afab7Kenny Root
567c74663799493f2b1e6123c18def94295d0afab7Kenny Root	*val = x32;
568c74663799493f2b1e6123c18def94295d0afab7Kenny Root	return true;
569c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
570c74663799493f2b1e6123c18def94295d0afab7Kenny Root
571c74663799493f2b1e6123c18def94295d0afab7Kenny RootFLAC__bool FLAC__bitreader_skip_bits_no_crc(FLAC__BitReader *br, unsigned bits)
572c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
573c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/*
574c74663799493f2b1e6123c18def94295d0afab7Kenny Root	 * OPT: a faster implementation is possible but probably not that useful
575c74663799493f2b1e6123c18def94295d0afab7Kenny Root	 * since this is only called a couple of times in the metadata readers.
576c74663799493f2b1e6123c18def94295d0afab7Kenny Root	 */
577c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(0 != br);
578c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(0 != br->buffer);
579c74663799493f2b1e6123c18def94295d0afab7Kenny Root
580c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(bits > 0) {
581c74663799493f2b1e6123c18def94295d0afab7Kenny Root		const unsigned n = br->consumed_bits & 7;
582c74663799493f2b1e6123c18def94295d0afab7Kenny Root		unsigned m;
583c74663799493f2b1e6123c18def94295d0afab7Kenny Root		FLAC__uint32 x;
584c74663799493f2b1e6123c18def94295d0afab7Kenny Root
585c74663799493f2b1e6123c18def94295d0afab7Kenny Root		if(n != 0) {
586c74663799493f2b1e6123c18def94295d0afab7Kenny Root			m = min(8-n, bits);
587c74663799493f2b1e6123c18def94295d0afab7Kenny Root			if(!FLAC__bitreader_read_raw_uint32(br, &x, m))
588c74663799493f2b1e6123c18def94295d0afab7Kenny Root				return false;
589c74663799493f2b1e6123c18def94295d0afab7Kenny Root			bits -= m;
590c74663799493f2b1e6123c18def94295d0afab7Kenny Root		}
591c74663799493f2b1e6123c18def94295d0afab7Kenny Root		m = bits / 8;
592c74663799493f2b1e6123c18def94295d0afab7Kenny Root		if(m > 0) {
593c74663799493f2b1e6123c18def94295d0afab7Kenny Root			if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(br, m))
594c74663799493f2b1e6123c18def94295d0afab7Kenny Root				return false;
595c74663799493f2b1e6123c18def94295d0afab7Kenny Root			bits %= 8;
596c74663799493f2b1e6123c18def94295d0afab7Kenny Root		}
597c74663799493f2b1e6123c18def94295d0afab7Kenny Root		if(bits > 0) {
598c74663799493f2b1e6123c18def94295d0afab7Kenny Root			if(!FLAC__bitreader_read_raw_uint32(br, &x, bits))
599c74663799493f2b1e6123c18def94295d0afab7Kenny Root				return false;
600c74663799493f2b1e6123c18def94295d0afab7Kenny Root		}
601c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
602c74663799493f2b1e6123c18def94295d0afab7Kenny Root
603c74663799493f2b1e6123c18def94295d0afab7Kenny Root	return true;
604c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
605c74663799493f2b1e6123c18def94295d0afab7Kenny Root
606c74663799493f2b1e6123c18def94295d0afab7Kenny RootFLAC__bool FLAC__bitreader_skip_byte_block_aligned_no_crc(FLAC__BitReader *br, unsigned nvals)
607c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
608c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__uint32 x;
609c74663799493f2b1e6123c18def94295d0afab7Kenny Root
610c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(0 != br);
611c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(0 != br->buffer);
612c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(br));
613c74663799493f2b1e6123c18def94295d0afab7Kenny Root
614c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/* step 1: skip over partial head word to get word aligned */
615c74663799493f2b1e6123c18def94295d0afab7Kenny Root	while(nvals && br->consumed_bits) { /* i.e. run until we read 'nvals' bytes or we hit the end of the head word */
616c74663799493f2b1e6123c18def94295d0afab7Kenny Root		if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
617c74663799493f2b1e6123c18def94295d0afab7Kenny Root			return false;
618c74663799493f2b1e6123c18def94295d0afab7Kenny Root		nvals--;
619c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
620c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(0 == nvals)
621c74663799493f2b1e6123c18def94295d0afab7Kenny Root		return true;
622c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/* step 2: skip whole words in chunks */
623c74663799493f2b1e6123c18def94295d0afab7Kenny Root	while(nvals >= FLAC__BYTES_PER_WORD) {
624c74663799493f2b1e6123c18def94295d0afab7Kenny Root		if(br->consumed_words < br->words) {
625c74663799493f2b1e6123c18def94295d0afab7Kenny Root			br->consumed_words++;
626c74663799493f2b1e6123c18def94295d0afab7Kenny Root			nvals -= FLAC__BYTES_PER_WORD;
627c74663799493f2b1e6123c18def94295d0afab7Kenny Root		}
628c74663799493f2b1e6123c18def94295d0afab7Kenny Root		else if(!bitreader_read_from_client_(br))
629c74663799493f2b1e6123c18def94295d0afab7Kenny Root			return false;
630c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
631c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/* step 3: skip any remainder from partial tail bytes */
632c74663799493f2b1e6123c18def94295d0afab7Kenny Root	while(nvals) {
633c74663799493f2b1e6123c18def94295d0afab7Kenny Root		if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
634c74663799493f2b1e6123c18def94295d0afab7Kenny Root			return false;
635c74663799493f2b1e6123c18def94295d0afab7Kenny Root		nvals--;
636c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
637c74663799493f2b1e6123c18def94295d0afab7Kenny Root
638c74663799493f2b1e6123c18def94295d0afab7Kenny Root	return true;
639c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
640c74663799493f2b1e6123c18def94295d0afab7Kenny Root
641c74663799493f2b1e6123c18def94295d0afab7Kenny RootFLAC__bool FLAC__bitreader_read_byte_block_aligned_no_crc(FLAC__BitReader *br, FLAC__byte *val, unsigned nvals)
642c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
643c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__uint32 x;
644c74663799493f2b1e6123c18def94295d0afab7Kenny Root
645c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(0 != br);
646c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(0 != br->buffer);
647c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(br));
648c74663799493f2b1e6123c18def94295d0afab7Kenny Root
649c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/* step 1: read from partial head word to get word aligned */
650c74663799493f2b1e6123c18def94295d0afab7Kenny Root	while(nvals && br->consumed_bits) { /* i.e. run until we read 'nvals' bytes or we hit the end of the head word */
651c74663799493f2b1e6123c18def94295d0afab7Kenny Root		if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
652c74663799493f2b1e6123c18def94295d0afab7Kenny Root			return false;
653c74663799493f2b1e6123c18def94295d0afab7Kenny Root		*val++ = (FLAC__byte)x;
654c74663799493f2b1e6123c18def94295d0afab7Kenny Root		nvals--;
655c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
656c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(0 == nvals)
657c74663799493f2b1e6123c18def94295d0afab7Kenny Root		return true;
658c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/* step 2: read whole words in chunks */
659c74663799493f2b1e6123c18def94295d0afab7Kenny Root	while(nvals >= FLAC__BYTES_PER_WORD) {
660c74663799493f2b1e6123c18def94295d0afab7Kenny Root		if(br->consumed_words < br->words) {
661c74663799493f2b1e6123c18def94295d0afab7Kenny Root			const brword word = br->buffer[br->consumed_words++];
662c74663799493f2b1e6123c18def94295d0afab7Kenny Root#if FLAC__BYTES_PER_WORD == 4
663c74663799493f2b1e6123c18def94295d0afab7Kenny Root			val[0] = (FLAC__byte)(word >> 24);
664c74663799493f2b1e6123c18def94295d0afab7Kenny Root			val[1] = (FLAC__byte)(word >> 16);
665c74663799493f2b1e6123c18def94295d0afab7Kenny Root			val[2] = (FLAC__byte)(word >> 8);
666c74663799493f2b1e6123c18def94295d0afab7Kenny Root			val[3] = (FLAC__byte)word;
667c74663799493f2b1e6123c18def94295d0afab7Kenny Root#elif FLAC__BYTES_PER_WORD == 8
668c74663799493f2b1e6123c18def94295d0afab7Kenny Root			val[0] = (FLAC__byte)(word >> 56);
669c74663799493f2b1e6123c18def94295d0afab7Kenny Root			val[1] = (FLAC__byte)(word >> 48);
670c74663799493f2b1e6123c18def94295d0afab7Kenny Root			val[2] = (FLAC__byte)(word >> 40);
671c74663799493f2b1e6123c18def94295d0afab7Kenny Root			val[3] = (FLAC__byte)(word >> 32);
672c74663799493f2b1e6123c18def94295d0afab7Kenny Root			val[4] = (FLAC__byte)(word >> 24);
673c74663799493f2b1e6123c18def94295d0afab7Kenny Root			val[5] = (FLAC__byte)(word >> 16);
674c74663799493f2b1e6123c18def94295d0afab7Kenny Root			val[6] = (FLAC__byte)(word >> 8);
675c74663799493f2b1e6123c18def94295d0afab7Kenny Root			val[7] = (FLAC__byte)word;
676c74663799493f2b1e6123c18def94295d0afab7Kenny Root#else
677c74663799493f2b1e6123c18def94295d0afab7Kenny Root			for(x = 0; x < FLAC__BYTES_PER_WORD; x++)
678c74663799493f2b1e6123c18def94295d0afab7Kenny Root				val[x] = (FLAC__byte)(word >> (8*(FLAC__BYTES_PER_WORD-x-1)));
679c74663799493f2b1e6123c18def94295d0afab7Kenny Root#endif
680c74663799493f2b1e6123c18def94295d0afab7Kenny Root			val += FLAC__BYTES_PER_WORD;
681c74663799493f2b1e6123c18def94295d0afab7Kenny Root			nvals -= FLAC__BYTES_PER_WORD;
682c74663799493f2b1e6123c18def94295d0afab7Kenny Root		}
683c74663799493f2b1e6123c18def94295d0afab7Kenny Root		else if(!bitreader_read_from_client_(br))
684c74663799493f2b1e6123c18def94295d0afab7Kenny Root			return false;
685c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
686c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/* step 3: read any remainder from partial tail bytes */
687c74663799493f2b1e6123c18def94295d0afab7Kenny Root	while(nvals) {
688c74663799493f2b1e6123c18def94295d0afab7Kenny Root		if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
689c74663799493f2b1e6123c18def94295d0afab7Kenny Root			return false;
690c74663799493f2b1e6123c18def94295d0afab7Kenny Root		*val++ = (FLAC__byte)x;
691c74663799493f2b1e6123c18def94295d0afab7Kenny Root		nvals--;
692c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
693c74663799493f2b1e6123c18def94295d0afab7Kenny Root
694c74663799493f2b1e6123c18def94295d0afab7Kenny Root	return true;
695c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
696c74663799493f2b1e6123c18def94295d0afab7Kenny Root
697c74663799493f2b1e6123c18def94295d0afab7Kenny RootFLaC__INLINE FLAC__bool FLAC__bitreader_read_unary_unsigned(FLAC__BitReader *br, unsigned *val)
698c74663799493f2b1e6123c18def94295d0afab7Kenny Root#if 0 /* slow but readable version */
699c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
700c74663799493f2b1e6123c18def94295d0afab7Kenny Root	unsigned bit;
701c74663799493f2b1e6123c18def94295d0afab7Kenny Root
702c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(0 != br);
703c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(0 != br->buffer);
704c74663799493f2b1e6123c18def94295d0afab7Kenny Root
705c74663799493f2b1e6123c18def94295d0afab7Kenny Root	*val = 0;
706c74663799493f2b1e6123c18def94295d0afab7Kenny Root	while(1) {
707c74663799493f2b1e6123c18def94295d0afab7Kenny Root		if(!FLAC__bitreader_read_bit(br, &bit))
708c74663799493f2b1e6123c18def94295d0afab7Kenny Root			return false;
709c74663799493f2b1e6123c18def94295d0afab7Kenny Root		if(bit)
710c74663799493f2b1e6123c18def94295d0afab7Kenny Root			break;
711c74663799493f2b1e6123c18def94295d0afab7Kenny Root		else
712c74663799493f2b1e6123c18def94295d0afab7Kenny Root			*val++;
713c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
714c74663799493f2b1e6123c18def94295d0afab7Kenny Root	return true;
715c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
716c74663799493f2b1e6123c18def94295d0afab7Kenny Root#else
717c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
718c74663799493f2b1e6123c18def94295d0afab7Kenny Root	unsigned i;
719c74663799493f2b1e6123c18def94295d0afab7Kenny Root
720c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(0 != br);
721c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(0 != br->buffer);
722c74663799493f2b1e6123c18def94295d0afab7Kenny Root
723c74663799493f2b1e6123c18def94295d0afab7Kenny Root	*val = 0;
724c74663799493f2b1e6123c18def94295d0afab7Kenny Root	while(1) {
725c74663799493f2b1e6123c18def94295d0afab7Kenny Root		while(br->consumed_words < br->words) { /* if we've not consumed up to a partial tail word... */
726c74663799493f2b1e6123c18def94295d0afab7Kenny Root			brword b = br->buffer[br->consumed_words] << br->consumed_bits;
727c74663799493f2b1e6123c18def94295d0afab7Kenny Root			if(b) {
728c74663799493f2b1e6123c18def94295d0afab7Kenny Root				i = COUNT_ZERO_MSBS(b);
729c74663799493f2b1e6123c18def94295d0afab7Kenny Root				*val += i;
730c74663799493f2b1e6123c18def94295d0afab7Kenny Root				i++;
731c74663799493f2b1e6123c18def94295d0afab7Kenny Root				br->consumed_bits += i;
732c74663799493f2b1e6123c18def94295d0afab7Kenny Root				if(br->consumed_bits >= FLAC__BITS_PER_WORD) { /* faster way of testing if(br->consumed_bits == FLAC__BITS_PER_WORD) */
733c74663799493f2b1e6123c18def94295d0afab7Kenny Root					crc16_update_word_(br, br->buffer[br->consumed_words]);
734c74663799493f2b1e6123c18def94295d0afab7Kenny Root					br->consumed_words++;
735c74663799493f2b1e6123c18def94295d0afab7Kenny Root					br->consumed_bits = 0;
736c74663799493f2b1e6123c18def94295d0afab7Kenny Root				}
737c74663799493f2b1e6123c18def94295d0afab7Kenny Root				return true;
738c74663799493f2b1e6123c18def94295d0afab7Kenny Root			}
739c74663799493f2b1e6123c18def94295d0afab7Kenny Root			else {
740c74663799493f2b1e6123c18def94295d0afab7Kenny Root				*val += FLAC__BITS_PER_WORD - br->consumed_bits;
741c74663799493f2b1e6123c18def94295d0afab7Kenny Root				crc16_update_word_(br, br->buffer[br->consumed_words]);
742c74663799493f2b1e6123c18def94295d0afab7Kenny Root				br->consumed_words++;
743c74663799493f2b1e6123c18def94295d0afab7Kenny Root				br->consumed_bits = 0;
744c74663799493f2b1e6123c18def94295d0afab7Kenny Root				/* didn't find stop bit yet, have to keep going... */
745c74663799493f2b1e6123c18def94295d0afab7Kenny Root			}
746c74663799493f2b1e6123c18def94295d0afab7Kenny Root		}
747c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* at this point we've eaten up all the whole words; have to try
748c74663799493f2b1e6123c18def94295d0afab7Kenny Root		 * reading through any tail bytes before calling the read callback.
749c74663799493f2b1e6123c18def94295d0afab7Kenny Root		 * this is a repeat of the above logic adjusted for the fact we
750c74663799493f2b1e6123c18def94295d0afab7Kenny Root		 * don't have a whole word.  note though if the client is feeding
751c74663799493f2b1e6123c18def94295d0afab7Kenny Root		 * us data a byte at a time (unlikely), br->consumed_bits may not
752c74663799493f2b1e6123c18def94295d0afab7Kenny Root		 * be zero.
753c74663799493f2b1e6123c18def94295d0afab7Kenny Root		 */
754c74663799493f2b1e6123c18def94295d0afab7Kenny Root		if(br->bytes) {
755c74663799493f2b1e6123c18def94295d0afab7Kenny Root			const unsigned end = br->bytes * 8;
756c74663799493f2b1e6123c18def94295d0afab7Kenny Root			brword b = (br->buffer[br->consumed_words] & (FLAC__WORD_ALL_ONES << (FLAC__BITS_PER_WORD-end))) << br->consumed_bits;
757c74663799493f2b1e6123c18def94295d0afab7Kenny Root			if(b) {
758c74663799493f2b1e6123c18def94295d0afab7Kenny Root				i = COUNT_ZERO_MSBS(b);
759c74663799493f2b1e6123c18def94295d0afab7Kenny Root				*val += i;
760c74663799493f2b1e6123c18def94295d0afab7Kenny Root				i++;
761c74663799493f2b1e6123c18def94295d0afab7Kenny Root				br->consumed_bits += i;
762c74663799493f2b1e6123c18def94295d0afab7Kenny Root				FLAC__ASSERT(br->consumed_bits < FLAC__BITS_PER_WORD);
763c74663799493f2b1e6123c18def94295d0afab7Kenny Root				return true;
764c74663799493f2b1e6123c18def94295d0afab7Kenny Root			}
765c74663799493f2b1e6123c18def94295d0afab7Kenny Root			else {
766c74663799493f2b1e6123c18def94295d0afab7Kenny Root				*val += end - br->consumed_bits;
767c74663799493f2b1e6123c18def94295d0afab7Kenny Root				br->consumed_bits += end;
768c74663799493f2b1e6123c18def94295d0afab7Kenny Root				FLAC__ASSERT(br->consumed_bits < FLAC__BITS_PER_WORD);
769c74663799493f2b1e6123c18def94295d0afab7Kenny Root				/* didn't find stop bit yet, have to keep going... */
770c74663799493f2b1e6123c18def94295d0afab7Kenny Root			}
771c74663799493f2b1e6123c18def94295d0afab7Kenny Root		}
772c74663799493f2b1e6123c18def94295d0afab7Kenny Root		if(!bitreader_read_from_client_(br))
773c74663799493f2b1e6123c18def94295d0afab7Kenny Root			return false;
774c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
775c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
776c74663799493f2b1e6123c18def94295d0afab7Kenny Root#endif
777c74663799493f2b1e6123c18def94295d0afab7Kenny Root
778c74663799493f2b1e6123c18def94295d0afab7Kenny RootFLAC__bool FLAC__bitreader_read_rice_signed(FLAC__BitReader *br, int *val, unsigned parameter)
779c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
780c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__uint32 lsbs = 0, msbs = 0;
781c74663799493f2b1e6123c18def94295d0afab7Kenny Root	unsigned uval;
782c74663799493f2b1e6123c18def94295d0afab7Kenny Root
783c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(0 != br);
784c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(0 != br->buffer);
785c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(parameter <= 31);
786c74663799493f2b1e6123c18def94295d0afab7Kenny Root
787c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/* read the unary MSBs and end bit */
788c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
789c74663799493f2b1e6123c18def94295d0afab7Kenny Root		return false;
790c74663799493f2b1e6123c18def94295d0afab7Kenny Root
791c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/* read the binary LSBs */
792c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, parameter))
793c74663799493f2b1e6123c18def94295d0afab7Kenny Root		return false;
794c74663799493f2b1e6123c18def94295d0afab7Kenny Root
795c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/* compose the value */
796c74663799493f2b1e6123c18def94295d0afab7Kenny Root	uval = (msbs << parameter) | lsbs;
797c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(uval & 1)
798c74663799493f2b1e6123c18def94295d0afab7Kenny Root		*val = -((int)(uval >> 1)) - 1;
799c74663799493f2b1e6123c18def94295d0afab7Kenny Root	else
800c74663799493f2b1e6123c18def94295d0afab7Kenny Root		*val = (int)(uval >> 1);
801c74663799493f2b1e6123c18def94295d0afab7Kenny Root
802c74663799493f2b1e6123c18def94295d0afab7Kenny Root	return true;
803c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
804c74663799493f2b1e6123c18def94295d0afab7Kenny Root
805c74663799493f2b1e6123c18def94295d0afab7Kenny Root/* this is by far the most heavily used reader call.  it ain't pretty but it's fast */
806c74663799493f2b1e6123c18def94295d0afab7Kenny Root/* a lot of the logic is copied, then adapted, from FLAC__bitreader_read_unary_unsigned() and FLAC__bitreader_read_raw_uint32() */
807c74663799493f2b1e6123c18def94295d0afab7Kenny RootFLAC__bool FLAC__bitreader_read_rice_signed_block(FLAC__BitReader *br, int vals[], unsigned nvals, unsigned parameter)
808c74663799493f2b1e6123c18def94295d0afab7Kenny Root/* OPT: possibly faster version for use with MSVC */
809c74663799493f2b1e6123c18def94295d0afab7Kenny Root#ifdef _MSC_VER
810c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
811c74663799493f2b1e6123c18def94295d0afab7Kenny Root	unsigned i;
812c74663799493f2b1e6123c18def94295d0afab7Kenny Root	unsigned uval = 0;
813c74663799493f2b1e6123c18def94295d0afab7Kenny Root	unsigned bits; /* the # of binary LSBs left to read to finish a rice codeword */
814c74663799493f2b1e6123c18def94295d0afab7Kenny Root
815c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/* try and get br->consumed_words and br->consumed_bits into register;
816c74663799493f2b1e6123c18def94295d0afab7Kenny Root	 * must remember to flush them back to *br before calling other
817c74663799493f2b1e6123c18def94295d0afab7Kenny Root	 * bitwriter functions that use them, and before returning */
818c74663799493f2b1e6123c18def94295d0afab7Kenny Root	register unsigned cwords;
819c74663799493f2b1e6123c18def94295d0afab7Kenny Root	register unsigned cbits;
820c74663799493f2b1e6123c18def94295d0afab7Kenny Root
821c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(0 != br);
822c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(0 != br->buffer);
823c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
824c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
825c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(parameter < 32);
826c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/* the above two asserts also guarantee that the binary part never straddles more that 2 words, so we don't have to loop to read it */
827c74663799493f2b1e6123c18def94295d0afab7Kenny Root
828c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(nvals == 0)
829c74663799493f2b1e6123c18def94295d0afab7Kenny Root		return true;
830c74663799493f2b1e6123c18def94295d0afab7Kenny Root
831c74663799493f2b1e6123c18def94295d0afab7Kenny Root	cbits = br->consumed_bits;
832c74663799493f2b1e6123c18def94295d0afab7Kenny Root	cwords = br->consumed_words;
833c74663799493f2b1e6123c18def94295d0afab7Kenny Root
834c74663799493f2b1e6123c18def94295d0afab7Kenny Root	while(1) {
835c74663799493f2b1e6123c18def94295d0afab7Kenny Root
836c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* read unary part */
837c74663799493f2b1e6123c18def94295d0afab7Kenny Root		while(1) {
838c74663799493f2b1e6123c18def94295d0afab7Kenny Root			while(cwords < br->words) { /* if we've not consumed up to a partial tail word... */
839c74663799493f2b1e6123c18def94295d0afab7Kenny Root				brword b = br->buffer[cwords] << cbits;
840c74663799493f2b1e6123c18def94295d0afab7Kenny Root				if(b) {
841c74663799493f2b1e6123c18def94295d0afab7Kenny Root#if 0 /* slower, probably due to bad register allocation... */ && defined FLAC__CPU_IA32 && !defined FLAC__NO_ASM && FLAC__BITS_PER_WORD == 32
842c74663799493f2b1e6123c18def94295d0afab7Kenny Root					__asm {
843c74663799493f2b1e6123c18def94295d0afab7Kenny Root						bsr eax, b
844c74663799493f2b1e6123c18def94295d0afab7Kenny Root						not eax
845c74663799493f2b1e6123c18def94295d0afab7Kenny Root						and eax, 31
846c74663799493f2b1e6123c18def94295d0afab7Kenny Root						mov i, eax
847c74663799493f2b1e6123c18def94295d0afab7Kenny Root					}
848c74663799493f2b1e6123c18def94295d0afab7Kenny Root#else
849c74663799493f2b1e6123c18def94295d0afab7Kenny Root					i = COUNT_ZERO_MSBS(b);
850c74663799493f2b1e6123c18def94295d0afab7Kenny Root#endif
851c74663799493f2b1e6123c18def94295d0afab7Kenny Root					uval += i;
852c74663799493f2b1e6123c18def94295d0afab7Kenny Root					bits = parameter;
853c74663799493f2b1e6123c18def94295d0afab7Kenny Root					i++;
854c74663799493f2b1e6123c18def94295d0afab7Kenny Root					cbits += i;
855c74663799493f2b1e6123c18def94295d0afab7Kenny Root					if(cbits == FLAC__BITS_PER_WORD) {
856c74663799493f2b1e6123c18def94295d0afab7Kenny Root						crc16_update_word_(br, br->buffer[cwords]);
857c74663799493f2b1e6123c18def94295d0afab7Kenny Root						cwords++;
858c74663799493f2b1e6123c18def94295d0afab7Kenny Root						cbits = 0;
859c74663799493f2b1e6123c18def94295d0afab7Kenny Root					}
860c74663799493f2b1e6123c18def94295d0afab7Kenny Root					goto break1;
861c74663799493f2b1e6123c18def94295d0afab7Kenny Root				}
862c74663799493f2b1e6123c18def94295d0afab7Kenny Root				else {
863c74663799493f2b1e6123c18def94295d0afab7Kenny Root					uval += FLAC__BITS_PER_WORD - cbits;
864c74663799493f2b1e6123c18def94295d0afab7Kenny Root					crc16_update_word_(br, br->buffer[cwords]);
865c74663799493f2b1e6123c18def94295d0afab7Kenny Root					cwords++;
866c74663799493f2b1e6123c18def94295d0afab7Kenny Root					cbits = 0;
867c74663799493f2b1e6123c18def94295d0afab7Kenny Root					/* didn't find stop bit yet, have to keep going... */
868c74663799493f2b1e6123c18def94295d0afab7Kenny Root				}
869c74663799493f2b1e6123c18def94295d0afab7Kenny Root			}
870c74663799493f2b1e6123c18def94295d0afab7Kenny Root			/* at this point we've eaten up all the whole words; have to try
871c74663799493f2b1e6123c18def94295d0afab7Kenny Root			 * reading through any tail bytes before calling the read callback.
872c74663799493f2b1e6123c18def94295d0afab7Kenny Root			 * this is a repeat of the above logic adjusted for the fact we
873c74663799493f2b1e6123c18def94295d0afab7Kenny Root			 * don't have a whole word.  note though if the client is feeding
874c74663799493f2b1e6123c18def94295d0afab7Kenny Root			 * us data a byte at a time (unlikely), br->consumed_bits may not
875c74663799493f2b1e6123c18def94295d0afab7Kenny Root			 * be zero.
876c74663799493f2b1e6123c18def94295d0afab7Kenny Root			 */
877c74663799493f2b1e6123c18def94295d0afab7Kenny Root			if(br->bytes) {
878c74663799493f2b1e6123c18def94295d0afab7Kenny Root				const unsigned end = br->bytes * 8;
879c74663799493f2b1e6123c18def94295d0afab7Kenny Root				brword b = (br->buffer[cwords] & (FLAC__WORD_ALL_ONES << (FLAC__BITS_PER_WORD-end))) << cbits;
880c74663799493f2b1e6123c18def94295d0afab7Kenny Root				if(b) {
881c74663799493f2b1e6123c18def94295d0afab7Kenny Root					i = COUNT_ZERO_MSBS(b);
882c74663799493f2b1e6123c18def94295d0afab7Kenny Root					uval += i;
883c74663799493f2b1e6123c18def94295d0afab7Kenny Root					bits = parameter;
884c74663799493f2b1e6123c18def94295d0afab7Kenny Root					i++;
885c74663799493f2b1e6123c18def94295d0afab7Kenny Root					cbits += i;
886c74663799493f2b1e6123c18def94295d0afab7Kenny Root					FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD);
887c74663799493f2b1e6123c18def94295d0afab7Kenny Root					goto break1;
888c74663799493f2b1e6123c18def94295d0afab7Kenny Root				}
889c74663799493f2b1e6123c18def94295d0afab7Kenny Root				else {
890c74663799493f2b1e6123c18def94295d0afab7Kenny Root					uval += end - cbits;
891c74663799493f2b1e6123c18def94295d0afab7Kenny Root					cbits += end;
892c74663799493f2b1e6123c18def94295d0afab7Kenny Root					FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD);
893c74663799493f2b1e6123c18def94295d0afab7Kenny Root					/* didn't find stop bit yet, have to keep going... */
894c74663799493f2b1e6123c18def94295d0afab7Kenny Root				}
895c74663799493f2b1e6123c18def94295d0afab7Kenny Root			}
896c74663799493f2b1e6123c18def94295d0afab7Kenny Root			/* flush registers and read; bitreader_read_from_client_() does
897c74663799493f2b1e6123c18def94295d0afab7Kenny Root			 * not touch br->consumed_bits at all but we still need to set
898c74663799493f2b1e6123c18def94295d0afab7Kenny Root			 * it in case it fails and we have to return false.
899c74663799493f2b1e6123c18def94295d0afab7Kenny Root			 */
900c74663799493f2b1e6123c18def94295d0afab7Kenny Root			br->consumed_bits = cbits;
901c74663799493f2b1e6123c18def94295d0afab7Kenny Root			br->consumed_words = cwords;
902c74663799493f2b1e6123c18def94295d0afab7Kenny Root			if(!bitreader_read_from_client_(br))
903c74663799493f2b1e6123c18def94295d0afab7Kenny Root				return false;
904c74663799493f2b1e6123c18def94295d0afab7Kenny Root			cwords = br->consumed_words;
905c74663799493f2b1e6123c18def94295d0afab7Kenny Root		}
906c74663799493f2b1e6123c18def94295d0afab7Kenny Rootbreak1:
907c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* read binary part */
908c74663799493f2b1e6123c18def94295d0afab7Kenny Root		FLAC__ASSERT(cwords <= br->words);
909c74663799493f2b1e6123c18def94295d0afab7Kenny Root
910c74663799493f2b1e6123c18def94295d0afab7Kenny Root		if(bits) {
911c74663799493f2b1e6123c18def94295d0afab7Kenny Root			while((br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits < bits) {
912c74663799493f2b1e6123c18def94295d0afab7Kenny Root				/* flush registers and read; bitreader_read_from_client_() does
913c74663799493f2b1e6123c18def94295d0afab7Kenny Root				 * not touch br->consumed_bits at all but we still need to set
914c74663799493f2b1e6123c18def94295d0afab7Kenny Root				 * it in case it fails and we have to return false.
915c74663799493f2b1e6123c18def94295d0afab7Kenny Root				 */
916c74663799493f2b1e6123c18def94295d0afab7Kenny Root				br->consumed_bits = cbits;
917c74663799493f2b1e6123c18def94295d0afab7Kenny Root				br->consumed_words = cwords;
918c74663799493f2b1e6123c18def94295d0afab7Kenny Root				if(!bitreader_read_from_client_(br))
919c74663799493f2b1e6123c18def94295d0afab7Kenny Root					return false;
920c74663799493f2b1e6123c18def94295d0afab7Kenny Root				cwords = br->consumed_words;
921c74663799493f2b1e6123c18def94295d0afab7Kenny Root			}
922c74663799493f2b1e6123c18def94295d0afab7Kenny Root			if(cwords < br->words) { /* if we've not consumed up to a partial tail word... */
923c74663799493f2b1e6123c18def94295d0afab7Kenny Root				if(cbits) {
924c74663799493f2b1e6123c18def94295d0afab7Kenny Root					/* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
925c74663799493f2b1e6123c18def94295d0afab7Kenny Root					const unsigned n = FLAC__BITS_PER_WORD - cbits;
926c74663799493f2b1e6123c18def94295d0afab7Kenny Root					const brword word = br->buffer[cwords];
927c74663799493f2b1e6123c18def94295d0afab7Kenny Root					if(bits < n) {
928c74663799493f2b1e6123c18def94295d0afab7Kenny Root						uval <<= bits;
929c74663799493f2b1e6123c18def94295d0afab7Kenny Root						uval |= (word & (FLAC__WORD_ALL_ONES >> cbits)) >> (n-bits);
930c74663799493f2b1e6123c18def94295d0afab7Kenny Root						cbits += bits;
931c74663799493f2b1e6123c18def94295d0afab7Kenny Root						goto break2;
932c74663799493f2b1e6123c18def94295d0afab7Kenny Root					}
933c74663799493f2b1e6123c18def94295d0afab7Kenny Root					uval <<= n;
934c74663799493f2b1e6123c18def94295d0afab7Kenny Root					uval |= word & (FLAC__WORD_ALL_ONES >> cbits);
935c74663799493f2b1e6123c18def94295d0afab7Kenny Root					bits -= n;
936c74663799493f2b1e6123c18def94295d0afab7Kenny Root					crc16_update_word_(br, word);
937c74663799493f2b1e6123c18def94295d0afab7Kenny Root					cwords++;
938c74663799493f2b1e6123c18def94295d0afab7Kenny Root					cbits = 0;
939c74663799493f2b1e6123c18def94295d0afab7Kenny Root					if(bits) { /* if there are still bits left to read, there have to be less than 32 so they will all be in the next word */
940c74663799493f2b1e6123c18def94295d0afab7Kenny Root						uval <<= bits;
941c74663799493f2b1e6123c18def94295d0afab7Kenny Root						uval |= (br->buffer[cwords] >> (FLAC__BITS_PER_WORD-bits));
942c74663799493f2b1e6123c18def94295d0afab7Kenny Root						cbits = bits;
943c74663799493f2b1e6123c18def94295d0afab7Kenny Root					}
944c74663799493f2b1e6123c18def94295d0afab7Kenny Root					goto break2;
945c74663799493f2b1e6123c18def94295d0afab7Kenny Root				}
946c74663799493f2b1e6123c18def94295d0afab7Kenny Root				else {
947c74663799493f2b1e6123c18def94295d0afab7Kenny Root					FLAC__ASSERT(bits < FLAC__BITS_PER_WORD);
948c74663799493f2b1e6123c18def94295d0afab7Kenny Root					uval <<= bits;
949c74663799493f2b1e6123c18def94295d0afab7Kenny Root					uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-bits);
950c74663799493f2b1e6123c18def94295d0afab7Kenny Root					cbits = bits;
951c74663799493f2b1e6123c18def94295d0afab7Kenny Root					goto break2;
952c74663799493f2b1e6123c18def94295d0afab7Kenny Root				}
953c74663799493f2b1e6123c18def94295d0afab7Kenny Root			}
954c74663799493f2b1e6123c18def94295d0afab7Kenny Root			else {
955c74663799493f2b1e6123c18def94295d0afab7Kenny Root				/* in this case we're starting our read at a partial tail word;
956c74663799493f2b1e6123c18def94295d0afab7Kenny Root				 * the reader has guaranteed that we have at least 'bits' bits
957c74663799493f2b1e6123c18def94295d0afab7Kenny Root				 * available to read, which makes this case simpler.
958c74663799493f2b1e6123c18def94295d0afab7Kenny Root				 */
959c74663799493f2b1e6123c18def94295d0afab7Kenny Root				uval <<= bits;
960c74663799493f2b1e6123c18def94295d0afab7Kenny Root				if(cbits) {
961c74663799493f2b1e6123c18def94295d0afab7Kenny Root					/* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
962c74663799493f2b1e6123c18def94295d0afab7Kenny Root					FLAC__ASSERT(cbits + bits <= br->bytes*8);
963c74663799493f2b1e6123c18def94295d0afab7Kenny Root					uval |= (br->buffer[cwords] & (FLAC__WORD_ALL_ONES >> cbits)) >> (FLAC__BITS_PER_WORD-cbits-bits);
964c74663799493f2b1e6123c18def94295d0afab7Kenny Root					cbits += bits;
965c74663799493f2b1e6123c18def94295d0afab7Kenny Root					goto break2;
966c74663799493f2b1e6123c18def94295d0afab7Kenny Root				}
967c74663799493f2b1e6123c18def94295d0afab7Kenny Root				else {
968c74663799493f2b1e6123c18def94295d0afab7Kenny Root					uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-bits);
969c74663799493f2b1e6123c18def94295d0afab7Kenny Root					cbits += bits;
970c74663799493f2b1e6123c18def94295d0afab7Kenny Root					goto break2;
971c74663799493f2b1e6123c18def94295d0afab7Kenny Root				}
972c74663799493f2b1e6123c18def94295d0afab7Kenny Root			}
973c74663799493f2b1e6123c18def94295d0afab7Kenny Root		}
974c74663799493f2b1e6123c18def94295d0afab7Kenny Rootbreak2:
975c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* compose the value */
976c74663799493f2b1e6123c18def94295d0afab7Kenny Root		*vals = (int)(uval >> 1 ^ -(int)(uval & 1));
977c74663799493f2b1e6123c18def94295d0afab7Kenny Root
978c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* are we done? */
979c74663799493f2b1e6123c18def94295d0afab7Kenny Root		--nvals;
980c74663799493f2b1e6123c18def94295d0afab7Kenny Root		if(nvals == 0) {
981c74663799493f2b1e6123c18def94295d0afab7Kenny Root			br->consumed_bits = cbits;
982c74663799493f2b1e6123c18def94295d0afab7Kenny Root			br->consumed_words = cwords;
983c74663799493f2b1e6123c18def94295d0afab7Kenny Root			return true;
984c74663799493f2b1e6123c18def94295d0afab7Kenny Root		}
985c74663799493f2b1e6123c18def94295d0afab7Kenny Root
986c74663799493f2b1e6123c18def94295d0afab7Kenny Root		uval = 0;
987c74663799493f2b1e6123c18def94295d0afab7Kenny Root		++vals;
988c74663799493f2b1e6123c18def94295d0afab7Kenny Root
989c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
990c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
991c74663799493f2b1e6123c18def94295d0afab7Kenny Root#else
992c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
993c74663799493f2b1e6123c18def94295d0afab7Kenny Root	unsigned i;
994c74663799493f2b1e6123c18def94295d0afab7Kenny Root	unsigned uval = 0;
995c74663799493f2b1e6123c18def94295d0afab7Kenny Root
996c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/* try and get br->consumed_words and br->consumed_bits into register;
997c74663799493f2b1e6123c18def94295d0afab7Kenny Root	 * must remember to flush them back to *br before calling other
998c74663799493f2b1e6123c18def94295d0afab7Kenny Root	 * bitwriter functions that use them, and before returning */
999c74663799493f2b1e6123c18def94295d0afab7Kenny Root	register unsigned cwords;
1000c74663799493f2b1e6123c18def94295d0afab7Kenny Root	register unsigned cbits;
1001c74663799493f2b1e6123c18def94295d0afab7Kenny Root	unsigned ucbits; /* keep track of the number of unconsumed bits in the buffer */
1002c74663799493f2b1e6123c18def94295d0afab7Kenny Root
1003c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(0 != br);
1004c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(0 != br->buffer);
1005c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
1006c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
1007c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(parameter < 32);
1008c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/* the above two asserts also guarantee that the binary part never straddles more than 2 words, so we don't have to loop to read it */
1009c74663799493f2b1e6123c18def94295d0afab7Kenny Root
1010c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(nvals == 0)
1011c74663799493f2b1e6123c18def94295d0afab7Kenny Root		return true;
1012c74663799493f2b1e6123c18def94295d0afab7Kenny Root
1013c74663799493f2b1e6123c18def94295d0afab7Kenny Root	cbits = br->consumed_bits;
1014c74663799493f2b1e6123c18def94295d0afab7Kenny Root	cwords = br->consumed_words;
1015c74663799493f2b1e6123c18def94295d0afab7Kenny Root	ucbits = (br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits;
1016c74663799493f2b1e6123c18def94295d0afab7Kenny Root
1017c74663799493f2b1e6123c18def94295d0afab7Kenny Root	while(1) {
1018c74663799493f2b1e6123c18def94295d0afab7Kenny Root
1019c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* read unary part */
1020c74663799493f2b1e6123c18def94295d0afab7Kenny Root		while(1) {
1021c74663799493f2b1e6123c18def94295d0afab7Kenny Root			while(cwords < br->words) { /* if we've not consumed up to a partial tail word... */
1022c74663799493f2b1e6123c18def94295d0afab7Kenny Root				brword b = br->buffer[cwords] << cbits;
1023c74663799493f2b1e6123c18def94295d0afab7Kenny Root				if(b) {
1024c74663799493f2b1e6123c18def94295d0afab7Kenny Root#if 0 /* is not discernably faster... */ && defined FLAC__CPU_IA32 && !defined FLAC__NO_ASM && FLAC__BITS_PER_WORD == 32 && defined __GNUC__
1025c74663799493f2b1e6123c18def94295d0afab7Kenny Root					asm volatile (
1026c74663799493f2b1e6123c18def94295d0afab7Kenny Root						"bsrl %1, %0;"
1027c74663799493f2b1e6123c18def94295d0afab7Kenny Root						"notl %0;"
1028c74663799493f2b1e6123c18def94295d0afab7Kenny Root						"andl $31, %0;"
1029c74663799493f2b1e6123c18def94295d0afab7Kenny Root						: "=r"(i)
1030c74663799493f2b1e6123c18def94295d0afab7Kenny Root						: "r"(b)
1031c74663799493f2b1e6123c18def94295d0afab7Kenny Root					);
1032c74663799493f2b1e6123c18def94295d0afab7Kenny Root#else
1033c74663799493f2b1e6123c18def94295d0afab7Kenny Root					i = COUNT_ZERO_MSBS(b);
1034c74663799493f2b1e6123c18def94295d0afab7Kenny Root#endif
1035c74663799493f2b1e6123c18def94295d0afab7Kenny Root					uval += i;
1036c74663799493f2b1e6123c18def94295d0afab7Kenny Root					cbits += i;
1037c74663799493f2b1e6123c18def94295d0afab7Kenny Root					cbits++; /* skip over stop bit */
1038c74663799493f2b1e6123c18def94295d0afab7Kenny Root					if(cbits >= FLAC__BITS_PER_WORD) { /* faster way of testing if(cbits == FLAC__BITS_PER_WORD) */
1039c74663799493f2b1e6123c18def94295d0afab7Kenny Root						crc16_update_word_(br, br->buffer[cwords]);
1040c74663799493f2b1e6123c18def94295d0afab7Kenny Root						cwords++;
1041c74663799493f2b1e6123c18def94295d0afab7Kenny Root						cbits = 0;
1042c74663799493f2b1e6123c18def94295d0afab7Kenny Root					}
1043c74663799493f2b1e6123c18def94295d0afab7Kenny Root					goto break1;
1044c74663799493f2b1e6123c18def94295d0afab7Kenny Root				}
1045c74663799493f2b1e6123c18def94295d0afab7Kenny Root				else {
1046c74663799493f2b1e6123c18def94295d0afab7Kenny Root					uval += FLAC__BITS_PER_WORD - cbits;
1047c74663799493f2b1e6123c18def94295d0afab7Kenny Root					crc16_update_word_(br, br->buffer[cwords]);
1048c74663799493f2b1e6123c18def94295d0afab7Kenny Root					cwords++;
1049c74663799493f2b1e6123c18def94295d0afab7Kenny Root					cbits = 0;
1050c74663799493f2b1e6123c18def94295d0afab7Kenny Root					/* didn't find stop bit yet, have to keep going... */
1051c74663799493f2b1e6123c18def94295d0afab7Kenny Root				}
1052c74663799493f2b1e6123c18def94295d0afab7Kenny Root			}
1053c74663799493f2b1e6123c18def94295d0afab7Kenny Root			/* at this point we've eaten up all the whole words; have to try
1054c74663799493f2b1e6123c18def94295d0afab7Kenny Root			 * reading through any tail bytes before calling the read callback.
1055c74663799493f2b1e6123c18def94295d0afab7Kenny Root			 * this is a repeat of the above logic adjusted for the fact we
1056c74663799493f2b1e6123c18def94295d0afab7Kenny Root			 * don't have a whole word.  note though if the client is feeding
1057c74663799493f2b1e6123c18def94295d0afab7Kenny Root			 * us data a byte at a time (unlikely), br->consumed_bits may not
1058c74663799493f2b1e6123c18def94295d0afab7Kenny Root			 * be zero.
1059c74663799493f2b1e6123c18def94295d0afab7Kenny Root			 */
1060c74663799493f2b1e6123c18def94295d0afab7Kenny Root			if(br->bytes) {
1061c74663799493f2b1e6123c18def94295d0afab7Kenny Root				const unsigned end = br->bytes * 8;
1062c74663799493f2b1e6123c18def94295d0afab7Kenny Root				brword b = (br->buffer[cwords] & ~(FLAC__WORD_ALL_ONES >> end)) << cbits;
1063c74663799493f2b1e6123c18def94295d0afab7Kenny Root				if(b) {
1064c74663799493f2b1e6123c18def94295d0afab7Kenny Root					i = COUNT_ZERO_MSBS(b);
1065c74663799493f2b1e6123c18def94295d0afab7Kenny Root					uval += i;
1066c74663799493f2b1e6123c18def94295d0afab7Kenny Root					cbits += i;
1067c74663799493f2b1e6123c18def94295d0afab7Kenny Root					cbits++; /* skip over stop bit */
1068c74663799493f2b1e6123c18def94295d0afab7Kenny Root					FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD);
1069c74663799493f2b1e6123c18def94295d0afab7Kenny Root					goto break1;
1070c74663799493f2b1e6123c18def94295d0afab7Kenny Root				}
1071c74663799493f2b1e6123c18def94295d0afab7Kenny Root				else {
1072c74663799493f2b1e6123c18def94295d0afab7Kenny Root					uval += end - cbits;
1073c74663799493f2b1e6123c18def94295d0afab7Kenny Root					cbits += end;
1074c74663799493f2b1e6123c18def94295d0afab7Kenny Root					FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD);
1075c74663799493f2b1e6123c18def94295d0afab7Kenny Root					/* didn't find stop bit yet, have to keep going... */
1076c74663799493f2b1e6123c18def94295d0afab7Kenny Root				}
1077c74663799493f2b1e6123c18def94295d0afab7Kenny Root			}
1078c74663799493f2b1e6123c18def94295d0afab7Kenny Root			/* flush registers and read; bitreader_read_from_client_() does
1079c74663799493f2b1e6123c18def94295d0afab7Kenny Root			 * not touch br->consumed_bits at all but we still need to set
1080c74663799493f2b1e6123c18def94295d0afab7Kenny Root			 * it in case it fails and we have to return false.
1081c74663799493f2b1e6123c18def94295d0afab7Kenny Root			 */
1082c74663799493f2b1e6123c18def94295d0afab7Kenny Root			br->consumed_bits = cbits;
1083c74663799493f2b1e6123c18def94295d0afab7Kenny Root			br->consumed_words = cwords;
1084c74663799493f2b1e6123c18def94295d0afab7Kenny Root			if(!bitreader_read_from_client_(br))
1085c74663799493f2b1e6123c18def94295d0afab7Kenny Root				return false;
1086c74663799493f2b1e6123c18def94295d0afab7Kenny Root			cwords = br->consumed_words;
1087c74663799493f2b1e6123c18def94295d0afab7Kenny Root			ucbits = (br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits + uval;
1088c74663799493f2b1e6123c18def94295d0afab7Kenny Root			/* + uval to offset our count by the # of unary bits already
1089c74663799493f2b1e6123c18def94295d0afab7Kenny Root			 * consumed before the read, because we will add these back
1090c74663799493f2b1e6123c18def94295d0afab7Kenny Root			 * in all at once at break1
1091c74663799493f2b1e6123c18def94295d0afab7Kenny Root			 */
1092c74663799493f2b1e6123c18def94295d0afab7Kenny Root		}
1093c74663799493f2b1e6123c18def94295d0afab7Kenny Rootbreak1:
1094c74663799493f2b1e6123c18def94295d0afab7Kenny Root		ucbits -= uval;
1095c74663799493f2b1e6123c18def94295d0afab7Kenny Root		ucbits--; /* account for stop bit */
1096c74663799493f2b1e6123c18def94295d0afab7Kenny Root
1097c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* read binary part */
1098c74663799493f2b1e6123c18def94295d0afab7Kenny Root		FLAC__ASSERT(cwords <= br->words);
1099c74663799493f2b1e6123c18def94295d0afab7Kenny Root
1100c74663799493f2b1e6123c18def94295d0afab7Kenny Root		if(parameter) {
1101c74663799493f2b1e6123c18def94295d0afab7Kenny Root			while(ucbits < parameter) {
1102c74663799493f2b1e6123c18def94295d0afab7Kenny Root				/* flush registers and read; bitreader_read_from_client_() does
1103c74663799493f2b1e6123c18def94295d0afab7Kenny Root				 * not touch br->consumed_bits at all but we still need to set
1104c74663799493f2b1e6123c18def94295d0afab7Kenny Root				 * it in case it fails and we have to return false.
1105c74663799493f2b1e6123c18def94295d0afab7Kenny Root				 */
1106c74663799493f2b1e6123c18def94295d0afab7Kenny Root				br->consumed_bits = cbits;
1107c74663799493f2b1e6123c18def94295d0afab7Kenny Root				br->consumed_words = cwords;
1108c74663799493f2b1e6123c18def94295d0afab7Kenny Root				if(!bitreader_read_from_client_(br))
1109c74663799493f2b1e6123c18def94295d0afab7Kenny Root					return false;
1110c74663799493f2b1e6123c18def94295d0afab7Kenny Root				cwords = br->consumed_words;
1111c74663799493f2b1e6123c18def94295d0afab7Kenny Root				ucbits = (br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits;
1112c74663799493f2b1e6123c18def94295d0afab7Kenny Root			}
1113c74663799493f2b1e6123c18def94295d0afab7Kenny Root			if(cwords < br->words) { /* if we've not consumed up to a partial tail word... */
1114c74663799493f2b1e6123c18def94295d0afab7Kenny Root				if(cbits) {
1115c74663799493f2b1e6123c18def94295d0afab7Kenny Root					/* this also works when consumed_bits==0, it's just slower than necessary for that case */
1116c74663799493f2b1e6123c18def94295d0afab7Kenny Root					const unsigned n = FLAC__BITS_PER_WORD - cbits;
1117c74663799493f2b1e6123c18def94295d0afab7Kenny Root					const brword word = br->buffer[cwords];
1118c74663799493f2b1e6123c18def94295d0afab7Kenny Root					if(parameter < n) {
1119c74663799493f2b1e6123c18def94295d0afab7Kenny Root						uval <<= parameter;
1120c74663799493f2b1e6123c18def94295d0afab7Kenny Root						uval |= (word & (FLAC__WORD_ALL_ONES >> cbits)) >> (n-parameter);
1121c74663799493f2b1e6123c18def94295d0afab7Kenny Root						cbits += parameter;
1122c74663799493f2b1e6123c18def94295d0afab7Kenny Root					}
1123c74663799493f2b1e6123c18def94295d0afab7Kenny Root					else {
1124c74663799493f2b1e6123c18def94295d0afab7Kenny Root						uval <<= n;
1125c74663799493f2b1e6123c18def94295d0afab7Kenny Root						uval |= word & (FLAC__WORD_ALL_ONES >> cbits);
1126c74663799493f2b1e6123c18def94295d0afab7Kenny Root						crc16_update_word_(br, word);
1127c74663799493f2b1e6123c18def94295d0afab7Kenny Root						cwords++;
1128c74663799493f2b1e6123c18def94295d0afab7Kenny Root						cbits = parameter - n;
1129c74663799493f2b1e6123c18def94295d0afab7Kenny Root						if(cbits) { /* parameter > n, i.e. if there are still bits left to read, there have to be less than 32 so they will all be in the next word */
1130c74663799493f2b1e6123c18def94295d0afab7Kenny Root							uval <<= cbits;
1131c74663799493f2b1e6123c18def94295d0afab7Kenny Root							uval |= (br->buffer[cwords] >> (FLAC__BITS_PER_WORD-cbits));
1132c74663799493f2b1e6123c18def94295d0afab7Kenny Root						}
1133c74663799493f2b1e6123c18def94295d0afab7Kenny Root					}
1134c74663799493f2b1e6123c18def94295d0afab7Kenny Root				}
1135c74663799493f2b1e6123c18def94295d0afab7Kenny Root				else {
1136c74663799493f2b1e6123c18def94295d0afab7Kenny Root					cbits = parameter;
1137c74663799493f2b1e6123c18def94295d0afab7Kenny Root					uval <<= parameter;
1138c74663799493f2b1e6123c18def94295d0afab7Kenny Root					uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-cbits);
1139c74663799493f2b1e6123c18def94295d0afab7Kenny Root				}
1140c74663799493f2b1e6123c18def94295d0afab7Kenny Root			}
1141c74663799493f2b1e6123c18def94295d0afab7Kenny Root			else {
1142c74663799493f2b1e6123c18def94295d0afab7Kenny Root				/* in this case we're starting our read at a partial tail word;
1143c74663799493f2b1e6123c18def94295d0afab7Kenny Root				 * the reader has guaranteed that we have at least 'parameter'
1144c74663799493f2b1e6123c18def94295d0afab7Kenny Root				 * bits available to read, which makes this case simpler.
1145c74663799493f2b1e6123c18def94295d0afab7Kenny Root				 */
1146c74663799493f2b1e6123c18def94295d0afab7Kenny Root				uval <<= parameter;
1147c74663799493f2b1e6123c18def94295d0afab7Kenny Root				if(cbits) {
1148c74663799493f2b1e6123c18def94295d0afab7Kenny Root					/* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
1149c74663799493f2b1e6123c18def94295d0afab7Kenny Root					FLAC__ASSERT(cbits + parameter <= br->bytes*8);
1150c74663799493f2b1e6123c18def94295d0afab7Kenny Root					uval |= (br->buffer[cwords] & (FLAC__WORD_ALL_ONES >> cbits)) >> (FLAC__BITS_PER_WORD-cbits-parameter);
1151c74663799493f2b1e6123c18def94295d0afab7Kenny Root					cbits += parameter;
1152c74663799493f2b1e6123c18def94295d0afab7Kenny Root				}
1153c74663799493f2b1e6123c18def94295d0afab7Kenny Root				else {
1154c74663799493f2b1e6123c18def94295d0afab7Kenny Root					cbits = parameter;
1155c74663799493f2b1e6123c18def94295d0afab7Kenny Root					uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-cbits);
1156c74663799493f2b1e6123c18def94295d0afab7Kenny Root				}
1157c74663799493f2b1e6123c18def94295d0afab7Kenny Root			}
1158c74663799493f2b1e6123c18def94295d0afab7Kenny Root		}
1159c74663799493f2b1e6123c18def94295d0afab7Kenny Root
1160c74663799493f2b1e6123c18def94295d0afab7Kenny Root		ucbits -= parameter;
1161c74663799493f2b1e6123c18def94295d0afab7Kenny Root
1162c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* compose the value */
1163c74663799493f2b1e6123c18def94295d0afab7Kenny Root		*vals = (int)(uval >> 1 ^ -(int)(uval & 1));
1164c74663799493f2b1e6123c18def94295d0afab7Kenny Root
1165c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* are we done? */
1166c74663799493f2b1e6123c18def94295d0afab7Kenny Root		--nvals;
1167c74663799493f2b1e6123c18def94295d0afab7Kenny Root		if(nvals == 0) {
1168c74663799493f2b1e6123c18def94295d0afab7Kenny Root			br->consumed_bits = cbits;
1169c74663799493f2b1e6123c18def94295d0afab7Kenny Root			br->consumed_words = cwords;
1170c74663799493f2b1e6123c18def94295d0afab7Kenny Root			return true;
1171c74663799493f2b1e6123c18def94295d0afab7Kenny Root		}
1172c74663799493f2b1e6123c18def94295d0afab7Kenny Root
1173c74663799493f2b1e6123c18def94295d0afab7Kenny Root		uval = 0;
1174c74663799493f2b1e6123c18def94295d0afab7Kenny Root		++vals;
1175c74663799493f2b1e6123c18def94295d0afab7Kenny Root
1176c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
1177c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
1178c74663799493f2b1e6123c18def94295d0afab7Kenny Root#endif
1179c74663799493f2b1e6123c18def94295d0afab7Kenny Root
1180c74663799493f2b1e6123c18def94295d0afab7Kenny Root#if 0 /* UNUSED */
1181c74663799493f2b1e6123c18def94295d0afab7Kenny RootFLAC__bool FLAC__bitreader_read_golomb_signed(FLAC__BitReader *br, int *val, unsigned parameter)
1182c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
1183c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__uint32 lsbs = 0, msbs = 0;
1184c74663799493f2b1e6123c18def94295d0afab7Kenny Root	unsigned bit, uval, k;
1185c74663799493f2b1e6123c18def94295d0afab7Kenny Root
1186c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(0 != br);
1187c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(0 != br->buffer);
1188c74663799493f2b1e6123c18def94295d0afab7Kenny Root
1189c74663799493f2b1e6123c18def94295d0afab7Kenny Root	k = FLAC__bitmath_ilog2(parameter);
1190c74663799493f2b1e6123c18def94295d0afab7Kenny Root
1191c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/* read the unary MSBs and end bit */
1192c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
1193c74663799493f2b1e6123c18def94295d0afab7Kenny Root		return false;
1194c74663799493f2b1e6123c18def94295d0afab7Kenny Root
1195c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/* read the binary LSBs */
1196c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, k))
1197c74663799493f2b1e6123c18def94295d0afab7Kenny Root		return false;
1198c74663799493f2b1e6123c18def94295d0afab7Kenny Root
1199c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(parameter == 1u<<k) {
1200c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* compose the value */
1201c74663799493f2b1e6123c18def94295d0afab7Kenny Root		uval = (msbs << k) | lsbs;
1202c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
1203c74663799493f2b1e6123c18def94295d0afab7Kenny Root	else {
1204c74663799493f2b1e6123c18def94295d0afab7Kenny Root		unsigned d = (1 << (k+1)) - parameter;
1205c74663799493f2b1e6123c18def94295d0afab7Kenny Root		if(lsbs >= d) {
1206c74663799493f2b1e6123c18def94295d0afab7Kenny Root			if(!FLAC__bitreader_read_bit(br, &bit))
1207c74663799493f2b1e6123c18def94295d0afab7Kenny Root				return false;
1208c74663799493f2b1e6123c18def94295d0afab7Kenny Root			lsbs <<= 1;
1209c74663799493f2b1e6123c18def94295d0afab7Kenny Root			lsbs |= bit;
1210c74663799493f2b1e6123c18def94295d0afab7Kenny Root			lsbs -= d;
1211c74663799493f2b1e6123c18def94295d0afab7Kenny Root		}
1212c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* compose the value */
1213c74663799493f2b1e6123c18def94295d0afab7Kenny Root		uval = msbs * parameter + lsbs;
1214c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
1215c74663799493f2b1e6123c18def94295d0afab7Kenny Root
1216c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/* unfold unsigned to signed */
1217c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(uval & 1)
1218c74663799493f2b1e6123c18def94295d0afab7Kenny Root		*val = -((int)(uval >> 1)) - 1;
1219c74663799493f2b1e6123c18def94295d0afab7Kenny Root	else
1220c74663799493f2b1e6123c18def94295d0afab7Kenny Root		*val = (int)(uval >> 1);
1221c74663799493f2b1e6123c18def94295d0afab7Kenny Root
1222c74663799493f2b1e6123c18def94295d0afab7Kenny Root	return true;
1223c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
1224c74663799493f2b1e6123c18def94295d0afab7Kenny Root
1225c74663799493f2b1e6123c18def94295d0afab7Kenny RootFLAC__bool FLAC__bitreader_read_golomb_unsigned(FLAC__BitReader *br, unsigned *val, unsigned parameter)
1226c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
1227c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__uint32 lsbs, msbs = 0;
1228c74663799493f2b1e6123c18def94295d0afab7Kenny Root	unsigned bit, k;
1229c74663799493f2b1e6123c18def94295d0afab7Kenny Root
1230c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(0 != br);
1231c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__ASSERT(0 != br->buffer);
1232c74663799493f2b1e6123c18def94295d0afab7Kenny Root
1233c74663799493f2b1e6123c18def94295d0afab7Kenny Root	k = FLAC__bitmath_ilog2(parameter);
1234c74663799493f2b1e6123c18def94295d0afab7Kenny Root
1235c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/* read the unary MSBs and end bit */
1236c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
1237c74663799493f2b1e6123c18def94295d0afab7Kenny Root		return false;
1238c74663799493f2b1e6123c18def94295d0afab7Kenny Root
1239c74663799493f2b1e6123c18def94295d0afab7Kenny Root	/* read the binary LSBs */
1240c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, k))
1241c74663799493f2b1e6123c18def94295d0afab7Kenny Root		return false;
1242c74663799493f2b1e6123c18def94295d0afab7Kenny Root
1243c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(parameter == 1u<<k) {
1244c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* compose the value */
1245c74663799493f2b1e6123c18def94295d0afab7Kenny Root		*val = (msbs << k) | lsbs;
1246c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
1247c74663799493f2b1e6123c18def94295d0afab7Kenny Root	else {
1248c74663799493f2b1e6123c18def94295d0afab7Kenny Root		unsigned d = (1 << (k+1)) - parameter;
1249c74663799493f2b1e6123c18def94295d0afab7Kenny Root		if(lsbs >= d) {
1250c74663799493f2b1e6123c18def94295d0afab7Kenny Root			if(!FLAC__bitreader_read_bit(br, &bit))
1251c74663799493f2b1e6123c18def94295d0afab7Kenny Root				return false;
1252c74663799493f2b1e6123c18def94295d0afab7Kenny Root			lsbs <<= 1;
1253c74663799493f2b1e6123c18def94295d0afab7Kenny Root			lsbs |= bit;
1254c74663799493f2b1e6123c18def94295d0afab7Kenny Root			lsbs -= d;
1255c74663799493f2b1e6123c18def94295d0afab7Kenny Root		}
1256c74663799493f2b1e6123c18def94295d0afab7Kenny Root		/* compose the value */
1257c74663799493f2b1e6123c18def94295d0afab7Kenny Root		*val = msbs * parameter + lsbs;
1258c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
1259c74663799493f2b1e6123c18def94295d0afab7Kenny Root
1260c74663799493f2b1e6123c18def94295d0afab7Kenny Root	return true;
1261c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
1262c74663799493f2b1e6123c18def94295d0afab7Kenny Root#endif /* UNUSED */
1263c74663799493f2b1e6123c18def94295d0afab7Kenny Root
1264c74663799493f2b1e6123c18def94295d0afab7Kenny Root/* on return, if *val == 0xffffffff then the utf-8 sequence was invalid, but the return value will be true */
1265c74663799493f2b1e6123c18def94295d0afab7Kenny RootFLAC__bool FLAC__bitreader_read_utf8_uint32(FLAC__BitReader *br, FLAC__uint32 *val, FLAC__byte *raw, unsigned *rawlen)
1266c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
1267c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__uint32 v = 0;
1268c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__uint32 x;
1269c74663799493f2b1e6123c18def94295d0afab7Kenny Root	unsigned i;
1270c74663799493f2b1e6123c18def94295d0afab7Kenny Root
1271c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
1272c74663799493f2b1e6123c18def94295d0afab7Kenny Root		return false;
1273c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(raw)
1274c74663799493f2b1e6123c18def94295d0afab7Kenny Root		raw[(*rawlen)++] = (FLAC__byte)x;
1275c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(!(x & 0x80)) { /* 0xxxxxxx */
1276c74663799493f2b1e6123c18def94295d0afab7Kenny Root		v = x;
1277c74663799493f2b1e6123c18def94295d0afab7Kenny Root		i = 0;
1278c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
1279c74663799493f2b1e6123c18def94295d0afab7Kenny Root	else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
1280c74663799493f2b1e6123c18def94295d0afab7Kenny Root		v = x & 0x1F;
1281c74663799493f2b1e6123c18def94295d0afab7Kenny Root		i = 1;
1282c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
1283c74663799493f2b1e6123c18def94295d0afab7Kenny Root	else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
1284c74663799493f2b1e6123c18def94295d0afab7Kenny Root		v = x & 0x0F;
1285c74663799493f2b1e6123c18def94295d0afab7Kenny Root		i = 2;
1286c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
1287c74663799493f2b1e6123c18def94295d0afab7Kenny Root	else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
1288c74663799493f2b1e6123c18def94295d0afab7Kenny Root		v = x & 0x07;
1289c74663799493f2b1e6123c18def94295d0afab7Kenny Root		i = 3;
1290c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
1291c74663799493f2b1e6123c18def94295d0afab7Kenny Root	else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
1292c74663799493f2b1e6123c18def94295d0afab7Kenny Root		v = x & 0x03;
1293c74663799493f2b1e6123c18def94295d0afab7Kenny Root		i = 4;
1294c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
1295c74663799493f2b1e6123c18def94295d0afab7Kenny Root	else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
1296c74663799493f2b1e6123c18def94295d0afab7Kenny Root		v = x & 0x01;
1297c74663799493f2b1e6123c18def94295d0afab7Kenny Root		i = 5;
1298c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
1299c74663799493f2b1e6123c18def94295d0afab7Kenny Root	else {
1300c74663799493f2b1e6123c18def94295d0afab7Kenny Root		*val = 0xffffffff;
1301c74663799493f2b1e6123c18def94295d0afab7Kenny Root		return true;
1302c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
1303c74663799493f2b1e6123c18def94295d0afab7Kenny Root	for( ; i; i--) {
1304c74663799493f2b1e6123c18def94295d0afab7Kenny Root		if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
1305c74663799493f2b1e6123c18def94295d0afab7Kenny Root			return false;
1306c74663799493f2b1e6123c18def94295d0afab7Kenny Root		if(raw)
1307c74663799493f2b1e6123c18def94295d0afab7Kenny Root			raw[(*rawlen)++] = (FLAC__byte)x;
1308c74663799493f2b1e6123c18def94295d0afab7Kenny Root		if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
1309c74663799493f2b1e6123c18def94295d0afab7Kenny Root			*val = 0xffffffff;
1310c74663799493f2b1e6123c18def94295d0afab7Kenny Root			return true;
1311c74663799493f2b1e6123c18def94295d0afab7Kenny Root		}
1312c74663799493f2b1e6123c18def94295d0afab7Kenny Root		v <<= 6;
1313c74663799493f2b1e6123c18def94295d0afab7Kenny Root		v |= (x & 0x3F);
1314c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
1315c74663799493f2b1e6123c18def94295d0afab7Kenny Root	*val = v;
1316c74663799493f2b1e6123c18def94295d0afab7Kenny Root	return true;
1317c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
1318c74663799493f2b1e6123c18def94295d0afab7Kenny Root
1319c74663799493f2b1e6123c18def94295d0afab7Kenny Root/* on return, if *val == 0xffffffffffffffff then the utf-8 sequence was invalid, but the return value will be true */
1320c74663799493f2b1e6123c18def94295d0afab7Kenny RootFLAC__bool FLAC__bitreader_read_utf8_uint64(FLAC__BitReader *br, FLAC__uint64 *val, FLAC__byte *raw, unsigned *rawlen)
1321c74663799493f2b1e6123c18def94295d0afab7Kenny Root{
1322c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__uint64 v = 0;
1323c74663799493f2b1e6123c18def94295d0afab7Kenny Root	FLAC__uint32 x;
1324c74663799493f2b1e6123c18def94295d0afab7Kenny Root	unsigned i;
1325c74663799493f2b1e6123c18def94295d0afab7Kenny Root
1326c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
1327c74663799493f2b1e6123c18def94295d0afab7Kenny Root		return false;
1328c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(raw)
1329c74663799493f2b1e6123c18def94295d0afab7Kenny Root		raw[(*rawlen)++] = (FLAC__byte)x;
1330c74663799493f2b1e6123c18def94295d0afab7Kenny Root	if(!(x & 0x80)) { /* 0xxxxxxx */
1331c74663799493f2b1e6123c18def94295d0afab7Kenny Root		v = x;
1332c74663799493f2b1e6123c18def94295d0afab7Kenny Root		i = 0;
1333c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
1334c74663799493f2b1e6123c18def94295d0afab7Kenny Root	else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
1335c74663799493f2b1e6123c18def94295d0afab7Kenny Root		v = x & 0x1F;
1336c74663799493f2b1e6123c18def94295d0afab7Kenny Root		i = 1;
1337c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
1338c74663799493f2b1e6123c18def94295d0afab7Kenny Root	else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
1339c74663799493f2b1e6123c18def94295d0afab7Kenny Root		v = x & 0x0F;
1340c74663799493f2b1e6123c18def94295d0afab7Kenny Root		i = 2;
1341c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
1342c74663799493f2b1e6123c18def94295d0afab7Kenny Root	else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
1343c74663799493f2b1e6123c18def94295d0afab7Kenny Root		v = x & 0x07;
1344c74663799493f2b1e6123c18def94295d0afab7Kenny Root		i = 3;
1345c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
1346c74663799493f2b1e6123c18def94295d0afab7Kenny Root	else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
1347c74663799493f2b1e6123c18def94295d0afab7Kenny Root		v = x & 0x03;
1348c74663799493f2b1e6123c18def94295d0afab7Kenny Root		i = 4;
1349c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
1350c74663799493f2b1e6123c18def94295d0afab7Kenny Root	else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
1351c74663799493f2b1e6123c18def94295d0afab7Kenny Root		v = x & 0x01;
1352c74663799493f2b1e6123c18def94295d0afab7Kenny Root		i = 5;
1353c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
1354c74663799493f2b1e6123c18def94295d0afab7Kenny Root	else if(x & 0xFE && !(x & 0x01)) { /* 11111110 */
1355c74663799493f2b1e6123c18def94295d0afab7Kenny Root		v = 0;
1356c74663799493f2b1e6123c18def94295d0afab7Kenny Root		i = 6;
1357c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
1358c74663799493f2b1e6123c18def94295d0afab7Kenny Root	else {
1359c74663799493f2b1e6123c18def94295d0afab7Kenny Root		*val = FLAC__U64L(0xffffffffffffffff);
1360c74663799493f2b1e6123c18def94295d0afab7Kenny Root		return true;
1361c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
1362c74663799493f2b1e6123c18def94295d0afab7Kenny Root	for( ; i; i--) {
1363c74663799493f2b1e6123c18def94295d0afab7Kenny Root		if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
1364c74663799493f2b1e6123c18def94295d0afab7Kenny Root			return false;
1365c74663799493f2b1e6123c18def94295d0afab7Kenny Root		if(raw)
1366c74663799493f2b1e6123c18def94295d0afab7Kenny Root			raw[(*rawlen)++] = (FLAC__byte)x;
1367c74663799493f2b1e6123c18def94295d0afab7Kenny Root		if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
1368c74663799493f2b1e6123c18def94295d0afab7Kenny Root			*val = FLAC__U64L(0xffffffffffffffff);
1369c74663799493f2b1e6123c18def94295d0afab7Kenny Root			return true;
1370c74663799493f2b1e6123c18def94295d0afab7Kenny Root		}
1371c74663799493f2b1e6123c18def94295d0afab7Kenny Root		v <<= 6;
1372c74663799493f2b1e6123c18def94295d0afab7Kenny Root		v |= (x & 0x3F);
1373c74663799493f2b1e6123c18def94295d0afab7Kenny Root	}
1374c74663799493f2b1e6123c18def94295d0afab7Kenny Root	*val = v;
1375c74663799493f2b1e6123c18def94295d0afab7Kenny Root	return true;
1376c74663799493f2b1e6123c18def94295d0afab7Kenny Root}
1377