1 /* flac - Command-line FLAC encoder/decoder
2 * Copyright (C) 2002-2009 Josh Coalson
3 * Copyright (C) 2011-2013 Xiph.Org Foundation
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.
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.
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.
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"
33 #include "share/compat.h"
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.
42 char *field; /* the whole field as passed on the command line, i.e. "NAME=VALUE" */
44 /* according to the vorbis spec, field values can contain \0 so simple C strings are not enough here */
45 unsigned field_value_length;
47 FLAC__bool field_value_from_file; /* true if field_value holds a filename for the value, false for plain value */
50 static void die(const char *message)
52 FLAC__ASSERT(0 != message);
53 fprintf(stderr, "ERROR: %s\n", message);
57 static char *local_strdup(const char *source)
60 FLAC__ASSERT(0 != source);
61 if(0 == (ret = strdup(source)))
62 die("out of memory during strdup()");
66 static FLAC__bool parse_vorbis_comment_field(const char *field_ref, char **field, char **name, char **value, unsigned *length, const char **violation)
68 static const char * const violations[] = {
69 "field name contains invalid character",
70 "field contains no '=' character"
76 *field = local_strdup(field_ref);
78 s = local_strdup(field_ref);
80 if(0 == (p = strchr(s, '='))) {
82 *violation = violations[1];
88 if(*q < 0x20 || *q > 0x7d || *q == 0x3d) {
90 *violation = violations[0];
95 *name = local_strdup(s);
96 *value = local_strdup(p);
103 /* slight modification: no 'filename' arg, and errors are passed back in 'violation' instead of printed to stderr */
104 static FLAC__bool set_vc_field(FLAC__StreamMetadata *block, const Argument_VcField *field, FLAC__bool *needs_write, FLAC__bool raw, const char **violation)
106 FLAC__StreamMetadata_VorbisComment_Entry entry;
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);
114 if(field->field_value_from_file) {
115 /* read the file into 'data' */
118 const FLAC__off_t size = grabbag__file_get_filesize(field->field_value);
120 *violation = "can't open file for tag value";
123 if(size >= 0x100000) { /* magic arbitrary limit, actual format limit is near 16MB */
124 *violation = "file for tag value is too large";
127 if(0 == (data = malloc(size+1)))
128 die("out of memory allocating tag value");
130 if(0 == (f = flac_fopen(field->field_value, "rb")) || fread(data, 1, size, f) != (size_t)size) {
134 *violation = "error while reading file for tag value";
138 if(strlen(data) != (size_t)size) {
140 *violation = "file for tag value has embedded NULs";
144 /* move 'data' into 'converted', converting to UTF-8 if necessary */
148 else if(utf8_encode(data, &converted) >= 0) {
153 *violation = "error converting file contents to UTF-8 for tag value";
157 /* create and entry and append it */
158 if(!FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, field->field_name, converted)) {
160 *violation = "file for tag value is not valid UTF-8";
164 if(!FLAC__metadata_object_vorbiscomment_append_comment(block, entry, /*copy=*/false)) {
165 *violation = "memory allocation failure";
173 FLAC__bool needs_free = false;
174 #ifdef _WIN32 /* everything in UTF-8 already. Must not alter */
175 entry.entry = (FLAC__byte *)field->field;
178 entry.entry = (FLAC__byte *)field->field;
180 else if(utf8_encode(field->field, &converted) >= 0) {
181 entry.entry = (FLAC__byte *)converted;
185 *violation = "error converting comment to UTF-8";
189 entry.length = strlen((const char *)entry.entry);
190 if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length)) {
194 * our previous parsing has already established that the field
195 * name is OK, so it must be the field value
197 *violation = "tag value for is not valid UTF-8";
201 if(!FLAC__metadata_object_vorbiscomment_append_comment(block, entry, /*copy=*/true)) {
204 *violation = "memory allocation failure";
216 * The rest of the code is novel
219 static void free_field(Argument_VcField *obj)
223 if(0 != obj->field_name)
224 free(obj->field_name);
225 if(0 != obj->field_value)
226 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)
231 Argument_VcField parsed;
234 FLAC__ASSERT(0 != block);
235 FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
236 FLAC__ASSERT(0 != comment);
238 memset(&parsed, 0, sizeof(parsed));
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)) {
246 if(!set_vc_field(block, &parsed, &dummy, raw, violation)) {