summaryrefslogtreecommitdiffstats
path: root/lib/rbcodec/metadata/ape.c
blob: 1460e4aa952fc1247cd173c6f6a9b5b155a52676 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2005 Dave Chapman
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <inttypes.h>

#include "metadata.h"
#include "metadata_common.h"
#include "metadata_parsers.h"
#include "structec.h"

#define APETAG_HEADER_LENGTH        32
#define APETAG_HEADER_FORMAT        "8llll8"
#define APETAG_ITEM_HEADER_FORMAT   "ll"
#define APETAG_ITEM_TYPE_MASK       3

#ifdef HAVE_ALBUMART
/* The AA header consists of the pseudo filename "Album Cover (Front).ext"
 * whereas ".ext" is the file extension. For now ".jpg" and ".png" are
 * supported by this APE metadata parser. Therefore the length is 22. */
#define APETAG_AA_HEADER_LENGTH     22
#endif

struct apetag_header 
{
    char id[8];
    uint32_t version;
    uint32_t length;
    uint32_t item_count;
    uint32_t flags;
    char reserved[8];
};

struct apetag_item_header 
{
    int32_t length;
    uint32_t flags;
};

/* Read the items in an APEV2 tag. Only looks for a tag at the end of a 
 * file. Returns true if a tag was found and fully read, false otherwise.
 */
bool read_ape_tags(int fd, struct mp3entry* id3)
{
    struct apetag_header header;

    if ((lseek(fd, -APETAG_HEADER_LENGTH, SEEK_END) < 0)
        || (ecread(fd, &header, 1, APETAG_HEADER_FORMAT, IS_BIG_ENDIAN)
            != APETAG_HEADER_LENGTH)
        || (memcmp(header.id, "APETAGEX", sizeof(header.id))))
    {
        return false;
    }

    if ((header.version == 2000) && (header.item_count > 0)
        && (header.length > APETAG_HEADER_LENGTH)) 
    {
        char *buf = id3->id3v2buf;
        unsigned int buf_remaining = sizeof(id3->id3v2buf) 
            + sizeof(id3->id3v1buf);
        unsigned int tag_remaining = header.length - APETAG_HEADER_LENGTH;
        unsigned int i;

        if (lseek(fd, -((int)header.length), SEEK_END) < 0)
        {
            return false;
        }

        for (i = 0; i < header.item_count; i++)
        {
            struct apetag_item_header item;
            char name[TAG_NAME_LENGTH];
            char value[TAG_VALUE_LENGTH];
            long r;

            if (tag_remaining < sizeof(item))
            {
                break;
            }
            
            if (ecread(fd, &item, 1, APETAG_ITEM_HEADER_FORMAT, IS_BIG_ENDIAN)
                < (long) sizeof(item))
            {
                return false;
            }
            
            tag_remaining -= sizeof(item);
            r = read_string(fd, name, sizeof(name), 0, tag_remaining);
            
            if (r == -1)
            {
                return false;
            }

            tag_remaining -= r + item.length;

            if ((item.flags & APETAG_ITEM_TYPE_MASK) == 0) 
            {
                long len;

                if (read_string(fd, value, sizeof(value), -1, item.length) 
                    != item.length)
                {
                    return false;
                }

                if (!strcasecmp(name, "cuesheet"))
                {
                    id3->has_embedded_cuesheet = true;
                    id3->embedded_cuesheet.pos = lseek(fd, 0, SEEK_CUR)-item.length;
                    id3->embedded_cuesheet.size = item.length;
                    id3->embedded_cuesheet.encoding = CHAR_ENC_UTF_8;
                }
                else
                {
                    len = parse_tag(name, value, id3, buf, buf_remaining,
                    TAGTYPE_APE);
                    buf += len;
                    buf_remaining -= len;
                }
            }
            else
            {
#ifdef HAVE_ALBUMART
                if (strcasecmp(name, "cover art (front)") == 0)
                {
                    /* Allow to read at least APETAG_AA_HEADER_LENGTH bytes. */
                    r = read_string(fd, name, sizeof(name), 0, APETAG_AA_HEADER_LENGTH);
                    if (r == -1)
                    {
                        return false;
                    }

                    /* Gather the album art format from the pseudo file name's ending. */
                    strcpy(name, name + strlen(name) - 4);
                    id3->albumart.type = AA_TYPE_UNKNOWN;
                    if      (strcasecmp(name, ".jpg") == 0)
                    {
                        id3->albumart.type = AA_TYPE_JPG;
                    }
                    else if (strcasecmp(name, ".png") == 0)
                    {
                        id3->albumart.type = AA_TYPE_PNG;
                    }

                    /* Set the album art size and position. */
                    if (id3->albumart.type != AA_TYPE_UNKNOWN)
                    {
                        id3->albumart.pos  = lseek(fd, 0, SEEK_CUR);
                        id3->albumart.size = item.length - r;
                        id3->has_embedded_albumart = true;
                    }
                    
                    /* Seek back to this APE items begin. */
                    if (lseek(fd, -r, SEEK_CUR) < 0)
                    {
                        return false;
                    }
                }
#endif
                /* Seek to the next APE item. */
                if (lseek(fd, item.length, SEEK_CUR) < 0)
                {
                    return false;
                }
            }
        }
    }

    return true;
}