change back to unix-format line endings
authorJosh Coalson <jcoalson@users.sourceforce.net>
Fri, 9 Nov 2001 19:23:42 +0000 (19:23 +0000)
committerJosh Coalson <jcoalson@users.sourceforce.net>
Fri, 9 Nov 2001 19:23:42 +0000 (19:23 +0000)
src/libFLAC/file_decoder.c
src/libFLAC/seekable_stream_decoder.c

index e0cf07e..194dcd4 100644 (file)
-/* libFLAC - Free Lossless Audio Codec library\r
- * Copyright (C) 2000,2001  Josh Coalson\r
- *\r
- * This library is free software; you can redistribute it and/or\r
- * modify it under the terms of the GNU Library General Public\r
- * License as published by the Free Software Foundation; either\r
- * version 2 of the License, or (at your option) any later version.\r
- *\r
- * This library is distributed in the hope that it will be useful,\r
- * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
- * Library General Public License for more details.\r
- *\r
- * You should have received a copy of the GNU Library General Public\r
- * License along with this library; if not, write to the\r
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,\r
- * Boston, MA  02111-1307, USA.\r
- */\r
-\r
-#include <stdio.h>\r
-#include <stdlib.h> /* for malloc() */\r
-#include <string.h> /* for strcmp() */\r
-#include <sys/stat.h> /* for stat() */\r
-#include "FLAC/assert.h"\r
-#include "protected/file_decoder.h"\r
-#include "protected/seekable_stream_decoder.h"\r
-#include "private/md5.h"\r
-\r
-/***********************************************************************\r
- *\r
- * Private class method prototypes\r
- *\r
- ***********************************************************************/\r
-\r
-static FLAC__SeekableStreamDecoderReadStatus read_callback_(const FLAC__SeekableStreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data);\r
-static FLAC__SeekableStreamDecoderSeekStatus seek_callback_(const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data);\r
-static FLAC__SeekableStreamDecoderTellStatus tell_callback_(const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data);\r
-static FLAC__SeekableStreamDecoderLengthStatus length_callback_(const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data);\r
-static FLAC__bool eof_callback_(const FLAC__SeekableStreamDecoder *decoder, void *client_data);\r
-static FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__SeekableStreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 *buffer[], void *client_data);\r
-static void metadata_callback_(const FLAC__SeekableStreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data);\r
-static void error_callback_(const FLAC__SeekableStreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);\r
-\r
-/***********************************************************************\r
- *\r
- * Private class data\r
- *\r
- ***********************************************************************/\r
-\r
-typedef struct FLAC__FileDecoderPrivate {\r
-       FLAC__StreamDecoderWriteStatus (*write_callback)(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 *buffer[], void *client_data);\r
-       void (*metadata_callback)(const FLAC__FileDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data);\r
-       void (*error_callback)(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);\r
-       void *client_data;\r
-       FILE *file;\r
-       char *filename; /* == NULL if stdin */\r
-       FLAC__SeekableStreamDecoder *seekable_stream_decoder;\r
-       struct {\r
-               FLAC__bool md5_checking;\r
-       } init_values_for_superclass;\r
-} FLAC__FileDecoderPrivate;\r
-\r
-/***********************************************************************\r
- *\r
- * Public static class data\r
- *\r
- ***********************************************************************/\r
-\r
-const char *FLAC__FileDecoderStateString[] = {\r
-       "FLAC__FILE_DECODER_OK",\r
-       "FLAC__FILE_DECODER_SEEKING",\r
-       "FLAC__FILE_DECODER_END_OF_FILE",\r
-       "FLAC__FILE_DECODER_ERROR_OPENING_FILE",\r
-       "FLAC__FILE_DECODER_MEMORY_ALLOCATION_ERROR",\r
-       "FLAC__FILE_DECODER_SEEK_ERROR",\r
-       "FLAC__FILE_DECODER_STREAM_ERROR",\r
-       "FLAC__FILE_DECODER_STREAM_DECODER_ERROR",\r
-       "FLAC__FILE_DECODER_ALREADY_INITIALIZED",\r
-       "FLAC__FILE_DECODER_INVALID_CALLBACK",\r
-       "FLAC__FILE_DECODER_UNINITIALIZED"\r
-};\r
-\r
-/***********************************************************************\r
- *\r
- * Class constructor/destructor\r
- *\r
- ***********************************************************************/\r
-\r
-FLAC__FileDecoder *FLAC__file_decoder_new()\r
-{\r
-       FLAC__FileDecoder *decoder;\r
-\r
-       FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */\r
-\r
-       decoder = (FLAC__FileDecoder*)malloc(sizeof(FLAC__FileDecoder));\r
-       if(decoder == 0) {\r
-               return 0;\r
-       }\r
-       decoder->protected_ = (FLAC__FileDecoderProtected*)malloc(sizeof(FLAC__FileDecoderProtected));\r
-       if(decoder->protected_ == 0) {\r
-               free(decoder);\r
-               return 0;\r
-       }\r
-       decoder->private_ = (FLAC__FileDecoderPrivate*)malloc(sizeof(FLAC__FileDecoderPrivate));\r
-       if(decoder->private_ == 0) {\r
-               free(decoder->protected_);\r
-               free(decoder);\r
-               return 0;\r
-       }\r
-\r
-       decoder->protected_->state = FLAC__FILE_DECODER_UNINITIALIZED;\r
-\r
-       decoder->private_->filename = 0;\r
-       decoder->private_->write_callback = 0;\r
-       decoder->private_->metadata_callback = 0;\r
-       decoder->private_->error_callback = 0;\r
-       decoder->private_->client_data = 0;\r
-       decoder->private_->init_values_for_superclass.md5_checking = false;\r
-\r
-       return decoder;\r
-}\r
-\r
-void FLAC__file_decoder_delete(FLAC__FileDecoder *decoder)\r
-{\r
-       FLAC__ASSERT(decoder != 0);\r
-       FLAC__ASSERT(decoder->protected_ != 0);\r
-       FLAC__ASSERT(decoder->private_ != 0);\r
-\r
-       free(decoder->private_);\r
-       free(decoder->protected_);\r
-       free(decoder);\r
-}\r
-\r
-/***********************************************************************\r
- *\r
- * Public class methods\r
- *\r
- ***********************************************************************/\r
-\r
-FLAC__FileDecoderState FLAC__file_decoder_init(FLAC__FileDecoder *decoder)\r
-{\r
-       FLAC__ASSERT(decoder != 0);\r
-\r
-       if(decoder->protected_->state != FLAC__FILE_DECODER_UNINITIALIZED)\r
-               return decoder->protected_->state = FLAC__FILE_DECODER_ALREADY_INITIALIZED;\r
-\r
-       decoder->protected_->state = FLAC__FILE_DECODER_OK;\r
-\r
-       if(0 == decoder->private_->write_callback || 0 == decoder->private_->metadata_callback || 0 == decoder->private_->error_callback)\r
-               return decoder->protected_->state = FLAC__FILE_DECODER_INVALID_CALLBACK;\r
-\r
-       decoder->private_->file = 0;\r
-       decoder->private_->seekable_stream_decoder = 0;\r
-\r
-       if(0 == decoder->private_->filename)\r
-               decoder->private_->file = stdin;\r
-       else\r
-               decoder->private_->file = fopen(decoder->private_->filename, "rb");\r
-\r
-       if(decoder->private_->file == 0)\r
-               return decoder->protected_->state = FLAC__FILE_DECODER_ERROR_OPENING_FILE;\r
-\r
-       decoder->private_->seekable_stream_decoder = FLAC__seekable_stream_decoder_new();\r
-\r
-       if(0 == decoder->private_->seekable_stream_decoder)\r
-               return decoder->protected_->state = FLAC__FILE_DECODER_MEMORY_ALLOCATION_ERROR;\r
-\r
-       FLAC__seekable_stream_decoder_set_read_callback(decoder->private_->seekable_stream_decoder, read_callback_);\r
-       FLAC__seekable_stream_decoder_set_seek_callback(decoder->private_->seekable_stream_decoder, seek_callback_);\r
-       FLAC__seekable_stream_decoder_set_tell_callback(decoder->private_->seekable_stream_decoder, tell_callback_);\r
-       FLAC__seekable_stream_decoder_set_length_callback(decoder->private_->seekable_stream_decoder, length_callback_);\r
-       FLAC__seekable_stream_decoder_set_eof_callback(decoder->private_->seekable_stream_decoder, eof_callback_);\r
-       FLAC__seekable_stream_decoder_set_write_callback(decoder->private_->seekable_stream_decoder, write_callback_);\r
-       FLAC__seekable_stream_decoder_set_metadata_callback(decoder->private_->seekable_stream_decoder, metadata_callback_);\r
-       FLAC__seekable_stream_decoder_set_error_callback(decoder->private_->seekable_stream_decoder, error_callback_);\r
-       FLAC__seekable_stream_decoder_set_client_data(decoder->private_->seekable_stream_decoder, decoder);\r
-\r
-       /*\r
-        * Unfortunately, because of the "_new() ... _set_() ... _init()" order of\r
-        * decoder initialization, settings that are 'inherited' from the superclass\r
-        * have to be passed up this way, because the superclass has not even been\r
-        * created yet when the value is set in the subclass.\r
-        */\r
-       (void)FLAC__seekable_stream_decoder_set_md5_checking(decoder->private_->seekable_stream_decoder, decoder->private_->init_values_for_superclass.md5_checking);\r
-\r
-       if(FLAC__seekable_stream_decoder_init(decoder->private_->seekable_stream_decoder) != FLAC__SEEKABLE_STREAM_DECODER_OK)\r
-               return decoder->protected_->state = FLAC__FILE_DECODER_STREAM_DECODER_ERROR; /*@@@ change this to FLAC__FILE_DECODER_SEEKABLE_STREAM_ERROR in next minor-revision */\r
-\r
-       return decoder->protected_->state;\r
-}\r
-\r
-FLAC__bool FLAC__file_decoder_finish(FLAC__FileDecoder *decoder)\r
-{\r
-       FLAC__bool ok = true;\r
-\r
-       FLAC__ASSERT(decoder != 0);\r
-       if(decoder->protected_->state == FLAC__FILE_DECODER_UNINITIALIZED)\r
-               return true;\r
-       if(decoder->private_->file != 0 && decoder->private_->file != stdin)\r
-               fclose(decoder->private_->file);\r
-       if(0 != decoder->private_->filename)\r
-               free(decoder->private_->filename);\r
-       if(decoder->private_->seekable_stream_decoder != 0) {\r
-               ok = FLAC__seekable_stream_decoder_finish(decoder->private_->seekable_stream_decoder);\r
-               FLAC__seekable_stream_decoder_delete(decoder->private_->seekable_stream_decoder);\r
-       }\r
-       decoder->protected_->state = FLAC__FILE_DECODER_UNINITIALIZED;\r
-       return ok;\r
-}\r
-\r
-FLAC__bool FLAC__file_decoder_set_md5_checking(const FLAC__FileDecoder *decoder, FLAC__bool value)\r
-{\r
-       if(decoder->protected_->state != FLAC__FILE_DECODER_UNINITIALIZED)\r
-               return false;\r
-       decoder->private_->init_values_for_superclass.md5_checking = value;\r
-       return true;\r
-}\r
-\r
-FLAC__bool FLAC__file_decoder_set_filename(const FLAC__FileDecoder *decoder, const char *value)\r
-{\r
-       FLAC__ASSERT(value != 0);\r
-       if(decoder->protected_->state != FLAC__FILE_DECODER_UNINITIALIZED)\r
-               return false;\r
-       if(0 != decoder->private_->filename)\r
-               free(decoder->private_->filename);\r
-       if(0 == strcmp(value, "-")) {\r
-               decoder->private_->filename = 0;\r
-       }\r
-       else  {\r
-               if(0 == (decoder->private_->filename = (char*)malloc(strlen(value)+1))) {\r
-                       decoder->protected_->state = FLAC__FILE_DECODER_MEMORY_ALLOCATION_ERROR;\r
-                       return false;\r
-               }\r
-               strcpy(decoder->private_->filename, value);\r
-       }\r
-       return true;\r
-}\r
-\r
-FLAC__bool FLAC__file_decoder_set_write_callback(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderWriteStatus (*value)(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 *buffer[], void *client_data))\r
-{\r
-       if(decoder->protected_->state != FLAC__FILE_DECODER_UNINITIALIZED)\r
-               return false;\r
-       decoder->private_->write_callback = value;\r
-       return true;\r
-}\r
-\r
-FLAC__bool FLAC__file_decoder_set_metadata_callback(const FLAC__FileDecoder *decoder, void (*value)(const FLAC__FileDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data))\r
-{\r
-       if(decoder->protected_->state != FLAC__FILE_DECODER_UNINITIALIZED)\r
-               return false;\r
-       decoder->private_->metadata_callback = value;\r
-       return true;\r
-}\r
-\r
-FLAC__bool FLAC__file_decoder_set_error_callback(const FLAC__FileDecoder *decoder, void (*value)(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data))\r
-{\r
-       if(decoder->protected_->state != FLAC__FILE_DECODER_UNINITIALIZED)\r
-               return false;\r
-       decoder->private_->error_callback = value;\r
-       return true;\r
-}\r
-\r
-FLAC__bool FLAC__file_decoder_set_client_data(const FLAC__FileDecoder *decoder, void *value)\r
-{\r
-       if(decoder->protected_->state != FLAC__FILE_DECODER_UNINITIALIZED)\r
-               return false;\r
-       decoder->private_->client_data = value;\r
-       return true;\r
-}\r
-\r
-FLAC__FileDecoderState FLAC__file_decoder_get_state(const FLAC__FileDecoder *decoder)\r
-{\r
-       return decoder->protected_->state;\r
-}\r
-\r
-FLAC__bool FLAC__file_decoder_get_md5_checking(const FLAC__FileDecoder *decoder)\r
-{\r
-       return FLAC__seekable_stream_decoder_get_md5_checking(decoder->private_->seekable_stream_decoder);\r
-}\r
-\r
-FLAC__bool FLAC__file_decoder_process_whole_file(FLAC__FileDecoder *decoder)\r
-{\r
-       FLAC__bool ret;\r
-       FLAC__ASSERT(decoder != 0);\r
-\r
-       if(decoder->private_->seekable_stream_decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM)\r
-               decoder->protected_->state = FLAC__FILE_DECODER_END_OF_FILE;\r
-\r
-       if(decoder->protected_->state == FLAC__FILE_DECODER_END_OF_FILE)\r
-               return true;\r
-\r
-       FLAC__ASSERT(decoder->protected_->state == FLAC__FILE_DECODER_OK);\r
-\r
-       ret = FLAC__seekable_stream_decoder_process_whole_stream(decoder->private_->seekable_stream_decoder);\r
-       if(!ret)\r
-               decoder->protected_->state = FLAC__FILE_DECODER_SEEKABLE_STREAM_ERROR;\r
-\r
-       return ret;\r
-}\r
-\r
-FLAC__bool FLAC__file_decoder_process_metadata(FLAC__FileDecoder *decoder)\r
-{\r
-       FLAC__bool ret;\r
-       FLAC__ASSERT(decoder != 0);\r
-\r
-       if(decoder->private_->seekable_stream_decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM)\r
-               decoder->protected_->state = FLAC__FILE_DECODER_END_OF_FILE;\r
-\r
-       if(decoder->protected_->state == FLAC__FILE_DECODER_END_OF_FILE)\r
-               return true;\r
-\r
-       FLAC__ASSERT(decoder->protected_->state == FLAC__FILE_DECODER_OK);\r
-\r
-       ret = FLAC__seekable_stream_decoder_process_metadata(decoder->private_->seekable_stream_decoder);\r
-       if(!ret)\r
-               decoder->protected_->state = FLAC__FILE_DECODER_SEEKABLE_STREAM_ERROR;\r
-\r
-       return ret;\r
-}\r
-\r
-FLAC__bool FLAC__file_decoder_process_one_frame(FLAC__FileDecoder *decoder)\r
-{\r
-       FLAC__bool ret;\r
-       FLAC__ASSERT(decoder != 0);\r
-\r
-       if(decoder->private_->seekable_stream_decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM)\r
-               decoder->protected_->state = FLAC__FILE_DECODER_END_OF_FILE;\r
-\r
-       if(decoder->protected_->state == FLAC__FILE_DECODER_END_OF_FILE)\r
-               return true;\r
-\r
-       FLAC__ASSERT(decoder->protected_->state == FLAC__FILE_DECODER_OK);\r
-\r
-       ret = FLAC__seekable_stream_decoder_process_one_frame(decoder->private_->seekable_stream_decoder);\r
-       if(!ret)\r
-               decoder->protected_->state = FLAC__FILE_DECODER_SEEKABLE_STREAM_ERROR;\r
-\r
-       return ret;\r
-}\r
-\r
-FLAC__bool FLAC__file_decoder_process_remaining_frames(FLAC__FileDecoder *decoder)\r
-{\r
-       FLAC__bool ret;\r
-       FLAC__ASSERT(decoder != 0);\r
-\r
-       if(decoder->private_->seekable_stream_decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM)\r
-               decoder->protected_->state = FLAC__FILE_DECODER_END_OF_FILE;\r
-\r
-       if(decoder->protected_->state == FLAC__FILE_DECODER_END_OF_FILE)\r
-               return true;\r
-\r
-       FLAC__ASSERT(decoder->protected_->state == FLAC__FILE_DECODER_OK);\r
-\r
-       ret = FLAC__seekable_stream_decoder_process_remaining_frames(decoder->private_->seekable_stream_decoder);\r
-       if(!ret)\r
-               decoder->protected_->state = FLAC__FILE_DECODER_SEEKABLE_STREAM_ERROR;\r
-\r
-       return ret;\r
-}\r
-\r
-/***********************************************************************\r
- *\r
- * Private class methods\r
- *\r
- ***********************************************************************/\r
-\r
-FLAC__bool FLAC__file_decoder_seek_absolute(FLAC__FileDecoder *decoder, FLAC__uint64 sample)\r
-{\r
-       FLAC__ASSERT(decoder != 0);\r
-       FLAC__ASSERT(decoder->protected_->state == FLAC__FILE_DECODER_OK);\r
-\r
-       if(decoder->private_->filename == 0) { /* means the file is stdin... */\r
-               decoder->protected_->state = FLAC__FILE_DECODER_SEEK_ERROR;\r
-               return false;\r
-       }\r
-\r
-       if(!FLAC__seekable_stream_decoder_seek_absolute(decoder->private_->seekable_stream_decoder, sample)) {\r
-               decoder->protected_->state = FLAC__FILE_DECODER_SEEK_ERROR;\r
-               return false;\r
-       }\r
-       else\r
-               return true;\r
-}\r
-\r
-FLAC__SeekableStreamDecoderReadStatus read_callback_(const FLAC__SeekableStreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)\r
-{\r
-       FLAC__FileDecoder *file_decoder = (FLAC__FileDecoder *)client_data;\r
-       (void)decoder;\r
-\r
-       if(*bytes > 0) {\r
-               size_t bytes_read = fread(buffer, sizeof(FLAC__byte), *bytes, file_decoder->private_->file);\r
-               if(bytes_read == 0 && !feof(file_decoder->private_->file)) {\r
-                       return FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_ERROR;\r
-               }\r
-               else {\r
-                       *bytes = (unsigned)bytes_read;\r
-                       return FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_OK;\r
-               }\r
-       }\r
-       else\r
-               return FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_ERROR; /* abort to avoid a deadlock */\r
-}\r
-\r
-FLAC__SeekableStreamDecoderSeekStatus seek_callback_(const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data)\r
-{\r
-       FLAC__FileDecoder *file_decoder = (FLAC__FileDecoder *)client_data;\r
-       (void)decoder;\r
-\r
-       if(fseek(file_decoder->private_->file, (long)absolute_byte_offset, SEEK_SET) < 0)\r
-               return FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_ERROR;\r
-       else\r
-               return FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_OK;\r
-}\r
-\r
-FLAC__SeekableStreamDecoderTellStatus tell_callback_(const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data)\r
-{\r
-       FLAC__FileDecoder *file_decoder = (FLAC__FileDecoder *)client_data;\r
-       long pos;\r
-       (void)decoder;\r
-\r
-       if((pos = ftell(file_decoder->private_->file)) < 0)\r
-               return FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_ERROR;\r
-       else {\r
-               *absolute_byte_offset = (FLAC__uint64)pos;\r
-               return FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_OK;\r
-       }\r
-}\r
-\r
-FLAC__SeekableStreamDecoderLengthStatus length_callback_(const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data)\r
-{\r
-       FLAC__FileDecoder *file_decoder = (FLAC__FileDecoder *)client_data;\r
-       struct stat filestats;\r
-       (void)decoder;\r
-\r
-       if(stat(file_decoder->private_->filename, &filestats) != 0)\r
-               return FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_ERROR;\r
-       else {\r
-               *stream_length = (FLAC__uint64)filestats.st_size;\r
-               return FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_OK;\r
-       }\r
-}\r
-\r
-FLAC__bool eof_callback_(const FLAC__SeekableStreamDecoder *decoder, void *client_data)\r
-{\r
-       FLAC__FileDecoder *file_decoder = (FLAC__FileDecoder *)client_data;\r
-       (void)decoder;\r
-\r
-       return feof(file_decoder->private_->file);\r
-}\r
-\r
-FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__SeekableStreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 *buffer[], void *client_data)\r
-{\r
-       FLAC__FileDecoder *file_decoder = (FLAC__FileDecoder *)client_data;\r
-       (void)decoder;\r
-\r
-       return file_decoder->private_->write_callback(file_decoder, frame, buffer, file_decoder->private_->client_data);\r
-}\r
-\r
-void metadata_callback_(const FLAC__SeekableStreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data)\r
-{\r
-       FLAC__FileDecoder *file_decoder = (FLAC__FileDecoder *)client_data;\r
-       (void)decoder;\r
-\r
-       file_decoder->private_->metadata_callback(file_decoder, metadata, file_decoder->private_->client_data);\r
-}\r
-\r
-void error_callback_(const FLAC__SeekableStreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)\r
-{\r
-       FLAC__FileDecoder *file_decoder = (FLAC__FileDecoder *)client_data;\r
-       (void)decoder;\r
-\r
-       file_decoder->private_->error_callback(file_decoder, status, file_decoder->private_->client_data);\r
-}\r
+/* libFLAC - Free Lossless Audio Codec library
+ * Copyright (C) 2000,2001  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.
+ *
+ * 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.
+ *
+ * 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.
+ */
+
+#include <stdio.h>
+#include <stdlib.h> /* for malloc() */
+#include <string.h> /* for strcmp() */
+#include <sys/stat.h> /* for stat() */
+#include "FLAC/assert.h"
+#include "protected/file_decoder.h"
+#include "protected/seekable_stream_decoder.h"
+#include "private/md5.h"
+
+/***********************************************************************
+ *
+ * Private class method prototypes
+ *
+ ***********************************************************************/
+
+static FLAC__SeekableStreamDecoderReadStatus read_callback_(const FLAC__SeekableStreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data);
+static FLAC__SeekableStreamDecoderSeekStatus seek_callback_(const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data);
+static FLAC__SeekableStreamDecoderTellStatus tell_callback_(const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data);
+static FLAC__SeekableStreamDecoderLengthStatus length_callback_(const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data);
+static FLAC__bool eof_callback_(const FLAC__SeekableStreamDecoder *decoder, void *client_data);
+static FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__SeekableStreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 *buffer[], void *client_data);
+static void metadata_callback_(const FLAC__SeekableStreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data);
+static void error_callback_(const FLAC__SeekableStreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
+
+/***********************************************************************
+ *
+ * Private class data
+ *
+ ***********************************************************************/
+
+typedef struct FLAC__FileDecoderPrivate {
+       FLAC__StreamDecoderWriteStatus (*write_callback)(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 *buffer[], void *client_data);
+       void (*metadata_callback)(const FLAC__FileDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data);
+       void (*error_callback)(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
+       void *client_data;
+       FILE *file;
+       char *filename; /* == NULL if stdin */
+       FLAC__SeekableStreamDecoder *seekable_stream_decoder;
+       struct {
+               FLAC__bool md5_checking;
+       } init_values_for_superclass;
+} FLAC__FileDecoderPrivate;
+
+/***********************************************************************
+ *
+ * Public static class data
+ *
+ ***********************************************************************/
+
+const char *FLAC__FileDecoderStateString[] = {
+       "FLAC__FILE_DECODER_OK",
+       "FLAC__FILE_DECODER_SEEKING",
+       "FLAC__FILE_DECODER_END_OF_FILE",
+       "FLAC__FILE_DECODER_ERROR_OPENING_FILE",
+       "FLAC__FILE_DECODER_MEMORY_ALLOCATION_ERROR",
+       "FLAC__FILE_DECODER_SEEK_ERROR",
+       "FLAC__FILE_DECODER_STREAM_ERROR",
+       "FLAC__FILE_DECODER_STREAM_DECODER_ERROR",
+       "FLAC__FILE_DECODER_ALREADY_INITIALIZED",
+       "FLAC__FILE_DECODER_INVALID_CALLBACK",
+       "FLAC__FILE_DECODER_UNINITIALIZED"
+};
+
+/***********************************************************************
+ *
+ * Class constructor/destructor
+ *
+ ***********************************************************************/
+
+FLAC__FileDecoder *FLAC__file_decoder_new()
+{
+       FLAC__FileDecoder *decoder;
+
+       FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
+
+       decoder = (FLAC__FileDecoder*)malloc(sizeof(FLAC__FileDecoder));
+       if(decoder == 0) {
+               return 0;
+       }
+       decoder->protected_ = (FLAC__FileDecoderProtected*)malloc(sizeof(FLAC__FileDecoderProtected));
+       if(decoder->protected_ == 0) {
+               free(decoder);
+               return 0;
+       }
+       decoder->private_ = (FLAC__FileDecoderPrivate*)malloc(sizeof(FLAC__FileDecoderPrivate));
+       if(decoder->private_ == 0) {
+               free(decoder->protected_);
+               free(decoder);
+               return 0;
+       }
+
+       decoder->protected_->state = FLAC__FILE_DECODER_UNINITIALIZED;
+
+       decoder->private_->filename = 0;
+       decoder->private_->write_callback = 0;
+       decoder->private_->metadata_callback = 0;
+       decoder->private_->error_callback = 0;
+       decoder->private_->client_data = 0;
+       decoder->private_->init_values_for_superclass.md5_checking = false;
+
+       return decoder;
+}
+
+void FLAC__file_decoder_delete(FLAC__FileDecoder *decoder)
+{
+       FLAC__ASSERT(decoder != 0);
+       FLAC__ASSERT(decoder->protected_ != 0);
+       FLAC__ASSERT(decoder->private_ != 0);
+
+       free(decoder->private_);
+       free(decoder->protected_);
+       free(decoder);
+}
+
+/***********************************************************************
+ *
+ * Public class methods
+ *
+ ***********************************************************************/
+
+FLAC__FileDecoderState FLAC__file_decoder_init(FLAC__FileDecoder *decoder)
+{
+       FLAC__ASSERT(decoder != 0);
+
+       if(decoder->protected_->state != FLAC__FILE_DECODER_UNINITIALIZED)
+               return decoder->protected_->state = FLAC__FILE_DECODER_ALREADY_INITIALIZED;
+
+       decoder->protected_->state = FLAC__FILE_DECODER_OK;
+
+       if(0 == decoder->private_->write_callback || 0 == decoder->private_->metadata_callback || 0 == decoder->private_->error_callback)
+               return decoder->protected_->state = FLAC__FILE_DECODER_INVALID_CALLBACK;
+
+       decoder->private_->file = 0;
+       decoder->private_->seekable_stream_decoder = 0;
+
+       if(0 == decoder->private_->filename)
+               decoder->private_->file = stdin;
+       else
+               decoder->private_->file = fopen(decoder->private_->filename, "rb");
+
+       if(decoder->private_->file == 0)
+               return decoder->protected_->state = FLAC__FILE_DECODER_ERROR_OPENING_FILE;
+
+       decoder->private_->seekable_stream_decoder = FLAC__seekable_stream_decoder_new();
+
+       if(0 == decoder->private_->seekable_stream_decoder)
+               return decoder->protected_->state = FLAC__FILE_DECODER_MEMORY_ALLOCATION_ERROR;
+
+       FLAC__seekable_stream_decoder_set_read_callback(decoder->private_->seekable_stream_decoder, read_callback_);
+       FLAC__seekable_stream_decoder_set_seek_callback(decoder->private_->seekable_stream_decoder, seek_callback_);
+       FLAC__seekable_stream_decoder_set_tell_callback(decoder->private_->seekable_stream_decoder, tell_callback_);
+       FLAC__seekable_stream_decoder_set_length_callback(decoder->private_->seekable_stream_decoder, length_callback_);
+       FLAC__seekable_stream_decoder_set_eof_callback(decoder->private_->seekable_stream_decoder, eof_callback_);
+       FLAC__seekable_stream_decoder_set_write_callback(decoder->private_->seekable_stream_decoder, write_callback_);
+       FLAC__seekable_stream_decoder_set_metadata_callback(decoder->private_->seekable_stream_decoder, metadata_callback_);
+       FLAC__seekable_stream_decoder_set_error_callback(decoder->private_->seekable_stream_decoder, error_callback_);
+       FLAC__seekable_stream_decoder_set_client_data(decoder->private_->seekable_stream_decoder, decoder);
+
+       /*
+        * Unfortunately, because of the "_new() ... _set_() ... _init()" order of
+        * decoder initialization, settings that are 'inherited' from the superclass
+        * have to be passed up this way, because the superclass has not even been
+        * created yet when the value is set in the subclass.
+        */
+       (void)FLAC__seekable_stream_decoder_set_md5_checking(decoder->private_->seekable_stream_decoder, decoder->private_->init_values_for_superclass.md5_checking);
+
+       if(FLAC__seekable_stream_decoder_init(decoder->private_->seekable_stream_decoder) != FLAC__SEEKABLE_STREAM_DECODER_OK)
+               return decoder->protected_->state = FLAC__FILE_DECODER_STREAM_DECODER_ERROR; /*@@@ change this to FLAC__FILE_DECODER_SEEKABLE_STREAM_ERROR in next minor-revision */
+
+       return decoder->protected_->state;
+}
+
+FLAC__bool FLAC__file_decoder_finish(FLAC__FileDecoder *decoder)
+{
+       FLAC__bool ok = true;
+
+       FLAC__ASSERT(decoder != 0);
+       if(decoder->protected_->state == FLAC__FILE_DECODER_UNINITIALIZED)
+               return true;
+       if(decoder->private_->file != 0 && decoder->private_->file != stdin)
+               fclose(decoder->private_->file);
+       if(0 != decoder->private_->filename)
+               free(decoder->private_->filename);
+       if(decoder->private_->seekable_stream_decoder != 0) {
+               ok = FLAC__seekable_stream_decoder_finish(decoder->private_->seekable_stream_decoder);
+               FLAC__seekable_stream_decoder_delete(decoder->private_->seekable_stream_decoder);
+       }
+       decoder->protected_->state = FLAC__FILE_DECODER_UNINITIALIZED;
+       return ok;
+}
+
+FLAC__bool FLAC__file_decoder_set_md5_checking(const FLAC__FileDecoder *decoder, FLAC__bool value)
+{
+       if(decoder->protected_->state != FLAC__FILE_DECODER_UNINITIALIZED)
+               return false;
+       decoder->private_->init_values_for_superclass.md5_checking = value;
+       return true;
+}
+
+FLAC__bool FLAC__file_decoder_set_filename(const FLAC__FileDecoder *decoder, const char *value)
+{
+       FLAC__ASSERT(value != 0);
+       if(decoder->protected_->state != FLAC__FILE_DECODER_UNINITIALIZED)
+               return false;
+       if(0 != decoder->private_->filename)
+               free(decoder->private_->filename);
+       if(0 == strcmp(value, "-")) {
+               decoder->private_->filename = 0;
+       }
+       else  {
+               if(0 == (decoder->private_->filename = (char*)malloc(strlen(value)+1))) {
+                       decoder->protected_->state = FLAC__FILE_DECODER_MEMORY_ALLOCATION_ERROR;
+                       return false;
+               }
+               strcpy(decoder->private_->filename, value);
+       }
+       return true;
+}
+
+FLAC__bool FLAC__file_decoder_set_write_callback(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderWriteStatus (*value)(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 *buffer[], void *client_data))
+{
+       if(decoder->protected_->state != FLAC__FILE_DECODER_UNINITIALIZED)
+               return false;
+       decoder->private_->write_callback = value;
+       return true;
+}
+
+FLAC__bool FLAC__file_decoder_set_metadata_callback(const FLAC__FileDecoder *decoder, void (*value)(const FLAC__FileDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data))
+{
+       if(decoder->protected_->state != FLAC__FILE_DECODER_UNINITIALIZED)
+               return false;
+       decoder->private_->metadata_callback = value;
+       return true;
+}
+
+FLAC__bool FLAC__file_decoder_set_error_callback(const FLAC__FileDecoder *decoder, void (*value)(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data))
+{
+       if(decoder->protected_->state != FLAC__FILE_DECODER_UNINITIALIZED)
+               return false;
+       decoder->private_->error_callback = value;
+       return true;
+}
+
+FLAC__bool FLAC__file_decoder_set_client_data(const FLAC__FileDecoder *decoder, void *value)
+{
+       if(decoder->protected_->state != FLAC__FILE_DECODER_UNINITIALIZED)
+               return false;
+       decoder->private_->client_data = value;
+       return true;
+}
+
+FLAC__FileDecoderState FLAC__file_decoder_get_state(const FLAC__FileDecoder *decoder)
+{
+       return decoder->protected_->state;
+}
+
+FLAC__bool FLAC__file_decoder_get_md5_checking(const FLAC__FileDecoder *decoder)
+{
+       return FLAC__seekable_stream_decoder_get_md5_checking(decoder->private_->seekable_stream_decoder);
+}
+
+FLAC__bool FLAC__file_decoder_process_whole_file(FLAC__FileDecoder *decoder)
+{
+       FLAC__bool ret;
+       FLAC__ASSERT(decoder != 0);
+
+       if(decoder->private_->seekable_stream_decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM)
+               decoder->protected_->state = FLAC__FILE_DECODER_END_OF_FILE;
+
+       if(decoder->protected_->state == FLAC__FILE_DECODER_END_OF_FILE)
+               return true;
+
+       FLAC__ASSERT(decoder->protected_->state == FLAC__FILE_DECODER_OK);
+
+       ret = FLAC__seekable_stream_decoder_process_whole_stream(decoder->private_->seekable_stream_decoder);
+       if(!ret)
+               decoder->protected_->state = FLAC__FILE_DECODER_SEEKABLE_STREAM_ERROR;
+
+       return ret;
+}
+
+FLAC__bool FLAC__file_decoder_process_metadata(FLAC__FileDecoder *decoder)
+{
+       FLAC__bool ret;
+       FLAC__ASSERT(decoder != 0);
+
+       if(decoder->private_->seekable_stream_decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM)
+               decoder->protected_->state = FLAC__FILE_DECODER_END_OF_FILE;
+
+       if(decoder->protected_->state == FLAC__FILE_DECODER_END_OF_FILE)
+               return true;
+
+       FLAC__ASSERT(decoder->protected_->state == FLAC__FILE_DECODER_OK);
+
+       ret = FLAC__seekable_stream_decoder_process_metadata(decoder->private_->seekable_stream_decoder);
+       if(!ret)
+               decoder->protected_->state = FLAC__FILE_DECODER_SEEKABLE_STREAM_ERROR;
+
+       return ret;
+}
+
+FLAC__bool FLAC__file_decoder_process_one_frame(FLAC__FileDecoder *decoder)
+{
+       FLAC__bool ret;
+       FLAC__ASSERT(decoder != 0);
+
+       if(decoder->private_->seekable_stream_decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM)
+               decoder->protected_->state = FLAC__FILE_DECODER_END_OF_FILE;
+
+       if(decoder->protected_->state == FLAC__FILE_DECODER_END_OF_FILE)
+               return true;
+
+       FLAC__ASSERT(decoder->protected_->state == FLAC__FILE_DECODER_OK);
+
+       ret = FLAC__seekable_stream_decoder_process_one_frame(decoder->private_->seekable_stream_decoder);
+       if(!ret)
+               decoder->protected_->state = FLAC__FILE_DECODER_SEEKABLE_STREAM_ERROR;
+
+       return ret;
+}
+
+FLAC__bool FLAC__file_decoder_process_remaining_frames(FLAC__FileDecoder *decoder)
+{
+       FLAC__bool ret;
+       FLAC__ASSERT(decoder != 0);
+
+       if(decoder->private_->seekable_stream_decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM)
+               decoder->protected_->state = FLAC__FILE_DECODER_END_OF_FILE;
+
+       if(decoder->protected_->state == FLAC__FILE_DECODER_END_OF_FILE)
+               return true;
+
+       FLAC__ASSERT(decoder->protected_->state == FLAC__FILE_DECODER_OK);
+
+       ret = FLAC__seekable_stream_decoder_process_remaining_frames(decoder->private_->seekable_stream_decoder);
+       if(!ret)
+               decoder->protected_->state = FLAC__FILE_DECODER_SEEKABLE_STREAM_ERROR;
+
+       return ret;
+}
+
+/***********************************************************************
+ *
+ * Private class methods
+ *
+ ***********************************************************************/
+
+FLAC__bool FLAC__file_decoder_seek_absolute(FLAC__FileDecoder *decoder, FLAC__uint64 sample)
+{
+       FLAC__ASSERT(decoder != 0);
+       FLAC__ASSERT(decoder->protected_->state == FLAC__FILE_DECODER_OK);
+
+       if(decoder->private_->filename == 0) { /* means the file is stdin... */
+               decoder->protected_->state = FLAC__FILE_DECODER_SEEK_ERROR;
+               return false;
+       }
+
+       if(!FLAC__seekable_stream_decoder_seek_absolute(decoder->private_->seekable_stream_decoder, sample)) {
+               decoder->protected_->state = FLAC__FILE_DECODER_SEEK_ERROR;
+               return false;
+       }
+       else
+               return true;
+}
+
+FLAC__SeekableStreamDecoderReadStatus read_callback_(const FLAC__SeekableStreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
+{
+       FLAC__FileDecoder *file_decoder = (FLAC__FileDecoder *)client_data;
+       (void)decoder;
+
+       if(*bytes > 0) {
+               size_t bytes_read = fread(buffer, sizeof(FLAC__byte), *bytes, file_decoder->private_->file);
+               if(bytes_read == 0 && !feof(file_decoder->private_->file)) {
+                       return FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_ERROR;
+               }
+               else {
+                       *bytes = (unsigned)bytes_read;
+                       return FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_OK;
+               }
+       }
+       else
+               return FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_ERROR; /* abort to avoid a deadlock */
+}
+
+FLAC__SeekableStreamDecoderSeekStatus seek_callback_(const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data)
+{
+       FLAC__FileDecoder *file_decoder = (FLAC__FileDecoder *)client_data;
+       (void)decoder;
+
+       if(fseek(file_decoder->private_->file, (long)absolute_byte_offset, SEEK_SET) < 0)
+               return FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_ERROR;
+       else
+               return FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_OK;
+}
+
+FLAC__SeekableStreamDecoderTellStatus tell_callback_(const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
+{
+       FLAC__FileDecoder *file_decoder = (FLAC__FileDecoder *)client_data;
+       long pos;
+       (void)decoder;
+
+       if((pos = ftell(file_decoder->private_->file)) < 0)
+               return FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_ERROR;
+       else {
+               *absolute_byte_offset = (FLAC__uint64)pos;
+               return FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_OK;
+       }
+}
+
+FLAC__SeekableStreamDecoderLengthStatus length_callback_(const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data)
+{
+       FLAC__FileDecoder *file_decoder = (FLAC__FileDecoder *)client_data;
+       struct stat filestats;
+       (void)decoder;
+
+       if(stat(file_decoder->private_->filename, &filestats) != 0)
+               return FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_ERROR;
+       else {
+               *stream_length = (FLAC__uint64)filestats.st_size;
+               return FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_OK;
+       }
+}
+
+FLAC__bool eof_callback_(const FLAC__SeekableStreamDecoder *decoder, void *client_data)
+{
+       FLAC__FileDecoder *file_decoder = (FLAC__FileDecoder *)client_data;
+       (void)decoder;
+
+       return feof(file_decoder->private_->file);
+}
+
+FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__SeekableStreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 *buffer[], void *client_data)
+{
+       FLAC__FileDecoder *file_decoder = (FLAC__FileDecoder *)client_data;
+       (void)decoder;
+
+       return file_decoder->private_->write_callback(file_decoder, frame, buffer, file_decoder->private_->client_data);
+}
+
+void metadata_callback_(const FLAC__SeekableStreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data)
+{
+       FLAC__FileDecoder *file_decoder = (FLAC__FileDecoder *)client_data;
+       (void)decoder;
+
+       file_decoder->private_->metadata_callback(file_decoder, metadata, file_decoder->private_->client_data);
+}
+
+void error_callback_(const FLAC__SeekableStreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
+{
+       FLAC__FileDecoder *file_decoder = (FLAC__FileDecoder *)client_data;
+       (void)decoder;
+
+       file_decoder->private_->error_callback(file_decoder, status, file_decoder->private_->client_data);
+}
index 78ade18..31b02f0 100644 (file)
-/* libFLAC - Free Lossless Audio Codec library\r
- * Copyright (C) 2000,2001  Josh Coalson\r
- *\r
- * This library is free software; you can redistribute it and/or\r
- * modify it under the terms of the GNU Library General Public\r
- * License as published by the Free Software Foundation; either\r
- * version 2 of the License, or (at your option) any later version.\r
- *\r
- * This library is distributed in the hope that it will be useful,\r
- * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
- * Library General Public License for more details.\r
- *\r
- * You should have received a copy of the GNU Library General Public\r
- * License along with this library; if not, write to the\r
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,\r
- * Boston, MA  02111-1307, USA.\r
- */\r
-\r
-#include <stdio.h>\r
-#include <stdlib.h> /* for malloc() */\r
-#include "FLAC/assert.h"\r
-#include "protected/seekable_stream_decoder.h"\r
-#include "protected/stream_decoder.h"\r
-#include "private/md5.h"\r
-\r
-/***********************************************************************\r
- *\r
- * Private class method prototypes\r
- *\r
- ***********************************************************************/\r
-\r
-static FLAC__StreamDecoderReadStatus read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data);\r
-static FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 *buffer[], void *client_data);\r
-static void metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data);\r
-static void error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);\r
-static FLAC__bool seek_to_absolute_sample_(FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample);\r
-\r
-/***********************************************************************\r
- *\r
- * Private class data\r
- *\r
- ***********************************************************************/\r
-\r
-typedef struct FLAC__SeekableStreamDecoderPrivate {\r
-       FLAC__SeekableStreamDecoderReadStatus (*read_callback)(const FLAC__SeekableStreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data);\r
-       FLAC__SeekableStreamDecoderSeekStatus (*seek_callback)(const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data);\r
-       FLAC__SeekableStreamDecoderTellStatus (*tell_callback)(const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data);\r
-       FLAC__SeekableStreamDecoderLengthStatus (*length_callback)(const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data);\r
-       FLAC__bool (*eof_callback)(const FLAC__SeekableStreamDecoder *decoder, void *client_data);\r
-       FLAC__StreamDecoderWriteStatus (*write_callback)(const FLAC__SeekableStreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 *buffer[], void *client_data);\r
-       void (*metadata_callback)(const FLAC__SeekableStreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data);\r
-       void (*error_callback)(const FLAC__SeekableStreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);\r
-       void *client_data;\r
-       FLAC__StreamDecoder *stream_decoder;\r
-       struct MD5Context md5context;\r
-       FLAC__byte stored_md5sum[16]; /* this is what is stored in the metadata */\r
-       FLAC__byte computed_md5sum[16]; /* this is the sum we computed from the decoded data */\r
-       /* the rest of these are only used for seeking: */\r
-       FLAC__StreamMetaData_StreamInfo stream_info; /* we keep this around so we can figure out how to seek quickly */\r
-       const FLAC__StreamMetaData_SeekTable *seek_table; /* we hold a pointer to the stream decoder's seek table for the same reason */\r
-       FLAC__Frame last_frame; /* holds the info of the last frame we seeked to */\r
-       FLAC__uint64 target_sample;\r
-} FLAC__SeekableStreamDecoderPrivate;\r
-\r
-/***********************************************************************\r
- *\r
- * Public static class data\r
- *\r
- ***********************************************************************/\r
-\r
-const char *FLAC__SeekableStreamDecoderStateString[] = {\r
-       "FLAC__SEEKABLE_STREAM_DECODER_OK",\r
-       "FLAC__SEEKABLE_STREAM_DECODER_SEEKING",\r
-       "FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM",\r
-       "FLAC__SEEKABLE_STREAM_DECODER_MEMORY_ALLOCATION_ERROR",\r
-       "FLAC__SEEKABLE_STREAM_DECODER_STREAM_ERROR",\r
-       "FLAC__SEEKABLE_STREAM_DECODER_READ_ERROR",\r
-       "FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR",\r
-    "FLAC__SEEKABLE_STREAM_DECODER_ALREADY_INITIALIZED",\r
-    "FLAC__SEEKABLE_STREAM_DECODER_INVALID_CALLBACK",\r
-    "FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED"\r
-};\r
-\r
-const char *FLAC__SeekableStreamDecoderReadStatusString[] = {\r
-       "FLAC__STREAM_DECODER_READ_STATUS_OK",\r
-       "FLAC__STREAM_DECODER_READ_STATUS_ERROR"\r
-};\r
-\r
-const char *FLAC__SeekableStreamDecoderSeekStatusString[] = {\r
-       "FLAC__STREAM_DECODER_SEEK_STATUS_OK",\r
-       "FLAC__STREAM_DECODER_SEEK_STATUS_ERROR"\r
-};\r
-\r
-const char *FLAC__SeekableStreamDecoderTellStatusString[] = {\r
-       "FLAC__STREAM_DECODER_TELL_STATUS_OK",\r
-       "FLAC__STREAM_DECODER_TELL_STATUS_ERROR"\r
-};\r
-\r
-const char *FLAC__SeekableStreamDecoderLengthStatusString[] = {\r
-       "FLAC__STREAM_DECODER_LENGTH_STATUS_OK",\r
-       "FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR"\r
-};\r
-\r
-/***********************************************************************\r
- *\r
- * Class constructor/destructor\r
- *\r
- ***********************************************************************/\r
-\r
-FLAC__SeekableStreamDecoder *FLAC__seekable_stream_decoder_new()\r
-{\r
-       FLAC__SeekableStreamDecoder *decoder;\r
-\r
-       FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */\r
-\r
-       decoder = (FLAC__SeekableStreamDecoder*)malloc(sizeof(FLAC__SeekableStreamDecoder));\r
-       if(decoder == 0) {\r
-               return 0;\r
-       }\r
-       decoder->protected_ = (FLAC__SeekableStreamDecoderProtected*)malloc(sizeof(FLAC__SeekableStreamDecoderProtected));\r
-       if(decoder->protected_ == 0) {\r
-               free(decoder);\r
-               return 0;\r
-       }\r
-       decoder->private_ = (FLAC__SeekableStreamDecoderPrivate*)malloc(sizeof(FLAC__SeekableStreamDecoderPrivate));\r
-       if(decoder->private_ == 0) {\r
-               free(decoder->protected_);\r
-               free(decoder);\r
-               return 0;\r
-       }\r
-\r
-       decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED;\r
-\r
-       decoder->private_->read_callback = 0;\r
-       decoder->private_->seek_callback = 0;\r
-       decoder->private_->tell_callback = 0;\r
-       decoder->private_->length_callback = 0;\r
-       decoder->private_->eof_callback = 0;\r
-       decoder->private_->write_callback = 0;\r
-       decoder->private_->metadata_callback = 0;\r
-       decoder->private_->error_callback = 0;\r
-       decoder->private_->client_data = 0;\r
-\r
-       return decoder;\r
-}\r
-\r
-void FLAC__seekable_stream_decoder_delete(FLAC__SeekableStreamDecoder *decoder)\r
-{\r
-       FLAC__ASSERT(decoder != 0);\r
-       FLAC__ASSERT(decoder->protected_ != 0);\r
-       FLAC__ASSERT(decoder->private_ != 0);\r
-\r
-       free(decoder->private_);\r
-       free(decoder->protected_);\r
-       free(decoder);\r
-}\r
-\r
-/***********************************************************************\r
- *\r
- * Public class methods\r
- *\r
- ***********************************************************************/\r
-\r
-FLAC__SeekableStreamDecoderState FLAC__seekable_stream_decoder_init(FLAC__SeekableStreamDecoder *decoder)\r
-{\r
-       FLAC__ASSERT(decoder != 0);\r
-\r
-       if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)\r
-               return decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_ALREADY_INITIALIZED;\r
-\r
-       decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_OK;\r
-\r
-       if(0 == decoder->private_->read_callback || 0 == decoder->private_->seek_callback || 0 == decoder->private_->tell_callback || 0 == decoder->private_->length_callback || 0 == decoder->private_->eof_callback)\r
-               return decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_INVALID_CALLBACK;\r
-\r
-       if(0 == decoder->private_->write_callback || 0 == decoder->private_->metadata_callback || 0 == decoder->private_->error_callback)\r
-               return decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_INVALID_CALLBACK;\r
-\r
-       decoder->private_->stream_decoder = 0;\r
-       decoder->private_->seek_table = 0;\r
-\r
-       /* We initialize the MD5Context even though we may never use it.  This is\r
-        * because md5_checking may be turned on to start and then turned off if a\r
-        * seek occurs.  So we always init the context here and finalize it in\r
-        * FLAC__seekable_stream_decoder_finish() to make sure things are always\r
-        * cleaned up properly.\r
-        */\r
-       MD5Init(&decoder->private_->md5context);\r
-\r
-       decoder->private_->stream_decoder = FLAC__stream_decoder_new();\r
-\r
-       if(0 == decoder->private_->stream_decoder)\r
-               return decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_MEMORY_ALLOCATION_ERROR;\r
-\r
-       FLAC__stream_decoder_set_read_callback(decoder->private_->stream_decoder, read_callback_);\r
-       FLAC__stream_decoder_set_write_callback(decoder->private_->stream_decoder, write_callback_);\r
-       FLAC__stream_decoder_set_metadata_callback(decoder->private_->stream_decoder, metadata_callback_);\r
-       FLAC__stream_decoder_set_error_callback(decoder->private_->stream_decoder, error_callback_);\r
-       FLAC__stream_decoder_set_client_data(decoder->private_->stream_decoder, decoder);\r
-\r
-       if(FLAC__stream_decoder_init(decoder->private_->stream_decoder) != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA)\r
-               return decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_ERROR;\r
-\r
-       return decoder->protected_->state;\r
-}\r
-\r
-FLAC__bool FLAC__seekable_stream_decoder_finish(FLAC__SeekableStreamDecoder *decoder)\r
-{\r
-       FLAC__bool md5_failed = false;\r
-\r
-       FLAC__ASSERT(decoder != 0);\r
-       if(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)\r
-               return true;\r
-       /* see the comment in FLAC__seekable_stream_decoder_init() as to why we\r
-        * always call MD5Final()\r
-        */\r
-       MD5Final(decoder->private_->computed_md5sum, &decoder->private_->md5context);\r
-       if(decoder->private_->stream_decoder != 0) {\r
-               FLAC__stream_decoder_finish(decoder->private_->stream_decoder);\r
-               FLAC__stream_decoder_delete(decoder->private_->stream_decoder);\r
-       }\r
-       if(decoder->protected_->md5_checking) {\r
-               if(memcmp(decoder->private_->stored_md5sum, decoder->private_->computed_md5sum, 16))\r
-                       md5_failed = true;\r
-       }\r
-       decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED;\r
-       return !md5_failed;\r
-}\r
-\r
-FLAC__bool FLAC__seekable_stream_decoder_set_md5_checking(const FLAC__SeekableStreamDecoder *decoder, FLAC__bool value)\r
-{\r
-       if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)\r
-               return false;\r
-       decoder->protected_->md5_checking = value;\r
-       return true;\r
-}\r
-\r
-FLAC__bool FLAC__seekable_stream_decoder_set_read_callback(const FLAC__SeekableStreamDecoder *decoder, FLAC__SeekableStreamDecoderReadStatus (*value)(const FLAC__SeekableStreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data))\r
-{\r
-       if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)\r
-               return false;\r
-       decoder->private_->read_callback = value;\r
-       return true;\r
-}\r
-\r
-FLAC__bool FLAC__seekable_stream_decoder_set_seek_callback(const FLAC__SeekableStreamDecoder *decoder, FLAC__SeekableStreamDecoderSeekStatus (*value)(const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data))\r
-{\r
-       if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)\r
-               return false;\r
-       decoder->private_->seek_callback = value;\r
-       return true;\r
-}\r
-\r
-FLAC__bool FLAC__seekable_stream_decoder_set_tell_callback(const FLAC__SeekableStreamDecoder *decoder, FLAC__SeekableStreamDecoderTellStatus (*value)(const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data))\r
-{\r
-       if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)\r
-               return false;\r
-       decoder->private_->tell_callback = value;\r
-       return true;\r
-}\r
-\r
-FLAC__bool FLAC__seekable_stream_decoder_set_length_callback(const FLAC__SeekableStreamDecoder *decoder, FLAC__SeekableStreamDecoderLengthStatus (*value)(const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data))\r
-{\r
-       if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)\r
-               return false;\r
-       decoder->private_->length_callback = value;\r
-       return true;\r
-}\r
-\r
-FLAC__bool FLAC__seekable_stream_decoder_set_eof_callback(const FLAC__SeekableStreamDecoder *decoder, FLAC__bool (*value)(const FLAC__SeekableStreamDecoder *decoder, void *client_data))\r
-{\r
-       if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)\r
-               return false;\r
-       decoder->private_->eof_callback = value;\r
-       return true;\r
-}\r
-\r
-FLAC__bool FLAC__seekable_stream_decoder_set_write_callback(const FLAC__SeekableStreamDecoder *decoder, FLAC__StreamDecoderWriteStatus (*value)(const FLAC__SeekableStreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 *buffer[], void *client_data))\r
-{\r
-       if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)\r
-               return false;\r
-       decoder->private_->write_callback = value;\r
-       return true;\r
-}\r
-\r
-FLAC__bool FLAC__seekable_stream_decoder_set_metadata_callback(const FLAC__SeekableStreamDecoder *decoder, void (*value)(const FLAC__SeekableStreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data))\r
-{\r
-       if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)\r
-               return false;\r
-       decoder->private_->metadata_callback = value;\r
-       return true;\r
-}\r
-\r
-FLAC__bool FLAC__seekable_stream_decoder_set_error_callback(const FLAC__SeekableStreamDecoder *decoder, void (*value)(const FLAC__SeekableStreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data))\r
-{\r
-       if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)\r
-               return false;\r
-       decoder->private_->error_callback = value;\r
-       return true;\r
-}\r
-\r
-FLAC__bool FLAC__seekable_stream_decoder_set_client_data(const FLAC__SeekableStreamDecoder *decoder, void *value)\r
-{\r
-       if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)\r
-               return false;\r
-       decoder->private_->client_data = value;\r
-       return true;\r
-}\r
-\r
-FLAC__SeekableStreamDecoderState FLAC__seekable_stream_decoder_get_state(const FLAC__SeekableStreamDecoder *decoder)\r
-{\r
-       return decoder->protected_->state;\r
-}\r
-\r
-FLAC__bool FLAC__seekable_stream_decoder_get_md5_checking(const FLAC__SeekableStreamDecoder *decoder)\r
-{\r
-       return decoder->protected_->md5_checking;\r
-}\r
-\r
-FLAC__bool FLAC__seekable_stream_decoder_process_whole_stream(FLAC__SeekableStreamDecoder *decoder)\r
-{\r
-       FLAC__bool ret;\r
-       FLAC__ASSERT(decoder != 0);\r
-\r
-       if(decoder->private_->stream_decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM)\r
-               decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM;\r
-\r
-       if(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM)\r
-               return true;\r
-\r
-       FLAC__ASSERT(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_OK);\r
-\r
-       ret = FLAC__stream_decoder_process_whole_stream(decoder->private_->stream_decoder);\r
-       if(!ret)\r
-               decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_ERROR;\r
-\r
-       return ret;\r
-}\r
-\r
-FLAC__bool FLAC__seekable_stream_decoder_process_metadata(FLAC__SeekableStreamDecoder *decoder)\r
-{\r
-       FLAC__bool ret;\r
-       FLAC__ASSERT(decoder != 0);\r
-\r
-       if(decoder->private_->stream_decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM)\r
-               decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM;\r
-\r
-       if(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM)\r
-               return true;\r
-\r
-       FLAC__ASSERT(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_OK);\r
-\r
-       ret = FLAC__stream_decoder_process_metadata(decoder->private_->stream_decoder);\r
-       if(!ret)\r
-               decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_ERROR;\r
-\r
-       return ret;\r
-}\r
-\r
-FLAC__bool FLAC__seekable_stream_decoder_process_one_frame(FLAC__SeekableStreamDecoder *decoder)\r
-{\r
-       FLAC__bool ret;\r
-       FLAC__ASSERT(decoder != 0);\r
-\r
-       if(decoder->private_->stream_decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM)\r
-               decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM;\r
-\r
-       if(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM)\r
-               return true;\r
-\r
-       FLAC__ASSERT(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_OK);\r
-\r
-       ret = FLAC__stream_decoder_process_one_frame(decoder->private_->stream_decoder);\r
-       if(!ret)\r
-               decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_ERROR;\r
-\r
-       return ret;\r
-}\r
-\r
-FLAC__bool FLAC__seekable_stream_decoder_process_remaining_frames(FLAC__SeekableStreamDecoder *decoder)\r
-{\r
-       FLAC__bool ret;\r
-       FLAC__ASSERT(decoder != 0);\r
-\r
-       if(decoder->private_->stream_decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM)\r
-               decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM;\r
-\r
-       if(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM)\r
-               return true;\r
-\r
-       FLAC__ASSERT(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_OK);\r
-\r
-       ret = FLAC__stream_decoder_process_remaining_frames(decoder->private_->stream_decoder);\r
-       if(!ret)\r
-               decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_ERROR;\r
-\r
-       return ret;\r
-}\r
-\r
-FLAC__bool FLAC__seekable_stream_decoder_seek_absolute(FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 sample)\r
-{\r
-       FLAC__uint64 length;\r
-\r
-       FLAC__ASSERT(decoder != 0);\r
-       FLAC__ASSERT(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_OK);\r
-\r
-       decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_SEEKING;\r
-\r
-       /* turn off md5 checking if a seek is attempted */\r
-       decoder->protected_->md5_checking = false;\r
-\r
-       if(!FLAC__stream_decoder_reset(decoder->private_->stream_decoder)) {\r
-               decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_ERROR;\r
-               return false;\r
-       }\r
-       /* get the file length */\r
-       if(decoder->private_->length_callback(decoder, &length, decoder->private_->client_data) != FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_OK) {\r
-               decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR;\r
-               return false;\r
-       }\r
-       /* rewind */\r
-       if(decoder->private_->seek_callback(decoder, 0, decoder->private_->client_data) != FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_OK) {\r
-               decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR;\r
-               return false;\r
-       }\r
-       if(!FLAC__stream_decoder_process_metadata(decoder->private_->stream_decoder)) {\r
-               decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_ERROR;\r
-               return false;\r
-       }\r
-       if(sample > decoder->private_->stream_info.total_samples) {\r
-               decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR;\r
-               return false;\r
-       }\r
-\r
-       return seek_to_absolute_sample_(decoder, length, sample);\r
-}\r
-\r
-/***********************************************************************\r
- *\r
- * Private class methods\r
- *\r
- ***********************************************************************/\r
-\r
-FLAC__StreamDecoderReadStatus read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)\r
-{\r
-       FLAC__SeekableStreamDecoder *seekable_stream_decoder = (FLAC__SeekableStreamDecoder *)client_data;\r
-       (void)decoder;\r
-       if(seekable_stream_decoder->private_->eof_callback(seekable_stream_decoder, seekable_stream_decoder->private_->client_data)) {\r
-               seekable_stream_decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM;\r
-               return FLAC__STREAM_DECODER_READ_END_OF_STREAM;\r
-       }\r
-       else if(*bytes > 0) {\r
-               unsigned bytes_read = *bytes;\r
-               if(seekable_stream_decoder->private_->read_callback(seekable_stream_decoder, buffer, &bytes_read, seekable_stream_decoder->private_->client_data) != FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_OK) {\r
-                       seekable_stream_decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_READ_ERROR;\r
-                       return FLAC__STREAM_DECODER_READ_ABORT;\r
-               }\r
-               if(bytes_read == 0) {\r
-                       if(seekable_stream_decoder->private_->eof_callback(seekable_stream_decoder, seekable_stream_decoder->private_->client_data)) {\r
-                               seekable_stream_decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM;\r
-                               return FLAC__STREAM_DECODER_READ_END_OF_STREAM;\r
-                       }\r
-                       else\r
-                               return FLAC__STREAM_DECODER_READ_CONTINUE;\r
-               }\r
-               else {\r
-                       *bytes = bytes_read;\r
-                       return FLAC__STREAM_DECODER_READ_CONTINUE;\r
-               }\r
-       }\r
-       else\r
-               return FLAC__STREAM_DECODER_READ_ABORT; /* abort to avoid a deadlock */\r
-}\r
-\r
-FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 *buffer[], void *client_data)\r
-{\r
-       FLAC__SeekableStreamDecoder *seekable_stream_decoder = (FLAC__SeekableStreamDecoder *)client_data;\r
-       (void)decoder;\r
-\r
-       if(seekable_stream_decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_SEEKING) {\r
-               FLAC__uint64 this_frame_sample = frame->header.number.sample_number;\r
-               FLAC__uint64 next_frame_sample = this_frame_sample + (FLAC__uint64)frame->header.blocksize;\r
-               FLAC__uint64 target_sample = seekable_stream_decoder->private_->target_sample;\r
-\r
-               FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);\r
-\r
-               seekable_stream_decoder->private_->last_frame = *frame; /* save the frame */\r
-               if(this_frame_sample <= target_sample && target_sample < next_frame_sample) { /* we hit our target frame */\r
-                       unsigned delta = (unsigned)(target_sample - this_frame_sample);\r
-                       /* kick out of seek mode */\r
-                       seekable_stream_decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_OK;\r
-                       /* shift out the samples before target_sample */\r
-                       if(delta > 0) {\r
-                               unsigned channel;\r
-                               const FLAC__int32 *newbuffer[FLAC__MAX_CHANNELS];\r
-                               for(channel = 0; channel < frame->header.channels; channel++)\r
-                                       newbuffer[channel] = buffer[channel] + delta;\r
-                               seekable_stream_decoder->private_->last_frame.header.blocksize -= delta;\r
-                               seekable_stream_decoder->private_->last_frame.header.number.sample_number += (FLAC__uint64)delta;\r
-                               /* write the relevant samples */\r
-                               return seekable_stream_decoder->private_->write_callback(seekable_stream_decoder, &seekable_stream_decoder->private_->last_frame, newbuffer, seekable_stream_decoder->private_->client_data);\r
-                       }\r
-                       else {\r
-                               /* write the relevant samples */\r
-                               return seekable_stream_decoder->private_->write_callback(seekable_stream_decoder, frame, buffer, seekable_stream_decoder->private_->client_data);\r
-                       }\r
-               }\r
-               else {\r
-                       return FLAC__STREAM_DECODER_WRITE_CONTINUE;\r
-               }\r
-       }\r
-       else {\r
-               if(seekable_stream_decoder->protected_->md5_checking) {\r
-                       if(!FLAC__MD5Accumulate(&seekable_stream_decoder->private_->md5context, buffer, frame->header.channels, frame->header.blocksize, (frame->header.bits_per_sample+7) / 8))\r
-                               return FLAC__STREAM_DECODER_WRITE_ABORT;\r
-               }\r
-               return seekable_stream_decoder->private_->write_callback(seekable_stream_decoder, frame, buffer, seekable_stream_decoder->private_->client_data);\r
-       }\r
-}\r
-\r
-void metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data)\r
-{\r
-       FLAC__SeekableStreamDecoder *seekable_stream_decoder = (FLAC__SeekableStreamDecoder *)client_data;\r
-       (void)decoder;\r
-\r
-       if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {\r
-               seekable_stream_decoder->private_->stream_info = metadata->data.stream_info;\r
-               /* save the MD5 signature for comparison later */\r
-               memcpy(seekable_stream_decoder->private_->stored_md5sum, metadata->data.stream_info.md5sum, 16);\r
-               if(0 == memcmp(seekable_stream_decoder->private_->stored_md5sum, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16))\r
-                       seekable_stream_decoder->protected_->md5_checking = false;\r
-       }\r
-       else if(metadata->type == FLAC__METADATA_TYPE_SEEKTABLE) {\r
-               seekable_stream_decoder->private_->seek_table = &metadata->data.seek_table;\r
-       }\r
-\r
-       if(seekable_stream_decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_SEEKING)\r
-               seekable_stream_decoder->private_->metadata_callback(seekable_stream_decoder, metadata, seekable_stream_decoder->private_->client_data);\r
-}\r
-\r
-void error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)\r
-{\r
-       FLAC__SeekableStreamDecoder *seekable_stream_decoder = (FLAC__SeekableStreamDecoder *)client_data;\r
-       (void)decoder;\r
-\r
-       if(seekable_stream_decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_SEEKING)\r
-               seekable_stream_decoder->private_->error_callback(seekable_stream_decoder, status, seekable_stream_decoder->private_->client_data);\r
-}\r
-\r
-FLAC__bool seek_to_absolute_sample_(FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample)\r
-{\r
-       FLAC__uint64 first_frame_offset, lower_bound, upper_bound;\r
-       FLAC__int64 pos = -1, last_pos = -1;\r
-       int i, lower_seek_point = -1, upper_seek_point = -1;\r
-       unsigned approx_bytes_per_frame;\r
-       FLAC__uint64 last_frame_sample = 0xffffffffffffffff;\r
-       FLAC__bool needs_seek;\r
-       const FLAC__bool is_variable_blocksize_stream = (decoder->private_->stream_info.min_blocksize != decoder->private_->stream_info.max_blocksize);\r
-\r
-       /* we are just guessing here, but we want to guess high, not low */\r
-       if(decoder->private_->stream_info.max_framesize > 0) {\r
-               approx_bytes_per_frame = decoder->private_->stream_info.max_framesize;\r
-       }\r
-       else if(!is_variable_blocksize_stream) {\r
-               /* note there are no () around 'decoder->private_->stream_info.bits_per_sample/8' to keep precision up since it's an integer calulation */\r
-               approx_bytes_per_frame = decoder->private_->stream_info.min_blocksize * decoder->private_->stream_info.channels * decoder->private_->stream_info.bits_per_sample/8 + 64;\r
-       }\r
-       else\r
-               approx_bytes_per_frame = 1152 * decoder->private_->stream_info.channels * decoder->private_->stream_info.bits_per_sample/8 + 64;\r
-\r
-       /*\r
-        * The stream position is currently at the first frame plus any read\r
-        * ahead data, so first we get the stream position, then subtract\r
-        * uncomsumed bytes to get the position of the first frame in the\r
-        * stream.\r
-        */\r
-       if(decoder->private_->tell_callback(decoder, &first_frame_offset, decoder->private_->client_data) != FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_OK) {\r
-               decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR;\r
-               return false;\r
-       }\r
-       FLAC__ASSERT(first_frame_offset >= FLAC__stream_decoder_get_input_bytes_unconsumed(decoder->private_->stream_decoder));\r
-       first_frame_offset -= FLAC__stream_decoder_get_input_bytes_unconsumed(decoder->private_->stream_decoder);\r
-\r
-       /*\r
-        * First, we set an upper and lower bound on where in the\r
-        * stream we will search.  For now we assume the worst case\r
-        * scenario, which is our best guess at the beginning of\r
-        * the first and last frames.\r
-        */\r
-       lower_bound = first_frame_offset;\r
-\r
-       /* calc the upper_bound, beyond which we never want to seek */\r
-       if(decoder->private_->stream_info.max_framesize > 0)\r
-               upper_bound = stream_length - (decoder->private_->stream_info.max_framesize + 128 + 2); /* 128 for a possible ID3V1 tag, 2 for indexing differences */\r
-       else\r
-               upper_bound = stream_length - ((decoder->private_->stream_info.channels * decoder->private_->stream_info.bits_per_sample * FLAC__MAX_BLOCK_SIZE) / 8 + 128 + 2);\r
-\r
-       /*\r
-        * Now we refine the bounds if we have a seektable with\r
-        * suitable points.  Note that according to the spec they\r
-        * must be ordered by ascending sample number.\r
-        */\r
-       if(0 != decoder->private_->seek_table) {\r
-               /* find the closest seek point <= target_sample, if it exists */\r
-               for(i = (int)decoder->private_->seek_table->num_points - 1; i >= 0; i--) {\r
-                       if(decoder->private_->seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER && decoder->private_->seek_table->points[i].sample_number <= target_sample)\r
-                               break;\r
-               }\r
-               if(i >= 0) { /* i.e. we found a suitable seek point... */\r
-                       lower_bound = first_frame_offset + decoder->private_->seek_table->points[i].stream_offset;\r
-                       lower_seek_point = i;\r
-               }\r
-\r
-               /* find the closest seek point > target_sample, if it exists */\r
-               for(i = 0; i < (int)decoder->private_->seek_table->num_points; i++) {\r
-                       if(decoder->private_->seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER && decoder->private_->seek_table->points[i].sample_number > target_sample)\r
-                               break;\r
-               }\r
-               if(i < (int)decoder->private_->seek_table->num_points) { /* i.e. we found a suitable seek point... */\r
-                       upper_bound = first_frame_offset + decoder->private_->seek_table->points[i].stream_offset;\r
-                       upper_seek_point = i;\r
-               }\r
-       }\r
-\r
-       /*\r
-        * Now guess at where within those bounds our target\r
-        * sample will be.\r
-        */\r
-       if(lower_seek_point >= 0) {\r
-               /* first see if our sample is within a few frames of the lower seekpoint */\r
-               if(decoder->private_->seek_table->points[lower_seek_point].sample_number <= target_sample && target_sample < decoder->private_->seek_table->points[lower_seek_point].sample_number + (decoder->private_->seek_table->points[lower_seek_point].frame_samples * 4)) {\r
-                       pos = (FLAC__int64)lower_bound;\r
-               }\r
-               else if(upper_seek_point >= 0) {\r
-                       const FLAC__uint64 target_offset = target_sample - decoder->private_->seek_table->points[lower_seek_point].sample_number;\r
-                       const FLAC__uint64 range_samples = decoder->private_->seek_table->points[upper_seek_point].sample_number - decoder->private_->seek_table->points[lower_seek_point].sample_number;\r
-                       const FLAC__uint64 range_bytes = upper_bound - lower_bound;\r
-#ifdef _MSC_VER\r
-                       /* with VC++ you have to spoon feed it the casting */\r
-                       pos = (FLAC__int64)lower_bound + (FLAC__int64)((double)(FLAC__int64)target_offset / (double)(FLAC__int64)range_samples * (double)(FLAC__int64)(range_bytes-1)) - approx_bytes_per_frame;\r
-#else\r
-                       pos = (FLAC__int64)lower_bound + (FLAC__int64)((double)target_offset / (double)range_samples * (double)(range_bytes-1)) - approx_bytes_per_frame;\r
-#endif\r
-               }\r
-       }\r
-       if(pos < 0) {\r
-               /* We need to use the metadata and the filelength to estimate the position of the frame with the correct sample */\r
-#ifdef _MSC_VER\r
-               /* with VC++ you have to spoon feed it the casting */\r
-               pos = (FLAC__int64)first_frame_offset + (FLAC__int64)((double)(FLAC__int64)target_sample / (double)(FLAC__int64)decoder->private_->stream_info.total_samples * (double)(FLAC__int64)(stream_length-first_frame_offset-1)) - approx_bytes_per_frame;\r
-#else\r
-               pos = (FLAC__int64)first_frame_offset + (FLAC__int64)((double)target_sample / (double)decoder->private_->stream_info.total_samples * (double)(stream_length-first_frame_offset-1)) - approx_bytes_per_frame;\r
-#endif\r
-       }\r
-\r
-       /* clip the position to the bounds, lower bound takes precedence */\r
-       if(pos >= (FLAC__int64)upper_bound)\r
-               pos = (FLAC__int64)upper_bound-1;\r
-       if(pos < (FLAC__int64)lower_bound)\r
-               pos = (FLAC__int64)lower_bound;\r
-       needs_seek = true;\r
-\r
-       decoder->private_->target_sample = target_sample;\r
-       while(1) {\r
-               if(needs_seek) {\r
-                       if(decoder->private_->seek_callback(decoder, (FLAC__uint64)pos, decoder->private_->client_data) != FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_OK) {\r
-                               decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR;\r
-                               return false;\r
-                       }\r
-                       if(!FLAC__stream_decoder_flush(decoder->private_->stream_decoder)) {\r
-                               decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_ERROR;\r
-                               return false;\r
-                       }\r
-               }\r
-               if(!FLAC__stream_decoder_process_one_frame(decoder->private_->stream_decoder)) {\r
-                       decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR;\r
-                       return false;\r
-               }\r
-               /* our write callback will change the state when it gets to the target frame */\r
-               if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_SEEKING) {\r
-                       break;\r
-               }\r
-               else { /* we need to narrow the search */\r
-                       FLAC__uint64 this_frame_sample = decoder->private_->last_frame.header.number.sample_number;\r
-                       FLAC__ASSERT(decoder->private_->last_frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);\r
-                       if(this_frame_sample == last_frame_sample) {\r
-                               /* our last move backwards wasn't big enough */\r
-                               pos -= (last_pos - pos);\r
-                               needs_seek = true;\r
-                       }\r
-                       else {\r
-                               if(target_sample < this_frame_sample) {\r
-                                       last_pos = pos;\r
-                                       approx_bytes_per_frame = decoder->private_->last_frame.header.blocksize * decoder->private_->last_frame.header.channels * decoder->private_->last_frame.header.bits_per_sample/8 + 64;\r
-                                       pos -= approx_bytes_per_frame;\r
-                                       needs_seek = true;\r
-                               }\r
-                               else { /* target_sample >= this_frame_sample + this frame's blocksize */\r
-                                       last_pos = pos;\r
-                                       if(decoder->private_->tell_callback(decoder, (FLAC__uint64*)(&pos), decoder->private_->client_data) != FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_OK) {\r
-                                               decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR;\r
-                                               return false;\r
-                                       }\r
-                                       pos -= FLAC__stream_decoder_get_input_bytes_unconsumed(decoder->private_->stream_decoder);\r
-                                       needs_seek = false;\r
-                               }\r
-                       }\r
-                       if(pos < (FLAC__int64)lower_bound)\r
-                               pos = (FLAC__int64)lower_bound;\r
-                       last_frame_sample = this_frame_sample;\r
-               }\r
-       }\r
-\r
-       return true;\r
-}\r
+/* libFLAC - Free Lossless Audio Codec library
+ * Copyright (C) 2000,2001  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.
+ *
+ * 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.
+ *
+ * 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.
+ */
+
+#include <stdio.h>
+#include <stdlib.h> /* for malloc() */
+#include "FLAC/assert.h"
+#include "protected/seekable_stream_decoder.h"
+#include "protected/stream_decoder.h"
+#include "private/md5.h"
+
+/***********************************************************************
+ *
+ * Private class method prototypes
+ *
+ ***********************************************************************/
+
+static FLAC__StreamDecoderReadStatus read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data);
+static FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 *buffer[], void *client_data);
+static void metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data);
+static void error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
+static FLAC__bool seek_to_absolute_sample_(FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample);
+
+/***********************************************************************
+ *
+ * Private class data
+ *
+ ***********************************************************************/
+
+typedef struct FLAC__SeekableStreamDecoderPrivate {
+       FLAC__SeekableStreamDecoderReadStatus (*read_callback)(const FLAC__SeekableStreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data);
+       FLAC__SeekableStreamDecoderSeekStatus (*seek_callback)(const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data);
+       FLAC__SeekableStreamDecoderTellStatus (*tell_callback)(const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data);
+       FLAC__SeekableStreamDecoderLengthStatus (*length_callback)(const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data);
+       FLAC__bool (*eof_callback)(const FLAC__SeekableStreamDecoder *decoder, void *client_data);
+       FLAC__StreamDecoderWriteStatus (*write_callback)(const FLAC__SeekableStreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 *buffer[], void *client_data);
+       void (*metadata_callback)(const FLAC__SeekableStreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data);
+       void (*error_callback)(const FLAC__SeekableStreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
+       void *client_data;
+       FLAC__StreamDecoder *stream_decoder;
+       struct MD5Context md5context;
+       FLAC__byte stored_md5sum[16]; /* this is what is stored in the metadata */
+       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__StreamMetaData_StreamInfo stream_info; /* we keep this around so we can figure out how to seek quickly */
+       const FLAC__StreamMetaData_SeekTable *seek_table; /* we hold a pointer to the stream decoder's seek table for the same reason */
+       FLAC__Frame last_frame; /* holds the info of the last frame we seeked to */
+       FLAC__uint64 target_sample;
+} FLAC__SeekableStreamDecoderPrivate;
+
+/***********************************************************************
+ *
+ * Public static class data
+ *
+ ***********************************************************************/
+
+const char *FLAC__SeekableStreamDecoderStateString[] = {
+       "FLAC__SEEKABLE_STREAM_DECODER_OK",
+       "FLAC__SEEKABLE_STREAM_DECODER_SEEKING",
+       "FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM",
+       "FLAC__SEEKABLE_STREAM_DECODER_MEMORY_ALLOCATION_ERROR",
+       "FLAC__SEEKABLE_STREAM_DECODER_STREAM_ERROR",
+       "FLAC__SEEKABLE_STREAM_DECODER_READ_ERROR",
+       "FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR",
+    "FLAC__SEEKABLE_STREAM_DECODER_ALREADY_INITIALIZED",
+    "FLAC__SEEKABLE_STREAM_DECODER_INVALID_CALLBACK",
+    "FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED"
+};
+
+const char *FLAC__SeekableStreamDecoderReadStatusString[] = {
+       "FLAC__STREAM_DECODER_READ_STATUS_OK",
+       "FLAC__STREAM_DECODER_READ_STATUS_ERROR"
+};
+
+const char *FLAC__SeekableStreamDecoderSeekStatusString[] = {
+       "FLAC__STREAM_DECODER_SEEK_STATUS_OK",
+       "FLAC__STREAM_DECODER_SEEK_STATUS_ERROR"
+};
+
+const char *FLAC__SeekableStreamDecoderTellStatusString[] = {
+       "FLAC__STREAM_DECODER_TELL_STATUS_OK",
+       "FLAC__STREAM_DECODER_TELL_STATUS_ERROR"
+};
+
+const char *FLAC__SeekableStreamDecoderLengthStatusString[] = {
+       "FLAC__STREAM_DECODER_LENGTH_STATUS_OK",
+       "FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR"
+};
+
+/***********************************************************************
+ *
+ * Class constructor/destructor
+ *
+ ***********************************************************************/
+
+FLAC__SeekableStreamDecoder *FLAC__seekable_stream_decoder_new()
+{
+       FLAC__SeekableStreamDecoder *decoder;
+
+       FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
+
+       decoder = (FLAC__SeekableStreamDecoder*)malloc(sizeof(FLAC__SeekableStreamDecoder));
+       if(decoder == 0) {
+               return 0;
+       }
+       decoder->protected_ = (FLAC__SeekableStreamDecoderProtected*)malloc(sizeof(FLAC__SeekableStreamDecoderProtected));
+       if(decoder->protected_ == 0) {
+               free(decoder);
+               return 0;
+       }
+       decoder->private_ = (FLAC__SeekableStreamDecoderPrivate*)malloc(sizeof(FLAC__SeekableStreamDecoderPrivate));
+       if(decoder->private_ == 0) {
+               free(decoder->protected_);
+               free(decoder);
+               return 0;
+       }
+
+       decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED;
+
+       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;
+
+       return decoder;
+}
+
+void FLAC__seekable_stream_decoder_delete(FLAC__SeekableStreamDecoder *decoder)
+{
+       FLAC__ASSERT(decoder != 0);
+       FLAC__ASSERT(decoder->protected_ != 0);
+       FLAC__ASSERT(decoder->private_ != 0);
+
+       free(decoder->private_);
+       free(decoder->protected_);
+       free(decoder);
+}
+
+/***********************************************************************
+ *
+ * Public class methods
+ *
+ ***********************************************************************/
+
+FLAC__SeekableStreamDecoderState FLAC__seekable_stream_decoder_init(FLAC__SeekableStreamDecoder *decoder)
+{
+       FLAC__ASSERT(decoder != 0);
+
+       if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
+               return decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_ALREADY_INITIALIZED;
+
+       decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_OK;
+
+       if(0 == decoder->private_->read_callback || 0 == decoder->private_->seek_callback || 0 == decoder->private_->tell_callback || 0 == decoder->private_->length_callback || 0 == decoder->private_->eof_callback)
+               return decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_INVALID_CALLBACK;
+
+       if(0 == decoder->private_->write_callback || 0 == decoder->private_->metadata_callback || 0 == decoder->private_->error_callback)
+               return decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_INVALID_CALLBACK;
+
+       decoder->private_->stream_decoder = 0;
+       decoder->private_->seek_table = 0;
+
+       /* We initialize the 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 always init the context here and finalize it in
+        * FLAC__seekable_stream_decoder_finish() to make sure things are always
+        * cleaned up properly.
+        */
+       MD5Init(&decoder->private_->md5context);
+
+       decoder->private_->stream_decoder = FLAC__stream_decoder_new();
+
+       if(0 == decoder->private_->stream_decoder)
+               return decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
+
+       FLAC__stream_decoder_set_read_callback(decoder->private_->stream_decoder, read_callback_);
+       FLAC__stream_decoder_set_write_callback(decoder->private_->stream_decoder, write_callback_);
+       FLAC__stream_decoder_set_metadata_callback(decoder->private_->stream_decoder, metadata_callback_);
+       FLAC__stream_decoder_set_error_callback(decoder->private_->stream_decoder, error_callback_);
+       FLAC__stream_decoder_set_client_data(decoder->private_->stream_decoder, decoder);
+
+       if(FLAC__stream_decoder_init(decoder->private_->stream_decoder) != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA)
+               return decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_ERROR;
+
+       return decoder->protected_->state;
+}
+
+FLAC__bool FLAC__seekable_stream_decoder_finish(FLAC__SeekableStreamDecoder *decoder)
+{
+       FLAC__bool md5_failed = false;
+
+       FLAC__ASSERT(decoder != 0);
+       if(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
+               return true;
+       /* see the comment in FLAC__seekable_stream_decoder_init() as to why we
+        * always call MD5Final()
+        */
+       MD5Final(decoder->private_->computed_md5sum, &decoder->private_->md5context);
+       if(decoder->private_->stream_decoder != 0) {
+               FLAC__stream_decoder_finish(decoder->private_->stream_decoder);
+               FLAC__stream_decoder_delete(decoder->private_->stream_decoder);
+       }
+       if(decoder->protected_->md5_checking) {
+               if(memcmp(decoder->private_->stored_md5sum, decoder->private_->computed_md5sum, 16))
+                       md5_failed = true;
+       }
+       decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED;
+       return !md5_failed;
+}
+
+FLAC__bool FLAC__seekable_stream_decoder_set_md5_checking(const FLAC__SeekableStreamDecoder *decoder, FLAC__bool value)
+{
+       if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
+               return false;
+       decoder->protected_->md5_checking = value;
+       return true;
+}
+
+FLAC__bool FLAC__seekable_stream_decoder_set_read_callback(const FLAC__SeekableStreamDecoder *decoder, FLAC__SeekableStreamDecoderReadStatus (*value)(const FLAC__SeekableStreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data))
+{
+       if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
+               return false;
+       decoder->private_->read_callback = value;
+       return true;
+}
+
+FLAC__bool FLAC__seekable_stream_decoder_set_seek_callback(const FLAC__SeekableStreamDecoder *decoder, FLAC__SeekableStreamDecoderSeekStatus (*value)(const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data))
+{
+       if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
+               return false;
+       decoder->private_->seek_callback = value;
+       return true;
+}
+
+FLAC__bool FLAC__seekable_stream_decoder_set_tell_callback(const FLAC__SeekableStreamDecoder *decoder, FLAC__SeekableStreamDecoderTellStatus (*value)(const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data))
+{
+       if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
+               return false;
+       decoder->private_->tell_callback = value;
+       return true;
+}
+
+FLAC__bool FLAC__seekable_stream_decoder_set_length_callback(const FLAC__SeekableStreamDecoder *decoder, FLAC__SeekableStreamDecoderLengthStatus (*value)(const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data))
+{
+       if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
+               return false;
+       decoder->private_->length_callback = value;
+       return true;
+}
+
+FLAC__bool FLAC__seekable_stream_decoder_set_eof_callback(const FLAC__SeekableStreamDecoder *decoder, FLAC__bool (*value)(const FLAC__SeekableStreamDecoder *decoder, void *client_data))
+{
+       if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
+               return false;
+       decoder->private_->eof_callback = value;
+       return true;
+}
+
+FLAC__bool FLAC__seekable_stream_decoder_set_write_callback(const FLAC__SeekableStreamDecoder *decoder, FLAC__StreamDecoderWriteStatus (*value)(const FLAC__SeekableStreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 *buffer[], void *client_data))
+{
+       if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
+               return false;
+       decoder->private_->write_callback = value;
+       return true;
+}
+
+FLAC__bool FLAC__seekable_stream_decoder_set_metadata_callback(const FLAC__SeekableStreamDecoder *decoder, void (*value)(const FLAC__SeekableStreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data))
+{
+       if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
+               return false;
+       decoder->private_->metadata_callback = value;
+       return true;
+}
+
+FLAC__bool FLAC__seekable_stream_decoder_set_error_callback(const FLAC__SeekableStreamDecoder *decoder, void (*value)(const FLAC__SeekableStreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data))
+{
+       if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
+               return false;
+       decoder->private_->error_callback = value;
+       return true;
+}
+
+FLAC__bool FLAC__seekable_stream_decoder_set_client_data(const FLAC__SeekableStreamDecoder *decoder, void *value)
+{
+       if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
+               return false;
+       decoder->private_->client_data = value;
+       return true;
+}
+
+FLAC__SeekableStreamDecoderState FLAC__seekable_stream_decoder_get_state(const FLAC__SeekableStreamDecoder *decoder)
+{
+       return decoder->protected_->state;
+}
+
+FLAC__bool FLAC__seekable_stream_decoder_get_md5_checking(const FLAC__SeekableStreamDecoder *decoder)
+{
+       return decoder->protected_->md5_checking;
+}
+
+FLAC__bool FLAC__seekable_stream_decoder_process_whole_stream(FLAC__SeekableStreamDecoder *decoder)
+{
+       FLAC__bool ret;
+       FLAC__ASSERT(decoder != 0);
+
+       if(decoder->private_->stream_decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM)
+               decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM;
+
+       if(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM)
+               return true;
+
+       FLAC__ASSERT(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_OK);
+
+       ret = FLAC__stream_decoder_process_whole_stream(decoder->private_->stream_decoder);
+       if(!ret)
+               decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_ERROR;
+
+       return ret;
+}
+
+FLAC__bool FLAC__seekable_stream_decoder_process_metadata(FLAC__SeekableStreamDecoder *decoder)
+{
+       FLAC__bool ret;
+       FLAC__ASSERT(decoder != 0);
+
+       if(decoder->private_->stream_decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM)
+               decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM;
+
+       if(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM)
+               return true;
+
+       FLAC__ASSERT(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_OK);
+
+       ret = FLAC__stream_decoder_process_metadata(decoder->private_->stream_decoder);
+       if(!ret)
+               decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_ERROR;
+
+       return ret;
+}
+
+FLAC__bool FLAC__seekable_stream_decoder_process_one_frame(FLAC__SeekableStreamDecoder *decoder)
+{
+       FLAC__bool ret;
+       FLAC__ASSERT(decoder != 0);
+
+       if(decoder->private_->stream_decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM)
+               decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM;
+
+       if(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM)
+               return true;
+
+       FLAC__ASSERT(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_OK);
+
+       ret = FLAC__stream_decoder_process_one_frame(decoder->private_->stream_decoder);
+       if(!ret)
+               decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_ERROR;
+
+       return ret;
+}
+
+FLAC__bool FLAC__seekable_stream_decoder_process_remaining_frames(FLAC__SeekableStreamDecoder *decoder)
+{
+       FLAC__bool ret;
+       FLAC__ASSERT(decoder != 0);
+
+       if(decoder->private_->stream_decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM)
+               decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM;
+
+       if(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM)
+               return true;
+
+       FLAC__ASSERT(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_OK);
+
+       ret = FLAC__stream_decoder_process_remaining_frames(decoder->private_->stream_decoder);
+       if(!ret)
+               decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_ERROR;
+
+       return ret;
+}
+
+FLAC__bool FLAC__seekable_stream_decoder_seek_absolute(FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 sample)
+{
+       FLAC__uint64 length;
+
+       FLAC__ASSERT(decoder != 0);
+       FLAC__ASSERT(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_OK);
+
+       decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_SEEKING;
+
+       /* turn off md5 checking if a seek is attempted */
+       decoder->protected_->md5_checking = false;
+
+       if(!FLAC__stream_decoder_reset(decoder->private_->stream_decoder)) {
+               decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_ERROR;
+               return false;
+       }
+       /* get the file length */
+       if(decoder->private_->length_callback(decoder, &length, decoder->private_->client_data) != FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_OK) {
+               decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR;
+               return false;
+       }
+       /* rewind */
+       if(decoder->private_->seek_callback(decoder, 0, decoder->private_->client_data) != FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_OK) {
+               decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR;
+               return false;
+       }
+       if(!FLAC__stream_decoder_process_metadata(decoder->private_->stream_decoder)) {
+               decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_ERROR;
+               return false;
+       }
+       if(sample > decoder->private_->stream_info.total_samples) {
+               decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR;
+               return false;
+       }
+
+       return seek_to_absolute_sample_(decoder, length, sample);
+}
+
+/***********************************************************************
+ *
+ * Private class methods
+ *
+ ***********************************************************************/
+
+FLAC__StreamDecoderReadStatus read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
+{
+       FLAC__SeekableStreamDecoder *seekable_stream_decoder = (FLAC__SeekableStreamDecoder *)client_data;
+       (void)decoder;
+       if(seekable_stream_decoder->private_->eof_callback(seekable_stream_decoder, seekable_stream_decoder->private_->client_data)) {
+               seekable_stream_decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM;
+               return FLAC__STREAM_DECODER_READ_END_OF_STREAM;
+       }
+       else if(*bytes > 0) {
+               unsigned bytes_read = *bytes;
+               if(seekable_stream_decoder->private_->read_callback(seekable_stream_decoder, buffer, &bytes_read, seekable_stream_decoder->private_->client_data) != FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_OK) {
+                       seekable_stream_decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_READ_ERROR;
+                       return FLAC__STREAM_DECODER_READ_ABORT;
+               }
+               if(bytes_read == 0) {
+                       if(seekable_stream_decoder->private_->eof_callback(seekable_stream_decoder, seekable_stream_decoder->private_->client_data)) {
+                               seekable_stream_decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM;
+                               return FLAC__STREAM_DECODER_READ_END_OF_STREAM;
+                       }
+                       else
+                               return FLAC__STREAM_DECODER_READ_CONTINUE;
+               }
+               else {
+                       *bytes = bytes_read;
+                       return FLAC__STREAM_DECODER_READ_CONTINUE;
+               }
+       }
+       else
+               return FLAC__STREAM_DECODER_READ_ABORT; /* abort to avoid a deadlock */
+}
+
+FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 *buffer[], void *client_data)
+{
+       FLAC__SeekableStreamDecoder *seekable_stream_decoder = (FLAC__SeekableStreamDecoder *)client_data;
+       (void)decoder;
+
+       if(seekable_stream_decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_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 = seekable_stream_decoder->private_->target_sample;
+
+               FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
+
+               seekable_stream_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 */
+                       seekable_stream_decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_OK;
+                       /* 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;
+                               seekable_stream_decoder->private_->last_frame.header.blocksize -= delta;
+                               seekable_stream_decoder->private_->last_frame.header.number.sample_number += (FLAC__uint64)delta;
+                               /* write the relevant samples */
+                               return seekable_stream_decoder->private_->write_callback(seekable_stream_decoder, &seekable_stream_decoder->private_->last_frame, newbuffer, seekable_stream_decoder->private_->client_data);
+                       }
+                       else {
+                               /* write the relevant samples */
+                               return seekable_stream_decoder->private_->write_callback(seekable_stream_decoder, frame, buffer, seekable_stream_decoder->private_->client_data);
+                       }
+               }
+               else {
+                       return FLAC__STREAM_DECODER_WRITE_CONTINUE;
+               }
+       }
+       else {
+               if(seekable_stream_decoder->protected_->md5_checking) {
+                       if(!FLAC__MD5Accumulate(&seekable_stream_decoder->private_->md5context, buffer, frame->header.channels, frame->header.blocksize, (frame->header.bits_per_sample+7) / 8))
+                               return FLAC__STREAM_DECODER_WRITE_ABORT;
+               }
+               return seekable_stream_decoder->private_->write_callback(seekable_stream_decoder, frame, buffer, seekable_stream_decoder->private_->client_data);
+       }
+}
+
+void metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data)
+{
+       FLAC__SeekableStreamDecoder *seekable_stream_decoder = (FLAC__SeekableStreamDecoder *)client_data;
+       (void)decoder;
+
+       if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
+               seekable_stream_decoder->private_->stream_info = metadata->data.stream_info;
+               /* save the MD5 signature for comparison later */
+               memcpy(seekable_stream_decoder->private_->stored_md5sum, metadata->data.stream_info.md5sum, 16);
+               if(0 == memcmp(seekable_stream_decoder->private_->stored_md5sum, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16))
+                       seekable_stream_decoder->protected_->md5_checking = false;
+       }
+       else if(metadata->type == FLAC__METADATA_TYPE_SEEKTABLE) {
+               seekable_stream_decoder->private_->seek_table = &metadata->data.seek_table;
+       }
+
+       if(seekable_stream_decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_SEEKING)
+               seekable_stream_decoder->private_->metadata_callback(seekable_stream_decoder, metadata, seekable_stream_decoder->private_->client_data);
+}
+
+void error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
+{
+       FLAC__SeekableStreamDecoder *seekable_stream_decoder = (FLAC__SeekableStreamDecoder *)client_data;
+       (void)decoder;
+
+       if(seekable_stream_decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_SEEKING)
+               seekable_stream_decoder->private_->error_callback(seekable_stream_decoder, status, seekable_stream_decoder->private_->client_data);
+}
+
+FLAC__bool seek_to_absolute_sample_(FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample)
+{
+       FLAC__uint64 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 = 0xffffffffffffffff;
+       FLAC__bool needs_seek;
+       const FLAC__bool is_variable_blocksize_stream = (decoder->private_->stream_info.min_blocksize != decoder->private_->stream_info.max_blocksize);
+
+       /* we are just guessing here, but we want to guess high, not low */
+       if(decoder->private_->stream_info.max_framesize > 0) {
+               approx_bytes_per_frame = decoder->private_->stream_info.max_framesize;
+       }
+       else if(!is_variable_blocksize_stream) {
+               /* note there are no () around 'decoder->private_->stream_info.bits_per_sample/8' to keep precision up since it's an integer calulation */
+               approx_bytes_per_frame = decoder->private_->stream_info.min_blocksize * decoder->private_->stream_info.channels * decoder->private_->stream_info.bits_per_sample/8 + 64;
+       }
+       else
+               approx_bytes_per_frame = 1152 * decoder->private_->stream_info.channels * decoder->private_->stream_info.bits_per_sample/8 + 64;
+
+       /*
+        * The stream position is currently at the first frame plus any read
+        * ahead data, so first we get the stream position, then subtract
+        * uncomsumed bytes to get the position of the first frame in the
+        * stream.
+        */
+       if(decoder->private_->tell_callback(decoder, &first_frame_offset, decoder->private_->client_data) != FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_OK) {
+               decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR;
+               return false;
+       }
+       FLAC__ASSERT(first_frame_offset >= FLAC__stream_decoder_get_input_bytes_unconsumed(decoder->private_->stream_decoder));
+       first_frame_offset -= FLAC__stream_decoder_get_input_bytes_unconsumed(decoder->private_->stream_decoder);
+
+       /*
+        * 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(decoder->private_->stream_info.max_framesize > 0)
+               upper_bound = stream_length - (decoder->private_->stream_info.max_framesize + 128 + 2); /* 128 for a possible ID3V1 tag, 2 for indexing differences */
+       else
+               upper_bound = stream_length - ((decoder->private_->stream_info.channels * decoder->private_->stream_info.bits_per_sample * 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(0 != decoder->private_->seek_table) {
+               /* find the closest seek point <= target_sample, if it exists */
+               for(i = (int)decoder->private_->seek_table->num_points - 1; i >= 0; i--) {
+                       if(decoder->private_->seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER && decoder->private_->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 + decoder->private_->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)decoder->private_->seek_table->num_points; i++) {
+                       if(decoder->private_->seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER && decoder->private_->seek_table->points[i].sample_number > target_sample)
+                               break;
+               }
+               if(i < (int)decoder->private_->seek_table->num_points) { /* i.e. we found a suitable seek point... */
+                       upper_bound = first_frame_offset + decoder->private_->seek_table->points[i].stream_offset;
+                       upper_seek_point = i;
+               }
+       }
+
+       /*
+        * Now guess at where within those bounds our target
+        * sample will be.
+        */
+       if(lower_seek_point >= 0) {
+               /* first see if our sample is within a few frames of the lower seekpoint */
+               if(decoder->private_->seek_table->points[lower_seek_point].sample_number <= target_sample && target_sample < decoder->private_->seek_table->points[lower_seek_point].sample_number + (decoder->private_->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 - decoder->private_->seek_table->points[lower_seek_point].sample_number;
+                       const FLAC__uint64 range_samples = decoder->private_->seek_table->points[upper_seek_point].sample_number - decoder->private_->seek_table->points[lower_seek_point].sample_number;
+                       const FLAC__uint64 range_bytes = upper_bound - lower_bound;
+#ifdef _MSC_VER
+                       /* with VC++ you have to spoon feed it the casting */
+                       pos = (FLAC__int64)lower_bound + (FLAC__int64)((double)(FLAC__int64)target_offset / (double)(FLAC__int64)range_samples * (double)(FLAC__int64)(range_bytes-1)) - approx_bytes_per_frame;
+#else
+                       pos = (FLAC__int64)lower_bound + (FLAC__int64)((double)target_offset / (double)range_samples * (double)(range_bytes-1)) - approx_bytes_per_frame;
+#endif
+               }
+       }
+       if(pos < 0) {
+               /* We need to use the metadata and the filelength to estimate the position of the frame with the correct sample */
+#ifdef _MSC_VER
+               /* with VC++ you have to spoon feed it the casting */
+               pos = (FLAC__int64)first_frame_offset + (FLAC__int64)((double)(FLAC__int64)target_sample / (double)(FLAC__int64)decoder->private_->stream_info.total_samples * (double)(FLAC__int64)(stream_length-first_frame_offset-1)) - approx_bytes_per_frame;
+#else
+               pos = (FLAC__int64)first_frame_offset + (FLAC__int64)((double)target_sample / (double)decoder->private_->stream_info.total_samples * (double)(stream_length-first_frame_offset-1)) - approx_bytes_per_frame;
+#endif
+       }
+
+       /* clip the position to the bounds, lower bound takes precedence */
+       if(pos >= (FLAC__int64)upper_bound)
+               pos = (FLAC__int64)upper_bound-1;
+       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__SEEKABLE_STREAM_DECODER_SEEK_STATUS_OK) {
+                               decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR;
+                               return false;
+                       }
+                       if(!FLAC__stream_decoder_flush(decoder->private_->stream_decoder)) {
+                               decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_ERROR;
+                               return false;
+                       }
+               }
+               if(!FLAC__stream_decoder_process_one_frame(decoder->private_->stream_decoder)) {
+                       decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR;
+                       return false;
+               }
+               /* our write callback will change the state when it gets to the target frame */
+               if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_SEEKING) {
+                       break;
+               }
+               else { /* we need to narrow the search */
+                       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) {
+                               /* our last move backwards wasn't big enough */
+                               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 * decoder->private_->last_frame.header.channels * decoder->private_->last_frame.header.bits_per_sample/8 + 64;
+                                       pos -= approx_bytes_per_frame;
+                                       needs_seek = true;
+                               }
+                               else { /* target_sample >= this_frame_sample + this frame's blocksize */
+                                       last_pos = pos;
+                                       if(decoder->private_->tell_callback(decoder, (FLAC__uint64*)(&pos), decoder->private_->client_data) != FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_OK) {
+                                               decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR;
+                                               return false;
+                                       }
+                                       pos -= FLAC__stream_decoder_get_input_bytes_unconsumed(decoder->private_->stream_decoder);
+                                       needs_seek = false;
+                               }
+                       }
+                       if(pos < (FLAC__int64)lower_bound)
+                               pos = (FLAC__int64)lower_bound;
+                       last_frame_sample = this_frame_sample;
+               }
+       }
+
+       return true;
+}