1 /* flac - Command-line FLAC encoder/decoder
2 * Copyright (C) 2002,2003,2004,2005,2006,2007 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
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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"
35 * This struct and the following 4 static functions are copied from
36 * ../metaflac/. Maybe someday there will be a convenience
37 * library for Vorbis comment parsing.
40 char *field; /* the whole field as passed on the command line, i.e. "NAME=VALUE" */
42 /* according to the vorbis spec, field values can contain \0 so simple C strings are not enough here */
43 unsigned field_value_length;
45 FLAC__bool field_value_from_file; /* true if field_value holds a filename for the value, false for plain value */
48 static void die(const char *message)
50 FLAC__ASSERT(0 != message);
51 fprintf(stderr, "ERROR: %s\n", message);
55 static char *local_strdup(const char *source)
58 FLAC__ASSERT(0 != source);
59 if(0 == (ret = strdup(source)))
60 die("out of memory during strdup()");
64 static FLAC__bool parse_vorbis_comment_field(const char *field_ref, char **field, char **name, char **value, unsigned *length, const char **violation)
66 static const char * const violations[] = {
67 "field name contains invalid character",
68 "field contains no '=' character"
74 *field = local_strdup(field_ref);
76 s = local_strdup(field_ref);
78 if(0 == (p = strchr(s, '='))) {
80 *violation = violations[1];
86 if(*q < 0x20 || *q > 0x7d || *q == 0x3d) {
88 *violation = violations[0];
93 *name = local_strdup(s);
94 *value = local_strdup(p);
101 /* slight modification: no 'filename' arg, and errors are passed back in 'violation' instead of printed to stderr */
102 static FLAC__bool set_vc_field(FLAC__StreamMetadata *block, const Argument_VcField *field, FLAC__bool *needs_write, FLAC__bool raw, const char **violation)
104 FLAC__StreamMetadata_VorbisComment_Entry entry;
107 FLAC__ASSERT(0 != block);
108 FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
109 FLAC__ASSERT(0 != field);
110 FLAC__ASSERT(0 != needs_write);
112 if(field->field_value_from_file) {
113 /* read the file into 'data' */
116 const off_t size = grabbag__file_get_filesize(field->field_value);
118 *violation = "can't open file for tag value";
121 if(size >= 0x100000) { /* magic arbitrary limit, actual format limit is near 16MB */
122 *violation = "file for tag value is too large";
125 if(0 == (data = malloc(size+1)))
126 die("out of memory allocating tag value");
128 if(0 == (f = fopen(field->field_value, "rb")) || fread(data, 1, size, f) != (size_t)size) {
132 *violation = "error while reading file for tag value";
136 if(strlen(data) != (size_t)size) {
138 *violation = "file for tag value has embedded NULs";
142 /* move 'data' into 'converted', converting to UTF-8 if necessary */
146 else if(utf8_encode(data, &converted) >= 0) {
151 *violation = "error converting file contents to UTF-8 for tag value";
155 /* create and entry and append it */
156 if(!FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, field->field_name, converted)) {
158 *violation = "file for tag value is not valid UTF-8";
162 if(!FLAC__metadata_object_vorbiscomment_append_comment(block, entry, /*copy=*/false)) {
163 *violation = "memory allocation failure";
171 FLAC__bool needs_free = false;
173 entry.entry = (FLAC__byte *)field->field;
175 else if(utf8_encode(field->field, &converted) >= 0) {
176 entry.entry = (FLAC__byte *)converted;
180 *violation = "error converting comment to UTF-8";
183 entry.length = strlen((const char *)entry.entry);
184 if(!FLAC__format_vorbiscomment_entry_is_legal(entry.entry, entry.length)) {
188 * our previous parsing has already established that the field
189 * name is OK, so it must be the field value
191 *violation = "tag value for is not valid UTF-8";
195 if(!FLAC__metadata_object_vorbiscomment_append_comment(block, entry, /*copy=*/true)) {
198 *violation = "memory allocation failure";
210 * The rest of the code is novel
213 static void free_field(Argument_VcField *obj)
217 if(0 != obj->field_name)
218 free(obj->field_name);
219 if(0 != obj->field_value)
220 free(obj->field_value);
223 FLAC__bool flac__vorbiscomment_add(FLAC__StreamMetadata *block, const char *comment, FLAC__bool value_from_file, const char **violation)
225 Argument_VcField parsed;
228 FLAC__ASSERT(0 != block);
229 FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
230 FLAC__ASSERT(0 != comment);
232 memset(&parsed, 0, sizeof(parsed));
234 parsed.field_value_from_file = value_from_file;
235 if(!parse_vorbis_comment_field(comment, &(parsed.field), &(parsed.field_name), &(parsed.field_value), &(parsed.field_value_length), violation)) {
240 if(!set_vc_field(block, &parsed, &dummy, /*raw=*/false, violation)) {