Tizen 2.0 Release
[pkgs/o/oma-ds-service.git] / src / plugins / ds-public / xcalllog / src / encoding_util.c
1 /*
2  * oma-ds-agent
3  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
4  *
5  * Licensed under the Apache License, Version 2.0 (the License);
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <glib.h>
19 #include <glib/gprintf.h>
20
21 #include <sync_agent.h>
22
23 #include "encoding_util.h"
24
25 #ifndef OMADS_AGENT_LOG
26 #undef LOG_TAG
27 #define LOG_TAG "ENCODING_UTIL"
28 #endif
29
30 #define TYPE_CHECK_BUF_SIZE                     100     /* temporary definition */
31 #define TEMP_BUFFER_SIZE                                1024    /* temporary definition */
32
33 /* BASE64 TABLE */
34 static char base64_table[65] = {
35         'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
36         'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
37         'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a',
38         'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
39         'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
40         't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1',
41         '2', '3', '4', '5', '6', '7', '8', '9', '+',
42         '/', '='
43 };
44
45 /* Quoted-Printable */
46 static char *_dec_to_hex(int dec);
47 static int _hex_to_dec(char *hex);
48
49 /* Base64 */
50 static int _find_base(char ch);
51
52 /*
53  * Quoted-Printable
54  */
55 int encode_qp(char *src, int src_len, char **en_src, int *en_src_len)
56 {
57         _EXTERN_FUNC_ENTER;
58
59         retvm_if(src == NULL, 0, "[encoding_util] src is NULL\n");
60
61         char *en_hex;
62         int i = 0;
63         int j = 0;
64         int size_cnt = 0;
65
66         /*encoded string length is three times longer than existing maximum string length */
67         int full_lenth = src_len * 3 + 1 + (int)(src_len * 3 / 76) * 3;
68         *en_src = (char *)malloc(sizeof(char) * full_lenth);
69         if (*en_src == NULL) {
70                 _DEBUG_ERROR("[encoding_util] malloc fail !!\n");
71                 return 0;
72         }
73         memset(*en_src, 0x00, src_len * 3 + 1);
74
75         _DEBUG_INFO("[encoding_util] src_len : %d\n", src_len);
76         for (i = 0; i < src_len; i++) {
77                 /* _DEBUG_INFO("[encoding_util] i : %d\n", i); */
78                 if (size_cnt >= QP_SIZE - 3) {
79                         size_cnt = 0;
80                         (*en_src)[j++] = 0x0A;
81                 }
82
83                 if ((src[i] >= 33 && src[i] <= 126) || (src[i] == 0x0A)) {
84                         if (src[i] == 61) {
85                                 en_hex = _dec_to_hex(src[i]);
86                                 (*en_src)[j++] = 0x3D;  /* '=' */
87                                 (*en_src)[j++] = en_hex[0];
88                                 (*en_src)[j++] = en_hex[1];
89                                 size_cnt += 3;
90                         } else {
91                                 size_cnt++;
92                                 (*en_src)[j++] = src[i];
93                         }
94                 } else if (src[i] == 9 || src[i] == 32) {
95                         if (src[i + 1] == 0x0A || src[i + 1] == '\0') { /* TAB or WhiteSpace */
96                                 en_hex = _dec_to_hex(src[i]);
97                                 (*en_src)[j++] = 0x3D;  /* '=' */
98                                 (*en_src)[j++] = en_hex[0];
99                                 (*en_src)[j++] = en_hex[1];
100                                 size_cnt += 3;
101                         } else {
102                                 size_cnt++;
103                                 (*en_src)[j++] = src[i];
104                         }
105                 } else {
106                         en_hex = _dec_to_hex(src[i]);
107                         (*en_src)[j++] = 0x3D;  /* '=' */
108                         (*en_src)[j++] = en_hex[0];
109                         (*en_src)[j++] = en_hex[1];
110                         _DEBUG_INFO("[encoding_util] en_src : %s\n", *en_src);
111                         size_cnt += 3;
112                 }
113         }
114
115         (*en_src)[j] = 0x00;
116         *en_src_len = size_cnt;
117
118         _EXTERN_FUNC_EXIT;
119         return 1;
120 }
121
122 int decode_qp(char *src, int src_len, char **de_src, int *de_src_len)
123 {
124         _EXTERN_FUNC_ENTER;
125
126         retvm_if(src == NULL, 0, "[encoding_util] src is NULL\n");
127
128         char hex[3];
129         char ch;
130         int dec = 0;
131         int is_space = 0;
132         int i = 0;
133         int j = 0;
134
135         *de_src = (char *)malloc(sizeof(char) * (src_len + 1));
136         if (*de_src == NULL) {
137                 _DEBUG_ERROR("[encoding_util] malloc is fail !!\n");
138                 return 0;
139         }
140         memset(*de_src, 0x00, sizeof(char) * (src_len + 1));
141
142         while (src[i] != '\0') {
143                 if (src[i] == 0x3D) {   /* '=' */
144                         /* check whiteSpace */
145                         ch = src[++i];
146
147                         /* '=' skip if next character is a TAB or WhiteSpace */
148                         while (ch == 0x09 || ch == 0x20) {
149                                 is_space = 1;
150                                 ch = src[++i];
151
152                                 if (is_space == 1) {
153                                         is_space = 0;
154                                         continue;
155                                 }
156                         }
157
158                         /* '=' skip if next character is 3D which means '=' (encoding error case ??) */
159                         while (ch == '3') {
160                                 ch = src[++i];
161
162                                 if (ch == 'D') {
163                                         ch = src[++i];
164                                         continue;
165                                 }
166                         }
167
168                         /* if next character is LF after '=' do doft line break
169                          * encoded QP string on one line 76 character is allowed
170                          */
171                         if (ch == 0x0A) {       /* LF */
172                                 i++;
173                                 continue;
174                         }
175
176                         hex[0] = src[i++];
177                         hex[1] = src[i++];
178                         hex[2] = '\0';
179
180                         dec = _hex_to_dec(hex);
181
182                         /* decoding error */
183                         if (dec < 0) {
184                                 /* when error occur, restore the previous encoding message */
185                                 (*de_src)[j++] = 0x3D;  /* '=' */
186                                 (*de_src)[j++] = hex[0];
187                                 (*de_src)[j++] = hex[1];
188                         } else {
189                                 (*de_src)[j++] = dec;
190                         }
191                 } else if (src[i] > 0x7E) {     /* encoding error */
192                         i++;    /* ignore that character */
193                 } else {
194                         (*de_src)[j++] = src[i++];
195                 }
196         }
197
198         (*de_src)[j] = '\0';
199         *de_src_len = j;
200
201         _EXTERN_FUNC_EXIT;
202         return 1;
203 }
204
205 static char *_dec_to_hex(int dec)
206 {
207         _INNER_FUNC_ENTER;
208
209         static char hex[3];
210         int i;
211         int ch;
212
213         for (i = 0; i < 2; i++) {
214                 if (i == 0) {
215                         ch = (dec & 0xF0) >> 4;
216                 } else if (i == 1) {
217                         ch = (dec & 0x0F);
218                 }
219
220                 if (ch >= 10) {
221                         hex[i] = 'A' + ch - 10;
222                 } else {
223                         hex[i] = '0' + ch;
224                 }
225         }
226
227         hex[i] = 0x00;
228
229         _DEBUG_TRACE("[encoding_util] hex : %s\n", hex);
230
231         _INNER_FUNC_EXIT;
232         return &hex[0];
233 }
234
235 static int _hex_to_dec(char *hex)
236 {
237         _INNER_FUNC_ENTER;
238
239         int dec = 0;
240         int byte;
241         int i = 0;
242
243         for (i = 0; i < 2; i++) {
244                 if (hex[i] >= '0' && hex[i] <= '9') {
245                         byte = hex[i] - '0';
246                 } else if (hex[i] >= 'A' && hex[i] <= 'F') {
247                         byte = hex[i] - 'A' + 10;
248                 } else if (hex[i] >= 'a' && hex[i] <= 'f') {
249                         byte = hex[i] - 'a' + 10;
250                 } else {
251                         byte = -1;
252                 }
253
254                 if (byte < 0)
255                         return -1;
256
257                 dec += (i == 0) ? byte << 4 : byte;
258         }
259
260         _INNER_FUNC_EXIT;
261         return dec;
262 }
263
264 /*
265  *      Base64
266  */
267 int encode_base64(char *src, int src_len, char **en_src, int *en_src_len)
268 {
269         _EXTERN_FUNC_ENTER;
270
271         retvm_if(src == NULL, 0, "[encoding_util] src is NULL\n");
272
273         int i = 0;
274         int j = 0;
275         int cnt = 0;
276         int ch = 0;
277         int size_cnt = 0;
278
279         *en_src = (char *)malloc(sizeof(char) * (src_len * 2));
280
281         if (*en_src == NULL) {
282                 _DEBUG_ERROR("[encoding_util] malloc fail !!\n");
283                 return 0;
284         }
285         memset(*en_src, 0x00, src_len * 2);
286
287         while (1) {
288                 switch (cnt++) {
289                 case 0:
290                         {
291                                 if (i < src_len) {
292                                         ch = (src[i] & 0xFC) >> 2;
293                                 } else {
294                                         ch = -1;
295                                 }
296                         }
297                         break;
298
299                 case 1:
300                         {
301                                 if (i < src_len) {
302                                         if (i + 1 < src_len) {
303                                                 ch = ((src[i] & 0x03) << 4) | ((src[i + 1] & 0xF0) >> 4);
304                                         } else {
305                                                 ch = ((src[i] & 0x03) << 4);
306                                         }
307                                 } else {
308                                         ch = -1;
309                                 }
310                                 i++;
311                                 break;
312                         }
313
314                 case 2:
315                         {
316                                 if (i < src_len) {
317                                         if (i + 1 < src_len) {
318                                                 ch = ((src[i] & 0x0F) << 2) | ((src[i] & 0xC0) >> 6);
319                                         } else {
320                                                 ch = ((src[i] & 0x0F) << 2);
321                                         }
322                                 } else {
323                                         ch = -1;
324                                 }
325                                 i++;
326                         }
327                         break;
328
329                 case 3:
330                         {
331                                 if (i < src_len) {
332                                         ch = (src[i] & 0x3F);
333                                 } else {
334                                         ch = -1;
335                                 }
336                                 i++;
337                                 cnt = 0;
338                         }
339                         break;
340                 }
341
342                 /*
343                    if (ch >= 0 && ch <= 25) {           // Upper Case Alphabet
344                    (*en_src)[j++] = 'A' + ch;
345                    } else if (ch >= 26 && ch <= 51) {           // Lower Case Alphabet
346                    (*en_src)[j++] = 'a' + ch - 26;
347                    } else if (ch >= 52 && ch <= 61) {           // Digit
348                    (*en_src)[j++] = '0' + ch - 52;
349                    } else if (ch == 62) {
350                    (*en_src)[j++] = '+';
351                    } else if (ch == 63) {
352                    (*en_src)[j++] = '/';
353                    } else if (ch == -1) {
354                    (*en_src)[j++] = '=';                // padding
355                    }
356                  */
357
358                 if (ch != -1) {
359                         (*en_src)[j++] = base64_table[ch];
360                 } else {
361                         (*en_src)[j++] = base64_table[64];      /* padding */
362                 }
363
364                 size_cnt++;
365
366                 if (j % 4 == 0) {
367                         if (size_cnt == BASE64_SIZE) {
368                                 size_cnt = 0;
369                                 (*en_src)[j++] = 0x0A;  /* soft line break */
370                         }
371
372                         if (i >= src_len)
373                                 break;
374                 }
375         }
376
377         (*en_src)[j] = 0x00;
378         *en_src_len = j;
379
380         _EXTERN_FUNC_EXIT;
381         return 1;
382 }
383
384 int decode_base64(char *src, int src_len, char **de_src, int *de_src_len)
385 {
386         _EXTERN_FUNC_ENTER;
387
388         retvm_if(src == NULL, 0, "[encoding_util] src is NULL\n");
389
390         long tmp = 0;           /* 4byte (using decoding) */
391         int i = 0;
392         int j = 0;
393         int cnt = 0;
394         int pad_cnt = 0;
395
396         /* de_src is enough for the src_len 3/4 size */
397         *de_src = (char *)malloc(sizeof(char) * (src_len));
398
399         if (*de_src == NULL) {
400                 _DEBUG_ERROR("[encoding_util] malloc is fail !!\n");
401                 return 0;
402         }
403         memset(*de_src, 0x00, src_len);
404
405         while (src[i] != '\0') {
406                 /*
407                    if (isupper(src[i])) {
408                    tmp = (tmp << 6) | (src[i] - 'A');           // Upper case : 0 ~ 25
409                    } else if (islower(src[i])) {
410                    tmp = (tmp << 6) | (src[i] - 'a' + 0x1A);            // Lower case : 26(0x1A) ~ 51
411                    } else if (isdigit(src[i])) {
412                    tmp = (tmp << 6) | (src[i] - '0' + 0x34);    // Number : 52(0x34) ~ 61
413                    } else if (src[i] == '+') {
414                    tmp = (tmp << 6) | 0x3E;             // '+' : 62(0x3E)
415                    } else if (src[i] == '/') {
416                    tmp = (tmp << 6) | 0x3F;                     // '/' : 63(0x3F)
417                    } else if (src[i] == '=') {
418                    pad_cnt++;
419                    tmp = (tmp << 6);                                    // '=' : padding
420                    } else {
421                    tmp = (tmp << 6);                                    // encoding error
422                    _DEBUG_INFO("encoding error !! \n");
423                    }
424                  */
425
426                 tmp = (tmp << 6) | (_find_base(src[i]));
427                 if (tmp == 64) {
428                         pad_cnt++;
429                 } else if (tmp == -1) {
430                         _DEBUG_ERROR("[encoding_util] encoding error \n");
431                 }
432
433                 if (++cnt >= 4) {
434                         (*de_src)[j++] = (char)((tmp & 0x00FF0000) >> 16);
435                         (*de_src)[j++] = (char)((tmp & 0x0000FF00) >> 8);
436                         (*de_src)[j++] = (char)(tmp & 0x000000FF);
437
438                         cnt = 0;
439                         tmp = 0;
440
441                         if (src[i + 1] == 0x0A) {       /* soft line break */
442                                 i++;
443                         }
444                 }
445
446                 i++;
447         }
448
449         (*de_src)[j - pad_cnt] = '\0';
450         *de_src_len = j - pad_cnt;
451
452         _EXTERN_FUNC_EXIT;
453         return 1;
454 }
455
456 int proc_decoding(const char *src, int src_len, char **de_src, int *de_src_len)
457 {
458         _EXTERN_FUNC_ENTER;
459
460         retvm_if(src == NULL, 0, "[encoding_util] src is NULL\n");
461
462         const char *reg_src = NULL;
463         reg_src = src;
464
465         _DEBUG_INFO("[encoding_util] << src >> \n%s\n", src);
466
467         *de_src = (char *)malloc(sizeof(char) * (src_len + 1));
468         if (*de_src == NULL) {
469                 _DEBUG_INFO("[encoding_util] malloc error !! \n");
470
471                 return 0;
472         }
473         memset(*de_src, 0x00, sizeof(char) * (src_len + 1));
474
475         char colon[] = ":";
476         char line_breaker[] = "\r\n";
477         char *start_decoding = NULL;
478         int data_size = 0;
479         int res = 0;
480         int de_temp_len = 0;
481         int len = 0;
482
483         char *de_temp = (char *)malloc(sizeof(char) * TEMP_BUFFER_SIZE);        /* todo : temporary */
484         if (de_temp == NULL) {
485                 _DEBUG_ERROR("[encoding_util] malloc error !!\n");
486
487                 if (*de_src != NULL)
488                         free(*de_src);
489
490                 return 0;
491         }
492
493         while ((start_decoding = strstr(src, "ENCODING="))) {
494                 char *colon_loc = strstr(start_decoding, colon);        /* find ':''s location */
495                 char *line_breaker_loc = NULL;
496                 if (colon_loc != NULL) {
497                         line_breaker_loc = strstr(colon_loc, line_breaker);     /* find "\r\n"'s location */
498                 } else {
499                         if (*de_src != NULL)
500                                 free(*de_src);
501
502                         if (de_temp != NULL)
503                                 free(de_temp);
504                         return 0;
505                 }
506
507                 /*
508                  *      if find "ENCODING=" do strcat data until ":" to de_src
509                  */
510                 data_size = (colon_loc + 1) - src;      /* colon_loc + 1 ==>  Until next character of ':' */
511
512                 char *temp = (char *)malloc(sizeof(char) * (data_size + 1));
513                 if (temp == NULL) {
514                         _DEBUG_ERROR("temp is null");
515                         if (*de_src != NULL)
516                                 free(*de_src);
517
518                         /* for prevent */
519                         if (de_temp != NULL)
520                                 free(de_temp);
521
522                         return 0;
523                 }
524                 memset(temp, 0x00, sizeof(char) * (data_size + 1));
525                 memcpy(temp, src, data_size);
526
527                 len = g_strlcat(*de_src, temp, (src_len + 1));
528                 if (len >= (src_len + 1)) {
529                         _DEBUG_ERROR("*de_src buffer overflow !!");
530
531                         if (*de_src != NULL)
532                                 free(*de_src);
533
534                         if (de_temp != NULL)
535                                 free(de_temp);
536
537                         if (temp != NULL)
538                                 free(temp);
539
540                         return 0;
541                 }
542
543                 if (temp != NULL)
544                         free(temp);
545
546                 _DEBUG_INFO("[encoding_util] << *de_src >> \n %s\n", *de_src);
547
548                 /*
549                  *              copy data from ":" until "\r\n"(actual encoding stream)
550                  */
551                 data_size = line_breaker_loc - colon_loc;       /* from ':' until "\r\n" + '\0' */
552                 char *value = (char *)malloc(sizeof(char) * (data_size + 1));
553                 if (value == NULL) {
554                         _DEBUG_ERROR("[encoding_util] malloc error !!\n");
555
556                         if (*de_src != NULL)
557                                 free(*de_src);
558
559                         if (de_temp != NULL)
560                                 free(de_temp);
561
562                         return 0;
563                 }
564                 memset(value, 0x00, sizeof(char) * (data_size + 1));
565                 memcpy(value, ++colon_loc, data_size);  /* from ':' until "\r\n" */
566                 value[data_size] = '\0';
567
568                 /*
569                  *      Get encoding type using data from "ENCODING=" to ":"
570                  */
571                 char type_check[TYPE_CHECK_BUF_SIZE] = { 0, };
572                 int type_check_size = colon_loc - start_decoding;
573                 strncpy(type_check, start_decoding, type_check_size);
574                 _DEBUG_INFO("[encoding_util] type check : %s\n", type_check);
575                 encoding_type_e type = find_encoding_type((const char *)type_check);
576
577                 /*
578                  *              Process decoding by passing the actual value and encoding type to decode_value()
579                  */
580                 de_temp_len = 0;
581                 memset(de_temp, 0x00, sizeof(char) * TEMP_BUFFER_SIZE); /* todo : temporary */
582
583                 res = decode_value(type, value, data_size, &de_temp, &de_temp_len);
584
585                 if (res != 1) {
586                         _DEBUG_ERROR("[encoding_util] decode_value error !!\n");
587
588                         if (*de_src != NULL)
589                                 free(*de_src);
590
591                         if (de_temp != NULL)
592                                 free(de_temp);
593
594                         if (value != NULL)
595                                 free(value);
596
597                         return 0;
598                 }
599
600                 /*
601                  *      Append decoded data to de_src
602                  */
603                 _DEBUG_INFO("[encoding_util] de_temp : %s\n", de_temp);
604                 len = 0;
605                 len = g_strlcat(*de_src, de_temp, (src_len + 1));
606                 if (len >= (src_len + 1)) {
607                         _DEBUG_ERROR("*de_src buffer overflow !!");
608
609                         if (*de_src != NULL)
610                                 free(*de_src);
611
612                         if (de_temp != NULL)
613                                 free(de_temp);
614
615                         if (value != NULL)
616                                 free(value);
617
618                         return 0;
619                 }
620
621                 /*
622                  *      find "ENCODING=" since "\r\n" agina
623                  */
624                 src = line_breaker_loc;
625
626                 if (value != NULL)
627                         free(value);
628         }
629
630         /* Append remain character */
631         len = 0;
632         len = g_strlcat(*de_src, src, (src_len + 1));
633         if (len >= (src_len + 1)) {
634                 _DEBUG_ERROR("*de_src buffer overflow !!");
635
636                 if (*de_src != NULL)
637                         free(*de_src);
638
639                 if (de_temp != NULL)
640                         free(de_temp);
641
642                 return 0;
643         }
644
645         *de_src_len = strlen(*de_src);
646         _DEBUG_INFO("[encoding_util] changed src : \n%s ( %d ) \n", *de_src, *de_src_len);
647
648         if (de_temp != NULL)
649                 free(de_temp);
650
651         _EXTERN_FUNC_EXIT;
652         return 1;
653 }
654
655 int check_encoding_data(const char *data)
656 {
657         _EXTERN_FUNC_ENTER;
658
659         if (strstr(data, "ENCODING=") != NULL) {
660                 _DEBUG_INFO("[encoding_util] exist encoding data !! \n");
661                 _EXTERN_FUNC_EXIT;
662                 return 1;
663         } else {
664                 _DEBUG_INFO("[encoding_util] not exist encoding data !! \n");
665                 _EXTERN_FUNC_EXIT;
666                 return 0;
667         }
668 }
669
670 encoding_type_e find_encoding_type(const char *data)
671 {
672         _EXTERN_FUNC_ENTER;
673
674         encoding_type_e type = EN_TYPE_NONE;
675         if (strstr(data, "QUOTED-PRINTABLE") != NULL) {
676                 _DEBUG_INFO("[encoding_util] type : QP\n");
677                 type = EN_TYPE_QUOTED_PRINTABLE;
678         } else if (strstr(data, "BASE64") != NULL) {
679                 _DEBUG_INFO("[encoding_util] type : BASE64\n");
680                 type = EN_TYPE_BASE64;
681         } else {
682                 _DEBUG_INFO("[encoding_util] not supported type !! \n");
683         }
684
685         _EXTERN_FUNC_EXIT;
686         return type;
687 }
688
689 int decode_value(encoding_type_e type, const char *value, int value_size, char **decode_str, int *decode_str_len)
690 {
691         _EXTERN_FUNC_ENTER;
692
693         retvm_if(value == NULL, 0, "[encoding_util] value is NULL\n");
694
695         int res = 1;
696         const char *start_pos = NULL;
697         const char *cursor = NULL;
698         int semi_cnt = 0;
699         int len = 0;
700
701         /*
702          *      ex> value - =EA=B9=80;=EC=B2=A0=EC=88=98;;;\0
703          */
704         cursor = value;
705         start_pos = value;
706
707         while (*cursor != '\0') {
708                 if ((*cursor != ';') && (*cursor != '\r')) {
709                         cursor++;
710                         continue;
711                 } else if (*cursor == ';') {
712                         semi_cnt++;
713                 }
714
715                 int data_size = 0;
716                 data_size = cursor - start_pos;
717
718                 if (data_size == 0) {
719                         cursor++;
720                         start_pos++;
721                 } else {
722                         char *temp = (char *)malloc(sizeof(char) * (value_size + 1));
723                         if (temp == NULL) {
724                                 _DEBUG_ERROR("MALLOC failed !!!");
725                                 res = 0;
726                                 return res;
727                         }
728                         memset(temp, 0x00, sizeof(char) * (value_size + 1));
729                         memcpy(temp, start_pos, data_size);
730
731                         _DEBUG_INFO("[encoding_util] temp : %s \n", temp);
732
733                         char *decoding = NULL;
734                         int decoding_len = 0;
735
736                         switch (type) {
737                         case EN_TYPE_QUOTED_PRINTABLE:
738                                 res = decode_qp(temp, data_size, &decoding, &decoding_len);
739                                 break;
740                         case EN_TYPE_BASE64:
741                                 res = decode_base64(temp, data_size, &decoding, &decoding_len);
742                                 break;
743                         default:
744                                 break;
745                         }
746
747                         if (temp != NULL)
748                                 free(temp);
749
750                         _DEBUG_INFO("[encoding_util] decoding : %s ( %d )\n", decoding, decoding_len);
751
752                         if (res != 1) {
753                                 _DEBUG_ERROR("[encoding_util] decoding error !! \n");
754
755                                 if (decoding != NULL)
756                                         free(decoding);
757
758                                 res = 0;
759                                 return res;
760                         }
761
762                         len = g_strlcat(*decode_str, decoding, TEMP_BUFFER_SIZE);
763                         if (len >= TEMP_BUFFER_SIZE) {
764                                 _DEBUG_ERROR("*decode_str buffer overflow !!");
765
766                                 if (decoding != NULL)
767                                         free(decoding);
768
769                                 res = 0;
770                                 return res;
771                         }
772
773                         _DEBUG_INFO("[encoding_util] *decode_str : %s\n", *decode_str);
774
775                         if (decoding != NULL)
776                                 free(decoding);
777
778                         cursor++;
779                         start_pos = cursor;
780                 }
781
782                 if (semi_cnt > 0) {
783                         int len = strlen(*decode_str);
784                         (*decode_str)[len] = ';';
785                         _DEBUG_INFO("[encoding_util] *decode_str : %s ( %d )\n", *decode_str, strlen(*decode_str));
786                         semi_cnt--;
787                 }
788         }
789
790         *decode_str_len = strlen(*decode_str);
791
792         _DEBUG_INFO("[encoding_util] *decode_str : %s ( %d )\n", *decode_str, *decode_str_len);
793
794         _EXTERN_FUNC_EXIT;
795         return res;
796 }
797
798 static int _find_base(char ch)
799 {
800         _EXTERN_FUNC_ENTER;
801
802         int i = 0;
803         for (i = 0; i < 65; i++) {
804                 if (base64_table[i] == ch) {
805                         _DEBUG_INFO("[encoding_util] End !! \n");
806                         return i;
807                 }
808         }
809
810         _EXTERN_FUNC_EXIT;
811         return -1;
812 }