1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000-2009 Josh Coalson
3 * Copyright (C) 2011-2018 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__BitReaderReadCallback read_callback;
115 void *client_data;
116 };
117
crc16_update_word_(FLAC__BitReader * br,brword word)118 static inline void crc16_update_word_(FLAC__BitReader *br, brword word)
119 {
120 register uint32_t crc = br->read_crc16;
121
122 for ( ; br->crc16_align < FLAC__BITS_PER_WORD ; br->crc16_align += 8) {
123 uint32_t shift = FLAC__BITS_PER_WORD - 8 - br->crc16_align ;
124 crc = FLAC__CRC16_UPDATE ((uint32_t) (shift < FLAC__BITS_PER_WORD ? (word >> shift) & 0xff : 0), crc);
125 }
126
127 br->read_crc16 = crc;
128 br->crc16_align = 0;
129 }
130
crc16_update_block_(FLAC__BitReader * br)131 static inline void crc16_update_block_(FLAC__BitReader *br)
132 {
133 if(br->consumed_words > br->crc16_offset && br->crc16_align)
134 crc16_update_word_(br, br->buffer[br->crc16_offset++]);
135
136 /* Prevent OOB read due to wrap-around. */
137 if (br->consumed_words > br->crc16_offset) {
138 #if FLAC__BYTES_PER_WORD == 4
139 br->read_crc16 = FLAC__crc16_update_words32(br->buffer + br->crc16_offset, br->consumed_words - br->crc16_offset, br->read_crc16);
140 #elif FLAC__BYTES_PER_WORD == 8
141 br->read_crc16 = FLAC__crc16_update_words64(br->buffer + br->crc16_offset, br->consumed_words - br->crc16_offset, br->read_crc16);
142 #else
143 unsigned i;
144
145 for (i = br->crc16_offset; i < br->consumed_words; i++)
146 crc16_update_word_(br, br->buffer[i]);
147 #endif
148 }
149
150 br->crc16_offset = 0;
151 }
152
bitreader_read_from_client_(FLAC__BitReader * br)153 static FLAC__bool bitreader_read_from_client_(FLAC__BitReader *br)
154 {
155 uint32_t start, end;
156 size_t bytes;
157 FLAC__byte *target;
158
159 /* first shift the unconsumed buffer data toward the front as much as possible */
160 if(br->consumed_words > 0) {
161 crc16_update_block_(br); /* CRC consumed words */
162
163 start = br->consumed_words;
164 end = br->words + (br->bytes? 1:0);
165 memmove(br->buffer, br->buffer+start, FLAC__BYTES_PER_WORD * (end - start));
166
167 br->words -= start;
168 br->consumed_words = 0;
169 }
170
171 /*
172 * set the target for reading, taking into account word alignment and endianness
173 */
174 bytes = (br->capacity - br->words) * FLAC__BYTES_PER_WORD - br->bytes;
175 if(bytes == 0)
176 return false; /* no space left, buffer is too small; see note for FLAC__BITREADER_DEFAULT_CAPACITY */
177 target = ((FLAC__byte*)(br->buffer+br->words)) + br->bytes;
178
179 /* before reading, if the existing reader looks like this (say brword is 32 bits wide)
180 * bitstream : 11 22 33 44 55 br->words=1 br->bytes=1 (partial tail word is left-justified)
181 * buffer[BE]: 11 22 33 44 55 ?? ?? ?? (shown laid out as bytes sequentially in memory)
182 * buffer[LE]: 44 33 22 11 ?? ?? ?? 55 (?? being don't-care)
183 * ^^-------target, bytes=3
184 * on LE machines, have to byteswap the odd tail word so nothing is
185 * overwritten:
186 */
187 #if WORDS_BIGENDIAN
188 #else
189 if(br->bytes)
190 br->buffer[br->words] = SWAP_BE_WORD_TO_HOST(br->buffer[br->words]);
191 #endif
192
193 /* now it looks like:
194 * bitstream : 11 22 33 44 55 br->words=1 br->bytes=1
195 * buffer[BE]: 11 22 33 44 55 ?? ?? ??
196 * buffer[LE]: 44 33 22 11 55 ?? ?? ??
197 * ^^-------target, bytes=3
198 */
199
200 /* read in the data; note that the callback may return a smaller number of bytes */
201 if(!br->read_callback(target, &bytes, br->client_data))
202 return false;
203
204 /* after reading bytes 66 77 88 99 AA BB CC DD EE FF from the client:
205 * bitstream : 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF
206 * buffer[BE]: 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF ??
207 * buffer[LE]: 44 33 22 11 55 66 77 88 99 AA BB CC DD EE FF ??
208 * now have to byteswap on LE machines:
209 */
210 #if WORDS_BIGENDIAN
211 #else
212 end = (br->words*FLAC__BYTES_PER_WORD + br->bytes + (uint32_t)bytes + (FLAC__BYTES_PER_WORD-1)) / FLAC__BYTES_PER_WORD;
213 for(start = br->words; start < end; start++)
214 br->buffer[start] = SWAP_BE_WORD_TO_HOST(br->buffer[start]);
215 #endif
216
217 /* now it looks like:
218 * bitstream : 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF
219 * buffer[BE]: 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF ??
220 * buffer[LE]: 44 33 22 11 88 77 66 55 CC BB AA 99 ?? FF EE DD
221 * finally we'll update the reader values:
222 */
223 end = br->words*FLAC__BYTES_PER_WORD + br->bytes + (uint32_t)bytes;
224 br->words = end / FLAC__BYTES_PER_WORD;
225 br->bytes = end % FLAC__BYTES_PER_WORD;
226
227 return true;
228 }
229
230 /***********************************************************************
231 *
232 * Class constructor/destructor
233 *
234 ***********************************************************************/
235
FLAC__bitreader_new(void)236 FLAC__BitReader *FLAC__bitreader_new(void)
237 {
238 FLAC__BitReader *br = calloc(1, sizeof(FLAC__BitReader));
239
240 /* calloc() implies:
241 memset(br, 0, sizeof(FLAC__BitReader));
242 br->buffer = 0;
243 br->capacity = 0;
244 br->words = br->bytes = 0;
245 br->consumed_words = br->consumed_bits = 0;
246 br->read_callback = 0;
247 br->client_data = 0;
248 */
249 return br;
250 }
251
FLAC__bitreader_delete(FLAC__BitReader * br)252 void FLAC__bitreader_delete(FLAC__BitReader *br)
253 {
254 FLAC__ASSERT(0 != br);
255
256 FLAC__bitreader_free(br);
257 free(br);
258 }
259
260 /***********************************************************************
261 *
262 * Public class methods
263 *
264 ***********************************************************************/
265
FLAC__bitreader_init(FLAC__BitReader * br,FLAC__BitReaderReadCallback rcb,void * cd)266 FLAC__bool FLAC__bitreader_init(FLAC__BitReader *br, FLAC__BitReaderReadCallback rcb, void *cd)
267 {
268 FLAC__ASSERT(0 != br);
269
270 br->words = br->bytes = 0;
271 br->consumed_words = br->consumed_bits = 0;
272 br->capacity = FLAC__BITREADER_DEFAULT_CAPACITY;
273 br->buffer = malloc(sizeof(brword) * br->capacity);
274 if(br->buffer == 0)
275 return false;
276 br->read_callback = rcb;
277 br->client_data = cd;
278
279 return true;
280 }
281
FLAC__bitreader_free(FLAC__BitReader * br)282 void FLAC__bitreader_free(FLAC__BitReader *br)
283 {
284 FLAC__ASSERT(0 != br);
285
286 if(0 != br->buffer)
287 free(br->buffer);
288 br->buffer = 0;
289 br->capacity = 0;
290 br->words = br->bytes = 0;
291 br->consumed_words = br->consumed_bits = 0;
292 br->read_callback = 0;
293 br->client_data = 0;
294 }
295
FLAC__bitreader_clear(FLAC__BitReader * br)296 FLAC__bool FLAC__bitreader_clear(FLAC__BitReader *br)
297 {
298 br->words = br->bytes = 0;
299 br->consumed_words = br->consumed_bits = 0;
300 return true;
301 }
302
FLAC__bitreader_dump(const FLAC__BitReader * br,FILE * out)303 void FLAC__bitreader_dump(const FLAC__BitReader *br, FILE *out)
304 {
305 uint32_t i, j;
306 if(br == 0) {
307 fprintf(out, "bitreader is NULL\n");
308 }
309 else {
310 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);
311
312 for(i = 0; i < br->words; i++) {
313 fprintf(out, "%08X: ", i);
314 for(j = 0; j < FLAC__BITS_PER_WORD; j++)
315 if(i < br->consumed_words || (i == br->consumed_words && j < br->consumed_bits))
316 fprintf(out, ".");
317 else
318 fprintf(out, "%01d", br->buffer[i] & ((brword)1 << (FLAC__BITS_PER_WORD-j-1)) ? 1:0);
319 fprintf(out, "\n");
320 }
321 if(br->bytes > 0) {
322 fprintf(out, "%08X: ", i);
323 for(j = 0; j < br->bytes*8; j++)
324 if(i < br->consumed_words || (i == br->consumed_words && j < br->consumed_bits))
325 fprintf(out, ".");
326 else
327 fprintf(out, "%01d", br->buffer[i] & ((brword)1 << (br->bytes*8-j-1)) ? 1:0);
328 fprintf(out, "\n");
329 }
330 }
331 }
332
FLAC__bitreader_reset_read_crc16(FLAC__BitReader * br,FLAC__uint16 seed)333 void FLAC__bitreader_reset_read_crc16(FLAC__BitReader *br, FLAC__uint16 seed)
334 {
335 FLAC__ASSERT(0 != br);
336 FLAC__ASSERT(0 != br->buffer);
337 FLAC__ASSERT((br->consumed_bits & 7) == 0);
338
339 br->read_crc16 = (uint32_t)seed;
340 br->crc16_offset = br->consumed_words;
341 br->crc16_align = br->consumed_bits;
342 }
343
FLAC__bitreader_get_read_crc16(FLAC__BitReader * br)344 FLAC__uint16 FLAC__bitreader_get_read_crc16(FLAC__BitReader *br)
345 {
346 FLAC__ASSERT(0 != br);
347 FLAC__ASSERT(0 != br->buffer);
348
349 /* CRC consumed words up to here */
350 crc16_update_block_(br);
351
352 FLAC__ASSERT((br->consumed_bits & 7) == 0);
353 FLAC__ASSERT(br->crc16_align <= br->consumed_bits);
354
355 /* CRC any tail bytes in a partially-consumed word */
356 if(br->consumed_bits) {
357 const brword tail = br->buffer[br->consumed_words];
358 for( ; br->crc16_align < br->consumed_bits; br->crc16_align += 8)
359 br->read_crc16 = FLAC__CRC16_UPDATE((uint32_t)((tail >> (FLAC__BITS_PER_WORD-8-br->crc16_align)) & 0xff), br->read_crc16);
360 }
361 return br->read_crc16;
362 }
363
FLAC__bitreader_is_consumed_byte_aligned(const FLAC__BitReader * br)364 inline FLAC__bool FLAC__bitreader_is_consumed_byte_aligned(const FLAC__BitReader *br)
365 {
366 return ((br->consumed_bits & 7) == 0);
367 }
368
FLAC__bitreader_bits_left_for_byte_alignment(const FLAC__BitReader * br)369 inline uint32_t FLAC__bitreader_bits_left_for_byte_alignment(const FLAC__BitReader *br)
370 {
371 return 8 - (br->consumed_bits & 7);
372 }
373
FLAC__bitreader_get_input_bits_unconsumed(const FLAC__BitReader * br)374 inline uint32_t FLAC__bitreader_get_input_bits_unconsumed(const FLAC__BitReader *br)
375 {
376 return (br->words-br->consumed_words)*FLAC__BITS_PER_WORD + br->bytes*8 - br->consumed_bits;
377 }
378
FLAC__bitreader_read_raw_uint32(FLAC__BitReader * br,FLAC__uint32 * val,uint32_t bits)379 FLAC__bool FLAC__bitreader_read_raw_uint32(FLAC__BitReader *br, FLAC__uint32 *val, uint32_t bits)
380 {
381 FLAC__ASSERT(0 != br);
382 FLAC__ASSERT(0 != br->buffer);
383
384 FLAC__ASSERT(bits <= 32);
385 FLAC__ASSERT((br->capacity*FLAC__BITS_PER_WORD) * 2 >= bits);
386 FLAC__ASSERT(br->consumed_words <= br->words);
387
388 /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
389 FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
390
391 if(bits == 0) { /* OPT: investigate if this can ever happen, maybe change to assertion */
392 *val = 0;
393 return true;
394 }
395
396 while((br->words-br->consumed_words)*FLAC__BITS_PER_WORD + br->bytes*8 - br->consumed_bits < bits) {
397 if(!bitreader_read_from_client_(br))
398 return false;
399 }
400 if(br->consumed_words < br->words) { /* if we've not consumed up to a partial tail word... */
401 /* OPT: taking out the consumed_bits==0 "else" case below might make things faster if less code allows the compiler to inline this function */
402 if(br->consumed_bits) {
403 /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
404 const uint32_t n = FLAC__BITS_PER_WORD - br->consumed_bits;
405 const brword word = br->buffer[br->consumed_words];
406 const brword mask = br->consumed_bits < FLAC__BITS_PER_WORD ? FLAC__WORD_ALL_ONES >> br->consumed_bits : 0;
407 if(bits < n) {
408 uint32_t shift = n - bits;
409 *val = shift < FLAC__BITS_PER_WORD ? (FLAC__uint32)((word & mask) >> shift) : 0; /* The result has <= 32 non-zero bits */
410 br->consumed_bits += bits;
411 return true;
412 }
413 /* (FLAC__BITS_PER_WORD - br->consumed_bits <= bits) ==> (FLAC__WORD_ALL_ONES >> br->consumed_bits) has no more than 'bits' non-zero bits */
414 *val = (FLAC__uint32)(word & mask);
415 bits -= n;
416 br->consumed_words++;
417 br->consumed_bits = 0;
418 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 */
419 uint32_t shift = FLAC__BITS_PER_WORD - bits;
420 *val = bits < 32 ? *val << bits : 0;
421 *val |= shift < FLAC__BITS_PER_WORD ? (FLAC__uint32)(br->buffer[br->consumed_words] >> shift) : 0;
422 br->consumed_bits = bits;
423 }
424 return true;
425 }
426 else { /* br->consumed_bits == 0 */
427 const brword word = br->buffer[br->consumed_words];
428 if(bits < FLAC__BITS_PER_WORD) {
429 *val = (FLAC__uint32)(word >> (FLAC__BITS_PER_WORD-bits));
430 br->consumed_bits = bits;
431 return true;
432 }
433 /* at this point bits == FLAC__BITS_PER_WORD == 32; because of previous assertions, it can't be larger */
434 *val = (FLAC__uint32)word;
435 br->consumed_words++;
436 return true;
437 }
438 }
439 else {
440 /* in this case we're starting our read at a partial tail word;
441 * the reader has guaranteed that we have at least 'bits' bits
442 * available to read, which makes this case simpler.
443 */
444 /* OPT: taking out the consumed_bits==0 "else" case below might make things faster if less code allows the compiler to inline this function */
445 if(br->consumed_bits) {
446 /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
447 FLAC__ASSERT(br->consumed_bits + bits <= br->bytes*8);
448 *val = (FLAC__uint32)((br->buffer[br->consumed_words] & (FLAC__WORD_ALL_ONES >> br->consumed_bits)) >> (FLAC__BITS_PER_WORD-br->consumed_bits-bits));
449 br->consumed_bits += bits;
450 return true;
451 }
452 else {
453 *val = (FLAC__uint32)(br->buffer[br->consumed_words] >> (FLAC__BITS_PER_WORD-bits));
454 br->consumed_bits += bits;
455 return true;
456 }
457 }
458 }
459
FLAC__bitreader_read_raw_int32(FLAC__BitReader * br,FLAC__int32 * val,uint32_t bits)460 FLAC__bool FLAC__bitreader_read_raw_int32(FLAC__BitReader *br, FLAC__int32 *val, uint32_t bits)
461 {
462 FLAC__uint32 uval, mask;
463 /* OPT: inline raw uint32 code here, or make into a macro if possible in the .h file */
464 if (bits < 1 || ! FLAC__bitreader_read_raw_uint32(br, &uval, bits))
465 return false;
466 /* sign-extend *val assuming it is currently bits wide. */
467 /* From: https://graphics.stanford.edu/~seander/bithacks.html#FixedSignExtend */
468 mask = bits >= 33 ? 0 : 1u << (bits - 1);
469 *val = (uval ^ mask) - mask;
470 return true;
471 }
472
FLAC__bitreader_read_raw_uint64(FLAC__BitReader * br,FLAC__uint64 * val,uint32_t bits)473 FLAC__bool FLAC__bitreader_read_raw_uint64(FLAC__BitReader *br, FLAC__uint64 *val, uint32_t bits)
474 {
475 FLAC__uint32 hi, lo;
476
477 if(bits > 32) {
478 if(!FLAC__bitreader_read_raw_uint32(br, &hi, bits-32))
479 return false;
480 if(!FLAC__bitreader_read_raw_uint32(br, &lo, 32))
481 return false;
482 *val = hi;
483 *val <<= 32;
484 *val |= lo;
485 }
486 else {
487 if(!FLAC__bitreader_read_raw_uint32(br, &lo, bits))
488 return false;
489 *val = lo;
490 }
491 return true;
492 }
493
FLAC__bitreader_read_uint32_little_endian(FLAC__BitReader * br,FLAC__uint32 * val)494 inline FLAC__bool FLAC__bitreader_read_uint32_little_endian(FLAC__BitReader *br, FLAC__uint32 *val)
495 {
496 FLAC__uint32 x8, x32 = 0;
497
498 /* this doesn't need to be that fast as currently it is only used for vorbis comments */
499
500 if(!FLAC__bitreader_read_raw_uint32(br, &x32, 8))
501 return false;
502
503 if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8))
504 return false;
505 x32 |= (x8 << 8);
506
507 if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8))
508 return false;
509 x32 |= (x8 << 16);
510
511 if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8))
512 return false;
513 x32 |= (x8 << 24);
514
515 *val = x32;
516 return true;
517 }
518
FLAC__bitreader_skip_bits_no_crc(FLAC__BitReader * br,uint32_t bits)519 FLAC__bool FLAC__bitreader_skip_bits_no_crc(FLAC__BitReader *br, uint32_t bits)
520 {
521 /*
522 * OPT: a faster implementation is possible but probably not that useful
523 * since this is only called a couple of times in the metadata readers.
524 */
525 FLAC__ASSERT(0 != br);
526 FLAC__ASSERT(0 != br->buffer);
527
528 if(bits > 0) {
529 const uint32_t n = br->consumed_bits & 7;
530 uint32_t m;
531 FLAC__uint32 x;
532
533 if(n != 0) {
534 m = flac_min(8-n, bits);
535 if(!FLAC__bitreader_read_raw_uint32(br, &x, m))
536 return false;
537 bits -= m;
538 }
539 m = bits / 8;
540 if(m > 0) {
541 if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(br, m))
542 return false;
543 bits %= 8;
544 }
545 if(bits > 0) {
546 if(!FLAC__bitreader_read_raw_uint32(br, &x, bits))
547 return false;
548 }
549 }
550
551 return true;
552 }
553
FLAC__bitreader_skip_byte_block_aligned_no_crc(FLAC__BitReader * br,uint32_t nvals)554 FLAC__bool FLAC__bitreader_skip_byte_block_aligned_no_crc(FLAC__BitReader *br, uint32_t nvals)
555 {
556 FLAC__uint32 x;
557
558 FLAC__ASSERT(0 != br);
559 FLAC__ASSERT(0 != br->buffer);
560 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(br));
561
562 /* step 1: skip over partial head word to get word aligned */
563 while(nvals && br->consumed_bits) { /* i.e. run until we read 'nvals' bytes or we hit the end of the head word */
564 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
565 return false;
566 nvals--;
567 }
568 if(0 == nvals)
569 return true;
570 /* step 2: skip whole words in chunks */
571 while(nvals >= FLAC__BYTES_PER_WORD) {
572 if(br->consumed_words < br->words) {
573 br->consumed_words++;
574 nvals -= FLAC__BYTES_PER_WORD;
575 }
576 else if(!bitreader_read_from_client_(br))
577 return false;
578 }
579 /* step 3: skip any remainder from partial tail bytes */
580 while(nvals) {
581 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
582 return false;
583 nvals--;
584 }
585
586 return true;
587 }
588
FLAC__bitreader_read_byte_block_aligned_no_crc(FLAC__BitReader * br,FLAC__byte * val,uint32_t nvals)589 FLAC__bool FLAC__bitreader_read_byte_block_aligned_no_crc(FLAC__BitReader *br, FLAC__byte *val, uint32_t nvals)
590 {
591 FLAC__uint32 x;
592
593 FLAC__ASSERT(0 != br);
594 FLAC__ASSERT(0 != br->buffer);
595 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(br));
596
597 /* step 1: read from partial head word to get word aligned */
598 while(nvals && br->consumed_bits) { /* i.e. run until we read 'nvals' bytes or we hit the end of the head word */
599 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
600 return false;
601 *val++ = (FLAC__byte)x;
602 nvals--;
603 }
604 if(0 == nvals)
605 return true;
606 /* step 2: read whole words in chunks */
607 while(nvals >= FLAC__BYTES_PER_WORD) {
608 if(br->consumed_words < br->words) {
609 const brword word = br->buffer[br->consumed_words++];
610 #if FLAC__BYTES_PER_WORD == 4
611 val[0] = (FLAC__byte)(word >> 24);
612 val[1] = (FLAC__byte)(word >> 16);
613 val[2] = (FLAC__byte)(word >> 8);
614 val[3] = (FLAC__byte)word;
615 #elif FLAC__BYTES_PER_WORD == 8
616 val[0] = (FLAC__byte)(word >> 56);
617 val[1] = (FLAC__byte)(word >> 48);
618 val[2] = (FLAC__byte)(word >> 40);
619 val[3] = (FLAC__byte)(word >> 32);
620 val[4] = (FLAC__byte)(word >> 24);
621 val[5] = (FLAC__byte)(word >> 16);
622 val[6] = (FLAC__byte)(word >> 8);
623 val[7] = (FLAC__byte)word;
624 #else
625 for(x = 0; x < FLAC__BYTES_PER_WORD; x++)
626 val[x] = (FLAC__byte)(word >> (8*(FLAC__BYTES_PER_WORD-x-1)));
627 #endif
628 val += FLAC__BYTES_PER_WORD;
629 nvals -= FLAC__BYTES_PER_WORD;
630 }
631 else if(!bitreader_read_from_client_(br))
632 return false;
633 }
634 /* step 3: read any remainder from partial tail bytes */
635 while(nvals) {
636 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
637 return false;
638 *val++ = (FLAC__byte)x;
639 nvals--;
640 }
641
642 return true;
643 }
644
FLAC__bitreader_read_unary_unsigned(FLAC__BitReader * br,uint32_t * val)645 FLAC__bool FLAC__bitreader_read_unary_unsigned(FLAC__BitReader *br, uint32_t *val)
646 #if 0 /* slow but readable version */
647 {
648 uint32_t bit;
649
650 FLAC__ASSERT(0 != br);
651 FLAC__ASSERT(0 != br->buffer);
652
653 *val = 0;
654 while(1) {
655 if(!FLAC__bitreader_read_bit(br, &bit))
656 return false;
657 if(bit)
658 break;
659 else
660 *val++;
661 }
662 return true;
663 }
664 #else
665 {
666 uint32_t i;
667
668 FLAC__ASSERT(0 != br);
669 FLAC__ASSERT(0 != br->buffer);
670
671 *val = 0;
672 while(1) {
673 while(br->consumed_words < br->words) { /* if we've not consumed up to a partial tail word... */
674 brword b = br->consumed_bits < FLAC__BITS_PER_WORD ? br->buffer[br->consumed_words] << br->consumed_bits : 0;
675 if(b) {
676 i = COUNT_ZERO_MSBS(b);
677 *val += i;
678 i++;
679 br->consumed_bits += i;
680 if(br->consumed_bits >= FLAC__BITS_PER_WORD) { /* faster way of testing if(br->consumed_bits == FLAC__BITS_PER_WORD) */
681 br->consumed_words++;
682 br->consumed_bits = 0;
683 }
684 return true;
685 }
686 else {
687 *val += FLAC__BITS_PER_WORD - br->consumed_bits;
688 br->consumed_words++;
689 br->consumed_bits = 0;
690 /* didn't find stop bit yet, have to keep going... */
691 }
692 }
693 /* at this point we've eaten up all the whole words; have to try
694 * reading through any tail bytes before calling the read callback.
695 * this is a repeat of the above logic adjusted for the fact we
696 * don't have a whole word. note though if the client is feeding
697 * us data a byte at a time (unlikely), br->consumed_bits may not
698 * be zero.
699 */
700 if(br->bytes*8 > br->consumed_bits) {
701 const uint32_t end = br->bytes * 8;
702 brword b = (br->buffer[br->consumed_words] & (FLAC__WORD_ALL_ONES << (FLAC__BITS_PER_WORD-end))) << br->consumed_bits;
703 if(b) {
704 i = COUNT_ZERO_MSBS(b);
705 *val += i;
706 i++;
707 br->consumed_bits += i;
708 FLAC__ASSERT(br->consumed_bits < FLAC__BITS_PER_WORD);
709 return true;
710 }
711 else {
712 *val += end - br->consumed_bits;
713 br->consumed_bits = end;
714 FLAC__ASSERT(br->consumed_bits < FLAC__BITS_PER_WORD);
715 /* didn't find stop bit yet, have to keep going... */
716 }
717 }
718 if(!bitreader_read_from_client_(br))
719 return false;
720 }
721 }
722 #endif
723
FLAC__bitreader_read_rice_signed(FLAC__BitReader * br,int * val,uint32_t parameter)724 FLAC__bool FLAC__bitreader_read_rice_signed(FLAC__BitReader *br, int *val, uint32_t parameter)
725 {
726 FLAC__uint32 lsbs = 0, msbs = 0;
727 uint32_t uval;
728
729 FLAC__ASSERT(0 != br);
730 FLAC__ASSERT(0 != br->buffer);
731 FLAC__ASSERT(parameter <= 31);
732
733 /* read the unary MSBs and end bit */
734 if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
735 return false;
736
737 /* read the binary LSBs */
738 if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, parameter))
739 return false;
740
741 /* compose the value */
742 uval = (msbs << parameter) | lsbs;
743 if(uval & 1)
744 *val = -((int)(uval >> 1)) - 1;
745 else
746 *val = (int)(uval >> 1);
747
748 return true;
749 }
750
751 /* 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)752 FLAC__bool FLAC__bitreader_read_rice_signed_block(FLAC__BitReader *br, int vals[], uint32_t nvals, uint32_t parameter)
753 {
754 /* try and get br->consumed_words and br->consumed_bits into register;
755 * must remember to flush them back to *br before calling other
756 * bitreader functions that use them, and before returning */
757 uint32_t cwords, words, lsbs, msbs, x, y;
758 uint32_t ucbits; /* keep track of the number of unconsumed bits in word */
759 brword b;
760 int *val, *end;
761
762 FLAC__ASSERT(0 != br);
763 FLAC__ASSERT(0 != br->buffer);
764 /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
765 FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
766 FLAC__ASSERT(parameter < 32);
767 /* 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 */
768
769 val = vals;
770 end = vals + nvals;
771
772 if(parameter == 0) {
773 while(val < end) {
774 /* read the unary MSBs and end bit */
775 if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
776 return false;
777
778 *val++ = (int)(msbs >> 1) ^ -(int)(msbs & 1);
779 }
780
781 return true;
782 }
783
784 FLAC__ASSERT(parameter > 0);
785
786 cwords = br->consumed_words;
787 words = br->words;
788
789 /* if we've not consumed up to a partial tail word... */
790 if(cwords >= words) {
791 x = 0;
792 goto process_tail;
793 }
794
795 ucbits = FLAC__BITS_PER_WORD - br->consumed_bits;
796 b = br->buffer[cwords] << br->consumed_bits; /* keep unconsumed bits aligned to left */
797
798 while(val < end) {
799 /* read the unary MSBs and end bit */
800 x = y = COUNT_ZERO_MSBS2(b);
801 if(x == FLAC__BITS_PER_WORD) {
802 x = ucbits;
803 do {
804 /* didn't find stop bit yet, have to keep going... */
805 cwords++;
806 if (cwords >= words)
807 goto incomplete_msbs;
808 b = br->buffer[cwords];
809 y = COUNT_ZERO_MSBS2(b);
810 x += y;
811 } while(y == FLAC__BITS_PER_WORD);
812 }
813 b <<= y;
814 b <<= 1; /* account for stop bit */
815 ucbits = (ucbits - x - 1) % FLAC__BITS_PER_WORD;
816 msbs = x;
817
818 /* read the binary LSBs */
819 x = (FLAC__uint32)(b >> (FLAC__BITS_PER_WORD - parameter)); /* parameter < 32, so we can cast to 32-bit uint32_t */
820 if(parameter <= ucbits) {
821 ucbits -= parameter;
822 b <<= parameter;
823 } else {
824 /* there are still bits left to read, they will all be in the next word */
825 cwords++;
826 if (cwords >= words)
827 goto incomplete_lsbs;
828 b = br->buffer[cwords];
829 ucbits += FLAC__BITS_PER_WORD - parameter;
830 x |= (FLAC__uint32)(b >> ucbits);
831 b <<= FLAC__BITS_PER_WORD - ucbits;
832 }
833 lsbs = x;
834
835 /* compose the value */
836 x = (msbs << parameter) | lsbs;
837 *val++ = (int)(x >> 1) ^ -(int)(x & 1);
838
839 continue;
840
841 /* at this point we've eaten up all the whole words */
842 process_tail:
843 do {
844 if(0) {
845 incomplete_msbs:
846 br->consumed_bits = 0;
847 br->consumed_words = cwords;
848 }
849
850 /* read the unary MSBs and end bit */
851 if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
852 return false;
853 msbs += x;
854 x = ucbits = 0;
855
856 if(0) {
857 incomplete_lsbs:
858 br->consumed_bits = 0;
859 br->consumed_words = cwords;
860 }
861
862 /* read the binary LSBs */
863 if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, parameter - ucbits))
864 return false;
865 lsbs = x | lsbs;
866
867 /* compose the value */
868 x = (msbs << parameter) | lsbs;
869 *val++ = (int)(x >> 1) ^ -(int)(x & 1);
870 x = 0;
871
872 cwords = br->consumed_words;
873 words = br->words;
874 ucbits = FLAC__BITS_PER_WORD - br->consumed_bits;
875 b = cwords < br->capacity ? br->buffer[cwords] << br->consumed_bits : 0;
876 } while(cwords >= words && val < end);
877 }
878
879 if(ucbits == 0 && cwords < words) {
880 /* don't leave the head word with no unconsumed bits */
881 cwords++;
882 ucbits = FLAC__BITS_PER_WORD;
883 }
884
885 br->consumed_bits = FLAC__BITS_PER_WORD - ucbits;
886 br->consumed_words = cwords;
887
888 return true;
889 }
890
891 #if 0 /* UNUSED */
892 FLAC__bool FLAC__bitreader_read_golomb_signed(FLAC__BitReader *br, int *val, uint32_t parameter)
893 {
894 FLAC__uint32 lsbs = 0, msbs = 0;
895 uint32_t bit, uval, k;
896
897 FLAC__ASSERT(0 != br);
898 FLAC__ASSERT(0 != br->buffer);
899
900 k = FLAC__bitmath_ilog2(parameter);
901
902 /* read the unary MSBs and end bit */
903 if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
904 return false;
905
906 /* read the binary LSBs */
907 if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, k))
908 return false;
909
910 if(parameter == 1u<<k) {
911 /* compose the value */
912 uval = (msbs << k) | lsbs;
913 }
914 else {
915 uint32_t d = (1 << (k+1)) - parameter;
916 if(lsbs >= d) {
917 if(!FLAC__bitreader_read_bit(br, &bit))
918 return false;
919 lsbs <<= 1;
920 lsbs |= bit;
921 lsbs -= d;
922 }
923 /* compose the value */
924 uval = msbs * parameter + lsbs;
925 }
926
927 /* unfold uint32_t to signed */
928 if(uval & 1)
929 *val = -((int)(uval >> 1)) - 1;
930 else
931 *val = (int)(uval >> 1);
932
933 return true;
934 }
935
936 FLAC__bool FLAC__bitreader_read_golomb_unsigned(FLAC__BitReader *br, uint32_t *val, uint32_t parameter)
937 {
938 FLAC__uint32 lsbs, msbs = 0;
939 uint32_t bit, k;
940
941 FLAC__ASSERT(0 != br);
942 FLAC__ASSERT(0 != br->buffer);
943
944 k = FLAC__bitmath_ilog2(parameter);
945
946 /* read the unary MSBs and end bit */
947 if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
948 return false;
949
950 /* read the binary LSBs */
951 if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, k))
952 return false;
953
954 if(parameter == 1u<<k) {
955 /* compose the value */
956 *val = (msbs << k) | lsbs;
957 }
958 else {
959 uint32_t d = (1 << (k+1)) - parameter;
960 if(lsbs >= d) {
961 if(!FLAC__bitreader_read_bit(br, &bit))
962 return false;
963 lsbs <<= 1;
964 lsbs |= bit;
965 lsbs -= d;
966 }
967 /* compose the value */
968 *val = msbs * parameter + lsbs;
969 }
970
971 return true;
972 }
973 #endif /* UNUSED */
974
975 /* 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)976 FLAC__bool FLAC__bitreader_read_utf8_uint32(FLAC__BitReader *br, FLAC__uint32 *val, FLAC__byte *raw, uint32_t *rawlen)
977 {
978 FLAC__uint32 v = 0;
979 FLAC__uint32 x;
980 uint32_t i;
981
982 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
983 return false;
984 if(raw)
985 raw[(*rawlen)++] = (FLAC__byte)x;
986 if(!(x & 0x80)) { /* 0xxxxxxx */
987 v = x;
988 i = 0;
989 }
990 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
991 v = x & 0x1F;
992 i = 1;
993 }
994 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
995 v = x & 0x0F;
996 i = 2;
997 }
998 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
999 v = x & 0x07;
1000 i = 3;
1001 }
1002 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
1003 v = x & 0x03;
1004 i = 4;
1005 }
1006 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
1007 v = x & 0x01;
1008 i = 5;
1009 }
1010 else {
1011 *val = 0xffffffff;
1012 return true;
1013 }
1014 for( ; i; i--) {
1015 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
1016 return false;
1017 if(raw)
1018 raw[(*rawlen)++] = (FLAC__byte)x;
1019 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
1020 *val = 0xffffffff;
1021 return true;
1022 }
1023 v <<= 6;
1024 v |= (x & 0x3F);
1025 }
1026 *val = v;
1027 return true;
1028 }
1029
1030 /* 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)1031 FLAC__bool FLAC__bitreader_read_utf8_uint64(FLAC__BitReader *br, FLAC__uint64 *val, FLAC__byte *raw, uint32_t *rawlen)
1032 {
1033 FLAC__uint64 v = 0;
1034 FLAC__uint32 x;
1035 uint32_t i;
1036
1037 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
1038 return false;
1039 if(raw)
1040 raw[(*rawlen)++] = (FLAC__byte)x;
1041 if(!(x & 0x80)) { /* 0xxxxxxx */
1042 v = x;
1043 i = 0;
1044 }
1045 else if(x & 0xC0 && !(x & 0x20)) { /* 110xxxxx */
1046 v = x & 0x1F;
1047 i = 1;
1048 }
1049 else if(x & 0xE0 && !(x & 0x10)) { /* 1110xxxx */
1050 v = x & 0x0F;
1051 i = 2;
1052 }
1053 else if(x & 0xF0 && !(x & 0x08)) { /* 11110xxx */
1054 v = x & 0x07;
1055 i = 3;
1056 }
1057 else if(x & 0xF8 && !(x & 0x04)) { /* 111110xx */
1058 v = x & 0x03;
1059 i = 4;
1060 }
1061 else if(x & 0xFC && !(x & 0x02)) { /* 1111110x */
1062 v = x & 0x01;
1063 i = 5;
1064 }
1065 else if(x & 0xFE && !(x & 0x01)) { /* 11111110 */
1066 v = 0;
1067 i = 6;
1068 }
1069 else {
1070 *val = FLAC__U64L(0xffffffffffffffff);
1071 return true;
1072 }
1073 for( ; i; i--) {
1074 if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
1075 return false;
1076 if(raw)
1077 raw[(*rawlen)++] = (FLAC__byte)x;
1078 if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
1079 *val = FLAC__U64L(0xffffffffffffffff);
1080 return true;
1081 }
1082 v <<= 6;
1083 v |= (x & 0x3F);
1084 }
1085 *val = v;
1086 return true;
1087 }
1088
1089 /* These functions are declared inline in this file but are also callable as
1090 * externs from elsewhere.
1091 * According to the C99 spec, section 6.7.4, simply providing a function
1092 * prototype in a header file without 'inline' and making the function inline
1093 * in this file should be sufficient.
1094 * Unfortunately, the Microsoft VS compiler doesn't pick them up externally. To
1095 * fix that we add extern declarations here.
1096 */
1097 extern FLAC__bool FLAC__bitreader_is_consumed_byte_aligned(const FLAC__BitReader *br);
1098 extern uint32_t FLAC__bitreader_bits_left_for_byte_alignment(const FLAC__BitReader *br);
1099 extern uint32_t FLAC__bitreader_get_input_bits_unconsumed(const FLAC__BitReader *br);
1100 extern FLAC__bool FLAC__bitreader_read_uint32_little_endian(FLAC__BitReader *br, FLAC__uint32 *val);
1101