Fix a couple of Windows 2Gig file size issues.
[platform/upstream/flac.git] / src / flac / vorbiscomment.c
1 /* flac - Command-line FLAC encoder/decoder
2  * Copyright (C) 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 "vorbiscomment.h"
24 #include "FLAC/assert.h"
25 #include "FLAC/metadata.h"
26 #include "share/grabbag.h" /* for grabbag__file_get_filesize() */
27 #include "share/utf8.h"
28 #include <ctype.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include "share/compat.h"
33
34
35 /*
36  * This struct and the following 4 static functions are copied from
37  * ../metaflac/.  Maybe someday there will be a convenience
38  * library for Vorbis comment parsing.
39  */
40 typedef struct {
41         char *field; /* the whole field as passed on the command line, i.e. "NAME=VALUE" */
42         char *field_name;
43         /* according to the vorbis spec, field values can contain \0 so simple C strings are not enough here */
44         unsigned field_value_length;
45         char *field_value;
46         FLAC__bool field_value_from_file; /* true if field_value holds a filename for the value, false for plain value */
47 } Argument_VcField;
48
49 static void die(const char *message)
50 {
51         FLAC__ASSERT(0 != message);
52         fprintf(stderr, "ERROR: %s\n", message);
53         exit(1);
54 }
55
56 static char *local_strdup(const char *source)
57 {
58         char *ret;
59         FLAC__ASSERT(0 != source);
60         if(0 == (ret = strdup(source)))
61                 die("out of memory during strdup()");
62         return ret;
63 }
64
65 static FLAC__bool parse_vorbis_comment_field(const char *field_ref, char **field, char **name, char **value, unsigned *length, const char **violation)
66 {
67         static const char * const violations[] = {
68                 "field name contains invalid character",
69                 "field contains no '=' character"
70         };
71
72         char *p, *q, *s;
73
74         if(0 != field)
75                 *field = local_strdup(field_ref);
76
77         s = local_strdup(field_ref);
78
79         if(0 == (p = strchr(s, '='))) {
80                 free(s);
81                 *violation = violations[1];
82                 return false;
83         }
84         *p++ = '\0';
85
86         for(q = s; *q; q++) {
87                 if(*q < 0x20 || *q > 0x7d || *q == 0x3d) {
88                         free(s);
89                         *violation = violations[0];
90                         return false;
91                 }
92         }
93
94         *name = local_strdup(s);
95         *value = local_strdup(p);
96         *length = strlen(p);
97
98         free(s);
99         return true;
100 }
101
102 /* slight modification: no 'filename' arg, and errors are passed back in 'violation' instead of printed to stderr */
103 static FLAC__bool set_vc_field(FLAC__StreamMetadata *block, const Argument_VcField *field, FLAC__bool *needs_write, FLAC__bool raw, const char **violation)
104 {
105         FLAC__StreamMetadata_VorbisComment_Entry entry;
106         char *converted;
107
108         FLAC__ASSERT(0 != block);
109         FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
110         FLAC__ASSERT(0 != field);
111         FLAC__ASSERT(0 != needs_write);
112
113         if(field->field_value_from_file) {
114                 /* read the file into 'data' */
115                 FILE *f = 0;
116                 char *data = 0;
117                 const FLAC__off_t size = grabbag__file_get_filesize(field->field_value);
118                 if(size < 0) {
119                         *violation = "can't open file for tag value";
120                         return false;
121                 }
122                 if(size >= 0x100000) { /* magic arbitrary limit, actual format limit is near 16MB */
123                         *violation = "file for tag value is too large";
124                         return false;
125                 }
126                 if(0 == (data = malloc(size+1)))
127                         die("out of memory allocating tag value");
128                 data[size] = '\0';
129                 if(0 == (f = fopen(field->field_value, "rb")) || fread(data, 1, size, f) != (size_t)size) {
130                         free(data);
131                         if(f)
132                                 fclose(f);
133                         *violation = "error while reading file for tag value";
134                         return false;
135                 }
136                 fclose(f);
137                 if(strlen(data) != (size_t)size) {
138                         free(data);
139                         *violation = "file for tag value has embedded NULs";
140                         return false;
141                 }
142
143                 /* move 'data' into 'converted', converting to UTF-8 if necessary */
144                 if(raw) {
145                         converted = data;
146                 }
147                 else if(utf8_encode(data, &converted) >= 0) {
148                         free(data);
149                 }
150                 else {
151                         free(data);
152                         *violation = "error converting file contents to UTF-8 for tag value";
153                         return false;
154                 }
155
156                 /* create and entry and append it */
157                 if(!FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, field->field_name, converted)) {
158                         free(converted);
159                         *violation = "file for tag value is not valid UTF-8";
160                         return false;
161                 }
162                 free(converted);
163                 if(!FLAC__metadata_object_vorbiscomment_append_comment(block, entry, /*copy=*/false)) {
164                         *violation = "memory allocation failure";
165                         return false;
166                 }
167
168                 *needs_write = true;
169                 return true;
170         }
171         else {
172                 FLAC__bool needs_free = false;
173                 if(raw) {
174                         entry.entry = (FLAC__byte *)field->field;
175                 }
176                 else if(utf8_encode(field->field, &converted) >= 0) {
177                         entry.entry = (FLAC__byte *)converted;
178                         needs_free = true;
179                 }
180                 else {
181                         *violation = "error converting comment to UTF-8";
182                         return false;
183                 }
184                 entry.length = strlen((const char *)entry.entry);
185                 if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length)) {
186                         if(needs_free)
187                                 free(converted);
188                         /*
189                          * our previous parsing has already established that the field
190                          * name is OK, so it must be the field value
191                          */
192                         *violation = "tag value for is not valid UTF-8";
193                         return false;
194                 }
195
196                 if(!FLAC__metadata_object_vorbiscomment_append_comment(block, entry, /*copy=*/true)) {
197                         if(needs_free)
198                                 free(converted);
199                         *violation = "memory allocation failure";
200                         return false;
201                 }
202
203                 *needs_write = true;
204                 if(needs_free)
205                         free(converted);
206                 return true;
207         }
208 }
209
210 /*
211  * The rest of the code is novel
212  */
213
214 static void free_field(Argument_VcField *obj)
215 {
216         if(0 != obj->field)
217                 free(obj->field);
218         if(0 != obj->field_name)
219                 free(obj->field_name);
220         if(0 != obj->field_value)
221                 free(obj->field_value);
222 }
223
224 FLAC__bool flac__vorbiscomment_add(FLAC__StreamMetadata *block, const char *comment, FLAC__bool value_from_file, FLAC__bool raw, const char **violation)
225 {
226         Argument_VcField parsed;
227         FLAC__bool dummy;
228
229         FLAC__ASSERT(0 != block);
230         FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
231         FLAC__ASSERT(0 != comment);
232
233         memset(&parsed, 0, sizeof(parsed));
234
235         parsed.field_value_from_file = value_from_file;
236         if(!parse_vorbis_comment_field(comment, &(parsed.field), &(parsed.field_name), &(parsed.field_value), &(parsed.field_value_length), violation)) {
237                 free_field(&parsed);
238                 return false;
239         }
240
241         if(!set_vc_field(block, &parsed, &dummy, raw, violation)) {
242                 free_field(&parsed);
243                 return false;
244         }
245         else {
246                 free_field(&parsed);
247                 return true;
248         }
249 }