patch from Eric Wong, slight improvement on Miroslav's work
[flac.git] / src / libFLAC / bitbuffer.c
1 /* libFLAC - Free Lossless Audio Codec library
2  * Copyright (C) 2000,2001,2002,2003,2004  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 #include <stdlib.h> /* for malloc() */
33 #include <string.h> /* for memcpy(), memset() */
34 #include "private/bitbuffer.h"
35 #include "private/bitmath.h"
36 #include "private/crc.h"
37 #include "FLAC/assert.h"
38
39 /*
40  * Along the way you will see two versions of some functions, selected
41  * by a FLAC__NO_MANUAL_INLINING macro.  One is the simplified, more
42  * readable, and slow version, and the other is the same function
43  * where crucial parts have been manually inlined and are much faster.
44  *
45  */
46
47 /*
48  * This should be at least twice as large as the largest number of blurbs
49  * required to represent any 'number' (in any encoding) you are going to
50  * read.  With FLAC this is on the order of maybe a few hundred bits.
51  * If the buffer is smaller than that, the decoder won't be able to read
52  * in a whole number that is in a variable length encoding (e.g. Rice).
53  *
54  * The number we are actually using here is based on what would be the
55  * approximate maximum size of a verbatim frame at the default block size,
56  * for CD audio (4096 sample * 4 bytes per sample), plus some wiggle room.
57  * 32kbytes sounds reasonable.  For kicks we subtract out 64 bytes for any
58  * alignment or malloc overhead.
59  *
60  * Increase this number to decrease the number of read callbacks, at the
61  * expense of using more memory.  Or decrease for the reverse effect,
62  * keeping in mind the limit from the first paragraph.
63  */
64 static const unsigned FLAC__BITBUFFER_DEFAULT_CAPACITY = ((65536 - 64) * 8) / FLAC__BITS_PER_BLURB; /* blurbs */
65
66 static const unsigned char byte_to_unary_table[] = {
67         8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
68         3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
69         2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
70         2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
71         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
72         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
73         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
74         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
75         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
76         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
77         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
78         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
79         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
80         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
81         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
82         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
83 };
84
85 #if FLAC__BITS_PER_BLURB == 8
86 #define FLAC__BITS_PER_BLURB_LOG2 3
87 #define FLAC__BYTES_PER_BLURB 1
88 #define FLAC__BLURB_ALL_ONES ((FLAC__byte)0xff)
89 #define FLAC__BLURB_TOP_BIT_ONE ((FLAC__byte)0x80)
90 #define BLURB_BIT_TO_MASK(b) (((FLAC__blurb)'\x80') >> (b))
91 #define CRC16_UPDATE_BLURB(bb, blurb, crc) FLAC__CRC16_UPDATE((blurb), (crc));
92 #define FLAC__ALIGNED_BLURB_UNARY(blurb) (byte_to_unary_table[blurb])
93 #elif FLAC__BITS_PER_BLURB == 32
94 #define FLAC__BITS_PER_BLURB_LOG2 5
95 #define FLAC__BYTES_PER_BLURB 4
96 #define FLAC__BLURB_ALL_ONES ((FLAC__uint32)0xffffffff)
97 #define FLAC__BLURB_TOP_BIT_ONE ((FLAC__uint32)0x80000000)
98 #define BLURB_BIT_TO_MASK(b) (((FLAC__blurb)0x80000000) >> (b))
99 #define CRC16_UPDATE_BLURB(bb, blurb, crc) crc16_update_blurb((bb), (blurb));
100 #define FLAC__ALIGNED_BLURB_UNARY(blurb) ((blurb) <= 0xff ? byte_to_unary_table[blurb] + 24 : ((blurb) <= 0xffff ? byte_to_unary_table[(blurb) >> 8] + 16 : ((blurb) <= 0xffffff ? byte_to_unary_table[(blurb) >> 16] + 8 : byte_to_unary_table[(blurb) >> 24])))
101 #else
102 /* ERROR, only sizes of 8 and 32 are supported */
103 #endif
104
105 #define FLAC__BLURBS_TO_BITS(blurbs) ((blurbs) << FLAC__BITS_PER_BLURB_LOG2)
106
107 #ifdef min
108 #undef min
109 #endif
110 #define min(x,y) ((x)<(y)?(x):(y))
111 #ifdef max
112 #undef max
113 #endif
114 #define max(x,y) ((x)>(y)?(x):(y))
115
116 /* adjust for compilers that can't understand using LLU suffix for uint64_t literals */
117 #ifdef _MSC_VER
118 #define FLAC__U64L(x) x
119 #else
120 #define FLAC__U64L(x) x##LLU
121 #endif
122
123 #ifndef FLaC__INLINE
124 #define FLaC__INLINE
125 #endif
126
127 struct FLAC__BitBuffer {
128         FLAC__blurb *buffer;
129         unsigned capacity; /* in blurbs */
130         unsigned blurbs, bits;
131         unsigned total_bits; /* must always == FLAC__BITS_PER_BLURB*blurbs+bits */
132         unsigned consumed_blurbs, consumed_bits;
133         unsigned total_consumed_bits; /* must always == FLAC__BITS_PER_BLURB*consumed_blurbs+consumed_bits */
134         FLAC__uint16 read_crc16;
135 #if FLAC__BITS_PER_BLURB == 32
136         unsigned crc16_align;
137 #endif
138         FLAC__blurb save_head, save_tail;
139 };
140
141 #if FLAC__BITS_PER_BLURB == 32
142 static void crc16_update_blurb(FLAC__BitBuffer *bb, FLAC__blurb blurb)
143 {
144         if(bb->crc16_align == 0) {
145                 FLAC__CRC16_UPDATE(blurb >> 24, bb->read_crc16);
146                 FLAC__CRC16_UPDATE((blurb >> 16) & 0xff, bb->read_crc16);
147                 FLAC__CRC16_UPDATE((blurb >> 8) & 0xff, bb->read_crc16);
148                 FLAC__CRC16_UPDATE(blurb & 0xff, bb->read_crc16);
149         }
150         else if(bb->crc16_align == 8) {
151                 FLAC__CRC16_UPDATE((blurb >> 16) & 0xff, bb->read_crc16);
152                 FLAC__CRC16_UPDATE((blurb >> 8) & 0xff, bb->read_crc16);
153                 FLAC__CRC16_UPDATE(blurb & 0xff, bb->read_crc16);
154         }
155         else if(bb->crc16_align == 16) {
156                 FLAC__CRC16_UPDATE((blurb >> 8) & 0xff, bb->read_crc16);
157                 FLAC__CRC16_UPDATE(blurb & 0xff, bb->read_crc16);
158         }
159         else if(bb->crc16_align == 24) {
160                 FLAC__CRC16_UPDATE(blurb & 0xff, bb->read_crc16);
161         }
162         bb->crc16_align = 0;
163 }
164 #endif
165
166 /*
167  * WATCHOUT: The current implentation is not friendly to shrinking, i.e. it
168  * does not shift left what is consumed, it just chops off the end, whether
169  * there is unconsumed data there or not.  This is OK because currently we
170  * never shrink the buffer, but if this ever changes, we'll have to do some
171  * fixups here.
172  */
173 static FLAC__bool bitbuffer_resize_(FLAC__BitBuffer *bb, unsigned new_capacity)
174 {
175         FLAC__blurb *new_buffer;
176
177         FLAC__ASSERT(0 != bb);
178         FLAC__ASSERT(0 != bb->buffer);
179
180         if(bb->capacity == new_capacity)
181                 return true;
182
183         new_buffer = (FLAC__blurb*)calloc(new_capacity, sizeof(FLAC__blurb));
184         if(new_buffer == 0)
185                 return false;
186         memcpy(new_buffer, bb->buffer, sizeof(FLAC__blurb)*min(bb->blurbs+(bb->bits?1:0), new_capacity));
187         if(new_capacity < bb->blurbs+(bb->bits?1:0)) {
188                 bb->blurbs = new_capacity;
189                 bb->bits = 0;
190                 bb->total_bits = FLAC__BLURBS_TO_BITS(new_capacity);
191         }
192         if(new_capacity < bb->consumed_blurbs+(bb->consumed_bits?1:0)) {
193                 bb->consumed_blurbs = new_capacity;
194                 bb->consumed_bits = 0;
195                 bb->total_consumed_bits = FLAC__BLURBS_TO_BITS(new_capacity);
196         }
197         free(bb->buffer); /* we've already asserted above that (0 != bb->buffer) */
198         bb->buffer = new_buffer;
199         bb->capacity = new_capacity;
200         return true;
201 }
202
203 static FLAC__bool bitbuffer_grow_(FLAC__BitBuffer *bb, unsigned min_blurbs_to_add)
204 {
205         unsigned new_capacity;
206
207         FLAC__ASSERT(min_blurbs_to_add > 0);
208
209         new_capacity = max(bb->capacity * 2, bb->capacity + min_blurbs_to_add);
210         return bitbuffer_resize_(bb, new_capacity);
211 }
212
213 static FLAC__bool bitbuffer_ensure_size_(FLAC__BitBuffer *bb, unsigned bits_to_add)
214 {
215         FLAC__ASSERT(0 != bb);
216         FLAC__ASSERT(0 != bb->buffer);
217
218         if(FLAC__BLURBS_TO_BITS(bb->capacity) < bb->total_bits + bits_to_add)
219                 return bitbuffer_grow_(bb, (bits_to_add >> FLAC__BITS_PER_BLURB_LOG2) + 2);
220         else
221                 return true;
222 }
223
224 static FLAC__bool bitbuffer_read_from_client_(FLAC__BitBuffer *bb, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data)
225 {
226         unsigned bytes;
227         FLAC__byte *target;
228
229         /* first shift the unconsumed buffer data toward the front as much as possible */
230         if(bb->total_consumed_bits >= FLAC__BITS_PER_BLURB) {
231                 unsigned l = 0, r = bb->consumed_blurbs, r_end = bb->blurbs + (bb->bits? 1:0);
232                 for( ; r < r_end; l++, r++)
233                         bb->buffer[l] = bb->buffer[r];
234                 for( ; l < r_end; l++)
235                         bb->buffer[l] = 0;
236                 bb->blurbs -= bb->consumed_blurbs;
237                 bb->total_bits -= FLAC__BLURBS_TO_BITS(bb->consumed_blurbs);
238                 bb->consumed_blurbs = 0;
239                 bb->total_consumed_bits = bb->consumed_bits;
240         }
241
242         /* grow if we need to */
243         if(bb->capacity <= 1) {
244                 if(!bitbuffer_resize_(bb, 16))
245                         return false;
246         }
247
248         /* set the target for reading, taking into account blurb alignment */
249 #if FLAC__BITS_PER_BLURB == 8
250         /* blurb == byte, so no gyrations necessary: */
251         target = bb->buffer + bb->blurbs;
252         bytes = bb->capacity - bb->blurbs;
253 #elif FLAC__BITS_PER_BLURB == 32
254         /* @@@ WATCHOUT: code currently only works for big-endian: */
255         FLAC__ASSERT((bb->bits & 7) == 0);
256         target = (FLAC__byte*)(bb->buffer + bb->blurbs) + (bb->bits >> 3);
257         bytes = ((bb->capacity - bb->blurbs) << 2) - (bb->bits >> 3); /* i.e. (bb->capacity - bb->blurbs) * FLAC__BYTES_PER_BLURB - (bb->bits / 8) */
258 #else
259         FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
260 #endif
261
262         /* finally, read in some data */
263         if(!read_callback(target, &bytes, client_data))
264                 return false;
265
266         /* now we have to handle partial blurb cases: */
267 #if FLAC__BITS_PER_BLURB == 8
268         /* blurb == byte, so no gyrations necessary: */
269         bb->blurbs += bytes;
270         bb->total_bits += FLAC__BLURBS_TO_BITS(bytes);
271 #elif FLAC__BITS_PER_BLURB == 32
272         /* @@@ WATCHOUT: code currently only works for big-endian: */
273         {
274                 const unsigned aligned_bytes = (bb->bits >> 3) + bytes;
275                 bb->blurbs += (aligned_bytes >> 2); /* i.e. aligned_bytes / FLAC__BYTES_PER_BLURB */
276                 bb->bits = (aligned_bytes & 3u) << 3; /* i.e. (aligned_bytes % FLAC__BYTES_PER_BLURB) * 8 */
277                 bb->total_bits += (bytes << 3);
278         }
279 #else
280         FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
281 #endif
282         return true;
283 }
284
285 /***********************************************************************
286  *
287  * Class constructor/destructor
288  *
289  ***********************************************************************/
290
291 FLAC__BitBuffer *FLAC__bitbuffer_new()
292 {
293         FLAC__BitBuffer *bb = (FLAC__BitBuffer*)calloc(1, sizeof(FLAC__BitBuffer));
294
295         /* calloc() implies:
296                 memset(bb, 0, sizeof(FLAC__BitBuffer));
297                 bb->buffer = 0;
298                 bb->capacity = 0;
299                 bb->blurbs = bb->bits = bb->total_bits = 0;
300                 bb->consumed_blurbs = bb->consumed_bits = bb->total_consumed_bits = 0;
301         */
302         return bb;
303 }
304
305 void FLAC__bitbuffer_delete(FLAC__BitBuffer *bb)
306 {
307         FLAC__ASSERT(0 != bb);
308
309         FLAC__bitbuffer_free(bb);
310         free(bb);
311 }
312
313 /***********************************************************************
314  *
315  * Public class methods
316  *
317  ***********************************************************************/
318
319 FLAC__bool FLAC__bitbuffer_init(FLAC__BitBuffer *bb)
320 {
321         FLAC__ASSERT(0 != bb);
322
323         bb->buffer = 0;
324         bb->capacity = 0;
325         bb->blurbs = bb->bits = bb->total_bits = 0;
326         bb->consumed_blurbs = bb->consumed_bits = bb->total_consumed_bits = 0;
327
328         return FLAC__bitbuffer_clear(bb);
329 }
330
331 FLAC__bool FLAC__bitbuffer_init_from(FLAC__BitBuffer *bb, const FLAC__byte buffer[], unsigned bytes)
332 {
333         FLAC__ASSERT(0 != bb);
334         FLAC__ASSERT(bytes > 0);
335
336         if(!FLAC__bitbuffer_init(bb))
337                 return false;
338
339         if(!bitbuffer_ensure_size_(bb, bytes << 3))
340                 return false;
341
342         FLAC__ASSERT(0 != buffer);
343         /* @@@ WATCHOUT: code currently only works for 8-bits-per-blurb inclusive-or big-endian: */
344         memcpy((FLAC__byte*)bb->buffer, buffer, sizeof(FLAC__byte)*bytes);
345         bb->blurbs = bytes / FLAC__BYTES_PER_BLURB;
346         bb->bits = (bytes % FLAC__BYTES_PER_BLURB) << 3;
347         bb->total_bits = bytes << 3;
348         return true;
349 }
350
351 FLAC__bool FLAC__bitbuffer_concatenate_aligned(FLAC__BitBuffer *dest, const FLAC__BitBuffer *src)
352 {
353         unsigned bits_to_add = src->total_bits - src->total_consumed_bits;
354
355         FLAC__ASSERT(0 != dest);
356         FLAC__ASSERT(0 != src);
357
358         if(bits_to_add == 0)
359                 return true;
360         if(dest->bits != src->consumed_bits)
361                 return false;
362         if(!bitbuffer_ensure_size_(dest, bits_to_add))
363                 return false;
364         if(dest->bits == 0) {
365                 memcpy(dest->buffer+dest->blurbs, src->buffer+src->consumed_blurbs, sizeof(FLAC__blurb)*(src->blurbs-src->consumed_blurbs + ((src->bits)? 1:0)));
366         }
367         else if(dest->bits + bits_to_add > FLAC__BITS_PER_BLURB) {
368                 dest->buffer[dest->blurbs] <<= (FLAC__BITS_PER_BLURB - dest->bits);
369                 dest->buffer[dest->blurbs] |= (src->buffer[src->consumed_blurbs] & ((1u << (FLAC__BITS_PER_BLURB-dest->bits)) - 1));
370                 memcpy(dest->buffer+dest->blurbs+1, src->buffer+src->consumed_blurbs+1, sizeof(FLAC__blurb)*(src->blurbs-src->consumed_blurbs-1 + ((src->bits)? 1:0)));
371         }
372         else {
373                 dest->buffer[dest->blurbs] <<= bits_to_add;
374                 dest->buffer[dest->blurbs] |= (src->buffer[src->consumed_blurbs] & ((1u << bits_to_add) - 1));
375         }
376         dest->bits = src->bits;
377         dest->total_bits += bits_to_add;
378         dest->blurbs = dest->total_bits / FLAC__BITS_PER_BLURB;
379
380         return true;
381 }
382
383 void FLAC__bitbuffer_free(FLAC__BitBuffer *bb)
384 {
385         FLAC__ASSERT(0 != bb);
386
387         if(0 != bb->buffer)
388                 free(bb->buffer);
389         bb->buffer = 0;
390         bb->capacity = 0;
391         bb->blurbs = bb->bits = bb->total_bits = 0;
392         bb->consumed_blurbs = bb->consumed_bits = bb->total_consumed_bits = 0;
393 }
394
395 FLAC__bool FLAC__bitbuffer_clear(FLAC__BitBuffer *bb)
396 {
397         if(bb->buffer == 0) {
398                 bb->capacity = FLAC__BITBUFFER_DEFAULT_CAPACITY;
399                 bb->buffer = (FLAC__blurb*)calloc(bb->capacity, sizeof(FLAC__blurb));
400                 if(bb->buffer == 0)
401                         return false;
402         }
403         else {
404                 memset(bb->buffer, 0, bb->blurbs + (bb->bits?1:0));
405         }
406         bb->blurbs = bb->bits = bb->total_bits = 0;
407         bb->consumed_blurbs = bb->consumed_bits = bb->total_consumed_bits = 0;
408         return true;
409 }
410
411 FLAC__bool FLAC__bitbuffer_clone(FLAC__BitBuffer *dest, const FLAC__BitBuffer *src)
412 {
413         FLAC__ASSERT(0 != dest);
414         FLAC__ASSERT(0 != dest->buffer);
415         FLAC__ASSERT(0 != src);
416         FLAC__ASSERT(0 != src->buffer);
417
418         if(dest->capacity < src->capacity)
419                 if(!bitbuffer_resize_(dest, src->capacity))
420                         return false;
421         memcpy(dest->buffer, src->buffer, sizeof(FLAC__blurb)*min(src->capacity, src->blurbs+1));
422         dest->blurbs = src->blurbs;
423         dest->bits = src->bits;
424         dest->total_bits = src->total_bits;
425         dest->consumed_blurbs = src->consumed_blurbs;
426         dest->consumed_bits = src->consumed_bits;
427         dest->total_consumed_bits = src->total_consumed_bits;
428         dest->read_crc16 = src->read_crc16;
429         return true;
430 }
431
432 void FLAC__bitbuffer_reset_read_crc16(FLAC__BitBuffer *bb, FLAC__uint16 seed)
433 {
434         FLAC__ASSERT(0 != bb);
435         FLAC__ASSERT(0 != bb->buffer);
436         FLAC__ASSERT((bb->consumed_bits & 7) == 0);
437
438         bb->read_crc16 = seed;
439 #if FLAC__BITS_PER_BLURB == 8
440         /* no need to do anything */
441 #elif FLAC__BITS_PER_BLURB == 32
442         bb->crc16_align = bb->consumed_bits;
443 #else
444         FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
445 #endif
446 }
447
448 FLAC__uint16 FLAC__bitbuffer_get_read_crc16(FLAC__BitBuffer *bb)
449 {
450         FLAC__ASSERT(0 != bb);
451         FLAC__ASSERT(0 != bb->buffer);
452         FLAC__ASSERT((bb->bits & 7) == 0);
453         FLAC__ASSERT((bb->consumed_bits & 7) == 0);
454
455 #if FLAC__BITS_PER_BLURB == 8
456         /* no need to do anything */
457 #elif FLAC__BITS_PER_BLURB == 32
458         /*@@@ BUG: even though this probably can't happen with FLAC, need to fix the case where we are called here for the very first blurb and crc16_align is > 0 */
459         if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) {
460                 if(bb->consumed_bits == 8) {
461                         const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
462                         FLAC__CRC16_UPDATE(blurb >> 24, bb->read_crc16);
463                 }
464                 else if(bb->consumed_bits == 16) {
465                         const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
466                         FLAC__CRC16_UPDATE(blurb >> 24, bb->read_crc16);
467                         FLAC__CRC16_UPDATE((blurb >> 16) & 0xff, bb->read_crc16);
468                 }
469                 else if(bb->consumed_bits == 24) {
470                         const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
471                         FLAC__CRC16_UPDATE(blurb >> 24, bb->read_crc16);
472                         FLAC__CRC16_UPDATE((blurb >> 16) & 0xff, bb->read_crc16);
473                         FLAC__CRC16_UPDATE((blurb >> 8) & 0xff, bb->read_crc16);
474                 }
475         }
476         else {
477                 if(bb->consumed_bits == 8) {
478                         const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
479                         FLAC__CRC16_UPDATE(blurb >> (bb->bits-8), bb->read_crc16);
480                 }
481                 else if(bb->consumed_bits == 16) {
482                         const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
483                         FLAC__CRC16_UPDATE(blurb >> (bb->bits-8), bb->read_crc16);
484                         FLAC__CRC16_UPDATE((blurb >> (bb->bits-16)) & 0xff, bb->read_crc16);
485                 }
486                 else if(bb->consumed_bits == 24) {
487                         const FLAC__blurb blurb = bb->buffer[bb->consumed_blurbs];
488                         FLAC__CRC16_UPDATE(blurb >> (bb->bits-8), bb->read_crc16);
489                         FLAC__CRC16_UPDATE((blurb >> (bb->bits-16)) & 0xff, bb->read_crc16);
490                         FLAC__CRC16_UPDATE((blurb >> (bb->bits-24)) & 0xff, bb->read_crc16);
491                 }
492         }
493         bb->crc16_align = bb->consumed_bits;
494 #else
495         FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
496 #endif
497         return bb->read_crc16;
498 }
499
500 FLAC__uint16 FLAC__bitbuffer_get_write_crc16(const FLAC__BitBuffer *bb)
501 {
502         FLAC__ASSERT((bb->bits & 7) == 0); /* assert that we're byte-aligned */
503
504 #if FLAC__BITS_PER_BLURB == 8
505         return FLAC__crc16(bb->buffer, bb->blurbs);
506 #elif FLAC__BITS_PER_BLURB == 32
507         /* @@@ WATCHOUT: code currently only works for big-endian: */
508         return FLAC__crc16((FLAC__byte*)(bb->buffer), (bb->blurbs * FLAC__BYTES_PER_BLURB) + (bb->bits >> 3));
509 #else
510         FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
511 #endif
512 }
513
514 FLAC__byte FLAC__bitbuffer_get_write_crc8(const FLAC__BitBuffer *bb)
515 {
516         FLAC__ASSERT(0 != bb);
517         FLAC__ASSERT((bb->bits & 7) == 0); /* assert that we're byte-aligned */
518         FLAC__ASSERT(bb->buffer[0] == 0xff); /* MAGIC NUMBER for the first byte of the sync code */
519 #if FLAC__BITS_PER_BLURB == 8
520         return FLAC__crc8(bb->buffer, bb->blurbs);
521 #elif FLAC__BITS_PER_BLURB == 32
522         /* @@@ WATCHOUT: code currently only works for big-endian: */
523         return FLAC__crc8((FLAC__byte*)(bb->buffer), (bb->blurbs * FLAC__BYTES_PER_BLURB) + (bb->bits >> 3));
524 #else
525         FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
526 #endif
527 }
528
529 FLAC__bool FLAC__bitbuffer_is_byte_aligned(const FLAC__BitBuffer *bb)
530 {
531         return ((bb->bits & 7) == 0);
532 }
533
534 FLAC__bool FLAC__bitbuffer_is_consumed_byte_aligned(const FLAC__BitBuffer *bb)
535 {
536         return ((bb->consumed_bits & 7) == 0);
537 }
538
539 unsigned FLAC__bitbuffer_bits_left_for_byte_alignment(const FLAC__BitBuffer *bb)
540 {
541         return 8 - (bb->consumed_bits & 7);
542 }
543
544 unsigned FLAC__bitbuffer_get_input_bytes_unconsumed(const FLAC__BitBuffer *bb)
545 {
546         FLAC__ASSERT((bb->consumed_bits & 7) == 0 && (bb->bits & 7) == 0);
547         return (bb->total_bits - bb->total_consumed_bits) >> 3;
548 }
549
550 void FLAC__bitbuffer_get_buffer(FLAC__BitBuffer *bb, const FLAC__byte **buffer, unsigned *bytes)
551 {
552         FLAC__ASSERT((bb->consumed_bits & 7) == 0 && (bb->bits & 7) == 0);
553 #if FLAC__BITS_PER_BLURB == 8
554         *buffer = bb->buffer + bb->consumed_blurbs;
555         *bytes = bb->blurbs - bb->consumed_blurbs;
556 #elif FLAC__BITS_PER_BLURB == 32
557         /* @@@ WATCHOUT: code currently only works for big-endian: */
558         *buffer = (FLAC__byte*)(bb->buffer + bb->consumed_blurbs) + (bb->consumed_bits >> 3);
559         *bytes = (bb->total_bits - bb->total_consumed_bits) >> 3;
560 #else
561         FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
562 #endif
563 }
564
565 void FLAC__bitbuffer_release_buffer(FLAC__BitBuffer *bb)
566 {
567 #if FLAC__BITS_PER_BLURB == 8
568         (void)bb;
569 #elif FLAC__BITS_PER_BLURB == 32
570         /* @@@ WATCHOUT: code currently only works for big-endian: */
571         (void)bb;
572 #else
573         FLAC__ASSERT(false); /* ERROR, only sizes of 8 and 32 are supported */
574 #endif
575 }
576
577 FLAC__bool FLAC__bitbuffer_write_zeroes(FLAC__BitBuffer *bb, unsigned bits)
578 {
579         unsigned n;
580
581         FLAC__ASSERT(0 != bb);
582         FLAC__ASSERT(0 != bb->buffer);
583
584         if(bits == 0)
585                 return true;
586         if(!bitbuffer_ensure_size_(bb, bits))
587                 return false;
588         bb->total_bits += bits;
589         while(bits > 0) {
590                 n = min(FLAC__BITS_PER_BLURB - bb->bits, bits);
591                 bb->buffer[bb->blurbs] <<= n;
592                 bits -= n;
593                 bb->bits += n;
594                 if(bb->bits == FLAC__BITS_PER_BLURB) {
595                         bb->blurbs++;
596                         bb->bits = 0;
597                 }
598         }
599         return true;
600 }
601
602 FLaC__INLINE FLAC__bool FLAC__bitbuffer_write_raw_uint32(FLAC__BitBuffer *bb, FLAC__uint32 val, unsigned bits)
603 {
604         unsigned n, k;
605
606         FLAC__ASSERT(0 != bb);
607         FLAC__ASSERT(0 != bb->buffer);
608
609         FLAC__ASSERT(bits <= 32);
610         if(bits == 0)
611                 return true;
612         /* inline the size check so we don't incure a function call unnecessarily */
613         if(FLAC__BLURBS_TO_BITS(bb->capacity) < bb->total_bits + bits) {
614                 if(!bitbuffer_ensure_size_(bb, bits))
615                         return false;
616         }
617
618         /* zero-out unused bits; WATCHOUT: other code relies on this, so this needs to stay */
619         if(bits < 32) /* @@@ gcc seems to require this because the following line causes incorrect results when bits==32; investigate */
620                 val &= (~(0xffffffff << bits)); /* zero-out unused bits */
621
622         bb->total_bits += bits;
623         while(bits > 0) {
624                 n = FLAC__BITS_PER_BLURB - bb->bits;
625                 if(n == FLAC__BITS_PER_BLURB) { /* i.e. bb->bits == 0 */
626                         if(bits < FLAC__BITS_PER_BLURB) {
627                                 bb->buffer[bb->blurbs] = (FLAC__blurb)val;
628                                 bb->bits = bits;
629                                 break;
630                         }
631                         else if(bits == FLAC__BITS_PER_BLURB) {
632                                 bb->buffer[bb->blurbs++] = (FLAC__blurb)val;
633                                 break;
634                         }
635                         else {
636                                 k = bits - FLAC__BITS_PER_BLURB;
637                                 bb->buffer[bb->blurbs++] = (FLAC__blurb)(val >> k);
638                                 /* we know k < 32 so no need to protect against the gcc bug mentioned above */
639                                 val &= (~(0xffffffff << k));
640                                 bits -= FLAC__BITS_PER_BLURB;
641                         }
642                 }
643                 else if(bits <= n) {
644                         bb->buffer[bb->blurbs] <<= bits;
645                         bb->buffer[bb->blurbs] |= val;
646                         if(bits == n) {
647                                 bb->blurbs++;
648                                 bb->bits = 0;
649                         }
650                         else
651                                 bb->bits += bits;
652                         break;
653                 }
654                 else {
655                         k = bits - n;
656                         bb->buffer[bb->blurbs] <<= n;
657                         bb->buffer[bb->blurbs] |= (val >> k);
658                         /* we know n > 0 so k < 32 so no need to protect against the gcc bug mentioned above */
659                         val &= (~(0xffffffff << k));
660                         bits -= n;
661                         bb->blurbs++;
662                         bb->bits = 0;
663                 }
664         }
665
666         return true;
667 }
668
669 FLAC__bool FLAC__bitbuffer_write_raw_int32(FLAC__BitBuffer *bb, FLAC__int32 val, unsigned bits)
670 {
671         return FLAC__bitbuffer_write_raw_uint32(bb, (FLAC__uint32)val, bits);
672 }
673
674 FLAC__bool FLAC__bitbuffer_write_raw_uint64(FLAC__BitBuffer *bb, FLAC__uint64 val, unsigned bits)
675 {
676         static const FLAC__uint64 mask[] = {
677                 0,
678                 FLAC__U64L(0x0000000000000001), FLAC__U64L(0x0000000000000003), FLAC__U64L(0x0000000000000007), FLAC__U64L(0x000000000000000F),
679                 FLAC__U64L(0x000000000000001F), FLAC__U64L(0x000000000000003F), FLAC__U64L(0x000000000000007F), FLAC__U64L(0x00000000000000FF),
680                 FLAC__U64L(0x00000000000001FF), FLAC__U64L(0x00000000000003FF), FLAC__U64L(0x00000000000007FF), FLAC__U64L(0x0000000000000FFF),
681                 FLAC__U64L(0x0000000000001FFF), FLAC__U64L(0x0000000000003FFF), FLAC__U64L(0x0000000000007FFF), FLAC__U64L(0x000000000000FFFF),
682                 FLAC__U64L(0x000000000001FFFF), FLAC__U64L(0x000000000003FFFF), FLAC__U64L(0x000000000007FFFF), FLAC__U64L(0x00000000000FFFFF),
683                 FLAC__U64L(0x00000000001FFFFF), FLAC__U64L(0x00000000003FFFFF), FLAC__U64L(0x00000000007FFFFF), FLAC__U64L(0x0000000000FFFFFF),
684                 FLAC__U64L(0x0000000001FFFFFF), FLAC__U64L(0x0000000003FFFFFF), FLAC__U64L(0x0000000007FFFFFF), FLAC__U64L(0x000000000FFFFFFF),
685                 FLAC__U64L(0x000000001FFFFFFF), FLAC__U64L(0x000000003FFFFFFF), FLAC__U64L(0x000000007FFFFFFF), FLAC__U64L(0x00000000FFFFFFFF),
686                 FLAC__U64L(0x00000001FFFFFFFF), FLAC__U64L(0x00000003FFFFFFFF), FLAC__U64L(0x00000007FFFFFFFF), FLAC__U64L(0x0000000FFFFFFFFF),
687                 FLAC__U64L(0x0000001FFFFFFFFF), FLAC__U64L(0x0000003FFFFFFFFF), FLAC__U64L(0x0000007FFFFFFFFF), FLAC__U64L(0x000000FFFFFFFFFF),
688                 FLAC__U64L(0x000001FFFFFFFFFF), FLAC__U64L(0x000003FFFFFFFFFF), FLAC__U64L(0x000007FFFFFFFFFF), FLAC__U64L(0x00000FFFFFFFFFFF),
689                 FLAC__U64L(0x00001FFFFFFFFFFF), FLAC__U64L(0x00003FFFFFFFFFFF), FLAC__U64L(0x00007FFFFFFFFFFF), FLAC__U64L(0x0000FFFFFFFFFFFF),
690                 FLAC__U64L(0x0001FFFFFFFFFFFF), FLAC__U64L(0x0003FFFFFFFFFFFF), FLAC__U64L(0x0007FFFFFFFFFFFF), FLAC__U64L(0x000FFFFFFFFFFFFF),
691                 FLAC__U64L(0x001FFFFFFFFFFFFF), FLAC__U64L(0x003FFFFFFFFFFFFF), FLAC__U64L(0x007FFFFFFFFFFFFF), FLAC__U64L(0x00FFFFFFFFFFFFFF),
692                 FLAC__U64L(0x01FFFFFFFFFFFFFF), FLAC__U64L(0x03FFFFFFFFFFFFFF), FLAC__U64L(0x07FFFFFFFFFFFFFF), FLAC__U64L(0x0FFFFFFFFFFFFFFF),
693                 FLAC__U64L(0x1FFFFFFFFFFFFFFF), FLAC__U64L(0x3FFFFFFFFFFFFFFF), FLAC__U64L(0x7FFFFFFFFFFFFFFF), FLAC__U64L(0xFFFFFFFFFFFFFFFF)
694         };
695         unsigned n, k;
696
697         FLAC__ASSERT(0 != bb);
698         FLAC__ASSERT(0 != bb->buffer);
699
700         FLAC__ASSERT(bits <= 64);
701         if(bits == 0)
702                 return true;
703         if(!bitbuffer_ensure_size_(bb, bits))
704                 return false;
705         val &= mask[bits];
706         bb->total_bits += bits;
707         while(bits > 0) {
708                 if(bb->bits == 0) {
709                         if(bits < FLAC__BITS_PER_BLURB) {
710                                 bb->buffer[bb->blurbs] = (FLAC__blurb)val;
711                                 bb->bits = bits;
712                                 break;
713                         }
714                         else if(bits == FLAC__BITS_PER_BLURB) {
715                                 bb->buffer[bb->blurbs++] = (FLAC__blurb)val;
716                                 break;
717                         }
718                         else {
719                                 k = bits - FLAC__BITS_PER_BLURB;
720                                 bb->buffer[bb->blurbs++] = (FLAC__blurb)(val >> k);
721                                 /* we know k < 64 so no need to protect against the gcc bug mentioned above */
722                                 val &= (~(FLAC__U64L(0xffffffffffffffff) << k));
723                                 bits -= FLAC__BITS_PER_BLURB;
724                         }
725                 }
726                 else {
727                         n = min(FLAC__BITS_PER_BLURB - bb->bits, bits);
728                         k = bits - n;
729                         bb->buffer[bb->blurbs] <<= n;
730                         bb->buffer[bb->blurbs] |= (val >> k);
731                         /* we know n > 0 so k < 64 so no need to protect against the gcc bug mentioned above */
732                         val &= (~(FLAC__U64L(0xffffffffffffffff) << k));
733                         bits -= n;
734                         bb->bits += n;
735                         if(bb->bits == FLAC__BITS_PER_BLURB) {
736                                 bb->blurbs++;
737                                 bb->bits = 0;
738                         }
739                 }
740         }
741
742         return true;
743 }
744
745 #if 0 /* UNUSED */
746 FLAC__bool FLAC__bitbuffer_write_raw_int64(FLAC__BitBuffer *bb, FLAC__int64 val, unsigned bits)
747 {
748         return FLAC__bitbuffer_write_raw_uint64(bb, (FLAC__uint64)val, bits);
749 }
750 #endif
751
752 FLaC__INLINE FLAC__bool FLAC__bitbuffer_write_raw_uint32_little_endian(FLAC__BitBuffer *bb, FLAC__uint32 val)
753 {
754         /* this doesn't need to be that fast as currently it is only used for vorbis comments */
755
756         /* NOTE: we rely on the fact that FLAC__bitbuffer_write_raw_uint32() masks out the unused bits */
757         if(!FLAC__bitbuffer_write_raw_uint32(bb, val, 8))
758                 return false;
759         if(!FLAC__bitbuffer_write_raw_uint32(bb, val>>8, 8))
760                 return false;
761         if(!FLAC__bitbuffer_write_raw_uint32(bb, val>>16, 8))
762                 return false;
763         if(!FLAC__bitbuffer_write_raw_uint32(bb, val>>24, 8))
764                 return false;
765
766         return true;
767 }
768
769 FLaC__INLINE FLAC__bool FLAC__bitbuffer_write_byte_block(FLAC__BitBuffer *bb, const FLAC__byte vals[], unsigned nvals)
770 {
771         unsigned i;
772
773         /* this could be faster but currently we don't need it to be */
774         for(i = 0; i < nvals; i++) {
775                 if(!FLAC__bitbuffer_write_raw_uint32(bb, (FLAC__uint32)(vals[i]), 8))
776                         return false;
777         }
778
779         return true;
780 }
781
782 FLAC__bool FLAC__bitbuffer_write_unary_unsigned(FLAC__BitBuffer *bb, unsigned val)
783 {
784         if(val < 32)
785                 return FLAC__bitbuffer_write_raw_uint32(bb, 1, ++val);
786         else if(val < 64)
787                 return FLAC__bitbuffer_write_raw_uint64(bb, 1, ++val);
788         else {
789                 if(!FLAC__bitbuffer_write_zeroes(bb, val))
790                         return false;
791                 return FLAC__bitbuffer_write_raw_uint32(bb, 1, 1);
792         }
793 }
794
795 unsigned FLAC__bitbuffer_rice_bits(int val, unsigned parameter)
796 {
797         unsigned msbs, uval;
798
799         /* fold signed to unsigned */
800         if(val < 0)
801                 /* equivalent to
802                  *     (unsigned)(((--val) << 1) - 1);
803                  * but without the overflow problem at MININT
804                  */
805                 uval = (unsigned)(((-(++val)) << 1) + 1);
806         else
807                 uval = (unsigned)(val << 1);
808
809         msbs = uval >> parameter;
810
811         return 1 + parameter + msbs;
812 }
813
814 #if 0 /* UNUSED */
815 unsigned FLAC__bitbuffer_golomb_bits_signed(int val, unsigned parameter)
816 {
817         unsigned bits, msbs, uval;
818         unsigned k;
819
820         FLAC__ASSERT(parameter > 0);
821
822         /* fold signed to unsigned */
823         if(val < 0)
824                 /* equivalent to
825                  *     (unsigned)(((--val) << 1) - 1);
826                  * but without the overflow problem at MININT
827                  */
828                 uval = (unsigned)(((-(++val)) << 1) + 1);
829         else
830                 uval = (unsigned)(val << 1);
831
832         k = FLAC__bitmath_ilog2(parameter);
833         if(parameter == 1u<<k) {
834                 FLAC__ASSERT(k <= 30);
835
836                 msbs = uval >> k;
837                 bits = 1 + k + msbs;
838         }
839         else {
840                 unsigned q, r, d;
841
842                 d = (1 << (k+1)) - parameter;
843                 q = uval / parameter;
844                 r = uval - (q * parameter);
845
846                 bits = 1 + q + k;
847                 if(r >= d)
848                         bits++;
849         }
850         return bits;
851 }
852
853 unsigned FLAC__bitbuffer_golomb_bits_unsigned(unsigned uval, unsigned parameter)
854 {
855         unsigned bits, msbs;
856         unsigned k;
857
858         FLAC__ASSERT(parameter > 0);
859
860         k = FLAC__bitmath_ilog2(parameter);
861         if(parameter == 1u<<k) {
862                 FLAC__ASSERT(k <= 30);
863
864                 msbs = uval >> k;
865                 bits = 1 + k + msbs;
866         }
867         else {
868                 unsigned q, r, d;
869
870                 d = (1 << (k+1)) - parameter;
871                 q = uval / parameter;
872                 r = uval - (q * parameter);
873
874                 bits = 1 + q + k;
875                 if(r >= d)
876                         bits++;
877         }
878         return bits;
879 }
880 #endif /* UNUSED */
881
882 #ifdef FLAC__SYMMETRIC_RICE
883 FLAC__bool FLAC__bitbuffer_write_symmetric_rice_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
884 {
885         unsigned total_bits, interesting_bits, msbs;
886         FLAC__uint32 pattern;
887
888         FLAC__ASSERT(0 != bb);
889         FLAC__ASSERT(0 != bb->buffer);
890         FLAC__ASSERT(parameter <= 31);
891
892         /* init pattern with the unary end bit and the sign bit */
893         if(val < 0) {
894                 pattern = 3;
895                 val = -val;
896         }
897         else
898                 pattern = 2;
899
900         msbs = val >> parameter;
901         interesting_bits = 2 + parameter;
902         total_bits = interesting_bits + msbs;
903         pattern <<= parameter;
904         pattern |= (val & ((1<<parameter)-1)); /* the binary LSBs */
905
906         if(total_bits <= 32) {
907                 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
908                         return false;
909         }
910         else {
911                 /* write the unary MSBs */
912                 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
913                         return false;
914                 /* write the unary end bit, the sign bit, and binary LSBs */
915                 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
916                         return false;
917         }
918         return true;
919 }
920
921 #if 0 /* UNUSED */
922 FLAC__bool FLAC__bitbuffer_write_symmetric_rice_signed_guarded(FLAC__BitBuffer *bb, int val, unsigned parameter, unsigned max_bits, FLAC__bool *overflow)
923 {
924         unsigned total_bits, interesting_bits, msbs;
925         FLAC__uint32 pattern;
926
927         FLAC__ASSERT(0 != bb);
928         FLAC__ASSERT(0 != bb->buffer);
929         FLAC__ASSERT(parameter <= 31);
930
931         *overflow = false;
932
933         /* init pattern with the unary end bit and the sign bit */
934         if(val < 0) {
935                 pattern = 3;
936                 val = -val;
937         }
938         else
939                 pattern = 2;
940
941         msbs = val >> parameter;
942         interesting_bits = 2 + parameter;
943         total_bits = interesting_bits + msbs;
944         pattern <<= parameter;
945         pattern |= (val & ((1<<parameter)-1)); /* the binary LSBs */
946
947         if(total_bits <= 32) {
948                 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
949                         return false;
950         }
951         else if(total_bits > max_bits) {
952                 *overflow = true;
953                 return true;
954         }
955         else {
956                 /* write the unary MSBs */
957                 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
958                         return false;
959                 /* write the unary end bit, the sign bit, and binary LSBs */
960                 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
961                         return false;
962         }
963         return true;
964 }
965 #endif /* UNUSED */
966
967 FLAC__bool FLAC__bitbuffer_write_symmetric_rice_signed_escape(FLAC__BitBuffer *bb, int val, unsigned parameter)
968 {
969         unsigned total_bits, val_bits;
970         FLAC__uint32 pattern;
971
972         FLAC__ASSERT(0 != bb);
973         FLAC__ASSERT(0 != bb->buffer);
974         FLAC__ASSERT(parameter <= 31);
975
976         val_bits = FLAC__bitmath_silog2(val);
977         total_bits = 2 + parameter + 5 + val_bits;
978
979         if(total_bits <= 32) {
980                 pattern = 3;
981                 pattern <<= (parameter + 5);
982                 pattern |= val_bits;
983                 pattern <<= val_bits;
984                 pattern |= (val & ((1 << val_bits) - 1));
985                 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
986                         return false;
987         }
988         else {
989                 /* write the '-0' escape code first */
990                 if(!FLAC__bitbuffer_write_raw_uint32(bb, 3u << parameter, 2+parameter))
991                         return false;
992                 /* write the length */
993                 if(!FLAC__bitbuffer_write_raw_uint32(bb, val_bits, 5))
994                         return false;
995                 /* write the value */
996                 if(!FLAC__bitbuffer_write_raw_int32(bb, val, val_bits))
997                         return false;
998         }
999         return true;
1000 }
1001 #endif /* ifdef FLAC__SYMMETRIC_RICE */
1002
1003 FLAC__bool FLAC__bitbuffer_write_rice_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
1004 {
1005         unsigned total_bits, interesting_bits, msbs, uval;
1006         FLAC__uint32 pattern;
1007
1008         FLAC__ASSERT(0 != bb);
1009         FLAC__ASSERT(0 != bb->buffer);
1010         FLAC__ASSERT(parameter <= 30);
1011
1012         /* fold signed to unsigned */
1013         if(val < 0)
1014                 /* equivalent to
1015                  *     (unsigned)(((--val) << 1) - 1);
1016                  * but without the overflow problem at MININT
1017                  */
1018                 uval = (unsigned)(((-(++val)) << 1) + 1);
1019         else
1020                 uval = (unsigned)(val << 1);
1021
1022         msbs = uval >> parameter;
1023         interesting_bits = 1 + parameter;
1024         total_bits = interesting_bits + msbs;
1025         pattern = 1 << parameter; /* the unary end bit */
1026         pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
1027
1028         if(total_bits <= 32) {
1029                 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
1030                         return false;
1031         }
1032         else {
1033                 /* write the unary MSBs */
1034                 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
1035                         return false;
1036                 /* write the unary end bit and binary LSBs */
1037                 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
1038                         return false;
1039         }
1040         return true;
1041 }
1042
1043 #if 0 /* UNUSED */
1044 FLAC__bool FLAC__bitbuffer_write_rice_signed_guarded(FLAC__BitBuffer *bb, int val, unsigned parameter, unsigned max_bits, FLAC__bool *overflow)
1045 {
1046         unsigned total_bits, interesting_bits, msbs, uval;
1047         FLAC__uint32 pattern;
1048
1049         FLAC__ASSERT(0 != bb);
1050         FLAC__ASSERT(0 != bb->buffer);
1051         FLAC__ASSERT(parameter <= 30);
1052
1053         *overflow = false;
1054
1055         /* fold signed to unsigned */
1056         if(val < 0)
1057                 /* equivalent to
1058                  *     (unsigned)(((--val) << 1) - 1);
1059                  * but without the overflow problem at MININT
1060                  */
1061                 uval = (unsigned)(((-(++val)) << 1) + 1);
1062         else
1063                 uval = (unsigned)(val << 1);
1064
1065         msbs = uval >> parameter;
1066         interesting_bits = 1 + parameter;
1067         total_bits = interesting_bits + msbs;
1068         pattern = 1 << parameter; /* the unary end bit */
1069         pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
1070
1071         if(total_bits <= 32) {
1072                 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
1073                         return false;
1074         }
1075         else if(total_bits > max_bits) {
1076                 *overflow = true;
1077                 return true;
1078         }
1079         else {
1080                 /* write the unary MSBs */
1081                 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
1082                         return false;
1083                 /* write the unary end bit and binary LSBs */
1084                 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
1085                         return false;
1086         }
1087         return true;
1088 }
1089 #endif /* UNUSED */
1090
1091 #if 0 /* UNUSED */
1092 FLAC__bool FLAC__bitbuffer_write_golomb_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
1093 {
1094         unsigned total_bits, msbs, uval;
1095         unsigned k;
1096
1097         FLAC__ASSERT(0 != bb);
1098         FLAC__ASSERT(0 != bb->buffer);
1099         FLAC__ASSERT(parameter > 0);
1100
1101         /* fold signed to unsigned */
1102         if(val < 0)
1103                 /* equivalent to
1104                  *     (unsigned)(((--val) << 1) - 1);
1105                  * but without the overflow problem at MININT
1106                  */
1107                 uval = (unsigned)(((-(++val)) << 1) + 1);
1108         else
1109                 uval = (unsigned)(val << 1);
1110
1111         k = FLAC__bitmath_ilog2(parameter);
1112         if(parameter == 1u<<k) {
1113                 unsigned pattern;
1114
1115                 FLAC__ASSERT(k <= 30);
1116
1117                 msbs = uval >> k;
1118                 total_bits = 1 + k + msbs;
1119                 pattern = 1 << k; /* the unary end bit */
1120                 pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
1121
1122                 if(total_bits <= 32) {
1123                         if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
1124                                 return false;
1125                 }
1126                 else {
1127                         /* write the unary MSBs */
1128                         if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
1129                                 return false;
1130                         /* write the unary end bit and binary LSBs */
1131                         if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, k+1))
1132                                 return false;
1133                 }
1134         }
1135         else {
1136                 unsigned q, r, d;
1137
1138                 d = (1 << (k+1)) - parameter;
1139                 q = uval / parameter;
1140                 r = uval - (q * parameter);
1141                 /* write the unary MSBs */
1142                 if(!FLAC__bitbuffer_write_zeroes(bb, q))
1143                         return false;
1144                 /* write the unary end bit */
1145                 if(!FLAC__bitbuffer_write_raw_uint32(bb, 1, 1))
1146                         return false;
1147                 /* write the binary LSBs */
1148                 if(r >= d) {
1149                         if(!FLAC__bitbuffer_write_raw_uint32(bb, r+d, k+1))
1150                                 return false;
1151                 }
1152                 else {
1153                         if(!FLAC__bitbuffer_write_raw_uint32(bb, r, k))
1154                                 return false;
1155                 }
1156         }
1157         return true;
1158 }
1159
1160 FLAC__bool FLAC__bitbuffer_write_golomb_unsigned(FLAC__BitBuffer *bb, unsigned uval, unsigned parameter)
1161 {
1162         unsigned total_bits, msbs;
1163         unsigned k;
1164
1165         FLAC__ASSERT(0 != bb);
1166         FLAC__ASSERT(0 != bb->buffer);
1167         FLAC__ASSERT(parameter > 0);
1168
1169         k = FLAC__bitmath_ilog2(parameter);
1170         if(parameter == 1u<<k) {
1171                 unsigned pattern;
1172
1173                 FLAC__ASSERT(k <= 30);
1174
1175                 msbs = uval >> k;
1176                 total_bits = 1 + k + msbs;
1177                 pattern = 1 << k; /* the unary end bit */
1178                 pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
1179
1180                 if(total_bits <= 32) {
1181                         if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
1182                                 return false;
1183                 }
1184                 else {
1185                         /* write the unary MSBs */
1186                         if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
1187                                 return false;
1188                         /* write the unary end bit and binary LSBs */
1189                         if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, k+1))
1190                                 return false;
1191                 }
1192         }
1193         else {
1194                 unsigned q, r, d;
1195
1196                 d = (1 << (k+1)) - parameter;
1197                 q = uval / parameter;
1198                 r = uval - (q * parameter);
1199                 /* write the unary MSBs */
1200                 if(!FLAC__bitbuffer_write_zeroes(bb, q))
1201                         return false;
1202                 /* write the unary end bit */
1203                 if(!FLAC__bitbuffer_write_raw_uint32(bb, 1, 1))
1204                         return false;
1205                 /* write the binary LSBs */
1206                 if(r >= d) {
1207                         if(!FLAC__bitbuffer_write_raw_uint32(bb, r+d, k+1))
1208                                 return false;
1209                 }
1210                 else {
1211                         if(!FLAC__bitbuffer_write_raw_uint32(bb, r, k))
1212                                 return false;
1213                 }
1214         }
1215         return true;
1216 }
1217 #endif /* UNUSED */
1218
1219 FLAC__bool FLAC__bitbuffer_write_utf8_uint32(FLAC__BitBuffer *bb, FLAC__uint32 val)
1220 {
1221         FLAC__bool ok = 1;
1222
1223         FLAC__ASSERT(0 != bb);
1224         FLAC__ASSERT(0 != bb->buffer);
1225
1226         FLAC__ASSERT(!(val & 0x80000000)); /* this version only handles 31 bits */
1227
1228         if(val < 0x80) {
1229                 return FLAC__bitbuffer_write_raw_uint32(bb, val, 8);
1230         }
1231         else if(val < 0x800) {
1232                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xC0 | (val>>6), 8);
1233                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
1234         }
1235         else if(val < 0x10000) {
1236                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xE0 | (val>>12), 8);
1237                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
1238                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
1239         }
1240         else if(val < 0x200000) {
1241                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF0 | (val>>18), 8);
1242                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
1243                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
1244                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
1245         }
1246         else if(val < 0x4000000) {
1247                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF8 | (val>>24), 8);
1248                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>18)&0x3F), 8);
1249                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
1250                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
1251                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
1252         }
1253         else {
1254                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFC | (val>>30), 8);
1255                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>24)&0x3F), 8);
1256                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>18)&0x3F), 8);
1257                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
1258                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
1259                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
1260         }
1261
1262         return ok;
1263 }
1264
1265 FLAC__bool FLAC__bitbuffer_write_utf8_uint64(FLAC__BitBuffer *bb, FLAC__uint64 val)
1266 {
1267         FLAC__bool ok = 1;
1268
1269         FLAC__ASSERT(0 != bb);
1270         FLAC__ASSERT(0 != bb->buffer);
1271
1272         FLAC__ASSERT(!(val & FLAC__U64L(0xFFFFFFF000000000))); /* this version only handles 36 bits */
1273
1274         if(val < 0x80) {
1275                 return FLAC__bitbuffer_write_raw_uint32(bb, (FLAC__uint32)val, 8);
1276         }
1277         else if(val < 0x800) {
1278                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xC0 | (FLAC__uint32)(val>>6), 8);
1279                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
1280         }
1281         else if(val < 0x10000) {
1282                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xE0 | (FLAC__uint32)(val>>12), 8);
1283                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
1284                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
1285         }
1286         else if(val < 0x200000) {
1287                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF0 | (FLAC__uint32)(val>>18), 8);
1288                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
1289                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
1290                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
1291         }
1292         else if(val < 0x4000000) {
1293                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF8 | (FLAC__uint32)(val>>24), 8);
1294                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
1295                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
1296                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
1297                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
1298         }
1299         else if(val < 0x80000000) {
1300                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFC | (FLAC__uint32)(val>>30), 8);
1301                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
1302                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
1303                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
1304                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
1305                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
1306         }
1307         else {
1308                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFE, 8);
1309                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>30)&0x3F), 8);
1310                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
1311                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
1312                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
1313                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
1314                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
1315         }
1316
1317         return ok;
1318 }
1319
1320 FLAC__bool FLAC__bitbuffer_zero_pad_to_byte_boundary(FLAC__BitBuffer *bb)
1321 {
1322         /* 0-pad to byte boundary */
1323         if(bb->bits & 7u)
1324                 return FLAC__bitbuffer_write_zeroes(bb, 8 - (bb->bits & 7u));
1325         else
1326                 return true;
1327 }
1328
1329 FLAC__bool FLAC__bitbuffer_peek_bit(FLAC__BitBuffer *bb, unsigned *val, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1330 {
1331         /* to avoid a drastic speed penalty we don't:
1332         FLAC__ASSERT(0 != bb);
1333         FLAC__ASSERT(0 != bb->buffer);
1334         FLAC__ASSERT(bb->bits == 0);
1335         */
1336
1337         while(1) {
1338                 if(bb->total_consumed_bits < bb->total_bits) {
1339                         *val = (bb->buffer[bb->consumed_blurbs] & BLURB_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
1340                         return true;
1341                 }
1342                 else {
1343                         if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1344                                 return false;
1345                 }
1346         }
1347 }
1348
1349 FLAC__bool FLAC__bitbuffer_read_bit(FLAC__BitBuffer *bb, unsigned *val, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1350 {
1351         /* to avoid a drastic speed penalty we don't:
1352         FLAC__ASSERT(0 != bb);
1353         FLAC__ASSERT(0 != bb->buffer);
1354         FLAC__ASSERT(bb->bits == 0);
1355         */
1356
1357         while(1) {
1358                 if(bb->total_consumed_bits < bb->total_bits) {
1359                         *val = (bb->buffer[bb->consumed_blurbs] & BLURB_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
1360                         bb->consumed_bits++;
1361                         if(bb->consumed_bits == FLAC__BITS_PER_BLURB) {
1362                                 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1363                                 bb->consumed_blurbs++;
1364                                 bb->consumed_bits = 0;
1365                         }
1366                         bb->total_consumed_bits++;
1367                         return true;
1368                 }
1369                 else {
1370                         if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1371                                 return false;
1372                 }
1373         }
1374 }
1375
1376 FLAC__bool FLAC__bitbuffer_read_bit_to_uint32(FLAC__BitBuffer *bb, FLAC__uint32 *val, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1377 {
1378         /* to avoid a drastic speed penalty we don't:
1379         FLAC__ASSERT(0 != bb);
1380         FLAC__ASSERT(0 != bb->buffer);
1381         FLAC__ASSERT(bb->bits == 0);
1382         */
1383
1384         while(1) {
1385                 if(bb->total_consumed_bits < bb->total_bits) {
1386                         *val <<= 1;
1387                         *val |= (bb->buffer[bb->consumed_blurbs] & BLURB_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
1388                         bb->consumed_bits++;
1389                         if(bb->consumed_bits == FLAC__BITS_PER_BLURB) {
1390                                 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1391                                 bb->consumed_blurbs++;
1392                                 bb->consumed_bits = 0;
1393                         }
1394                         bb->total_consumed_bits++;
1395                         return true;
1396                 }
1397                 else {
1398                         if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1399                                 return false;
1400                 }
1401         }
1402 }
1403
1404 FLAC__bool FLAC__bitbuffer_read_bit_to_uint64(FLAC__BitBuffer *bb, FLAC__uint64 *val, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1405 {
1406         /* to avoid a drastic speed penalty we don't:
1407         FLAC__ASSERT(0 != bb);
1408         FLAC__ASSERT(0 != bb->buffer);
1409         FLAC__ASSERT(bb->bits == 0);
1410         */
1411
1412         while(1) {
1413                 if(bb->total_consumed_bits < bb->total_bits) {
1414                         *val <<= 1;
1415                         *val |= (bb->buffer[bb->consumed_blurbs] & BLURB_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
1416                         bb->consumed_bits++;
1417                         if(bb->consumed_bits == FLAC__BITS_PER_BLURB) {
1418                                 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1419                                 bb->consumed_blurbs++;
1420                                 bb->consumed_bits = 0;
1421                         }
1422                         bb->total_consumed_bits++;
1423                         return true;
1424                 }
1425                 else {
1426                         if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1427                                 return false;
1428                 }
1429         }
1430 }
1431
1432 FLaC__INLINE FLAC__bool FLAC__bitbuffer_read_raw_uint32(FLAC__BitBuffer *bb, FLAC__uint32 *val, const unsigned bits, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1433 #ifdef FLAC__NO_MANUAL_INLINING
1434 {
1435         unsigned i;
1436
1437         FLAC__ASSERT(0 != bb);
1438         FLAC__ASSERT(0 != bb->buffer);
1439
1440         FLAC__ASSERT(bits <= 32);
1441
1442         *val = 0;
1443         for(i = 0; i < bits; i++) {
1444                 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, val, read_callback, client_data))
1445                         return false;
1446         }
1447         return true;
1448 }
1449 #else
1450 {
1451         unsigned i, bits_ = bits;
1452         FLAC__uint32 v = 0;
1453
1454         FLAC__ASSERT(0 != bb);
1455         FLAC__ASSERT(0 != bb->buffer);
1456
1457         FLAC__ASSERT(bits <= 32);
1458         FLAC__ASSERT((bb->capacity*FLAC__BITS_PER_BLURB) * 2 >= bits);
1459
1460         if(bits == 0) {
1461                 *val = 0;
1462                 return true;
1463         }
1464
1465         while(bb->total_consumed_bits + bits > bb->total_bits) {
1466                 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1467                         return false;
1468         }
1469 #if FLAC__BITS_PER_BLURB > 8
1470         if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { /*@@@ comment on why this is here*/
1471 #endif
1472                 if(bb->consumed_bits) {
1473                         i = FLAC__BITS_PER_BLURB - bb->consumed_bits;
1474                         if(i <= bits_) {
1475                                 v = bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits);
1476                                 bits_ -= i;
1477                                 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1478                                 bb->consumed_blurbs++;
1479                                 bb->consumed_bits = 0;
1480                                 /* we hold off updating bb->total_consumed_bits until the end */
1481                         }
1482                         else {
1483                                 *val = (bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits)) >> (i-bits_);
1484                                 bb->consumed_bits += bits_;
1485                                 bb->total_consumed_bits += bits_;
1486                                 return true;
1487                         }
1488                 }
1489 #if FLAC__BITS_PER_BLURB == 32
1490                 /* note that we know bits_ cannot be > 32 because of previous assertions */
1491                 if(bits_ == FLAC__BITS_PER_BLURB) {
1492                         v = bb->buffer[bb->consumed_blurbs];
1493                         CRC16_UPDATE_BLURB(bb, v, bb->read_crc16);
1494                         bb->consumed_blurbs++;
1495                         /* bb->consumed_bits is already 0 */
1496                         bb->total_consumed_bits += bits;
1497                         *val = v;
1498                         return true;
1499                 }
1500 #else
1501                 while(bits_ >= FLAC__BITS_PER_BLURB) {
1502                         v <<= FLAC__BITS_PER_BLURB;
1503                         v |= bb->buffer[bb->consumed_blurbs];
1504                         bits_ -= FLAC__BITS_PER_BLURB;
1505                         CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1506                         bb->consumed_blurbs++;
1507                         /* bb->consumed_bits is already 0 */
1508                         /* we hold off updating bb->total_consumed_bits until the end */
1509                 }
1510 #endif
1511                 if(bits_ > 0) {
1512                         v <<= bits_;
1513                         v |= (bb->buffer[bb->consumed_blurbs] >> (FLAC__BITS_PER_BLURB-bits_));
1514                         bb->consumed_bits = bits_;
1515                         /* we hold off updating bb->total_consumed_bits until the end */
1516                 }
1517                 bb->total_consumed_bits += bits;
1518                 *val = v;
1519 #if FLAC__BITS_PER_BLURB > 8
1520         }
1521         else {
1522                 *val = 0;
1523                 for(i = 0; i < bits; i++) {
1524                         if(!FLAC__bitbuffer_read_bit_to_uint32(bb, val, read_callback, client_data))
1525                                 return false;
1526                 }
1527         }
1528 #endif
1529         return true;
1530 }
1531 #endif
1532
1533 FLAC__bool FLAC__bitbuffer_read_raw_int32(FLAC__BitBuffer *bb, FLAC__int32 *val, const unsigned bits, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1534 #ifdef FLAC__NO_MANUAL_INLINING
1535 {
1536         unsigned i;
1537         FLAC__uint32 v;
1538
1539         FLAC__ASSERT(0 != bb);
1540         FLAC__ASSERT(0 != bb->buffer);
1541
1542         FLAC__ASSERT(bits <= 32);
1543
1544         if(bits == 0) {
1545                 *val = 0;
1546                 return true;
1547         }
1548
1549         v = 0;
1550         for(i = 0; i < bits; i++) {
1551                 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &v, read_callback, client_data))
1552                         return false;
1553         }
1554
1555         /* fix the sign */
1556         i = 32 - bits;
1557         if(i) {
1558                 v <<= i;
1559                 *val = (FLAC__int32)v;
1560                 *val >>= i;
1561         }
1562         else
1563                 *val = (FLAC__int32)v;
1564
1565         return true;
1566 }
1567 #else
1568 {
1569         unsigned i, bits_ = bits;
1570         FLAC__uint32 v = 0;
1571
1572         FLAC__ASSERT(0 != bb);
1573         FLAC__ASSERT(0 != bb->buffer);
1574
1575         FLAC__ASSERT(bits <= 32);
1576         FLAC__ASSERT((bb->capacity*FLAC__BITS_PER_BLURB) * 2 >= bits);
1577
1578         if(bits == 0) {
1579                 *val = 0;
1580                 return true;
1581         }
1582
1583         while(bb->total_consumed_bits + bits > bb->total_bits) {
1584                 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1585                         return false;
1586         }
1587 #if FLAC__BITS_PER_BLURB > 8
1588         if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { /*@@@ comment on why this is here*/
1589 #endif
1590                 if(bb->consumed_bits) {
1591                         i = FLAC__BITS_PER_BLURB - bb->consumed_bits;
1592                         if(i <= bits_) {
1593                                 v = bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits);
1594                                 bits_ -= i;
1595                                 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1596                                 bb->consumed_blurbs++;
1597                                 bb->consumed_bits = 0;
1598                                 /* we hold off updating bb->total_consumed_bits until the end */
1599                         }
1600                         else {
1601                                 /* bits_ must be < FLAC__BITS_PER_BLURB-1 if we get to here */
1602                                 v = (bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits));
1603                                 v <<= (32-i);
1604                                 *val = (FLAC__int32)v;
1605                                 *val >>= (32-bits_);
1606                                 bb->consumed_bits += bits_;
1607                                 bb->total_consumed_bits += bits_;
1608                                 return true;
1609                         }
1610                 }
1611 #if FLAC__BITS_PER_BLURB == 32
1612                 /* note that we know bits_ cannot be > 32 because of previous assertions */
1613                 if(bits_ == FLAC__BITS_PER_BLURB) {
1614                         v = bb->buffer[bb->consumed_blurbs];
1615                         bits_ = 0;
1616                         CRC16_UPDATE_BLURB(bb, v, bb->read_crc16);
1617                         bb->consumed_blurbs++;
1618                         /* bb->consumed_bits is already 0 */
1619                         /* we hold off updating bb->total_consumed_bits until the end */
1620                 }
1621 #else
1622                 while(bits_ >= FLAC__BITS_PER_BLURB) {
1623                         v <<= FLAC__BITS_PER_BLURB;
1624                         v |= bb->buffer[bb->consumed_blurbs];
1625                         bits_ -= FLAC__BITS_PER_BLURB;
1626                         CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1627                         bb->consumed_blurbs++;
1628                         /* bb->consumed_bits is already 0 */
1629                         /* we hold off updating bb->total_consumed_bits until the end */
1630                 }
1631 #endif
1632                 if(bits_ > 0) {
1633                         v <<= bits_;
1634                         v |= (bb->buffer[bb->consumed_blurbs] >> (FLAC__BITS_PER_BLURB-bits_));
1635                         bb->consumed_bits = bits_;
1636                         /* we hold off updating bb->total_consumed_bits until the end */
1637                 }
1638                 bb->total_consumed_bits += bits;
1639 #if FLAC__BITS_PER_BLURB > 8
1640         }
1641         else {
1642                 for(i = 0; i < bits; i++) {
1643                         if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &v, read_callback, client_data))
1644                                 return false;
1645                 }
1646         }
1647 #endif
1648
1649         /* fix the sign */
1650         i = 32 - bits;
1651         if(i) {
1652                 v <<= i;
1653                 *val = (FLAC__int32)v;
1654                 *val >>= i;
1655         }
1656         else
1657                 *val = (FLAC__int32)v;
1658
1659         return true;
1660 }
1661 #endif
1662
1663 FLAC__bool FLAC__bitbuffer_read_raw_uint64(FLAC__BitBuffer *bb, FLAC__uint64 *val, const unsigned bits, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1664 #ifdef FLAC__NO_MANUAL_INLINING
1665 {
1666         unsigned i;
1667
1668         FLAC__ASSERT(0 != bb);
1669         FLAC__ASSERT(0 != bb->buffer);
1670
1671         FLAC__ASSERT(bits <= 64);
1672
1673         *val = 0;
1674         for(i = 0; i < bits; i++) {
1675                 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, val, read_callback, client_data))
1676                         return false;
1677         }
1678         return true;
1679 }
1680 #else
1681 {
1682         unsigned i, bits_ = bits;
1683         FLAC__uint64 v = 0;
1684
1685         FLAC__ASSERT(0 != bb);
1686         FLAC__ASSERT(0 != bb->buffer);
1687
1688         FLAC__ASSERT(bits <= 64);
1689         FLAC__ASSERT((bb->capacity*FLAC__BITS_PER_BLURB) * 2 >= bits);
1690
1691         if(bits == 0) {
1692                 *val = 0;
1693                 return true;
1694         }
1695
1696         while(bb->total_consumed_bits + bits > bb->total_bits) {
1697                 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1698                         return false;
1699         }
1700 #if FLAC__BITS_PER_BLURB > 8
1701         if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { /*@@@ comment on why this is here*/
1702 #endif
1703                 if(bb->consumed_bits) {
1704                         i = FLAC__BITS_PER_BLURB - bb->consumed_bits;
1705                         if(i <= bits_) {
1706                                 v = bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits);
1707                                 bits_ -= i;
1708                                 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1709                                 bb->consumed_blurbs++;
1710                                 bb->consumed_bits = 0;
1711                                 /* we hold off updating bb->total_consumed_bits until the end */
1712                         }
1713                         else {
1714                                 *val = (bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits)) >> (i-bits_);
1715                                 bb->consumed_bits += bits_;
1716                                 bb->total_consumed_bits += bits_;
1717                                 return true;
1718                         }
1719                 }
1720                 while(bits_ >= FLAC__BITS_PER_BLURB) {
1721                         v <<= FLAC__BITS_PER_BLURB;
1722                         v |= bb->buffer[bb->consumed_blurbs];
1723                         bits_ -= FLAC__BITS_PER_BLURB;
1724                         CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1725                         bb->consumed_blurbs++;
1726                         /* bb->consumed_bits is already 0 */
1727                         /* we hold off updating bb->total_consumed_bits until the end */
1728                 }
1729                 if(bits_ > 0) {
1730                         v <<= bits_;
1731                         v |= (bb->buffer[bb->consumed_blurbs] >> (FLAC__BITS_PER_BLURB-bits_));
1732                         bb->consumed_bits = bits_;
1733                         /* we hold off updating bb->total_consumed_bits until the end */
1734                 }
1735                 bb->total_consumed_bits += bits;
1736                 *val = v;
1737 #if FLAC__BITS_PER_BLURB > 8
1738         }
1739         else {
1740                 *val = 0;
1741                 for(i = 0; i < bits; i++) {
1742                         if(!FLAC__bitbuffer_read_bit_to_uint64(bb, val, read_callback, client_data))
1743                                 return false;
1744                 }
1745         }
1746 #endif
1747         return true;
1748 }
1749 #endif
1750
1751 #if 0 /* UNUSED */
1752 FLAC__bool FLAC__bitbuffer_read_raw_int64(FLAC__BitBuffer *bb, FLAC__int64 *val, const unsigned bits, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1753 #ifdef FLAC__NO_MANUAL_INLINING
1754 {
1755         unsigned i;
1756         FLAC__uint64 v;
1757
1758         FLAC__ASSERT(0 != bb);
1759         FLAC__ASSERT(0 != bb->buffer);
1760
1761         FLAC__ASSERT(bits <= 64);
1762
1763         v = 0;
1764         for(i = 0; i < bits; i++) {
1765                 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, &v, read_callback, client_data))
1766                         return false;
1767         }
1768         /* fix the sign */
1769         i = 64 - bits;
1770         if(i) {
1771                 v <<= i;
1772                 *val = (FLAC__int64)v;
1773                 *val >>= i;
1774         }
1775         else
1776                 *val = (FLAC__int64)v;
1777
1778         return true;
1779 }
1780 #else
1781 {
1782         unsigned i, bits_ = bits;
1783         FLAC__uint64 v = 0;
1784
1785         FLAC__ASSERT(0 != bb);
1786         FLAC__ASSERT(0 != bb->buffer);
1787
1788         FLAC__ASSERT(bits <= 64);
1789         FLAC__ASSERT((bb->capacity*FLAC__BITS_PER_BLURB) * 2 >= bits);
1790
1791         if(bits == 0) {
1792                 *val = 0;
1793                 return true;
1794         }
1795
1796         while(bb->total_consumed_bits + bits > bb->total_bits) {
1797                 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1798                         return false;
1799         }
1800 #if FLAC__BITS_PER_BLURB > 8
1801         if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { /*@@@ comment on why this is here*/
1802 #endif
1803                 if(bb->consumed_bits) {
1804                         i = FLAC__BITS_PER_BLURB - bb->consumed_bits;
1805                         if(i <= bits_) {
1806                                 v = bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits);
1807                                 bits_ -= i;
1808                                 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1809                                 bb->consumed_blurbs++;
1810                                 bb->consumed_bits = 0;
1811                                 /* we hold off updating bb->total_consumed_bits until the end */
1812                         }
1813                         else {
1814                                 /* bits_ must be < FLAC__BITS_PER_BLURB-1 if we get to here */
1815                                 v = (bb->buffer[bb->consumed_blurbs] & (FLAC__BLURB_ALL_ONES >> bb->consumed_bits));
1816                                 v <<= (64-i);
1817                                 *val = (FLAC__int64)v;
1818                                 *val >>= (64-bits_);
1819                                 bb->consumed_bits += bits_;
1820                                 bb->total_consumed_bits += bits_;
1821                                 return true;
1822                         }
1823                 }
1824                 while(bits_ >= FLAC__BITS_PER_BLURB) {
1825                         v <<= FLAC__BITS_PER_BLURB;
1826                         v |= bb->buffer[bb->consumed_blurbs];
1827                         bits_ -= FLAC__BITS_PER_BLURB;
1828                         CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1829                         bb->consumed_blurbs++;
1830                         /* bb->consumed_bits is already 0 */
1831                         /* we hold off updating bb->total_consumed_bits until the end */
1832                 }
1833                 if(bits_ > 0) {
1834                         v <<= bits_;
1835                         v |= (bb->buffer[bb->consumed_blurbs] >> (FLAC__BITS_PER_BLURB-bits_));
1836                         bb->consumed_bits = bits_;
1837                         /* we hold off updating bb->total_consumed_bits until the end */
1838                 }
1839                 bb->total_consumed_bits += bits;
1840 #if FLAC__BITS_PER_BLURB > 8
1841         }
1842         else {
1843                 for(i = 0; i < bits; i++) {
1844                         if(!FLAC__bitbuffer_read_bit_to_uint64(bb, &v, read_callback, client_data))
1845                                 return false;
1846                 }
1847         }
1848 #endif
1849
1850         /* fix the sign */
1851         i = 64 - bits;
1852         if(i) {
1853                 v <<= i;
1854                 *val = (FLAC__int64)v;
1855                 *val >>= i;
1856         }
1857         else
1858                 *val = (FLAC__int64)v;
1859
1860         return true;
1861 }
1862 #endif
1863 #endif
1864
1865 FLaC__INLINE FLAC__bool FLAC__bitbuffer_read_raw_uint32_little_endian(FLAC__BitBuffer *bb, FLAC__uint32 *val, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1866 {
1867         FLAC__uint32 x8, x32 = 0;
1868
1869         /* this doesn't need to be that fast as currently it is only used for vorbis comments */
1870
1871         if(!FLAC__bitbuffer_read_raw_uint32(bb, &x32, 8, read_callback, client_data))
1872                 return false;
1873
1874         if(!FLAC__bitbuffer_read_raw_uint32(bb, &x8, 8, read_callback, client_data))
1875                 return false;
1876         x32 |= (x8 << 8);
1877
1878         if(!FLAC__bitbuffer_read_raw_uint32(bb, &x8, 8, read_callback, client_data))
1879                 return false;
1880         x32 |= (x8 << 16);
1881
1882         if(!FLAC__bitbuffer_read_raw_uint32(bb, &x8, 8, read_callback, client_data))
1883                 return false;
1884         x32 |= (x8 << 24);
1885
1886         *val = x32;
1887         return true;
1888 }
1889
1890 FLAC__bool FLAC__bitbuffer_skip_bits_no_crc(FLAC__BitBuffer *bb, unsigned bits, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1891 {
1892         /*
1893          * @@@ a slightly faster implementation is possible but
1894          * probably not that useful since this is only called a
1895          * couple of times in the metadata readers.
1896          */
1897         FLAC__ASSERT(0 != bb);
1898         FLAC__ASSERT(0 != bb->buffer);
1899
1900         if(bits > 0) {
1901                 const unsigned n = bb->consumed_bits & 7;
1902                 unsigned m;
1903                 FLAC__uint32 x;
1904
1905                 if(n != 0) {
1906                         m = min(8-n, bits);
1907                         if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, m, read_callback, client_data))
1908                                 return false;
1909                         bits -= m;
1910                 }
1911                 m = bits / 8;
1912                 if(m > 0) {
1913                         if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(bb, 0, m, read_callback, client_data))
1914                                 return false;
1915                         bits %= 8;
1916                 }
1917                 if(bits > 0) {
1918                         if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, bits, read_callback, client_data))
1919                                 return false;
1920                 }
1921         }
1922
1923         return true;
1924 }
1925
1926 FLAC__bool FLAC__bitbuffer_read_byte_block_aligned_no_crc(FLAC__BitBuffer *bb, FLAC__byte *val, unsigned nvals, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1927 {
1928         FLAC__ASSERT(0 != bb);
1929         FLAC__ASSERT(0 != bb->buffer);
1930         FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(bb));
1931         FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(bb));
1932 #if FLAC__BITS_PER_BLURB == 8
1933         while(nvals > 0) {
1934                 unsigned chunk = min(nvals, bb->blurbs - bb->consumed_blurbs);
1935                 if(chunk == 0) {
1936                         if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1937                                 return false;
1938                 }
1939                 else {
1940                         if(0 != val) {
1941                                 memcpy(val, bb->buffer + bb->consumed_blurbs, FLAC__BYTES_PER_BLURB * chunk);
1942                                 val += FLAC__BYTES_PER_BLURB * chunk;
1943                         }
1944                         nvals -= chunk;
1945                         bb->consumed_blurbs += chunk;
1946                         bb->total_consumed_bits = (bb->consumed_blurbs << FLAC__BITS_PER_BLURB_LOG2);
1947                 }
1948         }
1949 #else
1950         @@@ need to write this still
1951         FLAC__ASSERT(0);
1952 #endif
1953
1954         return true;
1955 }
1956
1957 FLaC__INLINE FLAC__bool FLAC__bitbuffer_read_unary_unsigned(FLAC__BitBuffer *bb, unsigned *val, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1958 #ifdef FLAC__NO_MANUAL_INLINING
1959 {
1960         unsigned bit, val_ = 0;
1961
1962         FLAC__ASSERT(0 != bb);
1963         FLAC__ASSERT(0 != bb->buffer);
1964
1965         while(1) {
1966                 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1967                         return false;
1968                 if(bit)
1969                         break;
1970                 else
1971                         val_++;
1972         }
1973         *val = val_;
1974         return true;
1975 }
1976 #else
1977 {
1978         unsigned i, val_ = 0;
1979         unsigned total_blurbs_ = (bb->total_bits + (FLAC__BITS_PER_BLURB-1)) / FLAC__BITS_PER_BLURB;
1980         FLAC__blurb b;
1981
1982         FLAC__ASSERT(0 != bb);
1983         FLAC__ASSERT(0 != bb->buffer);
1984
1985 #if FLAC__BITS_PER_BLURB > 8
1986         if(bb->bits == 0 || bb->consumed_blurbs < bb->blurbs) { /*@@@ comment on why this is here*/
1987 #endif
1988                 if(bb->consumed_bits) {
1989                         b = bb->buffer[bb->consumed_blurbs] << bb->consumed_bits;
1990                         if(b) {
1991                                 for(i = 0; !(b & FLAC__BLURB_TOP_BIT_ONE); i++)
1992                                         b <<= 1;
1993                                 *val = i;
1994                                 i++;
1995                                 bb->consumed_bits += i;
1996                                 bb->total_consumed_bits += i;
1997                                 if(bb->consumed_bits == FLAC__BITS_PER_BLURB) {
1998                                         CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
1999                                         bb->consumed_blurbs++;
2000                                         bb->consumed_bits = 0;
2001                                 }
2002                                 return true;
2003                         }
2004                         else {
2005                                 val_ = FLAC__BITS_PER_BLURB - bb->consumed_bits;
2006                                 CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
2007                                 bb->consumed_blurbs++;
2008                                 bb->consumed_bits = 0;
2009                                 bb->total_consumed_bits += val_;
2010                         }
2011                 }
2012                 while(1) {
2013                         if(bb->consumed_blurbs >= total_blurbs_) {
2014                                 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
2015                                         return false;
2016                                 total_blurbs_ = (bb->total_bits + (FLAC__BITS_PER_BLURB-1)) / FLAC__BITS_PER_BLURB;
2017                         }
2018                         b = bb->buffer[bb->consumed_blurbs];
2019                         if(b) {
2020                                 for(i = 0; !(b & FLAC__BLURB_TOP_BIT_ONE); i++)
2021                                         b <<= 1;
2022                                 val_ += i;
2023                                 i++;
2024                                 bb->consumed_bits = i;
2025                                 *val = val_;
2026                                 if(i == FLAC__BITS_PER_BLURB) {
2027                                         CRC16_UPDATE_BLURB(bb, bb->buffer[bb->consumed_blurbs], bb->read_crc16);
2028                                         bb->consumed_blurbs++;
2029                                         bb->consumed_bits = 0;
2030                                 }
2031                                 bb->total_consumed_bits += i;
2032                                 return true;
2033                         }
2034                         else {
2035                                 val_ += FLAC__BITS_PER_BLURB;
2036                                 CRC16_UPDATE_BLURB(bb, 0, bb->read_crc16);
2037                                 bb->consumed_blurbs++;
2038                                 /* bb->consumed_bits is already 0 */
2039                                 bb->total_consumed_bits += FLAC__BITS_PER_BLURB;
2040                         }
2041                 }
2042 #if FLAC__BITS_PER_BLURB > 8
2043         }
2044         else {
2045                 while(1) {
2046                         if(!FLAC__bitbuffer_read_bit(bb, &i, read_callback, client_data))
2047                                 return false;
2048                         if(i)
2049                                 break;
2050                         else
2051                                 val_++;
2052                 }
2053                 *val = val_;
2054                 return true;
2055         }
2056 #endif
2057 }
2058 #endif
2059
2060 #ifdef FLAC__SYMMETRIC_RICE
2061 FLAC__bool FLAC__bitbuffer_read_symmetric_rice_signed(FLAC__BitBuffer *bb, int *val, unsigned parameter, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data)
2062 {
2063         FLAC__uint32 sign = 0, lsbs = 0, msbs = 0;
2064
2065         FLAC__ASSERT(0 != bb);
2066         FLAC__ASSERT(0 != bb->buffer);
2067         FLAC__ASSERT(parameter <= 31);
2068
2069         /* read the unary MSBs and end bit */
2070         if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
2071                 return false;
2072
2073         /* read the sign bit */
2074         if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &sign, read_callback, client_data))
2075                 return false;
2076
2077         /* read the binary LSBs */
2078         if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, parameter, read_callback, client_data))
2079                 return false;
2080
2081         /* compose the value */
2082         *val = (msbs << parameter) | lsbs;
2083         if(sign)
2084                 *val = -(*val);
2085
2086         return true;
2087 }
2088 #endif /* ifdef FLAC__SYMMETRIC_RICE */
2089
2090 FLAC__bool FLAC__bitbuffer_read_rice_signed(FLAC__BitBuffer *bb, int *val, unsigned parameter, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data)
2091 {
2092         FLAC__uint32 lsbs = 0, msbs = 0;
2093         unsigned uval;
2094
2095         FLAC__ASSERT(0 != bb);
2096         FLAC__ASSERT(0 != bb->buffer);
2097         FLAC__ASSERT(parameter <= 31);
2098
2099         /* read the unary MSBs and end bit */
2100         if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
2101                 return false;
2102
2103         /* read the binary LSBs */
2104         if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, parameter, read_callback, client_data))
2105                 return false;
2106
2107         /* compose the value */
2108         uval = (msbs << parameter) | lsbs;
2109         if(uval & 1)
2110                 *val = -((int)(uval >> 1)) - 1;
2111         else
2112                 *val = (int)(uval >> 1);
2113
2114         return true;
2115 }
2116
2117 FLAC__bool FLAC__bitbuffer_read_rice_signed_block(FLAC__BitBuffer *bb, int vals[], unsigned nvals, unsigned parameter, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data)
2118 {
2119         const FLAC__blurb *buffer = bb->buffer;
2120
2121         unsigned i, j, val_i = nvals;
2122         unsigned cbits = 0, uval = 0, msbs = 0, lsbs_left = 0;
2123         FLAC__blurb blurb, save_blurb;
2124         unsigned state = 0; /* 0 = getting unary MSBs, 1 = getting binary LSBs */
2125
2126         FLAC__ASSERT(0 != bb);
2127         FLAC__ASSERT(0 != bb->buffer);
2128         FLAC__ASSERT(parameter <= 31);
2129
2130         if(nvals == 0)
2131                 return true;
2132
2133         cbits = bb->consumed_bits;
2134         i = bb->consumed_blurbs;
2135         while(val_i != 0) {
2136                 for( ; i < bb->blurbs; i++) {
2137                         blurb = (save_blurb = buffer[i]) << cbits;
2138                         while(1) {
2139                                 if(state == 0) {
2140                                         if(blurb) {
2141                                                 j = FLAC__ALIGNED_BLURB_UNARY(blurb);
2142                                                 msbs += j;
2143                                                 j++;
2144                                                 cbits += j;
2145
2146                                                 uval = 0;
2147                                                 lsbs_left = parameter;
2148                                                 state++;
2149                                                 if(cbits == FLAC__BITS_PER_BLURB) {
2150                                                         cbits = 0;
2151                                                         CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
2152                                                         break;
2153                                                 }
2154                                                 blurb <<= j;
2155                                         }
2156                                         else {
2157                                                 msbs += FLAC__BITS_PER_BLURB - cbits;
2158                                                 cbits = 0;
2159                                                 CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
2160                                                 break;
2161                                         }
2162                                 }
2163                                 else {
2164                                         const unsigned available_bits = FLAC__BITS_PER_BLURB - cbits;
2165                                         if(lsbs_left >= available_bits) {
2166                                                 uval <<= available_bits;
2167                                                 uval |= (blurb >> cbits);
2168                                                 cbits = 0;
2169                                                 CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16);
2170
2171                                                 if(lsbs_left == available_bits) {
2172                                                         /* compose the value */
2173                                                         uval |= (msbs << parameter);
2174                                                         *vals = (int)(uval >> 1 ^ -(int)(uval & 1));
2175                                                         --val_i;
2176                                                         if(val_i == 0) {
2177                                                                 i++;
2178                                                                 goto break2;
2179                                                         }
2180                                                         *(++vals);
2181
2182                                                         msbs = 0;
2183                                                         state = 0;
2184                                                 }
2185
2186                                                 lsbs_left -= available_bits;
2187                                                 break;
2188                                         }
2189                                         else {
2190                                                 cbits += lsbs_left;
2191                                                 uval <<= lsbs_left;
2192                                                 uval |= (blurb >> (FLAC__BITS_PER_BLURB - lsbs_left));
2193                                                 blurb <<= lsbs_left;
2194
2195                                                 /* compose the value */
2196                                                 uval |= (msbs << parameter);
2197                                                 *vals = (int)(uval >> 1 ^ -(int)(uval & 1));
2198                                                 --val_i;
2199                                                 if(val_i == 0)
2200                                                         goto break2;
2201                                                 *(++vals);
2202
2203                                                 msbs = 0;
2204                                                 state = 0;
2205                                         }
2206                                 }
2207                         }
2208                 }
2209 break2:
2210                 bb->consumed_blurbs = i;
2211                 bb->consumed_bits = cbits;
2212                 bb->total_consumed_bits = (i << FLAC__BITS_PER_BLURB_LOG2) | cbits;
2213                 if(val_i != 0) {
2214                         if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
2215                                 return false;
2216                         /* these must be zero because we can only get here if we got to the end of the buffer */
2217                         FLAC__ASSERT(bb->consumed_blurbs == 0);
2218                         FLAC__ASSERT(bb->consumed_bits == 0);
2219                         i = 0;
2220                 }
2221         }
2222
2223         return true;
2224 }
2225
2226 #if 0 /* UNUSED */
2227 FLAC__bool FLAC__bitbuffer_read_golomb_signed(FLAC__BitBuffer *bb, int *val, unsigned parameter, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data)
2228 {
2229         FLAC__uint32 lsbs = 0, msbs = 0;
2230         unsigned bit, uval, k;
2231
2232         FLAC__ASSERT(0 != bb);
2233         FLAC__ASSERT(0 != bb->buffer);
2234
2235         k = FLAC__bitmath_ilog2(parameter);
2236
2237         /* read the unary MSBs and end bit */
2238         if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
2239                 return false;
2240
2241         /* read the binary LSBs */
2242         if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, k, read_callback, client_data))
2243                 return false;
2244
2245         if(parameter == 1u<<k) {
2246                 /* compose the value */
2247                 uval = (msbs << k) | lsbs;
2248         }
2249         else {
2250                 unsigned d = (1 << (k+1)) - parameter;
2251                 if(lsbs >= d) {
2252                         if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
2253                                 return false;
2254                         lsbs <<= 1;
2255                         lsbs |= bit;
2256                         lsbs -= d;
2257                 }
2258                 /* compose the value */
2259                 uval = msbs * parameter + lsbs;
2260         }
2261
2262         /* unfold unsigned to signed */
2263         if(uval & 1)
2264                 *val = -((int)(uval >> 1)) - 1;
2265         else
2266                 *val = (int)(uval >> 1);
2267
2268         return true;
2269 }
2270
2271 FLAC__bool FLAC__bitbuffer_read_golomb_unsigned(FLAC__BitBuffer *bb, unsigned *val, unsigned parameter, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data)
2272 {
2273         FLAC__uint32 lsbs, msbs = 0;
2274         unsigned bit, k;
2275
2276         FLAC__ASSERT(0 != bb);
2277         FLAC__ASSERT(0 != bb->buffer);
2278
2279         k = FLAC__bitmath_ilog2(parameter);
2280
2281         /* read the unary MSBs and end bit */
2282         if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
2283                 return false;
2284
2285         /* read the binary LSBs */
2286         if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, k, read_callback, client_data))
2287                 return false;
2288
2289         if(parameter == 1u<<k) {
2290                 /* compose the value */
2291                 *val = (msbs << k) | lsbs;
2292         }
2293         else {
2294                 unsigned d = (1 << (k+1)) - parameter;
2295                 if(lsbs >= d) {
2296                         if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
2297                                 return false;
2298                         lsbs <<= 1;
2299                         lsbs |= bit;
2300                         lsbs -= d;
2301                 }
2302                 /* compose the value */
2303                 *val = msbs * parameter + lsbs;
2304         }
2305
2306         return true;
2307 }
2308 #endif /* UNUSED */
2309
2310 /* on return, if *val == 0xffffffff then the utf-8 sequence was invalid, but the return value will be true */
2311 FLAC__bool FLAC__bitbuffer_read_utf8_uint32(FLAC__BitBuffer *bb, FLAC__uint32 *val, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data, FLAC__byte *raw, unsigned *rawlen)
2312 {
2313         FLAC__uint32 v = 0;
2314         FLAC__uint32 x;
2315         unsigned i;
2316
2317         if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
2318                 return false;
2319         if(raw)
2320                 raw[(*rawlen)++] = (FLAC__byte)x;
2321         if(!(x & 0x80)) { /* 0xxxxxxx */
2322                 v = x;
2323                 i = 0;
2324         }
2325         else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
2326                 v = x & 0x1F;
2327                 i = 1;
2328         }
2329         else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
2330                 v = x & 0x0F;
2331                 i = 2;
2332         }
2333         else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
2334                 v = x & 0x07;
2335                 i = 3;
2336         }
2337         else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
2338                 v = x & 0x03;
2339                 i = 4;
2340         }
2341         else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
2342                 v = x & 0x01;
2343                 i = 5;
2344         }
2345         else {
2346                 *val = 0xffffffff;
2347                 return true;
2348         }
2349         for( ; i; i--) {
2350                 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
2351                         return false;
2352                 if(raw)
2353                         raw[(*rawlen)++] = (FLAC__byte)x;
2354                 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
2355                         *val = 0xffffffff;
2356                         return true;
2357                 }
2358                 v <<= 6;
2359                 v |= (x & 0x3F);
2360         }
2361         *val = v;
2362         return true;
2363 }
2364
2365 /* on return, if *val == 0xffffffffffffffff then the utf-8 sequence was invalid, but the return value will be true */
2366 FLAC__bool FLAC__bitbuffer_read_utf8_uint64(FLAC__BitBuffer *bb, FLAC__uint64 *val, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data, FLAC__byte *raw, unsigned *rawlen)
2367 {
2368         FLAC__uint64 v = 0;
2369         FLAC__uint32 x;
2370         unsigned i;
2371
2372         if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
2373                 return false;
2374         if(raw)
2375                 raw[(*rawlen)++] = (FLAC__byte)x;
2376         if(!(x & 0x80)) { /* 0xxxxxxx */
2377                 v = x;
2378                 i = 0;
2379         }
2380         else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
2381                 v = x & 0x1F;
2382                 i = 1;
2383         }
2384         else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
2385                 v = x & 0x0F;
2386                 i = 2;
2387         }
2388         else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
2389                 v = x & 0x07;
2390                 i = 3;
2391         }
2392         else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
2393                 v = x & 0x03;
2394                 i = 4;
2395         }
2396         else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
2397                 v = x & 0x01;
2398                 i = 5;
2399         }
2400         else if(x & 0xFE && !(x & 0x01)) { /* 11111110 */
2401                 v = 0;
2402                 i = 6;
2403         }
2404         else {
2405                 *val = FLAC__U64L(0xffffffffffffffff);
2406                 return true;
2407         }
2408         for( ; i; i--) {
2409                 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
2410                         return false;
2411                 if(raw)
2412                         raw[(*rawlen)++] = (FLAC__byte)x;
2413                 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
2414                         *val = FLAC__U64L(0xffffffffffffffff);
2415                         return true;
2416                 }
2417                 v <<= 6;
2418                 v |= (x & 0x3F);
2419         }
2420         *val = v;
2421         return true;
2422 }
2423
2424 void FLAC__bitbuffer_dump(const FLAC__BitBuffer *bb, FILE *out)
2425 {
2426         unsigned i, j;
2427         if(bb == 0) {
2428                 fprintf(out, "bitbuffer is NULL\n");
2429         }
2430         else {
2431                 fprintf(out, "bitbuffer: capacity=%u blurbs=%u bits=%u total_bits=%u consumed: blurbs=%u, bits=%u, total_bits=%u\n", bb->capacity, bb->blurbs, bb->bits, bb->total_bits, bb->consumed_blurbs, bb->consumed_bits, bb->total_consumed_bits);
2432
2433                 for(i = 0; i < bb->blurbs; i++) {
2434                         fprintf(out, "%08X: ", i);
2435                         for(j = 0; j < FLAC__BITS_PER_BLURB; j++)
2436                                 if(i*FLAC__BITS_PER_BLURB+j < bb->total_consumed_bits)
2437                                         fprintf(out, ".");
2438                                 else
2439                                         fprintf(out, "%01u", bb->buffer[i] & (1 << (FLAC__BITS_PER_BLURB-j-1)) ? 1:0);
2440                         fprintf(out, "\n");
2441                 }
2442                 if(bb->bits > 0) {
2443                         fprintf(out, "%08X: ", i);
2444                         for(j = 0; j < bb->bits; j++)
2445                                 if(i*FLAC__BITS_PER_BLURB+j < bb->total_consumed_bits)
2446                                         fprintf(out, ".");
2447                                 else
2448                                         fprintf(out, "%01u", bb->buffer[i] & (1 << (bb->bits-j-1)) ? 1:0);
2449                         fprintf(out, "\n");
2450                 }
2451         }
2452 }