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