Coverage Report

Created: 2026-05-30 06:48

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