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