Directory.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
 * 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/upnp/Util.hxx"
22
#include "lib/expat/ExpatParser.hxx"
23
#include "Tags.hxx"
24 25
#include "tag/Builder.hxx"
#include "tag/Table.hxx"
26
#include "util/NumberParser.hxx"
27
#include "util/StringView.hxx"
28

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

#include <string.h>

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

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

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

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

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

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

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

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

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

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

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

111
	UPnPDirObject object;
112
	TagBuilder tag;
113 114

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

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

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

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

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

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

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

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

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

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

				state = RES;
178 179
			}

180
			break;
181 182 183 184

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

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

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

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

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

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

209
		state = NONE;
210 211
	}

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

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

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

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

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

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