more fixes for ogg support
[flac.git] / src / metaflac / operations_shorthand_vorbiscomment.c
1 /* metaflac - Command-line FLAC metadata editor
2  * Copyright (C) 2001,2002,2003,2004,2005,2006  Josh Coalson
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #if HAVE_CONFIG_H
20 #  include <config.h>
21 #endif
22
23 #include "options.h"
24 #include "utils.h"
25 #include "FLAC/assert.h"
26 #include "share/grabbag.h" /* for grabbag__file_get_filesize() */
27 #include "share/utf8.h"
28 #include <errno.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 static FLAC__bool remove_vc_all(const char *filename, FLAC__StreamMetadata *block, FLAC__bool *needs_write);
33 static FLAC__bool remove_vc_field(const char *filename, FLAC__StreamMetadata *block, const char *field_name, FLAC__bool *needs_write);
34 static FLAC__bool remove_vc_firstfield(const char *filename, FLAC__StreamMetadata *block, const char *field_name, FLAC__bool *needs_write);
35 static FLAC__bool set_vc_field(const char *filename, FLAC__StreamMetadata *block, const Argument_VcField *field, FLAC__bool *needs_write, FLAC__bool raw);
36 static FLAC__bool import_vc_from(const char *filename, FLAC__StreamMetadata *block, const Argument_String *vc_filename, FLAC__bool *needs_write, FLAC__bool raw);
37 static FLAC__bool export_vc_to(const char *filename, FLAC__StreamMetadata *block, const Argument_String *vc_filename, FLAC__bool raw);
38
39 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)
40 {
41         FLAC__bool ok = true, found_vc_block = false;
42         FLAC__StreamMetadata *block = 0;
43         FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new();
44
45         if(0 == iterator)
46                 die("out of memory allocating iterator");
47
48         FLAC__metadata_iterator_init(iterator, chain);
49
50         do {
51                 block = FLAC__metadata_iterator_get_block(iterator);
52                 if(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT)
53                         found_vc_block = true;
54         } while(!found_vc_block && FLAC__metadata_iterator_next(iterator));
55
56         if(!found_vc_block) {
57                 /* create a new block if necessary */
58                 if(operation->type == OP__SET_VC_FIELD || operation->type == OP__IMPORT_VC_FROM) {
59                         block = FLAC__metadata_object_new(FLAC__METADATA_TYPE_VORBIS_COMMENT);
60                         if(0 == block)
61                                 die("out of memory allocating VORBIS_COMMENT block");
62                         while(FLAC__metadata_iterator_next(iterator))
63                                 ;
64                         if(!FLAC__metadata_iterator_insert_block_after(iterator, block)) {
65                                 print_error_with_chain_status(chain, "%s: ERROR: adding new VORBIS_COMMENT block to metadata", filename);
66                                 return false;
67                         }
68                         /* iterator is left pointing to new block */
69                         FLAC__ASSERT(FLAC__metadata_iterator_get_block(iterator) == block);
70                 }
71                 else {
72                         FLAC__metadata_iterator_delete(iterator);
73                         return ok;
74                 }
75         }
76
77         FLAC__ASSERT(0 != block);
78         FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
79
80         switch(operation->type) {
81                 case OP__SHOW_VC_VENDOR:
82                         write_vc_field(prefix_with_filename? filename : 0, &block->data.vorbis_comment.vendor_string, raw, stdout);
83                         break;
84                 case OP__SHOW_VC_FIELD:
85                         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);
86                         break;
87                 case OP__REMOVE_VC_ALL:
88                         ok = remove_vc_all(filename, block, needs_write);
89                         break;
90                 case OP__REMOVE_VC_FIELD:
91                         ok = remove_vc_field(filename, block, operation->argument.vc_field_name.value, needs_write);
92                         break;
93                 case OP__REMOVE_VC_FIRSTFIELD:
94                         ok = remove_vc_firstfield(filename, block, operation->argument.vc_field_name.value, needs_write);
95                         break;
96                 case OP__SET_VC_FIELD:
97                         ok = set_vc_field(filename, block, &operation->argument.vc_field, needs_write, raw);
98                         break;
99                 case OP__IMPORT_VC_FROM:
100                         ok = import_vc_from(filename, block, &operation->argument.filename, needs_write, raw);
101                         break;
102                 case OP__EXPORT_VC_TO:
103                         ok = export_vc_to(filename, block, &operation->argument.filename, raw);
104                         break;
105                 default:
106                         ok = false;
107                         FLAC__ASSERT(0);
108                         break;
109         };
110
111         FLAC__metadata_iterator_delete(iterator);
112         return ok;
113 }
114
115 /*
116  * local routines
117  */
118
119 FLAC__bool remove_vc_all(const char *filename, FLAC__StreamMetadata *block, FLAC__bool *needs_write)
120 {
121         FLAC__ASSERT(0 != block);
122         FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
123         FLAC__ASSERT(0 != needs_write);
124
125         if(0 != block->data.vorbis_comment.comments) {
126                 FLAC__ASSERT(block->data.vorbis_comment.num_comments > 0);
127                 if(!FLAC__metadata_object_vorbiscomment_resize_comments(block, 0)) {
128                         fprintf(stderr, "%s: ERROR: memory allocation failure\n", filename);
129                         return false;
130                 }
131                 *needs_write = true;
132         }
133         else {
134                 FLAC__ASSERT(block->data.vorbis_comment.num_comments == 0);
135         }
136
137         return true;
138 }
139
140 FLAC__bool remove_vc_field(const char *filename, FLAC__StreamMetadata *block, const char *field_name, FLAC__bool *needs_write)
141 {
142         int n;
143
144         FLAC__ASSERT(0 != needs_write);
145
146         n = FLAC__metadata_object_vorbiscomment_remove_entries_matching(block, field_name);
147
148         if(n < 0) {
149                 fprintf(stderr, "%s: ERROR: memory allocation failure\n", filename);
150                 return false;
151         }
152         else if(n > 0)
153                 *needs_write = true;
154
155         return true;
156 }
157
158 FLAC__bool remove_vc_firstfield(const char *filename, FLAC__StreamMetadata *block, const char *field_name, FLAC__bool *needs_write)
159 {
160         int n;
161
162         FLAC__ASSERT(0 != needs_write);
163
164         n = FLAC__metadata_object_vorbiscomment_remove_entry_matching(block, field_name);
165
166         if(n < 0) {
167                 fprintf(stderr, "%s: ERROR: memory allocation failure\n", filename);
168                 return false;
169         }
170         else if(n > 0)
171                 *needs_write = true;
172
173         return true;
174 }
175
176 FLAC__bool set_vc_field(const char *filename, FLAC__StreamMetadata *block, const Argument_VcField *field, FLAC__bool *needs_write, FLAC__bool raw)
177 {
178         FLAC__StreamMetadata_VorbisComment_Entry entry;
179         char *converted;
180
181         FLAC__ASSERT(0 != block);
182         FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
183         FLAC__ASSERT(0 != field);
184         FLAC__ASSERT(0 != needs_write);
185
186         if(field->field_value_from_file) {
187                 /* read the file into 'data' */
188                 FILE *f = 0;
189                 char *data = 0;
190                 const off_t size = grabbag__file_get_filesize(field->field_value);
191                 if(size < 0) {
192                         fprintf(stderr, "%s: ERROR: can't open file '%s' for '%s' tag value\n", filename, field->field_value, field->field_name);
193                         return false;
194                 }
195                 if(size >= 0x100000) { /* magic arbitrary limit, actual format limit is near 16MB */
196                         fprintf(stderr, "%s: ERROR: file '%s' for '%s' tag value is too large\n", filename, field->field_value, field->field_name);
197                         return false;
198                 }
199                 if(0 == (data = malloc(size+1)))
200                         die("out of memory allocating tag value");
201                 data[size] = '\0';
202                 if(0 == (f = fopen(field->field_value, "rb")) || fread(data, 1, size, f) != (size_t)size) {
203                         fprintf(stderr, "%s: ERROR: while reading file '%s' for '%s' tag value: %s\n", filename, field->field_value, field->field_name, strerror(errno));
204                         free(data);
205                         if(f)
206                                 fclose(f);
207                         return false;
208                 }
209                 fclose(f);
210                 if(strlen(data) != (size_t)size) {
211                         free(data);
212                         fprintf(stderr, "%s: ERROR: file '%s' for '%s' tag value has embedded NULs\n", filename, field->field_value, field->field_name);
213                         return false;
214                 }
215
216                 /* move 'data' into 'converted', converting to UTF-8 if necessary */
217                 if(raw) {
218                         converted = data;
219                 }
220                 else if(utf8_encode(data, &converted) >= 0) {
221                         free(data);
222                 }
223                 else {
224                         free(data);
225                         fprintf(stderr, "%s: ERROR: converting file '%s' contents to UTF-8 for tag value\n", filename, field->field_value);
226                         return false;
227                 }
228
229                 /* create and entry and append it */
230                 if(!FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, field->field_name, converted)) {
231                         free(converted);
232                         fprintf(stderr, "%s: ERROR: file '%s' for '%s' tag value is not valid UTF-8\n", filename, field->field_value, field->field_name);
233                         return false;
234                 }
235                 free(converted);
236                 if(!FLAC__metadata_object_vorbiscomment_append_comment(block, entry, /*copy=*/false)) {
237                         fprintf(stderr, "%s: ERROR: memory allocation failure\n", filename);
238                         return false;
239                 }
240
241                 *needs_write = true;
242                 return true;
243         }
244         else {
245                 FLAC__bool needs_free = false;
246                 if(raw) {
247                         entry.entry = (FLAC__byte *)field->field;
248                 }
249                 else if(utf8_encode(field->field, &converted) >= 0) {
250                         entry.entry = (FLAC__byte *)converted;
251                         needs_free = true;
252                 }
253                 else {
254                         fprintf(stderr, "%s: ERROR: converting comment '%s' to UTF-8\n", filename, field->field);
255                         return false;
256                 }
257                 entry.length = strlen((const char *)entry.entry);
258                 if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length)) {
259                         if(needs_free)
260                                 free(converted);
261                         /*
262                          * our previous parsing has already established that the field
263                          * name is OK, so it must be the field value
264                          */
265                         fprintf(stderr, "%s: ERROR: tag value for '%s' is not valid UTF-8\n", filename, field->field_name);
266                         return false;
267                 }
268
269                 if(!FLAC__metadata_object_vorbiscomment_append_comment(block, entry, /*copy=*/true)) {
270                         if(needs_free)
271                                 free(converted);
272                         fprintf(stderr, "%s: ERROR: memory allocation failure\n", filename);
273                         return false;
274                 }
275
276                 *needs_write = true;
277                 if(needs_free)
278                         free(converted);
279                 return true;
280         }
281 }
282
283 FLAC__bool import_vc_from(const char *filename, FLAC__StreamMetadata *block, const Argument_String *vc_filename, FLAC__bool *needs_write, FLAC__bool raw)
284 {
285         FILE *f;
286         char line[65536];
287         FLAC__bool ret;
288
289         if(0 == vc_filename->value || strlen(vc_filename->value) == 0) {
290                 fprintf(stderr, "%s: ERROR: empty import file name\n", filename);
291                 return false;
292         }
293         if(0 == strcmp(vc_filename->value, "-"))
294                 f = stdin;
295         else
296                 f = fopen(vc_filename->value, "r");
297
298         if(0 == f) {
299                 fprintf(stderr, "%s: ERROR: can't open import file %s: %s\n", filename, vc_filename->value, strerror(errno));
300                 return false;
301         }
302
303         ret = true;
304         while(ret && !feof(f)) {
305                 fgets(line, sizeof(line), f);
306                 if(!feof(f)) {
307                         char *p = strchr(line, '\n');
308                         if(0 == p) {
309                                 fprintf(stderr, "%s: ERROR: line too long, aborting\n", vc_filename->value);
310                                 ret = false;
311                         }
312                         else {
313                                 const char *violation;
314                                 Argument_VcField field;
315                                 *p = '\0';
316                                 memset(&field, 0, sizeof(Argument_VcField));
317                                 field.field_value_from_file = false;
318                                 if(!parse_vorbis_comment_field(line, &field.field, &field.field_name, &field.field_value, &field.field_value_length, &violation)) {
319                                         FLAC__ASSERT(0 != violation);
320                                         fprintf(stderr, "%s: ERROR: malformed vorbis comment field \"%s\",\n       %s\n", vc_filename->value, line, violation);
321                                         ret = false;
322                                 }
323                                 else {
324                                         ret = set_vc_field(filename, block, &field, needs_write, raw);
325                                 }
326                                 if(0 != field.field)
327                                         free(field.field);
328                                 if(0 != field.field_name)
329                                         free(field.field_name);
330                                 if(0 != field.field_value)
331                                         free(field.field_value);
332                         }
333                 }
334         };
335
336         if(f != stdin)
337                 fclose(f);
338         return ret;
339 }
340
341 FLAC__bool export_vc_to(const char *filename, FLAC__StreamMetadata *block, const Argument_String *vc_filename, FLAC__bool raw)
342 {
343         FILE *f;
344         FLAC__bool ret;
345
346         if(0 == vc_filename->value || strlen(vc_filename->value) == 0) {
347                 fprintf(stderr, "%s: ERROR: empty export file name\n", filename);
348                 return false;
349         }
350         if(0 == strcmp(vc_filename->value, "-"))
351                 f = stdout;
352         else
353                 f = fopen(vc_filename->value, "w");
354
355         if(0 == f) {
356                 fprintf(stderr, "%s: ERROR: can't open export file %s: %s\n", filename, vc_filename->value, strerror(errno));
357                 return false;
358         }
359
360         ret = true;
361
362         write_vc_fields(0, 0, block->data.vorbis_comment.comments, block->data.vorbis_comment.num_comments, raw, f);
363
364         if(f != stdout)
365                 fclose(f);
366         return ret;
367 }