1 /*
2 * Copyright (C) 2022 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 #ifndef INCLUDE_PERFETTO_PUBLIC_PB_UTILS_H_
18 #define INCLUDE_PERFETTO_PUBLIC_PB_UTILS_H_
19
20 #include <stdint.h>
21
22 #include "perfetto/public/compiler.h"
23
24 // Type of fields that can be found in a protobuf serialized message.
25 enum PerfettoPbWireType {
26 PERFETTO_PB_WIRE_TYPE_VARINT = 0,
27 PERFETTO_PB_WIRE_TYPE_FIXED64 = 1,
28 PERFETTO_PB_WIRE_TYPE_DELIMITED = 2,
29 PERFETTO_PB_WIRE_TYPE_FIXED32 = 5,
30 };
31
32 // Creates a field tag, which encodes the field type and the field id.
PerfettoPbMakeTag(int32_t field_id,enum PerfettoPbWireType wire_type)33 static inline uint32_t PerfettoPbMakeTag(int32_t field_id,
34 enum PerfettoPbWireType wire_type) {
35 return ((PERFETTO_STATIC_CAST(uint32_t, field_id)) << 3) |
36 PERFETTO_STATIC_CAST(uint32_t, wire_type);
37 }
38
39 enum {
40 // Maximum bytes size of a 64-bit integer encoded as a VarInt.
41 PERFETTO_PB_VARINT_MAX_SIZE_64 = 10,
42 // Maximum bytes size of a 32-bit integer encoded as a VarInt.
43 PERFETTO_PB_VARINT_MAX_SIZE_32 = 5,
44 };
45
46 // Encodes `value` as a VarInt into `*dst`.
47 //
48 // `dst` must point into a buffer big enough to represent `value`:
49 // PERFETTO_PB_VARINT_MAX_SIZE_* can help.
PerfettoPbWriteVarInt(uint64_t value,uint8_t * dst)50 static inline uint8_t* PerfettoPbWriteVarInt(uint64_t value, uint8_t* dst) {
51 uint8_t byte;
52 while (value >= 0x80) {
53 byte = (value & 0x7f) | 0x80;
54 *dst++ = byte;
55 value >>= 7;
56 }
57 byte = value & 0x7f;
58 *dst++ = byte;
59
60 return dst;
61 }
62
63 // Parses a VarInt from the encoded buffer [start, end). |end| is STL-style and
64 // points one byte past the end of buffer.
65 // The parsed int value is stored in the output arg |value|. Returns a pointer
66 // to the next unconsumed byte (so start < retval <= end) or |start| if the
67 // VarInt could not be fully parsed because there was not enough space in the
68 // buffer.
PerfettoPbParseVarInt(const uint8_t * start,const uint8_t * end,uint64_t * out_value)69 static inline const uint8_t* PerfettoPbParseVarInt(const uint8_t* start,
70 const uint8_t* end,
71 uint64_t* out_value) {
72 const uint8_t* pos = start;
73 uint64_t value = 0;
74 for (uint32_t shift = 0; pos < end && shift < 64u; shift += 7) {
75 // Cache *pos into |cur_byte| to prevent that the compiler dereferences the
76 // pointer twice (here and in the if() below) due to char* aliasing rules.
77 uint8_t cur_byte = *pos++;
78 value |= PERFETTO_STATIC_CAST(uint64_t, cur_byte & 0x7f) << shift;
79 if ((cur_byte & 0x80) == 0) {
80 // In valid cases we get here.
81 *out_value = value;
82 return pos;
83 }
84 }
85 *out_value = 0;
86 return start;
87 }
88
89 #endif // INCLUDE_PERFETTO_PUBLIC_PB_UTILS_H_
90