Coverage Report

Created: 2026-03-13 09:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exprs/function/string_hex_util.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
18
#pragma once
19
20
#include "core/column/column_string.h"
21
#include "util/simd/vstring_function.h"
22
23
namespace doris::string_hex {
24
25
static constexpr int MAX_STACK_CIPHER_LEN = 1024 * 64;
26
27
2.66k
inline bool check_and_decode_one(char& c, const char src_c, bool flag) {
28
2.66k
    int k = flag ? 16 : 1;
29
2.66k
    int value = src_c - '0';
30
    // 9 = ('9'-'0')
31
2.66k
    if (value >= 0 && value <= 9) {
32
1.88k
        c += value * k;
33
1.88k
        return true;
34
1.88k
    }
35
36
783
    value = src_c - 'A';
37
    // 5 = ('F'-'A')
38
783
    if (value >= 0 && value <= 5) {
39
499
        c += (value + 10) * k;
40
499
        return true;
41
499
    }
42
43
284
    value = src_c - 'a';
44
    // 5 = ('f'-'a')
45
284
    if (value >= 0 && value <= 5) {
46
254
        c += (value + 10) * k;
47
254
        return true;
48
254
    }
49
    // not in ( ['0','9'], ['a','f'], ['A','F'] )
50
30
    return false;
51
284
}
52
53
335
inline int hex_decode(const char* src_str, ColumnString::Offset src_len, char* dst_str) {
54
    // if str length is odd or 0, return empty string like mysql dose.
55
335
    if ((src_len & 1) != 0 or src_len == 0) {
56
123
        return 0;
57
123
    }
58
    //check and decode one character at the same time
59
    // character in ( ['0','9'], ['a','f'], ['A','F'] ), return 'NULL' like mysql dose.
60
1.53k
    for (auto i = 0, dst_index = 0; i < src_len; i += 2, dst_index++) {
61
1.33k
        char c = 0;
62
        // combine two character into dst_str one character
63
1.33k
        bool left_4bits_flag = check_and_decode_one(c, *(src_str + i), true);
64
1.33k
        bool right_4bits_flag = check_and_decode_one(c, *(src_str + i + 1), false);
65
66
1.33k
        if (!left_4bits_flag || !right_4bits_flag) {
67
15
            return 0;
68
15
        }
69
1.31k
        *(dst_str + dst_index) = c;
70
1.31k
    }
71
197
    return src_len / 2;
72
212
}
73
74
inline void hex_encode(const unsigned char* source, size_t srclen, unsigned char*& dst_data_ptr,
75
1.24k
                       size_t& offset) {
76
1.24k
    if (srclen != 0) {
77
1.19k
        doris::simd::VStringFunctions::hex_encode(source, srclen,
78
1.19k
                                                  reinterpret_cast<char*>(dst_data_ptr));
79
1.19k
        dst_data_ptr += (srclen * 2);
80
1.19k
        offset += (srclen * 2);
81
1.19k
    }
82
1.24k
}
83
84
} // namespace doris::string_hex