Directory.cxx 5.58 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 25 26
#include "Tags.hxx"
#include "tag/TagBuilder.hxx"
#include "tag/TagTable.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)
43
{
44
	if (name.EqualsLiteral("object.item.audioItem.musicTrack"))
45
		return UPnPDirObject::ItemClass::MUSIC;
46
	else if (name.EqualsLiteral("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)
55
{
56 57 58 59
	char *endptr;

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

	result *= 60;
	duration = endptr + 1;
	result += ParseUnsigned(duration, &endptr);
	if (endptr == duration || *endptr != ':')
66
		return SignedSongTime::Negative();
67 68 69 70 71

	result *= 60;
	duration = endptr + 1;
	result += ParseUnsigned(duration, &endptr);
	if (endptr == duration || *endptr != 0)
72
		return SignedSongTime::Negative();
73

74
	return SignedSongTime::FromS(result);
75 76
}

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

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

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

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

114
	UPnPDirObject object;
115
	TagBuilder tag;
116 117

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

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

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

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

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

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

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

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

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

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

				state = RES;
181 182
			}

183
			break;
184 185 186 187

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

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

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

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

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

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

212
		state = NONE;
213 214
	}

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

220
			value.append(s, len);
221 222 223
			return;
		}

224 225 226
		switch (state) {
		case NONE:
			break;
227

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

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

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