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