Initial revision
[flac.git] / src / libFLAC / bitbuffer.c
1 /* libFLAC - Free Lossless Audio Coder library
2  * Copyright (C) 2000  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 static const unsigned FLAC__BITBUFFER_DEFAULT_CAPACITY = 65536; /* bytes */
26
27 #ifdef min
28 #undef min
29 #endif
30 #define min(x,y) ((x)<(y)?(x):(y))
31 #ifdef max
32 #undef max
33 #endif
34 #define max(x,y) ((x)>(y)?(x):(y))
35
36 static bool bitbuffer_resize_(FLAC__BitBuffer *bb, unsigned new_capacity)
37 {
38         byte *new_buffer;
39
40         assert(bb != 0);
41         assert(bb->buffer != 0);
42
43         if(bb->capacity == new_capacity)
44                 return true;
45
46         new_buffer = (byte*)malloc(sizeof(byte) * new_capacity);
47         if(new_buffer == 0)
48                 return false;
49         memset(new_buffer, 0, new_capacity);
50         memcpy(new_buffer, bb->buffer, sizeof(byte)*min(bb->bytes+(bb->bits?1:0), new_capacity));
51         if(new_capacity < bb->bytes+(bb->bits?1:0)) {
52                 bb->bytes = new_capacity;
53                 bb->bits = 0;
54                 bb->total_bits = (new_capacity<<3);
55         }
56         if(new_capacity < bb->consumed_bytes+(bb->consumed_bits?1:0)) {
57                 bb->consumed_bytes = new_capacity;
58                 bb->consumed_bits = 0;
59                 bb->total_consumed_bits = (new_capacity<<3);
60         }
61         bb->buffer = new_buffer;
62         bb->capacity = new_capacity;
63         return true;
64 }
65
66 static bool bitbuffer_grow_(FLAC__BitBuffer *bb, unsigned min_bytes_to_add)
67 {
68         unsigned new_capacity;
69
70         assert(min_bytes_to_add > 0);
71
72         new_capacity = max(bb->capacity * 4, bb->capacity + min_bytes_to_add);
73         return bitbuffer_resize_(bb, new_capacity);
74 }
75
76 static bool bitbuffer_ensure_size_(FLAC__BitBuffer *bb, unsigned bits_to_add)
77 {
78         assert(bb != 0);
79         assert(bb->buffer != 0);
80         if((bb->capacity<<3) < bb->total_bits + bits_to_add)
81                 return bitbuffer_grow_(bb, (bits_to_add>>3)+2);
82         else
83                 return true;
84 }
85
86 static bool bitbuffer_read_from_client_(FLAC__BitBuffer *bb, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
87 {
88         unsigned bytes;
89
90         /* first shift the unconsumed buffer data toward the front as much as possible */
91         if(bb->total_consumed_bits >= 8) {
92                 unsigned l = 0, r = bb->consumed_bytes, r_end = bb->bytes;
93                 for( ; r < r_end; l++, r++)
94                         bb->buffer[l] = bb->buffer[r];
95                 for( ; l < r_end; l++)
96                         bb->buffer[l] = 0;
97                 bb->bytes -= bb->consumed_bytes;
98                 bb->total_bits -= (bb->consumed_bytes<<3);
99                 bb->consumed_bytes = 0;
100                 bb->total_consumed_bits = bb->consumed_bits;
101         }
102         /* grow if we need to */
103         if(bb->capacity <= 1) {
104                 if(!bitbuffer_resize_(bb, 16))
105                         return false;
106         }
107         /* finally, read in some data; if OK, go back to read_bit_, else fail */
108         bytes = bb->capacity - bb->bytes;
109         if(!read_callback(bb->buffer+bb->bytes, &bytes, client_data))
110                 return false;
111         bb->bytes += bytes;
112         bb->total_bits += (bytes<<3);
113         return true;
114 }
115
116 void FLAC__bitbuffer_init(FLAC__BitBuffer *bb)
117 {
118         assert(bb != 0);
119         bb->buffer = 0;
120         bb->capacity = 0;
121         bb->bytes = bb->bits = bb->total_bits = 0;
122         bb->consumed_bytes = bb->consumed_bits = bb->total_consumed_bits = 0;
123 }
124
125 bool FLAC__bitbuffer_init_from(FLAC__BitBuffer *bb, const byte buffer[], unsigned bytes)
126 {
127         assert(bb != 0);
128         FLAC__bitbuffer_init(bb);
129         if(bytes == 0)
130                 return true;
131         else {
132                 assert(buffer != 0);
133                 bb->buffer = (byte*)malloc(sizeof(byte)*bytes);
134                 if(bb->buffer == 0)
135                         return false;
136                 memcpy(bb->buffer, buffer, sizeof(byte)*bytes);
137                 bb->capacity = bb->bytes = bytes;
138                 bb->bits = 0;
139                 bb->total_bits = (bytes<<3);
140                 bb->consumed_bytes = bb->consumed_bits = bb->total_consumed_bits = 0;
141                 return true;
142         }
143 }
144
145 bool FLAC__bitbuffer_concatenate_aligned(FLAC__BitBuffer *dest, const FLAC__BitBuffer *src)
146 {
147         static byte mask_[9] = { 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff };
148         unsigned bits_to_add = src->total_bits - src->total_consumed_bits;
149         assert(dest != 0);
150         assert(src != 0);
151
152         if(bits_to_add == 0)
153                 return true;
154         if(dest->bits != src->consumed_bits)
155                 return false;
156         if(!bitbuffer_ensure_size_(dest, bits_to_add))
157                 return false;
158         if(dest->bits == 0) {
159                 memcpy(dest->buffer+dest->bytes, src->buffer+src->consumed_bytes, src->bytes-src->consumed_bytes + ((src->bits)? 1:0));
160         }
161         else if(dest->bits + bits_to_add > 8) {
162                 dest->buffer[dest->bytes] <<= (8 - dest->bits);
163                 dest->buffer[dest->bytes] |= (src->buffer[src->consumed_bytes] & mask_[8-dest->bits]);
164                 memcpy(dest->buffer+dest->bytes+1, src->buffer+src->consumed_bytes+1, src->bytes-src->consumed_bytes-1 + ((src->bits)? 1:0));
165         }
166         else {
167                 dest->buffer[dest->bytes] <<= bits_to_add;
168                 dest->buffer[dest->bytes] |= (src->buffer[src->consumed_bytes] & mask_[bits_to_add]);
169         }
170         dest->bits = src->bits;
171         dest->total_bits += bits_to_add;
172         dest->bytes = dest->total_bits / 8;
173
174         return true;
175 }
176
177 void FLAC__bitbuffer_free(FLAC__BitBuffer *bb)
178 {
179         assert(bb != 0);
180         if(bb->buffer != 0)
181                 free(bb->buffer);
182         bb->buffer = 0;
183         bb->capacity = 0;
184         bb->bytes = bb->bits = bb->total_bits = 0;
185         bb->consumed_bytes = bb->consumed_bits = bb->total_consumed_bits = 0;
186 }
187
188 bool FLAC__bitbuffer_clear(FLAC__BitBuffer *bb)
189 {
190         if(bb->buffer == 0) {
191                 bb->capacity = FLAC__BITBUFFER_DEFAULT_CAPACITY;
192                 bb->buffer = (byte*)malloc(sizeof(byte) * bb->capacity);
193                 if(bb->buffer == 0)
194                         return false;
195                 memset(bb->buffer, 0, bb->capacity);
196         }
197         else {
198                 memset(bb->buffer, 0, bb->bytes + (bb->bits?1:0));
199         }
200         bb->bytes = bb->bits = bb->total_bits = 0;
201         bb->consumed_bytes = bb->consumed_bits = bb->total_consumed_bits = 0;
202         return true;
203 }
204
205 bool FLAC__bitbuffer_clone(FLAC__BitBuffer *dest, const FLAC__BitBuffer *src)
206 {
207         if(dest->capacity < src->capacity)
208                 if(!bitbuffer_resize_(dest, src->capacity))
209                         return false;
210         memcpy(dest->buffer, src->buffer, sizeof(byte)*min(src->capacity, src->bytes+1));
211         dest->bytes = src->bytes;
212         dest->bits = src->bits;
213         dest->total_bits = src->total_bits;
214         dest->consumed_bytes = src->consumed_bytes;
215         dest->consumed_bits = src->consumed_bits;
216         dest->total_consumed_bits = src->total_consumed_bits;
217         return true;
218 }
219
220 bool FLAC__bitbuffer_write_zeroes(FLAC__BitBuffer *bb, unsigned bits)
221 {
222         unsigned n, k;
223
224         assert(bb != 0);
225         assert(bb->buffer != 0);
226
227         if(bits == 0)
228                 return true;
229         if(!bitbuffer_ensure_size_(bb, bits))
230                 return false;
231         bb->total_bits += bits;
232         while(bits > 0) {
233                 n = min(8 - bb->bits, bits);
234                 k = bits - n;
235                 bb->buffer[bb->bytes] <<= n;
236                 bits -= n;
237                 bb->bits += n;
238                 if(bb->bits == 8) {
239                         bb->bytes++;
240                         bb->bits = 0;
241                 }
242         }
243         return true;
244 }
245
246 bool FLAC__bitbuffer_write_raw_uint32(FLAC__BitBuffer *bb, uint32 val, unsigned bits)
247 {
248         static uint32 mask[] = {
249                 0,
250                 0x00000001, 0x00000003, 0x00000007, 0x0000000F,
251                 0x0000001F, 0x0000003F, 0x0000007F, 0x000000FF,
252                 0x000001FF, 0x000003FF, 0x000007FF, 0x00000FFF,
253                 0x00001FFF, 0x00003FFF, 0x00007FFF, 0x0000FFFF,
254                 0x0001FFFF, 0x0003FFFF, 0x0007FFFF, 0x000FFFFF,
255                 0x001FFFFF, 0x003FFFFF, 0x007FFFFF, 0x00FFFFFF,
256                 0x01FFFFFF, 0x03FFFFFF, 0x07FFFFFF, 0x0FFFFFFF,
257                 0x1FFFFFFF, 0x3FFFFFFF, 0x7FFFFFFF, 0xFFFFFFFF
258         };
259         unsigned n, k;
260
261         assert(bb != 0);
262         assert(bb->buffer != 0);
263
264         assert(bits <= 32);
265         if(bits == 0)
266                 return true;
267         if(!bitbuffer_ensure_size_(bb, bits))
268                 return false;
269         val &= mask[bits];
270         bb->total_bits += bits;
271         while(bits > 0) {
272                 n = 8 - bb->bits;
273                 if(n == 8) { /* i.e. bb->bits == 0 */
274                         if(bits < 8) {
275                                 bb->buffer[bb->bytes] = val;
276                                 bb->bits = bits;
277                                 break;
278                         }
279                         else if(bits == 8) {
280                                 bb->buffer[bb->bytes++] = val;
281                                 break;
282                         }
283                         else {
284                                 k = bits - 8;
285                                 bb->buffer[bb->bytes++] = val >> k;
286                                 val &= (~(0xffffffff << k));
287                                 bits -= 8;
288                         }
289                 }
290                 else if(bits <= n) {
291                         bb->buffer[bb->bytes] <<= bits;
292                         bb->buffer[bb->bytes] |= val;
293                         if(bits == n) {
294                                 bb->bytes++;
295                                 bb->bits = 0;
296                         }
297                         else
298                                 bb->bits += bits;
299                         break;
300                 }
301                 else {
302                         k = bits - n;
303                         bb->buffer[bb->bytes] <<= n;
304                         bb->buffer[bb->bytes] |= (val>>k);
305                         val &= (~(0xffffffff << k));
306                         bits -= n;
307                         bb->bytes++;
308                         bb->bits = 0;
309                 }
310         }
311
312         return true;
313 }
314
315 bool FLAC__bitbuffer_write_raw_int32(FLAC__BitBuffer *bb, int32 val, unsigned bits)
316 {
317         return FLAC__bitbuffer_write_raw_uint32(bb, (uint32)val, bits);
318 }
319
320 bool FLAC__bitbuffer_write_raw_uint64(FLAC__BitBuffer *bb, uint64 val, unsigned bits)
321 {
322         static uint64 mask[] = {
323                 0,
324                 0x0000000000000001, 0x0000000000000003, 0x0000000000000007, 0x000000000000000F,
325                 0x000000000000001F, 0x000000000000003F, 0x000000000000007F, 0x00000000000000FF,
326                 0x00000000000001FF, 0x00000000000003FF, 0x00000000000007FF, 0x0000000000000FFF,
327                 0x0000000000001FFF, 0x0000000000003FFF, 0x0000000000007FFF, 0x000000000000FFFF,
328                 0x000000000001FFFF, 0x000000000003FFFF, 0x000000000007FFFF, 0x00000000000FFFFF,
329                 0x00000000001FFFFF, 0x00000000003FFFFF, 0x00000000007FFFFF, 0x0000000000FFFFFF,
330                 0x0000000001FFFFFF, 0x0000000003FFFFFF, 0x0000000007FFFFFF, 0x000000000FFFFFFF,
331                 0x000000001FFFFFFF, 0x000000003FFFFFFF, 0x000000007FFFFFFF, 0x00000000FFFFFFFF,
332                 0x00000001FFFFFFFF, 0x00000003FFFFFFFF, 0x00000007FFFFFFFF, 0x0000000FFFFFFFFF,
333                 0x0000001FFFFFFFFF, 0x0000003FFFFFFFFF, 0x0000007FFFFFFFFF, 0x000000FFFFFFFFFF,
334                 0x000001FFFFFFFFFF, 0x000003FFFFFFFFFF, 0x000007FFFFFFFFFF, 0x00000FFFFFFFFFFF,
335                 0x00001FFFFFFFFFFF, 0x00003FFFFFFFFFFF, 0x00007FFFFFFFFFFF, 0x0000FFFFFFFFFFFF,
336                 0x0001FFFFFFFFFFFF, 0x0003FFFFFFFFFFFF, 0x0007FFFFFFFFFFFF, 0x000FFFFFFFFFFFFF,
337                 0x001FFFFFFFFFFFFF, 0x003FFFFFFFFFFFFF, 0x007FFFFFFFFFFFFF, 0x00FFFFFFFFFFFFFF,
338                 0x01FFFFFFFFFFFFFF, 0x03FFFFFFFFFFFFFF, 0x07FFFFFFFFFFFFFF, 0x0FFFFFFFFFFFFFFF,
339                 0x1FFFFFFFFFFFFFFF, 0x3FFFFFFFFFFFFFFF, 0x7FFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF
340         };
341         unsigned n, k;
342
343         assert(bb != 0);
344         assert(bb->buffer != 0);
345
346         assert(bits <= 64);
347         if(bits == 0)
348                 return true;
349         if(!bitbuffer_ensure_size_(bb, bits))
350                 return false;
351         val &= mask[bits];
352         bb->total_bits += bits;
353         while(bits > 0) {
354                 if(bb->bits == 0) {
355                         if(bits < 8) {
356                                 bb->buffer[bb->bytes] = val;
357                                 bb->bits = bits;
358                                 break;
359                         }
360                         else if(bits == 8) {
361                                 bb->buffer[bb->bytes++] = val;
362                                 break;
363                         }
364                         else {
365                                 k = bits - 8;
366                                 bb->buffer[bb->bytes++] = val >> k;
367                                 val &= (~(0xffffffffffffffff << k));
368                                 bits -= 8;
369                         }
370                 }
371                 else {
372                         n = min(8 - bb->bits, bits);
373                         k = bits - n;
374                         bb->buffer[bb->bytes] <<= n;
375                         bb->buffer[bb->bytes] |= (val>>k);
376                         val &= (~(0xffffffffffffffff << k));
377                         bits -= n;
378                         bb->bits += n;
379                         if(bb->bits == 8) {
380                                 bb->bytes++;
381                                 bb->bits = 0;
382                         }
383                 }
384         }
385
386         return true;
387 }
388
389 bool FLAC__bitbuffer_write_raw_int64(FLAC__BitBuffer *bb, int64 val, unsigned bits)
390 {
391         return FLAC__bitbuffer_write_raw_uint64(bb, (uint64)val, bits);
392 }
393
394 bool FLAC__bitbuffer_write_rice_signed(FLAC__BitBuffer *bb, int val, unsigned parameter)
395 {
396         unsigned bits, msbs;
397         uint32 pattern;
398
399         assert(bb != 0);
400         assert(bb->buffer != 0);
401
402         /* init pattern with sign bit */
403         if(val < 0) {
404                 pattern = 1;
405                 val = -val;
406         }
407         else
408                 pattern = 0;
409
410         msbs = val >> parameter;
411         bits = 2 + parameter + msbs;
412
413         if(bits <= 32) {
414                 pattern = (pattern << parameter) | (val & ((1<<parameter)-1));
415                 pattern = (pattern << (msbs+1)) | 1;
416                 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, bits))
417                         return false;
418         }
419         else {
420                 /* write the sign bit */
421                 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, 1))
422                         return false;
423                 /* write the binary LSBs */
424                 if(!FLAC__bitbuffer_write_raw_uint32(bb, val & ((1<<parameter)-1), parameter))
425                         return false;
426                 /* write the unary MSBs */
427                 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
428                         return false;
429                 /* write the end bit */
430                 if(!FLAC__bitbuffer_write_raw_uint32(bb, 1, 1))
431                         return false;
432         }
433         return true;
434 }
435
436 bool FLAC__bitbuffer_write_rice_signed_guarded(FLAC__BitBuffer *bb, int val, unsigned parameter, unsigned max_bits, bool *overflow)
437 {
438         unsigned bits, msbs;
439         uint32 pattern;
440
441         assert(bb != 0);
442         assert(bb->buffer != 0);
443
444         *overflow = false;
445
446         /* init pattern with sign bit */
447         if(val < 0) {
448                 pattern = 1;
449                 val = -val;
450         }
451         else
452                 pattern = 0;
453
454         msbs = val >> parameter;
455         bits = 2 + parameter + msbs;
456
457         if(bits <= 32) {
458                 pattern = (pattern << parameter) | (val & ((1<<parameter)-1));
459                 pattern = (pattern << (msbs+1)) | 1;
460                 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, bits))
461                         return false;
462         }
463         else if(bits > max_bits) {
464                 *overflow = true;
465                 return true;
466         }
467         else {
468                 /* write the sign bit */
469                 if(!FLAC__bitbuffer_write_raw_uint32(bb, pattern, 1))
470                         return false;
471                 /* write the binary LSBs */
472                 if(!FLAC__bitbuffer_write_raw_uint32(bb, val & ((1<<parameter)-1), parameter))
473                         return false;
474                 /* write the unary MSBs */
475                 if(!FLAC__bitbuffer_write_zeroes(bb, msbs))
476                         return false;
477                 /* write the end bit */
478                 if(!FLAC__bitbuffer_write_raw_uint32(bb, 1, 1))
479                         return false;
480         }
481         return true;
482 }
483
484 bool FLAC__bitbuffer_write_utf8_uint32(FLAC__BitBuffer *bb, uint32 val)
485 {
486         bool ok = 1;
487
488         assert(bb != 0);
489         assert(bb->buffer != 0);
490
491         assert(!(val & 0x80000000)); /* this version only handles 31 bits */
492
493         if(val < 0x80) {
494                 return FLAC__bitbuffer_write_raw_uint32(bb, val, 8);
495         }
496         else if(val < 0x800) {
497                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xC0 | (val>>6), 8);
498                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
499         }
500         else if(val < 0x10000) {
501                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xE0 | (val>>12), 8);
502                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
503                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
504         }
505         else if(val < 0x200000) {
506                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF0 | (val>>18), 8);
507                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
508                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
509                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
510         }
511         else if(val < 0x4000000) {
512                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF8 | (val>>24), 8);
513                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>18)&0x3F), 8);
514                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
515                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
516                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
517         }
518         else {
519                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFC | (val>>30), 8);
520                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>24)&0x3F), 8);
521                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>18)&0x3F), 8);
522                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>12)&0x3F), 8);
523                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | ((val>>6)&0x3F), 8);
524                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (val&0x3F), 8);
525         }
526
527         return ok;
528 }
529
530 bool FLAC__bitbuffer_write_utf8_uint64(FLAC__BitBuffer *bb, uint64 val)
531 {
532         bool ok = 1;
533
534         assert(bb != 0);
535         assert(bb->buffer != 0);
536
537         assert(!(val & 0xFFFFFFF000000000)); /* this version only handles 36 bits */
538
539         if(val < 0x80) {
540                 return FLAC__bitbuffer_write_raw_uint32(bb, (uint32)val, 8);
541         }
542         else if(val < 0x800) {
543                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xC0 | (uint32)(val>>6), 8);
544                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
545         }
546         else if(val < 0x10000) {
547                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xE0 | (uint32)(val>>12), 8);
548                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>6)&0x3F), 8);
549                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
550         }
551         else if(val < 0x200000) {
552                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF0 | (uint32)(val>>18), 8);
553                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>12)&0x3F), 8);
554                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>6)&0x3F), 8);
555                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
556         }
557         else if(val < 0x4000000) {
558                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xF8 | (uint32)(val>>24), 8);
559                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>18)&0x3F), 8);
560                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>12)&0x3F), 8);
561                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>6)&0x3F), 8);
562                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
563         }
564         else if(val < 0x80000000) {
565                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFC | (uint32)(val>>30), 8);
566                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>24)&0x3F), 8);
567                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>18)&0x3F), 8);
568                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>12)&0x3F), 8);
569                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>6)&0x3F), 8);
570                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
571         }
572         else {
573                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0xFE, 8);
574                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>30)&0x3F), 8);
575                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>24)&0x3F), 8);
576                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>18)&0x3F), 8);
577                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>12)&0x3F), 8);
578                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)((val>>6)&0x3F), 8);
579                 ok &= FLAC__bitbuffer_write_raw_uint32(bb, 0x80 | (uint32)(val&0x3F), 8);
580         }
581
582         return ok;
583 }
584
585 bool FLAC__bitbuffer_zero_pad_to_byte_boundary(FLAC__BitBuffer *bb)
586 {
587         /* 0-pad to byte boundary */
588         if(bb->bits != 0)
589                 return FLAC__bitbuffer_write_zeroes(bb, 8 - bb->bits);
590         else
591                 return true;
592 }
593
594 bool FLAC__bitbuffer_peek_bit(FLAC__BitBuffer *bb, unsigned *val, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
595 {
596         static byte mask[] = { 128, 64, 32, 16, 8, 4, 2, 1 };
597
598         /* to avoid a drastic speed penalty we don't:
599         assert(bb != 0);
600         assert(bb->buffer != 0);
601         assert(bb->bits == 0);
602         */
603
604 read_bit_:
605         if(bb->total_consumed_bits < bb->total_bits) {
606                 *val = (bb->buffer[bb->consumed_bytes] & mask[bb->consumed_bits])? 1 : 0;
607                 return true;
608         }
609         else {
610                 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
611                         return false;
612                 goto read_bit_;
613         }
614 }
615
616 bool FLAC__bitbuffer_read_bit(FLAC__BitBuffer *bb, unsigned *val, bool (*read_callback)(byte buffer[], unsigned *bytes, void *client_data), void *client_data)
617 {
618         static byte mask[] = { 128, 64, 32, 16, 8, 4, 2, 1 };
619
620         /* to avoid a drastic speed penalty we don't:
621         assert(bb != 0);
622         assert(bb->buffer != 0);
623         assert(bb->bits == 0);
624         */
625
626 read_bit_:
627         if(bb->total_consumed_bits < bb->total_bits) {
628                 *val = (bb->buffer[bb->consumed_bytes] & mask[bb->consumed_bits])? 1 : 0;
629                 bb->consumed_bits++;
630                 if(bb->consumed_bits == 8) {
631                         bb->consumed_bytes++;
632                         bb->consumed_bits = 0;
633                 }
634                 bb->total_consumed_bits++;
635                 return true;
636         }
637         else {
638                 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
639                         return false;
640                 goto read_bit_;
641         }
642 }
643
644 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)
645 {
646         static byte mask[] = { 128, 64, 32, 16, 8, 4, 2, 1 };
647
648         /* to avoid a drastic speed penalty we don't:
649         assert(bb != 0);
650         assert(bb->buffer != 0);
651         assert(bb->bits == 0);
652         */
653
654 read_bit_:
655         if(bb->total_consumed_bits < bb->total_bits) {
656                 *val <<= 1;
657                 *val |= (bb->buffer[bb->consumed_bytes] & mask[bb->consumed_bits])? 1 : 0;
658                 bb->consumed_bits++;
659                 if(bb->consumed_bits == 8) {
660                         bb->consumed_bytes++;
661                         bb->consumed_bits = 0;
662                 }
663                 bb->total_consumed_bits++;
664                 return true;
665         }
666         else {
667                 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
668                         return false;
669                 goto read_bit_;
670         }
671 }
672
673 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)
674 {
675         static byte mask[] = { 128, 64, 32, 16, 8, 4, 2, 1 };
676
677         /* to avoid a drastic speed penalty we don't:
678         assert(bb != 0);
679         assert(bb->buffer != 0);
680         assert(bb->bits == 0);
681         */
682
683 read_bit_:
684         if(bb->total_consumed_bits < bb->total_bits) {
685                 *val <<= 1;
686                 *val |= (bb->buffer[bb->consumed_bytes] & mask[bb->consumed_bits])? 1 : 0;
687                 bb->consumed_bits++;
688                 if(bb->consumed_bits == 8) {
689                         bb->consumed_bytes++;
690                         bb->consumed_bits = 0;
691                 }
692                 bb->total_consumed_bits++;
693                 return true;
694         }
695         else {
696                 if(!bitbuffer_read_from_client_(bb, read_callback, client_data))
697                         return false;
698                 goto read_bit_;
699         }
700 }
701
702 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)
703 {
704         unsigned i;
705
706         assert(bb != 0);
707         assert(bb->buffer != 0);
708
709         assert(bits <= 32);
710
711         *val = 0;
712         for(i = 0; i < bits; i++) {
713                 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, val, read_callback, client_data))
714                         return false;
715         }
716         return true;
717 }
718
719 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)
720 {
721         unsigned i;
722         uint32 x;
723
724         assert(bb != 0);
725         assert(bb->buffer != 0);
726
727         assert(bits <= 32);
728
729         x = 0;
730         for(i = 0; i < bits; i++) {
731                 if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &x, read_callback, client_data))
732                         return false;
733         }
734         /* fix the sign */
735         i = 32 - bits;
736         if(i) {
737                 x <<= i;
738                 *val = (int32)x;
739                 *val >>= i;
740         }
741         else
742                 *val = (int32)x;
743
744         return true;
745 }
746
747 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)
748 {
749         unsigned i;
750
751         assert(bb != 0);
752         assert(bb->buffer != 0);
753
754         assert(bits <= 64);
755
756         *val = 0;
757         for(i = 0; i < bits; i++) {
758                 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, val, read_callback, client_data))
759                         return false;
760         }
761         return true;
762 }
763
764 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)
765 {
766         unsigned i;
767         uint64 x;
768
769         assert(bb != 0);
770         assert(bb->buffer != 0);
771
772         assert(bits <= 64);
773
774         x = 0;
775         for(i = 0; i < bits; i++) {
776                 if(!FLAC__bitbuffer_read_bit_to_uint64(bb, &x, read_callback, client_data))
777                         return false;
778         }
779         /* fix the sign */
780         i = 64 - bits;
781         if(i) {
782                 x <<= i;
783                 *val = (int64)x;
784                 *val >>= i;
785         }
786         else
787                 *val = (int64)x;
788
789         return true;
790 }
791
792 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)
793 {
794         uint32 sign = 0, lsbs, msbs = 0;
795         unsigned bit;
796
797         assert(bb != 0);
798         assert(bb->buffer != 0);
799
800         /* read the sign bit */
801         if(!FLAC__bitbuffer_read_bit_to_uint32(bb, &sign, read_callback, client_data))
802                 return false;
803         /* read the binary LSBs */
804         if(!FLAC__bitbuffer_read_raw_uint32(bb, &lsbs, parameter, read_callback, client_data))
805                 return false;
806         /* read the unary MSBs and end bit */
807         while(1) {
808                 if(!FLAC__bitbuffer_read_bit(bb, &bit, read_callback, client_data))
809                         return false;
810                 if(bit)
811                         break;
812                 else
813                         msbs++;
814         }
815         /* compose the value */
816         *val = (msbs << parameter) | lsbs;
817         if(sign)
818                 *val = -(*val);
819
820         return true;
821 }
822
823 /* on return, if *val == 0xffffffff then the utf-8 sequence was invalid, but the return value will be true */
824 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)
825 {
826         uint32 v = 0;
827         uint32 x;
828         unsigned i;
829
830         if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
831                 return false;
832         if(raw)
833                 raw[(*rawlen)++] = (byte)x;
834         if(!(x & 0x80)) { /* 0xxxxxxx */
835                 v = x;
836                 i = 0;
837         }
838         else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
839                 v = x & 0x1F;
840                 i = 1;
841         }
842         else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
843                 v = x & 0x0F;
844                 i = 2;
845         }
846         else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
847                 v = x & 0x07;
848                 i = 3;
849         }
850         else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
851                 v = x & 0x03;
852                 i = 4;
853         }
854         else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
855                 v = x & 0x01;
856                 i = 5;
857         }
858         else
859                 goto invalid_;
860         for( ; i; i--) {
861                 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
862                         return false;
863                 if(raw)
864                         raw[(*rawlen)++] = (byte)x;
865                 if(!(x & 0x80) || (x & 0x40)) /* 10xxxxxx */
866                         goto invalid_;
867                 v <<= 6;
868                 v |= (x & 0x3F);
869         }
870         *val = v;
871         return true;
872 invalid_:
873         *val = 0xffffffff;
874         return true;
875 }
876
877 /* on return, if *val == 0xffffffffffffffff then the utf-8 sequence was invalid, but the return value will be true */
878 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)
879 {
880         uint64 v = 0;
881         uint32 x;
882         unsigned i;
883
884         if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
885                 return false;
886         if(raw)
887                 raw[(*rawlen)++] = (byte)x;
888         if(!(x & 0x80)) { /* 0xxxxxxx */
889                 v = x;
890                 i = 0;
891         }
892         else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
893                 v = x & 0x1F;
894                 i = 1;
895         }
896         else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
897                 v = x & 0x0F;
898                 i = 2;
899         }
900         else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
901                 v = x & 0x07;
902                 i = 3;
903         }
904         else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
905                 v = x & 0x03;
906                 i = 4;
907         }
908         else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
909                 v = x & 0x01;
910                 i = 5;
911         }
912         else if(x & 0xFE && !(x & 0x01)) { /* 11111110 */
913                 v = 0;
914                 i = 6;
915         }
916         else
917                 goto invalid_;
918         for( ; i; i--) {
919                 if(!FLAC__bitbuffer_read_raw_uint32(bb, &x, 8, read_callback, client_data))
920                         return false;
921                 if(raw)
922                         raw[(*rawlen)++] = (byte)x;
923                 if(!(x & 0x80) || (x & 0x40)) /* 10xxxxxx */
924                         goto invalid_;
925                 v <<= 6;
926                 v |= (x & 0x3F);
927         }
928         *val = v;
929         return true;
930 invalid_:
931         *val = 0xffffffff;
932         return true;
933 }
934
935 void FLAC__bitbuffer_dump(const FLAC__BitBuffer *bb, FILE *out)
936 {
937         unsigned i, j;
938         if(bb == 0) {
939                 fprintf(out, "bitbuffer is NULL\n");
940         }
941         else {
942                 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);
943                 for(i = 0; i < bb->bytes; i++) {
944                         fprintf(out, "%08X: ", i);
945                         for(j = 0; j < 8; j++)
946                                 if(i*8+j < bb->total_consumed_bits)
947                                         fprintf(out, ".");
948                                 else
949                                         fprintf(out, "%01u", bb->buffer[i] & (1 << (8-j-1)) ? 1:0);
950                         fprintf(out, "\n");
951                 }
952                 if(bb->bits > 0) {
953                         fprintf(out, "%08X: ", i);
954                         for(j = 0; j < bb->bits; j++)
955                                 if(i*8+j < bb->total_consumed_bits)
956                                         fprintf(out, ".");
957                                 else
958                                         fprintf(out, "%01u", bb->buffer[i] & (1 << (bb->bits-j-1)) ? 1:0);
959                         fprintf(out, "\n");
960                 }
961         }
962 }