1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef PPAPI_PROXY_CONTENT_DECRYPTOR_PRIVATE_SERIALIZER_H_
6 #define PPAPI_PROXY_CONTENT_DECRYPTOR_PRIVATE_SERIALIZER_H_
7
8 #include <cstring>
9 #include <string>
10
11 #include "ppapi/c/private/pp_content_decryptor.h"
12
13 namespace ppapi {
14 namespace proxy {
15
16 // Serialization/deserialization utility functions for storing/extracting
17 // PP_DecryptedBlockInfo, PP_EncryptedBlockInfo, and PP_DecompressedFrameInfo
18 // structs within std::string's for passing through IPC. Both functions return
19 // true upon success, and false upon failure.
20 //
21 // Note, these functions check the size of |block_info| against the size of
22 // the "serialized" data stored within |serialized_block_info|, and will report
23 // failure if expectations are not met. Use of CHECK/DCHECK has been avoided
24 // because the functions are intended for use on both sides of the IPC proxy.
25
26 template <typename T>
SerializeBlockInfo(const T & block_info,std::string * serialized_block_info)27 bool SerializeBlockInfo(const T& block_info,
28 std::string* serialized_block_info) {
29 if (!serialized_block_info)
30 return false;
31
32 serialized_block_info->assign(reinterpret_cast<const char*>(&block_info),
33 sizeof(block_info));
34
35 if (serialized_block_info->size() != sizeof(block_info))
36 return false;
37
38 return true;
39 }
40
41 template <typename T>
DeserializeBlockInfo(const std::string & serialized_block_info,T * block_info)42 bool DeserializeBlockInfo(const std::string& serialized_block_info,
43 T* block_info) {
44 if (!block_info)
45 return false;
46
47 if (serialized_block_info.size() != sizeof(*block_info))
48 return false;
49
50 std::memcpy(block_info, serialized_block_info.data(), sizeof(*block_info));
51 return true;
52 }
53
54 } // namespace proxy
55 } // namespace ppapi
56
57 #endif // PPAPI_PROXY_CONTENT_DECRYPTOR_PRIVATE_SERIALIZER_H_
58