blob: be0957402b4efa7d739aa69ac9b0495c86fdf379 [file] [log] [blame]
Josh Coalson423f8042007-01-28 17:40:26 +00001/* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007 Josh Coalson
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * - Neither the name of the Xiph.org Foundation nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#if HAVE_CONFIG_H
33# include <config.h>
34#endif
35
36#include <stdlib.h> /* for malloc() */
37#include <string.h> /* for memcpy(), memset() */
Josh Coalson67757782007-02-17 01:16:20 +000038#ifdef _MSC_VER
Josh Coalson423f8042007-01-28 17:40:26 +000039#include <winsock.h> /* for ntohl() */
Josh Coalsonf67f2062007-02-13 02:12:13 +000040#elif defined FLAC__SYS_DARWIN
41#include <machine/endian.h> /* for ntohl() */
Josh Coalson423f8042007-01-28 17:40:26 +000042#else
43#include <netinet/in.h> /* for ntohl() */
44#endif
45#include "private/bitmath.h"
46#include "private/bitreader.h"
47#include "private/crc.h"
48#include "FLAC/assert.h"
49
Josh Coalson423f8042007-01-28 17:40:26 +000050/* Things should be fastest when this matches the machine word size */
51/* WATCHOUT: if you change this you must also change the following #defines down to ALIGNED_UNARY_BITS below to match */
52/* WATCHOUT: there are a few places where the code will not work unless brword is >= 32 bits wide */
53/* also, some sections currently only have fast versions for 4 or 8 bytes per word */
54typedef FLAC__uint32 brword;
55#define FLAC__BYTES_PER_WORD 4
56#define FLAC__BITS_PER_WORD 32
57#define FLAC__WORD_ALL_ONES ((FLAC__uint32)0xffffffff)
58#define FLAC__WORD_TOP_BIT_ONE ((FLAC__uint32)0x80000000)
59/* SWAP_BE_WORD_TO_HOST swaps bytes in a brword (which is always big-endian) if necessary to match host byte order */
60#if WORDS_BIGENDIAN
61#define SWAP_BE_WORD_TO_HOST(x) (x)
62#else
Josh Coalsonc85056b2007-02-04 02:57:48 +000063#ifdef _MSC_VER
64#define SWAP_BE_WORD_TO_HOST(x) local_swap32_(x)
65#else
Josh Coalson423f8042007-01-28 17:40:26 +000066#define SWAP_BE_WORD_TO_HOST(x) ntohl(x)
67#endif
Josh Coalsonc85056b2007-02-04 02:57:48 +000068#endif
Josh Coalson423f8042007-01-28 17:40:26 +000069/* counts the # of zero MSBs in a word */
70#define ALIGNED_UNARY_BITS(word) ( \
71 (word) <= 0xffff ? \
72 ( (word) <= 0xff? byte_to_unary_table[word] + 24 : byte_to_unary_table[(word) >> 8] + 16 ) : \
73 ( (word) <= 0xffffff? byte_to_unary_table[word >> 16] + 8 : byte_to_unary_table[(word) >> 24] ) \
74)
75/* this alternate might be slightly faster on some systems/compilers: */
76#define ALIGNED_UNARY_BITS2(word) ( (word) <= 0xff ? byte_to_unary_table[word] + 24 : ((word) <= 0xffff ? byte_to_unary_table[(word) >> 8] + 16 : ((word) <= 0xffffff ? byte_to_unary_table[(word) >> 16] + 8 : byte_to_unary_table[(word) >> 24])) )
77
78
79/*
80 * This should be at least twice as large as the largest number of words
81 * required to represent any 'number' (in any encoding) you are going to
82 * read. With FLAC this is on the order of maybe a few hundred bits.
83 * If the buffer is smaller than that, the decoder won't be able to read
84 * in a whole number that is in a variable length encoding (e.g. Rice).
85 * But to be practical it should be at least 1K bytes.
86 *
87 * Increase this number to decrease the number of read callbacks, at the
88 * expense of using more memory. Or decrease for the reverse effect,
89 * keeping in mind the limit from the first paragraph. The optimal size
90 * also depends on the CPU cache size and other factors; some twiddling
91 * may be necessary to squeeze out the best performance.
92 */
93static const unsigned FLAC__BITREADER_DEFAULT_CAPACITY = 65536u / FLAC__BITS_PER_WORD; /* in words */
94
95static const unsigned char byte_to_unary_table[] = {
96 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
97 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
98 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
99 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
100 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
101 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
102 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
103 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
104 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
105 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
106 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
107 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
108 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
109 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
110 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
111 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
112};
113
114#ifdef min
115#undef min
116#endif
117#define min(x,y) ((x)<(y)?(x):(y))
118#ifdef max
119#undef max
120#endif
121#define max(x,y) ((x)>(y)?(x):(y))
122
123/* adjust for compilers that can't understand using LLU suffix for uint64_t literals */
124#ifdef _MSC_VER
125#define FLAC__U64L(x) x
126#else
127#define FLAC__U64L(x) x##LLU
128#endif
129
130#ifndef FLaC__INLINE
131#define FLaC__INLINE
132#endif
133
Josh Coalsonc63cf412007-03-17 05:21:36 +0000134/* WATCHOUT: assembly routines rely on the order in which these fields are declared */
Josh Coalson423f8042007-01-28 17:40:26 +0000135struct FLAC__BitReader {
136 /* any partially-consumed word at the head will stay right-justified as bits are consumed from the left */
137 /* any incomplete word at the tail will be left-justified, and bytes from the read callback are added on the right */
138 brword *buffer;
139 unsigned capacity; /* in words */
140 unsigned words; /* # of completed words in buffer */
141 unsigned bytes; /* # of bytes in incomplete word at buffer[words] */
Josh Coalsonc63cf412007-03-17 05:21:36 +0000142 unsigned consumed_words; /* #words ... */
143 unsigned consumed_bits; /* ... + (#bits of head word) already consumed from the front of buffer */
Josh Coalson423f8042007-01-28 17:40:26 +0000144 unsigned read_crc16; /* the running frame CRC */
145 unsigned crc16_align; /* the number of bits in the current consumed word that should not be CRC'd */
146 FLAC__BitReaderReadCallback read_callback;
147 void *client_data;
Josh Coalson65454092007-03-13 16:14:36 +0000148 FLAC__CPUInfo cpu_info;
Josh Coalson423f8042007-01-28 17:40:26 +0000149};
150
Josh Coalsonc85056b2007-02-04 02:57:48 +0000151#ifdef _MSC_VER
152/* OPT: an MSVC built-in would be better */
153static _inline FLAC__uint32 local_swap32_(FLAC__uint32 x)
154{
155 x = ((x<<8)&0xFF00FF00) | ((x>>8)&0x00FF00FF);
156 return (x>>16) | (x<<16);
157}
Josh Coalson65454092007-03-13 16:14:36 +0000158static void local_swap32_block_(FLAC__uint32 *start, FLAC__uint32 len)
159{
160 __asm {
161 mov edx, start
162 mov ecx, len
163 test ecx, ecx
164loop1:
165 jz done1
166 mov eax, [edx]
167 bswap eax
168 mov [edx], eax
169 add edx, 4
170 dec ecx
171 jmp short loop1
172done1:
173 }
174}
Josh Coalsonc85056b2007-02-04 02:57:48 +0000175#endif
176
Josh Coalson423f8042007-01-28 17:40:26 +0000177static FLaC__INLINE void crc16_update_word_(FLAC__BitReader *br, brword word)
178{
179 register unsigned crc = br->read_crc16;
180#if FLAC__BYTES_PER_WORD == 4
181 switch(br->crc16_align) {
182 case 0: crc = FLAC__CRC16_UPDATE((unsigned)(word >> 24), crc);
183 case 8: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 16) & 0xff), crc);
184 case 16: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 8) & 0xff), crc);
185 case 24: br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)(word & 0xff), crc);
186 }
187#elif FLAC__BYTES_PER_WORD == 8
188 switch(br->crc16_align) {
189 case 0: crc = FLAC__CRC16_UPDATE((unsigned)(word >> 56), crc);
190 case 8: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 48) & 0xff), crc);
191 case 16: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 40) & 0xff), crc);
192 case 24: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 32) & 0xff), crc);
193 case 32: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 24) & 0xff), crc);
194 case 40: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 16) & 0xff), crc);
195 case 48: crc = FLAC__CRC16_UPDATE((unsigned)((word >> 8) & 0xff), crc);
196 case 56: br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)(word & 0xff), crc);
197 }
198#else
199 for( ; br->crc16_align < FLAC__BITS_PER_WORD; br->crc16_align += 8)
200 crc = FLAC__CRC16_UPDATE((unsigned)((word >> (FLAC__BITS_PER_WORD-8-br->crc16_align)) & 0xff), crc);
201 br->read_crc16 = crc;
202#endif
203 br->crc16_align = 0;
204}
205
Josh Coalsonc63cf412007-03-17 05:21:36 +0000206/* would be static except it needs to be called by asm routines */
207FLAC__bool bitreader_read_from_client_(FLAC__BitReader *br)
Josh Coalson423f8042007-01-28 17:40:26 +0000208{
209 unsigned start, end;
210 size_t bytes;
211 FLAC__byte *target;
212
213 /* first shift the unconsumed buffer data toward the front as much as possible */
214 if(br->consumed_words > 0) {
215 start = br->consumed_words;
216 end = br->words + (br->bytes? 1:0);
217 memmove(br->buffer, br->buffer+start, FLAC__BYTES_PER_WORD * (end - start));
218
219 br->words -= start;
220 br->consumed_words = 0;
221 }
222
223 /*
224 * set the target for reading, taking into account word alignment and endianness
225 */
226 bytes = (br->capacity - br->words) * FLAC__BYTES_PER_WORD - br->bytes;
227 if(bytes == 0)
228 return false; /* no space left, buffer is too small; see note for FLAC__BITREADER_DEFAULT_CAPACITY */
229 target = ((FLAC__byte*)(br->buffer+br->words)) + br->bytes;
230
231 /* before reading, if the existing reader looks like this (say brword is 32 bits wide)
232 * bitstream : 11 22 33 44 55 br->words=1 br->bytes=1 (partial tail word is left-justified)
233 * buffer[BE]: 11 22 33 44 55 ?? ?? ?? (shown layed out as bytes sequentially in memory)
234 * buffer[LE]: 44 33 22 11 ?? ?? ?? 55 (?? being don't-care)
235 * ^^-------target, bytes=3
236 * on LE machines, have to byteswap the odd tail word so nothing is
237 * overwritten:
238 */
239#if WORDS_BIGENDIAN
240#else
241 if(br->bytes)
242 br->buffer[br->words] = SWAP_BE_WORD_TO_HOST(br->buffer[br->words]);
243#endif
244
245 /* now it looks like:
246 * bitstream : 11 22 33 44 55 br->words=1 br->bytes=1
247 * buffer[BE]: 11 22 33 44 55 ?? ?? ??
248 * buffer[LE]: 44 33 22 11 55 ?? ?? ??
249 * ^^-------target, bytes=3
250 */
251
252 /* read in the data; note that the callback may return a smaller number of bytes */
253 if(!br->read_callback(target, &bytes, br->client_data))
254 return false;
255
256 /* after reading bytes 66 77 88 99 AA BB CC DD EE FF from the client:
257 * bitstream : 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF
258 * buffer[BE]: 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF ??
259 * buffer[LE]: 44 33 22 11 55 66 77 88 99 AA BB CC DD EE FF ??
260 * now have to byteswap on LE machines:
261 */
262#if WORDS_BIGENDIAN
263#else
264 end = (br->words*FLAC__BYTES_PER_WORD + br->bytes + bytes + (FLAC__BYTES_PER_WORD-1)) / FLAC__BYTES_PER_WORD;
Josh Coalson65454092007-03-13 16:14:36 +0000265# if defined(_MSC_VER) && (FLAC__BYTES_PER_WORD == 4)
266 if(br->cpu_info.type == FLAC__CPUINFO_TYPE_IA32 && br->cpu_info.data.ia32.bswap) {
267 start = br->words;
268 local_swap32_block_(br->buffer + start, end - start);
269 }
270 else
271# endif
Josh Coalson423f8042007-01-28 17:40:26 +0000272 for(start = br->words; start < end; start++)
273 br->buffer[start] = SWAP_BE_WORD_TO_HOST(br->buffer[start]);
274#endif
275
276 /* now it looks like:
277 * bitstream : 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF
278 * buffer[BE]: 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF ??
279 * buffer[LE]: 44 33 22 11 88 77 66 55 CC BB AA 99 ?? FF EE DD
280 * finally we'll update the reader values:
281 */
282 end = br->words*FLAC__BYTES_PER_WORD + br->bytes + bytes;
283 br->words = end / FLAC__BYTES_PER_WORD;
284 br->bytes = end % FLAC__BYTES_PER_WORD;
285
286 return true;
287}
288
289/***********************************************************************
290 *
291 * Class constructor/destructor
292 *
293 ***********************************************************************/
294
Josh Coalsone3ec2ad2007-01-31 03:53:22 +0000295FLAC__BitReader *FLAC__bitreader_new(void)
Josh Coalson423f8042007-01-28 17:40:26 +0000296{
297 FLAC__BitReader *br = (FLAC__BitReader*)calloc(1, sizeof(FLAC__BitReader));
298
299 /* calloc() implies:
300 memset(br, 0, sizeof(FLAC__BitReader));
301 br->buffer = 0;
302 br->capacity = 0;
303 br->words = br->bytes = 0;
304 br->consumed_words = br->consumed_bits = 0;
305 br->read_callback = 0;
306 br->client_data = 0;
307 */
308 return br;
309}
310
311void FLAC__bitreader_delete(FLAC__BitReader *br)
312{
313 FLAC__ASSERT(0 != br);
314
315 FLAC__bitreader_free(br);
316 free(br);
317}
318
319/***********************************************************************
320 *
321 * Public class methods
322 *
323 ***********************************************************************/
324
Josh Coalson65454092007-03-13 16:14:36 +0000325FLAC__bool FLAC__bitreader_init(FLAC__BitReader *br, FLAC__CPUInfo cpu, FLAC__BitReaderReadCallback rcb, void *cd)
Josh Coalson423f8042007-01-28 17:40:26 +0000326{
327 FLAC__ASSERT(0 != br);
328
329 br->words = br->bytes = 0;
330 br->consumed_words = br->consumed_bits = 0;
331 br->capacity = FLAC__BITREADER_DEFAULT_CAPACITY;
332 br->buffer = (brword*)malloc(sizeof(brword) * br->capacity);
333 if(br->buffer == 0)
334 return false;
335 br->read_callback = rcb;
336 br->client_data = cd;
Josh Coalson65454092007-03-13 16:14:36 +0000337 br->cpu_info = cpu;
Josh Coalson423f8042007-01-28 17:40:26 +0000338
339 return true;
340}
341
342void FLAC__bitreader_free(FLAC__BitReader *br)
343{
344 FLAC__ASSERT(0 != br);
345
346 if(0 != br->buffer)
347 free(br->buffer);
348 br->buffer = 0;
349 br->capacity = 0;
350 br->words = br->bytes = 0;
351 br->consumed_words = br->consumed_bits = 0;
352 br->read_callback = 0;
353 br->client_data = 0;
354}
355
356FLAC__bool FLAC__bitreader_clear(FLAC__BitReader *br)
357{
358 br->words = br->bytes = 0;
359 br->consumed_words = br->consumed_bits = 0;
360 return true;
361}
362
363void FLAC__bitreader_dump(const FLAC__BitReader *br, FILE *out)
364{
365 unsigned i, j;
366 if(br == 0) {
367 fprintf(out, "bitreader is NULL\n");
368 }
369 else {
370 fprintf(out, "bitreader: capacity=%u words=%u bytes=%u consumed: words=%u, bits=%u\n", br->capacity, br->words, br->bytes, br->consumed_words, br->consumed_bits);
371
372 for(i = 0; i < br->words; i++) {
373 fprintf(out, "%08X: ", i);
374 for(j = 0; j < FLAC__BITS_PER_WORD; j++)
375 if(i < br->consumed_words || (i == br->consumed_words && j < br->consumed_bits))
376 fprintf(out, ".");
377 else
378 fprintf(out, "%01u", br->buffer[i] & (1 << (FLAC__BITS_PER_WORD-j-1)) ? 1:0);
379 fprintf(out, "\n");
380 }
381 if(br->bytes > 0) {
382 fprintf(out, "%08X: ", i);
383 for(j = 0; j < br->bytes*8; j++)
384 if(i < br->consumed_words || (i == br->consumed_words && j < br->consumed_bits))
385 fprintf(out, ".");
386 else
387 fprintf(out, "%01u", br->buffer[i] & (1 << (br->bytes*8-j-1)) ? 1:0);
388 fprintf(out, "\n");
389 }
390 }
391}
392
393void FLAC__bitreader_reset_read_crc16(FLAC__BitReader *br, FLAC__uint16 seed)
394{
395 FLAC__ASSERT(0 != br);
396 FLAC__ASSERT(0 != br->buffer);
397 FLAC__ASSERT((br->consumed_bits & 7) == 0);
398
399 br->read_crc16 = (unsigned)seed;
400 br->crc16_align = br->consumed_bits;
401}
402
403FLAC__uint16 FLAC__bitreader_get_read_crc16(FLAC__BitReader *br)
404{
405 FLAC__ASSERT(0 != br);
406 FLAC__ASSERT(0 != br->buffer);
407 FLAC__ASSERT((br->consumed_bits & 7) == 0);
408 FLAC__ASSERT(br->crc16_align <= br->consumed_bits);
409
410 /* CRC any tail bytes in a partially-consumed word */
411 if(br->consumed_bits) {
412 const brword tail = br->buffer[br->consumed_words];
Josh Coalson423f8042007-01-28 17:40:26 +0000413 for( ; br->crc16_align < br->consumed_bits; br->crc16_align += 8)
414 br->read_crc16 = FLAC__CRC16_UPDATE((unsigned)((tail >> (FLAC__BITS_PER_WORD-8-br->crc16_align)) & 0xff), br->read_crc16);
415 }
416 return br->read_crc16;
417}
418
419FLaC__INLINE FLAC__bool FLAC__bitreader_is_consumed_byte_aligned(const FLAC__BitReader *br)
420{
421 return ((br->consumed_bits & 7) == 0);
422}
423
424FLaC__INLINE unsigned FLAC__bitreader_bits_left_for_byte_alignment(const FLAC__BitReader *br)
425{
426 return 8 - (br->consumed_bits & 7);
427}
428
429FLaC__INLINE unsigned FLAC__bitreader_get_input_bits_unconsumed(const FLAC__BitReader *br)
430{
431 return (br->words-br->consumed_words)*FLAC__BITS_PER_WORD + br->bytes*8 - br->consumed_bits;
432}
433
434FLaC__INLINE FLAC__bool FLAC__bitreader_read_raw_uint32(FLAC__BitReader *br, FLAC__uint32 *val, unsigned bits)
435{
436 FLAC__ASSERT(0 != br);
437 FLAC__ASSERT(0 != br->buffer);
438
439 FLAC__ASSERT(bits <= 32);
440 FLAC__ASSERT((br->capacity*FLAC__BITS_PER_WORD) * 2 >= bits);
441 FLAC__ASSERT(br->consumed_words <= br->words);
442
443 /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
444 FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
445
446 if(bits == 0) { /* OPT: investigate if this can ever happen, maybe change to assertion */
447 *val = 0;
448 return true;
449 }
450
451 while((br->words-br->consumed_words)*FLAC__BITS_PER_WORD + br->bytes*8 - br->consumed_bits < bits) {
452 if(!bitreader_read_from_client_(br))
453 return false;
454 }
455 if(br->consumed_words < br->words) { /* if we've not consumed up to a partial tail word... */
456 /* OPT: taking out the consumed_bits==0 "else" case below might make things faster if less code allows the compiler to inline this function */
457 if(br->consumed_bits) {
458 /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
459 const unsigned n = FLAC__BITS_PER_WORD - br->consumed_bits;
460 const brword word = br->buffer[br->consumed_words];
461 if(bits < n) {
462 *val = (word & (FLAC__WORD_ALL_ONES >> br->consumed_bits)) >> (n-bits);
463 br->consumed_bits += bits;
464 return true;
465 }
466 *val = word & (FLAC__WORD_ALL_ONES >> br->consumed_bits);
467 bits -= n;
468 crc16_update_word_(br, word);
469 br->consumed_words++;
470 br->consumed_bits = 0;
471 if(bits) { /* if there are still bits left to read, there have to be less than 32 so they will all be in the next word */
472 *val <<= bits;
473 *val |= (br->buffer[br->consumed_words] >> (FLAC__BITS_PER_WORD-bits));
474 br->consumed_bits = bits;
475 }
476 return true;
477 }
478 else {
479 const brword word = br->buffer[br->consumed_words];
480 if(bits < FLAC__BITS_PER_WORD) {
481 *val = word >> (FLAC__BITS_PER_WORD-bits);
482 br->consumed_bits = bits;
483 return true;
484 }
485 /* at this point 'bits' must be == FLAC__BITS_PER_WORD; because of previous assertions, it can't be larger */
486 *val = word;
487 crc16_update_word_(br, word);
488 br->consumed_words++;
489 return true;
490 }
491 }
492 else {
493 /* in this case we're starting our read at a partial tail word;
494 * the reader has guaranteed that we have at least 'bits' bits
495 * available to read, which makes this case simpler.
496 */
497 /* OPT: taking out the consumed_bits==0 "else" case below might make things faster if less code allows the compiler to inline this function */
498 if(br->consumed_bits) {
499 /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
500 FLAC__ASSERT(br->consumed_bits + bits <= br->bytes*8);
501 *val = (br->buffer[br->consumed_words] & (FLAC__WORD_ALL_ONES >> br->consumed_bits)) >> (FLAC__BITS_PER_WORD-br->consumed_bits-bits);
502 br->consumed_bits += bits;
503 return true;
504 }
505 else {
506 *val = br->buffer[br->consumed_words] >> (FLAC__BITS_PER_WORD-bits);
507 br->consumed_bits += bits;
508 return true;
509 }
510 }
511}
512
513FLAC__bool FLAC__bitreader_read_raw_int32(FLAC__BitReader *br, FLAC__int32 *val, unsigned bits)
514{
515 /* OPT: inline raw uint32 code here, or make into a macro if possible in the .h file */
516 if(!FLAC__bitreader_read_raw_uint32(br, (FLAC__uint32*)val, bits))
517 return false;
518 /* sign-extend: */
519 *val <<= (32-bits);
520 *val >>= (32-bits);
521 return true;
522}
523
524FLAC__bool FLAC__bitreader_read_raw_uint64(FLAC__BitReader *br, FLAC__uint64 *val, unsigned bits)
525{
526 FLAC__uint32 hi, lo;
527
528 if(bits > 32) {
529 if(!FLAC__bitreader_read_raw_uint32(br, &hi, bits-32))
530 return false;
531 if(!FLAC__bitreader_read_raw_uint32(br, &lo, 32))
532 return false;
533 *val = hi;
534 *val <<= 32;
535 *val |= lo;
536 }
537 else {
538 if(!FLAC__bitreader_read_raw_uint32(br, &lo, bits))
539 return false;
540 *val = lo;
541 }
542 return true;
543}
544
545FLaC__INLINE FLAC__bool FLAC__bitreader_read_uint32_little_endian(FLAC__BitReader *br, FLAC__uint32 *val)
546{
547 FLAC__uint32 x8, x32 = 0;
548
549 /* this doesn't need to be that fast as currently it is only used for vorbis comments */
550
551 if(!FLAC__bitreader_read_raw_uint32(br, &x32, 8))
552 return false;
553
554 if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8))
555 return false;
556 x32 |= (x8 << 8);
557
558 if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8))
559 return false;
560 x32 |= (x8 << 16);
561
562 if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8))
563 return false;
564 x32 |= (x8 << 24);
565
566 *val = x32;
567 return true;
568}
569
570FLAC__bool FLAC__bitreader_skip_bits_no_crc(FLAC__BitReader *br, unsigned bits)
571{
572 /*
573 * OPT: a faster implementation is possible but probably not that useful
574 * since this is only called a couple of times in the metadata readers.
575 */
576 FLAC__ASSERT(0 != br);
577 FLAC__ASSERT(0 != br->buffer);
578
579 if(bits > 0) {
580 const unsigned n = br->consumed_bits & 7;
581 unsigned m;
582 FLAC__uint32 x;
583
584 if(n != 0) {
585 m = min(8-n, bits);
586 if(!FLAC__bitreader_read_raw_uint32(br, &x, m))
587 return false;
588 bits -= m;
589 }
590 m = bits / 8;
591 if(m > 0) {
592 if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(br, m))
593 return false;
594 bits %= 8;
595 }
596 if(bits > 0) {
597 if(!FLAC__bitreader_read_raw_uint32(br, &x, bits))
598 return false;
599 }
600 }
601
602 return true;
603}
604
605FLAC__bool FLAC__bitreader_skip_byte_block_aligned_no_crc(FLAC__BitReader *br, unsigned nvals)
606{
607 FLAC__uint32 x;
608
609 FLAC__ASSERT(0 != br);
610 FLAC__ASSERT(0 != br->buffer);
611 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(br));
612
613 /* step 1: skip over partial head word to get word aligned */
614 while(nvals && br->consumed_bits) { /* i.e. run until we read 'nvals' bytes or we hit the end of the head word */
615 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
616 return false;
617 nvals--;
618 }
619 if(0 == nvals)
620 return true;
621 /* step 2: skip whole words in chunks */
622 while(nvals >= FLAC__BYTES_PER_WORD) {
623 if(br->consumed_words < br->words) {
624 br->consumed_words++;
625 nvals -= FLAC__BYTES_PER_WORD;
626 }
627 else if(!bitreader_read_from_client_(br))
628 return false;
629 }
630 /* step 3: skip any remainder from partial tail bytes */
631 while(nvals) {
632 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
633 return false;
634 nvals--;
635 }
636
637 return true;
638}
639
640FLAC__bool FLAC__bitreader_read_byte_block_aligned_no_crc(FLAC__BitReader *br, FLAC__byte *val, unsigned nvals)
641{
642 FLAC__uint32 x;
643
644 FLAC__ASSERT(0 != br);
645 FLAC__ASSERT(0 != br->buffer);
646 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(br));
647
648 /* step 1: read from partial head word to get word aligned */
649 while(nvals && br->consumed_bits) { /* i.e. run until we read 'nvals' bytes or we hit the end of the head word */
650 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
651 return false;
652 *val++ = (FLAC__byte)x;
653 nvals--;
654 }
655 if(0 == nvals)
656 return true;
657 /* step 2: read whole words in chunks */
658 while(nvals >= FLAC__BYTES_PER_WORD) {
659 if(br->consumed_words < br->words) {
660 const brword word = br->buffer[br->consumed_words++];
661#if FLAC__BYTES_PER_WORD == 4
662 val[0] = (FLAC__byte)(word >> 24);
663 val[1] = (FLAC__byte)(word >> 16);
664 val[2] = (FLAC__byte)(word >> 8);
665 val[3] = (FLAC__byte)word;
666#elif FLAC__BYTES_PER_WORD == 8
667 val[0] = (FLAC__byte)(word >> 56);
668 val[1] = (FLAC__byte)(word >> 48);
669 val[2] = (FLAC__byte)(word >> 40);
670 val[3] = (FLAC__byte)(word >> 32);
671 val[4] = (FLAC__byte)(word >> 24);
672 val[5] = (FLAC__byte)(word >> 16);
673 val[6] = (FLAC__byte)(word >> 8);
674 val[7] = (FLAC__byte)word;
675#else
676 for(x = 0; x < FLAC__BYTES_PER_WORD; x++)
677 val[x] = (FLAC__byte)(word >> (8*(FLAC__BYTES_PER_WORD-x-1)));
678#endif
679 val += FLAC__BYTES_PER_WORD;
680 nvals -= FLAC__BYTES_PER_WORD;
681 }
682 else if(!bitreader_read_from_client_(br))
683 return false;
684 }
685 /* step 3: read any remainder from partial tail bytes */
686 while(nvals) {
687 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
688 return false;
689 *val++ = (FLAC__byte)x;
690 nvals--;
691 }
692
693 return true;
694}
695
696FLaC__INLINE FLAC__bool FLAC__bitreader_read_unary_unsigned(FLAC__BitReader *br, unsigned *val)
Josh Coalson65454092007-03-13 16:14:36 +0000697#if 0 /* slow but readable version */
Josh Coalson423f8042007-01-28 17:40:26 +0000698{
699 unsigned bit;
700
701 FLAC__ASSERT(0 != br);
702 FLAC__ASSERT(0 != br->buffer);
703
704 *val = 0;
705 while(1) {
706 if(!FLAC__bitreader_read_bit(br, &bit))
707 return false;
708 if(bit)
709 break;
710 else
711 *val++;
712 }
713 return true;
714}
715#else
716{
717 unsigned i;
718
719 FLAC__ASSERT(0 != br);
720 FLAC__ASSERT(0 != br->buffer);
721
722 *val = 0;
723 while(1) {
724 while(br->consumed_words < br->words) { /* if we've not consumed up to a partial tail word... */
725 brword b = br->buffer[br->consumed_words] << br->consumed_bits;
726 if(b) {
727#if 0 /* too slow, but this is the idea: */
728 for(i = 0; !(b & FLAC__WORD_TOP_BIT_ONE); i++)
729 b <<= 1;
730#else
731 i = ALIGNED_UNARY_BITS(b);
732#endif
733 *val += i;
734 i++;
735 br->consumed_bits += i;
736 if(br->consumed_bits == FLAC__BITS_PER_WORD) {
737 crc16_update_word_(br, br->buffer[br->consumed_words]);
738 br->consumed_words++;
739 br->consumed_bits = 0;
740 }
741 return true;
742 }
743 else {
744 *val += FLAC__BITS_PER_WORD - br->consumed_bits;
745 crc16_update_word_(br, br->buffer[br->consumed_words]);
746 br->consumed_words++;
747 br->consumed_bits = 0;
748 /* didn't find stop bit yet, have to keep going... */
749 }
750 }
751 /* at this point we've eaten up all the whole words; have to try
752 * reading through any tail bytes before calling the read callback.
753 * this is a repeat of the above logic adjusted for the fact we
754 * don't have a whole word. note though if the client is feeding
755 * us data a byte at a time (unlikely), br->consumed_bits may not
756 * be zero.
757 */
758 if(br->bytes) {
759 const unsigned end = br->bytes * 8;
760 brword b = (br->buffer[br->consumed_words] & (FLAC__WORD_ALL_ONES << (FLAC__BITS_PER_WORD-end))) << br->consumed_bits;
761 if(b) {
762#if 0 /* too slow, but this is the idea: */
763 for(i = 0; !(b & FLAC__WORD_TOP_BIT_ONE); i++)
764 b <<= 1;
765#else
766 i = ALIGNED_UNARY_BITS(b);
767#endif
768 *val += i;
769 i++;
770 br->consumed_bits += i;
771 FLAC__ASSERT(br->consumed_bits < FLAC__BITS_PER_WORD);
772 return true;
773 }
774 else {
775 *val += end - br->consumed_bits;
776 br->consumed_bits += end;
777 FLAC__ASSERT(br->consumed_bits < FLAC__BITS_PER_WORD);
778 /* didn't find stop bit yet, have to keep going... */
779 }
780 }
781 if(!bitreader_read_from_client_(br))
782 return false;
783 }
784}
785#endif
786
787FLAC__bool FLAC__bitreader_read_rice_signed(FLAC__BitReader *br, int *val, unsigned parameter)
788{
789 FLAC__uint32 lsbs = 0, msbs = 0;
790 unsigned uval;
791
792 FLAC__ASSERT(0 != br);
793 FLAC__ASSERT(0 != br->buffer);
794 FLAC__ASSERT(parameter <= 31);
795
796 /* read the unary MSBs and end bit */
797 if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
798 return false;
799
800 /* read the binary LSBs */
801 if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, parameter))
802 return false;
803
804 /* compose the value */
805 uval = (msbs << parameter) | lsbs;
806 if(uval & 1)
807 *val = -((int)(uval >> 1)) - 1;
808 else
809 *val = (int)(uval >> 1);
810
811 return true;
812}
813
814/* this is by far the most heavily used reader call. it ain't pretty but it's fast */
815/* a lot of the logic is copied, then adapted, from FLAC__bitreader_read_unary_unsigned() and FLAC__bitreader_read_raw_uint32() */
816FLAC__bool FLAC__bitreader_read_rice_signed_block(FLAC__BitReader *br, int vals[], unsigned nvals, unsigned parameter)
817{
818 unsigned i;
819 unsigned uval = 0;
820 unsigned bits; /* the # of binary LSBs left to read to finish a rice codeword */
821
822 /* try and get br->consumed_words and br->consumed_bits into register;
823 * must remember to flush them back to *br before calling other
824 * bitwriter functions that use them, and before returning */
825 register unsigned cwords;
826 register unsigned cbits;
827
828 FLAC__ASSERT(0 != br);
829 FLAC__ASSERT(0 != br->buffer);
830 /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
831 FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
832 FLAC__ASSERT(parameter < 32);
Josh Coalsonc63cf412007-03-17 05:21:36 +0000833 /* the above two asserts also guarantee that the binary part never straddles more than 2 words, so we don't have to loop to read it */
Josh Coalson423f8042007-01-28 17:40:26 +0000834
835 if(nvals == 0)
836 return true;
837
838 cbits = br->consumed_bits;
839 cwords = br->consumed_words;
840
841 while(1) {
842
843 /* read unary part */
844 while(1) {
845 while(cwords < br->words) { /* if we've not consumed up to a partial tail word... */
846 brword b = br->buffer[cwords] << cbits;
847 if(b) {
848#if 0 /* too slow, but this is the idea: */
849 for(i = 0; !(b & FLAC__WORD_TOP_BIT_ONE); i++)
850 b <<= 1;
851#else
852 i = ALIGNED_UNARY_BITS(b);
853#endif
854 uval += i;
Josh Coalson423f8042007-01-28 17:40:26 +0000855 cbits += i;
Josh Coalsonc63cf412007-03-17 05:21:36 +0000856 bits = parameter;
857 cbits++; /* skip over stop bit */
Josh Coalson423f8042007-01-28 17:40:26 +0000858 if(cbits == FLAC__BITS_PER_WORD) {
859 crc16_update_word_(br, br->buffer[cwords]);
860 cwords++;
861 cbits = 0;
862 }
863 goto break1;
864 }
865 else {
866 uval += FLAC__BITS_PER_WORD - cbits;
867 crc16_update_word_(br, br->buffer[cwords]);
868 cwords++;
869 cbits = 0;
870 /* didn't find stop bit yet, have to keep going... */
871 }
872 }
873 /* at this point we've eaten up all the whole words; have to try
874 * reading through any tail bytes before calling the read callback.
875 * this is a repeat of the above logic adjusted for the fact we
876 * don't have a whole word. note though if the client is feeding
877 * us data a byte at a time (unlikely), br->consumed_bits may not
878 * be zero.
879 */
880 if(br->bytes) {
881 const unsigned end = br->bytes * 8;
Josh Coalsonc63cf412007-03-17 05:21:36 +0000882 brword b = (br->buffer[cwords] & ~(FLAC__WORD_ALL_ONES >> end)) << cbits;
Josh Coalson423f8042007-01-28 17:40:26 +0000883 if(b) {
884#if 0 /* too slow, but this is the idea: */
885 for(i = 0; !(b & FLAC__WORD_TOP_BIT_ONE); i++)
886 b <<= 1;
887#else
888 i = ALIGNED_UNARY_BITS(b);
889#endif
890 uval += i;
Josh Coalson423f8042007-01-28 17:40:26 +0000891 cbits += i;
Josh Coalsonc63cf412007-03-17 05:21:36 +0000892 bits = parameter;
893 cbits++; /* skip over stop bit */
Josh Coalson423f8042007-01-28 17:40:26 +0000894 FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD);
895 goto break1;
896 }
897 else {
898 uval += end - cbits;
899 cbits += end;
900 FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD);
901 /* didn't find stop bit yet, have to keep going... */
902 }
903 }
904 /* flush registers and read; bitreader_read_from_client_() does
905 * not touch br->consumed_bits at all but we still need to set
906 * it in case it fails and we have to return false.
907 */
908 br->consumed_bits = cbits;
909 br->consumed_words = cwords;
910 if(!bitreader_read_from_client_(br))
911 return false;
912 cwords = br->consumed_words;
913 }
914break1:
915 /* read binary part */
916 FLAC__ASSERT(cwords <= br->words);
917
918 if(bits) {
919 while((br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits < bits) {
920 /* flush registers and read; bitreader_read_from_client_() does
921 * not touch br->consumed_bits at all but we still need to set
922 * it in case it fails and we have to return false.
923 */
924 br->consumed_bits = cbits;
925 br->consumed_words = cwords;
926 if(!bitreader_read_from_client_(br))
927 return false;
928 cwords = br->consumed_words;
929 }
930 if(cwords < br->words) { /* if we've not consumed up to a partial tail word... */
931 if(cbits) {
932 /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
933 const unsigned n = FLAC__BITS_PER_WORD - cbits;
934 const brword word = br->buffer[cwords];
935 if(bits < n) {
936 uval <<= bits;
937 uval |= (word & (FLAC__WORD_ALL_ONES >> cbits)) >> (n-bits);
938 cbits += bits;
939 goto break2;
940 }
941 uval <<= n;
942 uval |= word & (FLAC__WORD_ALL_ONES >> cbits);
943 bits -= n;
944 crc16_update_word_(br, word);
945 cwords++;
946 cbits = 0;
947 if(bits) { /* if there are still bits left to read, there have to be less than 32 so they will all be in the next word */
948 uval <<= bits;
949 uval |= (br->buffer[cwords] >> (FLAC__BITS_PER_WORD-bits));
950 cbits = bits;
951 }
952 goto break2;
953 }
954 else {
955 FLAC__ASSERT(bits < FLAC__BITS_PER_WORD);
956 uval <<= bits;
957 uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-bits);
958 cbits = bits;
959 goto break2;
960 }
961 }
962 else {
963 /* in this case we're starting our read at a partial tail word;
964 * the reader has guaranteed that we have at least 'bits' bits
965 * available to read, which makes this case simpler.
966 */
967 uval <<= bits;
968 if(cbits) {
969 /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
970 FLAC__ASSERT(cbits + bits <= br->bytes*8);
971 uval |= (br->buffer[cwords] & (FLAC__WORD_ALL_ONES >> cbits)) >> (FLAC__BITS_PER_WORD-cbits-bits);
972 cbits += bits;
973 goto break2;
974 }
975 else {
976 uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-bits);
977 cbits += bits;
978 goto break2;
979 }
980 }
981 }
982break2:
983 /* compose the value */
984 *vals = (int)(uval >> 1 ^ -(int)(uval & 1));
985
986 /* are we done? */
987 --nvals;
988 if(nvals == 0) {
989 br->consumed_bits = cbits;
990 br->consumed_words = cwords;
991 return true;
992 }
993
994 uval = 0;
995 ++vals;
996
997 }
998}
999
1000#if 0 /* UNUSED */
1001FLAC__bool FLAC__bitreader_read_golomb_signed(FLAC__BitReader *br, int *val, unsigned parameter)
1002{
1003 FLAC__uint32 lsbs = 0, msbs = 0;
1004 unsigned bit, uval, k;
1005
1006 FLAC__ASSERT(0 != br);
1007 FLAC__ASSERT(0 != br->buffer);
1008
1009 k = FLAC__bitmath_ilog2(parameter);
1010
1011 /* read the unary MSBs and end bit */
1012 if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
1013 return false;
1014
1015 /* read the binary LSBs */
1016 if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, k))
1017 return false;
1018
1019 if(parameter == 1u<<k) {
1020 /* compose the value */
1021 uval = (msbs << k) | lsbs;
1022 }
1023 else {
1024 unsigned d = (1 << (k+1)) - parameter;
1025 if(lsbs >= d) {
1026 if(!FLAC__bitreader_read_bit(br, &bit))
1027 return false;
1028 lsbs <<= 1;
1029 lsbs |= bit;
1030 lsbs -= d;
1031 }
1032 /* compose the value */
1033 uval = msbs * parameter + lsbs;
1034 }
1035
1036 /* unfold unsigned to signed */
1037 if(uval & 1)
1038 *val = -((int)(uval >> 1)) - 1;
1039 else
1040 *val = (int)(uval >> 1);
1041
1042 return true;
1043}
1044
1045FLAC__bool FLAC__bitreader_read_golomb_unsigned(FLAC__BitReader *br, unsigned *val, unsigned parameter)
1046{
1047 FLAC__uint32 lsbs, msbs = 0;
1048 unsigned bit, k;
1049
1050 FLAC__ASSERT(0 != br);
1051 FLAC__ASSERT(0 != br->buffer);
1052
1053 k = FLAC__bitmath_ilog2(parameter);
1054
1055 /* read the unary MSBs and end bit */
1056 if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
1057 return false;
1058
1059 /* read the binary LSBs */
1060 if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, k))
1061 return false;
1062
1063 if(parameter == 1u<<k) {
1064 /* compose the value */
1065 *val = (msbs << k) | lsbs;
1066 }
1067 else {
1068 unsigned d = (1 << (k+1)) - parameter;
1069 if(lsbs >= d) {
1070 if(!FLAC__bitreader_read_bit(br, &bit))
1071 return false;
1072 lsbs <<= 1;
1073 lsbs |= bit;
1074 lsbs -= d;
1075 }
1076 /* compose the value */
1077 *val = msbs * parameter + lsbs;
1078 }
1079
1080 return true;
1081}
1082#endif /* UNUSED */
1083
1084/* on return, if *val == 0xffffffff then the utf-8 sequence was invalid, but the return value will be true */
1085FLAC__bool FLAC__bitreader_read_utf8_uint32(FLAC__BitReader *br, FLAC__uint32 *val, FLAC__byte *raw, unsigned *rawlen)
1086{
1087 FLAC__uint32 v = 0;
1088 FLAC__uint32 x;
1089 unsigned i;
1090
1091 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
1092 return false;
1093 if(raw)
1094 raw[(*rawlen)++] = (FLAC__byte)x;
1095 if(!(x & 0x80)) { /* 0xxxxxxx */
1096 v = x;
1097 i = 0;
1098 }
1099 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
1100 v = x & 0x1F;
1101 i = 1;
1102 }
1103 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
1104 v = x & 0x0F;
1105 i = 2;
1106 }
1107 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
1108 v = x & 0x07;
1109 i = 3;
1110 }
1111 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
1112 v = x & 0x03;
1113 i = 4;
1114 }
1115 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
1116 v = x & 0x01;
1117 i = 5;
1118 }
1119 else {
1120 *val = 0xffffffff;
1121 return true;
1122 }
1123 for( ; i; i--) {
1124 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
1125 return false;
1126 if(raw)
1127 raw[(*rawlen)++] = (FLAC__byte)x;
1128 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
1129 *val = 0xffffffff;
1130 return true;
1131 }
1132 v <<= 6;
1133 v |= (x & 0x3F);
1134 }
1135 *val = v;
1136 return true;
1137}
1138
1139/* on return, if *val == 0xffffffffffffffff then the utf-8 sequence was invalid, but the return value will be true */
1140FLAC__bool FLAC__bitreader_read_utf8_uint64(FLAC__BitReader *br, FLAC__uint64 *val, FLAC__byte *raw, unsigned *rawlen)
1141{
1142 FLAC__uint64 v = 0;
1143 FLAC__uint32 x;
1144 unsigned i;
1145
1146 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
1147 return false;
1148 if(raw)
1149 raw[(*rawlen)++] = (FLAC__byte)x;
1150 if(!(x & 0x80)) { /* 0xxxxxxx */
1151 v = x;
1152 i = 0;
1153 }
1154 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
1155 v = x & 0x1F;
1156 i = 1;
1157 }
1158 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
1159 v = x & 0x0F;
1160 i = 2;
1161 }
1162 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
1163 v = x & 0x07;
1164 i = 3;
1165 }
1166 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
1167 v = x & 0x03;
1168 i = 4;
1169 }
1170 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
1171 v = x & 0x01;
1172 i = 5;
1173 }
1174 else if(x & 0xFE && !(x & 0x01)) { /* 11111110 */
1175 v = 0;
1176 i = 6;
1177 }
1178 else {
1179 *val = FLAC__U64L(0xffffffffffffffff);
1180 return true;
1181 }
1182 for( ; i; i--) {
1183 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
1184 return false;
1185 if(raw)
1186 raw[(*rawlen)++] = (FLAC__byte)x;
1187 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
1188 *val = FLAC__U64L(0xffffffffffffffff);
1189 return true;
1190 }
1191 v <<= 6;
1192 v |= (x & 0x3F);
1193 }
1194 *val = v;
1195 return true;
1196}