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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
|
#pragma once
#include <base/types.h>
#include <cstring>
#include <algorithm>
#include <type_traits>
#include <utility>
#include <base/range.h>
#include <base/unaligned.h>
#include <base/hex.h>
#include <Common/StringUtils/StringUtils.h>
#include <array>
constexpr size_t IPV4_BINARY_LENGTH = 4;
constexpr size_t IPV6_BINARY_LENGTH = 16;
constexpr size_t IPV4_MAX_TEXT_LENGTH = 15; /// Does not count tail zero byte.
constexpr size_t IPV6_MAX_TEXT_LENGTH = 45; /// Does not count tail zero byte.
namespace DB
{
extern const std::array<std::pair<const char *, size_t>, 256> one_byte_to_string_lookup_table;
/** Rewritten inet_ntop6 from http://svn.apache.org/repos/asf/apr/apr/trunk/network_io/unix/inet_pton.c
* performs significantly faster than the reference implementation due to the absence of sprintf calls,
* bounds checking, unnecessary string copying and length calculation.
*/
void formatIPv6(const unsigned char * src, char *& dst, uint8_t zeroed_tail_bytes_count = 0);
/** Unsafe (no bounds-checking for src nor dst), optimized version of parsing IPv4 string.
*
* Parses the input string `src` and stores binary host-endian value into buffer pointed by `dst`,
* which should be long enough.
* That is "127.0.0.1" becomes 0x7f000001.
*
* In case of failure doesn't modify buffer pointed by `dst`.
*
* WARNING - this function is adapted to work with ReadBuffer, where src is the position reference (ReadBuffer::position())
* and eof is the ReadBuffer::eof() - therefore algorithm below does not rely on buffer's continuity.
* To parse strings use overloads below.
*
* @param src - iterator (reference to pointer) over input string - warning - continuity is not guaranteed.
* @param eof - function returning true if iterator riched the end - warning - can break iterator's continuity.
* @param dst - where to put output bytes, expected to be non-null and at IPV4_BINARY_LENGTH-long.
* @param first_octet - preparsed first octet
* @return - true if parsed successfully, false otherwise.
*/
template <typename T, typename EOFfunction>
requires (std::is_same<typename std::remove_cv<T>::type, char>::value)
inline bool parseIPv4(T * &src, EOFfunction eof, unsigned char * dst, int32_t first_octet = -1)
{
if (src == nullptr || first_octet > 255)
return false;
UInt32 result = 0;
int offset = 24;
if (first_octet >= 0)
{
result |= first_octet << offset;
offset -= 8;
}
for (; true; offset -= 8, ++src)
{
if (eof())
return false;
UInt32 value = 0;
size_t len = 0;
while (isNumericASCII(*src) && len <= 3)
{
value = value * 10 + (*src - '0');
++len;
++src;
if (eof())
break;
}
if (len == 0 || value > 255 || (offset > 0 && (eof() || *src != '.')))
return false;
result |= value << offset;
if (offset == 0)
break;
}
memcpy(dst, &result, sizeof(result));
return true;
}
/// returns pointer to the right after parsed sequence or null on failed parsing
inline const char * parseIPv4(const char * src, const char * end, unsigned char * dst)
{
if (parseIPv4(src, [&src, end](){ return src == end; }, dst))
return src;
return nullptr;
}
/// returns true if whole buffer was parsed successfully
inline bool parseIPv4whole(const char * src, const char * end, unsigned char * dst)
{
return parseIPv4(src, end, dst) == end;
}
/// returns pointer to the right after parsed sequence or null on failed parsing
inline const char * parseIPv4(const char * src, unsigned char * dst)
{
if (parseIPv4(src, [](){ return false; }, dst))
return src;
return nullptr;
}
/// returns true if whole null-terminated string was parsed successfully
inline bool parseIPv4whole(const char * src, unsigned char * dst)
{
const char * end = parseIPv4(src, dst);
return end != nullptr && *end == '\0';
}
/** Unsafe (no bounds-checking for src nor dst), optimized version of parsing IPv6 string.
*
* Parses the input string `src` and stores binary big-endian value into buffer pointed by `dst`,
* which should be long enough. In case of failure zeroes IPV6_BINARY_LENGTH bytes of buffer pointed by `dst`.
*
* WARNING - this function is adapted to work with ReadBuffer, where src is the position reference (ReadBuffer::position())
* and eof is the ReadBuffer::eof() - therefore algorithm below does not rely on buffer's continuity.
* To parse strings use overloads below.
*
* @param src - iterator (reference to pointer) over input string - warning - continuity is not guaranteed.
* @param eof - function returning true if iterator riched the end - warning - can break iterator's continuity.
* @param dst - where to put output bytes, expected to be non-null and at IPV6_BINARY_LENGTH-long.
* @param first_block - preparsed first block
* @return - true if parsed successfully, false otherwise.
*/
template <typename T, typename EOFfunction>
requires (std::is_same<typename std::remove_cv<T>::type, char>::value)
inline bool parseIPv6(T * &src, EOFfunction eof, unsigned char * dst, int32_t first_block = -1)
{
const auto clear_dst = [dst]()
{
std::memset(dst, '\0', IPV6_BINARY_LENGTH);
return false;
};
if (src == nullptr || eof())
return clear_dst();
int groups = 0; /// number of parsed groups
unsigned char * iter = dst; /// iterator over dst buffer
unsigned char * zptr = nullptr; /// pointer into dst buffer array where all-zeroes block ("::") is started
std::memset(dst, '\0', IPV6_BINARY_LENGTH);
if (first_block >= 0)
{
*iter++ = static_cast<unsigned char>((first_block >> 8) & 0xffu);
*iter++ = static_cast<unsigned char>(first_block & 0xffu);
if (*src == ':')
{
zptr = iter;
++src;
}
++groups;
}
bool group_start = true;
while (!eof() && groups < 8)
{
if (*src == ':')
{
++src;
if (eof()) /// trailing colon is not allowed
return clear_dst();
group_start = true;
if (*src == ':')
{
if (zptr != nullptr) /// multiple all-zeroes blocks are not allowed
return clear_dst();
zptr = iter;
++src;
continue;
}
if (groups == 0) /// leading colon is not allowed
return clear_dst();
}
if (*src == '.') /// mixed IPv4 parsing
{
if (groups <= 1 && zptr == nullptr) /// IPv4 block can't be the first
return clear_dst();
if (group_start) /// first octet of IPv4 should be already parsed as an IPv6 group
return clear_dst();
++src;
if (eof())
return clear_dst();
/// last parsed group should be reinterpreted as a decimal value - it's the first octet of IPv4
--groups;
iter -= 2;
UInt16 num = 0;
for (int i = 0; i < 2; ++i)
{
unsigned char first = (iter[i] >> 4) & 0x0fu;
unsigned char second = iter[i] & 0x0fu;
if (first > 9 || second > 9)
return clear_dst();
(num *= 100) += first * 10 + second;
}
if (num > 255)
return clear_dst();
/// parse IPv4 with known first octet
if (!parseIPv4(src, eof, iter, num))
return clear_dst();
if constexpr (std::endian::native == std::endian::little)
std::reverse(iter, iter + IPV4_BINARY_LENGTH);
iter += 4;
groups += 2;
break; /// IPv4 block is the last - end of parsing
}
if (!group_start) /// end of parsing
break;
group_start = false;
UInt16 val = 0; /// current decoded group
int xdigits = 0; /// number of decoded hex digits in current group
for (; !eof() && xdigits < 4; ++src, ++xdigits)
{
UInt8 num = unhex(*src);
if (num == 0xFF)
break;
(val <<= 4) |= num;
}
if (xdigits == 0) /// end of parsing
break;
*iter++ = static_cast<unsigned char>((val >> 8) & 0xffu);
*iter++ = static_cast<unsigned char>(val & 0xffu);
++groups;
}
/// either all 8 groups or all-zeroes block should be present
if (groups < 8 && zptr == nullptr)
return clear_dst();
if (zptr != nullptr) /// process all-zeroes block
{
size_t msize = iter - zptr;
std::memmove(dst + IPV6_BINARY_LENGTH - msize, zptr, msize);
std::memset(zptr, '\0', IPV6_BINARY_LENGTH - (iter - dst));
}
return true;
}
/// returns pointer to the right after parsed sequence or null on failed parsing
inline const char * parseIPv6(const char * src, const char * end, unsigned char * dst)
{
if (parseIPv6(src, [&src, end](){ return src == end; }, dst))
return src;
return nullptr;
}
/// returns true if whole buffer was parsed successfully
inline bool parseIPv6whole(const char * src, const char * end, unsigned char * dst)
{
return parseIPv6(src, end, dst) == end;
}
/// returns pointer to the right after parsed sequence or null on failed parsing
inline const char * parseIPv6(const char * src, unsigned char * dst)
{
if (parseIPv6(src, [](){ return false; }, dst))
return src;
return nullptr;
}
/// returns true if whole null-terminated string was parsed successfully
inline bool parseIPv6whole(const char * src, unsigned char * dst)
{
const char * end = parseIPv6(src, dst);
return end != nullptr && *end == '\0';
}
/** Unsafe (no bounds-checking for src nor dst), optimized version of parsing IPv6 string.
*
* Parses the input string `src` IPv6 or possible IPv4 into IPv6 and stores binary big-endian value into buffer pointed by `dst`,
* which should be long enough. In case of failure zeroes IPV6_BINARY_LENGTH bytes of buffer pointed by `dst`.
*
* WARNING - this function is adapted to work with ReadBuffer, where src is the position reference (ReadBuffer::position())
* and eof is the ReadBuffer::eof() - therefore algorithm below does not rely on buffer's continuity.
*
* @param src - iterator (reference to pointer) over input string - warning - continuity is not guaranteed.
* @param eof - function returning true if iterator riched the end - warning - can break iterator's continuity.
* @param dst - where to put output bytes, expected to be non-null and at IPV6_BINARY_LENGTH-long.
* @return - true if parsed successfully, false otherwise.
*/
template <typename T, typename EOFfunction>
requires (std::is_same<typename std::remove_cv<T>::type, char>::value)
inline bool parseIPv6orIPv4(T * &src, EOFfunction eof, unsigned char * dst)
{
const auto clear_dst = [dst]()
{
std::memset(dst, '\0', IPV6_BINARY_LENGTH);
return false;
};
if (src == nullptr)
return clear_dst();
bool leading_zero = false;
uint16_t val = 0;
int digits = 0;
/// parse up to 4 first digits as hexadecimal
for (; !eof() && digits < 4; ++src, ++digits)
{
if (*src == ':' || *src == '.')
break;
if (digits == 0 && *src == '0')
leading_zero = true;
UInt8 num = unhex(*src);
if (num == 0xFF)
return clear_dst();
(val <<= 4) |= num;
}
if (eof())
return clear_dst();
if (*src == ':') /// IPv6
{
if (digits == 0) /// leading colon - no preparsed group
return parseIPv6(src, eof, dst);
++src;
return parseIPv6(src, eof, dst, val); /// parse with first preparsed group
}
if (*src == '.') /// IPv4
{
/// should has some digits
if (digits == 0)
return clear_dst();
/// should not has leading zeroes, should has no more than 3 digits
if ((leading_zero && digits > 1) || digits > 3)
return clear_dst();
/// recode first group as decimal
UInt16 num = 0;
for (int exp = 1; exp < 1000; exp *= 10)
{
int n = val & 0x0fu;
if (n > 9)
return clear_dst();
num += n * exp;
val >>= 4;
}
if (num > 255)
return clear_dst();
++src;
if (!parseIPv4(src, eof, dst, num)) /// try to parse as IPv4 with preparsed first octet
return clear_dst();
/// convert into IPv6
if constexpr (std::endian::native == std::endian::little)
{
dst[15] = dst[0]; dst[0] = 0;
dst[14] = dst[1]; dst[1] = 0;
dst[13] = dst[2]; dst[2] = 0;
dst[12] = dst[3]; dst[3] = 0;
}
else
{
dst[15] = dst[3]; dst[3] = 0;
dst[14] = dst[2]; dst[2] = 0;
dst[13] = dst[1]; dst[1] = 0;
dst[12] = dst[0]; dst[0] = 0;
}
dst[11] = 0xff;
dst[10] = 0xff;
return true;
}
return clear_dst();
}
/** Format 4-byte binary sequesnce as IPv4 text: 'aaa.bbb.ccc.ddd',
* expects in out to be in BE-format, that is 0x7f000001 => "127.0.0.1".
*
* Any number of the tail bytes can be masked with given mask string.
*
* Assumptions:
* src is IPV4_BINARY_LENGTH long,
* dst is IPV4_MAX_TEXT_LENGTH long,
* mask_tail_octets <= IPV4_BINARY_LENGTH
* mask_string is NON-NULL, if mask_tail_octets > 0.
*
* Examples:
* formatIPv4(&0x7f000001, dst, mask_tail_octets = 0, nullptr);
* > dst == "127.0.0.1"
* formatIPv4(&0x7f000001, dst, mask_tail_octets = 1, "xxx");
* > dst == "127.0.0.xxx"
* formatIPv4(&0x7f000001, dst, mask_tail_octets = 1, "0");
* > dst == "127.0.0.0"
*/
inline void formatIPv4(const unsigned char * src, size_t src_size, char *& dst, uint8_t mask_tail_octets = 0, const char * mask_string = "xxx")
{
const size_t mask_length = mask_string ? strlen(mask_string) : 0;
const size_t limit = std::min(IPV4_BINARY_LENGTH, IPV4_BINARY_LENGTH - mask_tail_octets);
const size_t padding = std::min(4 - src_size, limit);
for (size_t octet = 0; octet < padding; ++octet)
{
*dst++ = '0';
*dst++ = '.';
}
for (size_t octet = 4 - src_size; octet < limit; ++octet)
{
uint8_t value = 0;
if constexpr (std::endian::native == std::endian::little)
value = static_cast<uint8_t>(src[IPV4_BINARY_LENGTH - octet - 1]);
else
value = static_cast<uint8_t>(src[octet]);
const uint8_t len = one_byte_to_string_lookup_table[value].second;
const char* str = one_byte_to_string_lookup_table[value].first;
memcpy(dst, str, len);
dst += len;
*dst++ = '.';
}
for (size_t mask = 0; mask < mask_tail_octets; ++mask)
{
memcpy(dst, mask_string, mask_length);
dst += mask_length;
*dst++ = '.';
}
dst[-1] = '\0';
}
inline void formatIPv4(const unsigned char * src, char *& dst, uint8_t mask_tail_octets = 0, const char * mask_string = "xxx")
{
formatIPv4(src, 4, dst, mask_tail_octets, mask_string);
}
}
|