Changed the deprecated thumbnail_util APIs in msg-service
[platform/core/messaging/msg-service.git] / utils / MsgSerialize.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15 */
16
17 #include "MsgJsonParser.h"
18 #include "MsgSerialize.h"
19 #include "MsgMmsTypes.h"
20 #include "MsgDebug.h"
21 #include "MsgMmsMessage.h"
22
23
24 #ifdef MSG_MMS_USE_JSON_DATA
25 #include <glib.h>
26 #include <glib-object.h>
27 #include <json-glib/json-glib.h>
28 #include <json-glib/json-gobject.h>
29
30 typedef enum {
31         TYPE_STR,
32         TYPE_INT,
33         TYPE_ARRAY,
34         TYPE_OBJECT,
35 } mms_json_data_type_e;
36
37 typedef struct _mms_json_item {
38         const char *key;
39         int e_val;
40         int type;
41 } mms_json_item_s;
42
43 typedef enum {
44         MMS_UNKNOWN = -1,
45         MMS_HEADER = 0,
46         MMS_MULTIPART_LIST,
47         MMS_SMIL_MULTIPART,
48
49         MULTIPART_TYPE ,
50         MULTIPART_CONTENT_TYPE,
51         MULTIPART_NAME,
52         MULTIPART_FILEPATH,
53         MULTIPART_FILEDATA,
54         MULTIPART_CONTENT_ID,
55         MULTIPART_CONTENT_LOCATION,
56
57         HEADER_CONTENT_LOCATION,
58         HEADER_CONTENT_TYPE,
59         HEADER_CONTENT_TYPE_INT,
60         HEADER_DATE,
61         HEADER_DELIVERY_REPORT,
62         HEADER_DELIVERY_TIME,
63         HEADER_EXPIRY_TIME,
64         HEADER_MESSAGE_CLASS,
65         HEADER_MID,
66         HEADER_MESSAGE_TYPE,
67         HEADER_VERSION,
68         HEADER_PRIORITY,
69         HEADER_READ_REPORT,
70         HEADER_HIDE_ADDRESS,
71         HEADER_TRID,
72         HEADER_CONTENT_CLASS,
73
74         MMS_BACKUP_TYPE,
75 } mms_json_enum_e;
76
77 static mms_json_item_s mms_json_table[] = {
78         {"header",              MMS_HEADER,     TYPE_INT},
79         {"multipart_list",      MMS_MULTIPART_LIST, TYPE_ARRAY},
80         {"smil",                MMS_SMIL_MULTIPART,     TYPE_OBJECT},
81 /* multipart */
82         {"mp_type",      MULTIPART_TYPE,        TYPE_INT},
83         {"mp_ct",        MULTIPART_CONTENT_TYPE,        TYPE_STR},
84         {"mp_name",      MULTIPART_NAME,        TYPE_STR},
85         {"mp_path",      MULTIPART_FILEPATH,    TYPE_STR},
86         {"mp_data",      MULTIPART_FILEDATA,    TYPE_STR},
87
88         {"mp_cid",       MULTIPART_CONTENT_ID,  TYPE_STR},
89         {"mp_cl",        MULTIPART_CONTENT_LOCATION,    TYPE_STR},
90 /* header */
91         {"h_cl",        HEADER_CONTENT_LOCATION,        TYPE_STR},
92         {"h_ct",        HEADER_CONTENT_TYPE,    TYPE_STR},
93         {"h_ct_int",    HEADER_CONTENT_TYPE_INT,        TYPE_INT},
94         {"h_date",      HEADER_DATE, TYPE_INT},
95         {"h_d_rpt",     HEADER_DELIVERY_REPORT, TYPE_INT},
96         {"h_d_time",    HEADER_DELIVERY_TIME, TYPE_INT},
97         {"h_exp",       HEADER_EXPIRY_TIME, TYPE_INT},
98         {"h_mclass",    HEADER_MESSAGE_CLASS,   TYPE_INT},
99         {"h_mid",       HEADER_MID, TYPE_STR},
100         {"h_mtype",     HEADER_MESSAGE_TYPE,    TYPE_INT},
101         {"h_v",         HEADER_VERSION, TYPE_INT},
102         {"h_prioriy",   HEADER_PRIORITY,        TYPE_INT},
103         {"h_r_rpt",     HEADER_READ_REPORT, TYPE_INT},
104         {"h_hide_addr", HEADER_HIDE_ADDRESS, TYPE_INT},
105         {"h_tid",       HEADER_TRID,    TYPE_STR},
106         {"h_cclass",    HEADER_CONTENT_CLASS, TYPE_INT},
107         {"backup_type", MMS_BACKUP_TYPE, TYPE_INT},
108 };
109
110 mms_json_enum_e get_mms_key_type(const char * key)
111 {
112         int i;
113
114         int table_count = sizeof(mms_json_table)/sizeof(mms_json_item_s);
115         if (key) {
116                 for (i = 0; i < table_count; i++) {
117                         if (g_strcmp0(mms_json_table[i].key, key) == 0)
118                                 return (mms_json_enum_e)mms_json_table[i].e_val;
119                 }
120         }
121
122         return MMS_UNKNOWN;
123 }
124
125 const char *get_mms_key(mms_json_enum_e e)
126 {
127         int i;
128
129         int table_count = sizeof(mms_json_table)/sizeof(mms_json_item_s);
130
131         for (i = 0; i < table_count; i++) {
132                 if (mms_json_table[i].e_val == e)
133                         return mms_json_table[i].key;
134         }
135
136         return NULL;
137 }
138
139 int MsgSerializeHeader(const MMS_HEADER_DATA_S *pheader, JsonObject **headerObject)
140 {
141         JsonObject *header_object = json_object_new();
142
143         MSG_JSON_OBJ_SET_STR(header_object, get_mms_key(HEADER_CONTENT_LOCATION), pheader->contentLocation);
144
145         MSG_JSON_OBJ_SET_STR(header_object, get_mms_key(HEADER_CONTENT_TYPE), pheader->szContentType);
146
147         MSG_JSON_OBJ_SET_INT(header_object, get_mms_key(HEADER_CONTENT_TYPE_INT), pheader->contentType);
148
149         MSG_JSON_OBJ_SET_INT(header_object, get_mms_key(HEADER_DATE), pheader->date);
150
151         MSG_JSON_OBJ_SET_INT(header_object, get_mms_key(HEADER_DELIVERY_REPORT), pheader->bDeliveryReport);
152
153         MSG_JSON_OBJ_SET_INT(header_object, get_mms_key(HEADER_DELIVERY_TIME), pheader->delivery.time);
154
155         MSG_JSON_OBJ_SET_INT(header_object, get_mms_key(HEADER_EXPIRY_TIME), pheader->expiry.time);
156
157         MSG_JSON_OBJ_SET_INT(header_object, get_mms_key(HEADER_MESSAGE_CLASS), pheader->messageClass);
158
159         MSG_JSON_OBJ_SET_STR(header_object, get_mms_key(HEADER_MID), pheader->messageID);
160
161         MSG_JSON_OBJ_SET_INT(header_object, get_mms_key(HEADER_MESSAGE_TYPE), pheader->messageType);
162
163         MSG_JSON_OBJ_SET_INT(header_object, get_mms_key(HEADER_VERSION), pheader->mmsVersion);
164
165         MSG_JSON_OBJ_SET_INT(header_object, get_mms_key(HEADER_PRIORITY), pheader->mmsPriority);
166
167         MSG_JSON_OBJ_SET_INT(header_object, get_mms_key(HEADER_READ_REPORT), pheader->bReadReport);
168
169         MSG_JSON_OBJ_SET_INT(header_object, get_mms_key(HEADER_HIDE_ADDRESS), pheader->bHideAddress);
170
171         MSG_JSON_OBJ_SET_STR(header_object, get_mms_key(HEADER_TRID), pheader->trID);
172
173         MSG_JSON_OBJ_SET_INT(header_object, get_mms_key(HEADER_CONTENT_CLASS), pheader->contentClass);
174
175         *headerObject = header_object;
176
177         return 0;
178 }
179
180 int MsgParseHeader(msg_json_parser_object *parse_obj, MMS_HEADER_DATA_S *pheader)
181 {
182         MSG_BEGIN();
183
184         int index_child = 0;
185         mms_json_enum_e type;
186         msg_json_parser_object child = {};
187
188         if (parse_obj == NULL)
189                 return -1;
190
191         while (msg_json_parser_get_next_child(parse_obj, &child, index_child)) {
192                 MSG_PRINT_PARSER_OBJECT(index_child, child);
193                 type = get_mms_key_type(child.key);
194
195                 switch (type) {
196                 case HEADER_CONTENT_LOCATION:
197                         snprintf(pheader->contentLocation, sizeof(pheader->contentLocation), "%s", (char *)child.value);
198                         break;
199                 case HEADER_CONTENT_TYPE:
200                         snprintf(pheader->szContentType, sizeof(pheader->szContentType), "%s", (char *)child.value);
201                         break;
202                 case HEADER_CONTENT_TYPE_INT:
203                         pheader->contentType = (int)child.number_value;
204                         break;
205                 case HEADER_DATE:
206                         pheader->date = (unsigned long int)child.number_value;
207                         break;
208                 case HEADER_DELIVERY_REPORT:
209                         pheader->bDeliveryReport = (bool)child.number_value;
210                         break;
211                 case HEADER_DELIVERY_TIME:
212                         if ((unsigned int)child.number_value > 0) {
213                                 pheader->delivery.type = MMS_TIMETYPE_RELATIVE;
214                                 pheader->delivery.time = (unsigned int)child.number_value;
215                         }
216                         break;
217                 case HEADER_EXPIRY_TIME:
218                         if ((unsigned int)child.number_value > 0) {
219                         pheader->expiry.type = MMS_TIMETYPE_RELATIVE;
220                         pheader->expiry.time = (unsigned int)child.number_value;
221                         }
222                         break;
223                 case HEADER_MESSAGE_CLASS:
224                         pheader->messageClass = (int)child.number_value;
225                         break;
226                 case HEADER_MID:
227                         snprintf(pheader->messageID, sizeof(pheader->messageID), "%s", (char *)child.value);
228                         break;
229                 case HEADER_MESSAGE_TYPE:
230                         pheader->messageType = (int)child.number_value;
231                         break;
232                 case HEADER_VERSION:
233                         pheader->mmsVersion = (int)child.number_value;
234                         break;
235                 case HEADER_PRIORITY:
236                         pheader->mmsPriority = (int)child.number_value;
237                         break;
238                 case HEADER_READ_REPORT:
239                         pheader->bReadReport = (bool)child.number_value;
240                         break;
241                 case HEADER_HIDE_ADDRESS:
242                         pheader->bHideAddress = (bool)child.number_value;
243                         break;
244                 case HEADER_TRID:
245                         snprintf(pheader->trID, sizeof(pheader->trID), "%s", (char *)child.value);
246                         break;
247                 case HEADER_CONTENT_CLASS:
248                         pheader->contentClass = (int)child.number_value;
249                         break;
250                 default:
251                         MSG_DEBUG("Not Support key = [%s], type = [%d]", child.key, type);
252                         break;
253                 }
254
255                 index_child++;
256         }
257
258         MSG_END();
259         return 0;
260 }
261
262 int MsgSerializeMultipart(const MMS_MULTIPART_DATA_S *pMultipart, JsonObject **multipartObject)
263 {
264         JsonObject *object = json_object_new();
265
266         MSG_JSON_OBJ_SET_INT(object, get_mms_key(MULTIPART_TYPE), pMultipart->type);
267
268         MSG_JSON_OBJ_SET_STR(object, get_mms_key(MULTIPART_CONTENT_TYPE), pMultipart->szContentType);
269         MSG_JSON_OBJ_SET_STR(object, get_mms_key(MULTIPART_NAME), pMultipart->szFileName);
270         MSG_JSON_OBJ_SET_STR(object, get_mms_key(MULTIPART_FILEPATH), pMultipart->szFilePath);
271
272         if (pMultipart->pMultipartData) {
273                 char* base64data = g_base64_encode((const guchar*)pMultipart->pMultipartData, pMultipart->nMultipartDataLen);
274                 MSG_JSON_OBJ_SET_STR(object, get_mms_key(MULTIPART_FILEDATA), base64data);
275                 g_free(base64data);
276         }
277
278         MSG_JSON_OBJ_SET_STR(object, get_mms_key(MULTIPART_CONTENT_ID), pMultipart->szContentID);
279         MSG_JSON_OBJ_SET_STR(object, get_mms_key(MULTIPART_CONTENT_LOCATION), pMultipart->szContentLocation);
280
281         *multipartObject = object;
282
283         return 0;
284 }
285
286 int MsgParseMultipartData(msg_json_parser_object *parse_obj, MMS_MULTIPART_DATA_S *pMultipart)
287 {
288         MSG_BEGIN();
289
290         int index_child = 0;
291         mms_json_enum_e type;
292         msg_json_parser_object child = {};
293
294         if (parse_obj == NULL)
295                 return -1;
296
297         while (msg_json_parser_get_next_child(parse_obj, &child, index_child)) {
298                 MSG_PRINT_PARSER_OBJECT(index_child, child);
299
300                 type = get_mms_key_type(child.key);
301
302                 switch (type) {
303                 case MULTIPART_TYPE:
304                         pMultipart->type = (MimeType)child.number_value;
305                         break;
306                 case MULTIPART_CONTENT_TYPE:
307                         snprintf(pMultipart->szContentType, sizeof(pMultipart->szContentType), "%s", (char *)child.value);
308                         break;
309                 case MULTIPART_NAME:
310                         snprintf(pMultipart->szFileName, sizeof(pMultipart->szFileName), "%s", (char *)child.value);
311                         break;
312                 case MULTIPART_FILEPATH:
313                         snprintf(pMultipart->szFilePath, sizeof(pMultipart->szFilePath), "%s", (char *)child.value);
314                         break;
315                 case MULTIPART_FILEDATA:
316                         pMultipart->pMultipartData = (char*)g_base64_decode((char *)child.value, &pMultipart->nMultipartDataLen);
317                         break;
318                 case MULTIPART_CONTENT_ID:
319                         snprintf(pMultipart->szContentID, sizeof(pMultipart->szContentID), "%s", (char *)child.value);
320                         break;
321                 case MULTIPART_CONTENT_LOCATION:
322                         snprintf(pMultipart->szContentLocation, sizeof(pMultipart->szContentLocation), "%s", (char *)child.value);
323                         break;
324                 default:
325                         MSG_DEBUG("Not Support key = [%s], type = [%d]", child.key, type);
326                         break;
327                 }
328
329                 index_child++;
330         }
331
332         MSG_END();
333         return 0;
334 }
335
336 int MsgParseMultipartListData(msg_json_parser_object *parse_obj, MMS_DATA_S *pMsgData)
337 {
338         MSG_BEGIN();
339
340         int index_child = 0;
341
342         msg_json_parser_object child = {};
343
344         if (parse_obj == NULL)
345                 return -1;
346
347         while (msg_json_parser_get_next_child(parse_obj, &child, index_child)) {
348                 if (child.value != NULL && child.type != MSG_JSON_PARSER_NULL) {
349                         MSG_PRINT_PARSER_OBJECT(index_child, child);
350
351                         MMS_MULTIPART_DATA_S *pMultipart = MsgMmsCreateMultipart();
352
353                         if (pMultipart) {
354                                 if (MsgParseMultipartData(&child, pMultipart) == 0) {
355                                         pMsgData->multipartlist = g_list_append(pMsgData->multipartlist, pMultipart);
356                                 } else {
357                                         free(pMultipart);
358                                         pMultipart = NULL;
359                                 }
360                         }
361                 } else {
362                         MSG_DEBUG("Get child : idx  = %d, key = %s, type = %d, value = %p", index_child, child.key, child.type, child.value);
363                 }
364
365                 index_child++;
366         }
367
368         MSG_END();
369         return 0;
370 }
371
372 int MsgParseMmsData(msg_json_parser_object *parse_obj, MMS_DATA_S *pMsgData)
373 {
374         MSG_BEGIN();
375
376         int index_child = 0;
377         mms_json_enum_e type;
378         msg_json_parser_object child = {};
379
380         if (parse_obj == NULL)
381                 return -1;
382
383         while (msg_json_parser_get_next_child(parse_obj, &child, index_child)) {
384                 if (child.key != NULL) {
385                         MSG_PRINT_PARSER_OBJECT(index_child, child);
386
387                         type = get_mms_key_type(child.key);
388
389                         switch (type) {
390                         case MMS_MULTIPART_LIST:
391                                 MsgParseMultipartListData(&child, pMsgData);
392                                 break;
393                         case MMS_HEADER:
394                                 if (pMsgData->header == NULL) {
395                                         pMsgData->header = MsgMmsCreateHeader();
396                                 }
397                                 if (pMsgData->header)
398                                         MsgParseHeader(&child, pMsgData->header);
399                                 break;
400                         case MMS_SMIL_MULTIPART: {
401                                 MMS_MULTIPART_DATA_S *pMultipart = MsgMmsCreateMultipart();
402                                 if (pMultipart) {
403                                         if (MsgParseMultipartData(&child, pMultipart) == 0) {
404                                                 pMsgData->smil = pMultipart;
405                                         } else {
406                                                 free(pMultipart);
407                                                 pMultipart = NULL;
408                                         }
409                                 }
410                                 break;
411                         }
412                         case MMS_BACKUP_TYPE:
413                                 pMsgData->backup_type = (int)child.number_value;
414                                 break;
415                         default:
416                                 MSG_DEBUG("Not Support key = [%s], type = [%d]", child.key, type);
417                                 break;
418                         }
419
420                 } else {
421                         MSG_PRINT_PARSER_OBJECT(index_child, child);
422                 }
423
424                 index_child++;
425         }
426
427         MSG_END();
428         return 0;
429 }
430
431
432 int MsgSerializeMmsJsonData(const MMS_DATA_S *pMsgData, char **pValue)
433 {
434         MSG_BEGIN();
435
436         if (pMsgData == NULL)
437                 return MSG_ERR_NULL_POINTER;
438
439         JsonGenerator *generator;
440         JsonNode *root;
441
442         JsonObject *object_main, *object_header;
443         JsonArray *array_multipart;
444
445         gsize len = 0;
446
447         generator = json_generator_new();
448
449         root = json_node_new(JSON_NODE_OBJECT);
450
451         object_main = json_object_new();
452
453         MSG_JSON_OBJ_SET_INT(object_main, get_mms_key(MMS_BACKUP_TYPE), pMsgData->backup_type);
454
455         /* smil multipart */
456         if (pMsgData->smil) {
457                 JsonObject *smil_object = NULL;
458
459                 MsgSerializeMultipart(pMsgData->smil, &smil_object);
460                 MSG_JSON_OBJ_SET_OBJ(object_main, get_mms_key(MMS_SMIL_MULTIPART), smil_object);
461         }
462
463         if (pMsgData->multipartlist) {
464                 /* multipart */
465                 array_multipart = json_array_new();
466
467                 int list_count = g_list_length(pMsgData->multipartlist);
468
469                 MSG_DEBUG("Page Count is [%d]", list_count);
470
471                 for (int i = 0; i < list_count; i++) {
472                         MMS_MULTIPART_DATA_S *multipart = (MMS_MULTIPART_DATA_S *)g_list_nth_data(pMsgData->multipartlist, i);
473
474                         if (multipart) {
475                                 JsonObject *multipart_object = NULL;
476
477                                 MsgSerializeMultipart(multipart, &multipart_object);
478
479                                 MSG_JSON_ARRAY_ADD_OBJECT(array_multipart, multipart_object);
480
481                         } else {
482                                 MSG_DEBUG("Not Exist Multipart Data in [%d]th", i);
483                         }
484                 }
485
486                 MSG_JSON_OBJ_SET_ARRAY(object_main, get_mms_key(MMS_MULTIPART_LIST), array_multipart);
487         }
488
489         /* header */
490         if (pMsgData->header) {
491                 MsgSerializeHeader(pMsgData->header, &object_header);
492
493                 MSG_JSON_OBJ_SET_OBJ(object_main, get_mms_key(MMS_HEADER), object_header);
494         }
495
496         json_node_take_object(root, object_main);
497
498         json_generator_set_root(generator, root);
499
500         *pValue = json_generator_to_data(generator, &len);
501         MSG_BEGIN();
502         MSG_DEBUG("Serialized Data : %s", *pValue);
503
504         json_node_free(root);
505
506         g_object_unref(generator);
507
508         MSG_END();
509
510         return 0;
511 }
512
513
514 int MsgDeserializeMmsJsonData(char* value, int value_len, MMS_DATA_S **ppMmsData)
515 {
516         MSG_BEGIN();
517
518         int ret = 0;
519
520         msg_json_parser_handle parser_handle = NULL;
521         msg_json_parser_object root = {};
522         MMS_DATA_S *pMmsData = NULL;
523
524         pMmsData = MsgMmsCreate();
525
526         parser_handle = msg_json_parser_handle_create();
527
528         msg_json_parser_parse_buffer(parser_handle, value, value_len, &root);
529
530         MSG_DEBUG("Deserialized : %s", value);
531
532         MSG_DEBUG("root : key = %s, type = %d, value = %p", root.key, root.type, root.value);
533
534         ret = MsgParseMmsData(&root, pMmsData);
535         if (ret != 0) {
536                 MSG_DEBUG("Fail to MsgParseMessageData, ret = %d", ret);
537                 MsgMmsRelease(&pMmsData);
538         } else {
539                 *ppMmsData = pMmsData;
540         }
541
542         msg_json_parser_handle_destory(&parser_handle);
543
544         MSG_END();
545
546         return ret;
547 }
548 #endif
549
550
551 int MsgSerializeMms(const MMS_DATA_S *pMsgData, char **pValue)
552 {
553         MSG_BEGIN();
554
555         if (pMsgData == NULL)
556                 return MSG_ERR_NULL_POINTER;
557
558         int bufsize = 0;
559
560         int isExistHeader = 0, isExistSmil = 0, isExistMultipart = 0;
561         int multipart_cnt = 0;
562         char *buf = NULL;
563         int i;
564
565         bufsize += sizeof(int); /* back-up type */
566
567         int to_cnt = 0;
568         int cc_cnt = 0;
569         int bcc_cnt = 0;
570
571         bufsize += sizeof(int); /* check header data */
572
573         if (pMsgData->header) {
574                 isExistHeader = 1;
575
576                 bufsize += sizeof(MMS_HEADER_DATA_S); /* header */
577
578                 to_cnt = g_list_length(pMsgData->header->to);
579                 cc_cnt = g_list_length(pMsgData->header->cc);
580                 bcc_cnt = g_list_length(pMsgData->header->bcc);
581
582                 bufsize += (3 * sizeof(int));
583
584                 MSG_DEBUG("Address \"to\" count = [%d]", to_cnt);
585
586                 for (i = 0; i < to_cnt; i++) {
587                         MMS_ADDRESS_DATA_S *addr_data = (MMS_ADDRESS_DATA_S *)g_list_nth_data(pMsgData->header->to, i);
588                         if (addr_data && addr_data->address_val) {
589                                 bufsize += (sizeof(int) + sizeof(int) + strlen(addr_data->address_val)); /* type, length, address */
590                         }
591                 }
592                 for (i = 0; i < cc_cnt; i++) {
593                         MMS_ADDRESS_DATA_S *addr_data = (MMS_ADDRESS_DATA_S *)g_list_nth_data(pMsgData->header->cc, i);
594                         if (addr_data && addr_data->address_val) {
595                                 bufsize += (sizeof(int) + sizeof(int) + strlen(addr_data->address_val)); /* type, length, address */
596                         }
597                 }
598                 for (i = 0; i < bcc_cnt; i++) {
599                         MMS_ADDRESS_DATA_S *addr_data = (MMS_ADDRESS_DATA_S *)g_list_nth_data(pMsgData->header->bcc, i);
600                         if (addr_data && addr_data->address_val) {
601                                 bufsize += (sizeof(int) + sizeof(int) + strlen(addr_data->address_val)); /* type, length, address */
602                         }
603                 }
604         }
605
606         bufsize += sizeof(int); /* check smil data */
607
608         if (pMsgData->smil) {
609                 isExistSmil = 1;
610                 bufsize += (sizeof(MMS_MULTIPART_DATA_S) + (sizeof(char)*pMsgData->smil->nMultipartDataLen)); /* smil data */
611         }
612
613         bufsize += sizeof(int); /* check multipart list data */
614
615         if (pMsgData->multipartlist) {
616                 isExistMultipart = 1;
617                 multipart_cnt = g_list_length(pMsgData->multipartlist);
618
619                 bufsize += sizeof(int); /* multipart count */
620
621                 for (i = 0; i < multipart_cnt; i++) {
622                         MMS_MULTIPART_DATA_S *multipart_data = (MMS_MULTIPART_DATA_S *)g_list_nth_data(pMsgData->multipartlist, i);
623                         bufsize += sizeof(MMS_MULTIPART_DATA_S) + (sizeof(char)*multipart_data->nMultipartDataLen);
624                 }
625         }
626
627         MSG_DEBUG("Serialize bufsize = %d", bufsize);
628
629         buf = new char[bufsize];
630         if (buf == NULL)
631                 return -1;
632
633         memset(buf, 0x00, bufsize);
634
635         int serial_index = 0;
636         int offset = 0;
637
638         /* 1. Backup type */
639         memcpy(buf, &pMsgData->backup_type, sizeof(int));
640         MSG_DEBUG("[#%2d][%5d] backup type = %d", serial_index++, offset, pMsgData->backup_type);
641         offset += sizeof(int);
642
643         /* 2. Header Data */
644         memcpy(buf + offset, &isExistHeader, sizeof(int));
645         offset += sizeof(int);
646
647         if (pMsgData->header) {
648                 memcpy(buf + offset, pMsgData->header, sizeof(MMS_HEADER_DATA_S));
649                 offset += sizeof(MMS_HEADER_DATA_S);
650
651                 /* address */
652                 memcpy(buf + offset, &to_cnt, sizeof(int));
653                 MSG_DEBUG("[#%2d][%5d] TO Count = %d", serial_index++, offset, to_cnt);
654                 offset += sizeof(int);
655
656                 for (i = 0; i < to_cnt; i++) {
657                         MMS_ADDRESS_DATA_S *to_addr = (MMS_ADDRESS_DATA_S *)g_list_nth_data(pMsgData->header->to, i);
658                         if (to_addr && to_addr->address_val) {
659                                 memcpy(buf + offset, &to_addr->address_type, sizeof(int));
660                                 MSG_DEBUG("[#%2d][%5d] address type = %d", serial_index++, offset, to_addr->address_type);
661                                 offset += sizeof(int);
662                                 memcpy(buf + offset, to_addr->address_val, sizeof(char)*MAX_ADDRESS_VAL_LEN);
663                                 MSG_SEC_DEBUG("[#%2d][%5d] address val = %s", serial_index++, offset, to_addr->address_val);
664                                 offset += sizeof(char)*MAX_ADDRESS_VAL_LEN;
665                         }
666                 }
667
668                 /* address */
669                 memcpy(buf + offset, &cc_cnt, sizeof(int));
670                 MSG_DEBUG("[#%2d][%5d] CC Count = %d", serial_index++, offset, cc_cnt);
671                 offset += sizeof(int);
672
673                 for (i = 0; i < cc_cnt; i++) {
674                         MMS_ADDRESS_DATA_S *cc_addr = (MMS_ADDRESS_DATA_S *)g_list_nth_data(pMsgData->header->cc, i);
675                         if (cc_addr && cc_addr->address_val) {
676                                 memcpy(buf + offset, &cc_addr->address_type, sizeof(int));
677                                 MSG_DEBUG("[#%2d][%5d] address type = %d", serial_index++, offset, cc_addr->address_type);
678                                 offset += sizeof(int);
679                                 memcpy(buf + offset, cc_addr->address_val, sizeof(char)*MAX_ADDRESS_VAL_LEN);
680                                 MSG_SEC_DEBUG("[#%2d][%5d] address val = %s", serial_index++, offset, cc_addr->address_val);
681                                 offset += sizeof(char)*MAX_ADDRESS_VAL_LEN;
682                         }
683                 }
684
685                 /* address */
686                 memcpy(buf + offset, &bcc_cnt, sizeof(int));
687                 MSG_DEBUG("[#%2d][%5d] BCC Count = %d", serial_index++, offset, bcc_cnt);
688                 offset += sizeof(int);
689
690                 for (i = 0; i < bcc_cnt; i++) {
691                         MMS_ADDRESS_DATA_S *bcc_addr = (MMS_ADDRESS_DATA_S *)g_list_nth_data(pMsgData->header->bcc, i);
692                         if (bcc_addr && bcc_addr->address_val) {
693                                 memcpy(buf + offset, &bcc_addr->address_type, sizeof(int));
694                                 MSG_DEBUG("[#%2d][%5d] address type = %d", serial_index++, offset, bcc_addr->address_type);
695                                 offset += sizeof(int);
696                                 memcpy(buf + offset, bcc_addr->address_val, sizeof(char)*MAX_ADDRESS_VAL_LEN);
697                                 MSG_SEC_DEBUG("[#%2d][%5d] address val = %s", serial_index++, offset, bcc_addr->address_val);
698                                 offset += sizeof(char)*MAX_ADDRESS_VAL_LEN;
699                         }
700                 }
701         }
702
703         /* 3. Smil Data */
704         memcpy(buf + offset, &isExistSmil, sizeof(int));
705         offset += sizeof(int);
706
707         if (pMsgData->smil) {
708                 memcpy(buf + offset, pMsgData->smil, sizeof(MMS_MULTIPART_DATA_S));
709                 offset += sizeof(MMS_MULTIPART_DATA_S);
710
711                 MSG_DEBUG("SMIL file path = [%s]", pMsgData->smil->szFilePath);
712                 MSG_DEBUG("SMIL nMultipartDataLen = [%zu]", pMsgData->smil->nMultipartDataLen);
713
714                 if (pMsgData->smil->pMultipartData) {
715                         memcpy(buf + offset, pMsgData->smil->pMultipartData, sizeof(char)*pMsgData->smil->nMultipartDataLen);
716                         MSG_SEC_DEBUG("[#%2d][%5d] smil data = %s", serial_index++, offset, pMsgData->smil->pMultipartData);
717                         offset += sizeof(char)*pMsgData->smil->nMultipartDataLen;
718                 }
719         }
720
721         /* 4. Multipart list data */
722         memcpy(buf + offset, &isExistMultipart, sizeof(int));
723         offset += sizeof(int);
724
725         if (pMsgData->multipartlist) {
726                 MSG_DEBUG("Multipart list count = [ %d]", multipart_cnt);
727                 memcpy(buf + offset, &multipart_cnt, sizeof(int));
728                 offset += sizeof(int);
729
730                 for (i = 0; i < multipart_cnt; i++) {
731                         MMS_MULTIPART_DATA_S *multipart_data = (MMS_MULTIPART_DATA_S *)g_list_nth_data(pMsgData->multipartlist, i);
732                         MSG_DEBUG("multipart_data = [%p]", multipart_data);
733                         if (multipart_data) {
734                                 memcpy(buf + offset, multipart_data, sizeof(MMS_MULTIPART_DATA_S));
735                                 offset += sizeof(MMS_MULTIPART_DATA_S);
736
737                                 MSG_SEC_DEBUG("Multipart file path = [%s]", multipart_data->szFilePath);
738
739                                 if (multipart_data->pMultipartData) {
740                                         memcpy(buf + offset, multipart_data->pMultipartData, sizeof(char)*multipart_data->nMultipartDataLen);
741                                         MSG_DEBUG("[#%2d][%5d] multipart data ptr = %p", serial_index++, offset, multipart_data->pMultipartData);
742                                         offset += sizeof(char)*multipart_data->nMultipartDataLen;
743                                 }
744                         }
745                 }
746         }
747
748         *pValue = buf;
749
750         MSG_DEBUG("Expect Buffer Size: %d, Final offset : %d", bufsize, offset);
751
752         MSG_END();
753
754         return bufsize;
755 }
756
757
758 int MsgDeserializeMmsData(char* value, int value_len, MMS_DATA_S **ppMmsData)
759 {
760         MSG_BEGIN();
761
762         if (value == NULL) {
763                 MSG_DEBUG("Serialized data is NULL");
764                 return -1;
765         }
766
767         MMS_DATA_S *pMmsData = NULL;
768
769         int isExistHeader = 0, isExistSmil = 0, isExistMultipart = 0;
770         int addr_cnt = 0;
771         int multipart_cnt = 0;
772
773         int deserial_index = 0;
774         int offset = 0;
775         int i = 0;
776
777         pMmsData = MsgMmsCreate();
778         *ppMmsData = pMmsData;
779         if (pMmsData == NULL)
780                 return -1;
781
782
783         /* 1. Backup type */
784         memcpy(&(pMmsData->backup_type), value, sizeof(int));
785         MSG_DEBUG("[#%2d][%5d] backup type = %d", deserial_index++, offset, pMmsData->backup_type);
786         offset += sizeof(int);
787
788         /* 2. Header Data */
789         memcpy(&isExistHeader, value + offset, sizeof(int));
790         offset += sizeof(int);
791
792         if (isExistHeader) {
793                 pMmsData->header = (MMS_HEADER_DATA_S *)calloc(1, sizeof(MMS_HEADER_DATA_S));
794                 if (pMmsData->header == NULL)
795                         return -1;
796
797                 memcpy(pMmsData->header, value + offset, sizeof(MMS_HEADER_DATA_S));
798                 offset += sizeof(MMS_HEADER_DATA_S);
799
800                 memcpy(&addr_cnt, value + offset, sizeof(int));
801                 offset += sizeof(int);
802
803                 pMmsData->header->to = NULL;
804
805                 for (i = 0; i < addr_cnt; i++) {
806                         MMS_ADDRESS_DATA_S* to_addr = (MMS_ADDRESS_DATA_S*)calloc(1, sizeof(MMS_ADDRESS_DATA_S));
807                         if (to_addr == NULL)
808                                 return -1;
809
810                         memcpy(&(to_addr->address_type), value + offset, sizeof(int));
811                         MSG_DEBUG("[#%2d][%5d] address type = %d", deserial_index++, offset, to_addr->address_type);
812                         offset += sizeof(int);
813
814                         memcpy(to_addr->address_val, value + offset, sizeof(char)*MAX_ADDRESS_VAL_LEN);
815                         MSG_SEC_DEBUG("[#%2d][%5d] address val = %s", deserial_index++, offset, to_addr->address_val);
816                         offset += sizeof(char)*MAX_ADDRESS_VAL_LEN;
817
818                         pMmsData->header->to = g_list_append(pMmsData->header->to, (void *)to_addr);
819                 }
820
821                 memcpy(&addr_cnt, value + offset, sizeof(int));
822                 offset += sizeof(int);
823
824                 pMmsData->header->cc = NULL;
825
826                 for (i = 0; i < addr_cnt; i++) {
827                         MMS_ADDRESS_DATA_S* cc_addr = (MMS_ADDRESS_DATA_S*)calloc(1, sizeof(MMS_ADDRESS_DATA_S));
828                         if (cc_addr == NULL)
829                                 return -1;
830
831                         memcpy(&(cc_addr->address_type), value + offset, sizeof(int));
832                         MSG_DEBUG("[#%2d][%5d] address type = %d", deserial_index++, offset, cc_addr->address_type);
833                         offset += sizeof(int);
834
835                         memcpy(cc_addr->address_val, value + offset, sizeof(char)*MAX_ADDRESS_VAL_LEN);
836                         MSG_SEC_DEBUG("[#%2d][%5d] address val = %s", deserial_index++, offset, cc_addr->address_val);
837                         offset += sizeof(char)*MAX_ADDRESS_VAL_LEN;
838
839                         pMmsData->header->cc = g_list_append(pMmsData->header->cc, (void *)cc_addr);
840                 }
841
842                 memcpy(&addr_cnt, value + offset, sizeof(int));
843                 offset += sizeof(int);
844
845                 pMmsData->header->bcc = NULL;
846
847                 for (i = 0; i < addr_cnt; i++) {
848                         MMS_ADDRESS_DATA_S* bcc_addr = (MMS_ADDRESS_DATA_S*)calloc(1, sizeof(MMS_ADDRESS_DATA_S));
849                         if (bcc_addr == NULL)
850                                 return -1;
851
852                         memcpy(&(bcc_addr->address_type), value + offset, sizeof(int));
853                         MSG_DEBUG("[#%2d][%5d] address type = %d", deserial_index++, offset, bcc_addr->address_type);
854                         offset += sizeof(int);
855
856                         memcpy(bcc_addr->address_val, value + offset, sizeof(char)*MAX_ADDRESS_VAL_LEN);
857                         MSG_SEC_DEBUG("[#%2d][%5d] address val = %s", deserial_index++, offset, bcc_addr->address_val);
858                         offset += sizeof(char)*MAX_ADDRESS_VAL_LEN;
859
860                         pMmsData->header->bcc = g_list_append(pMmsData->header->bcc, (void *)bcc_addr);
861                 }
862         }
863
864         /* 3. Smil Data */
865         memcpy(&isExistSmil, value + offset, sizeof(int));
866         offset += sizeof(int);
867
868         if (isExistSmil) {
869                 pMmsData->smil = (MMS_MULTIPART_DATA_S *)calloc(1, sizeof(MMS_MULTIPART_DATA_S));
870                 if (pMmsData->smil == NULL)
871                         return -1;
872
873                 memcpy(pMmsData->smil, value + offset, sizeof(MMS_MULTIPART_DATA_S));
874                 offset += sizeof(MMS_MULTIPART_DATA_S);
875
876                 MSG_SEC_DEBUG("SMIL file path = [%s]", pMmsData->smil->szFilePath);
877                 MSG_DEBUG("SMIL nMultipartDataLen = [%zu]", pMmsData->smil->nMultipartDataLen);
878
879                 if (pMmsData->smil->nMultipartDataLen > 0) {
880                         pMmsData->smil->pMultipartData = (char *)calloc(1, sizeof(char)*pMmsData->smil->nMultipartDataLen);
881                         if (pMmsData->smil->pMultipartData == NULL)
882                                 return -1;
883
884                         memcpy(pMmsData->smil->pMultipartData, value + offset, sizeof(char)*pMmsData->smil->nMultipartDataLen);
885                         MSG_DEBUG("[#%2d][%5d] smil data ptr = %p", deserial_index++, offset, pMmsData->smil->pMultipartData);
886                         offset += sizeof(char)*pMmsData->smil->nMultipartDataLen;
887                 } else {
888                         pMmsData->smil->pMultipartData = NULL;
889                 }
890         }
891
892         /* 4. Multipart list data */
893         memcpy(&isExistMultipart, value + offset, sizeof(int));
894         offset += sizeof(int);
895
896         if (isExistMultipart) {
897                 memcpy(&multipart_cnt, value + offset, sizeof(int));
898                 offset += sizeof(int);
899
900                 MSG_DEBUG("Multipart list count = [ %d]", multipart_cnt);
901
902                 pMmsData->multipartlist = NULL;
903
904                 for (i = 0; i < multipart_cnt; i++) {
905                         MMS_MULTIPART_DATA_S *multipart_data = (MMS_MULTIPART_DATA_S *)calloc(1, sizeof(MMS_MULTIPART_DATA_S));
906                         if (multipart_data == NULL)
907                                 return -1;
908
909                         memcpy(multipart_data, value + offset, sizeof(MMS_MULTIPART_DATA_S));
910                         offset += sizeof(MMS_MULTIPART_DATA_S);
911
912                         MSG_SEC_DEBUG("Multipart file path = [%s]", multipart_data->szFilePath);
913
914                         if (multipart_data->nMultipartDataLen > 0) {
915                                 multipart_data->pMultipartData = (char *)calloc(1, sizeof(char)*multipart_data->nMultipartDataLen);
916                                 if (multipart_data->pMultipartData == NULL) {
917                                         free(multipart_data);
918                                         return -1;
919                                 }
920
921                                 memcpy(multipart_data->pMultipartData, value + offset, sizeof(char)*multipart_data->nMultipartDataLen);
922                                 MSG_DEBUG("[#%2d][%5d] multipart_data ptr = %p", deserial_index++, offset, multipart_data->pMultipartData);
923                                 offset += sizeof(char)*multipart_data->nMultipartDataLen;
924                         } else {
925                                 multipart_data->pMultipartData = NULL;
926                         }
927                         pMmsData->multipartlist = g_list_append(pMmsData->multipartlist, (void *)multipart_data);
928                 }
929         }
930
931         MSG_DEBUG("Final offset : %d", offset);
932
933         MSG_END();
934         return 0;
935 }