Coverage Report

Created: 2025-07-11 06:51

/src/flac/src/libFLAC/stream_decoder.c
Line
Count
Source (jump to first uncovered line)
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 <stdio.h>
38
#include <stdlib.h> /* for malloc() */
39
#include <string.h> /* for memset/memcpy() */
40
#include <sys/types.h> /* for off_t */
41
#include <sys/stat.h>  /* for stat() */
42
#include "share/compat.h"
43
#include "FLAC/assert.h"
44
#include "share/alloc.h"
45
#include "protected/stream_decoder.h"
46
#include "private/bitreader.h"
47
#include "private/bitmath.h"
48
#include "private/cpu.h"
49
#include "private/crc.h"
50
#include "private/fixed.h"
51
#include "private/format.h"
52
#include "private/lpc.h"
53
#include "private/md5.h"
54
#include "private/memory.h"
55
#include "private/macros.h"
56
57
58
/* technically this should be in an "export.c" but this is convenient enough */
59
FLAC_API int FLAC_API_SUPPORTS_OGG_FLAC = FLAC__HAS_OGG;
60
61
62
/***********************************************************************
63
 *
64
 * Private static data
65
 *
66
 ***********************************************************************/
67
68
static const FLAC__byte ID3V2_TAG_[3] = { 'I', 'D', '3' };
69
70
/***********************************************************************
71
 *
72
 * Private class method prototypes
73
 *
74
 ***********************************************************************/
75
76
static void set_defaults_(FLAC__StreamDecoder *decoder);
77
static FILE *get_binary_stdin_(void);
78
static FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, uint32_t size, uint32_t channels, uint32_t bps);
79
static FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id);
80
static FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder);
81
static FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder);
82
static FLAC__bool read_metadata_streaminfo_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, uint32_t length);
83
static FLAC__bool read_metadata_seektable_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, uint32_t length);
84
static FLAC__bool read_metadata_vorbiscomment_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_VorbisComment *obj, uint32_t length);
85
static FLAC__bool read_metadata_cuesheet_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_CueSheet *obj);
86
static FLAC__bool read_metadata_picture_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_Picture *obj);
87
static FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder);
88
static FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder);
89
static FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame, FLAC__bool do_full_decode);
90
static FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder);
91
static FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, uint32_t channel, uint32_t bps, FLAC__bool do_full_decode);
92
static FLAC__bool read_subframe_constant_(FLAC__StreamDecoder *decoder, uint32_t channel, uint32_t bps, FLAC__bool do_full_decode);
93
static FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, uint32_t channel, uint32_t bps, const uint32_t order, FLAC__bool do_full_decode);
94
static FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, uint32_t channel, uint32_t bps, const uint32_t order, FLAC__bool do_full_decode);
95
static FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, uint32_t channel, uint32_t bps, FLAC__bool do_full_decode);
96
static FLAC__bool read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, uint32_t predictor_order, uint32_t partition_order, FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, FLAC__int32 *residual, FLAC__bool is_extended);
97
static FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder);
98
static void       undo_channel_coding(FLAC__StreamDecoder *decoder);
99
static FLAC__bool read_callback_(FLAC__byte buffer[], size_t *bytes, void *client_data);
100
#if FLAC__HAS_OGG
101
static FLAC__StreamDecoderReadStatus read_callback_ogg_aspect_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes);
102
static FLAC__OggDecoderAspectReadStatus read_callback_proxy_(const void *void_decoder, FLAC__byte buffer[], size_t *bytes, void *client_data);
103
#endif
104
static FLAC__StreamDecoderWriteStatus write_audio_frame_to_client_(FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[]);
105
static void send_error_to_client_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status);
106
static FLAC__bool seek_to_absolute_sample_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample);
107
#if FLAC__HAS_OGG
108
static FLAC__bool seek_to_absolute_sample_ogg_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample);
109
#endif
110
static FLAC__StreamDecoderReadStatus file_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data);
111
static FLAC__StreamDecoderSeekStatus file_seek_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data);
112
static FLAC__StreamDecoderTellStatus file_tell_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data);
113
static FLAC__StreamDecoderLengthStatus file_length_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data);
114
static FLAC__bool file_eof_callback_(const FLAC__StreamDecoder *decoder, void *client_data);
115
static void reset_decoder_internal_(FLAC__StreamDecoder* decoder);
116
117
/***********************************************************************
118
 *
119
 * Private class data
120
 *
121
 ***********************************************************************/
122
123
typedef struct FLAC__StreamDecoderPrivate {
124
  FLAC__bool is_ogg;
125
  FLAC__StreamDecoderReadCallback read_callback;
126
  FLAC__StreamDecoderSeekCallback seek_callback;
127
  FLAC__StreamDecoderTellCallback tell_callback;
128
  FLAC__StreamDecoderLengthCallback length_callback;
129
  FLAC__StreamDecoderEofCallback eof_callback;
130
  FLAC__StreamDecoderWriteCallback write_callback;
131
  FLAC__StreamDecoderMetadataCallback metadata_callback;
132
  FLAC__StreamDecoderErrorCallback error_callback;
133
  void *client_data;
134
  FILE *file; /* only used if FLAC__stream_decoder_init_file()/FLAC__stream_decoder_init_file() called, else NULL */
135
  FLAC__BitReader *input;
136
  FLAC__int32 *output[FLAC__MAX_CHANNELS];
137
  FLAC__int32 *residual[FLAC__MAX_CHANNELS]; /* WATCHOUT: these are the aligned pointers; the real pointers that should be free()'d are residual_unaligned[] below */
138
  FLAC__int64 *side_subframe;
139
  FLAC__bool side_subframe_in_use;
140
  FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents[FLAC__MAX_CHANNELS];
141
  uint32_t output_capacity, output_channels;
142
  FLAC__uint32 fixed_block_size, next_fixed_block_size;
143
  FLAC__uint64 samples_decoded;
144
  FLAC__bool has_stream_info, has_seek_table;
145
  FLAC__StreamMetadata stream_info;
146
  FLAC__StreamMetadata seek_table;
147
  FLAC__bool metadata_filter[128]; /* MAGIC number 128 == total number of metadata block types == 1 << 7 */
148
  FLAC__byte *metadata_filter_ids;
149
  size_t metadata_filter_ids_count, metadata_filter_ids_capacity; /* units for both are IDs, not bytes */
150
  FLAC__Frame frame;
151
  FLAC__bool cached; /* true if there is a byte in lookahead */
152
  FLAC__CPUInfo cpuinfo;
153
  FLAC__byte header_warmup[2]; /* contains the sync code and reserved bits */
154
  FLAC__byte lookahead; /* temp storage when we need to look ahead one byte in the stream */
155
  /* unaligned (original) pointers to allocated data */
156
  FLAC__int32 *residual_unaligned[FLAC__MAX_CHANNELS];
157
  FLAC__bool do_md5_checking; /* initially gets protected_->md5_checking but is turned off after a seek or if the metadata has a zero MD5 */
158
  FLAC__bool internal_reset_hack; /* used only during init() so we can call reset to set up the decoder without rewinding the input */
159
  FLAC__bool is_seeking;
160
  FLAC__bool is_indexing; /* to be able to seek in chained streams */
161
  FLAC__MD5Context md5context;
162
  FLAC__byte computed_md5sum[16]; /* this is the sum we computed from the decoded data */
163
  /* (the rest of these are only used for seeking) */
164
  FLAC__Frame last_frame; /* holds the info of the last frame we decoded or seeked to */
165
  FLAC__bool last_frame_is_set;
166
  FLAC__uint64 first_frame_offset; /* hint to the seek routine of where in the stream the first audio frame starts */
167
  FLAC__uint64 last_seen_framesync; /* if tell callback works, the location of the last seen frame sync code, to rewind to if needed */
168
  FLAC__uint64 target_sample;
169
  uint32_t unparseable_frame_count; /* used to tell whether we're decoding a future version of FLAC or just got a bad sync */
170
  FLAC__bool got_a_frame; /* hack needed in Ogg FLAC seek routine and find_total_samples to check when process_single() actually writes a frame */
171
  FLAC__bool (*local_bitreader_read_rice_signed_block)(FLAC__BitReader *br, int vals[], uint32_t nvals, uint32_t parameter);
172
  FLAC__bool error_has_been_sent; /* To check whether a missing frame has been signalled yet */
173
#if FLAC__HAS_OGG
174
  FLAC__bool ogg_decoder_aspect_allocation_failure;
175
#endif
176
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
177
  uint32_t fuzzing_rewind_count; /* To stop excessive rewinding, as it causes timeouts */
178
#endif
179
} FLAC__StreamDecoderPrivate;
180
181
/***********************************************************************
182
 *
183
 * Public static class data
184
 *
185
 ***********************************************************************/
186
187
FLAC_API const char * const FLAC__StreamDecoderStateString[] = {
188
  "FLAC__STREAM_DECODER_SEARCH_FOR_METADATA",
189
  "FLAC__STREAM_DECODER_READ_METADATA",
190
  "FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC",
191
  "FLAC__STREAM_DECODER_READ_FRAME",
192
  "FLAC__STREAM_DECODER_END_OF_STREAM",
193
  "FLAC__STREAM_DECODER_OGG_ERROR",
194
  "FLAC__STREAM_DECODER_SEEK_ERROR",
195
  "FLAC__STREAM_DECODER_ABORTED",
196
  "FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR",
197
  "FLAC__STREAM_DECODER_UNINITIALIZED",
198
  "FLAC__STREAM_DECODER_END_OF_LINK"
199
};
200
201
FLAC_API const char * const FLAC__StreamDecoderInitStatusString[] = {
202
  "FLAC__STREAM_DECODER_INIT_STATUS_OK",
203
  "FLAC__STREAM_DECODER_INIT_STATUS_UNSUPPORTED_CONTAINER",
204
  "FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS",
205
  "FLAC__STREAM_DECODER_INIT_STATUS_MEMORY_ALLOCATION_ERROR",
206
  "FLAC__STREAM_DECODER_INIT_STATUS_ERROR_OPENING_FILE",
207
  "FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED"
208
};
209
210
FLAC_API const char * const FLAC__StreamDecoderReadStatusString[] = {
211
  "FLAC__STREAM_DECODER_READ_STATUS_CONTINUE",
212
  "FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM",
213
  "FLAC__STREAM_DECODER_READ_STATUS_ABORT",
214
  "FLAC__STREAM_DECODER_READ_STATUS_END_OF_LINK"
215
};
216
217
FLAC_API const char * const FLAC__StreamDecoderSeekStatusString[] = {
218
  "FLAC__STREAM_DECODER_SEEK_STATUS_OK",
219
  "FLAC__STREAM_DECODER_SEEK_STATUS_ERROR",
220
  "FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED"
221
};
222
223
FLAC_API const char * const FLAC__StreamDecoderTellStatusString[] = {
224
  "FLAC__STREAM_DECODER_TELL_STATUS_OK",
225
  "FLAC__STREAM_DECODER_TELL_STATUS_ERROR",
226
  "FLAC__STREAM_DECODER_TELL_STATUS_UNSUPPORTED"
227
};
228
229
FLAC_API const char * const FLAC__StreamDecoderLengthStatusString[] = {
230
  "FLAC__STREAM_DECODER_LENGTH_STATUS_OK",
231
  "FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR",
232
  "FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED"
233
};
234
235
FLAC_API const char * const FLAC__StreamDecoderWriteStatusString[] = {
236
  "FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE",
237
  "FLAC__STREAM_DECODER_WRITE_STATUS_ABORT"
238
};
239
240
FLAC_API const char * const FLAC__StreamDecoderErrorStatusString[] = {
241
  "FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC",
242
  "FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER",
243
  "FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH",
244
  "FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM",
245
  "FLAC__STREAM_DECODER_ERROR_STATUS_BAD_METADATA",
246
  "FLAC__STREAM_DECODER_ERROR_STATUS_OUT_OF_BOUNDS",
247
  "FLAC__STREAM_DECODER_ERROR_STATUS_MISSING_FRAME"
248
};
249
250
/***********************************************************************
251
 *
252
 * Class constructor/destructor
253
 *
254
 ***********************************************************************/
255
FLAC_API FLAC__StreamDecoder *FLAC__stream_decoder_new(void)
256
8.66k
{
257
8.66k
  FLAC__StreamDecoder *decoder;
258
8.66k
  uint32_t i;
259
260
8.66k
  FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
261
262
8.66k
  decoder = safe_calloc_(1, sizeof(FLAC__StreamDecoder));
263
8.66k
  if(decoder == 0) {
264
0
    return 0;
265
0
  }
266
267
8.66k
  decoder->protected_ = safe_calloc_(1, sizeof(FLAC__StreamDecoderProtected));
268
8.66k
  if(decoder->protected_ == 0) {
269
0
    free(decoder);
270
0
    return 0;
271
0
  }
272
273
8.66k
  decoder->private_ = safe_calloc_(1, sizeof(FLAC__StreamDecoderPrivate));
274
8.66k
  if(decoder->private_ == 0) {
275
0
    free(decoder->protected_);
276
0
    free(decoder);
277
0
    return 0;
278
0
  }
279
280
8.66k
  decoder->private_->input = FLAC__bitreader_new();
281
8.66k
  if(decoder->private_->input == 0) {
282
0
    free(decoder->private_);
283
0
    free(decoder->protected_);
284
0
    free(decoder);
285
0
    return 0;
286
0
  }
287
288
8.66k
  decoder->private_->metadata_filter_ids_capacity = 16;
289
8.66k
  if(0 == (decoder->private_->metadata_filter_ids = malloc((FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8) * decoder->private_->metadata_filter_ids_capacity))) {
290
0
    FLAC__bitreader_delete(decoder->private_->input);
291
0
    free(decoder->private_);
292
0
    free(decoder->protected_);
293
0
    free(decoder);
294
0
    return 0;
295
0
  }
296
297
77.9k
  for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
298
69.3k
    decoder->private_->output[i] = 0;
299
69.3k
    decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0;
300
69.3k
  }
301
302
8.66k
  decoder->private_->side_subframe = 0;
303
304
8.66k
  decoder->private_->output_capacity = 0;
305
8.66k
  decoder->private_->output_channels = 0;
306
8.66k
  decoder->private_->has_seek_table = false;
307
308
77.9k
  for(i = 0; i < FLAC__MAX_CHANNELS; i++)
309
69.3k
    FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&decoder->private_->partitioned_rice_contents[i]);
310
311
8.66k
  decoder->private_->file = 0;
312
313
8.66k
  set_defaults_(decoder);
314
315
8.66k
  decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
316
317
8.66k
  return decoder;
318
8.66k
}
319
320
FLAC_API void FLAC__stream_decoder_delete(FLAC__StreamDecoder *decoder)
321
8.66k
{
322
8.66k
  uint32_t i;
323
324
8.66k
  if (decoder == NULL)
325
0
    return ;
326
327
8.66k
  FLAC__ASSERT(0 != decoder->protected_);
328
8.66k
  FLAC__ASSERT(0 != decoder->private_);
329
8.66k
  FLAC__ASSERT(0 != decoder->private_->input);
330
331
8.66k
  (void)FLAC__stream_decoder_finish(decoder);
332
333
8.66k
  if(0 != decoder->private_->metadata_filter_ids)
334
8.66k
    free(decoder->private_->metadata_filter_ids);
335
336
8.66k
  FLAC__bitreader_delete(decoder->private_->input);
337
338
77.9k
  for(i = 0; i < FLAC__MAX_CHANNELS; i++)
339
69.3k
    FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&decoder->private_->partitioned_rice_contents[i]);
340
341
8.66k
  free(decoder->private_);
342
8.66k
  free(decoder->protected_);
343
8.66k
  free(decoder);
344
8.66k
}
345
346
/***********************************************************************
347
 *
348
 * Public class methods
349
 *
350
 ***********************************************************************/
351
352
static FLAC__StreamDecoderInitStatus init_stream_internal_(
353
  FLAC__StreamDecoder *decoder,
354
  FLAC__StreamDecoderReadCallback read_callback,
355
  FLAC__StreamDecoderSeekCallback seek_callback,
356
  FLAC__StreamDecoderTellCallback tell_callback,
357
  FLAC__StreamDecoderLengthCallback length_callback,
358
  FLAC__StreamDecoderEofCallback eof_callback,
359
  FLAC__StreamDecoderWriteCallback write_callback,
360
  FLAC__StreamDecoderMetadataCallback metadata_callback,
361
  FLAC__StreamDecoderErrorCallback error_callback,
362
  void *client_data,
363
  FLAC__bool is_ogg
364
)
365
8.48k
{
366
8.48k
  FLAC__ASSERT(0 != decoder);
367
368
8.48k
  if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
369
0
    return FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED;
370
371
8.48k
  if(FLAC__HAS_OGG == 0 && is_ogg)
372
0
    return FLAC__STREAM_DECODER_INIT_STATUS_UNSUPPORTED_CONTAINER;
373
374
8.48k
  if(
375
8.48k
    0 == read_callback ||
376
8.48k
    0 == write_callback ||
377
8.48k
    0 == error_callback ||
378
8.48k
    (seek_callback && (0 == tell_callback || 0 == length_callback || 0 == eof_callback))
379
8.48k
  )
380
0
    return FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS;
381
382
8.48k
#if FLAC__HAS_OGG
383
8.48k
  decoder->private_->is_ogg = is_ogg;
384
8.48k
  if(is_ogg && !FLAC__ogg_decoder_aspect_init(&decoder->protected_->ogg_decoder_aspect)) {
385
0
    FLAC__ogg_decoder_aspect_finish(&decoder->protected_->ogg_decoder_aspect);
386
0
    return decoder->protected_->initstate = FLAC__STREAM_DECODER_INIT_STATUS_ERROR_OPENING_FILE;
387
0
  }
388
8.48k
#endif
389
390
8.48k
  FLAC__cpu_info(&decoder->private_->cpuinfo);
391
8.48k
  decoder->private_->local_bitreader_read_rice_signed_block = FLAC__bitreader_read_rice_signed_block;
392
393
8.48k
#ifdef FLAC__BMI2_SUPPORTED
394
8.48k
  if (decoder->private_->cpuinfo.x86.bmi2) {
395
8.48k
    decoder->private_->local_bitreader_read_rice_signed_block = FLAC__bitreader_read_rice_signed_block_bmi2;
396
8.48k
  }
397
8.48k
#endif
398
399
  /* from here on, errors are fatal */
400
401
8.48k
  if(!FLAC__bitreader_init(decoder->private_->input, read_callback_, decoder)) {
402
0
    decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
403
0
    return FLAC__STREAM_DECODER_INIT_STATUS_MEMORY_ALLOCATION_ERROR;
404
0
  }
405
406
8.48k
  decoder->private_->read_callback = read_callback;
407
8.48k
  decoder->private_->seek_callback = seek_callback;
408
8.48k
  decoder->private_->tell_callback = tell_callback;
409
8.48k
  decoder->private_->length_callback = length_callback;
410
8.48k
  decoder->private_->eof_callback = eof_callback;
411
8.48k
  decoder->private_->write_callback = write_callback;
412
8.48k
  decoder->private_->metadata_callback = metadata_callback;
413
8.48k
  decoder->private_->error_callback = error_callback;
414
8.48k
  decoder->private_->client_data = client_data;
415
8.48k
  decoder->private_->fixed_block_size = decoder->private_->next_fixed_block_size = 0;
416
8.48k
  decoder->private_->samples_decoded = 0;
417
8.48k
  decoder->private_->has_stream_info = false;
418
8.48k
  decoder->private_->cached = false;
419
420
8.48k
  decoder->private_->do_md5_checking = decoder->protected_->md5_checking;
421
8.48k
  decoder->private_->is_seeking = false;
422
8.48k
  decoder->private_->is_indexing = false;
423
424
8.48k
  decoder->private_->internal_reset_hack = true; /* so the following reset does not try to rewind the input */
425
8.48k
  if(!FLAC__stream_decoder_reset(decoder)) {
426
    /* above call sets the state for us */
427
0
    return FLAC__STREAM_DECODER_INIT_STATUS_MEMORY_ALLOCATION_ERROR;
428
0
  }
429
430
8.48k
  return FLAC__STREAM_DECODER_INIT_STATUS_OK;
431
8.48k
}
432
433
FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_stream(
434
  FLAC__StreamDecoder *decoder,
435
  FLAC__StreamDecoderReadCallback read_callback,
436
  FLAC__StreamDecoderSeekCallback seek_callback,
437
  FLAC__StreamDecoderTellCallback tell_callback,
438
  FLAC__StreamDecoderLengthCallback length_callback,
439
  FLAC__StreamDecoderEofCallback eof_callback,
440
  FLAC__StreamDecoderWriteCallback write_callback,
441
  FLAC__StreamDecoderMetadataCallback metadata_callback,
442
  FLAC__StreamDecoderErrorCallback error_callback,
443
  void *client_data
444
)
445
7.41k
{
446
7.41k
  return init_stream_internal_(
447
7.41k
    decoder,
448
7.41k
    read_callback,
449
7.41k
    seek_callback,
450
7.41k
    tell_callback,
451
7.41k
    length_callback,
452
7.41k
    eof_callback,
453
7.41k
    write_callback,
454
7.41k
    metadata_callback,
455
7.41k
    error_callback,
456
7.41k
    client_data,
457
    /*is_ogg=*/false
458
7.41k
  );
459
7.41k
}
460
461
FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_ogg_stream(
462
  FLAC__StreamDecoder *decoder,
463
  FLAC__StreamDecoderReadCallback read_callback,
464
  FLAC__StreamDecoderSeekCallback seek_callback,
465
  FLAC__StreamDecoderTellCallback tell_callback,
466
  FLAC__StreamDecoderLengthCallback length_callback,
467
  FLAC__StreamDecoderEofCallback eof_callback,
468
  FLAC__StreamDecoderWriteCallback write_callback,
469
  FLAC__StreamDecoderMetadataCallback metadata_callback,
470
  FLAC__StreamDecoderErrorCallback error_callback,
471
  void *client_data
472
)
473
1.07k
{
474
1.07k
  return init_stream_internal_(
475
1.07k
    decoder,
476
1.07k
    read_callback,
477
1.07k
    seek_callback,
478
1.07k
    tell_callback,
479
1.07k
    length_callback,
480
1.07k
    eof_callback,
481
1.07k
    write_callback,
482
1.07k
    metadata_callback,
483
1.07k
    error_callback,
484
1.07k
    client_data,
485
    /*is_ogg=*/true
486
1.07k
  );
487
1.07k
}
488
489
static FLAC__StreamDecoderInitStatus init_FILE_internal_(
490
  FLAC__StreamDecoder *decoder,
491
  FILE *file,
492
  FLAC__StreamDecoderWriteCallback write_callback,
493
  FLAC__StreamDecoderMetadataCallback metadata_callback,
494
  FLAC__StreamDecoderErrorCallback error_callback,
495
  void *client_data,
496
  FLAC__bool is_ogg
497
)
498
0
{
499
0
  FLAC__ASSERT(0 != decoder);
500
0
  FLAC__ASSERT(0 != file);
501
502
0
  if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
503
0
    return decoder->protected_->initstate = FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED;
504
505
0
  if(0 == write_callback || 0 == error_callback)
506
0
    return decoder->protected_->initstate = FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS;
507
508
  /*
509
   * To make sure that our file does not go unclosed after an error, we
510
   * must assign the FILE pointer before any further error can occur in
511
   * this routine.
512
   */
513
0
  if(file == stdin)
514
0
    file = get_binary_stdin_(); /* just to be safe */
515
516
0
  decoder->private_->file = file;
517
518
0
  return init_stream_internal_(
519
0
    decoder,
520
0
    file_read_callback_,
521
0
    decoder->private_->file == stdin? 0: file_seek_callback_,
522
0
    decoder->private_->file == stdin? 0: file_tell_callback_,
523
0
    decoder->private_->file == stdin? 0: file_length_callback_,
524
0
    file_eof_callback_,
525
0
    write_callback,
526
0
    metadata_callback,
527
0
    error_callback,
528
0
    client_data,
529
0
    is_ogg
530
0
  );
531
0
}
532
533
FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_FILE(
534
  FLAC__StreamDecoder *decoder,
535
  FILE *file,
536
  FLAC__StreamDecoderWriteCallback write_callback,
537
  FLAC__StreamDecoderMetadataCallback metadata_callback,
538
  FLAC__StreamDecoderErrorCallback error_callback,
539
  void *client_data
540
)
541
0
{
542
0
  return init_FILE_internal_(decoder, file, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/false);
543
0
}
544
545
FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_ogg_FILE(
546
  FLAC__StreamDecoder *decoder,
547
  FILE *file,
548
  FLAC__StreamDecoderWriteCallback write_callback,
549
  FLAC__StreamDecoderMetadataCallback metadata_callback,
550
  FLAC__StreamDecoderErrorCallback error_callback,
551
  void *client_data
552
)
553
0
{
554
0
  return init_FILE_internal_(decoder, file, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/true);
555
0
}
556
557
static FLAC__StreamDecoderInitStatus init_file_internal_(
558
  FLAC__StreamDecoder *decoder,
559
  const char *filename,
560
  FLAC__StreamDecoderWriteCallback write_callback,
561
  FLAC__StreamDecoderMetadataCallback metadata_callback,
562
  FLAC__StreamDecoderErrorCallback error_callback,
563
  void *client_data,
564
  FLAC__bool is_ogg
565
)
566
0
{
567
0
  FILE *file;
568
569
0
  FLAC__ASSERT(0 != decoder);
570
571
  /*
572
   * To make sure that our file does not go unclosed after an error, we
573
   * have to do the same entrance checks here that are later performed
574
   * in FLAC__stream_decoder_init_FILE() before the FILE* is assigned.
575
   */
576
0
  if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
577
0
    return decoder->protected_->initstate = FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED;
578
579
0
  if(0 == write_callback || 0 == error_callback)
580
0
    return decoder->protected_->initstate = FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS;
581
582
0
  file = filename? flac_fopen(filename, "rb") : stdin;
583
584
0
  if(0 == file)
585
0
    return FLAC__STREAM_DECODER_INIT_STATUS_ERROR_OPENING_FILE;
586
587
0
  return init_FILE_internal_(decoder, file, write_callback, metadata_callback, error_callback, client_data, is_ogg);
588
0
}
589
590
FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_file(
591
  FLAC__StreamDecoder *decoder,
592
  const char *filename,
593
  FLAC__StreamDecoderWriteCallback write_callback,
594
  FLAC__StreamDecoderMetadataCallback metadata_callback,
595
  FLAC__StreamDecoderErrorCallback error_callback,
596
  void *client_data
597
)
598
0
{
599
0
  return init_file_internal_(decoder, filename, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/false);
600
0
}
601
602
FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_ogg_file(
603
  FLAC__StreamDecoder *decoder,
604
  const char *filename,
605
  FLAC__StreamDecoderWriteCallback write_callback,
606
  FLAC__StreamDecoderMetadataCallback metadata_callback,
607
  FLAC__StreamDecoderErrorCallback error_callback,
608
  void *client_data
609
)
610
0
{
611
0
  return init_file_internal_(decoder, filename, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/true);
612
0
}
613
614
FLAC_API FLAC__bool FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder)
615
25.9k
{
616
25.9k
  FLAC__bool md5_failed = false;
617
25.9k
  uint32_t i;
618
619
25.9k
  FLAC__ASSERT(0 != decoder);
620
25.9k
  FLAC__ASSERT(0 != decoder->private_);
621
25.9k
  FLAC__ASSERT(0 != decoder->protected_);
622
623
25.9k
  if(decoder->protected_->state == FLAC__STREAM_DECODER_UNINITIALIZED)
624
17.5k
    return true;
625
626
  /* see the comment in FLAC__stream_decoder_reset() as to why we
627
   * always call FLAC__MD5Final()
628
   */
629
8.48k
  FLAC__MD5Final(decoder->private_->computed_md5sum, &decoder->private_->md5context);
630
631
8.48k
  free(decoder->private_->seek_table.data.seek_table.points);
632
8.48k
  decoder->private_->seek_table.data.seek_table.points = 0;
633
8.48k
  decoder->private_->has_seek_table = false;
634
635
8.48k
  FLAC__bitreader_free(decoder->private_->input);
636
76.3k
  for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
637
    /* WATCHOUT:
638
     * FLAC__lpc_restore_signal_asm_ia32_mmx() and ..._intrin_sseN()
639
     * require that the output arrays have a buffer of up to 3 zeroes
640
     * in front (at negative indices) for alignment purposes;
641
     * we use 4 to keep the data well-aligned.
642
     */
643
67.9k
    if(0 != decoder->private_->output[i]) {
644
16.4k
      free(decoder->private_->output[i]-4);
645
16.4k
      decoder->private_->output[i] = 0;
646
16.4k
    }
647
67.9k
    if(0 != decoder->private_->residual_unaligned[i]) {
648
16.4k
      free(decoder->private_->residual_unaligned[i]);
649
16.4k
      decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0;
650
16.4k
    }
651
67.9k
  }
652
8.48k
  if(0 != decoder->private_->side_subframe) {
653
1.57k
    free(decoder->private_->side_subframe);
654
1.57k
    decoder->private_->side_subframe = 0;
655
1.57k
  }
656
8.48k
  decoder->private_->output_capacity = 0;
657
8.48k
  decoder->private_->output_channels = 0;
658
659
8.48k
#if FLAC__HAS_OGG
660
8.48k
  if(decoder->private_->is_ogg)
661
1.07k
    FLAC__ogg_decoder_aspect_finish(&decoder->protected_->ogg_decoder_aspect);
662
8.48k
#endif
663
664
8.48k
  if(0 != decoder->private_->file) {
665
0
    if(decoder->private_->file != stdin)
666
0
      fclose(decoder->private_->file);
667
0
    decoder->private_->file = 0;
668
0
  }
669
670
8.48k
  if(decoder->private_->do_md5_checking) {
671
1.16k
    if(memcmp(decoder->private_->stream_info.data.stream_info.md5sum, decoder->private_->computed_md5sum, 16))
672
1.15k
      md5_failed = true;
673
1.16k
  }
674
8.48k
  decoder->private_->is_seeking = false;
675
8.48k
  decoder->private_->is_indexing = false;
676
677
8.48k
  set_defaults_(decoder);
678
679
8.48k
  decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
680
681
8.48k
  return !md5_failed;
682
25.9k
}
683
684
FLAC_API FLAC__bool FLAC__stream_decoder_set_ogg_serial_number(FLAC__StreamDecoder *decoder, long value)
685
38
{
686
38
  FLAC__ASSERT(0 != decoder);
687
38
  FLAC__ASSERT(0 != decoder->private_);
688
38
  FLAC__ASSERT(0 != decoder->protected_);
689
38
  if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
690
0
    return false;
691
38
#if FLAC__HAS_OGG
692
  /* can't check decoder->private_->is_ogg since that's not set until init time */
693
38
  FLAC__ogg_decoder_aspect_set_serial_number(&decoder->protected_->ogg_decoder_aspect, value);
694
38
  return true;
695
#else
696
  (void)value;
697
  return false;
698
#endif
699
38
}
700
701
FLAC_API FLAC__bool FLAC__stream_decoder_set_decode_chained_stream(FLAC__StreamDecoder* decoder, FLAC__bool value)
702
8.61k
{
703
8.61k
  FLAC__ASSERT(0 != decoder);
704
8.61k
  FLAC__ASSERT(0 != decoder->protected_);
705
8.61k
  if (decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
706
0
    return false;
707
8.61k
#if FLAC__HAS_OGG
708
8.61k
  FLAC__ogg_decoder_aspect_set_decode_chained_stream(&decoder->protected_->ogg_decoder_aspect, value);
709
8.61k
  return true;
710
#else
711
  (void)value;
712
  return false;
713
#endif
714
8.61k
}
715
716
FLAC_API FLAC__bool FLAC__stream_decoder_set_md5_checking(FLAC__StreamDecoder *decoder, FLAC__bool value)
717
1.36k
{
718
1.36k
  FLAC__ASSERT(0 != decoder);
719
1.36k
  FLAC__ASSERT(0 != decoder->protected_);
720
1.36k
  if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
721
0
    return false;
722
1.36k
  decoder->protected_->md5_checking = value;
723
1.36k
  return true;
724
1.36k
}
725
726
FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
727
116
{
728
116
  FLAC__ASSERT(0 != decoder);
729
116
  FLAC__ASSERT(0 != decoder->private_);
730
116
  FLAC__ASSERT(0 != decoder->protected_);
731
116
  FLAC__ASSERT((uint32_t)type <= FLAC__MAX_METADATA_TYPE_CODE);
732
  /* double protection */
733
116
  if((uint32_t)type > FLAC__MAX_METADATA_TYPE_CODE)
734
0
    return false;
735
116
  decoder->private_->metadata_filter[type] = true;
736
116
  if(type == FLAC__METADATA_TYPE_APPLICATION)
737
5
    decoder->private_->metadata_filter_ids_count = 0;
738
116
  return true;
739
116
}
740
741
FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
742
19
{
743
19
  FLAC__ASSERT(0 != decoder);
744
19
  FLAC__ASSERT(0 != decoder->private_);
745
19
  FLAC__ASSERT(0 != decoder->protected_);
746
19
  FLAC__ASSERT(0 != id);
747
748
19
  if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION])
749
1
    return true;
750
751
18
  FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);
752
753
18
  if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) {
754
0
    if(0 == (decoder->private_->metadata_filter_ids = safe_realloc_mul_2op_(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity, /*times*/2))) {
755
0
      decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
756
0
      return false;
757
0
    }
758
0
    decoder->private_->metadata_filter_ids_capacity *= 2;
759
0
  }
760
761
18
  memcpy(decoder->private_->metadata_filter_ids + decoder->private_->metadata_filter_ids_count * (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), id, (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8));
762
18
  decoder->private_->metadata_filter_ids_count++;
763
764
18
  return true;
765
18
}
766
767
FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_all(FLAC__StreamDecoder *decoder)
768
4.68k
{
769
4.68k
  uint32_t i;
770
4.68k
  FLAC__ASSERT(0 != decoder);
771
4.68k
  FLAC__ASSERT(0 != decoder->private_);
772
4.68k
  FLAC__ASSERT(0 != decoder->protected_);
773
604k
  for(i = 0; i < sizeof(decoder->private_->metadata_filter) / sizeof(decoder->private_->metadata_filter[0]); i++)
774
599k
    decoder->private_->metadata_filter[i] = true;
775
4.68k
  decoder->private_->metadata_filter_ids_count = 0;
776
4.68k
  return true;
777
4.68k
}
778
779
FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
780
147
{
781
147
  FLAC__ASSERT(0 != decoder);
782
147
  FLAC__ASSERT(0 != decoder->private_);
783
147
  FLAC__ASSERT(0 != decoder->protected_);
784
147
  FLAC__ASSERT((uint32_t)type <= FLAC__MAX_METADATA_TYPE_CODE);
785
  /* double protection */
786
147
  if((uint32_t)type > FLAC__MAX_METADATA_TYPE_CODE)
787
0
    return false;
788
147
  decoder->private_->metadata_filter[type] = false;
789
147
  if(type == FLAC__METADATA_TYPE_APPLICATION)
790
2
    decoder->private_->metadata_filter_ids_count = 0;
791
147
  return true;
792
147
}
793
794
FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
795
62
{
796
62
  FLAC__ASSERT(0 != decoder);
797
62
  FLAC__ASSERT(0 != decoder->private_);
798
62
  FLAC__ASSERT(0 != decoder->protected_);
799
62
  FLAC__ASSERT(0 != id);
800
801
62
  if(!decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION])
802
10
    return true;
803
804
52
  FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);
805
806
52
  if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) {
807
0
    if(0 == (decoder->private_->metadata_filter_ids = safe_realloc_mul_2op_(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity, /*times*/2))) {
808
0
      decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
809
0
      return false;
810
0
    }
811
0
    decoder->private_->metadata_filter_ids_capacity *= 2;
812
0
  }
813
814
52
  memcpy(decoder->private_->metadata_filter_ids + decoder->private_->metadata_filter_ids_count * (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), id, (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8));
815
52
  decoder->private_->metadata_filter_ids_count++;
816
817
52
  return true;
818
52
}
819
820
FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_all(FLAC__StreamDecoder *decoder)
821
3.02k
{
822
3.02k
  FLAC__ASSERT(0 != decoder);
823
3.02k
  FLAC__ASSERT(0 != decoder->private_);
824
3.02k
  FLAC__ASSERT(0 != decoder->protected_);
825
3.02k
  memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter));
826
3.02k
  decoder->private_->metadata_filter_ids_count = 0;
827
3.02k
  return true;
828
3.02k
}
829
830
FLAC_API FLAC__StreamDecoderState FLAC__stream_decoder_get_state(const FLAC__StreamDecoder *decoder)
831
0
{
832
0
  FLAC__ASSERT(0 != decoder);
833
0
  FLAC__ASSERT(0 != decoder->protected_);
834
0
  return decoder->protected_->state;
835
0
}
836
837
FLAC_API const char *FLAC__stream_decoder_get_resolved_state_string(const FLAC__StreamDecoder *decoder)
838
0
{
839
0
  return FLAC__StreamDecoderStateString[decoder->protected_->state];
840
0
}
841
842
FLAC_API FLAC__bool FLAC__stream_decoder_get_decode_chained_stream(const FLAC__StreamDecoder* decoder)
843
1.67k
{
844
1.67k
  FLAC__ASSERT(0 != decoder);
845
1.67k
  FLAC__ASSERT(0 != decoder->protected_);
846
1.67k
#if FLAC__HAS_OGG
847
1.67k
  return FLAC__ogg_decoder_aspect_get_decode_chained_stream(&decoder->protected_->ogg_decoder_aspect);
848
#else
849
  (void)decoder;
850
  return false;
851
#endif
852
1.67k
}
853
854
FLAC_API FLAC__bool FLAC__stream_decoder_get_md5_checking(const FLAC__StreamDecoder *decoder)
855
231
{
856
231
  FLAC__ASSERT(0 != decoder);
857
231
  FLAC__ASSERT(0 != decoder->protected_);
858
231
  return decoder->protected_->md5_checking;
859
231
}
860
861
FLAC_API FLAC__uint64 FLAC__stream_decoder_get_total_samples(const FLAC__StreamDecoder *decoder)
862
1.34k
{
863
1.34k
  FLAC__ASSERT(0 != decoder);
864
1.34k
  FLAC__ASSERT(0 != decoder->protected_);
865
1.34k
  return decoder->private_->has_stream_info? decoder->private_->stream_info.data.stream_info.total_samples : 0;
866
1.34k
}
867
868
FLAC_API uint32_t FLAC__stream_decoder_get_channels(const FLAC__StreamDecoder *decoder)
869
75.3k
{
870
75.3k
  FLAC__ASSERT(0 != decoder);
871
75.3k
  FLAC__ASSERT(0 != decoder->protected_);
872
75.3k
  return decoder->protected_->channels;
873
75.3k
}
874
875
FLAC_API FLAC__ChannelAssignment FLAC__stream_decoder_get_channel_assignment(const FLAC__StreamDecoder *decoder)
876
0
{
877
0
  FLAC__ASSERT(0 != decoder);
878
0
  FLAC__ASSERT(0 != decoder->protected_);
879
0
  return decoder->protected_->channel_assignment;
880
0
}
881
882
FLAC_API uint32_t FLAC__stream_decoder_get_bits_per_sample(const FLAC__StreamDecoder *decoder)
883
2.07k
{
884
2.07k
  FLAC__ASSERT(0 != decoder);
885
2.07k
  FLAC__ASSERT(0 != decoder->protected_);
886
2.07k
  return decoder->protected_->bits_per_sample;
887
2.07k
}
888
889
FLAC_API uint32_t FLAC__stream_decoder_get_sample_rate(const FLAC__StreamDecoder *decoder)
890
791
{
891
791
  FLAC__ASSERT(0 != decoder);
892
791
  FLAC__ASSERT(0 != decoder->protected_);
893
791
  return decoder->protected_->sample_rate;
894
791
}
895
896
FLAC_API uint32_t FLAC__stream_decoder_get_blocksize(const FLAC__StreamDecoder *decoder)
897
244
{
898
244
  FLAC__ASSERT(0 != decoder);
899
244
  FLAC__ASSERT(0 != decoder->protected_);
900
244
  return decoder->protected_->blocksize;
901
244
}
902
903
FLAC_API FLAC__bool FLAC__stream_decoder_get_decode_position(const FLAC__StreamDecoder *decoder, FLAC__uint64 *position)
904
120k
{
905
120k
  FLAC__ASSERT(0 != decoder);
906
120k
  FLAC__ASSERT(0 != decoder->private_);
907
120k
  FLAC__ASSERT(0 != position);
908
909
120k
  if(FLAC__HAS_OGG && decoder->private_->is_ogg)
910
22.4k
    return false;
911
912
98.2k
  if(0 == decoder->private_->tell_callback)
913
0
    return false;
914
98.2k
  if(decoder->private_->tell_callback(decoder, position, decoder->private_->client_data) != FLAC__STREAM_DECODER_TELL_STATUS_OK)
915
98.2k
    return false;
916
  /* should never happen since all FLAC frames and metadata blocks are byte aligned, but check just in case */
917
0
  if(!FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input))
918
0
    return false;
919
0
  FLAC__ASSERT(*position >= FLAC__stream_decoder_get_input_bytes_unconsumed(decoder));
920
0
  *position -= FLAC__stream_decoder_get_input_bytes_unconsumed(decoder);
921
0
  return true;
922
0
}
923
924
FLAC_API const void *FLAC__stream_decoder_get_client_data(FLAC__StreamDecoder *decoder)
925
0
{
926
0
  return decoder->private_->client_data;
927
0
}
928
929
FLAC_API FLAC__bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder)
930
148k
{
931
148k
  FLAC__ASSERT(0 != decoder);
932
148k
  FLAC__ASSERT(0 != decoder->private_);
933
148k
  FLAC__ASSERT(0 != decoder->protected_);
934
935
148k
  if(!decoder->private_->internal_reset_hack && decoder->protected_->state == FLAC__STREAM_DECODER_UNINITIALIZED)
936
0
    return false;
937
148k
  if(decoder->protected_->state == FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR)
938
216
    return false;
939
940
148k
  decoder->private_->samples_decoded = 0;
941
148k
  decoder->private_->do_md5_checking = false;
942
148k
  decoder->private_->last_seen_framesync = 0;
943
148k
  decoder->private_->last_frame_is_set = false;
944
945
148k
#if FLAC__HAS_OGG
946
148k
  if(decoder->private_->is_ogg)
947
11.1k
    FLAC__ogg_decoder_aspect_flush(&decoder->protected_->ogg_decoder_aspect);
948
148k
#endif
949
950
148k
  if(!FLAC__bitreader_clear(decoder->private_->input)) {
951
0
    decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
952
0
    return false;
953
0
  }
954
148k
  decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
955
956
148k
  return true;
957
148k
}
958
959
134k
void reset_decoder_internal_(FLAC__StreamDecoder* decoder) {
960
  /* This code resets only the FLAC parser and decoder, not the Ogg part,
961
   * nor the input buffering etc. This is used to prepare the FLAC
962
   * decoder to receive another Ogg chain link */
963
134k
  decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
964
965
134k
  decoder->private_->has_stream_info = false;
966
967
134k
  free(decoder->private_->seek_table.data.seek_table.points);
968
134k
  decoder->private_->seek_table.data.seek_table.points = 0;
969
134k
  decoder->private_->has_seek_table = false;
970
971
134k
  decoder->private_->do_md5_checking = decoder->protected_->md5_checking;
972
  /*
973
   * This goes in reset() and not flush() because according to the spec, a
974
   * fixed-blocksize stream must stay that way through the whole stream.
975
   */
976
134k
  decoder->private_->fixed_block_size = decoder->private_->next_fixed_block_size = 0;
977
978
  /* We initialize the FLAC__MD5Context even though we may never use it.  This
979
   * is because md5 checking may be turned on to start and then turned off if
980
   * a seek occurs.  So we init the context here and finalize it in
981
   * FLAC__stream_decoder_finish() to make sure things are always cleaned up
982
   * properly.
983
   */
984
134k
  if(!decoder->private_->internal_reset_hack) {
985
    /* Only finish MD5 context when it has been initialized
986
     * (i.e. when internal_reset_hack is not set) */
987
125k
    FLAC__MD5Final(decoder->private_->computed_md5sum, &decoder->private_->md5context);
988
125k
  }
989
8.48k
  else
990
8.48k
    decoder->private_->internal_reset_hack = false;
991
134k
  FLAC__MD5Init(&decoder->private_->md5context);
992
993
134k
  decoder->private_->first_frame_offset = 0;
994
134k
  decoder->private_->unparseable_frame_count = 0;
995
134k
  decoder->private_->last_seen_framesync = 0;
996
134k
  decoder->private_->last_frame_is_set = false;
997
134k
  decoder->private_->error_has_been_sent = false;
998
134k
}
999
1000
FLAC_API FLAC__bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder)
1001
134k
{
1002
134k
  FLAC__ASSERT(0 != decoder);
1003
134k
  FLAC__ASSERT(0 != decoder->private_);
1004
134k
  FLAC__ASSERT(0 != decoder->protected_);
1005
1006
134k
  if(!FLAC__stream_decoder_flush(decoder)) {
1007
    /* above call sets the state for us */
1008
216
    return false;
1009
216
  }
1010
1011
134k
#if FLAC__HAS_OGG
1012
  /*@@@ could go in !internal_reset_hack block below */
1013
134k
  if(decoder->private_->is_ogg)
1014
7.83k
    FLAC__ogg_decoder_aspect_reset(&decoder->protected_->ogg_decoder_aspect);
1015
134k
#endif
1016
1017
  /* Rewind if necessary.  If FLAC__stream_decoder_init() is calling us,
1018
   * (internal_reset_hack) don't try to rewind since we are already at
1019
   * the beginning of the stream and don't want to fail if the input is
1020
   * not seekable.
1021
   */
1022
134k
  if(!decoder->private_->internal_reset_hack) {
1023
126k
    if(decoder->private_->file == stdin)
1024
0
      return false; /* can't rewind stdin, reset fails */
1025
126k
    if(decoder->private_->seek_callback && decoder->private_->seek_callback(decoder, 0, decoder->private_->client_data) == FLAC__STREAM_DECODER_SEEK_STATUS_ERROR)
1026
4.29k
      return false; /* seekable and seek fails, reset fails */
1027
126k
  }
1028
1029
130k
  reset_decoder_internal_(decoder);
1030
1031
130k
  return true;
1032
134k
}
1033
1034
FLAC_API FLAC__bool FLAC__stream_decoder_process_single(FLAC__StreamDecoder *decoder)
1035
17.0k
{
1036
17.0k
  FLAC__bool got_a_frame;
1037
17.0k
  FLAC__ASSERT(0 != decoder);
1038
17.0k
  FLAC__ASSERT(0 != decoder->protected_);
1039
1040
75.0k
  while(1) {
1041
75.0k
    switch(decoder->protected_->state) {
1042
9.37k
      case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
1043
9.37k
        if(!find_metadata_(decoder))
1044
2.89k
          return false; /* above function sets the status for us */
1045
6.47k
        break;
1046
6.47k
      case FLAC__STREAM_DECODER_READ_METADATA:
1047
1.43k
        if(!read_metadata_(decoder))
1048
599
          return false; /* above function sets the status for us */
1049
835
        else
1050
835
          return true;
1051
31.4k
      case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
1052
31.4k
        if(!frame_sync_(decoder)) {
1053
4.76k
          return true; /* above function sets the status for us */
1054
4.76k
        }
1055
26.6k
        break;
1056
31.8k
      case FLAC__STREAM_DECODER_READ_FRAME:
1057
31.8k
        if(!read_frame_(decoder, &got_a_frame, /*do_full_decode=*/true))
1058
6.70k
          return false; /* above function sets the status for us */
1059
25.1k
        if(got_a_frame)
1060
264
          return true; /* above function sets the status for us */
1061
24.8k
        break;
1062
24.8k
      case FLAC__STREAM_DECODER_END_OF_STREAM:
1063
66
      case FLAC__STREAM_DECODER_END_OF_LINK:
1064
754
      case FLAC__STREAM_DECODER_ABORTED:
1065
754
        return true;
1066
197
      default:
1067
197
        return false;
1068
75.0k
    }
1069
75.0k
  }
1070
17.0k
}
1071
1072
FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_metadata(FLAC__StreamDecoder *decoder)
1073
91.8k
{
1074
91.8k
  FLAC__ASSERT(0 != decoder);
1075
91.8k
  FLAC__ASSERT(0 != decoder->protected_);
1076
1077
117k
  while(1) {
1078
117k
    switch(decoder->protected_->state) {
1079
39.5k
      case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
1080
39.5k
        if(!find_metadata_(decoder))
1081
24.4k
          return false; /* above function sets the status for us */
1082
15.1k
        break;
1083
22.6k
      case FLAC__STREAM_DECODER_READ_METADATA:
1084
22.6k
        if(!read_metadata_(decoder))
1085
11.8k
          return false; /* above function sets the status for us */
1086
10.8k
        break;
1087
12.6k
      case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
1088
14.0k
      case FLAC__STREAM_DECODER_READ_FRAME:
1089
14.0k
      case FLAC__STREAM_DECODER_END_OF_STREAM:
1090
14.9k
      case FLAC__STREAM_DECODER_END_OF_LINK:
1091
54.8k
      case FLAC__STREAM_DECODER_ABORTED:
1092
54.8k
        return true;
1093
693
      default:
1094
693
        return false;
1095
117k
    }
1096
117k
  }
1097
91.8k
}
1098
1099
FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_link(FLAC__StreamDecoder *decoder)
1100
0
{
1101
0
  FLAC__bool dummy;
1102
0
  FLAC__ASSERT(0 != decoder);
1103
0
  FLAC__ASSERT(0 != decoder->protected_);
1104
1105
0
  while(1) {
1106
0
    switch(decoder->protected_->state) {
1107
0
      case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
1108
0
        if(!find_metadata_(decoder))
1109
0
          return false; /* above function sets the status for us */
1110
0
        break;
1111
0
      case FLAC__STREAM_DECODER_READ_METADATA:
1112
0
        if(!read_metadata_(decoder))
1113
0
          return false; /* above function sets the status for us */
1114
0
        break;
1115
0
      case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
1116
0
        if(!frame_sync_(decoder)) {
1117
0
          return true; /* above function sets the status for us */
1118
0
        }
1119
0
        break;
1120
0
      case FLAC__STREAM_DECODER_READ_FRAME:
1121
0
        if(!read_frame_(decoder, &dummy, /*do_full_decode=*/true))
1122
0
          return false; /* above function sets the status for us */
1123
0
        break;
1124
0
      case FLAC__STREAM_DECODER_END_OF_STREAM:
1125
0
      case FLAC__STREAM_DECODER_END_OF_LINK:
1126
0
      case FLAC__STREAM_DECODER_ABORTED:
1127
0
        return true;
1128
0
      default:
1129
0
        return false;
1130
0
    }
1131
0
  }
1132
0
}
1133
1134
FLAC_API FLAC__bool FLAC__stream_decoder_finish_link(FLAC__StreamDecoder *decoder)
1135
3.85k
{
1136
3.85k
  FLAC__bool md5_failed = false;
1137
1138
3.85k
  FLAC__ASSERT(0 != decoder);
1139
3.85k
  FLAC__ASSERT(0 != decoder->private_);
1140
3.85k
  FLAC__ASSERT(0 != decoder->protected_);
1141
1142
3.85k
  if(decoder->protected_->state != FLAC__STREAM_DECODER_END_OF_LINK) {
1143
0
    FLAC__ASSERT(0); /* This function should not be called in any other state */
1144
0
    return true;
1145
0
  }
1146
1147
3.85k
  FLAC__MD5Final(decoder->private_->computed_md5sum, &decoder->private_->md5context);
1148
1149
3.85k
  if(decoder->private_->do_md5_checking) {
1150
1.69k
    if(memcmp(decoder->private_->stream_info.data.stream_info.md5sum, decoder->private_->computed_md5sum, 16))
1151
1.36k
      md5_failed = true;
1152
1.69k
  }
1153
1154
3.85k
  reset_decoder_internal_(decoder);
1155
1156
3.85k
#if FLAC__HAS_OGG
1157
3.85k
  if(decoder->private_->is_ogg)
1158
3.85k
    FLAC__ogg_decoder_aspect_next_link(&decoder->protected_->ogg_decoder_aspect);
1159
3.85k
#endif
1160
1161
3.85k
  return !md5_failed;
1162
3.85k
}
1163
1164
FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_stream(FLAC__StreamDecoder *decoder)
1165
30.7k
{
1166
30.7k
  FLAC__bool dummy;
1167
30.7k
  FLAC__ASSERT(0 != decoder);
1168
30.7k
  FLAC__ASSERT(0 != decoder->protected_);
1169
1170
261k
  while(1) {
1171
261k
    switch(decoder->protected_->state) {
1172
32.0k
      case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
1173
32.0k
        if(!find_metadata_(decoder))
1174
1.88k
          return false; /* above function sets the status for us */
1175
30.1k
        break;
1176
38.9k
      case FLAC__STREAM_DECODER_READ_METADATA:
1177
38.9k
        if(!read_metadata_(decoder))
1178
21.8k
          return false; /* above function sets the status for us */
1179
17.0k
        break;
1180
94.2k
      case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
1181
94.2k
        if(!frame_sync_(decoder) && decoder->protected_->state != FLAC__STREAM_DECODER_END_OF_LINK && decoder->protected_->state != FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR) {
1182
4.49k
          return true; /* above function sets the status for us */
1183
4.49k
        }
1184
89.7k
        break;
1185
92.0k
      case FLAC__STREAM_DECODER_READ_FRAME:
1186
92.0k
        if(!read_frame_(decoder, &dummy, /*do_full_decode=*/true))
1187
2.04k
          return false; /* above function sets the status for us */
1188
90.0k
        break;
1189
90.0k
      case FLAC__STREAM_DECODER_END_OF_LINK:
1190
3.85k
        FLAC__stream_decoder_finish_link(decoder);
1191
3.85k
        break;
1192
0
      case FLAC__STREAM_DECODER_END_OF_STREAM:
1193
230
      case FLAC__STREAM_DECODER_ABORTED:
1194
230
        return true;
1195
195
      default:
1196
195
        return false;
1197
261k
    }
1198
261k
  }
1199
30.7k
}
1200
1201
FLAC_API FLAC__bool FLAC__stream_decoder_skip_single_frame(FLAC__StreamDecoder *decoder)
1202
2.07k
{
1203
2.07k
  FLAC__bool got_a_frame;
1204
2.07k
  FLAC__ASSERT(0 != decoder);
1205
2.07k
  FLAC__ASSERT(0 != decoder->protected_);
1206
1207
4.27k
  while(1) {
1208
4.27k
    switch(decoder->protected_->state) {
1209
405
      case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
1210
408
      case FLAC__STREAM_DECODER_READ_METADATA:
1211
408
        return false;
1212
1.75k
      case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
1213
1.75k
        if(!frame_sync_(decoder)) {
1214
357
          return true; /* above function sets the status for us */
1215
357
        }
1216
1.39k
        break;
1217
1.40k
      case FLAC__STREAM_DECODER_READ_FRAME:
1218
1.40k
        if(!read_frame_(decoder, &got_a_frame, /*do_full_decode=*/false))
1219
452
          return false; /* above function sets the status for us */
1220
956
        if(got_a_frame)
1221
157
          return true; /* above function sets the status for us */
1222
799
        break;
1223
799
      case FLAC__STREAM_DECODER_END_OF_STREAM:
1224
195
      case FLAC__STREAM_DECODER_END_OF_LINK:
1225
702
      case FLAC__STREAM_DECODER_ABORTED:
1226
702
        return true;
1227
0
      default:
1228
0
        return false;
1229
4.27k
    }
1230
4.27k
  }
1231
2.07k
}
1232
1233
FLAC_API FLAC__bool FLAC__stream_decoder_skip_single_link(FLAC__StreamDecoder *decoder)
1234
0
{
1235
0
#if FLAC__HAS_OGG
1236
0
  FLAC__OggDecoderAspectReadStatus status;
1237
0
  FLAC__ASSERT_DECLARATION(FLAC__uint32 linknumber_start);
1238
0
  FLAC__ASSERT(0 != decoder);
1239
0
  FLAC__ASSERT(0 != decoder->protected_);
1240
1241
0
  if(!decoder->private_->is_ogg ||
1242
0
     decoder->protected_->state == FLAC__STREAM_DECODER_ABORTED ||
1243
0
     decoder->protected_->state == FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR ||
1244
0
     decoder->protected_->state == FLAC__STREAM_DECODER_UNINITIALIZED)
1245
0
    return false;
1246
1247
0
  FLAC__ASSERT_DECLARATION(linknumber_start = decoder->protected_->ogg_decoder_aspect.current_linknumber);
1248
1249
0
  if(!FLAC__bitreader_clear(decoder->private_->input)) {
1250
0
    decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1251
0
    return false;
1252
0
  }
1253
0
  status = FLAC__ogg_decoder_aspect_skip_link(&decoder->protected_->ogg_decoder_aspect, read_callback_proxy_, decoder->private_->seek_callback, decoder->private_->tell_callback, decoder->private_->length_callback, decoder, decoder->private_->client_data);
1254
0
  if(status == FLAC__OGG_DECODER_ASPECT_READ_STATUS_END_OF_STREAM) {
1255
0
    decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
1256
0
    return true;
1257
0
  }
1258
0
  else if(status == FLAC__OGG_DECODER_ASPECT_READ_STATUS_CALLBACKS_NONFUNCTIONAL) {
1259
0
    decoder->private_->is_seeking = true;
1260
0
    FLAC__stream_decoder_process_until_end_of_link(decoder);
1261
0
    if(decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_LINK)
1262
0
      FLAC__stream_decoder_finish_link(decoder);
1263
0
    decoder->private_->is_seeking = false;
1264
0
  }
1265
0
  else if(status != FLAC__OGG_DECODER_ASPECT_READ_STATUS_OK) {
1266
0
    decoder->protected_->state = FLAC__STREAM_DECODER_OGG_ERROR;
1267
0
    return false;
1268
0
  }
1269
0
  else {
1270
0
    FLAC__MD5Final(decoder->private_->computed_md5sum, &decoder->private_->md5context);
1271
0
    reset_decoder_internal_(decoder);
1272
0
  }
1273
1274
0
  FLAC__ASSERT(decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM || decoder->protected_->ogg_decoder_aspect.current_linknumber > linknumber_start);
1275
0
  return true;
1276
#else
1277
  (void)decoder;
1278
  return false;
1279
#endif
1280
0
}
1281
1282
FLAC_API FLAC__bool FLAC__stream_decoder_seek_absolute(FLAC__StreamDecoder *decoder, FLAC__uint64 sample)
1283
1.74k
{
1284
1.74k
  FLAC__uint64 length;
1285
1286
1.74k
  FLAC__ASSERT(0 != decoder);
1287
1288
1.74k
  if(
1289
1.74k
    decoder->protected_->state != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA &&
1290
1.74k
    decoder->protected_->state != FLAC__STREAM_DECODER_READ_METADATA &&
1291
1.74k
    decoder->protected_->state != FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC &&
1292
1.74k
    decoder->protected_->state != FLAC__STREAM_DECODER_READ_FRAME &&
1293
1.74k
    decoder->protected_->state != FLAC__STREAM_DECODER_END_OF_STREAM
1294
1.74k
  )
1295
70
    return false;
1296
1297
1.67k
  if(0 == decoder->private_->seek_callback)
1298
0
    return false;
1299
1300
1.67k
  FLAC__ASSERT(decoder->private_->seek_callback);
1301
1.67k
  FLAC__ASSERT(decoder->private_->tell_callback);
1302
1.67k
  FLAC__ASSERT(decoder->private_->length_callback);
1303
1.67k
  FLAC__ASSERT(decoder->private_->eof_callback);
1304
1305
1.67k
  if(!FLAC__stream_decoder_get_decode_chained_stream(decoder) && FLAC__stream_decoder_get_total_samples(decoder) > 0 && sample >= FLAC__stream_decoder_get_total_samples(decoder))
1306
0
    return false;
1307
1308
1.67k
  decoder->private_->is_seeking = true;
1309
1310
  /* turn off md5 checking if a seek is attempted */
1311
1.67k
  decoder->private_->do_md5_checking = false;
1312
1313
  /* get the file length (currently our algorithm needs to know the length so it's also an error to get FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED) */
1314
1.67k
  if(decoder->private_->length_callback(decoder, &length, decoder->private_->client_data) != FLAC__STREAM_DECODER_LENGTH_STATUS_OK) {
1315
1.67k
    decoder->private_->is_seeking = false;
1316
1.67k
    return false;
1317
1.67k
  }
1318
1319
  /* if we haven't finished processing the metadata yet, do that so we have the STREAMINFO, SEEK_TABLE, and first_frame_offset */
1320
0
  if(
1321
0
    decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA ||
1322
0
    decoder->protected_->state == FLAC__STREAM_DECODER_READ_METADATA
1323
0
  ) {
1324
0
    if(!FLAC__stream_decoder_process_until_end_of_metadata(decoder)) {
1325
      /* above call sets the state for us */
1326
0
      decoder->private_->is_seeking = false;
1327
0
      return false;
1328
0
    }
1329
    /* check this again in case we didn't know total_samples the first time */
1330
0
    if(FLAC__stream_decoder_get_total_samples(decoder) > 0 && sample >= FLAC__stream_decoder_get_total_samples(decoder)) {
1331
0
      decoder->private_->is_seeking = false;
1332
0
      return false;
1333
0
    }
1334
0
  }
1335
1336
0
  {
1337
0
    const FLAC__bool ok =
1338
0
#if FLAC__HAS_OGG
1339
0
      decoder->private_->is_ogg?
1340
0
      seek_to_absolute_sample_ogg_(decoder, length, sample) :
1341
0
#endif
1342
0
      seek_to_absolute_sample_(decoder, length, sample)
1343
0
    ;
1344
0
    decoder->private_->is_seeking = false;
1345
0
    return ok;
1346
0
  }
1347
0
}
1348
1349
FLAC_API FLAC__uint64 FLAC__stream_decoder_find_total_samples(FLAC__StreamDecoder *decoder)
1350
0
{
1351
0
  if(
1352
0
    decoder->protected_->state != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA &&
1353
0
    decoder->protected_->state != FLAC__STREAM_DECODER_READ_METADATA &&
1354
0
    decoder->protected_->state != FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC &&
1355
0
    decoder->protected_->state != FLAC__STREAM_DECODER_READ_FRAME &&
1356
0
    decoder->protected_->state != FLAC__STREAM_DECODER_END_OF_STREAM
1357
0
  )
1358
0
    return 0;
1359
1360
0
  if(
1361
0
    decoder->private_->length_callback == NULL ||
1362
0
    decoder->private_->seek_callback == NULL ||
1363
0
    decoder->private_->tell_callback == NULL
1364
0
  )
1365
0
    return 0;
1366
1367
0
#if FLAC__HAS_OGG
1368
0
  if(decoder->private_->is_ogg && FLAC__ogg_decoder_aspect_get_decode_chained_stream(&decoder->protected_->ogg_decoder_aspect)) {
1369
    /* Keep moving forward until reaching end-of-stream */
1370
0
    uint32_t i;
1371
0
    FLAC__uint64 total_samples = 0;
1372
0
    decoder->private_->is_indexing = true;
1373
0
    while(1) {
1374
0
      FLAC__OggDecoderAspectReadStatus status;
1375
0
      if(decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM ||
1376
0
         decoder->protected_->state == FLAC__STREAM_DECODER_OGG_ERROR ||
1377
0
         decoder->protected_->state == FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR ||
1378
0
         decoder->protected_->state == FLAC__STREAM_DECODER_ABORTED) {
1379
0
        decoder->private_->is_indexing = false;
1380
0
        decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
1381
0
        return 0;
1382
0
      }
1383
0
      status = FLAC__ogg_decoder_aspect_skip_link(&decoder->protected_->ogg_decoder_aspect, read_callback_proxy_, decoder->private_->seek_callback, decoder->private_->tell_callback, decoder->private_->length_callback, decoder, decoder->private_->client_data);
1384
0
      if(status == FLAC__OGG_DECODER_ASPECT_READ_STATUS_END_OF_STREAM)
1385
0
        break;
1386
0
      else if(status != FLAC__OGG_DECODER_ASPECT_READ_STATUS_OK) {
1387
0
        decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
1388
0
        return 0;
1389
0
      }
1390
0
    }
1391
0
    decoder->private_->is_indexing = false;
1392
0
    for(i = 0; i < decoder->protected_->ogg_decoder_aspect.number_of_links_indexed; i++) {
1393
0
      total_samples += decoder->protected_->ogg_decoder_aspect.linkdetails[i].samples;
1394
0
    }
1395
0
    return total_samples;
1396
0
  }
1397
0
  else
1398
0
#endif /* FLAC__HAS_OGG */
1399
0
  { /* not decoding chained ogg */
1400
0
    FLAC__uint64 length;
1401
0
    FLAC__uint64 pos;
1402
0
    uint32_t eof_distance = 1024; /* Some number, needs tuning */
1403
0
    decoder->private_->is_seeking = true;
1404
0
    decoder->private_->target_sample = UINT64_MAX;
1405
    /* get the file length */
1406
0
    if(decoder->private_->length_callback(decoder, &length, decoder->private_->client_data) != FLAC__STREAM_DECODER_LENGTH_STATUS_OK) {
1407
0
      decoder->private_->is_indexing = false;
1408
0
      return 0;
1409
0
    }
1410
0
    pos = length;
1411
0
    for( ; ; eof_distance *= 2) {
1412
0
      if(eof_distance > (1u << FLAC__STREAM_METADATA_LENGTH_LEN)) {
1413
        /* Could not find a frame within a reasonable distance of EOF */
1414
0
        return 0;
1415
0
      }
1416
0
      else if(pos == 0) {
1417
        /* Did not find a frame while reading from the start of the file */
1418
0
        return 0;
1419
0
      }
1420
0
      else if(eof_distance > length) {
1421
0
        pos = 0;
1422
0
      }
1423
0
      else {
1424
0
        pos = length - eof_distance;
1425
0
      }
1426
1427
0
      if(decoder->private_->seek_callback(decoder, pos, decoder->private_->client_data) != FLAC__STREAM_DECODER_SEEK_STATUS_OK) {
1428
0
        decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
1429
0
        return 0;
1430
0
      }
1431
0
      if(!FLAC__stream_decoder_flush(decoder)) {
1432
        /* above call sets the state for us */
1433
0
        return 0;
1434
0
      }
1435
1436
0
      decoder->private_->got_a_frame = false;
1437
0
      if(!FLAC__stream_decoder_process_single(decoder) ||
1438
0
         decoder->protected_->state == FLAC__STREAM_DECODER_ABORTED) {
1439
0
        if(decoder->protected_->state != FLAC__STREAM_DECODER_ABORTED && decoder->protected_->state != FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR)
1440
0
          decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
1441
0
        return 0;
1442
0
      }
1443
0
      if(decoder->private_->got_a_frame) {
1444
        /* Found a frame, but we need the last frame and the frame before that, unless the last frame is
1445
         * also the first frame */
1446
0
        if(decoder->private_->frame.header.number.sample_number > 0) {
1447
          /* For now, assume this is not the last frame, set blocksize, and continue.
1448
           * If it turns out this is not the last frame, we'll start over anyway */
1449
0
          decoder->private_->fixed_block_size = decoder->private_->last_frame.header.blocksize;
1450
0
          if(!FLAC__stream_decoder_process_single(decoder) ||
1451
0
             decoder->protected_->state == FLAC__STREAM_DECODER_ABORTED) {
1452
0
            if(decoder->protected_->state != FLAC__STREAM_DECODER_ABORTED && decoder->protected_->state != FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR)
1453
0
              decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
1454
0
            return 0;
1455
0
          }
1456
0
          if(decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM) {
1457
            /* Found last frame, but need to find frame before that too */
1458
0
            continue;
1459
0
          }
1460
0
        }
1461
0
        if(!FLAC__stream_decoder_process_until_end_of_stream(decoder))
1462
0
          return 0;
1463
0
        FLAC__ASSERT(decoder->private_->is_seeking);
1464
0
        FLAC__ASSERT(decoder->private_->last_frame_is_set);
1465
0
        decoder->private_->is_seeking = false;
1466
0
        return (FLAC__uint64)decoder->private_->last_frame.header.number.sample_number + (FLAC__uint64)decoder->private_->last_frame.header.blocksize;
1467
1468
0
      }
1469
0
    }
1470
0
  }
1471
0
  return 0;
1472
0
}
1473
1474
FLAC_API int32_t FLAC__stream_decoder_get_link_lengths(FLAC__StreamDecoder *decoder, FLAC__uint64 **link_lengths)
1475
0
{
1476
  /* If we don't have Ogg, this is (for now) useless, fail */
1477
0
#if FLAC__HAS_OGG
1478
0
  uint32_t i;
1479
1480
  /* Check whether we're decoding chaines ogg, and decoder is somewhat valid */
1481
0
  if(!decoder->private_->is_ogg ||
1482
0
     !FLAC__stream_decoder_get_decode_chained_stream(decoder) ||
1483
0
     decoder->protected_->state == FLAC__STREAM_DECODER_ABORTED ||
1484
0
     decoder->protected_->state == FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR ||
1485
0
     decoder->protected_->state == FLAC__STREAM_DECODER_UNINITIALIZED)
1486
0
    return FLAC__STREAM_DECODER_GET_LINK_LENGTHS_INVALID;
1487
1488
  /* Check whether link details are known. If not, fail */
1489
0
  if(decoder->protected_->ogg_decoder_aspect.number_of_links_indexed == 0 ||
1490
0
     !decoder->protected_->ogg_decoder_aspect.linkdetails[decoder->protected_->ogg_decoder_aspect.number_of_links_indexed - 1].is_last)
1491
0
    return FLAC__STREAM_DECODER_GET_LINK_LENGTHS_NOT_INDEXED;
1492
1493
0
  if(link_lengths != NULL) {
1494
0
    *link_lengths = safe_malloc_mul_2op_p(sizeof(FLAC__uint64), decoder->protected_->ogg_decoder_aspect.number_of_links_indexed);
1495
0
    if(*link_lengths == NULL)
1496
0
      return FLAC__STREAM_DECODER_GET_LINK_LENGTHS_MEMORY_ALLOCATION_ERROR;
1497
1498
0
    for(i = 0; i < decoder->protected_->ogg_decoder_aspect.number_of_links_indexed; i++)
1499
0
      (*link_lengths)[i] = decoder->protected_->ogg_decoder_aspect.linkdetails[i].samples;
1500
0
  }
1501
1502
0
  return decoder->protected_->ogg_decoder_aspect.number_of_links_indexed;
1503
#else
1504
  (void)decoder;
1505
  (void)link_lengths;
1506
  return FLAC__STREAM_DECODER_GET_LINK_LENGTHS_INVALID;
1507
#endif
1508
0
}
1509
1510
1511
/***********************************************************************
1512
 *
1513
 * Protected class methods
1514
 *
1515
 ***********************************************************************/
1516
1517
uint32_t FLAC__stream_decoder_get_input_bytes_unconsumed(const FLAC__StreamDecoder *decoder)
1518
0
{
1519
0
  FLAC__ASSERT(0 != decoder);
1520
0
  FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
1521
0
  FLAC__ASSERT(!(FLAC__bitreader_get_input_bits_unconsumed(decoder->private_->input) & 7));
1522
0
  return FLAC__bitreader_get_input_bits_unconsumed(decoder->private_->input) / 8;
1523
0
}
1524
1525
/***********************************************************************
1526
 *
1527
 * Private class methods
1528
 *
1529
 ***********************************************************************/
1530
1531
void set_defaults_(FLAC__StreamDecoder *decoder)
1532
17.1k
{
1533
17.1k
  decoder->private_->is_ogg = false;
1534
17.1k
  decoder->private_->read_callback = 0;
1535
17.1k
  decoder->private_->seek_callback = 0;
1536
17.1k
  decoder->private_->tell_callback = 0;
1537
17.1k
  decoder->private_->length_callback = 0;
1538
17.1k
  decoder->private_->eof_callback = 0;
1539
17.1k
  decoder->private_->write_callback = 0;
1540
17.1k
  decoder->private_->metadata_callback = 0;
1541
17.1k
  decoder->private_->error_callback = 0;
1542
17.1k
  decoder->private_->client_data = 0;
1543
1544
17.1k
  memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter));
1545
17.1k
  decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO] = true;
1546
17.1k
  decoder->private_->metadata_filter_ids_count = 0;
1547
1548
17.1k
  decoder->protected_->md5_checking = false;
1549
1550
17.1k
#if FLAC__HAS_OGG
1551
17.1k
  FLAC__ogg_decoder_aspect_set_defaults(&decoder->protected_->ogg_decoder_aspect);
1552
17.1k
#endif
1553
17.1k
}
1554
1555
/*
1556
 * This will forcibly set stdin to binary mode (for OSes that require it)
1557
 */
1558
FILE *get_binary_stdin_(void)
1559
0
{
1560
  /* if something breaks here it is probably due to the presence or
1561
   * absence of an underscore before the identifiers 'setmode',
1562
   * 'fileno', and/or 'O_BINARY'; check your system header files.
1563
   */
1564
#if defined _MSC_VER || defined __MINGW32__
1565
  _setmode(_fileno(stdin), _O_BINARY);
1566
#elif defined __EMX__
1567
  setmode(fileno(stdin), O_BINARY);
1568
#endif
1569
1570
0
  return stdin;
1571
0
}
1572
1573
FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, uint32_t size, uint32_t channels, uint32_t bps)
1574
71.1k
{
1575
71.1k
  uint32_t i;
1576
71.1k
  FLAC__int32 *tmp;
1577
1578
71.1k
  if(size <= decoder->private_->output_capacity && channels <= decoder->private_->output_channels &&
1579
71.1k
     (bps < 32 || decoder->private_->side_subframe != 0))
1580
61.0k
    return true;
1581
1582
  /* simply using realloc() is not practical because the number of channels may change mid-stream */
1583
1584
90.7k
  for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
1585
80.6k
    if(0 != decoder->private_->output[i]) {
1586
20.8k
      free(decoder->private_->output[i]-4);
1587
20.8k
      decoder->private_->output[i] = 0;
1588
20.8k
    }
1589
80.6k
    if(0 != decoder->private_->residual_unaligned[i]) {
1590
20.8k
      free(decoder->private_->residual_unaligned[i]);
1591
20.8k
      decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0;
1592
20.8k
    }
1593
80.6k
  }
1594
1595
10.0k
  if(0 != decoder->private_->side_subframe) {
1596
2.13k
    free(decoder->private_->side_subframe);
1597
2.13k
    decoder->private_->side_subframe = 0;
1598
2.13k
  }
1599
1600
  /* Only grow per-channel buffers, even if number of channels increases */
1601
10.0k
  if(decoder->private_->output_capacity > size) {
1602
2.98k
    size = decoder->private_->output_capacity;
1603
2.98k
  }
1604
1605
47.3k
  for(i = 0; i < channels; i++) {
1606
    /* WATCHOUT:
1607
     * FLAC__lpc_restore_signal_asm_ia32_mmx() and ..._intrin_sseN()
1608
     * require that the output arrays have a buffer of up to 3 zeroes
1609
     * in front (at negative indices) for alignment purposes;
1610
     * we use 4 to keep the data well-aligned.
1611
     */
1612
37.3k
    tmp = safe_malloc_muladd2_(sizeof(FLAC__int32), /*times (*/size, /*+*/4/*)*/);
1613
37.3k
    if(tmp == 0) {
1614
0
      decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1615
0
      return false;
1616
0
    }
1617
37.3k
    memset(tmp, 0, sizeof(FLAC__int32)*4);
1618
37.3k
    decoder->private_->output[i] = tmp + 4;
1619
1620
37.3k
    if(!FLAC__memory_alloc_aligned_int32_array(size, &decoder->private_->residual_unaligned[i], &decoder->private_->residual[i])) {
1621
0
      decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1622
0
      return false;
1623
0
    }
1624
37.3k
  }
1625
1626
10.0k
  if(bps == 32) {
1627
3.71k
    decoder->private_->side_subframe = safe_malloc_mul_2op_p(sizeof(FLAC__int64), /*times (*/size);
1628
3.71k
    if(decoder->private_->side_subframe == NULL) {
1629
0
      decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1630
0
      return false;
1631
0
    }
1632
3.71k
  }
1633
1634
10.0k
  decoder->private_->output_capacity = size;
1635
10.0k
  decoder->private_->output_channels = channels;
1636
1637
10.0k
  return true;
1638
10.0k
}
1639
1640
FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id)
1641
1.71k
{
1642
1.71k
  size_t i;
1643
1644
1.71k
  FLAC__ASSERT(0 != decoder);
1645
1.71k
  FLAC__ASSERT(0 != decoder->private_);
1646
1647
3.21k
  for(i = 0; i < decoder->private_->metadata_filter_ids_count; i++)
1648
1.71k
    if(0 == memcmp(decoder->private_->metadata_filter_ids + i * (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), id, (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8)))
1649
221
      return true;
1650
1651
1.49k
  return false;
1652
1.71k
}
1653
1654
FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder)
1655
80.9k
{
1656
80.9k
  FLAC__uint32 x;
1657
80.9k
  uint32_t i, id;
1658
80.9k
  FLAC__bool first = true;
1659
1660
80.9k
  FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
1661
1662
1.39M
  for(i = id = 0; i < 4; ) {
1663
1.35M
    if(decoder->private_->cached) {
1664
17.5k
      x = (FLAC__uint32)decoder->private_->lookahead;
1665
17.5k
      decoder->private_->cached = false;
1666
17.5k
    }
1667
1.34M
    else {
1668
1.34M
      if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
1669
27.8k
        return false; /* read_callback_ sets the state for us */
1670
1.34M
    }
1671
1.33M
    if(x == FLAC__STREAM_SYNC_STRING[i]) {
1672
166k
      first = true;
1673
166k
      i++;
1674
166k
      id = 0;
1675
166k
      continue;
1676
166k
    }
1677
1678
1.16M
    if(id >= 3)
1679
52
      return false;
1680
1681
1.16M
    if(x == ID3V2_TAG_[id]) {
1682
4.42k
      id++;
1683
4.42k
      i = 0;
1684
4.42k
      if(id == 3) {
1685
1.02k
        if(!skip_id3v2_tag_(decoder))
1686
975
          return false; /* skip_id3v2_tag_ sets the state for us */
1687
1.02k
      }
1688
3.45k
      continue;
1689
4.42k
    }
1690
1.15M
    id = 0;
1691
1.15M
    if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1692
53.4k
      decoder->private_->header_warmup[0] = (FLAC__byte)x;
1693
53.4k
      if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
1694
256
        return false; /* read_callback_ sets the state for us */
1695
1696
      /* we have to check if we just read two 0xff's in a row; the second may actually be the beginning of the sync code */
1697
      /* else we have to check if the second byte is the end of a sync code */
1698
53.2k
      if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1699
17.5k
        decoder->private_->lookahead = (FLAC__byte)x;
1700
17.5k
        decoder->private_->cached = true;
1701
17.5k
      }
1702
35.6k
      else if(x >> 1 == 0x7c) { /* MAGIC NUMBER for the last 6 sync bits and reserved 7th bit */
1703
11.6k
        decoder->private_->header_warmup[1] = (FLAC__byte)x;
1704
11.6k
        decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
1705
11.6k
        return true;
1706
11.6k
      }
1707
53.2k
    }
1708
1.14M
    i = 0;
1709
1.14M
    if(first) {
1710
34.0k
      send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
1711
34.0k
      first = false;
1712
34.0k
    }
1713
1.14M
  }
1714
1715
40.0k
  decoder->protected_->state = FLAC__STREAM_DECODER_READ_METADATA;
1716
40.0k
  return true;
1717
80.9k
}
1718
1719
FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder)
1720
63.0k
{
1721
63.0k
  FLAC__bool is_last;
1722
63.0k
  FLAC__uint32 i, x, type, length;
1723
1724
63.0k
  FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
1725
1726
63.0k
  if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_IS_LAST_LEN))
1727
2.82k
    return false; /* read_callback_ sets the state for us */
1728
60.1k
  is_last = x? true : false;
1729
1730
60.1k
  if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &type, FLAC__STREAM_METADATA_TYPE_LEN))
1731
0
    return false; /* read_callback_ sets the state for us */
1732
1733
60.1k
  if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &length, FLAC__STREAM_METADATA_LENGTH_LEN))
1734
548
    return false; /* read_callback_ sets the state for us */
1735
1736
59.6k
  if(type == FLAC__METADATA_TYPE_STREAMINFO) {
1737
9.06k
    if(!read_metadata_streaminfo_(decoder, is_last, length))
1738
4.14k
      return false;
1739
1740
4.91k
    decoder->private_->has_stream_info = true;
1741
4.91k
    if(0 == memcmp(decoder->private_->stream_info.data.stream_info.md5sum, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16))
1742
1.98k
      decoder->private_->do_md5_checking = false;
1743
4.91k
    if(!decoder->private_->is_seeking && decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO] && decoder->private_->metadata_callback)
1744
3.55k
      decoder->private_->metadata_callback(decoder, &decoder->private_->stream_info, decoder->private_->client_data);
1745
4.91k
  }
1746
50.5k
  else if(type == FLAC__METADATA_TYPE_SEEKTABLE) {
1747
    /* just in case we already have a seek table, and reading the next one fails: */
1748
6.71k
    decoder->private_->has_seek_table = false;
1749
1750
6.71k
    if(length > 0) {
1751
4.31k
      if(!read_metadata_seektable_(decoder, is_last, length))
1752
2.56k
        return false;
1753
1754
1.75k
      decoder->private_->has_seek_table = true;
1755
1.75k
      if(!decoder->private_->is_seeking && decoder->private_->metadata_filter[FLAC__METADATA_TYPE_SEEKTABLE] && decoder->private_->metadata_callback)
1756
1.55k
        decoder->private_->metadata_callback(decoder, &decoder->private_->seek_table, decoder->private_->client_data);
1757
1.75k
    }
1758
6.71k
  }
1759
43.8k
  else {
1760
43.8k
    FLAC__bool skip_it = !decoder->private_->metadata_filter[type];
1761
43.8k
    uint32_t real_length = length;
1762
43.8k
    FLAC__StreamMetadata block;
1763
1764
43.8k
    memset(&block, 0, sizeof(block));
1765
43.8k
    block.is_last = is_last;
1766
43.8k
    block.type = (FLAC__MetadataType)type;
1767
43.8k
    block.length = length;
1768
1769
43.8k
    if(type == FLAC__METADATA_TYPE_APPLICATION) {
1770
3.63k
      if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8))
1771
1.27k
        return false; /* read_callback_ sets the state for us */
1772
1773
2.36k
      if(real_length < FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8) { /* underflow check */
1774
38
        decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;/*@@@@@@ maybe wrong error? need to resync?*/
1775
38
        return false;
1776
38
      }
1777
1778
2.32k
      real_length -= FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8;
1779
1780
2.32k
      if(decoder->private_->metadata_filter_ids_count > 0 && has_id_filtered_(decoder, block.data.application.id))
1781
221
        skip_it = !skip_it;
1782
2.32k
    }
1783
1784
42.5k
    if(skip_it) {
1785
1.66k
      if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, real_length))
1786
732
        return false; /* read_callback_ sets the state for us */
1787
1.66k
    }
1788
40.8k
    else {
1789
40.8k
      FLAC__bool ok = true;
1790
40.8k
      FLAC__bitreader_set_limit(decoder->private_->input, real_length*8);
1791
40.8k
      switch(type) {
1792
1.69k
        case FLAC__METADATA_TYPE_PADDING:
1793
          /* skip the padding bytes */
1794
1.69k
          if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, real_length))
1795
725
            ok = false; /* read_callback_ sets the state for us */
1796
1.69k
          break;
1797
1.86k
        case FLAC__METADATA_TYPE_APPLICATION:
1798
          /* remember, we read the ID already */
1799
1.86k
          if(real_length > 0) {
1800
1.02k
            if(0 == (block.data.application.data = malloc(real_length))) {
1801
0
              decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1802
0
              ok = false;
1803
0
            }
1804
1.02k
            else if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.application.data, real_length))
1805
439
              ok = false; /* read_callback_ sets the state for us */
1806
1.02k
          }
1807
842
          else
1808
842
            block.data.application.data = 0;
1809
1.86k
          break;
1810
5.25k
        case FLAC__METADATA_TYPE_VORBIS_COMMENT:
1811
5.25k
          if(!read_metadata_vorbiscomment_(decoder, &block.data.vorbis_comment, real_length))
1812
3.05k
            ok = false;
1813
5.25k
          break;
1814
7.27k
        case FLAC__METADATA_TYPE_CUESHEET:
1815
7.27k
          if(!read_metadata_cuesheet_(decoder, &block.data.cue_sheet))
1816
4.50k
            ok = false;
1817
7.27k
          break;
1818
19.7k
        case FLAC__METADATA_TYPE_PICTURE:
1819
19.7k
          if(!read_metadata_picture_(decoder, &block.data.picture))
1820
10.1k
            ok = false;
1821
19.7k
          break;
1822
0
        case FLAC__METADATA_TYPE_STREAMINFO:
1823
0
        case FLAC__METADATA_TYPE_SEEKTABLE:
1824
0
          FLAC__ASSERT(0);
1825
0
          break;
1826
5.06k
        default:
1827
5.06k
          if(real_length > 0) {
1828
3.47k
            if(0 == (block.data.unknown.data = malloc(real_length))) {
1829
0
              decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1830
0
              ok = false;
1831
0
            }
1832
3.47k
            else if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.unknown.data, real_length))
1833
1.77k
              ok = false; /* read_callback_ sets the state for us */
1834
3.47k
          }
1835
1.59k
          else
1836
1.59k
            block.data.unknown.data = 0;
1837
5.06k
          break;
1838
40.8k
      }
1839
40.8k
      if(FLAC__bitreader_limit_remaining(decoder->private_->input) > 0) {
1840
        /* Content in metadata block didn't fit in block length
1841
         * We cannot know whether the length or the content was
1842
         * corrupt, so stop parsing metadata */
1843
22.1k
        send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_METADATA);
1844
22.1k
        if(decoder->protected_->state == FLAC__STREAM_DECODER_READ_METADATA)
1845
8.02k
          decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1846
22.1k
        ok = false;
1847
22.1k
      }
1848
40.8k
      FLAC__bitreader_remove_limit(decoder->private_->input);
1849
40.8k
      if(ok && !decoder->private_->is_seeking && decoder->private_->metadata_callback)
1850
18.7k
        decoder->private_->metadata_callback(decoder, &block, decoder->private_->client_data);
1851
1852
      /* now we have to free any malloc()ed data in the block */
1853
40.8k
      switch(type) {
1854
1.69k
        case FLAC__METADATA_TYPE_PADDING:
1855
1.69k
          break;
1856
1.86k
        case FLAC__METADATA_TYPE_APPLICATION:
1857
1.86k
          if(0 != block.data.application.data)
1858
1.02k
            free(block.data.application.data);
1859
1.86k
          break;
1860
5.25k
        case FLAC__METADATA_TYPE_VORBIS_COMMENT:
1861
5.25k
          if(0 != block.data.vorbis_comment.vendor_string.entry)
1862
4.54k
            free(block.data.vorbis_comment.vendor_string.entry);
1863
5.25k
          if(block.data.vorbis_comment.num_comments > 0)
1864
7.55k
            for(i = 0; i < block.data.vorbis_comment.num_comments; i++)
1865
6.03k
              if(0 != block.data.vorbis_comment.comments[i].entry)
1866
6.03k
                free(block.data.vorbis_comment.comments[i].entry);
1867
5.25k
          if(0 != block.data.vorbis_comment.comments)
1868
1.96k
            free(block.data.vorbis_comment.comments);
1869
5.25k
          break;
1870
7.27k
        case FLAC__METADATA_TYPE_CUESHEET:
1871
7.27k
          if(block.data.cue_sheet.num_tracks > 0 && 0 != block.data.cue_sheet.tracks)
1872
125k
            for(i = 0; i < block.data.cue_sheet.num_tracks; i++)
1873
119k
              if(0 != block.data.cue_sheet.tracks[i].indices)
1874
8.33k
                free(block.data.cue_sheet.tracks[i].indices);
1875
7.27k
          if(0 != block.data.cue_sheet.tracks)
1876
5.95k
            free(block.data.cue_sheet.tracks);
1877
7.27k
          break;
1878
19.7k
        case FLAC__METADATA_TYPE_PICTURE:
1879
19.7k
          if(0 != block.data.picture.mime_type)
1880
17.9k
            free(block.data.picture.mime_type);
1881
19.7k
          if(0 != block.data.picture.description)
1882
16.9k
            free(block.data.picture.description);
1883
19.7k
          if(0 != block.data.picture.data)
1884
10.0k
            free(block.data.picture.data);
1885
19.7k
          break;
1886
0
        case FLAC__METADATA_TYPE_STREAMINFO:
1887
0
        case FLAC__METADATA_TYPE_SEEKTABLE:
1888
0
          FLAC__ASSERT(0);
1889
5.06k
        default:
1890
5.06k
          if(0 != block.data.unknown.data)
1891
3.47k
            free(block.data.unknown.data);
1892
5.06k
          break;
1893
40.8k
      }
1894
1895
40.8k
      if(!ok) /* anything that unsets "ok" should also make sure decoder->protected_->state is updated */
1896
22.1k
        return false;
1897
40.8k
    }
1898
42.5k
  }
1899
1900
28.7k
  if(is_last) {
1901
    /* if this fails, it's OK, it's just a hint for the seek routine */
1902
6.76k
    if(!FLAC__stream_decoder_get_decode_position(decoder, &decoder->private_->first_frame_offset))
1903
6.76k
      decoder->private_->first_frame_offset = 0;
1904
6.76k
    decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1905
6.76k
  }
1906
1907
28.7k
  return true;
1908
59.6k
}
1909
1910
FLAC__bool read_metadata_streaminfo_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, uint32_t length)
1911
9.06k
{
1912
9.06k
  FLAC__uint32 x;
1913
9.06k
  uint32_t bits, used_bits = 0;
1914
1915
9.06k
  FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
1916
1917
9.06k
  decoder->private_->stream_info.type = FLAC__METADATA_TYPE_STREAMINFO;
1918
9.06k
  decoder->private_->stream_info.is_last = is_last;
1919
9.06k
  decoder->private_->stream_info.length = length;
1920
1921
9.06k
  bits = FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN;
1922
9.06k
  if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, bits))
1923
480
    return false; /* read_callback_ sets the state for us */
1924
8.58k
  decoder->private_->stream_info.data.stream_info.min_blocksize = x;
1925
8.58k
  used_bits += bits;
1926
1927
8.58k
  bits = FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN;
1928
8.58k
  if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN))
1929
20
    return false; /* read_callback_ sets the state for us */
1930
8.56k
  decoder->private_->stream_info.data.stream_info.max_blocksize = x;
1931
8.56k
  used_bits += bits;
1932
1933
8.56k
  bits = FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN;
1934
8.56k
  if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN))
1935
533
    return false; /* read_callback_ sets the state for us */
1936
8.03k
  decoder->private_->stream_info.data.stream_info.min_framesize = x;
1937
8.03k
  used_bits += bits;
1938
1939
8.03k
  bits = FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN;
1940
8.03k
  if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN))
1941
158
    return false; /* read_callback_ sets the state for us */
1942
7.87k
  decoder->private_->stream_info.data.stream_info.max_framesize = x;
1943
7.87k
  used_bits += bits;
1944
1945
7.87k
  bits = FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN;
1946
7.87k
  if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN))
1947
771
    return false; /* read_callback_ sets the state for us */
1948
7.10k
  decoder->private_->stream_info.data.stream_info.sample_rate = x;
1949
7.10k
  used_bits += bits;
1950
1951
7.10k
  bits = FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN;
1952
7.10k
  if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN))
1953
0
    return false; /* read_callback_ sets the state for us */
1954
7.10k
  decoder->private_->stream_info.data.stream_info.channels = x+1;
1955
7.10k
  used_bits += bits;
1956
1957
7.10k
  bits = FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN;
1958
7.10k
  if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN))
1959
78
    return false; /* read_callback_ sets the state for us */
1960
7.02k
  decoder->private_->stream_info.data.stream_info.bits_per_sample = x+1;
1961
7.02k
  used_bits += bits;
1962
1963
7.02k
  bits = FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN;
1964
7.02k
  if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &decoder->private_->stream_info.data.stream_info.total_samples, FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN))
1965
87
    return false; /* read_callback_ sets the state for us */
1966
6.93k
  used_bits += bits;
1967
1968
6.93k
  if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, decoder->private_->stream_info.data.stream_info.md5sum, 16))
1969
452
    return false; /* read_callback_ sets the state for us */
1970
6.48k
  used_bits += 16*8;
1971
1972
  /* skip the rest of the block */
1973
6.48k
  FLAC__ASSERT(used_bits % 8 == 0);
1974
6.48k
  if (length < (used_bits / 8))
1975
796
    return false; /* read_callback_ sets the state for us */
1976
5.68k
  length -= (used_bits / 8);
1977
5.68k
  if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, length))
1978
774
    return false; /* read_callback_ sets the state for us */
1979
1980
4.91k
  return true;
1981
5.68k
}
1982
1983
FLAC__bool read_metadata_seektable_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, uint32_t length)
1984
4.31k
{
1985
4.31k
  FLAC__uint32 i, x;
1986
4.31k
  FLAC__uint64 xx;
1987
1988
4.31k
  FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
1989
1990
4.31k
  decoder->private_->seek_table.type = FLAC__METADATA_TYPE_SEEKTABLE;
1991
4.31k
  decoder->private_->seek_table.is_last = is_last;
1992
4.31k
  decoder->private_->seek_table.length = length;
1993
1994
4.31k
  if(length % FLAC__STREAM_METADATA_SEEKPOINT_LENGTH) {
1995
1.96k
    FLAC__bitreader_limit_invalidate(decoder->private_->input);
1996
1.96k
    return false;
1997
1.96k
  }
1998
1999
2.35k
  decoder->private_->seek_table.data.seek_table.num_points = length / FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
2000
2001
  /* use realloc since we may pass through here several times (e.g. after seeking) */
2002
2.35k
  if(0 == (decoder->private_->seek_table.data.seek_table.points = safe_realloc_mul_2op_(decoder->private_->seek_table.data.seek_table.points, decoder->private_->seek_table.data.seek_table.num_points, /*times*/sizeof(FLAC__StreamMetadata_SeekPoint)))) {
2003
0
    decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
2004
0
    return false;
2005
0
  }
2006
17.6k
  for(i = 0; i < decoder->private_->seek_table.data.seek_table.num_points; i++) {
2007
15.9k
    if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN))
2008
485
      return false; /* read_callback_ sets the state for us */
2009
15.4k
    decoder->private_->seek_table.data.seek_table.points[i].sample_number = xx;
2010
2011
15.4k
    if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN))
2012
78
      return false; /* read_callback_ sets the state for us */
2013
15.3k
    decoder->private_->seek_table.data.seek_table.points[i].stream_offset = xx;
2014
2015
15.3k
    if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN))
2016
37
      return false; /* read_callback_ sets the state for us */
2017
15.3k
    decoder->private_->seek_table.data.seek_table.points[i].frame_samples = x;
2018
15.3k
  }
2019
1.75k
  length -= (decoder->private_->seek_table.data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH);
2020
2021
1.75k
  FLAC__ASSERT(length == 0);
2022
2023
1.75k
  return true;
2024
1.75k
}
2025
2026
FLAC__bool read_metadata_vorbiscomment_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_VorbisComment *obj, uint32_t length)
2027
5.25k
{
2028
5.25k
  FLAC__uint32 i;
2029
2030
5.25k
  FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
2031
2032
  /* read vendor string */
2033
5.25k
  if (length >= 8) {
2034
5.21k
    length -= 8; /* vendor string length + num comments entries alone take 8 bytes */
2035
5.21k
    FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32);
2036
5.21k
    if (!FLAC__bitreader_read_uint32_little_endian(decoder->private_->input, &obj->vendor_string.length))
2037
233
      return false; /* read_callback_ sets the state for us */
2038
4.97k
    if (length < obj->vendor_string.length) {
2039
428
      obj->vendor_string.length = 0;
2040
428
      obj->vendor_string.entry = 0;
2041
428
      goto skip;
2042
428
    }
2043
4.54k
    else
2044
4.54k
      length -= obj->vendor_string.length;
2045
4.54k
    if (0 == (obj->vendor_string.entry = safe_malloc_add_2op_(obj->vendor_string.length, /*+*/1))) {
2046
0
      decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
2047
0
      return false;
2048
0
    }
2049
4.54k
    if (!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->vendor_string.entry, obj->vendor_string.length))
2050
403
      return false; /* read_callback_ sets the state for us */
2051
4.14k
    obj->vendor_string.entry[obj->vendor_string.length] = '\0';
2052
2053
    /* read num comments */
2054
4.14k
    FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN == 32);
2055
4.14k
    if (!FLAC__bitreader_read_uint32_little_endian(decoder->private_->input, &obj->num_comments))
2056
1.38k
      return false; /* read_callback_ sets the state for us */
2057
2058
    /* read comments */
2059
2.76k
    if (obj->num_comments > 100000) {
2060
      /* Possibly malicious file. */
2061
152
      obj->num_comments = 0;
2062
152
      return false;
2063
152
    }
2064
2.61k
    if (obj->num_comments > 0) {
2065
2.04k
      if (0 == (obj->comments = safe_malloc_mul_2op_p(obj->num_comments, /*times*/sizeof(FLAC__StreamMetadata_VorbisComment_Entry)))) {
2066
0
        obj->num_comments = 0;
2067
0
        decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
2068
0
        return false;
2069
0
      }
2070
8.08k
      for (i = 0; i < obj->num_comments; i++) {
2071
        /* Initialize here just to make sure. */
2072
7.39k
        obj->comments[i].length = 0;
2073
7.39k
        obj->comments[i].entry = 0;
2074
2075
7.39k
        FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32);
2076
7.39k
        if (length < 4) {
2077
883
          obj->num_comments = i;
2078
883
          goto skip;
2079
883
        }
2080
6.51k
        else
2081
6.51k
          length -= 4;
2082
6.51k
        if (!FLAC__bitreader_read_uint32_little_endian(decoder->private_->input, &obj->comments[i].length)) {
2083
65
          obj->num_comments = i;
2084
65
          return false; /* read_callback_ sets the state for us */
2085
65
        }
2086
6.44k
        if (length < obj->comments[i].length) {
2087
138
          obj->num_comments = i;
2088
138
          FLAC__bitreader_limit_invalidate(decoder->private_->input);
2089
138
          return false;
2090
138
        }
2091
6.31k
        else
2092
6.31k
          length -= obj->comments[i].length;
2093
6.31k
        if (0 == (obj->comments[i].entry = safe_malloc_add_2op_(obj->comments[i].length, /*+*/1))) {
2094
0
          decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
2095
0
          obj->num_comments = i;
2096
0
          return false;
2097
0
        }
2098
6.31k
        memset (obj->comments[i].entry, 0, obj->comments[i].length) ;
2099
6.31k
        if (!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->comments[i].entry, obj->comments[i].length)) {
2100
          /* Current i-th entry is bad, so we delete it. */
2101
272
          free (obj->comments[i].entry) ;
2102
272
          obj->comments[i].entry = NULL ;
2103
272
          obj->num_comments = i;
2104
272
          goto skip;
2105
272
        }
2106
6.03k
        obj->comments[i].entry[obj->comments[i].length] = '\0';
2107
6.03k
      }
2108
2.04k
    }
2109
2.61k
  }
2110
47
  else {
2111
47
    FLAC__bitreader_limit_invalidate(decoder->private_->input);
2112
47
    return false;
2113
47
  }
2114
2115
2.83k
  skip:
2116
2.83k
  if (length > 0) {
2117
    /* length > 0 can only happen on files with invalid data in comments */
2118
630
    if(obj->num_comments < 1) {
2119
433
      free(obj->comments);
2120
433
      obj->comments = NULL;
2121
433
    }
2122
630
    FLAC__bitreader_limit_invalidate(decoder->private_->input);
2123
630
    return false;
2124
630
  }
2125
2126
2.20k
  return true;
2127
2.83k
}
2128
2129
FLAC__bool read_metadata_cuesheet_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_CueSheet *obj)
2130
7.27k
{
2131
7.27k
  FLAC__uint32 i, j, x;
2132
2133
7.27k
  FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
2134
2135
7.27k
  memset(obj, 0, sizeof(FLAC__StreamMetadata_CueSheet));
2136
2137
7.27k
  FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN % 8 == 0);
2138
7.27k
  if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, (FLAC__byte*)obj->media_catalog_number, FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN/8))
2139
148
    return false; /* read_callback_ sets the state for us */
2140
2141
7.13k
  if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &obj->lead_in, FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN))
2142
84
    return false; /* read_callback_ sets the state for us */
2143
2144
7.04k
  if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN))
2145
75
    return false; /* read_callback_ sets the state for us */
2146
6.97k
  obj->is_cd = x? true : false;
2147
2148
6.97k
  if(!FLAC__bitreader_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN))
2149
545
    return false; /* read_callback_ sets the state for us */
2150
2151
6.42k
  if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN))
2152
176
    return false; /* read_callback_ sets the state for us */
2153
6.25k
  obj->num_tracks = x;
2154
2155
6.25k
  if(obj->num_tracks > 0) {
2156
5.95k
    if(0 == (obj->tracks = safe_calloc_(obj->num_tracks, sizeof(FLAC__StreamMetadata_CueSheet_Track)))) {
2157
0
      decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
2158
0
      return false;
2159
0
    }
2160
14.4k
    for(i = 0; i < obj->num_tracks; i++) {
2161
11.7k
      FLAC__StreamMetadata_CueSheet_Track *track = &obj->tracks[i];
2162
11.7k
      if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &track->offset, FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN))
2163
707
        return false; /* read_callback_ sets the state for us */
2164
2165
11.0k
      if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN))
2166
194
        return false; /* read_callback_ sets the state for us */
2167
10.8k
      track->number = (FLAC__byte)x;
2168
2169
10.8k
      FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN % 8 == 0);
2170
10.8k
      if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, (FLAC__byte*)track->isrc, FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN/8))
2171
152
        return false; /* read_callback_ sets the state for us */
2172
2173
10.6k
      if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN))
2174
10
        return false; /* read_callback_ sets the state for us */
2175
10.6k
      track->type = x;
2176
2177
10.6k
      if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN))
2178
0
        return false; /* read_callback_ sets the state for us */
2179
10.6k
      track->pre_emphasis = x;
2180
2181
10.6k
      if(!FLAC__bitreader_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN))
2182
521
        return false; /* read_callback_ sets the state for us */
2183
2184
10.1k
      if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN))
2185
210
        return false; /* read_callback_ sets the state for us */
2186
9.91k
      track->num_indices = (FLAC__byte)x;
2187
2188
9.91k
      if(track->num_indices > 0) {
2189
8.33k
        if(0 == (track->indices = safe_calloc_(track->num_indices, sizeof(FLAC__StreamMetadata_CueSheet_Index)))) {
2190
0
          decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
2191
0
          return false;
2192
0
        }
2193
53.7k
        for(j = 0; j < track->num_indices; j++) {
2194
46.8k
          FLAC__StreamMetadata_CueSheet_Index *indx = &track->indices[j];
2195
46.8k
          if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &indx->offset, FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN))
2196
1.04k
            return false; /* read_callback_ sets the state for us */
2197
2198
45.7k
          if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN))
2199
200
            return false; /* read_callback_ sets the state for us */
2200
45.5k
          indx->number = (FLAC__byte)x;
2201
2202
45.5k
          if(!FLAC__bitreader_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN))
2203
134
            return false; /* read_callback_ sets the state for us */
2204
45.5k
        }
2205
8.33k
      }
2206
9.91k
    }
2207
5.95k
  }
2208
300
  else { /* obj->num_tracks == 0 */
2209
300
    FLAC__bitreader_limit_invalidate(decoder->private_->input);
2210
300
    return false;
2211
300
  }
2212
2213
2.77k
  return true;
2214
6.25k
}
2215
2216
FLAC__bool read_metadata_picture_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_Picture *obj)
2217
19.7k
{
2218
19.7k
  FLAC__uint32 x;
2219
2220
19.7k
  FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
2221
2222
  /* read type */
2223
19.7k
  if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_PICTURE_TYPE_LEN))
2224
430
    return false; /* read_callback_ sets the state for us */
2225
19.3k
  if(x < FLAC__STREAM_METADATA_PICTURE_TYPE_UNDEFINED)
2226
536
    obj->type = x;
2227
18.7k
  else
2228
18.7k
    obj->type = FLAC__STREAM_METADATA_PICTURE_TYPE_OTHER;
2229
2230
  /* read MIME type */
2231
19.3k
  if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN))
2232
524
    return false; /* read_callback_ sets the state for us */
2233
18.7k
  if(FLAC__bitreader_limit_remaining(decoder->private_->input) < x){
2234
823
    FLAC__bitreader_limit_invalidate(decoder->private_->input);
2235
823
    return false;
2236
823
  }
2237
17.9k
  if(0 == (obj->mime_type = safe_malloc_add_2op_(x, /*+*/1))) {
2238
0
    decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
2239
0
    return false;
2240
0
  }
2241
17.9k
  if(x > 0) {
2242
1.39k
    if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, (FLAC__byte*)obj->mime_type, x))
2243
441
      return false; /* read_callback_ sets the state for us */
2244
1.39k
  }
2245
17.5k
  obj->mime_type[x] = '\0';
2246
2247
  /* read description */
2248
17.5k
  if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN))
2249
413
    return false; /* read_callback_ sets the state for us */
2250
17.1k
  if(FLAC__bitreader_limit_remaining(decoder->private_->input) < x){
2251
166
    FLAC__bitreader_limit_invalidate(decoder->private_->input);
2252
166
    return false;
2253
166
  }
2254
16.9k
  if(0 == (obj->description = safe_malloc_add_2op_(x, /*+*/1))) {
2255
0
    decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
2256
0
    return false;
2257
0
  }
2258
16.9k
  if(x > 0) {
2259
11.7k
    if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->description, x))
2260
699
      return false; /* read_callback_ sets the state for us */
2261
11.7k
  }
2262
16.2k
  obj->description[x] = '\0';
2263
2264
  /* read width */
2265
16.2k
  if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->width, FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN))
2266
3.40k
    return false; /* read_callback_ sets the state for us */
2267
2268
  /* read height */
2269
12.8k
  if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->height, FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN))
2270
123
    return false; /* read_callback_ sets the state for us */
2271
2272
  /* read depth */
2273
12.7k
  if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->depth, FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN))
2274
253
    return false; /* read_callback_ sets the state for us */
2275
2276
  /* read colors */
2277
12.4k
  if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->colors, FLAC__STREAM_METADATA_PICTURE_COLORS_LEN))
2278
503
    return false; /* read_callback_ sets the state for us */
2279
2280
  /* read data */
2281
11.9k
  if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &(obj->data_length), FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN))
2282
92
    return false; /* read_callback_ sets the state for us */
2283
11.8k
  if(FLAC__bitreader_limit_remaining(decoder->private_->input) < obj->data_length){
2284
1.85k
    FLAC__bitreader_limit_invalidate(decoder->private_->input);
2285
1.85k
    return false;
2286
1.85k
  }
2287
10.0k
  if(0 == (obj->data = safe_malloc_(obj->data_length))) {
2288
0
    decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
2289
0
    return false;
2290
0
  }
2291
10.0k
  if(obj->data_length > 0) {
2292
8.67k
    if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->data, obj->data_length))
2293
400
      return false; /* read_callback_ sets the state for us */
2294
8.67k
  }
2295
2296
9.60k
  return true;
2297
10.0k
}
2298
2299
FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder)
2300
1.02k
{
2301
1.02k
  FLAC__uint32 x;
2302
1.02k
  uint32_t i, skip;
2303
2304
  /* skip the version and flags bytes */
2305
1.02k
  if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 24))
2306
20
    return false; /* read_callback_ sets the state for us */
2307
  /* get the size (in bytes) to skip */
2308
1.00k
  skip = 0;
2309
3.49k
  for(i = 0; i < 4; i++) {
2310
2.99k
    if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
2311
511
      return false; /* read_callback_ sets the state for us */
2312
2.48k
    skip <<= 7;
2313
2.48k
    skip |= (x & 0x7f);
2314
2.48k
  }
2315
  /* skip the rest of the tag */
2316
497
  if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, skip))
2317
444
    return false; /* read_callback_ sets the state for us */
2318
53
  return true;
2319
497
}
2320
2321
FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder)
2322
127k
{
2323
127k
  FLAC__uint32 x;
2324
127k
  FLAC__bool first = true;
2325
2326
  /* make sure we're byte aligned */
2327
127k
  if(!FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)) {
2328
0
    if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__bitreader_bits_left_for_byte_alignment(decoder->private_->input)))
2329
0
      return false; /* read_callback_ sets the state for us */
2330
0
  }
2331
2332
2.32M
  while(1) {
2333
2.32M
    if(decoder->private_->cached) {
2334
102k
      x = (FLAC__uint32)decoder->private_->lookahead;
2335
102k
      decoder->private_->cached = false;
2336
102k
    }
2337
2.22M
    else {
2338
2.22M
      if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
2339
11.7k
        return false; /* read_callback_ sets the state for us */
2340
2.22M
    }
2341
2.31M
    if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
2342
244k
      decoder->private_->header_warmup[0] = (FLAC__byte)x;
2343
244k
      if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
2344
1.67k
        return false; /* read_callback_ sets the state for us */
2345
2346
      /* we have to check if we just read two 0xff's in a row; the second may actually be the beginning of the sync code */
2347
      /* else we have to check if the second byte is the end of a sync code */
2348
242k
      if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
2349
76.2k
        decoder->private_->lookahead = (FLAC__byte)x;
2350
76.2k
        decoder->private_->cached = true;
2351
76.2k
      }
2352
166k
      else if(x >> 1 == 0x7c) { /* MAGIC NUMBER for the last 6 sync bits and reserved 7th bit */
2353
113k
        decoder->private_->header_warmup[1] = (FLAC__byte)x;
2354
113k
        decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
2355
2356
        /* Save location so we can rewind in case the frame turns
2357
         * out to be invalid after the header */
2358
113k
        FLAC__bitreader_set_framesync_location(decoder->private_->input);
2359
113k
        if(!FLAC__stream_decoder_get_decode_position(decoder, &decoder->private_->last_seen_framesync))
2360
113k
          decoder->private_->last_seen_framesync = 0;
2361
113k
        return true;
2362
113k
      }
2363
242k
    }
2364
2.19M
    if(first) {
2365
94.9k
      send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
2366
94.9k
      first = false;
2367
94.9k
    }
2368
2.19M
  }
2369
2370
0
  return true;
2371
127k
}
2372
2373
FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame, FLAC__bool do_full_decode)
2374
125k
{
2375
125k
  uint32_t channel;
2376
125k
  uint32_t i;
2377
125k
  uint32_t frame_crc; /* the one we calculate from the input stream */
2378
125k
  FLAC__uint32 x;
2379
2380
125k
  *got_a_frame = false;
2381
125k
  decoder->private_->side_subframe_in_use = false;
2382
2383
  /* init the CRC */
2384
125k
  frame_crc = 0;
2385
125k
  frame_crc = FLAC__CRC16_UPDATE(decoder->private_->header_warmup[0], frame_crc);
2386
125k
  frame_crc = FLAC__CRC16_UPDATE(decoder->private_->header_warmup[1], frame_crc);
2387
125k
  FLAC__bitreader_reset_read_crc16(decoder->private_->input, (FLAC__uint16)frame_crc);
2388
2389
125k
  if(!read_frame_header_(decoder))
2390
1.59k
    return false;
2391
123k
  if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means we didn't sync on a valid header */
2392
52.6k
    return true;
2393
71.1k
  if(!allocate_output_(decoder, decoder->private_->frame.header.blocksize, decoder->private_->frame.header.channels, decoder->private_->frame.header.bits_per_sample))
2394
0
    return false;
2395
163k
  for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
2396
    /*
2397
     * first figure the correct bits-per-sample of the subframe
2398
     */
2399
140k
    uint32_t bps = decoder->private_->frame.header.bits_per_sample;
2400
140k
    switch(decoder->private_->frame.header.channel_assignment) {
2401
107k
      case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
2402
        /* no adjustment needed */
2403
107k
        break;
2404
7.52k
      case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
2405
7.52k
        FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
2406
7.52k
        if(channel == 1)
2407
3.17k
          bps++;
2408
7.52k
        break;
2409
22.0k
      case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
2410
22.0k
        FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
2411
22.0k
        if(channel == 0)
2412
12.6k
          bps++;
2413
22.0k
        break;
2414
2.76k
      case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
2415
2.76k
        FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
2416
2.76k
        if(channel == 1)
2417
1.23k
          bps++;
2418
2.76k
        break;
2419
0
      default:
2420
0
        FLAC__ASSERT(0);
2421
140k
    }
2422
    /*
2423
     * now read it
2424
     */
2425
140k
    if(!read_subframe_(decoder, channel, bps, do_full_decode)){
2426
      /* read_callback_ sets the state for us */
2427
6.17k
      if(decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM)
2428
0
        break;
2429
6.17k
      else
2430
6.17k
        return false;
2431
6.17k
    }
2432
134k
    if(decoder->protected_->state != FLAC__STREAM_DECODER_READ_FRAME)
2433
41.7k
      break;
2434
134k
  }
2435
2436
64.9k
  if(decoder->protected_->state != FLAC__STREAM_DECODER_END_OF_STREAM)
2437
64.9k
    if(!read_zero_padding_(decoder))
2438
0
      return false;
2439
2440
  /*
2441
   * Read the frame CRC-16 from the footer and check
2442
   */
2443
64.9k
  if(decoder->protected_->state == FLAC__STREAM_DECODER_READ_FRAME) {
2444
23.2k
    frame_crc = FLAC__bitreader_get_read_crc16(decoder->private_->input);
2445
23.2k
    if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__FRAME_FOOTER_CRC_LEN)) {
2446
      /* read_callback_ sets the state for us */
2447
187
      if(decoder->protected_->state != FLAC__STREAM_DECODER_END_OF_STREAM)
2448
187
        return false;
2449
187
    }
2450
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
2451
  }
2452
  if(decoder->protected_->state == FLAC__STREAM_DECODER_READ_FRAME && frame_crc == x) {
2453
#endif
2454
23.0k
    if(do_full_decode) {
2455
      /* Undo any special channel coding */
2456
22.8k
      undo_channel_coding(decoder);
2457
      /* Check whether decoded data actually fits bps */
2458
65.5k
      for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
2459
42.6k
        int shift_bits = 32 - decoder->private_->frame.header.bits_per_sample;
2460
42.6k
        int lower_limit = INT32_MIN >> shift_bits;
2461
42.6k
        int upper_limit = INT32_MAX >> shift_bits;
2462
145M
        for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
2463
          /* Check whether shift_bits MSBs are 'empty' by shifting up and down */
2464
145M
          if((decoder->private_->output[channel][i] < lower_limit) ||
2465
145M
             (decoder->private_->output[channel][i] > upper_limit)) {
2466
            /* Bad frame, emit error */
2467
4.95k
            send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_OUT_OF_BOUNDS);
2468
4.95k
            decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2469
4.95k
            break;
2470
4.95k
          }
2471
145M
        }
2472
42.6k
      }
2473
22.8k
    }
2474
23.0k
  }
2475
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
2476
  else if (decoder->protected_->state == FLAC__STREAM_DECODER_READ_FRAME) {
2477
    /* Bad frame, emit error */
2478
    send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH);
2479
    decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2480
  }
2481
#endif
2482
2483
  /* Check whether frames are missing, if so, add silence to compensate */
2484
64.7k
  if(decoder->private_->last_frame_is_set && decoder->protected_->state == FLAC__STREAM_DECODER_READ_FRAME && !decoder->private_->is_seeking && do_full_decode) {
2485
16.8k
    FLAC__ASSERT(decoder->private_->frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
2486
16.8k
    FLAC__ASSERT(decoder->private_->last_frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
2487
16.8k
    if(decoder->private_->last_frame.header.number.sample_number + decoder->private_->last_frame.header.blocksize < decoder->private_->frame.header.number.sample_number) {
2488
5.04k
      uint32_t padding_samples_needed = decoder->private_->frame.header.number.sample_number - (decoder->private_->last_frame.header.number.sample_number + decoder->private_->last_frame.header.blocksize);
2489
      /* Send an error that we lost sync, but only in case no error
2490
       * has been sent yet. This is the case if exactly one or more
2491
       * frames are missing, and the frames before and after it
2492
       * are complete */
2493
5.04k
      if(!decoder->private_->error_has_been_sent)
2494
2.09k
        send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_MISSING_FRAME);
2495
      /* Do some extra validation to assure last frame an current frame
2496
       * header are both valid before adding silence inbetween
2497
       * Technically both frames could be valid with differing sample_rates,
2498
       * channels and bits_per_sample, but it is quite rare */
2499
5.04k
      if(decoder->private_->last_frame.header.sample_rate == decoder->private_->frame.header.sample_rate &&
2500
5.04k
         decoder->private_->last_frame.header.channels == decoder->private_->frame.header.channels &&
2501
5.04k
         decoder->private_->last_frame.header.bits_per_sample == decoder->private_->frame.header.bits_per_sample &&
2502
5.04k
         decoder->private_->last_frame.header.blocksize >= 16) {
2503
2.09k
        FLAC__Frame empty_frame;
2504
2.09k
        FLAC__int32 * empty_buffer[FLAC__MAX_CHANNELS] = {NULL};
2505
2.09k
        empty_frame.header = decoder->private_->last_frame.header;
2506
2.09k
        empty_frame.footer.crc = 0;
2507
5.92k
        for(i = 0; i < empty_frame.header.channels; i++) {
2508
3.83k
          empty_buffer[i] = safe_calloc_(empty_frame.header.blocksize, sizeof(FLAC__int32));
2509
3.83k
          if(empty_buffer[i] == NULL) {
2510
0
            for(i = 0; i < empty_frame.header.channels; i++)
2511
0
              if(empty_buffer[i] != NULL)
2512
0
                free(empty_buffer[i]);
2513
0
            decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
2514
0
            return false;
2515
0
          }
2516
3.83k
        }
2517
        /* No repairs larger than 5 seconds or 50 frames are made, to not
2518
         * unexpectedly create enormous files when one of the headers was
2519
         * corrupt after all */
2520
2.09k
        if(padding_samples_needed > (5*empty_frame.header.sample_rate))
2521
510
          padding_samples_needed = 5*empty_frame.header.sample_rate;
2522
2.09k
        if(padding_samples_needed > (50*empty_frame.header.blocksize))
2523
858
          padding_samples_needed = 50*empty_frame.header.blocksize;
2524
56.8k
        while(padding_samples_needed){
2525
54.8k
          empty_frame.header.number.sample_number += empty_frame.header.blocksize;
2526
54.8k
          if(padding_samples_needed < empty_frame.header.blocksize)
2527
531
            empty_frame.header.blocksize = padding_samples_needed;
2528
54.8k
          padding_samples_needed -= empty_frame.header.blocksize;
2529
54.8k
          decoder->protected_->blocksize = empty_frame.header.blocksize;
2530
2531
54.8k
          FLAC__ASSERT(empty_frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
2532
54.8k
          decoder->private_->samples_decoded = empty_frame.header.number.sample_number + empty_frame.header.blocksize;
2533
2534
154k
          for(channel = 0; channel < empty_frame.header.channels; channel++) {
2535
99.1k
            empty_frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_CONSTANT;
2536
99.1k
            empty_frame.subframes[channel].data.constant.value = 0;
2537
99.1k
            empty_frame.subframes[channel].wasted_bits = 0;
2538
99.1k
          }
2539
2540
54.8k
          if(write_audio_frame_to_client_(decoder, &empty_frame, (const FLAC__int32 * const *)empty_buffer) != FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE) {
2541
172
            decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
2542
831
            for(i = 0; i < empty_frame.header.channels; i++)
2543
659
              if(empty_buffer[i] != NULL)
2544
659
                free(empty_buffer[i]);
2545
172
            return false;
2546
172
          }
2547
54.8k
        }
2548
5.09k
        for(i = 0; i < empty_frame.header.channels; i++)
2549
3.17k
          if(empty_buffer[i] != NULL)
2550
3.17k
            free(empty_buffer[i]);
2551
2552
1.92k
      }
2553
5.04k
    }
2554
16.8k
  }
2555
2556
64.5k
  decoder->private_->error_has_been_sent = false;
2557
2558
64.5k
  if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC || decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM) {
2559
44.6k
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
2560
44.6k
    decoder->private_->fuzzing_rewind_count++; /* To stop excessive rewinding, as it causes timeouts */
2561
44.6k
#endif
2562
    /* Got corruption, rewind if possible. Return value of seek
2563
    * isn't checked, if the seek fails the decoder will continue anyway */
2564
44.6k
    if(!FLAC__bitreader_rewind_to_after_last_seen_framesync(decoder->private_->input)){
2565
#ifndef NDEBUG
2566
      flac_fprintf(stderr, "Rewinding, seeking necessary\n");
2567
#endif
2568
4.68k
      if(decoder->private_->seek_callback && decoder->private_->last_seen_framesync){
2569
        /* Last framesync isn't in bitreader anymore, rewind with seek if possible */
2570
#ifndef NDEBUG
2571
        FLAC__uint64 current_decode_position;
2572
        if(FLAC__stream_decoder_get_decode_position(decoder, &current_decode_position))
2573
          flac_fprintf(stderr, "Bitreader was %" PRIu64 " bytes short\n", current_decode_position-decoder->private_->last_seen_framesync);
2574
#endif
2575
0
        if(decoder->private_->seek_callback(decoder, decoder->private_->last_seen_framesync, decoder->private_->client_data) == FLAC__STREAM_DECODER_SEEK_STATUS_ERROR) {
2576
0
          decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
2577
0
          return false;
2578
0
        }
2579
0
        if(!FLAC__bitreader_clear(decoder->private_->input)) {
2580
0
          decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
2581
0
          return false;
2582
0
        }
2583
0
      }
2584
4.68k
    }
2585
#ifndef NDEBUG
2586
    else{
2587
      flac_fprintf(stderr, "Rewinding, seeking not necessary\n");
2588
    }
2589
#endif
2590
44.6k
  }
2591
19.9k
  else {
2592
19.9k
    *got_a_frame = true;
2593
19.9k
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
2594
19.9k
    decoder->private_->fuzzing_rewind_count = 0;
2595
19.9k
#endif
2596
2597
    /* we wait to update fixed_block_size until here, when we're sure we've got a proper frame and hence a correct blocksize */
2598
19.9k
    if(decoder->private_->next_fixed_block_size)
2599
1.32k
      decoder->private_->fixed_block_size = decoder->private_->next_fixed_block_size;
2600
2601
    /* put the latest values into the public section of the decoder instance */
2602
19.9k
    decoder->protected_->channels = decoder->private_->frame.header.channels;
2603
19.9k
    decoder->protected_->channel_assignment = decoder->private_->frame.header.channel_assignment;
2604
19.9k
    decoder->protected_->bits_per_sample = decoder->private_->frame.header.bits_per_sample;
2605
19.9k
    decoder->protected_->sample_rate = decoder->private_->frame.header.sample_rate;
2606
19.9k
    decoder->protected_->blocksize = decoder->private_->frame.header.blocksize;
2607
2608
19.9k
    FLAC__ASSERT(decoder->private_->frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
2609
19.9k
    decoder->private_->samples_decoded = decoder->private_->frame.header.number.sample_number + decoder->private_->frame.header.blocksize;
2610
2611
    /* write it */
2612
19.9k
    if(do_full_decode) {
2613
19.7k
      if(write_audio_frame_to_client_(decoder, &decoder->private_->frame, (const FLAC__int32 * const *)decoder->private_->output) != FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE) {
2614
1.08k
        decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
2615
1.08k
        return false;
2616
1.08k
      }
2617
19.7k
    }
2618
19.9k
  }
2619
2620
63.4k
  decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2621
63.4k
  return true;
2622
64.5k
}
2623
2624
FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder)
2625
125k
{
2626
125k
  FLAC__uint32 x;
2627
125k
  FLAC__uint64 xx;
2628
125k
  uint32_t i, blocksize_hint = 0, sample_rate_hint = 0;
2629
125k
  FLAC__byte crc8, raw_header[16]; /* MAGIC NUMBER based on the maximum frame header size, including CRC */
2630
125k
  uint32_t raw_header_len;
2631
125k
  FLAC__bool is_unparseable = false;
2632
2633
125k
  FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
2634
2635
  /* init the raw header with the saved bits from synchronization */
2636
125k
  raw_header[0] = decoder->private_->header_warmup[0];
2637
125k
  raw_header[1] = decoder->private_->header_warmup[1];
2638
125k
  raw_header_len = 2;
2639
2640
  /* check to make sure that reserved bit is 0 */
2641
125k
  if(raw_header[1] & 0x02) /* MAGIC NUMBER */
2642
0
    is_unparseable = true;
2643
2644
  /*
2645
   * Note that along the way as we read the header, we look for a sync
2646
   * code inside.  If we find one it would indicate that our original
2647
   * sync was bad since there cannot be a sync code in a valid header.
2648
   *
2649
   * Three kinds of things can go wrong when reading the frame header:
2650
   *  1) We may have sync'ed incorrectly and not landed on a frame header.
2651
   *     If we don't find a sync code, it can end up looking like we read
2652
   *     a valid but unparseable header, until getting to the frame header
2653
   *     CRC.  Even then we could get a false positive on the CRC.
2654
   *  2) We may have sync'ed correctly but on an unparseable frame (from a
2655
   *     future encoder).
2656
   *  3) We may be on a damaged frame which appears valid but unparseable.
2657
   *
2658
   * For all these reasons, we try and read a complete frame header as
2659
   * long as it seems valid, even if unparseable, up until the frame
2660
   * header CRC.
2661
   */
2662
2663
  /*
2664
   * read in the raw header as bytes so we can CRC it, and parse it on the way
2665
   */
2666
355k
  for(i = 0; i < 2; i++) {
2667
241k
    if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
2668
794
      return false; /* read_callback_ sets the state for us */
2669
241k
    if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
2670
      /* if we get here it means our original sync was erroneous since the sync code cannot appear in the header */
2671
10.7k
      decoder->private_->lookahead = (FLAC__byte)x;
2672
10.7k
      decoder->private_->cached = true;
2673
10.7k
      send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
2674
10.7k
      decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2675
10.7k
      return true;
2676
10.7k
    }
2677
230k
    raw_header[raw_header_len++] = (FLAC__byte)x;
2678
230k
  }
2679
2680
113k
  switch(x = raw_header[2] >> 4) {
2681
13.6k
    case 0:
2682
13.6k
      is_unparseable = true;
2683
13.6k
      break;
2684
2.62k
    case 1:
2685
2.62k
      decoder->private_->frame.header.blocksize = 192;
2686
2.62k
      break;
2687
2.44k
    case 2:
2688
3.64k
    case 3:
2689
9.67k
    case 4:
2690
12.7k
    case 5:
2691
12.7k
      decoder->private_->frame.header.blocksize = 576 << (x-2);
2692
12.7k
      break;
2693
49.9k
    case 6:
2694
56.9k
    case 7:
2695
56.9k
      blocksize_hint = x;
2696
56.9k
      break;
2697
2.95k
    case 8:
2698
15.6k
    case 9:
2699
16.6k
    case 10:
2700
18.8k
    case 11:
2701
20.0k
    case 12:
2702
23.1k
    case 13:
2703
24.4k
    case 14:
2704
27.8k
    case 15:
2705
27.8k
      decoder->private_->frame.header.blocksize = 256 << (x-8);
2706
27.8k
      break;
2707
0
    default:
2708
0
      FLAC__ASSERT(0);
2709
0
      break;
2710
113k
  }
2711
2712
113k
  switch(x = raw_header[2] & 0x0f) {
2713
15.7k
    case 0:
2714
15.7k
      if(decoder->private_->has_stream_info)
2715
4.24k
        decoder->private_->frame.header.sample_rate = decoder->private_->stream_info.data.stream_info.sample_rate;
2716
11.5k
      else
2717
11.5k
        is_unparseable = true;
2718
15.7k
      break;
2719
16.6k
    case 1:
2720
16.6k
      decoder->private_->frame.header.sample_rate = 88200;
2721
16.6k
      break;
2722
11.3k
    case 2:
2723
11.3k
      decoder->private_->frame.header.sample_rate = 176400;
2724
11.3k
      break;
2725
5.71k
    case 3:
2726
5.71k
      decoder->private_->frame.header.sample_rate = 192000;
2727
5.71k
      break;
2728
6.45k
    case 4:
2729
6.45k
      decoder->private_->frame.header.sample_rate = 8000;
2730
6.45k
      break;
2731
2.15k
    case 5:
2732
2.15k
      decoder->private_->frame.header.sample_rate = 16000;
2733
2.15k
      break;
2734
9.06k
    case 6:
2735
9.06k
      decoder->private_->frame.header.sample_rate = 22050;
2736
9.06k
      break;
2737
11.6k
    case 7:
2738
11.6k
      decoder->private_->frame.header.sample_rate = 24000;
2739
11.6k
      break;
2740
6.69k
    case 8:
2741
6.69k
      decoder->private_->frame.header.sample_rate = 32000;
2742
6.69k
      break;
2743
2.57k
    case 9:
2744
2.57k
      decoder->private_->frame.header.sample_rate = 44100;
2745
2.57k
      break;
2746
12.4k
    case 10:
2747
12.4k
      decoder->private_->frame.header.sample_rate = 48000;
2748
12.4k
      break;
2749
2.43k
    case 11:
2750
2.43k
      decoder->private_->frame.header.sample_rate = 96000;
2751
2.43k
      break;
2752
1.39k
    case 12:
2753
4.95k
    case 13:
2754
9.34k
    case 14:
2755
9.34k
      sample_rate_hint = x;
2756
9.34k
      break;
2757
1.52k
    case 15:
2758
1.52k
      send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
2759
1.52k
      decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2760
1.52k
      return true;
2761
0
    default:
2762
0
      FLAC__ASSERT(0);
2763
113k
  }
2764
2765
112k
  x = (uint32_t)(raw_header[3] >> 4);
2766
112k
  if(x & 8) {
2767
29.4k
    decoder->private_->frame.header.channels = 2;
2768
29.4k
    switch(x & 7) {
2769
5.40k
      case 0:
2770
5.40k
        decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE;
2771
5.40k
        break;
2772
15.7k
      case 1:
2773
15.7k
        decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE;
2774
15.7k
        break;
2775
2.08k
      case 2:
2776
2.08k
        decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_MID_SIDE;
2777
2.08k
        break;
2778
6.22k
      default:
2779
6.22k
        is_unparseable = true;
2780
6.22k
        break;
2781
29.4k
    }
2782
29.4k
  }
2783
82.8k
  else {
2784
82.8k
    decoder->private_->frame.header.channels = (uint32_t)x + 1;
2785
82.8k
    decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT;
2786
82.8k
  }
2787
2788
112k
  switch(x = (uint32_t)(raw_header[3] & 0x0e) >> 1) {
2789
29.7k
    case 0:
2790
29.7k
      if(decoder->private_->has_stream_info)
2791
10.7k
        decoder->private_->frame.header.bits_per_sample = decoder->private_->stream_info.data.stream_info.bits_per_sample;
2792
19.0k
      else
2793
19.0k
        is_unparseable = true;
2794
29.7k
      break;
2795
28.2k
    case 1:
2796
28.2k
      decoder->private_->frame.header.bits_per_sample = 8;
2797
28.2k
      break;
2798
6.46k
    case 2:
2799
6.46k
      decoder->private_->frame.header.bits_per_sample = 12;
2800
6.46k
      break;
2801
2.56k
    case 3:
2802
2.56k
      is_unparseable = true;
2803
2.56k
      break;
2804
5.67k
    case 4:
2805
5.67k
      decoder->private_->frame.header.bits_per_sample = 16;
2806
5.67k
      break;
2807
6.10k
    case 5:
2808
6.10k
      decoder->private_->frame.header.bits_per_sample = 20;
2809
6.10k
      break;
2810
12.9k
    case 6:
2811
12.9k
      decoder->private_->frame.header.bits_per_sample = 24;
2812
12.9k
      break;
2813
20.3k
    case 7:
2814
20.3k
      decoder->private_->frame.header.bits_per_sample = 32;
2815
20.3k
      break;
2816
0
    default:
2817
0
      FLAC__ASSERT(0);
2818
0
      break;
2819
112k
  }
2820
2821
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
2822
  /* check to make sure that reserved bit is 0 */
2823
  if(raw_header[3] & 0x01) /* MAGIC NUMBER */
2824
    is_unparseable = true;
2825
#endif
2826
2827
  /* read the frame's starting sample number (or frame number as the case may be) */
2828
112k
  if(
2829
112k
    raw_header[1] & 0x01 ||
2830
    /*@@@ this clause is a concession to the old way of doing variable blocksize; the only known implementation is flake and can probably be removed without inconveniencing anyone */
2831
112k
    (decoder->private_->has_stream_info && decoder->private_->stream_info.data.stream_info.min_blocksize != decoder->private_->stream_info.data.stream_info.max_blocksize)
2832
112k
  ) { /* variable blocksize */
2833
47.1k
    if(!FLAC__bitreader_read_utf8_uint64(decoder->private_->input, &xx, raw_header, &raw_header_len))
2834
180
      return false; /* read_callback_ sets the state for us */
2835
46.9k
    if(xx == FLAC__U64L(0xffffffffffffffff)) { /* i.e. non-UTF8 code... */
2836
7.27k
      decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
2837
7.27k
      decoder->private_->cached = true;
2838
7.27k
      send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
2839
7.27k
      decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2840
7.27k
      return true;
2841
7.27k
    }
2842
39.7k
    decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
2843
39.7k
    decoder->private_->frame.header.number.sample_number = xx;
2844
39.7k
  }
2845
65.0k
  else { /* fixed blocksize */
2846
65.0k
    if(!FLAC__bitreader_read_utf8_uint32(decoder->private_->input, &x, raw_header, &raw_header_len))
2847
206
      return false; /* read_callback_ sets the state for us */
2848
64.8k
    if(x == 0xffffffff) { /* i.e. non-UTF8 code... */
2849
8.17k
      decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
2850
8.17k
      decoder->private_->cached = true;
2851
8.17k
      send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
2852
8.17k
      decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2853
8.17k
      return true;
2854
8.17k
    }
2855
56.6k
    decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER;
2856
56.6k
    decoder->private_->frame.header.number.frame_number = x;
2857
56.6k
  }
2858
2859
96.4k
  if(blocksize_hint) {
2860
51.1k
    if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
2861
46
      return false; /* read_callback_ sets the state for us */
2862
51.0k
    raw_header[raw_header_len++] = (FLAC__byte)x;
2863
51.0k
    if(blocksize_hint == 7) {
2864
4.63k
      FLAC__uint32 _x;
2865
4.63k
      if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &_x, 8))
2866
87
        return false; /* read_callback_ sets the state for us */
2867
4.54k
      raw_header[raw_header_len++] = (FLAC__byte)_x;
2868
4.54k
      x = (x << 8) | _x;
2869
4.54k
    }
2870
50.9k
    decoder->private_->frame.header.blocksize = x+1;
2871
50.9k
    if(decoder->private_->frame.header.blocksize > 65535) { /* invalid blocksize (65536) specified */
2872
435
      decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
2873
435
      decoder->private_->cached = true;
2874
435
      send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
2875
435
      decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2876
435
      return true;
2877
435
    }
2878
2879
50.9k
  }
2880
2881
95.8k
  if(sample_rate_hint) {
2882
7.83k
    if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
2883
84
      return false; /* read_callback_ sets the state for us */
2884
7.74k
    raw_header[raw_header_len++] = (FLAC__byte)x;
2885
7.74k
    if(sample_rate_hint != 12) {
2886
6.67k
      FLAC__uint32 _x;
2887
6.67k
      if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &_x, 8))
2888
73
        return false; /* read_callback_ sets the state for us */
2889
6.60k
      raw_header[raw_header_len++] = (FLAC__byte)_x;
2890
6.60k
      x = (x << 8) | _x;
2891
6.60k
    }
2892
7.67k
    if(sample_rate_hint == 12)
2893
1.07k
      decoder->private_->frame.header.sample_rate = x*1000;
2894
6.60k
    else if(sample_rate_hint == 13)
2895
2.85k
      decoder->private_->frame.header.sample_rate = x;
2896
3.74k
    else
2897
3.74k
      decoder->private_->frame.header.sample_rate = x*10;
2898
7.67k
  }
2899
2900
  /* read the CRC-8 byte */
2901
95.6k
  if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
2902
120
    return false; /* read_callback_ sets the state for us */
2903
95.5k
  crc8 = (FLAC__byte)x;
2904
2905
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
2906
  if(FLAC__crc8(raw_header, raw_header_len) != crc8) {
2907
#else
2908
95.5k
  if(decoder->private_->fuzzing_rewind_count > 32 && (FLAC__crc8(raw_header, raw_header_len) << 4) != (crc8 << 4)) {
2909
7.08k
#endif
2910
2911
7.08k
    send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
2912
7.08k
    decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2913
7.08k
    return true;
2914
7.08k
  }
2915
2916
  /* calculate the sample number from the frame number if needed */
2917
88.4k
  decoder->private_->next_fixed_block_size = 0;
2918
88.4k
  if(decoder->private_->frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER) {
2919
52.2k
    x = decoder->private_->frame.header.number.frame_number;
2920
52.2k
    decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
2921
52.2k
    if(decoder->private_->fixed_block_size)
2922
15.3k
      decoder->private_->frame.header.number.sample_number = (FLAC__uint64)decoder->private_->fixed_block_size * (FLAC__uint64)x;
2923
36.9k
    else if(decoder->private_->has_stream_info) {
2924
2.29k
      if(decoder->private_->stream_info.data.stream_info.min_blocksize == decoder->private_->stream_info.data.stream_info.max_blocksize) {
2925
2.29k
        decoder->private_->frame.header.number.sample_number = (FLAC__uint64)decoder->private_->stream_info.data.stream_info.min_blocksize * (FLAC__uint64)x;
2926
2.29k
        decoder->private_->next_fixed_block_size = decoder->private_->stream_info.data.stream_info.max_blocksize;
2927
2.29k
      }
2928
0
      else
2929
0
        is_unparseable = true;
2930
2.29k
    }
2931
34.6k
    else if(x == 0) {
2932
10.7k
      decoder->private_->frame.header.number.sample_number = 0;
2933
10.7k
      decoder->private_->next_fixed_block_size = decoder->private_->frame.header.blocksize;
2934
10.7k
    }
2935
23.8k
    else {
2936
      /* can only get here if the stream has invalid frame numbering and no STREAMINFO, so assume it's not the last (possibly short) frame */
2937
23.8k
      decoder->private_->frame.header.number.sample_number = (FLAC__uint64)decoder->private_->frame.header.blocksize * (FLAC__uint64)x;
2938
23.8k
    }
2939
52.2k
  }
2940
2941
88.4k
  if(is_unparseable) {
2942
17.3k
    send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
2943
17.3k
    decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2944
17.3k
    return true;
2945
17.3k
  }
2946
2947
71.1k
  return true;
2948
88.4k
}
2949
2950
FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, uint32_t channel, uint32_t bps, FLAC__bool do_full_decode)
2951
140k
{
2952
140k
  FLAC__uint32 x;
2953
140k
  FLAC__bool wasted_bits;
2954
140k
  uint32_t i;
2955
2956
140k
  if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8)) /* MAGIC NUMBER */
2957
589
    return false; /* read_callback_ sets the state for us */
2958
2959
139k
  wasted_bits = (x & 1);
2960
139k
  x &= 0xfe;
2961
2962
139k
  if(wasted_bits) {
2963
46.6k
    uint32_t u;
2964
46.6k
    if(!FLAC__bitreader_read_unary_unsigned(decoder->private_->input, &u))
2965
122
      return false; /* read_callback_ sets the state for us */
2966
46.5k
    decoder->private_->frame.subframes[channel].wasted_bits = u+1;
2967
46.5k
    if (decoder->private_->frame.subframes[channel].wasted_bits >= bps) {
2968
1.34k
      send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
2969
1.34k
      decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2970
1.34k
      return true;
2971
1.34k
    }
2972
45.1k
    bps -= decoder->private_->frame.subframes[channel].wasted_bits;
2973
45.1k
  }
2974
92.9k
  else
2975
92.9k
    decoder->private_->frame.subframes[channel].wasted_bits = 0;
2976
2977
  /*
2978
   * Lots of magic numbers here
2979
   */
2980
138k
  if(x & 0x80) {
2981
20.6k
    send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
2982
20.6k
    decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2983
20.6k
    return true;
2984
20.6k
  }
2985
117k
  else if(x == 0) {
2986
50.9k
    if(!read_subframe_constant_(decoder, channel, bps, do_full_decode))
2987
159
      return false;
2988
50.9k
  }
2989
66.5k
  else if(x == 2) {
2990
2.90k
    if(!read_subframe_verbatim_(decoder, channel, bps, do_full_decode))
2991
554
      return false;
2992
2.90k
  }
2993
63.6k
  else if(x < 16) {
2994
3.73k
    send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
2995
3.73k
    decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2996
3.73k
    return true;
2997
3.73k
  }
2998
59.9k
  else if(x <= 24) {
2999
22.8k
    uint32_t predictor_order = (x>>1)&7;
3000
22.8k
    if(decoder->private_->frame.header.blocksize <= predictor_order){
3001
227
      send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
3002
227
      decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
3003
227
      return true;
3004
227
    }
3005
22.6k
    if(!read_subframe_fixed_(decoder, channel, bps, predictor_order, do_full_decode))
3006
2.42k
      return false;
3007
20.2k
    if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means bad sync or got corruption */
3008
3.68k
      return true;
3009
20.2k
  }
3010
37.0k
  else if(x < 64) {
3011
3.25k
    send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
3012
3.25k
    decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
3013
3.25k
    return true;
3014
3.25k
  }
3015
33.8k
  else {
3016
33.8k
    uint32_t predictor_order = ((x>>1)&31)+1;
3017
33.8k
    if(decoder->private_->frame.header.blocksize <= predictor_order){
3018
2.34k
      send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
3019
2.34k
      decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
3020
2.34k
      return true;
3021
2.34k
    }
3022
31.4k
    if(!read_subframe_lpc_(decoder, channel, bps, predictor_order, do_full_decode))
3023
2.32k
      return false;
3024
29.1k
    if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means bad sync or got corruption */
3025
6.50k
      return true;
3026
29.1k
  }
3027
3028
92.3k
  if(wasted_bits && do_full_decode) {
3029
22.1k
    x = decoder->private_->frame.subframes[channel].wasted_bits;
3030
22.1k
    if((bps + x) < 33) {
3031
16.7M
      for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
3032
16.7M
        uint32_t val = decoder->private_->output[channel][i];
3033
16.7M
        decoder->private_->output[channel][i] = (val << x);
3034
16.7M
      }
3035
20.6k
    }
3036
1.48k
    else {
3037
      /* When there are wasted bits, bps is never 33 and so
3038
       * side_subframe is never already in use */
3039
1.48k
      FLAC__ASSERT(!decoder->private_->side_subframe_in_use);
3040
1.48k
      decoder->private_->side_subframe_in_use = true;
3041
2.00M
      for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
3042
2.00M
        uint64_t val = decoder->private_->output[channel][i];
3043
2.00M
        decoder->private_->side_subframe[i] = (val << x);
3044
2.00M
      }
3045
1.48k
    }
3046
22.1k
  }
3047
3048
92.3k
  return true;
3049
92.3k
}
3050
3051
FLAC__bool read_subframe_constant_(FLAC__StreamDecoder *decoder, uint32_t channel, uint32_t bps, FLAC__bool do_full_decode)
3052
50.9k
{
3053
50.9k
  FLAC__Subframe_Constant *subframe = &decoder->private_->frame.subframes[channel].data.constant;
3054
50.9k
  FLAC__int64 x;
3055
50.9k
  uint32_t i;
3056
3057
50.9k
  decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_CONSTANT;
3058
3059
50.9k
  if(!FLAC__bitreader_read_raw_int64(decoder->private_->input, &x, bps))
3060
159
    return false; /* read_callback_ sets the state for us */
3061
3062
50.7k
  subframe->value = x;
3063
3064
  /* decode the subframe */
3065
50.7k
  if(do_full_decode) {
3066
50.4k
    if(bps <= 32) {
3067
49.1k
      FLAC__int32 *output = decoder->private_->output[channel];
3068
166M
      for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
3069
166M
        output[i] = x;
3070
49.1k
    } else {
3071
1.25k
      FLAC__int64 *output = decoder->private_->side_subframe;
3072
1.25k
      decoder->private_->side_subframe_in_use = true;
3073
5.45M
      for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
3074
5.45M
        output[i] = x;
3075
1.25k
    }
3076
50.4k
  }
3077
3078
50.7k
  return true;
3079
50.9k
}
3080
3081
FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, uint32_t channel, uint32_t bps, const uint32_t order, FLAC__bool do_full_decode)
3082
22.6k
{
3083
22.6k
  FLAC__Subframe_Fixed *subframe = &decoder->private_->frame.subframes[channel].data.fixed;
3084
22.6k
  FLAC__int64 i64;
3085
22.6k
  FLAC__uint32 u32;
3086
22.6k
  uint32_t u;
3087
3088
22.6k
  decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_FIXED;
3089
3090
22.6k
  subframe->residual = decoder->private_->residual[channel];
3091
22.6k
  subframe->order = order;
3092
3093
  /* read warm-up samples */
3094
67.6k
  for(u = 0; u < order; u++) {
3095
45.0k
    if(!FLAC__bitreader_read_raw_int64(decoder->private_->input, &i64, bps))
3096
102
      return false; /* read_callback_ sets the state for us */
3097
44.9k
    subframe->warmup[u] = i64;
3098
44.9k
  }
3099
3100
  /* read entropy coding method info */
3101
22.5k
  if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN))
3102
74
    return false; /* read_callback_ sets the state for us */
3103
22.4k
  subframe->entropy_coding_method.type = (FLAC__EntropyCodingMethodType)u32;
3104
22.4k
  switch(subframe->entropy_coding_method.type) {
3105
18.4k
    case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
3106
20.9k
    case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2:
3107
20.9k
      if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
3108
71
        return false; /* read_callback_ sets the state for us */
3109
20.8k
      if((decoder->private_->frame.header.blocksize >> u32 < order) ||
3110
20.8k
         (decoder->private_->frame.header.blocksize % (1 << u32) > 0)) {
3111
1.67k
        send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
3112
1.67k
        decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
3113
1.67k
        return true;
3114
1.67k
      }
3115
19.1k
      subframe->entropy_coding_method.data.partitioned_rice.order = u32;
3116
19.1k
      subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel];
3117
19.1k
      break;
3118
1.51k
    default:
3119
1.51k
      send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
3120
1.51k
      decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
3121
1.51k
      return true;
3122
22.4k
  }
3123
3124
  /* read residual */
3125
19.1k
  switch(subframe->entropy_coding_method.type) {
3126
17.2k
    case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
3127
19.1k
    case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2:
3128
19.1k
      if(!read_residual_partitioned_rice_(decoder, order, subframe->entropy_coding_method.data.partitioned_rice.order, &decoder->private_->partitioned_rice_contents[channel], decoder->private_->residual[channel], /*is_extended=*/subframe->entropy_coding_method.type == FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2))
3129
2.18k
        return false;
3130
17.0k
      break;
3131
17.0k
    default:
3132
0
      FLAC__ASSERT(0);
3133
19.1k
  }
3134
3135
  /* decode the subframe */
3136
17.0k
  if(do_full_decode) {
3137
16.6k
    if(bps < 33){
3138
11.9k
      uint32_t i;
3139
35.1k
      for(i = 0; i < order; i++)
3140
23.1k
        decoder->private_->output[channel][i] = subframe->warmup[i];
3141
11.9k
      if(bps+order <= 32)
3142
7.49k
        FLAC__fixed_restore_signal(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, order, decoder->private_->output[channel]+order);
3143
4.47k
      else
3144
4.47k
        FLAC__fixed_restore_signal_wide(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, order, decoder->private_->output[channel]+order);
3145
11.9k
    }
3146
4.67k
    else {
3147
4.67k
      decoder->private_->side_subframe_in_use = true;
3148
4.67k
      memcpy(decoder->private_->side_subframe, subframe->warmup, sizeof(FLAC__int64) * order);
3149
4.67k
      FLAC__fixed_restore_signal_wide_33bit(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, order, decoder->private_->side_subframe+order);
3150
4.67k
    }
3151
16.6k
  }
3152
3153
17.0k
  return true;
3154
19.1k
}
3155
3156
FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, uint32_t channel, uint32_t bps, const uint32_t order, FLAC__bool do_full_decode)
3157
31.4k
{
3158
31.4k
  FLAC__Subframe_LPC *subframe = &decoder->private_->frame.subframes[channel].data.lpc;
3159
31.4k
  FLAC__int32 i32;
3160
31.4k
  FLAC__int64 i64;
3161
31.4k
  FLAC__uint32 u32;
3162
31.4k
  uint32_t u;
3163
3164
31.4k
  decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_LPC;
3165
3166
31.4k
  subframe->residual = decoder->private_->residual[channel];
3167
31.4k
  subframe->order = order;
3168
3169
  /* read warm-up samples */
3170
360k
  for(u = 0; u < order; u++) {
3171
329k
    if(!FLAC__bitreader_read_raw_int64(decoder->private_->input, &i64, bps))
3172
872
      return false; /* read_callback_ sets the state for us */
3173
328k
    subframe->warmup[u] = i64;
3174
328k
  }
3175
3176
  /* read qlp coeff precision */
3177
30.6k
  if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN))
3178
321
    return false; /* read_callback_ sets the state for us */
3179
30.3k
  if(u32 == (1u << FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN) - 1) {
3180
1.32k
    send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
3181
1.32k
    decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
3182
1.32k
    return true;
3183
1.32k
  }
3184
28.9k
  subframe->qlp_coeff_precision = u32+1;
3185
3186
  /* read qlp shift */
3187
28.9k
  if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i32, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN))
3188
70
    return false; /* read_callback_ sets the state for us */
3189
28.9k
  if(i32 < 0) {
3190
1.63k
    send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
3191
1.63k
    decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
3192
1.63k
    return true;
3193
1.63k
  }
3194
27.2k
  subframe->quantization_level = i32;
3195
3196
  /* read quantized lp coefficiencts */
3197
317k
  for(u = 0; u < order; u++) {
3198
290k
    if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i32, subframe->qlp_coeff_precision))
3199
29
      return false; /* read_callback_ sets the state for us */
3200
290k
    subframe->qlp_coeff[u] = i32;
3201
290k
  }
3202
3203
  /* read entropy coding method info */
3204
27.2k
  if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN))
3205
196
    return false; /* read_callback_ sets the state for us */
3206
27.0k
  subframe->entropy_coding_method.type = (FLAC__EntropyCodingMethodType)u32;
3207
27.0k
  switch(subframe->entropy_coding_method.type) {
3208
21.1k
    case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
3209
25.5k
    case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2:
3210
25.5k
      if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
3211
69
        return false; /* read_callback_ sets the state for us */
3212
25.4k
      if((decoder->private_->frame.header.blocksize >> u32 < order) ||
3213
25.4k
         (decoder->private_->frame.header.blocksize % (1 << u32) > 0)) {
3214
1.56k
        send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
3215
1.56k
        decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
3216
1.56k
        return true;
3217
1.56k
      }
3218
23.8k
      subframe->entropy_coding_method.data.partitioned_rice.order = u32;
3219
23.8k
      subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel];
3220
23.8k
      break;
3221
1.54k
    default:
3222
1.54k
      send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
3223
1.54k
      decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
3224
1.54k
      return true;
3225
27.0k
  }
3226
3227
  /* read residual */
3228
23.8k
  switch(subframe->entropy_coding_method.type) {
3229
20.0k
    case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
3230
23.8k
    case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2:
3231
23.8k
      if(!read_residual_partitioned_rice_(decoder, order, subframe->entropy_coding_method.data.partitioned_rice.order, &decoder->private_->partitioned_rice_contents[channel], decoder->private_->residual[channel], /*is_extended=*/subframe->entropy_coding_method.type == FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2))
3232
767
        return false;
3233
23.1k
      break;
3234
23.1k
    default:
3235
0
      FLAC__ASSERT(0);
3236
23.8k
  }
3237
3238
  /* decode the subframe */
3239
23.1k
  if(do_full_decode) {
3240
22.8k
    if(bps <= 32) {
3241
22.1k
      uint32_t i;
3242
256k
      for(i = 0; i < order; i++)
3243
234k
        decoder->private_->output[channel][i] = subframe->warmup[i];
3244
22.1k
      if(FLAC__lpc_max_residual_bps(bps, subframe->qlp_coeff, order, subframe->quantization_level) <= 32 &&
3245
22.1k
         FLAC__lpc_max_prediction_before_shift_bps(bps, subframe->qlp_coeff, order) <= 32)
3246
15.0k
        FLAC__lpc_restore_signal(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private_->output[channel]+order);
3247
7.08k
      else
3248
7.08k
        FLAC__lpc_restore_signal_wide(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private_->output[channel]+order);
3249
22.1k
    }
3250
749
    else {
3251
749
      decoder->private_->side_subframe_in_use = true;
3252
749
      memcpy(decoder->private_->side_subframe, subframe->warmup, sizeof(FLAC__int64) * order);
3253
749
      FLAC__lpc_restore_signal_wide_33bit(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private_->side_subframe+order);
3254
749
    }
3255
22.8k
  }
3256
3257
23.1k
  return true;
3258
23.8k
}
3259
3260
FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, uint32_t channel, uint32_t bps, FLAC__bool do_full_decode)
3261
2.90k
{
3262
2.90k
  FLAC__Subframe_Verbatim *subframe = &decoder->private_->frame.subframes[channel].data.verbatim;
3263
2.90k
  uint32_t i;
3264
3265
2.90k
  decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_VERBATIM;
3266
3267
2.90k
  if(bps < 33) {
3268
2.36k
    FLAC__int32 x, *residual = decoder->private_->residual[channel];
3269
3270
2.36k
    subframe->data_type = FLAC__VERBATIM_SUBFRAME_DATA_TYPE_INT32;
3271
2.36k
    subframe->data.int32 = residual;
3272
3273
45.5k
    for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
3274
43.4k
      if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &x, bps))
3275
317
        return false; /* read_callback_ sets the state for us */
3276
43.1k
      residual[i] = x;
3277
43.1k
    }
3278
3279
    /* decode the subframe */
3280
2.04k
    if(do_full_decode)
3281
1.82k
      memcpy(decoder->private_->output[channel], subframe->data.int32, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize);
3282
2.04k
  }
3283
545
  else {
3284
545
    FLAC__int64 x, *side = decoder->private_->side_subframe;
3285
3286
545
    subframe->data_type = FLAC__VERBATIM_SUBFRAME_DATA_TYPE_INT64;
3287
545
    subframe->data.int64 = side;
3288
545
    decoder->private_->side_subframe_in_use = true;
3289
3290
4.14k
    for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
3291
3.83k
      if(!FLAC__bitreader_read_raw_int64(decoder->private_->input, &x, bps))
3292
237
        return false; /* read_callback_ sets the state for us */
3293
3.59k
      side[i] = x;
3294
3.59k
    }
3295
545
  }
3296
3297
2.35k
  return true;
3298
2.90k
}
3299
3300
FLAC__bool read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, uint32_t predictor_order, uint32_t partition_order, FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, FLAC__int32 *residual, FLAC__bool is_extended)
3301
43.0k
{
3302
43.0k
  FLAC__uint32 rice_parameter;
3303
43.0k
  int i;
3304
43.0k
  uint32_t partition, sample, u;
3305
43.0k
  const uint32_t partitions = 1u << partition_order;
3306
43.0k
  const uint32_t partition_samples = decoder->private_->frame.header.blocksize >> partition_order;
3307
43.0k
  const uint32_t plen = is_extended? FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_PARAMETER_LEN : FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
3308
43.0k
  const uint32_t pesc = is_extended? FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_ESCAPE_PARAMETER : FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
3309
3310
  /* invalid predictor and partition orders mush be handled in the callers */
3311
43.0k
  FLAC__ASSERT(partition_order > 0? partition_samples >= predictor_order : decoder->private_->frame.header.blocksize >= predictor_order);
3312
3313
43.0k
  if(!FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, flac_max(6u, partition_order))) {
3314
0
    decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
3315
0
    return false;
3316
0
  }
3317
3318
43.0k
  sample = 0;
3319
290k
  for(partition = 0; partition < partitions; partition++) {
3320
251k
    if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &rice_parameter, plen))
3321
104
      return false; /* read_callback_ sets the state for us */
3322
251k
    partitioned_rice_contents->parameters[partition] = rice_parameter;
3323
251k
    if(rice_parameter < pesc) {
3324
219k
      partitioned_rice_contents->raw_bits[partition] = 0;
3325
219k
      u = (partition == 0) ? partition_samples - predictor_order : partition_samples;
3326
219k
      if(!decoder->private_->local_bitreader_read_rice_signed_block(decoder->private_->input, residual + sample, u, rice_parameter)){
3327
3.25k
        if(decoder->protected_->state == FLAC__STREAM_DECODER_READ_FRAME) {
3328
          /* no error was set, read_callback_ didn't set it, so
3329
           * invalid rice symbol was found */
3330
933
          send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
3331
933
          decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
3332
933
          return true;
3333
933
        }
3334
2.32k
        else
3335
2.32k
          return false; /* read_callback_ sets the state for us */
3336
3.25k
      }
3337
216k
      sample += u;
3338
216k
    }
3339
31.7k
    else {
3340
31.7k
      if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN))
3341
265
        return false; /* read_callback_ sets the state for us */
3342
31.4k
      partitioned_rice_contents->raw_bits[partition] = rice_parameter;
3343
31.4k
      if(rice_parameter == 0) {
3344
13.8M
        for(u = (partition == 0)? predictor_order : 0; u < partition_samples; u++, sample++)
3345
13.7M
          residual[sample] = 0;
3346
2.71k
      }
3347
28.7k
      else{
3348
137k
        for(u = (partition == 0)? predictor_order : 0; u < partition_samples; u++, sample++) {
3349
108k
          if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i, rice_parameter))
3350
257
            return false; /* read_callback_ sets the state for us */
3351
108k
          residual[sample] = i;
3352
108k
        }
3353
28.7k
      }
3354
31.4k
    }
3355
251k
  }
3356
3357
39.1k
  return true;
3358
43.0k
}
3359
3360
FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder)
3361
64.9k
{
3362
64.9k
  if(!FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)) {
3363
44.7k
    FLAC__uint32 zero = 0;
3364
44.7k
    if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &zero, FLAC__bitreader_bits_left_for_byte_alignment(decoder->private_->input)))
3365
0
      return false; /* read_callback_ sets the state for us */
3366
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
3367
    if(zero != 0) {
3368
      send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
3369
      decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
3370
    }
3371
#endif
3372
44.7k
  }
3373
64.9k
  return true;
3374
64.9k
}
3375
3376
FLAC__bool read_callback_(FLAC__byte buffer[], size_t *bytes, void *client_data)
3377
200k
{
3378
200k
  FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder *)client_data;
3379
3380
200k
  if(
3381
200k
#if FLAC__HAS_OGG
3382
    /* see [1] HACK NOTE below for why we don't call the eof_callback when decoding Ogg FLAC */
3383
200k
    !decoder->private_->is_ogg &&
3384
200k
#endif
3385
200k
    decoder->private_->eof_callback && decoder->private_->eof_callback(decoder, decoder->private_->client_data)
3386
200k
  ) {
3387
0
    *bytes = 0;
3388
0
    decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
3389
0
    return false;
3390
0
  }
3391
200k
  else if(*bytes > 0) {
3392
    /* While seeking, it is possible for our seek to land in the
3393
     * middle of audio data that looks exactly like a frame header
3394
     * from a future version of an encoder.  When that happens, our
3395
     * error callback will get an
3396
     * FLAC__STREAM_DECODER_UNPARSEABLE_STREAM and increment its
3397
     * unparseable_frame_count.  But there is a remote possibility
3398
     * that it is properly synced at such a "future-codec frame",
3399
     * so to make sure, we wait to see many "unparseable" errors in
3400
     * a row before bailing out.
3401
     */
3402
200k
    if(decoder->private_->is_seeking && decoder->private_->unparseable_frame_count > 20) {
3403
0
      decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
3404
0
      return false;
3405
0
    }
3406
200k
    else {
3407
200k
      const FLAC__StreamDecoderReadStatus status =
3408
200k
#if FLAC__HAS_OGG
3409
200k
        decoder->private_->is_ogg?
3410
47.7k
        read_callback_ogg_aspect_(decoder, buffer, bytes) :
3411
200k
#endif
3412
200k
        decoder->private_->read_callback(decoder, buffer, bytes, decoder->private_->client_data)
3413
200k
      ;
3414
200k
      if(status == FLAC__STREAM_DECODER_READ_STATUS_ABORT) {
3415
69.8k
#if FLAC__HAS_OGG
3416
69.8k
        if(decoder->private_->ogg_decoder_aspect_allocation_failure) {
3417
0
          decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
3418
0
          return false;
3419
0
        }
3420
69.8k
#endif
3421
69.8k
        decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
3422
69.8k
        return false;
3423
69.8k
      }
3424
130k
      else if(*bytes == 0) {
3425
53.3k
        if(status == FLAC__STREAM_DECODER_READ_STATUS_END_OF_LINK) {
3426
4.06k
          if(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input))
3427
3.99k
            decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_LINK;
3428
69
          else
3429
69
            decoder->protected_->state = FLAC__STREAM_DECODER_OGG_ERROR;
3430
4.06k
          return false;
3431
4.06k
        }
3432
49.2k
        else if(
3433
49.2k
          status == FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM ||
3434
49.2k
          (
3435
49.2k
#if FLAC__HAS_OGG
3436
            /* see [1] HACK NOTE below for why we don't call the eof_callback when decoding Ogg FLAC */
3437
49.2k
            !decoder->private_->is_ogg &&
3438
49.2k
#endif
3439
49.2k
            decoder->private_->eof_callback && decoder->private_->eof_callback(decoder, decoder->private_->client_data)
3440
49.2k
          )
3441
49.2k
        ) {
3442
0
          decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
3443
0
          return false;
3444
0
        }
3445
49.2k
        else
3446
49.2k
          return true;
3447
53.3k
      }
3448
77.5k
      else
3449
77.5k
        return true;
3450
200k
    }
3451
200k
  }
3452
0
  else {
3453
    /* abort to avoid a deadlock */
3454
0
    decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
3455
0
    return false;
3456
0
  }
3457
  /* [1] @@@ HACK NOTE: The end-of-stream checking has to be hacked around
3458
   * for Ogg FLAC.  This is because the ogg decoder aspect can lose sync
3459
   * and at the same time hit the end of the stream (for example, seeking
3460
   * to a point that is after the beginning of the last Ogg page).  There
3461
   * is no way to report an Ogg sync loss through the callbacks (see note
3462
   * in read_callback_ogg_aspect_()) so it returns CONTINUE with *bytes==0.
3463
   * So to keep the decoder from stopping at this point we gate the call
3464
   * to the eof_callback and let the Ogg decoder aspect set the
3465
   * end-of-stream state when it is needed.
3466
   */
3467
200k
}
3468
3469
#if defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) && !defined(FUZZING_BUILD_MODE_FLAC_SANITIZE_SIGNED_INTEGER_OVERFLOW)
3470
/* The attribute below is to silence the undefined sanitizer of oss-fuzz.
3471
 * Because fuzzing feeds bogus predictors and residual samples to the
3472
 * decoder, having overflows in this section is unavoidable. Also,
3473
 * because the calculated values are audio path only, there is no
3474
 * potential for security problems */
3475
__attribute__((no_sanitize("signed-integer-overflow")))
3476
#endif
3477
22.8k
void undo_channel_coding(FLAC__StreamDecoder *decoder) {
3478
22.8k
  uint32_t i;
3479
22.8k
  switch(decoder->private_->frame.header.channel_assignment) {
3480
18.7k
  case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
3481
    /* do nothing */
3482
18.7k
    break;
3483
1.91k
  case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
3484
1.91k
    FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
3485
1.91k
    FLAC__ASSERT(decoder->private_->side_subframe_in_use != /* logical XOR */ (decoder->private_->frame.header.bits_per_sample < 32));
3486
2.22M
    for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
3487
2.21M
      if(decoder->private_->side_subframe_in_use)
3488
508k
        decoder->private_->output[1][i] = decoder->private_->output[0][i] - decoder->private_->side_subframe[i];
3489
1.70M
      else
3490
1.70M
        decoder->private_->output[1][i] = decoder->private_->output[0][i] - decoder->private_->output[1][i];
3491
1.91k
    break;
3492
1.48k
  case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
3493
1.48k
    FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
3494
1.48k
    FLAC__ASSERT(decoder->private_->side_subframe_in_use != /* logical XOR */ (decoder->private_->frame.header.bits_per_sample < 32));
3495
6.59M
    for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
3496
6.59M
      if(decoder->private_->side_subframe_in_use)
3497
1.20M
        decoder->private_->output[0][i] = decoder->private_->output[1][i] + decoder->private_->side_subframe[i];
3498
5.38M
      else
3499
5.38M
        decoder->private_->output[0][i] += decoder->private_->output[1][i];
3500
1.48k
    break;
3501
773
  case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
3502
773
    FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
3503
773
    FLAC__ASSERT(decoder->private_->side_subframe_in_use != /* logical XOR */ (decoder->private_->frame.header.bits_per_sample < 32));
3504
2.37M
    for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
3505
2.36M
      if(!decoder->private_->side_subframe_in_use){
3506
1.71M
        FLAC__int32 mid, side;
3507
1.71M
        mid = decoder->private_->output[0][i];
3508
1.71M
        side = decoder->private_->output[1][i];
3509
1.71M
        mid = ((uint32_t) mid) << 1;
3510
1.71M
        mid |= (side & 1); /* i.e. if 'side' is odd... */
3511
1.71M
        decoder->private_->output[0][i] = (mid + side) >> 1;
3512
1.71M
        decoder->private_->output[1][i] = (mid - side) >> 1;
3513
1.71M
      }
3514
652k
      else { /* bps == 32 */
3515
652k
        FLAC__int64 mid;
3516
652k
        mid = ((uint64_t)decoder->private_->output[0][i]) << 1;
3517
652k
        mid |= (decoder->private_->side_subframe[i] & 1); /* i.e. if 'side' is odd... */
3518
652k
        decoder->private_->output[0][i] = (mid + decoder->private_->side_subframe[i]) >> 1;
3519
652k
        decoder->private_->output[1][i] = (mid - decoder->private_->side_subframe[i]) >> 1;
3520
652k
      }
3521
2.36M
    }
3522
773
    break;
3523
0
  default:
3524
0
    FLAC__ASSERT(0);
3525
0
    break;
3526
22.8k
  }
3527
22.8k
}
3528
3529
#if FLAC__HAS_OGG
3530
FLAC__StreamDecoderReadStatus read_callback_ogg_aspect_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes)
3531
47.7k
{
3532
47.7k
  switch(FLAC__ogg_decoder_aspect_read_callback_wrapper(&decoder->protected_->ogg_decoder_aspect, buffer, bytes, read_callback_proxy_, decoder->private_->tell_callback, decoder, decoder->private_->client_data)) {
3533
403
    case FLAC__OGG_DECODER_ASPECT_READ_STATUS_OK:
3534
403
      return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
3535
    /* we don't really have a way to handle lost sync via read
3536
     * callback so we'll let it pass and let the underlying
3537
     * FLAC decoder catch the error
3538
     */
3539
35.4k
    case FLAC__OGG_DECODER_ASPECT_READ_STATUS_LOST_SYNC:
3540
35.4k
      return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
3541
0
    case FLAC__OGG_DECODER_ASPECT_READ_STATUS_END_OF_STREAM:
3542
0
      return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
3543
4.06k
    case FLAC__OGG_DECODER_ASPECT_READ_STATUS_END_OF_LINK:
3544
4.06k
      return FLAC__STREAM_DECODER_READ_STATUS_END_OF_LINK;
3545
579
    case FLAC__OGG_DECODER_ASPECT_READ_STATUS_NOT_FLAC:
3546
1.05k
    case FLAC__OGG_DECODER_ASPECT_READ_STATUS_UNSUPPORTED_MAPPING_VERSION:
3547
7.79k
    case FLAC__OGG_DECODER_ASPECT_READ_STATUS_ABORT:
3548
7.79k
    case FLAC__OGG_DECODER_ASPECT_READ_STATUS_ERROR:
3549
7.79k
      return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
3550
0
    case FLAC__OGG_DECODER_ASPECT_READ_STATUS_MEMORY_ALLOCATION_ERROR:
3551
0
      decoder->private_->ogg_decoder_aspect_allocation_failure = true;
3552
0
      return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
3553
0
    default:
3554
0
      FLAC__ASSERT(0);
3555
      /* double protection */
3556
0
      return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
3557
47.7k
  }
3558
47.7k
}
3559
3560
FLAC__OggDecoderAspectReadStatus read_callback_proxy_(const void *void_decoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
3561
23.4k
{
3562
23.4k
  FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder*)void_decoder;
3563
3564
23.4k
  switch(decoder->private_->read_callback(decoder, buffer, bytes, client_data)) {
3565
16.6k
    case FLAC__STREAM_DECODER_READ_STATUS_CONTINUE:
3566
16.6k
      return FLAC__OGG_DECODER_ASPECT_READ_STATUS_OK;
3567
0
    case FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM:
3568
0
      return FLAC__OGG_DECODER_ASPECT_READ_STATUS_END_OF_STREAM;
3569
6.74k
    case FLAC__STREAM_DECODER_READ_STATUS_ABORT:
3570
6.74k
      return FLAC__OGG_DECODER_ASPECT_READ_STATUS_ABORT;
3571
0
    default:
3572
      /* double protection: */
3573
0
      FLAC__ASSERT(0);
3574
0
      return FLAC__OGG_DECODER_ASPECT_READ_STATUS_ABORT;
3575
23.4k
  }
3576
23.4k
}
3577
#endif
3578
3579
FLAC__StreamDecoderWriteStatus write_audio_frame_to_client_(FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[])
3580
74.6k
{
3581
74.6k
  decoder->private_->last_frame = *frame; /* save the frame */
3582
74.6k
  decoder->private_->last_frame_is_set = true;
3583
74.6k
  if(decoder->private_->is_seeking && !decoder->private_->is_indexing) {
3584
0
    FLAC__uint64 this_frame_sample = frame->header.number.sample_number;
3585
0
    FLAC__uint64 next_frame_sample = this_frame_sample + (FLAC__uint64)frame->header.blocksize;
3586
0
    FLAC__uint64 target_sample = decoder->private_->target_sample;
3587
3588
0
    FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
3589
3590
0
    decoder->private_->got_a_frame = true;
3591
3592
0
    if(this_frame_sample <= target_sample && target_sample < next_frame_sample) { /* we hit our target frame */
3593
0
      uint32_t delta = (uint32_t)(target_sample - this_frame_sample);
3594
      /* kick out of seek mode */
3595
0
      decoder->private_->is_seeking = false;
3596
      /* shift out the samples before target_sample */
3597
0
      if(delta > 0) {
3598
0
        uint32_t channel;
3599
0
        const FLAC__int32 *newbuffer[FLAC__MAX_CHANNELS];
3600
0
        for(channel = 0; channel < frame->header.channels; channel++) {
3601
0
          newbuffer[channel] = buffer[channel] + delta;
3602
0
          decoder->private_->last_frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_VERBATIM;
3603
0
          decoder->private_->last_frame.subframes[channel].data.verbatim.data_type = FLAC__VERBATIM_SUBFRAME_DATA_TYPE_INT32;
3604
0
          decoder->private_->last_frame.subframes[channel].data.verbatim.data.int32 = newbuffer[channel];
3605
0
        }
3606
0
        decoder->private_->last_frame.header.blocksize -= delta;
3607
0
        decoder->private_->last_frame.header.number.sample_number += (FLAC__uint64)delta;
3608
        /* write the relevant samples */
3609
0
        return decoder->private_->write_callback(decoder, &decoder->private_->last_frame, newbuffer, decoder->private_->client_data);
3610
0
      }
3611
0
      else {
3612
        /* write the relevant samples */
3613
0
        return decoder->private_->write_callback(decoder, frame, buffer, decoder->private_->client_data);
3614
0
      }
3615
0
    }
3616
0
    else {
3617
0
      return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
3618
0
    }
3619
0
  }
3620
74.6k
  else if(!decoder->private_->is_indexing) {
3621
    /*
3622
     * If we never got STREAMINFO, turn off MD5 checking to save
3623
     * cycles since we don't have a sum to compare to anyway
3624
     */
3625
74.6k
    if(!decoder->private_->has_stream_info)
3626
53.3k
      decoder->private_->do_md5_checking = false;
3627
74.6k
    if(decoder->private_->do_md5_checking) {
3628
20.9k
      if(!FLAC__MD5Accumulate(&decoder->private_->md5context, buffer, frame->header.channels, frame->header.blocksize, (frame->header.bits_per_sample+7) / 8))
3629
0
        return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
3630
20.9k
    }
3631
74.6k
    return decoder->private_->write_callback(decoder, frame, buffer, decoder->private_->client_data);
3632
74.6k
  }
3633
0
  else { /* decoder->private_->is_indexing == true */
3634
0
    return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
3635
0
  }
3636
74.6k
}
3637
3638
void send_error_to_client_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status)
3639
252k
{
3640
252k
  if(!decoder->private_->is_seeking) {
3641
252k
    decoder->private_->error_has_been_sent = true;
3642
252k
    decoder->private_->error_callback(decoder, status, decoder->private_->client_data);
3643
252k
  }
3644
0
  else if(status == FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM)
3645
0
    decoder->private_->unparseable_frame_count++;
3646
252k
}
3647
3648
FLAC__bool seek_to_absolute_sample_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample)
3649
0
{
3650
0
  FLAC__uint64 first_frame_offset = decoder->private_->first_frame_offset, lower_bound, upper_bound, lower_bound_sample, upper_bound_sample, this_frame_sample;
3651
0
  FLAC__int64 pos = -1;
3652
0
  int i;
3653
0
  uint32_t approx_bytes_per_frame;
3654
0
  FLAC__bool first_seek = true, seek_from_lower_bound = false;
3655
0
  const FLAC__uint64 total_samples = FLAC__stream_decoder_get_total_samples(decoder);
3656
0
  const uint32_t min_blocksize = decoder->private_->stream_info.data.stream_info.min_blocksize;
3657
0
  const uint32_t max_blocksize = decoder->private_->stream_info.data.stream_info.max_blocksize;
3658
0
  const uint32_t max_framesize = decoder->private_->stream_info.data.stream_info.max_framesize;
3659
0
  const uint32_t min_framesize = decoder->private_->stream_info.data.stream_info.min_framesize;
3660
  /* take these from the current frame in case they've changed mid-stream */
3661
0
  uint32_t channels = FLAC__stream_decoder_get_channels(decoder);
3662
0
  uint32_t bps = FLAC__stream_decoder_get_bits_per_sample(decoder);
3663
0
  const FLAC__StreamMetadata_SeekTable *seek_table = decoder->private_->has_seek_table? &decoder->private_->seek_table.data.seek_table : 0;
3664
3665
  /* use values from stream info if we didn't decode a frame */
3666
0
  if(channels == 0)
3667
0
    channels = decoder->private_->stream_info.data.stream_info.channels;
3668
0
  if(bps == 0)
3669
0
    bps = decoder->private_->stream_info.data.stream_info.bits_per_sample;
3670
3671
  /* we are just guessing here */
3672
0
  if(max_framesize > 0)
3673
0
    approx_bytes_per_frame = (max_framesize + min_framesize) / 2 + 1;
3674
  /*
3675
   * Check if it's a known fixed-blocksize stream.  Note that though
3676
   * the spec doesn't allow zeroes in the STREAMINFO block, we may
3677
   * never get a STREAMINFO block when decoding so the value of
3678
   * min_blocksize might be zero.
3679
   */
3680
0
  else if(min_blocksize == max_blocksize && min_blocksize > 0) {
3681
    /* note there are no () around 'bps/8' to keep precision up since it's an integer calculation */
3682
0
    approx_bytes_per_frame = min_blocksize * channels * bps/8 + 64;
3683
0
  }
3684
0
  else
3685
0
    approx_bytes_per_frame = 4096 * channels * bps/8 + 64;
3686
3687
  /*
3688
   * First, we set an upper and lower bound on where in the
3689
   * stream we will search.  For now we take the current position
3690
   * as one bound and, depending on where the target position lies,
3691
   * the beginning of the first frame or the end of the stream as
3692
   * the other bound.
3693
   */
3694
0
  lower_bound = first_frame_offset;
3695
0
  lower_bound_sample = 0;
3696
0
  upper_bound = stream_length;
3697
0
  upper_bound_sample = total_samples > 0 ? total_samples : target_sample /*estimate it*/;
3698
3699
0
  if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC &&
3700
0
     decoder->private_->samples_decoded != 0) {
3701
0
    if(target_sample < decoder->private_->samples_decoded) {
3702
0
      if(FLAC__stream_decoder_get_decode_position(decoder, &upper_bound))
3703
0
        upper_bound_sample = decoder->private_->samples_decoded;
3704
0
    } else {
3705
0
      if(FLAC__stream_decoder_get_decode_position(decoder, &lower_bound))
3706
0
        lower_bound_sample = decoder->private_->samples_decoded;
3707
0
    }
3708
0
  }
3709
3710
  /*
3711
   * Now we refine the bounds if we have a seektable with
3712
   * suitable points.  Note that according to the spec they
3713
   * must be ordered by ascending sample number.
3714
   *
3715
   * Note: to protect against invalid seek tables we will ignore points
3716
   * that have frame_samples==0 or sample_number>=total_samples. Also,
3717
   * because math is limited to 64-bit ints, seekpoints with an offset
3718
   * larger than 2^63 (8 exbibyte) are rejected.
3719
   */
3720
0
  if(seek_table) {
3721
0
    FLAC__uint64 new_lower_bound = lower_bound;
3722
0
    FLAC__uint64 new_upper_bound = upper_bound;
3723
0
    FLAC__uint64 new_lower_bound_sample = lower_bound_sample;
3724
0
    FLAC__uint64 new_upper_bound_sample = upper_bound_sample;
3725
3726
    /* find the closest seek point <= target_sample, if it exists */
3727
0
    for(i = (int)seek_table->num_points - 1; i >= 0; i--) {
3728
0
      if(
3729
0
        seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER &&
3730
0
        seek_table->points[i].frame_samples > 0 && /* defense against bad seekpoints */
3731
0
        (total_samples <= 0 || seek_table->points[i].sample_number < total_samples) && /* defense against bad seekpoints */
3732
0
        seek_table->points[i].sample_number <= target_sample
3733
0
      )
3734
0
        break;
3735
0
    }
3736
0
    if(i >= 0) { /* i.e. we found a suitable seek point... */
3737
0
      new_lower_bound = first_frame_offset + seek_table->points[i].stream_offset;
3738
0
      new_lower_bound_sample = seek_table->points[i].sample_number;
3739
0
    }
3740
3741
    /* find the closest seek point > target_sample, if it exists */
3742
0
    for(i = 0; i < (int)seek_table->num_points; i++) {
3743
0
      if(
3744
0
        seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER &&
3745
0
        seek_table->points[i].frame_samples > 0 && /* defense against bad seekpoints */
3746
0
        (total_samples <= 0 || seek_table->points[i].sample_number < total_samples) && /* defense against bad seekpoints */
3747
0
        seek_table->points[i].sample_number > target_sample
3748
0
      )
3749
0
        break;
3750
0
    }
3751
0
    if(i < (int)seek_table->num_points) { /* i.e. we found a suitable seek point... */
3752
0
      new_upper_bound = first_frame_offset + seek_table->points[i].stream_offset;
3753
0
      new_upper_bound_sample = seek_table->points[i].sample_number;
3754
0
    }
3755
    /* final protection against unsorted seek tables; keep original values if bogus */
3756
0
    if(new_upper_bound >= new_lower_bound) {
3757
0
      lower_bound = new_lower_bound;
3758
0
      upper_bound = new_upper_bound;
3759
0
      lower_bound_sample = new_lower_bound_sample;
3760
0
      upper_bound_sample = new_upper_bound_sample;
3761
0
    }
3762
0
  }
3763
3764
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
3765
  FLAC__ASSERT(upper_bound_sample >= lower_bound_sample);
3766
#endif
3767
  /* there are 3 insidious ways that the following equality occurs, which
3768
   * we need to fix:
3769
   *  1) total_samples is 0 (unknown) and target_sample is 0
3770
   *  2) total_samples is 0 (unknown) and target_sample happens to be
3771
   *     exactly equal to the last seek point in the seek table; this
3772
   *     means there is no seek point above it, and upper_bound_samples
3773
   *     remains equal to the estimate (of target_samples) we made above
3774
   * in either case it does not hurt to move upper_bound_sample up by 1
3775
   *  3) the file is corrupt
3776
   */
3777
0
  if(upper_bound_sample == lower_bound_sample) {
3778
0
    if(total_samples == 0)
3779
0
      upper_bound_sample++;
3780
0
    else {
3781
0
      decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3782
0
      return false;
3783
0
    }
3784
0
  }
3785
3786
0
  decoder->private_->target_sample = target_sample;
3787
0
  while(1) {
3788
    /* check whether decoder is still valid so bad state isn't overwritten
3789
     * with seek error */
3790
0
    if(decoder->protected_->state == FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR ||
3791
0
       decoder->protected_->state == FLAC__STREAM_DECODER_ABORTED)
3792
0
      return false;
3793
    /* check if the bounds are still ok */
3794
0
    if (lower_bound_sample >= upper_bound_sample ||
3795
0
        lower_bound > upper_bound ||
3796
0
        upper_bound >= INT64_MAX ||
3797
0
        target_sample > upper_bound_sample) {
3798
0
      decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3799
0
      return false;
3800
0
    }
3801
0
    if(seek_from_lower_bound) {
3802
0
      pos = lower_bound;
3803
0
    }
3804
0
    else {
3805
0
#ifndef FLAC__INTEGER_ONLY_LIBRARY
3806
0
      pos = (FLAC__int64)lower_bound + (FLAC__int64)((double)(target_sample - lower_bound_sample) / (double)(upper_bound_sample - lower_bound_sample) * (double)(upper_bound - lower_bound)) - approx_bytes_per_frame;
3807
#else
3808
      /* a little less accurate: */
3809
      if(upper_bound - lower_bound < 0xffffffff)
3810
        pos = (FLAC__int64)lower_bound + (FLAC__int64)(((target_sample - lower_bound_sample) * (upper_bound - lower_bound)) / (upper_bound_sample - lower_bound_sample)) - approx_bytes_per_frame;
3811
      else { /* @@@ WATCHOUT, ~2TB limit */
3812
              FLAC__uint64 ratio = (1<<16) / (upper_bound_sample - lower_bound_sample);
3813
        pos = (FLAC__int64)lower_bound + (FLAC__int64)((((target_sample - lower_bound_sample)>>8) * ((upper_bound - lower_bound)>>8) * ratio)) - approx_bytes_per_frame;
3814
      }
3815
#endif
3816
0
    }
3817
0
    if(pos >= (FLAC__int64)upper_bound)
3818
0
      pos = (FLAC__int64)upper_bound - 1;
3819
0
    if(pos < (FLAC__int64)lower_bound)
3820
0
      pos = (FLAC__int64)lower_bound;
3821
0
    if(decoder->private_->seek_callback(decoder, (FLAC__uint64)pos, decoder->private_->client_data) != FLAC__STREAM_DECODER_SEEK_STATUS_OK) {
3822
0
      decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3823
0
      return false;
3824
0
    }
3825
0
    if(!FLAC__stream_decoder_flush(decoder)) {
3826
      /* above call sets the state for us */
3827
0
      return false;
3828
0
    }
3829
    /* Now we need to get a frame.  First we need to reset our
3830
     * unparseable_frame_count; if we get too many unparseable
3831
     * frames in a row, the read callback will return
3832
     * FLAC__STREAM_DECODER_READ_STATUS_ABORT, causing
3833
     * FLAC__stream_decoder_process_single() to return false.
3834
     */
3835
0
    decoder->private_->unparseable_frame_count = 0;
3836
0
    if(!FLAC__stream_decoder_process_single(decoder) || decoder->protected_->state == FLAC__STREAM_DECODER_ABORTED || 0 == decoder->private_->samples_decoded) {
3837
      /* No frame could be decoded */
3838
0
      if(decoder->protected_->state != FLAC__STREAM_DECODER_ABORTED && decoder->private_->eof_callback(decoder, decoder->private_->client_data) && !seek_from_lower_bound){
3839
        /* decoder has hit end of stream while processing corrupt
3840
         * frame. To remedy this, try decoding a frame at the lower
3841
         * bound so the seek after that hopefully ends up somewhere
3842
         * else */
3843
0
        seek_from_lower_bound = true;
3844
0
        continue;
3845
0
      }
3846
0
      else {
3847
0
        if(decoder->protected_->state != FLAC__STREAM_DECODER_ABORTED && decoder->protected_->state != FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR)
3848
0
          decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3849
0
        return false;
3850
0
      }
3851
0
    }
3852
0
    seek_from_lower_bound = false;
3853
3854
    /* our write callback will change the state when it gets to the target frame */
3855
    /* actually, we could have got_a_frame if our decoder is at FLAC__STREAM_DECODER_END_OF_STREAM so we need to check for that also */
3856
0
    if(!decoder->private_->is_seeking)
3857
0
      break;
3858
3859
0
    FLAC__ASSERT(decoder->private_->last_frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
3860
0
    this_frame_sample = decoder->private_->last_frame.header.number.sample_number;
3861
3862
0
    if(this_frame_sample + decoder->private_->last_frame.header.blocksize >= upper_bound_sample && !first_seek) {
3863
0
      if (pos == (FLAC__int64)lower_bound) {
3864
        /* can't move back any more than the first frame, something is fatally wrong */
3865
0
        decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3866
0
        return false;
3867
0
      }
3868
      /* our last move backwards wasn't big enough, try again */
3869
0
      approx_bytes_per_frame = approx_bytes_per_frame? approx_bytes_per_frame * 2 : 16;
3870
0
      continue;
3871
0
    }
3872
    /* allow one seek over upper bound, so we can get a correct upper_bound_sample for streams with unknown total_samples */
3873
0
    first_seek = false;
3874
3875
    /* make sure we are not seeking in corrupted stream */
3876
0
    if (this_frame_sample < lower_bound_sample) {
3877
0
      decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3878
0
      return false;
3879
0
    }
3880
3881
    /* we need to narrow the search */
3882
0
    if(target_sample < this_frame_sample) {
3883
0
      upper_bound_sample = this_frame_sample + decoder->private_->last_frame.header.blocksize;
3884
/*@@@@@@ what will decode position be if at end of stream? */
3885
0
      if(!FLAC__stream_decoder_get_decode_position(decoder, &upper_bound)) {
3886
0
        decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3887
0
        return false;
3888
0
      }
3889
0
      approx_bytes_per_frame = (uint32_t)(2 * (upper_bound - pos) / 3 + 16);
3890
0
    }
3891
0
    else { /* target_sample >= this_frame_sample + this frame's blocksize */
3892
0
      lower_bound_sample = this_frame_sample + decoder->private_->last_frame.header.blocksize;
3893
0
      if(!FLAC__stream_decoder_get_decode_position(decoder, &lower_bound)) {
3894
0
        decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3895
0
        return false;
3896
0
      }
3897
0
      approx_bytes_per_frame = (uint32_t)(2 * (lower_bound - pos) / 3 + 16);
3898
0
    }
3899
0
  }
3900
3901
0
  return true;
3902
0
}
3903
3904
#if FLAC__HAS_OGG
3905
FLAC__bool seek_to_absolute_sample_ogg_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample)
3906
0
{
3907
0
  FLAC__uint64 left_pos = 0, right_pos = stream_length;
3908
0
  FLAC__uint64 left_sample = 0, right_sample = 0;
3909
0
  FLAC__uint64 this_frame_sample = (FLAC__uint64)0 - 1;
3910
0
  FLAC__uint64 pos = 0; /* only initialized to avoid compiler warning */
3911
0
  FLAC__bool did_a_seek;
3912
0
  FLAC__OggDecoderAspect_TargetLink *target_link;
3913
0
  uint32_t current_linknumber = decoder->protected_->ogg_decoder_aspect.current_linknumber;
3914
0
  uint32_t iteration = 0;
3915
3916
  /* In the first iterations, we will calculate the target byte position
3917
   * by the distance from the target sample to left_sample and
3918
   * right_sample (let's call it "proportional search").  After that, we
3919
   * will switch to binary search.
3920
   */
3921
0
  uint32_t BINARY_SEARCH_AFTER_ITERATION = 2;
3922
3923
  /* We will switch to a linear search once our current sample is less
3924
   * than this number of samples ahead of the target sample
3925
   */
3926
0
  static const FLAC__uint64 LINEAR_SEARCH_WITHIN_SAMPLES = FLAC__MAX_BLOCK_SIZE * 2;
3927
3928
0
  if(FLAC__ogg_decoder_aspect_get_decode_chained_stream(&decoder->protected_->ogg_decoder_aspect)) {
3929
    /* Check whether target sample is contained by indexed links.
3930
     * If it is not keep moving forward until found */
3931
0
    decoder->private_->is_indexing = true;
3932
0
    while(NULL == (target_link = FLAC__ogg_decoder_aspect_get_target_link(&decoder->protected_->ogg_decoder_aspect, target_sample))) {
3933
0
      FLAC__OggDecoderAspectReadStatus status;
3934
0
      if(decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM ||
3935
0
         decoder->protected_->state == FLAC__STREAM_DECODER_OGG_ERROR ||
3936
0
         decoder->protected_->state == FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR ||
3937
0
         decoder->protected_->state == FLAC__STREAM_DECODER_ABORTED) {
3938
        /* Target sample is not contained in stream or other error */
3939
0
        decoder->private_->is_indexing = false;
3940
0
        decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3941
0
        return false;
3942
0
      }
3943
0
      status = FLAC__ogg_decoder_aspect_skip_link(&decoder->protected_->ogg_decoder_aspect, read_callback_proxy_, decoder->private_->seek_callback, decoder->private_->tell_callback, decoder->private_->length_callback, decoder, decoder->private_->client_data);
3944
0
      if(status == FLAC__OGG_DECODER_ASPECT_READ_STATUS_END_OF_STREAM)
3945
0
        decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
3946
0
      else if(status != FLAC__OGG_DECODER_ASPECT_READ_STATUS_OK) {
3947
0
        decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3948
0
        return false;
3949
0
      }
3950
0
    }
3951
0
    decoder->private_->is_indexing = false;
3952
    /* Target link found, send metadata if necessary */
3953
0
    FLAC__ogg_decoder_aspect_set_seek_parameters(&decoder->protected_->ogg_decoder_aspect, target_link);
3954
0
    if(target_link->linknumber != current_linknumber) {
3955
0
      if(decoder->private_->seek_callback((FLAC__StreamDecoder*)decoder, (FLAC__uint64)target_link->start_byte, decoder->private_->client_data) != FLAC__STREAM_DECODER_SEEK_STATUS_OK) {
3956
0
        decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3957
0
        return false;
3958
0
      }
3959
0
      if(!FLAC__stream_decoder_flush(decoder)) {
3960
        /* above call sets the state for us */
3961
0
        return false;
3962
0
      }
3963
0
      decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
3964
0
      decoder->private_->is_seeking = false;
3965
0
      if(!FLAC__stream_decoder_process_until_end_of_metadata(decoder)) {
3966
0
        decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3967
0
        return false;
3968
0
      }
3969
0
      decoder->private_->is_seeking = true;
3970
0
    }
3971
    /* set boundaries */
3972
0
    left_pos = target_link->start_byte;
3973
0
    left_sample = 0;
3974
0
    right_pos = target_link->end_byte;
3975
0
    right_sample = target_link->samples_this_link;
3976
0
    target_sample -= target_link->samples_in_preceding_links;
3977
0
  }
3978
0
  else {
3979
0
    right_sample = FLAC__stream_decoder_get_total_samples(decoder);
3980
3981
    /* If the total number of samples is unknown, use a large value, and
3982
     * force binary search immediately.
3983
     */
3984
0
    if(right_sample == 0) {
3985
0
      right_sample = (FLAC__uint64)(-1);
3986
0
      BINARY_SEARCH_AFTER_ITERATION = 0;
3987
0
    }
3988
0
  }
3989
3990
0
  decoder->private_->target_sample = target_sample;
3991
0
  for( ; ; iteration++) {
3992
    /* Do sanity checks on bounds */
3993
0
    if(right_pos <= left_pos || right_pos - left_pos < 9) {
3994
      /* FLAC frame is at least 9 byte in size */
3995
0
      decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3996
0
      return false;
3997
0
    }
3998
0
    if (iteration == 0 || this_frame_sample > target_sample || target_sample - this_frame_sample > LINEAR_SEARCH_WITHIN_SAMPLES) {
3999
0
      if (iteration >= BINARY_SEARCH_AFTER_ITERATION) {
4000
0
        pos = (right_pos + left_pos) / 2;
4001
0
      }
4002
0
      else {
4003
0
#ifndef FLAC__INTEGER_ONLY_LIBRARY
4004
0
        pos = (FLAC__uint64)((double)(target_sample - left_sample) / (double)(right_sample - left_sample) * (double)(right_pos - left_pos)) + left_pos;
4005
#else
4006
        /* a little less accurate: */
4007
        if ((target_sample-left_sample <= 0xffffffff) && (right_pos-left_pos <= 0xffffffff))
4008
          pos = (FLAC__int64)(((target_sample-left_sample) * (right_pos-left_pos)) / (right_sample-left_sample)) + left_pos;
4009
        else /* @@@ WATCHOUT, ~2TB limit */
4010
          pos = (FLAC__int64)((((target_sample-left_sample)>>8) * ((right_pos-left_pos)>>8)) / ((right_sample-left_sample)>>16)) + left_pos;
4011
#endif
4012
        /* @@@ TODO: might want to limit pos to some distance
4013
         * before EOF, to make sure we land before the last frame,
4014
         * thereby getting a this_frame_sample and so having a better
4015
         * estimate.
4016
         */
4017
0
      }
4018
4019
      /* physical seek */
4020
0
      if(decoder->private_->seek_callback((FLAC__StreamDecoder*)decoder, (FLAC__uint64)pos, decoder->private_->client_data) != FLAC__STREAM_DECODER_SEEK_STATUS_OK) {
4021
0
        decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
4022
0
        return false;
4023
0
      }
4024
0
      if(!FLAC__stream_decoder_flush(decoder)) {
4025
        /* above call sets the state for us */
4026
0
        return false;
4027
0
      }
4028
0
      did_a_seek = true;
4029
0
    }
4030
0
    else
4031
0
      did_a_seek = false;
4032
4033
0
    decoder->private_->got_a_frame = false;
4034
0
    if(!FLAC__stream_decoder_process_single(decoder) ||
4035
0
       decoder->protected_->state == FLAC__STREAM_DECODER_ABORTED) {
4036
0
      if(decoder->protected_->state != FLAC__STREAM_DECODER_ABORTED && decoder->protected_->state != FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR)
4037
0
        decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
4038
0
      return false;
4039
0
    }
4040
0
    if(!decoder->private_->got_a_frame) {
4041
0
      if(did_a_seek) {
4042
        /* this can happen if we seek to a point after the last frame; we drop
4043
         * to binary search right away in this case to avoid any wasted
4044
         * iterations of proportional search.
4045
         */
4046
0
        right_pos = pos;
4047
0
        BINARY_SEARCH_AFTER_ITERATION = 0;
4048
0
      }
4049
0
      else {
4050
        /* this can probably only happen if total_samples is unknown and the
4051
         * target_sample is past the end of the stream
4052
         */
4053
0
        decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
4054
0
        return false;
4055
0
      }
4056
0
    }
4057
    /* our write callback will change the state when it gets to the target frame */
4058
0
    else if(!decoder->private_->is_seeking) {
4059
0
      break;
4060
0
    }
4061
0
    else {
4062
0
      this_frame_sample = decoder->private_->last_frame.header.number.sample_number;
4063
0
      FLAC__ASSERT(decoder->private_->last_frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
4064
4065
0
      if (did_a_seek) {
4066
0
        if (this_frame_sample <= target_sample) {
4067
          /* The 'equal' case should not happen, since
4068
           * FLAC__stream_decoder_process_single()
4069
           * should recognize that it has hit the
4070
           * target sample and we would exit through
4071
           * the 'break' above.
4072
           */
4073
0
          FLAC__ASSERT(this_frame_sample != target_sample);
4074
4075
0
          left_sample = this_frame_sample;
4076
          /* sanity check to avoid infinite loop */
4077
0
          if (left_pos == pos) {
4078
0
            decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
4079
0
            return false;
4080
0
          }
4081
0
          left_pos = pos;
4082
0
        }
4083
0
        else {
4084
0
          right_sample = this_frame_sample;
4085
          /* sanity check to avoid infinite loop */
4086
0
          if (right_pos == pos) {
4087
0
            decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
4088
0
            return false;
4089
0
          }
4090
0
          right_pos = pos;
4091
0
        }
4092
0
      }
4093
0
    }
4094
0
  }
4095
  /* Tell ogg decoder aspect that seeking is done */
4096
0
  FLAC__ogg_decoder_aspect_set_seek_parameters(&decoder->protected_->ogg_decoder_aspect, 0);
4097
0
  return true;
4098
0
}
4099
#endif
4100
4101
FLAC__StreamDecoderReadStatus file_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
4102
0
{
4103
0
  (void)client_data;
4104
4105
0
  if(*bytes > 0) {
4106
0
    *bytes = fread(buffer, sizeof(FLAC__byte), *bytes, decoder->private_->file);
4107
0
    if(ferror(decoder->private_->file))
4108
0
      return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
4109
0
    else if(*bytes == 0)
4110
0
      return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
4111
0
    else
4112
0
      return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
4113
0
  }
4114
0
  else
4115
0
    return FLAC__STREAM_DECODER_READ_STATUS_ABORT; /* abort to avoid a deadlock */
4116
0
}
4117
4118
FLAC__StreamDecoderSeekStatus file_seek_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data)
4119
0
{
4120
0
  (void)client_data;
4121
4122
0
  if(decoder->private_->file == stdin)
4123
0
    return FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED;
4124
0
  else if(fseeko(decoder->private_->file, (FLAC__off_t)absolute_byte_offset, SEEK_SET) < 0)
4125
0
    return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR;
4126
0
  else
4127
0
    return FLAC__STREAM_DECODER_SEEK_STATUS_OK;
4128
0
}
4129
4130
FLAC__StreamDecoderTellStatus file_tell_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
4131
0
{
4132
0
  FLAC__off_t pos;
4133
0
  (void)client_data;
4134
4135
0
  if(decoder->private_->file == stdin)
4136
0
    return FLAC__STREAM_DECODER_TELL_STATUS_UNSUPPORTED;
4137
0
  else if((pos = ftello(decoder->private_->file)) < 0)
4138
0
    return FLAC__STREAM_DECODER_TELL_STATUS_ERROR;
4139
0
  else {
4140
0
    *absolute_byte_offset = (FLAC__uint64)pos;
4141
0
    return FLAC__STREAM_DECODER_TELL_STATUS_OK;
4142
0
  }
4143
0
}
4144
4145
FLAC__StreamDecoderLengthStatus file_length_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data)
4146
0
{
4147
0
  struct flac_stat_s filestats;
4148
0
  (void)client_data;
4149
4150
0
  if(decoder->private_->file == stdin)
4151
0
    return FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED;
4152
4153
0
#ifndef FLAC__USE_FILELENGTHI64
4154
0
  if(flac_fstat(fileno(decoder->private_->file), &filestats) != 0)
4155
#else
4156
  filestats.st_size = _filelengthi64(fileno(decoder->private_->file));
4157
  if(filestats.st_size < 0)
4158
#endif
4159
0
    return FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR;
4160
0
  else {
4161
0
    *stream_length = (FLAC__uint64)filestats.st_size;
4162
0
    return FLAC__STREAM_DECODER_LENGTH_STATUS_OK;
4163
0
  }
4164
0
}
4165
4166
FLAC__bool file_eof_callback_(const FLAC__StreamDecoder *decoder, void *client_data)
4167
0
{
4168
0
  (void)client_data;
4169
4170
0
  return feof(decoder->private_->file)? true : false;
4171
0
}