aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/old/id3_extractor.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/plugins/old/id3_extractor.c')
-rw-r--r--src/plugins/old/id3_extractor.c306
1 files changed, 306 insertions, 0 deletions
diff --git a/src/plugins/old/id3_extractor.c b/src/plugins/old/id3_extractor.c
new file mode 100644
index 0000000..f402a1d
--- /dev/null
+++ b/src/plugins/old/id3_extractor.c
@@ -0,0 +1,306 @@
1/*
2 This file is part of libextractor.
3 (C) 2002, 2003, 2004, 2006, 2009, 2010 Vidyut Samanta and Christian Grothoff
4
5 libextractor is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
9
10 libextractor is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with libextractor; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19
20 */
21
22#include "platform.h"
23#include "extractor.h"
24#include "convert.h"
25#include <string.h>
26#include <stdio.h>
27#include <sys/types.h>
28#include <sys/stat.h>
29#include <unistd.h>
30#include <stdlib.h>
31
32#include "extractor_plugins.h"
33
34typedef struct
35{
36 char *title;
37 char *artist;
38 char *album;
39 char *year;
40 char *comment;
41 const char *genre;
42 unsigned int track_number;
43} id3tag;
44
45static const char *const genre_names[] = {
46 gettext_noop ("Blues"),
47 gettext_noop ("Classic Rock"),
48 gettext_noop ("Country"),
49 gettext_noop ("Dance"),
50 gettext_noop ("Disco"),
51 gettext_noop ("Funk"),
52 gettext_noop ("Grunge"),
53 gettext_noop ("Hip-Hop"),
54 gettext_noop ("Jazz"),
55 gettext_noop ("Metal"),
56 gettext_noop ("New Age"),
57 gettext_noop ("Oldies"),
58 gettext_noop ("Other"),
59 gettext_noop ("Pop"),
60 gettext_noop ("R&B"),
61 gettext_noop ("Rap"),
62 gettext_noop ("Reggae"),
63 gettext_noop ("Rock"),
64 gettext_noop ("Techno"),
65 gettext_noop ("Industrial"),
66 gettext_noop ("Alternative"),
67 gettext_noop ("Ska"),
68 gettext_noop ("Death Metal"),
69 gettext_noop ("Pranks"),
70 gettext_noop ("Soundtrack"),
71 gettext_noop ("Euro-Techno"),
72 gettext_noop ("Ambient"),
73 gettext_noop ("Trip-Hop"),
74 gettext_noop ("Vocal"),
75 gettext_noop ("Jazz+Funk"),
76 gettext_noop ("Fusion"),
77 gettext_noop ("Trance"),
78 gettext_noop ("Classical"),
79 gettext_noop ("Instrumental"),
80 gettext_noop ("Acid"),
81 gettext_noop ("House"),
82 gettext_noop ("Game"),
83 gettext_noop ("Sound Clip"),
84 gettext_noop ("Gospel"),
85 gettext_noop ("Noise"),
86 gettext_noop ("Alt. Rock"),
87 gettext_noop ("Bass"),
88 gettext_noop ("Soul"),
89 gettext_noop ("Punk"),
90 gettext_noop ("Space"),
91 gettext_noop ("Meditative"),
92 gettext_noop ("Instrumental Pop"),
93 gettext_noop ("Instrumental Rock"),
94 gettext_noop ("Ethnic"),
95 gettext_noop ("Gothic"),
96 gettext_noop ("Darkwave"),
97 gettext_noop ("Techno-Industrial"),
98 gettext_noop ("Electronic"),
99 gettext_noop ("Pop-Folk"),
100 gettext_noop ("Eurodance"),
101 gettext_noop ("Dream"),
102 gettext_noop ("Southern Rock"),
103 gettext_noop ("Comedy"),
104 gettext_noop ("Cult"),
105 gettext_noop ("Gangsta Rap"),
106 gettext_noop ("Top 40"),
107 gettext_noop ("Christian Rap"),
108 gettext_noop ("Pop/Funk"),
109 gettext_noop ("Jungle"),
110 gettext_noop ("Native American"),
111 gettext_noop ("Cabaret"),
112 gettext_noop ("New Wave"),
113 gettext_noop ("Psychedelic"),
114 gettext_noop ("Rave"),
115 gettext_noop ("Showtunes"),
116 gettext_noop ("Trailer"),
117 gettext_noop ("Lo-Fi"),
118 gettext_noop ("Tribal"),
119 gettext_noop ("Acid Punk"),
120 gettext_noop ("Acid Jazz"),
121 gettext_noop ("Polka"),
122 gettext_noop ("Retro"),
123 gettext_noop ("Musical"),
124 gettext_noop ("Rock & Roll"),
125 gettext_noop ("Hard Rock"),
126 gettext_noop ("Folk"),
127 gettext_noop ("Folk/Rock"),
128 gettext_noop ("National Folk"),
129 gettext_noop ("Swing"),
130 gettext_noop ("Fast-Fusion"),
131 gettext_noop ("Bebob"),
132 gettext_noop ("Latin"),
133 gettext_noop ("Revival"),
134 gettext_noop ("Celtic"),
135 gettext_noop ("Bluegrass"),
136 gettext_noop ("Avantgarde"),
137 gettext_noop ("Gothic Rock"),
138 gettext_noop ("Progressive Rock"),
139 gettext_noop ("Psychedelic Rock"),
140 gettext_noop ("Symphonic Rock"),
141 gettext_noop ("Slow Rock"),
142 gettext_noop ("Big Band"),
143 gettext_noop ("Chorus"),
144 gettext_noop ("Easy Listening"),
145 gettext_noop ("Acoustic"),
146 gettext_noop ("Humour"),
147 gettext_noop ("Speech"),
148 gettext_noop ("Chanson"),
149 gettext_noop ("Opera"),
150 gettext_noop ("Chamber Music"),
151 gettext_noop ("Sonata"),
152 gettext_noop ("Symphony"),
153 gettext_noop ("Booty Bass"),
154 gettext_noop ("Primus"),
155 gettext_noop ("Porn Groove"),
156 gettext_noop ("Satire"),
157 gettext_noop ("Slow Jam"),
158 gettext_noop ("Club"),
159 gettext_noop ("Tango"),
160 gettext_noop ("Samba"),
161 gettext_noop ("Folklore"),
162 gettext_noop ("Ballad"),
163 gettext_noop ("Power Ballad"),
164 gettext_noop ("Rhythmic Soul"),
165 gettext_noop ("Freestyle"),
166 gettext_noop ("Duet"),
167 gettext_noop ("Punk Rock"),
168 gettext_noop ("Drum Solo"),
169 gettext_noop ("A Cappella"),
170 gettext_noop ("Euro-House"),
171 gettext_noop ("Dance Hall"),
172 gettext_noop ("Goa"),
173 gettext_noop ("Drum & Bass"),
174 gettext_noop ("Club-House"),
175 gettext_noop ("Hardcore"),
176 gettext_noop ("Terror"),
177 gettext_noop ("Indie"),
178 gettext_noop ("BritPop"),
179 gettext_noop ("Negerpunk"),
180 gettext_noop ("Polsk Punk"),
181 gettext_noop ("Beat"),
182 gettext_noop ("Christian Gangsta Rap"),
183 gettext_noop ("Heavy Metal"),
184 gettext_noop ("Black Metal"),
185 gettext_noop ("Crossover"),
186 gettext_noop ("Contemporary Christian"),
187 gettext_noop ("Christian Rock"),
188 gettext_noop ("Merengue"),
189 gettext_noop ("Salsa"),
190 gettext_noop ("Thrash Metal"),
191 gettext_noop ("Anime"),
192 gettext_noop ("JPop"),
193 gettext_noop ("Synthpop"),
194};
195
196#define GENRE_NAME_COUNT \
197 ((unsigned int)(sizeof genre_names / sizeof (const char *const)))
198
199
200
201#define OK 0
202#define INVALID_ID3 1
203
204static void
205trim (char *k)
206{
207 if (k == NULL)
208 return;
209 while ((strlen (k) > 0) && (isspace ((unsigned char) k[strlen (k) - 1])))
210 k[strlen (k) - 1] = '\0';
211}
212
213static int
214get_id3 (const char *data, int64_t offset, int64_t size, id3tag *id3)
215{
216 const char *pos;
217
218 if (size < 128)
219 return INVALID_ID3;
220
221 pos = &data[offset];
222 if (0 != strncmp ("TAG", pos, 3))
223 return INVALID_ID3;
224 pos += 3;
225
226 id3->title = EXTRACTOR_common_convert_to_utf8 (pos, 30, "ISO-8859-1");
227 trim (id3->title);
228 pos += 30;
229 id3->artist = EXTRACTOR_common_convert_to_utf8 (pos, 30, "ISO-8859-1");
230 trim (id3->artist);
231 pos += 30;
232 id3->album = EXTRACTOR_common_convert_to_utf8 (pos, 30, "ISO-8859-1");
233 trim (id3->album);
234 pos += 30;
235 id3->year = EXTRACTOR_common_convert_to_utf8 (pos, 4, "ISO-8859-1");
236 trim (id3->year);
237 pos += 4;
238 id3->comment = EXTRACTOR_common_convert_to_utf8 (pos, 30, "ISO-8859-1");
239 trim (id3->comment);
240 if ( (pos[28] == '\0') &&
241 (pos[29] != '\0') )
242 {
243 /* ID3v1.1 */
244 id3->track_number = pos[29];
245 }
246 else
247 {
248 id3->track_number = 0;
249 }
250 pos += 30;
251 id3->genre = "";
252 if (pos[0] < GENRE_NAME_COUNT)
253 id3->genre = dgettext (PACKAGE, genre_names[(unsigned) pos[0]]);
254 return OK;
255}
256
257
258#define ADD(s,t) do { if ( (s != NULL) && (strlen(s) > 0) && (0 != proc (proc_cls, "id3", t, EXTRACTOR_METAFORMAT_UTF8, "text/plain", s, strlen(s)+1))) return 1; } while (0)
259
260
261int
262EXTRACTOR_id3_extract_method (struct EXTRACTOR_PluginList *plugin,
263 EXTRACTOR_MetaDataProcessor proc, void *proc_cls)
264{
265 id3tag info;
266 int64_t fsize;
267 unsigned char *data;
268 char track[16];
269
270 if (plugin == NULL)
271 return 1;
272
273 pl_seek (plugin, -128, SEEK_END);
274 fsize = pl_get_fsize (plugin);
275 if (fsize <= 0)
276 return 1;
277
278 if (128 != pl_read (plugin, &data, 128))
279 return 1;
280
281 memset (&info, 0, sizeof (info));
282
283 if (OK != get_id3 ((const char *) data, 0, 128, &info))
284 return 1;
285 ADD (info.title, EXTRACTOR_METATYPE_TITLE);
286 ADD (info.artist, EXTRACTOR_METATYPE_ARTIST);
287 ADD (info.album, EXTRACTOR_METATYPE_ALBUM);
288 ADD (info.year, EXTRACTOR_METATYPE_PUBLICATION_YEAR);
289 ADD (info.genre, EXTRACTOR_METATYPE_GENRE);
290 ADD (info.comment, EXTRACTOR_METATYPE_COMMENT);
291 if (info.track_number != 0)
292 {
293 snprintf (track, sizeof(track), "%u", info.track_number);
294 ADD (track, EXTRACTOR_METATYPE_TRACK_NUMBER);
295 }
296
297 if (info.title != NULL) free (info.title);
298 if (info.year != NULL) free (info.year);
299 if (info.album != NULL) free (info.album);
300 if (info.artist != NULL) free (info.artist);
301 if (info.comment != NULL) free (info.comment);
302
303 return 1;
304}
305
306/* end of id3_extractor.c */