1 /*
2 * Copyright 2016 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
17 #include "avb_messages.h"
18
19 #include <stdio.h>
20 #include <string.h>
21
22 namespace avb {
23
GetSerializedSize() const24 uint32_t RollbackIndexRequest::GetSerializedSize() const {
25 return sizeof(value_) + sizeof(slot_);
26 }
27
Serialize(uint8_t * payload,const uint8_t * end) const28 uint32_t RollbackIndexRequest::Serialize(uint8_t* payload,
29 const uint8_t* end) const {
30 if (payload + GetSerializedSize() > end)
31 return 0;
32 memcpy(payload, &value_, sizeof(value_));
33 payload += sizeof(value_);
34 memcpy(payload, &slot_, sizeof(slot_));
35 payload += sizeof(slot_);
36 return GetSerializedSize();
37 }
38
Deserialize(const uint8_t * payload,const uint8_t * end)39 int RollbackIndexRequest::Deserialize(const uint8_t* payload,
40 const uint8_t* end) {
41 if (payload + GetSerializedSize() != end)
42 return ERR_NOT_VALID;
43 memcpy(&value_, payload, sizeof(value_));
44 payload += sizeof(value_);
45 memcpy(&slot_, payload, sizeof(slot_));
46 payload += sizeof(slot_);
47 return NO_ERROR;
48 }
49
GetSerializedSize() const50 uint32_t RollbackIndexResponse::GetSerializedSize() const {
51 return sizeof(value_);
52 }
53
Serialize(uint8_t * payload,const uint8_t * end) const54 uint32_t RollbackIndexResponse::Serialize(uint8_t* payload,
55 const uint8_t* end) const {
56 if (payload + GetSerializedSize() > end)
57 return 0;
58 memcpy(payload, &value_, sizeof(value_));
59 payload += sizeof(value_);
60 return GetSerializedSize();
61 }
62
Deserialize(const uint8_t * payload,const uint8_t * end)63 int RollbackIndexResponse::Deserialize(const uint8_t* payload,
64 const uint8_t* end) {
65 if (payload + GetSerializedSize() != end)
66 return ERR_NOT_VALID;
67 memcpy(&value_, payload, sizeof(value_));
68 payload += sizeof(value_);
69 return NO_ERROR;
70 }
71
GetSerializedSize() const72 uint32_t GetVersionResponse::GetSerializedSize() const {
73 return sizeof(version_);
74 }
75
Serialize(uint8_t * payload,const uint8_t * end) const76 uint32_t GetVersionResponse::Serialize(uint8_t* payload,
77 const uint8_t* end) const {
78 if (payload + GetSerializedSize() > end)
79 return 0;
80 memcpy(payload, &version_, sizeof(version_));
81 payload += sizeof(version_);
82 return GetSerializedSize();
83 }
84
Deserialize(const uint8_t * payload,const uint8_t * end)85 int GetVersionResponse::Deserialize(const uint8_t* payload,
86 const uint8_t* end) {
87 if (payload + GetSerializedSize() != end)
88 return ERR_NOT_VALID;
89 memcpy(&version_, payload, sizeof(version_));
90 payload += sizeof(version_);
91 return NO_ERROR;
92 }
93
GetSerializedSize() const94 uint32_t PermanentAttributesMessage::GetSerializedSize() const {
95 return attributes_size_;
96 }
97
Serialize(uint8_t * payload,const uint8_t * end) const98 uint32_t PermanentAttributesMessage::Serialize(uint8_t* payload,
99 const uint8_t* end) const {
100 if (static_cast<uint32_t>(end - payload) != attributes_size_)
101 return 0;
102 memcpy(payload, attributes_.get(), attributes_size_);
103 return attributes_size_;
104 }
105
Deserialize(const uint8_t * payload,const uint8_t * end)106 int PermanentAttributesMessage::Deserialize(const uint8_t* payload,
107 const uint8_t* end) {
108 if (end < payload)
109 return ERR_NOT_VALID;
110 attributes_size_ = end - payload;
111 attributes_.reset(new uint8_t[attributes_size_]);
112 if (!attributes_.get())
113 return ERR_NO_MEMORY;
114 memcpy(attributes_.get(), payload, attributes_size_);
115 return NO_ERROR;
116 }
117
GetSerializedSize() const118 uint32_t LockStateMessage::GetSerializedSize() const {
119 return sizeof(lock_state_);
120 }
121
Serialize(uint8_t * payload,const uint8_t * end) const122 uint32_t LockStateMessage::Serialize(uint8_t* payload,
123 const uint8_t* end) const {
124 if (payload + GetSerializedSize() > end)
125 return 0;
126 memcpy(payload, &lock_state_, sizeof(lock_state_));
127 payload += sizeof(lock_state_);
128 return GetSerializedSize();
129 }
130
Deserialize(const uint8_t * payload,const uint8_t * end)131 int LockStateMessage::Deserialize(const uint8_t* payload, const uint8_t* end) {
132 if (payload + GetSerializedSize() != end)
133 return ERR_NOT_VALID;
134 memcpy(&lock_state_, payload, sizeof(lock_state_));
135 payload += sizeof(lock_state_);
136 return NO_ERROR;
137 }
138
139 }; // namespace avb
140