Coverity issue fixes for email service
[platform/core/messaging/email-service.git] / email-core / email-core-mime.c
1 /*
2 *  email-service
3 *
4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5 *
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *
20 */
21
22
23
24 /* ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ***
25  *File :  email-core-mime.c
26  *Desc :  MIME Operation
27  *
28  *Auth :
29  *
30  *History :
31  *   2011.04.14  :  created
32  ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ***/
33 #undef close
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <vconf.h>
39 #include <ctype.h>
40 #include <sys/stat.h>
41 #include <sys/time.h>
42 #include <glib.h>
43 #include "email-internal-types.h"
44 #include "lnx_inc.h"
45 #include "email-utilities.h"
46 #include "email-core-utils.h"
47 #include "email-core-mail.h"
48 #include "email-core-mime.h"
49 #include "email-core-gmime.h"
50 #include "email-storage.h"
51 #include "email-core-event.h"
52 #include "email-core-account.h"
53 #include "email-core-signal.h"
54 #include "email-core-mailbox-sync.h"
55 #include "email-core-container.h"
56 #include "email-debug-log.h"
57
58 #define MIME_MESG_IS_SOCKET
59
60 #define MIME_LINE_LEN   1024
61 #define BOUNDARY_LEN    256
62
63 #define TYPE_TEXT            1
64 #define TYPE_IMAGE           2
65 #define TYPE_AUDIO           3
66 #define TYPE_VIDEO           4
67 #define TYPE_APPLICATION     5
68 #define TYPE_MULTIPART       6
69 #define TYPE_MESSAGE         7
70 #define TYPE_UNKNOWN         8
71
72 #define TEXT_STR             "TEXT"
73 #define IMAGE_STR            "IMAGE"
74 #define AUDIO_STR            "AUDIO"
75 #define VIDEO_STR            "VIDEO"
76 #define APPLICATION_STR      "APPLICATION"
77 #define MULTIPART_STR        "MULTIPART"
78 #define MESSAGE_STR          "MESSAGE"
79
80 #define CONTENT_TYPE         1
81 #define CONTENT_SUBTYPE      2
82 #define CONTENT_ENCODING     3
83 #define CONTENT_CHARSET      4
84 #define CONTENT_DISPOSITION  5
85 #define CONTENT_NAME         6
86 #define CONTENT_FILENAME     7
87 #define CONTENT_BOUNDARY     8
88 #define CONTENT_REPORT_TYPE  9
89 #define CONTENT_ID           10
90 #define CONTENT_LOCATION     11
91 #define CONTENT_SMIME_TYPE   12
92 #define CONTENT_PROTOCOL     13
93
94 #define GRAB_TYPE_TEXT       1  /*  retrieve text and attachment name */
95 #define GRAB_TYPE_ATTACHMENT 2  /*  retrieve only attachmen */
96
97 #define SAVE_TYPE_SIZE       1  /*  only get content siz */
98 #define SAVE_TYPE_BUFFER     2  /*  save content to buffe */
99 #define SAVE_TYPE_FILE       3  /*  save content to temporary fil */
100
101 /* ---------------------------------------------------------------------- */
102 /*  Global variable */
103 static int  eml_data_count = 0;
104
105 /* ---------------------------------------------------------------------- */
106 /*  External variable */
107 extern int _pop3_receiving_mail_id;
108 extern int _pop3_received_body_size;
109 extern int _pop3_last_notified_body_size;
110 extern int _pop3_total_body_size;
111
112 extern int _imap4_received_body_size;
113 extern int _imap4_last_notified_body_size;
114 extern int _imap4_total_body_size;
115 extern int _imap4_download_noti_interval_value;
116
117 extern int multi_part_body_size;
118 extern bool only_body_download;
119
120 /* ---------------------------------------------------------------------- */
121 __thread BODY **g_inline_list = NULL;
122 __thread int g_inline_count = 0;
123
124 void emcore_mime_free_param(struct _parameter *param);
125 void emcore_mime_free_part_header(struct _m_part_header *header);
126 void emcore_mime_free_message_header(struct _m_mesg_header *header);
127 void emcore_mime_free_rfc822_header(struct _rfc822header *rfc822header);
128 void emcore_mime_free_part(struct _m_part *part);
129 void emcore_mime_free_part_body(struct _m_body *body);
130 void emcore_mime_free_mime(struct _m_mesg *mmsg);
131
132 extern long pop3_reply(MAILSTREAM *stream);
133 /* ------------------------------------------------------------------------------------------------- */
134
135 char *em_get_escaped_str(const char *str)
136 {
137         EM_DEBUG_FUNC_BEGIN_SEC("str [%s]", str);
138
139         int i = 0, j = 0;
140         int len = 0;
141
142         if (!str)
143                 return NULL;
144         len = strlen(str);
145         char *ret = calloc(1, 2 * len);
146         if (ret == NULL) {
147                 return NULL;
148         }
149
150         int ret_i = 0;
151         const char to_be_escaped[10] = {'\\', '\"', '\0',};
152
153         for (i = 0; i < len; i++) {
154                 for (j = 0; to_be_escaped[j] != '\0'; j++) {
155                         if (str[i] == to_be_escaped[j]) {
156                                 ret[ret_i++] = '\\';
157                                 break;
158                         }
159                 }
160                 ret[ret_i++] = str[i];
161         }
162         ret[ret_i++] = '\0';
163
164         EM_DEBUG_FUNC_END();
165         return ret;
166 }
167
168 /* Fix for issue junk characters in mail body */
169 char *em_split_file_path(char *str)
170 {
171         EM_DEBUG_FUNC_BEGIN("str [%s]", str);
172
173         EM_IF_NULL_RETURN_VALUE(str, NULL);
174
175         char *buf = NULL;
176         char delims[] = "@";
177         char *result = NULL;
178         char *temp_str = NULL;
179         char *content_id = NULL;
180         int buf_length = 0;
181         char *temp_cid_data = NULL;
182         char *temp_cid = NULL;
183
184         temp_str = g_strdup(str);
185         buf_length = EM_SAFE_STRLEN(str) + 1024;
186         buf = em_malloc(buf_length);
187         if (buf == NULL) {
188                 EM_DEBUG_EXCEPTION("em_mallocfailed");
189                 EM_SAFE_FREE(temp_str);
190                 return NULL;
191         }
192
193         content_id = temp_str;
194         temp_cid = strstr(temp_str, "\"");
195         if (temp_cid == NULL) {
196                 EM_DEBUG_EXCEPTION(">>>> File Path Doesnot contain end line for CID ");
197                 EM_SAFE_FREE(buf);
198                 return temp_str;
199         }
200
201         temp_cid_data = em_malloc((temp_cid-temp_str)+1);
202         if (temp_cid_data == NULL) {
203                 EM_DEBUG_EXCEPTION("em_mallocfailed");
204                 EM_SAFE_FREE(buf);
205                 return temp_str;
206         }
207
208         memcpy(temp_cid_data, temp_str, (temp_cid-temp_str));
209
210         if (temp_cid_data && !strstr(temp_cid_data, delims)) {
211                 EM_DEBUG_EXCEPTION(">>>> File Path Doesnot contain @ ");
212                 EM_SAFE_FREE(buf);
213                 EM_SAFE_FREE(temp_cid_data);
214                 return temp_str;
215         } else {
216                 result = strstr(temp_str, delims);
217                 if (result != NULL) {
218                         *result = '\0';
219                         result++;
220                         EM_DEBUG_LOG_SEC("content_id is [ %s ]", content_id);
221
222                         if (strcasestr(content_id, ".bmp") || strcasestr(content_id, ".jpeg") || strcasestr(content_id, ".png") ||
223                                         strcasestr(content_id, ".jpg") || strcasestr(content_id, ".gif"))
224                                 snprintf(buf+EM_SAFE_STRLEN(buf), buf_length - EM_SAFE_STRLEN(buf), "%s\"", content_id);
225                         else
226                                 snprintf(buf+EM_SAFE_STRLEN(buf), buf_length - EM_SAFE_STRLEN(buf), "%s%s", content_id, ".jpeg\"");
227                 } else {
228                         EM_DEBUG_EXCEPTION(">>>> File Path Doesnot contain end line for CID ");
229                         EM_SAFE_FREE(buf);
230                         EM_SAFE_FREE(temp_cid_data);
231                         return temp_str;
232                 }
233                 result = strstr(result, "\"");
234                 if (result != NULL) {
235                         result++;
236                         snprintf(buf+EM_SAFE_STRLEN(buf), buf_length - EM_SAFE_STRLEN(buf), "%s", result);
237                 }
238         }
239
240         EM_SAFE_FREE(temp_str);
241         EM_SAFE_FREE(temp_cid_data);
242
243         EM_DEBUG_FUNC_END();
244         return buf;
245 }
246
247 /*
248  * decode body text (quoted-printable, base64)
249  * enc_type : encoding type (base64/quotedprintable)
250  */
251 INTERNAL_FUNC int emcore_decode_body_text(char *enc_buf, int enc_len, int enc_type, int *dec_len, int *err_code)
252 {
253         EM_DEBUG_FUNC_BEGIN("enc_buf[%p], enc_len[%d], enc_type[%d], dec_len[%p]", enc_buf, enc_len, enc_type, dec_len);
254         unsigned char *content = NULL;
255
256         /*  too many called */
257         *dec_len = enc_len;
258
259         switch (enc_type) {
260         case ENCQUOTEDPRINTABLE:
261
262                 content = rfc822_qprint((unsigned char *)enc_buf, (unsigned long)enc_len, (unsigned long *)dec_len);
263                 break;
264
265         case ENCBASE64:
266
267                 content = rfc822_base64((unsigned char *)enc_buf, (unsigned long)enc_len, (unsigned long *)dec_len);
268                 break;
269
270         case ENC7BIT:
271         case ENC8BIT:
272         case ENCBINARY:
273         case ENCOTHER:
274         default:
275                 break;
276         }
277
278         if (content) {
279                 if (enc_len < *dec_len) {
280                         EM_DEBUG_EXCEPTION("Decoded length is too big to store it");
281                         EM_SAFE_FREE(content);
282                         if (err_code) *err_code = EMAIL_ERROR_INVALID_DATA;
283                         return EMAIL_ERROR_INVALID_DATA;
284                 }
285                 memcpy(enc_buf, content, *dec_len);
286                 enc_buf[*dec_len] = '\0';
287                 EM_SAFE_FREE(content);
288         }
289         EM_DEBUG_FUNC_END();
290
291         if (err_code) *err_code = EMAIL_ERROR_NONE;
292         return EMAIL_ERROR_NONE;
293 }
294
295 /*  get temporary file name */
296 char *emcore_mime_get_save_file_name(int *err_code)
297 {
298         EM_DEBUG_FUNC_BEGIN();
299         char tempname[512];
300         struct timeval tv;
301
302         gettimeofday(&tv, NULL);
303         srand(tv.tv_usec);
304
305         memset(tempname, 0x00, sizeof(tempname));
306         unsigned int seed = time(NULL);
307         SNPRINTF(tempname, sizeof(tempname), "%s%s%d", MAIL_TEMP, DIR_SEPERATOR, rand_r(&seed));
308         EM_DEBUG_FUNC_END();
309         return EM_SAFE_STRDUP(tempname);
310 }
311
312 /*  get a line from file pointer */
313 char *emcore_get_line_from_file(void *stream, char *buf, int size, int *err_code)
314 {
315         if (!fgets(buf, size, (FILE *)stream)) {
316                 if (feof((FILE *)stream)) {
317                         *err_code = EMAIL_ERROR_NO_MORE_DATA;
318                         return NULL;
319                 } else
320                         return NULL;
321         }
322         return buf;
323 }
324
325 /*  get a line from POP3 mail stream */
326 /*  emcore_mail_cmd_read_mail_pop3 must be called before this function */
327 char *emcore_mime_get_line_from_sock(void *stream, char *buf, int size, int *err_code)
328 {
329         EM_DEBUG_FUNC_BEGIN("stream[%p], buf[%p]", stream, buf);
330         POP3LOCAL *p_pop3local = NULL;
331
332         if (!stream || !buf) {
333                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
334                 if (err_code != NULL)
335                         *err_code = EMAIL_ERROR_INVALID_PARAM;
336                 return NULL;
337         }
338
339         memset(buf, 0x00, size);
340
341         p_pop3local = (POP3LOCAL *)(((MAILSTREAM *)stream)->local);
342         if (!p_pop3local) {
343                 EM_DEBUG_EXCEPTION("stream->local[%p]", p_pop3local);
344                 if (err_code != NULL)
345                         *err_code = EMAIL_ERROR_INVALID_PARAM;
346                 return NULL;
347         }
348
349         if (!pop3_reply((MAILSTREAM *)stream)) { /*  if TRUE, check response */
350                 EM_DEBUG_LOG_DEV("p_pop3local->response 1[%s]", p_pop3local->response);
351                 if (p_pop3local->response) {
352                         if (*p_pop3local->response == '.' && EM_SAFE_STRLEN(p_pop3local->response) == 1) {
353                                 EM_SAFE_FREE(p_pop3local->response);
354                                 if (err_code != NULL)
355                                         *err_code = EMAIL_ERROR_NO_MORE_DATA;
356                                 EM_DEBUG_FUNC_END("end of response");
357                                 return NULL;
358                         }
359                         EM_DEBUG_LOG_DEV("Not end of response");
360                         strncpy(buf, p_pop3local->response, size-1);
361                         strncat(buf, CRLF_STRING, size-(EM_SAFE_STRLEN(buf) + 1));
362
363                         EM_SAFE_FREE(p_pop3local->response);
364                         goto FINISH_OFF;
365                 }
366         }
367
368         EM_DEBUG_LOG_DEV("p_pop3local->response 2[%s]", p_pop3local->response);
369         if (p_pop3local->response) {
370                 /*  if response isn't NULL, check whether this response start with '+' */
371                 /*  if the first character is '+', return error because this response is normal data */
372                 strncpy(buf, p_pop3local->response, size-1);
373                 strncat(buf, CRLF_STRING, size-(EM_SAFE_STRLEN(buf)+1));
374                 EM_SAFE_FREE(p_pop3local->response);
375                 goto FINISH_OFF;
376         } else {
377                 EM_DEBUG_EXCEPTION("p_pop3local->response is null. network error... ");
378                 if (err_code != NULL)
379                         *err_code = EMAIL_ERROR_INVALID_RESPONSE;
380                 EM_DEBUG_FUNC_END();
381                 return NULL;
382         }
383
384 FINISH_OFF:
385         if (buf) {
386
387                 //remove stuffed dot
388                 if (EM_SAFE_STRLEN(buf) >= 2 && buf[0] == '.' && buf[1] == '.') {
389                         char *tmp = g_strdup(buf+1);
390                         memset(buf, 0x00, size);
391                         strncpy(buf, tmp, size-1);
392                         EM_SAFE_FREE(tmp);
393                 }
394
395                 int received_percentage, last_notified_percentage;
396                 _pop3_received_body_size += EM_SAFE_STRLEN(buf);
397
398                 last_notified_percentage = (double)_pop3_last_notified_body_size / (double)_pop3_total_body_size *100.0;
399                 received_percentage      = (double)_pop3_received_body_size / (double)_pop3_total_body_size *100.0;
400
401                 EM_DEBUG_LOG_DEV("_pop3_received_body_size = %d, _pop3_total_body_size = %d", _pop3_received_body_size, _pop3_total_body_size);
402                 EM_DEBUG_LOG_DEV("received_percentage = %d, last_notified_percentage = %d", received_percentage, last_notified_percentage);
403
404                 if (received_percentage > last_notified_percentage + 5) {
405                         if (!emcore_notify_network_event(NOTI_DOWNLOAD_BODY_START, _pop3_receiving_mail_id, "dummy-file", _pop3_total_body_size, _pop3_received_body_size))
406                                 EM_DEBUG_EXCEPTION(" emcore_notify_network_event[NOTI_DOWNLOAD_BODY_START] Failed >>>> ");
407                         else
408                                 EM_DEBUG_LOG("NOTI_DOWNLOAD_BODY_START notified (%d / %d)", _pop3_received_body_size, _pop3_total_body_size);
409                         _pop3_last_notified_body_size = _pop3_received_body_size;
410                 }
411         }
412         EM_DEBUG_FUNC_END();
413         return buf;
414 }
415
416 void emcore_mime_free_param(struct _parameter *param)
417 {
418         struct _parameter *t, *p = param;
419         EM_DEBUG_FUNC_BEGIN();
420         while (p) {
421                 t = p->next;
422                 EM_SAFE_FREE(p->name);
423                 EM_SAFE_FREE(p->value);
424                 EM_SAFE_FREE(p);
425                 p = t;
426         }
427         EM_DEBUG_FUNC_END();
428 }
429
430 void emcore_mime_free_part_header(struct _m_part_header *header)
431 {
432         EM_DEBUG_FUNC_BEGIN();
433         if (!header) return ;
434         EM_SAFE_FREE(header->type);
435         if (header->parameter) emcore_mime_free_param(header->parameter);
436         EM_SAFE_FREE(header->subtype);
437         EM_SAFE_FREE(header->encoding);
438         EM_SAFE_FREE(header->desc);
439         EM_SAFE_FREE(header->disp_type);
440         EM_SAFE_FREE(header->content_id);
441         EM_SAFE_FREE(header->content_location);
442         EM_SAFE_FREE(header->priority);
443         EM_SAFE_FREE(header->ms_priority);
444         if (header->disp_parameter) emcore_mime_free_param(header->disp_parameter);
445         EM_SAFE_FREE(header);
446         EM_DEBUG_FUNC_END();
447 }
448
449 void emcore_mime_free_message_header(struct _m_mesg_header *header)
450 {
451         EM_DEBUG_FUNC_BEGIN();
452         if (!header) return ;
453         EM_SAFE_FREE(header->version);
454         if (header->part_header) emcore_mime_free_part_header(header->part_header);
455         EM_SAFE_FREE(header);
456         EM_DEBUG_FUNC_END();
457 }
458
459 void emcore_mime_free_rfc822_header(struct _rfc822header *header)
460 {
461         EM_DEBUG_FUNC_BEGIN();
462         if (!header) return ;
463         EM_SAFE_FREE(header->return_path);
464         EM_SAFE_FREE(header->received);
465         EM_SAFE_FREE(header->reply_to);
466         EM_SAFE_FREE(header->date);
467         EM_SAFE_FREE(header->from);
468         EM_SAFE_FREE(header->subject);
469         EM_SAFE_FREE(header->sender);
470         EM_SAFE_FREE(header->to);
471         EM_SAFE_FREE(header->cc);
472         EM_SAFE_FREE(header->bcc);
473         EM_SAFE_FREE(header);
474         EM_DEBUG_FUNC_END();
475 }
476
477 void emcore_mime_free_part_body(struct _m_body *body)
478 {
479         EM_DEBUG_FUNC_BEGIN();
480         if (!body) return ;
481         if (body->part_header) emcore_mime_free_part_header(body->part_header);
482         EM_SAFE_FREE(body->text);
483         if (body->nested.body) emcore_mime_free_part_body(body->nested.body);
484         if (body->nested.next) emcore_mime_free_part(body->nested.next);
485         EM_SAFE_FREE(body);
486         EM_DEBUG_FUNC_END();
487 }
488
489 void emcore_mime_free_part(struct _m_part *part)
490 {
491         EM_DEBUG_FUNC_BEGIN();
492         if (!part) return ;
493         if (part->body) emcore_mime_free_part_body(part->body);
494         if (part->next) emcore_mime_free_part(part->next);
495         EM_SAFE_FREE(part);
496         EM_DEBUG_FUNC_END();
497 }
498
499 void emcore_mime_free_mime(struct _m_mesg *mmsg)
500 {
501         EM_DEBUG_FUNC_BEGIN();
502
503         if (!mmsg) return ;
504         if (mmsg->header) emcore_mime_free_message_header(mmsg->header);
505         if (mmsg->rfc822header) emcore_mime_free_rfc822_header(mmsg->rfc822header);
506         if (mmsg->nested.body) emcore_mime_free_part_body(mmsg->nested.body);
507         if (mmsg->nested.next) emcore_mime_free_part(mmsg->nested.next);
508         EM_SAFE_FREE(mmsg->text);
509         EM_SAFE_FREE(mmsg);
510         EM_DEBUG_FUNC_END();
511 }
512
513 /*
514 static char *em_parse_filename(char *filename)
515 {
516         EM_DEBUG_FUNC_BEGIN("filename [%p] ", filename);
517         if (!filename) {
518                 EM_DEBUG_EXCEPTION("filename is NULL ");
519                 return NULL;
520         }
521
522         char delims[] = "@";
523         char *result = NULL;
524         static char parsed_filename[512] = {0, };
525
526         memset(parsed_filename, 0x00, 512);
527
528         if (!strstr(filename, delims)) {
529                 EM_DEBUG_EXCEPTION("FileName does not contain @ ");
530                 return NULL;
531         }
532
533         result = strtok(filename, delims);
534
535         if (strcasestr(result, ".bmp") || strcasestr(result, ".jpeg") || strcasestr(result, ".png") || strcasestr(result, ".jpg"))
536                 sprintf(parsed_filename + EM_SAFE_STRLEN(parsed_filename), "%s", result);
537     else
538                 sprintf(parsed_filename + EM_SAFE_STRLEN(parsed_filename), "%s%s", result, ".jpeg");
539
540         EM_DEBUG_LOG_SEC(">>> FileName [ %s ] ", result);
541         EM_DEBUG_LOG_SEC("parsed_filename [%s] ", parsed_filename);
542         EM_DEBUG_FUNC_END();
543         return parsed_filename;
544 }
545 */
546
547 #define CONTENT_TYPE_STRING_IN_MIME_HEAEDER "Content-Type:"
548
549 INTERNAL_FUNC int emcore_get_content_type_from_mime_string(char *input_mime_string, char **output_content_type)
550 {
551         EM_DEBUG_FUNC_BEGIN("input_mime_string [%p], output_content_type [%p]", input_mime_string, output_content_type);
552
553         int   err = EMAIL_ERROR_NONE;
554         int   temp_mime_header_string_length = 0;
555         char  result_content_type[256] = { 0, };
556         char *temp_mime_header_string = NULL;
557         char *temp_content_type_start = NULL;
558         char *temp_content_type_end = NULL;
559
560         if (input_mime_string == NULL || output_content_type == NULL) {
561                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
562                 err = EMAIL_ERROR_INVALID_PARAM;
563                 goto FINISH_OFF;
564         }
565
566         temp_mime_header_string_length = EM_SAFE_STRLEN(input_mime_string);
567         temp_mime_header_string        = input_mime_string;
568
569         EM_DEBUG_LOG("temp_mime_header_string [%s]", temp_mime_header_string);
570
571         temp_content_type_start = strcasestr(temp_mime_header_string, CONTENT_TYPE_STRING_IN_MIME_HEAEDER);
572
573         if (temp_content_type_start == NULL) {
574                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_DATA");
575                 err = EMAIL_ERROR_INVALID_DATA;
576                 goto FINISH_OFF;
577         } else {
578                 temp_content_type_start += EM_SAFE_STRLEN(CONTENT_TYPE_STRING_IN_MIME_HEAEDER);
579                 temp_content_type_end = temp_content_type_start;
580
581                 while (temp_content_type_end && temp_content_type_end < (temp_mime_header_string + temp_mime_header_string_length) && *temp_content_type_end != ';')
582                         temp_content_type_end++;
583
584                 if (temp_content_type_end && *temp_content_type_end == ';') {
585                         if (temp_content_type_end - temp_content_type_start < 256) {
586                                 memcpy(result_content_type, temp_content_type_start, temp_content_type_end - temp_content_type_start);
587                                 EM_DEBUG_LOG_SEC("result_content_type [%s]", result_content_type);
588                                 *output_content_type = EM_SAFE_STRDUP(em_trim_left(result_content_type));
589                         } else {
590                                 EM_DEBUG_EXCEPTION("temp_content_type_end - temp_content_type_start [%ld]", (long int)(temp_content_type_end - temp_content_type_start));
591                                 err = EMAIL_ERROR_DATA_TOO_LONG;
592                                 goto FINISH_OFF;
593                         }
594                 }
595         }
596
597 FINISH_OFF:
598
599         EM_DEBUG_FUNC_END("err [%d]", err);
600         return err;
601 }
602
603 INTERNAL_FUNC int emcore_get_utf8_address(char **dest, ADDRESS *address, int *err_code)
604 {
605         EM_DEBUG_FUNC_BEGIN("dest[%p], address[%p], err_code[%p]", dest, address, err_code);
606
607         if (!dest || !address) {
608                 EM_DEBUG_EXCEPTION("dest[%p], address[%p]", dest, address);
609                 if (err_code != NULL)
610                         *err_code = EMAIL_ERROR_INVALID_PARAM;
611                 return false;
612         }
613
614         int ret = false;
615         int err = EMAIL_ERROR_NONE;
616
617         gchar *concatenated = NULL;
618         gchar *utf8_address = NULL;
619         gchar *temp = NULL;
620         char *nickname = NULL;
621
622         while (address) {
623                 if (address->personal) {
624                         if (!(nickname = emcore_gmime_get_decoding_text(address->personal))) {
625                                 EM_DEBUG_EXCEPTION("emcore_gmime_get_decoding_text failed - %d", err);
626                                 goto FINISH_OFF;
627                         }
628                         EM_DEBUG_LOG_DEV("nickname[%s]", nickname);
629                         if (*nickname != '\0') {
630                                 char *tmp_nickname = nickname;
631                                 nickname = em_get_escaped_str(tmp_nickname);
632                                 EM_SAFE_FREE(tmp_nickname);
633                                 utf8_address = g_strdup_printf("\"%s\" <%s%s%s>", nickname, address->mailbox ? address->mailbox : "", address->host ? "@" : "", address->host ? address->host : "");
634                         } else {
635                                 utf8_address = g_strdup_printf("<%s%s%s>", address->mailbox ? address->mailbox : "", address->host ? "@" : "", address->host ? address->host : "");
636                         }
637
638                         EM_SAFE_FREE(nickname);
639                 } else {
640                         if (address->mailbox || address->host)
641                                 utf8_address = g_strdup_printf("<%s%s%s>", address->mailbox ? address->mailbox : "", address->host ? "@" : "", address->host ? address->host : "");
642                 }
643
644                 EM_DEBUG_LOG_DEV("utf8_address[%s]", utf8_address);
645
646                 if (utf8_address) {
647                         if ((concatenated != NULL)) {
648                                 temp = concatenated;
649                                 concatenated = g_strdup_printf("%s; %s", temp, utf8_address);
650                                 g_free(temp);
651                         } else
652                                 concatenated = g_strdup(utf8_address);
653                 }
654
655                 g_free(utf8_address);
656                 utf8_address = NULL;
657
658                 address = address->next;
659         }
660
661         *dest = concatenated;
662
663         ret = true;
664
665 FINISH_OFF:
666         EM_SAFE_FREE(nickname);
667         EM_DEBUG_FUNC_END("ret[%d]", ret);
668         return ret;
669 }
670
671 #define SUBTYPE_STRING_LENGTH 128
672
673 INTERNAL_FUNC int emcore_get_content_type_from_mail_bodystruct(BODY *input_body, int input_buffer_length, char *output_content_type)
674 {
675         EM_DEBUG_FUNC_BEGIN("input_body [%p], input_buffer_length [%d], output_content_type [%p]", input_body, input_buffer_length, output_content_type);
676         int   err = EMAIL_ERROR_NONE;
677         char  subtype_string[SUBTYPE_STRING_LENGTH] = { 0 , };
678
679         if (input_body == NULL || output_content_type == NULL || input_buffer_length == 0) {
680                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
681                 err = EMAIL_ERROR_INVALID_PARAM;
682                 goto FINISH_OFF;
683         }
684
685         EM_SAFE_STRNCPY(subtype_string, input_body->subtype, SUBTYPE_STRING_LENGTH-1); /* prevent 21983 */
686         em_lower_string(subtype_string);
687
688         switch (input_body->type) {
689         case TYPETEXT:
690                 SNPRINTF(output_content_type, input_buffer_length, "text/%s", subtype_string);
691                 break;
692         case TYPEMULTIPART:
693                 SNPRINTF(output_content_type, input_buffer_length, "multipart/%s", subtype_string);
694                 break;
695         case TYPEMESSAGE:
696                 SNPRINTF(output_content_type, input_buffer_length, "message/%s", subtype_string);
697                 break;
698         case TYPEAPPLICATION:
699                 SNPRINTF(output_content_type, input_buffer_length, "application/%s", subtype_string);
700                 break;
701         case TYPEAUDIO:
702                 SNPRINTF(output_content_type, input_buffer_length, "audio/%s", subtype_string);
703                 break;
704         case TYPEIMAGE:
705                 SNPRINTF(output_content_type, input_buffer_length, "image/%s", subtype_string);
706                 break;
707         case TYPEVIDEO:
708                 SNPRINTF(output_content_type, input_buffer_length, "video/%s", subtype_string);
709                 break;
710         case TYPEMODEL:
711                 SNPRINTF(output_content_type, input_buffer_length, "model/%s", subtype_string);
712                 break;
713         case TYPEOTHER:
714                 SNPRINTF(output_content_type, input_buffer_length, "other/%s", subtype_string);
715                 break;
716         default:
717                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
718                 err = EMAIL_ERROR_INVALID_PARAM;
719                 goto FINISH_OFF;
720                 break;
721         }
722
723         EM_DEBUG_LOG_SEC("output_content_type [%s]", output_content_type);
724
725 FINISH_OFF:
726
727         EM_DEBUG_FUNC_END("err [%d]", err);
728         return err;
729 }
730
731 INTERNAL_FUNC int emcore_get_attribute_value_of_body_part(PARAMETER *input_param, char *atribute_name, char *output_value, int output_buffer_length, int with_rfc2047_text, int *err_code)
732 {
733         EM_DEBUG_FUNC_BEGIN_SEC("input_param [%p], atribute_name [%s], output_buffer_length [%d], with_rfc2047_text [%d]", input_param, atribute_name, output_buffer_length, with_rfc2047_text);
734         PARAMETER *temp_param = input_param;
735         char *decoded_value = NULL, *result_value = NULL;
736         int ret = false, err = EMAIL_ERROR_NONE;
737
738         if (!output_value) {
739                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
740                 err = EMAIL_ERROR_INVALID_PARAM;
741                 goto FINISH_OFF;
742         }
743
744         memset(output_value, 0, output_buffer_length);
745
746         while (temp_param) {
747                 if (!strcasecmp(temp_param->attribute, atribute_name)) {
748                         if (temp_param->value) {
749                                 if (with_rfc2047_text) {
750                                         decoded_value = emcore_gmime_get_decoding_text(temp_param->value);
751                                         result_value = EM_SAFE_STRDUP(decoded_value);
752                                         EM_SAFE_FREE(decoded_value);
753                                 } else
754                                         result_value = g_strdup(temp_param->value);
755                         } else {
756                                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_DATA_NOT_FOUND");
757                                 err = EMAIL_ERROR_DATA_NOT_FOUND;
758                                 goto FINISH_OFF;
759                         }
760
761                         if (result_value) {
762                                 if (output_buffer_length > EM_SAFE_STRLEN(result_value)) {
763                                         strncpy(output_value, result_value, output_buffer_length);
764                                         output_value[EM_SAFE_STRLEN(result_value)] = NULL_CHAR;
765                                         ret = true;
766                                 } else {
767                                         EM_DEBUG_EXCEPTION("buffer is too short");
768                                         err = EMAIL_ERROR_DATA_TOO_LONG;
769                                         goto FINISH_OFF;
770                                 }
771                         }
772
773                         break;
774                 }
775                 temp_param = temp_param->next;
776         }
777
778 FINISH_OFF:
779         EM_SAFE_FREE(decoded_value);
780         EM_SAFE_FREE(result_value);
781
782         if (err_code)
783                 *err_code = err;
784
785         EM_DEBUG_FUNC_END("ret [%d]", ret);
786         return ret;
787 }
788
789 /* get body structure */
790 INTERNAL_FUNC int emcore_get_body_structure(MAILSTREAM *stream, int msg_uid, BODY **body, int *err_code)
791 {
792         EM_DEBUG_FUNC_BEGIN("stream[%p], msg_uid[%d], body[%p], err_code[%p]", stream, msg_uid, body, err_code);
793
794         EM_IF_NULL_RETURN_VALUE(stream, false);
795         EM_IF_NULL_RETURN_VALUE(body, false);
796
797 #ifdef __FEATURE_HEADER_OPTIMIZATION__
798                 ENVELOPE *env = mail_fetch_structure(stream, msg_uid, body, FT_UID | FT_PEEK | FT_NOLOOKAHEAD, 1);
799 #else
800                 ENVELOPE *env = mail_fetch_structure(stream, msg_uid, body, FT_UID | FT_PEEK | FT_NOLOOKAHEAD);
801 #endif
802         if (!env) {
803                 if (err_code)
804                         *err_code = EMAIL_ERROR_MAIL_NOT_FOUND_ON_SERVER;
805                 EM_DEBUG_EXCEPTION("mail_fetch_structure failed");
806                 return FAILURE;
807         }
808
809 #ifdef FEATURE_CORE_DEBUG
810         _print_body(*body, true); /* shasikala.p@partner.samsung.com */
811 #endif
812         EM_DEBUG_FUNC_END();
813         return SUCCESS;
814 }
815
816 int emcore_set_fetch_part_section(BODY *body, char *section_pfx, int section_subno, int enable_inline_list, int *total_mail_size, int *total_body_size, int *err_code);
817
818 /* set body section to be fetched */
819 INTERNAL_FUNC int emcore_set_fetch_body_section(BODY *body, int enable_inline_list, int *total_mail_size, int *total_body_size, int *err_code)
820 {
821         EM_DEBUG_FUNC_BEGIN("body[%p], err_code[%p]", body, err_code);
822
823         if (!body) {
824                 EM_DEBUG_EXCEPTION("body [%p]", body);
825                 if (err_code != NULL)
826                         *err_code = EMAIL_ERROR_INVALID_PARAM;
827                 return FAILURE;
828         }
829
830 //      body->id = cpystr("1"); /*  top level body */
831         EM_DEBUG_LOG_SEC("body->id : [%s]", body->id);
832
833         if (enable_inline_list) {
834                 g_inline_count = 0;
835                 EM_SAFE_FREE(g_inline_list);
836         }
837
838         emcore_set_fetch_part_section(body, (char *)NULL, 0, enable_inline_list, total_mail_size, total_body_size, err_code);
839
840         if (body && body->id)
841                 EM_DEBUG_LOG_SEC(">>>>> FILE NAME [%s] ", body->id);
842         else
843                 EM_DEBUG_LOG(">>>>> BODY NULL ");
844
845         EM_DEBUG_FUNC_END();
846         return SUCCESS;
847 }
848
849 /* set part section of body to be fetched */
850 int emcore_set_fetch_part_section(BODY *body, char *section_pfx, int section_subno, int enable_inline_list, int *total_mail_size, int *total_body_size, int *err_code)
851 {
852         EM_DEBUG_FUNC_BEGIN("body[%p], section_pfx[%s], section_subno[%d], enable_inline_list[%d], total_mail_size[%p] err_code[%p]", body, section_pfx, section_subno, enable_inline_list, total_mail_size, err_code);
853
854         PART *part = NULL;
855         char section[64] = {0x00, };
856
857         /* multipart doesn't have a row to itself */
858         if (body->type == TYPEMULTIPART) {
859                 /* if not first time, extend prefix */
860                 if (section_pfx) {
861                         SNPRINTF(section, sizeof(section), "%s%d.", section_pfx, ++section_subno);
862                 } else {
863                         section[0] = '\0';
864                 }
865
866                 for (section_subno = 0, part = body->nested.part; part; part = part->next)
867                         emcore_set_fetch_part_section(&part->body, section, section_subno++, enable_inline_list, total_mail_size, total_body_size, err_code);
868         } else {
869                 if (!section_pfx) /* dummy prefix if top level */
870                         section_pfx = "";
871
872                 SNPRINTF(section, sizeof(section), "%s%d", section_pfx, ++section_subno);
873
874                 if (enable_inline_list && ((body->disposition.type && (body->disposition.type[0] == 'i' || body->disposition.type[0] == 'I')) ||
875                         (!body->disposition.type && body->id))) {
876                         BODY **temp = NULL;
877                         temp =  realloc(g_inline_list, sizeof(BODY *) *(g_inline_count + 1));
878                         if (NULL != temp) {
879                                 memset(temp+g_inline_count, 0x00, sizeof(BODY *));
880                                 g_inline_list = temp;
881                                 g_inline_list[g_inline_count] = body;
882                                 g_inline_count++;
883                                 temp = NULL;
884                         } else {
885                                 EM_DEBUG_EXCEPTION("realloc fails");
886                         }
887
888                         EM_DEBUG_LOG("Update g_inline_list with inline count [%d]", g_inline_count);
889                 }
890
891                 if (total_mail_size != NULL) {
892                         *total_mail_size = *total_mail_size + (int)body->size.bytes;
893                         EM_DEBUG_LOG("body->size.bytes [%lu]", body->size.bytes);
894                         EM_DEBUG_LOG("total_mail_size [%d]", *total_mail_size);
895                 }
896
897                 if ((total_body_size != NULL) && !(body->disposition.type && (body->disposition.type[0] == 'a' || body->disposition.type[0] == 'A'))) {
898                         *total_body_size = *total_body_size + (int)body->size.bytes;
899                         EM_DEBUG_LOG("body->size.bytes [%lu]", body->size.bytes);
900                         EM_DEBUG_LOG("total_mail_size [%d]", *total_body_size);
901                 }
902
903                 /* encapsulated message ? */
904                 if ((body->type == TYPEMESSAGE) && !strcasecmp(body->subtype, "RFC822") && (body = ((MESSAGE *)body->nested.msg)->body)) {
905                         if (body->type == TYPEMULTIPART) {
906                                 section[0] = '\0';
907                                 emcore_set_fetch_part_section(body, section, section_subno-1, enable_inline_list, total_mail_size, total_body_size, err_code);
908                         } else {                /*  build encapsulation prefi */
909                                 SNPRINTF(section, sizeof(section), "%s%d.", section_pfx, section_subno);
910                                 emcore_set_fetch_part_section(body, section, 0, enable_inline_list, total_mail_size, total_body_size, err_code);
911                         }
912                 } else {
913                         /* set body section */
914                         if (body)
915                                 body->sparep = cpystr(section);
916                 }
917         }
918         EM_DEBUG_FUNC_END();
919         return SUCCESS;
920 }
921 static int emcore_make_mail_data_from_m_mesg(email_mail_data_t *dst_mail_data, struct _m_mesg *mmsg)
922 {
923         EM_DEBUG_FUNC_BEGIN();
924         int err = EMAIL_ERROR_NONE;
925
926         if (!mmsg || !dst_mail_data) {
927                         EM_DEBUG_EXCEPTION("Invalid parameter");
928                         err = EMAIL_ERROR_INVALID_PARAM;
929                         return err;
930         }
931
932         char *encoded_subject = NULL;
933         char *first_address = NULL;
934         char *first_alias = NULL;
935         struct tm temp_time_info;
936         ADDRESS *from = NULL;
937         ADDRESS *to = NULL;
938         ADDRESS *cc = NULL;
939         ADDRESS *bcc = NULL;
940         ADDRESS *return_path = NULL;
941         ADDRESS *reply_to = NULL;
942         MESSAGECACHE mail_cache_element = {0, };
943         memset(&mail_cache_element, 0x00, sizeof(MESSAGECACHE));
944         memset((void *)&temp_time_info, 0, sizeof(struct tm));
945         /* Set the smime_type */
946         if (mmsg->rfc822header && mmsg->rfc822header->content_type) {
947                 if (strcasestr(mmsg->rfc822header->content_type, "pkcs7-mime")) {
948                         if (strcasestr(mmsg->rfc822header->content_type, "enveloped-data"))
949                                 dst_mail_data->smime_type = EMAIL_SMIME_ENCRYPTED;
950                         else
951                                 dst_mail_data->smime_type = EMAIL_SMIME_SIGNED_AND_ENCRYPTED;
952                 } else if (strcasestr(mmsg->rfc822header->content_type, "encrypted")) {
953                         dst_mail_data->smime_type = EMAIL_PGP_ENCRYPTED;
954                 } else if (strcasestr(mmsg->rfc822header->content_type, "signed")) {
955                         if (strcasestr(mmsg->rfc822header->content_type, "pkcs7-signature"))
956                                 dst_mail_data->smime_type = EMAIL_SMIME_SIGNED;
957                         else
958                                 dst_mail_data->smime_type = EMAIL_PGP_SIGNED;
959
960                         dst_mail_data->digest_type = emcore_get_digest_type(mmsg->rfc822header->content_type);
961                 } else {
962                         dst_mail_data->smime_type = EMAIL_SMIME_NONE;
963                 }
964         }
965
966         /* Set the priority */
967         if (mmsg->rfc822header) {
968                 if (mmsg->rfc822header->priority) {
969                         switch (atoi(mmsg->rfc822header->priority)) {
970                         case 1:
971                                 dst_mail_data->priority = EMAIL_MAIL_PRIORITY_HIGH;
972                                 break;
973                         case 5:
974                                 dst_mail_data->priority = EMAIL_MAIL_PRIORITY_LOW;
975                                 break;
976                         case 3:
977                         default:
978                                 dst_mail_data->priority = EMAIL_MAIL_PRIORITY_NORMAL;
979                                 break;
980                         }
981                 } else if (mmsg->rfc822header->ms_priority) {
982                         if (strcasestr(mmsg->rfc822header->ms_priority, "HIGH"))
983                                 dst_mail_data->priority = EMAIL_MAIL_PRIORITY_HIGH;
984                         else if (strcasestr(mmsg->rfc822header->ms_priority, "NORMAL"))
985                                 dst_mail_data->priority = EMAIL_MAIL_PRIORITY_NORMAL;
986                         else if (strcasestr(mmsg->rfc822header->ms_priority, "LOW"))
987                                 dst_mail_data->priority = EMAIL_MAIL_PRIORITY_LOW;
988                         else
989                                 dst_mail_data->priority = EMAIL_MAIL_PRIORITY_NORMAL;
990                 }
991         } else if (mmsg->nested.body) {
992                 if (mmsg->nested.body->part_header->priority) {
993                         switch (atoi(mmsg->nested.body->part_header->priority)) {
994                         case 1:
995                                 dst_mail_data->priority = EMAIL_MAIL_PRIORITY_HIGH;
996                                 break;
997                         case 5:
998                                 dst_mail_data->priority = EMAIL_MAIL_PRIORITY_LOW;
999                                 break;
1000                         case 3:
1001                         default:
1002                                 dst_mail_data->priority = EMAIL_MAIL_PRIORITY_NORMAL;
1003                                 break;
1004                         }
1005                 } else if (mmsg->nested.body->part_header->ms_priority) {
1006                         if (strcasestr(mmsg->nested.body->part_header->ms_priority, "HIGH"))
1007                                 dst_mail_data->priority = EMAIL_MAIL_PRIORITY_HIGH;
1008                         else if (strcasestr(mmsg->nested.body->part_header->ms_priority, "NORMAL"))
1009                                 dst_mail_data->priority = EMAIL_MAIL_PRIORITY_NORMAL;
1010                         else if (strcasestr(mmsg->nested.body->part_header->ms_priority, "LOW"))
1011                                 dst_mail_data->priority = EMAIL_MAIL_PRIORITY_LOW;
1012                         else
1013                                 dst_mail_data->priority = EMAIL_MAIL_PRIORITY_NORMAL;
1014                 }
1015         }
1016
1017         if (!mmsg->rfc822header) {
1018                 EM_DEBUG_LOG("This mail did not have envelop");
1019                 return err;
1020         }
1021
1022         /* Set the date */
1023         if (mmsg->rfc822header->date) {
1024                         EM_DEBUG_LOG("date : [%s]", mmsg->rfc822header->date);
1025                         mail_parse_date(&mail_cache_element, (unsigned char *)mmsg->rfc822header->date);
1026         }
1027
1028         temp_time_info.tm_sec = mail_cache_element.seconds;
1029         temp_time_info.tm_min = mail_cache_element.minutes - mail_cache_element.zminutes;
1030         temp_time_info.tm_hour = mail_cache_element.hours - mail_cache_element.zhours;
1031
1032         if (mail_cache_element.hours - mail_cache_element.zhours < 0) {
1033                 temp_time_info.tm_mday = mail_cache_element.day - 1;
1034                 temp_time_info.tm_hour += 24;
1035         } else
1036                 temp_time_info.tm_mday = mail_cache_element.day;
1037
1038         temp_time_info.tm_mon = mail_cache_element.month - 1;
1039         temp_time_info.tm_year = mail_cache_element.year + 70;
1040
1041         dst_mail_data->date_time                   = timegm(&temp_time_info);
1042
1043
1044         /* Set the subject */
1045         dst_mail_data->subject = EM_SAFE_STRDUP(mmsg->rfc822header->subject);
1046
1047         /* Set the email address(from, to, cc, bcc, received ...) */
1048         dst_mail_data->email_address_recipient     = EM_SAFE_STRDUP(mmsg->rfc822header->received);
1049
1050         if (mmsg->rfc822header->from) {
1051                 rfc822_parse_adrlist(&from, mmsg->rfc822header->from, NULL);
1052                 if (!emcore_get_utf8_address(&dst_mail_data->full_address_from, from, &err)) {
1053                         EM_DEBUG_EXCEPTION_SEC("emcore_get_utf8_address failed : [%d], [%s]", err, mmsg->rfc822header->from);
1054                 }
1055         }
1056
1057         if (mmsg->rfc822header->to) {
1058                 rfc822_parse_adrlist(&to, mmsg->rfc822header->to, NULL);
1059                 if (!emcore_get_utf8_address(&dst_mail_data->full_address_to, to, &err)) {
1060                         EM_DEBUG_EXCEPTION_SEC("emcore_get_utf8_address failed : [%d], [%s]", err, mmsg->rfc822header->to);
1061                 }
1062         }
1063
1064         if (mmsg->rfc822header->cc) {
1065                 rfc822_parse_adrlist(&cc, mmsg->rfc822header->cc, NULL);
1066                 if (!emcore_get_utf8_address(&dst_mail_data->full_address_cc, cc, &err)) {
1067                         EM_DEBUG_EXCEPTION_SEC("emcore_get_utf8_address failed : [%d], [%s]", err, mmsg->rfc822header->cc);
1068                 }
1069         }
1070
1071         if (mmsg->rfc822header->bcc) {
1072                 rfc822_parse_adrlist(&bcc, mmsg->rfc822header->bcc, NULL);
1073                 if (!emcore_get_utf8_address(&dst_mail_data->full_address_bcc, bcc, &err)) {
1074                         EM_DEBUG_EXCEPTION_SEC("emcore_get_utf8_address failed : [%d], [%s]", err, mmsg->rfc822header->bcc);
1075                 }
1076         }
1077
1078         if (mmsg->rfc822header->return_path) {
1079                 rfc822_parse_adrlist(&return_path, mmsg->rfc822header->return_path, NULL);
1080                 if (!emcore_get_utf8_address(&dst_mail_data->full_address_return, return_path, &err)) {
1081                         EM_DEBUG_EXCEPTION_SEC("emcore_get_utf8_address failed : [%d], [%s]", err, mmsg->rfc822header->return_path);
1082                 }
1083         }
1084
1085         if (mmsg->rfc822header->reply_to) {
1086                 rfc822_parse_adrlist(&reply_to, mmsg->rfc822header->reply_to, NULL);
1087                 if (!emcore_get_utf8_address(&dst_mail_data->full_address_reply, reply_to, &err)) {
1088                         EM_DEBUG_EXCEPTION_SEC("emcore_get_utf8_address failed : [%d], [%s]", err, mmsg->rfc822header->reply_to);
1089                 }
1090         }
1091
1092         if (emcore_get_first_address(dst_mail_data->full_address_from, &first_alias, &first_address) == true) {
1093                 dst_mail_data->alias_sender = EM_SAFE_STRDUP(first_alias);
1094                 dst_mail_data->email_address_sender = EM_SAFE_STRDUP(first_address);
1095         }
1096
1097         EM_SAFE_FREE(encoded_subject);
1098         EM_SAFE_FREE(first_alias);
1099         EM_SAFE_FREE(first_address);
1100
1101         if (from)
1102                 mail_free_address(&from);
1103
1104         if (to)
1105                 mail_free_address(&to);
1106
1107         if (cc)
1108                 mail_free_address(&cc);
1109
1110         if (bcc)
1111                 mail_free_address(&bcc);
1112
1113         if (return_path)
1114                 mail_free_address(&return_path);
1115
1116         if (reply_to)
1117                 mail_free_address(&reply_to);
1118         EM_DEBUG_FUNC_END("err : [%d]", err);
1119         return err;
1120 }
1121
1122 INTERNAL_FUNC int emcore_make_mail_data_from_mime_data(struct _m_mesg *mmsg,
1123                                                                                                         struct _m_content_info *cnt_info,
1124                                                                                                         email_mail_data_t **output_mail_data,
1125                                                                                                         email_attachment_data_t **output_attachment_data,
1126                                                                                                         int *output_attachment_count,
1127                                                                                                         int *err_code)
1128 {
1129         EM_DEBUG_FUNC_BEGIN();
1130         int i = 0;
1131         int ret = false;
1132         int eml_mail_id = 0;
1133         int err = EMAIL_ERROR_NONE;
1134         int save_status = EMAIL_BODY_DOWNLOAD_STATUS_NONE;
1135         int attachment_num = 0;
1136         int local_attachment_count = 0;
1137         int local_inline_content_count = 0;
1138         char move_buf[512];
1139         char path_buf[512];
1140         char html_body[MAX_PATH] = {0, };
1141         char *multi_user_name = NULL;
1142         struct timeval tv;
1143         struct attachment_info *ai = NULL;
1144         email_attachment_data_t *attachment = NULL;
1145         email_mail_data_t *p_mail_data = NULL;
1146
1147         if (!mmsg || !cnt_info || !output_mail_data || !output_attachment_data) {
1148                 EM_DEBUG_EXCEPTION("Invalid parameter");
1149                 err = EMAIL_ERROR_INVALID_PARAM;
1150                 goto FINISH_OFF;
1151         }
1152
1153         /* Create rand mail id of eml */
1154         gettimeofday(&tv, NULL);
1155         srand(tv.tv_usec);
1156         unsigned int seed = time(NULL);
1157         eml_mail_id = rand_r(&seed);
1158
1159         p_mail_data = (email_mail_data_t *)em_malloc(sizeof(email_mail_data_t));
1160         if (p_mail_data == NULL) {
1161                 EM_DEBUG_EXCEPTION("em_mallocfailed");
1162                 err = EMAIL_ERROR_OUT_OF_MEMORY;
1163                 goto FINISH_OFF;
1164         }
1165
1166         p_mail_data->mail_id = eml_mail_id;
1167         p_mail_data->account_id = EML_FOLDER;
1168         p_mail_data->mail_size = cnt_info->total_body_size;
1169
1170         if ((err = emcore_make_mail_data_from_m_mesg(p_mail_data, mmsg)) != EMAIL_ERROR_NONE) {
1171                         EM_DEBUG_EXCEPTION("emcore_make_mail_data_from_m_mesg failed : [%d]", err);
1172         }
1173
1174         EM_DEBUG_LOG("cnt_info->text.plain [%s], cnt_info->text.html [%s]", cnt_info->text.plain, cnt_info->text.html);
1175
1176         if (cnt_info->text.plain) {
1177                 memset(move_buf, 0x00, sizeof(move_buf));
1178                 memset(path_buf, 0x00, sizeof(path_buf));
1179                 EM_DEBUG_LOG("cnt_info->text.plain [%s]", cnt_info->text.plain);
1180                 if (!emstorage_create_dir(multi_user_name, EML_FOLDER, eml_mail_id, 0, &err)) {
1181                         EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
1182                         goto FINISH_OFF;
1183                 }
1184
1185                 if (!emstorage_get_save_name(multi_user_name, EML_FOLDER, eml_mail_id, 0,
1186                                                                         cnt_info->text.plain_charset ? cnt_info->text.plain_charset : UNKNOWN_CHARSET_PLAIN_TEXT_FILE,
1187                                                                         move_buf, path_buf, sizeof(path_buf), &err)) {
1188                         EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
1189                         goto FINISH_OFF;
1190                 }
1191
1192                 if (!emstorage_move_file(cnt_info->text.plain, move_buf, false, &err)) {
1193                         EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
1194                         goto FINISH_OFF;
1195                 }
1196
1197                 p_mail_data->file_path_plain = EM_SAFE_STRDUP(path_buf);
1198                 save_status = cnt_info->text.plain_save_status;
1199         }
1200
1201         if (cnt_info->text.html) {
1202                 memset(move_buf, 0x00, sizeof(move_buf));
1203                 memset(path_buf, 0x00, sizeof(path_buf));
1204                 if (!emstorage_create_dir(multi_user_name, EML_FOLDER, eml_mail_id, 0, &err)) {
1205                         EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
1206                         goto FINISH_OFF;
1207                 }
1208
1209                 if (cnt_info->text.html_charset != NULL) {
1210                         SNPRINTF(html_body, MAX_PATH, "%s%s", cnt_info->text.html_charset, HTML_EXTENSION_STRING);
1211                 } else {
1212                         g_strlcpy(html_body, UNKNOWN_CHARSET_HTML_TEXT_FILE, MAX_PATH);
1213                 }
1214
1215                 if (!emstorage_get_save_name(multi_user_name, EML_FOLDER, eml_mail_id,
1216                                                                         0, html_body, move_buf, path_buf,
1217                                                                         sizeof(path_buf), &err)) {
1218                         EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
1219                         goto FINISH_OFF;
1220                 }
1221
1222                 if (!emstorage_move_file(cnt_info->text.html, move_buf, false, &err)) {
1223                         EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
1224                         goto FINISH_OFF;
1225                 }
1226
1227                 p_mail_data->file_path_html = EM_SAFE_STRDUP(path_buf);
1228                 save_status = cnt_info->text.html_save_status;
1229         }
1230
1231         if (cnt_info->text.mime_entity) {
1232                 memset(move_buf, 0x00, sizeof(move_buf));
1233                 memset(path_buf, 0x00, sizeof(path_buf));
1234
1235                 if (!emstorage_create_dir(multi_user_name, EML_FOLDER, eml_mail_id, 0, &err)) {
1236                         EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
1237                         goto FINISH_OFF;
1238                 }
1239
1240                 if (!emstorage_get_save_name(multi_user_name, EML_FOLDER, eml_mail_id,
1241                                                                         0, "mime_entity", move_buf, path_buf,
1242                                                                         sizeof(path_buf), &err)) {
1243                         EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
1244                         goto FINISH_OFF;
1245                 }
1246
1247                 if (!emstorage_move_file(cnt_info->text.mime_entity, move_buf, false, &err)) {
1248                         EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
1249                         goto FINISH_OFF;
1250                 }
1251
1252                 p_mail_data->file_path_mime_entity = EM_SAFE_STRDUP(path_buf);
1253         }
1254
1255         if ((err = emcore_get_preview_text_from_file(multi_user_name,
1256                                                 p_mail_data->file_path_plain,
1257                                                 p_mail_data->file_path_html,
1258                                                 MAX_PREVIEW_TEXT_LENGTH,
1259                                                 &p_mail_data->preview_text)) != EMAIL_ERROR_NONE)
1260                 EM_DEBUG_EXCEPTION("emcore_get_preview_text_from_file error [%d]", err);
1261
1262         for (ai = cnt_info->file; ai; ai = ai->next, local_attachment_count++) {}
1263         EM_DEBUG_LOG("local_attachment_count : [%d]", local_attachment_count);
1264
1265         for (ai = cnt_info->inline_file; ai; ai = ai->next, local_inline_content_count++) {}
1266         EM_DEBUG_LOG("local_inline_content_count : [%d]", local_inline_content_count);
1267
1268         attachment_num = local_attachment_count + local_inline_content_count;
1269
1270         if (attachment_num > 0) {
1271                 attachment = (email_attachment_data_t *)em_malloc(sizeof(email_attachment_data_t) * attachment_num);
1272                 if (attachment == NULL) {
1273                         EM_DEBUG_EXCEPTION("em_mallocfailed");
1274                         err = EMAIL_ERROR_OUT_OF_MEMORY;
1275                         goto FINISH_OFF;
1276                 }
1277
1278                 for (ai = cnt_info->file; ai; ai = ai->next, i++) {
1279                         attachment[i].attachment_id          = i + 1;
1280                         attachment[i].attachment_size        = ai->size;
1281                         attachment[i].attachment_name        = EM_SAFE_STRDUP(ai->name);
1282                         attachment[i].content_id             = EM_SAFE_STRDUP(ai->content_id);
1283                         attachment[i].drm_status             = ai->drm;
1284                         attachment[i].save_status            = ai->save_status;
1285                         attachment[i].inline_content_status  = 0;
1286                         attachment[i].attachment_mime_type   = EM_SAFE_STRDUP(ai->attachment_mime_type);
1287 #ifdef __ATTACHMENT_OPTI__
1288                         attachment[i].encoding               = ai->encoding;
1289                         attachment[i].section                = ai->section;
1290 #endif
1291                         EM_DEBUG_LOG("attachment[%d].attachment_id[%d]", i, attachment[i].attachment_id);
1292                         EM_DEBUG_LOG("attachment[%d].attachment_size[%d]", i, attachment[i].attachment_size);
1293                         EM_DEBUG_LOG_SEC("attachment[%d].attachment_name[%s]", i, attachment[i].attachment_name);
1294                         EM_DEBUG_LOG("attachment[%d].drm_status[%d]", i, attachment[i].drm_status);
1295                         EM_DEBUG_LOG("attachment[%d].inline_content_status[%d]", i, attachment[i].inline_content_status);
1296                         EM_DEBUG_LOG("attachment[%d].attachment_mime_type : [%s]", i, attachment[i].attachment_mime_type);
1297
1298                         if (ai->save) {
1299                                 memset(move_buf, 0x00, sizeof(move_buf));
1300                                 memset(path_buf, 0x00, sizeof(path_buf));
1301
1302                                 if (!emstorage_create_dir(multi_user_name, EML_FOLDER, eml_mail_id, i + 1, &err)) {
1303                                         EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
1304                                         goto FINISH_OFF;
1305                                 }
1306
1307                                 if (!emstorage_get_save_name(multi_user_name, EML_FOLDER, eml_mail_id,
1308                                                                                         i + 1, attachment[i].attachment_name, move_buf,
1309                                                                                         path_buf, sizeof(path_buf), &err)) {
1310                                         EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
1311                                         goto FINISH_OFF;
1312                                 }
1313
1314                                 if (!emstorage_move_file(ai->save, move_buf, false, &err)) {
1315                                         EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
1316
1317                                         /*  delete all created files. */
1318                                         if (!emstorage_get_save_name(multi_user_name, EML_FOLDER, eml_mail_id,
1319                                                                                                 0, NULL, move_buf, path_buf,
1320                                                                                                 sizeof(path_buf), NULL)) {
1321                                                 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed...");
1322                                                 /* goto FINISH_OFF; */
1323                                         }
1324
1325                                         if (!emstorage_delete_dir(move_buf, NULL)) {
1326                                                 EM_DEBUG_EXCEPTION("emstorage_delete_dir failed...");
1327                                                 /* goto FINISH_OFF; */
1328                                         }
1329
1330
1331                                         goto FINISH_OFF;
1332                                 }
1333
1334                                 attachment[i].attachment_path = EM_SAFE_STRDUP(path_buf);
1335                         }
1336
1337                         EM_DEBUG_LOG_SEC("attachment[%d].attachment_path[%s]", i, attachment[i].attachment_path);
1338                         save_status = ai->save_status;
1339                 }
1340
1341                 for (ai = cnt_info->inline_file; ai; ai = ai->next, i++) {
1342                         attachment[i].attachment_id          = i + 1;
1343                         attachment[i].attachment_size        = ai->size;
1344                         attachment[i].attachment_name        = EM_SAFE_STRDUP(ai->name);
1345                         attachment[i].content_id             = EM_SAFE_STRDUP(ai->content_id);
1346                         attachment[i].drm_status             = ai->drm;
1347                         attachment[i].save_status            = ai->save_status;
1348                         attachment[i].inline_content_status  = INLINE_ATTACHMENT;
1349                         attachment[i].attachment_mime_type   = EM_SAFE_STRDUP(ai->attachment_mime_type);
1350 #ifdef __ATTACHMENT_OPTI__
1351                         attachment[i].encoding               = ai->encoding;
1352                         attachment[i].section                = ai->section;
1353 #endif
1354                         EM_DEBUG_LOG("attachment[%d].attachment_id[%d]", i, attachment[i].attachment_id);
1355                         EM_DEBUG_LOG("attachment[%d].attachment_size[%d]", i, attachment[i].attachment_size);
1356                         EM_DEBUG_LOG_SEC("attachment[%d].attachment_name[%s]", i, attachment[i].attachment_name);
1357                         EM_DEBUG_LOG("attachment[%d].drm_status[%d]", i, attachment[i].drm_status);
1358                         EM_DEBUG_LOG("attachment[%d].inline_content_status[%d]", i, attachment[i].inline_content_status);
1359                         EM_DEBUG_LOG("attachment[%d].attachment_mime_type : [%s]", i, attachment[i].attachment_mime_type);
1360
1361                         if (ai->save) {
1362                                 memset(move_buf, 0x00, sizeof(move_buf));
1363                                 memset(path_buf, 0x00, sizeof(path_buf));
1364
1365                                 if (!emstorage_create_dir(multi_user_name, EML_FOLDER, eml_mail_id, 0, &err)) {
1366                                         EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
1367                                         goto FINISH_OFF;
1368                                 }
1369
1370                                 if (!emstorage_get_save_name(multi_user_name, EML_FOLDER, eml_mail_id, 0,
1371                                                                                         attachment[i].attachment_name, move_buf, path_buf,
1372                                                                                         sizeof(path_buf), &err)) {
1373                                         EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
1374                                         goto FINISH_OFF;
1375                                 }
1376
1377                                 if (!emstorage_move_file(ai->save, move_buf, false, &err)) {
1378                                         EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
1379
1380                                         /*  delete all created files. */
1381                                         if (!emstorage_get_save_name(multi_user_name, EML_FOLDER, eml_mail_id, 0,
1382                                                                                                 NULL, move_buf, path_buf, sizeof(path_buf), NULL)) {
1383                                                 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed...");
1384                                                 /* goto FINISH_OFF; */
1385                                         }
1386
1387                                         if (!emstorage_delete_dir(move_buf, NULL)) {
1388                                                 EM_DEBUG_EXCEPTION("emstorage_delete_dir failed...");
1389                                                 /* goto FINISH_OFF; */
1390                                         }
1391
1392
1393                                         goto FINISH_OFF;
1394                                 }
1395
1396                                 attachment[i].attachment_path = EM_SAFE_STRDUP(path_buf);
1397                         }
1398
1399                         EM_DEBUG_LOG_SEC("attachment[%d].attachment_path[%s]", i, attachment[i].attachment_path);
1400                         save_status = ai->save_status;
1401                 }
1402         }
1403         EM_DEBUG_LOG("Check #1");
1404         EM_DEBUG_LOG("save_status : [%d]", save_status);
1405
1406         p_mail_data->attachment_count = local_attachment_count;
1407         p_mail_data->inline_content_count = local_inline_content_count;
1408         p_mail_data->body_download_status = (save_status == EMAIL_BODY_DOWNLOAD_STATUS_FULLY_DOWNLOADED) ? 1 : 0;
1409
1410         eml_data_count += 1;
1411         ret = true;
1412
1413 FINISH_OFF:
1414
1415         if (ret) {
1416                 if (output_mail_data)
1417                         *output_mail_data = p_mail_data;
1418
1419                 if (output_attachment_data)
1420                         *output_attachment_data = attachment;
1421
1422                 if (output_attachment_count)
1423                         *output_attachment_count = attachment_num;
1424         } else {
1425                 if (p_mail_data) {
1426                         emcore_free_mail_data(p_mail_data);
1427                         EM_SAFE_FREE(p_mail_data);
1428                 }
1429
1430                 if (attachment)
1431                         emcore_free_attachment_data(&attachment, attachment_num, NULL);
1432         }
1433
1434
1435         if (err_code)
1436                 *err_code = err;
1437
1438         EM_DEBUG_FUNC_END("ret : [%d]", ret);
1439         return ret;
1440 }
1441
1442 INTERNAL_FUNC int emcore_parse_mime_file_to_mail(char *eml_file_path,
1443                                                                                                 email_mail_data_t **output_mail_data,
1444                                                                                                 email_attachment_data_t **output_attachment_data,
1445                                                                                                 int *output_attachment_count,
1446                                                                                                 int *err_code)
1447 {
1448         EM_DEBUG_FUNC_BEGIN_SEC("eml_file_path : [%s], output_mail_data : [%p]", eml_file_path, output_mail_data);
1449
1450         int err = EMAIL_ERROR_NONE;
1451         int ret = false;
1452         char *mime_entity = NULL;
1453         struct _m_content_info *cnt_info = NULL;
1454         struct _m_mesg *mmsg = NULL;
1455
1456
1457         if (!eml_file_path || !output_mail_data || !output_attachment_data || !output_attachment_count) {
1458                 EM_DEBUG_EXCEPTION("Invalid paramter");
1459                 err = EMAIL_ERROR_INVALID_PARAM;
1460                 goto FINISH_OFF;
1461         }
1462
1463         cnt_info = (struct _m_content_info *)em_malloc(sizeof(struct _m_content_info));
1464         if (cnt_info == NULL) {
1465                 EM_DEBUG_EXCEPTION("em_mallocfailed...");
1466                 err = EMAIL_ERROR_OUT_OF_MEMORY;
1467                 goto FINISH_OFF;
1468         }
1469
1470         cnt_info->grab_type = GRAB_TYPE_TEXT | GRAB_TYPE_ATTACHMENT;
1471
1472         mmsg = (struct _m_mesg *)em_malloc(sizeof(struct _m_mesg));
1473         if (mmsg == NULL) {
1474                 EM_DEBUG_EXCEPTION("em_mallocfailed");
1475                 err = EMAIL_ERROR_OUT_OF_MEMORY;
1476                 goto FINISH_OFF;
1477         }
1478
1479         mmsg->rfc822header = (struct _rfc822header *)em_malloc(sizeof(struct _rfc822header));
1480         if (mmsg->rfc822header == NULL) {
1481                 EM_DEBUG_EXCEPTION("em_mallocfailed");
1482                 err = EMAIL_ERROR_OUT_OF_MEMORY;
1483                 goto FINISH_OFF;
1484         }
1485
1486         emcore_gmime_init();
1487         if (!emcore_gmime_eml_parse_mime(eml_file_path, mmsg->rfc822header, cnt_info, &err)) {
1488                 emcore_gmime_shutdown();
1489                 EM_DEBUG_EXCEPTION("emcore_gmime_parse_mime failed : [%d]", err);
1490                 err = EMAIL_ERROR_INVALID_DATA;
1491                 goto FINISH_OFF;
1492         }
1493
1494         if (!emcore_make_mail_data_from_mime_data(mmsg,
1495                                                                                         cnt_info,
1496                                                                                         output_mail_data,
1497                                                                                         output_attachment_data,
1498                                                                                         output_attachment_count,
1499                                                                                         &err)) {
1500                 emcore_gmime_shutdown();
1501                 EM_DEBUG_EXCEPTION("emcore_make_mail_tbl_data_from_mime failed : [%d]", err);
1502                 goto FINISH_OFF;
1503
1504         }
1505         emcore_gmime_shutdown();
1506
1507         ret = true;
1508
1509 FINISH_OFF:
1510
1511         if (mmsg)
1512                 emcore_mime_free_mime(mmsg);
1513
1514         if (cnt_info) {
1515                 emcore_free_content_info(cnt_info);
1516                 EM_SAFE_FREE(cnt_info);
1517         }
1518
1519         EM_SAFE_FREE(mime_entity);
1520
1521         if (err_code)
1522                 *err_code = err;
1523
1524         EM_DEBUG_FUNC_END("err : %d", err);
1525         return ret;
1526 }
1527
1528 INTERNAL_FUNC int emcore_delete_parsed_data(char *multi_user_name, email_mail_data_t *input_mail_data, int *err_code)
1529 {
1530         EM_DEBUG_FUNC_BEGIN("input_mail_data : [%p]", input_mail_data);
1531         int err = EMAIL_ERROR_NONE;
1532         int ret = false;
1533         char buf[MAX_PATH] = {0};
1534         char *prefix_path = NULL;
1535
1536         if (!input_mail_data) {
1537                 EM_DEBUG_EXCEPTION("Invliad parameter");
1538                 err = EMAIL_ERROR_INVALID_PARAM;
1539                 goto FINISH_OFF;
1540         }
1541
1542         if ((input_mail_data->account_id != EML_FOLDER) && (!input_mail_data->mail_id)) {
1543                 EM_DEBUG_EXCEPTION("Invliad parameter: account_id[%d], mail_id[%d]", input_mail_data->account_id, input_mail_data->mail_id);
1544                 err = EMAIL_ERROR_INVALID_PARAM;
1545                 goto FINISH_OFF;
1546         }
1547         if (EM_SAFE_STRLEN(multi_user_name) > 0) {
1548                 err = emcore_get_container_path(multi_user_name, &prefix_path);
1549                 if (err != EMAIL_ERROR_NONE) {
1550                         EM_DEBUG_EXCEPTION("emcore_get_container_path failed :[%d]", err);
1551                         goto FINISH_OFF;
1552                 }
1553         } else {
1554                 prefix_path = strdup("");
1555         }
1556
1557         if (!input_mail_data->mail_id) {
1558                 SNPRINTF(buf, sizeof(buf), "%s%s%s%d", prefix_path, MAIL_HOME, DIR_SEPERATOR, input_mail_data->account_id);
1559         } else {
1560                 SNPRINTF(buf, sizeof(buf), "%s%s%s%d%s%d", prefix_path, MAIL_HOME, DIR_SEPERATOR, input_mail_data->account_id, DIR_SEPERATOR, input_mail_data->mail_id);
1561         }
1562
1563         if (!emstorage_delete_dir(buf, &err)) {
1564                 EM_DEBUG_LOG("emstorage_delete_dir failed : buf[%s]", buf);
1565                 memset(buf, 0x00, sizeof(buf));
1566                 SNPRINTF(buf, sizeof(buf), "%s%s%d", MAIL_HOME, DIR_SEPERATOR, input_mail_data->account_id);
1567
1568                 if (!emstorage_delete_dir(buf, &err)) {
1569                         EM_DEBUG_EXCEPTION("emstorage_delete_dir failed : buf[%s]", buf);
1570                         goto FINISH_OFF;
1571                 }
1572
1573                 err = EMAIL_ERROR_NONE;
1574         }
1575
1576         ret = true;
1577
1578 FINISH_OFF:
1579
1580         EM_SAFE_FREE(prefix_path);
1581
1582         if (err_code)
1583                 *err_code = err;
1584
1585         return ret;
1586 }
1587
1588 INTERNAL_FUNC int emcore_get_mime_entity(char *mime_path, char **output_path, int *err_code)
1589 {
1590         EM_DEBUG_FUNC_BEGIN("mime_path : [%s], output_path : [%p]", mime_path, output_path);
1591         int err = EMAIL_ERROR_NONE;
1592         int ret = false;
1593         int fd = 0;
1594         char *tmp_path = NULL;
1595
1596         if (!mime_path) {
1597                 EM_DEBUG_EXCEPTION("Invalid parameter");
1598                 err = EMAIL_ERROR_INVALID_PARAM;
1599                 goto FINISH_OFF;
1600         }
1601
1602         err = em_open(mime_path, O_RDONLY, 0, &fd);
1603         if (err != EMAIL_ERROR_NONE) {
1604                 EM_DEBUG_EXCEPTION("em_open failed");
1605                 goto FINISH_OFF;
1606         }
1607
1608         emcore_gmime_init();
1609         tmp_path = emcore_gmime_get_mime_entity(fd);
1610         emcore_gmime_shutdown();
1611
1612         if (tmp_path == NULL) {
1613                 EM_DEBUG_EXCEPTION("emcore_gmime_get_mime_entity failed");
1614                 err = EMAIL_ERROR_INVALID_DATA;
1615                 goto FINISH_OFF;
1616         }
1617
1618         ret = true;
1619
1620 FINISH_OFF:
1621
1622         EM_SAFE_CLOSE(fd);
1623
1624         if (err_code)
1625                 *err_code = err;
1626
1627         if (output_path)
1628                 *output_path = tmp_path;
1629
1630         EM_DEBUG_FUNC_END();
1631         return ret;
1632 }
1633
1634 INTERNAL_FUNC int emcore_get_digest_type(char *micalg_value)
1635 {
1636         EM_DEBUG_FUNC_BEGIN();
1637         int digest_type = DIGEST_TYPE_NONE;
1638
1639         if (micalg_value == NULL)
1640                 return DIGEST_TYPE_NONE;
1641
1642         if (strcasestr(micalg_value, "sha1"))
1643                 digest_type = DIGEST_TYPE_SHA1;
1644         else if (strcasestr(micalg_value, "md5"))
1645                 digest_type = DIGEST_TYPE_MD5;
1646         else if (strcasestr(micalg_value, "ripemd160"))
1647                 digest_type = DIGEST_TYPE_RIPEMD160;
1648         else if (strcasestr(micalg_value, "md2"))
1649                 digest_type = DIGEST_TYPE_MD2;
1650         else if (strcasestr(micalg_value, "tiger192"))
1651                 digest_type = DIGEST_TYPE_TIGER192;
1652         else if (strcasestr(micalg_value, "haval5160"))
1653                 digest_type = DIGEST_TYPE_HAVAL5160;
1654         else if (strcasestr(micalg_value, "sha256"))
1655                 digest_type = DIGEST_TYPE_SHA256;
1656         else if (strcasestr(micalg_value, "sha384"))
1657                 digest_type = DIGEST_TYPE_SHA384;
1658         else if (strcasestr(micalg_value, "sha512"))
1659                 digest_type = DIGEST_TYPE_SHA512;
1660         else if (strcasestr(micalg_value, "sha224"))
1661                 digest_type = DIGEST_TYPE_SHA224;
1662         else if (strcasestr(micalg_value, "md4"))
1663                 digest_type = DIGEST_TYPE_MD4;
1664         else
1665                 digest_type = DIGEST_TYPE_NONE;
1666
1667         EM_DEBUG_FUNC_END();
1668         return digest_type;
1669 }