Coverage Report

Created: 2026-08-31 06:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/flac/src/libFLAC/stream_decoder.c
Line
Count
Source
1
/* libFLAC - Free Lossless Audio Codec library
2
 * Copyright (C) 2000-2009  Josh Coalson
3
 * Copyright (C) 2011-2025  Xiph.Org Foundation
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 * notice, this list of conditions and the following disclaimer.
11
 *
12
 * - Redistributions in binary form must reproduce the above copyright
13
 * notice, this list of conditions and the following disclaimer in the
14
 * documentation and/or other materials provided with the distribution.
15
 *
16
 * - Neither the name of the Xiph.org Foundation nor the names of its
17
 * contributors may be used to endorse or promote products derived from
18
 * this software without specific prior written permission.
19
 *
20
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23
 * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
24
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
 */
32
33
#ifdef HAVE_CONFIG_H
34
#  include <config.h>
35
#endif
36
37
#include <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.66k
{
366
8.66k
  FLAC__ASSERT(0 != decoder);
367
368
8.66k
  if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
369
0
    return FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED;
370
371
8.66k
  if(FLAC__HAS_OGG == 0 && is_ogg)
372
0
    return FLAC__STREAM_DECODER_INIT_STATUS_UNSUPPORTED_CONTAINER;
373
374
8.66k
  if(
375
8.66k
    0 == read_callback ||
376
8.66k
    0 == write_callback ||
377
8.66k
    0 == error_callback ||
378
8.66k
    (seek_callback && (0 == tell_callback || 0 == length_callback || 0 == eof_callback))
379
8.66k
  )
380
0
    return FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS;
381
382
8.66k
#if FLAC__HAS_OGG
383
8.66k
  decoder->private_->is_ogg = is_ogg;
384
8.66k
  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.66k
#endif
389
390
8.66k
  FLAC__cpu_info(&decoder->private_->cpuinfo);
391
8.66k
  decoder->private_->local_bitreader_read_rice_signed_block = FLAC__bitreader_read_rice_signed_block;
392
393
8.66k
#ifdef FLAC__BMI2_SUPPORTED
394
8.66k
  if (decoder->private_->cpuinfo.x86.bmi2) {
395
8.66k
    decoder->private_->local_bitreader_read_rice_signed_block = FLAC__bitreader_read_rice_signed_block_bmi2;
396
8.66k
  }
397
8.66k
#endif
398
399
  /* from here on, errors are fatal */
400
401
8.66k
  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.66k
  decoder->private_->read_callback = read_callback;
407
8.66k
  decoder->private_->seek_callback = seek_callback;
408
8.66k
  decoder->private_->tell_callback = tell_callback;
409
8.66k
  decoder->private_->length_callback = length_callback;
410
8.66k
  decoder->private_->eof_callback = eof_callback;
411
8.66k
  decoder->private_->write_callback = write_callback;
412
8.66k
  decoder->private_->metadata_callback = metadata_callback;
413
8.66k
  decoder->private_->error_callback = error_callback;
414
8.66k
  decoder->private_->client_data = client_data;
415
8.66k
  decoder->private_->fixed_block_size = decoder->private_->next_fixed_block_size = 0;
416
8.66k
  decoder->private_->samples_decoded = 0;
417
8.66k
  decoder->private_->has_stream_info = false;
418
8.66k
  decoder->private_->cached = false;
419
420
8.66k
  decoder->private_->do_md5_checking = decoder->protected_->md5_checking;
421
8.66k
  decoder->private_->is_seeking = false;
422
8.66k
  decoder->private_->is_indexing = false;
423
424
8.66k
  decoder->private_->internal_reset_hack = true; /* so the following reset does not try to rewind the input */
425
8.66k
  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.66k
  return FLAC__STREAM_DECODER_INIT_STATUS_OK;
431
8.66k
}
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
8.66k
{
446
8.66k
  return init_stream_internal_(
447
8.66k
    decoder,
448
8.66k
    read_callback,
449
8.66k
    seek_callback,
450
8.66k
    tell_callback,
451
8.66k
    length_callback,
452
8.66k
    eof_callback,
453
8.66k
    write_callback,
454
8.66k
    metadata_callback,
455
8.66k
    error_callback,
456
8.66k
    client_data,
457
8.66k
    /*is_ogg=*/false
458
8.66k
  );
459
8.66k
}
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
0
{
474
0
  return init_stream_internal_(
475
0
    decoder,
476
0
    read_callback,
477
0
    seek_callback,
478
0
    tell_callback,
479
0
    length_callback,
480
0
    eof_callback,
481
0
    write_callback,
482
0
    metadata_callback,
483
0
    error_callback,
484
0
    client_data,
485
0
    /*is_ogg=*/true
486
0
  );
487
0
}
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
17.3k
{
616
17.3k
  FLAC__bool md5_failed = false;
617
17.3k
  uint32_t i;
618
619
17.3k
  FLAC__ASSERT(0 != decoder);
620
17.3k
  FLAC__ASSERT(0 != decoder->private_);
621
17.3k
  FLAC__ASSERT(0 != decoder->protected_);
622
623
17.3k
  if(decoder->protected_->state == FLAC__STREAM_DECODER_UNINITIALIZED)
624
8.66k
    return true;
625
626
  /* see the comment in FLAC__stream_decoder_reset() as to why we
627
   * always call FLAC__MD5Final()
628
   */
629
8.66k
  FLAC__MD5Final(decoder->private_->computed_md5sum, &decoder->private_->md5context);
630
631
8.66k
  free(decoder->private_->seek_table.data.seek_table.points);
632
8.66k
  decoder->private_->seek_table.data.seek_table.points = 0;
633
8.66k
  decoder->private_->has_seek_table = false;
634
635
8.66k
  FLAC__bitreader_free(decoder->private_->input);
636
77.9k
  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
69.3k
    if(0 != decoder->private_->output[i]) {
644
14.0k
      free(decoder->private_->output[i]-4);
645
14.0k
      decoder->private_->output[i] = 0;
646
14.0k
    }
647
69.3k
    if(0 != decoder->private_->residual_unaligned[i]) {
648
14.0k
      free(decoder->private_->residual_unaligned[i]);
649
14.0k
      decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0;
650
14.0k
    }
651
69.3k
  }
652
8.66k
  if(0 != decoder->private_->side_subframe) {
653
5.38k
    free(decoder->private_->side_subframe);
654
5.38k
    decoder->private_->side_subframe = 0;
655
5.38k
  }
656
8.66k
  decoder->private_->output_capacity = 0;
657
8.66k
  decoder->private_->output_channels = 0;
658
659
8.66k
#if FLAC__HAS_OGG
660
8.66k
  if(decoder->private_->is_ogg)
661
0
    FLAC__ogg_decoder_aspect_finish(&decoder->protected_->ogg_decoder_aspect);
662
8.66k
#endif
663
664
8.66k
  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.66k
  if(decoder->private_->do_md5_checking) {
671
0
    if(memcmp(decoder->private_->stream_info.data.stream_info.md5sum, decoder->private_->computed_md5sum, 16))
672
0
      md5_failed = true;
673
0
  }
674
8.66k
  decoder->private_->is_seeking = false;
675
8.66k
  decoder->private_->is_indexing = false;
676
677
8.66k
  set_defaults_(decoder);
678
679
8.66k
  decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
680
681
8.66k
  return !md5_failed;
682
17.3k
}
683
684
FLAC_API FLAC__bool FLAC__stream_decoder_set_ogg_serial_number(FLAC__StreamDecoder *decoder, long value)
685
0
{
686
0
  FLAC__ASSERT(0 != decoder);
687
0
  FLAC__ASSERT(0 != decoder->private_);
688
0
  FLAC__ASSERT(0 != decoder->protected_);
689
0
  if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
690
0
    return false;
691
0
#if FLAC__HAS_OGG
692
  /* can't check decoder->private_->is_ogg since that's not set until init time */
693
0
  FLAC__ogg_decoder_aspect_set_serial_number(&decoder->protected_->ogg_decoder_aspect, value);
694
0
  return true;
695
#else
696
  (void)value;
697
  return false;
698
#endif
699
0
}
700
701
FLAC_API FLAC__bool FLAC__stream_decoder_set_decode_chained_stream(FLAC__StreamDecoder* decoder, FLAC__bool value)
702
0
{
703
0
  FLAC__ASSERT(0 != decoder);
704
0
  FLAC__ASSERT(0 != decoder->protected_);
705
0
  if (decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
706
0
    return false;
707
0
#if FLAC__HAS_OGG
708
0
  FLAC__ogg_decoder_aspect_set_decode_chained_stream(&decoder->protected_->ogg_decoder_aspect, value);
709
0
  return true;
710
#else
711
  (void)value;
712
  return false;
713
#endif
714
0
}
715
716
FLAC_API FLAC__bool FLAC__stream_decoder_set_md5_checking(FLAC__StreamDecoder *decoder, FLAC__bool value)
717
0
{
718
0
  FLAC__ASSERT(0 != decoder);
719
0
  FLAC__ASSERT(0 != decoder->protected_);
720
0
  if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
721
0
    return false;
722
0
  decoder->protected_->md5_checking = value;
723
0
  return true;
724
0
}
725
726
FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
727
0
{
728
0
  FLAC__ASSERT(0 != decoder);
729
0
  FLAC__ASSERT(0 != decoder->private_);
730
0
  FLAC__ASSERT(0 != decoder->protected_);
731
0
  FLAC__ASSERT((uint32_t)type <= FLAC__MAX_METADATA_TYPE_CODE);
732
  /* double protection */
733
0
  if((uint32_t)type > FLAC__MAX_METADATA_TYPE_CODE)
734
0
    return false;
735
0
  decoder->private_->metadata_filter[type] = true;
736
0
  if(type == FLAC__METADATA_TYPE_APPLICATION)
737
0
    decoder->private_->metadata_filter_ids_count = 0;
738
0
  return true;
739
0
}
740
741
FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
742
0
{
743
0
  FLAC__ASSERT(0 != decoder);
744
0
  FLAC__ASSERT(0 != decoder->private_);
745
0
  FLAC__ASSERT(0 != decoder->protected_);
746
0
  FLAC__ASSERT(0 != id);
747
748
0
  if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION])
749
0
    return true;
750
751
0
  FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);
752
753
0
  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
0
  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
0
  decoder->private_->metadata_filter_ids_count++;
763
764
0
  return true;
765
0
}
766
767
FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_all(FLAC__StreamDecoder *decoder)
768
0
{
769
0
  uint32_t i;
770
0
  FLAC__ASSERT(0 != decoder);
771
0
  FLAC__ASSERT(0 != decoder->private_);
772
0
  FLAC__ASSERT(0 != decoder->protected_);
773
0
  for(i = 0; i < sizeof(decoder->private_->metadata_filter) / sizeof(decoder->private_->metadata_filter[0]); i++)
774
0
    decoder->private_->metadata_filter[i] = true;
775
0
  decoder->private_->metadata_filter_ids_count = 0;
776
0
  return true;
777
0
}
778
779
FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
780
0
{
781
0
  FLAC__ASSERT(0 != decoder);
782
0
  FLAC__ASSERT(0 != decoder->private_);
783
0
  FLAC__ASSERT(0 != decoder->protected_);
784
0
  FLAC__ASSERT((uint32_t)type <= FLAC__MAX_METADATA_TYPE_CODE);
785
  /* double protection */
786
0
  if((uint32_t)type > FLAC__MAX_METADATA_TYPE_CODE)
787
0
    return false;
788
0
  decoder->private_->metadata_filter[type] = false;
789
0
  if(type == FLAC__METADATA_TYPE_APPLICATION)
790
0
    decoder->private_->metadata_filter_ids_count = 0;
791
0
  return true;
792
0
}
793
794
FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
795
0
{
796
0
  FLAC__ASSERT(0 != decoder);
797
0
  FLAC__ASSERT(0 != decoder->private_);
798
0
  FLAC__ASSERT(0 != decoder->protected_);
799
0
  FLAC__ASSERT(0 != id);
800
801
0
  if(!decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION])
802
0
    return true;
803
804
0
  FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);
805
806
0
  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
0
  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
0
  decoder->private_->metadata_filter_ids_count++;
816
817
0
  return true;
818
0
}
819
820
FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_all(FLAC__StreamDecoder *decoder)
821
0
{
822
0
  FLAC__ASSERT(0 != decoder);
823
0
  FLAC__ASSERT(0 != decoder->private_);
824
0
  FLAC__ASSERT(0 != decoder->protected_);
825
0
  memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter));
826
0
  decoder->private_->metadata_filter_ids_count = 0;
827
0
  return true;
828
0
}
829
830
FLAC_API FLAC__StreamDecoderState FLAC__stream_decoder_get_state(const FLAC__StreamDecoder *decoder)
831
217k
{
832
217k
  FLAC__ASSERT(0 != decoder);
833
217k
  FLAC__ASSERT(0 != decoder->protected_);
834
217k
  return decoder->protected_->state;
835
217k
}
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
0
{
844
0
  FLAC__ASSERT(0 != decoder);
845
0
  FLAC__ASSERT(0 != decoder->protected_);
846
0
#if FLAC__HAS_OGG
847
0
  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
0
}
853
854
FLAC_API FLAC__bool FLAC__stream_decoder_get_md5_checking(const FLAC__StreamDecoder *decoder)
855
0
{
856
0
  FLAC__ASSERT(0 != decoder);
857
0
  FLAC__ASSERT(0 != decoder->protected_);
858
0
  return decoder->protected_->md5_checking;
859
0
}
860
861
FLAC_API FLAC__uint64 FLAC__stream_decoder_get_total_samples(const FLAC__StreamDecoder *decoder)
862
0
{
863
0
  FLAC__ASSERT(0 != decoder);
864
0
  FLAC__ASSERT(0 != decoder->protected_);
865
0
  return decoder->private_->has_stream_info? decoder->private_->stream_info.data.stream_info.total_samples : 0;
866
0
}
867
868
FLAC_API uint32_t FLAC__stream_decoder_get_channels(const FLAC__StreamDecoder *decoder)
869
0
{
870
0
  FLAC__ASSERT(0 != decoder);
871
0
  FLAC__ASSERT(0 != decoder->protected_);
872
0
  return decoder->protected_->channels;
873
0
}
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
0
{
884
0
  FLAC__ASSERT(0 != decoder);
885
0
  FLAC__ASSERT(0 != decoder->protected_);
886
0
  return decoder->protected_->bits_per_sample;
887
0
}
888
889
FLAC_API uint32_t FLAC__stream_decoder_get_sample_rate(const FLAC__StreamDecoder *decoder)
890
0
{
891
0
  FLAC__ASSERT(0 != decoder);
892
0
  FLAC__ASSERT(0 != decoder->protected_);
893
0
  return decoder->protected_->sample_rate;
894
0
}
895
896
FLAC_API uint32_t FLAC__stream_decoder_get_blocksize(const FLAC__StreamDecoder *decoder)
897
0
{
898
0
  FLAC__ASSERT(0 != decoder);
899
0
  FLAC__ASSERT(0 != decoder->protected_);
900
0
  return decoder->protected_->blocksize;
901
0
}
902
903
FLAC_API FLAC__bool FLAC__stream_decoder_get_decode_position(const FLAC__StreamDecoder *decoder, FLAC__uint64 *position)
904
217k
{
905
217k
  FLAC__ASSERT(0 != decoder);
906
217k
  FLAC__ASSERT(0 != decoder->private_);
907
217k
  FLAC__ASSERT(0 != position);
908
909
217k
  if(FLAC__HAS_OGG && decoder->private_->is_ogg)
910
0
    return false;
911
912
217k
  if(0 == decoder->private_->tell_callback)
913
217k
    return false;
914
0
  if(decoder->private_->tell_callback(decoder, position, decoder->private_->client_data) != FLAC__STREAM_DECODER_TELL_STATUS_OK)
915
0
    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
8.66k
{
931
8.66k
  FLAC__ASSERT(0 != decoder);
932
8.66k
  FLAC__ASSERT(0 != decoder->private_);
933
8.66k
  FLAC__ASSERT(0 != decoder->protected_);
934
935
8.66k
  if(!decoder->private_->internal_reset_hack && decoder->protected_->state == FLAC__STREAM_DECODER_UNINITIALIZED)
936
0
    return false;
937
8.66k
  if(decoder->protected_->state == FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR)
938
0
    return false;
939
940
8.66k
  decoder->private_->samples_decoded = 0;
941
8.66k
  decoder->private_->do_md5_checking = false;
942
8.66k
  decoder->private_->last_seen_framesync = 0;
943
8.66k
  decoder->private_->last_frame_is_set = false;
944
945
8.66k
#if FLAC__HAS_OGG
946
8.66k
  if(decoder->private_->is_ogg)
947
0
    FLAC__ogg_decoder_aspect_flush(&decoder->protected_->ogg_decoder_aspect);
948
8.66k
#endif
949
950
8.66k
  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
8.66k
  decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
955
956
8.66k
  return true;
957
8.66k
}
958
959
8.66k
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
8.66k
  decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
964
965
8.66k
  decoder->private_->has_stream_info = false;
966
967
8.66k
  free(decoder->private_->seek_table.data.seek_table.points);
968
8.66k
  decoder->private_->seek_table.data.seek_table.points = 0;
969
8.66k
  decoder->private_->has_seek_table = false;
970
971
8.66k
  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
8.66k
  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
8.66k
  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
0
    FLAC__MD5Final(decoder->private_->computed_md5sum, &decoder->private_->md5context);
988
0
  }
989
8.66k
  else
990
8.66k
    decoder->private_->internal_reset_hack = false;
991
8.66k
  FLAC__MD5Init(&decoder->private_->md5context);
992
993
8.66k
  decoder->private_->first_frame_offset = 0;
994
8.66k
  decoder->private_->unparseable_frame_count = 0;
995
8.66k
  decoder->private_->last_seen_framesync = 0;
996
8.66k
  decoder->private_->last_frame_is_set = false;
997
8.66k
  decoder->private_->error_has_been_sent = false;
998
8.66k
}
999
1000
FLAC_API FLAC__bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder)
1001
8.66k
{
1002
8.66k
  FLAC__ASSERT(0 != decoder);
1003
8.66k
  FLAC__ASSERT(0 != decoder->private_);
1004
8.66k
  FLAC__ASSERT(0 != decoder->protected_);
1005
1006
8.66k
  if(!FLAC__stream_decoder_flush(decoder)) {
1007
    /* above call sets the state for us */
1008
0
    return false;
1009
0
  }
1010
1011
8.66k
#if FLAC__HAS_OGG
1012
  /*@@@ could go in !internal_reset_hack block below */
1013
8.66k
  if(decoder->private_->is_ogg)
1014
0
    FLAC__ogg_decoder_aspect_reset(&decoder->protected_->ogg_decoder_aspect);
1015
8.66k
#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
8.66k
  if(!decoder->private_->internal_reset_hack) {
1023
0
    if(decoder->private_->file == stdin)
1024
0
      return false; /* can't rewind stdin, reset fails */
1025
0
    if(decoder->private_->seek_callback && decoder->private_->seek_callback(decoder, 0, decoder->private_->client_data) == FLAC__STREAM_DECODER_SEEK_STATUS_ERROR)
1026
0
      return false; /* seekable and seek fails, reset fails */
1027
0
  }
1028
1029
8.66k
  reset_decoder_internal_(decoder);
1030
1031
8.66k
  return true;
1032
8.66k
}
1033
1034
FLAC_API FLAC__bool FLAC__stream_decoder_process_single(FLAC__StreamDecoder *decoder)
1035
225k
{
1036
225k
  FLAC__bool got_a_frame;
1037
225k
  FLAC__ASSERT(0 != decoder);
1038
225k
  FLAC__ASSERT(0 != decoder->protected_);
1039
1040
443k
  while(1) {
1041
443k
    switch(decoder->protected_->state) {
1042
8.66k
      case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
1043
8.66k
        if(!find_metadata_(decoder))
1044
0
          return false; /* above function sets the status for us */
1045
8.66k
        break;
1046
17.3k
      case FLAC__STREAM_DECODER_READ_METADATA:
1047
17.3k
        if(!read_metadata_(decoder))
1048
0
          return false; /* above function sets the status for us */
1049
17.3k
        else
1050
17.3k
          return true;
1051
208k
      case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
1052
208k
        if(!frame_sync_(decoder)) {
1053
0
          return true; /* above function sets the status for us */
1054
0
        }
1055
208k
        break;
1056
208k
      case FLAC__STREAM_DECODER_READ_FRAME:
1057
208k
        if(!read_frame_(decoder, &got_a_frame, /*do_full_decode=*/true))
1058
0
          return false; /* above function sets the status for us */
1059
208k
        if(got_a_frame)
1060
208k
          return true; /* above function sets the status for us */
1061
0
        break;
1062
0
      case FLAC__STREAM_DECODER_END_OF_STREAM:
1063
0
      case FLAC__STREAM_DECODER_END_OF_LINK:
1064
0
      case FLAC__STREAM_DECODER_ABORTED:
1065
0
        return true;
1066
0
      default:
1067
0
        return false;
1068
443k
    }
1069
443k
  }
1070
225k
}
1071
1072
FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_metadata(FLAC__StreamDecoder *decoder)
1073
0
{
1074
0
  FLAC__ASSERT(0 != decoder);
1075
0
  FLAC__ASSERT(0 != decoder->protected_);
1076
1077
0
  while(1) {
1078
0
    switch(decoder->protected_->state) {
1079
0
      case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
1080
0
        if(!find_metadata_(decoder))
1081
0
          return false; /* above function sets the status for us */
1082
0
        break;
1083
0
      case FLAC__STREAM_DECODER_READ_METADATA:
1084
0
        if(!read_metadata_(decoder))
1085
0
          return false; /* above function sets the status for us */
1086
0
        break;
1087
0
      case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
1088
0
      case FLAC__STREAM_DECODER_READ_FRAME:
1089
0
      case FLAC__STREAM_DECODER_END_OF_STREAM:
1090
0
      case FLAC__STREAM_DECODER_END_OF_LINK:
1091
0
      case FLAC__STREAM_DECODER_ABORTED:
1092
0
        return true;
1093
0
      default:
1094
0
        return false;
1095
0
    }
1096
0
  }
1097
0
}
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
0
{
1136
0
  FLAC__bool md5_failed = false;
1137
1138
0
  FLAC__ASSERT(0 != decoder);
1139
0
  FLAC__ASSERT(0 != decoder->private_);
1140
0
  FLAC__ASSERT(0 != decoder->protected_);
1141
1142
0
  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
0
  FLAC__MD5Final(decoder->private_->computed_md5sum, &decoder->private_->md5context);
1148
1149
0
  if(decoder->private_->do_md5_checking) {
1150
0
    if(memcmp(decoder->private_->stream_info.data.stream_info.md5sum, decoder->private_->computed_md5sum, 16))
1151
0
      md5_failed = true;
1152
0
  }
1153
1154
0
  reset_decoder_internal_(decoder);
1155
1156
0
#if FLAC__HAS_OGG
1157
0
  if(decoder->private_->is_ogg)
1158
0
    FLAC__ogg_decoder_aspect_next_link(&decoder->protected_->ogg_decoder_aspect);
1159
0
#endif
1160
1161
0
  return !md5_failed;
1162
0
}
1163
1164
FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_stream(FLAC__StreamDecoder *decoder)
1165
0
{
1166
0
  FLAC__bool dummy;
1167
0
  FLAC__ASSERT(0 != decoder);
1168
0
  FLAC__ASSERT(0 != decoder->protected_);
1169
1170
0
  while(1) {
1171
0
    switch(decoder->protected_->state) {
1172
0
      case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
1173
0
        if(!find_metadata_(decoder))
1174
0
          return false; /* above function sets the status for us */
1175
0
        break;
1176
0
      case FLAC__STREAM_DECODER_READ_METADATA:
1177
0
        if(!read_metadata_(decoder))
1178
0
          return false; /* above function sets the status for us */
1179
0
        break;
1180
0
      case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
1181
0
        if(!frame_sync_(decoder) && decoder->protected_->state != FLAC__STREAM_DECODER_END_OF_LINK && decoder->protected_->state != FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR) {
1182
0
          return true; /* above function sets the status for us */
1183
0
        }
1184
0
        break;
1185
0
      case FLAC__STREAM_DECODER_READ_FRAME:
1186
0
        if(!read_frame_(decoder, &dummy, /*do_full_decode=*/true))
1187
0
          return false; /* above function sets the status for us */
1188
0
        break;
1189
0
      case FLAC__STREAM_DECODER_END_OF_LINK:
1190
0
        FLAC__stream_decoder_finish_link(decoder);
1191
0
        break;
1192
0
      case FLAC__STREAM_DECODER_END_OF_STREAM:
1193
0
      case FLAC__STREAM_DECODER_ABORTED:
1194
0
        return true;
1195
0
      default:
1196
0
        return false;
1197
0
    }
1198
0
  }
1199
0
}
1200
1201
FLAC_API FLAC__bool FLAC__stream_decoder_skip_single_frame(FLAC__StreamDecoder *decoder)
1202
0
{
1203
0
  FLAC__bool got_a_frame;
1204
0
  FLAC__ASSERT(0 != decoder);
1205
0
  FLAC__ASSERT(0 != decoder->protected_);
1206
1207
0
  while(1) {
1208
0
    switch(decoder->protected_->state) {
1209
0
      case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
1210
0
      case FLAC__STREAM_DECODER_READ_METADATA:
1211
0
        return false;
1212
0
      case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
1213
0
        if(!frame_sync_(decoder)) {
1214
0
          return true; /* above function sets the status for us */
1215
0
        }
1216
0
        break;
1217
0
      case FLAC__STREAM_DECODER_READ_FRAME:
1218
0
        if(!read_frame_(decoder, &got_a_frame, /*do_full_decode=*/false))
1219
0
          return false; /* above function sets the status for us */
1220
0
        if(got_a_frame)
1221
0
          return true; /* above function sets the status for us */
1222
0
        break;
1223
0
      case FLAC__STREAM_DECODER_END_OF_STREAM:
1224
0
      case FLAC__STREAM_DECODER_END_OF_LINK:
1225
0
      case FLAC__STREAM_DECODER_ABORTED:
1226
0
        return true;
1227
0
      default:
1228
0
        return false;
1229
0
    }
1230
0
  }
1231
0
}
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
0
{
1284
0
  FLAC__uint64 length;
1285
1286
0
  FLAC__ASSERT(0 != decoder);
1287
1288
0
  if(
1289
0
    decoder->protected_->state != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA &&
1290
0
    decoder->protected_->state != FLAC__STREAM_DECODER_READ_METADATA &&
1291
0
    decoder->protected_->state != FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC &&
1292
0
    decoder->protected_->state != FLAC__STREAM_DECODER_READ_FRAME &&
1293
0
    decoder->protected_->state != FLAC__STREAM_DECODER_END_OF_STREAM
1294
0
  )
1295
0
    return false;
1296
1297
0
  if(0 == decoder->private_->seek_callback)
1298
0
    return false;
1299
1300
0
  FLAC__ASSERT(decoder->private_->seek_callback);
1301
0
  FLAC__ASSERT(decoder->private_->tell_callback);
1302
0
  FLAC__ASSERT(decoder->private_->length_callback);
1303
0
  FLAC__ASSERT(decoder->private_->eof_callback);
1304
1305
0
  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
0
  decoder->private_->is_seeking = true;
1309
1310
  /* turn off md5 checking if a seek is attempted */
1311
0
  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
0
  if(decoder->private_->length_callback(decoder, &length, decoder->private_->client_data) != FLAC__STREAM_DECODER_LENGTH_STATUS_OK) {
1315
0
    decoder->private_->is_seeking = false;
1316
0
    return false;
1317
0
  }
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.3k
{
1533
17.3k
  decoder->private_->is_ogg = false;
1534
17.3k
  decoder->private_->read_callback = 0;
1535
17.3k
  decoder->private_->seek_callback = 0;
1536
17.3k
  decoder->private_->tell_callback = 0;
1537
17.3k
  decoder->private_->length_callback = 0;
1538
17.3k
  decoder->private_->eof_callback = 0;
1539
17.3k
  decoder->private_->write_callback = 0;
1540
17.3k
  decoder->private_->metadata_callback = 0;
1541
17.3k
  decoder->private_->error_callback = 0;
1542
17.3k
  decoder->private_->client_data = 0;
1543
1544
17.3k
  memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter));
1545
17.3k
  decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO] = true;
1546
17.3k
  decoder->private_->metadata_filter_ids_count = 0;
1547
1548
17.3k
  decoder->protected_->md5_checking = false;
1549
1550
17.3k
#if FLAC__HAS_OGG
1551
17.3k
  FLAC__ogg_decoder_aspect_set_defaults(&decoder->protected_->ogg_decoder_aspect);
1552
17.3k
#endif
1553
17.3k
}
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
208k
{
1575
208k
  uint32_t i;
1576
208k
  FLAC__int32 *tmp;
1577
1578
208k
  if(size <= decoder->private_->output_capacity && channels <= decoder->private_->output_channels &&
1579
200k
     (bps < 32 || decoder->private_->side_subframe != 0))
1580
200k
    return true;
1581
1582
  /* simply using realloc() is not practical because the number of channels may change mid-stream */
1583
1584
73.4k
  for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
1585
65.2k
    if(0 != decoder->private_->output[i]) {
1586
0
      free(decoder->private_->output[i]-4);
1587
0
      decoder->private_->output[i] = 0;
1588
0
    }
1589
65.2k
    if(0 != decoder->private_->residual_unaligned[i]) {
1590
0
      free(decoder->private_->residual_unaligned[i]);
1591
0
      decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0;
1592
0
    }
1593
65.2k
  }
1594
1595
8.16k
  if(0 != decoder->private_->side_subframe) {
1596
0
    free(decoder->private_->side_subframe);
1597
0
    decoder->private_->side_subframe = 0;
1598
0
  }
1599
1600
  /* Only grow per-channel buffers, even if number of channels increases */
1601
8.16k
  if(decoder->private_->output_capacity > size) {
1602
0
    size = decoder->private_->output_capacity;
1603
0
  }
1604
1605
22.1k
  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
14.0k
    tmp = safe_malloc_muladd2_(sizeof(FLAC__int32), /*times (*/size, /*+*/4/*)*/);
1613
14.0k
    if(tmp == 0) {
1614
0
      decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1615
0
      return false;
1616
0
    }
1617
14.0k
    memset(tmp, 0, sizeof(FLAC__int32)*4);
1618
14.0k
    decoder->private_->output[i] = tmp + 4;
1619
1620
14.0k
    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
14.0k
  }
1625
1626
8.16k
  if(bps == 32) {
1627
5.38k
    decoder->private_->side_subframe = safe_malloc_mul_2op_p(sizeof(FLAC__int64), /*times (*/size);
1628
5.38k
    if(decoder->private_->side_subframe == NULL) {
1629
0
      decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1630
0
      return false;
1631
0
    }
1632
5.38k
  }
1633
1634
8.16k
  decoder->private_->output_capacity = size;
1635
8.16k
  decoder->private_->output_channels = channels;
1636
1637
8.16k
  return true;
1638
8.16k
}
1639
1640
FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id)
1641
0
{
1642
0
  size_t i;
1643
1644
0
  FLAC__ASSERT(0 != decoder);
1645
0
  FLAC__ASSERT(0 != decoder->private_);
1646
1647
0
  for(i = 0; i < decoder->private_->metadata_filter_ids_count; i++)
1648
0
    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
0
      return true;
1650
1651
0
  return false;
1652
0
}
1653
1654
FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder)
1655
8.66k
{
1656
8.66k
  FLAC__uint32 x;
1657
8.66k
  uint32_t i, id;
1658
8.66k
  FLAC__bool first = true;
1659
1660
8.66k
  FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
1661
1662
43.3k
  for(i = id = 0; i < 4; ) {
1663
34.6k
    if(decoder->private_->cached) {
1664
0
      x = (FLAC__uint32)decoder->private_->lookahead;
1665
0
      decoder->private_->cached = false;
1666
0
    }
1667
34.6k
    else {
1668
34.6k
      if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
1669
0
        return false; /* read_callback_ sets the state for us */
1670
34.6k
    }
1671
34.6k
    if(x == FLAC__STREAM_SYNC_STRING[i]) {
1672
34.6k
      first = true;
1673
34.6k
      i++;
1674
34.6k
      id = 0;
1675
34.6k
      continue;
1676
34.6k
    }
1677
1678
0
    if(id >= 3)
1679
0
      return false;
1680
1681
0
    if(x == ID3V2_TAG_[id]) {
1682
0
      id++;
1683
0
      i = 0;
1684
0
      if(id == 3) {
1685
0
        if(!skip_id3v2_tag_(decoder))
1686
0
          return false; /* skip_id3v2_tag_ sets the state for us */
1687
0
      }
1688
0
      continue;
1689
0
    }
1690
0
    id = 0;
1691
0
    if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1692
0
      decoder->private_->header_warmup[0] = (FLAC__byte)x;
1693
0
      if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
1694
0
        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
0
      if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
1699
0
        decoder->private_->lookahead = (FLAC__byte)x;
1700
0
        decoder->private_->cached = true;
1701
0
      }
1702
0
      else if(x >> 1 == 0x7c) { /* MAGIC NUMBER for the last 6 sync bits and reserved 7th bit */
1703
0
        decoder->private_->header_warmup[1] = (FLAC__byte)x;
1704
0
        decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
1705
0
        return true;
1706
0
      }
1707
0
    }
1708
0
    i = 0;
1709
0
    if(first) {
1710
0
      send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
1711
0
      first = false;
1712
0
    }
1713
0
  }
1714
1715
8.66k
  decoder->protected_->state = FLAC__STREAM_DECODER_READ_METADATA;
1716
8.66k
  return true;
1717
8.66k
}
1718
1719
FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder)
1720
17.3k
{
1721
17.3k
  FLAC__bool is_last;
1722
17.3k
  FLAC__uint32 i, x, type, length;
1723
1724
17.3k
  FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
1725
1726
17.3k
  if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_IS_LAST_LEN))
1727
0
    return false; /* read_callback_ sets the state for us */
1728
17.3k
  is_last = x? true : false;
1729
1730
17.3k
  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
17.3k
  if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &length, FLAC__STREAM_METADATA_LENGTH_LEN))
1734
0
    return false; /* read_callback_ sets the state for us */
1735
1736
17.3k
  if(type == FLAC__METADATA_TYPE_STREAMINFO) {
1737
8.66k
    if(!read_metadata_streaminfo_(decoder, is_last, length))
1738
0
      return false;
1739
1740
8.66k
    decoder->private_->has_stream_info = true;
1741
8.66k
    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
8.66k
      decoder->private_->do_md5_checking = false;
1743
8.66k
    if(!decoder->private_->is_seeking && decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO] && decoder->private_->metadata_callback)
1744
8.66k
      decoder->private_->metadata_callback(decoder, &decoder->private_->stream_info, decoder->private_->client_data);
1745
8.66k
  }
1746
8.66k
  else if(type == FLAC__METADATA_TYPE_SEEKTABLE) {
1747
    /* just in case we already have a seek table, and reading the next one fails: */
1748
0
    decoder->private_->has_seek_table = false;
1749
1750
0
    if(length > 0) {
1751
0
      if(!read_metadata_seektable_(decoder, is_last, length))
1752
0
        return false;
1753
1754
0
      decoder->private_->has_seek_table = true;
1755
0
      if(!decoder->private_->is_seeking && decoder->private_->metadata_filter[FLAC__METADATA_TYPE_SEEKTABLE] && decoder->private_->metadata_callback)
1756
0
        decoder->private_->metadata_callback(decoder, &decoder->private_->seek_table, decoder->private_->client_data);
1757
0
    }
1758
0
  }
1759
8.66k
  else {
1760
8.66k
    FLAC__bool skip_it = !decoder->private_->metadata_filter[type];
1761
8.66k
    uint32_t real_length = length;
1762
8.66k
    FLAC__StreamMetadata block;
1763
1764
8.66k
    memset(&block, 0, sizeof(block));
1765
8.66k
    block.is_last = is_last;
1766
8.66k
    block.type = (FLAC__MetadataType)type;
1767
8.66k
    block.length = length;
1768
1769
8.66k
    if(type == FLAC__METADATA_TYPE_APPLICATION) {
1770
0
      if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8))
1771
0
        return false; /* read_callback_ sets the state for us */
1772
1773
0
      if(real_length < FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8) { /* underflow check */
1774
0
        decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;/*@@@@@@ maybe wrong error? need to resync?*/
1775
0
        return false;
1776
0
      }
1777
1778
0
      real_length -= FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8;
1779
1780
0
      if(decoder->private_->metadata_filter_ids_count > 0 && has_id_filtered_(decoder, block.data.application.id))
1781
0
        skip_it = !skip_it;
1782
0
    }
1783
1784
8.66k
    if(skip_it) {
1785
8.66k
      if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, real_length))
1786
0
        return false; /* read_callback_ sets the state for us */
1787
8.66k
    }
1788
0
    else {
1789
0
      FLAC__bool ok = true;
1790
0
      FLAC__bitreader_set_limit(decoder->private_->input, real_length*8);
1791
0
      switch(type) {
1792
0
        case FLAC__METADATA_TYPE_PADDING:
1793
          /* skip the padding bytes */
1794
0
          if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, real_length))
1795
0
            ok = false; /* read_callback_ sets the state for us */
1796
0
          break;
1797
0
        case FLAC__METADATA_TYPE_APPLICATION:
1798
          /* remember, we read the ID already */
1799
0
          if(real_length > 0) {
1800
0
            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
0
            else if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.application.data, real_length))
1805
0
              ok = false; /* read_callback_ sets the state for us */
1806
0
          }
1807
0
          else
1808
0
            block.data.application.data = 0;
1809
0
          break;
1810
0
        case FLAC__METADATA_TYPE_VORBIS_COMMENT:
1811
0
          if(!read_metadata_vorbiscomment_(decoder, &block.data.vorbis_comment, real_length))
1812
0
            ok = false;
1813
0
          break;
1814
0
        case FLAC__METADATA_TYPE_CUESHEET:
1815
0
          if(!read_metadata_cuesheet_(decoder, &block.data.cue_sheet))
1816
0
            ok = false;
1817
0
          break;
1818
0
        case FLAC__METADATA_TYPE_PICTURE:
1819
0
          if(!read_metadata_picture_(decoder, &block.data.picture))
1820
0
            ok = false;
1821
0
          break;
1822
0
        case FLAC__METADATA_TYPE_STREAMINFO:
1823
0
        case FLAC__METADATA_TYPE_SEEKTABLE:
1824
0
          FLAC__ASSERT(0);
1825
0
          break;
1826
0
        default:
1827
0
          if(real_length > 0) {
1828
0
            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
0
            else if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.unknown.data, real_length))
1833
0
              ok = false; /* read_callback_ sets the state for us */
1834
0
          }
1835
0
          else
1836
0
            block.data.unknown.data = 0;
1837
0
          break;
1838
0
      }
1839
0
      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
0
        send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_METADATA);
1844
0
        if(decoder->protected_->state == FLAC__STREAM_DECODER_READ_METADATA)
1845
0
          decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1846
0
        ok = false;
1847
0
      }
1848
0
      FLAC__bitreader_remove_limit(decoder->private_->input);
1849
0
      if(ok && !decoder->private_->is_seeking && decoder->private_->metadata_callback)
1850
0
        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
0
      switch(type) {
1854
0
        case FLAC__METADATA_TYPE_PADDING:
1855
0
          break;
1856
0
        case FLAC__METADATA_TYPE_APPLICATION:
1857
0
          if(0 != block.data.application.data)
1858
0
            free(block.data.application.data);
1859
0
          break;
1860
0
        case FLAC__METADATA_TYPE_VORBIS_COMMENT:
1861
0
          if(0 != block.data.vorbis_comment.vendor_string.entry)
1862
0
            free(block.data.vorbis_comment.vendor_string.entry);
1863
0
          if(block.data.vorbis_comment.num_comments > 0)
1864
0
            for(i = 0; i < block.data.vorbis_comment.num_comments; i++)
1865
0
              if(0 != block.data.vorbis_comment.comments[i].entry)
1866
0
                free(block.data.vorbis_comment.comments[i].entry);
1867
0
          if(0 != block.data.vorbis_comment.comments)
1868
0
            free(block.data.vorbis_comment.comments);
1869
0
          break;
1870
0
        case FLAC__METADATA_TYPE_CUESHEET:
1871
0
          if(block.data.cue_sheet.num_tracks > 0 && 0 != block.data.cue_sheet.tracks)
1872
0
            for(i = 0; i < block.data.cue_sheet.num_tracks; i++)
1873
0
              if(0 != block.data.cue_sheet.tracks[i].indices)
1874
0
                free(block.data.cue_sheet.tracks[i].indices);
1875
0
          if(0 != block.data.cue_sheet.tracks)
1876
0
            free(block.data.cue_sheet.tracks);
1877
0
          break;
1878
0
        case FLAC__METADATA_TYPE_PICTURE:
1879
0
          if(0 != block.data.picture.mime_type)
1880
0
            free(block.data.picture.mime_type);
1881
0
          if(0 != block.data.picture.description)
1882
0
            free(block.data.picture.description);
1883
0
          if(0 != block.data.picture.data)
1884
0
            free(block.data.picture.data);
1885
0
          break;
1886
0
        case FLAC__METADATA_TYPE_STREAMINFO:
1887
0
        case FLAC__METADATA_TYPE_SEEKTABLE:
1888
0
          FLAC__ASSERT(0);
1889
0
        default:
1890
0
          if(0 != block.data.unknown.data)
1891
0
            free(block.data.unknown.data);
1892
0
          break;
1893
0
      }
1894
1895
0
      if(!ok) /* anything that unsets "ok" should also make sure decoder->protected_->state is updated */
1896
0
        return false;
1897
0
    }
1898
8.66k
  }
1899
1900
17.3k
  if(is_last) {
1901
    /* if this fails, it's OK, it's just a hint for the seek routine */
1902
8.66k
    if(!FLAC__stream_decoder_get_decode_position(decoder, &decoder->private_->first_frame_offset))
1903
8.66k
      decoder->private_->first_frame_offset = 0;
1904
8.66k
    decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1905
8.66k
  }
1906
1907
17.3k
  return true;
1908
17.3k
}
1909
1910
FLAC__bool read_metadata_streaminfo_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, uint32_t length)
1911
8.66k
{
1912
8.66k
  FLAC__uint32 x;
1913
8.66k
  uint32_t bits, used_bits = 0;
1914
1915
8.66k
  FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
1916
1917
8.66k
  decoder->private_->stream_info.type = FLAC__METADATA_TYPE_STREAMINFO;
1918
8.66k
  decoder->private_->stream_info.is_last = is_last;
1919
8.66k
  decoder->private_->stream_info.length = length;
1920
1921
8.66k
  bits = FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN;
1922
8.66k
  if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, bits))
1923
0
    return false; /* read_callback_ sets the state for us */
1924
8.66k
  decoder->private_->stream_info.data.stream_info.min_blocksize = x;
1925
8.66k
  used_bits += bits;
1926
1927
8.66k
  bits = FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN;
1928
8.66k
  if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN))
1929
0
    return false; /* read_callback_ sets the state for us */
1930
8.66k
  decoder->private_->stream_info.data.stream_info.max_blocksize = x;
1931
8.66k
  used_bits += bits;
1932
1933
8.66k
  bits = FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN;
1934
8.66k
  if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN))
1935
0
    return false; /* read_callback_ sets the state for us */
1936
8.66k
  decoder->private_->stream_info.data.stream_info.min_framesize = x;
1937
8.66k
  used_bits += bits;
1938
1939
8.66k
  bits = FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN;
1940
8.66k
  if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN))
1941
0
    return false; /* read_callback_ sets the state for us */
1942
8.66k
  decoder->private_->stream_info.data.stream_info.max_framesize = x;
1943
8.66k
  used_bits += bits;
1944
1945
8.66k
  bits = FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN;
1946
8.66k
  if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN))
1947
0
    return false; /* read_callback_ sets the state for us */
1948
8.66k
  decoder->private_->stream_info.data.stream_info.sample_rate = x;
1949
8.66k
  used_bits += bits;
1950
1951
8.66k
  bits = FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN;
1952
8.66k
  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
8.66k
  decoder->private_->stream_info.data.stream_info.channels = x+1;
1955
8.66k
  used_bits += bits;
1956
1957
8.66k
  bits = FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN;
1958
8.66k
  if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN))
1959
0
    return false; /* read_callback_ sets the state for us */
1960
8.66k
  decoder->private_->stream_info.data.stream_info.bits_per_sample = x+1;
1961
8.66k
  used_bits += bits;
1962
1963
8.66k
  bits = FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN;
1964
8.66k
  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
0
    return false; /* read_callback_ sets the state for us */
1966
8.66k
  used_bits += bits;
1967
1968
8.66k
  if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, decoder->private_->stream_info.data.stream_info.md5sum, 16))
1969
0
    return false; /* read_callback_ sets the state for us */
1970
8.66k
  used_bits += 16*8;
1971
1972
  /* skip the rest of the block */
1973
8.66k
  FLAC__ASSERT(used_bits % 8 == 0);
1974
8.66k
  if (length < (used_bits / 8))
1975
0
    return false; /* read_callback_ sets the state for us */
1976
8.66k
  length -= (used_bits / 8);
1977
8.66k
  if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, length))
1978
0
    return false; /* read_callback_ sets the state for us */
1979
1980
8.66k
  return true;
1981
8.66k
}
1982
1983
FLAC__bool read_metadata_seektable_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, uint32_t length)
1984
0
{
1985
0
  FLAC__uint32 i, x;
1986
0
  FLAC__uint64 xx;
1987
1988
0
  FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
1989
1990
0
  decoder->private_->seek_table.type = FLAC__METADATA_TYPE_SEEKTABLE;
1991
0
  decoder->private_->seek_table.is_last = is_last;
1992
0
  decoder->private_->seek_table.length = length;
1993
1994
0
  if(length % FLAC__STREAM_METADATA_SEEKPOINT_LENGTH) {
1995
0
    FLAC__bitreader_limit_invalidate(decoder->private_->input);
1996
0
    return false;
1997
0
  }
1998
1999
0
  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
0
  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
0
  for(i = 0; i < decoder->private_->seek_table.data.seek_table.num_points; i++) {
2007
0
    if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN))
2008
0
      return false; /* read_callback_ sets the state for us */
2009
0
    decoder->private_->seek_table.data.seek_table.points[i].sample_number = xx;
2010
2011
0
    if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN))
2012
0
      return false; /* read_callback_ sets the state for us */
2013
0
    decoder->private_->seek_table.data.seek_table.points[i].stream_offset = xx;
2014
2015
0
    if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN))
2016
0
      return false; /* read_callback_ sets the state for us */
2017
0
    decoder->private_->seek_table.data.seek_table.points[i].frame_samples = x;
2018
0
  }
2019
0
  length -= (decoder->private_->seek_table.data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH);
2020
2021
0
  FLAC__ASSERT(length == 0);
2022
2023
0
  return true;
2024
0
}
2025
2026
FLAC__bool read_metadata_vorbiscomment_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_VorbisComment *obj, uint32_t length)
2027
0
{
2028
0
  FLAC__uint32 i;
2029
2030
0
  FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
2031
2032
  /* read vendor string */
2033
0
  if (length >= 8) {
2034
0
    length -= 8; /* vendor string length + num comments entries alone take 8 bytes */
2035
0
    FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32);
2036
0
    if (!FLAC__bitreader_read_uint32_little_endian(decoder->private_->input, &obj->vendor_string.length))
2037
0
      return false; /* read_callback_ sets the state for us */
2038
0
    if (length < obj->vendor_string.length) {
2039
0
      obj->vendor_string.length = 0;
2040
0
      obj->vendor_string.entry = 0;
2041
0
      goto skip;
2042
0
    }
2043
0
    else
2044
0
      length -= obj->vendor_string.length;
2045
0
    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
0
    if (!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->vendor_string.entry, obj->vendor_string.length))
2050
0
      return false; /* read_callback_ sets the state for us */
2051
0
    obj->vendor_string.entry[obj->vendor_string.length] = '\0';
2052
2053
    /* read num comments */
2054
0
    FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN == 32);
2055
0
    if (!FLAC__bitreader_read_uint32_little_endian(decoder->private_->input, &obj->num_comments))
2056
0
      return false; /* read_callback_ sets the state for us */
2057
2058
    /* read comments */
2059
0
    if (obj->num_comments > 100000) {
2060
      /* Possibly malicious file. */
2061
0
      obj->num_comments = 0;
2062
0
      return false;
2063
0
    }
2064
0
    if (obj->num_comments > 0) {
2065
0
      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
0
      for (i = 0; i < obj->num_comments; i++) {
2071
        /* Initialize here just to make sure. */
2072
0
        obj->comments[i].length = 0;
2073
0
        obj->comments[i].entry = 0;
2074
2075
0
        FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32);
2076
0
        if (length < 4) {
2077
0
          obj->num_comments = i;
2078
0
          goto skip;
2079
0
        }
2080
0
        else
2081
0
          length -= 4;
2082
0
        if (!FLAC__bitreader_read_uint32_little_endian(decoder->private_->input, &obj->comments[i].length)) {
2083
0
          obj->num_comments = i;
2084
0
          return false; /* read_callback_ sets the state for us */
2085
0
        }
2086
0
        if (length < obj->comments[i].length) {
2087
0
          obj->num_comments = i;
2088
0
          FLAC__bitreader_limit_invalidate(decoder->private_->input);
2089
0
          return false;
2090
0
        }
2091
0
        else
2092
0
          length -= obj->comments[i].length;
2093
0
        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
0
        memset (obj->comments[i].entry, 0, obj->comments[i].length) ;
2099
0
        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
0
          free (obj->comments[i].entry) ;
2102
0
          obj->comments[i].entry = NULL ;
2103
0
          obj->num_comments = i;
2104
0
          goto skip;
2105
0
        }
2106
0
        obj->comments[i].entry[obj->comments[i].length] = '\0';
2107
0
      }
2108
0
    }
2109
0
  }
2110
0
  else {
2111
0
    FLAC__bitreader_limit_invalidate(decoder->private_->input);
2112
0
    return false;
2113
0
  }
2114
2115
0
  skip:
2116
0
  if (length > 0) {
2117
    /* length > 0 can only happen on files with invalid data in comments */
2118
0
    if(obj->num_comments < 1) {
2119
0
      free(obj->comments);
2120
0
      obj->comments = NULL;
2121
0
    }
2122
0
    FLAC__bitreader_limit_invalidate(decoder->private_->input);
2123
0
    return false;
2124
0
  }
2125
2126
0
  return true;
2127
0
}
2128
2129
FLAC__bool read_metadata_cuesheet_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_CueSheet *obj)
2130
0
{
2131
0
  FLAC__uint32 i, j, x;
2132
2133
0
  FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
2134
2135
0
  memset(obj, 0, sizeof(FLAC__StreamMetadata_CueSheet));
2136
2137
0
  FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN % 8 == 0);
2138
0
  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
0
    return false; /* read_callback_ sets the state for us */
2140
2141
0
  if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &obj->lead_in, FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN))
2142
0
    return false; /* read_callback_ sets the state for us */
2143
2144
0
  if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN))
2145
0
    return false; /* read_callback_ sets the state for us */
2146
0
  obj->is_cd = x? true : false;
2147
2148
0
  if(!FLAC__bitreader_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN))
2149
0
    return false; /* read_callback_ sets the state for us */
2150
2151
0
  if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN))
2152
0
    return false; /* read_callback_ sets the state for us */
2153
0
  obj->num_tracks = x;
2154
2155
0
  if(obj->num_tracks > 0) {
2156
0
    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
0
    for(i = 0; i < obj->num_tracks; i++) {
2161
0
      FLAC__StreamMetadata_CueSheet_Track *track = &obj->tracks[i];
2162
0
      if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &track->offset, FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN))
2163
0
        return false; /* read_callback_ sets the state for us */
2164
2165
0
      if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN))
2166
0
        return false; /* read_callback_ sets the state for us */
2167
0
      track->number = (FLAC__byte)x;
2168
2169
0
      FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN % 8 == 0);
2170
0
      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
0
        return false; /* read_callback_ sets the state for us */
2172
2173
0
      if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN))
2174
0
        return false; /* read_callback_ sets the state for us */
2175
0
      track->type = x;
2176
2177
0
      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
0
      track->pre_emphasis = x;
2180
2181
0
      if(!FLAC__bitreader_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN))
2182
0
        return false; /* read_callback_ sets the state for us */
2183
2184
0
      if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN))
2185
0
        return false; /* read_callback_ sets the state for us */
2186
0
      track->num_indices = (FLAC__byte)x;
2187
2188
0
      if(track->num_indices > 0) {
2189
0
        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
0
        for(j = 0; j < track->num_indices; j++) {
2194
0
          FLAC__StreamMetadata_CueSheet_Index *indx = &track->indices[j];
2195
0
          if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &indx->offset, FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN))
2196
0
            return false; /* read_callback_ sets the state for us */
2197
2198
0
          if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN))
2199
0
            return false; /* read_callback_ sets the state for us */
2200
0
          indx->number = (FLAC__byte)x;
2201
2202
0
          if(!FLAC__bitreader_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN))
2203
0
            return false; /* read_callback_ sets the state for us */
2204
0
        }
2205
0
      }
2206
0
    }
2207
0
  }
2208
0
  else { /* obj->num_tracks == 0 */
2209
0
    FLAC__bitreader_limit_invalidate(decoder->private_->input);
2210
0
    return false;
2211
0
  }
2212
2213
0
  return true;
2214
0
}
2215
2216
FLAC__bool read_metadata_picture_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_Picture *obj)
2217
0
{
2218
0
  FLAC__uint32 x;
2219
2220
0
  FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
2221
2222
  /* read type */
2223
0
  if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_PICTURE_TYPE_LEN))
2224
0
    return false; /* read_callback_ sets the state for us */
2225
0
  if(x < FLAC__STREAM_METADATA_PICTURE_TYPE_UNDEFINED)
2226
0
    obj->type = x;
2227
0
  else
2228
0
    obj->type = FLAC__STREAM_METADATA_PICTURE_TYPE_OTHER;
2229
2230
  /* read MIME type */
2231
0
  if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN))
2232
0
    return false; /* read_callback_ sets the state for us */
2233
0
  if(FLAC__bitreader_limit_remaining(decoder->private_->input) < x){
2234
0
    FLAC__bitreader_limit_invalidate(decoder->private_->input);
2235
0
    return false;
2236
0
  }
2237
0
  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
0
  if(x > 0) {
2242
0
    if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, (FLAC__byte*)obj->mime_type, x))
2243
0
      return false; /* read_callback_ sets the state for us */
2244
0
  }
2245
0
  obj->mime_type[x] = '\0';
2246
2247
  /* read description */
2248
0
  if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN))
2249
0
    return false; /* read_callback_ sets the state for us */
2250
0
  if(FLAC__bitreader_limit_remaining(decoder->private_->input) < x){
2251
0
    FLAC__bitreader_limit_invalidate(decoder->private_->input);
2252
0
    return false;
2253
0
  }
2254
0
  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
0
  if(x > 0) {
2259
0
    if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->description, x))
2260
0
      return false; /* read_callback_ sets the state for us */
2261
0
  }
2262
0
  obj->description[x] = '\0';
2263
2264
  /* read width */
2265
0
  if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->width, FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN))
2266
0
    return false; /* read_callback_ sets the state for us */
2267
2268
  /* read height */
2269
0
  if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->height, FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN))
2270
0
    return false; /* read_callback_ sets the state for us */
2271
2272
  /* read depth */
2273
0
  if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->depth, FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN))
2274
0
    return false; /* read_callback_ sets the state for us */
2275
2276
  /* read colors */
2277
0
  if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->colors, FLAC__STREAM_METADATA_PICTURE_COLORS_LEN))
2278
0
    return false; /* read_callback_ sets the state for us */
2279
2280
  /* read data */
2281
0
  if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &(obj->data_length), FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN))
2282
0
    return false; /* read_callback_ sets the state for us */
2283
0
  if(FLAC__bitreader_limit_remaining(decoder->private_->input) < obj->data_length){
2284
0
    FLAC__bitreader_limit_invalidate(decoder->private_->input);
2285
0
    return false;
2286
0
  }
2287
0
  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
0
  if(obj->data_length > 0) {
2292
0
    if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->data, obj->data_length))
2293
0
      return false; /* read_callback_ sets the state for us */
2294
0
  }
2295
2296
0
  return true;
2297
0
}
2298
2299
FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder)
2300
0
{
2301
0
  FLAC__uint32 x;
2302
0
  uint32_t i, skip;
2303
2304
  /* skip the version and flags bytes */
2305
0
  if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 24))
2306
0
    return false; /* read_callback_ sets the state for us */
2307
  /* get the size (in bytes) to skip */
2308
0
  skip = 0;
2309
0
  for(i = 0; i < 4; i++) {
2310
0
    if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
2311
0
      return false; /* read_callback_ sets the state for us */
2312
0
    skip <<= 7;
2313
0
    skip |= (x & 0x7f);
2314
0
  }
2315
  /* skip the rest of the tag */
2316
0
  if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, skip))
2317
0
    return false; /* read_callback_ sets the state for us */
2318
0
  return true;
2319
0
}
2320
2321
FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder)
2322
208k
{
2323
208k
  FLAC__uint32 x;
2324
208k
  FLAC__bool first = true;
2325
2326
  /* make sure we're byte aligned */
2327
208k
  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
208k
  while(1) {
2333
208k
    if(decoder->private_->cached) {
2334
0
      x = (FLAC__uint32)decoder->private_->lookahead;
2335
0
      decoder->private_->cached = false;
2336
0
    }
2337
208k
    else {
2338
208k
      if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
2339
0
        return false; /* read_callback_ sets the state for us */
2340
208k
    }
2341
208k
    if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
2342
208k
      decoder->private_->header_warmup[0] = (FLAC__byte)x;
2343
208k
      if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
2344
0
        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
208k
      if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
2349
0
        decoder->private_->lookahead = (FLAC__byte)x;
2350
0
        decoder->private_->cached = true;
2351
0
      }
2352
208k
      else if(x >> 1 == 0x7c) { /* MAGIC NUMBER for the last 6 sync bits and reserved 7th bit */
2353
208k
        decoder->private_->header_warmup[1] = (FLAC__byte)x;
2354
208k
        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
208k
        FLAC__bitreader_set_framesync_location(decoder->private_->input);
2359
208k
        if(!FLAC__stream_decoder_get_decode_position(decoder, &decoder->private_->last_seen_framesync))
2360
208k
          decoder->private_->last_seen_framesync = 0;
2361
208k
        return true;
2362
208k
      }
2363
208k
    }
2364
0
    if(first) {
2365
0
      send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
2366
0
      first = false;
2367
0
    }
2368
0
  }
2369
2370
0
  return true;
2371
208k
}
2372
2373
FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame, FLAC__bool do_full_decode)
2374
208k
{
2375
208k
  uint32_t channel;
2376
208k
  uint32_t i;
2377
208k
  uint32_t frame_crc; /* the one we calculate from the input stream */
2378
208k
  FLAC__uint32 x;
2379
2380
208k
  *got_a_frame = false;
2381
208k
  decoder->private_->side_subframe_in_use = false;
2382
2383
  /* init the CRC */
2384
208k
  frame_crc = 0;
2385
208k
  frame_crc = FLAC__CRC16_UPDATE(decoder->private_->header_warmup[0], frame_crc);
2386
208k
  frame_crc = FLAC__CRC16_UPDATE(decoder->private_->header_warmup[1], frame_crc);
2387
208k
  FLAC__bitreader_reset_read_crc16(decoder->private_->input, (FLAC__uint16)frame_crc);
2388
2389
208k
  if(!read_frame_header_(decoder))
2390
0
    return false;
2391
208k
  if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means we didn't sync on a valid header */
2392
0
    return true;
2393
208k
  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
488k
  for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
2396
    /*
2397
     * first figure the correct bits-per-sample of the subframe
2398
     */
2399
279k
    uint32_t bps = decoder->private_->frame.header.bits_per_sample;
2400
279k
    switch(decoder->private_->frame.header.channel_assignment) {
2401
206k
      case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
2402
        /* no adjustment needed */
2403
206k
        break;
2404
32.3k
      case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
2405
32.3k
        FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
2406
32.3k
        if(channel == 1)
2407
16.1k
          bps++;
2408
32.3k
        break;
2409
16.2k
      case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
2410
16.2k
        FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
2411
16.2k
        if(channel == 0)
2412
8.10k
          bps++;
2413
16.2k
        break;
2414
24.6k
      case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
2415
24.6k
        FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
2416
24.6k
        if(channel == 1)
2417
12.3k
          bps++;
2418
24.6k
        break;
2419
0
      default:
2420
0
        FLAC__ASSERT(0);
2421
279k
    }
2422
    /*
2423
     * now read it
2424
     */
2425
279k
    if(!read_subframe_(decoder, channel, bps, do_full_decode)){
2426
      /* read_callback_ sets the state for us */
2427
0
      if(decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM)
2428
0
        break;
2429
0
      else
2430
0
        return false;
2431
0
    }
2432
279k
    if(decoder->protected_->state != FLAC__STREAM_DECODER_READ_FRAME)
2433
0
      break;
2434
279k
  }
2435
2436
208k
  if(decoder->protected_->state != FLAC__STREAM_DECODER_END_OF_STREAM)
2437
208k
    if(!read_zero_padding_(decoder))
2438
0
      return false;
2439
2440
  /*
2441
   * Read the frame CRC-16 from the footer and check
2442
   */
2443
208k
  if(decoder->protected_->state == FLAC__STREAM_DECODER_READ_FRAME) {
2444
208k
    frame_crc = FLAC__bitreader_get_read_crc16(decoder->private_->input);
2445
208k
    if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__FRAME_FOOTER_CRC_LEN)) {
2446
      /* read_callback_ sets the state for us */
2447
0
      if(decoder->protected_->state != FLAC__STREAM_DECODER_END_OF_STREAM)
2448
0
        return false;
2449
0
    }
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
208k
    if(do_full_decode) {
2455
      /* Undo any special channel coding */
2456
208k
      undo_channel_coding(decoder);
2457
      /* Check whether decoded data actually fits bps */
2458
488k
      for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
2459
279k
        int shift_bits = 32 - decoder->private_->frame.header.bits_per_sample;
2460
279k
        int lower_limit = INT32_MIN >> shift_bits;
2461
279k
        int upper_limit = INT32_MAX >> shift_bits;
2462
28.2M
        for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
2463
          /* Check whether shift_bits MSBs are 'empty' by shifting up and down */
2464
28.0M
          if((decoder->private_->output[channel][i] < lower_limit) ||
2465
28.0M
             (decoder->private_->output[channel][i] > upper_limit)) {
2466
            /* Bad frame, emit error */
2467
0
            send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_OUT_OF_BOUNDS);
2468
0
            decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2469
0
            break;
2470
0
          }
2471
28.0M
        }
2472
279k
      }
2473
208k
    }
2474
208k
  }
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
208k
  if(decoder->private_->last_frame_is_set && decoder->protected_->state == FLAC__STREAM_DECODER_READ_FRAME && !decoder->private_->is_seeking && do_full_decode) {
2485
200k
    FLAC__ASSERT(decoder->private_->frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
2486
200k
    FLAC__ASSERT(decoder->private_->last_frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
2487
200k
    if(decoder->private_->last_frame.header.number.sample_number + decoder->private_->last_frame.header.blocksize < decoder->private_->frame.header.number.sample_number) {
2488
0
      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
0
      if(!decoder->private_->error_has_been_sent)
2494
0
        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
0
      if(decoder->private_->last_frame.header.sample_rate == decoder->private_->frame.header.sample_rate &&
2500
0
         decoder->private_->last_frame.header.channels == decoder->private_->frame.header.channels &&
2501
0
         decoder->private_->last_frame.header.bits_per_sample == decoder->private_->frame.header.bits_per_sample &&
2502
0
         decoder->private_->last_frame.header.blocksize >= 16) {
2503
0
        FLAC__Frame empty_frame;
2504
0
        FLAC__int32 * empty_buffer[FLAC__MAX_CHANNELS] = {NULL};
2505
0
        empty_frame.header = decoder->private_->last_frame.header;
2506
0
        empty_frame.footer.crc = 0;
2507
0
        for(i = 0; i < empty_frame.header.channels; i++) {
2508
0
          empty_buffer[i] = safe_calloc_(empty_frame.header.blocksize, sizeof(FLAC__int32));
2509
0
          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
0
        }
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
0
        if(padding_samples_needed > (5*empty_frame.header.sample_rate))
2521
0
          padding_samples_needed = 5*empty_frame.header.sample_rate;
2522
0
        if(padding_samples_needed > (50*empty_frame.header.blocksize))
2523
0
          padding_samples_needed = 50*empty_frame.header.blocksize;
2524
0
        while(padding_samples_needed){
2525
0
          empty_frame.header.number.sample_number += empty_frame.header.blocksize;
2526
0
          if(padding_samples_needed < empty_frame.header.blocksize)
2527
0
            empty_frame.header.blocksize = padding_samples_needed;
2528
0
          padding_samples_needed -= empty_frame.header.blocksize;
2529
0
          decoder->protected_->blocksize = empty_frame.header.blocksize;
2530
2531
0
          FLAC__ASSERT(empty_frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
2532
0
          decoder->private_->samples_decoded = empty_frame.header.number.sample_number + empty_frame.header.blocksize;
2533
2534
0
          for(channel = 0; channel < empty_frame.header.channels; channel++) {
2535
0
            empty_frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_CONSTANT;
2536
0
            empty_frame.subframes[channel].data.constant.value = 0;
2537
0
            empty_frame.subframes[channel].wasted_bits = 0;
2538
0
          }
2539
2540
0
          if(write_audio_frame_to_client_(decoder, &empty_frame, (const FLAC__int32 * const *)empty_buffer) != FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE) {
2541
0
            decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
2542
0
            for(i = 0; i < empty_frame.header.channels; i++)
2543
0
              if(empty_buffer[i] != NULL)
2544
0
                free(empty_buffer[i]);
2545
0
            return false;
2546
0
          }
2547
0
        }
2548
0
        for(i = 0; i < empty_frame.header.channels; i++)
2549
0
          if(empty_buffer[i] != NULL)
2550
0
            free(empty_buffer[i]);
2551
2552
0
      }
2553
0
    }
2554
200k
  }
2555
2556
208k
  decoder->private_->error_has_been_sent = false;
2557
2558
208k
  if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC || decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM) {
2559
0
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
2560
0
    decoder->private_->fuzzing_rewind_count++; /* To stop excessive rewinding, as it causes timeouts */
2561
0
#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
0
    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
0
      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
0
    }
2585
#ifndef NDEBUG
2586
    else{
2587
      flac_fprintf(stderr, "Rewinding, seeking not necessary\n");
2588
    }
2589
#endif
2590
0
  }
2591
208k
  else {
2592
208k
    *got_a_frame = true;
2593
208k
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
2594
208k
    decoder->private_->fuzzing_rewind_count = 0;
2595
208k
#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
208k
    if(decoder->private_->next_fixed_block_size)
2599
8.16k
      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
208k
    decoder->protected_->channels = decoder->private_->frame.header.channels;
2603
208k
    decoder->protected_->channel_assignment = decoder->private_->frame.header.channel_assignment;
2604
208k
    decoder->protected_->bits_per_sample = decoder->private_->frame.header.bits_per_sample;
2605
208k
    decoder->protected_->sample_rate = decoder->private_->frame.header.sample_rate;
2606
208k
    decoder->protected_->blocksize = decoder->private_->frame.header.blocksize;
2607
2608
208k
    FLAC__ASSERT(decoder->private_->frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
2609
208k
    decoder->private_->samples_decoded = decoder->private_->frame.header.number.sample_number + decoder->private_->frame.header.blocksize;
2610
2611
    /* write it */
2612
208k
    if(do_full_decode) {
2613
208k
      if(write_audio_frame_to_client_(decoder, &decoder->private_->frame, (const FLAC__int32 * const *)decoder->private_->output) != FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE) {
2614
0
        decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
2615
0
        return false;
2616
0
      }
2617
208k
    }
2618
208k
  }
2619
2620
208k
  decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2621
208k
  return true;
2622
208k
}
2623
2624
FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder)
2625
208k
{
2626
208k
  FLAC__uint32 x;
2627
208k
  FLAC__uint64 xx;
2628
208k
  uint32_t i, blocksize_hint = 0, sample_rate_hint = 0;
2629
208k
  FLAC__byte crc8, raw_header[16]; /* MAGIC NUMBER based on the maximum frame header size, including CRC */
2630
208k
  uint32_t raw_header_len;
2631
208k
  FLAC__bool is_unparseable = false;
2632
2633
208k
  FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
2634
2635
  /* init the raw header with the saved bits from synchronization */
2636
208k
  raw_header[0] = decoder->private_->header_warmup[0];
2637
208k
  raw_header[1] = decoder->private_->header_warmup[1];
2638
208k
  raw_header_len = 2;
2639
2640
  /* check to make sure that reserved bit is 0 */
2641
208k
  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
625k
  for(i = 0; i < 2; i++) {
2667
417k
    if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
2668
0
      return false; /* read_callback_ sets the state for us */
2669
417k
    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
0
      decoder->private_->lookahead = (FLAC__byte)x;
2672
0
      decoder->private_->cached = true;
2673
0
      send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
2674
0
      decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2675
0
      return true;
2676
0
    }
2677
417k
    raw_header[raw_header_len++] = (FLAC__byte)x;
2678
417k
  }
2679
2680
208k
  switch(x = raw_header[2] >> 4) {
2681
0
    case 0:
2682
0
      is_unparseable = true;
2683
0
      break;
2684
196
    case 1:
2685
196
      decoder->private_->frame.header.blocksize = 192;
2686
196
      break;
2687
196
    case 2:
2688
334
    case 3:
2689
400
    case 4:
2690
451
    case 5:
2691
451
      decoder->private_->frame.header.blocksize = 576 << (x-2);
2692
451
      break;
2693
205k
    case 6:
2694
207k
    case 7:
2695
207k
      blocksize_hint = x;
2696
207k
      break;
2697
219
    case 8:
2698
444
    case 9:
2699
578
    case 10:
2700
679
    case 11:
2701
862
    case 12:
2702
897
    case 13:
2703
926
    case 14:
2704
947
    case 15:
2705
947
      decoder->private_->frame.header.blocksize = 256 << (x-8);
2706
947
      break;
2707
0
    default:
2708
0
      FLAC__ASSERT(0);
2709
0
      break;
2710
208k
  }
2711
2712
208k
  switch(x = raw_header[2] & 0x0f) {
2713
16.4k
    case 0:
2714
16.4k
      if(decoder->private_->has_stream_info)
2715
16.4k
        decoder->private_->frame.header.sample_rate = decoder->private_->stream_info.data.stream_info.sample_rate;
2716
0
      else
2717
0
        is_unparseable = true;
2718
16.4k
      break;
2719
309
    case 1:
2720
309
      decoder->private_->frame.header.sample_rate = 88200;
2721
309
      break;
2722
494
    case 2:
2723
494
      decoder->private_->frame.header.sample_rate = 176400;
2724
494
      break;
2725
1.30k
    case 3:
2726
1.30k
      decoder->private_->frame.header.sample_rate = 192000;
2727
1.30k
      break;
2728
1.97k
    case 4:
2729
1.97k
      decoder->private_->frame.header.sample_rate = 8000;
2730
1.97k
      break;
2731
194
    case 5:
2732
194
      decoder->private_->frame.header.sample_rate = 16000;
2733
194
      break;
2734
7.93k
    case 6:
2735
7.93k
      decoder->private_->frame.header.sample_rate = 22050;
2736
7.93k
      break;
2737
360
    case 7:
2738
360
      decoder->private_->frame.header.sample_rate = 24000;
2739
360
      break;
2740
8.17k
    case 8:
2741
8.17k
      decoder->private_->frame.header.sample_rate = 32000;
2742
8.17k
      break;
2743
34.5k
    case 9:
2744
34.5k
      decoder->private_->frame.header.sample_rate = 44100;
2745
34.5k
      break;
2746
437
    case 10:
2747
437
      decoder->private_->frame.header.sample_rate = 48000;
2748
437
      break;
2749
4.30k
    case 11:
2750
4.30k
      decoder->private_->frame.header.sample_rate = 96000;
2751
4.30k
      break;
2752
7.17k
    case 12:
2753
86.5k
    case 13:
2754
132k
    case 14:
2755
132k
      sample_rate_hint = x;
2756
132k
      break;
2757
0
    case 15:
2758
0
      send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
2759
0
      decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2760
0
      return true;
2761
0
    default:
2762
0
      FLAC__ASSERT(0);
2763
208k
  }
2764
2765
208k
  x = (uint32_t)(raw_header[3] >> 4);
2766
208k
  if(x & 8) {
2767
36.6k
    decoder->private_->frame.header.channels = 2;
2768
36.6k
    switch(x & 7) {
2769
16.1k
      case 0:
2770
16.1k
        decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE;
2771
16.1k
        break;
2772
8.10k
      case 1:
2773
8.10k
        decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE;
2774
8.10k
        break;
2775
12.3k
      case 2:
2776
12.3k
        decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_MID_SIDE;
2777
12.3k
        break;
2778
0
      default:
2779
0
        is_unparseable = true;
2780
0
        break;
2781
36.6k
    }
2782
36.6k
  }
2783
172k
  else {
2784
172k
    decoder->private_->frame.header.channels = (uint32_t)x + 1;
2785
172k
    decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT;
2786
172k
  }
2787
2788
208k
  switch(x = (uint32_t)(raw_header[3] & 0x0e) >> 1) {
2789
4.45k
    case 0:
2790
4.45k
      if(decoder->private_->has_stream_info)
2791
4.45k
        decoder->private_->frame.header.bits_per_sample = decoder->private_->stream_info.data.stream_info.bits_per_sample;
2792
0
      else
2793
0
        is_unparseable = true;
2794
4.45k
      break;
2795
3.89k
    case 1:
2796
3.89k
      decoder->private_->frame.header.bits_per_sample = 8;
2797
3.89k
      break;
2798
265
    case 2:
2799
265
      decoder->private_->frame.header.bits_per_sample = 12;
2800
265
      break;
2801
0
    case 3:
2802
0
      is_unparseable = true;
2803
0
      break;
2804
4.53k
    case 4:
2805
4.53k
      decoder->private_->frame.header.bits_per_sample = 16;
2806
4.53k
      break;
2807
362
    case 5:
2808
362
      decoder->private_->frame.header.bits_per_sample = 20;
2809
362
      break;
2810
11.5k
    case 6:
2811
11.5k
      decoder->private_->frame.header.bits_per_sample = 24;
2812
11.5k
      break;
2813
183k
    case 7:
2814
183k
      decoder->private_->frame.header.bits_per_sample = 32;
2815
183k
      break;
2816
0
    default:
2817
0
      FLAC__ASSERT(0);
2818
0
      break;
2819
208k
  }
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
208k
  if(
2829
208k
    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
208k
    (decoder->private_->has_stream_info && decoder->private_->stream_info.data.stream_info.min_blocksize != decoder->private_->stream_info.data.stream_info.max_blocksize)
2832
208k
  ) { /* variable blocksize */
2833
0
    if(!FLAC__bitreader_read_utf8_uint64(decoder->private_->input, &xx, raw_header, &raw_header_len))
2834
0
      return false; /* read_callback_ sets the state for us */
2835
0
    if(xx == FLAC__U64L(0xffffffffffffffff)) { /* i.e. non-UTF8 code... */
2836
0
      decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
2837
0
      decoder->private_->cached = true;
2838
0
      send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
2839
0
      decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2840
0
      return true;
2841
0
    }
2842
0
    decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
2843
0
    decoder->private_->frame.header.number.sample_number = xx;
2844
0
  }
2845
208k
  else { /* fixed blocksize */
2846
208k
    if(!FLAC__bitreader_read_utf8_uint32(decoder->private_->input, &x, raw_header, &raw_header_len))
2847
0
      return false; /* read_callback_ sets the state for us */
2848
208k
    if(x == 0xffffffff) { /* i.e. non-UTF8 code... */
2849
0
      decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
2850
0
      decoder->private_->cached = true;
2851
0
      send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
2852
0
      decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2853
0
      return true;
2854
0
    }
2855
208k
    decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER;
2856
208k
    decoder->private_->frame.header.number.frame_number = x;
2857
208k
  }
2858
2859
208k
  if(blocksize_hint) {
2860
207k
    if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
2861
0
      return false; /* read_callback_ sets the state for us */
2862
207k
    raw_header[raw_header_len++] = (FLAC__byte)x;
2863
207k
    if(blocksize_hint == 7) {
2864
1.83k
      FLAC__uint32 _x;
2865
1.83k
      if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &_x, 8))
2866
0
        return false; /* read_callback_ sets the state for us */
2867
1.83k
      raw_header[raw_header_len++] = (FLAC__byte)_x;
2868
1.83k
      x = (x << 8) | _x;
2869
1.83k
    }
2870
207k
    decoder->private_->frame.header.blocksize = x+1;
2871
207k
    if(decoder->private_->frame.header.blocksize > 65535) { /* invalid blocksize (65536) specified */
2872
0
      decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
2873
0
      decoder->private_->cached = true;
2874
0
      send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
2875
0
      decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2876
0
      return true;
2877
0
    }
2878
2879
207k
  }
2880
2881
208k
  if(sample_rate_hint) {
2882
132k
    if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
2883
0
      return false; /* read_callback_ sets the state for us */
2884
132k
    raw_header[raw_header_len++] = (FLAC__byte)x;
2885
132k
    if(sample_rate_hint != 12) {
2886
125k
      FLAC__uint32 _x;
2887
125k
      if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &_x, 8))
2888
0
        return false; /* read_callback_ sets the state for us */
2889
125k
      raw_header[raw_header_len++] = (FLAC__byte)_x;
2890
125k
      x = (x << 8) | _x;
2891
125k
    }
2892
132k
    if(sample_rate_hint == 12)
2893
7.17k
      decoder->private_->frame.header.sample_rate = x*1000;
2894
125k
    else if(sample_rate_hint == 13)
2895
79.3k
      decoder->private_->frame.header.sample_rate = x;
2896
45.6k
    else
2897
45.6k
      decoder->private_->frame.header.sample_rate = x*10;
2898
132k
  }
2899
2900
  /* read the CRC-8 byte */
2901
208k
  if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
2902
0
    return false; /* read_callback_ sets the state for us */
2903
208k
  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
208k
  if(decoder->private_->fuzzing_rewind_count > 8 && (FLAC__crc8(raw_header, raw_header_len) << 4) != (crc8 << 4)) {
2909
0
#endif
2910
0
    send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
2911
0
    decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2912
0
    return true;
2913
0
  }
2914
2915
  /* calculate the sample number from the frame number if needed */
2916
208k
  decoder->private_->next_fixed_block_size = 0;
2917
208k
  if(decoder->private_->frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER) {
2918
208k
    x = decoder->private_->frame.header.number.frame_number;
2919
208k
    decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
2920
208k
    if(decoder->private_->fixed_block_size)
2921
200k
      decoder->private_->frame.header.number.sample_number = (FLAC__uint64)decoder->private_->fixed_block_size * (FLAC__uint64)x;
2922
8.16k
    else if(decoder->private_->has_stream_info) {
2923
8.16k
      if(decoder->private_->stream_info.data.stream_info.min_blocksize == decoder->private_->stream_info.data.stream_info.max_blocksize) {
2924
8.16k
        decoder->private_->frame.header.number.sample_number = (FLAC__uint64)decoder->private_->stream_info.data.stream_info.min_blocksize * (FLAC__uint64)x;
2925
8.16k
        decoder->private_->next_fixed_block_size = decoder->private_->stream_info.data.stream_info.max_blocksize;
2926
8.16k
      }
2927
0
      else
2928
0
        is_unparseable = true;
2929
8.16k
    }
2930
0
    else if(x == 0) {
2931
0
      decoder->private_->frame.header.number.sample_number = 0;
2932
0
      decoder->private_->next_fixed_block_size = decoder->private_->frame.header.blocksize;
2933
0
    }
2934
0
    else {
2935
      /* can only get here if the stream has invalid frame numbering and no STREAMINFO, so assume it's not the last (possibly short) frame */
2936
0
      decoder->private_->frame.header.number.sample_number = (FLAC__uint64)decoder->private_->frame.header.blocksize * (FLAC__uint64)x;
2937
0
    }
2938
208k
  }
2939
2940
208k
  if(is_unparseable) {
2941
0
    send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
2942
0
    decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2943
0
    return true;
2944
0
  }
2945
2946
208k
  return true;
2947
208k
}
2948
2949
FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, uint32_t channel, uint32_t bps, FLAC__bool do_full_decode)
2950
279k
{
2951
279k
  FLAC__uint32 x;
2952
279k
  FLAC__bool wasted_bits;
2953
279k
  uint32_t i;
2954
2955
279k
  if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8)) /* MAGIC NUMBER */
2956
0
    return false; /* read_callback_ sets the state for us */
2957
2958
279k
  wasted_bits = (x & 1);
2959
279k
  x &= 0xfe;
2960
2961
279k
  if(wasted_bits) {
2962
29.3k
    uint32_t u;
2963
29.3k
    if(!FLAC__bitreader_read_unary_unsigned(decoder->private_->input, &u))
2964
0
      return false; /* read_callback_ sets the state for us */
2965
29.3k
    decoder->private_->frame.subframes[channel].wasted_bits = u+1;
2966
29.3k
    if (decoder->private_->frame.subframes[channel].wasted_bits >= bps) {
2967
0
      send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
2968
0
      decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2969
0
      return true;
2970
0
    }
2971
29.3k
    bps -= decoder->private_->frame.subframes[channel].wasted_bits;
2972
29.3k
  }
2973
250k
  else
2974
250k
    decoder->private_->frame.subframes[channel].wasted_bits = 0;
2975
2976
  /*
2977
   * Lots of magic numbers here
2978
   */
2979
279k
  if(x & 0x80) {
2980
0
    send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
2981
0
    decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2982
0
    return true;
2983
0
  }
2984
279k
  else if(x == 0) {
2985
83.6k
    if(!read_subframe_constant_(decoder, channel, bps, do_full_decode))
2986
0
      return false;
2987
83.6k
  }
2988
196k
  else if(x == 2) {
2989
46.7k
    if(!read_subframe_verbatim_(decoder, channel, bps, do_full_decode))
2990
0
      return false;
2991
46.7k
  }
2992
149k
  else if(x < 16) {
2993
0
    send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
2994
0
    decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2995
0
    return true;
2996
0
  }
2997
149k
  else if(x <= 24) {
2998
129k
    uint32_t predictor_order = (x>>1)&7;
2999
129k
    if(decoder->private_->frame.header.blocksize <= predictor_order){
3000
0
      send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
3001
0
      decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
3002
0
      return true;
3003
0
    }
3004
129k
    if(!read_subframe_fixed_(decoder, channel, bps, predictor_order, do_full_decode))
3005
0
      return false;
3006
129k
    if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means bad sync or got corruption */
3007
0
      return true;
3008
129k
  }
3009
19.8k
  else if(x < 64) {
3010
0
    send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
3011
0
    decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
3012
0
    return true;
3013
0
  }
3014
19.8k
  else {
3015
19.8k
    uint32_t predictor_order = ((x>>1)&31)+1;
3016
19.8k
    if(decoder->private_->frame.header.blocksize <= predictor_order){
3017
0
      send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
3018
0
      decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
3019
0
      return true;
3020
0
    }
3021
19.8k
    if(!read_subframe_lpc_(decoder, channel, bps, predictor_order, do_full_decode))
3022
0
      return false;
3023
19.8k
    if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means bad sync or got corruption */
3024
0
      return true;
3025
19.8k
  }
3026
3027
279k
  if(wasted_bits && do_full_decode) {
3028
29.3k
    x = decoder->private_->frame.subframes[channel].wasted_bits;
3029
29.3k
    if((bps + x) < 33) {
3030
311k
      for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
3031
295k
        uint32_t val = decoder->private_->output[channel][i];
3032
295k
        decoder->private_->output[channel][i] = (val << x);
3033
295k
      }
3034
15.2k
    }
3035
14.1k
    else {
3036
      /* When there are wasted bits, bps is never 33 and so
3037
       * side_subframe is never already in use */
3038
14.1k
      FLAC__ASSERT(!decoder->private_->side_subframe_in_use);
3039
14.1k
      decoder->private_->side_subframe_in_use = true;
3040
449k
      for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
3041
435k
        uint64_t val = decoder->private_->output[channel][i];
3042
435k
        decoder->private_->side_subframe[i] = (val << x);
3043
435k
      }
3044
14.1k
    }
3045
29.3k
  }
3046
3047
279k
  return true;
3048
279k
}
3049
3050
FLAC__bool read_subframe_constant_(FLAC__StreamDecoder *decoder, uint32_t channel, uint32_t bps, FLAC__bool do_full_decode)
3051
83.6k
{
3052
83.6k
  FLAC__Subframe_Constant *subframe = &decoder->private_->frame.subframes[channel].data.constant;
3053
83.6k
  FLAC__int64 x;
3054
83.6k
  uint32_t i;
3055
3056
83.6k
  decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_CONSTANT;
3057
3058
83.6k
  if(!FLAC__bitreader_read_raw_int64(decoder->private_->input, &x, bps))
3059
0
    return false; /* read_callback_ sets the state for us */
3060
3061
83.6k
  subframe->value = x;
3062
3063
  /* decode the subframe */
3064
83.6k
  if(do_full_decode) {
3065
83.6k
    if(bps <= 32) {
3066
82.5k
      FLAC__int32 *output = decoder->private_->output[channel];
3067
1.57M
      for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
3068
1.49M
        output[i] = x;
3069
82.5k
    } else {
3070
1.01k
      FLAC__int64 *output = decoder->private_->side_subframe;
3071
1.01k
      decoder->private_->side_subframe_in_use = true;
3072
18.4k
      for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
3073
17.4k
        output[i] = x;
3074
1.01k
    }
3075
83.6k
  }
3076
3077
83.6k
  return true;
3078
83.6k
}
3079
3080
FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, uint32_t channel, uint32_t bps, const uint32_t order, FLAC__bool do_full_decode)
3081
129k
{
3082
129k
  FLAC__Subframe_Fixed *subframe = &decoder->private_->frame.subframes[channel].data.fixed;
3083
129k
  FLAC__int64 i64;
3084
129k
  FLAC__uint32 u32;
3085
129k
  uint32_t u;
3086
3087
129k
  decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_FIXED;
3088
3089
129k
  subframe->residual = decoder->private_->residual[channel];
3090
129k
  subframe->order = order;
3091
3092
  /* read warm-up samples */
3093
223k
  for(u = 0; u < order; u++) {
3094
93.7k
    if(!FLAC__bitreader_read_raw_int64(decoder->private_->input, &i64, bps))
3095
0
      return false; /* read_callback_ sets the state for us */
3096
93.7k
    subframe->warmup[u] = i64;
3097
93.7k
  }
3098
3099
  /* read entropy coding method info */
3100
129k
  if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN))
3101
0
    return false; /* read_callback_ sets the state for us */
3102
129k
  subframe->entropy_coding_method.type = (FLAC__EntropyCodingMethodType)u32;
3103
129k
  switch(subframe->entropy_coding_method.type) {
3104
46.3k
    case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
3105
129k
    case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2:
3106
129k
      if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
3107
0
        return false; /* read_callback_ sets the state for us */
3108
129k
      if((decoder->private_->frame.header.blocksize >> u32 < order) ||
3109
129k
         (decoder->private_->frame.header.blocksize % (1 << u32) > 0)) {
3110
0
        send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
3111
0
        decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
3112
0
        return true;
3113
0
      }
3114
129k
      subframe->entropy_coding_method.data.partitioned_rice.order = u32;
3115
129k
      subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel];
3116
129k
      break;
3117
0
    default:
3118
0
      send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
3119
0
      decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
3120
0
      return true;
3121
129k
  }
3122
3123
  /* read residual */
3124
129k
  switch(subframe->entropy_coding_method.type) {
3125
46.3k
    case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
3126
129k
    case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2:
3127
129k
      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))
3128
0
        return false;
3129
129k
      break;
3130
129k
    default:
3131
0
      FLAC__ASSERT(0);
3132
129k
  }
3133
3134
  /* decode the subframe */
3135
129k
  if(do_full_decode) {
3136
129k
    if(bps < 33){
3137
123k
      uint32_t i;
3138
213k
      for(i = 0; i < order; i++)
3139
90.3k
        decoder->private_->output[channel][i] = subframe->warmup[i];
3140
123k
      if(bps+order <= 32)
3141
68.1k
        FLAC__fixed_restore_signal(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, order, decoder->private_->output[channel]+order);
3142
55.0k
      else
3143
55.0k
        FLAC__fixed_restore_signal_wide(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, order, decoder->private_->output[channel]+order);
3144
123k
    }
3145
6.52k
    else {
3146
6.52k
      decoder->private_->side_subframe_in_use = true;
3147
6.52k
      memcpy(decoder->private_->side_subframe, subframe->warmup, sizeof(FLAC__int64) * order);
3148
6.52k
      FLAC__fixed_restore_signal_wide_33bit(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, order, decoder->private_->side_subframe+order);
3149
6.52k
    }
3150
129k
  }
3151
3152
129k
  return true;
3153
129k
}
3154
3155
FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, uint32_t channel, uint32_t bps, const uint32_t order, FLAC__bool do_full_decode)
3156
19.8k
{
3157
19.8k
  FLAC__Subframe_LPC *subframe = &decoder->private_->frame.subframes[channel].data.lpc;
3158
19.8k
  FLAC__int32 i32;
3159
19.8k
  FLAC__int64 i64;
3160
19.8k
  FLAC__uint32 u32;
3161
19.8k
  uint32_t u;
3162
3163
19.8k
  decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_LPC;
3164
3165
19.8k
  subframe->residual = decoder->private_->residual[channel];
3166
19.8k
  subframe->order = order;
3167
3168
  /* read warm-up samples */
3169
123k
  for(u = 0; u < order; u++) {
3170
103k
    if(!FLAC__bitreader_read_raw_int64(decoder->private_->input, &i64, bps))
3171
0
      return false; /* read_callback_ sets the state for us */
3172
103k
    subframe->warmup[u] = i64;
3173
103k
  }
3174
3175
  /* read qlp coeff precision */
3176
19.8k
  if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN))
3177
0
    return false; /* read_callback_ sets the state for us */
3178
19.8k
  if(u32 == (1u << FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN) - 1) {
3179
0
    send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
3180
0
    decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
3181
0
    return true;
3182
0
  }
3183
19.8k
  subframe->qlp_coeff_precision = u32+1;
3184
3185
  /* read qlp shift */
3186
19.8k
  if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i32, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN))
3187
0
    return false; /* read_callback_ sets the state for us */
3188
19.8k
  if(i32 < 0) {
3189
0
    send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
3190
0
    decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
3191
0
    return true;
3192
0
  }
3193
19.8k
  subframe->quantization_level = i32;
3194
3195
  /* read quantized lp coefficiencts */
3196
123k
  for(u = 0; u < order; u++) {
3197
103k
    if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i32, subframe->qlp_coeff_precision))
3198
0
      return false; /* read_callback_ sets the state for us */
3199
103k
    subframe->qlp_coeff[u] = i32;
3200
103k
  }
3201
3202
  /* read entropy coding method info */
3203
19.8k
  if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN))
3204
0
    return false; /* read_callback_ sets the state for us */
3205
19.8k
  subframe->entropy_coding_method.type = (FLAC__EntropyCodingMethodType)u32;
3206
19.8k
  switch(subframe->entropy_coding_method.type) {
3207
13.7k
    case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
3208
19.8k
    case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2:
3209
19.8k
      if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
3210
0
        return false; /* read_callback_ sets the state for us */
3211
19.8k
      if((decoder->private_->frame.header.blocksize >> u32 < order) ||
3212
19.8k
         (decoder->private_->frame.header.blocksize % (1 << u32) > 0)) {
3213
0
        send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
3214
0
        decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
3215
0
        return true;
3216
0
      }
3217
19.8k
      subframe->entropy_coding_method.data.partitioned_rice.order = u32;
3218
19.8k
      subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel];
3219
19.8k
      break;
3220
0
    default:
3221
0
      send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
3222
0
      decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
3223
0
      return true;
3224
19.8k
  }
3225
3226
  /* read residual */
3227
19.8k
  switch(subframe->entropy_coding_method.type) {
3228
13.7k
    case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
3229
19.8k
    case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2:
3230
19.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))
3231
0
        return false;
3232
19.8k
      break;
3233
19.8k
    default:
3234
0
      FLAC__ASSERT(0);
3235
19.8k
  }
3236
3237
  /* decode the subframe */
3238
19.8k
  if(do_full_decode) {
3239
19.8k
    if(bps <= 32) {
3240
17.8k
      uint32_t i;
3241
109k
      for(i = 0; i < order; i++)
3242
92.0k
        decoder->private_->output[channel][i] = subframe->warmup[i];
3243
17.8k
      if(FLAC__lpc_max_residual_bps(bps, subframe->qlp_coeff, order, subframe->quantization_level) <= 32 &&
3244
8.13k
         FLAC__lpc_max_prediction_before_shift_bps(bps, subframe->qlp_coeff, order) <= 32)
3245
7.00k
        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);
3246
10.8k
      else
3247
10.8k
        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);
3248
17.8k
    }
3249
2.01k
    else {
3250
2.01k
      decoder->private_->side_subframe_in_use = true;
3251
2.01k
      memcpy(decoder->private_->side_subframe, subframe->warmup, sizeof(FLAC__int64) * order);
3252
2.01k
      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);
3253
2.01k
    }
3254
19.8k
  }
3255
3256
19.8k
  return true;
3257
19.8k
}
3258
3259
FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, uint32_t channel, uint32_t bps, FLAC__bool do_full_decode)
3260
46.7k
{
3261
46.7k
  FLAC__Subframe_Verbatim *subframe = &decoder->private_->frame.subframes[channel].data.verbatim;
3262
46.7k
  uint32_t i;
3263
3264
46.7k
  decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_VERBATIM;
3265
3266
46.7k
  if(bps < 33) {
3267
45.8k
    FLAC__int32 x, *residual = decoder->private_->residual[channel];
3268
3269
45.8k
    subframe->data_type = FLAC__VERBATIM_SUBFRAME_DATA_TYPE_INT32;
3270
45.8k
    subframe->data.int32 = residual;
3271
3272
3.22M
    for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
3273
3.18M
      if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &x, bps))
3274
0
        return false; /* read_callback_ sets the state for us */
3275
3.18M
      residual[i] = x;
3276
3.18M
    }
3277
3278
    /* decode the subframe */
3279
45.8k
    if(do_full_decode)
3280
45.8k
      memcpy(decoder->private_->output[channel], subframe->data.int32, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize);
3281
45.8k
  }
3282
926
  else {
3283
926
    FLAC__int64 x, *side = decoder->private_->side_subframe;
3284
3285
926
    subframe->data_type = FLAC__VERBATIM_SUBFRAME_DATA_TYPE_INT64;
3286
926
    subframe->data.int64 = side;
3287
926
    decoder->private_->side_subframe_in_use = true;
3288
3289
1.76M
    for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
3290
1.76M
      if(!FLAC__bitreader_read_raw_int64(decoder->private_->input, &x, bps))
3291
0
        return false; /* read_callback_ sets the state for us */
3292
1.76M
      side[i] = x;
3293
1.76M
    }
3294
926
  }
3295
3296
46.7k
  return true;
3297
46.7k
}
3298
3299
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)
3300
149k
{
3301
149k
  FLAC__uint32 rice_parameter;
3302
149k
  int i;
3303
149k
  uint32_t partition, sample, u;
3304
149k
  const uint32_t partitions = 1u << partition_order;
3305
149k
  const uint32_t partition_samples = decoder->private_->frame.header.blocksize >> partition_order;
3306
149k
  const uint32_t plen = is_extended? FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_PARAMETER_LEN : FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
3307
149k
  const uint32_t pesc = is_extended? FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_ESCAPE_PARAMETER : FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
3308
3309
  /* invalid predictor and partition orders mush be handled in the callers */
3310
149k
  FLAC__ASSERT(partition_order > 0? partition_samples >= predictor_order : decoder->private_->frame.header.blocksize >= predictor_order);
3311
3312
149k
  if(!FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, flac_max(6u, partition_order))) {
3313
0
    decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
3314
0
    return false;
3315
0
  }
3316
3317
149k
  sample = 0;
3318
1.78M
  for(partition = 0; partition < partitions; partition++) {
3319
1.64M
    if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &rice_parameter, plen))
3320
0
      return false; /* read_callback_ sets the state for us */
3321
1.64M
    partitioned_rice_contents->parameters[partition] = rice_parameter;
3322
1.64M
    if(rice_parameter < pesc) {
3323
1.60M
      partitioned_rice_contents->raw_bits[partition] = 0;
3324
1.60M
      u = (partition == 0) ? partition_samples - predictor_order : partition_samples;
3325
1.60M
      if(!decoder->private_->local_bitreader_read_rice_signed_block(decoder->private_->input, residual + sample, u, rice_parameter)){
3326
0
        if(decoder->protected_->state == FLAC__STREAM_DECODER_READ_FRAME) {
3327
          /* no error was set, read_callback_ didn't set it, so
3328
           * invalid rice symbol was found */
3329
0
          send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
3330
0
          decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
3331
0
          return true;
3332
0
        }
3333
0
        else
3334
0
          return false; /* read_callback_ sets the state for us */
3335
0
      }
3336
1.60M
      sample += u;
3337
1.60M
    }
3338
32.9k
    else {
3339
32.9k
      if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN))
3340
0
        return false; /* read_callback_ sets the state for us */
3341
32.9k
      partitioned_rice_contents->raw_bits[partition] = rice_parameter;
3342
32.9k
      if(rice_parameter == 0) {
3343
0
        for(u = (partition == 0)? predictor_order : 0; u < partition_samples; u++, sample++)
3344
0
          residual[sample] = 0;
3345
0
      }
3346
32.9k
      else{
3347
708k
        for(u = (partition == 0)? predictor_order : 0; u < partition_samples; u++, sample++) {
3348
675k
          if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i, rice_parameter))
3349
0
            return false; /* read_callback_ sets the state for us */
3350
675k
          residual[sample] = i;
3351
675k
        }
3352
32.9k
      }
3353
32.9k
    }
3354
1.64M
  }
3355
3356
149k
  return true;
3357
149k
}
3358
3359
FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder)
3360
208k
{
3361
208k
  if(!FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)) {
3362
102k
    FLAC__uint32 zero = 0;
3363
102k
    if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &zero, FLAC__bitreader_bits_left_for_byte_alignment(decoder->private_->input)))
3364
0
      return false; /* read_callback_ sets the state for us */
3365
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
3366
    if(zero != 0) {
3367
      send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
3368
      decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
3369
    }
3370
#endif
3371
102k
  }
3372
208k
  return true;
3373
208k
}
3374
3375
FLAC__bool read_callback_(FLAC__byte buffer[], size_t *bytes, void *client_data)
3376
242k
{
3377
242k
  FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder *)client_data;
3378
3379
242k
  if(
3380
242k
#if FLAC__HAS_OGG
3381
    /* see [1] HACK NOTE below for why we don't call the eof_callback when decoding Ogg FLAC */
3382
242k
    !decoder->private_->is_ogg &&
3383
242k
#endif
3384
242k
    decoder->private_->eof_callback && decoder->private_->eof_callback(decoder, decoder->private_->client_data)
3385
242k
  ) {
3386
0
    *bytes = 0;
3387
0
    decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
3388
0
    return false;
3389
0
  }
3390
242k
  else if(*bytes > 0) {
3391
    /* While seeking, it is possible for our seek to land in the
3392
     * middle of audio data that looks exactly like a frame header
3393
     * from a future version of an encoder.  When that happens, our
3394
     * error callback will get an
3395
     * FLAC__STREAM_DECODER_UNPARSEABLE_STREAM and increment its
3396
     * unparseable_frame_count.  But there is a remote possibility
3397
     * that it is properly synced at such a "future-codec frame",
3398
     * so to make sure, we wait to see many "unparseable" errors in
3399
     * a row before bailing out.
3400
     */
3401
242k
    if(decoder->private_->is_seeking && decoder->private_->unparseable_frame_count > 20) {
3402
0
      decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
3403
0
      return false;
3404
0
    }
3405
242k
    else {
3406
242k
      const FLAC__StreamDecoderReadStatus status =
3407
242k
#if FLAC__HAS_OGG
3408
242k
        decoder->private_->is_ogg?
3409
0
        read_callback_ogg_aspect_(decoder, buffer, bytes) :
3410
242k
#endif
3411
242k
        decoder->private_->read_callback(decoder, buffer, bytes, decoder->private_->client_data)
3412
242k
      ;
3413
242k
      if(status == FLAC__STREAM_DECODER_READ_STATUS_ABORT) {
3414
0
#if FLAC__HAS_OGG
3415
0
        if(decoder->private_->ogg_decoder_aspect_allocation_failure) {
3416
0
          decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
3417
0
          return false;
3418
0
        }
3419
0
#endif
3420
0
        decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
3421
0
        return false;
3422
0
      }
3423
242k
      else if(*bytes == 0) {
3424
0
        if(status == FLAC__STREAM_DECODER_READ_STATUS_END_OF_LINK) {
3425
0
          if(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input))
3426
0
            decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_LINK;
3427
0
          else
3428
0
            decoder->protected_->state = FLAC__STREAM_DECODER_OGG_ERROR;
3429
0
          return false;
3430
0
        }
3431
0
        else if(
3432
0
          status == FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM ||
3433
0
          (
3434
0
#if FLAC__HAS_OGG
3435
            /* see [1] HACK NOTE below for why we don't call the eof_callback when decoding Ogg FLAC */
3436
0
            !decoder->private_->is_ogg &&
3437
0
#endif
3438
0
            decoder->private_->eof_callback && decoder->private_->eof_callback(decoder, decoder->private_->client_data)
3439
0
          )
3440
0
        ) {
3441
0
          decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
3442
0
          return false;
3443
0
        }
3444
0
        else
3445
0
          return true;
3446
0
      }
3447
242k
      else
3448
242k
        return true;
3449
242k
    }
3450
242k
  }
3451
0
  else {
3452
    /* abort to avoid a deadlock */
3453
0
    decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
3454
0
    return false;
3455
0
  }
3456
  /* [1] @@@ HACK NOTE: The end-of-stream checking has to be hacked around
3457
   * for Ogg FLAC.  This is because the ogg decoder aspect can lose sync
3458
   * and at the same time hit the end of the stream (for example, seeking
3459
   * to a point that is after the beginning of the last Ogg page).  There
3460
   * is no way to report an Ogg sync loss through the callbacks (see note
3461
   * in read_callback_ogg_aspect_()) so it returns CONTINUE with *bytes==0.
3462
   * So to keep the decoder from stopping at this point we gate the call
3463
   * to the eof_callback and let the Ogg decoder aspect set the
3464
   * end-of-stream state when it is needed.
3465
   */
3466
242k
}
3467
3468
#if defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) && !defined(FUZZING_BUILD_MODE_FLAC_SANITIZE_SIGNED_INTEGER_OVERFLOW)
3469
/* The attribute below is to silence the undefined sanitizer of oss-fuzz.
3470
 * Because fuzzing feeds bogus predictors and residual samples to the
3471
 * decoder, having overflows in this section is unavoidable. Also,
3472
 * because the calculated values are audio path only, there is no
3473
 * potential for security problems */
3474
__attribute__((no_sanitize("signed-integer-overflow")))
3475
#endif
3476
208k
void undo_channel_coding(FLAC__StreamDecoder *decoder) {
3477
208k
  uint32_t i;
3478
208k
  switch(decoder->private_->frame.header.channel_assignment) {
3479
172k
  case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
3480
    /* do nothing */
3481
172k
    break;
3482
16.1k
  case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
3483
16.1k
    FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
3484
16.1k
    FLAC__ASSERT(decoder->private_->side_subframe_in_use != /* logical XOR */ (decoder->private_->frame.header.bits_per_sample < 32));
3485
518k
    for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
3486
501k
      if(decoder->private_->side_subframe_in_use)
3487
353k
        decoder->private_->output[1][i] = decoder->private_->output[0][i] - decoder->private_->side_subframe[i];
3488
148k
      else
3489
148k
        decoder->private_->output[1][i] = decoder->private_->output[0][i] - decoder->private_->output[1][i];
3490
16.1k
    break;
3491
8.10k
  case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
3492
8.10k
    FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
3493
8.10k
    FLAC__ASSERT(decoder->private_->side_subframe_in_use != /* logical XOR */ (decoder->private_->frame.header.bits_per_sample < 32));
3494
323k
    for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
3495
315k
      if(decoder->private_->side_subframe_in_use)
3496
272k
        decoder->private_->output[0][i] = decoder->private_->output[1][i] + decoder->private_->side_subframe[i];
3497
43.1k
      else
3498
43.1k
        decoder->private_->output[0][i] += decoder->private_->output[1][i];
3499
8.10k
    break;
3500
12.3k
  case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
3501
12.3k
    FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
3502
12.3k
    FLAC__ASSERT(decoder->private_->side_subframe_in_use != /* logical XOR */ (decoder->private_->frame.header.bits_per_sample < 32));
3503
2.35M
    for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
3504
2.34M
      if(!decoder->private_->side_subframe_in_use){
3505
119k
        FLAC__int32 mid, side;
3506
119k
        mid = decoder->private_->output[0][i];
3507
119k
        side = decoder->private_->output[1][i];
3508
119k
        mid = ((uint32_t) mid) << 1;
3509
119k
        mid |= (side & 1); /* i.e. if 'side' is odd... */
3510
119k
        decoder->private_->output[0][i] = (mid + side) >> 1;
3511
119k
        decoder->private_->output[1][i] = (mid - side) >> 1;
3512
119k
      }
3513
2.22M
      else { /* bps == 32 */
3514
2.22M
        FLAC__int64 mid;
3515
2.22M
        mid = ((uint64_t)decoder->private_->output[0][i]) << 1;
3516
2.22M
        mid |= (decoder->private_->side_subframe[i] & 1); /* i.e. if 'side' is odd... */
3517
2.22M
        decoder->private_->output[0][i] = (mid + decoder->private_->side_subframe[i]) >> 1;
3518
2.22M
        decoder->private_->output[1][i] = (mid - decoder->private_->side_subframe[i]) >> 1;
3519
2.22M
      }
3520
2.34M
    }
3521
12.3k
    break;
3522
0
  default:
3523
0
    FLAC__ASSERT(0);
3524
0
    break;
3525
208k
  }
3526
208k
}
3527
3528
#if FLAC__HAS_OGG
3529
FLAC__StreamDecoderReadStatus read_callback_ogg_aspect_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes)
3530
0
{
3531
0
  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)) {
3532
0
    case FLAC__OGG_DECODER_ASPECT_READ_STATUS_OK:
3533
0
      return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
3534
    /* we don't really have a way to handle lost sync via read
3535
     * callback so we'll let it pass and let the underlying
3536
     * FLAC decoder catch the error
3537
     */
3538
0
    case FLAC__OGG_DECODER_ASPECT_READ_STATUS_LOST_SYNC:
3539
0
      return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
3540
0
    case FLAC__OGG_DECODER_ASPECT_READ_STATUS_END_OF_STREAM:
3541
0
      return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
3542
0
    case FLAC__OGG_DECODER_ASPECT_READ_STATUS_END_OF_LINK:
3543
0
      return FLAC__STREAM_DECODER_READ_STATUS_END_OF_LINK;
3544
0
    case FLAC__OGG_DECODER_ASPECT_READ_STATUS_NOT_FLAC:
3545
0
    case FLAC__OGG_DECODER_ASPECT_READ_STATUS_UNSUPPORTED_MAPPING_VERSION:
3546
0
    case FLAC__OGG_DECODER_ASPECT_READ_STATUS_ABORT:
3547
0
    case FLAC__OGG_DECODER_ASPECT_READ_STATUS_ERROR:
3548
0
      return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
3549
0
    case FLAC__OGG_DECODER_ASPECT_READ_STATUS_MEMORY_ALLOCATION_ERROR:
3550
0
      decoder->private_->ogg_decoder_aspect_allocation_failure = true;
3551
0
      return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
3552
0
    default:
3553
0
      FLAC__ASSERT(0);
3554
      /* double protection */
3555
0
      return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
3556
0
  }
3557
0
}
3558
3559
FLAC__OggDecoderAspectReadStatus read_callback_proxy_(const void *void_decoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
3560
0
{
3561
0
  FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder*)void_decoder;
3562
3563
0
  switch(decoder->private_->read_callback(decoder, buffer, bytes, client_data)) {
3564
0
    case FLAC__STREAM_DECODER_READ_STATUS_CONTINUE:
3565
0
      return FLAC__OGG_DECODER_ASPECT_READ_STATUS_OK;
3566
0
    case FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM:
3567
0
      return FLAC__OGG_DECODER_ASPECT_READ_STATUS_END_OF_STREAM;
3568
0
    case FLAC__STREAM_DECODER_READ_STATUS_ABORT:
3569
0
      return FLAC__OGG_DECODER_ASPECT_READ_STATUS_ABORT;
3570
0
    default:
3571
      /* double protection: */
3572
0
      FLAC__ASSERT(0);
3573
0
      return FLAC__OGG_DECODER_ASPECT_READ_STATUS_ABORT;
3574
0
  }
3575
0
}
3576
#endif
3577
3578
FLAC__StreamDecoderWriteStatus write_audio_frame_to_client_(FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[])
3579
208k
{
3580
208k
  decoder->private_->last_frame = *frame; /* save the frame */
3581
208k
  decoder->private_->last_frame_is_set = true;
3582
208k
  if(decoder->private_->is_seeking && !decoder->private_->is_indexing) {
3583
0
    FLAC__uint64 this_frame_sample = frame->header.number.sample_number;
3584
0
    FLAC__uint64 next_frame_sample = this_frame_sample + (FLAC__uint64)frame->header.blocksize;
3585
0
    FLAC__uint64 target_sample = decoder->private_->target_sample;
3586
3587
0
    FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
3588
3589
0
    decoder->private_->got_a_frame = true;
3590
3591
0
    if(this_frame_sample <= target_sample && target_sample < next_frame_sample) { /* we hit our target frame */
3592
0
      uint32_t delta = (uint32_t)(target_sample - this_frame_sample);
3593
      /* kick out of seek mode */
3594
0
      decoder->private_->is_seeking = false;
3595
      /* shift out the samples before target_sample */
3596
0
      if(delta > 0) {
3597
0
        uint32_t channel;
3598
0
        const FLAC__int32 *newbuffer[FLAC__MAX_CHANNELS];
3599
0
        for(channel = 0; channel < frame->header.channels; channel++) {
3600
0
          newbuffer[channel] = buffer[channel] + delta;
3601
0
          decoder->private_->last_frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_VERBATIM;
3602
0
          decoder->private_->last_frame.subframes[channel].data.verbatim.data_type = FLAC__VERBATIM_SUBFRAME_DATA_TYPE_INT32;
3603
0
          decoder->private_->last_frame.subframes[channel].data.verbatim.data.int32 = newbuffer[channel];
3604
0
        }
3605
0
        decoder->private_->last_frame.header.blocksize -= delta;
3606
0
        decoder->private_->last_frame.header.number.sample_number += (FLAC__uint64)delta;
3607
        /* write the relevant samples */
3608
0
        return decoder->private_->write_callback(decoder, &decoder->private_->last_frame, newbuffer, decoder->private_->client_data);
3609
0
      }
3610
0
      else {
3611
        /* write the relevant samples */
3612
0
        return decoder->private_->write_callback(decoder, frame, buffer, decoder->private_->client_data);
3613
0
      }
3614
0
    }
3615
0
    else {
3616
0
      return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
3617
0
    }
3618
0
  }
3619
208k
  else if(!decoder->private_->is_indexing) {
3620
    /*
3621
     * If we never got STREAMINFO, turn off MD5 checking to save
3622
     * cycles since we don't have a sum to compare to anyway
3623
     */
3624
208k
    if(!decoder->private_->has_stream_info)
3625
0
      decoder->private_->do_md5_checking = false;
3626
208k
    if(decoder->private_->do_md5_checking) {
3627
0
      if(!FLAC__MD5Accumulate(&decoder->private_->md5context, buffer, frame->header.channels, frame->header.blocksize, (frame->header.bits_per_sample+7) / 8))
3628
0
        return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
3629
0
    }
3630
208k
    return decoder->private_->write_callback(decoder, frame, buffer, decoder->private_->client_data);
3631
208k
  }
3632
0
  else { /* decoder->private_->is_indexing == true */
3633
0
    return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
3634
0
  }
3635
208k
}
3636
3637
void send_error_to_client_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status)
3638
0
{
3639
0
  if(!decoder->private_->is_seeking) {
3640
0
    decoder->private_->error_has_been_sent = true;
3641
0
    decoder->private_->error_callback(decoder, status, decoder->private_->client_data);
3642
0
  }
3643
0
  else if(status == FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM)
3644
0
    decoder->private_->unparseable_frame_count++;
3645
0
}
3646
3647
FLAC__bool seek_to_absolute_sample_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample)
3648
0
{
3649
0
  FLAC__uint64 first_frame_offset = decoder->private_->first_frame_offset, lower_bound, upper_bound, lower_bound_sample, upper_bound_sample, this_frame_sample;
3650
0
  FLAC__int64 pos = -1;
3651
0
  int i;
3652
0
  uint32_t approx_bytes_per_frame;
3653
0
  FLAC__bool first_seek = true, seek_from_lower_bound = false;
3654
0
  const FLAC__uint64 total_samples = FLAC__stream_decoder_get_total_samples(decoder);
3655
0
  const uint32_t min_blocksize = decoder->private_->stream_info.data.stream_info.min_blocksize;
3656
0
  const uint32_t max_blocksize = decoder->private_->stream_info.data.stream_info.max_blocksize;
3657
0
  const uint32_t max_framesize = decoder->private_->stream_info.data.stream_info.max_framesize;
3658
0
  const uint32_t min_framesize = decoder->private_->stream_info.data.stream_info.min_framesize;
3659
  /* take these from the current frame in case they've changed mid-stream */
3660
0
  uint32_t channels = FLAC__stream_decoder_get_channels(decoder);
3661
0
  uint32_t bps = FLAC__stream_decoder_get_bits_per_sample(decoder);
3662
0
  const FLAC__StreamMetadata_SeekTable *seek_table = decoder->private_->has_seek_table? &decoder->private_->seek_table.data.seek_table : 0;
3663
3664
  /* use values from stream info if we didn't decode a frame */
3665
0
  if(channels == 0)
3666
0
    channels = decoder->private_->stream_info.data.stream_info.channels;
3667
0
  if(bps == 0)
3668
0
    bps = decoder->private_->stream_info.data.stream_info.bits_per_sample;
3669
3670
  /* we are just guessing here */
3671
0
  if(max_framesize > 0)
3672
0
    approx_bytes_per_frame = (max_framesize + min_framesize) / 2 + 1;
3673
  /*
3674
   * Check if it's a known fixed-blocksize stream.  Note that though
3675
   * the spec doesn't allow zeroes in the STREAMINFO block, we may
3676
   * never get a STREAMINFO block when decoding so the value of
3677
   * min_blocksize might be zero.
3678
   */
3679
0
  else if(min_blocksize == max_blocksize && min_blocksize > 0) {
3680
    /* note there are no () around 'bps/8' to keep precision up since it's an integer calculation */
3681
0
    approx_bytes_per_frame = min_blocksize * channels * bps/8 + 64;
3682
0
  }
3683
0
  else
3684
0
    approx_bytes_per_frame = 4096 * channels * bps/8 + 64;
3685
3686
  /*
3687
   * First, we set an upper and lower bound on where in the
3688
   * stream we will search.  For now we take the current position
3689
   * as one bound and, depending on where the target position lies,
3690
   * the beginning of the first frame or the end of the stream as
3691
   * the other bound.
3692
   */
3693
0
  lower_bound = first_frame_offset;
3694
0
  lower_bound_sample = 0;
3695
0
  upper_bound = stream_length;
3696
0
  upper_bound_sample = total_samples > 0 ? total_samples : target_sample /*estimate it*/;
3697
3698
0
  if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC &&
3699
0
     decoder->private_->samples_decoded != 0) {
3700
0
    if(target_sample < decoder->private_->samples_decoded) {
3701
0
      if(FLAC__stream_decoder_get_decode_position(decoder, &upper_bound))
3702
0
        upper_bound_sample = decoder->private_->samples_decoded;
3703
0
    } else {
3704
0
      if(FLAC__stream_decoder_get_decode_position(decoder, &lower_bound))
3705
0
        lower_bound_sample = decoder->private_->samples_decoded;
3706
0
    }
3707
0
  }
3708
3709
  /*
3710
   * Now we refine the bounds if we have a seektable with
3711
   * suitable points.  Note that according to the spec they
3712
   * must be ordered by ascending sample number.
3713
   *
3714
   * Note: to protect against invalid seek tables we will ignore points
3715
   * that have frame_samples==0 or sample_number>=total_samples. Also,
3716
   * because math is limited to 64-bit ints, seekpoints with an offset
3717
   * larger than 2^63 (8 exbibyte) are rejected.
3718
   */
3719
0
  if(seek_table) {
3720
0
    FLAC__uint64 new_lower_bound = lower_bound;
3721
0
    FLAC__uint64 new_upper_bound = upper_bound;
3722
0
    FLAC__uint64 new_lower_bound_sample = lower_bound_sample;
3723
0
    FLAC__uint64 new_upper_bound_sample = upper_bound_sample;
3724
3725
    /* find the closest seek point <= target_sample, if it exists */
3726
0
    for(i = (int)seek_table->num_points - 1; i >= 0; i--) {
3727
0
      if(
3728
0
        seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER &&
3729
0
        seek_table->points[i].frame_samples > 0 && /* defense against bad seekpoints */
3730
0
        (total_samples <= 0 || seek_table->points[i].sample_number < total_samples) && /* defense against bad seekpoints */
3731
0
        seek_table->points[i].sample_number <= target_sample
3732
0
      )
3733
0
        break;
3734
0
    }
3735
0
    if(i >= 0) { /* i.e. we found a suitable seek point... */
3736
0
      new_lower_bound = first_frame_offset + seek_table->points[i].stream_offset;
3737
0
      new_lower_bound_sample = seek_table->points[i].sample_number;
3738
0
    }
3739
3740
    /* find the closest seek point > target_sample, if it exists */
3741
0
    for(i = 0; i < (int)seek_table->num_points; i++) {
3742
0
      if(
3743
0
        seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER &&
3744
0
        seek_table->points[i].frame_samples > 0 && /* defense against bad seekpoints */
3745
0
        (total_samples <= 0 || seek_table->points[i].sample_number < total_samples) && /* defense against bad seekpoints */
3746
0
        seek_table->points[i].sample_number > target_sample
3747
0
      )
3748
0
        break;
3749
0
    }
3750
0
    if(i < (int)seek_table->num_points) { /* i.e. we found a suitable seek point... */
3751
0
      new_upper_bound = first_frame_offset + seek_table->points[i].stream_offset;
3752
0
      new_upper_bound_sample = seek_table->points[i].sample_number;
3753
0
    }
3754
    /* final protection against unsorted seek tables; keep original values if bogus */
3755
0
    if(new_upper_bound >= new_lower_bound) {
3756
0
      lower_bound = new_lower_bound;
3757
0
      upper_bound = new_upper_bound;
3758
0
      lower_bound_sample = new_lower_bound_sample;
3759
0
      upper_bound_sample = new_upper_bound_sample;
3760
0
    }
3761
0
  }
3762
3763
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
3764
  FLAC__ASSERT(upper_bound_sample >= lower_bound_sample);
3765
#endif
3766
  /* there are 3 insidious ways that the following equality occurs, which
3767
   * we need to fix:
3768
   *  1) total_samples is 0 (unknown) and target_sample is 0
3769
   *  2) total_samples is 0 (unknown) and target_sample happens to be
3770
   *     exactly equal to the last seek point in the seek table; this
3771
   *     means there is no seek point above it, and upper_bound_samples
3772
   *     remains equal to the estimate (of target_samples) we made above
3773
   * in either case it does not hurt to move upper_bound_sample up by 1
3774
   *  3) the file is corrupt
3775
   */
3776
0
  if(upper_bound_sample == lower_bound_sample) {
3777
0
    if(total_samples == 0)
3778
0
      upper_bound_sample++;
3779
0
    else {
3780
0
      decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3781
0
      return false;
3782
0
    }
3783
0
  }
3784
3785
0
  decoder->private_->target_sample = target_sample;
3786
0
  while(1) {
3787
    /* check whether decoder is still valid so bad state isn't overwritten
3788
     * with seek error */
3789
0
    if(decoder->protected_->state == FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR ||
3790
0
       decoder->protected_->state == FLAC__STREAM_DECODER_ABORTED)
3791
0
      return false;
3792
    /* check if the bounds are still ok */
3793
0
    if (lower_bound_sample >= upper_bound_sample ||
3794
0
        lower_bound > upper_bound ||
3795
0
        upper_bound >= INT64_MAX ||
3796
0
        target_sample > upper_bound_sample) {
3797
0
      decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3798
0
      return false;
3799
0
    }
3800
0
    if(seek_from_lower_bound) {
3801
0
      pos = lower_bound;
3802
0
    }
3803
0
    else {
3804
0
#ifndef FLAC__INTEGER_ONLY_LIBRARY
3805
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;
3806
#else
3807
      /* a little less accurate: */
3808
      if(upper_bound - lower_bound < 0xffffffff)
3809
        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;
3810
      else { /* @@@ WATCHOUT, ~2TB limit */
3811
              FLAC__uint64 ratio = (1<<16) / (upper_bound_sample - lower_bound_sample);
3812
        pos = (FLAC__int64)lower_bound + (FLAC__int64)((((target_sample - lower_bound_sample)>>8) * ((upper_bound - lower_bound)>>8) * ratio)) - approx_bytes_per_frame;
3813
      }
3814
#endif
3815
0
    }
3816
0
    if(pos >= (FLAC__int64)upper_bound)
3817
0
      pos = (FLAC__int64)upper_bound - 1;
3818
0
    if(pos < (FLAC__int64)lower_bound)
3819
0
      pos = (FLAC__int64)lower_bound;
3820
0
    if(decoder->private_->seek_callback(decoder, (FLAC__uint64)pos, decoder->private_->client_data) != FLAC__STREAM_DECODER_SEEK_STATUS_OK) {
3821
0
      decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3822
0
      return false;
3823
0
    }
3824
0
    if(!FLAC__stream_decoder_flush(decoder)) {
3825
      /* above call sets the state for us */
3826
0
      return false;
3827
0
    }
3828
    /* Now we need to get a frame.  First we need to reset our
3829
     * unparseable_frame_count; if we get too many unparseable
3830
     * frames in a row, the read callback will return
3831
     * FLAC__STREAM_DECODER_READ_STATUS_ABORT, causing
3832
     * FLAC__stream_decoder_process_single() to return false.
3833
     */
3834
0
    decoder->private_->unparseable_frame_count = 0;
3835
0
    if(!FLAC__stream_decoder_process_single(decoder) || decoder->protected_->state == FLAC__STREAM_DECODER_ABORTED || 0 == decoder->private_->samples_decoded) {
3836
      /* No frame could be decoded */
3837
0
      if(decoder->protected_->state != FLAC__STREAM_DECODER_ABORTED && decoder->private_->eof_callback(decoder, decoder->private_->client_data) && !seek_from_lower_bound){
3838
        /* decoder has hit end of stream while processing corrupt
3839
         * frame. To remedy this, try decoding a frame at the lower
3840
         * bound so the seek after that hopefully ends up somewhere
3841
         * else */
3842
0
        seek_from_lower_bound = true;
3843
0
        continue;
3844
0
      }
3845
0
      else {
3846
0
        if(decoder->protected_->state != FLAC__STREAM_DECODER_ABORTED && decoder->protected_->state != FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR)
3847
0
          decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3848
0
        return false;
3849
0
      }
3850
0
    }
3851
0
    seek_from_lower_bound = false;
3852
3853
    /* our write callback will change the state when it gets to the target frame */
3854
    /* 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 */
3855
0
    if(!decoder->private_->is_seeking)
3856
0
      break;
3857
3858
0
    FLAC__ASSERT(decoder->private_->last_frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
3859
0
    this_frame_sample = decoder->private_->last_frame.header.number.sample_number;
3860
3861
0
    if(this_frame_sample + decoder->private_->last_frame.header.blocksize >= upper_bound_sample && !first_seek) {
3862
0
      if (pos == (FLAC__int64)lower_bound) {
3863
        /* can't move back any more than the first frame, something is fatally wrong */
3864
0
        decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3865
0
        return false;
3866
0
      }
3867
      /* our last move backwards wasn't big enough, try again */
3868
0
      approx_bytes_per_frame = approx_bytes_per_frame? approx_bytes_per_frame * 2 : 16;
3869
0
      continue;
3870
0
    }
3871
    /* allow one seek over upper bound, so we can get a correct upper_bound_sample for streams with unknown total_samples */
3872
0
    first_seek = false;
3873
3874
    /* make sure we are not seeking in corrupted stream */
3875
0
    if (this_frame_sample < lower_bound_sample) {
3876
0
      decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3877
0
      return false;
3878
0
    }
3879
3880
    /* we need to narrow the search */
3881
0
    if(target_sample < this_frame_sample) {
3882
0
      upper_bound_sample = this_frame_sample + decoder->private_->last_frame.header.blocksize;
3883
/*@@@@@@ what will decode position be if at end of stream? */
3884
0
      if(!FLAC__stream_decoder_get_decode_position(decoder, &upper_bound)) {
3885
0
        decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3886
0
        return false;
3887
0
      }
3888
0
      approx_bytes_per_frame = (uint32_t)(2 * (upper_bound - pos) / 3 + 16);
3889
0
    }
3890
0
    else { /* target_sample >= this_frame_sample + this frame's blocksize */
3891
0
      lower_bound_sample = this_frame_sample + decoder->private_->last_frame.header.blocksize;
3892
0
      if(!FLAC__stream_decoder_get_decode_position(decoder, &lower_bound)) {
3893
0
        decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3894
0
        return false;
3895
0
      }
3896
0
      approx_bytes_per_frame = (uint32_t)(2 * (lower_bound - pos) / 3 + 16);
3897
0
    }
3898
0
  }
3899
3900
0
  return true;
3901
0
}
3902
3903
#if FLAC__HAS_OGG
3904
FLAC__bool seek_to_absolute_sample_ogg_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample)
3905
0
{
3906
0
  FLAC__uint64 left_pos = 0, right_pos = stream_length;
3907
0
  FLAC__uint64 left_sample = 0, right_sample = 0;
3908
0
  FLAC__uint64 this_frame_sample = (FLAC__uint64)0 - 1;
3909
0
  FLAC__uint64 pos = 0; /* only initialized to avoid compiler warning */
3910
0
  FLAC__bool did_a_seek;
3911
0
  FLAC__OggDecoderAspect_TargetLink *target_link;
3912
0
  uint32_t current_linknumber = decoder->protected_->ogg_decoder_aspect.current_linknumber;
3913
0
  uint32_t iteration = 0;
3914
3915
  /* In the first iterations, we will calculate the target byte position
3916
   * by the distance from the target sample to left_sample and
3917
   * right_sample (let's call it "proportional search").  After that, we
3918
   * will switch to binary search.
3919
   */
3920
0
  uint32_t BINARY_SEARCH_AFTER_ITERATION = 2;
3921
3922
  /* We will switch to a linear search once our current sample is less
3923
   * than this number of samples ahead of the target sample
3924
   */
3925
0
  static const FLAC__uint64 LINEAR_SEARCH_WITHIN_SAMPLES = FLAC__MAX_BLOCK_SIZE * 2;
3926
3927
0
  if(FLAC__ogg_decoder_aspect_get_decode_chained_stream(&decoder->protected_->ogg_decoder_aspect)) {
3928
    /* Check whether target sample is contained by indexed links.
3929
     * If it is not keep moving forward until found */
3930
0
    decoder->private_->is_indexing = true;
3931
0
    while(NULL == (target_link = FLAC__ogg_decoder_aspect_get_target_link(&decoder->protected_->ogg_decoder_aspect, target_sample))) {
3932
0
      FLAC__OggDecoderAspectReadStatus status;
3933
0
      if(decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM ||
3934
0
         decoder->protected_->state == FLAC__STREAM_DECODER_OGG_ERROR ||
3935
0
         decoder->protected_->state == FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR ||
3936
0
         decoder->protected_->state == FLAC__STREAM_DECODER_ABORTED) {
3937
        /* Target sample is not contained in stream or other error */
3938
0
        decoder->private_->is_indexing = false;
3939
0
        decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3940
0
        return false;
3941
0
      }
3942
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);
3943
0
      if(status == FLAC__OGG_DECODER_ASPECT_READ_STATUS_END_OF_STREAM)
3944
0
        decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
3945
0
      else if(status != FLAC__OGG_DECODER_ASPECT_READ_STATUS_OK) {
3946
0
        decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3947
0
        return false;
3948
0
      }
3949
0
    }
3950
0
    decoder->private_->is_indexing = false;
3951
    /* Target link found, send metadata if necessary */
3952
0
    FLAC__ogg_decoder_aspect_set_seek_parameters(&decoder->protected_->ogg_decoder_aspect, target_link);
3953
0
    if(target_link->linknumber != current_linknumber) {
3954
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) {
3955
0
        decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3956
0
        return false;
3957
0
      }
3958
0
      if(!FLAC__stream_decoder_flush(decoder)) {
3959
        /* above call sets the state for us */
3960
0
        return false;
3961
0
      }
3962
0
      decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
3963
0
      decoder->private_->is_seeking = false;
3964
0
      if(!FLAC__stream_decoder_process_until_end_of_metadata(decoder)) {
3965
0
        decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3966
0
        return false;
3967
0
      }
3968
0
      decoder->private_->is_seeking = true;
3969
0
    }
3970
    /* set boundaries */
3971
0
    left_pos = target_link->start_byte;
3972
0
    left_sample = 0;
3973
0
    right_pos = target_link->end_byte;
3974
0
    right_sample = target_link->samples_this_link;
3975
0
    target_sample -= target_link->samples_in_preceding_links;
3976
0
  }
3977
0
  else {
3978
0
    right_sample = FLAC__stream_decoder_get_total_samples(decoder);
3979
3980
    /* If the total number of samples is unknown, use a large value, and
3981
     * force binary search immediately.
3982
     */
3983
0
    if(right_sample == 0) {
3984
0
      right_sample = (FLAC__uint64)(-1);
3985
0
      BINARY_SEARCH_AFTER_ITERATION = 0;
3986
0
    }
3987
0
  }
3988
3989
0
  decoder->private_->target_sample = target_sample;
3990
0
  for( ; ; iteration++) {
3991
    /* Do sanity checks on bounds */
3992
0
    if(right_pos <= left_pos || right_pos - left_pos < 9) {
3993
      /* FLAC frame is at least 9 byte in size */
3994
0
      decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
3995
0
      return false;
3996
0
    }
3997
0
    if (iteration == 0 || this_frame_sample > target_sample || target_sample - this_frame_sample > LINEAR_SEARCH_WITHIN_SAMPLES) {
3998
0
      if (iteration >= BINARY_SEARCH_AFTER_ITERATION) {
3999
0
        pos = (right_pos + left_pos) / 2;
4000
0
      }
4001
0
      else {
4002
0
#ifndef FLAC__INTEGER_ONLY_LIBRARY
4003
0
        pos = (FLAC__uint64)((double)(target_sample - left_sample) / (double)(right_sample - left_sample) * (double)(right_pos - left_pos)) + left_pos;
4004
#else
4005
        /* a little less accurate: */
4006
        if ((target_sample-left_sample <= 0xffffffff) && (right_pos-left_pos <= 0xffffffff))
4007
          pos = (FLAC__int64)(((target_sample-left_sample) * (right_pos-left_pos)) / (right_sample-left_sample)) + left_pos;
4008
        else /* @@@ WATCHOUT, ~2TB limit */
4009
          pos = (FLAC__int64)((((target_sample-left_sample)>>8) * ((right_pos-left_pos)>>8)) / ((right_sample-left_sample)>>16)) + left_pos;
4010
#endif
4011
        /* @@@ TODO: might want to limit pos to some distance
4012
         * before EOF, to make sure we land before the last frame,
4013
         * thereby getting a this_frame_sample and so having a better
4014
         * estimate.
4015
         */
4016
0
      }
4017
4018
      /* physical seek */
4019
0
      if(decoder->private_->seek_callback((FLAC__StreamDecoder*)decoder, (FLAC__uint64)pos, decoder->private_->client_data) != FLAC__STREAM_DECODER_SEEK_STATUS_OK) {
4020
0
        decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
4021
0
        return false;
4022
0
      }
4023
0
      if(!FLAC__stream_decoder_flush(decoder)) {
4024
        /* above call sets the state for us */
4025
0
        return false;
4026
0
      }
4027
0
      did_a_seek = true;
4028
0
    }
4029
0
    else
4030
0
      did_a_seek = false;
4031
4032
0
    decoder->private_->got_a_frame = false;
4033
0
    if(!FLAC__stream_decoder_process_single(decoder) ||
4034
0
       decoder->protected_->state == FLAC__STREAM_DECODER_ABORTED) {
4035
0
      if(decoder->protected_->state != FLAC__STREAM_DECODER_ABORTED && decoder->protected_->state != FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR)
4036
0
        decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
4037
0
      return false;
4038
0
    }
4039
0
    if(!decoder->private_->got_a_frame) {
4040
0
      if(did_a_seek) {
4041
        /* this can happen if we seek to a point after the last frame; we drop
4042
         * to binary search right away in this case to avoid any wasted
4043
         * iterations of proportional search.
4044
         */
4045
0
        right_pos = pos;
4046
0
        BINARY_SEARCH_AFTER_ITERATION = 0;
4047
0
      }
4048
0
      else {
4049
        /* this can probably only happen if total_samples is unknown and the
4050
         * target_sample is past the end of the stream
4051
         */
4052
0
        decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
4053
0
        return false;
4054
0
      }
4055
0
    }
4056
    /* our write callback will change the state when it gets to the target frame */
4057
0
    else if(!decoder->private_->is_seeking) {
4058
0
      break;
4059
0
    }
4060
0
    else {
4061
0
      this_frame_sample = decoder->private_->last_frame.header.number.sample_number;
4062
0
      FLAC__ASSERT(decoder->private_->last_frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
4063
4064
0
      if (did_a_seek) {
4065
0
        if (this_frame_sample <= target_sample) {
4066
          /* The 'equal' case should not happen, since
4067
           * FLAC__stream_decoder_process_single()
4068
           * should recognize that it has hit the
4069
           * target sample and we would exit through
4070
           * the 'break' above.
4071
           */
4072
0
          FLAC__ASSERT(this_frame_sample != target_sample);
4073
4074
0
          left_sample = this_frame_sample;
4075
          /* sanity check to avoid infinite loop */
4076
0
          if (left_pos == pos) {
4077
0
            decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
4078
0
            return false;
4079
0
          }
4080
0
          left_pos = pos;
4081
0
        }
4082
0
        else {
4083
0
          right_sample = this_frame_sample;
4084
          /* sanity check to avoid infinite loop */
4085
0
          if (right_pos == pos) {
4086
0
            decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
4087
0
            return false;
4088
0
          }
4089
0
          right_pos = pos;
4090
0
        }
4091
0
      }
4092
0
    }
4093
0
  }
4094
  /* Tell ogg decoder aspect that seeking is done */
4095
0
  FLAC__ogg_decoder_aspect_set_seek_parameters(&decoder->protected_->ogg_decoder_aspect, 0);
4096
0
  return true;
4097
0
}
4098
#endif
4099
4100
FLAC__StreamDecoderReadStatus file_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
4101
0
{
4102
0
  (void)client_data;
4103
4104
0
  if(*bytes > 0) {
4105
0
    *bytes = fread(buffer, sizeof(FLAC__byte), *bytes, decoder->private_->file);
4106
0
    if(ferror(decoder->private_->file))
4107
0
      return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
4108
0
    else if(*bytes == 0)
4109
0
      return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
4110
0
    else
4111
0
      return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
4112
0
  }
4113
0
  else
4114
0
    return FLAC__STREAM_DECODER_READ_STATUS_ABORT; /* abort to avoid a deadlock */
4115
0
}
4116
4117
FLAC__StreamDecoderSeekStatus file_seek_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data)
4118
0
{
4119
0
  (void)client_data;
4120
4121
0
  if(decoder->private_->file == stdin)
4122
0
    return FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED;
4123
0
  else if(fseeko(decoder->private_->file, (FLAC__off_t)absolute_byte_offset, SEEK_SET) < 0)
4124
0
    return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR;
4125
0
  else
4126
0
    return FLAC__STREAM_DECODER_SEEK_STATUS_OK;
4127
0
}
4128
4129
FLAC__StreamDecoderTellStatus file_tell_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
4130
0
{
4131
0
  FLAC__off_t pos;
4132
0
  (void)client_data;
4133
4134
0
  if(decoder->private_->file == stdin)
4135
0
    return FLAC__STREAM_DECODER_TELL_STATUS_UNSUPPORTED;
4136
0
  else if((pos = ftello(decoder->private_->file)) < 0)
4137
0
    return FLAC__STREAM_DECODER_TELL_STATUS_ERROR;
4138
0
  else {
4139
0
    *absolute_byte_offset = (FLAC__uint64)pos;
4140
0
    return FLAC__STREAM_DECODER_TELL_STATUS_OK;
4141
0
  }
4142
0
}
4143
4144
FLAC__StreamDecoderLengthStatus file_length_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data)
4145
0
{
4146
0
  struct flac_stat_s filestats;
4147
0
  (void)client_data;
4148
4149
0
  if(decoder->private_->file == stdin)
4150
0
    return FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED;
4151
4152
0
#ifndef FLAC__USE_FILELENGTHI64
4153
0
  if(flac_fstat(fileno(decoder->private_->file), &filestats) != 0)
4154
#else
4155
  filestats.st_size = _filelengthi64(fileno(decoder->private_->file));
4156
  if(filestats.st_size < 0)
4157
#endif
4158
0
    return FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR;
4159
0
  else {
4160
0
    *stream_length = (FLAC__uint64)filestats.st_size;
4161
0
    return FLAC__STREAM_DECODER_LENGTH_STATUS_OK;
4162
0
  }
4163
0
}
4164
4165
FLAC__bool file_eof_callback_(const FLAC__StreamDecoder *decoder, void *client_data)
4166
0
{
4167
0
  (void)client_data;
4168
4169
0
  return feof(decoder->private_->file)? true : false;
4170
0
}