Id3Load.cxx 5.59 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 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
 * 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 "Log.hxx"
#include "Riff.hxx"
#include "Aiff.hxx"
24
#include "input/InputStream.hxx"
25 26 27

#include <id3tag.h>

28 29
#include <algorithm>

30 31
static constexpr size_t ID3V1_SIZE = 128;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

157 158 159
	offset_type offset = size - ID3V1_SIZE;

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

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

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

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

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

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

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

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

202
	std::unique_ptr<id3_byte_t[]> buffer(new id3_byte_t[size]);
203
	is.ReadFull(lock, buffer.get(), size);
204

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

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

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

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