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