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