fix bug in code that skips id3v2 tags at the front of a file; false positives could...
[platform/upstream/flac.git] / src / libFLAC / stream_decoder.c
index 79cd277..f1f5fc8 100644 (file)
 /* libFLAC - Free Lossless Audio Codec library
- * Copyright (C) 2000,2001  Josh Coalson
+ * Copyright (C) 2000,2001,2002,2003,2004,2005,2006  Josh Coalson
  *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Library General Public
- * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
  *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Library General Public License for more details.
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
  *
- * You should have received a copy of the GNU Library General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA  02111-1307, USA.
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * - Neither the name of the Xiph.org Foundation nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-#include <assert.h>
+#if HAVE_CONFIG_H
+#  include <config.h>
+#endif
+
+#if defined _MSC_VER || defined __MINGW32__
+#include <io.h> /* for _setmode() */
+#include <fcntl.h> /* for _O_BINARY */
+#endif
+#if defined __CYGWIN__ || defined __EMX__
+#include <io.h> /* for setmode(), O_BINARY */
+#include <fcntl.h> /* for _O_BINARY */
+#endif
 #include <stdio.h>
 #include <stdlib.h> /* for malloc() */
 #include <string.h> /* for memset/memcpy() */
-#include "FLAC/stream_decoder.h"
+#include <sys/stat.h> /* for stat() */
+#include <sys/types.h> /* for off_t */
+#if defined _MSC_VER || defined __MINGW32__
+#if _MSC_VER <= 1200 /* @@@ [2G limit] */
+#define fseeko fseek
+#define ftello ftell
+#endif
+#endif
+#include "FLAC/assert.h"
+#include "protected/stream_decoder.h"
 #include "private/bitbuffer.h"
+#include "private/bitmath.h"
+#include "private/cpu.h"
 #include "private/crc.h"
 #include "private/fixed.h"
+#include "private/format.h"
 #include "private/lpc.h"
+#include "private/md5.h"
+#include "private/memory.h"
+
+#ifdef max
+#undef max
+#endif
+#define max(a,b) ((a)>(b)?(a):(b))
+
+/* adjust for compilers that can't understand using LLU suffix for uint64_t literals */
+#ifdef _MSC_VER
+#define FLAC__U64L(x) x
+#else
+#define FLAC__U64L(x) x##LLU
+#endif
+
+/***********************************************************************
+ *
+ * Private static data
+ *
+ ***********************************************************************/
+
+static FLAC__byte ID3V2_TAG_[3] = { 'I', 'D', '3' };
+
+/***********************************************************************
+ *
+ * Private class method prototypes
+ *
+ ***********************************************************************/
+
+static void set_defaults_(FLAC__StreamDecoder *decoder);
+static FILE *get_binary_stdin_();
+static FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels);
+static FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id);
+static FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder);
+static FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder);
+static FLAC__bool read_metadata_streaminfo_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length);
+static FLAC__bool read_metadata_seektable_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length);
+static FLAC__bool read_metadata_vorbiscomment_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_VorbisComment *obj);
+static FLAC__bool read_metadata_cuesheet_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_CueSheet *obj);
+static FLAC__bool read_metadata_picture_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_Picture *obj);
+static FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder);
+static FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder);
+static FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame, FLAC__bool do_full_decode);
+static FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder);
+static FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode);
+static FLAC__bool read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode);
+static FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode);
+static FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode);
+static FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode);
+static FLAC__bool read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order, FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, FLAC__int32 *residual);
+static FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder);
+static FLAC__bool read_callback_(FLAC__byte buffer[], unsigned *bytes, void *client_data);
+static FLAC__StreamDecoderWriteStatus write_audio_frame_to_client_(FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[]);
+static void send_error_to_client_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status);
+static FLAC__bool seek_to_absolute_sample_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample);
+static FLAC__StreamDecoderReadStatus file_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data);
+static FLAC__StreamDecoderSeekStatus file_seek_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data);
+static FLAC__StreamDecoderTellStatus file_tell_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data);
+static FLAC__StreamDecoderLengthStatus file_length_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data);
+static FLAC__bool file_eof_callback_(const FLAC__StreamDecoder *decoder, void *client_data);
+
+/***********************************************************************
+ *
+ * Private class data
+ *
+ ***********************************************************************/
 
 typedef struct FLAC__StreamDecoderPrivate {
-       FLAC__StreamDecoderReadStatus (*read_callback)(const FLAC__StreamDecoder *decoder, byte buffer[], unsigned *bytes, void *client_data);
-       FLAC__StreamDecoderWriteStatus (*write_callback)(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const int32 *buffer[], void *client_data);
-       void (*metadata_callback)(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data);
-       void (*error_callback)(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
+       FLAC__StreamDecoderReadCallback read_callback;
+       FLAC__StreamDecoderSeekCallback seek_callback;
+       FLAC__StreamDecoderTellCallback tell_callback;
+       FLAC__StreamDecoderLengthCallback length_callback;
+       FLAC__StreamDecoderEofCallback eof_callback;
+       FLAC__StreamDecoderWriteCallback write_callback;
+       FLAC__StreamDecoderMetadataCallback metadata_callback;
+       FLAC__StreamDecoderErrorCallback error_callback;
+       /* generic 32-bit datapath: */
+       void (*local_lpc_restore_signal)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]);
+       /* generic 64-bit datapath: */
+       void (*local_lpc_restore_signal_64bit)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]);
+       /* for use when the signal is <= 16 bits-per-sample, or <= 15 bits-per-sample on a side channel (which requires 1 extra bit): */
+       void (*local_lpc_restore_signal_16bit)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]);
+       /* for use when the signal is <= 16 bits-per-sample, or <= 15 bits-per-sample on a side channel (which requires 1 extra bit), AND order <= 8: */
+       void (*local_lpc_restore_signal_16bit_order8)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]);
        void *client_data;
-       FLAC__BitBuffer input;
-       int32 *output[FLAC__MAX_CHANNELS];
-       int32 *residual[FLAC__MAX_CHANNELS];
+       FILE *file; /* only used if FLAC__stream_decoder_init_file()/FLAC__stream_decoder_init_file() called, else NULL */
+       FLAC__BitBuffer *input;
+       FLAC__int32 *output[FLAC__MAX_CHANNELS];
+       FLAC__int32 *residual[FLAC__MAX_CHANNELS]; /* WATCHOUT: these are the aligned pointers; the real pointers that should be free()'d are residual_unaligned[] below */
+       FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents[FLAC__MAX_CHANNELS];
        unsigned output_capacity, output_channels;
-       uint32 last_frame_number;
-       uint64 samples_decoded;
-       bool has_stream_info, has_seek_table;
-       FLAC__StreamMetaData stream_info;
-       FLAC__StreamMetaData seek_table;
+       FLAC__uint32 last_frame_number;
+       FLAC__uint32 last_block_size;
+       FLAC__uint64 samples_decoded;
+       FLAC__bool has_stream_info, has_seek_table;
+       FLAC__StreamMetadata stream_info;
+       FLAC__StreamMetadata seek_table;
+       FLAC__bool metadata_filter[128]; /* MAGIC number 128 == total number of metadata block types == 1 << 7 */
+       FLAC__byte *metadata_filter_ids;
+       unsigned metadata_filter_ids_count, metadata_filter_ids_capacity; /* units for both are IDs, not bytes */
        FLAC__Frame frame;
-       byte header_warmup[2]; /* contains the sync code and reserved bits */
-       byte lookahead; /* temp storage when we need to look ahead one byte in the stream */
-       bool cached; /* true if there is a byte in lookahead */
+       FLAC__bool cached; /* true if there is a byte in lookahead */
+       FLAC__CPUInfo cpuinfo;
+       FLAC__byte header_warmup[2]; /* contains the sync code and reserved bits */
+       FLAC__byte lookahead; /* temp storage when we need to look ahead one byte in the stream */
+       /* unaligned (original) pointers to allocated data */
+       FLAC__int32 *residual_unaligned[FLAC__MAX_CHANNELS];
+       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 */
+       FLAC__bool internal_reset_hack; /* used only during init() so we can call reset to set up the decoder without rewinding the input */
+       FLAC__bool is_seeking;
+       struct FLAC__MD5Context md5context;
+       FLAC__byte computed_md5sum[16]; /* this is the sum we computed from the decoded data */
+       /* (the rest of these are only used for seeking) */
+       FLAC__Frame last_frame; /* holds the info of the last frame we seeked to */
+       FLAC__uint64 first_frame_offset; /* hint to the seek routine of where in the stream the first audio frame starts */
+       FLAC__uint64 target_sample;
+       unsigned unparseable_frame_count; /* used to tell whether we're decoding a future version of FLAC or just got a bad sync */
 } FLAC__StreamDecoderPrivate;
 
-static byte ID3V2_TAG_[3] = { 'I', 'D', '3' };
-
-static bool stream_decoder_allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels);
-static bool stream_decoder_find_metadata_(FLAC__StreamDecoder *decoder);
-static bool stream_decoder_read_metadata_(FLAC__StreamDecoder *decoder);
-static bool stream_decoder_skip_id3v2_tag_(FLAC__StreamDecoder *decoder);
-static bool stream_decoder_frame_sync_(FLAC__StreamDecoder *decoder);
-static bool stream_decoder_read_frame_(FLAC__StreamDecoder *decoder, bool *got_a_frame);
-static bool stream_decoder_read_frame_header_(FLAC__StreamDecoder *decoder);
-static bool stream_decoder_read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
-static bool stream_decoder_read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
-static bool stream_decoder_read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order);
-static bool stream_decoder_read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order);
-static bool stream_decoder_read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
-static bool stream_decoder_read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order, int32 *residual);
-static bool stream_decoder_read_zero_padding_(FLAC__StreamDecoder *decoder);
-static bool read_callback_(byte buffer[], unsigned *bytes, void *client_data);
-
-const char *FLAC__StreamDecoderStateString[] = {
+/***********************************************************************
+ *
+ * Public static class data
+ *
+ ***********************************************************************/
+
+FLAC_API const char * const FLAC__StreamDecoderStateString[] = {
        "FLAC__STREAM_DECODER_SEARCH_FOR_METADATA",
        "FLAC__STREAM_DECODER_READ_METADATA",
        "FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC",
        "FLAC__STREAM_DECODER_READ_FRAME",
        "FLAC__STREAM_DECODER_END_OF_STREAM",
+       "FLAC__STREAM_DECODER_SEEK_ERROR",
        "FLAC__STREAM_DECODER_ABORTED",
-       "FLAC__STREAM_DECODER_UNPARSEABLE_STREAM",
        "FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR",
        "FLAC__STREAM_DECODER_UNINITIALIZED"
 };
 
-const char *FLAC__StreamDecoderReadStatusString[] = {
-       "FLAC__STREAM_DECODER_READ_CONTINUE",
-       "FLAC__STREAM_DECODER_READ_END_OF_STREAM",
-       "FLAC__STREAM_DECODER_READ_ABORT"
+FLAC_API const char * const FLAC__StreamDecoderInitStatusString[] = {
+       "FLAC__STREAM_DECODER_INIT_STATUS_OK",
+       "FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS",
+       "FLAC__STREAM_DECODER_INIT_STATUS_MEMORY_ALLOCATION_ERROR",
+       "FLAC__STREAM_DECODER_INIT_STATUS_ERROR_OPENING_FILE",
+       "FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED"
+};
+
+FLAC_API const char * const FLAC__StreamDecoderReadStatusString[] = {
+       "FLAC__STREAM_DECODER_READ_STATUS_CONTINUE",
+       "FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM",
+       "FLAC__STREAM_DECODER_READ_STATUS_ABORT"
 };
 
-const char *FLAC__StreamDecoderWriteStatusString[] = {
-       "FLAC__STREAM_DECODER_WRITE_CONTINUE",
-       "FLAC__STREAM_DECODER_WRITE_ABORT"
+FLAC_API const char * const FLAC__StreamDecoderSeekStatusString[] = {
+       "FLAC__STREAM_DECODER_SEEK_STATUS_OK",
+       "FLAC__STREAM_DECODER_SEEK_STATUS_ERROR",
+       "FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED"
 };
 
-const char *FLAC__StreamDecoderErrorStatusString[] = {
-       "FLAC__STREAM_DECODER_ERROR_LOST_SYNC",
-       "FLAC__STREAM_DECODER_ERROR_BAD_HEADER",
-       "FLAC__STREAM_DECODER_ERROR_FRAME_CRC_MISMATCH"
+FLAC_API const char * const FLAC__StreamDecoderTellStatusString[] = {
+       "FLAC__STREAM_DECODER_TELL_STATUS_OK",
+       "FLAC__STREAM_DECODER_TELL_STATUS_ERROR",
+       "FLAC__STREAM_DECODER_TELL_STATUS_UNSUPPORTED"
 };
 
-FLAC__StreamDecoder *FLAC__stream_decoder_get_new_instance()
+FLAC_API const char * const FLAC__StreamDecoderLengthStatusString[] = {
+       "FLAC__STREAM_DECODER_LENGTH_STATUS_OK",
+       "FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR",
+       "FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED"
+};
+
+FLAC_API const char * const FLAC__StreamDecoderWriteStatusString[] = {
+       "FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE",
+       "FLAC__STREAM_DECODER_WRITE_STATUS_ABORT"
+};
+
+FLAC_API const char * const FLAC__StreamDecoderErrorStatusString[] = {
+       "FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC",
+       "FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER",
+       "FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH",
+       "FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM"
+};
+
+/***********************************************************************
+ *
+ * Class constructor/destructor
+ *
+ ***********************************************************************/
+FLAC_API FLAC__StreamDecoder *FLAC__stream_decoder_new()
 {
-       FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder*)malloc(sizeof(FLAC__StreamDecoder));
-       if(decoder != 0) {
-               decoder->state = FLAC__STREAM_DECODER_UNINITIALIZED;
-               decoder->guts = 0;
+       FLAC__StreamDecoder *decoder;
+       unsigned i;
+
+       FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
+
+       decoder = (FLAC__StreamDecoder*)calloc(1, sizeof(FLAC__StreamDecoder));
+       if(decoder == 0) {
+               return 0;
        }
+
+       decoder->protected_ = (FLAC__StreamDecoderProtected*)calloc(1, sizeof(FLAC__StreamDecoderProtected));
+       if(decoder->protected_ == 0) {
+               free(decoder);
+               return 0;
+       }
+
+       decoder->private_ = (FLAC__StreamDecoderPrivate*)calloc(1, sizeof(FLAC__StreamDecoderPrivate));
+       if(decoder->private_ == 0) {
+               free(decoder->protected_);
+               free(decoder);
+               return 0;
+       }
+
+       decoder->private_->input = FLAC__bitbuffer_new();
+       if(decoder->private_->input == 0) {
+               free(decoder->private_);
+               free(decoder->protected_);
+               free(decoder);
+               return 0;
+       }
+
+       decoder->private_->metadata_filter_ids_capacity = 16;
+       if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)malloc((FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8) * decoder->private_->metadata_filter_ids_capacity))) {
+               FLAC__bitbuffer_delete(decoder->private_->input);
+               free(decoder->private_);
+               free(decoder->protected_);
+               free(decoder);
+               return 0;
+       }
+
+       for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
+               decoder->private_->output[i] = 0;
+               decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0;
+       }
+
+       decoder->private_->output_capacity = 0;
+       decoder->private_->output_channels = 0;
+       decoder->private_->has_seek_table = false;
+
+       for(i = 0; i < FLAC__MAX_CHANNELS; i++)
+               FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&decoder->private_->partitioned_rice_contents[i]);
+
+       decoder->private_->file = 0;
+
+       set_defaults_(decoder);
+
+       decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
+
        return decoder;
 }
 
-void FLAC__stream_decoder_free_instance(FLAC__StreamDecoder *decoder)
+FLAC_API void FLAC__stream_decoder_delete(FLAC__StreamDecoder *decoder)
 {
+       unsigned i;
+
+       FLAC__ASSERT(0 != decoder);
+       FLAC__ASSERT(0 != decoder->protected_);
+       FLAC__ASSERT(0 != decoder->private_);
+       FLAC__ASSERT(0 != decoder->private_->input);
+
+       FLAC__stream_decoder_finish(decoder);
+
+       if(0 != decoder->private_->metadata_filter_ids)
+               free(decoder->private_->metadata_filter_ids);
+
+       FLAC__bitbuffer_delete(decoder->private_->input);
+
+       for(i = 0; i < FLAC__MAX_CHANNELS; i++)
+               FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&decoder->private_->partitioned_rice_contents[i]);
+
+       free(decoder->private_);
+       free(decoder->protected_);
        free(decoder);
 }
 
-FLAC__StreamDecoderState FLAC__stream_decoder_init(
+/***********************************************************************
+ *
+ * Public class methods
+ *
+ ***********************************************************************/
+
+FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_stream(
        FLAC__StreamDecoder *decoder,
-       FLAC__StreamDecoderReadStatus (*read_callback)(const FLAC__StreamDecoder *decoder, byte buffer[], unsigned *bytes, void *client_data),
-       FLAC__StreamDecoderWriteStatus (*write_callback)(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const int32 *buffer[], void *client_data),
-       void (*metadata_callback)(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data),
-       void (*error_callback)(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data),
+       FLAC__StreamDecoderReadCallback read_callback,
+       FLAC__StreamDecoderSeekCallback seek_callback,
+       FLAC__StreamDecoderTellCallback tell_callback,
+       FLAC__StreamDecoderLengthCallback length_callback,
+       FLAC__StreamDecoderEofCallback eof_callback,
+       FLAC__StreamDecoderWriteCallback write_callback,
+       FLAC__StreamDecoderMetadataCallback metadata_callback,
+       FLAC__StreamDecoderErrorCallback error_callback,
        void *client_data
 )
 {
-       unsigned i;
+       FLAC__ASSERT(0 != decoder);
 
-       assert(sizeof(int) >= 4); /* we want to die right away if this is not true */
-       assert(decoder != 0);
-       assert(read_callback != 0);
-       assert(write_callback != 0);
-       assert(metadata_callback != 0);
-       assert(error_callback != 0);
-       assert(decoder->state == FLAC__STREAM_DECODER_UNINITIALIZED);
-       assert(decoder->guts == 0);
+       if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
+               return FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED;
 
-       decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
+       if(
+               0 == read_callback ||
+               0 == write_callback ||
+               0 == error_callback ||
+               (seek_callback && (0 == tell_callback || 0 == length_callback || 0 == eof_callback))
+       )
+               return FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS;
 
-       decoder->guts = (FLAC__StreamDecoderPrivate*)malloc(sizeof(FLAC__StreamDecoderPrivate));
-       if(decoder->guts == 0)
-               return decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
+       /*
+        * get the CPU info and set the function pointers
+        */
+       FLAC__cpu_info(&decoder->private_->cpuinfo);
+       /* first default to the non-asm routines */
+       decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal;
+       decoder->private_->local_lpc_restore_signal_64bit = FLAC__lpc_restore_signal_wide;
+       decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal;
+       decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal;
+       /* now override with asm where appropriate */
+#ifndef FLAC__NO_ASM
+       if(decoder->private_->cpuinfo.use_asm) {
+#ifdef FLAC__CPU_IA32
+               FLAC__ASSERT(decoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
+#ifdef FLAC__HAS_NASM
+               if(decoder->private_->cpuinfo.data.ia32.mmx) {
+                       decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32;
+                       decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32_mmx;
+                       decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal_asm_ia32_mmx;
+               }
+               else {
+                       decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32;
+                       decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32;
+                       decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal_asm_ia32;
+               }
+#endif
+#elif defined FLAC__CPU_PPC
+               FLAC__ASSERT(decoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_PPC);
+               if(decoder->private_->cpuinfo.data.ppc.altivec) {
+                       decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ppc_altivec_16;
+                       decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal_asm_ppc_altivec_16_order8;
+               }
+#endif
+       }
+#endif
 
-       decoder->guts->read_callback = read_callback;
-       decoder->guts->write_callback = write_callback;
-       decoder->guts->metadata_callback = metadata_callback;
-       decoder->guts->error_callback = error_callback;
-       decoder->guts->client_data = client_data;
+       /* from here on, errors are fatal */
 
-       FLAC__bitbuffer_init(&decoder->guts->input);
+       if(!FLAC__bitbuffer_init(decoder->private_->input)) {
+               decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
+               return FLAC__STREAM_DECODER_INIT_STATUS_MEMORY_ALLOCATION_ERROR;
+       }
 
-       for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
-               decoder->guts->output[i] = 0;
-               decoder->guts->residual[i] = 0;
+       decoder->private_->read_callback = read_callback;
+       decoder->private_->seek_callback = seek_callback;
+       decoder->private_->tell_callback = tell_callback;
+       decoder->private_->length_callback = length_callback;
+       decoder->private_->eof_callback = eof_callback;
+       decoder->private_->write_callback = write_callback;
+       decoder->private_->metadata_callback = metadata_callback;
+       decoder->private_->error_callback = error_callback;
+       decoder->private_->client_data = client_data;
+       decoder->private_->last_frame_number = 0;
+       decoder->private_->last_block_size = 0;
+       decoder->private_->samples_decoded = 0;
+       decoder->private_->has_stream_info = false;
+       decoder->private_->cached = false;
+
+       decoder->private_->do_md5_checking = decoder->protected_->md5_checking;
+       decoder->private_->is_seeking = false;
+
+       decoder->private_->internal_reset_hack = true; /* so the following reset does not try to rewind the input */
+       if(!FLAC__stream_decoder_reset(decoder)) {
+               /* above call sets the state for us */
+               return FLAC__STREAM_DECODER_INIT_STATUS_MEMORY_ALLOCATION_ERROR;
        }
 
-       decoder->guts->output_capacity = 0;
-       decoder->guts->output_channels = 0;
-       decoder->guts->last_frame_number = 0;
-       decoder->guts->samples_decoded = 0;
-       decoder->guts->has_stream_info = false;
-       decoder->guts->has_seek_table = false;
-       decoder->guts->cached = false;
+       return FLAC__STREAM_DECODER_INIT_STATUS_OK;
+}
+
+FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_FILE(
+       FLAC__StreamDecoder *decoder,
+       FILE *file,
+       FLAC__StreamDecoderWriteCallback write_callback,
+       FLAC__StreamDecoderMetadataCallback metadata_callback,
+       FLAC__StreamDecoderErrorCallback error_callback,
+       void *client_data
+)
+{
+       FLAC__ASSERT(0 != decoder);
+       FLAC__ASSERT(0 != file);
 
-       return decoder->state;
+       if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
+               return decoder->protected_->state = FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED;
+
+       if(0 == write_callback || 0 == error_callback)
+               return decoder->protected_->state = FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS;
+
+       /*
+        * To make sure that our file does not go unclosed after an error, we
+        * must assign the FILE pointer before any further error can occur in
+        * this routine.
+        */
+       if(file == stdin)
+               file = get_binary_stdin_(); /* just to be safe */
+
+       decoder->private_->file = file;
+
+       return FLAC__stream_decoder_init_stream(
+               decoder,
+               file_read_callback_,
+               decoder->private_->file == stdin? 0: file_seek_callback_,
+               decoder->private_->file == stdin? 0: file_tell_callback_,/*@@@@@@ might work for stdin*/
+               decoder->private_->file == stdin? 0: file_length_callback_,
+               decoder->private_->file == stdin? 0: file_eof_callback_,/*@@@@@@ might work for stdin*/
+               write_callback,
+               metadata_callback,
+               error_callback,
+               client_data
+       );
 }
 
-void FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder)
+FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_file(
+       FLAC__StreamDecoder *decoder,
+       const char *filename,
+       FLAC__StreamDecoderWriteCallback write_callback,
+       FLAC__StreamDecoderMetadataCallback metadata_callback,
+       FLAC__StreamDecoderErrorCallback error_callback,
+       void *client_data
+)
 {
+       FILE *file;
+
+       FLAC__ASSERT(0 != decoder);
+
+       /*
+        * To make sure that our file does not go unclosed after an error, we
+        * have to do the same entrance checks here that are later performed
+        * in FLAC__stream_decoder_init_FILE() before the FILE* is assigned.
+        */
+       if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
+               return decoder->protected_->state = FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED;
+
+       if(0 == write_callback || 0 == error_callback)
+               return decoder->protected_->state = FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS;
+
+       file = filename? fopen(filename, "rb") : stdin;
+
+       if(0 == file)
+               return FLAC__STREAM_DECODER_INIT_STATUS_ERROR_OPENING_FILE;
+
+       return FLAC__stream_decoder_init_FILE(decoder, file, write_callback, metadata_callback, error_callback, client_data);
+}
+
+FLAC_API FLAC__bool FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder)
+{
+       FLAC__bool md5_failed = false;
        unsigned i;
-       assert(decoder != 0);
-       if(decoder->state == FLAC__STREAM_DECODER_UNINITIALIZED)
-               return;
-       if(decoder->guts != 0) {
-               if(decoder->guts->has_seek_table) {
-                       free(decoder->guts->seek_table.data.seek_table.points);
-                       decoder->guts->seek_table.data.seek_table.points = 0;
-               }
-               FLAC__bitbuffer_free(&decoder->guts->input);
-               for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
-                       if(decoder->guts->output[i] != 0) {
-                               free(decoder->guts->output[i]);
-                               decoder->guts->output[i] = 0;
-                       }
-                       if(decoder->guts->residual[i] != 0) {
-                               free(decoder->guts->residual[i]);
-                               decoder->guts->residual[i] = 0;
-                       }
+
+       FLAC__ASSERT(0 != decoder);
+       FLAC__ASSERT(0 != decoder->private_);
+       FLAC__ASSERT(0 != decoder->protected_);
+
+       if(decoder->protected_->state == FLAC__STREAM_DECODER_UNINITIALIZED)
+               return true;
+
+       /* see the comment in FLAC__seekable_stream_decoder_reset() as to why we
+        * always call FLAC__MD5Final()
+        */
+       FLAC__MD5Final(decoder->private_->computed_md5sum, &decoder->private_->md5context);
+
+       if(decoder->private_->has_seek_table && 0 != decoder->private_->seek_table.data.seek_table.points) {
+               free(decoder->private_->seek_table.data.seek_table.points);
+               decoder->private_->seek_table.data.seek_table.points = 0;
+               decoder->private_->has_seek_table = false;
+       }
+       FLAC__bitbuffer_free(decoder->private_->input);
+       for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
+               /* WATCHOUT:
+                * FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the
+                * output arrays have a buffer of up to 3 zeroes in front
+                * (at negative indices) for alignment purposes; we use 4
+                * to keep the data well-aligned.
+                */
+               if(0 != decoder->private_->output[i]) {
+                       free(decoder->private_->output[i]-4);
+                       decoder->private_->output[i] = 0;
+               }
+               if(0 != decoder->private_->residual_unaligned[i]) {
+                       free(decoder->private_->residual_unaligned[i]);
+                       decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0;
                }
-               free(decoder->guts);
-               decoder->guts = 0;
        }
-       decoder->state = FLAC__STREAM_DECODER_UNINITIALIZED;
+       decoder->private_->output_capacity = 0;
+       decoder->private_->output_channels = 0;
+
+       if(0 != decoder->private_->file) {
+               if(decoder->private_->file != stdin)
+                       fclose(decoder->private_->file);
+               decoder->private_->file = 0;
+       }
+
+       if(decoder->private_->do_md5_checking) {
+               if(memcmp(decoder->private_->stream_info.data.stream_info.md5sum, decoder->private_->computed_md5sum, 16))
+                       md5_failed = true;
+       }
+       decoder->private_->is_seeking = false;
+
+       set_defaults_(decoder);
+
+       decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
+
+       return !md5_failed;
 }
 
-bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder)
+FLAC_API FLAC__bool FLAC__stream_decoder_set_md5_checking(FLAC__StreamDecoder *decoder, FLAC__bool value)
 {
-       assert(decoder != 0);
+       FLAC__ASSERT(0 != decoder);
+       FLAC__ASSERT(0 != decoder->protected_);
+       if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
+               return false;
+       decoder->protected_->md5_checking = value;
+       return true;
+}
 
-       if(!FLAC__bitbuffer_clear(&decoder->guts->input)) {
-               decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
+FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
+{
+       FLAC__ASSERT(0 != decoder);
+       FLAC__ASSERT(0 != decoder->private_);
+       FLAC__ASSERT(0 != decoder->protected_);
+       FLAC__ASSERT((unsigned)type <= FLAC__MAX_METADATA_TYPE_CODE);
+       /* double protection */
+       if((unsigned)type > FLAC__MAX_METADATA_TYPE_CODE)
                return false;
+       if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
+               return false;
+       decoder->private_->metadata_filter[type] = true;
+       if(type == FLAC__METADATA_TYPE_APPLICATION)
+               decoder->private_->metadata_filter_ids_count = 0;
+       return true;
+}
+
+FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
+{
+       FLAC__ASSERT(0 != decoder);
+       FLAC__ASSERT(0 != decoder->private_);
+       FLAC__ASSERT(0 != decoder->protected_);
+       FLAC__ASSERT(0 != id);
+       if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
+               return false;
+
+       if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION])
+               return true;
+
+       FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);
+
+       if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) {
+               if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2))) {
+                       decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
+                       return false;
+               }
+               decoder->private_->metadata_filter_ids_capacity *= 2;
        }
 
+       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));
+       decoder->private_->metadata_filter_ids_count++;
+
        return true;
 }
 
-bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder)
+FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_all(FLAC__StreamDecoder *decoder)
 {
-       assert(decoder != 0);
+       unsigned i;
+       FLAC__ASSERT(0 != decoder);
+       FLAC__ASSERT(0 != decoder->private_);
+       FLAC__ASSERT(0 != decoder->protected_);
+       if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
+               return false;
+       for(i = 0; i < sizeof(decoder->private_->metadata_filter) / sizeof(decoder->private_->metadata_filter[0]); i++)
+               decoder->private_->metadata_filter[i] = true;
+       decoder->private_->metadata_filter_ids_count = 0;
+       return true;
+}
 
-       if(!FLAC__stream_decoder_flush(decoder)) {
-               decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
+FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
+{
+       FLAC__ASSERT(0 != decoder);
+       FLAC__ASSERT(0 != decoder->private_);
+       FLAC__ASSERT(0 != decoder->protected_);
+       FLAC__ASSERT((unsigned)type <= FLAC__MAX_METADATA_TYPE_CODE);
+       /* double protection */
+       if((unsigned)type > FLAC__MAX_METADATA_TYPE_CODE)
+               return false;
+       if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
                return false;
+       decoder->private_->metadata_filter[type] = false;
+       if(type == FLAC__METADATA_TYPE_APPLICATION)
+               decoder->private_->metadata_filter_ids_count = 0;
+       return true;
+}
+
+FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
+{
+       FLAC__ASSERT(0 != decoder);
+       FLAC__ASSERT(0 != decoder->private_);
+       FLAC__ASSERT(0 != decoder->protected_);
+       FLAC__ASSERT(0 != id);
+       if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
+               return false;
+
+       if(!decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION])
+               return true;
+
+       FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);
+
+       if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) {
+               if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2))) {
+                       decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
+                       return false;
+               }
+               decoder->private_->metadata_filter_ids_capacity *= 2;
        }
-       decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
+
+       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));
+       decoder->private_->metadata_filter_ids_count++;
 
        return true;
 }
 
-bool FLAC__stream_decoder_process_whole_stream(FLAC__StreamDecoder *decoder)
+FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_all(FLAC__StreamDecoder *decoder)
 {
-       bool dummy;
-       assert(decoder != 0);
+       FLAC__ASSERT(0 != decoder);
+       FLAC__ASSERT(0 != decoder->private_);
+       FLAC__ASSERT(0 != decoder->protected_);
+       if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
+               return false;
+       memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter));
+       decoder->private_->metadata_filter_ids_count = 0;
+       return true;
+}
 
-       if(decoder->state == FLAC__STREAM_DECODER_END_OF_STREAM)
-               return true;
+FLAC_API FLAC__StreamDecoderState FLAC__stream_decoder_get_state(const FLAC__StreamDecoder *decoder)
+{
+       FLAC__ASSERT(0 != decoder);
+       FLAC__ASSERT(0 != decoder->protected_);
+       return decoder->protected_->state;
+}
 
-       assert(decoder->state == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA);
+FLAC_API const char *FLAC__stream_decoder_get_resolved_state_string(const FLAC__StreamDecoder *decoder)
+{
+       return FLAC__StreamDecoderStateString[decoder->protected_->state];
+}
 
-       if(!FLAC__stream_decoder_reset(decoder)) {
-               decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
+FLAC_API FLAC__bool FLAC__stream_decoder_get_md5_checking(const FLAC__StreamDecoder *decoder)
+{
+       FLAC__ASSERT(0 != decoder);
+       FLAC__ASSERT(0 != decoder->protected_);
+       return decoder->protected_->md5_checking;
+}
+
+FLAC_API FLAC__uint64 FLAC__stream_decoder_get_total_samples(const FLAC__StreamDecoder *decoder)
+{
+       FLAC__ASSERT(0 != decoder);
+       FLAC__ASSERT(0 != decoder->protected_);
+       return decoder->private_->has_stream_info? decoder->private_->stream_info.data.stream_info.total_samples : 0;
+}
+
+FLAC_API unsigned FLAC__stream_decoder_get_channels(const FLAC__StreamDecoder *decoder)
+{
+       FLAC__ASSERT(0 != decoder);
+       FLAC__ASSERT(0 != decoder->protected_);
+       return decoder->protected_->channels;
+}
+
+FLAC_API FLAC__ChannelAssignment FLAC__stream_decoder_get_channel_assignment(const FLAC__StreamDecoder *decoder)
+{
+       FLAC__ASSERT(0 != decoder);
+       FLAC__ASSERT(0 != decoder->protected_);
+       return decoder->protected_->channel_assignment;
+}
+
+FLAC_API unsigned FLAC__stream_decoder_get_bits_per_sample(const FLAC__StreamDecoder *decoder)
+{
+       FLAC__ASSERT(0 != decoder);
+       FLAC__ASSERT(0 != decoder->protected_);
+       return decoder->protected_->bits_per_sample;
+}
+
+FLAC_API unsigned FLAC__stream_decoder_get_sample_rate(const FLAC__StreamDecoder *decoder)
+{
+       FLAC__ASSERT(0 != decoder);
+       FLAC__ASSERT(0 != decoder->protected_);
+       return decoder->protected_->sample_rate;
+}
+
+FLAC_API unsigned FLAC__stream_decoder_get_blocksize(const FLAC__StreamDecoder *decoder)
+{
+       FLAC__ASSERT(0 != decoder);
+       FLAC__ASSERT(0 != decoder->protected_);
+       return decoder->protected_->blocksize;
+}
+
+FLAC_API FLAC__bool FLAC__stream_decoder_get_decode_position(const FLAC__StreamDecoder *decoder, FLAC__uint64 *position)
+{
+       FLAC__ASSERT(0 != decoder);
+       FLAC__ASSERT(0 != decoder->private_);
+       FLAC__ASSERT(0 != position);
+
+       if(0 == decoder->private_->tell_callback)
+               return false;
+       if(decoder->private_->tell_callback(decoder, position, decoder->private_->client_data) != FLAC__STREAM_DECODER_TELL_STATUS_OK)
+               return false;
+       FLAC__ASSERT(*position >= FLAC__stream_decoder_get_input_bytes_unconsumed(decoder));
+       *position -= FLAC__stream_decoder_get_input_bytes_unconsumed(decoder);
+       return true;
+}
+
+FLAC_API FLAC__bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder)
+{
+       FLAC__ASSERT(0 != decoder);
+       FLAC__ASSERT(0 != decoder->private_);
+       FLAC__ASSERT(0 != decoder->protected_);
+
+       decoder->private_->samples_decoded = 0;
+       decoder->private_->do_md5_checking = false;
+
+       if(!FLAC__bitbuffer_clear(decoder->private_->input)) {
+               decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
+               return false;
+       }
+       decoder->private_->last_frame_number = 0;
+       decoder->private_->last_block_size = 0;
+       decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
+
+       return true;
+}
+
+FLAC_API FLAC__bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder)
+{
+       FLAC__ASSERT(0 != decoder);
+       FLAC__ASSERT(0 != decoder->private_);
+       FLAC__ASSERT(0 != decoder->protected_);
+
+       if(!FLAC__stream_decoder_flush(decoder)) {
+               /* above call sets the state for us */
                return false;
        }
 
+       /* Rewind if necessary.  If FLAC__stream_decoder_init() is calling us,
+        * (internal_reset_hack) don't try to rewind since we are already at
+        * the beginning of the stream and don't want to fail if the input is
+        * not seekable.
+        */
+       if(!decoder->private_->internal_reset_hack) {
+               if(decoder->private_->file == stdin)
+                       return false; /* can't rewind stdin, reset fails */
+               if(decoder->private_->seek_callback && decoder->private_->seek_callback(decoder, 0, decoder->private_->client_data) == FLAC__STREAM_DECODER_SEEK_STATUS_ERROR)
+                       return false; /* seekable and seek fails, reset fails */
+       }
+       else
+               decoder->private_->internal_reset_hack = false;
+
+       decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
+
+       decoder->private_->has_stream_info = false;
+       if(decoder->private_->has_seek_table && 0 != decoder->private_->seek_table.data.seek_table.points) {
+               free(decoder->private_->seek_table.data.seek_table.points);
+               decoder->private_->seek_table.data.seek_table.points = 0;
+               decoder->private_->has_seek_table = false;
+       }
+       decoder->private_->do_md5_checking = decoder->protected_->md5_checking;
+
+       /* We initialize the FLAC__MD5Context even though we may never use it.  This
+        * is because md5 checking may be turned on to start and then turned off if
+        * a seek occurs.  So we init the context here and finalize it in
+        * FLAC__seekable_stream_decoder_finish() to make sure things are always
+        * cleaned up properly.
+        */
+       FLAC__MD5Init(&decoder->private_->md5context);
+
+       decoder->private_->first_frame_offset = 0;
+       decoder->private_->unparseable_frame_count = 0;
+
+       return true;
+}
+
+FLAC_API FLAC__bool FLAC__stream_decoder_process_single(FLAC__StreamDecoder *decoder)
+{
+       FLAC__bool got_a_frame;
+       FLAC__ASSERT(0 != decoder);
+       FLAC__ASSERT(0 != decoder->protected_);
+
        while(1) {
-               switch(decoder->state) {
+               switch(decoder->protected_->state) {
                        case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
-                               if(!stream_decoder_find_metadata_(decoder))
+                               if(!find_metadata_(decoder))
                                        return false; /* above function sets the status for us */
                                break;
                        case FLAC__STREAM_DECODER_READ_METADATA:
-                               if(!stream_decoder_read_metadata_(decoder))
+                               if(!read_metadata_(decoder))
                                        return false; /* above function sets the status for us */
-                               break;
+                               else
+                                       return true;
                        case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
-                               if(!stream_decoder_frame_sync_(decoder))
+                               if(!frame_sync_(decoder))
                                        return true; /* above function sets the status for us */
                                break;
                        case FLAC__STREAM_DECODER_READ_FRAME:
-                               if(!stream_decoder_read_frame_(decoder, &dummy))
+                               if(!read_frame_(decoder, &got_a_frame, /*do_full_decode=*/true))
                                        return false; /* above function sets the status for us */
+                               if(got_a_frame)
+                                       return true; /* above function sets the status for us */
                                break;
                        case FLAC__STREAM_DECODER_END_OF_STREAM:
+                       case FLAC__STREAM_DECODER_ABORTED:
                                return true;
                        default:
-                               assert(0);
+                               FLAC__ASSERT(0);
+                               return false;
                }
        }
 }
 
-bool FLAC__stream_decoder_process_metadata(FLAC__StreamDecoder *decoder)
+FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_metadata(FLAC__StreamDecoder *decoder)
 {
-       assert(decoder != 0);
-
-       if(decoder->state == FLAC__STREAM_DECODER_END_OF_STREAM)
-               return true;
-
-       assert(decoder->state == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA);
-
-       if(!FLAC__stream_decoder_reset(decoder)) {
-               decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
-               return false;
-       }
+       FLAC__ASSERT(0 != decoder);
+       FLAC__ASSERT(0 != decoder->protected_);
 
        while(1) {
-               switch(decoder->state) {
+               switch(decoder->protected_->state) {
                        case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
-                               if(!stream_decoder_find_metadata_(decoder))
+                               if(!find_metadata_(decoder))
                                        return false; /* above function sets the status for us */
                                break;
                        case FLAC__STREAM_DECODER_READ_METADATA:
-                               if(!stream_decoder_read_metadata_(decoder))
+                               if(!read_metadata_(decoder))
                                        return false; /* above function sets the status for us */
                                break;
                        case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
-                               return true;
-                               break;
+                       case FLAC__STREAM_DECODER_READ_FRAME:
                        case FLAC__STREAM_DECODER_END_OF_STREAM:
+                       case FLAC__STREAM_DECODER_ABORTED:
                                return true;
                        default:
-                               assert(0);
+                               FLAC__ASSERT(0);
+                               return false;
                }
        }
 }
 
-bool FLAC__stream_decoder_process_one_frame(FLAC__StreamDecoder *decoder)
+FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_stream(FLAC__StreamDecoder *decoder)
 {
-       bool got_a_frame;
-       assert(decoder != 0);
-
-       if(decoder->state == FLAC__STREAM_DECODER_END_OF_STREAM)
-               return true;
-
-       assert(decoder->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC);
+       FLAC__bool dummy;
+       FLAC__ASSERT(0 != decoder);
+       FLAC__ASSERT(0 != decoder->protected_);
 
        while(1) {
-               switch(decoder->state) {
+               switch(decoder->protected_->state) {
+                       case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
+                               if(!find_metadata_(decoder))
+                                       return false; /* above function sets the status for us */
+                               break;
+                       case FLAC__STREAM_DECODER_READ_METADATA:
+                               if(!read_metadata_(decoder))
+                                       return false; /* above function sets the status for us */
+                               break;
                        case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
-                               if(!stream_decoder_frame_sync_(decoder))
+                               if(!frame_sync_(decoder))
                                        return true; /* above function sets the status for us */
                                break;
                        case FLAC__STREAM_DECODER_READ_FRAME:
-                               if(!stream_decoder_read_frame_(decoder, &got_a_frame))
+                               if(!read_frame_(decoder, &dummy, /*do_full_decode=*/true))
                                        return false; /* above function sets the status for us */
-                               if(got_a_frame)
-                                       return true; /* above function sets the status for us */
                                break;
                        case FLAC__STREAM_DECODER_END_OF_STREAM:
+                       case FLAC__STREAM_DECODER_ABORTED:
                                return true;
                        default:
-                               assert(0);
+                               FLAC__ASSERT(0);
+                               return false;
                }
        }
 }
 
-bool FLAC__stream_decoder_process_remaining_frames(FLAC__StreamDecoder *decoder)
+FLAC_API FLAC__bool FLAC__stream_decoder_skip_single_frame(FLAC__StreamDecoder *decoder)
 {
-       bool dummy;
-       assert(decoder != 0);
-
-       if(decoder->state == FLAC__STREAM_DECODER_END_OF_STREAM)
-               return true;
-
-       assert(decoder->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC);
+       FLAC__bool got_a_frame;
+       FLAC__ASSERT(0 != decoder);
+       FLAC__ASSERT(0 != decoder->protected_);
 
        while(1) {
-               switch(decoder->state) {
+               switch(decoder->protected_->state) {
+                       case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
+                       case FLAC__STREAM_DECODER_READ_METADATA:
+                               return false; /* above function sets the status for us */
                        case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
-                               if(!stream_decoder_frame_sync_(decoder))
+                               if(!frame_sync_(decoder))
                                        return true; /* above function sets the status for us */
                                break;
                        case FLAC__STREAM_DECODER_READ_FRAME:
-                               if(!stream_decoder_read_frame_(decoder, &dummy))
+                               if(!read_frame_(decoder, &got_a_frame, /*do_full_decode=*/false))
                                        return false; /* above function sets the status for us */
+                               if(got_a_frame)
+                                       return true; /* above function sets the status for us */
                                break;
                        case FLAC__STREAM_DECODER_END_OF_STREAM:
+                       case FLAC__STREAM_DECODER_ABORTED:
                                return true;
                        default:
-                               assert(0);
+                               FLAC__ASSERT(0);
+                               return false;
                }
        }
 }
 
-unsigned FLAC__stream_decoder_input_bytes_unconsumed(FLAC__StreamDecoder *decoder)
+FLAC_API FLAC__bool FLAC__stream_decoder_seek_absolute(FLAC__StreamDecoder *decoder, FLAC__uint64 sample)
 {
-       assert(decoder != 0);
-       return decoder->guts->input.bytes - decoder->guts->input.consumed_bytes;
+       FLAC__uint64 length;
+
+       FLAC__ASSERT(0 != decoder);
+
+       if(
+               decoder->protected_->state != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA &&
+               decoder->protected_->state != FLAC__STREAM_DECODER_READ_METADATA &&
+               decoder->protected_->state != FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC &&
+               decoder->protected_->state != FLAC__STREAM_DECODER_READ_FRAME &&
+               decoder->protected_->state != FLAC__STREAM_DECODER_END_OF_STREAM
+       )
+               return false;
+
+       if(0 == decoder->private_->seek_callback)
+               return false;
+
+       FLAC__ASSERT(decoder->private_->seek_callback);
+       FLAC__ASSERT(decoder->private_->tell_callback);
+       FLAC__ASSERT(decoder->private_->length_callback);
+       FLAC__ASSERT(decoder->private_->eof_callback);
+
+       if(FLAC__stream_decoder_get_total_samples(decoder) > 0 && sample >= FLAC__stream_decoder_get_total_samples(decoder))
+               return false;
+
+       decoder->private_->is_seeking = true;
+
+       /* turn off md5 checking if a seek is attempted */
+       decoder->private_->do_md5_checking = false;
+
+       /* 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) */
+       if(decoder->private_->length_callback(decoder, &length, decoder->private_->client_data) != FLAC__STREAM_DECODER_LENGTH_STATUS_OK) {
+               decoder->private_->is_seeking = false;
+               return false;
+       }
+       /* if we haven't finished processing the metadata yet, do that so we have the STREAMINFO, SEEK_TABLE, and first_frame_offset */
+       if(
+               decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA ||
+               decoder->protected_->state == FLAC__STREAM_DECODER_READ_METADATA
+       ) {
+               if(!FLAC__stream_decoder_process_until_end_of_metadata(decoder)) {
+                       /* above call sets the state for us */
+                       decoder->private_->is_seeking = false;
+                       return false;
+               }
+               /* check this again in case we didn't know total_samples the first time */
+               if(FLAC__stream_decoder_get_total_samples(decoder) > 0 && sample >= FLAC__stream_decoder_get_total_samples(decoder)) {
+                       decoder->private_->is_seeking = false;
+                       return false;
+               }
+       }
+
+       {
+               FLAC__bool ok = seek_to_absolute_sample_(decoder, length, sample);
+               decoder->private_->is_seeking = false;
+               return ok;
+       }
+}
+
+/***********************************************************************
+ *
+ * Protected class methods
+ *
+ ***********************************************************************/
+
+unsigned FLAC__stream_decoder_get_input_bytes_unconsumed(const FLAC__StreamDecoder *decoder)
+{
+       FLAC__ASSERT(0 != decoder);
+       return FLAC__bitbuffer_get_input_bytes_unconsumed(decoder->private_->input);
+}
+
+/***********************************************************************
+ *
+ * Private class methods
+ *
+ ***********************************************************************/
+
+void set_defaults_(FLAC__StreamDecoder *decoder)
+{
+       decoder->private_->read_callback = 0;
+       decoder->private_->seek_callback = 0;
+       decoder->private_->tell_callback = 0;
+       decoder->private_->length_callback = 0;
+       decoder->private_->eof_callback = 0;
+       decoder->private_->write_callback = 0;
+       decoder->private_->metadata_callback = 0;
+       decoder->private_->error_callback = 0;
+       decoder->private_->client_data = 0;
+
+       memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter));
+       decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO] = true;
+       decoder->private_->metadata_filter_ids_count = 0;
+
+       decoder->protected_->md5_checking = false;
+}
+
+/*
+ * This will forcibly set stdin to binary mode (for OSes that require it)
+ */
+FILE *get_binary_stdin_()
+{
+       /* if something breaks here it is probably due to the presence or
+        * absence of an underscore before the identifiers 'setmode',
+        * 'fileno', and/or 'O_BINARY'; check your system header files.
+        */
+#if defined _MSC_VER || defined __MINGW32__
+       _setmode(_fileno(stdin), _O_BINARY);
+#elif defined __CYGWIN__ || defined __EMX__
+       /* almost certainly not needed for any modern Cygwin, but let's be safe... */
+       setmode(_fileno(stdin), _O_BINARY);
+#endif
+
+       return stdin;
 }
 
-bool stream_decoder_allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels)
+FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels)
 {
        unsigned i;
-       int32 *tmp;
+       FLAC__int32 *tmp;
 
-       if(size <= decoder->guts->output_capacity && channels <= decoder->guts->output_channels)
+       if(size <= decoder->private_->output_capacity && channels <= decoder->private_->output_channels)
                return true;
 
-       /* @@@ should change to use realloc() */
+       /* simply using realloc() is not practical because the number of channels may change mid-stream */
 
        for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
-               if(decoder->guts->output[i] != 0) {
-                       free(decoder->guts->output[i]);
-                       decoder->guts->output[i] = 0;
+               if(0 != decoder->private_->output[i]) {
+                       free(decoder->private_->output[i]-4);
+                       decoder->private_->output[i] = 0;
                }
-               if(decoder->guts->residual[i] != 0) {
-                       free(decoder->guts->residual[i]);
-                       decoder->guts->residual[i] = 0;
+               if(0 != decoder->private_->residual_unaligned[i]) {
+                       free(decoder->private_->residual_unaligned[i]);
+                       decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0;
                }
        }
 
        for(i = 0; i < channels; i++) {
-               tmp = (int32*)malloc(sizeof(int32)*size);
+               /* WATCHOUT:
+                * FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the
+                * output arrays have a buffer of up to 3 zeroes in front
+                * (at negative indices) for alignment purposes; we use 4
+                * to keep the data well-aligned.
+                */
+               tmp = (FLAC__int32*)malloc(sizeof(FLAC__int32)*(size+4));
                if(tmp == 0) {
-                       decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
+                       decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
                        return false;
                }
-               decoder->guts->output[i] = tmp;
+               memset(tmp, 0, sizeof(FLAC__int32)*4);
+               decoder->private_->output[i] = tmp + 4;
 
-               tmp = (int32*)malloc(sizeof(int32)*size);
-               if(tmp == 0) {
-                       decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
+               /* WATCHOUT:
+                * minimum of quadword alignment for PPC vector optimizations is REQUIRED:
+                */
+               if(!FLAC__memory_alloc_aligned_int32_array(size, &decoder->private_->residual_unaligned[i], &decoder->private_->residual[i])) {
+                       decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
+                       return false;
+               }
+       }
+
+       decoder->private_->output_capacity = size;
+       decoder->private_->output_channels = channels;
+
+       return true;
+}
+
+FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id)
+{
+       unsigned i;
+
+       FLAC__ASSERT(0 != decoder);
+       FLAC__ASSERT(0 != decoder->private_);
+
+       for(i = 0; i < decoder->private_->metadata_filter_ids_count; i++)
+               if(0 == memcmp(decoder->private_->metadata_filter_ids + i * (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), id, (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8)))
+                       return true;
+
+       return false;
+}
+
+FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder)
+{
+       FLAC__uint32 x;
+       unsigned i, id;
+       FLAC__bool first = true;
+
+       FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
+
+       for(i = id = 0; i < 4; ) {
+               if(decoder->private_->cached) {
+                       x = (FLAC__uint32)decoder->private_->lookahead;
+                       decoder->private_->cached = false;
+               }
+               else {
+                       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
+                               return false; /* read_callback_ sets the state for us */
+               }
+               if(x == FLAC__STREAM_SYNC_STRING[i]) {
+                       first = true;
+                       i++;
+                       id = 0;
+                       continue;
+               }
+               if(x == ID3V2_TAG_[id]) {
+                       id++;
+                       i = 0;
+                       if(id == 3) {
+                               if(!skip_id3v2_tag_(decoder))
+                                       return false; /* skip_id3v2_tag_ sets the state for us */
+                       }
+                       continue;
+               }
+               id = 0;
+               if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
+                       decoder->private_->header_warmup[0] = (FLAC__byte)x;
+                       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
+                               return false; /* read_callback_ sets the state for us */
+
+                       /* 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 */
+                       /* else we have to check if the second byte is the end of a sync code */
+                       if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
+                               decoder->private_->lookahead = (FLAC__byte)x;
+                               decoder->private_->cached = true;
+                       }
+                       else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
+                               decoder->private_->header_warmup[1] = (FLAC__byte)x;
+                               decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
+                               return true;
+                       }
+               }
+               i = 0;
+               if(first) {
+                       send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
+                       first = false;
+               }
+       }
+
+       decoder->protected_->state = FLAC__STREAM_DECODER_READ_METADATA;
+       return true;
+}
+
+FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder)
+{
+       FLAC__bool is_last;
+       FLAC__uint32 i, x, type, length;
+
+       FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
+
+       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_IS_LAST_LEN, read_callback_, decoder))
+               return false; /* read_callback_ sets the state for us */
+       is_last = x? true : false;
+
+       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &type, FLAC__STREAM_METADATA_TYPE_LEN, read_callback_, decoder))
+               return false; /* read_callback_ sets the state for us */
+
+       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &length, FLAC__STREAM_METADATA_LENGTH_LEN, read_callback_, decoder))
+               return false; /* read_callback_ sets the state for us */
+
+       if(type == FLAC__METADATA_TYPE_STREAMINFO) {
+               if(!read_metadata_streaminfo_(decoder, is_last, length))
                        return false;
+
+               decoder->private_->has_stream_info = true;
+               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))
+                       decoder->private_->do_md5_checking = false;
+               if(!decoder->private_->is_seeking && decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO] && decoder->private_->metadata_callback)
+                       decoder->private_->metadata_callback(decoder, &decoder->private_->stream_info, decoder->private_->client_data);
+       }
+       else if(type == FLAC__METADATA_TYPE_SEEKTABLE) {
+               if(!read_metadata_seektable_(decoder, is_last, length))
+                       return false;
+
+               decoder->private_->has_seek_table = true;
+               if(!decoder->private_->is_seeking && decoder->private_->metadata_filter[FLAC__METADATA_TYPE_SEEKTABLE] && decoder->private_->metadata_callback)
+                       decoder->private_->metadata_callback(decoder, &decoder->private_->seek_table, decoder->private_->client_data);
+       }
+       else {
+               FLAC__bool skip_it = !decoder->private_->metadata_filter[type];
+               unsigned real_length = length;
+               FLAC__StreamMetadata block;
+
+               block.is_last = is_last;
+               block.type = (FLAC__MetadataType)type;
+               block.length = length;
+
+               if(type == FLAC__METADATA_TYPE_APPLICATION) {
+                       if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8, read_callback_, decoder))
+                               return false; /* read_callback_ sets the state for us */
+
+                       real_length -= FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8;
+
+                       if(decoder->private_->metadata_filter_ids_count > 0 && has_id_filtered_(decoder, block.data.application.id))
+                               skip_it = !skip_it;
+               }
+
+               if(skip_it) {
+                       if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, real_length, read_callback_, decoder))
+                               return false; /* read_callback_ sets the state for us */
+               }
+               else {
+                       switch(type) {
+                               case FLAC__METADATA_TYPE_PADDING:
+                                       /* skip the padding bytes */
+                                       if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, real_length, read_callback_, decoder))
+                                               return false; /* read_callback_ sets the state for us */
+                                       break;
+                               case FLAC__METADATA_TYPE_APPLICATION:
+                                       /* remember, we read the ID already */
+                                       if(real_length > 0) {
+                                               if(0 == (block.data.application.data = (FLAC__byte*)malloc(real_length))) {
+                                                       decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
+                                                       return false;
+                                               }
+                                               if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.application.data, real_length, read_callback_, decoder))
+                                                       return false; /* read_callback_ sets the state for us */
+                                       }
+                                       else
+                                               block.data.application.data = 0;
+                                       break;
+                               case FLAC__METADATA_TYPE_VORBIS_COMMENT:
+                                       if(!read_metadata_vorbiscomment_(decoder, &block.data.vorbis_comment))
+                                               return false;
+                                       break;
+                               case FLAC__METADATA_TYPE_CUESHEET:
+                                       if(!read_metadata_cuesheet_(decoder, &block.data.cue_sheet))
+                                               return false;
+                                       break;
+                               case FLAC__METADATA_TYPE_PICTURE:
+                                       if(!read_metadata_picture_(decoder, &block.data.picture))
+                                               return false;
+                                       break;
+                               case FLAC__METADATA_TYPE_STREAMINFO:
+                               case FLAC__METADATA_TYPE_SEEKTABLE:
+                                       FLAC__ASSERT(0);
+                                       break;
+                               default:
+                                       if(real_length > 0) {
+                                               if(0 == (block.data.unknown.data = (FLAC__byte*)malloc(real_length))) {
+                                                       decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
+                                                       return false;
+                                               }
+                                               if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.unknown.data, real_length, read_callback_, decoder))
+                                                       return false; /* read_callback_ sets the state for us */
+                                       }
+                                       else
+                                               block.data.unknown.data = 0;
+                                       break;
+                       }
+                       if(!decoder->private_->is_seeking && decoder->private_->metadata_callback)
+                               decoder->private_->metadata_callback(decoder, &block, decoder->private_->client_data);
+
+                       /* now we have to free any malloc'ed data in the block */
+                       switch(type) {
+                               case FLAC__METADATA_TYPE_PADDING:
+                                       break;
+                               case FLAC__METADATA_TYPE_APPLICATION:
+                                       if(0 != block.data.application.data)
+                                               free(block.data.application.data);
+                                       break;
+                               case FLAC__METADATA_TYPE_VORBIS_COMMENT:
+                                       if(0 != block.data.vorbis_comment.vendor_string.entry)
+                                               free(block.data.vorbis_comment.vendor_string.entry);
+                                       if(block.data.vorbis_comment.num_comments > 0)
+                                               for(i = 0; i < block.data.vorbis_comment.num_comments; i++)
+                                                       if(0 != block.data.vorbis_comment.comments[i].entry)
+                                                               free(block.data.vorbis_comment.comments[i].entry);
+                                       if(0 != block.data.vorbis_comment.comments)
+                                               free(block.data.vorbis_comment.comments);
+                                       break;
+                               case FLAC__METADATA_TYPE_CUESHEET:
+                                       if(block.data.cue_sheet.num_tracks > 0)
+                                               for(i = 0; i < block.data.cue_sheet.num_tracks; i++)
+                                                       if(0 != block.data.cue_sheet.tracks[i].indices)
+                                                               free(block.data.cue_sheet.tracks[i].indices);
+                                       if(0 != block.data.cue_sheet.tracks)
+                                               free(block.data.cue_sheet.tracks);
+                                       break;
+                               case FLAC__METADATA_TYPE_PICTURE:
+                                       if(0 != block.data.picture.mime_type)
+                                               free(block.data.picture.mime_type);
+                                       if(0 != block.data.picture.description)
+                                               free(block.data.picture.description);
+                                       if(0 != block.data.picture.data)
+                                               free(block.data.picture.data);
+                                       break;
+                               case FLAC__METADATA_TYPE_STREAMINFO:
+                               case FLAC__METADATA_TYPE_SEEKTABLE:
+                                       FLAC__ASSERT(0);
+                               default:
+                                       if(0 != block.data.unknown.data)
+                                               free(block.data.unknown.data);
+                                       break;
+                       }
                }
-               decoder->guts->residual[i] = tmp;
        }
 
-       decoder->guts->output_capacity = size;
-       decoder->guts->output_channels = channels;
+       if(is_last) {
+               /* if this fails, it's OK, it's just a hint for the seek routine */
+               if(!FLAC__stream_decoder_get_decode_position(decoder, &decoder->private_->first_frame_offset))
+                       decoder->private_->first_frame_offset = 0;
+               decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
+       }
+
+       return true;
+}
+
+FLAC__bool read_metadata_streaminfo_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length)
+{
+       FLAC__uint32 x;
+       unsigned bits, used_bits = 0;
+
+       FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
+
+       decoder->private_->stream_info.type = FLAC__METADATA_TYPE_STREAMINFO;
+       decoder->private_->stream_info.is_last = is_last;
+       decoder->private_->stream_info.length = length;
+
+       bits = FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN;
+       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, bits, read_callback_, decoder))
+               return false; /* read_callback_ sets the state for us */
+       decoder->private_->stream_info.data.stream_info.min_blocksize = x;
+       used_bits += bits;
+
+       bits = FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN;
+       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN, read_callback_, decoder))
+               return false; /* read_callback_ sets the state for us */
+       decoder->private_->stream_info.data.stream_info.max_blocksize = x;
+       used_bits += bits;
+
+       bits = FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN;
+       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN, read_callback_, decoder))
+               return false; /* read_callback_ sets the state for us */
+       decoder->private_->stream_info.data.stream_info.min_framesize = x;
+       used_bits += bits;
+
+       bits = FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN;
+       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN, read_callback_, decoder))
+               return false; /* read_callback_ sets the state for us */
+       decoder->private_->stream_info.data.stream_info.max_framesize = x;
+       used_bits += bits;
+
+       bits = FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN;
+       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN, read_callback_, decoder))
+               return false; /* read_callback_ sets the state for us */
+       decoder->private_->stream_info.data.stream_info.sample_rate = x;
+       used_bits += bits;
+
+       bits = FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN;
+       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN, read_callback_, decoder))
+               return false; /* read_callback_ sets the state for us */
+       decoder->private_->stream_info.data.stream_info.channels = x+1;
+       used_bits += bits;
+
+       bits = FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN;
+       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN, read_callback_, decoder))
+               return false; /* read_callback_ sets the state for us */
+       decoder->private_->stream_info.data.stream_info.bits_per_sample = x+1;
+       used_bits += bits;
+
+       bits = FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN;
+       if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &decoder->private_->stream_info.data.stream_info.total_samples, FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN, read_callback_, decoder))
+               return false; /* read_callback_ sets the state for us */
+       used_bits += bits;
+
+       if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, decoder->private_->stream_info.data.stream_info.md5sum, 16, read_callback_, decoder))
+               return false; /* read_callback_ sets the state for us */
+       used_bits += 16*8;
+
+       /* skip the rest of the block */
+       FLAC__ASSERT(used_bits % 8 == 0);
+       length -= (used_bits / 8);
+       if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, length, read_callback_, decoder))
+               return false; /* read_callback_ sets the state for us */
+
+       return true;
+}
+
+FLAC__bool read_metadata_seektable_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length)
+{
+       FLAC__uint32 i, x;
+       FLAC__uint64 xx;
+
+       FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
+
+       decoder->private_->seek_table.type = FLAC__METADATA_TYPE_SEEKTABLE;
+       decoder->private_->seek_table.is_last = is_last;
+       decoder->private_->seek_table.length = length;
+
+       decoder->private_->seek_table.data.seek_table.num_points = length / FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
+
+       /* use realloc since we may pass through here several times (e.g. after seeking) */
+       if(0 == (decoder->private_->seek_table.data.seek_table.points = (FLAC__StreamMetadata_SeekPoint*)realloc(decoder->private_->seek_table.data.seek_table.points, decoder->private_->seek_table.data.seek_table.num_points * sizeof(FLAC__StreamMetadata_SeekPoint)))) {
+               decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
+               return false;
+       }
+       for(i = 0; i < decoder->private_->seek_table.data.seek_table.num_points; i++) {
+               if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN, read_callback_, decoder))
+                       return false; /* read_callback_ sets the state for us */
+               decoder->private_->seek_table.data.seek_table.points[i].sample_number = xx;
+
+               if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN, read_callback_, decoder))
+                       return false; /* read_callback_ sets the state for us */
+               decoder->private_->seek_table.data.seek_table.points[i].stream_offset = xx;
+
+               if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN, read_callback_, decoder))
+                       return false; /* read_callback_ sets the state for us */
+               decoder->private_->seek_table.data.seek_table.points[i].frame_samples = x;
+       }
+       length -= (decoder->private_->seek_table.data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH);
+       /* if there is a partial point left, skip over it */
+       if(length > 0) {
+               /*@@@ do an error_callback() here?  there's an argument for either way */
+               if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, length, read_callback_, decoder))
+                       return false; /* read_callback_ sets the state for us */
+       }
+
+       return true;
+}
+
+FLAC__bool read_metadata_vorbiscomment_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_VorbisComment *obj)
+{
+       FLAC__uint32 i;
+
+       FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
+
+       /* read vendor string */
+       FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32);
+       if(!FLAC__bitbuffer_read_raw_uint32_little_endian(decoder->private_->input, &obj->vendor_string.length, read_callback_, decoder))
+               return false; /* read_callback_ sets the state for us */
+       if(obj->vendor_string.length > 0) {
+               if(0 == (obj->vendor_string.entry = (FLAC__byte*)malloc(obj->vendor_string.length+1))) {
+                       decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
+                       return false;
+               }
+               if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, obj->vendor_string.entry, obj->vendor_string.length, read_callback_, decoder))
+                       return false; /* read_callback_ sets the state for us */
+               obj->vendor_string.entry[obj->vendor_string.length] = '\0';
+       }
+       else
+               obj->vendor_string.entry = 0;
+
+       /* read num comments */
+       FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN == 32);
+       if(!FLAC__bitbuffer_read_raw_uint32_little_endian(decoder->private_->input, &obj->num_comments, read_callback_, decoder))
+               return false; /* read_callback_ sets the state for us */
+
+       /* read comments */
+       if(obj->num_comments > 0) {
+               if(0 == (obj->comments = (FLAC__StreamMetadata_VorbisComment_Entry*)malloc(obj->num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry)))) {
+                       decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
+                       return false;
+               }
+               for(i = 0; i < obj->num_comments; i++) {
+                       FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32);
+                       if(!FLAC__bitbuffer_read_raw_uint32_little_endian(decoder->private_->input, &obj->comments[i].length, read_callback_, decoder))
+                               return false; /* read_callback_ sets the state for us */
+                       if(obj->comments[i].length > 0) {
+                               if(0 == (obj->comments[i].entry = (FLAC__byte*)malloc(obj->comments[i].length+1))) {
+                                       decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
+                                       return false;
+                               }
+                               if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, obj->comments[i].entry, obj->comments[i].length, read_callback_, decoder))
+                                       return false; /* read_callback_ sets the state for us */
+                               obj->comments[i].entry[obj->comments[i].length] = '\0';
+                       }
+                       else
+                               obj->comments[i].entry = 0;
+               }
+       }
+       else {
+               obj->comments = 0;
+       }
+
+       return true;
+}
+
+FLAC__bool read_metadata_cuesheet_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_CueSheet *obj)
+{
+       FLAC__uint32 i, j, x;
+
+       FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
+
+       memset(obj, 0, sizeof(FLAC__StreamMetadata_CueSheet));
+
+       FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN % 8 == 0);
+       if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, (FLAC__byte*)obj->media_catalog_number, FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN/8, read_callback_, decoder))
+               return false; /* read_callback_ sets the state for us */
+
+       if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &obj->lead_in, FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN, read_callback_, decoder))
+               return false; /* read_callback_ sets the state for us */
 
-       return true;
-}
+       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN, read_callback_, decoder))
+               return false; /* read_callback_ sets the state for us */
+       obj->is_cd = x? true : false;
 
-bool stream_decoder_find_metadata_(FLAC__StreamDecoder *decoder)
-{
-       uint32 x;
-       unsigned i, id;
-       bool first = true;
+       if(!FLAC__bitbuffer_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN, read_callback_, decoder))
+               return false; /* read_callback_ sets the state for us */
 
-       assert(decoder->guts->input.consumed_bits == 0); /* make sure we're byte aligned */
+       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN, read_callback_, decoder))
+               return false; /* read_callback_ sets the state for us */
+       obj->num_tracks = x;
 
-       for(i = id = 0; i < 4; ) {
-               if(decoder->guts->cached) {
-                       x = (uint32)decoder->guts->lookahead;
-                       decoder->guts->cached = false;
-               }
-               else {
-                       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
-                               return false; /* the read_callback_ sets the state for us */
-               }
-               if(x == FLAC__STREAM_SYNC_STRING[i]) {
-                       first = true;
-                       i++;
-                       id = 0;
-                       continue;
-               }
-               if(x == ID3V2_TAG_[id]) {
-                       id++;
-                       i = 0;
-                       if(id == 3) {
-                               if(!stream_decoder_skip_id3v2_tag_(decoder))
-                                       return false; /* the read_callback_ sets the state for us */
-                       }
-                       continue;
+       if(obj->num_tracks > 0) {
+               if(0 == (obj->tracks = (FLAC__StreamMetadata_CueSheet_Track*)calloc(obj->num_tracks, sizeof(FLAC__StreamMetadata_CueSheet_Track)))) {
+                       decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
+                       return false;
                }
-               if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
-                       decoder->guts->header_warmup[0] = (byte)x;
-                       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
-                               return false; /* the read_callback_ sets the state for us */
+               for(i = 0; i < obj->num_tracks; i++) {
+                       FLAC__StreamMetadata_CueSheet_Track *track = &obj->tracks[i];
+                       if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &track->offset, FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN, read_callback_, decoder))
+                               return false; /* read_callback_ sets the state for us */
+
+                       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN, read_callback_, decoder))
+                               return false; /* read_callback_ sets the state for us */
+                       track->number = (FLAC__byte)x;
+
+                       FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN % 8 == 0);
+                       if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, (FLAC__byte*)track->isrc, FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN/8, read_callback_, decoder))
+                               return false; /* read_callback_ sets the state for us */
+
+                       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN, read_callback_, decoder))
+                               return false; /* read_callback_ sets the state for us */
+                       track->type = x;
+
+                       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN, read_callback_, decoder))
+                               return false; /* read_callback_ sets the state for us */
+                       track->pre_emphasis = x;
+
+                       if(!FLAC__bitbuffer_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN, read_callback_, decoder))
+                               return false; /* read_callback_ sets the state for us */
+
+                       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN, read_callback_, decoder))
+                               return false; /* read_callback_ sets the state for us */
+                       track->num_indices = (FLAC__byte)x;
+
+                       if(track->num_indices > 0) {
+                               if(0 == (track->indices = (FLAC__StreamMetadata_CueSheet_Index*)calloc(track->num_indices, sizeof(FLAC__StreamMetadata_CueSheet_Index)))) {
+                                       decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
+                                       return false;
+                               }
+                               for(j = 0; j < track->num_indices; j++) {
+                                       FLAC__StreamMetadata_CueSheet_Index *index = &track->indices[j];
+                                       if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &index->offset, FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN, read_callback_, decoder))
+                                               return false; /* read_callback_ sets the state for us */
 
-                       /* 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 */
-                       /* else we have to check if the second byte is the end of a sync code */
-                       if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
-                               decoder->guts->lookahead = (byte)x;
-                               decoder->guts->cached = true;
-                       }
-                       else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
-                               decoder->guts->header_warmup[1] = (byte)x;
-                               decoder->state = FLAC__STREAM_DECODER_READ_FRAME;
-                               return true;
+                                       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN, read_callback_, decoder))
+                                               return false; /* read_callback_ sets the state for us */
+                                       index->number = (FLAC__byte)x;
+
+                                       if(!FLAC__bitbuffer_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN, read_callback_, decoder))
+                                               return false; /* read_callback_ sets the state for us */
+                               }
                        }
                }
-               i = 0;
-               if(first) {
-                       decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
-                       first = false;
-               }
        }
 
-       decoder->state = FLAC__STREAM_DECODER_READ_METADATA;
        return true;
 }
 
-bool stream_decoder_read_metadata_(FLAC__StreamDecoder *decoder)
+FLAC__bool read_metadata_picture_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_Picture *obj)
 {
-       uint32 i, x, last_block, type, length;
-       uint64 xx;
+       FLAC__uint32 len;
 
-       assert(decoder->guts->input.consumed_bits == 0); /* make sure we're byte aligned */
+       FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
 
-       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &last_block, FLAC__STREAM_METADATA_IS_LAST_LEN, read_callback_, decoder))
-               return false; /* the read_callback_ sets the state for us */
-       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &type, FLAC__STREAM_METADATA_TYPE_LEN, read_callback_, decoder))
-               return false; /* the read_callback_ sets the state for us */
-       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &length, FLAC__STREAM_METADATA_LENGTH_LEN, read_callback_, decoder))
-               return false; /* the read_callback_ sets the state for us */
-       if(type == FLAC__METADATA_TYPE_STREAMINFO) {
-               unsigned used_bits = 0;
-               decoder->guts->stream_info.type = type;
-               decoder->guts->stream_info.is_last = last_block;
-               decoder->guts->stream_info.length = length;
-
-               if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN, read_callback_, decoder))
-                       return false; /* the read_callback_ sets the state for us */
-               decoder->guts->stream_info.data.stream_info.min_blocksize = x;
-               used_bits += FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN;
-
-               if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN, read_callback_, decoder))
-                       return false; /* the read_callback_ sets the state for us */
-               decoder->guts->stream_info.data.stream_info.max_blocksize = x;
-               used_bits += FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN;
-
-               if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN, read_callback_, decoder))
-                       return false; /* the read_callback_ sets the state for us */
-               decoder->guts->stream_info.data.stream_info.min_framesize = x;
-               used_bits += FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN;
-
-               if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN, read_callback_, decoder))
-                       return false; /* the read_callback_ sets the state for us */
-               decoder->guts->stream_info.data.stream_info.max_framesize = x;
-               used_bits += FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN;
-
-               if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN, read_callback_, decoder))
-                       return false; /* the read_callback_ sets the state for us */
-               decoder->guts->stream_info.data.stream_info.sample_rate = x;
-               used_bits += FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN;
-
-               if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN, read_callback_, decoder))
-                       return false; /* the read_callback_ sets the state for us */
-               decoder->guts->stream_info.data.stream_info.channels = x+1;
-               used_bits += FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN;
-
-               if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN, read_callback_, decoder))
-                       return false; /* the read_callback_ sets the state for us */
-               decoder->guts->stream_info.data.stream_info.bits_per_sample = x+1;
-               used_bits += FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN;
-
-               if(!FLAC__bitbuffer_read_raw_uint64(&decoder->guts->input, &decoder->guts->stream_info.data.stream_info.total_samples, FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN, read_callback_, decoder))
-                       return false; /* the read_callback_ sets the state for us */
-               used_bits += FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN;
-
-               for(i = 0; i < 16; i++) {
-                       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
-                               return false; /* the read_callback_ sets the state for us */
-                       decoder->guts->stream_info.data.stream_info.md5sum[i] = (byte)x;
-               }
-               used_bits += i*8;
-
-               /* skip the rest of the block */
-               assert(used_bits % 8 == 0);
-               length -= (used_bits / 8);
-               for(i = 0; i < length; i++) {
-                       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
-                               return false; /* the read_callback_ sets the state for us */
-               }
-
-               decoder->guts->has_stream_info = true;
-               decoder->guts->metadata_callback(decoder, &decoder->guts->stream_info, decoder->guts->client_data);
+       /* read type */
+       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &obj->type, FLAC__STREAM_METADATA_PICTURE_TYPE_LEN, read_callback_, decoder))
+               return false; /* read_callback_ sets the state for us */
+
+       /* read MIME type */
+       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &len, FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN, read_callback_, decoder))
+               return false; /* read_callback_ sets the state for us */
+       if(0 == (obj->mime_type = (char*)malloc(len+1))) {
+               decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
+               return false;
        }
-       else if(type == FLAC__METADATA_TYPE_SEEKTABLE) {
-               decoder->guts->seek_table.type = type;
-               decoder->guts->seek_table.is_last = last_block;
-               decoder->guts->seek_table.length = length;
+       if(len > 0) {
+               if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, (FLAC__byte*)obj->mime_type, len, read_callback_, decoder))
+                       return false; /* read_callback_ sets the state for us */
+       }
+       obj->mime_type[len] = '\0';
 
-               decoder->guts->seek_table.data.seek_table.num_points = length / FLAC__STREAM_METADATA_SEEKPOINT_LEN;
+       /* read description */
+       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &len, FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN, read_callback_, decoder))
+               return false; /* read_callback_ sets the state for us */
+       if(0 == (obj->description = (FLAC__byte*)malloc(len+1))) {
+               decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
+               return false;
+       }
+       if(len > 0) {
+               if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, obj->description, len, read_callback_, decoder))
+                       return false; /* read_callback_ sets the state for us */
+       }
+       obj->description[len] = '\0';
 
-               if(0 == (decoder->guts->seek_table.data.seek_table.points = (FLAC__StreamMetaData_SeekPoint*)malloc(decoder->guts->seek_table.data.seek_table.num_points * sizeof(FLAC__StreamMetaData_SeekPoint)))) {
-                       decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
-                       return false;
-               }
-               for(i = 0; i < decoder->guts->seek_table.data.seek_table.num_points; i++) {
-                       if(!FLAC__bitbuffer_read_raw_uint64(&decoder->guts->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN, read_callback_, decoder))
-                               return false; /* the read_callback_ sets the state for us */
-                       decoder->guts->seek_table.data.seek_table.points[i].sample_number = xx;
+       /* read width */
+       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &obj->width, FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN, read_callback_, decoder))
+               return false; /* read_callback_ sets the state for us */
 
-                       if(!FLAC__bitbuffer_read_raw_uint64(&decoder->guts->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN, read_callback_, decoder))
-                               return false; /* the read_callback_ sets the state for us */
-                       decoder->guts->seek_table.data.seek_table.points[i].stream_offset = xx;
+       /* read height */
+       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &obj->height, FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN, read_callback_, decoder))
+               return false; /* read_callback_ sets the state for us */
 
-                       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_SEEKPOINT_BLOCK_OFFSET_LEN, read_callback_, decoder))
-                               return false; /* the read_callback_ sets the state for us */
-                       decoder->guts->seek_table.data.seek_table.points[i].block_offset = x;
-               }
+       /* read depth */
+       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &obj->depth, FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN, read_callback_, decoder))
+               return false; /* read_callback_ sets the state for us */
 
-               decoder->guts->has_seek_table = true;
-               decoder->guts->metadata_callback(decoder, &decoder->guts->seek_table, decoder->guts->client_data);
+       /* read colors */
+       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &obj->colors, FLAC__STREAM_METADATA_PICTURE_COLORS_LEN, read_callback_, decoder))
+               return false; /* read_callback_ sets the state for us */
+
+       /* read data */
+       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &(obj->data_length), FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN, read_callback_, decoder))
+               return false; /* read_callback_ sets the state for us */
+       if(0 == (obj->data = (FLAC__byte*)malloc(obj->data_length))) {
+               decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
+               return false;
        }
-       else {
-               /* skip other metadata blocks */
-               for(i = 0; i < length; i++) {
-                       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
-                               return false; /* the read_callback_ sets the state for us */
-               }
+       if(obj->data_length > 0) {
+               if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, obj->data, obj->data_length, read_callback_, decoder))
+                       return false; /* read_callback_ sets the state for us */
        }
 
-       if(last_block)
-               decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
-
        return true;
 }
 
-bool stream_decoder_skip_id3v2_tag_(FLAC__StreamDecoder *decoder)
+FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder)
 {
-       uint32 x;
+       FLAC__uint32 x;
        unsigned i, skip;
 
        /* skip the version and flags bytes */
-       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 24, read_callback_, decoder))
-               return false; /* the read_callback_ sets the state for us */
+       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 24, read_callback_, decoder))
+               return false; /* read_callback_ sets the state for us */
        /* get the size (in bytes) to skip */
        skip = 0;
        for(i = 0; i < 4; i++) {
-               if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
-                       return false; /* the read_callback_ sets the state for us */
+               if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
+                       return false; /* read_callback_ sets the state for us */
                skip <<= 7;
                skip |= (x & 0x7f);
        }
        /* skip the rest of the tag */
-       for(i = 0; i < skip; i++) {
-               if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
-                       return false; /* the read_callback_ sets the state for us */
-       }
+       if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, skip, read_callback_, decoder))
+               return false; /* read_callback_ sets the state for us */
        return true;
 }
 
-bool stream_decoder_frame_sync_(FLAC__StreamDecoder *decoder)
+FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder)
 {
-       uint32 x;
-       bool first = true;
+       FLAC__uint32 x;
+       FLAC__bool first = true;
 
        /* If we know the total number of samples in the stream, stop if we've read that many. */
        /* This will stop us, for example, from wasting time trying to sync on an ID3V1 tag. */
-       if(decoder->guts->has_stream_info && decoder->guts->stream_info.data.stream_info.total_samples) {
-               if(decoder->guts->samples_decoded >= decoder->guts->stream_info.data.stream_info.total_samples) {
-                       decoder->state = FLAC__STREAM_DECODER_END_OF_STREAM;
+       if(FLAC__stream_decoder_get_total_samples(decoder) > 0) {
+               if(decoder->private_->samples_decoded >= FLAC__stream_decoder_get_total_samples(decoder)) {
+                       decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
                        return true;
                }
        }
 
        /* make sure we're byte aligned */
-       if(decoder->guts->input.consumed_bits != 0) {
-               if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8-decoder->guts->input.consumed_bits, read_callback_, decoder))
-                       return false; /* the read_callback_ sets the state for us */
+       if(!FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)) {
+               if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__bitbuffer_bits_left_for_byte_alignment(decoder->private_->input), read_callback_, decoder))
+                       return false; /* read_callback_ sets the state for us */
        }
 
        while(1) {
-               if(decoder->guts->cached) {
-                       x = (uint32)decoder->guts->lookahead;
-                       decoder->guts->cached = false;
+               if(decoder->private_->cached) {
+                       x = (FLAC__uint32)decoder->private_->lookahead;
+                       decoder->private_->cached = false;
                }
                else {
-                       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
-                               return false; /* the read_callback_ sets the state for us */
+                       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
+                               return false; /* read_callback_ sets the state for us */
                }
                if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
-                       decoder->guts->header_warmup[0] = (byte)x;
-                       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
-                               return false; /* the read_callback_ sets the state for us */
+                       decoder->private_->header_warmup[0] = (FLAC__byte)x;
+                       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
+                               return false; /* read_callback_ sets the state for us */
 
                        /* 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 */
                        /* else we have to check if the second byte is the end of a sync code */
                        if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
-                               decoder->guts->lookahead = (byte)x;
-                               decoder->guts->cached = true;
+                               decoder->private_->lookahead = (FLAC__byte)x;
+                               decoder->private_->cached = true;
                        }
                        else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
-                               decoder->guts->header_warmup[1] = (byte)x;
-                               decoder->state = FLAC__STREAM_DECODER_READ_FRAME;
+                               decoder->private_->header_warmup[1] = (FLAC__byte)x;
+                               decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
                                return true;
                        }
                }
                if(first) {
-                       decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
-                       first = 0;
+                       send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
+                       first = false;
                }
        }
 
        return true;
 }
 
-bool stream_decoder_read_frame_(FLAC__StreamDecoder *decoder, bool *got_a_frame)
+FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame, FLAC__bool do_full_decode)
 {
        unsigned channel;
        unsigned i;
-       int32 mid, side, left, right;
-       uint16 frame_crc; /* the one we calculate from the input stream */
-       uint32 x;
+       FLAC__int32 mid, side, left, right;
+       FLAC__uint16 frame_crc; /* the one we calculate from the input stream */
+       FLAC__uint32 x;
 
        *got_a_frame = false;
 
        /* init the CRC */
        frame_crc = 0;
-       FLAC__CRC16_UPDATE(decoder->guts->header_warmup[0], frame_crc);
-       FLAC__CRC16_UPDATE(decoder->guts->header_warmup[1], frame_crc);
-       FLAC__bitbuffer_init_read_crc16(&decoder->guts->input, frame_crc);
+       FLAC__CRC16_UPDATE(decoder->private_->header_warmup[0], frame_crc);
+       FLAC__CRC16_UPDATE(decoder->private_->header_warmup[1], frame_crc);
+       FLAC__bitbuffer_reset_read_crc16(decoder->private_->input, frame_crc);
 
-       if(!stream_decoder_read_frame_header_(decoder))
+       if(!read_frame_header_(decoder))
                return false;
-       if(decoder->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC)
+       if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means we didn't sync on a valid header */
                return true;
-       if(!stream_decoder_allocate_output_(decoder, decoder->guts->frame.header.blocksize, decoder->guts->frame.header.channels))
+       if(!allocate_output_(decoder, decoder->private_->frame.header.blocksize, decoder->private_->frame.header.channels))
                return false;
-       for(channel = 0; channel < decoder->guts->frame.header.channels; channel++) {
+       for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
                /*
                 * first figure the correct bits-per-sample of the subframe
                 */
-               unsigned bps = decoder->guts->frame.header.bits_per_sample;
-               switch(decoder->guts->frame.header.channel_assignment) {
+               unsigned bps = decoder->private_->frame.header.bits_per_sample;
+               switch(decoder->private_->frame.header.channel_assignment) {
                        case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
                                /* no adjustment needed */
                                break;
                        case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
-                               assert(decoder->guts->frame.header.channels == 2);
+                               FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
                                if(channel == 1)
                                        bps++;
                                break;
                        case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
-                               assert(decoder->guts->frame.header.channels == 2);
+                               FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
                                if(channel == 0)
                                        bps++;
                                break;
                        case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
-                               assert(decoder->guts->frame.header.channels == 2);
+                               FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
                                if(channel == 1)
                                        bps++;
                                break;
                        default:
-                               assert(0);
+                               FLAC__ASSERT(0);
                }
                /*
                 * now read it
                 */
-               if(!stream_decoder_read_subframe_(decoder, channel, bps))
+               if(!read_subframe_(decoder, channel, bps, do_full_decode))
                        return false;
-               if(decoder->state != FLAC__STREAM_DECODER_READ_FRAME) {
-                       decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
+               if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means bad sync or got corruption */
                        return true;
-               }
        }
-       if(!stream_decoder_read_zero_padding_(decoder))
+       if(!read_zero_padding_(decoder))
                return false;
 
        /*
         * Read the frame CRC-16 from the footer and check
         */
-       frame_crc = decoder->guts->input.read_crc16;
-       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__FRAME_FOOTER_CRC_LEN, read_callback_, decoder))
-               return false; /* the read_callback_ sets the state for us */
-       if(frame_crc == (uint16)x) {
-               /* Undo any special channel coding */
-               switch(decoder->guts->frame.header.channel_assignment) {
-                       case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
-                               /* do nothing */
-                               break;
-                       case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
-                               assert(decoder->guts->frame.header.channels == 2);
-                               for(i = 0; i < decoder->guts->frame.header.blocksize; i++)
-                                       decoder->guts->output[1][i] = decoder->guts->output[0][i] - decoder->guts->output[1][i];
-                               break;
-                       case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
-                               assert(decoder->guts->frame.header.channels == 2);
-                               for(i = 0; i < decoder->guts->frame.header.blocksize; i++)
-                                       decoder->guts->output[0][i] += decoder->guts->output[1][i];
-                               break;
-                       case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
-                               assert(decoder->guts->frame.header.channels == 2);
-                               for(i = 0; i < decoder->guts->frame.header.blocksize; i++) {
-                                       mid = decoder->guts->output[0][i];
-                                       side = decoder->guts->output[1][i];
-                                       mid <<= 1;
-                                       if(side & 1) /* i.e. if 'side' is odd... */
-                                               mid++;
-                                       left = mid + side;
-                                       right = mid - side;
-                                       decoder->guts->output[0][i] = left >> 1;
-                                       decoder->guts->output[1][i] = right >> 1;
-                               }
-                               break;
-                       default:
-                               assert(0);
-                               break;
+       frame_crc = FLAC__bitbuffer_get_read_crc16(decoder->private_->input);
+       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__FRAME_FOOTER_CRC_LEN, read_callback_, decoder))
+               return false; /* read_callback_ sets the state for us */
+       if(frame_crc == (FLAC__uint16)x) {
+               if(do_full_decode) {
+                       /* Undo any special channel coding */
+                       switch(decoder->private_->frame.header.channel_assignment) {
+                               case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
+                                       /* do nothing */
+                                       break;
+                               case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
+                                       FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
+                                       for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
+                                               decoder->private_->output[1][i] = decoder->private_->output[0][i] - decoder->private_->output[1][i];
+                                       break;
+                               case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
+                                       FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
+                                       for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
+                                               decoder->private_->output[0][i] += decoder->private_->output[1][i];
+                                       break;
+                               case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
+                                       FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
+                                       for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
+                                               mid = decoder->private_->output[0][i];
+                                               side = decoder->private_->output[1][i];
+                                               mid <<= 1;
+                                               if(side & 1) /* i.e. if 'side' is odd... */
+                                                       mid++;
+                                               left = mid + side;
+                                               right = mid - side;
+                                               decoder->private_->output[0][i] = left >> 1;
+                                               decoder->private_->output[1][i] = right >> 1;
+                                       }
+                                       break;
+                               default:
+                                       FLAC__ASSERT(0);
+                                       break;
+                       }
                }
        }
        else {
                /* Bad frame, emit error and zero the output signal */
-               decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_FRAME_CRC_MISMATCH, decoder->guts->client_data);
-               for(channel = 0; channel < decoder->guts->frame.header.channels; channel++) {
-                       memset(decoder->guts->output[channel], 0, sizeof(int32) * decoder->guts->frame.header.blocksize);
+               send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH);
+               if(do_full_decode) {
+                       for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
+                               memset(decoder->private_->output[channel], 0, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize);
+                       }
                }
        }
 
        *got_a_frame = true;
 
        /* put the latest values into the public section of the decoder instance */
-       decoder->channels = decoder->guts->frame.header.channels;
-       decoder->channel_assignment = decoder->guts->frame.header.channel_assignment;
-       decoder->bits_per_sample = decoder->guts->frame.header.bits_per_sample;
-       decoder->sample_rate = decoder->guts->frame.header.sample_rate;
-       decoder->blocksize = decoder->guts->frame.header.blocksize;
+       decoder->protected_->channels = decoder->private_->frame.header.channels;
+       decoder->protected_->channel_assignment = decoder->private_->frame.header.channel_assignment;
+       decoder->protected_->bits_per_sample = decoder->private_->frame.header.bits_per_sample;
+       decoder->protected_->sample_rate = decoder->private_->frame.header.sample_rate;
+       decoder->protected_->blocksize = decoder->private_->frame.header.blocksize;
 
-       decoder->guts->samples_decoded += decoder->guts->frame.header.blocksize;
+       FLAC__ASSERT(decoder->private_->frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
+       decoder->private_->samples_decoded = decoder->private_->frame.header.number.sample_number + decoder->private_->frame.header.blocksize;
 
        /* write it */
-       if(decoder->guts->write_callback(decoder, &decoder->guts->frame, decoder->guts->output, decoder->guts->client_data) != FLAC__STREAM_DECODER_WRITE_CONTINUE)
-               return false;
+       if(do_full_decode) {
+               if(write_audio_frame_to_client_(decoder, &decoder->private_->frame, (const FLAC__int32 * const *)decoder->private_->output) != FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE)
+                       return false;
+       }
 
-       decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
+       decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
        return true;
 }
 
-bool stream_decoder_read_frame_header_(FLAC__StreamDecoder *decoder)
+FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder)
 {
-       uint32 x;
-       uint64 xx;
+       FLAC__uint32 x;
+       FLAC__uint64 xx;
        unsigned i, blocksize_hint = 0, sample_rate_hint = 0;
-       byte crc8, raw_header[16]; /* MAGIC NUMBER based on the maximum frame header size, including CRC */
+       FLAC__byte crc8, raw_header[16]; /* MAGIC NUMBER based on the maximum frame header size, including CRC */
        unsigned raw_header_len;
-       bool is_unparseable = false;
+       FLAC__bool is_unparseable = false;
+       const FLAC__bool is_known_variable_blocksize_stream = (decoder->private_->has_stream_info && decoder->private_->stream_info.data.stream_info.min_blocksize != decoder->private_->stream_info.data.stream_info.max_blocksize);
+       const FLAC__bool is_known_fixed_blocksize_stream = (decoder->private_->has_stream_info && decoder->private_->stream_info.data.stream_info.min_blocksize == decoder->private_->stream_info.data.stream_info.max_blocksize);
 
-       assert(decoder->guts->input.consumed_bits == 0); /* make sure we're byte aligned */
+       FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
 
        /* init the raw header with the saved bits from synchronization */
-       raw_header[0] = decoder->guts->header_warmup[0];
-       raw_header[1] = decoder->guts->header_warmup[1];
+       raw_header[0] = decoder->private_->header_warmup[0];
+       raw_header[1] = decoder->private_->header_warmup[1];
        raw_header_len = 2;
 
        /*
@@ -816,34 +1945,34 @@ bool stream_decoder_read_frame_header_(FLAC__StreamDecoder *decoder)
         * read in the raw header as bytes so we can CRC it, and parse it on the way
         */
        for(i = 0; i < 2; i++) {
-               if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
-                       return false; /* the read_callback_ sets the state for us */
+               if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
+                       return false; /* read_callback_ sets the state for us */
                if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
                        /* if we get here it means our original sync was erroneous since the sync code cannot appear in the header */
-                       decoder->guts->lookahead = (byte)x;
-                       decoder->guts->cached = true;
-                       decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->guts->client_data);
-                       decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
+                       decoder->private_->lookahead = (FLAC__byte)x;
+                       decoder->private_->cached = true;
+                       send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
+                       decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
                        return true;
                }
-               raw_header[raw_header_len++] = (byte)x;
+               raw_header[raw_header_len++] = (FLAC__byte)x;
        }
 
        switch(x = raw_header[2] >> 4) {
                case 0:
-                       if(decoder->guts->has_stream_info && decoder->guts->stream_info.data.stream_info.min_blocksize == decoder->guts->stream_info.data.stream_info.max_blocksize) /* i.e. it's a fixed-blocksize stream */
-                               decoder->guts->frame.header.blocksize = decoder->guts->stream_info.data.stream_info.min_blocksize;
+                       if(is_known_fixed_blocksize_stream)
+                               decoder->private_->frame.header.blocksize = decoder->private_->stream_info.data.stream_info.min_blocksize;
                        else
                                is_unparseable = true;
                        break;
                case 1:
-                       decoder->guts->frame.header.blocksize = 192;
+                       decoder->private_->frame.header.blocksize = 192;
                        break;
                case 2:
                case 3:
                case 4:
                case 5:
-                       decoder->guts->frame.header.blocksize = 576 << (x-2);
+                       decoder->private_->frame.header.blocksize = 576 << (x-2);
                        break;
                case 6:
                case 7:
@@ -857,17 +1986,17 @@ bool stream_decoder_read_frame_header_(FLAC__StreamDecoder *decoder)
                case 13:
                case 14:
                case 15:
-                       decoder->guts->frame.header.blocksize = 256 << (x-8);
+                       decoder->private_->frame.header.blocksize = 256 << (x-8);
                        break;
                default:
-                       assert(0);
+                       FLAC__ASSERT(0);
                        break;
        }
 
        switch(x = raw_header[2] & 0x0f) {
                case 0:
-                       if(decoder->guts->has_stream_info)
-                               decoder->guts->frame.header.sample_rate = decoder->guts->stream_info.data.stream_info.sample_rate;
+                       if(decoder->private_->has_stream_info)
+                               decoder->private_->frame.header.sample_rate = decoder->private_->stream_info.data.stream_info.sample_rate;
                        else
                                is_unparseable = true;
                        break;
@@ -877,28 +2006,28 @@ bool stream_decoder_read_frame_header_(FLAC__StreamDecoder *decoder)
                        is_unparseable = true;
                        break;
                case 4:
-                       decoder->guts->frame.header.sample_rate = 8000;
+                       decoder->private_->frame.header.sample_rate = 8000;
                        break;
                case 5:
-                       decoder->guts->frame.header.sample_rate = 16000;
+                       decoder->private_->frame.header.sample_rate = 16000;
                        break;
                case 6:
-                       decoder->guts->frame.header.sample_rate = 22050;
+                       decoder->private_->frame.header.sample_rate = 22050;
                        break;
                case 7:
-                       decoder->guts->frame.header.sample_rate = 24000;
+                       decoder->private_->frame.header.sample_rate = 24000;
                        break;
                case 8:
-                       decoder->guts->frame.header.sample_rate = 32000;
+                       decoder->private_->frame.header.sample_rate = 32000;
                        break;
                case 9:
-                       decoder->guts->frame.header.sample_rate = 44100;
+                       decoder->private_->frame.header.sample_rate = 44100;
                        break;
                case 10:
-                       decoder->guts->frame.header.sample_rate = 48000;
+                       decoder->private_->frame.header.sample_rate = 48000;
                        break;
                case 11:
-                       decoder->guts->frame.header.sample_rate = 96000;
+                       decoder->private_->frame.header.sample_rate = 96000;
                        break;
                case 12:
                case 13:
@@ -906,25 +2035,25 @@ bool stream_decoder_read_frame_header_(FLAC__StreamDecoder *decoder)
                        sample_rate_hint = x;
                        break;
                case 15:
-                       decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->guts->client_data);
-                       decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
+                       send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
+                       decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
                        return true;
                default:
-                       assert(0);
+                       FLAC__ASSERT(0);
        }
 
        x = (unsigned)(raw_header[3] >> 4);
        if(x & 8) {
-               decoder->guts->frame.header.channels = 2;
+               decoder->private_->frame.header.channels = 2;
                switch(x & 7) {
                        case 0:
-                               decoder->guts->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE;
+                               decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE;
                                break;
                        case 1:
-                               decoder->guts->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE;
+                               decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE;
                                break;
                        case 2:
-                               decoder->guts->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_MID_SIDE;
+                               decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_MID_SIDE;
                                break;
                        default:
                                is_unparseable = true;
@@ -932,394 +2061,463 @@ bool stream_decoder_read_frame_header_(FLAC__StreamDecoder *decoder)
                }
        }
        else {
-               decoder->guts->frame.header.channels = (unsigned)x + 1;
-               decoder->guts->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT;
+               decoder->private_->frame.header.channels = (unsigned)x + 1;
+               decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT;
        }
 
        switch(x = (unsigned)(raw_header[3] & 0x0e) >> 1) {
                case 0:
-                       if(decoder->guts->has_stream_info)
-                               decoder->guts->frame.header.bits_per_sample = decoder->guts->stream_info.data.stream_info.bits_per_sample;
+                       if(decoder->private_->has_stream_info)
+                               decoder->private_->frame.header.bits_per_sample = decoder->private_->stream_info.data.stream_info.bits_per_sample;
                        else
                                is_unparseable = true;
                        break;
                case 1:
-                       decoder->guts->frame.header.bits_per_sample = 8;
+                       decoder->private_->frame.header.bits_per_sample = 8;
                        break;
                case 2:
-                       decoder->guts->frame.header.bits_per_sample = 12;
+                       decoder->private_->frame.header.bits_per_sample = 12;
                        break;
                case 4:
-                       decoder->guts->frame.header.bits_per_sample = 16;
+                       decoder->private_->frame.header.bits_per_sample = 16;
                        break;
                case 5:
-                       decoder->guts->frame.header.bits_per_sample = 20;
+                       decoder->private_->frame.header.bits_per_sample = 20;
                        break;
                case 6:
-                       decoder->guts->frame.header.bits_per_sample = 24;
+                       decoder->private_->frame.header.bits_per_sample = 24;
                        break;
                case 3:
                case 7:
                        is_unparseable = true;
                        break;
                default:
-                       assert(0);
+                       FLAC__ASSERT(0);
                        break;
        }
 
        if(raw_header[3] & 0x01) { /* this should be a zero padding bit */
-               decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->guts->client_data);
-               decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
+               send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
+               decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
                return true;
        }
 
-       if(blocksize_hint) {
-               if(!FLAC__bitbuffer_read_utf8_uint64(&decoder->guts->input, &xx, read_callback_, decoder, raw_header, &raw_header_len))
-                       return false; /* the read_callback_ sets the state for us */
-               if(xx == 0xffffffffffffffff) { /* i.e. non-UTF8 code... */
-                       decoder->guts->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
-                       decoder->guts->cached = true;
-                       decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->guts->client_data);
-                       decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
-                       return true;
+       /*
+        * Now we get to the regrettable consequences of not knowing for sure
+        * whether we got a frame number or a sample number.  There are no
+        * encoders that do variable-blocksize encoding so unless we know from
+        * the STREAMINFO that it is variable-blocksize we will assume it is
+        * fixed-blocksize.  The trouble comes when we have no STREAMINFO; again
+        * we will guess that is fixed-blocksize.  Where this can go wrong: 1) a
+        * variable-blocksize stream with no STREAMINFO; 2) a fixed-blocksize
+        * stream that was edited such that one or more frames before or
+        * including this one do not have the same number of samples as the
+        * STREAMINFO's min and max blocksize.
+        */
+       if(is_known_variable_blocksize_stream) {
+               if(blocksize_hint) {
+                       if(!FLAC__bitbuffer_read_utf8_uint64(decoder->private_->input, &xx, read_callback_, decoder, raw_header, &raw_header_len))
+                               return false; /* read_callback_ sets the state for us */
+                       if(xx == FLAC__U64L(0xffffffffffffffff)) { /* i.e. non-UTF8 code... */
+                               decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
+                               decoder->private_->cached = true;
+                               send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
+                               decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
+                               return true;
+                       }
+                       decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
+                       decoder->private_->frame.header.number.sample_number = xx;
                }
-               if(decoder->guts->has_stream_info && decoder->guts->stream_info.data.stream_info.min_blocksize == decoder->guts->stream_info.data.stream_info.max_blocksize) /* i.e. it's a fixed-blocksize stream */
-                       decoder->guts->frame.header.number.sample_number = (uint64)decoder->guts->last_frame_number * (int64)decoder->guts->stream_info.data.stream_info.min_blocksize + xx;
                else
-                       decoder->guts->frame.header.number.sample_number = xx;
+                       is_unparseable = true;
        }
        else {
-               if(!FLAC__bitbuffer_read_utf8_uint32(&decoder->guts->input, &x, read_callback_, decoder, raw_header, &raw_header_len))
-                       return false; /* the read_callback_ sets the state for us */
+               if(!FLAC__bitbuffer_read_utf8_uint32(decoder->private_->input, &x, read_callback_, decoder, raw_header, &raw_header_len))
+                       return false; /* read_callback_ sets the state for us */
                if(x == 0xffffffff) { /* i.e. non-UTF8 code... */
-                       decoder->guts->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
-                       decoder->guts->cached = true;
-                       decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->guts->client_data);
-                       decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
+                       decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
+                       decoder->private_->cached = true;
+                       send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
+                       decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
                        return true;
                }
-               decoder->guts->last_frame_number = x;
-               if(decoder->guts->has_stream_info) {
-                       decoder->guts->frame.header.number.sample_number = (int64)decoder->guts->stream_info.data.stream_info.min_blocksize * (int64)x;
+               decoder->private_->last_frame_number = x;
+               decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
+               if(decoder->private_->has_stream_info) {
+                       FLAC__ASSERT(decoder->private_->stream_info.data.stream_info.min_blocksize == decoder->private_->stream_info.data.stream_info.max_blocksize);
+                       decoder->private_->frame.header.number.sample_number = (FLAC__uint64)decoder->private_->stream_info.data.stream_info.min_blocksize * (FLAC__uint64)x;
+                       decoder->private_->last_block_size = decoder->private_->frame.header.blocksize;
+               }
+               else if(blocksize_hint) {
+                       if(decoder->private_->last_block_size)
+                               decoder->private_->frame.header.number.sample_number = (FLAC__uint64)decoder->private_->last_block_size * (FLAC__uint64)x;
+                       else
+                               is_unparseable = true;
                }
                else {
-                       is_unparseable = true;
+                       decoder->private_->frame.header.number.sample_number = (FLAC__uint64)decoder->private_->frame.header.blocksize * (FLAC__uint64)x;
+                       decoder->private_->last_block_size = decoder->private_->frame.header.blocksize;
                }
        }
 
        if(blocksize_hint) {
-               if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
-                       return false; /* the read_callback_ sets the state for us */
-               raw_header[raw_header_len++] = (byte)x;
+               if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
+                       return false; /* read_callback_ sets the state for us */
+               raw_header[raw_header_len++] = (FLAC__byte)x;
                if(blocksize_hint == 7) {
-                       uint32 _x;
-                       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &_x, 8, read_callback_, decoder))
-                               return false; /* the read_callback_ sets the state for us */
-                       raw_header[raw_header_len++] = (byte)_x;
+                       FLAC__uint32 _x;
+                       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &_x, 8, read_callback_, decoder))
+                               return false; /* read_callback_ sets the state for us */
+                       raw_header[raw_header_len++] = (FLAC__byte)_x;
                        x = (x << 8) | _x;
                }
-               decoder->guts->frame.header.blocksize = x+1;
+               decoder->private_->frame.header.blocksize = x+1;
        }
 
        if(sample_rate_hint) {
-               if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
-                       return false; /* the read_callback_ sets the state for us */
-               raw_header[raw_header_len++] = (byte)x;
+               if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
+                       return false; /* read_callback_ sets the state for us */
+               raw_header[raw_header_len++] = (FLAC__byte)x;
                if(sample_rate_hint != 12) {
-                       uint32 _x;
-                       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &_x, 8, read_callback_, decoder))
-                               return false; /* the read_callback_ sets the state for us */
-                       raw_header[raw_header_len++] = (byte)_x;
+                       FLAC__uint32 _x;
+                       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &_x, 8, read_callback_, decoder))
+                               return false; /* read_callback_ sets the state for us */
+                       raw_header[raw_header_len++] = (FLAC__byte)_x;
                        x = (x << 8) | _x;
                }
                if(sample_rate_hint == 12)
-                       decoder->guts->frame.header.sample_rate = x*1000;
+                       decoder->private_->frame.header.sample_rate = x*1000;
                else if(sample_rate_hint == 13)
-                       decoder->guts->frame.header.sample_rate = x;
+                       decoder->private_->frame.header.sample_rate = x;
                else
-                       decoder->guts->frame.header.sample_rate = x*10;
+                       decoder->private_->frame.header.sample_rate = x*10;
        }
 
        /* read the CRC-8 byte */
-       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
-               return false; /* the read_callback_ sets the state for us */
-       crc8 = (byte)x;
+       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
+               return false; /* read_callback_ sets the state for us */
+       crc8 = (FLAC__byte)x;
 
        if(FLAC__crc8(raw_header, raw_header_len) != crc8) {
-               decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->guts->client_data);
-               decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
+               send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
+               decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
                return true;
        }
 
        if(is_unparseable) {
-               decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
-               return false;
+               send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
+               decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
+               return true;
        }
 
        return true;
 }
 
-bool stream_decoder_read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
+FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode)
 {
-       uint32 x;
-       bool wasted_bits;
+       FLAC__uint32 x;
+       FLAC__bool wasted_bits;
+       unsigned i;
 
-       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder)) /* MAGIC NUMBER */
-               return false; /* the read_callback_ sets the state for us */
+       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder)) /* MAGIC NUMBER */
+               return false; /* read_callback_ sets the state for us */
 
        wasted_bits = (x & 1);
        x &= 0xfe;
 
        if(wasted_bits) {
                unsigned u;
-               if(!FLAC__bitbuffer_read_unary_unsigned(&decoder->guts->input, &u, read_callback_, decoder))
-                       return false; /* the read_callback_ sets the state for us */
-               decoder->guts->frame.subframes[channel].wasted_bits = u+1;
-               bps -= decoder->guts->frame.subframes[channel].wasted_bits;
+               if(!FLAC__bitbuffer_read_unary_unsigned(decoder->private_->input, &u, read_callback_, decoder))
+                       return false; /* read_callback_ sets the state for us */
+               decoder->private_->frame.subframes[channel].wasted_bits = u+1;
+               bps -= decoder->private_->frame.subframes[channel].wasted_bits;
        }
        else
-               decoder->guts->frame.subframes[channel].wasted_bits = 0;
+               decoder->private_->frame.subframes[channel].wasted_bits = 0;
 
        /*
         * Lots of magic numbers here
         */
        if(x & 0x80) {
-               decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
-               decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
+               send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
+               decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
                return true;
        }
        else if(x == 0) {
-               if(!stream_decoder_read_subframe_constant_(decoder, channel, bps))
+               if(!read_subframe_constant_(decoder, channel, bps, do_full_decode))
                        return false;
        }
        else if(x == 2) {
-               if(!stream_decoder_read_subframe_verbatim_(decoder, channel, bps))
+               if(!read_subframe_verbatim_(decoder, channel, bps, do_full_decode))
                        return false;
        }
        else if(x < 16) {
-               decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
-               return false;
+               send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
+               decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
+               return true;
        }
        else if(x <= 24) {
-               if(!stream_decoder_read_subframe_fixed_(decoder, channel, bps, (x>>1)&7))
+               if(!read_subframe_fixed_(decoder, channel, bps, (x>>1)&7, do_full_decode))
                        return false;
+               if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means bad sync or got corruption */
+                       return true;
        }
        else if(x < 64) {
-               decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
-               return false;
+               send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
+               decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
+               return true;
        }
        else {
-               if(!stream_decoder_read_subframe_lpc_(decoder, channel, bps, ((x>>1)&31)+1))
+               if(!read_subframe_lpc_(decoder, channel, bps, ((x>>1)&31)+1, do_full_decode))
                        return false;
+               if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means bad sync or got corruption */
+                       return true;
        }
 
-       if(wasted_bits) {
-               unsigned i;
-               x = decoder->guts->frame.subframes[channel].wasted_bits;
-               for(i = 0; i < decoder->guts->frame.header.blocksize; i++)
-                       decoder->guts->output[channel][i] <<= x;
+       if(wasted_bits && do_full_decode) {
+               x = decoder->private_->frame.subframes[channel].wasted_bits;
+               for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
+                       decoder->private_->output[channel][i] <<= x;
        }
 
        return true;
 }
 
-bool stream_decoder_read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
+FLAC__bool read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode)
 {
-       FLAC__Subframe_Constant *subframe = &decoder->guts->frame.subframes[channel].data.constant;
-       int32 x;
+       FLAC__Subframe_Constant *subframe = &decoder->private_->frame.subframes[channel].data.constant;
+       FLAC__int32 x;
        unsigned i;
-       int32 *output = decoder->guts->output[channel];
+       FLAC__int32 *output = decoder->private_->output[channel];
 
-       decoder->guts->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_CONSTANT;
+       decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_CONSTANT;
 
-       if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &x, bps, read_callback_, decoder))
-               return false; /* the read_callback_ sets the state for us */
+       if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &x, bps, read_callback_, decoder))
+               return false; /* read_callback_ sets the state for us */
 
        subframe->value = x;
 
        /* decode the subframe */
-       for(i = 0; i < decoder->guts->frame.header.blocksize; i++)
-               output[i] = x;
+       if(do_full_decode) {
+               for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
+                       output[i] = x;
+       }
 
        return true;
 }
 
-bool stream_decoder_read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order)
+FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode)
 {
-       FLAC__Subframe_Fixed *subframe = &decoder->guts->frame.subframes[channel].data.fixed;
-       int32 i32;
-       uint32 u32;
+       FLAC__Subframe_Fixed *subframe = &decoder->private_->frame.subframes[channel].data.fixed;
+       FLAC__int32 i32;
+       FLAC__uint32 u32;
        unsigned u;
 
-       decoder->guts->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_FIXED;
+       decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_FIXED;
 
-       subframe->residual = decoder->guts->residual[channel];
+       subframe->residual = decoder->private_->residual[channel];
        subframe->order = order;
 
        /* read warm-up samples */
        for(u = 0; u < order; u++) {
-               if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &i32, bps, read_callback_, decoder))
-                       return false; /* the read_callback_ sets the state for us */
+               if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, bps, read_callback_, decoder))
+                       return false; /* read_callback_ sets the state for us */
                subframe->warmup[u] = i32;
        }
 
        /* read entropy coding method info */
-       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
-               return false; /* the read_callback_ sets the state for us */
-       subframe->entropy_coding_method.type = u32;
+       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
+               return false; /* read_callback_ sets the state for us */
+       subframe->entropy_coding_method.type = (FLAC__EntropyCodingMethodType)u32;
        switch(subframe->entropy_coding_method.type) {
                case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
-                       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
-                               return false; /* the read_callback_ sets the state for us */
+                       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
+                               return false; /* read_callback_ sets the state for us */
                        subframe->entropy_coding_method.data.partitioned_rice.order = u32;
+                       subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel];
                        break;
                default:
-                       decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
-                       return false;
+                       send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
+                       decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
+                       return true;
        }
 
        /* read residual */
        switch(subframe->entropy_coding_method.type) {
                case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
-                       if(!stream_decoder_read_residual_partitioned_rice_(decoder, order, subframe->entropy_coding_method.data.partitioned_rice.order, decoder->guts->residual[channel]))
+                       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]))
                                return false;
                        break;
                default:
-                       assert(0);
+                       FLAC__ASSERT(0);
        }
 
        /* decode the subframe */
-       memcpy(decoder->guts->output[channel], subframe->warmup, sizeof(int32) * order);
-       FLAC__fixed_restore_signal(decoder->guts->residual[channel], decoder->guts->frame.header.blocksize-order, order, decoder->guts->output[channel]+order);
+       if(do_full_decode) {
+               memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
+               FLAC__fixed_restore_signal(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, order, decoder->private_->output[channel]+order);
+       }
 
        return true;
 }
 
-bool stream_decoder_read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order)
+FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode)
 {
-       FLAC__Subframe_LPC *subframe = &decoder->guts->frame.subframes[channel].data.lpc;
-       int32 i32;
-       uint32 u32;
+       FLAC__Subframe_LPC *subframe = &decoder->private_->frame.subframes[channel].data.lpc;
+       FLAC__int32 i32;
+       FLAC__uint32 u32;
        unsigned u;
 
-       decoder->guts->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_LPC;
+       decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_LPC;
 
-       subframe->residual = decoder->guts->residual[channel];
+       subframe->residual = decoder->private_->residual[channel];
        subframe->order = order;
 
        /* read warm-up samples */
        for(u = 0; u < order; u++) {
-               if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &i32, bps, read_callback_, decoder))
-                       return false; /* the read_callback_ sets the state for us */
+               if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, bps, read_callback_, decoder))
+                       return false; /* read_callback_ sets the state for us */
                subframe->warmup[u] = i32;
        }
 
        /* read qlp coeff precision */
-       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN, read_callback_, decoder))
-               return false; /* the read_callback_ sets the state for us */
-       if(u32 == (1 << FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN) - 1) {
-               decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
-               decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
+       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN, read_callback_, decoder))
+               return false; /* read_callback_ sets the state for us */
+       if(u32 == (1u << FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN) - 1) {
+               send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
+               decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
                return true;
        }
        subframe->qlp_coeff_precision = u32+1;
 
        /* read qlp shift */
-       if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &i32, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN, read_callback_, decoder))
-               return false; /* the read_callback_ sets the state for us */
+       if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN, read_callback_, decoder))
+               return false; /* read_callback_ sets the state for us */
        subframe->quantization_level = i32;
 
        /* read quantized lp coefficiencts */
        for(u = 0; u < order; u++) {
-               if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &i32, subframe->qlp_coeff_precision, read_callback_, decoder))
-                       return false; /* the read_callback_ sets the state for us */
+               if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, subframe->qlp_coeff_precision, read_callback_, decoder))
+                       return false; /* read_callback_ sets the state for us */
                subframe->qlp_coeff[u] = i32;
        }
 
        /* read entropy coding method info */
-       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
-               return false; /* the read_callback_ sets the state for us */
-       subframe->entropy_coding_method.type = u32;
+       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
+               return false; /* read_callback_ sets the state for us */
+       subframe->entropy_coding_method.type = (FLAC__EntropyCodingMethodType)u32;
        switch(subframe->entropy_coding_method.type) {
                case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
-                       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
-                               return false; /* the read_callback_ sets the state for us */
+                       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
+                               return false; /* read_callback_ sets the state for us */
                        subframe->entropy_coding_method.data.partitioned_rice.order = u32;
+                       subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel];
                        break;
                default:
-                       decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
-                       return false;
+                       send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
+                       decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
+                       return true;
        }
 
        /* read residual */
        switch(subframe->entropy_coding_method.type) {
                case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
-                       if(!stream_decoder_read_residual_partitioned_rice_(decoder, order, subframe->entropy_coding_method.data.partitioned_rice.order, decoder->guts->residual[channel]))
+                       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]))
                                return false;
                        break;
                default:
-                       assert(0);
+                       FLAC__ASSERT(0);
        }
 
        /* decode the subframe */
-       memcpy(decoder->guts->output[channel], subframe->warmup, sizeof(int32) * order);
-       FLAC__lpc_restore_signal(decoder->guts->residual[channel], decoder->guts->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->guts->output[channel]+order);
+       if(do_full_decode) {
+               memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
+               if(bps + subframe->qlp_coeff_precision + FLAC__bitmath_ilog2(order) <= 32)
+                       if(bps <= 16 && subframe->qlp_coeff_precision <= 16) {
+                               if(order <= 8)
+                                       decoder->private_->local_lpc_restore_signal_16bit_order8(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private_->output[channel]+order);
+                               else
+                                       decoder->private_->local_lpc_restore_signal_16bit(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private_->output[channel]+order);
+                       }
+                       else
+                               decoder->private_->local_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);
+               else
+                       decoder->private_->local_lpc_restore_signal_64bit(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private_->output[channel]+order);
+       }
 
        return true;
 }
 
-bool stream_decoder_read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
+FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode)
 {
-       FLAC__Subframe_Verbatim *subframe = &decoder->guts->frame.subframes[channel].data.verbatim;
-       int32 x, *residual = decoder->guts->residual[channel];
+       FLAC__Subframe_Verbatim *subframe = &decoder->private_->frame.subframes[channel].data.verbatim;
+       FLAC__int32 x, *residual = decoder->private_->residual[channel];
        unsigned i;
 
-       decoder->guts->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_VERBATIM;
+       decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_VERBATIM;
 
        subframe->data = residual;
 
-       for(i = 0; i < decoder->guts->frame.header.blocksize; i++) {
-               if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &x, bps, read_callback_, decoder))
-                       return false; /* the read_callback_ sets the state for us */
+       for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
+               if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &x, bps, read_callback_, decoder))
+                       return false; /* read_callback_ sets the state for us */
                residual[i] = x;
        }
 
        /* decode the subframe */
-       memcpy(decoder->guts->output[channel], subframe->data, sizeof(int32) * decoder->guts->frame.header.blocksize);
+       if(do_full_decode)
+               memcpy(decoder->private_->output[channel], subframe->data, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize);
 
        return true;
 }
 
-bool stream_decoder_read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order, int32 *residual)
+FLAC__bool read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order, FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, FLAC__int32 *residual)
 {
-       uint32 rice_parameter;
+       FLAC__uint32 rice_parameter;
        int i;
        unsigned partition, sample, u;
        const unsigned partitions = 1u << partition_order;
-       const unsigned partition_samples = partition_order > 0? decoder->guts->frame.header.blocksize >> partition_order : decoder->guts->frame.header.blocksize - predictor_order;
+       const unsigned partition_samples = partition_order > 0? decoder->private_->frame.header.blocksize >> partition_order : decoder->private_->frame.header.blocksize - predictor_order;
+
+       /* sanity checks */
+       if(partition_order == 0) {
+               if(decoder->private_->frame.header.blocksize < predictor_order) {
+                       send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
+                       decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
+                       return true;
+               }
+       }
+       else {
+               if(partition_samples < predictor_order) {
+                       send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
+                       decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
+                       return true;
+               }
+       }
+
+       if(!FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order))) {
+               decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
+               return false;
+       }
 
        sample = 0;
        for(partition = 0; partition < partitions; partition++) {
-               if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN, read_callback_, decoder))
-                       return false; /* the read_callback_ sets the state for us */
+               if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN, read_callback_, decoder))
+                       return false; /* read_callback_ sets the state for us */
+               partitioned_rice_contents->parameters[partition] = rice_parameter;
                if(rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
-                       for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
-#ifdef SYMMETRIC_RICE
-                               if(!FLAC__bitbuffer_read_symmetric_rice_signed(&decoder->guts->input, &i, rice_parameter, read_callback_, decoder))
-                                       return false; /* the read_callback_ sets the state for us */
-#else
-                               if(!FLAC__bitbuffer_read_rice_signed(&decoder->guts->input, &i, rice_parameter, read_callback_, decoder))
-                                       return false; /* the read_callback_ sets the state for us */
-#endif
-                               residual[sample] = i;
-                       }
+                       u = (partition_order == 0 || partition > 0)? partition_samples : partition_samples - predictor_order;
+                       if(!FLAC__bitbuffer_read_rice_signed_block(decoder->private_->input, residual + sample, u, rice_parameter, read_callback_, decoder))
+                               return false; /* read_callback_ sets the state for us */
+                       sample += u;
                }
                else {
-                       if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN, read_callback_, decoder))
-                               return false; /* the read_callback_ sets the state for us */
+                       if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN, read_callback_, decoder))
+                               return false; /* read_callback_ sets the state for us */
+                       partitioned_rice_contents->raw_bits[partition] = rice_parameter;
                        for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
-                               if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &i, rice_parameter, read_callback_, decoder))
-                                       return false; /* the read_callback_ sets the state for us */
+                               if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i, rice_parameter, read_callback_, decoder))
+                                       return false; /* read_callback_ sets the state for us */
                                residual[sample] = i;
                        }
                }
@@ -1328,28 +2526,426 @@ bool stream_decoder_read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder
        return true;
 }
 
-bool stream_decoder_read_zero_padding_(FLAC__StreamDecoder *decoder)
+FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder)
 {
-       if(decoder->guts->input.consumed_bits != 0) {
-               uint32 zero = 0;
-               if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &zero, 8-decoder->guts->input.consumed_bits, read_callback_, decoder))
-                       return false; /* the read_callback_ sets the state for us */
+       if(!FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)) {
+               FLAC__uint32 zero = 0;
+               if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &zero, FLAC__bitbuffer_bits_left_for_byte_alignment(decoder->private_->input), read_callback_, decoder))
+                       return false; /* read_callback_ sets the state for us */
                if(zero != 0) {
-                       decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
-                       decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
+                       send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
+                       decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
                }
        }
        return true;
 }
 
-bool read_callback_(byte buffer[], unsigned *bytes, void *client_data)
+FLAC__bool read_callback_(FLAC__byte buffer[], unsigned *bytes, void *client_data)
 {
        FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder *)client_data;
-       FLAC__StreamDecoderReadStatus status;
-       status = decoder->guts->read_callback(decoder, buffer, bytes, decoder->guts->client_data);
-       if(status == FLAC__STREAM_DECODER_READ_END_OF_STREAM)
-               decoder->state = FLAC__STREAM_DECODER_END_OF_STREAM;
-       else if(status == FLAC__STREAM_DECODER_READ_ABORT)
-               decoder->state = FLAC__STREAM_DECODER_ABORTED;
-       return status == FLAC__STREAM_DECODER_READ_CONTINUE;
+
+       if(decoder->private_->eof_callback && decoder->private_->eof_callback(decoder, decoder->private_->client_data)) {
+               *bytes = 0;
+               decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
+               return false;
+       }
+       else if(*bytes > 0) {
+               /* While seeking, it is possible for our seek to land in the
+                * middle of audio data that looks exactly like a frame header
+                * from a future version of an encoder.  When that happens, our
+                * error callback will get an
+                * FLAC__STREAM_DECODER_UNPARSEABLE_STREAM and increment its
+                * unparseable_frame_count.  But there is a remote possibility
+                * that it is properly synced at such a "future-codec frame",
+                * so to make sure, we wait to see many "unparseable" errors in
+                * a row before bailing out.
+                */
+               if(decoder->private_->is_seeking && decoder->private_->unparseable_frame_count > 20) {
+                       decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
+                       return false;
+               }
+               else {
+                       const FLAC__StreamDecoderReadStatus status = decoder->private_->read_callback(decoder, buffer, bytes, decoder->private_->client_data);
+                       if(status == FLAC__STREAM_DECODER_READ_STATUS_ABORT) {
+                               decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
+                               return false;
+                       }
+                       else if(*bytes == 0) {
+                               if(status == FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM || (decoder->private_->eof_callback && decoder->private_->eof_callback(decoder, decoder->private_->client_data))) {
+                                       decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
+                                       return false;
+                               }
+                               else
+                                       return true;
+                       }
+                       else
+                               return true;
+               }
+       }
+       else {
+               /* abort to avoid a deadlock */
+               decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
+               return false;
+       }
+}
+
+FLAC__StreamDecoderWriteStatus write_audio_frame_to_client_(FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[])
+{
+       if(decoder->private_->is_seeking) {
+               FLAC__uint64 this_frame_sample = frame->header.number.sample_number;
+               FLAC__uint64 next_frame_sample = this_frame_sample + (FLAC__uint64)frame->header.blocksize;
+               FLAC__uint64 target_sample = decoder->private_->target_sample;
+
+               FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
+
+               decoder->private_->last_frame = *frame; /* save the frame */
+               if(this_frame_sample <= target_sample && target_sample < next_frame_sample) { /* we hit our target frame */
+                       unsigned delta = (unsigned)(target_sample - this_frame_sample);
+                       /* kick out of seek mode */
+                       decoder->private_->is_seeking = false;
+                       /* shift out the samples before target_sample */
+                       if(delta > 0) {
+                               unsigned channel;
+                               const FLAC__int32 *newbuffer[FLAC__MAX_CHANNELS];
+                               for(channel = 0; channel < frame->header.channels; channel++)
+                                       newbuffer[channel] = buffer[channel] + delta;
+                               decoder->private_->last_frame.header.blocksize -= delta;
+                               decoder->private_->last_frame.header.number.sample_number += (FLAC__uint64)delta;
+                               /* write the relevant samples */
+                               return decoder->private_->write_callback(decoder, &decoder->private_->last_frame, newbuffer, decoder->private_->client_data);
+                       }
+                       else {
+                               /* write the relevant samples */
+                               return decoder->private_->write_callback(decoder, frame, buffer, decoder->private_->client_data);
+                       }
+               }
+               else {
+                       return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
+               }
+       }
+       else {
+               /*
+                * If we never got STREAMINFO, turn off MD5 checking to save
+                * cycles since we don't have a sum to compare to anyway
+                */
+               if(!decoder->private_->has_stream_info)
+                       decoder->private_->do_md5_checking = false;
+               if(decoder->private_->do_md5_checking) {
+                       if(!FLAC__MD5Accumulate(&decoder->private_->md5context, buffer, frame->header.channels, frame->header.blocksize, (frame->header.bits_per_sample+7) / 8))
+                               return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
+               }
+               return decoder->private_->write_callback(decoder, frame, buffer, decoder->private_->client_data);
+       }
+}
+
+void send_error_to_client_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status)
+{
+       if(!decoder->private_->is_seeking)
+               decoder->private_->error_callback(decoder, status, decoder->private_->client_data);
+       else if(status == FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM)
+               decoder->private_->unparseable_frame_count++;
+}
+
+FLAC__bool seek_to_absolute_sample_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample)
+{
+       FLAC__uint64 first_frame_offset = decoder->private_->first_frame_offset, lower_bound, upper_bound;
+       FLAC__int64 pos = -1, last_pos = -1;
+       int i, lower_seek_point = -1, upper_seek_point = -1;
+       unsigned approx_bytes_per_frame;
+       FLAC__uint64 last_frame_sample = FLAC__U64L(0xffffffffffffffff);
+       FLAC__bool needs_seek;
+       const FLAC__uint64 total_samples = FLAC__stream_decoder_get_total_samples(decoder);
+       const unsigned min_blocksize = decoder->private_->stream_info.data.stream_info.min_blocksize;
+       const unsigned max_blocksize = decoder->private_->stream_info.data.stream_info.max_blocksize;
+       const unsigned max_framesize = decoder->private_->stream_info.data.stream_info.max_framesize;
+       const unsigned channels = FLAC__stream_decoder_get_channels(decoder);
+       const unsigned bps = FLAC__stream_decoder_get_bits_per_sample(decoder);
+       const FLAC__StreamMetadata_SeekTable *seek_table = decoder->private_->has_seek_table? &decoder->private_->seek_table.data.seek_table : 0;
+
+       /* we are just guessing here, but we want to guess high, not low */
+       if(max_framesize > 0)
+               approx_bytes_per_frame = max_framesize;
+
+       /*
+        * Check if it's a known fixed-blocksize stream.  Note that though
+        * the spec doesn't allow zeroes in the STREAMINFO block, we may
+        * never get a STREAMINFO block when decoding so the value of
+        * min_blocksize might be zero.
+        */
+       else if(min_blocksize == max_blocksize && min_blocksize > 0) {
+               /* note there are no () around 'bps/8' to keep precision up since it's an integer calulation */
+               approx_bytes_per_frame = min_blocksize * channels * bps/8 + 64;
+       }
+       else
+               approx_bytes_per_frame = 4608 * channels * bps/8 + 64;
+
+       /*
+        * First, we set an upper and lower bound on where in the
+        * stream we will search.  For now we assume the worst case
+        * scenario, which is our best guess at the beginning of
+        * the first and last frames.
+        */
+       lower_bound = first_frame_offset;
+
+       /* calc the upper_bound, beyond which we never want to seek */
+       if(max_framesize > 0)
+               upper_bound = stream_length - (max_framesize + 128 + 2); /* 128 for a possible ID3V1 tag, 2 for indexing differences */
+       else
+               upper_bound = stream_length - ((channels * bps * FLAC__MAX_BLOCK_SIZE) / 8 + 128 + 2);
+
+       /*
+        * Now we refine the bounds if we have a seektable with
+        * suitable points.  Note that according to the spec they
+        * must be ordered by ascending sample number.
+        */
+       if(seek_table) {
+               /* find the closest seek point <= target_sample, if it exists */
+               for(i = (int)seek_table->num_points - 1; i >= 0; i--) {
+                       if(seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER && seek_table->points[i].sample_number <= target_sample)
+                               break;
+               }
+               if(i >= 0) { /* i.e. we found a suitable seek point... */
+                       lower_bound = first_frame_offset + seek_table->points[i].stream_offset;
+                       lower_seek_point = i;
+               }
+
+               /* find the closest seek point > target_sample, if it exists */
+               for(i = 0; i < (int)seek_table->num_points; i++) {
+                       if(seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER && seek_table->points[i].sample_number > target_sample)
+                               break;
+               }
+               if(i < (int)seek_table->num_points) { /* i.e. we found a suitable seek point... */
+                       upper_bound = first_frame_offset + seek_table->points[i].stream_offset;
+                       upper_seek_point = i;
+               }
+       }
+
+       /*
+        * Now guess at where within those bounds our target
+        * sample will be.
+        */
+       if(seek_table && lower_seek_point >= 0) {
+               /* first see if our sample is within a few frames of the lower seekpoint */
+               if(seek_table->points[lower_seek_point].sample_number <= target_sample && target_sample < seek_table->points[lower_seek_point].sample_number + (seek_table->points[lower_seek_point].frame_samples * 4)) {
+                       pos = (FLAC__int64)lower_bound;
+               }
+               else if(upper_seek_point >= 0) {
+                       const FLAC__uint64 target_offset = target_sample - seek_table->points[lower_seek_point].sample_number;
+                       const FLAC__uint64 range_samples = seek_table->points[upper_seek_point].sample_number - seek_table->points[lower_seek_point].sample_number;
+                       const FLAC__uint64 range_bytes = (upper_bound>lower_bound? upper_bound - lower_bound - 1 : 0);
+#ifndef FLAC__INTEGER_ONLY_LIBRARY
+#if defined _MSC_VER || defined __MINGW32__
+                       /* with MSVC you have to spoon feed it the casting */
+                       pos = (FLAC__int64)lower_bound + (FLAC__int64)(((FLAC__double)(FLAC__int64)target_offset / (FLAC__double)(FLAC__int64)range_samples) * (FLAC__double)(FLAC__int64)(range_bytes-1)) - approx_bytes_per_frame;
+#else
+                       pos = (FLAC__int64)lower_bound + (FLAC__int64)(((FLAC__double)target_offset / (FLAC__double)range_samples) * (FLAC__double)range_bytes) - approx_bytes_per_frame;
+#endif
+#else
+                       /* a little less accurate: */
+                       if (range_bytes <= 0xffffffff)
+                               pos = (FLAC__int64)lower_bound + (FLAC__int64)((target_offset * range_bytes) / range_samples) - approx_bytes_per_frame;
+                       else /* @@@ WATCHOUT, ~2TB limit */
+                               pos = (FLAC__int64)lower_bound + (FLAC__int64)(((target_offset>>8) * (range_bytes>>8)) / (range_samples>>16)) - approx_bytes_per_frame;
+#endif
+               }
+       }
+
+       /*
+        * If there's no seek table, we need to use the metadata (if we
+        * have it) and the filelength to estimate the position of the
+        * frame with the correct sample.
+        */
+       if(pos < 0 && total_samples > 0) {
+               /*
+                * For max accuracy we should be using
+                * (stream_length-first_frame_offset-1) in the divisor, but the
+                * difference is trivial and (stream_length-first_frame_offset)
+                * has no chance of underflow.
+                */
+#ifndef FLAC__INTEGER_ONLY_LIBRARY
+#if defined _MSC_VER || defined __MINGW32__
+               /* with VC++ you have to spoon feed it the casting */
+               pos = (FLAC__int64)first_frame_offset + (FLAC__int64)(((FLAC__double)(FLAC__int64)target_sample / (FLAC__double)(FLAC__int64)total_samples) * (FLAC__double)(FLAC__int64)(stream_length-first_frame_offset)) - approx_bytes_per_frame;
+#else
+               pos = (FLAC__int64)first_frame_offset + (FLAC__int64)(((FLAC__double)target_sample / (FLAC__double)total_samples) * (FLAC__double)(stream_length-first_frame_offset)) - approx_bytes_per_frame;
+#endif
+#else
+               /* a little less accurate: */
+               if (stream_length < 0xffffffff)
+                       pos = (FLAC__int64)first_frame_offset + (FLAC__int64)((target_sample * (stream_length-first_frame_offset)) / total_samples) - approx_bytes_per_frame;
+               else /* @@@ WATCHOUT, ~2TB limit */
+                       pos = (FLAC__int64)first_frame_offset + (FLAC__int64)(((target_sample>>8) * ((stream_length-first_frame_offset)>>8)) / (total_samples>>16)) - approx_bytes_per_frame;
+#endif
+       }
+
+       /*
+        * If there's no seek table and total_samples is unknown, we
+        * don't even bother trying to figure out a target, we just use
+        * our current position.
+        */
+       if(pos < 0) {
+               FLAC__uint64 upos;
+               if(decoder->private_->tell_callback(decoder, &upos, decoder->private_->client_data) != FLAC__STREAM_DECODER_TELL_STATUS_OK) {
+                       decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
+                       return false;
+               }
+               pos = (FLAC__int64)upos;
+               needs_seek = false;
+       }
+       else
+               needs_seek = true;
+
+       /* clip the position to the bounds, lower bound takes precedence */
+       if(pos >= (FLAC__int64)upper_bound) {
+               pos = (FLAC__int64)upper_bound-1;
+               needs_seek = true;
+       }
+       if(pos < (FLAC__int64)lower_bound) {
+               pos = (FLAC__int64)lower_bound;
+               needs_seek = true;
+       }
+
+       decoder->private_->target_sample = target_sample;
+       while(1) {
+               if(needs_seek) {
+                       if(decoder->private_->seek_callback(decoder, (FLAC__uint64)pos, decoder->private_->client_data) != FLAC__STREAM_DECODER_SEEK_STATUS_OK) {
+                               decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
+                               return false;
+                       }
+                       if(!FLAC__stream_decoder_flush(decoder)) {
+                               /* above call sets the state for us */
+                               return false;
+                       }
+               }
+               /* Now we need to get a frame.  First we need to reset our
+                * unparseable_frame_count; if we get too many unparseable
+                * frames in a row, the read callback will return
+                * FLAC__STREAM_DECODER_READ_STATUS_ABORT, causing
+                * FLAC__stream_decoder_process_single() to return false.
+                */
+               decoder->private_->unparseable_frame_count = 0;
+               if(!FLAC__stream_decoder_process_single(decoder)) {
+                       decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
+                       return false;
+               }
+               /* our write callback will change the state when it gets to the target frame */
+               /* 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 */
+#if 0
+               /*@@@@@@ used to be the following; not clear if the check for end of stream is needed anymore */
+               if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_SEEKING && decoder->protected_->state != FLAC__STREAM_DECODER_END_OF_STREAM)
+                       break;
+#endif
+               if(!decoder->private_->is_seeking) {
+                       break;
+               }
+               else { /* we need to narrow the search */
+                       const FLAC__uint64 this_frame_sample = decoder->private_->last_frame.header.number.sample_number;
+                       FLAC__ASSERT(decoder->private_->last_frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
+                       if(this_frame_sample == last_frame_sample && pos < last_pos) {
+                               /* our last move backwards wasn't big enough, double it */
+                               pos -= (last_pos - pos);
+                               needs_seek = true;
+                       }
+                       else {
+                               if(target_sample < this_frame_sample) {
+                                       last_pos = pos;
+                                       approx_bytes_per_frame = decoder->private_->last_frame.header.blocksize * channels * bps/8 + 64;
+                                       pos -= approx_bytes_per_frame;
+                                       needs_seek = true;
+                               }
+                               else { /* target_sample >= this_frame_sample + this frame's blocksize */
+                                       FLAC__uint64 upos;
+                                       if(decoder->private_->tell_callback(decoder, &upos, decoder->private_->client_data) != FLAC__STREAM_DECODER_TELL_STATUS_OK) {
+                                               decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
+                                               return false;
+                                       }
+                                       last_pos = pos;
+                                       pos = (FLAC__int64)upos;
+                                       pos -= FLAC__stream_decoder_get_input_bytes_unconsumed(decoder);
+                                       needs_seek = false;
+                                       /*
+                                        * if we haven't hit the target frame yet and our position hasn't changed,
+                                        * it means we're at the end of the stream and the seek target does not exist.
+                                        */
+                                       if(last_pos == pos) {
+                                               decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
+                                               return false;
+                                       }
+                               }
+                       }
+                       if(pos < (FLAC__int64)lower_bound)
+                               pos = (FLAC__int64)lower_bound;
+                       last_frame_sample = this_frame_sample;
+               }
+       }
+
+       return true;
+}
+
+FLAC__StreamDecoderReadStatus file_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
+{
+       (void)client_data;
+
+       if(*bytes > 0) {
+               *bytes = (unsigned)fread(buffer, sizeof(FLAC__byte), *bytes, decoder->private_->file);
+               if(ferror(decoder->private_->file))
+                       return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
+               else if(*bytes == 0)
+                       return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
+               else
+                       return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
+       }
+       else
+               return FLAC__STREAM_DECODER_READ_STATUS_ABORT; /* abort to avoid a deadlock */
+}
+
+FLAC__StreamDecoderSeekStatus file_seek_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data)
+{
+       (void)client_data;
+
+       if(decoder->private_->file == stdin)
+               return FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED;
+       else if(fseeko(decoder->private_->file, (off_t)absolute_byte_offset, SEEK_SET) < 0)
+               return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR;
+       else
+               return FLAC__STREAM_DECODER_SEEK_STATUS_OK;
+}
+
+FLAC__StreamDecoderTellStatus file_tell_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
+{
+       off_t pos;
+       (void)client_data;
+
+       if(decoder->private_->file == stdin) /*@@@@@@ may work for stdin */
+               return FLAC__STREAM_DECODER_TELL_STATUS_UNSUPPORTED;
+       else if((pos = ftello(decoder->private_->file)) < 0)
+               return FLAC__STREAM_DECODER_TELL_STATUS_ERROR;
+       else {
+               *absolute_byte_offset = (FLAC__uint64)pos;
+               return FLAC__STREAM_DECODER_TELL_STATUS_OK;
+       }
+}
+
+FLAC__StreamDecoderLengthStatus file_length_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data)
+{
+       struct stat filestats;
+       (void)client_data;
+
+       if(decoder->private_->file == stdin)
+               return FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED;
+       else if(fstat(fileno(decoder->private_->file), &filestats) != 0)
+               return FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR;
+       else {
+               *stream_length = (FLAC__uint64)filestats.st_size;
+               return FLAC__STREAM_DECODER_LENGTH_STATUS_OK;
+       }
+}
+
+FLAC__bool file_eof_callback_(const FLAC__StreamDecoder *decoder, void *client_data)
+{
+       (void)client_data;
+
+       if(decoder->private_->file == stdin) /*@@@@@@ feof() may work for stdin */
+               return false;
+       return feof(decoder->private_->file)? true : false;
 }