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