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