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 namespace tracing { | |
12 namespace v2 { | |
13 namespace proto { | |
14 | |
15 const uint8_t* ParseVarInt(const uint8_t* start, | |
16 const uint8_t* end, | |
17 uint64_t* value) { | |
18 const uint8_t* pos = start; | |
19 uint64_t shift = 0; | |
20 *value = 0; | |
21 do { | |
22 CHECK_LT(pos, end); | |
23 DCHECK_LT(shift, 64ull); | |
24 *value |= static_cast<uint64_t>(*pos & 0x7f) << shift; | |
25 shift += 7; | |
26 } while (*pos++ & 0x80); | |
kraynov
2016/09/02 16:06:49
Termination condition looks too tricky
Primiano Tucci (use gerrit)
2016/09/02 16:54:59
uh, but the only other option I see is doing:
whi
| |
27 return pos; | |
28 } | |
29 | |
30 const uint8_t* ParseField(const uint8_t* start, | |
31 const uint8_t* end, | |
32 uint32_t* field_id, | |
33 FieldType* field_type, | |
34 uint64_t* field_intvalue) { | |
35 const uint8_t* pos = start; | |
36 CHECK_LT(pos, end); | |
37 *field_type = static_cast<FieldType>(*pos & 7); | |
38 | |
39 uint64_t raw_field_id; | |
40 pos = ParseVarInt(pos, end, &raw_field_id); | |
41 raw_field_id >>= 3; | |
42 | |
43 DCHECK_LE(raw_field_id, std::numeric_limits<uint32_t>::max()); | |
44 *field_id = static_cast<uint32_t>(raw_field_id); | |
45 | |
46 switch (*field_type) { | |
47 case kFieldTypeFixed64: { | |
48 CHECK_LT(pos + 8, end); | |
49 memcpy(field_intvalue, pos, 8); | |
50 *field_intvalue = base::ByteSwapToLE64(*field_intvalue); | |
51 pos += 8; | |
52 break; | |
53 } | |
54 case kFieldTypeFixed32: { | |
55 CHECK_LT(pos + 4, end); | |
56 uint32_t tmp; | |
57 memcpy(&tmp, pos, 4); | |
58 *field_intvalue = base::ByteSwapToLE32(tmp); | |
59 pos += 8; | |
60 break; | |
61 } | |
62 case kFieldTypeVarInt: | |
63 case kFieldTypeLengthDelimited: { | |
64 pos = ParseVarInt(pos, end, field_intvalue); | |
65 if (*field_type == kFieldTypeLengthDelimited) | |
66 CHECK_LE(pos + *field_intvalue, end); | |
67 break; | |
68 } | |
69 default: | |
70 NOTREACHED() << "Unsupported proto field type " << *field_type; | |
71 } | |
72 return pos; | |
73 } | |
74 | |
75 } // namespace proto | |
76 } // namespace v2 | |
77 } // namespace tracing | |
OLD | NEW |