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

20
#include "FixString.hxx"
21
#include "util/Alloc.hxx"
22
#include "util/WritableBuffer.hxx"
23
#include "util/StringView.hxx"
24
#include "util/UTF8.hxx"
25 26

#include <assert.h>
27
#include <stdlib.h>
28

29 30
gcc_pure
static const char *
31
FindInvalidUTF8(const char *p, const char *const end) noexcept
32 33 34 35 36 37 38 39 40 41 42 43
{
	while (p < end) {
		const size_t s = SequenceLengthUTF8(*p);
		if (p + s > end)
			/* partial sequence at end of string */
			return p;

		/* now call the other SequenceLengthUTF8() overload
		   which also validates the continuations */
		const size_t t = SequenceLengthUTF8(p);
		if (t == 0)
			return p;
44
		assert(s == t);
45 46 47 48 49 50

		p += s;
	}

	return nullptr;
}
51

52 53 54
/**
 * Replace invalid sequences with the question mark.
 */
55
static WritableBuffer<char>
56
patch_utf8(StringView src, const char *_invalid)
57 58 59
{
	/* duplicate the string, and replace invalid bytes in that
	   buffer */
60 61
	char *dest = (char *)xmemdup(src.data, src.size);
	char *const end = dest + src.size;
62

63
	char *invalid = dest + (_invalid - src.data);
64
	do {
65 66 67 68 69
		*invalid = '?';

		const char *__invalid = FindInvalidUTF8(invalid + 1, end);
		invalid = const_cast<char *>(__invalid);
	} while (invalid != nullptr);
70

71
	return { dest, src.size };
72 73
}

74
static WritableBuffer<char>
75
fix_utf8(StringView p)
76 77
{
	/* check if the string is already valid UTF-8 */
78
	const char *invalid = FindInvalidUTF8(p.begin(), p.end());
79
	if (invalid == nullptr)
80 81
		return nullptr;

82
	/* no, broken - patch invalid sequences */
83
	return patch_utf8(p, invalid);
84 85 86 87 88 89 90 91 92
}

static bool
char_is_non_printable(unsigned char ch)
{
	return ch < 0x20;
}

static const char *
93
find_non_printable(StringView p)
94
{
95 96 97
	for (const char &ch : p)
		if (char_is_non_printable(ch))
			return &ch;
98 99 100 101 102 103 104 105

	return nullptr;
}

/**
 * Clears all non-printable characters, convert them to space.
 * Returns nullptr if nothing needs to be cleared.
 */
106
static WritableBuffer<char>
107
clear_non_printable(StringView src)
108
{
109
	const char *first = find_non_printable(src);
110 111 112
	if (first == nullptr)
		return nullptr;

113
	char *dest = (char *)xmemdup(src.data, src.size);
114

115
	for (size_t i = first - src.data; i < src.size; ++i)
116 117 118
		if (char_is_non_printable(dest[i]))
			dest[i] = ' ';

119
	return { dest, src.size };
120 121
}

122
WritableBuffer<char>
123
FixTagString(StringView p)
124
{
125 126 127
	auto utf8 = fix_utf8(p);
	if (!utf8.IsNull())
		p = {utf8.data, utf8.size};
128

129
	WritableBuffer<char> cleared = clear_non_printable(p);
130
	if (cleared.IsNull())
131 132
		cleared = utf8;
	else
133
		free(utf8.data);
134 135 136

	return cleared;
}