Directory.cxx 5.82 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Copyright (C) 2003-2014 The Music Player Daemon Project
 * 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

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 41 42 43 44 45 46
gcc_pure gcc_nonnull_all
static bool
CompareStringLiteral(const char *literal, const char *value, size_t length)
{
	return length == strlen(literal) &&
		memcmp(literal, value, length) == 0;
}

47 48
gcc_pure
static UPnPDirObject::ItemClass
49
ParseItemClass(const char *name, size_t length)
50
{
51 52
	if (CompareStringLiteral("object.item.audioItem.musicTrack",
				 name, length))
53
		return UPnPDirObject::ItemClass::MUSIC;
54 55
	else if (CompareStringLiteral("object.item.playlistItem",
				      name, length))
56 57 58 59 60
		return UPnPDirObject::ItemClass::PLAYLIST;
	else
		return UPnPDirObject::ItemClass::UNKNOWN;
}

61
gcc_pure
62
static SignedSongTime
63
ParseDuration(const char *duration)
64
{
65 66 67 68
	char *endptr;

	unsigned result = ParseUnsigned(duration, &endptr);
	if (endptr == duration || *endptr != ':')
69
		return SignedSongTime::Negative();
70 71 72 73 74

	result *= 60;
	duration = endptr + 1;
	result += ParseUnsigned(duration, &endptr);
	if (endptr == duration || *endptr != ':')
75
		return SignedSongTime::Negative();
76 77 78 79 80

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

83
	return SignedSongTime::FromS(result);
84 85
}

86 87 88 89 90 91
/**
 * 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
92
static std::string &&
93
TitleToPathSegment(std::string &&s)
94 95
{
	std::replace(s.begin(), s.end(), '/', '_');
96
	return std::move(s);
97 98
}

99 100 101 102
/**
 * An XML parser which builds directory contents from DIDL lite input.
 */
class UPnPDirParser final : public CommonExpatParser {
103
	UPnPDirContent &directory;
104

105 106 107 108 109
	enum {
		NONE,
		RES,
		CLASS,
	} state;
110 111 112 113 114 115 116 117 118 119 120 121 122

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

123
	UPnPDirObject object;
124
	TagBuilder tag;
125 126

public:
127 128
	UPnPDirParser(UPnPDirContent &_directory)
		:directory(_directory),
129
		 state(NONE),
130
		 tag_type(TAG_NUM_OF_ITEM_TYPES)
131
	{
132
		object.Clear();
133 134 135 136 137
	}

protected:
	virtual void StartElement(const XML_Char *name, const XML_Char **attrs)
	{
138
		if (object.type != UPnPDirObject::Type::UNKNOWN &&
139 140 141 142 143 144 145 146
		    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);
		}

147 148 149
		switch (name[0]) {
		case 'c':
			if (!strcmp(name, "container")) {
150 151
				object.Clear();
				object.type = UPnPDirObject::Type::CONTAINER;
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
		case 'i':
			if (!strcmp(name, "item")) {
165 166
				object.Clear();
				object.type = UPnPDirObject::Type::ITEM;
167 168 169

				const char *id = GetAttribute(attrs, "id");
				if (id != nullptr)
170
					object.id = id;
171 172 173

				const char *pid = GetAttribute(attrs, "parentID");
				if (pid != nullptr)
174
					object.parent_id = pid;
175 176
			}
			break;
177 178 179 180 181 182

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

184 185 186
				const char *duration =
					GetAttribute(attrs, "duration");
				if (duration != nullptr)
187
					tag.SetDuration(ParseDuration(duration));
188 189

				state = RES;
190 191
			}

192
			break;
193 194 195 196

		case 'u':
			if (strcmp(name, "upnp:class") == 0)
				state = CLASS;
197 198 199 200 201
		}
	}

	virtual void EndElement(const XML_Char *name)
	{
202
		if (tag_type != TAG_NUM_OF_ITEM_TYPES) {
203
			assert(object.type != UPnPDirObject::Type::UNKNOWN);
204 205 206 207

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

			if (tag_type == TAG_TITLE)
208
				object.name = TitleToPathSegment(std::move(value));
209 210 211 212 213 214

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

215
		if ((!strcmp(name, "container") || !strcmp(name, "item")) &&
216 217 218
		    object.Check()) {
			tag.Commit(object.tag);
			directory.objects.emplace_back(std::move(object));
219
		}
220

221
		state = NONE;
222 223 224 225
	}

	virtual void CharacterData(const XML_Char *s, int len)
	{
226
		if (tag_type != TAG_NUM_OF_ITEM_TYPES) {
227
			assert(object.type != UPnPDirObject::Type::UNKNOWN);
228

229
			value.append(s, len);
230 231 232
			return;
		}

233 234 235
		switch (state) {
		case NONE:
			break;
236

237
		case RES:
238
			object.url.assign(s, len);
239
			break;
240 241

		case CLASS:
242
			object.item_class = ParseItemClass(s, len);
243 244 245 246 247 248
			break;
		}
	}
};

bool
249
UPnPDirContent::parse(const char *input, Error &error)
250 251
{
	UPnPDirParser parser(*this);
252
	return parser.Parse(input, strlen(input), true, error);
253
}