Fix buffer overflow by replacing strcpy with memcpy.
[flac.git] / src / metaflac / operations_shorthand_seektable.c
index 7d20f24..08f59b7 100644 (file)
@@ -1,5 +1,5 @@
 /* metaflac - Command-line FLAC metadata editor
- * Copyright (C) 2001,2002  Josh Coalson
+ * Copyright (C) 2001,2002,2003,2004,2005,2006,2007,2008,2009  Josh Coalson
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  */
 
+#if HAVE_CONFIG_H
+#  include <config.h>
+#endif
+
 #include "utils.h"
 #include "FLAC/assert.h"
-#include "FLAC/file_decoder.h"
+#include "FLAC/stream_decoder.h"
 #include "FLAC/metadata.h"
 #include "share/grabbag.h"
+#include "operations_shorthand.h"
 
 static FLAC__bool populate_seekpoint_values(const char *filename, FLAC__StreamMetadata *block, FLAC__bool *needs_write);
 
@@ -60,7 +65,8 @@ FLAC__bool do_shorthand_operation__add_seekpoints(const char *filename, FLAC__Me
                while(FLAC__metadata_iterator_prev(iterator))
                        ;
                if(!FLAC__metadata_iterator_insert_block_after(iterator, block)) {
-                       fprintf(stderr, "%s: ERROR: adding new SEEKTABLE block to metadata, status =\"%s\"\n", filename, FLAC__Metadata_ChainStatusString[FLAC__metadata_chain_status(chain)]);
+                       print_error_with_chain_status(chain, "%s: ERROR: adding new SEEKTABLE block to metadata", filename);
+                       FLAC__metadata_object_delete(block);
                        return false;
                }
                /* iterator is left pointing to new block */
@@ -79,9 +85,8 @@ FLAC__bool do_shorthand_operation__add_seekpoints(const char *filename, FLAC__Me
 
        ok = populate_seekpoint_values(filename, block, needs_write);
 
-       if(ok) {
-               //@@@@ compact it
-       }
+       if(ok)
+               (void) FLAC__format_seektable_sort(&block->data.seek_table);
 
        return ok;
 }
@@ -93,68 +98,60 @@ FLAC__bool do_shorthand_operation__add_seekpoints(const char *filename, FLAC__Me
 typedef struct {
        FLAC__StreamMetadata_SeekTable *seektable_template;
        FLAC__uint64 samples_written;
+       FLAC__uint64 audio_offset, last_offset;
        unsigned first_seekpoint_to_check;
        FLAC__bool error_occurred;
        FLAC__StreamDecoderErrorStatus error_status;
 } ClientData;
 
-static FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
+static FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
 {
        ClientData *cd = (ClientData*)client_data;
 
+       (void)buffer;
        FLAC__ASSERT(0 != cd);
 
-       if(!cd->error_occurred && cd->seektable_template->num_points > 0) {
+       if(!cd->error_occurred) {
                const unsigned blocksize = frame->header.blocksize;
-               //@@@@
+               const FLAC__uint64 frame_first_sample = cd->samples_written;
+               const FLAC__uint64 frame_last_sample = frame_first_sample + (FLAC__uint64)blocksize - 1;
+               FLAC__uint64 test_sample;
+               unsigned i;
+               for(i = cd->first_seekpoint_to_check; i < cd->seektable_template->num_points; i++) {
+                       test_sample = cd->seektable_template->points[i].sample_number;
+                       if(test_sample > frame_last_sample) {
+                               break;
+                       }
+                       else if(test_sample >= frame_first_sample) {
+                               cd->seektable_template->points[i].sample_number = frame_first_sample;
+                               cd->seektable_template->points[i].stream_offset = cd->last_offset - cd->audio_offset;
+                               cd->seektable_template->points[i].frame_samples = blocksize;
+                               cd->first_seekpoint_to_check++;
+                               /* DO NOT: "break;" and here's why:
+                                * The seektable template may contain more than one target
+                                * sample for any given frame; we will keep looping, generating
+                                * duplicate seekpoints for them, and we'll clean it up later,
+                                * just before writing the seektable back to the metadata.
+                                */
+                       }
+                       else {
+                               cd->first_seekpoint_to_check++;
+                       }
+               }
                cd->samples_written += blocksize;
+               if(!FLAC__stream_decoder_get_decode_position(decoder, &cd->last_offset))
+                       return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
                return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
        }
        else
                return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
 }
-#if 0
-@@@@
-{
-       const unsigned blocksize = FLAC__stream_encoder_get_blocksize(encoder);
-       const FLAC__uint64 frame_first_sample = seekable_stream_encoder->private_->samples_written;
-       const FLAC__uint64 frame_last_sample = frame_first_sample + (FLAC__uint64)blocksize - 1;
-       FLAC__uint64 test_sample;
-       unsigned i;
-       for(i = seekable_stream_encoder->private_->first_seekpoint_to_check; i < seekable_stream_encoder->private_->seek_table->num_points; i++) {
-               test_sample = seekable_stream_encoder->private_->seek_table->points[i].sample_number;
-               if(test_sample > frame_last_sample) {
-                       break;
-               }
-               else if(test_sample >= frame_first_sample) {
-                       seekable_stream_encoder->private_->seek_table->points[i].sample_number = frame_first_sample;
-                       seekable_stream_encoder->private_->seek_table->points[i].stream_offset = seekable_stream_encoder->private_->bytes_written - seekable_stream_encoder->private_->stream_offset;
-                       seekable_stream_encoder->private_->seek_table->points[i].frame_samples = blocksize;
-                       seekable_stream_encoder->private_->first_seekpoint_to_check++;
-                       /* DO NOT: "break;" and here's why:
-                        * The seektable template may contain more than one target
-                        * sample for any given frame; we will keep looping, generating
-                        * duplicate seekpoints for them, and we'll clean it up later,
-                        * just before writing the seektable back to the metadata.
-                        */
-               }
-               else {
-                       seekable_stream_encoder->private_->first_seekpoint_to_check++;
-               }
-       }
-}
-#endif
 
-static void metadata_callback_(const FLAC__FileDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
-{
-       (void)decoder, (void)metadata, (void)client_data;
-       FLAC__ASSERT(0); /* we asked to skip all metadata */
-}
-
-static void error_callback_(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
+static void error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
 {
        ClientData *cd = (ClientData*)client_data;
 
+       (void)decoder;
        FLAC__ASSERT(0 != cd);
 
        if(!cd->error_occurred) { /* don't let multiple errors overwrite the first one */
@@ -165,7 +162,7 @@ static void error_callback_(const FLAC__FileDecoder *decoder, FLAC__StreamDecode
 
 FLAC__bool populate_seekpoint_values(const char *filename, FLAC__StreamMetadata *block, FLAC__bool *needs_write)
 {
-       FLAC__FileDecoder *decoder;
+       FLAC__StreamDecoder *decoder;
        ClientData client_data;
        FLAC__bool ok = true;
 
@@ -174,31 +171,38 @@ FLAC__bool populate_seekpoint_values(const char *filename, FLAC__StreamMetadata
 
        client_data.seektable_template = &block->data.seek_table;
        client_data.samples_written = 0;
+       /* client_data.audio_offset must be determined later */
        client_data.first_seekpoint_to_check = 0;
        client_data.error_occurred = false;
 
-       decoder = FLAC__file_decoder_new();
+       decoder = FLAC__stream_decoder_new();
 
        if(0 == decoder) {
                fprintf(stderr, "%s: ERROR (--add-seekpoint) creating the decoder instance\n", filename);
                return false;
        }
 
-       FLAC__file_decoder_set_md5_checking(decoder, false);
-       FLAC__file_decoder_set_filename(decoder, filename);
-       FLAC__file_decoder_set_metadata_ignore_all(decoder);
-       FLAC__file_decoder_set_write_callback(decoder, write_callback_);
-       FLAC__file_decoder_set_metadata_callback(decoder, metadata_callback_);
-       FLAC__file_decoder_set_error_callback(decoder, error_callback_);
-       FLAC__file_decoder_set_client_data(decoder, &client_data);
+       FLAC__stream_decoder_set_md5_checking(decoder, false);
+       FLAC__stream_decoder_set_metadata_ignore_all(decoder);
+
+       if(FLAC__stream_decoder_init_file(decoder, filename, write_callback_, /*metadata_callback=*/0, error_callback_, &client_data) != FLAC__STREAM_DECODER_INIT_STATUS_OK) {
+               fprintf(stderr, "%s: ERROR (--add-seekpoint) initializing the decoder instance (%s)\n", filename, FLAC__stream_decoder_get_resolved_state_string(decoder));
+               ok = false;
+       }
+
+       if(ok && !FLAC__stream_decoder_process_until_end_of_metadata(decoder)) {
+               fprintf(stderr, "%s: ERROR (--add-seekpoint) decoding file (%s)\n", filename, FLAC__stream_decoder_get_resolved_state_string(decoder));
+               ok = false;
+       }
 
-       if(FLAC__file_decoder_init(decoder) != FLAC__FILE_DECODER_OK) {
-               fprintf(stderr, "%s: ERROR (--add-seekpoint) initializing the decoder instance (%s)\n", filename, FLAC__file_decoder_get_resolved_state_string(decoder));
+       if(ok && !FLAC__stream_decoder_get_decode_position(decoder, &client_data.audio_offset)) {
+               fprintf(stderr, "%s: ERROR (--add-seekpoint) decoding file\n", filename);
                ok = false;
        }
+       client_data.last_offset = client_data.audio_offset;
 
-       if(ok && !FLAC__file_decoder_process_until_end_of_file(decoder)) {
-               fprintf(stderr, "%s: ERROR (--add-seekpoint) decoding file (%s)\n", filename, FLAC__file_decoder_get_resolved_state_string(decoder));
+       if(ok && !FLAC__stream_decoder_process_until_end_of_stream(decoder)) {
+               fprintf(stderr, "%s: ERROR (--add-seekpoint) decoding file (%s)\n", filename, FLAC__stream_decoder_get_resolved_state_string(decoder));
                ok = false;
        }
 
@@ -207,6 +211,7 @@ FLAC__bool populate_seekpoint_values(const char *filename, FLAC__StreamMetadata
                ok = false;
        }
 
-       FLAC__file_decoder_delete(decoder);
+       *needs_write = true;
+       FLAC__stream_decoder_delete(decoder);
        return ok;
 }