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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
|
/*
* InspIRCd -- Internet Relay Chat Daemon
*
* Copyright (C) 2017, 2022 Sadie Powell <sadie@witchery.services>
* Copyright (C) 2013-2014 Attila Molnar <attilamolnar@hush.com>
* Copyright (C) 2012 Robby <robby@chatbelgie.be>
* Copyright (C) 2010 Craig Edwards <brain@inspircd.org>
* Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
* Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
* Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
* Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
*
* This file is part of InspIRCd. InspIRCd 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, version 2.
*
* 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, see <http://www.gnu.org/licenses/>.
*/
#include <fmt/format.cc>
#include "inspircd.h"
#include "stringutils.h"
#include "utility/string.h"
std::string Percent::Encode(const void* data, size_t length, const char* table, bool upper)
{
if (!table)
table = Percent::TABLE;
// Preallocate the output buffer to avoid constant reallocations.
std::string buffer;
buffer.reserve(length * 3);
const char* hex_table = upper ? Hex::TABLE_UPPER : Hex::TABLE_LOWER;
const unsigned char* udata = reinterpret_cast<const unsigned char*>(data);
for (size_t idx = 0; idx < length; ++idx)
{
unsigned char chr = udata[idx];
if (strchr(table, chr))
{
// The character is on the safe list; push it as is.
buffer.push_back(chr);
}
else
{
// The character is not on the safe list; percent encode it.
buffer.push_back('%');
buffer.push_back(hex_table[chr >> 4]);
buffer.push_back(hex_table[chr & 15]);
}
}
return buffer;
}
std::string Percent::Decode(const void* data, size_t length)
{
// Preallocate the output buffer to avoid constant reallocations.
std::string buffer;
buffer.reserve(length * 3);
const char* cdata = reinterpret_cast<const char*>(data);
for (size_t idx = 0; idx < length; ++idx)
{
if (cdata[idx] == '%')
{
// Percent encoding encodes two octets into 1-2 characters.
const char octet1 = ++idx < length ? toupper(cdata[idx]) : 0;
const char octet2 = ++idx < length ? toupper(cdata[idx]) : 0;
const char* table1 = strchr(Hex::TABLE_UPPER, octet1);
const char* table2 = strchr(Hex::TABLE_UPPER, octet2);
char pair = ((table1 ? table1 - Hex::TABLE_UPPER : 0) << 4) + (table2 ? table2 - Hex::TABLE_UPPER : 0);
buffer.push_back(pair);
}
else
{
buffer.push_back(cdata[idx]);
}
}
return buffer;
}
std::string Hex::Encode(const void* data, size_t length, const char* table, char separator)
{
if (!table)
table = Hex::TABLE_LOWER;
// Preallocate the output buffer to avoid constant reallocations.
std::string buffer;
buffer.reserve((length * 2) + (!!separator * length));
const unsigned char* udata = reinterpret_cast<const unsigned char*>(data);
for (size_t idx = 0; idx < length; ++idx)
{
if (idx && separator)
buffer.push_back(separator);
const unsigned char chr = udata[idx];
buffer.push_back(table[chr >> 4]);
buffer.push_back(table[chr & 15]);
}
return buffer;
}
std::string Hex::Decode(const void* data, size_t length, const char* table, char separator)
{
if (!table)
table = Hex::TABLE_LOWER;
// The size of each hex segment.
size_t segment = (separator ? 3 : 2);
// Preallocate the output buffer to avoid constant reallocations.
std::string buffer;
buffer.reserve(length / segment);
const char* cdata = static_cast<const char*>(data);
for (size_t idx = 0; idx + 1 < length; idx += segment)
{
// Attempt to find the octets in the table.
const char* table1 = strchr(table, cdata[idx]);
const char* table2 = strchr(table, cdata[idx + 1]);
char pair = ((table1 ? table1 - table : 0) << 4) + (table2 ? table2 - table : 0);
buffer.push_back(pair);
}
return buffer;
}
std::string Base64::Encode(const void* data, size_t length, const char* table, char padding)
{
// Use the default table if one is not specified.
if (!table)
table = Base64::TABLE;
// Preallocate the output buffer to avoid constant reallocations.
std::string buffer;
buffer.reserve(4 * ((length + 2) / 3));
const uint8_t* udata = static_cast<const uint8_t*>(data);
for (size_t idx = 0; idx < length; )
{
// Base64 encodes three octets into four characters.
uint32_t octet1 = idx < length ? udata[idx++] : 0;
uint32_t octet2 = idx < length ? udata[idx++] : 0;
uint32_t octet3 = idx < length ? udata[idx++] : 0;
uint32_t triple = (octet1 << 16) + (octet2 << 8) + octet3;
buffer.push_back(table[(triple >> 3 * 6) & 63]);
buffer.push_back(table[(triple >> 2 * 6) & 63]);
buffer.push_back(table[(triple >> 1 * 6) & 63]);
buffer.push_back(table[(triple >> 0 * 6) & 63]);
}
static constexpr size_t padding_count[] = { 0, 2, 1 };
if (padding)
{
// Replace any trailing characters with padding.
for (size_t idx = 0; idx < padding_count[length % 3]; ++idx)
buffer[buffer.length() - 1 - idx] = '=';
}
else
{
// Remove any trailing characters.
buffer.erase(buffer.length() - padding_count[length % 3]);
}
return buffer;
}
std::string Base64::Decode(const void* data, size_t length, const char* table)
{
if (!table)
table = Base64::TABLE;
// Preallocate the output buffer to avoid constant reallocations.
std::string buffer;
buffer.reserve((length / 4) * 3);
uint32_t current_bits = 0;
size_t seen_bits = 0;
const char* cdata = static_cast<const char*>(data);
for (size_t idx = 0; idx < length; ++idx)
{
// Attempt to find the octet in the table.
const char* chr = strchr(table, cdata[idx]);
if (!chr)
continue; // Skip invalid octets.
// Add the bits for this octet to the active buffer.
current_bits = (current_bits << 6) | uint32_t(chr - table);
seen_bits += 6;
if (seen_bits >= 8)
{
// We have seen an entire octet; add it to the buffer.
seen_bits -= 8;
buffer.push_back((current_bits >> seen_bits) & 0xFF);
}
}
return buffer;
}
std::string Template::Replace(const std::string& str, const VariableMap& vars)
{
std::string out;
out.reserve(str.length());
for (size_t idx = 0; idx < str.size(); ++idx)
{
if (str[idx] != '%')
{
out.push_back(str[idx]);
continue;
}
for (size_t endidx = idx + 1; endidx < str.size(); ++endidx)
{
if (str[endidx] == '%')
{
if (endidx - idx == 1)
{
// foo%%bar is an escape of foo%bar
out.push_back('%');
idx = endidx;
break;
}
auto var = vars.find(str.substr(idx + 1, endidx - idx - 1));
if (var != vars.end())
{
// We have a variable, replace it in the string.
out.append(var->second);
}
idx = endidx;
break;
}
}
}
return out;
}
bool InspIRCd::TimingSafeCompare(const std::string& one, const std::string& two)
{
if (one.length() != two.length())
return false;
unsigned int diff = 0;
for (std::string::const_iterator i = one.begin(), j = two.begin(); i != one.end(); ++i, ++j)
{
unsigned char a = static_cast<unsigned char>(*i);
unsigned char b = static_cast<unsigned char>(*j);
diff |= a ^ b;
}
return (diff == 0);
}
TokenList::TokenList(const std::string& tokenlist)
{
AddList(tokenlist);
}
void TokenList::AddList(const std::string& tokenlist)
{
std::string token;
irc::spacesepstream tokenstream(tokenlist);
while (tokenstream.GetToken(token))
{
if (token[0] == '-')
Remove(token.substr(1));
else
Add(token);
}
}
void TokenList::Add(const std::string& token)
{
// If the token is empty or contains just whitespace it is invalid.
if (token.empty() || token.find_first_not_of(" \t") == std::string::npos)
return;
// If the token is a wildcard entry then permissive mode has been enabled.
if (token == "*")
{
permissive = true;
tokens.clear();
return;
}
// If we are in permissive mode then remove the token from the token list.
// Otherwise, add it to the token list.
if (permissive)
tokens.erase(token);
else
tokens.insert(token);
}
void TokenList::Clear()
{
permissive = false;
tokens.clear();
}
bool TokenList::Contains(const std::string& token) const
{
// If we are in permissive mode and the token is in the list
// then we don't have it.
if (permissive && tokens.find(token) != tokens.end())
return false;
// If we are not in permissive mode and the token is not in
// the list then we don't have it.
if (!permissive && tokens.find(token) == tokens.end())
return false;
// We have the token!
return true;
}
void TokenList::Remove(const std::string& token)
{
// If the token is empty or contains just whitespace it is invalid.
if (token.empty() || token.find_first_not_of(" \t") == std::string::npos)
return;
// If the token is a wildcard entry then permissive mode has been disabled.
if (token == "*")
{
permissive = false;
tokens.clear();
return;
}
// If we are in permissive mode then add the token to the token list.
// Otherwise, remove it from the token list.
if (permissive)
tokens.insert(token);
else
tokens.erase(token);
}
std::string TokenList::ToString() const
{
if (permissive)
{
// If the token list is in permissive mode then the tokens are a list
// of disallowed tokens.
std::string buffer("*");
for (const auto& token : tokens)
buffer.append(" -").append(token);
return buffer;
}
// If the token list is not in permissive mode then the token list is just
// a list of allowed tokens.
return insp::join(tokens);
}
bool TokenList::operator==(const TokenList& other) const
{
// Both sets must be in the same mode to be equal.
if (permissive != other.permissive)
return false;
// Both sets must be the same size to be equal.
if (tokens.size() != other.tokens.size())
return false;
for (const auto& token : tokens)
{
// Both sets must contain the same tokens to be equal.
if (other.tokens.find(token) == other.tokens.end())
return false;
}
return true;
}
|