2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "include/bundle.h"
21 #include "exception-internal.h"
22 #include "key-info-internal.h"
24 namespace tizen_base {
27 KeyInfo::KeyInfo(int type, std::string key,
28 std::vector<unsigned char> value)
30 key_(std::move(key)) {
31 int ret = SetValue(value);
32 if (ret != BUNDLE_ERROR_NONE)
36 KeyInfo::KeyInfo(int type, std::string key,
37 std::vector<std::vector<unsigned char>> values)
39 key_(std::move(key)) {
40 int ret = SetValues(values);
41 if (ret != BUNDLE_ERROR_NONE)
45 KeyInfo::KeyInfo(std::vector<unsigned char> encoded_bytes) {
46 int ret = Decode(encoded_bytes);
47 if (ret != BUNDLE_ERROR_NONE)
51 KeyInfo::~KeyInfo() = default;
53 KeyInfo::KeyInfo(const KeyInfo& key_info) {
54 type_ = key_info.type_;
56 values_size_ = key_info.values_size_;
57 uvalues_size_ = key_info.uvalues_size_;
58 for (unsigned int i = 0; i < key_info.values_.size(); ++i) {
59 auto* new_value = new (std::nothrow) unsigned char[values_size_[i]];
60 if (new_value == nullptr)
61 THROW(BUNDLE_ERROR_OUT_OF_MEMORY);
63 std::copy(key_info.values_[i].get(),
64 key_info.values_[i].get() + values_size_[i], new_value);
65 values_.emplace_back(new_value);
69 KeyInfo& KeyInfo::operator = (const KeyInfo& key_info) {
70 if (this != &key_info) {
71 type_ = key_info.type_;
73 values_size_ = key_info.values_size_;
74 uvalues_size_ = key_info.uvalues_size_;
75 for (unsigned int i = 0; i < key_info.values_.size(); ++i) {
76 auto* new_value = new (std::nothrow) unsigned char[values_size_[i]];
77 if (new_value == nullptr)
78 THROW(BUNDLE_ERROR_OUT_OF_MEMORY);
80 std::copy(key_info.values_[i].get(),
81 key_info.values_[i].get() + values_size_[i], new_value);
82 values_.emplace_back(new_value);
88 KeyInfo::KeyInfo(KeyInfo&& key_info) noexcept {
89 type_ = key_info.type_;
91 key_ = std::move(key_info.key_);
92 values_ = std::move(key_info.values_);
93 values_size_ = std::move(key_info.values_size_);
94 uvalues_size_ = std::move(key_info.uvalues_size_);
97 KeyInfo& KeyInfo::operator = (KeyInfo&& key_info) noexcept {
98 if (this != &key_info) {
99 type_ = key_info.type_;
101 key_ = std::move(key_info.key_);
102 values_ = std::move(key_info.values_);
103 values_size_ = std::move(key_info.values_size_);
104 uvalues_size_ = std::move(key_info.uvalues_size_);
109 bool KeyInfo::operator == (const KeyInfo& key_info) {
110 if (this == &key_info)
113 if (type_ != key_info.type_)
116 if (key_ != key_info.key_)
119 if (values_.size() != key_info.values_.size())
122 for (unsigned int i = 0; i < values_.size(); ++i) {
123 if (values_size_[i] == key_info.values_size_[i])
126 int ret = std::memcmp(values_[i].get(), key_info.values_[i].get(),
135 int KeyInfo::GetType() const {
139 bool KeyInfo::IsArray() const {
140 if (type_ & BUNDLE_TYPE_ARRAY)
146 const std::string& KeyInfo::GetKey() {
150 const std::vector<std::unique_ptr<unsigned char[]>>& KeyInfo::GetValues() {
154 const std::vector<std::size_t>& KeyInfo::GetValuesSize() {
158 const std::vector<unsigned int>& KeyInfo::GetUValuesSize() {
159 return uvalues_size_;
162 int KeyInfo::SetValue(const std::vector<unsigned char>& value) {
163 auto* new_value = new (std::nothrow) unsigned char[value.size()];
164 if (new_value == nullptr)
165 return BUNDLE_ERROR_OUT_OF_MEMORY;
167 std::copy(value.begin(), value.end(), new_value);
168 values_.emplace_back(new_value);
169 values_size_.push_back(value.size());
170 uvalues_size_.push_back(static_cast<unsigned int>(value.size()));
171 return BUNDLE_ERROR_NONE;
174 int KeyInfo::SetValues(const std::vector<std::vector<unsigned char>>& values) {
175 for (unsigned int i = 0; i< values.size(); ++i) {
176 auto* new_value = new (std::nothrow) unsigned char[values[i].size()];
177 if (new_value == nullptr)
178 return BUNDLE_ERROR_OUT_OF_MEMORY;
180 std::copy(values[i].begin(), values[i].end(), new_value);
181 values_.emplace_back(new_value);
182 values_size_.push_back(values[i].size());
183 uvalues_size_.push_back(values[i].size());
185 return BUNDLE_ERROR_NONE;
188 std::vector<unsigned char> KeyInfo::Encode() {
189 std::size_t encoded_size = GetEncodedSize();
190 if (encoded_size == 0)
193 std::vector<unsigned char> bytes;
196 unsigned char* p = reinterpret_cast<unsigned char*>(&encoded_size);
197 std::copy(p, p + sizeof(encoded_size), std::back_inserter(bytes));
200 p = reinterpret_cast<unsigned char*>(&type_);
201 std::copy(p, p + sizeof(type_), std::back_inserter(bytes));
204 std::size_t key_length = key_.length() + 1;
205 p = reinterpret_cast<unsigned char*>(&key_length);
206 std::copy(p, p + sizeof(key_length), std::back_inserter(bytes));
209 std::copy(key_.begin(), key_.end() + 1, std::back_inserter(bytes));
211 if (type_ & BUNDLE_TYPE_ARRAY) {
213 std::size_t values_size = values_.size();
214 p = reinterpret_cast<unsigned char*>(&values_size);
215 std::copy(p , p + sizeof(values_size), std::back_inserter(bytes));
219 for (unsigned int i = 0; i < values_.size(); i++) {
220 std::size_t value_size = values_size_[i];
221 p = reinterpret_cast<unsigned char*>(&value_size);
222 std::copy(p, p + sizeof(value_size), std::back_inserter(bytes));
224 std::copy(values_[i].get(), values_[i].get() + value_size,
225 std::back_inserter(bytes));
231 std::size_t KeyInfo::GetEncodedSize() {
233 std::size_t encoded_size = sizeof(std::size_t);
236 encoded_size += sizeof(int);
239 encoded_size += sizeof(std::size_t);
242 if ((encoded_size + key_.length() + 1) < encoded_size)
245 encoded_size += key_.length() + 1;
247 if (type_ & BUNDLE_TYPE_ARRAY) {
249 if ((encoded_size + sizeof(std::size_t)) < encoded_size)
252 encoded_size += sizeof(std::size_t);
256 std::size_t values_size = 0;
257 for (unsigned int i = 0; i < values_.size(); ++i) {
259 if ((values_size + sizeof(std::size_t)) < values_size)
262 values_size += sizeof(std::size_t);
265 if ((values_size + values_size_[i]) < values_size)
268 values_size += values_size_[i];
271 if ((encoded_size + values_size) < encoded_size)
274 encoded_size += values_size;
279 int KeyInfo::Decode(const std::vector<unsigned char>& bytes) {
280 unsigned int reader = 0;
283 std::size_t total_size = 0;
284 if ((reader + sizeof(total_size)) > bytes.size())
285 return BUNDLE_ERROR_INVALID_PARAMETER;
287 unsigned char* p = reinterpret_cast<unsigned char*>(&total_size);
288 std::copy(&bytes[reader], &bytes[reader] + sizeof(total_size), p);
289 reader += sizeof(total_size);
292 if ((reader + sizeof(type_)) > bytes.size())
293 return BUNDLE_ERROR_INVALID_PARAMETER;
295 p = reinterpret_cast<unsigned char*>(&type_);
296 std::copy(&bytes[reader], &bytes[reader] + sizeof(type_), p);
297 reader += sizeof(type_);
300 std::size_t key_size = 0;
302 if ((reader + sizeof(key_size)) > bytes.size())
303 return BUNDLE_ERROR_INVALID_PARAMETER;
305 p = reinterpret_cast<unsigned char*>(&key_size);
306 std::copy(&bytes[reader], &bytes[reader] + sizeof(key_size), p);
307 reader += sizeof(key_size);
309 if ((reader + key_size) > bytes.size())
310 return BUNDLE_ERROR_INVALID_PARAMETER;
313 std::vector<unsigned char> key(&bytes[reader], &bytes[reader] + key_size);
314 p = reinterpret_cast<unsigned char*>(&key[0]);
315 key_ = std::string(reinterpret_cast<char*>(p));
318 std::size_t values_size = 0;
319 if (type_ & BUNDLE_TYPE_ARRAY) {
321 if ((reader + sizeof(values_size)) > bytes.size())
322 return BUNDLE_ERROR_INVALID_PARAMETER;
324 p = reinterpret_cast<unsigned char*>(&values_size);
325 std::copy(&bytes[reader], &bytes[reader] + sizeof(values_size), p);
326 reader += sizeof(values_size);
332 for (std::size_t i = 0; i < values_size; ++i) {
334 std::size_t value_size = 0;
335 if ((reader + sizeof(value_size)) > bytes.size())
336 return BUNDLE_ERROR_INVALID_PARAMETER;
338 p = reinterpret_cast<unsigned char*>(&value_size);
339 std::copy(&bytes[reader], &bytes[reader] + sizeof(value_size), p);
340 reader += sizeof(value_size);
343 if ((reader + value_size) > bytes.size())
344 return BUNDLE_ERROR_INVALID_PARAMETER;
346 auto* new_value = new (std::nothrow) unsigned char[value_size];
347 if (new_value == nullptr)
348 return BUNDLE_ERROR_OUT_OF_MEMORY;
350 std::copy(&bytes[reader], &bytes[reader] + value_size, new_value);
351 reader += value_size;
353 values_.emplace_back(new_value);
354 values_size_.push_back(value_size);
355 uvalues_size_.push_back(static_cast<unsigned int>(value_size));
358 return BUNDLE_ERROR_NONE;
361 int KeyInfo::SetValue(int index, const std::vector<unsigned char>& value) {
362 if (index > GetSize() || index < 0)
363 return BUNDLE_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS;
366 return BUNDLE_ERROR_INVALID_PARAMETER;
368 auto* new_value = new (std::nothrow) unsigned char[value.size()];
369 if (new_value == nullptr)
370 return BUNDLE_ERROR_OUT_OF_MEMORY;
372 if (value.size() != 0)
373 std::copy(value.begin(), value.end(), new_value);
375 values_[index].reset(new_value);
376 values_size_[index] = value.size();
377 uvalues_size_[index] = static_cast<unsigned int>(value.size());
378 return BUNDLE_ERROR_NONE;
381 int KeyInfo::GetSize() const {
382 return values_.size();
385 } // namespace internal
386 } // namespace tizen_base