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