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