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 #include "components/tracing/core/proto_utils.h" |
| 6 |
| 7 #include <limits> |
| 8 |
| 9 #include "base/sys_byteorder.h" |
| 10 |
| 11 #define CHECK_PTR_LE(a, b) \ |
| 12 CHECK_LE(reinterpret_cast<const void*>(a), reinterpret_cast<const void*>(b)) |
| 13 namespace tracing { |
| 14 namespace v2 { |
| 15 namespace proto { |
| 16 |
| 17 const uint8_t* ParseVarInt(const uint8_t* start, |
| 18 const uint8_t* end, |
| 19 uint64_t* value) { |
| 20 const uint8_t* pos = start; |
| 21 uint64_t shift = 0; |
| 22 *value = 0; |
| 23 do { |
| 24 CHECK_PTR_LE(pos, end - 1); |
| 25 DCHECK_LT(shift, 64ull); |
| 26 *value |= static_cast<uint64_t>(*pos & 0x7f) << shift; |
| 27 shift += 7; |
| 28 } while (*pos++ & 0x80); |
| 29 return pos; |
| 30 } |
| 31 |
| 32 const uint8_t* ParseField(const uint8_t* start, |
| 33 const uint8_t* end, |
| 34 uint32_t* field_id, |
| 35 FieldType* field_type, |
| 36 uint64_t* field_intvalue) { |
| 37 // The first byte of a proto field is structured as follows: |
| 38 // The least 3 significant bits determine the field type. |
| 39 // The most 5 significant bits determine the field id. If MSB == 1, the |
| 40 // field id continues on the next bytes following the VarInt encoding. |
| 41 const uint8_t kFieldTypeNumBits = 3; |
| 42 const uint8_t kFieldTypeMask = (1 << kFieldTypeNumBits) - 1; // 0000 0111; |
| 43 |
| 44 const uint8_t* pos = start; |
| 45 CHECK_PTR_LE(pos, end - 1); |
| 46 *field_type = static_cast<FieldType>(*pos & kFieldTypeMask); |
| 47 |
| 48 uint64_t raw_field_id; |
| 49 pos = ParseVarInt(pos, end, &raw_field_id); |
| 50 raw_field_id >>= kFieldTypeNumBits; |
| 51 |
| 52 DCHECK_LE(raw_field_id, std::numeric_limits<uint32_t>::max()); |
| 53 *field_id = static_cast<uint32_t>(raw_field_id); |
| 54 |
| 55 switch (*field_type) { |
| 56 case kFieldTypeFixed64: { |
| 57 CHECK_PTR_LE(pos + sizeof(uint64_t), end); |
| 58 memcpy(field_intvalue, pos, sizeof(uint64_t)); |
| 59 *field_intvalue = base::ByteSwapToLE64(*field_intvalue); |
| 60 pos += sizeof(uint64_t); |
| 61 break; |
| 62 } |
| 63 case kFieldTypeFixed32: { |
| 64 CHECK_PTR_LE(pos + sizeof(uint32_t), end); |
| 65 uint32_t tmp; |
| 66 memcpy(&tmp, pos, sizeof(uint32_t)); |
| 67 *field_intvalue = base::ByteSwapToLE32(tmp); |
| 68 pos += sizeof(uint32_t); |
| 69 break; |
| 70 } |
| 71 case kFieldTypeVarInt: { |
| 72 pos = ParseVarInt(pos, end, field_intvalue); |
| 73 break; |
| 74 } |
| 75 case kFieldTypeLengthDelimited: { |
| 76 pos = ParseVarInt(pos, end, field_intvalue); |
| 77 pos += *field_intvalue; |
| 78 CHECK_PTR_LE(pos, end); |
| 79 break; |
| 80 } |
| 81 default: |
| 82 NOTREACHED() << "Unsupported proto field type " << *field_type; |
| 83 } |
| 84 return pos; |
| 85 } |
| 86 |
| 87 } // namespace proto |
| 88 } // namespace v2 |
| 89 } // namespace tracing |
OLD | NEW |