summaryrefslogtreecommitdiff
path: root/src/tag/Id3Picture.cxx
blob: a5a351e0c5d5d971fc704be363c295b03dbd6cc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
 * Copyright 2003-2020 The Music Player Daemon Project
 * 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 "Id3Picture.hxx"
#include "Handler.hxx"
#include "util/ByteOrder.hxx"
#include "util/ConstBuffer.hxx"
#include "util/StringView.hxx"

#include <cstdint>
#include <string>

static StringView
ReadString(ConstBuffer<uint8_t> &src) noexcept
{
	if (src.size < 4)
		return nullptr;

	const size_t length = FromBE32(*(const uint32_t *)src.data);
	src.skip_front(4);

	if (src.size < length)
		return nullptr;

	StringView result((const char *)src.data, length);
	src.skip_front(length);
	return result;
}

void
ScanId3Apic(ConstBuffer<void> _buffer, TagHandler &handler) noexcept
{
	auto buffer = ConstBuffer<uint8_t>::FromVoid(_buffer);
	if (buffer.size < 4)
		return;

	buffer.skip_front(4); /* picture type */

	const auto mime_type = ReadString(buffer);
	if (mime_type.IsNull())
		return;

	/* description */
	if (ReadString(buffer).IsNull())
		return;

	if (buffer.size < 20)
		return;

	buffer.skip_front(16);

	const size_t image_size = FromBE32(*(const uint32_t *)buffer.data);
	buffer.skip_front(4);

	if (buffer.size < image_size)
		return;

	ConstBuffer<void> image(buffer.data, image_size);

	// TODO: don't copy MIME type, pass StringView to TagHandler::OnPicture()
	handler.OnPicture(std::string(mime_type.data, mime_type.size).c_str(),
			  image);
}