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