proto.cc 6.9 KB

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