From 429ed24c9903008c3699da48d353f48e1a3151f6 Mon Sep 17 00:00:00 2001
From: Max Kellermann <max@duempel.org>
Date: Wed, 24 Nov 2010 08:59:04 +0100
Subject: [PATCH] tag_ape: support multiple values

One APE tag may contain more than one value, separated by null bytes.
---
 NEWS          |  1 +
 src/tag_ape.c | 16 +++++++++++++++-
 2 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/NEWS b/NEWS
index 998a44ab3..e528e7128 100644
--- a/NEWS
+++ b/NEWS
@@ -21,6 +21,7 @@ ver 0.16 (20??/??/??)
   - added tags "ArtistSort", "AlbumArtistSort"
   - id3: revised "performer" tag support
   - ape: MusicBrainz tags
+  - ape: support multiple values
 * decoders:
   - don't try a plugin twice (MIME type & suffix)
   - don't fall back to "mad" unless no plugin matches
diff --git a/src/tag_ape.c b/src/tag_ape.c
index 82e36d7a4..79facba1b 100644
--- a/src/tag_ape.c
+++ b/src/tag_ape.c
@@ -52,7 +52,21 @@ tag_ape_import_item(struct tag *tag, unsigned long flags,
 
 	if (tag == NULL)
 		tag = tag_new();
-	tag_add_item_n(tag, type, value, value_length);
+
+	const char *end = value + value_length;
+	while (true) {
+		/* multiple values are separated by null bytes */
+		const char *n = memchr(value, 0, end - value);
+		if (n != NULL) {
+			if (n > value)
+				tag_add_item_n(tag, type, value, n - value);
+			value = n + 1;
+		} else {
+			if (end > value)
+				tag_add_item_n(tag, type, value, end - value);
+			break;
+		}
+	}
 
 	return tag;
 }