Coverity fix: check return type
[platform/core/messaging/msg-service.git] / utils / MsgMmsMessage.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 <stdio.h>
18 #include <string.h>
19 #include <glib.h>
20 #include <errno.h>
21 #include <sys/stat.h>
22 #include <sys/smack.h>
23
24 #include "MsgTypes.h"
25 #include "MsgMmsTypes.h"
26 #include "MsgUtilFile.h"
27 #include "MsgMmsMessage.h"
28 #include "MsgUtilFile.h"
29 #include "MsgUtilFunction.h"
30 #include "MsgSmil.h"
31 #include "MsgDebug.h"
32 #include "MsgSerialize.h"
33
34 static void __release_glist_element(gpointer data, gpointer user_data);
35 static void __release_page_element(gpointer data, gpointer user_data);
36 static msg_error_t __releasePageList(MMS_MESSAGE_DATA_S *pMsgData);
37 static msg_error_t __releaseRegionList(MMS_MESSAGE_DATA_S *pMsgData);
38 static msg_error_t __releaseAttachList(MMS_MESSAGE_DATA_S *pMsgData);
39 static msg_error_t __releaseTransitionList(MMS_MESSAGE_DATA_S *pMsgData);
40 static msg_error_t __releaseMetaList(MMS_MESSAGE_DATA_S *pMsgData);
41
42 void __release_glist_element(gpointer data, gpointer user_data)
43 {
44         if(data != NULL) {
45                 free(data);
46         }
47 }
48
49 void __release_page_element(gpointer data, gpointer user_data)
50 {
51         if(data != NULL) {
52                 MMS_PAGE_S *page = (MMS_PAGE_S *)data;
53
54                 if (page->medialist) {
55                         MMS_MEDIA_S *media = NULL;
56                         int mediaCnt = g_list_length(page->medialist);
57
58                         for (int i = 0; i < mediaCnt; i++) {
59                                 media = (MMS_MEDIA_S *)g_list_nth_data(page->medialist, i);
60                                 if (media)
61                                         free(media);
62                         }
63
64                         g_list_free(page->medialist);
65                         page->medialist = NULL;
66                         page->mediaCnt = 0;
67                 }
68
69                 free(page);
70         }
71 }
72
73 msg_error_t __releasePageList(MMS_MESSAGE_DATA_S *pMsgData)
74 {
75         if (pMsgData == NULL) {
76                 MSG_DEBUG("pMsgData is NULL");
77                 return MSG_ERR_INVALID_PARAMETER;
78         }
79
80         if (pMsgData->pagelist) {
81                 g_list_foreach(pMsgData->pagelist, __release_page_element, NULL);
82                 g_list_free(pMsgData->pagelist);
83                 pMsgData->pagelist = NULL;
84         }
85
86         pMsgData->pageCnt = 0;
87
88         return MSG_SUCCESS;
89 }
90
91 msg_error_t __releaseRegionList(MMS_MESSAGE_DATA_S *pMsgData)
92 {
93         if (pMsgData == NULL) {
94                 MSG_DEBUG("pMsgData is NULL");
95                 return MSG_ERR_INVALID_PARAMETER;
96         }
97
98         if (pMsgData->regionlist) {
99                 g_list_foreach(pMsgData->regionlist, __release_glist_element, NULL);
100                 g_list_free(pMsgData->regionlist);
101                 pMsgData->regionlist = NULL;
102         }
103
104         pMsgData->regionCnt = 0;
105
106         return MSG_SUCCESS;
107 }
108
109 msg_error_t __releaseAttachList(MMS_MESSAGE_DATA_S *pMsgData)
110 {
111         if (pMsgData == NULL) {
112                 MSG_DEBUG("pMsgData is NULL");
113                 return MSG_ERR_INVALID_PARAMETER;
114         }
115
116         if (pMsgData->attachlist) {
117                 g_list_foreach(pMsgData->attachlist, __release_glist_element, NULL);
118                 g_list_free(pMsgData->attachlist);
119                 pMsgData->attachlist = NULL;
120         }
121
122         pMsgData->attachCnt = 0;
123
124         return MSG_SUCCESS;
125 }
126
127 msg_error_t __releaseTransitionList(MMS_MESSAGE_DATA_S *pMsgData)
128 {
129         if (pMsgData == NULL) {
130                 MSG_DEBUG("pMsgData is NULL");
131                 return MSG_ERR_INVALID_PARAMETER;
132         }
133
134         if (pMsgData->transitionlist) {
135                 g_list_foreach(pMsgData->transitionlist, __release_glist_element, NULL);
136                 g_list_free(pMsgData->transitionlist);
137                 pMsgData->transitionlist = NULL;
138         }
139
140         pMsgData->transitionCnt = 0;
141
142         return MSG_SUCCESS;
143 }
144
145 msg_error_t __releaseMetaList(MMS_MESSAGE_DATA_S *pMsgData)
146 {
147         if (pMsgData == NULL) {
148                 MSG_DEBUG("pMsgData is NULL");
149                 return MSG_ERR_INVALID_PARAMETER;
150         }
151
152         if (pMsgData->metalist) {
153                 g_list_foreach(pMsgData->metalist, __release_glist_element, NULL);
154                 g_list_free(pMsgData->metalist);
155                 pMsgData->metalist = NULL;
156         }
157
158         pMsgData->metaCnt = 0;
159
160         return MSG_SUCCESS;
161 }
162
163 void MsgMmsReleaseMmsLists(MMS_MESSAGE_DATA_S *pMsgData)
164 {
165         __releasePageList(pMsgData);
166         __releaseRegionList(pMsgData);
167         __releaseAttachList(pMsgData);
168         __releaseTransitionList(pMsgData);
169         __releaseMetaList(pMsgData);
170 }
171
172 msg_error_t _MsgMmsAddRegion(MMS_MESSAGE_DATA_S *pMsgData, MMS_SMIL_REGION* pRegion)
173 {
174         if(pMsgData == NULL || pRegion == NULL)
175                 return MSG_ERR_INVALID_PARAMETER;
176
177         pMsgData->regionlist = g_list_append(pMsgData->regionlist, pRegion);
178         pMsgData->regionCnt++;
179
180         return MSG_SUCCESS;
181 }
182
183 msg_error_t _MsgMmsAddPage(MMS_MESSAGE_DATA_S *pMsgData, MMS_PAGE_S *pPage)
184 {
185         if(pMsgData == NULL || pPage == NULL)
186                 return MSG_ERR_INVALID_PARAMETER;
187
188         pMsgData->pagelist = g_list_append(pMsgData->pagelist, pPage);
189         pMsgData->pageCnt++;
190         MSG_DEBUG("MmsData's Page Count : %d", pMsgData->pageCnt);
191         return MSG_SUCCESS;
192 }
193
194 msg_error_t _MsgMmsAddMedia(MMS_PAGE_S* pPage, MMS_MEDIA_S *pMedia)
195 {
196         if(pPage == NULL || pMedia == NULL)
197                 return MSG_ERR_INVALID_PARAMETER;
198
199         pPage->medialist = g_list_append(pPage->medialist, pMedia);
200         pPage->mediaCnt++;
201         MSG_DEBUG("Page's media count: %d", pPage->mediaCnt);
202         return MSG_SUCCESS;
203 }
204
205 msg_error_t _MsgMmsAddTransition(MMS_MESSAGE_DATA_S *pMsgData, MMS_SMIL_TRANSITION* pTransition)
206 {
207         if(pMsgData == NULL || pTransition == NULL)
208                 return MSG_ERR_INVALID_PARAMETER;
209
210         pMsgData->transitionlist = g_list_append(pMsgData->transitionlist, pTransition);
211         pMsgData->transitionCnt++;
212
213         return MSG_SUCCESS;
214 }
215
216 msg_error_t _MsgMmsAddMeta(MMS_MESSAGE_DATA_S *pMsgData, MMS_SMIL_META* pMeta)
217 {
218         if(pMsgData == NULL || pMeta == NULL)
219                 return MSG_ERR_INVALID_PARAMETER;
220
221         pMsgData->metalist = g_list_append(pMsgData->metalist, pMeta);
222         pMsgData->metaCnt++;
223
224         return MSG_SUCCESS;
225 }
226
227 msg_error_t _MsgMmsAddAttachment(MMS_MESSAGE_DATA_S *pMsgData, MMS_ATTACH_S *pAttach)
228 {
229         if(pMsgData == NULL || pAttach == NULL)
230                 return MSG_ERR_INVALID_PARAMETER;
231
232         pMsgData->attachlist = g_list_append(pMsgData->attachlist, pAttach);
233         pMsgData->attachCnt++;
234
235         return MSG_SUCCESS;
236 }
237
238 int _MsgMmsGetPageCount(MMS_MESSAGE_DATA_S *pMsgData)
239 {
240         if (pMsgData == NULL) {
241                 MSG_DEBUG("pMsgData is NULL");
242                 return 0;
243         }
244
245         int count = 0;
246
247         if (pMsgData->pagelist)
248                 count = g_list_length(pMsgData->pagelist);
249
250         MSG_DEBUG("Page Count: %d", count);
251         return count;
252 }
253
254 MMS_PAGE_S *_MsgMmsGetPage(MMS_MESSAGE_DATA_S *pMsgData, int pageIdx)
255 {
256         if (pMsgData == NULL) {
257                 MSG_DEBUG("pMsgData is NULL");
258                 return NULL;
259         }
260
261         MMS_PAGE_S *page = NULL;
262
263         if (pMsgData->pagelist)
264                 page = (MMS_PAGE_S *)g_list_nth_data(pMsgData->pagelist, pageIdx);
265
266         return page;
267 }
268
269 int _MsgMmsGetAttachCount(MMS_MESSAGE_DATA_S *pMsgData)
270 {
271         if (pMsgData == NULL) {
272                 MSG_DEBUG("pMsgData is NULL");
273                 return 0;
274         }
275
276         int count = 0;
277
278         if (pMsgData->attachlist)
279                 count = g_list_length(pMsgData->attachlist);
280
281         MSG_DEBUG("Attachment Count: %d", count);
282         return count;
283 }
284
285 MMS_ATTACH_S *_MsgMmsGetAttachment(MMS_MESSAGE_DATA_S *pMsgData, int attachIdx)
286 {
287         if (pMsgData == NULL) {
288                 MSG_DEBUG("pMsgData is NULL");
289                 return NULL;
290         }
291
292         MMS_ATTACH_S *attach = NULL;
293         if (pMsgData->attachlist)
294                 attach = (MMS_ATTACH_S *)g_list_nth_data(pMsgData->attachlist, attachIdx);
295
296         return attach;
297 }
298
299 MMS_SMIL_REGION *_MsgMmsGetSmilRegion(MMS_MESSAGE_DATA_S *pMsgData, int regionIdx)
300 {
301         if (pMsgData == NULL) {
302                 MSG_DEBUG("pMsgData is NULL");
303                 return NULL;
304         }
305
306         MMS_SMIL_REGION *region = NULL;
307
308         if (pMsgData->regionlist)
309                 region = (MMS_SMIL_REGION *)g_list_nth_data(pMsgData->regionlist, regionIdx);
310
311         return region;
312 }
313
314 MMS_MEDIA_S *_MsgMmsGetMedia(MMS_PAGE_S *pPage, int mediaIdx)
315 {
316         if (!pPage) {
317                 MSG_FATAL("pPage is NULL");
318                 return NULL;
319         }
320
321         if (mediaIdx > pPage->mediaCnt || mediaIdx < 0) {
322                 MSG_FATAL("Invalid media index = %d", mediaIdx);
323                 return NULL;
324         }
325
326         MMS_MEDIA_S *media = NULL;
327         if (pPage->medialist)
328                 media = (MMS_MEDIA_S *)g_list_nth_data(pPage->medialist, mediaIdx);
329
330         return media;
331 }
332
333 int _MsgMmsGetTransitionCount(MMS_MESSAGE_DATA_S *pMsgData)
334 {
335         if (pMsgData == NULL) {
336                 MSG_DEBUG("pMsgData is NULL");
337                 return 0;
338         }
339
340         int count = 0;
341
342         if (pMsgData->transitionlist)
343                 count = g_list_length(pMsgData->transitionlist);
344
345         MSG_DEBUG("Transition Count: %d", count);
346         return count;
347 }
348
349 MMS_SMIL_TRANSITION *_MsgMmsGetTransition(MMS_MESSAGE_DATA_S *pMsgData, int transitionIdx)
350 {
351         if (pMsgData == NULL) {
352                 MSG_DEBUG("pMsgData is NULL");
353                 return NULL;
354         }
355
356         MMS_SMIL_TRANSITION *transition = NULL;
357         if (pMsgData->transitionlist)
358                 transition = (MMS_SMIL_TRANSITION *)g_list_nth_data(pMsgData->transitionlist, transitionIdx);
359
360         return transition;
361 }
362
363 MMS_SMIL_META *_MsgMmsGetMeta(MMS_MESSAGE_DATA_S *pMsgData, int metaIdx)
364 {
365         if (pMsgData == NULL) {
366                 MSG_DEBUG("pMsgData is NULL");
367                 return NULL;
368         }
369
370         MMS_SMIL_META *meta = NULL;
371
372         if (pMsgData->metalist)
373                 meta = (MMS_SMIL_META *)g_list_nth_data(pMsgData->metalist, metaIdx);
374
375         return meta;
376 }
377
378 int     _MsgMmsGetMetaCount(MMS_MESSAGE_DATA_S *pMsgData)
379 {
380         if (pMsgData == NULL) {
381                 MSG_DEBUG("pMsgData is NULL");
382                 return 0;
383         }
384
385         int count = 0;
386
387         if (pMsgData->metalist)
388                 count = g_list_length(pMsgData->metalist);
389
390         MSG_DEBUG("Meta Count: %d", count);
391         return count;
392 }
393
394 bool _MsgMmsSetRootLayout(MMS_MESSAGE_DATA_S *pMsgData, MMS_SMIL_ROOTLAYOUT *pRootlayout)
395 {
396         memcpy(&pMsgData->rootlayout, pRootlayout, sizeof(MMS_SMIL_ROOTLAYOUT));
397         return true;
398 }
399
400 char* _MsgMmsSerializeMessageData(const MMS_MESSAGE_DATA_S *pMsgData, unsigned int *pSize)
401 {
402         MSG_BEGIN();
403
404         if (pMsgData == NULL)
405                 return NULL;
406
407         int bufsize = 0;
408         int offset = 0;
409         int pageCnt = 0;
410         char *buf = NULL;
411
412         bufsize += sizeof(int); /* Page cnt */
413
414         pageCnt = pMsgData->pageCnt;
415
416         if (pMsgData->pagelist) {
417                 for (int pageIdx = 0; pageIdx < pageCnt; pageIdx++) {
418                         int mediaCnt = 0;
419
420                         MMS_PAGE_S *page = (MMS_PAGE_S *)g_list_nth_data(pMsgData->pagelist, pageIdx);
421
422                         mediaCnt = page->mediaCnt;
423
424                         bufsize += sizeof(int); /* Media cnt */
425
426                         if (page->medialist) {
427                                 bufsize += sizeof(MMS_MEDIA_S) * mediaCnt;
428                         }
429
430                         bufsize += sizeof(int) * 6; /* Dur, Begin, End, Min, Max, Repeat */
431                 }
432         }
433
434         bufsize += sizeof(int); /* region count; */
435
436         if (pMsgData->regionlist) {
437                 int elementSize = g_list_length(pMsgData->regionlist);
438                 bufsize += elementSize * (sizeof(MMS_SMIL_REGION));
439         }
440
441         bufsize += sizeof(int); /* attachment count; */
442         if (pMsgData->attachlist) {
443                 int elementSize = g_list_length(pMsgData->attachlist);
444                 bufsize += elementSize * sizeof(MMS_ATTACH_S);
445         }
446
447         bufsize += sizeof(int); /* transition count; */
448         if (pMsgData->transitionlist) {
449                 int elementSize = g_list_length(pMsgData->transitionlist);
450                 bufsize += elementSize * sizeof(MMS_SMIL_TRANSITION);
451         }
452
453         bufsize += sizeof(int); /* meta count; */
454         if (pMsgData->metalist) {
455                 int elementSize = g_list_length(pMsgData->metalist);
456                 bufsize += elementSize * sizeof(MMS_SMIL_META);
457         }
458
459         bufsize += sizeof(MMS_SMIL_ROOTLAYOUT);
460
461 #ifdef FEATURE_JAVA_MMS
462         bufsize += sizeof(MMS_APPID_INFO_S);
463 #endif
464
465         int filePathLen = strlen(pMsgData->szSmilFilePath);
466
467         bufsize += sizeof(int) + filePathLen;
468
469         bufsize += sizeof(int); /* type */
470         bufsize += sizeof(MMS_HEADER_DATA_S);
471         bufsize += sizeof(MMS_MULTIPART_DATA_S);
472
473         MSG_DEBUG("Serialize bufsize = %d", bufsize);
474
475         buf = (char *)calloc(bufsize, 1);
476
477         if (buf == NULL)
478                 return buf;
479
480         int serial_index = 0;
481
482         memcpy(buf, &pMsgData->backup_type, sizeof(int));
483         MSG_DEBUG("[#%2d][%5d] backup type = %d", serial_index++, offset, pMsgData->backup_type);
484         offset += sizeof(int);
485
486         /* smilFilePath */
487         memcpy(buf + offset , &filePathLen, sizeof(int));
488
489         /* copy file path */
490         MSG_SEC_DEBUG("[#%2d][%5d] smilFilePath = %s, len = %d", serial_index++, offset, pMsgData->szSmilFilePath, filePathLen);
491         offset += sizeof(int);
492
493         if (filePathLen > 0) {
494                 memcpy(buf + offset, pMsgData->szSmilFilePath, filePathLen);
495                 offset += filePathLen;
496         }
497
498         /* copy page count */
499         MSG_DEBUG("[#%2d][%5d] page count = %d", serial_index++, offset, pMsgData->pageCnt);
500         memcpy(buf + offset, &(pMsgData->pageCnt), sizeof(int));
501         offset += sizeof(int);
502
503         if (pMsgData->pagelist) {
504                 for (int pageIdx = 0; pageIdx < pageCnt; pageIdx++) {
505                         MMS_PAGE_S *page = (MMS_PAGE_S *)g_list_nth_data(pMsgData->pagelist, pageIdx);
506
507                         MSG_DEBUG("[#%2d][%5d][%d page] media count = %d", serial_index++, offset, pageIdx, page->mediaCnt);
508                         memcpy(buf + offset, &page->mediaCnt, sizeof(int));
509                         offset += sizeof(int);
510
511                         if (page->medialist) {
512                                 for (int i = 0; i < page->mediaCnt; ++i) {
513                                         MMS_MEDIA_S *media = (MMS_MEDIA_S *)g_list_nth_data(page->medialist, i);
514                                         memcpy(buf + offset, media, sizeof(MMS_MEDIA_S));
515                                         offset += sizeof(MMS_MEDIA_S);
516                                         MSG_SEC_DEBUG("[#%2d][%5d][%d page][%d media] media type [%d],  drm type [%d], filepath [%s]", serial_index++, offset, pageIdx, i, media->mediatype, media->drmType, media->szFilePath);
517                                 }
518                         }
519
520                         memcpy(buf + offset, &page->nDur, sizeof(int));
521                         offset += sizeof(int);
522                         memcpy(buf + offset, &page->nBegin, sizeof(int));
523                         offset += sizeof(int);
524                         memcpy(buf + offset, &page->nEnd, sizeof(int));
525                         offset += sizeof(int);
526                         memcpy(buf + offset, &page->nMin, sizeof(int));
527                         offset += sizeof(int);
528                         memcpy(buf + offset, &page->nMax, sizeof(int));
529                         offset += sizeof(int);
530                         memcpy(buf + offset, &page->nRepeat, sizeof(int));
531                         offset += sizeof(int);
532                 }
533         }
534
535         MSG_DEBUG("[#%2d][%5d] region count = %d", serial_index++, offset, pMsgData->regionCnt);
536
537         memcpy(buf + offset, &pMsgData->regionCnt, sizeof(int));
538         offset += sizeof(int);
539
540         if (pMsgData->regionlist) {
541                 for (int i = 0; i < pMsgData->regionCnt; ++i) {
542                         MMS_SMIL_REGION *region = (MMS_SMIL_REGION *)g_list_nth_data(pMsgData->regionlist, i);
543                         memcpy(buf + offset, region, sizeof(MMS_SMIL_REGION));
544                         offset += sizeof(MMS_SMIL_REGION);
545                 }
546         }
547
548         MSG_DEBUG("[#%2d][%5d] attach count = %d", serial_index++, offset, pMsgData->attachCnt);
549
550         memcpy(buf + offset, &pMsgData->attachCnt, sizeof(int));
551         offset += sizeof(int);
552
553         if (pMsgData->attachlist) {
554                 for (int i = 0; i < pMsgData->attachCnt; ++i) {
555                         MMS_ATTACH_S *attach = (MMS_ATTACH_S *)g_list_nth_data(pMsgData->attachlist, i);
556                         memcpy(buf + offset, attach, sizeof(MMS_ATTACH_S));
557                         offset += sizeof(MMS_ATTACH_S);
558                         MSG_SEC_DEBUG("[#%2d][%5d][%d attach] attach filepath = %s, drm type = [%d]", serial_index++, offset, i, attach->szFilePath, attach->drmType);
559                 }
560         }
561
562         MSG_DEBUG("[#%2d][%5d] transition count = %d", serial_index++, offset, pMsgData->transitionCnt);
563
564         memcpy(buf + offset, &pMsgData->transitionCnt, sizeof(int));
565         offset += sizeof(int);
566         if (pMsgData->transitionlist) {
567                 for (int i = 0; i < pMsgData->transitionCnt; ++i) {
568                         MMS_SMIL_TRANSITION *transition = (MMS_SMIL_TRANSITION *)g_list_nth_data(pMsgData->transitionlist, i);
569                         memcpy(buf + offset, transition, sizeof(MMS_SMIL_TRANSITION));
570                         offset += sizeof(MMS_SMIL_TRANSITION);
571                 }
572         }
573
574         MSG_DEBUG("[#%2d][%5d] meta count = %d", serial_index++, offset, pMsgData->metaCnt);
575         memcpy(buf + offset, &pMsgData->metaCnt, sizeof(int));
576         offset += sizeof(int);
577
578         if (pMsgData->metalist) {
579                 for (int i = 0; i < pMsgData->metaCnt; ++i) {
580                         MMS_SMIL_META *meta = (MMS_SMIL_META *)g_list_nth_data(pMsgData->metalist, i);
581
582                         memcpy(buf + offset, meta, sizeof(MMS_SMIL_META));
583                         offset += sizeof(MMS_SMIL_META);
584                 }
585         }
586
587         MSG_DEBUG("[#%2d][%5d] root layout", serial_index++, offset);
588         memcpy(buf + offset, &pMsgData->rootlayout, sizeof(MMS_SMIL_ROOTLAYOUT));
589         offset += sizeof(MMS_SMIL_ROOTLAYOUT);
590
591 #ifdef FEATURE_JAVA_MMS
592         MSG_DEBUG("[#%2d][%5d] java mms", serial_index++, offset);
593         memcpy(buf + offset, &pMsgData->msgAppId, sizeof(MMS_APPID_INFO_S));
594         offset += sizeof(MMS_APPID_INFO_S);
595 #endif
596
597         memcpy (buf + offset, &pMsgData->header, sizeof(MMS_HEADER_DATA_S));
598         MSG_DEBUG("[#%2d][%5d] mms header", serial_index++, offset);
599         offset += sizeof(MMS_HEADER_DATA_S);
600
601         memcpy (buf + offset, &pMsgData->smil, sizeof(MMS_MULTIPART_DATA_S));
602         MSG_DEBUG("[#%2d][%5d] mms smil", serial_index++, offset);
603         offset += sizeof(MMS_MULTIPART_DATA_S);
604
605         *pSize = offset;
606
607         MSG_DEBUG("Expect Buffer Size: %d, Final offset : %d", bufsize, offset);
608         MSG_END();
609         return buf;
610 }
611
612 bool _MsgMmsDeserializeMessageData(MMS_MESSAGE_DATA_S *pMsgData, const char *pData)
613 {
614         MSG_BEGIN();
615
616         if (pMsgData == NULL || pData == NULL) {
617                 MSG_DEBUG("param is NULL. pBody = %p, pData = %s", pMsgData, pData);
618                 return false;
619         }
620
621         int offset = 0;
622         int pageCnt = 0;
623         int filePathLen = 0;
624
625         MMS_PAGE_S *pPage = NULL;
626         MMS_MEDIA_S *pMedia = NULL;
627         MMS_SMIL_REGION *pRegion = NULL;
628         MMS_ATTACH_S *pAttach = NULL;
629         MMS_SMIL_TRANSITION *pTransition = NULL;
630         MMS_SMIL_META *pMeta = NULL;
631
632         int serial_index = 0;
633
634         int type;
635
636         memcpy(&type, pData, sizeof(int));
637         MSG_DEBUG("[#%2d][%5d] backup type = %d", serial_index++, offset, type);
638         offset += sizeof(int);
639
640         pMsgData->backup_type = type;
641
642         memcpy(&filePathLen, pData + offset, sizeof(int));
643         MSG_DEBUG("[#%2d][%5d] smil path len = %d", serial_index, offset, filePathLen);
644         offset += sizeof(int);
645
646         if (filePathLen > MSG_FILEPATH_LEN_MAX) {
647                 MSG_DEBUG("Smil File Path Length is abnormal.");
648                 return false;
649         }
650
651         memset(pMsgData->szSmilFilePath, 0x00, MSG_FILEPATH_LEN_MAX);
652
653         if (filePathLen > 0) {
654                 memcpy(pMsgData->szSmilFilePath, pData + offset, filePathLen);
655                 MSG_DEBUG("[#%2d][%5d] smil path = %s", serial_index, offset, pMsgData->szSmilFilePath);
656                 offset += filePathLen;
657         }
658
659         serial_index++;
660
661         memcpy(&(pMsgData->pageCnt), pData + offset, sizeof(int));
662         MSG_DEBUG("[#%2d][%5d] page count = %d", serial_index++, offset, pMsgData->pageCnt);
663         offset += sizeof(int);
664
665         pageCnt = pMsgData->pageCnt;
666
667         for (int j = 0; j < pageCnt; ++j) {
668                 pPage = (MMS_PAGE_S *)calloc(sizeof(MMS_PAGE_S), 1);
669                 if (pPage == NULL)
670                         return false;
671
672                 memcpy(&pPage->mediaCnt, pData + offset, sizeof(int));
673                 MSG_DEBUG("[#%2d][%5d][%d page] media count = %d", serial_index++, offset, j, pPage->mediaCnt);
674                 offset += sizeof(int);
675
676                 for (int i = 0; i < pPage->mediaCnt; ++i) {
677                         pMedia = (MMS_MEDIA_S *)calloc(sizeof(MMS_MEDIA_S), 1);
678                         if (pMedia == NULL) {
679                                 free(pPage);
680                                 return false;
681                         }
682
683                         memcpy(pMedia, pData + offset, sizeof(MMS_MEDIA_S));
684
685                         offset += sizeof(MMS_MEDIA_S);
686
687                         MSG_SEC_DEBUG("[#%2d][%5d][%d page][%d media] media type [%d], drm type [%d], filepath [%s], content type [%s]", serial_index++, offset, j, i, pMedia->mediatype, pMedia->drmType, pMedia->szFilePath, pMedia->szContentType);
688
689                         if (strlen(pMedia->szFilePath) >0) {
690                                 pPage->medialist = g_list_append(pPage->medialist, pMedia);
691                         } else {
692                                 free(pMedia);
693                         }
694                 }
695
696                 pPage->mediaCnt = g_list_length(pPage->medialist);
697
698                 memcpy(&pPage->nDur , pData + offset, sizeof(int));
699                 offset += sizeof(int);
700                 memcpy(&pPage->nBegin , pData + offset, sizeof(int));
701                 offset += sizeof(int);
702                 memcpy(&pPage->nEnd , pData + offset, sizeof(int));
703                 offset += sizeof(int);
704                 memcpy(&pPage->nMin , pData + offset, sizeof(int));
705                 offset += sizeof(int);
706                 memcpy(&pPage->nMax , pData + offset, sizeof(int));
707                 offset += sizeof(int);
708                 memcpy(&pPage->nRepeat , pData + offset, sizeof(int));
709                 offset += sizeof(int);
710
711                 pMsgData->pagelist = g_list_append(pMsgData->pagelist, pPage);
712 #if 0
713                 if (pPage->medialist) {
714                         pMsgData->pagelist = g_list_append(pMsgData->pagelist, pPage);
715                 } else {
716                         free(pPage);
717                         pPage = NULL;
718                 }
719 #endif
720         }
721
722         pMsgData->pageCnt = g_list_length(pMsgData->pagelist);
723
724         /* Processing Region List */
725         memcpy(&pMsgData->regionCnt, pData + offset, sizeof(int));
726         MSG_DEBUG("[#%2d][%5d] region count = %d", serial_index++, offset, pMsgData->regionCnt);
727         offset += sizeof(int);
728
729         /* MSG_DEBUG(" pBody->regionCnt: %d", pBody->regionCnt); */
730
731         for (int i = 0; i < pMsgData->regionCnt; ++i) {
732                 pRegion = (MMS_SMIL_REGION *)calloc(sizeof(MMS_SMIL_REGION), 1);
733                 if (pRegion == NULL)
734                         return false;
735
736                 memcpy(pRegion, pData + offset, sizeof(MMS_SMIL_REGION));
737                 offset += sizeof(MMS_SMIL_REGION);
738
739                 pMsgData->regionlist = g_list_append(pMsgData->regionlist, pRegion);
740         }
741
742         /* Processing Attachment List */
743         memcpy(&pMsgData->attachCnt, pData + offset, sizeof(int));
744         MSG_DEBUG("[#%2d][%5d] attach count = %d", serial_index++, offset, pMsgData->attachCnt);
745         offset += sizeof(int);
746
747         for (int i = 0; i < pMsgData->attachCnt; ++i) {
748                 pAttach = (MMS_ATTACH_S *)calloc(sizeof(MMS_ATTACH_S), 1);
749                 if (pAttach == NULL)
750                         return false;
751
752                 memcpy(pAttach, pData + offset, sizeof(MMS_ATTACH_S));
753                 offset += sizeof(MMS_ATTACH_S);
754
755                 MSG_SEC_DEBUG("[#%2d][%5d][%d attach] drm type [%d],  attach filepath = [%s], content type = [%s]", serial_index++, offset, i, pAttach->drmType, pAttach->szFilePath, pAttach->szContentType);
756                 if (strlen(pAttach->szFilePath) >0) {
757                         pMsgData->attachlist = g_list_append(pMsgData->attachlist, pAttach);
758                 } else {
759                         free(pAttach);
760                         pAttach = NULL;
761                 }
762         }
763
764         pMsgData->attachCnt = g_list_length(pMsgData->attachlist);
765
766         /* Processing Transition List */
767         memcpy(&pMsgData->transitionCnt, pData + offset, sizeof(int));
768         MSG_DEBUG("[#%2d][%5d] transition count = %d", serial_index++, offset, pMsgData->transitionCnt);
769         offset += sizeof(int);
770
771         for (int i = 0; i < pMsgData->transitionCnt; ++i) {
772                 pTransition = (MMS_SMIL_TRANSITION *)calloc(sizeof(MMS_SMIL_TRANSITION), 1);
773                 if (pTransition == NULL)
774                         return false;
775
776                 memcpy(pTransition, pData + offset, sizeof(MMS_SMIL_TRANSITION));
777                 offset += sizeof(MMS_SMIL_TRANSITION);
778
779                 pMsgData->transitionlist = g_list_append(pMsgData->transitionlist, pTransition);
780         }
781
782         /* Processing Meta List */
783         memcpy(&pMsgData->metaCnt, pData + offset, sizeof(int));
784         MSG_DEBUG("[#%2d][%5d] meta count = %d", serial_index++, offset, pMsgData->metaCnt);
785         offset += sizeof(int);
786
787         for (int i = 0; i < pMsgData->metaCnt; ++i) {
788                 pMeta = (MMS_SMIL_META *)calloc(sizeof(MMS_SMIL_META), 1);
789                 if (pMeta == NULL)
790                         return false;
791
792                 memcpy(pMeta, pData + offset, sizeof(MMS_SMIL_META));
793                 offset += sizeof(MMS_SMIL_META);
794
795                 pMsgData->metalist = g_list_append(pMsgData->metalist, pMeta);
796         }
797
798         MSG_DEBUG("[#%2d][%5d] root layout", serial_index++, offset);
799         memcpy(&pMsgData->rootlayout, pData + offset, sizeof(MMS_SMIL_ROOTLAYOUT));
800         offset += sizeof(MMS_SMIL_ROOTLAYOUT);
801
802 #ifdef FEATURE_JAVA_MMS
803         MSG_DEBUG("[#%2d][%5d] java mms", serial_index++, offset);
804         memcpy(&pMsgData->msgAppId, pData + offset, sizeof(MMS_APPID_INFO_S));
805         offset += sizeof(MMS_APPID_INFO_S);
806 /*      MSG_DEBUG("java_app_id valid:%d, appId:%s repleToAppId:%s", pBody->msgAppId.valid, pBody->msgAppId.appId, pBody->msgAppId.replyToAppId); */
807 #endif
808
809         memcpy(&pMsgData->header, pData + offset, sizeof(MMS_HEADER_DATA_S));
810         MSG_DEBUG("[#%2d][%5d] mms header", serial_index++, offset);
811         offset += sizeof(MMS_HEADER_DATA_S);
812
813         memcpy(&pMsgData->smil, pData + offset, sizeof(MMS_MULTIPART_DATA_S));
814         MSG_DEBUG("[#%2d][%5d] mms smil", serial_index++, offset);
815         offset += sizeof(MMS_MULTIPART_DATA_S);
816
817         MSG_DEBUG("Final offset : %d", offset);
818         MSG_END();
819         return true;
820 }
821
822 void _MsgMmsAttachPrint(MMS_ATTACH_S *attach)
823 {
824         if (attach == NULL) {
825                 MSG_DEBUG("Invalid Parameter");
826                 return;
827         }
828
829         MSG_DEBUG("%-25s : %d", "Attach Mimetype", attach->mediatype);
830         MSG_SEC_DEBUG("%-25s : %s", "Attach content type", attach->szContentType);
831         MSG_SEC_DEBUG("%-25s : %s", "Attach filename", attach->szFileName);
832         MSG_SEC_DEBUG("%-25s : %s", "Attach filepath", attach->szFilePath);
833         MSG_DEBUG("%-25s : %d", "Attach filesize", attach->fileSize);
834         MSG_DEBUG("%-25s : %d", "Attach drm type", attach->drmType);
835         MSG_SEC_DEBUG("%-25s : %s", "Attach drm filepath", attach->szDrm2FullPath);
836 }
837
838 void _MsgMmsMediaPrint(MMS_MEDIA_S *media)
839 {
840         if (media == NULL) {
841                 MSG_DEBUG("Invalid Parameter");
842                 return;
843         }
844
845         if (media->mediatype == MMS_SMIL_MEDIA_INVALID) {
846                 MSG_DEBUG("%-25s : %s", "Media type", "MMS_SMIL_MEDIA_INVALID");
847         } else if (media->mediatype == MMS_SMIL_MEDIA_IMG) {
848                 MSG_DEBUG("%-25s : %s", "Media type", "MMS_SMIL_MEDIA_IMG");
849         } else if (media->mediatype == MMS_SMIL_MEDIA_AUDIO) {
850                 MSG_DEBUG("%-25s : %s", "Media type", "MMS_SMIL_MEDIA_AUDIO");
851         } else if (media->mediatype == MMS_SMIL_MEDIA_VIDEO) {
852                 MSG_DEBUG("%-25s : %s", "Media type", "MMS_SMIL_MEDIA_VIDEO");
853         } else if (media->mediatype == MMS_SMIL_MEDIA_TEXT) {
854                 MSG_DEBUG("%-25s : %s", "Media type", "MMS_SMIL_MEDIA_TEXT");
855         } else if (media->mediatype == MMS_SMIL_MEDIA_ANIMATE) {
856                 MSG_DEBUG("%-25s : %s", "Media type", "MMS_SMIL_MEDIA_ANIMATE");
857         } else if (media->mediatype == MMS_SMIL_MEDIA_IMG_OR_VIDEO) {
858                 MSG_DEBUG("%-25s : %s", "Media type", "MMS_SMIL_MEDIA_REF");
859         } else {
860                 MSG_DEBUG("%-25s : Unknown [%d]", "Media type", media->mediatype);
861         }
862
863         MSG_DEBUG("%-25s : %s", "Media src", media->szSrc);
864         MSG_SEC_DEBUG("%-25s : %s", "Media filename", media->szFileName);
865         MSG_SEC_DEBUG("%-25s : %s", "Media filepath", media->szFilePath);
866         MSG_SEC_DEBUG("%-25s : %s", "Media content type", media->szContentType);
867         MSG_SEC_DEBUG("%-25s : %s", "Media content id", media->szContentID);
868         MSG_SEC_DEBUG("%-25s : %s", "Media content location", media->szContentLocation);
869         MSG_SEC_DEBUG("%-25s : %s", "Media region id", media->regionId);
870         MSG_DEBUG("%-25s : %s", "Media alt", media->szAlt);
871         MSG_DEBUG("%-25s : %d", "Media drm type", media->drmType);
872         MSG_SEC_DEBUG("%-25s : %s", "Media drm filepath", media->szDrm2FullPath);
873 }
874
875 void _MsgMmsPagePrint(MMS_PAGE_S *page)
876 {
877         if (page == NULL) {
878                 MSG_DEBUG("Invalid Parameter");
879                 return;
880         }
881
882         MSG_DEBUG("%-25s : %d", "Page duration", page->nDur);
883
884         if (page->medialist) {
885                 int list_count = g_list_length(page->medialist);
886                 MSG_DEBUG("Media Count is [%d]", list_count);
887                 for (int i = 0; i < list_count; i++) {
888                         MMS_MEDIA_S *media = (MMS_MEDIA_S *)g_list_nth_data(page->medialist, i);
889                         if (media) {
890                                 MSG_DEBUG("[%d]th Media", i);
891                                 _MsgMmsMediaPrint(media);
892                         } else {
893                                 MSG_DEBUG("Not Exist Media Data in [%d]th", i);
894                         }
895                 }
896         }
897 }
898
899 void _MsgMmsPrint(MMS_MESSAGE_DATA_S *pMsgData)
900 {
901         if (pMsgData == NULL) {
902                 MSG_DEBUG("Invalid Parameter");
903                 return;
904         }
905
906         if (pMsgData->pagelist) {
907                 int list_count = g_list_length(pMsgData->pagelist);
908                 MSG_DEBUG("Page Count is [%d]", list_count);
909                 for (int i = 0; i < list_count; i++) {
910                         MMS_PAGE_S *page = (MMS_PAGE_S *)g_list_nth_data(pMsgData->pagelist, i);
911                         if (page) {
912                                 MSG_DEBUG("[%d]th Page", i);
913                                 _MsgMmsPagePrint(page);
914                         } else {
915                                 MSG_DEBUG("Not Exist Page Data in [%d]th", i);
916                         }
917                 }
918         }
919
920         if (pMsgData->attachlist) {
921                 int list_count = g_list_length(pMsgData->attachlist);
922                 MSG_DEBUG("Attach Count is [%d]", list_count);
923                 for (int i = 0; i < list_count; i++) {
924                         MMS_ATTACH_S *attach = (MMS_ATTACH_S *)g_list_nth_data(pMsgData->attachlist, i);
925                         if (attach) {
926                                 MSG_DEBUG("[%d]th Attach", i);
927                                 _MsgMmsAttachPrint(attach);
928                         } else {
929                                 MSG_DEBUG("Not Exist Attach Data in [%d]th", i);
930                         }
931                 }
932         }
933 }
934
935 MMS_ADDRESS_DATA_S *MsgMmsCreateAddress(int addressType, const char *addressVal)
936 {
937         MMS_ADDRESS_DATA_S * pMmsAddressData = (MMS_ADDRESS_DATA_S * )calloc(1, sizeof(MMS_ADDRESS_DATA_S));
938         if (pMmsAddressData != NULL) {
939                 pMmsAddressData->address_type = addressType;
940                 pMmsAddressData->address_val = strdup(addressVal);
941         }
942         return pMmsAddressData;
943 }
944
945 void MsgMmsReleaseAddress(MMS_ADDRESS_DATA_S **ppMmsAddressData)
946 {
947         if (ppMmsAddressData && *ppMmsAddressData) {
948                 MMS_ADDRESS_DATA_S *pMmsAddressData = *ppMmsAddressData;
949
950                 if (pMmsAddressData->address_val) {
951                         free(pMmsAddressData->address_val);
952                         pMmsAddressData->address_val = NULL;
953                 }
954
955                 free(pMmsAddressData);
956                 *ppMmsAddressData = NULL;
957         }
958 }
959
960 void MsgMmsInitMultipart(MMS_MULTIPART_DATA_S *pMmsMultipart)
961 {
962         if (pMmsMultipart) {
963                 pMmsMultipart->type = MIME_UNKNOWN;
964                 pMmsMultipart->szContentType[0] = '\0';
965                 pMmsMultipart->szFileName[0] = '\0';
966                 pMmsMultipart->szContentID[0] = '\0';
967                 pMmsMultipart->szContentLocation[0] = '\0';
968                 pMmsMultipart->drmType = MSG_DRM_TYPE_NONE;
969                 pMmsMultipart->szFilePath[0] = '\0';
970                 pMmsMultipart->pMultipartData = NULL;
971                 pMmsMultipart->nMultipartDataLen = 0;
972                 pMmsMultipart->tcs_bc_level = -1;
973                 pMmsMultipart->malware_allow = 0;
974                 pMmsMultipart->szThumbFilePath[0] = '\0';
975         }
976 }
977
978 MMS_MULTIPART_DATA_S *MsgMmsCreateMultipart()
979 {
980         MMS_MULTIPART_DATA_S *pMmsMultipart = (MMS_MULTIPART_DATA_S *)calloc(1, sizeof(MMS_MULTIPART_DATA_S));
981         if (pMmsMultipart) {
982                 pMmsMultipart->type = MIME_UNKNOWN;
983                 pMmsMultipart->szContentType[0] = '\0';
984                 pMmsMultipart->szFileName[0] = '\0';
985                 pMmsMultipart->szContentID[0] = '\0';
986                 pMmsMultipart->szContentLocation[0] = '\0';
987                 pMmsMultipart->drmType = MSG_DRM_TYPE_NONE;
988                 pMmsMultipart->szFilePath[0] = '\0';
989                 pMmsMultipart->pMultipartData = NULL;
990                 pMmsMultipart->nMultipartDataLen = 0;
991                 pMmsMultipart->tcs_bc_level = -1;
992                 pMmsMultipart->malware_allow = 0;
993                 pMmsMultipart->szThumbFilePath[0] = '\0';
994         }
995
996         return pMmsMultipart;
997 }
998
999 void MsgMmsReleaseMultipart(MMS_MULTIPART_DATA_S **ppMmsMultipart)
1000 {
1001         if (ppMmsMultipart && *ppMmsMultipart) {
1002                 MMS_MULTIPART_DATA_S *pDelMmsMultipart = *ppMmsMultipart;
1003
1004                 if (pDelMmsMultipart->pMultipartData != NULL) {
1005                         g_free(pDelMmsMultipart->pMultipartData);
1006                         pDelMmsMultipart->pMultipartData = NULL;
1007                 }
1008                 g_free(pDelMmsMultipart);
1009                 *ppMmsMultipart = NULL;
1010         }
1011 }
1012
1013 void MsgMmsInitHeader(MMS_HEADER_DATA_S *pMmsHeaderData)
1014 {
1015         if (pMmsHeaderData) {
1016                 pMmsHeaderData->bcc = NULL;
1017                 pMmsHeaderData->cc = NULL;
1018                 pMmsHeaderData->contentLocation[0] = '\0';
1019                 pMmsHeaderData->szContentType[0] = '\0';
1020                 pMmsHeaderData->date = 0;
1021                 pMmsHeaderData->bDeliveryReport = false;
1022                 pMmsHeaderData->delivery.type = MMS_TIMETYPE_NONE;
1023                 pMmsHeaderData->delivery.time = 0;
1024                 pMmsHeaderData->expiry.type = MMS_TIMETYPE_NONE;
1025                 pMmsHeaderData->expiry.time = 0;
1026                 pMmsHeaderData->szFrom[0] = '\0';
1027                 pMmsHeaderData->messageClass = -1;
1028                 pMmsHeaderData->messageID[0]= '\0';
1029                 pMmsHeaderData->messageType = -1;
1030                 pMmsHeaderData->mmsVersion = -1;
1031                 pMmsHeaderData->messageSize = 0;
1032                 pMmsHeaderData->mmsPriority = -1;
1033                 pMmsHeaderData->bReadReport = 0;
1034                 pMmsHeaderData->bHideAddress = false;
1035                 pMmsHeaderData->mmsStatus = MSG_DELIVERY_REPORT_NONE;
1036                 pMmsHeaderData->szSubject[0] = '\0';
1037                 pMmsHeaderData->to = NULL;
1038                 pMmsHeaderData->trID[0] = '\0';
1039                 pMmsHeaderData->contentClass = -1;
1040         }
1041 }
1042
1043 MMS_HEADER_DATA_S *MsgMmsCreateHeader()
1044 {
1045         MMS_HEADER_DATA_S *pMmsHeaderData = (MMS_HEADER_DATA_S *)calloc(1, sizeof(MMS_HEADER_DATA_S));
1046         if (pMmsHeaderData) {
1047                 pMmsHeaderData->bcc = NULL; /* Bcc */
1048                 pMmsHeaderData->cc = NULL;  /* Cc */
1049                 pMmsHeaderData->contentLocation[0] = '\0';
1050                 pMmsHeaderData->szContentType[0] = '\0';
1051                 pMmsHeaderData->date = 0;
1052                 pMmsHeaderData->bDeliveryReport = false; /* X-Mms-Delivery-Report */
1053                 pMmsHeaderData->delivery.type = MMS_TIMETYPE_NONE;
1054                 pMmsHeaderData->delivery.time = 0;
1055                 pMmsHeaderData->expiry.type = MMS_TIMETYPE_NONE;
1056                 pMmsHeaderData->expiry.time = 0;
1057                 pMmsHeaderData->szFrom[0] = '\0'; /* From */
1058                 pMmsHeaderData->messageClass = -1; /* Personal | Advertisement | Informational | Auto */
1059                 pMmsHeaderData->messageID[0]= '\0';
1060                 pMmsHeaderData->messageType = -1; /* MmsMsgType : ex) sendreq */
1061                 pMmsHeaderData->mmsVersion = -1; /* 1.0 1.3 */
1062                 pMmsHeaderData->messageSize = 0; /* X-Mms-Message-Size */
1063                 pMmsHeaderData->mmsPriority = -1; /* MSG_PRIORITY_TYPE_E : Low | Normal | High */
1064                 pMmsHeaderData->bReadReport = 0; /* X-Mms-Read-Report */
1065                 pMmsHeaderData->bHideAddress = false; /* X-Mms-Sender-Visibility */
1066                 pMmsHeaderData->mmsStatus = MSG_DELIVERY_REPORT_NONE; /* X-Mms-Status */
1067                 pMmsHeaderData->szSubject[0] = '\0'; /* Subject */
1068                 pMmsHeaderData->to = NULL;
1069                 pMmsHeaderData->trID[0] = '\0';
1070                 pMmsHeaderData->contentClass = -1;/* text | image-basic| image-rich | video-basic | video-rich | megapixel | content-basic | content-rich */
1071 #if 0
1072                 X-Mms-Report-Allowed
1073                 X-Mms-Response-Status
1074                 X-Mms-Response-Text
1075                 X-Mms-Retrieve-Status
1076                 X-Mms-Retrieve-Text
1077                 X-Mms-Read-Status
1078                 X-Mms-Reply-Charging
1079                 X-Mms-Reply-Charging-Deadline
1080                 X-Mms-Reply-Charging-ID
1081                 X-Mms-Reply-Charging-Size
1082                 X-Mms-Previously-Sent-By
1083                 X-Mms-Previously-Sent-Date
1084                 X-Mms-Store
1085                 X-Mms-MM-State
1086                 X-Mms-MM-Flags
1087                 X-Mms-Store-Status
1088                 X-Mms-Store-Status-Text
1089                 X-Mms-Stored
1090                 X-Mms-Attributes
1091                 X-Mms-Totals
1092                 X-Mms-Mbox-Totals
1093                 X-Mms-Quotas
1094                 X-Mms-Mbox-Quotas
1095                 X-Mms-Message-Count
1096                 Content
1097                 X-Mms-Start
1098                 Additional-headers
1099                 X-Mms-Distribution-Indicator
1100                 X-Mms-Element-Descriptor
1101                 X-Mms-Limit
1102                 X-Mms-Recommended-Retrieval-Mode
1103                 X-Mms-Recommended-Retrieval-Mode-Text
1104                 X-Mms-Status-Text
1105                 X-Mms-Applic-ID
1106                 X-Mms-Reply-Applic-ID
1107                 X-Mms-Aux-Applic-Info
1108                 X-Mms-DRM-Content
1109                 X-Mms-Adaptation-Allowed
1110                 X-Mms-Replace-ID
1111                 X-Mms-Cancel-ID
1112                 X-Mms-Cancel-Status
1113 #endif
1114         }
1115
1116         return pMmsHeaderData;
1117 }
1118
1119 void MsgMmsReleaseHeader(MMS_HEADER_DATA_S **ppMmHeadersData)
1120 {
1121         if (ppMmHeadersData && *ppMmHeadersData) {
1122                 MMS_HEADER_DATA_S *pMmsHeaderData = *ppMmHeadersData;
1123                 MsgMmsReleaseAddressList(&pMmsHeaderData->to);
1124                 MsgMmsReleaseAddressList(&pMmsHeaderData->cc);
1125                 MsgMmsReleaseAddressList(&pMmsHeaderData->bcc);
1126                 free(pMmsHeaderData);
1127
1128                 *ppMmHeadersData = NULL;
1129         }
1130 }
1131
1132 MMS_DATA_S *MsgMmsCreate()
1133 {
1134         MMS_DATA_S * mms_data = (MMS_DATA_S * )calloc(1, sizeof(MMS_DATA_S));
1135         if (mms_data) {
1136                 mms_data->header = NULL;
1137                 mms_data->multipartlist = NULL;
1138                 mms_data->smil = NULL;
1139         }
1140         return mms_data;
1141 }
1142
1143 void MsgMmsRelease(MMS_DATA_S **ppMmsData)
1144 {
1145         if (ppMmsData && *ppMmsData) {
1146                 MMS_DATA_S *pMmsData = *ppMmsData;
1147
1148                 if (pMmsData->header)
1149                         MsgMmsReleaseHeader(&pMmsData->header);
1150
1151                 if (pMmsData->smil)
1152                         MsgMmsReleaseMultipart(&pMmsData->smil);
1153
1154                 MsgMmsReleaseMultipartList(&pMmsData->multipartlist);
1155
1156                 free(pMmsData);
1157
1158                 *ppMmsData = NULL;
1159         }
1160 }
1161
1162 static void __freeMultipartListItem(gpointer data)
1163 {
1164         MMS_MULTIPART_DATA_S * pMultipart = (MMS_MULTIPART_DATA_S *)data;
1165
1166         if (pMultipart) {
1167                 MsgMmsReleaseMultipart(&pMultipart);
1168         }
1169 }
1170
1171 int MsgMmsReleaseMultipartList(MMSList **ppMultipartList)
1172 {
1173         if (ppMultipartList && *ppMultipartList) {
1174                 g_list_free_full(*ppMultipartList, __freeMultipartListItem);
1175                 *ppMultipartList = NULL;
1176         }
1177
1178         return 0;
1179 }
1180
1181 static void __freeAddressListItem(gpointer data)
1182 {
1183         MMS_ADDRESS_DATA_S * pAddressData = (MMS_ADDRESS_DATA_S *)data;
1184
1185         MsgMmsReleaseAddress(&pAddressData);
1186 }
1187
1188 int MsgMmsReleaseAddressList(MMSList **ppAddressList)
1189 {
1190         if (ppAddressList && *ppAddressList) {
1191                 g_list_free_full(*ppAddressList, __freeAddressListItem);
1192                 *ppAddressList = NULL;
1193         }
1194
1195         return 0;
1196 }
1197
1198 static void removeLessGreaterMark(const char *szSrcID, char *szDest, int destSize)
1199 {
1200         char szBuf[MSG_MSG_ID_LEN + 1] = {0, };
1201         int cLen = strlen(szSrcID);
1202
1203         if (cLen > 1 && szSrcID[0] == '<' && szSrcID[cLen - 1] == '>') {
1204                 strncpy(szBuf, &szSrcID[1], cLen - 2);
1205                 szBuf[cLen - 2] = '\0';
1206         } else if (cLen > 1 && szSrcID[0] == '"' && szSrcID[cLen-1] == '"') {
1207                 strncpy(szBuf, &szSrcID[1], cLen - 2);
1208                 szBuf[cLen - 2] = '\0';
1209         } else {
1210                 strncpy(szBuf, szSrcID, cLen);
1211                 szBuf[cLen] = '\0';
1212         }
1213
1214         snprintf(szDest, destSize, "%s", szBuf);
1215 }
1216
1217 static bool IsMatchedMedia(MMS_MEDIA_S *media, MMS_MULTIPART_DATA_S *pMultipart)
1218 {
1219         if (strlen(pMultipart->szContentID) > 0) {
1220                 char szTempContentID[MSG_MSG_ID_LEN + 1] = {0, };
1221                 removeLessGreaterMark(pMultipart->szContentID, szTempContentID, sizeof(szTempContentID));
1222
1223                 if (strcmp(media->szContentID, szTempContentID) == 0) {
1224                         if (strlen(media->szContentLocation) > 0 && strlen(pMultipart->szContentLocation) > 0) {
1225                                 if (strcmp(media->szContentLocation,  pMultipart->szContentLocation) == 0) {
1226                                         return true;
1227                                 } else {
1228                                         /* go through */
1229                                 }
1230                         } else {
1231                                 return true;
1232                         }
1233                 }
1234
1235                 if (strcmp(media->szContentLocation,  szTempContentID) == 0) {
1236                         return true;
1237                 }
1238
1239                 if (media->szContentID[0] != '\0') {
1240                         char *pszExt = strrchr(media->szContentID, '.');
1241                         if (pszExt) {
1242                                 char tmpContentID[MSG_MSG_ID_LEN+1] = {0};
1243                                 strncpy(tmpContentID, media->szContentID, strlen(media->szContentID));
1244                                 int extLength = strlen(pszExt);
1245                                 int contentIDLength = strlen(media->szContentID);
1246                                 tmpContentID[contentIDLength-extLength] = '\0';
1247
1248                                 if (g_strcmp0(tmpContentID, szTempContentID) == 0)
1249                                         return true;
1250                         }
1251                 }
1252         }
1253
1254         if (strlen(pMultipart->szContentLocation) > 0) {
1255                 if (strcmp(media->szContentID,  pMultipart->szContentLocation) == 0) {
1256                         return true;
1257                 }
1258
1259                 if (strcmp(media->szContentLocation,  pMultipart->szContentLocation) == 0) {
1260                         return true;
1261                 }
1262         }
1263
1264         return false;
1265 }
1266
1267 static bool MmsFindAndInsertPart(MMS_MESSAGE_DATA_S *pMsgData, MMS_MULTIPART_DATA_S *pMultipart)
1268 {
1269         bool insert_media = false;
1270
1271         if (pMsgData->pagelist) {
1272                 for (int pageIdx = 0; pageIdx < pMsgData->pageCnt; pageIdx++) {
1273                         MMS_PAGE_S *page = (MMS_PAGE_S *)g_list_nth_data(pMsgData->pagelist, pageIdx);
1274
1275                         if (page && page->medialist) {
1276                                 for (int mediaIdx = 0; mediaIdx < page->mediaCnt; mediaIdx++) {
1277                                         MMS_MEDIA_S *media = (MMS_MEDIA_S *)g_list_nth_data(page->medialist, mediaIdx);
1278
1279                                         if (media) { /* add media */
1280                                                 if (IsMatchedMedia(media, pMultipart) == true) { /* matched media */
1281                                                         insert_media = true;
1282                                                         media->drmType = pMultipart->drmType;
1283                                                         snprintf(media->szFilePath, sizeof(media->szFilePath), "%s", pMultipart->szFilePath);
1284                                                         snprintf(media->szFileName, sizeof(media->szFileName), "%s", pMultipart->szFileName);
1285                                                         snprintf(media->szContentID, sizeof(media->szContentID), "%s", pMultipart->szContentID);
1286                                                         snprintf(media->szContentLocation, sizeof(media->szContentLocation), "%s", pMultipart->szContentLocation);
1287                                                         snprintf(media->szContentType, sizeof(media->szContentType), "%s", pMultipart->szContentType);
1288                                                         MSG_SEC_DEBUG("InsertPart to pageIndx [%d] mediaIdx[%d] media[%p] : path = [%s], name = [%s], cid = [%s], cl = [%s], ct = [%s]"\
1289                                                                         , pageIdx, mediaIdx, media, media->szFilePath, media->szFileName, media->szContentID, media->szContentLocation, media->szContentType);
1290                                                 }
1291                                         }
1292                                 } /* end for media list */
1293                         }
1294                 } /* end for page list */
1295         }
1296
1297         return insert_media;
1298 }
1299
1300 static bool MmsInsertPartToMmsData(MMS_MESSAGE_DATA_S *pMsgData, MMS_MULTIPART_DATA_S *pMultipart)
1301 {
1302         MSG_BEGIN();
1303
1304         bool isInsert = false;
1305
1306         if (pMsgData == NULL || pMultipart == NULL) {
1307                 return false;
1308         }
1309
1310         isInsert = MmsFindAndInsertPart(pMsgData, pMultipart);
1311
1312         if (isInsert == false) {
1313                 MMS_ATTACH_S *attachment = NULL;
1314                 attachment = (MMS_ATTACH_S *)calloc(sizeof(MMS_ATTACH_S), 1);
1315                 if (attachment) {
1316                         attachment->drmType = pMultipart->drmType;
1317                         attachment->mediatype = pMultipart->type;
1318                         snprintf(attachment->szContentType, sizeof(attachment->szContentType), "%s", pMultipart->szContentType);
1319                         snprintf(attachment->szFilePath, sizeof(attachment->szFilePath), "%s", pMultipart->szFilePath);
1320                         snprintf(attachment->szFileName, sizeof(attachment->szFileName), "%s", pMultipart->szFileName);
1321                         attachment->fileSize = MsgGetFileSize(attachment->szFilePath);
1322                         MSG_SEC_DEBUG("Insert Attach to attachment[%p] : path = [%s], name = [%s], ct = [%s], size = [%d]"\
1323                                                         , attachment, attachment->szFilePath, attachment->szFileName, attachment->szContentType, attachment->fileSize);
1324                 }
1325
1326                 if (_MsgMmsAddAttachment(pMsgData, attachment) != MSG_SUCCESS) {
1327                         g_free(attachment);
1328                         return false;
1329                 }
1330         }
1331
1332         MSG_END();
1333         return true;
1334 }
1335
1336 int MsgMmsConvertMmsDataToMmsMessageData(MMS_DATA_S *pSrc, MMS_MESSAGE_DATA_S *pDst)
1337 {
1338         MSG_BEGIN();
1339
1340         bzero(pDst, sizeof(MMS_MESSAGE_DATA_S));
1341
1342         if (pSrc->smil) {
1343                 if (MsgAccessFile(pSrc->smil->szFilePath, F_OK)) {
1344                         gchar *contents = NULL;
1345                         gsize length = 0;
1346
1347                         if(g_file_get_contents((gchar*)pSrc->smil->szFilePath, (gchar**)&contents, (gsize*)&length, NULL) == FALSE)
1348                                 MSG_ERR("g_file_get_contents is failed");
1349
1350                         if (contents) {
1351                                 MsgSmilParseSmilDoc(pDst, contents);
1352                                 g_free(contents);
1353                         }
1354                 } else {
1355                         if (pSrc->smil->pMultipartData) {
1356                                 char *smil_data = (char *)calloc(1, sizeof(char)*(pSrc->smil->nMultipartDataLen + 1));
1357                                 if (smil_data) {
1358                                         memcpy(smil_data, pSrc->smil->pMultipartData, pSrc->smil->nMultipartDataLen);
1359                                         MsgSmilParseSmilDoc(pDst, smil_data);
1360                                         g_free(smil_data);
1361                                 }
1362                         }
1363                 }
1364         }
1365
1366         int len = g_list_length(pSrc->multipartlist);
1367         for (int i = 0; i < len; i++) {
1368                 MMS_MULTIPART_DATA_S *multipart = (MMS_MULTIPART_DATA_S *)g_list_nth_data(pSrc->multipartlist, i);
1369                 if (multipart) {
1370                         MmsInsertPartToMmsData(pDst, multipart);
1371                 }
1372         }
1373
1374         MSG_END();
1375         return 0;
1376 }
1377
1378 int MsgMmsConvertMmsMessageDataToMmsData(MMS_MESSAGE_DATA_S *pSrc, MMS_DATA_S *pDst)
1379 {
1380         MSG_BEGIN();
1381
1382         char *pRawData = NULL;
1383
1384         pDst->backup_type = pSrc->backup_type;
1385
1386         int pageCnt = _MsgMmsGetPageCount(pSrc);
1387         if (pSrc->smil.szFilePath[0] != '\0') {
1388                 MMS_MULTIPART_DATA_S *pMultipart = MsgMmsCreateMultipart();
1389                 if (pMultipart)
1390                         memcpy(pMultipart, &pSrc->smil, sizeof(MMS_MULTIPART_DATA_S));
1391                 pDst->smil = pMultipart;
1392         } else if (pageCnt > 0) {       /* Multipart related */
1393                 MsgSmilGenerateSmilDoc(pSrc, &pRawData);
1394                 if (pRawData) {
1395                         MMS_MULTIPART_DATA_S *pMultipart = MsgMmsCreateMultipart();
1396
1397                         MSG_DEBUG("%s", pRawData);
1398                         if (pMultipart) {
1399                                 pMultipart->pMultipartData = pRawData;
1400                                 pMultipart->nMultipartDataLen = strlen(pRawData);
1401                                 pMultipart->type = MIME_APPLICATION_SMIL;
1402                                 snprintf(pMultipart->szContentType, sizeof(pMultipart->szContentType), "%s", "application/smil");
1403                                 pDst->smil = pMultipart;
1404                         }
1405                 } else {
1406                         MSG_DEBUG("Fail to Generate SmilDoc");
1407                 }
1408         }
1409
1410         for (int i = 0; i < pageCnt; ++i) {
1411                 MMS_PAGE_S *pPage = _MsgMmsGetPage(pSrc, i);
1412                 if (pPage) {
1413                         int mediaCnt = pPage->mediaCnt;
1414                         MSG_DEBUG("PAGE %d's media Cnt: %d", i+1, mediaCnt);
1415
1416                         for (int j = 0; j < mediaCnt; ++j) {
1417                                 MMS_MEDIA_S *pMedia = _MsgMmsGetMedia(pPage, j);
1418                                 if ((pMedia) && (pMedia->szFilePath[0] != 0)) {
1419                                         MMS_MULTIPART_DATA_S *pMultipart = MsgMmsCreateMultipart();
1420                                         if (pMultipart) {
1421                                                 snprintf(pMultipart->szContentID, sizeof(pMultipart->szContentID), "%s", pMedia->szContentID);
1422                                                 snprintf(pMultipart->szContentLocation, sizeof(pMultipart->szContentLocation), "%s", pMedia->szContentLocation);
1423                                                 snprintf(pMultipart->szFileName, sizeof(pMultipart->szFileName), "%s", pMedia->szFileName);
1424                                                 snprintf(pMultipart->szFilePath, sizeof(pMultipart->szFilePath), "%s", pMedia->szFilePath);
1425                                                 snprintf(pMultipart->szContentType, sizeof(pMultipart->szContentType), "%s", pMedia->szContentType);
1426                                                 pDst->multipartlist = g_list_append(pDst->multipartlist, pMultipart);
1427                                         }
1428                                 }
1429                         }
1430                 }
1431         }
1432
1433         /* Processing Attachment List */
1434         int attachCnt = _MsgMmsGetAttachCount(pSrc);
1435
1436         for (int i = 0; i < attachCnt; ++i) {
1437                 MMS_ATTACH_S *pMedia = _MsgMmsGetAttachment(pSrc, i);
1438                 if (pMedia->szFilePath[0] != 0) {
1439                         MMS_MULTIPART_DATA_S *pMultipart = MsgMmsCreateMultipart();
1440                         if (pMultipart) {
1441                                 snprintf(pMultipart->szContentID, sizeof(pMultipart->szContentID), "%s", pMedia->szFileName);
1442                                 snprintf(pMultipart->szContentLocation, sizeof(pMultipart->szContentLocation), "%s", pMedia->szFileName);
1443                                 snprintf(pMultipart->szFileName, sizeof(pMultipart->szFileName), "%s", pMedia->szFileName);
1444                                 snprintf(pMultipart->szFilePath, sizeof(pMultipart->szFilePath), "%s", pMedia->szFilePath);
1445                                 snprintf(pMultipart->szContentType, sizeof(pMultipart->szContentType), "%s", pMedia->szContentType);
1446
1447                                 pDst->multipartlist = g_list_append(pDst->multipartlist, pMultipart);
1448                         }
1449                 }
1450         }
1451
1452         MSG_END();
1453
1454         return 0;
1455 }
1456
1457 int MsgMmsGetSmilMultipart(MMSList *pMultipartList, MMS_MULTIPART_DATA_S **smil_multipart)
1458 {
1459         const char *smil_content_type = "application/smil";
1460
1461         int len = g_list_length(pMultipartList);
1462
1463         for (int i = 0; i < len; i++) {
1464                 MMS_MULTIPART_DATA_S *multipart = (MMS_MULTIPART_DATA_S *)g_list_nth_data(pMultipartList, i);
1465
1466                 if (multipart) {
1467                         if (strcasecmp(multipart->szContentType, smil_content_type) == 0) {
1468                                 *smil_multipart = multipart;
1469                                 break;
1470                         }
1471                 }
1472         }
1473
1474         return 0;
1475 }
1476
1477 /* get content from filepath and save to pMultipartData */
1478 int MsgMmsSetMultipartData(MMS_MULTIPART_DATA_S *pMultipart)
1479 {
1480         if (pMultipart->pMultipartData != NULL)
1481                 return 0;
1482
1483         if (g_file_get_contents((gchar*)pMultipart->szFilePath, (gchar**)&pMultipart->pMultipartData, (gsize*)&pMultipart->nMultipartDataLen, NULL) == false)
1484                 return -1;
1485
1486         /* Due to Get data for Backup message */
1487         /* memset(pMultipart->szFilePath, 0x00, sizeof(pMultipart->szFilePath)); */
1488         return 0;
1489 }
1490
1491 int MsgMmsSetMultipartListData(MMS_DATA_S *pMmsData)
1492 {
1493         MSG_BEGIN();
1494
1495         MMSList *multipart_list = pMmsData->multipartlist;
1496
1497         if (multipart_list) {
1498                 if (pMmsData->smil) {
1499                         MsgMmsSetMultipartData(pMmsData->smil);
1500                 }
1501
1502                 for (int i = 0; i < (int)g_list_length(multipart_list); i++) {
1503                         MMS_MULTIPART_DATA_S *pMultipart = (MMS_MULTIPART_DATA_S *)g_list_nth_data(multipart_list, i);
1504
1505                         if (pMultipart) {
1506                                 MsgMmsSetMultipartData(pMultipart);/* app file -> data */
1507                         }
1508                 }
1509         }
1510
1511         MSG_END();
1512         return 0;
1513 }
1514
1515 /* pMultipartData set to file path */
1516 int MsgMmsSetMultipartFilePath(const char *dirPath, MMS_MULTIPART_DATA_S *pMultipart)
1517 {
1518         if (g_file_test(dirPath, G_FILE_TEST_IS_DIR) != true) {
1519                 MSG_SEC_DEBUG("g_file_test is false: [%s] is not dir or not exist", dirPath);
1520                 return -1;
1521         }
1522
1523         memset(pMultipart->szFilePath, 0x00, sizeof(pMultipart->szFilePath));
1524
1525         int wrn = snprintf(pMultipart->szFilePath, sizeof(pMultipart->szFilePath), "%s/%s", dirPath, pMultipart->szFileName);
1526         if(wrn<0)
1527                 MSG_DEBUG("snprintf was failed");
1528
1529         /* remove space character of original file name */
1530         msg_replace_space_char(pMultipart->szFilePath);
1531
1532         if (!MsgCreateFile(pMultipart->szFilePath, pMultipart->pMultipartData, pMultipart->nMultipartDataLen)) {
1533                 MSG_SEC_DEBUG("Fail to set content to file [%s]", pMultipart->szFilePath);
1534                 return -1;
1535         }
1536
1537         return 0;
1538 }
1539
1540 int MsgMmsSetMultipartListFilePath(const char *dirPath, MMS_DATA_S *pMmsData)
1541 {
1542         MSG_BEGIN();
1543
1544         char working_dir[MSG_FILENAME_LEN_MAX+1] = {0, };
1545
1546         snprintf(working_dir, sizeof(working_dir), "%s", dirPath);
1547
1548         if (mkdir(working_dir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0) {
1549                 if (errno == EEXIST) {
1550                         MSG_SEC_DEBUG("exist dir : [%s]", working_dir);
1551                 } else {
1552                         MSG_SEC_DEBUG("Fail to Create Dir [%s]", working_dir);
1553                         return -1;
1554                 }
1555         }
1556
1557         MMSList *multipart_list = pMmsData->multipartlist;
1558
1559         if (pMmsData->smil) {
1560                 snprintf(pMmsData->smil->szFileName, sizeof(pMmsData->smil->szFileName), "%s", "smil.smil");
1561                 MsgMmsSetMultipartFilePath(working_dir, pMmsData->smil);
1562         }
1563
1564         if (multipart_list) {
1565                 for (int i = 0; i < (int)g_list_length(multipart_list); i++) {
1566                         MMS_MULTIPART_DATA_S *pMultipart = (MMS_MULTIPART_DATA_S *)g_list_nth_data(multipart_list, i);
1567
1568                         if (pMultipart) {
1569                                 MsgMmsSetMultipartFilePath(working_dir, pMultipart); /* data -> svc file */
1570                         }
1571                 }
1572         }
1573
1574         MSG_END();
1575         return 0;
1576 }
1577
1578 int printMultipart(MMS_MULTIPART_DATA_S *multipart)
1579 {
1580         if (multipart) {
1581                 MSG_DEBUG("multipart ptr [%p]", multipart);
1582                 MSG_DEBUG("type : %d", multipart->type);
1583                 MSG_DEBUG("type str : %s", multipart->szContentType);
1584                 MSG_DEBUG("cid : %s", multipart->szContentID);
1585                 MSG_DEBUG("cl : %s", multipart->szContentLocation);
1586                 MSG_DEBUG("name : %s", multipart->szFileName);
1587                 MSG_DEBUG("filepath : %s", multipart->szFilePath);
1588                 MSG_DEBUG("tcs_bc_level : %d", multipart->tcs_bc_level);
1589                 MSG_DEBUG("malware_allow : %d", multipart->malware_allow);
1590                 MSG_DEBUG("thumbfilepath : %s", multipart->szThumbFilePath);
1591         }
1592         return 0;
1593 }
1594
1595 int printMultipartList(MMSList *pMultipartList)
1596 {
1597         int len = g_list_length(pMultipartList);
1598
1599         for (int i = 0; i < len; i++) {
1600                 MMS_MULTIPART_DATA_S *multipart = (MMS_MULTIPART_DATA_S *)g_list_nth_data(pMultipartList, i);
1601
1602                 if (multipart) {
1603                         MSG_DEBUG("[%d] multipart ptr [%p]", i,  multipart);
1604                         MSG_DEBUG("type : %d", multipart->type);
1605                         MSG_DEBUG("type str : %s", multipart->szContentType);
1606                         MSG_DEBUG("cid : %s", multipart->szContentID);
1607                         MSG_DEBUG("cl : %s", multipart->szContentLocation);
1608                         MSG_DEBUG("name : %s", multipart->szFileName);
1609                         MSG_DEBUG("filepath : %s", multipart->szFilePath);
1610                         MSG_DEBUG("tcs_bc_level : %d", multipart->tcs_bc_level);
1611                         MSG_DEBUG("malware_allow : %d", multipart->malware_allow);
1612                         MSG_DEBUG("thumbfilepath : %s", multipart->szThumbFilePath);
1613                 }
1614         }
1615
1616         return 0;
1617 }
1618
1619 void _MsgMmsMultipartPrint(MMS_MULTIPART_DATA_S *multipart)
1620 {
1621         if (multipart == NULL) {
1622                 MSG_DEBUG("Invalid Parameter");
1623                 return;
1624         }
1625
1626         MSG_DEBUG("%-25s : %d", "Multipart type", multipart->type);
1627         MSG_SEC_DEBUG("%-25s : %s", "Multipart filename", multipart->szFileName);
1628         MSG_SEC_DEBUG("%-25s : %s", "Multipart filepath", multipart->szFilePath);
1629         MSG_SEC_DEBUG("%-25s : %s", "Multipart content type", multipart->szContentType);
1630         MSG_SEC_DEBUG("%-25s : %s", "Multipart content id", multipart->szContentID);
1631         MSG_SEC_DEBUG("%-25s : %s", "Multipart content location", multipart->szContentLocation);
1632         MSG_DEBUG("%-25s : %d", "Multipart drm type", multipart->drmType);
1633 }
1634
1635 bool  _MsgMmsRemoveEmptyMedia(MMS_PAGE_S *pPage)
1636 {
1637         MMS_MEDIA_S *pMedia = NULL;
1638
1639         int mediaCnt  = g_list_length(pPage->medialist);
1640
1641         for (int i = 0; i < mediaCnt; i++) {
1642                 GList *nth = g_list_nth(pPage->medialist, i);
1643
1644                 if (nth == NULL)
1645                         return false;
1646
1647                 pMedia = (MMS_MEDIA_S *)nth->data;
1648
1649                 if (pMedia == NULL)
1650                         continue;
1651
1652                 if (strlen(pMedia->szFilePath) == 0) {
1653                         MSG_DEBUG("Found Empty Media [%d]", i);
1654
1655                         g_free(pMedia);
1656
1657                         nth->data = NULL;
1658                 }
1659         }
1660
1661         pPage->medialist = g_list_remove_all(pPage->medialist, NULL);
1662
1663         pPage->mediaCnt = g_list_length(pPage->medialist);
1664
1665         return true;
1666 }
1667
1668 /* remove media object with no filepath */
1669 bool _MsgMmsRemoveEmptyObject(MMS_MESSAGE_DATA_S *pMmsMsg)
1670 {
1671         MMS_PAGE_S *pPage;
1672
1673         int pageCnt = g_list_length(pMmsMsg->pagelist);
1674
1675         for (int i = 0; i < pageCnt; i++) {
1676                 GList *nth = g_list_nth(pMmsMsg->pagelist, i);
1677
1678                 if (nth == NULL)
1679                         return false;
1680
1681                 pPage = (MMS_PAGE_S *)nth->data;
1682
1683                 if (pPage == NULL)
1684                         continue;
1685
1686                 _MsgMmsRemoveEmptyMedia(pPage);
1687
1688 #if 0
1689                 if (g_list_length(pPage->medialist) == 0) {
1690                         MSG_DEBUG("Found Empty Page [%d]", i);
1691
1692                         g_free(pPage);
1693
1694                         nth->data = NULL;
1695                 }
1696 #endif
1697         }
1698
1699         pMmsMsg->pagelist = g_list_remove_all(pMmsMsg->pagelist, NULL);
1700
1701         pMmsMsg->pageCnt = g_list_length(pMmsMsg->pagelist);
1702
1703         return true;
1704 }
1705
1706 int MsgMmsCheckFilepathSmack(int fd, const char* ipc_filename)
1707 {
1708         int err = MSG_SUCCESS;
1709
1710         char *app_smack_label = NULL;
1711         smack_new_label_from_socket(fd, &app_smack_label);
1712         if (app_smack_label == NULL) {
1713                 return MSG_ERR_PERMISSION_DENIED;
1714         }
1715
1716         MSG_SEC_DEBUG("app_smack_label [%s]", app_smack_label);
1717
1718         char ipc_filepath[MSG_FILEPATH_LEN_MAX+1] = {0, };
1719         snprintf(ipc_filepath, MSG_FILEPATH_LEN_MAX, "%s%s", MSG_IPC_DATA_PATH, ipc_filename);
1720
1721         gchar *serialized_data = NULL;
1722         gsize serialized_len = 0;
1723         MSG_SEC_DEBUG("ipc_path [%s]", ipc_filepath);
1724
1725         if (!g_file_get_contents((gchar*)ipc_filepath, (gchar**)&serialized_data, (gsize*)&serialized_len, NULL)) {
1726                 MSG_FREE(app_smack_label);
1727                 return MSG_ERR_PERMISSION_DENIED;
1728         }
1729
1730         MMS_DATA_S *mms_data = NULL;
1731
1732         MsgDeserializeMmsData(serialized_data, serialized_len, &mms_data);
1733
1734         if (mms_data) {
1735                 if (mms_data->multipartlist) {
1736                         int len = g_list_length(mms_data->multipartlist);
1737                         for (int i = 0; i < len; i++) {
1738                                 MMS_MULTIPART_DATA_S *multipart = (MMS_MULTIPART_DATA_S *)g_list_nth_data(mms_data->multipartlist, i);
1739                                 if (multipart) {
1740                                         err = MsgCheckFilepathSmack(app_smack_label, multipart->szFilePath);
1741                                         if (err != MSG_SUCCESS)
1742                                                 break;
1743                                 }
1744                         }
1745                 }
1746                 if (err == MSG_SUCCESS && mms_data->smil) {
1747                         err = MsgCheckFilepathSmack(app_smack_label, mms_data->smil->szFilePath);
1748                 }
1749                 MsgMmsRelease(&mms_data);
1750         } else {
1751                 err = MSG_ERR_INVALID_PARAMETER;
1752         }
1753
1754         MSG_FREE(serialized_data);
1755         MSG_FREE(app_smack_label);
1756         return err;
1757 }