Refactor Bundle
[platform/core/base/bundle.git] / src / key-info-internal.cc
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All rights reserved.
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 <cstring>
18
19 #include "key-info-internal.h"
20 #include "log-private.h"
21
22 namespace tizen_base {
23 namespace internal {
24
25 KeyInfo::KeyInfo(int type, std::string key,
26     std::vector<unsigned char> value)
27   : type_(type),
28     key_(std::move(key)) {
29   SetValue(value);
30 }
31
32 KeyInfo::KeyInfo(int type, std::string key,
33     std::vector<std::vector<unsigned char>> values)
34   : type_(type),
35     key_(std::move(key)) {
36   SetValues(values);
37 }
38
39 KeyInfo::KeyInfo(std::vector<unsigned char> encoded_bytes) {
40   Decode(encoded_bytes);
41 }
42
43 KeyInfo::~KeyInfo() = default;
44
45 KeyInfo::KeyInfo(const KeyInfo& key_info) {
46   type_ = key_info.type_;
47   key_ = key_info.key_;
48   values_size_ = key_info.values_size_;
49   for (unsigned int i = 0; i < key_info.values_.size(); ++i) {
50     auto* new_value = new (std::nothrow) unsigned char[values_size_[i]];
51     std::copy(key_info.values_[i].get(),
52         key_info.values_[i].get() + values_size_[i], new_value);
53     values_.emplace_back(new_value);
54   }
55 }
56
57 KeyInfo& KeyInfo::operator = (const KeyInfo& key_info) {
58   if (this != &key_info) {
59     type_ = key_info.type_;
60     key_ = key_info.key_;
61     values_size_ = key_info.values_size_;
62     for (unsigned int i = 0; i < key_info.values_.size(); ++i) {
63       auto* new_value = new (std::nothrow) unsigned char[values_size_[i]];
64       std::copy(key_info.values_[i].get(),
65           key_info.values_[i].get() + values_size_[i], new_value);
66       values_.emplace_back(new_value);
67     }
68   }
69   return *this;
70 }
71
72 KeyInfo::KeyInfo(KeyInfo&& key_info) noexcept {
73   type_ = key_info.type_;
74   key_info.type_ = 0;
75   key_ = std::move(key_info.key_);
76   values_ = std::move(key_info.values_);
77   values_size_ = std::move(key_info.values_size_);
78 }
79
80 KeyInfo& KeyInfo::operator = (KeyInfo&& key_info) noexcept {
81   if (this != &key_info) {
82     type_ = key_info.type_;
83     key_info.type_ = 0;
84     key_ = std::move(key_info.key_);
85     values_ = std::move(key_info.values_);
86     values_size_ = std::move(key_info.values_size_);
87   }
88   return *this;
89 }
90
91 bool KeyInfo::operator == (const KeyInfo& key_info) {
92   if (this == &key_info)
93     return true;
94
95   if (type_ != key_info.type_)
96     return false;
97
98   if (key_ != key_info.key_)
99     return false;
100
101   if (values_.size() != key_info.values_.size())
102     return false;
103
104   for (unsigned int i = 0; i < values_.size(); ++i) {
105     if (values_size_[i] == key_info.values_size_[i])
106       return false;
107
108     int ret = std::memcmp(values_[i].get(), key_info.values_[i].get(),
109         values_size_[i]);
110     if (ret == 0)
111       return false;
112   }
113
114   return true;
115 }
116
117 int KeyInfo::GetType() const {
118   return type_;
119 }
120
121 bool KeyInfo::IsArray() const {
122   if (values_.size() == 1)
123     return true;
124
125   return false;
126 }
127
128 const std::string& KeyInfo::GetKey() {
129   return key_;
130 }
131
132 const std::vector<std::unique_ptr<unsigned char[]>>& KeyInfo::GetValues() {
133   return values_;
134 }
135
136 const std::vector<std::size_t>& KeyInfo::GetValuesSize() {
137   return values_size_;
138 }
139
140 void KeyInfo::SetValue(const std::vector<unsigned char>& value) {
141   auto* new_value = new (std::nothrow) unsigned char[value.size()];
142   std::copy(value.begin(), value.end(), new_value);
143   values_.emplace_back(new_value);
144   values_size_.push_back(value.size());
145 }
146
147 void KeyInfo::SetValues(const std::vector<std::vector<unsigned char>>& values) {
148   for (unsigned int i = 0; i< values.size(); ++i) {
149     auto* new_value = new (std::nothrow) unsigned char[values[i].size()];
150     std::copy(values[i].begin(), values[i].end(), new_value);
151     values_.emplace_back(new_value);
152     values_size_.push_back(values[i].size());
153   }
154 }
155
156 std::vector<unsigned char> KeyInfo::Encode() {
157   std::size_t encoded_size = GetEncodedSize();
158   if (encoded_size == 0)
159     return {};
160
161   std::vector<unsigned char> bytes;
162
163   // total size
164   unsigned char* p = reinterpret_cast<unsigned char*>(&encoded_size);
165   std::copy(p, p + sizeof(encoded_size), std::back_inserter(bytes));
166
167   // type
168   p = reinterpret_cast<unsigned char*>(&type_);
169   std::copy(p, p + sizeof(type_), std::back_inserter(bytes));
170
171   // key size
172   std::size_t key_length = key_.length() + 1;
173   p = reinterpret_cast<unsigned char*>(&key_length);
174   std::copy(p, p + sizeof(key_length), std::back_inserter(bytes));
175
176   // key
177   std::copy(key_.begin(), key_.end() + 1, std::back_inserter(bytes));
178
179   // values size
180   std::size_t values_size = values_.size();
181   p = reinterpret_cast<unsigned char*>(&values_size);
182   std::copy(p , p + sizeof(values_size), std::back_inserter(bytes));
183
184   // values
185   for (unsigned int i = 0; i < values_.size(); i++) {
186     std::size_t value_size = values_size_[i];
187     p = reinterpret_cast<unsigned char*>(&value_size);
188     std::copy(p, p + sizeof(value_size), std::back_inserter(bytes));
189
190     std::copy(values_[i].get(), values_[i].get() + value_size,
191         std::back_inserter(bytes));
192   }
193
194   return bytes;
195 }
196
197 std::size_t KeyInfo::GetEncodedSize() {
198   // total size
199   std::size_t encoded_size = sizeof(std::size_t);
200
201   // type
202   encoded_size += sizeof(int);
203
204   // key size
205   encoded_size += sizeof(std::size_t);
206
207   // key
208   if ((encoded_size + key_.length() + 1) < encoded_size)
209     return 0;
210
211   encoded_size += key_.length() + 1;
212
213   // values size
214   if ((encoded_size + sizeof(std::size_t)) < encoded_size)
215     return 0;
216
217   encoded_size += sizeof(std::size_t);
218
219   // values
220   std::size_t values_size = 0;
221   for (unsigned int i = 0; i < values_.size(); ++i) {
222     // value size
223     if ((values_size + sizeof(std::size_t)) < values_size)
224       return 0;
225
226     values_size += sizeof(std::size_t);
227
228     // value
229     if ((values_size + values_size_[i]) < values_size)
230       return 0;
231
232     values_size += values_size_[i];
233   }
234
235   if ((encoded_size + values_size) < encoded_size)
236     return 0;
237
238   encoded_size += values_size;
239
240   return encoded_size;
241 }
242
243 void KeyInfo::Decode(const std::vector<unsigned char>& bytes) {
244   unsigned int reader = 0;
245
246   // total size
247   std::size_t total_size = 0;
248   if ((reader + sizeof(total_size)) > bytes.size())
249     return;
250
251   unsigned char* p = reinterpret_cast<unsigned char*>(&total_size);
252   std::copy(&bytes[reader], &bytes[reader] + sizeof(total_size), p);
253   reader += sizeof(total_size);
254
255   // type
256   if ((reader + sizeof(type_)) > bytes.size())
257       return;
258
259   p = reinterpret_cast<unsigned char*>(&type_);
260   std::copy(&bytes[reader], &bytes[reader] + sizeof(type_),  p);
261   reader += sizeof(type_);
262
263   // key size
264   std::size_t key_size = 0;
265
266   if ((reader + sizeof(key_size)) > bytes.size())
267     return;
268
269   p = reinterpret_cast<unsigned char*>(&key_size);
270   std::copy(&bytes[reader], &bytes[reader] + sizeof(key_size), p);
271   reader += sizeof(key_size);
272
273   if ((reader + key_size) > bytes.size())
274     return;
275
276   // key
277   std::vector<unsigned char> key(&bytes[reader], &bytes[reader] + key_size);
278   p = reinterpret_cast<unsigned char*>(&key[0]);
279   key_ = std::string(reinterpret_cast<char*>(p));
280   reader += key_size;
281
282   // values size
283   std::size_t values_size = 0;
284   if ((reader + sizeof(values_size)) > bytes.size())
285     return;
286
287   p = reinterpret_cast<unsigned char*>(&values_size);
288   std::copy(&bytes[reader], &bytes[reader] + sizeof(values_size), p);
289   reader += sizeof(values_size);
290
291   // values
292   for (std::size_t i = 0; i < values_size; ++i) {
293     // value_size
294     std::size_t value_size = 0;
295     if ((reader + sizeof(value_size)) > bytes.size())
296       return;
297
298     p = reinterpret_cast<unsigned char*>(&value_size);
299     std::copy(&bytes[reader], &bytes[reader] + sizeof(value_size), p);
300     reader += sizeof(value_size);
301
302     // value
303     if ((reader + value_size) > bytes.size())
304       return;
305
306     auto* new_value = new unsigned char[value_size];
307     std::copy(&bytes[reader], &bytes[reader] + value_size, new_value);
308     reader += value_size;
309
310     values_.emplace_back(new_value);
311     values_size_.push_back(value_size);
312   }
313 }
314
315 void KeyInfo::SetValue(int index, const std::vector<unsigned char>& value) {
316   if (index > GetSize() && index < 0)
317     return;
318
319   auto* new_value = new unsigned char[value.size()];
320   std::copy(value.begin(), value.end(), new_value);
321   values_[index].reset(new_value);
322   values_size_[index] = value.size();
323 }
324
325 int KeyInfo::GetSize() const {
326   return values_.size();
327 }
328
329 }  // namespace internal
330 }  // namespace tizen_base