Directory.cxx 5.61 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * 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 "config.h"
#include "Directory.hxx"
22
#include "lib/upnp/Util.hxx"
23
#include "lib/expat/ExpatParser.hxx"
24
#include "Tags.hxx"
25 26
#include "tag/Builder.hxx"
#include "tag/Table.hxx"
27
#include "util/NumberParser.hxx"
28
#include "util/StringView.hxx"
29

30
#include <algorithm>
31 32 33 34
#include <string>

#include <string.h>

35 36 37 38 39
UPnPDirContent::~UPnPDirContent()
{
	/* this destructor exists here just so it won't get inlined */
}

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

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

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

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

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

72
	return SignedSongTime::FromS((((hours * 60) + minutes) * 60) + seconds);
73 74
}

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

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

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

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

112
	UPnPDirObject object;
113
	TagBuilder tag;
114 115

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

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

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

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

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

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

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

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

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

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

				state = RES;
179 180
			}

181
			break;
182 183 184 185

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

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

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

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

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

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

210
		state = NONE;
211 212
	}

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

218
			value.append(s, len);
219 220 221
			return;
		}

222 223 224
		switch (state) {
		case NONE:
			break;
225

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

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

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