• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <assert.h>
21 #include <stdint.h>
22 
23 #include "perfetto/public/compiler.h"
24 
25 // Type of fields that can be found in a protobuf serialized message.
26 enum PerfettoPbWireType {
27   PERFETTO_PB_WIRE_TYPE_VARINT = 0,
28   PERFETTO_PB_WIRE_TYPE_FIXED64 = 1,
29   PERFETTO_PB_WIRE_TYPE_DELIMITED = 2,
30   PERFETTO_PB_WIRE_TYPE_FIXED32 = 5,
31 };
32 
33 // Creates a field tag, which encodes the field type and the field id.
PerfettoPbMakeTag(int32_t field_id,enum PerfettoPbWireType wire_type)34 static inline uint32_t PerfettoPbMakeTag(int32_t field_id,
35                                          enum PerfettoPbWireType wire_type) {
36   return ((PERFETTO_STATIC_CAST(uint32_t, field_id)) << 3) |
37          PERFETTO_STATIC_CAST(uint32_t, wire_type);
38 }
39 
40 enum {
41   // Maximum bytes size of a 64-bit integer encoded as a VarInt.
42   PERFETTO_PB_VARINT_MAX_SIZE_64 = 10,
43   // Maximum bytes size of a 32-bit integer encoded as a VarInt.
44   PERFETTO_PB_VARINT_MAX_SIZE_32 = 5,
45 };
46 
47 // Encodes `value` as a VarInt into `*dst`.
48 //
49 // `dst` must point into a buffer big enough to represent `value`:
50 // PERFETTO_PB_VARINT_MAX_SIZE_* can help.
PerfettoPbWriteVarInt(uint64_t value,uint8_t * dst)51 static inline uint8_t* PerfettoPbWriteVarInt(uint64_t value, uint8_t* dst) {
52   uint8_t byte;
53   while (value >= 0x80) {
54     byte = (value & 0x7f) | 0x80;
55     *dst++ = byte;
56     value >>= 7;
57   }
58   byte = value & 0x7f;
59   *dst++ = byte;
60 
61   return dst;
62 }
63 
64 // Encodes `value` as a fixed32 (little endian) into `*dst`.
65 //
66 // `dst` must point into a buffer with at least 4 bytes of space.
PerfettoPbWriteFixed32(uint32_t value,uint8_t * buf)67 static inline uint8_t* PerfettoPbWriteFixed32(uint32_t value, uint8_t* buf) {
68   buf[0] = PERFETTO_STATIC_CAST(uint8_t, value);
69   buf[1] = PERFETTO_STATIC_CAST(uint8_t, value >> 8);
70   buf[2] = PERFETTO_STATIC_CAST(uint8_t, value >> 16);
71   buf[3] = PERFETTO_STATIC_CAST(uint8_t, value >> 24);
72   return buf + 4;
73 }
74 
75 // Encodes `value` as a fixed32 (little endian) into `*dst`.
76 //
77 // `dst` must point into a buffer with at least 8 bytes of space.
PerfettoPbWriteFixed64(uint64_t value,uint8_t * buf)78 static inline uint8_t* PerfettoPbWriteFixed64(uint64_t value, uint8_t* buf) {
79   buf[0] = PERFETTO_STATIC_CAST(uint8_t, value);
80   buf[1] = PERFETTO_STATIC_CAST(uint8_t, value >> 8);
81   buf[2] = PERFETTO_STATIC_CAST(uint8_t, value >> 16);
82   buf[3] = PERFETTO_STATIC_CAST(uint8_t, value >> 24);
83   buf[4] = PERFETTO_STATIC_CAST(uint8_t, value >> 32);
84   buf[5] = PERFETTO_STATIC_CAST(uint8_t, value >> 40);
85   buf[6] = PERFETTO_STATIC_CAST(uint8_t, value >> 48);
86   buf[7] = PERFETTO_STATIC_CAST(uint8_t, value >> 56);
87   return buf + 8;
88 }
89 
90 // Parses a VarInt from the encoded buffer [start, end). |end| is STL-style and
91 // points one byte past the end of buffer.
92 // The parsed int value is stored in the output arg |value|. Returns a pointer
93 // to the next unconsumed byte (so start < retval <= end) or |start| if the
94 // VarInt could not be fully parsed because there was not enough space in the
95 // buffer.
PerfettoPbParseVarInt(const uint8_t * start,const uint8_t * end,uint64_t * out_value)96 static inline const uint8_t* PerfettoPbParseVarInt(const uint8_t* start,
97                                                    const uint8_t* end,
98                                                    uint64_t* out_value) {
99   const uint8_t* pos = start;
100   uint64_t value = 0;
101   for (uint32_t shift = 0; pos < end && shift < 64u; shift += 7) {
102     // Cache *pos into |cur_byte| to prevent that the compiler dereferences the
103     // pointer twice (here and in the if() below) due to char* aliasing rules.
104     uint8_t cur_byte = *pos++;
105     value |= PERFETTO_STATIC_CAST(uint64_t, cur_byte & 0x7f) << shift;
106     if ((cur_byte & 0x80) == 0) {
107       // In valid cases we get here.
108       *out_value = value;
109       return pos;
110     }
111   }
112   *out_value = 0;
113   return start;
114 }
115 
PerfettoPbZigZagEncode32(int32_t value)116 static inline uint32_t PerfettoPbZigZagEncode32(int32_t value) {
117 #if defined(__cplusplus) || \
118     (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L)
119   // Right-shift of negative values is implementation specific.
120   // Assert the implementation does what we expect, which is that shifting an
121   // positive int32_t by 31 gives an all 0 bitmap, and a negative int32_t gives
122   // an all 1 bitmap.
123   static_assert(
124       PERFETTO_STATIC_CAST(uint32_t, INT32_C(-1) >> 31) == ~UINT32_C(0),
125       "implementation does not support assumed rightshift");
126   static_assert(PERFETTO_STATIC_CAST(uint32_t, INT32_C(1) >> 31) == UINT32_C(0),
127                 "implementation does not support assumed rightshift");
128 #endif
129 
130   return (PERFETTO_STATIC_CAST(uint32_t, value) << 1) ^
131          PERFETTO_STATIC_CAST(uint32_t, value >> 31);
132 }
133 
PerfettoPbZigZagEncode64(int64_t value)134 static inline uint64_t PerfettoPbZigZagEncode64(int64_t value) {
135 #if defined(__cplusplus) || \
136     (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L)
137   // Right-shift of negative values is implementation specific.
138   // Assert the implementation does what we expect, which is that shifting an
139   // positive int64_t by 63 gives an all 0 bitmap, and a negative int64_t gives
140   // an all 1 bitmap.
141   static_assert(
142       PERFETTO_STATIC_CAST(uint64_t, INT64_C(-1) >> 63) == ~UINT64_C(0),
143       "implementation does not support assumed rightshift");
144   static_assert(PERFETTO_STATIC_CAST(uint64_t, INT64_C(1) >> 63) == UINT64_C(0),
145                 "implementation does not support assumed rightshift");
146 #endif
147 
148   return (PERFETTO_STATIC_CAST(uint64_t, value) << 1) ^
149          PERFETTO_STATIC_CAST(uint64_t, value >> 63);
150 }
151 
PerfettoPbZigZagDecode32(uint32_t value)152 static inline int32_t PerfettoPbZigZagDecode32(uint32_t value) {
153   uint32_t mask =
154       PERFETTO_STATIC_CAST(uint32_t, -PERFETTO_STATIC_CAST(int32_t, value & 1));
155   return PERFETTO_STATIC_CAST(int32_t, ((value >> 1) ^ mask));
156 }
157 
PerfettoPbZigZagDecode64(uint64_t value)158 static inline int64_t PerfettoPbZigZagDecode64(uint64_t value) {
159   uint64_t mask =
160       PERFETTO_STATIC_CAST(uint64_t, -PERFETTO_STATIC_CAST(int64_t, value & 1));
161   return PERFETTO_STATIC_CAST(int64_t, ((value >> 1) ^ mask));
162 }
163 
164 #endif  // INCLUDE_PERFETTO_PUBLIC_PB_UTILS_H_
165