FixString.cxx 3.17 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2020 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 27
#include <cassert>

28
#include <stdlib.h>
29

30 31
gcc_pure
static const char *
32
FindInvalidUTF8(const char *p, const char *const end) noexcept
33 34 35 36 37 38 39 40 41 42 43 44
{
	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;
45
		assert(s == t);
46 47 48 49 50 51

		p += s;
	}

	return nullptr;
}
52

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

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

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

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

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

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

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

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

	return nullptr;
}

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

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

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

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

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

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

	return cleared;
}