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