1 //
2 // Copyright (C) 2020 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 #include "composite_serialization.h"
17 
18 using keymaster::Serializable;
19 
20 namespace cuttlefish {
21 
CompositeSerializable(const std::vector<Serializable * > & members)22 CompositeSerializable::CompositeSerializable(
23     const std::vector<Serializable*>& members) : members_(members) {
24 }
25 
SerializedSize() const26 size_t CompositeSerializable::SerializedSize() const {
27   size_t sum = 0;
28   for (const auto& member : members_) {
29     sum += member->SerializedSize();
30   }
31   return sum;
32 }
33 
Serialize(uint8_t * buf,const uint8_t * end) const34 uint8_t* CompositeSerializable::Serialize(
35     uint8_t* buf, const uint8_t* end) const {
36   for (const auto& member : members_) {
37     buf = member->Serialize(buf, end);
38   }
39   return buf;
40 }
41 
Deserialize(const uint8_t ** buf_ptr,const uint8_t * end)42 bool CompositeSerializable::Deserialize(
43     const uint8_t** buf_ptr, const uint8_t* end) {
44   for (const auto& member : members_) {
45     if (!member->Deserialize(buf_ptr, end)) {
46       return false;
47     }
48   }
49   return true;
50 }
51 
52 }  // namespace cuttlefish
53