-
-
Notifications
You must be signed in to change notification settings - Fork 988
/
validation.cpp
269 lines (232 loc) · 6.49 KB
/
validation.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
Copyright (C) 2003 - 2008 by David White <dave@whitevine.net>
2008 - 2015 by Ignacio R. Morelle <shadowm2006@gmail.com>
Part of the Battle for Wesnoth Project http://www.wesnoth.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.
See the COPYING file for more details.
*/
#include "addon/validation.hpp"
#include "config.hpp"
#include "serialization/unicode_cast.hpp"
#include <algorithm>
#include <boost/algorithm/string.hpp>
#include <set>
const unsigned short default_campaignd_port = 15008;
namespace {
const std::string addon_type_strings[] {
"unknown", "core", "campaign", "scenario", "campaign_sp_mp", "campaign_mp",
"scenario_mp", "map_pack", "era", "faction", "mod_mp", /*"gui", */ "media",
"other", ""
};
struct addon_name_char_illegal
{
/**
* Returns whether the given add-on name char is not whitelisted.
*/
inline bool operator()(char c) const
{
switch(c)
{
case '-': // hyphen-minus
case '_': // low line
return false;
default:
return !isalnum(c);
}
}
};
struct addon_filename_ucs4char_illegal
{
inline bool operator()(ucs4::char_t c) const
{
switch(c){
case ' ':
case '"':
case '*':
case '/':
case ':':
case '<':
case '>':
case '?':
case '\\':
case '|':
case '~':
case 0x7F: // DEL
return true;
default:
return (
c < 0x20 || // C0 control characters
(c >= 0x80 && c < 0xA0) || // C1 control characters
(c >= 0xD800 && c < 0xE000) // surrogate pairs
);
}
}
};
}
bool addon_name_legal(const std::string& name)
{
if(name.empty() ||
std::find_if(name.begin(), name.end(), addon_name_char_illegal()) != name.end()) {
return false;
} else {
return true;
}
}
bool addon_filename_legal(const std::string& name)
{
if(name.empty() || name.back() == '.' ||
name.find("..") != std::string::npos ||
name.size() > 255) {
return false;
} else {
const ucs4::string name_ucs4 = unicode_cast<ucs4::string>(name);
const std::string name_utf8 = unicode_cast<utf8::string>(name_ucs4);
if(name != name_utf8){ // name is invalid UTF-8
return false;
}
return std::find_if(name_ucs4.begin(), name_ucs4.end(), addon_filename_ucs4char_illegal()) == name_ucs4.end();
}
}
namespace {
bool check_names_legal_internal(const config& dir, std::string current_prefix, std::vector<std::string>* badlist)
{
if (!current_prefix.empty()) {
current_prefix += '/';
}
for(const config& path : dir.child_range("file")) {
const std::string& filename = path["name"];
if(!addon_filename_legal(filename)) {
if(badlist) {
badlist->push_back(current_prefix + filename);
} else {
return false;
}
}
}
for(const config& path : dir.child_range("dir")) {
const std::string& dirname = path["name"];
const std::string& new_prefix = current_prefix + dirname;
if(!addon_filename_legal(dirname)) {
if(badlist) {
badlist->push_back(new_prefix + "/");
} else {
return false;
}
}
// Recurse into subdir.
if(!check_names_legal_internal(path, new_prefix, badlist) && !badlist) {
return false;
}
}
return badlist ? badlist->empty() : true;
}
bool check_case_insensitive_duplicates_internal(const config& dir, std::string prefix, std::vector<std::string>* badlist){
std::set<std::string> filenames;
bool inserted;
for (const config &path : dir.child_range("file")) {
const config::attribute_value &filename = path["name"];
std::tie(std::ignore, inserted) = filenames.insert(boost::algorithm::to_lower_copy(filename.str(), std::locale::classic()));
if (!inserted){
if(badlist){
badlist->push_back(prefix + filename.str());
} else {
return false;
}
}
}
for (const config &path : dir.child_range("dir")) {
const config::attribute_value &filename = path["name"];
std::tie(std::ignore, inserted) = filenames.insert(boost::algorithm::to_lower_copy(filename.str(), std::locale::classic()));
if (!inserted) {
if(badlist){
badlist->push_back(prefix + filename.str());
} else {
return false;
}
}
if (!check_case_insensitive_duplicates_internal(path, prefix + filename + "/", badlist) && !badlist){
return false;
}
}
return badlist ? badlist->empty() : true;
}
} // end unnamed namespace 3
bool check_names_legal(const config& dir, std::vector<std::string>* badlist)
{
// Usually our caller is passing us the root [dir] for an add-on, which
// shall contain a single subdir named after the add-on itself, so we can
// start with an empty display prefix and that'll reflect the addon
// structure correctly (e.g. "Addon_Name/~illegalfilename1").
return check_names_legal_internal(dir, "", badlist);
}
bool check_case_insensitive_duplicates(const config& dir, std::vector<std::string>* badlist){
return check_case_insensitive_duplicates_internal(dir, "", badlist);
}
ADDON_TYPE get_addon_type(const std::string& str)
{
if (str.empty())
return ADDON_UNKNOWN;
unsigned addon_type_num = 0;
while(++addon_type_num != ADDON_TYPES_COUNT) {
if(str == addon_type_strings[addon_type_num]) {
return ADDON_TYPE(addon_type_num);
}
}
return ADDON_UNKNOWN;
}
std::string get_addon_type_string(ADDON_TYPE type)
{
assert(type != ADDON_TYPES_COUNT);
return addon_type_strings[type];
}
namespace {
const char escape_char = '\x01'; /**< Binary escape char. */
} // end unnamed namespace 2
bool needs_escaping(char c) {
switch(c) {
case '\x00':
case escape_char:
case '\x0D': //Windows -- carriage return
case '\xFE': //Parser code -- textdomain or linenumber&filename
return true;
default:
return false;
}
}
std::string encode_binary(const std::string& str)
{
std::string res;
res.resize(str.size());
size_t n = 0;
for(std::string::const_iterator j = str.begin(); j != str.end(); ++j) {
if(needs_escaping(*j)) {
res.resize(res.size()+1);
res[n++] = escape_char;
res[n++] = *j + 1;
} else {
res[n++] = *j;
}
}
return res;
}
std::string unencode_binary(const std::string& str)
{
std::string res;
res.resize(str.size());
size_t n = 0;
for(std::string::const_iterator j = str.begin(); j != str.end(); ++j) {
if(*j == escape_char && j+1 != str.end()) {
++j;
res[n++] = *j - 1;
res.resize(res.size()-1);
} else {
res[n++] = *j;
}
}
return res;
}