2009-01-03 23:35:30 +01:00
|
|
|
/*
|
2020-01-18 19:22:19 +01:00
|
|
|
* Copyright 2003-2020 The Music Player Daemon Project
|
2009-01-03 23:35:30 +01:00
|
|
|
* 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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2009-01-03 23:35:30 +01:00
|
|
|
*/
|
|
|
|
|
2013-01-10 22:33:16 +01:00
|
|
|
#include "IcyMetaDataParser.hxx"
|
2017-02-08 08:26:58 +01:00
|
|
|
#include "tag/Builder.hxx"
|
2019-08-09 15:03:07 +02:00
|
|
|
#include "util/AllocatedString.hxx"
|
2015-09-30 22:03:01 +02:00
|
|
|
#include "util/StringView.hxx"
|
2009-01-03 23:35:30 +01:00
|
|
|
|
2019-07-05 09:59:00 +02:00
|
|
|
#include <algorithm>
|
2020-03-12 23:20:59 +01:00
|
|
|
#include <cassert>
|
2019-07-05 09:59:00 +02:00
|
|
|
|
2009-01-03 23:35:30 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
2019-08-09 15:03:07 +02:00
|
|
|
#ifdef HAVE_ICU_CONVERTER
|
|
|
|
|
|
|
|
void
|
|
|
|
IcyMetaDataParser::SetCharset(const char *charset)
|
|
|
|
{
|
2019-11-04 15:44:06 +01:00
|
|
|
icu_converter = IcuConverter::Create(charset);
|
2019-08-09 15:03:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2009-01-03 23:35:30 +01:00
|
|
|
void
|
2017-12-20 15:05:14 +01:00
|
|
|
IcyMetaDataParser::Reset() noexcept
|
2009-01-03 23:35:30 +01:00
|
|
|
{
|
2013-01-10 22:33:16 +01:00
|
|
|
if (!IsDefined())
|
2009-01-03 23:35:30 +01:00
|
|
|
return;
|
|
|
|
|
2013-01-10 22:33:16 +01:00
|
|
|
if (data_rest == 0 && meta_size > 0)
|
2013-12-04 15:03:39 +01:00
|
|
|
delete[] meta_data;
|
2009-01-03 23:35:30 +01:00
|
|
|
|
2017-12-20 15:06:33 +01:00
|
|
|
tag.reset();
|
2009-01-03 23:35:30 +01:00
|
|
|
|
2013-01-10 22:33:16 +01:00
|
|
|
data_rest = data_size;
|
|
|
|
meta_size = 0;
|
2009-01-03 23:35:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
2017-12-20 15:05:14 +01:00
|
|
|
IcyMetaDataParser::Data(size_t length) noexcept
|
2009-01-03 23:35:30 +01:00
|
|
|
{
|
|
|
|
assert(length > 0);
|
|
|
|
|
2013-01-10 22:33:16 +01:00
|
|
|
if (!IsDefined())
|
2009-01-03 23:55:03 +01:00
|
|
|
return length;
|
|
|
|
|
2013-01-10 22:33:16 +01:00
|
|
|
if (data_rest == 0)
|
2009-01-03 23:35:30 +01:00
|
|
|
return 0;
|
|
|
|
|
2013-01-10 22:33:16 +01:00
|
|
|
if (length >= data_rest) {
|
|
|
|
length = data_rest;
|
|
|
|
data_rest = 0;
|
2009-01-03 23:35:30 +01:00
|
|
|
} else
|
2013-01-10 22:33:16 +01:00
|
|
|
data_rest -= length;
|
2009-01-03 23:35:30 +01:00
|
|
|
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-07-05 17:17:44 +02:00
|
|
|
icy_add_item(TagBuilder &tag, TagType type, StringView value) noexcept
|
2009-01-03 23:35:30 +01:00
|
|
|
{
|
2019-07-05 17:17:44 +02:00
|
|
|
if (value.size >= 2 && value.front() == '\'' && value.back() == '\'') {
|
2009-01-03 23:35:30 +01:00
|
|
|
/* strip the single quotes */
|
2019-07-05 17:17:44 +02:00
|
|
|
++value.data;
|
|
|
|
value.size -= 2;
|
2009-01-03 23:35:30 +01:00
|
|
|
}
|
|
|
|
|
2019-07-05 17:17:44 +02:00
|
|
|
if (value.size > 0)
|
|
|
|
tag.AddItem(type, value);
|
2009-01-03 23:35:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-12-20 15:05:14 +01:00
|
|
|
icy_parse_tag_item(TagBuilder &tag,
|
2019-08-09 15:03:07 +02:00
|
|
|
#ifdef HAVE_ICU_CONVERTER
|
|
|
|
const IcuConverter *icu_converter,
|
|
|
|
#endif
|
2017-12-20 15:05:14 +01:00
|
|
|
const char *name, const char *value) noexcept
|
2009-01-03 23:35:30 +01:00
|
|
|
{
|
2019-08-09 15:03:07 +02:00
|
|
|
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
|
|
|
|
|
2014-01-21 14:51:35 +01:00
|
|
|
icy_add_item(tag, TAG_TITLE, value);
|
2019-08-09 15:03:07 +02:00
|
|
|
}
|
2014-01-21 14:51:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 *
|
2017-12-20 15:05:14 +01:00
|
|
|
find_end_quote(char *p, char *const end) noexcept
|
2014-01-21 14:51:35 +01:00
|
|
|
{
|
|
|
|
char *fallback = std::find(p, end, '\'');
|
|
|
|
if (fallback >= end - 1 || fallback[1] == ';')
|
|
|
|
return fallback;
|
2009-01-03 23:35:30 +01:00
|
|
|
|
2014-01-21 14:51:35 +01:00
|
|
|
p = fallback + 1;
|
|
|
|
while (true) {
|
|
|
|
p = std::find(p, end, '\'');
|
|
|
|
if (p == end)
|
|
|
|
return fallback;
|
2009-01-03 23:35:30 +01:00
|
|
|
|
2014-01-21 14:51:35 +01:00
|
|
|
if (p == end - 1 || p[1] == ';')
|
|
|
|
return p;
|
|
|
|
|
|
|
|
++p;
|
|
|
|
}
|
2009-01-03 23:35:30 +01:00
|
|
|
}
|
|
|
|
|
2017-12-20 14:49:48 +01:00
|
|
|
static std::unique_ptr<Tag>
|
2019-08-09 15:03:07 +02:00
|
|
|
icy_parse_tag(
|
|
|
|
#ifdef HAVE_ICU_CONVERTER
|
|
|
|
const IcuConverter *icu_converter,
|
|
|
|
#endif
|
|
|
|
char *p, char *const end) noexcept
|
2009-01-03 23:35:30 +01:00
|
|
|
{
|
2014-01-21 14:51:35 +01:00
|
|
|
assert(p != nullptr);
|
|
|
|
assert(end != nullptr);
|
|
|
|
assert(p <= end);
|
|
|
|
|
2013-12-03 11:31:03 +01:00
|
|
|
TagBuilder tag;
|
|
|
|
|
2014-01-21 14:51:35 +01:00
|
|
|
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;
|
2009-01-03 23:35:30 +01:00
|
|
|
|
2014-01-21 14:51:35 +01:00
|
|
|
*quote = 0;
|
|
|
|
p = quote + 1;
|
2009-01-03 23:35:30 +01:00
|
|
|
|
2019-08-09 15:03:07 +02:00
|
|
|
icy_parse_tag_item(tag,
|
|
|
|
#ifdef HAVE_ICU_CONVERTER
|
|
|
|
icu_converter,
|
|
|
|
#endif
|
|
|
|
name, value);
|
2014-01-21 14:51:35 +01:00
|
|
|
|
|
|
|
char *semicolon = std::find(p, end, ';');
|
|
|
|
if (semicolon == end)
|
|
|
|
break;
|
|
|
|
p = semicolon + 1;
|
|
|
|
}
|
2009-01-03 23:35:30 +01:00
|
|
|
|
2014-01-08 19:43:09 +01:00
|
|
|
return tag.CommitNew();
|
2009-01-03 23:35:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
2017-12-20 15:05:14 +01:00
|
|
|
IcyMetaDataParser::Meta(const void *data, size_t length) noexcept
|
2009-01-03 23:35:30 +01:00
|
|
|
{
|
2020-02-01 13:55:08 +01:00
|
|
|
const auto *p = (const unsigned char *)data;
|
2009-01-03 23:35:30 +01:00
|
|
|
|
2013-01-10 22:33:16 +01:00
|
|
|
assert(IsDefined());
|
|
|
|
assert(data_rest == 0);
|
2009-01-03 23:35:30 +01:00
|
|
|
assert(length > 0);
|
|
|
|
|
2013-01-10 22:33:16 +01:00
|
|
|
if (meta_size == 0) {
|
2009-01-03 23:35:30 +01:00
|
|
|
/* read meta_size from the first byte of a meta
|
|
|
|
block */
|
2013-01-10 22:33:16 +01:00
|
|
|
meta_size = *p++ * 16;
|
|
|
|
if (meta_size == 0) {
|
2009-01-03 23:35:30 +01:00
|
|
|
/* special case: no metadata */
|
2013-01-10 22:33:16 +01:00
|
|
|
data_rest = data_size;
|
2009-01-03 23:35:30 +01:00
|
|
|
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) */
|
2013-01-10 22:33:16 +01:00
|
|
|
meta_position = 0;
|
2013-12-04 15:03:39 +01:00
|
|
|
meta_data = new char[meta_size + 1];
|
2009-01-03 23:35:30 +01:00
|
|
|
}
|
|
|
|
|
2013-01-10 22:33:16 +01:00
|
|
|
assert(meta_position < meta_size);
|
2009-01-03 23:35:30 +01:00
|
|
|
|
2013-01-10 22:33:16 +01:00
|
|
|
if (length > meta_size - meta_position)
|
|
|
|
length = meta_size - meta_position;
|
2009-01-03 23:35:30 +01:00
|
|
|
|
2013-01-10 22:33:16 +01:00
|
|
|
memcpy(meta_data + meta_position, p, length);
|
|
|
|
meta_position += length;
|
2009-01-03 23:35:30 +01:00
|
|
|
|
|
|
|
if (p != data)
|
|
|
|
/* re-add the first byte (which contained meta_size) */
|
|
|
|
++length;
|
|
|
|
|
2013-01-10 22:33:16 +01:00
|
|
|
if (meta_position == meta_size) {
|
2009-01-03 23:35:30 +01:00
|
|
|
/* parse */
|
|
|
|
|
2019-08-09 15:03:07 +02:00
|
|
|
tag = icy_parse_tag(
|
|
|
|
#ifdef HAVE_ICU_CONVERTER
|
|
|
|
icu_converter.get(),
|
|
|
|
#endif
|
|
|
|
meta_data, meta_data + meta_size);
|
2013-12-04 15:03:39 +01:00
|
|
|
delete[] meta_data;
|
2009-01-03 23:35:30 +01:00
|
|
|
|
|
|
|
/* change back to normal data mode */
|
|
|
|
|
2013-01-10 22:33:16 +01:00
|
|
|
meta_size = 0;
|
|
|
|
data_rest = data_size;
|
2009-01-03 23:35:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return length;
|
|
|
|
}
|
2014-05-11 15:32:47 +02:00
|
|
|
|
|
|
|
size_t
|
2017-12-20 15:05:14 +01:00
|
|
|
IcyMetaDataParser::ParseInPlace(void *data, size_t length) noexcept
|
2014-05-11 15:32:47 +02:00
|
|
|
{
|
2020-02-01 13:55:08 +01:00
|
|
|
auto *const dest0 = (uint8_t *)data;
|
2014-05-11 15:32:47 +02:00
|
|
|
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;
|
|
|
|
}
|