1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000,2001 Josh Coalson
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.
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.
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.
21 #include <stdlib.h> /* for malloc() */
22 #include <string.h> /* for memcpy(), memset() */
23 #include "private/bitbuffer.h"
24 #include "private/bitmath.h"
25 #include "private/crc.h"
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.
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 */
38 #define BYTE_BIT_TO_MASK(b) (((byte)'\x80') >> (b))
43 #define min(x,y) ((x)<(y)?(x):(y))
47 #define max(x,y) ((x)>(y)?(x):(y))
49 static bool bitbuffer_resize_(FLAC__BitBuffer *bb, unsigned new_capacity)
54 assert(bb->buffer != 0);
56 if(bb->capacity == new_capacity)
59 new_buffer = (byte*)malloc(sizeof(byte) * new_capacity);
62 memset(new_buffer, 0, new_capacity);
63 memcpy(new_buffer, bb->buffer, sizeof(byte)*min(bb->bytes+(bb->bits?1:0), new_capacity));
64 if(new_capacity < bb->bytes+(bb->bits?1:0)) {
65 bb->bytes = new_capacity;
67 bb->total_bits = (new_capacity<<3);
69 if(new_capacity < bb->consumed_bytes+(bb->consumed_bits?1:0)) {
70 bb->consumed_bytes = new_capacity;
71 bb->consumed_bits = 0;
72 bb->total_consumed_bits = (new_capacity<<3);
74 bb->buffer = new_buffer;
75 bb->capacity = new_capacity;
79 static bool bitbuffer_grow_(FLAC__BitBuffer *bb, unsigned min_bytes_to_add)
81 unsigned new_capacity;
83 assert(min_bytes_to_add > 0);
85 new_capacity = max(bb->capacity * 4, bb->capacity + min_bytes_to_add);
86 return bitbuffer_resize_(bb, new_capacity);
89 static bool bitbuffer_ensure_size_(FLAC__BitBuffer *bb, unsigned bits_to_add)
92 assert(bb->buffer != 0);
93 if((bb->capacity<<3) < bb->total_bits + bits_to_add)
94 return bitbuffer_grow_(bb, (bits_to_add>>3)+2);
99 static bool bitbuffer_read_from_client_(FLAC__BitBuffer *bb, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
103 /* first shift the unconsumed buffer data toward the front as much as possible */
104 if(bb->total_consumed_bits >= 8) {
105 unsigned l = 0, r = bb->consumed_bytes, r_end = bb->bytes;
106 for( ; r < r_end; l++, r++)
107 bb->buffer[l] = bb->buffer[r];
108 for( ; l < r_end; l++)
110 bb->bytes -= bb->consumed_bytes;
111 bb->total_bits -= (bb->consumed_bytes<<3);
112 bb->consumed_bytes = 0;
113 bb->total_consumed_bits = bb->consumed_bits;
115 /* grow if we need to */
116 if(bb->capacity <= 1) {
117 if(!bitbuffer_resize_(bb, 16))
120 /* finally, read in some data */
121 bytes = bb->capacity - bb->bytes;
122 if(!read_callback(bb->buffer+bb->bytes, &bytes, client_data))
125 bb->total_bits += (bytes<<3);
129 void FLAC__bitbuffer_init(FLAC__BitBuffer *bb)
134 bb->bytes = bb->bits = bb->total_bits = 0;
135 bb->consumed_bytes = bb->consumed_bits = bb->total_consumed_bits = 0;
138 bool FLAC__bitbuffer_init_from(FLAC__BitBuffer *bb, const byte buffer[], unsigned bytes)
141 FLAC__bitbuffer_init(bb);
146 bb->buffer = (byte*)malloc(sizeof(byte)*bytes);
149 memcpy(bb->buffer, buffer, sizeof(byte)*bytes);
150 bb->capacity = bb->bytes = bytes;
152 bb->total_bits = (bytes<<3);
153 bb->consumed_bytes = bb->consumed_bits = bb->total_consumed_bits = 0;
158 void FLAC__bitbuffer_init_read_crc16(FLAC__BitBuffer *bb, uint16 seed)
162 bb->read_crc16 = seed;
165 bool FLAC__bitbuffer_concatenate_aligned(FLAC__BitBuffer *dest, const FLAC__BitBuffer *src)
167 static const byte mask_[9] = { 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff };
168 unsigned bits_to_add = src->total_bits - src->total_consumed_bits;
175 if(dest->bits != src->consumed_bits)
177 if(!bitbuffer_ensure_size_(dest, bits_to_add))
179 if(dest->bits == 0) {
180 memcpy(dest->buffer+dest->bytes, src->buffer+src->consumed_bytes, src->bytes-src->consumed_bytes + ((src->bits)? 1:0));
182 else if(dest->bits + bits_to_add > 8) {
183 dest->buffer[dest->bytes] <<= (8 - dest->bits);
184 dest->buffer[dest->bytes] |= (src->buffer[src->consumed_bytes] & mask_[8-dest->bits]);
185 memcpy(dest->buffer+dest->bytes+1, src->buffer+src->consumed_bytes+1, src->bytes-src->consumed_bytes-1 + ((src->bits)? 1:0));
188 dest->buffer[dest->bytes] <<= bits_to_add;
189 dest->buffer[dest->bytes] |= (src->buffer[src->consumed_bytes] & mask_[bits_to_add]);
191 dest->bits = src->bits;
192 dest->total_bits += bits_to_add;
193 dest->bytes = dest->total_bits / 8;
198 void FLAC__bitbuffer_free(FLAC__BitBuffer *bb)
205 bb->bytes = bb->bits = bb->total_bits = 0;
206 bb->consumed_bytes = bb->consumed_bits = bb->total_consumed_bits = 0;
209 bool FLAC__bitbuffer_clear(FLAC__BitBuffer *bb)
211 if(bb->buffer == 0) {
212 bb->capacity = FLAC__BITBUFFER_DEFAULT_CAPACITY;
213 bb->buffer = (byte*)malloc(sizeof(byte) * bb->capacity);
216 memset(bb->buffer, 0, bb->capacity);
219 memset(bb->buffer, 0, bb->bytes + (bb->bits?1:0));
221 bb->bytes = bb->bits = bb->total_bits = 0;
222 bb->consumed_bytes = bb->consumed_bits = bb->total_consumed_bits = 0;
226 bool FLAC__bitbuffer_clone(FLAC__BitBuffer *dest, const FLAC__BitBuffer *src)
228 if(dest->capacity < src->capacity)
229 if(!bitbuffer_resize_(dest, src->capacity))
231 memcpy(dest->buffer, src->buffer, sizeof(byte)*min(src->capacity, src->bytes+1));
232 dest->bytes = src->bytes;
233 dest->bits = src->bits;
234 dest->total_bits = src->total_bits;
235 dest->consumed_bytes = src->consumed_bytes;
236 dest->consumed_bits = src->consumed_bits;
237 dest->total_consumed_bits = src->total_consumed_bits;
238 dest->read_crc16 = src->read_crc16;
242 bool FLAC__bitbuffer_write_zeroes(FLAC__BitBuffer *bb, unsigned bits)
247 assert(bb->buffer != 0);
251 if(!bitbuffer_ensure_size_(bb, bits))
253 bb->total_bits += bits;
255 n = min(8 - bb->bits, bits);
257 bb->buffer[bb->bytes] <<= n;
268 bool FLAC__bitbuffer_write_raw_uint32(FLAC__BitBuffer *bb, uint32 val, unsigned bits)
273 assert(bb->buffer != 0);
278 if(!bitbuffer_ensure_size_(bb, bits))
280 val &= (~(0xffffffffu << bits)); /* zero-out unused bits */
281 bb->total_bits += bits;
284 if(n == 8) { /* i.e. bb->bits == 0 */
286 bb->buffer[bb->bytes] = val;
291 bb->buffer[bb->bytes++] = val;
296 bb->buffer[bb->bytes++] = val >> k;
297 val &= (~(0xffffffff << k));
302 bb->buffer[bb->bytes] <<= bits;
303 bb->buffer[bb->bytes] |= val;
314 bb->buffer[bb->bytes] <<= n;
315 bb->buffer[bb->bytes] |= (val>>k);
316 val &= (~(0xffffffff << k));
326 bool FLAC__bitbuffer_write_raw_int32(FLAC__BitBuffer *bb, int32 val, unsigned bits)
328 return FLAC__bitbuffer_write_raw_uint32(bb, (uint32)val, bits);
331 bool FLAC__bitbuffer_write_raw_uint64(FLAC__BitBuffer *bb, uint64 val, unsigned bits)
333 static const uint64 mask[] = {
335 0x0000000000000001, 0x0000000000000003, 0x0000000000000007, 0x000000000000000F,
336 0x000000000000001F, 0x000000000000003F, 0x000000000000007F, 0x00000000000000FF,
337 0x00000000000001FF, 0x00000000000003FF, 0x00000000000007FF, 0x0000000000000FFF,
338 0x0000000000001FFF, 0x0000000000003FFF, 0x0000000000007FFF, 0x000000000000FFFF,
339 0x000000000001FFFF, 0x000000000003FFFF, 0x000000000007FFFF, 0x00000000000FFFFF,
340 0x00000000001FFFFF, 0x00000000003FFFFF, 0x00000000007FFFFF, 0x0000000000FFFFFF,
341 0x0000000001FFFFFF, 0x0000000003FFFFFF, 0x0000000007FFFFFF, 0x000000000FFFFFFF,
342 0x000000001FFFFFFF, 0x000000003FFFFFFF, 0x000000007FFFFFFF, 0x00000000FFFFFFFF,
343 0x00000001FFFFFFFF, 0x00000003FFFFFFFF, 0x00000007FFFFFFFF, 0x0000000FFFFFFFFF,
344 0x0000001FFFFFFFFF, 0x0000003FFFFFFFFF, 0x0000007FFFFFFFFF, 0x000000FFFFFFFFFF,
345 0x000001FFFFFFFFFF, 0x000003FFFFFFFFFF, 0x000007FFFFFFFFFF, 0x00000FFFFFFFFFFF,
346 0x00001FFFFFFFFFFF, 0x00003FFFFFFFFFFF, 0x00007FFFFFFFFFFF, 0x0000FFFFFFFFFFFF,
347 0x0001FFFFFFFFFFFF, 0x0003FFFFFFFFFFFF, 0x0007FFFFFFFFFFFF, 0x000FFFFFFFFFFFFF,
348 0x001FFFFFFFFFFFFF, 0x003FFFFFFFFFFFFF, 0x007FFFFFFFFFFFFF, 0x00FFFFFFFFFFFFFF,
349 0x01FFFFFFFFFFFFFF, 0x03FFFFFFFFFFFFFF, 0x07FFFFFFFFFFFFFF, 0x0FFFFFFFFFFFFFFF,
350 0x1FFFFFFFFFFFFFFF, 0x3FFFFFFFFFFFFFFF, 0x7FFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF
355 assert(bb->buffer != 0);
360 if(!bitbuffer_ensure_size_(bb, bits))
363 bb->total_bits += bits;
367 bb->buffer[bb->bytes] = val;
372 bb->buffer[bb->bytes++] = val;
377 bb->buffer[bb->bytes++] = val >> k;
378 val &= (~(0xffffffffffffffff << k));
383 n = min(8 - bb->bits, bits);
385 bb->buffer[bb->bytes] <<= n;
386 bb->buffer[bb->bytes] |= (val>>k);
387 val &= (~(0xffffffffffffffff << k));
400 bool FLAC__bitbuffer_write_raw_int64(FLAC__BitBuffer *bb, int64 val, unsigned bits)
402 return FLAC__bitbuffer_write_raw_uint64(bb, (uint64)val, bits);
405 bool FLAC__bitbuffer_write_unary_unsigned(FLAC__BitBuffer *bb, unsigned val)
408 return FLAC__bitbuffer_write_raw_uint32(bb, 1, ++val);
410 return FLAC__bitbuffer_write_raw_uint64(bb, 1, ++val);
412 if(!FLAC__bitbuffer_write_zeroes(bb, val))
414 return FLAC__bitbuffer_write_raw_uint32(bb, 1, 1);
418 unsigned FLAC__bitbuffer_rice_bits(int val, unsigned parameter)
422 /* convert signed to unsigned */
425 * (unsigned)(((--val) << 1) - 1);
426 * but without the overflow problem at -MAXINT
428 uval = (unsigned)(((-(++val)) << 1) + 1);
430 uval = (unsigned)(val << 1);
432 msbs = uval >> parameter;
434 return 1 + parameter + msbs;
437 unsigned FLAC__bitbuffer_golomb_bits_signed(int val, unsigned parameter)
439 unsigned bits, msbs, uval;
442 assert(parameter > 0);
444 /* convert signed to unsigned */
447 * (unsigned)(((--val) << 1) - 1);
448 * but without the overflow problem at -MAXINT
450 uval = (unsigned)(((-(++val)) << 1) + 1);
452 uval = (unsigned)(val << 1);
454 k = FLAC__bitmath_ilog2(parameter);
455 if(parameter == 1u<<k) {
464 d = (1 << (k+1)) - parameter;
465 q = uval / parameter;
466 r = uval - (q * parameter);
475 unsigned FLAC__bitbuffer_golomb_bits_unsigned(unsigned uval, unsigned parameter)
480 assert(parameter > 0);
482 k = FLAC__bitmath_ilog2(parameter);
483 if(parameter == 1u<<k) {
492 d = (1 << (k+1)) - parameter;
493 q = uval / parameter;
494 r = uval - (q * parameter);
503 bool FLAC__bitbuffer_write_symmetric_rice_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
505 unsigned total_bits, interesting_bits, msbs;
509 assert(bb->buffer != 0);
510 assert(parameter <= 31);
512 /* init pattern with the unary end bit and the sign bit */
520 msbs = val >> parameter;
521 interesting_bits = 2 + parameter;
522 total_bits = interesting_bits + msbs;
523 pattern <<= parameter;
524 pattern |= (val & ((1<<parameter)-1)); /* the binary LSBs */
526 if(total_bits <= 32) {
527 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
531 /* write the unary MSBs */
532 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
534 /* write the unary end bit, the sign bit, and binary LSBs */
535 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
541 bool FLAC__bitbuffer_write_symmetric_rice_signed_guarded(FLAC__BitBuffer *bb, int val, unsigned parameter, unsigned max_bits, bool *overflow)
543 unsigned total_bits, interesting_bits, msbs;
547 assert(bb->buffer != 0);
548 assert(parameter <= 31);
552 /* init pattern with the unary end bit and the sign bit */
560 msbs = val >> parameter;
561 interesting_bits = 2 + parameter;
562 total_bits = interesting_bits + msbs;
563 pattern <<= parameter;
564 pattern |= (val & ((1<<parameter)-1)); /* the binary LSBs */
566 if(total_bits <= 32) {
567 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
570 else if(total_bits > max_bits) {
575 /* write the unary MSBs */
576 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
578 /* write the unary end bit, the sign bit, and binary LSBs */
579 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
585 bool FLAC__bitbuffer_write_symmetric_rice_signed_escape(FLAC__BitBuffer *bb, int val, unsigned parameter)
587 unsigned total_bits, val_bits;
591 assert(bb->buffer != 0);
592 assert(parameter <= 31);
594 val_bits = FLAC__bitmath_silog2(val);
595 total_bits = 2 + parameter + 5 + val_bits;
597 if(total_bits <= 32) {
599 pattern <<= (parameter + 5);
601 pattern <<= val_bits;
602 pattern |= (val & ((1 << val_bits) - 1));
603 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
607 /* write the '-0' escape code first */
608 if(!FLAC__bitbuffer_write_raw_uint32(bb, 3u << parameter, 2+parameter))
610 /* write the length */
611 if(!FLAC__bitbuffer_write_raw_uint32(bb, val_bits, 5))
613 /* write the value */
614 if(!FLAC__bitbuffer_write_raw_int32(bb, val, val_bits))
620 bool FLAC__bitbuffer_write_rice_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
622 unsigned total_bits, interesting_bits, msbs, uval;
626 assert(bb->buffer != 0);
627 assert(parameter <= 30);
629 /* convert signed to unsigned */
632 * (unsigned)(((--val) << 1) - 1);
633 * but without the overflow problem at -MAXINT
635 uval = (unsigned)(((-(++val)) << 1) + 1);
637 uval = (unsigned)(val << 1);
639 msbs = uval >> parameter;
640 interesting_bits = 1 + parameter;
641 total_bits = interesting_bits + msbs;
642 pattern = 1 << parameter; /* the unary end bit */
643 pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
645 if(total_bits <= 32) {
646 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
650 /* write the unary MSBs */
651 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
653 /* write the unary end bit and binary LSBs */
654 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
660 bool FLAC__bitbuffer_write_rice_signed_guarded(FLAC__BitBuffer *bb, int val, unsigned parameter, unsigned max_bits, bool *overflow)
662 unsigned total_bits, interesting_bits, msbs, uval;
666 assert(bb->buffer != 0);
667 assert(parameter <= 30);
671 /* convert signed to unsigned */
674 * (unsigned)(((--val) << 1) - 1);
675 * but without the overflow problem at -MAXINT
677 uval = (unsigned)(((-(++val)) << 1) + 1);
679 uval = (unsigned)(val << 1);
681 msbs = uval >> parameter;
682 interesting_bits = 1 + parameter;
683 total_bits = interesting_bits + msbs;
684 pattern = 1 << parameter; /* the unary end bit */
685 pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
687 if(total_bits <= 32) {
688 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
691 else if(total_bits > max_bits) {
696 /* write the unary MSBs */
697 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
699 /* write the unary end bit and binary LSBs */
700 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
706 bool FLAC__bitbuffer_write_golomb_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
708 unsigned total_bits, msbs, uval;
712 assert(bb->buffer != 0);
713 assert(parameter > 0);
715 /* convert signed to unsigned */
718 * (unsigned)(((--val) << 1) - 1);
719 * but without the overflow problem at -MAXINT
721 uval = (unsigned)(((-(++val)) << 1) + 1);
723 uval = (unsigned)(val << 1);
725 k = FLAC__bitmath_ilog2(parameter);
726 if(parameter == 1u<<k) {
732 total_bits = 1 + k + msbs;
733 pattern = 1 << k; /* the unary end bit */
734 pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
736 if(total_bits <= 32) {
737 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
741 /* write the unary MSBs */
742 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
744 /* write the unary end bit and binary LSBs */
745 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, k+1))
752 d = (1 << (k+1)) - parameter;
753 q = uval / parameter;
754 r = uval - (q * parameter);
755 /* write the unary MSBs */
756 if(!FLAC__bitbuffer_write_zeroes(bb, q))
758 /* write the unary end bit */
759 if(!FLAC__bitbuffer_write_raw_uint32(bb, 1, 1))
761 /* write the binary LSBs */
763 if(!FLAC__bitbuffer_write_raw_uint32(bb, r+d, k+1))
767 if(!FLAC__bitbuffer_write_raw_uint32(bb, r, k))
774 bool FLAC__bitbuffer_write_golomb_unsigned(FLAC__BitBuffer *bb, unsigned uval, unsigned parameter)
776 unsigned total_bits, msbs;
780 assert(bb->buffer != 0);
781 assert(parameter > 0);
783 k = FLAC__bitmath_ilog2(parameter);
784 if(parameter == 1u<<k) {
790 total_bits = 1 + k + msbs;
791 pattern = 1 << k; /* the unary end bit */
792 pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
794 if(total_bits <= 32) {
795 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
799 /* write the unary MSBs */
800 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
802 /* write the unary end bit and binary LSBs */
803 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, k+1))
810 d = (1 << (k+1)) - parameter;
811 q = uval / parameter;
812 r = uval - (q * parameter);
813 /* write the unary MSBs */
814 if(!FLAC__bitbuffer_write_zeroes(bb, q))
816 /* write the unary end bit */
817 if(!FLAC__bitbuffer_write_raw_uint32(bb, 1, 1))
819 /* write the binary LSBs */
821 if(!FLAC__bitbuffer_write_raw_uint32(bb, r+d, k+1))
825 if(!FLAC__bitbuffer_write_raw_uint32(bb, r, k))
832 bool FLAC__bitbuffer_write_utf8_uint32(FLAC__BitBuffer *bb, uint32 val)
837 assert(bb->buffer != 0);
839 assert(!(val & 0x80000000)); /* this version only handles 31 bits */
842 return FLAC__bitbuffer_write_raw_uint32(bb, val, 8);
844 else if(val < 0x800) {
845 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xC0 | (val>>6), 8);
846 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
848 else if(val < 0x10000) {
849 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xE0 | (val>>12), 8);
850 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
851 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
853 else if(val < 0x200000) {
854 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF0 | (val>>18), 8);
855 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
856 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
857 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
859 else if(val < 0x4000000) {
860 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF8 | (val>>24), 8);
861 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>18)&0x3F), 8);
862 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
863 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
864 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
867 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFC | (val>>30), 8);
868 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>24)&0x3F), 8);
869 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>18)&0x3F), 8);
870 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
871 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
872 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
878 bool FLAC__bitbuffer_write_utf8_uint64(FLAC__BitBuffer *bb, uint64 val)
883 assert(bb->buffer != 0);
885 assert(!(val & 0xFFFFFFF000000000)); /* this version only handles 36 bits */
888 return FLAC__bitbuffer_write_raw_uint32(bb, (uint32)val, 8);
890 else if(val < 0x800) {
891 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xC0 | (uint32)(val>>6), 8);
892 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
894 else if(val < 0x10000) {
895 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xE0 | (uint32)(val>>12), 8);
896 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>6)&0x3F), 8);
897 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
899 else if(val < 0x200000) {
900 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF0 | (uint32)(val>>18), 8);
901 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>12)&0x3F), 8);
902 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>6)&0x3F), 8);
903 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
905 else if(val < 0x4000000) {
906 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF8 | (uint32)(val>>24), 8);
907 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>18)&0x3F), 8);
908 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>12)&0x3F), 8);
909 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>6)&0x3F), 8);
910 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
912 else if(val < 0x80000000) {
913 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFC | (uint32)(val>>30), 8);
914 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>24)&0x3F), 8);
915 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>18)&0x3F), 8);
916 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>12)&0x3F), 8);
917 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>6)&0x3F), 8);
918 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
921 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFE, 8);
922 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>30)&0x3F), 8);
923 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>24)&0x3F), 8);
924 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>18)&0x3F), 8);
925 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>12)&0x3F), 8);
926 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>6)&0x3F), 8);
927 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
933 bool FLAC__bitbuffer_zero_pad_to_byte_boundary(FLAC__BitBuffer *bb)
935 /* 0-pad to byte boundary */
937 return FLAC__bitbuffer_write_zeroes(bb, 8 - bb->bits);
942 bool FLAC__bitbuffer_peek_bit(FLAC__BitBuffer *bb, unsigned *val, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
944 /* to avoid a drastic speed penalty we don't:
946 assert(bb->buffer != 0);
947 assert(bb->bits == 0);
951 if(bb->total_consumed_bits < bb->total_bits) {
952 *val = (bb->buffer[bb->consumed_bytes] & BYTE_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
956 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
962 bool FLAC__bitbuffer_read_bit(FLAC__BitBuffer *bb, unsigned *val, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
964 /* to avoid a drastic speed penalty we don't:
966 assert(bb->buffer != 0);
967 assert(bb->bits == 0);
971 if(bb->total_consumed_bits < bb->total_bits) {
972 *val = (bb->buffer[bb->consumed_bytes] & BYTE_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
974 if(bb->consumed_bits == 8) {
975 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
976 bb->consumed_bytes++;
977 bb->consumed_bits = 0;
979 bb->total_consumed_bits++;
983 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
989 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)
991 /* to avoid a drastic speed penalty we don't:
993 assert(bb->buffer != 0);
994 assert(bb->bits == 0);
998 if(bb->total_consumed_bits < bb->total_bits) {
1000 *val |= (bb->buffer[bb->consumed_bytes] & BYTE_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
1001 bb->consumed_bits++;
1002 if(bb->consumed_bits == 8) {
1003 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1004 bb->consumed_bytes++;
1005 bb->consumed_bits = 0;
1007 bb->total_consumed_bits++;
1011 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1017 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)
1019 /* to avoid a drastic speed penalty we don't:
1021 assert(bb->buffer != 0);
1022 assert(bb->bits == 0);
1026 if(bb->total_consumed_bits < bb->total_bits) {
1028 *val |= (bb->buffer[bb->consumed_bytes] & BYTE_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
1029 bb->consumed_bits++;
1030 if(bb->consumed_bits == 8) {
1031 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1032 bb->consumed_bytes++;
1033 bb->consumed_bits = 0;
1035 bb->total_consumed_bits++;
1039 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1045 bool FLAC__bitbuffer_read_raw_uint32(FLAC__BitBuffer *bb, uint32 *val, const unsigned bits, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1046 #ifdef FLAC__NO_MANUAL_INLINING
1051 assert(bb->buffer != 0);
1056 for(i = 0; i < bits; i++) {
1057 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, val, read_callback, client_data))
1064 unsigned i, bits_ = bits;
1068 assert(bb->buffer != 0);
1071 assert((bb->capacity*8) * 2 >= bits);
1073 while(bb->total_consumed_bits + bits > bb->total_bits) {
1074 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1077 if(bb->consumed_bits) {
1078 i = 8 - bb->consumed_bits;
1080 v = bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits);
1082 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1083 bb->consumed_bytes++;
1084 bb->consumed_bits = 0;
1085 /* we hold off updating bb->total_consumed_bits until the end */
1088 *val = (bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits)) >> (i-bits_);
1089 bb->consumed_bits += bits_;
1090 bb->total_consumed_bits += bits_;
1096 v |= bb->buffer[bb->consumed_bytes];
1098 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1099 bb->consumed_bytes++;
1100 /* bb->consumed_bits is already 0 */
1101 /* we hold off updating bb->total_consumed_bits until the end */
1105 v |= (bb->buffer[bb->consumed_bytes] >> (8-bits_));
1106 bb->consumed_bits = bits_;
1107 /* we hold off updating bb->total_consumed_bits until the end */
1109 bb->total_consumed_bits += bits;
1115 bool FLAC__bitbuffer_read_raw_int32(FLAC__BitBuffer *bb, int32 *val, const unsigned bits, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1116 #ifdef FLAC__NO_MANUAL_INLINING
1122 assert(bb->buffer != 0);
1127 for(i = 0; i < bits; i++) {
1128 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &v, read_callback, client_data))
1146 unsigned i, bits_ = bits;
1150 assert(bb->buffer != 0);
1153 assert((bb->capacity*8) * 2 >= bits);
1155 while(bb->total_consumed_bits + bits > bb->total_bits) {
1156 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1159 if(bb->consumed_bits) {
1160 i = 8 - bb->consumed_bits;
1162 v = bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits);
1164 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1165 bb->consumed_bytes++;
1166 bb->consumed_bits = 0;
1167 /* we hold off updating bb->total_consumed_bits until the end */
1170 *val = (bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits)) >> (i-bits_);
1171 bb->consumed_bits += bits_;
1172 bb->total_consumed_bits += bits_;
1178 v |= bb->buffer[bb->consumed_bytes];
1180 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1181 bb->consumed_bytes++;
1182 /* bb->consumed_bits is already 0 */
1183 /* we hold off updating bb->total_consumed_bits until the end */
1187 v |= (bb->buffer[bb->consumed_bytes] >> (8-bits_));
1188 bb->consumed_bits = bits_;
1189 /* we hold off updating bb->total_consumed_bits until the end */
1191 bb->total_consumed_bits += bits;
1207 bool FLAC__bitbuffer_read_raw_uint64(FLAC__BitBuffer *bb, uint64 *val, const unsigned bits, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1208 #ifdef FLAC__NO_MANUAL_INLINING
1213 assert(bb->buffer != 0);
1218 for(i = 0; i < bits; i++) {
1219 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, val, read_callback, client_data))
1226 unsigned i, bits_ = bits;
1230 assert(bb->buffer != 0);
1233 assert((bb->capacity*8) * 2 >= bits);
1235 while(bb->total_consumed_bits + bits > bb->total_bits) {
1236 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1239 if(bb->consumed_bits) {
1240 i = 8 - bb->consumed_bits;
1242 v = bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits);
1244 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1245 bb->consumed_bytes++;
1246 bb->consumed_bits = 0;
1247 /* we hold off updating bb->total_consumed_bits until the end */
1250 *val = (bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits)) >> (i-bits_);
1251 bb->consumed_bits += bits_;
1252 bb->total_consumed_bits += bits_;
1258 v |= bb->buffer[bb->consumed_bytes];
1260 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1261 bb->consumed_bytes++;
1262 /* bb->consumed_bits is already 0 */
1263 /* we hold off updating bb->total_consumed_bits until the end */
1267 v |= (bb->buffer[bb->consumed_bytes] >> (8-bits_));
1268 bb->consumed_bits = bits_;
1269 /* we hold off updating bb->total_consumed_bits until the end */
1271 bb->total_consumed_bits += bits;
1277 bool FLAC__bitbuffer_read_raw_int64(FLAC__BitBuffer *bb, int64 *val, const unsigned bits, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1278 #ifdef FLAC__NO_MANUAL_INLINING
1284 assert(bb->buffer != 0);
1289 for(i = 0; i < bits; i++) {
1290 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, &v, read_callback, client_data))
1307 unsigned i, bits_ = bits;
1311 assert(bb->buffer != 0);
1314 assert((bb->capacity*8) * 2 >= bits);
1316 while(bb->total_consumed_bits + bits > bb->total_bits) {
1317 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1320 if(bb->consumed_bits) {
1321 i = 8 - bb->consumed_bits;
1323 v = bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits);
1325 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1326 bb->consumed_bytes++;
1327 bb->consumed_bits = 0;
1328 /* we hold off updating bb->total_consumed_bits until the end */
1331 *val = (bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits)) >> (i-bits_);
1332 bb->consumed_bits += bits_;
1333 bb->total_consumed_bits += bits_;
1339 v |= bb->buffer[bb->consumed_bytes];
1341 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1342 bb->consumed_bytes++;
1343 /* bb->consumed_bits is already 0 */
1344 /* we hold off updating bb->total_consumed_bits until the end */
1348 v |= (bb->buffer[bb->consumed_bytes] >> (8-bits_));
1349 bb->consumed_bits = bits_;
1350 /* we hold off updating bb->total_consumed_bits until the end */
1352 bb->total_consumed_bits += bits;
1368 bool FLAC__bitbuffer_read_unary_unsigned(FLAC__BitBuffer *bb, unsigned *val, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1369 #ifdef FLAC__NO_MANUAL_INLINING
1371 unsigned bit, val_ = 0;
1374 assert(bb->buffer != 0);
1377 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1389 unsigned i, val_ = 0;
1390 unsigned total_bytes_ = (bb->total_bits + 7) / 8;
1394 assert(bb->buffer != 0);
1396 if(bb->consumed_bits) {
1397 b = bb->buffer[bb->consumed_bytes] << bb->consumed_bits;
1399 for(i = 0; !(b & 0x80); i++)
1403 bb->consumed_bits += i;
1404 bb->total_consumed_bits += i;
1405 if(bb->consumed_bits == 8) {
1406 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1407 bb->consumed_bytes++;
1408 bb->consumed_bits = 0;
1413 val_ = 8 - bb->consumed_bits;
1414 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1415 bb->consumed_bytes++;
1416 bb->consumed_bits = 0;
1417 bb->total_consumed_bits += val_;
1421 if(bb->consumed_bytes >= total_bytes_) {
1422 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1424 total_bytes_ = (bb->total_bits + 7) / 8;
1426 b = bb->buffer[bb->consumed_bytes];
1428 for(i = 0; !(b & 0x80); i++)
1432 bb->consumed_bits = i;
1435 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1436 bb->consumed_bytes++;
1437 bb->consumed_bits = 0;
1439 bb->total_consumed_bits += i;
1444 FLAC__CRC16_UPDATE(0, bb->read_crc16);
1445 bb->consumed_bytes++;
1446 /* bb->consumed_bits is already 0 */
1447 /* we hold off updating bb->total_consumed_bits until the end */
1448 bb->total_consumed_bits += 8;
1454 bool FLAC__bitbuffer_read_symmetric_rice_signed(FLAC__BitBuffer *bb, int *val, unsigned parameter, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1456 uint32 sign = 0, lsbs = 0, msbs = 0;
1459 assert(bb->buffer != 0);
1460 assert(parameter <= 31);
1462 /* read the unary MSBs and end bit */
1463 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
1466 /* read the sign bit */
1467 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &sign, read_callback, client_data))
1470 /* read the binary LSBs */
1471 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, parameter, read_callback, client_data))
1474 /* compose the value */
1475 *val = (msbs << parameter) | lsbs;
1482 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)
1484 uint32 lsbs = 0, msbs = 0;
1488 assert(bb->buffer != 0);
1489 assert(parameter <= 31);
1491 /* read the unary MSBs and end bit */
1492 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
1495 /* read the binary LSBs */
1496 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, parameter, read_callback, client_data))
1499 /* compose the value */
1500 uval = (msbs << parameter) | lsbs;
1502 *val = -((int)(uval >> 1)) - 1;
1504 *val = (int)(uval >> 1);
1509 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)
1511 uint32 lsbs = 0, msbs = 0;
1512 unsigned bit, uval, k;
1515 assert(bb->buffer != 0);
1517 k = FLAC__bitmath_ilog2(parameter);
1519 /* read the unary MSBs and end bit */
1520 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
1523 /* read the binary LSBs */
1524 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, k, read_callback, client_data))
1527 if(parameter == 1u<<k) {
1528 /* compose the value */
1529 uval = (msbs << k) | lsbs;
1532 unsigned d = (1 << (k+1)) - parameter;
1534 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1540 /* compose the value */
1541 uval = msbs * parameter + lsbs;
1544 /* unfold unsigned to signed */
1546 *val = -((int)(uval >> 1)) - 1;
1548 *val = (int)(uval >> 1);
1553 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)
1555 uint32 lsbs, msbs = 0;
1559 assert(bb->buffer != 0);
1561 k = FLAC__bitmath_ilog2(parameter);
1563 /* read the unary MSBs and end bit */
1564 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
1567 /* read the binary LSBs */
1568 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, k, read_callback, client_data))
1571 if(parameter == 1u<<k) {
1572 /* compose the value */
1573 *val = (msbs << k) | lsbs;
1576 unsigned d = (1 << (k+1)) - parameter;
1578 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1584 /* compose the value */
1585 *val = msbs * parameter + lsbs;
1591 /* on return, if *val == 0xffffffff then the utf-8 sequence was invalid, but the return value will be true */
1592 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)
1598 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
1601 raw[(*rawlen)++] = (byte)x;
1602 if(!(x & 0x80)) { /* 0xxxxxxx */
1606 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
1610 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
1614 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
1618 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
1622 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
1631 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
1634 raw[(*rawlen)++] = (byte)x;
1635 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
1646 /* on return, if *val == 0xffffffffffffffff then the utf-8 sequence was invalid, but the return value will be true */
1647 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)
1653 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
1656 raw[(*rawlen)++] = (byte)x;
1657 if(!(x & 0x80)) { /* 0xxxxxxx */
1661 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
1665 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
1669 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
1673 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
1677 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
1681 else if(x & 0xFE && !(x & 0x01)) { /* 11111110 */
1686 *val = 0xffffffffffffffff;
1690 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
1693 raw[(*rawlen)++] = (byte)x;
1694 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
1695 *val = 0xffffffffffffffff;
1705 void FLAC__bitbuffer_dump(const FLAC__BitBuffer *bb, FILE *out)
1709 fprintf(out, "bitbuffer is NULL\n");
1712 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);
1713 for(i = 0; i < bb->bytes; i++) {
1714 fprintf(out, "%08X: ", i);
1715 for(j = 0; j < 8; j++)
1716 if(i*8+j < bb->total_consumed_bits)
1719 fprintf(out, "%01u", bb->buffer[i] & (1 << (8-j-1)) ? 1:0);
1723 fprintf(out, "%08X: ", i);
1724 for(j = 0; j < bb->bits; j++)
1725 if(i*8+j < bb->total_consumed_bits)
1728 fprintf(out, "%01u", bb->buffer[i] & (1 << (bb->bits-j-1)) ? 1:0);