1 /* metaflac - Command-line FLAC metadata editor
2 * Copyright (C) 2001,2002,2003,2004,2005,2006 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
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #include "FLAC/assert.h"
22 #include "share/grabbag.h" /* for grabbag__file_get_filesize() */
23 #include "share/utf8.h"
27 static FLAC__bool remove_vc_all(const char *filename, FLAC__StreamMetadata *block, FLAC__bool *needs_write);
28 static FLAC__bool remove_vc_field(const char *filename, FLAC__StreamMetadata *block, const char *field_name, FLAC__bool *needs_write);
29 static FLAC__bool remove_vc_firstfield(const char *filename, FLAC__StreamMetadata *block, const char *field_name, FLAC__bool *needs_write);
30 static FLAC__bool set_vc_field(const char *filename, FLAC__StreamMetadata *block, const Argument_VcField *field, FLAC__bool *needs_write, FLAC__bool raw);
31 static FLAC__bool import_vc_from(const char *filename, FLAC__StreamMetadata *block, const Argument_Filename *vc_filename, FLAC__bool *needs_write, FLAC__bool raw);
32 static FLAC__bool export_vc_to(const char *filename, FLAC__StreamMetadata *block, const Argument_Filename *vc_filename, FLAC__bool raw);
34 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)
36 FLAC__bool ok = true, found_vc_block = false;
37 FLAC__StreamMetadata *block = 0;
38 FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new();
41 die("out of memory allocating iterator");
43 FLAC__metadata_iterator_init(iterator, chain);
46 block = FLAC__metadata_iterator_get_block(iterator);
47 if(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT)
48 found_vc_block = true;
49 } while(!found_vc_block && FLAC__metadata_iterator_next(iterator));
52 /* create a new block if necessary */
53 if(operation->type == OP__SET_VC_FIELD || operation->type == OP__IMPORT_VC_FROM) {
54 block = FLAC__metadata_object_new(FLAC__METADATA_TYPE_VORBIS_COMMENT);
56 die("out of memory allocating VORBIS_COMMENT block");
57 while(FLAC__metadata_iterator_next(iterator))
59 if(!FLAC__metadata_iterator_insert_block_after(iterator, block)) {
60 print_error_with_chain_status(chain, "%s: ERROR: adding new VORBIS_COMMENT block to metadata", filename);
63 /* iterator is left pointing to new block */
64 FLAC__ASSERT(FLAC__metadata_iterator_get_block(iterator) == block);
67 FLAC__metadata_iterator_delete(iterator);
72 FLAC__ASSERT(0 != block);
73 FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
75 switch(operation->type) {
76 case OP__SHOW_VC_VENDOR:
77 write_vc_field(prefix_with_filename? filename : 0, &block->data.vorbis_comment.vendor_string, raw, stdout);
79 case OP__SHOW_VC_FIELD:
80 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);
82 case OP__REMOVE_VC_ALL:
83 ok = remove_vc_all(filename, block, needs_write);
85 case OP__REMOVE_VC_FIELD:
86 ok = remove_vc_field(filename, block, operation->argument.vc_field_name.value, needs_write);
88 case OP__REMOVE_VC_FIRSTFIELD:
89 ok = remove_vc_firstfield(filename, block, operation->argument.vc_field_name.value, needs_write);
91 case OP__SET_VC_FIELD:
92 ok = set_vc_field(filename, block, &operation->argument.vc_field, needs_write, raw);
94 case OP__IMPORT_VC_FROM:
95 ok = import_vc_from(filename, block, &operation->argument.filename, needs_write, raw);
97 case OP__EXPORT_VC_TO:
98 ok = export_vc_to(filename, block, &operation->argument.filename, raw);
106 FLAC__metadata_iterator_delete(iterator);
114 FLAC__bool remove_vc_all(const char *filename, FLAC__StreamMetadata *block, FLAC__bool *needs_write)
116 FLAC__ASSERT(0 != block);
117 FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
118 FLAC__ASSERT(0 != needs_write);
120 if(0 != block->data.vorbis_comment.comments) {
121 FLAC__ASSERT(block->data.vorbis_comment.num_comments > 0);
122 if(!FLAC__metadata_object_vorbiscomment_resize_comments(block, 0)) {
123 fprintf(stderr, "%s: ERROR: memory allocation failure\n", filename);
129 FLAC__ASSERT(block->data.vorbis_comment.num_comments == 0);
135 FLAC__bool remove_vc_field(const char *filename, FLAC__StreamMetadata *block, const char *field_name, FLAC__bool *needs_write)
139 FLAC__ASSERT(0 != needs_write);
141 n = FLAC__metadata_object_vorbiscomment_remove_entries_matching(block, field_name);
144 fprintf(stderr, "%s: ERROR: memory allocation failure\n", filename);
153 FLAC__bool remove_vc_firstfield(const char *filename, FLAC__StreamMetadata *block, const char *field_name, FLAC__bool *needs_write)
157 FLAC__ASSERT(0 != needs_write);
159 n = FLAC__metadata_object_vorbiscomment_remove_entry_matching(block, field_name);
162 fprintf(stderr, "%s: ERROR: memory allocation failure\n", filename);
171 FLAC__bool set_vc_field(const char *filename, FLAC__StreamMetadata *block, const Argument_VcField *field, FLAC__bool *needs_write, FLAC__bool raw)
173 FLAC__StreamMetadata_VorbisComment_Entry entry;
176 FLAC__ASSERT(0 != block);
177 FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
178 FLAC__ASSERT(0 != field);
179 FLAC__ASSERT(0 != needs_write);
181 if(field->field_value_from_file) {
182 /* read the file into 'data' */
185 const off_t size = grabbag__file_get_filesize(field->field_value);
187 fprintf(stderr, "%s: ERROR: can't open file '%s' for '%s' tag value\n", filename, field->field_value, field->field_name);
190 if(size >= 0x100000) { /* magic arbitrary limit, actual format limit is near 16MB */
191 fprintf(stderr, "%s: ERROR: file '%s' for '%s' tag value is too large\n", filename, field->field_value, field->field_name);
194 if(0 == (data = malloc(size+1)))
195 die("out of memory allocating tag value");
197 if(0 == (f = fopen(field->field_value, "rb")) || fread(data, 1, size, f) != (size_t)size) {
201 fprintf(stderr, "%s: ERROR: while reading file '%s' for '%s' tag value\n", filename, field->field_value, field->field_name);
205 if(strlen(data) != (size_t)size) {
207 fprintf(stderr, "%s: ERROR: file '%s' for '%s' tag value has embedded NULs\n", filename, field->field_value, field->field_name);
211 /* move 'data' into 'converted', converting to UTF-8 if necessary */
215 else if(utf8_encode(data, &converted) >= 0) {
220 fprintf(stderr, "%s: ERROR: converting file '%s' contents to UTF-8 for tag value\n", filename, field->field_value);
224 /* create and entry and append it */
225 if(!FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, field->field_name, converted)) {
227 fprintf(stderr, "%s: ERROR: file '%s' for '%s' tag value is not valid UTF-8\n", filename, field->field_value, field->field_name);
231 if(!FLAC__metadata_object_vorbiscomment_append_comment(block, entry, /*copy=*/false)) {
232 fprintf(stderr, "%s: ERROR: memory allocation failure\n", filename);
240 FLAC__bool needs_free = false;
242 entry.entry = (FLAC__byte *)field->field;
244 else if(utf8_encode(field->field, &converted) >= 0) {
245 entry.entry = (FLAC__byte *)converted;
249 fprintf(stderr, "%s: ERROR: converting comment '%s' to UTF-8\n", filename, field->field);
252 entry.length = strlen((const char *)entry.entry);
253 if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length)) {
257 * our previous parsing has already established that the field
258 * name is OK, so it must be the field value
260 fprintf(stderr, "%s: ERROR: tag value for '%s' is not valid UTF-8\n", filename, field->field_name);
264 if(!FLAC__metadata_object_vorbiscomment_append_comment(block, entry, /*copy=*/true)) {
267 fprintf(stderr, "%s: ERROR: memory allocation failure\n", filename);
278 FLAC__bool import_vc_from(const char *filename, FLAC__StreamMetadata *block, const Argument_Filename *vc_filename, FLAC__bool *needs_write, FLAC__bool raw)
284 if(0 == vc_filename->value || strlen(vc_filename->value) == 0) {
285 fprintf(stderr, "%s: ERROR: empty import file name\n", filename);
288 if(0 == strcmp(vc_filename->value, "-"))
291 f = fopen(vc_filename->value, "r");
294 fprintf(stderr, "%s: ERROR: can't open import file %s\n", filename, vc_filename->value);
299 while(ret && !feof(f)) {
300 fgets(line, sizeof(line), f);
302 char *p = strchr(line, '\n');
304 fprintf(stderr, "%s: ERROR: line too long, aborting\n", vc_filename->value);
308 const char *violation;
309 Argument_VcField field;
311 memset(&field, 0, sizeof(Argument_VcField));
312 field.field_value_from_file = false;
313 if(!parse_vorbis_comment_field(line, &field.field, &field.field_name, &field.field_value, &field.field_value_length, &violation)) {
314 FLAC__ASSERT(0 != violation);
315 fprintf(stderr, "%s: ERROR: malformed vorbis comment field \"%s\",\n %s\n", vc_filename->value, line, violation);
319 ret = set_vc_field(filename, block, &field, needs_write, raw);
323 if(0 != field.field_name)
324 free(field.field_name);
325 if(0 != field.field_value)
326 free(field.field_value);
336 FLAC__bool export_vc_to(const char *filename, FLAC__StreamMetadata *block, const Argument_Filename *vc_filename, FLAC__bool raw)
341 if(0 == vc_filename->value || strlen(vc_filename->value) == 0) {
342 fprintf(stderr, "%s: ERROR: empty export file name\n", filename);
345 if(0 == strcmp(vc_filename->value, "-"))
348 f = fopen(vc_filename->value, "w");
351 fprintf(stderr, "%s: ERROR: can't open export file %s\n", filename, vc_filename->value);
357 write_vc_fields(0, 0, block->data.vorbis_comment.comments, block->data.vorbis_comment.num_comments, raw, f);