1 /*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifndef PROTO_READER_HELP_H
17 #define PROTO_READER_HELP_H
18
19 #include <map>
20 #include <string>
21 #include <vector>
22
23 namespace SysTuning {
24 namespace ProtoReader {
25 constexpr int DATA_AREA_ID = 3;
26 class BytesView {
27 public:
BytesView()28 BytesView() : data_(nullptr), size_(0) {}
BytesView(const uint8_t *data, size_t size)29 BytesView(const uint8_t *data, size_t size) : data_(data), size_(size) {}
BytesView(const BytesView &bytesView)30 BytesView(const BytesView &bytesView)
31 {
32 data_ = bytesView.data_;
33 size_ = bytesView.size_;
34 }
operator =(const BytesView &bytesView)35 BytesView &operator=(const BytesView &bytesView)
36 {
37 data_ = bytesView.data_;
38 size_ = bytesView.size_;
39 return *this;
40 }
ToStdString() const41 std::string ToStdString() const
42 {
43 return std::string(reinterpret_cast<const char *>(data_), size_);
44 }
Size() const45 size_t Size() const
46 {
47 return size_;
48 }
Data() const49 const uint8_t *Data() const
50 {
51 return data_;
52 }
53 const uint8_t *data_;
54 size_t size_;
55 };
56
57 struct CharsView {
ToStdStringSysTuning::ProtoReader::CharsView58 std::string ToStdString() const
59 {
60 return std::string(data, size);
61 }
62
63 const char *data;
64 size_t size;
65 };
66
67 enum class ProtoWireType : uint8_t {
68 kVarInt = 0,
69 kFixed64 = 1,
70 kLengthDelimited = 2,
71 kFixed32 = 5,
72 };
73
CreateTagVarInt(uint32_t DataAreaId)74 inline uint32_t CreateTagVarInt(uint32_t DataAreaId)
75 {
76 return (DataAreaId << DATA_AREA_ID) | static_cast<uint32_t>(ProtoWireType::kVarInt);
77 }
78
Lowercase(char c)79 inline char Lowercase(char c)
80 {
81 return ('A' <= c && c <= 'Z') ? static_cast<char>(c - ('A' - 'a')) : c;
82 }
83
Uppercase(char c)84 inline char Uppercase(char c)
85 {
86 return ('a' <= c && c <= 'z') ? static_cast<char>(c + ('A' - 'a')) : c;
87 }
88
ToUppercase(const std::string &str)89 inline std::string ToUppercase(const std::string &str)
90 {
91 std::string string(str);
92 auto end = string.end();
93 for (auto c = string.begin(); c != end; ++c)
94 *c = Uppercase(*c);
95 return string;
96 }
97
ToLowercase(const std::string &str)98 inline std::string ToLowercase(const std::string &str)
99 {
100 std::string string(str);
101 auto end = string.end();
102 for (auto c = string.begin(); c != end; ++c)
103 *c = Lowercase(*c);
104 return string;
105 }
106
107 constexpr size_t kMessageLengthFieldSize = 4;
108 constexpr size_t kMaxMessageLength = (1u << (kMessageLengthFieldSize * 7)) - 1;
109
110 constexpr size_t kMaxTagEncodedSize = 5;
111 constexpr size_t kMaxSimpleFieldEncodedSize = kMaxTagEncodedSize + 10;
112
113 template <typename T>
ZigZagDecode(T value)114 inline typename std::make_signed<T>::type ZigZagDecode(T value)
115 {
116 using UnsignedType = typename std::make_unsigned<T>::type;
117 using SignedType = typename std::make_signed<T>::type;
118 auto unsignedValue = static_cast<UnsignedType>(value);
119 auto unsignedmask = static_cast<UnsignedType>(-static_cast<SignedType>(unsignedValue & 1));
120 return static_cast<SignedType>((unsignedValue >> 1) ^ unsignedmask);
121 }
122
123 constexpr uint8_t varIntValueBits = 7;
124 constexpr uint8_t varIntValueMask = 0x7f; // 111 1111b
125 constexpr uint8_t varIntValueDecodeMaxOffset = 64;
126 constexpr uint8_t byteHighestBitMark = 0x80; // 1000 0000b
127 const uint8_t *VarIntDecode(const uint8_t *start, const uint8_t *end, uint64_t *varIntValue);
128 } // namespace ProtoReader
129 } // namespace SysTuning
130 #endif // PROTO_READER_HELP_H
131