ApeLoader.cxx 2.74 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * 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.
 *
 * 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.
 */

#include "config.h"
Max Kellermann's avatar
Max Kellermann committed
21
#include "ApeLoader.hxx"
22
#include "system/ByteOrder.hxx"
23
#include "fs/FileSystem.hxx"
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

#include <stdint.h>
#include <assert.h>
#include <stdio.h>
#include <string.h>

struct ape_footer {
	unsigned char id[8];
	uint32_t version;
	uint32_t length;
	uint32_t count;
	unsigned char flags[4];
	unsigned char reserved[8];
};

static bool
Max Kellermann's avatar
Max Kellermann committed
40
ape_scan_internal(FILE *fp, ApeTagCallback callback)
41 42 43 44 45 46
{
	/* determine if file has an apeV2 tag */
	struct ape_footer footer;
	if (fseek(fp, -(long)sizeof(footer), SEEK_END) ||
	    fread(&footer, 1, sizeof(footer), fp) != sizeof(footer) ||
	    memcmp(footer.id, "APETAGEX", sizeof(footer.id)) != 0 ||
47
	    FromLE32(footer.version) != 2000)
48 49 50
		return false;

	/* find beginning of ape tag */
51
	size_t remaining = FromLE32(footer.length);
52 53 54 55 56 57 58 59 60 61
	if (remaining <= sizeof(footer) + 10 ||
	    /* refuse to load more than one megabyte of tag data */
	    remaining > 1024 * 1024 ||
	    fseek(fp, -(long)remaining, SEEK_END))
		return false;

	/* read tag into buffer */
	remaining -= sizeof(footer);
	assert(remaining > 10);

62
	char *buffer = new char[remaining];
63
	if (fread(buffer, 1, remaining, fp) != remaining) {
64
		delete[] buffer;
65
		return false;
66
	}
67 68

	/* read tags */
69
	unsigned n = FromLE32(footer.count);
70 71
	const char *p = buffer;
	while (n-- && remaining > 10) {
72
		size_t size = FromLE32(*(const uint32_t *)p);
73 74
		p += 4;
		remaining -= 4;
75
		unsigned long flags = FromLE32(*(const uint32_t *)p);
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
		p += 4;
		remaining -= 4;

		/* get the key */
		const char *key = p;
		while (remaining > size && *p != '\0') {
			p++;
			remaining--;
		}
		p++;
		remaining--;

		/* get the value */
		if (remaining < size)
			break;

Max Kellermann's avatar
Max Kellermann committed
92
		if (!callback(flags, key, p, size))
93 94 95 96 97 98
			break;

		p += size;
		remaining -= size;
	}

99
	delete[] buffer;
100 101 102 103
	return true;
}

bool
104
tag_ape_scan(Path path_fs, ApeTagCallback callback)
105
{
106
	FILE *fp = FOpen(path_fs, "rb");
Max Kellermann's avatar
Max Kellermann committed
107
	if (fp == nullptr)
108 109
		return false;

Max Kellermann's avatar
Max Kellermann committed
110
	bool success = ape_scan_internal(fp, callback);
111 112 113
	fclose(fp);
	return success;
}