2 * Copyright 2012-2013 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.1 (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
8 * http://floralicense.org/license/
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.
20 #include "MsgUtilFile.h"
21 #include "MmsPluginDebug.h"
22 #include "MmsPluginEncode.h"
23 #include "MmsPluginCodecTypes.h"
24 #include "MmsPluginCodecCommon.h"
25 #include "MmsPluginMIME.h"
26 #include "MmsPluginUtil.h"
29 /** Sending message related variables ------------------------ */
30 static char gszMmsEncodeBuf[MSG_MMS_ENCODE_BUFFER_MAX] = {0, };
31 static int gCurMmsEncodeBuffPos = 0; /* number of characters on gpMmsEncodeBuf */
32 static int gMmsEncodeMaxLen = 0;
33 static int gMmsEncodeCurOffset = 0; /* offset in file */
34 static char *gpMmsEncodeBuf = NULL;
36 /* Acknowledge.ind & NotifyResp.ind related variables ------------------------ */
37 static char gszMmsEncodeBuf2[MSG_MMS_ENCODE_BUFFER_MAX] = {0, };
38 static int gCurMmsEncodeBuffPos2 = 0; /* number of characters on gpMmsEncodeBuf */
39 static int gMmsEncodeMaxLen2 = 0;
40 static int gMmsEncodeCurOffset2 = 0; /* offset in file */
41 static char *gpMmsEncodeBuf2 = NULL;
43 static int __MmsGetEncodeOffset(void);
44 static void __MmsRegisterEncodeBuffer2(char *pInBuff, int maxLen);
45 static void __MmsUnregisterEncodeBuffer2(void);
47 static int __MmsBinaryEncodeUintvarLen(UINT32 integer);
48 static bool __MmsBinaryEncodeUintvar(FILE *pFile, UINT32 integer, int length);
49 static int __MmsBinaryEncodeValueLengthLen(UINT32 integer);
50 static bool __MmsBinaryEncodeValueLength(FILE *pFile, UINT32 integer, int length);
51 static int __MmsBinaryEncodeIntegerLen(UINT32 integer);
52 static bool __MmsBinaryEncodeInteger(FILE *pFile, UINT32 integer, int length);
53 static int __MmsBinaryEncodeLongIntegerLen(UINT32 integer);
54 static bool __MmsBinaryEncodeLongInteger(FILE *pFile, UINT32 integer, int length);
55 static int __MmsBinaryEncodeTextStringLen(UINT8 *source);
56 static bool __MmsBinaryEncodeTextString(FILE *pFile, UINT8 *source, int length);
57 static int __MmsBinaryEncodeQuotedStringLen(UINT8 *pSrc);
58 static bool __MmsBinaryEncodeQuotedString(FILE *pFile, UINT8 *source, int length);
59 static int __MmsBinaryEncodeEncodedStringLen(UINT8 *source);
60 static bool __MmsBinaryEncodeEncodedString(FILE *pFile, UINT8 *source, int length);
62 static int __MmsBinaryEncodeContentTypeLen(MsgType *pType);
63 static bool __MmsBinaryEncodeContentType(FILE *pFile, MsgType *pType, int typeLength);
64 static int __MmsBinaryEncodeContentHeaderLen(MimeType contentType, MsgType *pType, bool bMultipart);
65 static bool __MmsBinaryEncodeContentHeader(FILE *pFile, MimeType contentType, MsgType *pType, bool bMultipart);
67 static bool __MmsBinaryEncodeContentBody(FILE *pFile, MsgBody *pBody);
68 static bool __MmsBinaryEncodeMsgPart(FILE *pFile, int contentType, MsgType *pType, MsgBody *pBody);
70 static bool __MmsBinaryEncodeMmsVersion(FILE *pFile);
71 static bool __MmsBinaryEncodeTrID(FILE *pFile, char *szTrID, int bufLen);
72 static bool __MmsBinaryEncodeMsgID(FILE *pFile, const char *szMsgID); /** 2005-05-24, added for read-reply PDU 1.2 */
73 static bool __MmsBinaryEncodeFrom(FILE *pFile);
74 static bool __MmsBinaryEncodeTime(FILE *pFile, MmsFieldCode fieldCode, MmsTimeStruct time);
75 static bool __MmsBinaryEncodeDate(FILE *pFile);
76 static bool __MmsBinaryEncodeOneAddress(FILE *pFile, MmsFieldCode addrType, char *szAddrStr);
77 static bool __MmsBinaryEncodeAddress(FILE *pFile, MmsFieldCode addrType, char *szAddr);
79 static bool __MmsBinaryEncodeFieldCodeAndValue(FILE *pFile, UINT8 fieldCode, UINT8 fieldValue);
80 static bool __MmsBinaryEncodeSendReqHdr(FILE *pFile, MmsMsg *pMsg);
81 static bool __MmsBinaryEncodeAckIndHdr(FILE *pFile, char *pTrID, bool bReportAllowed);
82 static bool __MmsBinaryEncodeNotiRespIndHdr(FILE* pFile, char *pTrID, msg_delivery_report_status_t iStatus, bool bReportAllowed);
83 static bool __MmsBinaryEncodeReadReport10Hdr(FILE *pFile, MmsMsg *pMsg, msg_read_report_status_t mmsReadStatus);
84 static bool __MmsBinaryEncodeReadReport11Hdr(FILE *pFile, MmsMsg *pMsg, msg_read_report_status_t mmsReadStatus);
86 static bool __MmsBinaryEncodeMsgBody(FILE *pFile, MsgType *pType, MsgBody *pBody, int nPartCount, bool bTemplate);//NEW_TEMPLATE
87 static bool __MmsEncodeSendReq(FILE *pFile, MmsMsg *pMsg, bool bIncludeSendReqHeader);
89 /* Functions for Acknowledge.ind & NotifyResp.ind ------------------------ */
90 static bool __MmsBinaryEncodeTextString2(FILE *pFile, UINT8 *source, int length);
91 static bool __MmsBinaryEncodeMmsVersion2(FILE *pFile);
92 static bool __MmsBinaryEncodeFieldCodeAndValue2(FILE *pFile, UINT8 fieldCode, UINT8 fieldValue);
94 /** -----------------------------------------------------------------
96 * * -----------------------------------------------------------------*/
98 static void __MmsCleanEncodeBuff(void)
100 memset(gpMmsEncodeBuf, 0, MSG_MMS_ENCODE_BUFFER_MAX);
101 gCurMmsEncodeBuffPos = 0;
105 void MmsRegisterEncodeBuffer(char *pInBuff, int maxLen)
107 gpMmsEncodeBuf = pInBuff;
108 gCurMmsEncodeBuffPos = 0;
109 gMmsEncodeMaxLen = maxLen;
110 gMmsEncodeCurOffset = 0;
113 void MmsUnregisterEncodeBuffer(void)
115 gpMmsEncodeBuf = NULL;
116 gCurMmsEncodeBuffPos = 0;
117 gMmsEncodeMaxLen = 0;
118 gMmsEncodeCurOffset = 0;
121 static int __MmsGetEncodeOffset(void)
123 return (gMmsEncodeCurOffset + gCurMmsEncodeBuffPos);
126 bool MmsEncodeSendReq(FILE *pFile, MmsMsg *pMsg)
128 return __MmsEncodeSendReq(pFile, pMsg, true);
131 bool MmsEncodeTemplate(FILE *pFile, MmsMsg *pMsg)
133 return __MmsEncodeSendReq(pFile, pMsg, false);
136 static bool __MmsEncodeSendReq(FILE* pFile, MmsMsg* pMsg, bool bIncludeSendReqHeader)
138 MmsRegisterEncodeBuffer(gszMmsEncodeBuf, MSG_MMS_ENCODE_BUFFER_MAX);
140 if (bIncludeSendReqHeader) {
141 if (__MmsBinaryEncodeSendReqHdr(pFile, pMsg) == false) {
142 MmsUnregisterEncodeBuffer();
147 if (__MmsBinaryEncodeMsgBody(pFile, &pMsg->msgType, &pMsg->msgBody, pMsg->nPartCount, !bIncludeSendReqHeader) == false) {
148 MmsUnregisterEncodeBuffer();
152 MmsUnregisterEncodeBuffer();
157 /* Functions for Acknowledge.ind & NotifyResp.ind ------------------------ */
159 static void __MmsCleanEncodeBuff2(void)
161 memset(gpMmsEncodeBuf2, 0, MSG_MMS_ENCODE_BUFFER_MAX);
162 gCurMmsEncodeBuffPos2 = 0;
165 static void __MmsRegisterEncodeBuffer2(char *pInBuff, int maxLen)
167 gpMmsEncodeBuf2 = pInBuff;
168 gCurMmsEncodeBuffPos2 = 0;
169 gMmsEncodeMaxLen2 = maxLen;
170 gMmsEncodeCurOffset2 = 0;
173 static void __MmsUnregisterEncodeBuffer2(void)
175 gpMmsEncodeBuf2 = NULL;
176 gCurMmsEncodeBuffPos2 = 0;
177 gMmsEncodeMaxLen2 = 0;
178 gMmsEncodeCurOffset2 = 0;
182 * @param source [in] originam string
183 * @param length [in] gotten from MmsBinaryEncodeTextStringLen()
184 * @param dest [in] buffer to store quted string
185 * @return changed string length
187 static bool __MmsBinaryEncodeTextString2(FILE *pFile, UINT8 *source, int length)
189 MSG_DEBUG("MmsBinaryEncodeTextString2: \n");
193 * Text-string = [Quote] *TEXT End-of-string
194 * If the 1st char in the TEXT is in the range of 128-255, a Quote char must precede it.
195 * Otherwise the Quote char must be omitted.
196 * The Quote is not part of the contents.
197 * Quote = <Octet 127>
200 if (pFile == NULL || source == NULL) {
201 MSG_DEBUG("MmsBinaryEncodeTextString2: source == NULL \n");
205 if ((gMmsEncodeMaxLen2 - gCurMmsEncodeBuffPos2) < length) {
206 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf2, &gCurMmsEncodeBuffPos2,
207 gMmsEncodeMaxLen2, &gMmsEncodeCurOffset2) == false) {
208 MSG_DEBUG("MmsBinaryEncodeTextString2: MsgWriteDataFromEncodeBuffer fail \n");
213 if (source[0] > 0x7F) {
214 gpMmsEncodeBuf2[gCurMmsEncodeBuffPos2++] = QUOTE;
218 strncpy(gpMmsEncodeBuf2 + gCurMmsEncodeBuffPos2, (char*)source, (length - 1)); /** except NULL */
219 gCurMmsEncodeBuffPos2 += (length - 1); /** except NULL */
220 gpMmsEncodeBuf2[gCurMmsEncodeBuffPos2++] = (UINT8)NULL;
228 static bool __MmsBinaryEncodeFieldCodeAndValue2(FILE *pFile, UINT8 fieldCode, UINT8 fieldValue)
230 if ((gMmsEncodeMaxLen2 - gCurMmsEncodeBuffPos2) < 2) {
231 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf2, &gCurMmsEncodeBuffPos2,
232 gMmsEncodeMaxLen2, &gMmsEncodeCurOffset2) == false) {
233 MSG_DEBUG("__MmsBinaryEncodeFieldCodeAndValue: MsgWriteDataFromEncodeBuffer fail \n");
238 if (fieldCode == 0xff) {
239 MSG_DEBUG("__MmsBinaryEncodeFieldCodeAndValue: invalid fieldCode \n");
243 if (fieldValue == 0xff) {
244 MSG_DEBUG("__MmsBinaryEncodeFieldCodeAndValue: invalid fieldValue \n");
248 gpMmsEncodeBuf2[gCurMmsEncodeBuffPos2++] = fieldCode;
249 gpMmsEncodeBuf2[gCurMmsEncodeBuffPos2++] = fieldValue;
257 static bool __MmsBinaryEncodeMmsVersion2(FILE *pFile)
259 UINT8 majorVer = MMS_MAJOR_VERSION;
260 UINT8 minorVer = MMS_MINOR_VERSION;
262 MSG_DEBUG("__MmsBinaryEncodeMmsVersion2: \n");
265 MSG_DEBUG("__MmsBinaryEncodeMmsVersion2: invalid input file \n");
269 if ((gMmsEncodeMaxLen2 - gCurMmsEncodeBuffPos2) < 2) {
270 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf2, &gCurMmsEncodeBuffPos2,
271 gMmsEncodeMaxLen2, &gMmsEncodeCurOffset2) == false) {
272 MSG_DEBUG("__MmsBinaryEncodeMmsVersion2: MsgWriteDataFromEncodeBuffer fail \n");
277 gpMmsEncodeBuf2[gCurMmsEncodeBuffPos2++] = MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_VERSION) | 0x80;
278 gpMmsEncodeBuf2[gCurMmsEncodeBuffPos2] = (majorVer << 4) | (minorVer & 0x0f) | MSB;
280 if (gpMmsEncodeBuf2[gCurMmsEncodeBuffPos2] < 0x80) {
281 gpMmsEncodeBuf2[gCurMmsEncodeBuffPos2++] |= 0x80;
283 gCurMmsEncodeBuffPos2++;
292 static bool __MmsBinaryEncodeAckIndHdr(FILE *pFile, char *szTrID, bool bReportAllowed)
295 UINT8 fieldCode = 0xff;
296 UINT8 fieldValue = 0xff;
298 MSG_DEBUG("_MmsBinaryEncodeAckIndHdr: szTrID = %s\n", szTrID);
299 MSG_DEBUG("_MmsBinaryEncodeAckIndHdr: bReportAllowed = %d\n", bReportAllowed);
301 __MmsCleanEncodeBuff2();
304 fieldCode = MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_MSGTYPE) | 0x80;
305 fieldValue = MmsGetBinaryValue(MmsCodeMsgType, MMS_MSGTYPE_ACKNOWLEDGE_IND) | 0x80;
306 if (__MmsBinaryEncodeFieldCodeAndValue2(pFile, fieldCode, fieldValue) == false) {
307 MSG_DEBUG("_MmsBinaryEncodeAckIndHdr: msgType error\n");
311 /* trID (other type of message) */
312 length = __MmsBinaryEncodeTextStringLen((UINT8*)szTrID);
314 MSG_DEBUG("_MmsBinaryEncodeAckIndHdr: MmsBinaryEncodeTextStringLen fail \n");
318 if ((gMmsEncodeMaxLen2 - gCurMmsEncodeBuffPos2) < (length + 1)) {
319 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf2, &gCurMmsEncodeBuffPos2,
320 gMmsEncodeMaxLen2, &gMmsEncodeCurOffset2) == false) {
321 MSG_DEBUG("_MmsBinaryEncodeAckIndHdr: MsgWriteDataFromEncodeBuffer fail \n");
326 gpMmsEncodeBuf2[gCurMmsEncodeBuffPos2++] = MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_TRID) | 0x80;
328 if (__MmsBinaryEncodeTextString2(pFile, (UINT8*)szTrID, length) == false) {
329 MSG_DEBUG("_MmsBinaryEncodeAckIndHdr: MmsBinaryEncodeTextString fail\n");
334 if (__MmsBinaryEncodeMmsVersion2(pFile) == false) {
335 MSG_DEBUG("_MmsBinaryEncodeAckIndHdr: __MmsBinaryEncodeMmsVersion error\n");
341 fieldCode = MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_REPORTALLOWED) | 0x80;
343 if (bReportAllowed) {
344 fieldValue = MmsGetBinaryValue(MmsCodeReportAllowed, MMS_REPORTALLOWED_YES) | 0x80;
346 fieldValue = MmsGetBinaryValue(MmsCodeReportAllowed, MMS_REPORTALLOWED_NO) | 0x80;
349 if (__MmsBinaryEncodeFieldCodeAndValue2(pFile, fieldCode, fieldValue) == false) {
350 MSG_DEBUG("_MmsBinaryEncodeAckIndHdr: Report Allowed error\n");
354 /* flush remained data on encoding file */
355 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf2, &gCurMmsEncodeBuffPos2,
356 gMmsEncodeMaxLen2, &gMmsEncodeCurOffset2) == false) {
357 MSG_DEBUG("_MmsBinaryEncodeAckIndHdr: remained data MsgWriteDataFromEncodeBuffer fail \n");
368 static bool __MmsBinaryEncodeNotiRespIndHdr(FILE *pFile, char *szTrID, msg_delivery_report_status_t iStatus, bool bReportAllowed)
371 UINT8 fieldCode = 0xff;
372 UINT8 fieldValue = 0xff;
374 MSG_DEBUG("_MmsBinaryEncodeNotiRespIndHdr: szTrID = %s\n", szTrID);
375 MSG_DEBUG("_MmsBinaryEncodeNotiRespIndHdr: iStatus = %d\n", iStatus);
376 MSG_DEBUG("_MmsBinaryEncodeNotiRespIndHdr: bReportAllowed = %d\n", bReportAllowed);
378 __MmsCleanEncodeBuff2();
381 fieldCode = MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_MSGTYPE) | 0x80;
382 fieldValue = MmsGetBinaryValue(MmsCodeMsgType, MMS_MSGTYPE_NOTIFYRESP_IND) | 0x80;
384 if (__MmsBinaryEncodeFieldCodeAndValue2(pFile, fieldCode, fieldValue) == false) {
385 MSG_DEBUG("_MmsBinaryEncodeNotiRespIndHdr: msgType error\n");
390 /* trID (other type of message) */
391 length = __MmsBinaryEncodeTextStringLen((UINT8*)szTrID);
393 MSG_DEBUG("_MmsBinaryEncodeNotiRespIndHdr: MmsBinaryEncodeTextStringLen fail \n");
397 if ((gMmsEncodeMaxLen2 - gCurMmsEncodeBuffPos2) < (length + 1)) {
398 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf2, &gCurMmsEncodeBuffPos2,
399 gMmsEncodeMaxLen2, &gMmsEncodeCurOffset2) == false) {
400 MSG_DEBUG("_MmsBinaryEncodeNotiRespIndHdr: MsgWriteDataFromEncodeBuffer fail \n");
405 gpMmsEncodeBuf2[gCurMmsEncodeBuffPos2++] = MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_TRID) | 0x80;
406 if (__MmsBinaryEncodeTextString2(pFile, (UINT8*)szTrID, length) == false) {
407 MSG_DEBUG("_MmsBinaryEncodeNotiRespIndHdr: MmsBinaryEncodeTextString fail\n");
412 if (__MmsBinaryEncodeMmsVersion2(pFile) == false) {
413 MSG_DEBUG("_MmsBinaryEncodeNotiRespIndHdr: __MmsBinaryEncodeMmsVersion error\n");
419 MSG_DEBUG("_MmsBinaryEncodeNotiRespIndHdr: MsgStatus = %d\n", iStatus);
421 if (iStatus != MSG_DELIVERY_REPORT_NONE) {
422 fieldCode = MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_MSGSTATUS) | 0x80;
423 fieldValue = MmsGetBinaryValue(MmsCodeMsgStatus, iStatus) | 0x80;
424 if (__MmsBinaryEncodeFieldCodeAndValue2(pFile, fieldCode, fieldValue) == false) {
425 MSG_DEBUG("_MmsBinaryEncodeNotiRespIndHdr: MsgStatus error\n");
432 fieldCode = MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_REPORTALLOWED) | 0x80;
434 if (bReportAllowed) {
435 fieldValue = MmsGetBinaryValue(MmsCodeReportAllowed, MMS_REPORTALLOWED_YES) | 0x80;
437 fieldValue = MmsGetBinaryValue(MmsCodeReportAllowed, MMS_REPORTALLOWED_NO) | 0x80;
440 if (__MmsBinaryEncodeFieldCodeAndValue2(pFile, fieldCode, fieldValue) == false) {
441 MSG_DEBUG("_MmsBinaryEncodeNotiRespIndHdr: Report Allowed error\n");
445 /* flush remained data on encoding file */
446 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf2, &gCurMmsEncodeBuffPos2,
447 gMmsEncodeMaxLen2, &gMmsEncodeCurOffset2) == false) {
448 MSG_DEBUG("_MmsBinaryEncodeNotiRespIndHdr: remained data MsgWriteDataFromEncodeBuffer fail \n");
459 bool MmsEncodeAckInd(FILE *pFile, char *pTrID, bool bReportAllowed)
461 __MmsRegisterEncodeBuffer2(gszMmsEncodeBuf2, MSG_MMS_ENCODE_BUFFER_MAX);
463 MSG_DEBUG("_MmsEncodeAckInd: Start Binary Encoding now ============= \n");
465 if (__MmsBinaryEncodeAckIndHdr(pFile, pTrID, bReportAllowed) == false) {
466 MSG_DEBUG("_MmsEncodeAckInd: SendReq Binary encoding fail \n");
470 __MmsUnregisterEncodeBuffer2();
476 MSG_DEBUG("## _MmsEncodeAckInd: failed");
477 __MmsUnregisterEncodeBuffer2();
482 bool MmsEncodeNotiRespInd(FILE *pFile, char *pTrID, msg_delivery_report_status_t iStatus, bool bReportAllowed)
484 __MmsRegisterEncodeBuffer2(gszMmsEncodeBuf2, MSG_MMS_ENCODE_BUFFER_MAX);
486 MSG_DEBUG("_MmsEncodeNotiRespInd: Start Binary Encoding now ============= \n");
488 if (__MmsBinaryEncodeNotiRespIndHdr(pFile, pTrID, iStatus, bReportAllowed) == false) {
489 MSG_DEBUG("_MmsEncodeNotiRespInd: SendReq Binary encoding fail \n");
493 __MmsUnregisterEncodeBuffer2();
502 /* Functions for Acknowledge.ind & NotifyResp.ind (END) ------------------------ */
503 bool MmsEncodeReadReport10(FILE *pFile, MmsMsg *pMsg, msg_read_report_status_t mmsReadStatus)
506 MsgMultipart *pPart = NULL;
510 char *pszReportMsg = NULL;
513 struct tm *dateTime = NULL;
517 MmsRegisterEncodeBuffer(gszMmsEncodeBuf, MSG_MMS_ENCODE_BUFFER_MAX);
519 MSG_DEBUG("_MmsEncodeMsg: Start Binary Encoding now V1.0============= \n");
521 if (__MmsBinaryEncodeReadReport10Hdr(pFile, pMsg, mmsReadStatus) == false) {
522 MSG_DEBUG("_MmsEncodeReadReport10: SendReq Binary encoding fail \n");
526 memset(&msgType, 0, sizeof(MsgType));
527 memset(&msgBody, 0, sizeof(MsgBody));
529 pText = (char *)malloc(MSG_STDSTR_LONG);
531 MSG_DEBUG("__MmsSendReadReportV10: text body malloc fail \n");
535 memset(pText, 0, MSG_STDSTR_LONG);
538 dateTime = localtime(&RawTime);
539 dateSec = mktime(dateTime);
541 // get report message
542 if (mmsReadStatus == MSG_READ_REPORT_IS_DELETED) {
543 pszReportMsg = (char*)"Your message has been deleted " ;
545 pszReportMsg = (char*)"Your message has been read " ;
548 // make report body ..
549 maxLen = strlen (pszReportMsg) +16 /* date string */ + 8 /* enter chars */ ;
551 if (maxLen > MSG_STDSTR_LONG) {
552 snprintf (pText, MSG_STDSTR_LONG, "%s\n", pszReportMsg);
554 snprintf(pText, MSG_STDSTR_LONG, "%s\r\n\r\n%.4d/%.2d/%.2d %.2d:%.2d\r\n",
555 pszReportMsg, dateTime->tm_year+1900, dateTime->tm_mon+1, dateTime->tm_mday, dateTime->tm_hour, dateTime->tm_min);
559 msgType.type = MIME_APPLICATION_VND_WAP_MULTIPART_MIXED;
560 msgType.contentSize = strlen(pText);
561 msgType.param.charset = MSG_CHARSET_UNKNOWN;
564 if ((pPart = MmsAllocMultipart()) == NULL) {
565 MSG_DEBUG("__MmsSendReadReportV10: MsgAllocMultipart Fail \n");
570 pPart->type.type = MIME_TEXT_PLAIN;
571 pPart->type.contentSize = strlen(pText);
572 pPart->type.param.charset = MSG_CHARSET_UTF8;
574 if (pPart->pBody == NULL) {
575 MSG_DEBUG("__MmsSendReadReportV10: pPart->pBody is NULL \n");
579 pPart->pBody->size = strlen(pText);
580 pPart->pBody->body.pText = pText;
582 msgBody.body.pMultipart = pPart;
584 if (__MmsBinaryEncodeMsgBody(pFile, &msgType, &msgBody, 1, false) == false) {
585 MSG_DEBUG("__MmsSendReadReportV10: MmsBinaryEncodeMsgBody fail \n");
589 MSG_DEBUG("__MmsSendReadReportV10: Send To RM ReadReport Msg \n");
605 MmsUnregisterEncodeBuffer();
625 MmsUnregisterEncodeBuffer();
630 bool MmsEncodeReadReport11(FILE *pFile, MmsMsg *pMsg, msg_read_report_status_t mmsReadStatus)
632 MmsRegisterEncodeBuffer(gszMmsEncodeBuf, MSG_MMS_ENCODE_BUFFER_MAX);
634 MSG_DEBUG("_MmsEncodeMsg: Start Binary Encoding now V1.1============= \n");
636 if (__MmsBinaryEncodeReadReport11Hdr(pFile, pMsg, mmsReadStatus) == false) {
637 MSG_DEBUG("_MmsEncodeMsg: SendReq Binary encoding fail \n");
641 MmsUnregisterEncodeBuffer();
646 MmsUnregisterEncodeBuffer();
651 /* ==========================================================
653 B I N A R Y E N C O D I N G
655 ==========================================================*/
658 * Binary Encoded Message Format
660 * < Single Part Body Message >
661 * -----------------------------------
663 * -----------------------------------
664 * | Content Type:start=xxx;type=xxx | ->(ex) Text/Plain, Text/Html, ....
665 * -----------------------------------
666 * | Single Part Body |
667 * -----------------------------------
669 * < Multi Part Body Message >
670 * -----------------------------------
672 * -----------------------------------
673 * | Content Type:start=xxx;type=xxx | -> (ex) Application/vnd.wap.multipart.mixed(related), multipart/mixed(related)
674 * -----------------------------------
675 * | # of Entries (body parts) |
676 * ----------------------------------- < Each Entry >
677 * | Entry 1 | -> -----------------------------
678 * ----------------------------------- | header Length |
679 * | Entry 2 | -----------------------------
680 * ----------------------------------- | Data Length |
681 * | ...... | ----------------------------- -
682 * ----------------------------------- | Content-Type | |
683 * | Entry n | ----------------------------- | Header Length
684 * ----------------------------------- | Header | |
685 * ----------------------------- -
686 * | Data | | Data Length
687 * ----------------------------- -
689 static bool __MmsBinaryEncodeSendReqHdr(FILE *pFile, MmsMsg *pMsg)
691 UINT8 fieldCode = 0xff;
692 UINT8 fieldValue = 0xff;
695 __MmsCleanEncodeBuff();
698 fieldCode = MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_MSGTYPE) | 0x80;
699 fieldValue = MmsGetBinaryValue(MmsCodeMsgType, MMS_MSGTYPE_SEND_REQ) | 0x80;
700 if (__MmsBinaryEncodeFieldCodeAndValue(pFile, fieldCode, fieldValue) == false) {
701 MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: msgType error\n");
705 /* trID (other type of message) */
706 if (__MmsBinaryEncodeTrID(pFile, pMsg->szTrID, MMS_TR_ID_LEN + 1) == false) {
707 MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: trID error\n");
711 MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: pMsg->szTrID = %s\n", pMsg->szTrID);
713 if (__MmsBinaryEncodeMmsVersion(pFile) == false) {
714 MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: __MmsBinaryEncodeMmsVersion error\n");
718 /* From : Insert Token mode */
719 if (__MmsBinaryEncodeFrom(pFile) == false) {
720 MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: __MmsBinaryEncodeFrom fail\n");
725 /* To = Encoded-string-value */
726 if (__MmsBinaryEncodeAddress(pFile, MMS_CODE_TO, pMsg->mmsAttrib.szTo) == false)
728 MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: To __MmsBinaryEncodeAddress fail\n");
733 /* Cc = Encoded-string-value */
734 if (__MmsBinaryEncodeAddress(pFile, MMS_CODE_CC, pMsg->mmsAttrib.szCc) == false) {
735 MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: Cc __MmsBinaryEncodeAddress fail\n");
740 /* Bcc = Encoded-string-value */
742 if (__MmsBinaryEncodeAddress(pFile, MMS_CODE_BCC, pMsg->mmsAttrib.szBcc) == false) {
743 MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: Bcc __MmsBinaryEncodeAddress fail\n");
748 MSG_DEBUG("_MmsBinaryEnocdeSendReqHdr() pMsg->mmsAttrib.szSubject =%s\n",pMsg->mmsAttrib.szSubject);
750 /* Subject = Encoded-string-value */
751 if (pMsg->mmsAttrib.szSubject[0]) {
752 length = __MmsBinaryEncodeEncodedStringLen((UINT8*)pMsg->mmsAttrib.szSubject);
754 MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: subject MmsBinaryEncodeEncodedStringLen fail \n");
758 if ((gMmsEncodeMaxLen - gCurMmsEncodeBuffPos) < length + 1) {
759 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
760 gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
761 MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: subject MsgWriteDataFromEncodeBuffer fail \n");
766 fieldCode = MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_SUBJECT) | 0x80;
767 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = fieldCode;
768 if (__MmsBinaryEncodeEncodedString(pFile, (UINT8*)pMsg->mmsAttrib.szSubject, length) == false) {
769 MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: subject MmsBinaryEncodeEncodedString fail \n");
774 /* MMS-1.3-con-739 */
776 fieldCode = MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_MSGCLASS) | 0x80;
777 fieldValue = MmsGetBinaryValue(MmsCodeMsgClass, (int)pMsg->mmsAttrib.msgClass) | 0x80;
778 if (__MmsBinaryEncodeFieldCodeAndValue(pFile, fieldCode, fieldValue) == false) {
779 MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: msgClass error\n");
782 /* MMS-1.3-con-739 */
783 /* MMS-1.3-con-733 */
784 /* Date = Long-integer */
785 if (!__MmsBinaryEncodeDate(pFile)) {
786 MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: __MmsBinaryEncodeDate error\n");
789 /* MMS-1.3-con-733 */
791 /* Expiry Time : Value-length Absolute-token Date-value */
792 if (pMsg->mmsAttrib.bUseExpiryCustomTime == true) {
793 if (__MmsBinaryEncodeTime(pFile, MMS_CODE_EXPIRYTIME, pMsg->mmsAttrib.expiryCustomTime) == false) {
794 MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: expiryTime __MmsBinaryEncodeTime fail\n");
797 } else if (pMsg->mmsAttrib.expiryTime.type != MMS_TIMETYPE_NONE) { // for avoiding the creation of the expiry field in case the user selects the maximum
798 if (__MmsBinaryEncodeTime(pFile, MMS_CODE_EXPIRYTIME, pMsg->mmsAttrib.expiryTime) == false) {
799 MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: expiryTime __MmsBinaryEncodeTime fail\n");
804 /* Use Custom time for Delivery Time */
805 if (pMsg->mmsAttrib.bUseDeliveryCustomTime == true) {
806 if (__MmsBinaryEncodeTime(pFile, MMS_CODE_DELIVERYTIME, pMsg->mmsAttrib.deliveryCustomTime) == false) {
807 MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: deliveryTime __MmsBinaryEncodeTime fail\n");
811 if (__MmsBinaryEncodeTime(pFile, MMS_CODE_DELIVERYTIME, pMsg->mmsAttrib.deliveryTime) == false) {
812 MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: deliveryTime __MmsBinaryEncodeTime fail\n");
818 if (pMsg->mmsAttrib.priority!= MMS_PRIORITY_ERROR && pMsg->mmsAttrib.priority!= MMS_PRIORITY_NORMAL) {
819 /* MMS_PRIORITY_NORMAL is default : don't send optional field */
821 fieldCode = MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_PRIORITY)|0x80;
822 fieldValue = MmsGetBinaryValue(MmsCodePriority, pMsg->mmsAttrib.priority)|0x80;
824 if (__MmsBinaryEncodeFieldCodeAndValue(pFile, fieldCode, fieldValue) == false) {
825 MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: priority error\n");
830 /* Sender Visible (hide | show) */
831 if (pMsg->mmsAttrib.bHideAddress == true) {
832 fieldCode = MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_SENDERVISIBILLITY) | MSB;
833 fieldValue = MmsGetBinaryValue(MmsCodeSenderVisibility, MMS_SENDER_HIDE) | 0x80;
835 if (__MmsBinaryEncodeFieldCodeAndValue(pFile, fieldCode, fieldValue) == false) {
836 MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: sender visibility error\n");
841 /* Delivery Report (yes | no) */
842 fieldCode = MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_DELIVERYREPORT)|0x80;
844 if (pMsg->mmsAttrib.bAskDeliveryReport) {
845 fieldValue = MmsGetBinaryValue(MmsCodeDeliveryReport, MMS_REPORT_YES) | 0x80;
847 fieldValue = MmsGetBinaryValue(MmsCodeDeliveryReport, MMS_REPORT_NO) | 0x80;
850 if (__MmsBinaryEncodeFieldCodeAndValue(pFile, fieldCode, fieldValue) == false) {
851 MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: sender visibility error\n");
855 /* Read Reply (Yes | no) */
856 fieldCode = MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_READREPLY)|0x80;
857 if (pMsg->mmsAttrib.bAskReadReply) {
858 fieldValue = MmsGetBinaryValue(MmsCodeReadReply, MMS_REPORT_YES)|0x80;
860 fieldValue = MmsGetBinaryValue(MmsCodeReadReply, MMS_REPORT_NO)|0x80;
863 if (__MmsBinaryEncodeFieldCodeAndValue(pFile, fieldCode, fieldValue) == false) {
864 MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: read reply error\n");
868 if ((pMsg->mmsAttrib.replyCharge.chargeType == MMS_REPLY_REQUESTED) ||
869 (pMsg->mmsAttrib.replyCharge.chargeType == MMS_REPLY_REQUESTED_TEXT_ONLY)) {
872 fieldCode = MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_REPLYCHARGING)|0x80;
873 fieldValue = MmsGetBinaryValue(MmsCodeReadReply, pMsg->mmsAttrib.replyCharge.chargeType)|0x80;
875 if (__MmsBinaryEncodeFieldCodeAndValue(pFile, fieldCode, fieldValue) == false) {
876 MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: replyCharging error\n");
880 /** fixme: Reply-charging-deadline */
881 if (pMsg->mmsAttrib.replyCharge.deadLine.time > 0) {
882 if (__MmsBinaryEncodeTime(pFile, MMS_CODE_REPLYCHARGINGDEADLINE, pMsg->mmsAttrib.replyCharge.deadLine) == false) {
883 MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: replyCharging __MmsBinaryEncodeTime fail\n");
888 /** fixme: Reply-charging-size */
889 if (pMsg->mmsAttrib.replyCharge.chargeSize > 0) {
890 length = __MmsBinaryEncodeIntegerLen(pMsg->mmsAttrib.replyCharge.chargeSize);
892 if ((gMmsEncodeMaxLen - gCurMmsEncodeBuffPos) < (length + 1)) {
893 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
894 gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
895 MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: MsgWriteDataFromEncodeBuffer fail \n");
900 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_REPLYCHARGINGSIZE) | 0x80;
902 if (__MmsBinaryEncodeInteger(pFile, pMsg->mmsAttrib.replyCharge.chargeSize, length) == false) {
903 MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: replyChargingSize MmsBinaryEncodeInteger error\n");
908 /** fixme: Reply-charging-ID ----> used only when reply message */
909 if (pMsg->mmsAttrib.replyCharge.szChargeID[0]) {
910 length = __MmsBinaryEncodeTextStringLen((UINT8*)pMsg->mmsAttrib.replyCharge.szChargeID);
912 MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: szReplyChargingID MmsBinaryEncodeTextStringLen fail\n");
916 if ((gMmsEncodeMaxLen - gCurMmsEncodeBuffPos) < (length + 1)) {
917 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
918 gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
919 MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: MsgWriteDataFromEncodeBuffer fail \n");
924 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_REPLYCHARGINGID) | 0x80;
926 if (__MmsBinaryEncodeTextString(pFile, (UINT8*)pMsg->mmsAttrib.replyCharge.szChargeID, length) == false) {
927 MSG_DEBUG("szContentLocation MmsBinaryEncodeTextString fail\n");
933 /* flush remained data on encoding file */
934 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos, gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
935 MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: remained data MsgWriteDataFromEncodeBuffer fail \n");
943 MSG_DEBUG("## _MmsBinaryEncodeSendReqHdr: failed");
947 static bool __MmsBinaryEncodeReadReport10Hdr(FILE *pFile, MmsMsg *pMsg, msg_read_report_status_t mmsReadStatus)
951 UINT8 fieldCode = 0xff;
952 UINT8 fieldValue = 0xff;
953 char szSubject[MSG_LOCALE_SUBJ_LEN + 8] = {0, };
956 MSG_DEBUG("pMsg is NULL");
960 MSG_DEBUG("_MmsBinaryEncodeReadReport10Hdr: mmsReadStatus = %d\n", mmsReadStatus);
962 __MmsCleanEncodeBuff();
965 fieldCode = MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_MSGTYPE) | 0x80;
966 fieldValue = MmsGetBinaryValue(MmsCodeMsgType, MMS_MSGTYPE_SEND_REQ) | 0x80;
967 if (__MmsBinaryEncodeFieldCodeAndValue(pFile, fieldCode, fieldValue) == false) {
968 MSG_DEBUG("_MmsBinaryEncodeReadReport10Hdr: msgType error\n");
972 /* trID (other type of message) */
973 if (__MmsBinaryEncodeTrID(pFile, NULL, 0) == false) {
974 MSG_DEBUG("_MmsBinaryEncodeReadReport10Hdr: __MmsBinaryEncodeTrID error\n");
978 if (__MmsBinaryEncodeMmsVersion(pFile) == false) {
979 MSG_DEBUG("_MmsBinaryEncodeReadReport10Hdr: __MmsBinaryEncodeMmsVersion error\n");
983 /* Date = Long-integer */
984 if (!__MmsBinaryEncodeDate(pFile)) {
985 MSG_DEBUG("_MmsBinaryEncodeReadReport10Hdr: date __MmsBinaryEncodeDate error\n");
989 /* From : Insert Token mode */
990 if (__MmsBinaryEncodeFrom(pFile) == false) {
991 MSG_DEBUG("_MmsBinaryEncodeReadReport10Hdr: __MmsBinaryEncodeFrom fail\n");
995 MSG_DEBUG("_MmsBinaryEncodeReadReport10Hdr: To = %s\n", pMsg->mmsAttrib.szFrom);
997 /* To = Encoded-string */
998 if (pMsg && (strchr(pMsg->mmsAttrib.szFrom, '/') == NULL)) {
999 length = strlen(pMsg->mmsAttrib.szFrom);
1000 szTo = (char *)malloc(length + 11);
1002 MSG_DEBUG("_MmsBinaryEncodeReadReport10Hdr: szTo alloc fail\n");
1006 snprintf(szTo, length + 11, "%s/TYPE=PLMN", pMsg->mmsAttrib.szFrom);
1007 MSG_DEBUG("_MmsBinaryEncodeReadReport10Hdr: To = %s\n", szTo);
1009 if (__MmsBinaryEncodeAddress(pFile, MMS_CODE_TO, szTo) == false) {
1010 MSG_DEBUG("_MmsBinaryEncodeReadReport10Hdr: To __MmsBinaryEncodeAddress fail\n");
1019 if (__MmsBinaryEncodeAddress(pFile, MMS_CODE_TO, pMsg->mmsAttrib.szFrom) == false) {
1020 MSG_DEBUG("_MmsBinaryEncodeReadReport10Hdr: To __MmsBinaryEncodeAddress fail\n");
1025 /* Subject = Encoded-string-value */
1026 if (pMsg && pMsg->mmsAttrib.szSubject[0]) {
1027 if (mmsReadStatus == MSG_READ_REPORT_IS_READ) {
1028 snprintf(szSubject, MSG_LOCALE_SUBJ_LEN + 8, "%s%s", MMS_READ_REPORT_STRING_READ, pMsg->mmsAttrib.szSubject);
1030 snprintf(szSubject, MSG_LOCALE_SUBJ_LEN + 8, "%s%s", MMS_READ_REPORT_STRING_DELETED, pMsg->mmsAttrib.szSubject);
1033 if (mmsReadStatus == MSG_READ_REPORT_IS_READ) {
1034 snprintf(szSubject, MSG_LOCALE_SUBJ_LEN + 8, "%s", MMS_READ_REPORT_STRING_READ);
1037 snprintf(szSubject, MSG_LOCALE_SUBJ_LEN + 8, "%s", MMS_READ_REPORT_STRING_DELETED );
1041 length = __MmsBinaryEncodeEncodedStringLen((UINT8*)szSubject);
1043 MSG_DEBUG("_MmsBinaryEncodeReadReport10Hdr: subject MmsBinaryEncodeEncodedStringLen fail \n");
1047 if ((gMmsEncodeMaxLen - gCurMmsEncodeBuffPos) < length + 1) {
1048 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos, gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
1049 MSG_DEBUG("_MmsBinaryEncodeReadReport10Hdr: subject MsgWriteDataFromEncodeBuffer fail \n");
1054 fieldCode = MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_SUBJECT) | 0x80;
1055 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = fieldCode;
1056 if (__MmsBinaryEncodeEncodedString(pFile, (UINT8*)szSubject, length) == false) {
1057 MSG_DEBUG("_MmsBinaryEncodeReadReport10Hdr: subject MmsBinaryEncodeEncodedString fail \n");
1062 fieldCode = MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_MSGCLASS) | 0x80;
1063 fieldValue = MmsGetBinaryValue(MmsCodeMsgClass, MMS_MSGCLASS_AUTO) | 0x80;
1064 if (__MmsBinaryEncodeFieldCodeAndValue(pFile, fieldCode, fieldValue) == false) {
1065 MSG_DEBUG("_MmsBinaryEncodeReadReport10Hdr: msgClass error\n");
1070 /* Delivery Report (yes | no) */
1071 fieldCode = MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_DELIVERYREPORT) | 0x80;
1072 fieldValue = MmsGetBinaryValue(MmsCodeDeliveryReport, MMS_REPORT_NO) | 0x80;
1073 if (__MmsBinaryEncodeFieldCodeAndValue(pFile, fieldCode, fieldValue) == false) {
1074 MSG_DEBUG("_MmsBinaryEncodeReadReport10Hdr: Delivery Report error\n");
1079 /* Read Reply (Yes | no) */
1080 fieldCode = MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_READREPLY) | 0x80;
1081 fieldValue = MmsGetBinaryValue(MmsCodeReadReply, MMS_REPORT_NO) | 0x80;
1082 if (__MmsBinaryEncodeFieldCodeAndValue(pFile, fieldCode, fieldValue) == false) {
1083 MSG_DEBUG("_MmsBinaryEncodeReadReport10Hdr: Read Reply error\n");
1088 /* Sender Visible (hide | show) */
1089 fieldCode = MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_SENDERVISIBILLITY) | 0x80;
1090 fieldValue = MmsGetBinaryValue(MmsCodeSenderVisibility, MMS_SENDER_SHOW) | 0x80;
1091 if (__MmsBinaryEncodeFieldCodeAndValue(pFile, fieldCode, fieldValue) == false) {
1092 MSG_DEBUG("_MmsBinaryEncodeReadReport10Hdr: Sender Visible error\n");
1096 /* fixme: MimeType */
1097 /* fixme: msgHeader */
1098 /* fixme: msgBody */
1100 /* flush remained data on encoding file */
1101 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
1102 gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
1103 MSG_DEBUG("_MmsBinaryEncodeReadReport10Hdr: remained data MsgWriteDataFromEncodeBuffer fail \n");
1118 static bool __MmsBinaryEncodeReadReport11Hdr(FILE *pFile, MmsMsg *pMsg, msg_read_report_status_t mmsReadStatus)
1120 UINT8 fieldCode = 0xff;
1121 UINT8 fieldValue = 0xff;
1124 __MmsCleanEncodeBuff();
1127 fieldCode = MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_MSGTYPE) | 0x80;
1128 fieldValue = MmsGetBinaryValue(MmsCodeMsgType, MMS_MSGTYPE_READREC_IND) | 0x80;
1129 if (__MmsBinaryEncodeFieldCodeAndValue(pFile, fieldCode, fieldValue) == false) {
1130 MSG_DEBUG("_MmsBinaryEncodeReadReport11Hdr: msgType error\n");
1135 if (__MmsBinaryEncodeMmsVersion(pFile) == false) {
1136 MSG_DEBUG("_MmsBinaryEncodeReadReport11Hdr: __MmsBinaryEncodeMmsVersion error\n");
1140 if (__MmsBinaryEncodeMsgID(pFile, pMsg->szMsgID) == false)
1143 /* To = Encoded-string */
1144 if (strchr(pMsg->mmsAttrib.szFrom, '/') == NULL) {
1146 length = strlen(pMsg->mmsAttrib.szFrom);
1147 szTo = (char *)malloc(length + 11);
1150 MSG_DEBUG("_MmsBinaryEncodeReadReport10Hdr: szTo alloc fail\n");
1154 snprintf(szTo, length + 11,"%s/TYPE=PLMN", pMsg->mmsAttrib.szFrom);
1155 MSG_DEBUG("_MmsBinaryEncodeReadReport10Hdr: To = %s\n", szTo);
1157 if (__MmsBinaryEncodeAddress(pFile, MMS_CODE_TO, szTo) == false) {
1158 MSG_DEBUG("_MmsBinaryEncodeReadReport10Hdr: To __MmsBinaryEncodeAddress fail\n");
1167 if (__MmsBinaryEncodeAddress(pFile, MMS_CODE_TO, pMsg->mmsAttrib.szFrom) == false) {
1168 MSG_DEBUG("_MmsBinaryEncodeReadReport11Hdr: To __MmsBinaryEncodeAddress fail\n");
1173 /* From : Insert Token mode */
1174 if (__MmsBinaryEncodeFrom(pFile) == false) {
1175 MSG_DEBUG("_MmsBinaryEncodeReadReport11Hdr: __MmsBinaryEncodeFrom fail\n");
1179 /* Date = Long-integer */
1180 if (!__MmsBinaryEncodeDate(pFile)) {
1181 MSG_DEBUG("_MmsBinaryEncodeReadReport11Hdr: __MmsBinaryEncodeDate error\n");
1185 /* Read Status (Yes | no) */
1186 fieldCode = MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_READSTATUS) | 0x80;
1187 fieldValue = MmsGetBinaryValue(MmsCodeReadStatus, mmsReadStatus) | 0x80;
1189 if (__MmsBinaryEncodeFieldCodeAndValue(pFile, fieldCode, fieldValue) == false) {
1190 MSG_DEBUG("_MmsBinaryEncodeReadReport11Hdr: Read Status error\n");
1194 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
1195 gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
1196 MSG_DEBUG("_MmsBinaryEncodeReadReport11Hdr: remained data MsgWriteDataFromEncodeBuffer fail \n");
1211 static bool __MmsBinaryEncodeMsgBody(FILE *pFile, MsgType *pType, MsgBody *pBody, int nPartCount, bool bTemplate)
1214 MsgMultipart *pMultipart = NULL;
1216 MSG_DEBUG("MmsBinaryEncodeMsgBody: nPartCount = %d\n", nPartCount);
1218 if (pFile == NULL || pType == NULL) {
1219 MSG_DEBUG("MmsBinaryEncodeMsgBody: invalid file handle\n");
1223 if (MmsIsMultipart(pType->type)) {
1226 /* ---------------------------
1228 * ---------------------------*/
1230 pType->offset = __MmsGetEncodeOffset();
1233 length = __MmsBinaryEncodeContentTypeLen(pType);
1235 MSG_DEBUG("MmsBinaryEncodeMsgBody: MmsBinaryEncodeContentTypeLen fail \n");
1238 if (bTemplate == false)
1239 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_CONTENTTYPE) | 0x80;
1241 if (__MmsBinaryEncodeContentType(pFile, pType, length) == false) {
1242 MSG_DEBUG("MmsBinaryEncodeMsgBody: MmsBinaryEncodeContentType fail \n");
1246 pBody->offset = __MmsGetEncodeOffset();
1249 if (pBody->pPresentationBody) {
1250 nEntries = nPartCount + 1;
1252 nEntries = nPartCount;
1255 if (nEntries >= 0) {
1256 length = __MmsBinaryEncodeUintvarLen(nEntries);
1258 MSG_DEBUG("MmsBinaryEncodeMsgBody: nEntries MmsBinaryEncodeUintvarLen fail \n");
1261 if (__MmsBinaryEncodeUintvar(pFile, nEntries, length) == false) {
1262 MSG_DEBUG("MmsBinaryEncodeMsgBody: nEntries MmsBinaryEncodeUintvar fail \n");
1266 pType->size = __MmsGetEncodeOffset() - pType->offset;
1270 if (nEntries && pBody->pPresentationBody) {
1271 if (__MmsBinaryEncodeMsgPart(pFile, pType->type, &pBody->presentationType, pBody->pPresentationBody) == false) {
1272 MSG_DEBUG("MmsBinaryEncodeMsgBody: __MmsBinaryEncodeMsgPart fail \n");
1279 pMultipart = pBody->body.pMultipart;
1280 while (nEntries && pMultipart) {
1281 if (__MmsBinaryEncodeMsgPart(pFile, pType->type, &pMultipart->type, pMultipart->pBody) == false) {
1282 MSG_DEBUG("MmsBinaryEncodeMsgBody: __MmsBinaryEncodeMsgPart fail \n");
1285 pMultipart = pMultipart->pNext;
1289 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
1290 gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
1291 MSG_DEBUG("MmsBinaryEncodeMsgBody: Empty message body MsgWriteDataFromEncodeBuffer fail \n");
1296 pBody->size = __MmsGetEncodeOffset() - pBody->offset;
1298 /* ---------------------------
1299 * Singlepart message
1300 * ---------------------------*/
1301 pType->offset = __MmsGetEncodeOffset();
1303 if (__MmsBinaryEncodeContentHeader(pFile, (MimeType)pType->type, pType, false) == false)
1306 length = __MmsBinaryEncodeContentTypeLen(pType);
1308 MSG_DEBUG("MmsBinaryEncodeMsgBody: Singlepart MmsBinaryEncodeContentTypeLen fail \n");
1312 if (__MmsBinaryEncodeContentType(pFile, pType, length) == false)
1315 pType->size = __MmsGetEncodeOffset() - pType->offset;
1317 if (__MmsBinaryEncodeContentBody(pFile, pBody) == false)
1324 MSG_DEBUG("## MmsBinaryEncodeMsgBody: failed\n");
1329 static int __MmsBinaryEncodeContentTypeLen(MsgType *pType)
1332 int totalLength = 0;
1333 UINT16 fieldValue = 0xffff;
1334 const char *szTextType = NULL;
1335 int contentType = MIME_UNKNOWN;
1337 MSG_DEBUG("MSmsBinaryEncodeContentTypeLen: type\n");
1340 * Content-type-value = Constrained-media | Content-general-form
1341 * Constrained-media = Constrained-encoding
1342 * Constrained-encoding = Extension-Media | Short-integer
1343 * Extension-media = *TEXT End-of-string
1344 * Content-general-form = Value-length Media-type
1345 * Media-type = (Well-known-media | Extension-Media) *(Parameter)
1348 /* Content-type-value = Content-general-form ------------------------------- */
1351 contentType = pType->type;
1353 fieldValue = MmsGetBinaryValue(MmsCodeContentType, contentType);
1354 if (fieldValue == UNDEFINED_BINARY || fieldValue == 0x0049) {
1355 /* Extension-media type */
1356 szTextType = MmsGetTextValue(MmsCodeContentType, contentType);
1357 if (szTextType != NULL) {
1358 length = __MmsBinaryEncodeTextStringLen((UINT8*)szTextType);
1360 MSG_DEBUG("MSmsBinaryEncodeContentTypeLen: szTextType MmsBinaryEncodeTextStringLen fail \n");
1363 totalLength += length;
1371 /* Parameters -------------------------------------------------------------- */
1373 MSG_DEBUG("MSmsBinaryEncodeContentTypeLen: parameters \n\n");
1375 /* Well-known-charset = Any-charset | Integer-value ----------------------- */
1377 if (pType->param.charset != MSG_CHARSET_UNKNOWN) {
1378 fieldValue = MmsGetBinaryValue(MmsCodeCharSet, pType->param.charset);
1379 length = __MmsBinaryEncodeIntegerLen(fieldValue);
1381 MSG_DEBUG("MmsBinaryEncodeContentTypeLen: charSet MmsBinaryEncodeIntegerLen fail \n");
1384 totalLength += (length + 1);
1386 if (MmsIsText(contentType)) { // Any-charset
1387 if (!MmsIsVitemContent (contentType, pType->param.szName))
1393 /* Name = Text-string ------------------------------------------------------ */
1395 if (pType->param.szName[0]) {
1396 char* pszName = NULL;
1398 if (MsgIsASCII (pType->param.szName)) {
1399 MSG_DEBUG("MmsBinaryEncodeContentTypeLen: szName is consisted of ascii char-set chars. \n");
1401 pszName = (char *)malloc(strlen(pType->param.szName) +1);
1402 memset(pszName, 0, (strlen(pType->param.szName)+1));
1403 strcpy(pszName, pType->param.szName);
1405 MSG_DEBUG("MmsBinaryEncodeContentTypeLen: szName is not consisted of ascii char-set chars. \n");
1406 MSG_DEBUG("MmsBinaryEncodeContentTypeLen: pType->param.szName : %s\n", pType->param.szName);
1408 /* if msg-server can't support non ascii, then convert non-ascii to '_' by using _MsgReplaceNonAscii*/
1409 int filelen = strlen(pType->param.szName);
1411 pszName = (char *)malloc(filelen + 1);
1412 memset(pszName, 0, (filelen + 1));
1413 strncpy(pszName, pType->param.szName, filelen);
1415 MSG_DEBUG("MmsBinaryEncodeContentTypeLen: pszName : %s\n", pszName);
1418 //change empty space to '_' in the file name
1419 if (MsgIsSpace(pszName)) {
1420 char *pszTempName = NULL;
1422 MSG_DEBUG("MmsBinaryEncodeContentTypeLen: szName has space(' '). \n");
1424 MsgReplaceSpecialChar(pszName, &pszTempName, ' ');
1428 pszName = pszTempName;
1430 MSG_DEBUG("MmsBinaryEncodeContentTypeLen: pszName : %s\n", pszName);
1433 length = __MmsBinaryEncodeTextStringLen((UINT8*)pszName);
1437 MSG_DEBUG("MmsBinaryEncodeContentTypeLen: szName MmsBinaryEncodeIntegerLen fail \n");
1441 totalLength += (length + 1);
1444 #ifdef FEATURE_JAVA_MMS
1445 if (pType->param.szApplicationID) {
1446 length = __MmsBinaryEncodeTextStringLen((UINT8*)pType->param.szApplicationID);
1448 MSG_DEBUG("MmsBinaryEncodeContentTypeLen: szApplicationID MmsBinaryEncodeTextStrinLen fail \n");
1452 totalLength += (length);
1453 if (MmsGetBinaryValue(MmsCodeParameterCode, MSG_PARAM_APPLICATION_ID) == UNDEFINED_BINARY) {
1454 totalLength += strlen(MmsGetTextValue(MmsCodeParameterCode, MSG_PARAM_APPLICATION_ID)) + 1; /* NULL */
1460 if (pType->param.szReplyToApplicationID) {
1461 length = __MmsBinaryEncodeTextStringLen((UINT8*)pType->param.szReplyToApplicationID);
1463 MSG_DEBUG("MmsBinaryEncodeContentTypeLen: szApplicationID MmsBinaryEncodeTextStrinLen fail \n");
1467 totalLength += (length);
1468 if (MmsGetBinaryValue(MmsCodeParameterCode, MSG_PARAM_REPLY_TO_APPLICATION_ID) == UNDEFINED_BINARY) {
1469 totalLength += strlen(MmsGetTextValue(MmsCodeParameterCode, MSG_PARAM_REPLY_TO_APPLICATION_ID)) + 1; /* NULL */
1476 /* type, start, & startInfo : multipart/related only parameters -------------- */
1477 if (contentType == MIME_MULTIPART_RELATED || contentType == MIME_APPLICATION_VND_WAP_MULTIPART_RELATED) {
1478 /* type ------------------------------------- */
1479 fieldValue = MmsGetBinaryValue(MmsCodeContentType, pType->param.type);
1480 if (fieldValue == UNDEFINED_BINARY || fieldValue == 0x0049) {
1481 /* Extension-media type */
1482 szTextType = MmsGetTextValue(MmsCodeContentType, pType->param.type);
1483 if (szTextType != NULL) {
1484 length = __MmsBinaryEncodeTextStringLen((UINT8*)szTextType);
1486 MSG_DEBUG("MmsBinaryEncodeContentTypeLen: type param MmsBinaryEncodeTextStringLen fail \n");
1489 totalLength += (length + 1);
1497 /* start = Text-string ----------------------- */
1498 if (pType->param.szStart[0]) {
1499 /* start = Text-string */
1500 length = __MmsBinaryEncodeTextStringLen((UINT8*)pType->param.szStart);
1502 MSG_DEBUG("MmsBinaryEncodeContentType: szStart MmsBinaryEncodeTextStringLen fail \n");
1506 totalLength += (length + 1);
1510 /* startInfo = Text-string -------------------- */
1511 if (pType->param.szStartInfo[0]) {
1512 /* StartInfo (with multipart/related) = Text-string */
1513 length = __MmsBinaryEncodeTextStringLen((UINT8*)pType->param.szStartInfo);
1515 MSG_DEBUG("MmsBinaryEncodeContentType: szStartInfo MmsBinaryEncodeTextStringLen fail \n");
1519 totalLength += (length + 1);
1527 MSG_DEBUG("## MmsBinaryEncodeContentTypeLen: failed");
1531 static bool __MmsBinaryEncodeContentType(FILE *pFile, MsgType *pType, int typeLength)
1534 UINT16 fieldValue = 0xffff;
1535 const char *szTextType = NULL;
1536 int contentType = MIME_UNKNOWN;
1538 #ifdef FEATURE_JAVA_MMS
1539 const char *szParameter = NULL;
1542 MSG_DEBUG("************************************************************************************\n");
1543 MSG_DEBUG("MmsBinaryEncodeContentType: C O N T E N T T Y P E \n\n");
1546 * Content-type-value = Constrained-media | Content-general-form
1547 * Constrained-media = Constrained-encoding
1548 * Constrained-encoding = Extension-Media | Short-integer
1549 * Extension-media = *TEXT End-of-string
1550 * Content-general-form = Value-length Media-type
1551 * Media-type = (Well-known-media | Extension-Media) *(Parameter)
1554 if (pFile == NULL) {
1555 MSG_DEBUG("MmsBinaryEncodeContentType: invalid file handle\n");
1560 /* Content-Type = Content-general-form ------------------------------- */
1562 length = __MmsBinaryEncodeValueLengthLen(typeLength);
1564 MSG_DEBUG("MSmsBinaryEncodeContentType : MmsBinaryEncodeValueLengthLen fail.\n");
1568 if (__MmsBinaryEncodeValueLength(pFile, typeLength, length) == false) {
1569 MSG_DEBUG("MSmsBinaryEncodeContentType : MmsBinaryEncodeValueLength fail.\n");
1573 contentType = pType->type;
1575 fieldValue = MmsGetBinaryValue(MmsCodeContentType, (int)contentType);
1576 if (fieldValue == UNDEFINED_BINARY || fieldValue == 0x0049) {
1577 /* Extension-media type */
1578 szTextType = MmsGetTextValue(MmsCodeContentType, (int)contentType);
1579 if (szTextType == NULL)
1583 if (szTextType == NULL) {
1584 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = (UINT8)fieldValue | 0x80;
1586 length = __MmsBinaryEncodeTextStringLen((UINT8*)szTextType);
1588 MSG_DEBUG("MSmsBinaryEncodeContentType: szTextType MmsBinaryEncodeTextStringLen fail \n");
1592 if (__MmsBinaryEncodeTextString(pFile, (UINT8*)szTextType, length) == false) {
1593 MSG_DEBUG("MmsBinaryEncodeContentType: szTextType MmsBinaryEncodeTextString fail \n");
1598 /* Parameters -------------------------------------------------------------- */
1600 MSG_DEBUG("MmsBinaryEncodeContentType: P A R M E T E R S \n\n");
1602 /* Name = Text-string ------------------------------------------------------ */
1604 if (pType->param.szName[0]) {
1605 char* pszName = NULL;
1607 if (MsgIsASCII (pType->param.szName)) {
1609 MSG_DEBUG("MmsBinaryEncodeContentType: szName is consisted of ascii char-set chars. \n");
1611 pszName = (char *)malloc(strlen(pType->param.szName) +1);
1612 memset(pszName, 0, (strlen(pType->param.szName)+1));
1613 strcpy(pszName, pType->param.szName);
1615 MSG_DEBUG("MmsBinaryEncodeContentTypeLen: szName is not consisted of ascii char-set chars. \n");
1617 /* if msg-server can't support non ascii, then convert non-ascii to '_' by using _MsgReplaceNonAscii*/
1618 int filelen = strlen(pType->param.szName);
1620 pszName = (char *)malloc(filelen + 1);
1621 memset(pszName, 0, (filelen + 1));
1622 strncpy(pszName, pType->param.szName, filelen);
1624 MSG_DEBUG("MmsBinaryEncodeContentType: pszName : %s\n", pszName);
1627 //change empty space to '_' in the file name
1628 if (MsgIsSpace(pszName)) {
1629 char* pszTempName = NULL;
1631 MSG_DEBUG("MmsBinaryEncodeContentType: szName has space(' '). \n");
1633 MsgReplaceSpecialChar(pszName, &pszTempName, ' ');
1637 pszName = pszTempName;
1639 MSG_DEBUG("MmsBinaryEncodeContentType: pszName : %s\n", pszName);
1642 length = __MmsBinaryEncodeTextStringLen((UINT8*)pszName);
1644 MSG_DEBUG("MmsBinaryEncodeContentType: szName MmsBinaryEncodeIntegerLen fail \n");
1648 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = MmsGetBinaryValue(MmsCodeParameterCode, MSG_PARAM_NAME) | 0x80;
1650 if (__MmsBinaryEncodeTextString(pFile, (UINT8*)pszName, length) == false) {
1651 MSG_DEBUG("MmsBinaryEncodeContentType: szName MmsBinaryEncodeTextString fail\n");
1657 #ifdef FEATURE_JAVA_MMS
1658 MSG_DEBUG(" MmsBinaryEncodeContentType: Application-ID \n");
1660 /* Application-ID: Text-string */
1661 if (pType->param.szApplicationID) {
1662 length = __MmsBinaryEncodeTextStringLen((UINT8*) pType->param.szApplicationID);
1664 MSG_DEBUG("MmsBinaryEncodeContentType: szApplicationID MmsBinaryEncodeIntegerLen Fail \n");
1668 fieldValue = MmsGetBinaryValue(MmsCodeParameterCode, MSG_PARAM_APPLICATION_ID);
1670 if (fieldValue == UNDEFINED_BINARY)
1671 szParameter = MmsGetTextValue(MmsCodeParameterCode, MSG_PARAM_APPLICATION_ID);
1673 if (szParameter == NULL) {
1674 MSG_DEBUG("MmsBinaryEncodeContentType: szParameter is NULL \n");
1678 strncpy(gpMmsEncodeBuf + gCurMmsEncodeBuffPos, (char*)szParameter, strlen(szParameter));
1679 gCurMmsEncodeBuffPos += strlen(szParameter);
1680 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] =(UINT8)NULL;
1682 if (__MmsBinaryEncodeTextString(pFile, (UINT8*) pType->param.szApplicationID, length) == false) {
1683 MSG_DEBUG(" MmsBinaryEncodeContentType: szApplicationID MmsBinaryEncodeTextString fail\n");
1689 /* ReplyToApplicationID: Text-string */
1690 if (pType->param.szReplyToApplicationID) {
1691 length = __MmsBinaryEncodeTextStringLen((UINT8*) pType->param.szReplyToApplicationID);
1693 MSG_DEBUG("MmsBinaryEncodeContentType: szReplyToApplicationID MmsBinaryEncodeIntegerLen Fail \n");
1697 fieldValue = MmsGetBinaryValue(MmsCodeParameterCode, MSG_PARAM_REPLY_TO_APPLICATION_ID);
1699 if (fieldValue == UNDEFINED_BINARY)
1700 szParameter = MmsGetTextValue(MmsCodeParameterCode, MSG_PARAM_REPLY_TO_APPLICATION_ID);
1702 if (szParameter == NULL) {
1703 MSG_DEBUG("MmsBinaryEncodeContentType: szParameter is NULL \n");
1707 strncpy(gpMmsEncodeBuf + gCurMmsEncodeBuffPos, (char*)szParameter, strlen(szParameter));
1708 gCurMmsEncodeBuffPos += strlen(szParameter);
1709 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] =(UINT8)NULL;
1711 if (__MmsBinaryEncodeTextString(pFile, (UINT8*) pType->param.szReplyToApplicationID, length) == false) {
1712 MSG_DEBUG(" MmsBinaryEncodeContentType: szApplicationID MmsBinaryEncodeTextString fail\n");
1718 /* Well-known-charset = Any-charset | Integer-value ----------------------- */
1720 if (pType->param.charset != MSG_CHARSET_UNKNOWN) {
1721 fieldValue = MmsGetBinaryValue(MmsCodeCharSet, pType->param.charset);
1722 length = __MmsBinaryEncodeIntegerLen(fieldValue);
1724 MSG_DEBUG("MmsBinaryEncodeContentType: charSet MmsBinaryEncodeIntegerLen fail \n");
1728 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = MmsGetBinaryValue(MmsCodeParameterCode, MSG_PARAM_CHARSET) | 0x80;
1729 if (__MmsBinaryEncodeInteger(pFile, fieldValue, length) == false) {
1730 MSG_DEBUG("MmsBinaryEncodeContentType: charSet MmsBinaryEncodeInteger fail\n");
1735 if (MmsIsText(contentType)) {
1736 if (!MmsIsVitemContent (contentType, pType->param.szName)) {
1737 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = MmsGetBinaryValue(MmsCodeParameterCode, MSG_PARAM_CHARSET) | 0x80;
1738 fieldValue = 0x0000; //laconic_warning, just to remove warning message
1739 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = (UINT8)fieldValue | 0x80;
1744 /* type, start, & startInfo : multipart/related only parameters -------------- */
1745 if (contentType == MIME_MULTIPART_RELATED || contentType == MIME_APPLICATION_VND_WAP_MULTIPART_RELATED) {
1746 /* type ------------------------------------- */
1747 fieldValue = MmsGetBinaryValue(MmsCodeContentType, pType->param.type);
1748 if (fieldValue == UNDEFINED_BINARY || fieldValue == 0x0049) {
1749 /* Extension-media type */
1750 szTextType = MmsGetTextValue(MmsCodeContentType, pType->param.type);
1751 if (szTextType == NULL)
1755 if (szTextType == NULL) {
1756 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = (UINT8)fieldValue | 0x80;
1757 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = MmsGetBinaryValue(MmsCodeParameterCode, MSG_PARAM_TYPE) | 0x80;
1759 length = __MmsBinaryEncodeTextStringLen((UINT8*)szTextType);
1761 MSG_DEBUG("MmsBinaryEncodeContentType: type param MmsBinaryEncodeTextStringLen fail \n");
1765 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = MmsGetBinaryValue(MmsCodeParameterCode, MSG_PARAM_TYPE) | 0x80;
1766 if (__MmsBinaryEncodeTextString(pFile, (UINT8*)szTextType, length) == false) {
1767 MSG_DEBUG("MmsBinaryEncodeContentType: type param MmsBinaryEncodeTextString fail \n");
1772 /* start = Text-string ----------------------- */
1773 if (pType->param.szStart[0]) {
1774 /* start = Text-string */
1775 length = __MmsBinaryEncodeTextStringLen((UINT8*)pType->param.szStart);
1777 MSG_DEBUG("MmsBinaryEncodeContentType: szStart MmsBinaryEncodeTextStringLen fail \n");
1781 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = MmsGetBinaryValue(MmsCodeParameterCode,
1782 MSG_PARAM_START) | 0x80;
1783 if (__MmsBinaryEncodeTextString(pFile, (UINT8*)pType->param.szStart, length) == false) {
1784 MSG_DEBUG("MmsBinaryEncodeContentType: szStart MmsBinaryEncodeTextString fail \n");
1789 /* startInfo = Text-string -------------------- */
1790 if (pType->param.szStartInfo[0]) {
1791 /* StartInfo (with multipart/related) = Text-string */
1792 length = __MmsBinaryEncodeTextStringLen((UINT8*)pType->param.szStartInfo);
1794 MSG_DEBUG("MmsBinaryEncodeContentType: szStartInfo MmsBinaryEncodeTextStringLen fail \n");
1798 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = MmsGetBinaryValue(MmsCodeParameterCode, MSG_PARAM_START_INFO) | 0x80;
1799 if (__MmsBinaryEncodeTextString(pFile, (UINT8*)pType->param.szStartInfo, length) == false) {
1800 MSG_DEBUG("MmsBinaryEncodeContentType: szStartInfo MmsBinaryEncodeTextString fail \n");
1809 MSG_DEBUG("## MmsBinaryEncodeContentType: failed");
1813 static bool __MmsBinaryEncodeMsgPart(FILE *pFile, int contentType, MsgType *pType, MsgBody *pBody)
1815 FILE *pFile2 = NULL;
1820 if (pType->offset && pType->size) {
1821 /* header length & body length --------------------------- */
1823 length = __MmsBinaryEncodeUintvarLen(pType->size);
1825 MSG_DEBUG("MmsBinaryEncodeMsgPart: 1. headerLeng MmsBinaryEncodeUintvarLen fail \n");
1829 if (__MmsBinaryEncodeUintvar(pFile, pType->size, length) == false) {
1830 MSG_DEBUG("MmsBinaryEncodeMsgPart: 1. eaderLeng fail \n");
1834 length = __MmsBinaryEncodeUintvarLen(pBody->size);
1836 MSG_DEBUG("MmsBinaryEncodeMsgPart: 1. bodyLeng MmsBinaryEncodeUintvarLen fail \n");
1840 if (__MmsBinaryEncodeUintvar(pFile, pBody->size, length) == false) {
1841 MSG_DEBUG("MmsBinaryEncodeMsgPart: 1. bodyLeng fail \n");
1845 pFile2 = MsgOpenFile(pType->szOrgFilePath, "rb");
1846 if (pFile != NULL) {
1847 pData = (char *)malloc(pType->size);
1851 if (MsgFseek(pFile2, pType->offset, SEEK_SET) < 0) {
1852 MSG_DEBUG("MmsBinaryEncodeMsgPart: MsgFseek fail \n");
1858 if ((nRead = MsgReadFile(pData, sizeof(char), pType->size, pFile2)) == 0) {
1859 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
1860 gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
1861 MSG_DEBUG("MmsBinaryEncodeMsgPart: 1. header MsgWriteDataFromEncodeBuffer fail \n");
1864 pType->offset = __MmsGetEncodeOffset();
1865 if(MsgWriteFile(pData, sizeof(char), nRead, pFile) != (size_t)nRead) {
1866 MSG_DEBUG("MsgWriteFile failed");
1869 gMmsEncodeCurOffset = MsgFtell(pFile);
1870 if(gMmsEncodeCurOffset < 0) {
1871 MSG_DEBUG("MsgFtell returns negative value [%ld]", gMmsEncodeCurOffset);
1876 MsgCloseFile(pFile2);
1881 int contentTypeLen = 0;
1882 int contentHdrLen = 0;
1884 /* header length & body length --------------------------- */
1885 contentTypeLen = __MmsBinaryEncodeContentTypeLen(pType);
1886 length = __MmsBinaryEncodeValueLengthLen(contentTypeLen);
1887 contentHdrLen = __MmsBinaryEncodeContentHeaderLen((MimeType)contentType, pType, true);
1889 if (contentTypeLen == -1 || length == -1 || contentHdrLen == -1) {
1890 MSG_DEBUG("MmsBinaryEncodeMsgPart: headerLeng calculation fail \n");
1894 headerLeng = contentTypeLen + contentHdrLen + length;
1895 length = __MmsBinaryEncodeUintvarLen(headerLeng);
1897 MSG_DEBUG("MmsBinaryEncodeMsgPart: headerLeng MmsBinaryEncodeUintvarLen fail \n");
1901 if (__MmsBinaryEncodeUintvar(pFile, headerLeng, length) == false) {
1902 MSG_DEBUG("MmsBinaryEncodeMsgPart: headerLeng fail \n");
1906 length = __MmsBinaryEncodeUintvarLen(pBody->size);
1908 MSG_DEBUG("MmsBinaryEncodeMsgPart: bodyLeng MmsBinaryEncodeUintvarLen fail \n");
1912 if (__MmsBinaryEncodeUintvar(pFile, pBody->size, length) == false) {
1913 MSG_DEBUG("MmsBinaryEncodeMsgPart: bodyLeng fail \n");
1917 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
1918 gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
1919 MSG_DEBUG("MmsBinaryEncodeMsgPart: 2. header MsgWriteDataFromEncodeBuffer fail \n");
1923 /* content-type & header --------------------------- */
1924 pType->offset = __MmsGetEncodeOffset();
1926 if (__MmsBinaryEncodeContentType(pFile, pType, contentTypeLen) == false) {
1927 MSG_DEBUG("MmsBinaryEncodeMsgPart: MmsBinaryEncodeContentType fail \n");
1931 if (__MmsBinaryEncodeContentHeader(pFile, (MimeType)contentType, pType, true) == false) {
1932 MSG_DEBUG("MmsBinaryEncodeMsgPart: MmsBinaryEncodeContentHeader fail \n");
1936 pType->size = __MmsGetEncodeOffset() - pType->offset;
1939 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos, gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
1940 MSG_DEBUG("MmsBinaryEncodeMsgPart: contentBody MsgWriteDataFromEncodeBuffer fail \n");
1944 /* content-body --------------------------- */
1945 if (__MmsBinaryEncodeContentBody(pFile, pBody) == false) {
1946 MSG_DEBUG("MmsBinaryEncodeMsgPart: __MmsBinaryEncodeContentBody fail \n");
1964 MSG_DEBUG("## MmsBinaryEncodeMsgPart: failed\n");
1966 MsgCloseFile(pFile2);
1973 static int __MmsBinaryEncodeContentHeaderLen(MimeType contentType, MsgType *pType, bool bMultipart)
1976 int totalLength = 0;
1977 const char *szTextValue = NULL;
1980 MSG_DEBUG("MmsBinaryEncodeContentHeaderLen: S T A R T \n\n");
1982 /* content-id ------------------------------------------------- */
1983 if (pType->szContentID[0]) {
1984 if (bMultipart) { //Binary Encoding
1987 /* content-id = Quoted-string */
1988 length = __MmsBinaryEncodeTextStringLen((UINT8*)"Content-ID");
1990 MSG_DEBUG("MmsBinaryEncodeContentHeaderLen: Content-ID MmsBinaryEncodeTextStringLen fail.\n");
1994 totalLength += length;
1997 length = __MmsBinaryEncodeQuotedStringLen((UINT8*)pType->szContentID);
1999 MSG_DEBUG("MmsBinaryEncodeContentHeader: pType->szContentID MmsBinaryEncodeQuotedStringLen fail.\n");
2002 totalLength += length;
2006 if (pType->szContentLocation[0]) {
2007 if (bMultipart) { //Binary Encoding
2010 /* content-location = Quoted-string */
2011 length = __MmsBinaryEncodeTextStringLen((UINT8*)"Content-Location");
2013 MSG_DEBUG("MmsBinaryEncodeContentHeader: Content-Location MmsBinaryEncodeTextStringLen fail.\n");
2017 totalLength += length;
2020 length = __MmsBinaryEncodeTextStringLen((UINT8*)pType->szContentLocation);
2022 MSG_DEBUG("MmsBinaryEncodeContentHeader: pType->szContentLocation MmsBinaryEncodeTextStringLen fail.\n");
2026 totalLength += length;
2030 /* MIME_APPLICATION_VND_WAP_MULTIPART_RELATEDrequires always "inline" */
2032 if (contentType != MIME_APPLICATION_VND_WAP_MULTIPART_RELATED &&
2033 contentType != MIME_MULTIPART_RELATED &&
2034 pType->disposition != INVALID_VALUE) {
2037 * Content-disposition-value = Value-length Disposition *(Parameter)
2038 * Disposition = Form-data | Attachment | Inline | Token-text
2039 * Form-data = <Octet 128> : 0x80
2040 * Attachment = <Octet 129> : 0x81
2041 * Inline = <Octet 130> : 0x82
2044 if (bMultipart) { //Binary Encoding
2047 /* content-disposition = Quoted-string */
2048 szTextValue = MmsGetTextValue(MmsCodeMsgDisposition, pType->disposition);
2051 length = __MmsBinaryEncodeTextStringLen((UINT8*)"Content-Disposition");
2053 MSG_DEBUG("MmsBinaryEncodeContentHeader: Content-Disposition MmsBinaryEncodeTextStringLen fail.\n");
2057 totalLength += length;
2059 length = __MmsBinaryEncodeTextStringLen((UINT8*)szTextValue);
2061 MSG_DEBUG("MmsBinaryEncodeContentHeader: Content-Disposition MmsBinaryEncodeTextStringLen fail.\n");
2064 totalLength += length;
2072 MSG_DEBUG("## MmsBinaryEncodeContentHeadeLen: failed");
2077 static bool __MmsBinaryEncodeContentHeader(FILE *pFile, MimeType contentType, MsgType *pType, bool bMultipart)
2080 const char *szTextValue = NULL;
2082 MSG_DEBUG("MmsBinaryEncodeContentHeader: S T A R T \n\n");
2084 /* content-id ------------------------------------------------- */
2085 if (pType->szContentID[0]) {
2086 if (bMultipart) { //Binary Encoding
2087 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = MmsGetBinaryValue(MmsCodeMsgBodyHeaderCode, MMS_BODYHDR_CONTENTID) | 0x80;
2089 /* content-id = Quoted-string */
2090 length = __MmsBinaryEncodeTextStringLen((UINT8*)"Content-ID");
2092 MSG_DEBUG("MmsBinaryEncodeContentHeader: Content-ID MmsBinaryEncodeTextStringLen fail.\n");
2096 if (__MmsBinaryEncodeTextString(pFile, (UINT8*)"Content-ID", length) == false) {
2097 MSG_DEBUG("MmsBinaryEncodeContentHeader: Content-ID MmsBinaryEncodeTextString fail.\n");
2102 length = __MmsBinaryEncodeQuotedStringLen((UINT8*)pType->szContentID);
2104 MSG_DEBUG("MmsBinaryEncodeContentHeader: pType->szContentID MmsBinaryEncodeQuotedStringLen fail.\n");
2108 if (__MmsBinaryEncodeQuotedString(pFile, (UINT8*)pType->szContentID, length) == false) {
2109 MSG_DEBUG("MmsBinaryEncodeContentHeader: pType->szContentID MmsBinaryEncodeQuotedString fail.\n");
2114 if (pType->szContentLocation[0]) {
2115 if (bMultipart) { //Binary Encoding
2116 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = MmsGetBinaryValue(MmsCodeMsgBodyHeaderCode, MMS_BODYHDR_CONTENTLOCATION) | 0x80;
2118 /* content-location = Quoted-string */
2119 length = __MmsBinaryEncodeTextStringLen((UINT8*)"Content-Location");
2121 MSG_DEBUG("MmsBinaryEncodeContentHeader: Content-Location MmsBinaryEncodeTextStringLen fail.\n");
2125 if (__MmsBinaryEncodeTextString(pFile, (UINT8*)"Content-Location", length) == false) {
2126 MSG_DEBUG("MmsBinaryEncodeContentHeader: Content-Location MmsBinaryEncodeTextString fail.\n");
2131 length = __MmsBinaryEncodeTextStringLen((UINT8*)pType->szContentLocation);
2133 MSG_DEBUG("MmsBinaryEncodeContentHeader: pType->szContentLocation MmsBinaryEncodeTextStringLen fail.\n");
2137 if (__MmsBinaryEncodeTextString(pFile, (UINT8*)pType->szContentLocation, length) == false) {
2138 MSG_DEBUG("MmsBinaryEncodeContentHeader: pType->szContentLocation MmsBinaryEncodeTextString fail.\n");
2143 /* MIME_APPLICATION_VND_WAP_MULTIPART_RELATEDrequires always "inline" */
2145 if (contentType != MIME_APPLICATION_VND_WAP_MULTIPART_RELATED &&
2146 contentType != MIME_MULTIPART_RELATED &&
2147 pType->disposition != INVALID_VALUE) {
2150 * Content-disposition-value = Value-length Disposition *(Parameter)
2151 * Disposition = Form-data | Attachment | Inline | Token-text
2152 * Form-data = <Octet 128> : 0x80
2153 * Attachment = <Octet 129> : 0x81
2154 * Inline = <Octet 130> : 0x82
2157 if (bMultipart) {//Binary Encoding
2159 UINT8 fieldValue = 0xff;
2160 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = MmsGetBinaryValue(MmsCodeMsgBodyHeaderCode, MMS_BODYHDR_DISPOSITION) | 0x80;
2162 fieldValue = MmsGetBinaryValue(MmsCodeMsgDisposition, pType->disposition) | 0x80;
2164 if (fieldValue == 0xff)
2167 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = 0x01;
2168 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = fieldValue;
2170 /* content-disposition = Disposition (no support parameter) */
2172 szTextValue = MmsGetTextValue(MmsCodeMsgDisposition, pType->disposition);
2175 length = __MmsBinaryEncodeTextStringLen((UINT8*)"Content-Disposition");
2177 MSG_DEBUG("MmsBinaryEncodeContentHeader: Content-Disposition MmsBinaryEncodeTextStringLen fail.\n");
2181 if (__MmsBinaryEncodeTextString(pFile, (UINT8*)"Content-Disposition", length) == false) {
2182 MSG_DEBUG("MmsBinaryEncodeContentHeader: Content-Disposition MmsBinaryEncodeTextString fail.\n");
2186 length = __MmsBinaryEncodeTextStringLen((UINT8*)szTextValue);
2188 MSG_DEBUG("MmsBinaryEncodeContentHeader: Content-Disposition MmsBinaryEncodeTextStringLen fail.\n");
2192 if (__MmsBinaryEncodeTextString(pFile, (UINT8*)szTextValue, length) == false) {
2193 MSG_DEBUG("MmsBinaryEncodeContentHeader: Content-Disposition MmsBinaryEncodeTextString fail.\n");
2203 MSG_DEBUG("## MmsBinaryEncodeContentHeader: failed");
2207 static bool __MmsBinaryEncodeContentBody(FILE *pFile, MsgBody *pBody)
2219 if (pBody->szOrgFilePath[0]) {
2220 pData = MsgOpenAndReadMmsFile(pBody->szOrgFilePath, pBody->offset, pBody->size, &nRead);
2224 pBody->offset = __MmsGetEncodeOffset();
2225 if(MsgWriteFile(pData, sizeof(char), nRead, pFile) != (size_t)nRead) {
2226 MSG_DEBUG("MsgWriteFile failed");
2229 gMmsEncodeCurOffset = MsgFtell(pFile);
2231 if (gMmsEncodeCurOffset < 0) {
2232 MSG_DEBUG("MsgFtell returns negative value [%ld]", gMmsEncodeCurOffset);
2240 } else if (pBody->body.pText && pBody->size) {
2241 pBody->offset = __MmsGetEncodeOffset();
2242 if (MsgWriteFile(pBody->body.pText, sizeof(char),(size_t)pBody->size, pFile) != (size_t)pBody->size) {
2243 MSG_DEBUG("MsgWriteFile failed");
2246 gMmsEncodeCurOffset = MsgFtell(pFile);
2248 if(gMmsEncodeCurOffset < 0) {
2249 MSG_DEBUG("MsgFtell returns negative value [%ld]", gMmsEncodeCurOffset);
2257 MSG_DEBUG("## __MmsBinaryEncodeContentBody: failed\n");
2266 /* =========================================================================
2268 B I N A R Y E N C O D I N G U T I L I T Y
2270 =========================================================================*/
2271 static int __MmsBinaryEncodeIntegerLen(UINT32 integer)
2273 if (integer < 0x80) {
2280 * Long-integer = Short-length Multi-octet-integer
2281 * The Short-length indicates the length of the Multi-octet-integer
2286 integer = (integer >> 8);
2289 length++; // + Short-length
2297 * This makes value-length by specified integer value
2299 * @param length [in] gotten from MmsBinaryEncodeIntegerLen()
2301 static bool __MmsBinaryEncodeInteger(FILE *pFile, UINT32 integer, int length)
2309 if (pFile == NULL) {
2310 MSG_DEBUG("MmsBinaryEncodeInteger: source == NULL \n");
2314 if ((gMmsEncodeMaxLen - gCurMmsEncodeBuffPos) < length) {
2315 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
2316 gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
2317 MSG_DEBUG("MmsBinaryEncodeInteger: MsgWriteDataFromEncodeBuffer fail \n");
2322 if (integer < 0x80) {
2324 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = (UINT8)integer | 0x80;
2327 * Long-integer = Short-length Multi-octet-integer
2328 * The Short-length indicates the length of the Multi-octet-integer
2330 changer.integer = integer;
2331 length--; // - "Short-length"
2333 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = (UINT8)length;
2336 for(i = 0; i < length; i++)
2337 gpMmsEncodeBuf[gCurMmsEncodeBuffPos + i] = changer.seg[length - (i + 1)];
2339 gCurMmsEncodeBuffPos += length;
2348 static int __MmsBinaryEncodeLongIntegerLen(UINT32 integer)
2353 * Long-integer = Short-length Multi-octet-integer
2354 * The Short-length indicates the length of the Multi-octet-integer
2362 integer = (integer >> 8);
2365 length++; // + Short-length
2371 * This makes value-length by specified integer value
2373 * @param length [in] gotten from MmsBinaryEncodeIntegerLen()
2375 static bool __MmsBinaryEncodeLongInteger(FILE *pFile, UINT32 integer, int length)
2385 * Long-integer = Short-length Multi-octet-integer
2386 * The Short-length indicates the length of the Multi-octet-integer
2389 if (pFile == NULL) {
2390 MSG_DEBUG("MmsBinaryEncodeLongInteger: source == NULL \n");
2394 if ((gMmsEncodeMaxLen - gCurMmsEncodeBuffPos) < length) {
2395 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
2396 gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
2397 MSG_DEBUG("MmsBinaryEncodeLongInteger: MsgWriteDataFromEncodeBuffer fail \n");
2402 changer.integer = integer;
2403 length--; // - "Short-length"
2405 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = (UINT8)length;
2407 for(i = 0; i < length; i++)
2408 gpMmsEncodeBuf[gCurMmsEncodeBuffPos + i] = changer.seg[length - (i + 1)];
2410 gCurMmsEncodeBuffPos += length;
2418 static int __MmsBinaryEncodeTextStringLen(UINT8 *source)
2422 MSG_DEBUG("MmsBinaryEncodeTextStringLen: \n");
2424 if (source == NULL) {
2425 MSG_DEBUG("MmsBinaryEncodeTextStringLen: source == NULL \n");
2429 length = (int)strlen((char*)source);
2430 if (source[0] > 0x7F) {
2431 length += 2; // + NULL
2440 * @param source [in] originam string
2441 * @param length [in] gotten from MmsBinaryEncodeTextStringLen()
2442 * @param dest [in] buffer to store quted string
2443 * @return changed string length
2445 static bool __MmsBinaryEncodeTextString(FILE *pFile, UINT8 *source, int length)
2448 MSG_DEBUG("MmsBinaryEncodeTextString: \n");
2452 * Text-string = [Quote] *TEXT End-of-string
2453 * If the 1st char in the TEXT is in the range of 128-255, a Quote char must precede it.
2454 * Otherwise the Quote char must be omitted.
2455 * The Quote is not part of the contents.
2456 * Quote = <Octet 127>
2459 if (pFile == NULL || source == NULL) {
2460 MSG_DEBUG("MmsBinaryEncodeTextString: source == NULL \n");
2464 if ((gMmsEncodeMaxLen - gCurMmsEncodeBuffPos) < length) {
2465 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
2466 gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
2467 MSG_DEBUG("MmsBinaryEncodeTextString: MsgWriteDataFromEncodeBuffer fail \n");
2472 if (source[0] > 0x7F) {
2473 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = QUOTE;
2477 strncpy(gpMmsEncodeBuf + gCurMmsEncodeBuffPos, (char*)source, (length - 1)); // except NULL
2478 gCurMmsEncodeBuffPos += (length - 1); // except NULL
2479 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = (UINT8)NULL;
2488 * Encode 28bit unsigned integer(Maximum) to uintvar
2490 * @param interger [in] integer to be encoded
2491 * @return encoded UINTVAR stream
2493 const UINT32 UINTVAR_LENGTH_1 = 0x0000007f; //7bit
2494 const UINT32 UINTVAR_LENGTH_2 = 0x00003fff; //14bit
2495 const UINT32 UINTVAR_LENGTH_3 = 0x001fffff; //21bit
2498 static int __MmsBinaryEncodeUintvarLen(UINT32 integer)
2502 /* Find encoded unitvar length */
2503 if (integer <= UINTVAR_LENGTH_1) {
2506 if (integer <= UINTVAR_LENGTH_2) {
2509 if (integer <= UINTVAR_LENGTH_3) {
2520 static bool __MmsBinaryEncodeUintvar(FILE *pFile, UINT32 integer, int length)
2522 const char ZERO = 0x00;
2524 char szReverse[MSG_STDSTR_LONG] = {0, };
2531 if (pFile == NULL) {
2532 MSG_DEBUG("MmsBinaryEncodeUintvar: pFile == INVALID_HBOJ \n");
2536 if ((gMmsEncodeMaxLen - gCurMmsEncodeBuffPos) < length) {
2537 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
2538 gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
2539 MSG_DEBUG("MmsBinaryEncodeUintvar: MsgWriteDataFromEncodeBuffer fail \n");
2544 source.integer = integer;
2545 memset(szReverse, 0, MSG_STDSTR_LONG);
2547 /* Seperate integer to 4 1 byte integer */
2548 szReverse[3] = source.bytes[3] & 0x0f;
2549 szReverse[0] = source.bytes[0];
2550 szReverse[0] = szReverse[0] & 0x7f;
2552 while (length >= i) { // initially, i = 2
2553 /* Move integer bit to proper position */
2554 source.integer = source.integer << 1;
2555 source.integer = source.integer >> 8;
2556 source.bytes[3] = ZERO;
2558 /* Retrive 1 encode uintvar */
2559 szReverse[i-1] = source.bytes[0];
2560 szReverse[i-1] = szReverse[i-1] | 0x80;
2564 for(i=0; i < length; i++)
2565 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = szReverse[length - i - 1];
2573 static int __MmsBinaryEncodeValueLengthLen(UINT32 integer)
2577 if (integer < 0x1f) {
2580 length = __MmsBinaryEncodeUintvarLen(integer) + 1; //LENGTH_QUOTE
2587 * This makes value-length by specified integer value
2589 * @param length [in] from MmsBinaryEncodeValueLengthLen()
2590 * @return encoded value-length
2592 static bool __MmsBinaryEncodeValueLength(FILE *pFile, UINT32 integer, int length)
2595 * Value-length = Short-length | (Length-quote Length)
2596 * ; Value length is used to indicate the length of the value to follow
2597 * Short-length = <Any octet 0-30>
2598 * Length-quote = <Octet 31>
2599 * Length = Uintvar-integer
2602 if (pFile == NULL) {
2603 MSG_DEBUG("MmsBinaryEncodeValueLength: pFile == INVALID_HBOJ \n");
2607 if ((gMmsEncodeMaxLen - gCurMmsEncodeBuffPos) < length) {
2608 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
2609 gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
2610 MSG_DEBUG("MmsBinaryEncodeValueLength: MsgWriteDataFromEncodeBuffer fail \n");
2615 if (integer < 0x1F) {
2616 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = (UINT8)integer;
2618 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = (UINT8)LENGTH_QUOTE;
2619 if (__MmsBinaryEncodeUintvar(pFile, integer, length - 1) == false) { // LENGTH_QUOTE
2620 MSG_DEBUG("MmsBinaryEncodeValueLength: MmsBinaryEncodeUintvar fail\n");
2631 static int __MmsBinaryEncodeQuotedStringLen(UINT8 *pSrc)
2634 MSG_DEBUG("MmsBinaryEncodeQuotedStringLen: invalid file\n");
2638 return (strlen((char*)pSrc) + 2); // QUOTE + NULL
2645 * make quoted string
2646 * Quoted-string = <Octet 34> *TEXT End-of-string
2648 * @param source [in] original string
2649 * @param length [in] length (in bytes) of data
2650 * @param dest [out] buffer to store quted string
2651 * @return changed string length
2653 static bool __MmsBinaryEncodeQuotedString(FILE *pFile, UINT8 *source, int length)
2655 if (source == NULL || pFile == NULL) {
2656 MSG_DEBUG("MmsBinaryEncodeQuotedString: invalid file\n");
2660 if ((gMmsEncodeMaxLen - gCurMmsEncodeBuffPos) < length) {
2661 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
2662 gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
2663 MSG_DEBUG("MmsBinaryEncodeQuotedString: MsgWriteDataFromEncodeBuffer fail \n");
2668 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = '\"';
2669 strncpy(gpMmsEncodeBuf + gCurMmsEncodeBuffPos, (char*)source, length - 2); // except '\"' & NULL
2670 gCurMmsEncodeBuffPos += (length - 2);
2671 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = (UINT8)NULL;
2679 static int __MmsBinaryEncodeEncodedStringLen(UINT8 *source)
2681 UINT32 charset = 0x6A; // default = utf-8
2684 int valueLengthLen = 0;
2687 MSG_DEBUG("MmsBinaryEncodeEncodedStringLen: \n");
2689 /* value-length charSet text-string */
2690 /* Estimate charset value length and text string length */
2691 charLeng = __MmsBinaryEncodeIntegerLen(charset);
2692 if (charLeng == -1) {
2693 MSG_DEBUG("MmsBinaryEncodeEncodedStringLen : charLeng MmsBinaryEncodeTextStringLen fail.\n");
2697 textLeng = __MmsBinaryEncodeTextStringLen((UINT8*)source);
2698 if (textLeng == -1) {
2699 MSG_DEBUG("MmsBinaryEncodeEncodedStringLen : textLeng MmsBinaryEncodeTextStringLen fail.\n");
2703 valueLengthLen = __MmsBinaryEncodeValueLengthLen(charLeng + textLeng);
2704 if (valueLengthLen == -1) {
2705 MSG_DEBUG("MmsBinaryEncodeEncodedStringLen : valLengthLen MmsBinaryEncodeTextStringLen fail.\n");
2709 return (charLeng + textLeng + valueLengthLen);
2716 * This makes value-length by specified integer value
2718 * @param length [in] from MmsBinaryEncodeEncodedStringLen()
2719 * @return encoded encoded-string
2721 static bool __MmsBinaryEncodeEncodedString(FILE *pFile, UINT8 *source, int length)
2723 UINT32 charset = 0x6A; // default = utf-8
2726 int valLengthLen = 0;
2729 MSG_DEBUG("MmsBinaryEncodeEncodedString: \n");
2731 /* value-length charSet text-string */
2733 if (pFile == NULL || source == NULL) {
2734 MSG_DEBUG("MmsBinaryEncodeEncodedString: invalid input parameter\n");
2738 /* Estimate charset value length and text string length */
2739 charLeng = __MmsBinaryEncodeIntegerLen(charset);
2740 if (charLeng == -1) {
2741 MSG_DEBUG("MmsBinaryEncodeEncodedString : charLeng MmsBinaryEncodeTextStringLen fail.\n");
2745 textLeng = __MmsBinaryEncodeTextStringLen((UINT8*)source);
2746 if (textLeng == -1) {
2747 MSG_DEBUG("MmsBinaryEncodeEncodedString : textLeng MmsBinaryEncodeTextStringLen fail.\n");
2751 valLengthLen = __MmsBinaryEncodeValueLengthLen(charLeng + textLeng);
2752 if (valLengthLen == -1) {
2753 MSG_DEBUG("MmsBinaryEncodeEncodedString : MmsBinaryEncodeValueLengthLen fail.\n");
2757 if (length != (charLeng + textLeng + valLengthLen))
2759 MSG_DEBUG("MmsBinaryEncodeEncodedString: invalid length\n");
2763 if ((gMmsEncodeMaxLen - gCurMmsEncodeBuffPos) < length) {
2764 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
2765 gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
2766 MSG_DEBUG("MmsBinaryEncodeEncodedString: MsgWriteDataFromEncodeBuffer fail \n");
2771 /* Value length of charset value and text string */
2772 if (__MmsBinaryEncodeValueLength(pFile, charLeng + textLeng, valLengthLen) == false) {
2773 MSG_DEBUG("MmsBinaryEncodeEncodedString : MmsBinaryEncodeValueLength fail.\n");
2777 /* fixme: Write charset on buffer -> integer value not long-integer */
2778 if (__MmsBinaryEncodeInteger(pFile, charset, charLeng) == false) {
2779 MSG_DEBUG("MmsBinaryEncodeEncodedString : MmsBinaryEncodeInteger fail.\n");
2784 /* Write text string on buffer */
2785 if (__MmsBinaryEncodeTextString(pFile, (UINT8*)source, textLeng) == false) {
2786 MSG_DEBUG("MmsBinaryEncodeEncodedString : MmsBinaryEncodeTextString fail.\n");
2796 static bool __MmsBinaryEncodeFieldCodeAndValue(FILE *pFile, UINT8 fieldCode, UINT8 fieldValue)
2798 if ((gMmsEncodeMaxLen - gCurMmsEncodeBuffPos) < 2) {
2799 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
2800 gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
2801 MSG_DEBUG("__MmsBinaryEncodeFieldCodeAndValue: MsgWriteDataFromEncodeBuffer fail \n");
2806 if (fieldCode == 0xff) {
2807 MSG_DEBUG("__MmsBinaryEncodeFieldCodeAndValue: invalid fieldCode \n");
2811 if (fieldValue == 0xff) {
2812 MSG_DEBUG("__MmsBinaryEncodeFieldCodeAndValue: invalid fieldValue \n");
2816 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = fieldCode;
2817 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = fieldValue;
2825 static bool __MmsBinaryEncodeTrID(FILE *pFile, char *szTrID, int bufLen)
2828 UINT8 fieldCode = 0xff;
2829 char szBuff[MMS_TR_ID_LEN + 1] = {0, };
2830 struct tm *dateTime = NULL;
2835 dateTime = localtime(&RawTime);
2836 dateSec = mktime(dateTime);
2839 fieldCode = MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_TRID) | 0x80;
2840 if (fieldCode == 0xff) {
2841 MSG_DEBUG("__MmsBinaryEncodeTrID: invalid fieldCode \n");
2845 snprintf(szBuff, MMS_TR_ID_LEN + 1, "%lu.%lu", dateSec, (unsigned long)random());
2846 MSG_DEBUG("__MmsBinaryEncodeTrID: 2. szBuff = %s\n", szBuff);
2848 length = __MmsBinaryEncodeTextStringLen((UINT8*)szBuff);
2850 MSG_DEBUG("__MmsBinaryEncodeTrID: MmsBinaryEncodeTextStringLen fail \n");
2854 if ((gMmsEncodeMaxLen - gCurMmsEncodeBuffPos) < (length + 1)) {
2855 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
2856 gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
2857 MSG_DEBUG("__MmsBinaryEncodeTrID: MsgWriteDataFromEncodeBuffer fail \n");
2863 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = fieldCode;
2864 if (__MmsBinaryEncodeTextString(pFile, (UINT8*)szBuff, length) == false) {
2865 MSG_DEBUG("__MmsBinaryEncodeTrID: MmsBinaryEncodeTextString fail\n");
2870 memset(szTrID, 0, bufLen);
2871 strncpy(szTrID, szBuff, bufLen - 1);
2881 static bool __MmsBinaryEncodeMsgID(FILE *pFile, const char *szMsgID)
2884 UINT8 fieldCode = 0xff;
2886 fieldCode = MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_MSGID) | 0x80;
2887 if (fieldCode == 0xff) {
2888 MSG_DEBUG("__MmsBinaryEncodeTrID: invalid fieldCode \n");
2892 MSG_DEBUG("__MmsBinaryEncodeMsgID: 2. szBuff = %s\n", szMsgID);
2894 length = __MmsBinaryEncodeTextStringLen((UINT8*)szMsgID);
2896 MSG_DEBUG("__MmsBinaryEncodeMsgID: MmsBinaryEncodeTextStringLen fail \n");
2900 if ((gMmsEncodeMaxLen - gCurMmsEncodeBuffPos) < (length + 1)) {
2901 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
2902 gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
2903 MSG_DEBUG("__MmsBinaryEncodeTrID: MsgWriteDataFromEncodeBuffer fail \n");
2908 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = fieldCode;
2909 if (__MmsBinaryEncodeTextString(pFile, (UINT8*)szMsgID, length) == false) {
2910 MSG_DEBUG("__MmsBinaryEncodeTrID: MmsBinaryEncodeTextString fail\n");
2921 static bool __MmsBinaryEncodeMmsVersion(FILE *pFile)
2923 UINT8 majorVer = MMS_MAJOR_VERSION;
2924 UINT8 minorVer = MMS_MINOR_VERSION;
2926 MSG_DEBUG("__MmsBinaryEncodeMmsVersion: \n");
2928 if (pFile == NULL) {
2929 MSG_DEBUG("__MmsBinaryEncodeMmsVersion: invalid input file \n");
2933 if ((gMmsEncodeMaxLen - gCurMmsEncodeBuffPos) < 2) {
2934 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
2935 gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
2936 MSG_DEBUG("__MmsBinaryEncodeMmsVersion: MsgWriteDataFromEncodeBuffer fail \n");
2941 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_VERSION) | 0x80;
2943 MSG_DEBUG("__MmsBinaryEncodeMmsVersion: major version (%d)\n", majorVer);
2944 MSG_DEBUG("__MmsBinaryEncodeMmsVersion: minor version (%d)\n", minorVer);
2946 gpMmsEncodeBuf[gCurMmsEncodeBuffPos] = (majorVer << 4) | (minorVer & 0x0f) | MSB;
2948 if (gpMmsEncodeBuf[gCurMmsEncodeBuffPos] < 0x80) {
2949 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] |= 0x80;
2951 gCurMmsEncodeBuffPos++;
2960 static bool __MmsBinaryEncodeDate(FILE *pFile)
2962 struct tm *dateTime = NULL;
2965 dateSec = time(NULL);
2966 dateTime = localtime(&dateSec);
2968 MSG_DEBUG("%d - %d - %d, %d : %d (SYSTEM)", dateTime->tm_year + 1900, dateTime->tm_mon + 1, dateTime->tm_mday
2969 , dateTime->tm_hour, dateTime->tm_min);
2973 length = __MmsBinaryEncodeLongIntegerLen(dateSec);
2975 if ((gMmsEncodeMaxLen - gCurMmsEncodeBuffPos) < (length + 1)) { // + fieldCode
2976 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
2977 gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
2978 MSG_DEBUG("__MmsBinaryEncodeDate: MsgWriteDataFromEncodeBuffer fail \n");
2983 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_DATE) | 0x80;
2985 if (__MmsBinaryEncodeLongInteger(pFile, dateSec, length) == false) {
2986 MSG_DEBUG("__MmsBinaryEncodeDate: date MmsBinaryEncodeLongInteger error\n");
2990 MSG_DEBUG("__MmsBinaryEncodeDate: date has a negative value (%d) \n", dateSec);
2999 static bool __MmsBinaryEncodeFrom(FILE *pFile)
3001 if (pFile == NULL) {
3002 MSG_DEBUG("__MmsBinaryEncodeFrom: invalid input file \n");
3006 if ((gMmsEncodeMaxLen - gCurMmsEncodeBuffPos) < 3) {
3007 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
3008 gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
3009 MSG_DEBUG("__MmsBinaryEncodeFrom: MsgWriteDataFromEncodeBuffer fail \n");
3014 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_FROM) | 0x80;
3015 /* length of MMS_INSERT_ADDRESS_TOKEN value */
3016 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = 0x01;
3017 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = MmsGetBinaryValue(MmsCodeAddressType, MMS_INSERT_ADDRESS_TOKEN) | 0x80;
3024 static bool __MmsBinaryEncodeOneAddress(FILE *pFile, MmsFieldCode addrType, char *szAddrStr)
3028 if (pFile == NULL) {
3029 MSG_DEBUG("__MmsBinaryEncodeOneAddress: invalid input file \n");
3034 length = __MmsBinaryEncodeEncodedStringLen((UINT8*)szAddrStr);
3037 MSG_DEBUG("__MmsBinaryEncodeOneAddress: MmsBinaryEncodeEncodedStringLen fail \n");
3041 if ((gMmsEncodeMaxLen - gCurMmsEncodeBuffPos) < length + 1) {
3042 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
3043 gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
3044 MSG_DEBUG("__MmsBinaryEncodeOneAddress: MsgWriteDataFromEncodeBuffer fail \n");
3049 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = MmsGetBinaryValue(MmsCodeFieldCode, addrType) | 0x80;
3052 if (__MmsBinaryEncodeEncodedString(pFile, (UINT8*)szAddrStr, length) == false) {
3053 MSG_DEBUG("__MmsBinaryEncodeOneAddress: MmsBinaryEncodeEncodedString fail \n");
3063 static bool __MmsBinaryEncodeAddress(FILE *pFile, MmsFieldCode addrType, char *szAddr)
3065 char *pSingleAddr = NULL;
3068 if (pFile == NULL) {
3069 MSG_DEBUG("__MmsBinaryEncodeAddress: invalid input file \n");
3073 while (szAddr && szAddr[0]) {
3074 szAddr = _MsgSkipWS3(szAddr);
3078 pSingleAddr = strchr(szAddr, MSG_CH_SEMICOLON);
3081 *pSingleAddr = MSG_CH_NULL;
3082 if (__MmsBinaryEncodeOneAddress(pFile, addrType, szAddr) == false) {
3083 MSG_DEBUG("__MmsBinaryEncodeAddress: __MmsBinaryEncodeAddress fail\n");
3086 *pSingleAddr = MSG_CH_SEMICOLON;
3088 szAddr = pSingleAddr + 1;
3091 if (__MmsBinaryEncodeOneAddress(pFile, addrType, szAddr) == false) {
3092 MSG_DEBUG("__MmsBinaryEncodeAddress: __MmsBinaryEncodeAddress fail\n");
3106 static bool __MmsBinaryEncodeTime(FILE *pFile, MmsFieldCode fieldCode, MmsTimeStruct time)
3111 if (pFile == NULL) {
3112 MSG_DEBUG("__MmsBinaryEncodeTime: invalid input file \n");
3116 if (time.time == 0 ||
3117 (fieldCode != MMS_CODE_EXPIRYTIME && fieldCode != MMS_CODE_DELIVERYTIME && fieldCode != MMS_CODE_REPLYCHARGINGDEADLINE) ||
3118 (time.type != MMS_TIMETYPE_ABSOLUTE && time.type != MMS_TIMETYPE_RELATIVE)) {
3119 MSG_DEBUG("__MmsBinaryEncodeTime: time.type = %d \n", time.type);
3124 * use temporary buffer to estimate value length
3125 * and copy it to pData buffer later.
3128 if (time.type == MMS_TIMETYPE_RELATIVE) {
3129 timeLen = __MmsBinaryEncodeIntegerLen(time.time);
3131 timeLen = __MmsBinaryEncodeLongIntegerLen(time.time);
3135 MSG_DEBUG("__MmsBinaryEncodeTime: MmsBinaryEncodeLongIntegerLen fail \n");
3139 length = __MmsBinaryEncodeValueLengthLen(timeLen + 1); //time length + time type token
3141 MSG_DEBUG("__MmsBinaryEncodeTime: MmsBinaryEncodeValueLengthLen fail \n");
3145 if ((gMmsEncodeMaxLen - gCurMmsEncodeBuffPos) < (length + timeLen + 2)) { // + fieldCode + timeType
3147 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
3148 gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
3149 MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: MsgWriteDataFromEncodeBuffer fail \n");
3155 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = MmsGetBinaryValue(MmsCodeFieldCode, fieldCode) | 0x80;
3158 if (__MmsBinaryEncodeValueLength(pFile, timeLen + 1, length) == false) {
3159 MSG_DEBUG("__MmsBinaryEncodeTime: MmsBinaryEncodeValueLength fail \n");
3163 /* time type & value */
3164 if (time.type == MMS_TIMETYPE_RELATIVE) {
3165 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = MmsGetBinaryValue(MmsCodeTimeType, MMS_TIMETYPE_RELATIVE) | 0x80;
3166 if (__MmsBinaryEncodeInteger(pFile, time.time, timeLen) == false) {
3167 MSG_DEBUG("__MmsBinaryEncodeTime: MmsBinaryEncodeLongInteger fail \n");
3171 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = MmsGetBinaryValue(MmsCodeTimeType, MMS_TIMETYPE_ABSOLUTE) | 0x80;
3172 if (__MmsBinaryEncodeLongInteger(pFile, time.time, timeLen) == false) {
3173 MSG_DEBUG("__MmsBinaryEncodeTime: MmsBinaryEncodeLongInteger fail \n");