Unify fourcc macro and some style changes
[kugel-rb.git] / apps / metadata / ape.c
blob44fc69a45abdc1315c863a693ccb7ca986294377
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 Dave Chapman
12 * 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.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <ctype.h>
25 #include <inttypes.h>
27 #include "system.h"
28 #include "metadata.h"
29 #include "metadata_common.h"
30 #include "metadata_parsers.h"
31 #include "structec.h"
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
38 struct apetag_header
40 char id[8];
41 long version;
42 long length;
43 long item_count;
44 long flags;
45 char reserved[8];
48 struct apetag_item_header
50 long length;
51 long flags;
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.
57 bool read_ape_tags(int fd, struct mp3entry* id3)
59 struct apetag_header header;
61 if ((lseek(fd, -APETAG_HEADER_LENGTH, SEEK_END) < 0)
62 || (ecread(fd, &header, 1, APETAG_HEADER_FORMAT, IS_BIG_ENDIAN)
63 != APETAG_HEADER_LENGTH)
64 || (memcmp(header.id, "APETAGEX", sizeof(header.id))))
66 return false;
69 if ((header.version == 2000) && (header.item_count > 0)
70 && (header.length > APETAG_HEADER_LENGTH))
72 char *buf = id3->id3v2buf;
73 unsigned int buf_remaining = sizeof(id3->id3v2buf)
74 + sizeof(id3->id3v1buf);
75 unsigned int tag_remaining = header.length - APETAG_HEADER_LENGTH;
76 int i;
78 if (lseek(fd, -header.length, SEEK_END) < 0)
80 return false;
83 for (i = 0; i < header.item_count; i++)
85 struct apetag_item_header item;
86 char name[TAG_NAME_LENGTH];
87 char value[TAG_VALUE_LENGTH];
88 long r;
90 if (tag_remaining < sizeof(item))
92 break;
95 if (ecread(fd, &item, 1, APETAG_ITEM_HEADER_FORMAT, IS_BIG_ENDIAN)
96 < (long) sizeof(item))
98 return false;
101 tag_remaining -= sizeof(item);
102 r = read_string(fd, name, sizeof(name), 0, tag_remaining);
104 if (r == -1)
106 return false;
109 tag_remaining -= r + item.length;
111 if ((item.flags & APETAG_ITEM_TYPE_MASK) == 0)
113 long len;
115 if (read_string(fd, value, sizeof(value), -1, item.length)
116 != item.length)
118 return false;
121 len = parse_tag(name, value, id3, buf, buf_remaining,
122 TAGTYPE_APE);
123 buf += len;
124 buf_remaining -= len;
126 else
128 if (lseek(fd, item.length, SEEK_CUR) < 0)
130 return false;
136 return true;