c49e6ed1a91ff17df4aebecd9a8c1ab381011af9
[platform/core/base/bundle.git] / src / bundle.c
1 /*
2  * Copyright (c) 2000 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /**
18  * bundle.c
19  */
20
21 #include <stdlib.h>
22 #include <string.h>
23 #include <glib.h>
24
25 #include "bundle.h"
26 #include "bundle_internal.h"
27 #include "keyval.h"
28 #include "keyval_array.h"
29 #include "keyval_type.h"
30 #include "bundle_log.h"
31
32 #define CHECKSUM_LENGTH 32
33 #define TAG_IMPORT_EXPORT_CHECK "`zaybxcwdveuftgsh`"
34
35 /* ADT */
36 struct _bundle_t {
37         keyval_t *kv_head;
38 };
39
40 /**
41  * Find a kv from bundle
42  */
43 static keyval_t *_bundle_find_kv(bundle *b, const char *key)
44 {
45         keyval_t *kv;
46
47         if (b == NULL) {
48                 set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
49                 return NULL;
50         }
51
52         if (key == NULL) {
53                 set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
54                 return NULL;
55         }
56
57         kv = b->kv_head;
58         while (kv != NULL) {
59                 if (strcmp(key, kv->key) == 0) {
60                         set_last_result(BUNDLE_ERROR_NONE);
61                         return kv;
62                 }
63                 kv = kv->next;
64         }
65         /* Not found */
66         set_last_result(BUNDLE_ERROR_KEY_NOT_AVAILABLE);
67         return NULL;
68 }
69
70 /**
71  * Append kv into bundle
72  */
73 static int _bundle_append_kv(bundle *b, keyval_t *new_kv)
74 {
75         keyval_t *kv;
76
77         if (b->kv_head == NULL) {
78                 b->kv_head = new_kv;
79         } else {
80                 kv = b->kv_head;
81                 while (kv->next)
82                         kv = kv->next;
83                 kv->next = new_kv;
84         }
85
86         return BUNDLE_ERROR_NONE;
87 }
88
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)
91 {
92         keyval_t *kv;
93         keyval_t *new_kv;
94         keyval_array_t *kva;
95
96         /* basic value check */
97         if (b == NULL)
98                 return BUNDLE_ERROR_INVALID_PARAMETER;
99         if (key == NULL)
100                 return BUNDLE_ERROR_INVALID_PARAMETER;
101         if (strlen(key) == 0)
102                 return BUNDLE_ERROR_INVALID_PARAMETER;
103
104         kv = _bundle_find_kv(b, key);
105         if (kv) /* Key already exists */
106                 return BUNDLE_ERROR_KEY_EXISTS;
107
108         if (keyval_type_is_array(type)) {
109                 /* array type */
110                 kva = keyval_array_new(NULL, key, type,
111                                 (const void **) val, len);
112                 new_kv = (keyval_t *)kva;
113         } else {
114                 /* normal type */
115                 new_kv = keyval_new(NULL, key, type, val, size);
116         }
117
118         if (!new_kv)
119                 return BUNDLE_ERROR_OUT_OF_MEMORY;
120
121         _bundle_append_kv(b, new_kv);
122
123         return BUNDLE_ERROR_NONE;
124 }
125
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)
129 {
130         keyval_t *kv;
131         keyval_array_t *kva;
132
133         kv = _bundle_find_kv(b, key);
134         if (!kv) /* Key doesn't exist */
135                 return get_last_result();
136
137         if (BUNDLE_TYPE_ANY != type && type != kv->type)
138                 return BUNDLE_ERROR_INVALID_PARAMETER;
139
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);
144         } else {
145                 keyval_get_data(kv, NULL, val, size);
146         }
147
148         return BUNDLE_ERROR_NONE;
149 }
150
151 /**
152  * global initialization
153  * Run only once.
154  */
155 static void _bundle_global_init(void)
156 {
157         static int _is_done;
158
159         if (_is_done)
160                 return;
161
162         /* Run init functions */
163         keyval_type_init();
164
165         _is_done = 1;
166         return;
167 }
168
169 /* APIs */
170 bundle *bundle_create(void)
171 {
172         bundle *b = NULL;
173
174         _bundle_global_init();
175
176         b = calloc(1, sizeof(bundle));  /* fill mem with NULL */
177         if (b == NULL) {
178                 BUNDLE_EXCEPTION_PRINT(
179                                 "Unable to allocate memory for bundle\n");
180                 set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY);
181                 goto exception;
182         }
183
184         set_last_result(BUNDLE_ERROR_NONE);
185         return b;
186
187 exception:
188         return NULL;
189 }
190
191 int bundle_free(bundle *b)
192 {
193         keyval_t *kv;
194         keyval_t *tmp_kv;
195
196         if (b == NULL) {
197                 BUNDLE_EXCEPTION_PRINT("Bundle is already freed\n");
198                 return BUNDLE_ERROR_INVALID_PARAMETER;
199         }
200
201         /* Free keyval list */
202         kv = b->kv_head;
203         while (kv) {
204                 tmp_kv = kv;
205                 kv = kv->next;
206                 tmp_kv->method->free(tmp_kv, 1);
207         }
208
209         /* free bundle */
210         free(b);
211
212         return BUNDLE_ERROR_NONE;
213 }
214
215 int bundle_add_str(bundle *b, const char *key, const char *str)
216 {
217         if (!str)
218                 return BUNDLE_ERROR_INVALID_PARAMETER;
219         return _bundle_add_kv(b, key, str, strlen(str) + 1, BUNDLE_TYPE_STR, 1);
220 }
221
222 int bundle_get_str(bundle *b, const char *key, char **str)
223 {
224         return _bundle_get_val(b, key, BUNDLE_TYPE_STR, (void **)str,
225                         NULL, NULL, NULL);
226 }
227
228 int bundle_add(bundle *b, const char *key, const char *val)
229 {
230         return bundle_add_str(b, key, val);
231 }
232
233 int bundle_del(bundle *b, const char *key)
234 {
235         keyval_t *kv;
236         keyval_t *prev_kv = NULL;
237
238         /* basic value check */
239         if (b == NULL)
240                 return BUNDLE_ERROR_INVALID_PARAMETER;
241         if (key == NULL)
242                 return BUNDLE_ERROR_INVALID_PARAMETER;
243         if (strlen(key) == 0)
244                 return BUNDLE_ERROR_INVALID_PARAMETER;
245
246         kv = b->kv_head;
247         while (kv) {
248                 if (strcmp(key, kv->key) == 0)
249                         break;
250                 prev_kv = kv;
251                 kv = kv->next;
252         }
253
254         if (kv == NULL) {
255                 return BUNDLE_ERROR_KEY_NOT_AVAILABLE;
256         } else {
257                 if (prev_kv)
258                         prev_kv->next = kv->next;
259                 if (kv == b->kv_head)
260                         b->kv_head = kv->next;
261                 kv->method->free(kv, 1);
262         }
263
264         return BUNDLE_ERROR_NONE;
265 }
266
267 const char *bundle_get_val(bundle *b, const char *key)
268 {
269         char *val = NULL;
270         int ret = BUNDLE_ERROR_NONE;
271
272         ret = bundle_get_str(b, key, &val);
273         set_last_result(ret);
274
275         return val;
276 }
277
278 /**
279  * @brief used by bundle_get_count() API, to count number of items in a bundle
280  */
281 static void _bundle_get_count_iter(const char *k, const int type,
282                 const bundle_keyval_t *kv, void *user_data)
283 {
284         int *count = (int *)user_data;
285         *count += 1;
286 }
287
288 int bundle_get_count(bundle *b)
289 {
290         int count = 0;
291
292         if (b == NULL)
293                 return count;
294         bundle_foreach(b, _bundle_get_count_iter, &count);
295         return count;
296 }
297
298 void bundle_iterate(bundle *b, bundle_iterate_cb_t callback, void *data)
299 {
300         keyval_t *kv;
301
302         if (b == NULL || callback == NULL) {
303                 set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
304                 return;
305         }
306
307         kv = b->kv_head;
308         while (kv) {
309                 callback(kv->key, kv->val, data);
310                 kv = kv->next;
311         }
312
313         set_last_result(BUNDLE_ERROR_NONE);
314 }
315
316 void bundle_foreach(bundle *b, bundle_iterator_t iter, void *user_data)
317 {
318         keyval_t *kv;
319
320         if (b == NULL || iter == NULL) {
321                 set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
322                 return; /* TC_FIX if b = NULL- error handling */
323         }
324
325         kv = b->kv_head;
326         while (kv) {
327                 iter(kv->key, kv->type, kv, user_data);
328                 kv = kv->next;
329         }
330
331         set_last_result(BUNDLE_ERROR_NONE);
332 }
333
334 /* keyval functions */
335 int bundle_keyval_get_type(bundle_keyval_t *kv)
336 {
337         if (kv == NULL) {
338                 set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
339                 return -1;
340         }
341         set_last_result(BUNDLE_ERROR_NONE);
342         return kv->type;
343 }
344
345 int bundle_keyval_type_is_array(bundle_keyval_t *kv)
346 {
347         if (kv == NULL) {
348                 set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
349                 return -1;
350         }
351         set_last_result(BUNDLE_ERROR_NONE);
352         return keyval_type_is_array(kv->type);
353 }
354
355 int bundle_keyval_type_is_measurable(bundle_keyval_t *kv)
356 {
357         if (kv == NULL) {
358                 set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
359                 return -1;
360         }
361         set_last_result(BUNDLE_ERROR_NONE);
362         return keyval_type_is_measurable(kv->type);
363 }
364
365 int bundle_keyval_get_basic_val(bundle_keyval_t *kv, void **val, size_t *size)
366 {
367         return keyval_get_data(kv, NULL, val, size);
368 }
369
370 int bundle_keyval_get_array_val(bundle_keyval_t *kv, void ***array_val,
371                 unsigned int *array_len, size_t **array_item_size)
372 {
373         return keyval_array_get_data((keyval_array_t *)kv, NULL,
374                         array_val, array_len, array_item_size);
375 }
376
377 bundle *bundle_dup(bundle *b_from)
378 {
379         bundle *b_to;
380         keyval_t *kv_from;
381         keyval_t *kv_to;
382         keyval_array_t *kva_from;
383         int i;
384
385         if (b_from == NULL) {
386                 set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
387                 return NULL;
388         }
389
390         b_to = bundle_create();
391         if (b_to == NULL)
392                 return NULL;
393
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);
400                         if (!kv_to)
401                                 goto err;
402
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]);
409                                 }
410                         }
411
412                         _bundle_append_kv(b_to, kv_to);
413                 } else {
414                         if (_bundle_add_kv(b_to, kv_from->key,
415                                 kv_from->val, kv_from->size, kv_from->type, 0))
416                                 goto err;
417                 }
418
419                 kv_from = kv_from->next;
420         }
421
422         return b_to;
423
424 err:
425         bundle_free(b_to);
426         return NULL;
427 }
428
429 int bundle_encode(bundle *b, bundle_raw **r, int *len)
430 {
431         keyval_t *kv;
432         unsigned char *m;
433         unsigned char *p_m;
434         unsigned char *byte;
435         size_t byte_len;
436         gchar *chksum_val;
437         size_t msize = 0;
438
439         if (b == NULL || r == NULL || len == NULL)
440                 return BUNDLE_ERROR_INVALID_PARAMETER;
441
442         /* calculate memory size */
443         kv = b->kv_head;
444         while (kv != NULL) {
445                 msize += kv->method->get_encoded_size(kv);
446                 kv = kv->next;
447         }
448
449         m = calloc(msize + CHECKSUM_LENGTH, sizeof(unsigned char));
450         if (unlikely(m == NULL))
451                 return BUNDLE_ERROR_OUT_OF_MEMORY;
452
453         p_m = m + CHECKSUM_LENGTH; /* temporary pointer */
454
455         kv = b->kv_head;
456         while (kv != NULL) {
457                 byte = NULL;
458                 byte_len = 0;
459
460                 kv->method->encode(kv, &byte, &byte_len);
461                 memcpy(p_m, byte, byte_len);
462
463                 p_m += byte_len;
464                 kv = kv->next;
465
466                 free(byte);
467         }
468
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);
474         if (r) {
475                 /* base64 encode for whole string checksum and data */
476                 *r = (unsigned char *)g_base64_encode(m, msize + CHECKSUM_LENGTH);
477                 if (len)
478                         *len = strlen((char *)*r);
479         }
480         free(m);
481         g_free(chksum_val); /* free checksum string */
482
483         return BUNDLE_ERROR_NONE;
484 }
485
486 int bundle_free_encoded_rawdata(bundle_raw **r)
487 {
488         if (!*r)
489                 return BUNDLE_ERROR_INVALID_PARAMETER; /* TC_FIX - double free sigabrt handling */
490
491         free(*r);
492         *r = NULL;
493         return BUNDLE_ERROR_NONE;
494 }
495
496 bundle *bundle_decode(const bundle_raw *r, const int data_size)
497 {
498         bundle *b;
499         bundle_raw *p_r;
500         unsigned char *d_str;
501         gint state = 0;
502         guint save = 0;
503         unsigned int d_len_raw;
504         unsigned char *d_r;
505         unsigned int d_len;
506         char *extract_cksum;
507         gchar *compute_cksum;
508         size_t bytes_read;
509         keyval_t *kv;
510         int type;
511
512         if (r == NULL) {
513                 set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
514                 return NULL;
515         }
516
517         extract_cksum = calloc(CHECKSUM_LENGTH + 1, sizeof(char));
518         if (unlikely(extract_cksum == NULL)) {
519                 set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
520                 return NULL;
521         }
522
523         /*
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
527          */
528         d_str = malloc((data_size / 4) * 3 + 3);
529         if (unlikely(d_str == NULL)) {
530                 set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY);
531                 free(extract_cksum);
532                 return NULL;
533         }
534
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);
538                 free(d_str);
539                 free(extract_cksum);
540                 return NULL;
541         }
542
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);
549         /*
550          * compare checksum values- extracted from the received
551          * string and computed from the data
552          */
553         if (strcmp(extract_cksum, compute_cksum) != 0) {
554                 free(d_str);
555                 free(extract_cksum);
556                 g_free(compute_cksum);
557                 return NULL;
558         }
559         d_r = d_str + CHECKSUM_LENGTH;
560         d_len = d_len_raw - CHECKSUM_LENGTH;
561
562         /* re-construct bundle */
563         b = bundle_create();
564         if (b == NULL) {
565                 free(d_str);
566                 free(extract_cksum);
567                 g_free(compute_cksum);
568                 return NULL;
569         }
570
571         p_r = (bundle_raw *)d_r;
572         while (p_r < d_r + d_len - 1) {
573                 kv = NULL;      /* To get a new kv */
574
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);
580                 else
581                         bytes_read = keyval_decode(p_r, &kv);
582
583                 if (kv)
584                         _bundle_append_kv(b, kv);
585                 else
586                         break;
587                 p_r += bytes_read;
588         }
589
590         free(extract_cksum);
591         g_free(compute_cksum);
592         free(d_str);
593
594         set_last_result(BUNDLE_ERROR_NONE);
595         return b;
596 }
597
598 struct _argv_idx {
599         int argc;
600         char **argv;
601         int idx;
602 };
603
604 int bundle_encode_raw(bundle *b, bundle_raw **r, int *len)
605 {
606         keyval_t *kv = NULL;
607         unsigned char *m = NULL;
608         unsigned char *p_m = NULL;
609         unsigned char *byte = NULL;
610         size_t byte_len;
611         gchar *chksum_val = NULL;
612         size_t msize = 0;
613
614         if (b == NULL || r == NULL)
615                 return BUNDLE_ERROR_INVALID_PARAMETER;
616
617         /* calculate memory size */
618         kv = b->kv_head;
619         while (kv != NULL) {
620                 msize += kv->method->get_encoded_size(kv);
621                 kv = kv->next;
622         }
623
624         m = calloc(msize+CHECKSUM_LENGTH, sizeof(unsigned char));
625         if (unlikely(m == NULL))
626                 return BUNDLE_ERROR_OUT_OF_MEMORY;
627
628         p_m = m + CHECKSUM_LENGTH;      /* temporary pointer */
629
630         kv = b->kv_head;
631         while (kv != NULL) {
632                 byte = NULL;
633                 byte_len = 0;
634
635                 kv->method->encode(kv, &byte, &byte_len);
636                 memcpy(p_m, byte, byte_len);
637
638                 p_m += byte_len;
639                 kv = kv->next;
640
641                 free(byte);
642         }
643
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);
649
650         *r = m;
651         *len = msize + CHECKSUM_LENGTH;
652         g_free(chksum_val); /* free checksum string */
653
654         return BUNDLE_ERROR_NONE;
655 }
656
657 bundle *bundle_decode_raw(const bundle_raw *r, const int data_size)
658 {
659         bundle *b = NULL;
660         bundle_raw *p_r = NULL;
661         unsigned char *d_str = NULL;
662         unsigned int d_len_raw;
663         unsigned char *d_r = NULL;
664         unsigned int d_len;
665         char *extract_cksum;
666         gchar *compute_cksum = NULL;
667         size_t bytes_read;
668         keyval_t *kv;
669         int type;
670
671         if (r == NULL) {
672                 set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
673                 return NULL;
674         }
675
676         extract_cksum = calloc(CHECKSUM_LENGTH + 1, sizeof(char));
677         if (unlikely(extract_cksum == NULL)) {
678                 set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY);
679                 return NULL;
680         }
681
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);
692         /*
693          * compare checksum values- extracted from the received
694          * string and computed from the data
695          */
696         if (strcmp(extract_cksum, compute_cksum) != 0) {
697                 free(extract_cksum);
698                 g_free(compute_cksum);
699                 set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
700                 return NULL;
701         }
702         d_r = d_str + CHECKSUM_LENGTH;
703         d_len = d_len_raw - CHECKSUM_LENGTH;
704
705         /* re-construct bundle */
706         b = bundle_create();
707         if (b == NULL) {
708                 free(extract_cksum);
709                 g_free(compute_cksum);
710                 return NULL;
711         }
712
713         p_r = (bundle_raw *)d_r;
714         while (p_r < d_r + d_len - 1) {
715                 kv = NULL;      /* To get a new kv */
716
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);
722                 else
723                         bytes_read = keyval_decode(p_r, &kv);
724
725                 if (kv)
726                         _bundle_append_kv(b, kv);
727                 else
728                         break;
729                 p_r += bytes_read;
730         }
731
732         free(extract_cksum);
733         g_free(compute_cksum);
734         set_last_result(BUNDLE_ERROR_NONE);
735
736         return b;
737 }
738
739 void _iter_export_to_argv(const char *key, const int type, const keyval_t *kv,
740                 void *user_data)
741 {
742         struct _argv_idx *vi = (struct _argv_idx *)user_data;
743         unsigned char *byte = NULL;
744         unsigned char *encoded_byte = NULL;
745         size_t byte_len = 0;
746
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",
751                                 key);
752                 return;
753         }
754
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");
758                 return;
759         }
760
761         vi->argv[vi->idx + 1] = (char *)encoded_byte;
762         (vi->idx) += 2;
763
764         free(byte);
765 }
766
767 int bundle_export_to_argv(bundle *b, char ***argv)
768 {
769         struct _argv_idx vi;
770         int argc;
771         int item_count;
772
773         if (b == NULL || argv == NULL) {
774                 set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
775                 return -1;
776         }
777
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 *));
782         if (!*argv) {
783                 set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY);
784                 return -1;
785         }
786
787         vi.argc = argc;
788         vi.argv = *argv;
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]); */
792
793         bundle_foreach(b, _iter_export_to_argv, &vi);
794
795         set_last_result(BUNDLE_ERROR_NONE);
796         return argc;
797 }
798
799 int bundle_free_exported_argv(int argc, char ***argv)
800 {
801         int i;
802
803         if (argc < 1)
804                 return BUNDLE_ERROR_INVALID_PARAMETER;
805
806         if (!*argv || argc < 2)
807                 return BUNDLE_ERROR_INVALID_PARAMETER;
808
809         for (i = 2; i < argc; i++)
810                 free((*argv)[i]);
811         free(*argv);
812         *argv = NULL;
813
814         return BUNDLE_ERROR_NONE;
815 }
816
817 bundle *bundle_import_from_argv(int argc, char **argv)
818 {
819         int idx;
820         int type;
821         keyval_t *kv;
822         keyval_array_t *kva;
823         unsigned char *byte;
824         char *encoded_byte;
825         unsigned int byte_size;
826         bundle *b;
827
828         if (!argv) {
829                 set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
830                 return NULL;  /* TC_FIX error handling for argv =NULL*/
831         }
832
833         b = bundle_create();
834         if (!b) {
835                 set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY);
836                 return NULL;
837         }
838
839         if (argc < 2) {
840                 set_last_result(BUNDLE_ERROR_NONE);
841                 return b;
842         }
843
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]);
848
849                 return b;
850         }
851
852         /* start idx from 2 as argv[1] is encoded */
853         for (idx = 2; idx + 1 < argc; idx = idx + 2) {
854                 kv = NULL;
855                 kva = NULL;
856
857                 encoded_byte = argv[idx+1];
858
859                 /* base64_decode */
860                 byte = g_base64_decode(encoded_byte, (gsize *)&byte_size);
861                 if (byte == NULL) {
862                         if (b)
863                                 set_last_result(bundle_free(b));
864                         return NULL;
865                 }
866
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;
872                 } else {
873                         if (keyval_decode(byte, &kv) == 0) /* TODO: error! */
874                                 BUNDLE_EXCEPTION_PRINT("Unable to Decode\n");
875                 }
876                 _bundle_append_kv(b, kv);
877
878                 free(byte);
879                 byte = NULL;
880         }
881
882         set_last_result(BUNDLE_ERROR_NONE);
883         return b;
884 }
885
886 int bundle_get_type(bundle *b, const char *key)
887 {
888         keyval_t *kv = _bundle_find_kv(b, key);
889
890         if (kv) {
891                 return kv->type;
892         } else {
893                 set_last_result(BUNDLE_ERROR_KEY_NOT_AVAILABLE);
894                 return BUNDLE_TYPE_NONE;
895         }
896 }
897
898 /*
899  * Get length of an array
900  */
901 unsigned int bundle_get_array_len(bundle *b, const char *key)
902 {
903         return BUNDLE_ERROR_NONE;
904 }
905
906 /*
907  * Get size of an item in byte, of given pointer
908  */
909 size_t bundle_get_array_val_size(bundle *b, const char *key,
910                 const void *val_ptr)
911 {
912         return BUNDLE_ERROR_NONE;
913 }
914
915 static int bundle_set_array_val(bundle *b, const char *key, const int type,
916                 const unsigned int idx, const void *val, const size_t size)
917 {
918         keyval_t *kv;
919         keyval_array_t *kva;
920
921         kv = _bundle_find_kv(b, key);
922         if (kv == NULL)
923                 return get_last_result();
924
925         if (type != kv->type)
926                 return BUNDLE_ERROR_INVALID_PARAMETER;
927
928         /* TODO: Is this needed? */
929         if (!keyval_type_is_array(kv->type))
930                 return BUNDLE_ERROR_INVALID_PARAMETER;
931
932         kva = (keyval_array_t *)kv;
933         if (!keyval_array_is_idx_valid(kva, idx))
934                 return BUNDLE_ERROR_INVALID_PARAMETER;
935
936         /* NULL value test (TODO: is this needed?) */
937         if (!kva->array_val)
938                 return BUNDLE_ERROR_INVALID_PARAMETER;
939
940         return keyval_array_set_element(kva, idx, (void *)val, size);
941 }
942
943 int bundle_add_str_array(bundle *b, const char *key, const char **str_array,
944                 const int len)
945 {
946         return _bundle_add_kv(b, key, str_array, 0, BUNDLE_TYPE_STR_ARRAY, len);
947 }
948
949 int bundle_get_val_array(bundle *b, const char *key, char ***str_array,
950                 int *len)
951 {
952         return _bundle_get_val(b, key, BUNDLE_TYPE_STR_ARRAY,
953                         (void **)str_array, NULL, (unsigned int *)len, NULL);
954 }
955
956 const char **bundle_get_str_array(bundle *b, const char *key, int *len)
957 {
958         int ret = BUNDLE_ERROR_NONE;
959         const char **arr_val = NULL;
960
961         ret = bundle_get_val_array(b, key, (char ***)&arr_val, len);
962         set_last_result(ret);
963
964         return arr_val;
965 }
966
967 int bundle_compare(bundle *b1, bundle *b2)
968 {
969         keyval_t *kv1;
970         keyval_t *kv2;
971
972         if (!b1 || !b2)
973                 return -1;
974
975         if (bundle_get_count(b1) != bundle_get_count(b2))
976                 return 1;
977
978         for (kv1 = b1->kv_head; kv1 != NULL; kv1 = kv1->next) {
979                 kv2 = _bundle_find_kv(b2, kv1->key);
980                 if (!kv2)
981                         return 1;
982                 if (kv1->method->compare(kv1, kv2))
983                         return 1;
984         }
985
986         return 0;
987 }
988
989 int bundle_set_str_array_element(bundle *b, const char *key,
990                 const unsigned int idx, const char *val)
991 {
992         if (!val)
993                 return BUNDLE_ERROR_INVALID_PARAMETER;
994
995         return bundle_set_array_val(b, key, BUNDLE_TYPE_STR_ARRAY,
996                         idx, val, strlen(val) + 1);
997 }
998
999 int bundle_add_byte(bundle *b, const char *key, const void *byte,
1000                 const size_t size)
1001 {
1002         return _bundle_add_kv(b, key, byte, size, BUNDLE_TYPE_BYTE, 1);
1003 }
1004
1005 int bundle_get_byte(bundle *b, const char *key, void **byte, size_t *size)
1006 {
1007         return _bundle_get_val(b, key, BUNDLE_TYPE_BYTE, (void **)byte,
1008                         size, NULL, NULL);
1009 }
1010
1011 int bundle_add_byte_array(bundle *b, const char *key, void **byte_array,
1012                 const unsigned int len)
1013 {
1014         return _bundle_add_kv(b, key, byte_array, 0,
1015                         BUNDLE_TYPE_BYTE_ARRAY, len);
1016 }
1017
1018 int bundle_get_byte_array(bundle *b, const char *key, void ***byte_array,
1019                 unsigned int *len, unsigned int **array_element_size)
1020 {
1021         return _bundle_get_val(b, key, BUNDLE_TYPE_BYTE_ARRAY,
1022                         (void **)byte_array, NULL, len, (size_t **)array_element_size);
1023 }
1024
1025 int bundle_set_byte_array_element(bundle *b, const char *key,
1026                 const unsigned int idx, const void *val, const size_t size)
1027 {
1028         return bundle_set_array_val(b, key, BUNDLE_TYPE_BYTE_ARRAY,
1029                         idx, val, size);
1030 }