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