Id3Load.cxx 5.57 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2020 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
 * 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 "Id3Load.hxx"
#include "Riff.hxx"
#include "Aiff.hxx"
23
#include "input/InputStream.hxx"
24 25 26

#include <id3tag.h>

27 28
#include <algorithm>

29 30
static constexpr size_t ID3V1_SIZE = 128;

31 32
gcc_pure
static inline bool
33
tag_is_id3v1(struct id3_tag *tag) noexcept
34 35 36 37 38
{
	return (id3_tag_options(tag, 0, 0) & ID3_TAG_OPTION_ID3V1) != 0;
}

static long
39 40
get_id3v2_footer_size(InputStream &is, std::unique_lock<Mutex> &lock,
		      offset_type offset)
41
try {
42
	id3_byte_t buf[ID3_TAG_QUERYSIZE];
43 44
	is.Seek(lock, offset);
	is.ReadFull(lock, buf, sizeof(buf));
45 46

	return id3_tag_query(buf, sizeof(buf));
47
} catch (...) {
48
	return 0;
49 50
}

51
static UniqueId3Tag
52
ReadId3Tag(InputStream &is, std::unique_lock<Mutex> &lock)
53
try {
54
	id3_byte_t query_buffer[ID3_TAG_QUERYSIZE];
55
	is.ReadFull(lock, query_buffer, sizeof(query_buffer));
56 57

	/* Look for a tag header */
58
	long tag_size = id3_tag_query(query_buffer, sizeof(query_buffer));
59 60 61
	if (tag_size <= 0) return nullptr;

	/* Found a tag.  Allocate a buffer and read it in. */
62
	if (size_t(tag_size) <= sizeof(query_buffer))
63 64 65
		/* we have enough data already */
		return UniqueId3Tag(id3_tag_parse(query_buffer, tag_size));

66
	std::unique_ptr<id3_byte_t[]> tag_buffer(new id3_byte_t[tag_size]);
67 68 69

	/* copy the start of the tag we already have to the allocated
	   buffer */
70
	id3_byte_t *end = std::copy_n(query_buffer, sizeof(query_buffer),
71 72 73
				      tag_buffer.get());

	/* now read the remaining bytes */
74
	const size_t remaining = tag_size - sizeof(query_buffer);
75
	is.ReadFull(lock, end, remaining);
76

77
	return UniqueId3Tag(id3_tag_parse(tag_buffer.get(), tag_size));
78
} catch (...) {
79
	return nullptr;
80 81
}

82
static UniqueId3Tag
83
ReadId3Tag(InputStream &is, std::unique_lock<Mutex> &lock, offset_type offset)
84
try {
85
	is.Seek(lock, offset);
86

87
	return ReadId3Tag(is, lock);
88
} catch (...) {
89
	return nullptr;
90 91 92
}

static UniqueId3Tag
93
ReadId3v1Tag(InputStream &is, std::unique_lock<Mutex> &lock)
94
try {
95
	id3_byte_t buffer[ID3V1_SIZE];
96
	is.ReadFull(lock, buffer, ID3V1_SIZE);
97 98

	return UniqueId3Tag(id3_tag_parse(buffer, ID3V1_SIZE));
99
} catch (...) {
100
	return nullptr;
101 102
}

103
static UniqueId3Tag
104 105
ReadId3v1Tag(InputStream &is, std::unique_lock<Mutex> &lock,
	     offset_type offset)
106
try {
107 108
	is.Seek(lock, offset);
	return ReadId3v1Tag(is, lock);
109
} catch (...) {
110
	return nullptr;
111 112
}

113
static UniqueId3Tag
114
tag_id3_find_from_beginning(InputStream &is, std::unique_lock<Mutex> &lock)
115
try {
116
	auto tag = ReadId3Tag(is, lock);
117 118
	if (!tag) {
		return nullptr;
119
	} else if (tag_is_id3v1(tag.get())) {
120 121 122 123 124 125
		/* id3v1 tags don't belong here */
		return nullptr;
	}

	/* We have an id3v2 tag, so let's look for SEEK frames */
	id3_frame *frame;
126
	while ((frame = id3_tag_findframe(tag.get(), "SEEK", 0))) {
127 128 129 130 131 132
		/* 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 */
133
		auto seektag = ReadId3Tag(is, lock, is.GetOffset() + seek);
134
		if (!seektag || tag_is_id3v1(seektag.get()))
135 136 137
			break;

		/* Replace the old tag with the new one */
138
		tag = std::move(seektag);
139 140 141
	}

	return tag;
142
} catch (...) {
143
	return nullptr;
144 145
}

146
static UniqueId3Tag
147
tag_id3_find_from_end(InputStream &is, std::unique_lock<Mutex> &lock)
148
try {
149 150
	if (!is.KnownSize() || !is.CheapSeeking())
		return nullptr;
151

152 153
	const offset_type size = is.GetSize();
	if (size < ID3V1_SIZE)
154 155
		return nullptr;

156 157 158
	offset_type offset = size - ID3V1_SIZE;

	/* Get an id3v1 tag from the end of file for later use */
159
	auto v1tag = ReadId3v1Tag(is, lock, offset);
160
	if (!v1tag)
161
		offset = size;
162 163

	/* Get the id3v2 tag size from the footer (located before v1tag) */
164 165 166 167
	if (offset < ID3_TAG_QUERYSIZE)
		return v1tag;

	long tag_offset =
168
		get_id3v2_footer_size(is, lock, offset - ID3_TAG_QUERYSIZE);
169 170 171 172 173
	if (tag_offset >= 0)
		return v1tag;

	offset_type tag_size = -tag_offset;
	if (tag_size > offset)
174 175 176
		return v1tag;

	/* Get the tag which the footer belongs to */
177
	auto tag = ReadId3Tag(is, lock, offset - tag_size);
178 179 180 181 182
	if (!tag)
		return v1tag;

	/* We have an id3v2 tag, so ditch v1tag */
	return tag;
183
} catch (...) {
184
	return nullptr;
185 186
}

187
static UniqueId3Tag
188
tag_id3_riff_aiff_load(InputStream &is, std::unique_lock<Mutex> &lock)
189
try {
190 191
	size_t size;
	try {
192
		size = riff_seek_id3(is, lock);
193
	} catch (...) {
194
		size = aiff_seek_id3(is, lock);
195
	}
196 197 198 199 200

	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
	is.ReadFull(lock, buffer.get(), size);
203

204
	return UniqueId3Tag(id3_tag_parse(buffer.get(), size));
205
} catch (...) {
206
	return nullptr;
207 208
}

209
UniqueId3Tag
210
tag_id3_load(InputStream &is)
211
try {
212
	std::unique_lock<Mutex> lock(is.mutex);
213

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

	return tag;
222
} catch (...) {
223
	return nullptr;
224
}