| 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 BASE_TRACE_EVENT_V2_PROTO_UTILS_H_ |
| 6 #define BASE_TRACE_EVENT_V2_PROTO_UTILS_H_ |
| 7 |
| 8 #include <inttypes.h> |
| 9 |
| 10 #include "base/base_export.h" |
| 11 #include "base/macros.h" |
| 12 |
| 13 namespace base { |
| 14 namespace trace_event { |
| 15 namespace v2 { |
| 16 |
| 17 class BASE_EXPORT ProtoUtils { |
| 18 public: |
| 19 static const uint32_t kFieldTypeVarInt = 0; |
| 20 static const uint32_t kFieldTypeFixed64 = 1; |
| 21 static const uint32_t kFieldTypeLengthLimited = 2; |
| 22 static const uint32_t kFieldTypeFixed32 = 5; |
| 23 |
| 24 static inline uint32_t GetVarIntFieldHeader(uint32_t field_id) { |
| 25 return (field_id << 3) | kFieldTypeVarInt; |
| 26 } |
| 27 |
| 28 static inline uint32_t GetFixed64FieldHeader(uint32_t field_id) { |
| 29 return (field_id << 3) | kFieldTypeFixed64; |
| 30 } |
| 31 |
| 32 static inline uint32_t GetLengthLimitedFieldHeader(uint32_t field_id) { |
| 33 return (field_id << 3) | kFieldTypeLengthLimited; |
| 34 } |
| 35 |
| 36 static inline uint32_t GetFixed32FieldHeader(uint32_t field_id) { |
| 37 return (field_id << 3) | kFieldTypeFixed32; |
| 38 } |
| 39 |
| 40 static inline size_t EncodeVarIntUnsigned(uint64_t value, uint8_t *buf) { |
| 41 uint8_t *cur = buf; |
| 42 value &= 0x7FFFFFFF; |
| 43 do { |
| 44 char w = (char)(value & 0x7F); |
| 45 value >>= 7; |
| 46 if (value) |
| 47 w |= 0x80; |
| 48 *cur = w; |
| 49 ++cur; |
| 50 } while (value); |
| 51 return cur - buf; |
| 52 } |
| 53 |
| 54 static inline void |
| 55 EncodeRedundantVarIntUnsigned(uint64_t value, size_t length, uint8_t *buf) { |
| 56 for (size_t i = 0; i < length; ++i) { |
| 57 const uint8_t msb = (i < length - 1) ? 0x80 : 0; |
| 58 buf[i] = static_cast<uint8_t>((value & 0x7F) | msb); |
| 59 value >>= 7; |
| 60 } |
| 61 } |
| 62 |
| 63 DISALLOW_COPY_AND_ASSIGN(ProtoUtils); |
| 64 }; |
| 65 |
| 66 } // namespace v2 |
| 67 } // namespace trace_event |
| 68 } // namespace base |
| 69 |
| 70 #endif // BASE_TRACE_EVENT_V2_PROTO_UTILS_H_ |
| OLD | NEW |