1 /* flac - Command-line FLAC encoder/decoder
2 * Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009 Josh Coalson
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.
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.
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.
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"
32 #include "share/compat.h"
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.
41 char *field; /* the whole field as passed on the command line, i.e. "NAME=VALUE" */
43 /* according to the vorbis spec, field values can contain \0 so simple C strings are not enough here */
44 unsigned field_value_length;
46 FLAC__bool field_value_from_file; /* true if field_value holds a filename for the value, false for plain value */
49 static void die(const char *message)
51 FLAC__ASSERT(0 != message);
52 fprintf(stderr, "ERROR: %s\n", message);
56 static char *local_strdup(const char *source)
59 FLAC__ASSERT(0 != source);
60 if(0 == (ret = strdup(source)))
61 die("out of memory during strdup()");
65 static FLAC__bool parse_vorbis_comment_field(const char *field_ref, char **field, char **name, char **value, unsigned *length, const char **violation)
67 static const char * const violations[] = {
68 "field name contains invalid character",
69 "field contains no '=' character"
75 *field = local_strdup(field_ref);
77 s = local_strdup(field_ref);
79 if(0 == (p = strchr(s, '='))) {
81 *violation = violations[1];
87 if(*q < 0x20 || *q > 0x7d || *q == 0x3d) {
89 *violation = violations[0];
94 *name = local_strdup(s);
95 *value = local_strdup(p);
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)
105 FLAC__StreamMetadata_VorbisComment_Entry entry;
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);
113 if(field->field_value_from_file) {
114 /* read the file into 'data' */
117 const FLAC__off_t size = grabbag__file_get_filesize(field->field_value);
119 *violation = "can't open file for tag value";
122 if(size >= 0x100000) { /* magic arbitrary limit, actual format limit is near 16MB */
123 *violation = "file for tag value is too large";
126 if(0 == (data = malloc(size+1)))
127 die("out of memory allocating tag value");
129 if(0 == (f = fopen(field->field_value, "rb")) || fread(data, 1, size, f) != (size_t)size) {
133 *violation = "error while reading file for tag value";
137 if(strlen(data) != (size_t)size) {
139 *violation = "file for tag value has embedded NULs";
143 /* move 'data' into 'converted', converting to UTF-8 if necessary */
147 else if(utf8_encode(data, &converted) >= 0) {
152 *violation = "error converting file contents to UTF-8 for tag value";
156 /* create and entry and append it */
157 if(!FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, field->field_name, converted)) {
159 *violation = "file for tag value is not valid UTF-8";
163 if(!FLAC__metadata_object_vorbiscomment_append_comment(block, entry, /*copy=*/false)) {
164 *violation = "memory allocation failure";
172 FLAC__bool needs_free = false;
174 entry.entry = (FLAC__byte *)field->field;
176 else if(utf8_encode(field->field, &converted) >= 0) {
177 entry.entry = (FLAC__byte *)converted;
181 *violation = "error converting comment to UTF-8";
184 entry.length = strlen((const char *)entry.entry);
185 if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length)) {
189 * our previous parsing has already established that the field
190 * name is OK, so it must be the field value
192 *violation = "tag value for is not valid UTF-8";
196 if(!FLAC__metadata_object_vorbiscomment_append_comment(block, entry, /*copy=*/true)) {
199 *violation = "memory allocation failure";
211 * The rest of the code is novel
214 static void free_field(Argument_VcField *obj)
218 if(0 != obj->field_name)
219 free(obj->field_name);
220 if(0 != obj->field_value)
221 free(obj->field_value);
224 FLAC__bool flac__vorbiscomment_add(FLAC__StreamMetadata *block, const char *comment, FLAC__bool value_from_file, FLAC__bool raw, const char **violation)
226 Argument_VcField parsed;
229 FLAC__ASSERT(0 != block);
230 FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
231 FLAC__ASSERT(0 != comment);
233 memset(&parsed, 0, sizeof(parsed));
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)) {
241 if(!set_vc_field(block, &parsed, &dummy, raw, violation)) {