fix bug when reading a zero-bit-long value
[platform/upstream/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         if(bits == 0) {
1081                 *val = 0;
1082                 return true;
1083         }
1084
1085         while(bb->total_consumed_bits + bits > bb->total_bits) {
1086                 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1087                         return false;
1088         }
1089         if(bb->consumed_bits) {
1090                 i = 8 - bb->consumed_bits;
1091                 if(i <= bits_) {
1092                         v = bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits);
1093                         bits_ -= i;
1094                         FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1095                         bb->consumed_bytes++;
1096                         bb->consumed_bits = 0;
1097                         /* we hold off updating bb->total_consumed_bits until the end */
1098                 }
1099                 else {
1100                         *val = (bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits)) >> (i-bits_);
1101                         bb->consumed_bits += bits_;
1102                         bb->total_consumed_bits += bits_;
1103                         return true;
1104                 }
1105         }
1106         while(bits_ >= 8) {
1107                 v <<= 8;
1108                 v |= bb->buffer[bb->consumed_bytes];
1109                 bits_ -= 8;
1110                 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1111                 bb->consumed_bytes++;
1112                 /* bb->consumed_bits is already 0 */
1113                 /* we hold off updating bb->total_consumed_bits until the end */
1114         }
1115         if(bits_ > 0) {
1116                 v <<= bits_;
1117                 v |= (bb->buffer[bb->consumed_bytes] >> (8-bits_));
1118                 bb->consumed_bits = bits_;
1119                 /* we hold off updating bb->total_consumed_bits until the end */
1120         }
1121         bb->total_consumed_bits += bits;
1122         *val = v;
1123         return true;
1124 }
1125 #endif
1126
1127 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)
1128 #ifdef FLAC__NO_MANUAL_INLINING
1129 {
1130         unsigned i;
1131         FLAC__uint32 v;
1132
1133         FLAC__ASSERT(bb != 0);
1134         FLAC__ASSERT(bb->buffer != 0);
1135
1136         FLAC__ASSERT(bits <= 32);
1137
1138         if(bits == 0) {
1139                 *val = 0;
1140                 return true;
1141         }
1142
1143         v = 0;
1144         for(i = 0; i < bits; i++) {
1145                 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &v, read_callback, client_data))
1146                         return false;
1147         }
1148
1149         /* fix the sign */
1150         i = 32 - bits;
1151         if(i) {
1152                 v <<= i;
1153                 *val = (FLAC__int32)v;
1154                 *val >>= i;
1155         }
1156         else
1157                 *val = (FLAC__int32)v;
1158
1159         return true;
1160 }
1161 #else
1162 {
1163         unsigned i, bits_ = bits;
1164         FLAC__uint32 v = 0;
1165
1166         FLAC__ASSERT(bb != 0);
1167         FLAC__ASSERT(bb->buffer != 0);
1168
1169         FLAC__ASSERT(bits <= 32);
1170         FLAC__ASSERT((bb->capacity*8) * 2 >= bits);
1171
1172         if(bits == 0) {
1173                 *val = 0;
1174                 return true;
1175         }
1176
1177         while(bb->total_consumed_bits + bits > bb->total_bits) {
1178                 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1179                         return false;
1180         }
1181         if(bb->consumed_bits) {
1182                 i = 8 - bb->consumed_bits;
1183                 if(i <= bits_) {
1184                         v = bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits);
1185                         bits_ -= i;
1186                         FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1187                         bb->consumed_bytes++;
1188                         bb->consumed_bits = 0;
1189                         /* we hold off updating bb->total_consumed_bits until the end */
1190                 }
1191                 else {
1192                         /* bits_ must be < 7 if we get to here */
1193                         v = (bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits));
1194                         v <<= (32-i);
1195                         *val = (FLAC__int32)v;
1196                         *val >>= (32-bits_);
1197                         bb->consumed_bits += bits_;
1198                         bb->total_consumed_bits += bits_;
1199                         return true;
1200                 }
1201         }
1202         while(bits_ >= 8) {
1203                 v <<= 8;
1204                 v |= bb->buffer[bb->consumed_bytes];
1205                 bits_ -= 8;
1206                 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1207                 bb->consumed_bytes++;
1208                 /* bb->consumed_bits is already 0 */
1209                 /* we hold off updating bb->total_consumed_bits until the end */
1210         }
1211         if(bits_ > 0) {
1212                 v <<= bits_;
1213                 v |= (bb->buffer[bb->consumed_bytes] >> (8-bits_));
1214                 bb->consumed_bits = bits_;
1215                 /* we hold off updating bb->total_consumed_bits until the end */
1216         }
1217         bb->total_consumed_bits += bits;
1218
1219         /* fix the sign */
1220         i = 32 - bits;
1221         if(i) {
1222                 v <<= i;
1223                 *val = (FLAC__int32)v;
1224                 *val >>= i;
1225         }
1226         else
1227                 *val = (FLAC__int32)v;
1228
1229         return true;
1230 }
1231 #endif
1232
1233 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)
1234 #ifdef FLAC__NO_MANUAL_INLINING
1235 {
1236         unsigned i;
1237
1238         FLAC__ASSERT(bb != 0);
1239         FLAC__ASSERT(bb->buffer != 0);
1240
1241         FLAC__ASSERT(bits <= 64);
1242
1243         *val = 0;
1244         for(i = 0; i < bits; i++) {
1245                 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, val, read_callback, client_data))
1246                         return false;
1247         }
1248         return true;
1249 }
1250 #else
1251 {
1252         unsigned i, bits_ = bits;
1253         FLAC__uint64 v = 0;
1254
1255         FLAC__ASSERT(bb != 0);
1256         FLAC__ASSERT(bb->buffer != 0);
1257
1258         FLAC__ASSERT(bits <= 64);
1259         FLAC__ASSERT((bb->capacity*8) * 2 >= bits);
1260
1261         if(bits == 0) {
1262                 *val = 0;
1263                 return true;
1264         }
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         if(bits == 0) {
1320                 *val = 0;
1321                 return true;
1322         }
1323
1324         v = 0;
1325         for(i = 0; i < bits; i++) {
1326                 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, &v, read_callback, client_data))
1327                         return false;
1328         }
1329         /* fix the sign */
1330         i = 64 - bits;
1331         if(i) {
1332                 v <<= i;
1333                 *val = (FLAC__int64)v;
1334                 *val >>= i;
1335         }
1336         else
1337                 *val = (FLAC__int64)v;
1338
1339         return true;
1340 }
1341 #else
1342 {
1343         unsigned i, bits_ = bits;
1344         FLAC__uint64 v = 0;
1345
1346         FLAC__ASSERT(bb != 0);
1347         FLAC__ASSERT(bb->buffer != 0);
1348
1349         FLAC__ASSERT(bits <= 64);
1350         FLAC__ASSERT((bb->capacity*8) * 2 >= bits);
1351
1352         if(bits == 0) {
1353                 *val = 0;
1354                 return true;
1355         }
1356
1357         while(bb->total_consumed_bits + bits > bb->total_bits) {
1358                 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1359                         return false;
1360         }
1361         if(bb->consumed_bits) {
1362                 i = 8 - bb->consumed_bits;
1363                 if(i <= bits_) {
1364                         v = bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits);
1365                         bits_ -= i;
1366                         FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1367                         bb->consumed_bytes++;
1368                         bb->consumed_bits = 0;
1369                         /* we hold off updating bb->total_consumed_bits until the end */
1370                 }
1371                 else {
1372                         /* bits_ must be < 7 if we get to here */
1373                         v = (bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits));
1374                         v <<= (64-i);
1375                         *val = (FLAC__int64)v;
1376                         *val >>= (64-bits_);
1377                         bb->consumed_bits += bits_;
1378                         bb->total_consumed_bits += bits_;
1379                         return true;
1380                 }
1381         }
1382         while(bits_ >= 8) {
1383                 v <<= 8;
1384                 v |= bb->buffer[bb->consumed_bytes];
1385                 bits_ -= 8;
1386                 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1387                 bb->consumed_bytes++;
1388                 /* bb->consumed_bits is already 0 */
1389                 /* we hold off updating bb->total_consumed_bits until the end */
1390         }
1391         if(bits_ > 0) {
1392                 v <<= bits_;
1393                 v |= (bb->buffer[bb->consumed_bytes] >> (8-bits_));
1394                 bb->consumed_bits = bits_;
1395                 /* we hold off updating bb->total_consumed_bits until the end */
1396         }
1397         bb->total_consumed_bits += bits;
1398
1399         /* fix the sign */
1400         i = 64 - bits;
1401         if(i) {
1402                 v <<= i;
1403                 *val = (FLAC__int64)v;
1404                 *val >>= i;
1405         }
1406         else
1407                 *val = (FLAC__int64)v;
1408
1409         return true;
1410 }
1411 #endif
1412
1413 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)
1414 #ifdef FLAC__NO_MANUAL_INLINING
1415 {
1416         unsigned bit, val_ = 0;
1417
1418         FLAC__ASSERT(bb != 0);
1419         FLAC__ASSERT(bb->buffer != 0);
1420
1421         while(1) {
1422                 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1423                         return false;
1424                 if(bit)
1425                         break;
1426                 else
1427                         val_++;
1428         }
1429         *val = val_;
1430         return true;
1431 }
1432 #else
1433 {
1434         unsigned i, val_ = 0;
1435         unsigned total_bytes_ = (bb->total_bits + 7) / 8;
1436         FLAC__byte b;
1437
1438         FLAC__ASSERT(bb != 0);
1439         FLAC__ASSERT(bb->buffer != 0);
1440
1441         if(bb->consumed_bits) {
1442                 b = bb->buffer[bb->consumed_bytes] << bb->consumed_bits;
1443                 if(b) {
1444                         for(i = 0; !(b & 0x80); i++)
1445                                 b <<= 1;
1446                         *val = i;
1447                         i++;
1448                         bb->consumed_bits += i;
1449                         bb->total_consumed_bits += i;
1450                         if(bb->consumed_bits == 8) {
1451                                 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1452                                 bb->consumed_bytes++;
1453                                 bb->consumed_bits = 0;
1454                         }
1455                         return true;
1456                 }
1457                 else {
1458                         val_ = 8 - bb->consumed_bits;
1459                         FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1460                         bb->consumed_bytes++;
1461                         bb->consumed_bits = 0;
1462                         bb->total_consumed_bits += val_;
1463                 }
1464         }
1465         while(1) {
1466                 if(bb->consumed_bytes >= total_bytes_) {
1467                         if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1468                                 return false;
1469                         total_bytes_ = (bb->total_bits + 7) / 8;
1470                 }
1471                 b = bb->buffer[bb->consumed_bytes];
1472                 if(b) {
1473                         for(i = 0; !(b & 0x80); i++)
1474                                 b <<= 1;
1475                         val_ += i;
1476                         i++;
1477                         bb->consumed_bits = i;
1478                         *val = val_;
1479                         if(i == 8) {
1480                                 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1481                                 bb->consumed_bytes++;
1482                                 bb->consumed_bits = 0;
1483                         }
1484                         bb->total_consumed_bits += i;
1485                         return true;
1486                 }
1487                 else {
1488                         val_ += 8;
1489                         FLAC__CRC16_UPDATE(0, bb->read_crc16);
1490                         bb->consumed_bytes++;
1491                         /* bb->consumed_bits is already 0 */
1492                         /* we hold off updating bb->total_consumed_bits until the end */
1493                         bb->total_consumed_bits += 8;
1494                 }
1495         }
1496 }
1497 #endif
1498
1499 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)
1500 {
1501         FLAC__uint32 sign = 0, lsbs = 0, msbs = 0;
1502
1503         FLAC__ASSERT(bb != 0);
1504         FLAC__ASSERT(bb->buffer != 0);
1505         FLAC__ASSERT(parameter <= 31);
1506
1507         /* read the unary MSBs and end bit */
1508         if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
1509                 return false;
1510
1511         /* read the sign bit */
1512         if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &sign, read_callback, client_data))
1513                 return false;
1514
1515         /* read the binary LSBs */
1516         if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, parameter, read_callback, client_data))
1517                 return false;
1518
1519         /* compose the value */
1520         *val = (msbs << parameter) | lsbs;
1521         if(sign)
1522                 *val = -(*val);
1523
1524         return true;
1525 }
1526
1527 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)
1528 {
1529         FLAC__uint32 lsbs = 0, msbs = 0;
1530         unsigned uval;
1531
1532         FLAC__ASSERT(bb != 0);
1533         FLAC__ASSERT(bb->buffer != 0);
1534         FLAC__ASSERT(parameter <= 31);
1535
1536         /* read the unary MSBs and end bit */
1537         if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
1538                 return false;
1539
1540         /* read the binary LSBs */
1541         if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, parameter, read_callback, client_data))
1542                 return false;
1543
1544         /* compose the value */
1545         uval = (msbs << parameter) | lsbs;
1546         if(uval & 1)
1547                 *val = -((int)(uval >> 1)) - 1;
1548         else
1549                 *val = (int)(uval >> 1);
1550
1551         return true;
1552 }
1553
1554 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)
1555 {
1556         FLAC__uint32 lsbs = 0, msbs = 0;
1557         unsigned bit, uval, k;
1558
1559         FLAC__ASSERT(bb != 0);
1560         FLAC__ASSERT(bb->buffer != 0);
1561
1562         k = FLAC__bitmath_ilog2(parameter);
1563
1564         /* read the unary MSBs and end bit */
1565         if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
1566                 return false;
1567
1568         /* read the binary LSBs */
1569         if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, k, read_callback, client_data))
1570                 return false;
1571
1572         if(parameter == 1u<<k) {
1573                 /* compose the value */
1574                 uval = (msbs << k) | lsbs;
1575         }
1576         else {
1577                 unsigned d = (1 << (k+1)) - parameter;
1578                 if(lsbs >= d) {
1579                         if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1580                                 return false;
1581                         lsbs <<= 1;
1582                         lsbs |= bit;
1583                         lsbs -= d;
1584                 }
1585                 /* compose the value */
1586                 uval = msbs * parameter + lsbs;
1587         }
1588
1589         /* unfold unsigned to signed */
1590         if(uval & 1)
1591                 *val = -((int)(uval >> 1)) - 1;
1592         else
1593                 *val = (int)(uval >> 1);
1594
1595         return true;
1596 }
1597
1598 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)
1599 {
1600         FLAC__uint32 lsbs, msbs = 0;
1601         unsigned bit, k;
1602
1603         FLAC__ASSERT(bb != 0);
1604         FLAC__ASSERT(bb->buffer != 0);
1605
1606         k = FLAC__bitmath_ilog2(parameter);
1607
1608         /* read the unary MSBs and end bit */
1609         if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
1610                 return false;
1611
1612         /* read the binary LSBs */
1613         if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, k, read_callback, client_data))
1614                 return false;
1615
1616         if(parameter == 1u<<k) {
1617                 /* compose the value */
1618                 *val = (msbs << k) | lsbs;
1619         }
1620         else {
1621                 unsigned d = (1 << (k+1)) - parameter;
1622                 if(lsbs >= d) {
1623                         if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1624                                 return false;
1625                         lsbs <<= 1;
1626                         lsbs |= bit;
1627                         lsbs -= d;
1628                 }
1629                 /* compose the value */
1630                 *val = msbs * parameter + lsbs;
1631         }
1632
1633         return true;
1634 }
1635
1636 /* on return, if *val == 0xffffffff then the utf-8 sequence was invalid, but the return value will be true */
1637 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)
1638 {
1639         FLAC__uint32 v = 0;
1640         FLAC__uint32 x;
1641         unsigned i;
1642
1643         if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
1644                 return false;
1645         if(raw)
1646                 raw[(*rawlen)++] = (FLAC__byte)x;
1647         if(!(x & 0x80)) { /* 0xxxxxxx */
1648                 v = x;
1649                 i = 0;
1650         }
1651         else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
1652                 v = x & 0x1F;
1653                 i = 1;
1654         }
1655         else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
1656                 v = x & 0x0F;
1657                 i = 2;
1658         }
1659         else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
1660                 v = x & 0x07;
1661                 i = 3;
1662         }
1663         else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
1664                 v = x & 0x03;
1665                 i = 4;
1666         }
1667         else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
1668                 v = x & 0x01;
1669                 i = 5;
1670         }
1671         else {
1672                 *val = 0xffffffff;
1673                 return true;
1674         }
1675         for( ; i; i--) {
1676                 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
1677                         return false;
1678                 if(raw)
1679                         raw[(*rawlen)++] = (FLAC__byte)x;
1680                 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
1681                         *val = 0xffffffff;
1682                         return true;
1683                 }
1684                 v <<= 6;
1685                 v |= (x & 0x3F);
1686         }
1687         *val = v;
1688         return true;
1689 }
1690
1691 /* on return, if *val == 0xffffffffffffffff then the utf-8 sequence was invalid, but the return value will be true */
1692 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)
1693 {
1694         FLAC__uint64 v = 0;
1695         FLAC__uint32 x;
1696         unsigned i;
1697
1698         if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
1699                 return false;
1700         if(raw)
1701                 raw[(*rawlen)++] = (FLAC__byte)x;
1702         if(!(x & 0x80)) { /* 0xxxxxxx */
1703                 v = x;
1704                 i = 0;
1705         }
1706         else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
1707                 v = x & 0x1F;
1708                 i = 1;
1709         }
1710         else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
1711                 v = x & 0x0F;
1712                 i = 2;
1713         }
1714         else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
1715                 v = x & 0x07;
1716                 i = 3;
1717         }
1718         else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
1719                 v = x & 0x03;
1720                 i = 4;
1721         }
1722         else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
1723                 v = x & 0x01;
1724                 i = 5;
1725         }
1726         else if(x & 0xFE && !(x & 0x01)) { /* 11111110 */
1727                 v = 0;
1728                 i = 6;
1729         }
1730         else {
1731                 *val = 0xffffffffffffffff;
1732                 return true;
1733         }
1734         for( ; i; i--) {
1735                 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
1736                         return false;
1737                 if(raw)
1738                         raw[(*rawlen)++] = (FLAC__byte)x;
1739                 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
1740                         *val = 0xffffffffffffffff;
1741                         return true;
1742                 }
1743                 v <<= 6;
1744                 v |= (x & 0x3F);
1745         }
1746         *val = v;
1747         return true;
1748 }
1749
1750 void FLAC__bitbuffer_dump(const FLAC__BitBuffer *bb, FILE *out)
1751 {
1752         unsigned i, j;
1753         if(bb == 0) {
1754                 fprintf(out, "bitbuffer is NULL\n");
1755         }
1756         else {
1757                 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);
1758                 for(i = 0; i < bb->bytes; i++) {
1759                         fprintf(out, "%08X: ", i);
1760                         for(j = 0; j < 8; j++)
1761                                 if(i*8+j < bb->total_consumed_bits)
1762                                         fprintf(out, ".");
1763                                 else
1764                                         fprintf(out, "%01u", bb->buffer[i] & (1 << (8-j-1)) ? 1:0);
1765                         fprintf(out, "\n");
1766                 }
1767                 if(bb->bits > 0) {
1768                         fprintf(out, "%08X: ", i);
1769                         for(j = 0; j < bb->bits; j++)
1770                                 if(i*8+j < bb->total_consumed_bits)
1771                                         fprintf(out, ".");
1772                                 else
1773                                         fprintf(out, "%01u", bb->buffer[i] & (1 << (bb->bits-j-1)) ? 1:0);
1774                         fprintf(out, "\n");
1775                 }
1776         }
1777 }