xref: /aosp_15_r20/external/perfetto/src/trace_redaction/proto_util.cc (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 /*
2  * Copyright (C) 2024 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 "src/trace_redaction/proto_util.h"
18 
19 #include "perfetto/protozero/field.h"
20 #include "perfetto/protozero/message.h"
21 
22 namespace perfetto::trace_redaction {
23 namespace proto_util {
24 
25 // This is copied from "src/protozero/field.cc", but was modified to use the
26 // serialization methods provided in "perfetto/protozero/message.h".
AppendField(const protozero::Field & field,protozero::Message * message)27 void AppendField(const protozero::Field& field, protozero::Message* message) {
28   auto id = field.id();
29   auto type = field.type();
30 
31   switch (type) {
32     case protozero::proto_utils::ProtoWireType::kVarInt: {
33       message->AppendVarInt(id, field.raw_int_value());
34       return;
35     }
36 
37     case protozero::proto_utils::ProtoWireType::kFixed32: {
38       message->AppendFixed(id, field.as_uint32());
39       return;
40     }
41 
42     case protozero::proto_utils::ProtoWireType::kFixed64: {
43       message->AppendFixed(id, field.as_uint64());
44       return;
45     }
46 
47     case protozero::proto_utils::ProtoWireType::kLengthDelimited: {
48       message->AppendBytes(id, field.data(), field.size());
49       return;
50     }
51   }
52 
53   // A switch-statement would be preferred, but when using a switch statement,
54   // it complains that about case coverage.
55   PERFETTO_FATAL("Unknown field type %u", static_cast<uint8_t>(type));
56 }
57 
58 }  // namespace proto_util
59 }  // namespace perfetto::trace_redaction
60