1 // Copyright 2019 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 // Utility functions for protobuf handling.
16
17 #ifndef SANDBOXED_API_PROTO_HELPER_H_
18 #define SANDBOXED_API_PROTO_HELPER_H_
19
20 #include <cstddef>
21 #include <cstdint>
22 #include <type_traits>
23 #include <vector>
24
25 #include "absl/status/status.h"
26 #include "absl/status/statusor.h"
27 #include "sandboxed_api/proto_arg.pb.h"
28 #include "sandboxed_api/util/status_macros.h"
29
30 namespace sapi {
31
32 namespace internal {
33
34 absl::Status DeserializeProto(const char* data, size_t len,
35 google::protobuf::MessageLite& output);
36
37 } // namespace internal
38
39 absl::StatusOr<std::vector<uint8_t>> SerializeProto(
40 const google::protobuf::MessageLite& proto);
41
42 template <typename T>
DeserializeProto(const char * data,size_t len)43 absl::StatusOr<T> DeserializeProto(const char* data, size_t len) {
44 static_assert(std::is_base_of<google::protobuf::MessageLite, T>::value,
45 "Template argument must be a proto message");
46 T result;
47 SAPI_RETURN_IF_ERROR(
48 internal::DeserializeProto(data, len, /*output=*/result));
49 return result;
50 }
51
52 } // namespace sapi
53
54 #endif // SANDBOXED_API_PROTO_HELPER_H_
55