1 /* plugin_common - Routines common to several plugins
2 * Copyright (C) 2002,2003,2004,2005,2006,2007 Josh Coalson
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library 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 GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 #include "FLAC/assert.h"
29 #include "FLAC/metadata.h"
32 static __inline unsigned local__wide_strlen(const FLAC__uint16 *s)
41 * also disallows non-shortest-form encodings, c.f.
42 * http://www.unicode.org/versions/corrigendum1.html
43 * and a more clear explanation at the end of this section:
44 * http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
46 static __inline unsigned local__utf8len(const FLAC__byte *utf8)
48 FLAC__ASSERT(0 != utf8);
49 if ((utf8[0] & 0x80) == 0) {
52 else if ((utf8[0] & 0xE0) == 0xC0 && (utf8[1] & 0xC0) == 0x80) {
53 if ((utf8[0] & 0xFE) == 0xC0) /* overlong sequence check */
57 else if ((utf8[0] & 0xF0) == 0xE0 && (utf8[1] & 0xC0) == 0x80 && (utf8[2] & 0xC0) == 0x80) {
58 if (utf8[0] == 0xE0 && (utf8[1] & 0xE0) == 0x80) /* overlong sequence check */
60 /* illegal surrogates check (U+D800...U+DFFF and U+FFFE...U+FFFF) */
61 if (utf8[0] == 0xED && (utf8[1] & 0xE0) == 0xA0) /* D800-DFFF */
63 if (utf8[0] == 0xEF && utf8[1] == 0xBF && (utf8[2] & 0xFE) == 0xBE) /* FFFE-FFFF */
67 else if ((utf8[0] & 0xF8) == 0xF0 && (utf8[1] & 0xC0) == 0x80 && (utf8[2] & 0xC0) == 0x80 && (utf8[3] & 0xC0) == 0x80) {
68 if (utf8[0] == 0xF0 && (utf8[1] & 0xF0) == 0x80) /* overlong sequence check */
72 else if ((utf8[0] & 0xFC) == 0xF8 && (utf8[1] & 0xC0) == 0x80 && (utf8[2] & 0xC0) == 0x80 && (utf8[3] & 0xC0) == 0x80 && (utf8[4] & 0xC0) == 0x80) {
73 if (utf8[0] == 0xF8 && (utf8[1] & 0xF8) == 0x80) /* overlong sequence check */
77 else if ((utf8[0] & 0xFE) == 0xFC && (utf8[1] & 0xC0) == 0x80 && (utf8[2] & 0xC0) == 0x80 && (utf8[3] & 0xC0) == 0x80 && (utf8[4] & 0xC0) == 0x80 && (utf8[5] & 0xC0) == 0x80) {
78 if (utf8[0] == 0xFC && (utf8[1] & 0xFC) == 0x80) /* overlong sequence check */
88 static __inline unsigned local__utf8_to_ucs2(const FLAC__byte *utf8, FLAC__uint16 *ucs2)
90 const unsigned len = local__utf8len(utf8);
92 FLAC__ASSERT(0 != ucs2);
97 *ucs2 = (*utf8 & 0x3F)<<6 | (*(utf8+1) & 0x3F);
99 *ucs2 = (*utf8 & 0x1F)<<12 | (*(utf8+1) & 0x3F)<<6 | (*(utf8+2) & 0x3F);
106 static FLAC__uint16 *local__convert_utf8_to_ucs2(const char *src, unsigned length)
111 FLAC__ASSERT(0 != src);
113 /* calculate length */
115 const unsigned char *s, *end;
116 for (s=(const unsigned char *)src, end=s+length; s<end; chars++) {
117 const unsigned n = local__utf8len(s);
122 FLAC__ASSERT(s == end);
126 out = (FLAC__uint16*)malloc(chars * sizeof(FLAC__uint16));
134 const unsigned char *s = (const unsigned char *)src;
135 FLAC__uint16 *u = out;
136 for ( ; chars; chars--)
137 s += local__utf8_to_ucs2(s, u++);
143 static __inline unsigned local__ucs2len(FLAC__uint16 ucs2)
147 else if (ucs2 < 0x0800)
153 static __inline unsigned local__ucs2_to_utf8(FLAC__uint16 ucs2, FLAC__byte *utf8)
156 utf8[0] = (FLAC__byte)ucs2;
159 else if (ucs2 < 0x800) {
160 utf8[0] = 0xc0 | (ucs2 >> 6);
161 utf8[1] = 0x80 | (ucs2 & 0x3f);
165 utf8[0] = 0xe0 | (ucs2 >> 12);
166 utf8[1] = 0x80 | ((ucs2 >> 6) & 0x3f);
167 utf8[2] = 0x80 | (ucs2 & 0x3f);
172 static char *local__convert_ucs2_to_utf8(const FLAC__uint16 *src, unsigned length)
177 FLAC__ASSERT(0 != src);
179 /* calculate length */
182 for (i = 0; i < length; i++)
183 len += local__ucs2len(src[i]);
187 out = (char*)malloc(len * sizeof(char));
193 unsigned char *u = (unsigned char *)out;
195 u += local__ucs2_to_utf8(*src, u);
196 local__ucs2_to_utf8(*src, u);
203 FLAC__bool FLAC_plugin__tags_get(const char *filename, FLAC__StreamMetadata **tags)
205 if(!FLAC__metadata_get_tags(filename, tags))
206 if(0 == (*tags = FLAC__metadata_object_new(FLAC__METADATA_TYPE_VORBIS_COMMENT)))
211 FLAC__bool FLAC_plugin__tags_set(const char *filename, const FLAC__StreamMetadata *tags)
213 FLAC__Metadata_Chain *chain;
214 FLAC__Metadata_Iterator *iterator;
215 FLAC__StreamMetadata *block;
216 FLAC__bool got_vorbis_comments = false;
219 if(0 == (chain = FLAC__metadata_chain_new()))
222 if(!FLAC__metadata_chain_read(chain, filename)) {
223 FLAC__metadata_chain_delete(chain);
227 if(0 == (iterator = FLAC__metadata_iterator_new())) {
228 FLAC__metadata_chain_delete(chain);
232 FLAC__metadata_iterator_init(iterator, chain);
235 if(FLAC__metadata_iterator_get_block_type(iterator) == FLAC__METADATA_TYPE_VORBIS_COMMENT)
236 got_vorbis_comments = true;
237 } while(!got_vorbis_comments && FLAC__metadata_iterator_next(iterator));
239 if(0 == (block = FLAC__metadata_object_clone(tags))) {
240 FLAC__metadata_chain_delete(chain);
241 FLAC__metadata_iterator_delete(iterator);
245 if(got_vorbis_comments)
246 ok = FLAC__metadata_iterator_set_block(iterator, block);
248 ok = FLAC__metadata_iterator_insert_block_after(iterator, block);
250 FLAC__metadata_iterator_delete(iterator);
253 FLAC__metadata_chain_sort_padding(chain);
254 ok = FLAC__metadata_chain_write(chain, /*use_padding=*/true, /*preserve_file_stats=*/true);
257 FLAC__metadata_chain_delete(chain);
262 void FLAC_plugin__tags_destroy(FLAC__StreamMetadata **tags)
264 FLAC__metadata_object_delete(*tags);
268 const char *FLAC_plugin__tags_get_tag_utf8(const FLAC__StreamMetadata *tags, const char *name)
270 const int i = FLAC__metadata_object_vorbiscomment_find_entry_from(tags, /*offset=*/0, name);
271 return (i < 0? 0 : strchr((const char *)tags->data.vorbis_comment.comments[i].entry, '=')+1);
274 FLAC__uint16 *FLAC_plugin__tags_get_tag_ucs2(const FLAC__StreamMetadata *tags, const char *name)
276 const char *utf8 = FLAC_plugin__tags_get_tag_utf8(tags, name);
279 return local__convert_utf8_to_ucs2(utf8, strlen(utf8)+1); /* +1 for terminating null */
282 int FLAC_plugin__tags_delete_tag(FLAC__StreamMetadata *tags, const char *name)
284 return FLAC__metadata_object_vorbiscomment_remove_entries_matching(tags, name);
287 int FLAC_plugin__tags_delete_all(FLAC__StreamMetadata *tags)
289 int n = (int)tags->data.vorbis_comment.num_comments;
291 if(!FLAC__metadata_object_vorbiscomment_resize_comments(tags, 0))
297 FLAC__bool FLAC_plugin__tags_add_tag_utf8(FLAC__StreamMetadata *tags, const char *name, const char *value, const char *separator)
301 FLAC__ASSERT(0 != tags);
302 FLAC__ASSERT(0 != name);
303 FLAC__ASSERT(0 != value);
305 if(separator && (i = FLAC__metadata_object_vorbiscomment_find_entry_from(tags, /*offset=*/0, name)) >= 0) {
306 FLAC__StreamMetadata_VorbisComment_Entry *entry = tags->data.vorbis_comment.comments+i;
307 const size_t value_len = strlen(value);
308 const size_t separator_len = strlen(separator);
309 FLAC__byte *new_entry;
310 if(0 == (new_entry = (FLAC__byte*)realloc(entry->entry, entry->length + value_len + separator_len + 1)))
312 memcpy(new_entry+entry->length, separator, separator_len);
313 entry->length += separator_len;
314 memcpy(new_entry+entry->length, value, value_len);
315 entry->length += value_len;
316 new_entry[entry->length] = '\0';
317 entry->entry = new_entry;
320 FLAC__StreamMetadata_VorbisComment_Entry entry;
321 if(!FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, name, value))
323 FLAC__metadata_object_vorbiscomment_append_comment(tags, entry, /*copy=*/false);
328 FLAC__bool FLAC_plugin__tags_set_tag_ucs2(FLAC__StreamMetadata *tags, const char *name, const FLAC__uint16 *value, FLAC__bool replace_all)
330 FLAC__StreamMetadata_VorbisComment_Entry entry;
332 FLAC__ASSERT(0 != tags);
333 FLAC__ASSERT(0 != name);
334 FLAC__ASSERT(0 != value);
337 char *utf8 = local__convert_ucs2_to_utf8(value, local__wide_strlen(value)+1); /* +1 for the terminating null */
340 if(!FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, name, utf8)) {
346 if(!FLAC__metadata_object_vorbiscomment_replace_comment(tags, entry, replace_all, /*copy=*/false))