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