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