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 "key-info-internal.h"
20 #include "log-private.h"
22 namespace tizen_base {
25 KeyInfo::KeyInfo(int type, std::string key,
26 std::vector<unsigned char> value)
28 key_(std::move(key)) {
32 KeyInfo::KeyInfo(int type, std::string key,
33 std::vector<std::vector<unsigned char>> values)
35 key_(std::move(key)) {
39 KeyInfo::KeyInfo(std::vector<unsigned char> encoded_bytes) {
40 Decode(encoded_bytes);
43 KeyInfo::~KeyInfo() = default;
45 KeyInfo::KeyInfo(const KeyInfo& key_info) {
46 type_ = key_info.type_;
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);
58 KeyInfo& KeyInfo::operator = (const KeyInfo& key_info) {
59 if (this != &key_info) {
60 type_ = key_info.type_;
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);
74 KeyInfo::KeyInfo(KeyInfo&& key_info) noexcept {
75 type_ = key_info.type_;
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_);
83 KeyInfo& KeyInfo::operator = (KeyInfo&& key_info) noexcept {
84 if (this != &key_info) {
85 type_ = key_info.type_;
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_);
95 bool KeyInfo::operator == (const KeyInfo& key_info) {
96 if (this == &key_info)
99 if (type_ != key_info.type_)
102 if (key_ != key_info.key_)
105 if (values_.size() != key_info.values_.size())
108 for (unsigned int i = 0; i < values_.size(); ++i) {
109 if (values_size_[i] == key_info.values_size_[i])
112 int ret = std::memcmp(values_[i].get(), key_info.values_[i].get(),
121 int KeyInfo::GetType() const {
125 bool KeyInfo::IsArray() const {
126 if (values_.size() == 1)
132 const std::string& KeyInfo::GetKey() {
136 const std::vector<std::unique_ptr<unsigned char[]>>& KeyInfo::GetValues() {
140 const std::vector<std::size_t>& KeyInfo::GetValuesSize() {
144 const std::vector<unsigned int>& KeyInfo::GetUValuesSize() {
145 return uvalues_size_;
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()));
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());
166 std::vector<unsigned char> KeyInfo::Encode() {
167 std::size_t encoded_size = GetEncodedSize();
168 if (encoded_size == 0)
171 std::vector<unsigned char> bytes;
174 unsigned char* p = reinterpret_cast<unsigned char*>(&encoded_size);
175 std::copy(p, p + sizeof(encoded_size), std::back_inserter(bytes));
178 p = reinterpret_cast<unsigned char*>(&type_);
179 std::copy(p, p + sizeof(type_), std::back_inserter(bytes));
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));
187 std::copy(key_.begin(), key_.end() + 1, std::back_inserter(bytes));
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));
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));
200 std::copy(values_[i].get(), values_[i].get() + value_size,
201 std::back_inserter(bytes));
207 std::size_t KeyInfo::GetEncodedSize() {
209 std::size_t encoded_size = sizeof(std::size_t);
212 encoded_size += sizeof(int);
215 encoded_size += sizeof(std::size_t);
218 if ((encoded_size + key_.length() + 1) < encoded_size)
221 encoded_size += key_.length() + 1;
224 if ((encoded_size + sizeof(std::size_t)) < encoded_size)
227 encoded_size += sizeof(std::size_t);
230 std::size_t values_size = 0;
231 for (unsigned int i = 0; i < values_.size(); ++i) {
233 if ((values_size + sizeof(std::size_t)) < values_size)
236 values_size += sizeof(std::size_t);
239 if ((values_size + values_size_[i]) < values_size)
242 values_size += values_size_[i];
245 if ((encoded_size + values_size) < encoded_size)
248 encoded_size += values_size;
253 void KeyInfo::Decode(const std::vector<unsigned char>& bytes) {
254 unsigned int reader = 0;
257 std::size_t total_size = 0;
258 if ((reader + sizeof(total_size)) > bytes.size())
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);
266 if ((reader + sizeof(type_)) > bytes.size())
269 p = reinterpret_cast<unsigned char*>(&type_);
270 std::copy(&bytes[reader], &bytes[reader] + sizeof(type_), p);
271 reader += sizeof(type_);
274 std::size_t key_size = 0;
276 if ((reader + sizeof(key_size)) > bytes.size())
279 p = reinterpret_cast<unsigned char*>(&key_size);
280 std::copy(&bytes[reader], &bytes[reader] + sizeof(key_size), p);
281 reader += sizeof(key_size);
283 if ((reader + key_size) > bytes.size())
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));
293 std::size_t values_size = 0;
294 if ((reader + sizeof(values_size)) > bytes.size())
297 p = reinterpret_cast<unsigned char*>(&values_size);
298 std::copy(&bytes[reader], &bytes[reader] + sizeof(values_size), p);
299 reader += sizeof(values_size);
302 for (std::size_t i = 0; i < values_size; ++i) {
304 std::size_t value_size = 0;
305 if ((reader + sizeof(value_size)) > bytes.size())
308 p = reinterpret_cast<unsigned char*>(&value_size);
309 std::copy(&bytes[reader], &bytes[reader] + sizeof(value_size), p);
310 reader += sizeof(value_size);
313 if ((reader + value_size) > bytes.size())
316 auto* new_value = new unsigned char[value_size];
317 std::copy(&bytes[reader], &bytes[reader] + value_size, new_value);
318 reader += value_size;
320 values_.emplace_back(new_value);
321 values_size_.push_back(value_size);
322 uvalues_size_.push_back(static_cast<unsigned int>(value_size));
326 void KeyInfo::SetValue(int index, const std::vector<unsigned char>& value) {
327 if (index > GetSize() && index < 0)
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());
337 int KeyInfo::GetSize() const {
338 return values_.size();
341 } // namespace internal
342 } // namespace tizen_base