1 /* metaflac - Command-line FLAC metadata editor
2 * Copyright (C) 2001,2002,2003,2004,2005,2006,2007,2008,2009 Josh Coalson
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include "FLAC/assert.h"
26 #include "share/grabbag.h" /* for grabbag__file_get_filesize() */
27 #include "share/utf8.h"
31 #include "operations_shorthand.h"
32 #include "share/compat.h"
34 static FLAC__bool remove_vc_all(const char *filename, FLAC__StreamMetadata *block, FLAC__bool *needs_write);
35 static FLAC__bool remove_vc_field(const char *filename, FLAC__StreamMetadata *block, const char *field_name, FLAC__bool *needs_write);
36 static FLAC__bool remove_vc_firstfield(const char *filename, FLAC__StreamMetadata *block, const char *field_name, FLAC__bool *needs_write);
37 static FLAC__bool set_vc_field(const char *filename, FLAC__StreamMetadata *block, const Argument_VcField *field, FLAC__bool *needs_write, FLAC__bool raw);
38 static FLAC__bool import_vc_from(const char *filename, FLAC__StreamMetadata *block, const Argument_String *vc_filename, FLAC__bool *needs_write, FLAC__bool raw);
39 static FLAC__bool export_vc_to(const char *filename, FLAC__StreamMetadata *block, const Argument_String *vc_filename, FLAC__bool raw);
41 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)
43 FLAC__bool ok = true, found_vc_block = false;
44 FLAC__StreamMetadata *block = 0;
45 FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new();
48 die("out of memory allocating iterator");
50 FLAC__metadata_iterator_init(iterator, chain);
53 block = FLAC__metadata_iterator_get_block(iterator);
54 if(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT)
55 found_vc_block = true;
56 } while(!found_vc_block && FLAC__metadata_iterator_next(iterator));
59 /* create a new block if necessary */
60 if(operation->type == OP__SET_VC_FIELD || operation->type == OP__IMPORT_VC_FROM) {
61 block = FLAC__metadata_object_new(FLAC__METADATA_TYPE_VORBIS_COMMENT);
63 die("out of memory allocating VORBIS_COMMENT block");
64 while(FLAC__metadata_iterator_next(iterator))
66 if(!FLAC__metadata_iterator_insert_block_after(iterator, block)) {
67 print_error_with_chain_status(chain, "%s: ERROR: adding new VORBIS_COMMENT block to metadata", filename);
70 /* iterator is left pointing to new block */
71 FLAC__ASSERT(FLAC__metadata_iterator_get_block(iterator) == block);
74 FLAC__metadata_iterator_delete(iterator);
79 FLAC__ASSERT(0 != block);
80 FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
82 switch(operation->type) {
83 case OP__SHOW_VC_VENDOR:
84 write_vc_field(prefix_with_filename? filename : 0, &block->data.vorbis_comment.vendor_string, raw, stdout);
86 case OP__SHOW_VC_FIELD:
87 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);
89 case OP__REMOVE_VC_ALL:
90 ok = remove_vc_all(filename, block, needs_write);
92 case OP__REMOVE_VC_FIELD:
93 ok = remove_vc_field(filename, block, operation->argument.vc_field_name.value, needs_write);
95 case OP__REMOVE_VC_FIRSTFIELD:
96 ok = remove_vc_firstfield(filename, block, operation->argument.vc_field_name.value, needs_write);
98 case OP__SET_VC_FIELD:
99 #ifdef _WIN32 /* do not convert anything or things will break */
100 ok = set_vc_field(filename, block, &operation->argument.vc_field, needs_write, true);
102 ok = set_vc_field(filename, block, &operation->argument.vc_field, needs_write, raw);
105 case OP__IMPORT_VC_FROM:
106 ok = import_vc_from(filename, block, &operation->argument.filename, needs_write, raw);
108 case OP__EXPORT_VC_TO:
109 ok = export_vc_to(filename, block, &operation->argument.filename, raw);
117 FLAC__metadata_iterator_delete(iterator);
125 FLAC__bool remove_vc_all(const char *filename, FLAC__StreamMetadata *block, FLAC__bool *needs_write)
127 FLAC__ASSERT(0 != block);
128 FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
129 FLAC__ASSERT(0 != needs_write);
131 if(0 != block->data.vorbis_comment.comments) {
132 FLAC__ASSERT(block->data.vorbis_comment.num_comments > 0);
133 if(!FLAC__metadata_object_vorbiscomment_resize_comments(block, 0)) {
134 flac_fprintf(stderr, "%s: ERROR: memory allocation failure\n", filename);
140 FLAC__ASSERT(block->data.vorbis_comment.num_comments == 0);
146 FLAC__bool remove_vc_field(const char *filename, FLAC__StreamMetadata *block, const char *field_name, FLAC__bool *needs_write)
150 FLAC__ASSERT(0 != needs_write);
152 n = FLAC__metadata_object_vorbiscomment_remove_entries_matching(block, field_name);
155 flac_fprintf(stderr, "%s: ERROR: memory allocation failure\n", filename);
164 FLAC__bool remove_vc_firstfield(const char *filename, FLAC__StreamMetadata *block, const char *field_name, FLAC__bool *needs_write)
168 FLAC__ASSERT(0 != needs_write);
170 n = FLAC__metadata_object_vorbiscomment_remove_entry_matching(block, field_name);
173 flac_fprintf(stderr, "%s: ERROR: memory allocation failure\n", filename);
182 FLAC__bool set_vc_field(const char *filename, FLAC__StreamMetadata *block, const Argument_VcField *field, FLAC__bool *needs_write, FLAC__bool raw)
184 FLAC__StreamMetadata_VorbisComment_Entry entry;
187 FLAC__ASSERT(0 != block);
188 FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
189 FLAC__ASSERT(0 != field);
190 FLAC__ASSERT(0 != needs_write);
192 if(field->field_value_from_file) {
193 /* read the file into 'data' */
196 const FLAC__off_t size = grabbag__file_get_filesize(field->field_value);
198 flac_fprintf(stderr, "%s: ERROR: can't open file '%s' for '%s' tag value\n", filename, field->field_value, field->field_name);
201 if(size >= 0x100000) { /* magic arbitrary limit, actual format limit is near 16MB */
202 flac_fprintf(stderr, "%s: ERROR: file '%s' for '%s' tag value is too large\n", filename, field->field_value, field->field_name);
205 if(0 == (data = malloc(size+1)))
206 die("out of memory allocating tag value");
208 if(0 == (f = flac_fopen(field->field_value, "rb")) || fread(data, 1, size, f) != (size_t)size) {
209 flac_fprintf(stderr, "%s: ERROR: while reading file '%s' for '%s' tag value: %s\n", filename, field->field_value, field->field_name, strerror(errno));
216 if(strlen(data) != (size_t)size) {
218 flac_fprintf(stderr, "%s: ERROR: file '%s' for '%s' tag value has embedded NULs\n", filename, field->field_value, field->field_name);
222 /* move 'data' into 'converted', converting to UTF-8 if necessary */
226 else if(utf8_encode(data, &converted) >= 0) {
231 flac_fprintf(stderr, "%s: ERROR: converting file '%s' contents to UTF-8 for tag value\n", filename, field->field_value);
235 /* create and entry and append it */
236 if(!FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, field->field_name, converted)) {
238 flac_fprintf(stderr, "%s: ERROR: file '%s' for '%s' tag value is not valid UTF-8\n", filename, field->field_value, field->field_name);
242 if(!FLAC__metadata_object_vorbiscomment_append_comment(block, entry, /*copy=*/false)) {
243 flac_fprintf(stderr, "%s: ERROR: memory allocation failure\n", filename);
251 FLAC__bool needs_free = false;
252 entry.entry = (FLAC__byte *)field->field;
254 entry.entry = (FLAC__byte *)field->field;
256 else if(utf8_encode(field->field, &converted) >= 0) {
257 entry.entry = (FLAC__byte *)converted;
261 flac_fprintf(stderr, "%s: ERROR: converting comment '%s' to UTF-8\n", filename, field->field);
264 entry.length = strlen((const char *)entry.entry);
265 if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length)) {
269 * our previous parsing has already established that the field
270 * name is OK, so it must be the field value
272 flac_fprintf(stderr, "%s: ERROR: tag value for '%s' is not valid UTF-8\n", filename, field->field_name);
276 if(!FLAC__metadata_object_vorbiscomment_append_comment(block, entry, /*copy=*/true)) {
279 flac_fprintf(stderr, "%s: ERROR: memory allocation failure\n", filename);
290 FLAC__bool import_vc_from(const char *filename, FLAC__StreamMetadata *block, const Argument_String *vc_filename, FLAC__bool *needs_write, FLAC__bool raw)
296 if(0 == vc_filename->value || strlen(vc_filename->value) == 0) {
297 flac_fprintf(stderr, "%s: ERROR: empty import file name\n", filename);
300 if(0 == strcmp(vc_filename->value, "-"))
303 f = flac_fopen(vc_filename->value, "r");
306 flac_fprintf(stderr, "%s: ERROR: can't open import file %s: %s\n", filename, vc_filename->value, strerror(errno));
311 while(ret && !feof(f) && fgets(line, sizeof(line), f) != NULL) {
313 char *p = strchr(line, '\n');
315 flac_fprintf(stderr, "%s: ERROR: line too long, aborting\n", vc_filename->value);
319 const char *violation;
320 Argument_VcField field;
322 memset(&field, 0, sizeof(Argument_VcField));
323 field.field_value_from_file = false;
324 if(!parse_vorbis_comment_field(line, &field.field, &field.field_name, &field.field_value, &field.field_value_length, &violation)) {
325 FLAC__ASSERT(0 != violation);
326 flac_fprintf(stderr, "%s: ERROR: malformed vorbis comment field \"%s\",\n %s\n", vc_filename->value, line, violation);
330 ret = set_vc_field(filename, block, &field, needs_write, raw);
334 if(0 != field.field_name)
335 free(field.field_name);
336 if(0 != field.field_value)
337 free(field.field_value);
347 FLAC__bool export_vc_to(const char *filename, FLAC__StreamMetadata *block, const Argument_String *vc_filename, FLAC__bool raw)
352 if(0 == vc_filename->value || strlen(vc_filename->value) == 0) {
353 flac_fprintf(stderr, "%s: ERROR: empty export file name\n", filename);
356 if(0 == strcmp(vc_filename->value, "-"))
359 f = flac_fopen(vc_filename->value, "w");
362 flac_fprintf(stderr, "%s: ERROR: can't open export file %s: %s\n", filename, vc_filename->value, strerror(errno));
368 write_vc_fields(0, 0, block->data.vorbis_comment.comments, block->data.vorbis_comment.num_comments, raw, f);