2009-02-28 16:44:41 +01:00
|
|
|
/*
|
2010-01-01 05:55:13 +01:00
|
|
|
* Copyright (C) 2003-2010 The Music Player Daemon Project
|
2009-02-28 16:44:41 +01:00
|
|
|
* http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* 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 program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2009-02-28 16:44:41 +01:00
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2009-02-28 16:44:41 +01:00
|
|
|
#include "tag_ape.h"
|
|
|
|
#include "tag.h"
|
2010-05-30 22:39:09 +02:00
|
|
|
#include "tag_table.h"
|
2009-02-28 16:44:41 +01:00
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
|
2009-07-19 17:37:02 +02:00
|
|
|
#include <assert.h>
|
2009-02-28 16:44:41 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2010-05-30 22:39:09 +02:00
|
|
|
static const char *const ape_tag_names[TAG_NUM_OF_ITEM_TYPES] = {
|
2009-10-13 16:12:45 +02:00
|
|
|
[TAG_TITLE] = "title",
|
|
|
|
[TAG_ARTIST] = "artist",
|
2010-05-30 22:29:48 +02:00
|
|
|
[TAG_ARTIST_SORT] = "artistsort",
|
2009-10-13 16:12:45 +02:00
|
|
|
[TAG_ALBUM] = "album",
|
2010-05-30 22:29:48 +02:00
|
|
|
[TAG_ALBUM_ARTIST] = "album artist",
|
|
|
|
[TAG_ALBUM_ARTIST_SORT] = "albumartistsort",
|
2009-10-13 16:12:45 +02:00
|
|
|
[TAG_COMMENT] = "comment",
|
|
|
|
[TAG_GENRE] = "genre",
|
|
|
|
[TAG_TRACK] = "track",
|
2010-05-30 22:29:48 +02:00
|
|
|
[TAG_DATE] = "year",
|
|
|
|
[TAG_MUSICBRAINZ_ARTISTID] = "musicbrainz_artistid",
|
|
|
|
[TAG_MUSICBRAINZ_ALBUMID] = "musicbrainz_albumid",
|
|
|
|
[TAG_MUSICBRAINZ_ALBUMARTISTID] = "musicbrainz_albumartistid",
|
|
|
|
[TAG_MUSICBRAINZ_TRACKID] = "musicbrainz_trackid",
|
2009-07-19 17:59:35 +02:00
|
|
|
};
|
|
|
|
|
2010-05-31 08:22:53 +02:00
|
|
|
static enum tag_type
|
|
|
|
tag_ape_name_parse(const char *name)
|
|
|
|
{
|
|
|
|
return tag_table_lookup(ape_tag_names, name);
|
|
|
|
}
|
|
|
|
|
2009-07-19 17:59:36 +02:00
|
|
|
static struct tag *
|
|
|
|
tag_ape_import_item(struct tag *tag, unsigned long flags,
|
|
|
|
const char *key, const char *value, size_t value_length)
|
|
|
|
{
|
|
|
|
/* we only care about utf-8 text tags */
|
|
|
|
if ((flags & (0x3 << 1)) != 0)
|
|
|
|
return tag;
|
|
|
|
|
2010-05-31 08:22:53 +02:00
|
|
|
enum tag_type type = tag_ape_name_parse(key);
|
2010-05-30 22:39:09 +02:00
|
|
|
if (type == TAG_NUM_OF_ITEM_TYPES)
|
|
|
|
return tag;
|
|
|
|
|
|
|
|
if (tag == NULL)
|
|
|
|
tag = tag_new();
|
|
|
|
tag_add_item_n(tag, type, value, value_length);
|
2009-07-19 17:59:36 +02:00
|
|
|
|
|
|
|
return tag;
|
|
|
|
}
|
|
|
|
|
2009-02-28 16:44:41 +01:00
|
|
|
struct tag *
|
|
|
|
tag_ape_load(const char *file)
|
|
|
|
{
|
|
|
|
struct tag *ret = NULL;
|
|
|
|
FILE *fp;
|
|
|
|
int tagCount;
|
|
|
|
char *buffer = NULL;
|
|
|
|
char *p;
|
|
|
|
size_t tagLen;
|
|
|
|
size_t size;
|
|
|
|
unsigned long flags;
|
|
|
|
char *key;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
unsigned char id[8];
|
|
|
|
uint32_t version;
|
|
|
|
uint32_t length;
|
|
|
|
uint32_t tagCount;
|
|
|
|
unsigned char flags[4];
|
|
|
|
unsigned char reserved[8];
|
|
|
|
} footer;
|
|
|
|
|
2010-05-20 07:11:33 +02:00
|
|
|
fp = fopen(file, "rb");
|
2009-02-28 16:44:41 +01:00
|
|
|
if (!fp)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* determine if file has an apeV2 tag */
|
|
|
|
if (fseek(fp, 0, SEEK_END))
|
|
|
|
goto fail;
|
|
|
|
size = (size_t)ftell(fp);
|
|
|
|
if (fseek(fp, size - sizeof(footer), SEEK_SET))
|
|
|
|
goto fail;
|
|
|
|
if (fread(&footer, 1, sizeof(footer), fp) != sizeof(footer))
|
|
|
|
goto fail;
|
|
|
|
if (memcmp(footer.id, "APETAGEX", sizeof(footer.id)) != 0)
|
|
|
|
goto fail;
|
|
|
|
if (GUINT32_FROM_LE(footer.version) != 2000)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
/* find beginning of ape tag */
|
|
|
|
tagLen = GUINT32_FROM_LE(footer.length);
|
2009-07-19 17:37:02 +02:00
|
|
|
if (tagLen <= sizeof(footer) + 10)
|
2009-02-28 16:44:41 +01:00
|
|
|
goto fail;
|
2009-07-19 17:38:46 +02:00
|
|
|
if (tagLen > 1024 * 1024)
|
|
|
|
/* refuse to load more than one megabyte of tag data */
|
2009-02-28 16:44:41 +01:00
|
|
|
goto fail;
|
|
|
|
if (fseek(fp, size - tagLen, SEEK_SET))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
/* read tag into buffer */
|
|
|
|
tagLen -= sizeof(footer);
|
2009-07-19 17:37:02 +02:00
|
|
|
assert(tagLen > 10);
|
|
|
|
|
2009-02-28 16:44:41 +01:00
|
|
|
buffer = g_malloc(tagLen);
|
|
|
|
if (fread(buffer, 1, tagLen, fp) != tagLen)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
/* read tags */
|
|
|
|
tagCount = GUINT32_FROM_LE(footer.tagCount);
|
|
|
|
p = buffer;
|
|
|
|
while (tagCount-- && tagLen > 10) {
|
|
|
|
size = GUINT32_FROM_LE(*(const uint32_t *)p);
|
|
|
|
p += 4;
|
|
|
|
tagLen -= 4;
|
|
|
|
flags = GUINT32_FROM_LE(*(const uint32_t *)p);
|
|
|
|
p += 4;
|
|
|
|
tagLen -= 4;
|
|
|
|
|
|
|
|
/* get the key */
|
|
|
|
key = p;
|
2009-07-18 22:45:56 +02:00
|
|
|
while (tagLen > size && *p != '\0') {
|
2009-02-28 16:44:41 +01:00
|
|
|
p++;
|
|
|
|
tagLen--;
|
|
|
|
}
|
|
|
|
p++;
|
|
|
|
tagLen--;
|
|
|
|
|
|
|
|
/* get the value */
|
|
|
|
if (tagLen < size)
|
|
|
|
goto fail;
|
|
|
|
|
2009-07-19 17:59:36 +02:00
|
|
|
ret = tag_ape_import_item(ret, flags, key, p, size);
|
|
|
|
|
2009-02-28 16:44:41 +01:00
|
|
|
p += size;
|
|
|
|
tagLen -= size;
|
|
|
|
}
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (fp)
|
|
|
|
fclose(fp);
|
|
|
|
g_free(buffer);
|
|
|
|
return ret;
|
|
|
|
}
|