Fix buffer overflow by replacing strcpy with memcpy.
[platform/upstream/flac.git] / src / test_libFLAC++ / encoders.cpp
index bbc0560..b2961fe 100644 (file)
@@ -1,5 +1,5 @@
 /* test_libFLAC++ - Unit tester for libFLAC++
- * Copyright (C) 2002,2003,2004,2005,2006  Josh Coalson
+ * Copyright (C) 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
@@ -49,7 +49,7 @@ static const unsigned num_metadata_ = sizeof(metadata_sequence_) / sizeof(metada
 
 static const char *flacfilename(bool is_ogg)
 {
-       return is_ogg? "metadata.ogg" : "metadata.flac";
+       return is_ogg? "metadata.oga" : "metadata.flac";
 }
 
 static bool die_(const char *msg)
@@ -89,13 +89,14 @@ static void free_metadata_blocks_()
 class StreamEncoder : public FLAC::Encoder::Stream {
 public:
        Layer layer_;
+       FILE *file_;
 
-       StreamEncoder(Layer layer): FLAC::Encoder::Stream(), layer_(layer) { }
+       StreamEncoder(Layer layer): FLAC::Encoder::Stream(), layer_(layer), file_(0) { }
        ~StreamEncoder() { }
 
        // from FLAC::Encoder::Stream
        ::FLAC__StreamEncoderReadStatus read_callback(FLAC__byte buffer[], size_t *bytes);
-       ::FLAC__StreamEncoderWriteStatus write_callback(const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame);
+       ::FLAC__StreamEncoderWriteStatus write_callback(const FLAC__byte buffer[], size_t bytes, unsigned samples, unsigned current_frame);
        ::FLAC__StreamEncoderSeekStatus seek_callback(FLAC__uint64 absolute_byte_offset);
        ::FLAC__StreamEncoderTellStatus tell_callback(FLAC__uint64 *absolute_byte_offset);
        void metadata_callback(const ::FLAC__StreamMetadata *metadata);
@@ -103,30 +104,50 @@ public:
 
 ::FLAC__StreamEncoderReadStatus StreamEncoder::read_callback(FLAC__byte buffer[], size_t *bytes)
 {
-       (void)buffer, (void)bytes;
-
-       return ::FLAC__STREAM_ENCODER_READ_STATUS_CONTINUE;
+       if(*bytes > 0) {
+               *bytes = fread(buffer, sizeof(FLAC__byte), *bytes, file_);
+               if(ferror(file_))
+                       return ::FLAC__STREAM_ENCODER_READ_STATUS_ABORT;
+               else if(*bytes == 0)
+                       return ::FLAC__STREAM_ENCODER_READ_STATUS_END_OF_STREAM;
+               else
+                       return ::FLAC__STREAM_ENCODER_READ_STATUS_CONTINUE;
+       }
+       else
+               return ::FLAC__STREAM_ENCODER_READ_STATUS_ABORT;
 }
 
-::FLAC__StreamEncoderWriteStatus StreamEncoder::write_callback(const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame)
+::FLAC__StreamEncoderWriteStatus StreamEncoder::write_callback(const FLAC__byte buffer[], size_t bytes, unsigned samples, unsigned current_frame)
 {
-       (void)buffer, (void)bytes, (void)samples, (void)current_frame;
+       (void)samples, (void)current_frame;
 
-       return ::FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
+       if(fwrite(buffer, 1, bytes, file_) != bytes)
+               return ::FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
+       else
+               return ::FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
 }
 
 ::FLAC__StreamEncoderSeekStatus StreamEncoder::seek_callback(FLAC__uint64 absolute_byte_offset)
 {
-       (void)absolute_byte_offset;
-
-       return layer_==LAYER_STREAM? ::FLAC__STREAM_ENCODER_SEEK_STATUS_UNSUPPORTED : ::FLAC__STREAM_ENCODER_SEEK_STATUS_OK;
+       if(layer_==LAYER_STREAM)
+               return ::FLAC__STREAM_ENCODER_SEEK_STATUS_UNSUPPORTED;
+       else if(fseek(file_, (long)absolute_byte_offset, SEEK_SET) < 0)
+               return FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR;
+       else
+               return FLAC__STREAM_ENCODER_SEEK_STATUS_OK;
 }
 
 ::FLAC__StreamEncoderTellStatus StreamEncoder::tell_callback(FLAC__uint64 *absolute_byte_offset)
 {
-       *absolute_byte_offset = 0;
-
-       return layer_==LAYER_STREAM? ::FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED : ::FLAC__STREAM_ENCODER_TELL_STATUS_OK;
+       long pos;
+       if(layer_==LAYER_STREAM)
+               return ::FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED;
+       else if((pos = ftell(file_)) < 0)
+               return FLAC__STREAM_ENCODER_TELL_STATUS_ERROR;
+       else {
+               *absolute_byte_offset = (FLAC__uint64)pos;
+               return FLAC__STREAM_ENCODER_TELL_STATUS_OK;
+       }
 }
 
 void StreamEncoder::metadata_callback(const ::FLAC__StreamMetadata *metadata)
@@ -201,16 +222,6 @@ static bool test_stream_encoder(Layer layer, bool is_ogg)
                return die_s_("returned false", encoder);
        printf("OK\n");
 
-       printf("testing set_do_mid_side_stereo()... ");
-       if(!encoder->set_do_mid_side_stereo(false))
-               return die_s_("returned false", encoder);
-       printf("OK\n");
-
-       printf("testing set_loose_mid_side_stereo()... ");
-       if(!encoder->set_loose_mid_side_stereo(false))
-               return die_s_("returned false", encoder);
-       printf("OK\n");
-
        printf("testing set_channels()... ");
        if(!encoder->set_channels(streaminfo_.data.stream_info.channels))
                return die_s_("returned false", encoder);
@@ -226,11 +237,26 @@ static bool test_stream_encoder(Layer layer, bool is_ogg)
                return die_s_("returned false", encoder);
        printf("OK\n");
 
+       printf("testing set_compression_level()... ");
+       if(!encoder->set_compression_level((unsigned)(-1)))
+               return die_s_("returned false", encoder);
+       printf("OK\n");
+
        printf("testing set_blocksize()... ");
        if(!encoder->set_blocksize(streaminfo_.data.stream_info.min_blocksize))
                return die_s_("returned false", encoder);
        printf("OK\n");
 
+       printf("testing set_do_mid_side_stereo()... ");
+       if(!encoder->set_do_mid_side_stereo(false))
+               return die_s_("returned false", encoder);
+       printf("OK\n");
+
+       printf("testing set_loose_mid_side_stereo()... ");
+       if(!encoder->set_loose_mid_side_stereo(false))
+               return die_s_("returned false", encoder);
+       printf("OK\n");
+
        printf("testing set_max_lpc_order()... ");
        if(!encoder->set_max_lpc_order(0))
                return die_s_("returned false", encoder);
@@ -281,6 +307,18 @@ static bool test_stream_encoder(Layer layer, bool is_ogg)
                return die_s_("returned false", encoder);
        printf("OK\n");
 
+       if(layer < LAYER_FILENAME) {
+               printf("opening file for FLAC output... ");
+               file = ::fopen(flacfilename(is_ogg), "w+b");
+               if(0 == file) {
+                       printf("ERROR (%s)\n", strerror(errno));
+                       return false;
+               }
+               printf("OK\n");
+               if(layer < LAYER_FILE)
+                       dynamic_cast<StreamEncoder*>(encoder)->file_ = file;
+       }
+
        switch(layer) {
                case LAYER_STREAM:
                case LAYER_SEEKABLE_STREAM:
@@ -288,14 +326,6 @@ static bool test_stream_encoder(Layer layer, bool is_ogg)
                        init_status = is_ogg? encoder->init_ogg() : encoder->init();
                        break;
                case LAYER_FILE:
-                       printf("opening file for FLAC output... ");
-                       file = ::fopen(flacfilename(is_ogg), "w+b");
-                       if(0 == file) {
-                               printf("ERROR (%s)\n", strerror(errno));
-                               return false;
-                       }
-                       printf("OK\n");
-
                        printf("testing init%s()... ", is_ogg? "_ogg":"");
                        init_status = is_ogg?
                                dynamic_cast<FLAC::Encoder::File*>(encoder)->init_ogg(file) :
@@ -448,7 +478,11 @@ static bool test_stream_encoder(Layer layer, bool is_ogg)
 
        printf("testing get_total_samples_estimate()... ");
        if(encoder->get_total_samples_estimate() != streaminfo_.data.stream_info.total_samples) {
-               printf("FAILED, expected %llu, got %llu\n", streaminfo_.data.stream_info.total_samples, encoder->get_total_samples_estimate());
+#ifdef _MSC_VER
+               printf("FAILED, expected %I64u, got %I64u\n", streaminfo_.data.stream_info.total_samples, encoder->get_total_samples_estimate());
+#else
+               printf("FAILED, expected %llu, got %llu\n", (unsigned long long)streaminfo_.data.stream_info.total_samples, (unsigned long long)encoder->get_total_samples_estimate());
+#endif
                return false;
        }
        printf("OK\n");
@@ -468,9 +502,16 @@ static bool test_stream_encoder(Layer layer, bool is_ogg)
        printf("OK\n");
 
        printf("testing finish()... ");
-       encoder->finish();
+       if(!encoder->finish()) {
+               FLAC::Encoder::Stream::State state = encoder->get_state();
+               printf("FAILED, returned false, state = %u (%s)\n", (unsigned)((::FLAC__StreamEncoderState)state), state.as_cstring());
+               return false;
+       }
        printf("OK\n");
 
+       if(layer < LAYER_FILE)
+               ::fclose(dynamic_cast<StreamEncoder*>(encoder)->file_);
+
        printf("freeing encoder instance... ");
        delete encoder;
        printf("OK\n");