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 if(bits < 32) /* @@@ gcc seems to require this because the following line causes incorrect results when bits==32; investigate */
281 val &= (~(0xffffffff << bits)); /* zero-out unused bits */
282 bb->total_bits += bits;
285 if(n == 8) { /* i.e. bb->bits == 0 */
287 bb->buffer[bb->bytes] = val;
292 bb->buffer[bb->bytes++] = val;
297 bb->buffer[bb->bytes++] = val >> k;
298 val &= (~(0xffffffff << k));
303 bb->buffer[bb->bytes] <<= bits;
304 bb->buffer[bb->bytes] |= val;
315 bb->buffer[bb->bytes] <<= n;
316 bb->buffer[bb->bytes] |= (val>>k);
317 val &= (~(0xffffffff << k));
327 bool FLAC__bitbuffer_write_raw_int32(FLAC__BitBuffer *bb, int32 val, unsigned bits)
329 return FLAC__bitbuffer_write_raw_uint32(bb, (uint32)val, bits);
332 bool FLAC__bitbuffer_write_raw_uint64(FLAC__BitBuffer *bb, uint64 val, unsigned bits)
334 static const uint64 mask[] = {
336 0x0000000000000001, 0x0000000000000003, 0x0000000000000007, 0x000000000000000F,
337 0x000000000000001F, 0x000000000000003F, 0x000000000000007F, 0x00000000000000FF,
338 0x00000000000001FF, 0x00000000000003FF, 0x00000000000007FF, 0x0000000000000FFF,
339 0x0000000000001FFF, 0x0000000000003FFF, 0x0000000000007FFF, 0x000000000000FFFF,
340 0x000000000001FFFF, 0x000000000003FFFF, 0x000000000007FFFF, 0x00000000000FFFFF,
341 0x00000000001FFFFF, 0x00000000003FFFFF, 0x00000000007FFFFF, 0x0000000000FFFFFF,
342 0x0000000001FFFFFF, 0x0000000003FFFFFF, 0x0000000007FFFFFF, 0x000000000FFFFFFF,
343 0x000000001FFFFFFF, 0x000000003FFFFFFF, 0x000000007FFFFFFF, 0x00000000FFFFFFFF,
344 0x00000001FFFFFFFF, 0x00000003FFFFFFFF, 0x00000007FFFFFFFF, 0x0000000FFFFFFFFF,
345 0x0000001FFFFFFFFF, 0x0000003FFFFFFFFF, 0x0000007FFFFFFFFF, 0x000000FFFFFFFFFF,
346 0x000001FFFFFFFFFF, 0x000003FFFFFFFFFF, 0x000007FFFFFFFFFF, 0x00000FFFFFFFFFFF,
347 0x00001FFFFFFFFFFF, 0x00003FFFFFFFFFFF, 0x00007FFFFFFFFFFF, 0x0000FFFFFFFFFFFF,
348 0x0001FFFFFFFFFFFF, 0x0003FFFFFFFFFFFF, 0x0007FFFFFFFFFFFF, 0x000FFFFFFFFFFFFF,
349 0x001FFFFFFFFFFFFF, 0x003FFFFFFFFFFFFF, 0x007FFFFFFFFFFFFF, 0x00FFFFFFFFFFFFFF,
350 0x01FFFFFFFFFFFFFF, 0x03FFFFFFFFFFFFFF, 0x07FFFFFFFFFFFFFF, 0x0FFFFFFFFFFFFFFF,
351 0x1FFFFFFFFFFFFFFF, 0x3FFFFFFFFFFFFFFF, 0x7FFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF
356 assert(bb->buffer != 0);
361 if(!bitbuffer_ensure_size_(bb, bits))
364 bb->total_bits += bits;
368 bb->buffer[bb->bytes] = val;
373 bb->buffer[bb->bytes++] = val;
378 bb->buffer[bb->bytes++] = val >> k;
379 val &= (~(0xffffffffffffffff << k));
384 n = min(8 - bb->bits, bits);
386 bb->buffer[bb->bytes] <<= n;
387 bb->buffer[bb->bytes] |= (val>>k);
388 val &= (~(0xffffffffffffffff << k));
401 bool FLAC__bitbuffer_write_raw_int64(FLAC__BitBuffer *bb, int64 val, unsigned bits)
403 return FLAC__bitbuffer_write_raw_uint64(bb, (uint64)val, bits);
406 bool FLAC__bitbuffer_write_unary_unsigned(FLAC__BitBuffer *bb, unsigned val)
409 return FLAC__bitbuffer_write_raw_uint32(bb, 1, ++val);
411 return FLAC__bitbuffer_write_raw_uint64(bb, 1, ++val);
413 if(!FLAC__bitbuffer_write_zeroes(bb, val))
415 return FLAC__bitbuffer_write_raw_uint32(bb, 1, 1);
419 unsigned FLAC__bitbuffer_rice_bits(int val, unsigned parameter)
423 /* convert signed to unsigned */
426 * (unsigned)(((--val) << 1) - 1);
427 * but without the overflow problem at -MAXINT
429 uval = (unsigned)(((-(++val)) << 1) + 1);
431 uval = (unsigned)(val << 1);
433 msbs = uval >> parameter;
435 return 1 + parameter + msbs;
438 unsigned FLAC__bitbuffer_golomb_bits_signed(int val, unsigned parameter)
440 unsigned bits, msbs, uval;
443 assert(parameter > 0);
445 /* convert signed to unsigned */
448 * (unsigned)(((--val) << 1) - 1);
449 * but without the overflow problem at -MAXINT
451 uval = (unsigned)(((-(++val)) << 1) + 1);
453 uval = (unsigned)(val << 1);
455 k = FLAC__bitmath_ilog2(parameter);
456 if(parameter == 1u<<k) {
465 d = (1 << (k+1)) - parameter;
466 q = uval / parameter;
467 r = uval - (q * parameter);
476 unsigned FLAC__bitbuffer_golomb_bits_unsigned(unsigned uval, unsigned parameter)
481 assert(parameter > 0);
483 k = FLAC__bitmath_ilog2(parameter);
484 if(parameter == 1u<<k) {
493 d = (1 << (k+1)) - parameter;
494 q = uval / parameter;
495 r = uval - (q * parameter);
504 bool FLAC__bitbuffer_write_symmetric_rice_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
506 unsigned total_bits, interesting_bits, msbs;
510 assert(bb->buffer != 0);
511 assert(parameter <= 31);
513 /* init pattern with the unary end bit and the sign bit */
521 msbs = val >> parameter;
522 interesting_bits = 2 + parameter;
523 total_bits = interesting_bits + msbs;
524 pattern <<= parameter;
525 pattern |= (val & ((1<<parameter)-1)); /* the binary LSBs */
527 if(total_bits <= 32) {
528 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
532 /* write the unary MSBs */
533 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
535 /* write the unary end bit, the sign bit, and binary LSBs */
536 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
542 bool FLAC__bitbuffer_write_symmetric_rice_signed_guarded(FLAC__BitBuffer *bb, int val, unsigned parameter, unsigned max_bits, bool *overflow)
544 unsigned total_bits, interesting_bits, msbs;
548 assert(bb->buffer != 0);
549 assert(parameter <= 31);
553 /* init pattern with the unary end bit and the sign bit */
561 msbs = val >> parameter;
562 interesting_bits = 2 + parameter;
563 total_bits = interesting_bits + msbs;
564 pattern <<= parameter;
565 pattern |= (val & ((1<<parameter)-1)); /* the binary LSBs */
567 if(total_bits <= 32) {
568 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
571 else if(total_bits > max_bits) {
576 /* write the unary MSBs */
577 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
579 /* write the unary end bit, the sign bit, and binary LSBs */
580 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
586 bool FLAC__bitbuffer_write_symmetric_rice_signed_escape(FLAC__BitBuffer *bb, int val, unsigned parameter)
588 unsigned total_bits, val_bits;
592 assert(bb->buffer != 0);
593 assert(parameter <= 31);
595 val_bits = FLAC__bitmath_silog2(val);
596 total_bits = 2 + parameter + 5 + val_bits;
598 if(total_bits <= 32) {
600 pattern <<= (parameter + 5);
602 pattern <<= val_bits;
603 pattern |= (val & ((1 << val_bits) - 1));
604 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
608 /* write the '-0' escape code first */
609 if(!FLAC__bitbuffer_write_raw_uint32(bb, 3u << parameter, 2+parameter))
611 /* write the length */
612 if(!FLAC__bitbuffer_write_raw_uint32(bb, val_bits, 5))
614 /* write the value */
615 if(!FLAC__bitbuffer_write_raw_int32(bb, val, val_bits))
621 bool FLAC__bitbuffer_write_rice_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
623 unsigned total_bits, interesting_bits, msbs, uval;
627 assert(bb->buffer != 0);
628 assert(parameter <= 30);
630 /* convert signed to unsigned */
633 * (unsigned)(((--val) << 1) - 1);
634 * but without the overflow problem at -MAXINT
636 uval = (unsigned)(((-(++val)) << 1) + 1);
638 uval = (unsigned)(val << 1);
640 msbs = uval >> parameter;
641 interesting_bits = 1 + parameter;
642 total_bits = interesting_bits + msbs;
643 pattern = 1 << parameter; /* the unary end bit */
644 pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
646 if(total_bits <= 32) {
647 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
651 /* write the unary MSBs */
652 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
654 /* write the unary end bit and binary LSBs */
655 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
661 bool FLAC__bitbuffer_write_rice_signed_guarded(FLAC__BitBuffer *bb, int val, unsigned parameter, unsigned max_bits, bool *overflow)
663 unsigned total_bits, interesting_bits, msbs, uval;
667 assert(bb->buffer != 0);
668 assert(parameter <= 30);
672 /* convert signed to unsigned */
675 * (unsigned)(((--val) << 1) - 1);
676 * but without the overflow problem at -MAXINT
678 uval = (unsigned)(((-(++val)) << 1) + 1);
680 uval = (unsigned)(val << 1);
682 msbs = uval >> parameter;
683 interesting_bits = 1 + parameter;
684 total_bits = interesting_bits + msbs;
685 pattern = 1 << parameter; /* the unary end bit */
686 pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
688 if(total_bits <= 32) {
689 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
692 else if(total_bits > max_bits) {
697 /* write the unary MSBs */
698 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
700 /* write the unary end bit and binary LSBs */
701 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, interesting_bits))
707 bool FLAC__bitbuffer_write_golomb_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
709 unsigned total_bits, msbs, uval;
713 assert(bb->buffer != 0);
714 assert(parameter > 0);
716 /* convert signed to unsigned */
719 * (unsigned)(((--val) << 1) - 1);
720 * but without the overflow problem at -MAXINT
722 uval = (unsigned)(((-(++val)) << 1) + 1);
724 uval = (unsigned)(val << 1);
726 k = FLAC__bitmath_ilog2(parameter);
727 if(parameter == 1u<<k) {
733 total_bits = 1 + k + msbs;
734 pattern = 1 << k; /* the unary end bit */
735 pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
737 if(total_bits <= 32) {
738 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
742 /* write the unary MSBs */
743 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
745 /* write the unary end bit and binary LSBs */
746 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, k+1))
753 d = (1 << (k+1)) - parameter;
754 q = uval / parameter;
755 r = uval - (q * parameter);
756 /* write the unary MSBs */
757 if(!FLAC__bitbuffer_write_zeroes(bb, q))
759 /* write the unary end bit */
760 if(!FLAC__bitbuffer_write_raw_uint32(bb, 1, 1))
762 /* write the binary LSBs */
764 if(!FLAC__bitbuffer_write_raw_uint32(bb, r+d, k+1))
768 if(!FLAC__bitbuffer_write_raw_uint32(bb, r, k))
775 bool FLAC__bitbuffer_write_golomb_unsigned(FLAC__BitBuffer *bb, unsigned uval, unsigned parameter)
777 unsigned total_bits, msbs;
781 assert(bb->buffer != 0);
782 assert(parameter > 0);
784 k = FLAC__bitmath_ilog2(parameter);
785 if(parameter == 1u<<k) {
791 total_bits = 1 + k + msbs;
792 pattern = 1 << k; /* the unary end bit */
793 pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
795 if(total_bits <= 32) {
796 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, total_bits))
800 /* write the unary MSBs */
801 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
803 /* write the unary end bit and binary LSBs */
804 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, k+1))
811 d = (1 << (k+1)) - parameter;
812 q = uval / parameter;
813 r = uval - (q * parameter);
814 /* write the unary MSBs */
815 if(!FLAC__bitbuffer_write_zeroes(bb, q))
817 /* write the unary end bit */
818 if(!FLAC__bitbuffer_write_raw_uint32(bb, 1, 1))
820 /* write the binary LSBs */
822 if(!FLAC__bitbuffer_write_raw_uint32(bb, r+d, k+1))
826 if(!FLAC__bitbuffer_write_raw_uint32(bb, r, k))
833 bool FLAC__bitbuffer_write_utf8_uint32(FLAC__BitBuffer *bb, uint32 val)
838 assert(bb->buffer != 0);
840 assert(!(val & 0x80000000)); /* this version only handles 31 bits */
843 return FLAC__bitbuffer_write_raw_uint32(bb, val, 8);
845 else if(val < 0x800) {
846 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xC0 | (val>>6), 8);
847 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
849 else if(val < 0x10000) {
850 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xE0 | (val>>12), 8);
851 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
852 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
854 else if(val < 0x200000) {
855 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF0 | (val>>18), 8);
856 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
857 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
858 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
860 else if(val < 0x4000000) {
861 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF8 | (val>>24), 8);
862 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>18)&0x3F), 8);
863 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
864 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
865 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
868 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFC | (val>>30), 8);
869 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>24)&0x3F), 8);
870 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>18)&0x3F), 8);
871 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
872 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
873 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
879 bool FLAC__bitbuffer_write_utf8_uint64(FLAC__BitBuffer *bb, uint64 val)
884 assert(bb->buffer != 0);
886 assert(!(val & 0xFFFFFFF000000000)); /* this version only handles 36 bits */
889 return FLAC__bitbuffer_write_raw_uint32(bb, (uint32)val, 8);
891 else if(val < 0x800) {
892 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xC0 | (uint32)(val>>6), 8);
893 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
895 else if(val < 0x10000) {
896 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xE0 | (uint32)(val>>12), 8);
897 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>6)&0x3F), 8);
898 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
900 else if(val < 0x200000) {
901 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF0 | (uint32)(val>>18), 8);
902 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>12)&0x3F), 8);
903 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>6)&0x3F), 8);
904 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
906 else if(val < 0x4000000) {
907 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF8 | (uint32)(val>>24), 8);
908 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>18)&0x3F), 8);
909 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>12)&0x3F), 8);
910 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>6)&0x3F), 8);
911 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
913 else if(val < 0x80000000) {
914 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFC | (uint32)(val>>30), 8);
915 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>24)&0x3F), 8);
916 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>18)&0x3F), 8);
917 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>12)&0x3F), 8);
918 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>6)&0x3F), 8);
919 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
922 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFE, 8);
923 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>30)&0x3F), 8);
924 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>24)&0x3F), 8);
925 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>18)&0x3F), 8);
926 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>12)&0x3F), 8);
927 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>6)&0x3F), 8);
928 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
934 bool FLAC__bitbuffer_zero_pad_to_byte_boundary(FLAC__BitBuffer *bb)
936 /* 0-pad to byte boundary */
938 return FLAC__bitbuffer_write_zeroes(bb, 8 - bb->bits);
943 bool FLAC__bitbuffer_peek_bit(FLAC__BitBuffer *bb, unsigned *val, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
945 /* to avoid a drastic speed penalty we don't:
947 assert(bb->buffer != 0);
948 assert(bb->bits == 0);
952 if(bb->total_consumed_bits < bb->total_bits) {
953 *val = (bb->buffer[bb->consumed_bytes] & BYTE_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
957 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
963 bool FLAC__bitbuffer_read_bit(FLAC__BitBuffer *bb, unsigned *val, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
965 /* to avoid a drastic speed penalty we don't:
967 assert(bb->buffer != 0);
968 assert(bb->bits == 0);
972 if(bb->total_consumed_bits < bb->total_bits) {
973 *val = (bb->buffer[bb->consumed_bytes] & BYTE_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
975 if(bb->consumed_bits == 8) {
976 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
977 bb->consumed_bytes++;
978 bb->consumed_bits = 0;
980 bb->total_consumed_bits++;
984 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
990 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)
992 /* to avoid a drastic speed penalty we don't:
994 assert(bb->buffer != 0);
995 assert(bb->bits == 0);
999 if(bb->total_consumed_bits < bb->total_bits) {
1001 *val |= (bb->buffer[bb->consumed_bytes] & BYTE_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
1002 bb->consumed_bits++;
1003 if(bb->consumed_bits == 8) {
1004 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1005 bb->consumed_bytes++;
1006 bb->consumed_bits = 0;
1008 bb->total_consumed_bits++;
1012 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1018 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)
1020 /* to avoid a drastic speed penalty we don't:
1022 assert(bb->buffer != 0);
1023 assert(bb->bits == 0);
1027 if(bb->total_consumed_bits < bb->total_bits) {
1029 *val |= (bb->buffer[bb->consumed_bytes] & BYTE_BIT_TO_MASK(bb->consumed_bits))? 1 : 0;
1030 bb->consumed_bits++;
1031 if(bb->consumed_bits == 8) {
1032 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1033 bb->consumed_bytes++;
1034 bb->consumed_bits = 0;
1036 bb->total_consumed_bits++;
1040 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1046 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)
1047 #ifdef FLAC__NO_MANUAL_INLINING
1052 assert(bb->buffer != 0);
1057 for(i = 0; i < bits; i++) {
1058 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, val, read_callback, client_data))
1065 unsigned i, bits_ = bits;
1069 assert(bb->buffer != 0);
1072 assert((bb->capacity*8) * 2 >= bits);
1074 while(bb->total_consumed_bits + bits > bb->total_bits) {
1075 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1078 if(bb->consumed_bits) {
1079 i = 8 - bb->consumed_bits;
1081 v = bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits);
1083 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1084 bb->consumed_bytes++;
1085 bb->consumed_bits = 0;
1086 /* we hold off updating bb->total_consumed_bits until the end */
1089 *val = (bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits)) >> (i-bits_);
1090 bb->consumed_bits += bits_;
1091 bb->total_consumed_bits += bits_;
1097 v |= bb->buffer[bb->consumed_bytes];
1099 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1100 bb->consumed_bytes++;
1101 /* bb->consumed_bits is already 0 */
1102 /* we hold off updating bb->total_consumed_bits until the end */
1106 v |= (bb->buffer[bb->consumed_bytes] >> (8-bits_));
1107 bb->consumed_bits = bits_;
1108 /* we hold off updating bb->total_consumed_bits until the end */
1110 bb->total_consumed_bits += bits;
1116 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)
1117 #ifdef FLAC__NO_MANUAL_INLINING
1123 assert(bb->buffer != 0);
1128 for(i = 0; i < bits; i++) {
1129 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &v, read_callback, client_data))
1147 unsigned i, bits_ = bits;
1151 assert(bb->buffer != 0);
1154 assert((bb->capacity*8) * 2 >= bits);
1156 while(bb->total_consumed_bits + bits > bb->total_bits) {
1157 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1160 if(bb->consumed_bits) {
1161 i = 8 - bb->consumed_bits;
1163 v = bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits);
1165 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1166 bb->consumed_bytes++;
1167 bb->consumed_bits = 0;
1168 /* we hold off updating bb->total_consumed_bits until the end */
1171 /* bits_ must be < 7 if we get to here */
1172 v = (bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits));
1175 *val >>= (32-bits_);
1176 bb->consumed_bits += bits_;
1177 bb->total_consumed_bits += bits_;
1183 v |= bb->buffer[bb->consumed_bytes];
1185 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1186 bb->consumed_bytes++;
1187 /* bb->consumed_bits is already 0 */
1188 /* we hold off updating bb->total_consumed_bits until the end */
1192 v |= (bb->buffer[bb->consumed_bytes] >> (8-bits_));
1193 bb->consumed_bits = bits_;
1194 /* we hold off updating bb->total_consumed_bits until the end */
1196 bb->total_consumed_bits += bits;
1212 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)
1213 #ifdef FLAC__NO_MANUAL_INLINING
1218 assert(bb->buffer != 0);
1223 for(i = 0; i < bits; i++) {
1224 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, val, read_callback, client_data))
1231 unsigned i, bits_ = bits;
1235 assert(bb->buffer != 0);
1238 assert((bb->capacity*8) * 2 >= bits);
1240 while(bb->total_consumed_bits + bits > bb->total_bits) {
1241 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1244 if(bb->consumed_bits) {
1245 i = 8 - bb->consumed_bits;
1247 v = bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits);
1249 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1250 bb->consumed_bytes++;
1251 bb->consumed_bits = 0;
1252 /* we hold off updating bb->total_consumed_bits until the end */
1255 *val = (bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits)) >> (i-bits_);
1256 bb->consumed_bits += bits_;
1257 bb->total_consumed_bits += bits_;
1263 v |= bb->buffer[bb->consumed_bytes];
1265 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1266 bb->consumed_bytes++;
1267 /* bb->consumed_bits is already 0 */
1268 /* we hold off updating bb->total_consumed_bits until the end */
1272 v |= (bb->buffer[bb->consumed_bytes] >> (8-bits_));
1273 bb->consumed_bits = bits_;
1274 /* we hold off updating bb->total_consumed_bits until the end */
1276 bb->total_consumed_bits += bits;
1282 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)
1283 #ifdef FLAC__NO_MANUAL_INLINING
1289 assert(bb->buffer != 0);
1294 for(i = 0; i < bits; i++) {
1295 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, &v, read_callback, client_data))
1312 unsigned i, bits_ = bits;
1316 assert(bb->buffer != 0);
1319 assert((bb->capacity*8) * 2 >= bits);
1321 while(bb->total_consumed_bits + bits > bb->total_bits) {
1322 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1325 if(bb->consumed_bits) {
1326 i = 8 - bb->consumed_bits;
1328 v = bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits);
1330 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1331 bb->consumed_bytes++;
1332 bb->consumed_bits = 0;
1333 /* we hold off updating bb->total_consumed_bits until the end */
1336 /* bits_ must be < 7 if we get to here */
1337 v = (bb->buffer[bb->consumed_bytes] & (0xff >> bb->consumed_bits));
1340 *val >>= (64-bits_);
1341 bb->consumed_bits += bits_;
1342 bb->total_consumed_bits += bits_;
1348 v |= bb->buffer[bb->consumed_bytes];
1350 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1351 bb->consumed_bytes++;
1352 /* bb->consumed_bits is already 0 */
1353 /* we hold off updating bb->total_consumed_bits until the end */
1357 v |= (bb->buffer[bb->consumed_bytes] >> (8-bits_));
1358 bb->consumed_bits = bits_;
1359 /* we hold off updating bb->total_consumed_bits until the end */
1361 bb->total_consumed_bits += bits;
1377 bool FLAC__bitbuffer_read_unary_unsigned(FLAC__BitBuffer *bb, unsigned *val, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
1378 #ifdef FLAC__NO_MANUAL_INLINING
1380 unsigned bit, val_ = 0;
1383 assert(bb->buffer != 0);
1386 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1398 unsigned i, val_ = 0;
1399 unsigned total_bytes_ = (bb->total_bits + 7) / 8;
1403 assert(bb->buffer != 0);
1405 if(bb->consumed_bits) {
1406 b = bb->buffer[bb->consumed_bytes] << bb->consumed_bits;
1408 for(i = 0; !(b & 0x80); i++)
1412 bb->consumed_bits += i;
1413 bb->total_consumed_bits += i;
1414 if(bb->consumed_bits == 8) {
1415 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1416 bb->consumed_bytes++;
1417 bb->consumed_bits = 0;
1422 val_ = 8 - bb->consumed_bits;
1423 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1424 bb->consumed_bytes++;
1425 bb->consumed_bits = 0;
1426 bb->total_consumed_bits += val_;
1430 if(bb->consumed_bytes >= total_bytes_) {
1431 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
1433 total_bytes_ = (bb->total_bits + 7) / 8;
1435 b = bb->buffer[bb->consumed_bytes];
1437 for(i = 0; !(b & 0x80); i++)
1441 bb->consumed_bits = i;
1444 FLAC__CRC16_UPDATE(bb->buffer[bb->consumed_bytes], bb->read_crc16);
1445 bb->consumed_bytes++;
1446 bb->consumed_bits = 0;
1448 bb->total_consumed_bits += i;
1453 FLAC__CRC16_UPDATE(0, bb->read_crc16);
1454 bb->consumed_bytes++;
1455 /* bb->consumed_bits is already 0 */
1456 /* we hold off updating bb->total_consumed_bits until the end */
1457 bb->total_consumed_bits += 8;
1463 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)
1465 uint32 sign = 0, lsbs = 0, msbs = 0;
1468 assert(bb->buffer != 0);
1469 assert(parameter <= 31);
1471 /* read the unary MSBs and end bit */
1472 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
1475 /* read the sign bit */
1476 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &sign, read_callback, client_data))
1479 /* read the binary LSBs */
1480 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, parameter, read_callback, client_data))
1483 /* compose the value */
1484 *val = (msbs << parameter) | lsbs;
1491 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)
1493 uint32 lsbs = 0, msbs = 0;
1497 assert(bb->buffer != 0);
1498 assert(parameter <= 31);
1500 /* read the unary MSBs and end bit */
1501 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
1504 /* read the binary LSBs */
1505 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, parameter, read_callback, client_data))
1508 /* compose the value */
1509 uval = (msbs << parameter) | lsbs;
1511 *val = -((int)(uval >> 1)) - 1;
1513 *val = (int)(uval >> 1);
1518 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)
1520 uint32 lsbs = 0, msbs = 0;
1521 unsigned bit, uval, k;
1524 assert(bb->buffer != 0);
1526 k = FLAC__bitmath_ilog2(parameter);
1528 /* read the unary MSBs and end bit */
1529 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
1532 /* read the binary LSBs */
1533 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, k, read_callback, client_data))
1536 if(parameter == 1u<<k) {
1537 /* compose the value */
1538 uval = (msbs << k) | lsbs;
1541 unsigned d = (1 << (k+1)) - parameter;
1543 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1549 /* compose the value */
1550 uval = msbs * parameter + lsbs;
1553 /* unfold unsigned to signed */
1555 *val = -((int)(uval >> 1)) - 1;
1557 *val = (int)(uval >> 1);
1562 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)
1564 uint32 lsbs, msbs = 0;
1568 assert(bb->buffer != 0);
1570 k = FLAC__bitmath_ilog2(parameter);
1572 /* read the unary MSBs and end bit */
1573 if(!FLAC__bitbuffer_read_unary_unsigned(bb, &msbs, read_callback, client_data))
1576 /* read the binary LSBs */
1577 if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, k, read_callback, client_data))
1580 if(parameter == 1u<<k) {
1581 /* compose the value */
1582 *val = (msbs << k) | lsbs;
1585 unsigned d = (1 << (k+1)) - parameter;
1587 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
1593 /* compose the value */
1594 *val = msbs * parameter + lsbs;
1600 /* on return, if *val == 0xffffffff then the utf-8 sequence was invalid, but the return value will be true */
1601 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)
1607 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
1610 raw[(*rawlen)++] = (byte)x;
1611 if(!(x & 0x80)) { /* 0xxxxxxx */
1615 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
1619 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
1623 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
1627 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
1631 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
1640 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
1643 raw[(*rawlen)++] = (byte)x;
1644 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
1655 /* on return, if *val == 0xffffffffffffffff then the utf-8 sequence was invalid, but the return value will be true */
1656 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)
1662 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
1665 raw[(*rawlen)++] = (byte)x;
1666 if(!(x & 0x80)) { /* 0xxxxxxx */
1670 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
1674 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
1678 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
1682 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
1686 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
1690 else if(x & 0xFE && !(x & 0x01)) { /* 11111110 */
1695 *val = 0xffffffffffffffff;
1699 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
1702 raw[(*rawlen)++] = (byte)x;
1703 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
1704 *val = 0xffffffffffffffff;
1714 void FLAC__bitbuffer_dump(const FLAC__BitBuffer *bb, FILE *out)
1718 fprintf(out, "bitbuffer is NULL\n");
1721 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);
1722 for(i = 0; i < bb->bytes; i++) {
1723 fprintf(out, "%08X: ", i);
1724 for(j = 0; j < 8; j++)
1725 if(i*8+j < bb->total_consumed_bits)
1728 fprintf(out, "%01u", bb->buffer[i] & (1 << (8-j-1)) ? 1:0);
1732 fprintf(out, "%08X: ", i);
1733 for(j = 0; j < bb->bits; j++)
1734 if(i*8+j < bb->total_consumed_bits)
1737 fprintf(out, "%01u", bb->buffer[i] & (1 << (bb->bits-j-1)) ? 1:0);