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