Directory.cxx 5.58 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 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 "Directory.hxx"
21
#include "lib/expat/ExpatParser.hxx"
22
#include "Tags.hxx"
23 24
#include "tag/Builder.hxx"
#include "tag/Table.hxx"
25
#include "util/NumberParser.hxx"
26
#include "util/StringView.hxx"
27

28
#include <algorithm>
29 30 31 32
#include <string>

#include <string.h>

33 34
/* this destructor exists here just so it won't get inlined */
UPnPDirContent::~UPnPDirContent() = default;
35

36 37
gcc_pure
static UPnPDirObject::ItemClass
38
ParseItemClass(StringView name) noexcept
39
{
40
	if (name.Equals("object.item.audioItem.musicTrack"))
41
		return UPnPDirObject::ItemClass::MUSIC;
42
	else if (name.Equals("object.item.playlistItem"))
43 44 45 46 47
		return UPnPDirObject::ItemClass::PLAYLIST;
	else
		return UPnPDirObject::ItemClass::UNKNOWN;
}

48
gcc_pure
49
static SignedSongTime
50
ParseDuration(const char *duration) noexcept
51
{
52 53
	char *endptr;

54
	int hours = ParseInt(duration, &endptr);
55
	if (endptr == duration || *endptr != ':')
56
		return SignedSongTime::Negative();
57 58

	duration = endptr + 1;
59
	unsigned minutes = ParseUnsigned(duration, &endptr);
60
	if (endptr == duration || *endptr != ':')
61
		return SignedSongTime::Negative();
62 63

	duration = endptr + 1;
64 65
	double seconds = ParseDouble(duration, &endptr);
	if (endptr == duration || *endptr != 0 || seconds < 0.0)
66
		return SignedSongTime::Negative();
67

68
	return SignedSongTime::FromS((((hours * 60) + minutes) * 60) + seconds);
69 70
}

71 72 73 74 75 76
/**
 * Transform titles to turn '/' into '_' to make them acceptable path
 * elements. There is a very slight risk of collision in doing
 * this. Twonky returns directory names (titles) like 'Artist/Album'.
 */
gcc_pure
77
static std::string &&
78
TitleToPathSegment(std::string &&s) noexcept
79 80
{
	std::replace(s.begin(), s.end(), '/', '_');
81
	return std::move(s);
82 83
}

84 85 86 87
/**
 * An XML parser which builds directory contents from DIDL lite input.
 */
class UPnPDirParser final : public CommonExpatParser {
88
	UPnPDirContent &directory;
89

90 91 92 93 94
	enum {
		NONE,
		RES,
		CLASS,
	} state;
95 96 97 98 99 100 101 102 103 104 105 106 107

	/**
	 * If not equal to #TAG_NUM_OF_ITEM_TYPES, then we're
	 * currently reading an element containing a tag value.  The
	 * value is being constructed in #value.
	 */
	TagType tag_type;

	/**
	 * The text inside the current element.
	 */
	std::string value;

108
	UPnPDirObject object;
109
	TagBuilder tag;
110 111

public:
Max Kellermann's avatar
Max Kellermann committed
112
	explicit UPnPDirParser(UPnPDirContent &_directory)
113
		:directory(_directory),
114
		 state(NONE),
115
		 tag_type(TAG_NUM_OF_ITEM_TYPES)
116
	{
117
		object.Clear();
118 119 120
	}

protected:
121
	void StartElement(const XML_Char *name, const XML_Char **attrs) override
122
	{
123
		if (object.type != UPnPDirObject::Type::UNKNOWN &&
124 125 126 127 128 129 130 131
		    tag_type == TAG_NUM_OF_ITEM_TYPES) {
			tag_type = tag_table_lookup(upnp_tags, name);
			if (tag_type != TAG_NUM_OF_ITEM_TYPES)
				return;
		} else {
			assert(tag_type == TAG_NUM_OF_ITEM_TYPES);
		}

132 133 134
		switch (name[0]) {
		case 'c':
			if (!strcmp(name, "container")) {
135 136
				object.Clear();
				object.type = UPnPDirObject::Type::CONTAINER;
137 138 139

				const char *id = GetAttribute(attrs, "id");
				if (id != nullptr)
140
					object.id = id;
141 142 143

				const char *pid = GetAttribute(attrs, "parentID");
				if (pid != nullptr)
144
					object.parent_id = pid;
145 146
			}
			break;
147

148 149
		case 'i':
			if (!strcmp(name, "item")) {
150 151
				object.Clear();
				object.type = UPnPDirObject::Type::ITEM;
152 153 154

				const char *id = GetAttribute(attrs, "id");
				if (id != nullptr)
155
					object.id = id;
156 157 158

				const char *pid = GetAttribute(attrs, "parentID");
				if (pid != nullptr)
159
					object.parent_id = pid;
160 161
			}
			break;
162 163 164 165 166 167

		case 'r':
			if (!strcmp(name, "res")) {
				// <res protocolInfo="http-get:*:audio/mpeg:*" size="5171496"
				// bitrate="24576" duration="00:03:35" sampleFrequency="44100"
				// nrAudioChannels="2">
168

169 170 171
				const char *duration =
					GetAttribute(attrs, "duration");
				if (duration != nullptr)
172
					tag.SetDuration(ParseDuration(duration));
173 174

				state = RES;
175 176
			}

177
			break;
178 179 180 181

		case 'u':
			if (strcmp(name, "upnp:class") == 0)
				state = CLASS;
182 183 184
		}
	}

185
	void EndElement(const XML_Char *name) override
186
	{
187
		if (tag_type != TAG_NUM_OF_ITEM_TYPES) {
188
			assert(object.type != UPnPDirObject::Type::UNKNOWN);
189 190 191 192

			tag.AddItem(tag_type, value.c_str());

			if (tag_type == TAG_TITLE)
193
				object.name = TitleToPathSegment(std::move(value));
194 195 196 197 198 199

			value.clear();
			tag_type = TAG_NUM_OF_ITEM_TYPES;
			return;
		}

200
		if ((!strcmp(name, "container") || !strcmp(name, "item")) &&
201 202 203
		    object.Check()) {
			tag.Commit(object.tag);
			directory.objects.emplace_back(std::move(object));
204
		}
205

206
		state = NONE;
207 208
	}

209
	void CharacterData(const XML_Char *s, int len) override
210
	{
211
		if (tag_type != TAG_NUM_OF_ITEM_TYPES) {
212
			assert(object.type != UPnPDirObject::Type::UNKNOWN);
213

214
			value.append(s, len);
215 216 217
			return;
		}

218 219 220
		switch (state) {
		case NONE:
			break;
221

222
		case RES:
223
			object.url.assign(s, len);
224
			break;
225 226

		case CLASS:
227
			object.item_class = ParseItemClass(StringView(s, len));
228 229 230 231 232
			break;
		}
	}
};

233 234
void
UPnPDirContent::Parse(const char *input)
235 236
{
	UPnPDirParser parser(*this);
237
	parser.Parse(input, strlen(input), true);
238
}