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 #include "absl/log/internal/proto.h"
16
17 #include <algorithm>
18 #include <cassert>
19 #include <cstddef>
20 #include <cstdint>
21 #include <cstring>
22
23 #include "absl/base/attributes.h"
24 #include "absl/base/config.h"
25 #include "absl/types/span.h"
26
27 namespace absl {
28 ABSL_NAMESPACE_BEGIN
29 namespace log_internal {
30 namespace {
EncodeRawVarint(uint64_t value,size_t size,absl::Span<char> * buf)31 void EncodeRawVarint(uint64_t value, size_t size, absl::Span<char> *buf) {
32 for (size_t s = 0; s < size; s++) {
33 (*buf)[s] = static_cast<char>((value & 0x7f) | (s + 1 == size ? 0 : 0x80));
34 value >>= 7;
35 }
36 buf->remove_prefix(size);
37 }
MakeTagType(uint64_t tag,WireType type)38 constexpr uint64_t MakeTagType(uint64_t tag, WireType type) {
39 return tag << 3 | static_cast<uint64_t>(type);
40 }
41 } // namespace
42
EncodeVarint(uint64_t tag,uint64_t value,absl::Span<char> * buf)43 bool EncodeVarint(uint64_t tag, uint64_t value, absl::Span<char> *buf) {
44 const uint64_t tag_type = MakeTagType(tag, WireType::kVarint);
45 const size_t tag_type_size = VarintSize(tag_type);
46 const size_t value_size = VarintSize(value);
47 if (tag_type_size + value_size > buf->size()) {
48 buf->remove_suffix(buf->size());
49 return false;
50 }
51 EncodeRawVarint(tag_type, tag_type_size, buf);
52 EncodeRawVarint(value, value_size, buf);
53 return true;
54 }
55
Encode64Bit(uint64_t tag,uint64_t value,absl::Span<char> * buf)56 bool Encode64Bit(uint64_t tag, uint64_t value, absl::Span<char> *buf) {
57 const uint64_t tag_type = MakeTagType(tag, WireType::k64Bit);
58 const size_t tag_type_size = VarintSize(tag_type);
59 if (tag_type_size + sizeof(value) > buf->size()) {
60 buf->remove_suffix(buf->size());
61 return false;
62 }
63 EncodeRawVarint(tag_type, tag_type_size, buf);
64 for (size_t s = 0; s < sizeof(value); s++) {
65 (*buf)[s] = static_cast<char>(value & 0xff);
66 value >>= 8;
67 }
68 buf->remove_prefix(sizeof(value));
69 return true;
70 }
71
Encode32Bit(uint64_t tag,uint32_t value,absl::Span<char> * buf)72 bool Encode32Bit(uint64_t tag, uint32_t value, absl::Span<char> *buf) {
73 const uint64_t tag_type = MakeTagType(tag, WireType::k32Bit);
74 const size_t tag_type_size = VarintSize(tag_type);
75 if (tag_type_size + sizeof(value) > buf->size()) {
76 buf->remove_suffix(buf->size());
77 return false;
78 }
79 EncodeRawVarint(tag_type, tag_type_size, buf);
80 for (size_t s = 0; s < sizeof(value); s++) {
81 (*buf)[s] = static_cast<char>(value & 0xff);
82 value >>= 8;
83 }
84 buf->remove_prefix(sizeof(value));
85 return true;
86 }
87
EncodeBytes(uint64_t tag,absl::Span<const char> value,absl::Span<char> * buf)88 bool EncodeBytes(uint64_t tag, absl::Span<const char> value,
89 absl::Span<char> *buf) {
90 const uint64_t tag_type = MakeTagType(tag, WireType::kLengthDelimited);
91 const size_t tag_type_size = VarintSize(tag_type);
92 uint64_t length = value.size();
93 const size_t length_size = VarintSize(length);
94 if (tag_type_size + length_size + value.size() > buf->size()) {
95 buf->remove_suffix(buf->size());
96 return false;
97 }
98 EncodeRawVarint(tag_type, tag_type_size, buf);
99 EncodeRawVarint(length, length_size, buf);
100 memcpy(buf->data(), value.data(), value.size());
101 buf->remove_prefix(value.size());
102 return true;
103 }
104
EncodeBytesTruncate(uint64_t tag,absl::Span<const char> value,absl::Span<char> * buf)105 bool EncodeBytesTruncate(uint64_t tag, absl::Span<const char> value,
106 absl::Span<char> *buf) {
107 const uint64_t tag_type = MakeTagType(tag, WireType::kLengthDelimited);
108 const size_t tag_type_size = VarintSize(tag_type);
109 uint64_t length = value.size();
110 const size_t length_size =
111 VarintSize(std::min<uint64_t>(length, buf->size()));
112 if (tag_type_size + length_size <= buf->size() &&
113 tag_type_size + length_size + value.size() > buf->size()) {
114 value.remove_suffix(tag_type_size + length_size + value.size() -
115 buf->size());
116 length = value.size();
117 }
118 if (tag_type_size + length_size + value.size() > buf->size()) {
119 buf->remove_suffix(buf->size());
120 return false;
121 }
122 EncodeRawVarint(tag_type, tag_type_size, buf);
123 EncodeRawVarint(length, length_size, buf);
124 memcpy(buf->data(), value.data(), value.size());
125 buf->remove_prefix(value.size());
126 return true;
127 }
128
EncodeMessageStart(uint64_t tag,uint64_t max_size,absl::Span<char> * buf)129 ABSL_MUST_USE_RESULT absl::Span<char> EncodeMessageStart(
130 uint64_t tag, uint64_t max_size, absl::Span<char> *buf) {
131 const uint64_t tag_type = MakeTagType(tag, WireType::kLengthDelimited);
132 const size_t tag_type_size = VarintSize(tag_type);
133 max_size = std::min<uint64_t>(max_size, buf->size());
134 const size_t length_size = VarintSize(max_size);
135 if (tag_type_size + length_size > buf->size()) {
136 buf->remove_suffix(buf->size());
137 return absl::Span<char>();
138 }
139 EncodeRawVarint(tag_type, tag_type_size, buf);
140 const absl::Span<char> ret = buf->subspan(0, length_size);
141 EncodeRawVarint(0, length_size, buf);
142 return ret;
143 }
144
EncodeMessageLength(absl::Span<char> msg,const absl::Span<char> * buf)145 void EncodeMessageLength(absl::Span<char> msg, const absl::Span<char> *buf) {
146 if (!msg.data()) return;
147 assert(buf->data() >= msg.data());
148 if (buf->data() < msg.data()) return;
149 EncodeRawVarint(
150 static_cast<uint64_t>(buf->data() - (msg.data() + msg.size())),
151 msg.size(), &msg);
152 }
153
154 namespace {
DecodeVarint(absl::Span<const char> * buf)155 uint64_t DecodeVarint(absl::Span<const char> *buf) {
156 uint64_t value = 0;
157 size_t s = 0;
158 while (s < buf->size()) {
159 value |= static_cast<uint64_t>(static_cast<unsigned char>((*buf)[s]) & 0x7f)
160 << 7 * s;
161 if (!((*buf)[s++] & 0x80)) break;
162 }
163 buf->remove_prefix(s);
164 return value;
165 }
166
Decode64Bit(absl::Span<const char> * buf)167 uint64_t Decode64Bit(absl::Span<const char> *buf) {
168 uint64_t value = 0;
169 size_t s = 0;
170 while (s < buf->size()) {
171 value |= static_cast<uint64_t>(static_cast<unsigned char>((*buf)[s]))
172 << 8 * s;
173 if (++s == sizeof(value)) break;
174 }
175 buf->remove_prefix(s);
176 return value;
177 }
178
Decode32Bit(absl::Span<const char> * buf)179 uint32_t Decode32Bit(absl::Span<const char> *buf) {
180 uint32_t value = 0;
181 size_t s = 0;
182 while (s < buf->size()) {
183 value |= static_cast<uint32_t>(static_cast<unsigned char>((*buf)[s]))
184 << 8 * s;
185 if (++s == sizeof(value)) break;
186 }
187 buf->remove_prefix(s);
188 return value;
189 }
190 } // namespace
191
DecodeFrom(absl::Span<const char> * data)192 bool ProtoField::DecodeFrom(absl::Span<const char> *data) {
193 if (data->empty()) return false;
194 const uint64_t tag_type = DecodeVarint(data);
195 tag_ = tag_type >> 3;
196 type_ = static_cast<WireType>(tag_type & 0x07);
197 switch (type_) {
198 case WireType::kVarint:
199 value_ = DecodeVarint(data);
200 break;
201 case WireType::k64Bit:
202 value_ = Decode64Bit(data);
203 break;
204 case WireType::kLengthDelimited: {
205 value_ = DecodeVarint(data);
206 data_ = data->subspan(
207 0, static_cast<size_t>(std::min<uint64_t>(value_, data->size())));
208 data->remove_prefix(data_.size());
209 break;
210 }
211 case WireType::k32Bit:
212 value_ = Decode32Bit(data);
213 break;
214 }
215 return true;
216 }
217
218 } // namespace log_internal
219 ABSL_NAMESPACE_END
220 } // namespace absl
221