• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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_ABI_PB_DECODER_ABI_H_
18 #define INCLUDE_PERFETTO_PUBLIC_ABI_PB_DECODER_ABI_H_
19 
20 #include <stddef.h>
21 #include <stdint.h>
22 
23 #include "perfetto/public/abi/export.h"
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 // Stores the state required to decode a protobuf message (from a continuous
30 // memory buffer).
31 struct PerfettoPbDecoder {
32   // Pointer to the beginning of the next field that should be decoded.
33   const uint8_t* read_ptr;
34   // Pointer to one past the end of the buffer.
35   const uint8_t* end_ptr;
36 };
37 
38 enum PerfettoPbDecoderStatus {
39   // A field has been decoded correctly. There is more data into the buffer,
40   // starting from an updated `read_ptr`.
41   PERFETTO_PB_DECODER_OK = 0,
42   // The last field has been decoded correctly until the end. There is no more
43   // data into the buffer.
44   PERFETTO_PB_DECODER_DONE = 1,
45   // The data starting at `read_ptr` cannot be fully decoded as a protobuf
46   // field. `read_ptr` has not been updated.
47   PERFETTO_PB_DECODER_ERROR = 2,
48 };
49 
50 // The content of a length-delimited field (wire type 2)
51 struct PerfettoPbDecoderDelimitedField {
52   const uint8_t* start;
53   size_t len;
54 };
55 
56 // A field parsed by the decoder.
57 struct PerfettoPbDecoderField {
58   // PerfettoPbDecoderStatus
59   uint32_t status;
60   // PerfettoPbWireType
61   uint32_t wire_type;
62   // The protobuf field id.
63   uint32_t id;
64   // The value of this field.
65   union {
66     // For wire type 2.
67     struct PerfettoPbDecoderDelimitedField delimited;
68     // For wire type 0 and 1.
69     uint64_t integer64;
70     // For wire type 5.
71     uint32_t integer32;
72     // For wire type 1.
73     double double_val;
74     // For wire type 5.
75     float float_val;
76   } value;
77 };
78 
79 // Parses a field and returns it. Advances `*decoder->read_ptr` to point after
80 // the field.
81 PERFETTO_SDK_EXPORT struct PerfettoPbDecoderField PerfettoPbDecoderParseField(
82     struct PerfettoPbDecoder* decoder);
83 
84 // Advances `*decoder->read_ptr` to point after the current field.
85 // Returns a `PerfettoPbDecoderStatus`.
86 PERFETTO_SDK_EXPORT uint32_t
87 PerfettoPbDecoderSkipField(struct PerfettoPbDecoder* decoder);
88 
89 #ifdef __cplusplus
90 }
91 #endif
92 
93 #endif  // INCLUDE_PERFETTO_PUBLIC_ABI_PB_DECODER_ABI_H_
94