blob: 833dfddf399cf816030ac268e76537e09298c817 [file] [log] [blame]
Primiano Tucci97440f42017-10-24 13:27:18 +01001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "protozero/proto_utils.h"
18
19#include <string.h>
20
21#include <limits>
22
Oystein Eftevaagdd727e42017-12-05 08:49:55 -080023#include "perfetto_base/logging.h"
Primiano Tucci97440f42017-10-24 13:27:18 +010024
Primiano Tuccid7d1be02017-10-30 17:41:34 +000025#define PERFETTO_CHECK_PTR_LE(a, b) \
26 PERFETTO_CHECK(reinterpret_cast<uintptr_t>(a) <= \
27 reinterpret_cast<uintptr_t>(b))
Primiano Tucci97440f42017-10-24 13:27:18 +010028
29namespace protozero {
30namespace proto_utils {
31
32#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
33#define BYTE_SWAP_TO_LE32(x) (x)
34#define BYTE_SWAP_TO_LE64(x) (x)
35#else
36#error Unimplemented for big endian archs.
37#endif
38
39const uint8_t* ParseVarInt(const uint8_t* start,
40 const uint8_t* end,
41 uint64_t* value) {
42 const uint8_t* pos = start;
43 uint64_t shift = 0;
44 *value = 0;
45 do {
Primiano Tuccid7d1be02017-10-30 17:41:34 +000046 PERFETTO_CHECK_PTR_LE(pos, end - 1);
47 PERFETTO_DCHECK(shift < 64ull);
Primiano Tucci97440f42017-10-24 13:27:18 +010048 *value |= static_cast<uint64_t>(*pos & 0x7f) << shift;
49 shift += 7;
50 } while (*pos++ & 0x80);
51 return pos;
52}
53
54const uint8_t* ParseField(const uint8_t* start,
55 const uint8_t* end,
56 uint32_t* field_id,
57 FieldType* field_type,
58 uint64_t* field_intvalue) {
59 // The first byte of a proto field is structured as follows:
60 // The least 3 significant bits determine the field type.
61 // The most 5 significant bits determine the field id. If MSB == 1, the
62 // field id continues on the next bytes following the VarInt encoding.
63 const uint8_t kFieldTypeNumBits = 3;
64 const uint8_t kFieldTypeMask = (1 << kFieldTypeNumBits) - 1; // 0000 0111;
65
66 const uint8_t* pos = start;
Primiano Tuccid7d1be02017-10-30 17:41:34 +000067 PERFETTO_CHECK_PTR_LE(pos, end - 1);
Primiano Tucci97440f42017-10-24 13:27:18 +010068 *field_type = static_cast<FieldType>(*pos & kFieldTypeMask);
69
70 uint64_t raw_field_id;
71 pos = ParseVarInt(pos, end, &raw_field_id);
72 raw_field_id >>= kFieldTypeNumBits;
73
Primiano Tuccid7d1be02017-10-30 17:41:34 +000074 PERFETTO_DCHECK(raw_field_id <= std::numeric_limits<uint32_t>::max());
Primiano Tucci97440f42017-10-24 13:27:18 +010075 *field_id = static_cast<uint32_t>(raw_field_id);
76
77 switch (*field_type) {
78 case kFieldTypeFixed64: {
Primiano Tuccid7d1be02017-10-30 17:41:34 +000079 PERFETTO_CHECK_PTR_LE(pos + sizeof(uint64_t), end);
Primiano Tucci97440f42017-10-24 13:27:18 +010080 memcpy(field_intvalue, pos, sizeof(uint64_t));
81 *field_intvalue = BYTE_SWAP_TO_LE64(*field_intvalue);
82 pos += sizeof(uint64_t);
83 break;
84 }
85 case kFieldTypeFixed32: {
Primiano Tuccid7d1be02017-10-30 17:41:34 +000086 PERFETTO_CHECK_PTR_LE(pos + sizeof(uint32_t), end);
Primiano Tucci97440f42017-10-24 13:27:18 +010087 uint32_t tmp;
88 memcpy(&tmp, pos, sizeof(uint32_t));
89 *field_intvalue = BYTE_SWAP_TO_LE32(tmp);
90 pos += sizeof(uint32_t);
91 break;
92 }
93 case kFieldTypeVarInt: {
94 pos = ParseVarInt(pos, end, field_intvalue);
95 break;
96 }
97 case kFieldTypeLengthDelimited: {
98 pos = ParseVarInt(pos, end, field_intvalue);
99 pos += *field_intvalue;
Primiano Tuccid7d1be02017-10-30 17:41:34 +0000100 PERFETTO_CHECK_PTR_LE(pos, end);
Primiano Tucci97440f42017-10-24 13:27:18 +0100101 break;
102 }
103 }
104 return pos;
105}
106
107} // namespace proto_utils
108} // namespace protozero