| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef COMPONENTS_TRACING_CORE_PROTO_UTILS_H_ |
| 6 #define COMPONENTS_TRACING_CORE_PROTO_UTILS_H_ |
| 7 |
| 8 #include <inttypes.h> |
| 9 |
| 10 #include <type_traits> |
| 11 |
| 12 #include "base/logging.h" |
| 13 #include "base/macros.h" |
| 14 #include "components/tracing/tracing_export.h" |
| 15 |
| 16 namespace tracing { |
| 17 namespace v2 { |
| 18 namespace proto { |
| 19 |
| 20 // See https://developers.google.com/protocol-buffers/docs/encoding wire types. |
| 21 |
| 22 enum : uint32_t { |
| 23 kFieldTypeVarInt = 0, |
| 24 kFieldTypeFixed64 = 1, |
| 25 kFieldTypeLengthDelimited = 2, |
| 26 kFieldTypeFixed32 = 5, |
| 27 }; |
| 28 |
| 29 // Returns the number of bytes sufficient to encode the largest |
| 30 // |int_size_in_bits|-bits integer using a non-redundant varint encoding. |
| 31 template <typename T> |
| 32 constexpr size_t GetMaxVarIntEncodedSize() { |
| 33 return (sizeof(T) * 8 + 6) / 7; |
| 34 } |
| 35 |
| 36 // Variable-length field types: (s)int32, (s)int64, bool, enum. |
| 37 inline uint32_t MakeTagVarInt(uint32_t field_id) { |
| 38 return (field_id << 3) | kFieldTypeVarInt; |
| 39 } |
| 40 |
| 41 // Length-limited field types: string, bytes, embedded messages. |
| 42 inline uint32_t MakeTagLengthDelimited(uint32_t field_id) { |
| 43 return (field_id << 3) | kFieldTypeLengthDelimited; |
| 44 } |
| 45 |
| 46 // 32-bit fixed-length field types: fixed32, sfixed32, float. |
| 47 inline uint32_t MakeTagFixed32(uint32_t field_id) { |
| 48 return (field_id << 3) | kFieldTypeFixed32; |
| 49 } |
| 50 |
| 51 // 64-bit fixed-length field types: fixed64, sfixed64, double. |
| 52 inline uint32_t MakeTagFixed64(uint32_t field_id) { |
| 53 return (field_id << 3) | kFieldTypeFixed64; |
| 54 } |
| 55 |
| 56 template <typename T> |
| 57 inline uint8_t* WriteVarIntInternal(T value, uint8_t* target) { |
| 58 static_assert(std::is_unsigned<T>::value, "value must be unsigned"); |
| 59 while (value >= 0x80) { |
| 60 *target++ = static_cast<uint8_t>(value | 0x80); |
| 61 value >>= 7; |
| 62 } |
| 63 *target = static_cast<uint8_t>(value); |
| 64 return target + 1; |
| 65 } |
| 66 |
| 67 inline uint8_t* WriteVarIntU32(uint32_t value, uint8_t* target) { |
| 68 return WriteVarIntInternal<uint32_t>(value, target); |
| 69 } |
| 70 |
| 71 inline uint8_t* WriteVarIntU64(uint64_t value, uint8_t* target) { |
| 72 return WriteVarIntInternal<uint64_t>(value, target); |
| 73 } |
| 74 |
| 75 // TODO(kraynov): add support for signed integers and zig-zag encoding. |
| 76 |
| 77 // Writes a fixed-size redundant encoding of the given |value|. This is |
| 78 // used to backfill fixed-size reservations for the length field using a |
| 79 // non-canonical varint encoding (e.g. \x81\x80\x80\x00 instead of \x01). |
| 80 // See https://github.com/google/protobuf/issues/1530. |
| 81 // In particular, this is used for nested messages. The size of a nested message |
| 82 // is not known until all its field have been written. A fixed amount of bytes |
| 83 // is reserved to encode the size field and backfilled at the end. |
| 84 template <size_t LENGTH> |
| 85 void WriteRedundantVarIntU32(uint32_t value, uint8_t* buf) { |
| 86 for (size_t i = 0; i < LENGTH; ++i) { |
| 87 const uint8_t msb = (i < LENGTH - 1) ? 0x80 : 0; |
| 88 buf[i] = static_cast<uint8_t>((value & 0x7F) | msb); |
| 89 value >>= 7; |
| 90 } |
| 91 DCHECK_EQ(0u, value) << "Buffer too short to encode the given value"; |
| 92 } |
| 93 |
| 94 } // namespace proto |
| 95 } // namespace v2 |
| 96 } // namespace tracing |
| 97 |
| 98 #endif // COMPONENTS_TRACING_CORE_PROTO_UTILS_H_ |
| OLD | NEW |