2 * Copyright (c) 2000 - 2016 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.
26 #include "bundle_internal.h"
28 #include "keyval_array.h"
29 #include "keyval_type.h"
30 #include "bundle_log.h"
32 #define CHECKSUM_LENGTH 32
33 #define TAG_IMPORT_EXPORT_CHECK "`zaybxcwdveuftgsh`"
41 * Find a kv from bundle
43 static keyval_t *_bundle_find_kv(bundle *b, const char *key)
48 set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
53 set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
59 if (strcmp(key, kv->key) == 0) {
60 set_last_result(BUNDLE_ERROR_NONE);
66 set_last_result(BUNDLE_ERROR_KEY_NOT_AVAILABLE);
71 * Append kv into bundle
73 static int _bundle_append_kv(bundle *b, keyval_t *new_kv)
77 if (b->kv_head == NULL) {
86 return BUNDLE_ERROR_NONE;
89 static int _bundle_add_kv(bundle *b, const char *key, const void *val,
90 const size_t size, const int type, const unsigned int len)
96 /* basic value check */
98 return BUNDLE_ERROR_INVALID_PARAMETER;
100 return BUNDLE_ERROR_INVALID_PARAMETER;
101 if (strlen(key) == 0)
102 return BUNDLE_ERROR_INVALID_PARAMETER;
104 kv = _bundle_find_kv(b, key);
105 if (kv) /* Key already exists */
106 return BUNDLE_ERROR_KEY_EXISTS;
108 if (keyval_type_is_array(type)) {
110 kva = keyval_array_new(NULL, key, type,
111 (const void **) val, len);
112 new_kv = (keyval_t *)kva;
115 new_kv = keyval_new(NULL, key, type, val, size);
119 return BUNDLE_ERROR_OUT_OF_MEMORY;
121 _bundle_append_kv(b, new_kv);
123 return BUNDLE_ERROR_NONE;
126 static int _bundle_get_val(bundle *b, const char *key, const int type,
127 void **val, size_t *size, unsigned int *len,
128 size_t **array_element_size)
133 kv = _bundle_find_kv(b, key);
134 if (!kv) /* Key doesn't exist */
135 return get_last_result();
137 if (BUNDLE_TYPE_ANY != type && type != kv->type)
138 return BUNDLE_ERROR_INVALID_PARAMETER;
140 if (keyval_type_is_array(type)) {
141 kva = (keyval_array_t *)kv;
142 keyval_array_get_data(kva, NULL, (void ***)val,
143 len, array_element_size);
145 keyval_get_data(kv, NULL, val, size);
148 return BUNDLE_ERROR_NONE;
152 * global initialization
155 static void _bundle_global_init(void)
162 /* Run init functions */
170 bundle *bundle_create(void)
174 _bundle_global_init();
176 b = calloc(1, sizeof(bundle)); /* fill mem with NULL */
178 BUNDLE_EXCEPTION_PRINT(
179 "Unable to allocate memory for bundle\n");
180 set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY);
184 set_last_result(BUNDLE_ERROR_NONE);
191 int bundle_free(bundle *b)
197 BUNDLE_EXCEPTION_PRINT("Bundle is already freed\n");
198 return BUNDLE_ERROR_INVALID_PARAMETER;
201 /* Free keyval list */
206 tmp_kv->method->free(tmp_kv, 1);
212 return BUNDLE_ERROR_NONE;
215 int bundle_add_str(bundle *b, const char *key, const char *str)
218 return BUNDLE_ERROR_INVALID_PARAMETER;
219 return _bundle_add_kv(b, key, str, strlen(str) + 1, BUNDLE_TYPE_STR, 1);
222 int bundle_get_str(bundle *b, const char *key, char **str)
224 return _bundle_get_val(b, key, BUNDLE_TYPE_STR, (void **)str,
228 int bundle_add(bundle *b, const char *key, const char *val)
230 return bundle_add_str(b, key, val);
233 int bundle_del(bundle *b, const char *key)
236 keyval_t *prev_kv = NULL;
238 /* basic value check */
240 return BUNDLE_ERROR_INVALID_PARAMETER;
242 return BUNDLE_ERROR_INVALID_PARAMETER;
243 if (strlen(key) == 0)
244 return BUNDLE_ERROR_INVALID_PARAMETER;
248 if (strcmp(key, kv->key) == 0)
255 return BUNDLE_ERROR_KEY_NOT_AVAILABLE;
258 prev_kv->next = kv->next;
259 if (kv == b->kv_head)
260 b->kv_head = kv->next;
261 kv->method->free(kv, 1);
264 return BUNDLE_ERROR_NONE;
267 const char *bundle_get_val(bundle *b, const char *key)
270 int ret = BUNDLE_ERROR_NONE;
272 ret = bundle_get_str(b, key, &val);
273 set_last_result(ret);
279 * @brief used by bundle_get_count() API, to count number of items in a bundle
281 static void _bundle_get_count_iter(const char *k, const int type,
282 const bundle_keyval_t *kv, void *user_data)
284 int *count = (int *)user_data;
288 int bundle_get_count(bundle *b)
294 bundle_foreach(b, _bundle_get_count_iter, &count);
298 void bundle_iterate(bundle *b, bundle_iterate_cb_t callback, void *data)
302 if (b == NULL || callback == NULL) {
303 set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
309 callback(kv->key, kv->val, data);
313 set_last_result(BUNDLE_ERROR_NONE);
316 void bundle_foreach(bundle *b, bundle_iterator_t iter, void *user_data)
320 if (b == NULL || iter == NULL) {
321 set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
322 return; /* TC_FIX if b = NULL- error handling */
327 iter(kv->key, kv->type, kv, user_data);
331 set_last_result(BUNDLE_ERROR_NONE);
334 /* keyval functions */
335 int bundle_keyval_get_type(bundle_keyval_t *kv)
338 set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
341 set_last_result(BUNDLE_ERROR_NONE);
345 int bundle_keyval_type_is_array(bundle_keyval_t *kv)
348 set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
351 set_last_result(BUNDLE_ERROR_NONE);
352 return keyval_type_is_array(kv->type);
355 int bundle_keyval_type_is_measurable(bundle_keyval_t *kv)
358 set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
361 set_last_result(BUNDLE_ERROR_NONE);
362 return keyval_type_is_measurable(kv->type);
365 int bundle_keyval_get_basic_val(bundle_keyval_t *kv, void **val, size_t *size)
367 return keyval_get_data(kv, NULL, val, size);
370 int bundle_keyval_get_array_val(bundle_keyval_t *kv, void ***array_val,
371 unsigned int *array_len, size_t **array_item_size)
373 return keyval_array_get_data((keyval_array_t *)kv, NULL,
374 array_val, array_len, array_item_size);
377 bundle *bundle_dup(bundle *b_from)
382 keyval_array_t *kva_from;
385 if (b_from == NULL) {
386 set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
390 b_to = bundle_create();
394 kv_from = b_from->kv_head;
395 while (kv_from != NULL) {
396 if (keyval_type_is_array(kv_from->type)) {
397 kva_from = (keyval_array_t *)kv_from;
398 kv_to = (keyval_t *)keyval_array_new(NULL, kv_from->key,
399 kv_from->type, NULL, kva_from->len);
403 for (i = 0; i < kva_from->len; i++) {
404 if (((keyval_array_t *)kv_from)->array_val[i]) {
405 keyval_array_set_element(
406 (keyval_array_t *)kv_to, i,
407 ((keyval_array_t *)kv_from)->array_val[i],
408 ((keyval_array_t *)kv_from)->array_element_size[i]);
412 _bundle_append_kv(b_to, kv_to);
414 if (_bundle_add_kv(b_to, kv_from->key,
415 kv_from->val, kv_from->size, kv_from->type, 0))
419 kv_from = kv_from->next;
429 int bundle_encode(bundle *b, bundle_raw **r, int *len)
439 if (b == NULL || r == NULL || len == NULL)
440 return BUNDLE_ERROR_INVALID_PARAMETER;
442 /* calculate memory size */
445 msize += kv->method->get_encoded_size(kv);
449 m = calloc(msize + CHECKSUM_LENGTH, sizeof(unsigned char));
450 if (unlikely(m == NULL))
451 return BUNDLE_ERROR_OUT_OF_MEMORY;
453 p_m = m + CHECKSUM_LENGTH; /* temporary pointer */
460 kv->method->encode(kv, &byte, &byte_len);
461 memcpy(p_m, byte, byte_len);
469 /* compute checksum from the data */
470 chksum_val = g_compute_checksum_for_string(G_CHECKSUM_MD5,
471 (const char *)(m + CHECKSUM_LENGTH), msize);
472 /* prefix checksum to the data */
473 memcpy(m, chksum_val, CHECKSUM_LENGTH);
475 /* base64 encode for whole string checksum and data */
476 *r = (unsigned char *)g_base64_encode(m, msize + CHECKSUM_LENGTH);
478 *len = strlen((char *)*r);
481 g_free(chksum_val); /* free checksum string */
483 return BUNDLE_ERROR_NONE;
486 int bundle_free_encoded_rawdata(bundle_raw **r)
489 return BUNDLE_ERROR_INVALID_PARAMETER; /* TC_FIX - double free sigabrt handling */
493 return BUNDLE_ERROR_NONE;
496 bundle *bundle_decode(const bundle_raw *r, const int data_size)
500 unsigned char *d_str;
503 unsigned int d_len_raw;
507 gchar *compute_cksum;
513 set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
517 extract_cksum = calloc(CHECKSUM_LENGTH + 1, sizeof(char));
518 if (unlikely(extract_cksum == NULL)) {
519 set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
524 * base 64 decode of input string
525 * Since base64 encodes 3 bytes in 4 chars (+3 may be needed in case of non-zero state)
526 * refer to: https://developer.gnome.org/glib/stable/glib-Base64-Encoding.html#g-base64-decode-step
528 d_str = malloc((data_size / 4) * 3 + 3);
529 if (unlikely(d_str == NULL)) {
530 set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY);
535 d_len_raw = g_base64_decode_step((char *)r, data_size, d_str, &state, &save);
536 if (d_len_raw < CHECKSUM_LENGTH) {
537 set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
543 /* extract checksum from the received string */
544 strncpy(extract_cksum, (const char *)d_str, CHECKSUM_LENGTH);
545 /* compute checksum for the data */
546 compute_cksum = g_compute_checksum_for_string(G_CHECKSUM_MD5,
547 (const char *)(d_str + CHECKSUM_LENGTH),
548 d_len_raw - CHECKSUM_LENGTH);
550 * compare checksum values- extracted from the received
551 * string and computed from the data
553 if (strcmp(extract_cksum, compute_cksum) != 0) {
556 g_free(compute_cksum);
559 d_r = d_str + CHECKSUM_LENGTH;
560 d_len = d_len_raw - CHECKSUM_LENGTH;
562 /* re-construct bundle */
567 g_free(compute_cksum);
571 p_r = (bundle_raw *)d_r;
572 while (p_r < d_r + d_len - 1) {
573 kv = NULL; /* To get a new kv */
575 /* Find type, and use decode function according to type */
576 type = keyval_get_type_from_encoded_byte(p_r);
577 if (keyval_type_is_array(type))
578 bytes_read = keyval_array_decode(p_r,
579 (keyval_array_t **) &kv);
581 bytes_read = keyval_decode(p_r, &kv);
584 _bundle_append_kv(b, kv);
591 g_free(compute_cksum);
594 set_last_result(BUNDLE_ERROR_NONE);
604 int bundle_encode_raw(bundle *b, bundle_raw **r, int *len)
607 unsigned char *m = NULL;
608 unsigned char *p_m = NULL;
609 unsigned char *byte = NULL;
611 gchar *chksum_val = NULL;
614 if (b == NULL || r == NULL)
615 return BUNDLE_ERROR_INVALID_PARAMETER;
617 /* calculate memory size */
620 msize += kv->method->get_encoded_size(kv);
624 m = calloc(msize+CHECKSUM_LENGTH, sizeof(unsigned char));
625 if (unlikely(m == NULL))
626 return BUNDLE_ERROR_OUT_OF_MEMORY;
628 p_m = m + CHECKSUM_LENGTH; /* temporary pointer */
635 kv->method->encode(kv, &byte, &byte_len);
636 memcpy(p_m, byte, byte_len);
644 /* compute checksum from the data */
645 chksum_val = g_compute_checksum_for_string(G_CHECKSUM_MD5,
646 (const char *)(m + CHECKSUM_LENGTH), msize);
647 /* prefix checksum to the data */
648 memcpy(m, chksum_val, CHECKSUM_LENGTH);
651 *len = msize + CHECKSUM_LENGTH;
652 g_free(chksum_val); /* free checksum string */
654 return BUNDLE_ERROR_NONE;
657 bundle *bundle_decode_raw(const bundle_raw *r, const int data_size)
660 bundle_raw *p_r = NULL;
661 unsigned char *d_str = NULL;
662 unsigned int d_len_raw;
663 unsigned char *d_r = NULL;
666 gchar *compute_cksum = NULL;
672 set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
676 extract_cksum = calloc(CHECKSUM_LENGTH + 1, sizeof(char));
677 if (unlikely(extract_cksum == NULL)) {
678 set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY);
682 /* base 64 decode of input string*/
683 /* d_str = g_base64_decode((char*)r, &d_len_raw); */
684 d_str = (unsigned char *)r;
685 d_len_raw = data_size;
686 /* extract checksum from the received string */
687 strncpy(extract_cksum, (const char *)d_str, CHECKSUM_LENGTH);
688 /* compute checksum for the data */
689 compute_cksum = g_compute_checksum_for_string(G_CHECKSUM_MD5,
690 (const char *)(d_str + CHECKSUM_LENGTH),
691 d_len_raw - CHECKSUM_LENGTH);
693 * compare checksum values- extracted from the received
694 * string and computed from the data
696 if (strcmp(extract_cksum, compute_cksum) != 0) {
698 g_free(compute_cksum);
699 set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
702 d_r = d_str + CHECKSUM_LENGTH;
703 d_len = d_len_raw - CHECKSUM_LENGTH;
705 /* re-construct bundle */
709 g_free(compute_cksum);
713 p_r = (bundle_raw *)d_r;
714 while (p_r < d_r + d_len - 1) {
715 kv = NULL; /* To get a new kv */
717 /* Find type, and use decode function according to type */
718 type = keyval_get_type_from_encoded_byte(p_r);
719 if (keyval_type_is_array(type))
720 bytes_read = keyval_array_decode(p_r,
721 (keyval_array_t **) &kv);
723 bytes_read = keyval_decode(p_r, &kv);
726 _bundle_append_kv(b, kv);
733 g_free(compute_cksum);
734 set_last_result(BUNDLE_ERROR_NONE);
739 void _iter_export_to_argv(const char *key, const int type, const keyval_t *kv,
742 struct _argv_idx *vi = (struct _argv_idx *)user_data;
743 unsigned char *byte = NULL;
744 unsigned char *encoded_byte = NULL;
747 vi->argv[vi->idx] = strdup(key);
748 if (kv->method->encode((struct keyval_t *)kv, &byte, &byte_len) == 0) {
749 /* TODO: encode FAILED! */
750 BUNDLE_EXCEPTION_PRINT("bundle: FAILED to encode keyval: %s\n",
755 encoded_byte = (unsigned char *)g_base64_encode(byte, byte_len);
756 if (encoded_byte == NULL) {
757 BUNDLE_EXCEPTION_PRINT("bundle: failed to encode byte\n");
761 vi->argv[vi->idx + 1] = (char *)encoded_byte;
767 int bundle_export_to_argv(bundle *b, char ***argv)
773 if (b == NULL || argv == NULL) {
774 set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
778 item_count = bundle_get_count(b);
779 argc = 2 * item_count + 2;
780 /* 2 more count for argv[0] and arv[1] = encoded */
781 *argv = calloc(argc + 1, sizeof(char *));
783 set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY);
789 vi.idx = 2; /* start from index 2*/
790 vi.argv[1] = TAG_IMPORT_EXPORT_CHECK; /* set argv[1] as encoded*/
791 /* BUNDLE_LOG_PRINT("\nargument 1 is %s",vi.argv[1]); */
793 bundle_foreach(b, _iter_export_to_argv, &vi);
795 set_last_result(BUNDLE_ERROR_NONE);
799 int bundle_free_exported_argv(int argc, char ***argv)
804 return BUNDLE_ERROR_INVALID_PARAMETER;
806 if (!*argv || argc < 2)
807 return BUNDLE_ERROR_INVALID_PARAMETER;
809 for (i = 2; i < argc; i++)
814 return BUNDLE_ERROR_NONE;
817 bundle *bundle_import_from_argv(int argc, char **argv)
825 unsigned int byte_size;
829 set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
830 return NULL; /* TC_FIX error handling for argv =NULL*/
835 set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY);
840 set_last_result(BUNDLE_ERROR_NONE);
844 if (!argv[1] || strcmp(argv[1], TAG_IMPORT_EXPORT_CHECK)) {
845 /* start idx from one as argv[1] is user given argument */
846 for (idx = 1; idx + 1 < argc; idx = idx + 2)
847 bundle_add(b, argv[idx], argv[idx + 1]);
852 /* start idx from 2 as argv[1] is encoded */
853 for (idx = 2; idx + 1 < argc; idx = idx + 2) {
857 encoded_byte = argv[idx+1];
860 byte = g_base64_decode(encoded_byte, (gsize *)&byte_size);
863 set_last_result(bundle_free(b));
867 type = keyval_get_type_from_encoded_byte(byte);
868 if (keyval_type_is_array(type)) {
869 if (keyval_array_decode(byte, &kva) == 0) /* TODO: error! */
870 BUNDLE_EXCEPTION_PRINT("Unable to Decode array\n");
871 kv = (keyval_t *)kva;
873 if (keyval_decode(byte, &kv) == 0) /* TODO: error! */
874 BUNDLE_EXCEPTION_PRINT("Unable to Decode\n");
876 _bundle_append_kv(b, kv);
882 set_last_result(BUNDLE_ERROR_NONE);
886 int bundle_get_type(bundle *b, const char *key)
888 keyval_t *kv = _bundle_find_kv(b, key);
893 set_last_result(BUNDLE_ERROR_KEY_NOT_AVAILABLE);
894 return BUNDLE_TYPE_NONE;
899 * Get length of an array
901 /* LCOV_EXCL_START */
902 unsigned int bundle_get_array_len(bundle *b, const char *key)
904 return BUNDLE_ERROR_NONE;
909 * Get size of an item in byte, of given pointer
911 /* LCOV_EXCL_START */
912 size_t bundle_get_array_val_size(bundle *b, const char *key,
915 return BUNDLE_ERROR_NONE;
919 static int bundle_set_array_val(bundle *b, const char *key, const int type,
920 const unsigned int idx, const void *val, const size_t size)
925 kv = _bundle_find_kv(b, key);
927 return get_last_result();
929 if (type != kv->type)
930 return BUNDLE_ERROR_INVALID_PARAMETER;
932 /* TODO: Is this needed? */
933 if (!keyval_type_is_array(kv->type))
934 return BUNDLE_ERROR_INVALID_PARAMETER;
936 kva = (keyval_array_t *)kv;
937 if (!keyval_array_is_idx_valid(kva, idx))
938 return BUNDLE_ERROR_INVALID_PARAMETER;
940 /* NULL value test (TODO: is this needed?) */
942 return BUNDLE_ERROR_INVALID_PARAMETER;
944 return keyval_array_set_element(kva, idx, (void *)val, size);
947 int bundle_add_str_array(bundle *b, const char *key, const char **str_array,
950 return _bundle_add_kv(b, key, str_array, 0, BUNDLE_TYPE_STR_ARRAY, len);
953 /* LCOV_EXCL_START */
954 int bundle_get_val_array(bundle *b, const char *key, char ***str_array,
957 return _bundle_get_val(b, key, BUNDLE_TYPE_STR_ARRAY,
958 (void **)str_array, NULL, (unsigned int *)len, NULL);
962 const char **bundle_get_str_array(bundle *b, const char *key, int *len)
964 int ret = BUNDLE_ERROR_NONE;
965 const char **arr_val = NULL;
967 ret = bundle_get_val_array(b, key, (char ***)&arr_val, len);
968 set_last_result(ret);
973 /* LCOV_EXCL_START */
974 int bundle_compare(bundle *b1, bundle *b2)
982 if (bundle_get_count(b1) != bundle_get_count(b2))
985 for (kv1 = b1->kv_head; kv1 != NULL; kv1 = kv1->next) {
986 kv2 = _bundle_find_kv(b2, kv1->key);
989 if (kv1->method->compare(kv1, kv2))
997 int bundle_set_str_array_element(bundle *b, const char *key,
998 const unsigned int idx, const char *val)
1001 return BUNDLE_ERROR_INVALID_PARAMETER;
1003 return bundle_set_array_val(b, key, BUNDLE_TYPE_STR_ARRAY,
1004 idx, val, strlen(val) + 1);
1007 int bundle_add_byte(bundle *b, const char *key, const void *byte,
1010 return _bundle_add_kv(b, key, byte, size, BUNDLE_TYPE_BYTE, 1);
1013 int bundle_get_byte(bundle *b, const char *key, void **byte, size_t *size)
1015 return _bundle_get_val(b, key, BUNDLE_TYPE_BYTE, (void **)byte,
1019 int bundle_add_byte_array(bundle *b, const char *key, void **byte_array,
1020 const unsigned int len)
1022 return _bundle_add_kv(b, key, byte_array, 0,
1023 BUNDLE_TYPE_BYTE_ARRAY, len);
1026 int bundle_get_byte_array(bundle *b, const char *key, void ***byte_array,
1027 unsigned int *len, unsigned int **array_element_size)
1029 return _bundle_get_val(b, key, BUNDLE_TYPE_BYTE_ARRAY,
1030 (void **)byte_array, NULL, len, (size_t **)array_element_size);
1033 int bundle_set_byte_array_element(bundle *b, const char *key,
1034 const unsigned int idx, const void *val, const size_t size)
1036 return bundle_set_array_val(b, key, BUNDLE_TYPE_BYTE_ARRAY,