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