RSA sync with private
[platform/core/messaging/msg-service.git] / plugin / mms_plugin / MmsPluginDecode.cpp
1 /*
2 * Copyright 2012  Samsung Electronics Co., Ltd
3 *
4 * Licensed under the Flora License, Version 1.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.tizenopensource.org/license
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 <sys/stat.h>
20 #include <stdlib.h>
21 #include <errno.h>
22
23 #include "MsgDebug.h"
24 #include "MsgException.h"
25 #include "MmsPluginMessage.h"
26 #include "MmsPluginStorage.h"
27 #include "MsgUtilFile.h"
28 #include "MmsPluginCodec.h"
29 #include "MmsPluginDebug.h"
30 #include "MsgSettingTypes.h"
31 #include "MsgSettingHandler.h"
32 #include "MmsPluginInternal.h"
33 #include "MmsPluginAvCodec.h"
34 #include "MmsPluginStorage.h"
35 #include "MmsPluginSmil.h"
36 #ifdef __SUPPORT_DRM__
37 #include "MmsPluginDrm.h"
38 #include "MsgDrmWrapper.h"
39 #endif
40 #include "MmsPluginTextConvert.h"
41 #include "MmsPluginUtil.h"
42
43 static bool _MmsBinaryDecodeGetBytes(FILE *pFile, char *szBuff, int bufLen, int totalLength);           /* bufLen < gMmsDecodeMaxLen */
44 static bool _MmsBinaryDecodeGetLongBytes(FILE *pFile, char *szBuff, int bufLen, int totalLength);       /* no bufLen limit */
45 static bool _MmsBinaryDecodeGetOneByte(FILE *pFile, UINT8 *pOneByte, int totalLength);
46
47 static int __MmsBinaryDecodeUintvar(FILE *pFile, UINT32 *pUintVar, int totalLength);
48 static int __MmsDecodeValueLength(FILE *pFile, UINT32 *pValueLength, int totalLength);
49 static int __MmsDecodeValueLength2(FILE *pFile, UINT32 *pValueLength, int totalLength);
50 static int __MmsBinaryDecodeQuotedString(FILE *pFile, char *szBuff, int bufLen, int totalLength);
51 static bool __MmsBinaryDecodeEncodedString(FILE *pFile, char *szBuff, int bufLen, int totalLength);
52 static bool __MmsBinaryDecodeInteger(FILE *pFile, UINT32 *pInteger, int *pIntLen, int totalLength);
53 static bool __MmsBinaryDecodeCharset(FILE *pFile, UINT32 *nCharSet, int *pCharSetLen, int totalLength);
54 static char *__MmsBinaryDecodeText2(FILE *pFile, int totalLength, int *pLength);
55 static int __MmsBinaryDecodeText(FILE *pFile, char *szBuff, int bufLen, int totalLength);
56 static UINT32 __MmsHeaderDecodeIntegerByLength(FILE *pFile, UINT32 length, int totalLength);
57 static bool __MmsDecodeLongInteger(FILE *pFile, UINT32 *pLongInteger, int totalLength);
58 static int __MmsDecodeGetFilename(FILE *pFile, char *szBuff, int bufLen, int totalLength);
59 static MsgHeaderAddress *__MmsDecodeEncodedAddress(FILE *pFile, int totalLength);
60
61 #ifdef __SUPPORT_DRM__
62 bool __MmsParseDCFInfo(FILE *pFile, MsgDRMInfo *pDrmInfo, int totalLength);
63 bool __MmsParseDCFHdr(FILE *pFile, MsgDRMInfo *pDrmInfo, UINT32 headerLen, int totalLength);
64 bool MmsBinaryDecodeDRMMessage(FILE *pFile, char *szFilePath, MsgType *pMsgType, MsgBody *pMsgBody, unsigned int fullBodyLength, int totalLength);
65 bool MmsBinaryDecodeDRMContent(FILE *pFile, char *szFilePath, MsgType *pMsgType, MsgBody *pMsgBody, unsigned int bodyLength, int totalLength);
66 #endif
67
68 static bool MmsBinaryDecodeMovePointer(FILE *pFile, int offset, int totalLength);
69 bool MmsBinaryIsTextEncodedPart(FILE *pFile, int totalLength);
70 static bool     __MmsBinaryDecodeCheckAndDecreaseLength(int *pLength, int valueLength);
71
72 bool __MmsTextDecodeMsgHeader(FILE *pFile);
73 bool MmsTextDecodeMsgBody(FILE *pFile);
74
75 char gszMmsLoadBuf1[MSG_MMS_DECODE_BUFFER_MAX + 1] = {0, };
76 char gszMmsLoadBuf2[MSG_MMS_DECODE_BUFFER_MAX + 1] = {0, };
77
78 static char *gpCurMmsDecodeBuff = NULL;
79 static int gCurMmsDecodeBuffPos = 0;    /* next decoding position in gpCurMmsDecodeBuff  */
80 static int gMmsDecodeMaxLen = 0;
81 static int gMmsDecodeCurOffset = 0;     /* current offset in file (last read) */
82 static int gMmsDecodeBufLen = 0;                /* number of last read characters */
83
84 static char *gpMmsDecodeBuf1 = NULL;
85 static char *gpMmsDecodeBuf2 = NULL;
86
87 MmsHeader       mmsHeader =
88 {
89         false,                                                                  //bActive
90         NULL,                                                                   //pszOwner
91         -1,                                                                             //msgID
92
93         (MmsMsgType)MMS_MSGTYPE_ERROR,                  //MmsMsgType                    iType;
94         "",                                                                             //char[]                                szTrID;
95         //"",                                                                           //short int                             version;
96         0,                                                                              //short int                             version;
97         0,                                                                              //UINT32                                date;
98
99         NULL,                                                                   //MsgHeaderAddress*             pFrom;
100         NULL,                                                                   //MsgHeaderAddress*             pTo;
101         NULL,                                                                   //MsgHeaderAddress*             pCc;
102         NULL,                                                                   //MsgHeaderAddress*             pBcc;
103         "",                                                                             //char[]                                szSubject;
104         (MmsResponseStatus)MMS_RESPSTATUS_OK,   //MmsResponseStatus             iResponseStatus;
105         (MmsRetrieveStatus)MMS_RETRSTATUS_OK,   //MmsRetrieveStatus             iRetrieveStatus;
106         "",                                                                             //char[]                                szResponseText;
107         "",                                                                             //char[]                                szRetrieveText;
108
109
110         /* has default value in specification */
111
112         (MmsMsgClass)MMS_MSGCLASS_PERSONAL,             //MmsMsgClass                   msgClass;
113         {MMS_TIMETYPE_RELATIVE, 0},                             //MmsTimeStruct                 expiryTime;
114         {MMS_TIMETYPE_RELATIVE, 0},                             //MmsTimeStruct                 deliveryTime;
115         (MmsPriority)MMS_PRIORITY_NORMAL,               //MmsPriority                   priority;               // Refer [OMA-MMS-ENC-v1_2-20030915-C]
116         (MmsSenderVisible)MMS_SENDER_SHOW,              //MmsSenderVisible              senderVisible;
117         (MmsReport)MMS_REPORT_NO,                               //MmsReport                             deliveryReport;
118         (MmsReport)MMS_REPORT_NO,                               //MmsReport                             readReply;
119         (MmsReportAllowed)MMS_REPORTALLOWED_NO,//MmsReportAllowed               iReportAllowed;
120         "",                                                                             //char[]                                szContentLocation;
121
122
123         /* there is no right default value */
124
125         (msg_delivery_report_status_t)MSG_DELIVERY_REPORT_NONE,         //MmsMsgStatus                  iMsgStatus;
126         (msg_read_report_status_t)MSG_READ_REPORT_NONE,         //MmsReadStatus                 readStatus;
127
128         /* MMS v1.1 ReplyCharge */
129         {
130                 (MmsReplyChargeType)MMS_REPLY_NONE,     //MmsReplyChargeType    chargeType;
131                 {MMS_TIMETYPE_RELATIVE, 0},                     //MmsTimeStruct                 deadLine;
132                 0,                                                                      //int                                   chargeSize;
133                 "" ,                                                            //char                                  szChargeID;
134         },
135
136         "",                                                                             //char[]                                szMsgID;
137         0,                                                                              //UINT32                                msgSize;
138 };
139
140 #ifdef __SUPPORT_DRM__
141
142 #define MMS_DRM2_CONVERT_BUFFER_MAX     4*1024
143 const UINT32 MMS_UINTVAR_LENGTH_1 =  0x0000007f;                //7bit
144 const UINT32 MMS_UINTVAR_LENGTH_2 =  0x00003fff;                //14bit
145 const UINT32 MMS_UINTVAR_LENGTH_3 =  0x001fffff;                //21bit
146
147 #endif
148
149
150 bool _MmsDecodeInitialize(void)
151 {
152         MmsInitMsgType(&mmsHeader.msgType);
153         MmsInitMsgBody(&mmsHeader.msgBody);
154
155         return true;
156 }
157
158 bool __MmsSetMmsHeaderOwner(int msgID, char* pszNewOwner)
159 {
160         if (pszNewOwner) {
161                 // set new owner
162                 MSG_DEBUG("__MmsSetMmsHeaderOwner: set (%s, msgID=%d)\n", pszNewOwner, msgID);
163
164                 if ((mmsHeader.pszOwner = (char*)malloc(strlen(pszNewOwner) + 1)) == NULL)
165                         return false;
166
167                 memset (mmsHeader.pszOwner, 0, strlen(pszNewOwner) + 1) ;
168
169                 strcpy(mmsHeader.pszOwner, pszNewOwner);
170                 mmsHeader.bActive = true;
171                 mmsHeader.msgID = msgID;
172         } else {
173                 // delete current owner
174                 if (mmsHeader.pszOwner) {
175                         MSG_DEBUG("__MmsSetMmsHeaderOwner: free (%s %d)\n", mmsHeader.pszOwner, msgID);
176                         free(mmsHeader.pszOwner);
177                         mmsHeader.pszOwner = NULL;
178                 }
179                 mmsHeader.bActive = false;
180                 mmsHeader.msgID = -1;
181         }
182
183         return true;
184 }
185
186
187 void _MmsInitHeader()
188 {
189         mmsHeader.type = MMS_MSGTYPE_ERROR;
190
191         memset(mmsHeader.szTrID, 0, MMS_TR_ID_LEN + 1);
192         mmsHeader.version = MMS_VERSION;
193         mmsHeader.date = 0;
194
195         MsgFreeHeaderAddress(mmsHeader.pFrom);
196         MsgFreeHeaderAddress(mmsHeader.pTo);
197         MsgFreeHeaderAddress(mmsHeader.pCc);
198         MsgFreeHeaderAddress(mmsHeader.pBcc);
199
200         mmsHeader.pFrom = NULL;
201         mmsHeader.pTo = NULL;
202         mmsHeader.pCc = NULL;
203         mmsHeader.pBcc = NULL;
204
205         memset(mmsHeader.szSubject, 0, MSG_LOCALE_SUBJ_LEN + 1);
206
207         mmsHeader.responseStatus = (MmsResponseStatus)MMS_RESPSTATUS_OK;
208         mmsHeader.retrieveStatus = (MmsRetrieveStatus)MMS_RETRSTATUS_OK;
209         memset(mmsHeader.szResponseText, 0, MMS_LOCALE_RESP_TEXT_LEN + 1);
210         memset(mmsHeader.szRetrieveText, 0, MMS_LOCALE_RESP_TEXT_LEN + 1);
211
212
213         mmsHeader.msgClass = (MmsMsgClass)MMS_MSGCLASS_PERSONAL;
214         mmsHeader.expiryTime.type = MMS_TIMETYPE_RELATIVE;
215         mmsHeader.expiryTime.time = 0;
216         mmsHeader.deliveryTime.type     = MMS_TIMETYPE_RELATIVE;
217         mmsHeader.deliveryTime.time     = 0;
218         mmsHeader.priority = (MmsPriority)MMS_PRIORITY_NORMAL;  // Refer [OMA-MMS-ENC-v1_2-20030915-C]
219         mmsHeader.hideAddress =(MmsSenderVisible)MMS_SENDER_SHOW;
220         mmsHeader.deliveryReport = (MmsReport)MMS_REPORT_NO;
221         mmsHeader.readReply = (MmsReport)MMS_REPORT_NO;
222         mmsHeader.reportAllowed = (MmsReportAllowed)MMS_REPORTALLOWED_YES;
223         memset(mmsHeader.szContentLocation, 0, MMS_LOCATION_LEN + 1);
224
225
226         mmsHeader.msgStatus = (msg_delivery_report_status_t)MSG_DELIVERY_REPORT_NONE;
227         mmsHeader.readStatus = (msg_read_report_status_t)MSG_READ_REPORT_NONE;
228
229         mmsHeader.replyCharge.chargeType = (MmsReplyChargeType)MMS_REPLY_NONE;
230         mmsHeader.replyCharge.chargeSize = 0;
231         mmsHeader.replyCharge.deadLine.type = MMS_TIMETYPE_RELATIVE;
232         mmsHeader.replyCharge.deadLine.time = 0;
233         memset(mmsHeader.replyCharge.szChargeID, 0, MMS_MSG_ID_LEN + 1);
234
235
236         memset(mmsHeader.szMsgID, 0, MMS_MSG_ID_LEN + 1);
237         mmsHeader.msgSize = 0;
238 #ifdef __SUPPORT_DRM__
239         mmsHeader.drmType = MSG_DRM_TYPE_NONE;
240 #endif
241
242         _MmsDecodeInitialize();
243 }
244
245
246
247 void _MmsCleanDecodeBuff(void)
248 {
249         memset(gpMmsDecodeBuf1, 0, gMmsDecodeMaxLen + 1);
250         memset(gpMmsDecodeBuf2, 0, gMmsDecodeMaxLen + 1);
251         gpCurMmsDecodeBuff = NULL;
252         gCurMmsDecodeBuffPos = 0;
253         gMmsDecodeBufLen = 0;
254 }
255
256
257 void _MmsRegisterDecodeBuffer(char *pInBuff1, char *pInBuff2, int maxLen)
258 {
259         gpMmsDecodeBuf1 = pInBuff1;
260         gpMmsDecodeBuf2 = pInBuff2;
261         gpCurMmsDecodeBuff = NULL;
262         gCurMmsDecodeBuffPos = 0;
263         gMmsDecodeMaxLen = maxLen;
264         gMmsDecodeCurOffset = 0;
265         gMmsDecodeBufLen = 0;
266 }
267
268 void _MmsUnregisterDecodeBuffer(void)
269 {
270         gpMmsDecodeBuf1 = NULL;
271         gpMmsDecodeBuf2 = NULL;
272         gpCurMmsDecodeBuff = NULL;
273         gCurMmsDecodeBuffPos = 0;
274         gMmsDecodeMaxLen = 0;
275         gMmsDecodeCurOffset = 0;
276         gMmsDecodeBufLen = 0;
277 }
278
279
280 int _MmsGetDecodeOffset(void)
281 {
282         return (gMmsDecodeCurOffset - gMmsDecodeBufLen + gCurMmsDecodeBuffPos);
283 }
284
285
286 static bool __MmsBinaryDecodeCheckAndDecreaseLength(int *pLength, int valueLength)
287 {
288         if (*pLength <= valueLength) {
289                 gCurMmsDecodeBuffPos -= valueLength;
290                 gCurMmsDecodeBuffPos += *pLength;
291                 return false;
292         }
293
294         *pLength -= valueLength;
295
296         return true;
297 }
298
299
300 /* ==========================================================
301
302            B  I  N  A  R  Y         D  E  C  O  D  I  N  G
303
304    ==========================================================*/
305
306 /*
307  * Binary Encoded Message Format
308  *
309  *              < Single Part Body Message >
310  * -----------------------------------
311  * |            Header Fields                    |
312  * -----------------------------------
313  * | Content Type:start=xxx;type=xxx |  ->(ex) Text/Plain, Text/Html, ....
314  * -----------------------------------
315  * |            Single Part Body                 |
316  * -----------------------------------
317  *
318  *              < Multi Part Body Message >
319  * -----------------------------------
320  * |            Header Fields                    |
321  * -----------------------------------
322  * | Content Type:start=xxx;type=xxx |  -> (ex) Application/vnd.wap.multipart.mixed(related), multipart/mixed(related)
323  * -----------------------------------
324  * |    # of Entries (body parts)        |
325  * -----------------------------------                          < Each Entry >
326  * |                    Entry 1                          |      ->      -----------------------------
327  * -----------------------------------          |               header Length           |
328  * |                    Entry 2                          |              -----------------------------
329  * -----------------------------------          |               Data Length                     |
330  * |                    ......                           |              -----------------------------  -
331  * -----------------------------------          |               Content-Type            |  |
332  * |                    Entry n                          |              -----------------------------  | Header Length
333  * -----------------------------------          |                       Header                  |  |
334  *                                                                                      -----------------------------  -
335  *                                                                                      |                       Data                    |  | Data Length
336  *                                                                                      -----------------------------  -
337  */
338 bool MmsBinaryDecodeMsgHeader(FILE *pFile, int totalLength)
339 {
340         UINT16 fieldCode = 0xffff;
341         UINT16 fieldValue = 0xffff;
342         UINT8 oneByte = 0xff;
343
344         MsgHeaderAddress *pAddr = NULL;
345         MsgHeaderAddress *pLastTo = NULL;
346         MsgHeaderAddress *pLastCc = NULL;
347         MsgHeaderAddress *pLastBcc = NULL;
348
349         UINT32 valueLength = 0;
350         UINT32 tmpInteger = 0;
351         int     tmpIntLen = 0;
352
353         int offset = 0;
354
355         char szGarbageBuff[MSG_STDSTR_LONG]     = {0, };
356         char *pLimitData = NULL;
357         int nRead = 0;
358
359         MSG_DEBUG("MmsBinaryDecodeMsgHeader: pFile=%d, total len=%d\n", pFile, totalLength);
360
361         _MmsCleanDecodeBuff();
362
363         if (MsgLoadDataToDecodeBuffer(pFile, &gpCurMmsDecodeBuff, &gCurMmsDecodeBuffPos,
364                                                                    &gMmsDecodeCurOffset, gpMmsDecodeBuf1, gpMmsDecodeBuf2,
365                                                                    gMmsDecodeMaxLen, &gMmsDecodeBufLen, totalLength) == false) {
366                 MSG_DEBUG("MmsBinaryDecodeMsgHeader: fail to load to buffer \n");
367                 goto __CATCH;
368         }
369
370         while (_MmsBinaryDecodeGetOneByte(pFile, &oneByte, totalLength)) {
371                 fieldCode = oneByte & 0x7f;
372
373                 switch (_MmsGetBinaryType(MmsCodeFieldCode, fieldCode)) {
374                 case MMS_CODE_RESPONSESTATUS:
375
376                         if (_MmsBinaryDecodeGetOneByte(pFile, &oneByte, totalLength) == false) {
377                                 MSG_DEBUG("MmsBinaryDecodeMsgHeader: responseStatus GetOneByte fail\n");
378                                 goto __CATCH;
379                         }
380
381                         fieldValue = _MmsGetBinaryType(MmsCodeResponseStatus, (UINT16)(oneByte & 0x7F));
382
383                         if (fieldValue == 0xFFFF) {
384                                 MSG_DEBUG("MmsBinaryDecodeMsgHeader: responseStatus error\n");
385                                 goto __CATCH;
386                         }
387
388                         if (fieldValue >= 0x0044 && fieldValue <= 0x005F) {
389                                 fieldValue = 0x0040;
390                         } else if (fieldValue >= 0x006A && fieldValue <= 0x007F) {
391                                 fieldValue = 0x0060;
392                         }
393
394                         mmsHeader.responseStatus = (MmsResponseStatus)fieldValue;
395
396                         MSG_DEBUG("MmsBinaryDecodeMsgHeader: response status = %s\n", MmsDebugGetResponseStatus(mmsHeader.responseStatus));
397
398                         break;
399
400                 case MMS_CODE_RETRIEVESTATUS:
401
402                         if (_MmsBinaryDecodeGetOneByte(pFile, &oneByte, totalLength) == false) {
403                                 MSG_DEBUG("MmsBinaryDecodeMsgHeader: retrieveStatus GetOneByte fail\n");
404                                 goto __CATCH;
405                         }
406
407                         fieldValue = _MmsGetBinaryType(MmsCodeRetrieveStatus, (UINT16)(oneByte & 0x7F));
408
409                         if (fieldValue == 0xFFFF) {
410                                 MSG_DEBUG("MmsBinaryDecodeMsgHeader: retrieveStatus error\n");
411                                 goto __CATCH;
412                         }
413
414                         if (fieldValue >= 0x0043 && fieldValue <= 0x005F) {
415                                 fieldValue = 0x0040; // 192; Error-transient-failure
416                         } else if (fieldValue >= 0x0064 && fieldValue <= 0x007F) {
417                                 fieldValue = 0x0060; //224; Error-permanent-failure
418                         }
419
420                         mmsHeader.retrieveStatus = (MmsRetrieveStatus)fieldValue;
421
422                         MSG_DEBUG("MmsBinaryDecodeMsgHeader: retrieve status = %s\n",
423                                                                                                                         MmsDebugGetRetrieveStatus(mmsHeader.retrieveStatus));
424
425                         break;
426
427                 case MMS_CODE_RESPONSETEXT:
428
429                         if (__MmsBinaryDecodeEncodedString(pFile, mmsHeader.szResponseText, MMS_LOCALE_RESP_TEXT_LEN + 1, totalLength) == false) {
430                                 MSG_DEBUG("MmsBinaryDecodeMsgHeader : invalid MMS_CODE_RESPONSETEXT \n");
431                                 goto __CATCH;
432                         }
433
434                         MSG_DEBUG("MmsBinaryDecodeMsgHeader: response text = %s\n", mmsHeader.szResponseText);
435                         break;
436
437                 case MMS_CODE_RETRIEVETEXT:
438
439                         if (__MmsBinaryDecodeEncodedString(pFile, mmsHeader.szRetrieveText, MMS_LOCALE_RESP_TEXT_LEN + 1, totalLength) == false) {
440                                 MSG_DEBUG("MmsBinaryDecodeMsgHeader : invalid MMS_CODE_RETRIEVETEXT \n");
441                                 goto __CATCH;
442                         }
443
444                         MSG_DEBUG("MmsBinaryDecodeMsgHeader: retrieve text = %s\n", mmsHeader.szResponseText);
445                         break;
446
447                 case MMS_CODE_MSGID:
448
449                         if (__MmsBinaryDecodeText(pFile, mmsHeader.szMsgID, MMS_MSG_ID_LEN + 1, totalLength) < 0) {
450                                 MSG_DEBUG("MmsBinaryDecodeMsgHeader : MMS_CODE_MSGID is invalid\n");
451                                 goto __CATCH;
452                         }
453
454                         MSG_DEBUG("MmsBinaryDecodeMsgHeader: msg id = %s\n", mmsHeader.szMsgID);
455
456                         if (MsgStrlen (mmsHeader.szMsgID) > 2)
457                                 MsgMIMERemoveQuote (mmsHeader.szMsgID);
458
459                         break;
460
461                 case MMS_CODE_SUBJECT:
462
463                         if (__MmsBinaryDecodeEncodedString(pFile, mmsHeader.szSubject, MSG_LOCALE_SUBJ_LEN + 1, totalLength) == false) {
464                                 MSG_DEBUG("MmsBinaryDecodeMsgHeader : invalid MMS_CODE_SUBJECT \n");
465                                 goto __CATCH;
466                         }
467
468                         pLimitData = (char *)malloc(MSG_LOCALE_SUBJ_LEN + 1);
469
470                         if (pLimitData == NULL) {
471                                 MSG_DEBUG("MmsBinaryDecodeMsgHeader : pLimitData malloc fail \n");
472                                 goto __CATCH;
473                         }
474
475                         nRead = MsgCutUTFString((unsigned char*)pLimitData, MSG_LOCALE_SUBJ_LEN + 1, (unsigned char*)mmsHeader.szSubject, MSG_SUBJ_LEN);
476                         MSG_DEBUG("MmsBinaryDecodeMsgHeader : Subject edit.. \n");
477
478                         if (nRead > MSG_LOCALE_SUBJ_LEN) {
479                                 memset(mmsHeader.szSubject, 0 , sizeof(mmsHeader.szSubject));
480                                 strncpy(mmsHeader.szSubject, pLimitData, MSG_SUBJ_LEN);
481                         } else {
482                                 memset(mmsHeader.szSubject, 0 , sizeof(mmsHeader.szSubject));
483                                 strncpy(mmsHeader.szSubject, pLimitData, MSG_LOCALE_SUBJ_LEN);
484                         }
485
486                         if (pLimitData) {
487                                 free(pLimitData);
488                                 pLimitData = NULL;
489                         }
490
491                         MSG_DEBUG("MmsBinaryDecodeMsgHeader: subject = %s\n", mmsHeader.szSubject);
492                         break;
493
494                 case MMS_CODE_FROM:
495
496                         /* Value-length (Address-present-token Encoded-string-value | Insert-address-token) */
497
498                         if (__MmsDecodeValueLength(pFile, &valueLength, totalLength) <= 0) {
499                                 MSG_DEBUG("MmsBinaryDecodeMsgHeader : MMS_CODE_FROM is invalid\n");
500                                 goto __CATCH;
501                         }
502
503                         if (_MmsBinaryDecodeGetOneByte(pFile, &oneByte, totalLength) == false) {
504                                 MSG_DEBUG("MmsBinaryDecodeMsgHeader: MMS_CODE_FROM GetOneByte fail\n");
505                                 goto __CATCH;
506                         }
507
508                         // DRM_TEMPLATE - start
509
510                         valueLength--;
511
512                         if (oneByte == (_MmsGetBinaryValue(MmsCodeAddressType, MMS_ADDRESS_PRESENT_TOKEN)|0x80)) {
513                                 if (valueLength > 0) {
514                                         mmsHeader.pFrom = __MmsDecodeEncodedAddress(pFile, totalLength);
515                                         if (mmsHeader.pFrom == NULL) {
516                                                 MSG_DEBUG("MmsBinaryDecodeMsgHeader : MMS_CODE_FROM __MmsDecodeEncodedAddress fail\n");
517                                                 goto __CATCH;
518                                         }
519                                 } else {
520                                         mmsHeader.pFrom = (MsgHeaderAddress *)malloc(sizeof(MsgHeaderAddress));
521                                         if (mmsHeader.pFrom == NULL)
522                                                 goto __CATCH;
523
524                                         mmsHeader.pFrom->szAddr = (char *)malloc(1);
525                                         if (mmsHeader.pFrom->szAddr == NULL) {
526                                                 free(mmsHeader.pFrom);
527                                                 mmsHeader.pFrom = NULL;
528                                                 goto __CATCH;
529                                         }
530
531                                         mmsHeader.pFrom->szAddr[0] = '\0';
532                                         mmsHeader.pFrom->pNext = NULL;
533                                 }
534
535                                 MSG_DEBUG("MmsBinaryDecodeMsgHeader: from = %s\n", mmsHeader.pFrom->szAddr);
536                                 // DRM_TEMPLATE - end
537                         } else if (oneByte == (_MmsGetBinaryValue(MmsCodeAddressType, MMS_INSERT_ADDRESS_TOKEN)|0x80)) {
538                                 /* Present Token only */
539                                 MSG_DEBUG("MmsBinaryDecodeMsgHeader : MMS_CODE_FROM insert token\n");
540                         } else {
541                                 /* from data broken */
542                                 MSG_DEBUG("MmsBinaryDecodeMsgHeader: from addr broken\n");
543                                 gCurMmsDecodeBuffPos--;
544                                 goto __CATCH;
545                         }
546                         break;
547
548                 case MMS_CODE_TO:
549
550                         pAddr = __MmsDecodeEncodedAddress(pFile, totalLength);
551                         if (pAddr == NULL) {
552                                 MSG_DEBUG("MmsBinaryDecodeMsgHeader : MMS_CODE_TO __MmsDecodeEncodedAddress fail\n");
553                                 goto __CATCH;
554                         }
555
556                         if (mmsHeader.pTo == NULL) {
557                                 /* the first TO */
558                                 pLastTo = mmsHeader.pTo = pAddr;
559                         } else {
560                                 if (pLastTo)
561                                         pLastTo->pNext = pAddr;
562                                 pLastTo = pAddr;
563                         }
564
565                         MSG_DEBUG("MmsBinaryDecodeMsgHeader: to = %s\n", mmsHeader.pTo->szAddr);
566                         break;
567
568                 case MMS_CODE_BCC:
569
570                         pAddr = __MmsDecodeEncodedAddress(pFile, totalLength);
571                         if (pAddr == NULL) {
572                                 MSG_DEBUG("MmsBinaryDecodeMsgHeader : MMS_CODE_BCC __MmsDecodeEncodedAddress fail\n");
573                                 goto __CATCH;
574                         }
575
576                         if (mmsHeader.pBcc == NULL) {
577                                 /* the first Bcc */
578                                 pLastBcc = mmsHeader.pBcc = pAddr;
579                         } else {
580                                 if (pLastBcc)
581                                         pLastBcc->pNext = pAddr;
582                                 pLastBcc = pAddr;
583                         }
584
585                         MSG_DEBUG("MmsBinaryDecodeMsgHeader: bcc = %s\n", mmsHeader.pBcc->szAddr);
586                         break;
587
588                 case MMS_CODE_CC:
589
590                         pAddr = __MmsDecodeEncodedAddress(pFile, totalLength);
591                         if (pAddr == NULL) {
592                                 MSG_DEBUG("MmsBinaryDecodeMsgHeader : MMS_CODE_CC __MmsDecodeEncodedAddress fail\n");
593                                 goto __CATCH;
594                         }
595
596                         if (mmsHeader.pCc == NULL) {
597                                 /* the first Cc */
598                                 pLastCc = mmsHeader.pCc = pAddr;
599                         } else {
600                                 if (pLastCc)
601                                         pLastCc->pNext = pAddr;
602                                 pLastCc = pAddr;
603                         }
604                         MSG_DEBUG("MmsBinaryDecodeMsgHeader: cc = %s\n", mmsHeader.pCc->szAddr);
605                         break;
606
607                 case MMS_CODE_CONTENTLOCATION:
608
609                         if (__MmsBinaryDecodeText(pFile, mmsHeader.szContentLocation, MMS_LOCATION_LEN + 1, totalLength) < 0) {
610                                 MSG_DEBUG("MmsBinaryDecodeMsgHeader : MMS_CODE_CONTENTLOCATION is invalid\n");
611                                 goto __CATCH;
612                         }
613                         MSG_DEBUG("MmsBinaryDecodeMsgHeader: content location = %s\n", mmsHeader.szContentLocation);
614                         break;
615
616                 case MMS_CODE_DATE:
617
618                         if (__MmsDecodeLongInteger(pFile, (UINT32*)&mmsHeader.date, totalLength) == false) {
619                                 MSG_DEBUG("MmsBinaryDecodeMsgHeader : MMS_CODE_DATE is invalid\n");
620                                 goto __CATCH;
621                         }
622                         MSG_DEBUG("MmsBinaryDecodeMsgHeader: date = %d\n", mmsHeader.date);
623                         break;
624
625                 case MMS_CODE_DELIVERYREPORT:
626
627                         if (_MmsBinaryDecodeGetOneByte(pFile, &oneByte, totalLength) == false) {
628                                 MSG_DEBUG("MmsBinaryDecodeMsgHeader: deliveryReport GetOneByte fail\n");
629                                 goto __CATCH;
630                         }
631
632                         fieldValue = _MmsGetBinaryType(MmsCodeDeliveryReport, (UINT16)(oneByte & 0x7F));
633
634                         if (fieldValue == 0xFFFF) {
635                                 MSG_DEBUG("MmsBinaryDecodeMsgHeader: deliveryReport error\n");
636                                 goto __CATCH;
637                         }
638
639                         mmsHeader.deliveryReport = (MmsReport)fieldValue;
640
641                         MSG_DEBUG("MmsBinaryDecodeMsgHeader: delivery report=%s\n", MmsDebugGetMmsReport(mmsHeader.deliveryReport));
642                         break;
643
644                 case MMS_CODE_DELIVERYTIME:
645
646                         /* value_length (absolute-token Long-integer | Relative-token Long-integer) */
647
648                         if (__MmsDecodeValueLength(pFile, &valueLength, totalLength) <= 0) {
649                                 MSG_DEBUG("MmsBinaryDecodeMsgHeader : invalid MMS_CODE_DELIVERYTIME \n");
650                                 goto __CATCH;
651                         }
652
653                         if (_MmsBinaryDecodeGetOneByte(pFile, &oneByte, totalLength) == false) {
654                                 MSG_DEBUG("MmsBinaryDecodeMsgHeader: delivery time GetOneByte fail\n");
655                                 goto __CATCH;
656                         }
657
658                         //DRM_TEMPLATE - start
659                         valueLength--;
660
661                         if (oneByte == (_MmsGetBinaryValue(MmsCodeTimeType, MMS_TIMETYPE_ABSOLUTE)|0x80)) {
662                                 mmsHeader.deliveryTime.type = MMS_TIMETYPE_ABSOLUTE;
663
664                                 if (valueLength > 0) {
665                                         if (__MmsDecodeLongInteger(pFile, (UINT32*)&mmsHeader.deliveryTime.time, totalLength) == false) {
666                                                 MSG_DEBUG("MmsBinaryDecodeMsgHeader : invalid MMS_CODE_DELIVERYTIME\n");
667                                                 goto __CATCH;
668                                         }
669                                 }
670                         // DRM_TEMPLATE - end
671                         } else {
672                                 mmsHeader.deliveryTime.type = MMS_TIMETYPE_RELATIVE;
673
674                                 if (__MmsBinaryDecodeInteger(pFile, (UINT32*)&mmsHeader.deliveryTime.time, &tmpIntLen, totalLength) == false) {
675                                         MSG_DEBUG("MmsBinaryDecodeMsgHeader : __MmsBinaryDecodeInteger fail...\n");
676                                         goto __CATCH;
677                                 }
678                         }
679                         MSG_DEBUG("MmsBinaryDecodeMsgHeader: delivery type=%d, time=%d\n", mmsHeader.deliveryTime.type, mmsHeader.deliveryTime.time);
680                         break;
681
682                 case MMS_CODE_EXPIRYTIME:
683
684                         /* value_length(absolute-token Long-integer | Relative-token Long-integer) */
685
686                         if (__MmsDecodeValueLength(pFile, &valueLength, totalLength) <= 0) {
687                                 MSG_DEBUG("MmsBinaryDecodeMsgHeader : invalid MMS_CODE_EXPIRYTIME \n");
688                                 goto __CATCH;
689                         }
690
691                         if (_MmsBinaryDecodeGetOneByte(pFile, &oneByte, totalLength) == false) {
692                                 MSG_DEBUG("MmsBinaryDecodeMsgHeader: expiry time GetOneByte fail\n");
693                                 goto __CATCH;
694                         }
695
696                         // DRM_TEMPLATE - start
697                         valueLength--;
698
699                         if (oneByte == (_MmsGetBinaryValue(MmsCodeTimeType, MMS_TIMETYPE_ABSOLUTE)|0x80)) {
700                                 mmsHeader.expiryTime.type = MMS_TIMETYPE_ABSOLUTE;
701
702                                 if (valueLength > 0) {
703                                         if (__MmsDecodeLongInteger(pFile, (UINT32*)&mmsHeader.expiryTime.time, totalLength) == false) {
704                                                 MSG_DEBUG("MmsBinaryDecodeMsgHeader : MMS_CODE_EXPIRYTIME is invalid\n");
705                                                 goto __CATCH;
706                                         }
707                                 }
708                         // DRM_TEMPLATE - end
709                         } else {
710                                 mmsHeader.expiryTime.type = MMS_TIMETYPE_RELATIVE;
711
712                                 if (__MmsBinaryDecodeInteger(pFile, (UINT32*)&mmsHeader.expiryTime.time, &tmpIntLen, totalLength) == false) {
713                                         MSG_DEBUG("MmsBinaryDecodeMsgHeader : __MmsBinaryDecodeInteger fail...\n");
714                                         goto __CATCH;
715                                 }
716                         }
717                         MSG_DEBUG("MmsBinaryDecodeMsgHeader: expiry = %d\n", mmsHeader.expiryTime.time);
718                         break;
719
720                 case MMS_CODE_MSGCLASS:
721
722                         /* Class-value = Class-identifier | Token Text */
723
724                         if (_MmsBinaryDecodeGetOneByte(pFile, &oneByte, totalLength) == false) {
725                                 MSG_DEBUG("MmsBinaryDecodeMsgHeader: msgClass GetOneByte fail\n");
726                                 goto __CATCH;
727                         }
728
729                         if (oneByte > 0x7f) {
730                                 /* Class-identifier */
731                                 mmsHeader.msgClass = (MmsMsgClass)_MmsGetBinaryType(MmsCodeMsgClass, (UINT16)(oneByte & 0x7F));
732                         } else {
733                                 if (__MmsBinaryDecodeText(pFile, szGarbageBuff, MSG_STDSTR_LONG, totalLength) < 0) {
734                                         MSG_DEBUG("MmsBinaryDecodeMsgHeader: 1. __MmsBinaryDecodeText fail. (class)\n");
735                                         goto __CATCH;
736                                 }
737                         }
738                         MSG_DEBUG("MmsBinaryDecodeMsgHeader: msg class=%s\n", MmsDebugGetMsgClass(mmsHeader.msgClass));
739                         break;
740
741                 case MMS_CODE_MSGSIZE:
742
743                         if (__MmsDecodeLongInteger(pFile, (UINT32*)&mmsHeader.msgSize, totalLength) == false) {
744                                 MSG_DEBUG("MmsBinaryDecodeMsgHeader : MMS_CODE_MSGSIZE is invalid\n");
745                                 goto __CATCH;
746                         }
747                         MSG_DEBUG("MmsBinaryDecodeMsgHeader: msg size = %d\n", mmsHeader.msgSize);
748                         break;
749
750                 case MMS_CODE_MSGSTATUS:
751
752                         if (_MmsBinaryDecodeGetOneByte(pFile, &oneByte, totalLength) == false) {
753                                 MSG_DEBUG("MmsBinaryDecodeMsgHeader: msgStatus GetOneByte fail\n");
754                                 goto __CATCH;
755                         }
756
757                         mmsHeader.msgStatus =  (msg_delivery_report_status_t)_MmsGetBinaryType(MmsCodeMsgStatus, (UINT16)(oneByte & 0x7F));
758                         MSG_DEBUG("MmsBinaryDecodeMsgHeader: msg status=%s \n", MmsDebugGetMsgStatus(mmsHeader.msgStatus)) ;
759                         break;
760
761                 case MMS_CODE_MSGTYPE:
762
763                         if (_MmsBinaryDecodeGetOneByte(pFile, &oneByte, totalLength) == false) {
764                                 MSG_DEBUG("MmsBinaryDecodeMsgHeader: msgStatus GetOneByte fail\n");
765                                 goto __CATCH;
766                         }
767
768                         mmsHeader.type = (MmsMsgType)_MmsGetBinaryType(MmsCodeMsgType, (UINT16)(oneByte & 0x7F));
769                         MSG_DEBUG("MmsBinaryDecodeMsgHeader: msg type=%s\n", MmsDebugGetMsgType(mmsHeader.type));
770                         break;
771
772                 case MMS_CODE_PRIORITY:
773
774                         if (_MmsBinaryDecodeGetOneByte(pFile, &oneByte, totalLength) == false) {
775                                 MSG_DEBUG("MmsBinaryDecodeMsgHeader: msgStatus GetOneByte fail\n");
776                                 goto __CATCH;
777                         }
778                         mmsHeader.priority = (MmsPriority)_MmsGetBinaryType(MmsCodePriority, (UINT16)(oneByte & 0x7F));
779                         MSG_DEBUG("MmsBinaryDecodeMsgHeader: priority=%d\n", mmsHeader.priority);
780                         break;
781
782                 case MMS_CODE_READREPLY:
783
784                         if (_MmsBinaryDecodeGetOneByte(pFile, &oneByte, totalLength) == false) {
785                                 MSG_DEBUG("MmsBinaryDecodeMsgHeader: msgStatus GetOneByte fail\n");
786                                 goto __CATCH;
787                         }
788                         mmsHeader.readReply = (MmsReport)_MmsGetBinaryType(MmsCodeReadReply, (UINT16)(oneByte & 0x7F));
789                         MSG_DEBUG("MmsBinaryDecodeMsgHeader: read reply=%s \n", MmsDebugGetMmsReport(mmsHeader.readReply));
790                         break;
791
792                 case MMS_CODE_REPORTALLOWED:
793
794                         if (_MmsBinaryDecodeGetOneByte(pFile, &oneByte, totalLength) == false) {
795                                 MSG_DEBUG("MmsBinaryDecodeMsgHeader: msgStatus GetOneByte fail\n");
796                                 goto __CATCH;
797                         }
798                         mmsHeader.reportAllowed =  (MmsReportAllowed)_MmsGetBinaryType(MmsCodeReportAllowed, (UINT16)(oneByte & 0x7F));
799                         MSG_DEBUG("MmsBinaryDecodeMsgHeader: mmsHeader.reportAllowed=%d\n", MmsDebugGetMmsReportAllowed(mmsHeader.reportAllowed));
800                         break;
801
802                 case MMS_CODE_SENDERVISIBILLITY:
803
804                         if (_MmsBinaryDecodeGetOneByte(pFile, &oneByte, totalLength) == false) {
805                                 MSG_DEBUG("MmsBinaryDecodeMsgHeader: msgStatus GetOneByte fail\n");
806                                 goto __CATCH;
807                         }
808                         mmsHeader.hideAddress= (MmsSenderVisible)!(_MmsGetBinaryType(MmsCodeSenderVisibility, (UINT16)(oneByte &0x7F)));
809                         MSG_DEBUG("MmsBinaryDecodeMsgHeader: sender visible=%d \n", mmsHeader.hideAddress);
810                         break;
811
812                 case MMS_CODE_TRID:
813
814                         if (__MmsBinaryDecodeText(pFile, mmsHeader.szTrID, MMS_TR_ID_LEN + 1, totalLength) < 0) {
815                                 MSG_DEBUG("Transaction ID Too Long \n");
816                                 goto __CATCH;
817                         }
818                         MSG_DEBUG("MmsBinaryDecodeMsgHeader: trID = %s\n", mmsHeader.szTrID);
819                         break;
820
821                 case MMS_CODE_VERSION:
822                         if (_MmsBinaryDecodeGetOneByte(pFile, &oneByte, totalLength) == false) {
823                                 MSG_DEBUG("MmsBinaryDecodeMsgHeader: msgStatus GetOneByte fail\n");
824                                 goto __CATCH;
825                         }
826                         mmsHeader.version = oneByte;
827
828                         MSG_DEBUG("MmsBinaryDecodeMsgHeader: ver = 0x%x\n", mmsHeader.version);
829                         break;
830
831                 case MMS_CODE_CONTENTTYPE:
832
833                         /*
834                          * Content-type is the last header field of SendRequest and RetrieveConf.
835                          * It's required to decrease pointer by one and return,
836                          * to parse this field in MmsBinaryDecodeContentType().
837                          */
838                         goto __RETURN;
839
840
841                 /* ----------- Add by MMSENC v1.1 ----------- */
842
843                 case MMS_CODE_READSTATUS:
844
845                         /* Read-status-value = Read | Deleted without being read */
846
847                         if (_MmsBinaryDecodeGetOneByte(pFile, &oneByte, totalLength) == false) {
848                                 MSG_DEBUG("MmsBinaryDecodeMsgHeader: msgStatus GetOneByte fail\n");
849                                 goto __CATCH;
850                         }
851
852                         mmsHeader.readStatus =  (msg_read_report_status_t)_MmsGetBinaryType(MmsCodeReadStatus, (UINT16)(oneByte & 0x7F));
853                         MSG_DEBUG("MmsBinaryDecodeMsgHeader: read status=%s\n", MmsDebugGetMmsReadStatus(mmsHeader.readStatus));
854                         break;
855
856                 case MMS_CODE_REPLYCHARGING:
857
858                         /* Reply-charging-value = Requested | Requested text only | Accepted | Accepted text only */
859
860                         if (_MmsBinaryDecodeGetOneByte(pFile, &oneByte, totalLength) == false) {
861                                 MSG_DEBUG("MmsBinaryDecodeMsgHeader: msgStatus GetOneByte fail\n");
862                                 goto __CATCH;
863                         }
864
865                         mmsHeader.replyCharge.chargeType =  (MmsReplyChargeType)_MmsGetBinaryType(MmsCodeReplyCharging, (UINT16)(oneByte & 0x7F));
866                         MSG_DEBUG("MmsBinaryDecodeMsgHeader: mmsHeader.reply charge=%d\n", mmsHeader.replyCharge.chargeType);
867                         break;
868
869                 case MMS_CODE_REPLYCHARGINGDEADLINE:
870
871                         /* Reply-charging-deadline-value = Value-length (Absolute-token Date-value | Relative-token Delta-seconds-value) */
872
873                         if (__MmsDecodeValueLength(pFile, &valueLength, totalLength) <= 0) {
874                                 MSG_DEBUG("MmsBinaryDecodeMsgHeader : invalid MMS_CODE_REPLYCHARGINGDEADLINE \n");
875                                 goto __CATCH;
876                         }
877
878                         if (_MmsBinaryDecodeGetOneByte(pFile, &oneByte, totalLength) == false) {
879                                 MSG_DEBUG("MmsBinaryDecodeMsgHeader: msgStatus GetOneByte fail\n");
880                                 goto __CATCH;
881                         }
882
883                         if (oneByte == (_MmsGetBinaryValue(MmsCodeTimeType, MMS_TIMETYPE_ABSOLUTE) | 0x80)) {
884                                 mmsHeader.replyCharge.deadLine.type = MMS_TIMETYPE_ABSOLUTE;
885                         } else {
886                                 mmsHeader.replyCharge.deadLine.type = MMS_TIMETYPE_RELATIVE;
887                         }
888
889                         // DRM_TEMPLATE - start
890                         valueLength--;
891
892                         if (valueLength > 0) {
893                                 if (__MmsDecodeLongInteger(pFile, (UINT32*)&mmsHeader.replyCharge.deadLine.time, totalLength) == false) {
894                                         MSG_DEBUG("MmsBinaryDecodeMsgHeader : MMS_CODE_REPLYCHARGINGDEADLINE is invalid\n");
895                                         goto __CATCH;
896                                 }
897                         }
898                         // DRM_TEMPLATE - end
899                         break;
900
901                 case MMS_CODE_REPLYCHARGINGID:
902
903                         /* Reply-charging-ID-value = Text-string */
904
905                         if (__MmsBinaryDecodeText(pFile, mmsHeader.replyCharge.szChargeID, MMS_MSG_ID_LEN + 1, totalLength) < 0) {
906                                 MSG_DEBUG("MmsBinaryDecodeMsgHeader: 1. __MmsBinaryDecodeText fail. (szReplyChargingID)\n");
907                                 goto __CATCH;
908                         }
909                         break;
910
911                 case MMS_CODE_REPLYCHARGINGSIZE:
912
913                         /* Reply-charging-size-value = Long-integer */
914
915                         if (__MmsDecodeLongInteger(pFile, (UINT32*)&mmsHeader.replyCharge.chargeSize, totalLength) == false) {
916                                 MSG_DEBUG("MmsBinaryDecodeMsgHeader : MMS_CODE_REPLYCHARGINGSIZE is invalid\n");
917                                 goto __CATCH;
918                         }
919                         break;
920
921                 case MMS_CODE_PREVIOUSLYSENTBY:
922
923                         /*
924                          * Previously-sent-by-value = Value-length Forwarded-count-value Encoded-string-value
925                          * Forwarded-count-value = Integer-value
926                          * MMS_CODE_PREVIOUSLYSENTBY shall be a pair with MMS_CODE_PREVIOUSLYSENTDATE
927                          */
928
929                         /*
930                          * fixme: There is no proper field to store this information.
931                          * Just increase pointer now.
932                          */
933
934                         if (__MmsDecodeValueLength(pFile, &valueLength, totalLength) <= 0) {
935                                 MSG_DEBUG("MmsBinaryDecodeMsgHeader : 1. invalid MMS_CODE_PREVIOUSLYSENTBY \n");
936                                 goto __CATCH;
937                         }
938
939                         if (__MmsBinaryDecodeInteger(pFile, &tmpInteger, &tmpIntLen, totalLength) == false) {
940                                 MSG_DEBUG("MmsBinaryDecodeMsgHeader : 2. invalid MMS_CODE_PREVIOUSLYSENTBY \n");
941                                 goto __CATCH;
942                         }
943
944                         if (__MmsBinaryDecodeEncodedString(pFile, szGarbageBuff, MSG_STDSTR_LONG, totalLength) == false) {
945                                 MSG_DEBUG("MmsBinaryDecodeMsgHeader : invalid MMS_CODE_RETRIEVETEXT \n");
946                                 goto __CATCH;
947                         }
948                         break;
949
950                 case MMS_CODE_PREVIOUSLYSENTDATE:
951
952                         /*
953                          * Previously-sent-date-value = Value-length Forwarded-count-value Date-value
954                          * Forwarded-count-value = Integer-value
955                          * MMS_CODE_PREVIOUSLYSENTDATE shall be a pair with MMS_CODE_PREVIOUSLYSENTBY
956                          */
957
958                         /*
959                          * fixme: There is no proper field to store this information.
960                          * Just increase pointer now.
961                          */
962
963                         if (__MmsDecodeValueLength(pFile, &valueLength, totalLength) <= 0) {
964                                 MSG_DEBUG("MmsBinaryDecodeMsgHeader : 1. invalid MMS_CODE_PREVIOUSLYSENTDATE \n");
965                                 goto __CATCH;
966                         }
967
968                         if (__MmsBinaryDecodeInteger(pFile, &tmpInteger, &tmpIntLen, totalLength) == false) {
969                                 MSG_DEBUG("MmsBinaryDecodeMsgHeader : 2. invalid MS_CODE_PREVIOUSLYSENTDATE \n");
970                                 goto __CATCH;
971                         }
972
973                         if (__MmsDecodeLongInteger(pFile, (UINT32*)&tmpInteger, totalLength) == false) {
974                                 MSG_DEBUG("MmsBinaryDecodeMsgHeader : 3. invalid MMS_CODE_PREVIOUSLYSENTDATE \n");
975                                 goto __CATCH;
976                         }
977                         break;
978
979
980                 default:
981
982                         /*
983                          * Application-header             = Token-text Application-specific-value
984                          * Token-text                             = Token End-of-string
985                          * Application-specific-value = Text -string
986                          *
987                          * OR unknown header field - Just ignore these fields.
988                          *
989                          * Read one byte and check the value >= 0x80
990                          * (check these value can be field code)
991                          */
992                         {
993                                 int             remainLength = 0;
994
995                                 oneByte = 0x00;
996
997                                 offset = _MmsGetDecodeOffset();
998                                 if (offset >= totalLength)
999                                         goto __RETURN;
1000
1001                                 remainLength = totalLength - offset;
1002
1003                                 while ((oneByte < 0x80) && (remainLength > 0)) {
1004                                         if (__MmsBinaryDecodeCheckAndDecreaseLength(&remainLength, 1) == false) {
1005                                                 MSG_DEBUG("MmsBinaryDecodeMsgHeader: __MmsBinaryDecodeCheckAndDecreaseLength fail\n");
1006                                                 goto __CATCH;
1007                                         }
1008                                         if (_MmsBinaryDecodeGetOneByte(pFile, &oneByte, totalLength) == false) {
1009                                                 MSG_DEBUG("MmsBinaryDecodeMsgHeader: responseStatus GetOneByte fail\n");
1010                                                 goto __CATCH;
1011                                         }
1012                                 }
1013
1014                                 gCurMmsDecodeBuffPos--;
1015                         }
1016
1017                         break;
1018                 }       /* switch */
1019
1020                 offset = _MmsGetDecodeOffset();
1021                 if (offset >= totalLength)
1022                         goto __RETURN;
1023
1024         }       /* while */
1025
1026
1027 __RETURN:
1028
1029         if (mmsHeader.pTo == NULL && pLastTo) {
1030                 free(pLastTo);
1031         }
1032
1033         if (mmsHeader.pCc == NULL && pLastCc) {
1034                 free(pLastCc);
1035         }
1036
1037         if (mmsHeader.pBcc == NULL && pLastBcc) {
1038                 free(pLastBcc);
1039         }
1040
1041         MSG_DEBUG("MmsBinaryDecodeMsgHeader: success\n");
1042         return true;
1043
1044
1045 __CATCH:
1046
1047         if (mmsHeader.pTo == NULL && pLastTo) {
1048                 free(pLastTo);
1049         }
1050
1051         if (mmsHeader.pCc == NULL && pLastCc) {
1052                 free(pLastCc);
1053         }
1054
1055         if (mmsHeader.pBcc == NULL && pLastBcc) {
1056                 free(pLastBcc);
1057         }
1058
1059         MSG_DEBUG("MmsBinaryDecodeMsgHeader: failed\n");
1060
1061         return false;
1062 }
1063
1064 #ifdef FEATURE_JAVA_MMS
1065 // for JAVA MMS AppId - check later whether it can be handled by MmsBinaryDecodeMsgHeader
1066 bool MmsBinaryDecodeContentType(FILE *pFile, char *szFilePath, int totalLength)
1067 {
1068         int length      = 0;
1069
1070         MSG_DEBUG("MmsBinaryDecodeContentType:\n");
1071
1072         if (szFilePath != NULL)
1073                 strncpy(mmsHeader.msgType.szOrgFilePath, szFilePath, strlen(szFilePath));
1074         mmsHeader.msgType.offset = _MmsGetDecodeOffset() - 1;           // + Content-Type code value
1075
1076         // read data(2K) from msg file(/User/Msg/Inbox/5) to gpCurMmsDecodeBuff for decoding
1077         if (MsgLoadDataToDecodeBuffer(pFile, &gpCurMmsDecodeBuff, &gCurMmsDecodeBuffPos,
1078                                                                         &gMmsDecodeCurOffset, gpMmsDecodeBuf1, gpMmsDecodeBuf2,
1079                                                                         gMmsDecodeMaxLen, &gMmsDecodeBufLen, totalLength) == false) {
1080                 MSG_DEBUG("MmsBinaryDecodeContentType: fail to load to buffer \n");
1081                 goto __CATCH;
1082         }
1083
1084         // msg's type [ex] related, mixed, single part (jpg, amr and etc)
1085         length = MmsBinaryDecodeContentType(pFile, &mmsHeader.msgType, totalLength);
1086         if (length == -1) {
1087                 MSG_DEBUG("MmsBinaryDecodeContentType: MMS_CODE_CONTENTTYPE is fail\n");
1088                 goto __CATCH;
1089         }
1090         MSG_DEBUG("Content-Type: Application-ID:%s Reply-To-Application-ID:%s",mmsHeader.msgType.param.szApplicationID, mmsHeader.msgType.param.szReplyToApplicationID);
1091
1092         return true;
1093
1094 __CATCH:
1095
1096         /* fixme: Delete multipart using MmsDeleteMsg() */
1097
1098         return false;
1099 }
1100 #endif
1101
1102 bool MmsBinaryDecodeMsgBody(FILE *pFile, char *szFilePath, int totalLength)
1103 {
1104         int length = 0;
1105         int offset = 0;
1106
1107         MSG_DEBUG("MmsBinaryDecodeMsgBody:\n");
1108
1109         if (szFilePath != NULL)
1110                 strncpy(mmsHeader.msgType.szOrgFilePath, szFilePath , strlen(szFilePath));
1111
1112         mmsHeader.msgType.offset = _MmsGetDecodeOffset() - 1;           // + Content-Type code value
1113
1114         // read data(2K) from msg file(/User/Msg/Inbox/5) to gpCurMmsDecodeBuff for decoding
1115         if (MsgLoadDataToDecodeBuffer(pFile, &gpCurMmsDecodeBuff, &gCurMmsDecodeBuffPos,
1116                                                                         &gMmsDecodeCurOffset, gpMmsDecodeBuf1, gpMmsDecodeBuf2,
1117                                                                         gMmsDecodeMaxLen, &gMmsDecodeBufLen, totalLength) == false) {
1118                 MSG_DEBUG("MmsBinaryDecodeMsgBody: fail to load to buffer \n");
1119                 goto __CATCH;
1120         }
1121
1122         // msg's type [ex] related, mixed, single part (jpg, amr and etc)
1123         length = MmsBinaryDecodeContentType(pFile, &mmsHeader.msgType, totalLength);
1124         if (length == -1) {
1125                 MSG_DEBUG("MmsBinaryDecodeMsgBody: MMS_CODE_CONTENTTYPE is fail\n");
1126                 goto __CATCH;
1127         }
1128
1129         mmsHeader.msgType.size   = length + 1;                                          // + Content-Type code value
1130         mmsHeader.msgBody.offset = _MmsGetDecodeOffset();
1131
1132         switch (mmsHeader.msgType.type) {
1133         case MIME_APPLICATION_VND_WAP_MULTIPART_MIXED:
1134         case MIME_APPLICATION_VND_WAP_MULTIPART_RELATED:
1135         case MIME_APPLICATION_VND_WAP_MULTIPART_ASTERIC:
1136         case MIME_APPLICATION_VND_WAP_MULTIPART_ALTERNATIVE:
1137         case MIME_MULTIPART_REPORT:
1138         case MIME_MULTIPART_MIXED:
1139         case MIME_MULTIPART_RELATED:
1140         case MIME_MULTIPART_ALTERNATIVE:
1141         case MIME_APPLICATION_VND_OMA_DRM_MESSAGE:
1142         case MIME_APPLICATION_VND_OMA_DRM_CONTENT:
1143
1144                 MSG_DEBUG("__MmsBinaryMsgBodyDecode: Decode multipart\n");
1145
1146                 offset = _MmsGetDecodeOffset();
1147                 if (offset >= totalLength)
1148                         goto __RETURN;
1149
1150                 if (MmsBinaryDecodeMultipart(pFile, szFilePath, &mmsHeader.msgType, &mmsHeader.msgBody, totalLength) == false) {
1151                         MSG_DEBUG("__MmsBinaryMsgBodyDecode: MmsBinaryDecodeMultipart is fail.\n");
1152                         goto __CATCH;
1153                 }
1154                 break;
1155
1156         default:
1157
1158                 /* Single part message ---------------------------------------------- */
1159                 if (szFilePath != NULL)
1160                         strcpy(mmsHeader.msgBody.szOrgFilePath, szFilePath);
1161
1162                 offset = _MmsGetDecodeOffset();
1163                 if (offset >= totalLength)
1164                         goto __RETURN;
1165
1166                 if (MmsBinaryDecodePartBody(pFile, totalLength - mmsHeader.msgBody.offset, totalLength) == false) {
1167                         MSG_DEBUG("__MmsBinaryMsgBodyDecode: MmsBinaryDecodePartBody is fail.(Single Part)\n");
1168                         goto __CATCH;
1169                 }
1170
1171                 mmsHeader.msgBody.size = totalLength - mmsHeader.msgBody.offset;
1172                 mmsHeader.msgType.contentSize = totalLength - mmsHeader.msgBody.offset;
1173
1174                 break;
1175         }
1176
1177 #ifdef __SUPPORT_DRM__
1178         mmsHeader.drmType = MsgGetDRMType(&mmsHeader.msgType, &mmsHeader.msgBody);
1179 #endif
1180
1181 __RETURN:
1182
1183         return true;
1184
1185 __CATCH:
1186
1187         return false;
1188 }
1189
1190 #if MMS_ENABLE_EXTEND_CFM
1191 // DRM (Extended)CFM
1192 //      Get Extended CFM value.
1193 static bool __MmsConvertString2Bool(char *pszValue)
1194 {
1195         if (!strcasecmp(pszValue, "no"))
1196                 return true;
1197
1198         return false;
1199 }
1200 #endif
1201
1202 bool MmsBinaryDecodeParameter(FILE *pFile, MsgType *pMsgType, int valueLength, int totalLength)
1203 {
1204         UINT8 oneByte = 0;
1205         int charSetLen = 0;
1206         char *szTypeString = NULL;
1207         char *szTypeValue = NULL;
1208         UINT8 paramCode = 0xff;
1209         UINT32 integer = 0;
1210         int intLen = 0;
1211         int length = 0;
1212         int textLength = 0;
1213
1214         /*
1215          * Parameter = Typed-parameter | Untyped-parameter
1216          * WAP-230-WSP-20010118-p, Proposed Version 18 January 2001 (pp.107)
1217          */
1218
1219         MSG_DEBUG("MmsBinaryDecodeParameter: \n");
1220
1221         while (valueLength > 0) {
1222                 if (_MmsBinaryDecodeGetOneByte(pFile, &oneByte, totalLength) == false) {
1223                         MSG_DEBUG("MmsBinaryDecodeContentType: paramCode _MmsBinaryDecodeGetOneByte fail\n");
1224                         goto __CATCH;
1225                 }
1226
1227                 paramCode = oneByte;
1228                 valueLength--;
1229
1230                 switch (paramCode) {
1231                 case 0x81: // charset
1232
1233                         if (__MmsBinaryDecodeCharset(pFile, (UINT32*)&(pMsgType->param.charset), &charSetLen, totalLength) == false) {
1234                                 MSG_DEBUG("MmsBinaryDecodeContentType : __MmsBinaryDecodeCharset fail.\n");
1235                                 goto __CATCH;
1236                         }
1237
1238                         if (__MmsBinaryDecodeCheckAndDecreaseLength(&valueLength, charSetLen) == false)
1239                                 goto __RETURN;
1240
1241                         break;
1242
1243                 case 0x85: //name = Text-string
1244                 case 0x97: //name = Text-value  = No-value | Token-text | Quoted-string
1245                         memset(pMsgType->param.szName, 0, sizeof(pMsgType->param.szName));
1246                         length = __MmsDecodeGetFilename(pFile,  pMsgType->param.szName,
1247                                                                                          MSG_FILENAME_LEN_MAX -5,               // MSG_LOCALE_FILENAME_LEN_MAX + 1, :  change @ 110(Ui code have to change for this instead of DM)
1248                                                                                          totalLength);
1249                         if (length < 0) {
1250                                 MSG_DEBUG("MmsBinaryDecodeContentType : __MmsDecodeGetFilename fail. (name parameter)\n");
1251                                 goto __CATCH;
1252                         }
1253
1254                         if (MsgCheckFileNameHasInvalidChar(pMsgType->param.szName)) {
1255                                 _MsgReplaceInvalidFileNameChar(pMsgType->param.szName, '_');
1256                         }
1257
1258                         if (__MmsBinaryDecodeCheckAndDecreaseLength(&valueLength, length) == false)
1259                                 goto __RETURN;
1260
1261                         break;
1262
1263                 case 0x86: //filename = Text-string
1264                 case 0x98: //filename = Text-value  = No-value | Token-text | Quoted-string
1265                         memset(pMsgType->param.szFileName, 0, sizeof(pMsgType->param.szFileName));
1266                         length = __MmsDecodeGetFilename(pFile, pMsgType->param.szFileName, MSG_FILENAME_LEN_MAX -5, totalLength);
1267                         if (length < 0) {
1268                                 MSG_DEBUG("MmsBinaryDecodeContentType : __MmsDecodeGetFilename fail. (filename parameter)\n");
1269                                 goto __CATCH;
1270                         }
1271
1272                         if (MsgCheckFileNameHasInvalidChar(pMsgType->param.szFileName)) {
1273                                 _MsgReplaceInvalidFileNameChar(pMsgType->param.szFileName, '_');
1274                         }
1275
1276                         if (__MmsBinaryDecodeCheckAndDecreaseLength(&valueLength, length) == false)
1277                                 goto __RETURN;
1278
1279                         break;
1280
1281                 case 0x89: //type = Constrained-encoding = Extension-Media | Short-integer
1282
1283                         if (_MmsBinaryDecodeGetOneByte(pFile, &oneByte, totalLength) == false) {
1284                                 MSG_DEBUG("MmsBinaryDecodeContentType: type _MmsBinaryDecodeGetOneByte fail\n");
1285                                 goto __CATCH;
1286                         }
1287
1288                         if (oneByte > 0x7f) {
1289                                 pMsgType->param.type = _MmsGetBinaryType(MmsCodeContentType,
1290                                                                                                                   (UINT16)(oneByte & 0x7f));
1291                                 if (__MmsBinaryDecodeCheckAndDecreaseLength(&valueLength, 1) == false)
1292                                         goto __RETURN;
1293                         } else {
1294                                 gCurMmsDecodeBuffPos--;
1295
1296                                 textLength = 0;
1297                                 szTypeString = __MmsBinaryDecodeText2(pFile, totalLength, &textLength);
1298                                 pMsgType->param.type = _MmsGetTextType(MmsCodeContentType, szTypeString);
1299
1300                                 if (szTypeString) {
1301                                         free(szTypeString);
1302                                         szTypeString = NULL;
1303                                 }
1304
1305                                 if (__MmsBinaryDecodeCheckAndDecreaseLength(&valueLength, textLength) == false)
1306                                         goto __RETURN;
1307                         }
1308
1309                         break;
1310
1311                 case 0x8A: //start encoding version 1.2
1312                 case 0x99: //start encoding version 1.4
1313
1314                         textLength       = 0;
1315                         szTypeString = __MmsBinaryDecodeText2(pFile, totalLength, &textLength);
1316                         if (szTypeString) {
1317                                 memset(pMsgType->param.szStart, 0, MMS_CONTENT_ID_LEN + 1);
1318                                 strncpy(pMsgType->param.szStart, szTypeString, MMS_CONTENT_ID_LEN);
1319                                 free(szTypeString);
1320                                 szTypeString = NULL;
1321
1322                                 if (__MmsBinaryDecodeCheckAndDecreaseLength(&valueLength, textLength) == false)
1323                                         goto __RETURN;
1324                         }
1325
1326                         break;
1327
1328                 case 0x8B: //startInfo encoding version 1.2
1329                 case 0x9A: //startInfo encoding version 1.4
1330
1331                         textLength       = 0;
1332                         szTypeString = __MmsBinaryDecodeText2(pFile, totalLength, &textLength);
1333
1334                         if (szTypeString) {
1335                                 memset(pMsgType->param.szStartInfo, 0, MMS_CONTENT_ID_LEN + 1);
1336                                 strncpy(pMsgType->param.szStartInfo, szTypeString, MMS_CONTENT_ID_LEN);
1337
1338                                 free(szTypeString);
1339                                 szTypeString = NULL;
1340
1341                                 if (__MmsBinaryDecodeCheckAndDecreaseLength(&valueLength, textLength) == false)
1342                                         goto __RETURN;
1343                         }
1344
1345                         break;
1346
1347                 default:
1348
1349                         if (paramCode > 0x7F) {
1350                                 MSG_DEBUG("MmsBinaryDecodeContentType: Unsupported parameter\n");
1351
1352                                 // In case of the last byte of Parameter field, it should be returned without decreasing the gCurMmsDecodeBuffPos value.
1353                                 valueLength++;
1354                                 if (__MmsBinaryDecodeCheckAndDecreaseLength(&valueLength, 1) == false)
1355                                         goto __RETURN;
1356                         } else {
1357                                 /*
1358                                  * Untyped Parameter = Token-text Untyped-value
1359                                  * Token-text            = Token End-of-string
1360                                  * Untyped-value         = Integer-value | Text-value
1361                                  * Text-value            = No-value | Token-text | Quoted-string
1362                                  *
1363                                  * Just increase pointer!!!
1364                                  */
1365
1366
1367                                 /* Token-text */
1368
1369                                 gCurMmsDecodeBuffPos--;
1370                                 valueLength++;
1371
1372                                 textLength        = 0;
1373                                 szTypeString  = __MmsBinaryDecodeText2(pFile, totalLength, &textLength);
1374                                 if (__MmsBinaryDecodeCheckAndDecreaseLength(&valueLength, textLength) == false)
1375                                         goto __RETURN;
1376
1377
1378                                 /* Text-value */
1379
1380                                 if (__MmsBinaryDecodeInteger(pFile, &integer, &intLen, totalLength) == true) {
1381                                         MSG_DEBUG("MmsBinaryDecodeContentType: Unsupported parameter(%d)\n", integer);
1382                                         if (__MmsBinaryDecodeCheckAndDecreaseLength(&valueLength, intLen) == false)
1383                                                 goto __RETURN;
1384                                 } else {
1385                                         textLength        = 0;
1386                                         szTypeValue  = __MmsBinaryDecodeText2(pFile, totalLength, &textLength);
1387
1388                                         if (szTypeValue) {
1389                                                 /* checkMe:  forwardLock needs boudary string */
1390                                                 if (strcasecmp(szTypeString, "boundary") == 0) {
1391                                                         memset(pMsgType->param.szBoundary, 0, MSG_BOUNDARY_LEN + 1);
1392                                                         strncpy(pMsgType->param.szBoundary, szTypeValue, MSG_BOUNDARY_LEN);
1393 #ifdef FEATURE_JAVA_MMS
1394                                                 } else if (strcasecmp(szTypeString, "Application-ID") == 0) {
1395                                                         pMsgType->param.szApplicationID = (char*) malloc(textLength + 1);
1396                                                         memset(pMsgType->param.szApplicationID,  0,  textLength + 1);
1397                                                         strncpy(pMsgType->param.szApplicationID, szTypeValue, textLength);
1398                                                         MSG_DEBUG("Application-ID:%s",pMsgType->param.szApplicationID);
1399                                                 } else if (strcasecmp(szTypeString,"Reply-To-Application-ID") == 0) {
1400                                                         pMsgType->param.szReplyToApplicationID= (char*) malloc(textLength + 1);
1401                                                         memset(pMsgType->param.szReplyToApplicationID, 0, textLength + 1);
1402                                                         strncpy(pMsgType->param.szReplyToApplicationID, szTypeValue, textLength);
1403                                                         MSG_DEBUG("ReplyToApplication-ID:%s",pMsgType->param.szReplyToApplicationID);
1404 #endif
1405                                                 }
1406                                                 free(szTypeValue);
1407                                                 szTypeValue = NULL;
1408
1409                                                 MSG_DEBUG("MmsBinaryDecodeContentType: Unsupported parameter(%s)\n", szTypeValue);
1410                                                 if (__MmsBinaryDecodeCheckAndDecreaseLength(&valueLength, textLength) == false)
1411                                                         goto __RETURN;
1412                                         }
1413                                 }
1414
1415                                 if (szTypeString) {
1416                                         MSG_DEBUG("MmsBinaryDecodeContentType: Unsupported parameter(%s)\n", szTypeString);
1417                                         free(szTypeString);
1418                                         szTypeString = NULL;
1419                                 }
1420                         }
1421
1422                         break;
1423                 }
1424         }       /*end of while loop*/
1425
1426
1427 __RETURN:
1428
1429         if (szTypeString) {
1430                 free(szTypeString);
1431                 szTypeString = NULL;
1432         }
1433
1434         return true;
1435
1436 __CATCH:
1437
1438         return false;
1439 }
1440
1441
1442
1443 /**
1444  * Decode Encoded Content type
1445  *
1446  * @param       pEncodedData    [in] ContentType encoded data
1447  * @param       pMsgType                [out] Decoded MsgType
1448  * @return      Decoded address list
1449  */
1450 int MmsBinaryDecodeContentType(FILE *pFile, MsgType *pMsgType, int totalLength)
1451 {
1452         UINT8 oneByte = 0;
1453         char *szTypeString = NULL;
1454         int valueLength = 0;
1455         int length = 0;
1456         int textLength = 0;
1457
1458
1459         /*
1460          * Content-type-value             : [WAPWSP 8.4.2.24]
1461          * Preassigned content-types  : [WAPWSP Appendix A, Table 40]
1462          * The use of start-parameter : [RFC2387] and SHOULD be encoded according to [WAPWSP].
1463          *
1464          * Content-type-value   = Constrained-media | Content-general-form
1465          * Content-general-form = Value-length Media-type
1466          * Media-type                   = (Well-known-media | Extension-Media) *(Parameter)
1467          */
1468
1469         MSG_DEBUG("MmsBinaryDecodeContentType: decoding content type..\n");
1470
1471
1472         length = __MmsDecodeValueLength(pFile, (UINT32*)&valueLength, totalLength);
1473         if (length <= 0) {
1474                 /*
1475                  * Constrained-media or Single part message
1476                  * Constrained-media = Constrained-encoding = Extension-Media | Short-integer
1477                  * Extension-media   = *TEXT End-of-string
1478                  * Short-integer     = OCTET(1xxx xxxx)
1479                  */
1480
1481                 if (_MmsBinaryDecodeGetOneByte(pFile, &oneByte, totalLength) == false) {
1482                         MSG_DEBUG("MmsBinaryDecodeContentType: Constrained-media _MmsBinaryDecodeGetOneByte fail\n");
1483                         goto __CATCH;
1484                 }
1485
1486                 if (oneByte > 0x7F) {
1487                         /* Short-integer */
1488                         pMsgType->type = _MmsGetBinaryType(MmsCodeContentType, (UINT16)(oneByte & 0x7F));
1489                         length = 1;
1490                 } else {
1491                         char *pszTemp = NULL;
1492
1493                         /* Extension-Media */
1494                         gCurMmsDecodeBuffPos--;
1495
1496                         textLength = 0;
1497                         szTypeString = __MmsBinaryDecodeText2(pFile, totalLength, &textLength);
1498
1499                         if (szTypeString && (strchr(szTypeString, ';')) != NULL) {
1500                                 pszTemp = _MsgGetStringUntilDelimiter(szTypeString, ';');
1501                                 if (pszTemp) {
1502                                         free(szTypeString);
1503                                         szTypeString = pszTemp;
1504                                 }
1505                         }
1506
1507                         pMsgType->type = _MmsGetTextType(MmsCodeContentType, szTypeString);
1508
1509                         length = textLength;
1510
1511                         if (szTypeString) {
1512                                 free(szTypeString);
1513                                 szTypeString = NULL;
1514                         }
1515                 }
1516         } else {
1517                 /*
1518                  * Content-general-form = Value-length Media-type
1519                  * Media-type                   = (Well-known-media | Extension-Media)*(Parameter)
1520                  */
1521
1522                 length += valueLength;
1523
1524                 if (_MmsBinaryDecodeGetOneByte(pFile, &oneByte, totalLength) == false) {
1525                         MSG_DEBUG("MmsBinaryDecodeContentType: Well-known-media _MmsBinaryDecodeGetOneByte fail\n");
1526                         goto __CATCH;
1527                 }
1528
1529                 if (oneByte > 0x7F) {
1530                         /* Well-known-media */
1531                         pMsgType->type = _MmsGetBinaryType(MmsCodeContentType, (UINT16)(oneByte & 0x7F));
1532                         valueLength--;
1533                 } else {
1534                         /* Extension-Media */
1535                         gCurMmsDecodeBuffPos--;
1536
1537                         textLength = 0;
1538                         szTypeString = __MmsBinaryDecodeText2(pFile, totalLength, &textLength);
1539                         pMsgType->type = _MmsGetTextType(MmsCodeContentType, szTypeString);
1540                         valueLength -= textLength;
1541
1542                         if (szTypeString) {
1543                                 free(szTypeString);
1544                                 szTypeString = NULL;
1545                         }
1546                 }
1547
1548                 MSG_DEBUG("MmsBinaryDecodeContentType: content type=%s\n", MmsDebugGetMimeType((MimeType)pMsgType->type));
1549
1550
1551                 if (MmsBinaryDecodeParameter(pFile, pMsgType, valueLength, totalLength) == false) {
1552                         MSG_DEBUG("MmsBinaryDecodeContentType: Content-Type parameter fail\n");
1553                         goto __CATCH;
1554                 }
1555         }
1556
1557         return length;
1558
1559 __CATCH:
1560         return -1;
1561 }
1562
1563
1564 bool MmsBinaryDecodePartHeader(FILE *pFile, MsgType *pMsgType, int headerLen, int totalLength)
1565 {
1566         UINT8 fieldCode = 0xff;
1567         int length = 0;
1568         UINT32 valueLength = 0;
1569         char *pCode = NULL;
1570         char *pValue = NULL;
1571         char *pParam = NULL;
1572         char ch = '\0';
1573         UINT8 oneByte = 0;
1574         int     textLength = 0;
1575         int tmpInt = 0;
1576         int tmpIntLen = 0;
1577         char *pLatinBuff = NULL;
1578         char *szSrc = NULL;
1579         char *szTemp = NULL;
1580
1581
1582         if (pFile == NULL || pMsgType == NULL)
1583                 return false;
1584
1585         /*
1586          * Message-header                         = Well-known-header | Application-header
1587          * Well-known-header              = Well-known-field-name Wap-value
1588          * Application-header             = Token-text Application-specific-value
1589          * Well-known-field-name          = Short-integer
1590          * Application-specific-value = Text-string
1591          */
1592
1593         while (headerLen > 0) {
1594                 if (_MmsBinaryDecodeGetOneByte(pFile, &oneByte, totalLength) == false) {
1595                         MSG_DEBUG("MmsBinaryDecodePartHeader: field code GetOneByte fail\n");
1596                         goto __CATCH;
1597                 }
1598
1599                 if (0x80 <= oneByte && oneByte <= 0xC7) {
1600                         /* Well-known-header = Well-known-field-name Wap-value (0x00 ~ 0x47) */
1601
1602                         if (__MmsBinaryDecodeCheckAndDecreaseLength(&headerLen, 1) == false)
1603                                 goto __RETURN;
1604
1605                         fieldCode = oneByte & 0x7f;
1606
1607                         switch (fieldCode) {
1608                         case 0x0E:      //Content-Location
1609                         case 0x04:      //Content-Location
1610
1611                                 pLatinBuff = (char *)malloc(MMS_CONTENT_ID_LEN + 1);
1612                                 if (pLatinBuff == NULL)
1613                                         goto __CATCH;
1614
1615                                 length = __MmsBinaryDecodeText(pFile, pLatinBuff, MMS_CONTENT_ID_LEN + 1, totalLength);
1616                                 if (length == -1) {
1617                                         MSG_DEBUG("MmsBinaryDecodePartHeader : __MmsBinaryDecodeQuotedString fail.\n");
1618                                         goto __CATCH;
1619                                 }
1620
1621                                 szSrc = MsgChangeHexString(pLatinBuff);
1622
1623                                 if (szSrc) {
1624                                         strcpy(pLatinBuff, szSrc);
1625                                         free(szSrc);
1626                                         szSrc = NULL;
1627                                 }
1628
1629                                 textLength = strlen(pLatinBuff);
1630
1631                                 if (MsgLatin2UTF ((unsigned char*)pMsgType->szContentLocation, MMS_CONTENT_ID_LEN + 1, (unsigned char*)pLatinBuff, textLength) < 0) {
1632                                         MSG_DEBUG("MmsBinaryDecodePartHeader: MsgLatin2UTF fail \n");
1633                                         goto __CATCH;
1634                                 }
1635
1636                                 free(pLatinBuff);
1637                                 pLatinBuff = NULL;
1638
1639                                 if (__MmsBinaryDecodeCheckAndDecreaseLength(&headerLen, length) == false)
1640                                         goto __RETURN;
1641
1642                                 break;
1643
1644                         case 0x40:      // Content-ID
1645
1646                                 pLatinBuff = (char *)malloc(MMS_CONTENT_ID_LEN + 1);
1647                                 if (pLatinBuff == NULL)
1648                                         goto __CATCH;
1649
1650                                 length = __MmsBinaryDecodeQuotedString(pFile, pLatinBuff, MMS_CONTENT_ID_LEN + 1, totalLength);
1651
1652                                 if (length == -1) {
1653                                         MSG_DEBUG("MmsBinaryDecodePartHeader : Content-ID __MmsBinaryDecodeQuotedString fail.\n");
1654                                         goto __CATCH;
1655                                 }
1656
1657                                 szSrc = MsgChangeHexString(pLatinBuff);
1658
1659                                 if (szSrc) {
1660                                         strcpy(pLatinBuff, szSrc);
1661                                         free(szSrc);
1662                                         szSrc = NULL;
1663                                 }
1664
1665                                 textLength = strlen(pLatinBuff);
1666                                 if (MsgLatin2UTF ((unsigned char*)pMsgType->szContentID, MMS_CONTENT_ID_LEN + 1, (unsigned char*)pLatinBuff, textLength) < 0) {
1667                                         MSG_DEBUG("MmsBinaryDecodePartHeader: MsgLatin2UTF fail \n");
1668                                         goto __CATCH;
1669                                 }
1670                                 free(pLatinBuff);
1671                                 pLatinBuff = NULL;
1672
1673                                 if (__MmsBinaryDecodeCheckAndDecreaseLength(&headerLen, length) == false)
1674                                         goto __RETURN;
1675
1676                                 break;
1677
1678                         case 0x2E:      // Content-Disposition
1679                         case 0x45:      // Content-Disposition
1680
1681                                 /*
1682                                  * Content-disposition-value = Value-length Disposition *(Parameter)
1683                                  * Disposition = Form-data | Attachment | Inline | Token-text
1684                                  *                               Form-data = <Octet 128> : 0x80
1685                                  *                               Attachment = <Octet 129> : 0x81
1686                                  *                               Inline = <Octet 130> : 0x82
1687                                  */
1688
1689                                 length = __MmsDecodeValueLength2(pFile, &valueLength, totalLength);
1690
1691                                 if (length > 0) {
1692                                         if (__MmsBinaryDecodeCheckAndDecreaseLength(&headerLen, length) == false)
1693                                                 goto __RETURN;
1694
1695                                 }
1696
1697                                 if (_MmsBinaryDecodeGetOneByte(pFile, &oneByte, totalLength) == false) {
1698                                         MSG_DEBUG("MmsBinaryDecodePartHeader: Disposition value GetOneByte fail\n");
1699                                         goto __CATCH;
1700                                 }
1701
1702                                 if (length > 0)
1703                                         valueLength--;
1704
1705                                 if (oneByte >= 0x80) {
1706                                         pMsgType->disposition = _MmsGetBinaryType(MmsCodeMsgDisposition, (UINT16)(oneByte & 0x7F));
1707
1708                                         if (pMsgType->disposition == INVALID_HOBJ) {
1709                                                 MSG_DEBUG("MmsBinaryDecodePartHeader : Content-Disposition _MmsGetBinaryType fail.\n");
1710                                                 pMsgType->disposition = MSG_DISPOSITION_ATTACHMENT;             // default
1711                                         }
1712
1713                                         if (__MmsBinaryDecodeCheckAndDecreaseLength(&headerLen, 1) == false)
1714                                                 goto __RETURN;
1715
1716                                         if (MmsBinaryDecodeParameter(pFile, pMsgType, valueLength, totalLength) == false) {
1717                                                 MSG_DEBUG("MmsBinaryDecodePartHeader: Disposition parameter fail\n");
1718                                                 goto __CATCH;
1719                                         }
1720
1721                                         if (__MmsBinaryDecodeCheckAndDecreaseLength(&headerLen, valueLength) == false)
1722                                                 goto __RETURN;
1723                                 } else {
1724
1725                                         gCurMmsDecodeBuffPos--;
1726                                         valueLength++;
1727
1728                                         pLatinBuff = (char *)malloc(MSG_FILENAME_LEN_MAX);
1729                                         memset(pLatinBuff, 0, MSG_FILENAME_LEN_MAX);
1730
1731                                         textLength = __MmsBinaryDecodeText(pFile, pLatinBuff, MSG_FILENAME_LEN_MAX-1, totalLength);
1732
1733
1734                                         if (textLength < 0) {
1735                                                 MSG_DEBUG("MmsBinaryDecodePartHeader : Content-Disposition decodingfail. \n");
1736                                                 goto __CATCH;
1737                                         }
1738                                         free(pLatinBuff);
1739                                         pLatinBuff = NULL;
1740
1741                                         if (__MmsBinaryDecodeCheckAndDecreaseLength(&headerLen, textLength) == false)
1742                                                 goto __RETURN;
1743
1744                                         valueLength -= textLength;
1745
1746                                         if (MmsBinaryDecodeParameter(pFile, pMsgType, valueLength, totalLength) == false)
1747                                         {
1748                                                 MSG_DEBUG("MmsBinaryDecodePartHeader: Disposition parameter fail\n");
1749                                                 goto __CATCH;
1750                                         }
1751
1752                                         if (__MmsBinaryDecodeCheckAndDecreaseLength(&headerLen, valueLength) == false)
1753                                                 goto __RETURN;
1754
1755                                 }
1756
1757                                 break;
1758
1759                         case 0x0B:      //Content-Encoding
1760
1761                                 if (_MmsBinaryDecodeGetOneByte(pFile, &oneByte, totalLength) == false) {
1762                                         MSG_DEBUG("MmsBinaryDecodePartHeader: Disposition value GetOneByte fail\n");
1763                                         goto __CATCH;
1764                                 }
1765
1766                                 if (__MmsBinaryDecodeCheckAndDecreaseLength(&headerLen, 1) == false)
1767                                         goto __RETURN;
1768
1769                                 break;
1770
1771                         case 0x0C:      //Content-Language
1772
1773                                 if (__MmsBinaryDecodeInteger(pFile, (UINT32*)&tmpInt, &tmpIntLen, totalLength) == true) {
1774                                         if (__MmsBinaryDecodeCheckAndDecreaseLength(&headerLen, tmpIntLen) == false)
1775                                                 goto __RETURN;
1776                                 } else {
1777                                         char* cTemp = NULL;
1778
1779                                         cTemp = __MmsBinaryDecodeText2(pFile, totalLength, &textLength);
1780
1781                                         if (cTemp == NULL) {
1782                                                 MSG_DEBUG("MmsBinaryDecodePartHeader : __MmsBinaryDecodeText2 fail...\n");
1783                                                 goto __CATCH;
1784                                         }
1785
1786                                         if (__MmsBinaryDecodeCheckAndDecreaseLength(&headerLen, textLength) == false) {
1787                                                 if (cTemp) {
1788                                                         free(cTemp);
1789                                                 }
1790                                                 goto __RETURN;
1791                                         }
1792
1793                                         if (cTemp)
1794                                                 free(cTemp);
1795                                 }
1796
1797                                 break;
1798
1799                         case 0x0D:      //Content-Length
1800
1801                                 if (__MmsBinaryDecodeInteger(pFile, (UINT32*)&tmpInt, &tmpIntLen, totalLength) == false) {
1802                                         MSG_DEBUG("MmsBinaryDecodePartHeader : __MmsBinaryDecodeInteger fail...\n");
1803                                         goto __CATCH;
1804                                 }
1805
1806                                 if (__MmsBinaryDecodeCheckAndDecreaseLength(&headerLen, tmpIntLen) == false)
1807                                         goto __RETURN;
1808
1809                                 break;
1810
1811                         case 0x30:      //X-Wap-Content-URI skip this value
1812
1813                                 MSG_DEBUG("MmsBinaryDecodePartHeader : X-Wap-Content-URI header.\n");
1814                                 pLatinBuff = (char *)malloc(MMS_TEXT_LEN);
1815                                 if (pLatinBuff == NULL)
1816                                         goto __CATCH;
1817
1818                                 length = __MmsBinaryDecodeText(pFile, pLatinBuff, MMS_TEXT_LEN, totalLength);
1819
1820                                 if (length == -1) {
1821                                         MSG_DEBUG("MmsBinaryDecodePartHeader : __MmsBinaryDecodeQuotedString fail.\n");
1822                                         goto __CATCH;
1823                                 }
1824
1825                                 MSG_DEBUG("MmsBinaryDecodePartHeader : X-Wap-Content-URI header decoded. Value length %d\n", length);
1826                                 free(pLatinBuff);
1827                                 pLatinBuff = NULL;
1828
1829                                 if (__MmsBinaryDecodeCheckAndDecreaseLength(&headerLen, length) == false)
1830                                         goto __RETURN;
1831
1832                                 MSG_DEBUG("MmsBinaryDecodePartHeader : X-Wap-Content-URI header skipped.\n");
1833
1834                                 break;
1835
1836                         case 0x01:      // Accept-charset
1837                                 //if (NvGetInt(NV_SI_ADM_GCF_STATE) == 1)
1838                                 {
1839                                         /*      WAP-230-WSP-200010705-a.pdf
1840                                                 8.4.2.8 Accept charset field
1841                                                 The following rules are used to encode accept character set values.
1842                                                 Accept-charset-value = Constrained-charset | Accept-charset-general-form
1843                                                 Accept-charset-general-form = Value-length (Well-known-charset | Token-text) [Q-value]
1844                                                 Constrained-charset = Any-charset | Constrained-encoding
1845                                                 Well-known-charset = Any-charset | Integer-value
1846                                                 ; Both are encoded using values from Character Set Assignments table in Assigned Numbers
1847                                                 Any-charset = <Octet 128>
1848                                                 ; Equivalent to the special RFC2616 charset value ï¿½ï¿½*��
1849                                         */
1850
1851                                         int     charset = 0;
1852                                         int charSetLen = 0;
1853
1854                                         MSG_DEBUG("MmsBinaryDecodePartHeader : Accept-charset. \n");
1855
1856                                         length = __MmsDecodeValueLength(pFile, &valueLength, totalLength);
1857                                         if (length > 0) {
1858                                                 if (__MmsBinaryDecodeCheckAndDecreaseLength(&headerLen, length) == false)
1859                                                         goto __RETURN;
1860
1861                                         }
1862
1863                                         if (__MmsBinaryDecodeInteger(pFile, (UINT32*)&charset, &charSetLen, totalLength) == false) {
1864                                                 // We only support the well-known-charset format
1865                                                 MSG_DEBUG("MmsBinaryDecodePartHeader : __MmsBinaryDecodeInteger fail...\n");
1866                                                 goto __CATCH;
1867                                         }
1868
1869                                         if (charset > 0)
1870                                                 _MmsGetBinaryType(MmsCodeCharSet, (UINT16)charset);
1871
1872                                         if (__MmsBinaryDecodeCheckAndDecreaseLength(&headerLen, charSetLen) == false)
1873                                                 goto __RETURN;
1874
1875                                         break;
1876                                 }
1877
1878                         default:
1879
1880                                 /* Other Content-xxx headers : Have valueLength */
1881
1882                                 MSG_DEBUG("MmsBinaryDecodePartHeader : unknown Value = 0x%x\n", oneByte);
1883
1884                                 length = __MmsDecodeValueLength(pFile, &valueLength, totalLength);
1885                                 if (length <= 0) {
1886                                         MSG_DEBUG("MmsBinaryDecodePartHeader : 1. invalid MMS_CODE_PREVIOUSLYSENTDATE \n");
1887                                         goto __CATCH;
1888                                 }
1889
1890                                 if (__MmsBinaryDecodeCheckAndDecreaseLength(&headerLen, length) == false)
1891                                         goto __RETURN;
1892
1893                                 szTemp = (char *)malloc(valueLength);
1894                                 if (szTemp == NULL)
1895                                         goto __CATCH;
1896
1897                                 if (_MmsBinaryDecodeGetBytes(pFile, szTemp, valueLength, totalLength) == false) {
1898                                         MSG_DEBUG("MmsBinaryDecodePartHeader : default _MmsBinaryDecodeGetBytes() fail\n");
1899                                         if (szTemp) {
1900                                                 free(szTemp);
1901                                                 szTemp = NULL;
1902                                         }
1903                                         goto __CATCH;
1904                                 }
1905
1906                                 if (__MmsBinaryDecodeCheckAndDecreaseLength(&headerLen, valueLength) == false)
1907                                         goto __RETURN;
1908
1909                                 break;
1910                         }
1911                 } else {
1912                         /*
1913                          * Application-header  = Token-text Application-specific-value
1914                          * Application-specific-value = Text-string
1915                          */
1916
1917                         MSG_DEBUG(" MmsBinaryDecodePartHeader: Application-header = Token-text Application-specific-value \n");
1918
1919                         gCurMmsDecodeBuffPos--;
1920
1921                         /* Token-text */
1922
1923                         textLength = 0;
1924                         pCode = __MmsBinaryDecodeText2(pFile, totalLength, &textLength);
1925                         if (pCode == NULL) {
1926                                 MSG_DEBUG("MmsBinaryDecodePartHeader: pCode is null\n");
1927                                 goto __CATCH;
1928                         }
1929
1930                         if (__MmsBinaryDecodeCheckAndDecreaseLength(&headerLen, textLength) == false)
1931                                 goto __RETURN;
1932
1933                         MSG_DEBUG(" MmsBinaryDecodePartHeader: Token-text (%s) \n", pCode);
1934
1935
1936                         /* Application-specific-value */
1937
1938                         textLength = 0;
1939                         pValue = __MmsBinaryDecodeText2(pFile, totalLength, &textLength);
1940                         if (pValue == NULL) {
1941                                 MSG_DEBUG("MmsBinaryDecodePartHeader: pValue is null\n");
1942                                 goto __CATCH;
1943                         }
1944
1945                         MSG_DEBUG(" MmsBinaryDecodePartHeader: Application-specific-value (%s) \n", pValue);
1946
1947
1948                         pParam = strchr(pValue, MSG_CH_ADDR_DELIMETER);
1949                         if (pParam) {
1950                                 ch = *pParam;
1951                                 *pParam = '\0';
1952                         }
1953
1954                         switch (_MmsGetTextType(MmsCodeMsgBodyHeaderCode, pCode)) {
1955                         case MMS_BODYHDR_TRANSFERENCODING:              // Content-Transfer-Encoding
1956                                 pMsgType->encoding = _MmsGetTextType(MmsCodeContentTransferEncoding, pValue);
1957                                 break;
1958
1959                         case MMS_BODYHDR_CONTENTID:                             // Content-ID
1960
1961                                 pLatinBuff = (char *)malloc(MMS_CONTENT_ID_LEN + 1);
1962                                 if (pLatinBuff == NULL)
1963                                 {
1964                                         goto __CATCH;
1965                                 }
1966
1967                                 MsgMIMERemoveQuote (pValue);
1968                                 strncpy(pLatinBuff, pValue, MMS_MSG_ID_LEN);
1969
1970                                 length = strlen(pLatinBuff);
1971                                 if (MsgLatin2UTF ((unsigned char*)pMsgType->szContentID, MMS_CONTENT_ID_LEN + 1, (unsigned char*)pLatinBuff, length) < 0)
1972                                 {
1973                                         MSG_DEBUG("MmsBinaryDecodePartHeader: MsgLatin2UTF fail \n");
1974                                         goto __CATCH;
1975                                 }
1976
1977                                 free(pLatinBuff);
1978                                 pLatinBuff = NULL;
1979                                 break;
1980
1981                         case MMS_BODYHDR_CONTENTLOCATION:               // Content-Location
1982
1983                                 pLatinBuff = (char *)malloc(MMS_CONTENT_ID_LEN + 1);
1984                                 if (pLatinBuff == NULL)
1985                                         goto __CATCH;
1986
1987                                 strncpy(pLatinBuff, pValue, MMS_MSG_ID_LEN);
1988
1989                                 length = strlen(pLatinBuff);
1990                                 if (MsgLatin2UTF ((unsigned char*)pMsgType->szContentLocation, MMS_CONTENT_ID_LEN + 1, (unsigned char*)pLatinBuff, length) < 0) {
1991                                         MSG_DEBUG("MmsBinaryDecodePartHeader: MsgLatin2UTF fail \n");
1992                                         goto __CATCH;
1993                                 }
1994
1995                                 free(pLatinBuff);
1996                                 pLatinBuff = NULL;
1997                                 break;
1998
1999                         case MMS_BODYHDR_DISPOSITION:                   // Content-Disposition
2000                                 pMsgType->disposition = _MmsGetTextType(MmsCodeMsgDisposition, pValue);
2001                                 break;
2002
2003                         case MMS_BODYHDR_X_OMA_DRM_SEPARATE_DELIVERY:   // DRM RO WAITING
2004                                         break;
2005
2006                         default:
2007                                 MSG_DEBUG("Unknown Field : %s, Value: %s\n", pCode, pValue);
2008                                 break;
2009                         }
2010
2011                         if (pParam) {
2012                                 _MsgParseParameter(pMsgType, pParam + 1);
2013                                 *pParam = ch;
2014                         }
2015                         if (pCode) {
2016                                 free(pCode);
2017                                 pCode = NULL;
2018                         }
2019                         if (pValue) {
2020                                 free(pValue);
2021                                 pValue = NULL;
2022                         }
2023
2024                         if (__MmsBinaryDecodeCheckAndDecreaseLength(&headerLen, textLength) == false)
2025                                 goto __RETURN;
2026
2027                 }
2028         }       //while
2029
2030 __RETURN:
2031
2032         if (pLatinBuff) {
2033                 free(pLatinBuff);
2034                 pLatinBuff = NULL;
2035         }
2036
2037         if (szTemp) {
2038                 free(szTemp);
2039                 szTemp = NULL;
2040         }
2041
2042         if (pCode) {
2043                 free(pCode);
2044                 pCode = NULL;
2045         }
2046
2047         return true;
2048
2049 __CATCH:
2050
2051         if (pLatinBuff) {
2052                 free(pLatinBuff);
2053                 pLatinBuff = NULL;
2054         }
2055         if (pCode) {
2056                 free(pCode);
2057                 pCode = NULL;
2058         }
2059         if (pValue) {
2060                 free(pValue);
2061                 pValue = NULL;
2062         }
2063
2064         if (szTemp) {
2065                 free(szTemp);
2066                 szTemp = NULL;
2067         }
2068
2069         return false;
2070 }
2071
2072
2073 bool MmsBinaryDecodeEntries(FILE *pFile, UINT32 *npEntries, int totalLength)
2074 {
2075         int length = 0;
2076
2077         length = __MmsBinaryDecodeUintvar(pFile, npEntries, totalLength);
2078         if (length <= 0) {
2079                 goto __CATCH;
2080         }
2081
2082         MSG_DEBUG("MmsBinaryDecodeEntries: Number of Entries = %d\n", *npEntries);
2083
2084         return true;
2085
2086 __CATCH:
2087         return false;
2088 }
2089
2090
2091
2092 bool MmsBinaryDecodePartBody(FILE *pFile, UINT32 bodyLength, int totalLength)
2093 {
2094         int offset = 0;
2095
2096         /*
2097          * Currently, offset and size is
2098          * the only information used with msgBody.
2099          * If you need, add here more information
2100          */
2101
2102         MSG_DEBUG("MmsBinaryDecodePartBody: \n");
2103
2104         offset = _MmsGetDecodeOffset();
2105         offset += bodyLength;
2106
2107         if (MsgFseek(pFile, offset, SEEK_SET) < 0) {
2108                 MSG_DEBUG("MmsBinaryDecodePartBody: fail to seek file pointer \n");
2109                 goto __CATCH;
2110         }
2111
2112         _MmsCleanDecodeBuff();
2113
2114         gMmsDecodeCurOffset = offset;
2115
2116         if (offset >= totalLength)
2117                 goto __RETURN;
2118
2119         if (MsgLoadDataToDecodeBuffer(pFile, &gpCurMmsDecodeBuff, &gCurMmsDecodeBuffPos, &gMmsDecodeCurOffset,
2120                                                                    gpMmsDecodeBuf1, gpMmsDecodeBuf2, gMmsDecodeMaxLen, &gMmsDecodeBufLen, totalLength) == false) {
2121                 MSG_DEBUG("_MmsBinaryDecodeGetOneByte: fail to load to buffer \n");
2122                 goto __CATCH;
2123         }
2124
2125         return true;
2126
2127 __RETURN:
2128         return true;
2129
2130 __CATCH:
2131         return false;
2132 }
2133
2134
2135 static bool MmsBinaryDecodeMovePointer(FILE *pFile, int offset, int totalLength)
2136 {
2137         if (offset > totalLength)
2138                 goto __RETURN;
2139
2140         if (MsgFseek(pFile, offset, SEEK_SET) < 0) {
2141                 MSG_DEBUG("MmsBinaryDecodeMovePointer: fail to seek file pointer \n");
2142                 goto __CATCH;
2143         }
2144
2145         _MmsCleanDecodeBuff();
2146
2147         gMmsDecodeCurOffset = offset;
2148
2149         if (offset == totalLength)
2150                 goto __RETURN;
2151
2152         if (MsgLoadDataToDecodeBuffer(pFile, &gpCurMmsDecodeBuff, &gCurMmsDecodeBuffPos, &gMmsDecodeCurOffset,
2153                                                                         gpMmsDecodeBuf1, gpMmsDecodeBuf2, gMmsDecodeMaxLen, &gMmsDecodeBufLen, totalLength) == false) {
2154                 MSG_DEBUG("MmsBinaryDecodeMovePointer: fail to load to buffer \n");
2155                 goto __CATCH;
2156         }
2157
2158 __RETURN:
2159         return true;
2160
2161 __CATCH:
2162         return false;
2163 }
2164
2165
2166 bool MmsBinaryIsTextEncodedPart(FILE *pFile, int totalLength)
2167 {
2168         UINT8 oneByte   = 0;
2169         int byteCount = 0;
2170
2171         byteCount++;                            //check 0x0D
2172
2173         if (_MmsBinaryDecodeGetOneByte(pFile, &oneByte, totalLength) == false)
2174                 goto __CATCH;
2175
2176         if (oneByte != 0x0D) {
2177                 //it can be started "--" without 0x0D 0x0A
2178                 if (oneByte != 0x2D) {
2179                         goto __CATCH;
2180                 } else {
2181                         byteCount++;            // check 0x2D ('-')
2182                         if (_MmsBinaryDecodeGetOneByte(pFile, &oneByte, totalLength) == false)
2183                                 goto __CATCH;
2184
2185                         if (oneByte != 0x2D) {
2186                                 goto __CATCH;
2187                         } else {
2188                                 goto __RETURN;
2189                         }
2190                 }
2191         } else {
2192                 byteCount++;                    //check 0x0A
2193                 if (_MmsBinaryDecodeGetOneByte(pFile, &oneByte, totalLength) == false)
2194                         goto __CATCH;
2195
2196                 if (oneByte != 0x0A) {
2197                         goto __CATCH;
2198                 } else {
2199                         byteCount++;            // check 0x2D ('-')
2200                         if (_MmsBinaryDecodeGetOneByte(pFile, &oneByte, totalLength) == false)
2201                                 goto __CATCH;
2202
2203                         if (oneByte != 0x2D) {
2204                                 goto __CATCH;
2205                         } else {
2206                                 byteCount++;    // check 0x2D ('-')
2207                                 if (_MmsBinaryDecodeGetOneByte(pFile, &oneByte, totalLength) == false)
2208                                         goto __CATCH;
2209
2210                                 if (oneByte != 0x2D) {
2211                                         goto __CATCH;
2212                                 } else {
2213                                         goto __RETURN;
2214                                 }
2215                         }
2216                 }
2217         }
2218
2219 __RETURN:
2220         gCurMmsDecodeBuffPos -= byteCount;
2221         return true;
2222
2223 __CATCH:
2224         gCurMmsDecodeBuffPos -= byteCount;
2225         return false;
2226 }
2227
2228
2229 bool MmsBinaryDecodeMultipart(FILE *pFile, char *szFilePath, MsgType *pMsgType, MsgBody *pMsgBody, int totalLength)
2230 {
2231         UINT32 nEntries = 0;
2232         MsgMultipart *pMultipart = NULL;
2233         MsgMultipart *pLastMultipart = NULL;
2234         MsgMultipart *pPreMultipart     = NULL;
2235         int offset = 0;
2236         int     index = 0;
2237
2238         MsgPresentationFactor factor = MSG_PRESENTATION_NONE;
2239         MsgPresentaionInfo presentationInfo;
2240
2241         MSG_DEBUG("MmsBinaryDecodeMultipart: total length=%d\n", totalLength);
2242
2243         presentationInfo.factor = MSG_PRESENTATION_NONE;
2244         presentationInfo.pCurPresentation = NULL;
2245         presentationInfo.pPrevPart = NULL;
2246
2247         if (MmsBinaryDecodeEntries(pFile, &nEntries, totalLength) == false) {
2248                 MSG_DEBUG("MmsBinaryDecodeMultipart: MmsBinaryDecodeEntries is fail.\n");
2249                 goto __CATCH;
2250         }
2251
2252         while (nEntries) {
2253                 MSG_DEBUG("MmsBinaryDecodeMultipart: decoding %dth multipart\n", index);
2254
2255                 offset = _MmsGetDecodeOffset();
2256                 if (offset >= totalLength)
2257                         goto __RETURN;
2258
2259                 if ((pMultipart = MsgAllocMultipart()) == NULL) {
2260                         MSG_DEBUG("MmsBinaryDecodeMultipart: MsgAllocMultipart Fail \n");
2261                         goto __CATCH;
2262                 }
2263
2264                 if (MmsBinaryDecodeEachPart(pFile, szFilePath, &(pMultipart->type), pMultipart->pBody, totalLength) == false) {
2265                         MSG_DEBUG("MmsBinaryDecodeMultipart: MmsBinaryDecodeEachPart is fail.(nEntries = %d)\n", nEntries);
2266                         goto __CATCH;
2267                 }
2268
2269                 if (pMsgType->param.type == MIME_APPLICATION_SMIL) {
2270                         factor = MsgIsPresentationEx(&(pMultipart->type), pMsgType->param.szStart, (MimeType)pMsgType->param.type);
2271                 } else {
2272                         factor = MSG_PRESENTATION_NONE;
2273                 }
2274                 // priority 1 : content type match, 2: content location, 3: type
2275                 if (presentationInfo.factor < factor) {
2276                         // Presentation part
2277                         presentationInfo.factor = factor;
2278                         presentationInfo.pPrevPart = pPreMultipart;
2279                         presentationInfo.pCurPresentation = pMultipart;
2280                 }
2281
2282                 if (pMsgBody->body.pMultipart == NULL) {
2283                         /* first multipart */
2284                         pMsgBody->body.pMultipart = pMultipart;
2285                         pLastMultipart                    = pMultipart;
2286                         pPreMultipart                     = NULL;
2287                 } else if (pLastMultipart != NULL) {
2288                         pPreMultipart = pLastMultipart;
2289
2290                         pLastMultipart->pNext   = pMultipart;
2291                         pLastMultipart                  = pMultipart;
2292                         pPreMultipart = pMultipart;
2293                 }
2294
2295                 pMsgType->contentSize += pMultipart->pBody->size;
2296
2297                 nEntries--;
2298
2299                 MmsDebugPrintMulitpartEntry(pMultipart, index++);
2300
2301         }
2302
2303         pMsgBody->size = totalLength - pMsgBody->offset;
2304
2305 #ifdef  __SUPPORT_DRM__
2306         if (MmsDrm2GetConvertState() != MMS_DRM2_CONVERT_REQUIRED)
2307 #endif
2308                 MsgConfirmPresentationPart(pMsgType, pMsgBody, &presentationInfo);
2309
2310         if (MsgResolveNestedMultipart(pMsgType, pMsgBody) == false) {
2311                 MSG_DEBUG("MmsBinaryDecodeMultipart : MsgResolveNestedMultipart failed \n");
2312                 goto __CATCH;
2313         }
2314
2315 __RETURN:
2316         return true;
2317
2318 __CATCH:
2319         return false;
2320 }
2321
2322
2323 bool MmsBinaryDecodeEachPart(FILE *pFile, char *szFilePath, MsgType *pMsgType, MsgBody *pMsgBody, int totalLength)
2324 {
2325         int     length = 0;
2326         bool bSuccess = false;
2327         UINT32 headerLength = 0;
2328         UINT32 bodyLength = 0;
2329         int offset = 0;
2330
2331         MSG_DEBUG("MmsBinaryDecodeEachPart: total length=%d\n", totalLength);
2332
2333         /* header length */
2334
2335         if (__MmsBinaryDecodeUintvar(pFile, &headerLength, totalLength) <= 0) {
2336                 MSG_DEBUG("MmsBinaryDecodeEachPart: Get header length fail \n");
2337                 goto __CATCH;
2338         }
2339
2340         offset = _MmsGetDecodeOffset();
2341         if (offset >= totalLength)
2342                 goto __RETURN;
2343
2344
2345         /* body length */
2346
2347         if (__MmsBinaryDecodeUintvar(pFile, &bodyLength, totalLength) <= 0) {
2348                 MSG_DEBUG("MmsBinaryDecodeEachPart: Get body length fail\n");
2349                 goto __CATCH;
2350         }
2351
2352
2353         offset = _MmsGetDecodeOffset();
2354         if (offset >= totalLength)
2355                 goto __RETURN;
2356
2357
2358         /* Content Type */
2359         if (szFilePath != NULL)
2360                 strncpy(pMsgType->szOrgFilePath, szFilePath, strlen(szFilePath));
2361
2362         pMsgType->offset = _MmsGetDecodeOffset();
2363         pMsgType->size = headerLength;
2364         pMsgType->contentSize = bodyLength;
2365
2366         if (pMsgType->offset > totalLength)
2367                 goto __RETURN;
2368
2369         length = MmsBinaryDecodeContentType(pFile, pMsgType, totalLength);
2370         if (length <= 0) {
2371                 MSG_DEBUG("MmsBinaryDecodeEachPart: Decode contentType Fail \n");
2372                 goto __CATCH;
2373         }
2374
2375         offset = _MmsGetDecodeOffset();
2376         if (offset >= totalLength)
2377                 goto __RETURN;
2378
2379
2380         /* Part Header */
2381
2382         if (MmsBinaryDecodePartHeader(pFile, pMsgType, headerLength - length, totalLength) == false) {
2383                 MSG_DEBUG("MmsBinaryDecodeEachPart: Decode contentHeader Fail \n");
2384                 goto __CATCH;
2385         }
2386
2387         offset = _MmsGetDecodeOffset();
2388         if (offset >= totalLength)
2389                 goto __RETURN;
2390
2391         /* Part Body */
2392
2393         if (szFilePath != NULL)
2394                 strncpy(pMsgBody->szOrgFilePath, szFilePath, strlen(szFilePath));
2395
2396         pMsgBody->offset = _MmsGetDecodeOffset();
2397         pMsgBody->size   = bodyLength;
2398
2399         if (pMsgBody->offset > totalLength)
2400                 goto __RETURN;
2401
2402         switch (pMsgType->type) {
2403         case MIME_APPLICATION_VND_WAP_MULTIPART_MIXED:
2404         case MIME_APPLICATION_VND_WAP_MULTIPART_RELATED:
2405         case MIME_APPLICATION_VND_WAP_MULTIPART_ASTERIC:
2406         case MIME_APPLICATION_VND_WAP_MULTIPART_ALTERNATIVE:
2407         case MIME_MULTIPART_REPORT:
2408         case MIME_MULTIPART_MIXED:
2409         case MIME_MULTIPART_RELATED:
2410         case MIME_MULTIPART_ALTERNATIVE:
2411
2412                 MSG_DEBUG("MmsBinaryDecodeEachPart: Decode multipart\n");
2413
2414                 if (MmsBinaryDecodeMultipart(pFile, szFilePath, pMsgType, pMsgBody, totalLength) == false) {
2415                         MSG_DEBUG("MmsBinaryDecodeEachPart: MmsBinaryDecodeMultipart is fail.\n");
2416                         goto __CATCH;
2417                 }
2418
2419                 offset = _MmsGetDecodeOffset();
2420                 if (offset >= totalLength)
2421                         goto __RETURN;
2422
2423                 break;
2424
2425
2426 #ifdef __SUPPORT_DRM__
2427
2428         case MIME_APPLICATION_VND_OMA_DRM_MESSAGE: /* Contains forwardLock OR combined-delivery media part */
2429                 MSG_DEBUG("MmsBinaryDecodeEachPart: MIME_APPLICATION_VND_OMA_DRM_MESSAGE Part \n");
2430
2431                 if (MmsDrm2GetConvertState() != MMS_DRM2_CONVERT_NOT_FIXED && MmsDrm2GetConvertState() != MMS_DRM2_CONVERT_REQUIRED) {
2432
2433                         if (MmsBinaryDecodeDRMContent(pFile, szFilePath, pMsgType, pMsgBody, bodyLength, totalLength) == false)
2434                                 goto __CATCH;
2435                 } else {
2436                         MmsDrm2SetConvertState(MMS_DRM2_CONVERT_REQUIRED);
2437
2438                         bSuccess = MmsBinaryDecodePartBody(pFile, bodyLength, totalLength);
2439                         if (bSuccess == false) {
2440                                 MSG_DEBUG("MmsBinaryDecodeEachPart: Decode contentBody Fail \n");
2441                                 goto __CATCH;
2442                         }
2443                 }
2444                 offset = _MmsGetDecodeOffset();
2445                 if (offset >= totalLength)
2446                         goto __RETURN;
2447
2448                 break;
2449
2450         case MIME_APPLICATION_VND_OMA_DRM_CONTENT: /* Contains seperate-delivery media part (DCF) */
2451
2452                 MSG_DEBUG("MmsBinaryDecodeEachPart: MIME_APPLICATION_VND_OMA_DRM_CONTENT Part \n");
2453
2454                 if (MmsBinaryDecodeDRMContent(pFile, szFilePath, pMsgType, pMsgBody, bodyLength, totalLength) == false)
2455                         goto __CATCH;
2456
2457                 offset = _MmsGetDecodeOffset();
2458                 if (offset >= totalLength)
2459                         goto __RETURN;
2460
2461                 break;
2462 #endif
2463
2464         default:
2465                 MSG_DEBUG("MmsBinaryDecodeEachPart: Other normal Part \n");
2466
2467                 bSuccess = MmsBinaryDecodePartBody(pFile, bodyLength, totalLength);
2468                 if (bSuccess == false) {
2469                         MSG_DEBUG("MmsBinaryDecodeEachPart: Decode contentBody Fail \n");
2470                         goto __CATCH;
2471                 }
2472
2473                 offset = _MmsGetDecodeOffset();
2474                 if (offset >= totalLength)
2475                         goto __RETURN;
2476
2477                 break;
2478         }
2479
2480         return true;
2481
2482 __RETURN:
2483
2484         return true;
2485
2486 __CATCH:
2487
2488         return false;
2489 }
2490
2491 #ifdef __SUPPORT_DRM__
2492 bool __MmsParseDCFInfo(FILE *pFile, MsgDRMInfo *pDrmInfo, int totalLength)
2493 {
2494         UINT8 version = 0;
2495         UINT8 contentTypeLen = 0;
2496         UINT8 contentURILen = 0;
2497         char *szContentType     = NULL;
2498         char *szContentURI = NULL;
2499
2500         if (_MmsBinaryDecodeGetOneByte(pFile, &version, totalLength) == false) {
2501                 MSG_DEBUG("__MmsParseDCFInfo: [version] GetOneByte fail\n");
2502                 goto __CATCH;
2503         }
2504
2505         if (_MmsBinaryDecodeGetOneByte(pFile, &contentTypeLen, totalLength) == false) {
2506                 MSG_DEBUG("__MmsParseDCFInfo: [contentTypeLen] GetOneByte fail\n");
2507                 goto __CATCH;
2508         }
2509
2510         if (_MmsBinaryDecodeGetOneByte(pFile, &contentURILen, totalLength) == false) {
2511                 MSG_DEBUG("__MmsParseDCFInfo: [contentURILen] GetOneByte fail\n");
2512                 goto __CATCH;
2513         }
2514
2515         //Get media content-type (mime-type)
2516         szContentType = (char *)malloc(contentTypeLen + 1);
2517         if (szContentType == NULL)
2518                 goto __CATCH;
2519
2520         memset(szContentType, 0, contentTypeLen + 1);
2521
2522         if (_MmsBinaryDecodeGetBytes(pFile, szContentType, contentTypeLen + 1, totalLength) < 0) {
2523                 MSG_DEBUG("__MmsParseDCFInfo : contentType is invalid\n");
2524                 goto __CATCH;
2525         }
2526         gCurMmsDecodeBuffPos--;
2527         pDrmInfo->contentType = (MsgContentType)_MsgGetCode(MSG_TYPE, szContentType);
2528
2529
2530         //Get content-ID - 1.remover "cid:",   2.resolve "%hexa",   3.and copy the string
2531         szContentURI = (char *)malloc(contentURILen + 1);
2532         if (szContentURI == NULL)
2533                 goto __CATCH;
2534
2535         memset(szContentURI, 0, contentURILen + 1);
2536
2537         if (_MmsBinaryDecodeGetBytes(pFile, szContentURI, contentURILen + 1, totalLength) == false) {
2538                 MSG_DEBUG("__MmsParseDCFInfo : contentType is invalid\n");
2539                 goto __CATCH;
2540         }
2541         gCurMmsDecodeBuffPos--;
2542         pDrmInfo->szContentURI = MsgResolveContentURI(szContentURI);
2543
2544
2545         if (szContentType) {
2546                 free(szContentType);
2547                 szContentType = NULL;
2548         }
2549
2550         if (szContentURI) {
2551                 free(szContentURI);
2552                 szContentURI = NULL;
2553         }
2554         return true;
2555
2556
2557 __CATCH:
2558
2559         if (szContentType) {
2560                 free(szContentType);
2561                 szContentType = NULL;
2562         }
2563
2564         if (szContentURI) {
2565                 free(szContentURI);
2566                 szContentURI = NULL;
2567         }
2568         return false;
2569
2570 }
2571
2572
2573 bool __MmsParseDCFHdr(FILE *pFile, MsgDRMInfo *pDrmInfo, UINT32 headerLen, int totalLength)
2574 {
2575         char *szDCFHdr = NULL;
2576         MsgType partType;
2577         ULONG nRead = 0;
2578         int offset = 0;
2579
2580         /* add to parse DCF header such as,
2581          *  Right-Issuer, Content-Name, and Content-Description.
2582          */
2583
2584         szDCFHdr = (char *)malloc(headerLen + 1);
2585         if (szDCFHdr == NULL) {
2586                 MSG_DEBUG("__MmsParseDCFHdr: szDCFHdr alloc fail\n");
2587                 goto __CATCH;
2588         }
2589         memset(szDCFHdr, 0, headerLen + 1);
2590
2591         offset = _MmsGetDecodeOffset();
2592         if (offset >= totalLength)
2593                 goto __RETURN;
2594
2595         if (MsgFseek(pFile, offset, SEEK_SET) < 0)
2596                 goto __CATCH;
2597
2598         if ((nRead = MsgReadFile(szDCFHdr, sizeof(char), headerLen, pFile)) == 0){
2599                 goto __CATCH;
2600         }
2601         szDCFHdr[nRead] = '\0';
2602
2603         _MsgInitMsgType(&partType);
2604         _MsgParsePartHeader(&partType, szDCFHdr, headerLen);
2605
2606         pDrmInfo->szContentName = partType.drmInfo.szContentName;
2607         pDrmInfo->szContentVendor = partType.drmInfo.szContentVendor;
2608         pDrmInfo->szContentDescription = partType.drmInfo.szContentDescription;
2609         pDrmInfo->szRightIssuer = partType.drmInfo.szRightIssuer;
2610
2611         if (MmsBinaryDecodeMovePointer(pFile, offset + headerLen, totalLength) == false)
2612                 goto __CATCH;
2613
2614 __RETURN:
2615
2616         if (szDCFHdr) {
2617                 free(szDCFHdr);
2618                 szDCFHdr = NULL;
2619         }
2620
2621         return true;
2622
2623 __CATCH:
2624
2625         if (szDCFHdr) {
2626                 free(szDCFHdr);
2627                 szDCFHdr = NULL;
2628         }
2629
2630         return false;
2631 }
2632
2633
2634 bool MmsBinaryDecodeDRMContent(FILE *pFile, char *szFilePath, MsgType *pMsgType, MsgBody *pMsgBody, unsigned int bodyLength, int totalLength)
2635 {
2636         int offset = 0;
2637         char szTempFilePath[MSG_FILEPATH_LEN_MAX] = MSG_DATA_PATH"drm.dcf";
2638         char *pRawData = NULL;
2639         bool isFileCreated = false;
2640
2641         MSG_DEBUG("bodyLength: %d\n", bodyLength);
2642
2643         offset = _MmsGetDecodeOffset();
2644
2645         if (offset >= totalLength)
2646                 goto __RETURN;
2647
2648         if (szFilePath != NULL)
2649                 strncpy(pMsgBody->szOrgFilePath, szFilePath, strlen(szFilePath));
2650         if (szFilePath != NULL)
2651                 strncpy(pMsgType->szOrgFilePath, szFilePath, strlen(szFilePath));
2652
2653         pRawData = (char *)malloc(bodyLength);
2654         if (pRawData == NULL) {
2655                 MSG_DEBUG("pRawData alloc FAIL \n");
2656                 goto __CATCH;
2657         }
2658
2659         if (MsgFseek(pFile, offset, SEEK_SET) < 0) {
2660                 MSG_DEBUG("MsgFseek() returns -1 \n");
2661                 goto __CATCH;
2662         }
2663         if (MsgReadFile(pRawData, sizeof(char), bodyLength, pFile) != (size_t)bodyLength) {
2664                 MSG_DEBUG("FmReadFile() returns false \n");
2665                 goto __CATCH;
2666         }
2667         if (MsgOpenCreateAndOverwriteFile(szTempFilePath, pRawData, bodyLength) == false) {
2668                 MSG_DEBUG("MsgOpenCreateAndOverwriteFile() returns false \n");
2669                 goto __CATCH;
2670         }
2671
2672         isFileCreated = true;
2673         MSG_DEBUG("MmsDrm2GetConvertState() [%d]", MmsDrm2GetConvertState());
2674
2675         if (pMsgType->type == MIME_APPLICATION_VND_OMA_DRM_MESSAGE && (MmsDrm2GetConvertState() != MMS_DRM2_CONVERT_FINISH)) {
2676                 MmsDrm2SetConvertState(MMS_DRM2_CONVERT_REQUIRED);
2677         } else {
2678                 if (MsgDRM2GetDRMInfo(szTempFilePath, pMsgType) == false) {
2679                         MSG_DEBUG("MsgDRM2GetDRMInfo() returns false \n");
2680                         goto __CATCH;
2681                 }
2682         }
2683
2684         remove(szTempFilePath);
2685         isFileCreated = false;
2686
2687         if (MmsBinaryDecodeMovePointer(pFile, offset + bodyLength, totalLength) == false)
2688                 goto __CATCH;
2689
2690 __RETURN:
2691
2692         if (pRawData) {
2693                 free(pRawData);
2694                 pRawData = NULL;
2695         }
2696
2697         return true;
2698
2699 __CATCH:
2700         if (isFileCreated)
2701                 remove(szTempFilePath);
2702
2703         if (pRawData) {
2704                 free(pRawData);
2705                 pRawData = NULL;
2706         }
2707
2708         return false;
2709 }
2710
2711
2712 bool MmsBinaryDecodeDRMMessage(FILE *pFile, char *szFilePath, MsgType *pMsgType, MsgBody *pMsgBody, unsigned int fullBodyLength, int totalLength)
2713 {
2714         int offset = 0;
2715         char szTempFilePath[MSG_FILEPATH_LEN_MAX] = "/User/Msg/Mms/Temp/drm.dm";
2716         char *pRawData = NULL;
2717         bool isFileCreated = false;
2718
2719         offset = _MmsGetDecodeOffset();
2720         if (offset >= totalLength)
2721                 goto __RETURN;
2722
2723         if (szFilePath != NULL)
2724                 strncpy(pMsgBody->szOrgFilePath, szFilePath, strlen(szFilePath));
2725         if (szFilePath != NULL)
2726                 strncpy(pMsgType->szOrgFilePath, szFilePath, strlen(szFilePath));
2727
2728         pRawData = (char *)malloc(fullBodyLength);
2729         if (pRawData == NULL) {
2730                 MSG_DEBUG("pRawData alloc FAIL \n");
2731                 goto __CATCH;
2732         }
2733
2734         if (MsgFseek(pFile, offset,  SEEK_SET) < 0) {
2735                 MSG_DEBUG("MsgFseek() returns -1 \n");
2736                 goto __CATCH;
2737         }
2738
2739         if (MsgReadFile(pRawData, sizeof(char), fullBodyLength, pFile)!= (size_t)fullBodyLength) {
2740                 MSG_DEBUG("FmReadFile() returns false \n");
2741                 goto __CATCH;
2742         }
2743
2744         if (MsgOpenCreateAndOverwriteFile(szTempFilePath, pRawData, fullBodyLength) == false) {
2745                 MSG_DEBUG("MsgOpenCreateAndOverwriteFile() returns false \n");
2746                 goto __CATCH;
2747         }
2748         isFileCreated = true;
2749
2750         if (strstr(szTempFilePath, ".dm")) {
2751                 char szConvertedFilePath[MSG_FILEPATH_LEN_MAX] = {0,};
2752
2753                 if (MsgDrmConvertDmtoDcfType(szTempFilePath, szConvertedFilePath)) {
2754                         remove(szTempFilePath);
2755                         memset(szTempFilePath, 0, MSG_FILEPATH_LEN_MAX);
2756                         strncpy(szTempFilePath, szConvertedFilePath, MSG_FILEPATH_LEN_MAX-1);
2757                 }
2758         }
2759
2760         if (MsgDRM2GetDRMInfo(szTempFilePath, pMsgType) == false) {
2761                 MSG_DEBUG("MsgDRM2GetDRMInfo() returns false \n");
2762                 goto __CATCH;
2763         }
2764
2765         if (remove(szTempFilePath) != 0)
2766                 MSG_DEBUG("remove fail");
2767
2768         isFileCreated = false;
2769
2770         if (MmsBinaryDecodeMovePointer(pFile, offset + fullBodyLength, totalLength) == false)
2771                 goto __CATCH;
2772
2773 __RETURN:
2774
2775         if (pRawData) {
2776                 free(pRawData);
2777                 pRawData = NULL;
2778         }
2779         return true;
2780
2781 __CATCH:
2782         if (isFileCreated) {
2783                 if (remove(szTempFilePath) != 0)
2784                         MSG_DEBUG("remove fail");
2785         }
2786         if (pRawData) {
2787                 free(pRawData);
2788                 pRawData = NULL;
2789         }
2790         return false;
2791 }
2792
2793 static int __MmsDrm2BinaryEncodeUintvarLen(UINT32 integer)
2794 {
2795         UINT32 length   = 0;
2796
2797         /* Find encoded unitvar length */
2798         if (integer  <= MMS_UINTVAR_LENGTH_1) {
2799                 length = 1;
2800         } else {
2801                 if (integer <= MMS_UINTVAR_LENGTH_2) {
2802                         length = 2;
2803                 } else {
2804                         if (integer <= MMS_UINTVAR_LENGTH_3) {
2805                                 length = 3;
2806                         } else {
2807                                 length = 4;
2808                         }
2809                 }
2810         }
2811
2812         return length;
2813 }
2814
2815
2816 static bool __MmsDrm2BinaryEncodeUintvar(UINT32 integer, int length, char *pszOutput)
2817 {
2818         const char ZERO = 0x00;
2819         int i = 2;
2820         char szReverse[MSG_STDSTR_LONG] = {0, };
2821
2822         union {
2823                 UINT32 integer;
2824                 char bytes[4];
2825         } source;
2826         source.integer = integer;
2827         memset(szReverse, 0, MSG_STDSTR_LONG);
2828
2829         /* Seperate integer to 4 1 byte integer */
2830         szReverse[3] = source.bytes[3] & 0x0f;
2831         szReverse[0] = source.bytes[0];
2832         szReverse[0] = szReverse[0] & 0x7f;
2833
2834         while (length >= i) {// initially, i = 2
2835                 /* Move integer bit to proper position */
2836                 source.integer = source.integer << 1;
2837                 source.integer = source.integer >> 8;
2838                 source.bytes[3] = ZERO;
2839
2840                 /* Retrive 1 encode uintvar */
2841                 szReverse[i-1] = source.bytes[0];
2842                 szReverse[i-1] = szReverse[i-1] | 0x80;
2843                 i++;
2844         }
2845
2846         for (i=0; i < length; i++)
2847                 pszOutput[i] = szReverse[length - i - 1];
2848
2849         return true;
2850 }
2851
2852
2853 static int __MmsDrm2GetEntriesValueLength(FILE *pFile, int orgOffset)
2854 {
2855         char szEntries[5] = {0, };
2856         UINT8 oneByte   = 0;
2857         int j = 0;                      //j is the length of nEntries value
2858
2859         if (MsgReadFile(szEntries, sizeof(char), 4, pFile) != (size_t)4) {
2860                 MSG_DEBUG("__MmsDrm2GetEntriesValueLength: FmReadFile() returns false \n");
2861                 return false;
2862         }
2863
2864         while (true) {
2865                 oneByte = szEntries[j++];
2866
2867                 if (oneByte <= 0x7f)
2868                         break;
2869         }
2870
2871         //move file pointer to point nEntries
2872         if (MsgFseek(pFile, orgOffset, SEEK_SET) < 0) {
2873                 MSG_DEBUG("__MmsDrm2GetEntriesValueLength: fail to seek file pointer\n");
2874                 return false;
2875         }
2876
2877         return j;
2878 }
2879
2880
2881 static bool __MmsDrm2WriteDataToConvertedFile(FILE *pSrcFile, FILE *pDestinationFile, char *pszMmsLoadTempBuf, int length, int bufLen)
2882 {
2883         int loadLen = 0, totalLoadLen = 0, nRead = 0;
2884
2885         for (int i=0; i<(length/bufLen)+1; i++) {
2886                 loadLen = (length-totalLoadLen < bufLen) ? length-totalLoadLen : bufLen;
2887
2888                 memset(pszMmsLoadTempBuf, 0, MMS_DRM2_CONVERT_BUFFER_MAX + 1);
2889
2890                 if (MsgReadFile(pszMmsLoadTempBuf, sizeof(char), loadLen, pSrcFile) != (size_t)loadLen) {
2891                         MSG_DEBUG("__MmsDrm2WriteDataToConvertedFile: FmReadFile() returns false \n");
2892                         return false;
2893                 }
2894
2895                 if (MsgWriteFile(pszMmsLoadTempBuf, sizeof(char), loadLen, pDestinationFile) != (size_t)loadLen) {
2896                         MSG_DEBUG("__MmsDrm2WriteDataToConvertedFile: File Writing is failed.\n");
2897                         return false;
2898                 }
2899
2900                 totalLoadLen += nRead;
2901         }
2902
2903         return true;
2904 }
2905
2906
2907 /*************************************************************************
2908  * description : make new message file converting CD & FL part of original message file to SD type
2909  * argument : void
2910  * return value
2911     - bool :  result of converting
2912 **************************************************************************/
2913 bool MmsDrm2ConvertMsgBody(char *szOriginFilePath)
2914 {
2915         FILE *pFile = NULL;
2916         FILE *hConvertedFile = NULL;
2917         FILE *hTempFile = NULL;
2918         FILE *hFile = NULL;
2919         MsgMultipart *pMultipart = NULL;
2920         char szTempFilePath[MSG_FILEPATH_LEN_MAX] = MSG_DATA_PATH"Drm_Convert";
2921         char szTempFile[MSG_FILEPATH_LEN_MAX] = MSG_DATA_PATH"temp.dm";
2922         char *pszMmsLoadTempBuf = NULL;
2923         char *pszOrgData = NULL;
2924         int length = 0;
2925         int bufLen = MMS_DRM2_CONVERT_BUFFER_MAX;
2926         int curOffset = 0;
2927
2928         MSG_DEBUG("start convert~~~~~~\n");
2929
2930         pFile = MsgOpenFile(szOriginFilePath, "rb");
2931         if (pFile == NULL) {
2932                 MSG_DEBUG("Open decode temporary file fail\n");
2933                 goto __CATCH;
2934         }
2935
2936         hConvertedFile = MsgOpenFile(MMS_DECODE_DRM_CONVERTED_TEMP_FILE, "wb+");
2937         if (hConvertedFile == NULL) {
2938                 MSG_DEBUG("Open decode temporary file fail\n");
2939                 goto __CATCH;
2940         }
2941
2942         pszMmsLoadTempBuf = (char*)malloc(MMS_DRM2_CONVERT_BUFFER_MAX + 1);
2943         if (pszMmsLoadTempBuf == NULL) {
2944                 MSG_DEBUG("malloc for pszMmsLoadTempBuf failed\n");
2945                 goto __CATCH;
2946         }
2947         memset(pszMmsLoadTempBuf, 0, MMS_DRM2_CONVERT_BUFFER_MAX + 1);
2948
2949         // MMS Header  copy
2950         length = mmsHeader.msgBody.offset;
2951         if (__MmsDrm2WriteDataToConvertedFile(pFile, hConvertedFile, pszMmsLoadTempBuf, length, bufLen) == false) {
2952                 MSG_DEBUG("Write header data fail\n");
2953                 goto __CATCH;
2954         }
2955
2956         curOffset += length;    //change offset
2957
2958         // MMS Body copy
2959         if (MsgIsMultipart(mmsHeader.msgType.type) == true)
2960         {
2961                 // nEntries copy
2962                 length = __MmsDrm2GetEntriesValueLength(pFile, curOffset);      // getting nEntries value's length
2963
2964                 if (__MmsDrm2WriteDataToConvertedFile(pFile, hConvertedFile, pszMmsLoadTempBuf, length, bufLen) == false) {
2965                         MSG_DEBUG("Write nEntries fail\n");
2966                         goto __CATCH;
2967                 }
2968
2969                 curOffset += length;    //change offset
2970
2971                 // each Multipart entry copy
2972                 pMultipart = mmsHeader.msgBody.body.pMultipart;
2973
2974                 while (pMultipart) {
2975                         if (pMultipart->type.type == MIME_APPLICATION_VND_OMA_DRM_MESSAGE) {
2976                                 int orgDataLen = pMultipart->pBody->size;
2977                                 int nSize = 0;
2978
2979                                 MSG_DEBUG("Write MIME_APPLICATION_VND_OMA_DRM_MESSAGE multipart data(orgDataLen = %d).\n", orgDataLen);
2980
2981                                 pszOrgData = (char *)malloc(orgDataLen + 1);
2982                                 if (pszOrgData == NULL) {
2983                                         MSG_DEBUG("pszOrgData is NULL \n");
2984                                         goto __CATCH;
2985                                 }
2986                                 memset(pszOrgData, 0, orgDataLen + 1);
2987
2988                                 // move file pointer to data
2989                                 if (MsgFseek(pFile, pMultipart->pBody->offset, SEEK_SET) < 0) {
2990                                         MSG_DEBUG("fail to seek file pointer 1\n");
2991                                         goto __CATCH;
2992                                 }
2993
2994                                 if (MsgReadFile(pszOrgData, sizeof(char), orgDataLen, pFile) != (size_t)orgDataLen) {
2995                                         MSG_DEBUG("FmReadFile() returns false for orgData\n");
2996                                         goto __CATCH;
2997                                 }
2998
2999                                 if((hFile = MsgOpenFile(szTempFile, "wb+")) == NULL) {
3000                                         MSG_DEBUG("file open failed [%s]", szTempFile);
3001                                         goto __CATCH;
3002                                 }
3003
3004                                 if (MsgWriteFile(pszOrgData, sizeof(char), orgDataLen, hFile) != (size_t)orgDataLen) {
3005                                         MSG_DEBUG("File write error");
3006                                         goto __CATCH;
3007                                 }
3008
3009                                 if (pszOrgData) {
3010                                         free(pszOrgData);
3011                                         pszOrgData = NULL;
3012                                 }
3013
3014                                 MsgFflush(hFile);
3015                                 MsgCloseFile(hFile);
3016
3017                                 hFile = NULL;
3018
3019                                 // --> invoking drm agent api, converting data part start
3020                                 MSG_DEBUG("start data part convert by callling drm agent api\n");
3021
3022                                 int ret = 0;
3023                                 ret = MsgDrmConvertDmtoDcfType(szTempFile, szTempFilePath);
3024                                 MSG_DEBUG("MsgDrmConvertDmtoDcfType returned %s", ret ? "true": "false");
3025
3026                                 if (MsgGetFileSize(szTempFilePath, &nSize) == false) {
3027                                         MSG_DEBUG("MsgGetFileSize error");
3028                                         goto __CATCH;
3029                                 }
3030                                 MSG_DEBUG("end data part convert(converted data len = %d)\n", nSize);
3031
3032                                 // move file pointer to the head of multipart
3033                                 if (MsgFseek(pFile, curOffset, SEEK_SET) < 0) {
3034                                         MSG_DEBUG("fail to seek file pointer 2\n");
3035                                         goto __CATCH;
3036                                 }
3037
3038                                 // read headerLen, dataLen
3039                                 length = pMultipart->type.offset - curOffset;
3040                                 memset(pszMmsLoadTempBuf, 0, MMS_DRM2_CONVERT_BUFFER_MAX + 1);
3041                                 if (MsgReadFile(pszMmsLoadTempBuf, sizeof(char), length, pFile) != (size_t)length) {
3042                                         MSG_DEBUG("FmReadFile() returns false for headerLen, dataLen\n");
3043                                         goto __CATCH;
3044                                 }
3045
3046                                 curOffset += length;
3047
3048                                 // change dataLen based on converted data
3049                                 {
3050                                         UINT8   oneByte = 0;
3051                                         int             j = 0;
3052                                         int             encodeLen = 0;
3053                                         char    szOutput[MSG_STDSTR_LONG] = {0, };
3054
3055                                         while (true) {
3056                                                 oneByte = pszMmsLoadTempBuf[j++];
3057
3058                                                 if (oneByte <= 0x7f)
3059                                                         break;
3060                                         }
3061
3062                                         encodeLen = __MmsDrm2BinaryEncodeUintvarLen((UINT32)nSize);
3063                                         __MmsDrm2BinaryEncodeUintvar((UINT32)nSize, encodeLen, szOutput);
3064
3065                                         strncpy(&(pszMmsLoadTempBuf[j]), szOutput, encodeLen);
3066                                         pszMmsLoadTempBuf[j+encodeLen] = '\0';
3067
3068                                         if (MsgWriteFile(pszMmsLoadTempBuf, sizeof(char), length, hConvertedFile) != (size_t)length) {
3069                                                 MSG_DEBUG("Drm2WriteConvertData: FmWriteFile() returns false for dateLen\n");
3070                                                 goto __CATCH;
3071                                         }
3072                                 }
3073
3074
3075                                 length = pMultipart->pBody->offset - pMultipart->type.offset;
3076
3077                                 if (__MmsDrm2WriteDataToConvertedFile(pFile, hConvertedFile, pszMmsLoadTempBuf, length, bufLen) == false) {
3078                                         MSG_DEBUG("Drm2WriteConvertData: Write content type, headers fail\n");
3079                                         goto __CATCH;
3080                                 }
3081
3082                                 curOffset += length;
3083
3084                                 // write converted data
3085                                 hTempFile = MsgOpenFile(szTempFilePath, "rb");
3086                                 if (hTempFile == NULL) {
3087                                         MSG_DEBUG("Open decode temporary file fail\n");
3088                                         goto __CATCH;
3089                                 }
3090
3091                                 length = nSize;
3092
3093                                 if (__MmsDrm2WriteDataToConvertedFile(hTempFile, hConvertedFile, pszMmsLoadTempBuf, length, bufLen) == false) {
3094                                         MSG_DEBUG("Write converted data fail\n");
3095                                         goto __CATCH;
3096                                 }
3097
3098                                 if (hTempFile != NULL) {
3099                                         MsgCloseFile(hTempFile);
3100                                         hTempFile = NULL;
3101                                 }
3102
3103                                 curOffset += pMultipart->pBody->size;
3104
3105                                 // move file pointer to the head of multipart
3106                                 if (MsgFseek(pFile, curOffset, SEEK_SET) < 0) {
3107                                         MSG_DEBUG("fail to seek file pointer \n");
3108                                         goto __CATCH;
3109                                 }
3110                         } else {        // it doesn't need to convert if it is not CD or FL
3111                                 MSG_DEBUG("Write normal multipart data\n");
3112
3113                                 length = pMultipart->pBody->offset + pMultipart->pBody->size - curOffset;
3114
3115                                 if (__MmsDrm2WriteDataToConvertedFile(pFile, hConvertedFile, pszMmsLoadTempBuf, length, bufLen) == false) {
3116                                         MSG_DEBUG("Write multipart data fail \n");
3117                                         goto __CATCH;
3118                                 }
3119
3120                                 curOffset += length;
3121                         }
3122
3123                         pMultipart = pMultipart->pNext;
3124                 }
3125         }
3126
3127         MSG_DEBUG("end convert~~~~~~\n");
3128
3129         if (pFile != NULL) {
3130                 MsgCloseFile(pFile);
3131                 pFile = NULL;
3132         }
3133
3134         if (hConvertedFile != NULL) {
3135                 MsgCloseFile(hConvertedFile);
3136                 hConvertedFile = NULL;
3137         }
3138
3139         if (pszMmsLoadTempBuf) {
3140                 free(pszMmsLoadTempBuf);
3141                 pszMmsLoadTempBuf = NULL;
3142         }
3143
3144         if (pszOrgData) {
3145                 free(pszOrgData);
3146                 pszOrgData = NULL;
3147         }
3148
3149         remove(szTempFile);
3150         remove(szTempFilePath);
3151
3152         return true;
3153
3154 __CATCH:
3155
3156         if (pFile != NULL) {
3157                 MsgCloseFile(pFile);
3158                 pFile = NULL;
3159         }
3160
3161         if (hConvertedFile != NULL) {
3162                 MsgCloseFile(hConvertedFile);
3163                 hConvertedFile = NULL;
3164         }
3165
3166         if (hTempFile != NULL) {
3167                 MsgCloseFile(hTempFile);
3168                 hTempFile = NULL;
3169         }
3170
3171         if (pszMmsLoadTempBuf) {
3172                 free(pszMmsLoadTempBuf);
3173                 pszMmsLoadTempBuf = NULL;
3174         }
3175
3176         if (pszOrgData) {
3177                 free(pszOrgData);
3178                 pszOrgData = NULL;
3179         }
3180
3181         if (hFile != NULL)
3182         {
3183                 MsgCloseFile(hFile);
3184                 hFile = NULL;
3185         }
3186
3187         if (remove(szTempFile) != 0)
3188                 MSG_DEBUG("remove fail");
3189
3190         if (remove(szTempFilePath) != 0)
3191                 MSG_DEBUG("remove fail");
3192
3193         if (remove(MMS_DECODE_DRM_CONVERTED_TEMP_FILE) != 0)
3194                 MSG_DEBUG("remove fail");       //remove convertin result if it goes to __CATCH
3195
3196         return false;
3197 }
3198
3199
3200 /*************************************************************************
3201  * description : Function for decoding a converted file
3202  * argument : void
3203  * return value
3204     - bool :  result of converting
3205 **************************************************************************/
3206 bool MmsDrm2DecodeConvertedMsg(int msgID, char *pszFullPath)
3207 {
3208         FILE *hConvertedFile = NULL;
3209         int     nSize = 0;
3210
3211         MSG_DEBUG("MmsDrm2DecodeConvertedMsg: start re-decoding~~~~~~\n");
3212
3213         // free
3214         _MsgFreeDRMInfo(&mmsHeader.msgType.drmInfo);
3215         _MsgFreeBody(&mmsHeader.msgBody, mmsHeader.msgType.type);
3216
3217         _MmsInitHeader();
3218         _MmsUnregisterDecodeBuffer();
3219
3220         // start decoding
3221         _MmsRegisterDecodeBuffer(gszMmsLoadBuf1, gszMmsLoadBuf2, MSG_MMS_DECODE_BUFFER_MAX);
3222
3223         // open converted file
3224         if ((hConvertedFile = MsgOpenFile(MMS_DECODE_DRM_CONVERTED_TEMP_FILE, "rb")) == NULL) {
3225                 MSG_DEBUG("MmsDrm2ReDecodeMsg: opening temporary file failed\n");
3226                 goto __CATCH;
3227         }
3228
3229         if (MsgGetFileSize(MMS_DECODE_DRM_CONVERTED_TEMP_FILE, &nSize) == false) {
3230                 MSG_DEBUG("MsgGetFileSize: failed\n");
3231                 goto __CATCH;
3232         }
3233
3234         if (!MmsBinaryDecodeMsgHeader(hConvertedFile, nSize)) {
3235                 MSG_DEBUG("MmsDrm2ReDecodeMsg: decoding header(binary mode) failed\n");
3236                 goto __CATCH;
3237         }
3238
3239         if (!MmsBinaryDecodeMsgBody(hConvertedFile, pszFullPath, nSize)) {
3240                 MSG_DEBUG("MmsDrm2ReDecodeMsg: decoding body failed\n");
3241                 goto __CATCH;
3242         }
3243
3244         if (hConvertedFile != NULL) {
3245                 MsgCloseFile(hConvertedFile);
3246                 hConvertedFile = NULL;
3247         }
3248
3249         return true;
3250
3251 __CATCH:
3252
3253         if (hConvertedFile != NULL) {
3254                 MsgCloseFile(hConvertedFile);
3255
3256                 if (remove(MMS_DECODE_DRM_CONVERTED_TEMP_FILE) != 0)
3257                         MSG_DEBUG("remove fail");
3258
3259                 hConvertedFile = NULL;
3260         }
3261
3262         return false;
3263 }
3264
3265
3266 bool MmsDrm2ReadMsgConvertedBody(MSG_MESSAGE_INFO_S *pMsg, bool bSavePartsAsTempFiles, bool bRetrieved, char *retrievedPath)
3267 {
3268         MmsMsg *pMmsMsg;
3269         MmsPluginStorage::instance()->getMmsMessage(&pMmsMsg);
3270         _MmsUnregisterDecodeBuffer();
3271 #ifdef __SUPPORT_DRM__
3272         _MsgFreeDRMInfo(&pMmsMsg->msgType.drmInfo);
3273 #endif
3274         _MsgFreeBody(&pMmsMsg->msgBody, pMmsMsg->msgType.type);
3275
3276         if (_MmsReadMsgBody(pMsg->msgId, bSavePartsAsTempFiles, bRetrieved, retrievedPath) == false) {
3277                 MSG_DEBUG("MmsDrm2ReadMsgConvertedBody: _MmsReadMsgBody with converted file is failed\n");
3278                 return false;
3279         }
3280
3281         return true;
3282 }
3283
3284 #endif
3285
3286 /* --------------------------------------------------------------------
3287  *
3288  *     B  I  N  A  R  Y       D  E  C  D  E      U  T  I  L  I  T  Y
3289  *
3290  * --------------------------------------------------------------------*/
3291
3292 bool _MmsBinaryDecodeGetOneByte(FILE *pFile, UINT8 *pOneByte, int totalLength)
3293 {
3294         int length = gMmsDecodeMaxLen - gCurMmsDecodeBuffPos;
3295
3296         if (pFile == NULL || pOneByte == NULL)
3297         {
3298                 MSG_DEBUG("_MmsBinaryDecodeGetOneByte: invalid file or buffer\n");
3299                 goto __CATCH;
3300         }
3301
3302         if (length < 1) {
3303                 if (MsgLoadDataToDecodeBuffer(pFile, &gpCurMmsDecodeBuff, &gCurMmsDecodeBuffPos, &gMmsDecodeCurOffset,
3304                                                                            gpMmsDecodeBuf1, gpMmsDecodeBuf2, gMmsDecodeMaxLen, &gMmsDecodeBufLen, totalLength) == false) {
3305                         MSG_DEBUG("_MmsBinaryDecodeGetOneByte: fail to load to buffer \n");
3306                         goto __CATCH;
3307                 }
3308         }
3309
3310         *pOneByte = gpCurMmsDecodeBuff[gCurMmsDecodeBuffPos++];
3311
3312         return true;
3313
3314 __CATCH:
3315         return false;
3316 }
3317
3318
3319 /*
3320  * @remark: bufLen < gMmsDecodeMaxLen
3321  */
3322 bool _MmsBinaryDecodeGetBytes(FILE *pFile, char *szBuff, int bufLen, int totalLength)
3323 {
3324         int length = gMmsDecodeMaxLen - gCurMmsDecodeBuffPos;
3325         int i = 0;
3326
3327
3328         if (pFile == NULL || szBuff == NULL || bufLen == 0 || bufLen > gMmsDecodeMaxLen)
3329                 goto __CATCH;
3330
3331         memset(szBuff, 0, bufLen);
3332
3333         if (length < bufLen) {
3334                 if (MsgLoadDataToDecodeBuffer(pFile, &gpCurMmsDecodeBuff, &gCurMmsDecodeBuffPos, &gMmsDecodeCurOffset,
3335                                                                            gpMmsDecodeBuf1, gpMmsDecodeBuf2, gMmsDecodeMaxLen, &gMmsDecodeBufLen, totalLength) == false) {
3336                         MSG_DEBUG("_MmsBinaryDecodeGetBytes: fail to load to buffer \n");
3337                         goto __CATCH;
3338                 }
3339         }
3340
3341         for (i = 0; i < bufLen - 1; i++)
3342                 szBuff[i] = gpCurMmsDecodeBuff[gCurMmsDecodeBuffPos++];
3343
3344
3345         gCurMmsDecodeBuffPos++; //NULL
3346
3347         return true;
3348
3349 __CATCH:
3350         return false;
3351 }
3352
3353
3354
3355 bool _MmsBinaryDecodeGetLongBytes(FILE *pFile, char *szBuff, int bufLen, int totalLength)
3356 {
3357         int iPos = 0;
3358
3359         if (pFile == NULL || szBuff == NULL || bufLen == 0)
3360                 goto __CATCH;
3361
3362         memset(szBuff, 0, bufLen);
3363
3364         if (MsgLoadDataToDecodeBuffer(pFile, &gpCurMmsDecodeBuff, &gCurMmsDecodeBuffPos, &gMmsDecodeCurOffset,
3365                                                                    gpMmsDecodeBuf1, gpMmsDecodeBuf2, gMmsDecodeMaxLen, &gMmsDecodeBufLen, totalLength) == false) {
3366                 MSG_DEBUG("_MmsBinaryDecodeGetLongBytes: fail to load to buffer \n");
3367                 goto __CATCH;
3368         }
3369
3370         while ((bufLen - iPos) >= gMmsDecodeMaxLen) {
3371                 if (_MmsBinaryDecodeGetBytes(pFile, szBuff + iPos, gMmsDecodeMaxLen, totalLength) == false) {
3372                         MSG_DEBUG("_MmsBinaryDecodeGetLongBytes: 1. _MmsBinaryDecodeGetBytes fail \n");
3373                         goto __CATCH;
3374                 }
3375
3376                 iPos += gMmsDecodeMaxLen;
3377         }
3378
3379         if ((bufLen - iPos) > 0) {
3380                 if (_MmsBinaryDecodeGetBytes(pFile, szBuff + iPos, (bufLen - iPos), totalLength) == false) {
3381                         MSG_DEBUG("_MmsBinaryDecodeGetLongBytes: 2. _MmsBinaryDecodeGetBytes fail \n");
3382                         goto __CATCH;
3383                 }
3384
3385                 iPos += (bufLen - iPos);
3386         }
3387
3388         return true;
3389
3390 __CATCH:
3391         return false;
3392 }
3393
3394
3395 /**
3396  * Decode uintvar to 32bit unsigned integer
3397  *
3398  * @param       pEncodedData    [in] encoded data
3399  * @param       pUintVar                [out] Decode uintvar (32bit unsigned integer)
3400  * @return      The length of uintvar (-1, if cannot be converted to a uintvar)
3401  *
3402  * 0 XXXXXXX -> 0-bit: continue bit & 1~7bit: integer value
3403  * - -------
3404  */
3405 static const UINT32 uintvarDecodeTable[] = { 0x00000001, 0x00000080, 0x00004000, 0x00100000, 0x08000000 };
3406
3407 static int __MmsBinaryDecodeUintvar(FILE *pFile, UINT32 *pUintVar, int totalLength)
3408 {
3409         UINT8 count = 0;
3410         UINT8 oneByte = 0;
3411         UINT32 decodedUintvar = 0;
3412         UINT8 iBuff[5] = {0};
3413         int length = MSG_MMS_DECODE_BUFFER_MAX - gCurMmsDecodeBuffPos;
3414
3415
3416         if (pFile == NULL || pUintVar == NULL)
3417                 return -1;
3418
3419         if (length < 5) {
3420                 if (MsgLoadDataToDecodeBuffer(pFile, &gpCurMmsDecodeBuff, &gCurMmsDecodeBuffPos, &gMmsDecodeCurOffset,
3421                                                                            gpMmsDecodeBuf1, gpMmsDecodeBuf2, gMmsDecodeMaxLen, &gMmsDecodeBufLen, totalLength) == false) {
3422                         MSG_DEBUG("__MmsBinaryDecodeUintvar: fail to load to buffer \n");
3423                         goto __CATCH;
3424                 }
3425         }
3426
3427         while (true) {
3428                 if (_MmsBinaryDecodeGetOneByte(pFile, &oneByte, totalLength) == false)
3429                         goto __CATCH;
3430
3431                 if (oneByte > 0x7f)     {
3432                         iBuff[count++] = oneByte;
3433                 } else {
3434                         iBuff[count++] = oneByte;
3435                         break;
3436                 }
3437
3438                 if (count > 4) {
3439                         MSG_DEBUG("__MmsBinaryDecodeUintvar : legnth is too long\n");
3440                         goto __CATCH;
3441                 }
3442         }
3443
3444         for (int i = 0; i < count; i++)
3445                 decodedUintvar += (uintvarDecodeTable[i] * (iBuff[count-(i+1)]&0x7f));
3446
3447         *pUintVar = decodedUintvar;
3448
3449         return count;
3450
3451 __CATCH:
3452         gCurMmsDecodeBuffPos -= count;
3453         return -1;
3454 }
3455
3456
3457 /**
3458  * Decode uintvar to 32bit unsigned integer by uintvar length
3459  *
3460  * @param       pEncodedData [in] uintvar encoded data
3461  * @param       length           [in] length of integer value
3462  * @return      unsigned integer value
3463  */
3464 static UINT32 __MmsHeaderDecodeIntegerByLength(FILE *pFile, UINT32 length, int totalLength)
3465 {
3466         UINT32 i = 0;
3467         UINT8 oneByte = 0;
3468         char *pData = NULL;
3469         union {
3470                 UINT32  integer;
3471                 UINT8   seg[4];
3472         } returner;
3473
3474         returner.integer = 0;
3475
3476         if (length > 4)
3477                 length = 4;
3478
3479         if (length == 1)
3480         {
3481                 if (_MmsBinaryDecodeGetOneByte(pFile, &oneByte, totalLength) == false) {
3482                         MSG_DEBUG("__MmsHeaderDecodeIntegerByLength: _MmsBinaryDecodeGetOneByte fail\n");
3483                         return oneByte;
3484                 }
3485
3486                 if (oneByte > 0x7f) {
3487                         return (oneByte & 0x7f);
3488                 } else {
3489                         return oneByte;
3490                 }
3491         }
3492
3493         if (length == 0)
3494                 return 0;
3495
3496         pData = (char *)malloc(length + 1);
3497         if (pData == NULL) {
3498                 MSG_DEBUG("__MmsHeaderDecodeIntegerByLength: pData alloc fail\n");
3499                 goto __CATCH;
3500         }
3501         memset(pData, 0, length + 1);
3502
3503         if (_MmsBinaryDecodeGetBytes(pFile, pData, length + 1, totalLength) == false) {
3504                 MSG_DEBUG("__MmsHeaderDecodeIntegerByLength: _MmsBinaryDecodeGetOneByte fail\n");
3505                 goto __CATCH;
3506         }
3507
3508         gCurMmsDecodeBuffPos--; // - NULL
3509
3510         for (i= 0; i < length; i++)
3511                 returner.seg[length - (i+1)] = pData[i];
3512
3513         if (pData) {
3514                 free(pData);
3515                 pData = NULL;
3516         }
3517
3518         return returner.integer;
3519
3520 __CATCH:
3521
3522         if (pData) {
3523                 free(pData);
3524                 pData = NULL;
3525         }
3526
3527         return returner.integer;
3528 }
3529
3530
3531 /**
3532  * Decode uintvar to 32bit unsigned integer by uintvar length
3533  *
3534  * @param       pEncodedData    [in]  uintvar encoded data
3535  * @param       pInteger                [out] Decode integer value (long/short)
3536  * @return      unsigned integer value (-1, if cannot be converted to unsigned integer value)
3537  */
3538 static bool __MmsBinaryDecodeInteger(FILE *pFile, UINT32 *pInteger, int *pIntLen, int totalLength)
3539 {
3540         UINT8 oneByte   = 0;
3541         char *pData     = NULL;
3542         union {
3543                 UINT32  integer;
3544                 UINT8   seg[4];
3545         } returner;
3546
3547
3548         if (pInteger == NULL)
3549                 return false;
3550
3551         returner.integer = 0;
3552         *pIntLen                 = 0;
3553
3554         if (_MmsBinaryDecodeGetOneByte(pFile, &oneByte, totalLength) == false) {
3555                 MSG_DEBUG("__MmsBinaryDecodeInteger: GetOneByte fail\n");
3556                 return false;
3557         }
3558
3559         if (oneByte < 0x1F)                             /* long integer : WAP-230-WSP-20010118-p, Proposed Version 18 January 2001 (pp.86) */
3560         {
3561                 pData = (char *)malloc(oneByte + 1);
3562                 if (pData == NULL) {
3563                         MSG_DEBUG("__MmsBinaryDecodeInteger: pData memalloc fail\n");
3564                         goto __CATCH;
3565                 }
3566                 memset(pData, 0, oneByte + 1);
3567
3568                 // Even NULL is copied in the _MmsBinaryDecodeGetBytes
3569                 if (_MmsBinaryDecodeGetBytes(pFile, pData, oneByte + 1, totalLength) == false) {
3570                         MSG_DEBUG("__MmsBinaryDecodeInteger: GetBytes fail\n");
3571                         goto __CATCH;
3572                 }
3573
3574                 gCurMmsDecodeBuffPos--; // - NULL
3575
3576                 int             length  = 0;
3577                 if (oneByte > 4) {
3578                         length = 4;
3579                 } else {
3580                         length = oneByte;
3581                 }
3582
3583                 int             i = 0;
3584                 for (i = 0; i < length; i++)
3585                         returner.seg[length - (i+1)] = pData[i];
3586
3587                 *pInteger = returner.integer;
3588                 *pIntLen  = oneByte + 1;
3589         } else if (oneByte >= 0x80)     {
3590                 /* short integer : WAP-230-WSP-20010118-p, Proposed Version 18 January 2001 (pp.86) */
3591                 *pInteger = oneByte & 0x7f;
3592                 *pIntLen  = 1;
3593         } else {
3594                 goto __CATCH;
3595         }
3596
3597         if (pData) {
3598                 free(pData);
3599                 pData = NULL;
3600         }
3601
3602         return true;
3603
3604 __CATCH:
3605
3606         gCurMmsDecodeBuffPos--;
3607
3608         if (pData) {
3609                 free(pData);
3610                 pData = NULL;
3611         }
3612
3613         return false;
3614 }
3615
3616
3617 /**
3618  * Decode uintvar to 32bit unsigned integer by uintvar length
3619  *
3620  * @return      1  : Success
3621  *                      0  : This is not Value Length type data
3622  *                      -1 : Requires System error report
3623  */
3624 static int __MmsDecodeValueLength(FILE *pFile, UINT32 *pValueLength, int totalLength)
3625 {
3626         int length = 0;
3627         UINT32 uintvar = 0;
3628         UINT8 oneByte = 0;
3629
3630
3631         /*
3632          * value-length = short-length | (Length-quote Length)
3633          *                              = 0~30             | 31 + Uintvar-length
3634          */
3635
3636         if (pFile == NULL || pValueLength == NULL)
3637                 goto __CATCH;
3638
3639         *pValueLength = 0;
3640
3641         if (_MmsBinaryDecodeGetOneByte(pFile, &oneByte, totalLength) == false) {
3642                 gCurMmsDecodeBuffPos--;
3643                 goto __CATCH;
3644         }
3645
3646         if (0x00 < oneByte && oneByte < 0x1F) {
3647                 /* short-length */
3648
3649                 *pValueLength = oneByte;
3650                 length = 1;
3651         } else if (oneByte == 0x1F) {
3652                 /* Length-quote = 0x1F */
3653
3654                 length = __MmsBinaryDecodeUintvar(pFile, &uintvar, totalLength);
3655                 if (length == -1) {
3656                         MSG_DEBUG("__MmsDecodeValueLength: __MmsBinaryDecodeUintvar fail..\n");
3657                         goto __CATCH;
3658                 }
3659                 length ++;                                      // + length-quote
3660                 *pValueLength = uintvar;
3661         } else {
3662                 MSG_DEBUG("__MmsDecodeValueLength: not a value length type data\n");
3663                 gCurMmsDecodeBuffPos--;
3664                 return 0;
3665         }
3666
3667         return length;
3668
3669 __CATCH:
3670         MSG_DEBUG("__MmsDecodeValueLength: getting data fail\n");
3671         return -1;
3672 }
3673
3674
3675
3676 /**
3677  * Decode uintvar to 32bit unsigned integer by uintvar length
3678  *
3679  * @return      1  : Success
3680  *                      0  : This is not Value Length type data
3681  *                      -1 : Requires System error report
3682  * @ defference : if there is not length-quote, consider it as short length.
3683  */
3684 static int __MmsDecodeValueLength2(FILE *pFile, UINT32 *pValueLength, int totalLength)
3685 {
3686         int length      = 0;
3687         UINT32 uintvar = 0;
3688         UINT8 oneByte = 0;
3689
3690
3691         /*
3692          * value-length = short-length | (Length-quote Length)
3693          *                              = 0~30             | 31 + Uintvar-length
3694          */
3695
3696         if (pFile == NULL || pValueLength == NULL)
3697                 goto __CATCH;
3698
3699         *pValueLength = 0;
3700
3701         if (_MmsBinaryDecodeGetOneByte(pFile, &oneByte, totalLength) == false) {
3702                 gCurMmsDecodeBuffPos--;
3703                 goto __CATCH;
3704         }
3705
3706         if (0x00 < oneByte && oneByte < 0x1F) {
3707                 /* short-length */
3708
3709                 *pValueLength = oneByte;
3710                 length = 1;
3711         } else if (oneByte == 0x1F) {
3712                 /* Length-quote = 0x1F */
3713
3714                 length = __MmsBinaryDecodeUintvar(pFile, &uintvar, totalLength);
3715                 if (length == -1) {
3716                         MSG_DEBUG("__MmsDecodeValueLength2: __MmsBinaryDecodeUintvar fail..\n");
3717                         goto __CATCH;
3718                 }
3719                 length ++;                                      // + length-quote
3720                 *pValueLength = uintvar;
3721         } else {
3722                 MSG_DEBUG("__MmsDecodeValueLength2: there is not length-quote, consider it as short length.\n");
3723                 *pValueLength = oneByte;
3724                 length = 1;
3725         }
3726
3727         return length;
3728
3729 __CATCH:
3730         MSG_DEBUG("__MmsDecodeValueLength2: getting data fail\n");
3731         return -1;
3732 }
3733
3734
3735
3736 /**
3737  * Decode QuotedString
3738  *
3739  * @param       pEncodedData    [in] QuotedString encoded data
3740  * @param       szBuff                  [out] Decoded quoted string
3741  * @param       bufLen                  [out] Buffer length
3742  * @return      length of quoted string
3743  */
3744 static int __MmsBinaryDecodeQuotedString(FILE *pFile, char *szBuff, int bufLen, int totalLength)
3745 {
3746         int iPos = 0;
3747         int length = 0;
3748         int readBytes = 0;
3749         char *pData = NULL;
3750         int returnLength = 0;
3751
3752         /*
3753          * Quoted-string = <Octet 34> *TEXT End-of-string
3754          * The TEXT encodes an RFC2616 Quoted-string with the enclosing quotation-marks <"> removed
3755          */
3756
3757         if (pFile == NULL || szBuff == NULL || bufLen <= 0)
3758                 return -1;
3759
3760         memset(szBuff, 0, bufLen);
3761
3762         if (MsgLoadDataToDecodeBuffer(pFile, &gpCurMmsDecodeBuff, &gCurMmsDecodeBuffPos, &gMmsDecodeCurOffset,
3763                                                                          gpMmsDecodeBuf1, gpMmsDecodeBuf2, gMmsDecodeMaxLen, &gMmsDecodeBufLen, totalLength) == false) {
3764                 MSG_DEBUG("__MmsBinaryDecodeQuotedString: 1. fail to load to buffer \n");
3765                 goto __CATCH;
3766         }
3767
3768         length = strlen(gpCurMmsDecodeBuff) + 1;        // + NULL
3769
3770         if (length == 0)
3771                 goto __RETURN;
3772
3773         while (length > gMmsDecodeBufLen) {
3774                 if (gMmsDecodeBufLen <= 0) {
3775                         MSG_DEBUG("__MmsBinaryDecodeQuotedString: gMmsDecodeBufLen <= 0 \n");
3776                         MSG_DEBUG("__MmsBinaryDecodeQuotedString: %x %x %x %x %x\n",
3777                                                                                 gpCurMmsDecodeBuff[0], gpCurMmsDecodeBuff[1], gpCurMmsDecodeBuff[2],
3778                                                                                 gpCurMmsDecodeBuff[3], gpCurMmsDecodeBuff[4]);
3779                         MSG_DEBUG("__MmsBinaryDecodeQuotedString: %x %x %x %x %x\n",
3780                                                                                 gpCurMmsDecodeBuff[5], gpCurMmsDecodeBuff[6], gpCurMmsDecodeBuff[7],
3781                                                                                 gpCurMmsDecodeBuff[8], gpCurMmsDecodeBuff[9]);
3782                         MSG_DEBUG("__MmsBinaryDecodeQuotedString: %x %x %x %x %x\n",
3783                                                                                 gpCurMmsDecodeBuff[10], gpCurMmsDecodeBuff[11], gpCurMmsDecodeBuff[12],
3784                                                                                 gpCurMmsDecodeBuff[13], gpCurMmsDecodeBuff[14]);
3785                         MSG_DEBUG("__MmsBinaryDecodeQuotedString: %x %x %x %x %x\n",
3786                                                                                 gpCurMmsDecodeBuff[15], gpCurMmsDecodeBuff[16], gpCurMmsDecodeBuff[17],
3787                                                                                 gpCurMmsDecodeBuff[18], gpCurMmsDecodeBuff[19]);
3788                         goto __CATCH;
3789                 }
3790
3791                 pData = (char *)malloc(gMmsDecodeBufLen + 1);
3792                 if (pData == NULL)
3793                         goto __CATCH;
3794
3795                 memset(pData, 0, gMmsDecodeBufLen + 1);
3796
3797                 if (_MmsBinaryDecodeGetBytes(pFile, pData, gMmsDecodeBufLen, totalLength) == false)
3798                         goto __CATCH;
3799
3800                 returnLength += gMmsDecodeBufLen;
3801
3802                 if ((bufLen - iPos) > 0) {
3803                         readBytes = (gMmsDecodeBufLen < (bufLen - iPos)) ? gMmsDecodeBufLen : (bufLen - iPos);
3804                         if (iPos == 0 && (pData[0] == MARK)) {
3805                                 /* MARK: check first time only */
3806
3807                                 strncpy(szBuff + iPos, (char*)pData + 1, readBytes - 1);
3808                                 iPos += (readBytes - 1);
3809                         } else {
3810                                 strncpy(szBuff + iPos, (char*)pData, readBytes);
3811                                 iPos += readBytes;
3812                         }
3813                 }
3814
3815                 if (pData) {
3816                         free(pData);
3817                         pData = NULL;
3818                 }
3819
3820                 if (MsgLoadDataToDecodeBuffer(pFile, &gpCurMmsDecodeBuff, &gCurMmsDecodeBuffPos, &gMmsDecodeCurOffset,
3821                                                                                 gpMmsDecodeBuf1, gpMmsDecodeBuf2, gMmsDecodeMaxLen, &gMmsDecodeBufLen, totalLength) == false) {
3822                         MSG_DEBUG("__MmsBinaryDecodeText: 2. fail to load to buffer \n");
3823                         goto __CATCH;
3824                 }
3825                 length = strlen(gpCurMmsDecodeBuff) + 1;        // + NULL
3826         }       /* while */
3827
3828         if (length > 0) {
3829                 pData = (char *)malloc(length);
3830                 if (pData == NULL)
3831                         goto __CATCH;
3832
3833                 if (_MmsBinaryDecodeGetBytes(pFile, pData, length, totalLength) == false)
3834                         goto __CATCH;
3835
3836                 returnLength += length;
3837
3838                 if ((bufLen - iPos) > 0) {
3839                         /* read until NULL from raw data, and copy only string */
3840                         readBytes = (length < (bufLen - iPos)) ? length : (bufLen - iPos);
3841                         if (iPos == 0 && (pData[0] == MARK)) {
3842                                 /* MARK: check first time only */
3843                                 strncpy(szBuff + iPos, (char*)pData + 1, readBytes - 1);
3844                                 iPos += (readBytes - 1);
3845                         } else {
3846                                 strncpy(szBuff + iPos, (char*)pData, readBytes - 1);    // + NULL
3847                                 iPos += readBytes;
3848                         }
3849                 }
3850
3851                 if (pData) {
3852                         free(pData);
3853                         pData = NULL;
3854                 }
3855         }
3856
3857         szBuff[bufLen - 1] = '\0';
3858
3859         return returnLength;
3860
3861 __RETURN:
3862
3863         return length;
3864
3865 __CATCH:
3866
3867         if (pData) {
3868                 free(pData);
3869                 pData = NULL;
3870         }
3871
3872         return -1;
3873 }
3874
3875
3876 /**
3877  * Decode Text
3878  *
3879  * @param       pEncodedData    [in] QuotedString encoded data
3880  * @param       szBuff                  [out] Decoded quoted string
3881  * @param       bufLen                  [out] Buffer length
3882  * @return      length of decode text string
3883  */
3884 static int __MmsBinaryDecodeText(FILE *pFile, char *szBuff, int bufLen, int totalLength)
3885 {
3886         int length = 0;
3887         int readBytes = 0;
3888         int iPos = 0;
3889         int returnLength = 0;
3890         char *pData = NULL;
3891         bool bQuote = false;
3892         int offset = 0;
3893
3894         /*
3895          * Text-String = [QUOTE]*TEXT end_of_string
3896          *                               [QUOTE]*(128~255)\0
3897          *                               *(32~126)\0
3898          */
3899
3900         if (pFile == NULL || szBuff == NULL || bufLen <= 0)
3901                 return -1;
3902
3903         offset = _MmsGetDecodeOffset();
3904         if (offset >= totalLength)
3905                 goto __RETURN;
3906
3907         memset(szBuff, 0, bufLen);
3908
3909         if (MsgLoadDataToDecodeBuffer(pFile, &gpCurMmsDecodeBuff, &gCurMmsDecodeBuffPos, &gMmsDecodeCurOffset,
3910                                                                         gpMmsDecodeBuf1, gpMmsDecodeBuf2, gMmsDecodeMaxLen, &gMmsDecodeBufLen, totalLength) == false) {
3911                 MSG_DEBUG("__MmsBinaryDecodeText: 1. fail to load to buffer \n");
3912                 goto __CATCH;
3913         }
3914
3915         length = strlen(gpCurMmsDecodeBuff) + 1;        // + NULL
3916
3917         if (length == 0)
3918                 goto __RETURN;
3919
3920         while (length > gMmsDecodeBufLen) {
3921                 if (gMmsDecodeBufLen <= 0) {
3922                         MSG_DEBUG("__MmsBinaryDecodeQuotedString: gMmsDecodeBufLen <= 0 \n");
3923                         MSG_DEBUG("__MmsBinaryDecodeQuotedString: %x %x %x %x %x\n", gpCurMmsDecodeBuff[0], gpCurMmsDecodeBuff[1], gpCurMmsDecodeBuff[2],
3924                                                                                 gpCurMmsDecodeBuff[3], gpCurMmsDecodeBuff[4]);
3925                         MSG_DEBUG("__MmsBinaryDecodeQuotedString: %x %x %x %x %x\n", gpCurMmsDecodeBuff[5], gpCurMmsDecodeBuff[6], gpCurMmsDecodeBuff[7],
3926                                                                                 gpCurMmsDecodeBuff[8], gpCurMmsDecodeBuff[9]);
3927                         MSG_DEBUG("__MmsBinaryDecodeQuotedString: %x %x %x %x %x\n", gpCurMmsDecodeBuff[10], gpCurMmsDecodeBuff[11], gpCurMmsDecodeBuff[12],
3928                                                                                 gpCurMmsDecodeBuff[13], gpCurMmsDecodeBuff[14]);
3929                         MSG_DEBUG("__MmsBinaryDecodeQuotedString: %x %x %x %x %x\n", gpCurMmsDecodeBuff[15], gpCurMmsDecodeBuff[16], gpCurMmsDecodeBuff[17],
3930                                                                                 gpCurMmsDecodeBuff[18], gpCurMmsDecodeBuff[19]);
3931                         goto __CATCH;
3932                 }
3933
3934                 pData = (char *)malloc(gMmsDecodeBufLen + 1);
3935                 if (pData == NULL)
3936                         goto __CATCH;
3937
3938                 memset(pData, 0, gMmsDecodeBufLen + 1);
3939
3940                 if (_MmsBinaryDecodeGetBytes(pFile, pData, gMmsDecodeBufLen, totalLength) == false)
3941                         goto __CATCH;
3942
3943                 if ((bufLen - iPos) > 0) {
3944                         readBytes = (gMmsDecodeBufLen < (bufLen - iPos)) ? gMmsDecodeBufLen : (bufLen - iPos);
3945                         if (iPos == 0 && (pData[0] == QUOTE) && (bQuote == false)) {
3946                                 /* QUOTE: check first time only */
3947
3948                                 strncpy(szBuff + iPos, (char*)pData + 1, readBytes - 1);
3949                                 iPos += (readBytes - 1);
3950                                 bQuote = true;
3951                         } else {
3952                                 strncpy(szBuff + iPos, (char*)pData, readBytes);
3953                                 iPos += readBytes;
3954                         }
3955                 }
3956
3957                 if (pData) {
3958                         free(pData);
3959                         pData = NULL;
3960                 }
3961
3962                 returnLength += gMmsDecodeBufLen;
3963
3964                 if (MsgLoadDataToDecodeBuffer(pFile, &gpCurMmsDecodeBuff, &gCurMmsDecodeBuffPos, &gMmsDecodeCurOffset,
3965                                                                            gpMmsDecodeBuf1, gpMmsDecodeBuf2, gMmsDecodeMaxLen, &gMmsDecodeBufLen, totalLength) == false) {
3966                         MSG_DEBUG("__MmsBinaryDecodeText: 2. fail to load to buffer \n");
3967                         goto __CATCH;
3968                 }
3969                 length = strlen(gpCurMmsDecodeBuff) + 1;        // + NULL
3970         }       /* while */
3971
3972         if (length > 0) {
3973                 pData = (char *)malloc(length);
3974                 if (pData == NULL)
3975                         goto __CATCH;
3976
3977                 memset(pData, 0, length);
3978
3979                 if (_MmsBinaryDecodeGetBytes(pFile, pData, length, totalLength) == false)
3980                         goto __CATCH;
3981
3982                 if ((bufLen - iPos) > 0) {
3983                         readBytes = (length < (bufLen - iPos)) ? length : (bufLen - iPos);
3984                         if (iPos == 0 && (pData[0] == QUOTE) && (bQuote == false)) {
3985                                 /* QUOTE: check first time only */
3986
3987                                 strncpy(szBuff + iPos, (char*)pData + 1, readBytes - 1);
3988                                 iPos += (readBytes - 1);
3989                                 bQuote = true;
3990                         } else {
3991                                 strncpy(szBuff + iPos, (char*)pData, readBytes - 1);    // + NULL
3992                                 iPos += readBytes;
3993                         }
3994                 }
3995
3996                 if (pData) {
3997                         free(pData);
3998                         pData = NULL;
3999                 }
4000
4001                 returnLength += length;         // + NULL
4002         }
4003
4004         szBuff[bufLen - 1] = '\0';
4005
4006         return returnLength;
4007
4008 __RETURN:
4009
4010         szBuff[0] = '\0';
4011         length = 0;
4012
4013         MmsBinaryDecodeMovePointer(pFile, offset, totalLength);
4014
4015         return length;
4016
4017 __CATCH:
4018
4019         if (pData) {
4020                 free(pData);
4021                 pData = NULL;
4022         }
4023
4024         return -1;
4025 }
4026
4027
4028 static char* __MmsBinaryDecodeText2(FILE *pFile, int totalLength, int *pLength)
4029 {
4030         int length = 0;
4031         int curLen = 0;
4032         char *pData = NULL;
4033         char *szBuff = NULL;
4034         char *szTempPtr = NULL;
4035         bool bQuote = false;
4036         int offset = 0;
4037
4038         /*
4039          * Text-String = [QUOTE]*TEXT end_of_string
4040          *                               [QUOTE]*(128~255)\0
4041          *                               *(32~126)\0
4042          */
4043
4044         if (pFile == NULL || pLength == NULL)
4045                 goto __CATCH;
4046
4047         *pLength = 0;
4048         offset = _MmsGetDecodeOffset();
4049         if (offset >= totalLength)
4050                 goto __RETURN;
4051
4052         if (MsgLoadDataToDecodeBuffer(pFile, &gpCurMmsDecodeBuff, &gCurMmsDecodeBuffPos, &gMmsDecodeCurOffset,
4053                                                                    gpMmsDecodeBuf1, gpMmsDecodeBuf2, gMmsDecodeMaxLen, &gMmsDecodeBufLen, totalLength) == false) {
4054                 MSG_DEBUG("__MmsBinaryDecodeTextLen: 1. fail to load to buffer \n");
4055                 goto __CATCH;
4056         }
4057
4058         length = strlen(gpCurMmsDecodeBuff) + 1;
4059
4060         if (length == 0)
4061                 goto __CATCH;
4062
4063         while (length > gMmsDecodeBufLen) {
4064                 if (gMmsDecodeBufLen <= 0) {
4065                         MSG_DEBUG("__MmsBinaryDecodeQuotedString: gMmsDecodeBufLen <= 0 \n");
4066                         MSG_DEBUG("__MmsBinaryDecodeQuotedString: %x %x %x %x %x\n",
4067                                                                                 gpCurMmsDecodeBuff[0], gpCurMmsDecodeBuff[1], gpCurMmsDecodeBuff[2],
4068                                                                                 gpCurMmsDecodeBuff[3], gpCurMmsDecodeBuff[4]);
4069                         MSG_DEBUG("__MmsBinaryDecodeQuotedString: %x %x %x %x %x\n",
4070                                                                                 gpCurMmsDecodeBuff[5], gpCurMmsDecodeBuff[6], gpCurMmsDecodeBuff[7],
4071                                                                                 gpCurMmsDecodeBuff[8], gpCurMmsDecodeBuff[9]);
4072                         MSG_DEBUG("__MmsBinaryDecodeQuotedString: %x %x %x %x %x\n",
4073                                                                                 gpCurMmsDecodeBuff[10], gpCurMmsDecodeBuff[11], gpCurMmsDecodeBuff[12],
4074                                                                                 gpCurMmsDecodeBuff[13], gpCurMmsDecodeBuff[14]);
4075                         MSG_DEBUG("__MmsBinaryDecodeQuotedString: %x %x %x %x %x\n",
4076                                                                                 gpCurMmsDecodeBuff[15], gpCurMmsDecodeBuff[16], gpCurMmsDecodeBuff[17],
4077                                                                                 gpCurMmsDecodeBuff[18], gpCurMmsDecodeBuff[19]);
4078                         goto __CATCH;
4079                 }
4080
4081                 pData = (char *)malloc(gMmsDecodeBufLen + 1);
4082                 if (pData == NULL)
4083                         goto __CATCH;
4084
4085                 memset(pData, 0, gMmsDecodeBufLen + 1);
4086
4087                 if (_MmsBinaryDecodeGetBytes(pFile, pData, gMmsDecodeBufLen, totalLength) == false)
4088                         goto __CATCH;
4089
4090                 if (szBuff == NULL)     {
4091                         szBuff = (char *)malloc(gMmsDecodeBufLen + 1);
4092                 } else {
4093                         szTempPtr = (char *)realloc(szBuff, curLen + gMmsDecodeBufLen + 1);
4094
4095                         //NULL pointer check for realloc
4096                         if (szTempPtr == NULL) {
4097                                 goto __CATCH;
4098                         } else {
4099                                 szBuff = szTempPtr;
4100                         }
4101                 }
4102                 if (szBuff == NULL)
4103                         goto __CATCH;
4104
4105                 memset(szBuff + curLen, 0, gMmsDecodeBufLen + 1);
4106
4107                 if (curLen == 0 && (pData[0] == QUOTE) && (bQuote == false)) {
4108                         /* QUOTE: check first time only */
4109
4110                         strncpy(szBuff + curLen, (char*)pData + 1, gMmsDecodeBufLen - 1);
4111                         curLen += (gMmsDecodeBufLen - 1);
4112                         bQuote = true;
4113                 } else {
4114                         strncpy(szBuff + curLen, (char*)pData, gMmsDecodeBufLen);
4115                         curLen += gMmsDecodeBufLen;
4116                 }
4117
4118                 if (pData) {
4119                         free(pData);
4120                         pData = NULL;
4121                 }
4122
4123                 *pLength += gMmsDecodeBufLen;
4124
4125                 if (MsgLoadDataToDecodeBuffer(pFile,
4126                                                            &gpCurMmsDecodeBuff,
4127                                                            &gCurMmsDecodeBuffPos,
4128                                                            &gMmsDecodeCurOffset,
4129                                                            gpMmsDecodeBuf1,
4130                                                            gpMmsDecodeBuf2,
4131                                                            gMmsDecodeMaxLen,
4132                                                            &gMmsDecodeBufLen,
4133                                                            totalLength) == false)
4134                 {
4135                         MSG_DEBUG("__MmsBinaryDecodeText: 2. fail to load to buffer \n");
4136                         goto __CATCH;
4137                 }
4138                 length = strlen(gpCurMmsDecodeBuff) + 1;
4139         }       /* while */
4140
4141         if (length > 0) {
4142                 pData = (char *)malloc(length);
4143                 if (pData == NULL) {
4144                         goto __CATCH;
4145                 }
4146
4147                 if (_MmsBinaryDecodeGetBytes(pFile, pData, length, totalLength) == false) {
4148                         goto __CATCH;
4149                 }
4150
4151                 if (szBuff == NULL) {
4152                         szBuff = (char *)malloc(length);
4153                 } else {
4154                         szTempPtr = (char *)realloc(szBuff, curLen + length);
4155
4156                         //NULL pointer check for realloc
4157                         if (szTempPtr == NULL)
4158                                 goto __CATCH;
4159                         else
4160                                 szBuff = szTempPtr;
4161                 }
4162
4163                 if (szBuff == NULL) {
4164                         goto __CATCH;
4165                 }
4166
4167                 memset(szBuff + curLen, 0, length);
4168
4169                 if (curLen == 0 && (pData[0] == QUOTE)  && (bQuote == false)) {
4170                         /* QUOTE: check first time only */
4171
4172                         strncpy(szBuff + curLen, (char*)pData + 1, length - 2);
4173                         curLen += (length - 1);
4174                         bQuote = true;
4175                 } else {
4176                         strncpy(szBuff + curLen, (char*)pData, length - 1);
4177                         curLen += length;
4178                 }
4179
4180                 if (pData) {
4181                         free(pData);
4182                         pData = NULL;
4183                 }
4184
4185                 *pLength += length;             // + NULL
4186         }
4187
4188         return szBuff;
4189
4190 __RETURN:
4191
4192         *pLength = 1;
4193
4194         MmsBinaryDecodeMovePointer(pFile, offset, totalLength);
4195
4196         return szBuff;
4197
4198 __CATCH:
4199
4200         if (szBuff) {
4201                 free(szBuff);
4202                 szBuff = NULL;
4203         }
4204
4205         if (pData) {
4206                 free(pData);
4207                 pData = NULL;
4208         }
4209
4210         return NULL;
4211 }
4212
4213
4214 /**
4215  * Decode Charset
4216  *
4217  * @param       pEncodedData    [in] QuotedString encoded data
4218  * @param       nCharSet                [out] Decoded character set
4219  * @return      length of charset value
4220  */
4221 static bool __MmsBinaryDecodeCharset(FILE *pFile, UINT32 *nCharSet, int *pCharSetLen, int totalLength)
4222 {
4223         UINT32 integer = 0;
4224
4225         /*
4226          * Charset v1.1 0x01 Well-known-charset
4227          *                                       Well-known-charset = Any-charset | Integer-value
4228          *                                              ; Both are encoded using values from
4229          *                                                Character Set Assignments table in Assigned Numbers
4230          *                                       Any-charset = <Octet 128>
4231          *                                              ; Equivalent to the special RFC2616 charset value ï¿½ï¿½*��
4232          */
4233
4234         if (pFile == NULL || nCharSet == NULL || pCharSetLen == NULL)
4235                 return false;
4236
4237         if (__MmsBinaryDecodeInteger(pFile, &integer, pCharSetLen, totalLength) == false) {
4238                 MSG_DEBUG("__MmsBinaryDecodeCharset : __MmsBinaryDecodeInteger fail...\n");
4239                 goto __CATCH;
4240         }
4241
4242         if (integer == 0) {
4243                 /* AnyCharSet : return MSG_CHARSET_UTF8 */
4244                 *nCharSet = MSG_CHARSET_UTF8;
4245                 return true;
4246         }
4247
4248         *nCharSet = _MmsGetBinaryType(MmsCodeCharSet, (UINT16)integer);
4249
4250         if (*nCharSet == MIME_UNKNOWN) {
4251                 MSG_DEBUG("__MmsBinaryDecodeCharset : _MmsGetBinaryType fail..\n");
4252                 *nCharSet = MSG_CHARSET_UNKNOWN;
4253         }
4254
4255         return true;
4256
4257 __CATCH:
4258         return false;
4259 }
4260
4261
4262
4263 /**
4264  * Decode EncodedString
4265  *
4266  * @param       pEncodedData    [in] QuotedString encoded data
4267  * @param       szBuff                  [out] Decoded string buffer
4268  * @param       bufLen                  [in]  Decoded buffer length
4269  * @return      length of decoded string length
4270  */
4271 static bool __MmsBinaryDecodeEncodedString(FILE *pFile, char *szBuff, int bufLen, int totalLength)
4272 {
4273         UINT32 valueLength = 0;
4274         UINT32 charSet = 0;
4275         int charSetLen = 0;
4276         int nTemp = 0;
4277         char *pData = NULL;
4278         char *pTempData = NULL;
4279         unsigned short *mszTempStr = NULL;
4280         char *pConvertedStr = NULL;
4281
4282         MSG_DEBUG("__MmsBinaryDecodeEncodedString: decode string..\n");
4283
4284         if (pFile == NULL || szBuff == NULL || bufLen <= 0) {
4285                 MSG_DEBUG("__MmsBinaryDecodeEncodedString: invalid file or buffer\n");
4286                 goto __CATCH;
4287         }
4288
4289         /*
4290          * Encoded_string_value = Text-string | Value-length Char-set Text-String
4291          *                                                Text-string    = [Quote]*TEXT End-of-string
4292          *                                                Value-length   = 0 ~ 31
4293          */
4294
4295         memset(szBuff, 0, bufLen);
4296
4297         switch (__MmsDecodeValueLength(pFile, &valueLength, totalLength)) {
4298         case -1:
4299                 goto __CATCH;
4300
4301         case 0:
4302
4303                 /* Text-string = [Quote]*TEXT End-of-string */
4304
4305                 if (__MmsBinaryDecodeText(pFile, szBuff, bufLen, totalLength) < 0) {
4306                         MSG_DEBUG("__MmsBinaryDecodeEncodedString : 1. __MmsBinaryDecodeText fail.\n");
4307                         goto __CATCH;
4308                 }
4309                 break;
4310
4311         default:
4312
4313                 /* Value-length Charset Text_string */
4314
4315                 if (__MmsBinaryDecodeCharset(pFile, &charSet, &charSetLen, totalLength) == false) {
4316                         MSG_DEBUG("__MmsBinaryDecodeEncodedString : __MmsBinaryDecodeCharset error\n");
4317                         goto __CATCH;                   /* (valueLength + valueLengthLen) */
4318                 }
4319
4320                 nTemp = __MmsBinaryDecodeText(pFile, szBuff, bufLen, totalLength);
4321                 if (nTemp < 0) {
4322                         /* There can be some error in data - no NULL -> try again with value length */
4323
4324                         pData = (char *)malloc(valueLength - charSetLen);
4325                         if (pData == NULL) {
4326                                 MSG_DEBUG("__MmsBinaryDecodeEncodedString : pData alloc fail.\n");
4327                                 goto __CATCH;
4328                         }
4329
4330                         if (_MmsBinaryDecodeGetLongBytes(pFile, pData, valueLength - charSetLen, totalLength) == false) {
4331                                 MSG_DEBUG("__MmsBinaryDecodeEncodedString : _MmsBinaryDecodeGetLongBytes fail.\n");
4332                                 goto __CATCH;
4333                         }
4334
4335                         strncpy(szBuff, pData, bufLen - 1);
4336                 }
4337
4338                 {//temp brace
4339
4340                         nTemp = strlen(szBuff);
4341
4342                         const char *pToCharSet = "UTF-8";
4343
4344                         UINT16 charset_code =  _MmsGetBinaryValue(MmsCodeCharSet, charSet);
4345
4346                         const char *pFromCharSet = MmsPluginTextConvertGetCharSet(charset_code);
4347                         if (pFromCharSet == NULL || !strcmp(pFromCharSet, pToCharSet)) {
4348                                 return true;
4349                         }
4350
4351                         char *pDest = NULL;
4352                         int destLen = 0;
4353
4354                         if (MmsPluginTextConvert(pToCharSet, pFromCharSet, szBuff, nTemp, &pDest, &destLen) == false) {
4355                                 MSG_DEBUG("MmsPluginTextConvert Fail");
4356
4357                         } else {
4358
4359                                 memset(szBuff, 0x00, bufLen);
4360                                 snprintf(szBuff, destLen, "%s", pDest);
4361                         }
4362                 }
4363         }
4364
4365         if (pData) {
4366                 free(pData);
4367                 pData = NULL;
4368         }
4369
4370         if (pTempData) {
4371                 free(pTempData);
4372                 pTempData = NULL;
4373         }
4374
4375         if (mszTempStr) {
4376                 free(mszTempStr);
4377                 mszTempStr = NULL;
4378         }
4379
4380         if (pConvertedStr) {
4381                 free(pConvertedStr);
4382                 pConvertedStr = NULL;
4383         }
4384
4385
4386         return true;
4387
4388 __CATCH:
4389
4390         if (pData) {
4391                 free(pData);
4392                 pData = NULL;
4393         }
4394
4395         if (pTempData) {
4396                 free(pTempData);
4397                 pTempData = NULL;
4398         }
4399
4400         if (mszTempStr) {
4401                 free(mszTempStr);
4402                 mszTempStr = NULL;
4403         }
4404
4405         if (pConvertedStr) {
4406                 free(pConvertedStr);
4407                 pConvertedStr = NULL;
4408         }
4409
4410         return false;
4411 }
4412
4413
4414
4415 /**
4416  * Decode Encoded Addresses
4417  *
4418  * @param       pEncodedData    [in] QuotedString encoded data
4419  * @param       pAddrLength             [out] Decoded address length
4420  * @return      Decoded address list
4421  */
4422 MsgHeaderAddress *__MmsDecodeEncodedAddress(FILE *pFile, int totalLength)
4423 {
4424         UINT32 valueLength      = 0;
4425         UINT32 charSet          = 0;
4426         int charSetLen  = 0;
4427         int textLength  = 0;
4428         char *pAddrStr  = NULL;
4429         MsgHeaderAddress *pAddr = NULL;
4430
4431         MSG_DEBUG("__MmsDecodeEncodedAddress: decoding address..\n");
4432
4433         if (pFile == NULL) {
4434                 MSG_DEBUG("__MmsDecodeEncodedAddress: invalid file or buffer\n");
4435                 goto __CATCH;
4436         }
4437
4438         /*
4439          * Encoded_string_value = Text-string | Value-length Char-set Text-String
4440          *                                                Text-string    = [Quote]*TEXT End-of-string
4441          *                                                Value-length   = 0 ~ 31
4442          */
4443
4444         switch (__MmsDecodeValueLength(pFile, &valueLength, totalLength)) {
4445         case -1:
4446                 goto __CATCH;
4447
4448         case 0:
4449
4450                 /* Text-string = [Quote]*TEXT End-of-string */
4451
4452                 textLength = 0;
4453                 pAddrStr   = __MmsBinaryDecodeText2(pFile, totalLength, &textLength);
4454                 if (pAddrStr == NULL) {
4455                         MSG_DEBUG("__MmsDecodeEncodedAddress : 1. __MmsBinaryDecodeText2 fail.\n");
4456                         goto __CATCH;
4457                 }
4458                 break;
4459
4460         default:
4461
4462                 /* Value-length Charset Text_string */
4463
4464                 if (__MmsBinaryDecodeCharset(pFile, &charSet, &charSetLen, totalLength) == false) {
4465                         MSG_DEBUG("__MmsDecodeEncodedAddress : __MmsBinaryDecodeCharset error\n");
4466                         goto __CATCH;
4467                 }
4468
4469                 textLength = 0;
4470                 pAddrStr   = __MmsBinaryDecodeText2(pFile, totalLength, &textLength);
4471                 if (pAddrStr == NULL) {
4472                         /* There can be some error in data - no NULL -> try again with value length */
4473
4474                         pAddrStr = (char *)malloc(valueLength - charSetLen);
4475                         if (pAddrStr == NULL) {
4476                                 MSG_DEBUG("__MmsDecodeEncodedAddress : pData alloc fail.\n");
4477                                 goto __CATCH;
4478                         }
4479
4480                         if (_MmsBinaryDecodeGetLongBytes(pFile, pAddrStr, valueLength - charSetLen, totalLength) == false) {
4481                                 MSG_DEBUG("__MmsDecodeEncodedAddress : _MmsBinaryDecodeGetLongBytes fail.\n");
4482                                 goto __CATCH;
4483                         }
4484                 }
4485
4486                 /* fixme: charset transformation */
4487
4488                 break;
4489         }
4490
4491         pAddr = (MsgHeaderAddress *)malloc(sizeof(MsgHeaderAddress));
4492         if (pAddr == NULL)
4493                 goto __CATCH;
4494
4495         memset(pAddr, 0, sizeof(MsgHeaderAddress));
4496         pAddr->szAddr = pAddrStr;
4497
4498         return pAddr;
4499
4500 __CATCH:
4501
4502         if (pAddrStr) {
4503                 free(pAddrStr);
4504                 pAddrStr = NULL;
4505         }
4506
4507         return NULL;
4508 }
4509
4510
4511 /**
4512  * Decode Encoded Pointer String
4513  *
4514  * @param       pEncodedData    [in] Long integer encoded data
4515  * @param       pLongInteger    [out] Decoded long integer
4516  * @return      Decoded address list
4517  */
4518 static bool __MmsDecodeLongInteger(FILE *pFile, UINT32 *pLongInteger, int totalLength)
4519 {
4520         UINT8 oneByte = 0;
4521
4522         /*
4523          * Long-integer = Short-length Multi-octet-integer
4524          *                                Short-length = 0~30
4525          *                                Multi-octet-integer
4526          */
4527
4528         if (pFile == NULL || pLongInteger == NULL)
4529                 return false;
4530
4531         *pLongInteger = 0;
4532
4533         if (_MmsBinaryDecodeGetOneByte(pFile, &oneByte, totalLength) == false)
4534                 goto __CATCH;
4535
4536         if (oneByte > 31)
4537                 goto __CATCH;
4538
4539         *pLongInteger = __MmsHeaderDecodeIntegerByLength(pFile, oneByte, totalLength);
4540
4541         return true;
4542
4543 __CATCH:
4544         return false;
4545 }
4546
4547
4548 /*
4549  * @param       pEncodedData    [in] filename encoded data
4550  * @param       szBuff                  [out] filename output buffer
4551  * @param       fullLength              [in] full filename length
4552  * @param       bufLen                  [in] buffer length
4553  * CAUTION: bufLen - 1
4554  */
4555 static int __MmsDecodeGetFilename(FILE *pFile, char *szBuff, int bufLen, int totalLength)
4556 {
4557         char *pUTF8Buff = NULL;
4558         char *pLatinBuff = NULL;
4559         char *pExt = NULL;
4560         char *szSrc = NULL;
4561         char *szSrc2 = NULL;
4562         int length = 0;
4563         int textLength = 0;
4564
4565         char *pTmpBuff = NULL;
4566
4567         memset (szBuff, 0, bufLen);
4568
4569         textLength = 0;
4570         pLatinBuff  = __MmsBinaryDecodeText2(pFile, totalLength, &textLength);
4571
4572         //remove ""
4573         if (pLatinBuff) {
4574                 szSrc = MsgRemoveQuoteFromFilename(pLatinBuff);
4575                 if (szSrc) {
4576                         strcpy(pLatinBuff, szSrc);
4577                         free(szSrc);
4578                         szSrc = NULL;
4579                 }
4580
4581                 szSrc2 = MsgChangeHexString(pLatinBuff);
4582                 if (szSrc2) {
4583                         strcpy(pLatinBuff, szSrc2);
4584                         free(szSrc2);
4585                         szSrc2 = NULL;
4586                 }
4587
4588                 if (MsgIsUTF8String((unsigned char*)pLatinBuff, strlen(pLatinBuff)) == false) {
4589                         length = strlen(pLatinBuff);
4590
4591                         int             utf8BufSize = 0;
4592                         utf8BufSize = MsgGetLatin2UTFCodeSize((unsigned char*)pLatinBuff, length);
4593                         if (utf8BufSize < 3)
4594                                 utf8BufSize = 3;//min value
4595
4596                         pUTF8Buff = (char *)malloc(utf8BufSize + 1);
4597                         if (pUTF8Buff == NULL) {
4598                                 MSG_DEBUG("__MmsDecodeGetFilename: pUTF8Buff alloc fail \n");
4599                                 goto __CATCH;
4600                         }
4601
4602                         if (MsgLatin2UTF ((unsigned char*)pUTF8Buff, utf8BufSize + 1, (unsigned char*)pLatinBuff, length) < 0) {
4603                                 MSG_DEBUG("__MmsDecodeGetFilename: MsgLatin2UTF fail \n");
4604                                 goto __CATCH;
4605                         }
4606                         free(pLatinBuff);
4607                         pLatinBuff = NULL;
4608                 } else {
4609                         pTmpBuff = _MsgDecodeText(pLatinBuff);
4610                         pUTF8Buff = pTmpBuff;
4611                         free (pLatinBuff);
4612                         pLatinBuff = NULL;
4613                 }
4614         }
4615
4616         if (pUTF8Buff) {
4617                 /*
4618                  * keeping extension
4619                  * it should be kept extention even if the file name is shorten
4620                  */
4621
4622                 length = strlen(pUTF8Buff);
4623                 if ((pExt = strrchr(pUTF8Buff, '.')) != NULL) {
4624                         int nameLength  = 0;
4625                         nameLength = (length < bufLen) ? (length - strlen(pExt)) : (bufLen - strlen(pExt));
4626                         strncpy(szBuff, pUTF8Buff, nameLength);
4627                         strcat (szBuff, pExt);
4628                 } else {
4629                         strncpy(szBuff, pUTF8Buff, bufLen - 1);
4630                 }
4631                 free(pUTF8Buff);
4632                 pUTF8Buff = NULL;
4633
4634                 return textLength;
4635         }
4636
4637 __CATCH:
4638
4639         if (pLatinBuff) {
4640                 free(pLatinBuff);
4641                 pLatinBuff = NULL;
4642         }
4643
4644         if (pUTF8Buff) {
4645                 free(pUTF8Buff);
4646                 pUTF8Buff = NULL;
4647         }
4648
4649         return -1;
4650 }
4651
4652
4653 /* ==========================================================
4654
4655            T  E  X  T         D  E  C  O  D  I  N  G
4656
4657    ==========================================================*/
4658
4659
4660 bool MmsTextDecodeMsgBody(FILE *pFile)
4661 {
4662         MSG_DEBUG("MmsTextDecodeMsgBody: \n");
4663         return false;
4664 }
4665
4666 bool __MmsTextDecodeMsgHeader(FILE *pFile)
4667 {
4668         return true;
4669 }
4670
4671
4672 /* ==========================================================
4673
4674            M  M  S        D  E  C  O  D  I  N  G
4675
4676    ==========================================================*/
4677
4678 //  to get message body this function should be modified from message raw file.
4679 bool _MmsReadMsgBody(msg_message_id_t msgID, bool bSavePartsAsTempFiles, bool bRetrieved, char *retrievedPath)
4680 {
4681         FILE *pFile     = NULL;
4682         MmsMsg *pMsg = NULL;
4683         MsgMultipart *pMultipart = NULL;
4684         int nSize = 0;
4685         char szFullPath[MSG_FILEPATH_LEN_MAX] = {0, };
4686         char szTempMediaDir[MSG_FILEPATH_LEN_MAX] = {0, };
4687         int attachmax = MSG_ATTACH_MAX;
4688
4689         MSG_DEBUG("_MmsReadMsgBody: start read msg(msgID=%d)\n", msgID);
4690
4691         MmsPluginStorage::instance()->getMmsMessage(&pMsg);
4692         memset(pMsg, 0, sizeof(MmsMsg));
4693
4694         _MmsInitHeader();
4695
4696         if (bRetrieved && (retrievedPath != NULL)) {
4697                 strncpy(szFullPath, retrievedPath, (strlen(retrievedPath) > MSG_FILEPATH_LEN_MAX ? MSG_FILEPATH_LEN_MAX:strlen(retrievedPath)));
4698         } else {
4699                 MmsPluginStorage::instance()->getMmsRawFilePath(msgID, szFullPath);
4700         }
4701
4702         pMsg->msgID = msgID;
4703
4704         /*      read from MMS raw file  */
4705         strncpy(pMsg->szFileName, szFullPath + strlen(MSG_DATA_PATH), strlen(szFullPath + strlen(MSG_DATA_PATH)));
4706
4707         MSG_DEBUG("szFullPath = (%s)", szFullPath);
4708
4709         if (MsgGetFileSize(szFullPath, &nSize) == false) {
4710                 MSG_DEBUG("MsgGetFileSize: failed");
4711                 goto __CATCH;
4712         }
4713
4714         pFile = MsgOpenFile(szFullPath, "rb");
4715
4716         if (pFile == NULL) {
4717                 MSG_DEBUG("_MmsReadMsgBody: invalid mailbox\n");
4718                 goto __CATCH;
4719         }
4720
4721         _MmsRegisterDecodeBuffer(gszMmsLoadBuf1, gszMmsLoadBuf2, MSG_MMS_DECODE_BUFFER_MAX);
4722
4723         if (MmsBinaryDecodeMsgHeader(pFile, nSize) == false) {
4724                 MSG_DEBUG("_MmsReadMsgBody: MmsBinaryDecodeMsgHeader fail...\n");
4725                 goto __CATCH;
4726         }
4727
4728 #ifdef  __SUPPORT_DRM__
4729         if (MmsDrm2GetConvertState() != MMS_DRM2_CONVERT_FINISH)
4730                 MmsDrm2SetConvertState(MMS_DRM2_CONVERT_NONE);  //initialize convertState
4731 #endif
4732
4733         if (MmsBinaryDecodeMsgBody(pFile, szFullPath, nSize) == false) {
4734                 MSG_DEBUG("_MmsReadMsgBody: MmsBinaryDecodeMsgBody fail\n");
4735                 goto __CATCH;
4736         }
4737
4738 #ifdef  __SUPPORT_DRM__
4739         if (MmsDrm2GetConvertState() == MMS_DRM2_CONVERT_REQUIRED) {
4740                 MSG_DEBUG("_MmsReadMsgBody: MmsDrm2GetConvertState returns MMS_DRM2_CONVERT_REQUIRED.\n");
4741                 goto RETURN;
4742         }
4743 #endif
4744
4745         /* Set mmsHeader.msgType & msgBody to pMsg ----------- */
4746
4747         pMsg->mmsAttrib.contentType = (MsgContentType)mmsHeader.msgType.type;
4748
4749         memcpy(&(pMsg->msgType), &(mmsHeader.msgType), sizeof(MsgType));
4750         memcpy(&(pMsg->msgBody), &(mmsHeader.msgBody), sizeof(MsgBody));
4751
4752         if (pMsg->msgBody.pPresentationBody) {
4753                 if(MsgFseek(pFile, pMsg->msgBody.pPresentationBody->offset, SEEK_SET) < 0)
4754                         goto __CATCH;
4755
4756                 pMsg->msgBody.pPresentationBody->body.pText = (char *)malloc(pMsg->msgBody.pPresentationBody->size + 1);
4757                 if (pMsg->msgBody.pPresentationBody->body.pText == NULL)
4758                         goto __CATCH;
4759
4760                 memset(pMsg->msgBody.pPresentationBody->body.pText, 0, pMsg->msgBody.pPresentationBody->size + 1);
4761
4762                 ULONG nRead = 0;
4763                 nRead = MsgReadFile(pMsg->msgBody.pPresentationBody->body.pText, sizeof(char), pMsg->msgBody.pPresentationBody->size, pFile);
4764                 if (nRead == 0)
4765                         goto __CATCH;
4766
4767         }
4768
4769         MsgCloseFile(pFile);
4770         pFile = NULL;
4771
4772         /* nPartCount */
4773         pMsg->nPartCount = 0;
4774
4775         if (MsgIsMultipart(mmsHeader.msgType.type) == true) {
4776                 pMultipart = pMsg->msgBody.body.pMultipart;
4777                 while (pMultipart) {
4778                         pMsg->nPartCount++;
4779
4780                         if (pMultipart->type.type == MIME_TEXT_PLAIN)
4781                                 attachmax++;
4782
4783                         if ((mmsHeader.msgType.type == MIME_APPLICATION_VND_WAP_MULTIPART_MIXED)||(mmsHeader.msgType.type == MIME_MULTIPART_MIXED)) {
4784                                 if ((pMsg->nPartCount >= attachmax)&&(pMultipart->pNext != NULL)) {
4785                                         _MsgFreeBody(pMultipart->pNext->pBody, pMultipart->pNext->type.type);
4786
4787                                         free(pMultipart->pNext->pBody);
4788                                         pMultipart->pNext->pBody= NULL;
4789
4790                                         free(pMultipart->pNext);
4791
4792                                         pMultipart->pNext = NULL;
4793                                         break;
4794                                 }
4795                         }
4796                         pMultipart = pMultipart->pNext;
4797                 }
4798         } else {
4799                 if (pMsg->msgBody.size > 0)
4800                         pMsg->nPartCount++;
4801         }
4802
4803         /*      make temporary  */
4804         snprintf(szTempMediaDir, MSG_FILEPATH_LEN_MAX, MSG_DATA_PATH"%s.dir", pMsg->szFileName);
4805
4806         ///////////////////////////////////////////////
4807         // call before processing urgent event.
4808         //_MmsInitHeader();
4809         //_MmsUnregisterDecodeBuffer();
4810         ///////////////////////////////////////////////
4811
4812         if (MsgIsMultipart(pMsg->msgType.type) == true) {
4813                 int partIndex = 0;
4814                 pMultipart = pMsg->msgBody.body.pMultipart;
4815
4816                 if (bSavePartsAsTempFiles) {
4817                         if (mkdir(szTempMediaDir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0) {
4818                                 if (errno == EEXIST) {
4819                                         MSG_DEBUG("The %s already exists", szTempMediaDir);
4820                                 } else {
4821                                         MSG_DEBUG("Fail to Create Dir [%s]", szTempMediaDir);
4822                                         goto __CATCH;
4823                                 }
4824                         }
4825                 }
4826
4827                 while (pMultipart) {
4828
4829                         if (_MmsMultipartSaveAsTempFile(&pMultipart->type, pMultipart->pBody,
4830                                                                                         (char*)MSG_DATA_PATH, pMsg->szFileName, partIndex, bSavePartsAsTempFiles) == false)
4831                                 goto __CATCH;
4832
4833                         pMultipart = pMultipart->pNext;
4834                         partIndex ++;
4835                 }
4836
4837         } else { //single part
4838                 if (pMsg->nPartCount > 0) {
4839
4840                         if (bSavePartsAsTempFiles) {
4841                                 if (mkdir(szTempMediaDir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0) {
4842                                         if (errno == EEXIST) {
4843                                                 MSG_DEBUG("The %s already exists", szTempMediaDir);
4844                                         } else {
4845                                                 MSG_DEBUG("Fail to Create Dir [%s]", szTempMediaDir);
4846                                                 goto __CATCH;
4847                                         }
4848                                 }
4849                         }
4850
4851                         if (_MmsMultipartSaveAsTempFile( &pMsg->msgType, &pMsg->msgBody,
4852                                                                                         (char*)MSG_DATA_PATH, pMsg->szFileName, 0, bSavePartsAsTempFiles) == false)
4853                                 goto __CATCH;
4854                 }
4855         }
4856         MSG_DEBUG("****   _MmsReadMsgBody:  E  N  D   (Success)  ***\n");
4857         return true;
4858
4859 #ifdef  __SUPPORT_DRM__
4860
4861 RETURN:
4862
4863         if (pFile != NULL) {
4864                 MsgCloseFile(pFile);
4865                 pFile = NULL;
4866         }
4867
4868         return false;
4869
4870 #endif
4871
4872 __CATCH:
4873
4874         _MmsInitHeader();
4875         _MmsUnregisterDecodeBuffer();
4876
4877         if (pFile != NULL) {
4878                 MsgCloseFile(pFile);
4879                 pFile = NULL;
4880         }
4881
4882 #ifdef __SUPPORT_DRM__
4883         _MsgFreeDRMInfo(&pMsg->msgType.drmInfo);
4884 #endif
4885
4886         _MsgFreeBody(&pMsg->msgBody, pMsg->msgType.type);
4887         MSG_DEBUG("_MmsReadMsgBody:    E  N  D    (fail)    ******************** \n");
4888
4889         return false;
4890 }
4891
4892
4893 bool MsgFreeHeaderAddress(MsgHeaderAddress *pAddr)
4894 {
4895         MsgHeaderAddress *pTempAddr = NULL;
4896
4897         while (pAddr != NULL) {
4898                 pTempAddr = pAddr;
4899                 pAddr = pAddr->pNext;
4900
4901                 if (pTempAddr->szAddr) {
4902                         free(pTempAddr->szAddr);
4903                         pTempAddr->szAddr = NULL;
4904                 }
4905
4906                 free(pTempAddr);
4907                 pTempAddr = NULL;
4908         }
4909
4910         return true;
4911 }
4912
4913 bool MsgCheckFileNameHasInvalidChar(char *szName)
4914 {
4915         int     strLen = 0;
4916         int i = 0;
4917
4918         strLen = strlen(szName);
4919
4920         for (i=0; i<strLen; i++) {
4921                 if (__MsgIsInvalidFileNameChar(szName[i]))
4922                         return true;
4923         }
4924
4925         return false;
4926 }
4927
4928 bool _MsgReplaceInvalidFileNameChar(char *szInText, char replaceChar)
4929 {
4930         int nCount = 0;
4931         int totalLength = 0;
4932
4933         totalLength = strlen(szInText);
4934
4935         while ((*(szInText+nCount) != '\0') && (nCount < totalLength)) {
4936                 if (0x0001 <= *(szInText+nCount) && *(szInText+nCount) <= 0x007F) {
4937                         if (__MsgIsInvalidFileNameChar(szInText[nCount]))
4938                                 *(szInText+nCount) = replaceChar;
4939
4940                         nCount += 1;
4941                 } else {
4942                         nCount += 2;
4943                 }
4944         }
4945
4946         return true;
4947 }
4948
4949 char *_MsgGetStringUntilDelimiter(char *pszString, char delimiter)
4950 {
4951         char *pszBuffer = NULL;
4952         char *pszStrDelimiter = NULL;
4953         int     bufLength = 0;
4954
4955         if (!pszString) {
4956                 MSG_DEBUG("_MsgGetStringUntilDelimiter: pszString == NULL \n");
4957                 return NULL;
4958         }
4959
4960         if ((pszStrDelimiter = strchr(pszString, delimiter)) == NULL) {
4961                 MSG_DEBUG("_MsgGetStringUntilDelimiter: There is no %c in %s. \n", delimiter, pszString);
4962                 return NULL;
4963         }
4964
4965         bufLength = pszStrDelimiter - pszString;
4966
4967         if ((pszBuffer = (char*)malloc (bufLength + 1)) == NULL) {
4968                 MSG_DEBUG("malloc is failed");
4969                 return NULL;
4970         }
4971         memset(pszBuffer, 0, bufLength + 1) ;
4972
4973         strncat(pszBuffer, pszString, bufLength);
4974
4975         return pszBuffer;
4976 }
4977
4978 char *MsgChangeHexString(char *pOrg)
4979 {
4980         char *pNew = NULL;
4981         char szBuf[10] = {0,};
4982         char OneChar;
4983         int cLen = 0;
4984         int cIndex =0;
4985         int index = 0;
4986
4987         if (pOrg == NULL)
4988                 return false;
4989
4990         cLen = strlen(pOrg);
4991
4992         pNew = (char *)malloc(cLen + 1);
4993         if (pNew == NULL)
4994                 return NULL;
4995
4996         memset(pNew, 0, cLen + 1);
4997
4998         for (cIndex = 0; cIndex< cLen ; cIndex++) {
4999                 if (pOrg[cIndex] == '%') {
5000                         if (pOrg[cIndex+1] != 0 && pOrg[cIndex+2] != 0)         {
5001                                 snprintf(szBuf, sizeof(szBuf), "%c%c", pOrg[cIndex+1], pOrg[cIndex+2]); // read two chars after '%'
5002
5003                                 if (MsgIsHexChar(szBuf) == true) { // check the two character is between  0 ~ F
5004                                         OneChar = _MsgConvertHexValue(szBuf);
5005
5006                                         pNew[index] = OneChar;
5007                                         index++;
5008                                         cIndex+= 2;
5009                                         continue;
5010                                 }
5011                         }
5012                 }
5013                 pNew[index++] = pOrg[cIndex];
5014         }
5015         return pNew;
5016 }
5017
5018 bool _MsgParseParameter(MsgType *pType, char *pSrc)
5019 {
5020         char *pName = NULL;
5021         char *pValue = NULL;
5022         char *pDec = NULL;
5023         char *pTest = NULL;
5024         char *pNextParam = NULL;
5025         char *pExt = NULL;
5026         int nameLen = 0;
5027         int count;
5028         char *pTempNextParam = NULL;
5029         char *pCh = NULL;
5030         char *szSrc = NULL;
5031         char *pUTF8Buff = NULL;
5032
5033         while (pSrc != NULL) {
5034                 pSrc = _MsgSkipWS(pSrc);
5035                 if (pSrc == NULL) {
5036                         /* End of parse parameter */
5037                         return true;
5038                 }
5039
5040                 pNextParam = NULL;
5041                 pTempNextParam = strchr(pSrc, MSG_CH_SEMICOLON);
5042                 pCh = pSrc;
5043
5044                 if (*pCh == MSG_CH_QUOT) {
5045                         count = 1;
5046                 } else {
5047                         count = 0;
5048                 }
5049
5050                 pCh++;
5051                 for (; pCh<=pTempNextParam ; pCh++) {
5052                         if (*pCh == MSG_CH_QUOT)
5053                                 if (*(pCh - 1) != '\\')
5054                                         count++;
5055                 }
5056
5057                 if (count%2 == 0)
5058                         pNextParam = pTempNextParam;
5059
5060                 if (pNextParam)
5061                         *pNextParam++ = MSG_CH_NULL;
5062
5063                 if ((pName = strchr(pSrc, MSG_CH_EQUAL)) != NULL) {
5064                         *pName++ = MSG_CH_NULL;
5065
5066                         if ((pValue = strchr(pName, MSG_CH_QUOT))!= NULL) {
5067                                 *pValue++ = MSG_CH_NULL;
5068
5069                                 if ((pTest = strchr(pValue, MSG_CH_QUOT)) != NULL)
5070                                         *pTest = MSG_CH_NULL;
5071
5072                                 pDec = _MsgDecodeText(pValue);          // Api is to long, consider Add to another file (MsgMIMECodec.c)
5073                         } else {
5074                                 pDec = _MsgDecodeText(pName);
5075                         }
5076
5077                         switch (_MsgGetCode(MSG_PARAM, pSrc)) {
5078                         case MSG_PARAM_BOUNDARY:
5079
5080                                 /* RFC 822: boundary := 0*69<bchars> bcharsnospace */
5081
5082                                 memset (pType->param.szBoundary, 0, MSG_BOUNDARY_LEN + 1);
5083                                 strncpy(pType->param.szBoundary, pDec, MSG_BOUNDARY_LEN);
5084                                 MSG_DEBUG("_MsgParseParameter: szBoundary = %s \n", pType->param.szBoundary);
5085                                 break;
5086
5087                         case MSG_PARAM_CHARSET:
5088                                 pType->param.charset = _MsgGetCode(MSG_CHARSET, pDec);
5089
5090                                 if (pType->param.charset == INVALID_HOBJ)
5091                                         pType->param.charset = MSG_CHARSET_UNKNOWN;
5092
5093                                 MSG_DEBUG("_MsgParseParameter: type = %d    [charset] = %d \n", pType->type, pType->param.charset);
5094                                 break;
5095
5096                         case MSG_PARAM_NAME:
5097
5098                                 memset (pType->param.szName, 0, MSG_LOCALE_FILENAME_LEN_MAX + 1);
5099
5100                                 pUTF8Buff = MsgConvertLatin2UTF8FileName(pDec);
5101
5102                                 if (pUTF8Buff) {
5103                                         if ((pExt = strrchr(pUTF8Buff, '.')) != NULL) {
5104                                                 if ((MSG_LOCALE_FILENAME_LEN_MAX-1) < strlen(pUTF8Buff)) {
5105                                                         nameLen = (MSG_LOCALE_FILENAME_LEN_MAX-1) - strlen(pExt);
5106                                                 } else {
5107                                                         nameLen = strlen(pUTF8Buff) - strlen(pExt);
5108                                                 }
5109
5110                                                 strncpy(pType->param.szName, pUTF8Buff, nameLen);
5111                                                 strcat (pType->param.szName, pExt);
5112                                         } else {
5113                                                 strncpy(pType->param.szName, pUTF8Buff, (MSG_LOCALE_FILENAME_LEN_MAX-1));
5114                                         }
5115                                         free(pUTF8Buff);
5116                                         pUTF8Buff = NULL;
5117
5118                                         if (_MsgChangeSpace(pType->param.szName, &szSrc) == true) {
5119                                                 if (szSrc)
5120                                                         strncpy(pType->param.szName, szSrc , strlen(szSrc));
5121                                         }
5122
5123                                         if (szSrc) {
5124                                                 free(szSrc);
5125                                                 szSrc = NULL;
5126                                         }
5127
5128                                         // Remvoe '/', ex) Content-Type: image/gif; name="images/vf7.gif"
5129                                         _MsgRemoveFilePath(pType->param.szName);
5130                                 } else {
5131                                         MSG_DEBUG("_MsgParseParameter: MsgConvertLatin2UTF8FileName(%s) return NULL\n", pDec);
5132                                 }
5133
5134                                 MSG_DEBUG("_MsgParseParameter: szName = %s \n", pType->param.szName);
5135                                 break;
5136
5137                         case MSG_PARAM_FILENAME:
5138
5139                                 memset (pType->param.szFileName, 0, MSG_FILENAME_LEN_MAX+1);
5140
5141                                 pUTF8Buff = MsgConvertLatin2UTF8FileName(pDec);
5142
5143                                 if (pUTF8Buff) {
5144                                         if ((pExt = strrchr(pUTF8Buff, '.')) != NULL) {
5145                                                 if ((MSG_FILENAME_LEN_MAX-1) < strlen(pUTF8Buff)) {
5146                                                         nameLen = (MSG_FILENAME_LEN_MAX-1) - strlen(pExt);
5147                                                 } else {
5148                                                         nameLen = strlen(pUTF8Buff) - strlen(pExt);
5149                                                 }
5150
5151                                                 strncpy(pType->param.szFileName, pUTF8Buff, nameLen);
5152                                                 strcat (pType->param.szFileName, pExt);
5153                                         } else {
5154                                                 strncpy(pType->param.szFileName, pUTF8Buff, (MSG_FILENAME_LEN_MAX-1));
5155                                         }
5156                                         free(pUTF8Buff);
5157                                         pUTF8Buff = NULL;
5158
5159                                         if (_MsgChangeSpace(pType->param.szFileName, &szSrc) == true)
5160                                                 strcpy(pType->param.szFileName, szSrc);
5161
5162                                         if (szSrc) {
5163                                                 free(szSrc);
5164                                                 szSrc = NULL;
5165                                         }
5166
5167                                         // Remvoe '/', ex) Content-Type: image/gif; name="images/vf7.gif"
5168                                         _MsgRemoveFilePath(pType->param.szFileName);
5169                                 } else {
5170                                         MSG_DEBUG("_MsgParseParameter: MsgConvertLatin2UTF8FileName(%s) return NULL\n", pDec);
5171                                 }
5172
5173                                 MSG_DEBUG("_MsgParseParameter: szFileName = %s \n", pType->param.szFileName);
5174
5175                                 break;
5176
5177                         case MSG_PARAM_TYPE:
5178
5179                                 /* type/subtype of root. Only if content-type is multipart/related */
5180
5181                                 pType->param.type = _MsgGetCode(MSG_TYPE, pDec);
5182                                 MSG_DEBUG("_MsgParseParameter: type = %d \n", pType->param.type);
5183
5184                                 break;
5185
5186                         case MSG_PARAM_START:
5187
5188                                 /* Content-id. Only if content-type is multipart/related */
5189
5190                                 memset (pType->param.szStart, 0, MSG_MSG_ID_LEN + 1);
5191                                 strncpy(pType->param.szStart, pDec, MSG_MSG_ID_LEN);
5192
5193                                 MSG_DEBUG("_MsgParseParameter: szStart = %s \n", pType->param.szStart);
5194
5195                                 break;
5196
5197                         case MSG_PARAM_START_INFO :
5198
5199                                 /* Only if content-type is multipart/related */
5200
5201                                 memset (pType->param.szStartInfo, 0, MSG_MSG_ID_LEN + 1);
5202                                 strncpy(pType->param.szStartInfo, pDec, MSG_MSG_ID_LEN);
5203
5204                                 MSG_DEBUG("_MsgParseParameter: szStartInfo = %s \n", pType->param.szStartInfo);
5205
5206                                 break;
5207
5208                         case MSG_PARAM_REPORT_TYPE :
5209
5210                                 //  only used as parameter of Content-Type: multipart/report; report-type=delivery-status;
5211
5212                                 if (pDec != NULL && strcasecmp(pDec, "delivery-status") == 0) {
5213                                         pType->param.reportType = MSG_PARAM_REPORT_TYPE_DELIVERY_STATUS;
5214                                 } else {
5215                                         pType->param.reportType = MSG_PARAM_REPORT_TYPE_UNKNOWN;
5216                                 }
5217
5218                                 MSG_DEBUG("_MsgParseParameter: reportType = %s \n", pDec);
5219                                 break;
5220
5221                         default:
5222
5223                                 MSG_DEBUG("_MsgParseParameter: Unknown paremeter (%s)\n", pDec);
5224                                 break;
5225                         }
5226
5227                         if (pDec) {
5228                                 free(pDec);
5229                                 pDec = NULL;
5230                         }
5231                 }
5232                 pSrc = pNextParam;
5233         }
5234         return true;
5235 }
5236
5237 char *_MsgSkipWS(char *s)
5238 {
5239         while (true) {
5240                 if ((*s == MSG_CH_CR) || (*s == MSG_CH_LF) || (*s == MSG_CH_SP) || (*s == MSG_CH_TAB)) {
5241                         ++s;
5242                 } else if ((*s != '(') || (__MsgSkipComment(s,(long)NULL)==NULL)) {
5243                         return s;
5244                 }
5245         }
5246 }
5247
5248 char *__MsgSkipComment (char *s,long trim)
5249 {
5250
5251         char *ret;
5252         char *s1 = s;
5253         char *t = NULL;
5254
5255         // ignore empty space
5256         for (ret = ++s1; *ret == ' '; ret++)
5257                 ;
5258
5259         // handle '(', ')', '\',  '\0'
5260         do {
5261                 switch (*s1) {
5262                 case '(':
5263                         if (!__MsgSkipComment (s1,(long)NULL))
5264                                 return NULL;
5265                         t = --s1;
5266                         break;
5267                 case ')':
5268                         s = ++s1;
5269                         if (trim) {
5270                                 if (t) {
5271                                         t[1] = '\0';
5272                                 } else {
5273                                         *ret = '\0';
5274                                 }
5275                         }
5276                         return ret;
5277                 case '\\':
5278                         if (*++s1)
5279                                 break;
5280                 case '\0':
5281                         *s = '\0';
5282                         return NULL;
5283                 case ' ':
5284                         break;
5285                 default:
5286                         t = s1;
5287                         break;
5288                 }
5289         }while (s1++);
5290
5291         return NULL;
5292 }
5293
5294 char *MsgConvertLatin2UTF8FileName(char *pSrc)
5295 {
5296         char *pUTF8Buff  = NULL;
5297         char *pData = NULL;
5298
5299
5300         //convert utf8 string
5301         if (MsgIsUTF8String((unsigned char*)pSrc, strlen(pSrc)) == false) {
5302                 int length  = 0;
5303                 int utf8BufSize = 0;
5304
5305                 length = strlen(pSrc);
5306                 utf8BufSize = MsgGetLatin2UTFCodeSize((unsigned char*)pSrc, length);
5307                 if (utf8BufSize < 3)
5308                         utf8BufSize = 3; //min value
5309
5310                 pUTF8Buff = (char *)malloc(utf8BufSize + 1);
5311
5312                 if (pUTF8Buff == NULL) {
5313                         MSG_DEBUG("MsgConvertLatin2UTF8FileName: pUTF8Buff alloc fail \n");
5314                         goto __CATCH;
5315                 }
5316
5317                 if (MsgLatin2UTF ((unsigned char*)pUTF8Buff, utf8BufSize + 1, (unsigned char*)pSrc, length) < 0) {
5318                         MSG_DEBUG("MsgConvertLatin2UTF8FileName: MsgLatin2UTF fail \n");
5319                         goto __CATCH;
5320                 }
5321         } else {
5322                 int length = strlen(pSrc);
5323                 pUTF8Buff = (char *)calloc(1, length+1);
5324
5325                 if (pUTF8Buff == NULL) {
5326                         MSG_DEBUG("MsgConvertLatin2UTF8FileName: pUTF8Buff alloc fail \n");
5327                         goto __CATCH;
5328                 }
5329
5330                 memcpy(pUTF8Buff, pSrc, length);
5331         }
5332
5333         //convert hex string
5334         if (MsgIsPercentSign(pUTF8Buff) == true) {
5335                 pData = MsgChangeHexString(pUTF8Buff);
5336                 if (pData) {
5337                         strcpy(pUTF8Buff, pData);
5338                         free(pData);
5339                         pData = NULL;
5340                 }
5341         }
5342
5343         return pUTF8Buff;
5344
5345 __CATCH:
5346
5347         if (pUTF8Buff) {
5348                 free(pUTF8Buff);
5349                 pUTF8Buff = NULL;
5350         }
5351
5352         return NULL;
5353 }
5354
5355 bool _MsgChangeSpace(char *pOrg, char **ppNew)
5356 {
5357         char *pNew = NULL;
5358         int cLen = 0;
5359         int cIndex =0;
5360         int index = 0;
5361
5362         if (pOrg == NULL)
5363                 return false;
5364
5365         cLen = strlen(pOrg);
5366
5367         pNew = (char *)malloc(cLen + 1);
5368         if (pNew == NULL)
5369                 return false;
5370
5371         memset(pNew, 0, cLen + 1);
5372
5373         for (cIndex=0; cIndex<cLen;cIndex++) {
5374                 if (pOrg[cIndex] == '%' && pOrg[cIndex+1] == '2' && pOrg[cIndex+2] == '0') {
5375                         pNew[index] = ' ';
5376                         index++;
5377                         cIndex+= 2;
5378                         continue;
5379                 }
5380                 pNew[index++] = pOrg[cIndex];
5381         }
5382
5383         *ppNew = pNew;
5384
5385         return true;
5386 }
5387
5388 void _MsgRemoveFilePath(char *pSrc)
5389 {
5390         // Remvoe '/', ex) Content-Type: image/gif; name="images/vf7.gif"
5391         char *pPath = NULL;
5392         char *pTemp = NULL;
5393         char szFileName[MSG_FILENAME_LEN_MAX] = {0};
5394
5395         if (pSrc == NULL)
5396                 return;
5397
5398         pTemp = pSrc;
5399         while ((pTemp = strchr(pTemp, '/')) != NULL) {
5400                 // Find the last  '/'
5401                 pPath = pTemp;
5402                 pTemp++;
5403         }
5404
5405         if (pPath) {
5406                 MSG_DEBUG("_MsgRemoveFilePath: filename(%s)\n", pSrc);
5407
5408                 // case : images/vf7.gif -> vf7.gif
5409                 if (pPath != NULL && *(pPath+1) != '\0') {
5410                         strncpy(szFileName, pPath+1, strlen(pPath+1));
5411                         strncpy(pSrc, szFileName , strlen(szFileName));
5412                 }
5413         }
5414         // Remove additional file information
5415         // ex) Content-type: application/octet-stream; name="060728gibson_210.jpg?size=s"
5416         // if "?size=" exist, insert NULL char.
5417         {
5418                 pTemp = strcasestr(pSrc, "?size=");
5419                 if (pTemp != NULL)
5420                         *pTemp = '\0';
5421         }
5422 }
5423
5424 bool MsgIsUTF8String(unsigned char *szSrc, int nChar)
5425 {
5426         MSG_DEBUG("MsgIsUTF8String: --------------- \n");
5427
5428         if (szSrc == NULL) {
5429                 MSG_DEBUG("MsgIsUTF8String: szSrc is NULL !!!! --------------- \n");
5430                 return true;
5431         }
5432
5433         while (nChar > 0 && (*szSrc != '\0')) {
5434                 if (*szSrc < 0x80) {
5435                         szSrc++;
5436                         nChar--;
5437                 } else if ((0xC0 <= *szSrc) && (*szSrc < 0xE0)) {
5438                         if (*(szSrc + 1) >= 0x80) {
5439                                 szSrc += 2;
5440                                 nChar -= 2;
5441                         } else {
5442                                 MSG_DEBUG("MsgIsUTF8String: 1. NOT utf8 range!\n");
5443                                 goto __CATCH;
5444                         }
5445                 } else if (*szSrc >= 0xE0) {
5446                         if (*(szSrc + 1) >= 0x80) {
5447                                 if (*(szSrc + 2) >= 0x80) {
5448                                         szSrc += 3;
5449                                         nChar -= 3;
5450                                 } else {
5451                                         MSG_DEBUG("MsgIsUTF8String: 2. NOT utf8 range!\n");
5452                                         goto __CATCH;
5453                                 }
5454                         } else {
5455                                 MSG_DEBUG("MsgIsUTF8String: 3. NOT utf8 range!\n");
5456                                 goto __CATCH;
5457                         }
5458                 } else {
5459                         MSG_DEBUG("MsgIsUTF8String: 4. NOT utf8 range!\n");
5460                         goto __CATCH;
5461                 }
5462         }
5463
5464         return true;
5465
5466 __CATCH:
5467         return false;
5468 }
5469
5470 bool MsgIsPercentSign(char *pSrc)
5471 {
5472         char *pCh = NULL;
5473         bool bRet = false;
5474
5475         pCh = strchr(pSrc , '%');
5476
5477         if (pCh != NULL) {
5478                 bRet = true;
5479         } else {
5480                 bRet = false;
5481         }
5482
5483         return bRet;
5484 }
5485
5486 MsgMultipart *MsgAllocMultipart(void)
5487 {
5488         MsgMultipart *pMultipart = NULL;
5489
5490         MSG_DEBUG("MsgAllocMultipart: --------- \n");
5491
5492         pMultipart = (MsgMultipart*)malloc(sizeof(MsgMultipart));
5493         if (pMultipart == NULL) {
5494                 MSG_DEBUG("MsgAllocMultipart: pMultipart malloc Fail \n");
5495                 goto __CATCH;
5496         }
5497
5498         pMultipart->pBody = (MsgBody*)malloc(sizeof(MsgBody));
5499         if (pMultipart->pBody == NULL) {
5500                 MSG_DEBUG("MsgAllocMultipart: pMultipart->pBody malloc Fail \n");
5501                 goto __CATCH;
5502         }
5503
5504         _MsgInitMsgType(&pMultipart->type);
5505         _MsgInitMsgBody(pMultipart->pBody);
5506
5507         pMultipart->pNext = NULL;
5508
5509         return pMultipart;
5510
5511 __CATCH:
5512
5513         if (pMultipart) {
5514                 if (pMultipart->pBody) {
5515                         free(pMultipart->pBody);
5516                         pMultipart->pBody = NULL;
5517                 }
5518
5519                 free(pMultipart);
5520                 pMultipart = NULL;
5521         }
5522
5523         return NULL;
5524 }
5525
5526 bool _MsgInitMsgType(MsgType *pMsgType)
5527 {
5528         pMsgType->offset = 0;
5529         pMsgType->size = 0;
5530         pMsgType->contentSize = 0;
5531         pMsgType->disposition = 0;
5532         pMsgType->encoding = 0;
5533         pMsgType->type = MIME_UNKNOWN;
5534 #ifdef FEATURE_JAVA_MMS_MIME
5535         pMsgType->szMimeString[0] ='\0';
5536 #endif
5537         pMsgType->section = 0;
5538
5539         pMsgType->szOrgFilePath[0] = '\0';
5540         pMsgType->szContentID[0] = '\0';
5541         pMsgType->szContentLocation[0] = '\0';
5542
5543         pMsgType->szContentRepPos[0] = '\0';
5544         pMsgType->szContentRepSize[0] = '\0';
5545         pMsgType->szContentRepIndex[0] = '\0';
5546
5547         __MsgInitMsgContentParam(&pMsgType->param);
5548 #ifdef __SUPPORT_DRM__
5549         __MsgInitMsgDRMInfo(&pMsgType->drmInfo);
5550 #endif
5551
5552         return true;
5553 }
5554
5555
5556 bool __MsgInitMsgContentParam(MsgContentParam *pMsgContentParam)
5557 {
5558         pMsgContentParam->charset = MSG_CHARSET_UNKNOWN;
5559         pMsgContentParam->type = MIME_UNKNOWN;
5560         pMsgContentParam->szBoundary[0] = '\0';
5561         pMsgContentParam->szFileName[0] = '\0';
5562         pMsgContentParam->szName[0] = '\0';
5563 #ifdef FEATURE_JAVA_MMS
5564         pMsgContentParam->szApplicationID = NULL;
5565         pMsgContentParam->szReplyToApplicationID = NULL;
5566 #endif
5567         pMsgContentParam->szStart[0] = '\0';
5568         pMsgContentParam->szStartInfo[0] = '\0';
5569         pMsgContentParam->pPresentation = NULL;
5570
5571         pMsgContentParam->reportType = MSG_PARAM_REPORT_TYPE_UNKNOWN; //  add only used as parameter of Content-Type: multipart/report; report-type
5572
5573         return true;
5574 }
5575
5576
5577 bool _MsgInitMsgBody(MsgBody *pMsgBody)
5578 {
5579         pMsgBody->offset = 0;
5580         pMsgBody->size = 0;
5581         pMsgBody->body.pText = NULL;
5582         pMsgBody->szOrgFilePath[0] = '\0';
5583
5584         _MsgInitMsgType(&pMsgBody->presentationType);
5585         pMsgBody->pPresentationBody = NULL;
5586
5587         memset(pMsgBody->szOrgFilePath, 0, MSG_FILEPATH_LEN_MAX);
5588
5589         return true;
5590 }
5591
5592
5593 MsgPresentationFactor MsgIsPresentationEx(MsgType *multipartType, char* szStart, MsgContentType typeParam)
5594 {
5595         char szTmpStart[MSG_MSG_ID_LEN + 3] = { 0, };
5596         char szTmpContentID[MSG_MSG_ID_LEN + 3] = { 0, };
5597         char szTmpContentLO[MSG_MSG_ID_LEN + 3] = { 0, };
5598         int strLen = 0;
5599
5600         // remove '<' and '>' in Start Param : contentID ex] <0_1.jpg> or <1233445>
5601         if (szStart && szStart[0]) {
5602                 int startLen = 0;
5603                 startLen = strlen(szStart);
5604                 if (szStart[0] == '<' && szStart[startLen - 1] == '>') {
5605                         strncpy(szTmpStart, &szStart[1], startLen - 2);
5606                 } else {
5607                         strncpy(szTmpStart, szStart, startLen);
5608                 }
5609         }
5610
5611         // remove '<' and '>' in ContentID : contentID ex] <0_1.jpg> or <1233445>
5612         if (multipartType->szContentID[0])      {
5613                 strLen = strlen(multipartType->szContentID);
5614                 if (multipartType->szContentID[0] == '<' && multipartType->szContentID[strLen - 1] == '>') {
5615                         strncpy(szTmpContentID, &(multipartType->szContentID[1]), strLen - 2);
5616                 } else {
5617                         strncpy(szTmpContentID, multipartType->szContentID, strLen);
5618                 }
5619         }
5620
5621         // remove '<' and '>' in ContentLocation : contentID ex] <0_1.jpg> or <1233445>
5622         if (multipartType->szContentLocation[0]) {
5623                 strLen = strlen(multipartType->szContentLocation);
5624                 if (multipartType->szContentLocation[0] == '<' && multipartType->szContentLocation[strLen - 1] == '>') {
5625                         strncpy(szTmpContentLO, &multipartType->szContentLocation[1], strLen - 2);
5626                 } else {
5627                         strncpy(szTmpContentLO, multipartType->szContentLocation, strLen);
5628                 }
5629         }
5630
5631         if ((szTmpContentID[0] == '\0') && (szTmpContentLO[0] == '\0') && (multipartType->type == MIME_UNKNOWN))
5632                 return  MSG_PRESENTATION_NONE;
5633
5634         // exception handling
5635         if (szTmpStart[0] != '\0') {
5636                 // presentation part : 1.compare with contentID 2.compare with content Location 3. compare with type
5637                 if (strcmp(szTmpStart, szTmpContentID) == 0) {
5638                         return MSG_PRESENTATION_ID;
5639                 } else if (strcmp(szTmpStart, szTmpContentLO) == 0) {
5640                         return   MSG_PRESENTATION_LOCATION;
5641                 } else if (multipartType->type == typeParam) {
5642                         return   MSG_PRESENTATION_TYPE_BASE;
5643                 } else {
5644                         return   MSG_PRESENTATION_NONE;
5645                 }
5646         } else {
5647                 if (multipartType->type == typeParam && typeParam != MIME_UNKNOWN) {
5648                         return   MSG_PRESENTATION_TYPE_BASE;
5649                 } else {
5650                         return   MSG_PRESENTATION_NONE;
5651                 }
5652         }
5653 }
5654
5655 void MsgConfirmPresentationPart(MsgType *pMsgType, MsgBody *pMsgBody, MsgPresentaionInfo *pPresentationInfo)
5656 {
5657         MSG_BEGIN();
5658         MsgMultipart *pNextPart = NULL;
5659         MsgMultipart *pRemovePart = NULL;
5660
5661         if (MsgIsMultipartRelated(pMsgType->type)) {
5662                 // assign the multipart to presentation part
5663                 // remove the multipart(pCurPresentation) which is presentation part from the linked list.
5664                 // if there is no presentation part -> assign first multipart to presentation part by force.
5665                 if (pPresentationInfo->pCurPresentation == NULL) {
5666                         pPresentationInfo->pCurPresentation     = pMsgBody->body.pMultipart;
5667                         pPresentationInfo->pPrevPart            = NULL;
5668                         pPresentationInfo->factor                       = MSG_PRESENTATION_NONE;
5669                 }
5670
5671                 if (pPresentationInfo->pCurPresentation != NULL && MsgIsPresentablePart(pPresentationInfo->pCurPresentation->type.type)) {
5672                         /* Presentable Part is some MARK-UP page, such as SMIL, HTML, WML, XHTML.
5673                          * In this case, COPY the Presentation part and leave other multiparts.
5674                          */
5675                         memcpy(&pMsgBody->presentationType, &pPresentationInfo->pCurPresentation->type, sizeof(MsgType));
5676                         pMsgBody->pPresentationBody = pPresentationInfo->pCurPresentation->pBody;
5677
5678                         // remove pCurPresentation from multipart linked list
5679                         if ((pPresentationInfo->factor == MSG_PRESENTATION_NONE)||(pPresentationInfo->pPrevPart == NULL)) {
5680                                 // first part
5681                                 pMsgBody->body.pMultipart = pPresentationInfo->pCurPresentation->pNext;
5682                                 pMsgType->contentSize -= pPresentationInfo->pCurPresentation->pBody->size;
5683                                 pMsgBody->size -= pPresentationInfo->pCurPresentation->pBody->size;
5684                                 if (pPresentationInfo->pCurPresentation) {
5685 #ifdef __SUPPORT_DRM__
5686                                         _MsgFreeDRMInfo(&pPresentationInfo->pCurPresentation->type.drmInfo);
5687 #endif
5688                                         free(pPresentationInfo->pCurPresentation);
5689                                         pPresentationInfo->pCurPresentation = NULL;
5690                                 }
5691                         } else {
5692                                 // not a first part
5693                                 pPresentationInfo->pPrevPart->pNext = pPresentationInfo->pCurPresentation->pNext;
5694                                 pMsgType->contentSize -= pPresentationInfo->pCurPresentation->pBody->size;
5695                                 pMsgBody->size -= pPresentationInfo->pCurPresentation->pBody->size;
5696                                 if (pPresentationInfo->pCurPresentation) {
5697                                         free(pPresentationInfo->pCurPresentation);
5698                                         pPresentationInfo->pCurPresentation = NULL;
5699                                 }
5700                         }
5701                 } else if (pPresentationInfo->pCurPresentation != NULL && MsgIsText(pPresentationInfo->pCurPresentation->type.type)) {
5702                         /* NON-Presentable Part is some PLAIN part such as, text/plain, multipart/alternative.
5703                          * In this case, leave the Presentation part as a multipart and remove other multiparts.
5704                          */
5705
5706                         // Backup the multipart link information
5707                         pNextPart = pMsgBody->body.pMultipart;
5708
5709                         // Copy presentation part as a main part
5710                         memcpy(pMsgType, &pPresentationInfo->pCurPresentation->type, sizeof(MsgType));
5711                         memcpy(pMsgBody, pPresentationInfo->pCurPresentation->pBody, sizeof(MsgBody));
5712
5713                         // Remove multipart linked list
5714                         while (pNextPart) {
5715                                 pRemovePart = pNextPart;
5716                                 pNextPart = pNextPart->pNext;
5717
5718                                 if (pRemovePart->pBody) {
5719                                         _MsgFreeBody(pRemovePart->pBody, pRemovePart->type.type);
5720                                         free(pRemovePart->pBody);
5721                                         pRemovePart->pBody = NULL;
5722                                 }
5723
5724                                 free(pRemovePart);
5725                                 pRemovePart = NULL;
5726                         }
5727                 } else {
5728 #ifdef __SUPPORT_DRM__
5729                         _MsgFreeDRMInfo(&pMsgBody->presentationType.drmInfo);
5730 #endif
5731                         _MsgInitMsgType(&pMsgBody->presentationType);
5732                         pMsgBody->pPresentationBody = NULL;
5733                 }
5734         }
5735         MSG_END();
5736 }
5737
5738 bool MsgIsMultipartRelated(int type)
5739 {
5740         if (type == MIME_MULTIPART_RELATED || type == MIME_APPLICATION_VND_WAP_MULTIPART_RELATED) {
5741                 return true;
5742         } else {
5743                 return false;
5744         }
5745 }
5746
5747 bool MsgIsPresentablePart(int type)
5748 {
5749         if (type == MIME_TEXT_HTML || type == MIME_TEXT_VND_WAP_WML || type == MIME_APPLICATION_SMIL) {
5750                 return true;
5751         } else {
5752                 return false;
5753         }
5754 }
5755
5756 #ifdef __SUPPORT_DRM__
5757 void _MsgFreeDRMInfo(MsgDRMInfo *pDrmInfo)
5758 {
5759         MSG_DEBUG("_MsgFreeDRMInfo: S T A R T  !!! \n");
5760
5761         if (pDrmInfo == NULL) {
5762                 MSG_DEBUG("pDrmInfo is NULL");
5763                 return;
5764         }
5765
5766         if (pDrmInfo->szContentDescription) {
5767                 free(pDrmInfo->szContentDescription);
5768                 pDrmInfo->szContentDescription = NULL;
5769         }
5770
5771         if (pDrmInfo->szContentVendor) {
5772                 free(pDrmInfo->szContentVendor);
5773                 pDrmInfo->szContentVendor = NULL;
5774         }
5775
5776         if (pDrmInfo->szContentName) {
5777                 free(pDrmInfo->szContentName);
5778                 pDrmInfo->szContentName = NULL;
5779         }
5780
5781         if (pDrmInfo->szContentURI) {
5782                 free(pDrmInfo->szContentURI);
5783                 pDrmInfo->szContentURI = NULL;
5784         }
5785
5786         if (pDrmInfo->szRightIssuer) {
5787                 free(pDrmInfo->szRightIssuer);
5788                 pDrmInfo->szRightIssuer = NULL;
5789         }
5790
5791         if (pDrmInfo->szDrm2FullPath) {
5792                 free(pDrmInfo->szDrm2FullPath);
5793                 pDrmInfo->szDrm2FullPath = NULL;
5794         }
5795
5796 #if MMS_ENABLE_EXTEND_CFM
5797         // DRM (Extended)CFM
5798         if (pDrmInfo->pszContentType) {
5799                 free(pDrmInfo->pszContentType);
5800                 pDrmInfo->pszContentType = NULL;
5801         }
5802 #endif
5803
5804         pDrmInfo->contentType = MIME_UNKNOWN;
5805         pDrmInfo->drmType = MSG_DRM_TYPE_NONE;
5806 }
5807
5808 bool MsgCopyDrmInfo(MsgType *pPartType)
5809 {
5810         char *pExt = NULL;
5811         char *pTmpBuf = NULL;
5812
5813         //convert application/vnd.oma.drm.content to media type
5814         pPartType->type = pPartType->drmInfo.contentType;
5815
5816         // fix wrong file name presentation on save media screen.
5817         if (pPartType->szContentID[0] == '\0' && pPartType->drmInfo.szContentURI)
5818                 strncpy(pPartType->szContentID, pPartType->drmInfo.szContentURI, MSG_MSG_ID_LEN);
5819
5820         /* set title name (content name) */
5821         if (pPartType->param.szName[0] == '\0') {
5822                 /*      szName is vitual name, real filename is *.dcf or *.dm   */
5823                 if (pPartType->drmInfo.szContentName && pPartType->drmInfo.szContentName[0] != '\0') {
5824                         /* In case of szContentName retrieved from DRM agent is exist. */
5825                         pTmpBuf = pPartType->drmInfo.szContentName;
5826                 } else if (pPartType->szContentLocation[0] != '\0')     {
5827                         /* In case of szContentLocation parsed from MMS header */
5828                         pTmpBuf = strrchr(pPartType->szContentLocation, '/');
5829                         if (pTmpBuf == NULL)
5830                                 pTmpBuf = pPartType->szContentLocation;
5831                 } else {
5832                         /* use another name */
5833                         /* possible NULL pointer assignment*/
5834                         pTmpBuf = strdup("untitled");
5835                 }
5836
5837                 if ((pExt = strrchr(pTmpBuf, '.')) != NULL) {
5838                         int extLen = 0;
5839                         int fileNameLen = 0;
5840                         int tmpLen = 0;
5841
5842                         extLen = strlen(pExt);
5843                         tmpLen = strlen(pTmpBuf);
5844                         fileNameLen = (tmpLen - extLen < MSG_LOCALE_FILENAME_LEN_MAX - extLen)?(tmpLen - extLen):(MSG_LOCALE_FILENAME_LEN_MAX - extLen);
5845                         strncpy(pPartType->param.szName, pTmpBuf, fileNameLen);
5846                         strcpy (pPartType->param.szName + fileNameLen, pExt);
5847                 } else {
5848                         strncpy(pPartType->param.szName, pTmpBuf, MSG_LOCALE_FILENAME_LEN_MAX);
5849                         MsgMakeFileName(pPartType->type, pPartType->param.szName, MSG_DRM_TYPE_NONE, 0);
5850                 }
5851         }
5852
5853         return true;
5854 }
5855
5856 #endif
5857
5858 bool MsgIsText(int type)
5859 {
5860         if (type == MIME_TEXT_PLAIN || type == MIME_TEXT_HTML || type == MIME_TEXT_VND_WAP_WML ||
5861                 type == MIME_TEXT_X_VNOTE || type == MIME_APPLICATION_SMIL || type == MIME_TEXT_X_IMELODY) {
5862                 return true;
5863         } else {
5864                 return false;
5865         }
5866 }
5867
5868 bool MsgFreeAttrib(MmsAttrib *pAttrib)
5869 {
5870         MSG_BEGIN();
5871
5872         if (pAttrib == NULL) {
5873                 MSG_DEBUG("pAttrib is NULL");
5874                 return false;
5875         }
5876
5877         if (pAttrib->szTo) {
5878                 free(pAttrib->szTo);
5879                 pAttrib->szTo = NULL;
5880         }
5881
5882         if (pAttrib->szCc) {
5883                 free(pAttrib->szCc);
5884                 pAttrib->szCc = NULL;
5885         }
5886
5887         if (pAttrib->szBcc) {
5888                 free(pAttrib->szBcc);
5889                 pAttrib->szBcc = NULL;
5890         }
5891
5892         //check if pMultiStatus should be freed or not, because pMultiStatus is not allocated
5893         if (pAttrib->pMultiStatus) {
5894                 MmsMsgMultiStatus *pMultiStatus = pAttrib->pMultiStatus;
5895                 MmsMsgMultiStatus *pCurStatus = NULL;
5896
5897                 while (pMultiStatus != NULL ) {
5898                         pCurStatus = pMultiStatus;
5899                         pMultiStatus = pMultiStatus->pNext;
5900
5901                         if (pCurStatus) {
5902                                 free(pCurStatus);
5903                                 pCurStatus = NULL;
5904                         }
5905                 }
5906
5907                 pAttrib->pMultiStatus = NULL;
5908         }
5909
5910
5911         MSG_END();
5912
5913         return true;
5914 }
5915
5916 bool _MsgFreeBody(MsgBody *pBody, int type)
5917 {
5918         MSG_DEBUG("_MsgFreeBody: S T A R T  !!! \n") ;
5919
5920         if (pBody == NULL) {
5921                 MSG_DEBUG("_MsgFreeBody: pBody == NULL \n" );
5922                 MSG_DEBUG("_MsgFreeBody: E  N  D   (End)!!! \n") ;
5923
5924                 return false;
5925         }
5926
5927         switch (type) {
5928         case MIME_MULTIPART_REPORT:
5929         case MIME_APPLICATION_VND_OMA_DRM_MESSAGE:
5930         case MIME_APPLICATION_VND_WAP_MULTIPART_MIXED:
5931         case MIME_APPLICATION_VND_WAP_MULTIPART_RELATED:
5932         case MIME_APPLICATION_VND_WAP_MULTIPART_ASTERIC:
5933         case MIME_MULTIPART_MIXED:
5934         case MIME_MULTIPART_RELATED:
5935         case MIME_MULTIPART_ALTERNATIVE:
5936         case MIME_APPLICATION_VND_WAP_MULTIPART_ALTERNATIVE:
5937                 {
5938                         MsgMultipart *pMulti = pBody->body.pMultipart;
5939                         MsgMultipart *pCurrPart = NULL;
5940                         MsgBody *pPresentation = pBody->pPresentationBody;
5941                         while (pMulti != NULL) {
5942                                 pCurrPart = pMulti;
5943
5944                                 pMulti = pMulti->pNext;
5945
5946                                 if (pCurrPart) {
5947 #ifdef __SUPPORT_DRM__
5948                                         _MsgFreeDRMInfo(&pCurrPart->type.drmInfo);
5949 #endif
5950
5951                                         if (pCurrPart->pBody) {
5952                                                 if (pCurrPart->pBody->body.pBinary) {
5953                                                         free(pCurrPart->pBody->body.pBinary);
5954                                                         pCurrPart->pBody->body.pBinary = NULL;
5955                                                 }
5956                                                 free(pCurrPart->pBody);
5957                                                 pCurrPart->pBody = NULL;
5958                                         }
5959                                         free(pCurrPart);
5960                                         pCurrPart = NULL;
5961                                 }
5962                         }
5963
5964                         pBody->body.pMultipart = NULL;
5965
5966                         if (pPresentation) {
5967                                 if (pPresentation->body.pText) {
5968                                         free(pPresentation->body.pText);
5969                                         pPresentation->body.pText = NULL;
5970                                 }
5971                                 free(pPresentation);
5972                                 pBody->pPresentationBody = NULL;
5973                         }
5974
5975                         _MsgInitMsgType(&pBody->presentationType);
5976
5977                         break;
5978                 }
5979
5980         default:
5981                 /* Any single part */
5982
5983                 if (pBody->body.pBinary) {
5984                         free(pBody->body.pBinary);
5985                         pBody->body.pBinary = NULL;
5986                 }
5987
5988                 break;
5989         }
5990
5991         MSG_DEBUG("_MsgFreeBody: E  N  D  (Successfully) !!! \n") ;
5992
5993         return true;
5994
5995 }
5996
5997 bool MsgResolveNestedMultipart(MsgType *pPartType, MsgBody *pPartBody)
5998 {
5999         MSG_BEGIN();
6000         MsgMultipart *pTmpMultipart = NULL;
6001         MsgMultipart *pSelectedPart = NULL;
6002         MsgMultipart *pPrevPart = NULL;
6003         MsgMultipart *pFirstPart = NULL;
6004         MsgMultipart *pLastPart = NULL;
6005         MsgMultipart *pRemoveList = NULL;
6006         MsgMultipart *pNextRemovePart = NULL;
6007
6008         switch (pPartType->type) {
6009         case MIME_APPLICATION_VND_WAP_MULTIPART_ALTERNATIVE:
6010         case MIME_MULTIPART_ALTERNATIVE:
6011
6012                 /* fixme:
6013                  * Policy: multipart/alternative
6014                  * multipart/alternative message has only several parts of media.
6015                  * You should choose one of them and make the alternative part
6016                  * to the selected media part.
6017                  */
6018
6019                 MSG_DEBUG("MsgResolveNestedMultipart : MIME_APPLICATION_VND_WAP_MULTIPART_ALTERNATIVE\n");
6020
6021                 pSelectedPart = pPartBody->body.pMultipart;
6022
6023                 // NULL Pointer check!!
6024                 if (pSelectedPart == NULL) {
6025                         MSG_DEBUG("MsgResolveNestedMultipart : multipart(ALTERNATIVE) does not exist\n");
6026                         break;
6027                 }
6028
6029                 pTmpMultipart = pPartBody->body.pMultipart->pNext;
6030
6031                 while (pTmpMultipart) {
6032                         if (pSelectedPart->type.type <= pTmpMultipart->type.type)
6033                                 pSelectedPart = pTmpMultipart;
6034
6035                         pTmpMultipart = pTmpMultipart->pNext;
6036                 }
6037
6038                 pTmpMultipart = pPartBody->body.pMultipart;
6039                 pPrevPart = NULL;
6040
6041                 while (pTmpMultipart) {
6042                         if (pSelectedPart == pTmpMultipart)
6043                                 break;
6044
6045                         pPrevPart = pTmpMultipart;
6046                         pTmpMultipart = pTmpMultipart->pNext;
6047                 }
6048
6049                 if (pPrevPart == NULL) {
6050                         /* selected part is the first part */
6051                         pRemoveList = pSelectedPart->pNext;
6052                 } else {
6053                         pPrevPart->pNext = pSelectedPart->pNext;
6054                         pRemoveList = pPartBody->body.pMultipart;
6055                         pPartBody->body.pMultipart = pSelectedPart;
6056                 }
6057
6058                 pSelectedPart->pNext = NULL;
6059
6060                 if (pRemoveList) {
6061 #ifdef __SUPPORT_DRM__
6062                         _MsgFreeDRMInfo(&pRemoveList->type.drmInfo);
6063 #endif
6064                         _MsgFreeBody(pRemoveList->pBody, pRemoveList->type.type);
6065
6066                         free(pRemoveList->pBody);
6067                         free(pRemoveList);
6068                 }
6069
6070                 if (MsgCopyNestedMsgType(pPartType, &(pSelectedPart->type)) == false) {
6071                         MSG_DEBUG("MsgResolveNestedMultipart : MsgPriorityCopyMsgType failed \n");
6072                         goto __CATCH;
6073                 }
6074
6075                 if (pSelectedPart->pBody != NULL)
6076                         memcpy(pPartBody, pSelectedPart->pBody, sizeof(MsgBody));
6077
6078                 if (pSelectedPart != NULL) {
6079 #ifdef __SUPPORT_DRM__
6080                         _MsgFreeDRMInfo(&pSelectedPart->type.drmInfo);
6081 #endif
6082
6083                         if (pSelectedPart->pBody != NULL) {
6084                                 free(pSelectedPart->pBody);
6085                                 pSelectedPart->pBody = NULL;
6086                         }
6087                         free(pSelectedPart);
6088                         pSelectedPart = NULL;
6089                 }
6090
6091                 break;
6092
6093         case MIME_APPLICATION_VND_WAP_MULTIPART_RELATED:
6094         case MIME_MULTIPART_RELATED:
6095
6096                 MSG_DEBUG("MsgResolveNestedMultipart : MIME_APPLICATION_VND_WAP_MULTIPART_RELATED\n");
6097
6098                 pSelectedPart = pPartBody->body.pMultipart;
6099
6100                 while (pSelectedPart) {
6101                         if (MsgIsMultipartMixed(pSelectedPart->type.type)) {
6102
6103                                 if (pSelectedPart->pBody == NULL) {
6104                                         MSG_DEBUG("MsgResolveNestedMultipart :pSelectedPart->pBody(1) is NULL\n");
6105                                         break;
6106                                 }
6107
6108                                 pFirstPart = pSelectedPart->pBody->body.pMultipart;
6109
6110                                 if (pFirstPart == NULL) {
6111                                         MSG_DEBUG("MsgResolveNestedMultipart : multipart(RELATED) does not exist\n");
6112                                         break;
6113                                 }
6114
6115                                 if (pFirstPart->pNext) {
6116                                         pLastPart = pFirstPart->pNext;
6117                                         while (pLastPart->pNext)
6118                                                 pLastPart = pLastPart->pNext;
6119                                 } else {
6120                                         pLastPart = pFirstPart;
6121                                 }
6122
6123                                 if (pPrevPart == NULL) {
6124                                         /* the first part */
6125                                         pTmpMultipart = pPartBody->body.pMultipart->pNext;
6126                                         pPartBody->body.pMultipart = pFirstPart;
6127                                         pLastPart->pNext = pTmpMultipart;
6128                                 } else {
6129                                         pTmpMultipart = pSelectedPart->pNext;
6130                                         pPrevPart->pNext = pFirstPart;
6131                                         pLastPart->pNext = pTmpMultipart;
6132                                 }
6133
6134                                 if (pSelectedPart) {
6135 #ifdef __SUPPORT_DRM__
6136                                         _MsgFreeDRMInfo(&pSelectedPart->type.drmInfo);
6137 #endif
6138                                         free(pSelectedPart->pBody);
6139                                         free(pSelectedPart);
6140                                 }
6141                                 pSelectedPart = pTmpMultipart;
6142                         } else if (MsgIsMultipartRelated(pSelectedPart->type.type) && pPrevPart != NULL) {
6143                                 pPrevPart->pNext = pTmpMultipart = pSelectedPart->pNext;
6144                                 _MsgFreeBody(pSelectedPart->pBody, pSelectedPart->type.type);
6145
6146                                 free(pSelectedPart->pBody);
6147                                 free(pSelectedPart);
6148                                 pSelectedPart = pTmpMultipart;
6149                         } else {
6150                                 pPrevPart = pSelectedPart;
6151                                 pSelectedPart = pSelectedPart->pNext;
6152                         }
6153                 }
6154
6155                 break;
6156
6157
6158         case MIME_APPLICATION_VND_WAP_MULTIPART_MIXED:
6159         case MIME_MULTIPART_MIXED:
6160
6161                 MSG_DEBUG("MsgResolveNestedMultipart : MIME_APPLICATION_VND_WAP_MULTIPART_MIXED\n");
6162
6163                 pPrevPart = NULL;
6164                 pSelectedPart = pPartBody->body.pMultipart;
6165
6166                 while (pSelectedPart) {
6167                         if (MsgIsMultipart(pSelectedPart->type.type)) {
6168                                 if (pSelectedPart->pBody == NULL) {
6169                                         MSG_DEBUG("MsgResolveNestedMultipart :pSelectedPart->pBody(2) is NULL\n");
6170                                         break;
6171                                 }
6172
6173                                 pFirstPart = pSelectedPart->pBody->body.pMultipart;
6174
6175                                 // NULL Pointer check!!
6176                                 if (pFirstPart == NULL) {
6177                                         MSG_DEBUG("MsgResolveNestedMultipart : multipart does not exist\n");
6178                                         break;
6179                                 }
6180
6181                                 if (pFirstPart->pNext) {
6182                                         pLastPart = pFirstPart->pNext;
6183                                         while (pLastPart->pNext)
6184                                                 pLastPart = pLastPart->pNext;
6185                                 } else {
6186                                         pLastPart = pFirstPart;
6187                                 }
6188
6189                                 if (pPrevPart == NULL) {
6190                                         /* the first part */
6191                                         pTmpMultipart = pPartBody->body.pMultipart->pNext;
6192                                         pPartBody->body.pMultipart = pFirstPart;
6193                                         pLastPart->pNext = pTmpMultipart;
6194                                 } else {
6195                                         pTmpMultipart = pSelectedPart->pNext;
6196                                         pPrevPart->pNext = pFirstPart;
6197                                         pLastPart->pNext = pTmpMultipart;
6198                                 }
6199
6200                                 if (pSelectedPart->pBody->pPresentationBody)
6201                                         pPartBody->pPresentationBody = pSelectedPart->pBody->pPresentationBody;
6202
6203                                 memcpy(&pPartBody->presentationType,
6204                                                   &pSelectedPart->pBody->presentationType, sizeof(MsgType));
6205
6206                                 pPartType->type = pSelectedPart->type.type;
6207
6208 #ifdef __SUPPORT_DRM__
6209                                 _MsgFreeDRMInfo(&pSelectedPart->type.drmInfo);
6210 #endif
6211                                 free(pSelectedPart->pBody);
6212                                 free(pSelectedPart);
6213
6214                                 pSelectedPart = pTmpMultipart;
6215                         } else {
6216                                 pPrevPart = pSelectedPart;
6217                                 pSelectedPart = pSelectedPart->pNext;
6218                         }
6219                 }
6220
6221                 break;
6222
6223         case MIME_MULTIPART_REPORT:
6224
6225                 MSG_DEBUG("MsgResolveNestedMultipart : MIME_MULTIPART_REPORT \n");
6226
6227                 pTmpMultipart = pPartBody->body.pMultipart;
6228                 pPrevPart = NULL;
6229
6230                 if (pTmpMultipart == NULL) {
6231                         MSG_DEBUG("MsgResolveNestedMultipart : pTmpMultipart == NULL \n");
6232                         return false;
6233                 }
6234
6235                 while (pTmpMultipart) {
6236                         if (pTmpMultipart->type.type == MIME_TEXT_PLAIN) {
6237                                 pSelectedPart = pTmpMultipart;
6238                                 break;
6239                         }
6240
6241                         pPrevPart = pTmpMultipart;
6242                         pTmpMultipart = pTmpMultipart->pNext;
6243                 }
6244
6245                 if (pSelectedPart == NULL) {
6246                         MSG_DEBUG("MsgResolveNestedMultipart : MIME_MULTIPART_REPORT [no selected part]\n");
6247
6248                         pRemoveList = pPartBody->body.pMultipart->pNext;
6249                         if (pPartBody->body.pMultipart != NULL) {
6250                                 pSelectedPart = pPartBody->body.pMultipart;
6251                                 pSelectedPart->pNext = NULL;
6252                         }
6253                 } else {
6254                         if (pPrevPart == NULL) {
6255                                 // first part is selected
6256                                 pRemoveList = pPartBody->body.pMultipart->pNext;
6257                         } else {
6258                                 pRemoveList = pPartBody->body.pMultipart->pNext;
6259                                 pPrevPart->pNext = pSelectedPart->pNext;
6260                         }
6261
6262                         pSelectedPart->pNext = NULL;
6263                         pPartBody->body.pMultipart = pSelectedPart;
6264                 }
6265
6266                 pTmpMultipart = pRemoveList;
6267
6268                 while (pTmpMultipart) {
6269 #ifdef __SUPPORT_DRM__
6270                         _MsgFreeDRMInfo(&pTmpMultipart->type.drmInfo);
6271 #endif
6272                         _MsgFreeBody(pTmpMultipart->pBody, pTmpMultipart->type.type);
6273                         pNextRemovePart = pTmpMultipart->pNext;
6274
6275                         free(pTmpMultipart->pBody);
6276                         free(pTmpMultipart);
6277                         pTmpMultipart = pNextRemovePart;
6278                 }
6279
6280                 if (MsgCopyNestedMsgType(pPartType, &(pSelectedPart->type)) == false) {
6281                         MSG_DEBUG("MsgResolveNestedMultipart : MsgPriorityCopyMsgType failed \n");
6282                         goto __CATCH;
6283                 }
6284
6285                 if (pSelectedPart->pBody != NULL)
6286                         memcpy(pPartBody, pSelectedPart->pBody, sizeof(MsgBody));
6287
6288                 if (pSelectedPart != NULL) {
6289 #ifdef __SUPPORT_DRM__
6290                         _MsgFreeDRMInfo(&pSelectedPart->type.drmInfo);
6291 #endif
6292                         if (pSelectedPart->pBody != NULL) {
6293                                 free(pSelectedPart->pBody);
6294                                 pSelectedPart->pBody = NULL;
6295                         }
6296                         free(pSelectedPart);
6297                         pSelectedPart = NULL;
6298                 }
6299
6300                 break;
6301
6302         default:
6303                 break;
6304         }
6305         MSG_END();
6306
6307         return true;
6308
6309 __CATCH:
6310         return false;
6311
6312 }
6313
6314 char *MsgResolveContentURI(char *szSrc)
6315 {
6316         char *szTemp = NULL;
6317         char *szReturn = NULL;
6318         int length = 0;
6319
6320         if (szSrc == NULL) {
6321                 goto __CATCH;
6322         }
6323
6324         if (szSrc[0] == '\0')
6325                 goto __CATCH;
6326
6327
6328         if (!strncasecmp(szSrc, "cid:", 4)) {
6329                 length = strlen(szSrc) - 3;
6330                 szSrc += 4;
6331         } else {
6332                 length = strlen(szSrc) + 1;
6333         }
6334
6335         szTemp = (char *)malloc(length);
6336         if (szTemp == NULL) {
6337                 MSG_DEBUG("MsgResolveContentURI: memory full\n");
6338                 goto __CATCH;
6339         }
6340
6341         memset(szTemp, 0, length);
6342         strcpy(szTemp, szSrc);
6343
6344         szReturn = MsgChangeHexString(szTemp);
6345
6346         if (szTemp) {
6347                 free(szTemp);
6348                 szTemp = NULL;
6349         }
6350
6351         return szReturn;
6352
6353 __CATCH:
6354
6355         return NULL;
6356 }
6357
6358 bool _MsgParsePartHeader(MsgType *pType, const char *pRawData, int nRawData)
6359 {
6360         char ch = '\0';
6361         int cRaw = nRawData;
6362         char *pFieldLine = NULL;
6363         char *pFieldValue = NULL;
6364         char *szFieldValue = NULL;
6365         char *szSrc = NULL;
6366         int nLen = 0;
6367         char fieldLine[MSG_LINE_LEN] = {0, }; //temporary line buffer
6368         int length = 0;
6369
6370         char szTempBuf[MSG_LOCAL_TEMP_BUF_SIZE];
6371
6372         while ((cRaw > 0) && *pRawData != MSG_CH_LF) {
6373                 memset(fieldLine, 0, MSG_LINE_LEN);
6374                 pFieldLine = fieldLine;
6375
6376                 ch = MSG_CH_SP; //remember previous character.
6377
6378                 while (ch) {
6379                         switch (ch = *pRawData++) {
6380                         case MSG_CH_CR: //'\r'
6381                                 if (*pRawData == MSG_CH_LF)
6382                                         break;
6383
6384                         case MSG_CH_LF: //'\n'
6385                                 if (*pRawData != MSG_CH_SP && *pRawData != MSG_CH_TAB) {
6386                                         ch = MSG_CH_NULL;
6387                                         *pFieldLine = ch;
6388                                         pFieldLine++;
6389                                 }
6390                                 break;
6391
6392                         case MSG_CH_TAB: //'\t'
6393                                 *pFieldLine = MSG_CH_SP;
6394                                 pFieldLine++;
6395                                 break;
6396
6397                         default:
6398                                 *pFieldLine = ch;
6399                                 pFieldLine++;
6400                                 break;
6401                         }
6402
6403                         if (--cRaw <= 0) {  //If is the last character of header
6404                                 *pFieldLine = MSG_CH_NULL;
6405                                 pFieldLine++;
6406                         }
6407                 }
6408
6409
6410                 if ((pFieldValue =  strchr(fieldLine, MSG_CH_COLON)) != NULL) {
6411                         char*   pTemp = pFieldValue;
6412                         char*   pValue = NULL;
6413
6414                         *pFieldValue++ = MSG_CH_NULL; //remove ':'
6415
6416                         while (*pFieldValue == MSG_CH_SP) //remove space ' '
6417                                 pFieldValue++;
6418
6419                         while ((fieldLine < pTemp--) && (*pTemp == MSG_CH_SP))
6420                                 *pTemp = MSG_CH_NULL;
6421
6422                         nLen = strlen(pFieldValue);
6423
6424                         if (nLen >= MSG_LOCAL_TEMP_BUF_SIZE) {
6425                                 szFieldValue = (char *)malloc(nLen + 1);
6426                                 memset(szFieldValue, 0 , nLen + 1);
6427                                 memcpy(szFieldValue, pFieldValue, nLen + 1);
6428                         } else {
6429                                 memset(szTempBuf, 0, MSG_LOCAL_TEMP_BUF_SIZE);
6430                                 strcpy(szTempBuf, pFieldValue);
6431                                 szFieldValue = szTempBuf;
6432                         }
6433
6434                         if (MsgStrcpyWithoutCRLF(szFieldValue, &szSrc) == true) {
6435                                 if (szSrc != NULL)
6436                                         strncpy(szFieldValue, szSrc, strlen(szSrc));
6437                         }
6438                         if (szSrc) {
6439                                 free(szSrc);
6440                                 szSrc = NULL;
6441                         }
6442
6443                         switch (_MsgGetCode(MSG_FIELD, fieldLine)) {
6444                         case MSG_FIELD_CONTENT_ID:
6445                                 if ((pTemp = strchr(szFieldValue, MSG_CH_SEMICOLON)) != NULL)
6446                                         *pTemp++ = MSG_CH_NULL;
6447
6448                                 pValue = _MsgDecodeText(szFieldValue);
6449                                 memset (pType->szContentID, 0, MSG_MSG_ID_LEN + 1);
6450
6451                                 if (pValue) {
6452                                         // support CD & SD
6453                                         length = MsgStrlen(pValue);
6454                                         if (pValue[0] == '<' && pValue[length-1] == '>') {
6455                                                 strncpy(pType->szContentID, pValue + 1, length - 2);
6456                                         } else {
6457                                                 strncpy(pType->szContentID, pValue, MSG_MSG_ID_LEN);
6458                                         }
6459
6460                                         MSG_DEBUG("_MsgParsePartHeader: Content-ID = %s     %s\n", pFieldValue, pType->szContentID);
6461
6462                                         if (pTemp != NULL)
6463                                                 _MsgParseParameter(pType, pTemp);
6464
6465                                         free(pValue);
6466                                         pValue = NULL;
6467                                 }
6468
6469                                 break;
6470
6471                         case MSG_FIELD_CONTENT_LOCATION:
6472                                 if ((pTemp = strchr(szFieldValue, MSG_CH_SEMICOLON)) != NULL)
6473                                         *pTemp++ = MSG_CH_NULL;
6474
6475                                 pValue = _MsgDecodeText(szFieldValue);
6476                                 memset (pType->szContentLocation, 0, MSG_MSG_ID_LEN + 1);
6477
6478                                 if (pValue) {
6479                                         strncpy(pType->szContentLocation, pValue, MSG_MSG_ID_LEN);
6480
6481                                         MSG_DEBUG("_MsgParsePartHeader: Content-Location = %s     %s\n", pFieldValue, pType->szContentLocation);
6482
6483                                         if (pTemp != NULL)
6484                                                 _MsgParseParameter(pType, pTemp);
6485
6486                                         free(pValue);
6487                                         pValue = NULL;
6488                                 }
6489
6490                                 break;
6491
6492                         case MSG_FIELD_CONTENT_TYPE:
6493                                 if ((pTemp = strchr(szFieldValue, MSG_CH_SEMICOLON)) == NULL) {
6494                                         if ((pTemp = strchr(szFieldValue, MSG_CH_SP)) != NULL)
6495                                                 *pTemp++ = MSG_CH_NULL;
6496                                 } else {
6497                                         *pTemp++ = MSG_CH_NULL;
6498                                 }
6499
6500                                 pType->type = _MsgGetCode(MSG_TYPE, szFieldValue);
6501
6502                                 MSG_DEBUG("_MsgParsePartHeader: Content-Type = %s     %d\n", pFieldValue, pType->type);
6503
6504                                 if (pType->type == INVALID_HOBJ)
6505                                         pType->type = MIME_UNKNOWN;
6506
6507                                 if (pTemp != NULL)
6508                                         _MsgParseParameter(pType, pTemp);
6509
6510                                 break;
6511
6512                         case MSG_FIELD_CONTENT_TRANSFER_ENCODING:
6513                                 if ((pTemp = strchr(szFieldValue, MSG_CH_SEMICOLON)) == NULL) {
6514                                         if ((pTemp = strchr(szFieldValue, MSG_CH_SP)) != NULL)
6515                                                 *pTemp++ = MSG_CH_NULL;
6516                                 } else {
6517                                         *pTemp++ = MSG_CH_NULL;
6518                                 }
6519
6520                                 pType->encoding = _MsgGetCode(MSG_ENCODING, szFieldValue);
6521
6522                                 MSG_DEBUG("_MsgParsePartHeader: Content-Encoding = %s     %d\n", pFieldValue, pType->encoding);
6523
6524                                 if (pTemp != NULL)
6525                                         _MsgParseParameter(pType, pTemp);
6526
6527                                 break;
6528
6529                         case MSG_FIELD_CONTENT_DISPOSITION:
6530                                 if ((pTemp = strchr(szFieldValue, MSG_CH_SEMICOLON)) == NULL) {
6531                                         if ((pTemp = strchr(szFieldValue, MSG_CH_SP)) != NULL)
6532                                                 *pTemp++ = MSG_CH_NULL;
6533                                 } else {
6534                                         *pTemp++ = MSG_CH_NULL;
6535                                 }
6536
6537                                 pType->disposition = _MsgGetCode(MSG_DISPOSITION, szFieldValue);
6538
6539                                 MSG_DEBUG("_MsgParsePartHeader: Content-Disposition = %s     %d\n", pFieldValue, pType->disposition);
6540
6541                                 if (pTemp != NULL)
6542                                         _MsgParseParameter(pType, pTemp);
6543
6544                                 break;
6545
6546 #ifdef __SUPPORT_DRM__
6547
6548                         case MSG_FIELD_CONTENT_NAME:
6549                                 /* add to parse DCF header such as,
6550                                  * Right-Issuer, Content-Name, and Content-Description.
6551                                  */
6552                                 MsgMIMERemoveQuote(szFieldValue);
6553                                 pType->drmInfo.szContentName = MsgStrCopy(szFieldValue);
6554                                 break;
6555
6556                         case MSG_FIELD_CONTENT_DESCRIPTION:
6557                                 MsgMIMERemoveQuote(szFieldValue);
6558                                 pType->drmInfo.szContentDescription = MsgStrCopy(szFieldValue);
6559                                 break;
6560
6561                         case MSG_FIELD_CONTENT_VENDOR:
6562                                 MsgMIMERemoveQuote(szFieldValue);
6563                                 pType->drmInfo.szContentVendor = MsgStrCopy(szFieldValue);
6564                                 break;
6565
6566                         case MSG_FIELD_RIGHT_ISSUER:
6567                                 MsgMIMERemoveQuote(szFieldValue);
6568                                 pType->drmInfo.szRightIssuer = MsgStrCopy(szFieldValue);
6569                                 break;
6570
6571
6572                         case MSG_FIELD_DRM_CONVERTED:
6573                                 MsgMIMERemoveQuote(szFieldValue);
6574                                 pType->nDrmConvertedStatus = atoi(szFieldValue);
6575                                 break;
6576
6577 #endif
6578                         case MSG_FILED_CONTENT_REPLACE_POS:
6579                                 MsgMIMERemoveQuote(szFieldValue);
6580                                 strncpy(pType->szContentRepPos, szFieldValue, sizeof(pType->szContentRepPos) - 1);
6581                                 break;
6582
6583                         case MSG_FILED_CONTENT_REPLACE_SIZE:
6584                                 MsgMIMERemoveQuote(szFieldValue);
6585                                 strncpy(pType->szContentRepSize, szFieldValue, sizeof(pType->szContentRepSize) - 1);
6586                                 break;
6587
6588                         case MSG_FILED_CONTENT_REPLACE_INDEX:
6589                                 MsgMIMERemoveQuote(szFieldValue);
6590                                 strncpy(pType->szContentRepIndex, szFieldValue, sizeof(pType->szContentRepIndex) - 1);
6591                                 break;
6592
6593                         default:
6594                                 MSG_DEBUG("_MsgParsePartHeader: Unhandled field \n");
6595                                 break;
6596                         }
6597                 }
6598
6599                 if (szFieldValue != NULL && szFieldValue != szTempBuf) {
6600                         free(szFieldValue);
6601                         szFieldValue = NULL;
6602                 }
6603
6604         }
6605
6606         return true;
6607 }
6608
6609 char *MsgRemoveQuoteFromFilename(char *pSrc)
6610 {
6611         int cLen = 0;   // length of pBuff
6612         char *pBuff = NULL;
6613
6614         if (pSrc == NULL) {
6615                 MSG_DEBUG("MsgRemoveQuoteFromFilename: pSrc is Null\n");
6616                 return NULL;
6617         }
6618
6619         cLen = strlen(pSrc);
6620
6621         pBuff = (char *)malloc(cLen + 1);
6622
6623         if (pBuff == NULL) {
6624                 MSG_DEBUG("MsgRemoveQuoteFromFilename: pBuff mem alloc fail!\n");
6625                 return NULL;
6626         }
6627         memset(pBuff, 0 , sizeof(char)*(cLen + 1));
6628
6629         // remove front quote
6630         if (pSrc[0] == MSG_CH_QUOT) {
6631                 cLen--;
6632                 strncpy(pBuff, &pSrc[1], cLen);
6633                 pBuff[cLen] = '\0';
6634         }
6635
6636         if (pSrc[0] == MSG_CH_LF) {
6637                 cLen--;
6638                 strncpy(pBuff, &pSrc[1], cLen);
6639         } else {
6640                 strcpy(pBuff, pSrc);
6641         }
6642
6643         // remove last qoute
6644         if (pBuff[cLen-1] == MSG_CH_QUOT) {
6645                 pBuff[cLen-1] = '\0';
6646         }
6647
6648         return pBuff;
6649 }
6650
6651 bool MsgIsMultipart(int type)
6652 {
6653         if (type == MIME_MULTIPART_RELATED || type == MIME_APPLICATION_VND_WAP_MULTIPART_MIXED ||
6654                 type == MIME_APPLICATION_VND_WAP_MULTIPART_RELATED || type == MIME_APPLICATION_VND_WAP_MULTIPART_ASTERIC ||
6655                 type == MIME_MULTIPART_MIXED || type == MIME_MULTIPART_REPORT) {
6656                 return true;
6657         } else {
6658                 return false;
6659         }
6660 }
6661
6662
6663 bool MsgIsHexChar(char *pSrc)
6664 {
6665         int cIndex = 0;
6666         int cLen = 0;
6667         bool bRet = false;
6668
6669         cLen = strlen(pSrc);
6670
6671         for (cIndex = 0; cIndex < cLen ; cIndex++) {
6672                 if ((pSrc[cIndex] >= '0' && pSrc[cIndex] <= '9') || (pSrc[cIndex] >= 'A'&& pSrc[cIndex] <= 'F') ||
6673                         (pSrc[cIndex] >= 'a' && pSrc[cIndex] <= 'f')) {
6674                         bRet = true;
6675                 } else {
6676                         return false;
6677                 }
6678         }
6679
6680         return bRet;
6681 }
6682
6683 char _MsgConvertHexValue(char *pSrc)
6684 {
6685         int ch = 0;
6686         int cIndex = 0;
6687         int cLen = 0;
6688         char ResultChar;
6689         unsigned char uCh[2] = {0,};
6690
6691         cLen = strlen(pSrc);
6692
6693         for (cIndex = 0; cIndex < cLen ; cIndex += 2) {
6694                 uCh[0] = __MsgConvertCharToInt(pSrc[cIndex]);
6695                 uCh[1] = __MsgConvertCharToInt(pSrc[cIndex+1]);
6696                 ch = (int)uCh[0]<<4|uCh[1];
6697         }
6698
6699         ResultChar = (char)ch;
6700
6701         return ResultChar;
6702 }
6703
6704 int __MsgConvertCharToInt(char ch)
6705 {
6706         if (ch>='0' && ch<='9') {
6707                 return ch - '0';
6708         } else if (ch>='a'&& ch <='f') {
6709                 return ch -'a'+10;
6710         } else if (ch>='A'&& ch <='F') {
6711                 return ch -'A'+10;
6712         } else {
6713                 return 0;
6714         }
6715 }
6716
6717 bool MsgCopyNestedMsgType(MsgType *pMsgType1, MsgType *pMsgType2)
6718 {
6719         if (pMsgType1->section == INVALID_HOBJ)
6720                 pMsgType1->section = pMsgType2->section;
6721
6722 #ifdef __SUPPORT_DRM__
6723         int             length = 0;
6724
6725         if (pMsgType1->drmInfo.drmType == MSG_DRM_TYPE_NONE)
6726                 pMsgType1->drmInfo.drmType = pMsgType2->drmInfo.drmType;
6727
6728
6729         if (pMsgType1->szContentID[0] == '\0') {
6730                 strcpy(pMsgType1->szContentID, pMsgType2->szContentID);
6731
6732                 if (pMsgType2->szContentID[0]) {
6733                         length = MsgStrlen(pMsgType2->szContentID);
6734                         if (pMsgType2->szContentID[0] == '<' && pMsgType2->szContentID[length - 1] == '>') {
6735                                 pMsgType1->drmInfo.szContentURI = MsgStrNCopy(pMsgType2->szContentID + 1, length - 2);
6736                         } else {
6737                                 pMsgType1->drmInfo.szContentURI = MsgStrCopy(pMsgType2->szContentID);
6738                         }
6739                 }
6740         } else {
6741                 length = MsgStrlen(pMsgType1->szContentID);
6742                 if (pMsgType1->szContentID[0] == '<' && pMsgType1->szContentID[length - 1] == '>') {
6743                         pMsgType1->drmInfo.szContentURI = MsgStrNCopy(pMsgType1->szContentID + 1, length - 2);
6744                 } else {
6745                         pMsgType1->drmInfo.szContentURI = MsgStrCopy(pMsgType1->szContentID);
6746                 }
6747         }
6748 #endif
6749
6750         if (pMsgType1->szContentLocation[0] == '\0')
6751                 strcpy(pMsgType1->szContentLocation, pMsgType2->szContentLocation);
6752
6753         /* Copy informations - we shoud open the pMsgType2's orgFile
6754          * concerning its offset and size.
6755          */
6756         if (pMsgType2->szOrgFilePath[0] != '\0')
6757                 strcpy(pMsgType1->szOrgFilePath, pMsgType2->szOrgFilePath);
6758
6759         if (pMsgType2->disposition != INVALID_HOBJ)
6760                 pMsgType1->disposition = pMsgType2->disposition;
6761
6762         if ((pMsgType1->type != MIME_APPLICATION_VND_OMA_DRM_MESSAGE && pMsgType1->type != MIME_APPLICATION_VND_OMA_DRM_CONTENT) &&
6763                  pMsgType2->encoding != INVALID_HOBJ)
6764                 pMsgType1->encoding = pMsgType2->encoding;
6765
6766         pMsgType1->contentSize = pMsgType2->contentSize;
6767         pMsgType1->offset = pMsgType2->offset;
6768         pMsgType1->size = pMsgType2->size;
6769         pMsgType1->type = pMsgType2->type;
6770
6771         MsgCopyNestedMsgParam(&(pMsgType1->param), &(pMsgType2->param));
6772
6773         if (pMsgType1->param.szName[0]) {
6774 #ifdef __SUPPORT_DRM__
6775                 pMsgType1->drmInfo.szContentName = MsgStrCopy(pMsgType2->param.szName);
6776 #endif
6777         }
6778
6779         return true;
6780 }
6781
6782
6783 bool MsgCopyNestedMsgParam(MsgContentParam *pParam1, MsgContentParam *pParam2)
6784 {
6785         if (pParam1->charset == MSG_CHARSET_UNKNOWN)
6786                 pParam1->charset = pParam2->charset;
6787
6788         if (pParam1->type == MIME_UNKNOWN)
6789                 pParam1->type = pParam2->type;
6790
6791         /* Don't copy pParam2->pPresentation */
6792
6793         /* For alternative: copy the boundary string */
6794         if (pParam2->szBoundary[0] !='\0')
6795                 strcpy(pParam1->szBoundary, pParam2->szBoundary);
6796
6797         if (pParam1->szFileName[0] =='\0')
6798                 strcpy(pParam1->szFileName, pParam2->szFileName);
6799
6800         if (pParam1->szName[0] =='\0')
6801                 strcpy(pParam1->szName, pParam2->szName);
6802
6803         if (pParam1->szStart[0] =='\0')
6804                 strcpy(pParam1->szStart, pParam2->szStart);
6805
6806         if (pParam1->szStartInfo[0] =='\0')
6807                 strcpy(pParam1->szStartInfo, pParam2->szStartInfo);
6808
6809         return true;
6810 }
6811
6812 bool MsgIsMultipartMixed(int type)
6813 {
6814         if (type == MIME_APPLICATION_VND_WAP_MULTIPART_MIXED || type == MIME_MULTIPART_MIXED) {
6815                 return true;
6816         } else {
6817                 return false;
6818         }
6819 }
6820
6821 bool MsgStrcpyWithoutCRLF(char *pOrg, char **ppNew)
6822 {
6823         int nLen = 0;
6824         int     i = 0;
6825         int index = 0;
6826         char*   pDest = NULL;
6827
6828
6829         nLen = strlen(pOrg);
6830
6831         pDest = (char *)malloc(nLen + 1);
6832
6833         if (pDest == NULL) {
6834                 MSG_DEBUG("MsgStrcpyWithoutCRLF: malloc is failed\n");
6835                 return false;
6836         }
6837
6838         memset(pDest, 0 , (nLen + 1));
6839
6840         for (i = 0; i < nLen ; i++) {
6841                 if (i < nLen - 2) {
6842                         if ((pOrg[i] == MSG_CH_CR && pOrg[i+1] == MSG_CH_LF && pOrg[i+2] == MSG_CH_TAB) ||
6843                                 (pOrg[i] == MSG_CH_CR && pOrg[i+1] == MSG_CH_LF && pOrg[i+2] == MSG_CH_SP)) {
6844                                 i+=2;
6845                                 continue;
6846                         }
6847                 }
6848                 pDest[index++] = pOrg[i];
6849         }
6850         *ppNew = pDest;
6851
6852         return true;
6853 }
6854
6855 bool MmsGetMsgAttrib(MmsMsgID msgID, MmsAttrib* pAttrib)
6856 {
6857         MmsMsg *pMsg = NULL;
6858
6859         memset(pAttrib, 0, sizeof(MmsAttrib));
6860         MmsPluginStorage::instance()->getMmsMessage(&pMsg);
6861         memcpy(pAttrib, &(pMsg->mmsAttrib), sizeof(MmsAttrib));
6862
6863         MSG_DEBUG("MmsGetMsgAttrib: msgID = %lu ---------------------\n", msgID);
6864
6865         if ('\0' != pMsg->szTrID[0])
6866                 MSG_DEBUG("szTrID = %s \n", pMsg->szTrID);
6867
6868         MSG_END();
6869         return true;
6870 }
6871
6872 bool __MsgIsInvalidFileNameChar(char ch)
6873 {
6874         if ((ch == 0x5C /* \ */) ||
6875                 (ch == 0x2F /* / */) ||
6876                 (ch == 0x3A /* : */) ||
6877                 (ch == 0x2A /* * */) ||
6878                 (ch == 0x3F /* ? */) ||
6879                 (ch == 0x22 /* " */) ||
6880                 (ch == 0x3C /* < */) ||
6881                 (ch == 0x3E /* > */) ||
6882                 (ch == 0x7C /* | */))
6883                 return true;
6884
6885         return false;
6886 }
6887
6888 bool _MmsDataUpdateLastStatus(MmsMsg *pMsg)
6889 {
6890         MmsMsgMultiStatus*      pStatus = NULL;
6891
6892         pStatus = pMsg->mmsAttrib.pMultiStatus;
6893
6894         while (pStatus != NULL) {
6895                 pStatus->bDeliveyrReportIsLast = false;
6896                 pStatus->bReadReplyIsLast = false;
6897                 pStatus = pStatus->pNext;
6898         }
6899
6900         return true;
6901 }
6902
6903
6904 bool MmsAddrUtilCompareAddr(char *pszAddr1, char *pszAddr2)
6905 {
6906         int len1;
6907         int len2;
6908         char *p;
6909
6910         MmsAddrUtilRemovePlmnString(pszAddr1);
6911         MmsAddrUtilRemovePlmnString(pszAddr2);
6912
6913         MSG_DEBUG("##### pszAddr1 = %s #####", pszAddr1);
6914         MSG_DEBUG("##### pszAddr2 = %s #####", pszAddr2);
6915         if (!strcmp(pszAddr1, pszAddr2))
6916                 return true;
6917
6918         len1 = strlen(pszAddr1);
6919         len2 = strlen(pszAddr2);
6920
6921         if (len1 > len2) {
6922                 p = strstr(pszAddr1, pszAddr2);
6923         } else {
6924                 p = strstr(pszAddr2, pszAddr1);
6925         }
6926
6927         if (p)
6928                 return true;
6929
6930         return false;
6931 }
6932
6933 int MsgGetLatin2UTFCodeSize(unsigned char *szSrc, int nChar)
6934 {
6935         int nCount = 0;
6936
6937         MSG_DEBUG("MsgGetLatin2UTFCodeSize: --------------- \n");
6938
6939         if ((szSrc == NULL) || (nChar <= 0)) {
6940                 MSG_DEBUG("MsgGetLatin2UTFCodeSize: szSrc is NULL !!!! --------------- \n");
6941                 return 0;
6942         }
6943
6944         while ((nChar > 0) && (*szSrc != '\0')) {
6945                 if (0x01 <= *szSrc && *szSrc <= 0x7F) {
6946                         nCount += 1;
6947                         szSrc++;
6948                         nChar--;
6949                 } else {
6950                         nCount += 2;
6951                         szSrc++;
6952                         nChar--;
6953                 }
6954         }
6955
6956         return nCount;
6957 }
6958
6959 int MsgLatin5code2UTF(unsigned char *des, int outBufSize, unsigned char *szSrc, int nChar)
6960 {
6961         unsigned char *org;
6962         unsigned char t1;
6963         unsigned char t2;
6964         unsigned short temp = 0;
6965
6966         org = des;
6967         outBufSize--;   //Null Character
6968
6969         while ((nChar > 0) && (*szSrc != '\0')) {
6970
6971                 if (*szSrc >= 0x01 && *szSrc <= 0x7F) { //basic common
6972                         temp = (unsigned short)(*szSrc);
6973
6974                         outBufSize --;
6975                         if (outBufSize < 0)
6976                                 goto __RETURN;
6977
6978                         *des = (unsigned char) ((*szSrc) & 0x007F);
6979
6980                         des++;
6981                         szSrc++;
6982                         nChar--;
6983                 } else if ((*szSrc == 0x00) || (*szSrc >= 0x80 && *szSrc <= 0x9F) ||
6984                                         (*szSrc >= 0xA0 && *szSrc <= 0xCF) || (*szSrc >= 0xD1 && *szSrc <= 0xDC) ||
6985                                         (*szSrc >= 0xDF && *szSrc <= 0xEF) || (*szSrc >= 0xF1 && *szSrc <= 0xFC) ||
6986                                         (*szSrc == 0xFF)) {//uni 0x00A0 ~ 0x00CF
6987
6988                         temp = (unsigned short)(*szSrc);
6989
6990                         outBufSize -= 2;
6991                         if (outBufSize < 0)
6992                                 goto __RETURN;
6993
6994                         t2 = (unsigned char) (temp & 0x003F);                           //      right most 6 bit
6995                         t1 = (unsigned char) ((temp & 0x07C0) >> 6);            //      right most 5 bit
6996
6997                         *des = 0xC0 | (t1 & 0x1F);
6998                         *(des+1) = 0x80 | (t2 & 0x3F);
6999
7000                         des += 2;
7001                         szSrc += 1;
7002                         nChar -= 1;
7003                 } else if (*szSrc == 0xD0) {//empty section OR vendor specific codes.
7004
7005                         temp = 0x011E;
7006
7007                         outBufSize -= 2;
7008                         if (outBufSize < 0)
7009                                 goto __RETURN;
7010
7011                         t2 = (unsigned char) (temp & 0x003F);                           //      right most 6 bit
7012                         t1 = (unsigned char) ((temp & 0x07C0) >> 6);            //      right most 5 bit
7013
7014                         *des = 0xC0 | (t1 & 0x1F);
7015                         *(des+1) = 0x80 | (t2 & 0x3F);
7016
7017                         des += 2;
7018                         szSrc += 1;
7019                         nChar -= 1;
7020                 } else if (*szSrc == 0xDD) {
7021                         temp = 0x0130;
7022
7023                         outBufSize -= 2;
7024                         if (outBufSize < 0)
7025                                 goto __RETURN;
7026
7027                         t2 = (unsigned char) (temp & 0x003F);                           //      right most 6 bit
7028                         t1 = (unsigned char) ((temp & 0x07C0) >> 6);            //      right most 5 bit
7029
7030                         *des = 0xC0 | (t1 & 0x1F);
7031                         *(des+1) = 0x80 | (t2 & 0x3F);
7032
7033                         des += 2;
7034                         szSrc += 1;
7035                         nChar -= 1;
7036                 } else if (*szSrc == 0xDE) {
7037                         temp = 0x015E;
7038
7039                         outBufSize -= 2;
7040                         if (outBufSize < 0)
7041                                 goto __RETURN;
7042
7043                         t2 = (unsigned char) (temp & 0x003F);                           //      right most 6 bit
7044                         t1 = (unsigned char) ((temp & 0x07C0) >> 6);            //      right most 5 bit
7045
7046                         *des = 0xC0 | (t1 & 0x1F);
7047                         *(des+1) = 0x80 | (t2 & 0x3F);
7048
7049                         des += 2;
7050                         szSrc += 1;
7051                         nChar -= 1;
7052                 } else if (*szSrc == 0xF0) {
7053                         temp = 0x011F;
7054                                 outBufSize -= 2;
7055                         if (outBufSize < 0)
7056                                 goto __RETURN;
7057
7058                         t2 = (unsigned char) (temp & 0x003F);                           //      right most 6 bit
7059                         t1 = (unsigned char) ((temp & 0x07C0) >> 6);            //      right most 5 bit
7060
7061                         *des = 0xC0 | (t1 & 0x1F);
7062                         *(des+1) = 0x80 | (t2 & 0x3F);
7063
7064                         des += 2;
7065                         szSrc += 1;
7066                         nChar -= 1;
7067                 } else if (*szSrc == 0xFD) {
7068                         temp = 0x0131;
7069
7070                         outBufSize -= 2;
7071
7072                         if (outBufSize < 0)
7073                                 goto __RETURN;
7074
7075                         t2 = (unsigned char) (temp & 0x003F);                           //      right most 6 bit
7076                         t1 = (unsigned char) ((temp & 0x07C0) >> 6);            //      right most 5 bit
7077
7078                         *des = 0xC0 | (t1 & 0x1F);
7079                         *(des+1) = 0x80 | (t2 & 0x3F);
7080
7081                         des += 2;
7082                         szSrc += 1;
7083                         nChar -= 1;
7084                 } else if (*szSrc == 0xFE) {
7085                         temp = 0x015F;
7086
7087                         outBufSize -= 2;
7088                         if (outBufSize < 0)
7089                                 goto __RETURN;
7090
7091                         t2 = (unsigned char) (temp & 0x003F);                           //      right most 6 bit
7092                         t1 = (unsigned char) ((temp & 0x07C0) >> 6);            //      right most 5 bit
7093
7094                         *des = 0xC0 | (t1 & 0x1F);
7095                         *(des+1) = 0x80 | (t2 & 0x3F);
7096
7097                         des += 2;
7098                         szSrc += 1;
7099                         nChar -= 1;
7100                 } else {
7101                         return -1;
7102                 }
7103         }
7104 __RETURN:
7105         *des = 0;
7106         return(des-org);
7107 }
7108
7109 int MsgGetLatin52UTFCodeSize(unsigned char *szSrc, int nChar)
7110 {
7111         int nCount = 0;
7112
7113         MSG_DEBUG("MsgGetLatin52UTFCodeSize: --------------- \n");
7114
7115         if ((szSrc == NULL) || (nChar <= 0))
7116                 return 0;
7117
7118         while ((nChar > 0) && (*szSrc != '\0')) {
7119                 if (*szSrc >= 0x01 && *szSrc <= 0x7F) {
7120                         nCount += 1;
7121                         szSrc++;
7122                         nChar--;
7123                 } else if (*szSrc == 0x00 || (*szSrc >= 0x80 && *szSrc <= 0x9F) ||
7124                                         (*szSrc >= 0xA0 && *szSrc <= 0xCF) || (*szSrc >= 0xD1 && *szSrc <= 0xDC) |
7125                                         (*szSrc >= 0xDF && *szSrc <= 0xEF) || (*szSrc >= 0xF1 && *szSrc <= 0xFC) ||
7126                                         *szSrc == 0xD0 || *szSrc == 0xDD || *szSrc == 0xDE || *szSrc == 0xF0 ||
7127                                         *szSrc == 0xFD || *szSrc == 0xFE        || *szSrc == 0xFF) { //uni 0x00A0 ~ 0x00CF
7128                         nCount += 2;
7129                         szSrc++;
7130                         nChar--;
7131                 } else {
7132                         return -1;
7133                 }
7134         }
7135         return nCount;
7136 }
7137
7138 int MsgLatin2UTF(unsigned char *des, int outBufSize, unsigned char *szSrc, int nChar)
7139 {
7140         unsigned char*  org;
7141         unsigned char   t1, t2;
7142
7143         MSG_DEBUG("MsgLatin2UTF: --------------- \n");
7144
7145         org = des;
7146         outBufSize--;                   // NULL character
7147
7148         while ((nChar > 0) && (*szSrc != '\0')) {
7149                 if (0x01 <= *szSrc && *szSrc <= 0x7F) {
7150                         /* check outbuffer's room for this UTF8 character */
7151
7152                         outBufSize --;
7153                         if (outBufSize < 0)
7154                                 goto __RETURN;
7155
7156                         *des = (unsigned char) (*szSrc & 0x007F);
7157
7158                         des++;
7159                         szSrc++;
7160                         nChar--;
7161                 } else {
7162                         /* check outbuffer's room for this UTF8 character */
7163
7164                         outBufSize -= 2;
7165                         if (outBufSize < 0)
7166                                 goto __RETURN;
7167
7168                         t2 = (unsigned char) (*szSrc & 0x003F);                         //      right most 6 bit
7169                         t1 = (unsigned char) ((*szSrc & 0xC0) >> 6);            //      right most 2 bit
7170
7171                         *des = 0xC0 | (t1 & 0x1F);
7172                         *(des + 1) = 0x80 | (t2 & 0x3F);
7173
7174                         des += 2;
7175                         szSrc += 1;
7176                         nChar -= 1;
7177                 }
7178         }
7179
7180 __RETURN:
7181
7182         *des = 0;
7183         return (des - org);
7184 }
7185
7186
7187 int MsgLatin7code2UTF(unsigned char *des, int outBufSize, unsigned char *szSrc, int nChar)
7188 {
7189         unsigned char *org;
7190         unsigned char t1;
7191         unsigned char t2;
7192         unsigned char t3;
7193         unsigned short temp = 0;
7194
7195         MSG_DEBUG("MsgUnicode2UTF: --------------- \n");
7196
7197         org = des;
7198         outBufSize--;   //Null Character
7199
7200         while ((nChar > 0) && (*szSrc != '\0')) {
7201                 if (*szSrc >= 0x01 && *szSrc <= 0x7F) {
7202                         temp = (unsigned short)(*szSrc);
7203
7204                         outBufSize --;
7205                         if (outBufSize < 0)
7206                                 goto __RETURN;
7207
7208                         *des = (unsigned char) (temp & 0x007F);
7209
7210                         des++;
7211                         szSrc++;
7212                         nChar--;
7213
7214                 } else if ((*szSrc == 0x00) || (*szSrc >= 0x80 && *szSrc <= 0x9F) ||
7215                                         (*szSrc >= 0xA3 && *szSrc <= 0xAD) || (*szSrc == 0xBB)) { // consider 0xA4, 0xA5
7216
7217                         temp = (unsigned short)(*szSrc);
7218
7219                         outBufSize -= 2;
7220                         if (outBufSize < 0)
7221                                 goto __RETURN;
7222
7223                         t2 = (unsigned char) (temp & 0x003F);                           //      right most 6 bit
7224                         t1 = (unsigned char) ((temp & 0xC0) >> 6);              //      right most 2 bit
7225
7226                         *des = 0xC0 | (t1 & 0x1F);
7227                         *(des + 1) = 0x80 | (t2 & 0x3F);
7228
7229                         des += 2;
7230                         szSrc += 1;
7231                         nChar -= 1;
7232                 } else if (*szSrc == 0xA0) {
7233                         temp = 0x0020;
7234                         //*des = temp to utf-8
7235                         outBufSize--;
7236                         if (outBufSize < 0)
7237                                 goto __RETURN;
7238
7239                         *des = (unsigned char) (temp & 0x007F);
7240
7241                         des++;
7242                         szSrc++;
7243                         nChar--;
7244
7245                 } else if (*szSrc == 0xA1) {
7246                         temp = 0x2018;
7247
7248                         outBufSize -= 3;
7249                         if (outBufSize < 0)
7250                                 goto __RETURN;
7251
7252                         t3 = (unsigned char) (temp & 0x003F);                                   //      right most 6 bit
7253                         t2 = (unsigned char) ((temp & 0x0FC0) >> 6);                    //      right most 6 bit
7254                         t1 = (unsigned char) ((temp & 0xF000) >> 12);                   //      right most 4 bit
7255
7256                         *des = 0xE0 | (t1 & 0x0F);
7257                         *(des+1) = 0x80 | (t2 & 0x3F);
7258                         *(des+2) = 0x80 | (t3 & 0x3F);
7259
7260                         des += 3;
7261                         szSrc += 1;
7262                         nChar -= 1;
7263
7264                 } else if (*szSrc == 0xA2) {
7265                         temp = 0x2019;
7266
7267                         outBufSize -= 3;
7268                         if (outBufSize < 0)
7269                                 goto __RETURN;
7270
7271                         t3 = (unsigned char) (temp & 0x003F);                                   //      right most 6 bit
7272                         t2 = (unsigned char) ((temp & 0x0FC0) >> 6);                    //      right most 6 bit
7273                         t1 = (unsigned char) ((temp & 0xF000) >> 12);                   //      right most 4 bit
7274
7275                         *des = 0xE0 | (t1 & 0x0F);
7276                         *(des+1) = 0x80 | (t2 & 0x3F);
7277                         *(des+2) = 0x80 | (t3 & 0x3F);
7278
7279                         des += 3;
7280                         szSrc += 1;
7281                         nChar -= 1;
7282
7283                 } else if (*szSrc == 0xAF) {
7284                         temp = 0x2015;
7285
7286                         outBufSize -= 3;
7287                         if (outBufSize < 0)
7288                                 goto __RETURN;
7289
7290                         t3 = (unsigned char) (temp & 0x003F);                                   //      right most 6 bit
7291                         t2 = (unsigned char) ((temp & 0x0FC0) >> 6);                    //      right most 6 bit
7292                         t1 = (unsigned char) ((temp & 0xF000) >> 12);                   //      right most 4 bit
7293
7294                         *des = 0xE0 | (t1 & 0x0F);
7295                         *(des+1) = 0x80 | (t2 & 0x3F);
7296                         *(des+2) = 0x80 | (t3 & 0x3F);
7297
7298                         des += 3;
7299                         szSrc += 1;
7300                         nChar -= 1;
7301
7302                 } else if (0xB0 <= *szSrc && *szSrc <= 0xB4) { //0x00B0 ~ 0x00B4
7303
7304                         temp = (unsigned short)(*szSrc);
7305
7306                         outBufSize -= 2;
7307                         if (outBufSize < 0)
7308                                 goto __RETURN;
7309
7310                         t2 = (unsigned char) (temp & 0x003F);                           //      right most 6 bit
7311                         t1 = (unsigned char) ((temp & 0x07C0) >> 6);            //      right most 5 bit
7312
7313                         *des = 0xC0 | (t1 & 0x1F);
7314                         *(des+1) = 0x80 | (t2 & 0x3F);
7315
7316                         des += 2;
7317                         szSrc += 1;
7318                         nChar -= 1;
7319
7320                 } else if ((0xB5 <= *szSrc &&  *szSrc <= 0xBA) ||
7321                                 (0xBC <= *szSrc && *szSrc <= 0xD1) ||
7322                                 (0xD3 <= *szSrc && *szSrc <= 0xFE)) {
7323                         temp= (unsigned short)(*szSrc + 0x02D0);
7324
7325                         outBufSize -= 2;
7326                         if (outBufSize < 0)
7327                                 goto __RETURN;
7328
7329                         t2 = (unsigned char) (temp & 0x003F);                           //      right most 6 bit
7330                         t1 = (unsigned char) ((temp & 0x07C0) >> 6);            //      right most 5 bit
7331
7332                         *des = 0xC0 | (t1 & 0x1F);
7333                         *(des+1) = 0x80 | (t2 & 0x3F);
7334
7335                         des += 2;
7336                         szSrc += 1;
7337                         nChar -= 1;
7338
7339                 } else {
7340                         return -1;
7341                 }
7342
7343         }
7344
7345 __RETURN:
7346         *des = 0;
7347         return(des - org);
7348 }
7349
7350 int MsgGetLatin72UTFCodeSize(unsigned char *szSrc, int nChar)
7351 {
7352         int nCount = 0;
7353
7354         MSG_DEBUG("MsgGetLatin72UTFCodeSize: --------------- \n");
7355
7356         if ((szSrc == NULL) || (nChar <= 0))
7357                 return 0;
7358
7359         while ((nChar > 0) && (*szSrc != '\0')) {
7360
7361                 if ((*szSrc >= 0x01 && *szSrc <= 0x7F) || (*szSrc == 0xA0)) {
7362                         nCount += 1;
7363                         szSrc++;
7364                         nChar--;
7365                 } else if (*szSrc == 0x00 || (0x80 <= *szSrc && *szSrc <= 0x9F) || (0xA3 <= *szSrc && *szSrc <= 0xAD) ||
7366                                         (0xB0 <= *szSrc && *szSrc <= 0xB4) || (0xB5 <= *szSrc && *szSrc <= 0xFE)) {
7367                         nCount += 2;
7368                         szSrc++;
7369                         nChar--;
7370                 } else if (*szSrc == 0xA1 ||*szSrc == 0xA2 || *szSrc == 0xAF) {
7371                         nCount += 3;
7372                         szSrc += 1;
7373                         nChar -= 1;
7374
7375                 } else {
7376                         return -1;
7377                 }
7378         }
7379         return nCount;
7380 }
7381
7382 int MsgUnicode2UTF(unsigned char *des, int outBufSize, unsigned short *szSrc, int nChar)
7383 {
7384         unsigned char *org;
7385         unsigned char t1;
7386         unsigned char t2;
7387         unsigned char t3;
7388
7389         MSG_DEBUG("MsgUnicode2UTF: --------------- \n");
7390
7391         org = des;
7392         outBufSize--;                   // NULL character
7393
7394         while ((nChar > 0) && (*szSrc != '\0')) {
7395                 if (0x0001 <= *szSrc && *szSrc <= 0x007F) {
7396                         /* check outbuffer's room for this UTF8 character */
7397
7398                         outBufSize --;
7399                         if (outBufSize < 0)
7400                                 goto __RETURN;
7401
7402                         *des = (unsigned char) (*szSrc & 0x007F);
7403
7404                         des++;
7405                         szSrc++;
7406                         nChar--;
7407                 } else if  ((*szSrc == 0x0000) || (0x0080 <= *szSrc && *szSrc <= 0x07FF)) {
7408                         /* check outbuffer's room for this UTF8 character */
7409
7410                         outBufSize -= 2;
7411                         if (outBufSize < 0)
7412                                 goto __RETURN;
7413
7414                         t2 = (unsigned char) (*szSrc & 0x003F);                         //      right most 6 bit
7415                         t1 = (unsigned char) ((*szSrc & 0x07C0) >> 6);          //      right most 5 bit
7416
7417                         *des = 0xC0 | (t1 & 0x1F);
7418                         *(des+1) = 0x80 | (t2 & 0x3F);
7419
7420                         des += 2;
7421                         szSrc += 1;
7422                         nChar -= 1;
7423                 } else {
7424                         /* check outbuffer's room for this UTF8 character */
7425
7426                         outBufSize -= 3;
7427                         if (outBufSize < 0)
7428                                 goto __RETURN;
7429
7430                         t3 = (unsigned char) (*szSrc & 0x003F);                                 //      right most 6 bit
7431                         t2 = (unsigned char) ((*szSrc & 0x0FC0) >> 6);                  //      right most 6 bit
7432                         t1 = (unsigned char) ((*szSrc & 0xF000) >> 12);                 //      right most 4 bit
7433
7434                         *des = 0xE0 | (t1 & 0x0F);
7435                         *(des+1) = 0x80 | (t2 & 0x3F);
7436                         *(des+2) = 0x80 | (t3 & 0x3F);
7437
7438                         des += 3;
7439                         szSrc += 1;
7440                         nChar -= 1;
7441                 }
7442         }
7443
7444 __RETURN:
7445
7446         *des = 0;
7447         return (des - org);
7448 }
7449
7450 int MsgGetUnicode2UTFCodeSize(unsigned short *szSrc, int nChar)
7451 {
7452         int nCount = 0;
7453
7454         MSG_DEBUG("MsgGetUnicode2UTFCodeSize: --------------- \n");
7455
7456         if ((szSrc == NULL) || (nChar <= 0)) {
7457                 MSG_DEBUG("MsgGetUnicode2UTFCodeSize: szSrc is NULL !!!! --------------- \n");
7458                 return 0;
7459         }
7460
7461         while ((nChar > 0) && (*szSrc != '\0')) {
7462                 if (0x0001 <= *szSrc && *szSrc <= 0x007F) {
7463                         nCount += 1;
7464                         szSrc++;
7465                         nChar--;
7466                 } else if  ((*szSrc == 0x0000) || (0x0080 <= *szSrc && *szSrc <= 0x07FF)) {
7467                         nCount += 2;
7468                         szSrc++;
7469                         nChar--;
7470                 } else {
7471                         nCount += 3;
7472                         szSrc++;
7473                         nChar--;
7474                 }
7475         }
7476
7477         return nCount;
7478 }
7479
7480 bool MmsAddrUtilCheckEmailAddress(char *pszAddr)
7481 {
7482         if (!pszAddr || pszAddr[0] == 0)
7483                 return false;
7484
7485         if (!strchr (pszAddr, MSG_MMS_CH_EMAIL_AT))
7486                 return false;
7487
7488         return true;
7489 }
7490
7491 bool MmsAddrUtilRemovePlmnString(char *pszAddr)
7492 {
7493         char *pszAddrCopy = NULL;
7494         char *pszStrStart = NULL;
7495         char *pszStrTemp = NULL;
7496         int strLen = 0;
7497
7498         if ((!pszAddr) || (pszAddr[0] == 0)) {
7499                 MSG_DEBUG("MmsAddrUtilRemovePlmnString: pszAddr is null or zero\n");
7500                 return false;
7501         }
7502
7503         strLen = strlen(pszAddr);
7504
7505         pszAddrCopy = (char*)calloc(1,strLen + 1);
7506         if (!pszAddrCopy) {
7507                 MSG_DEBUG("MmsAddrUtilRemovePlmnString: pszAddrCopy is NULL, mem alloc failed\n");
7508                 return false;
7509         }
7510
7511         strcpy(pszAddrCopy, pszAddr);
7512
7513
7514         pszAddr[0] = 0;
7515         pszStrStart = pszAddrCopy;
7516
7517         while (true) {
7518                 char*   pszStrEnd = NULL;
7519                 int             addressLen = 0;
7520
7521                 if (MmsAddrUtilCheckEmailAddress(pszAddrCopy))
7522                         pszStrEnd = strstr(pszStrStart, "/TYPE=PLMN");
7523                 else
7524                         pszStrEnd = strstr(pszStrStart, "/");
7525
7526                 if (!pszStrEnd) {
7527                         // "/TYPE=PLMN" not found
7528
7529                         int remainedLen = strlen(pszStrStart);
7530
7531                         if (remainedLen <= 0)
7532                                 break;
7533
7534                         strcat(pszAddr, pszStrStart);
7535
7536                         break;
7537                 }
7538
7539                 // Get one address length
7540                 addressLen = pszStrEnd - pszStrStart;
7541
7542                 strncat(pszAddr, pszStrStart, addressLen);
7543
7544                 // Find next address
7545                 pszStrStart = pszStrEnd;
7546
7547                 pszStrTemp = strstr(pszStrStart, MSG_MMS_STR_ADDR_DELIMETER);
7548
7549                 if (pszStrTemp) {
7550                         addressLen = pszStrTemp - pszStrEnd;
7551                         pszStrStart += addressLen;
7552                 } else {
7553                         pszStrStart += strlen(pszStrEnd);
7554                 }
7555
7556                 if (pszStrStart[0] == 0)        // end of string
7557                         break;
7558
7559
7560                 strcat(pszAddr, MSG_MMS_STR_ADDR_DELIMETER);    // add ';'
7561                 pszStrStart++;  // remove ';'
7562         }
7563
7564         if (pszAddr[0] == 0)
7565                 strcpy(pszAddr, pszAddrCopy);
7566
7567         free(pszAddrCopy);
7568
7569         return true;
7570 }
7571
7572 int MsgCutUTFString(unsigned char *des, int outBufSize, unsigned char *szSrc, int nChar)
7573 {
7574         unsigned char *org;
7575
7576         MSG_DEBUG("MsgCutUTFString: --------------- \n");
7577
7578         org = des;
7579         outBufSize--;                   // NULL character
7580
7581         while ((nChar > 0) && (*szSrc != '\0')) {
7582                 if (*szSrc < 0x80) {
7583                         outBufSize --;
7584                         if (outBufSize < 0)
7585                                 goto __RETURN;
7586
7587                         *des = *szSrc;
7588                         des++;
7589                         szSrc++;
7590                 } else if  (((0xC0 <= *szSrc) && (*szSrc < 0xE0)) && (*(szSrc+1) >= 0x80)) {
7591                         outBufSize -= 2;
7592                         if (outBufSize < 0)
7593                                 goto __RETURN;
7594
7595                         *des = *szSrc;
7596                         *(des + 1) = *(szSrc + 1);
7597
7598                         des += 2;
7599                         szSrc += 2;
7600                 } else if  ((*szSrc >= 0xE0) && (*(szSrc+1) >= 0x80) && (*(szSrc+2) >= 0x80)) {
7601                         outBufSize -= 3;
7602                         if (outBufSize < 0)
7603                                 goto __RETURN;
7604
7605                         *des = *szSrc;
7606                         *(des + 1) = *(szSrc + 1);
7607                         *(des + 2) = *(szSrc + 2);
7608
7609                         des += 3;
7610                         szSrc += 3;
7611                 } else {
7612                         outBufSize --;
7613                         if (outBufSize < 0)
7614                                 goto __RETURN;
7615
7616                         *des = *szSrc;
7617                         des++;
7618                         szSrc++;
7619                         MSG_DEBUG("MsgCutUTFString: utf8 incorrect range!\n");
7620                 }
7621
7622                 nChar--;
7623         }
7624
7625 __RETURN:
7626
7627         *des = 0;
7628         return (des - org);
7629 }
7630
7631 void MsgMIMERemoveQuote(char *szSrc)
7632 {
7633         int             length = 0;
7634
7635         length = MsgStrlen(szSrc);
7636         if (szSrc[0] == MSG_CH_QUOT && szSrc[length-1] == MSG_CH_QUOT) {
7637                 int index = 0;
7638
7639                 for (index = 0; index < length-2; index++)
7640                         szSrc[index] = szSrc[index+1];
7641                 szSrc[index] = '\0';
7642         }
7643 }
7644
7645 bool MsgLoadDataToDecodeBuffer(FILE *pFile, char **ppBuf, int *pPtr, int *pOffset, char *pInBuf1, char *pInBuf2, int maxLen, int *pBufLen, int endOfFile)
7646 {
7647         MSG_BEGIN();
7648         int nRead = 0;
7649         int length= 0;
7650
7651         MSG_DEBUG("MsgLoadDataToDecodeBuffer: \n");
7652
7653         if (pFile == NULL) {
7654                 MSG_DEBUG("Error");
7655
7656                 *pBufLen = 0;
7657                 return false;
7658         }
7659
7660         if (pPtr == NULL || pInBuf1 == NULL || pInBuf2 == NULL) {
7661                 MSG_DEBUG("Error");
7662
7663                 *pBufLen = 0;
7664                 return false;
7665         }
7666
7667         if (*pBufLen == 0) {
7668                 length = maxLen - (*pPtr);
7669         } else {
7670                 length = (*pBufLen) - (*pPtr);
7671         }
7672
7673         if (length < 0)
7674                 length = 0;
7675
7676         if ((*ppBuf) == NULL) {
7677                 memset(pInBuf1, 0, maxLen);
7678                 (*ppBuf) = pInBuf1;
7679         } else if ((*ppBuf) == pInBuf1) {
7680                 memset(pInBuf2, 0, maxLen);
7681                 if (length)
7682                         memcpy(pInBuf2, pInBuf1 + (*pPtr), length);
7683                 (*ppBuf) = pInBuf2;
7684         } else {
7685                 memset(pInBuf1, 0, maxLen);
7686                 if (length)
7687                         memcpy(pInBuf1, pInBuf2 + (*pPtr), length);
7688                 (*ppBuf) = pInBuf1;
7689         }
7690
7691         (*pPtr) = 0;
7692
7693         if (*pOffset == endOfFile) {
7694                 *pBufLen = length;
7695                 return true;
7696         }
7697
7698         if (maxLen == length) {
7699                 /* (*pPtr) was 0 */
7700                 if (MsgReadFileForDecode(pFile, (*ppBuf), maxLen, &nRead) == false)
7701                         return false;
7702
7703                 *pBufLen = nRead;
7704         } else {
7705                 if (MsgReadFileForDecode(pFile, (*ppBuf) + length, maxLen - length, &nRead) == false)
7706                         return false;
7707
7708                 *pBufLen = length + nRead;
7709         }
7710
7711         if ((*pOffset = MsgFtell(pFile)) == -1L) {
7712                 MSG_DEBUG("MsgFtell Error");
7713                 return false;
7714         }
7715
7716         MSG_END();
7717
7718         return true;
7719 }
7720
7721
7722 bool MsgGetTypeByFileName(int *type, char *szFileName)
7723 {
7724         char *pExt   = NULL;
7725         AvCodecType AvType = AV_CODEC_NONE;
7726
7727         pExt = strrchr(szFileName, '.');
7728         if (pExt == NULL || pExt[0] == '\0')
7729                 goto __CATCH;
7730
7731         pExt++;
7732
7733         if (strcasecmp(pExt, "mp4") == 0 ||strcasecmp(pExt, "mpeg4") == 0 ||strcasecmp(pExt, "3gp") == 0 ||strcasecmp(pExt, "3gpp") == 0) {
7734
7735                 if (szFileName[0] != '/')
7736                         goto __CATCH;
7737
7738                 AvType = AvGetFileCodecType(szFileName);
7739                 MSG_DEBUG("MsgGetTypeByFileName:AvType(0x%x)\n", AvType);
7740
7741                 switch (AvType) {
7742                 case AV_DEC_AUDIO_MPEG4:
7743                         *type = MIME_AUDIO_MP4;
7744                         break;
7745
7746                 case AV_DEC_VIDEO_MPEG4:
7747                         *type = MIME_VIDEO_MP4;
7748                         break;
7749
7750                 default:
7751                         *type = MIME_VIDEO_3GPP;
7752                         break;
7753                 }
7754                 return true;
7755         }
7756
7757         if (strcasecmp(pExt, "amr") == 0) {
7758                 *type = MIME_AUDIO_AMR;
7759                 return true;
7760         } else if ((strcasecmp(pExt, "mid") == 0) || (strcasecmp(pExt, "midi") == 0)) {
7761                 *type = MIME_AUDIO_MIDI;
7762                 return true;
7763         } else if (strcasecmp(pExt, "imy") == 0) {
7764                 *type = MIME_TEXT_X_IMELODY;
7765                 return true;
7766         }
7767
7768         *type = MimeGetMimeFromExtInt((const char*)pExt);
7769         MSG_DEBUG("MsgGetTypeByFileName: szFileName = %s     type = %d \n", szFileName, type);
7770         return true;
7771
7772
7773 __CATCH:
7774
7775         *type = MIME_UNKNOWN;
7776         MSG_DEBUG("MsgGetTypeByFileName: szFileName = %s     type = %d \n", szFileName, type);
7777
7778         return false;
7779
7780 }
7781
7782
7783 /*
7784  *      This function write media data from raw data to file.
7785  *      @param  pMsg
7786  *      @param  pPartBody
7787  *      @param  pszMailboxPath  : path of mailbox
7788  *      @param  pszMsgFilename  : name of msg file
7789  *      @param  index                   : used for file naming
7790  *      @param  bSave                   : if true, file will be save otherwise just filename will be stored.
7791  */
7792 bool _MmsMultipartSaveAsTempFile(MsgType *pPartType, MsgBody *pPartBody, char *pszMailboxPath, char *pszMsgFilename, int index, bool bSave)
7793 {
7794         FILE *pFile = NULL;
7795 //      char *pExt = NULL;
7796         char szFileName[MSG_FILENAME_LEN_MAX+1] = {0, };        // file name of temp file
7797         char szFullPath[MSG_FILEPATH_LEN_MAX] = {0, }; // full absolute path of temp file.
7798
7799         MSG_DEBUG("****   _MmsSaveMediaData:  [Multi part]  START  ***\n");
7800
7801         if (!pPartType) {
7802                 MSG_DEBUG("pPartType is NULL\n");
7803                 return true; // why true value is retured ??? ; false;
7804         }
7805
7806         if (pPartType->param.szName[0] == '\0' && pPartType->param.szFileName[0] == '\0')
7807                 snprintf(pPartType->param.szName, sizeof(pPartType->param.szName), "%s", pPartType->param.szFileName);
7808
7809         if (pPartType->param.szName[0] != '\0') {
7810                 snprintf(szFileName, MSG_FILENAME_LEN_MAX+1, "%s", pPartType->param.szName);
7811         } else {
7812                 snprintf(szFileName, MSG_FILENAME_LEN_MAX+1, "%lu", (unsigned long)index);
7813         }
7814
7815
7816 #ifndef __SUPPORT_DRM__
7817         MsgMakeFileName(pPartType->type, szFileName, 0);        //FL & CD -> extension(.dm) SD -> extension(.dcf)
7818 #else
7819         MsgMakeFileName(pPartType->type, szFileName, pPartType->drmInfo.drmType, 0);    //FL & CD -> extension(.dm) SD -> extension(.dcf)
7820         if (MsgDRMIsForwardLockType(pPartType->drmInfo.drmType))
7821                 MsgChangeDrm2FileName(szFileName);
7822 #endif
7823
7824
7825         snprintf(szFullPath, MSG_FILEPATH_LEN_MAX, "%s%s.dir/%s", pszMailboxPath, pszMsgFilename, szFileName);  // get absolute path of each temp file of each part
7826         snprintf(pPartType->param.szFileName, MSG_FILENAME_LEN_MAX+1, "%s.dir/%s", pszMsgFilename, szFileName);         // store relative path of each temp file of each part including sub folder.
7827
7828         if (pPartType->type == MIME_APPLICATION_OCTET_STREAM)
7829                 MsgGetTypeByFileName(&pPartType->type, szFullPath);
7830
7831         // save file
7832         if (bSave) {
7833                 if ((pFile = MsgOpenFile(szFullPath, "wb+")) == NULL) {
7834                         MSG_DEBUG("MsgOpenFile failed");
7835                         goto __CATCH;
7836                 }
7837
7838                 if (MmsGetMediaPartData(pPartType, pPartBody, pFile) == false) {
7839                         MSG_DEBUG("MmsGetMediaPartData fail [index:%d]\n", index);
7840                         goto __CATCH;
7841                 }
7842
7843                 MsgCloseFile(pFile);
7844                 pFile = NULL;
7845                 if (pPartType->drmInfo.drmType != MSG_DRM_TYPE_NONE) {
7846                         MsgDrmRegisterFile(MSG_MODE_FILE, szFullPath, strlen(szFullPath));
7847
7848                         /* change szDrm2FullPath as current content path*/
7849                         if (pPartType->drmInfo.szDrm2FullPath) {
7850                                 free(pPartType->drmInfo.szDrm2FullPath);
7851                                 pPartType->drmInfo.szDrm2FullPath = MsgStrCopy(szFullPath);
7852                         }
7853                 }
7854
7855         }
7856         MSG_DEBUG("****   MmsGetMediaPartData:  [Multi part]  E  N  D  (Successfully)  ***\n");
7857
7858         return true;
7859
7860 __CATCH:
7861
7862         if (pFile != NULL) {
7863                 MsgCloseFile(pFile);
7864                 pFile = NULL;
7865         }
7866
7867         return false;
7868 }
7869
7870
7871
7872 bool MmsGetMediaPartData(MsgType *pPartType, MsgBody *pPartBody, FILE* pFile)
7873 {
7874         int nRead = 0;
7875         int nRead2 = 0;
7876         char *pData = NULL;
7877         char *pNewData = NULL;
7878         char *pTempData = NULL;
7879         int msgEncodingValue = 0;
7880         int msgTypeValue = 0;
7881         int msgCharsetValue     = 0;
7882         int cidLen = 0;
7883         char *szCid = NULL;
7884         int offset = 0;
7885         int size = 0;
7886
7887         msgEncodingValue = pPartType->encoding;
7888         msgTypeValue = pPartType->type;
7889         msgCharsetValue = pPartType->param.charset;
7890
7891         cidLen = MsgStrlen(szCid);
7892
7893         offset = pPartBody->offset;
7894         size = pPartBody->size;
7895
7896         if (pPartBody->szOrgFilePath[0]) {
7897                 pTempData = MsgOpenAndReadMmsFile(pPartBody->szOrgFilePath, offset, size, &nRead);
7898
7899                 if (pTempData == NULL) {
7900                         MSG_DEBUG("MmsGetMediaPartData : pTempData read fail\n");
7901                         goto __CATCH;
7902                 }
7903
7904                 pData = pTempData;
7905         } else if (pPartBody->body.pText) {
7906                 pData = pPartBody->body.pText;
7907                 nRead = pPartBody->size;
7908         }
7909
7910         if (pData == NULL) {
7911                 MSG_DEBUG("MmsGetMediaPartData : there is no data \n");
7912                 goto __RETURN;
7913         }
7914
7915         pNewData = MmsGetBinaryUTF8Data(pData, nRead, msgEncodingValue, msgTypeValue, msgCharsetValue, &nRead2);
7916         pPartType->encoding = MSG_ENCODING_BINARY;
7917
7918         if (MsgIsText(msgTypeValue))
7919                 pPartType->param.charset = MSG_CHARSET_UTF8;
7920
7921         if (MsgWriteFile(pNewData, sizeof(char), nRead2,  pFile) != (size_t)nRead2) {
7922                 MSG_DEBUG("MmsGetMediaPartData: file writing fail \n");
7923
7924                 goto __CATCH;
7925         }
7926
7927 __RETURN:
7928
7929         if (pNewData) {
7930                 free(pNewData);
7931                 pNewData = NULL;
7932         }
7933
7934         if (pTempData) {
7935                 free(pTempData);
7936                 pTempData = NULL;
7937         }
7938
7939         return true;
7940
7941 __CATCH:
7942
7943         if (pNewData) {
7944                 free(pNewData);
7945                 pNewData = NULL;
7946         }
7947
7948         if (pTempData) {
7949                 free(pTempData);
7950                 pTempData = NULL;
7951         }
7952
7953         return false;
7954 }
7955
7956 char *MmsGetBinaryUTF8Data(char *pData, int nRead, int msgEncodingValue, int msgTypeValue, int msgCharsetValue, int *npRead)
7957 {
7958         int nChar = 0;
7959         int nByte = 0;
7960         int nTemp = 0;
7961         char *pTemp = NULL;
7962         unsigned short *mszTempStr = NULL;
7963         char *pConvertedStr     = NULL;
7964         char *pConvertedData = NULL;
7965         char *pNewData = NULL;
7966         char *pReturnData = NULL;
7967
7968
7969         switch (msgEncodingValue) {
7970         case MSG_ENCODING_BASE64:
7971
7972                 pConvertedData = (char*)_MsgDecodeBase64((UCHAR*)pData, (ULONG)nRead, (ULONG*)&nByte);
7973                 MSG_DEBUG("MmsGetBinaryUTF8Data : MSG_ENCODING_BASE64     bodyLength = %d \n", nByte);
7974
7975                 pTemp = pConvertedData;
7976                 nTemp = nByte;
7977
7978                 break;
7979
7980         case MSG_ENCODING_QUOTE_PRINTABLE:
7981
7982                 pConvertedData = (char*)_MsgDecodeQuotePrintable((UCHAR*)pData, (ULONG)nRead, (ULONG*)&nByte);
7983                 MSG_DEBUG("MmsGetBinaryUTF8Data: MSG_ENCODING_QUOTE_PRINTABLE     bodyLength = %d \n", nByte);
7984
7985                 pTemp = pConvertedData;
7986                 nTemp = nByte;
7987
7988                 break;
7989
7990         default:
7991
7992                 MSG_DEBUG("MmsGetBinaryUTF8Data: 8bit    OR    Binary   bodyLength = %d \n", nRead);
7993
7994                 pTemp = pData;
7995                 nTemp = nRead;
7996
7997                 break;
7998         }
7999
8000         if (MsgIsText(msgTypeValue)) {
8001                 /* charset converting */
8002
8003                 switch (msgCharsetValue) {
8004                 case MSG_CHARSET_UTF16:
8005                 case MSG_CHARSET_USC2:
8006
8007                         MSG_DEBUG("MmsGetBinaryUTF8Data: MSG_CHARSET_USC2 \n");
8008
8009                         if (((UINT8)pTemp[0]) == 0xFF && ((UINT8)pTemp[1]) == 0xFE) {
8010                                 nChar = (nTemp / 2 - 1);
8011
8012                                 mszTempStr = (unsigned short*) malloc(nChar * sizeof(unsigned short));
8013                                 if (mszTempStr == NULL) {
8014                                         MSG_DEBUG("MmsGetBinaryUTF8Data : 1. Memory Full !!! \n");
8015                                         goto __CATCH;
8016                                 }
8017
8018                                 memcpy(mszTempStr, ((unsigned short*)pTemp + 1), nChar * sizeof(unsigned short));
8019
8020                                 nByte = MsgGetUnicode2UTFCodeSize(((unsigned short*)pTemp + 1), nChar);
8021                                 if (nByte < 3)
8022                                         nByte = 3; //min value
8023
8024                                 pConvertedStr = (char *)malloc(nByte + 1);
8025                                 if (pConvertedStr != NULL)
8026                                         MsgUnicode2UTF ((unsigned char*)pConvertedStr, nByte + 1, mszTempStr, nChar);
8027                         } else {
8028                                 nChar = (nTemp / 2);
8029
8030                                 mszTempStr = (unsigned short*) malloc(nChar * sizeof(unsigned short));
8031                                 if (mszTempStr == NULL) {
8032                                         MSG_DEBUG("MmsGetBinaryUTF8Data: 2. Memory Full !!! \n");
8033                                         goto __CATCH;
8034                                 }
8035
8036                                 memcpy(mszTempStr, ((unsigned short*)pTemp), nChar * sizeof(unsigned short));
8037
8038                                 nByte = MsgGetUnicode2UTFCodeSize(((unsigned short*)pTemp), nChar);
8039
8040                                 pConvertedStr = (char *)malloc(nByte + 1);
8041                                 if (pConvertedStr)
8042                                         MsgUnicode2UTF ((unsigned char*)pConvertedStr, nByte + 1, mszTempStr, nChar);
8043                         }
8044
8045                         if (pConvertedStr != NULL)
8046                                 pNewData = pConvertedStr;
8047
8048                         *npRead = nByte + 1;
8049
8050                         break;
8051
8052                 case MSG_CHARSET_US_ASCII:
8053
8054                         MSG_DEBUG("MmsGetBinaryUTF8Data: MSG_CHARSET_US_ASCII \n");
8055
8056                         /* fall through */
8057
8058                 case MSG_CHARSET_UTF8:
8059
8060                         MSG_DEBUG("MmsGetBinaryUTF8Data: MSG_CHARSET_UTF8 or Others \n");
8061
8062                         // skip BOM (Byte Order Mark) bytes .. (Please refer to the http://www.unicode.org/faq/utf_bom.html#BOM)
8063                         if (nTemp >= 3) {
8064                                 if (((UINT8)pTemp[0]) == 0xEF && ((UINT8)pTemp[1]) == 0xBB && ((UINT8)pTemp[2]) == 0xBF) {
8065                                         pTemp += 3;
8066                                         nTemp -= 3;
8067                                 }
8068                         }
8069                         pNewData = pTemp;
8070                         *npRead = nTemp;
8071
8072                         break;
8073
8074                 case MSG_CHARSET_ISO_8859_7:
8075
8076                         /* Greek */
8077
8078                         MSG_DEBUG("MmsGetBinaryUTF8Data: MSG_CHARSET_ISO_8859_7 \n");
8079
8080                         nByte = MsgGetLatin72UTFCodeSize((unsigned char*)pTemp, nTemp);
8081                         pConvertedStr = (char *)malloc(nByte + 1);
8082                         if (pConvertedStr)
8083                                 MsgLatin7code2UTF((unsigned char*)pConvertedStr, nByte + 1, (unsigned char*)pTemp, nTemp);
8084
8085                         pNewData = pConvertedStr;
8086                         *npRead = nByte + 1;
8087
8088                         break;
8089
8090                 case MSG_CHARSET_ISO_8859_9:
8091
8092                         /* Turkish */
8093                         MSG_DEBUG("MmsGetBinaryUTF8Data: MSG_CHARSET_ISO_8859_9 \n");
8094
8095                         nByte = MsgGetLatin52UTFCodeSize((unsigned char*)pTemp, nTemp);
8096                         pConvertedStr = (char *)malloc(nByte + 1);
8097                         if (pConvertedStr)
8098                                         MsgLatin5code2UTF((unsigned char*)pConvertedStr, nByte + 1, (unsigned char*)pTemp, nTemp);
8099
8100                         pNewData = pConvertedStr;
8101                         *npRead = nByte + 1;
8102
8103                         break;
8104
8105                 default:
8106
8107                         MSG_DEBUG("MmsGetBinaryUTF8Data: Other charsets \n");
8108
8109                         nByte = MsgGetLatin2UTFCodeSize((unsigned char*)pTemp, nTemp);
8110                         pConvertedStr = (char *)malloc(nByte + 1);
8111                         if (pConvertedStr)
8112                                 MsgLatin2UTF((unsigned char*)pConvertedStr, nByte + 1, (unsigned char*)pTemp, nTemp);
8113
8114                         pNewData = pConvertedStr;
8115                         *npRead = nByte + 1;
8116
8117                         break;
8118                 }
8119         } else {
8120                 pNewData = pTemp;
8121                 *npRead = nTemp;
8122         }
8123
8124         pReturnData = (char *)malloc(*npRead);
8125         if (pReturnData == NULL) {
8126                 MSG_DEBUG("MmsGetBinaryUTF8Data : pReturnData alloc fail. \n");
8127
8128                 goto __CATCH;
8129         }
8130
8131         if (pNewData != NULL) {
8132                 memset(pReturnData, 0, *npRead);
8133                 memcpy(pReturnData, pNewData, *npRead);
8134         }
8135
8136         if (pConvertedData) {
8137                 free(pConvertedData);
8138                 pConvertedData = NULL;
8139         }
8140
8141         if (pConvertedStr) {
8142                 free(pConvertedStr);
8143                 pConvertedStr = NULL;
8144         }
8145
8146         if (mszTempStr) {
8147                 free(mszTempStr);
8148                 mszTempStr = NULL;
8149         }
8150
8151         return pReturnData;
8152
8153 __CATCH:
8154
8155         if (pConvertedData) {
8156                 free(pConvertedData);
8157                 pConvertedData = NULL;
8158         }
8159
8160         if (pConvertedStr) {
8161                 free(pConvertedStr);
8162                 pConvertedStr = NULL;
8163         }
8164
8165         if (mszTempStr) {
8166                 free(mszTempStr);
8167                 mszTempStr = NULL;
8168         }
8169
8170         return NULL;
8171 }
8172
8173 #ifndef __SUPPORT_DRM__
8174 bool MsgMakeFileName(int iMsgType, char *szFileName, int nUntitleIndex)
8175 {
8176         char szText[MSG_FILENAME_LEN_MAX+1]={0,};
8177         char szTemp[MSG_FILENAME_LEN_MAX+1]={0,};
8178         char szTempFileName[MSG_FILENAME_LEN_MAX+1]={0,};
8179         char *pExt = NULL;
8180
8181
8182         MSG_DEBUG("MsgMakeFileName: iMsgType = %d     szFileName = %s \n", iMsgType, szFileName);
8183
8184         if (szFileName == NULL)
8185                 return false;
8186
8187         if (szFileName && (szFileName[0] != '\0')) {
8188                 MsgGetFileNameWithoutExtension (szTempFileName, szFileName);
8189
8190                 pExt = strrchr(szTempFileName, '.');
8191                 if (pExt == NULL) {
8192                         memset  (szText, 0, MSG_FILENAME_LEN_MAX+1);
8193                         strncpy(szText, szTempFileName, MSG_FILEPATH_LEN_MAX - 1);
8194                         strcat(szText, ".");                    // add '.'
8195                 } else {
8196                         memset  (szText, 0, MSG_FILENAME_LEN_MAX+1);
8197                         strncpy(szText, szTempFileName, pExt+1 - szFileName);   // add '.'
8198                 }
8199         } else {
8200                 if (nUntitleIndex >= 1) {
8201                         snprintf(szText, MSG_FILENAME_LEN_MAX+1, "%s_%d.", "Untitled", nUntitleIndex);
8202                 } else {
8203                         snprintf(szText, MSG_FILENAME_LEN_MAX+1, "%s.", "Untitled");
8204                 }
8205         }
8206
8207         if (iMsgType == MIME_APPLICATION_OCTET_STREAM) {
8208                 MSG_DEBUG("MsgMakeFileName: unsupported MsgType\n");
8209                 goto __CATCH;
8210         } else {
8211                 int             nLen = 0;
8212                 strncpy(szTemp, szText, MSG_FILENAME_LEN_MAX - 5);
8213                 if (iMsgType == MIME_UNKNOWN || (pExt = MimeGetExtFromMimeInt((MimeType)iMsgType)) == NULL) {
8214                         MSG_DEBUG("MsgMakeFileName: Failed to get extension of that mime data file. \n");
8215                         goto __CATCH;
8216                 }
8217                 nLen = MSG_FILENAME_LEN_MAX - strlen(szTemp);
8218                 strncat(szTemp, pExt, nLen);
8219         }
8220
8221
8222         strcpy(szFileName, szTemp);
8223
8224         MSG_DEBUG("MsgMakeFileName: made szFileName = %s \n", szFileName);
8225
8226         return true;
8227
8228 __CATCH:
8229         {
8230                 char *p = NULL;
8231                 p = strrchr(szText, '.');
8232                 if (p != NULL)
8233                         *p = 0;
8234                 snprintf(szFileName, MSG_FILENAME_LEN_MAX+1, "%s", szText);
8235
8236                 return false;
8237         }
8238 }
8239 #else
8240 bool MsgMakeFileName(int iMsgType, char *szFileName, MsgDrmType drmType, int nUntitleIndex)
8241 {
8242         char szText[MSG_FILENAME_LEN_MAX+1]={0,};
8243         char szTemp[MSG_FILENAME_LEN_MAX+1]={0,};
8244         char szTempFileName[MSG_FILENAME_LEN_MAX+1]={0,};
8245         char *pExt = NULL;
8246
8247         MSG_DEBUG("MsgMakeFileName: iMsgType = 0x%x, drmType = %d, szFileName = %s \n", iMsgType, drmType, szFileName);
8248
8249         if (szFileName == NULL)
8250                 return false;
8251
8252         if (szFileName && (szFileName[0] != '\0')) {
8253                 MsgGetFileNameWithoutExtension (szTempFileName, szFileName);
8254
8255                 if (drmType != MSG_DRM_TYPE_NONE) {
8256                         pExt = strrchr(szTempFileName, '.');
8257                         if (pExt == NULL) {
8258                                 memset(szText, 0, MSG_FILENAME_LEN_MAX+1);
8259                                 strncpy(szText, szTempFileName, MSG_FILENAME_LEN_MAX - 1);
8260                                 strcat(szText, ".");                    // add '.'
8261                         } else {
8262                                 memset(szText, 0, MSG_FILENAME_LEN_MAX+1);
8263                                 strncpy(szText, szTempFileName, pExt+1 - szFileName);
8264                         }
8265                 } else {
8266                         if (strrchr(szTempFileName, '.'))
8267                                 return true;
8268
8269                         memset(szText, 0, MSG_FILENAME_LEN_MAX+1);
8270                         strncpy(szText, szTempFileName, MSG_FILENAME_LEN_MAX - 1);
8271                 //temporary commented to save file as original name.
8272                         pExt = strrchr(szFileName, '.');
8273                 }
8274         } else {
8275                 if (nUntitleIndex >= 1) {
8276                         snprintf(szText, MSG_FILENAME_LEN_MAX+1, "%s_%d.", "untitled", nUntitleIndex);
8277                 } else {
8278                         snprintf(szText, MSG_FILENAME_LEN_MAX+1, "%s.", "untitled");
8279                 }
8280         }
8281
8282         if (drmType == MSG_DRM_TYPE_SD) {
8283                 strncpy(szTemp, szText, MSG_FILENAME_LEN_MAX - 5);
8284                 strcat(szTemp, "dcf");
8285         } else if (MsgDRMIsForwardLockType(drmType)) {
8286                 strncpy(szTemp, szText, MSG_FILENAME_LEN_MAX - 4);
8287                 strcat(szTemp, "dm");
8288         } else {
8289                 if (iMsgType == MIME_APPLICATION_OCTET_STREAM) {
8290                         MSG_DEBUG("MsgMakeFileName: unsupported MsgType\n");
8291                         goto __CATCH;
8292                 } else {
8293                         int             nLen = 0;
8294                         strncpy(szTemp, szText, MSG_FILENAME_LEN_MAX - 5);
8295                         //temporary commented to save file as original name.
8296                         if (pExt == NULL) {
8297
8298                                 if (iMsgType == MIME_UNKNOWN || (pExt = MimeGetExtFromMimeInt((MimeType)iMsgType)) == NULL) {
8299                                         MSG_DEBUG("MsgMakeFileName: Failed to get extension of that mime data file. \n");
8300                                         goto __CATCH;
8301                                 }
8302                         }
8303
8304                         nLen = MSG_FILENAME_LEN_MAX - strlen(szTemp);
8305                         strncat(szTemp, pExt, nLen);
8306                 }
8307         }
8308
8309         strcpy(szFileName, szTemp);
8310
8311         MSG_DEBUG("MsgMakeFileName: made szFileName = %s \n", szFileName);
8312
8313         return true;
8314
8315 __CATCH:
8316         {
8317                 char *p = NULL;
8318                 p = strrchr(szText, '.');
8319                 if (p != NULL)
8320                         *p = 0;
8321                 snprintf(szFileName, MSG_FILENAME_LEN_MAX+1, "%s", szText);
8322
8323                 return false;
8324         }
8325 }
8326 #endif
8327
8328 bool MsgGetFileNameWithoutExtension (char *szOutputName, char *szName)
8329 {
8330         char *pszExt = NULL;
8331
8332         if (szOutputName == NULL) {
8333                 MSG_DEBUG("szOutputName is NULL");
8334                 return false;
8335         }
8336
8337         strncpy(szOutputName, szName, strlen(szName));
8338
8339         if ((pszExt = strrchr(szOutputName, '.'))) {
8340                 if (pszExt[0] == '.')
8341                         pszExt[0] = '\0';
8342         }
8343
8344         return true;
8345 }
8346
8347 int MmsGetMediaPartCount(msg_message_id_t msgId)
8348 {
8349         MmsMsg *pMsg;
8350
8351         MmsPluginStorage::instance()->getMmsMessage(&pMsg);
8352
8353         if (msgId != pMsg->msgID) {
8354                 MSG_DEBUG("Invalid Message Id");
8355                 return -1;
8356         }
8357
8358         return pMsg->nPartCount;
8359 }
8360
8361 bool MmsGetMediaPartHeader(int index, MsgType *pHeader)
8362 {
8363         MmsMsg *pMsg = NULL;
8364         MsgMultipart *pPart = NULL;
8365
8366         if (pHeader == NULL) {
8367                 MSG_DEBUG("MmsGetMediaPartHeader: Invalid pHeader input. It's null \n");
8368                 return false;
8369         }
8370
8371         MmsPluginStorage::instance()->getMmsMessage(&pMsg);
8372
8373         _MsgInitMsgType(pHeader);
8374
8375
8376         /* Requires header of non-presentation */
8377         if (MsgIsMultipart(pMsg->msgType.type)) {
8378                 MSG_DEBUG("MmsGetMediaPartHeader: Multipart header [index = %d] \n", index);
8379
8380                 pPart = pMsg->msgBody.body.pMultipart;
8381
8382                 while (pPart && index--)
8383                         pPart = pPart->pNext;
8384
8385                 if (pPart == NULL) {
8386                         MSG_DEBUG("MmsGetMediaPartHeader: There is no such msg part.\n");
8387                         return false;
8388                 }
8389
8390                 memcpy(pHeader, &pPart->type, sizeof(MsgType));
8391         } else {
8392                 MSG_DEBUG("MmsGetMediaPartHeader: Requires singlepart header \n");
8393                 memcpy(pHeader, &pMsg->msgType, sizeof(MsgType));
8394         }
8395
8396         return true;
8397 }
8398
8399 bool MmsDebugPrintMulitpartEntry(MsgMultipart *pMultipart, int index)
8400 {
8401         MSG_DEBUG("------------------------------\n");
8402         MSG_DEBUG("%dth multipart info\n", index);
8403         MSG_DEBUG("header size=%d\n", pMultipart->type.size);
8404         MSG_DEBUG("body size=%d\n", pMultipart->type.contentSize);
8405         MSG_DEBUG("content type=%s\n", MmsDebugGetMimeType((MimeType)pMultipart->type.type));
8406         MSG_DEBUG("content ID=%s\n", pMultipart->type.szContentID);
8407         MSG_DEBUG("content location=%s\n", pMultipart->type.szContentLocation);
8408
8409         if (pMultipart->type.type == MIME_TEXT_PLAIN) {
8410                 MSG_DEBUG("text info\n");
8411                 MSG_DEBUG("charset=%d\n", pMultipart->type.param.charset);
8412                 MSG_DEBUG("text file name=%s\n", pMultipart->type.param.szName);
8413         }
8414 #ifdef __SUPPORT_DRM__
8415         if (pMultipart->type.drmInfo.drmType != MSG_DRM_TYPE_NONE) {
8416                 MSG_DEBUG("drm info\n");
8417                 MSG_DEBUG("drm type=%d      (0: NONE 1: Fowward Lock, 2:Combined Delivery, 3: Separated Delivery)\n", pMultipart->type.drmInfo.drmType);
8418                 MSG_DEBUG("drm content type=%s\n", MmsDebugGetMimeType((MimeType)pMultipart->type.drmInfo.contentType));
8419                 MSG_DEBUG("drm content URI=%s\n", pMultipart->type.drmInfo.szContentURI);
8420                 MSG_DEBUG("drm2FullPath=%s\n", pMultipart->type.drmInfo.szDrm2FullPath);
8421         }
8422 #endif
8423         MSG_DEBUG("------------------------------\n");
8424         return true;
8425 }
8426