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