1 /* flac - Command-line FLAC encoder/decoder
2 * Copyright (C) 2002 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 "FLAC/assert.h"
24 #include "FLAC/metadata.h"
25 #include "share/utf8.h"
33 * This struct and the following 4 static functions are copied from
34 * ../metaflac/main.c. Maybe someday there will be a convenience
35 * library for Vorbis comment parsing.
38 char *field; /* the whole field as passed on the command line, i.e. "NAME=VALUE" */
40 /* according to the vorbis spec, field values can contain \0 so simple C strings are not enough here */
41 unsigned field_value_length;
45 static void die(const char *message)
47 FLAC__ASSERT(0 != message);
48 fprintf(stderr, "ERROR: %s\n", message);
52 static char *local_strdup(const char *source)
55 FLAC__ASSERT(0 != source);
56 if(0 == (ret = strdup(source)))
57 die("out of memory during strdup()");
61 static FLAC__bool parse_vorbis_comment_field(const char *field_ref, char **field, char **name, char **value, unsigned *length, const char **violation)
63 static const char * const violations[] = {
64 "field name contains invalid character",
65 "field contains no '=' character"
71 *field = local_strdup(field_ref);
73 s = local_strdup(field_ref);
75 if(0 == (p = strchr(s, '='))) {
77 *violation = violations[1];
83 if(*q < 0x20 || *q > 0x7d || *q == 0x3d) {
85 *violation = violations[0];
90 *name = local_strdup(s);
91 *value = local_strdup(p);
98 /* slight modification: no 'filename' arg, and errors are passed back in 'violation' instead of printed to stderr */
99 static FLAC__bool set_vc_field(FLAC__StreamMetadata *block, const Argument_VcField *field, FLAC__bool *needs_write, FLAC__bool raw, const char **violation)
101 FLAC__StreamMetadata_VorbisComment_Entry entry;
103 FLAC__bool needs_free = false;
105 FLAC__ASSERT(0 != block);
106 FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
107 FLAC__ASSERT(0 != field);
108 FLAC__ASSERT(0 != needs_write);
111 entry.entry = field->field;
113 else if(utf8_encode(field->field, &converted) >= 0) {
114 entry.entry = converted;
118 *violation = "couldn't convert comment to UTF-8";
122 entry.length = strlen(entry.entry);
124 if(!FLAC__metadata_object_vorbiscomment_insert_comment(block, block->data.vorbis_comment.num_comments, entry, /*copy=*/true)) {
127 *violation = "memory allocation failure";
139 * The rest of the code is novel
142 static void free_field(Argument_VcField *obj)
146 if(0 != obj->field_name)
147 free(obj->field_name);
148 if(0 != obj->field_value)
149 free(obj->field_value);
152 FLAC__bool flac__vorbiscomment_add(FLAC__StreamMetadata *block, const char *comment, const char **violation)
154 Argument_VcField parsed;
157 FLAC__ASSERT(0 != block);
158 FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
159 FLAC__ASSERT(0 != comment);
161 memset(&parsed, 0, sizeof(parsed));
163 if(!parse_vorbis_comment_field(comment, &(parsed.field), &(parsed.field_name), &(parsed.field_value), &(parsed.field_value_length), violation)) {
168 if(!set_vc_field(block, &parsed, &dummy, /*raw=*/false, violation)) {