comparison src/libaudtag/ape/ape.c @ 4887:0ddbd0025174 default tip

added libaudtag. (not used yet.)
author Yoshiki Yazawa <yaz@honeyplanet.jp>
date Wed, 05 May 2010 18:26:06 +0900
parents
children
comparison
equal deleted inserted replaced
4886:54b4f7aaca24 4887:0ddbd0025174
1 /*
2 * Copyright 2010 John Lindgren
3 *
4 * This file is part of Audacious.
5 *
6 * Audacious is free software: you can redistribute it and/or modify it under
7 * the terms of the GNU General Public License as published by the Free Software
8 * Foundation, version 3 of the License.
9 *
10 * Audacious is distributed in the hope that it will be useful, but WITHOUT ANY
11 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * Audacious. If not, see <http://www.gnu.org/licenses/>.
16 *
17 * The Audacious team does not consider modular code linking to Audacious or
18 * using our public API to be a derived work.
19 */
20
21 /* TODO:
22 * - ReplayGain info
23 * - Support updating files that have their tag at the beginning?
24 */
25
26 #include <glib.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include <audlegacy/vfs.h>
32
33 #include "ape.h"
34
35 #define DEBUG(...) fprintf (stderr, "APE: " __VA_ARGS__)
36
37 typedef struct
38 {
39 gchar magic[8];
40 guint32 version; /* LE */
41 guint32 length; /* LE */
42 guint32 items; /* LE */
43 guint32 flags; /* LE */
44 guint64 reserved;
45 }
46 APEHeader;
47
48 typedef struct
49 {
50 gchar * key, * value;
51 }
52 ValuePair;
53
54 #define APE_FLAG_HAS_HEADER (1 << 31)
55 #define APE_FLAG_HAS_NO_FOOTER (1 << 30)
56 #define APE_FLAG_IS_HEADER (1 << 29)
57
58 static gboolean ape_read_header (VFSFile * handle, APEHeader * header)
59 {
60 if (vfs_fread (header, 1, sizeof (APEHeader), handle) != sizeof (APEHeader))
61 return FALSE;
62
63 if (strncmp (header->magic, "APETAGEX", 8))
64 return FALSE;
65
66 header->version = GUINT32_FROM_LE (header->version);
67 header->length = GUINT32_FROM_LE (header->length);
68 header->items = GUINT32_FROM_LE (header->items);
69 header->flags = GUINT32_FROM_LE (header->flags);
70
71 if (header->length < sizeof (APEHeader))
72 return FALSE;
73
74 return TRUE;
75 }
76
77 static gboolean ape_find_header (VFSFile * handle, APEHeader * header, gint *
78 start, gint * length, gint * data_start, gint * data_length)
79 {
80 APEHeader secondary;
81
82 if (vfs_fseek (handle, 0, SEEK_SET))
83 return FALSE;
84
85 if (ape_read_header (handle, header))
86 {
87 DEBUG ("Found header at 0, length = %d, version = %d.\n", (gint)
88 header->length, (gint) header->version);
89 * start = 0;
90 * length = header->length;
91 * data_start = sizeof (APEHeader);
92 * data_length = header->length - sizeof (APEHeader);
93
94 if (! (header->flags & APE_FLAG_HAS_HEADER) || ! (header->flags &
95 APE_FLAG_IS_HEADER))
96 {
97 DEBUG ("Invalid header flags (%u).\n", (guint) header->flags);
98 return FALSE;
99 }
100
101 if (! (header->flags & APE_FLAG_HAS_NO_FOOTER))
102 {
103 if (vfs_fseek (handle, header->length, SEEK_CUR))
104 return FALSE;
105
106 if (! ape_read_header (handle, & secondary))
107 {
108 DEBUG ("Expected footer, but found none.\n");
109 return FALSE;
110 }
111
112 * length += sizeof (APEHeader);
113 }
114
115 return TRUE;
116 }
117
118 if (vfs_fseek (handle, -sizeof (APEHeader), SEEK_END))
119 return FALSE;
120
121 if (ape_read_header (handle, header))
122 {
123 DEBUG ("Found footer at %d, length = %d, version = %d.\n", (gint)
124 vfs_ftell (handle) - (gint) sizeof (APEHeader), (gint) header->length,
125 (gint) header->version);
126 * start = vfs_ftell (handle) - header->length;
127 * length = header->length;
128 * data_start = vfs_ftell (handle) - header->length;
129 * data_length = header->length - sizeof (APEHeader);
130
131 if ((header->flags & APE_FLAG_HAS_NO_FOOTER) || (header->flags &
132 APE_FLAG_IS_HEADER))
133 {
134 DEBUG ("Invalid footer flags (%u).\n", (guint) header->flags);
135 return FALSE;
136 }
137
138 if (header->flags & APE_FLAG_HAS_HEADER)
139 {
140 if (vfs_fseek (handle, -(gint) header->length - sizeof (APEHeader),
141 SEEK_CUR))
142 return FALSE;
143
144 if (! ape_read_header (handle, & secondary))
145 {
146 DEBUG ("Expected header, but found none.\n");
147 return FALSE;
148 }
149
150 * start -= sizeof (APEHeader);
151 * length += sizeof (APEHeader);
152 }
153
154 return TRUE;
155 }
156
157 DEBUG ("No header found.\n");
158 return FALSE;
159 }
160
161 static gboolean ape_is_our_file (VFSFile * handle)
162 {
163 APEHeader header;
164 gint start, length, data_start, data_length;
165
166 return ape_find_header (handle, & header, & start, & length, & data_start,
167 & data_length);
168 }
169
170 static ValuePair * ape_read_item (void * * data, gint length)
171 {
172 guint32 * header = * data;
173 gchar * value;
174 ValuePair * pair;
175
176 if (length < 8)
177 {
178 DEBUG ("Expected item, but only %d bytes remain in tag.\n", length);
179 return NULL;
180 }
181
182 value = memchr ((gchar *) (* data) + 8, 0, length - 8);
183
184 if (value == NULL)
185 {
186 DEBUG ("Unterminated item key (max length = %d).\n", length - 8);
187 return NULL;
188 }
189
190 value ++;
191
192 if (header[0] > (gchar *) (* data) + length - value)
193 {
194 DEBUG ("Item value of length %d, but only %d bytes remain in tag.\n",
195 (gint) header[0], (gint) ((gchar *) (* data) + length - value));
196 return NULL;
197 }
198
199 pair = g_malloc (sizeof (ValuePair));
200 pair->key = g_strdup ((gchar *) (* data) + 8);
201 pair->value = g_strndup (value, header[0]);
202
203 * data = value + header[0];
204
205 return pair;
206 }
207
208 static GList * ape_read_tag (VFSFile * handle)
209 {
210 GList * list = NULL;
211 APEHeader header;
212 gint start, length, data_start, data_length;
213 void * data, * item;
214
215 if (! ape_find_header (handle, & header, & start, & length, & data_start,
216 & data_length))
217 return NULL;
218
219 if (vfs_fseek (handle, data_start, SEEK_SET))
220 return NULL;
221
222 data = g_malloc (data_length);
223
224 if (vfs_fread (data, 1, data_length, handle) != data_length)
225 {
226 g_free (data);
227 return NULL;
228 }
229
230 DEBUG ("Reading %d items:\n", header.items);
231 item = data;
232
233 while (header.items --)
234 {
235 ValuePair * pair = ape_read_item (& item, (gchar *) data + data_length -
236 (gchar *) item);
237
238 if (pair == NULL)
239 break;
240
241 DEBUG ("Read: %s = %s.\n", pair->key, pair->value);
242 list = g_list_prepend (list, pair);
243 }
244
245 g_free (data);
246 return g_list_reverse (list);
247 }
248
249 static void free_tag_list (GList * list)
250 {
251 while (list != NULL)
252 {
253 g_free (((ValuePair *) list->data)->key);
254 g_free (((ValuePair *) list->data)->value);
255 g_free (list->data);
256 list = g_list_delete_link (list, list);
257 }
258 }
259
260 static Tuple * ape_fill_tuple (Tuple * tuple, VFSFile * handle)
261 {
262 GList * list = ape_read_tag (handle), * node;
263
264 for (node = list; node != NULL; node = node->next)
265 {
266 gchar * key = ((ValuePair *) node->data)->key;
267 gchar * value = ((ValuePair *) node->data)->value;
268
269 if (! strcmp (key, "Artist"))
270 tuple_associate_string (tuple, FIELD_ARTIST, NULL, value);
271 else if (! strcmp (key, "Title"))
272 tuple_associate_string (tuple, FIELD_TITLE, NULL, value);
273 else if (! strcmp (key, "Album"))
274 tuple_associate_string (tuple, FIELD_ALBUM, NULL, value);
275 else if (! strcmp (key, "Comment"))
276 tuple_associate_string (tuple, FIELD_COMMENT, NULL, value);
277 else if (! strcmp (key, "Genre"))
278 tuple_associate_string (tuple, FIELD_GENRE, NULL, value);
279 else if (! strcmp (key, "Track"))
280 tuple_associate_int (tuple, FIELD_TRACK_NUMBER, NULL, atoi (value));
281 else if (! strcmp (key, "Date"))
282 tuple_associate_int (tuple, FIELD_YEAR, NULL, atoi (value));
283 }
284
285 free_tag_list (list);
286 return tuple;
287 }
288
289 static gboolean ape_write_item (VFSFile * handle, const gchar * key,
290 const gchar * value, int * written_length)
291 {
292 gint key_len = strlen (key) + 1;
293 gint value_len = strlen (value);
294 guint32 header[2];
295
296 DEBUG ("Write: %s = %s.\n", key, value);
297
298 header[0] = GUINT32_TO_LE (value_len);
299 header[1] = 0;
300
301 if (vfs_fwrite (header, 1, 8, handle) != 8)
302 return FALSE;
303
304 if (vfs_fwrite (key, 1, key_len, handle) != key_len)
305 return FALSE;
306
307 if (vfs_fwrite (value, 1, value_len, handle) != value_len)
308 return FALSE;
309
310 * written_length += 8 + key_len + value_len;
311 return TRUE;
312 }
313
314 static gboolean write_string_item (Tuple * tuple, int field, VFSFile * handle,
315 const gchar * key, int * written_length, int * written_items)
316 {
317 const gchar * value = tuple_get_string (tuple, field, NULL);
318
319 if (value == NULL)
320 return TRUE;
321
322 if (! ape_write_item (handle, key, value, written_length))
323 return FALSE;
324
325 (* written_items) ++;
326 return TRUE;
327 }
328
329 static gboolean write_integer_item (Tuple * tuple, int field, VFSFile * handle,
330 const gchar * key, int * written_length, int * written_items)
331 {
332 gint value = tuple_get_int (tuple, field, NULL);
333 gchar scratch[32];
334
335 if (! value)
336 return TRUE;
337
338 snprintf (scratch, sizeof scratch, "%d", value);
339
340 if (! ape_write_item (handle, key, scratch, written_length))
341 return FALSE;
342
343 (* written_items) ++;
344 return TRUE;
345 }
346
347 static gboolean write_header (gint data_length, gint items, gboolean is_header,
348 VFSFile * handle)
349 {
350 APEHeader header;
351
352 memcpy (header.magic, "APETAGEX", 8);
353 header.version = GUINT32_TO_LE (2000);
354 header.length = GUINT32_TO_LE (data_length + sizeof (APEHeader));
355 header.items = GUINT32_TO_LE (items);
356 header.flags = is_header ? GUINT32_TO_LE (APE_FLAG_HAS_HEADER |
357 APE_FLAG_IS_HEADER) : GUINT32_TO_LE (APE_FLAG_HAS_HEADER);
358 header.reserved = 0;
359
360 return vfs_fwrite (& header, 1, sizeof (APEHeader), handle) == sizeof
361 (APEHeader);
362 }
363
364 static gboolean ape_write_tag (Tuple * tuple, VFSFile * handle)
365 {
366 GList * list = ape_read_tag (handle), * node;
367 APEHeader header;
368 gint start, length, data_start, data_length, items;
369
370 if (! ape_find_header (handle, & header, & start, & length, & data_start,
371 & data_length))
372 goto ERROR;
373
374 if (start + length != vfs_fsize (handle))
375 {
376 DEBUG ("Writing tags is only supported at end of file.\n");
377 goto ERROR;
378 }
379
380 if (vfs_truncate (handle, start) || vfs_fseek (handle, start, SEEK_SET) ||
381 ! write_header (0, 0, TRUE, handle))
382 goto ERROR;
383
384 length = 0;
385 items = 0;
386
387 if (! write_string_item (tuple, FIELD_ARTIST, handle, "Artist", & length,
388 & items) || ! write_string_item (tuple, FIELD_TITLE, handle, "Title",
389 & length, & items) || ! write_string_item (tuple, FIELD_ALBUM, handle,
390 "Album", & length, & items) || ! write_string_item (tuple, FIELD_COMMENT,
391 handle, "Comment", & length, & items) || ! write_string_item (tuple,
392 FIELD_GENRE, handle, "Genre", & length, & items) || ! write_integer_item
393 (tuple, FIELD_TRACK_NUMBER, handle, "Track", & length, & items) ||
394 ! write_integer_item (tuple, FIELD_YEAR, handle, "Date", & length, & items))
395 goto ERROR;
396
397 for (node = list; node != NULL; node = node->next)
398 {
399 gchar * key = ((ValuePair *) node->data)->key;
400 gchar * value = ((ValuePair *) node->data)->value;
401
402 if (! strcmp (key, "Artist") || ! strcmp (key, "Title") || ! strcmp
403 (key, "Album") || ! strcmp (key, "Comment") || ! strcmp (key, "Genre")
404 || ! strcmp (key, "Track") || ! strcmp (key, "Date"))
405 continue;
406
407 if (! ape_write_item (handle, key, value, & length))
408 goto ERROR;
409
410 items ++;
411 }
412
413 DEBUG ("Wrote %d items, %d bytes.\n", items, length);
414
415 if (write_header (length, items, FALSE, handle) || vfs_fseek (handle, start,
416 SEEK_SET) || ! write_header (length, items, TRUE, handle))
417 goto ERROR;
418
419 free_tag_list (list);
420 return TRUE;
421
422 ERROR:
423 free_tag_list (list);
424 return FALSE;
425 }
426
427 const tag_module_t ape =
428 {
429 .name = "APE",
430 .can_handle_file = ape_is_our_file,
431 .populate_tuple_from_file = ape_fill_tuple,
432 .write_tuple_to_file = ape_write_tag,
433 };