RSA sync with private
[platform/core/messaging/msg-service.git] / plugin / mms_plugin / MmsPluginEncode.cpp
1 /*
2 * Copyright 2012  Samsung Electronics Co., Ltd
3 *
4 * Licensed under the Flora License, Version 1.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *    http://www.tizenopensource.org/license
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <time.h>
18 #include <stdlib.h>
19
20 #include "MmsPluginMessage.h"
21 #include "MmsPluginCodec.h"
22 #include "MsgDebug.h"
23 #include "MsgUtilFile.h"
24 #include "MmsPluginMIME.h"
25 #include "MmsPluginUtil.h"
26
27 gint _g_time_rev_set = 0;
28 gint _g_time_rev = 0;
29
30 /**  Sending message related variables  ------------------------ */
31
32 static  char gszMmsEncodeBuf[MSG_MMS_ENCODE_BUFFER_MAX] = {0, };
33 static  int gCurMmsEncodeBuffPos = 0;   /* number of characters on gpMmsEncodeBuf */
34 static  int     gMmsEncodeMaxLen = 0;
35 static  int     gMmsEncodeCurOffset = 0;        /* offset in file */
36 static  char *gpMmsEncodeBuf = NULL;
37
38
39 /* Acknowledge.ind & NotifyResp.ind related variables ------------------------ */
40
41 static  char gszMmsEncodeBuf2[MSG_MMS_ENCODE_BUFFER_MAX] = {0, };
42 static  int     gCurMmsEncodeBuffPos2 = 0;      /* number of characters on gpMmsEncodeBuf */
43 static  int     gMmsEncodeMaxLen2 = 0;
44 static  int     gMmsEncodeCurOffset2 = 0;       /* offset in file */
45 static  char *gpMmsEncodeBuf2 = NULL;
46
47
48 bool _MmsBinaryEncodeSendReqHdr(FILE *pFile, MmsMsg *pMsg);
49 bool _MmsBinaryEncodeAckIndHdr(FILE *pFile, char *pTrID, bool bReportAllowed);
50 bool _MmsBinaryEncodeNotiRespIndHdr(FILE* pFile, char *pTrID, msg_delivery_report_status_t iStatus, bool bReportAllowed);
51 bool _MmsBinaryEncodeForwardReqHdr(FILE *pFile, char *szContentLocation, char *szForwardTo, char *szForwardCc, char *szForwardBcc);
52 bool _MmsBinaryEncodeReadReport10Hdr(FILE *pFile, MmsMsg *pMsg, msg_read_report_status_t mmsReadStatus);
53 bool _MmsBinaryEncodeReadReport11Hdr(FILE *pFile, MmsMsg *pMsg, msg_read_report_status_t mmsReadStatus);
54
55 static  bool __MmsEncodeSendReq(FILE *pFile, MmsMsg *pMsg, bool bIncludeSendReqHeader);
56 static  int MmsBinaryEncodeUintvarLen(UINT32 integer);
57 static  bool MmsBinaryEncodeUintvar(FILE *pFile, UINT32 integer, int length);
58 static  int MmsBinaryEncodeValueLengthLen(UINT32 integer);
59 static  bool MmsBinaryEncodeValueLength(FILE *pFile, UINT32 integer, int length);
60 static  int     MmsBinaryEncodeIntegerLen(UINT32 integer);
61 static  bool MmsBinaryEncodeInteger(FILE *pFile, UINT32 integer, int length);
62 static  int     MmsBinaryEncodeLongIntegerLen(UINT32 integer);
63 static  bool MmsBinaryEncodeLongInteger(FILE *pFile, UINT32 integer, int length);
64 static  int     MmsBinaryEncodeTextStringLen(UINT8 *source);
65 static  bool MmsBinaryEncodeTextString(FILE *pFile, UINT8 *source, int length);
66 static  int     MmsBinaryEncodeQuotedStringLen(UINT8 *pSrc);
67 static  bool MmsBinaryEncodeQuotedString(FILE *pFile, UINT8 *source, int length);
68 static  int     MmsBinaryEncodeEncodedStringLen(UINT8 *source);
69 static  bool MmsBinaryEncodeEncodedString(FILE *pFile, UINT8 *source, int length);
70 bool MmsBinaryEncodeSendReqHdrwithinBufRegi(FILE *pFile, MmsMsg *pMsg);
71 bool MmsBinaryEncodeSendReqHdrContTypeFildCode(FILE *pFile, int msgID);
72
73 static  bool __MmsBinaryEncodeFieldCodeAndValue(FILE *pFile, UINT8 fieldCode, UINT8 fieldValue);
74
75 static  bool __MmsBinaryEncodeMmsVersion(FILE *pFile);
76 static  bool __MmsBinaryEncodeTrID(FILE *pFile, char *szTrID, int bufLen);
77 static  bool __MmsBinaryEncodeMsgID(FILE *pFile, const char *szMsgID);  /** 2005-05-24, added for read-reply PDU 1.2 */
78 static  bool __MmsBinaryEncodeFrom(FILE *pFile);
79 static  bool __MmsBinaryEncodeTime(FILE *pFile, MmsFieldCode fieldCode, MmsTimeStruct time);
80 static  bool __MmsBinaryEncodeDate(FILE *pFile);
81 static  bool __MmsBinaryEncodeOneAddress(FILE *pFile, MmsFieldCode addrType, char *szAddrStr);
82 static  bool __MmsBinaryEncodeAddress(FILE *pFile, MmsFieldCode addrType, char *szAddr);
83
84
85 /* Functions for Acknowledge.ind & NotifyResp.ind  ------------------------ */
86
87 static  bool MmsBinaryEncodeTextString2(FILE *pFile, UINT8 *source, int length);
88 static  bool __MmsBinaryEncodeMmsVersion2(FILE *pFile);
89 static  bool __MmsBinaryEncodeFieldCodeAndValue2(FILE *pFile, UINT8 fieldCode, UINT8 fieldValue);
90 extern  int MsgProcessUrgentEvent(int iMode);
91
92
93 /** -----------------------------------------------------------------
94  *                                      M   M   S       E   N   C   O   D   E
95  * * -----------------------------------------------------------------*/
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 int _MmsGetEncodeOffset(void)
122 {
123         return (gMmsEncodeCurOffset + gCurMmsEncodeBuffPos);
124 }
125
126
127 bool _MmsEncodeMsg(void)
128 {
129         MSG_DEBUG("_MmsEncodeMsg: Start Binary Encoding now ============= \n");
130         return true;
131 }
132
133
134 bool _MmsEncodeSendReq(FILE *pFile, MmsMsg *pMsg)
135 {
136         return __MmsEncodeSendReq(pFile, pMsg, true);
137 }
138
139
140 bool _MmsEncodeTemplate(FILE *pFile, MmsMsg *pMsg)
141 {
142         return __MmsEncodeSendReq(pFile, pMsg, false);
143 }
144
145
146 static bool __MmsEncodeSendReq(FILE* pFile, MmsMsg* pMsg, bool bIncludeSendReqHeader)
147 {
148         _MmsRegisterEncodeBuffer(gszMmsEncodeBuf, MSG_MMS_ENCODE_BUFFER_MAX);
149
150         if (bIncludeSendReqHeader) {
151                 if (_MmsBinaryEncodeSendReqHdr(pFile, pMsg) == false) {
152                         _MmsUnregisterEncodeBuffer();
153                         return false;
154                 }
155         }
156
157         if (MmsBinaryEncodeMsgBody(pFile, &pMsg->msgType, &pMsg->msgBody, pMsg->nPartCount, !bIncludeSendReqHeader) == false) {
158                 _MmsUnregisterEncodeBuffer();
159                 return false;
160         }
161
162         _MmsUnregisterEncodeBuffer();
163
164         return true;
165 }
166
167
168 /* Functions for Acknowledge.ind & NotifyResp.ind ------------------------ */
169
170 static  void __MmsCleanEncodeBuff2(void)
171 {
172         memset(gpMmsEncodeBuf2, 0, MSG_MMS_ENCODE_BUFFER_MAX);
173         gCurMmsEncodeBuffPos2 = 0;
174 }
175
176
177 void _MmsRegisterEncodeBuffer2(char *pInBuff, int maxLen)
178 {
179         gpMmsEncodeBuf2 = pInBuff;
180         gCurMmsEncodeBuffPos2 = 0;
181         gMmsEncodeMaxLen2 = maxLen;
182         gMmsEncodeCurOffset2 = 0;
183 }
184
185 void _MmsUnregisterEncodeBuffer2(void)
186 {
187         gpMmsEncodeBuf2 = NULL;
188         gCurMmsEncodeBuffPos2 = 0;
189         gMmsEncodeMaxLen2 = 0;
190         gMmsEncodeCurOffset2 = 0;
191 }
192
193
194 /**
195  * @param       source [in] originam string
196  * @param       length [in] gotten from MmsBinaryEncodeTextStringLen()
197  * @param       dest [in] buffer to store quted string
198  * @return      changed string length
199 */
200 static bool MmsBinaryEncodeTextString2(FILE *pFile, UINT8 *source, int length)
201 {
202         MSG_DEBUG("MmsBinaryEncodeTextString2: \n");
203
204         /**
205          * make text string
206          * Text-string = [Quote] *TEXT End-of-string
207          * If the 1st char in the TEXT is in the range of 128-255, a Quote char must precede it.
208          * Otherwise the Quote char must be omitted.
209          * The Quote is not part of the contents.
210          * Quote = <Octet 127>
211          */
212
213         if (pFile == NULL || source == NULL) {
214                 MSG_DEBUG("MmsBinaryEncodeTextString2: source == NULL \n");
215                 goto __CATCH;
216         }
217
218         if ((gMmsEncodeMaxLen2 - gCurMmsEncodeBuffPos2) < length) {
219                 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf2, &gCurMmsEncodeBuffPos2,
220                                                                                         gMmsEncodeMaxLen2, &gMmsEncodeCurOffset2) == false) {
221                         MSG_DEBUG("MmsBinaryEncodeTextString2: MsgWriteDataFromEncodeBuffer fail \n");
222                         goto __CATCH;
223                 }
224         }
225
226         if (source[0] > 0x7F) {
227                 gpMmsEncodeBuf2[gCurMmsEncodeBuffPos2++] = QUOTE;
228                 length--;
229         }
230
231         strncpy(gpMmsEncodeBuf2 + gCurMmsEncodeBuffPos2, (char*)source, (length - 1));  /** except NULL */
232         gCurMmsEncodeBuffPos2 += (length - 1);  /** except NULL */
233         gpMmsEncodeBuf2[gCurMmsEncodeBuffPos2++] = (UINT8)NULL;
234
235         return true;
236
237 __CATCH:
238         return false;
239 }
240
241
242 static  bool __MmsBinaryEncodeFieldCodeAndValue2(FILE *pFile, UINT8 fieldCode, UINT8 fieldValue)
243 {
244         if ((gMmsEncodeMaxLen2 - gCurMmsEncodeBuffPos2) < 2) {
245                 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf2, &gCurMmsEncodeBuffPos2,
246                                                                                         gMmsEncodeMaxLen2, &gMmsEncodeCurOffset2) == false) {
247                         MSG_DEBUG("__MmsBinaryEncodeFieldCodeAndValue: MsgWriteDataFromEncodeBuffer fail \n");
248                         goto __CATCH;
249                 }
250         }
251
252         if (fieldCode == 0xff) {
253                 MSG_DEBUG("__MmsBinaryEncodeFieldCodeAndValue: invalid fieldCode \n");
254                 goto __CATCH;
255         }
256
257         if (fieldValue == 0xff) {
258                 MSG_DEBUG("__MmsBinaryEncodeFieldCodeAndValue: invalid fieldValue \n");
259                 return true;
260         }
261
262         gpMmsEncodeBuf2[gCurMmsEncodeBuffPos2++] = fieldCode;
263         gpMmsEncodeBuf2[gCurMmsEncodeBuffPos2++] = fieldValue;
264
265         return true;
266
267 __CATCH:
268         return false;
269 }
270
271
272 static bool __MmsBinaryEncodeMmsVersion2(FILE *pFile)
273 {
274         UINT8 majorVer = MMS_MAJOR_VERSION;
275         UINT8 minorVer = MMS_MINOR_VERSION;
276
277         MSG_DEBUG("__MmsBinaryEncodeMmsVersion2: \n");
278
279         if (pFile == NULL) {
280                 MSG_DEBUG("__MmsBinaryEncodeMmsVersion2: invalid input file \n");
281                 goto __CATCH;
282         }
283
284         if ((gMmsEncodeMaxLen2 - gCurMmsEncodeBuffPos2) < 2) {
285                 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf2, &gCurMmsEncodeBuffPos2,
286                                                                                         gMmsEncodeMaxLen2, &gMmsEncodeCurOffset2) == false) {
287                         MSG_DEBUG("__MmsBinaryEncodeMmsVersion2: MsgWriteDataFromEncodeBuffer fail \n");
288                         goto __CATCH;
289                 }
290         }
291
292         gpMmsEncodeBuf2[gCurMmsEncodeBuffPos2++] = _MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_VERSION) | 0x80;
293         gpMmsEncodeBuf2[gCurMmsEncodeBuffPos2] = (majorVer << 4) | (minorVer & 0x0f) | MSB;
294
295         if (gpMmsEncodeBuf2[gCurMmsEncodeBuffPos2] < 0x80) {
296                 gpMmsEncodeBuf2[gCurMmsEncodeBuffPos2++] |= 0x80;
297         } else {
298                 gCurMmsEncodeBuffPos2++;
299         }
300
301         return true;
302
303 __CATCH:
304         return false;
305 }
306
307
308 bool _MmsBinaryEncodeAckIndHdr(FILE *pFile, char *szTrID, bool bReportAllowed)
309 {
310         int length = 0;
311         UINT8 fieldCode = 0xff;
312         UINT8 fieldValue = 0xff;
313
314         MSG_DEBUG("_MmsBinaryEncodeAckIndHdr: szTrID = %s\n", szTrID);
315         MSG_DEBUG("_MmsBinaryEncodeAckIndHdr: bReportAllowed = %d\n", bReportAllowed);
316
317         __MmsCleanEncodeBuff2();
318
319         /* msgType */
320         fieldCode  = _MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_MSGTYPE) | 0x80;
321         fieldValue = _MmsGetBinaryValue(MmsCodeMsgType, MMS_MSGTYPE_ACKNOWLEDGE_IND) | 0x80;
322         if (__MmsBinaryEncodeFieldCodeAndValue2(pFile, fieldCode, fieldValue) == false) {
323                 MSG_DEBUG("_MmsBinaryEncodeAckIndHdr: msgType error\n");
324                 goto __CATCH;
325         }
326
327         /* trID (other type of message) */
328         length = MmsBinaryEncodeTextStringLen((UINT8*)szTrID);
329         if (length == -1) {
330                 MSG_DEBUG("_MmsBinaryEncodeAckIndHdr: MmsBinaryEncodeTextStringLen fail \n");
331                 goto __CATCH;
332         }
333
334         if ((gMmsEncodeMaxLen2 - gCurMmsEncodeBuffPos2) < (length + 1)) {
335                 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf2, &gCurMmsEncodeBuffPos2,
336                                                                                         gMmsEncodeMaxLen2, &gMmsEncodeCurOffset2) == false) {
337                         MSG_DEBUG("_MmsBinaryEncodeAckIndHdr: MsgWriteDataFromEncodeBuffer fail \n");
338                         goto __CATCH;
339                 }
340         }
341
342         gpMmsEncodeBuf2[gCurMmsEncodeBuffPos2++] = _MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_TRID) | 0x80;
343
344         if (MmsBinaryEncodeTextString2(pFile, (UINT8*)szTrID, length) == false) {
345                 MSG_DEBUG("_MmsBinaryEncodeAckIndHdr: MmsBinaryEncodeTextString fail\n");
346                 goto __CATCH;
347         }
348
349
350         if (__MmsBinaryEncodeMmsVersion2(pFile) == false) {
351                 MSG_DEBUG("_MmsBinaryEncodeAckIndHdr: __MmsBinaryEncodeMmsVersion error\n");
352                 goto __CATCH;
353         }
354
355
356         /* Report Allowed */
357         fieldCode  = _MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_REPORTALLOWED) | 0x80;
358
359         if (bReportAllowed) {
360                 fieldValue = _MmsGetBinaryValue(MmsCodeReportAllowed, MMS_REPORTALLOWED_YES) | 0x80;
361         } else {
362                 fieldValue = _MmsGetBinaryValue(MmsCodeReportAllowed, MMS_REPORTALLOWED_NO) | 0x80;
363         }
364
365         if (__MmsBinaryEncodeFieldCodeAndValue2(pFile, fieldCode, fieldValue) == false) {
366                 MSG_DEBUG("_MmsBinaryEncodeAckIndHdr: Report Allowed error\n");
367                 goto __CATCH;
368         }
369
370         /* flush remained data on encoding file */
371         if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf2, &gCurMmsEncodeBuffPos2,
372                                                                                 gMmsEncodeMaxLen2, &gMmsEncodeCurOffset2) == false) {
373                 MSG_DEBUG("_MmsBinaryEncodeAckIndHdr: remained data MsgWriteDataFromEncodeBuffer fail \n");
374                 goto __CATCH;
375         }
376
377         return true;
378
379 __CATCH:
380
381         return false;
382 }
383
384
385
386 bool _MmsBinaryEncodeNotiRespIndHdr(FILE *pFile, char *szTrID, msg_delivery_report_status_t iStatus, bool bReportAllowed)
387 {
388         int length = 0;
389         UINT8 fieldCode = 0xff;
390         UINT8 fieldValue = 0xff;
391
392         MSG_DEBUG("_MmsBinaryEncodeNotiRespIndHdr: szTrID = %s\n", szTrID);
393         MSG_DEBUG("_MmsBinaryEncodeNotiRespIndHdr: iStatus = %d\n", iStatus);
394         MSG_DEBUG("_MmsBinaryEncodeNotiRespIndHdr: bReportAllowed = %d\n", bReportAllowed);
395
396         __MmsCleanEncodeBuff2();
397
398         /* msgType */
399         fieldCode  = _MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_MSGTYPE) | 0x80;
400         fieldValue = _MmsGetBinaryValue(MmsCodeMsgType, MMS_MSGTYPE_NOTIFYRESP_IND) | 0x80;
401
402         if (__MmsBinaryEncodeFieldCodeAndValue2(pFile, fieldCode, fieldValue) == false) {
403                 MSG_DEBUG("_MmsBinaryEncodeNotiRespIndHdr: msgType error\n");
404                 goto __CATCH;
405         }
406
407
408         /* trID (other type of message) */
409         length = MmsBinaryEncodeTextStringLen((UINT8*)szTrID);
410         if (length == -1) {
411                 MSG_DEBUG("_MmsBinaryEncodeNotiRespIndHdr: MmsBinaryEncodeTextStringLen fail \n");
412                 goto __CATCH;
413         }
414
415         if ((gMmsEncodeMaxLen2 - gCurMmsEncodeBuffPos2) < (length + 1)) {
416                 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf2, &gCurMmsEncodeBuffPos2,
417                                                                                         gMmsEncodeMaxLen2, &gMmsEncodeCurOffset2) == false) {
418                         MSG_DEBUG("_MmsBinaryEncodeNotiRespIndHdr: MsgWriteDataFromEncodeBuffer fail \n");
419                         goto __CATCH;
420                 }
421         }
422
423         gpMmsEncodeBuf2[gCurMmsEncodeBuffPos2++] = _MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_TRID) | 0x80;
424         if (MmsBinaryEncodeTextString2(pFile, (UINT8*)szTrID, length) == false) {
425                 MSG_DEBUG("_MmsBinaryEncodeNotiRespIndHdr: MmsBinaryEncodeTextString fail\n");
426                 goto __CATCH;
427         }
428
429
430         if (__MmsBinaryEncodeMmsVersion2(pFile) == false) {
431                 MSG_DEBUG("_MmsBinaryEncodeNotiRespIndHdr: __MmsBinaryEncodeMmsVersion error\n");
432                 goto __CATCH;
433         }
434
435
436         /* MsgStatus */
437         MSG_DEBUG("_MmsBinaryEncodeNotiRespIndHdr: MsgStatus = %d\n", iStatus);
438
439         if (iStatus != MSG_DELIVERY_REPORT_NONE) {
440                 fieldCode  = _MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_MSGSTATUS) | 0x80;
441                 fieldValue = _MmsGetBinaryValue(MmsCodeMsgStatus, iStatus) | 0x80;
442                 if (__MmsBinaryEncodeFieldCodeAndValue2(pFile, fieldCode, fieldValue) == false) {
443                         MSG_DEBUG("_MmsBinaryEncodeNotiRespIndHdr: MsgStatus error\n");
444                         goto __CATCH;
445                 }
446         }
447
448
449         /* Report Allowed */
450         fieldCode  = _MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_REPORTALLOWED) | 0x80;
451
452         if (bReportAllowed) {
453                 fieldValue = _MmsGetBinaryValue(MmsCodeReportAllowed, MMS_REPORTALLOWED_YES) | 0x80;
454         } else {
455                 fieldValue = _MmsGetBinaryValue(MmsCodeReportAllowed, MMS_REPORTALLOWED_NO) | 0x80;
456         }
457
458         if (__MmsBinaryEncodeFieldCodeAndValue2(pFile, fieldCode, fieldValue) == false) {
459                 MSG_DEBUG("_MmsBinaryEncodeNotiRespIndHdr: Report Allowed error\n");
460                 goto __CATCH;
461         }
462
463         /* flush remained data on encoding file */
464         if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf2, &gCurMmsEncodeBuffPos2,
465                                                                                 gMmsEncodeMaxLen2, &gMmsEncodeCurOffset2) == false) {
466                 MSG_DEBUG("_MmsBinaryEncodeNotiRespIndHdr: remained data MsgWriteDataFromEncodeBuffer fail \n");
467                 goto __CATCH;
468         }
469
470         return true;
471
472 __CATCH:
473
474         return false;
475 }
476
477
478 bool _MmsEncodeAckInd(FILE *pFile, char *pTrID, bool bReportAllowed)
479 {
480         _MmsRegisterEncodeBuffer2(gszMmsEncodeBuf2, MSG_MMS_ENCODE_BUFFER_MAX);
481
482         MSG_DEBUG("_MmsEncodeAckInd: Start Binary Encoding now ============= \n");
483
484         if (_MmsBinaryEncodeAckIndHdr(pFile, pTrID, bReportAllowed) == false) {
485                 MSG_DEBUG("_MmsEncodeAckInd: SendReq Binary encoding fail \n");
486                 goto __CATCH;
487         }
488
489         _MmsUnregisterEncodeBuffer2();
490
491         return true;
492
493 __CATCH:
494
495         MSG_DEBUG("## _MmsEncodeAckInd: failed");
496         _MmsUnregisterEncodeBuffer2();
497
498         return false;
499 }
500
501
502
503 bool _MmsEncodeNotiRespInd(FILE *pFile, char *pTrID, msg_delivery_report_status_t iStatus, bool bReportAllowed)
504 {
505         _MmsRegisterEncodeBuffer2(gszMmsEncodeBuf2, MSG_MMS_ENCODE_BUFFER_MAX);
506
507         MSG_DEBUG("_MmsEncodeNotiRespInd: Start Binary Encoding now ============= \n");
508
509         if (_MmsBinaryEncodeNotiRespIndHdr(pFile, pTrID, iStatus, bReportAllowed) == false) {
510                 MSG_DEBUG("_MmsEncodeNotiRespInd: SendReq Binary encoding fail \n");
511                 goto __CATCH;
512         }
513
514         _MmsUnregisterEncodeBuffer2();
515
516         return true;
517
518 __CATCH:
519
520         return false;
521 }
522
523
524 /* Functions for Acknowledge.ind & NotifyResp.ind  (END) ------------------------ */
525 bool _MmsEncodeReadReport10(FILE *pFile, MmsMsg *pMsg, msg_read_report_status_t mmsReadStatus)
526 {
527         char *pText = NULL;
528         MsgMultipart *pPart = NULL;
529         MsgType msgType;
530         MsgBody msgBody;
531
532         char *pszReportMsg = NULL;
533         int     maxLen = 0;
534
535         struct  tm      *dateTime = NULL;
536         time_t  RawTime = 0;
537         time_t  dateSec = 0;
538
539         _MmsRegisterEncodeBuffer(gszMmsEncodeBuf, MSG_MMS_ENCODE_BUFFER_MAX);
540
541         MSG_DEBUG("_MmsEncodeMsg: Start Binary Encoding now V1.0============= \n");
542
543         if (_MmsBinaryEncodeReadReport10Hdr(pFile, pMsg, mmsReadStatus) == false) {
544                 MSG_DEBUG("_MmsEncodeReadReport10: SendReq Binary encoding fail \n");
545                 goto __CATCH;
546         }
547
548         memset(&msgType, 0, sizeof(MsgType));
549         memset(&msgBody, 0, sizeof(MsgBody));
550
551         pText = (char *)malloc(MSG_STDSTR_LONG);
552         if (pText == NULL) {
553                 MSG_DEBUG("__MmsSendReadReportV10: text body malloc fail \n");
554                 goto __CATCH;
555         }
556
557         memset(pText, 0, MSG_STDSTR_LONG);
558
559         time(&RawTime);
560         dateTime = localtime(&RawTime);
561         dateSec = mktime(dateTime);
562
563         // get report message
564         if (mmsReadStatus == MSG_READ_REPORT_IS_DELETED) {
565                 pszReportMsg = (char*)"Your message has been deleted " ;
566         } else {
567                 pszReportMsg = (char*)"Your message has been read " ;
568         }
569
570         // make report body ..
571         maxLen = strlen (pszReportMsg) +16 /* date string */ + 8 /* enter chars */ ;
572
573         if (maxLen > MSG_STDSTR_LONG) {
574                 snprintf (pText, MSG_STDSTR_LONG, "%s\n", pszReportMsg);
575         } else {
576                 snprintf(pText, MSG_STDSTR_LONG, "%s\r\n\r\n%.4d/%.2d/%.2d %.2d:%.2d\r\n",
577                                                 pszReportMsg, dateTime->tm_year+1900, dateTime->tm_mon+1, dateTime->tm_mday, dateTime->tm_hour, dateTime->tm_min);
578         }
579
580         // make header
581         msgType.type = MIME_APPLICATION_VND_WAP_MULTIPART_MIXED;
582         msgType.contentSize = strlen(pText);
583         msgType.param.charset = MSG_CHARSET_UNKNOWN;
584
585         // make body
586         if ((pPart = MmsAllocMultipart()) == NULL) {
587                 MSG_DEBUG("__MmsSendReadReportV10: MsgAllocMultipart Fail \n");
588                 goto __CATCH;
589         }
590
591
592         pPart->type.type = MIME_TEXT_PLAIN;
593         pPart->type.contentSize = strlen(pText);
594         pPart->type.param.charset = MSG_CHARSET_UTF8;
595
596         if (pPart->pBody == NULL) {
597                 MSG_DEBUG("__MmsSendReadReportV10: pPart->pBody is NULL \n");
598                 goto __CATCH;
599         }
600
601         pPart->pBody->size = strlen(pText);
602         pPart->pBody->body.pText = pText;
603
604         msgBody.body.pMultipart = pPart;
605
606         if (MmsBinaryEncodeMsgBody(pFile, &msgType, &msgBody, 1, false) == false) {
607                 MSG_DEBUG("__MmsSendReadReportV10: MmsBinaryEncodeMsgBody fail \n");
608                 goto __CATCH;
609         }
610
611         MSG_DEBUG("__MmsSendReadReportV10:  Send To RM ReadReport Msg \n");
612
613         if (pText) {
614                 free(pText);
615                 pText = NULL;
616         }
617
618         if (pPart) {
619                 if (pPart->pBody) {
620                         free(pPart->pBody);
621                         pPart->pBody = NULL;
622                 }
623                 free(pPart);
624                 pPart = NULL;
625         }
626
627         _MmsUnregisterEncodeBuffer();
628
629         return true;
630
631 __CATCH:
632
633         if (pText) {
634                 free(pText);
635                 pText = NULL;
636         }
637
638         if (pPart) {
639                 if (pPart->pBody) {
640                         free(pPart->pBody);
641                         pPart->pBody = NULL;
642                 }
643                 free(pPart);
644                 pPart = NULL;
645         }
646
647         _MmsUnregisterEncodeBuffer();
648
649         return false;
650 }
651
652
653 bool _MmsEncodeReadReport11(FILE *pFile, MmsMsg *pMsg, msg_read_report_status_t mmsReadStatus)
654 {
655         _MmsRegisterEncodeBuffer(gszMmsEncodeBuf, MSG_MMS_ENCODE_BUFFER_MAX);
656
657         MSG_DEBUG("_MmsEncodeMsg: Start Binary Encoding now V1.1============= \n");
658
659         if (_MmsBinaryEncodeReadReport11Hdr(pFile, pMsg, mmsReadStatus) == false) {
660                 MSG_DEBUG("_MmsEncodeMsg: SendReq Binary encoding fail \n");
661                 goto __CATCH;
662         }
663
664         _MmsUnregisterEncodeBuffer();
665
666         return true;
667
668 __CATCH:
669         _MmsUnregisterEncodeBuffer();
670
671         return false;
672 }
673
674
675
676 /* ==========================================================
677
678                                         B  I  N  A  R  Y         E  N  C  O  D  I  N  G
679
680    ==========================================================*/
681
682 /*
683  * Binary Encoded Message Format
684  *
685  *              < Single Part Body Message >
686  * -----------------------------------
687  * |            Header Fields                    |
688  * -----------------------------------
689  * | Content Type:start=xxx;type=xxx |  ->(ex) Text/Plain, Text/Html, ....
690  * -----------------------------------
691  * |            Single Part Body                 |
692  * -----------------------------------
693  *
694  *              < Multi Part Body Message >
695  * -----------------------------------
696  * |            Header Fields                    |
697  * -----------------------------------
698  * | Content Type:start=xxx;type=xxx |  -> (ex) Application/vnd.wap.multipart.mixed(related), multipart/mixed(related)
699  * -----------------------------------
700  * |    # of Entries (body parts)        |
701  * -----------------------------------                          < Each Entry >
702  * |                    Entry 1                          |      ->      -----------------------------
703  * -----------------------------------          |               header Length           |
704  * |                    Entry 2                          |              -----------------------------
705  * -----------------------------------          |               Data Length                     |
706  * |                    ......                           |              -----------------------------  -
707  * -----------------------------------          |               Content-Type            |  |
708  * |                    Entry n                          |              -----------------------------  | Header Length
709  * -----------------------------------          |                       Header                  |  |
710  *                                                                                      -----------------------------  -
711  *                                                                                      |                       Data                    |  | Data Length
712  *                                                                                      -----------------------------  -
713  */
714
715 bool _MmsBinaryEncodeSendReqHdr(FILE *pFile, MmsMsg *pMsg)
716 {
717         UINT8 fieldCode = 0xff;
718         UINT8 fieldValue        = 0xff;
719         int length              = 0;
720
721         __MmsCleanEncodeBuff();
722
723         /* msgType */
724         fieldCode  = _MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_MSGTYPE) | 0x80;
725         fieldValue = _MmsGetBinaryValue(MmsCodeMsgType, MMS_MSGTYPE_SEND_REQ) | 0x80;
726         if (__MmsBinaryEncodeFieldCodeAndValue(pFile, fieldCode, fieldValue) == false) {
727                 MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: msgType error\n");
728                 goto __CATCH;
729         }
730
731         /* trID (other type of message) */
732         if (__MmsBinaryEncodeTrID(pFile, pMsg->szTrID, MMS_TR_ID_LEN + 1) == false) {
733                 MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: trID error\n");
734                 goto __CATCH;
735         }
736
737         MSG_DEBUG("_MmsBinaryEncodeSendReqHdr:                pMsg->szTrID = %s\n", pMsg->szTrID);
738
739         if (__MmsBinaryEncodeMmsVersion(pFile) == false) {
740                 MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: __MmsBinaryEncodeMmsVersion error\n");
741                 goto __CATCH;
742         }
743
744         /* From : Insert Token mode */
745         if (__MmsBinaryEncodeFrom(pFile) == false) {
746                 MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: __MmsBinaryEncodeFrom fail\n");
747                 goto __CATCH;
748         }
749
750
751         /* To = Encoded-string-value */
752         if (__MmsBinaryEncodeAddress(pFile, MMS_CODE_TO, pMsg->mmsAttrib.szTo) == false)
753         {
754                 MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: To __MmsBinaryEncodeAddress fail\n");
755                 goto __CATCH;
756         }
757
758
759         /* Cc = Encoded-string-value */
760         if (__MmsBinaryEncodeAddress(pFile, MMS_CODE_CC, pMsg->mmsAttrib.szCc) == false) {
761                 MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: Cc __MmsBinaryEncodeAddress fail\n");
762                 goto __CATCH;
763         }
764
765
766         /* Bcc = Encoded-string-value */
767
768         if (__MmsBinaryEncodeAddress(pFile, MMS_CODE_BCC, pMsg->mmsAttrib.szBcc) == false) {
769                 MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: Bcc __MmsBinaryEncodeAddress fail\n");
770                 goto __CATCH;
771         }
772
773
774         MSG_DEBUG("_MmsBinaryEnocdeSendReqHdr() pMsg->mmsAttrib.szSubject =%s\n",pMsg->mmsAttrib.szSubject);
775
776         /* Subject = Encoded-string-value */
777         if (pMsg->mmsAttrib.szSubject[0]) {
778                 length = MmsBinaryEncodeEncodedStringLen((UINT8*)pMsg->mmsAttrib.szSubject);
779                 if (length == -1) {
780                         MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: subject MmsBinaryEncodeEncodedStringLen fail \n");
781                         goto __CATCH;
782                 }
783
784                 if ((gMmsEncodeMaxLen - gCurMmsEncodeBuffPos) < length + 1) {
785                         if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
786                                                                                                 gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
787                                 MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: subject MsgWriteDataFromEncodeBuffer fail \n");
788                                 goto __CATCH;
789                         }
790                 }
791
792                 fieldCode = _MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_SUBJECT) | 0x80;
793                 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = fieldCode;
794                 if (MmsBinaryEncodeEncodedString(pFile, (UINT8*)pMsg->mmsAttrib.szSubject, length) == false) {
795                         MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: subject MmsBinaryEncodeEncodedString fail \n");
796                         goto __CATCH;
797                 }
798         }
799
800         /* MMS-1.3-con-739 */
801         /* Msg class */
802         fieldCode  = _MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_MSGCLASS) | 0x80;
803         fieldValue = _MmsGetBinaryValue(MmsCodeMsgClass,  (int)pMsg->mmsAttrib.msgClass) | 0x80;
804         if (__MmsBinaryEncodeFieldCodeAndValue(pFile, fieldCode, fieldValue) == false) {
805                 MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: msgClass error\n");
806                 goto __CATCH;
807         }
808         /* MMS-1.3-con-739 */
809         /* MMS-1.3-con-733 */
810         /* Date = Long-integer */
811         if (!__MmsBinaryEncodeDate(pFile)) {
812                         MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: __MmsBinaryEncodeDate error\n");
813                         goto __CATCH;
814         }
815         /* MMS-1.3-con-733 */
816
817         /* Expiry Time  : Value-length Absolute-token Date-value */
818         if (pMsg->mmsAttrib.bUseExpiryCustomTime == true) {
819                 if (__MmsBinaryEncodeTime(pFile, MMS_CODE_EXPIRYTIME, pMsg->mmsAttrib.expiryCustomTime) == false) {
820                         MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: expiryTime __MmsBinaryEncodeTime fail\n");
821                         goto __CATCH;
822                 }
823         } else if (pMsg->mmsAttrib.expiryTime.type != MMS_TIMETYPE_NONE) {      // for avoiding the creation of the expiry field in case the user selects the maximum
824                 if (__MmsBinaryEncodeTime(pFile, MMS_CODE_EXPIRYTIME, pMsg->mmsAttrib.expiryTime) == false) {
825                         MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: expiryTime __MmsBinaryEncodeTime fail\n");
826                         goto __CATCH;
827                 }
828         }
829
830         /* Use Custom time for Delivery Time */
831         if (pMsg->mmsAttrib.bUseDeliveryCustomTime == true) {
832                 if (__MmsBinaryEncodeTime(pFile, MMS_CODE_DELIVERYTIME, pMsg->mmsAttrib.deliveryCustomTime) == false) {
833                         MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: deliveryTime __MmsBinaryEncodeTime fail\n");
834                         goto __CATCH;
835                 }
836         } else {
837                 if (__MmsBinaryEncodeTime(pFile, MMS_CODE_DELIVERYTIME, pMsg->mmsAttrib.deliveryTime) == false) {
838                         MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: deliveryTime __MmsBinaryEncodeTime fail\n");
839                         goto __CATCH;
840                 }
841         }
842
843         /* Priority */
844         if (pMsg->mmsAttrib.priority!= MMS_PRIORITY_ERROR && pMsg->mmsAttrib.priority!= MMS_PRIORITY_NORMAL) {
845                 /* MMS_PRIORITY_NORMAL is default : don't send optional field */
846
847                 fieldCode = _MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_PRIORITY)|0x80;
848                 fieldValue = _MmsGetBinaryValue(MmsCodePriority, pMsg->mmsAttrib.priority)|0x80;
849
850                 if (__MmsBinaryEncodeFieldCodeAndValue(pFile, fieldCode, fieldValue) == false) {
851                         MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: priority error\n");
852                         goto __CATCH;
853                 }
854         }
855
856         /* Sender Visible (hide | show) */
857         if (pMsg->mmsAttrib.bHideAddress == true) {
858                 fieldCode = _MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_SENDERVISIBILLITY) | MSB;
859                 fieldValue = _MmsGetBinaryValue(MmsCodeSenderVisibility, MMS_SENDER_HIDE) | 0x80;
860
861                 if (__MmsBinaryEncodeFieldCodeAndValue(pFile, fieldCode, fieldValue) == false) {
862                         MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: sender visibility error\n");
863                         goto __CATCH;
864                 }
865         }
866
867         /* Delivery Report (yes | no) */
868         fieldCode = _MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_DELIVERYREPORT)|0x80;
869
870         if (pMsg->mmsAttrib.bAskDeliveryReport) {
871                 fieldValue = _MmsGetBinaryValue(MmsCodeDeliveryReport, MMS_REPORT_YES) | 0x80;
872         } else {
873                 fieldValue = _MmsGetBinaryValue(MmsCodeDeliveryReport, MMS_REPORT_NO) | 0x80;
874         }
875
876         if (__MmsBinaryEncodeFieldCodeAndValue(pFile, fieldCode, fieldValue) == false) {
877                 MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: sender visibility error\n");
878                 goto __CATCH;
879         }
880
881         /* Read Reply (Yes | no) */
882         fieldCode = _MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_READREPLY)|0x80;
883         if (pMsg->mmsAttrib.bAskReadReply) {
884                 fieldValue = _MmsGetBinaryValue(MmsCodeReadReply, MMS_REPORT_YES)|0x80;
885         } else {
886                 fieldValue = _MmsGetBinaryValue(MmsCodeReadReply, MMS_REPORT_NO)|0x80;
887         }
888
889         if (__MmsBinaryEncodeFieldCodeAndValue(pFile, fieldCode, fieldValue) == false) {
890                 MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: read reply error\n");
891                 goto __CATCH;
892         }
893
894         if ((pMsg->mmsAttrib.replyCharge.chargeType == MMS_REPLY_REQUESTED) ||
895                 (pMsg->mmsAttrib.replyCharge.chargeType == MMS_REPLY_REQUESTED_TEXT_ONLY)) {
896
897                 // reply charging
898                 fieldCode = _MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_REPLYCHARGING)|0x80;
899                 fieldValue = _MmsGetBinaryValue(MmsCodeReadReply, pMsg->mmsAttrib.replyCharge.chargeType)|0x80;
900
901                 if (__MmsBinaryEncodeFieldCodeAndValue(pFile, fieldCode, fieldValue) == false) {
902                         MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: replyCharging error\n");
903                         goto __CATCH;
904                 }
905
906                 /** fixme: Reply-charging-deadline */
907                 if (pMsg->mmsAttrib.replyCharge.deadLine.time > 0) {
908                         if (__MmsBinaryEncodeTime(pFile, MMS_CODE_REPLYCHARGINGDEADLINE, pMsg->mmsAttrib.replyCharge.deadLine) == false) {
909                                 MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: replyCharging __MmsBinaryEncodeTime fail\n");
910                                 goto __CATCH;
911                         }
912                 }
913
914                 /** fixme: Reply-charging-size */
915                 if (pMsg->mmsAttrib.replyCharge.chargeSize > 0) {
916                         length = MmsBinaryEncodeIntegerLen(pMsg->mmsAttrib.replyCharge.chargeSize);
917
918                         if ((gMmsEncodeMaxLen - gCurMmsEncodeBuffPos) < (length + 1)) {
919                                 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
920                                                                                                 gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
921                                         MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: MsgWriteDataFromEncodeBuffer fail \n");
922                                         goto __CATCH;
923                                 }
924                         }
925
926                         gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = _MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_REPLYCHARGINGSIZE) | 0x80;
927
928                         if (MmsBinaryEncodeInteger(pFile, pMsg->mmsAttrib.replyCharge.chargeSize, length) == false)     {
929                                 MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: replyChargingSize MmsBinaryEncodeInteger error\n");
930                                 goto __CATCH;
931                         }
932                 }
933
934                 /** fixme: Reply-charging-ID  ----> used only when reply message  */
935                 if (pMsg->mmsAttrib.replyCharge.szChargeID[0]) {
936                         length = MmsBinaryEncodeTextStringLen((UINT8*)pMsg->mmsAttrib.replyCharge.szChargeID);
937                         if (length == -1) {
938                                 MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: szReplyChargingID MmsBinaryEncodeTextStringLen fail\n");
939                                 goto __CATCH;
940                         }
941
942                         if ((gMmsEncodeMaxLen - gCurMmsEncodeBuffPos) < (length + 1)) {
943                                 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
944                                                                                                 gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
945                                         MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: MsgWriteDataFromEncodeBuffer fail \n");
946                                         goto __CATCH;
947                                 }
948                         }
949
950                         gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = _MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_REPLYCHARGINGID) | 0x80;
951
952                         if (MmsBinaryEncodeTextString(pFile, (UINT8*)pMsg->mmsAttrib.replyCharge.szChargeID, length) == false) {
953                                 MSG_DEBUG("_MmsBinaryEncodeForwardReqHdr: szContentLocation MmsBinaryEncodeTextString fail\n");
954                                 goto __CATCH;
955                         }
956                 }
957         }
958
959         /* flush remained data on encoding file */
960         if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos, gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
961                 MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: remained data MsgWriteDataFromEncodeBuffer fail \n");
962                 goto __CATCH;
963         }
964
965         return true;
966
967 __CATCH:
968
969         MSG_DEBUG("## _MmsBinaryEncodeSendReqHdr: failed");
970         return false;
971 }
972
973 bool MmsBinaryEncodeSendReqHdrwithinBufRegi(FILE *pFile, MmsMsg *pMsg)
974 {
975         bool ret = false;
976
977         _MmsRegisterEncodeBuffer(gszMmsEncodeBuf, MSG_MMS_ENCODE_BUFFER_MAX);
978         ret = _MmsBinaryEncodeSendReqHdr(pFile, pMsg);
979         _MmsUnregisterEncodeBuffer();
980
981         return ret;
982 }
983
984 bool _MmsBinaryEncodeContentTypeFieldCode(FILE *pFile, int msgID)
985 {
986         if ((gMmsEncodeMaxLen - gCurMmsEncodeBuffPos) < 2) {
987                 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
988                                                                                         gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
989                         MSG_DEBUG("__MmsBinaryEncodeMmsVersion: MsgWriteDataFromEncodeBuffer fail \n");
990                         return false;
991                 }
992         }
993
994         gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = _MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_CONTENTTYPE) | 0x80;
995
996         if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos, gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
997                 MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: remained data MsgWriteDataFromEncodeBuffer fail \n");
998                 return false;
999         }
1000
1001         return true;
1002 }
1003
1004 bool MmsBinaryEncodeSendReqHdrContTypeFildCode(FILE *pFile, int msgID)
1005 {
1006         bool ret = false;
1007
1008         _MmsRegisterEncodeBuffer(gszMmsEncodeBuf, MSG_MMS_ENCODE_BUFFER_MAX);
1009
1010         ret = _MmsBinaryEncodeContentTypeFieldCode(pFile, msgID);
1011
1012         _MmsUnregisterEncodeBuffer();
1013
1014
1015         return ret;
1016 }
1017
1018 #if 0
1019 bool
1020 _MmsBinaryEncodeForwardReqHdr(FILE* pFile, char* szContentLocation, char* szForwardTo, char* szForwardCc, char* szForwardBcc)
1021 {
1022         int             length  = 0;
1023         UINT8   fieldCode = 0xff;
1024         UINT8   fieldValue = 0xff;
1025         UINT32  date    = 0;
1026
1027
1028         __MmsCleanEncodeBuff();
1029
1030         /* msgType */
1031         fieldCode  = _MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_MSGTYPE) | 0x80;
1032         fieldValue = _MmsGetBinaryValue(MmsCodeMsgType, MMS_MSGTYPE_FORWARD_REQ) | 0x80;
1033         if (__MmsBinaryEncodeFieldCodeAndValue(pFile, fieldCode, fieldValue) == false)
1034         {
1035                 MSG_DEBUG("_MmsBinaryEncodeForwardReqHdr: msgType error\n");
1036                 goto __CATCH;
1037         }
1038
1039
1040         /* trID (other type of message) */
1041         if (__MmsBinaryEncodeTrID(pFile, NULL, 0) == false)
1042         {
1043                 MSG_DEBUG("_MmsBinaryEncodeForwardReqHdr: __MmsBinaryEncodeTrID error\n");
1044                 goto __CATCH;
1045         }
1046
1047
1048         /* MMS version (currently we use 1.0 : VDF Spain's version) */
1049         if (__MmsBinaryEncodeMmsVersion(pFile) == false)
1050         {
1051                 MSG_DEBUG("_MmsBinaryEncodeForwardReqHdr: __MmsBinaryEncodeMmsVersion error\n");
1052                 goto __CATCH;
1053         }
1054
1055
1056         /* Date = Long-integer */
1057         if (!__MmsBinaryEncodeDate(pFile))
1058         {
1059                         MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: __MmsBinaryEncodeDate error\n");
1060                         goto __CATCH;
1061         }
1062
1063         /* From : Insert Token mode */
1064         if (__MmsBinaryEncodeFrom(pFile) == false)
1065         {
1066                 MSG_DEBUG("_MmsBinaryEncodeForwardReqHdr: __MmsBinaryEncodeFrom fail\n");
1067                 goto __CATCH;
1068         }
1069
1070
1071         /* To = Encoded-string */
1072         if (__MmsBinaryEncodeAddress(pFile,
1073                                                                   MMS_CODE_TO,
1074                                                                   szForwardTo) == false)
1075         {
1076                 MSG_DEBUG("_MmsBinaryEncodeForwardReqHdr: To __MmsBinaryEncodeAddress fail\n");
1077                 goto __CATCH;
1078         }
1079
1080
1081         /* Cc = Encoded-string */
1082         if (__MmsBinaryEncodeAddress(pFile,
1083                                                                   MMS_CODE_CC,
1084                                                                   szForwardCc) == false)
1085         {
1086                 MSG_DEBUG("_MmsBinaryEncodeForwardReqHdr: Cc __MmsBinaryEncodeAddress fail\n");
1087                 goto __CATCH;
1088         }
1089
1090
1091         /* Bcc = Encoded-string */
1092         if (__MmsBinaryEncodeAddress(pFile,
1093                                                                   MMS_CODE_BCC,
1094                                                                   szForwardBcc) == false)
1095         {
1096                 MSG_DEBUG("_MmsBinaryEncodeForwardReqHdr: Bcc __MmsBinaryEncodeAddress fail\n");
1097                 goto __CATCH;
1098         }
1099
1100
1101         /* Expiry Time  : Value-length Absolute-token Date-value */
1102         if (gMmsSetup.sendSetup.bExpiryUseCustomTime == true) //jhlee expiry time use custom time
1103         {
1104                 if (__MmsBinaryEncodeTime(pFile,
1105                                                            MMS_CODE_EXPIRYTIME,
1106                                                            gMmsSetup.sendSetup.expiryCustomTime) == false)
1107                 {
1108                         MSG_DEBUG("_MmsBinaryEncodeForwardReqHdr: expiryTime __MmsBinaryEncodeTime fail\n");
1109                         goto __CATCH;
1110                 }
1111         }
1112         else
1113         {
1114                 if (__MmsBinaryEncodeTime(pFile,
1115                                                            MMS_CODE_EXPIRYTIME,
1116                                                            gMmsSetup.sendSetup.expiryTime) == false)
1117                 {
1118                         MSG_DEBUG("_MmsBinaryEncodeForwardReqHdr: expiryTime __MmsBinaryEncodeTime fail\n");
1119                         goto __CATCH;
1120                 }
1121
1122         }
1123
1124
1125         /* Delivery Time  : Value-length Absolute-token Date-value */
1126         if (__MmsBinaryEncodeTime(pFile,
1127                                                            MMS_CODE_DELIVERYTIME,
1128                                                            gMmsSetup.sendSetup.deliveryTime) == false)
1129         {
1130                 MSG_DEBUG("_MmsBinaryEncodeForwardReqHdr: deliveryTime __MmsBinaryEncodeTime fail\n");
1131                 goto __CATCH;
1132         }
1133
1134
1135         /* Report Allowed */
1136         fieldCode  = _MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_REPORTALLOWED) | 0x80;
1137         if (gMmsSetup.recvSetup.bSendDeliveryReport)
1138                 fieldValue  = _MmsGetBinaryValue(MmsCodeReportAllowed,
1139                                                                                   MMS_REPORTALLOWED_YES) | 0x80;
1140         else
1141                 fieldValue  = _MmsGetBinaryValue(MmsCodeReportAllowed,
1142                                                                                   MMS_REPORTALLOWED_NO) | 0x80;
1143         if (__MmsBinaryEncodeFieldCodeAndValue(pFile, fieldCode, fieldValue) == false)
1144         {
1145                 MSG_DEBUG("_MmsBinaryEncodeForwardReqHdr: Report Allowed error\n");
1146                 goto __CATCH;
1147         }
1148
1149
1150         /* Delivery Report */
1151         fieldCode  = _MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_DELIVERYREPORT) | 0x80;
1152         if (gMmsSetup.recvSetup.bSendDeliveryReport)
1153                 fieldValue  = _MmsGetBinaryValue(MmsCodeDeliveryReport,
1154                                                                                   MMS_REPORT_YES) | 0x80;
1155         else
1156                 fieldValue  = _MmsGetBinaryValue(MmsCodeDeliveryReport,
1157                                                                                   MMS_REPORT_NO) | 0x80;
1158         if (__MmsBinaryEncodeFieldCodeAndValue(pFile, fieldCode, fieldValue) == false)
1159         {
1160                 MSG_DEBUG("_MmsBinaryEncodeForwardReqHdr: Report Allowed error\n");
1161                 goto __CATCH;
1162         }
1163
1164
1165         /* Read Reply */
1166         fieldCode  = _MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_READREPLY) | 0x80;
1167         if (gMmsSetup.recvSetup.bSendDeliveryReport)
1168                 fieldValue  = _MmsGetBinaryValue(MmsCodeReadReply,
1169                                                                                   MMS_REPORT_YES) | 0x80;
1170         else
1171                 fieldValue  = _MmsGetBinaryValue(MmsCodeReadReply,
1172                                                                                   MMS_REPORT_NO) | 0x80;
1173         if (__MmsBinaryEncodeFieldCodeAndValue(pFile, fieldCode, fieldValue) == false)
1174         {
1175                 MSG_DEBUG("_MmsBinaryEncodeForwardReqHdr: Read Reply error\n");
1176                 goto __CATCH;
1177         }
1178
1179
1180         /* Content Location */
1181         if (szContentLocation[0])
1182         {
1183                 length = MmsBinaryEncodeTextStringLen((UINT8*)szContentLocation);
1184                 if (length == -1)
1185                 {
1186                         MSG_DEBUG("_MmsBinaryEncodeForwardReqHdr: szContentLocation MmsBinaryEncodeTextStringLen fail\n");
1187                         goto __CATCH;
1188                 }
1189
1190                 if ((gMmsEncodeMaxLen - gCurMmsEncodeBuffPos) < (length + 1))   // + fieldCode
1191                 {
1192                         if (MsgWriteDataFromEncodeBuffer(pFile,
1193                                                                 gpMmsEncodeBuf,
1194                                                                 &gCurMmsEncodeBuffPos,
1195                                                                 gMmsEncodeMaxLen,
1196                                                                 &gMmsEncodeCurOffset) == false)
1197                         {
1198                                 MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: MsgWriteDataFromEncodeBuffer fail \n");
1199                                 goto __CATCH;
1200                         }
1201                 }
1202
1203                 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = _MmsGetBinaryValue(MmsCodeFieldCode,
1204                                                                                                                                           MMS_CODE_CONTENTLOCATION) | 0x80;
1205                 if (MmsBinaryEncodeTextString(pFile, (UINT8*)szContentLocation, length) == false)
1206                 {
1207                         MSG_DEBUG("_MmsBinaryEncodeForwardReqHdr: szContentLocation MmsBinaryEncodeTextString fail\n");
1208                         goto __CATCH;
1209                 }
1210         }
1211
1212         /* flush remained data on encoding file */
1213         if (MsgWriteDataFromEncodeBuffer(pFile,
1214                                                   gpMmsEncodeBuf,
1215                                                   &gCurMmsEncodeBuffPos,
1216                                                   gMmsEncodeMaxLen,
1217                                                   &gMmsEncodeCurOffset) == false)
1218         {
1219                 MSG_DEBUG("_MmsBinaryEncodeForwardReqHdr: remained data MsgWriteDataFromEncodeBuffer fail \n");
1220                 goto __CATCH;
1221         }
1222
1223         return true;
1224
1225 __CATCH:
1226
1227         MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos, gMmsEncodeMaxLen, &gMmsEncodeCurOffset);
1228         return false;
1229 }
1230 #endif
1231
1232
1233 bool _MmsBinaryEncodeReadReport10Hdr(FILE *pFile, MmsMsg *pMsg, msg_read_report_status_t mmsReadStatus)
1234 {
1235         int length      = 0;
1236         char *szTo      = NULL;
1237         UINT8 fieldCode = 0xff;
1238         UINT8 fieldValue = 0xff;
1239         char szSubject[MSG_LOCALE_SUBJ_LEN + 8] = {0, };
1240
1241         if (pMsg == NULL) {
1242                 MSG_DEBUG("pMsg is NULL");
1243                 goto __CATCH;
1244         }
1245
1246         MSG_DEBUG("_MmsBinaryEncodeReadReport10Hdr: mmsReadStatus = %d\n", mmsReadStatus);
1247
1248         __MmsCleanEncodeBuff();
1249
1250         /* msgType */
1251         fieldCode  = _MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_MSGTYPE) | 0x80;
1252         fieldValue = _MmsGetBinaryValue(MmsCodeMsgType, MMS_MSGTYPE_SEND_REQ) | 0x80;
1253         if (__MmsBinaryEncodeFieldCodeAndValue(pFile, fieldCode, fieldValue) == false) {
1254                 MSG_DEBUG("_MmsBinaryEncodeReadReport10Hdr: msgType error\n");
1255                 goto __CATCH;
1256         }
1257
1258         /* trID (other type of message) */
1259         if (__MmsBinaryEncodeTrID(pFile, NULL, 0) == false) {
1260                 MSG_DEBUG("_MmsBinaryEncodeReadReport10Hdr: __MmsBinaryEncodeTrID error\n");
1261                 goto __CATCH;
1262         }
1263
1264         if (__MmsBinaryEncodeMmsVersion(pFile) == false) {
1265                 MSG_DEBUG("_MmsBinaryEncodeReadReport10Hdr: __MmsBinaryEncodeMmsVersion error\n");
1266                 goto __CATCH;
1267         }
1268
1269         /* Date = Long-integer */
1270         if (!__MmsBinaryEncodeDate(pFile)) {
1271                 MSG_DEBUG("_MmsBinaryEncodeReadReport10Hdr: date __MmsBinaryEncodeDate error\n");
1272                 goto __CATCH;
1273         }
1274
1275         /* From : Insert Token mode */
1276         if (__MmsBinaryEncodeFrom(pFile) == false) {
1277                 MSG_DEBUG("_MmsBinaryEncodeReadReport10Hdr: __MmsBinaryEncodeFrom fail\n");
1278                 goto __CATCH;
1279         }
1280
1281         MSG_DEBUG("_MmsBinaryEncodeReadReport10Hdr: To = %s\n", pMsg->mmsAttrib.szFrom);
1282
1283         /* To = Encoded-string */
1284         if (pMsg && (strchr(pMsg->mmsAttrib.szFrom, '/') == NULL)) {
1285                 length = strlen(pMsg->mmsAttrib.szFrom);
1286                 szTo = (char *)malloc(length + 11);
1287                 if (szTo == NULL) {
1288                         MSG_DEBUG("_MmsBinaryEncodeReadReport10Hdr: szTo alloc fail\n");
1289                         goto __CATCH;
1290                 }
1291
1292                 snprintf(szTo, length + 11, "%s/TYPE=PLMN", pMsg->mmsAttrib.szFrom);
1293                 MSG_DEBUG("_MmsBinaryEncodeReadReport10Hdr: To = %s\n", szTo);
1294
1295                 if (__MmsBinaryEncodeAddress(pFile, MMS_CODE_TO, szTo) == false) {
1296                         MSG_DEBUG("_MmsBinaryEncodeReadReport10Hdr: To __MmsBinaryEncodeAddress fail\n");
1297                         goto __CATCH;
1298                 }
1299
1300                 if (szTo) {
1301                         free(szTo);
1302                         szTo = NULL;
1303                 }
1304         } else {
1305                 if (__MmsBinaryEncodeAddress(pFile, MMS_CODE_TO, pMsg->mmsAttrib.szFrom) == false) {
1306                         MSG_DEBUG("_MmsBinaryEncodeReadReport10Hdr: To __MmsBinaryEncodeAddress fail\n");
1307                         goto __CATCH;
1308                 }
1309         }
1310
1311         /* Subject = Encoded-string-value */
1312         if (pMsg && pMsg->mmsAttrib.szSubject[0]) {
1313                 if (mmsReadStatus == MSG_READ_REPORT_IS_READ) {
1314                         snprintf(szSubject, MSG_LOCALE_SUBJ_LEN + 8, "%s%s", MMS_READ_REPORT_STRING_READ, pMsg->mmsAttrib.szSubject);
1315                 } else {
1316                         snprintf(szSubject, MSG_LOCALE_SUBJ_LEN + 8, "%s%s", MMS_READ_REPORT_STRING_DELETED, pMsg->mmsAttrib.szSubject);
1317                 }
1318         } else {
1319                 if (mmsReadStatus == MSG_READ_REPORT_IS_READ) {
1320                         snprintf(szSubject, MSG_LOCALE_SUBJ_LEN + 8, "%s", MMS_READ_REPORT_STRING_READ);
1321
1322                 } else {
1323                         snprintf(szSubject, MSG_LOCALE_SUBJ_LEN + 8, "%s", MMS_READ_REPORT_STRING_DELETED );
1324                 }
1325         }
1326
1327         length = MmsBinaryEncodeEncodedStringLen((UINT8*)szSubject);
1328         if (length == -1) {
1329                 MSG_DEBUG("_MmsBinaryEncodeReadReport10Hdr: subject MmsBinaryEncodeEncodedStringLen fail \n");
1330                 goto __CATCH;
1331         }
1332
1333         if ((gMmsEncodeMaxLen - gCurMmsEncodeBuffPos) < length + 1) {
1334                 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos, gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
1335                         MSG_DEBUG("_MmsBinaryEncodeReadReport10Hdr: subject MsgWriteDataFromEncodeBuffer fail \n");
1336                         goto __CATCH;
1337                 }
1338         }
1339
1340         fieldCode = _MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_SUBJECT) | 0x80;
1341         gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = fieldCode;
1342         if (MmsBinaryEncodeEncodedString(pFile, (UINT8*)szSubject, length) == false) {
1343                 MSG_DEBUG("_MmsBinaryEncodeReadReport10Hdr: subject MmsBinaryEncodeEncodedString fail \n");
1344                 goto __CATCH;
1345         }
1346
1347         /* Msg class */
1348         fieldCode  = _MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_MSGCLASS) | 0x80;
1349         fieldValue = _MmsGetBinaryValue(MmsCodeMsgClass, MMS_MSGCLASS_AUTO) | 0x80;
1350         if (__MmsBinaryEncodeFieldCodeAndValue(pFile, fieldCode, fieldValue) == false) {
1351                 MSG_DEBUG("_MmsBinaryEncodeReadReport10Hdr: msgClass error\n");
1352                 goto __CATCH;
1353         }
1354
1355
1356         /* Delivery Report (yes | no) */
1357         fieldCode  = _MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_DELIVERYREPORT) | 0x80;
1358         fieldValue = _MmsGetBinaryValue(MmsCodeDeliveryReport, MMS_REPORT_NO) | 0x80;
1359         if (__MmsBinaryEncodeFieldCodeAndValue(pFile, fieldCode, fieldValue) == false) {
1360                 MSG_DEBUG("_MmsBinaryEncodeReadReport10Hdr: Delivery Report error\n");
1361                 goto __CATCH;
1362         }
1363
1364
1365         /* Read Reply (Yes | no) */
1366         fieldCode  = _MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_READREPLY) | 0x80;
1367         fieldValue = _MmsGetBinaryValue(MmsCodeReadReply, MMS_REPORT_NO) | 0x80;
1368         if (__MmsBinaryEncodeFieldCodeAndValue(pFile, fieldCode, fieldValue) == false) {
1369                 MSG_DEBUG("_MmsBinaryEncodeReadReport10Hdr: Read Reply error\n");
1370                 goto __CATCH;
1371         }
1372
1373
1374         /* Sender Visible (hide | show) */
1375         fieldCode  = _MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_SENDERVISIBILLITY) | 0x80;
1376         fieldValue = _MmsGetBinaryValue(MmsCodeSenderVisibility, MMS_SENDER_SHOW) | 0x80;
1377         if (__MmsBinaryEncodeFieldCodeAndValue(pFile, fieldCode, fieldValue) == false) {
1378                 MSG_DEBUG("_MmsBinaryEncodeReadReport10Hdr: Sender Visible error\n");
1379                 goto __CATCH;
1380         }
1381
1382         /* fixme: msgContentType */
1383         /* fixme: msgHeader */
1384         /* fixme: msgBody */
1385
1386         /* flush remained data on encoding file */
1387         if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
1388                                                                                 gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
1389                 MSG_DEBUG("_MmsBinaryEncodeReadReport10Hdr: remained data MsgWriteDataFromEncodeBuffer fail \n");
1390                 goto __CATCH;
1391         }
1392
1393         return true;
1394
1395 __CATCH:
1396         if (szTo) {
1397                 free(szTo);
1398                 szTo = NULL;
1399         }
1400
1401         return false;
1402 }
1403
1404
1405
1406 bool _MmsBinaryEncodeReadReport11Hdr(FILE *pFile, MmsMsg *pMsg, msg_read_report_status_t mmsReadStatus)
1407 {
1408         UINT8 fieldCode = 0xff;
1409         UINT8 fieldValue = 0xff;
1410         char *szTo = NULL;
1411
1412         __MmsCleanEncodeBuff();
1413
1414         /* msgType */
1415         fieldCode  = _MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_MSGTYPE) | 0x80;
1416         fieldValue = _MmsGetBinaryValue(MmsCodeMsgType, MMS_MSGTYPE_READREC_IND) | 0x80;
1417         if (__MmsBinaryEncodeFieldCodeAndValue(pFile, fieldCode, fieldValue) == false) {
1418                 MSG_DEBUG("_MmsBinaryEncodeReadReport11Hdr: msgType error\n");
1419                 goto __CATCH;
1420         }
1421
1422         /* MMS version */
1423         if (__MmsBinaryEncodeMmsVersion(pFile) == false) {
1424                 MSG_DEBUG("_MmsBinaryEncodeReadReport11Hdr: __MmsBinaryEncodeMmsVersion error\n");
1425                 goto __CATCH;
1426         }
1427
1428         if (__MmsBinaryEncodeMsgID(pFile, pMsg->szMsgID) == false)
1429                 goto __CATCH;
1430
1431         /* To = Encoded-string */
1432         if (strchr(pMsg->mmsAttrib.szFrom, '/') == NULL) {
1433                 int length = 0;
1434                 length = strlen(pMsg->mmsAttrib.szFrom);
1435                 szTo = (char *)malloc(length + 11);
1436
1437                 if (szTo == NULL) {
1438                         MSG_DEBUG("_MmsBinaryEncodeReadReport10Hdr: szTo alloc fail\n");
1439                         goto __CATCH;
1440                 }
1441
1442                 snprintf(szTo, length + 11,"%s/TYPE=PLMN", pMsg->mmsAttrib.szFrom);
1443                 MSG_DEBUG("_MmsBinaryEncodeReadReport10Hdr: To = %s\n", szTo);
1444
1445                 if (__MmsBinaryEncodeAddress(pFile, MMS_CODE_TO, szTo) == false) {
1446                         MSG_DEBUG("_MmsBinaryEncodeReadReport10Hdr: To __MmsBinaryEncodeAddress fail\n");
1447                         goto __CATCH;
1448                 }
1449
1450                 if (szTo) {
1451                         free(szTo);
1452                         szTo = NULL;
1453                 }
1454         } else {
1455                 if (__MmsBinaryEncodeAddress(pFile, MMS_CODE_TO, pMsg->mmsAttrib.szFrom) == false) {
1456                         MSG_DEBUG("_MmsBinaryEncodeReadReport11Hdr: To __MmsBinaryEncodeAddress fail\n");
1457                         goto __CATCH;
1458                 }
1459         }
1460
1461         /* From : Insert Token mode */
1462         if (__MmsBinaryEncodeFrom(pFile) == false) {
1463                 MSG_DEBUG("_MmsBinaryEncodeReadReport11Hdr: __MmsBinaryEncodeFrom fail\n");
1464                 goto __CATCH;
1465         }
1466
1467         /* Date = Long-integer */
1468         if (!__MmsBinaryEncodeDate(pFile)) {
1469                 MSG_DEBUG("_MmsBinaryEncodeReadReport11Hdr: __MmsBinaryEncodeDate error\n");
1470                 goto __CATCH;
1471         }
1472
1473         /* Read Status (Yes | no) */
1474         fieldCode  = _MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_READSTATUS) | 0x80;
1475         fieldValue = _MmsGetBinaryValue(MmsCodeReadStatus, mmsReadStatus) | 0x80;
1476
1477         if (__MmsBinaryEncodeFieldCodeAndValue(pFile, fieldCode, fieldValue) == false) {
1478                 MSG_DEBUG("_MmsBinaryEncodeReadReport11Hdr: Read Status error\n");
1479                 goto __CATCH;
1480         };
1481
1482         if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
1483                                                                                 gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
1484                 MSG_DEBUG("_MmsBinaryEncodeReadReport11Hdr: remained data MsgWriteDataFromEncodeBuffer fail \n");
1485                 goto __CATCH;
1486         }
1487
1488         return true;
1489
1490 __CATCH:
1491         if (szTo) {
1492                 free(szTo);
1493                 szTo = NULL;
1494         }
1495
1496         return false;
1497 }
1498
1499 bool MmsBinaryEncodeMsgBody(FILE *pFile, MsgType *pType, MsgBody *pBody, int nPartCount, bool bTemplate)
1500 {
1501         int length = 0;
1502         MsgMultipart *pMultipart = NULL;
1503
1504         MSG_DEBUG("MmsBinaryEncodeMsgBody: nPartCount = %d\n", nPartCount);
1505
1506         if (pFile == NULL || pType == NULL) {
1507                 MSG_DEBUG("MmsBinaryEncodeMsgBody: invalid file handle\n");
1508                 goto __CATCH;
1509         }
1510
1511         if (MmsIsMultipart(pType->type)) {
1512
1513                 int     nEntries   = 0;
1514                 /* ---------------------------
1515                  *       Multipart message
1516                  * ---------------------------*/
1517
1518                 pType->offset = _MmsGetEncodeOffset();
1519
1520                 /* Content type */
1521                 length = MmsBinaryEncodeContentTypeLen(pType);
1522                 if (length == -1) {
1523                         MSG_DEBUG("MmsBinaryEncodeMsgBody: MmsBinaryEncodeContentTypeLen fail \n");
1524                         goto __CATCH;
1525                 }
1526                 if (bTemplate == false)
1527                         gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = _MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_CONTENTTYPE) | 0x80;
1528
1529                 if (MmsBinaryEncodeContentType(pFile, pType, length) == false) {
1530                         MSG_DEBUG("MmsBinaryEncodeMsgBody: MmsBinaryEncodeContentType fail \n");
1531                         goto __CATCH;
1532                 }
1533
1534                 pBody->offset = _MmsGetEncodeOffset();
1535
1536                 /* nEntries */
1537                 if (pBody->pPresentationBody) {
1538                         nEntries = nPartCount + 1;
1539                 } else {
1540                         nEntries = nPartCount;
1541                 }
1542
1543                 if (nEntries >= 0) {
1544                         length = MmsBinaryEncodeUintvarLen(nEntries);
1545                         if (length == -1) {
1546                                 MSG_DEBUG("MmsBinaryEncodeMsgBody: nEntries MmsBinaryEncodeUintvarLen fail \n");
1547                                 goto __CATCH;
1548                         }
1549                         if (MmsBinaryEncodeUintvar(pFile, nEntries, length) == false) {
1550                                 MSG_DEBUG("MmsBinaryEncodeMsgBody: nEntries MmsBinaryEncodeUintvar fail \n");
1551                                 goto __CATCH;
1552                         }
1553
1554                         pType->size = _MmsGetEncodeOffset() - pType->offset;
1555                 }
1556
1557                 if (nEntries > 0) {
1558                         if (nEntries && pBody->pPresentationBody) {
1559                                 if (MmsBinaryEncodeMsgPart(pFile, pType->type, &pBody->presentationType, pBody->pPresentationBody) == false) {
1560                                         MSG_DEBUG("MmsBinaryEncodeMsgBody: __MmsBinaryEncodeMsgPart fail \n");
1561                                         goto __CATCH;
1562                                 }
1563
1564                                 nEntries--;
1565                         }
1566
1567                         pMultipart = pBody->body.pMultipart;
1568                         while (nEntries && pMultipart) {
1569                                 if (MmsBinaryEncodeMsgPart(pFile, pType->type, &pMultipart->type, pMultipart->pBody) == false) {
1570                                         MSG_DEBUG("MmsBinaryEncodeMsgBody: __MmsBinaryEncodeMsgPart fail \n");
1571                                         goto __CATCH;
1572                                 }
1573                                 pMultipart = pMultipart->pNext;
1574                                 nEntries--;
1575                         }
1576                 } else {
1577                         if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
1578                                                                                                 gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
1579                                 MSG_DEBUG("MmsBinaryEncodeMsgBody: Empty message body MsgWriteDataFromEncodeBuffer fail \n");
1580                                 goto __CATCH;
1581                         }
1582                 }
1583
1584                 pBody->size = _MmsGetEncodeOffset() - pBody->offset;
1585         } else {
1586                 /* ---------------------------
1587                  *       Singlepart message
1588                  * ---------------------------*/
1589                 pType->offset = _MmsGetEncodeOffset();
1590
1591                 if (MmsBinaryEncodeContentHeader(pFile, (MimeType)pType->type, pType, false) == false)
1592                         goto __CATCH;
1593
1594                 length = MmsBinaryEncodeContentTypeLen(pType);
1595                 if (length == -1) {
1596                         MSG_DEBUG("MmsBinaryEncodeMsgBody: Singlepart MmsBinaryEncodeContentTypeLen fail \n");
1597                         goto __CATCH;
1598                 }
1599
1600                 if (MmsBinaryEncodeContentType(pFile, pType, length) == false)
1601                         goto __CATCH;
1602
1603                 pType->size = _MmsGetEncodeOffset() - pType->offset;
1604
1605                 if (MmsBinaryEncodeContentBody(pFile, pBody) == false)
1606                         goto __CATCH;
1607         }
1608
1609         return true;
1610
1611 __CATCH:
1612         MSG_DEBUG("## MmsBinaryEncodeMsgBody: failed\n");
1613
1614         return false;
1615 }
1616
1617
1618 int MmsBinaryEncodeContentTypeLen(MsgType *pType)
1619 {
1620         int length = 0;
1621         int     totalLength = 0;
1622         UINT16 fieldValue = 0xffff;
1623         char *szTextType = NULL;
1624         int     contentType = MIME_UNKNOWN;
1625
1626         MSG_DEBUG("MSmsBinaryEncodeContentTypeLen:  type\n");
1627
1628         /*
1629          * Content-type-value = Constrained-media | Content-general-form
1630          * Constrained-media  = Constrained-encoding
1631          *                                              Constrained-encoding = Extension-Media | Short-integer
1632          *                                              Extension-media = *TEXT End-of-string
1633          * Content-general-form = Value-length Media-type
1634          *                                              Media-type = (Well-known-media | Extension-Media) *(Parameter)
1635          */
1636
1637         /* Content-type-value = Content-general-form ------------------------------- */
1638         /* Content-Type */
1639
1640         contentType = pType->type;
1641
1642         fieldValue = _MmsGetBinaryValue(MmsCodeContentType, contentType);
1643         if (fieldValue == UNDEFINED_BINARY || fieldValue == 0x0049)     {
1644                 /* Extension-media type */
1645                 szTextType = _MmsGetTextValue(MmsCodeContentType, contentType);
1646                 if (szTextType != NULL) {
1647                         length  = MmsBinaryEncodeTextStringLen((UINT8*)szTextType);
1648                         if (length == -1) {
1649                                 MSG_DEBUG("MSmsBinaryEncodeContentTypeLen: szTextType MmsBinaryEncodeTextStringLen fail \n");
1650                                 goto __CATCH;
1651                         }
1652                         totalLength += length;
1653                 } else {
1654                         totalLength++;
1655                 }
1656         } else {
1657                 totalLength++;
1658         }
1659
1660         /* Parameters -------------------------------------------------------------- */
1661
1662         MSG_DEBUG("MSmsBinaryEncodeContentTypeLen:  parameters    \n\n");
1663
1664         /* Well-known-charset = Any-charset | Integer-value ----------------------- */
1665
1666         if (pType->param.charset != MSG_CHARSET_UNKNOWN) {
1667                 fieldValue = _MmsGetBinaryValue(MmsCodeCharSet, pType->param.charset);
1668                 length     = MmsBinaryEncodeIntegerLen(fieldValue);
1669                 if (length == -1) {
1670                         MSG_DEBUG("MmsBinaryEncodeContentTypeLen: charSet MmsBinaryEncodeIntegerLen fail \n");
1671                         goto __CATCH;
1672                 }
1673                 totalLength += (length + 1);
1674         } else {
1675                 if (MmsIsText(contentType)) {   // Any-charset
1676                         if (!MmsIsVitemContent (contentType, pType->param.szName))
1677                                 totalLength += 2;
1678                 }
1679         }
1680
1681
1682         /* Name = Text-string ------------------------------------------------------ */
1683
1684         if (pType->param.szName[0]) {
1685                 char* pszName = NULL;
1686
1687                 if (MsgIsASCII (pType->param.szName)) {
1688                         MSG_DEBUG("MmsBinaryEncodeContentTypeLen: szName is consisted of ascii char-set chars. \n");
1689
1690                         pszName = (char *)malloc(strlen(pType->param.szName) +1);
1691                         memset(pszName, 0, (strlen(pType->param.szName)+1));
1692                         strcpy(pszName, pType->param.szName);
1693                 } else {
1694                         MSG_DEBUG("MmsBinaryEncodeContentTypeLen: szName is not consisted of ascii char-set chars. \n");
1695                         MSG_DEBUG("MmsBinaryEncodeContentTypeLen: pType->param.szName : %s\n", pType->param.szName);
1696
1697                         /* if msg-server can't support non ascii, then convert non-ascii to '_' by using _MsgReplaceNonAscii*/
1698                         int filelen = strlen(pType->param.szName);
1699
1700                         pszName = (char *)malloc(filelen + 1);
1701                         memset(pszName, 0, (filelen + 1));
1702                         strncpy(pszName, pType->param.szName, filelen);
1703
1704                         MSG_DEBUG("MmsBinaryEncodeContentTypeLen: pszName : %s\n", pszName);
1705                 }
1706
1707                 //change empty space to '_' in the file name
1708                 if (MsgIsSpace(pszName)) {
1709                         char *pszTempName = NULL;
1710
1711                         MSG_DEBUG("MmsBinaryEncodeContentTypeLen: szName has space(' '). \n");
1712
1713                         MsgReplaceSpecialChar(pszName, &pszTempName, ' ');
1714
1715                         if (pszTempName) {
1716                                 free(pszName);
1717                                 pszName = pszTempName;
1718                         }
1719                         MSG_DEBUG("MmsBinaryEncodeContentTypeLen: pszName : %s\n", pszName);
1720                 }
1721
1722                 length = MmsBinaryEncodeTextStringLen((UINT8*)pszName);
1723                 free(pszName);
1724
1725                 if (length == -1) {
1726                         MSG_DEBUG("MmsBinaryEncodeContentTypeLen: szName MmsBinaryEncodeIntegerLen fail \n");
1727                         goto __CATCH;
1728                 }
1729
1730                 totalLength += (length + 1);
1731         }
1732
1733 #ifdef FEATURE_JAVA_MMS
1734         if (pType->param.szApplicationID) {
1735                 length = MmsBinaryEncodeTextStringLen((UINT8*)pType->param.szApplicationID);
1736                 if (length == -1) {
1737                         MSG_DEBUG("MmsBinaryEncodeContentTypeLen: szApplicationID MmsBinaryEncodeTextStrinLen fail \n");
1738                         goto __CATCH;
1739                 }
1740
1741                 totalLength += (length);
1742                 if (_MmsGetBinaryValue(MmsCodeParameterCode, MSG_PARAM_APPLICATION_ID) == UNDEFINED_BINARY) {
1743                         totalLength += strlen(_MmsGetTextValue(MmsCodeParameterCode, MSG_PARAM_APPLICATION_ID)) + 1; /* NULL */
1744                 } else {
1745                         totalLength++;
1746                 }
1747         }
1748
1749         if (pType->param.szReplyToApplicationID) {
1750                 length = MmsBinaryEncodeTextStringLen((UINT8*)pType->param.szReplyToApplicationID);
1751                 if (length == -1) {
1752                         MSG_DEBUG("MmsBinaryEncodeContentTypeLen: szApplicationID MmsBinaryEncodeTextStrinLen fail \n");
1753                         goto __CATCH;
1754                 }
1755
1756                 totalLength += (length);
1757                 if (_MmsGetBinaryValue(MmsCodeParameterCode, MSG_PARAM_REPLY_TO_APPLICATION_ID) == UNDEFINED_BINARY) {
1758                         totalLength += strlen(_MmsGetTextValue(MmsCodeParameterCode, MSG_PARAM_REPLY_TO_APPLICATION_ID)) + 1; /* NULL */
1759                 } else {
1760                         totalLength++;
1761                 }
1762         }
1763 #endif
1764
1765         /* type, start, & startInfo : multipart/related only parameters -------------- */
1766         if (contentType == MIME_MULTIPART_RELATED || contentType == MIME_APPLICATION_VND_WAP_MULTIPART_RELATED) {
1767                 /* type ------------------------------------- */
1768                 fieldValue = _MmsGetBinaryValue(MmsCodeContentType, pType->param.type);
1769                 if (fieldValue == UNDEFINED_BINARY || fieldValue == 0x0049) {
1770                         /* Extension-media type */
1771                         szTextType = _MmsGetTextValue(MmsCodeContentType, pType->param.type);
1772                         if (szTextType != NULL) {
1773                                 length  = MmsBinaryEncodeTextStringLen((UINT8*)szTextType);
1774                                 if (length == -1) {
1775                                         MSG_DEBUG("MmsBinaryEncodeContentTypeLen: type param MmsBinaryEncodeTextStringLen fail \n");
1776                                         goto __CATCH;
1777                                 }
1778                                 totalLength += (length + 1);
1779                         } else {
1780                                 totalLength += 2;
1781                         }
1782                 } else {
1783                         totalLength += 2;
1784                 }
1785
1786                 /* start = Text-string ----------------------- */
1787                 if (pType->param.szStart[0]) {
1788                         /* start = Text-string */
1789                         length  = MmsBinaryEncodeTextStringLen((UINT8*)pType->param.szStart);
1790                         if (length == -1) {
1791                                 MSG_DEBUG("MmsBinaryEncodeContentType: szStart MmsBinaryEncodeTextStringLen fail \n");
1792                                 goto __CATCH;
1793                         }
1794
1795                         totalLength += (length + 1);
1796                 }
1797
1798
1799                 /* startInfo = Text-string -------------------- */
1800                 if (pType->param.szStartInfo[0]) {
1801                         /* StartInfo (with multipart/related) = Text-string */
1802                         length  = MmsBinaryEncodeTextStringLen((UINT8*)pType->param.szStartInfo);
1803                         if (length == -1) {
1804                                 MSG_DEBUG("MmsBinaryEncodeContentType: szStartInfo MmsBinaryEncodeTextStringLen fail \n");
1805                                 goto __CATCH;
1806                         }
1807
1808                         totalLength += (length + 1);
1809                 }
1810         }
1811
1812         return totalLength;
1813
1814 __CATCH:
1815
1816         MSG_DEBUG("## MmsBinaryEncodeContentTypeLen: failed");
1817         return -1;
1818 }
1819
1820
1821
1822 bool MmsBinaryEncodeContentType(FILE *pFile, MsgType *pType, int typeLength)
1823 {
1824         int length = 0;
1825         UINT16 fieldValue = 0xffff;
1826         char *szTextType = NULL;
1827         int contentType = MIME_UNKNOWN;
1828
1829 #ifdef FEATURE_JAVA_MMS
1830         char *szParameter = NULL;
1831 #endif
1832
1833         MSG_DEBUG("************************************************************************************\n");
1834         MSG_DEBUG("MmsBinaryEncodeContentType:  C O N T E N T     T Y P E    \n\n");
1835
1836         /*
1837          * Content-type-value = Constrained-media | Content-general-form
1838          * Constrained-media  = Constrained-encoding
1839          *                                              Constrained-encoding = Extension-Media | Short-integer
1840          *                                              Extension-media = *TEXT End-of-string
1841          * Content-general-form = Value-length Media-type
1842          *                                              Media-type = (Well-known-media | Extension-Media) *(Parameter)
1843          */
1844
1845         if (pFile == NULL) {
1846                 MSG_DEBUG("MmsBinaryEncodeContentType: invalid file handle\n");
1847                 goto __CATCH;
1848         }
1849
1850
1851         /* Content-Type = Content-general-form ------------------------------- */
1852
1853         length = MmsBinaryEncodeValueLengthLen(typeLength);
1854         if (length == -1) {
1855                 MSG_DEBUG("MSmsBinaryEncodeContentType : MmsBinaryEncodeValueLengthLen fail.\n");
1856                 goto __CATCH;
1857         }
1858
1859         if (MmsBinaryEncodeValueLength(pFile, typeLength, length) == false) {
1860                 MSG_DEBUG("MSmsBinaryEncodeContentType : MmsBinaryEncodeValueLength fail.\n");
1861                 goto __CATCH;
1862         }
1863
1864         contentType = pType->type;
1865
1866         fieldValue = _MmsGetBinaryValue(MmsCodeContentType, (int)contentType);
1867         if (fieldValue == UNDEFINED_BINARY || fieldValue == 0x0049) {
1868                 /* Extension-media type */
1869                 szTextType = _MmsGetTextValue(MmsCodeContentType, (int)contentType);
1870                 if (szTextType == NULL)
1871                         fieldValue = 0x00;
1872         }
1873
1874         if (szTextType == NULL) {
1875                 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = (UINT8)fieldValue | 0x80;
1876         } else {
1877                 length  = MmsBinaryEncodeTextStringLen((UINT8*)szTextType);
1878                 if (length == -1) {
1879                         MSG_DEBUG("MSmsBinaryEncodeContentType: szTextType MmsBinaryEncodeTextStringLen fail \n");
1880                         goto __CATCH;
1881                 }
1882
1883                 if (MmsBinaryEncodeTextString(pFile, (UINT8*)szTextType, length) == false) {
1884                         MSG_DEBUG("MmsBinaryEncodeContentType: szTextType MmsBinaryEncodeTextString fail \n");
1885                         goto __CATCH;
1886                 }
1887         }
1888
1889         /* Parameters -------------------------------------------------------------- */
1890
1891         MSG_DEBUG("MmsBinaryEncodeContentType:  P A R M E T E R S    \n\n");
1892
1893         /* Name = Text-string ------------------------------------------------------ */
1894
1895         if (pType->param.szName[0]) {
1896                 char* pszName = NULL;
1897
1898                 if (MsgIsASCII (pType->param.szName)) {
1899
1900                         MSG_DEBUG("MmsBinaryEncodeContentType: szName is consisted of ascii char-set chars. \n");
1901
1902                         pszName = (char *)malloc(strlen(pType->param.szName) +1);
1903                         memset(pszName, 0, (strlen(pType->param.szName)+1));
1904                         strcpy(pszName, pType->param.szName);
1905                 } else {
1906                         MSG_DEBUG("MmsBinaryEncodeContentTypeLen: szName is not consisted of ascii char-set chars. \n");
1907
1908                         /* if msg-server can't support non ascii, then convert non-ascii to '_' by using _MsgReplaceNonAscii*/
1909                         int filelen = strlen(pType->param.szName);
1910
1911                         pszName = (char *)malloc(filelen + 1);
1912                         memset(pszName, 0, (filelen + 1));
1913                         strncpy(pszName, pType->param.szName, filelen);
1914
1915                         MSG_DEBUG("MmsBinaryEncodeContentType: pszName : %s\n", pszName);
1916                 }
1917
1918                 //change empty space to '_' in the file name
1919                 if (MsgIsSpace(pszName)) {
1920                         char*   pszTempName = NULL;
1921
1922                         MSG_DEBUG("MmsBinaryEncodeContentType: szName has space(' '). \n");
1923
1924                         MsgReplaceSpecialChar(pszName, &pszTempName, ' ');
1925
1926                         if (pszTempName) {
1927                                 free(pszName);
1928                                 pszName = pszTempName;
1929                         }
1930                         MSG_DEBUG("MmsBinaryEncodeContentType: pszName : %s\n", pszName);
1931                 }
1932
1933                 length = MmsBinaryEncodeTextStringLen((UINT8*)pszName);
1934                 if (length == -1) {
1935                         MSG_DEBUG("MmsBinaryEncodeContentType: szName MmsBinaryEncodeIntegerLen fail \n");
1936                         free(pszName);
1937                         goto __CATCH;
1938                 }
1939                 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = _MmsGetBinaryValue(MmsCodeParameterCode, MSG_PARAM_NAME) | 0x80;
1940
1941                 if (MmsBinaryEncodeTextString(pFile, (UINT8*)pszName, length) == false) {
1942                         MSG_DEBUG("MmsBinaryEncodeContentType: szName MmsBinaryEncodeTextString fail\n");
1943                         free(pszName);
1944                         goto __CATCH;
1945                 }
1946                 free(pszName);
1947         }
1948 #ifdef FEATURE_JAVA_MMS
1949         MSG_DEBUG(" MmsBinaryEncodeContentType: Application-ID \n");
1950
1951         /* Application-ID: Text-string */
1952         if (pType->param.szApplicationID) {
1953                 length = MmsBinaryEncodeTextStringLen((UINT8*) pType->param.szApplicationID);
1954                 if (length == -1) {
1955                         MSG_DEBUG("MmsBinaryEncodeContentType: szApplicationID MmsBinaryEncodeIntegerLen Fail \n");
1956                         goto __CATCH;
1957                 }
1958
1959                 fieldValue = _MmsGetBinaryValue(MmsCodeParameterCode, MSG_PARAM_APPLICATION_ID);
1960
1961                 if (fieldValue == UNDEFINED_BINARY)
1962                         szParameter = _MmsGetTextValue(MmsCodeParameterCode, MSG_PARAM_APPLICATION_ID);
1963
1964                 if (szParameter == NULL) {
1965                         MSG_DEBUG("MmsBinaryEncodeContentType: szParameter is NULL \n");
1966                         goto __CATCH;
1967                 }
1968
1969                 strncpy(gpMmsEncodeBuf + gCurMmsEncodeBuffPos, (char*)szParameter, strlen(szParameter));
1970                 gCurMmsEncodeBuffPos += strlen(szParameter);
1971                 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] =(UINT8)NULL;
1972
1973                 if (MmsBinaryEncodeTextString(pFile, (UINT8*) pType->param.szApplicationID, length) == false) {
1974                         MSG_DEBUG(" MmsBinaryEncodeContentType: szApplicationID MmsBinaryEncodeTextString fail\n");
1975                         goto __CATCH;
1976                 }
1977
1978         }
1979
1980         /* ReplyToApplicationID: Text-string */
1981         if (pType->param.szReplyToApplicationID) {
1982                 length = MmsBinaryEncodeTextStringLen((UINT8*) pType->param.szReplyToApplicationID);
1983                 if (length == -1) {
1984                         MSG_DEBUG("MmsBinaryEncodeContentType: szReplyToApplicationID MmsBinaryEncodeIntegerLen Fail \n");
1985                         goto __CATCH;
1986                 }
1987
1988                 fieldValue = _MmsGetBinaryValue(MmsCodeParameterCode, MSG_PARAM_REPLY_TO_APPLICATION_ID);
1989
1990                 if (fieldValue == UNDEFINED_BINARY)
1991                         szParameter = _MmsGetTextValue(MmsCodeParameterCode, MSG_PARAM_REPLY_TO_APPLICATION_ID);
1992
1993                 if (szParameter == NULL) {
1994                         MSG_DEBUG("MmsBinaryEncodeContentType: szParameter is NULL \n");
1995                         goto __CATCH;
1996                 }
1997
1998                 strncpy(gpMmsEncodeBuf + gCurMmsEncodeBuffPos, (char*)szParameter, strlen(szParameter));
1999                 gCurMmsEncodeBuffPos += strlen(szParameter);
2000                 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] =(UINT8)NULL;
2001
2002                 if (MmsBinaryEncodeTextString(pFile, (UINT8*) pType->param.szReplyToApplicationID, length) == false) {
2003                         MSG_DEBUG(" MmsBinaryEncodeContentType: szApplicationID MmsBinaryEncodeTextString fail\n");
2004                         goto __CATCH;
2005                 }
2006         }
2007 #endif
2008
2009         /* Well-known-charset = Any-charset | Integer-value ----------------------- */
2010
2011         if (pType->param.charset != MSG_CHARSET_UNKNOWN) {
2012                 fieldValue = _MmsGetBinaryValue(MmsCodeCharSet, pType->param.charset);
2013                 length = MmsBinaryEncodeIntegerLen(fieldValue);
2014                 if (length == -1) {
2015                         MSG_DEBUG("MmsBinaryEncodeContentType: charSet MmsBinaryEncodeIntegerLen fail \n");
2016                         goto __CATCH;
2017                 }
2018
2019                 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = _MmsGetBinaryValue(MmsCodeParameterCode, MSG_PARAM_CHARSET) | 0x80;
2020                 if (MmsBinaryEncodeInteger(pFile, fieldValue, length) == false) {
2021                         MSG_DEBUG("MmsBinaryEncodeContentType: charSet MmsBinaryEncodeInteger fail\n");
2022                         goto __CATCH;
2023                 }
2024         } else {
2025                 /* Any-charset */
2026                 if (MmsIsText(contentType)) {
2027                         if (!MmsIsVitemContent (contentType, pType->param.szName)) {
2028                                 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = _MmsGetBinaryValue(MmsCodeParameterCode, MSG_PARAM_CHARSET) | 0x80;
2029                                 fieldValue = 0x0000;    //laconic_warning, just to remove warning message
2030                                 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = (UINT8)fieldValue | 0x80;
2031                         }
2032                 }
2033         }
2034
2035         /* type, start, & startInfo : multipart/related only parameters -------------- */
2036         if (contentType == MIME_MULTIPART_RELATED || contentType == MIME_APPLICATION_VND_WAP_MULTIPART_RELATED) {
2037                 /* type ------------------------------------- */
2038                 fieldValue = _MmsGetBinaryValue(MmsCodeContentType, pType->param.type);
2039                 if (fieldValue == UNDEFINED_BINARY || fieldValue == 0x0049) {
2040                         /* Extension-media type */
2041                         szTextType = _MmsGetTextValue(MmsCodeContentType, pType->param.type);
2042                         if (szTextType == NULL)
2043                                 fieldValue = 0x00;
2044                 }
2045
2046                 if (szTextType == NULL) {
2047                         gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = (UINT8)fieldValue | 0x80;
2048                         gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = _MmsGetBinaryValue(MmsCodeParameterCode, MSG_PARAM_TYPE) | 0x80;
2049                 } else {
2050                         length  = MmsBinaryEncodeTextStringLen((UINT8*)szTextType);
2051                         if (length == -1) {
2052                                 MSG_DEBUG("MmsBinaryEncodeContentType: type param MmsBinaryEncodeTextStringLen fail \n");
2053                                 goto __CATCH;
2054                         }
2055
2056                         gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = _MmsGetBinaryValue(MmsCodeParameterCode, MSG_PARAM_TYPE) | 0x80;
2057                         if (MmsBinaryEncodeTextString(pFile, (UINT8*)szTextType, length) == false) {
2058                                 MSG_DEBUG("MmsBinaryEncodeContentType: type param MmsBinaryEncodeTextString fail \n");
2059                                 goto __CATCH;
2060                         }
2061                 }
2062
2063                 /* start = Text-string ----------------------- */
2064                 if (pType->param.szStart[0]) {
2065                         /* start = Text-string */
2066                         length  = MmsBinaryEncodeTextStringLen((UINT8*)pType->param.szStart);
2067                         if (length == -1) {
2068                                 MSG_DEBUG("MmsBinaryEncodeContentType: szStart MmsBinaryEncodeTextStringLen fail \n");
2069                                 goto __CATCH;
2070                         }
2071
2072                         gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = _MmsGetBinaryValue(MmsCodeParameterCode,
2073                                                                                                                                                  MSG_PARAM_START) | 0x80;
2074                         if (MmsBinaryEncodeTextString(pFile, (UINT8*)pType->param.szStart, length) == false) {
2075                                 MSG_DEBUG("MmsBinaryEncodeContentType: szStart MmsBinaryEncodeTextString fail \n");
2076                                 goto __CATCH;
2077                         }
2078                 }
2079
2080                 /* startInfo = Text-string -------------------- */
2081                 if (pType->param.szStartInfo[0]) {
2082                         /* StartInfo (with multipart/related) = Text-string */
2083                         length  = MmsBinaryEncodeTextStringLen((UINT8*)pType->param.szStartInfo);
2084                         if (length == -1) {
2085                                 MSG_DEBUG("MmsBinaryEncodeContentType: szStartInfo MmsBinaryEncodeTextStringLen fail \n");
2086                                 goto __CATCH;
2087                         }
2088
2089                         gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = _MmsGetBinaryValue(MmsCodeParameterCode, MSG_PARAM_START_INFO) | 0x80;
2090                         if (MmsBinaryEncodeTextString(pFile, (UINT8*)pType->param.szStartInfo, length) == false) {
2091                                 MSG_DEBUG("MmsBinaryEncodeContentType: szStartInfo MmsBinaryEncodeTextString fail \n");
2092                                 goto __CATCH;
2093                         }
2094                 }
2095         }
2096
2097         return true;
2098
2099 __CATCH:
2100         MSG_DEBUG("## MmsBinaryEncodeContentType: failed");
2101         return false;
2102 }
2103
2104
2105
2106 bool MmsBinaryEncodeMsgPart(FILE *pFile, int contentType, MsgType *pType, MsgBody *pBody)
2107 {
2108         FILE *pFile2 = NULL;
2109         char *pData = NULL;
2110         int     length = 0;
2111
2112
2113         if (pType->offset && pType->size) {
2114                 /* header length & body length --------------------------- */
2115
2116                 length = MmsBinaryEncodeUintvarLen(pType->size);
2117                 if (length == -1) {
2118                         MSG_DEBUG("MmsBinaryEncodeMsgPart: 1. headerLeng MmsBinaryEncodeUintvarLen fail \n");
2119                         goto __CATCH;
2120                 }
2121
2122                 if (MmsBinaryEncodeUintvar(pFile, pType->size, length) == false) {
2123                         MSG_DEBUG("MmsBinaryEncodeMsgPart: 1. eaderLeng fail \n");
2124                         goto __CATCH;
2125                 }
2126
2127                 length = MmsBinaryEncodeUintvarLen(pBody->size);
2128                 if (length == -1) {
2129                         MSG_DEBUG("MmsBinaryEncodeMsgPart: 1. bodyLeng MmsBinaryEncodeUintvarLen fail \n");
2130                         goto __CATCH;
2131                 }
2132
2133                 if (MmsBinaryEncodeUintvar(pFile, pBody->size, length) == false) {
2134                         MSG_DEBUG("MmsBinaryEncodeMsgPart: 1. bodyLeng fail \n");
2135                         goto __CATCH;
2136                 }
2137
2138                 pFile2 = MsgOpenFile(pType->szOrgFilePath, "rb");
2139                 if (pFile != NULL) {
2140                         pData = (char *)malloc(pType->size);
2141                         if (pData == NULL)
2142                                 goto __CATCH;
2143
2144                         if (MsgFseek(pFile2, pType->offset, SEEK_SET) < 0) {
2145                                 MSG_DEBUG("MmsBinaryEncodeMsgPart: MsgFseek fail \n");
2146                                 goto __CATCH;
2147                         }
2148
2149                         ULONG nRead = 0;
2150
2151                         if ((nRead = MsgReadFile(pData, sizeof(char), pType->size, pFile2)) == 0) {
2152                                 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
2153                                                                                                         gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
2154                                         MSG_DEBUG("MmsBinaryEncodeMsgPart: 1. header MsgWriteDataFromEncodeBuffer fail \n");
2155                                         goto __CATCH;
2156                                 }
2157                                 pType->offset = _MmsGetEncodeOffset();
2158                                 if(MsgWriteFile(pData, sizeof(char), nRead, pFile) != (size_t)nRead) {
2159                                         MSG_DEBUG("MsgWriteFile failed");
2160                                         goto __CATCH;
2161                                 }
2162                                 gMmsEncodeCurOffset = MsgFtell(pFile);
2163                                 if(gMmsEncodeCurOffset < 0) {
2164                                         MSG_DEBUG("MsgFtell returns negative value [%ld]", gMmsEncodeCurOffset);
2165                                         goto __CATCH;
2166                                 }
2167                         }
2168
2169                         MsgCloseFile(pFile2);
2170                         pFile2 = NULL;
2171                 }
2172         } else {
2173                 int headerLeng     = 0;
2174                 int contentTypeLen = 0;
2175                 int contentHdrLen  = 0;
2176
2177                 /* header length & body length --------------------------- */
2178                 contentTypeLen = MmsBinaryEncodeContentTypeLen(pType);
2179                 length = MmsBinaryEncodeValueLengthLen(contentTypeLen);
2180                 contentHdrLen  = MmsBinaryEncodeContentHeaderLen((MimeType)contentType, pType, true);
2181
2182                 if (contentTypeLen == -1 || length == -1 || contentHdrLen == -1) {
2183                         MSG_DEBUG("MmsBinaryEncodeMsgPart: headerLeng calculation fail \n");
2184                         goto __CATCH;
2185                 }
2186
2187                 headerLeng = contentTypeLen + contentHdrLen + length;
2188                 length = MmsBinaryEncodeUintvarLen(headerLeng);
2189                 if (length == -1) {
2190                         MSG_DEBUG("MmsBinaryEncodeMsgPart: headerLeng MmsBinaryEncodeUintvarLen fail \n");
2191                         goto __CATCH;
2192                 }
2193
2194                 if (MmsBinaryEncodeUintvar(pFile, headerLeng, length) == false) {
2195                         MSG_DEBUG("MmsBinaryEncodeMsgPart: headerLeng fail \n");
2196                         goto __CATCH;
2197                 }
2198
2199                 length = MmsBinaryEncodeUintvarLen(pBody->size);
2200                 if (length == -1) {
2201                         MSG_DEBUG("MmsBinaryEncodeMsgPart: bodyLeng MmsBinaryEncodeUintvarLen fail \n");
2202                         goto __CATCH;
2203                 }
2204
2205                 if (MmsBinaryEncodeUintvar(pFile, pBody->size, length) == false) {
2206                         MSG_DEBUG("MmsBinaryEncodeMsgPart: bodyLeng fail \n");
2207                         goto __CATCH;
2208                 }
2209
2210                 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
2211                                                                                         gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
2212                         MSG_DEBUG("MmsBinaryEncodeMsgPart: 2. header MsgWriteDataFromEncodeBuffer fail \n");
2213                         goto __CATCH;
2214                 }
2215
2216                 /* content-type & header --------------------------- */
2217                 pType->offset = _MmsGetEncodeOffset();
2218
2219                 if (MmsBinaryEncodeContentType(pFile, pType, contentTypeLen) == false) {
2220                         MSG_DEBUG("MmsBinaryEncodeMsgPart: MmsBinaryEncodeContentType fail \n");
2221                         goto __CATCH;
2222                 }
2223
2224                 if (MmsBinaryEncodeContentHeader(pFile, (MsgContentType)contentType, pType, true) == false)     {
2225                         MSG_DEBUG("MmsBinaryEncodeMsgPart: MmsBinaryEncodeContentHeader fail \n");
2226                         goto __CATCH;
2227                 }
2228
2229                 pType->size = _MmsGetEncodeOffset() - pType->offset;
2230         }
2231
2232         if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos, gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
2233                 MSG_DEBUG("MmsBinaryEncodeMsgPart: contentBody MsgWriteDataFromEncodeBuffer fail \n");
2234                 goto __CATCH;
2235         }
2236
2237         /* content-body --------------------------- */
2238         if (MmsBinaryEncodeContentBody(pFile, pBody) == false) {
2239                 MSG_DEBUG("MmsBinaryEncodeMsgPart: MmsBinaryEncodeContentBody fail \n");
2240                 goto __CATCH;
2241         }
2242
2243         if (pData) {
2244                 free(pData);
2245                 pData = NULL;
2246         }
2247
2248         return true;
2249
2250 __CATCH:
2251
2252         if (pData) {
2253                 free(pData);
2254                 pData = NULL;
2255         }
2256
2257         MSG_DEBUG("## MmsBinaryEncodeMsgPart: failed\n");
2258         if (pFile2) {
2259                 MsgCloseFile(pFile2);
2260                 pFile2 = NULL;
2261         }
2262
2263         return false;
2264 }
2265
2266
2267 int MmsBinaryEncodeContentHeaderLen(MsgContentType contentType, MsgType *pType, bool bMultipart)
2268 {
2269         int     length = 0;
2270         int     totalLength = 0;
2271         char *szTextValue = NULL;
2272
2273
2274         MSG_DEBUG("MmsBinaryEncodeContentHeaderLen: S T A R T \n\n");
2275
2276         /* content-id ------------------------------------------------- */
2277         if (pType->szContentID[0]) {
2278                 if (bMultipart) { //Binary Encoding
2279                         totalLength++;
2280                 } else {
2281                         /* content-id = Quoted-string */
2282                         length = MmsBinaryEncodeTextStringLen((UINT8*)"Content-ID");
2283                         if (length == -1) {
2284                                 MSG_DEBUG("MmsBinaryEncodeContentHeaderLen: Content-ID MmsBinaryEncodeTextStringLen fail.\n");
2285                                 goto __CATCH;
2286                         }
2287
2288                         totalLength += length;
2289                 }
2290
2291                 length = MmsBinaryEncodeQuotedStringLen((UINT8*)pType->szContentID);
2292                 if (length == -1) {
2293                         MSG_DEBUG("MmsBinaryEncodeContentHeader: pType->szContentID MmsBinaryEncodeQuotedStringLen fail.\n");
2294                         goto __CATCH;
2295                 }
2296                 totalLength += length;
2297         }
2298
2299
2300         if (pType->szContentLocation[0]) {
2301                 if (bMultipart) { //Binary Encoding
2302                         totalLength++;
2303                 } else {
2304                         /* content-location = Quoted-string */
2305                         length = MmsBinaryEncodeTextStringLen((UINT8*)"Content-Location");
2306                         if (length == -1) {
2307                                 MSG_DEBUG("MmsBinaryEncodeContentHeader: Content-Location MmsBinaryEncodeTextStringLen fail.\n");
2308                                 goto __CATCH;
2309                         }
2310
2311                         totalLength += length;
2312                 }
2313
2314                 length = MmsBinaryEncodeTextStringLen((UINT8*)pType->szContentLocation);
2315                 if (length == -1) {
2316                         MSG_DEBUG("MmsBinaryEncodeContentHeader: pType->szContentLocation MmsBinaryEncodeTextStringLen fail.\n");
2317                         goto __CATCH;
2318                 }
2319
2320                 totalLength += length;
2321         }
2322
2323
2324         /* MIME_APPLICATION_VND_WAP_MULTIPART_RELATEDrequires always "inline" */
2325
2326         if (contentType != MIME_APPLICATION_VND_WAP_MULTIPART_RELATED &&
2327                 contentType != MIME_MULTIPART_RELATED &&
2328                 pType->disposition != INVALID_VALUE) {
2329
2330                 /*
2331                  * Content-disposition-value = Value-length Disposition *(Parameter)
2332                  * Disposition = Form-data | Attachment | Inline | Token-text
2333                  * Form-data = <Octet 128> : 0x80
2334                  * Attachment = <Octet 129> : 0x81
2335                  * Inline = <Octet 130> : 0x82
2336                  */
2337
2338                 if (bMultipart) { //Binary Encoding
2339                         totalLength += 3;
2340                 } else {
2341                         /* content-disposition = Quoted-string */
2342                         szTextValue = _MmsGetTextValue(MmsCodeMsgDisposition, pType->disposition);
2343
2344                         if (szTextValue) {
2345                                 length = MmsBinaryEncodeTextStringLen((UINT8*)"Content-Disposition");
2346                                 if (length == -1) {
2347                                         MSG_DEBUG("MmsBinaryEncodeContentHeader: Content-Disposition MmsBinaryEncodeTextStringLen fail.\n");
2348                                         goto __CATCH;
2349                                 }
2350
2351                                 totalLength += length;
2352
2353                                 length = MmsBinaryEncodeTextStringLen((UINT8*)szTextValue);
2354                                 if (length == -1) {
2355                                         MSG_DEBUG("MmsBinaryEncodeContentHeader: Content-Disposition MmsBinaryEncodeTextStringLen fail.\n");
2356                                         goto __CATCH;
2357                                 }
2358                                 totalLength += length;
2359                         }
2360                 }
2361         }
2362
2363         return totalLength;
2364
2365 __CATCH:
2366         MSG_DEBUG("## MmsBinaryEncodeContentHeadeLen: failed");
2367
2368         return -1;
2369 }
2370
2371
2372
2373 bool MmsBinaryEncodeContentHeader(FILE *pFile, MsgContentType contentType, MsgType *pType, bool bMultipart)
2374 {
2375         int     length = 0;
2376         char *szTextValue = NULL;
2377
2378         MSG_DEBUG("MmsBinaryEncodeContentHeader: S T A R T \n\n");
2379
2380         /* content-id ------------------------------------------------- */
2381         if (pType->szContentID[0]) {
2382                 if (bMultipart) { //Binary Encoding
2383                         gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = _MmsGetBinaryValue(MmsCodeMsgBodyHeaderCode, MMS_BODYHDR_CONTENTID) | 0x80;
2384                 } else {
2385                         /* content-id = Quoted-string */
2386                         length = MmsBinaryEncodeTextStringLen((UINT8*)"Content-ID");
2387                         if (length == -1) {
2388                                 MSG_DEBUG("MmsBinaryEncodeContentHeader: Content-ID MmsBinaryEncodeTextStringLen fail.\n");
2389                                 goto __CATCH;
2390                         }
2391
2392                         if (MmsBinaryEncodeTextString(pFile, (UINT8*)"Content-ID", length) == false) {
2393                                 MSG_DEBUG("MmsBinaryEncodeContentHeader: Content-ID MmsBinaryEncodeTextString fail.\n");
2394                                 goto __CATCH;
2395                         }
2396                 }
2397
2398                 length = MmsBinaryEncodeQuotedStringLen((UINT8*)pType->szContentID);
2399                 if (length == -1) {
2400                         MSG_DEBUG("MmsBinaryEncodeContentHeader: pType->szContentID MmsBinaryEncodeQuotedStringLen fail.\n");
2401                         goto __CATCH;
2402                 }
2403
2404                 if (MmsBinaryEncodeQuotedString(pFile, (UINT8*)pType->szContentID, length) == false) {
2405                         MSG_DEBUG("MmsBinaryEncodeContentHeader: pType->szContentID MmsBinaryEncodeQuotedString fail.\n");
2406                         goto __CATCH;
2407                 }
2408         }
2409
2410         if (pType->szContentLocation[0]) {
2411                 if (bMultipart) { //Binary Encoding
2412                         gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = _MmsGetBinaryValue(MmsCodeMsgBodyHeaderCode, MMS_BODYHDR_CONTENTLOCATION) | 0x80;
2413                 } else {
2414                         /* content-location = Quoted-string */
2415                         length = MmsBinaryEncodeTextStringLen((UINT8*)"Content-Location");
2416                         if (length == -1) {
2417                                 MSG_DEBUG("MmsBinaryEncodeContentHeader: Content-Location MmsBinaryEncodeTextStringLen fail.\n");
2418                                 goto __CATCH;
2419                         }
2420
2421                         if (MmsBinaryEncodeTextString(pFile, (UINT8*)"Content-Location", length) == false) {
2422                                 MSG_DEBUG("MmsBinaryEncodeContentHeader: Content-Location MmsBinaryEncodeTextString fail.\n");
2423                                 goto __CATCH;
2424                         }
2425                 }
2426
2427                 length = MmsBinaryEncodeTextStringLen((UINT8*)pType->szContentLocation);
2428                 if (length == -1) {
2429                         MSG_DEBUG("MmsBinaryEncodeContentHeader: pType->szContentLocation MmsBinaryEncodeTextStringLen fail.\n");
2430                         goto __CATCH;
2431                 }
2432
2433                 if (MmsBinaryEncodeTextString(pFile, (UINT8*)pType->szContentLocation, length) == false) {
2434                         MSG_DEBUG("MmsBinaryEncodeContentHeader: pType->szContentLocation MmsBinaryEncodeTextString fail.\n");
2435                         goto __CATCH;
2436                 }
2437         }
2438
2439         /* MIME_APPLICATION_VND_WAP_MULTIPART_RELATEDrequires always "inline" */
2440
2441         if (contentType != MIME_APPLICATION_VND_WAP_MULTIPART_RELATED &&
2442                 contentType != MIME_MULTIPART_RELATED &&
2443                 pType->disposition != INVALID_VALUE) {
2444
2445                 /*
2446                  * Content-disposition-value = Value-length Disposition *(Parameter)
2447                  * Disposition = Form-data | Attachment | Inline | Token-text
2448                  * Form-data = <Octet 128> : 0x80
2449                  * Attachment = <Octet 129> : 0x81
2450                  * Inline = <Octet 130> : 0x82
2451                  */
2452
2453                 if (bMultipart) {//Binary Encoding
2454
2455                         UINT8 fieldValue  = 0xff;
2456                         gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = _MmsGetBinaryValue(MmsCodeMsgBodyHeaderCode, MMS_BODYHDR_DISPOSITION) | 0x80;
2457
2458                         fieldValue = _MmsGetBinaryValue(MmsCodeMsgDisposition, pType->disposition) | 0x80;
2459
2460                         if (fieldValue == 0xff)
2461                                 fieldValue = 0x81;
2462
2463                         gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = 0x01;
2464                         gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = fieldValue;
2465                 } else {
2466                         /* content-disposition = Disposition (no support parameter) */
2467
2468                         szTextValue = _MmsGetTextValue(MmsCodeMsgDisposition, pType->disposition);
2469
2470                         if (szTextValue) {
2471                                 length = MmsBinaryEncodeTextStringLen((UINT8*)"Content-Disposition");
2472                                 if (length == -1) {
2473                                         MSG_DEBUG("MmsBinaryEncodeContentHeader: Content-Disposition MmsBinaryEncodeTextStringLen fail.\n");
2474                                         goto __CATCH;
2475                                 }
2476
2477                                 if (MmsBinaryEncodeTextString(pFile, (UINT8*)"Content-Disposition", length) == false) {
2478                                         MSG_DEBUG("MmsBinaryEncodeContentHeader: Content-Disposition MmsBinaryEncodeTextString fail.\n");
2479                                         goto __CATCH;
2480                                 }
2481
2482                                 length = MmsBinaryEncodeTextStringLen((UINT8*)szTextValue);
2483                                 if (length == -1) {
2484                                         MSG_DEBUG("MmsBinaryEncodeContentHeader: Content-Disposition MmsBinaryEncodeTextStringLen fail.\n");
2485                                         goto __CATCH;
2486                                 }
2487
2488                                 if (MmsBinaryEncodeTextString(pFile, (UINT8*)szTextValue, length) == false) {
2489                                         MSG_DEBUG("MmsBinaryEncodeContentHeader: Content-Disposition MmsBinaryEncodeTextString fail.\n");
2490                                         goto __CATCH;
2491                                 }
2492                         }
2493                 }
2494         }
2495
2496         return true;
2497
2498 __CATCH:
2499         MSG_DEBUG("## MmsBinaryEncodeContentHeader: failed");
2500         return false;
2501 }
2502
2503
2504
2505 bool MmsBinaryEncodeContentBody(FILE *pFile, MsgBody *pBody)
2506 {
2507         int nRead = 0;
2508         char *pData = NULL;
2509
2510
2511         if (pFile == NULL)
2512                 goto __CATCH;
2513
2514         if (pBody == NULL)
2515                 return true;
2516
2517         if (pBody->szOrgFilePath[0]) {
2518                 pData = MsgOpenAndReadMmsFile(pBody->szOrgFilePath, pBody->offset, pBody->size, &nRead);
2519                 if (pData == NULL)
2520                         goto __CATCH;
2521
2522                 pBody->offset = _MmsGetEncodeOffset();
2523                 if(MsgWriteFile(pData, sizeof(char), nRead, pFile) != (size_t)nRead) {
2524                         MSG_DEBUG("MsgWriteFile failed");
2525                         goto __CATCH;
2526                 }
2527                 gMmsEncodeCurOffset = MsgFtell(pFile);
2528
2529                 if (gMmsEncodeCurOffset < 0) {
2530                         MSG_DEBUG("MsgFtell returns negative value [%ld]", gMmsEncodeCurOffset);
2531                         goto __CATCH;
2532                 }
2533
2534                 if (pData) {
2535                         free(pData);
2536                         pData = NULL;
2537                 }
2538         } else if (pBody->body.pText && pBody->size) {
2539                 pBody->offset = _MmsGetEncodeOffset();
2540                 if (MsgWriteFile(pBody->body.pText, sizeof(char),(size_t)pBody->size, pFile) != (size_t)pBody->size) {
2541                         MSG_DEBUG("MsgWriteFile failed");
2542                         goto __CATCH;
2543                 }
2544                 gMmsEncodeCurOffset = MsgFtell(pFile);
2545
2546                 if(gMmsEncodeCurOffset < 0) {
2547                         MSG_DEBUG("MsgFtell returns negative value [%ld]", gMmsEncodeCurOffset);
2548                         goto __CATCH;
2549                 }
2550         }
2551
2552         return true;
2553
2554 __CATCH:
2555         MSG_DEBUG("## MmsBinaryEncodeContentBody: failed\n");
2556         if (pData) {
2557                 free(pData);
2558                 pData = NULL;
2559         }
2560
2561         return false;
2562 }
2563
2564
2565
2566 /* =========================================================================
2567
2568    B  I  N  A  R  Y       E  N  C  O  D  I  N  G      U  T  I  L  I  T  Y
2569
2570    =========================================================================*/
2571
2572 static int MmsBinaryEncodeIntegerLen(UINT32 integer)
2573 {
2574         if (integer < 0x80) {
2575                 /* short-integer */
2576                 return 1;
2577         } else {
2578
2579                 int     length = 0;
2580                 /*
2581                  * Long-integer = Short-length Multi-octet-integer
2582                  * The Short-length indicates the length of the Multi-octet-integer
2583                  */
2584
2585                 while (integer) {
2586                         length++;
2587                         integer = (integer >> 8);
2588                 }
2589
2590                 length++;       // + Short-length
2591
2592                 return length;
2593         }
2594 }
2595
2596
2597 /*
2598  * This makes value-length by specified integer value
2599  *
2600  * @param       length [in] gotten from MmsBinaryEncodeIntegerLen()
2601  */
2602 static bool MmsBinaryEncodeInteger(FILE *pFile, UINT32 integer, int length)
2603 {
2604         union {
2605                 UINT32 integer;
2606                 UINT8 seg[4];
2607         } changer;
2608
2609
2610         if (pFile == NULL) {
2611                 MSG_DEBUG("MmsBinaryEncodeInteger: source == NULL \n");
2612                 goto __CATCH;
2613         }
2614
2615         if ((gMmsEncodeMaxLen - gCurMmsEncodeBuffPos) < length) {
2616                 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
2617                                                                                         gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
2618                         MSG_DEBUG("MmsBinaryEncodeInteger: MsgWriteDataFromEncodeBuffer fail \n");
2619                         goto __CATCH;
2620                 }
2621         }
2622
2623         if (integer < 0x80) {
2624                 /* short-integer */
2625                 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = (UINT8)integer | 0x80;
2626         } else {
2627                 /*
2628                  * Long-integer = Short-length Multi-octet-integer
2629                  * The Short-length indicates the length of the Multi-octet-integer
2630                  */
2631                 changer.integer = integer;
2632                 length--;                                       // - "Short-length"
2633
2634                 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = (UINT8)length;
2635
2636                 int i;
2637                 for(i = 0; i < length; i++)
2638                         gpMmsEncodeBuf[gCurMmsEncodeBuffPos + i] = changer.seg[length - (i + 1)];
2639
2640                 gCurMmsEncodeBuffPos += length;
2641         }
2642
2643         return true;
2644
2645 __CATCH:
2646         return false;
2647 }
2648
2649
2650 static int MmsBinaryEncodeLongIntegerLen(UINT32 integer)
2651 {
2652         int length = 0;
2653
2654         /*
2655          * Long-integer = Short-length Multi-octet-integer
2656          * The Short-length indicates the length of the Multi-octet-integer
2657          */
2658
2659         if (integer == 0)
2660                 return 2;
2661
2662         while (integer) {
2663                 length++;
2664                 integer = (integer >> 8);
2665         }
2666
2667         length++;       // + Short-length
2668
2669         return length;
2670 }
2671
2672
2673 /*
2674  * This makes value-length by specified integer value
2675  *
2676  * @param       length [in] gotten from MmsBinaryEncodeIntegerLen()
2677  */
2678 static bool MmsBinaryEncodeLongInteger(FILE *pFile, UINT32 integer, int length)
2679 {
2680         int i  = 0;
2681         union {
2682                 UINT32 integer;
2683                 UINT8 seg[4];
2684         }changer;
2685
2686
2687         /*
2688          * Long-integer = Short-length Multi-octet-integer
2689          * The Short-length indicates the length of the Multi-octet-integer
2690          */
2691
2692         if (pFile == NULL) {
2693                 MSG_DEBUG("MmsBinaryEncodeLongInteger: source == NULL \n");
2694                 goto __CATCH;
2695         }
2696
2697         if ((gMmsEncodeMaxLen - gCurMmsEncodeBuffPos) < length) {
2698                 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
2699                                                                                         gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
2700                         MSG_DEBUG("MmsBinaryEncodeLongInteger: MsgWriteDataFromEncodeBuffer fail \n");
2701                         goto __CATCH;
2702                 }
2703         }
2704
2705         changer.integer = integer;
2706         length--;                                       // - "Short-length"
2707
2708         gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = (UINT8)length;
2709
2710         for(i = 0; i < length; i++)
2711                 gpMmsEncodeBuf[gCurMmsEncodeBuffPos + i] = changer.seg[length - (i + 1)];
2712
2713         gCurMmsEncodeBuffPos += length;
2714
2715         return true;
2716
2717 __CATCH:
2718         return false;
2719 }
2720
2721
2722
2723 static int MmsBinaryEncodeTextStringLen(UINT8 *source)
2724 {
2725         int      length = 0;
2726
2727         MSG_DEBUG("MmsBinaryEncodeTextStringLen: \n");
2728
2729         if (source == NULL) {
2730                 MSG_DEBUG("MmsBinaryEncodeTextStringLen: source == NULL \n");
2731                 return -1;
2732         }
2733
2734         length = (int)strlen((char*)source);
2735         if (source[0] > 0x7F) {
2736                 length += 2;                    // + NULL
2737         } else {
2738                 length++;                               // + NULL
2739         }
2740
2741         return length;
2742 }
2743
2744
2745 /*
2746  * @param       source [in] originam string
2747  * @param       length [in] gotten from MmsBinaryEncodeTextStringLen()
2748  * @param       dest [in] buffer to store quted string
2749  * @return      changed string length
2750 */
2751 static bool MmsBinaryEncodeTextString(FILE *pFile, UINT8 *source, int length)
2752 {
2753
2754         MSG_DEBUG("MmsBinaryEncodeTextString: \n");
2755
2756         /*
2757          * make text string
2758          * Text-string = [Quote] *TEXT End-of-string
2759          * If the 1st char in the TEXT is in the range of 128-255, a Quote char must precede it.
2760          * Otherwise the Quote char must be omitted.
2761          * The Quote is not part of the contents.
2762          * Quote = <Octet 127>
2763          */
2764
2765         if (pFile == NULL || source == NULL) {
2766                 MSG_DEBUG("MmsBinaryEncodeTextString: source == NULL \n");
2767                 goto __CATCH;
2768         }
2769
2770         if ((gMmsEncodeMaxLen - gCurMmsEncodeBuffPos) < length) {
2771                 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
2772                                                                                         gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
2773                         MSG_DEBUG("MmsBinaryEncodeTextString: MsgWriteDataFromEncodeBuffer fail \n");
2774                         goto __CATCH;
2775                 }
2776         }
2777
2778         if (source[0] > 0x7F) {
2779                 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = QUOTE;
2780                 length--;
2781         }
2782
2783         strncpy(gpMmsEncodeBuf + gCurMmsEncodeBuffPos, (char*)source, (length - 1));    // except NULL
2784         gCurMmsEncodeBuffPos += (length - 1);                   // except NULL
2785         gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = (UINT8)NULL;
2786
2787         return true;
2788
2789 __CATCH:
2790         return false;
2791 }
2792
2793
2794
2795 /*
2796  * Encode 28bit unsigned integer(Maximum) to uintvar
2797  *
2798  * @param       interger [in] integer to be encoded
2799  * @return      encoded UINTVAR stream
2800 */
2801 const UINT32 UINTVAR_LENGTH_1 =  0x0000007f;            //7bit
2802 const UINT32 UINTVAR_LENGTH_2 =  0x00003fff;            //14bit
2803 const UINT32 UINTVAR_LENGTH_3 =  0x001fffff;            //21bit
2804
2805
2806 static int MmsBinaryEncodeUintvarLen(UINT32 integer)
2807 {
2808         UINT32          length  = 0;
2809
2810         /* Find encoded unitvar length */
2811         if (integer  <= UINTVAR_LENGTH_1) {
2812                 length = 1;
2813         } else {
2814                 if (integer <= UINTVAR_LENGTH_2) {
2815                         length = 2;
2816                 } else {
2817                         if (integer <= UINTVAR_LENGTH_3) {
2818                                 length = 3;
2819                         } else {
2820                                 length = 4;
2821                         }
2822                 }
2823         }
2824
2825         return length;
2826 }
2827
2828
2829 static bool MmsBinaryEncodeUintvar(FILE *pFile, UINT32 integer, int length)
2830 {
2831         const char ZERO = 0x00;
2832         int i = 2;
2833         char szReverse[MSG_STDSTR_LONG] = {0, };
2834
2835         union {
2836                 UINT32  integer;
2837                 char    bytes[4];
2838         } source;
2839
2840         if (pFile == NULL) {
2841                 MSG_DEBUG("MmsBinaryEncodeUintvar: pFile == INVALID_HBOJ \n");
2842                 goto __CATCH;
2843         }
2844
2845         if ((gMmsEncodeMaxLen - gCurMmsEncodeBuffPos) < length) {
2846                 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
2847                                                                                         gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
2848                         MSG_DEBUG("MmsBinaryEncodeUintvar: MsgWriteDataFromEncodeBuffer fail \n");
2849                         goto __CATCH;
2850                 }
2851         }
2852
2853         source.integer = integer;
2854         memset(szReverse, 0, MSG_STDSTR_LONG);
2855
2856         /* Seperate integer to 4 1 byte integer */
2857         szReverse[3] = source.bytes[3] & 0x0f;
2858         szReverse[0] = source.bytes[0];
2859         szReverse[0] = szReverse[0] & 0x7f;
2860
2861         while (length >= i) { // initially, i = 2
2862                 /* Move integer bit to proper position */
2863                 source.integer = source.integer << 1;
2864                 source.integer = source.integer >> 8;
2865                 source.bytes[3] = ZERO;
2866
2867                 /* Retrive 1 encode uintvar */
2868                 szReverse[i-1] = source.bytes[0];
2869                 szReverse[i-1] = szReverse[i-1] | 0x80;
2870                 i++;
2871         }
2872
2873         for(i=0; i < length; i++)
2874                 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = szReverse[length - i - 1];
2875
2876         return true;
2877
2878 __CATCH:
2879         return false;
2880 }
2881
2882
2883
2884 static int MmsBinaryEncodeValueLengthLen(UINT32 integer)
2885 {
2886         int length = 0;
2887
2888         if (integer < 0x1f) {
2889                 length = 1;
2890         } else {
2891                 length = MmsBinaryEncodeUintvarLen(integer) + 1;                //LENGTH_QUOTE
2892         }
2893
2894         return length;
2895 }
2896
2897
2898 /*
2899  * This makes value-length by specified integer value
2900  *
2901  * @param       length [in] from MmsBinaryEncodeValueLengthLen()
2902  * @return      encoded value-length
2903  */
2904 static bool MmsBinaryEncodeValueLength(FILE *pFile, UINT32 integer, int length)
2905 {
2906         /*
2907          * Value-length = Short-length | (Length-quote Length)
2908          * ; Value length is used to indicate the length of the value to follow
2909          * Short-length = <Any octet 0-30>
2910          * Length-quote = <Octet 31>
2911          * Length = Uintvar-integer
2912          */
2913
2914         if (pFile == NULL) {
2915                 MSG_DEBUG("MmsBinaryEncodeValueLength: pFile == INVALID_HBOJ \n");
2916                 goto __CATCH;
2917         }
2918
2919         if ((gMmsEncodeMaxLen - gCurMmsEncodeBuffPos) < length) {
2920                 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
2921                                                                                         gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
2922                         MSG_DEBUG("MmsBinaryEncodeValueLength: MsgWriteDataFromEncodeBuffer fail \n");
2923                         goto __CATCH;
2924                 }
2925         }
2926
2927         if (integer < 0x1F) {
2928                 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = (UINT8)integer;
2929         } else {
2930                 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = (UINT8)LENGTH_QUOTE;
2931                 if (MmsBinaryEncodeUintvar(pFile, integer, length - 1) == false) {      // LENGTH_QUOTE
2932                         MSG_DEBUG("MmsBinaryEncodeValueLength: MmsBinaryEncodeUintvar fail\n");
2933                         goto __CATCH;
2934                 }
2935         }
2936
2937         return true;
2938
2939 __CATCH:
2940         return false;
2941 }
2942
2943
2944 static int MmsBinaryEncodeQuotedStringLen(UINT8 *pSrc)
2945 {
2946         if (pSrc == NULL) {
2947                 MSG_DEBUG("MmsBinaryEncodeQuotedStringLen: invalid file\n");
2948                 goto __CATCH;
2949         }
2950
2951         return (strlen((char*)pSrc) + 2);       // QUOTE + NULL
2952
2953 __CATCH:
2954         return -1;
2955 }
2956
2957
2958 /*
2959  * make quoted string
2960  * Quoted-string = <Octet 34> *TEXT End-of-string
2961  *
2962  * @param       source [in] original string
2963  * @param       length [in] length (in bytes) of data
2964  * @param       dest [out] buffer to store quted string
2965  * @return      changed string length
2966 */
2967 static bool MmsBinaryEncodeQuotedString(FILE *pFile, UINT8 *source, int length)
2968 {
2969         if (source == NULL || pFile == NULL) {
2970                 MSG_DEBUG("MmsBinaryEncodeQuotedString: invalid file\n");
2971                 goto __CATCH;
2972         }
2973
2974         if ((gMmsEncodeMaxLen - gCurMmsEncodeBuffPos) < length) {
2975                 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
2976                                                                                         gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
2977                         MSG_DEBUG("MmsBinaryEncodeQuotedString: MsgWriteDataFromEncodeBuffer fail \n");
2978                         goto __CATCH;
2979                 }
2980         }
2981
2982         gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = '\"';
2983         strncpy(gpMmsEncodeBuf + gCurMmsEncodeBuffPos, (char*)source, length - 2);              // except '\"' & NULL
2984         gCurMmsEncodeBuffPos += (length - 2);
2985         gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = (UINT8)NULL;
2986
2987         return true;
2988
2989 __CATCH:
2990         return false;
2991 }
2992
2993
2994
2995 static int MmsBinaryEncodeEncodedStringLen(UINT8 *source)
2996 {
2997         UINT32 charset = 0x6A;          // default = utf-8
2998         int charLeng            = 0;
2999         int textLeng            = 0;
3000         int valueLengthLen      = 0;
3001
3002
3003         MSG_DEBUG("MmsBinaryEncodeEncodedStringLen: \n");
3004
3005         /* value-length charSet text-string */
3006         /* Estimate charset value length and text string length */
3007         charLeng = MmsBinaryEncodeIntegerLen(charset);
3008         if (charLeng == -1) {
3009                 MSG_DEBUG("MmsBinaryEncodeEncodedStringLen : charLeng MmsBinaryEncodeTextStringLen fail.\n");
3010                 goto __CATCH;;
3011         }
3012
3013         textLeng = MmsBinaryEncodeTextStringLen((UINT8*)source);
3014         if (textLeng == -1)     {
3015                 MSG_DEBUG("MmsBinaryEncodeEncodedStringLen : textLeng MmsBinaryEncodeTextStringLen fail.\n");
3016                 goto __CATCH;;
3017         }
3018
3019         valueLengthLen = MmsBinaryEncodeValueLengthLen(charLeng + textLeng);
3020         if (valueLengthLen == -1) {
3021                 MSG_DEBUG("MmsBinaryEncodeEncodedStringLen : valLengthLen MmsBinaryEncodeTextStringLen fail.\n");
3022                 goto __CATCH;
3023         }
3024
3025         return (charLeng + textLeng + valueLengthLen);
3026
3027 __CATCH:
3028         return -1;
3029 }
3030
3031
3032 /*
3033  * This makes value-length by specified integer value
3034  *
3035  * @param       length [in] from MmsBinaryEncodeEncodedStringLen()
3036  * @return      encoded encoded-string
3037  */
3038 static bool MmsBinaryEncodeEncodedString(FILE *pFile, UINT8 *source, int length)
3039 {
3040         UINT32 charset = 0x6A;          // default = utf-8
3041         int charLeng = 0;
3042         int textLeng = 0;
3043         int valLengthLen = 0;
3044
3045
3046         MSG_DEBUG("MmsBinaryEncodeEncodedString: \n");
3047
3048         /* value-length charSet text-string */
3049
3050         if (pFile == NULL || source == NULL) {
3051                 MSG_DEBUG("MmsBinaryEncodeEncodedString: invalid input parameter\n");
3052                 goto __CATCH;
3053         }
3054
3055         /* Estimate charset value length and text string length */
3056         charLeng = MmsBinaryEncodeIntegerLen(charset);
3057         if (charLeng == -1) {
3058                 MSG_DEBUG("MmsBinaryEncodeEncodedString : charLeng MmsBinaryEncodeTextStringLen fail.\n");
3059                 goto __CATCH;;
3060         }
3061
3062         textLeng = MmsBinaryEncodeTextStringLen((UINT8*)source);
3063         if (textLeng == -1) {
3064                 MSG_DEBUG("MmsBinaryEncodeEncodedString : textLeng MmsBinaryEncodeTextStringLen fail.\n");
3065                 goto __CATCH;;
3066         }
3067
3068         valLengthLen = MmsBinaryEncodeValueLengthLen(charLeng + textLeng);
3069         if (valLengthLen == -1) {
3070                 MSG_DEBUG("MmsBinaryEncodeEncodedString : MmsBinaryEncodeValueLengthLen fail.\n");
3071                 goto __CATCH;
3072         }
3073
3074         if (length != (charLeng + textLeng + valLengthLen))
3075         {
3076                 MSG_DEBUG("MmsBinaryEncodeEncodedString: invalid length\n");
3077                 goto __CATCH;
3078         }
3079
3080         if ((gMmsEncodeMaxLen - gCurMmsEncodeBuffPos) < length) {
3081                 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
3082                                                                                         gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
3083                         MSG_DEBUG("MmsBinaryEncodeEncodedString: MsgWriteDataFromEncodeBuffer fail \n");
3084                         goto __CATCH;
3085                 }
3086         }
3087
3088         /* Value length of charset value and text string */
3089         if (MmsBinaryEncodeValueLength(pFile, charLeng + textLeng, valLengthLen) == false) {
3090                 MSG_DEBUG("MmsBinaryEncodeEncodedString : MmsBinaryEncodeValueLength fail.\n");
3091                 goto __CATCH;
3092         }
3093
3094         /* fixme: Write charset on buffer -> integer value not long-integer */
3095         if (MmsBinaryEncodeInteger(pFile, charset, charLeng) == false) {
3096                 MSG_DEBUG("MmsBinaryEncodeEncodedString : MmsBinaryEncodeInteger fail.\n");
3097                 goto __CATCH;
3098         }
3099
3100
3101         /* Write text string on buffer */
3102         if (MmsBinaryEncodeTextString(pFile, (UINT8*)source, textLeng) == false) {
3103                 MSG_DEBUG("MmsBinaryEncodeEncodedString : MmsBinaryEncodeTextString fail.\n");
3104                 goto __CATCH;
3105         }
3106
3107         return true;
3108
3109 __CATCH:
3110         return false;
3111 }
3112
3113
3114 static  bool __MmsBinaryEncodeFieldCodeAndValue(FILE *pFile, UINT8 fieldCode, UINT8 fieldValue)
3115 {
3116         if ((gMmsEncodeMaxLen - gCurMmsEncodeBuffPos) < 2) {
3117                 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
3118                                                                                 gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
3119                         MSG_DEBUG("__MmsBinaryEncodeFieldCodeAndValue: MsgWriteDataFromEncodeBuffer fail \n");
3120                         goto __CATCH;
3121                 }
3122         }
3123
3124         if (fieldCode == 0xff) {
3125                 MSG_DEBUG("__MmsBinaryEncodeFieldCodeAndValue: invalid fieldCode \n");
3126                 goto __CATCH;
3127         }
3128
3129         if (fieldValue == 0xff) {
3130                 MSG_DEBUG("__MmsBinaryEncodeFieldCodeAndValue: invalid fieldValue \n");
3131                 return true;
3132         }
3133
3134         gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = fieldCode;
3135         gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = fieldValue;
3136
3137         return true;
3138
3139 __CATCH:
3140         return false;
3141 }
3142
3143
3144
3145 static bool __MmsBinaryEncodeTrID(FILE *pFile, char *szTrID, int bufLen)
3146 {
3147         int     length    = 0;
3148         UINT8 fieldCode = 0xff;
3149         char szBuff[MMS_TR_ID_LEN + 1] = {0, };
3150         struct  tm      *dateTime = NULL;
3151         time_t  RawTime = 0;
3152         time_t  dateSec = 0;
3153
3154         time(&RawTime);
3155         dateTime = localtime(&RawTime);
3156         dateSec = mktime(dateTime);
3157
3158
3159         fieldCode = _MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_TRID) | 0x80;
3160         if (fieldCode == 0xff) {
3161                 MSG_DEBUG("__MmsBinaryEncodeTrID: invalid fieldCode \n");
3162                 goto __CATCH;
3163         }
3164
3165         snprintf(szBuff, MMS_TR_ID_LEN + 1, "%lu.%lu", dateSec, (unsigned long)random());
3166         MSG_DEBUG("__MmsBinaryEncodeTrID: 2. szBuff = %s\n", szBuff);
3167
3168         length = MmsBinaryEncodeTextStringLen((UINT8*)szBuff);
3169         if (length == -1) {
3170                 MSG_DEBUG("__MmsBinaryEncodeTrID: MmsBinaryEncodeTextStringLen fail \n");
3171                 goto __CATCH;
3172         }
3173
3174         if ((gMmsEncodeMaxLen - gCurMmsEncodeBuffPos) < (length + 1)) {
3175                 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
3176                                                                                         gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
3177                         MSG_DEBUG("__MmsBinaryEncodeTrID: MsgWriteDataFromEncodeBuffer fail \n");
3178                         goto __CATCH;
3179                 }
3180         }
3181
3182
3183         gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = fieldCode;
3184         if (MmsBinaryEncodeTextString(pFile, (UINT8*)szBuff, length) == false) {
3185                 MSG_DEBUG("__MmsBinaryEncodeTrID: MmsBinaryEncodeTextString fail\n");
3186                 goto __CATCH;
3187         }
3188
3189         if (szTrID) {
3190                 memset(szTrID, 0, bufLen);
3191                 strncpy(szTrID, szBuff, bufLen - 1);
3192         }
3193
3194         return true;
3195
3196 __CATCH:
3197
3198         return false;
3199 }
3200
3201 static bool __MmsBinaryEncodeMsgID(FILE *pFile, const char *szMsgID)
3202 {
3203         int     length = 0;
3204         UINT8 fieldCode = 0xff;
3205
3206         fieldCode = _MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_MSGID) | 0x80;
3207         if (fieldCode == 0xff) {
3208                 MSG_DEBUG("__MmsBinaryEncodeTrID: invalid fieldCode \n");
3209                 goto __CATCH;
3210         }
3211
3212         MSG_DEBUG("__MmsBinaryEncodeMsgID: 2. szBuff = %s\n", szMsgID);
3213
3214         length = MmsBinaryEncodeTextStringLen((UINT8*)szMsgID);
3215         if (length == -1) {
3216                 MSG_DEBUG("__MmsBinaryEncodeMsgID: MmsBinaryEncodeTextStringLen fail \n");
3217                 goto __CATCH;
3218         }
3219
3220         if ((gMmsEncodeMaxLen - gCurMmsEncodeBuffPos) < (length + 1)) {
3221                 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
3222                                                                                 gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
3223                         MSG_DEBUG("__MmsBinaryEncodeTrID: MsgWriteDataFromEncodeBuffer fail \n");
3224                         goto __CATCH;
3225                 }
3226         }
3227
3228         gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = fieldCode;
3229         if (MmsBinaryEncodeTextString(pFile, (UINT8*)szMsgID, length) == false) {
3230                 MSG_DEBUG("__MmsBinaryEncodeTrID: MmsBinaryEncodeTextString fail\n");
3231                 goto __CATCH;
3232         }
3233
3234         return true;
3235
3236 __CATCH:
3237
3238         return false;
3239 }
3240
3241
3242
3243 static bool __MmsBinaryEncodeMmsVersion(FILE *pFile)
3244 {
3245         UINT8 majorVer = MMS_MAJOR_VERSION;
3246         UINT8 minorVer = MMS_MINOR_VERSION;
3247
3248         MSG_DEBUG("__MmsBinaryEncodeMmsVersion: \n");
3249
3250         if (pFile == NULL) {
3251                 MSG_DEBUG("__MmsBinaryEncodeMmsVersion: invalid input file \n");
3252                 goto __CATCH;
3253         }
3254
3255         if ((gMmsEncodeMaxLen - gCurMmsEncodeBuffPos) < 2) {
3256                 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
3257                                                                                 gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
3258                         MSG_DEBUG("__MmsBinaryEncodeMmsVersion: MsgWriteDataFromEncodeBuffer fail \n");
3259                         goto __CATCH;
3260                 }
3261         }
3262
3263         gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = _MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_VERSION) | 0x80;
3264
3265         MSG_DEBUG("__MmsBinaryEncodeMmsVersion: major version (%d)\n", majorVer);
3266         MSG_DEBUG("__MmsBinaryEncodeMmsVersion: minor version (%d)\n", minorVer);
3267
3268         gpMmsEncodeBuf[gCurMmsEncodeBuffPos] = (majorVer << 4) | (minorVer & 0x0f) | MSB;
3269
3270         if (gpMmsEncodeBuf[gCurMmsEncodeBuffPos] < 0x80) {
3271                 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] |= 0x80;
3272         } else {
3273                 gCurMmsEncodeBuffPos++;
3274         }
3275
3276         return true;
3277
3278 __CATCH:
3279         return false;
3280 }
3281
3282
3283
3284 static bool __MmsBinaryEncodeDate(FILE *pFile)
3285 {
3286         struct  tm      *dateTime = NULL;
3287         time_t  dateSec = 0;
3288
3289         dateSec = time(NULL);
3290         dateTime = localtime(&dateSec);
3291
3292         MSG_DEBUG("%d - %d - %d, %d : %d (SYSTEM)", dateTime->tm_year + 1900, dateTime->tm_mon + 1, dateTime->tm_mday
3293                 , dateTime->tm_hour, dateTime->tm_min);
3294
3295         if (dateSec > 0) {
3296                 int     length  = 0;
3297                 length = MmsBinaryEncodeLongIntegerLen(dateSec);
3298
3299                 if ((gMmsEncodeMaxLen - gCurMmsEncodeBuffPos) < (length + 1)) { // + fieldCode
3300                         if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
3301                                                                                                 gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
3302                                 MSG_DEBUG("__MmsBinaryEncodeDate: MsgWriteDataFromEncodeBuffer fail \n");
3303                                 goto __CATCH;
3304                         }
3305                 }
3306
3307                 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = _MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_DATE) | 0x80;
3308
3309                 if (MmsBinaryEncodeLongInteger(pFile, dateSec, length) == false) {
3310                         MSG_DEBUG("__MmsBinaryEncodeDate: date MmsBinaryEncodeLongInteger error\n");
3311                         goto __CATCH;
3312                 }
3313         } else {
3314                 MSG_DEBUG("__MmsBinaryEncodeDate: date has a negative value (%d) \n", dateSec);
3315                 goto __CATCH;
3316         }
3317         return true;
3318
3319 __CATCH:
3320         return false;
3321 }
3322
3323
3324
3325 static bool __MmsBinaryEncodeFrom(FILE *pFile)
3326 {
3327         if (pFile == NULL) {
3328                 MSG_DEBUG("__MmsBinaryEncodeFrom: invalid input file \n");
3329                 goto __CATCH;
3330         }
3331
3332         if ((gMmsEncodeMaxLen - gCurMmsEncodeBuffPos) < 3) {
3333                 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
3334                                                                                         gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
3335                         MSG_DEBUG("__MmsBinaryEncodeFrom: MsgWriteDataFromEncodeBuffer fail \n");
3336                         goto __CATCH;
3337                 }
3338         }
3339
3340         gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = _MmsGetBinaryValue(MmsCodeFieldCode, MMS_CODE_FROM) | 0x80;
3341         /* length of MMS_INSERT_ADDRESS_TOKEN value */
3342         gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = 0x01;
3343         gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = _MmsGetBinaryValue(MmsCodeAddressType, MMS_INSERT_ADDRESS_TOKEN) | 0x80;
3344         return true;
3345
3346 __CATCH:
3347         return false;
3348 }
3349
3350
3351 static bool __MmsBinaryEncodeOneAddress(FILE *pFile, MmsFieldCode addrType, char *szAddrStr)
3352 {
3353         int length = 0;
3354
3355         if (pFile == NULL) {
3356                 MSG_DEBUG("__MmsBinaryEncodeOneAddress: invalid input file \n");
3357                 goto __CATCH;
3358         }
3359
3360         /* EncodedString */
3361         length = MmsBinaryEncodeEncodedStringLen((UINT8*)szAddrStr);
3362
3363         if (length == -1) {
3364                 MSG_DEBUG("__MmsBinaryEncodeOneAddress: MmsBinaryEncodeEncodedStringLen fail \n");
3365                 goto __CATCH;
3366         }
3367
3368         if ((gMmsEncodeMaxLen - gCurMmsEncodeBuffPos) < length + 1) {
3369                 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
3370                                                                                         gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
3371                         MSG_DEBUG("__MmsBinaryEncodeOneAddress: MsgWriteDataFromEncodeBuffer fail \n");
3372                         goto __CATCH;
3373                 }
3374         }
3375
3376         gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = _MmsGetBinaryValue(MmsCodeFieldCode, addrType) | 0x80;
3377
3378
3379         if (MmsBinaryEncodeEncodedString(pFile, (UINT8*)szAddrStr, length) == false) {
3380                 MSG_DEBUG("__MmsBinaryEncodeOneAddress: MmsBinaryEncodeEncodedString fail \n");
3381                 goto __CATCH;
3382         }
3383
3384         return true;
3385
3386 __CATCH:
3387         return false;
3388 }
3389
3390
3391 static bool __MmsBinaryEncodeAddress(FILE *pFile, MmsFieldCode addrType, char *szAddr)
3392 {
3393         char *pSingleAddr = NULL;
3394
3395
3396         if (pFile == NULL) {
3397                 MSG_DEBUG("__MmsBinaryEncodeAddress: invalid input file \n");
3398                 goto __CATCH;
3399         }
3400
3401         while (szAddr && szAddr[0]) {
3402                 szAddr = _MsgSkipWS3(szAddr);
3403                 if (szAddr == NULL)
3404                         break;
3405
3406                 pSingleAddr = strchr(szAddr, MSG_CH_SEMICOLON);
3407                 if (pSingleAddr) {
3408
3409                         *pSingleAddr = MSG_CH_NULL;
3410                         if (__MmsBinaryEncodeOneAddress(pFile, addrType, szAddr) == false) {
3411                                 MSG_DEBUG("__MmsBinaryEncodeAddress: __MmsBinaryEncodeAddress fail\n");
3412                                 goto __CATCH;
3413                         }
3414                         *pSingleAddr = MSG_CH_SEMICOLON;
3415
3416                         szAddr           = pSingleAddr + 1;
3417                         pSingleAddr  = NULL;
3418                 } else {
3419                         if (__MmsBinaryEncodeOneAddress(pFile, addrType, szAddr) == false) {
3420                                 MSG_DEBUG("__MmsBinaryEncodeAddress: __MmsBinaryEncodeAddress fail\n");
3421                                 goto __CATCH;
3422                         }
3423
3424                         szAddr = NULL;
3425                 }
3426         }
3427
3428         return true;
3429
3430 __CATCH:
3431         return false;
3432 }
3433
3434
3435
3436 static bool __MmsBinaryEncodeTime(FILE *pFile, MmsFieldCode fieldCode, MmsTimeStruct time)
3437 {
3438         int             timeLen  = 0;
3439         int             length   = 0;
3440
3441         if (pFile == NULL) {
3442                 MSG_DEBUG("__MmsBinaryEncodeTime: invalid input file \n");
3443                 goto __CATCH;
3444         }
3445
3446         if (time.time == 0 ||
3447                 (fieldCode != MMS_CODE_EXPIRYTIME && fieldCode != MMS_CODE_DELIVERYTIME && fieldCode != MMS_CODE_REPLYCHARGINGDEADLINE) ||
3448                 (time.type != MMS_TIMETYPE_ABSOLUTE && time.type != MMS_TIMETYPE_RELATIVE)) {
3449                 MSG_DEBUG("__MmsBinaryEncodeTime: time.type = %d \n", time.type);
3450                 return true;
3451         }
3452
3453         /*
3454          * use temporary buffer to estimate value length
3455          * and copy it to pData buffer later.
3456          */
3457
3458         if (time.type == MMS_TIMETYPE_RELATIVE) {
3459                 timeLen = MmsBinaryEncodeIntegerLen(time.time);
3460         } else {
3461                 timeLen = MmsBinaryEncodeLongIntegerLen(time.time);
3462         }
3463
3464         if (timeLen <= 0) {
3465                 MSG_DEBUG("__MmsBinaryEncodeTime: MmsBinaryEncodeLongIntegerLen fail \n");
3466                 goto __CATCH;
3467         }
3468
3469         length = MmsBinaryEncodeValueLengthLen(timeLen + 1);    //time length + time type token
3470         if (length == -1) {
3471                 MSG_DEBUG("__MmsBinaryEncodeTime: MmsBinaryEncodeValueLengthLen fail \n");
3472                 goto __CATCH;
3473         }
3474
3475         if ((gMmsEncodeMaxLen - gCurMmsEncodeBuffPos) < (length + timeLen + 2)) {       // + fieldCode + timeType
3476
3477                 if (MsgWriteDataFromEncodeBuffer(pFile, gpMmsEncodeBuf, &gCurMmsEncodeBuffPos,
3478                                                                                         gMmsEncodeMaxLen, &gMmsEncodeCurOffset) == false) {
3479                         MSG_DEBUG("_MmsBinaryEncodeSendReqHdr: MsgWriteDataFromEncodeBuffer fail \n");
3480                         goto __CATCH;
3481                 }
3482         }
3483
3484         /* fieldCode */
3485         gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = _MmsGetBinaryValue(MmsCodeFieldCode, fieldCode) | 0x80;
3486
3487         /* value length */
3488         if (MmsBinaryEncodeValueLength(pFile, timeLen + 1, length) == false) {
3489                 MSG_DEBUG("__MmsBinaryEncodeTime: MmsBinaryEncodeValueLength fail \n");
3490                 goto __CATCH;
3491         }
3492
3493         /* time type & value */
3494         if (time.type == MMS_TIMETYPE_RELATIVE) {
3495                 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = _MmsGetBinaryValue(MmsCodeTimeType, MMS_TIMETYPE_RELATIVE) | 0x80;
3496                 if (MmsBinaryEncodeInteger(pFile, time.time, timeLen) == false) {
3497                         MSG_DEBUG("__MmsBinaryEncodeTime: MmsBinaryEncodeLongInteger fail \n");
3498                         goto __CATCH;
3499                 }
3500         } else {
3501                 gpMmsEncodeBuf[gCurMmsEncodeBuffPos++] = _MmsGetBinaryValue(MmsCodeTimeType, MMS_TIMETYPE_ABSOLUTE) | 0x80;
3502                 if (MmsBinaryEncodeLongInteger(pFile, time.time, timeLen) == false) {
3503                         MSG_DEBUG("__MmsBinaryEncodeTime: MmsBinaryEncodeLongInteger fail \n");
3504                         goto __CATCH;
3505                 }
3506         }
3507
3508         return true;
3509
3510 __CATCH:
3511         return false;
3512 }
3513