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