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.
20 #include "include/bundle.h"
21 #include "include/bundle_internal.h"
23 #include "bundle-internal.h"
24 #include "exception-internal.h"
25 #include "export-api-internal.h"
26 #include "json-internal.h"
28 using namespace tizen_base::internal;
30 extern "C" EXPORT_API bundle* bundle_create(void) {
31 auto* h = new (std::nothrow) Bundle();
33 set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY);
37 set_last_result(BUNDLE_ERROR_NONE);
38 return reinterpret_cast<bundle*>(h);
41 extern "C" EXPORT_API int bundle_free(bundle* b) {
43 return BUNDLE_ERROR_INVALID_PARAMETER;
45 auto* h = reinterpret_cast<Bundle*>(b);
47 return BUNDLE_ERROR_NONE;
50 extern "C" EXPORT_API int bundle_add_str(bundle* b,
51 const char* key, const char* str) {
52 if (b == nullptr || key == nullptr || strlen(key) == 0 || str == nullptr)
53 return BUNDLE_ERROR_INVALID_PARAMETER;
55 auto* h = reinterpret_cast<Bundle*>(b);
56 std::vector<unsigned char> value(str, str + (strlen(str) + 1));
60 key_info = new KeyInfo(BUNDLE_TYPE_STR, key, value);
61 } catch (Exception& e) {
62 return e.GetErrorCode();
63 } catch (const std::bad_alloc& ba) {
64 return BUNDLE_ERROR_OUT_OF_MEMORY;
68 h->Add(std::shared_ptr<KeyInfo>(key_info));
69 } catch (Exception& e) {
70 return e.GetErrorCode();
73 return BUNDLE_ERROR_NONE;
76 extern "C" EXPORT_API int bundle_get_str(bundle* b,
77 const char* key, char** str) {
78 if (b == nullptr || key == nullptr)
79 return BUNDLE_ERROR_INVALID_PARAMETER;
81 auto* h = reinterpret_cast<Bundle*>(b);
82 std::shared_ptr<KeyInfo> key_info;
84 key_info = h->Get(key);
85 } catch (Exception& e) {
86 return e.GetErrorCode();
89 if (key_info->GetType() != BUNDLE_TYPE_STR)
90 return BUNDLE_ERROR_INVALID_PARAMETER;
93 auto& values = key_info->GetValues();
94 auto& value = const_cast<std::unique_ptr<unsigned char[]>&>(values[0]);
95 *str = reinterpret_cast<char*>(&value[0]);
98 return BUNDLE_ERROR_NONE;
101 extern "C" EXPORT_API int bundle_add(bundle* b, const char* key,
103 return bundle_add_str(b, key, val);
106 extern "C" EXPORT_API int bundle_del(bundle* b, const char* key) {
107 if (b == nullptr || key == nullptr || strlen(key) == 0)
108 return BUNDLE_ERROR_INVALID_PARAMETER;
110 auto* h = reinterpret_cast<Bundle*>(b);
113 } catch (Exception& e) {
114 return e.GetErrorCode();
117 return BUNDLE_ERROR_NONE;
120 extern "C" EXPORT_API const char* bundle_get_val(bundle* b, const char* key) {
122 int ret = bundle_get_str(b, key, &val);
123 set_last_result(ret);
127 extern "C" EXPORT_API int bundle_get_count(bundle* b) {
131 auto* h = reinterpret_cast<Bundle*>(b);
132 set_last_result(BUNDLE_ERROR_NONE);
136 extern "C" EXPORT_API void bundle_iterate(bundle* b,
137 bundle_iterate_cb_t callback, void* user_data) {
138 if (b == nullptr || callback == nullptr) {
139 set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
143 auto* h = reinterpret_cast<Bundle*>(b);
144 for (const auto& kv : h->GetMap()) {
145 auto& key_info = kv.second;
146 auto& values = key_info->GetValues();
147 auto& value = const_cast<std::unique_ptr<unsigned char[]>&>(values[0]);
148 auto* val = reinterpret_cast<char*>(&value[0]);
149 callback(key_info->GetKey().c_str(), val, user_data);
152 set_last_result(BUNDLE_ERROR_NONE);
155 extern "C" EXPORT_API void bundle_foreach(bundle* b,
156 bundle_iterator_t callback, void* user_data) {
157 if (b == nullptr || callback == nullptr) {
158 set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
162 auto* h = reinterpret_cast<Bundle*>(b);
163 for (const auto& kv : h->GetMap()) {
164 auto& key_info = kv.second;
165 callback(key_info->GetKey().c_str(), key_info->GetType(),
166 reinterpret_cast<bundle_keyval_t*>(key_info.get()), user_data);
169 set_last_result(BUNDLE_ERROR_NONE);
172 extern "C" EXPORT_API int bundle_keyval_get_type(bundle_keyval_t* kv) {
174 set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
178 auto* key_info = reinterpret_cast<KeyInfo*>(kv);
179 set_last_result(BUNDLE_ERROR_NONE);
180 return key_info->GetType();
183 extern "C" EXPORT_API int bundle_keyval_type_is_array(bundle_keyval_t* kv) {
184 int type = bundle_keyval_get_type(kv);
188 set_last_result(BUNDLE_ERROR_NONE);
189 if (type & BUNDLE_TYPE_ARRAY)
195 extern "C" EXPORT_API int bundle_keyval_type_is_measurable(
196 bundle_keyval_t* kv) {
197 int type = bundle_keyval_get_type(kv);
201 set_last_result(BUNDLE_ERROR_NONE);
202 if (type & BUNDLE_TYPE_MEASURABLE)
208 extern "C" EXPORT_API int bundle_keyval_get_basic_val(bundle_keyval_t* kv,
209 void** val, size_t* size) {
211 return BUNDLE_ERROR_INVALID_PARAMETER;
213 auto* key_info = reinterpret_cast<KeyInfo*>(kv);
214 if (key_info->IsArray())
215 return BUNDLE_ERROR_INVALID_PARAMETER;
218 auto& values = key_info->GetValues();
219 auto& value = const_cast<std::unique_ptr<unsigned char[]>&>(values[0]);
220 *val = reinterpret_cast<void*>(&value[0]);
224 auto& values_size = key_info->GetValuesSize();
225 *size = reinterpret_cast<size_t>(values_size[0]);
228 return BUNDLE_ERROR_NONE;
231 extern "C" EXPORT_API int bundle_keyval_get_array_val(bundle_keyval_t* kv,
232 void*** array_val, unsigned int* array_len, size_t** array_item_size) {
234 return BUNDLE_ERROR_INVALID_PARAMETER;
236 auto* key_info = reinterpret_cast<KeyInfo*>(kv);
237 if (!key_info->IsArray())
238 return BUNDLE_ERROR_INVALID_PARAMETER;
240 auto& values = const_cast<std::vector<std::unique_ptr<unsigned char[]>>&>(
241 key_info->GetValues());
244 *array_val = reinterpret_cast<void**>(&values[0]);
247 *array_len = static_cast<unsigned int>(values.size());
249 if (array_item_size) {
250 auto& values_size = const_cast<std::vector<std::size_t>&>(
251 key_info->GetValuesSize());
252 *array_item_size = reinterpret_cast<size_t*>(&values_size[0]);
255 return BUNDLE_ERROR_NONE;
258 extern "C" EXPORT_API bundle_keyval_t* bundle_keyval_dup(
259 const bundle_keyval_t* kv) {
261 set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
265 auto* keyval = const_cast<bundle_keyval_t*>(kv);
266 auto* key_info = reinterpret_cast<KeyInfo*>(keyval);
270 k = new KeyInfo(*key_info);
271 } catch (Exception& e) {
272 set_last_result(e.GetErrorCode());
274 } catch (const std::bad_alloc& ba) {
275 set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY);
279 set_last_result(BUNDLE_ERROR_NONE);
280 return reinterpret_cast<bundle_keyval_t*>(k);
283 extern "C" EXPORT_API int bundle_keyval_free(bundle_keyval_t* kv) {
285 return BUNDLE_ERROR_INVALID_PARAMETER;
287 auto* key_info = reinterpret_cast<KeyInfo*>(kv);
289 return BUNDLE_ERROR_NONE;
292 extern "C" EXPORT_API bundle* bundle_dup(bundle* b_from) {
293 if (b_from == nullptr) {
294 set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
298 auto* h = reinterpret_cast<Bundle*>(b_from);
299 auto* b = new (std::nothrow) Bundle(*h);
301 set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY);
305 set_last_result(BUNDLE_ERROR_NONE);
306 return reinterpret_cast<bundle*>(b);
309 extern "C" EXPORT_API int bundle_encode(bundle *b, bundle_raw** raw, int* len) {
310 if (b == nullptr || raw == nullptr || len == nullptr)
311 return BUNDLE_ERROR_INVALID_PARAMETER;
313 auto* h = reinterpret_cast<Bundle*>(b);
314 *raw = reinterpret_cast<bundle_raw*>(h->Encode());
315 *len = strlen(reinterpret_cast<char*>(*raw));
316 return BUNDLE_ERROR_NONE;
319 extern "C" EXPORT_API int bundle_free_encoded_rawdata(bundle_raw **r) {
320 if (r == nullptr || *r == nullptr)
321 return BUNDLE_ERROR_INVALID_PARAMETER;
325 return BUNDLE_ERROR_NONE;
328 extern "C" EXPORT_API bundle* bundle_decode(const bundle_raw* r,
329 const int data_size) {
331 set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
337 auto* raw = const_cast<bundle_raw*>(r);
338 b = new Bundle(static_cast<unsigned char*>(raw), data_size);
339 } catch (Exception& e) {
340 set_last_result(e.GetErrorCode());
342 } catch (const std::bad_alloc& ba) {
343 set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY);
347 set_last_result(BUNDLE_ERROR_NONE);
348 return reinterpret_cast<bundle*>(b);
351 extern "C" EXPORT_API int bundle_encode_raw(bundle* b, bundle_raw** r,
353 if (b == nullptr || r == nullptr || len == nullptr)
354 return BUNDLE_ERROR_INVALID_PARAMETER;
356 auto* h = reinterpret_cast<Bundle*>(b);
358 *r = reinterpret_cast<bundle_raw*>(h->EncodeRaw(len));
359 } catch (Exception& e) {
360 return e.GetErrorCode();
362 return BUNDLE_ERROR_NONE;
365 extern "C" EXPORT_API bundle* bundle_decode_raw(const bundle_raw* r,
366 const int data_size) {
368 set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
372 bundle* b = bundle_create();
376 auto* h = reinterpret_cast<Bundle*>(b);
377 auto* raw = const_cast<bundle_raw*>(r);
378 int ret = h->DecodeRaw(static_cast<unsigned char*>(raw), data_size);
379 if (ret != BUNDLE_ERROR_NONE) {
381 set_last_result(ret);
385 set_last_result(BUNDLE_ERROR_NONE);
389 extern "C" EXPORT_API int bundle_get_type(bundle* b, const char* key) {
390 if (b == nullptr || key == nullptr) {
391 set_last_result(BUNDLE_ERROR_KEY_NOT_AVAILABLE);
392 return BUNDLE_TYPE_NONE;
395 auto* h = reinterpret_cast<Bundle*>(b);
396 std::shared_ptr<KeyInfo> key_info;
398 key_info = h->Get(key);
399 } catch (Exception& e) {
400 set_last_result(e.GetErrorCode());
401 return BUNDLE_TYPE_NONE;
404 set_last_result(BUNDLE_ERROR_NONE);
405 return key_info->GetType();
408 extern "C" EXPORT_API int bundle_add_str_array(bundle* b, const char* key,
409 const char** str_array, const int len) {
410 if (b == nullptr || key == nullptr || strlen(key) == 0)
411 return BUNDLE_ERROR_INVALID_PARAMETER;
413 auto* h = reinterpret_cast<Bundle*>(b);
414 std::vector<std::vector<unsigned char>> values(len);
416 for (int i = 0; i < len; ++i) {
417 std::vector<unsigned char> value(str_array[i],
418 str_array[i] + (strlen(str_array[i]) + 1));
419 values[i] = std::move(value);
425 key_info = new KeyInfo((BUNDLE_TYPE_STR_ARRAY | BUNDLE_TYPE_ARRAY),
427 } catch (Exception& e) {
428 return e.GetErrorCode();
432 h->Add(std::shared_ptr<KeyInfo>(key_info));
433 } catch (Exception& e) {
434 return e.GetErrorCode();
437 return BUNDLE_ERROR_NONE;
440 extern "C" EXPORT_API const char** bundle_get_str_array(bundle* b,
441 const char* key, int* len) {
442 if (b == nullptr || key == nullptr) {
443 set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
447 auto* h = reinterpret_cast<Bundle*>(b);
448 std::shared_ptr<KeyInfo> key_info;
450 key_info = h->Get(key);
451 } catch (Exception& e) {
452 set_last_result(e.GetErrorCode());
456 if (key_info->GetType() != BUNDLE_TYPE_STR_ARRAY) {
457 set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
461 set_last_result(BUNDLE_ERROR_NONE);
463 auto& raw_values = const_cast<std::vector<std::unique_ptr<unsigned char[]>>&>(
464 key_info->GetValues());
467 *len = static_cast<int>(raw_values.size());
469 if (raw_values.size() == 0)
472 auto** values = reinterpret_cast<unsigned char**>(&raw_values[0]);
473 return const_cast<const char**>(reinterpret_cast<char**>(values));
476 extern "C" EXPORT_API int bundle_add_byte(bundle* b, const char* key,
477 const void* bytes, const size_t size) {
478 if (b == nullptr || key == nullptr || strlen(key) == 0)
479 return BUNDLE_ERROR_INVALID_PARAMETER;
481 auto* h = reinterpret_cast<Bundle*>(b);
482 auto* p = reinterpret_cast<const unsigned char*>(bytes);
483 std::vector<unsigned char> value;
485 std::copy(p, p + size, std::back_inserter(value));
489 key_info = new KeyInfo(BUNDLE_TYPE_BYTE, key, value);
490 } catch (Exception& e) {
491 return e.GetErrorCode();
492 } catch (const std::bad_alloc& ba) {
493 return BUNDLE_ERROR_OUT_OF_MEMORY;
497 h->Add(std::shared_ptr<KeyInfo>(key_info));
498 } catch (Exception& e) {
499 return e.GetErrorCode();
502 return BUNDLE_ERROR_NONE;
505 extern "C" EXPORT_API int bundle_get_byte(bundle* b, const char* key,
506 void** bytes, size_t* size) {
507 if (b == nullptr || key == nullptr)
508 return BUNDLE_ERROR_INVALID_PARAMETER;
510 auto* h = reinterpret_cast<Bundle*>(b);
511 std::shared_ptr<KeyInfo> key_info;
513 key_info = h->Get(key);
514 } catch (Exception& e) {
515 return e.GetErrorCode();
518 if (key_info->GetType() != BUNDLE_TYPE_BYTE)
519 return BUNDLE_ERROR_INVALID_PARAMETER;
522 auto& values = const_cast<std::vector<std::unique_ptr<unsigned char[]>>&>(
523 key_info->GetValues());
524 if (values.size() == 0) {
527 auto& value = values[0];
528 *bytes = reinterpret_cast<void*>(&value[0]);
533 auto& values_size = key_info->GetValuesSize();
534 *size = reinterpret_cast<size_t>(values_size[0]);
537 return BUNDLE_ERROR_NONE;
540 extern "C" EXPORT_API int bundle_export_to_argv(bundle* b, char*** argv) {
541 if (b == nullptr || argv == nullptr) {
542 set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
546 auto* h = reinterpret_cast<Bundle*>(b);
547 std::vector<std::string> exported_vt;
549 exported_vt = h->Export();
550 } catch (Exception& e) {
551 set_last_result(e.GetErrorCode());
555 int argc = exported_vt.size();
556 auto** exported_argv = reinterpret_cast<char**>(
557 calloc(argc + 1, sizeof(char*)));
558 if (exported_argv == nullptr) {
559 set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY);
563 exported_argv[1] = const_cast<char*>(TAG_IMPORT_EXPORT_CHECK);
564 for (unsigned int idx = 2; idx < exported_vt.size(); idx += 2) {
565 exported_argv[idx] = strdup(exported_vt[idx].c_str());
566 if (exported_argv[idx] == nullptr) {
567 set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY);
568 bundle_free_exported_argv(argc, &exported_argv);
572 exported_argv[idx + 1] = strdup(exported_vt[idx + 1].c_str());
573 if (exported_argv[idx + 1] == nullptr) {
574 set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY);
575 bundle_free_exported_argv(argc, &exported_argv);
580 *argv = exported_argv;
581 set_last_result(BUNDLE_ERROR_NONE);
585 extern "C" EXPORT_API int bundle_free_exported_argv(int argc, char*** argv) {
586 if (argc < 2 || !argv || !*argv)
587 return BUNDLE_ERROR_INVALID_PARAMETER;
589 for (int i = 2; i < argc; i++)
590 std::free((*argv)[i]);
595 return BUNDLE_ERROR_NONE;
598 extern "C" EXPORT_API bundle* bundle_import_from_argv(int argc, char** argv) {
599 if (argv == nullptr) {
600 set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
606 b = new (std::nothrow) Bundle(argc, argv);
607 } catch (Exception& e) {
608 set_last_result(e.GetErrorCode());
612 set_last_result(BUNDLE_ERROR_NONE);
613 return reinterpret_cast<bundle*>(b);
616 extern "C" EXPORT_API int bundle_compare(bundle* b1, bundle* b2) {
617 if (b1 == nullptr || b2 == nullptr)
620 auto* h1 = reinterpret_cast<Bundle*>(b1);
621 auto* h2 = reinterpret_cast<Bundle*>(b2);
628 extern "C" EXPORT_API int bundle_set_str_array_element(bundle* b,
629 const char* key, const unsigned int idx, const char* val) {
630 if (b == nullptr || key == nullptr || val == nullptr)
631 return BUNDLE_ERROR_INVALID_PARAMETER;
633 auto len = strlen(val) + 1;
634 auto* p = reinterpret_cast<unsigned char*>(const_cast<char*>(val));
635 std::vector<unsigned char> value(p, p + len);
637 auto* h = reinterpret_cast<Bundle*>(b);
639 h->Set(key, idx, value);
640 } catch (Exception& e) {
641 return e.GetErrorCode();
644 return BUNDLE_ERROR_NONE;
647 extern "C" EXPORT_API int bundle_add_byte_array(bundle* b,
648 const char* key, const unsigned int len) {
649 return bundle_init_byte_array(b, key, len);
652 extern "C" EXPORT_API int bundle_init_byte_array(bundle* b,
653 const char* key, const unsigned int len) {
654 if (b == nullptr || key == nullptr || strlen(key) == 0)
655 return BUNDLE_ERROR_INVALID_PARAMETER;
657 auto* h = reinterpret_cast<Bundle*>(b);
661 std::vector<std::vector<unsigned char>> values(len);
662 key_info = new KeyInfo(Bundle::Type::ByteArray, key, values);
663 } catch (Exception& e) {
664 return e.GetErrorCode();
665 } catch (const std::bad_alloc& ba) {
666 return BUNDLE_ERROR_OUT_OF_MEMORY;
670 h->Add(std::shared_ptr<KeyInfo>(key_info));
671 } catch (Exception& e) {
672 return e.GetErrorCode();
675 return BUNDLE_ERROR_NONE;
678 extern "C" EXPORT_API int bundle_get_byte_array(bundle* b,
679 const char* key, void*** bytes_array, unsigned int* len,
680 unsigned int** array_element_size) {
681 if (b == nullptr || key == nullptr)
682 return BUNDLE_ERROR_INVALID_PARAMETER;
684 auto* h = reinterpret_cast<Bundle*>(b);
685 std::shared_ptr<KeyInfo> key_info;
687 key_info = h->Get(key);
688 } catch (Exception& e) {
689 return e.GetErrorCode();
692 if (key_info->GetType() != BUNDLE_TYPE_BYTE_ARRAY)
693 return BUNDLE_ERROR_INVALID_PARAMETER;
695 auto& raw_values = const_cast<std::vector<std::unique_ptr<unsigned char[]>>&>(
696 key_info->GetValues());
699 auto** values = reinterpret_cast<unsigned char**>(&raw_values[0]);
700 *bytes_array = reinterpret_cast<void**>(values);
704 *len = static_cast<unsigned int>(raw_values.size());
706 if (array_element_size) {
707 auto& raw_values_size = const_cast<std::vector<unsigned int>&>(
708 key_info->GetUValuesSize());
709 *array_element_size = reinterpret_cast<unsigned int*>(&raw_values_size[0]);
712 return BUNDLE_ERROR_NONE;
715 extern "C" EXPORT_API int bundle_set_byte_array_element(bundle* b,
716 const char* key, const unsigned int idx,
717 const void* bytes, const size_t size) {
718 if (b == nullptr || key == nullptr || size <= 0)
719 return BUNDLE_ERROR_INVALID_PARAMETER;
721 std::vector<unsigned char> value;
723 auto* p = reinterpret_cast<unsigned char*>(const_cast<void*>(bytes));
724 std::copy(p, p + size, std::back_inserter(value));
727 auto* h = reinterpret_cast<Bundle*>(b);
729 h->Set(key, idx, value);
730 } catch (Exception& e) {
731 return e.GetErrorCode();
734 return BUNDLE_ERROR_NONE;
737 extern "C" EXPORT_API int bundle_to_json(bundle* b, char** json) {
738 if (b == nullptr || json == nullptr)
739 return BUNDLE_ERROR_INVALID_PARAMETER;
741 auto* h = reinterpret_cast<Bundle*>(b);
744 *json = strdup(js.ToString().c_str());
745 } catch (Exception& e) {
746 return e.GetErrorCode();
749 return BUNDLE_ERROR_NONE;
752 extern "C" EXPORT_API int bundle_from_json(const char* json, bundle** b) {
753 if (json == nullptr || b == nullptr)
754 return BUNDLE_ERROR_INVALID_PARAMETER;
758 *b = reinterpret_cast<bundle*>(js.ToBundle());
759 } catch (Exception& e) {
760 return e.GetErrorCode();
763 return BUNDLE_ERROR_NONE;