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