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.
17 #include "bundle-internal.h"
24 #include "include/bundle.h"
26 #include "exception-internal.h"
28 namespace tizen_base {
31 static const int CHECKSUM_LENGTH = 32;
33 Bundle::Bundle(unsigned char* raw, int size, bool base64) {
36 ret = Decode(raw, size);
38 ret = DecodeRaw(raw, size);
39 if (ret != BUNDLE_ERROR_NONE)
43 Bundle::Bundle(int argc, char** argv) {
44 int ret = Import(argc, argv);
45 if (ret != BUNDLE_ERROR_NONE)
49 Bundle::Bundle(const Bundle& b) {
54 Bundle& Bundle::operator = (const Bundle& b) {
62 Bundle::Bundle(Bundle&& b) noexcept {
63 map_ = std::move(b.map_);
64 list_ = std::move(b.list_);
67 Bundle& Bundle::operator = (Bundle&& b) noexcept {
69 map_ = std::move(b.map_);
70 list_ = std::move(b.list_);
75 bool Bundle::operator == (const Bundle& b) {
79 if (map_.size() != b.map_.size())
82 for (const auto& kv : map_) {
83 auto& lhs = kv.second;
84 auto iter = b.map_.find(lhs->GetKey());
85 if (iter == b.map_.end())
88 auto& rhs = iter->second;
96 void Bundle::Remove(const std::string& key) {
97 auto iter = map_.find(key);
98 if (iter == map_.end())
99 THROW(BUNDLE_ERROR_KEY_NOT_AVAILABLE);
101 list_.remove(iter->second);
105 void Bundle::Add(std::shared_ptr<KeyInfo> key_info) {
106 auto iter = map_.find(key_info->GetKey());
107 if (iter != map_.end())
108 THROW(BUNDLE_ERROR_KEY_EXISTS);
110 map_[key_info->GetKey()] = key_info;
111 list_.push_back(std::move(key_info));
114 void Bundle::Set(const std::string& key, int index,
115 std::vector<unsigned char> value) {
116 auto iter = map_.find(key);
117 if (iter == map_.end())
118 THROW(BUNDLE_ERROR_KEY_NOT_AVAILABLE);
120 int ret = iter->second->SetValue(index, value);
121 if (ret != BUNDLE_ERROR_NONE)
125 std::shared_ptr<KeyInfo>& Bundle::Get(const std::string& key) {
126 auto iter = map_.find(key.c_str());
127 if (iter == map_.end())
128 THROW(BUNDLE_ERROR_KEY_NOT_AVAILABLE);
133 int Bundle::GetSize() {
137 int Bundle::GetType(const std::string& key) {
138 auto iter = map_.find(key);
139 if (iter == map_.end())
140 THROW(BUNDLE_ERROR_KEY_NOT_AVAILABLE);
142 return iter->second->GetType();
145 unsigned char* Bundle::Encode() {
149 raw = EncodeRaw(&size);
150 } catch (const Exception& e) {
151 THROW(e.GetErrorCode());
154 std::unique_ptr<unsigned char, decltype(std::free)*> raw_ptr(raw, std::free);
155 char* encoded_data = reinterpret_cast<char*>(
156 g_base64_encode(reinterpret_cast<guchar*>(raw),
157 static_cast<gsize>(size)));
158 if (encoded_data == nullptr)
159 THROW(BUNDLE_ERROR_OUT_OF_MEMORY);
161 return reinterpret_cast<unsigned char*>(encoded_data);
164 int Bundle::Decode(unsigned char* raw, int size) {
165 unsigned char* d_str = new (std::nothrow) unsigned char[(size / 4) * 3 + 3];
166 if (d_str == nullptr)
167 return BUNDLE_ERROR_OUT_OF_MEMORY;
169 std::unique_ptr<unsigned char[]> d_ptr(d_str);
172 unsigned int d_len_raw = g_base64_decode_step(reinterpret_cast<char*>(raw),
173 size, d_str, &state, &save);
174 if (d_len_raw < CHECKSUM_LENGTH)
175 return BUNDLE_ERROR_OUT_OF_MEMORY;
177 return DecodeRaw(d_str, d_len_raw);
180 unsigned char* Bundle::EncodeRaw(int* size) {
181 std::vector<unsigned char> bytes;
182 for (const auto& key_info : list_) {
183 auto encoded_bytes = key_info->Encode();
184 bytes.insert(bytes.end(),
185 std::make_move_iterator(encoded_bytes.begin()),
186 std::make_move_iterator(encoded_bytes.end()));
189 gchar* checksum = g_compute_checksum_for_string(
190 G_CHECKSUM_MD5, reinterpret_cast<gchar*>(&bytes[0]),
191 static_cast<gssize>(bytes.size()));
192 if (checksum == nullptr)
193 THROW(BUNDLE_ERROR_OUT_OF_MEMORY);
195 std::unique_ptr<gchar, decltype(g_free)*> ptr(checksum, g_free);
196 unsigned char* p = reinterpret_cast<unsigned char*>(checksum);
197 bytes.insert(bytes.begin(), p, p + CHECKSUM_LENGTH);
199 unsigned char* raw = static_cast<unsigned char*>(malloc(bytes.size()));
201 THROW(BUNDLE_ERROR_OUT_OF_MEMORY);
203 std::copy(std::make_move_iterator(bytes.begin()),
204 std::make_move_iterator(bytes.end()), raw);
205 *size = static_cast<int>(bytes.size());
209 int Bundle::DecodeRaw(unsigned char* raw, int size) {
210 char* extract_checksum = new (std::nothrow) char[CHECKSUM_LENGTH + 1];
211 if (extract_checksum == nullptr)
212 return BUNDLE_ERROR_OUT_OF_MEMORY;
214 std::unique_ptr<char[]> extract_ptr(extract_checksum);
215 unsigned char* d_str = raw;
216 unsigned int d_len_raw = size;
217 strncpy(extract_checksum, reinterpret_cast<char*>(d_str), CHECKSUM_LENGTH);
218 extract_checksum[CHECKSUM_LENGTH] = '\0';
220 gchar* compute_checksum = g_compute_checksum_for_string(G_CHECKSUM_MD5,
221 reinterpret_cast<gchar*>(d_str + CHECKSUM_LENGTH),
222 d_len_raw - CHECKSUM_LENGTH);
223 if (compute_checksum == nullptr)
224 return BUNDLE_ERROR_OUT_OF_MEMORY;
226 std::unique_ptr<gchar, decltype(g_free)*> compute_ptr(compute_checksum,
228 if (strcmp(extract_checksum, static_cast<char*>(compute_checksum)) != 0)
229 return BUNDLE_ERROR_INVALID_PARAMETER;
231 unsigned char* d_r = d_str + CHECKSUM_LENGTH;
232 unsigned int d_len = d_len_raw - CHECKSUM_LENGTH;
234 unsigned int reader = 0;
235 std::vector<unsigned char> bytes(d_r, d_r + d_len);
237 while (reader < bytes.size()) {
238 std::size_t total_size = -1;
239 unsigned char* p = reinterpret_cast<unsigned char*>(&total_size);
240 std::copy(&bytes[reader], &bytes[reader] + sizeof(total_size), p);
242 std::vector<unsigned char> encoded_bytes(
243 &bytes[reader], &bytes[reader] + total_size);
244 reader += total_size;
246 KeyInfo* new_key_info;
248 new_key_info = new KeyInfo(std::move(encoded_bytes));
249 } catch (const Exception& e) {
250 return e.GetErrorCode();
251 } catch (const std::bad_alloc& ba) {
252 return BUNDLE_ERROR_OUT_OF_MEMORY;
255 auto key_info = std::shared_ptr<KeyInfo>(new_key_info);
256 auto iter = map_.find(key_info->GetKey());
257 if (iter != map_.end())
260 map_[key_info->GetKey()] = key_info;
261 list_.push_back(std::move(key_info));
264 return BUNDLE_ERROR_NONE;
267 const std::unordered_map<std::string, std::shared_ptr<KeyInfo>>&
268 Bundle::GetMap() const {
272 std::vector<std::string> Bundle::Export() {
273 std::vector<std::string> argv(2);
274 for (const auto& key_info : list_) {
275 argv.push_back(key_info->GetKey());
277 auto encoded_bytes = key_info->Encode();
278 auto* p = reinterpret_cast<unsigned char*>(&encoded_bytes[0]);
279 auto* base64_bytes = g_base64_encode(p, encoded_bytes.size());
280 if (base64_bytes == nullptr)
281 THROW(BUNDLE_ERROR_OUT_OF_MEMORY);
283 std::unique_ptr<gchar, decltype(g_free)*> base64_bytes_ptr(base64_bytes,
285 argv.push_back(base64_bytes);
291 int Bundle::Import(int argc, char** argv) {
293 return BUNDLE_ERROR_NONE;
295 if (!argv[1] || std::strcmp(argv[1], TAG_IMPORT_EXPORT_CHECK)) {
296 for (int idx = 1; idx + 1 < argc; idx += 2) {
297 auto* p = reinterpret_cast<unsigned char*>(argv[idx +1]);
298 auto len = strlen(argv[idx + 1]) + 1;
299 std::vector<unsigned char> value(p, p + len);
301 KeyInfo* new_key_info;
303 new_key_info = new KeyInfo(Type::String, argv[idx], std::move(value));
304 } catch (const Exception& e) {
305 return e.GetErrorCode();
306 } catch (const std::bad_alloc& ba) {
307 return BUNDLE_ERROR_OUT_OF_MEMORY;
310 auto key_info = std::shared_ptr<KeyInfo>(new_key_info);
311 auto iter = map_.find(key_info->GetKey());
312 if (iter != map_.end())
315 map_[key_info->GetKey()] = key_info;
316 list_.push_back(key_info);
318 return BUNDLE_ERROR_NONE;
321 for (int idx = 2; idx + 1 < argc; idx += 2) {
323 auto* bytes = g_base64_decode(argv[idx + 1], &out_len);
324 if (bytes == nullptr)
325 return BUNDLE_ERROR_OUT_OF_MEMORY;
327 std::unique_ptr<guchar, decltype(g_free)*> bytes_ptr(bytes, g_free);
329 if (out_len < sizeof(std::size_t))
332 auto* p = reinterpret_cast<unsigned char*>(bytes);
333 std::vector<unsigned char> decoded_bytes(p, p + (out_len + 1));
335 KeyInfo* new_key_info;
337 new_key_info = new KeyInfo(std::move(decoded_bytes));
338 } catch (const Exception& e) {
339 return e.GetErrorCode();
340 } catch (const std::bad_alloc& ba) {
341 return BUNDLE_ERROR_OUT_OF_MEMORY;
344 auto key_info = std::shared_ptr<KeyInfo>(new_key_info);
345 auto iter = map_.find(key_info->GetKey());
346 if (iter != map_.end())
349 map_[key_info->GetKey()] = key_info;
350 list_.push_back(std::move(key_info));
353 return BUNDLE_ERROR_NONE;
356 } // namespace internal
357 } // namespace tizen_base