Fixed the invalid casting and remove the flash noti
[platform/core/messaging/email-service.git] / email-common-use / email-convert.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 #include <stdlib.h>
23 #include <stdio.h>
24 #include "string.h"
25 #include "email-convert.h"
26 #include "email-core-mail.h"
27 #include "email-debug-log.h"
28 #include "email-core-utils.h"
29 #include "email-utilities.h"
30 #include "email-storage.h"
31 #include "tpl.h"
32
33 #define fSEEN 0x1
34 #define fDELETED 0x2
35 #define fFLAGGED 0x4
36 #define fANSWERED 0x8
37 #define fOLD 0x10
38 #define fDRAFT 0x20
39 #define fATTACHMENT 0x40
40 #define fFORWARD    0x80
41
42
43 INTERNAL_FUNC int em_convert_account_to_account_tbl(email_account_t *account, emstorage_account_tbl_t *account_tbl)
44 {
45         EM_DEBUG_FUNC_BEGIN("account[%p], account_tbl[%p]", account, account_tbl);
46         int ret = 1;
47
48         account_tbl->account_name                                  = EM_SAFE_STRDUP(account->account_name);
49         account_tbl->incoming_server_type                          = account->incoming_server_type;
50         account_tbl->incoming_server_address                       = EM_SAFE_STRDUP(account->incoming_server_address);
51         account_tbl->user_email_address                            = EM_SAFE_STRDUP(account->user_email_address);
52         account_tbl->incoming_server_user_name                     = EM_SAFE_STRDUP(account->incoming_server_user_name);
53         account_tbl->incoming_server_password                      = EM_SAFE_STRDUP(account->incoming_server_password);
54         account_tbl->retrieval_mode                                = account->retrieval_mode;
55         account_tbl->incoming_server_port_number                   = account->incoming_server_port_number;
56         account_tbl->incoming_server_secure_connection             = account->incoming_server_secure_connection;
57         account_tbl->outgoing_server_type                          = account->outgoing_server_type;
58         account_tbl->outgoing_server_address                       = EM_SAFE_STRDUP(account->outgoing_server_address);
59         account_tbl->outgoing_server_port_number                   = account->outgoing_server_port_number;
60         account_tbl->outgoing_server_need_authentication           = account->outgoing_server_need_authentication;
61         account_tbl->outgoing_server_secure_connection             = account->outgoing_server_secure_connection;
62         account_tbl->outgoing_server_user_name                     = EM_SAFE_STRDUP(account->outgoing_server_user_name);
63         account_tbl->outgoing_server_password                      = EM_SAFE_STRDUP(account->outgoing_server_password);
64         account_tbl->user_display_name                             = EM_SAFE_STRDUP(account->user_display_name);
65         account_tbl->reply_to_address                              = EM_SAFE_STRDUP(account->reply_to_address);
66         account_tbl->return_address                                = EM_SAFE_STRDUP(account->return_address);
67         account_tbl->account_id                                    = account->account_id;
68         account_tbl->keep_mails_on_pop_server_after_download       = account->keep_mails_on_pop_server_after_download;
69         account_tbl->auto_download_size                            = account->auto_download_size;
70         account_tbl->peak_interval                                 = account->peak_interval;
71         account_tbl->peak_days                                     = account->peak_days;
72         account_tbl->peak_start_time                               = account->peak_start_time;
73         account_tbl->peak_end_time                                 = account->peak_end_time;
74
75
76         account_tbl->outgoing_server_use_same_authenticator        = account->outgoing_server_use_same_authenticator;
77         account_tbl->auto_resend_times                             = account->auto_resend_times;
78         account_tbl->pop_before_smtp                               = account->pop_before_smtp;
79         account_tbl->incoming_server_requires_apop                 = account->incoming_server_requires_apop;
80         account_tbl->incoming_server_authentication_method         = account->incoming_server_authentication_method;
81         account_tbl->logo_icon_path                                = EM_SAFE_STRDUP(account->logo_icon_path);
82
83         account_tbl->roaming_option                                = account->roaming_option;
84         account_tbl->color_label                                   = account->color_label;
85         account_tbl->user_data                                     = em_memdup(account->user_data, account->user_data_length);
86         account_tbl->user_data_length                              = account->user_data_length;
87
88         account_tbl->options.priority                              = account->options.priority;
89         account_tbl->options.keep_local_copy                       = account->options.keep_local_copy;
90         account_tbl->options.req_delivery_receipt                  = account->options.req_delivery_receipt;
91         account_tbl->options.req_read_receipt                      = account->options.req_read_receipt;
92         account_tbl->options.download_limit                        = account->options.download_limit;
93         account_tbl->options.block_address                         = account->options.block_address;
94         account_tbl->options.block_subject                         = account->options.block_subject;
95         account_tbl->options.display_name_from                     = EM_SAFE_STRDUP(account->options.display_name_from);
96         account_tbl->options.reply_with_body                       = account->options.reply_with_body;
97         account_tbl->options.forward_with_files                    = account->options.forward_with_files;
98         account_tbl->options.add_myname_card                       = account->options.add_myname_card;
99         account_tbl->options.add_signature                         = account->options.add_signature;
100         account_tbl->options.signature                             = EM_SAFE_STRDUP(account->options.signature);
101         account_tbl->options.add_my_address_to_bcc                 = account->options.add_my_address_to_bcc;
102         account_tbl->options.notification_status                   = account->options.notification_status;
103         account_tbl->options.vibrate_status                        = account->options.vibrate_status;
104         account_tbl->options.display_content_status                = account->options.display_content_status;
105         account_tbl->options.default_ringtone_status               = account->options.default_ringtone_status;
106         account_tbl->options.alert_ringtone_path                   = EM_SAFE_STRDUP(account->options.alert_ringtone_path);
107
108         account_tbl->check_interval                                = account->check_interval;
109         account_tbl->account_svc_id                                = account->account_svc_id;
110         account_tbl->sync_status                                   = account->sync_status;
111         account_tbl->sync_disabled                                 = account->sync_disabled;
112         account_tbl->default_mail_slot_size                        = account->default_mail_slot_size;
113         account_tbl->smime_type                                    = account->smime_type;
114         account_tbl->certificate_path                              = EM_SAFE_STRDUP(account->certificate_path);
115         account_tbl->cipher_type                                   = account->cipher_type;
116         account_tbl->digest_type                                   = account->digest_type;
117         account_tbl->outgoing_server_size_limit                    = account->outgoing_server_size_limit;
118         account_tbl->wifi_auto_download                            = account->wifi_auto_download;
119
120     account_tbl->user_name                                     = EM_SAFE_STRDUP(account->user_name);
121         EM_DEBUG_FUNC_END();
122         return ret;
123 }
124
125 INTERNAL_FUNC int em_convert_account_tbl_to_account(emstorage_account_tbl_t *account_tbl, email_account_t *account)
126 {
127         EM_DEBUG_FUNC_BEGIN("account_tbl[%p], account[%p]", account_tbl, account);
128         int ret = 1;
129
130         account->account_name                                      = EM_SAFE_STRDUP(account_tbl->account_name);
131         account->incoming_server_type                              = account_tbl->incoming_server_type;
132         account->incoming_server_address                           = EM_SAFE_STRDUP(account_tbl->incoming_server_address);
133         account->user_email_address                                = EM_SAFE_STRDUP(account_tbl->user_email_address);
134         account->incoming_server_user_name                         = EM_SAFE_STRDUP(account_tbl->incoming_server_user_name);
135         account->incoming_server_password                          = EM_SAFE_STRDUP(account_tbl->incoming_server_password);
136         account->retrieval_mode                                    = account_tbl->retrieval_mode;
137         account->incoming_server_port_number                       = account_tbl->incoming_server_port_number;
138         account->incoming_server_secure_connection                 = account_tbl->incoming_server_secure_connection;
139         account->outgoing_server_type                              = account_tbl->outgoing_server_type;
140         account->outgoing_server_address                           = EM_SAFE_STRDUP(account_tbl->outgoing_server_address);
141         account->outgoing_server_port_number                       = account_tbl->outgoing_server_port_number;
142         account->outgoing_server_need_authentication               = account_tbl->outgoing_server_need_authentication;
143         account->outgoing_server_secure_connection                 = account_tbl->outgoing_server_secure_connection;
144         account->outgoing_server_user_name                         = EM_SAFE_STRDUP(account_tbl->outgoing_server_user_name);
145         account->outgoing_server_password                          = EM_SAFE_STRDUP(account_tbl->outgoing_server_password);
146         account->user_display_name                                 = EM_SAFE_STRDUP(account_tbl->user_display_name);
147         account->reply_to_address                                  = EM_SAFE_STRDUP(account_tbl->reply_to_address);
148         account->return_address                                    = EM_SAFE_STRDUP(account_tbl->return_address);
149         account->account_id                                        = account_tbl->account_id;
150         account->keep_mails_on_pop_server_after_download           = account_tbl->keep_mails_on_pop_server_after_download;
151         account->auto_download_size                                = account_tbl->auto_download_size;
152         account->peak_interval                                     = account_tbl->peak_interval;
153         account->peak_days                                         = account_tbl->peak_days;
154         account->peak_start_time                                   = account_tbl->peak_start_time;
155         account->peak_end_time                                     = account_tbl->peak_end_time;
156         account->outgoing_server_use_same_authenticator            = account_tbl->outgoing_server_use_same_authenticator;
157         account->pop_before_smtp                                   = account_tbl->pop_before_smtp;
158         account->auto_resend_times                                 = account_tbl->auto_resend_times;
159         account->incoming_server_requires_apop                     = account_tbl->incoming_server_requires_apop;
160         account->incoming_server_authentication_method             = account_tbl->incoming_server_authentication_method;
161         account->logo_icon_path                                    = EM_SAFE_STRDUP(account_tbl->logo_icon_path);
162         account->user_data                                         = em_memdup(account_tbl->user_data, account_tbl->user_data_length);
163         account->user_data_length                                  = account_tbl->user_data_length;
164         account->roaming_option                                    = account_tbl->roaming_option;
165         account->color_label                                       = account_tbl->color_label;
166         account->options.priority                                  = account_tbl->options.priority;
167         account->options.keep_local_copy                           = account_tbl->options.keep_local_copy;
168         account->options.req_delivery_receipt                      = account_tbl->options.req_delivery_receipt;
169         account->options.req_read_receipt                          = account_tbl->options.req_read_receipt;
170         account->options.download_limit                            = account_tbl->options.download_limit;
171         account->options.block_address                             = account_tbl->options.block_address;
172         account->options.block_subject                             = account_tbl->options.block_subject;
173         account->options.display_name_from                         = EM_SAFE_STRDUP(account_tbl->options.display_name_from);
174         account->options.reply_with_body                           = account_tbl->options.reply_with_body;
175         account->options.forward_with_files                        = account_tbl->options.forward_with_files;
176         account->options.add_myname_card                           = account_tbl->options.add_myname_card;
177         account->options.add_signature                             = account_tbl->options.add_signature;
178         account->options.signature                                 = EM_SAFE_STRDUP(account_tbl->options.signature);
179         account->options.add_my_address_to_bcc                     = account_tbl->options.add_my_address_to_bcc;
180         account->options.notification_status                       = account_tbl->options.notification_status;
181         account->options.vibrate_status                            = account_tbl->options.vibrate_status;
182         account->options.display_content_status                    = account_tbl->options.display_content_status;
183         account->options.default_ringtone_status                   = account_tbl->options.default_ringtone_status;
184         account->options.alert_ringtone_path                       = EM_SAFE_STRDUP(account_tbl->options.alert_ringtone_path);
185         account->check_interval                                    = account_tbl->check_interval;
186         account->account_svc_id                                    = account_tbl->account_svc_id;
187         account->sync_status                                       = account_tbl->sync_status;
188         account->sync_disabled                                     = account_tbl->sync_disabled;
189         account->default_mail_slot_size                            = account_tbl->default_mail_slot_size;
190         account->smime_type                                        = account_tbl->smime_type;
191         account->certificate_path                                  = EM_SAFE_STRDUP(account_tbl->certificate_path);
192         account->cipher_type                                       = account_tbl->cipher_type;
193         account->digest_type                                       = account_tbl->digest_type;
194         account->outgoing_server_size_limit                        = account_tbl->outgoing_server_size_limit;
195         account->wifi_auto_download                                = account_tbl->wifi_auto_download;
196         account->user_name                                         = EM_SAFE_STRDUP(account_tbl->user_name);
197
198         EM_DEBUG_FUNC_END();
199         return ret;
200 }
201
202 INTERNAL_FUNC int em_convert_mailbox_to_mailbox_tbl(email_mailbox_t *mailbox, emstorage_mailbox_tbl_t *mailbox_tbl)
203 {
204         EM_DEBUG_FUNC_BEGIN("mailbox[%p], mailbox_tbl[%p]", mailbox, mailbox_tbl);
205         int ret = 1;
206
207         mailbox_tbl->account_id                 = mailbox->account_id;
208         mailbox_tbl->mailbox_id                 = mailbox->mailbox_id;
209         mailbox_tbl->mailbox_name               = EM_SAFE_STRDUP(mailbox->mailbox_name);
210         mailbox_tbl->alias                      = EM_SAFE_STRDUP(mailbox->alias);
211         mailbox_tbl->local_yn                   = mailbox->local;
212         mailbox_tbl->mailbox_type               = mailbox->mailbox_type;
213         mailbox_tbl->unread_count               = mailbox->unread_count;
214         mailbox_tbl->total_mail_count_on_local  = mailbox->total_mail_count_on_local;
215         mailbox_tbl->total_mail_count_on_server = mailbox->total_mail_count_on_server;
216         mailbox_tbl->mail_slot_size             = mailbox->mail_slot_size;
217         mailbox_tbl->no_select                  = mailbox->no_select;
218         mailbox_tbl->last_sync_time             = mailbox->last_sync_time;
219         mailbox_tbl->deleted_flag               = mailbox->deleted_flag;
220         mailbox_tbl->eas_data_length            = mailbox->eas_data_length;
221
222         if ((mailbox->eas_data_length>0) && mailbox->eas_data) {
223                 if ((mailbox_tbl->eas_data = em_malloc(mailbox->eas_data_length)) == NULL) {
224                         EM_DEBUG_EXCEPTION("em_malloc failed");
225                         return 0; /*prevent 53448*/
226                 }
227                 memcpy(mailbox_tbl->eas_data, mailbox->eas_data, mailbox->eas_data_length);
228         }
229
230
231         EM_DEBUG_FUNC_END();
232         return ret;
233 }
234
235 INTERNAL_FUNC int em_convert_mailbox_tbl_to_mailbox(emstorage_mailbox_tbl_t *mailbox_tbl, email_mailbox_t *mailbox)
236 {
237         EM_DEBUG_FUNC_BEGIN("mailbox_tbl[%p], mailbox[%p]", mailbox_tbl, mailbox);
238         int ret = 1;
239
240         mailbox->account_id                 = mailbox_tbl->account_id;
241         mailbox->mailbox_id                 = mailbox_tbl->mailbox_id;
242         mailbox->mailbox_name               = EM_SAFE_STRDUP(mailbox_tbl->mailbox_name);
243         mailbox->alias                      = EM_SAFE_STRDUP(mailbox_tbl->alias);
244         mailbox->local                      = mailbox_tbl->local_yn;
245         mailbox->mailbox_type               = mailbox_tbl->mailbox_type;
246         mailbox->unread_count               = mailbox_tbl->unread_count;
247         mailbox->total_mail_count_on_local  = mailbox_tbl->total_mail_count_on_local;
248         mailbox->total_mail_count_on_server = mailbox_tbl->total_mail_count_on_server;
249         mailbox->mail_slot_size             = mailbox_tbl->mail_slot_size;
250         mailbox->no_select                  = mailbox_tbl->no_select;
251         mailbox->last_sync_time             = mailbox_tbl->last_sync_time;
252         mailbox->deleted_flag               = mailbox_tbl->deleted_flag;
253         mailbox->eas_data_length            = mailbox_tbl->eas_data_length;
254
255         if ((mailbox_tbl->eas_data_length >0) && mailbox_tbl->eas_data) {
256                 if ((mailbox->eas_data = em_malloc(mailbox_tbl->eas_data_length)) == NULL) {
257                         EM_DEBUG_EXCEPTION("em_malloc failed");
258                         return 0; /*prevent 53445*/
259                 }
260                 memcpy(mailbox->eas_data, mailbox_tbl->eas_data, mailbox_tbl->eas_data_length);
261         }
262
263         EM_DEBUG_FUNC_END();
264         return ret;
265 }
266
267
268 INTERNAL_FUNC int em_convert_mail_tbl_to_mail_data(emstorage_mail_tbl_t *mail_table_data, int item_count, email_mail_data_t **mail_data, int *error)
269 {
270         EM_DEBUG_FUNC_BEGIN("mail_table_data[%p], item_count [%d], mail_data[%p]", mail_table_data, item_count, mail_data);
271         int i, ret = false, err_code = EMAIL_ERROR_NONE;
272         email_mail_data_t *temp_mail_data = NULL;
273
274         if (!mail_table_data || !mail_data || !item_count) {
275                 EM_DEBUG_EXCEPTION("Invalid parameter");
276                 err_code = EMAIL_ERROR_INVALID_PARAM;
277                 goto FINISH_OFF;
278         }
279
280         temp_mail_data = em_malloc(sizeof(email_mail_data_t) * item_count);
281
282         if(!temp_mail_data) {
283                 EM_DEBUG_EXCEPTION("em_malloc failed");
284                 err_code = EMAIL_ERROR_OUT_OF_MEMORY;
285                 goto FINISH_OFF;
286         }
287
288         for(i = 0; i < item_count; i++) {
289                 temp_mail_data[i].mail_id                 = mail_table_data[i].mail_id;
290                 temp_mail_data[i].account_id              = mail_table_data[i].account_id;
291                 temp_mail_data[i].mailbox_id              = mail_table_data[i].mailbox_id;
292                 temp_mail_data[i].mailbox_type            = mail_table_data[i].mailbox_type;
293                 temp_mail_data[i].subject                 = EM_SAFE_STRDUP(mail_table_data[i].subject);
294                 temp_mail_data[i].date_time               = mail_table_data[i].date_time;
295                 temp_mail_data[i].server_mail_status      = mail_table_data[i].server_mail_status;
296                 temp_mail_data[i].server_mailbox_name     = EM_SAFE_STRDUP(mail_table_data[i].server_mailbox_name);
297                 temp_mail_data[i].server_mail_id          = EM_SAFE_STRDUP(mail_table_data[i].server_mail_id);
298                 temp_mail_data[i].message_id              = EM_SAFE_STRDUP(mail_table_data[i].message_id);
299                 temp_mail_data[i].reference_mail_id       = mail_table_data[i].reference_mail_id;
300                 temp_mail_data[i].full_address_from       = EM_SAFE_STRDUP(mail_table_data[i].full_address_from);
301                 temp_mail_data[i].full_address_reply      = EM_SAFE_STRDUP(mail_table_data[i].full_address_reply);
302                 temp_mail_data[i].full_address_to         = EM_SAFE_STRDUP(mail_table_data[i].full_address_to);
303                 temp_mail_data[i].full_address_cc         = EM_SAFE_STRDUP(mail_table_data[i].full_address_cc);
304                 temp_mail_data[i].full_address_bcc        = EM_SAFE_STRDUP(mail_table_data[i].full_address_bcc);
305                 temp_mail_data[i].full_address_return     = EM_SAFE_STRDUP(mail_table_data[i].full_address_return);
306                 temp_mail_data[i].email_address_sender    = EM_SAFE_STRDUP(mail_table_data[i].email_address_sender);
307                 temp_mail_data[i].email_address_recipient = EM_SAFE_STRDUP(mail_table_data[i].email_address_recipient);
308                 temp_mail_data[i].alias_sender            = EM_SAFE_STRDUP(mail_table_data[i].alias_sender);
309                 temp_mail_data[i].alias_recipient         = EM_SAFE_STRDUP(mail_table_data[i].alias_recipient);
310                 temp_mail_data[i].body_download_status    = mail_table_data[i].body_download_status;
311                 temp_mail_data[i].file_path_plain         = EM_SAFE_STRDUP(mail_table_data[i].file_path_plain);
312                 temp_mail_data[i].file_path_html          = EM_SAFE_STRDUP(mail_table_data[i].file_path_html);
313                 temp_mail_data[i].file_path_mime_entity   = EM_SAFE_STRDUP(mail_table_data[i].file_path_mime_entity);
314                 temp_mail_data[i].mail_size               = mail_table_data[i].mail_size;
315                 temp_mail_data[i].flags_seen_field        = mail_table_data[i].flags_seen_field;
316                 temp_mail_data[i].flags_deleted_field     = mail_table_data[i].flags_deleted_field;
317                 temp_mail_data[i].flags_flagged_field     = mail_table_data[i].flags_flagged_field;
318                 temp_mail_data[i].flags_answered_field    = mail_table_data[i].flags_answered_field;
319                 temp_mail_data[i].flags_recent_field      = mail_table_data[i].flags_recent_field;
320                 temp_mail_data[i].flags_draft_field       = mail_table_data[i].flags_draft_field;
321                 temp_mail_data[i].flags_forwarded_field   = mail_table_data[i].flags_forwarded_field;
322                 temp_mail_data[i].DRM_status              = mail_table_data[i].DRM_status;
323                 temp_mail_data[i].priority                = mail_table_data[i].priority;
324                 temp_mail_data[i].save_status             = mail_table_data[i].save_status;
325                 temp_mail_data[i].lock_status             = mail_table_data[i].lock_status;
326                 temp_mail_data[i].report_status           = mail_table_data[i].report_status;
327                 temp_mail_data[i].attachment_count        = mail_table_data[i].attachment_count;
328                 temp_mail_data[i].inline_content_count    = mail_table_data[i].inline_content_count;
329                 temp_mail_data[i].thread_id               = mail_table_data[i].thread_id;
330                 temp_mail_data[i].thread_item_count       = mail_table_data[i].thread_item_count;
331                 temp_mail_data[i].preview_text            = EM_SAFE_STRDUP(mail_table_data[i].preview_text);
332                 temp_mail_data[i].meeting_request_status  = mail_table_data[i].meeting_request_status;
333                 temp_mail_data[i].message_class           = mail_table_data[i].message_class;
334                 temp_mail_data[i].digest_type             = mail_table_data[i].digest_type;
335                 temp_mail_data[i].smime_type              = mail_table_data[i].smime_type;
336                 temp_mail_data[i].scheduled_sending_time  = mail_table_data[i].scheduled_sending_time;
337                 temp_mail_data[i].remaining_resend_times  = mail_table_data[i].remaining_resend_times;
338                 temp_mail_data[i].tag_id                  = mail_table_data[i].tag_id;
339                 temp_mail_data[i].replied_time            = mail_table_data[i].replied_time;
340                 temp_mail_data[i].forwarded_time          = mail_table_data[i].forwarded_time;
341                 temp_mail_data[i].eas_data_length         = mail_table_data[i].eas_data_length;
342                 if(mail_table_data[i].eas_data_length && mail_table_data[i].eas_data) {
343                         if((temp_mail_data[i].eas_data = em_malloc(mail_table_data[i].eas_data_length)) == NULL) {
344                                 EM_DEBUG_EXCEPTION("em_malloc failed");
345                                 err_code = EMAIL_ERROR_OUT_OF_MEMORY;
346                                 goto FINISH_OFF;
347                         }
348                 }
349                 memcpy(temp_mail_data[i].eas_data, mail_table_data[i].eas_data, mail_table_data[i].eas_data_length);
350                 temp_mail_data[i].pgp_password            = EM_SAFE_STRDUP(mail_table_data[i].pgp_password);
351                 temp_mail_data[i].user_name               = EM_SAFE_STRDUP(mail_table_data[i].user_name);
352         }
353
354         *mail_data = temp_mail_data;
355
356         ret = true;
357 FINISH_OFF:
358
359         if(ret == false && temp_mail_data) {
360                 emcore_free_mail_data_list(&temp_mail_data, item_count);
361         }
362
363
364         if(error)
365                 *error = err_code;
366
367         EM_DEBUG_FUNC_END();
368         return ret;
369 }
370
371 INTERNAL_FUNC int   em_convert_mail_data_to_mail_tbl(email_mail_data_t *mail_data, int item_count, emstorage_mail_tbl_t **mail_table_data, int *error)
372 {
373         EM_DEBUG_FUNC_BEGIN("mail_data[%p], item_count [%d], mail_table_data[%p]", mail_data, item_count, mail_table_data);
374         int i, ret = false, err_code = EMAIL_ERROR_NONE;
375         emstorage_mail_tbl_t *temp_mail_tbl = NULL;
376
377         if (!mail_data || !mail_table_data || !item_count) {
378                 EM_DEBUG_EXCEPTION("Invalid parameter");
379                 err_code = EMAIL_ERROR_INVALID_PARAM;
380                 goto FINISH_OFF;
381         }
382
383         temp_mail_tbl = em_malloc(sizeof(emstorage_mail_tbl_t) * item_count);
384
385         if(!temp_mail_tbl) {
386                 EM_DEBUG_EXCEPTION("em_malloc failed");
387                 err_code = EMAIL_ERROR_OUT_OF_MEMORY;
388                 goto FINISH_OFF;
389         }
390
391         for(i = 0; i < item_count; i++) {
392                 temp_mail_tbl[i].mail_id                 = mail_data[i].mail_id;
393                 temp_mail_tbl[i].account_id              = mail_data[i].account_id;
394                 temp_mail_tbl[i].mailbox_id              = mail_data[i].mailbox_id;
395                 temp_mail_tbl[i].mailbox_type            = mail_data[i].mailbox_type;
396                 temp_mail_tbl[i].date_time               = mail_data[i].date_time;
397                 temp_mail_tbl[i].subject                 = EM_SAFE_STRDUP(mail_data[i].subject);
398                 temp_mail_tbl[i].server_mail_status      = mail_data[i].server_mail_status;
399                 temp_mail_tbl[i].server_mailbox_name     = EM_SAFE_STRDUP(mail_data[i].server_mailbox_name);
400                 temp_mail_tbl[i].server_mail_id          = EM_SAFE_STRDUP(mail_data[i].server_mail_id);
401                 temp_mail_tbl[i].message_id              = EM_SAFE_STRDUP(mail_data[i].message_id);
402                 temp_mail_tbl[i].reference_mail_id       = mail_data[i].reference_mail_id;
403                 temp_mail_tbl[i].full_address_from       = EM_SAFE_STRDUP(mail_data[i].full_address_from);
404                 temp_mail_tbl[i].full_address_reply      = EM_SAFE_STRDUP(mail_data[i].full_address_reply);
405                 temp_mail_tbl[i].full_address_to         = EM_SAFE_STRDUP(mail_data[i].full_address_to);
406                 temp_mail_tbl[i].full_address_cc         = EM_SAFE_STRDUP(mail_data[i].full_address_cc);
407                 temp_mail_tbl[i].full_address_bcc        = EM_SAFE_STRDUP(mail_data[i].full_address_bcc);
408                 temp_mail_tbl[i].full_address_return     = EM_SAFE_STRDUP(mail_data[i].full_address_return);
409                 temp_mail_tbl[i].email_address_sender    = EM_SAFE_STRDUP(mail_data[i].email_address_sender);
410                 temp_mail_tbl[i].email_address_recipient = EM_SAFE_STRDUP(mail_data[i].email_address_recipient);
411                 temp_mail_tbl[i].alias_sender            = EM_SAFE_STRDUP(mail_data[i].alias_sender);
412                 temp_mail_tbl[i].alias_recipient         = EM_SAFE_STRDUP(mail_data[i].alias_recipient);
413                 temp_mail_tbl[i].body_download_status    = mail_data[i].body_download_status;
414                 temp_mail_tbl[i].file_path_plain         = EM_SAFE_STRDUP(mail_data[i].file_path_plain);
415                 temp_mail_tbl[i].file_path_html          = EM_SAFE_STRDUP(mail_data[i].file_path_html);
416                 temp_mail_tbl[i].file_path_mime_entity   = EM_SAFE_STRDUP(mail_data[i].file_path_mime_entity);
417                 temp_mail_tbl[i].mail_size               = mail_data[i].mail_size;
418                 temp_mail_tbl[i].flags_seen_field        = mail_data[i].flags_seen_field;
419                 temp_mail_tbl[i].flags_deleted_field     = mail_data[i].flags_deleted_field;
420                 temp_mail_tbl[i].flags_flagged_field     = mail_data[i].flags_flagged_field;
421                 temp_mail_tbl[i].flags_answered_field    = mail_data[i].flags_answered_field;
422                 temp_mail_tbl[i].flags_recent_field      = mail_data[i].flags_recent_field;
423                 temp_mail_tbl[i].flags_draft_field       = mail_data[i].flags_draft_field;
424                 temp_mail_tbl[i].flags_forwarded_field   = mail_data[i].flags_forwarded_field;
425                 temp_mail_tbl[i].DRM_status              = mail_data[i].DRM_status;
426                 temp_mail_tbl[i].priority                = mail_data[i].priority;
427                 temp_mail_tbl[i].save_status             = mail_data[i].save_status;
428                 temp_mail_tbl[i].lock_status             = mail_data[i].lock_status;
429                 temp_mail_tbl[i].report_status           = mail_data[i].report_status;
430                 temp_mail_tbl[i].attachment_count        = mail_data[i].attachment_count;
431                 temp_mail_tbl[i].inline_content_count    = mail_data[i].inline_content_count;
432                 temp_mail_tbl[i].thread_id               = mail_data[i].thread_id;
433                 temp_mail_tbl[i].thread_item_count       = mail_data[i].thread_item_count;
434                 temp_mail_tbl[i].preview_text            = EM_SAFE_STRDUP(mail_data[i].preview_text);
435                 temp_mail_tbl[i].meeting_request_status  = mail_data[i].meeting_request_status;
436                 temp_mail_tbl[i].message_class           = mail_data[i].message_class;
437                 temp_mail_tbl[i].digest_type             = mail_data[i].digest_type;
438                 temp_mail_tbl[i].smime_type              = mail_data[i].smime_type;
439                 temp_mail_tbl[i].scheduled_sending_time  = mail_data[i].scheduled_sending_time;
440                 temp_mail_tbl[i].remaining_resend_times  = mail_data[i].remaining_resend_times;
441                 temp_mail_tbl[i].tag_id                  = mail_data[i].tag_id;
442                 temp_mail_tbl[i].replied_time            = mail_data[i].replied_time;
443                 temp_mail_tbl[i].forwarded_time          = mail_data[i].forwarded_time;
444                 temp_mail_tbl[i].eas_data_length         = mail_data[i].eas_data_length;
445                 if(mail_data[i].eas_data_length && mail_data[i].eas_data) {
446                         if((temp_mail_tbl[i].eas_data = em_malloc(mail_data[i].eas_data_length)) == NULL) {
447                                 EM_DEBUG_EXCEPTION("em_malloc failed");
448                                 err_code = EMAIL_ERROR_OUT_OF_MEMORY;
449                                 /*prevent 44357*/
450                                 if (i > 0)
451                                         emstorage_free_mail(&temp_mail_tbl, i, NULL);
452                                 else 
453                                         EM_SAFE_FREE(temp_mail_tbl);
454                                 goto FINISH_OFF;
455                         }
456                 }
457                 memcpy(temp_mail_tbl[i].eas_data, mail_data[i].eas_data, mail_data[i].eas_data_length);
458                 temp_mail_tbl[i].pgp_password             = EM_SAFE_STRDUP(mail_data[i].pgp_password);
459                 temp_mail_tbl[i].user_name                = EM_SAFE_STRDUP(mail_data[i].user_name);
460         }
461
462         *mail_table_data = temp_mail_tbl;
463
464         ret = true;
465 FINISH_OFF:
466
467         if(error)
468                 *error = err_code;
469
470         EM_DEBUG_FUNC_END();
471         return ret;
472
473 }
474
475
476
477 INTERNAL_FUNC int em_convert_string_to_time_t(char *input_datetime_string, time_t *output_time)
478 {
479         EM_DEBUG_FUNC_BEGIN("input_datetime_string[%s], output_time[%p]", input_datetime_string, output_time);
480
481         char buf[16] = { 0, };
482         struct tm temp_time_info = { 0 };
483
484         if (!input_datetime_string || !output_time) {
485                 EM_DEBUG_EXCEPTION("input_datetime_string[%p], output_time[%p]", input_datetime_string, output_time);
486                 return EMAIL_ERROR_INVALID_PARAM;
487         }
488
489         memset(buf, 0x00, sizeof(buf));
490         SNPRINTF(buf, sizeof(buf), "%.4s", input_datetime_string);
491         temp_time_info.tm_year = atoi(buf) - 1970;
492
493         memset(buf, 0x00, sizeof(buf));
494         SNPRINTF(buf, sizeof(buf), "%.2s", input_datetime_string + 4);
495         temp_time_info.tm_mon = atoi(buf) - 1;
496
497         memset(buf, 0x00, sizeof(buf));
498         SNPRINTF(buf, sizeof(buf), "%.2s", input_datetime_string + 6);
499         temp_time_info.tm_mday = atoi(buf);
500
501         memset(buf, 0x00, sizeof(buf));
502         SNPRINTF(buf, sizeof(buf), "%.2s", input_datetime_string + 8);
503         temp_time_info.tm_hour = atoi(buf);
504
505         memset(buf, 0x00, sizeof(buf));
506         SNPRINTF(buf, sizeof(buf), "%.2s", input_datetime_string + 10);
507         temp_time_info.tm_min = atoi(buf);
508
509         memset(buf, 0x00, sizeof(buf));
510         SNPRINTF(buf, sizeof(buf), "%.2s", input_datetime_string + 12);
511         temp_time_info.tm_sec = atoi(buf);
512
513         *output_time = timegm(&temp_time_info);
514
515         EM_DEBUG_LOG("*output_time [%d", *output_time);
516
517         EM_DEBUG_FUNC_END("err %d", EMAIL_ERROR_NONE);
518         return EMAIL_ERROR_NONE;
519 }
520
521 INTERNAL_FUNC int em_convert_time_t_to_string(time_t *input_time, char **output_datetime_string)
522 {
523         EM_DEBUG_FUNC_BEGIN("input_time[%p], output_datetime_string[%p]", input_time, output_datetime_string);
524         char temp_buffer[20] = { 0, };
525         struct tm *temp_time_info;
526
527         if (!input_time || !output_datetime_string) {
528                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
529                 return EMAIL_ERROR_INVALID_PARAM;
530         }
531
532         temp_time_info = localtime(input_time);
533
534         if(!temp_time_info) {
535                 EM_DEBUG_EXCEPTION("localtime failed.");
536                 return EMAIL_ERROR_SYSTEM_FAILURE;
537         }
538         SNPRINTF(temp_buffer, sizeof(temp_buffer), "%04d%02d%02d%02d%02d%02d",
539                 temp_time_info->tm_year + 1970, temp_time_info->tm_mon, temp_time_info->tm_mday, temp_time_info->tm_hour, temp_time_info->tm_min, temp_time_info->tm_sec);
540
541         *output_datetime_string = EM_SAFE_STRDUP(temp_buffer);
542
543         EM_DEBUG_FUNC_END("err %d", EMAIL_ERROR_NONE);
544         return EMAIL_ERROR_NONE;
545 }
546
547 static char* append_sized_data_to_stream(char *stream, int *stream_len, char *src, int src_len)
548 {
549         /* EM_DEBUG_FUNC_BEGIN("input_stream [%p], input_output_stream_length [%p], input_sized_data [%p], input_data_size [%d]", input_stream, input_output_stream_length, input_sized_data, input_data_size); */
550         char *new_stream = NULL;
551
552         if( !stream_len || src_len == 0 || src == NULL || (stream != NULL && *stream_len == 0) ||
553                         (stream == NULL && *stream_len != 0) ) {
554                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
555                 return NULL;
556         }
557
558         int old_stream_len = *stream_len;
559
560         /*TODO: don't increase stream buffer incrementally when appending new data */
561         new_stream = (char*)em_malloc(old_stream_len + src_len);
562
563         if(!new_stream) {
564                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_OUT_OF_MEMORY");
565                 return NULL;
566         }
567
568         if(stream != NULL)
569                 memcpy(new_stream, stream, old_stream_len);
570
571         memcpy(new_stream + old_stream_len, src, src_len);
572
573         *stream_len = old_stream_len + src_len;
574
575         EM_SAFE_FREE(stream);
576         /* EM_DEBUG_FUNC_END("*input_output_stream_length [%d]", *input_output_stream_length); */
577         return new_stream;
578 }
579
580
581 static char* append_string_to_stream(char *input_stream, int *input_output_stream_length, char *input_source_string)
582 {
583         EM_DEBUG_FUNC_BEGIN("input_stream [%p], input_output_stream_length [%p] input_source_string[%p]", input_stream, input_output_stream_length, input_source_string);
584         char *new_stream = NULL;
585         int   data_length = 0;
586
587         if( !input_output_stream_length || (input_stream != NULL && *input_output_stream_length == 0) ||
588                 (input_stream == NULL && *input_output_stream_length != 0) ) {
589                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
590                 return NULL;
591         }
592
593         int source_stream_length = *input_output_stream_length;
594
595         data_length = EM_SAFE_STRLEN(input_source_string);
596
597         new_stream = (char*)em_malloc(source_stream_length + data_length + sizeof(int));
598
599         if(!new_stream) {
600                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_OUT_OF_MEMORY");
601                 return NULL;
602         }
603
604         if(input_stream != NULL)
605                 memcpy(new_stream, input_stream, source_stream_length);
606
607         /* write string length */
608         memcpy(new_stream + source_stream_length, (char*)&data_length, sizeof(int));
609
610         /* write string */
611         if(input_source_string)
612                 memcpy(new_stream + source_stream_length + sizeof(int), input_source_string, data_length);
613
614         /* for example, "abc" is written to stream buffer with "3abc" */
615         *input_output_stream_length = source_stream_length + sizeof(int) + data_length;
616
617         EM_SAFE_FREE(input_stream);
618         EM_DEBUG_FUNC_END("*input_output_stream_length [%d]", *input_output_stream_length);
619         return new_stream;
620 }
621
622 static int fetch_sized_data_from_stream(char *input_stream, int *input_output_stream_offset, int input_data_size, char *output_data)
623 {
624         /* EM_DEBUG_FUNC_BEGIN("input_stream [%p], input_output_stream_offset [%p] input_data_size [%d], output_data[%p]", input_stream, input_output_stream_offset, input_data_size, output_data); */
625         int stream_offset = 0;
626
627         if( !input_stream || !input_output_stream_offset || !input_data_size || !output_data) {
628                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
629                 return EMAIL_ERROR_INVALID_PARAM;
630         }
631
632         stream_offset = *input_output_stream_offset;
633
634         memcpy(output_data, input_stream + stream_offset, input_data_size);
635         stream_offset += input_data_size;
636
637         *input_output_stream_offset = stream_offset;
638
639         /* EM_DEBUG_FUNC_END("stream_offset [%d]", stream_offset); */
640         return EMAIL_ERROR_NONE;
641 }
642
643
644 static int fetch_string_from_stream(char *input_stream, int *input_output_stream_offset, char **output_string)
645 {
646         /* EM_DEBUG_FUNC_BEGIN("input_stream [%p], input_output_stream_offset [%p] output_string[%p]", input_stream, input_output_stream_offset, output_string); */
647         int string_length = 0;
648         int stream_offset = 0;
649         char *result_string = NULL;
650
651         if( !input_stream || !input_output_stream_offset || !output_string) {
652                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
653                 return EMAIL_ERROR_INVALID_PARAM;
654         }
655
656         stream_offset = *input_output_stream_offset;
657         /*      EM_DEBUG_LOG("stream_offset [%d]", stream_offset);      */
658
659         memcpy(&string_length, input_stream + stream_offset, sizeof(int));
660         stream_offset +=  sizeof(int);
661
662         /*      EM_DEBUG_LOG("string_length [%d]", string_length);      */
663
664         if(string_length != 0) {
665                 result_string = (char*)em_malloc(string_length + 1);
666                 if(result_string) {
667                         memcpy(result_string, input_stream + stream_offset, string_length);
668                         stream_offset += string_length;
669                 }
670         }
671         /*
672         if(result_string)
673                 EM_DEBUG_LOG("result_string [%s]", result_string);
674         */
675
676         *output_string              = result_string;
677         *input_output_stream_offset = stream_offset;
678
679         /* EM_DEBUG_FUNC_END("stream_offset [%d]", stream_offset); */
680         return EMAIL_ERROR_NONE;
681 }
682                                     /* divide struct at binary field (void* user_data)*/
683 #define EMAIL_ACCOUNT_FMT   "S(" "isiii" "isii" ")" "B" "S(" "issss"  "isiss" "iiiii" "iiiii" "isiss" "iii"\
684                                  "$(" "iiiii" "iisii" "iisi" "iiiis" ")" "iiiiiisiis" ")"
685
686 /* For converting fmt : to distinguish between 64bit or 32bit */
687 static char *convert_format(char *fmt)
688 {
689         EM_DEBUG_FUNC_BEGIN();
690
691         if (fmt == NULL) {
692                 EM_DEBUG_EXCEPTION("Invalid parameter");
693                 return NULL;
694         }
695
696         int size = 0;
697         int string_size = 0;
698         int ret = false;
699         char *c = NULL;
700         char *converted_fmt = NULL;
701
702         string_size = strlen(fmt);
703         converted_fmt = em_malloc(string_size);
704         if (converted_fmt == NULL) {
705                 EM_DEBUG_EXCEPTION("em_malloc failed");
706                 return NULL;
707         }
708
709         c = fmt;
710     while (*c != '\0') {
711         switch (*c) {
712             case 'c':
713             case 'i':
714             case 'u':
715             case 'j':
716             case 'v':
717             case 'I':
718             case 'U':
719             case 'f':
720             case 's':
721             case '#':
722             case 'B':
723             case 'A':
724             case 'S':
725             case '$': /* nested structure */
726             case ')':
727             case '(':
728                                 strncat(converted_fmt, c, 1);
729                                 break;
730                         case 't': /* special charater for time_t */
731                         case 'T':
732                                 size = sizeof(time_t);
733                                 if (size == 4) {
734                                         /* 32bit */
735                                         strncat(converted_fmt, "i", 1);
736                                 } else if (size == 8) {
737                                         /* 64bit */
738                                         strncat(converted_fmt, "I", 1);
739                                 } else {
740                                         EM_DEBUG_LOG("size : [%d]", size);
741                                 }
742                                 
743                                 break;
744             default:
745                 EM_DEBUG_EXCEPTION("unsupported option %c\n", *c);
746                                 goto FINISH_OFF;
747         }
748         c++;
749     }
750
751         EM_DEBUG_LOG("original fmt : [%s]", fmt);
752         EM_DEBUG_LOG("converted_fmt : [%s]", converted_fmt);
753
754         ret = true;
755
756 FINISH_OFF:
757
758         if (ret == false) {
759                 EM_SAFE_FREE(converted_fmt);
760         }
761
762         EM_DEBUG_FUNC_END();
763         return converted_fmt;
764 }
765
766 INTERNAL_FUNC char* em_convert_account_to_byte_stream(email_account_t* account, int *stream_len)
767 {
768         EM_DEBUG_FUNC_END();
769         EM_IF_NULL_RETURN_VALUE(account, NULL);
770
771         tpl_node *tn = NULL;
772         tpl_bin tb;
773         char *converted_fmt = NULL;
774
775         converted_fmt = convert_format(EMAIL_ACCOUNT_FMT);
776         if (converted_fmt == NULL) {
777                 EM_DEBUG_EXCEPTION("converting failed");
778                 return NULL;
779         }
780
781         tn = tpl_map(EMAIL_ACCOUNT_FMT, account, &tb, &(account->user_data_length));
782         if (!tn) {
783                 EM_SAFE_FREE(converted_fmt);
784                 EM_DEBUG_EXCEPTION("tpl_map failed");
785                 return NULL;
786         }
787         EM_SAFE_FREE(converted_fmt);
788
789         tb.sz = account->user_data_length;
790         tb.addr = account->user_data;
791         tpl_pack(tn, 0);
792
793         /* write account to buffer */
794         void *buf = NULL;
795         size_t len = 0;
796         tpl_dump(tn, TPL_MEM, &buf, &len);
797         tpl_free(tn);
798
799         *stream_len = len;
800
801         EM_DEBUG_FUNC_END();
802         return (char*) buf;
803 }
804
805
806 INTERNAL_FUNC void em_convert_byte_stream_to_account(char *stream, int stream_len, email_account_t *account)
807 {
808         EM_DEBUG_FUNC_END();
809         EM_NULL_CHECK_FOR_VOID(stream);
810         EM_NULL_CHECK_FOR_VOID(account);
811
812         tpl_node *tn = NULL;
813         tpl_bin tb;
814         char *converted_fmt = NULL;
815
816         converted_fmt = convert_format(EMAIL_ACCOUNT_FMT);
817         if (converted_fmt == NULL) {
818                 EM_DEBUG_EXCEPTION("converting failed");
819                 return;
820         }
821
822         tn = tpl_map(converted_fmt, account, &tb, &(account->user_data_length));
823         if (!tn) {
824                 EM_SAFE_FREE(converted_fmt);
825                 EM_DEBUG_EXCEPTION("tpl_map failed");
826                 return;
827         }
828         EM_SAFE_FREE(converted_fmt);
829
830         tpl_load(tn, TPL_MEM, stream, stream_len);
831         tpl_unpack(tn, 0);
832         tpl_free(tn);
833
834         /* tb will be destroyed at end of func, but tb.addr remains */
835         account->user_data = tb.addr;
836
837         EM_DEBUG_FUNC_END();
838 }
839
840 #define EMAIL_MAIL_DATA_FMT  "S(" "iiiis" "tisss" "issss" "sssss" "sisss"\
841                             "icccc" "cccii" "iiiii" "iisii" "iitii" "ttsss" "si" ")B"
842
843 INTERNAL_FUNC char* em_convert_mail_data_to_byte_stream(email_mail_data_t *mail_data, int *stream_len)
844 {
845         EM_DEBUG_FUNC_BEGIN("mail_data[%p] stream_len[%p]", mail_data, stream_len);
846         EM_IF_NULL_RETURN_VALUE(mail_data, NULL);
847         EM_IF_NULL_RETURN_VALUE(stream_len, NULL);
848
849         tpl_node *tn = NULL;
850         tpl_bin tb;
851         char *converted_fmt = NULL;
852
853         converted_fmt = convert_format(EMAIL_MAIL_DATA_FMT);
854         if (converted_fmt == NULL) {
855                 EM_DEBUG_EXCEPTION("converting failed");
856                 return NULL;
857         }
858
859         EM_DEBUG_LOG("eas_data_length[%d]", mail_data->eas_data_length); /*prevent 44369*/
860
861         tn = tpl_map(converted_fmt, mail_data, &tb);
862         if (!tn) {
863                 EM_SAFE_FREE(converted_fmt);
864                 EM_DEBUG_EXCEPTION("tpl_map failed");
865                 return NULL;
866         }
867         EM_SAFE_FREE(converted_fmt);
868         tb.sz   = mail_data->eas_data_length;
869         tb.addr = mail_data->eas_data;
870         tpl_pack(tn, 0);
871
872         /* write account to buffer */
873         void *buf = NULL;
874         size_t len = 0;
875         tpl_dump(tn, TPL_MEM, &buf, &len);
876         tpl_free(tn);
877
878         *stream_len = len;
879
880         EM_DEBUG_FUNC_END();
881         return (char*) buf;
882 }
883
884 INTERNAL_FUNC void em_convert_byte_stream_to_mail_data(char *stream, int stream_len, email_mail_data_t *mail_data)
885 {
886         EM_DEBUG_FUNC_BEGIN("stream[%p] stream_len[%d] mail_data[%p]", stream, stream_len, mail_data);
887         EM_NULL_CHECK_FOR_VOID(stream);
888         EM_NULL_CHECK_FOR_VOID(mail_data);
889
890         tpl_node *tn = NULL;
891         tpl_bin tb;
892         char *converted_fmt = NULL;
893
894         converted_fmt = convert_format(EMAIL_MAIL_DATA_FMT);
895         if (converted_fmt == NULL) {
896                 EM_DEBUG_EXCEPTION("converting failed");
897                 return;
898         }
899
900         tn = tpl_map(converted_fmt, mail_data, &tb);
901         if(!tn) {
902                 EM_SAFE_FREE(converted_fmt);
903                 EM_DEBUG_EXCEPTION("tpl_map failed");
904                 return;
905         }
906         EM_SAFE_FREE(converted_fmt);
907
908         tpl_load(tn, TPL_MEM, stream, stream_len);
909         tpl_unpack(tn, 0);
910
911         if(mail_data->eas_data_length <= 0 || tb.addr == NULL) {
912                 EM_DEBUG_LOG("No eas data. eas_data_length[%d] addr[%p]", mail_data->eas_data_length, tb.addr);
913         }
914         else {
915                 EM_DEBUG_LOG("eas_data_length[%d] addr[%p]", mail_data->eas_data_length, tb.addr);
916                 mail_data->eas_data = tb.addr;
917         }
918
919         if(tn)
920                 tpl_free(tn);
921
922         EM_DEBUG_FUNC_END();
923 }
924
925
926 #define EMAIL_ATTACHMENT_DATA_FMT "A(S(" "isssi" "iicii" "is" "))"
927
928 INTERNAL_FUNC char* em_convert_attachment_data_to_byte_stream(email_attachment_data_t *attachment, int attachment_count, int* stream_len)
929 {
930         EM_DEBUG_FUNC_BEGIN();
931         EM_IF_NULL_RETURN_VALUE(stream_len, NULL);
932
933         email_attachment_data_t cur = {0};
934         tpl_node *tn = NULL;
935         char *converted_fmt = NULL;
936
937         converted_fmt = convert_format(EMAIL_ATTACHMENT_DATA_FMT);
938         if (converted_fmt == NULL) {
939                 EM_DEBUG_EXCEPTION("convertinf failed");
940                 return NULL;
941         }
942
943         /* tpl_map adds value at 2nd param addr to packing buffer iterately */
944         /* 2nd param value (not addr via pointer) should be modified at each iteration */
945         tn = tpl_map(converted_fmt, &cur);
946         if (!tn) {
947                 EM_SAFE_FREE(converted_fmt);
948                 EM_DEBUG_EXCEPTION("tpl_map failed");
949                 return NULL;
950         }
951         EM_SAFE_FREE(converted_fmt);
952         
953         /* if attachment_count is zero, for loop is skipped */
954         int i=0;
955         for( ; (i < attachment_count) && (attachment+i) ; i++ ) {
956                 memcpy(&cur, attachment + i, sizeof(email_attachment_data_t));
957                 tpl_pack(tn, 1);                       
958         }
959
960         /* write data to buffer */
961         void *buf = NULL;
962         size_t len = 0;
963         tpl_dump(tn, TPL_MEM, &buf, &len);
964         tpl_free(tn);
965
966         *stream_len = len;
967
968         EM_DEBUG_LOG("stream_len: %d", len);
969
970         EM_DEBUG_FUNC_END();
971         return (char*) buf;
972 }
973
974 INTERNAL_FUNC void em_convert_byte_stream_to_attachment_data(char *stream, int stream_len, email_attachment_data_t **attachment_data, int *attachment_count)
975 {
976         EM_DEBUG_FUNC_BEGIN();
977         EM_NULL_CHECK_FOR_VOID(stream);
978         EM_NULL_CHECK_FOR_VOID(attachment_data);
979         EM_NULL_CHECK_FOR_VOID(attachment_count);
980
981
982         email_attachment_data_t cur = {0};
983         tpl_node *tn = NULL;
984         char *converted_fmt = NULL;
985         
986         converted_fmt = convert_format(EMAIL_ATTACHMENT_DATA_FMT);
987         if (converted_fmt == NULL) {
988                 EM_DEBUG_EXCEPTION("converting failed");
989                 return;
990         }
991
992         tn = tpl_map(converted_fmt, &cur);
993         if (!tn) {
994                 EM_SAFE_FREE(converted_fmt);
995                 EM_DEBUG_EXCEPTION("tpl_map failed");
996                 return;
997         }
998         EM_SAFE_FREE(converted_fmt);
999         tpl_load(tn, TPL_MEM, stream, stream_len);
1000
1001         /* tpl does not return the size of variable-length array, but we need variable-length array */
1002         /* so, make list and get list count in the first phase, */
1003         /* and then copy list to var array after allocating memory */
1004
1005         int num_element = tpl_Alen (tn, 1);
1006         email_attachment_data_t *attached = NULL;
1007         if (num_element > 0)
1008                 attached = (email_attachment_data_t*) em_malloc(sizeof(email_attachment_data_t)*num_element);
1009
1010         int i = 0;
1011         while( tpl_unpack(tn, 1) > 0) {
1012                 if (!(attached+i)) {
1013                         EM_DEBUG_EXCEPTION ("num element mismatched [%d] vs [%d]", num_element, i);
1014                         num_element = i;
1015                         break;
1016                 }
1017                 email_attachment_data_t* pdata = attached+i;
1018                 memcpy(pdata, &cur, sizeof(email_attachment_data_t)); /* copy unpacked data to list item */
1019                 pdata->attachment_name          = EM_SAFE_STRDUP(cur.attachment_name);
1020                 pdata->attachment_path          = EM_SAFE_STRDUP(cur.attachment_path);
1021                 pdata->content_id               = EM_SAFE_STRDUP(cur.content_id);
1022                 pdata->attachment_mime_type     = EM_SAFE_STRDUP(cur.attachment_mime_type);
1023                 memset(&cur, 0, sizeof(email_attachment_data_t));    /* initialize variable, used for unpacking */
1024                 i++;
1025         }
1026         tpl_free(tn);
1027
1028         *attachment_count = num_element;
1029         *attachment_data = attached;
1030
1031         EM_DEBUG_FUNC_END();
1032 }
1033
1034
1035 #define EMAIL_MAILBOX_FMT  "S(" "isisi" "iiiii" "itii" ")B"
1036
1037 INTERNAL_FUNC char* em_convert_mailbox_to_byte_stream(email_mailbox_t *mailbox_data, int *stream_len)
1038 {
1039         EM_DEBUG_FUNC_BEGIN();
1040         EM_IF_NULL_RETURN_VALUE(mailbox_data, NULL);
1041         EM_IF_NULL_RETURN_VALUE(stream_len, NULL);
1042
1043         tpl_node *tn = NULL;
1044         tpl_bin tb;
1045         char *converted_fmt = NULL;
1046
1047         converted_fmt = convert_format(EMAIL_MAILBOX_FMT);
1048         if (converted_fmt == NULL) {
1049                 EM_DEBUG_EXCEPTION("converting failed");
1050                 return NULL;
1051         }
1052
1053         EM_DEBUG_LOG("eas_data_length[%d]", mailbox_data->eas_data_length);
1054
1055         tn = tpl_map(converted_fmt, mailbox_data, &tb);
1056         if (!tn) {
1057                 EM_SAFE_FREE(converted_fmt);
1058                 EM_DEBUG_EXCEPTION("tpl_map failed");
1059                 return NULL;
1060         }
1061         EM_SAFE_FREE(converted_fmt);
1062         tb.sz   = mailbox_data->eas_data_length;
1063         tb.addr = mailbox_data->eas_data;
1064         tpl_pack(tn, 0);
1065
1066         /* write account to buffer */
1067         void *buf = NULL;
1068         size_t len = 0;
1069         tpl_dump(tn, TPL_MEM, &buf, &len);
1070
1071         if(tn)
1072                 tpl_free(tn);
1073
1074         *stream_len = len;
1075
1076         EM_DEBUG_FUNC_END("serialized len: %d", len);
1077         return (char*) buf;
1078 }
1079
1080
1081 INTERNAL_FUNC void em_convert_byte_stream_to_mailbox(char *stream, int stream_len, email_mailbox_t *mailbox_data)
1082 {
1083         EM_DEBUG_FUNC_BEGIN();
1084         EM_NULL_CHECK_FOR_VOID(stream);
1085         EM_NULL_CHECK_FOR_VOID(mailbox_data);
1086
1087         tpl_node *tn = NULL;
1088         tpl_bin tb;
1089         char *converted_fmt = NULL;
1090
1091         converted_fmt = convert_format(EMAIL_MAILBOX_FMT);
1092         if (converted_fmt == NULL) {
1093                 EM_DEBUG_EXCEPTION("converting failed");
1094                 return;
1095         }
1096
1097         tn = tpl_map(converted_fmt, mailbox_data, &tb);
1098         if (!tn) {
1099                 EM_SAFE_FREE(converted_fmt);
1100                 EM_DEBUG_EXCEPTION("tpl_map failed");
1101                 return;
1102         }
1103         EM_SAFE_FREE(converted_fmt);
1104         tpl_load(tn, TPL_MEM, stream, stream_len);
1105         tpl_unpack(tn, 0);
1106
1107         if(mailbox_data->eas_data_length <= 0 || tb.addr == NULL) {
1108                 EM_DEBUG_LOG("No eas data. eas_data_length[%d] addr[%p]", mailbox_data->eas_data_length, tb.addr);
1109         }
1110         else {
1111                 EM_DEBUG_LOG("eas_data_length[%d] addr[%p]", mailbox_data->eas_data_length, tb.addr);
1112                 mailbox_data->eas_data = tb.addr;
1113         }
1114
1115         if(tn)
1116                 tpl_free(tn);
1117
1118         EM_DEBUG_FUNC_END("deserialized len %d", stream_len);
1119 }
1120
1121 #define EMAIL_OPTION_FMT "S(" "iiiii" "iisii" "iisi" ")"
1122
1123 INTERNAL_FUNC char* em_convert_option_to_byte_stream(email_option_t* option, int* stream_len)
1124 {
1125         EM_DEBUG_FUNC_BEGIN();
1126         EM_IF_NULL_RETURN_VALUE(option, NULL);
1127         EM_IF_NULL_RETURN_VALUE(stream_len, NULL);
1128
1129         tpl_node *tn = NULL;
1130         char *converted_fmt = NULL;
1131
1132         converted_fmt = convert_format(EMAIL_OPTION_FMT);
1133         if (converted_fmt == NULL) {
1134                 EM_DEBUG_EXCEPTION("converting failed");
1135                 return NULL;
1136         }
1137
1138         tn = tpl_map(converted_fmt, option);
1139         if (!tn) {
1140                 EM_SAFE_FREE(converted_fmt);
1141                 EM_DEBUG_EXCEPTION("tpl_map failed");
1142                 return NULL;
1143         }
1144         EM_SAFE_FREE(converted_fmt);
1145         tpl_pack(tn, 0);
1146
1147         /* write account to buffer */
1148         void *buf = NULL;
1149         size_t len = 0;
1150         tpl_dump(tn, TPL_MEM, &buf, &len);
1151         tpl_free(tn);
1152
1153         *stream_len = len;
1154
1155         EM_DEBUG_FUNC_END("serialized len: %d", len);
1156         return (char*) buf;
1157 }
1158
1159 INTERNAL_FUNC void em_convert_byte_stream_to_option(char *stream, int stream_len, email_option_t *option)
1160 {
1161         EM_DEBUG_FUNC_BEGIN();
1162         EM_NULL_CHECK_FOR_VOID(stream);
1163         EM_NULL_CHECK_FOR_VOID(option);
1164
1165         tpl_node *tn = NULL;
1166         char *converted_fmt = NULL;
1167
1168         converted_fmt = convert_format(EMAIL_OPTION_FMT);
1169         if (converted_fmt == NULL) {
1170                 EM_DEBUG_EXCEPTION("converting failed");
1171                 return;
1172         }
1173
1174         tn = tpl_map(converted_fmt, option);
1175         if (!tn) {
1176                 EM_SAFE_FREE(converted_fmt);
1177                 EM_DEBUG_EXCEPTION("tpl_map failed");
1178                 return;
1179         }
1180         EM_SAFE_FREE(converted_fmt);
1181         tpl_load(tn, TPL_MEM, stream, stream_len);
1182         tpl_unpack(tn, 0);
1183         tpl_free(tn);
1184
1185         EM_DEBUG_FUNC_END("deserialized len %d", stream_len);
1186 }
1187
1188
1189 #define EMAIL_RULE_FMT "S(" "iisis" "siiii" ")"
1190
1191 INTERNAL_FUNC char* em_convert_rule_to_byte_stream(email_rule_t *rule, int *stream_len)
1192 {
1193         EM_DEBUG_FUNC_BEGIN();
1194         EM_IF_NULL_RETURN_VALUE(rule, NULL);
1195         EM_IF_NULL_RETURN_VALUE(stream_len, NULL);
1196
1197         tpl_node *tn = NULL;
1198         char *converted_fmt = NULL;
1199
1200         converted_fmt = convert_format(EMAIL_RULE_FMT);
1201         if (converted_fmt == NULL) {
1202                 EM_DEBUG_EXCEPTION("converting failed");
1203                 return NULL;
1204         }
1205
1206         tn = tpl_map(converted_fmt, rule);
1207         if (!tn) {
1208                 EM_SAFE_FREE(converted_fmt);
1209                 EM_DEBUG_EXCEPTION("tpl_map failed");
1210                 return NULL;
1211         }
1212         EM_SAFE_FREE(converted_fmt);
1213         tpl_pack(tn, 0);
1214
1215         /* write account to buffer */
1216         void *buf = NULL;
1217         size_t len = 0;
1218         tpl_dump(tn, TPL_MEM, &buf, &len);
1219         tpl_free(tn);
1220
1221         *stream_len = len;
1222
1223         EM_DEBUG_FUNC_END("serialized len: %d", len);
1224         return (char*) buf;
1225 }
1226
1227 INTERNAL_FUNC void em_convert_byte_stream_to_rule(char *stream, int stream_len, email_rule_t *rule)
1228 {
1229         EM_DEBUG_FUNC_BEGIN();
1230         EM_NULL_CHECK_FOR_VOID(stream);
1231         EM_NULL_CHECK_FOR_VOID(rule);
1232
1233         tpl_node *tn = NULL;
1234         char *converted_fmt = NULL;
1235
1236         converted_fmt = convert_format(EMAIL_RULE_FMT);
1237         if (converted_fmt == NULL) {
1238                 EM_DEBUG_EXCEPTION("converting failed");
1239                 return;
1240         }
1241
1242         tn = tpl_map(converted_fmt, rule);
1243         if (!tn) {
1244                 EM_SAFE_FREE(converted_fmt);
1245                 EM_DEBUG_EXCEPTION("tpl_map failed");
1246                 return;
1247         }
1248         EM_SAFE_FREE(converted_fmt);
1249         tpl_load(tn, TPL_MEM, stream, stream_len);
1250         tpl_unpack(tn, 0);
1251         tpl_free(tn);
1252
1253         EM_DEBUG_FUNC_END("deserialized len %d", stream_len);
1254 }
1255
1256 #define EMAIL_MEETING_REQUEST_FMT   "iiBBs" "sic#Bi" "c#Bi"
1257
1258 INTERNAL_FUNC char* em_convert_meeting_req_to_byte_stream(email_meeting_request_t *meeting_req, int *stream_len)
1259 {
1260
1261         EM_DEBUG_FUNC_END();
1262         EM_IF_NULL_RETURN_VALUE(meeting_req, NULL);
1263
1264         tpl_node *tn = NULL;
1265         tpl_bin tb[4];
1266         char *converted_fmt = NULL;
1267
1268         converted_fmt = convert_format(EMAIL_MEETING_REQUEST_FMT);
1269         if (converted_fmt == NULL) {
1270                 EM_DEBUG_EXCEPTION("converting failed");
1271                 return NULL;
1272         }
1273
1274         tn = tpl_map(converted_fmt,
1275                                                 &meeting_req->mail_id,
1276                                                 &meeting_req->meeting_response,
1277                                                 &tb[0],
1278                                                 &tb[1],
1279                                                 &meeting_req->location,
1280                                                 &meeting_req->global_object_id,
1281                                                 &meeting_req->time_zone.offset_from_GMT,
1282                                                 meeting_req->time_zone.standard_name, 32,
1283                                                 &tb[2],
1284                                                 &meeting_req->time_zone.standard_bias,
1285                                                 meeting_req->time_zone.daylight_name, 32,
1286                                                 &tb[3],
1287                                                 &meeting_req->time_zone.daylight_bias
1288                                 );
1289         if (!tn) {
1290                 EM_SAFE_FREE(converted_fmt);
1291                 EM_DEBUG_EXCEPTION("tpl_map failed");
1292                 return NULL;
1293         }
1294         EM_SAFE_FREE(converted_fmt);
1295         tb[0].sz = tb[1].sz = tb[2].sz = tb[3].sz = sizeof(struct tm);
1296         tb[0].addr = &meeting_req->start_time;
1297         tb[1].addr = &meeting_req->end_time;
1298         tb[2].addr = &meeting_req->time_zone.standard_time_start_date;
1299         tb[3].addr = &meeting_req->time_zone.daylight_time_start_date;
1300
1301         tpl_pack(tn, 0);
1302
1303         /* write account to buffer */
1304         void *buf = NULL;
1305         size_t len = 0;
1306         tpl_dump(tn, TPL_MEM, &buf, &len);
1307         tpl_free(tn);
1308
1309         *stream_len = len;
1310
1311         EM_DEBUG_FUNC_END();
1312         return (char*) buf;
1313 }
1314
1315
1316 INTERNAL_FUNC void em_convert_byte_stream_to_meeting_req(char *stream, int stream_len, email_meeting_request_t *meeting_req)
1317 {
1318         EM_DEBUG_FUNC_END();
1319         EM_NULL_CHECK_FOR_VOID(stream);
1320         EM_NULL_CHECK_FOR_VOID(meeting_req);
1321
1322         tpl_node *tn = NULL;
1323         tpl_bin tb[4];
1324         char *converted_fmt = NULL;
1325
1326         converted_fmt = convert_format(EMAIL_MEETING_REQUEST_FMT);
1327         if (converted_fmt == NULL) {
1328                 EM_DEBUG_EXCEPTION("converting failed");
1329                 return;
1330         }
1331
1332         tn = tpl_map(converted_fmt,
1333                                                 &meeting_req->mail_id,
1334                                                 &meeting_req->meeting_response,
1335                                                 &tb[0],
1336                                                 &tb[1],
1337                                                 &meeting_req->location,
1338                                                 &meeting_req->global_object_id,
1339                                                 &meeting_req->time_zone.offset_from_GMT,
1340                                                 meeting_req->time_zone.standard_name, 32,
1341                                                 &tb[2],
1342                                                 &meeting_req->time_zone.standard_bias,
1343                                                 meeting_req->time_zone.daylight_name, 32,
1344                                                 &tb[3],
1345                                                 &meeting_req->time_zone.daylight_bias
1346                                 );
1347         if (!tn) {
1348                 EM_SAFE_FREE(converted_fmt);
1349                 EM_DEBUG_EXCEPTION("tpl_map failed");
1350                 return;
1351         }
1352         tpl_load(tn, TPL_MEM, stream, stream_len);
1353         tpl_unpack(tn, 0);
1354         tpl_free(tn);
1355         EM_SAFE_FREE(converted_fmt);
1356
1357         /* tb will be destroyed at end of func, but tb.addr remains */
1358         memcpy(&meeting_req->start_time, tb[0].addr, sizeof(struct tm));
1359         memcpy(&meeting_req->end_time, tb[1].addr, sizeof(struct tm));
1360         memcpy(&meeting_req->time_zone.standard_time_start_date, tb[2].addr, sizeof(struct tm));
1361         memcpy(&meeting_req->time_zone.daylight_time_start_date, tb[3].addr, sizeof(struct tm));
1362
1363         int i=0;
1364         for(i=0; i< 4 ; i++)
1365                 EM_SAFE_FREE(tb[i].addr);
1366
1367         EM_DEBUG_FUNC_END();
1368 }
1369
1370 INTERNAL_FUNC char* em_convert_search_filter_to_byte_stream(email_search_filter_t *input_search_filter_list,
1371                                                                         int input_search_filter_count, int *output_stream_size)
1372 {
1373         EM_DEBUG_FUNC_BEGIN("input_search_filter_list [%p] input_search_filter_count [%d]", input_search_filter_list, input_search_filter_count);
1374
1375         char *result_stream = NULL;
1376         int   stream_size   = 0;
1377         int   i = 0;
1378
1379         EM_IF_NULL_RETURN_VALUE(input_search_filter_list, NULL);
1380
1381         result_stream = append_sized_data_to_stream(result_stream, &stream_size, (char*)&(input_search_filter_count), sizeof(int));
1382
1383         for( i = 0; i < input_search_filter_count; i++) {
1384                 result_stream = append_sized_data_to_stream(result_stream, &stream_size, (char*)&(input_search_filter_list[i].search_filter_type), sizeof(int));
1385                 switch(input_search_filter_list[i].search_filter_type) {
1386                         case EMAIL_SEARCH_FILTER_TYPE_MESSAGE_NO       :
1387                         case EMAIL_SEARCH_FILTER_TYPE_UID              :
1388                         case EMAIL_SEARCH_FILTER_TYPE_ALL              :
1389                         case EMAIL_SEARCH_FILTER_TYPE_SIZE_LARSER      :
1390                         case EMAIL_SEARCH_FILTER_TYPE_SIZE_SMALLER     :
1391                         case EMAIL_SEARCH_FILTER_TYPE_FLAGS_ANSWERED   :
1392                         case EMAIL_SEARCH_FILTER_TYPE_FLAGS_NEW        :
1393                         case EMAIL_SEARCH_FILTER_TYPE_FLAGS_DELETED    :
1394                         case EMAIL_SEARCH_FILTER_TYPE_FLAGS_OLD        :
1395                         case EMAIL_SEARCH_FILTER_TYPE_FLAGS_DRAFT      :
1396                         case EMAIL_SEARCH_FILTER_TYPE_FLAGS_FLAGED     :
1397                         case EMAIL_SEARCH_FILTER_TYPE_FLAGS_RECENT     :
1398                         case EMAIL_SEARCH_FILTER_TYPE_FLAGS_SEEN       :
1399                         case EMAIL_SEARCH_FILTER_TYPE_HEADER_PRIORITY  :
1400                                 result_stream = append_sized_data_to_stream(result_stream, &stream_size, (char*)&(input_search_filter_list[i].search_filter_key_value.integer_type_key_value), sizeof(int));
1401                                 break;
1402
1403                         case EMAIL_SEARCH_FILTER_TYPE_BCC              :
1404                         case EMAIL_SEARCH_FILTER_TYPE_BODY             :
1405                         case EMAIL_SEARCH_FILTER_TYPE_CC               :
1406                         case EMAIL_SEARCH_FILTER_TYPE_FROM             :
1407                         case EMAIL_SEARCH_FILTER_TYPE_KEYWORD          :
1408                         case EMAIL_SEARCH_FILTER_TYPE_TEXT             :
1409                         case EMAIL_SEARCH_FILTER_TYPE_SUBJECT          :
1410                         case EMAIL_SEARCH_FILTER_TYPE_TO               :
1411                         case EMAIL_SEARCH_FILTER_TYPE_MESSAGE_ID       :
1412                         case EMAIL_SEARCH_FILTER_TYPE_ATTACHMENT_NAME  :
1413                                 result_stream = append_string_to_stream(result_stream, &stream_size, input_search_filter_list[i].search_filter_key_value.string_type_key_value);
1414                                 break;
1415
1416                         case EMAIL_SEARCH_FILTER_TYPE_SENT_DATE_BEFORE :
1417                         case EMAIL_SEARCH_FILTER_TYPE_SENT_DATE_ON     :
1418                         case EMAIL_SEARCH_FILTER_TYPE_SENT_DATE_SINCE  :
1419                                 result_stream = append_sized_data_to_stream(result_stream, &stream_size, (char*)&(input_search_filter_list[i].search_filter_key_value.time_type_key_value), sizeof(time_t));
1420                                 break;
1421                         default :
1422                                 EM_DEBUG_EXCEPTION("Invalid filter type [%d]", input_search_filter_list[i].search_filter_type);
1423                                 break;
1424                 }
1425         }
1426
1427         *output_stream_size = stream_size;
1428
1429         EM_DEBUG_FUNC_END();
1430         return result_stream;
1431 }
1432
1433 INTERNAL_FUNC void em_convert_byte_stream_to_search_filter(char *input_stream,
1434                                 email_search_filter_t **output_search_filter_list, int *output_search_filter_count)
1435 {
1436         EM_DEBUG_FUNC_BEGIN("input_stream [%p] output_search_filter_list [%p] output_search_filter_count [%p]", input_stream, output_search_filter_list, output_search_filter_count);
1437
1438         int stream_offset = 0;
1439         int i = 0;
1440         int local_search_filter_count = 0;
1441         email_search_filter_t *local_search_filter = NULL;
1442
1443         EM_NULL_CHECK_FOR_VOID(input_stream);
1444         EM_NULL_CHECK_FOR_VOID(output_search_filter_list);
1445         EM_NULL_CHECK_FOR_VOID(output_search_filter_count);
1446
1447         fetch_sized_data_from_stream(input_stream, &stream_offset, sizeof(int), (char*)&(local_search_filter_count));
1448
1449         if(local_search_filter_count == 0) {
1450                 EM_DEBUG_EXCEPTION("local_search_filter_count is 0.");
1451                 goto FINISH_OFF;
1452         }
1453
1454         local_search_filter = em_malloc(sizeof(email_search_filter_t) * local_search_filter_count);
1455
1456         if(local_search_filter == NULL) {
1457                 EM_DEBUG_EXCEPTION("em_malloc for local_search_filter failed");
1458                 goto FINISH_OFF;
1459         }
1460
1461         *output_search_filter_count = local_search_filter_count;
1462
1463         for( i = 0; i < local_search_filter_count; i++) {
1464                 fetch_sized_data_from_stream(input_stream, &stream_offset, sizeof(int), (char*)&(local_search_filter[i].search_filter_type));
1465                 switch(local_search_filter[i].search_filter_type) {
1466                         case EMAIL_SEARCH_FILTER_TYPE_MESSAGE_NO       :
1467                         case EMAIL_SEARCH_FILTER_TYPE_UID              :
1468                         case EMAIL_SEARCH_FILTER_TYPE_ALL              :
1469                         case EMAIL_SEARCH_FILTER_TYPE_SIZE_LARSER      :
1470                         case EMAIL_SEARCH_FILTER_TYPE_SIZE_SMALLER     :
1471                         case EMAIL_SEARCH_FILTER_TYPE_FLAGS_ANSWERED   :
1472                         case EMAIL_SEARCH_FILTER_TYPE_FLAGS_NEW        :
1473                         case EMAIL_SEARCH_FILTER_TYPE_FLAGS_DELETED    :
1474                         case EMAIL_SEARCH_FILTER_TYPE_FLAGS_OLD        :
1475                         case EMAIL_SEARCH_FILTER_TYPE_FLAGS_DRAFT      :
1476                         case EMAIL_SEARCH_FILTER_TYPE_FLAGS_FLAGED     :
1477                         case EMAIL_SEARCH_FILTER_TYPE_FLAGS_RECENT     :
1478                         case EMAIL_SEARCH_FILTER_TYPE_FLAGS_SEEN       :
1479                         case EMAIL_SEARCH_FILTER_TYPE_HEADER_PRIORITY  :
1480                                 fetch_sized_data_from_stream(input_stream, &stream_offset, sizeof(int), (char*)&(local_search_filter[i].search_filter_key_value.integer_type_key_value));
1481                                 break;
1482
1483                         case EMAIL_SEARCH_FILTER_TYPE_BCC              :
1484                         case EMAIL_SEARCH_FILTER_TYPE_BODY             :
1485                         case EMAIL_SEARCH_FILTER_TYPE_CC               :
1486                         case EMAIL_SEARCH_FILTER_TYPE_FROM             :
1487                         case EMAIL_SEARCH_FILTER_TYPE_KEYWORD          :
1488                         case EMAIL_SEARCH_FILTER_TYPE_TEXT             :
1489                         case EMAIL_SEARCH_FILTER_TYPE_SUBJECT          :
1490                         case EMAIL_SEARCH_FILTER_TYPE_TO               :
1491                         case EMAIL_SEARCH_FILTER_TYPE_MESSAGE_ID       :
1492                         case EMAIL_SEARCH_FILTER_TYPE_ATTACHMENT_NAME  :
1493                                 fetch_string_from_stream(input_stream, &stream_offset, &(local_search_filter[i].search_filter_key_value.string_type_key_value));
1494                                 break;
1495
1496                         case EMAIL_SEARCH_FILTER_TYPE_SENT_DATE_BEFORE :
1497                         case EMAIL_SEARCH_FILTER_TYPE_SENT_DATE_ON     :
1498                         case EMAIL_SEARCH_FILTER_TYPE_SENT_DATE_SINCE  :
1499                                 fetch_sized_data_from_stream(input_stream, &stream_offset, sizeof(time_t), (char*)&(local_search_filter[i].search_filter_key_value.time_type_key_value));
1500                                 break;
1501                         default :
1502                                 EM_DEBUG_EXCEPTION("Invalid filter type [%d]", local_search_filter[i].search_filter_type);
1503                                 break;
1504                 }
1505         }
1506
1507         *output_search_filter_list = local_search_filter;
1508
1509 FINISH_OFF:
1510
1511         EM_DEBUG_FUNC_END();
1512 }
1513
1514
1515 #define EMAIL_JOB_INFORMATION_FMT   "A(S(iiii))"
1516
1517 INTERNAL_FUNC char* em_convert_task_information_to_byte_stream(email_task_information_t *input_task_information, int input_task_information_count, int *stream_len)
1518 {
1519         EM_DEBUG_FUNC_BEGIN("input_task_information[%p] input_task_information_count[%d] stream_len[%p]", input_task_information, input_task_information_count, stream_len);
1520         EM_IF_NULL_RETURN_VALUE(input_task_information, NULL);
1521         EM_IF_NULL_RETURN_VALUE(stream_len, NULL);
1522
1523         email_task_information_t cur = {0};
1524         tpl_node *tn = NULL;
1525         int i = 0;
1526
1527         tn = tpl_map(EMAIL_JOB_INFORMATION_FMT, &cur);
1528         if (!tn) {
1529                 EM_DEBUG_EXCEPTION("tpl_map failed");
1530                 return NULL;
1531         }
1532
1533         for( ; i < input_task_information_count ; i++ ) {
1534                 memcpy(&cur, input_task_information + i, sizeof(email_task_information_t));
1535                 tpl_pack(tn, 1);
1536         }
1537
1538         /* write data to buffer */
1539         void *buf = NULL;
1540         size_t len = 0;
1541         tpl_dump(tn, TPL_MEM, &buf, &len);
1542         tpl_free(tn);
1543
1544         *stream_len = len;
1545
1546         EM_DEBUG_FUNC_END("serialized len: %d", len);
1547         return (char*) buf;
1548 }
1549
1550 INTERNAL_FUNC void em_convert_byte_stream_to_task_information(char *input_stream, int input_stream_len, email_task_information_t **output_task_information, int *output_task_information_count)
1551 {
1552         EM_DEBUG_FUNC_BEGIN("input_stream[%p] input_stream_len[%d] output_task_information[%p] output_task_information_count[%p]", input_stream, input_stream_len, output_task_information, output_task_information_count);
1553         EM_NULL_CHECK_FOR_VOID(input_stream);
1554         EM_NULL_CHECK_FOR_VOID(output_task_information);
1555
1556         email_task_information_t cur = {0};
1557         tpl_node *tn = NULL;
1558         int i = 0;
1559
1560         tn = tpl_map(EMAIL_JOB_INFORMATION_FMT, &cur);
1561         if (!tn) {
1562                 EM_DEBUG_EXCEPTION("tpl_map failed");
1563                 return;
1564         }
1565         tpl_load(tn, TPL_MEM, input_stream, input_stream_len);
1566         int num_element = tpl_Alen (tn, 1);
1567         email_task_information_t* deserialized = NULL;
1568         if (num_element > 0)
1569                 deserialized = (email_task_information_t*) em_malloc(sizeof(email_task_information_t)*num_element);
1570
1571         while( tpl_unpack(tn, 1) > 0) {
1572                 if (!(deserialized+i)) {
1573                         EM_DEBUG_EXCEPTION ("num element mismatched [%d] vs [%d]", num_element, i);
1574                         num_element = i;
1575                         break;
1576                 }
1577                 email_task_information_t* pdata = deserialized+i;
1578                 memcpy(pdata, &cur, sizeof(email_task_information_t));
1579                 memset(&cur, 0, sizeof(email_task_information_t));
1580                 i++;
1581         }
1582         tpl_free(tn);
1583
1584
1585         *output_task_information_count = num_element;
1586         *output_task_information       = deserialized;
1587
1588         EM_DEBUG_FUNC_END();
1589 }
1590
1591 INTERNAL_FUNC int em_convert_certificate_tbl_to_certificate(emstorage_certificate_tbl_t *certificate_tbl, email_certificate_t **certificate, int *error)
1592 {
1593         EM_DEBUG_FUNC_BEGIN("certficate_tbl[%p], certificate[%p]", certificate_tbl, certificate);
1594
1595         int err_code = EMAIL_ERROR_NONE;
1596
1597         email_certificate_t *temp_certificate = NULL;
1598
1599         if (!certificate_tbl || !certificate)  {
1600                 EM_DEBUG_EXCEPTION("Invalid parameter");
1601                 err_code = EMAIL_ERROR_INVALID_PARAM;
1602                 goto FINISH_OFF;
1603         }
1604
1605         temp_certificate = em_malloc(sizeof(email_certificate_t)) ;
1606         if (!temp_certificate) {
1607                 EM_DEBUG_EXCEPTION("em_malloc failed");
1608                 err_code = EMAIL_ERROR_OUT_OF_MEMORY;
1609                 goto FINISH_OFF;
1610         }
1611
1612         temp_certificate->certificate_id = certificate_tbl->certificate_id;
1613         temp_certificate->issue_year = certificate_tbl->issue_year;
1614         temp_certificate->issue_month = certificate_tbl->issue_month;
1615         temp_certificate->issue_day = certificate_tbl->issue_day;
1616         temp_certificate->expiration_year = certificate_tbl->expiration_year;
1617         temp_certificate->expiration_month = certificate_tbl->expiration_month;
1618         temp_certificate->expiration_day = certificate_tbl->expiration_day;
1619         temp_certificate->issue_organization_name = EM_SAFE_STRDUP(certificate_tbl->issue_organization_name);
1620         temp_certificate->email_address = EM_SAFE_STRDUP(certificate_tbl->email_address);
1621         temp_certificate->subject_str = EM_SAFE_STRDUP(certificate_tbl->subject_str);
1622         temp_certificate->filepath = EM_SAFE_STRDUP(certificate_tbl->filepath);
1623
1624         *certificate = temp_certificate;
1625
1626 FINISH_OFF:
1627         if (error)
1628                 *error = err_code;
1629
1630         EM_DEBUG_FUNC_END();
1631         return true;
1632 }
1633
1634 INTERNAL_FUNC int em_convert_certificate_to_certificate_tbl(email_certificate_t *certificate, emstorage_certificate_tbl_t *certificate_tbl)
1635 {
1636         EM_DEBUG_FUNC_BEGIN("certficate[%p], certificate_tbl[%p]", certificate, certificate_tbl);
1637
1638         certificate_tbl->certificate_id = certificate->certificate_id;
1639         certificate_tbl->issue_year = certificate->issue_year;
1640         certificate_tbl->issue_month = certificate->issue_month;
1641         certificate_tbl->issue_day = certificate->issue_day;
1642         certificate_tbl->expiration_year = certificate->expiration_year;
1643         certificate_tbl->expiration_month = certificate->expiration_month;
1644         certificate_tbl->expiration_day = certificate->expiration_day;
1645         certificate_tbl->issue_organization_name = EM_SAFE_STRDUP(certificate->issue_organization_name);
1646         certificate_tbl->email_address = EM_SAFE_STRDUP(certificate->email_address);
1647         certificate_tbl->subject_str = EM_SAFE_STRDUP(certificate->subject_str);
1648         certificate_tbl->filepath = EM_SAFE_STRDUP(certificate->filepath);
1649
1650         EM_DEBUG_FUNC_END();
1651         return true;
1652 }
1653