vorbiscomment.c 6.87 KB
Newer Older
Josh Coalson's avatar
Josh Coalson committed
1
/* flac - Command-line FLAC encoder/decoder
2
 * Copyright (C) 2002-2009  Josh Coalson
3
 * Copyright (C) 2011-2014  Xiph.Org Foundation
Josh Coalson's avatar
Josh Coalson committed
4
5
6
7
8
9
10
11
12
13
14
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
Miroslav Lichvar's avatar
Miroslav Lichvar committed
15
16
17
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Josh Coalson's avatar
Josh Coalson committed
18
19
 */

20
#ifdef HAVE_CONFIG_H
Josh Coalson's avatar
Josh Coalson committed
21
22
23
#  include <config.h>
#endif

Josh Coalson's avatar
Josh Coalson committed
24
#include "vorbiscomment.h"
Josh Coalson's avatar
Josh Coalson committed
25
26
#include "FLAC/assert.h"
#include "FLAC/metadata.h"
27
#include "share/grabbag.h" /* for grabbag__file_get_filesize() */
Josh Coalson's avatar
Josh Coalson committed
28
29
30
31
32
#include "share/utf8.h"
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
33
#include "share/compat.h"
Josh Coalson's avatar
Josh Coalson committed
34
35
36
37


/*
 * This struct and the following 4 static functions are copied from
38
 * ../metaflac/.  Maybe someday there will be a convenience
Josh Coalson's avatar
Josh Coalson committed
39
40
41
42
43
44
45
46
 * library for Vorbis comment parsing.
 */
typedef struct {
	char *field; /* the whole field as passed on the command line, i.e. "NAME=VALUE" */
	char *field_name;
	/* according to the vorbis spec, field values can contain \0 so simple C strings are not enough here */
	unsigned field_value_length;
	char *field_value;
47
	FLAC__bool field_value_from_file; /* true if field_value holds a filename for the value, false for plain value */
Josh Coalson's avatar
Josh Coalson committed
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
} Argument_VcField;

static void die(const char *message)
{
	FLAC__ASSERT(0 != message);
	fprintf(stderr, "ERROR: %s\n", message);
	exit(1);
}

static char *local_strdup(const char *source)
{
	char *ret;
	FLAC__ASSERT(0 != source);
	if(0 == (ret = strdup(source)))
		die("out of memory during strdup()");
	return ret;
}

static FLAC__bool parse_vorbis_comment_field(const char *field_ref, char **field, char **name, char **value, unsigned *length, const char **violation)
{
	static const char * const violations[] = {
		"field name contains invalid character",
		"field contains no '=' character"
	};

	char *p, *q, *s;

	if(0 != field)
		*field = local_strdup(field_ref);

	s = local_strdup(field_ref);

	if(0 == (p = strchr(s, '='))) {
		free(s);
		*violation = violations[1];
		return false;
	}
	*p++ = '\0';

	for(q = s; *q; q++) {
		if(*q < 0x20 || *q > 0x7d || *q == 0x3d) {
			free(s);
			*violation = violations[0];
			return false;
		}
	}

	*name = local_strdup(s);
	*value = local_strdup(p);
	*length = strlen(p);

	free(s);
	return true;
}

/* slight modification: no 'filename' arg, and errors are passed back in 'violation' instead of printed to stderr */
static FLAC__bool set_vc_field(FLAC__StreamMetadata *block, const Argument_VcField *field, FLAC__bool *needs_write, FLAC__bool raw, const char **violation)
{
	FLAC__StreamMetadata_VorbisComment_Entry entry;
	char *converted;

	FLAC__ASSERT(0 != block);
	FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
	FLAC__ASSERT(0 != field);
	FLAC__ASSERT(0 != needs_write);

114
115
116
117
	if(field->field_value_from_file) {
		/* read the file into 'data' */
		FILE *f = 0;
		char *data = 0;
118
		const FLAC__off_t size = grabbag__file_get_filesize(field->field_value);
119
120
121
122
123
124
125
126
127
128
129
		if(size < 0) {
			*violation = "can't open file for tag value";
			return false;
		}
		if(size >= 0x100000) { /* magic arbitrary limit, actual format limit is near 16MB */
			*violation = "file for tag value is too large";
			return false;
		}
		if(0 == (data = malloc(size+1)))
			die("out of memory allocating tag value");
		data[size] = '\0';
130
		if(0 == (f = flac_fopen(field->field_value, "rb")) || fread(data, 1, size, f) != (size_t)size) {
131
132
133
134
135
136
137
138
139
140
141
142
			free(data);
			if(f)
				fclose(f);
			*violation = "error while reading file for tag value";
			return false;
		}
		fclose(f);
		if(strlen(data) != (size_t)size) {
			free(data);
			*violation = "file for tag value has embedded NULs";
			return false;
		}
Josh Coalson's avatar
Josh Coalson committed
143

144
145
146
147
148
149
150
151
152
153
154
155
		/* move 'data' into 'converted', converting to UTF-8 if necessary */
		if(raw) {
			converted = data;
		}
		else if(utf8_encode(data, &converted) >= 0) {
			free(data);
		}
		else {
			free(data);
			*violation = "error converting file contents to UTF-8 for tag value";
			return false;
		}
Josh Coalson's avatar
Josh Coalson committed
156

157
158
		/* create and entry and append it */
		if(!FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, field->field_name, converted)) {
Josh Coalson's avatar
Josh Coalson committed
159
			free(converted);
160
161
162
163
164
165
166
167
168
169
170
			*violation = "file for tag value is not valid UTF-8";
			return false;
		}
		free(converted);
		if(!FLAC__metadata_object_vorbiscomment_append_comment(block, entry, /*copy=*/false)) {
			*violation = "memory allocation failure";
			return false;
		}

		*needs_write = true;
		return true;
Josh Coalson's avatar
Josh Coalson committed
171
172
	}
	else {
173
		FLAC__bool needs_free = false;
174
#ifdef _WIN32 /* everything in UTF-8 already. Must not alter */
175
176
		entry.entry = (FLAC__byte *)field->field;
#else
177
178
179
180
181
182
183
184
185
186
187
		if(raw) {
			entry.entry = (FLAC__byte *)field->field;
		}
		else if(utf8_encode(field->field, &converted) >= 0) {
			entry.entry = (FLAC__byte *)converted;
			needs_free = true;
		}
		else {
			*violation = "error converting comment to UTF-8";
			return false;
		}
188
#endif
189
190
191
192
193
194
195
196
		entry.length = strlen((const char *)entry.entry);
		if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length)) {
			if(needs_free)
				free(converted);
			/*
			 * our previous parsing has already established that the field
			 * name is OK, so it must be the field value
			 */
197
			*violation = "tag value is not valid UTF-8";
198
199
200
201
202
203
204
205
206
207
			return false;
		}

		if(!FLAC__metadata_object_vorbiscomment_append_comment(block, entry, /*copy=*/true)) {
			if(needs_free)
				free(converted);
			*violation = "memory allocation failure";
			return false;
		}

Josh Coalson's avatar
Josh Coalson committed
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
		*needs_write = true;
		if(needs_free)
			free(converted);
		return true;
	}
}

/*
 * The rest of the code is novel
 */

static void free_field(Argument_VcField *obj)
{
	if(0 != obj->field)
		free(obj->field);
	if(0 != obj->field_name)
		free(obj->field_name);
	if(0 != obj->field_value)
		free(obj->field_value);
}

229
FLAC__bool flac__vorbiscomment_add(FLAC__StreamMetadata *block, const char *comment, FLAC__bool value_from_file, FLAC__bool raw, const char **violation)
Josh Coalson's avatar
Josh Coalson committed
230
231
232
233
234
235
236
237
238
239
{
	Argument_VcField parsed;
	FLAC__bool dummy;

	FLAC__ASSERT(0 != block);
	FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
	FLAC__ASSERT(0 != comment);

	memset(&parsed, 0, sizeof(parsed));

240
	parsed.field_value_from_file = value_from_file;
Josh Coalson's avatar
Josh Coalson committed
241
242
243
244
245
	if(!parse_vorbis_comment_field(comment, &(parsed.field), &(parsed.field_name), &(parsed.field_value), &(parsed.field_value_length), violation)) {
		free_field(&parsed);
		return false;
	}

246
	if(parsed.field_value_length > 0 && !set_vc_field(block, &parsed, &dummy, raw, violation)) {
Josh Coalson's avatar
Josh Coalson committed
247
248
249
250
251
252
253
254
		free_field(&parsed);
		return false;
	}
	else {
		free_field(&parsed);
		return true;
	}
}