diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-09-05 13:14:37 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-09-05 13:14:37 +0000 |
commit | fd3b2704efc2b206784615c1a23eb25501842259 (patch) | |
tree | 61ba3a8af2a0ae2ac9ec362bbf18b038f5dc0448 /src/metaflac | |
parent | Initial commit. (diff) | |
download | flac-fd3b2704efc2b206784615c1a23eb25501842259.tar.xz flac-fd3b2704efc2b206784615c1a23eb25501842259.zip |
Adding upstream version 1.4.3+ds.upstream/1.4.3+dsupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/metaflac')
-rw-r--r-- | src/metaflac/CMakeLists.txt | 18 | ||||
-rw-r--r-- | src/metaflac/Makefile.am | 65 | ||||
-rw-r--r-- | src/metaflac/Makefile.in | 789 | ||||
-rw-r--r-- | src/metaflac/main.c | 75 | ||||
-rw-r--r-- | src/metaflac/operations.c | 823 | ||||
-rw-r--r-- | src/metaflac/operations.h | 27 | ||||
-rw-r--r-- | src/metaflac/operations_shorthand.h | 26 | ||||
-rw-r--r-- | src/metaflac/operations_shorthand_cuesheet.c | 226 | ||||
-rw-r--r-- | src/metaflac/operations_shorthand_picture.c | 184 | ||||
-rw-r--r-- | src/metaflac/operations_shorthand_seektable.c | 220 | ||||
-rw-r--r-- | src/metaflac/operations_shorthand_streaminfo.c | 127 | ||||
-rw-r--r-- | src/metaflac/operations_shorthand_vorbiscomment.c | 430 | ||||
-rw-r--r-- | src/metaflac/options.c | 1146 | ||||
-rw-r--r-- | src/metaflac/options.h | 221 | ||||
-rw-r--r-- | src/metaflac/usage.c | 349 | ||||
-rw-r--r-- | src/metaflac/usage.h | 26 | ||||
-rw-r--r-- | src/metaflac/utils.c | 282 | ||||
-rw-r--r-- | src/metaflac/utils.h | 47 | ||||
-rw-r--r-- | src/metaflac/version.rc | 38 |
19 files changed, 5119 insertions, 0 deletions
diff --git a/src/metaflac/CMakeLists.txt b/src/metaflac/CMakeLists.txt new file mode 100644 index 0000000..b8af705 --- /dev/null +++ b/src/metaflac/CMakeLists.txt @@ -0,0 +1,18 @@ +add_executable(metaflac + main.c + operations.c + operations_shorthand_cuesheet.c + operations_shorthand_picture.c + operations_shorthand_seektable.c + operations_shorthand_streaminfo.c + operations_shorthand_vorbiscomment.c + options.c + usage.c + utils.c + version.rc + $<$<BOOL:${WIN32}>:../../include/share/win_utf8_io.h> + $<$<BOOL:${WIN32}>:../share/win_utf8_io/win_utf8_io.c>) +target_link_libraries(metaflac FLAC getopt utf8) + +install(TARGETS metaflac EXPORT targets + RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}") diff --git a/src/metaflac/Makefile.am b/src/metaflac/Makefile.am new file mode 100644 index 0000000..8c212ff --- /dev/null +++ b/src/metaflac/Makefile.am @@ -0,0 +1,65 @@ +# metaflac - Command-line FLAC metadata editor +# Copyright (C) 2000-2009 Josh Coalson +# Copyright (C) 2011-2023 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 +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU 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., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +if OS_IS_WINDOWS +win_utf8_lib = $(top_builddir)/src/share/win_utf8_io/libwin_utf8_io.la +if HAVE_WINDRES +metaflac_DEPENDENCIES = version.o +windows_resource_link = -Wl,version.o +endif +endif + +bin_PROGRAMS = metaflac + +AM_CFLAGS = @OGG_CFLAGS@ +AM_CPPFLAGS = -I$(top_builddir) -I$(srcdir)/include -I$(top_srcdir)/include +EXTRA_DIST = \ + CMakeLists.txt \ + version.rc + +metaflac_SOURCES = \ + main.c \ + operations.c \ + operations_shorthand_cuesheet.c \ + operations_shorthand_picture.c \ + operations_shorthand_seektable.c \ + operations_shorthand_streaminfo.c \ + operations_shorthand_vorbiscomment.c \ + options.c \ + usage.c \ + utils.c \ + operations.h \ + operations_shorthand.h \ + options.h \ + usage.h \ + utils.h +metaflac_LDFLAGS = $(AM_LDFLAGS) $(windows_resource_link) + +metaflac_LDADD = \ + $(top_builddir)/src/share/grabbag/libgrabbag.la \ + $(top_builddir)/src/share/replaygain_analysis/libreplaygain_analysis.la \ + $(top_builddir)/src/share/getopt/libgetopt.la \ + $(top_builddir)/src/share/utf8/libutf8.la \ + $(top_builddir)/src/libFLAC/libFLAC.la \ + $(win_utf8_lib) \ + @LTLIBICONV@ + +CLEANFILES = metaflac.exe + +.rc.o: + $(RC) $(AM_CPPFLAGS) $< $@ diff --git a/src/metaflac/Makefile.in b/src/metaflac/Makefile.in new file mode 100644 index 0000000..dae2fae --- /dev/null +++ b/src/metaflac/Makefile.in @@ -0,0 +1,789 @@ +# Makefile.in generated by automake 1.16.5 from Makefile.am. +# @configure_input@ + +# Copyright (C) 1994-2021 Free Software Foundation, Inc. + +# This Makefile.in is free software; the Free Software Foundation +# gives unlimited permission to copy and/or distribute it, +# with or without modifications, as long as this notice is preserved. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY, to the extent permitted by law; without +# even the implied warranty of MERCHANTABILITY or FITNESS FOR A +# PARTICULAR PURPOSE. + +@SET_MAKE@ + +# metaflac - Command-line FLAC metadata editor +# Copyright (C) 2000-2009 Josh Coalson +# Copyright (C) 2011-2023 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 +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU 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., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +VPATH = @srcdir@ +am__is_gnu_make = { \ + if test -z '$(MAKELEVEL)'; then \ + false; \ + elif test -n '$(MAKE_HOST)'; then \ + true; \ + elif test -n '$(MAKE_VERSION)' && test -n '$(CURDIR)'; then \ + true; \ + else \ + false; \ + fi; \ +} +am__make_running_with_option = \ + case $${target_option-} in \ + ?) ;; \ + *) echo "am__make_running_with_option: internal error: invalid" \ + "target option '$${target_option-}' specified" >&2; \ + exit 1;; \ + esac; \ + has_opt=no; \ + sane_makeflags=$$MAKEFLAGS; \ + if $(am__is_gnu_make); then \ + sane_makeflags=$$MFLAGS; \ + else \ + case $$MAKEFLAGS in \ + *\\[\ \ ]*) \ + bs=\\; \ + sane_makeflags=`printf '%s\n' "$$MAKEFLAGS" \ + | sed "s/$$bs$$bs[$$bs $$bs ]*//g"`;; \ + esac; \ + fi; \ + skip_next=no; \ + strip_trailopt () \ + { \ + flg=`printf '%s\n' "$$flg" | sed "s/$$1.*$$//"`; \ + }; \ + for flg in $$sane_makeflags; do \ + test $$skip_next = yes && { skip_next=no; continue; }; \ + case $$flg in \ + *=*|--*) continue;; \ + -*I) strip_trailopt 'I'; skip_next=yes;; \ + -*I?*) strip_trailopt 'I';; \ + -*O) strip_trailopt 'O'; skip_next=yes;; \ + -*O?*) strip_trailopt 'O';; \ + -*l) strip_trailopt 'l'; skip_next=yes;; \ + -*l?*) strip_trailopt 'l';; \ + -[dEDm]) skip_next=yes;; \ + -[JT]) skip_next=yes;; \ + esac; \ + case $$flg in \ + *$$target_option*) has_opt=yes; break;; \ + esac; \ + done; \ + test $$has_opt = yes +am__make_dryrun = (target_option=n; $(am__make_running_with_option)) +am__make_keepgoing = (target_option=k; $(am__make_running_with_option)) +pkgdatadir = $(datadir)/@PACKAGE@ +pkgincludedir = $(includedir)/@PACKAGE@ +pkglibdir = $(libdir)/@PACKAGE@ +pkglibexecdir = $(libexecdir)/@PACKAGE@ +am__cd = CDPATH="$${ZSH_VERSION+.}$(PATH_SEPARATOR)" && cd +install_sh_DATA = $(install_sh) -c -m 644 +install_sh_PROGRAM = $(install_sh) -c +install_sh_SCRIPT = $(install_sh) -c +INSTALL_HEADER = $(INSTALL_DATA) +transform = $(program_transform_name) +NORMAL_INSTALL = : +PRE_INSTALL = : +POST_INSTALL = : +NORMAL_UNINSTALL = : +PRE_UNINSTALL = : +POST_UNINSTALL = : +build_triplet = @build@ +host_triplet = @host@ +@HAVE_WINDRES_FALSE@metaflac_DEPENDENCIES = $(top_builddir)/src/share/grabbag/libgrabbag.la \ +@HAVE_WINDRES_FALSE@ $(top_builddir)/src/share/replaygain_analysis/libreplaygain_analysis.la \ +@HAVE_WINDRES_FALSE@ $(top_builddir)/src/share/getopt/libgetopt.la \ +@HAVE_WINDRES_FALSE@ $(top_builddir)/src/share/utf8/libutf8.la \ +@HAVE_WINDRES_FALSE@ $(top_builddir)/src/libFLAC/libFLAC.la \ +@HAVE_WINDRES_FALSE@ $(win_utf8_lib) +@OS_IS_WINDOWS_FALSE@metaflac_DEPENDENCIES = $(top_builddir)/src/share/grabbag/libgrabbag.la \ +@OS_IS_WINDOWS_FALSE@ $(top_builddir)/src/share/replaygain_analysis/libreplaygain_analysis.la \ +@OS_IS_WINDOWS_FALSE@ $(top_builddir)/src/share/getopt/libgetopt.la \ +@OS_IS_WINDOWS_FALSE@ $(top_builddir)/src/share/utf8/libutf8.la \ +@OS_IS_WINDOWS_FALSE@ $(top_builddir)/src/libFLAC/libFLAC.la \ +@OS_IS_WINDOWS_FALSE@ $(win_utf8_lib) +bin_PROGRAMS = metaflac$(EXEEXT) +subdir = src/metaflac +ACLOCAL_M4 = $(top_srcdir)/aclocal.m4 +am__aclocal_m4_deps = $(top_srcdir)/m4/add_cflags.m4 \ + $(top_srcdir)/m4/add_cxxflags.m4 \ + $(top_srcdir)/m4/ax_add_fortify_source.m4 \ + $(top_srcdir)/m4/ax_check_compile_flag.m4 \ + $(top_srcdir)/m4/ax_check_enable_debug.m4 \ + $(top_srcdir)/m4/bswap.m4 $(top_srcdir)/m4/clang.m4 \ + $(top_srcdir)/m4/codeset.m4 $(top_srcdir)/m4/gcc_version.m4 \ + $(top_srcdir)/m4/iconv.m4 $(top_srcdir)/m4/lib-ld.m4 \ + $(top_srcdir)/m4/lib-link.m4 $(top_srcdir)/m4/lib-prefix.m4 \ + $(top_srcdir)/m4/libtool.m4 $(top_srcdir)/m4/ltoptions.m4 \ + $(top_srcdir)/m4/ltsugar.m4 $(top_srcdir)/m4/ltversion.m4 \ + $(top_srcdir)/m4/lt~obsolete.m4 $(top_srcdir)/m4/ogg.m4 \ + $(top_srcdir)/m4/really_gcc.m4 \ + $(top_srcdir)/m4/stack_protect.m4 $(top_srcdir)/configure.ac +am__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) \ + $(ACLOCAL_M4) +DIST_COMMON = $(srcdir)/Makefile.am $(am__DIST_COMMON) +mkinstalldirs = $(install_sh) -d +CONFIG_HEADER = $(top_builddir)/config.h +CONFIG_CLEAN_FILES = +CONFIG_CLEAN_VPATH_FILES = +am__installdirs = "$(DESTDIR)$(bindir)" +PROGRAMS = $(bin_PROGRAMS) +am_metaflac_OBJECTS = main.$(OBJEXT) operations.$(OBJEXT) \ + operations_shorthand_cuesheet.$(OBJEXT) \ + operations_shorthand_picture.$(OBJEXT) \ + operations_shorthand_seektable.$(OBJEXT) \ + operations_shorthand_streaminfo.$(OBJEXT) \ + operations_shorthand_vorbiscomment.$(OBJEXT) options.$(OBJEXT) \ + usage.$(OBJEXT) utils.$(OBJEXT) +metaflac_OBJECTS = $(am_metaflac_OBJECTS) +AM_V_lt = $(am__v_lt_@AM_V@) +am__v_lt_ = $(am__v_lt_@AM_DEFAULT_V@) +am__v_lt_0 = --silent +am__v_lt_1 = +metaflac_LINK = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \ + $(LIBTOOLFLAGS) --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \ + $(metaflac_LDFLAGS) $(LDFLAGS) -o $@ +AM_V_P = $(am__v_P_@AM_V@) +am__v_P_ = $(am__v_P_@AM_DEFAULT_V@) +am__v_P_0 = false +am__v_P_1 = : +AM_V_GEN = $(am__v_GEN_@AM_V@) +am__v_GEN_ = $(am__v_GEN_@AM_DEFAULT_V@) +am__v_GEN_0 = @echo " GEN " $@; +am__v_GEN_1 = +AM_V_at = $(am__v_at_@AM_V@) +am__v_at_ = $(am__v_at_@AM_DEFAULT_V@) +am__v_at_0 = @ +am__v_at_1 = +DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir) +depcomp = $(SHELL) $(top_srcdir)/depcomp +am__maybe_remake_depfiles = depfiles +am__depfiles_remade = ./$(DEPDIR)/main.Po ./$(DEPDIR)/operations.Po \ + ./$(DEPDIR)/operations_shorthand_cuesheet.Po \ + ./$(DEPDIR)/operations_shorthand_picture.Po \ + ./$(DEPDIR)/operations_shorthand_seektable.Po \ + ./$(DEPDIR)/operations_shorthand_streaminfo.Po \ + ./$(DEPDIR)/operations_shorthand_vorbiscomment.Po \ + ./$(DEPDIR)/options.Po ./$(DEPDIR)/usage.Po \ + ./$(DEPDIR)/utils.Po +am__mv = mv -f +COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \ + $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) +LTCOMPILE = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \ + $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) \ + $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) \ + $(AM_CFLAGS) $(CFLAGS) +AM_V_CC = $(am__v_CC_@AM_V@) +am__v_CC_ = $(am__v_CC_@AM_DEFAULT_V@) +am__v_CC_0 = @echo " CC " $@; +am__v_CC_1 = +CCLD = $(CC) +LINK = $(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) \ + $(LIBTOOLFLAGS) --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \ + $(AM_LDFLAGS) $(LDFLAGS) -o $@ +AM_V_CCLD = $(am__v_CCLD_@AM_V@) +am__v_CCLD_ = $(am__v_CCLD_@AM_DEFAULT_V@) +am__v_CCLD_0 = @echo " CCLD " $@; +am__v_CCLD_1 = +SOURCES = $(metaflac_SOURCES) +DIST_SOURCES = $(metaflac_SOURCES) +am__can_run_installinfo = \ + case $$AM_UPDATE_INFO_DIR in \ + n|no|NO) false;; \ + *) (install-info --version) >/dev/null 2>&1;; \ + esac +am__tagged_files = $(HEADERS) $(SOURCES) $(TAGS_FILES) $(LISP) +# Read a list of newline-separated strings from the standard input, +# and print each of them once, without duplicates. Input order is +# *not* preserved. +am__uniquify_input = $(AWK) '\ + BEGIN { nonempty = 0; } \ + { items[$$0] = 1; nonempty = 1; } \ + END { if (nonempty) { for (i in items) print i; }; } \ +' +# Make sure the list of sources is unique. This is necessary because, +# e.g., the same source file might be shared among _SOURCES variables +# for different programs/libraries. +am__define_uniq_tagged_files = \ + list='$(am__tagged_files)'; \ + unique=`for i in $$list; do \ + if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \ + done | $(am__uniquify_input)` +am__DIST_COMMON = $(srcdir)/Makefile.in $(top_srcdir)/depcomp +DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST) +ACLOCAL = @ACLOCAL@ +AMTAR = @AMTAR@ +AM_DEFAULT_VERBOSITY = @AM_DEFAULT_VERBOSITY@ +AR = @AR@ +AS = @AS@ +AUTOCONF = @AUTOCONF@ +AUTOHEADER = @AUTOHEADER@ +AUTOMAKE = @AUTOMAKE@ +AWK = @AWK@ +CC = @CC@ +CCAS = @CCAS@ +CCASDEPMODE = @CCASDEPMODE@ +CCASFLAGS = @CCASFLAGS@ +CCDEPMODE = @CCDEPMODE@ +CFLAGS = @CFLAGS@ +CPP = @CPP@ +CPPFLAGS = @CPPFLAGS@ +CSCOPE = @CSCOPE@ +CTAGS = @CTAGS@ +CXX = @CXX@ +CXXCPP = @CXXCPP@ +CXXDEPMODE = @CXXDEPMODE@ +CXXFLAGS = @CXXFLAGS@ +CYGPATH_W = @CYGPATH_W@ +DEFS = @DEFS@ +DEPDIR = @DEPDIR@ +DLLTOOL = @DLLTOOL@ +DOXYGEN = @DOXYGEN@ +DSYMUTIL = @DSYMUTIL@ +DUMPBIN = @DUMPBIN@ +ECHO_C = @ECHO_C@ +ECHO_N = @ECHO_N@ +ECHO_T = @ECHO_T@ +EGREP = @EGREP@ +ENABLE_64_BIT_WORDS = @ENABLE_64_BIT_WORDS@ +ETAGS = @ETAGS@ +EXEEXT = @EXEEXT@ +FGREP = @FGREP@ +FLAC__HAS_OGG = @FLAC__HAS_OGG@ +FLAC__TEST_LEVEL = @FLAC__TEST_LEVEL@ +FLAC__TEST_WITH_VALGRIND = @FLAC__TEST_WITH_VALGRIND@ +GCC_MAJOR_VERSION = @GCC_MAJOR_VERSION@ +GCC_MINOR_VERSION = @GCC_MINOR_VERSION@ +GCC_VERSION = @GCC_VERSION@ +GIT_COMMIT_VERSION_HASH = @GIT_COMMIT_VERSION_HASH@ +GIT_FOUND = @GIT_FOUND@ +GREP = @GREP@ +INSTALL = @INSTALL@ +INSTALL_DATA = @INSTALL_DATA@ +INSTALL_PROGRAM = @INSTALL_PROGRAM@ +INSTALL_SCRIPT = @INSTALL_SCRIPT@ +INSTALL_STRIP_PROGRAM = @INSTALL_STRIP_PROGRAM@ +LD = @LD@ +LDFLAGS = @LDFLAGS@ +LIBICONV = @LIBICONV@ +LIBOBJS = @LIBOBJS@ +LIBS = @LIBS@ +LIBTOOL = @LIBTOOL@ +LIB_CLOCK_GETTIME = @LIB_CLOCK_GETTIME@ +LIB_FUZZING_ENGINE = @LIB_FUZZING_ENGINE@ +LIPO = @LIPO@ +LN_S = @LN_S@ +LTLIBICONV = @LTLIBICONV@ +LTLIBOBJS = @LTLIBOBJS@ +LT_SYS_LIBRARY_PATH = @LT_SYS_LIBRARY_PATH@ +MAKEINFO = @MAKEINFO@ +MANIFEST_TOOL = @MANIFEST_TOOL@ +MKDIR_P = @MKDIR_P@ +NM = @NM@ +NMEDIT = @NMEDIT@ +OBJDUMP = @OBJDUMP@ +OBJEXT = @OBJEXT@ +OBJ_FORMAT = @OBJ_FORMAT@ +OGG_CFLAGS = @OGG_CFLAGS@ +OGG_LIBS = @OGG_LIBS@ +OGG_PACKAGE = @OGG_PACKAGE@ +OTOOL = @OTOOL@ +OTOOL64 = @OTOOL64@ +PACKAGE = @PACKAGE@ +PACKAGE_BUGREPORT = @PACKAGE_BUGREPORT@ +PACKAGE_NAME = @PACKAGE_NAME@ +PACKAGE_STRING = @PACKAGE_STRING@ +PACKAGE_TARNAME = @PACKAGE_TARNAME@ +PACKAGE_URL = @PACKAGE_URL@ +PACKAGE_VERSION = @PACKAGE_VERSION@ +PANDOC = @PANDOC@ +PATH_SEPARATOR = @PATH_SEPARATOR@ +RANLIB = @RANLIB@ +RC = @RC@ +SED = @SED@ +SET_MAKE = @SET_MAKE@ +SHELL = @SHELL@ +STRIP = @STRIP@ +VERSION = @VERSION@ +abs_builddir = @abs_builddir@ +abs_srcdir = @abs_srcdir@ +abs_top_builddir = @abs_top_builddir@ +abs_top_srcdir = @abs_top_srcdir@ +ac_ct_AR = @ac_ct_AR@ +ac_ct_CC = @ac_ct_CC@ +ac_ct_CXX = @ac_ct_CXX@ +ac_ct_DUMPBIN = @ac_ct_DUMPBIN@ +am__include = @am__include@ +am__leading_dot = @am__leading_dot@ +am__quote = @am__quote@ +am__tar = @am__tar@ +am__untar = @am__untar@ +bindir = @bindir@ +build = @build@ +build_alias = @build_alias@ +build_cpu = @build_cpu@ +build_os = @build_os@ +build_vendor = @build_vendor@ +builddir = @builddir@ +datadir = @datadir@ +datarootdir = @datarootdir@ +docdir = @docdir@ +dvidir = @dvidir@ +exec_prefix = @exec_prefix@ +host = @host@ +host_alias = @host_alias@ +host_cpu = @host_cpu@ +host_os = @host_os@ +host_vendor = @host_vendor@ +htmldir = @htmldir@ +includedir = @includedir@ +infodir = @infodir@ +install_sh = @install_sh@ +libdir = @libdir@ +libexecdir = @libexecdir@ +localedir = @localedir@ +localstatedir = @localstatedir@ +mandir = @mandir@ +mkdir_p = @mkdir_p@ +oldincludedir = @oldincludedir@ +pdfdir = @pdfdir@ +prefix = @prefix@ +program_transform_name = @program_transform_name@ +psdir = @psdir@ +runstatedir = @runstatedir@ +sbindir = @sbindir@ +sharedstatedir = @sharedstatedir@ +srcdir = @srcdir@ +sysconfdir = @sysconfdir@ +target_alias = @target_alias@ +top_build_prefix = @top_build_prefix@ +top_builddir = @top_builddir@ +top_srcdir = @top_srcdir@ +@OS_IS_WINDOWS_TRUE@win_utf8_lib = $(top_builddir)/src/share/win_utf8_io/libwin_utf8_io.la +@HAVE_WINDRES_TRUE@@OS_IS_WINDOWS_TRUE@metaflac_DEPENDENCIES = version.o +@HAVE_WINDRES_TRUE@@OS_IS_WINDOWS_TRUE@windows_resource_link = -Wl,version.o +AM_CFLAGS = @OGG_CFLAGS@ +AM_CPPFLAGS = -I$(top_builddir) -I$(srcdir)/include -I$(top_srcdir)/include +EXTRA_DIST = \ + CMakeLists.txt \ + version.rc + +metaflac_SOURCES = \ + main.c \ + operations.c \ + operations_shorthand_cuesheet.c \ + operations_shorthand_picture.c \ + operations_shorthand_seektable.c \ + operations_shorthand_streaminfo.c \ + operations_shorthand_vorbiscomment.c \ + options.c \ + usage.c \ + utils.c \ + operations.h \ + operations_shorthand.h \ + options.h \ + usage.h \ + utils.h + +metaflac_LDFLAGS = $(AM_LDFLAGS) $(windows_resource_link) +metaflac_LDADD = \ + $(top_builddir)/src/share/grabbag/libgrabbag.la \ + $(top_builddir)/src/share/replaygain_analysis/libreplaygain_analysis.la \ + $(top_builddir)/src/share/getopt/libgetopt.la \ + $(top_builddir)/src/share/utf8/libutf8.la \ + $(top_builddir)/src/libFLAC/libFLAC.la \ + $(win_utf8_lib) \ + @LTLIBICONV@ + +CLEANFILES = metaflac.exe +all: all-am + +.SUFFIXES: +.SUFFIXES: .c .lo .o .obj .rc +$(srcdir)/Makefile.in: $(srcdir)/Makefile.am $(am__configure_deps) + @for dep in $?; do \ + case '$(am__configure_deps)' in \ + *$$dep*) \ + ( cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh ) \ + && { if test -f $@; then exit 0; else break; fi; }; \ + exit 1;; \ + esac; \ + done; \ + echo ' cd $(top_srcdir) && $(AUTOMAKE) --foreign src/metaflac/Makefile'; \ + $(am__cd) $(top_srcdir) && \ + $(AUTOMAKE) --foreign src/metaflac/Makefile +Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status + @case '$?' in \ + *config.status*) \ + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh;; \ + *) \ + echo ' cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__maybe_remake_depfiles)'; \ + cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__maybe_remake_depfiles);; \ + esac; + +$(top_builddir)/config.status: $(top_srcdir)/configure $(CONFIG_STATUS_DEPENDENCIES) + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh + +$(top_srcdir)/configure: $(am__configure_deps) + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh +$(ACLOCAL_M4): $(am__aclocal_m4_deps) + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh +$(am__aclocal_m4_deps): +install-binPROGRAMS: $(bin_PROGRAMS) + @$(NORMAL_INSTALL) + @list='$(bin_PROGRAMS)'; test -n "$(bindir)" || list=; \ + if test -n "$$list"; then \ + echo " $(MKDIR_P) '$(DESTDIR)$(bindir)'"; \ + $(MKDIR_P) "$(DESTDIR)$(bindir)" || exit 1; \ + fi; \ + for p in $$list; do echo "$$p $$p"; done | \ + sed 's/$(EXEEXT)$$//' | \ + while read p p1; do if test -f $$p \ + || test -f $$p1 \ + ; then echo "$$p"; echo "$$p"; else :; fi; \ + done | \ + sed -e 'p;s,.*/,,;n;h' \ + -e 's|.*|.|' \ + -e 'p;x;s,.*/,,;s/$(EXEEXT)$$//;$(transform);s/$$/$(EXEEXT)/' | \ + sed 'N;N;N;s,\n, ,g' | \ + $(AWK) 'BEGIN { files["."] = ""; dirs["."] = 1 } \ + { d=$$3; if (dirs[d] != 1) { print "d", d; dirs[d] = 1 } \ + if ($$2 == $$4) files[d] = files[d] " " $$1; \ + else { print "f", $$3 "/" $$4, $$1; } } \ + END { for (d in files) print "f", d, files[d] }' | \ + while read type dir files; do \ + if test "$$dir" = .; then dir=; else dir=/$$dir; fi; \ + test -z "$$files" || { \ + echo " $(INSTALL_PROGRAM_ENV) $(LIBTOOL) $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=install $(INSTALL_PROGRAM) $$files '$(DESTDIR)$(bindir)$$dir'"; \ + $(INSTALL_PROGRAM_ENV) $(LIBTOOL) $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=install $(INSTALL_PROGRAM) $$files "$(DESTDIR)$(bindir)$$dir" || exit $$?; \ + } \ + ; done + +uninstall-binPROGRAMS: + @$(NORMAL_UNINSTALL) + @list='$(bin_PROGRAMS)'; test -n "$(bindir)" || list=; \ + files=`for p in $$list; do echo "$$p"; done | \ + sed -e 'h;s,^.*/,,;s/$(EXEEXT)$$//;$(transform)' \ + -e 's/$$/$(EXEEXT)/' \ + `; \ + test -n "$$list" || exit 0; \ + echo " ( cd '$(DESTDIR)$(bindir)' && rm -f" $$files ")"; \ + cd "$(DESTDIR)$(bindir)" && rm -f $$files + +clean-binPROGRAMS: + @list='$(bin_PROGRAMS)'; test -n "$$list" || exit 0; \ + echo " rm -f" $$list; \ + rm -f $$list || exit $$?; \ + test -n "$(EXEEXT)" || exit 0; \ + list=`for p in $$list; do echo "$$p"; done | sed 's/$(EXEEXT)$$//'`; \ + echo " rm -f" $$list; \ + rm -f $$list + +metaflac$(EXEEXT): $(metaflac_OBJECTS) $(metaflac_DEPENDENCIES) $(EXTRA_metaflac_DEPENDENCIES) + @rm -f metaflac$(EXEEXT) + $(AM_V_CCLD)$(metaflac_LINK) $(metaflac_OBJECTS) $(metaflac_LDADD) $(LIBS) + +mostlyclean-compile: + -rm -f *.$(OBJEXT) + +distclean-compile: + -rm -f *.tab.c + +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/main.Po@am__quote@ # am--include-marker +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/operations.Po@am__quote@ # am--include-marker +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/operations_shorthand_cuesheet.Po@am__quote@ # am--include-marker +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/operations_shorthand_picture.Po@am__quote@ # am--include-marker +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/operations_shorthand_seektable.Po@am__quote@ # am--include-marker +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/operations_shorthand_streaminfo.Po@am__quote@ # am--include-marker +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/operations_shorthand_vorbiscomment.Po@am__quote@ # am--include-marker +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/options.Po@am__quote@ # am--include-marker +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/usage.Po@am__quote@ # am--include-marker +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/utils.Po@am__quote@ # am--include-marker + +$(am__depfiles_remade): + @$(MKDIR_P) $(@D) + @echo '# dummy' >$@-t && $(am__mv) $@-t $@ + +am--depfiles: $(am__depfiles_remade) + +.c.o: +@am__fastdepCC_TRUE@ $(AM_V_CC)depbase=`echo $@ | sed 's|[^/]*$$|$(DEPDIR)/&|;s|\.o$$||'`;\ +@am__fastdepCC_TRUE@ $(COMPILE) -MT $@ -MD -MP -MF $$depbase.Tpo -c -o $@ $< &&\ +@am__fastdepCC_TRUE@ $(am__mv) $$depbase.Tpo $$depbase.Po +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='$<' object='$@' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(COMPILE) -c -o $@ $< + +.c.obj: +@am__fastdepCC_TRUE@ $(AM_V_CC)depbase=`echo $@ | sed 's|[^/]*$$|$(DEPDIR)/&|;s|\.obj$$||'`;\ +@am__fastdepCC_TRUE@ $(COMPILE) -MT $@ -MD -MP -MF $$depbase.Tpo -c -o $@ `$(CYGPATH_W) '$<'` &&\ +@am__fastdepCC_TRUE@ $(am__mv) $$depbase.Tpo $$depbase.Po +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='$<' object='$@' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(COMPILE) -c -o $@ `$(CYGPATH_W) '$<'` + +.c.lo: +@am__fastdepCC_TRUE@ $(AM_V_CC)depbase=`echo $@ | sed 's|[^/]*$$|$(DEPDIR)/&|;s|\.lo$$||'`;\ +@am__fastdepCC_TRUE@ $(LTCOMPILE) -MT $@ -MD -MP -MF $$depbase.Tpo -c -o $@ $< &&\ +@am__fastdepCC_TRUE@ $(am__mv) $$depbase.Tpo $$depbase.Plo +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='$<' object='$@' libtool=yes @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(LTCOMPILE) -c -o $@ $< + +mostlyclean-libtool: + -rm -f *.lo + +clean-libtool: + -rm -rf .libs _libs + +ID: $(am__tagged_files) + $(am__define_uniq_tagged_files); mkid -fID $$unique +tags: tags-am +TAGS: tags + +tags-am: $(TAGS_DEPENDENCIES) $(am__tagged_files) + set x; \ + here=`pwd`; \ + $(am__define_uniq_tagged_files); \ + shift; \ + if test -z "$(ETAGS_ARGS)$$*$$unique"; then :; else \ + test -n "$$unique" || unique=$$empty_fix; \ + if test $$# -gt 0; then \ + $(ETAGS) $(ETAGSFLAGS) $(AM_ETAGSFLAGS) $(ETAGS_ARGS) \ + "$$@" $$unique; \ + else \ + $(ETAGS) $(ETAGSFLAGS) $(AM_ETAGSFLAGS) $(ETAGS_ARGS) \ + $$unique; \ + fi; \ + fi +ctags: ctags-am + +CTAGS: ctags +ctags-am: $(TAGS_DEPENDENCIES) $(am__tagged_files) + $(am__define_uniq_tagged_files); \ + test -z "$(CTAGS_ARGS)$$unique" \ + || $(CTAGS) $(CTAGSFLAGS) $(AM_CTAGSFLAGS) $(CTAGS_ARGS) \ + $$unique + +GTAGS: + here=`$(am__cd) $(top_builddir) && pwd` \ + && $(am__cd) $(top_srcdir) \ + && gtags -i $(GTAGS_ARGS) "$$here" +cscopelist: cscopelist-am + +cscopelist-am: $(am__tagged_files) + list='$(am__tagged_files)'; \ + case "$(srcdir)" in \ + [\\/]* | ?:[\\/]*) sdir="$(srcdir)" ;; \ + *) sdir=$(subdir)/$(srcdir) ;; \ + esac; \ + for i in $$list; do \ + if test -f "$$i"; then \ + echo "$(subdir)/$$i"; \ + else \ + echo "$$sdir/$$i"; \ + fi; \ + done >> $(top_builddir)/cscope.files + +distclean-tags: + -rm -f TAGS ID GTAGS GRTAGS GSYMS GPATH tags +distdir: $(BUILT_SOURCES) + $(MAKE) $(AM_MAKEFLAGS) distdir-am + +distdir-am: $(DISTFILES) + @srcdirstrip=`echo "$(srcdir)" | sed 's/[].[^$$\\*]/\\\\&/g'`; \ + topsrcdirstrip=`echo "$(top_srcdir)" | sed 's/[].[^$$\\*]/\\\\&/g'`; \ + list='$(DISTFILES)'; \ + dist_files=`for file in $$list; do echo $$file; done | \ + sed -e "s|^$$srcdirstrip/||;t" \ + -e "s|^$$topsrcdirstrip/|$(top_builddir)/|;t"`; \ + case $$dist_files in \ + */*) $(MKDIR_P) `echo "$$dist_files" | \ + sed '/\//!d;s|^|$(distdir)/|;s,/[^/]*$$,,' | \ + sort -u` ;; \ + esac; \ + for file in $$dist_files; do \ + if test -f $$file || test -d $$file; then d=.; else d=$(srcdir); fi; \ + if test -d $$d/$$file; then \ + dir=`echo "/$$file" | sed -e 's,/[^/]*$$,,'`; \ + if test -d "$(distdir)/$$file"; then \ + find "$(distdir)/$$file" -type d ! -perm -700 -exec chmod u+rwx {} \;; \ + fi; \ + if test -d $(srcdir)/$$file && test $$d != $(srcdir); then \ + cp -fpR $(srcdir)/$$file "$(distdir)$$dir" || exit 1; \ + find "$(distdir)/$$file" -type d ! -perm -700 -exec chmod u+rwx {} \;; \ + fi; \ + cp -fpR $$d/$$file "$(distdir)$$dir" || exit 1; \ + else \ + test -f "$(distdir)/$$file" \ + || cp -p $$d/$$file "$(distdir)/$$file" \ + || exit 1; \ + fi; \ + done +check-am: all-am +check: check-am +all-am: Makefile $(PROGRAMS) +installdirs: + for dir in "$(DESTDIR)$(bindir)"; do \ + test -z "$$dir" || $(MKDIR_P) "$$dir"; \ + done +install: install-am +install-exec: install-exec-am +install-data: install-data-am +uninstall: uninstall-am + +install-am: all-am + @$(MAKE) $(AM_MAKEFLAGS) install-exec-am install-data-am + +installcheck: installcheck-am +install-strip: + if test -z '$(STRIP)'; then \ + $(MAKE) $(AM_MAKEFLAGS) INSTALL_PROGRAM="$(INSTALL_STRIP_PROGRAM)" \ + install_sh_PROGRAM="$(INSTALL_STRIP_PROGRAM)" INSTALL_STRIP_FLAG=-s \ + install; \ + else \ + $(MAKE) $(AM_MAKEFLAGS) INSTALL_PROGRAM="$(INSTALL_STRIP_PROGRAM)" \ + install_sh_PROGRAM="$(INSTALL_STRIP_PROGRAM)" INSTALL_STRIP_FLAG=-s \ + "INSTALL_PROGRAM_ENV=STRIPPROG='$(STRIP)'" install; \ + fi +mostlyclean-generic: + +clean-generic: + -test -z "$(CLEANFILES)" || rm -f $(CLEANFILES) + +distclean-generic: + -test -z "$(CONFIG_CLEAN_FILES)" || rm -f $(CONFIG_CLEAN_FILES) + -test . = "$(srcdir)" || test -z "$(CONFIG_CLEAN_VPATH_FILES)" || rm -f $(CONFIG_CLEAN_VPATH_FILES) + +maintainer-clean-generic: + @echo "This command is intended for maintainers to use" + @echo "it deletes files that may require special tools to rebuild." +clean: clean-am + +clean-am: clean-binPROGRAMS clean-generic clean-libtool mostlyclean-am + +distclean: distclean-am + -rm -f ./$(DEPDIR)/main.Po + -rm -f ./$(DEPDIR)/operations.Po + -rm -f ./$(DEPDIR)/operations_shorthand_cuesheet.Po + -rm -f ./$(DEPDIR)/operations_shorthand_picture.Po + -rm -f ./$(DEPDIR)/operations_shorthand_seektable.Po + -rm -f ./$(DEPDIR)/operations_shorthand_streaminfo.Po + -rm -f ./$(DEPDIR)/operations_shorthand_vorbiscomment.Po + -rm -f ./$(DEPDIR)/options.Po + -rm -f ./$(DEPDIR)/usage.Po + -rm -f ./$(DEPDIR)/utils.Po + -rm -f Makefile +distclean-am: clean-am distclean-compile distclean-generic \ + distclean-tags + +dvi: dvi-am + +dvi-am: + +html: html-am + +html-am: + +info: info-am + +info-am: + +install-data-am: + +install-dvi: install-dvi-am + +install-dvi-am: + +install-exec-am: install-binPROGRAMS + +install-html: install-html-am + +install-html-am: + +install-info: install-info-am + +install-info-am: + +install-man: + +install-pdf: install-pdf-am + +install-pdf-am: + +install-ps: install-ps-am + +install-ps-am: + +installcheck-am: + +maintainer-clean: maintainer-clean-am + -rm -f ./$(DEPDIR)/main.Po + -rm -f ./$(DEPDIR)/operations.Po + -rm -f ./$(DEPDIR)/operations_shorthand_cuesheet.Po + -rm -f ./$(DEPDIR)/operations_shorthand_picture.Po + -rm -f ./$(DEPDIR)/operations_shorthand_seektable.Po + -rm -f ./$(DEPDIR)/operations_shorthand_streaminfo.Po + -rm -f ./$(DEPDIR)/operations_shorthand_vorbiscomment.Po + -rm -f ./$(DEPDIR)/options.Po + -rm -f ./$(DEPDIR)/usage.Po + -rm -f ./$(DEPDIR)/utils.Po + -rm -f Makefile +maintainer-clean-am: distclean-am maintainer-clean-generic + +mostlyclean: mostlyclean-am + +mostlyclean-am: mostlyclean-compile mostlyclean-generic \ + mostlyclean-libtool + +pdf: pdf-am + +pdf-am: + +ps: ps-am + +ps-am: + +uninstall-am: uninstall-binPROGRAMS + +.MAKE: install-am install-strip + +.PHONY: CTAGS GTAGS TAGS all all-am am--depfiles check check-am clean \ + clean-binPROGRAMS clean-generic clean-libtool cscopelist-am \ + ctags ctags-am distclean distclean-compile distclean-generic \ + distclean-libtool distclean-tags distdir dvi dvi-am html \ + html-am info info-am install install-am install-binPROGRAMS \ + install-data install-data-am install-dvi install-dvi-am \ + install-exec install-exec-am install-html install-html-am \ + install-info install-info-am install-man install-pdf \ + install-pdf-am install-ps install-ps-am install-strip \ + installcheck installcheck-am installdirs maintainer-clean \ + maintainer-clean-generic mostlyclean mostlyclean-compile \ + mostlyclean-generic mostlyclean-libtool pdf pdf-am ps ps-am \ + tags tags-am uninstall uninstall-am uninstall-binPROGRAMS + +.PRECIOUS: Makefile + + +.rc.o: + $(RC) $(AM_CPPFLAGS) $< $@ + +# Tell versions [3.59,3.63) of GNU make to not export all variables. +# Otherwise a system limit (for SysV at least) may be exceeded. +.NOEXPORT: diff --git a/src/metaflac/main.c b/src/metaflac/main.c new file mode 100644 index 0000000..bb66293 --- /dev/null +++ b/src/metaflac/main.c @@ -0,0 +1,75 @@ +/* metaflac - Command-line FLAC metadata editor + * Copyright (C) 2001-2009 Josh Coalson + * Copyright (C) 2011-2023 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 + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include "operations.h" +#include "options.h" +#include <locale.h> +#include <stdlib.h> +#include <string.h> +#include "share/compat.h" + +#ifndef FUZZ_TOOL_METAFLAC +int main(int argc, char *argv[]) +#else +static int main_to_fuzz(int argc, char *argv[]) +#endif +{ + CommandLineOptions options; + int ret = 0; + +#ifdef __EMX__ + _response(&argc, &argv); + _wildcard(&argc, &argv); +#endif +#ifdef _WIN32 + if (get_utf8_argv(&argc, &argv) != 0) { + fputs("ERROR: failed to convert command line parameters to UTF-8\n", stderr); + return 1; + } +#endif + +#ifdef _WIN32 + { + const char *var; + var = getenv("LC_ALL"); + if (!var) + var = getenv("LC_NUMERIC"); + if (!var) + var = getenv("LANG"); + if (!var || strcmp(var, "C") != 0) + setlocale(LC_ALL, ""); + } +#else + setlocale(LC_ALL, ""); +#endif + init_options(&options); + + if ((ret = parse_options(argc, argv, &options)) == 0) + ret = !do_operations(&options); + else + ret = 1; + + free_options(&options); + + return ret; +} diff --git a/src/metaflac/operations.c b/src/metaflac/operations.c new file mode 100644 index 0000000..d81d87a --- /dev/null +++ b/src/metaflac/operations.c @@ -0,0 +1,823 @@ +/* metaflac - Command-line FLAC metadata editor + * Copyright (C) 2001-2009 Josh Coalson + * Copyright (C) 2011-2023 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 + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include "operations.h" +#include "usage.h" +#include "utils.h" +#include "FLAC/assert.h" +#include "FLAC/metadata.h" +#include "share/alloc.h" +#include "share/grabbag.h" +#include "share/compat.h" +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include "operations_shorthand.h" + +static void show_version(void); +static FLAC__bool do_major_operation(const CommandLineOptions *options); +static FLAC__bool do_major_operation_on_file(const char *filename, const CommandLineOptions *options); +static FLAC__bool do_major_operation__list(const char *filename, FLAC__Metadata_Chain *chain, const CommandLineOptions *options); +static FLAC__bool do_major_operation__append(FLAC__Metadata_Chain *chain, const CommandLineOptions *options); +static FLAC__bool do_major_operation__remove(FLAC__Metadata_Chain *chain, const CommandLineOptions *options); +static FLAC__bool do_major_operation__remove_all(FLAC__Metadata_Chain *chain, const CommandLineOptions *options); +static FLAC__bool do_shorthand_operations(const CommandLineOptions *options); +static FLAC__bool do_shorthand_operations_on_file(const char *filename, const CommandLineOptions *options); +static FLAC__bool do_shorthand_operation(const char *filename, FLAC__bool prefix_with_filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write, FLAC__bool utf8_convert); +static FLAC__bool do_shorthand_operation__add_replay_gain(char **filenames, unsigned num_files, FLAC__bool preserve_modtime, FLAC__bool scan); +static FLAC__bool do_shorthand_operation__add_padding(const char *filename, FLAC__Metadata_Chain *chain, unsigned length, FLAC__bool *needs_write); + +static FLAC__bool passes_filter(const CommandLineOptions *options, const FLAC__StreamMetadata *block, unsigned block_number); +static void write_metadata(const char *filename, FLAC__StreamMetadata *block, unsigned block_number, FLAC__bool raw, FLAC__bool hexdump_application); +static void write_metadata_binary(FLAC__StreamMetadata *block, FLAC__byte *block_raw, FLAC__bool headerless); + +/* from operations_shorthand_seektable.c */ +extern FLAC__bool do_shorthand_operation__add_seekpoints(const char *filename, FLAC__Metadata_Chain *chain, const char *specification, FLAC__bool *needs_write); + +/* from operations_shorthand_streaminfo.c */ +extern FLAC__bool do_shorthand_operation__streaminfo(const char *filename, FLAC__bool prefix_with_filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write); + +/* from operations_shorthand_vorbiscomment.c */ +extern FLAC__bool do_shorthand_operation__vorbis_comment(const char *filename, FLAC__bool prefix_with_filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write, FLAC__bool raw); + +/* from operations_shorthand_cuesheet.c */ +extern FLAC__bool do_shorthand_operation__cuesheet(const char *filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write); + +/* from operations_shorthand_picture.c */ +extern FLAC__bool do_shorthand_operation__picture(const char *filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write); + + +FLAC__bool do_operations(const CommandLineOptions *options) +{ + FLAC__bool ok = true; + + if(options->show_long_help) { + long_usage(0); + } + if(options->show_version) { + show_version(); + } + else if(options->args.checks.num_major_ops > 0) { + FLAC__ASSERT(options->args.checks.num_shorthand_ops == 0); + FLAC__ASSERT(options->args.checks.num_major_ops == 1); + FLAC__ASSERT(options->args.checks.num_major_ops == options->ops.num_operations); + ok = do_major_operation(options); + } + else if(options->args.checks.num_shorthand_ops > 0) { + FLAC__ASSERT(options->args.checks.num_shorthand_ops == options->ops.num_operations); + ok = do_shorthand_operations(options); + } + + return ok; +} + +/* + * local routines + */ + +void show_version(void) +{ + printf("metaflac %s\n", FLAC__VERSION_STRING); +} + +FLAC__bool do_major_operation(const CommandLineOptions *options) +{ + unsigned i; + FLAC__bool ok = true; + + /* to die after first error, v--- add '&& ok' here */ + for(i = 0; i < options->num_files; i++) + ok &= do_major_operation_on_file(options->filenames[i], options); + + return ok; +} + +FLAC__bool do_major_operation_on_file(const char *filename, const CommandLineOptions *options) +{ + FLAC__bool ok = true, needs_write = false, is_ogg = false; + FLAC__Metadata_Chain *chain = FLAC__metadata_chain_new(); + + if(0 == chain) + die("out of memory allocating chain"); + + /*@@@@ lame way of guessing the file type */ + if(strlen(filename) >= 4 && (0 == strcmp(filename+strlen(filename)-4, ".oga") || 0 == strcmp(filename+strlen(filename)-4, ".ogg"))) + is_ogg = true; + + if(! (is_ogg? FLAC__metadata_chain_read_ogg(chain, filename) : FLAC__metadata_chain_read(chain, filename)) ) { + print_error_with_chain_status(chain, "%s: ERROR: reading metadata", filename); + FLAC__metadata_chain_delete(chain); + return false; + } + + switch(options->ops.operations[0].type) { + case OP__LIST: + ok = do_major_operation__list(options->prefix_with_filename? filename : 0, chain, options); + break; + case OP__APPEND: + ok = do_major_operation__append(chain, options); + needs_write = true; + break; + case OP__REMOVE: + ok = do_major_operation__remove(chain, options); + needs_write = true; + break; + case OP__REMOVE_ALL: + ok = do_major_operation__remove_all(chain, options); + needs_write = true; + break; + case OP__MERGE_PADDING: + FLAC__metadata_chain_merge_padding(chain); + needs_write = true; + break; + case OP__SORT_PADDING: + FLAC__metadata_chain_sort_padding(chain); + needs_write = true; + break; + default: + FLAC__ASSERT(0); + return false; + } + + if(ok && needs_write) { + if(options->use_padding) + FLAC__metadata_chain_sort_padding(chain); + ok = FLAC__metadata_chain_write(chain, options->use_padding, options->preserve_modtime); + if(!ok) + print_error_with_chain_status(chain, "%s: ERROR: writing FLAC file", filename); + } + + FLAC__metadata_chain_delete(chain); + + return ok; +} + +FLAC__bool do_major_operation__list(const char *filename, FLAC__Metadata_Chain *chain, const CommandLineOptions *options) +{ + FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new(); + FLAC__StreamMetadata *block; + FLAC__bool ok = true; + unsigned block_number; + + if(0 == iterator) + die("out of memory allocating iterator"); + + FLAC__metadata_iterator_init(iterator, chain); + + block_number = 0; + do { + block = FLAC__metadata_iterator_get_block(iterator); + ok &= (0 != block); + if(!ok) + flac_fprintf(stderr, "%s: ERROR: couldn't get block from chain\n", filename); + else if(passes_filter(options, FLAC__metadata_iterator_get_block(iterator), block_number)) { + if(!options->data_format_is_binary && !options->data_format_is_binary_headerless) + write_metadata(filename, block, block_number, !options->utf8_convert, options->application_data_format_is_hexdump); + else { + FLAC__byte * block_raw = FLAC__metadata_object_get_raw(block); + if(block_raw == 0) { + flac_fprintf(stderr, "%s: ERROR: couldn't get block in raw form\n", filename); + FLAC__metadata_iterator_delete(iterator); + return false; + } + write_metadata_binary(block, block_raw, options->data_format_is_binary_headerless); + free(block_raw); + } + } + block_number++; + } while(ok && FLAC__metadata_iterator_next(iterator)); + + FLAC__metadata_iterator_delete(iterator); + + return ok; +} + +FLAC__bool do_major_operation__append(FLAC__Metadata_Chain *chain, const CommandLineOptions *options) +{ + FLAC__byte header[FLAC__STREAM_METADATA_HEADER_LENGTH]; + FLAC__byte *buffer; + FLAC__uint32 buffer_size, num_objects = 0, i, append_after = UINT32_MAX; + FLAC__StreamMetadata *object; + FLAC__Metadata_Iterator *iterator = 0; + FLAC__bool has_vorbiscomment = false; + + /* First, find out after which block appending should take place */ + for(i = 0; i < options->args.num_arguments; i++) { + if(options->args.arguments[i].type == ARG__BLOCK_NUMBER) { + if(append_after != UINT32_MAX || options->args.arguments[i].value.block_number.num_entries > 1) { + flac_fprintf(stderr, "ERROR: more than one block number specified with --append\n"); + return false; + } + append_after = options->args.arguments[i].value.block_number.entries[0]; + } + } + + iterator = FLAC__metadata_iterator_new(); + + if(0 == iterator) + die("out of memory allocating iterator"); + + FLAC__metadata_iterator_init(iterator, chain); + + /* Find out whether there is already a vorbis comment block present */ + do { + FLAC__MetadataType type = FLAC__metadata_iterator_get_block_type(iterator); + if(type == FLAC__METADATA_TYPE_VORBIS_COMMENT) + has_vorbiscomment = true; + } + while(FLAC__metadata_iterator_next(iterator)); + + /* Reset iterator */ + FLAC__metadata_iterator_init(iterator, chain); + + /* Go to requested block */ + for(i = 0; i < append_after; i++) { + if(!FLAC__metadata_iterator_next(iterator)) + break; + } + +#ifdef _WIN32 + _setmode(fileno(stdin),_O_BINARY); +#endif + + /* Read header from stdin */ + while(fread(header, 1, FLAC__STREAM_METADATA_HEADER_LENGTH, stdin) == FLAC__STREAM_METADATA_HEADER_LENGTH) { + + buffer_size = ((FLAC__uint32)(header[1]) << 16) + ((FLAC__uint32)(header[2]) << 8) + header[3]; + buffer = safe_malloc_(buffer_size + FLAC__STREAM_METADATA_HEADER_LENGTH); + if(0 == buffer) + die("out of memory allocating read buffer"); + memcpy(buffer, header, FLAC__STREAM_METADATA_HEADER_LENGTH); + + num_objects++; + + if(fread(buffer+FLAC__STREAM_METADATA_HEADER_LENGTH, 1, buffer_size, stdin) < buffer_size) { + flac_fprintf(stderr, "ERROR: couldn't read metadata block #%u from stdin\n",(num_objects)); + free(buffer); + FLAC__metadata_iterator_delete(iterator); + return false; + } + + if((object = FLAC__metadata_object_set_raw(buffer, buffer_size + FLAC__STREAM_METADATA_HEADER_LENGTH)) == NULL) { + flac_fprintf(stderr, "ERROR: couldn't parse supplied metadata block #%u\n",(num_objects)); + free(buffer); + FLAC__metadata_iterator_delete(iterator); + return false; + } + free(buffer); + + if(has_vorbiscomment && object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) { + flac_fprintf(stderr, "ERROR: can't add another vorbis comment block to file, it already has one\n"); + FLAC__metadata_object_delete(object); + FLAC__metadata_iterator_delete(iterator); + return false; + } + + + if(object->type == FLAC__METADATA_TYPE_STREAMINFO) { + flac_fprintf(stderr, "ERROR: can't add streaminfo to file\n"); + FLAC__metadata_object_delete(object); + FLAC__metadata_iterator_delete(iterator); + return false; + } + + if(object->type == FLAC__METADATA_TYPE_SEEKTABLE) { + flac_fprintf(stderr, "ERROR: can't add seektable to file, please use --add-seekpoint instead\n"); + FLAC__metadata_object_delete(object); + FLAC__metadata_iterator_delete(iterator); + return false; + } + + if(!FLAC__metadata_iterator_insert_block_after(iterator, object)) { + flac_fprintf(stderr, "ERROR: couldn't add supplied metadata block #%u to file\n",(num_objects)); + FLAC__metadata_object_delete(object); + FLAC__metadata_iterator_delete(iterator); + return false; + } + /* Now check whether what type of block was added */ + { + FLAC__MetadataType type = FLAC__metadata_iterator_get_block_type(iterator); + if(type == FLAC__METADATA_TYPE_VORBIS_COMMENT) + has_vorbiscomment = true; + } + } + +#ifdef _WIN32 + _setmode(fileno(stdin),_O_TEXT); +#endif + + if(num_objects == 0) + flac_fprintf(stderr, "ERROR: unable to find a metadata block in the supplied input\n"); + + FLAC__metadata_iterator_delete(iterator); + + return true; +} + +FLAC__bool do_major_operation__remove(FLAC__Metadata_Chain *chain, const CommandLineOptions *options) +{ + FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new(); + FLAC__bool ok = true; + unsigned block_number; + + if(0 == iterator) + die("out of memory allocating iterator"); + + FLAC__metadata_iterator_init(iterator, chain); + + block_number = 0; + while(ok && FLAC__metadata_iterator_next(iterator)) { + block_number++; + if(passes_filter(options, FLAC__metadata_iterator_get_block(iterator), block_number)) { + ok &= FLAC__metadata_iterator_delete_block(iterator, options->use_padding); + if(options->use_padding) + ok &= FLAC__metadata_iterator_next(iterator); + } + } + + FLAC__metadata_iterator_delete(iterator); + + return ok; +} + +FLAC__bool do_major_operation__remove_all(FLAC__Metadata_Chain *chain, const CommandLineOptions *options) +{ + FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new(); + FLAC__bool ok = true; + + if(0 == iterator) + die("out of memory allocating iterator"); + + FLAC__metadata_iterator_init(iterator, chain); + + while(ok && FLAC__metadata_iterator_next(iterator)) { + ok &= FLAC__metadata_iterator_delete_block(iterator, options->use_padding); + if(options->use_padding) + ok &= FLAC__metadata_iterator_next(iterator); + } + + FLAC__metadata_iterator_delete(iterator); + + return ok; +} + +FLAC__bool do_shorthand_operations(const CommandLineOptions *options) +{ + unsigned i; + FLAC__bool ok = true; + + /* to die after first error, v--- add '&& ok' here */ + for(i = 0; i < options->num_files; i++) + ok &= do_shorthand_operations_on_file(options->filenames[i], options); + + /* check if OP__ADD_REPLAY_GAIN requested */ + if(ok && options->num_files > 0) { + for(i = 0; i < options->ops.num_operations; i++) { + if(options->ops.operations[i].type == OP__ADD_REPLAY_GAIN) + ok = do_shorthand_operation__add_replay_gain(options->filenames, options->num_files, options->preserve_modtime, false); + else if(options->ops.operations[i].type == OP__SCAN_REPLAY_GAIN) + ok = do_shorthand_operation__add_replay_gain(options->filenames, options->num_files, options->preserve_modtime, true); + } + } + + return ok; +} + +FLAC__bool do_shorthand_operations_on_file(const char *filename, const CommandLineOptions *options) +{ + unsigned i; + FLAC__bool ok = true, needs_write = false, use_padding = options->use_padding; + FLAC__Metadata_Chain *chain = FLAC__metadata_chain_new(); + + if(0 == chain) + die("out of memory allocating chain"); + + if(!FLAC__metadata_chain_read(chain, filename)) { + print_error_with_chain_status(chain, "%s: ERROR: reading metadata", filename); + ok = false; + goto cleanup; + } + + for(i = 0; i < options->ops.num_operations && ok; i++) { + /* + * Do OP__ADD_SEEKPOINT last to avoid decoding twice if both + * --add-seekpoint and --import-cuesheet-from are used. + */ + if(options->ops.operations[i].type != OP__ADD_SEEKPOINT) + ok &= do_shorthand_operation(filename, options->prefix_with_filename, chain, &options->ops.operations[i], &needs_write, options->utf8_convert); + + /* The following seems counterintuitive but the meaning + * of 'use_padding' is 'try to keep the overall metadata + * to its original size, adding or truncating extra + * padding if necessary' which is why we need to turn it + * off in this case. If we don't, the extra padding block + * will just be truncated. + */ + if(options->ops.operations[i].type == OP__ADD_PADDING) + use_padding = false; + } + + /* + * Do OP__ADD_SEEKPOINT last to avoid decoding twice if both + * --add-seekpoint and --import-cuesheet-from are used. + */ + for(i = 0; i < options->ops.num_operations && ok; i++) { + if(options->ops.operations[i].type == OP__ADD_SEEKPOINT) + ok &= do_shorthand_operation(filename, options->prefix_with_filename, chain, &options->ops.operations[i], &needs_write, options->utf8_convert); + } + + if(ok && needs_write) { + if(use_padding) + FLAC__metadata_chain_sort_padding(chain); + ok = FLAC__metadata_chain_write(chain, use_padding, options->preserve_modtime); + if(!ok) + print_error_with_chain_status(chain, "%s: ERROR: writing FLAC file", filename); + } + + cleanup : + FLAC__metadata_chain_delete(chain); + + return ok; +} + +FLAC__bool do_shorthand_operation(const char *filename, FLAC__bool prefix_with_filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write, FLAC__bool utf8_convert) +{ + FLAC__bool ok = true; + + switch(operation->type) { + case OP__SHOW_MD5SUM: + case OP__SHOW_MIN_BLOCKSIZE: + case OP__SHOW_MAX_BLOCKSIZE: + case OP__SHOW_MIN_FRAMESIZE: + case OP__SHOW_MAX_FRAMESIZE: + case OP__SHOW_SAMPLE_RATE: + case OP__SHOW_CHANNELS: + case OP__SHOW_BPS: + case OP__SHOW_TOTAL_SAMPLES: + case OP__SET_MD5SUM: + case OP__SET_MIN_BLOCKSIZE: + case OP__SET_MAX_BLOCKSIZE: + case OP__SET_MIN_FRAMESIZE: + case OP__SET_MAX_FRAMESIZE: + case OP__SET_SAMPLE_RATE: + case OP__SET_CHANNELS: + case OP__SET_BPS: + case OP__SET_TOTAL_SAMPLES: + ok = do_shorthand_operation__streaminfo(filename, prefix_with_filename, chain, operation, needs_write); + break; + case OP__SHOW_VC_VENDOR: + case OP__SHOW_VC_FIELD: + case OP__REMOVE_VC_ALL: + case OP__REMOVE_VC_ALL_EXCEPT: + case OP__REMOVE_VC_FIELD: + case OP__REMOVE_VC_FIRSTFIELD: + case OP__SET_VC_FIELD: + case OP__IMPORT_VC_FROM: + case OP__EXPORT_VC_TO: + ok = do_shorthand_operation__vorbis_comment(filename, prefix_with_filename, chain, operation, needs_write, !utf8_convert); + break; + case OP__IMPORT_CUESHEET_FROM: + case OP__EXPORT_CUESHEET_TO: + ok = do_shorthand_operation__cuesheet(filename, chain, operation, needs_write); + break; + case OP__IMPORT_PICTURE_FROM: + case OP__EXPORT_PICTURE_TO: + ok = do_shorthand_operation__picture(filename, chain, operation, needs_write); + break; + case OP__ADD_SEEKPOINT: + ok = do_shorthand_operation__add_seekpoints(filename, chain, operation->argument.add_seekpoint.specification, needs_write); + break; + case OP__ADD_REPLAY_GAIN: + case OP__SCAN_REPLAY_GAIN: + /* these commands are always executed last */ + ok = true; + break; + case OP__ADD_PADDING: + ok = do_shorthand_operation__add_padding(filename, chain, operation->argument.add_padding.length, needs_write); + break; + default: + ok = false; + FLAC__ASSERT(0); + break; + }; + + return ok; +} + +FLAC__bool do_shorthand_operation__add_replay_gain(char **filenames, unsigned num_files, FLAC__bool preserve_modtime, FLAC__bool scan) +{ + FLAC__StreamMetadata streaminfo; + float *title_gains = 0, *title_peaks = 0; + float album_gain, album_peak; + unsigned sample_rate = 0; + unsigned bits_per_sample = 0; + unsigned channels = 0; + unsigned i; + const char *error; + FLAC__bool first = true; + + FLAC__ASSERT(num_files > 0); + + for(i = 0; i < num_files; i++) { + FLAC__ASSERT(0 != filenames[i]); + if(!FLAC__metadata_get_streaminfo(filenames[i], &streaminfo)) { + flac_fprintf(stderr, "%s: ERROR: can't open file or get STREAMINFO block\n", filenames[i]); + return false; + } + if(first) { + first = false; + sample_rate = streaminfo.data.stream_info.sample_rate; + bits_per_sample = streaminfo.data.stream_info.bits_per_sample; + channels = streaminfo.data.stream_info.channels; + } + else { + if(sample_rate != streaminfo.data.stream_info.sample_rate) { + flac_fprintf(stderr, "%s: ERROR: sample rate of %u Hz does not match previous files' %u Hz\n", filenames[i], streaminfo.data.stream_info.sample_rate, sample_rate); + return false; + } + if(bits_per_sample != streaminfo.data.stream_info.bits_per_sample) { + flac_fprintf(stderr, "%s: ERROR: resolution of %u bps does not match previous files' %u bps\n", filenames[i], streaminfo.data.stream_info.bits_per_sample, bits_per_sample); + return false; + } + if(channels != streaminfo.data.stream_info.channels) { + flac_fprintf(stderr, "%s: ERROR: # channels (%u) does not match previous files' (%u)\n", filenames[i], streaminfo.data.stream_info.channels, channels); + return false; + } + } + if(!grabbag__replaygain_is_valid_sample_frequency(sample_rate)) { + flac_fprintf(stderr, "%s: ERROR: sample rate of %u Hz is not supported\n", filenames[i], sample_rate); + return false; + } + if(channels != 1 && channels != 2) { + flac_fprintf(stderr, "%s: ERROR: # of channels (%u) is not supported, must be 1 or 2\n", filenames[i], channels); + return false; + } + if(bits_per_sample < FLAC__MIN_BITS_PER_SAMPLE || bits_per_sample > FLAC__MAX_BITS_PER_SAMPLE) { + flac_fprintf(stderr, "%s: ERROR: resolution (%u) is not supported, must be between %u and %u\n", filenames[i], bits_per_sample, FLAC__MIN_BITS_PER_SAMPLE, FLAC__MAX_BITS_PER_SAMPLE); + return false; + } + } + + if(!grabbag__replaygain_init(sample_rate)) { + FLAC__ASSERT(0); + /* double protection */ + flac_fprintf(stderr, "internal error\n"); + return false; + } + + if( + 0 == (title_gains = safe_malloc_mul_2op_(sizeof(float), /*times*/num_files)) || + 0 == (title_peaks = safe_malloc_mul_2op_(sizeof(float), /*times*/num_files)) + ) + die("out of memory allocating space for title gains/peaks"); + + for(i = 0; i < num_files; i++) { + if(0 != (error = grabbag__replaygain_analyze_file(filenames[i], title_gains+i, title_peaks+i))) { + flac_fprintf(stderr, "%s: ERROR: during analysis (%s)\n", filenames[i], error); + free(title_gains); + free(title_peaks); + return false; + } + } + grabbag__replaygain_get_album(&album_gain, &album_peak); + + for(i = 0; i < num_files; i++) { + if(!scan) { + if(0 != (error = grabbag__replaygain_store_to_file(filenames[i], album_gain, album_peak, title_gains[i], title_peaks[i], preserve_modtime))) { + flac_fprintf(stderr, "%s: ERROR: writing tags (%s)\n", filenames[i], error); + free(title_gains); + free(title_peaks); + return false; + } + } else { + flac_fprintf(stdout, "%s: %f %f %f %f\n", filenames[i], album_gain, album_peak, title_gains[i], title_peaks[i]); + } + } + + free(title_gains); + free(title_peaks); + return true; +} + +FLAC__bool do_shorthand_operation__add_padding(const char *filename, FLAC__Metadata_Chain *chain, unsigned length, FLAC__bool *needs_write) +{ + FLAC__StreamMetadata *padding = 0; + FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new(); + + if(0 == iterator) + die("out of memory allocating iterator"); + + FLAC__metadata_iterator_init(iterator, chain); + + while(FLAC__metadata_iterator_next(iterator)) + ; + + padding = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING); + if(0 == padding) + die("out of memory allocating PADDING block"); + + padding->length = length; + + if(!FLAC__metadata_iterator_insert_block_after(iterator, padding)) { + print_error_with_chain_status(chain, "%s: ERROR: adding new PADDING block to metadata", filename); + FLAC__metadata_object_delete(padding); + FLAC__metadata_iterator_delete(iterator); + return false; + } + + FLAC__metadata_iterator_delete(iterator); + *needs_write = true; + return true; +} + +FLAC__bool passes_filter(const CommandLineOptions *options, const FLAC__StreamMetadata *block, unsigned block_number) +{ + unsigned i, j; + FLAC__bool matches_number = false, matches_type = false; + FLAC__bool has_block_number_arg = false; + + for(i = 0; i < options->args.num_arguments; i++) { + if(options->args.arguments[i].type == ARG__BLOCK_TYPE || options->args.arguments[i].type == ARG__EXCEPT_BLOCK_TYPE) { + for(j = 0; j < options->args.arguments[i].value.block_type.num_entries; j++) { + if(options->args.arguments[i].value.block_type.entries[j].type == block->type) { + if(block->type != FLAC__METADATA_TYPE_APPLICATION || !options->args.arguments[i].value.block_type.entries[j].filter_application_by_id || 0 == memcmp(options->args.arguments[i].value.block_type.entries[j].application_id, block->data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8)) + matches_type = true; + } + } + } + else if(options->args.arguments[i].type == ARG__BLOCK_NUMBER) { + has_block_number_arg = true; + for(j = 0; j < options->args.arguments[i].value.block_number.num_entries; j++) { + if(options->args.arguments[i].value.block_number.entries[j] == block_number) + matches_number = true; + } + } + } + + if(!has_block_number_arg) + matches_number = true; + + if(options->args.checks.has_block_type) { + FLAC__ASSERT(!options->args.checks.has_except_block_type); + } + else if(options->args.checks.has_except_block_type) + matches_type = !matches_type; + else + matches_type = true; + + return matches_number && matches_type; +} + +void write_metadata(const char *filename, FLAC__StreamMetadata *block, unsigned block_number, FLAC__bool raw, FLAC__bool hexdump_application) +{ + unsigned i, j; + +/*@@@ yuck, should do this with a varargs function or something: */ +#define PPR if(filename) { if(raw) printf("%s:",filename); else flac_printf("%s:",filename); } + PPR; printf("METADATA block #%u\n", block_number); + PPR; printf(" type: %u (%s)\n", (unsigned)block->type, block->type < FLAC__METADATA_TYPE_UNDEFINED? FLAC__MetadataTypeString[block->type] : "UNKNOWN"); + PPR; printf(" is last: %s\n", block->is_last? "true":"false"); + PPR; printf(" length: %u\n", block->length); + + switch(block->type) { + case FLAC__METADATA_TYPE_STREAMINFO: + PPR; printf(" minimum blocksize: %u samples\n", block->data.stream_info.min_blocksize); + PPR; printf(" maximum blocksize: %u samples\n", block->data.stream_info.max_blocksize); + PPR; printf(" minimum framesize: %u bytes\n", block->data.stream_info.min_framesize); + PPR; printf(" maximum framesize: %u bytes\n", block->data.stream_info.max_framesize); + PPR; printf(" sample_rate: %u Hz\n", block->data.stream_info.sample_rate); + PPR; printf(" channels: %u\n", block->data.stream_info.channels); + PPR; printf(" bits-per-sample: %u\n", block->data.stream_info.bits_per_sample); + PPR; printf(" total samples: %" PRIu64 "\n", block->data.stream_info.total_samples); + PPR; printf(" MD5 signature: "); + for(i = 0; i < 16; i++) { + printf("%02x", (unsigned)block->data.stream_info.md5sum[i]); + } + printf("\n"); + break; + case FLAC__METADATA_TYPE_PADDING: + /* nothing to print */ + break; + case FLAC__METADATA_TYPE_APPLICATION: + PPR; printf(" application ID: "); + for(i = 0; i < 4; i++) + printf("%02x", block->data.application.id[i]); + printf("\n"); + PPR; printf(" data contents:\n"); + if(0 != block->data.application.data) { + if(hexdump_application) + hexdump(filename, block->data.application.data, block->length - FLAC__STREAM_METADATA_HEADER_LENGTH, " "); + else + (void) local_fwrite(block->data.application.data, 1, block->length - FLAC__STREAM_METADATA_HEADER_LENGTH, stdout); + } + break; + case FLAC__METADATA_TYPE_SEEKTABLE: + PPR; printf(" seek points: %u\n", block->data.seek_table.num_points); + for(i = 0; i < block->data.seek_table.num_points; i++) { + if(block->data.seek_table.points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER) { + PPR; printf(" point %u: sample_number=%" PRIu64 ", stream_offset=%" PRIu64 ", frame_samples=%u\n", i, block->data.seek_table.points[i].sample_number, block->data.seek_table.points[i].stream_offset, block->data.seek_table.points[i].frame_samples); + } + else { + PPR; printf(" point %u: PLACEHOLDER\n", i); + } + } + break; + case FLAC__METADATA_TYPE_VORBIS_COMMENT: + PPR; printf(" vendor string: "); + write_vc_field(0, &block->data.vorbis_comment.vendor_string, raw, stdout); + PPR; printf(" comments: %u\n", block->data.vorbis_comment.num_comments); + for(i = 0; i < block->data.vorbis_comment.num_comments; i++) { + PPR; printf(" comment[%u]: ", i); + write_vc_field(0, &block->data.vorbis_comment.comments[i], raw, stdout); + } + break; + case FLAC__METADATA_TYPE_CUESHEET: + PPR; printf(" media catalog number: %s\n", block->data.cue_sheet.media_catalog_number); + PPR; printf(" lead-in: %" PRIu64 "\n", block->data.cue_sheet.lead_in); + PPR; printf(" is CD: %s\n", block->data.cue_sheet.is_cd? "true":"false"); + PPR; printf(" number of tracks: %u\n", block->data.cue_sheet.num_tracks); + for(i = 0; i < block->data.cue_sheet.num_tracks; i++) { + const FLAC__StreamMetadata_CueSheet_Track *track = block->data.cue_sheet.tracks+i; + const FLAC__bool is_last = (i == block->data.cue_sheet.num_tracks-1); + const FLAC__bool is_leadout = is_last && track->num_indices == 0; + PPR; printf(" track[%u]\n", i); + PPR; printf(" offset: %" PRIu64 "\n", track->offset); + if(is_last) { + PPR; printf(" number: %u (%s)\n", (unsigned)track->number, is_leadout? "LEAD-OUT" : "INVALID"); + } + else { + PPR; printf(" number: %u\n", (unsigned)track->number); + } + if(!is_leadout) { + PPR; printf(" ISRC: %s\n", track->isrc); + PPR; printf(" type: %s\n", track->type == 1? "DATA" : "AUDIO"); + PPR; printf(" pre-emphasis: %s\n", track->pre_emphasis? "true":"false"); + PPR; printf(" number of index points: %u\n", track->num_indices); + for(j = 0; j < track->num_indices; j++) { + const FLAC__StreamMetadata_CueSheet_Index *indx = track->indices+j; + PPR; printf(" index[%u]\n", j); + PPR; printf(" offset: %" PRIu64 "\n", indx->offset); + PPR; printf(" number: %u\n", (unsigned)indx->number); + } + } + } + break; + case FLAC__METADATA_TYPE_PICTURE: + PPR; printf(" type: %u (%s)\n", block->data.picture.type, block->data.picture.type < FLAC__STREAM_METADATA_PICTURE_TYPE_UNDEFINED? FLAC__StreamMetadata_Picture_TypeString[block->data.picture.type] : "UNDEFINED"); + PPR; printf(" MIME type: %s\n", block->data.picture.mime_type); + PPR; printf(" description: %s\n", block->data.picture.description); + PPR; printf(" width: %u\n", (unsigned)block->data.picture.width); + PPR; printf(" height: %u\n", (unsigned)block->data.picture.height); + PPR; printf(" depth: %u\n", (unsigned)block->data.picture.depth); + PPR; printf(" colors: %u%s\n", (unsigned)block->data.picture.colors, block->data.picture.colors? "" : " (unindexed)"); + PPR; printf(" data length: %u\n", (unsigned)block->data.picture.data_length); + PPR; printf(" data:\n"); + if(0 != block->data.picture.data) + hexdump(filename, block->data.picture.data, block->data.picture.data_length, " "); + break; + default: + PPR; printf(" data contents:\n"); + if(0 != block->data.unknown.data) + hexdump(filename, block->data.unknown.data, block->length, " "); + break; + } +#undef PPR +} + +void write_metadata_binary(FLAC__StreamMetadata *block, FLAC__byte *block_raw, FLAC__bool headerless) +{ +#ifdef _WIN32 + fflush(stdout); + _setmode(fileno(stdout),_O_BINARY); +#endif + if(!headerless) + local_fwrite(block_raw, 1, block->length+FLAC__STREAM_METADATA_HEADER_LENGTH, stdout); + else if(block->type == FLAC__METADATA_TYPE_APPLICATION && block->length > 3) + local_fwrite(block_raw+FLAC__STREAM_METADATA_HEADER_LENGTH+4, 1, block->length-4, stdout); + else + local_fwrite(block_raw+FLAC__STREAM_METADATA_HEADER_LENGTH, 1, block->length, stdout); +#ifdef _WIN32 + fflush(stdout); + _setmode(fileno(stdout),_O_TEXT); +#endif +} diff --git a/src/metaflac/operations.h b/src/metaflac/operations.h new file mode 100644 index 0000000..79e1c3b --- /dev/null +++ b/src/metaflac/operations.h @@ -0,0 +1,27 @@ +/* metaflac - Command-line FLAC metadata editor + * Copyright (C) 2001-2009 Josh Coalson + * Copyright (C) 2011-2023 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 + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef metaflac__operations_h +#define metaflac__operations_h + +#include "options.h" + +FLAC__bool do_operations(const CommandLineOptions *options); + +#endif diff --git a/src/metaflac/operations_shorthand.h b/src/metaflac/operations_shorthand.h new file mode 100644 index 0000000..1877c26 --- /dev/null +++ b/src/metaflac/operations_shorthand.h @@ -0,0 +1,26 @@ +/* metaflac - Command-line FLAC metadata editor + * Copyright (C) 2001-2009 Josh Coalson + * Copyright (C) 2011-2023 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 + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "options.h" + +FLAC__bool do_shorthand_operation__picture(const char *filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write); +FLAC__bool do_shorthand_operation__cuesheet(const char *filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write); +FLAC__bool do_shorthand_operation__add_seekpoints(const char *filename, FLAC__Metadata_Chain *chain, const char *specification, FLAC__bool *needs_write); +FLAC__bool do_shorthand_operation__streaminfo(const char *filename, FLAC__bool prefix_with_filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write); +FLAC__bool do_shorthand_operation__vorbis_comment(const char *filename, FLAC__bool prefix_with_filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write, FLAC__bool raw); diff --git a/src/metaflac/operations_shorthand_cuesheet.c b/src/metaflac/operations_shorthand_cuesheet.c new file mode 100644 index 0000000..13c4ded --- /dev/null +++ b/src/metaflac/operations_shorthand_cuesheet.c @@ -0,0 +1,226 @@ +/* metaflac - Command-line FLAC metadata editor + * Copyright (C) 2001-2009 Josh Coalson + * Copyright (C) 2011-2023 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 + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <errno.h> +#include <string.h> +#include "options.h" +#include "utils.h" +#include "FLAC/assert.h" +#include "share/grabbag.h" +#include "share/compat.h" +#include "operations_shorthand.h" + +static FLAC__bool import_cs_from(const char *filename, FLAC__StreamMetadata **cuesheet, const char *cs_filename, FLAC__bool *needs_write, FLAC__uint64 lead_out_offset, unsigned sample_rate, FLAC__bool is_cdda, Argument_AddSeekpoint *add_seekpoint_link); +static FLAC__bool export_cs_to(const char *filename, const FLAC__StreamMetadata *cuesheet, const char *cs_filename); + +FLAC__bool do_shorthand_operation__cuesheet(const char *filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write) +{ + FLAC__bool ok = true; + FLAC__StreamMetadata *cuesheet = 0; + FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new(); + FLAC__uint64 lead_out_offset = 0; + FLAC__bool is_cdda = false; + unsigned sample_rate = 0; + + if(0 == iterator) + die("out of memory allocating iterator"); + + FLAC__metadata_iterator_init(iterator, chain); + + do { + FLAC__StreamMetadata *block = FLAC__metadata_iterator_get_block(iterator); + if(block->type == FLAC__METADATA_TYPE_STREAMINFO) { + lead_out_offset = block->data.stream_info.total_samples; + if(lead_out_offset == 0) { + flac_fprintf(stderr, "%s: ERROR: FLAC file must have total_samples set in STREAMINFO in order to import/export cuesheet\n", filename); + FLAC__metadata_iterator_delete(iterator); + return false; + } + sample_rate = block->data.stream_info.sample_rate; + is_cdda = (block->data.stream_info.channels == 1 || block->data.stream_info.channels == 2) && (block->data.stream_info.bits_per_sample == 16) && (sample_rate == 44100); + } + else if(block->type == FLAC__METADATA_TYPE_CUESHEET) + cuesheet = block; + } while(FLAC__metadata_iterator_next(iterator)); + + if(lead_out_offset == 0) { + flac_fprintf(stderr, "%s: ERROR: FLAC stream has no STREAMINFO block\n", filename); + FLAC__metadata_iterator_delete(iterator); + return false; + } + + if(sample_rate == 0) { + flac_fprintf(stderr, "%s: ERROR: cannot parse cuesheet when sample rate is unknown\n", filename); + FLAC__metadata_iterator_delete(iterator); + return false; + } + + switch(operation->type) { + case OP__IMPORT_CUESHEET_FROM: + if(0 != cuesheet) { + flac_fprintf(stderr, "%s: ERROR: FLAC file already has CUESHEET block\n", filename); + ok = false; + } + else { + ok = import_cs_from(filename, &cuesheet, operation->argument.import_cuesheet_from.filename, needs_write, lead_out_offset, sample_rate, is_cdda, operation->argument.import_cuesheet_from.add_seekpoint_link); + if(ok) { + /* append CUESHEET block */ + while(FLAC__metadata_iterator_next(iterator)) + ; + if(!FLAC__metadata_iterator_insert_block_after(iterator, cuesheet)) { + print_error_with_chain_status(chain, "%s: ERROR: adding new CUESHEET block to metadata", filename); + FLAC__metadata_object_delete(cuesheet); + ok = false; + } + } + } + break; + case OP__EXPORT_CUESHEET_TO: + if(0 == cuesheet) { + flac_fprintf(stderr, "%s: ERROR: FLAC file has no CUESHEET block\n", filename); + ok = false; + } + else + ok = export_cs_to(filename, cuesheet, operation->argument.filename.value); + break; + default: + ok = false; + FLAC__ASSERT(0); + break; + }; + + FLAC__metadata_iterator_delete(iterator); + return ok; +} + +/* + * local routines + */ + +FLAC__bool import_cs_from(const char *filename, FLAC__StreamMetadata **cuesheet, const char *cs_filename, FLAC__bool *needs_write, FLAC__uint64 lead_out_offset, unsigned sample_rate, FLAC__bool is_cdda, Argument_AddSeekpoint *add_seekpoint_link) +{ + FILE *f; + const char *error_message; + char **seekpoint_specification = add_seekpoint_link? &(add_seekpoint_link->specification) : 0; + unsigned last_line_read; + + if(0 == cs_filename || strlen(cs_filename) == 0) { + flac_fprintf(stderr, "%s: ERROR: empty import file name\n", filename); + return false; + } + if(0 == strcmp(cs_filename, "-")) + f = stdin; + else + f = flac_fopen(cs_filename, "r"); + + if(0 == f) { + flac_fprintf(stderr, "%s: ERROR: can't open import file %s: %s\n", filename, cs_filename, strerror(errno)); + return false; + } + + *cuesheet = grabbag__cuesheet_parse(f, &error_message, &last_line_read, sample_rate, is_cdda, lead_out_offset); + + if(f != stdin) + fclose(f); + + if(0 == *cuesheet) { + flac_fprintf(stderr, "%s: ERROR: while parsing cuesheet \"%s\" on line %u: %s\n", filename, cs_filename, last_line_read, error_message); + return false; + } + + if(!FLAC__format_cuesheet_is_legal(&(*cuesheet)->data.cue_sheet, /*check_cd_da_subset=*/false, &error_message)) { + flac_fprintf(stderr, "%s: ERROR parsing cuesheet \"%s\": %s\n", filename, cs_filename, error_message); + FLAC__metadata_object_delete(*cuesheet); + return false; + } + + /* if we're expecting CDDA, warn about non-compliance */ + if(is_cdda && !FLAC__format_cuesheet_is_legal(&(*cuesheet)->data.cue_sheet, /*check_cd_da_subset=*/true, &error_message)) { + flac_fprintf(stderr, "%s: WARNING cuesheet \"%s\" is not audio CD compliant: %s\n", filename, cs_filename, error_message); + (*cuesheet)->data.cue_sheet.is_cd = false; + } + + /* add seekpoints for each index point if required */ + if(0 != seekpoint_specification) { + char spec[128]; + unsigned track, indx; + const FLAC__StreamMetadata_CueSheet *cs = &(*cuesheet)->data.cue_sheet; + if(0 == *seekpoint_specification) + *seekpoint_specification = local_strdup(""); + for(track = 0; track < cs->num_tracks; track++) { + const FLAC__StreamMetadata_CueSheet_Track *tr = cs->tracks+track; + for(indx = 0; indx < tr->num_indices; indx++) { + flac_snprintf(spec, sizeof (spec), "%" PRIu64 ";", (tr->offset + tr->indices[indx].offset)); + local_strcat(seekpoint_specification, spec); + } + } + } + + *needs_write = true; + return true; +} + +FLAC__bool export_cs_to(const char *filename, const FLAC__StreamMetadata *cuesheet, const char *cs_filename) +{ + FILE *f; + char *ref = 0; + size_t reflen; + + if(0 == cs_filename || strlen(cs_filename) == 0) { + flac_fprintf(stderr, "%s: ERROR: empty export file name\n", filename); + return false; + } + if(0 == strcmp(cs_filename, "-")) + f = stdout; + else + f = flac_fopen(cs_filename, "w"); + + if(0 == f) { + flac_fprintf(stderr, "%s: ERROR: can't open export file %s: %s\n", filename, cs_filename, strerror(errno)); + return false; + } + + reflen = strlen(filename) + 7 + 1; + if(0 == (ref = malloc(reflen))) { + flac_fprintf(stderr, "%s: ERROR: allocating memory\n", filename); + if(f != stdout) + fclose(f); + return false; + } + + flac_snprintf(ref, reflen, "\"%s\" FLAC", filename); + + grabbag__cuesheet_emit(f, cuesheet, ref); + + free(ref); + + if(f != stdout) + fclose(f); +#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION + /* Delete output file when fuzzing */ + if(f != stdout) + flac_unlink(cs_filename); +#endif + + return true; +} diff --git a/src/metaflac/operations_shorthand_picture.c b/src/metaflac/operations_shorthand_picture.c new file mode 100644 index 0000000..6bb459b --- /dev/null +++ b/src/metaflac/operations_shorthand_picture.c @@ -0,0 +1,184 @@ +/* metaflac - Command-line FLAC metadata editor + * Copyright (C) 2001-2009 Josh Coalson + * Copyright (C) 2011-2023 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 + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <errno.h> +#include <string.h> +#include "options.h" +#include "utils.h" +#include "FLAC/assert.h" +#include "share/grabbag.h" /* for grabbag__picture_parse_specification() etc */ + +#include "operations_shorthand.h" + +static FLAC__bool import_pic_from(const char *filename, FLAC__StreamMetadata **picture, const char *specification, FLAC__bool *needs_write); +static FLAC__bool export_pic_to(const char *filename, const FLAC__StreamMetadata *picture, const char *pic_filename); + +FLAC__bool do_shorthand_operation__picture(const char *filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write) +{ + FLAC__bool ok = true, has_type1 = false, has_type2 = false; + FLAC__StreamMetadata *picture = 0; + FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new(); + + if(0 == iterator) + die("out of memory allocating iterator"); + + FLAC__metadata_iterator_init(iterator, chain); + + switch(operation->type) { + case OP__IMPORT_PICTURE_FROM: + ok = import_pic_from(filename, &picture, operation->argument.specification.value, needs_write); + if(ok) { + /* append PICTURE block */ + while(FLAC__metadata_iterator_next(iterator)) + ; + if(!FLAC__metadata_iterator_insert_block_after(iterator, picture)) { + print_error_with_chain_status(chain, "%s: ERROR: adding new PICTURE block to metadata", filename); + FLAC__metadata_object_delete(picture); + ok = false; + } + } + if(ok) { + /* check global PICTURE constraints (max 1 block each of type=1 and type=2) */ + while(FLAC__metadata_iterator_prev(iterator)) + ; + do { + FLAC__StreamMetadata *block = FLAC__metadata_iterator_get_block(iterator); + if(block->type == FLAC__METADATA_TYPE_PICTURE) { + if(block->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD) { + if(has_type1) { + print_error_with_chain_status(chain, "%s: ERROR: FLAC stream can only have one 32x32 standard icon (type=1) PICTURE block", filename); + ok = false; + } + has_type1 = true; + } + else if(block->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON) { + if(has_type2) { + print_error_with_chain_status(chain, "%s: ERROR: FLAC stream can only have one icon (type=2) PICTURE block", filename); + ok = false; + } + has_type2 = true; + } + } + } while(FLAC__metadata_iterator_next(iterator)); + } + break; + case OP__EXPORT_PICTURE_TO: + { + const Argument_BlockNumber *a = operation->argument.export_picture_to.block_number_link; + int block_number = (a && a->num_entries > 0)? (int)a->entries[0] : -1; + unsigned i = 0; + do { + FLAC__StreamMetadata *block = FLAC__metadata_iterator_get_block(iterator); + if(block->type == FLAC__METADATA_TYPE_PICTURE && (block_number < 0 || i == (unsigned)block_number)) + picture = block; + i++; + } while(FLAC__metadata_iterator_next(iterator) && 0 == picture); + if(0 == picture) { + if(block_number < 0) + flac_fprintf(stderr, "%s: ERROR: FLAC file has no PICTURE block\n", filename); + else + flac_fprintf(stderr, "%s: ERROR: FLAC file has no PICTURE block at block #%d\n", filename, block_number); + ok = false; + } + else + ok = export_pic_to(filename, picture, operation->argument.filename.value); + } + break; + default: + ok = false; + FLAC__ASSERT(0); + break; + }; + + FLAC__metadata_iterator_delete(iterator); + return ok; +} + +/* + * local routines + */ + +FLAC__bool import_pic_from(const char *filename, FLAC__StreamMetadata **picture, const char *specification, FLAC__bool *needs_write) +{ + const char *error_message; + + if(0 == specification || strlen(specification) == 0) { + flac_fprintf(stderr, "%s: ERROR: empty picture specification\n", filename); + return false; + } + + *picture = grabbag__picture_parse_specification(specification, &error_message); + + if(0 == *picture) { + flac_fprintf(stderr, "%s: ERROR: while parsing picture specification \"%s\": %s\n", filename, specification, error_message); + return false; + } + + if(!FLAC__format_picture_is_legal(&(*picture)->data.picture, &error_message)) { + flac_fprintf(stderr, "%s: ERROR: new PICTURE block for \"%s\" is illegal: %s\n", filename, specification, error_message); + FLAC__metadata_object_delete(*picture); + *picture = 0; + return false; + } + + *needs_write = true; + return true; +} + +FLAC__bool export_pic_to(const char *filename, const FLAC__StreamMetadata *picture, const char *pic_filename) +{ + FILE *f; + const FLAC__uint32 len = picture->data.picture.data_length; + + if(0 == pic_filename || strlen(pic_filename) == 0) { + flac_fprintf(stderr, "%s: ERROR: empty export file name\n", filename); + return false; + } + if(0 == strcmp(pic_filename, "-")) + f = grabbag__file_get_binary_stdout(); + else + f = flac_fopen(pic_filename, "wb"); + + if(0 == f) { + flac_fprintf(stderr, "%s: ERROR: can't open export file %s: %s\n", filename, pic_filename, strerror(errno)); + return false; + } + + if(fwrite(picture->data.picture.data, 1, len, f) != len) { + flac_fprintf(stderr, "%s: ERROR: writing PICTURE data to file\n", filename); + if(f != stdout) + fclose(f); + return false; + } + + if(f != stdout) + fclose(f); + +#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION + /* Delete output file when fuzzing */ + if(f != stdout) + flac_unlink(pic_filename); +#endif + + return true; +} diff --git a/src/metaflac/operations_shorthand_seektable.c b/src/metaflac/operations_shorthand_seektable.c new file mode 100644 index 0000000..c9175b3 --- /dev/null +++ b/src/metaflac/operations_shorthand_seektable.c @@ -0,0 +1,220 @@ +/* metaflac - Command-line FLAC metadata editor + * Copyright (C) 2001-2009 Josh Coalson + * Copyright (C) 2011-2023 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 + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include "utils.h" +#include "FLAC/assert.h" +#include "FLAC/stream_decoder.h" +#include "FLAC/metadata.h" +#include "share/grabbag.h" +#include "operations_shorthand.h" + +static FLAC__bool populate_seekpoint_values(const char *filename, FLAC__StreamMetadata *block, FLAC__bool *needs_write); + +FLAC__bool do_shorthand_operation__add_seekpoints(const char *filename, FLAC__Metadata_Chain *chain, const char *specification, FLAC__bool *needs_write) +{ + FLAC__bool ok = true, found_seektable_block = false; + FLAC__StreamMetadata *block = 0; + FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new(); + FLAC__uint64 total_samples = 0; + unsigned sample_rate = 0; + + if(0 == iterator) + die("out of memory allocating iterator"); + + FLAC__metadata_iterator_init(iterator, chain); + + do { + block = FLAC__metadata_iterator_get_block(iterator); + if(block->type == FLAC__METADATA_TYPE_STREAMINFO) { + sample_rate = block->data.stream_info.sample_rate; + total_samples = block->data.stream_info.total_samples; + } + else if(block->type == FLAC__METADATA_TYPE_SEEKTABLE) + found_seektable_block = true; + } while(!found_seektable_block && FLAC__metadata_iterator_next(iterator)); + + if(total_samples == 0) { + flac_fprintf(stderr, "%s: ERROR: cannot add seekpoints because STREAMINFO block does not specify total_samples\n", filename); + FLAC__metadata_iterator_delete(iterator); + return false; + } + + if(!found_seektable_block) { + /* create a new block */ + block = FLAC__metadata_object_new(FLAC__METADATA_TYPE_SEEKTABLE); + if(0 == block) + die("out of memory allocating SEEKTABLE block"); + while(FLAC__metadata_iterator_prev(iterator)) + ; + if(!FLAC__metadata_iterator_insert_block_after(iterator, block)) { + print_error_with_chain_status(chain, "%s: ERROR: adding new SEEKTABLE block to metadata", filename); + FLAC__metadata_object_delete(block); + FLAC__metadata_iterator_delete(iterator); + return false; + } + /* iterator is left pointing to new block */ + FLAC__ASSERT(FLAC__metadata_iterator_get_block(iterator) == block); + } + + FLAC__metadata_iterator_delete(iterator); + + FLAC__ASSERT(0 != block); + FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_SEEKTABLE); + + if(!grabbag__seektable_convert_specification_to_template(specification, /*only_explicit_placeholders=*/false, total_samples, sample_rate, block, /*spec_has_real_points=*/0)) { + flac_fprintf(stderr, "%s: ERROR (internal) preparing seektable with seekpoints\n", filename); + return false; + } + + ok = populate_seekpoint_values(filename, block, needs_write); + + if(ok) + (void) FLAC__format_seektable_sort(&block->data.seek_table); + + return ok; +} + +/* + * local routines + */ + +typedef struct { + FLAC__StreamMetadata_SeekTable *seektable_template; + FLAC__uint64 samples_written; + FLAC__uint64 audio_offset, last_offset; + unsigned first_seekpoint_to_check; + FLAC__bool error_occurred; + FLAC__StreamDecoderErrorStatus error_status; +} ClientData; + +static FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data) +{ + ClientData *cd = (ClientData*)client_data; + + (void)buffer; + FLAC__ASSERT(0 != cd); + + if(!cd->error_occurred) { + const unsigned blocksize = frame->header.blocksize; + const FLAC__uint64 frame_first_sample = cd->samples_written; + const FLAC__uint64 frame_last_sample = frame_first_sample + (FLAC__uint64)blocksize - 1; + FLAC__uint64 test_sample; + unsigned i; + for(i = cd->first_seekpoint_to_check; i < cd->seektable_template->num_points; i++) { + test_sample = cd->seektable_template->points[i].sample_number; + if(test_sample > frame_last_sample) { + break; + } + else if(test_sample >= frame_first_sample) { + cd->seektable_template->points[i].sample_number = frame_first_sample; + cd->seektable_template->points[i].stream_offset = cd->last_offset - cd->audio_offset; + cd->seektable_template->points[i].frame_samples = blocksize; + cd->first_seekpoint_to_check++; + /* DO NOT: "break;" and here's why: + * The seektable template may contain more than one target + * sample for any given frame; we will keep looping, generating + * duplicate seekpoints for them, and we'll clean it up later, + * just before writing the seektable back to the metadata. + */ + } + else { + cd->first_seekpoint_to_check++; + } + } + cd->samples_written += blocksize; + if(!FLAC__stream_decoder_get_decode_position(decoder, &cd->last_offset)) + return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT; + return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE; + } + else + return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT; +} + +static void error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data) +{ + ClientData *cd = (ClientData*)client_data; + + (void)decoder; + FLAC__ASSERT(0 != cd); + + if(!cd->error_occurred) { /* don't let multiple errors overwrite the first one */ + cd->error_occurred = true; + cd->error_status = status; + } +} + +FLAC__bool populate_seekpoint_values(const char *filename, FLAC__StreamMetadata *block, FLAC__bool *needs_write) +{ + FLAC__StreamDecoder *decoder; + ClientData client_data; + FLAC__bool ok = true; + + FLAC__ASSERT(0 != block); + FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_SEEKTABLE); + + client_data.seektable_template = &block->data.seek_table; + client_data.samples_written = 0; + /* client_data.audio_offset must be determined later */ + client_data.first_seekpoint_to_check = 0; + client_data.error_occurred = false; + + decoder = FLAC__stream_decoder_new(); + + if(0 == decoder) { + flac_fprintf(stderr, "%s: ERROR (--add-seekpoint) creating the decoder instance\n", filename); + return false; + } + + FLAC__stream_decoder_set_md5_checking(decoder, false); + FLAC__stream_decoder_set_metadata_ignore_all(decoder); + + if(FLAC__stream_decoder_init_file(decoder, filename, write_callback_, /*metadata_callback=*/0, error_callback_, &client_data) != FLAC__STREAM_DECODER_INIT_STATUS_OK) { + flac_fprintf(stderr, "%s: ERROR (--add-seekpoint) initializing the decoder instance (%s)\n", filename, FLAC__stream_decoder_get_resolved_state_string(decoder)); + ok = false; + } + + if(ok && !FLAC__stream_decoder_process_until_end_of_metadata(decoder)) { + flac_fprintf(stderr, "%s: ERROR (--add-seekpoint) decoding file (%s)\n", filename, FLAC__stream_decoder_get_resolved_state_string(decoder)); + ok = false; + } + + if(ok && !FLAC__stream_decoder_get_decode_position(decoder, &client_data.audio_offset)) { + flac_fprintf(stderr, "%s: ERROR (--add-seekpoint) decoding file\n", filename); + ok = false; + } + client_data.last_offset = client_data.audio_offset; + + if(ok && !FLAC__stream_decoder_process_until_end_of_stream(decoder)) { + flac_fprintf(stderr, "%s: ERROR (--add-seekpoint) decoding file (%s)\n", filename, FLAC__stream_decoder_get_resolved_state_string(decoder)); + ok = false; + } + + if(ok && client_data.error_occurred) { + flac_fprintf(stderr, "%s: ERROR (--add-seekpoint) decoding file (%u:%s)\n", filename, (unsigned)client_data.error_status, FLAC__StreamDecoderErrorStatusString[client_data.error_status]); + ok = false; + } + + *needs_write = true; + FLAC__stream_decoder_delete(decoder); + return ok; +} diff --git a/src/metaflac/operations_shorthand_streaminfo.c b/src/metaflac/operations_shorthand_streaminfo.c new file mode 100644 index 0000000..3219841 --- /dev/null +++ b/src/metaflac/operations_shorthand_streaminfo.c @@ -0,0 +1,127 @@ +/* metaflac - Command-line FLAC metadata editor + * Copyright (C) 2001-2009 Josh Coalson + * Copyright (C) 2011-2023 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 + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include "options.h" +#include "utils.h" +#include "FLAC/assert.h" +#include "FLAC/metadata.h" +#include "share/compat.h" +#include <string.h> +#include "operations_shorthand.h" + +FLAC__bool do_shorthand_operation__streaminfo(const char *filename, FLAC__bool prefix_with_filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write) +{ + unsigned i; + FLAC__bool ok = true; + FLAC__StreamMetadata *block; + FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new(); + + if(0 == iterator) + die("out of memory allocating iterator"); + + FLAC__metadata_iterator_init(iterator, chain); + + block = FLAC__metadata_iterator_get_block(iterator); + + FLAC__ASSERT(0 != block); + FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_STREAMINFO); + + if(prefix_with_filename) + flac_printf("%s:", filename); + + switch(operation->type) { + case OP__SHOW_MD5SUM: + for(i = 0; i < 16; i++) + printf("%02x", block->data.stream_info.md5sum[i]); + printf("\n"); + break; + case OP__SHOW_MIN_BLOCKSIZE: + printf("%u\n", block->data.stream_info.min_blocksize); + break; + case OP__SHOW_MAX_BLOCKSIZE: + printf("%u\n", block->data.stream_info.max_blocksize); + break; + case OP__SHOW_MIN_FRAMESIZE: + printf("%u\n", block->data.stream_info.min_framesize); + break; + case OP__SHOW_MAX_FRAMESIZE: + printf("%u\n", block->data.stream_info.max_framesize); + break; + case OP__SHOW_SAMPLE_RATE: + printf("%u\n", block->data.stream_info.sample_rate); + break; + case OP__SHOW_CHANNELS: + printf("%u\n", block->data.stream_info.channels); + break; + case OP__SHOW_BPS: + printf("%u\n", block->data.stream_info.bits_per_sample); + break; + case OP__SHOW_TOTAL_SAMPLES: + printf("%" PRIu64 "\n", block->data.stream_info.total_samples); + break; + case OP__SET_MD5SUM: + memcpy(block->data.stream_info.md5sum, operation->argument.streaminfo_md5.value, 16); + *needs_write = true; + break; + case OP__SET_MIN_BLOCKSIZE: + block->data.stream_info.min_blocksize = operation->argument.streaminfo_uint32.value; + *needs_write = true; + break; + case OP__SET_MAX_BLOCKSIZE: + block->data.stream_info.max_blocksize = operation->argument.streaminfo_uint32.value; + *needs_write = true; + break; + case OP__SET_MIN_FRAMESIZE: + block->data.stream_info.min_framesize = operation->argument.streaminfo_uint32.value; + *needs_write = true; + break; + case OP__SET_MAX_FRAMESIZE: + block->data.stream_info.max_framesize = operation->argument.streaminfo_uint32.value; + *needs_write = true; + break; + case OP__SET_SAMPLE_RATE: + block->data.stream_info.sample_rate = operation->argument.streaminfo_uint32.value; + *needs_write = true; + break; + case OP__SET_CHANNELS: + block->data.stream_info.channels = operation->argument.streaminfo_uint32.value; + *needs_write = true; + break; + case OP__SET_BPS: + block->data.stream_info.bits_per_sample = operation->argument.streaminfo_uint32.value; + *needs_write = true; + break; + case OP__SET_TOTAL_SAMPLES: + block->data.stream_info.total_samples = operation->argument.streaminfo_uint64.value; + *needs_write = true; + break; + default: + ok = false; + FLAC__ASSERT(0); + break; + }; + + FLAC__metadata_iterator_delete(iterator); + + return ok; +} diff --git a/src/metaflac/operations_shorthand_vorbiscomment.c b/src/metaflac/operations_shorthand_vorbiscomment.c new file mode 100644 index 0000000..27c9e4c --- /dev/null +++ b/src/metaflac/operations_shorthand_vorbiscomment.c @@ -0,0 +1,430 @@ +/* metaflac - Command-line FLAC metadata editor + * Copyright (C) 2001-2009 Josh Coalson + * Copyright (C) 2011-2023 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 + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include "options.h" +#include "utils.h" +#include "FLAC/assert.h" +#include "share/grabbag.h" /* for grabbag__file_get_filesize() */ +#include "share/utf8.h" +#include <errno.h> +#include <stdlib.h> +#include <string.h> +#include "operations_shorthand.h" +#include "share/compat.h" + +static FLAC__bool remove_vc_all(const char *filename, FLAC__StreamMetadata *block, FLAC__bool *needs_write); +static FLAC__bool remove_vc_all_except(const char *filename, FLAC__StreamMetadata *block, const char *field_name, FLAC__bool *needs_write); +static FLAC__bool remove_vc_field(const char *filename, FLAC__StreamMetadata *block, const char *field_name, FLAC__bool *needs_write); +static FLAC__bool remove_vc_firstfield(const char *filename, FLAC__StreamMetadata *block, const char *field_name, FLAC__bool *needs_write); +static FLAC__bool set_vc_field(const char *filename, FLAC__StreamMetadata *block, const Argument_VcField *field, FLAC__bool *needs_write, FLAC__bool raw); +static FLAC__bool import_vc_from(const char *filename, FLAC__StreamMetadata *block, const Argument_String *vc_filename, FLAC__bool *needs_write, FLAC__bool raw); +static FLAC__bool export_vc_to(const char *filename, FLAC__StreamMetadata *block, const Argument_String *vc_filename, FLAC__bool raw); + +FLAC__bool do_shorthand_operation__vorbis_comment(const char *filename, FLAC__bool prefix_with_filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write, FLAC__bool raw) +{ + FLAC__bool ok = true, found_vc_block = false; + FLAC__StreamMetadata *block = 0; + FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new(); + + if(0 == iterator) + die("out of memory allocating iterator"); + + FLAC__metadata_iterator_init(iterator, chain); + + do { + block = FLAC__metadata_iterator_get_block(iterator); + if(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) + found_vc_block = true; + } while(!found_vc_block && FLAC__metadata_iterator_next(iterator)); + + if(!found_vc_block) { + /* create a new block if necessary */ + if(operation->type == OP__SET_VC_FIELD || operation->type == OP__IMPORT_VC_FROM) { + block = FLAC__metadata_object_new(FLAC__METADATA_TYPE_VORBIS_COMMENT); + if(0 == block) + die("out of memory allocating VORBIS_COMMENT block"); + while(FLAC__metadata_iterator_next(iterator)) + ; + if(!FLAC__metadata_iterator_insert_block_after(iterator, block)) { + print_error_with_chain_status(chain, "%s: ERROR: adding new VORBIS_COMMENT block to metadata", filename); + return false; + } + /* iterator is left pointing to new block */ + FLAC__ASSERT(FLAC__metadata_iterator_get_block(iterator) == block); + } + else { + FLAC__metadata_iterator_delete(iterator); + return ok; + } + } + + FLAC__ASSERT(0 != block); + FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT); + + switch(operation->type) { + case OP__SHOW_VC_VENDOR: + write_vc_field(prefix_with_filename? filename : 0, &block->data.vorbis_comment.vendor_string, raw, stdout); + break; + case OP__SHOW_VC_FIELD: + write_vc_fields(prefix_with_filename? filename : 0, operation->argument.vc_field_name.value, block->data.vorbis_comment.comments, block->data.vorbis_comment.num_comments, raw, stdout); + break; + case OP__REMOVE_VC_ALL: + ok = remove_vc_all(filename, block, needs_write); + break; + case OP__REMOVE_VC_ALL_EXCEPT: + ok = remove_vc_all_except(filename, block, operation->argument.vc_field_name.value, needs_write); + break; + case OP__REMOVE_VC_FIELD: + ok = remove_vc_field(filename, block, operation->argument.vc_field_name.value, needs_write); + break; + case OP__REMOVE_VC_FIRSTFIELD: + ok = remove_vc_firstfield(filename, block, operation->argument.vc_field_name.value, needs_write); + break; + case OP__SET_VC_FIELD: +#ifdef _WIN32 /* do not convert anything or things will break */ + ok = set_vc_field(filename, block, &operation->argument.vc_field, needs_write, true); +#else + ok = set_vc_field(filename, block, &operation->argument.vc_field, needs_write, raw); +#endif + break; + case OP__IMPORT_VC_FROM: + ok = import_vc_from(filename, block, &operation->argument.filename, needs_write, raw); + break; + case OP__EXPORT_VC_TO: + ok = export_vc_to(filename, block, &operation->argument.filename, raw); + break; + default: + ok = false; + FLAC__ASSERT(0); + break; + }; + + FLAC__metadata_iterator_delete(iterator); + return ok; +} + +/* + * local routines + */ + +FLAC__bool remove_vc_all(const char *filename, FLAC__StreamMetadata *block, FLAC__bool *needs_write) +{ + FLAC__ASSERT(0 != block); + FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT); + FLAC__ASSERT(0 != needs_write); + + if(0 != block->data.vorbis_comment.comments) { + FLAC__ASSERT(block->data.vorbis_comment.num_comments > 0); + if(!FLAC__metadata_object_vorbiscomment_resize_comments(block, 0)) { + flac_fprintf(stderr, "%s: ERROR: memory allocation failure\n", filename); + return false; + } + *needs_write = true; + } + else { + FLAC__ASSERT(block->data.vorbis_comment.num_comments == 0); + } + + return true; +} + +FLAC__bool remove_vc_all_except(const char *filename, FLAC__StreamMetadata *block, const char *field_name, FLAC__bool *needs_write) +{ + char * field_names[200]; + uint32_t field_name_length, i; + int j, num_field_names; + + FLAC__ASSERT(0 != block); + FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT); + FLAC__ASSERT(0 != needs_write); + + field_name_length = strlen(field_name); + field_names[0] = (void *)field_name; + + for(num_field_names = 1; num_field_names < 200; num_field_names++) { + char * separator = strchr(field_names[num_field_names-1], '='); + if(separator == 0 || separator >= field_name + field_name_length) + break; + field_names[num_field_names] = separator+1; + } + + if(num_field_names > 200) { + flac_fprintf(stderr, "%s: ERROR: too many field names\n", filename); + return false; + } + + for(i = 0; i < block->data.vorbis_comment.num_comments; ) { + int field_name_found = false; + for(j = 0; j < num_field_names; j++) { + const uint32_t length = (j == (num_field_names - 1))?(uint32_t)strlen(field_names[j]):(uint32_t)(strchr(field_names[j],'=')-field_names[j]); + if(FLAC__metadata_object_vorbiscomment_entry_matches(block->data.vorbis_comment.comments[i], field_names[j], length)) { + field_name_found = true; + break; + } + } + if(!field_name_found) { + FLAC__metadata_object_vorbiscomment_delete_comment(block, i); + *needs_write = true; + } + else + i++; + } + + return true; +} + +FLAC__bool remove_vc_field(const char *filename, FLAC__StreamMetadata *block, const char *field_name, FLAC__bool *needs_write) +{ + int n; + + FLAC__ASSERT(0 != needs_write); + + n = FLAC__metadata_object_vorbiscomment_remove_entries_matching(block, field_name); + + if(n < 0) { + flac_fprintf(stderr, "%s: ERROR: memory allocation failure\n", filename); + return false; + } + else if(n > 0) + *needs_write = true; + + return true; +} + +FLAC__bool remove_vc_firstfield(const char *filename, FLAC__StreamMetadata *block, const char *field_name, FLAC__bool *needs_write) +{ + int n; + + FLAC__ASSERT(0 != needs_write); + + n = FLAC__metadata_object_vorbiscomment_remove_entry_matching(block, field_name); + + if(n < 0) { + flac_fprintf(stderr, "%s: ERROR: memory allocation failure\n", filename); + return false; + } + else if(n > 0) + *needs_write = true; + + return true; +} + +FLAC__bool set_vc_field(const char *filename, FLAC__StreamMetadata *block, const Argument_VcField *field, FLAC__bool *needs_write, FLAC__bool raw) +{ + FLAC__StreamMetadata_VorbisComment_Entry entry; + char *converted; + + FLAC__ASSERT(0 != block); + FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT); + FLAC__ASSERT(0 != field); + FLAC__ASSERT(0 != needs_write); + + if(field->field_value_from_file) { + /* read the file into 'data' */ + FILE *f = 0; + char *data = 0; + const FLAC__off_t size = grabbag__file_get_filesize(field->field_value); + if(size < 0) { + flac_fprintf(stderr, "%s: ERROR: can't open file '%s' for '%s' tag value\n", filename, field->field_value, field->field_name); + return false; + } + if(size >= 0x100000) { /* magic arbitrary limit, actual format limit is near 16MB */ + flac_fprintf(stderr, "%s: ERROR: file '%s' for '%s' tag value is too large\n", filename, field->field_value, field->field_name); + return false; + } + if(0 == (data = malloc(size+1))) + die("out of memory allocating tag value"); + data[size] = '\0'; + if(0 == (f = flac_fopen(field->field_value, "rb")) || fread(data, 1, size, f) != (size_t)size) { + flac_fprintf(stderr, "%s: ERROR: while reading file '%s' for '%s' tag value: %s\n", filename, field->field_value, field->field_name, strerror(errno)); + free(data); + if(f) + fclose(f); + return false; + } + fclose(f); + if(strlen(data) != (size_t)size) { + free(data); + flac_fprintf(stderr, "%s: ERROR: file '%s' for '%s' tag value has embedded NULs\n", filename, field->field_value, field->field_name); + return false; + } + + /* move 'data' into 'converted', converting to UTF-8 if necessary */ + if(raw) { + converted = data; + } + else if(utf8_encode(data, &converted) >= 0) { + free(data); + } + else { + free(data); + flac_fprintf(stderr, "%s: ERROR: converting file '%s' contents to UTF-8 for tag value\n", filename, field->field_value); + return false; + } + + /* create and entry and append it */ + if(!FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, field->field_name, converted)) { + free(converted); + flac_fprintf(stderr, "%s: ERROR: file '%s' for '%s' tag value is not valid UTF-8\n", filename, field->field_value, field->field_name); + return false; + } + free(converted); + if(!FLAC__metadata_object_vorbiscomment_append_comment(block, entry, /*copy=*/false)) { + flac_fprintf(stderr, "%s: ERROR: memory allocation failure\n", filename); + return false; + } + + *needs_write = true; + return true; + } + else { + FLAC__bool needs_free = false; + entry.entry = (FLAC__byte *)field->field; + if(raw) { + entry.entry = (FLAC__byte *)field->field; + } + else if(utf8_encode(field->field, &converted) >= 0) { + entry.entry = (FLAC__byte *)converted; + needs_free = true; + } + else { + flac_fprintf(stderr, "%s: ERROR: converting comment '%s' to UTF-8\n", filename, field->field); + return false; + } + entry.length = strlen((const char *)entry.entry); + if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length)) { + if(needs_free) + free(converted); + /* + * our previous parsing has already established that the field + * name is OK, so it must be the field value + */ + flac_fprintf(stderr, "%s: ERROR: tag value for '%s' is not valid UTF-8\n", filename, field->field_name); + return false; + } + + if(!FLAC__metadata_object_vorbiscomment_append_comment(block, entry, /*copy=*/true)) { + if(needs_free) + free(converted); + flac_fprintf(stderr, "%s: ERROR: memory allocation failure\n", filename); + return false; + } + + *needs_write = true; + if(needs_free) + free(converted); + return true; + } +} + +FLAC__bool import_vc_from(const char *filename, FLAC__StreamMetadata *block, const Argument_String *vc_filename, FLAC__bool *needs_write, FLAC__bool raw) +{ + FILE *f; + char line[65536]; + FLAC__bool ret; + + if(0 == vc_filename->value || strlen(vc_filename->value) == 0) { + flac_fprintf(stderr, "%s: ERROR: empty import file name\n", filename); + return false; + } + if(0 == strcmp(vc_filename->value, "-")) + f = stdin; + else + f = flac_fopen(vc_filename->value, "r"); + + if(0 == f) { + flac_fprintf(stderr, "%s: ERROR: can't open import file %s: %s\n", filename, vc_filename->value, strerror(errno)); + return false; + } + + ret = true; + while(ret && !feof(f) && fgets(line, sizeof(line), f) != NULL) { + if(!feof(f)) { + char *p = strchr(line, '\n'); + if(0 == p) { + flac_fprintf(stderr, "%s: ERROR: line too long, aborting\n", vc_filename->value); + ret = false; + } + else { + const char *violation; + Argument_VcField field; + *p = '\0'; + memset(&field, 0, sizeof(Argument_VcField)); + field.field_value_from_file = false; + if(!parse_vorbis_comment_field(line, &field.field, &field.field_name, &field.field_value, &field.field_value_length, &violation)) { + FLAC__ASSERT(0 != violation); + flac_fprintf(stderr, "%s: ERROR: malformed vorbis comment field \"%s\",\n %s\n", vc_filename->value, line, violation); + ret = false; + } + else { + ret = set_vc_field(filename, block, &field, needs_write, raw); + } + if(0 != field.field) + free(field.field); + if(0 != field.field_name) + free(field.field_name); + if(0 != field.field_value) + free(field.field_value); + } + } + }; + + if(f != stdin) + fclose(f); + return ret; +} + +FLAC__bool export_vc_to(const char *filename, FLAC__StreamMetadata *block, const Argument_String *vc_filename, FLAC__bool raw) +{ + FILE *f; + FLAC__bool ret; + + if(0 == vc_filename->value || strlen(vc_filename->value) == 0) { + flac_fprintf(stderr, "%s: ERROR: empty export file name\n", filename); + return false; + } + if(0 == strcmp(vc_filename->value, "-")) + f = stdout; + else + f = flac_fopen(vc_filename->value, "w"); + + if(0 == f) { + flac_fprintf(stderr, "%s: ERROR: can't open export file %s: %s\n", filename, vc_filename->value, strerror(errno)); + return false; + } + + ret = true; + + write_vc_fields(0, 0, block->data.vorbis_comment.comments, block->data.vorbis_comment.num_comments, raw, f); + + if(f != stdout) + fclose(f); + +#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION + /* Delete output file when fuzzing */ + if(f != stdout) + flac_unlink(vc_filename->value); +#endif + + return ret; +} diff --git a/src/metaflac/options.c b/src/metaflac/options.c new file mode 100644 index 0000000..1b4b6f6 --- /dev/null +++ b/src/metaflac/options.c @@ -0,0 +1,1146 @@ +/* metaflac - Command-line FLAC metadata editor + * Copyright (C) 2001-2009 Josh Coalson + * Copyright (C) 2011-2023 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 + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifdef 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> +#include <string.h> + +/* + share__getopt format struct; note we don't use short options so we just + set the 'val' field to 0 everywhere to indicate a valid option. +*/ +struct share__option long_options_[] = { + /* global options */ + { "preserve-modtime", 0, 0, 0 }, + { "with-filename", 0, 0, 0 }, + { "no-filename", 0, 0, 0 }, + { "no-utf8-convert", 0, 0, 0 }, + { "dont-use-padding", 0, 0, 0 }, + { "no-cued-seekpoints", 0, 0, 0 }, + /* shorthand operations */ + { "show-md5sum", 0, 0, 0 }, + { "show-min-blocksize", 0, 0, 0 }, + { "show-max-blocksize", 0, 0, 0 }, + { "show-min-framesize", 0, 0, 0 }, + { "show-max-framesize", 0, 0, 0 }, + { "show-sample-rate", 0, 0, 0 }, + { "show-channels", 0, 0, 0 }, + { "show-bps", 0, 0, 0 }, + { "show-total-samples", 0, 0, 0 }, + { "set-md5sum", 1, 0, 0 }, /* undocumented */ + { "set-min-blocksize", 1, 0, 0 }, /* undocumented */ + { "set-max-blocksize", 1, 0, 0 }, /* undocumented */ + { "set-min-framesize", 1, 0, 0 }, /* undocumented */ + { "set-max-framesize", 1, 0, 0 }, /* undocumented */ + { "set-sample-rate", 1, 0, 0 }, /* undocumented */ + { "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-all-tags", 0, 0, 0 }, + { "show-tag", 1, 0, 0 }, + { "remove-all-tags", 0, 0, 0 }, + { "remove-all-tags-except", 1, 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 }, + { "scan-replay-gain", 0, 0, 0 }, + { "remove-replay-gain", 0, 0, 0 }, + { "add-padding", 1, 0, 0 }, + /* major operations */ + { "help", 0, 0, 0 }, + { "version", 0, 0, 0 }, + { "list", 0, 0, 0 }, + { "append", 0, 0, 0 }, + { "remove", 0, 0, 0 }, + { "remove-all", 0, 0, 0 }, + { "merge-padding", 0, 0, 0 }, + { "sort-padding", 0, 0, 0 }, + /* major operation arguments */ + { "block-number", 1, 0, 0 }, + { "block-type", 1, 0, 0 }, + { "except-block-type", 1, 0, 0 }, + { "data-format", 1, 0, 0 }, + { "application-data-format", 1, 0, 0 }, + { "from-file", 1, 0, 0 }, + {0, 0, 0, 0} +}; + +static FLAC__bool parse_option(int option_index, const char *option_argument, CommandLineOptions *options); +static void append_new_operation(CommandLineOptions *options, Operation operation); +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_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_vorbis_comment_field_names(const char *field_ref, char **names, 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); +static FLAC__bool parse_block_number(const char *in, Argument_BlockNumber *out); +static FLAC__bool parse_block_type(const char *in, Argument_BlockType *out); +static FLAC__bool parse_data_format(const char *in, Argument_DataFormat *out); +static FLAC__bool parse_application_data_format(const char *in, FLAC__bool *out); +static void undocumented_warning(const char *opt); + + +void init_options(CommandLineOptions *options) +{ + options->preserve_modtime = false; + + /* '2' is a hack to mean "use default if not forced on command line" */ + FLAC__ASSERT(true != 2); + options->prefix_with_filename = 2; + + options->utf8_convert = true; + options->use_padding = true; + options->cued_seekpoints = true; + options->show_long_help = false; + options->show_version = false; + options->data_format_is_binary = false; + options->data_format_is_binary_headerless = false; + options->application_data_format_is_hexdump = false; + + options->ops.operations = 0; + options->ops.num_operations = 0; + options->ops.capacity = 0; + + options->args.arguments = 0; + options->args.num_arguments = 0; + options->args.capacity = 0; + + options->args.checks.num_shorthand_ops = 0; + options->args.checks.num_major_ops = 0; + options->args.checks.has_block_type = false; + options->args.checks.has_except_block_type = false; + + options->num_files = 0; + options->filenames = 0; +} + +FLAC__bool parse_options(int argc, char *argv[], CommandLineOptions *options) +{ + int ret; + int option_index = 1; + FLAC__bool had_error = false; + + while ((ret = share__getopt_long(argc, argv, "", long_options_, &option_index)) != -1) { + switch (ret) { + case 0: + had_error |= !parse_option(option_index, share__optarg, options); + break; + case '?': + case ':': + had_error = true; + break; + default: + FLAC__ASSERT(0); + break; + } + } + + if(options->prefix_with_filename == 2) + options->prefix_with_filename = (argc - share__optind > 1); + + if(share__optind >= argc && !options->show_long_help && !options->show_version) { + 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; + } + + options->num_files = argc - share__optind; + + if(options->num_files > 0) { + unsigned i = 0; + 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++]); + } + + if(options->args.checks.num_major_ops > 0) { + if(options->args.checks.num_major_ops > 1) { + 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) { + flac_fprintf(stderr, "ERROR: you may not mix shorthand and major operations\n"); + had_error = true; + } + } + + /* check for only one FLAC file used with certain options */ + if(!had_error && 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) { + flac_fprintf(stderr, "ERROR: you may not specify both '--block-type' and '--except-block-type'\n"); + had_error = true; + } + + if(had_error) + short_usage(0); + + /* + * We need to create an OP__ADD_SEEKPOINT operation if there is + * not one already, and --import-cuesheet-from was specified but + * --no-cued-seekpoints was not: + */ + if(options->cued_seekpoints) { + Operation *op = find_shorthand_operation(options, OP__IMPORT_CUESHEET_FROM); + if(0 != op) { + Operation *op2 = find_shorthand_operation(options, OP__ADD_SEEKPOINT); + if(0 == op2) + op2 = append_shorthand_operation(options, OP__ADD_SEEKPOINT); + op->argument.import_cuesheet_from.add_seekpoint_link = &(op2->argument.add_seekpoint); + } + } + + return had_error; +} + +void free_options(CommandLineOptions *options) +{ + unsigned i; + Operation *op; + Argument *arg; + + FLAC__ASSERT(0 == options->ops.operations || options->ops.num_operations > 0); + FLAC__ASSERT(0 == options->args.arguments || options->args.num_arguments > 0); + + for(i = 0, op = options->ops.operations; i < options->ops.num_operations; i++, op++) { + switch(op->type) { + case OP__SHOW_VC_FIELD: + case OP__REMOVE_VC_FIELD: + case OP__REMOVE_VC_FIRSTFIELD: + case OP__REMOVE_VC_ALL_EXCEPT: + if(0 != op->argument.vc_field_name.value) + free(op->argument.vc_field_name.value); + break; + case OP__SET_VC_FIELD: + if(0 != op->argument.vc_field.field) + free(op->argument.vc_field.field); + if(0 != op->argument.vc_field.field_name) + free(op->argument.vc_field.field_name); + if(0 != op->argument.vc_field.field_value) + free(op->argument.vc_field.field_value); + break; + case OP__IMPORT_VC_FROM: + case OP__EXPORT_VC_TO: + case OP__EXPORT_CUESHEET_TO: + if(0 != op->argument.filename.value) + free(op->argument.filename.value); + break; + case OP__IMPORT_CUESHEET_FROM: + 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); + break; + default: + break; + } + } + + for(i = 0, arg = options->args.arguments; i < options->args.num_arguments; i++, arg++) { + switch(arg->type) { + case ARG__BLOCK_NUMBER: + if(0 != arg->value.block_number.entries) + free(arg->value.block_number.entries); + break; + case ARG__BLOCK_TYPE: + case ARG__EXCEPT_BLOCK_TYPE: + if(0 != arg->value.block_type.entries) + free(arg->value.block_type.entries); + break; + case ARG__FROM_FILE: + if(0 != arg->value.from_file.file_name) + free(arg->value.from_file.file_name); + break; + default: + break; + } + } + + if(0 != options->ops.operations) + free(options->ops.operations); + + if(0 != options->args.arguments) + free(options->args.arguments); + + if(0 != options->filenames) { + for(i = 0; i < options->num_files; i++) { + if(0 != options->filenames[i]) + free(options->filenames[i]); + } + free(options->filenames); + } +} + +/* + * local routines + */ + +FLAC__bool parse_option(int option_index, const char *option_argument, CommandLineOptions *options) +{ + const char *opt = long_options_[option_index].name; + Operation *op; + Argument *arg; + FLAC__bool ok = true; + + if(0 == strcmp(opt, "preserve-modtime")) { + options->preserve_modtime = true; + } + else if(0 == strcmp(opt, "with-filename")) { + options->prefix_with_filename = true; + } + else if(0 == strcmp(opt, "no-filename")) { + options->prefix_with_filename = false; + } + else if(0 == strcmp(opt, "no-utf8-convert")) { + options->utf8_convert = false; + } + else if(0 == strcmp(opt, "dont-use-padding")) { + options->use_padding = false; + } + else if(0 == strcmp(opt, "no-cued-seekpoints")) { + options->cued_seekpoints = false; + } + else if(0 == strcmp(opt, "show-md5sum")) { + (void) append_shorthand_operation(options, OP__SHOW_MD5SUM); + } + else if(0 == strcmp(opt, "show-min-blocksize")) { + (void) append_shorthand_operation(options, OP__SHOW_MIN_BLOCKSIZE); + } + else if(0 == strcmp(opt, "show-max-blocksize")) { + (void) append_shorthand_operation(options, OP__SHOW_MAX_BLOCKSIZE); + } + else if(0 == strcmp(opt, "show-min-framesize")) { + (void) append_shorthand_operation(options, OP__SHOW_MIN_FRAMESIZE); + } + else if(0 == strcmp(opt, "show-max-framesize")) { + (void) append_shorthand_operation(options, OP__SHOW_MAX_FRAMESIZE); + } + else if(0 == strcmp(opt, "show-sample-rate")) { + (void) append_shorthand_operation(options, OP__SHOW_SAMPLE_RATE); + } + else if(0 == strcmp(opt, "show-channels")) { + (void) append_shorthand_operation(options, OP__SHOW_CHANNELS); + } + else if(0 == strcmp(opt, "show-bps")) { + (void) append_shorthand_operation(options, OP__SHOW_BPS); + } + else if(0 == strcmp(opt, "show-total-samples")) { + (void) append_shorthand_operation(options, OP__SHOW_TOTAL_SAMPLES); + } + else if(0 == strcmp(opt, "set-md5sum")) { + op = append_shorthand_operation(options, OP__SET_MD5SUM); + FLAC__ASSERT(0 != option_argument); + if(!parse_md5(option_argument, op->argument.streaminfo_md5.value)) { + flac_fprintf(stderr, "ERROR (--%s): bad MD5 sum\n", opt); + ok = false; + } + else + undocumented_warning(opt); + } + 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) { + flac_fprintf(stderr, "ERROR (--%s): value must be >= %u and <= %u\n", opt, FLAC__MIN_BLOCK_SIZE, FLAC__MAX_BLOCK_SIZE); + ok = false; + } + else + undocumented_warning(opt); + } + 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) { + flac_fprintf(stderr, "ERROR (--%s): value must be >= %u and <= %u\n", opt, FLAC__MIN_BLOCK_SIZE, FLAC__MAX_BLOCK_SIZE); + ok = false; + } + else + undocumented_warning(opt); + } + 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)) { + 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 + undocumented_warning(opt); + } + 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)) { + 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 + undocumented_warning(opt); + } + 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)) { + flac_fprintf(stderr, "ERROR (--%s): invalid sample rate\n", opt); + ok = false; + } + else + undocumented_warning(opt); + } + 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) { + flac_fprintf(stderr, "ERROR (--%s): value must be > 0 and <= %u\n", opt, FLAC__MAX_CHANNELS); + ok = false; + } + else + undocumented_warning(opt); + } + 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) { + 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 + undocumented_warning(opt); + } + 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 >= (((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")) { + (void) append_shorthand_operation(options, OP__SHOW_VC_VENDOR); + } + else if(0 == strcmp(opt, "show-tag")) { + const char *violation; + 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); + 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, "show-all-tags")) { + op = append_shorthand_operation(options, OP__EXPORT_VC_TO); + parse_string("-",&op->argument.filename.value); + } + else if(0 == strcmp(opt, "remove-all-tags")) { + (void) append_shorthand_operation(options, OP__REMOVE_VC_ALL); + } + else if(0 == strcmp(opt, "remove-all-tags-except")) { + const char *violation; + op = append_shorthand_operation(options, OP__REMOVE_VC_ALL_EXCEPT); + FLAC__ASSERT(0 != option_argument); + if(!parse_vorbis_comment_field_names(option_argument, &(op->argument.vc_field_name.value), &violation)) { + FLAC__ASSERT(0 != 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-tag")) { + const char *violation; + 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); + 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")) { + const char *violation; + 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); + 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-from-file")) { + 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 = 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); + 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")) { + op = append_shorthand_operation(options, OP__IMPORT_VC_FROM); + FLAC__ASSERT(0 != option_argument); + 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")) { + op = append_shorthand_operation(options, OP__EXPORT_VC_TO); + FLAC__ASSERT(0 != option_argument); + 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)) { + 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_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_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; + char *spec; + FLAC__ASSERT(0 != option_argument); + if(!parse_add_seekpoint(option_argument, &spec, &violation)) { + FLAC__ASSERT(0 != violation); + flac_fprintf(stderr, "ERROR (--%s): malformed seekpoint specification \"%s\",\n %s\n", opt, option_argument, violation); + ok = false; + } + 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, "scan-replay-gain")) { + (void) append_shorthand_operation(options, OP__SCAN_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))) { + 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; + } + } + else if(0 == strcmp(opt, "help")) { + options->show_long_help = true; + } + else if(0 == strcmp(opt, "version")) { + options->show_version = true; + } + else if(0 == strcmp(opt, "list")) { + (void) append_major_operation(options, OP__LIST); + } + else if(0 == strcmp(opt, "append")) { + (void) append_major_operation(options, OP__APPEND); + } + else if(0 == strcmp(opt, "remove")) { + (void) append_major_operation(options, OP__REMOVE); + } + else if(0 == strcmp(opt, "remove-all")) { + (void) append_major_operation(options, OP__REMOVE_ALL); + } + else if(0 == strcmp(opt, "merge-padding")) { + (void) append_major_operation(options, OP__MERGE_PADDING); + } + else if(0 == strcmp(opt, "sort-padding")) { + (void) append_major_operation(options, OP__SORT_PADDING); + } + else if(0 == strcmp(opt, "block-number")) { + arg = append_argument(options, ARG__BLOCK_NUMBER); + FLAC__ASSERT(0 != option_argument); + if(!parse_block_number(option_argument, &(arg->value.block_number))) { + flac_fprintf(stderr, "ERROR: malformed block number specification \"%s\"\n", option_argument); + ok = false; + } + } + else if(0 == strcmp(opt, "block-type")) { + arg = append_argument(options, ARG__BLOCK_TYPE); + FLAC__ASSERT(0 != option_argument); + if(!parse_block_type(option_argument, &(arg->value.block_type))) { + flac_fprintf(stderr, "ERROR (--%s): malformed block type specification \"%s\"\n", opt, option_argument); + ok = false; + } + options->args.checks.has_block_type = true; + } + else if(0 == strcmp(opt, "except-block-type")) { + arg = append_argument(options, ARG__EXCEPT_BLOCK_TYPE); + FLAC__ASSERT(0 != option_argument); + if(!parse_block_type(option_argument, &(arg->value.block_type))) { + flac_fprintf(stderr, "ERROR (--%s): malformed block type specification \"%s\"\n", opt, option_argument); + ok = false; + } + options->args.checks.has_except_block_type = true; + } + else if(0 == strcmp(opt, "data-format")) { + arg = append_argument(options, ARG__DATA_FORMAT); + FLAC__ASSERT(0 != option_argument); + if(!parse_data_format(option_argument, &(arg->value.data_format))) { + flac_fprintf(stderr, "ERROR (--%s): illegal data format \"%s\"\n", opt, option_argument); + ok = false; + } + options->data_format_is_binary = arg->value.data_format.is_binary; + options->data_format_is_binary_headerless = arg->value.data_format.is_headerless; + } + 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))) { + flac_fprintf(stderr, "ERROR (--%s): illegal application data format \"%s\"\n", opt, option_argument); + ok = false; + } + } + else if(0 == strcmp(opt, "from-file")) { + arg = append_argument(options, ARG__FROM_FILE); + FLAC__ASSERT(0 != option_argument); + arg->value.from_file.file_name = local_strdup(option_argument); + } + else { + FLAC__ASSERT(0); + } + + return ok; +} + +void append_new_operation(CommandLineOptions *options, Operation operation) +{ + if(options->ops.capacity == 0) { + options->ops.capacity = 50; + 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; + 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)); + } + + options->ops.operations[options->ops.num_operations++] = operation; +} + +void append_new_argument(CommandLineOptions *options, Argument argument) +{ + if(options->args.capacity == 0) { + options->args.capacity = 50; + 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; + 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)); + } + + options->args.arguments[options->args.num_arguments++] = argument; +} + +Operation *append_major_operation(CommandLineOptions *options, OperationType type) +{ + Operation op; + memset(&op, 0, sizeof(op)); + op.type = type; + append_new_operation(options, op); + options->args.checks.num_major_ops++; + return options->ops.operations + (options->ops.num_operations - 1); +} + +Operation *append_shorthand_operation(CommandLineOptions *options, OperationType type) +{ + Operation op; + memset(&op, 0, sizeof(op)); + op.type = type; + append_new_operation(options, op); + options->args.checks.num_shorthand_ops++; + 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; + for(i = 0; i < options->ops.num_operations; i++) + if(options->ops.operations[i].type == type) + return &options->ops.operations[i]; + return 0; +} + +Argument *append_argument(CommandLineOptions *options, ArgumentType type) +{ + Argument arg; + memset(&arg, 0, sizeof(arg)); + arg.type = type; + append_new_argument(options, arg); + return options->args.arguments + (options->args.num_arguments - 1); +} + +FLAC__bool parse_md5(const char *src, FLAC__byte dest[16]) +{ + unsigned i, d; + int c; + FLAC__ASSERT(0 != src); + if(strlen(src) != 32) + return false; + /* strtoul() accepts negative numbers which we do not want, so we do it the hard way */ + for(i = 0; i < 16; i++) { + c = (int)(*src++); + if(isdigit(c)) + d = (unsigned)(c - '0'); + else if(c >= 'a' && c <= 'f') + d = (unsigned)(c - 'a') + 10u; + else if(c >= 'A' && c <= 'F') + d = (unsigned)(c - 'A') + 10u; + else + return false; + d <<= 4; + c = (int)(*src++); + if(isdigit(c)) + d |= (unsigned)(c - '0'); + else if(c >= 'a' && c <= 'f') + d |= (unsigned)(c - 'a') + 10u; + else if(c >= 'A' && c <= 'F') + d |= (unsigned)(c - 'A') + 10u; + else + return false; + dest[i] = (FLAC__byte)d; + } + return true; +} + +FLAC__bool parse_uint32(const char *src, FLAC__uint32 *dest) +{ + FLAC__ASSERT(0 != src); + if(strlen(src) == 0 || strspn(src, "0123456789") != strlen(src)) + return false; + *dest = strtoul(src, 0, 10); + return true; +} + +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 = strtoull(src, 0, 10); + return true; +} + +FLAC__bool parse_string(const char *src, char **dest) +{ + if(0 == src || strlen(src) == 0) + return false; + *dest = strdup(src); + return true; +} + +FLAC__bool parse_vorbis_comment_field_name(const char *field_ref, char **name, const char **violation) +{ + static const char * const violations[] = { + "field name contains invalid character" + }; + + char *q, *s; + + s = local_strdup(field_ref); + + for(q = s; *q; q++) { + if(*q < 0x20 || *q > 0x7d || *q == 0x3d) { + free(s); + *violation = violations[0]; + return false; + } + } + + *name = s; + + return true; +} + +FLAC__bool parse_vorbis_comment_field_names(const char *field_ref, char **names, const char **violation) +{ + static const char * const violations[] = { + "field name contains invalid character" + }; + + char *q, *s; + + s = local_strdup(field_ref); + + for(q = s; *q; q++) { + if(*q < 0x20 || *q > 0x7d) { + free(s); + *violation = violations[0]; + return false; + } + } + + *names = s; + + return true; +} + +FLAC__bool parse_add_seekpoint(const char *in, char **out, const char **violation) +{ + static const char *garbled_ = "garbled specification"; + const unsigned n = strlen(in); + + FLAC__ASSERT(0 != in); + FLAC__ASSERT(0 != out); + + if(n == 0) { + *violation = "specification is empty"; + return false; + } + + if(n > strspn(in, "0123456789.Xsx")) { + *violation = "specification contains invalid character"; + return false; + } + + if(in[n-1] == 'X') { + if(n > 1) { + *violation = garbled_; + return false; + } + } + else if(in[n-1] == 's') { + if(n-1 > strspn(in, "0123456789.")) { + *violation = garbled_; + return false; + } + } + else if(in[n-1] == 'x') { + if(n-1 > strspn(in, "0123456789")) { + *violation = garbled_; + return false; + } + } + else { + if(n > strspn(in, "0123456789")) { + *violation = garbled_; + return false; + } + } + + *out = local_strdup(in); + return true; +} + +FLAC__bool parse_add_padding(const char *in, unsigned *out) +{ + FLAC__ASSERT(0 != in); + FLAC__ASSERT(0 != out); + *out = (unsigned)strtoul(in, 0, 10); + return *out < (1u << FLAC__STREAM_METADATA_LENGTH_LEN); +} + +FLAC__bool parse_block_number(const char *in, Argument_BlockNumber *out) +{ + char *p, *q, *s, *end; + long i; + unsigned entry; + + if(*in == '\0') + return false; + + s = local_strdup(in); + + /* first count the entries */ + for(out->num_entries = 1, p = strchr(s, ','); p; out->num_entries++, p = strchr(++p, ',')) + ; + + /* make space */ + FLAC__ASSERT(out->num_entries > 0); + 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 */ + entry = 0; + q = s; + while(q) { + FLAC__ASSERT(entry < out->num_entries); + if(0 != (p = strchr(q, ','))) + *p++ = '\0'; + if(!isdigit((int)(*q)) || (i = strtol(q, &end, 10)) < 0 || *end) { + free(s); + return false; + } + out->entries[entry++] = (unsigned)i; + q = p; + } + FLAC__ASSERT(entry == out->num_entries); + + free(s); + return true; +} + +FLAC__bool parse_block_type(const char *in, Argument_BlockType *out) +{ + char *p, *q, *r, *s; + unsigned entry; + + if(*in == '\0') + return false; + + s = local_strdup(in); + + /* first count the entries */ + for(out->num_entries = 1, p = strchr(s, ','); p; out->num_entries++, p = strchr(++p, ',')) + ; + + /* make space */ + FLAC__ASSERT(out->num_entries > 0); + 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 */ + entry = 0; + q = s; + while(q) { + FLAC__ASSERT(entry < out->num_entries); + if(0 != (p = strchr(q, ','))) + *p++ = 0; + r = strchr(q, ':'); + if(r) + *r++ = '\0'; + if(0 != r && 0 != strcmp(q, "APPLICATION")) { + free(s); + return false; + } + if(0 == strcmp(q, "STREAMINFO")) { + out->entries[entry++].type = FLAC__METADATA_TYPE_STREAMINFO; + } + else if(0 == strcmp(q, "PADDING")) { + out->entries[entry++].type = FLAC__METADATA_TYPE_PADDING; + } + else if(0 == strcmp(q, "APPLICATION")) { + out->entries[entry].type = FLAC__METADATA_TYPE_APPLICATION; + out->entries[entry].filter_application_by_id = (0 != r); + if(0 != 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 && FLAC__STRNCASECMP(r, "0x", 2) == 0 && strspn(r+2, "0123456789ABCDEFabcdef") == 8) { + FLAC__uint32 x = strtoul(r+2, 0, 16); + out->entries[entry].application_id[3] = (FLAC__byte)(x & 0xff); + out->entries[entry].application_id[2] = (FLAC__byte)((x>>=8) & 0xff); + out->entries[entry].application_id[1] = (FLAC__byte)((x>>=8) & 0xff); + out->entries[entry].application_id[0] = (FLAC__byte)((x>>=8) & 0xff); + } + else { + free(s); + return false; + } + } + entry++; + } + else if(0 == strcmp(q, "SEEKTABLE")) { + out->entries[entry++].type = FLAC__METADATA_TYPE_SEEKTABLE; + } + else if(0 == strcmp(q, "VORBIS_COMMENT")) { + out->entries[entry++].type = FLAC__METADATA_TYPE_VORBIS_COMMENT; + } + 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; + } + q = p; + } + FLAC__ASSERT(entry == out->num_entries); + + free(s); + return true; +} + +FLAC__bool parse_data_format(const char *in, Argument_DataFormat *out) +{ + if(0 == strcmp(in, "binary-headerless")) { + out->is_binary = false; + out->is_headerless = true; + } + else if(0 == strcmp(in, "binary")) { + out->is_binary = true; + out->is_headerless = false; + } + else if(0 == strcmp(in, "text")) { + out->is_binary = false; + out->is_headerless = false; + } + else + return false; + return true; +} + +FLAC__bool parse_application_data_format(const char *in, FLAC__bool *out) +{ + if(0 == strcmp(in, "hexdump")) + *out = true; + else if(0 == strcmp(in, "text")) + *out = false; + else + return false; + return true; +} + +void undocumented_warning(const char *opt) +{ + flac_fprintf(stderr, "WARNING: undocumented option --%s should be used with caution,\n only for repairing a damaged STREAMINFO block\n", opt); +} diff --git a/src/metaflac/options.h b/src/metaflac/options.h new file mode 100644 index 0000000..984f2e1 --- /dev/null +++ b/src/metaflac/options.h @@ -0,0 +1,221 @@ +/* metaflac - Command-line FLAC metadata editor + * Copyright (C) 2001-2009 Josh Coalson + * Copyright (C) 2011-2023 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 + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef metaflac__options_h +#define metaflac__options_h + +#include "FLAC/format.h" + +#if 0 +/*[JEC] was:#if HAVE_GETOPT_LONG*/ +/*[JEC] see flac/include/share/getopt.h as to why the change */ +# include <getopt.h> +#else +# include "share/getopt.h" +#endif + +extern struct share__option long_options_[]; + +typedef enum { + OP__SHOW_MD5SUM, + OP__SHOW_MIN_BLOCKSIZE, + OP__SHOW_MAX_BLOCKSIZE, + OP__SHOW_MIN_FRAMESIZE, + OP__SHOW_MAX_FRAMESIZE, + OP__SHOW_SAMPLE_RATE, + OP__SHOW_CHANNELS, + OP__SHOW_BPS, + OP__SHOW_TOTAL_SAMPLES, + OP__SET_MD5SUM, + OP__SET_MIN_BLOCKSIZE, + OP__SET_MAX_BLOCKSIZE, + OP__SET_MIN_FRAMESIZE, + OP__SET_MAX_FRAMESIZE, + OP__SET_SAMPLE_RATE, + OP__SET_CHANNELS, + OP__SET_BPS, + OP__SET_TOTAL_SAMPLES, + OP__SHOW_VC_VENDOR, + OP__SHOW_VC_FIELD, + OP__REMOVE_VC_ALL, + OP__REMOVE_VC_ALL_EXCEPT, + OP__REMOVE_VC_FIELD, + OP__REMOVE_VC_FIRSTFIELD, + OP__SET_VC_FIELD, + OP__IMPORT_VC_FROM, + OP__EXPORT_VC_TO, + OP__IMPORT_CUESHEET_FROM, + OP__EXPORT_CUESHEET_TO, + OP__IMPORT_PICTURE_FROM, + OP__EXPORT_PICTURE_TO, + OP__ADD_SEEKPOINT, + OP__ADD_REPLAY_GAIN, + OP__SCAN_REPLAY_GAIN, + OP__ADD_PADDING, + OP__LIST, + OP__APPEND, + OP__REMOVE, + OP__REMOVE_ALL, + OP__MERGE_PADDING, + OP__SORT_PADDING +} OperationType; + +typedef enum { + ARG__BLOCK_NUMBER, + ARG__BLOCK_TYPE, + ARG__EXCEPT_BLOCK_TYPE, + ARG__DATA_FORMAT, + ARG__FROM_FILE +} ArgumentType; + +typedef struct { + FLAC__byte value[16]; +} Argument_StreaminfoMD5; + +typedef struct { + FLAC__uint32 value; +} Argument_StreaminfoUInt32; + +typedef struct { + FLAC__uint64 value; +} Argument_StreaminfoUInt64; + +typedef struct { + char *value; +} Argument_VcFieldName; + +typedef struct { + char *field; /* the whole field as passed on the command line, i.e. "NAME=VALUE" */ + char *field_name; + /* according to the vorbis spec, field values can contain \0 so simple C strings are not enough here */ + unsigned field_value_length; + char *field_value; + FLAC__bool field_value_from_file; /* true if field_value holds a filename for the value, false for plain value */ +} Argument_VcField; + +typedef struct { + char *value; +} Argument_String; + +typedef struct { + unsigned num_entries; + unsigned *entries; +} Argument_BlockNumber; + +typedef struct { + FLAC__MetadataType type; + char application_id[4]; /* only relevant if type == FLAC__STREAM_METADATA_TYPE_APPLICATION */ + FLAC__bool filter_application_by_id; +} Argument_BlockTypeEntry; + +typedef struct { + unsigned num_entries; + Argument_BlockTypeEntry *entries; +} Argument_BlockType; + +typedef struct { + FLAC__bool is_binary; + FLAC__bool is_headerless; +} Argument_DataFormat; + +typedef struct { + char *file_name; +} Argument_FromFile; + +typedef struct { + char *specification; +} Argument_AddSeekpoint; + +typedef struct { + char *filename; + Argument_AddSeekpoint *add_seekpoint_link; +} Argument_ImportCuesheetFrom; + +typedef struct { + char *filename; + const Argument_BlockNumber *block_number_link; /* may be NULL to mean 'first PICTURE block' */ +} Argument_ExportPictureTo; + +typedef struct { + unsigned length; +} Argument_AddPadding; + +typedef struct { + OperationType type; + union { + Argument_StreaminfoMD5 streaminfo_md5; + Argument_StreaminfoUInt32 streaminfo_uint32; + Argument_StreaminfoUInt64 streaminfo_uint64; + Argument_VcFieldName vc_field_name; + Argument_VcField vc_field; + Argument_String filename; + Argument_String specification; + Argument_ImportCuesheetFrom import_cuesheet_from; + Argument_ExportPictureTo export_picture_to; + Argument_AddSeekpoint add_seekpoint; + Argument_AddPadding add_padding; + } argument; +} Operation; + +typedef struct { + ArgumentType type; + union { + Argument_BlockNumber block_number; + Argument_BlockType block_type; + Argument_DataFormat data_format; + Argument_FromFile from_file; + } value; +} Argument; + +typedef struct { + FLAC__bool preserve_modtime; + FLAC__bool prefix_with_filename; + FLAC__bool utf8_convert; + FLAC__bool use_padding; + FLAC__bool cued_seekpoints; + FLAC__bool show_long_help; + FLAC__bool show_version; + FLAC__bool data_format_is_binary; + FLAC__bool data_format_is_binary_headerless; + FLAC__bool application_data_format_is_hexdump; + struct { + Operation *operations; + unsigned num_operations; + unsigned capacity; + } ops; + struct { + struct { + unsigned num_shorthand_ops; + unsigned num_major_ops; + FLAC__bool has_block_type; + FLAC__bool has_except_block_type; + } checks; + Argument *arguments; + unsigned num_arguments; + unsigned capacity; + } args; + unsigned num_files; + char **filenames; +} CommandLineOptions; + +void init_options(CommandLineOptions *options); +FLAC__bool parse_options(int argc, char *argv[], CommandLineOptions *options); +void free_options(CommandLineOptions *options); + +#endif diff --git a/src/metaflac/usage.c b/src/metaflac/usage.c new file mode 100644 index 0000000..58afc0e --- /dev/null +++ b/src/metaflac/usage.c @@ -0,0 +1,349 @@ +/* metaflac - Command-line FLAC metadata editor + * Copyright (C) 2001-2009 Josh Coalson + * Copyright (C) 2011-2023 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 + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include "utils.h" +#include "usage.h" +#include "FLAC/format.h" +#include <stdarg.h> +#include <stdio.h> +#include "share/compat.h" + +static void usage_header(FILE *out) +{ + fprintf(out, "==============================================================================\n"); + fprintf(out, "metaflac - Command-line FLAC metadata editor version %s\n", FLAC__VERSION_STRING); + fprintf(out, "Copyright (C) 2001-2009 Josh Coalson\n"); + fprintf(out, "Copyright (C) 2011-2023 Xiph.Org Foundation\n"); + fprintf(out, "\n"); + fprintf(out, "This program is free software; you can redistribute it and/or\n"); + fprintf(out, "modify it under the terms of the GNU General Public License\n"); + fprintf(out, "as published by the Free Software Foundation; either version 2\n"); + fprintf(out, "of the License, or (at your option) any later version.\n"); + fprintf(out, "\n"); + fprintf(out, "This program is distributed in the hope that it will be useful,\n"); + fprintf(out, "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"); + fprintf(out, "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"); + fprintf(out, "GNU General Public License for more details.\n"); + fprintf(out, "\n"); + fprintf(out, "You should have received a copy of the GNU General Public License along\n"); + fprintf(out, "with this program; if not, write to the Free Software Foundation, Inc.,\n"); + fprintf(out, "51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.\n"); + fprintf(out, "==============================================================================\n"); +} + +static void usage_summary(FILE *out) +{ + fprintf(out, "Usage:\n"); + fprintf(out, " metaflac [options] [operations] FLACfile [FLACfile ...]\n"); + fprintf(out, "\n"); + fprintf(out, "Use metaflac to list, add, remove, or edit metadata in one or more FLAC files.\n"); + fprintf(out, "You may perform one major operation, or many shorthand operations at a time.\n"); + fprintf(out, "\n"); + fprintf(out, "Options:\n"); + fprintf(out, "--preserve-modtime Preserve the original modification time in spite of edits\n"); + fprintf(out, "--with-filename Prefix each output line with the FLAC file name\n"); + fprintf(out, " (the default if more than one FLAC file is specified).\n"); + fprintf(out, " This option has no effect for options exporting to a\n"); + fprintf(out, " file, like --export-tags-to.\n"); + fprintf(out, "--no-filename Do not prefix each output line with the FLAC file name\n"); + fprintf(out, " (the default if only one FLAC file is specified)\n"); + fprintf(out, "--no-utf8-convert Do not convert tags from UTF-8 to local charset,\n"); + fprintf(out, " or vice versa. This is useful for scripts, and setting\n"); + fprintf(out, " tags in situations where the locale is wrong.\n"); + fprintf(out, "--dont-use-padding By default metaflac tries to use padding where possible\n"); + fprintf(out, " to avoid rewriting the entire file if the metadata size\n"); + fprintf(out, " changes. Use this option to tell metaflac to not take\n"); + fprintf(out, " advantage of padding this way.\n"); +} + +int short_usage(const char *message, ...) +{ + va_list args; + + if(message) { + va_start(args, message); + + (void) vfprintf(stderr, message, args); + + va_end(args); + + } + usage_header(stderr); + flac_fprintf(stderr, "\n"); + flac_fprintf(stderr, "This is the short help; for full help use 'metaflac --help'\n"); + flac_fprintf(stderr, "\n"); + usage_summary(stderr); + + return message? 1 : 0; +} + +int long_usage(const char *message, ...) +{ + FILE *out = (message? stderr : stdout); + va_list args; + + if(message) { + va_start(args, message); + + (void) vfprintf(stderr, message, args); + + va_end(args); + + } + usage_header(out); + fprintf(out, "\n"); + usage_summary(out); + fprintf(out, "\n"); + fprintf(out, "Shorthand operations:\n"); + fprintf(out, "--show-md5sum Show the MD5 signature from the STREAMINFO block.\n"); + fprintf(out, "--show-min-blocksize Show the minimum block size from the STREAMINFO block.\n"); + fprintf(out, "--show-max-blocksize Show the maximum block size from the STREAMINFO block.\n"); + fprintf(out, "--show-min-framesize Show the minimum frame size from the STREAMINFO block.\n"); + fprintf(out, "--show-max-framesize Show the maximum frame size from the STREAMINFO block.\n"); + fprintf(out, "--show-sample-rate Show the sample rate from the STREAMINFO block.\n"); + fprintf(out, "--show-channels Show the number of channels from the STREAMINFO block.\n"); + fprintf(out, "--show-bps Show the # of bits per sample from the STREAMINFO block.\n"); + fprintf(out, "--show-total-samples Show the total # of samples from the STREAMINFO block.\n"); + fprintf(out, "\n"); + fprintf(out, "--show-vendor-tag Show the vendor string from the VORBIS_COMMENT block.\n"); + fprintf(out, "--show-tag=NAME Show all tags where the field name matches 'NAME'.\n"); + fprintf(out, "--show-all-tags Show all tags. This is an alias for --export-tags-to=-.\n"); + fprintf(out, "--remove-tag=NAME Remove all tags whose field name is 'NAME'.\n"); + fprintf(out, "--remove-first-tag=NAME Remove first tag whose field name is 'NAME'.\n"); + fprintf(out, "--remove-all-tags Remove all tags, leaving only the vendor string.\n"); + fprintf(out, "--remove-all-tags-except=NAME1[=NAME2[=...]] Remove all tags, except the vendor\n"); + fprintf(out, " string and the tag names specified. Tag names must be\n"); + fprintf(out, " separated by an = character.\n"); + fprintf(out, "--set-tag=FIELD Add a tag. The FIELD must comply with the Vorbis comment\n"); + fprintf(out, " spec, of the form \"NAME=VALUE\". If there is currently\n"); + fprintf(out, " no tag block, one will be created.\n"); + fprintf(out, "--set-tag-from-file=FIELD Like --set-tag, except the VALUE is a filename\n"); + fprintf(out, " whose contents will be read verbatim to set the tag value.\n"); + fprintf(out, " Unless --no-utf8-convert is specified, the contents will\n"); + fprintf(out, " be converted to UTF-8 from the local charset. This can\n"); + fprintf(out, " be used to store a cuesheet in a tag (e.g.\n"); + fprintf(out, " --set-tag-from-file=\"CUESHEET=image.cue\"). Do not try\n"); + fprintf(out, " to store binary data in tag fields! Use APPLICATION\n"); + fprintf(out, " blocks for that.\n"); + fprintf(out, "--import-tags-from=FILE Import tags from a file. Use '-' for stdin. Each line\n"); + fprintf(out, " should be of the form NAME=VALUE. Multi-line comments\n"); + fprintf(out, " are currently not supported. Specify --remove-all-tags\n"); + fprintf(out, " and/or --no-utf8-convert before --import-tags-from if\n"); + fprintf(out, " necessary. If FILE is '-' (stdin), only one FLAC file\n"); + fprintf(out, " may be specified.\n"); + fprintf(out, "--export-tags-to=FILE Export tags to a file. Use '-' for stdout. Each line\n"); + fprintf(out, " will be of the form NAME=VALUE. Specify\n"); + fprintf(out, " --no-utf8-convert if necessary.\n"); + fprintf(out, "--import-cuesheet-from=FILE Import a cuesheet from a file. Use '-' for stdin.\n"); + fprintf(out, " Only one FLAC file may be specified. A seekpoint will be\n"); + fprintf(out, " added for each index point in the cuesheet to the\n"); + fprintf(out, " SEEKTABLE unless --no-cued-seekpoints is specified.\n"); + fprintf(out, "--export-cuesheet-to=FILE Export CUESHEET block to a cuesheet file, suitable\n"); + fprintf(out, " for use by CD authoring software. Use '-' for stdout.\n"); + fprintf(out, " Only one FLAC file may be specified on the command line.\n"); + fprintf(out, "--import-picture-from=FILENAME|SPECIFICATION Import a picture and store it in a\n"); + fprintf(out, " PICTURE block. Either a filename for the picture file or\n"); + fprintf(out, " a more complete specification form can be used. The\n"); + fprintf(out, " SPECIFICATION is a string whose parts are separated by |\n"); + fprintf(out, " characters. Some parts may be left empty to invoke\n"); + fprintf(out, " default values. FILENAME is just shorthand for\n"); + fprintf(out, " \"||||FILENAME\". The format of SPECIFICATION is:\n"); + fprintf(out, " [TYPE]|[MIME-TYPE]|[DESCRIPTION]|[WIDTHxHEIGHTxDEPTH[/COLORS]]|FILE\n"); + fprintf(out, " TYPE is optional; it is a number from one of:\n"); + fprintf(out, " 0: Other\n"); + fprintf(out, " 1: 32x32 pixels 'file icon' (PNG only)\n"); + fprintf(out, " 2: Other file icon\n"); + fprintf(out, " 3: Cover (front)\n"); + fprintf(out, " 4: Cover (back)\n"); + fprintf(out, " 5: Leaflet page\n"); + fprintf(out, " 6: Media (e.g. label side of CD)\n"); + fprintf(out, " 7: Lead artist/lead performer/soloist\n"); + fprintf(out, " 8: Artist/performer\n"); + fprintf(out, " 9: Conductor\n"); + fprintf(out, " 10: Band/Orchestra\n"); + fprintf(out, " 11: Composer\n"); + fprintf(out, " 12: Lyricist/text writer\n"); + fprintf(out, " 13: Recording Location\n"); + fprintf(out, " 14: During recording\n"); + fprintf(out, " 15: During performance\n"); + fprintf(out, " 16: Movie/video screen capture\n"); + fprintf(out, " 17: A bright coloured fish\n"); + fprintf(out, " 18: Illustration\n"); + fprintf(out, " 19: Band/artist logotype\n"); + fprintf(out, " 20: Publisher/Studio logotype\n"); + fprintf(out, " The default is 3 (front cover). There may only be one picture each\n"); + fprintf(out, " of type 1 and 2 in a file.\n"); + fprintf(out, " MIME-TYPE is optional; if left blank, it will be detected from the\n"); + fprintf(out, " file. For best compatibility with players, use pictures with MIME\n"); + fprintf(out, " type image/jpeg or image/png. The MIME type can also be --> to\n"); + fprintf(out, " mean that FILE is actually a URL to an image, though this use is\n"); + fprintf(out, " discouraged.\n"); + fprintf(out, " DESCRIPTION is optional; the default is an empty string\n"); + fprintf(out, " The next part specifies the resolution and color information. If\n"); + fprintf(out, " the MIME-TYPE is image/jpeg, image/png, or image/gif, you can\n"); + fprintf(out, " usually leave this empty and they can be detected from the file.\n"); + fprintf(out, " Otherwise, you must specify the width in pixels, height in pixels,\n"); + fprintf(out, " and color depth in bits-per-pixel. If the image has indexed colors\n"); + fprintf(out, " you should also specify the number of colors used.\n"); + fprintf(out, " FILE is the path to the picture file to be imported, or the URL if\n"); + fprintf(out, " MIME type is -->\n"); + fprintf(out, "--export-picture-to=FILE Export PICTURE block to a file. Use '-' for stdout.\n"); + fprintf(out, " Only one FLAC file may be specified. The first PICTURE\n"); + fprintf(out, " block will be exported unless --export-picture-to is\n"); + fprintf(out, " preceded by a --block-number=# option to specify the exact\n"); + fprintf(out, " metadata block to extract. Note that the block number is\n"); + fprintf(out, " the one shown by --list.\n"); + fprintf(out, "--add-replay-gain Calculates the title and album gains/peaks of the given\n"); + fprintf(out, " FLAC files as if all the files were part of one album,\n"); + fprintf(out, " then stores them in the VORBIS_COMMENT block. The tags\n"); + fprintf(out, " are the same as those used by vorbisgain. Existing\n"); + fprintf(out, " ReplayGain tags will be replaced. If only one FLAC file\n"); + fprintf(out, " is given, the album and title gains will be the same.\n"); + fprintf(out, " Since this operation requires two passes, it is always\n"); + fprintf(out, " executed last, after all other operations have been\n"); + fprintf(out, " completed and written to disk. All FLAC files specified\n"); + fprintf(out, " must have the same resolution, sample rate, and number\n"); + fprintf(out, " of channels. Only mono and stereo files are allowed,\n"); + fprintf(out, " and the sample rate must be 8, 11.025, 12, 16, 18.9,\n"); + fprintf(out, " 22.05, 24, 28, 32, 36, 37.8, 44.1, 48, 56, 64, 72, 75.6,\n"); + fprintf(out, " 88.2, 96, 112, 128, 144, 151.2, 176.4, 192, 224, 256,\n"); + fprintf(out, " 288, 302.4, 352.8, 384, 448, 512, 576, or 604.8 kHz.\n"); + fprintf(out, "--scan-replay-gain Like --add-replay-gain, but only analyzes the files\n"); + fprintf(out, " rather than writing them to tags.\n"); + fprintf(out, "--remove-replay-gain Removes the ReplayGain tags.\n"); + fprintf(out, "--add-seekpoint={#|X|#x|#s} Add seek points to a SEEKTABLE block\n"); + fprintf(out, " # : a specific sample number for a seek point\n"); + fprintf(out, " X : a placeholder point (always goes at the end of the SEEKTABLE)\n"); + fprintf(out, " #x : # evenly spaced seekpoints, the first being at sample 0\n"); + fprintf(out, " #s : a seekpoint every # seconds; # does not have to be a whole number\n"); + fprintf(out, " If no SEEKTABLE block exists, one will be created. If\n"); + fprintf(out, " one already exists, points will be added to the existing\n"); + fprintf(out, " table, and any duplicates will be turned into placeholder\n"); + fprintf(out, " points. You may use many --add-seekpoint options; the\n"); + fprintf(out, " resulting SEEKTABLE will be the unique-ified union of\n"); + fprintf(out, " all such values. Example: --add-seekpoint=100x\n"); + fprintf(out, " --add-seekpoint=3.5s will add 100 evenly spaced\n"); + fprintf(out, " seekpoints and a seekpoint every 3.5 seconds.\n"); + fprintf(out, "--add-padding=length Add a padding block of the given length (in bytes).\n"); + fprintf(out, " The overall length of the new block will be 4 + length;\n"); + fprintf(out, " the extra 4 bytes is for the metadata block header.\n"); + fprintf(out, "\n"); + fprintf(out, "Major operations:\n"); + fprintf(out, "--version\n"); + fprintf(out, " Show the metaflac version number.\n"); + fprintf(out, "--list\n"); + fprintf(out, " List the contents of one or more metadata blocks to stdout. By default,\n"); + fprintf(out, " all metadata blocks are listed in text format. Use the following options\n"); + fprintf(out, " to change this behavior:\n"); + fprintf(out, "\n"); + fprintf(out, " --block-number=#[,#[...]]\n"); + fprintf(out, " An optional comma-separated list of block numbers to display. The first\n"); + fprintf(out, " block, the STREAMINFO block, is block 0.\n"); + fprintf(out, "\n"); + fprintf(out, " --block-type=type[,type[...]]\n"); + fprintf(out, " --except-block-type=type[,type[...]]\n"); + fprintf(out, " An optional comma-separated list of block types to be included or ignored\n"); + fprintf(out, " with this option. Use only one of --block-type or --except-block-type.\n"); + fprintf(out, " The valid block types are: STREAMINFO, PADDING, APPLICATION, SEEKTABLE,\n"); + fprintf(out, " VORBIS_COMMENT. You may narrow down the types of APPLICATION blocks\n"); + fprintf(out, " displayed as follows:\n"); + fprintf(out, " APPLICATION:abcd The APPLICATION block(s) whose textual repre-\n"); + fprintf(out, " sentation of the 4-byte ID is \"abcd\"\n"); + fprintf(out, " APPLICATION:0xXXXXXXXX The APPLICATION block(s) whose hexadecimal big-\n"); + fprintf(out, " endian representation of the 4-byte ID is\n"); + fprintf(out, " \"0xXXXXXXXX\". For the example \"abcd\" above the\n"); + fprintf(out, " hexadecimal equivalalent is 0x61626364\n"); + fprintf(out, "\n"); + fprintf(out, " NOTE: if both --block-number and --[except-]block-type are specified,\n"); + fprintf(out, " the result is the logical AND of both arguments.\n"); + fprintf(out, "\n"); + fprintf(out, " --data-format=binary|binary-headerless|text\n"); + fprintf(out, " By default a human-readable text representation of the data is displayed.\n"); + fprintf(out, " You may specify --data-format=binary to dump the raw binary form of each\n"); + fprintf(out, " metadata block. Specify --data-format=binary-headerless to omit output of\n"); + fprintf(out, " metadata block headers, including the id of APPLICATION metadata blocks.\n"); + fprintf(out, " The output can be read in using a subsequent call to\n"); + fprintf(out, " \"metaflac --append\"\n"); + fprintf(out, "\n"); + fprintf(out, " --application-data-format=hexdump|text\n"); + fprintf(out, " If the application block you are displaying contains binary data but your\n"); + fprintf(out, " --data-format=text, you can display a hex dump of the application data\n"); + fprintf(out, " contents instead using --application-data-format=hexdump\n"); + fprintf(out, "\n"); + fprintf(out, "--append\n"); + fprintf(out, " Insert a metadata block from a file. This must be a binary block as\n"); + fprintf(out, " exported with --list --data-format=binary. The insertion point is\n"); + fprintf(out, " defined with --block-number=#. The new block will be added after the\n"); + fprintf(out, " given block number. This prevents the illegal insertion of a block\n"); + fprintf(out, " before the first STREAMINFO block. You may not --append another\n"); + fprintf(out, " STREAMINFO block. It is possible to copy a metadata block from one\n"); + fprintf(out, " file to another with this option. For example use\n"); + fprintf(out, " metaflac --list --data-format=binary --block-number=6 file.flac > block\n"); + fprintf(out, " to export the block, and then import it with\n"); + fprintf(out, " metaflac --append anotherfile.flac < block\n"); + fprintf(out, " Insert a metadata block from a file. The input file must be in the same\n"); + fprintf(out, " format as generated with --list.\n"); + fprintf(out, "\n"); + fprintf(out, " --block-number=#\n"); + fprintf(out, " Specify the insertion point (defaults to last block). The new block will\n"); + fprintf(out, " be added after the given block number. This prevents the illegal insertion\n"); + fprintf(out, " of a block before the first STREAMINFO block. You may not --append another\n"); + fprintf(out, " STREAMINFO block.\n"); + fprintf(out, "\n"); +#if 0 + /*@@@ not implemented yet */ + fprintf(out, " --from-file=filename\n"); + fprintf(out, " Mandatory 'option' to specify the input file containing the block contents.\n"); + fprintf(out, "\n"); + fprintf(out, " --data-format=binary|text\n"); + fprintf(out, " By default the block contents are assumed to be in binary format. You can\n"); + fprintf(out, " override this by specifying --data-format=text\n"); + fprintf(out, "\n"); +#endif + fprintf(out, "--remove\n"); + fprintf(out, " Remove one or more metadata blocks from the metadata. Unless\n"); + fprintf(out, " --dont-use-padding is specified, the blocks will be replaced with padding.\n"); + fprintf(out, " You may not remove the STREAMINFO block.\n"); + fprintf(out, "\n"); + fprintf(out, " --block-number=#[,#[...]]\n"); + fprintf(out, " --block-type=type[,type[...]]\n"); + fprintf(out, " --except-block-type=type[,type[...]]\n"); + fprintf(out, " See --list above for usage.\n"); + fprintf(out, "\n"); + fprintf(out, " NOTE: if both --block-number and --[except-]block-type are specified,\n"); + fprintf(out, " the result is the logical AND of both arguments.\n"); + fprintf(out, "\n"); + fprintf(out, "--remove-all\n"); + fprintf(out, " Remove all metadata blocks (except the STREAMINFO block) from the\n"); + fprintf(out, " metadata. Unless --dont-use-padding is specified, the blocks will be\n"); + fprintf(out, " replaced with padding.\n"); + fprintf(out, "\n"); + fprintf(out, "--merge-padding\n"); + fprintf(out, " Merge adjacent PADDING blocks into single blocks.\n"); + fprintf(out, "\n"); + fprintf(out, "--sort-padding\n"); + fprintf(out, " Move all PADDING blocks to the end of the metadata and merge them into a\n"); + fprintf(out, " single block.\n"); + + return message? 1 : 0; +} diff --git a/src/metaflac/usage.h b/src/metaflac/usage.h new file mode 100644 index 0000000..1366417 --- /dev/null +++ b/src/metaflac/usage.h @@ -0,0 +1,26 @@ +/* metaflac - Command-line FLAC metadata editor + * Copyright (C) 2001-2009 Josh Coalson + * Copyright (C) 2011-2023 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 + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef metaflac__usage_h +#define metaflac__usage_h + +int short_usage(const char *message, ...); +int long_usage(const char *message, ...); + +#endif diff --git a/src/metaflac/utils.c b/src/metaflac/utils.c new file mode 100644 index 0000000..045719a --- /dev/null +++ b/src/metaflac/utils.c @@ -0,0 +1,282 @@ +/* metaflac - Command-line FLAC metadata editor + * Copyright (C) 2001-2009 Josh Coalson + * Copyright (C) 2011-2023 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 + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <ctype.h> +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include "utils.h" +#include "FLAC/assert.h" +#include "share/alloc.h" +#include "share/safe_str.h" +#include "share/utf8.h" +#include "share/compat.h" + +void die(const char *message) +{ + FLAC__ASSERT(0 != message); + flac_fprintf(stderr, "ERROR: %s\n", message); + exit(1); +} + +#ifdef FLAC__VALGRIND_TESTING +size_t local_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) +{ + size_t ret = fwrite(ptr, size, nmemb, stream); + if(!ferror(stream)) + fflush(stream); + return ret; +} +#endif + +char *local_strdup(const char *source) +{ + char *ret; + FLAC__ASSERT(0 != source); + if(0 == (ret = strdup(source))) + die("out of memory during strdup()"); + return ret; +} + +void local_strcat(char **dest, const char *source) +{ + size_t ndest, nsource, outlen; + + FLAC__ASSERT(0 != dest); + FLAC__ASSERT(0 != source); + + ndest = *dest ? strlen(*dest) : 0; + nsource = strlen(source); + outlen = ndest + nsource + 1; + + if(nsource == 0) + return; + + *dest = safe_realloc_add_3op_(*dest, ndest, /*+*/nsource, /*+*/1); + if(*dest == NULL) + die("out of memory growing string"); + /* If ndest == 0, strlen in safe_strncat reads + * uninitialized data. To prevent that, set first character + * to zero */ + if(ndest == 0) + *dest[0] = 0; + safe_strncat(*dest, source, outlen); +} + +static inline int local_isprint(int c) +{ + if (c < 32) return 0; + if (c > 127) return 0; + return isprint(c); +} + +void hexdump(const char *filename, const FLAC__byte *buf, unsigned bytes, const char *indent) +{ + unsigned i, left = bytes; + const FLAC__byte *b = buf; + + for(i = 0; i < bytes; i += 16) { + flac_printf("%s%s", filename? filename:"", filename? ":":""); + printf("%s%08X: " + "%02X %02X %02X %02X %02X %02X %02X %02X " + "%02X %02X %02X %02X %02X %02X %02X %02X " + "%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n", + indent, i, + left > 0? (unsigned char)b[ 0] : 0, + left > 1? (unsigned char)b[ 1] : 0, + left > 2? (unsigned char)b[ 2] : 0, + left > 3? (unsigned char)b[ 3] : 0, + left > 4? (unsigned char)b[ 4] : 0, + left > 5? (unsigned char)b[ 5] : 0, + left > 6? (unsigned char)b[ 6] : 0, + left > 7? (unsigned char)b[ 7] : 0, + left > 8? (unsigned char)b[ 8] : 0, + left > 9? (unsigned char)b[ 9] : 0, + left > 10? (unsigned char)b[10] : 0, + left > 11? (unsigned char)b[11] : 0, + left > 12? (unsigned char)b[12] : 0, + left > 13? (unsigned char)b[13] : 0, + left > 14? (unsigned char)b[14] : 0, + left > 15? (unsigned char)b[15] : 0, + (left > 0) ? (local_isprint(b[ 0]) ? b[ 0] : '.') : ' ', + (left > 1) ? (local_isprint(b[ 1]) ? b[ 1] : '.') : ' ', + (left > 2) ? (local_isprint(b[ 2]) ? b[ 2] : '.') : ' ', + (left > 3) ? (local_isprint(b[ 3]) ? b[ 3] : '.') : ' ', + (left > 4) ? (local_isprint(b[ 4]) ? b[ 4] : '.') : ' ', + (left > 5) ? (local_isprint(b[ 5]) ? b[ 5] : '.') : ' ', + (left > 6) ? (local_isprint(b[ 6]) ? b[ 6] : '.') : ' ', + (left > 7) ? (local_isprint(b[ 7]) ? b[ 7] : '.') : ' ', + (left > 8) ? (local_isprint(b[ 8]) ? b[ 8] : '.') : ' ', + (left > 9) ? (local_isprint(b[ 9]) ? b[ 9] : '.') : ' ', + (left > 10) ? (local_isprint(b[10]) ? b[10] : '.') : ' ', + (left > 11) ? (local_isprint(b[11]) ? b[11] : '.') : ' ', + (left > 12) ? (local_isprint(b[12]) ? b[12] : '.') : ' ', + (left > 13) ? (local_isprint(b[13]) ? b[13] : '.') : ' ', + (left > 14) ? (local_isprint(b[14]) ? b[14] : '.') : ' ', + (left > 15) ? (local_isprint(b[15]) ? b[15] : '.') : ' ' + ); + left -= 16; + b += 16; + } +} + +void print_error_with_chain_status(FLAC__Metadata_Chain *chain, const char *format, ...) +{ + const FLAC__Metadata_ChainStatus status = FLAC__metadata_chain_status(chain); + va_list args; + + FLAC__ASSERT(0 != format); + + va_start(args, format); + + (void) flac_vfprintf(stderr, format, args); + + va_end(args); + + flac_fprintf(stderr, ", status = \"%s\"\n", FLAC__Metadata_ChainStatusString[status]); + + if(status == FLAC__METADATA_CHAIN_STATUS_ERROR_OPENING_FILE) { + flac_fprintf(stderr, "\n" + "The FLAC file could not be opened. Most likely the file does not exist\n" + "or is not readable.\n" + ); + } + else if(status == FLAC__METADATA_CHAIN_STATUS_NOT_A_FLAC_FILE) { + flac_fprintf(stderr, "\n" + "The file does not appear to be a FLAC file.\n" + ); + } + else if(status == FLAC__METADATA_CHAIN_STATUS_NOT_WRITABLE) { + flac_fprintf(stderr, "\n" + "The FLAC file does not have write permissions.\n" + ); + } + else if(status == FLAC__METADATA_CHAIN_STATUS_BAD_METADATA) { + flac_fprintf(stderr, "\n" + "The metadata to be written does not conform to the FLAC metadata\n" + "specifications.\n" + ); + } + else if(status == FLAC__METADATA_CHAIN_STATUS_READ_ERROR) { + flac_fprintf(stderr, "\n" + "There was an error while reading the FLAC file.\n" + ); + } + else if(status == FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR) { + flac_fprintf(stderr, "\n" + "There was an error while writing FLAC file; most probably the disk is\n" + "full.\n" + ); + } + else if(status == FLAC__METADATA_CHAIN_STATUS_UNLINK_ERROR) { + flac_fprintf(stderr, "\n" + "There was an error removing the temporary FLAC file.\n" + ); + } +} + +FLAC__bool parse_vorbis_comment_field(const char *field_ref, char **field, char **name, char **value, unsigned *length, const char **violation) +{ + static const char * const violations[] = { + "field name contains invalid character", + "field contains no '=' character" + }; + + char *p, *q, *s; + + if(0 != field) + *field = local_strdup(field_ref); + + s = local_strdup(field_ref); + + if(0 == (p = strchr(s, '='))) { + free(s); + *violation = violations[1]; + return false; + } + *p++ = '\0'; + + for(q = s; *q; q++) { + if(*q < 0x20 || *q > 0x7d || *q == 0x3d) { + free(s); + *violation = violations[0]; + return false; + } + } + + *name = local_strdup(s); + *value = local_strdup(p); + *length = strlen(p); + + free(s); + return true; +} + +void write_vc_field(const char *filename, const FLAC__StreamMetadata_VorbisComment_Entry *entry, FLAC__bool raw, FILE *f) +{ + if(0 != entry->entry) { + if(filename) + flac_fprintf(f, "%s:", filename); + + if(!raw) { + /* + * WATCHOUT: comments that contain an embedded null will + * be truncated by utf_decode(). + */ +#ifdef _WIN32 /* if we are outputting to console, we need to use proper print functions to show unicode characters */ + if (f == stdout || f == stderr) { + flac_fprintf(f, "%s", entry->entry); + } else { +#endif + char *converted; + + if(utf8_decode((const char *)entry->entry, &converted) >= 0) { + (void) local_fwrite(converted, 1, strlen(converted), f); + free(converted); + } + else { + (void) local_fwrite(entry->entry, 1, entry->length, f); + } +#ifdef _WIN32 + } +#endif + } + else { + (void) local_fwrite(entry->entry, 1, entry->length, f); + } + } + + putc('\n', f); +} + +void write_vc_fields(const char *filename, const char *field_name, const FLAC__StreamMetadata_VorbisComment_Entry entry[], unsigned num_entries, FLAC__bool raw, FILE *f) +{ + unsigned i; + const unsigned field_name_length = (0 != field_name)? strlen(field_name) : 0; + + for(i = 0; i < num_entries; i++) { + if(0 == field_name || FLAC__metadata_object_vorbiscomment_entry_matches(entry[i], field_name, field_name_length)) + write_vc_field(filename, entry + i, raw, f); + } +} diff --git a/src/metaflac/utils.h b/src/metaflac/utils.h new file mode 100644 index 0000000..972a450 --- /dev/null +++ b/src/metaflac/utils.h @@ -0,0 +1,47 @@ +/* metaflac - Command-line FLAC metadata editor + * Copyright (C) 2001-2009 Josh Coalson + * Copyright (C) 2011-2023 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 + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef metaflac__utils_h +#define metaflac__utils_h + +#include "FLAC/metadata.h" +#include <stdio.h> /* for FILE */ + +#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION +#undef stderr +#define stderr stdout +#endif + +void die(const char *message); +#ifdef FLAC__VALGRIND_TESTING +size_t local_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream); +#else +#define local_fwrite fwrite +#endif +char *local_strdup(const char *source); +void local_strcat(char **dest, const char *source); +void hexdump(const char *filename, const FLAC__byte *buf, unsigned bytes, const char *indent); +void print_error_with_chain_status(FLAC__Metadata_Chain *chain, const char *format, ...); + +FLAC__bool parse_vorbis_comment_field(const char *field_ref, char **field, char **name, char **value, unsigned *length, const char **violation); + +void write_vc_field(const char *filename, const FLAC__StreamMetadata_VorbisComment_Entry *entry, FLAC__bool raw, FILE *f); +void write_vc_fields(const char *filename, const char *field_name, const FLAC__StreamMetadata_VorbisComment_Entry entry[], unsigned num_entries, FLAC__bool raw, FILE *f); + +#endif diff --git a/src/metaflac/version.rc b/src/metaflac/version.rc new file mode 100644 index 0000000..5117202 --- /dev/null +++ b/src/metaflac/version.rc @@ -0,0 +1,38 @@ +#include <winver.h> +#include "config.h" + +#if (defined GIT_COMMIT_HASH && defined GIT_COMMIT_DATE) +# ifdef GIT_COMMIT_TAG +# define VERSIONSTRING GIT_COMMIT_TAG +# else +# define VERSIONSTRING "git-" GIT_COMMIT_HASH +# endif +#else +# define VERSIONSTRING PACKAGE_VERSION +#endif + +#define xstr(s) str(s) +#define str(s) #s + +VS_VERSION_INFO VERSIONINFO +FILEFLAGSMASK VS_FFI_FILEFLAGSMASK +FILEFLAGS 0 +FILEOS VOS__WINDOWS32 +FILETYPE VFT_DLL +BEGIN + BLOCK "StringFileInfo" + BEGIN + BLOCK "040904b0" + BEGIN + VALUE "FileDescription", "metaflac command line tool for Windows" + VALUE "ProductName", "Free Lossless Audio Codec" + VALUE "ProductVersion", VERSIONSTRING + VALUE "CompanyName", "Xiph.Org" + VALUE "LegalCopyright", "2000-2009 Josh Coalson, 2011-2023 Xiph.Org Foundation" + END + END + BLOCK "VarFileInfo" + BEGIN + VALUE "Translation", 0x409, 1200 + END +END |