fix memory leak, tiny speedup, add some asserts
[flac.git] / src / libFLAC / bitbuffer.c
1 /* libFLAC - Free Lossless Audio Codec library
2  * Copyright (C) 2000,2001,2002  Josh Coalson
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA  02111-1307, USA.
18  */
19
20 #include <stdlib.h> /* for malloc() */
21 #include <string.h> /* for memcpy(), memset() */
22 #include "private/bitbuffer.h"
23 #include "private/bitmath.h"
24 #include "private/crc.h"
25 #include "FLAC/assert.h"
26
27 /*
28  * Along the way you will see two versions of some functions, selected
29  * by a FLAC__NO_MANUAL_INLINING macro.  One is the simplified, more
30  * readable, and slow version, and the other is the same function
31  * where crucial parts have been manually inlined and are much faster.
32  *
33  */
34
35 /* This should be at least twice as large as the largest number of bytes required to represent any 'number' (in any encoding) you are going to read. */
36 static const unsigned FLAC__BITBUFFER_DEFAULT_CAPACITY = 65536; /* bytes */
37
38 #define BYTE_BIT_TO_MASK(b) (((FLAC__byte)'\x80') >> (b))
39
40 #ifdef min
41 #undef min
42 #endif
43 #define min(x,y) ((x)<(y)?(x):(y))
44 #ifdef max
45 #undef max
46 #endif
47 #define max(x,y) ((x)>(y)?(x):(y))
48
49 #ifndef FLaC__INLINE
50 #define FLaC__INLINE
51 #endif
52
53 static FLAC__bool bitbuffer_resize_(FLAC__BitBuffer *bb, unsigned new_capacity)
54 {
55         FLAC__byte *new_buffer;
56
57         FLAC__ASSERT(bb != 0);
58         FLAC__ASSERT(bb->buffer != 0);
59
60         if(bb->capacity == new_capacity)
61                 return true;
62
63         new_buffer = (FLAC__byte*)malloc(sizeof(FLAC__byte) * new_capacity);
64         if(new_buffer == 0)
65                 return false;
66         memset(new_buffer, 0, new_capacity);
67         memcpy(new_buffer, bb->buffer, sizeof(FLAC__byte)*min(bb->bytes+(bb->bits?1:0), new_capacity));
68         if(new_capacity < bb->bytes+(bb->bits?1:0)) {
69                 bb->bytes = new_capacity;
70                 bb->bits = 0;
71                 bb->total_bits = (new_capacity<<3);
72         }
73         if(new_capacity < bb->consumed_bytes+(bb->consumed_bits?1:0)) {
74                 bb->consumed_bytes = new_capacity;
75                 bb->consumed_bits = 0;
76                 bb->total_consumed_bits = (new_capacity<<3);
77         }
78         free(bb->buffer); // we've already asserted above that (bb->buffer != 0)
79         bb->buffer = new_buffer;
80         bb->capacity = new_capacity;
81         return true;
82 }
83
84 static FLAC__bool bitbuffer_grow_(FLAC__BitBuffer *bb, unsigned min_bytes_to_add)
85 {
86         unsigned new_capacity;
87
88         FLAC__ASSERT(min_bytes_to_add > 0);
89
90         new_capacity = max(bb->capacity * 4, bb->capacity + min_bytes_to_add);
91         return bitbuffer_resize_(bb, new_capacity);
92 }
93
94 static FLAC__bool bitbuffer_ensure_size_(FLAC__BitBuffer *bb, unsigned bits_to_add)
95 {
96         FLAC__ASSERT(bb != 0);
97         FLAC__ASSERT(bb->buffer != 0);
98
99         if((bb->capacity<<3) < bb->total_bits + bits_to_add)
100                 return bitbuffer_grow_(bb, (bits_to_add>>3)+2);
101         else
102                 return true;
103 }
104
105 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)
106 {
107         unsigned bytes;
108
109         /* first shift the unconsumed buffer data toward the front as much as possible */
110         if(bb->total_consumed_bits >= 8) {
111                 unsigned l = 0, r = bb->consumed_bytes, r_end = bb->bytes;
112                 for( ; r < r_end; l++, r++)
113                         bb->buffer[l] = bb->buffer[r];
114                 for( ; l < r_end; l++)
115                         bb->buffer[l] = 0;
116                 bb->bytes -= bb->consumed_bytes;
117                 bb->total_bits -= (bb->consumed_bytes<<3);
118                 bb->consumed_bytes = 0;
119                 bb->total_consumed_bits = bb->consumed_bits;
120         }
121         /* grow if we need to */
122         if(bb->capacity <= 1) {
123                 if(!bitbuffer_resize_(bb, 16))
124                         return false;
125         }
126         /* finally, read in some data */
127         bytes = bb->capacity - bb->bytes;
128         if(!read_callback(bb->buffer+bb->bytes, &bytes, client_data))
129                 return false;
130         bb->bytes += bytes;
131         bb->total_bits += (bytes<<3);
132         return true;
133 }
134
135 void FLAC__bitbuffer_init(FLAC__BitBuffer *bb)
136 {
137         FLAC__ASSERT(bb != 0);
138
139         bb->buffer = 0;
140         bb->capacity = 0;
141         bb->bytes = bb->bits = bb->total_bits = 0;
142         bb->consumed_bytes = bb->consumed_bits = bb->total_consumed_bits = 0;
143 }
144
145 FLAC__bool FLAC__bitbuffer_init_from(FLAC__BitBuffer *bb, const FLAC__byte buffer[], unsigned bytes)
146 {
147         FLAC__ASSERT(bb != 0);
148         FLAC__bitbuffer_init(bb);
149
150         if(bytes == 0)
151                 return true;
152         else {
153                 FLAC__ASSERT(buffer != 0);
154                 bb->buffer = (FLAC__byte*)malloc(sizeof(FLAC__byte)*bytes);
155                 if(bb->buffer == 0)
156                         return false;
157                 memcpy(bb->buffer, buffer, sizeof(FLAC__byte)*bytes);
158                 bb->capacity = bb->bytes = bytes;
159                 bb->bits = 0;
160                 bb->total_bits = (bytes<<3);
161                 bb->consumed_bytes = bb->consumed_bits = bb->total_consumed_bits = 0;
162                 return true;
163         }
164 }
165
166 void FLAC__bitbuffer_init_read_crc16(FLAC__BitBuffer *bb, FLAC__uint16 seed)
167 {
168         FLAC__ASSERT(bb != 0);
169
170         bb->read_crc16 = seed;
171 }
172
173 FLAC__bool FLAC__bitbuffer_concatenate_aligned(FLAC__BitBuffer *dest, const FLAC__BitBuffer *src)
174 {
175         static const FLAC__byte mask_[9] = { 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff };
176         unsigned bits_to_add = src->total_bits - src->total_consumed_bits;
177
178         FLAC__ASSERT(dest != 0);
179         FLAC__ASSERT(src != 0);
180
181         if(bits_to_add == 0)
182                 return true;
183         if(dest->bits != src->consumed_bits)
184                 return false;
185         if(!bitbuffer_ensure_size_(dest, bits_to_add))
186                 return false;
187         if(dest->bits == 0) {
188                 memcpy(dest->buffer+dest->bytes, src->buffer+src->consumed_bytes, src->bytes-src->consumed_bytes + ((src->bits)? 1:0));
189         }
190         else if(dest->bits + bits_to_add > 8) {
191                 dest->buffer[dest->bytes] <<= (8 - dest->bits);
192                 dest->buffer[dest->bytes] |= (src->buffer[src->consumed_bytes] & mask_[8-dest->bits]);
193                 memcpy(dest->buffer+dest->bytes+1, src->buffer+src->consumed_bytes+1, src->bytes-src->consumed_bytes-1 + ((src->bits)? 1:0));
194         }
195         else {
196                 dest->buffer[dest->bytes] <<= bits_to_add;
197                 dest->buffer[dest->bytes] |= (src->buffer[src->consumed_bytes] & mask_[bits_to_add]);
198         }
199         dest->bits = src->bits;
200         dest->total_bits += bits_to_add;
201         dest->bytes = dest->total_bits / 8;
202
203         return true;
204 }
205
206 void FLAC__bitbuffer_free(FLAC__BitBuffer *bb)
207 {
208         FLAC__ASSERT(bb != 0);
209
210         if(bb->buffer != 0)
211                 free(bb->buffer);
212         bb->buffer = 0;
213         bb->capacity = 0;
214         bb->bytes = bb->bits = bb->total_bits = 0;
215         bb->consumed_bytes = bb->consumed_bits = bb->total_consumed_bits = 0;
216 }
217
218 FLAC__bool FLAC__bitbuffer_clear(FLAC__BitBuffer *bb)
219 {
220         if(bb->buffer == 0) {
221                 bb->capacity = FLAC__BITBUFFER_DEFAULT_CAPACITY;
222                 bb->buffer = (FLAC__byte*)malloc(sizeof(FLAC__byte) * bb->capacity);
223                 if(bb->buffer == 0)
224                         return false;
225                 memset(bb->buffer, 0, bb->capacity);
226         }
227         else {
228                 memset(bb->buffer, 0, bb->bytes + (bb->bits?1:0));
229         }
230         bb->bytes = bb->bits = bb->total_bits = 0;
231         bb->consumed_bytes = bb->consumed_bits = bb->total_consumed_bits = 0;
232         return true;
233 }
234
235 FLAC__bool FLAC__bitbuffer_clone(FLAC__BitBuffer *dest, const FLAC__BitBuffer *src)
236 {
237         FLAC__ASSERT(dest != 0);
238         FLAC__ASSERT(dest->buffer != 0);
239         FLAC__ASSERT(src != 0);
240         FLAC__ASSERT(src->buffer != 0);
241
242         if(dest->capacity < src->capacity)
243                 if(!bitbuffer_resize_(dest, src->capacity))
244                         return false;
245         memcpy(dest->buffer, src->buffer, sizeof(FLAC__byte)*min(src->capacity, src->bytes+1));
246         dest->bytes = src->bytes;
247         dest->bits = src->bits;
248         dest->total_bits = src->total_bits;
249         dest->consumed_bytes = src->consumed_bytes;
250         dest->consumed_bits = src->consumed_bits;
251         dest->total_consumed_bits = src->total_consumed_bits;
252         dest->read_crc16 = src->read_crc16;
253         return true;
254 }
255
256 FLAC__bool FLAC__bitbuffer_write_zeroes(FLAC__BitBuffer *bb, unsigned bits)
257 {
258         unsigned n;
259
260         FLAC__ASSERT(bb != 0);
261         FLAC__ASSERT(bb->buffer != 0);
262
263         if(bits == 0)
264                 return true;
265         if(!bitbuffer_ensure_size_(bb, bits))
266                 return false;
267         bb->total_bits += bits;
268         while(bits > 0) {
269                 n = min(8 - bb->bits, bits);
270                 bb->buffer[bb->bytes] <<= n;
271                 bits -= n;
272                 bb->bits += n;
273                 if(bb->bits == 8) {
274                         bb->bytes++;
275                         bb->bits = 0;
276                 }
277         }
278         return true;
279 }
280
281 FLaC__INLINE FLAC__bool FLAC__bitbuffer_write_raw_uint32(FLAC__BitBuffer *bb, FLAC__uint32 val, unsigned bits)
282 {
283         unsigned n, k;
284
285         FLAC__ASSERT(bb != 0);
286         FLAC__ASSERT(bb->buffer != 0);
287
288         FLAC__ASSERT(bits <= 32);
289         if(bits == 0)
290                 return true;
291         /* inline the size check so we don't incure a function call unnecessarily */
292         if((bb->capacity<<3) < bb->total_bits + bits) {
293                 if(!bitbuffer_ensure_size_(bb, bits))
294                         return false;
295         }
296         if(bits < 32) /* @@@ gcc seems to require this because the following line causes incorrect results when bits==32; investigate */
297                 val &= (~(0xffffffff << bits)); /* zero-out unused bits */
298         bb->total_bits += bits;
299         while(bits > 0) {
300                 n = 8 - bb->bits;
301                 if(n == 8) { /* i.e. bb->bits == 0 */
302                         if(bits < 8) {
303                                 bb->buffer[bb->bytes] = (FLAC__byte)val;
304                                 bb->bits = bits;
305                                 break;
306                         }
307                         else if(bits == 8) {
308                                 bb->buffer[bb->bytes++] = (FLAC__byte)val;
309                                 break;
310                         }
311                         else {
312                                 k = bits - 8;
313                                 bb->buffer[bb->bytes++] = (FLAC__byte)(val >> k);
314                                 val &= (~(0xffffffff << k));
315                                 bits -= 8;
316                         }
317                 }
318                 else if(bits <= n) {
319                         bb->buffer[bb->bytes] <<= bits;
320                         bb->buffer[bb->bytes] |= val;
321                         if(bits == n) {
322                                 bb->bytes++;
323                                 bb->bits = 0;
324                         }
325                         else
326                                 bb->bits += bits;
327                         break;
328                 }
329                 else {
330                         k = bits - n;
331                         bb->buffer[bb->bytes] <<= n;
332                         bb->buffer[bb->bytes] |= (val>>k);
333                         val &= (~(0xffffffff << k));
334                         bits -= n;
335                         bb->bytes++;
336                         bb->bits = 0;
337                 }
338         }
339
340         return true;
341 }
342
343 FLAC__bool FLAC__bitbuffer_write_raw_int32(FLAC__BitBuffer *bb, FLAC__int32 val, unsigned bits)
344 {
345         return FLAC__bitbuffer_write_raw_uint32(bb, (FLAC__uint32)val, bits);
346 }
347
348 FLAC__bool FLAC__bitbuffer_write_raw_uint64(FLAC__BitBuffer *bb, FLAC__uint64 val, unsigned bits)
349 {
350         static const FLAC__uint64 mask[] = {
351                 0,
352                 0x0000000000000001, 0x0000000000000003, 0x0000000000000007, 0x000000000000000F,
353                 0x000000000000001F, 0x000000000000003F, 0x000000000000007F, 0x00000000000000FF,
354                 0x00000000000001FF, 0x00000000000003FF, 0x00000000000007FF, 0x0000000000000FFF,
355                 0x0000000000001FFF, 0x0000000000003FFF, 0x0000000000007FFF, 0x000000000000FFFF,
356                 0x000000000001FFFF, 0x000000000003FFFF, 0x000000000007FFFF, 0x00000000000FFFFF,
357                 0x00000000001FFFFF, 0x00000000003FFFFF, 0x00000000007FFFFF, 0x0000000000FFFFFF,
358                 0x0000000001FFFFFF, 0x0000000003FFFFFF, 0x0000000007FFFFFF, 0x000000000FFFFFFF,
359                 0x000000001FFFFFFF, 0x000000003FFFFFFF, 0x000000007FFFFFFF, 0x00000000FFFFFFFF,
360                 0x00000001FFFFFFFF, 0x00000003FFFFFFFF, 0x00000007FFFFFFFF, 0x0000000FFFFFFFFF,
361                 0x0000001FFFFFFFFF, 0x0000003FFFFFFFFF, 0x0000007FFFFFFFFF, 0x000000FFFFFFFFFF,
362                 0x000001FFFFFFFFFF, 0x000003FFFFFFFFFF, 0x000007FFFFFFFFFF, 0x00000FFFFFFFFFFF,
363                 0x00001FFFFFFFFFFF, 0x00003FFFFFFFFFFF, 0x00007FFFFFFFFFFF, 0x0000FFFFFFFFFFFF,
364                 0x0001FFFFFFFFFFFF, 0x0003FFFFFFFFFFFF, 0x0007FFFFFFFFFFFF, 0x000FFFFFFFFFFFFF,
365                 0x001FFFFFFFFFFFFF, 0x003FFFFFFFFFFFFF, 0x007FFFFFFFFFFFFF, 0x00FFFFFFFFFFFFFF,
366                 0x01FFFFFFFFFFFFFF, 0x03FFFFFFFFFFFFFF, 0x07FFFFFFFFFFFFFF, 0x0FFFFFFFFFFFFFFF,
367                 0x1FFFFFFFFFFFFFFF, 0x3FFFFFFFFFFFFFFF, 0x7FFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF
368         };
369         unsigned n, k;
370
371         FLAC__ASSERT(bb != 0);
372         FLAC__ASSERT(bb->buffer != 0);
373
374         FLAC__ASSERT(bits <= 64);
375         if(bits == 0)
376                 return true;
377         if(!bitbuffer_ensure_size_(bb, bits))
378                 return false;
379         val &= mask[bits];
380         bb->total_bits += bits;
381         while(bits > 0) {
382                 if(bb->bits == 0) {
383                         if(bits < 8) {
384                                 bb->buffer[bb->bytes] = (FLAC__byte)val;
385                                 bb->bits = bits;
386                                 break;
387                         }
388                         else if(bits == 8) {
389                                 bb->buffer[bb->bytes++] = (FLAC__byte)val;
390                                 break;
391                         }
392                         else {
393                                 k = bits - 8;
394                                 bb->buffer[bb->bytes++] = (FLAC__byte)(val >> k);
395                                 val &= (~(0xffffffffffffffff << k));
396                                 bits -= 8;
397                         }
398                 }
399                 else {
400                         n = min(8 - bb->bits, bits);
401                         k = bits - n;
402                         bb->buffer[bb->bytes] <<= n;
403                         bb->buffer[bb->bytes] |= (val>>k);
404                         val &= (~(0xffffffffffffffff << k));
405                         bits -= n;
406                         bb->bits += n;
407                         if(bb->bits == 8) {
408                                 bb->bytes++;
409                                 bb->bits = 0;
410                         }
411                 }
412         }
413
414         return true;
415 }
416
417 FLAC__bool FLAC__bitbuffer_write_raw_int64(FLAC__BitBuffer *bb, FLAC__int64 val, unsigned bits)
418 {
419         return FLAC__bitbuffer_write_raw_uint64(bb, (FLAC__uint64)val, bits);
420 }
421
422 FLAC__bool FLAC__bitbuffer_write_unary_unsigned(FLAC__BitBuffer *bb, unsigned val)
423 {
424         if(val < 32)
425                 return FLAC__bitbuffer_write_raw_uint32(bb, 1, ++val);
426         else if(val < 64)
427                 return FLAC__bitbuffer_write_raw_uint64(bb, 1, ++val);
428         else {
429                 if(!FLAC__bitbuffer_write_zeroes(bb, val))
430                         return false;
431                 return FLAC__bitbuffer_write_raw_uint32(bb, 1, 1);
432         }
433 }
434
435 unsigned FLAC__bitbuffer_rice_bits(int val, unsigned parameter)
436 {
437         unsigned msbs, uval;
438
439         /* fold signed to unsigned */
440         if(val < 0)
441                 /* equivalent to
442                  *     (unsigned)(((--val) << 1) - 1);
443                  * but without the overflow problem at MININT
444                  */
445                 uval = (unsigned)(((-(++val)) << 1) + 1);
446         else
447                 uval = (unsigned)(val << 1);
448
449         msbs = uval >> parameter;
450
451         return 1 + parameter + msbs;
452 }
453
454 unsigned FLAC__bitbuffer_golomb_bits_signed(int val, unsigned parameter)
455 {
456         unsigned bits, msbs, uval;
457         unsigned k;
458
459         FLAC__ASSERT(parameter > 0);
460
461         /* fold signed to unsigned */
462         if(val < 0)
463                 /* equivalent to
464                  *     (unsigned)(((--val) << 1) - 1);
465                  * but without the overflow problem at MININT
466                  */
467                 uval = (unsigned)(((-(++val)) << 1) + 1);
468         else
469                 uval = (unsigned)(val << 1);
470
471         k = FLAC__bitmath_ilog2(parameter);
472         if(parameter == 1u<<k) {
473                 FLAC__ASSERT(k <= 30);
474
475                 msbs = uval >> k;
476                 bits = 1 + k + msbs;
477         }
478         else {
479                 unsigned q, r, d;
480
481                 d = (1 << (k+1)) - parameter;
482                 q = uval / parameter;
483                 r = uval - (q * parameter);
484
485                 bits = 1 + q + k;
486                 if(r >= d)
487                         bits++;
488         }
489         return bits;
490 }
491
492 unsigned FLAC__bitbuffer_golomb_bits_unsigned(unsigned uval, unsigned parameter)
493 {
494         unsigned bits, msbs;
495         unsigned k;
496
497         FLAC__ASSERT(parameter > 0);
498
499         k = FLAC__bitmath_ilog2(parameter);
500         if(parameter == 1u<<k) {
501                 FLAC__ASSERT(k <= 30);
502
503                 msbs = uval >> k;
504                 bits = 1 + k + msbs;
505         }
506         else {
507                 unsigned q, r, d;
508
509                 d = (1 << (k+1)) - parameter;
510                 q = uval / parameter;
511                 r = uval - (q * parameter);
512
513                 bits = 1 + q + k;
514                 if(r >= d)
515                         bits++;
516         }
517         return bits;
518 }
519
520 FLAC__bool FLAC__bitbuffer_write_symmetric_rice_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
521 {
522         unsigned total_bits, interesting_bits, msbs;
523         FLAC__uint32 pattern;
524
525         FLAC__ASSERT(bb != 0);
526         FLAC__ASSERT(bb->buffer != 0);
527         FLAC__ASSERT(parameter <= 31);
528
529         /* init pattern with the unary end bit and the sign bit */
530         if(val < 0) {
531                 pattern = 3;
532                 val = -val;
533         }
534         else
535                 pattern = 2;
536
537         msbs = val >> parameter;
538         interesting_bits = 2 + parameter;
539         total_bits = interesting_bits + msbs;
540         pattern <<= parameter;
541         pattern |= (val & ((1<<parameter)-1)); /* the binary LSBs */
542
543         if(total_bits <= 32) {
544                 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
545                         return false;
546         }
547         else {
548                 /* write the unary MSBs */
549                 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
550                         return false;
551                 /* write the unary end bit, the sign bit, and binary LSBs */
552                 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
553                         return false;
554         }
555         return true;
556 }
557
558 FLAC__bool FLAC__bitbuffer_write_symmetric_rice_signed_guarded(FLAC__BitBuffer *bb, int val, unsigned parameter, unsigned max_bits, FLAC__bool *overflow)
559 {
560         unsigned total_bits, interesting_bits, msbs;
561         FLAC__uint32 pattern;
562
563         FLAC__ASSERT(bb != 0);
564         FLAC__ASSERT(bb->buffer != 0);
565         FLAC__ASSERT(parameter <= 31);
566
567         *overflow = false;
568
569         /* init pattern with the unary end bit and the sign bit */
570         if(val < 0) {
571                 pattern = 3;
572                 val = -val;
573         }
574         else
575                 pattern = 2;
576
577         msbs = val >> parameter;
578         interesting_bits = 2 + parameter;
579         total_bits = interesting_bits + msbs;
580         pattern <<= parameter;
581         pattern |= (val & ((1<<parameter)-1)); /* the binary LSBs */
582
583         if(total_bits <= 32) {
584                 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
585                         return false;
586         }
587         else if(total_bits > max_bits) {
588                 *overflow = true;
589                 return true;
590         }
591         else {
592                 /* write the unary MSBs */
593                 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
594                         return false;
595                 /* write the unary end bit, the sign bit, and binary LSBs */
596                 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
597                         return false;
598         }
599         return true;
600 }
601
602 FLAC__bool FLAC__bitbuffer_write_symmetric_rice_signed_escape(FLAC__BitBuffer *bb, int val, unsigned parameter)
603 {
604         unsigned total_bits, val_bits;
605         FLAC__uint32 pattern;
606
607         FLAC__ASSERT(bb != 0);
608         FLAC__ASSERT(bb->buffer != 0);
609         FLAC__ASSERT(parameter <= 31);
610
611         val_bits = FLAC__bitmath_silog2(val);
612         total_bits = 2 + parameter + 5 + val_bits;
613
614         if(total_bits <= 32) {
615                 pattern = 3;
616                 pattern <<= (parameter + 5);
617                 pattern |= val_bits;
618                 pattern <<= val_bits;
619                 pattern |= (val & ((1 << val_bits) - 1));
620                 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
621                         return false;
622         }
623         else {
624                 /* write the '-0' escape code first */
625                 if(!FLAC__bitbuffer_write_raw_uint32(bb, 3u << parameter, 2+parameter))
626                         return false;
627                 /* write the length */
628                 if(!FLAC__bitbuffer_write_raw_uint32(bb, val_bits, 5))
629                         return false;
630                 /* write the value */
631                 if(!FLAC__bitbuffer_write_raw_int32(bb, val, val_bits))
632                         return false;
633         }
634         return true;
635 }
636
637 FLAC__bool FLAC__bitbuffer_write_rice_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
638 {
639         unsigned total_bits, interesting_bits, msbs, uval;
640         FLAC__uint32 pattern;
641
642         FLAC__ASSERT(bb != 0);
643         FLAC__ASSERT(bb->buffer != 0);
644         FLAC__ASSERT(parameter <= 30);
645
646         /* fold signed to unsigned */
647         if(val < 0)
648                 /* equivalent to
649                  *     (unsigned)(((--val) << 1) - 1);
650                  * but without the overflow problem at MININT
651                  */
652                 uval = (unsigned)(((-(++val)) << 1) + 1);
653         else
654                 uval = (unsigned)(val << 1);
655
656         msbs = uval >> parameter;
657         interesting_bits = 1 + parameter;
658         total_bits = interesting_bits + msbs;
659         pattern = 1 << parameter; /* the unary end bit */
660         pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
661
662         if(total_bits <= 32) {
663                 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
664                         return false;
665         }
666         else {
667                 /* write the unary MSBs */
668                 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
669                         return false;
670                 /* write the unary end bit and binary LSBs */
671                 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
672                         return false;
673         }
674         return true;
675 }
676
677 FLAC__bool FLAC__bitbuffer_write_rice_signed_guarded(FLAC__BitBuffer *bb, int val, unsigned parameter, unsigned max_bits, FLAC__bool *overflow)
678 {
679         unsigned total_bits, interesting_bits, msbs, uval;
680         FLAC__uint32 pattern;
681
682         FLAC__ASSERT(bb != 0);
683         FLAC__ASSERT(bb->buffer != 0);
684         FLAC__ASSERT(parameter <= 30);
685
686         *overflow = false;
687
688         /* fold signed to unsigned */
689         if(val < 0)
690                 /* equivalent to
691                  *     (unsigned)(((--val) << 1) - 1);
692                  * but without the overflow problem at MININT
693                  */
694                 uval = (unsigned)(((-(++val)) << 1) + 1);
695         else
696                 uval = (unsigned)(val << 1);
697
698         msbs = uval >> parameter;
699         interesting_bits = 1 + parameter;
700         total_bits = interesting_bits + msbs;
701         pattern = 1 << parameter; /* the unary end bit */
702         pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
703
704         if(total_bits <= 32) {
705                 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
706                         return false;
707         }
708         else if(total_bits > max_bits) {
709                 *overflow = true;
710                 return true;
711         }
712         else {
713                 /* write the unary MSBs */
714                 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
715                         return false;
716                 /* write the unary end bit and binary LSBs */
717                 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
718                         return false;
719         }
720         return true;
721 }
722
723 FLAC__bool FLAC__bitbuffer_write_golomb_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
724 {
725         unsigned total_bits, msbs, uval;
726         unsigned k;
727
728         FLAC__ASSERT(bb != 0);
729         FLAC__ASSERT(bb->buffer != 0);
730         FLAC__ASSERT(parameter > 0);
731
732         /* fold signed to unsigned */
733         if(val < 0)
734                 /* equivalent to
735                  *     (unsigned)(((--val) << 1) - 1);
736                  * but without the overflow problem at MININT
737                  */
738                 uval = (unsigned)(((-(++val)) << 1) + 1);
739         else
740                 uval = (unsigned)(val << 1);
741
742         k = FLAC__bitmath_ilog2(parameter);
743         if(parameter == 1u<<k) {
744                 unsigned pattern;
745
746                 FLAC__ASSERT(k <= 30);
747
748                 msbs = uval >> k;
749                 total_bits = 1 + k + msbs;
750                 pattern = 1 << k; /* the unary end bit */
751                 pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
752
753                 if(total_bits <= 32) {
754                         if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
755                                 return false;
756                 }
757                 else {
758                         /* write the unary MSBs */
759                         if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
760                                 return false;
761                         /* write the unary end bit and binary LSBs */
762                         if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, k+1))
763                                 return false;
764                 }
765         }
766         else {
767                 unsigned q, r, d;
768
769                 d = (1 << (k+1)) - parameter;
770                 q = uval / parameter;
771                 r = uval - (q * parameter);
772                 /* write the unary MSBs */
773                 if(!FLAC__bitbuffer_write_zeroes(bb, q))
774                         return false;
775                 /* write the unary end bit */
776                 if(!FLAC__bitbuffer_write_raw_uint32(bb, 1, 1))
777                         return false;
778                 /* write the binary LSBs */
779                 if(r >= d) {
780                         if(!FLAC__bitbuffer_write_raw_uint32(bb, r+d, k+1))
781                                 return false;
782                 }
783                 else {
784                         if(!FLAC__bitbuffer_write_raw_uint32(bb, r, k))
785                                 return false;
786                 }
787         }
788         return true;
789 }
790
791 FLAC__bool FLAC__bitbuffer_write_golomb_unsigned(FLAC__BitBuffer *bb, unsigned uval, unsigned parameter)
792 {
793         unsigned total_bits, msbs;
794         unsigned k;
795
796         FLAC__ASSERT(bb != 0);
797         FLAC__ASSERT(bb->buffer != 0);
798         FLAC__ASSERT(parameter > 0);
799
800         k = FLAC__bitmath_ilog2(parameter);
801         if(parameter == 1u<<k) {
802                 unsigned pattern;
803
804                 FLAC__ASSERT(k <= 30);
805
806                 msbs = uval >> k;
807                 total_bits = 1 + k + msbs;
808                 pattern = 1 << k; /* the unary end bit */
809                 pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
810
811                 if(total_bits <= 32) {
812                         if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
813                                 return false;
814                 }
815                 else {
816                         /* write the unary MSBs */
817                         if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
818                                 return false;
819                         /* write the unary end bit and binary LSBs */
820                         if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, k+1))
821                                 return false;
822                 }
823         }
824         else {
825                 unsigned q, r, d;
826
827                 d = (1 << (k+1)) - parameter;
828                 q = uval / parameter;
829                 r = uval - (q * parameter);
830                 /* write the unary MSBs */
831                 if(!FLAC__bitbuffer_write_zeroes(bb, q))
832                         return false;
833                 /* write the unary end bit */
834                 if(!FLAC__bitbuffer_write_raw_uint32(bb, 1, 1))
835                         return false;
836                 /* write the binary LSBs */
837                 if(r >= d) {
838                         if(!FLAC__bitbuffer_write_raw_uint32(bb, r+d, k+1))
839                                 return false;
840                 }
841                 else {
842                         if(!FLAC__bitbuffer_write_raw_uint32(bb, r, k))
843                                 return false;
844                 }
845         }
846         return true;
847 }
848
849 FLAC__bool FLAC__bitbuffer_write_utf8_uint32(FLAC__BitBuffer *bb, FLAC__uint32 val)
850 {
851         FLAC__bool ok = 1;
852
853         FLAC__ASSERT(bb != 0);
854         FLAC__ASSERT(bb->buffer != 0);
855
856         FLAC__ASSERT(!(val & 0x80000000)); /* this version only handles 31 bits */
857
858         if(val < 0x80) {
859                 return FLAC__bitbuffer_write_raw_uint32(bb, val, 8);
860         }
861         else if(val < 0x800) {
862                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xC0 | (val>>6), 8);
863                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
864         }
865         else if(val < 0x10000) {
866                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xE0 | (val>>12), 8);
867                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
868                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
869         }
870         else if(val < 0x200000) {
871                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF0 | (val>>18), 8);
872                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
873                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
874                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
875         }
876         else if(val < 0x4000000) {
877                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF8 | (val>>24), 8);
878                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>18)&0x3F), 8);
879                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
880                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
881                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
882         }
883         else {
884                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFC | (val>>30), 8);
885                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>24)&0x3F), 8);
886                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>18)&0x3F), 8);
887                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
888                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
889                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
890         }
891
892         return ok;
893 }
894
895 FLAC__bool FLAC__bitbuffer_write_utf8_uint64(FLAC__BitBuffer *bb, FLAC__uint64 val)
896 {
897         FLAC__bool ok = 1;
898
899         FLAC__ASSERT(bb != 0);
900         FLAC__ASSERT(bb->buffer != 0);
901
902         FLAC__ASSERT(!(val & 0xFFFFFFF000000000)); /* this version only handles 36 bits */
903
904         if(val < 0x80) {
905                 return FLAC__bitbuffer_write_raw_uint32(bb, (FLAC__uint32)val, 8);
906         }
907         else if(val < 0x800) {
908                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xC0 | (FLAC__uint32)(val>>6), 8);
909                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
910         }
911         else if(val < 0x10000) {
912                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xE0 | (FLAC__uint32)(val>>12), 8);
913                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
914                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
915         }
916         else if(val < 0x200000) {
917                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF0 | (FLAC__uint32)(val>>18), 8);
918                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
919                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
920                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
921         }
922         else if(val < 0x4000000) {
923                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF8 | (FLAC__uint32)(val>>24), 8);
924                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
925                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
926                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
927                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
928         }
929         else if(val < 0x80000000) {
930                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFC | (FLAC__uint32)(val>>30), 8);
931                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
932                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
933                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
934                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
935                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
936         }
937         else {
938                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFE, 8);
939                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>30)&0x3F), 8);
940                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
941                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
942                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
943                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
944                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (FLAC__uint32)(val&0x3F), 8);
945         }
946
947         return ok;
948 }
949
950 FLAC__bool FLAC__bitbuffer_zero_pad_to_byte_boundary(FLAC__BitBuffer *bb)
951 {
952         /* 0-pad to byte boundary */
953         if(bb->bits != 0)
954                 return FLAC__bitbuffer_write_zeroes(bb, 8 - bb->bits);
955         else
956                 return true;
957 }
958
959 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)
960 {
961         /* to avoid a drastic speed penalty we don't:
962         FLAC__ASSERT(bb != 0);
963         FLAC__ASSERT(bb->buffer != 0);
964         FLAC__ASSERT(bb->bits == 0);
965         */
966
967         while(1) {
968                 if(bb->total_consumed_bits < bb->total_bits) {
969                         *val = (bb->buffer[bb->consumed_bytes] & BYTE_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
970                         return true;
971                 }
972                 else {
973                         if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
974                                 return false;
975                 }
976         }
977 }
978
979 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)
980 {
981         /* to avoid a drastic speed penalty we don't:
982         FLAC__ASSERT(bb != 0);
983         FLAC__ASSERT(bb->buffer != 0);
984         FLAC__ASSERT(bb->bits == 0);
985         */
986
987         while(1) {
988                 if(bb->total_consumed_bits < bb->total_bits) {
989                         *val = (bb->buffer[bb->consumed_bytes] & BYTE_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
990                         bb->consumed_bits++;
991                         if(bb->consumed_bits == 8) {
992                                 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
993                                 bb->consumed_bytes++;
994                                 bb->consumed_bits = 0;
995                         }
996                         bb->total_consumed_bits++;
997                         return true;
998                 }
999                 else {
1000                         if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1001                                 return false;
1002                 }
1003         }
1004 }
1005
1006 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)
1007 {
1008         /* to avoid a drastic speed penalty we don't:
1009         FLAC__ASSERT(bb != 0);
1010         FLAC__ASSERT(bb->buffer != 0);
1011         FLAC__ASSERT(bb->bits == 0);
1012         */
1013
1014         while(1) {
1015                 if(bb->total_consumed_bits < bb->total_bits) {
1016                         *val <<= 1;
1017                         *val |= (bb->buffer[bb->consumed_bytes] & BYTE_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
1018                         bb->consumed_bits++;
1019                         if(bb->consumed_bits == 8) {
1020                                 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1021                                 bb->consumed_bytes++;
1022                                 bb->consumed_bits = 0;
1023                         }
1024                         bb->total_consumed_bits++;
1025                         return true;
1026                 }
1027                 else {
1028                         if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1029                                 return false;
1030                 }
1031         }
1032 }
1033
1034 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)
1035 {
1036         /* to avoid a drastic speed penalty we don't:
1037         FLAC__ASSERT(bb != 0);
1038         FLAC__ASSERT(bb->buffer != 0);
1039         FLAC__ASSERT(bb->bits == 0);
1040         */
1041
1042         while(1) {
1043                 if(bb->total_consumed_bits < bb->total_bits) {
1044                         *val <<= 1;
1045                         *val |= (bb->buffer[bb->consumed_bytes] & BYTE_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
1046                         bb->consumed_bits++;
1047                         if(bb->consumed_bits == 8) {
1048                                 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1049                                 bb->consumed_bytes++;
1050                                 bb->consumed_bits = 0;
1051                         }
1052                         bb->total_consumed_bits++;
1053                         return true;
1054                 }
1055                 else {
1056                         if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1057                                 return false;
1058                 }
1059         }
1060 }
1061
1062 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)
1063 #ifdef FLAC__NO_MANUAL_INLINING
1064 {
1065         unsigned i;
1066
1067         FLAC__ASSERT(bb != 0);
1068         FLAC__ASSERT(bb->buffer != 0);
1069
1070         FLAC__ASSERT(bits <= 32);
1071
1072         *val = 0;
1073         for(i = 0; i < bits; i++) {
1074                 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, val, read_callback, client_data))
1075                         return false;
1076         }
1077         return true;
1078 }
1079 #else
1080 {
1081         unsigned i, bits_ = bits;
1082         FLAC__uint32 v = 0;
1083
1084         FLAC__ASSERT(bb != 0);
1085         FLAC__ASSERT(bb->buffer != 0);
1086
1087         FLAC__ASSERT(bits <= 32);
1088         FLAC__ASSERT((bb->capacity*8) * 2 >= bits);
1089
1090         while(bb->total_consumed_bits + bits > bb->total_bits) {
1091                 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1092                         return false;
1093         }
1094         if(bb->consumed_bits) {
1095                 i = 8 - bb->consumed_bits;
1096                 if(i <= bits_) {
1097                         v = bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits);
1098                         bits_ -= i;
1099                         FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1100                         bb->consumed_bytes++;
1101                         bb->consumed_bits = 0;
1102                         /* we hold off updating bb->total_consumed_bits until the end */
1103                 }
1104                 else {
1105                         *val = (bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits)) >> (i-bits_);
1106                         bb->consumed_bits += bits_;
1107                         bb->total_consumed_bits += bits_;
1108                         return true;
1109                 }
1110         }
1111         while(bits_ >= 8) {
1112                 v <<= 8;
1113                 v |= bb->buffer[bb->consumed_bytes];
1114                 bits_ -= 8;
1115                 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1116                 bb->consumed_bytes++;
1117                 /* bb->consumed_bits is already 0 */
1118                 /* we hold off updating bb->total_consumed_bits until the end */
1119         }
1120         if(bits_ > 0) {
1121                 v <<= bits_;
1122                 v |= (bb->buffer[bb->consumed_bytes] >> (8-bits_));
1123                 bb->consumed_bits = bits_;
1124                 /* we hold off updating bb->total_consumed_bits until the end */
1125         }
1126         bb->total_consumed_bits += bits;
1127         *val = v;
1128         return true;
1129 }
1130 #endif
1131
1132 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)
1133 #ifdef FLAC__NO_MANUAL_INLINING
1134 {
1135         unsigned i;
1136         FLAC__uint32 v;
1137
1138         FLAC__ASSERT(bb != 0);
1139         FLAC__ASSERT(bb->buffer != 0);
1140
1141         FLAC__ASSERT(bits <= 32);
1142
1143         if(bits == 0) {
1144                 *val = 0;
1145                 return true;
1146         }
1147
1148         v = 0;
1149         for(i = 0; i < bits; i++) {
1150                 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &v, read_callback, client_data))
1151                         return false;
1152         }
1153
1154         /* fix the sign */
1155         i = 32 - bits;
1156         if(i) {
1157                 v <<= i;
1158                 *val = (FLAC__int32)v;
1159                 *val >>= i;
1160         }
1161         else
1162                 *val = (FLAC__int32)v;
1163
1164         return true;
1165 }
1166 #else
1167 {
1168         unsigned i, bits_ = bits;
1169         FLAC__uint32 v = 0;
1170
1171         FLAC__ASSERT(bb != 0);
1172         FLAC__ASSERT(bb->buffer != 0);
1173
1174         FLAC__ASSERT(bits <= 32);
1175         FLAC__ASSERT((bb->capacity*8) * 2 >= bits);
1176
1177         if(bits == 0) {
1178                 *val = 0;
1179                 return true;
1180         }
1181
1182         while(bb->total_consumed_bits + bits > bb->total_bits) {
1183                 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1184                         return false;
1185         }
1186         if(bb->consumed_bits) {
1187                 i = 8 - bb->consumed_bits;
1188                 if(i <= bits_) {
1189                         v = bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits);
1190                         bits_ -= i;
1191                         FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1192                         bb->consumed_bytes++;
1193                         bb->consumed_bits = 0;
1194                         /* we hold off updating bb->total_consumed_bits until the end */
1195                 }
1196                 else {
1197                         /* bits_ must be < 7 if we get to here */
1198                         v = (bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits));
1199                         v <<= (32-i);
1200                         *val = (FLAC__int32)v;
1201                         *val >>= (32-bits_);
1202                         bb->consumed_bits += bits_;
1203                         bb->total_consumed_bits += bits_;
1204                         return true;
1205                 }
1206         }
1207         while(bits_ >= 8) {
1208                 v <<= 8;
1209                 v |= bb->buffer[bb->consumed_bytes];
1210                 bits_ -= 8;
1211                 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1212                 bb->consumed_bytes++;
1213                 /* bb->consumed_bits is already 0 */
1214                 /* we hold off updating bb->total_consumed_bits until the end */
1215         }
1216         if(bits_ > 0) {
1217                 v <<= bits_;
1218                 v |= (bb->buffer[bb->consumed_bytes] >> (8-bits_));
1219                 bb->consumed_bits = bits_;
1220                 /* we hold off updating bb->total_consumed_bits until the end */
1221         }
1222         bb->total_consumed_bits += bits;
1223
1224         /* fix the sign */
1225         i = 32 - bits;
1226         if(i) {
1227                 v <<= i;
1228                 *val = (FLAC__int32)v;
1229                 *val >>= i;
1230         }
1231         else
1232                 *val = (FLAC__int32)v;
1233
1234         return true;
1235 }
1236 #endif
1237
1238 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)
1239 #ifdef FLAC__NO_MANUAL_INLINING
1240 {
1241         unsigned i;
1242
1243         FLAC__ASSERT(bb != 0);
1244         FLAC__ASSERT(bb->buffer != 0);
1245
1246         FLAC__ASSERT(bits <= 64);
1247
1248         *val = 0;
1249         for(i = 0; i < bits; i++) {
1250                 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, val, read_callback, client_data))
1251                         return false;
1252         }
1253         return true;
1254 }
1255 #else
1256 {
1257         unsigned i, bits_ = bits;
1258         FLAC__uint64 v = 0;
1259
1260         FLAC__ASSERT(bb != 0);
1261         FLAC__ASSERT(bb->buffer != 0);
1262
1263         FLAC__ASSERT(bits <= 64);
1264         FLAC__ASSERT((bb->capacity*8) * 2 >= bits);
1265
1266         while(bb->total_consumed_bits + bits > bb->total_bits) {
1267                 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1268                         return false;
1269         }
1270         if(bb->consumed_bits) {
1271                 i = 8 - bb->consumed_bits;
1272                 if(i <= bits_) {
1273                         v = bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits);
1274                         bits_ -= i;
1275                         FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1276                         bb->consumed_bytes++;
1277                         bb->consumed_bits = 0;
1278                         /* we hold off updating bb->total_consumed_bits until the end */
1279                 }
1280                 else {
1281                         *val = (bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits)) >> (i-bits_);
1282                         bb->consumed_bits += bits_;
1283                         bb->total_consumed_bits += bits_;
1284                         return true;
1285                 }
1286         }
1287         while(bits_ >= 8) {
1288                 v <<= 8;
1289                 v |= bb->buffer[bb->consumed_bytes];
1290                 bits_ -= 8;
1291                 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1292                 bb->consumed_bytes++;
1293                 /* bb->consumed_bits is already 0 */
1294                 /* we hold off updating bb->total_consumed_bits until the end */
1295         }
1296         if(bits_ > 0) {
1297                 v <<= bits_;
1298                 v |= (bb->buffer[bb->consumed_bytes] >> (8-bits_));
1299                 bb->consumed_bits = bits_;
1300                 /* we hold off updating bb->total_consumed_bits until the end */
1301         }
1302         bb->total_consumed_bits += bits;
1303         *val = v;
1304         return true;
1305 }
1306 #endif
1307
1308 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)
1309 #ifdef FLAC__NO_MANUAL_INLINING
1310 {
1311         unsigned i;
1312         FLAC__uint64 v;
1313
1314         FLAC__ASSERT(bb != 0);
1315         FLAC__ASSERT(bb->buffer != 0);
1316
1317         FLAC__ASSERT(bits <= 64);
1318
1319         v = 0;
1320         for(i = 0; i < bits; i++) {
1321                 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, &v, read_callback, client_data))
1322                         return false;
1323         }
1324         /* fix the sign */
1325         i = 64 - bits;
1326         if(i) {
1327                 v <<= i;
1328                 *val = (FLAC__int64)v;
1329                 *val >>= i;
1330         }
1331         else
1332                 *val = (FLAC__int64)v;
1333
1334         return true;
1335 }
1336 #else
1337 {
1338         unsigned i, bits_ = bits;
1339         FLAC__uint64 v = 0;
1340
1341         FLAC__ASSERT(bb != 0);
1342         FLAC__ASSERT(bb->buffer != 0);
1343
1344         FLAC__ASSERT(bits <= 64);
1345         FLAC__ASSERT((bb->capacity*8) * 2 >= bits);
1346
1347         while(bb->total_consumed_bits + bits > bb->total_bits) {
1348                 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1349                         return false;
1350         }
1351         if(bb->consumed_bits) {
1352                 i = 8 - bb->consumed_bits;
1353                 if(i <= bits_) {
1354                         v = bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits);
1355                         bits_ -= i;
1356                         FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1357                         bb->consumed_bytes++;
1358                         bb->consumed_bits = 0;
1359                         /* we hold off updating bb->total_consumed_bits until the end */
1360                 }
1361                 else {
1362                         /* bits_ must be < 7 if we get to here */
1363                         v = (bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits));
1364                         v <<= (64-i);
1365                         *val = (FLAC__int64)v;
1366                         *val >>= (64-bits_);
1367                         bb->consumed_bits += bits_;
1368                         bb->total_consumed_bits += bits_;
1369                         return true;
1370                 }
1371         }
1372         while(bits_ >= 8) {
1373                 v <<= 8;
1374                 v |= bb->buffer[bb->consumed_bytes];
1375                 bits_ -= 8;
1376                 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1377                 bb->consumed_bytes++;
1378                 /* bb->consumed_bits is already 0 */
1379                 /* we hold off updating bb->total_consumed_bits until the end */
1380         }
1381         if(bits_ > 0) {
1382                 v <<= bits_;
1383                 v |= (bb->buffer[bb->consumed_bytes] >> (8-bits_));
1384                 bb->consumed_bits = bits_;
1385                 /* we hold off updating bb->total_consumed_bits until the end */
1386         }
1387         bb->total_consumed_bits += bits;
1388
1389         /* fix the sign */
1390         i = 64 - bits;
1391         if(i) {
1392                 v <<= i;
1393                 *val = (FLAC__int64)v;
1394                 *val >>= i;
1395         }
1396         else
1397                 *val = (FLAC__int64)v;
1398
1399         return true;
1400 }
1401 #endif
1402
1403 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)
1404 #ifdef FLAC__NO_MANUAL_INLINING
1405 {
1406         unsigned bit, val_ = 0;
1407
1408         FLAC__ASSERT(bb != 0);
1409         FLAC__ASSERT(bb->buffer != 0);
1410
1411         while(1) {
1412                 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1413                         return false;
1414                 if(bit)
1415                         break;
1416                 else
1417                         val_++;
1418         }
1419         *val = val_;
1420         return true;
1421 }
1422 #else
1423 {
1424         unsigned i, val_ = 0;
1425         unsigned total_bytes_ = (bb->total_bits + 7) / 8;
1426         FLAC__byte b;
1427
1428         FLAC__ASSERT(bb != 0);
1429         FLAC__ASSERT(bb->buffer != 0);
1430
1431         if(bb->consumed_bits) {
1432                 b = bb->buffer[bb->consumed_bytes] << bb->consumed_bits;
1433                 if(b) {
1434                         for(i = 0; !(b & 0x80); i++)
1435                                 b <<= 1;
1436                         *val = i;
1437                         i++;
1438                         bb->consumed_bits += i;
1439                         bb->total_consumed_bits += i;
1440                         if(bb->consumed_bits == 8) {
1441                                 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1442                                 bb->consumed_bytes++;
1443                                 bb->consumed_bits = 0;
1444                         }
1445                         return true;
1446                 }
1447                 else {
1448                         val_ = 8 - bb->consumed_bits;
1449                         FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1450                         bb->consumed_bytes++;
1451                         bb->consumed_bits = 0;
1452                         bb->total_consumed_bits += val_;
1453                 }
1454         }
1455         while(1) {
1456                 if(bb->consumed_bytes >= total_bytes_) {
1457                         if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1458                                 return false;
1459                         total_bytes_ = (bb->total_bits + 7) / 8;
1460                 }
1461                 b = bb->buffer[bb->consumed_bytes];
1462                 if(b) {
1463                         for(i = 0; !(b & 0x80); i++)
1464                                 b <<= 1;
1465                         val_ += i;
1466                         i++;
1467                         bb->consumed_bits = i;
1468                         *val = val_;
1469                         if(i == 8) {
1470                                 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1471                                 bb->consumed_bytes++;
1472                                 bb->consumed_bits = 0;
1473                         }
1474                         bb->total_consumed_bits += i;
1475                         return true;
1476                 }
1477                 else {
1478                         val_ += 8;
1479                         FLAC__CRC16_UPDATE(0, bb->read_crc16);
1480                         bb->consumed_bytes++;
1481                         /* bb->consumed_bits is already 0 */
1482                         /* we hold off updating bb->total_consumed_bits until the end */
1483                         bb->total_consumed_bits += 8;
1484                 }
1485         }
1486 }
1487 #endif
1488
1489 FLAC__bool FLAC__bitbuffer_read_symmetric_rice_signed(FLAC__BitBuffer *bb, int *val, unsigned parameter, FLAC__bool (*read_callback)(FLAC__byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1490 {
1491         FLAC__uint32 sign = 0, lsbs = 0, msbs = 0;
1492
1493         FLAC__ASSERT(bb != 0);
1494         FLAC__ASSERT(bb->buffer != 0);
1495         FLAC__ASSERT(parameter <= 31);
1496
1497         /* read the unary MSBs and end bit */
1498         if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
1499                 return false;
1500
1501         /* read the sign bit */
1502         if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &sign, read_callback, client_data))
1503                 return false;
1504
1505         /* read the binary LSBs */
1506         if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, parameter, read_callback, client_data))
1507                 return false;
1508
1509         /* compose the value */
1510         *val = (msbs << parameter) | lsbs;
1511         if(sign)
1512                 *val = -(*val);
1513
1514         return true;
1515 }
1516
1517 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)
1518 {
1519         FLAC__uint32 lsbs = 0, msbs = 0;
1520         unsigned uval;
1521
1522         FLAC__ASSERT(bb != 0);
1523         FLAC__ASSERT(bb->buffer != 0);
1524         FLAC__ASSERT(parameter <= 31);
1525
1526         /* read the unary MSBs and end bit */
1527         if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
1528                 return false;
1529
1530         /* read the binary LSBs */
1531         if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, parameter, read_callback, client_data))
1532                 return false;
1533
1534         /* compose the value */
1535         uval = (msbs << parameter) | lsbs;
1536         if(uval & 1)
1537                 *val = -((int)(uval >> 1)) - 1;
1538         else
1539                 *val = (int)(uval >> 1);
1540
1541         return true;
1542 }
1543
1544 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)
1545 {
1546         FLAC__uint32 lsbs = 0, msbs = 0;
1547         unsigned bit, uval, k;
1548
1549         FLAC__ASSERT(bb != 0);
1550         FLAC__ASSERT(bb->buffer != 0);
1551
1552         k = FLAC__bitmath_ilog2(parameter);
1553
1554         /* read the unary MSBs and end bit */
1555         if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
1556                 return false;
1557
1558         /* read the binary LSBs */
1559         if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, k, read_callback, client_data))
1560                 return false;
1561
1562         if(parameter == 1u<<k) {
1563                 /* compose the value */
1564                 uval = (msbs << k) | lsbs;
1565         }
1566         else {
1567                 unsigned d = (1 << (k+1)) - parameter;
1568                 if(lsbs >= d) {
1569                         if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1570                                 return false;
1571                         lsbs <<= 1;
1572                         lsbs |= bit;
1573                         lsbs -= d;
1574                 }
1575                 /* compose the value */
1576                 uval = msbs * parameter + lsbs;
1577         }
1578
1579         /* unfold unsigned to signed */
1580         if(uval & 1)
1581                 *val = -((int)(uval >> 1)) - 1;
1582         else
1583                 *val = (int)(uval >> 1);
1584
1585         return true;
1586 }
1587
1588 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)
1589 {
1590         FLAC__uint32 lsbs, msbs = 0;
1591         unsigned bit, k;
1592
1593         FLAC__ASSERT(bb != 0);
1594         FLAC__ASSERT(bb->buffer != 0);
1595
1596         k = FLAC__bitmath_ilog2(parameter);
1597
1598         /* read the unary MSBs and end bit */
1599         if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
1600                 return false;
1601
1602         /* read the binary LSBs */
1603         if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, k, read_callback, client_data))
1604                 return false;
1605
1606         if(parameter == 1u<<k) {
1607                 /* compose the value */
1608                 *val = (msbs << k) | lsbs;
1609         }
1610         else {
1611                 unsigned d = (1 << (k+1)) - parameter;
1612                 if(lsbs >= d) {
1613                         if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1614                                 return false;
1615                         lsbs <<= 1;
1616                         lsbs |= bit;
1617                         lsbs -= d;
1618                 }
1619                 /* compose the value */
1620                 *val = msbs * parameter + lsbs;
1621         }
1622
1623         return true;
1624 }
1625
1626 /* on return, if *val == 0xffffffff then the utf-8 sequence was invalid, but the return value will be true */
1627 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)
1628 {
1629         FLAC__uint32 v = 0;
1630         FLAC__uint32 x;
1631         unsigned i;
1632
1633         if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
1634                 return false;
1635         if(raw)
1636                 raw[(*rawlen)++] = (FLAC__byte)x;
1637         if(!(x & 0x80)) { /* 0xxxxxxx */
1638                 v = x;
1639                 i = 0;
1640         }
1641         else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
1642                 v = x & 0x1F;
1643                 i = 1;
1644         }
1645         else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
1646                 v = x & 0x0F;
1647                 i = 2;
1648         }
1649         else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
1650                 v = x & 0x07;
1651                 i = 3;
1652         }
1653         else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
1654                 v = x & 0x03;
1655                 i = 4;
1656         }
1657         else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
1658                 v = x & 0x01;
1659                 i = 5;
1660         }
1661         else {
1662                 *val = 0xffffffff;
1663                 return true;
1664         }
1665         for( ; i; i--) {
1666                 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
1667                         return false;
1668                 if(raw)
1669                         raw[(*rawlen)++] = (FLAC__byte)x;
1670                 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
1671                         *val = 0xffffffff;
1672                         return true;
1673                 }
1674                 v <<= 6;
1675                 v |= (x & 0x3F);
1676         }
1677         *val = v;
1678         return true;
1679 }
1680
1681 /* on return, if *val == 0xffffffffffffffff then the utf-8 sequence was invalid, but the return value will be true */
1682 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)
1683 {
1684         FLAC__uint64 v = 0;
1685         FLAC__uint32 x;
1686         unsigned i;
1687
1688         if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
1689                 return false;
1690         if(raw)
1691                 raw[(*rawlen)++] = (FLAC__byte)x;
1692         if(!(x & 0x80)) { /* 0xxxxxxx */
1693                 v = x;
1694                 i = 0;
1695         }
1696         else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
1697                 v = x & 0x1F;
1698                 i = 1;
1699         }
1700         else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
1701                 v = x & 0x0F;
1702                 i = 2;
1703         }
1704         else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
1705                 v = x & 0x07;
1706                 i = 3;
1707         }
1708         else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
1709                 v = x & 0x03;
1710                 i = 4;
1711         }
1712         else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
1713                 v = x & 0x01;
1714                 i = 5;
1715         }
1716         else if(x & 0xFE && !(x & 0x01)) { /* 11111110 */
1717                 v = 0;
1718                 i = 6;
1719         }
1720         else {
1721                 *val = 0xffffffffffffffff;
1722                 return true;
1723         }
1724         for( ; i; i--) {
1725                 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
1726                         return false;
1727                 if(raw)
1728                         raw[(*rawlen)++] = (FLAC__byte)x;
1729                 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
1730                         *val = 0xffffffffffffffff;
1731                         return true;
1732                 }
1733                 v <<= 6;
1734                 v |= (x & 0x3F);
1735         }
1736         *val = v;
1737         return true;
1738 }
1739
1740 void FLAC__bitbuffer_dump(const FLAC__BitBuffer *bb, FILE *out)
1741 {
1742         unsigned i, j;
1743         if(bb == 0) {
1744                 fprintf(out, "bitbuffer is NULL\n");
1745         }
1746         else {
1747                 fprintf(out, "bitbuffer: capacity=%u bytes=%u bits=%u total_bits=%u consumed: bytes=%u, bits=%u, total_bits=%u\n", bb->capacity, bb->bytes, bb->bits, bb->total_bits, bb->consumed_bytes, bb->consumed_bits, bb->total_consumed_bits);
1748                 for(i = 0; i < bb->bytes; i++) {
1749                         fprintf(out, "%08X: ", i);
1750                         for(j = 0; j < 8; j++)
1751                                 if(i*8+j < bb->total_consumed_bits)
1752                                         fprintf(out, ".");
1753                                 else
1754                                         fprintf(out, "%01u", bb->buffer[i] & (1 << (8-j-1)) ? 1:0);
1755                         fprintf(out, "\n");
1756                 }
1757                 if(bb->bits > 0) {
1758                         fprintf(out, "%08X: ", i);
1759                         for(j = 0; j < bb->bits; j++)
1760                                 if(i*8+j < bb->total_consumed_bits)
1761                                         fprintf(out, ".");
1762                                 else
1763                                         fprintf(out, "%01u", bb->buffer[i] & (1 << (bb->bits-j-1)) ? 1:0);
1764                         fprintf(out, "\n");
1765                 }
1766         }
1767 }