Id3Load.cxx 5.4 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 * 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"
#include "Id3Load.hxx"
#include "util/Error.hxx"
#include "util/Domain.hxx"
#include "Log.hxx"
#include "Riff.hxx"
#include "Aiff.hxx"
27
#include "input/InputStream.hxx"
28 29 30

#include <id3tag.h>

31 32
#include <algorithm>

33 34
static constexpr Domain id3_domain("id3");

35 36
static constexpr size_t ID3V1_SIZE = 128;

37 38 39 40 41 42 43 44
gcc_pure
static inline bool
tag_is_id3v1(struct id3_tag *tag)
{
	return (id3_tag_options(tag, 0, 0) & ID3_TAG_OPTION_ID3V1) != 0;
}

static long
45
get_id3v2_footer_size(InputStream &is, offset_type offset)
46 47
{
	id3_byte_t buf[ID3_TAG_QUERYSIZE];
48 49 50 51 52 53 54
	if (!is.Seek(offset, IgnoreError()))
		return 0;

	if (!is.ReadFull(buf, sizeof(buf), IgnoreError()))
		return 0;

	return id3_tag_query(buf, sizeof(buf));
55 56
}

57
static UniqueId3Tag
58
ReadId3Tag(InputStream &is)
59 60
{
	id3_byte_t query_buffer[ID3_TAG_QUERYSIZE];
61
	if (!is.ReadFull(query_buffer, sizeof(query_buffer), IgnoreError()))
62 63 64
		return nullptr;

	/* Look for a tag header */
65
	long tag_size = id3_tag_query(query_buffer, sizeof(query_buffer));
66 67 68
	if (tag_size <= 0) return nullptr;

	/* Found a tag.  Allocate a buffer and read it in. */
69
	if (size_t(tag_size) <= sizeof(query_buffer))
70 71 72
		/* we have enough data already */
		return UniqueId3Tag(id3_tag_parse(query_buffer, tag_size));

73
	std::unique_ptr<id3_byte_t[]> tag_buffer(new id3_byte_t[tag_size]);
74 75 76

	/* copy the start of the tag we already have to the allocated
	   buffer */
77
	id3_byte_t *end = std::copy_n(query_buffer, sizeof(query_buffer),
78 79 80
				      tag_buffer.get());

	/* now read the remaining bytes */
81 82
	const size_t remaining = tag_size - sizeof(query_buffer);
	const size_t nbytes = is.Read(end, remaining, IgnoreError());
83
	if (nbytes != remaining)
84 85
		return nullptr;

86
	return UniqueId3Tag(id3_tag_parse(tag_buffer.get(), tag_size));
87 88
}

89
static UniqueId3Tag
90
ReadId3Tag(InputStream &is, offset_type offset)
91 92 93 94 95 96 97 98 99
{
	if (!is.Seek(offset, IgnoreError()))
		return nullptr;

	return ReadId3Tag(is);
}

static UniqueId3Tag
ReadId3v1Tag(InputStream &is)
100 101 102
{
	id3_byte_t buffer[ID3V1_SIZE];

103
	if (is.Read(buffer, ID3V1_SIZE, IgnoreError()) != ID3V1_SIZE)
104 105 106 107 108
		return nullptr;

	return UniqueId3Tag(id3_tag_parse(buffer, ID3V1_SIZE));
}

109
static UniqueId3Tag
110
ReadId3v1Tag(InputStream &is, offset_type offset)
111
{
112 113
	if (!is.Seek(offset, IgnoreError()))
		return nullptr;
114

115
	return ReadId3v1Tag(is);
116 117
}

118
static UniqueId3Tag
119
tag_id3_find_from_beginning(InputStream &is)
120
{
121
	auto tag = ReadId3Tag(is);
122 123
	if (!tag) {
		return nullptr;
124
	} else if (tag_is_id3v1(tag.get())) {
125 126 127 128 129 130
		/* id3v1 tags don't belong here */
		return nullptr;
	}

	/* We have an id3v2 tag, so let's look for SEEK frames */
	id3_frame *frame;
131
	while ((frame = id3_tag_findframe(tag.get(), "SEEK", 0))) {
132 133 134 135 136 137
		/* Found a SEEK frame, get it's value */
		int seek = id3_field_getint(id3_frame_field(frame, 0));
		if (seek < 0)
			break;

		/* Get the tag specified by the SEEK frame */
138
		auto seektag = ReadId3Tag(is, is.GetOffset() + seek);
139
		if (!seektag || tag_is_id3v1(seektag.get()))
140 141 142
			break;

		/* Replace the old tag with the new one */
143
		tag = std::move(seektag);
144 145 146 147 148
	}

	return tag;
}

149
static UniqueId3Tag
150
tag_id3_find_from_end(InputStream &is)
151
{
152 153
	if (!is.KnownSize() || !is.CheapSeeking())
		return nullptr;
154

155 156
	const offset_type size = is.GetSize();
	if (size < ID3V1_SIZE)
157 158
		return nullptr;

159 160 161 162
	offset_type offset = size - ID3V1_SIZE;

	/* Get an id3v1 tag from the end of file for later use */
	auto v1tag = ReadId3v1Tag(is, offset);
163
	if (!v1tag)
164
		offset = size;
165 166

	/* Get the id3v2 tag size from the footer (located before v1tag) */
167 168 169 170 171 172 173 174 175 176
	if (offset < ID3_TAG_QUERYSIZE)
		return v1tag;

	long tag_offset =
		get_id3v2_footer_size(is, offset - ID3_TAG_QUERYSIZE);
	if (tag_offset >= 0)
		return v1tag;

	offset_type tag_size = -tag_offset;
	if (tag_size > offset)
177 178 179
		return v1tag;

	/* Get the tag which the footer belongs to */
180
	auto tag = ReadId3Tag(is, offset - tag_size);
181 182 183 184 185 186 187
	if (!tag)
		return v1tag;

	/* We have an id3v2 tag, so ditch v1tag */
	return tag;
}

188
static UniqueId3Tag
189
tag_id3_riff_aiff_load(InputStream &is)
190
{
191
	size_t size = riff_seek_id3(is);
192
	if (size == 0)
193
		size = aiff_seek_id3(is);
194 195 196 197 198 199 200
	if (size == 0)
		return nullptr;

	if (size > 4 * 1024 * 1024)
		/* too large, don't allocate so much memory */
		return nullptr;

201
	std::unique_ptr<id3_byte_t[]> buffer(new id3_byte_t[size]);
202
	if (!is.ReadFull(buffer.get(), size, IgnoreError())) {
203 204 205 206
		LogWarning(id3_domain, "Failed to read RIFF chunk");
		return nullptr;
	}

207
	return UniqueId3Tag(id3_tag_parse(buffer.get(), size));
208 209
}

210
UniqueId3Tag
211
tag_id3_load(InputStream &is)
212
{
213
	const ScopeLock protect(is.mutex);
214

215 216 217
	auto tag = tag_id3_find_from_beginning(is);
	if (tag == nullptr && is.CheapSeeking()) {
		tag = tag_id3_riff_aiff_load(is);
218
		if (tag == nullptr)
219
			tag = tag_id3_find_from_end(is);
220 221 222 223
	}

	return tag;
}