-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathutils.h
396 lines (329 loc) · 11.4 KB
/
utils.h
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
395
396
/*
This file is part of FlashMQ (https://www.flashmq.org)
Copyright (C) 2021-2023 Wiebe Cazemier
FlashMQ is free software: you can redistribute it and/or modify
it under the terms of The Open Software License 3.0 (OSL-3.0).
See LICENSE for license details.
*/
#ifndef UTILS_H
#define UTILS_H
#include <string.h>
#include <errno.h>
#include <string>
#include <list>
#include <limits>
#include <vector>
#include <algorithm>
#include <openssl/evp.h>
#include <memory>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/random.h>
#include <stdexcept>
#include <cassert>
#include "cirbuf.h"
#include "bindaddr.h"
#include "types.h"
#include "flashmq_plugin.h"
#include "threadlocalutils.h"
#define UNUSED(expr) do { (void)(expr); } while (0)
template<typename T> int check(int rc)
{
if (rc < 0)
{
char *err = strerror(errno);
std::string msg(err);
throw T(msg);
}
return rc;
}
std::list<std::string> split(const std::string &input, const char sep, size_t max = std::numeric_limits<int>::max(), bool keep_empty_parts = true);
std::vector<std::string> splitToVector(const std::string &input, const char sep, size_t max = std::numeric_limits<int>::max(), bool keep_empty_parts = true);
std::vector<std::string> splitTopic(const std::string &topic);
bool isValidUtf8Generic(const char *s, bool alsoCheckInvalidPublishChars = false);
template<typename T>
bool isValidUtf8Generic(const T &s, bool alsoCheckInvalidPublishChars = false)
{
int multibyte_remain = 0;
uint32_t cur_code_point = 0;
int total_char_len = 0;
for(const uint8_t x : s)
{
if (alsoCheckInvalidPublishChars && (x == '#' || x == '+'))
return false;
if(!multibyte_remain)
{
cur_code_point = 0;
if ((x & 0b10000000) == 0) // when the MSB is 0, it's ASCII, most common case
{
cur_code_point += (x & 0b01111111);
}
else if((x & 0b11100000) == 0b11000000) // 2 byte char
{
multibyte_remain = 1;
cur_code_point += ((x & 0b00011111) << 6);
}
else if((x & 0b11110000) == 0b11100000) // 3 byte char
{
multibyte_remain = 2;
cur_code_point += ((x & 0b00001111) << 12);
}
else if((x & 0b11111000) == 0b11110000) // 4 byte char
{
multibyte_remain = 3;
cur_code_point += ((x & 0b00000111) << 18);
}
else if((x & 0b10000000) != 0)
return false;
else
cur_code_point += (x & 0b01111111);
total_char_len = multibyte_remain + 1;
}
else // All remainer bytes of this code point needs to start with 10
{
if((x & 0b11000000) != 0b10000000)
return false;
multibyte_remain--;
cur_code_point += ((x & 0b00111111) << (6*multibyte_remain));
}
if (multibyte_remain == 0)
{
// Check overlong values, to avoid having mulitiple representations of the same value.
if (total_char_len == 1)
{
}
else if (total_char_len == 2 && cur_code_point < 0x80)
return false;
else if (total_char_len == 3 && cur_code_point < 0x800)
return false;
else if (total_char_len == 4 && cur_code_point < 0x10000)
return false;
if (cur_code_point <= 0x001F)
return false;
if (cur_code_point >= 0x007F && cur_code_point <= 0x009F)
return false;
if (total_char_len > 1)
{
// Invalid range for MQTT. [MQTT-1.5.3-1]
if (cur_code_point >= 0xD800 && cur_code_point <= 0xDFFF) // Dec 55296-57343
return false;
// Unicode spec: "Which code points are noncharacters?".
if (cur_code_point >= 0xFDD0 && cur_code_point <= 0xFDEF)
return false;
// The last two code points of each of the 17 planes are the remaining 34 non-chars.
const uint32_t plane = (cur_code_point & 0x1F0000) >> 16;
const uint32_t last_16_bit = cur_code_point & 0xFFFF;
if (plane <= 16 && (last_16_bit == 0xFFFE || last_16_bit == 0xFFFF))
return false;
}
cur_code_point = 0;
}
}
return multibyte_remain == 0;
}
template<typename T>
bool isValidUtf8(const T &s, bool alsoCheckInvalidPublishChars = false)
{
#ifdef __SSE4_2__
thread_local static SimdUtils simdUtils;
return simdUtils.isValidUtf8(s, alsoCheckInvalidPublishChars);
#else
return isValidUtf8Generic(s, alsoCheckInvalidPublishChars);
#endif
}
bool strContains(const std::string &s, const std::string &needle);
bool isValidShareName(const std::string &s);
bool isValidPublishPath(const std::string &s);
bool isValidSubscribePath(const std::string &s);
bool containsDangerousCharacters(const std::string &s);
void ltrim(std::string &s);
void rtrim(std::string &s);
void trim(std::string &s);
bool startsWith(const std::string &s, const std::string &needle);
bool endsWith(const std::string &s, const std::string &ending);
std::string &rtrim(std::string &s, unsigned char c);
std::string getSecureRandomString(const ssize_t len);
std::string str_tolower(std::string s);
bool stringTruthiness(const std::string &val);
bool isPowerOfTwo(int val);
bool parseHttpHeader(CirBuf &buf, std::string &websocket_key, int &websocket_version, std::string &subprotocol, std::string &xRealIp);
std::vector<char> base64Decode(const std::string &s);
std::string base64Encode(const unsigned char *input, const int length);
std::string generateWebsocketAcceptString(const std::string &websocketKey);
std::string generateInvalidWebsocketVersionHttpHeaders(const int wantedVersion);
std::string generateBadHttpRequestReponse(const std::string &msg);
std::string generateWebsocketAnswer(const std::string &acceptString, const std::string &subprotocol);
void testSsl(const std::string &fullchain, const std::string &privkey);
void testSslVerifyLocations(const std::string &caFile, const std::string &caDir, const std::string &error);
std::string formatString(const std::string str, ...);
std::string_view dirnameOf(std::string_view fname);
BindAddr getBindAddr(int family, const std::string &bindAddress, int port);
size_t getFileSize(const std::string &path);
size_t getFreeSpace(const std::string &path);
std::string sockaddrToString(const struct sockaddr *addr);
template<typename ex> void checkWritableDir(const std::string &path)
{
if (path.empty())
throw ex("Dir path to check is an empty string.");
if (access(path.c_str(), W_OK) != 0)
{
std::string msg = formatString("Path '%s' is not there or not writable", path.c_str());
throw ex(msg);
}
struct stat statbuf;
memset(&statbuf, 0, sizeof(struct stat));
if (stat(path.c_str(), &statbuf) < 0)
{
// We checked for W_OK above, so this shouldn't happen.
std::string msg = formatString("Error getting information about '%s'.", path.c_str());
throw ex(msg);
}
if (!S_ISDIR(statbuf.st_mode))
{
std::string msg = formatString("Path '%s' is not a directory.", path.c_str());
throw ex(msg);
}
}
std::string websocketCloseCodeToString(uint16_t code);
std::string protocolVersionString(ProtocolVersion p);
unsigned int distanceBetweenStrings(const std::string &stringA, const std::string &stringB);
template<typename it>
it findCloseStringMatch(it first, it last, const std::string &s)
{
it alternative = last;
unsigned int alternative_distance = UINT_MAX;
for (auto possible_key = first; possible_key != last; ++possible_key)
{
unsigned int distance = distanceBetweenStrings(s, *possible_key);
// We only want to suggest options that look a bit like the unknown
// one. Experimentally I found 50% of the total length a decent
// cutoff.
//
// The mathemathical formula "distance/length < 0.5" can be
// approximated with integers as "distance*2/length < 1"
if ((distance * 2) / s.length() < 1 && distance < alternative_distance)
{
alternative = possible_key;
alternative_distance = distance;
}
}
return alternative;
}
uint32_t ageFromTimePoint(const std::chrono::time_point<std::chrono::steady_clock> &point);
std::chrono::time_point<std::chrono::steady_clock> timepointFromAge(const uint32_t age);
ReasonCodes authResultToReasonCode(AuthResult authResult);
int maskAllSignalsCurrentThread();
void parseSubscriptionShare(std::vector<std::string> &subtopics, std::string &shareName, std::string &topic);
std::string timestampWithMillis();
template<class T>
T get_random_int()
{
std::vector<T> buf(1);
// We use urandom, so we don't have check for blocking / interrupted conditions.
if (getrandom(buf.data(), sizeof(T), 0) < 0)
throw std::runtime_error(strerror(errno));
T val = buf.at(0);
return val;
}
void exceptionOnNonMqtt(const std::vector<char> &data);
uint16_t getFirstWildcardDepth(const std::vector<std::string> &subtopics);
std::string reasonCodeToString(ReasonCodes code);
std::string packetTypeToString(PacketType ptype);
std::string propertyToString(Mqtt5Properties p);
/**
* @brief parseValuesWithOptionalQuoting parses argument lists space encoded, with quote and escaping support.
* @param s
* @return
*
* So, like
*
* "hallo" "you": becomes a vector with those two strings, but without the quote.
* hallo you: is the same as above.
* "hallo" you: is the same as above.
* "hallo you": is a vector with one element.
* "I quote you with \"" is: I quote you with "
* 'I quote you with "' is: I quote you with "
*/
template<typename ex>
std::vector<std::string> parseValuesWithOptionalQuoting(std::string s)
{
trim(s);
std::vector<std::string> result;
char quote = 0;
std::string cur;
bool escape = false;
for (char c : s)
{
if (escape)
{
if (!(c == '"' || c == '\'' || c == '\\'))
throw ex("Invalid escape");
cur.push_back(c);
escape = false;
}
else if (c == '\\')
{
escape = true;
}
else if (std::isspace(c))
{
if (quote)
cur.push_back(c);
else if (!cur.empty())
{
result.push_back(cur);
cur.clear();
}
}
else if (c == '"' || c == '\'')
{
if (quote == 0)
{
quote = c;
}
else if (quote == c)
{
result.push_back(cur);
cur.clear();
quote = 0;
}
else
{
cur.push_back(c);
}
}
else
{
cur.push_back(c);
}
}
if (!cur.empty())
result.push_back(cur);
if (quote)
throw ex("Unterminated quote");
return result;
}
template<typename T>
class DecrementGuard
{
T &n;
public:
DecrementGuard(T &n) :
n(n)
{
}
~DecrementGuard()
{
n--;
assert(n >= 0);
}
};
template<typename T>
std::optional<T> &non_optional(std::optional<T> &o)
{
if (!o)
o.emplace();
return o;
}
#endif // UTILS_H