Fix a couple of Windows 2Gig file size issues.
[platform/upstream/flac.git] / src / test_libFLAC++ / metadata_manip.cpp
index 893fef2..ff02416 100644 (file)
@@ -1,5 +1,5 @@
 /* test_libFLAC++ - Unit tester for libFLAC++
- * Copyright (C) 2002  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
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
-extern "C" {
-#include "file_utils.h"
-}
-#include "FLAC/assert.h"
-#include "FLAC++/decoder.h"
-#include "FLAC++/metadata.h"
-#include "share/file_utils.h"
+#if HAVE_CONFIG_H
+#  include <config.h>
+#endif
+
 #include <stdio.h>
 #include <stdlib.h> /* for malloc() */
 #include <string.h> /* for memcpy()/memset() */
+#include <sys/types.h> /* some flavors of BSD (like OS X) require this to get time_t */
+#ifdef _MSC_VER
+#include <sys/utime.h>
+#else
+#include <utime.h> /* for utime() */
+#endif
+#if !defined _MSC_VER && !defined __MINGW32__ && !defined __EMX__
+#include <unistd.h> /* for chown(), unlink() */
+#endif
+#include <sys/stat.h> /* for stat(), maybe chmod() */
+#include "FLAC/assert.h"
+#include "FLAC++/decoder.h"
+#include "FLAC++/metadata.h"
+#include "share/grabbag.h"
+#include "share/compat.h"
+#include "share/macros.h"
+extern "C" {
+#include "test_libs_common/file_utils_flac.h"
+}
 
 /******************************************************************************
        The general strategy of these tests (for interface levels 1 and 2) is
@@ -42,7 +58,7 @@ class OurFileDecoder: public FLAC::Decoder::File {
 public:
        inline OurFileDecoder(bool ignore_metadata): ignore_metadata_(ignore_metadata), error_occurred_(false) { }
 
-       bool ignore_metadata_;;
+       bool ignore_metadata_;
        bool error_occurred_;
 protected:
        ::FLAC__StreamDecoderWriteStatus write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[]);
@@ -55,14 +71,17 @@ struct OurMetadata {
        unsigned num_blocks;
 };
 
-static const char *flacfile_ = "metadata.flac";
-
-/* our copy of the metadata in flacfile_ */
+/* our copy of the metadata in flacfilename() */
 static OurMetadata our_metadata_;
 
 /* the current block number that corresponds to the position of the iterator we are testing */
 static unsigned mc_our_block_number_ = 0;
 
+static const char *flacfilename(bool is_ogg)
+{
+       return is_ogg? "metadata.oga" : "metadata.flac";
+}
+
 static bool die_(const char *msg)
 {
        printf("ERROR: %s\n", msg);
@@ -94,6 +113,16 @@ static void *malloc_or_die_(size_t size)
        return x;
 }
 
+static char *strdup_or_die_(const char *s)
+{
+       char *x = strdup(s);
+       if(0 == x) {
+               fprintf(stderr, "ERROR: out of memory copying string \"%s\"\n", s);
+               exit(1);
+       }
+       return x;
+}
+
 /* functions for working with our metadata copy */
 
 static bool replace_in_our_metadata_(FLAC::Metadata::Prototype *block, unsigned position, bool copy)
@@ -166,13 +195,211 @@ void add_to_padding_length_(unsigned index, int delta)
        padding->set_length((unsigned)((int)padding->get_length() + delta));
 }
 
+/*
+ * This wad of functions supports filename- and callback-based chain reading/writing.
+ * Everything up to set_file_stats_() is copied from libFLAC/metadata_iterators.c
+ */
+bool open_tempfile_(const char *filename, FILE **tempfile, char **tempfilename)
+{
+       static const char *tempfile_suffix = ".metadata_edit";
+
+       if(0 == (*tempfilename = (char*)malloc(strlen(filename) + strlen(tempfile_suffix) + 1)))
+               return false;
+       strcpy(*tempfilename, filename);
+       strcat(*tempfilename, tempfile_suffix);
+
+       if(0 == (*tempfile = fopen(*tempfilename, "wb")))
+               return false;
+
+       return true;
+}
+
+void cleanup_tempfile_(FILE **tempfile, char **tempfilename)
+{
+       if(0 != *tempfile) {
+               (void)fclose(*tempfile);
+               *tempfile = 0;
+       }
+
+       if(0 != *tempfilename) {
+               (void)unlink(*tempfilename);
+               free(*tempfilename);
+               *tempfilename = 0;
+       }
+}
+
+bool transport_tempfile_(const char *filename, FILE **tempfile, char **tempfilename)
+{
+       FLAC__ASSERT(0 != filename);
+       FLAC__ASSERT(0 != tempfile);
+       FLAC__ASSERT(0 != tempfilename);
+       FLAC__ASSERT(0 != *tempfilename);
+
+       if(0 != *tempfile) {
+               (void)fclose(*tempfile);
+               *tempfile = 0;
+       }
+
+#if defined _MSC_VER || defined __MINGW32__ || defined __EMX__
+       /* on some flavors of windows, rename() will fail if the destination already exists */
+       if(unlink(filename) < 0) {
+               cleanup_tempfile_(tempfile, tempfilename);
+               return false;
+       }
+#endif
+
+       if(0 != rename(*tempfilename, filename)) {
+               cleanup_tempfile_(tempfile, tempfilename);
+               return false;
+       }
+
+       cleanup_tempfile_(tempfile, tempfilename);
+
+       return true;
+}
+
+bool get_file_stats_(const char *filename, struct stat *stats)
+{
+       FLAC__ASSERT(0 != filename);
+       FLAC__ASSERT(0 != stats);
+       return (0 == stat(filename, stats));
+}
+
+void set_file_stats_(const char *filename, struct stat *stats)
+{
+       struct utimbuf srctime;
+
+       FLAC__ASSERT(0 != filename);
+       FLAC__ASSERT(0 != stats);
+
+       srctime.actime = stats->st_atime;
+       srctime.modtime = stats->st_mtime;
+       (void)chmod(filename, stats->st_mode);
+       (void)utime(filename, &srctime);
+#if !defined _MSC_VER && !defined __MINGW32__ && !defined __EMX__
+       FLAC_CHECK_RETURN(chown(filename, stats->st_uid, (gid_t)(-1)));
+       FLAC_CHECK_RETURN(chown(filename, (uid_t)(-1), stats->st_gid));
+#endif
+}
+
+#ifdef FLAC__VALGRIND_TESTING
+static size_t chain_write_cb_(const void *ptr, size_t size, size_t nmemb, ::FLAC__IOHandle handle)
+{
+       FILE *stream = (FILE*)handle;
+       size_t ret = fwrite(ptr, size, nmemb, stream);
+       if(!ferror(stream))
+               fflush(stream);
+       return ret;
+}
+#endif
+
+static int chain_seek_cb_(::FLAC__IOHandle handle, FLAC__int64 offset, int whence)
+{
+       FLAC__off_t o = (FLAC__off_t)offset;
+       FLAC__ASSERT(offset == o);
+       return fseeko((FILE*)handle, o, whence);
+}
+
+static FLAC__int64 chain_tell_cb_(::FLAC__IOHandle handle)
+{
+       return ftello((FILE*)handle);
+}
+
+static int chain_eof_cb_(::FLAC__IOHandle handle)
+{
+       return feof((FILE*)handle);
+}
+
+static bool write_chain_(FLAC::Metadata::Chain &chain, bool use_padding, bool preserve_file_stats, bool filename_based, const char *filename)
+{
+       if(filename_based)
+               return chain.write(use_padding, preserve_file_stats);
+       else {
+               ::FLAC__IOCallbacks callbacks;
+
+               memset(&callbacks, 0, sizeof(callbacks));
+               callbacks.read = (::FLAC__IOCallback_Read)fread;
+#ifdef FLAC__VALGRIND_TESTING
+               callbacks.write = chain_write_cb_;
+#else
+               callbacks.write = (::FLAC__IOCallback_Write)fwrite;
+#endif
+               callbacks.seek = chain_seek_cb_;
+               callbacks.eof = chain_eof_cb_;
+
+               if(chain.check_if_tempfile_needed(use_padding)) {
+                       struct stat stats;
+                       FILE *file, *tempfile;
+                       char *tempfilename;
+                       if(preserve_file_stats) {
+                               if(!get_file_stats_(filename, &stats))
+                                       return false;
+                       }
+                       if(0 == (file = fopen(filename, "rb")))
+                               return false; /*@@@@ chain status still says OK though */
+                       if(!open_tempfile_(filename, &tempfile, &tempfilename)) {
+                               fclose(file);
+                               cleanup_tempfile_(&tempfile, &tempfilename);
+                               return false; /*@@@@ chain status still says OK though */
+                       }
+                       if(!chain.write(use_padding, (::FLAC__IOHandle)file, callbacks, (::FLAC__IOHandle)tempfile, callbacks)) {
+                               fclose(file);
+                               fclose(tempfile);
+                               return false;
+                       }
+                       fclose(file);
+                       fclose(tempfile);
+                       file = tempfile = 0;
+                       if(!transport_tempfile_(filename, &tempfile, &tempfilename))
+                               return false;
+                       if(preserve_file_stats)
+                               set_file_stats_(filename, &stats);
+               }
+               else {
+                       FILE *file = fopen(filename, "r+b");
+                       if(0 == file)
+                               return false; /*@@@@ chain status still says OK though */
+                       if(!chain.write(use_padding, (::FLAC__IOHandle)file, callbacks)) {
+                               fclose(file);
+                               return false;
+                       }
+                       fclose(file);
+               }
+       }
+
+       return true;
+}
+
+static bool read_chain_(FLAC::Metadata::Chain &chain, const char *filename, bool filename_based, bool is_ogg)
+{
+       if(filename_based)
+               return chain.read(filename, is_ogg);
+       else {
+               ::FLAC__IOCallbacks callbacks;
+
+               memset(&callbacks, 0, sizeof(callbacks));
+               callbacks.read = (::FLAC__IOCallback_Read)fread;
+               callbacks.seek = chain_seek_cb_;
+               callbacks.tell = chain_tell_cb_;
+
+               {
+                       bool ret;
+                       FILE *file = fopen(filename, "rb");
+                       if(0 == file)
+                               return false; /*@@@@ chain status still says OK though */
+                       ret = chain.read((::FLAC__IOHandle)file, callbacks, is_ogg);
+                       fclose(file);
+                       return ret;
+               }
+       }
+}
+
 /* function for comparing our metadata to a FLAC::Metadata::Chain */
 
 static bool compare_chain_(FLAC::Metadata::Chain &chain, unsigned current_position, FLAC::Metadata::Prototype *current_block)
 {
        unsigned i;
        FLAC::Metadata::Iterator iterator;
-       FLAC::Metadata::Prototype *block;
        bool next_ok = true;
 
        printf("\tcomparing chain... ");
@@ -185,6 +412,8 @@ static bool compare_chain_(FLAC::Metadata::Chain &chain, unsigned current_positi
 
        i = 0;
        do {
+               FLAC::Metadata::Prototype *block;
+
                printf("%u... ", i);
                fflush(stdout);
 
@@ -194,6 +423,7 @@ static bool compare_chain_(FLAC::Metadata::Chain &chain, unsigned current_positi
                if(*block != *our_metadata_.blocks[i])
                        return die_("metadata block mismatch");
 
+               delete block;
                i++;
                next_ok = iterator.next();
        } while(i < our_metadata_.num_blocks && next_ok);
@@ -263,12 +493,13 @@ void OurFileDecoder::error_callback(::FLAC__StreamDecoderErrorStatus status)
        printf("ERROR: got error callback, status = %s (%u)\n", FLAC__StreamDecoderErrorStatusString[status], (unsigned)status);
 }
 
-static bool generate_file_()
+static bool generate_file_(bool include_extras, bool is_ogg)
 {
-       ::FLAC__StreamMetadata streaminfo, vorbiscomment, padding;
-       ::FLAC__StreamMetadata *metadata[1];
+       ::FLAC__StreamMetadata streaminfo, vorbiscomment, *cuesheet, picture, padding;
+       ::FLAC__StreamMetadata *metadata[4];
+       unsigned i = 0, n = 0;
 
-       printf("generating FLAC file for test\n");
+       printf("generating %sFLAC file for test\n", is_ogg? "Ogg " : "");
 
        while(our_metadata_.num_blocks > 0)
                delete_from_our_metadata_(0);
@@ -289,45 +520,99 @@ static bool generate_file_()
        {
                const unsigned vendor_string_length = (unsigned)strlen(FLAC__VENDOR_STRING);
                vorbiscomment.is_last = false;
-               vorbiscomment.type = FLAC__METADATA_TYPE_VORBIS_COMMENT;
+               vorbiscomment.type = ::FLAC__METADATA_TYPE_VORBIS_COMMENT;
                vorbiscomment.length = (4 + vendor_string_length) + 4;
                vorbiscomment.data.vorbis_comment.vendor_string.length = vendor_string_length;
-               vorbiscomment.data.vorbis_comment.vendor_string.entry = (FLAC__byte*)malloc_or_die_(vendor_string_length);
-               memcpy(vorbiscomment.data.vorbis_comment.vendor_string.entry, FLAC__VENDOR_STRING, vendor_string_length);
+               vorbiscomment.data.vorbis_comment.vendor_string.entry = (FLAC__byte*)malloc_or_die_(vendor_string_length+1);
+               memcpy(vorbiscomment.data.vorbis_comment.vendor_string.entry, FLAC__VENDOR_STRING, vendor_string_length+1);
                vorbiscomment.data.vorbis_comment.num_comments = 0;
                vorbiscomment.data.vorbis_comment.comments = 0;
        }
 
+       {
+               if (0 == (cuesheet = ::FLAC__metadata_object_new(::FLAC__METADATA_TYPE_CUESHEET)))
+                       return die_("priming our metadata");
+               cuesheet->is_last = false;
+               strcpy(cuesheet->data.cue_sheet.media_catalog_number, "bogo-MCN");
+               cuesheet->data.cue_sheet.lead_in = 123;
+               cuesheet->data.cue_sheet.is_cd = false;
+               if (!FLAC__metadata_object_cuesheet_insert_blank_track(cuesheet, 0))
+                       return die_("priming our metadata");
+               cuesheet->data.cue_sheet.tracks[0].number = 1;
+               if (!FLAC__metadata_object_cuesheet_track_insert_blank_index(cuesheet, 0, 0))
+                       return die_("priming our metadata");
+       }
+
+       {
+               picture.is_last = false;
+               picture.type = ::FLAC__METADATA_TYPE_PICTURE;
+               picture.length =
+                       (
+                               FLAC__STREAM_METADATA_PICTURE_TYPE_LEN +
+                               FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN + /* will add the length for the string later */
+                               FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN + /* will add the length for the string later */
+                               FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN +
+                               FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN +
+                               FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN +
+                               FLAC__STREAM_METADATA_PICTURE_COLORS_LEN +
+                               FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN /* will add the length for the data later */
+                       ) / 8
+               ;
+               picture.data.picture.type = ::FLAC__STREAM_METADATA_PICTURE_TYPE_FRONT_COVER;
+               picture.data.picture.mime_type = strdup_or_die_("image/jpeg");
+               picture.length += strlen(picture.data.picture.mime_type);
+               picture.data.picture.description = (FLAC__byte*)strdup_or_die_("desc");
+               picture.length += strlen((const char *)picture.data.picture.description);
+               picture.data.picture.width = 300;
+               picture.data.picture.height = 300;
+               picture.data.picture.depth = 24;
+               picture.data.picture.colors = 0;
+               picture.data.picture.data = (FLAC__byte*)strdup_or_die_("SOMEJPEGDATA");
+               picture.data.picture.data_length = strlen((const char *)picture.data.picture.data);
+               picture.length += picture.data.picture.data_length;
+       }
+
        padding.is_last = true;
        padding.type = ::FLAC__METADATA_TYPE_PADDING;
        padding.length = 1234;
 
-       metadata[0] = &padding;
+       metadata[n++] = &vorbiscomment;
+       if(include_extras) {
+               metadata[n++] = cuesheet;
+               metadata[n++] = &picture;
+       }
+       metadata[n++] = &padding;
 
        FLAC::Metadata::StreamInfo s(&streaminfo);
        FLAC::Metadata::VorbisComment v(&vorbiscomment);
+       FLAC::Metadata::CueSheet c(cuesheet, /*copy=*/false);
+       FLAC::Metadata::Picture pi(&picture);
        FLAC::Metadata::Padding p(&padding);
        if(
-               !insert_to_our_metadata_(&s, 0, /*copy=*/true) ||
-               !insert_to_our_metadata_(&v, 1, /*copy=*/true) ||
-               !insert_to_our_metadata_(&p, 2, /*copy=*/true)
+               !insert_to_our_metadata_(&s, i++, /*copy=*/true) ||
+               !insert_to_our_metadata_(&v, i++, /*copy=*/true) ||
+               (include_extras && !insert_to_our_metadata_(&c, i++, /*copy=*/true)) ||
+               (include_extras && !insert_to_our_metadata_(&pi, i++, /*copy=*/true)) ||
+               !insert_to_our_metadata_(&p, i++, /*copy=*/true)
        )
                return die_("priming our metadata");
 
-       if(!file_utils__generate_flacfile(flacfile_, 0, 512 * 1024, &streaminfo, metadata, 1))
+       if(!file_utils__generate_flacfile(is_ogg, flacfilename(is_ogg), 0, 512 * 1024, &streaminfo, metadata, n))
                return die_("creating the encoded file");
 
        free(vorbiscomment.data.vorbis_comment.vendor_string.entry);
+       free(picture.data.picture.mime_type);
+       free(picture.data.picture.description);
+       free(picture.data.picture.data);
 
        return true;
 }
 
-static bool test_file_(const char *filename, bool ignore_metadata)
+static bool test_file_(bool is_ogg, bool ignore_metadata)
 {
+       const char *filename = flacfilename(is_ogg);
        OurFileDecoder decoder(ignore_metadata);
 
-       FLAC__ASSERT(0 != filename);
-
        mc_our_block_number_ = 0;
        decoder.error_occurred_ = false;
 
@@ -338,18 +623,17 @@ static bool test_file_(const char *filename, bool ignore_metadata)
                return die_("couldn't allocate decoder instance");
 
        decoder.set_md5_checking(true);
-       decoder.set_filename(filename);
        decoder.set_metadata_respond_all();
-       if(decoder.init() != ::FLAC__FILE_DECODER_OK) {
-               decoder.finish();
+       if((is_ogg? decoder.init_ogg(filename) : decoder.init(filename)) != ::FLAC__STREAM_DECODER_INIT_STATUS_OK) {
+               (void)decoder.finish();
                return die_("initializing decoder\n");
        }
-       if(!decoder.process_until_end_of_file()) {
-               decoder.finish();
+       if(!decoder.process_until_end_of_stream()) {
+               (void)decoder.finish();
                return die_("decoding file\n");
        }
 
-       decoder.finish();
+       (void)decoder.finish();
 
        if(decoder.error_occurred_)
                return false;
@@ -363,8 +647,8 @@ static bool test_file_(const char *filename, bool ignore_metadata)
 
 static bool change_stats_(const char *filename, bool read_only)
 {
-       if(!FLAC__file_utils_change_stats(filename, read_only))
-               return die_("during FLAC__file_utils_change_stats()");
+       if(!grabbag__file_change_stats(filename, read_only))
+               return die_("during grabbag__file_change_stats()");
 
        return true;
 }
@@ -374,7 +658,7 @@ static bool remove_file_(const char *filename)
        while(our_metadata_.num_blocks > 0)
                delete_from_our_metadata_(0);
 
-       if(!FLAC__file_utils_remove_file(filename))
+       if(!grabbag__file_remove_file(filename))
                return die_("removing file");
 
        return true;
@@ -386,13 +670,15 @@ static bool test_level_0_()
 
        printf("\n\n++++++ testing level 0 interface\n");
 
-       if(!generate_file_())
+       if(!generate_file_(/*include_extras=*/true, /*is_ogg=*/false))
                return false;
 
-       if(!test_file_(flacfile_, /*ignore_metadata=*/true))
+       if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/true))
                return false;
 
-       if(!FLAC::Metadata::get_streaminfo(flacfile_, streaminfo))
+       printf("testing FLAC::Metadata::get_streaminfo()... ");
+
+       if(!FLAC::Metadata::get_streaminfo(flacfilename(/*is_ogg=*/false), streaminfo))
                return die_("during FLAC::Metadata::get_streaminfo()");
 
        /* check to see if some basic data matches (c.f. generate_file_()) */
@@ -407,7 +693,105 @@ static bool test_level_0_()
        if(streaminfo.get_max_blocksize() != 576)
                return die_("mismatch in streaminfo.get_max_blocksize()");
 
-       if(!remove_file_(flacfile_))
+       printf("OK\n");
+
+       {
+               printf("testing FLAC::Metadata::get_tags(VorbisComment *&)... ");
+
+               FLAC::Metadata::VorbisComment *tags = 0;
+
+               if(!FLAC::Metadata::get_tags(flacfilename(/*is_ogg=*/false), tags))
+                       return die_("during FLAC::Metadata::get_tags()");
+
+               /* check to see if some basic data matches (c.f. generate_file_()) */
+               if(tags->get_num_comments() != 0)
+                       return die_("mismatch in tags->get_num_comments()");
+
+               printf("OK\n");
+
+               delete tags;
+       }
+
+       {
+               printf("testing FLAC::Metadata::get_tags(VorbisComment &)... ");
+
+               FLAC::Metadata::VorbisComment tags;
+
+               if(!FLAC::Metadata::get_tags(flacfilename(/*is_ogg=*/false), tags))
+                       return die_("during FLAC::Metadata::get_tags()");
+
+               /* check to see if some basic data matches (c.f. generate_file_()) */
+               if(tags.get_num_comments() != 0)
+                       return die_("mismatch in tags.get_num_comments()");
+
+               printf("OK\n");
+       }
+
+       {
+               printf("testing FLAC::Metadata::get_cuesheet(CueSheet *&)... ");
+
+               FLAC::Metadata::CueSheet *cuesheet = 0;
+
+               if(!FLAC::Metadata::get_cuesheet(flacfilename(/*is_ogg=*/false), cuesheet))
+                       return die_("during FLAC::Metadata::get_cuesheet()");
+
+               /* check to see if some basic data matches (c.f. generate_file_()) */
+               if(cuesheet->get_lead_in() != 123)
+                       return die_("mismatch in cuesheet->get_lead_in()");
+
+               printf("OK\n");
+
+               delete cuesheet;
+       }
+
+       {
+               printf("testing FLAC::Metadata::get_cuesheet(CueSheet &)... ");
+
+               FLAC::Metadata::CueSheet cuesheet;
+
+               if(!FLAC::Metadata::get_cuesheet(flacfilename(/*is_ogg=*/false), cuesheet))
+                       return die_("during FLAC::Metadata::get_cuesheet()");
+
+               /* check to see if some basic data matches (c.f. generate_file_()) */
+               if(cuesheet.get_lead_in() != 123)
+                       return die_("mismatch in cuesheet.get_lead_in()");
+
+               printf("OK\n");
+       }
+
+       {
+               printf("testing FLAC::Metadata::get_picture(Picture *&)... ");
+
+               FLAC::Metadata::Picture *picture = 0;
+
+               if(!FLAC::Metadata::get_picture(flacfilename(/*is_ogg=*/false), picture, /*type=*/(::FLAC__StreamMetadata_Picture_Type)(-1), /*mime_type=*/0, /*description=*/0, /*max_width=*/(unsigned)(-1), /*max_height=*/(unsigned)(-1), /*max_depth=*/(unsigned)(-1), /*max_colors=*/(unsigned)(-1)))
+                       return die_("during FLAC::Metadata::get_picture()");
+
+               /* check to see if some basic data matches (c.f. generate_file_()) */
+               if(picture->get_type () != ::FLAC__STREAM_METADATA_PICTURE_TYPE_FRONT_COVER)
+                       return die_("mismatch in picture->get_type ()");
+
+               printf("OK\n");
+
+               delete picture;
+       }
+
+       {
+               printf("testing FLAC::Metadata::get_picture(Picture &)... ");
+
+               FLAC::Metadata::Picture picture;
+
+               if(!FLAC::Metadata::get_picture(flacfilename(/*is_ogg=*/false), picture, /*type=*/(::FLAC__StreamMetadata_Picture_Type)(-1), /*mime_type=*/0, /*description=*/0, /*max_width=*/(unsigned)(-1), /*max_height=*/(unsigned)(-1), /*max_depth=*/(unsigned)(-1), /*max_colors=*/(unsigned)(-1)))
+                       return die_("during FLAC::Metadata::get_picture()");
+
+               /* check to see if some basic data matches (c.f. generate_file_()) */
+               if(picture.get_type () != ::FLAC__STREAM_METADATA_PICTURE_TYPE_FRONT_COVER)
+                       return die_("mismatch in picture->get_type ()");
+
+               printf("OK\n");
+       }
+
+       if(!remove_file_(flacfilename(/*is_ogg=*/false)))
                return false;
 
        return true;
@@ -422,19 +806,22 @@ static bool test_level_1_()
        FLAC__byte data[1000];
        unsigned our_current_position = 0;
 
+       // initialize 'data' to avoid Valgrind errors
+       memset(data, 0, sizeof(data));
+
        printf("\n\n++++++ testing level 1 interface\n");
 
        /************************************************************/
        {
        printf("simple iterator on read-only file\n");
 
-       if(!generate_file_())
+       if(!generate_file_(/*include_extras=*/false, /*is_ogg=*/false))
                return false;
 
-       if(!change_stats_(flacfile_, /*read_only=*/true))
+       if(!change_stats_(flacfilename(/*is_ogg=*/false), /*read_only=*/true))
                return false;
 
-       if(!test_file_(flacfile_, /*ignore_metadata=*/true))
+       if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/true))
                return false;
 
        FLAC::Metadata::SimpleIterator iterator;
@@ -442,7 +829,7 @@ static bool test_level_1_()
        if(!iterator.is_valid())
                return die_("iterator.is_valid() returned false");
 
-       if(!iterator.init(flacfile_, /*read_only=*/false, /*preserve_file_stats=*/false))
+       if(!iterator.init(flacfilename(/*is_ogg=*/false), /*read_only=*/false, /*preserve_file_stats=*/false))
                return die_("iterator.init() returned false");
 
        printf("is writable = %u\n", (unsigned)iterator.is_writable());
@@ -474,6 +861,7 @@ static bool test_level_1_()
                return die_("mismatch in min_blocksize");
        if(streaminfo->get_max_blocksize() != 576)
                return die_("mismatch in max_blocksize");
+       // we will delete streaminfo a little later when we're really done with it...
 
        if(!iterator.next())
                return die_("forward iterator ended early");
@@ -494,6 +882,7 @@ static bool test_level_1_()
        /* check to see if some basic data matches (c.f. generate_file_()) */
        if(block->get_length() != 1234)
                return die_("bad PADDING length");
+       delete block;
 
        if(iterator.next())
                return die_("forward iterator returned true but should have returned false");
@@ -512,13 +901,14 @@ static bool test_level_1_()
                printf("PASSED.  iterator.set_block() returned false like it should\n");
        else
                return die_("iterator.set_block() returned true but shouldn't have");
+       delete streaminfo;
        }
 
        /************************************************************/
        {
        printf("simple iterator on writable file\n");
 
-       if(!change_stats_(flacfile_, /*read-only=*/false))
+       if(!change_stats_(flacfilename(/*is_ogg=*/false), /*read-only=*/false))
                return false;
 
        printf("creating APPLICATION block\n");
@@ -538,7 +928,7 @@ static bool test_level_1_()
        if(!iterator.is_valid())
                return die_("iterator.is_valid() returned false");
 
-       if(!iterator.init(flacfile_, /*read_only=*/false, /*preserve_file_stats=*/false))
+       if(!iterator.init(flacfilename(/*is_ogg=*/false), /*read_only=*/false, /*preserve_file_stats=*/false))
                return die_("iterator.init() returned false");
        our_current_position = 0;
 
@@ -584,7 +974,7 @@ static bool test_level_1_()
        if(!insert_to_our_metadata_(padding, ++our_current_position, /*copy=*/true))
                return false;
 
-       if(!test_file_(flacfile_, /*ignore_metadata=*/false))
+       if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false))
                return false;
 
        printf("SV[P]PP\tprev\n");
@@ -601,7 +991,7 @@ static bool test_level_1_()
        if(iterator.delete_block(false))
                return die_ss_("iterator.delete_block(false) should have returned false", iterator);
 
-       if(!test_file_(flacfile_, /*ignore_metadata=*/false))
+       if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false))
                return false;
 
        printf("[S]VPPP\tnext\n");
@@ -629,7 +1019,7 @@ static bool test_level_1_()
                return die_ss_("iterator.delete_block(false)", iterator);
        delete_from_our_metadata_(our_current_position--);
 
-       if(!test_file_(flacfile_, /*ignore_metadata=*/false))
+       if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false))
                return false;
 
        printf("S[V]PP\tnext\n");
@@ -647,7 +1037,7 @@ static bool test_level_1_()
                return die_ss_("iterator.delete_block(false)", iterator);
        our_current_position--;
 
-       if(!test_file_(flacfile_, /*ignore_metadata=*/false))
+       if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false))
                return false;
 
        printf("SV[P]P\tnext\n");
@@ -660,7 +1050,7 @@ static bool test_level_1_()
                return die_ss_("iterator.delete_block(false)", iterator);
        delete_from_our_metadata_(our_current_position--);
 
-       if(!test_file_(flacfile_, /*ignore_metadata=*/false))
+       if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false))
                return false;
 
        printf("SV[P]\tprev\n");
@@ -685,7 +1075,7 @@ static bool test_level_1_()
                return die_ss_("iterator.set_block(block, false)", iterator);
        delete block;
 
-       if(!test_file_(flacfile_, /*ignore_metadata=*/false))
+       if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false))
                return false;
 
        printf("[S]VP\tnext\n");
@@ -701,7 +1091,7 @@ static bool test_level_1_()
                return false;
        add_to_padding_length_(our_current_position+1, -((int)(FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8) + (int)app->get_length()));
 
-       if(!test_file_(flacfile_, /*ignore_metadata=*/false))
+       if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false))
                return false;
 
        printf("SV[A]P\tnext\n");
@@ -717,7 +1107,7 @@ static bool test_level_1_()
                return false;
        add_to_padding_length_(our_current_position+1, -((int)(FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8) + (int)app->get_length()));
 
-       if(!test_file_(flacfile_, /*ignore_metadata=*/false))
+       if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false))
                return false;
 
        printf("SVA[A]P\tset APPLICATION (grow), don't expand into padding\n");
@@ -729,7 +1119,7 @@ static bool test_level_1_()
        if(!iterator.set_block(app, false))
                return die_ss_("iterator.set_block(app, false)", iterator);
 
-       if(!test_file_(flacfile_, /*ignore_metadata=*/false))
+       if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false))
                return false;
 
        printf("SVA[A]P\tset APPLICATION (shrink), don't fill in with padding\n");
@@ -741,7 +1131,7 @@ static bool test_level_1_()
        if(!iterator.set_block(app, false))
                return die_ss_("iterator.set_block(app, false)", iterator);
 
-       if(!test_file_(flacfile_, /*ignore_metadata=*/false))
+       if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false))
                return false;
 
        printf("SVA[A]P\tset APPLICATION (grow), expand into padding of exceeding size\n");
@@ -754,7 +1144,7 @@ static bool test_level_1_()
        if(!iterator.set_block(app, true))
                return die_ss_("iterator.set_block(app, true)", iterator);
 
-       if(!test_file_(flacfile_, /*ignore_metadata=*/false))
+       if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false))
                return false;
 
        printf("SVA[A]P\tset APPLICATION (shrink), fill in with padding\n");
@@ -769,7 +1159,7 @@ static bool test_level_1_()
        if(!iterator.set_block(app, true))
                return die_ss_("iterator.set_block(app, true)", iterator);
 
-       if(!test_file_(flacfile_, /*ignore_metadata=*/false))
+       if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false))
                return false;
 
        printf("SVA[A]PP\tnext\n");
@@ -789,7 +1179,7 @@ static bool test_level_1_()
        if(!iterator.set_block(padding, false))
                return die_ss_("iterator.set_block(padding, false)", iterator);
 
-       if(!test_file_(flacfile_, /*ignore_metadata=*/false))
+       if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false))
                return false;
 
        printf("SVAAP[P]\tset APPLICATION (grow)\n");
@@ -799,7 +1189,7 @@ static bool test_level_1_()
        if(!iterator.set_block(app, false))
                return die_ss_("iterator.set_block(app, false)", iterator);
 
-       if(!test_file_(flacfile_, /*ignore_metadata=*/false))
+       if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false))
                return false;
 
        printf("SVAAP[A]\tset PADDING (equal)\n");
@@ -809,7 +1199,7 @@ static bool test_level_1_()
        if(!iterator.set_block(padding, false))
                return die_ss_("iterator.set_block(padding, false)", iterator);
 
-       if(!test_file_(flacfile_, /*ignore_metadata=*/false))
+       if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false))
                return false;
 
        printf("SVAAP[P]\tprev\n");
@@ -822,7 +1212,7 @@ static bool test_level_1_()
                return die_ss_("iterator.delete_block(false)", iterator);
        delete_from_our_metadata_(our_current_position--);
 
-       if(!test_file_(flacfile_, /*ignore_metadata=*/false))
+       if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false))
                return false;
 
        printf("SVA[A]P\tdelete (middle block), don't replace with padding\n");
@@ -830,7 +1220,7 @@ static bool test_level_1_()
                return die_ss_("iterator.delete_block(false)", iterator);
        delete_from_our_metadata_(our_current_position--);
 
-       if(!test_file_(flacfile_, /*ignore_metadata=*/false))
+       if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false))
                return false;
 
        printf("SV[A]P\tnext\n");
@@ -845,7 +1235,7 @@ static bool test_level_1_()
        if(!insert_to_our_metadata_(padding, ++our_current_position, /*copy=*/true))
                return false;
 
-       if(!test_file_(flacfile_, /*ignore_metadata=*/false))
+       if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false))
                return false;
 
        printf("SVAP[P]\tprev\n");
@@ -866,7 +1256,7 @@ static bool test_level_1_()
        if(!iterator.set_block(app, true))
                return die_ss_("iterator.set_block(app, true)", iterator);
 
-       if(!test_file_(flacfile_, /*ignore_metadata=*/false))
+       if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false))
                return false;
 
        printf("SV[A]PP\tset APPLICATION (grow), try to expand into padding which is 'close' but still too small\n");
@@ -877,7 +1267,7 @@ static bool test_level_1_()
        if(!iterator.set_block(app, true))
                return die_ss_("iterator.set_block(app, true)", iterator);
 
-       if(!test_file_(flacfile_, /*ignore_metadata=*/false))
+       if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false))
                return false;
 
        printf("SV[A]PP\tset APPLICATION (grow), expand into padding which will leave 0-length pad\n");
@@ -889,7 +1279,7 @@ static bool test_level_1_()
        if(!iterator.set_block(app, true))
                return die_ss_("iterator.set_block(app, true)", iterator);
 
-       if(!test_file_(flacfile_, /*ignore_metadata=*/false))
+       if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false))
                return false;
 
        printf("SV[A]PP\tset APPLICATION (grow), expand into padding which is exactly consumed\n");
@@ -901,7 +1291,7 @@ static bool test_level_1_()
        if(!iterator.set_block(app, true))
                return die_ss_("iterator.set_block(app, true)", iterator);
 
-       if(!test_file_(flacfile_, /*ignore_metadata=*/false))
+       if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false))
                return false;
 
        printf("SV[A]P\tset APPLICATION (grow), expand into padding which is exactly consumed\n");
@@ -914,7 +1304,7 @@ static bool test_level_1_()
        if(!iterator.set_block(app, true))
                return die_ss_("iterator.set_block(app, true)", iterator);
 
-       if(!test_file_(flacfile_, /*ignore_metadata=*/false))
+       if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false))
                return false;
 
        printf("SV[A]\tset PADDING (equal size)\n");
@@ -924,7 +1314,7 @@ static bool test_level_1_()
        if(!iterator.set_block(padding, true))
                return die_ss_("iterator.set_block(padding, true)", iterator);
 
-       if(!test_file_(flacfile_, /*ignore_metadata=*/false))
+       if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false))
                return false;
 
        printf("SV[P]\tinsert PADDING after\n");
@@ -933,7 +1323,7 @@ static bool test_level_1_()
        if(!insert_to_our_metadata_(padding, ++our_current_position, /*copy=*/true))
                return false;
 
-       if(!test_file_(flacfile_, /*ignore_metadata=*/false))
+       if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false))
                return false;
 
        printf("SVP[P]\tinsert PADDING after\n");
@@ -943,7 +1333,7 @@ static bool test_level_1_()
        if(!insert_to_our_metadata_(padding, ++our_current_position, /*copy=*/true))
                return false;
 
-       if(!test_file_(flacfile_, /*ignore_metadata=*/false))
+       if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false))
                return false;
 
        printf("SVPP[P]\tprev\n");
@@ -969,7 +1359,7 @@ static bool test_level_1_()
        if(!iterator.insert_block_after(app, true))
                return die_ss_("iterator.insert_block_after(app, true)", iterator);
 
-       if(!test_file_(flacfile_, /*ignore_metadata=*/false))
+       if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false))
                return false;
 
        printf("SV[A]PPP\tdelete (middle block), don't replace with padding\n");
@@ -977,7 +1367,7 @@ static bool test_level_1_()
                return die_ss_("iterator.delete_block(false)", iterator);
        delete_from_our_metadata_(our_current_position--);
 
-       if(!test_file_(flacfile_, /*ignore_metadata=*/false))
+       if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false))
                return false;
 
        printf("S[V]PPP\tinsert APPLICATION after, try to expand into padding which is 'close' but still too small\n");
@@ -988,7 +1378,7 @@ static bool test_level_1_()
        if(!iterator.insert_block_after(app, true))
                return die_ss_("iterator.insert_block_after(app, true)", iterator);
 
-       if(!test_file_(flacfile_, /*ignore_metadata=*/false))
+       if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false))
                return false;
 
        printf("SV[A]PPP\tdelete (middle block), don't replace with padding\n");
@@ -996,7 +1386,7 @@ static bool test_level_1_()
                return die_ss_("iterator.delete_block(false)", iterator);
        delete_from_our_metadata_(our_current_position--);
 
-       if(!test_file_(flacfile_, /*ignore_metadata=*/false))
+       if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false))
                return false;
 
        printf("S[V]PPP\tinsert APPLICATION after, expand into padding which is exactly consumed\n");
@@ -1008,7 +1398,7 @@ static bool test_level_1_()
        if(!iterator.insert_block_after(app, true))
                return die_ss_("iterator.insert_block_after(app, true)", iterator);
 
-       if(!test_file_(flacfile_, /*ignore_metadata=*/false))
+       if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false))
                return false;
 
        printf("SV[A]PP\tdelete (middle block), don't replace with padding\n");
@@ -1016,7 +1406,7 @@ static bool test_level_1_()
                return die_ss_("iterator.delete_block(false)", iterator);
        delete_from_our_metadata_(our_current_position--);
 
-       if(!test_file_(flacfile_, /*ignore_metadata=*/false))
+       if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false))
                return false;
 
        printf("S[V]PP\tinsert APPLICATION after, expand into padding which will leave 0-length pad\n");
@@ -1028,7 +1418,7 @@ static bool test_level_1_()
        if(!iterator.insert_block_after(app, true))
                return die_ss_("iterator.insert_block_after(app, true)", iterator);
 
-       if(!test_file_(flacfile_, /*ignore_metadata=*/false))
+       if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false))
                return false;
 
        printf("SV[A]PP\tdelete (middle block), don't replace with padding\n");
@@ -1036,7 +1426,7 @@ static bool test_level_1_()
                return die_ss_("iterator.delete_block(false)", iterator);
        delete_from_our_metadata_(our_current_position--);
 
-       if(!test_file_(flacfile_, /*ignore_metadata=*/false))
+       if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false))
                return false;
 
        printf("S[V]PP\tnext\n");
@@ -1049,7 +1439,7 @@ static bool test_level_1_()
                return die_ss_("iterator.delete_block(false)", iterator);
        delete_from_our_metadata_(our_current_position--);
 
-       if(!test_file_(flacfile_, /*ignore_metadata=*/false))
+       if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false))
                return false;
 
        printf("S[V]P\tinsert APPLICATION after, expand into padding which is exactly consumed\n");
@@ -1061,20 +1451,20 @@ static bool test_level_1_()
        if(!iterator.insert_block_after(app, true))
                return die_ss_("iterator.insert_block_after(app, true)", iterator);
 
-       if(!test_file_(flacfile_, /*ignore_metadata=*/false))
+       if(!test_file_(/*is_ogg=*/false, /*ignore_metadata=*/false))
                return false;
        }
 
        delete app;
        delete padding;
 
-       if(!remove_file_(flacfile_))
+       if(!remove_file_(flacfilename(/*is_ogg=*/false)))
                return false;
 
        return true;
 }
 
-static bool test_level_2_()
+static bool test_level_2_(bool filename_based, bool is_ogg)
 {
        FLAC::Metadata::Prototype *block;
        FLAC::Metadata::StreamInfo *streaminfo;
@@ -1083,14 +1473,17 @@ static bool test_level_2_()
        FLAC__byte data[2000];
        unsigned our_current_position;
 
-       printf("\n\n++++++ testing level 2 interface\n");
+       // initialize 'data' to avoid Valgrind errors
+       memset(data, 0, sizeof(data));
+
+       printf("\n\n++++++ testing level 2 interface (%s-based, %s FLAC)\n", filename_based? "filename":"callback", is_ogg? "Ogg":"native");
 
        printf("generate read-only file\n");
 
-       if(!generate_file_())
+       if(!generate_file_(/*include_extras=*/false, is_ogg))
                return false;
 
-       if(!change_stats_(flacfile_, /*read_only=*/true))
+       if(!change_stats_(flacfilename(is_ogg), /*read_only=*/true))
                return false;
 
        printf("create chain\n");
@@ -1100,19 +1493,22 @@ static bool test_level_2_()
 
        printf("read chain\n");
 
-       if(!chain.read(flacfile_))
+       if(!read_chain_(chain, flacfilename(is_ogg), filename_based, is_ogg))
                return die_c_("reading chain", chain.status());
 
        printf("[S]VP\ttest initial metadata\n");
 
        if(!compare_chain_(chain, 0, 0))
                return false;
-       if(!test_file_(flacfile_, /*ignore_metadata=*/false))
+       if(!test_file_(is_ogg, /*ignore_metadata=*/false))
                return false;
 
+       if(is_ogg)
+               goto end;
+
        printf("switch file to read-write\n");
 
-       if(!change_stats_(flacfile_, /*read-only=*/false))
+       if(!change_stats_(flacfilename(is_ogg), /*read-only=*/false))
                return false;
 
        printf("create iterator\n");
@@ -1137,12 +1533,15 @@ static bool test_level_2_()
        streaminfo->set_sample_rate(32000);
        if(!replace_in_our_metadata_(block, our_current_position, /*copy=*/true))
                return die_("copying object");
+       delete block;
 
-       if(!chain.write(/*use_padding=*/false, /*preserve_file_stats=*/true))
+       if(!write_chain_(chain, /*use_padding=*/false, /*preserve_file_stats=*/true, filename_based, flacfilename(is_ogg)))
                return die_c_("during chain.write(false, true)", chain.status());
-       if(!compare_chain_(chain, our_current_position, iterator.get_block()))
+       block = iterator.get_block();
+       if(!compare_chain_(chain, our_current_position, block))
                return false;
-       if(!test_file_(flacfile_, /*ignore_metadata=*/false))
+       delete block;
+       if(!test_file_(is_ogg, /*ignore_metadata=*/false))
                return false;
 
        printf("[S]VP\tnext\n");
@@ -1163,16 +1562,19 @@ static bool test_level_2_()
        app->set_id((const unsigned char *)"duh");
        if(!app->set_data(data, block->get_length()-(FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), true))
                return die_("setting APPLICATION data");
+       delete block;
        if(!replace_in_our_metadata_(app, our_current_position, /*copy=*/true))
                return die_("copying object");
        if(!iterator.set_block(app))
                return die_c_("iterator.set_block(app)", chain.status());
 
-       if(!chain.write(/*use_padding=*/false, /*preserve_file_stats=*/false))
+       if(!write_chain_(chain, /*use_padding=*/false, /*preserve_file_stats=*/false, filename_based, flacfilename(is_ogg)))
                return die_c_("during chain.write(false, false)", chain.status());
-       if(!compare_chain_(chain, our_current_position, iterator.get_block()))
+       block = iterator.get_block();
+       if(!compare_chain_(chain, our_current_position, block))
                return false;
-       if(!test_file_(flacfile_, /*ignore_metadata=*/false))
+       delete block;
+       if(!test_file_(is_ogg, /*ignore_metadata=*/false))
                return false;
 
        printf("SV[A]\tshrink APPLICATION, don't use padding\n");
@@ -1185,11 +1587,13 @@ static bool test_level_2_()
        if(!iterator.set_block(app))
                return die_c_("iterator.set_block(app)", chain.status());
 
-       if(!chain.write(/*use_padding=*/false, /*preserve_file_stats=*/false))
+       if(!write_chain_(chain, /*use_padding=*/false, /*preserve_file_stats=*/false, filename_based, flacfilename(is_ogg)))
                return die_c_("during chain.write(false, false)", chain.status());
-       if(!compare_chain_(chain, our_current_position, iterator.get_block()))
+       block = iterator.get_block();
+       if(!compare_chain_(chain, our_current_position, block))
                return false;
-       if(!test_file_(flacfile_, /*ignore_metadata=*/false))
+       delete block;
+       if(!test_file_(is_ogg, /*ignore_metadata=*/false))
                return false;
 
        printf("SV[A]\tgrow APPLICATION, don't use padding\n");
@@ -1202,11 +1606,13 @@ static bool test_level_2_()
        if(!iterator.set_block(app))
                return die_c_("iterator.set_block(app)", chain.status());
 
-       if(!chain.write(/*use_padding=*/false, /*preserve_file_stats=*/false))
+       if(!write_chain_(chain, /*use_padding=*/false, /*preserve_file_stats=*/false, filename_based, flacfilename(is_ogg)))
                return die_c_("during chain.write(false, false)", chain.status());
-       if(!compare_chain_(chain, our_current_position, iterator.get_block()))
+       block = iterator.get_block();
+       if(!compare_chain_(chain, our_current_position, block))
                return false;
-       if(!test_file_(flacfile_, /*ignore_metadata=*/false))
+       delete block;
+       if(!test_file_(is_ogg, /*ignore_metadata=*/false))
                return false;
 
        printf("SV[A]\tgrow APPLICATION, use padding, but last block is not padding\n");
@@ -1219,11 +1625,13 @@ static bool test_level_2_()
        if(!iterator.set_block(app))
                return die_c_("iterator.set_block(app)", chain.status());
 
-       if(!chain.write(/*use_padding=*/false, /*preserve_file_stats=*/false))
+       if(!write_chain_(chain, /*use_padding=*/false, /*preserve_file_stats=*/false, filename_based, flacfilename(is_ogg)))
                return die_c_("during chain.write(false, false)", chain.status());
-       if(!compare_chain_(chain, our_current_position, iterator.get_block()))
+       block = iterator.get_block();
+       if(!compare_chain_(chain, our_current_position, block))
                return false;
-       if(!test_file_(flacfile_, /*ignore_metadata=*/false))
+       delete block;
+       if(!test_file_(is_ogg, /*ignore_metadata=*/false))
                return false;
 
        printf("SV[A]\tshrink APPLICATION, use padding, last block is not padding, but delta is too small for new PADDING block\n");
@@ -1236,11 +1644,13 @@ static bool test_level_2_()
        if(!iterator.set_block(app))
                return die_c_("iterator.set_block(app)", chain.status());
 
-       if(!chain.write(/*use_padding=*/true, /*preserve_file_stats=*/false))
+       if(!write_chain_(chain, /*use_padding=*/true, /*preserve_file_stats=*/false, filename_based, flacfilename(is_ogg)))
                return die_c_("during chain.write(true, false)", chain.status());
-       if(!compare_chain_(chain, our_current_position, iterator.get_block()))
+       block = iterator.get_block();
+       if(!compare_chain_(chain, our_current_position, block))
                return false;
-       if(!test_file_(flacfile_, /*ignore_metadata=*/false))
+       delete block;
+       if(!test_file_(is_ogg, /*ignore_metadata=*/false))
                return false;
 
        printf("SV[A]\tshrink APPLICATION, use padding, last block is not padding, delta is enough for new PADDING block\n");
@@ -1258,11 +1668,13 @@ static bool test_level_2_()
        if(!iterator.set_block(app))
                return die_c_("iterator.set_block(app)", chain.status());
 
-       if(!chain.write(/*use_padding=*/true, /*preserve_file_stats=*/false))
+       if(!write_chain_(chain, /*use_padding=*/true, /*preserve_file_stats=*/false, filename_based, flacfilename(is_ogg)))
                return die_c_("during chain.write(true, false)", chain.status());
-       if(!compare_chain_(chain, our_current_position, iterator.get_block()))
+       block = iterator.get_block();
+       if(!compare_chain_(chain, our_current_position, block))
                return false;
-       if(!test_file_(flacfile_, /*ignore_metadata=*/false))
+       delete block;
+       if(!test_file_(is_ogg, /*ignore_metadata=*/false))
                return false;
 
        printf("SV[A]P\tshrink APPLICATION, use padding, last block is padding\n");
@@ -1276,11 +1688,13 @@ static bool test_level_2_()
        if(!iterator.set_block(app))
                return die_c_("iterator.set_block(app)", chain.status());
 
-       if(!chain.write(/*use_padding=*/true, /*preserve_file_stats=*/false))
+       if(!write_chain_(chain, /*use_padding=*/true, /*preserve_file_stats=*/false, filename_based, flacfilename(is_ogg)))
                return die_c_("during chain.write(true, false)", chain.status());
-       if(!compare_chain_(chain, our_current_position, iterator.get_block()))
+       block = iterator.get_block();
+       if(!compare_chain_(chain, our_current_position, block))
                return false;
-       if(!test_file_(flacfile_, /*ignore_metadata=*/false))
+       delete block;
+       if(!test_file_(is_ogg, /*ignore_metadata=*/false))
                return false;
 
        printf("SV[A]P\tgrow APPLICATION, use padding, last block is padding, but delta is too small\n");
@@ -1293,11 +1707,13 @@ static bool test_level_2_()
        if(!iterator.set_block(app))
                return die_c_("iterator.set_block(app)", chain.status());
 
-       if(!chain.write(/*use_padding=*/true, /*preserve_file_stats=*/false))
+       if(!write_chain_(chain, /*use_padding=*/true, /*preserve_file_stats=*/false, filename_based, flacfilename(is_ogg)))
                return die_c_("during chain.write(true, false)", chain.status());
-       if(!compare_chain_(chain, our_current_position, iterator.get_block()))
+       block = iterator.get_block();
+       if(!compare_chain_(chain, our_current_position, block))
                return false;
-       if(!test_file_(flacfile_, /*ignore_metadata=*/false))
+       delete block;
+       if(!test_file_(is_ogg, /*ignore_metadata=*/false))
                return false;
 
        printf("SV[A]P\tgrow APPLICATION, use padding, last block is padding of exceeding size\n");
@@ -1311,11 +1727,13 @@ static bool test_level_2_()
        if(!iterator.set_block(app))
                return die_c_("iterator.set_block(app)", chain.status());
 
-       if(!chain.write(/*use_padding=*/true, /*preserve_file_stats=*/false))
+       if(!write_chain_(chain, /*use_padding=*/true, /*preserve_file_stats=*/false, filename_based, flacfilename(is_ogg)))
                return die_c_("during chain.write(true, false)", chain.status());
-       if(!compare_chain_(chain, our_current_position, iterator.get_block()))
+       block = iterator.get_block();
+       if(!compare_chain_(chain, our_current_position, block))
                return false;
-       if(!test_file_(flacfile_, /*ignore_metadata=*/false))
+       delete block;
+       if(!test_file_(is_ogg, /*ignore_metadata=*/false))
                return false;
 
        printf("SV[A]P\tgrow APPLICATION, use padding, last block is padding of exact size\n");
@@ -1329,11 +1747,13 @@ static bool test_level_2_()
        if(!iterator.set_block(app))
                return die_c_("iterator.set_block(app)", chain.status());
 
-       if(!chain.write(/*use_padding=*/true, /*preserve_file_stats=*/false))
+       if(!write_chain_(chain, /*use_padding=*/true, /*preserve_file_stats=*/false, filename_based, flacfilename(is_ogg)))
                return die_c_("during chain.write(true, false)", chain.status());
-       if(!compare_chain_(chain, our_current_position, iterator.get_block()))
+       block = iterator.get_block();
+       if(!compare_chain_(chain, our_current_position, block))
                return false;
-       if(!test_file_(flacfile_, /*ignore_metadata=*/false))
+       delete block;
+       if(!test_file_(is_ogg, /*ignore_metadata=*/false))
                return false;
 
        printf("SV[A]\tprev\n");
@@ -1366,8 +1786,10 @@ static bool test_level_2_()
        if(!iterator.insert_block_after(padding))
                return die_("iterator.insert_block_after(padding)");
 
-       if(!compare_chain_(chain, our_current_position, iterator.get_block()))
+       block = iterator.get_block();
+       if(!compare_chain_(chain, our_current_position, block))
                return false;
+       delete block;
 
        printf("SV[P]A\tinsert PADDING before\n");
        if(0 == (padding = dynamic_cast<FLAC::Metadata::Padding *>(FLAC::Metadata::clone(our_metadata_.blocks[our_current_position]))))
@@ -1378,8 +1800,10 @@ static bool test_level_2_()
        if(!iterator.insert_block_before(padding))
                return die_("iterator.insert_block_before(padding)");
 
-       if(!compare_chain_(chain, our_current_position, iterator.get_block()))
+       block = iterator.get_block();
+       if(!compare_chain_(chain, our_current_position, block))
                return false;
+       delete block;
 
        printf("SV[P]PA\tinsert PADDING before\n");
        if(0 == (padding = dynamic_cast<FLAC::Metadata::Padding *>(FLAC::Metadata::clone(our_metadata_.blocks[our_current_position]))))
@@ -1390,8 +1814,10 @@ static bool test_level_2_()
        if(!iterator.insert_block_before(padding))
                return die_("iterator.insert_block_before(padding)");
 
-       if(!compare_chain_(chain, our_current_position, iterator.get_block()))
+       block = iterator.get_block();
+       if(!compare_chain_(chain, our_current_position, block))
                return false;
+       delete block;
 
        printf("SV[P]PPA\tnext\n");
        if(!iterator.next())
@@ -1417,8 +1843,10 @@ static bool test_level_2_()
        if(!iterator.insert_block_after(padding))
                return die_("iterator.insert_block_after(padding)");
 
-       if(!compare_chain_(chain, our_current_position, iterator.get_block()))
+       block = iterator.get_block();
+       if(!compare_chain_(chain, our_current_position, block))
                return false;
+       delete block;
 
        printf("SVPPPA[P]\tinsert PADDING before\n");
        if(0 == (padding = dynamic_cast<FLAC::Metadata::Padding *>(FLAC::Metadata::clone(our_metadata_.blocks[2]))))
@@ -1429,8 +1857,10 @@ static bool test_level_2_()
        if(!iterator.insert_block_before(padding))
                return die_("iterator.insert_block_before(padding)");
 
-       if(!compare_chain_(chain, our_current_position, iterator.get_block()))
+       block = iterator.get_block();
+       if(!compare_chain_(chain, our_current_position, block))
                return false;
+       delete block;
 
        }
        our_current_position = 0;
@@ -1444,11 +1874,11 @@ static bool test_level_2_()
        delete_from_our_metadata_(4);
        delete_from_our_metadata_(3);
 
-       if(!chain.write(/*use_padding=*/true, /*preserve_file_stats=*/false))
+       if(!write_chain_(chain, /*use_padding=*/true, /*preserve_file_stats=*/false, filename_based, flacfilename(is_ogg)))
                return die_c_("during chain.write(true, false)", chain.status());
        if(!compare_chain_(chain, 0, 0))
                return false;
-       if(!test_file_(flacfile_, /*ignore_metadata=*/false))
+       if(!test_file_(is_ogg, /*ignore_metadata=*/false))
                return false;
 
        printf("SVPAP\tsort padding\n");
@@ -1456,11 +1886,11 @@ static bool test_level_2_()
        add_to_padding_length_(4, FLAC__STREAM_METADATA_HEADER_LENGTH + our_metadata_.blocks[2]->get_length());
        delete_from_our_metadata_(2);
 
-       if(!chain.write(/*use_padding=*/true, /*preserve_file_stats=*/false))
+       if(!write_chain_(chain, /*use_padding=*/true, /*preserve_file_stats=*/false, filename_based, flacfilename(is_ogg)))
                return die_c_("during chain.write(true, false)", chain.status());
        if(!compare_chain_(chain, 0, 0))
                return false;
-       if(!test_file_(flacfile_, /*ignore_metadata=*/false))
+       if(!test_file_(is_ogg, /*ignore_metadata=*/false))
                return false;
 
        printf("create iterator\n");
@@ -1492,8 +1922,10 @@ static bool test_level_2_()
        if(!iterator.delete_block(/*replace_with_padding=*/true))
                return die_c_("iterator.delete_block(true)", chain.status());
 
-       if(!compare_chain_(chain, our_current_position, iterator.get_block()))
+       block = iterator.get_block();
+       if(!compare_chain_(chain, our_current_position, block))
                return false;
+       delete block;
 
        printf("S[V]PP\tnext\n");
        if(!iterator.next())
@@ -1505,8 +1937,10 @@ static bool test_level_2_()
        if(!iterator.delete_block(/*replace_with_padding=*/false))
                return die_c_("iterator.delete_block(false)", chain.status());
 
-       if(!compare_chain_(chain, our_current_position, iterator.get_block()))
+       block = iterator.get_block();
+       if(!compare_chain_(chain, our_current_position, block))
                return false;
+       delete block;
 
        printf("S[V]P\tnext\n");
        if(!iterator.next())
@@ -1522,8 +1956,10 @@ static bool test_level_2_()
        if(!iterator.delete_block(/*replace_with_padding=*/true))
                return die_c_("iterator.delete_block(true)", chain.status());
 
-       if(!compare_chain_(chain, our_current_position, iterator.get_block()))
+       block = iterator.get_block();
+       if(!compare_chain_(chain, our_current_position, block))
                return false;
+       delete block;
 
        printf("S[V]P\tnext\n");
        if(!iterator.next())
@@ -1535,8 +1971,10 @@ static bool test_level_2_()
        if(!iterator.delete_block(/*replace_with_padding=*/false))
                return die_c_("iterator.delete_block(false)", chain.status());
 
-       if(!compare_chain_(chain, our_current_position, iterator.get_block()))
+       block = iterator.get_block();
+       if(!compare_chain_(chain, our_current_position, block))
                return false;
+       delete block;
 
        printf("S[V]\tprev\n");
        if(!iterator.prev())
@@ -1547,33 +1985,192 @@ static bool test_level_2_()
        if(iterator.delete_block(/*replace_with_padding=*/false))
                return die_("iterator.delete_block() on STREAMINFO should have failed but didn't");
 
-       if(!compare_chain_(chain, our_current_position, iterator.get_block()))
+       block = iterator.get_block();
+       if(!compare_chain_(chain, our_current_position, block))
                return false;
+       delete block;
 
-       }
+       } // delete iterator
        our_current_position = 0;
 
        printf("SV\tmerge padding\n");
        chain.merge_padding();
 
-       if(!chain.write(/*use_padding=*/false, /*preserve_file_stats=*/false))
+       if(!write_chain_(chain, /*use_padding=*/false, /*preserve_file_stats=*/false, filename_based, flacfilename(is_ogg)))
                return die_c_("during chain.write(false, false)", chain.status());
        if(!compare_chain_(chain, 0, 0))
                return false;
-       if(!test_file_(flacfile_, /*ignore_metadata=*/false))
+       if(!test_file_(is_ogg, /*ignore_metadata=*/false))
                return false;
 
        printf("SV\tsort padding\n");
        chain.sort_padding();
 
-       if(!chain.write(/*use_padding=*/false, /*preserve_file_stats=*/false))
+       if(!write_chain_(chain, /*use_padding=*/false, /*preserve_file_stats=*/false, filename_based, flacfilename(is_ogg)))
                return die_c_("during chain.write(false, false)", chain.status());
        if(!compare_chain_(chain, 0, 0))
                return false;
-       if(!test_file_(flacfile_, /*ignore_metadata=*/false))
+       if(!test_file_(is_ogg, /*ignore_metadata=*/false))
                return false;
 
-       if(!remove_file_(flacfile_))
+end:
+       if(!remove_file_(flacfilename(is_ogg)))
+               return false;
+
+       return true;
+}
+
+static bool test_level_2_misc_(bool is_ogg)
+{
+       ::FLAC__IOCallbacks callbacks;
+
+       memset(&callbacks, 0, sizeof(callbacks));
+       callbacks.read = (::FLAC__IOCallback_Read)fread;
+#ifdef FLAC__VALGRIND_TESTING
+       callbacks.write = chain_write_cb_;
+#else
+       callbacks.write = (::FLAC__IOCallback_Write)fwrite;
+#endif
+       callbacks.seek = chain_seek_cb_;
+       callbacks.tell = chain_tell_cb_;
+       callbacks.eof = chain_eof_cb_;
+
+       printf("\n\n++++++ testing level 2 interface (mismatched read/write protections)\n");
+
+       printf("generate file\n");
+
+       if(!generate_file_(/*include_extras=*/false, is_ogg))
+               return false;
+
+       printf("create chain\n");
+       FLAC::Metadata::Chain chain;
+       if(!chain.is_valid())
+               return die_("allocating chain");
+
+       printf("read chain (filename-based)\n");
+
+       if(!chain.read(flacfilename(is_ogg)))
+               return die_c_("reading chain", chain.status());
+
+       printf("write chain with wrong method Chain::write(with callbacks)\n");
+       {
+               if(chain.write(/*use_padding=*/false, 0, callbacks))
+                       return die_c_("mismatched write should have failed", chain.status());
+               if(chain.status() != ::FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH)
+                       return die_c_("expected FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH", chain.status());
+               printf("  OK: Chain::write(with callbacks) returned false,FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH like it should\n");
+       }
+
+       printf("read chain (filename-based)\n");
+
+       if(!chain.read(flacfilename(is_ogg)))
+               return die_c_("reading chain", chain.status());
+
+       printf("write chain with wrong method Chain::write(with callbacks and tempfile)\n");
+       {
+               if(chain.write(/*use_padding=*/false, 0, callbacks, 0, callbacks))
+                       return die_c_("mismatched write should have failed", chain.status());
+               if(chain.status() != ::FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH)
+                       return die_c_("expected FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH", chain.status());
+               printf("  OK: Chain::write(with callbacks and tempfile) returned false,FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH like it should\n");
+       }
+
+       printf("read chain (callback-based)\n");
+       {
+               FILE *file = fopen(flacfilename(is_ogg), "rb");
+               if(0 == file)
+                       return die_("opening file");
+               if(!chain.read((::FLAC__IOHandle)file, callbacks)) {
+                       fclose(file);
+                       return die_c_("reading chain", chain.status());
+               }
+               fclose(file);
+       }
+
+       printf("write chain with wrong method write()\n");
+       {
+               if(chain.write(/*use_padding=*/false, /*preserve_file_stats=*/false))
+                       return die_c_("mismatched write should have failed", chain.status());
+               if(chain.status() != ::FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH)
+                       return die_c_("expected FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH", chain.status());
+               printf("  OK: write() returned false,FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH like it should\n");
+       }
+
+       printf("read chain (callback-based)\n");
+       {
+               FILE *file = fopen(flacfilename(is_ogg), "rb");
+               if(0 == file)
+                       return die_("opening file");
+               if(!chain.read((::FLAC__IOHandle)file, callbacks)) {
+                       fclose(file);
+                       return die_c_("reading chain", chain.status());
+               }
+               fclose(file);
+       }
+
+       printf("testing Chain::check_if_tempfile_needed()... ");
+
+       if(!chain.check_if_tempfile_needed(/*use_padding=*/false))
+               printf("OK: Chain::check_if_tempfile_needed() returned false like it should\n");
+       else
+               return die_("Chain::check_if_tempfile_needed() returned true but shouldn't have");
+
+       printf("write chain with wrong method Chain::write(with callbacks and tempfile)\n");
+       {
+               if(chain.write(/*use_padding=*/false, 0, callbacks, 0, callbacks))
+                       return die_c_("mismatched write should have failed", chain.status());
+               if(chain.status() != ::FLAC__METADATA_CHAIN_STATUS_WRONG_WRITE_CALL)
+                       return die_c_("expected FLAC__METADATA_CHAIN_STATUS_WRONG_WRITE_CALL", chain.status());
+               printf("  OK: Chain::write(with callbacks and tempfile) returned false,FLAC__METADATA_CHAIN_STATUS_WRONG_WRITE_CALL like it should\n");
+       }
+
+       printf("read chain (callback-based)\n");
+       {
+               FILE *file = fopen(flacfilename(is_ogg), "rb");
+               if(0 == file)
+                       return die_("opening file");
+               if(!chain.read((::FLAC__IOHandle)file, callbacks)) {
+                       fclose(file);
+                       return die_c_("reading chain", chain.status());
+               }
+               fclose(file);
+       }
+
+       printf("create iterator\n");
+       {
+       FLAC::Metadata::Iterator iterator;
+       if(!iterator.is_valid())
+               return die_("allocating memory for iterator");
+
+       iterator.init(chain);
+
+       printf("[S]VP\tnext\n");
+       if(!iterator.next())
+               return die_("iterator ended early\n");
+
+       printf("S[V]P\tdelete VORBIS_COMMENT, write\n");
+       if(!iterator.delete_block(/*replace_with_padding=*/false))
+               return die_c_("block delete failed\n", chain.status());
+
+       printf("testing Chain::check_if_tempfile_needed()... ");
+
+       if(chain.check_if_tempfile_needed(/*use_padding=*/false))
+               printf("OK: Chain::check_if_tempfile_needed() returned true like it should\n");
+       else
+               return die_("Chain::check_if_tempfile_needed() returned false but shouldn't have");
+
+       printf("write chain with wrong method Chain::write(with callbacks)\n");
+       {
+               if(chain.write(/*use_padding=*/false, 0, callbacks))
+                       return die_c_("mismatched write should have failed", chain.status());
+               if(chain.status() != ::FLAC__METADATA_CHAIN_STATUS_WRONG_WRITE_CALL)
+                       return die_c_("expected FLAC__METADATA_CHAIN_STATUS_WRONG_WRITE_CALL", chain.status());
+               printf("  OK: Chain::write(with callbacks) returned false,FLAC__METADATA_CHAIN_STATUS_WRONG_WRITE_CALL like it should\n");
+       }
+
+       } // delete iterator
+
+       if(!remove_file_(flacfilename(is_ogg)))
                return false;
 
        return true;
@@ -1591,8 +2188,24 @@ bool test_metadata_file_manipulation()
        if(!test_level_1_())
                return false;
 
-       if(!test_level_2_())
+       if(!test_level_2_(/*filename_based=*/true, /*is_ogg=*/false)) /* filename-based */
                return false;
+       if(!test_level_2_(/*filename_based=*/false, /*is_ogg=*/false)) /* callback-based */
+               return false;
+       if(!test_level_2_misc_(/*is_ogg=*/false))
+               return false;
+
+       if(FLAC_API_SUPPORTS_OGG_FLAC) {
+               if(!test_level_2_(/*filename_based=*/true, /*is_ogg=*/true)) /* filename-based */
+                       return false;
+               if(!test_level_2_(/*filename_based=*/false, /*is_ogg=*/true)) /* callback-based */
+                       return false;
+#if 0
+               /* when ogg flac write is supported, will have to add this: */
+               if(!test_level_2_misc_(/*is_ogg=*/true))
+                       return false;
+#endif
+       }
 
        return true;
 }