initial import
[platform/upstream/flac.git] / src / flac / vorbiscomment.c
1 /* flac - Command-line FLAC encoder/decoder
2  * Copyright (C) 2002  Josh Coalson
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #if HAVE_CONFIG_H
20 #  include <config.h>
21 #endif
22
23 #include "FLAC/assert.h"
24 #include "FLAC/metadata.h"
25 #include "share/utf8.h"
26 #include <ctype.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31
32 /*
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.
36  */
37 typedef struct {
38         char *field; /* the whole field as passed on the command line, i.e. "NAME=VALUE" */
39         char *field_name;
40         /* according to the vorbis spec, field values can contain \0 so simple C strings are not enough here */
41         unsigned field_value_length;
42         char *field_value;
43 } Argument_VcField;
44
45 static void die(const char *message)
46 {
47         FLAC__ASSERT(0 != message);
48         fprintf(stderr, "ERROR: %s\n", message);
49         exit(1);
50 }
51
52 static char *local_strdup(const char *source)
53 {
54         char *ret;
55         FLAC__ASSERT(0 != source);
56         if(0 == (ret = strdup(source)))
57                 die("out of memory during strdup()");
58         return ret;
59 }
60
61 static FLAC__bool parse_vorbis_comment_field(const char *field_ref, char **field, char **name, char **value, unsigned *length, const char **violation)
62 {
63         static const char * const violations[] = {
64                 "field name contains invalid character",
65                 "field contains no '=' character"
66         };
67
68         char *p, *q, *s;
69
70         if(0 != field)
71                 *field = local_strdup(field_ref);
72
73         s = local_strdup(field_ref);
74
75         if(0 == (p = strchr(s, '='))) {
76                 free(s);
77                 *violation = violations[1];
78                 return false;
79         }
80         *p++ = '\0';
81
82         for(q = s; *q; q++) {
83                 if(*q < 0x20 || *q > 0x7d || *q == 0x3d) {
84                         free(s);
85                         *violation = violations[0];
86                         return false;
87                 }
88         }
89
90         *name = local_strdup(s);
91         *value = local_strdup(p);
92         *length = strlen(p);
93
94         free(s);
95         return true;
96 }
97
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)
100 {
101         FLAC__StreamMetadata_VorbisComment_Entry entry;
102         char *converted;
103         FLAC__bool needs_free = false;
104
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);
109
110         if(raw) {
111                 entry.entry = field->field;
112         }
113         else if(utf8_encode(field->field, &converted) >= 0) {
114                 entry.entry = converted;
115                 needs_free = true;
116         }
117         else {
118                 *violation = "couldn't convert comment to UTF-8";
119                 return false;
120         }
121
122         entry.length = strlen(entry.entry);
123
124         if(!FLAC__metadata_object_vorbiscomment_insert_comment(block, block->data.vorbis_comment.num_comments, entry, /*copy=*/true)) {
125                 if(needs_free)
126                         free(converted);
127                 *violation = "memory allocation failure";
128                 return false;
129         }
130         else {
131                 *needs_write = true;
132                 if(needs_free)
133                         free(converted);
134                 return true;
135         }
136 }
137
138 /*
139  * The rest of the code is novel
140  */
141
142 static void free_field(Argument_VcField *obj)
143 {
144         if(0 != obj->field)
145                 free(obj->field);
146         if(0 != obj->field_name)
147                 free(obj->field_name);
148         if(0 != obj->field_value)
149                 free(obj->field_value);
150 }
151
152 FLAC__bool flac__vorbiscomment_add(FLAC__StreamMetadata *block, const char *comment, const char **violation)
153 {
154         Argument_VcField parsed;
155         FLAC__bool dummy;
156
157         FLAC__ASSERT(0 != block);
158         FLAC__ASSERT(block->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
159         FLAC__ASSERT(0 != comment);
160
161         memset(&parsed, 0, sizeof(parsed));
162
163         if(!parse_vorbis_comment_field(comment, &(parsed.field), &(parsed.field_name), &(parsed.field_value), &(parsed.field_value_length), violation)) {
164                 free_field(&parsed);
165                 return false;
166         }
167
168         if(!set_vc_field(block, &parsed, &dummy, /*raw=*/false, violation)) {
169                 free_field(&parsed);
170                 return false;
171         }
172         else {
173                 free_field(&parsed);
174                 return true;
175         }
176 }