OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef COMPONENTS_TRACING_CORE_PROTO_UTILS_H_ | 5 #ifndef COMPONENTS_TRACING_CORE_PROTO_UTILS_H_ |
6 #define COMPONENTS_TRACING_CORE_PROTO_UTILS_H_ | 6 #define COMPONENTS_TRACING_CORE_PROTO_UTILS_H_ |
7 | 7 |
8 #include <inttypes.h> | 8 #include <inttypes.h> |
9 | 9 |
10 #include <type_traits> | 10 #include <type_traits> |
11 | 11 |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "components/tracing/tracing_export.h" |
13 | 14 |
14 namespace tracing { | 15 namespace tracing { |
15 namespace v2 { | 16 namespace v2 { |
16 namespace proto { | 17 namespace proto { |
17 | 18 |
18 // See https://developers.google.com/protocol-buffers/docs/encoding wire types. | 19 // See https://developers.google.com/protocol-buffers/docs/encoding wire types. |
19 | 20 |
20 enum : uint32_t { | 21 enum FieldType : uint32_t { |
21 kFieldTypeVarInt = 0, | 22 kFieldTypeVarInt = 0, |
22 kFieldTypeFixed64 = 1, | 23 kFieldTypeFixed64 = 1, |
23 kFieldTypeLengthDelimited = 2, | 24 kFieldTypeLengthDelimited = 2, |
24 kFieldTypeFixed32 = 5, | 25 kFieldTypeFixed32 = 5, |
25 }; | 26 }; |
26 | 27 |
27 // Maximum message size supported: 256 MiB (4 x 7-bit due to varint encoding). | 28 // Maximum message size supported: 256 MiB (4 x 7-bit due to varint encoding). |
28 constexpr size_t kMessageLengthFieldSize = 4; | 29 constexpr size_t kMessageLengthFieldSize = 4; |
29 constexpr size_t kMaxMessageLength = (1u << (kMessageLengthFieldSize * 7)) - 1; | 30 constexpr size_t kMaxMessageLength = (1u << (kMessageLengthFieldSize * 7)) - 1; |
30 | 31 |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 } | 88 } |
88 DCHECK_EQ(0u, value) << "Message is too long"; | 89 DCHECK_EQ(0u, value) << "Message is too long"; |
89 } | 90 } |
90 | 91 |
91 template <uint32_t field_id> | 92 template <uint32_t field_id> |
92 void StaticAssertSingleBytePreamble() { | 93 void StaticAssertSingleBytePreamble() { |
93 static_assert(field_id < 16, | 94 static_assert(field_id < 16, |
94 "Proto field id too big to fit in a single byte preamble"); | 95 "Proto field id too big to fit in a single byte preamble"); |
95 }; | 96 }; |
96 | 97 |
| 98 // Parses a VarInt from the encoded buffer [start, end). |end| is STL-style and |
| 99 // points one byte past the end of buffer. |
| 100 // The parsed int value is stored in the output arg |value|. Returns a pointer |
| 101 // to the next unconsumed byte (so start < retval <= end). |
| 102 TRACING_EXPORT const uint8_t* ParseVarInt(const uint8_t* start, |
| 103 const uint8_t* end, |
| 104 uint64_t* value); |
| 105 |
| 106 // Parses a protobuf field and computes its id, type and value. |
| 107 // Returns a pointer to the next unconsumed byte (|start| < retval <= end) that |
| 108 // is either the beginning of the next field or the end of the parent message. |
| 109 // In the case of a kFieldTypeLengthDelimited field, |field_intvalue| will store |
| 110 // the length of the payload (either a string or a nested message). In this |
| 111 // case, the start of the payload will be at (return value) - |field_intvalue|. |
| 112 TRACING_EXPORT const uint8_t* ParseField(const uint8_t* start, |
| 113 const uint8_t* end, |
| 114 uint32_t* field_id, |
| 115 FieldType* field_type, |
| 116 uint64_t* field_intvalue); |
| 117 |
97 } // namespace proto | 118 } // namespace proto |
98 } // namespace v2 | 119 } // namespace v2 |
99 } // namespace tracing | 120 } // namespace tracing |
100 | 121 |
101 #endif // COMPONENTS_TRACING_CORE_PROTO_UTILS_H_ | 122 #endif // COMPONENTS_TRACING_CORE_PROTO_UTILS_H_ |
OLD | NEW |