61
|
1 /* plugin_common - Routines common to several plugins
|
|
2 * Copyright (C) 2002,2003,2004,2005 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 #include <stdio.h>
|
|
20 #include <string.h>
|
|
21 #include <stdlib.h>
|
|
22
|
|
23 #include "tags.h"
|
|
24 #include "FLAC/assert.h"
|
|
25 #include "FLAC/metadata.h"
|
|
26
|
|
27
|
|
28 static __inline unsigned local__wide_strlen(const FLAC__uint16 *s)
|
|
29 {
|
|
30 unsigned n = 0;
|
|
31 while(*s++)
|
|
32 n++;
|
|
33 return n;
|
|
34 }
|
|
35
|
|
36 static __inline unsigned local__utf8len(const FLAC__byte *utf8)
|
|
37 {
|
|
38 FLAC__ASSERT(0 != utf8);
|
|
39 if ((utf8[0] & 0x80) == 0)
|
|
40 return 1;
|
|
41 else if ((utf8[0] & 0xE0) == 0xC0 && (utf8[1] & 0xC0) == 0x80)
|
|
42 return 2;
|
|
43 else if ((utf8[0] & 0xF0) == 0xE0 && (utf8[1] & 0xC0) == 0x80 && (utf8[2] & 0xC0) == 0x80)
|
|
44 return 3;
|
|
45 else
|
|
46 return 0;
|
|
47 }
|
|
48
|
|
49 static __inline unsigned local__utf8_to_ucs2(const FLAC__byte *utf8, FLAC__uint16 *ucs2)
|
|
50 {
|
|
51 const unsigned len = local__utf8len(utf8);
|
|
52
|
|
53 FLAC__ASSERT(0 != ucs2);
|
|
54
|
|
55 if (len == 1)
|
|
56 *ucs2 = *utf8;
|
|
57 else if (len == 2)
|
|
58 *ucs2 = (*utf8 & 0x3F)<<6 | (*(utf8+1) & 0x3F);
|
|
59 else if (len == 3)
|
|
60 *ucs2 = (*utf8 & 0x1F)<<12 | (*(utf8+1) & 0x3F)<<6 | (*(utf8+2) & 0x3F);
|
|
61
|
|
62 return len;
|
|
63 }
|
|
64
|
|
65 static FLAC__uint16 *local__convert_utf8_to_ucs2(const char *src, unsigned length)
|
|
66 {
|
|
67 FLAC__uint16 *out;
|
|
68 unsigned chars = 0;
|
|
69
|
|
70 FLAC__ASSERT(0 != src);
|
|
71
|
|
72 /* calculate length */
|
|
73 {
|
|
74 const char *s, *end;
|
|
75 for (s=src, end=src+length; s<end; chars++) {
|
|
76 const unsigned n = local__utf8len(s);
|
|
77 if (n == 0)
|
|
78 return 0;
|
|
79 s += n;
|
|
80 }
|
|
81 FLAC__ASSERT(s == end);
|
|
82 }
|
|
83
|
|
84 /* allocate */
|
|
85 out = (FLAC__uint16*)malloc(chars * sizeof(FLAC__uint16));
|
|
86 if (0 == out) {
|
|
87 FLAC__ASSERT(0);
|
|
88 return 0;
|
|
89 }
|
|
90
|
|
91 /* convert */
|
|
92 {
|
|
93 FLAC__uint16 *u = out;
|
|
94 for ( ; chars; chars--)
|
|
95 src += local__utf8_to_ucs2(src, u++);
|
|
96 }
|
|
97
|
|
98 return out;
|
|
99 }
|
|
100
|
|
101 static __inline unsigned local__ucs2len(FLAC__uint16 ucs2)
|
|
102 {
|
|
103 if (ucs2 < 0x0080)
|
|
104 return 1;
|
|
105 else if (ucs2 < 0x0800)
|
|
106 return 2;
|
|
107 else
|
|
108 return 3;
|
|
109 }
|
|
110
|
|
111 static __inline unsigned local__ucs2_to_utf8(FLAC__uint16 ucs2, FLAC__byte *utf8)
|
|
112 {
|
|
113 if (ucs2 < 0x080) {
|
|
114 utf8[0] = (FLAC__byte)ucs2;
|
|
115 return 1;
|
|
116 }
|
|
117 else if (ucs2 < 0x800) {
|
|
118 utf8[0] = 0xc0 | (ucs2 >> 6);
|
|
119 utf8[1] = 0x80 | (ucs2 & 0x3f);
|
|
120 return 2;
|
|
121 }
|
|
122 else {
|
|
123 utf8[0] = 0xe0 | (ucs2 >> 12);
|
|
124 utf8[1] = 0x80 | ((ucs2 >> 6) & 0x3f);
|
|
125 utf8[2] = 0x80 | (ucs2 & 0x3f);
|
|
126 return 3;
|
|
127 }
|
|
128 }
|
|
129
|
|
130 static char *local__convert_ucs2_to_utf8(const FLAC__uint16 *src, unsigned length)
|
|
131 {
|
|
132 char *out;
|
|
133 unsigned len = 0;
|
|
134
|
|
135 FLAC__ASSERT(0 != src);
|
|
136
|
|
137 /* calculate length */
|
|
138 {
|
|
139 unsigned i;
|
|
140 for (i = 0; i < length; i++)
|
|
141 len += local__ucs2len(src[i]);
|
|
142 }
|
|
143
|
|
144 /* allocate */
|
|
145 out = (char*)malloc(len * sizeof(char));
|
|
146 if (0 == out)
|
|
147 return 0;
|
|
148
|
|
149 /* convert */
|
|
150 {
|
|
151 char *u = out;
|
|
152 for ( ; *src; src++)
|
|
153 u += local__ucs2_to_utf8(*src, u);
|
|
154 local__ucs2_to_utf8(*src, u);
|
|
155 }
|
|
156
|
|
157 return out;
|
|
158 }
|
|
159
|
|
160
|
|
161 FLAC__bool FLAC_plugin__tags_get(const char *filename, FLAC__StreamMetadata **tags)
|
|
162 {
|
|
163 if(!FLAC__metadata_get_tags(filename, tags))
|
|
164 if(0 == (*tags = FLAC__metadata_object_new(FLAC__METADATA_TYPE_VORBIS_COMMENT)))
|
|
165 return false;
|
|
166 return true;
|
|
167 }
|
|
168
|
|
169 FLAC__bool FLAC_plugin__tags_set(const char *filename, const FLAC__StreamMetadata *tags)
|
|
170 {
|
|
171 FLAC__Metadata_Chain *chain;
|
|
172 FLAC__Metadata_Iterator *iterator;
|
|
173 FLAC__StreamMetadata *block;
|
|
174 FLAC__bool got_vorbis_comments = false;
|
|
175 FLAC__bool ok;
|
|
176
|
|
177 if(0 == (chain = FLAC__metadata_chain_new()))
|
|
178 return false;
|
|
179
|
|
180 if(!FLAC__metadata_chain_read(chain, filename)) {
|
|
181 FLAC__metadata_chain_delete(chain);
|
|
182 return false;
|
|
183 }
|
|
184
|
|
185 if(0 == (iterator = FLAC__metadata_iterator_new())) {
|
|
186 FLAC__metadata_chain_delete(chain);
|
|
187 return false;
|
|
188 }
|
|
189
|
|
190 FLAC__metadata_iterator_init(iterator, chain);
|
|
191
|
|
192 do {
|
|
193 if(FLAC__metadata_iterator_get_block_type(iterator) == FLAC__METADATA_TYPE_VORBIS_COMMENT)
|
|
194 got_vorbis_comments = true;
|
|
195 } while(!got_vorbis_comments && FLAC__metadata_iterator_next(iterator));
|
|
196
|
|
197 if(0 == (block = FLAC__metadata_object_clone(tags))) {
|
|
198 FLAC__metadata_chain_delete(chain);
|
|
199 FLAC__metadata_iterator_delete(iterator);
|
|
200 return false;
|
|
201 }
|
|
202
|
|
203 if(got_vorbis_comments)
|
|
204 ok = FLAC__metadata_iterator_set_block(iterator, block);
|
|
205 else
|
|
206 ok = FLAC__metadata_iterator_insert_block_after(iterator, block);
|
|
207
|
|
208 FLAC__metadata_iterator_delete(iterator);
|
|
209
|
|
210 if(ok) {
|
|
211 FLAC__metadata_chain_sort_padding(chain);
|
|
212 ok = FLAC__metadata_chain_write(chain, /*use_padding=*/true, /*preserve_file_stats=*/true);
|
|
213 }
|
|
214
|
|
215 FLAC__metadata_chain_delete(chain);
|
|
216
|
|
217 return ok;
|
|
218 }
|
|
219
|
|
220 void FLAC_plugin__tags_destroy(FLAC__StreamMetadata **tags)
|
|
221 {
|
|
222 FLAC__metadata_object_delete(*tags);
|
|
223 *tags = 0;
|
|
224 }
|
|
225
|
|
226 const char *FLAC_plugin__tags_get_tag_utf8(const FLAC__StreamMetadata *tags, const char *name)
|
|
227 {
|
|
228 const int i = FLAC__metadata_object_vorbiscomment_find_entry_from(tags, /*offset=*/0, name);
|
|
229 return (i < 0? 0 : strchr(tags->data.vorbis_comment.comments[i].entry, '=')+1);
|
|
230 }
|
|
231
|
|
232 FLAC__uint16 *FLAC_plugin__tags_get_tag_ucs2(const FLAC__StreamMetadata *tags, const char *name)
|
|
233 {
|
|
234 const char *utf8 = FLAC_plugin__tags_get_tag_utf8(tags, name);
|
|
235 if(0 == utf8)
|
|
236 return 0;
|
|
237 return local__convert_utf8_to_ucs2(utf8, strlen(utf8)+1); /* +1 for terminating null */
|
|
238 }
|
|
239
|
|
240 int FLAC_plugin__tags_delete_tag(FLAC__StreamMetadata *tags, const char *name)
|
|
241 {
|
|
242 return FLAC__metadata_object_vorbiscomment_remove_entries_matching(tags, name);
|
|
243 }
|
|
244
|
|
245 int FLAC_plugin__tags_delete_all(FLAC__StreamMetadata *tags)
|
|
246 {
|
|
247 int n = (int)tags->data.vorbis_comment.num_comments;
|
|
248 if(n > 0) {
|
|
249 if(!FLAC__metadata_object_vorbiscomment_resize_comments(tags, 0))
|
|
250 n = -1;
|
|
251 }
|
|
252 return n;
|
|
253 }
|
|
254
|
|
255 FLAC__bool FLAC_plugin__tags_add_tag_utf8(FLAC__StreamMetadata *tags, const char *name, const char *value, const char *separator)
|
|
256 {
|
|
257 int i;
|
|
258
|
|
259 FLAC__ASSERT(0 != tags);
|
|
260 FLAC__ASSERT(0 != name);
|
|
261 FLAC__ASSERT(0 != value);
|
|
262
|
|
263 if(separator && (i = FLAC__metadata_object_vorbiscomment_find_entry_from(tags, /*offset=*/0, name)) >= 0) {
|
|
264 FLAC__StreamMetadata_VorbisComment_Entry *entry = tags->data.vorbis_comment.comments+i;
|
|
265 const size_t value_len = strlen(value);
|
|
266 const size_t separator_len = strlen(separator);
|
|
267 FLAC__byte *new_entry;
|
|
268 if(0 == (new_entry = (FLAC__byte*)realloc(entry->entry, entry->length + value_len + separator_len + 1)))
|
|
269 return false;
|
|
270 memcpy(new_entry+entry->length, separator, separator_len);
|
|
271 entry->length += separator_len;
|
|
272 memcpy(new_entry+entry->length, value, value_len);
|
|
273 entry->length += value_len;
|
|
274 new_entry[entry->length] = '\0';
|
|
275 entry->entry = new_entry;
|
|
276 }
|
|
277 else {
|
|
278 FLAC__StreamMetadata_VorbisComment_Entry entry;
|
|
279 if(!FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, name, value))
|
|
280 return false;
|
|
281 FLAC__metadata_object_vorbiscomment_append_comment(tags, entry, /*copy=*/false);
|
|
282 }
|
|
283 return true;
|
|
284 }
|
|
285
|
|
286 FLAC__bool FLAC_plugin__tags_set_tag_ucs2(FLAC__StreamMetadata *tags, const char *name, const FLAC__uint16 *value, FLAC__bool replace_all)
|
|
287 {
|
|
288 FLAC__StreamMetadata_VorbisComment_Entry entry;
|
|
289
|
|
290 FLAC__ASSERT(0 != tags);
|
|
291 FLAC__ASSERT(0 != name);
|
|
292 FLAC__ASSERT(0 != value);
|
|
293
|
|
294 {
|
|
295 char *utf8 = local__convert_ucs2_to_utf8(value, local__wide_strlen(value)+1); /* +1 for the terminating null */
|
|
296 if(0 == utf8)
|
|
297 return false;
|
|
298 if(!FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, name, utf8)) {
|
|
299 free(utf8);
|
|
300 return false;
|
|
301 }
|
|
302 free(utf8);
|
|
303 }
|
|
304 if(!FLAC__metadata_object_vorbiscomment_replace_comment(tags, entry, replace_all, /*copy=*/false))
|
|
305 return false;
|
|
306 return true;
|
|
307 }
|