update tizen source
[framework/messaging/msg-service.git] / plugin / mms_plugin / include / MmsPluginCodec.h
1 /*
2 *
3 * Copyright (c) 2000-2012 Samsung Electronics Co., Ltd. All Rights Reserved.
4 *
5 * This file is part of msg-service.
6 *
7 * Contact: Jaeyun Jeong <jyjeong@samsung.com>
8 *          Sangkoo Kim <sangkoo.kim@samsung.com>
9 *          Seunghwan Lee <sh.cat.lee@samsung.com>
10 *          SoonMin Jung <sm0415.jung@samsung.com>
11 *          Jae-Young Lee <jy4710.lee@samsung.com>
12 *          KeeBum Kim <keebum.kim@samsung.com>
13 *
14 * PROPRIETARY/CONFIDENTIAL
15 *
16 * This software is the confidential and proprietary information of
17 * SAMSUNG ELECTRONICS ("Confidential Information"). You shall not
18 * disclose such Confidential Information and shall use it only in
19 * accordance with the terms of the license agreement you entered
20 * into with SAMSUNG ELECTRONICS.
21 *
22 * SAMSUNG make no representations or warranties about the suitability
23 * of the software, either express or implied, including but not limited
24 * to the implied warranties of merchantability, fitness for a particular
25 * purpose, or non-infringement. SAMSUNG shall not be liable for any
26 * damages suffered by licensee as a result of using, modifying or
27 * distributing this software or its derivatives.
28 *
29 */
30
31 #ifndef MMS_PLUGIN_CODEC_H
32 #define MMS_PLUGIN_CODEC_H
33
34
35 /*==================================================================================================
36                                                         HEADER
37 ==================================================================================================*/
38
39 #include <glib.h>
40 #include <vconf.h>
41 #include "MsgMmsTypes.h"
42 #include "MmsPluginMessage.h"
43 #include "MsgTransportTypes.h"
44
45 /*==================================================================================================
46                                                         DEFINE
47 ==================================================================================================*/
48
49 #define MMS_MAJOR_VERSION                       1
50
51 #if defined(MMS_V1_1)
52 #define MMS_MINOR_VERSION                       1
53 #define MMS_VERSION                                     0x91
54
55 #elif defined(MMS_V1_2)
56 #define MMS_MINOR_VERSION                       2
57 #define MMS_VERSION                                     0x92
58
59 #else
60 #define MMS_MINOR_VERSION                       0
61 #define MMS_VERSION                                     0x90
62 #endif
63
64 #define MMS_SIGN_TEXT_LEN                       100
65 #define MMS_MAX_FIELD_VALUE_COUNT       74
66 #define MMS_MAX_FIELD_TYPE_COUNT        21
67 #define MSG_MMS_ENCODE_BUFFER_MAX       (2 * 1024)
68 #define MSG_MMS_DECODE_BUFFER_MAX       (2 * 1024)
69 #define MSB                                                             0x80
70 #define QUOTE                                                   0x7F
71 #define MARK                                                    0x22    // "
72 #define NO_VALUE                                                0x00
73 #define LENGTH_QUOTE                                    0x1F
74 #define MSG_STDSTR_LONG                         0xFF
75 #define INVALID_VALUE                                   -1
76 #define MSG_INVALID_VALUE                               -1
77
78 #define MMS_MAX_FIELD_VALUE_COUNT       74
79 #define MMS_MAX_FIELD_TYPE_COUNT        21
80
81 #define MSG_LOCAL_TEMP_BUF_SIZE 1024
82 #define MSG_INBOX_DIRECTORY                     "/User/Msg/Inbox"
83 /*==================================================================================================
84                                                         ENUMS
85 ==================================================================================================*/
86
87 typedef enum {
88         MMS_CODE_BCC,
89         MMS_CODE_CC,
90         MMS_CODE_CONTENTLOCATION,
91         MMS_CODE_CONTENTTYPE,
92         MMS_CODE_DATE,
93         MMS_CODE_DELIVERYREPORT,
94         MMS_CODE_DELIVERYTIME,
95         MMS_CODE_EXPIRYTIME,
96         MMS_CODE_FROM,
97         MMS_CODE_MSGCLASS,
98         MMS_CODE_MSGID,
99         MMS_CODE_MSGTYPE,
100         MMS_CODE_VERSION,
101         MMS_CODE_MSGSIZE,
102         MMS_CODE_PRIORITY,
103         MMS_CODE_READREPLY,
104         MMS_CODE_REPORTALLOWED,
105         MMS_CODE_RESPONSESTATUS,
106         MMS_CODE_RETRIEVESTATUS,                /* Add by MMSENC v1.1 */
107         MMS_CODE_RESPONSETEXT,
108         MMS_CODE_RETRIEVETEXT,                  /* Add by MMSENC v1.1 */
109         MMS_CODE_SENDERVISIBILLITY,
110         MMS_CODE_MSGSTATUS,
111         MMS_CODE_SUBJECT,
112         MMS_CODE_TO,
113         MMS_CODE_TRID,
114
115         /* Add by MMSENC v1.1 */
116         MMS_CODE_READSTATUS,
117         MMS_CODE_REPLYCHARGING,
118         MMS_CODE_REPLYCHARGINGDEADLINE,
119         MMS_CODE_REPLYCHARGINGID,
120         MMS_CODE_REPLYCHARGINGSIZE,
121         MMS_CODE_PREVIOUSLYSENTBY,
122         MMS_CODE_PREVIOUSLYSENTDATE,
123
124         MMS_CODE_TRANSFERENCODING,
125         MMS_CODE_DISPOSITION,
126         MMS_CODE_CONTENT_ID
127 } MmsFieldCode;
128
129 enum {
130         MMS_BODYHDR_TRANSFERENCODING,
131         MMS_BODYHDR_DISPOSITION,
132         MMS_BODYHDR_CONTENTID,
133         MMS_BODYHDR_CONTENTLOCATION,
134         MMS_BODYHDR_X_OMA_DRM_SEPARATE_DELIVERY,        // DRM RO WAITING
135 };
136
137 typedef enum {
138         //code
139         MmsCodeFieldCode,
140         MmsCodeParameterCode,
141         MmsCodeMsgBodyHeaderCode,
142
143         //Data
144         MmsCodeMsgType,
145         MmsCodeDeliveryReport,
146         MmsCodeTimeType,
147         MmsCodeMsgClass,
148         MmsCodePriority,
149         MmsCodeResponseStatus,
150         MmsCodeRetrieveStatus,
151         MmsCodeReadReply,
152         MmsCodeReportAllowed,
153         MmsCodeSenderVisibility,
154         MmsCodeMsgStatus,
155         MmsCodeReadStatus,
156         MmsCodeAddressType,
157         MmsCodeCharSet,
158         MmsCodeReplyCharging,
159
160         MmsCodeContentType,
161
162         MmsCodeMsgDisposition,
163         MmsCodeContentTransferEncoding
164 } MmsCode;
165
166 typedef enum {
167         MSG_PRESENTATION_NONE = -1,
168         MSG_PRESENTATION_FIRSTPART,             //Content-type == type parameter
169         MSG_PRESENTATION_TYPE_BASE,             //Content-type == type parameter
170         MSG_PRESENTATION_LOCATION,              //Content-Location == start parameter
171         MSG_PRESENTATION_ID,                    //Content-ID == start parameter
172 } MsgPresentationFactor;
173
174 /*==================================================================================================
175                                                         Structures
176 ==================================================================================================*/
177
178 typedef struct {
179         const char *szText;
180         UINT16 binary;
181 } MmsField;
182
183
184 typedef struct {
185         char *pData;
186         UINT32 length;
187 } EncodedPData;
188
189 typedef struct {
190         MsgPresentationFactor factor;
191         MsgMultipart *pPrevPart;
192         MsgMultipart *pCurPresentation;
193 } MsgPresentaionInfo;
194
195 // for Decoding & Encoding
196 typedef struct {
197         bool bActive;
198         char *pszOwner;
199         int msgID;              // if noti.ind, msgID is -1.
200
201         MmsMsgType type;
202         char szTrID[MMS_TR_ID_LEN+1];
203         UINT8 version;
204         UINT32 date;
205
206         MsgHeaderAddress *pFrom;                //"/TYPE=PLMN", /"TYPE=IPv4", "/TYPE=IPv6"
207         MsgHeaderAddress *pTo;
208         MsgHeaderAddress *pCc;
209         MsgHeaderAddress *pBcc;
210         char szSubject[MSG_LOCALE_SUBJ_LEN + 1];
211         MmsResponseStatus responseStatus;
212         MmsRetrieveStatus retrieveStatus;
213         char szResponseText[MMS_LOCALE_RESP_TEXT_LEN + 1];
214         char szRetrieveText[MMS_LOCALE_RESP_TEXT_LEN + 1];
215
216         MmsMsgClass msgClass;
217         MmsTimeStruct expiryTime;
218         MmsTimeStruct deliveryTime;
219         MmsPriority priority;
220         MmsSenderVisible hideAddress;
221         MmsReport deliveryReport;
222         MmsReport readReply;
223         MmsReportAllowed reportAllowed;
224         char szContentLocation[MMS_LOCATION_LEN + 1];
225
226         MSG_DELIVERY_REPORT_STATUS_T msgStatus;
227         MSG_READ_REPORT_STATUS_T readStatus;
228
229         MmsReplyCharge replyCharge;
230
231         // only used at Decoding module
232         char szMsgID[MMS_MSG_ID_LEN + 1];
233         UINT32 msgSize;
234
235 #ifdef __SUPPORT_DRM__
236         MsgDrmType drmType;
237 #endif
238
239         // dependent to Client implementation
240         MsgType msgType;
241         MsgBody msgBody;
242 } MmsHeader;
243
244 extern const MmsField gMmsField[MMS_MAX_FIELD_TYPE_COUNT][MMS_MAX_FIELD_VALUE_COUNT];
245 extern char gszMmsLoadBuf1[MSG_MMS_DECODE_BUFFER_MAX + 1];
246 extern char gszMmsLoadBuf2[MSG_MMS_DECODE_BUFFER_MAX + 1];
247
248 extern MmsHeader mmsHeader;
249
250 /* Common */
251 char *_MmsGetTextValue(MmsCode i, int j);
252 char *_MmsGetTextValuebyField(int field, int value);
253 int _MmsGetTextType(MmsCode i, char *pValue);
254 UINT16 _MmsGetBinaryValue(MmsCode i, int j);
255 int _MmsGetBinaryType(MmsCode i, UINT16 value);
256 UINT8 _MmsGetVersion(MmsMsg *pMsg);
257 bool _MmsSetVersion(int majorVer, int minorVer);
258
259
260 /* Decoding */
261 void _MmsInitHeader();
262 void _MmsCleanDecodeBuff(void);
263 bool _MmsDecodeInitialize(void);
264 void _MmsRegisterDecodeBuffer(char *pInBuff1, char *pInBuffer2, int maxLen);
265 void _MmsUnregisterDecodeBuffer(void);
266 int _MmsGetDecodeOffset(void);
267
268 bool MmsBinaryDecodeMsgBody(FILE *pFile, char *szFilePath, int totalLength);
269 bool MmsBinaryDecodeContentType(FILE *pFile, char *szFilePath, int totalLength); // for JAVA MMS AppId
270
271 bool MmsBinaryDecodeEachPart(FILE *pFile, char *szFilePath, MsgType *pMsgType, MsgBody *pMsgBody, int totalLength);
272 int MmsBinaryDecodeContentType(FILE *pFile, MsgType *pMsgType, int totalLength);
273 bool MmsBinaryDecodePartHeader(FILE *pFile, MsgType *pMsgType, int headerLen, int totalLength);
274 bool MmsBinaryDecodePartBody(FILE *pFile, UINT32 bodyLength, int totalLength);
275 bool MmsBinaryDecodeEntries(FILE *pFile, UINT32 *npEntries, int totalLength);
276 bool MmsBinaryDecodeMsgHeader(FILE *pFile, int totalLength);
277
278 bool MmsBinaryDecodeMultipart(FILE *pFile, char *szFilePath, MsgType *pMsgType, MsgBody *pMsgBody, int totalLength);
279
280
281 /* Encoding */
282 void _MmsRegisterEncodeBuffer(char *pInBuff, int maxLen);
283 void _MmsUnregisterEncodeBuffer(void);
284 int _MmsGetEncodeOffset(void);
285
286 bool _MmsEncodeMsg(void);
287 bool _MmsEncodeSendReq(FILE *pFile, MmsMsg *pMsg);
288 bool _MmsEncodeTemplate(FILE *pFile, MmsMsg *pMsg);     // just encode MMS Body without any header.
289 bool _MmsEncodeAckInd(FILE *pFile, char *szTrID, bool bReportAllowed);
290 bool _MmsEncodeNotiRespInd(FILE *pFile, char *szTrID, MSG_DELIVERY_REPORT_STATUS_T iStatus, bool bReportAllowed);
291 bool _MmsEncodeForwardReq(FILE *pFile, char *szContentLocation, char *szForwardTo, char *szForwardCc, char *szForwardBcc);
292 bool _MmsEncodeReadReport10(FILE *pFile, MmsMsg *pMsg, MSG_READ_REPORT_STATUS_T mmsReadStatus);
293 bool _MmsEncodeReadReport11(FILE *pFile, MmsMsg *pMsg, MSG_READ_REPORT_STATUS_T mmsReadStatus);
294
295
296 bool MmsBinaryEncodeMsgBody(FILE *pFile, MsgType *pType, MsgBody *pBody, int nPartCount, bool bTemplate);//NEW_TEMPLATE
297
298 int MmsBinaryEncodeContentTypeLen(MsgType *pType);
299 bool MmsBinaryEncodeContentType(FILE *pFile, MsgType *pType, int typeLength);
300 int MmsBinaryEncodeContentHeaderLen(MsgContentType contentType, MsgType *pType, bool bMultipart);
301 bool MmsBinaryEncodeContentHeader(FILE *pFile, MsgContentType contentType, MsgType *pType, bool bMultipart);
302 bool MmsBinaryEncodeContentBody(FILE *pFile, MsgBody *pBody);
303 bool MmsBinaryEncodeMsgPart(FILE *pFile, int contentType, MsgType *pType, MsgBody *pBody);
304 bool MmsBinaryEncodeSendReqHdrwithinBufRegi(FILE *pFile, MmsMsg *pMsg);
305 bool MsgWriteDataFromEncodeBuffer(FILE *pFile, char *pInBuffer, int *pPtr, int maxLen, int *pOffset);
306 bool MsgLoadDataToDecodeBuffer(FILE *pFile, char **ppBuf, int *pPtr, int *pOffset, char *pInBuf1, char *pInBuf2, int maxLen, int *npRead, int endOfFile);
307 bool MsgFreeHeaderAddress(MsgHeaderAddress *pAddr);
308 bool MsgCheckFileNameHasInvalidChar(char *szName);
309 bool _MsgReplaceInvalidFileNameChar(char *szInText, char replaceChar);
310 char *_MsgGetStringUntilDelimiter(char *pszString, char delimiter);
311 char *MsgChangeHexString(char *pOrg);
312 bool _MsgParseParameter(MsgType *pType, char *pSrc);
313 char *_MsgSkipWS(char *s);
314 char *__MsgSkipComment(char *s, long trim);
315 int _MsgGetCode(MsgHeaderField tableId, char *pStr);
316 char *MsgConvertLatin2UTF8FileName(char *pSrc);
317 bool _MsgChangeSpace(char *pOrg, char **ppNew);
318 void _MsgRemoveFilePath(char *pSrc);
319 bool MsgIsUTF8String(unsigned char *szSrc, int nChar);
320 bool MsgIsPercentSign(char *pSrc);
321 MsgMultipart *MsgAllocMultipart(void);
322 bool _MsgInitMsgType(MsgType *pMsgType);
323 bool _MsgInitMsgBody(MsgBody *pMsgBody);
324 MsgPresentationFactor MsgIsPresentationEx(MsgType *multipartType, char *szStart, MsgContentType typeParam);
325 void MsgConfirmPresentationPart(MsgType *pMsgType, MsgBody *pMsgBody, MsgPresentaionInfo *pPresentationInfo);
326 bool MsgIsMultipartRelated(int type);
327 bool MsgIsPresentablePart(int type);
328 bool MsgIsText(int type);
329 bool _MsgFreeBody(MsgBody *pBody, int type);
330 bool MsgFreeAttrib(MmsAttrib *pAttrib);
331 bool MsgResolveNestedMultipart(MsgType *pPartType, MsgBody *pPartBody);
332 char *MsgResolveContentURI(char *szSrc);
333 bool _MsgParsePartHeader(MsgType *pType, const char *pRawData, int nRawData);
334 char *MsgRemoveQuoteFromFilename(char *pSrc);
335 MmsMsg *MmsGetMsgById(MmsMsgID msgID, bool msgLock);
336 bool MsgIsMultipart(int type);
337 bool MsgIsHexChar(char *pSrc);
338 char _MsgConvertHexValue(char *pSrc);
339 int __MsgConvertCharToInt(char ch);
340 bool __MsgInitMsgContentParam(MsgContentParam *pMsgContentParam);
341 bool MsgCopyNestedMsgType(MsgType *pMsgType1, MsgType *pMsgType2);
342 bool MsgCopyNestedMsgParam(MsgContentParam *pParam1, MsgContentParam *pParam2);
343 bool MsgIsMultipartMixed(int type);
344 bool MsgStrcpyWithoutCRLF(char *pOrg, char **ppNew);
345 bool __MsgIsInvalidFileNameChar(char ch);
346 bool MmsAddrUtilCompareAddr(char *pszAddr1, char *pszAddr2);
347 bool _MmsDataUpdateLastStatus(MmsMsg *pMsg);
348
349 bool _MsgEncodeBase64(void *pSrc, unsigned long srcLen, unsigned long *len, unsigned char *ret);
350 void *_MsgDecodeBase64(unsigned char *pSrc, unsigned long srcLen, unsigned long *len);
351 bool _MsgEncodeQuotePrintable(unsigned char *pSrc, unsigned long srcLen, unsigned long *len, unsigned char *ret);
352 unsigned char *_MsgDecodeQuotePrintable(unsigned char *pSrc, unsigned long srcLen, unsigned long *len);
353 bool _MsgEncode2Base64(void *pSrc, unsigned long srcLen, unsigned long *len, unsigned char *ret);
354 char *_MsgDecodeText(char *pOri);
355 char *MsgEncodeText(char *pOri);
356 int MsgGetLatin2UTFCodeSize(unsigned char *szSrc, int nChar);
357 int MsgLatin5code2UTF(unsigned char *des, int outBufSize, unsigned char *szSrc, int nChar);
358 int MsgGetLatin52UTFCodeSize(unsigned char *szSrc, int nChar);
359 int MsgLatin2UTF(unsigned char *des, int outBufSize, unsigned char *szSrc, int nChar);
360 int MsgLatin7code2UTF(unsigned char *des, int outBufSize, unsigned char *szSrc, int nChar);
361 int MsgGetLatin72UTFCodeSize(unsigned char *szSrc, int nChar);
362 int MsgUnicode2UTF(unsigned char *des, int outBufSize, unsigned short *szSrc, int nChar);
363 int MsgGetUnicode2UTFCodeSize(unsigned short *szSrc, int nChar);
364 bool MmsAddrUtilCheckEmailAddress(char *pszAddr);
365 bool MmsAddrUtilRemovePlmnString(char *pszAddr);
366 int MsgCutUTFString(unsigned char *des, int outBufSize, unsigned char *szSrc, int nChar);
367 void MsgMIMERemoveQuote(char *szSrc);
368 bool _MmsMultipartSaveAsTempFile(MsgType *pPartType, MsgBody *pPartBody, char *pszMailboxPath, char *pszMsgFilename, int index, bool bSave);
369 bool MsgGetTypeByFileName(int *type, char *szFileName);
370 bool MmsGetMediaPartData(MsgType *pPartType, MsgBody *pPartBody, FILE *pFile);
371 char *MmsGetBinaryUTF8Data(char *pData, int nRead, int msgEncodingValue, int msgTypeValue, int msgCharsetValue, int *npRead);
372 #ifndef __SUPPORT_DRM__
373 bool MsgMakeFileName(int iMsgType, char *szFileName, int nUntitleIndex);
374 #else
375 bool MsgMakeFileName(int iMsgType, char *szFileName, MsgDrmType drmType, int nUntitleIndex);
376 #endif
377 bool MsgGetFileNameWithoutExtension(char *szOutputName, char *szName);
378 int MmsGetMediaPartCount(MSG_MESSAGE_ID_T msgId);
379 bool MmsGetMediaPartHeader(int index, MsgType *pHeader);
380 bool MmsGetMsgAttrib(MmsMsgID msgID, MmsAttrib *pAttrib);
381 bool _MmsReadMsgBody(MSG_MESSAGE_ID_T msgID, bool bSavePartsAsTempFiles, bool bRetrieved, char *retrievedPath);
382
383 #ifdef __SUPPORT_DRM__
384 void _MsgFreeDRMInfo(MsgDRMInfo *pDrmInfo);
385 bool MsgCopyDrmInfo(MsgType *pPartType);
386 bool MmsDrm2ConvertMsgBody(char *szOriginFilePath);
387 bool MmsDrm2ReadMsgConvertedBody(MSG_MESSAGE_INFO_S *pMsg, bool bSavePartsAsTempFiles, bool bRetrieved, char *retrievedPath);
388 bool MmsDrm2DecodeConvertedMsg(int msgID, char *pszFullPath);
389 #endif
390
391 bool MmsDebugPrintMulitpartEntry(MsgMultipart *pMultipart, int index);
392
393 #endif