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 
CompositeSerializable(const std::vector<Serializable * > & members)20 CompositeSerializable::CompositeSerializable(
21     const std::vector<Serializable*>& members) : members_(members) {
22 }
23 
SerializedSize() const24 size_t CompositeSerializable::SerializedSize() const {
25   size_t sum = 0;
26   for (const auto& member : members_) {
27     sum += member->SerializedSize();
28   }
29   return sum;
30 }
31 
Serialize(uint8_t * buf,const uint8_t * end) const32 uint8_t* CompositeSerializable::Serialize(
33     uint8_t* buf, const uint8_t* end) const {
34   for (const auto& member : members_) {
35     buf = member->Serialize(buf, end);
36   }
37   return buf;
38 }
39 
Deserialize(const uint8_t ** buf_ptr,const uint8_t * end)40 bool CompositeSerializable::Deserialize(
41     const uint8_t** buf_ptr, const uint8_t* end) {
42   for (const auto& member : members_) {
43     if (!member->Deserialize(buf_ptr, end)) {
44       return false;
45     }
46   }
47   return true;
48 }
49