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