1 /* flac - Command-line FLAC encoder/decoder
2 * Copyright (C) 2002,2003,2004,2005,2006 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/utf8.h"
34 * This struct and the following 4 static functions are copied from
35 * ../metaflac/main.c. Maybe someday there will be a convenience
36 * library for Vorbis comment parsing.
39 char *field; /* the whole field as passed on the command line, i.e. "NAME=VALUE" */
41 /* according to the vorbis spec, field values can contain \0 so simple C strings are not enough here */
42 unsigned field_value_length;
46 static void die(const char *message)
48 FLAC__ASSERT(0 != message);
49 fprintf(stderr, "ERROR: %s\n", message);
53 static char *local_strdup(const char *source)
56 FLAC__ASSERT(0 != source);
57 if(0 == (ret = strdup(source)))
58 die("out of memory during strdup()");
62 static FLAC__bool parse_vorbis_comment_field(const char *field_ref, char **field, char **name, char **value, unsigned *length, const char **violation)
64 static const char * const violations[] = {
65 "field name contains invalid character",
66 "field contains no '=' character"
72 *field = local_strdup(field_ref);
74 s = local_strdup(field_ref);
76 if(0 == (p = strchr(s, '='))) {
78 *violation = violations[1];
84 if(*q < 0x20 || *q > 0x7d || *q == 0x3d) {
86 *violation = violations[0];
91 *name = local_strdup(s);
92 *value = local_strdup(p);
99 /* slight modification: no 'filename' arg, and errors are passed back in 'violation' instead of printed to stderr */
100 static FLAC__bool set_vc_field(FLAC__StreamMetadata *block, const Argument_VcField *field, FLAC__bool *needs_write, FLAC__bool raw, const char **violation)
102 FLAC__StreamMetadata_VorbisComment_Entry entry;
104 FLAC__bool needs_free = false;
106 FLAC__ASSERT(0 != block);
107 FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
108 FLAC__ASSERT(0 != field);
109 FLAC__ASSERT(0 != needs_write);
112 entry.entry = (FLAC__byte *)field->field;
114 else if(utf8_encode(field->field, &converted) >= 0) {
115 entry.entry = (FLAC__byte *)converted;
119 *violation = "couldn't convert comment to UTF-8";
123 entry.length = strlen((const char *)entry.entry);
125 if(!FLAC__metadata_object_vorbiscomment_append_comment(block, entry, /*copy=*/true)) {
128 *violation = "memory allocation failure";
140 * The rest of the code is novel
143 static void free_field(Argument_VcField *obj)
147 if(0 != obj->field_name)
148 free(obj->field_name);
149 if(0 != obj->field_value)
150 free(obj->field_value);
153 FLAC__bool flac__vorbiscomment_add(FLAC__StreamMetadata *block, const char *comment, const char **violation)
155 Argument_VcField parsed;
158 FLAC__ASSERT(0 != block);
159 FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
160 FLAC__ASSERT(0 != comment);
162 memset(&parsed, 0, sizeof(parsed));
164 if(!parse_vorbis_comment_field(comment, &(parsed.field), &(parsed.field_name), &(parsed.field_value), &(parsed.field_value_length), violation)) {
169 if(!set_vc_field(block, &parsed, &dummy, /*raw=*/false, violation)) {