Modify flora license version.
[platform/core/messaging/msg-service.git] / plugin / mms_plugin / MmsPluginEncode.cpp
1 /*
2 * Copyright 2012-2013  Samsung Electronics Co., Ltd
3 *
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
7 *
8 *    http://floralicense.org/license/
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <time.h>
18 #include <stdlib.h>
19 #include <stdio.h>
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"
27
28
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;
35
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;
42
43 static int __MmsGetEncodeOffset(void);
44 static void __MmsRegisterEncodeBuffer2(char *pInBuff, int maxLen);
45 static void __MmsUnregisterEncodeBuffer2(void);
46
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);
61
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);
66
67 static bool __MmsBinaryEncodeContentBody(FILE *pFile, MsgBody *pBody);
68 static bool __MmsBinaryEncodeMsgPart(FILE *pFile, int contentType, MsgType *pType, MsgBody *pBody);
69
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);
78
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);
85
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);
88
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);
93
94 /** -----------------------------------------------------------------
95  *                                      M   M   S       E   N   C   O   D   E
96  * * -----------------------------------------------------------------*/
97
98 static  void __MmsCleanEncodeBuff(void)
99 {
100         memset(gpMmsEncodeBuf, 0, MSG_MMS_ENCODE_BUFFER_MAX);
101         gCurMmsEncodeBuffPos = 0;
102 }
103
104
105 void MmsRegisterEncodeBuffer(char *pInBuff, int maxLen)
106 {
107         gpMmsEncodeBuf = pInBuff;
108         gCurMmsEncodeBuffPos = 0;
109         gMmsEncodeMaxLen = maxLen;
110         gMmsEncodeCurOffset = 0;
111 }
112
113 void MmsUnregisterEncodeBuffer(void)
114 {
115         gpMmsEncodeBuf = NULL;
116         gCurMmsEncodeBuffPos = 0;
117         gMmsEncodeMaxLen = 0;
118         gMmsEncodeCurOffset = 0;
119 }
120
121 static int __MmsGetEncodeOffset(void)
122 {
123         return (gMmsEncodeCurOffset + gCurMmsEncodeBuffPos);
124 }
125
126 bool MmsEncodeSendReq(FILE *pFile, MmsMsg *pMsg)
127 {
128         return __MmsEncodeSendReq(pFile, pMsg, true);
129 }
130
131 bool MmsEncodeTemplate(FILE *pFile, MmsMsg *pMsg)
132 {
133         return __MmsEncodeSendReq(pFile, pMsg, false);
134 }
135
136 static bool __MmsEncodeSendReq(FILE* pFile, MmsMsg* pMsg, bool bIncludeSendReqHeader)
137 {
138         MmsRegisterEncodeBuffer(gszMmsEncodeBuf, MSG_MMS_ENCODE_BUFFER_MAX);
139
140         if (bIncludeSendReqHeader) {
141                 if (__MmsBinaryEncodeSendReqHdr(pFile, pMsg) == false) {
142                         MmsUnregisterEncodeBuffer();
143                         return false;
144                 }
145         }
146
147         if (__MmsBinaryEncodeMsgBody(pFile, &pMsg->msgType, &pMsg->msgBody, pMsg->nPartCount, !bIncludeSendReqHeader) == false) {
148                 MmsUnregisterEncodeBuffer();
149                 return false;
150         }
151
152         MmsUnregisterEncodeBuffer();
153
154         return true;
155 }
156
157 /* Functions for Acknowledge.ind & NotifyResp.ind ------------------------ */
158
159 static  void __MmsCleanEncodeBuff2(void)
160 {
161         memset(gpMmsEncodeBuf2, 0, MSG_MMS_ENCODE_BUFFER_MAX);
162         gCurMmsEncodeBuffPos2 = 0;
163 }
164
165 static void __MmsRegisterEncodeBuffer2(char *pInBuff, int maxLen)
166 {
167         gpMmsEncodeBuf2 = pInBuff;
168         gCurMmsEncodeBuffPos2 = 0;
169         gMmsEncodeMaxLen2 = maxLen;
170         gMmsEncodeCurOffset2 = 0;
171 }
172
173 static void __MmsUnregisterEncodeBuffer2(void)
174 {
175         gpMmsEncodeBuf2 = NULL;
176         gCurMmsEncodeBuffPos2 = 0;
177         gMmsEncodeMaxLen2 = 0;
178         gMmsEncodeCurOffset2 = 0;
179 }
180
181 /**
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
186 */
187 static bool __MmsBinaryEncodeTextString2(FILE *pFile, UINT8 *source, int length)
188 {
189         MSG_DEBUG("MmsBinaryEncodeTextString2: \n");
190
191         /**
192          * make text string
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>
198          */
199
200         if (pFile == NULL || source == NULL) {
201                 MSG_DEBUG("MmsBinaryEncodeTextString2: source == NULL \n");
202                 goto __CATCH;
203         }
204
205         if ((gMmsEncodeMaxLen2 - gCurMmsEncodeBuffPos2) < length) {
206                 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf2, &gCurMmsEncodeBuffPos2,
207                                                                                         gMmsEncodeMaxLen2, &gMmsEncodeCurOffset2) == false) {
208                         MSG_DEBUG("MmsBinaryEncodeTextString2: MsgWriteDataFromEncodeBuffer fail \n");
209                         goto __CATCH;
210                 }
211         }
212
213         if (source[0] > 0x7F) {
214                 gpMmsEncodeBuf2[gCurMmsEncodeBuffPos2++] = QUOTE;
215                 length--;
216         }
217
218         strncpy(gpMmsEncodeBuf2 + gCurMmsEncodeBuffPos2, (char*)source, (length - 1));  /** except NULL */
219         gCurMmsEncodeBuffPos2 += (length - 1);  /** except NULL */
220         gpMmsEncodeBuf2[gCurMmsEncodeBuffPos2++] = (UINT8)NULL;
221
222         return true;
223
224 __CATCH:
225         return false;
226 }
227
228 static  bool __MmsBinaryEncodeFieldCodeAndValue2(FILE *pFile, UINT8 fieldCode, UINT8 fieldValue)
229 {
230         if ((gMmsEncodeMaxLen2 - gCurMmsEncodeBuffPos2) < 2) {
231                 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf2, &gCurMmsEncodeBuffPos2,
232                                                                                         gMmsEncodeMaxLen2, &gMmsEncodeCurOffset2) == false) {
233                         MSG_DEBUG("__MmsBinaryEncodeFieldCodeAndValue: MsgWriteDataFromEncodeBuffer fail \n");
234                         goto __CATCH;
235                 }
236         }
237
238         if (fieldCode == 0xff) {
239                 MSG_DEBUG("__MmsBinaryEncodeFieldCodeAndValue: invalid fieldCode \n");
240                 goto __CATCH;
241         }
242
243         if (fieldValue == 0xff) {
244                 MSG_DEBUG("__MmsBinaryEncodeFieldCodeAndValue: invalid fieldValue \n");
245                 return true;
246         }
247
248         gpMmsEncodeBuf2[gCurMmsEncodeBuffPos2++] = fieldCode;
249         gpMmsEncodeBuf2[gCurMmsEncodeBuffPos2++] = fieldValue;
250
251         return true;
252
253 __CATCH:
254         return false;
255 }
256
257 static bool __MmsBinaryEncodeMmsVersion2(FILE *pFile)
258 {
259         UINT8 majorVer = MMS_MAJOR_VERSION;
260         UINT8 minorVer = MMS_MINOR_VERSION;
261
262         MSG_DEBUG("__MmsBinaryEncodeMmsVersion2: \n");
263
264         if (pFile == NULL) {
265                 MSG_DEBUG("__MmsBinaryEncodeMmsVersion2: invalid input file \n");
266                 goto __CATCH;
267         }
268
269         if ((gMmsEncodeMaxLen2 - gCurMmsEncodeBuffPos2) < 2) {
270                 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf2, &gCurMmsEncodeBuffPos2,
271                                                                                         gMmsEncodeMaxLen2, &gMmsEncodeCurOffset2) == false) {
272                         MSG_DEBUG("__MmsBinaryEncodeMmsVersion2: MsgWriteDataFromEncodeBuffer fail \n");
273                         goto __CATCH;
274                 }
275         }
276
277         gpMmsEncodeBuf2[gCurMmsEncodeBuffPos2++] = MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_VERSION) | 0x80;
278         gpMmsEncodeBuf2[gCurMmsEncodeBuffPos2] = (majorVer << 4) | (minorVer & 0x0f) | MSB;
279
280         if (gpMmsEncodeBuf2[gCurMmsEncodeBuffPos2] < 0x80) {
281                 gpMmsEncodeBuf2[gCurMmsEncodeBuffPos2++] |= 0x80;
282         } else {
283                 gCurMmsEncodeBuffPos2++;
284         }
285
286         return true;
287
288 __CATCH:
289         return false;
290 }
291
292 static bool __MmsBinaryEncodeAckIndHdr(FILE *pFile, char *szTrID, bool bReportAllowed)
293 {
294         int length = 0;
295         UINT8 fieldCode = 0xff;
296         UINT8 fieldValue = 0xff;
297
298         MSG_DEBUG("_MmsBinaryEncodeAckIndHdr: szTrID = %s\n", szTrID);
299         MSG_DEBUG("_MmsBinaryEncodeAckIndHdr: bReportAllowed = %d\n", bReportAllowed);
300
301         __MmsCleanEncodeBuff2();
302
303         /* msgType */
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");
308                 goto __CATCH;
309         }
310
311         /* trID (other type of message) */
312         length = __MmsBinaryEncodeTextStringLen((UINT8*)szTrID);
313         if (length == -1) {
314                 MSG_DEBUG("_MmsBinaryEncodeAckIndHdr: MmsBinaryEncodeTextStringLen fail \n");
315                 goto __CATCH;
316         }
317
318         if ((gMmsEncodeMaxLen2 - gCurMmsEncodeBuffPos2) < (length + 1)) {
319                 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf2, &gCurMmsEncodeBuffPos2,
320                                                                                         gMmsEncodeMaxLen2, &gMmsEncodeCurOffset2) == false) {
321                         MSG_DEBUG("_MmsBinaryEncodeAckIndHdr: MsgWriteDataFromEncodeBuffer fail \n");
322                         goto __CATCH;
323                 }
324         }
325
326         gpMmsEncodeBuf2[gCurMmsEncodeBuffPos2++] = MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_TRID) | 0x80;
327
328         if (__MmsBinaryEncodeTextString2(pFile, (UINT8*)szTrID, length) == false) {
329                 MSG_DEBUG("_MmsBinaryEncodeAckIndHdr: MmsBinaryEncodeTextString fail\n");
330                 goto __CATCH;
331         }
332
333
334         if (__MmsBinaryEncodeMmsVersion2(pFile) == false) {
335                 MSG_DEBUG("_MmsBinaryEncodeAckIndHdr: __MmsBinaryEncodeMmsVersion error\n");
336                 goto __CATCH;
337         }
338
339
340         /* Report Allowed */
341         fieldCode  = MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_REPORTALLOWED) | 0x80;
342
343         if (bReportAllowed) {
344                 fieldValue = MmsGetBinaryValue(MmsCodeReportAllowed, MMS_REPORTALLOWED_YES) | 0x80;
345         } else {
346                 fieldValue = MmsGetBinaryValue(MmsCodeReportAllowed, MMS_REPORTALLOWED_NO) | 0x80;
347         }
348
349         if (__MmsBinaryEncodeFieldCodeAndValue2(pFile, fieldCode, fieldValue) == false) {
350                 MSG_DEBUG("_MmsBinaryEncodeAckIndHdr: Report Allowed error\n");
351                 goto __CATCH;
352         }
353
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");
358                 goto __CATCH;
359         }
360
361         return true;
362
363 __CATCH:
364
365         return false;
366 }
367
368 static bool __MmsBinaryEncodeNotiRespIndHdr(FILE *pFile, char *szTrID, msg_delivery_report_status_t iStatus, bool bReportAllowed)
369 {
370         int length = 0;
371         UINT8 fieldCode = 0xff;
372         UINT8 fieldValue = 0xff;
373
374         MSG_DEBUG("_MmsBinaryEncodeNotiRespIndHdr: szTrID = %s\n", szTrID);
375         MSG_DEBUG("_MmsBinaryEncodeNotiRespIndHdr: iStatus = %d\n", iStatus);
376         MSG_DEBUG("_MmsBinaryEncodeNotiRespIndHdr: bReportAllowed = %d\n", bReportAllowed);
377
378         __MmsCleanEncodeBuff2();
379
380         /* msgType */
381         fieldCode  = MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_MSGTYPE) | 0x80;
382         fieldValue = MmsGetBinaryValue(MmsCodeMsgType, MMS_MSGTYPE_NOTIFYRESP_IND) | 0x80;
383
384         if (__MmsBinaryEncodeFieldCodeAndValue2(pFile, fieldCode, fieldValue) == false) {
385                 MSG_DEBUG("_MmsBinaryEncodeNotiRespIndHdr: msgType error\n");
386                 goto __CATCH;
387         }
388
389
390         /* trID (other type of message) */
391         length = __MmsBinaryEncodeTextStringLen((UINT8*)szTrID);
392         if (length == -1) {
393                 MSG_DEBUG("_MmsBinaryEncodeNotiRespIndHdr: MmsBinaryEncodeTextStringLen fail \n");
394                 goto __CATCH;
395         }
396
397         if ((gMmsEncodeMaxLen2 - gCurMmsEncodeBuffPos2) < (length + 1)) {
398                 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf2, &gCurMmsEncodeBuffPos2,
399                                                                                         gMmsEncodeMaxLen2, &gMmsEncodeCurOffset2) == false) {
400                         MSG_DEBUG("_MmsBinaryEncodeNotiRespIndHdr: MsgWriteDataFromEncodeBuffer fail \n");
401                         goto __CATCH;
402                 }
403         }
404
405         gpMmsEncodeBuf2[gCurMmsEncodeBuffPos2++] = MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_TRID) | 0x80;
406         if (__MmsBinaryEncodeTextString2(pFile, (UINT8*)szTrID, length) == false) {
407                 MSG_DEBUG("_MmsBinaryEncodeNotiRespIndHdr: MmsBinaryEncodeTextString fail\n");
408                 goto __CATCH;
409         }
410
411
412         if (__MmsBinaryEncodeMmsVersion2(pFile) == false) {
413                 MSG_DEBUG("_MmsBinaryEncodeNotiRespIndHdr: __MmsBinaryEncodeMmsVersion error\n");
414                 goto __CATCH;
415         }
416
417
418         /* MsgStatus */
419         MSG_DEBUG("_MmsBinaryEncodeNotiRespIndHdr: MsgStatus = %d\n", iStatus);
420
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");
426                         goto __CATCH;
427                 }
428         }
429
430
431         /* Report Allowed */
432         fieldCode  = MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_REPORTALLOWED) | 0x80;
433
434         if (bReportAllowed) {
435                 fieldValue = MmsGetBinaryValue(MmsCodeReportAllowed, MMS_REPORTALLOWED_YES) | 0x80;
436         } else {
437                 fieldValue = MmsGetBinaryValue(MmsCodeReportAllowed, MMS_REPORTALLOWED_NO) | 0x80;
438         }
439
440         if (__MmsBinaryEncodeFieldCodeAndValue2(pFile, fieldCode, fieldValue) == false) {
441                 MSG_DEBUG("_MmsBinaryEncodeNotiRespIndHdr: Report Allowed error\n");
442                 goto __CATCH;
443         }
444
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");
449                 goto __CATCH;
450         }
451
452         return true;
453
454 __CATCH:
455
456         return false;
457 }
458
459 bool MmsEncodeAckInd(FILE *pFile, char *pTrID, bool bReportAllowed)
460 {
461         __MmsRegisterEncodeBuffer2(gszMmsEncodeBuf2, MSG_MMS_ENCODE_BUFFER_MAX);
462
463         MSG_DEBUG("_MmsEncodeAckInd: Start Binary Encoding now ============= \n");
464
465         if (__MmsBinaryEncodeAckIndHdr(pFile, pTrID, bReportAllowed) == false) {
466                 MSG_DEBUG("_MmsEncodeAckInd: SendReq Binary encoding fail \n");
467                 goto __CATCH;
468         }
469
470         __MmsUnregisterEncodeBuffer2();
471
472         return true;
473
474 __CATCH:
475
476         MSG_DEBUG("## _MmsEncodeAckInd: failed");
477         __MmsUnregisterEncodeBuffer2();
478
479         return false;
480 }
481
482 bool MmsEncodeNotiRespInd(FILE *pFile, char *pTrID, msg_delivery_report_status_t iStatus, bool bReportAllowed)
483 {
484         __MmsRegisterEncodeBuffer2(gszMmsEncodeBuf2, MSG_MMS_ENCODE_BUFFER_MAX);
485
486         MSG_DEBUG("_MmsEncodeNotiRespInd: Start Binary Encoding now ============= \n");
487
488         if (__MmsBinaryEncodeNotiRespIndHdr(pFile, pTrID, iStatus, bReportAllowed) == false) {
489                 MSG_DEBUG("_MmsEncodeNotiRespInd: SendReq Binary encoding fail \n");
490                 goto __CATCH;
491         }
492
493         __MmsUnregisterEncodeBuffer2();
494
495         return true;
496
497 __CATCH:
498
499         return false;
500 }
501
502 /* Functions for Acknowledge.ind & NotifyResp.ind  (END) ------------------------ */
503 bool MmsEncodeReadReport10(FILE *pFile, MmsMsg *pMsg, msg_read_report_status_t mmsReadStatus)
504 {
505         char *pText = NULL;
506         MsgMultipart *pPart = NULL;
507         MsgType msgType;
508         MsgBody msgBody;
509
510         char *pszReportMsg = NULL;
511         int     maxLen = 0;
512
513         struct  tm      *dateTime = NULL;
514         time_t  RawTime = 0;
515         time_t  dateSec = 0;
516
517         MmsRegisterEncodeBuffer(gszMmsEncodeBuf, MSG_MMS_ENCODE_BUFFER_MAX);
518
519         MSG_DEBUG("_MmsEncodeMsg: Start Binary Encoding now V1.0============= \n");
520
521         if (__MmsBinaryEncodeReadReport10Hdr(pFile, pMsg, mmsReadStatus) == false) {
522                 MSG_DEBUG("_MmsEncodeReadReport10: SendReq Binary encoding fail \n");
523                 goto __CATCH;
524         }
525
526         memset(&msgType, 0, sizeof(MsgType));
527         memset(&msgBody, 0, sizeof(MsgBody));
528
529         pText = (char *)malloc(MSG_STDSTR_LONG);
530         if (pText == NULL) {
531                 MSG_DEBUG("__MmsSendReadReportV10: text body malloc fail \n");
532                 goto __CATCH;
533         }
534
535         memset(pText, 0, MSG_STDSTR_LONG);
536
537         time(&RawTime);
538         dateTime = localtime(&RawTime);
539         dateSec = mktime(dateTime);
540
541         // get report message
542         if (mmsReadStatus == MSG_READ_REPORT_IS_DELETED) {
543                 pszReportMsg = (char*)"Your message has been deleted " ;
544         } else {
545                 pszReportMsg = (char*)"Your message has been read " ;
546         }
547
548         // make report body ..
549         maxLen = strlen (pszReportMsg) +16 /* date string */ + 8 /* enter chars */ ;
550
551         if (maxLen > MSG_STDSTR_LONG) {
552                 snprintf (pText, MSG_STDSTR_LONG, "%s\n", pszReportMsg);
553         } else {
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);
556         }
557
558         // make header
559         msgType.type = MIME_APPLICATION_VND_WAP_MULTIPART_MIXED;
560         msgType.contentSize = strlen(pText);
561         msgType.param.charset = MSG_CHARSET_UNKNOWN;
562
563         // make body
564         if ((pPart = MmsAllocMultipart()) == NULL) {
565                 MSG_DEBUG("__MmsSendReadReportV10: MsgAllocMultipart Fail \n");
566                 goto __CATCH;
567         }
568
569
570         pPart->type.type = MIME_TEXT_PLAIN;
571         pPart->type.contentSize = strlen(pText);
572         pPart->type.param.charset = MSG_CHARSET_UTF8;
573
574         if (pPart->pBody == NULL) {
575                 MSG_DEBUG("__MmsSendReadReportV10: pPart->pBody is NULL \n");
576                 goto __CATCH;
577         }
578
579         pPart->pBody->size = strlen(pText);
580         pPart->pBody->body.pText = pText;
581
582         msgBody.body.pMultipart = pPart;
583
584         if (__MmsBinaryEncodeMsgBody(pFile, &msgType, &msgBody, 1, false) == false) {
585                 MSG_DEBUG("__MmsSendReadReportV10: MmsBinaryEncodeMsgBody fail \n");
586                 goto __CATCH;
587         }
588
589         MSG_DEBUG("__MmsSendReadReportV10:  Send To RM ReadReport Msg \n");
590
591         if (pText) {
592                 free(pText);
593                 pText = NULL;
594         }
595
596         if (pPart) {
597                 if (pPart->pBody) {
598                         free(pPart->pBody);
599                         pPart->pBody = NULL;
600                 }
601                 free(pPart);
602                 pPart = NULL;
603         }
604
605         MmsUnregisterEncodeBuffer();
606
607         return true;
608
609 __CATCH:
610
611         if (pText) {
612                 free(pText);
613                 pText = NULL;
614         }
615
616         if (pPart) {
617                 if (pPart->pBody) {
618                         free(pPart->pBody);
619                         pPart->pBody = NULL;
620                 }
621                 free(pPart);
622                 pPart = NULL;
623         }
624
625         MmsUnregisterEncodeBuffer();
626
627         return false;
628 }
629
630 bool MmsEncodeReadReport11(FILE *pFile, MmsMsg *pMsg, msg_read_report_status_t mmsReadStatus)
631 {
632         MmsRegisterEncodeBuffer(gszMmsEncodeBuf, MSG_MMS_ENCODE_BUFFER_MAX);
633
634         MSG_DEBUG("_MmsEncodeMsg: Start Binary Encoding now V1.1============= \n");
635
636         if (__MmsBinaryEncodeReadReport11Hdr(pFile, pMsg, mmsReadStatus) == false) {
637                 MSG_DEBUG("_MmsEncodeMsg: SendReq Binary encoding fail \n");
638                 goto __CATCH;
639         }
640
641         MmsUnregisterEncodeBuffer();
642
643         return true;
644
645 __CATCH:
646         MmsUnregisterEncodeBuffer();
647
648         return false;
649 }
650
651 /* ==========================================================
652
653                                         B  I  N  A  R  Y         E  N  C  O  D  I  N  G
654
655    ==========================================================*/
656
657 /*
658  * Binary Encoded Message Format
659  *
660  *              < Single Part Body Message >
661  * -----------------------------------
662  * |            Header Fields                    |
663  * -----------------------------------
664  * | Content Type:start=xxx;type=xxx |  ->(ex) Text/Plain, Text/Html, ....
665  * -----------------------------------
666  * |            Single Part Body                 |
667  * -----------------------------------
668  *
669  *              < Multi Part Body Message >
670  * -----------------------------------
671  * |            Header Fields                    |
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  *                                                                                      -----------------------------  -
688  */
689 static bool __MmsBinaryEncodeSendReqHdr(FILE *pFile, MmsMsg *pMsg)
690 {
691         UINT8 fieldCode = 0xff;
692         UINT8 fieldValue        = 0xff;
693         int length              = 0;
694
695         __MmsCleanEncodeBuff();
696
697         /* msgType */
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");
702                 goto __CATCH;
703         }
704
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");
708                 goto __CATCH;
709         }
710
711         MSG_DEBUG("_MmsBinaryEncodeSendReqHdr:                pMsg->szTrID = %s\n", pMsg->szTrID);
712
713         if (__MmsBinaryEncodeMmsVersion(pFile) == false) {
714                 MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: __MmsBinaryEncodeMmsVersion error\n");
715                 goto __CATCH;
716         }
717
718         /* From : Insert Token mode */
719         if (__MmsBinaryEncodeFrom(pFile) == false) {
720                 MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: __MmsBinaryEncodeFrom fail\n");
721                 goto __CATCH;
722         }
723
724
725         /* To = Encoded-string-value */
726         if (__MmsBinaryEncodeAddress(pFile, MMS_CODE_TO, pMsg->mmsAttrib.szTo) == false)
727         {
728                 MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: To __MmsBinaryEncodeAddress fail\n");
729                 goto __CATCH;
730         }
731
732
733         /* Cc = Encoded-string-value */
734         if (__MmsBinaryEncodeAddress(pFile, MMS_CODE_CC, pMsg->mmsAttrib.szCc) == false) {
735                 MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: Cc __MmsBinaryEncodeAddress fail\n");
736                 goto __CATCH;
737         }
738
739
740         /* Bcc = Encoded-string-value */
741
742         if (__MmsBinaryEncodeAddress(pFile, MMS_CODE_BCC, pMsg->mmsAttrib.szBcc) == false) {
743                 MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: Bcc __MmsBinaryEncodeAddress fail\n");
744                 goto __CATCH;
745         }
746
747
748         MSG_DEBUG("_MmsBinaryEnocdeSendReqHdr() pMsg->mmsAttrib.szSubject =%s\n",pMsg->mmsAttrib.szSubject);
749
750         /* Subject = Encoded-string-value */
751         if (pMsg->mmsAttrib.szSubject[0]) {
752                 length = __MmsBinaryEncodeEncodedStringLen((UINT8*)pMsg->mmsAttrib.szSubject);
753                 if (length == -1) {
754                         MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: subject MmsBinaryEncodeEncodedStringLen fail \n");
755                         goto __CATCH;
756                 }
757
758                 if ((gMmsEncodeMaxLen - gCurMmsEncodeBuffPos) < length + 1) {
759                         if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
760                                                                                                 gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
761                                 MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: subject MsgWriteDataFromEncodeBuffer fail \n");
762                                 goto __CATCH;
763                         }
764                 }
765
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");
770                         goto __CATCH;
771                 }
772         }
773
774         /* MMS-1.3-con-739 */
775         /* Msg class */
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");
780                 goto __CATCH;
781         }
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");
787                         goto __CATCH;
788         }
789         /* MMS-1.3-con-733 */
790
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");
795                         goto __CATCH;
796                 }
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");
800                         goto __CATCH;
801                 }
802         }
803
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");
808                         goto __CATCH;
809                 }
810         } else {
811                 if (__MmsBinaryEncodeTime(pFile, MMS_CODE_DELIVERYTIME, pMsg->mmsAttrib.deliveryTime) == false) {
812                         MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: deliveryTime __MmsBinaryEncodeTime fail\n");
813                         goto __CATCH;
814                 }
815         }
816
817         /* Priority */
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 */
820
821                 fieldCode = MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_PRIORITY)|0x80;
822                 fieldValue = MmsGetBinaryValue(MmsCodePriority, pMsg->mmsAttrib.priority)|0x80;
823
824                 if (__MmsBinaryEncodeFieldCodeAndValue(pFile, fieldCode, fieldValue) == false) {
825                         MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: priority error\n");
826                         goto __CATCH;
827                 }
828         }
829
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;
834
835                 if (__MmsBinaryEncodeFieldCodeAndValue(pFile, fieldCode, fieldValue) == false) {
836                         MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: sender visibility error\n");
837                         goto __CATCH;
838                 }
839         }
840
841         /* Delivery Report (yes | no) */
842         fieldCode = MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_DELIVERYREPORT)|0x80;
843
844         if (pMsg->mmsAttrib.bAskDeliveryReport) {
845                 fieldValue = MmsGetBinaryValue(MmsCodeDeliveryReport, MMS_REPORT_YES) | 0x80;
846         } else {
847                 fieldValue = MmsGetBinaryValue(MmsCodeDeliveryReport, MMS_REPORT_NO) | 0x80;
848         }
849
850         if (__MmsBinaryEncodeFieldCodeAndValue(pFile, fieldCode, fieldValue) == false) {
851                 MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: sender visibility error\n");
852                 goto __CATCH;
853         }
854
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;
859         } else {
860                 fieldValue = MmsGetBinaryValue(MmsCodeReadReply, MMS_REPORT_NO)|0x80;
861         }
862
863         if (__MmsBinaryEncodeFieldCodeAndValue(pFile, fieldCode, fieldValue) == false) {
864                 MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: read reply error\n");
865                 goto __CATCH;
866         }
867
868         if ((pMsg->mmsAttrib.replyCharge.chargeType == MMS_REPLY_REQUESTED) ||
869                 (pMsg->mmsAttrib.replyCharge.chargeType == MMS_REPLY_REQUESTED_TEXT_ONLY)) {
870
871                 // reply charging
872                 fieldCode = MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_REPLYCHARGING)|0x80;
873                 fieldValue = MmsGetBinaryValue(MmsCodeReadReply, pMsg->mmsAttrib.replyCharge.chargeType)|0x80;
874
875                 if (__MmsBinaryEncodeFieldCodeAndValue(pFile, fieldCode, fieldValue) == false) {
876                         MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: replyCharging error\n");
877                         goto __CATCH;
878                 }
879
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");
884                                 goto __CATCH;
885                         }
886                 }
887
888                 /** fixme: Reply-charging-size */
889                 if (pMsg->mmsAttrib.replyCharge.chargeSize > 0) {
890                         length = __MmsBinaryEncodeIntegerLen(pMsg->mmsAttrib.replyCharge.chargeSize);
891
892                         if ((gMmsEncodeMaxLen - gCurMmsEncodeBuffPos) < (length + 1)) {
893                                 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
894                                                                                                 gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
895                                         MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: MsgWriteDataFromEncodeBuffer fail \n");
896                                         goto __CATCH;
897                                 }
898                         }
899
900                         gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_REPLYCHARGINGSIZE) | 0x80;
901
902                         if (__MmsBinaryEncodeInteger(pFile, pMsg->mmsAttrib.replyCharge.chargeSize, length) == false)   {
903                                 MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: replyChargingSize MmsBinaryEncodeInteger error\n");
904                                 goto __CATCH;
905                         }
906                 }
907
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);
911                         if (length == -1) {
912                                 MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: szReplyChargingID MmsBinaryEncodeTextStringLen fail\n");
913                                 goto __CATCH;
914                         }
915
916                         if ((gMmsEncodeMaxLen - gCurMmsEncodeBuffPos) < (length + 1)) {
917                                 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
918                                                                                                 gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
919                                         MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: MsgWriteDataFromEncodeBuffer fail \n");
920                                         goto __CATCH;
921                                 }
922                         }
923
924                         gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_REPLYCHARGINGID) | 0x80;
925
926                         if (__MmsBinaryEncodeTextString(pFile, (UINT8*)pMsg->mmsAttrib.replyCharge.szChargeID, length) == false) {
927                                 MSG_DEBUG("szContentLocation MmsBinaryEncodeTextString fail\n");
928                                 goto __CATCH;
929                         }
930                 }
931         }
932
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");
936                 goto __CATCH;
937         }
938
939         return true;
940
941 __CATCH:
942
943         MSG_DEBUG("## _MmsBinaryEncodeSendReqHdr: failed");
944         return false;
945 }
946
947 static bool __MmsBinaryEncodeReadReport10Hdr(FILE *pFile, MmsMsg *pMsg, msg_read_report_status_t mmsReadStatus)
948 {
949         int length      = 0;
950         char *szTo      = NULL;
951         UINT8 fieldCode = 0xff;
952         UINT8 fieldValue = 0xff;
953         char szSubject[MSG_LOCALE_SUBJ_LEN + 8] = {0, };
954
955         if (pMsg == NULL) {
956                 MSG_DEBUG("pMsg is NULL");
957                 goto __CATCH;
958         }
959
960         MSG_DEBUG("_MmsBinaryEncodeReadReport10Hdr: mmsReadStatus = %d\n", mmsReadStatus);
961
962         __MmsCleanEncodeBuff();
963
964         /* msgType */
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");
969                 goto __CATCH;
970         }
971
972         /* trID (other type of message) */
973         if (__MmsBinaryEncodeTrID(pFile, NULL, 0) == false) {
974                 MSG_DEBUG("_MmsBinaryEncodeReadReport10Hdr: __MmsBinaryEncodeTrID error\n");
975                 goto __CATCH;
976         }
977
978         if (__MmsBinaryEncodeMmsVersion(pFile) == false) {
979                 MSG_DEBUG("_MmsBinaryEncodeReadReport10Hdr: __MmsBinaryEncodeMmsVersion error\n");
980                 goto __CATCH;
981         }
982
983         /* Date = Long-integer */
984         if (!__MmsBinaryEncodeDate(pFile)) {
985                 MSG_DEBUG("_MmsBinaryEncodeReadReport10Hdr: date __MmsBinaryEncodeDate error\n");
986                 goto __CATCH;
987         }
988
989         /* From : Insert Token mode */
990         if (__MmsBinaryEncodeFrom(pFile) == false) {
991                 MSG_DEBUG("_MmsBinaryEncodeReadReport10Hdr: __MmsBinaryEncodeFrom fail\n");
992                 goto __CATCH;
993         }
994
995         MSG_DEBUG("_MmsBinaryEncodeReadReport10Hdr: To = %s\n", pMsg->mmsAttrib.szFrom);
996
997         /* To = Encoded-string */
998         if (pMsg && (strchr(pMsg->mmsAttrib.szFrom, '/') == NULL)) {
999                 length = strlen(pMsg->mmsAttrib.szFrom);
1000                 szTo = (char *)malloc(length + 11);
1001                 if (szTo == NULL) {
1002                         MSG_DEBUG("_MmsBinaryEncodeReadReport10Hdr: szTo alloc fail\n");
1003                         goto __CATCH;
1004                 }
1005
1006                 snprintf(szTo, length + 11, "%s/TYPE=PLMN", pMsg->mmsAttrib.szFrom);
1007                 MSG_DEBUG("_MmsBinaryEncodeReadReport10Hdr: To = %s\n", szTo);
1008
1009                 if (__MmsBinaryEncodeAddress(pFile, MMS_CODE_TO, szTo) == false) {
1010                         MSG_DEBUG("_MmsBinaryEncodeReadReport10Hdr: To __MmsBinaryEncodeAddress fail\n");
1011                         goto __CATCH;
1012                 }
1013
1014                 if (szTo) {
1015                         free(szTo);
1016                         szTo = NULL;
1017                 }
1018         } else {
1019                 if (__MmsBinaryEncodeAddress(pFile, MMS_CODE_TO, pMsg->mmsAttrib.szFrom) == false) {
1020                         MSG_DEBUG("_MmsBinaryEncodeReadReport10Hdr: To __MmsBinaryEncodeAddress fail\n");
1021                         goto __CATCH;
1022                 }
1023         }
1024
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);
1029                 } else {
1030                         snprintf(szSubject, MSG_LOCALE_SUBJ_LEN + 8, "%s%s", MMS_READ_REPORT_STRING_DELETED, pMsg->mmsAttrib.szSubject);
1031                 }
1032         } else {
1033                 if (mmsReadStatus == MSG_READ_REPORT_IS_READ) {
1034                         snprintf(szSubject, MSG_LOCALE_SUBJ_LEN + 8, "%s", MMS_READ_REPORT_STRING_READ);
1035
1036                 } else {
1037                         snprintf(szSubject, MSG_LOCALE_SUBJ_LEN + 8, "%s", MMS_READ_REPORT_STRING_DELETED );
1038                 }
1039         }
1040
1041         length = __MmsBinaryEncodeEncodedStringLen((UINT8*)szSubject);
1042         if (length == -1) {
1043                 MSG_DEBUG("_MmsBinaryEncodeReadReport10Hdr: subject MmsBinaryEncodeEncodedStringLen fail \n");
1044                 goto __CATCH;
1045         }
1046
1047         if ((gMmsEncodeMaxLen - gCurMmsEncodeBuffPos) < length + 1) {
1048                 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos, gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
1049                         MSG_DEBUG("_MmsBinaryEncodeReadReport10Hdr: subject MsgWriteDataFromEncodeBuffer fail \n");
1050                         goto __CATCH;
1051                 }
1052         }
1053
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");
1058                 goto __CATCH;
1059         }
1060
1061         /* Msg class */
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");
1066                 goto __CATCH;
1067         }
1068
1069
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");
1075                 goto __CATCH;
1076         }
1077
1078
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");
1084                 goto __CATCH;
1085         }
1086
1087
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");
1093                 goto __CATCH;
1094         }
1095
1096         /* fixme: MimeType */
1097         /* fixme: msgHeader */
1098         /* fixme: msgBody */
1099
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");
1104                 goto __CATCH;
1105         }
1106
1107         return true;
1108
1109 __CATCH:
1110         if (szTo) {
1111                 free(szTo);
1112                 szTo = NULL;
1113         }
1114
1115         return false;
1116 }
1117
1118 static bool __MmsBinaryEncodeReadReport11Hdr(FILE *pFile, MmsMsg *pMsg, msg_read_report_status_t mmsReadStatus)
1119 {
1120         UINT8 fieldCode = 0xff;
1121         UINT8 fieldValue = 0xff;
1122         char *szTo = NULL;
1123
1124         __MmsCleanEncodeBuff();
1125
1126         /* msgType */
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");
1131                 goto __CATCH;
1132         }
1133
1134         /* MMS version */
1135         if (__MmsBinaryEncodeMmsVersion(pFile) == false) {
1136                 MSG_DEBUG("_MmsBinaryEncodeReadReport11Hdr: __MmsBinaryEncodeMmsVersion error\n");
1137                 goto __CATCH;
1138         }
1139
1140         if (__MmsBinaryEncodeMsgID(pFile, pMsg->szMsgID) == false)
1141                 goto __CATCH;
1142
1143         /* To = Encoded-string */
1144         if (strchr(pMsg->mmsAttrib.szFrom, '/') == NULL) {
1145                 int length = 0;
1146                 length = strlen(pMsg->mmsAttrib.szFrom);
1147                 szTo = (char *)malloc(length + 11);
1148
1149                 if (szTo == NULL) {
1150                         MSG_DEBUG("_MmsBinaryEncodeReadReport10Hdr: szTo alloc fail\n");
1151                         goto __CATCH;
1152                 }
1153
1154                 snprintf(szTo, length + 11,"%s/TYPE=PLMN", pMsg->mmsAttrib.szFrom);
1155                 MSG_DEBUG("_MmsBinaryEncodeReadReport10Hdr: To = %s\n", szTo);
1156
1157                 if (__MmsBinaryEncodeAddress(pFile, MMS_CODE_TO, szTo) == false) {
1158                         MSG_DEBUG("_MmsBinaryEncodeReadReport10Hdr: To __MmsBinaryEncodeAddress fail\n");
1159                         goto __CATCH;
1160                 }
1161
1162                 if (szTo) {
1163                         free(szTo);
1164                         szTo = NULL;
1165                 }
1166         } else {
1167                 if (__MmsBinaryEncodeAddress(pFile, MMS_CODE_TO, pMsg->mmsAttrib.szFrom) == false) {
1168                         MSG_DEBUG("_MmsBinaryEncodeReadReport11Hdr: To __MmsBinaryEncodeAddress fail\n");
1169                         goto __CATCH;
1170                 }
1171         }
1172
1173         /* From : Insert Token mode */
1174         if (__MmsBinaryEncodeFrom(pFile) == false) {
1175                 MSG_DEBUG("_MmsBinaryEncodeReadReport11Hdr: __MmsBinaryEncodeFrom fail\n");
1176                 goto __CATCH;
1177         }
1178
1179         /* Date = Long-integer */
1180         if (!__MmsBinaryEncodeDate(pFile)) {
1181                 MSG_DEBUG("_MmsBinaryEncodeReadReport11Hdr: __MmsBinaryEncodeDate error\n");
1182                 goto __CATCH;
1183         }
1184
1185         /* Read Status (Yes | no) */
1186         fieldCode  = MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_READSTATUS) | 0x80;
1187         fieldValue = MmsGetBinaryValue(MmsCodeReadStatus, mmsReadStatus) | 0x80;
1188
1189         if (__MmsBinaryEncodeFieldCodeAndValue(pFile, fieldCode, fieldValue) == false) {
1190                 MSG_DEBUG("_MmsBinaryEncodeReadReport11Hdr: Read Status error\n");
1191                 goto __CATCH;
1192         };
1193
1194         if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
1195                                                                                 gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
1196                 MSG_DEBUG("_MmsBinaryEncodeReadReport11Hdr: remained data MsgWriteDataFromEncodeBuffer fail \n");
1197                 goto __CATCH;
1198         }
1199
1200         return true;
1201
1202 __CATCH:
1203         if (szTo) {
1204                 free(szTo);
1205                 szTo = NULL;
1206         }
1207
1208         return false;
1209 }
1210
1211 static bool __MmsBinaryEncodeMsgBody(FILE *pFile, MsgType *pType, MsgBody *pBody, int nPartCount, bool bTemplate)
1212 {
1213         int length = 0;
1214         MsgMultipart *pMultipart = NULL;
1215
1216         MSG_DEBUG("MmsBinaryEncodeMsgBody: nPartCount = %d\n", nPartCount);
1217
1218         if (pFile == NULL || pType == NULL) {
1219                 MSG_DEBUG("MmsBinaryEncodeMsgBody: invalid file handle\n");
1220                 goto __CATCH;
1221         }
1222
1223         if (MmsIsMultipart(pType->type)) {
1224
1225                 int     nEntries   = 0;
1226                 /* ---------------------------
1227                  *       Multipart message
1228                  * ---------------------------*/
1229
1230                 pType->offset = __MmsGetEncodeOffset();
1231
1232                 /* Content type */
1233                 length = __MmsBinaryEncodeContentTypeLen(pType);
1234                 if (length == -1) {
1235                         MSG_DEBUG("MmsBinaryEncodeMsgBody: MmsBinaryEncodeContentTypeLen fail \n");
1236                         goto __CATCH;
1237                 }
1238                 if (bTemplate == false)
1239                         gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_CONTENTTYPE) | 0x80;
1240
1241                 if (__MmsBinaryEncodeContentType(pFile, pType, length) == false) {
1242                         MSG_DEBUG("MmsBinaryEncodeMsgBody: MmsBinaryEncodeContentType fail \n");
1243                         goto __CATCH;
1244                 }
1245
1246                 pBody->offset = __MmsGetEncodeOffset();
1247
1248                 /* nEntries */
1249                 if (pBody->pPresentationBody) {
1250                         nEntries = nPartCount + 1;
1251                 } else {
1252                         nEntries = nPartCount;
1253                 }
1254
1255                 if (nEntries >= 0) {
1256                         length = __MmsBinaryEncodeUintvarLen(nEntries);
1257                         if (length == -1) {
1258                                 MSG_DEBUG("MmsBinaryEncodeMsgBody: nEntries MmsBinaryEncodeUintvarLen fail \n");
1259                                 goto __CATCH;
1260                         }
1261                         if (__MmsBinaryEncodeUintvar(pFile, nEntries, length) == false) {
1262                                 MSG_DEBUG("MmsBinaryEncodeMsgBody: nEntries MmsBinaryEncodeUintvar fail \n");
1263                                 goto __CATCH;
1264                         }
1265
1266                         pType->size = __MmsGetEncodeOffset() - pType->offset;
1267                 }
1268
1269                 if (nEntries > 0) {
1270                         if (nEntries && pBody->pPresentationBody) {
1271                                 if (__MmsBinaryEncodeMsgPart(pFile, pType->type, &pBody->presentationType, pBody->pPresentationBody) == false) {
1272                                         MSG_DEBUG("MmsBinaryEncodeMsgBody: __MmsBinaryEncodeMsgPart fail \n");
1273                                         goto __CATCH;
1274                                 }
1275
1276                                 nEntries--;
1277                         }
1278
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");
1283                                         goto __CATCH;
1284                                 }
1285                                 pMultipart = pMultipart->pNext;
1286                                 nEntries--;
1287                         }
1288                 } else {
1289                         if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
1290                                                                                                 gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
1291                                 MSG_DEBUG("MmsBinaryEncodeMsgBody: Empty message body MsgWriteDataFromEncodeBuffer fail \n");
1292                                 goto __CATCH;
1293                         }
1294                 }
1295
1296                 pBody->size = __MmsGetEncodeOffset() - pBody->offset;
1297         } else {
1298                 /* ---------------------------
1299                  *       Singlepart message
1300                  * ---------------------------*/
1301                 pType->offset = __MmsGetEncodeOffset();
1302
1303                 if (__MmsBinaryEncodeContentHeader(pFile, (MimeType)pType->type, pType, false) == false)
1304                         goto __CATCH;
1305
1306                 length = __MmsBinaryEncodeContentTypeLen(pType);
1307                 if (length == -1) {
1308                         MSG_DEBUG("MmsBinaryEncodeMsgBody: Singlepart MmsBinaryEncodeContentTypeLen fail \n");
1309                         goto __CATCH;
1310                 }
1311
1312                 if (__MmsBinaryEncodeContentType(pFile, pType, length) == false)
1313                         goto __CATCH;
1314
1315                 pType->size = __MmsGetEncodeOffset() - pType->offset;
1316
1317                 if (__MmsBinaryEncodeContentBody(pFile, pBody) == false)
1318                         goto __CATCH;
1319         }
1320
1321         return true;
1322
1323 __CATCH:
1324         MSG_DEBUG("## MmsBinaryEncodeMsgBody: failed\n");
1325
1326         return false;
1327 }
1328
1329 static int __MmsBinaryEncodeContentTypeLen(MsgType *pType)
1330 {
1331         int length = 0;
1332         int     totalLength = 0;
1333         UINT16 fieldValue = 0xffff;
1334         const char *szTextType = NULL;
1335         int     contentType = MIME_UNKNOWN;
1336
1337         MSG_DEBUG("MSmsBinaryEncodeContentTypeLen:  type\n");
1338
1339         /*
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)
1346          */
1347
1348         /* Content-type-value = Content-general-form ------------------------------- */
1349         /* Content-Type */
1350
1351         contentType = pType->type;
1352
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);
1359                         if (length == -1) {
1360                                 MSG_DEBUG("MSmsBinaryEncodeContentTypeLen: szTextType MmsBinaryEncodeTextStringLen fail \n");
1361                                 goto __CATCH;
1362                         }
1363                         totalLength += length;
1364                 } else {
1365                         totalLength++;
1366                 }
1367         } else {
1368                 totalLength++;
1369         }
1370
1371         /* Parameters -------------------------------------------------------------- */
1372
1373         MSG_DEBUG("MSmsBinaryEncodeContentTypeLen:  parameters    \n\n");
1374
1375         /* Well-known-charset = Any-charset | Integer-value ----------------------- */
1376
1377         if (pType->param.charset != MSG_CHARSET_UNKNOWN) {
1378                 fieldValue = MmsGetBinaryValue(MmsCodeCharSet, pType->param.charset);
1379                 length     = __MmsBinaryEncodeIntegerLen(fieldValue);
1380                 if (length == -1) {
1381                         MSG_DEBUG("MmsBinaryEncodeContentTypeLen: charSet MmsBinaryEncodeIntegerLen fail \n");
1382                         goto __CATCH;
1383                 }
1384                 totalLength += (length + 1);
1385         } else {
1386                 if (MmsIsText(contentType)) {   // Any-charset
1387                         if (!MmsIsVitemContent (contentType, pType->param.szName))
1388                                 totalLength += 2;
1389                 }
1390         }
1391
1392
1393         /* Name = Text-string ------------------------------------------------------ */
1394
1395         if (pType->param.szName[0]) {
1396                 char* pszName = NULL;
1397
1398                 if (MsgIsASCII (pType->param.szName)) {
1399                         MSG_DEBUG("MmsBinaryEncodeContentTypeLen: szName is consisted of ascii char-set chars. \n");
1400
1401                         pszName = (char *)malloc(strlen(pType->param.szName) +1);
1402                         memset(pszName, 0, (strlen(pType->param.szName)+1));
1403                         strcpy(pszName, pType->param.szName);
1404                 } else {
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);
1407
1408                         /* if msg-server can't support non ascii, then convert non-ascii to '_' by using _MsgReplaceNonAscii*/
1409                         int filelen = strlen(pType->param.szName);
1410
1411                         pszName = (char *)malloc(filelen + 1);
1412                         memset(pszName, 0, (filelen + 1));
1413                         strncpy(pszName, pType->param.szName, filelen);
1414
1415                         MSG_DEBUG("MmsBinaryEncodeContentTypeLen: pszName : %s\n", pszName);
1416                 }
1417
1418                 //change empty space to '_' in the file name
1419                 if (MsgIsSpace(pszName)) {
1420                         char *pszTempName = NULL;
1421
1422                         MSG_DEBUG("MmsBinaryEncodeContentTypeLen: szName has space(' '). \n");
1423
1424                         MsgReplaceSpecialChar(pszName, &pszTempName, ' ');
1425
1426                         if (pszTempName) {
1427                                 free(pszName);
1428                                 pszName = pszTempName;
1429                         }
1430                         MSG_DEBUG("MmsBinaryEncodeContentTypeLen: pszName : %s\n", pszName);
1431                 }
1432
1433                 length = __MmsBinaryEncodeTextStringLen((UINT8*)pszName);
1434                 free(pszName);
1435
1436                 if (length == -1) {
1437                         MSG_DEBUG("MmsBinaryEncodeContentTypeLen: szName MmsBinaryEncodeIntegerLen fail \n");
1438                         goto __CATCH;
1439                 }
1440
1441                 totalLength += (length + 1);
1442         }
1443
1444 #ifdef FEATURE_JAVA_MMS
1445         if (pType->param.szApplicationID) {
1446                 length = __MmsBinaryEncodeTextStringLen((UINT8*)pType->param.szApplicationID);
1447                 if (length == -1) {
1448                         MSG_DEBUG("MmsBinaryEncodeContentTypeLen: szApplicationID MmsBinaryEncodeTextStrinLen fail \n");
1449                         goto __CATCH;
1450                 }
1451
1452                 totalLength += (length);
1453                 if (MmsGetBinaryValue(MmsCodeParameterCode, MSG_PARAM_APPLICATION_ID) == UNDEFINED_BINARY) {
1454                         totalLength += strlen(MmsGetTextValue(MmsCodeParameterCode, MSG_PARAM_APPLICATION_ID)) + 1; /* NULL */
1455                 } else {
1456                         totalLength++;
1457                 }
1458         }
1459
1460         if (pType->param.szReplyToApplicationID) {
1461                 length = __MmsBinaryEncodeTextStringLen((UINT8*)pType->param.szReplyToApplicationID);
1462                 if (length == -1) {
1463                         MSG_DEBUG("MmsBinaryEncodeContentTypeLen: szApplicationID MmsBinaryEncodeTextStrinLen fail \n");
1464                         goto __CATCH;
1465                 }
1466
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 */
1470                 } else {
1471                         totalLength++;
1472                 }
1473         }
1474 #endif
1475
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);
1485                                 if (length == -1) {
1486                                         MSG_DEBUG("MmsBinaryEncodeContentTypeLen: type param MmsBinaryEncodeTextStringLen fail \n");
1487                                         goto __CATCH;
1488                                 }
1489                                 totalLength += (length + 1);
1490                         } else {
1491                                 totalLength += 2;
1492                         }
1493                 } else {
1494                         totalLength += 2;
1495                 }
1496
1497                 /* start = Text-string ----------------------- */
1498                 if (pType->param.szStart[0]) {
1499                         /* start = Text-string */
1500                         length  = __MmsBinaryEncodeTextStringLen((UINT8*)pType->param.szStart);
1501                         if (length == -1) {
1502                                 MSG_DEBUG("MmsBinaryEncodeContentType: szStart MmsBinaryEncodeTextStringLen fail \n");
1503                                 goto __CATCH;
1504                         }
1505
1506                         totalLength += (length + 1);
1507                 }
1508
1509
1510                 /* startInfo = Text-string -------------------- */
1511                 if (pType->param.szStartInfo[0]) {
1512                         /* StartInfo (with multipart/related) = Text-string */
1513                         length  = __MmsBinaryEncodeTextStringLen((UINT8*)pType->param.szStartInfo);
1514                         if (length == -1) {
1515                                 MSG_DEBUG("MmsBinaryEncodeContentType: szStartInfo MmsBinaryEncodeTextStringLen fail \n");
1516                                 goto __CATCH;
1517                         }
1518
1519                         totalLength += (length + 1);
1520                 }
1521         }
1522
1523         return totalLength;
1524
1525 __CATCH:
1526
1527         MSG_DEBUG("## MmsBinaryEncodeContentTypeLen: failed");
1528         return -1;
1529 }
1530
1531 static bool __MmsBinaryEncodeContentType(FILE *pFile, MsgType *pType, int typeLength)
1532 {
1533         int length = 0;
1534         UINT16 fieldValue = 0xffff;
1535         const char *szTextType = NULL;
1536         int contentType = MIME_UNKNOWN;
1537
1538 #ifdef FEATURE_JAVA_MMS
1539         const char *szParameter = NULL;
1540 #endif
1541
1542         MSG_DEBUG("************************************************************************************\n");
1543         MSG_DEBUG("MmsBinaryEncodeContentType:  C O N T E N T     T Y P E    \n\n");
1544
1545         /*
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)
1552          */
1553
1554         if (pFile == NULL) {
1555                 MSG_DEBUG("MmsBinaryEncodeContentType: invalid file handle\n");
1556                 goto __CATCH;
1557         }
1558
1559
1560         /* Content-Type = Content-general-form ------------------------------- */
1561
1562         length = __MmsBinaryEncodeValueLengthLen(typeLength);
1563         if (length == -1) {
1564                 MSG_DEBUG("MSmsBinaryEncodeContentType : MmsBinaryEncodeValueLengthLen fail.\n");
1565                 goto __CATCH;
1566         }
1567
1568         if (__MmsBinaryEncodeValueLength(pFile, typeLength, length) == false) {
1569                 MSG_DEBUG("MSmsBinaryEncodeContentType : MmsBinaryEncodeValueLength fail.\n");
1570                 goto __CATCH;
1571         }
1572
1573         contentType = pType->type;
1574
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)
1580                         fieldValue = 0x00;
1581         }
1582
1583         if (szTextType == NULL) {
1584                 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = (UINT8)fieldValue | 0x80;
1585         } else {
1586                 length  = __MmsBinaryEncodeTextStringLen((UINT8*)szTextType);
1587                 if (length == -1) {
1588                         MSG_DEBUG("MSmsBinaryEncodeContentType: szTextType MmsBinaryEncodeTextStringLen fail \n");
1589                         goto __CATCH;
1590                 }
1591
1592                 if (__MmsBinaryEncodeTextString(pFile, (UINT8*)szTextType, length) == false) {
1593                         MSG_DEBUG("MmsBinaryEncodeContentType: szTextType MmsBinaryEncodeTextString fail \n");
1594                         goto __CATCH;
1595                 }
1596         }
1597
1598         /* Parameters -------------------------------------------------------------- */
1599
1600         MSG_DEBUG("MmsBinaryEncodeContentType:  P A R M E T E R S    \n\n");
1601
1602         /* Name = Text-string ------------------------------------------------------ */
1603
1604         if (pType->param.szName[0]) {
1605                 char* pszName = NULL;
1606
1607                 if (MsgIsASCII (pType->param.szName)) {
1608
1609                         MSG_DEBUG("MmsBinaryEncodeContentType: szName is consisted of ascii char-set chars. \n");
1610
1611                         pszName = (char *)malloc(strlen(pType->param.szName) +1);
1612                         memset(pszName, 0, (strlen(pType->param.szName)+1));
1613                         strcpy(pszName, pType->param.szName);
1614                 } else {
1615                         MSG_DEBUG("MmsBinaryEncodeContentTypeLen: szName is not consisted of ascii char-set chars. \n");
1616
1617                         /* if msg-server can't support non ascii, then convert non-ascii to '_' by using _MsgReplaceNonAscii*/
1618                         int filelen = strlen(pType->param.szName);
1619
1620                         pszName = (char *)malloc(filelen + 1);
1621                         memset(pszName, 0, (filelen + 1));
1622                         strncpy(pszName, pType->param.szName, filelen);
1623
1624                         MSG_DEBUG("MmsBinaryEncodeContentType: pszName : %s\n", pszName);
1625                 }
1626
1627                 //change empty space to '_' in the file name
1628                 if (MsgIsSpace(pszName)) {
1629                         char*   pszTempName = NULL;
1630
1631                         MSG_DEBUG("MmsBinaryEncodeContentType: szName has space(' '). \n");
1632
1633                         MsgReplaceSpecialChar(pszName, &pszTempName, ' ');
1634
1635                         if (pszTempName) {
1636                                 free(pszName);
1637                                 pszName = pszTempName;
1638                         }
1639                         MSG_DEBUG("MmsBinaryEncodeContentType: pszName : %s\n", pszName);
1640                 }
1641
1642                 length = __MmsBinaryEncodeTextStringLen((UINT8*)pszName);
1643                 if (length == -1) {
1644                         MSG_DEBUG("MmsBinaryEncodeContentType: szName MmsBinaryEncodeIntegerLen fail \n");
1645                         free(pszName);
1646                         goto __CATCH;
1647                 }
1648                 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = MmsGetBinaryValue(MmsCodeParameterCode, MSG_PARAM_NAME) | 0x80;
1649
1650                 if (__MmsBinaryEncodeTextString(pFile, (UINT8*)pszName, length) == false) {
1651                         MSG_DEBUG("MmsBinaryEncodeContentType: szName MmsBinaryEncodeTextString fail\n");
1652                         free(pszName);
1653                         goto __CATCH;
1654                 }
1655                 free(pszName);
1656         }
1657 #ifdef FEATURE_JAVA_MMS
1658         MSG_DEBUG(" MmsBinaryEncodeContentType: Application-ID \n");
1659
1660         /* Application-ID: Text-string */
1661         if (pType->param.szApplicationID) {
1662                 length = __MmsBinaryEncodeTextStringLen((UINT8*) pType->param.szApplicationID);
1663                 if (length == -1) {
1664                         MSG_DEBUG("MmsBinaryEncodeContentType: szApplicationID MmsBinaryEncodeIntegerLen Fail \n");
1665                         goto __CATCH;
1666                 }
1667
1668                 fieldValue = MmsGetBinaryValue(MmsCodeParameterCode, MSG_PARAM_APPLICATION_ID);
1669
1670                 if (fieldValue == UNDEFINED_BINARY)
1671                         szParameter = MmsGetTextValue(MmsCodeParameterCode, MSG_PARAM_APPLICATION_ID);
1672
1673                 if (szParameter == NULL) {
1674                         MSG_DEBUG("MmsBinaryEncodeContentType: szParameter is NULL \n");
1675                         goto __CATCH;
1676                 }
1677
1678                 strncpy(gpMmsEncodeBuf + gCurMmsEncodeBuffPos, (char*)szParameter, strlen(szParameter));
1679                 gCurMmsEncodeBuffPos += strlen(szParameter);
1680                 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] =(UINT8)NULL;
1681
1682                 if (__MmsBinaryEncodeTextString(pFile, (UINT8*) pType->param.szApplicationID, length) == false) {
1683                         MSG_DEBUG(" MmsBinaryEncodeContentType: szApplicationID MmsBinaryEncodeTextString fail\n");
1684                         goto __CATCH;
1685                 }
1686
1687         }
1688
1689         /* ReplyToApplicationID: Text-string */
1690         if (pType->param.szReplyToApplicationID) {
1691                 length = __MmsBinaryEncodeTextStringLen((UINT8*) pType->param.szReplyToApplicationID);
1692                 if (length == -1) {
1693                         MSG_DEBUG("MmsBinaryEncodeContentType: szReplyToApplicationID MmsBinaryEncodeIntegerLen Fail \n");
1694                         goto __CATCH;
1695                 }
1696
1697                 fieldValue = MmsGetBinaryValue(MmsCodeParameterCode, MSG_PARAM_REPLY_TO_APPLICATION_ID);
1698
1699                 if (fieldValue == UNDEFINED_BINARY)
1700                         szParameter = MmsGetTextValue(MmsCodeParameterCode, MSG_PARAM_REPLY_TO_APPLICATION_ID);
1701
1702                 if (szParameter == NULL) {
1703                         MSG_DEBUG("MmsBinaryEncodeContentType: szParameter is NULL \n");
1704                         goto __CATCH;
1705                 }
1706
1707                 strncpy(gpMmsEncodeBuf + gCurMmsEncodeBuffPos, (char*)szParameter, strlen(szParameter));
1708                 gCurMmsEncodeBuffPos += strlen(szParameter);
1709                 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] =(UINT8)NULL;
1710
1711                 if (__MmsBinaryEncodeTextString(pFile, (UINT8*) pType->param.szReplyToApplicationID, length) == false) {
1712                         MSG_DEBUG(" MmsBinaryEncodeContentType: szApplicationID MmsBinaryEncodeTextString fail\n");
1713                         goto __CATCH;
1714                 }
1715         }
1716 #endif
1717
1718         /* Well-known-charset = Any-charset | Integer-value ----------------------- */
1719
1720         if (pType->param.charset != MSG_CHARSET_UNKNOWN) {
1721                 fieldValue = MmsGetBinaryValue(MmsCodeCharSet, pType->param.charset);
1722                 length = __MmsBinaryEncodeIntegerLen(fieldValue);
1723                 if (length == -1) {
1724                         MSG_DEBUG("MmsBinaryEncodeContentType: charSet MmsBinaryEncodeIntegerLen fail \n");
1725                         goto __CATCH;
1726                 }
1727
1728                 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = MmsGetBinaryValue(MmsCodeParameterCode, MSG_PARAM_CHARSET) | 0x80;
1729                 if (__MmsBinaryEncodeInteger(pFile, fieldValue, length) == false) {
1730                         MSG_DEBUG("MmsBinaryEncodeContentType: charSet MmsBinaryEncodeInteger fail\n");
1731                         goto __CATCH;
1732                 }
1733         } else {
1734                 /* Any-charset */
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;
1740                         }
1741                 }
1742         }
1743
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)
1752                                 fieldValue = 0x00;
1753                 }
1754
1755                 if (szTextType == NULL) {
1756                         gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = (UINT8)fieldValue | 0x80;
1757                         gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = MmsGetBinaryValue(MmsCodeParameterCode, MSG_PARAM_TYPE) | 0x80;
1758                 } else {
1759                         length  = __MmsBinaryEncodeTextStringLen((UINT8*)szTextType);
1760                         if (length == -1) {
1761                                 MSG_DEBUG("MmsBinaryEncodeContentType: type param MmsBinaryEncodeTextStringLen fail \n");
1762                                 goto __CATCH;
1763                         }
1764
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");
1768                                 goto __CATCH;
1769                         }
1770                 }
1771
1772                 /* start = Text-string ----------------------- */
1773                 if (pType->param.szStart[0]) {
1774                         /* start = Text-string */
1775                         length  = __MmsBinaryEncodeTextStringLen((UINT8*)pType->param.szStart);
1776                         if (length == -1) {
1777                                 MSG_DEBUG("MmsBinaryEncodeContentType: szStart MmsBinaryEncodeTextStringLen fail \n");
1778                                 goto __CATCH;
1779                         }
1780
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");
1785                                 goto __CATCH;
1786                         }
1787                 }
1788
1789                 /* startInfo = Text-string -------------------- */
1790                 if (pType->param.szStartInfo[0]) {
1791                         /* StartInfo (with multipart/related) = Text-string */
1792                         length  = __MmsBinaryEncodeTextStringLen((UINT8*)pType->param.szStartInfo);
1793                         if (length == -1) {
1794                                 MSG_DEBUG("MmsBinaryEncodeContentType: szStartInfo MmsBinaryEncodeTextStringLen fail \n");
1795                                 goto __CATCH;
1796                         }
1797
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");
1801                                 goto __CATCH;
1802                         }
1803                 }
1804         }
1805
1806         return true;
1807
1808 __CATCH:
1809         MSG_DEBUG("## MmsBinaryEncodeContentType: failed");
1810         return false;
1811 }
1812
1813 static bool __MmsBinaryEncodeMsgPart(FILE *pFile, int contentType, MsgType *pType, MsgBody *pBody)
1814 {
1815         FILE *pFile2 = NULL;
1816         char *pData = NULL;
1817         int     length = 0;
1818
1819
1820         if (pType->offset && pType->size) {
1821                 /* header length & body length --------------------------- */
1822
1823                 length = __MmsBinaryEncodeUintvarLen(pType->size);
1824                 if (length == -1) {
1825                         MSG_DEBUG("MmsBinaryEncodeMsgPart: 1. headerLeng MmsBinaryEncodeUintvarLen fail \n");
1826                         goto __CATCH;
1827                 }
1828
1829                 if (__MmsBinaryEncodeUintvar(pFile, pType->size, length) == false) {
1830                         MSG_DEBUG("MmsBinaryEncodeMsgPart: 1. eaderLeng fail \n");
1831                         goto __CATCH;
1832                 }
1833
1834                 length = __MmsBinaryEncodeUintvarLen(pBody->size);
1835                 if (length == -1) {
1836                         MSG_DEBUG("MmsBinaryEncodeMsgPart: 1. bodyLeng MmsBinaryEncodeUintvarLen fail \n");
1837                         goto __CATCH;
1838                 }
1839
1840                 if (__MmsBinaryEncodeUintvar(pFile, pBody->size, length) == false) {
1841                         MSG_DEBUG("MmsBinaryEncodeMsgPart: 1. bodyLeng fail \n");
1842                         goto __CATCH;
1843                 }
1844
1845                 pFile2 = MsgOpenFile(pType->szOrgFilePath, "rb");
1846                 if (pFile != NULL) {
1847                         pData = (char *)malloc(pType->size);
1848                         if (pData == NULL)
1849                                 goto __CATCH;
1850
1851                         if (MsgFseek(pFile2, pType->offset, SEEK_SET) < 0) {
1852                                 MSG_DEBUG("MmsBinaryEncodeMsgPart: MsgFseek fail \n");
1853                                 goto __CATCH;
1854                         }
1855
1856                         ULONG nRead = 0;
1857
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");
1862                                         goto __CATCH;
1863                                 }
1864                                 pType->offset = __MmsGetEncodeOffset();
1865                                 if(MsgWriteFile(pData, sizeof(char), nRead, pFile) != (size_t)nRead) {
1866                                         MSG_DEBUG("MsgWriteFile failed");
1867                                         goto __CATCH;
1868                                 }
1869                                 gMmsEncodeCurOffset = MsgFtell(pFile);
1870                                 if(gMmsEncodeCurOffset < 0) {
1871                                         MSG_DEBUG("MsgFtell returns negative value [%ld]", gMmsEncodeCurOffset);
1872                                         goto __CATCH;
1873                                 }
1874                         }
1875
1876                         MsgCloseFile(pFile2);
1877                         pFile2 = NULL;
1878                 }
1879         } else {
1880                 int headerLeng     = 0;
1881                 int contentTypeLen = 0;
1882                 int contentHdrLen  = 0;
1883
1884                 /* header length & body length --------------------------- */
1885                 contentTypeLen = __MmsBinaryEncodeContentTypeLen(pType);
1886                 length = __MmsBinaryEncodeValueLengthLen(contentTypeLen);
1887                 contentHdrLen  = __MmsBinaryEncodeContentHeaderLen((MimeType)contentType, pType, true);
1888
1889                 if (contentTypeLen == -1 || length == -1 || contentHdrLen == -1) {
1890                         MSG_DEBUG("MmsBinaryEncodeMsgPart: headerLeng calculation fail \n");
1891                         goto __CATCH;
1892                 }
1893
1894                 headerLeng = contentTypeLen + contentHdrLen + length;
1895                 length = __MmsBinaryEncodeUintvarLen(headerLeng);
1896                 if (length == -1) {
1897                         MSG_DEBUG("MmsBinaryEncodeMsgPart: headerLeng MmsBinaryEncodeUintvarLen fail \n");
1898                         goto __CATCH;
1899                 }
1900
1901                 if (__MmsBinaryEncodeUintvar(pFile, headerLeng, length) == false) {
1902                         MSG_DEBUG("MmsBinaryEncodeMsgPart: headerLeng fail \n");
1903                         goto __CATCH;
1904                 }
1905
1906                 length = __MmsBinaryEncodeUintvarLen(pBody->size);
1907                 if (length == -1) {
1908                         MSG_DEBUG("MmsBinaryEncodeMsgPart: bodyLeng MmsBinaryEncodeUintvarLen fail \n");
1909                         goto __CATCH;
1910                 }
1911
1912                 if (__MmsBinaryEncodeUintvar(pFile, pBody->size, length) == false) {
1913                         MSG_DEBUG("MmsBinaryEncodeMsgPart: bodyLeng fail \n");
1914                         goto __CATCH;
1915                 }
1916
1917                 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
1918                                                                                         gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
1919                         MSG_DEBUG("MmsBinaryEncodeMsgPart: 2. header MsgWriteDataFromEncodeBuffer fail \n");
1920                         goto __CATCH;
1921                 }
1922
1923                 /* content-type & header --------------------------- */
1924                 pType->offset = __MmsGetEncodeOffset();
1925
1926                 if (__MmsBinaryEncodeContentType(pFile, pType, contentTypeLen) == false) {
1927                         MSG_DEBUG("MmsBinaryEncodeMsgPart: MmsBinaryEncodeContentType fail \n");
1928                         goto __CATCH;
1929                 }
1930
1931                 if (__MmsBinaryEncodeContentHeader(pFile, (MimeType)contentType, pType, true) == false) {
1932                         MSG_DEBUG("MmsBinaryEncodeMsgPart: MmsBinaryEncodeContentHeader fail \n");
1933                         goto __CATCH;
1934                 }
1935
1936                 pType->size = __MmsGetEncodeOffset() - pType->offset;
1937         }
1938
1939         if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos, gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
1940                 MSG_DEBUG("MmsBinaryEncodeMsgPart: contentBody MsgWriteDataFromEncodeBuffer fail \n");
1941                 goto __CATCH;
1942         }
1943
1944         /* content-body --------------------------- */
1945         if (__MmsBinaryEncodeContentBody(pFile, pBody) == false) {
1946                 MSG_DEBUG("MmsBinaryEncodeMsgPart: __MmsBinaryEncodeContentBody fail \n");
1947                 goto __CATCH;
1948         }
1949
1950         if (pData) {
1951                 free(pData);
1952                 pData = NULL;
1953         }
1954
1955         return true;
1956
1957 __CATCH:
1958
1959         if (pData) {
1960                 free(pData);
1961                 pData = NULL;
1962         }
1963
1964         MSG_DEBUG("## MmsBinaryEncodeMsgPart: failed\n");
1965         if (pFile2) {
1966                 MsgCloseFile(pFile2);
1967                 pFile2 = NULL;
1968         }
1969
1970         return false;
1971 }
1972
1973 static int __MmsBinaryEncodeContentHeaderLen(MimeType contentType, MsgType *pType, bool bMultipart)
1974 {
1975         int     length = 0;
1976         int     totalLength = 0;
1977         const char *szTextValue = NULL;
1978
1979
1980         MSG_DEBUG("MmsBinaryEncodeContentHeaderLen: S T A R T \n\n");
1981
1982         /* content-id ------------------------------------------------- */
1983         if (pType->szContentID[0]) {
1984                 if (bMultipart) { //Binary Encoding
1985                         totalLength++;
1986                 } else {
1987                         /* content-id = Quoted-string */
1988                         length = __MmsBinaryEncodeTextStringLen((UINT8*)"Content-ID");
1989                         if (length == -1) {
1990                                 MSG_DEBUG("MmsBinaryEncodeContentHeaderLen: Content-ID MmsBinaryEncodeTextStringLen fail.\n");
1991                                 goto __CATCH;
1992                         }
1993
1994                         totalLength += length;
1995                 }
1996
1997                 length = __MmsBinaryEncodeQuotedStringLen((UINT8*)pType->szContentID);
1998                 if (length == -1) {
1999                         MSG_DEBUG("MmsBinaryEncodeContentHeader: pType->szContentID MmsBinaryEncodeQuotedStringLen fail.\n");
2000                         goto __CATCH;
2001                 }
2002                 totalLength += length;
2003         }
2004
2005
2006         if (pType->szContentLocation[0]) {
2007                 if (bMultipart) { //Binary Encoding
2008                         totalLength++;
2009                 } else {
2010                         /* content-location = Quoted-string */
2011                         length = __MmsBinaryEncodeTextStringLen((UINT8*)"Content-Location");
2012                         if (length == -1) {
2013                                 MSG_DEBUG("MmsBinaryEncodeContentHeader: Content-Location MmsBinaryEncodeTextStringLen fail.\n");
2014                                 goto __CATCH;
2015                         }
2016
2017                         totalLength += length;
2018                 }
2019
2020                 length = __MmsBinaryEncodeTextStringLen((UINT8*)pType->szContentLocation);
2021                 if (length == -1) {
2022                         MSG_DEBUG("MmsBinaryEncodeContentHeader: pType->szContentLocation MmsBinaryEncodeTextStringLen fail.\n");
2023                         goto __CATCH;
2024                 }
2025
2026                 totalLength += length;
2027         }
2028
2029
2030         /* MIME_APPLICATION_VND_WAP_MULTIPART_RELATEDrequires always "inline" */
2031
2032         if (contentType != MIME_APPLICATION_VND_WAP_MULTIPART_RELATED &&
2033                 contentType != MIME_MULTIPART_RELATED &&
2034                 pType->disposition != INVALID_VALUE) {
2035
2036                 /*
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
2042                  */
2043
2044                 if (bMultipart) { //Binary Encoding
2045                         totalLength += 3;
2046                 } else {
2047                         /* content-disposition = Quoted-string */
2048                         szTextValue = MmsGetTextValue(MmsCodeMsgDisposition, pType->disposition);
2049
2050                         if (szTextValue) {
2051                                 length = __MmsBinaryEncodeTextStringLen((UINT8*)"Content-Disposition");
2052                                 if (length == -1) {
2053                                         MSG_DEBUG("MmsBinaryEncodeContentHeader: Content-Disposition MmsBinaryEncodeTextStringLen fail.\n");
2054                                         goto __CATCH;
2055                                 }
2056
2057                                 totalLength += length;
2058
2059                                 length = __MmsBinaryEncodeTextStringLen((UINT8*)szTextValue);
2060                                 if (length == -1) {
2061                                         MSG_DEBUG("MmsBinaryEncodeContentHeader: Content-Disposition MmsBinaryEncodeTextStringLen fail.\n");
2062                                         goto __CATCH;
2063                                 }
2064                                 totalLength += length;
2065                         }
2066                 }
2067         }
2068
2069         return totalLength;
2070
2071 __CATCH:
2072         MSG_DEBUG("## MmsBinaryEncodeContentHeadeLen: failed");
2073
2074         return -1;
2075 }
2076
2077 static bool __MmsBinaryEncodeContentHeader(FILE *pFile, MimeType contentType, MsgType *pType, bool bMultipart)
2078 {
2079         int     length = 0;
2080         const char *szTextValue = NULL;
2081
2082         MSG_DEBUG("MmsBinaryEncodeContentHeader: S T A R T \n\n");
2083
2084         /* content-id ------------------------------------------------- */
2085         if (pType->szContentID[0]) {
2086                 if (bMultipart) { //Binary Encoding
2087                         gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = MmsGetBinaryValue(MmsCodeMsgBodyHeaderCode, MMS_BODYHDR_CONTENTID) | 0x80;
2088                 } else {
2089                         /* content-id = Quoted-string */
2090                         length = __MmsBinaryEncodeTextStringLen((UINT8*)"Content-ID");
2091                         if (length == -1) {
2092                                 MSG_DEBUG("MmsBinaryEncodeContentHeader: Content-ID MmsBinaryEncodeTextStringLen fail.\n");
2093                                 goto __CATCH;
2094                         }
2095
2096                         if (__MmsBinaryEncodeTextString(pFile, (UINT8*)"Content-ID", length) == false) {
2097                                 MSG_DEBUG("MmsBinaryEncodeContentHeader: Content-ID MmsBinaryEncodeTextString fail.\n");
2098                                 goto __CATCH;
2099                         }
2100                 }
2101
2102                 length = __MmsBinaryEncodeQuotedStringLen((UINT8*)pType->szContentID);
2103                 if (length == -1) {
2104                         MSG_DEBUG("MmsBinaryEncodeContentHeader: pType->szContentID MmsBinaryEncodeQuotedStringLen fail.\n");
2105                         goto __CATCH;
2106                 }
2107
2108                 if (__MmsBinaryEncodeQuotedString(pFile, (UINT8*)pType->szContentID, length) == false) {
2109                         MSG_DEBUG("MmsBinaryEncodeContentHeader: pType->szContentID MmsBinaryEncodeQuotedString fail.\n");
2110                         goto __CATCH;
2111                 }
2112         }
2113
2114         if (pType->szContentLocation[0]) {
2115                 if (bMultipart) { //Binary Encoding
2116                         gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = MmsGetBinaryValue(MmsCodeMsgBodyHeaderCode, MMS_BODYHDR_CONTENTLOCATION) | 0x80;
2117                 } else {
2118                         /* content-location = Quoted-string */
2119                         length = __MmsBinaryEncodeTextStringLen((UINT8*)"Content-Location");
2120                         if (length == -1) {
2121                                 MSG_DEBUG("MmsBinaryEncodeContentHeader: Content-Location MmsBinaryEncodeTextStringLen fail.\n");
2122                                 goto __CATCH;
2123                         }
2124
2125                         if (__MmsBinaryEncodeTextString(pFile, (UINT8*)"Content-Location", length) == false) {
2126                                 MSG_DEBUG("MmsBinaryEncodeContentHeader: Content-Location MmsBinaryEncodeTextString fail.\n");
2127                                 goto __CATCH;
2128                         }
2129                 }
2130
2131                 length = __MmsBinaryEncodeTextStringLen((UINT8*)pType->szContentLocation);
2132                 if (length == -1) {
2133                         MSG_DEBUG("MmsBinaryEncodeContentHeader: pType->szContentLocation MmsBinaryEncodeTextStringLen fail.\n");
2134                         goto __CATCH;
2135                 }
2136
2137                 if (__MmsBinaryEncodeTextString(pFile, (UINT8*)pType->szContentLocation, length) == false) {
2138                         MSG_DEBUG("MmsBinaryEncodeContentHeader: pType->szContentLocation MmsBinaryEncodeTextString fail.\n");
2139                         goto __CATCH;
2140                 }
2141         }
2142
2143         /* MIME_APPLICATION_VND_WAP_MULTIPART_RELATEDrequires always "inline" */
2144
2145         if (contentType != MIME_APPLICATION_VND_WAP_MULTIPART_RELATED &&
2146                 contentType != MIME_MULTIPART_RELATED &&
2147                 pType->disposition != INVALID_VALUE) {
2148
2149                 /*
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
2155                  */
2156
2157                 if (bMultipart) {//Binary Encoding
2158
2159                         UINT8 fieldValue  = 0xff;
2160                         gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = MmsGetBinaryValue(MmsCodeMsgBodyHeaderCode, MMS_BODYHDR_DISPOSITION) | 0x80;
2161
2162                         fieldValue = MmsGetBinaryValue(MmsCodeMsgDisposition, pType->disposition) | 0x80;
2163
2164                         if (fieldValue == 0xff)
2165                                 fieldValue = 0x81;
2166
2167                         gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = 0x01;
2168                         gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = fieldValue;
2169                 } else {
2170                         /* content-disposition = Disposition (no support parameter) */
2171
2172                         szTextValue = MmsGetTextValue(MmsCodeMsgDisposition, pType->disposition);
2173
2174                         if (szTextValue) {
2175                                 length = __MmsBinaryEncodeTextStringLen((UINT8*)"Content-Disposition");
2176                                 if (length == -1) {
2177                                         MSG_DEBUG("MmsBinaryEncodeContentHeader: Content-Disposition MmsBinaryEncodeTextStringLen fail.\n");
2178                                         goto __CATCH;
2179                                 }
2180
2181                                 if (__MmsBinaryEncodeTextString(pFile, (UINT8*)"Content-Disposition", length) == false) {
2182                                         MSG_DEBUG("MmsBinaryEncodeContentHeader: Content-Disposition MmsBinaryEncodeTextString fail.\n");
2183                                         goto __CATCH;
2184                                 }
2185
2186                                 length = __MmsBinaryEncodeTextStringLen((UINT8*)szTextValue);
2187                                 if (length == -1) {
2188                                         MSG_DEBUG("MmsBinaryEncodeContentHeader: Content-Disposition MmsBinaryEncodeTextStringLen fail.\n");
2189                                         goto __CATCH;
2190                                 }
2191
2192                                 if (__MmsBinaryEncodeTextString(pFile, (UINT8*)szTextValue, length) == false) {
2193                                         MSG_DEBUG("MmsBinaryEncodeContentHeader: Content-Disposition MmsBinaryEncodeTextString fail.\n");
2194                                         goto __CATCH;
2195                                 }
2196                         }
2197                 }
2198         }
2199
2200         return true;
2201
2202 __CATCH:
2203         MSG_DEBUG("## MmsBinaryEncodeContentHeader: failed");
2204         return false;
2205 }
2206
2207 static bool __MmsBinaryEncodeContentBody(FILE *pFile, MsgBody *pBody)
2208 {
2209         int nRead = 0;
2210         char *pData = NULL;
2211
2212
2213         if (pFile == NULL)
2214                 goto __CATCH;
2215
2216         if (pBody == NULL)
2217                 return true;
2218
2219         if (pBody->szOrgFilePath[0]) {
2220                 pData = MsgOpenAndReadMmsFile(pBody->szOrgFilePath, pBody->offset, pBody->size, &nRead);
2221                 if (pData == NULL)
2222                         goto __CATCH;
2223
2224                 pBody->offset = __MmsGetEncodeOffset();
2225                 if(MsgWriteFile(pData, sizeof(char), nRead, pFile) != (size_t)nRead) {
2226                         MSG_DEBUG("MsgWriteFile failed");
2227                         goto __CATCH;
2228                 }
2229                 gMmsEncodeCurOffset = MsgFtell(pFile);
2230
2231                 if (gMmsEncodeCurOffset < 0) {
2232                         MSG_DEBUG("MsgFtell returns negative value [%ld]", gMmsEncodeCurOffset);
2233                         goto __CATCH;
2234                 }
2235
2236                 if (pData) {
2237                         free(pData);
2238                         pData = NULL;
2239                 }
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");
2244                         goto __CATCH;
2245                 }
2246                 gMmsEncodeCurOffset = MsgFtell(pFile);
2247
2248                 if(gMmsEncodeCurOffset < 0) {
2249                         MSG_DEBUG("MsgFtell returns negative value [%ld]", gMmsEncodeCurOffset);
2250                         goto __CATCH;
2251                 }
2252         }
2253
2254         return true;
2255
2256 __CATCH:
2257         MSG_DEBUG("## __MmsBinaryEncodeContentBody: failed\n");
2258         if (pData) {
2259                 free(pData);
2260                 pData = NULL;
2261         }
2262
2263         return false;
2264 }
2265
2266 /* =========================================================================
2267
2268    B  I  N  A  R  Y       E  N  C  O  D  I  N  G      U  T  I  L  I  T  Y
2269
2270    =========================================================================*/
2271 static int __MmsBinaryEncodeIntegerLen(UINT32 integer)
2272 {
2273         if (integer < 0x80) {
2274                 /* short-integer */
2275                 return 1;
2276         } else {
2277
2278                 int     length = 0;
2279                 /*
2280                  * Long-integer = Short-length Multi-octet-integer
2281                  * The Short-length indicates the length of the Multi-octet-integer
2282                  */
2283
2284                 while (integer) {
2285                         length++;
2286                         integer = (integer >> 8);
2287                 }
2288
2289                 length++;       // + Short-length
2290
2291                 return length;
2292         }
2293 }
2294
2295
2296 /*
2297  * This makes value-length by specified integer value
2298  *
2299  * @param       length [in] gotten from MmsBinaryEncodeIntegerLen()
2300  */
2301 static bool __MmsBinaryEncodeInteger(FILE *pFile, UINT32 integer, int length)
2302 {
2303         union {
2304                 UINT32 integer;
2305                 UINT8 seg[4];
2306         } changer;
2307
2308
2309         if (pFile == NULL) {
2310                 MSG_DEBUG("MmsBinaryEncodeInteger: source == NULL \n");
2311                 goto __CATCH;
2312         }
2313
2314         if ((gMmsEncodeMaxLen - gCurMmsEncodeBuffPos) < length) {
2315                 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
2316                                                                                         gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
2317                         MSG_DEBUG("MmsBinaryEncodeInteger: MsgWriteDataFromEncodeBuffer fail \n");
2318                         goto __CATCH;
2319                 }
2320         }
2321
2322         if (integer < 0x80) {
2323                 /* short-integer */
2324                 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = (UINT8)integer | 0x80;
2325         } else {
2326                 /*
2327                  * Long-integer = Short-length Multi-octet-integer
2328                  * The Short-length indicates the length of the Multi-octet-integer
2329                  */
2330                 changer.integer = integer;
2331                 length--;                                       // - "Short-length"
2332
2333                 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = (UINT8)length;
2334
2335                 int i;
2336                 for(i = 0; i < length; i++)
2337                         gpMmsEncodeBuf[gCurMmsEncodeBuffPos + i] = changer.seg[length - (i + 1)];
2338
2339                 gCurMmsEncodeBuffPos += length;
2340         }
2341
2342         return true;
2343
2344 __CATCH:
2345         return false;
2346 }
2347
2348 static int __MmsBinaryEncodeLongIntegerLen(UINT32 integer)
2349 {
2350         int length = 0;
2351
2352         /*
2353          * Long-integer = Short-length Multi-octet-integer
2354          * The Short-length indicates the length of the Multi-octet-integer
2355          */
2356
2357         if (integer == 0)
2358                 return 2;
2359
2360         while (integer) {
2361                 length++;
2362                 integer = (integer >> 8);
2363         }
2364
2365         length++;       // + Short-length
2366
2367         return length;
2368 }
2369
2370 /*
2371  * This makes value-length by specified integer value
2372  *
2373  * @param       length [in] gotten from MmsBinaryEncodeIntegerLen()
2374  */
2375 static bool __MmsBinaryEncodeLongInteger(FILE *pFile, UINT32 integer, int length)
2376 {
2377         int i  = 0;
2378         union {
2379                 UINT32 integer;
2380                 UINT8 seg[4];
2381         }changer;
2382
2383
2384         /*
2385          * Long-integer = Short-length Multi-octet-integer
2386          * The Short-length indicates the length of the Multi-octet-integer
2387          */
2388
2389         if (pFile == NULL) {
2390                 MSG_DEBUG("MmsBinaryEncodeLongInteger: source == NULL \n");
2391                 goto __CATCH;
2392         }
2393
2394         if ((gMmsEncodeMaxLen - gCurMmsEncodeBuffPos) < length) {
2395                 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
2396                                                                                         gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
2397                         MSG_DEBUG("MmsBinaryEncodeLongInteger: MsgWriteDataFromEncodeBuffer fail \n");
2398                         goto __CATCH;
2399                 }
2400         }
2401
2402         changer.integer = integer;
2403         length--;                                       // - "Short-length"
2404
2405         gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = (UINT8)length;
2406
2407         for(i = 0; i < length; i++)
2408                 gpMmsEncodeBuf[gCurMmsEncodeBuffPos + i] = changer.seg[length - (i + 1)];
2409
2410         gCurMmsEncodeBuffPos += length;
2411
2412         return true;
2413
2414 __CATCH:
2415         return false;
2416 }
2417
2418 static int __MmsBinaryEncodeTextStringLen(UINT8 *source)
2419 {
2420         int      length = 0;
2421
2422         MSG_DEBUG("MmsBinaryEncodeTextStringLen: \n");
2423
2424         if (source == NULL) {
2425                 MSG_DEBUG("MmsBinaryEncodeTextStringLen: source == NULL \n");
2426                 return -1;
2427         }
2428
2429         length = (int)strlen((char*)source);
2430         if (source[0] > 0x7F) {
2431                 length += 2;                    // + NULL
2432         } else {
2433                 length++;                               // + NULL
2434         }
2435
2436         return length;
2437 }
2438
2439 /*
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
2444 */
2445 static bool __MmsBinaryEncodeTextString(FILE *pFile, UINT8 *source, int length)
2446 {
2447
2448         MSG_DEBUG("MmsBinaryEncodeTextString: \n");
2449
2450         /*
2451          * make text string
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>
2457          */
2458
2459         if (pFile == NULL || source == NULL) {
2460                 MSG_DEBUG("MmsBinaryEncodeTextString: source == NULL \n");
2461                 goto __CATCH;
2462         }
2463
2464         if ((gMmsEncodeMaxLen - gCurMmsEncodeBuffPos) < length) {
2465                 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
2466                                                                                         gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
2467                         MSG_DEBUG("MmsBinaryEncodeTextString: MsgWriteDataFromEncodeBuffer fail \n");
2468                         goto __CATCH;
2469                 }
2470         }
2471
2472         if (source[0] > 0x7F) {
2473                 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = QUOTE;
2474                 length--;
2475         }
2476
2477         strncpy(gpMmsEncodeBuf + gCurMmsEncodeBuffPos, (char*)source, (length - 1));    // except NULL
2478         gCurMmsEncodeBuffPos += (length - 1);                   // except NULL
2479         gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = (UINT8)NULL;
2480
2481         return true;
2482
2483 __CATCH:
2484         return false;
2485 }
2486
2487 /*
2488  * Encode 28bit unsigned integer(Maximum) to uintvar
2489  *
2490  * @param       interger [in] integer to be encoded
2491  * @return      encoded UINTVAR stream
2492 */
2493 const UINT32 UINTVAR_LENGTH_1 =  0x0000007f;            //7bit
2494 const UINT32 UINTVAR_LENGTH_2 =  0x00003fff;            //14bit
2495 const UINT32 UINTVAR_LENGTH_3 =  0x001fffff;            //21bit
2496
2497
2498 static int __MmsBinaryEncodeUintvarLen(UINT32 integer)
2499 {
2500         UINT32          length  = 0;
2501
2502         /* Find encoded unitvar length */
2503         if (integer  <= UINTVAR_LENGTH_1) {
2504                 length = 1;
2505         } else {
2506                 if (integer <= UINTVAR_LENGTH_2) {
2507                         length = 2;
2508                 } else {
2509                         if (integer <= UINTVAR_LENGTH_3) {
2510                                 length = 3;
2511                         } else {
2512                                 length = 4;
2513                         }
2514                 }
2515         }
2516
2517         return length;
2518 }
2519
2520 static bool __MmsBinaryEncodeUintvar(FILE *pFile, UINT32 integer, int length)
2521 {
2522         const char ZERO = 0x00;
2523         int i = 2;
2524         char szReverse[MSG_STDSTR_LONG] = {0, };
2525
2526         union {
2527                 UINT32  integer;
2528                 char    bytes[4];
2529         } source;
2530
2531         if (pFile == NULL) {
2532                 MSG_DEBUG("MmsBinaryEncodeUintvar: pFile == INVALID_HBOJ \n");
2533                 goto __CATCH;
2534         }
2535
2536         if ((gMmsEncodeMaxLen - gCurMmsEncodeBuffPos) < length) {
2537                 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
2538                                                                                         gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
2539                         MSG_DEBUG("MmsBinaryEncodeUintvar: MsgWriteDataFromEncodeBuffer fail \n");
2540                         goto __CATCH;
2541                 }
2542         }
2543
2544         source.integer = integer;
2545         memset(szReverse, 0, MSG_STDSTR_LONG);
2546
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;
2551
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;
2557
2558                 /* Retrive 1 encode uintvar */
2559                 szReverse[i-1] = source.bytes[0];
2560                 szReverse[i-1] = szReverse[i-1] | 0x80;
2561                 i++;
2562         }
2563
2564         for(i=0; i < length; i++)
2565                 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = szReverse[length - i - 1];
2566
2567         return true;
2568
2569 __CATCH:
2570         return false;
2571 }
2572
2573 static int __MmsBinaryEncodeValueLengthLen(UINT32 integer)
2574 {
2575         int length = 0;
2576
2577         if (integer < 0x1f) {
2578                 length = 1;
2579         } else {
2580                 length = __MmsBinaryEncodeUintvarLen(integer) + 1;              //LENGTH_QUOTE
2581         }
2582
2583         return length;
2584 }
2585
2586 /*
2587  * This makes value-length by specified integer value
2588  *
2589  * @param       length [in] from MmsBinaryEncodeValueLengthLen()
2590  * @return      encoded value-length
2591  */
2592 static bool __MmsBinaryEncodeValueLength(FILE *pFile, UINT32 integer, int length)
2593 {
2594         /*
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
2600          */
2601
2602         if (pFile == NULL) {
2603                 MSG_DEBUG("MmsBinaryEncodeValueLength: pFile == INVALID_HBOJ \n");
2604                 goto __CATCH;
2605         }
2606
2607         if ((gMmsEncodeMaxLen - gCurMmsEncodeBuffPos) < length) {
2608                 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
2609                                                                                         gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
2610                         MSG_DEBUG("MmsBinaryEncodeValueLength: MsgWriteDataFromEncodeBuffer fail \n");
2611                         goto __CATCH;
2612                 }
2613         }
2614
2615         if (integer < 0x1F) {
2616                 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = (UINT8)integer;
2617         } else {
2618                 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = (UINT8)LENGTH_QUOTE;
2619                 if (__MmsBinaryEncodeUintvar(pFile, integer, length - 1) == false) {    // LENGTH_QUOTE
2620                         MSG_DEBUG("MmsBinaryEncodeValueLength: MmsBinaryEncodeUintvar fail\n");
2621                         goto __CATCH;
2622                 }
2623         }
2624
2625         return true;
2626
2627 __CATCH:
2628         return false;
2629 }
2630
2631 static int __MmsBinaryEncodeQuotedStringLen(UINT8 *pSrc)
2632 {
2633         if (pSrc == NULL) {
2634                 MSG_DEBUG("MmsBinaryEncodeQuotedStringLen: invalid file\n");
2635                 goto __CATCH;
2636         }
2637
2638         return (strlen((char*)pSrc) + 2);       // QUOTE + NULL
2639
2640 __CATCH:
2641         return -1;
2642 }
2643
2644 /*
2645  * make quoted string
2646  * Quoted-string = <Octet 34> *TEXT End-of-string
2647  *
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
2652 */
2653 static bool __MmsBinaryEncodeQuotedString(FILE *pFile, UINT8 *source, int length)
2654 {
2655         if (source == NULL || pFile == NULL) {
2656                 MSG_DEBUG("MmsBinaryEncodeQuotedString: invalid file\n");
2657                 goto __CATCH;
2658         }
2659
2660         if ((gMmsEncodeMaxLen - gCurMmsEncodeBuffPos) < length) {
2661                 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
2662                                                                                         gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
2663                         MSG_DEBUG("MmsBinaryEncodeQuotedString: MsgWriteDataFromEncodeBuffer fail \n");
2664                         goto __CATCH;
2665                 }
2666         }
2667
2668         gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = '\"';
2669         strncpy(gpMmsEncodeBuf + gCurMmsEncodeBuffPos, (char*)source, length - 2);              // except '\"' & NULL
2670         gCurMmsEncodeBuffPos += (length - 2);
2671         gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = (UINT8)NULL;
2672
2673         return true;
2674
2675 __CATCH:
2676         return false;
2677 }
2678
2679 static int __MmsBinaryEncodeEncodedStringLen(UINT8 *source)
2680 {
2681         UINT32 charset = 0x6A;          // default = utf-8
2682         int charLeng            = 0;
2683         int textLeng            = 0;
2684         int valueLengthLen      = 0;
2685
2686
2687         MSG_DEBUG("MmsBinaryEncodeEncodedStringLen: \n");
2688
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");
2694                 goto __CATCH;;
2695         }
2696
2697         textLeng = __MmsBinaryEncodeTextStringLen((UINT8*)source);
2698         if (textLeng == -1)     {
2699                 MSG_DEBUG("MmsBinaryEncodeEncodedStringLen : textLeng MmsBinaryEncodeTextStringLen fail.\n");
2700                 goto __CATCH;;
2701         }
2702
2703         valueLengthLen = __MmsBinaryEncodeValueLengthLen(charLeng + textLeng);
2704         if (valueLengthLen == -1) {
2705                 MSG_DEBUG("MmsBinaryEncodeEncodedStringLen : valLengthLen MmsBinaryEncodeTextStringLen fail.\n");
2706                 goto __CATCH;
2707         }
2708
2709         return (charLeng + textLeng + valueLengthLen);
2710
2711 __CATCH:
2712         return -1;
2713 }
2714
2715 /*
2716  * This makes value-length by specified integer value
2717  *
2718  * @param       length [in] from MmsBinaryEncodeEncodedStringLen()
2719  * @return      encoded encoded-string
2720  */
2721 static bool __MmsBinaryEncodeEncodedString(FILE *pFile, UINT8 *source, int length)
2722 {
2723         UINT32 charset = 0x6A;          // default = utf-8
2724         int charLeng = 0;
2725         int textLeng = 0;
2726         int valLengthLen = 0;
2727
2728
2729         MSG_DEBUG("MmsBinaryEncodeEncodedString: \n");
2730
2731         /* value-length charSet text-string */
2732
2733         if (pFile == NULL || source == NULL) {
2734                 MSG_DEBUG("MmsBinaryEncodeEncodedString: invalid input parameter\n");
2735                 goto __CATCH;
2736         }
2737
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");
2742                 goto __CATCH;;
2743         }
2744
2745         textLeng = __MmsBinaryEncodeTextStringLen((UINT8*)source);
2746         if (textLeng == -1) {
2747                 MSG_DEBUG("MmsBinaryEncodeEncodedString : textLeng MmsBinaryEncodeTextStringLen fail.\n");
2748                 goto __CATCH;;
2749         }
2750
2751         valLengthLen = __MmsBinaryEncodeValueLengthLen(charLeng + textLeng);
2752         if (valLengthLen == -1) {
2753                 MSG_DEBUG("MmsBinaryEncodeEncodedString : MmsBinaryEncodeValueLengthLen fail.\n");
2754                 goto __CATCH;
2755         }
2756
2757         if (length != (charLeng + textLeng + valLengthLen))
2758         {
2759                 MSG_DEBUG("MmsBinaryEncodeEncodedString: invalid length\n");
2760                 goto __CATCH;
2761         }
2762
2763         if ((gMmsEncodeMaxLen - gCurMmsEncodeBuffPos) < length) {
2764                 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
2765                                                                                         gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
2766                         MSG_DEBUG("MmsBinaryEncodeEncodedString: MsgWriteDataFromEncodeBuffer fail \n");
2767                         goto __CATCH;
2768                 }
2769         }
2770
2771         /* Value length of charset value and text string */
2772         if (__MmsBinaryEncodeValueLength(pFile, charLeng + textLeng, valLengthLen) == false) {
2773                 MSG_DEBUG("MmsBinaryEncodeEncodedString : MmsBinaryEncodeValueLength fail.\n");
2774                 goto __CATCH;
2775         }
2776
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");
2780                 goto __CATCH;
2781         }
2782
2783
2784         /* Write text string on buffer */
2785         if (__MmsBinaryEncodeTextString(pFile, (UINT8*)source, textLeng) == false) {
2786                 MSG_DEBUG("MmsBinaryEncodeEncodedString : MmsBinaryEncodeTextString fail.\n");
2787                 goto __CATCH;
2788         }
2789
2790         return true;
2791
2792 __CATCH:
2793         return false;
2794 }
2795
2796 static  bool __MmsBinaryEncodeFieldCodeAndValue(FILE *pFile, UINT8 fieldCode, UINT8 fieldValue)
2797 {
2798         if ((gMmsEncodeMaxLen - gCurMmsEncodeBuffPos) < 2) {
2799                 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
2800                                                                                 gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
2801                         MSG_DEBUG("__MmsBinaryEncodeFieldCodeAndValue: MsgWriteDataFromEncodeBuffer fail \n");
2802                         goto __CATCH;
2803                 }
2804         }
2805
2806         if (fieldCode == 0xff) {
2807                 MSG_DEBUG("__MmsBinaryEncodeFieldCodeAndValue: invalid fieldCode \n");
2808                 goto __CATCH;
2809         }
2810
2811         if (fieldValue == 0xff) {
2812                 MSG_DEBUG("__MmsBinaryEncodeFieldCodeAndValue: invalid fieldValue \n");
2813                 return true;
2814         }
2815
2816         gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = fieldCode;
2817         gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = fieldValue;
2818
2819         return true;
2820
2821 __CATCH:
2822         return false;
2823 }
2824
2825 static bool __MmsBinaryEncodeTrID(FILE *pFile, char *szTrID, int bufLen)
2826 {
2827         int     length    = 0;
2828         UINT8 fieldCode = 0xff;
2829         char szBuff[MMS_TR_ID_LEN + 1] = {0, };
2830         struct  tm      *dateTime = NULL;
2831         time_t  RawTime = 0;
2832         time_t  dateSec = 0;
2833
2834         time(&RawTime);
2835         dateTime = localtime(&RawTime);
2836         dateSec = mktime(dateTime);
2837
2838
2839         fieldCode = MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_TRID) | 0x80;
2840         if (fieldCode == 0xff) {
2841                 MSG_DEBUG("__MmsBinaryEncodeTrID: invalid fieldCode \n");
2842                 goto __CATCH;
2843         }
2844
2845         snprintf(szBuff, MMS_TR_ID_LEN + 1, "%lu.%lu", dateSec, (unsigned long)random());
2846         MSG_DEBUG("__MmsBinaryEncodeTrID: 2. szBuff = %s\n", szBuff);
2847
2848         length = __MmsBinaryEncodeTextStringLen((UINT8*)szBuff);
2849         if (length == -1) {
2850                 MSG_DEBUG("__MmsBinaryEncodeTrID: MmsBinaryEncodeTextStringLen fail \n");
2851                 goto __CATCH;
2852         }
2853
2854         if ((gMmsEncodeMaxLen - gCurMmsEncodeBuffPos) < (length + 1)) {
2855                 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
2856                                                                                         gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
2857                         MSG_DEBUG("__MmsBinaryEncodeTrID: MsgWriteDataFromEncodeBuffer fail \n");
2858                         goto __CATCH;
2859                 }
2860         }
2861
2862
2863         gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = fieldCode;
2864         if (__MmsBinaryEncodeTextString(pFile, (UINT8*)szBuff, length) == false) {
2865                 MSG_DEBUG("__MmsBinaryEncodeTrID: MmsBinaryEncodeTextString fail\n");
2866                 goto __CATCH;
2867         }
2868
2869         if (szTrID) {
2870                 memset(szTrID, 0, bufLen);
2871                 strncpy(szTrID, szBuff, bufLen - 1);
2872         }
2873
2874         return true;
2875
2876 __CATCH:
2877
2878         return false;
2879 }
2880
2881 static bool __MmsBinaryEncodeMsgID(FILE *pFile, const char *szMsgID)
2882 {
2883         int     length = 0;
2884         UINT8 fieldCode = 0xff;
2885
2886         fieldCode = MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_MSGID) | 0x80;
2887         if (fieldCode == 0xff) {
2888                 MSG_DEBUG("__MmsBinaryEncodeTrID: invalid fieldCode \n");
2889                 goto __CATCH;
2890         }
2891
2892         MSG_DEBUG("__MmsBinaryEncodeMsgID: 2. szBuff = %s\n", szMsgID);
2893
2894         length = __MmsBinaryEncodeTextStringLen((UINT8*)szMsgID);
2895         if (length == -1) {
2896                 MSG_DEBUG("__MmsBinaryEncodeMsgID: MmsBinaryEncodeTextStringLen fail \n");
2897                 goto __CATCH;
2898         }
2899
2900         if ((gMmsEncodeMaxLen - gCurMmsEncodeBuffPos) < (length + 1)) {
2901                 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
2902                                                                                 gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
2903                         MSG_DEBUG("__MmsBinaryEncodeTrID: MsgWriteDataFromEncodeBuffer fail \n");
2904                         goto __CATCH;
2905                 }
2906         }
2907
2908         gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = fieldCode;
2909         if (__MmsBinaryEncodeTextString(pFile, (UINT8*)szMsgID, length) == false)       {
2910                 MSG_DEBUG("__MmsBinaryEncodeTrID: MmsBinaryEncodeTextString fail\n");
2911                 goto __CATCH;
2912         }
2913
2914         return true;
2915
2916 __CATCH:
2917
2918         return false;
2919 }
2920
2921 static bool __MmsBinaryEncodeMmsVersion(FILE *pFile)
2922 {
2923         UINT8 majorVer = MMS_MAJOR_VERSION;
2924         UINT8 minorVer = MMS_MINOR_VERSION;
2925
2926         MSG_DEBUG("__MmsBinaryEncodeMmsVersion: \n");
2927
2928         if (pFile == NULL) {
2929                 MSG_DEBUG("__MmsBinaryEncodeMmsVersion: invalid input file \n");
2930                 goto __CATCH;
2931         }
2932
2933         if ((gMmsEncodeMaxLen - gCurMmsEncodeBuffPos) < 2) {
2934                 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
2935                                                                                 gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
2936                         MSG_DEBUG("__MmsBinaryEncodeMmsVersion: MsgWriteDataFromEncodeBuffer fail \n");
2937                         goto __CATCH;
2938                 }
2939         }
2940
2941         gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_VERSION) | 0x80;
2942
2943         MSG_DEBUG("__MmsBinaryEncodeMmsVersion: major version (%d)\n", majorVer);
2944         MSG_DEBUG("__MmsBinaryEncodeMmsVersion: minor version (%d)\n", minorVer);
2945
2946         gpMmsEncodeBuf[gCurMmsEncodeBuffPos] = (majorVer << 4) | (minorVer & 0x0f) | MSB;
2947
2948         if (gpMmsEncodeBuf[gCurMmsEncodeBuffPos] < 0x80) {
2949                 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] |= 0x80;
2950         } else {
2951                 gCurMmsEncodeBuffPos++;
2952         }
2953
2954         return true;
2955
2956 __CATCH:
2957         return false;
2958 }
2959
2960 static bool __MmsBinaryEncodeDate(FILE *pFile)
2961 {
2962         struct  tm      *dateTime = NULL;
2963         time_t  dateSec = 0;
2964
2965         dateSec = time(NULL);
2966         dateTime = localtime(&dateSec);
2967
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);
2970
2971         if (dateSec > 0) {
2972                 int     length  = 0;
2973                 length = __MmsBinaryEncodeLongIntegerLen(dateSec);
2974
2975                 if ((gMmsEncodeMaxLen - gCurMmsEncodeBuffPos) < (length + 1)) { // + fieldCode
2976                         if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
2977                                                                                                 gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
2978                                 MSG_DEBUG("__MmsBinaryEncodeDate: MsgWriteDataFromEncodeBuffer fail \n");
2979                                 goto __CATCH;
2980                         }
2981                 }
2982
2983                 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_DATE) | 0x80;
2984
2985                 if (__MmsBinaryEncodeLongInteger(pFile, dateSec, length) == false) {
2986                         MSG_DEBUG("__MmsBinaryEncodeDate: date MmsBinaryEncodeLongInteger error\n");
2987                         goto __CATCH;
2988                 }
2989         } else {
2990                 MSG_DEBUG("__MmsBinaryEncodeDate: date has a negative value (%d) \n", dateSec);
2991                 goto __CATCH;
2992         }
2993         return true;
2994
2995 __CATCH:
2996         return false;
2997 }
2998
2999 static bool __MmsBinaryEncodeFrom(FILE *pFile)
3000 {
3001         if (pFile == NULL) {
3002                 MSG_DEBUG("__MmsBinaryEncodeFrom: invalid input file \n");
3003                 goto __CATCH;
3004         }
3005
3006         if ((gMmsEncodeMaxLen - gCurMmsEncodeBuffPos) < 3) {
3007                 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
3008                                                                                         gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
3009                         MSG_DEBUG("__MmsBinaryEncodeFrom: MsgWriteDataFromEncodeBuffer fail \n");
3010                         goto __CATCH;
3011                 }
3012         }
3013
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;
3018         return true;
3019
3020 __CATCH:
3021         return false;
3022 }
3023
3024 static bool __MmsBinaryEncodeOneAddress(FILE *pFile, MmsFieldCode addrType, char *szAddrStr)
3025 {
3026         int length = 0;
3027
3028         if (pFile == NULL) {
3029                 MSG_DEBUG("__MmsBinaryEncodeOneAddress: invalid input file \n");
3030                 goto __CATCH;
3031         }
3032
3033         /* EncodedString */
3034         length = __MmsBinaryEncodeEncodedStringLen((UINT8*)szAddrStr);
3035
3036         if (length == -1) {
3037                 MSG_DEBUG("__MmsBinaryEncodeOneAddress: MmsBinaryEncodeEncodedStringLen fail \n");
3038                 goto __CATCH;
3039         }
3040
3041         if ((gMmsEncodeMaxLen - gCurMmsEncodeBuffPos) < length + 1) {
3042                 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
3043                                                                                         gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
3044                         MSG_DEBUG("__MmsBinaryEncodeOneAddress: MsgWriteDataFromEncodeBuffer fail \n");
3045                         goto __CATCH;
3046                 }
3047         }
3048
3049         gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = MmsGetBinaryValue(MmsCodeFieldCode, addrType) | 0x80;
3050
3051
3052         if (__MmsBinaryEncodeEncodedString(pFile, (UINT8*)szAddrStr, length) == false) {
3053                 MSG_DEBUG("__MmsBinaryEncodeOneAddress: MmsBinaryEncodeEncodedString fail \n");
3054                 goto __CATCH;
3055         }
3056
3057         return true;
3058
3059 __CATCH:
3060         return false;
3061 }
3062
3063 static bool __MmsBinaryEncodeAddress(FILE *pFile, MmsFieldCode addrType, char *szAddr)
3064 {
3065         char *pSingleAddr = NULL;
3066
3067
3068         if (pFile == NULL) {
3069                 MSG_DEBUG("__MmsBinaryEncodeAddress: invalid input file \n");
3070                 goto __CATCH;
3071         }
3072
3073         while (szAddr && szAddr[0]) {
3074                 szAddr = _MsgSkipWS3(szAddr);
3075                 if (szAddr == NULL)
3076                         break;
3077
3078                 pSingleAddr = strchr(szAddr, MSG_CH_SEMICOLON);
3079                 if (pSingleAddr) {
3080
3081                         *pSingleAddr = MSG_CH_NULL;
3082                         if (__MmsBinaryEncodeOneAddress(pFile, addrType, szAddr) == false) {
3083                                 MSG_DEBUG("__MmsBinaryEncodeAddress: __MmsBinaryEncodeAddress fail\n");
3084                                 goto __CATCH;
3085                         }
3086                         *pSingleAddr = MSG_CH_SEMICOLON;
3087
3088                         szAddr           = pSingleAddr + 1;
3089                         pSingleAddr  = NULL;
3090                 } else {
3091                         if (__MmsBinaryEncodeOneAddress(pFile, addrType, szAddr) == false) {
3092                                 MSG_DEBUG("__MmsBinaryEncodeAddress: __MmsBinaryEncodeAddress fail\n");
3093                                 goto __CATCH;
3094                         }
3095
3096                         szAddr = NULL;
3097                 }
3098         }
3099
3100         return true;
3101
3102 __CATCH:
3103         return false;
3104 }
3105
3106 static bool __MmsBinaryEncodeTime(FILE *pFile, MmsFieldCode fieldCode, MmsTimeStruct time)
3107 {
3108         int             timeLen  = 0;
3109         int             length   = 0;
3110
3111         if (pFile == NULL) {
3112                 MSG_DEBUG("__MmsBinaryEncodeTime: invalid input file \n");
3113                 goto __CATCH;
3114         }
3115
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);
3120                 return true;
3121         }
3122
3123         /*
3124          * use temporary buffer to estimate value length
3125          * and copy it to pData buffer later.
3126          */
3127
3128         if (time.type == MMS_TIMETYPE_RELATIVE) {
3129                 timeLen = __MmsBinaryEncodeIntegerLen(time.time);
3130         } else {
3131                 timeLen = __MmsBinaryEncodeLongIntegerLen(time.time);
3132         }
3133
3134         if (timeLen <= 0) {
3135                 MSG_DEBUG("__MmsBinaryEncodeTime: MmsBinaryEncodeLongIntegerLen fail \n");
3136                 goto __CATCH;
3137         }
3138
3139         length = __MmsBinaryEncodeValueLengthLen(timeLen + 1);  //time length + time type token
3140         if (length == -1) {
3141                 MSG_DEBUG("__MmsBinaryEncodeTime: MmsBinaryEncodeValueLengthLen fail \n");
3142                 goto __CATCH;
3143         }
3144
3145         if ((gMmsEncodeMaxLen - gCurMmsEncodeBuffPos) < (length + timeLen + 2)) {       // + fieldCode + timeType
3146
3147                 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
3148                                                                                         gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
3149                         MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: MsgWriteDataFromEncodeBuffer fail \n");
3150                         goto __CATCH;
3151                 }
3152         }
3153
3154         /* fieldCode */
3155         gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = MmsGetBinaryValue(MmsCodeFieldCode, fieldCode) | 0x80;
3156
3157         /* value length */
3158         if (__MmsBinaryEncodeValueLength(pFile, timeLen + 1, length) == false) {
3159                 MSG_DEBUG("__MmsBinaryEncodeTime: MmsBinaryEncodeValueLength fail \n");
3160                 goto __CATCH;
3161         }
3162
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");
3168                         goto __CATCH;
3169                 }
3170         } else {
3171                 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = MmsGetBinaryValue(MmsCodeTimeType, MMS_TIMETYPE_ABSOLUTE) | 0x80;
3172                 if (__MmsBinaryEncodeLongInteger(pFile, time.time, timeLen) == false) {
3173                         MSG_DEBUG("__MmsBinaryEncodeTime: MmsBinaryEncodeLongInteger fail \n");
3174                         goto __CATCH;
3175                 }
3176         }
3177
3178         return true;
3179
3180 __CATCH:
3181         return false;
3182 }