src/libFLAC/stream_decoder.c : Fix buffer read overflow.
[platform/upstream/flac.git] / src / metaflac / options.c
index 8e6f59a..a9629b2 100644 (file)
@@ -1,5 +1,6 @@
 /* metaflac - Command-line FLAC metadata editor
- * Copyright (C) 2001,2002,2003,2004  Josh Coalson
+ * Copyright (C) 2001-2009  Josh Coalson
+ * Copyright (C) 2011-2013  Xiph.Org Foundation
  *
  * 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.
  */
 
+#if HAVE_CONFIG_H
+#  include <config.h>
+#endif
+
 #include "options.h"
 #include "usage.h"
 #include "utils.h"
 #include "FLAC/assert.h"
+#include "share/alloc.h"
+#include "share/compat.h"
+#include "share/grabbag/replaygain.h"
 #include <ctype.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -56,26 +64,22 @@ struct share__option long_options_[] = {
        { "set-channels", 1, 0, 0 }, /* undocumented */
        { "set-bps", 1, 0, 0 }, /* undocumented */
        { "set-total-samples", 1, 0, 0 }, /* undocumented */ /* WATCHOUT: used by test/test_flac.sh on windows */
-       { "show-vendor-tag", 0, 0, 0 }, 
-       { "show-tag", 1, 0, 0 }, 
-       { "remove-all-tags", 0, 0, 0 }, 
-       { "remove-tag", 1, 0, 0 }, 
-       { "remove-first-tag", 1, 0, 0 }, 
-       { "set-tag", 1, 0, 0 }, 
-       { "import-tags-from", 1, 0, 0 }, 
-       { "export-tags-to", 1, 0, 0 }, 
-       { "show-vc-vendor", 0, 0, 0 }, /* deprecated */
-       { "show-vc-field", 1, 0, 0 }, /* deprecated */
-       { "remove-vc-all", 0, 0, 0 }, /* deprecated */
-       { "remove-vc-field", 1, 0, 0 }, /* deprecated */
-       { "remove-vc-firstfield", 1, 0, 0 }, /* deprecated */
-       { "set-vc-field", 1, 0, 0 }, /* deprecated */
-       { "import-vc-from", 1, 0, 0 }, /* deprecated */
-       { "export-vc-to", 1, 0, 0 }, /* deprecated */
+       { "show-vendor-tag", 0, 0, 0 },
+       { "show-tag", 1, 0, 0 },
+       { "remove-all-tags", 0, 0, 0 },
+       { "remove-tag", 1, 0, 0 },
+       { "remove-first-tag", 1, 0, 0 },
+       { "set-tag", 1, 0, 0 },
+       { "set-tag-from-file", 1, 0, 0 },
+       { "import-tags-from", 1, 0, 0 },
+       { "export-tags-to", 1, 0, 0 },
        { "import-cuesheet-from", 1, 0, 0 },
        { "export-cuesheet-to", 1, 0, 0 },
+       { "import-picture-from", 1, 0, 0 },
+       { "export-picture-to", 1, 0, 0 },
        { "add-seekpoint", 1, 0, 0 },
        { "add-replay-gain", 0, 0, 0 },
+       { "remove-replay-gain", 0, 0, 0 },
        { "add-padding", 1, 0, 0 },
        /* major operations */
        { "help", 0, 0, 0 },
@@ -101,12 +105,13 @@ static void append_new_operation(CommandLineOptions *options, Operation operatio
 static void append_new_argument(CommandLineOptions *options, Argument argument);
 static Operation *append_major_operation(CommandLineOptions *options, OperationType type);
 static Operation *append_shorthand_operation(CommandLineOptions *options, OperationType type);
+static Argument *find_argument(CommandLineOptions *options, ArgumentType type);
 static Operation *find_shorthand_operation(CommandLineOptions *options, OperationType type);
 static Argument *append_argument(CommandLineOptions *options, ArgumentType type);
 static FLAC__bool parse_md5(const char *src, FLAC__byte dest[16]);
 static FLAC__bool parse_uint32(const char *src, FLAC__uint32 *dest);
 static FLAC__bool parse_uint64(const char *src, FLAC__uint64 *dest);
-static FLAC__bool parse_filename(const char *src, char **dest);
+static FLAC__bool parse_string(const char *src, char **dest);
 static FLAC__bool parse_vorbis_comment_field_name(const char *field_ref, char **name, const char **violation);
 static FLAC__bool parse_add_seekpoint(const char *in, char **out, const char **violation);
 static FLAC__bool parse_add_padding(const char *in, unsigned *out);
@@ -174,8 +179,8 @@ FLAC__bool parse_options(int argc, char *argv[], CommandLineOptions *options)
                options->prefix_with_filename = (argc - share__optind > 1);
 
        if(share__optind >= argc && !options->show_long_help && !options->show_version) {
-               fprintf(stderr,"ERROR: you must specify at least one FLAC file;\n");
-               fprintf(stderr,"       metaflac cannot be used as a pipe\n");
+               flac_fprintf(stderr,"ERROR: you must specify at least one FLAC file;\n");
+               flac_fprintf(stderr,"       metaflac cannot be used as a pipe\n");
                had_error = true;
        }
 
@@ -183,7 +188,7 @@ FLAC__bool parse_options(int argc, char *argv[], CommandLineOptions *options)
 
        if(options->num_files > 0) {
                unsigned i = 0;
-               if(0 == (options->filenames = (char**)malloc(sizeof(char*) * options->num_files)))
+               if(0 == (options->filenames = safe_malloc_mul_2op_(sizeof(char*), /*times*/options->num_files)))
                        die("out of memory allocating space for file names list");
                while(share__optind < argc)
                        options->filenames[i++] = local_strdup(argv[share__optind++]);
@@ -191,23 +196,40 @@ FLAC__bool parse_options(int argc, char *argv[], CommandLineOptions *options)
 
        if(options->args.checks.num_major_ops > 0) {
                if(options->args.checks.num_major_ops > 1) {
-                       fprintf(stderr, "ERROR: you may only specify one major operation at a time\n");
+                       flac_fprintf(stderr, "ERROR: you may only specify one major operation at a time\n");
                        had_error = true;
                }
                else if(options->args.checks.num_shorthand_ops > 0) {
-                       fprintf(stderr, "ERROR: you may not mix shorthand and major operations\n");
+                       flac_fprintf(stderr, "ERROR: you may not mix shorthand and major operations\n");
                        had_error = true;
                }
        }
 
-       /* check for only one FLAC file used with --import-cuesheet-from/--export-cuesheet-to */
-       if((0 != find_shorthand_operation(options, OP__IMPORT_CUESHEET_FROM) || 0 != find_shorthand_operation(options, OP__EXPORT_CUESHEET_TO)) && options->num_files > 1) {
-               fprintf(stderr, "ERROR: you may only specify one FLAC file when using '--import-cuesheet-from' or '--export-cuesheet-to'\n");
-               had_error = true;
+       /* check for only one FLAC file used with certain options */
+       if(options->num_files > 1) {
+               if(0 != find_shorthand_operation(options, OP__IMPORT_CUESHEET_FROM)) {
+                       flac_fprintf(stderr, "ERROR: you may only specify one FLAC file when using '--import-cuesheet-from'\n");
+                       had_error = true;
+               }
+               if(0 != find_shorthand_operation(options, OP__EXPORT_CUESHEET_TO)) {
+                       flac_fprintf(stderr, "ERROR: you may only specify one FLAC file when using '--export-cuesheet-to'\n");
+                       had_error = true;
+               }
+               if(0 != find_shorthand_operation(options, OP__EXPORT_PICTURE_TO)) {
+                       flac_fprintf(stderr, "ERROR: you may only specify one FLAC file when using '--export-picture-to'\n");
+                       had_error = true;
+               }
+               if(
+                       0 != find_shorthand_operation(options, OP__IMPORT_VC_FROM) &&
+                       0 == strcmp(find_shorthand_operation(options, OP__IMPORT_VC_FROM)->argument.filename.value, "-")
+               ) {
+                       flac_fprintf(stderr, "ERROR: you may only specify one FLAC file when using '--import-tags-from=-'\n");
+                       had_error = true;
+               }
        }
 
        if(options->args.checks.has_block_type && options->args.checks.has_except_block_type) {
-               fprintf(stderr, "ERROR: you may not specify both '--block-type' and '--except-block-type'\n");
+               flac_fprintf(stderr, "ERROR: you may not specify both '--block-type' and '--except-block-type'\n");
                had_error = true;
        }
 
@@ -216,7 +238,7 @@ FLAC__bool parse_options(int argc, char *argv[], CommandLineOptions *options)
 
        /*
         * We need to create an OP__ADD_SEEKPOINT operation if there is
-        * not one already,  and --import-cuesheet-from was specified but
+        * not one already, and --import-cuesheet-from was specified but
         * --no-cued-seekpoints was not:
         */
        if(options->cued_seekpoints) {
@@ -229,7 +251,7 @@ FLAC__bool parse_options(int argc, char *argv[], CommandLineOptions *options)
                }
        }
 
-       return !had_error;
+       return had_error;
 }
 
 void free_options(CommandLineOptions *options)
@@ -267,6 +289,14 @@ void free_options(CommandLineOptions *options)
                                if(0 != op->argument.import_cuesheet_from.filename)
                                        free(op->argument.import_cuesheet_from.filename);
                                break;
+                       case OP__IMPORT_PICTURE_FROM:
+                               if(0 != op->argument.specification.value)
+                                       free(op->argument.specification.value);
+                               break;
+                       case OP__EXPORT_PICTURE_TO:
+                               if(0 != op->argument.export_picture_to.filename)
+                                       free(op->argument.export_picture_to.filename);
+                               break;
                        case OP__ADD_SEEKPOINT:
                                if(0 != op->argument.add_seekpoint.specification)
                                        free(op->argument.add_seekpoint.specification);
@@ -371,7 +401,7 @@ FLAC__bool parse_option(int option_index, const char *option_argument, CommandLi
                op = append_shorthand_operation(options, OP__SET_MD5SUM);
                FLAC__ASSERT(0 != option_argument);
                if(!parse_md5(option_argument, op->argument.streaminfo_md5.value)) {
-                       fprintf(stderr, "ERROR (--%s): bad MD5 sum\n", opt);
+                       flac_fprintf(stderr, "ERROR (--%s): bad MD5 sum\n", opt);
                        ok = false;
                }
                else
@@ -380,7 +410,7 @@ FLAC__bool parse_option(int option_index, const char *option_argument, CommandLi
        else if(0 == strcmp(opt, "set-min-blocksize")) {
                op = append_shorthand_operation(options, OP__SET_MIN_BLOCKSIZE);
                if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || op->argument.streaminfo_uint32.value < FLAC__MIN_BLOCK_SIZE || op->argument.streaminfo_uint32.value > FLAC__MAX_BLOCK_SIZE) {
-                       fprintf(stderr, "ERROR (--%s): value must be >= %u and <= %u\n", opt, FLAC__MIN_BLOCK_SIZE, FLAC__MAX_BLOCK_SIZE);
+                       flac_fprintf(stderr, "ERROR (--%s): value must be >= %u and <= %u\n", opt, FLAC__MIN_BLOCK_SIZE, FLAC__MAX_BLOCK_SIZE);
                        ok = false;
                }
                else
@@ -389,7 +419,7 @@ FLAC__bool parse_option(int option_index, const char *option_argument, CommandLi
        else if(0 == strcmp(opt, "set-max-blocksize")) {
                op = append_shorthand_operation(options, OP__SET_MAX_BLOCKSIZE);
                if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || op->argument.streaminfo_uint32.value < FLAC__MIN_BLOCK_SIZE || op->argument.streaminfo_uint32.value > FLAC__MAX_BLOCK_SIZE) {
-                       fprintf(stderr, "ERROR (--%s): value must be >= %u and <= %u\n", opt, FLAC__MIN_BLOCK_SIZE, FLAC__MAX_BLOCK_SIZE);
+                       flac_fprintf(stderr, "ERROR (--%s): value must be >= %u and <= %u\n", opt, FLAC__MIN_BLOCK_SIZE, FLAC__MAX_BLOCK_SIZE);
                        ok = false;
                }
                else
@@ -398,7 +428,7 @@ FLAC__bool parse_option(int option_index, const char *option_argument, CommandLi
        else if(0 == strcmp(opt, "set-min-framesize")) {
                op = append_shorthand_operation(options, OP__SET_MIN_FRAMESIZE);
                if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || op->argument.streaminfo_uint32.value >= (1u<<FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN)) {
-                       fprintf(stderr, "ERROR (--%s): value must be a %u-bit unsigned integer\n", opt, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN);
+                       flac_fprintf(stderr, "ERROR (--%s): value must be a %u-bit unsigned integer\n", opt, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN);
                        ok = false;
                }
                else
@@ -407,7 +437,7 @@ FLAC__bool parse_option(int option_index, const char *option_argument, CommandLi
        else if(0 == strcmp(opt, "set-max-framesize")) {
                op = append_shorthand_operation(options, OP__SET_MAX_FRAMESIZE);
                if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || op->argument.streaminfo_uint32.value >= (1u<<FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN)) {
-                       fprintf(stderr, "ERROR (--%s): value must be a %u-bit unsigned integer\n", opt, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN);
+                       flac_fprintf(stderr, "ERROR (--%s): value must be a %u-bit unsigned integer\n", opt, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN);
                        ok = false;
                }
                else
@@ -416,7 +446,7 @@ FLAC__bool parse_option(int option_index, const char *option_argument, CommandLi
        else if(0 == strcmp(opt, "set-sample-rate")) {
                op = append_shorthand_operation(options, OP__SET_SAMPLE_RATE);
                if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || !FLAC__format_sample_rate_is_valid(op->argument.streaminfo_uint32.value)) {
-                       fprintf(stderr, "ERROR (--%s): invalid sample rate\n", opt);
+                       flac_fprintf(stderr, "ERROR (--%s): invalid sample rate\n", opt);
                        ok = false;
                }
                else
@@ -425,7 +455,7 @@ FLAC__bool parse_option(int option_index, const char *option_argument, CommandLi
        else if(0 == strcmp(opt, "set-channels")) {
                op = append_shorthand_operation(options, OP__SET_CHANNELS);
                if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || op->argument.streaminfo_uint32.value > FLAC__MAX_CHANNELS) {
-                       fprintf(stderr, "ERROR (--%s): value must be > 0 and <= %u\n", opt, FLAC__MAX_CHANNELS);
+                       flac_fprintf(stderr, "ERROR (--%s): value must be > 0 and <= %u\n", opt, FLAC__MAX_CHANNELS);
                        ok = false;
                }
                else
@@ -434,7 +464,7 @@ FLAC__bool parse_option(int option_index, const char *option_argument, CommandLi
        else if(0 == strcmp(opt, "set-bps")) {
                op = append_shorthand_operation(options, OP__SET_BPS);
                if(!parse_uint32(option_argument, &(op->argument.streaminfo_uint32.value)) || op->argument.streaminfo_uint32.value < FLAC__MIN_BITS_PER_SAMPLE || op->argument.streaminfo_uint32.value > FLAC__MAX_BITS_PER_SAMPLE) {
-                       fprintf(stderr, "ERROR (--%s): value must be >= %u and <= %u\n", opt, FLAC__MIN_BITS_PER_SAMPLE, FLAC__MAX_BITS_PER_SAMPLE);
+                       flac_fprintf(stderr, "ERROR (--%s): value must be >= %u and <= %u\n", opt, FLAC__MIN_BITS_PER_SAMPLE, FLAC__MAX_BITS_PER_SAMPLE);
                        ok = false;
                }
                else
@@ -442,110 +472,124 @@ FLAC__bool parse_option(int option_index, const char *option_argument, CommandLi
        }
        else if(0 == strcmp(opt, "set-total-samples")) {
                op = append_shorthand_operation(options, OP__SET_TOTAL_SAMPLES);
-               if(!parse_uint64(option_argument, &(op->argument.streaminfo_uint64.value)) || op->argument.streaminfo_uint64.value >= (1u<<FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN)) {
-                       fprintf(stderr, "ERROR (--%s): value must be a %u-bit unsigned integer\n", opt, FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN);
+               if(!parse_uint64(option_argument, &(op->argument.streaminfo_uint64.value)) || op->argument.streaminfo_uint64.value >= (((FLAC__uint64)1)<<FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN)) {
+                       flac_fprintf(stderr, "ERROR (--%s): value must be a %u-bit unsigned integer\n", opt, FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN);
                        ok = false;
                }
                else
                        undocumented_warning(opt);
        }
-       else if(0 == strcmp(opt, "show-vendor-tag") || 0 == strcmp(opt, "show-vc-vendor")) {
-               if(0 == strcmp(opt, "show-vc-vendor"))
-                       fprintf(stderr, "WARNING: --%s is deprecated, the new name is --show-vendor-tag\n", opt);
+       else if(0 == strcmp(opt, "show-vendor-tag")) {
                (void) append_shorthand_operation(options, OP__SHOW_VC_VENDOR);
        }
-       else if(0 == strcmp(opt, "show-tag") || 0 == strcmp(opt, "show-vc-field")) {
+       else if(0 == strcmp(opt, "show-tag")) {
                const char *violation;
-               if(0 == strcmp(opt, "show-vc-field"))
-                       fprintf(stderr, "WARNING: --%s is deprecated, the new name is --show-tag\n", opt);
                op = append_shorthand_operation(options, OP__SHOW_VC_FIELD);
                FLAC__ASSERT(0 != option_argument);
                if(!parse_vorbis_comment_field_name(option_argument, &(op->argument.vc_field_name.value), &violation)) {
                        FLAC__ASSERT(0 != violation);
-                       fprintf(stderr, "ERROR (--%s): malformed vorbis comment field name \"%s\",\n       %s\n", opt, option_argument, violation);
+                       flac_fprintf(stderr, "ERROR (--%s): malformed vorbis comment field name \"%s\",\n       %s\n", opt, option_argument, violation);
                        ok = false;
                }
        }
-       else if(0 == strcmp(opt, "remove-all-tags") || 0 == strcmp(opt, "remove-vc-all")) {
-               if(0 == strcmp(opt, "remove-vc-all"))
-                       fprintf(stderr, "WARNING: --%s is deprecated, the new name is --remove-all-tags\n", opt);
+       else if(0 == strcmp(opt, "remove-all-tags")) {
                (void) append_shorthand_operation(options, OP__REMOVE_VC_ALL);
        }
-       else if(0 == strcmp(opt, "remove-tag") || 0 == strcmp(opt, "remove-vc-field")) {
+       else if(0 == strcmp(opt, "remove-tag")) {
                const char *violation;
-               if(0 == strcmp(opt, "remove-vc-field"))
-                       fprintf(stderr, "WARNING: --%s is deprecated, the new name is --remove-tag\n", opt);
                op = append_shorthand_operation(options, OP__REMOVE_VC_FIELD);
                FLAC__ASSERT(0 != option_argument);
                if(!parse_vorbis_comment_field_name(option_argument, &(op->argument.vc_field_name.value), &violation)) {
                        FLAC__ASSERT(0 != violation);
-                       fprintf(stderr, "ERROR (--%s): malformed vorbis comment field name \"%s\",\n       %s\n", opt, option_argument, violation);
+                       flac_fprintf(stderr, "ERROR (--%s): malformed vorbis comment field name \"%s\",\n       %s\n", opt, option_argument, violation);
                        ok = false;
                }
        }
-       else if(0 == strcmp(opt, "remove-first-tag") || 0 == strcmp(opt, "remove-vc-firstfield")) {
+       else if(0 == strcmp(opt, "remove-first-tag")) {
                const char *violation;
-               if(0 == strcmp(opt, "remove-vc-firstfield"))
-                       fprintf(stderr, "WARNING: --%s is deprecated, the new name is --remove-first-tag\n", opt);
                op = append_shorthand_operation(options, OP__REMOVE_VC_FIRSTFIELD);
                FLAC__ASSERT(0 != option_argument);
                if(!parse_vorbis_comment_field_name(option_argument, &(op->argument.vc_field_name.value), &violation)) {
                        FLAC__ASSERT(0 != violation);
-                       fprintf(stderr, "ERROR (--%s): malformed vorbis comment field name \"%s\",\n       %s\n", opt, option_argument, violation);
+                       flac_fprintf(stderr, "ERROR (--%s): malformed vorbis comment field name \"%s\",\n       %s\n", opt, option_argument, violation);
+                       ok = false;
+               }
+       }
+       else if(0 == strcmp(opt, "set-tag")) {
+               const char *violation;
+               op = append_shorthand_operation(options, OP__SET_VC_FIELD);
+               FLAC__ASSERT(0 != option_argument);
+               op->argument.vc_field.field_value_from_file = false;
+               if(!parse_vorbis_comment_field(option_argument, &(op->argument.vc_field.field), &(op->argument.vc_field.field_name), &(op->argument.vc_field.field_value), &(op->argument.vc_field.field_value_length), &violation)) {
+                       FLAC__ASSERT(0 != violation);
+                       flac_fprintf(stderr, "ERROR (--%s): malformed vorbis comment field \"%s\",\n       %s\n", opt, option_argument, violation);
                        ok = false;
                }
        }
-       else if(0 == strcmp(opt, "set-tag") || 0 == strcmp(opt, "set-vc-field")) {
+       else if(0 == strcmp(opt, "set-tag-from-file")) {
                const char *violation;
-               if(0 == strcmp(opt, "set-vc-field"))
-                       fprintf(stderr, "WARNING: --%s is deprecated, the new name is --set-tag\n", opt);
                op = append_shorthand_operation(options, OP__SET_VC_FIELD);
                FLAC__ASSERT(0 != option_argument);
+               op->argument.vc_field.field_value_from_file = true;
                if(!parse_vorbis_comment_field(option_argument, &(op->argument.vc_field.field), &(op->argument.vc_field.field_name), &(op->argument.vc_field.field_value), &(op->argument.vc_field.field_value_length), &violation)) {
                        FLAC__ASSERT(0 != violation);
-                       fprintf(stderr, "ERROR (--%s): malformed vorbis comment field \"%s\",\n       %s\n", opt, option_argument, violation);
+                       flac_fprintf(stderr, "ERROR (--%s): malformed vorbis comment field \"%s\",\n       %s\n", opt, option_argument, violation);
                        ok = false;
                }
        }
-       else if(0 == strcmp(opt, "import-tags-from") || 0 == strcmp(opt, "import-vc-from")) {
-               if(0 == strcmp(opt, "import-vc-from"))
-                       fprintf(stderr, "WARNING: --%s is deprecated, the new name is --import-tags-from\n", opt);
+       else if(0 == strcmp(opt, "import-tags-from")) {
                op = append_shorthand_operation(options, OP__IMPORT_VC_FROM);
                FLAC__ASSERT(0 != option_argument);
-               if(!parse_filename(option_argument, &(op->argument.filename.value))) {
-                       fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
+               if(!parse_string(option_argument, &(op->argument.filename.value))) {
+                       flac_fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
                        ok = false;
                }
        }
-       else if(0 == strcmp(opt, "export-tags-to") || 0 == strcmp(opt, "export-vc-to")) {
-               if(0 == strcmp(opt, "export-vc-to"))
-                       fprintf(stderr, "WARNING: --%s is deprecated, the new name is --export-tags-to\n", opt);
+       else if(0 == strcmp(opt, "export-tags-to")) {
                op = append_shorthand_operation(options, OP__EXPORT_VC_TO);
                FLAC__ASSERT(0 != option_argument);
-               if(!parse_filename(option_argument, &(op->argument.filename.value))) {
-                       fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
+               if(!parse_string(option_argument, &(op->argument.filename.value))) {
+                       flac_fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
                        ok = false;
                }
        }
        else if(0 == strcmp(opt, "import-cuesheet-from")) {
                if(0 != find_shorthand_operation(options, OP__IMPORT_CUESHEET_FROM)) {
-                       fprintf(stderr, "ERROR (--%s): may be specified only once\n", opt);
+                       flac_fprintf(stderr, "ERROR (--%s): may be specified only once\n", opt);
                        ok = false;
                }
                op = append_shorthand_operation(options, OP__IMPORT_CUESHEET_FROM);
                FLAC__ASSERT(0 != option_argument);
-               if(!parse_filename(option_argument, &(op->argument.import_cuesheet_from.filename))) {
-                       fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
+               if(!parse_string(option_argument, &(op->argument.import_cuesheet_from.filename))) {
+                       flac_fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
                        ok = false;
                }
        }
        else if(0 == strcmp(opt, "export-cuesheet-to")) {
                op = append_shorthand_operation(options, OP__EXPORT_CUESHEET_TO);
                FLAC__ASSERT(0 != option_argument);
-               if(!parse_filename(option_argument, &(op->argument.filename.value))) {
-                       fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
+               if(!parse_string(option_argument, &(op->argument.filename.value))) {
+                       flac_fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
+                       ok = false;
+               }
+       }
+       else if(0 == strcmp(opt, "import-picture-from")) {
+               op = append_shorthand_operation(options, OP__IMPORT_PICTURE_FROM);
+               FLAC__ASSERT(0 != option_argument);
+               if(!parse_string(option_argument, &(op->argument.specification.value))) {
+                       flac_fprintf(stderr, "ERROR (--%s): missing specification\n", opt);
+                       ok = false;
+               }
+       }
+       else if(0 == strcmp(opt, "export-picture-to")) {
+               arg = find_argument(options, ARG__BLOCK_NUMBER);
+               op = append_shorthand_operation(options, OP__EXPORT_PICTURE_TO);
+               FLAC__ASSERT(0 != option_argument);
+               if(!parse_string(option_argument, &(op->argument.export_picture_to.filename))) {
+                       flac_fprintf(stderr, "ERROR (--%s): missing filename\n", opt);
                        ok = false;
                }
+               op->argument.export_picture_to.block_number_link = arg? &(arg->value.block_number) : 0;
        }
        else if(0 == strcmp(opt, "add-seekpoint")) {
                const char *violation;
@@ -553,24 +597,40 @@ FLAC__bool parse_option(int option_index, const char *option_argument, CommandLi
                FLAC__ASSERT(0 != option_argument);
                if(!parse_add_seekpoint(option_argument, &spec, &violation)) {
                        FLAC__ASSERT(0 != violation);
-                       fprintf(stderr, "ERROR (--%s): malformed seekpoint specification \"%s\",\n       %s\n", opt, option_argument, violation);
+                       flac_fprintf(stderr, "ERROR (--%s): malformed seekpoint specification \"%s\",\n       %s\n", opt, option_argument, violation);
                        ok = false;
                }
-               op = find_shorthand_operation(options, OP__ADD_SEEKPOINT);
-               if(0 == op)
-                       op = append_shorthand_operation(options, OP__ADD_SEEKPOINT);
-               local_strcat(&(op->argument.add_seekpoint.specification), spec);
-               local_strcat(&(op->argument.add_seekpoint.specification), ";");
-               free(spec);
+               else {
+                       op = find_shorthand_operation(options, OP__ADD_SEEKPOINT);
+                       if(0 == op)
+                               op = append_shorthand_operation(options, OP__ADD_SEEKPOINT);
+                       local_strcat(&(op->argument.add_seekpoint.specification), spec);
+                       local_strcat(&(op->argument.add_seekpoint.specification), ";");
+                       free(spec);
+               }
        }
        else if(0 == strcmp(opt, "add-replay-gain")) {
                (void) append_shorthand_operation(options, OP__ADD_REPLAY_GAIN);
        }
+       else if(0 == strcmp(opt, "remove-replay-gain")) {
+               const FLAC__byte * const tags[5] = {
+                       GRABBAG__REPLAYGAIN_TAG_REFERENCE_LOUDNESS,
+                       GRABBAG__REPLAYGAIN_TAG_TITLE_GAIN,
+                       GRABBAG__REPLAYGAIN_TAG_TITLE_PEAK,
+                       GRABBAG__REPLAYGAIN_TAG_ALBUM_GAIN,
+                       GRABBAG__REPLAYGAIN_TAG_ALBUM_PEAK
+               };
+               size_t i;
+               for(i = 0; i < sizeof(tags)/sizeof(tags[0]); i++) {
+                       op = append_shorthand_operation(options, OP__REMOVE_VC_FIELD);
+                       op->argument.vc_field_name.value = local_strdup((const char *)tags[i]);
+               }
+       }
        else if(0 == strcmp(opt, "add-padding")) {
                op = append_shorthand_operation(options, OP__ADD_PADDING);
                FLAC__ASSERT(0 != option_argument);
                if(!parse_add_padding(option_argument, &(op->argument.add_padding.length))) {
-                       fprintf(stderr, "ERROR (--%s): illegal length \"%s\", length must be >= 0 and < 2^%u\n", opt, option_argument, FLAC__STREAM_METADATA_LENGTH_LEN);
+                       flac_fprintf(stderr, "ERROR (--%s): illegal length \"%s\", length must be >= 0 and < 2^%u\n", opt, option_argument, FLAC__STREAM_METADATA_LENGTH_LEN);
                        ok = false;
                }
        }
@@ -602,7 +662,7 @@ FLAC__bool parse_option(int option_index, const char *option_argument, CommandLi
                arg = append_argument(options, ARG__BLOCK_NUMBER);
                FLAC__ASSERT(0 != option_argument);
                if(!parse_block_number(option_argument, &(arg->value.block_number))) {
-                       fprintf(stderr, "ERROR: malformed block number specification \"%s\"\n", option_argument);
+                       flac_fprintf(stderr, "ERROR: malformed block number specification \"%s\"\n", option_argument);
                        ok = false;
                }
        }
@@ -610,7 +670,7 @@ FLAC__bool parse_option(int option_index, const char *option_argument, CommandLi
                arg = append_argument(options, ARG__BLOCK_TYPE);
                FLAC__ASSERT(0 != option_argument);
                if(!parse_block_type(option_argument, &(arg->value.block_type))) {
-                       fprintf(stderr, "ERROR (--%s): malformed block type specification \"%s\"\n", opt, option_argument);
+                       flac_fprintf(stderr, "ERROR (--%s): malformed block type specification \"%s\"\n", opt, option_argument);
                        ok = false;
                }
                options->args.checks.has_block_type = true;
@@ -619,7 +679,7 @@ FLAC__bool parse_option(int option_index, const char *option_argument, CommandLi
                arg = append_argument(options, ARG__EXCEPT_BLOCK_TYPE);
                FLAC__ASSERT(0 != option_argument);
                if(!parse_block_type(option_argument, &(arg->value.block_type))) {
-                       fprintf(stderr, "ERROR (--%s): malformed block type specification \"%s\"\n", opt, option_argument);
+                       flac_fprintf(stderr, "ERROR (--%s): malformed block type specification \"%s\"\n", opt, option_argument);
                        ok = false;
                }
                options->args.checks.has_except_block_type = true;
@@ -628,14 +688,14 @@ FLAC__bool parse_option(int option_index, const char *option_argument, CommandLi
                arg = append_argument(options, ARG__DATA_FORMAT);
                FLAC__ASSERT(0 != option_argument);
                if(!parse_data_format(option_argument, &(arg->value.data_format))) {
-                       fprintf(stderr, "ERROR (--%s): illegal data format \"%s\"\n", opt, option_argument);
+                       flac_fprintf(stderr, "ERROR (--%s): illegal data format \"%s\"\n", opt, option_argument);
                        ok = false;
                }
        }
        else if(0 == strcmp(opt, "application-data-format")) {
                FLAC__ASSERT(0 != option_argument);
                if(!parse_application_data_format(option_argument, &(options->application_data_format_is_hexdump))) {
-                       fprintf(stderr, "ERROR (--%s): illegal application data format \"%s\"\n", opt, option_argument);
+                       flac_fprintf(stderr, "ERROR (--%s): illegal application data format \"%s\"\n", opt, option_argument);
                        ok = false;
                }
        }
@@ -655,14 +715,16 @@ void append_new_operation(CommandLineOptions *options, Operation operation)
 {
        if(options->ops.capacity == 0) {
                options->ops.capacity = 50;
-               if(0 == (options->ops.operations = (Operation*)malloc(sizeof(Operation) * options->ops.capacity)))
+               if(0 == (options->ops.operations = malloc(sizeof(Operation) * options->ops.capacity)))
                        die("out of memory allocating space for option list");
                memset(options->ops.operations, 0, sizeof(Operation) * options->ops.capacity);
        }
        if(options->ops.capacity <= options->ops.num_operations) {
                unsigned original_capacity = options->ops.capacity;
-               options->ops.capacity *= 4;
-               if(0 == (options->ops.operations = (Operation*)realloc(options->ops.operations, sizeof(Operation) * options->ops.capacity)))
+               if(options->ops.capacity > UINT32_MAX / 2) /* overflow check */
+                       die("out of memory allocating space for option list");
+               options->ops.capacity *= 2;
+               if(0 == (options->ops.operations = safe_realloc_mul_2op_(options->ops.operations, sizeof(Operation), /*times*/options->ops.capacity)))
                        die("out of memory allocating space for option list");
                memset(options->ops.operations + original_capacity, 0, sizeof(Operation) * (options->ops.capacity - original_capacity));
        }
@@ -674,14 +736,16 @@ void append_new_argument(CommandLineOptions *options, Argument argument)
 {
        if(options->args.capacity == 0) {
                options->args.capacity = 50;
-               if(0 == (options->args.arguments = (Argument*)malloc(sizeof(Argument) * options->args.capacity)))
+               if(0 == (options->args.arguments = malloc(sizeof(Argument) * options->args.capacity)))
                        die("out of memory allocating space for option list");
                memset(options->args.arguments, 0, sizeof(Argument) * options->args.capacity);
        }
        if(options->args.capacity <= options->args.num_arguments) {
                unsigned original_capacity = options->args.capacity;
-               options->args.capacity *= 4;
-               if(0 == (options->args.arguments = (Argument*)realloc(options->args.arguments, sizeof(Argument) * options->args.capacity)))
+               if(options->args.capacity > UINT32_MAX / 2) /* overflow check */
+                       die("out of memory allocating space for option list");
+               options->args.capacity *= 2;
+               if(0 == (options->args.arguments = safe_realloc_mul_2op_(options->args.arguments, sizeof(Argument), /*times*/options->args.capacity)))
                        die("out of memory allocating space for option list");
                memset(options->args.arguments + original_capacity, 0, sizeof(Argument) * (options->args.capacity - original_capacity));
        }
@@ -709,6 +773,15 @@ Operation *append_shorthand_operation(CommandLineOptions *options, OperationType
        return options->ops.operations + (options->ops.num_operations - 1);
 }
 
+Argument *find_argument(CommandLineOptions *options, ArgumentType type)
+{
+       unsigned i;
+       for(i = 0; i < options->args.num_arguments; i++)
+               if(options->args.arguments[i].type == type)
+                       return &options->args.arguments[i];
+       return 0;
+}
+
 Operation *find_shorthand_operation(CommandLineOptions *options, OperationType type)
 {
        unsigned i;
@@ -769,32 +842,16 @@ FLAC__bool parse_uint32(const char *src, FLAC__uint32 *dest)
        return true;
 }
 
-/* There's no stroull() in MSVC6 so we just write a specialized one */
-static FLAC__uint64 local__strtoull(const char *src)
-{
-       FLAC__uint64 ret = 0;
-       int c;
-       FLAC__ASSERT(0 != src);
-       while(0 != (c = *src++)) {
-               c -= '0';
-               if(c >= 0 && c <= 9)
-                       ret = (ret * 10) + c;
-               else
-                       break;
-       }
-       return ret;
-}
-
 FLAC__bool parse_uint64(const char *src, FLAC__uint64 *dest)
 {
        FLAC__ASSERT(0 != src);
        if(strlen(src) == 0 || strspn(src, "0123456789") != strlen(src))
                return false;
-       *dest = local__strtoull(src);
+       *dest = strtoull(src, 0, 10);
        return true;
 }
 
-FLAC__bool parse_filename(const char *src, char **dest)
+FLAC__bool parse_string(const char *src, char **dest)
 {
        if(0 == src || strlen(src) == 0)
                return false;
@@ -897,7 +954,7 @@ FLAC__bool parse_block_number(const char *in, Argument_BlockNumber *out)
 
        /* make space */
        FLAC__ASSERT(out->num_entries > 0);
-       if(0 == (out->entries = (unsigned*)malloc(sizeof(unsigned) * out->num_entries)))
+       if(0 == (out->entries = safe_malloc_mul_2op_(sizeof(unsigned), /*times*/out->num_entries)))
                die("out of memory allocating space for option list");
 
        /* load 'em up */
@@ -936,7 +993,7 @@ FLAC__bool parse_block_type(const char *in, Argument_BlockType *out)
 
        /* make space */
        FLAC__ASSERT(out->num_entries > 0);
-       if(0 == (out->entries = (Argument_BlockTypeEntry*)malloc(sizeof(Argument_BlockTypeEntry) * out->num_entries)))
+       if(0 == (out->entries = safe_malloc_mul_2op_(sizeof(Argument_BlockTypeEntry), /*times*/out->num_entries)))
                die("out of memory allocating space for option list");
 
        /* load 'em up */
@@ -963,8 +1020,8 @@ FLAC__bool parse_block_type(const char *in, Argument_BlockType *out)
                        out->entries[entry].type = FLAC__METADATA_TYPE_APPLICATION;
                        out->entries[entry].filter_application_by_id = (0 != r);
                        if(0 != r) {
-                               if(strlen(r) == 4) {
-                                       strcpy(out->entries[entry].application_id, r);
+                               if(strlen(r) == sizeof (out->entries[entry].application_id)) {
+                                       memcpy(out->entries[entry].application_id, r, sizeof (out->entries[entry].application_id));
                                }
                                else if(strlen(r) == 10 && strncmp(r, "0x", 2) == 0 && strspn(r+2, "0123456789ABCDEFabcdef") == 8) {
                                        FLAC__uint32 x = strtoul(r+2, 0, 16);
@@ -989,6 +1046,9 @@ FLAC__bool parse_block_type(const char *in, Argument_BlockType *out)
                else if(0 == strcmp(q, "CUESHEET")) {
                        out->entries[entry++].type = FLAC__METADATA_TYPE_CUESHEET;
                }
+               else if(0 == strcmp(q, "PICTURE")) {
+                       out->entries[entry++].type = FLAC__METADATA_TYPE_PICTURE;
+               }
                else {
                        free(s);
                        return false;
@@ -1025,5 +1085,5 @@ FLAC__bool parse_application_data_format(const char *in, FLAC__bool *out)
 
 void undocumented_warning(const char *opt)
 {
-       fprintf(stderr, "WARNING: undocmented option --%s should be used with caution,\n         only for repairing a damaged STREAMINFO block\n", opt);
+       flac_fprintf(stderr, "WARNING: undocumented option --%s should be used with caution,\n         only for repairing a damaged STREAMINFO block\n", opt);
 }