Directory.cxx 5.56 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2019 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 35 36 37
UPnPDirContent::~UPnPDirContent()
{
	/* this destructor exists here just so it won't get inlined */
}

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

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

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

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

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

70
	return SignedSongTime::FromS((((hours * 60) + minutes) * 60) + seconds);
71 72
}

73 74 75 76 77 78
/**
 * 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
79
static std::string &&
80
TitleToPathSegment(std::string &&s) noexcept
81 82
{
	std::replace(s.begin(), s.end(), '/', '_');
83
	return std::move(s);
84 85
}

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

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

	/**
	 * 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;

110
	UPnPDirObject object;
111
	TagBuilder tag;
112 113

public:
114 115
	UPnPDirParser(UPnPDirContent &_directory)
		:directory(_directory),
116
		 state(NONE),
117
		 tag_type(TAG_NUM_OF_ITEM_TYPES)
118
	{
119
		object.Clear();
120 121 122
	}

protected:
123
	void StartElement(const XML_Char *name, const XML_Char **attrs) override
124
	{
125
		if (object.type != UPnPDirObject::Type::UNKNOWN &&
126 127 128 129 130 131 132 133
		    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);
		}

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

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

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

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

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

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

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

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

				state = RES;
177 178
			}

179
			break;
180 181 182 183

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

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

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

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

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

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

208
		state = NONE;
209 210
	}

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

216
			value.append(s, len);
217 218 219
			return;
		}

220 221 222
		switch (state) {
		case NONE:
			break;
223

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

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

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