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 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);
57 KeyInfo& KeyInfo::operator = (const KeyInfo& key_info) {
58 if (this != &key_info) {
59 type_ = key_info.type_;
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);
72 KeyInfo::KeyInfo(KeyInfo&& key_info) noexcept {
73 type_ = key_info.type_;
75 key_ = std::move(key_info.key_);
76 values_ = std::move(key_info.values_);
77 values_size_ = std::move(key_info.values_size_);
80 KeyInfo& KeyInfo::operator = (KeyInfo&& key_info) noexcept {
81 if (this != &key_info) {
82 type_ = key_info.type_;
84 key_ = std::move(key_info.key_);
85 values_ = std::move(key_info.values_);
86 values_size_ = std::move(key_info.values_size_);
91 bool KeyInfo::operator == (const KeyInfo& key_info) {
92 if (this == &key_info)
95 if (type_ != key_info.type_)
98 if (key_ != key_info.key_)
101 if (values_.size() != key_info.values_.size())
104 for (unsigned int i = 0; i < values_.size(); ++i) {
105 if (values_size_[i] == key_info.values_size_[i])
108 int ret = std::memcmp(values_[i].get(), key_info.values_[i].get(),
117 int KeyInfo::GetType() const {
121 bool KeyInfo::IsArray() const {
122 if (values_.size() == 1)
128 const std::string& KeyInfo::GetKey() {
132 const std::vector<std::unique_ptr<unsigned char[]>>& KeyInfo::GetValues() {
136 const std::vector<std::size_t>& KeyInfo::GetValuesSize() {
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());
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());
156 std::vector<unsigned char> KeyInfo::Encode() {
157 std::size_t encoded_size = GetEncodedSize();
158 if (encoded_size == 0)
161 std::vector<unsigned char> bytes;
164 unsigned char* p = reinterpret_cast<unsigned char*>(&encoded_size);
165 std::copy(p, p + sizeof(encoded_size), std::back_inserter(bytes));
168 p = reinterpret_cast<unsigned char*>(&type_);
169 std::copy(p, p + sizeof(type_), std::back_inserter(bytes));
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));
177 std::copy(key_.begin(), key_.end() + 1, std::back_inserter(bytes));
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));
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));
190 std::copy(values_[i].get(), values_[i].get() + value_size,
191 std::back_inserter(bytes));
197 std::size_t KeyInfo::GetEncodedSize() {
199 std::size_t encoded_size = sizeof(std::size_t);
202 encoded_size += sizeof(int);
205 encoded_size += sizeof(std::size_t);
208 if ((encoded_size + key_.length() + 1) < encoded_size)
211 encoded_size += key_.length() + 1;
214 if ((encoded_size + sizeof(std::size_t)) < encoded_size)
217 encoded_size += sizeof(std::size_t);
220 std::size_t values_size = 0;
221 for (unsigned int i = 0; i < values_.size(); ++i) {
223 if ((values_size + sizeof(std::size_t)) < values_size)
226 values_size += sizeof(std::size_t);
229 if ((values_size + values_size_[i]) < values_size)
232 values_size += values_size_[i];
235 if ((encoded_size + values_size) < encoded_size)
238 encoded_size += values_size;
243 void KeyInfo::Decode(const std::vector<unsigned char>& bytes) {
244 unsigned int reader = 0;
247 std::size_t total_size = 0;
248 if ((reader + sizeof(total_size)) > bytes.size())
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);
256 if ((reader + sizeof(type_)) > bytes.size())
259 p = reinterpret_cast<unsigned char*>(&type_);
260 std::copy(&bytes[reader], &bytes[reader] + sizeof(type_), p);
261 reader += sizeof(type_);
264 std::size_t key_size = 0;
266 if ((reader + sizeof(key_size)) > bytes.size())
269 p = reinterpret_cast<unsigned char*>(&key_size);
270 std::copy(&bytes[reader], &bytes[reader] + sizeof(key_size), p);
271 reader += sizeof(key_size);
273 if ((reader + key_size) > bytes.size())
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));
283 std::size_t values_size = 0;
284 if ((reader + sizeof(values_size)) > bytes.size())
287 p = reinterpret_cast<unsigned char*>(&values_size);
288 std::copy(&bytes[reader], &bytes[reader] + sizeof(values_size), p);
289 reader += sizeof(values_size);
292 for (std::size_t i = 0; i < values_size; ++i) {
294 std::size_t value_size = 0;
295 if ((reader + sizeof(value_size)) > bytes.size())
298 p = reinterpret_cast<unsigned char*>(&value_size);
299 std::copy(&bytes[reader], &bytes[reader] + sizeof(value_size), p);
300 reader += sizeof(value_size);
303 if ((reader + value_size) > bytes.size())
306 auto* new_value = new unsigned char[value_size];
307 std::copy(&bytes[reader], &bytes[reader] + value_size, new_value);
308 reader += value_size;
310 values_.emplace_back(new_value);
311 values_size_.push_back(value_size);
315 void KeyInfo::SetValue(int index, const std::vector<unsigned char>& value) {
316 if (index > GetSize() && index < 0)
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();
325 int KeyInfo::GetSize() const {
326 return values_.size();
329 } // namespace internal
330 } // namespace tizen_base