1 /*
2  *  flac123 a command-line flac player
3  *  Copyright (C) 2003-2007  Jake Angerman
4  *
5  *  This vorbiscomment.c module heavily based upon:
6  *  plugin_common - Routines common to several xmms plugins
7  *  Copyright (C) 2002,2003  Josh Coalson
8  *
9  *  This program is free software; you can redistribute it and/or
10  *  modify it under the terms of the GNU General Public License
11  *  as published by the Free Software Foundation; either version 2
12  *  of the License, or (at your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, write to the Free Software
21  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
22  */
23 
24 #include <string.h>
25 #include <strings.h>
26 #include <stdlib.h>
27 
28 #include "flac123.h"
29 
local__vcentry_matches(const char * field_name,const FLAC__StreamMetadata_VorbisComment_Entry * entry)30 static int local__vcentry_matches(const char *field_name, const FLAC__StreamMetadata_VorbisComment_Entry *entry)
31 {
32     const FLAC__byte *eq = memchr(entry->entry, '=', entry->length);
33     const unsigned field_name_length = strlen(field_name);
34     return (0 != eq && (unsigned)(eq-entry->entry) == field_name_length && 0 == strncasecmp(field_name, entry->entry, field_name_length));
35 }
36 
37 /* parse a string entry and put it into dest which is of size len.
38  * len is the width of non-null bytes.
39  */
local__vcentry_parse_value(const FLAC__StreamMetadata_VorbisComment_Entry * entry,char * dest,int len)40 static void local__vcentry_parse_value(const FLAC__StreamMetadata_VorbisComment_Entry *entry, char *dest, int len)
41 {
42     const FLAC__byte * const eq = memchr(entry->entry, '=', entry->length);
43 
44     if(!eq || !dest)
45 	return;
46     else {
47 	unsigned value_length = entry->length - (unsigned)((eq+1) - entry->entry);
48 	if(value_length > len) {
49 	    value_length = len;
50 	}
51 
52 	memset(dest, ' ', len);
53 	memcpy(dest, eq+1, value_length);
54 	dest[len] = '\0'; /* assumes char dest[len+1] */
55     }
56 }
57 
get_vorbis_comments(const char * filename)58 FLAC__bool get_vorbis_comments(const char *filename)
59 {
60     FLAC__Metadata_SimpleIterator *iterator = FLAC__metadata_simple_iterator_new();
61     FLAC__bool got_vorbis_comments = false;
62 
63     if(0 != iterator) {
64 	if(FLAC__metadata_simple_iterator_init(iterator, filename, /*read_only=*/true, /*preserve_file_stats=*/true)) {
65 	    do {
66 		if(FLAC__metadata_simple_iterator_get_block_type(iterator) == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
67 		    FLAC__StreamMetadata *block = FLAC__metadata_simple_iterator_get_block(iterator);
68 		    if(0 != block) {
69 			int i;
70 			const FLAC__StreamMetadata_VorbisComment *vc = &block->data.vorbis_comment;
71 			for(i = 0; i < vc->num_comments; i++) {
72 			    if(local__vcentry_matches("artist", &vc->comments[i]))
73 			    {
74 				local__vcentry_parse_value(&vc->comments[i], file_info.artist, VORBIS_TAG_LEN);
75 				got_vorbis_comments = true;
76 			    }
77 			    else if(local__vcentry_matches("album", &vc->comments[i]))
78 			    {
79 				local__vcentry_parse_value(&vc->comments[i], file_info.album, VORBIS_TAG_LEN);
80 				got_vorbis_comments = true;
81 			    }
82 			    else if(local__vcentry_matches("title", &vc->comments[i]))
83 			    {
84 				local__vcentry_parse_value(&vc->comments[i], file_info.title, VORBIS_TAG_LEN);
85 				got_vorbis_comments = true;
86 			    }
87 			    else if(local__vcentry_matches("genre", &vc->comments[i]))
88 			    {
89 				local__vcentry_parse_value(&vc->comments[i], file_info.genre, VORBIS_TAG_LEN);
90 				got_vorbis_comments = true;
91 			    }
92 			    else if(local__vcentry_matches("description", &vc->comments[i]))
93 			    {
94 				local__vcentry_parse_value(&vc->comments[i], file_info.comment, VORBIS_TAG_LEN);
95 				got_vorbis_comments = true;
96 			    }
97 			    else if(local__vcentry_matches("date", &vc->comments[i]))
98 			    {
99 				local__vcentry_parse_value(&vc->comments[i], file_info.year, VORBIS_YEAR_LEN);
100 				got_vorbis_comments = true;
101 			    }
102 			}
103 			FLAC__metadata_object_delete(block);
104 		    }
105 		}
106 	    } while (!got_vorbis_comments && FLAC__metadata_simple_iterator_next(iterator));
107 	}
108 	FLAC__metadata_simple_iterator_delete(iterator);
109     }
110     return got_vorbis_comments;
111 }
112