blob: 7e9100a50aec63e2a187b53cdf9df028cd5568e3 [file] [log] [blame]
Marcoen Hirschberg2175d1e2007-06-16 18:19:51 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2005 Dave Chapman
11 *
Daniel Stenberg2acc0ac2008-06-28 18:10:04 +000012 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
Marcoen Hirschberg2175d1e2007-06-16 18:19:51 +000016 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21#include <stdio.h>
22#include <string.h>
23#include <stdlib.h>
24#include <ctype.h>
25#include <inttypes.h>
26
27#include "system.h"
28#include "id3.h"
29#include "metadata_common.h"
Bertrik Sikken1273f952008-05-03 07:03:59 +000030#include "metadata_parsers.h"
Marcoen Hirschberg2175d1e2007-06-16 18:19:51 +000031#include "structec.h"
32
33#define APETAG_HEADER_LENGTH 32
34#define APETAG_HEADER_FORMAT "8llll8"
35#define APETAG_ITEM_HEADER_FORMAT "ll"
36#define APETAG_ITEM_TYPE_MASK 3
37
38struct apetag_header
39{
40 char id[8];
41 long version;
42 long length;
43 long item_count;
44 long flags;
45 char reserved[8];
46};
47
48struct apetag_item_header
49{
50 long length;
51 long flags;
52};
53
54/* Read the items in an APEV2 tag. Only looks for a tag at the end of a
55 * file. Returns true if a tag was found and fully read, false otherwise.
56 */
57bool read_ape_tags(int fd, struct mp3entry* id3)
58{
59 struct apetag_header header;
60
61 if ((lseek(fd, -APETAG_HEADER_LENGTH, SEEK_END) < 0)
62 || (ecread(fd, &header, 1, APETAG_HEADER_FORMAT, IS_BIG_ENDIAN) != APETAG_HEADER_LENGTH)
63 || (memcmp(header.id, "APETAGEX", sizeof(header.id))))
64 {
65 return false;
66 }
67
68 if ((header.version == 2000) && (header.item_count > 0)
69 && (header.length > APETAG_HEADER_LENGTH))
70 {
71 char *buf = id3->id3v2buf;
72 unsigned int buf_remaining = sizeof(id3->id3v2buf)
73 + sizeof(id3->id3v1buf);
74 unsigned int tag_remaining = header.length - APETAG_HEADER_LENGTH;
75 int i;
76
77 if (lseek(fd, -header.length, SEEK_END) < 0)
78 {
79 return false;
80 }
81
82 for (i = 0; i < header.item_count; i++)
83 {
84 struct apetag_item_header item;
85 char name[TAG_NAME_LENGTH];
86 char value[TAG_VALUE_LENGTH];
87 long r;
88
89 if (tag_remaining < sizeof(item))
90 {
91 break;
92 }
93
94 if (ecread(fd, &item, 1, APETAG_ITEM_HEADER_FORMAT, IS_BIG_ENDIAN) < (long) sizeof(item))
95 {
96 return false;
97 }
98
99 tag_remaining -= sizeof(item);
100 r = read_string(fd, name, sizeof(name), 0, tag_remaining);
101
102 if (r == -1)
103 {
104 return false;
105 }
106
107 tag_remaining -= r + item.length;
108
109 if ((item.flags & APETAG_ITEM_TYPE_MASK) == 0)
110 {
111 long len;
112
113 if (read_string(fd, value, sizeof(value), -1, item.length)
114 != item.length)
115 {
116 return false;
117 }
118
119 len = parse_tag(name, value, id3, buf, buf_remaining,
120 TAGTYPE_APE);
121 buf += len;
122 buf_remaining -= len;
123 }
124 else
125 {
126 if (lseek(fd, item.length, SEEK_CUR) < 0)
127 {
128 return false;
129 }
130 }
131 }
132 }
133
134 return true;
135}