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