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