Coverage Report

Created: 2025-06-13 06:23

/src/brpc/src/json2pb/encode_decode.cpp
Line
Count
Source (jump to first uncovered line)
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
#include <string>
19
#include <sstream>
20
#include "encode_decode.h"
21
22
namespace json2pb {
23
24
0
inline int match_pattern(const std::string& str, int index) {
25
    //pattern: _Zxxx_  
26
0
    const char digit = '0';
27
0
    const int pattern_length = 6;
28
29
0
    int length = str.size();
30
0
    if (length <= index || length - index < pattern_length) {
31
0
        return -1;
32
0
    }
33
0
    if (str[index] != '_' ||
34
0
        str[index + 1] != 'Z' ||
35
0
        str[index + 5] != '_' || 
36
0
        !isdigit(str[index + 2]) ||
37
0
        !isdigit(str[index + 3]) ||
38
0
        !isdigit(str[index + 4])) { 
39
0
        return -1;
40
0
    }
41
0
    int sum = (str[index + 2] - digit) * 100 + (str[index + 3] - digit) * 10 + (str[index + 4] - digit);
42
0
    return sum < 256 ? sum : -1;
43
0
}
44
45
0
bool encode_name(const std::string& content, std::string& encoded_content) {
46
0
    int index = 0;
47
0
    size_t begin = 0;
48
0
    bool convert = false; 
49
0
    for (std::string::const_iterator it = content.begin(); it != content.end(); ++it, ++index) {
50
0
        if ((!isalnum(*it) && 
51
0
            (*it != '_')) ||
52
0
            (it == content.begin() &&
53
0
            isdigit(*it))) { 
54
0
            if (!convert) {
55
0
                encoded_content.clear();
56
0
                encoded_content.reserve(2*content.size());
57
0
                convert = true;
58
0
            }
59
0
            encoded_content.append(content, begin, index - begin);
60
0
            begin = index + 1;
61
            
62
0
            char pattern[6];
63
0
            pattern[0] = '_';
64
0
            pattern[1] = 'Z';
65
0
            int first = *it / 100;
66
0
            int second = (*it - first * 100) / 10;
67
0
            int third = *it - first * 100 - second * 10;
68
0
            pattern[2] = first + '0';
69
0
            pattern[3] = second + '0';
70
0
            pattern[4] = third + '0';
71
0
            pattern[5] = '_';
72
0
            encoded_content.append(pattern, sizeof(pattern));
73
0
        }
74
0
    }
75
0
    if (!convert) {
76
0
        return false;
77
0
    } else {
78
0
        encoded_content.append(content, begin, index - begin);
79
0
        return true;
80
0
    }
81
0
}
82
83
0
bool decode_name(const std::string& content, std::string& decoded_content) {
84
0
    const int pattern_length = 6;
85
0
    int begin = 0;
86
0
    int second = 0;
87
0
    bool convert = false; 
88
0
    for (std::string::const_iterator it = content.begin(); it < content.end(); ++it, ++second) {
89
0
        if (*it != '_') {
90
0
            continue;
91
0
        }
92
0
        int val = match_pattern(content, second);
93
0
        if (val != -1) { 
94
0
            if (!convert) {
95
0
                decoded_content.clear();
96
0
                decoded_content.reserve(content.size());
97
0
                convert = true;
98
0
            }
99
0
            decoded_content.append(content, begin, second - begin);
100
0
            decoded_content.push_back(static_cast<char>(val));
101
0
            second += pattern_length - 1;
102
0
            begin = second + 1;
103
0
            it += pattern_length - 1;
104
0
        }
105
0
    } 
106
0
    if (!convert) {
107
0
        return false;
108
0
    } else {
109
0
        decoded_content.append(content, begin, second - begin);
110
0
        return true; 
111
0
    }
112
0
} 
113
114
} // namespace json2pb