1 /* libFLAC - Free Lossless Audio Codec library
2  * Copyright (C) 2000-2009  Josh Coalson
3  * Copyright (C) 2011-2013  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/bitwriter.h"
40 #include "private/crc.h"
41 #include "private/macros.h"
42 #include "FLAC/assert.h"
43 #include "share/alloc.h"
44 #include "share/compat.h"
45 #include "share/endswap.h"
46 
47 #include <retro_inline.h>
48 #include <retro_miscellaneous.h>
49 
50 /* Things should be fastest when this matches the machine word size */
51 /* WATCHOUT: if you change this you must also change the following #defines down to SWAP_BE_WORD_TO_HOST below to match */
52 /* WATCHOUT: there are a few places where the code will not work unless uint32_t is >= 32 bits wide */
53 #define FLAC__BYTES_PER_WORD 4
54 #define FLAC__BITS_PER_WORD 32
55 #define FLAC__WORD_ALL_ONES ((FLAC__uint32)0xffffffff)
56 /* SWAP_BE_WORD_TO_HOST swaps bytes in a uint32_t (which is always big-endian) if necessary to match host byte order */
57 #ifdef MSB_FIRST
58 #define SWAP_BE_WORD_TO_HOST(x) (x)
59 #else
60 #define SWAP_BE_WORD_TO_HOST(x) ENDSWAP_32(x)
61 #endif
62 
63 /*
64  * The default capacity here doesn't matter too much.  The buffer always grows
65  * to hold whatever is written to it.  Usually the encoder will stop adding at
66  * a frame or metadata block, then write that out and clear the buffer for the
67  * next one.
68  */
69 static const unsigned FLAC__BITWRITER_DEFAULT_CAPACITY = 32768u / sizeof(uint32_t); /* size in words */
70 /* When growing, increment 4K at a time */
71 static const unsigned FLAC__BITWRITER_DEFAULT_INCREMENT = 4096u / sizeof(uint32_t); /* size in words */
72 
73 #define FLAC__WORDS_TO_BITS(words) ((words) * FLAC__BITS_PER_WORD)
74 #define FLAC__TOTAL_BITS(bw) (FLAC__WORDS_TO_BITS((bw)->words) + (bw)->bits)
75 
76 struct FLAC__BitWriter {
77 	uint32_t *buffer;
78 	uint32_t accum; /* accumulator; bits are right-justified; when full, accum is appended to buffer */
79 	unsigned capacity; /* capacity of buffer in words */
80 	unsigned words; /* # of complete words in buffer */
81 	unsigned bits; /* # of used bits in accum */
82 };
83 
84 /* * WATCHOUT: The current implementation only grows the buffer. */
85 #ifndef __SUNPRO_C
86 static
87 #endif
bitwriter_grow_(FLAC__BitWriter * bw,unsigned bits_to_add)88 FLAC__bool bitwriter_grow_(FLAC__BitWriter *bw, unsigned bits_to_add)
89 {
90 	unsigned new_capacity;
91 	uint32_t *new_buffer;
92 
93 	FLAC__ASSERT(0 != bw);
94 	FLAC__ASSERT(0 != bw->buffer);
95 
96 	/* calculate total words needed to store 'bits_to_add' additional bits */
97 	new_capacity = bw->words + ((bw->bits + bits_to_add + FLAC__BITS_PER_WORD - 1) / FLAC__BITS_PER_WORD);
98 
99 	/* it's possible (due to pessimism in the growth estimation that
100 	 * leads to this call) that we don't actually need to grow
101 	 */
102 	if(bw->capacity >= new_capacity)
103 		return true;
104 
105 	/* round up capacity increase to the nearest FLAC__BITWRITER_DEFAULT_INCREMENT */
106 	if((new_capacity - bw->capacity) % FLAC__BITWRITER_DEFAULT_INCREMENT)
107 		new_capacity += FLAC__BITWRITER_DEFAULT_INCREMENT - ((new_capacity - bw->capacity) % FLAC__BITWRITER_DEFAULT_INCREMENT);
108 	/* make sure we got everything right */
109 	FLAC__ASSERT(0 == (new_capacity - bw->capacity) % FLAC__BITWRITER_DEFAULT_INCREMENT);
110 	FLAC__ASSERT(new_capacity > bw->capacity);
111 	FLAC__ASSERT(new_capacity >= bw->words + ((bw->bits + bits_to_add + FLAC__BITS_PER_WORD - 1) / FLAC__BITS_PER_WORD));
112 
113 	new_buffer = (uint32_t*)safe_realloc_mul_2op_(bw->buffer, sizeof(uint32_t), /*times*/new_capacity);
114 	if(new_buffer == 0)
115 		return false;
116 	bw->buffer = new_buffer;
117 	bw->capacity = new_capacity;
118 	return true;
119 }
120 
121 
122 /***********************************************************************
123  *
124  * Class constructor/destructor
125  *
126  ***********************************************************************/
127 
FLAC__bitwriter_new(void)128 FLAC__BitWriter *FLAC__bitwriter_new(void)
129 {
130 	FLAC__BitWriter *bw = (FLAC__BitWriter*)calloc(1, sizeof(FLAC__BitWriter));
131 	/* note that calloc() sets all members to 0 for us */
132 	return bw;
133 }
134 
FLAC__bitwriter_delete(FLAC__BitWriter * bw)135 void FLAC__bitwriter_delete(FLAC__BitWriter *bw)
136 {
137 	FLAC__ASSERT(0 != bw);
138 
139 	FLAC__bitwriter_free(bw);
140 	free(bw);
141 }
142 
143 /***********************************************************************
144  *
145  * Public class methods
146  *
147  ***********************************************************************/
148 
FLAC__bitwriter_init(FLAC__BitWriter * bw)149 FLAC__bool FLAC__bitwriter_init(FLAC__BitWriter *bw)
150 {
151 	FLAC__ASSERT(0 != bw);
152 
153 	bw->words = bw->bits = 0;
154 	bw->capacity = FLAC__BITWRITER_DEFAULT_CAPACITY;
155 	bw->buffer = (uint32_t*)malloc(sizeof(uint32_t) * bw->capacity);
156 	if(bw->buffer == 0)
157 		return false;
158 
159 	return true;
160 }
161 
FLAC__bitwriter_free(FLAC__BitWriter * bw)162 void FLAC__bitwriter_free(FLAC__BitWriter *bw)
163 {
164 	FLAC__ASSERT(0 != bw);
165 
166 	if(0 != bw->buffer)
167 		free(bw->buffer);
168 	bw->buffer = 0;
169 	bw->capacity = 0;
170 	bw->words = bw->bits = 0;
171 }
172 
FLAC__bitwriter_clear(FLAC__BitWriter * bw)173 void FLAC__bitwriter_clear(FLAC__BitWriter *bw)
174 {
175 	bw->words = bw->bits = 0;
176 }
177 
FLAC__bitwriter_dump(const FLAC__BitWriter * bw,FILE * out)178 void FLAC__bitwriter_dump(const FLAC__BitWriter *bw, FILE *out)
179 {
180 	unsigned i, j;
181 	if(bw == 0) {
182 		fprintf(out, "bitwriter is NULL\n");
183 	}
184 	else {
185 		fprintf(out, "bitwriter: capacity=%u words=%u bits=%u total_bits=%u\n", bw->capacity, bw->words, bw->bits, FLAC__TOTAL_BITS(bw));
186 
187 		for(i = 0; i < bw->words; i++) {
188 			fprintf(out, "%08X: ", i);
189 			for(j = 0; j < FLAC__BITS_PER_WORD; j++)
190 				fprintf(out, "%01u", bw->buffer[i] & (1 << (FLAC__BITS_PER_WORD-j-1)) ? 1:0);
191 			fprintf(out, "\n");
192 		}
193 		if(bw->bits > 0) {
194 			fprintf(out, "%08X: ", i);
195 			for(j = 0; j < bw->bits; j++)
196 				fprintf(out, "%01u", bw->accum & (1 << (bw->bits-j-1)) ? 1:0);
197 			fprintf(out, "\n");
198 		}
199 	}
200 }
201 
FLAC__bitwriter_get_write_crc16(FLAC__BitWriter * bw,FLAC__uint16 * crc)202 FLAC__bool FLAC__bitwriter_get_write_crc16(FLAC__BitWriter *bw, FLAC__uint16 *crc)
203 {
204 	const FLAC__byte *buffer;
205 	size_t bytes;
206 
207 	FLAC__ASSERT((bw->bits & 7) == 0); /* assert that we're byte-aligned */
208 
209 	if(!FLAC__bitwriter_get_buffer(bw, &buffer, &bytes))
210 		return false;
211 
212 	*crc = (FLAC__uint16)FLAC__crc16(buffer, bytes);
213 	FLAC__bitwriter_release_buffer(bw);
214 	return true;
215 }
216 
FLAC__bitwriter_get_write_crc8(FLAC__BitWriter * bw,FLAC__byte * crc)217 FLAC__bool FLAC__bitwriter_get_write_crc8(FLAC__BitWriter *bw, FLAC__byte *crc)
218 {
219 	const FLAC__byte *buffer;
220 	size_t bytes;
221 
222 	FLAC__ASSERT((bw->bits & 7) == 0); /* assert that we're byte-aligned */
223 
224 	if(!FLAC__bitwriter_get_buffer(bw, &buffer, &bytes))
225 		return false;
226 
227 	*crc = FLAC__crc8(buffer, bytes);
228 	FLAC__bitwriter_release_buffer(bw);
229 	return true;
230 }
231 
FLAC__bitwriter_is_byte_aligned(const FLAC__BitWriter * bw)232 FLAC__bool FLAC__bitwriter_is_byte_aligned(const FLAC__BitWriter *bw)
233 {
234 	return ((bw->bits & 7) == 0);
235 }
236 
FLAC__bitwriter_get_input_bits_unconsumed(const FLAC__BitWriter * bw)237 unsigned FLAC__bitwriter_get_input_bits_unconsumed(const FLAC__BitWriter *bw)
238 {
239 	return FLAC__TOTAL_BITS(bw);
240 }
241 
FLAC__bitwriter_get_buffer(FLAC__BitWriter * bw,const FLAC__byte ** buffer,size_t * bytes)242 FLAC__bool FLAC__bitwriter_get_buffer(FLAC__BitWriter *bw, const FLAC__byte **buffer, size_t *bytes)
243 {
244 	FLAC__ASSERT((bw->bits & 7) == 0);
245 	/* double protection */
246 	if(bw->bits & 7)
247 		return false;
248 	/* if we have bits in the accumulator we have to flush those to the buffer first */
249 	if(bw->bits) {
250 		FLAC__ASSERT(bw->words <= bw->capacity);
251 		if(bw->words == bw->capacity && !bitwriter_grow_(bw, FLAC__BITS_PER_WORD))
252 			return false;
253 		/* append bits as complete word to buffer, but don't change bw->accum or bw->bits */
254 		bw->buffer[bw->words] = SWAP_BE_WORD_TO_HOST(bw->accum << (FLAC__BITS_PER_WORD-bw->bits));
255 	}
256 	/* now we can just return what we have */
257 	*buffer = (FLAC__byte*)bw->buffer;
258 	*bytes = (FLAC__BYTES_PER_WORD * bw->words) + (bw->bits >> 3);
259 	return true;
260 }
261 
FLAC__bitwriter_release_buffer(FLAC__BitWriter * bw)262 void FLAC__bitwriter_release_buffer(FLAC__BitWriter *bw)
263 {
264 	/* nothing to do.  in the future, strict checking of a 'writer-is-in-
265 	 * get-mode' flag could be added everywhere and then cleared here
266 	 */
267 	(void)bw;
268 }
269 
FLAC__bitwriter_write_zeroes(FLAC__BitWriter * bw,unsigned bits)270 FLAC__bool FLAC__bitwriter_write_zeroes(FLAC__BitWriter *bw, unsigned bits)
271 {
272 	unsigned n;
273 
274 	FLAC__ASSERT(0 != bw);
275 	FLAC__ASSERT(0 != bw->buffer);
276 
277 	if(bits == 0)
278 		return true;
279 	/* slightly pessimistic size check but faster than "<= bw->words + (bw->bits+bits+FLAC__BITS_PER_WORD-1)/FLAC__BITS_PER_WORD" */
280 	if(bw->capacity <= bw->words + bits && !bitwriter_grow_(bw, bits))
281 		return false;
282 	/* first part gets to word alignment */
283 	if(bw->bits) {
284 		n = MIN(FLAC__BITS_PER_WORD - bw->bits, bits);
285 		bw->accum <<= n;
286 		bits -= n;
287 		bw->bits += n;
288 		if(bw->bits == FLAC__BITS_PER_WORD) {
289 			bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
290 			bw->bits = 0;
291 		}
292 		else
293 			return true;
294 	}
295 	/* do whole words */
296 	while(bits >= FLAC__BITS_PER_WORD) {
297 		bw->buffer[bw->words++] = 0;
298 		bits -= FLAC__BITS_PER_WORD;
299 	}
300 	/* do any leftovers */
301 	if(bits > 0) {
302 		bw->accum = 0;
303 		bw->bits = bits;
304 	}
305 	return true;
306 }
307 
FLAC__bitwriter_write_raw_uint32(FLAC__BitWriter * bw,FLAC__uint32 val,unsigned bits)308 FLAC__bool FLAC__bitwriter_write_raw_uint32(FLAC__BitWriter *bw, FLAC__uint32 val, unsigned bits)
309 {
310 	register unsigned left;
311 
312 	/* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
313 	FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
314 
315 	FLAC__ASSERT(0 != bw);
316 	FLAC__ASSERT(0 != bw->buffer);
317 
318 	FLAC__ASSERT(bits <= 32);
319 	if(bits == 0)
320 		return true;
321 
322 	/* slightly pessimistic size check but faster than "<= bw->words + (bw->bits+bits+FLAC__BITS_PER_WORD-1)/FLAC__BITS_PER_WORD" */
323 	if(bw->capacity <= bw->words + bits && !bitwriter_grow_(bw, bits))
324 		return false;
325 
326 	left = FLAC__BITS_PER_WORD - bw->bits;
327 	if(bits < left) {
328 		bw->accum <<= bits;
329 		bw->accum |= val;
330 		bw->bits += bits;
331 	}
332 	else if(bw->bits) { /* WATCHOUT: if bw->bits == 0, left==FLAC__BITS_PER_WORD and bw->accum<<=left is a NOP instead of setting to 0 */
333 		bw->accum <<= left;
334 		bw->accum |= val >> (bw->bits = bits - left);
335 		bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
336 		bw->accum = val;
337 	}
338 	else {
339 		bw->accum = val;
340 		bw->bits = 0;
341 		bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(val);
342 	}
343 
344 	return true;
345 }
346 
FLAC__bitwriter_write_raw_int32(FLAC__BitWriter * bw,FLAC__int32 val,unsigned bits)347 FLAC__bool FLAC__bitwriter_write_raw_int32(FLAC__BitWriter *bw, FLAC__int32 val, unsigned bits)
348 {
349 	/* zero-out unused bits */
350 	if(bits < 32)
351 		val &= (~(0xffffffff << bits));
352 
353 	return FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, bits);
354 }
355 
FLAC__bitwriter_write_raw_uint64(FLAC__BitWriter * bw,FLAC__uint64 val,unsigned bits)356 FLAC__bool FLAC__bitwriter_write_raw_uint64(FLAC__BitWriter *bw, FLAC__uint64 val, unsigned bits)
357 {
358 	/* this could be a little faster but it's not used for much */
359 	if(bits > 32) {
360 		return
361 			FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)(val>>32), bits-32) &&
362 			FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, 32);
363 	}
364 	else
365 		return FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, bits);
366 }
367 
FLAC__bitwriter_write_raw_uint32_little_endian(FLAC__BitWriter * bw,FLAC__uint32 val)368 FLAC__bool FLAC__bitwriter_write_raw_uint32_little_endian(FLAC__BitWriter *bw, FLAC__uint32 val)
369 {
370 	/* this doesn't need to be that fast as currently it is only used for vorbis comments */
371 
372 	if(!FLAC__bitwriter_write_raw_uint32(bw, val & 0xff, 8))
373 		return false;
374 	if(!FLAC__bitwriter_write_raw_uint32(bw, (val>>8) & 0xff, 8))
375 		return false;
376 	if(!FLAC__bitwriter_write_raw_uint32(bw, (val>>16) & 0xff, 8))
377 		return false;
378 	if(!FLAC__bitwriter_write_raw_uint32(bw, val>>24, 8))
379 		return false;
380 
381 	return true;
382 }
383 
FLAC__bitwriter_write_byte_block(FLAC__BitWriter * bw,const FLAC__byte vals[],unsigned nvals)384 FLAC__bool FLAC__bitwriter_write_byte_block(FLAC__BitWriter *bw, const FLAC__byte vals[], unsigned nvals)
385 {
386 	unsigned i;
387 
388 	/* this could be faster but currently we don't need it to be since it's only used for writing metadata */
389 	for(i = 0; i < nvals; i++) {
390 		if(!FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)(vals[i]), 8))
391 			return false;
392 	}
393 
394 	return true;
395 }
396 
FLAC__bitwriter_write_unary_unsigned(FLAC__BitWriter * bw,unsigned val)397 FLAC__bool FLAC__bitwriter_write_unary_unsigned(FLAC__BitWriter *bw, unsigned val)
398 {
399 	if(val < 32)
400 		return FLAC__bitwriter_write_raw_uint32(bw, 1, ++val);
401 	else
402 		return
403 			FLAC__bitwriter_write_zeroes(bw, val) &&
404 			FLAC__bitwriter_write_raw_uint32(bw, 1, 1);
405 }
406 
FLAC__bitwriter_rice_bits(FLAC__int32 val,unsigned parameter)407 unsigned FLAC__bitwriter_rice_bits(FLAC__int32 val, unsigned parameter)
408 {
409 	FLAC__uint32 uval;
410 
411 	FLAC__ASSERT(parameter < sizeof(unsigned)*8);
412 
413 	/* fold signed to unsigned; actual formula is: negative(v)? -2v-1 : 2v */
414 	uval = (val<<1) ^ (val>>31);
415 
416 	return 1 + parameter + (uval >> parameter);
417 }
418 
419 #if 0 /* UNUSED */
420 unsigned FLAC__bitwriter_golomb_bits_signed(int val, unsigned parameter)
421 {
422 	unsigned bits, msbs, uval;
423 	unsigned k;
424 
425 	FLAC__ASSERT(parameter > 0);
426 
427 	/* fold signed to unsigned */
428 	if(val < 0)
429 		uval = (unsigned)(((-(++val)) << 1) + 1);
430 	else
431 		uval = (unsigned)(val << 1);
432 
433 	k = FLAC__bitmath_ilog2(parameter);
434 	if(parameter == 1u<<k) {
435 		FLAC__ASSERT(k <= 30);
436 
437 		msbs = uval >> k;
438 		bits = 1 + k + msbs;
439 	}
440 	else {
441 		unsigned q, r, d;
442 
443 		d = (1 << (k+1)) - parameter;
444 		q = uval / parameter;
445 		r = uval - (q * parameter);
446 
447 		bits = 1 + q + k;
448 		if(r >= d)
449 			bits++;
450 	}
451 	return bits;
452 }
453 
454 unsigned FLAC__bitwriter_golomb_bits_unsigned(unsigned uval, unsigned parameter)
455 {
456 	unsigned bits, msbs;
457 	unsigned k;
458 
459 	FLAC__ASSERT(parameter > 0);
460 
461 	k = FLAC__bitmath_ilog2(parameter);
462 	if(parameter == 1u<<k) {
463 		FLAC__ASSERT(k <= 30);
464 
465 		msbs = uval >> k;
466 		bits = 1 + k + msbs;
467 	}
468 	else {
469 		unsigned q, r, d;
470 
471 		d = (1 << (k+1)) - parameter;
472 		q = uval / parameter;
473 		r = uval - (q * parameter);
474 
475 		bits = 1 + q + k;
476 		if(r >= d)
477 			bits++;
478 	}
479 	return bits;
480 }
481 #endif /* UNUSED */
482 
FLAC__bitwriter_write_rice_signed(FLAC__BitWriter * bw,FLAC__int32 val,unsigned parameter)483 FLAC__bool FLAC__bitwriter_write_rice_signed(FLAC__BitWriter *bw, FLAC__int32 val, unsigned parameter)
484 {
485 	unsigned total_bits, interesting_bits, msbs;
486 	FLAC__uint32 uval, pattern;
487 
488 	FLAC__ASSERT(0 != bw);
489 	FLAC__ASSERT(0 != bw->buffer);
490 	FLAC__ASSERT(parameter < 8*sizeof(uval));
491 
492 	/* fold signed to unsigned; actual formula is: negative(v)? -2v-1 : 2v */
493 	uval = (val<<1) ^ (val>>31);
494 
495 	msbs = uval >> parameter;
496 	interesting_bits = 1 + parameter;
497 	total_bits = interesting_bits + msbs;
498 	pattern = 1 << parameter; /* the unary end bit */
499 	pattern |= (uval & ((1<<parameter)-1)); /* the binary LSBs */
500 
501 	if(total_bits <= 32)
502 		return FLAC__bitwriter_write_raw_uint32(bw, pattern, total_bits);
503 	else
504 		return
505 			FLAC__bitwriter_write_zeroes(bw, msbs) && /* write the unary MSBs */
506 			FLAC__bitwriter_write_raw_uint32(bw, pattern, interesting_bits); /* write the unary end bit and binary LSBs */
507 }
508 
FLAC__bitwriter_write_rice_signed_block(FLAC__BitWriter * bw,const FLAC__int32 * vals,unsigned nvals,unsigned parameter)509 FLAC__bool FLAC__bitwriter_write_rice_signed_block(FLAC__BitWriter *bw, const FLAC__int32 *vals, unsigned nvals, unsigned parameter)
510 {
511 	const FLAC__uint32 mask1 = FLAC__WORD_ALL_ONES << parameter; /* we val|=mask1 to set the stop bit above it... */
512 	const FLAC__uint32 mask2 = FLAC__WORD_ALL_ONES >> (31-parameter); /* ...then mask off the bits above the stop bit with val&=mask2*/
513 	FLAC__uint32 uval;
514 	unsigned left;
515 	const unsigned lsbits = 1 + parameter;
516 	unsigned msbits;
517 
518 	FLAC__ASSERT(0 != bw);
519 	FLAC__ASSERT(0 != bw->buffer);
520 	FLAC__ASSERT(parameter < 8*sizeof(uint32_t)-1);
521 	/* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
522 	FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
523 
524 	while(nvals) {
525 		/* fold signed to unsigned; actual formula is: negative(v)? -2v-1 : 2v */
526 		uval = (*vals<<1) ^ (*vals>>31);
527 
528 		msbits = uval >> parameter;
529 
530 		if(bw->bits && bw->bits + msbits + lsbits < FLAC__BITS_PER_WORD) { /* i.e. if the whole thing fits in the current uint32_t */
531 			/* ^^^ if bw->bits is 0 then we may have filled the buffer and have no free uint32_t to work in */
532 			bw->bits = bw->bits + msbits + lsbits;
533 			uval |= mask1; /* set stop bit */
534 			uval &= mask2; /* mask off unused top bits */
535 			bw->accum <<= msbits + lsbits;
536 			bw->accum |= uval;
537 		}
538 		else {
539 			/* slightly pessimistic size check but faster than "<= bw->words + (bw->bits+msbits+lsbits+FLAC__BITS_PER_WORD-1)/FLAC__BITS_PER_WORD" */
540 			/* OPT: pessimism may cause flurry of false calls to grow_ which eat up all savings before it */
541 			if(bw->capacity <= bw->words + bw->bits + msbits + 1/*lsbits always fit in 1 uint32_t*/ && !bitwriter_grow_(bw, msbits+lsbits))
542 				return false;
543 
544 			if(msbits) {
545 				/* first part gets to word alignment */
546 				if(bw->bits) {
547 					left = FLAC__BITS_PER_WORD - bw->bits;
548 					if(msbits < left) {
549 						bw->accum <<= msbits;
550 						bw->bits += msbits;
551 						goto break1;
552 					}
553 					else {
554 						bw->accum <<= left;
555 						msbits -= left;
556 						bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
557 						bw->bits = 0;
558 					}
559 				}
560 				/* do whole words */
561 				while(msbits >= FLAC__BITS_PER_WORD) {
562 					bw->buffer[bw->words++] = 0;
563 					msbits -= FLAC__BITS_PER_WORD;
564 				}
565 				/* do any leftovers */
566 				if(msbits > 0) {
567 					bw->accum = 0;
568 					bw->bits = msbits;
569 				}
570 			}
571 break1:
572 			uval |= mask1; /* set stop bit */
573 			uval &= mask2; /* mask off unused top bits */
574 
575 			left = FLAC__BITS_PER_WORD - bw->bits;
576 			if(lsbits < left) {
577 				bw->accum <<= lsbits;
578 				bw->accum |= uval;
579 				bw->bits += lsbits;
580 			}
581 			else {
582 				/* if bw->bits == 0, left==FLAC__BITS_PER_WORD which will always
583 				 * be > lsbits (because of previous assertions) so it would have
584 				 * triggered the (lsbits<left) case above.
585 				 */
586 				FLAC__ASSERT(bw->bits);
587 				FLAC__ASSERT(left < FLAC__BITS_PER_WORD);
588 				bw->accum <<= left;
589 				bw->accum |= uval >> (bw->bits = lsbits - left);
590 				bw->buffer[bw->words++] = SWAP_BE_WORD_TO_HOST(bw->accum);
591 				bw->accum = uval;
592 			}
593 		}
594 		vals++;
595 		nvals--;
596 	}
597 	return true;
598 }
599 
600 #if 0 /* UNUSED */
601 FLAC__bool FLAC__bitwriter_write_golomb_signed(FLAC__BitWriter *bw, int val, unsigned parameter)
602 {
603 	unsigned total_bits, msbs, uval;
604 	unsigned k;
605 
606 	FLAC__ASSERT(0 != bw);
607 	FLAC__ASSERT(0 != bw->buffer);
608 	FLAC__ASSERT(parameter > 0);
609 
610 	/* fold signed to unsigned */
611 	if(val < 0)
612 		uval = (unsigned)(((-(++val)) << 1) + 1);
613 	else
614 		uval = (unsigned)(val << 1);
615 
616 	k = FLAC__bitmath_ilog2(parameter);
617 	if(parameter == 1u<<k) {
618 		unsigned pattern;
619 
620 		FLAC__ASSERT(k <= 30);
621 
622 		msbs = uval >> k;
623 		total_bits = 1 + k + msbs;
624 		pattern = 1 << k; /* the unary end bit */
625 		pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
626 
627 		if(total_bits <= 32) {
628 			if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, total_bits))
629 				return false;
630 		}
631 		else {
632 			/* write the unary MSBs */
633 			if(!FLAC__bitwriter_write_zeroes(bw, msbs))
634 				return false;
635 			/* write the unary end bit and binary LSBs */
636 			if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, k+1))
637 				return false;
638 		}
639 	}
640 	else {
641 		unsigned q, r, d;
642 
643 		d = (1 << (k+1)) - parameter;
644 		q = uval / parameter;
645 		r = uval - (q * parameter);
646 		/* write the unary MSBs */
647 		if(!FLAC__bitwriter_write_zeroes(bw, q))
648 			return false;
649 		/* write the unary end bit */
650 		if(!FLAC__bitwriter_write_raw_uint32(bw, 1, 1))
651 			return false;
652 		/* write the binary LSBs */
653 		if(r >= d) {
654 			if(!FLAC__bitwriter_write_raw_uint32(bw, r+d, k+1))
655 				return false;
656 		}
657 		else {
658 			if(!FLAC__bitwriter_write_raw_uint32(bw, r, k))
659 				return false;
660 		}
661 	}
662 	return true;
663 }
664 
665 FLAC__bool FLAC__bitwriter_write_golomb_unsigned(FLAC__BitWriter *bw, unsigned uval, unsigned parameter)
666 {
667 	unsigned total_bits, msbs;
668 	unsigned k;
669 
670 	FLAC__ASSERT(0 != bw);
671 	FLAC__ASSERT(0 != bw->buffer);
672 	FLAC__ASSERT(parameter > 0);
673 
674 	k = FLAC__bitmath_ilog2(parameter);
675 	if(parameter == 1u<<k) {
676 		unsigned pattern;
677 
678 		FLAC__ASSERT(k <= 30);
679 
680 		msbs = uval >> k;
681 		total_bits = 1 + k + msbs;
682 		pattern = 1 << k; /* the unary end bit */
683 		pattern |= (uval & ((1u<<k)-1)); /* the binary LSBs */
684 
685 		if(total_bits <= 32) {
686 			if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, total_bits))
687 				return false;
688 		}
689 		else {
690 			/* write the unary MSBs */
691 			if(!FLAC__bitwriter_write_zeroes(bw, msbs))
692 				return false;
693 			/* write the unary end bit and binary LSBs */
694 			if(!FLAC__bitwriter_write_raw_uint32(bw, pattern, k+1))
695 				return false;
696 		}
697 	}
698 	else {
699 		unsigned q, r, d;
700 
701 		d = (1 << (k+1)) - parameter;
702 		q = uval / parameter;
703 		r = uval - (q * parameter);
704 		/* write the unary MSBs */
705 		if(!FLAC__bitwriter_write_zeroes(bw, q))
706 			return false;
707 		/* write the unary end bit */
708 		if(!FLAC__bitwriter_write_raw_uint32(bw, 1, 1))
709 			return false;
710 		/* write the binary LSBs */
711 		if(r >= d) {
712 			if(!FLAC__bitwriter_write_raw_uint32(bw, r+d, k+1))
713 				return false;
714 		}
715 		else {
716 			if(!FLAC__bitwriter_write_raw_uint32(bw, r, k))
717 				return false;
718 		}
719 	}
720 	return true;
721 }
722 #endif /* UNUSED */
723 
FLAC__bitwriter_write_utf8_uint32(FLAC__BitWriter * bw,FLAC__uint32 val)724 FLAC__bool FLAC__bitwriter_write_utf8_uint32(FLAC__BitWriter *bw, FLAC__uint32 val)
725 {
726 	FLAC__bool ok = 1;
727 
728 	FLAC__ASSERT(0 != bw);
729 	FLAC__ASSERT(0 != bw->buffer);
730 
731 	FLAC__ASSERT(!(val & 0x80000000)); /* this version only handles 31 bits */
732 
733 	if(val < 0x80) {
734 		return FLAC__bitwriter_write_raw_uint32(bw, val, 8);
735 	}
736 	else if(val < 0x800) {
737 		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xC0 | (val>>6), 8);
738 		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
739 	}
740 	else if(val < 0x10000) {
741 		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xE0 | (val>>12), 8);
742 		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>6)&0x3F), 8);
743 		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
744 	}
745 	else if(val < 0x200000) {
746 		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xF0 | (val>>18), 8);
747 		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>12)&0x3F), 8);
748 		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>6)&0x3F), 8);
749 		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
750 	}
751 	else if(val < 0x4000000) {
752 		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xF8 | (val>>24), 8);
753 		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>18)&0x3F), 8);
754 		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>12)&0x3F), 8);
755 		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>6)&0x3F), 8);
756 		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
757 	}
758 	else {
759 		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xFC | (val>>30), 8);
760 		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>24)&0x3F), 8);
761 		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>18)&0x3F), 8);
762 		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>12)&0x3F), 8);
763 		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | ((val>>6)&0x3F), 8);
764 		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (val&0x3F), 8);
765 	}
766 
767 	return ok;
768 }
769 
FLAC__bitwriter_write_utf8_uint64(FLAC__BitWriter * bw,FLAC__uint64 val)770 FLAC__bool FLAC__bitwriter_write_utf8_uint64(FLAC__BitWriter *bw, FLAC__uint64 val)
771 {
772 	FLAC__bool ok = 1;
773 
774 	FLAC__ASSERT(0 != bw);
775 	FLAC__ASSERT(0 != bw->buffer);
776 
777 	FLAC__ASSERT(!(val & FLAC__U64L(0xFFFFFFF000000000))); /* this version only handles 36 bits */
778 
779 	if(val < 0x80) {
780 		return FLAC__bitwriter_write_raw_uint32(bw, (FLAC__uint32)val, 8);
781 	}
782 	else if(val < 0x800) {
783 		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xC0 | (FLAC__uint32)(val>>6), 8);
784 		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
785 	}
786 	else if(val < 0x10000) {
787 		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xE0 | (FLAC__uint32)(val>>12), 8);
788 		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
789 		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
790 	}
791 	else if(val < 0x200000) {
792 		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xF0 | (FLAC__uint32)(val>>18), 8);
793 		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
794 		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
795 		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
796 	}
797 	else if(val < 0x4000000) {
798 		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xF8 | (FLAC__uint32)(val>>24), 8);
799 		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
800 		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
801 		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
802 		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
803 	}
804 	else if(val < 0x80000000) {
805 		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xFC | (FLAC__uint32)(val>>30), 8);
806 		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
807 		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
808 		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
809 		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
810 		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
811 	}
812 	else {
813 		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0xFE, 8);
814 		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>30)&0x3F), 8);
815 		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>24)&0x3F), 8);
816 		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>18)&0x3F), 8);
817 		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>12)&0x3F), 8);
818 		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)((val>>6)&0x3F), 8);
819 		ok &= FLAC__bitwriter_write_raw_uint32(bw, 0x80 | (FLAC__uint32)(val&0x3F), 8);
820 	}
821 
822 	return ok;
823 }
824 
FLAC__bitwriter_zero_pad_to_byte_boundary(FLAC__BitWriter * bw)825 FLAC__bool FLAC__bitwriter_zero_pad_to_byte_boundary(FLAC__BitWriter *bw)
826 {
827 	/* 0-pad to byte boundary */
828 	if(bw->bits & 7u)
829 		return FLAC__bitwriter_write_zeroes(bw, 8 - (bw->bits & 7u));
830 	else
831 		return true;
832 }
833 
834 /* These functions are declared inline in this file but are also callable as
835  * externs from elsewhere.
836  * According to the C99 spec, section 6.7.4, simply providing a function
837  * prototype in a header file without 'inline' and making the function inline
838  * in this file should be sufficient.
839  * Unfortunately, the Microsoft VS compiler doesn't pick them up externally. To
840  * fix that we add extern declarations here.
841  */
842 extern FLAC__bool FLAC__bitwriter_write_zeroes(FLAC__BitWriter *bw, unsigned bits);
843 extern FLAC__bool FLAC__bitwriter_write_raw_int32(FLAC__BitWriter *bw, FLAC__int32 val, unsigned bits);
844 extern FLAC__bool FLAC__bitwriter_write_raw_uint64(FLAC__BitWriter *bw, FLAC__uint64 val, unsigned bits);
845 extern FLAC__bool FLAC__bitwriter_write_raw_uint32_little_endian(FLAC__BitWriter *bw, FLAC__uint32 val);
846 extern FLAC__bool FLAC__bitwriter_write_byte_block(FLAC__BitWriter *bw, const FLAC__byte vals[], unsigned nvals);
847