FixString.cxx 3.39 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/CharUtil.hxx"
23
#include "util/WritableBuffer.hxx"
24
#include "util/StringView.hxx"
25
#include "util/UTF8.hxx"
26

27
#include <algorithm>
28 29
#include <cassert>

30
#include <stdlib.h>
31

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

		p += s;
	}

	return nullptr;
}
54

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

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

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

74
	return { dest, src.size };
75 76
}

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

85
	/* no, broken - patch invalid sequences */
86
	return patch_utf8(p, invalid);
87 88 89
}

static const char *
90
find_non_printable(StringView p)
91
{
92
	for (const char &ch : p)
93
		if (IsNonPrintableASCII(ch))
94
			return &ch;
95 96 97 98 99 100 101 102

	return nullptr;
}

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

110
	char *dest = (char *)xmemdup(src.data, src.size);
111

112
	for (size_t i = first - src.data; i < src.size; ++i)
113
		if (IsNonPrintableASCII(dest[i]))
114 115
			dest[i] = ' ';

116
	return { dest, src.size };
117 118
}

119 120 121 122 123 124 125 126 127 128
gcc_pure
static bool
IsSafe(StringView s) noexcept
{
	return std::all_of(s.begin(), s.end(),
			   [](char ch){
				   return IsASCII(ch) && IsPrintableASCII(ch);
			   });
}

129
WritableBuffer<char>
130
FixTagString(StringView p)
131
{
132 133 134 135
	if (IsSafe(p))
		/* optimistic optimization for the common case */
		return nullptr;

136 137 138
	auto utf8 = fix_utf8(p);
	if (!utf8.IsNull())
		p = {utf8.data, utf8.size};
139

140
	WritableBuffer<char> cleared = clear_non_printable(p);
141
	if (cleared.IsNull())
142 143
		cleared = utf8;
	else
144
		free(utf8.data);
145 146 147

	return cleared;
}