OSDN Git Service

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