set large default size for bug 131976 workaround
[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 <assert.h>
21 #include <stdlib.h> /* for malloc() */
22 #include <string.h> /* for memcpy(), memset() */
23 #include "private/bitbuffer.h"
24
25 /* WATCHOUT: this size here is a workaround due to a bug in the
26  * verify_read_callback in flac/encode.c, c.f.
27  *
28  *     http://sourceforge.net/bugs/?func=detailbug&bug_id=131976&group_id=13478
29  *
30  * When that bug is fixed the number here can be set back to a
31  * reasonable level.
32  */
33 static const unsigned FLAC__BITBUFFER_DEFAULT_CAPACITY = 300000; /* bytes */
34
35 #ifdef min
36 #undef min
37 #endif
38 #define min(x,y) ((x)<(y)?(x):(y))
39 #ifdef max
40 #undef max
41 #endif
42 #define max(x,y) ((x)>(y)?(x):(y))
43
44 static unsigned ilog2_(unsigned v)
45 {
46         unsigned l = 0;
47         assert(v > 0);
48         while(v >>= 1)
49                 l++;
50         return l;
51 }
52
53 static bool bitbuffer_resize_(FLAC__BitBuffer *bb, unsigned new_capacity)
54 {
55         byte *new_buffer;
56
57         assert(bb != 0);
58         assert(bb->buffer != 0);
59
60         if(bb->capacity == new_capacity)
61                 return true;
62
63         new_buffer = (byte*)malloc(sizeof(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(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 bool bitbuffer_grow_(FLAC__BitBuffer *bb, unsigned min_bytes_to_add)
84 {
85         unsigned new_capacity;
86
87         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 bool bitbuffer_ensure_size_(FLAC__BitBuffer *bb, unsigned bits_to_add)
94 {
95         assert(bb != 0);
96         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 bool bitbuffer_read_from_client_(FLAC__BitBuffer *bb, bool (*read_callback)(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; if OK, go back to read_bit_, else fail */
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         assert(bb != 0);
136         bb->buffer = 0;
137         bb->capacity = 0;
138         bb->bytes = bb->bits = bb->total_bits = 0;
139         bb->consumed_bytes = bb->consumed_bits = bb->total_consumed_bits = 0;
140 }
141
142 bool FLAC__bitbuffer_init_from(FLAC__BitBuffer *bb, const byte buffer[], unsigned bytes)
143 {
144         assert(bb != 0);
145         FLAC__bitbuffer_init(bb);
146         if(bytes == 0)
147                 return true;
148         else {
149                 assert(buffer != 0);
150                 bb->buffer = (byte*)malloc(sizeof(byte)*bytes);
151                 if(bb->buffer == 0)
152                         return false;
153                 memcpy(bb->buffer, buffer, sizeof(byte)*bytes);
154                 bb->capacity = bb->bytes = bytes;
155                 bb->bits = 0;
156                 bb->total_bits = (bytes<<3);
157                 bb->consumed_bytes = bb->consumed_bits = bb->total_consumed_bits = 0;
158                 return true;
159         }
160 }
161
162 bool FLAC__bitbuffer_concatenate_aligned(FLAC__BitBuffer *dest, const FLAC__BitBuffer *src)
163 {
164         static byte mask_[9] = { 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff };
165         unsigned bits_to_add = src->total_bits - src->total_consumed_bits;
166         assert(dest != 0);
167         assert(src != 0);
168
169         if(bits_to_add == 0)
170                 return true;
171         if(dest->bits != src->consumed_bits)
172                 return false;
173         if(!bitbuffer_ensure_size_(dest, bits_to_add))
174                 return false;
175         if(dest->bits == 0) {
176                 memcpy(dest->buffer+dest->bytes, src->buffer+src->consumed_bytes, src->bytes-src->consumed_bytes + ((src->bits)? 1:0));
177         }
178         else if(dest->bits + bits_to_add > 8) {
179                 dest->buffer[dest->bytes] <<= (8 - dest->bits);
180                 dest->buffer[dest->bytes] |= (src->buffer[src->consumed_bytes] & mask_[8-dest->bits]);
181                 memcpy(dest->buffer+dest->bytes+1, src->buffer+src->consumed_bytes+1, src->bytes-src->consumed_bytes-1 + ((src->bits)? 1:0));
182         }
183         else {
184                 dest->buffer[dest->bytes] <<= bits_to_add;
185                 dest->buffer[dest->bytes] |= (src->buffer[src->consumed_bytes] & mask_[bits_to_add]);
186         }
187         dest->bits = src->bits;
188         dest->total_bits += bits_to_add;
189         dest->bytes = dest->total_bits / 8;
190
191         return true;
192 }
193
194 void FLAC__bitbuffer_free(FLAC__BitBuffer *bb)
195 {
196         assert(bb != 0);
197         if(bb->buffer != 0)
198                 free(bb->buffer);
199         bb->buffer = 0;
200         bb->capacity = 0;
201         bb->bytes = bb->bits = bb->total_bits = 0;
202         bb->consumed_bytes = bb->consumed_bits = bb->total_consumed_bits = 0;
203 }
204
205 bool FLAC__bitbuffer_clear(FLAC__BitBuffer *bb)
206 {
207         if(bb->buffer == 0) {
208                 bb->capacity = FLAC__BITBUFFER_DEFAULT_CAPACITY;
209                 bb->buffer = (byte*)malloc(sizeof(byte) * bb->capacity);
210                 if(bb->buffer == 0)
211                         return false;
212                 memset(bb->buffer, 0, bb->capacity);
213         }
214         else {
215                 memset(bb->buffer, 0, bb->bytes + (bb->bits?1:0));
216         }
217         bb->bytes = bb->bits = bb->total_bits = 0;
218         bb->consumed_bytes = bb->consumed_bits = bb->total_consumed_bits = 0;
219         return true;
220 }
221
222 bool FLAC__bitbuffer_clone(FLAC__BitBuffer *dest, const FLAC__BitBuffer *src)
223 {
224         if(dest->capacity < src->capacity)
225                 if(!bitbuffer_resize_(dest, src->capacity))
226                         return false;
227         memcpy(dest->buffer, src->buffer, sizeof(byte)*min(src->capacity, src->bytes+1));
228         dest->bytes = src->bytes;
229         dest->bits = src->bits;
230         dest->total_bits = src->total_bits;
231         dest->consumed_bytes = src->consumed_bytes;
232         dest->consumed_bits = src->consumed_bits;
233         dest->total_consumed_bits = src->total_consumed_bits;
234         return true;
235 }
236
237 bool FLAC__bitbuffer_write_zeroes(FLAC__BitBuffer *bb, unsigned bits)
238 {
239         unsigned n, k;
240
241         assert(bb != 0);
242         assert(bb->buffer != 0);
243
244         if(bits == 0)
245                 return true;
246         if(!bitbuffer_ensure_size_(bb, bits))
247                 return false;
248         bb->total_bits += bits;
249         while(bits > 0) {
250                 n = min(8 - bb->bits, bits);
251                 k = bits - n;
252                 bb->buffer[bb->bytes] <<= n;
253                 bits -= n;
254                 bb->bits += n;
255                 if(bb->bits == 8) {
256                         bb->bytes++;
257                         bb->bits = 0;
258                 }
259         }
260         return true;
261 }
262
263 bool FLAC__bitbuffer_write_raw_uint32(FLAC__BitBuffer *bb, uint32 val, unsigned bits)
264 {
265         static uint32 mask[] = {
266                 0,
267                 0x00000001, 0x00000003, 0x00000007, 0x0000000F,
268                 0x0000001F, 0x0000003F, 0x0000007F, 0x000000FF,
269                 0x000001FF, 0x000003FF, 0x000007FF, 0x00000FFF,
270                 0x00001FFF, 0x00003FFF, 0x00007FFF, 0x0000FFFF,
271                 0x0001FFFF, 0x0003FFFF, 0x0007FFFF, 0x000FFFFF,
272                 0x001FFFFF, 0x003FFFFF, 0x007FFFFF, 0x00FFFFFF,
273                 0x01FFFFFF, 0x03FFFFFF, 0x07FFFFFF, 0x0FFFFFFF,
274                 0x1FFFFFFF, 0x3FFFFFFF, 0x7FFFFFFF, 0xFFFFFFFF
275         };
276         unsigned n, k;
277
278         assert(bb != 0);
279         assert(bb->buffer != 0);
280
281         assert(bits <= 32);
282         if(bits == 0)
283                 return true;
284         if(!bitbuffer_ensure_size_(bb, bits))
285                 return false;
286         val &= mask[bits];
287         bb->total_bits += bits;
288         while(bits > 0) {
289                 n = 8 - bb->bits;
290                 if(n == 8) { /* i.e. bb->bits == 0 */
291                         if(bits < 8) {
292                                 bb->buffer[bb->bytes] = val;
293                                 bb->bits = bits;
294                                 break;
295                         }
296                         else if(bits == 8) {
297                                 bb->buffer[bb->bytes++] = val;
298                                 break;
299                         }
300                         else {
301                                 k = bits - 8;
302                                 bb->buffer[bb->bytes++] = val >> k;
303                                 val &= (~(0xffffffff << k));
304                                 bits -= 8;
305                         }
306                 }
307                 else if(bits <= n) {
308                         bb->buffer[bb->bytes] <<= bits;
309                         bb->buffer[bb->bytes] |= val;
310                         if(bits == n) {
311                                 bb->bytes++;
312                                 bb->bits = 0;
313                         }
314                         else
315                                 bb->bits += bits;
316                         break;
317                 }
318                 else {
319                         k = bits - n;
320                         bb->buffer[bb->bytes] <<= n;
321                         bb->buffer[bb->bytes] |= (val>>k);
322                         val &= (~(0xffffffff << k));
323                         bits -= n;
324                         bb->bytes++;
325                         bb->bits = 0;
326                 }
327         }
328
329         return true;
330 }
331
332 bool FLAC__bitbuffer_write_raw_int32(FLAC__BitBuffer *bb, int32 val, unsigned bits)
333 {
334         return FLAC__bitbuffer_write_raw_uint32(bb, (uint32)val, bits);
335 }
336
337 bool FLAC__bitbuffer_write_raw_uint64(FLAC__BitBuffer *bb, uint64 val, unsigned bits)
338 {
339         static uint64 mask[] = {
340                 0,
341                 0x0000000000000001, 0x0000000000000003, 0x0000000000000007, 0x000000000000000F,
342                 0x000000000000001F, 0x000000000000003F, 0x000000000000007F, 0x00000000000000FF,
343                 0x00000000000001FF, 0x00000000000003FF, 0x00000000000007FF, 0x0000000000000FFF,
344                 0x0000000000001FFF, 0x0000000000003FFF, 0x0000000000007FFF, 0x000000000000FFFF,
345                 0x000000000001FFFF, 0x000000000003FFFF, 0x000000000007FFFF, 0x00000000000FFFFF,
346                 0x00000000001FFFFF, 0x00000000003FFFFF, 0x00000000007FFFFF, 0x0000000000FFFFFF,
347                 0x0000000001FFFFFF, 0x0000000003FFFFFF, 0x0000000007FFFFFF, 0x000000000FFFFFFF,
348                 0x000000001FFFFFFF, 0x000000003FFFFFFF, 0x000000007FFFFFFF, 0x00000000FFFFFFFF,
349                 0x00000001FFFFFFFF, 0x00000003FFFFFFFF, 0x00000007FFFFFFFF, 0x0000000FFFFFFFFF,
350                 0x0000001FFFFFFFFF, 0x0000003FFFFFFFFF, 0x0000007FFFFFFFFF, 0x000000FFFFFFFFFF,
351                 0x000001FFFFFFFFFF, 0x000003FFFFFFFFFF, 0x000007FFFFFFFFFF, 0x00000FFFFFFFFFFF,
352                 0x00001FFFFFFFFFFF, 0x00003FFFFFFFFFFF, 0x00007FFFFFFFFFFF, 0x0000FFFFFFFFFFFF,
353                 0x0001FFFFFFFFFFFF, 0x0003FFFFFFFFFFFF, 0x0007FFFFFFFFFFFF, 0x000FFFFFFFFFFFFF,
354                 0x001FFFFFFFFFFFFF, 0x003FFFFFFFFFFFFF, 0x007FFFFFFFFFFFFF, 0x00FFFFFFFFFFFFFF,
355                 0x01FFFFFFFFFFFFFF, 0x03FFFFFFFFFFFFFF, 0x07FFFFFFFFFFFFFF, 0x0FFFFFFFFFFFFFFF,
356                 0x1FFFFFFFFFFFFFFF, 0x3FFFFFFFFFFFFFFF, 0x7FFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF
357         };
358         unsigned n, k;
359
360         assert(bb != 0);
361         assert(bb->buffer != 0);
362
363         assert(bits <= 64);
364         if(bits == 0)
365                 return true;
366         if(!bitbuffer_ensure_size_(bb, bits))
367                 return false;
368         val &= mask[bits];
369         bb->total_bits += bits;
370         while(bits > 0) {
371                 if(bb->bits == 0) {
372                         if(bits < 8) {
373                                 bb->buffer[bb->bytes] = val;
374                                 bb->bits = bits;
375                                 break;
376                         }
377                         else if(bits == 8) {
378                                 bb->buffer[bb->bytes++] = val;
379                                 break;
380                         }
381                         else {
382                                 k = bits - 8;
383                                 bb->buffer[bb->bytes++] = val >> k;
384                                 val &= (~(0xffffffffffffffff << k));
385                                 bits -= 8;
386                         }
387                 }
388                 else {
389                         n = min(8 - bb->bits, bits);
390                         k = bits - n;
391                         bb->buffer[bb->bytes] <<= n;
392                         bb->buffer[bb->bytes] |= (val>>k);
393                         val &= (~(0xffffffffffffffff << k));
394                         bits -= n;
395                         bb->bits += n;
396                         if(bb->bits == 8) {
397                                 bb->bytes++;
398                                 bb->bits = 0;
399                         }
400                 }
401         }
402
403         return true;
404 }
405
406 bool FLAC__bitbuffer_write_raw_int64(FLAC__BitBuffer *bb, int64 val, unsigned bits)
407 {
408         return FLAC__bitbuffer_write_raw_uint64(bb, (uint64)val, bits);
409 }
410
411 unsigned FLAC__bitbuffer_rice_bits(int val, unsigned parameter)
412 {
413         unsigned msbs, uval;
414
415         /* convert signed to unsigned */
416         if(val < 0)
417                 /* equivalent to
418                  *     (unsigned)(((--val) << 1) - 1);
419                  * but without the overflow problem at -MAXINT
420                  */
421                 uval = (unsigned)(((-(++val)) << 1) + 1);
422         else
423                 uval = (unsigned)(val << 1);
424
425         msbs = uval >> parameter;
426
427         return 1 + parameter + msbs;
428 }
429
430 unsigned FLAC__bitbuffer_golomb_bits_signed(int val, unsigned parameter)
431 {
432         unsigned bits, msbs, uval;
433         unsigned k;
434
435         assert(parameter > 0);
436
437         /* convert signed to unsigned */
438         if(val < 0)
439                 /* equivalent to
440                  *     (unsigned)(((--val) << 1) - 1);
441                  * but without the overflow problem at -MAXINT
442                  */
443                 uval = (unsigned)(((-(++val)) << 1) + 1);
444         else
445                 uval = (unsigned)(val << 1);
446
447         k = ilog2_(parameter);
448         if(parameter == 1u<<k) {
449                 assert(k <= 30);
450
451                 msbs = uval >> k;
452                 bits = 1 + k + msbs;
453         }
454         else {
455                 unsigned q, r, d;
456
457                 d = (1 << (k+1)) - parameter;
458                 q = uval / parameter;
459                 r = uval - (q * parameter);
460
461                 bits = 1 + q + k;
462                 if(r >= d)
463                         bits++;
464         }
465         return bits;
466 }
467
468 unsigned FLAC__bitbuffer_golomb_bits_unsigned(unsigned uval, unsigned parameter)
469 {
470         unsigned bits, msbs;
471         unsigned k;
472
473         assert(parameter > 0);
474
475         k = ilog2_(parameter);
476         if(parameter == 1u<<k) {
477                 assert(k <= 30);
478
479                 msbs = uval >> k;
480                 bits = 1 + k + msbs;
481         }
482         else {
483                 unsigned q, r, d;
484
485                 d = (1 << (k+1)) - parameter;
486                 q = uval / parameter;
487                 r = uval - (q * parameter);
488
489                 bits = 1 + q + k;
490                 if(r >= d)
491                         bits++;
492         }
493         return bits;
494 }
495
496 bool FLAC__bitbuffer_write_rice_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
497 {
498         unsigned bits, msbs, uval;
499         uint32 pattern;
500
501         assert(bb != 0);
502         assert(bb->buffer != 0);
503         assert(parameter <= 30);
504
505         /* convert signed to unsigned */
506         if(val < 0)
507                 /* equivalent to
508                  *     (unsigned)(((--val) << 1) - 1);
509                  * but without the overflow problem at -MAXINT
510                  */
511                 uval = (unsigned)(((-(++val)) << 1) + 1);
512         else
513                 uval = (unsigned)(val << 1);
514
515         msbs = uval >> parameter;
516         bits = 1 + parameter + msbs;
517         pattern = 1 << parameter; /* the unary end bit */
518         pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
519
520         if(bits <= 32) {
521                 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, bits))
522                         return false;
523         }
524         else {
525                 /* write the unary MSBs */
526                 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
527                         return false;
528                 /* write the unary end bit and binary LSBs */
529                 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, parameter+1))
530                         return false;
531         }
532         return true;
533 }
534
535 bool FLAC__bitbuffer_write_rice_signed_guarded(FLAC__BitBuffer *bb, int val, unsigned parameter, unsigned max_bits, bool *overflow)
536 {
537         unsigned bits, msbs, uval;
538         uint32 pattern;
539
540         assert(bb != 0);
541         assert(bb->buffer != 0);
542         assert(parameter <= 30);
543
544         *overflow = false;
545
546         /* convert signed to unsigned */
547         if(val < 0)
548                 /* equivalent to
549                  *     (unsigned)(((--val) << 1) - 1);
550                  * but without the overflow problem at -MAXINT
551                  */
552                 uval = (unsigned)(((-(++val)) << 1) + 1);
553         else
554                 uval = (unsigned)(val << 1);
555
556         msbs = uval >> parameter;
557         bits = 1 + parameter + msbs;
558         pattern = 1 << parameter; /* the unary end bit */
559         pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
560
561         if(bits <= 32) {
562                 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, bits))
563                         return false;
564         }
565         else if(bits > max_bits) {
566                 *overflow = true;
567                 return true;
568         }
569         else {
570                 /* write the unary MSBs */
571                 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
572                         return false;
573                 /* write the unary end bit and binary LSBs */
574                 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, parameter+1))
575                         return false;
576         }
577         return true;
578 }
579
580 bool FLAC__bitbuffer_write_golomb_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
581 {
582         unsigned bits, msbs, uval;
583         unsigned k;
584
585         assert(bb != 0);
586         assert(bb->buffer != 0);
587         assert(parameter > 0);
588
589         /* convert signed to unsigned */
590         if(val < 0)
591                 /* equivalent to
592                  *     (unsigned)(((--val) << 1) - 1);
593                  * but without the overflow problem at -MAXINT
594                  */
595                 uval = (unsigned)(((-(++val)) << 1) + 1);
596         else
597                 uval = (unsigned)(val << 1);
598
599         k = ilog2_(parameter);
600         if(parameter == 1u<<k) {
601                 unsigned pattern;
602
603                 assert(k <= 30);
604
605                 msbs = uval >> k;
606                 bits = 1 + k + msbs;
607                 pattern = 1 << k; /* the unary end bit */
608                 pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
609
610                 if(bits <= 32) {
611                         if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, bits))
612                                 return false;
613                 }
614                 else {
615                         /* write the unary MSBs */
616                         if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
617                                 return false;
618                         /* write the unary end bit and binary LSBs */
619                         if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, k+1))
620                                 return false;
621                 }
622         }
623         else {
624                 unsigned q, r, d;
625
626                 d = (1 << (k+1)) - parameter;
627                 q = uval / parameter;
628                 r = uval - (q * parameter);
629                 /* write the unary MSBs */
630                 if(!FLAC__bitbuffer_write_zeroes(bb, q))
631                         return false;
632                 /* write the unary end bit */
633                 if(!FLAC__bitbuffer_write_raw_uint32(bb, 1, 1))
634                         return false;
635                 /* write the binary LSBs */
636                 if(r >= d) {
637                         if(!FLAC__bitbuffer_write_raw_uint32(bb, r+d, k+1))
638                                 return false;
639                 }
640                 else {
641                         if(!FLAC__bitbuffer_write_raw_uint32(bb, r, k))
642                                 return false;
643                 }
644         }
645         return true;
646 }
647
648 bool FLAC__bitbuffer_write_golomb_unsigned(FLAC__BitBuffer *bb, unsigned uval, unsigned parameter)
649 {
650         unsigned bits, msbs;
651         unsigned k;
652
653         assert(bb != 0);
654         assert(bb->buffer != 0);
655         assert(parameter > 0);
656
657         k = ilog2_(parameter);
658         if(parameter == 1u<<k) {
659                 unsigned pattern;
660
661                 assert(k <= 30);
662
663                 msbs = uval >> k;
664                 bits = 1 + k + msbs;
665                 pattern = 1 << k; /* the unary end bit */
666                 pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
667
668                 if(bits <= 32) {
669                         if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, bits))
670                                 return false;
671                 }
672                 else {
673                         /* write the unary MSBs */
674                         if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
675                                 return false;
676                         /* write the unary end bit and binary LSBs */
677                         if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, k+1))
678                                 return false;
679                 }
680         }
681         else {
682                 unsigned q, r, d;
683
684                 d = (1 << (k+1)) - parameter;
685                 q = uval / parameter;
686                 r = uval - (q * parameter);
687                 /* write the unary MSBs */
688                 if(!FLAC__bitbuffer_write_zeroes(bb, q))
689                         return false;
690                 /* write the unary end bit */
691                 if(!FLAC__bitbuffer_write_raw_uint32(bb, 1, 1))
692                         return false;
693                 /* write the binary LSBs */
694                 if(r >= d) {
695                         if(!FLAC__bitbuffer_write_raw_uint32(bb, r+d, k+1))
696                                 return false;
697                 }
698                 else {
699                         if(!FLAC__bitbuffer_write_raw_uint32(bb, r, k))
700                                 return false;
701                 }
702         }
703         return true;
704 }
705
706 bool FLAC__bitbuffer_write_utf8_uint32(FLAC__BitBuffer *bb, uint32 val)
707 {
708         bool ok = 1;
709
710         assert(bb != 0);
711         assert(bb->buffer != 0);
712
713         assert(!(val & 0x80000000)); /* this version only handles 31 bits */
714
715         if(val < 0x80) {
716                 return FLAC__bitbuffer_write_raw_uint32(bb, val, 8);
717         }
718         else if(val < 0x800) {
719                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xC0 | (val>>6), 8);
720                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
721         }
722         else if(val < 0x10000) {
723                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xE0 | (val>>12), 8);
724                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
725                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
726         }
727         else if(val < 0x200000) {
728                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF0 | (val>>18), 8);
729                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
730                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
731                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
732         }
733         else if(val < 0x4000000) {
734                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF8 | (val>>24), 8);
735                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>18)&0x3F), 8);
736                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
737                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
738                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
739         }
740         else {
741                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFC | (val>>30), 8);
742                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>24)&0x3F), 8);
743                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>18)&0x3F), 8);
744                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
745                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
746                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
747         }
748
749         return ok;
750 }
751
752 bool FLAC__bitbuffer_write_utf8_uint64(FLAC__BitBuffer *bb, uint64 val)
753 {
754         bool ok = 1;
755
756         assert(bb != 0);
757         assert(bb->buffer != 0);
758
759         assert(!(val & 0xFFFFFFF000000000)); /* this version only handles 36 bits */
760
761         if(val < 0x80) {
762                 return FLAC__bitbuffer_write_raw_uint32(bb, (uint32)val, 8);
763         }
764         else if(val < 0x800) {
765                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xC0 | (uint32)(val>>6), 8);
766                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
767         }
768         else if(val < 0x10000) {
769                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xE0 | (uint32)(val>>12), 8);
770                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>6)&0x3F), 8);
771                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
772         }
773         else if(val < 0x200000) {
774                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF0 | (uint32)(val>>18), 8);
775                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>12)&0x3F), 8);
776                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>6)&0x3F), 8);
777                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
778         }
779         else if(val < 0x4000000) {
780                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF8 | (uint32)(val>>24), 8);
781                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>18)&0x3F), 8);
782                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>12)&0x3F), 8);
783                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>6)&0x3F), 8);
784                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
785         }
786         else if(val < 0x80000000) {
787                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFC | (uint32)(val>>30), 8);
788                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>24)&0x3F), 8);
789                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>18)&0x3F), 8);
790                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>12)&0x3F), 8);
791                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>6)&0x3F), 8);
792                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
793         }
794         else {
795                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFE, 8);
796                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>30)&0x3F), 8);
797                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>24)&0x3F), 8);
798                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>18)&0x3F), 8);
799                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>12)&0x3F), 8);
800                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>6)&0x3F), 8);
801                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
802         }
803
804         return ok;
805 }
806
807 bool FLAC__bitbuffer_zero_pad_to_byte_boundary(FLAC__BitBuffer *bb)
808 {
809         /* 0-pad to byte boundary */
810         if(bb->bits != 0)
811                 return FLAC__bitbuffer_write_zeroes(bb, 8 - bb->bits);
812         else
813                 return true;
814 }
815
816 bool FLAC__bitbuffer_peek_bit(FLAC__BitBuffer *bb, unsigned *val, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
817 {
818         static byte mask[] = { 128, 64, 32, 16, 8, 4, 2, 1 };
819
820         /* to avoid a drastic speed penalty we don't:
821         assert(bb != 0);
822         assert(bb->buffer != 0);
823         assert(bb->bits == 0);
824         */
825
826 read_bit_:
827         if(bb->total_consumed_bits < bb->total_bits) {
828                 *val = (bb->buffer[bb->consumed_bytes] & mask[bb->consumed_bits])? 1 : 0;
829                 return true;
830         }
831         else {
832                 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
833                         return false;
834                 goto read_bit_;
835         }
836 }
837
838 bool FLAC__bitbuffer_read_bit(FLAC__BitBuffer *bb, unsigned *val, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
839 {
840         static byte mask[] = { 128, 64, 32, 16, 8, 4, 2, 1 };
841
842         /* to avoid a drastic speed penalty we don't:
843         assert(bb != 0);
844         assert(bb->buffer != 0);
845         assert(bb->bits == 0);
846         */
847
848 read_bit_:
849         if(bb->total_consumed_bits < bb->total_bits) {
850                 *val = (bb->buffer[bb->consumed_bytes] & mask[bb->consumed_bits])? 1 : 0;
851                 bb->consumed_bits++;
852                 if(bb->consumed_bits == 8) {
853                         bb->consumed_bytes++;
854                         bb->consumed_bits = 0;
855                 }
856                 bb->total_consumed_bits++;
857                 return true;
858         }
859         else {
860                 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
861                         return false;
862                 goto read_bit_;
863         }
864 }
865
866 bool FLAC__bitbuffer_read_bit_to_uint32(FLAC__BitBuffer *bb, uint32 *val, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
867 {
868         static byte mask[] = { 128, 64, 32, 16, 8, 4, 2, 1 };
869
870         /* to avoid a drastic speed penalty we don't:
871         assert(bb != 0);
872         assert(bb->buffer != 0);
873         assert(bb->bits == 0);
874         */
875
876 read_bit_:
877         if(bb->total_consumed_bits < bb->total_bits) {
878                 *val <<= 1;
879                 *val |= (bb->buffer[bb->consumed_bytes] & mask[bb->consumed_bits])? 1 : 0;
880                 bb->consumed_bits++;
881                 if(bb->consumed_bits == 8) {
882                         bb->consumed_bytes++;
883                         bb->consumed_bits = 0;
884                 }
885                 bb->total_consumed_bits++;
886                 return true;
887         }
888         else {
889                 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
890                         return false;
891                 goto read_bit_;
892         }
893 }
894
895 bool FLAC__bitbuffer_read_bit_to_uint64(FLAC__BitBuffer *bb, uint64 *val, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
896 {
897         static byte mask[] = { 128, 64, 32, 16, 8, 4, 2, 1 };
898
899         /* to avoid a drastic speed penalty we don't:
900         assert(bb != 0);
901         assert(bb->buffer != 0);
902         assert(bb->bits == 0);
903         */
904
905 read_bit_:
906         if(bb->total_consumed_bits < bb->total_bits) {
907                 *val <<= 1;
908                 *val |= (bb->buffer[bb->consumed_bytes] & mask[bb->consumed_bits])? 1 : 0;
909                 bb->consumed_bits++;
910                 if(bb->consumed_bits == 8) {
911                         bb->consumed_bytes++;
912                         bb->consumed_bits = 0;
913                 }
914                 bb->total_consumed_bits++;
915                 return true;
916         }
917         else {
918                 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
919                         return false;
920                 goto read_bit_;
921         }
922 }
923
924 bool FLAC__bitbuffer_read_raw_uint32(FLAC__BitBuffer *bb, uint32 *val, unsigned bits, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
925 {
926         unsigned i;
927
928         assert(bb != 0);
929         assert(bb->buffer != 0);
930
931         assert(bits <= 32);
932
933         *val = 0;
934         for(i = 0; i < bits; i++) {
935                 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, val, read_callback, client_data))
936                         return false;
937         }
938         return true;
939 }
940
941 bool FLAC__bitbuffer_read_raw_int32(FLAC__BitBuffer *bb, int32 *val, unsigned bits, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
942 {
943         unsigned i;
944         uint32 x;
945
946         assert(bb != 0);
947         assert(bb->buffer != 0);
948
949         assert(bits <= 32);
950
951         x = 0;
952         for(i = 0; i < bits; i++) {
953                 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &x, read_callback, client_data))
954                         return false;
955         }
956         /* fix the sign */
957         i = 32 - bits;
958         if(i) {
959                 x <<= i;
960                 *val = (int32)x;
961                 *val >>= i;
962         }
963         else
964                 *val = (int32)x;
965
966         return true;
967 }
968
969 bool FLAC__bitbuffer_read_raw_uint64(FLAC__BitBuffer *bb, uint64 *val, unsigned bits, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
970 {
971         unsigned i;
972
973         assert(bb != 0);
974         assert(bb->buffer != 0);
975
976         assert(bits <= 64);
977
978         *val = 0;
979         for(i = 0; i < bits; i++) {
980                 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, val, read_callback, client_data))
981                         return false;
982         }
983         return true;
984 }
985
986 bool FLAC__bitbuffer_read_raw_int64(FLAC__BitBuffer *bb, int64 *val, unsigned bits, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
987 {
988         unsigned i;
989         uint64 x;
990
991         assert(bb != 0);
992         assert(bb->buffer != 0);
993
994         assert(bits <= 64);
995
996         x = 0;
997         for(i = 0; i < bits; i++) {
998                 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, &x, read_callback, client_data))
999                         return false;
1000         }
1001         /* fix the sign */
1002         i = 64 - bits;
1003         if(i) {
1004                 x <<= i;
1005                 *val = (int64)x;
1006                 *val >>= i;
1007         }
1008         else
1009                 *val = (int64)x;
1010
1011         return true;
1012 }
1013
1014 bool FLAC__bitbuffer_read_rice_signed(FLAC__BitBuffer *bb, int *val, unsigned parameter, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1015 {
1016         uint32 lsbs = 0, msbs = 0;
1017         unsigned bit, uval;
1018
1019         assert(bb != 0);
1020         assert(bb->buffer != 0);
1021         assert(parameter <= 32);
1022
1023         /* read the unary MSBs and end bit */
1024         while(1) {
1025                 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1026                         return false;
1027                 if(bit)
1028                         break;
1029                 else
1030                         msbs++;
1031         }
1032         /* read the binary LSBs */
1033         if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, parameter, read_callback, client_data))
1034                 return false;
1035         /* compose the value */
1036         uval = (msbs << parameter) | lsbs;
1037         if(uval & 1)
1038                 *val = -((int)(uval >> 1)) - 1;
1039         else
1040                 *val = (int)(uval >> 1);
1041
1042         return true;
1043 }
1044
1045 bool FLAC__bitbuffer_read_golomb_signed(FLAC__BitBuffer *bb, int *val, unsigned parameter, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1046 {
1047         uint32 lsbs = 0, msbs = 0;
1048         unsigned bit, uval, k;
1049
1050         assert(bb != 0);
1051         assert(bb->buffer != 0);
1052
1053         k = ilog2_(parameter);
1054
1055         /* read the unary MSBs and end bit */
1056         while(1) {
1057                 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1058                         return false;
1059                 if(bit)
1060                         break;
1061                 else
1062                         msbs++;
1063         }
1064         /* read the binary LSBs */
1065         if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, k, read_callback, client_data))
1066                 return false;
1067
1068         if(parameter == 1u<<k) {
1069                 /* compose the value */
1070                 uval = (msbs << k) | lsbs;
1071         }
1072         else {
1073                 unsigned d = (1 << (k+1)) - parameter;
1074                 if(lsbs >= d) {
1075                         if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1076                                 return false;
1077                         lsbs <<= 1;
1078                         lsbs |= bit;
1079                         lsbs -= d;
1080                 }
1081                 /* compose the value */
1082                 uval = msbs * parameter + lsbs;
1083         }
1084
1085         /* unfold unsigned to signed */
1086         if(uval & 1)
1087                 *val = -((int)(uval >> 1)) - 1;
1088         else
1089                 *val = (int)(uval >> 1);
1090
1091         return true;
1092 }
1093
1094 bool FLAC__bitbuffer_read_golomb_unsigned(FLAC__BitBuffer *bb, unsigned *val, unsigned parameter, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1095 {
1096         uint32 lsbs, msbs = 0;
1097         unsigned bit, k;
1098
1099         assert(bb != 0);
1100         assert(bb->buffer != 0);
1101
1102         k = ilog2_(parameter);
1103
1104         /* read the unary MSBs and end bit */
1105         while(1) {
1106                 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1107                         return false;
1108                 if(bit)
1109                         break;
1110                 else
1111                         msbs++;
1112         }
1113         /* read the binary LSBs */
1114         if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, k, read_callback, client_data))
1115                 return false;
1116
1117         if(parameter == 1u<<k) {
1118                 /* compose the value */
1119                 *val = (msbs << k) | lsbs;
1120         }
1121         else {
1122                 unsigned d = (1 << (k+1)) - parameter;
1123                 if(lsbs >= d) {
1124                         if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1125                                 return false;
1126                         lsbs <<= 1;
1127                         lsbs |= bit;
1128                         lsbs -= d;
1129                 }
1130                 /* compose the value */
1131                 *val = msbs * parameter + lsbs;
1132         }
1133
1134         return true;
1135 }
1136
1137 /* on return, if *val == 0xffffffff then the utf-8 sequence was invalid, but the return value will be true */
1138 bool FLAC__bitbuffer_read_utf8_uint32(FLAC__BitBuffer *bb, uint32 *val, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data, byte *raw, unsigned *rawlen)
1139 {
1140         uint32 v = 0;
1141         uint32 x;
1142         unsigned i;
1143
1144         if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
1145                 return false;
1146         if(raw)
1147                 raw[(*rawlen)++] = (byte)x;
1148         if(!(x & 0x80)) { /* 0xxxxxxx */
1149                 v = x;
1150                 i = 0;
1151         }
1152         else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
1153                 v = x & 0x1F;
1154                 i = 1;
1155         }
1156         else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
1157                 v = x & 0x0F;
1158                 i = 2;
1159         }
1160         else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
1161                 v = x & 0x07;
1162                 i = 3;
1163         }
1164         else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
1165                 v = x & 0x03;
1166                 i = 4;
1167         }
1168         else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
1169                 v = x & 0x01;
1170                 i = 5;
1171         }
1172         else
1173                 goto invalid_;
1174         for( ; i; i--) {
1175                 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
1176                         return false;
1177                 if(raw)
1178                         raw[(*rawlen)++] = (byte)x;
1179                 if(!(x & 0x80) || (x & 0x40)) /* 10xxxxxx */
1180                         goto invalid_;
1181                 v <<= 6;
1182                 v |= (x & 0x3F);
1183         }
1184         *val = v;
1185         return true;
1186 invalid_:
1187         *val = 0xffffffff;
1188         return true;
1189 }
1190
1191 /* on return, if *val == 0xffffffffffffffff then the utf-8 sequence was invalid, but the return value will be true */
1192 bool FLAC__bitbuffer_read_utf8_uint64(FLAC__BitBuffer *bb, uint64 *val, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data, byte *raw, unsigned *rawlen)
1193 {
1194         uint64 v = 0;
1195         uint32 x;
1196         unsigned i;
1197
1198         if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
1199                 return false;
1200         if(raw)
1201                 raw[(*rawlen)++] = (byte)x;
1202         if(!(x & 0x80)) { /* 0xxxxxxx */
1203                 v = x;
1204                 i = 0;
1205         }
1206         else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
1207                 v = x & 0x1F;
1208                 i = 1;
1209         }
1210         else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
1211                 v = x & 0x0F;
1212                 i = 2;
1213         }
1214         else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
1215                 v = x & 0x07;
1216                 i = 3;
1217         }
1218         else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
1219                 v = x & 0x03;
1220                 i = 4;
1221         }
1222         else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
1223                 v = x & 0x01;
1224                 i = 5;
1225         }
1226         else if(x & 0xFE && !(x & 0x01)) { /* 11111110 */
1227                 v = 0;
1228                 i = 6;
1229         }
1230         else
1231                 goto invalid_;
1232         for( ; i; i--) {
1233                 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
1234                         return false;
1235                 if(raw)
1236                         raw[(*rawlen)++] = (byte)x;
1237                 if(!(x & 0x80) || (x & 0x40)) /* 10xxxxxx */
1238                         goto invalid_;
1239                 v <<= 6;
1240                 v |= (x & 0x3F);
1241         }
1242         *val = v;
1243         return true;
1244 invalid_:
1245         *val = 0xffffffff;
1246         return true;
1247 }
1248
1249 void FLAC__bitbuffer_dump(const FLAC__BitBuffer *bb, FILE *out)
1250 {
1251         unsigned i, j;
1252         if(bb == 0) {
1253                 fprintf(out, "bitbuffer is NULL\n");
1254         }
1255         else {
1256                 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);
1257                 for(i = 0; i < bb->bytes; i++) {
1258                         fprintf(out, "%08X: ", i);
1259                         for(j = 0; j < 8; j++)
1260                                 if(i*8+j < bb->total_consumed_bits)
1261                                         fprintf(out, ".");
1262                                 else
1263                                         fprintf(out, "%01u", bb->buffer[i] & (1 << (8-j-1)) ? 1:0);
1264                         fprintf(out, "\n");
1265                 }
1266                 if(bb->bits > 0) {
1267                         fprintf(out, "%08X: ", i);
1268                         for(j = 0; j < bb->bits; j++)
1269                                 if(i*8+j < bb->total_consumed_bits)
1270                                         fprintf(out, ".");
1271                                 else
1272                                         fprintf(out, "%01u", bb->buffer[i] & (1 << (bb->bits-j-1)) ? 1:0);
1273                         fprintf(out, "\n");
1274                 }
1275         }
1276 }