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_UTIL_PROTO_HELPER_H_
18 #define SANDBOXED_API_UTIL_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 "google/protobuf/message_lite.h"
28 #include "sandboxed_api/util/proto_arg.pb.h"
29 #include "sandboxed_api/util/status_macros.h"
30
31 namespace sapi {
32
33 namespace internal {
34
35 absl::Status DeserializeProto(const char* data, size_t len,
36 google::protobuf::MessageLite& output);
37
38 } // namespace internal
39
40 absl::StatusOr<std::vector<uint8_t>> SerializeProto(
41 const google::protobuf::MessageLite& proto);
42
43 template <typename T>
DeserializeProto(const char * data,size_t len)44 absl::StatusOr<T> DeserializeProto(const char* data, size_t len) {
45 static_assert(std::is_base_of<google::protobuf::MessageLite, T>::value,
46 "Template argument must be a proto message");
47 T result;
48 SAPI_RETURN_IF_ERROR(
49 internal::DeserializeProto(data, len, /*output=*/result));
50 return result;
51 }
52
53 } // namespace sapi
54
55 #endif // SANDBOXED_API_UTIL_PROTO_HELPER_H_
56