aboutsummaryrefslogtreecommitdiffstats
path: root/src/tag
diff options
context:
space:
mode:
Diffstat (limited to 'src/tag')
-rw-r--r--src/tag/Tag.cxx509
-rw-r--r--src/tag/Tag.hxx223
-rw-r--r--src/tag/TagHandler.cxx62
-rw-r--r--src/tag/TagHandler.hxx101
-rw-r--r--src/tag/TagInternal.hxx27
-rw-r--r--src/tag/TagNames.c44
-rw-r--r--src/tag/TagPool.cxx155
-rw-r--r--src/tag/TagPool.hxx39
-rw-r--r--src/tag/TagType.h56
9 files changed, 1216 insertions, 0 deletions
diff --git a/src/tag/Tag.cxx b/src/tag/Tag.cxx
new file mode 100644
index 000000000..c960da537
--- /dev/null
+++ b/src/tag/Tag.cxx
@@ -0,0 +1,509 @@
+/*
+ * Copyright (C) 2003-2013 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 "Tag.hxx"
+#include "TagInternal.hxx"
+#include "TagPool.hxx"
+#include "ConfigGlobal.hxx"
+#include "ConfigOption.hxx"
+#include "Song.hxx"
+#include "system/FatalError.hxx"
+
+#include <glib.h>
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+/**
+ * Maximum number of items managed in the bulk list; if it is
+ * exceeded, we switch back to "normal" reallocation.
+ */
+#define BULK_MAX 64
+
+static struct {
+#ifndef NDEBUG
+ bool busy;
+#endif
+ TagItem *items[BULK_MAX];
+} bulk;
+
+bool ignore_tag_items[TAG_NUM_OF_ITEM_TYPES];
+
+enum tag_type
+tag_name_parse(const char *name)
+{
+ assert(name != nullptr);
+
+ for (unsigned i = 0; i < TAG_NUM_OF_ITEM_TYPES; ++i) {
+ assert(tag_item_names[i] != nullptr);
+
+ if (strcmp(name, tag_item_names[i]) == 0)
+ return (enum tag_type)i;
+ }
+
+ return TAG_NUM_OF_ITEM_TYPES;
+}
+
+enum tag_type
+tag_name_parse_i(const char *name)
+{
+ assert(name != nullptr);
+
+ for (unsigned i = 0; i < TAG_NUM_OF_ITEM_TYPES; ++i) {
+ assert(tag_item_names[i] != nullptr);
+
+ if (g_ascii_strcasecmp(name, tag_item_names[i]) == 0)
+ return (enum tag_type)i;
+ }
+
+ return TAG_NUM_OF_ITEM_TYPES;
+}
+
+static size_t
+items_size(const Tag &tag)
+{
+ return tag.num_items * sizeof(TagItem *);
+}
+
+void tag_lib_init(void)
+{
+ const char *value;
+ int quit = 0;
+ char *temp;
+ char *s;
+ char *c;
+ enum tag_type type;
+
+ /* parse the "metadata_to_use" config parameter below */
+
+ /* ignore comments by default */
+ ignore_tag_items[TAG_COMMENT] = true;
+
+ value = config_get_string(CONF_METADATA_TO_USE, nullptr);
+ if (value == nullptr)
+ return;
+
+ memset(ignore_tag_items, true, TAG_NUM_OF_ITEM_TYPES);
+
+ if (0 == g_ascii_strcasecmp(value, "none"))
+ return;
+
+ temp = c = s = g_strdup(value);
+ while (!quit) {
+ if (*s == ',' || *s == '\0') {
+ if (*s == '\0')
+ quit = 1;
+ *s = '\0';
+
+ c = g_strstrip(c);
+ if (*c == 0)
+ continue;
+
+ type = tag_name_parse_i(c);
+ if (type == TAG_NUM_OF_ITEM_TYPES)
+ FormatFatalError("error parsing metadata item \"%s\"",
+ c);
+
+ ignore_tag_items[type] = false;
+
+ s++;
+ c = s;
+ }
+ s++;
+ }
+
+ g_free(temp);
+}
+
+void
+Tag::Clear()
+{
+ time = -1;
+ has_playlist = false;
+
+ tag_pool_lock.lock();
+ for (unsigned i = 0; i < num_items; ++i)
+ tag_pool_put_item(items[i]);
+ tag_pool_lock.unlock();
+
+ if (items == bulk.items) {
+#ifndef NDEBUG
+ assert(bulk.busy);
+ bulk.busy = false;
+#endif
+ } else
+ g_free(items);
+
+ items = nullptr;
+ num_items = 0;
+}
+
+void
+Tag::DeleteItem(unsigned idx)
+{
+ assert(idx < num_items);
+ --num_items;
+
+ tag_pool_lock.lock();
+ tag_pool_put_item(items[idx]);
+ tag_pool_lock.unlock();
+
+ if (num_items - idx > 0) {
+ memmove(items + idx, items + idx + 1,
+ (num_items - idx) * sizeof(items[0]));
+ }
+
+ if (num_items > 0) {
+ items = (TagItem **)
+ g_realloc(items, items_size(*this));
+ } else {
+ g_free(items);
+ items = nullptr;
+ }
+}
+
+void
+Tag::ClearItemsByType(tag_type type)
+{
+ for (unsigned i = 0; i < num_items; i++) {
+ if (items[i]->type == type) {
+ DeleteItem(i);
+ /* decrement since when just deleted this node */
+ i--;
+ }
+ }
+}
+
+Tag::~Tag()
+{
+ tag_pool_lock.lock();
+ for (int i = num_items; --i >= 0; )
+ tag_pool_put_item(items[i]);
+ tag_pool_lock.unlock();
+
+ if (items == bulk.items) {
+#ifndef NDEBUG
+ assert(bulk.busy);
+ bulk.busy = false;
+#endif
+ } else
+ g_free(items);
+}
+
+Tag::Tag(const Tag &other)
+ :time(other.time), has_playlist(other.has_playlist),
+ items(nullptr),
+ num_items(other.num_items)
+{
+ if (num_items > 0) {
+ items = (TagItem **)g_malloc(items_size(other));
+
+ tag_pool_lock.lock();
+ for (unsigned i = 0; i < num_items; i++)
+ items[i] = tag_pool_dup_item(other.items[i]);
+ tag_pool_lock.unlock();
+ }
+}
+
+Tag *
+Tag::Merge(const Tag &base, const Tag &add)
+{
+ unsigned n;
+
+ /* allocate new tag object */
+
+ Tag *ret = new Tag();
+ ret->time = add.time > 0 ? add.time : base.time;
+ ret->num_items = base.num_items + add.num_items;
+ ret->items = ret->num_items > 0
+ ? (TagItem **)g_malloc(items_size(*ret))
+ : nullptr;
+
+ tag_pool_lock.lock();
+
+ /* copy all items from "add" */
+
+ for (unsigned i = 0; i < add.num_items; ++i)
+ ret->items[i] = tag_pool_dup_item(add.items[i]);
+
+ n = add.num_items;
+
+ /* copy additional items from "base" */
+
+ for (unsigned i = 0; i < base.num_items; ++i)
+ if (!add.HasType(base.items[i]->type))
+ ret->items[n++] = tag_pool_dup_item(base.items[i]);
+
+ tag_pool_lock.unlock();
+
+ assert(n <= ret->num_items);
+
+ if (n < ret->num_items) {
+ /* some tags were not copied - shrink ret->items */
+ assert(n > 0);
+
+ ret->num_items = n;
+ ret->items = (TagItem **)
+ g_realloc(ret->items, items_size(*ret));
+ }
+
+ return ret;
+}
+
+Tag *
+Tag::MergeReplace(Tag *base, Tag *add)
+{
+ if (add == nullptr)
+ return base;
+
+ if (base == nullptr)
+ return add;
+
+ Tag *tag = Merge(*base, *add);
+ delete base;
+ delete add;
+
+ return tag;
+}
+
+const char *
+Tag::GetValue(tag_type type) const
+{
+ assert(type < TAG_NUM_OF_ITEM_TYPES);
+
+ for (unsigned i = 0; i < num_items; i++)
+ if (items[i]->type == type)
+ return items[i]->value;
+
+ return nullptr;
+}
+
+bool
+Tag::HasType(tag_type type) const
+{
+ return GetValue(type) != nullptr;
+}
+
+bool
+Tag::Equals(const Tag &other) const
+{
+ if (time != other.time)
+ return false;
+
+ if (num_items != other.num_items)
+ return false;
+
+ for (unsigned i = 0; i < num_items; i++) {
+ if (items[i]->type != other.items[i]->type)
+ return false;
+ if (strcmp(items[i]->value, other.items[i]->value)) {
+ return false;
+ }
+ }
+
+ return true;
+}
+
+/**
+ * Replace invalid sequences with the question mark.
+ */
+static char *
+patch_utf8(const char *src, size_t length, const gchar *end)
+{
+ /* duplicate the string, and replace invalid bytes in that
+ buffer */
+ char *dest = g_strdup(src);
+
+ do {
+ dest[end - src] = '?';
+ } while (!g_utf8_validate(end + 1, (src + length) - (end + 1), &end));
+
+ return dest;
+}
+
+static char *
+fix_utf8(const char *str, size_t length)
+{
+ const gchar *end;
+ char *temp;
+ gsize written;
+
+ assert(str != nullptr);
+
+ /* check if the string is already valid UTF-8 */
+ if (g_utf8_validate(str, length, &end))
+ return nullptr;
+
+ /* no, it's not - try to import it from ISO-Latin-1 */
+ temp = g_convert(str, length, "utf-8", "iso-8859-1",
+ nullptr, &written, nullptr);
+ if (temp != nullptr)
+ /* success! */
+ return temp;
+
+ /* no, still broken - there's no medication, just patch
+ invalid sequences */
+ return patch_utf8(str, length, end);
+}
+
+void
+Tag::BeginAdd()
+{
+ assert(!bulk.busy);
+ assert(items == nullptr);
+ assert(num_items == 0);
+
+#ifndef NDEBUG
+ bulk.busy = true;
+#endif
+ items = bulk.items;
+}
+
+void
+Tag::EndAdd()
+{
+ if (items == bulk.items) {
+ assert(num_items <= BULK_MAX);
+
+ if (num_items > 0) {
+ /* copy the tag items from the bulk list over
+ to a new list (which fits exactly) */
+ items = (TagItem **)
+ g_malloc(items_size(*this));
+ memcpy(items, bulk.items, items_size(*this));
+ } else
+ items = nullptr;
+ }
+
+#ifndef NDEBUG
+ bulk.busy = false;
+#endif
+}
+
+static bool
+char_is_non_printable(unsigned char ch)
+{
+ return ch < 0x20;
+}
+
+static const char *
+find_non_printable(const char *p, size_t length)
+{
+ for (size_t i = 0; i < length; ++i)
+ if (char_is_non_printable(p[i]))
+ return p + i;
+
+ return nullptr;
+}
+
+/**
+ * Clears all non-printable characters, convert them to space.
+ * Returns nullptr if nothing needs to be cleared.
+ */
+static char *
+clear_non_printable(const char *p, size_t length)
+{
+ const char *first = find_non_printable(p, length);
+ char *dest;
+
+ if (first == nullptr)
+ return nullptr;
+
+ dest = g_strndup(p, length);
+
+ for (size_t i = first - p; i < length; ++i)
+ if (char_is_non_printable(dest[i]))
+ dest[i] = ' ';
+
+ return dest;
+}
+
+static char *
+fix_tag_value(const char *p, size_t length)
+{
+ char *utf8, *cleared;
+
+ utf8 = fix_utf8(p, length);
+ if (utf8 != nullptr) {
+ p = utf8;
+ length = strlen(p);
+ }
+
+ cleared = clear_non_printable(p, length);
+ if (cleared == nullptr)
+ cleared = utf8;
+ else
+ g_free(utf8);
+
+ return cleared;
+}
+
+void
+Tag::AddItemInternal(tag_type type, const char *value, size_t len)
+{
+ unsigned int i = num_items;
+ char *p;
+
+ p = fix_tag_value(value, len);
+ if (p != nullptr) {
+ value = p;
+ len = strlen(value);
+ }
+
+ num_items++;
+
+ if (items != bulk.items)
+ /* bulk mode disabled */
+ items = (TagItem **)
+ g_realloc(items, items_size(*this));
+ else if (num_items >= BULK_MAX) {
+ /* bulk list already full - switch back to non-bulk */
+ assert(bulk.busy);
+
+ items = (TagItem **)g_malloc(items_size(*this));
+ memcpy(items, bulk.items,
+ items_size(*this) - sizeof(TagItem *));
+ }
+
+ tag_pool_lock.lock();
+ items[i] = tag_pool_get_item(type, value, len);
+ tag_pool_lock.unlock();
+
+ g_free(p);
+}
+
+void
+Tag::AddItem(tag_type type, const char *value, size_t len)
+{
+ if (ignore_tag_items[type])
+ return;
+
+ if (value == nullptr || len == 0)
+ return;
+
+ AddItemInternal(type, value, len);
+}
+
+void
+Tag::AddItem(tag_type type, const char *value)
+{
+ AddItem(type, value, strlen(value));
+}
diff --git a/src/tag/Tag.hxx b/src/tag/Tag.hxx
new file mode 100644
index 000000000..2c5f599e2
--- /dev/null
+++ b/src/tag/Tag.hxx
@@ -0,0 +1,223 @@
+/*
+ * Copyright (C) 2003-2013 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.
+ */
+
+#ifndef MPD_TAG_HXX
+#define MPD_TAG_HXX
+
+#include "TagType.h"
+#include "gcc.h"
+
+#include <algorithm>
+
+#include <stddef.h>
+
+/**
+ * One tag value. It is a mapping of #tag_type to am arbitrary string
+ * value. Each tag can have multiple items of one tag type (although
+ * few clients support that).
+ */
+struct TagItem {
+ /** the type of this item */
+ enum tag_type type;
+
+ /**
+ * the value of this tag; this is a variable length string
+ */
+ char value[sizeof(long)];
+} gcc_packed;
+
+/**
+ * The meta information about a song file. It is a MPD specific
+ * subset of tags (e.g. from ID3, vorbis comments, ...).
+ */
+struct Tag {
+ /**
+ * The duration of the song (in seconds). A value of zero
+ * means that the length is unknown. If the duration is
+ * really between zero and one second, you should round up to
+ * 1.
+ */
+ int time;
+
+ /**
+ * Does this file have an embedded playlist (e.g. embedded CUE
+ * sheet)?
+ */
+ bool has_playlist;
+
+ /** an array of tag items */
+ TagItem **items;
+
+ /** the total number of tag items in the #items array */
+ unsigned num_items;
+
+ /**
+ * Create an empty tag.
+ */
+ Tag():time(-1), has_playlist(false),
+ items(nullptr), num_items(0) {}
+
+ Tag(const Tag &other);
+
+ Tag(Tag &&other)
+ :time(other.time), has_playlist(other.has_playlist),
+ items(other.items), num_items(other.num_items) {
+ other.items = nullptr;
+ other.num_items = 0;
+ }
+
+ /**
+ * Free the tag object and all its items.
+ */
+ ~Tag();
+
+ Tag &operator=(const Tag &other) = delete;
+
+ Tag &operator=(Tag &&other) {
+ time = other.time;
+ has_playlist = other.has_playlist;
+ std::swap(items, other.items);
+ std::swap(num_items, other.num_items);
+ return *this;
+ }
+
+ /**
+ * Returns true if the tag contains no items. This ignores the "time"
+ * attribute.
+ */
+ bool IsEmpty() const {
+ return num_items == 0;
+ }
+
+ /**
+ * Returns true if the tag contains any information.
+ */
+ bool IsDefined() const {
+ return !IsEmpty() || time >= 0;
+ }
+
+ /**
+ * Clear everything, as if this was a new Tag object.
+ */
+ void Clear();
+
+ void DeleteItem(unsigned i);
+
+ /**
+ * Clear all tag items with the specified type.
+ */
+ void ClearItemsByType(tag_type type);
+
+ /**
+ * Gives an optional hint to the tag library that we will now
+ * add several tag items; this is used by the library to
+ * optimize memory allocation. Only one tag may be in this
+ * state, and this tag must not have any items yet. You must
+ * call tag_end_add() when you are done.
+ */
+ void BeginAdd();
+
+ /**
+ * Finishes the operation started with tag_begin_add().
+ */
+ void EndAdd();
+
+ /**
+ * Appends a new tag item.
+ *
+ * @param type the type of the new tag item
+ * @param value the value of the tag item (not null-terminated)
+ * @param len the length of #value
+ */
+ void AddItem(tag_type type, const char *value, size_t len);
+
+ /**
+ * Appends a new tag item.
+ *
+ * @param tag the #tag object
+ * @param type the type of the new tag item
+ * @param value the value of the tag item (null-terminated)
+ */
+ void AddItem(tag_type type, const char *value);
+
+ /**
+ * Merges the data from two tags. If both tags share data for the
+ * same tag_type, only data from "add" is used.
+ *
+ * @return a newly allocated tag
+ */
+ gcc_malloc
+ static Tag *Merge(const Tag &base, const Tag &add);
+
+ /**
+ * Merges the data from two tags. Any of the two may be NULL. Both
+ * are freed by this function.
+ *
+ * @return a newly allocated tag
+ */
+ gcc_malloc
+ static Tag *MergeReplace(Tag *base, Tag *add);
+
+ /**
+ * Returns the first value of the specified tag type, or NULL if none
+ * is present in this tag object.
+ */
+ gcc_pure
+ const char *GetValue(tag_type type) const;
+
+ /**
+ * Checks whether the tag contains one or more items with
+ * the specified type.
+ */
+ bool HasType(tag_type type) const;
+
+ /**
+ * Compares two tags, including the duration and all tag items. The
+ * order of the tag items matters.
+ */
+ gcc_pure
+ bool Equals(const Tag &other) const;
+
+private:
+ void AddItemInternal(tag_type type, const char *value, size_t len);
+};
+
+/**
+ * Parse the string, and convert it into a #tag_type. Returns
+ * #TAG_NUM_OF_ITEM_TYPES if the string could not be recognized.
+ */
+enum tag_type
+tag_name_parse(const char *name);
+
+/**
+ * Parse the string, and convert it into a #tag_type. Returns
+ * #TAG_NUM_OF_ITEM_TYPES if the string could not be recognized.
+ *
+ * Case does not matter.
+ */
+enum tag_type
+tag_name_parse_i(const char *name);
+
+/**
+ * Initializes the tag library.
+ */
+void
+tag_lib_init();
+
+#endif
diff --git a/src/tag/TagHandler.cxx b/src/tag/TagHandler.cxx
new file mode 100644
index 000000000..055fae49a
--- /dev/null
+++ b/src/tag/TagHandler.cxx
@@ -0,0 +1,62 @@
+/*
+ * Copyright (C) 2003-2013 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 "TagHandler.hxx"
+#include "Tag.hxx"
+
+#include <glib.h>
+
+static void
+add_tag_duration(unsigned seconds, void *ctx)
+{
+ Tag *tag = (Tag *)ctx;
+
+ tag->time = seconds;
+}
+
+static void
+add_tag_tag(enum tag_type type, const char *value, void *ctx)
+{
+ Tag *tag = (Tag *)ctx;
+
+ tag->AddItem(type, value);
+}
+
+const struct tag_handler add_tag_handler = {
+ add_tag_duration,
+ add_tag_tag,
+ nullptr,
+};
+
+static void
+full_tag_pair(const char *name, gcc_unused const char *value, void *ctx)
+{
+ Tag *tag = (Tag *)ctx;
+
+ if (g_ascii_strcasecmp(name, "cuesheet") == 0)
+ tag->has_playlist = true;
+}
+
+const struct tag_handler full_tag_handler = {
+ add_tag_duration,
+ add_tag_tag,
+ full_tag_pair,
+};
+
diff --git a/src/tag/TagHandler.hxx b/src/tag/TagHandler.hxx
new file mode 100644
index 000000000..3303dd27e
--- /dev/null
+++ b/src/tag/TagHandler.hxx
@@ -0,0 +1,101 @@
+/*
+ * Copyright (C) 2003-2013 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.
+ */
+
+#ifndef MPD_TAG_HANDLER_HXX
+#define MPD_TAG_HANDLER_HXX
+
+#include "check.h"
+#include "TagType.h"
+
+#include <assert.h>
+
+/**
+ * A callback table for receiving metadata of a song.
+ */
+struct tag_handler {
+ /**
+ * Declare the duration of a song, in seconds. Do not call
+ * this when the duration could not be determined, because
+ * there is no magic value for "unknown duration".
+ */
+ void (*duration)(unsigned seconds, void *ctx);
+
+ /**
+ * A tag has been read.
+ *
+ * @param the value of the tag; the pointer will become
+ * invalid after returning
+ */
+ void (*tag)(enum tag_type type, const char *value, void *ctx);
+
+ /**
+ * A name-value pair has been read. It is the codec specific
+ * representation of tags.
+ */
+ void (*pair)(const char *key, const char *value, void *ctx);
+};
+
+static inline void
+tag_handler_invoke_duration(const struct tag_handler *handler, void *ctx,
+ unsigned seconds)
+{
+ assert(handler != nullptr);
+
+ if (handler->duration != nullptr)
+ handler->duration(seconds, ctx);
+}
+
+static inline void
+tag_handler_invoke_tag(const struct tag_handler *handler, void *ctx,
+ enum tag_type type, const char *value)
+{
+ assert(handler != nullptr);
+ assert((unsigned)type < TAG_NUM_OF_ITEM_TYPES);
+ assert(value != nullptr);
+
+ if (handler->tag != nullptr)
+ handler->tag(type, value, ctx);
+}
+
+static inline void
+tag_handler_invoke_pair(const struct tag_handler *handler, void *ctx,
+ const char *name, const char *value)
+{
+ assert(handler != nullptr);
+ assert(name != nullptr);
+ assert(value != nullptr);
+
+ if (handler->pair != nullptr)
+ handler->pair(name, value, ctx);
+}
+
+/**
+ * This #tag_handler implementation adds tag values to a #tag object
+ * (casted from the context pointer).
+ */
+extern const struct tag_handler add_tag_handler;
+
+/**
+ * This #tag_handler implementation adds tag values to a #tag object
+ * (casted from the context pointer), and supports the has_playlist
+ * attribute.
+ */
+extern const struct tag_handler full_tag_handler;
+
+#endif
diff --git a/src/tag/TagInternal.hxx b/src/tag/TagInternal.hxx
new file mode 100644
index 000000000..8172d1319
--- /dev/null
+++ b/src/tag/TagInternal.hxx
@@ -0,0 +1,27 @@
+/*
+ * Copyright (C) 2003-2013 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.
+ */
+
+#ifndef MPD_TAG_INTERNAL_HXX
+#define MPD_TAG_INTERNAL_HXX
+
+#include "TagType.h"
+
+extern bool ignore_tag_items[TAG_NUM_OF_ITEM_TYPES];
+
+#endif
diff --git a/src/tag/TagNames.c b/src/tag/TagNames.c
new file mode 100644
index 000000000..2e318f913
--- /dev/null
+++ b/src/tag/TagNames.c
@@ -0,0 +1,44 @@
+/*
+ * Copyright (C) 2003-2013 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 "TagType.h"
+
+const char *tag_item_names[TAG_NUM_OF_ITEM_TYPES] = {
+ [TAG_ARTIST] = "Artist",
+ [TAG_ARTIST_SORT] = "ArtistSort",
+ [TAG_ALBUM] = "Album",
+ [TAG_ALBUM_ARTIST] = "AlbumArtist",
+ [TAG_ALBUM_ARTIST_SORT] = "AlbumArtistSort",
+ [TAG_TITLE] = "Title",
+ [TAG_TRACK] = "Track",
+ [TAG_NAME] = "Name",
+ [TAG_GENRE] = "Genre",
+ [TAG_DATE] = "Date",
+ [TAG_COMPOSER] = "Composer",
+ [TAG_PERFORMER] = "Performer",
+ [TAG_COMMENT] = "Comment",
+ [TAG_DISC] = "Disc",
+
+ /* MusicBrainz tags from http://musicbrainz.org/doc/MusicBrainzTag */
+ [TAG_MUSICBRAINZ_ARTISTID] = "MUSICBRAINZ_ARTISTID",
+ [TAG_MUSICBRAINZ_ALBUMID] = "MUSICBRAINZ_ALBUMID",
+ [TAG_MUSICBRAINZ_ALBUMARTISTID] = "MUSICBRAINZ_ALBUMARTISTID",
+ [TAG_MUSICBRAINZ_TRACKID] = "MUSICBRAINZ_TRACKID",
+};
diff --git a/src/tag/TagPool.cxx b/src/tag/TagPool.cxx
new file mode 100644
index 000000000..5a0b33c47
--- /dev/null
+++ b/src/tag/TagPool.cxx
@@ -0,0 +1,155 @@
+/*
+ * Copyright (C) 2003-2013 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 "TagPool.hxx"
+#include "Tag.hxx"
+
+#include <glib.h>
+
+#include <assert.h>
+#include <string.h>
+
+Mutex tag_pool_lock;
+
+#define NUM_SLOTS 4096
+
+struct slot {
+ struct slot *next;
+ unsigned char ref;
+ TagItem item;
+} mpd_packed;
+
+static struct slot *slots[NUM_SLOTS];
+
+static inline unsigned
+calc_hash_n(enum tag_type type, const char *p, size_t length)
+{
+ unsigned hash = 5381;
+
+ assert(p != nullptr);
+
+ while (length-- > 0)
+ hash = (hash << 5) + hash + *p++;
+
+ return hash ^ type;
+}
+
+static inline unsigned
+calc_hash(enum tag_type type, const char *p)
+{
+ unsigned hash = 5381;
+
+ assert(p != nullptr);
+
+ while (*p != 0)
+ hash = (hash << 5) + hash + *p++;
+
+ return hash ^ type;
+}
+
+static inline struct slot *
+tag_item_to_slot(TagItem *item)
+{
+ return (struct slot*)(((char*)item) - offsetof(struct slot, item));
+}
+
+static struct slot *slot_alloc(struct slot *next,
+ enum tag_type type,
+ const char *value, int length)
+{
+ struct slot *slot;
+
+ slot = (struct slot *)
+ g_malloc(sizeof(*slot) - sizeof(slot->item.value) + length + 1);
+ slot->next = next;
+ slot->ref = 1;
+ slot->item.type = type;
+ memcpy(slot->item.value, value, length);
+ slot->item.value[length] = 0;
+ return slot;
+}
+
+TagItem *
+tag_pool_get_item(enum tag_type type, const char *value, size_t length)
+{
+ struct slot **slot_p, *slot;
+
+ slot_p = &slots[calc_hash_n(type, value, length) % NUM_SLOTS];
+ for (slot = *slot_p; slot != nullptr; slot = slot->next) {
+ if (slot->item.type == type &&
+ length == strlen(slot->item.value) &&
+ memcmp(value, slot->item.value, length) == 0 &&
+ slot->ref < 0xff) {
+ assert(slot->ref > 0);
+ ++slot->ref;
+ return &slot->item;
+ }
+ }
+
+ slot = slot_alloc(*slot_p, type, value, length);
+ *slot_p = slot;
+ return &slot->item;
+}
+
+TagItem *
+tag_pool_dup_item(TagItem *item)
+{
+ struct slot *slot = tag_item_to_slot(item);
+
+ assert(slot->ref > 0);
+
+ if (slot->ref < 0xff) {
+ ++slot->ref;
+ return item;
+ } else {
+ /* the reference counter overflows above 0xff;
+ duplicate the item, and start with 1 */
+ size_t length = strlen(item->value);
+ struct slot **slot_p =
+ &slots[calc_hash_n(item->type, item->value,
+ length) % NUM_SLOTS];
+ slot = slot_alloc(*slot_p, item->type,
+ item->value, strlen(item->value));
+ *slot_p = slot;
+ return &slot->item;
+ }
+}
+
+void
+tag_pool_put_item(TagItem *item)
+{
+ struct slot **slot_p, *slot;
+
+ slot = tag_item_to_slot(item);
+ assert(slot->ref > 0);
+ --slot->ref;
+
+ if (slot->ref > 0)
+ return;
+
+ for (slot_p = &slots[calc_hash(item->type, item->value) % NUM_SLOTS];
+ *slot_p != slot;
+ slot_p = &(*slot_p)->next) {
+ assert(*slot_p != nullptr);
+ }
+
+ *slot_p = slot->next;
+ g_free(slot);
+}
diff --git a/src/tag/TagPool.hxx b/src/tag/TagPool.hxx
new file mode 100644
index 000000000..a6b28b355
--- /dev/null
+++ b/src/tag/TagPool.hxx
@@ -0,0 +1,39 @@
+/*
+ * Copyright (C) 2003-2013 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.
+ */
+
+#ifndef MPD_TAG_POOL_HXX
+#define MPD_TAG_POOL_HXX
+
+#include "TagType.h"
+#include "thread/Mutex.hxx"
+
+extern Mutex tag_pool_lock;
+
+struct TagItem;
+
+TagItem *
+tag_pool_get_item(enum tag_type type, const char *value, size_t length);
+
+TagItem *
+tag_pool_dup_item(TagItem *item);
+
+void
+tag_pool_put_item(TagItem *item);
+
+#endif
diff --git a/src/tag/TagType.h b/src/tag/TagType.h
new file mode 100644
index 000000000..7a1d351a5
--- /dev/null
+++ b/src/tag/TagType.h
@@ -0,0 +1,56 @@
+/*
+ * Copyright (C) 2003-2013 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.
+ */
+
+#ifndef MPD_TAG_TYPE_H
+#define MPD_TAG_TYPE_H
+
+/**
+ * Codes for the type of a tag item.
+ */
+enum tag_type {
+ TAG_ARTIST,
+ TAG_ARTIST_SORT,
+ TAG_ALBUM,
+ TAG_ALBUM_ARTIST,
+ TAG_ALBUM_ARTIST_SORT,
+ TAG_TITLE,
+ TAG_TRACK,
+ TAG_NAME,
+ TAG_GENRE,
+ TAG_DATE,
+ TAG_COMPOSER,
+ TAG_PERFORMER,
+ TAG_COMMENT,
+ TAG_DISC,
+
+ TAG_MUSICBRAINZ_ARTISTID,
+ TAG_MUSICBRAINZ_ALBUMID,
+ TAG_MUSICBRAINZ_ALBUMARTISTID,
+ TAG_MUSICBRAINZ_TRACKID,
+
+ TAG_NUM_OF_ITEM_TYPES
+};
+
+/**
+ * An array of strings, which map the #tag_type to its machine
+ * readable name (specific to the MPD protocol).
+ */
+extern const char *tag_item_names[];
+
+#endif