IcyMetaDataParser.cxx 5.45 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
 * 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.
14 15 16 17
 *
 * 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.
18 19
 */

20
#include "IcyMetaDataParser.hxx"
21
#include "tag/Builder.hxx"
22
#include "util/AllocatedString.hxx"
23
#include "util/StringView.hxx"
24

25 26
#include <algorithm>

27 28 29
#include <assert.h>
#include <string.h>

30 31 32 33 34
#ifdef HAVE_ICU_CONVERTER

void
IcyMetaDataParser::SetCharset(const char *charset)
{
35
	icu_converter = IcuConverter::Create(charset);
36 37 38 39
}

#endif

40
void
41
IcyMetaDataParser::Reset() noexcept
42
{
43
	if (!IsDefined())
44 45
		return;

46
	if (data_rest == 0 && meta_size > 0)
47
		delete[] meta_data;
48

49
	tag.reset();
50

51 52
	data_rest = data_size;
	meta_size = 0;
53 54 55
}

size_t
56
IcyMetaDataParser::Data(size_t length) noexcept
57 58 59
{
	assert(length > 0);

60
	if (!IsDefined())
61 62
		return length;

63
	if (data_rest == 0)
64 65
		return 0;

66 67 68
	if (length >= data_rest) {
		length = data_rest;
		data_rest = 0;
69
	} else
70
		data_rest -= length;
71 72 73 74 75

	return length;
}

static void
76
icy_add_item(TagBuilder &tag, TagType type, StringView value) noexcept
77
{
78
	if (value.size >= 2 && value.front() == '\'' && value.back() == '\'') {
79
		/* strip the single quotes */
80 81
		++value.data;
		value.size -= 2;
82 83
	}

84 85
	if (value.size > 0)
		tag.AddItem(type, value);
86 87 88
}

static void
89
icy_parse_tag_item(TagBuilder &tag,
90 91 92
#ifdef HAVE_ICU_CONVERTER
		   const IcuConverter *icu_converter,
#endif
93
		   const char *name, const char *value) noexcept
94
{
95 96 97 98 99 100 101 102 103 104 105 106 107
	if (strcmp(name, "StreamTitle") == 0) {
#ifdef HAVE_ICU_CONVERTER
		if (icu_converter != nullptr) {
			try {
				icy_add_item(tag, TAG_TITLE,
					     icu_converter->ToUTF8(value).c_str());
			} catch (...) {
			}

			return;
		}
#endif

108
		icy_add_item(tag, TAG_TITLE, value);
109
	}
110 111 112 113 114 115 116 117
}

/**
 * Find a single quote that is followed by a semicolon (or by the end
 * of the string).  If that fails, return the first single quote.  If
 * that also fails, return #end.
 */
static char *
118
find_end_quote(char *p, char *const end) noexcept
119 120 121 122
{
	char *fallback = std::find(p, end, '\'');
	if (fallback >= end - 1 || fallback[1] == ';')
		return fallback;
123

124 125 126 127 128
	p = fallback + 1;
	while (true) {
		p = std::find(p, end, '\'');
		if (p == end)
			return fallback;
129

130 131 132 133 134
		if (p == end - 1 || p[1] == ';')
			return p;

		++p;
	}
135 136
}

137
static std::unique_ptr<Tag>
138 139 140 141 142
icy_parse_tag(
#ifdef HAVE_ICU_CONVERTER
	      const IcuConverter *icu_converter,
#endif
	      char *p, char *const end) noexcept
143
{
144 145 146 147
	assert(p != nullptr);
	assert(end != nullptr);
	assert(p <= end);

148 149
	TagBuilder tag;

150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
	while (p != end) {
		const char *const name = p;
		char *eq = std::find(p, end, '=');
		if (eq == end)
			break;

		*eq = 0;
		p = eq + 1;

		if (*p != '\'') {
			/* syntax error; skip to the next semicolon,
			   try to recover */
			char *semicolon = std::find(p, end, ';');
			if (semicolon == end)
				break;
			p = semicolon + 1;
			continue;
		}

		++p;

		const char *const value = p;
		char *quote = find_end_quote(p, end);
		if (quote == end)
			break;
175

176 177
		*quote = 0;
		p = quote + 1;
178

179 180 181 182 183
		icy_parse_tag_item(tag,
#ifdef HAVE_ICU_CONVERTER
				   icu_converter,
#endif
				   name, value);
184 185 186 187 188 189

		char *semicolon = std::find(p, end, ';');
		if (semicolon == end)
			break;
		p = semicolon + 1;
	}
190

191
	return tag.CommitNew();
192 193 194
}

size_t
195
IcyMetaDataParser::Meta(const void *data, size_t length) noexcept
196
{
197
	const unsigned char *p = (const unsigned char *)data;
198

199 200
	assert(IsDefined());
	assert(data_rest == 0);
201 202
	assert(length > 0);

203
	if (meta_size == 0) {
204 205
		/* read meta_size from the first byte of a meta
		   block */
206 207
		meta_size = *p++ * 16;
		if (meta_size == 0) {
208
			/* special case: no metadata */
209
			data_rest = data_size;
210 211 212 213 214 215 216 217 218
			return 1;
		}

		/* 1 byte was consumed (must be re-added later for the
		   return value */
		--length;

		/* initialize metadata reader, allocate enough
		   memory (+1 for the null terminator) */
219
		meta_position = 0;
220
		meta_data = new char[meta_size + 1];
221 222
	}

223
	assert(meta_position < meta_size);
224

225 226
	if (length > meta_size - meta_position)
		length = meta_size - meta_position;
227

228 229
	memcpy(meta_data + meta_position, p, length);
	meta_position += length;
230 231 232 233 234

	if (p != data)
		/* re-add the first byte (which contained meta_size) */
		++length;

235
	if (meta_position == meta_size) {
236 237
		/* parse */

238 239 240 241 242
		tag = icy_parse_tag(
#ifdef HAVE_ICU_CONVERTER
				    icu_converter.get(),
#endif
				    meta_data, meta_data + meta_size);
243
		delete[] meta_data;
244 245 246

		/* change back to normal data mode */

247 248
		meta_size = 0;
		data_rest = data_size;
249 250 251 252
	}

	return length;
}
253 254

size_t
255
IcyMetaDataParser::ParseInPlace(void *data, size_t length) noexcept
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
{
	uint8_t *const dest0 = (uint8_t *)data;
	uint8_t *dest = dest0;
	const uint8_t *src = dest0;

	while (length > 0) {
		size_t chunk = Data(length);
		if (chunk > 0) {
			memmove(dest, src, chunk);
			dest += chunk;
			src += chunk;
			length -= chunk;

			if (length == 0)
				break;
		}

		chunk = Meta(src, length);
		if (chunk > 0) {
			src += chunk;
			length -= chunk;
		}
	}

	return dest - dest0;
}