/src/abseil-cpp/absl/log/internal/proto.cc
Line | Count | Source |
1 | | // Copyright 2020 The Abseil Authors. |
2 | | // |
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 | | // https://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 | | // All of the data that passes through this code is trusted because it flows |
16 | | // through a closed loop within the absl::LogMessage object. It is not a robust |
17 | | // protocol buffer encoder or decoder. |
18 | | // |
19 | | // Encoding: When `LOG(INFO) << "foo"` is called, the library uses the Encode* |
20 | | // functions to build a protocol buffer in a private, fixed-size internal buffer |
21 | | // (`LogMessageData::encoded_buf`). |
22 | | // |
23 | | // Decoding: During the same logging call, `LogMessage::Flush()` calls |
24 | | // `FinalizeEncodingAndFormat()`, which uses `ProtoField::DecodeFrom` to parse |
25 | | // that same internal buffer to generate the human-readable string for |
26 | | // text-based log sinks. |
27 | | |
28 | | #include "absl/log/internal/proto.h" |
29 | | |
30 | | #include <algorithm> |
31 | | #include <cassert> |
32 | | #include <cstddef> |
33 | | #include <cstdint> |
34 | | #include <cstring> |
35 | | |
36 | | #include "absl/base/attributes.h" |
37 | | #include "absl/base/config.h" |
38 | | #include "absl/types/span.h" |
39 | | |
40 | | namespace absl { |
41 | | ABSL_NAMESPACE_BEGIN |
42 | | namespace log_internal { |
43 | | namespace { |
44 | 0 | void EncodeRawVarint(uint64_t value, size_t size, absl::Span<char> *buf) { |
45 | 0 | for (size_t s = 0; s < size; s++) { |
46 | 0 | (*buf)[s] = static_cast<char>((value & 0x7f) | (s + 1 == size ? 0 : 0x80)); |
47 | 0 | value >>= 7; |
48 | 0 | } |
49 | 0 | buf->remove_prefix(size); |
50 | 0 | } |
51 | | } // namespace |
52 | | |
53 | 0 | bool EncodeVarint(uint64_t tag, uint64_t value, absl::Span<char> *buf) { |
54 | 0 | const uint64_t tag_type = MakeTagType(tag, WireType::kVarint); |
55 | 0 | const size_t tag_type_size = VarintSize(tag_type); |
56 | 0 | const size_t value_size = VarintSize(value); |
57 | 0 | if (tag_type_size + value_size > buf->size()) { |
58 | 0 | buf->remove_suffix(buf->size()); |
59 | 0 | return false; |
60 | 0 | } |
61 | 0 | EncodeRawVarint(tag_type, tag_type_size, buf); |
62 | 0 | EncodeRawVarint(value, value_size, buf); |
63 | 0 | return true; |
64 | 0 | } |
65 | | |
66 | 0 | bool Encode64Bit(uint64_t tag, uint64_t value, absl::Span<char> *buf) { |
67 | 0 | const uint64_t tag_type = MakeTagType(tag, WireType::k64Bit); |
68 | 0 | const size_t tag_type_size = VarintSize(tag_type); |
69 | 0 | if (tag_type_size + sizeof(value) > buf->size()) { |
70 | 0 | buf->remove_suffix(buf->size()); |
71 | 0 | return false; |
72 | 0 | } |
73 | 0 | EncodeRawVarint(tag_type, tag_type_size, buf); |
74 | 0 | for (size_t s = 0; s < sizeof(value); s++) { |
75 | 0 | (*buf)[s] = static_cast<char>(value & 0xff); |
76 | 0 | value >>= 8; |
77 | 0 | } |
78 | 0 | buf->remove_prefix(sizeof(value)); |
79 | 0 | return true; |
80 | 0 | } |
81 | | |
82 | 0 | bool Encode32Bit(uint64_t tag, uint32_t value, absl::Span<char> *buf) { |
83 | 0 | const uint64_t tag_type = MakeTagType(tag, WireType::k32Bit); |
84 | 0 | const size_t tag_type_size = VarintSize(tag_type); |
85 | 0 | if (tag_type_size + sizeof(value) > buf->size()) { |
86 | 0 | buf->remove_suffix(buf->size()); |
87 | 0 | return false; |
88 | 0 | } |
89 | 0 | EncodeRawVarint(tag_type, tag_type_size, buf); |
90 | 0 | for (size_t s = 0; s < sizeof(value); s++) { |
91 | 0 | (*buf)[s] = static_cast<char>(value & 0xff); |
92 | 0 | value >>= 8; |
93 | 0 | } |
94 | 0 | buf->remove_prefix(sizeof(value)); |
95 | 0 | return true; |
96 | 0 | } |
97 | | |
98 | | bool EncodeBytes(uint64_t tag, absl::Span<const char> value, |
99 | 0 | absl::Span<char> *buf) { |
100 | 0 | const uint64_t tag_type = MakeTagType(tag, WireType::kLengthDelimited); |
101 | 0 | const size_t tag_type_size = VarintSize(tag_type); |
102 | 0 | uint64_t length = value.size(); |
103 | 0 | const size_t length_size = VarintSize(length); |
104 | 0 | if (tag_type_size + length_size + value.size() > buf->size()) { |
105 | 0 | buf->remove_suffix(buf->size()); |
106 | 0 | return false; |
107 | 0 | } |
108 | 0 | EncodeRawVarint(tag_type, tag_type_size, buf); |
109 | 0 | EncodeRawVarint(length, length_size, buf); |
110 | 0 | memcpy(buf->data(), value.data(), value.size()); |
111 | 0 | buf->remove_prefix(value.size()); |
112 | 0 | return true; |
113 | 0 | } |
114 | | |
115 | | bool EncodeBytesTruncate(uint64_t tag, absl::Span<const char> value, |
116 | 0 | absl::Span<char> *buf) { |
117 | 0 | const uint64_t tag_type = MakeTagType(tag, WireType::kLengthDelimited); |
118 | 0 | const size_t tag_type_size = VarintSize(tag_type); |
119 | 0 | uint64_t length = value.size(); |
120 | 0 | const size_t length_size = |
121 | 0 | VarintSize(std::min<uint64_t>(length, buf->size())); |
122 | 0 | if (tag_type_size + length_size <= buf->size() && |
123 | 0 | tag_type_size + length_size + value.size() > buf->size()) { |
124 | 0 | value.remove_suffix(tag_type_size + length_size + value.size() - |
125 | 0 | buf->size()); |
126 | 0 | length = value.size(); |
127 | 0 | } |
128 | 0 | if (tag_type_size + length_size + value.size() > buf->size()) { |
129 | 0 | buf->remove_suffix(buf->size()); |
130 | 0 | return false; |
131 | 0 | } |
132 | 0 | EncodeRawVarint(tag_type, tag_type_size, buf); |
133 | 0 | EncodeRawVarint(length, length_size, buf); |
134 | 0 | memcpy(buf->data(), value.data(), value.size()); |
135 | 0 | buf->remove_prefix(value.size()); |
136 | 0 | return true; |
137 | 0 | } |
138 | | |
139 | | [[nodiscard]] absl::Span<char> EncodeMessageStart(uint64_t tag, |
140 | | uint64_t max_size, |
141 | 0 | absl::Span<char> *buf) { |
142 | 0 | const uint64_t tag_type = MakeTagType(tag, WireType::kLengthDelimited); |
143 | 0 | const size_t tag_type_size = VarintSize(tag_type); |
144 | 0 | max_size = std::min<uint64_t>(max_size, buf->size()); |
145 | 0 | const size_t length_size = VarintSize(max_size); |
146 | 0 | if (tag_type_size + length_size > buf->size()) { |
147 | 0 | buf->remove_suffix(buf->size()); |
148 | 0 | return absl::Span<char>(); |
149 | 0 | } |
150 | 0 | EncodeRawVarint(tag_type, tag_type_size, buf); |
151 | 0 | const absl::Span<char> ret = buf->subspan(0, length_size); |
152 | 0 | EncodeRawVarint(0, length_size, buf); |
153 | 0 | return ret; |
154 | 0 | } |
155 | | |
156 | 0 | void EncodeMessageLength(absl::Span<char> msg, const absl::Span<char> *buf) { |
157 | 0 | if (!msg.data()) return; |
158 | 0 | assert(buf->data() >= msg.data()); |
159 | 0 | if (buf->data() < msg.data()) return; |
160 | 0 | EncodeRawVarint( |
161 | 0 | static_cast<uint64_t>(buf->data() - (msg.data() + msg.size())), |
162 | 0 | msg.size(), &msg); |
163 | 0 | } |
164 | | |
165 | | namespace { |
166 | 0 | uint64_t DecodeVarint(absl::Span<const char> *buf) { |
167 | 0 | uint64_t value = 0; |
168 | 0 | size_t s = 0; |
169 | 0 | while (s < buf->size()) { |
170 | 0 | value |= static_cast<uint64_t>(static_cast<unsigned char>((*buf)[s]) & 0x7f) |
171 | 0 | << 7 * s; |
172 | 0 | if (!((*buf)[s++] & 0x80)) break; |
173 | 0 | } |
174 | 0 | buf->remove_prefix(s); |
175 | 0 | return value; |
176 | 0 | } |
177 | | |
178 | 0 | uint64_t Decode64Bit(absl::Span<const char> *buf) { |
179 | 0 | uint64_t value = 0; |
180 | 0 | size_t s = 0; |
181 | 0 | while (s < buf->size()) { |
182 | 0 | value |= static_cast<uint64_t>(static_cast<unsigned char>((*buf)[s])) |
183 | 0 | << 8 * s; |
184 | 0 | if (++s == sizeof(value)) break; |
185 | 0 | } |
186 | 0 | buf->remove_prefix(s); |
187 | 0 | return value; |
188 | 0 | } |
189 | | |
190 | 0 | uint32_t Decode32Bit(absl::Span<const char> *buf) { |
191 | 0 | uint32_t value = 0; |
192 | 0 | size_t s = 0; |
193 | 0 | while (s < buf->size()) { |
194 | 0 | value |= static_cast<uint32_t>(static_cast<unsigned char>((*buf)[s])) |
195 | 0 | << 8 * s; |
196 | 0 | if (++s == sizeof(value)) break; |
197 | 0 | } |
198 | 0 | buf->remove_prefix(s); |
199 | 0 | return value; |
200 | 0 | } |
201 | | } // namespace |
202 | | |
203 | 0 | bool ProtoField::DecodeFrom(absl::Span<const char> *data) { |
204 | 0 | if (data->empty()) return false; |
205 | 0 | const uint64_t tag_type = DecodeVarint(data); |
206 | 0 | tag_ = tag_type >> 3; |
207 | 0 | type_ = static_cast<WireType>(tag_type & 0x07); |
208 | 0 | switch (type_) { |
209 | 0 | case WireType::kVarint: |
210 | 0 | value_ = DecodeVarint(data); |
211 | 0 | break; |
212 | 0 | case WireType::k64Bit: |
213 | 0 | value_ = Decode64Bit(data); |
214 | 0 | break; |
215 | 0 | case WireType::kLengthDelimited: { |
216 | 0 | value_ = DecodeVarint(data); |
217 | 0 | data_ = data->subspan( |
218 | 0 | 0, static_cast<size_t>(std::min<uint64_t>(value_, data->size()))); |
219 | 0 | data->remove_prefix(data_.size()); |
220 | 0 | break; |
221 | 0 | } |
222 | 0 | case WireType::k32Bit: |
223 | 0 | value_ = Decode32Bit(data); |
224 | 0 | break; |
225 | 0 | } |
226 | 0 | return true; |
227 | 0 | } |
228 | | |
229 | | } // namespace log_internal |
230 | | ABSL_NAMESPACE_END |
231 | | } // namespace absl |