be/src/exec/common/ipv6_to_binary.h
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | // This file is copied from |
18 | | // https://github.com/ClickHouse/ClickHouse/blob/master/src/Common/IPv6ToBinary.cpp |
19 | | // and modified by Doris |
20 | | |
21 | | #pragma once |
22 | | |
23 | | #include "exec/common/format_ip.h" |
24 | | |
25 | | namespace doris { |
26 | | |
27 | | /// Result array could be indexed with all possible uint8 values without extra check. |
28 | | /// For values greater than 128 we will store same value as for 128 (all bits set). |
29 | | constexpr size_t IPV6_MASKS_COUNT = 256; |
30 | | using RawMaskArrayV6 = std::array<uint8_t, IPV6_BINARY_LENGTH>; |
31 | | |
32 | | template <typename RawMaskArrayT> |
33 | 0 | static constexpr RawMaskArrayT generate_bit_mask(size_t prefix) { |
34 | 0 | RawMaskArrayT arr {0}; |
35 | 0 | if (prefix >= arr.size() * 8) { |
36 | 0 | prefix = arr.size() * 8; |
37 | 0 | } |
38 | 0 | int8_t i = IPV6_BINARY_LENGTH - 1; |
39 | 0 | for (; prefix >= 8; --i, prefix -= 8) { |
40 | 0 | arr[i] = 0xff; |
41 | 0 | } |
42 | 0 | if (prefix > 0) { |
43 | 0 | arr[i--] = ~(0xff >> prefix); |
44 | 0 | } |
45 | 0 | while (i >= 0) { |
46 | 0 | arr[i--] = 0x00; |
47 | 0 | } |
48 | 0 | return arr; |
49 | 0 | } Unexecuted instantiation: function_ip.cpp:_ZN5dorisL17generate_bit_maskISt5arrayIhLm16EEEET_m Unexecuted instantiation: ip_address_dictionary.cpp:_ZN5dorisL17generate_bit_maskISt5arrayIhLm16EEEET_m |
50 | | |
51 | | template <typename RawMaskArrayT, size_t masksCount> |
52 | 0 | static constexpr std::array<RawMaskArrayT, masksCount> generate_bit_masks() { |
53 | 0 | std::array<RawMaskArrayT, masksCount> arr {}; |
54 | 0 | for (size_t i = 0; i < masksCount; ++i) { |
55 | 0 | arr[i] = generate_bit_mask<RawMaskArrayT>(i); |
56 | 0 | } |
57 | 0 | return arr; |
58 | 0 | } Unexecuted instantiation: function_ip.cpp:_ZN5dorisL18generate_bit_masksISt5arrayIhLm16EELm256EEES1_IT_XT0_EEv Unexecuted instantiation: ip_address_dictionary.cpp:_ZN5dorisL18generate_bit_masksISt5arrayIhLm16EELm256EEES1_IT_XT0_EEv |
59 | | |
60 | | /// Returns a reference to 16-byte array containing mask with first `prefix_len` bits set to `1` and `128 - prefix_len` to `0`. |
61 | | /// Store in little-endian byte order |
62 | | /// The reference is valid during all program execution time. |
63 | | /// Values of prefix_len greater than 128 interpreted as 128 exactly. |
64 | 125 | inline const std::array<uint8_t, 16>& get_cidr_mask_ipv6(uint8_t prefix_len) { |
65 | 125 | static constexpr auto IPV6_RAW_MASK_ARRAY = |
66 | 125 | generate_bit_masks<RawMaskArrayV6, IPV6_MASKS_COUNT>(); |
67 | 125 | return IPV6_RAW_MASK_ARRAY[prefix_len]; |
68 | 125 | } |
69 | | |
70 | | } // namespace doris |