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