Coverage Report

Created: 2025-12-14 06:19

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/flac/src/libFLAC/bitreader.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/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
24.7M
#define FLAC__BYTES_PER_WORD 8    /* sizeof brword */
73
1.17G
#define FLAC__BITS_PER_WORD 64
74
109M
#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
60.7M
#define SWAP_BE_WORD_TO_HOST(x) ENDSWAP_64(x)
80
#endif
81
/* counts the # of zero MSBs in a word */
82
172M
#define COUNT_ZERO_MSBS(word) FLAC__clz_uint64(word)
83
101M
#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
122
static inline void crc16_update_word_(FLAC__BitReader *br, brword word)
123
4.87M
{
124
4.87M
  register uint32_t crc = br->read_crc16;
125
126
24.1M
  for ( ; br->crc16_align < FLAC__BITS_PER_WORD ; br->crc16_align += 8) {
127
19.2M
    uint32_t shift = FLAC__BITS_PER_WORD - 8 - br->crc16_align ;
128
19.2M
    crc = FLAC__CRC16_UPDATE ((uint32_t) (shift < FLAC__BITS_PER_WORD ? (word >> shift) & 0xff : 0), crc);
129
19.2M
  }
130
131
4.87M
  br->read_crc16 = crc;
132
4.87M
  br->crc16_align = 0;
133
4.87M
}
134
135
static inline void crc16_update_block_(FLAC__BitReader *br)
136
5.29M
{
137
5.29M
  if(br->consumed_words > br->crc16_offset && br->crc16_align)
138
4.87M
    crc16_update_word_(br, br->buffer[br->crc16_offset++]);
139
140
  /* Prevent OOB read due to wrap-around. */
141
5.29M
  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
4.77M
  }
153
154
5.29M
  br->crc16_offset = 0;
155
5.29M
}
156
157
static FLAC__bool bitreader_read_from_client_(FLAC__BitReader *br)
158
2.70M
{
159
2.70M
  uint32_t start, end;
160
2.70M
  size_t bytes;
161
2.70M
  FLAC__byte *target;
162
#if WORDS_BIGENDIAN
163
#else
164
2.70M
  brword preswap_backup;
165
2.70M
#endif
166
167
  /* first shift the unconsumed buffer data toward the front as much as possible */
168
2.70M
  if(br->consumed_words > 0) {
169
    /* invalidate last seen framesync */
170
2.66M
    br->last_seen_framesync = -1;
171
172
2.66M
    crc16_update_block_(br); /* CRC consumed words */
173
174
2.66M
    start = br->consumed_words;
175
2.66M
    end = br->words + (br->bytes? 1:0);
176
2.66M
    memmove(br->buffer, br->buffer+start, FLAC__BYTES_PER_WORD * (end - start));
177
178
2.66M
    br->words -= start;
179
2.66M
    br->consumed_words = 0;
180
2.66M
  }
181
182
  /*
183
   * set the target for reading, taking into account word alignment and endianness
184
   */
185
2.70M
  bytes = (br->capacity - br->words) * FLAC__BYTES_PER_WORD - br->bytes;
186
2.70M
  if(bytes == 0)
187
0
    return false; /* no space left, buffer is too small; see note for FLAC__BITREADER_DEFAULT_CAPACITY  */
188
2.70M
  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
2.70M
  preswap_backup = br->buffer[br->words];
201
2.70M
  if(br->bytes)
202
2.43M
    br->buffer[br->words] = SWAP_BE_WORD_TO_HOST(br->buffer[br->words]);
203
2.70M
#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
2.70M
  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
6.86k
    br->buffer[br->words] = preswap_backup;
220
6.86k
#endif
221
6.86k
    return false;
222
6.86k
  }
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
2.69M
  end = (br->words*FLAC__BYTES_PER_WORD + br->bytes + (uint32_t)bytes + (FLAC__BYTES_PER_WORD-1)) / FLAC__BYTES_PER_WORD;
233
61.0M
  for(start = br->words; start < end; start++)
234
58.3M
    br->buffer[start] = SWAP_BE_WORD_TO_HOST(br->buffer[start]);
235
2.69M
#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
2.69M
  end = br->words*FLAC__BYTES_PER_WORD + br->bytes + (uint32_t)bytes;
244
2.69M
  br->words = end / FLAC__BYTES_PER_WORD;
245
2.69M
  br->bytes = end % FLAC__BYTES_PER_WORD;
246
247
2.69M
  return true;
248
2.70M
}
249
250
/***********************************************************************
251
 *
252
 * Class constructor/destructor
253
 *
254
 ***********************************************************************/
255
256
FLAC__BitReader *FLAC__bitreader_new(void)
257
12.0k
{
258
12.0k
  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
12.0k
  return br;
270
12.0k
}
271
272
void FLAC__bitreader_delete(FLAC__BitReader *br)
273
12.0k
{
274
12.0k
  FLAC__ASSERT(0 != br);
275
276
12.0k
  FLAC__bitreader_free(br);
277
12.0k
  free(br);
278
12.0k
}
279
280
/***********************************************************************
281
 *
282
 * Public class methods
283
 *
284
 ***********************************************************************/
285
286
FLAC__bool FLAC__bitreader_init(FLAC__BitReader *br, FLAC__BitReaderReadCallback rcb, void *cd)
287
11.8k
{
288
11.8k
  FLAC__ASSERT(0 != br);
289
290
11.8k
  br->words = br->bytes = 0;
291
11.8k
  br->consumed_words = br->consumed_bits = 0;
292
11.8k
  br->capacity = FLAC__BITREADER_DEFAULT_CAPACITY;
293
11.8k
  br->buffer = malloc(sizeof(brword) * br->capacity);
294
11.8k
  if(br->buffer == 0)
295
0
    return false;
296
11.8k
  br->read_callback = rcb;
297
11.8k
  br->client_data = cd;
298
11.8k
  br->read_limit_set = false;
299
11.8k
  br->read_limit = -1;
300
11.8k
  br->last_seen_framesync = -1;
301
302
11.8k
  return true;
303
11.8k
}
304
305
void FLAC__bitreader_free(FLAC__BitReader *br)
306
23.8k
{
307
23.8k
  FLAC__ASSERT(0 != br);
308
309
23.8k
  if(0 != br->buffer)
310
11.8k
    free(br->buffer);
311
23.8k
  br->buffer = 0;
312
23.8k
  br->capacity = 0;
313
23.8k
  br->words = br->bytes = 0;
314
23.8k
  br->consumed_words = br->consumed_bits = 0;
315
23.8k
  br->read_callback = 0;
316
23.8k
  br->client_data = 0;
317
23.8k
  br->read_limit_set = false;
318
23.8k
  br->read_limit = -1;
319
23.8k
  br->last_seen_framesync = -1;
320
23.8k
}
321
322
FLAC__bool FLAC__bitreader_clear(FLAC__BitReader *br)
323
11.8k
{
324
11.8k
  br->words = br->bytes = 0;
325
11.8k
  br->consumed_words = br->consumed_bits = 0;
326
11.8k
  br->read_limit_set = false;
327
11.8k
  br->read_limit = -1;
328
11.8k
  br->last_seen_framesync = -1;
329
11.8k
  return true;
330
11.8k
}
331
332
void FLAC__bitreader_set_framesync_location(FLAC__BitReader *br)
333
2.64M
{
334
2.64M
  br->last_seen_framesync = br->consumed_words * FLAC__BYTES_PER_WORD + br->consumed_bits / 8;
335
2.64M
}
336
337
FLAC__bool FLAC__bitreader_rewind_to_after_last_seen_framesync(FLAC__BitReader *br)
338
11.8k
{
339
11.8k
  if(br->last_seen_framesync == (uint32_t)-1) {
340
659
    br->consumed_words = br->consumed_bits = 0;
341
659
    return false;
342
659
  }
343
11.2k
  else {
344
11.2k
    br->consumed_words = (br->last_seen_framesync + 1) / FLAC__BYTES_PER_WORD;
345
11.2k
    br->consumed_bits  = ((br->last_seen_framesync + 1) % FLAC__BYTES_PER_WORD) * 8;
346
11.2k
    return true;
347
11.2k
  }
348
11.8k
}
349
350
void FLAC__bitreader_reset_read_crc16(FLAC__BitReader *br, FLAC__uint16 seed)
351
2.65M
{
352
2.65M
  FLAC__ASSERT(0 != br);
353
2.65M
  FLAC__ASSERT(0 != br->buffer);
354
2.65M
  FLAC__ASSERT((br->consumed_bits & 7) == 0);
355
356
2.65M
  br->read_crc16 = (uint32_t)seed;
357
2.65M
  br->crc16_offset = br->consumed_words;
358
2.65M
  br->crc16_align = br->consumed_bits;
359
2.65M
}
360
361
FLAC__uint16 FLAC__bitreader_get_read_crc16(FLAC__BitReader *br)
362
2.62M
{
363
2.62M
  FLAC__ASSERT(0 != br);
364
2.62M
  FLAC__ASSERT(0 != br->buffer);
365
366
  /* CRC consumed words up to here */
367
2.62M
  crc16_update_block_(br);
368
369
2.62M
  FLAC__ASSERT((br->consumed_bits & 7) == 0);
370
2.62M
  FLAC__ASSERT(br->crc16_align <= br->consumed_bits);
371
372
  /* CRC any tail bytes in a partially-consumed word */
373
2.62M
  if(br->consumed_bits) {
374
2.43M
    const brword tail = br->buffer[br->consumed_words];
375
12.4M
    for( ; br->crc16_align < br->consumed_bits; br->crc16_align += 8)
376
9.97M
      br->read_crc16 = FLAC__CRC16_UPDATE((uint32_t)((tail >> (FLAC__BITS_PER_WORD-8-br->crc16_align)) & 0xff), br->read_crc16);
377
2.43M
  }
378
2.62M
  return br->read_crc16;
379
2.62M
}
380
381
inline FLAC__bool FLAC__bitreader_is_consumed_byte_aligned(const FLAC__BitReader *br)
382
8.11M
{
383
8.11M
  return ((br->consumed_bits & 7) == 0);
384
8.11M
}
385
386
inline uint32_t FLAC__bitreader_bits_left_for_byte_alignment(const FLAC__BitReader *br)
387
1.10M
{
388
1.10M
  return 8 - (br->consumed_bits & 7);
389
1.10M
}
390
391
inline uint32_t FLAC__bitreader_get_input_bits_unconsumed(const FLAC__BitReader *br)
392
0
{
393
0
  return (br->words-br->consumed_words)*FLAC__BITS_PER_WORD + br->bytes*8 - br->consumed_bits;
394
0
}
395
396
void FLAC__bitreader_set_limit(FLAC__BitReader *br, uint32_t limit)
397
38.1k
{
398
38.1k
  br->read_limit = limit;
399
38.1k
  br->read_limit_set = true;
400
38.1k
}
401
402
void FLAC__bitreader_remove_limit(FLAC__BitReader *br)
403
38.1k
{
404
38.1k
  br->read_limit_set = false;
405
38.1k
  br->read_limit = -1;
406
38.1k
}
407
408
uint32_t FLAC__bitreader_limit_remaining(FLAC__BitReader *br)
409
39.9k
{
410
39.9k
  FLAC__ASSERT(br->read_limit_set);
411
39.9k
  return br->read_limit;
412
39.9k
}
413
void FLAC__bitreader_limit_invalidate(FLAC__BitReader *br)
414
297
{
415
297
  br->read_limit = -1;
416
297
}
417
418
FLAC__bool FLAC__bitreader_read_raw_uint32(FLAC__BitReader *br, FLAC__uint32 *val, uint32_t bits)
419
111M
{
420
111M
  FLAC__ASSERT(0 != br);
421
111M
  FLAC__ASSERT(0 != br->buffer);
422
423
111M
  FLAC__ASSERT(bits <= 32);
424
111M
  FLAC__ASSERT((br->capacity*FLAC__BITS_PER_WORD) * 2 >= bits);
425
111M
  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
111M
  FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
429
430
111M
  if(bits == 0) { /* OPT: investigate if this can ever happen, maybe change to assertion */
431
0
    *val = 0;
432
0
    return true;
433
0
  }
434
435
111M
  if(br->read_limit_set && br->read_limit < (uint32_t)-1){
436
119k
    if(br->read_limit < bits) {
437
8
      br->read_limit = -1;
438
8
      return false;
439
8
    }
440
119k
    else
441
119k
      br->read_limit -= bits;
442
119k
  }
443
444
114M
  while((br->words-br->consumed_words)*FLAC__BITS_PER_WORD + br->bytes*8 - br->consumed_bits < bits) {
445
2.69M
    if(!bitreader_read_from_client_(br))
446
6.44k
      return false;
447
2.69M
  }
448
111M
  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
106M
    if(br->consumed_bits) {
451
      /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
452
98.5M
      const uint32_t n = FLAC__BITS_PER_WORD - br->consumed_bits;
453
98.5M
      const brword word = br->buffer[br->consumed_words];
454
98.5M
      const brword mask = br->consumed_bits < FLAC__BITS_PER_WORD ? FLAC__WORD_ALL_ONES >> br->consumed_bits : 0;
455
98.5M
      if(bits < n) {
456
73.7M
        uint32_t shift = n - bits;
457
73.7M
        *val = shift < FLAC__BITS_PER_WORD ? (FLAC__uint32)((word & mask) >> shift) : 0; /* The result has <= 32 non-zero bits */
458
73.7M
        br->consumed_bits += bits;
459
73.7M
        return true;
460
73.7M
      }
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
24.8M
      *val = (FLAC__uint32)(word & mask);
463
24.8M
      bits -= n;
464
24.8M
      br->consumed_words++;
465
24.8M
      br->consumed_bits = 0;
466
24.8M
      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
16.2M
        uint32_t shift = FLAC__BITS_PER_WORD - bits;
468
16.2M
        *val = bits < 32 ? *val << bits : 0;
469
16.2M
        *val |= shift < FLAC__BITS_PER_WORD ? (FLAC__uint32)(br->buffer[br->consumed_words] >> shift) : 0;
470
16.2M
        br->consumed_bits = bits;
471
16.2M
      }
472
24.8M
      return true;
473
98.5M
    }
474
7.59M
    else { /* br->consumed_bits == 0 */
475
7.59M
      const brword word = br->buffer[br->consumed_words];
476
7.59M
      if(bits < FLAC__BITS_PER_WORD) {
477
7.59M
        *val = (FLAC__uint32)(word >> (FLAC__BITS_PER_WORD-bits));
478
7.59M
        br->consumed_bits = bits;
479
7.59M
        return true;
480
7.59M
      }
481
      /* at this point bits == FLAC__BITS_PER_WORD == 32; because of previous assertions, it can't be larger */
482
0
      *val = (FLAC__uint32)word;
483
0
      br->consumed_words++;
484
0
      return true;
485
7.59M
    }
486
106M
  }
487
5.63M
  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
5.63M
    if(br->consumed_bits) {
494
      /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
495
4.63M
      FLAC__ASSERT(br->consumed_bits + bits <= br->bytes*8);
496
4.63M
      *val = (FLAC__uint32)((br->buffer[br->consumed_words] & (FLAC__WORD_ALL_ONES >> br->consumed_bits)) >> (FLAC__BITS_PER_WORD-br->consumed_bits-bits));
497
4.63M
      br->consumed_bits += bits;
498
4.63M
      return true;
499
4.63M
    }
500
995k
    else {
501
995k
      *val = (FLAC__uint32)(br->buffer[br->consumed_words] >> (FLAC__BITS_PER_WORD-bits));
502
995k
      br->consumed_bits += bits;
503
995k
      return true;
504
995k
    }
505
5.63M
  }
506
111M
}
507
508
FLAC__bool FLAC__bitreader_read_raw_int32(FLAC__BitReader *br, FLAC__int32 *val, uint32_t bits)
509
31.0M
{
510
31.0M
  FLAC__uint32 uval, mask;
511
  /* OPT: inline raw uint32 code here, or make into a macro if possible in the .h file */
512
31.0M
  if (bits < 1 || ! FLAC__bitreader_read_raw_uint32(br, &uval, bits))
513
46
    return false;
514
  /* sign-extend *val assuming it is currently bits wide. */
515
  /* From: https://graphics.stanford.edu/~seander/bithacks.html#FixedSignExtend */
516
31.0M
  mask = bits >= 33 ? 0 : 1lu << (bits - 1);
517
31.0M
  *val = (uval ^ mask) - mask;
518
31.0M
  return true;
519
31.0M
}
520
521
FLAC__bool FLAC__bitreader_read_raw_uint64(FLAC__BitReader *br, FLAC__uint64 *val, uint32_t bits)
522
16.9M
{
523
16.9M
  FLAC__uint32 hi, lo;
524
525
16.9M
  if(bits > 32) {
526
12.2M
    if(!FLAC__bitreader_read_raw_uint32(br, &hi, bits-32))
527
41
      return false;
528
12.2M
    if(!FLAC__bitreader_read_raw_uint32(br, &lo, 32))
529
37
      return false;
530
12.2M
    *val = hi;
531
12.2M
    *val <<= 32;
532
12.2M
    *val |= lo;
533
12.2M
  }
534
4.69M
  else {
535
4.69M
    if(!FLAC__bitreader_read_raw_uint32(br, &lo, bits))
536
67
      return false;
537
4.69M
    *val = lo;
538
4.69M
  }
539
16.9M
  return true;
540
16.9M
}
541
542
FLAC__bool FLAC__bitreader_read_raw_int64(FLAC__BitReader *br, FLAC__int64 *val, uint32_t bits)
543
16.9M
{
544
16.9M
  FLAC__uint64 uval, mask;
545
  /* OPT: inline raw uint64 code here, or make into a macro if possible in the .h file */
546
16.9M
  if (bits < 1 || ! FLAC__bitreader_read_raw_uint64(br, &uval, bits))
547
85
    return false;
548
  /* sign-extend *val assuming it is currently bits wide. */
549
  /* From: https://graphics.stanford.edu/~seander/bithacks.html#FixedSignExtend */
550
16.9M
  mask = bits >= 65 ? 0 : 1llu << (bits - 1);
551
16.9M
  *val = (uval ^ mask) - mask;
552
16.9M
  return true;
553
16.9M
}
554
555
inline FLAC__bool FLAC__bitreader_read_uint32_little_endian(FLAC__BitReader *br, FLAC__uint32 *val)
556
5.53k
{
557
5.53k
  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
5.53k
  if(!FLAC__bitreader_read_raw_uint32(br, &x32, 8))
562
13
    return false;
563
564
5.52k
  if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8))
565
6
    return false;
566
5.52k
  x32 |= (x8 << 8);
567
568
5.52k
  if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8))
569
2
    return false;
570
5.51k
  x32 |= (x8 << 16);
571
572
5.51k
  if(!FLAC__bitreader_read_raw_uint32(br, &x8, 8))
573
4
    return false;
574
5.51k
  x32 |= (x8 << 24);
575
576
5.51k
  *val = x32;
577
5.51k
  return true;
578
5.51k
}
579
580
FLAC__bool FLAC__bitreader_skip_bits_no_crc(FLAC__BitReader *br, uint32_t bits)
581
5.92k
{
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
5.92k
  FLAC__ASSERT(0 != br);
587
5.92k
  FLAC__ASSERT(0 != br->buffer);
588
589
5.92k
  if(bits > 0) {
590
5.92k
    const uint32_t n = br->consumed_bits & 7;
591
5.92k
    uint32_t m;
592
5.92k
    FLAC__uint32 x;
593
594
5.92k
    if(n != 0) {
595
1.74k
      m = flac_min(8-n, bits);
596
1.74k
      if(!FLAC__bitreader_read_raw_uint32(br, &x, m))
597
0
        return false;
598
1.74k
      bits -= m;
599
1.74k
    }
600
5.92k
    m = bits / 8;
601
5.92k
    if(m > 0) {
602
5.92k
      if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(br, m))
603
32
        return false;
604
5.89k
      bits %= 8;
605
5.89k
    }
606
5.89k
    if(bits > 0) {
607
0
      if(!FLAC__bitreader_read_raw_uint32(br, &x, bits))
608
0
        return false;
609
0
    }
610
5.89k
  }
611
612
5.89k
  return true;
613
5.92k
}
614
615
FLAC__bool FLAC__bitreader_skip_byte_block_aligned_no_crc(FLAC__BitReader *br, uint32_t nvals)
616
67.0k
{
617
67.0k
  FLAC__uint32 x;
618
619
67.0k
  FLAC__ASSERT(0 != br);
620
67.0k
  FLAC__ASSERT(0 != br->buffer);
621
67.0k
  FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(br));
622
623
67.0k
  if(br->read_limit_set && br->read_limit < (uint32_t)-1){
624
39.1k
    if(br->read_limit < nvals*8){
625
1
      br->read_limit = -1;
626
1
      return false;
627
1
    }
628
39.1k
  }
629
630
  /* step 1: skip over partial head word to get word aligned */
631
118k
  while(nvals && br->consumed_bits) { /* i.e. run until we read 'nvals' bytes or we hit the end of the head word */
632
51.1k
    if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
633
22
      return false;
634
51.1k
    nvals--;
635
51.1k
  }
636
67.0k
  if(0 == nvals)
637
52.5k
    return true;
638
639
  /* step 2: skip whole words in chunks */
640
119k
  while(nvals >= FLAC__BYTES_PER_WORD) {
641
104k
    if(br->consumed_words < br->words) {
642
104k
      br->consumed_words++;
643
104k
      nvals -= FLAC__BYTES_PER_WORD;
644
104k
      if(br->read_limit_set)
645
13.7k
        br->read_limit -= FLAC__BITS_PER_WORD;
646
104k
    }
647
897
    else if(!bitreader_read_from_client_(br))
648
161
      return false;
649
104k
  }
650
  /* step 3: skip any remainder from partial tail bytes */
651
61.1k
  while(nvals) {
652
46.7k
    if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
653
8
      return false;
654
46.7k
    nvals--;
655
46.7k
  }
656
657
14.3k
  return true;
658
14.3k
}
659
660
FLAC__bool FLAC__bitreader_read_byte_block_aligned_no_crc(FLAC__BitReader *br, FLAC__byte *val, uint32_t nvals)
661
15.9k
{
662
15.9k
  FLAC__uint32 x;
663
664
15.9k
  FLAC__ASSERT(0 != br);
665
15.9k
  FLAC__ASSERT(0 != br->buffer);
666
15.9k
  FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(br));
667
668
15.9k
  if(br->read_limit_set && br->read_limit < (uint32_t)-1){
669
9.31k
    if(br->read_limit < nvals*8){
670
44
      br->read_limit = -1;
671
44
      return false;
672
44
    }
673
9.31k
  }
674
675
  /* step 1: read from partial head word to get word aligned */
676
62.8k
  while(nvals && br->consumed_bits) { /* i.e. run until we read 'nvals' bytes or we hit the end of the head word */
677
46.9k
    if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
678
65
      return false;
679
46.9k
    *val++ = (FLAC__byte)x;
680
46.9k
    nvals--;
681
46.9k
  }
682
15.8k
  if(0 == nvals)
683
6.45k
    return true;
684
  /* step 2: read whole words in chunks */
685
109k
  while(nvals >= FLAC__BYTES_PER_WORD) {
686
100k
    if(br->consumed_words < br->words) {
687
94.9k
      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
94.9k
      val[1] = (FLAC__byte)(word >> 48);
696
94.9k
      val[2] = (FLAC__byte)(word >> 40);
697
94.9k
      val[3] = (FLAC__byte)(word >> 32);
698
94.9k
      val[4] = (FLAC__byte)(word >> 24);
699
94.9k
      val[5] = (FLAC__byte)(word >> 16);
700
94.9k
      val[6] = (FLAC__byte)(word >> 8);
701
94.9k
      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
94.9k
      val += FLAC__BYTES_PER_WORD;
707
94.9k
      nvals -= FLAC__BYTES_PER_WORD;
708
94.9k
      if(br->read_limit_set)
709
90.0k
        br->read_limit -= FLAC__BITS_PER_WORD;
710
94.9k
    }
711
5.28k
    else if(!bitreader_read_from_client_(br))
712
190
      return false;
713
100k
  }
714
  /* step 3: read any remainder from partial tail bytes */
715
31.7k
  while(nvals) {
716
22.5k
    if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
717
12
      return false;
718
22.5k
    *val++ = (FLAC__byte)x;
719
22.5k
    nvals--;
720
22.5k
  }
721
722
9.22k
  return true;
723
9.23k
}
724
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
172M
{
746
172M
  uint32_t i;
747
748
172M
  FLAC__ASSERT(0 != br);
749
172M
  FLAC__ASSERT(0 != br->buffer);
750
751
172M
  *val = 0;
752
172M
  while(1) {
753
173M
    while(br->consumed_words < br->words) { /* if we've not consumed up to a partial tail word... */
754
166M
      brword b = br->consumed_bits < FLAC__BITS_PER_WORD ? br->buffer[br->consumed_words] << br->consumed_bits : 0;
755
166M
      if(b) {
756
166M
        i = COUNT_ZERO_MSBS(b);
757
166M
        *val += i;
758
166M
        i++;
759
166M
        br->consumed_bits += i;
760
166M
        if(br->consumed_bits >= FLAC__BITS_PER_WORD) { /* faster way of testing if(br->consumed_bits == FLAC__BITS_PER_WORD) */
761
2.68M
          br->consumed_words++;
762
2.68M
          br->consumed_bits = 0;
763
2.68M
        }
764
166M
        return true;
765
166M
      }
766
635k
      else {
767
635k
        *val += FLAC__BITS_PER_WORD - br->consumed_bits;
768
635k
        br->consumed_words++;
769
635k
        br->consumed_bits = 0;
770
        /* didn't find stop bit yet, have to keep going... */
771
635k
      }
772
166M
    }
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
6.06M
    if(br->bytes*8 > br->consumed_bits) {
781
6.06M
      const uint32_t end = br->bytes * 8;
782
6.06M
      brword b = (br->buffer[br->consumed_words] & (FLAC__WORD_ALL_ONES << (FLAC__BITS_PER_WORD-end))) << br->consumed_bits;
783
6.06M
      if(b) {
784
6.05M
        i = COUNT_ZERO_MSBS(b);
785
6.05M
        *val += i;
786
6.05M
        i++;
787
6.05M
        br->consumed_bits += i;
788
6.05M
        FLAC__ASSERT(br->consumed_bits < FLAC__BITS_PER_WORD);
789
6.05M
        return true;
790
6.05M
      }
791
115
      else {
792
115
        *val += end - br->consumed_bits;
793
115
        br->consumed_bits = end;
794
115
        FLAC__ASSERT(br->consumed_bits < FLAC__BITS_PER_WORD);
795
        /* didn't find stop bit yet, have to keep going... */
796
115
      }
797
6.06M
    }
798
3.48k
    if(!bitreader_read_from_client_(br))
799
63
      return false;
800
3.48k
  }
801
172M
}
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 */
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
2.63M
{
930
2.63M
  FLAC__uint32 v = 0;
931
2.63M
  FLAC__uint32 x;
932
2.63M
  uint32_t i;
933
934
2.63M
  if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
935
19
    return false;
936
2.63M
  if(raw)
937
2.63M
    raw[(*rawlen)++] = (FLAC__byte)x;
938
2.63M
  if(!(x & 0x80)) { /* 0xxxxxxx */
939
170k
    v = x;
940
170k
    i = 0;
941
170k
  }
942
2.46M
  else if((x & 0xE0) == 0xC0) { /* 110xxxxx */
943
486k
    v = x & 0x1F;
944
486k
    i = 1;
945
486k
  }
946
1.98M
  else if((x & 0xF0) == 0xE0) { /* 1110xxxx */
947
1.03M
    v = x & 0x0F;
948
1.03M
    i = 2;
949
1.03M
  }
950
942k
  else if((x & 0xF8) == 0xF0) { /* 11110xxx */
951
940k
    v = x & 0x07;
952
940k
    i = 3;
953
940k
  }
954
2.15k
  else if((x & 0xFC) == 0xF8) { /* 111110xx */
955
416
    v = x & 0x03;
956
416
    i = 4;
957
416
  }
958
1.74k
  else if((x & 0xFE) == 0xFC) { /* 1111110x */
959
346
    v = x & 0x01;
960
346
    i = 5;
961
346
  }
962
1.39k
  else {
963
1.39k
    *val = 0xffffffff;
964
1.39k
    return true;
965
1.39k
  }
966
8.02M
  for( ; i; i--) {
967
5.38M
    if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
968
5
      return false;
969
5.38M
    if(raw)
970
5.38M
      raw[(*rawlen)++] = (FLAC__byte)x;
971
5.38M
    if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
972
1.40k
      *val = 0xffffffff;
973
1.40k
      return true;
974
1.40k
    }
975
5.38M
    v <<= 6;
976
5.38M
    v |= (x & 0x3F);
977
5.38M
  }
978
2.63M
  *val = v;
979
2.63M
  return true;
980
2.63M
}
981
982
/* on return, if *val == 0xffffffffffffffff then the utf-8 sequence was invalid, but the return value will be true */
983
FLAC__bool FLAC__bitreader_read_utf8_uint64(FLAC__BitReader *br, FLAC__uint64 *val, FLAC__byte *raw, uint32_t *rawlen)
984
10.8k
{
985
10.8k
  FLAC__uint64 v = 0;
986
10.8k
  FLAC__uint32 x;
987
10.8k
  uint32_t i;
988
989
10.8k
  if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
990
10
    return false;
991
10.8k
  if(raw)
992
10.8k
    raw[(*rawlen)++] = (FLAC__byte)x;
993
10.8k
  if(!(x & 0x80)) { /* 0xxxxxxx */
994
7.72k
    v = x;
995
7.72k
    i = 0;
996
7.72k
  }
997
3.11k
  else if((x & 0xE0) == 0xC0) { /* 110xxxxx */
998
347
    v = x & 0x1F;
999
347
    i = 1;
1000
347
  }
1001
2.76k
  else if((x & 0xF0) == 0xE0) { /* 1110xxxx */
1002
492
    v = x & 0x0F;
1003
492
    i = 2;
1004
492
  }
1005
2.27k
  else if((x & 0xF8) == 0xF0) { /* 11110xxx */
1006
263
    v = x & 0x07;
1007
263
    i = 3;
1008
263
  }
1009
2.00k
  else if((x & 0xFC) == 0xF8) { /* 111110xx */
1010
468
    v = x & 0x03;
1011
468
    i = 4;
1012
468
  }
1013
1.54k
  else if((x & 0xFE) == 0xFC) { /* 1111110x */
1014
231
    v = x & 0x01;
1015
231
    i = 5;
1016
231
  }
1017
1.31k
  else if(x == 0xFE) { /* 11111110 */
1018
113
    v = 0;
1019
113
    i = 6;
1020
113
  }
1021
1.19k
  else {
1022
1.19k
    *val = FLAC__U64L(0xffffffffffffffff);
1023
1.19k
    return true;
1024
1.19k
  }
1025
10.9k
  for( ; i; i--) {
1026
2.72k
    if(!FLAC__bitreader_read_raw_uint32(br, &x, 8))
1027
3
      return false;
1028
2.72k
    if(raw)
1029
2.72k
      raw[(*rawlen)++] = (FLAC__byte)x;
1030
2.72k
    if(!(x & 0x80) || (x & 0x40)) { /* 10xxxxxx */
1031
1.45k
      *val = FLAC__U64L(0xffffffffffffffff);
1032
1.45k
      return true;
1033
1.45k
    }
1034
1.26k
    v <<= 6;
1035
1.26k
    v |= (x & 0x3F);
1036
1.26k
  }
1037
8.18k
  *val = v;
1038
  return true;
1039
9.64k
}
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);