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