Coverity issue resolved resource_leak
[platform/core/messaging/msg-service.git] / plugin / sms_plugin / 3gpp / Sms3gppWapPushHandler.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved
3  *
4  * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
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 <glib.h>
18 #include <gio/gio.h>
19 #if MSG_DRM_SUPPORT
20 #include <drm_client.h>
21 #endif
22
23 #include "MsgDebug.h"
24 #include "MsgCppTypes.h"
25 #include "MsgGconfWrapper.h"
26 #include "MsgException.h"
27 #include "MsgUtilFile.h"
28 #include "Sms3gppTransport.h"
29 #include "Sms3gppEventHandler.h"
30 #include "Sms3gppWapPushHandler.h"
31 #include "SmsPluginStorage.h"
32
33
34 static unsigned short wapPushPortList[] = { 0x0b84, 0x0b85, 0x23F0, 0x23F1, 0x23F2, 0x23F3, 0xC34F };
35
36 static char gWapCodeBufferLeft[WSP_CODE_BUFFER_LEFT_LEN_MAX];
37 static char gWapCodeBufferRight[WSP_CODE_BUFFER_RIGHT_LEN_MAX];
38
39 static const unsigned long wspUintvarDecodeTable[] = { 0x00000001, 0x00000080, 0x00004000, 0x00200000, 0x10000000};
40
41 static const unsigned char wspHeaderFieldCount = 0x43;
42 static const unsigned char wspContentsTypeCount = 0x34;
43 static const unsigned long wspLanguageCount = 0x11a;
44 static const unsigned char wspSecurityTypeCount = 0x04;
45
46
47 static const SMS_3GPP_WSP_CONTENTS_TYPE_S wspExtendedContentsType[] = {
48         { (char*)"text/vnd/wap/connectivity-xml", 0x35 },
49         { (char*)"application/vnd.wap.connectivity-wbxml", 0x36 },
50         { (char*)"application/pkcs7-mime",  0x37 },
51         { (char*)"application/vnd.wap.hashed-certificate", 0x38 },
52         { (char*)"application/vnd.wap.signed-certificate", 0x39  },
53         { (char*)"application/vnd.wap.cert-response", 0x3A },
54         { (char*)"application/xhtml+xml", 0x3B },
55         { (char*)"application/wml+xml", 0x3C  },
56         { (char*)"text/css", 0x3D },
57
58         { (char*)"application/vnd.wap.mms-message", 0x3E },
59
60         { (char*)"application/vnd.wap.rollover-certificate", 0x3F  },
61         { (char*)"application/vnd.wap.locc+wbxml", 0x40 },
62         { (char*)"application/vnd.wap.loc+xml", 0x41 },
63         { (char*)"application/vnd.syncml.dm+wbxml", 0x42 },
64         { (char*)"application/vnd.syncml.dm+xml", 0x43 },
65         { (char*)"application/vnd.syncml.notification", 0x44 },
66         { (char*)"application/vnd.wap.xhtml+xml", 0x45 },
67         { (char*)"application/vnd.wv.csp.cir", 0x46 },
68
69         { (char*)"application/vnd.oma.dd+xml", 0x47},
70         { (char*)"application/vnd.oma.drm.message", 0x48 },
71         { (char*)"application/vnd.oma.drm.content", 0x49 },
72         { (char*)"application/vnd.oma.drm.rights+xml", 0x4A },
73         { (char*)"application/vnd.oma.drm.rights+wbxml", 0x4B },
74         { (char*)"application/vnd.syncml.ds.notification", 0x4E},
75         { (char*)"application/mikey", 0x52},
76         { (char*)"", 0xff }
77 };
78
79
80 static const char* wspHeaderFieldName[] = {
81                 (char*)"Accept", /* 0x00 */
82                 (char*)"Accept-Charset",
83                 (char*)"Accept-Encoding",
84                 (char*)"Accept-Language",
85                 (char*)"Accept-Ranges",
86                 (char*)"Age", /* 0x05 */
87                 (char*)"Allow",
88                 (char*)"Authorization",
89                 (char*)"Cache-Control",
90                 (char*)"Connection",
91                 (char*)"Content-Base", /* 0x0a */
92                 (char*)"Content-Encoding",
93                 (char*)"Content-Language",
94                 (char*)"Content-Length",
95                 (char*)"Content-Location",
96                 (char*)"Content-MD5",
97                 (char*)"Content-Range", /* 0x10 */
98                 (char*)"Content-Type",
99                 (char*)"Date",
100                 (char*)"ETag",
101                 (char*)"Expires",
102                 (char*)"From", /* 0x15 */
103                 (char*)"Host",
104                 (char*)"If-Modified-Since",
105                 (char*)"If-Match",
106                 (char*)"If-None-Match",
107                 (char*)"If-Range", /* 0x1a */
108                 (char*)"If-Unmodified-Since",
109                 (char*)"Location",
110                 (char*)"Last-Modified",
111                 (char*)"Max-Forwards",
112                 (char*)"Pragma",
113                 (char*)"Proxy-Authenticate", /* 0x20 */
114                 (char*)"Proxy-Authorization",
115                 (char*)"Public",
116                 (char*)"Range",
117                 (char*)"Referer",
118                 (char*)"Retry-After", /* 0x25 */
119                 (char*)"Server",
120                 (char*)"Transfer-Encodig",
121                 (char*)"Upgrade",
122                 (char*)"User-Agent",
123                 (char*)"Vary", /* 0x2a */
124                 (char*)"Via",
125                 (char*)"Warning",
126                 (char*)"Www-Authenticate",
127                 (char*)"Content-Disposition",
128                 (char*)"X-Wap-Application-Id",
129                 (char*)"X-Wap-Content-URI", /* 0x30 */
130                 (char*)"X-Wap-Iinitiator-URI",
131                 (char*)"Accept-Application", /* Back */
132                 (char*)"Bearer-Indication",
133                 (char*)"Push-Flag",
134                 (char*)"Profile", /* 0x35 */
135                 (char*)"Profile-Diff",
136                 (char*)"Profile-Warning", /* end of WAP 1.2 */
137                 (char*)"Expect",
138                 (char*)"Te",
139                 (char*)"Trailer", /* 0x3a */
140                 (char*)"Accept-Charset", /* Back */
141                 (char*)"Accept-Encoding", /* Back */
142                 (char*)"Cache-Control", /* back */
143                 (char*)"Content-Range",
144                 (char*)"X-Wap-Tod",
145                 (char*)"Content-ID", /* 0x40 */
146                 (char*)"Set-Cookie",
147                 (char*)"Cookie",
148                 (char*)"Encoding-Version"
149 };
150
151
152 static const SMS_3GPP_WSP_CHARSET_S wspCharset[] = {
153                 { (char*)"big5", 0x07ea },
154                 { (char*)"iso-10646-ucs-2", 0x03e8 },
155                 { (char*)"iso-8859-1", 0x04 },
156                 { (char*)"iso-8859-2", 0x05 },
157                 { (char*)"iso-8859-3", 0x06 },
158                 { (char*)"iso-8859-4", 0x07 },
159                 { (char*)"iso-8859-5", 0x08 },
160                 { (char*)"iso-8859-6", 0x09 },
161                 { (char*)"iso-8859-7", 0x0a },
162                 { (char*)"iso-8859-8", 0x0b },
163                 { (char*)"iso-8859-9", 0x0c },
164                 { (char*)"shift-JIS", 0x11 },
165                 { (char*)"us-ascii", 0x03 },
166                 { (char*)"utf-8", 0x6a },
167                 { (char*)"none", 0x26 },
168                 { (char*)"", 0xffff }
169 };
170
171
172 static const char* wspEncodeMethod[] = {
173                 (char*)"Gzip",
174                 (char*)"Compress",
175                 (char*)"Deflate"
176 };
177
178
179 static const SMS_3GPP_WSP_CONTENTS_TYPE_S wspContentsType[] = {
180                 { (char*)"*/*", 0x00 },
181                 { (char*)"text/*", 0x01 },
182                 { (char*)"text/html", 0x02 },
183                 { (char*)"text/plain", 0x03 },
184                 { (char*)"text/x-hdml", 0x04 },
185                 { (char*)"text/x-ttml", 0x05 },
186                 { (char*)"text/x-vCalendar", 0x06 },
187                 { (char*)"text/x-vCard", 0x07 },
188                 { (char*)"text/vnd.wap.wml", 0x08 },
189                 { (char*)"text/vnd.wap.wmlscript", 0x09 },
190                 { (char*)"text/vnd.wap.wta-event", 0x0a },
191                 { (char*)"multipart/*", 0x0b },
192                 { (char*)"multipart/mixed", 0x0c },
193                 { (char*)"multipart/form-data", 0x0d },
194                 { (char*)"multipart/byteranges", 0x0e },
195                 { (char*)"multipart/alternative", 0x0f },
196                 { (char*)"application/*", 0x10 },
197                 { (char*)"application/java-vm", 0x11 },
198                 { (char*)"application/x-www-form-urlencoded", 0x12 },
199                 { (char*)"application/x-hdmlc", 0x13 },
200                 { (char*)"application/vnd.wap.wmlc", 0x14 },
201                 { (char*)"application/vnd.wap.wmlscriptc", 0x15 },
202                 { (char*)"application/vnd.wap.wta-eventc", 0x16 },
203                 { (char*)"application/vnd.wap.uaprof", 0x17 },
204                 { (char*)"application/vnd.wap.wtls-ca-certificate", 0x18 },
205                 { (char*)"application/vnd.wap.wtls-user-certificate", 0x19 },
206                 { (char*)"application/x-x509-ca-cert", 0x1a },
207                 { (char*)"application/x-x509-user-cert", 0x1b },
208                 { (char*)"image/*", 0x1c },
209                 { (char*)"image/gif", 0x1d },
210                 { (char*)"image/jpeg", 0x1e },
211                 { (char*)"image/tiff", 0x1f },
212                 { (char*)"image/png", 0x20 },
213                 { (char*)"image/vnd.wap.wbmp", 0x21 },
214                 { (char*)"application/vnd.wap.multipart.*", 0x22 },
215                 { (char*)"application/vnd.wap.multipart.mixed", 0x23 },
216                 { (char*)"application/vnd.wap.multipart.form-data", 0x24 },
217                 { (char*)"application/vnd.wap.multipart.byteranges", 0x25 },
218                 { (char*)"application/vnd.wap.multipart.alternative", 0x26 },
219                 { (char*)"application/xml", 0x27 },
220                 { (char*)"text/xml", 0x28 },
221                 { (char*)"application/vnd.wap.wbxml", 0x29 },
222                 { (char*)"application/x-x968-cross-cert", 0x2a },
223                 { (char*)"application/x-x968-ca-cert", 0x2b },
224                 { (char*)"application/x-x968-user-cert", 0x2c },
225                 { (char*)"text/vnd.wap.si", 0x2d },
226                 { (char*)"application/vnd.wap.sic", 0x2e },
227                 { (char*)"text/vnd.wap.sl", 0x2f },
228                 { (char*)"application/vnd.wap.slc", 0x30 },
229                 { (char*)"text/vnd.wap.co", 0x31 },
230                 { (char*)"application/vnd.wap.coc", 0x32 },
231                 { (char*)"application/vnd.wap.multipart.related", 0x33 },
232                 { (char*)"application/vnd.wap.sia", 0x34 },
233
234                 { (char*)"text/vnd/wap/connectivity-xml", 0x35 },
235                 { (char*)"application/vnd.connectivity-wbxml", 0x36 },
236                 { (char*)"application/pkcs7-mime",  0x37 },
237                 { (char*)"application/vnd.wap.hashed-certificate", 0x38 },
238                 { (char*)"application/vnd.wap.signed-certificate", 0x39  },
239                 { (char*)"application/vnd.wap.cert-response", 0x3A },
240                 { (char*)"application/xhtml+xml", 0x3B },
241                 { (char*)"application/wml+xml", 0x3C  },
242                 { (char*)"text/css", 0x3D },
243
244                 { (char*)"application/vnd.wap.mms-message", 0x3E },
245
246                 { (char*)"application/vnd.wap.rollover-certificate", 0x3F  },
247                 { (char*)"application/vnd.wap.locc+wbxml", 0x40 },
248                 { (char*)"application/vnd.wap.loc+xml", 0x41 },
249                 { (char*)"application/vnd.syncml.dm+wbxml", 0x42 },
250                 { (char*)"application/vnd.syncml.dm+xml", 0x43 },
251                 { (char*)"application/vnd.syncml.notification", 0x44 },
252                 { (char*)"application/vnd.wap.xhtml+xml", 0x45 },
253                 { (char*)"application/vnd.wv.csp.cir", 0x46 }
254 };
255
256
257 static const SMS_3GPP_WAP_UNREGISTER_CONTENTS_TYPE_S wspUnregisterContentsType[]= {
258                 { (char*)"application/vnd.wap.emn+wbxml", 0x30A},
259                 { (char*)"application/vnd.omaloc-supl-init", 0x312},
260                 { (char*)"application/vnd.oma.drm.roap-trigger+wbxml", 0x316}
261 };
262
263
264 static const unsigned long wspUnregisteredContentsTypeCount = sizeof(wspUnregisterContentsType)/sizeof(SMS_3GPP_WAP_UNREGISTER_CONTENTS_TYPE_S);
265
266
267 static const SMS_3GPP_WSP_LANGUAGE_S wspLanguage[] = {
268                 { (char*)"English", 0x19 },
269                 { (char*)"en", 0x19 },
270                 { (char*)"Korean", 0x3d },
271                 { (char*)"*", 0x00 },
272                 { (char*)"Afar", 0x01 },
273                 { (char*)"aa", 0x01 },
274                 { (char*)"Abkhazian", 0x02 },
275                 { (char*)"ab", 0x02 },
276                 { (char*)"Afrikaans", 0x03 },
277                 { (char*)"af", 0x03 },
278                 { (char*)"Amharic", 0x04 },
279                 { (char*)"am", 0x04 },
280                 { (char*)"Arabic", 0x05 },
281                 { (char*)"ar", 0x05 },
282                 { (char*)"Assamese", 0x06 },
283                 { (char*)"as", 0x06 },
284                 { (char*)"Aymara", 0x07 },
285                 { (char*)"ay", 0x07 },
286                 { (char*)"Azerbaijani", 0x08 },
287                 { (char*)"az", 0x08 },
288                 { (char*)"Bashkir", 0x09 },
289                 { (char*)"ba", 0x09 },
290                 { (char*)"Byelorussian", 0x0a },
291                 { (char*)"be", 0x0a },
292                 { (char*)"Bulgarian", 0x0b },
293                 { (char*)"bg", 0x0b },
294                 { (char*)"Bihari", 0x0c },
295                 { (char*)"bh", 0x0c },
296                 { (char*)"Bislama", 0x0d },
297                 { (char*)"bi", 0x0f },
298                 { (char*)"Bengali", 0x0e },
299                 { (char*)"Bangla", 0x0e },
300                 { (char*)"bn", 0x0e },
301                 { (char*)"Tibetan", 0x0f },
302                 { (char*)"bo", 0x0f },
303                 { (char*)"Breton", 0x10 },
304                 { (char*)"br", 0x10 },
305                 { (char*)"Catalan", 0x11 },
306                 { (char*)"ca", 0x11 },
307                 { (char*)"Corsican", 0x12 },
308                 { (char*)"co", 0x12 },
309                 { (char*)"Czech", 0x13 },
310                 { (char*)"cs", 0x13 },
311                 { (char*)"Welsh", 0x14 },
312                 { (char*)"cy", 0x14 },
313                 { (char*)"Denish", 0x15 },
314                 { (char*)"da", 0x15 },
315                 { (char*)"German", 0x16 },
316                 { (char*)"de", 0x16 },
317                 { (char*)"Bhutani", 0x17 },
318                 { (char*)"dz", 0x17 },
319                 { (char*)"Greek", 0x18 },
320                 { (char*)"el", 0x18 },
321                 { (char*)"Esperanto", 0x81 },
322                 { (char*)"eo", 0x1a },
323                 { (char*)"Spanish", 0x1b },
324                 { (char*)"es", 0x1b },
325                 { (char*)"Estonian", 0x1c },
326                 { (char*)"et", 0x1c },
327                 { (char*)"Basque", 0x1d },
328                 { (char*)"eu", 0x1d },
329                 { (char*)"Persian", 0x1e },
330                 { (char*)"fa", 0x1e },
331                 { (char*)"Finnish", 0x1f },
332                 { (char*)"fi", 0x1f },
333                 { (char*)"Fiji", 0x20 },
334                 { (char*)"fj", 0x20 },
335                 { (char*)"Faeroese", 0x82 },
336                 { (char*)"fo", 0x82 },
337                 { (char*)"French", 0x22 },
338                 { (char*)"fr", 0x22 },
339                 { (char*)"Frisian", 0x83 },
340                 { (char*)"fy", 0x83 },
341                 { (char*)"Irish", 0x24 },
342                 { (char*)"ga", 0x24 },
343                 { (char*)"Scots Gaelic", 0x25 },
344                 { (char*)"gd", 0x25 },
345                 { (char*)"Galician", 0x26 },
346                 { (char*)"gl", 0x26 },
347                 { (char*)"Guarani", 0x27 },
348                 { (char*)"gn", 0x27 },
349                 { (char*)"Gujarati", 0x28 },
350                 { (char*)"gu", 0x28 },
351                 { (char*)"Hausa", 0x29 },
352                 { (char*)"ha", 0x29 },
353                 { (char*)"Hebrew", 0x2a },
354                 { (char*)"he", 0x2a },
355                 { (char*)"Hindi", 0x2b },
356                 { (char*)"hi", 0x2b },
357                 { (char*)"Croatian", 0x2c },
358                 { (char*)"hr", 0x2c },
359                 { (char*)"Hungarian", 0x2d },
360                 { (char*)"hu", 0x2d },
361                 { (char*)"Armenian", 0x2e },
362                 { (char*)"hy", 0x2e },
363                 { (char*)"Interlingua", 0x84 },
364                 { (char*)"ia", 0x84 },
365                 { (char*)"Indonesian", 0x30 },
366                 { (char*)"id", 0x30 },
367                 { (char*)"Interlingue", 0x86 },
368                 { (char*)"ie", 0x86 },
369                 { (char*)"Maori", 0x47 },
370                 { (char*)"mi", 0x47 },
371                 { (char*)"Macedonian", 0x48 },
372                 { (char*)"mk", 0x48 },
373                 { (char*)"Malayalam", 0x49 },
374                 { (char*)"ml", 0x49 },
375                 { (char*)"Mongolian", 0x4a },
376                 { (char*)"mn", 0x4a },
377                 { (char*)"Moldavian", 0x4b },
378                 { (char*)"mo", 0x4d },
379                 { (char*)"Marathi", 0x4c },
380                 { (char*)"mr", 0x4c },
381                 { (char*)"Malay", 0x4d },
382                 { (char*)"ms", 0x4d },
383                 { (char*)"Maltese", 0x4e },
384                 { (char*)"mt", 0x4e },
385                 { (char*)"Burmese", 0x4f },
386                 { (char*)"my", 0x4f },
387                 { (char*)"Nauru", 0x50 },
388                 { (char*)"na", 0x50 },
389                 { (char*)"Nepali", 0x51 },
390                 { (char*)"ne", 0x51 },
391                 { (char*)"Dutch", 0x52 },
392                 { (char*)"nl", 0x52 },
393                 { (char*)"Norwegian", 0x53 },
394                 { (char*)"no", 0x53 },
395                 { (char*)"Occitan", 0x54 },
396                 { (char*)"oc", 0x54 },
397                 { (char*)"(Afan) Oromo", 0x55 },
398                 { (char*)"(Afan)Oromo", 0x55 },
399                 { (char*)"om", 0x55 },
400                 { (char*)"Oriya", 0x56 },
401                 { (char*)"or", 0x56 },
402                 { (char*)"Punjabi", 0x57 },
403                 { (char*)"pa", 0x57 },
404                 { (char*)"Polish", 0x58 },
405                 { (char*)"po", 0x58 },
406                 { (char*)"Pashto", 0x59 },
407                 { (char*)"Pushto", 0x59 },
408                 { (char*)"ps", 0x59 },
409                 { (char*)"Portugurse", 0x5a },
410                 { (char*)"pt", 0x5a },
411                 { (char*)"Quechua", 0x5b },
412                 { (char*)"qu", 0x5b },
413                 { (char*)"Rhaeto-Romance", 0x8c },
414                 { (char*)"rm", 0x8c },
415                 { (char*)"Kirundi", 0x5d },
416                 { (char*)"rn", 0x5d },
417                 { (char*)"Romanian", 0x5e },
418                 { (char*)"ro", 0x5e },
419                 { (char*)"Russian", 0x5f },
420                 { (char*)"ru", 0x5f },
421                 { (char*)"Kinyarwanda", 0x60 },
422                 { (char*)"rw", 0x60 },
423                 { (char*)"Sanskrit", 0x61 },
424                 { (char*)"sa", 0x61 },
425                 { (char*)"Sindhi", 0x62 },
426                 { (char*)"sd", 0x62 },
427                 { (char*)"Sangho", 0x63 },
428                 { (char*)"sg", 0x63 },
429                 { (char*)"Serbo-Croatian", 0x64 },
430                 { (char*)"sh", 0x64 },
431                 { (char*)"Sinhalese", 0x65 },
432                 { (char*)"si", 0x65 },
433                 { (char*)"Slovak", 0x66 },
434                 { (char*)"sk", 0x66 },
435                 { (char*)"Slovenian", 0x67 },
436                 { (char*)"sl", 0x67 },
437                 { (char*)"Samoan", 0x68 },
438                 { (char*)"sm", 0x68 },
439                 { (char*)"Shona", 0x69 },
440                 { (char*)"sn", 0x69 },
441                 { (char*)"Somali", 0x6a },
442                 { (char*)"so", 0x6a },
443                 { (char*)"Albanian", 0x6b },
444                 { (char*)"sq", 0x6b },
445                 { (char*)"Serbian", 0x6c },
446                 { (char*)"sr", 0x6c },
447                 { (char*)"Siswati", 0x6d },
448                 { (char*)"ss", 0x6d },
449                 { (char*)"Sesotho", 0x6e },
450                 { (char*)"st", 0x6e },
451                 { (char*)"Sundanese", 0x6f },
452                 { (char*)"su", 0x6f },
453                 { (char*)"Swedish", 0x70 },
454                 { (char*)"sv", 0x70 },
455                 { (char*)"Swahili", 0x71 },
456                 { (char*)"sw", 0x71 },
457                 { (char*)"Tamil", 0x72 },
458                 { (char*)"ta", 0x72 },
459                 { (char*)"Telugu", 0x73 },
460                 { (char*)"te", 0x73 },
461                 { (char*)"Tajik", 0x74 },
462                 { (char*)"tg", 0x74 },
463                 { (char*)"Thai", 0x75 },
464                 { (char*)"th", 0x75 },
465                 { (char*)"Tigrinya", 0x76 },
466                 { (char*)"ti", 0x76 },
467                 { (char*)"Turkmen", 0x77 },
468                 { (char*)"tk", 0x77 },
469                 { (char*)"Inupiak", 0x87 },
470                 { (char*)"ik", 0x87 },
471                 { (char*)"Icelandic", 0x33 },
472                 { (char*)"is", 0x33 },
473                 { (char*)"Italian", 0x34 },
474                 { (char*)"it", 0x34 },
475                 { (char*)"Inuktitut", 0x89 },
476                 { (char*)"iu", 0x89 },
477                 { (char*)"Japanese", 0x36 },
478                 { (char*)"ja", 0x36 },
479                 { (char*)"Javanese", 0x37 },
480                 { (char*)"jw", 0x37 },
481                 { (char*)"Georgian", 0x38 },
482                 { (char*)"ka", 0x38 },
483                 { (char*)"Kazakh", 0x39 },
484                 { (char*)"kk", 0x39 },
485                 { (char*)"Gerrnlandic", 0x8a },
486                 { (char*)"kl", 0x8a },
487                 { (char*)"Cambodian", 0x3b },
488                 { (char*)"km", 0x3b },
489                 { (char*)"Kannada", 0x3c },
490                 { (char*)"kn", 0x3c },
491                 { (char*)"Kashmiri", 0x3e },
492                 { (char*)"ks", 0x3e },
493                 { (char*)"Kurdish", 0x3f },
494                 { (char*)"ku", 0x3f },
495                 { (char*)"Kirghiz", 0x40 },
496                 { (char*)"ky", 0x40 },
497                 { (char*)"Latin", 0x8b },
498                 { (char*)"la", 0x8b },
499                 { (char*)"Lingala", 0x42 },
500                 { (char*)"ln", 0x42 },
501                 { (char*)"Laothian", 0x43 },
502                 { (char*)"lo", 0x43 },
503                 { (char*)"Lithuanian", 0x44 },
504                 { (char*)"lt", 0x44 },
505                 { (char*)"Lavian", 0x45 },
506                 { (char*)"Lettish", 0x45 },
507                 { (char*)"lv", 0x45 },
508                 { (char*)"Malagasy", 0x46 },
509                 { (char*)"mg", 0x46 },
510                 { (char*)"Tagalog", 0x78 },
511                 { (char*)"tl", 0x78 },
512                 { (char*)"Setswana", 0x79 },
513                 { (char*)"tn", 0x79 },
514                 { (char*)"Tonga", 0x7a },
515                 { (char*)"to", 0x7a },
516                 { (char*)"Turkish", 0x7b },
517                 { (char*)"tr", 0x7b },
518                 { (char*)"Tsonga", 0x7c },
519                 { (char*)"ts", 0x7c },
520                 { (char*)"Tatar", 0x7d },
521                 { (char*)"tt", 0x7d },
522                 { (char*)"Twi", 0x7e },
523                 { (char*)"tw", 0x7e },
524                 { (char*)"Uighur", 0x7f },
525                 { (char*)"ug", 0x7f },
526                 { (char*)"Ukrainian", 0x1a },
527                 { (char*)"uk", 0x1a },
528                 { (char*)"Urdu", 0x21 },
529                 { (char*)"ur", 0x21 },
530                 { (char*)"Uzbek", 0x23 },
531                 { (char*)"uz", 0x23 },
532                 { (char*)"Vietnamese", 0x2f },
533                 { (char*)"vi", 0x2f },
534                 { (char*)"Volapuk", 0x85 },
535                 { (char*)"vo", 0x85 },
536                 { (char*)"Wolof", 0x31 },
537                 { (char*)"wo" , 0x31 },
538                 { (char*)"Xhosa", 0x32 },
539                 { (char*)"xh", 0x32 },
540                 { (char*)"Yiddish", 0x88 },
541                 { (char*)"yi", 0x88 },
542                 { (char*)"Yoruba", 0x35 },
543                 { (char*)"yo", 0x35 },
544                 { (char*)"Zhuang", 0x3a },
545                 { (char*)"z", 0x3a },
546                 { (char*)"Chinese", 0x41 },
547                 { (char*)"ch", 0x41 },
548                 { (char*)"Zulu", 0x5c },
549                 { (char*)"zu", 0x5c },
550                 { (char*)"Ko", 0x3d }
551 };
552
553
554 static const SMS_3GPP_WSP_HEADER_PARAMETER_S wspHeaderApplId[] = {
555         { (char*)"x-wap-application:*",  0x00 },
556         { (char*)"x-wap-application:push.sia",  0x01},
557         { (char*)"x-wap-application:wml.ua",  0x02 },
558         { (char*)"x-wap-application:wta.ua", 0x03 },
559         { (char*)"x-wap-application:mms.ua", 0x04 },
560         { (char*)"x-wap-application:push.syncml", 0x05 },
561         { (char*)"x-wap-application:loc.ua",  0x06 },
562         { (char*)"x-wap-application:syncml.dm", 0x07 },
563         { (char*)"x-wap-application:drm.ua", 0x08 },
564         { (char*)"x-wap-application:emn.ua", 0x09 },
565         { (char*)"x-oma-application:ulp.ua ", 0x10 },
566         { (char*)"x-oma-docomo:open.ctl", 0x9055 },
567         { (char*)"x-oma-docomo:xmd.mail.ua", 0x905C },
568         { (char*)"x-oma-docomo:xmd.storage.ua", 0x905F },
569         { (char*)"x-oma-docomo:xmd.lcsapp.ua", 0x9060 },
570         { (char*)"x-oma-docomo:xmd.info.ua", 0x9061 },
571         { (char*)"x-oma-docomo:xmd.agent.ua", 0x9062 },
572         { (char*)"x-oma-docomo:xmd.sab.ua", 0x9063 },
573         { (char*)"x-oma-docomo:xmd.am.ua", 0x9064 },
574         { (char*)"x-oma-docomo:xmd.emdm.ua", 0x906B },
575         { (char*)"x-oma-docomo:xmd.lac.ua", 0x906C },
576         { (char*)"x-oma-docomo:xmd.osv.ua", 0x906D },
577         { (char*)"x-oma-docomo:xmd.dcs.ua", 0x906E },
578         { (char*)"x-oma-docomo:xmd.wipe.ua", 0x906F },
579         { (char*)"x-oma-docomo:xmd.vdapp.ua ", 0x9070 },
580 };
581
582
583 static const char* wspCacheControl[] = {
584                 (char*)"No-cache",
585                 (char*)"No-store",
586                 (char*)"Max-age",
587                 (char*)"Max-stale",
588                 (char*)"Min-fresh",
589                 (char*)"Only-if-cached",
590                 (char*)"Public",
591                 (char*)"Private",
592                 (char*)"No-transform",
593                 (char*)"Must-revalidate",
594                 (char*)"Proxy-revalidate"
595 };
596
597
598 static const SMS_3GPP_WSP_METHOD_TYPE_S wspMethodType[] = {
599                 { (char*)"GET", 0x40 },
600                 { (char*)"OPTIONS", 0x41 },
601                 { (char*)"HEAD", 0x42 },
602                 { (char*)"DELETE", 0x43 },
603                 { (char*)"TRACE", 0x44 },
604                 { (char*)"POST", 0x60 },
605                 { (char*)"PUT", 0x61 }
606 };
607
608
609 static const SMS_3GPP_WSP_SECURITY_TYPE_S wspSecurityType[] = {
610                 {(char*)"NETWPIN", 0x00},
611                 {(char*)"USERPIN", 0x01},
612                 {(char*)"USERNETWPIN", 0x02},
613                 {(char*)"USERPINMAC", 0x03}
614 };
615
616
617 static const char* wspWeek[] = {
618         (char*)"Sun",
619         (char*)"Mon",
620         (char*)"Tue",
621         (char*)"Wed",
622         (char*)"Thu",
623         (char*)"Fri",
624         (char*)"Sat"
625 };
626
627
628 #ifdef MSG_FW_FOR_DEBUG
629 static const char* wspWeekDay[] = {
630         (char*)"Sunday",
631         (char*)"Monday",
632         (char*)"Tuesday",
633         (char*)"Wednesday",
634         (char*)"Thursday",
635         (char*)"Friday",
636         (char*)"Saturday"
637 };
638 #endif
639
640
641 static const char* wspMonth[] = {
642                 (char*)"Jan",
643                 (char*)"Feb",
644                 (char*)"Mar",
645                 (char*)"Apr",
646                 (char*)"May",
647                 (char*)"Jun",
648                 (char*)"Jul",
649                 (char*)"Aug",
650                 (char*)"Sep",
651                 (char*)"Oct",
652                 (char*)"Nov",
653                 (char*)"Dec"
654 };
655
656 /*==================================================================================================
657                                      IMPLEMENTATION OF Sms3gppWapPushHandler - Member Functions
658 ==================================================================================================*/
659 Sms3gppWapPushHandler* Sms3gppWapPushHandler::pInstance = NULL;
660
661
662 Sms3gppWapPushHandler::Sms3gppWapPushHandler()
663 {
664         memset(&tmpAddress, 0x00, sizeof(tmpAddress));
665         memset(&tmpTimeStamp, 0x00, sizeof(tmpTimeStamp));
666 }
667
668
669 Sms3gppWapPushHandler::~Sms3gppWapPushHandler()
670 {
671 }
672
673
674 Sms3gppWapPushHandler* Sms3gppWapPushHandler::instance()
675 {
676         if (!pInstance)
677                 pInstance = new Sms3gppWapPushHandler();
678
679         return pInstance;
680 }
681
682
683 bool Sms3gppWapPushHandler::IsWapPushMsg(SMS_3GPP_USERDATA_S *pUserData)
684 {
685         MSG_BEGIN();
686
687         if (pUserData == NULL) {
688                 MSG_DEBUG("pUserData is NULL");
689                 return false;
690         }
691
692         int PortCount = sizeof(wapPushPortList) / sizeof(wapPushPortList[0]);
693
694         MSG_DEBUG("Port Count [%d]", PortCount);
695
696         for (int i = 0; i < pUserData->headerCnt; i++) {
697                 if (pUserData->header[i].udhType == SMS_UDH_APP_PORT_8BIT) {
698                         /** Have to check something??? */
699                 } else if (pUserData->header[i].udhType == SMS_UDH_APP_PORT_16BIT) {
700                         for (int j = 0; j < PortCount; j++) {
701                                 if (pUserData->header[i].udh.appPort16bit.destPort == wapPushPortList[j]) {
702                                         MSG_DEBUG("Wap Push Msg : [%04x]", wapPushPortList[j]);
703                                         return true;
704                                 }
705                         }
706                 }
707         }
708
709         MSG_END();
710
711         return false;
712 }
713
714 #if 0
715 SMS_WAP_APP_CODE_T Sms3gppWapPushHandler::getAppCode(const char *pPushHeader)
716 {
717         int appCount = sizeof(pushDefaultApplication)/sizeof(pushDefaultApplication[0]) - 1;
718
719         unsigned char *header = NULL;
720         SMS_WAP_APP_CODE_T      appCode = SMS_WAP_APPLICATION_DEFAULT;
721
722         MSG_DEBUG("Application Count [%d]", appCount);
723
724         for (int i = 0; i < appCount; i++) {
725                 header = (unsigned char*)strcasestr(pPushHeader, pushDefaultApplication[i].pContentType);
726
727                 if (header != NULL) {
728                         appCode = pushDefaultApplication[i].appCode;
729                         break;
730                 }
731         }
732
733         if (appCode == SMS_WAP_APPLICATION_DEFAULT) {
734                 MSG_DEBUG("Application Type is not supported");
735         } else {
736                 MSG_DEBUG("Find Application [%d]", appCode);
737         }
738
739         return appCode;
740 }
741 #endif
742
743
744 void Sms3gppWapPushHandler::copyDeliverData(SMS_3GPP_DELIVER_S *pDeliver)
745 {
746         tmpAddress.ton = pDeliver->originAddress.ton;
747         tmpAddress.npi = pDeliver->originAddress.npi;
748
749         strncpy(tmpAddress.address, pDeliver->originAddress.address, MAX_ADDRESS_LEN);
750
751         MSG_SEC_DEBUG("Address [%s]", tmpAddress.address);
752
753         tmpTimeStamp.format = pDeliver->timeStamp.format;
754
755         if (tmpTimeStamp.format == SMS_3GPP_TIME_ABSOLUTE) {
756                 tmpTimeStamp.time.absolute.year = pDeliver->timeStamp.time.absolute.year;
757                 tmpTimeStamp.time.absolute.month = pDeliver->timeStamp.time.absolute.month;
758                 tmpTimeStamp.time.absolute.day = pDeliver->timeStamp.time.absolute.day;
759                 tmpTimeStamp.time.absolute.hour = pDeliver->timeStamp.time.absolute.hour;
760                 tmpTimeStamp.time.absolute.minute = pDeliver->timeStamp.time.absolute.minute;
761                 tmpTimeStamp.time.absolute.second = pDeliver->timeStamp.time.absolute.second;
762                 tmpTimeStamp.time.absolute.timeZone = pDeliver->timeStamp.time.absolute.timeZone;
763         }
764 }
765
766
767 void Sms3gppWapPushHandler::handleWapPushMsg(const char *pUserData, int DataSize, int simIndex, time_t sent_time)
768 {
769         MSG_BEGIN();
770
771 #ifdef MSG_FW_FOR_DEBUG
772         MSG_DEBUG("DataSize [%d]", DataSize);
773
774         MSG_DEBUG("[pUserData]");
775         for (int i = 0; i < DataSize; i++) {
776                 printf("[%02x]", pUserData[i]);
777         }
778         printf("\n\n");
779 #endif
780
781         unsigned char* pPDUTypeData = (unsigned char*)pUserData;
782         unsigned long PDUTypeDataLen = DataSize;
783
784         char* pPushHeader = NULL;
785         unique_ptr<char*, void(*)(char**)> pushHeaderBuf(&pPushHeader, unique_ptr_deleter);
786         unsigned long pushHeaderLen = 0;
787
788         char* pPushBody = NULL;
789         unique_ptr<char*, void(*)(char**)> PushBodyBuf(&pPushBody, unique_ptr_deleter);
790         unsigned long pushBodyLen = 0;
791
792         unsigned long iPDU = 1;
793
794         char* pWspHeader = NULL;
795         unique_ptr<char*, void(*)(char**)> WspHeaderBuf(&pWspHeader, unique_ptr_deleter);
796         unsigned long   wspHeaderLen = 0;
797
798         char* pWspBody = NULL;
799         unique_ptr<char*, void(*)(char**)> WspBodyBuf(&pWspBody, unique_ptr_deleter);
800         unsigned long wspBodyLen = 0;
801
802         /* pass PDU type */
803         iPDU++;
804
805         pushHeaderLen = wspRetriveUintvarDecode(pPDUTypeData, &iPDU);
806
807         MSG_DEBUG("PDUTypeDataLen [%lu]", PDUTypeDataLen);
808         MSG_DEBUG("pushHeaderLen [%lu]", pushHeaderLen);
809         MSG_DEBUG("iPDU [%lu]", iPDU);
810
811         wspHeaderLen = pushHeaderLen + iPDU;
812
813         MSG_DEBUG("wspBodyLen [%lu]", wspBodyLen);
814
815         if (wspHeaderLen > 0) {
816                 pWspHeader = new char[wspHeaderLen];
817
818                 if (pWspHeader == NULL) {
819                         MSG_DEBUG("pWspHeader Memory Allocation Failed");
820                         return;
821                 } else {
822                         memcpy(pWspHeader, pPDUTypeData, wspHeaderLen);
823                 }
824         }
825
826 #ifdef MSG_FW_FOR_DEBUG
827         MSG_DEBUG("wspHeaderLen [%d]", wspHeaderLen);
828
829         MSG_DEBUG("[pWspHeader]");
830         for (int i = 0; i < wspHeaderLen; i++) {
831                 printf("[%02x]", pWspHeader[i]);
832         }
833         printf("\n\n");
834 #endif
835
836         /* return if it is below case error */
837         if (PDUTypeDataLen < wspHeaderLen) {
838                 MSG_DEBUG("PDUTypeDataLen < wspHeaderLen !!!");
839                 return;
840         }
841
842         /* Finding wspBody Information */
843         wspBodyLen = PDUTypeDataLen - wspHeaderLen;
844
845         if (wspBodyLen > 0) {
846                 pWspBody = new char[wspBodyLen];
847
848                 if (pWspBody == NULL) {
849                         MSG_DEBUG("pWspBody Memory Allocation Failed");
850                         return;
851                 } else {
852                         memcpy(pWspBody, pPDUTypeData + wspHeaderLen, wspBodyLen);
853                 }
854         } else {
855                 pWspBody = NULL;
856         }
857
858         wspDecodeHeader(pPDUTypeData + iPDU, pushHeaderLen, PDUTypeDataLen - (iPDU + pushHeaderLen), TRUE, &pPushHeader);
859
860         iPDU = iPDU + pushHeaderLen;
861
862         pushBodyLen = PDUTypeDataLen - iPDU ;
863
864         if (pushBodyLen > 0) {
865                 pPushBody = new char[pushBodyLen];
866
867                 if (pPushBody == NULL) {
868                         MSG_DEBUG("pPushBody Memory Allocation Failed");
869                         return;
870                 } else {
871                         memcpy(pPushBody, pPDUTypeData+ iPDU, pushBodyLen);
872                 }
873         } else {
874                 pPushBody = NULL;
875                 return;
876         }
877
878         handleWapPushCallback((char *)pPushHeader, (char *)pPushBody, (int)pushBodyLen, (char *)pWspHeader, (int)wspHeaderLen, (char *)pWspBody, (int)wspBodyLen, simIndex, sent_time);
879
880         MSG_END();
881 }
882
883 #if 0
884 void SmsPluginWapPushHandler::handleWapPushCallback(char* pPushHeader, char* pPushBody, int PushBodyLen, char* pWspHeader, int WspHeaderLen, char* pWspBody, int WspBodyLen)
885 {
886         MSG_BEGIN();
887
888         if (pPushBody == NULL) {
889                 MSG_DEBUG("pPushBody is NULL");
890                 return;
891         }
892
893         SMS_WAP_APP_CODE_T      appCode = getAppCode(pPushHeader);
894
895         /**  check Push message receive setting */
896         bool bPushRecv = false;
897
898         if (MsgSettingGetBool(PUSH_RECV_OPTION, &bPushRecv) != MSG_SUCCESS)
899                 MSG_INFO("MsgSettingGetBool() is failed");
900
901         if ((bPushRecv == false) && (appCode != SMS_WAP_APPLICATION_MMS_UA)) {
902                 MSG_DEBUG("Push Message Receive option is OFF. Drop Push Message.");
903                 return;
904         }
905
906         switch (appCode) {
907         case SMS_WAP_APPLICATION_MMS_UA:
908                 MSG_DEBUG("Received MMS Notification");
909                 handleMMSNotification(pPushBody, PushBodyLen);
910                 break;
911
912         case SMS_WAP_APPLICATION_PUSH_SI:
913                 MSG_DEBUG("Received WAP Push (Service Indication Textual form)");
914                 handleSIMessage(pPushBody, PushBodyLen, true);
915                 break;
916
917         case SMS_WAP_APPLICATION_PUSH_SIC:
918                 MSG_DEBUG("Received WAP Push (Service Indication Tokenised form)");
919                 handleSIMessage(pPushBody, PushBodyLen, false);
920                 break;
921
922         case SMS_WAP_APPLICATION_PUSH_SL:
923                 MSG_DEBUG("Received WAP Push (Service Loading Textual form)");
924                 handleSLMessage(pPushBody, PushBodyLen, true);
925                 break;
926
927         case SMS_WAP_APPLICATION_PUSH_SLC:
928                 MSG_DEBUG("Received WAP Push (Service Loading Tokenised form)");
929                 handleSLMessage(pPushBody, PushBodyLen, false);
930                 break;
931
932         case SMS_WAP_APPLICATION_PUSH_CO:
933                 MSG_DEBUG("Received WAP Push (Cache Operation Textual form)");
934                 handleCOMessage(pPushBody, PushBodyLen, true);
935                 break;
936
937         case SMS_WAP_APPLICATION_PUSH_COC:
938                 MSG_DEBUG("Received WAP Push (Cache Operation Tokenised form)");
939                 handleCOMessage(pPushBody, PushBodyLen, false);
940                 break;
941
942         case SMS_WAP_APPLICATION_SYNCML_DM_BOOTSTRAP:
943                 MSG_DEBUG("Received DM BOOTSTRAP");
944                 SmsPluginEventHandler::instance()->handleSyncMLMsgIncoming(DM_WBXML, pPushBody, PushBodyLen, pWspHeader, WspHeaderLen);
945                 break;
946
947         case SMS_WAP_APPLICATION_SYNCML_DM_BOOTSTRAP_XML:
948                 MSG_DEBUG("Received DM BOOTSTRAP");
949                 SmsPluginEventHandler::instance()->handleSyncMLMsgIncoming(DM_XML, pPushBody, PushBodyLen, pWspHeader, WspHeaderLen);
950                 break;
951
952         case SMS_WAP_APPLICATION_PUSH_PROVISIONING_XML:
953                 MSG_DEBUG("Received Provisioning");
954                 SmsPluginEventHandler::instance()->handleSyncMLMsgIncoming(CP_XML, pPushBody, PushBodyLen, pWspHeader, WspHeaderLen);
955                 break;
956
957         case SMS_WAP_APPLICATION_PUSH_PROVISIONING_WBXML:
958                 MSG_DEBUG("Received Provisioning");
959                 SmsPluginEventHandler::instance()->handleSyncMLMsgIncoming(CP_WBXML, pPushBody, PushBodyLen, pWspHeader, WspHeaderLen);
960                 break;
961
962         case SMS_WAP_APPLICATION_PUSH_BROWSER_SETTINGS:
963         case SMS_WAP_APPLICATION_PUSH_BROWSER_BOOKMARKS:
964                 MSG_DEBUG("Received Provisioning");
965                 SmsPluginEventHandler::instance()->handleSyncMLMsgIncoming(OTHERS, pPushBody, PushBodyLen, pWspHeader, WspHeaderLen);
966                 break;
967
968         case SMS_WAP_APPLICATION_SYNCML_DM_NOTIFICATION:
969                 MSG_DEBUG("Received DM Notification");
970                 SmsPluginEventHandler::instance()->handleSyncMLMsgIncoming(DM_NOTIFICATION, pPushBody, PushBodyLen, pWspHeader, WspHeaderLen);
971                 break;
972
973         case SMS_WAP_APPLICATION_SYNCML_DS_NOTIFICATION:
974                 MSG_DEBUG("Received DS Notification");
975                 SmsPluginEventHandler::instance()->handleSyncMLMsgIncoming(DS_NOTIFICATION, pPushBody, PushBodyLen, pWspHeader, WspHeaderLen);
976                 break;
977
978         case SMS_WAP_APPLICATION_SYNCML_DS_NOTIFICATION_WBXML:
979                 MSG_DEBUG("Received DS Notification");
980                 SmsPluginEventHandler::instance()->handleSyncMLMsgIncoming(DS_WBXML, pPushBody, PushBodyLen, pWspHeader, WspHeaderLen);
981                 break;
982
983         case SMS_WAP_APPLICATION_DRM_UA_RIGHTS_XML:
984         case SMS_WAP_APPLICATION_DRM_UA_RIGHTS_WBXML:
985                 MSG_DEBUG("Received DRM UA");
986
987                 if (pPushBody != NULL)
988                         handleDrmVer1(pPushBody, PushBodyLen);
989
990                 break;
991
992         case SMS_WAP_APPLICATION_DRM_V2_RO_XML:
993         case SMS_WAP_APPLICATION_DRM_V2_ROAP_PDU_XML:
994         case SMS_WAP_APPLICATION_DRM_V2_ROAP_TRIGGER_XML:
995         case SMS_WAP_APPLICATION_DRM_V2_ROAP_TRIGGER_WBXML:
996                 MSG_DEBUG("Received DRM V2");
997                 /* TODO: DRM V2 */
998                 break;
999
1000         case SMS_WAP_APPLICATION_PUSH_EMAIL:
1001         case SMS_WAP_APPLICATION_PUSH_EMAIL_XML:
1002         case SMS_WAP_APPLICATION_PUSH_EMAIL_WBXML:
1003                 MSG_DEBUG("Received Email");
1004                 /* TODO: Email */
1005                 break;
1006
1007         case SMS_WAP_APPLICATION_PUSH_IMPS_CIR:
1008                 MSG_DEBUG("Received IMPS CIR");
1009                 /* TODO: IMPS CIR */
1010                 break;
1011
1012         case SMS_WAP_APPLICATION_LBS :
1013                 MSG_DEBUG("Received LBS related message");
1014                 SmsPluginEventHandler::instance()->handleLBSMsgIncoming(pPushHeader, pPushBody, PushBodyLen);
1015                 /* TODO: LBS */
1016                 break;
1017
1018         case SMS_WAP_APPLICATION_PUSH_SIA :
1019                 MSG_DEBUG("Received SIA");
1020                 /* TODO: SIA */
1021                 break;
1022
1023         default:
1024                 MSG_DEBUG("Unknown Application [%d]", appCode);
1025                 break;
1026         }
1027
1028         MSG_END();
1029 }
1030 #else
1031
1032 static void launchProcessByAppcode(int appcode)
1033 {
1034         MSG_BEGIN();
1035         GError *error = NULL;
1036         GDBusConnection *connection_agent = NULL;
1037         GDBusProxy *dbus_proxy_agent = NULL;
1038         GDBusConnection *connection_service = NULL;
1039         GDBusProxy *dbus_proxy_service = NULL;
1040         GVariant *result_agent = NULL;
1041         GVariant *result_service = NULL;
1042
1043         switch (appcode) {
1044         case SMS_WAP_APPLICATION_SYNCML_DM_BOOTSTRAP:
1045         case SMS_WAP_APPLICATION_SYNCML_DM_BOOTSTRAP_XML:
1046         case SMS_WAP_APPLICATION_PUSH_PROVISIONING_XML:
1047         case SMS_WAP_APPLICATION_PUSH_PROVISIONING_WBXML:
1048         case SMS_WAP_APPLICATION_PUSH_BROWSER_SETTINGS:
1049         case SMS_WAP_APPLICATION_PUSH_BROWSER_BOOKMARKS:
1050         case SMS_WAP_APPLICATION_SYNCML_DM_NOTIFICATION: {
1051                         connection_agent = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
1052                         if (error) {
1053                                 MSG_DEBUG("Connecting to system bus failed: %s\n", error->message);
1054                                 goto _DBUS_ERROR;
1055                         }
1056
1057                         dbus_proxy_agent = g_dbus_proxy_new_sync(connection_agent, G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
1058                                                                                 NULL, "org.tizen.omadmagent", "/org/tizen/omadmagent",
1059                                                                                 "org.tizen.omadmagent", NULL, &error);
1060                         if (error) {
1061                                 MSG_DEBUG("Connecting to agent proxy failed: %s\n", error->message);
1062                                 goto _DBUS_ERROR;
1063                         }
1064
1065                         result_agent = g_dbus_proxy_call_sync(dbus_proxy_agent, "Hello_Agent", NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
1066                         if (error) {
1067                                 MSG_DEBUG("invoking agent proxy call failed: %s\n", error->message);
1068                                 goto _DBUS_ERROR;
1069                         }
1070
1071                         connection_service = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
1072                         if (error) {
1073                                 MSG_DEBUG("Connecting to system bus failed: %s\n", error->message);
1074                                 goto _DBUS_ERROR;
1075                         }
1076
1077                         dbus_proxy_service = g_dbus_proxy_new_sync(connection_service, G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES, NULL,
1078                                                                                 "org.tizen.omadmservice", "/org/tizen/omadmservice",
1079                                                                                 "org.tizen.omadmservice", NULL, &error);
1080                         if (error) {
1081                                 MSG_DEBUG("Connecting to service proxy failed: %s\n", error->message);
1082                                 goto _DBUS_ERROR;
1083                         }
1084
1085                         result_service = g_dbus_proxy_call_sync(dbus_proxy_service, "wakeup", NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
1086                         if (error) {
1087                                 MSG_DEBUG("invoking service proxy call failed: %s\n", error->message);
1088                                 goto _DBUS_ERROR;
1089                         }
1090
1091                         MsgMutex mx;
1092                         MsgCndVar cv;
1093                         mx.lock();
1094                         cv.timedwait(mx.pMsgMutex(), 2);
1095                         mx.unlock();
1096                         MSG_END();
1097                 }
1098                 break;
1099         case SMS_WAP_APPLICATION_SYNCML_DS_NOTIFICATION:
1100         case SMS_WAP_APPLICATION_SYNCML_DS_NOTIFICATION_WBXML: {
1101                         connection_agent = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
1102                         if (error) {
1103                                 MSG_DEBUG("Connecting to system bus failed: %s\n", error->message);
1104                                 goto _DBUS_ERROR;
1105                         }
1106
1107                         dbus_proxy_agent = g_dbus_proxy_new_sync(connection_agent, G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES, NULL,
1108                                                                                 "org.tizen.omadsagent", "/org/tizen/omadsagent",
1109                                                                                 "org.tizen.omadsagent", NULL, &error);
1110                         if (error) {
1111                                 MSG_DEBUG("Connecting to agent proxy failed: %s\n", error->message);
1112                                 goto _DBUS_ERROR;
1113                         }
1114
1115                         result_agent = g_dbus_proxy_call_sync(dbus_proxy_agent, "Hello_Agent", NULL,
1116                                                                         G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
1117                         if (error) {
1118                                 MSG_DEBUG("invoking service error: %s\n", error->message);
1119                                 goto _DBUS_ERROR;
1120                         }
1121                 }
1122                 break;
1123         default:
1124                 break;
1125         }
1126
1127 _DBUS_ERROR:
1128         if (error) {
1129                 g_error_free(error);
1130                 error = NULL;
1131         }
1132
1133         if (connection_agent) {
1134                 g_object_unref(connection_agent);
1135                 connection_agent = NULL;
1136         }
1137
1138         if (dbus_proxy_agent) {
1139                 g_object_unref(dbus_proxy_agent);
1140                 dbus_proxy_agent = NULL;
1141         }
1142
1143         if (result_agent) {
1144                 g_object_unref(result_service);
1145                 result_service = NULL;
1146         }
1147
1148         if (connection_service) {
1149                 g_object_unref(connection_service);
1150                 connection_service = NULL;
1151         }
1152
1153         if (dbus_proxy_service) {
1154                 g_object_unref(dbus_proxy_service);
1155                 dbus_proxy_service = NULL;
1156         }
1157
1158         if (result_service) {
1159                 g_object_unref(result_service);
1160                 result_service = NULL;
1161         }
1162
1163         MSG_END();
1164 }
1165
1166
1167 void Sms3gppWapPushHandler::handleWapPushCallback(char* pPushHeader, char* pPushBody, int PushBodyLen, char* pWspHeader, int WspHeaderLen, char* pWspBody, int WspBodyLen, int simIndex, time_t sent_time)
1168 {
1169         MSG_BEGIN();
1170
1171         MSG_ERR_RET_M(pPushHeader == NULL, "pPushHeader is NULL!");
1172         MSG_ERR_RET_M(pPushBody == NULL, "pPushBody is NULL!");
1173
1174         msg_error_t err = MSG_SUCCESS;
1175         int pushEvt_cnt = 0;
1176         char app_id[MAX_WAPPUSH_ID_LEN] = {0, };
1177         char content_type[MAX_WAPPUSH_CONTENT_TYPE_LEN] = {0, };
1178         SmsPluginStorage *storageHandler = SmsPluginStorage::instance();
1179
1180         err = storageHandler->getRegisteredPushEvent(pPushHeader, &pushEvt_cnt, app_id, sizeof(app_id), content_type, sizeof(content_type));
1181         MSG_DEBUG("pushEvt_cnt: %d", pushEvt_cnt);
1182         if (err != MSG_SUCCESS) {
1183                 MSG_DEBUG("Fail to get registered push event");
1184                 return;
1185         }
1186
1187         for (int i = 0; i < pushEvt_cnt; ++i) {
1188                 /* check Push message receive setting */
1189                 bool bPushRecv = false;
1190                 int appcode = 0;
1191                 if (MsgSettingGetBool(PUSH_RECV_OPTION, &bPushRecv) != MSG_SUCCESS)
1192                         MSG_INFO("MsgSettingGetBool() is failed");
1193
1194                 storageHandler->getnthPushEvent(i, &appcode);
1195                 MSG_DEBUG("pushEvt_cnt: %d, appcode: %d", pushEvt_cnt, appcode);
1196                 if ((bPushRecv == false) && (IsWapPushMsgInWhiteList(appcode) == false)) {
1197                         MSG_DEBUG("Push Message Receive option is OFF. Drop Push Message.");
1198                         return;
1199                 }
1200
1201 #ifdef FEATURE_MMS_DISABLE
1202                 if (appcode == SMS_WAP_APPLICATION_MMS_UA) {
1203                         MSG_DEBUG("Drop MMS Notification for DOCOMO");
1204                         return;
1205                 }
1206
1207 #endif
1208                 launchProcessByAppcode(appcode);
1209
1210                 switch (appcode) {
1211                 case SMS_WAP_APPLICATION_MMS_UA:
1212                         MSG_DEBUG("Received MMS Notification");
1213                         handleMMSNotification(pPushBody, PushBodyLen, simIndex, sent_time);
1214                         break;
1215                 case SMS_WAP_APPLICATION_PUSH_SI:
1216                         MSG_DEBUG("Received WAP Push (Service Indication Textual form)");
1217                         handleSIMessage(pPushBody, PushBodyLen, true, simIndex);
1218                         break;
1219                 case SMS_WAP_APPLICATION_PUSH_SIC:
1220                         MSG_DEBUG("Received WAP Push (Service Indication Tokenised form)");
1221                         handleSIMessage(pPushBody, PushBodyLen, false, simIndex);
1222                         break;
1223                 case SMS_WAP_APPLICATION_PUSH_SL:
1224                         MSG_DEBUG("Received WAP Push (Service Loading Textual form)");
1225                         handleSLMessage(pPushBody, PushBodyLen, true, simIndex);
1226                         break;
1227                 case SMS_WAP_APPLICATION_PUSH_SLC:
1228                         MSG_DEBUG("Received WAP Push (Service Loading Tokenised form)");
1229                         handleSLMessage(pPushBody, PushBodyLen, false, simIndex);
1230                         break;
1231                 case SMS_WAP_APPLICATION_PUSH_CO:
1232                         MSG_DEBUG("Received WAP Push (Cache Operation Textual form)");
1233                         handleCOMessage(pPushBody, PushBodyLen, true, simIndex);
1234                         break;
1235                 case SMS_WAP_APPLICATION_PUSH_COC:
1236                         MSG_DEBUG("Received WAP Push (Cache Operation Tokenised form)");
1237                         handleCOMessage(pPushBody, PushBodyLen, false, simIndex);
1238                         break;
1239                 case SMS_WAP_APPLICATION_SYNCML_DM_BOOTSTRAP:
1240                         MSG_DEBUG("Received DM BOOTSTRAP");
1241                         Sms3gppEventHandler::instance()->handleSyncMLMsgIncoming(DM_WBXML, pPushBody, PushBodyLen, pWspHeader, WspHeaderLen, simIndex);
1242                         break;
1243                 case SMS_WAP_APPLICATION_SYNCML_DM_BOOTSTRAP_XML:
1244                         MSG_DEBUG("Received DM BOOTSTRAP");
1245                         Sms3gppEventHandler::instance()->handleSyncMLMsgIncoming(DM_XML, pPushBody, PushBodyLen, pWspHeader, WspHeaderLen, simIndex);
1246                         break;
1247                 case SMS_WAP_APPLICATION_PUSH_PROVISIONING_XML:
1248                         MSG_DEBUG("Received Provisioning");
1249                         Sms3gppEventHandler::instance()->handleSyncMLMsgIncoming(CP_XML, pPushBody, PushBodyLen, pWspHeader, WspHeaderLen, simIndex);
1250                         break;
1251                 case SMS_WAP_APPLICATION_PUSH_PROVISIONING_WBXML:
1252                         MSG_DEBUG("Received Provisioning");
1253                         Sms3gppEventHandler::instance()->handleSyncMLMsgIncoming(CP_WBXML, pPushBody, PushBodyLen, pWspHeader, WspHeaderLen, simIndex);
1254                         break;
1255                 case SMS_WAP_APPLICATION_PUSH_BROWSER_SETTINGS:
1256                 case SMS_WAP_APPLICATION_PUSH_BROWSER_BOOKMARKS:
1257                         MSG_DEBUG("Received Provisioning");
1258                         Sms3gppEventHandler::instance()->handleSyncMLMsgIncoming(OTHERS, pPushBody, PushBodyLen, pWspHeader, WspHeaderLen, simIndex);
1259                         break;
1260                 case SMS_WAP_APPLICATION_SYNCML_DM_NOTIFICATION:
1261                         MSG_DEBUG("Received DM Notification");
1262                         Sms3gppEventHandler::instance()->handleSyncMLMsgIncoming(DM_NOTIFICATION, pPushBody, PushBodyLen, pWspHeader, WspHeaderLen, simIndex);
1263                         break;
1264                 case SMS_WAP_APPLICATION_SYNCML_DS_NOTIFICATION:
1265                         MSG_DEBUG("Received DS Notification");
1266                         Sms3gppEventHandler::instance()->handleSyncMLMsgIncoming(DS_NOTIFICATION, pPushBody, PushBodyLen, pWspHeader, WspHeaderLen, simIndex);
1267                         break;
1268                 case SMS_WAP_APPLICATION_SYNCML_DS_NOTIFICATION_WBXML:
1269                         MSG_DEBUG("Received DS Notification");
1270                         Sms3gppEventHandler::instance()->handleSyncMLMsgIncoming(DS_WBXML, pPushBody, PushBodyLen, pWspHeader, WspHeaderLen, simIndex);
1271                         break;
1272                 case SMS_WAP_APPLICATION_DRM_UA_RIGHTS_XML:
1273                 case SMS_WAP_APPLICATION_DRM_UA_RIGHTS_WBXML:
1274                         MSG_DEBUG("Received DRM UA");
1275                         if (pPushBody != NULL)
1276                                 handleDrmVer1(pPushBody, PushBodyLen);
1277
1278                         break;
1279                 case SMS_WAP_APPLICATION_DRM_V2_RO_XML:
1280                 case SMS_WAP_APPLICATION_DRM_V2_ROAP_PDU_XML:
1281                 case SMS_WAP_APPLICATION_DRM_V2_ROAP_TRIGGER_XML:
1282                 case SMS_WAP_APPLICATION_DRM_V2_ROAP_TRIGGER_WBXML:
1283                         MSG_DEBUG("Received DRM V2");
1284                         /* TODO: DRM V2 */
1285                         break;
1286                 case SMS_WAP_APPLICATION_PUSH_EMAIL:
1287                 case SMS_WAP_APPLICATION_PUSH_EMAIL_XML:
1288                 case SMS_WAP_APPLICATION_PUSH_EMAIL_WBXML:
1289                         MSG_DEBUG("Received Email");
1290                         /* TODO: Email */
1291                         break;
1292                 case SMS_WAP_APPLICATION_PUSH_IMPS_CIR:
1293                         MSG_DEBUG("Received IMPS CIR");
1294                         /* TODO: IMPS CIR */
1295                         break;
1296                 case SMS_WAP_APPLICATION_LBS:
1297                         MSG_DEBUG("Received LBS related message");
1298                         Sms3gppEventHandler::instance()->handleLBSMsgIncoming(pPushHeader, pPushBody, PushBodyLen);
1299                         /* TODO: LBS */
1300                         break;
1301                 case SMS_WAP_APPLICATION_PUSH_SIA:
1302                         MSG_DEBUG("Received SIA");
1303                         /* TODO: SIA */
1304                         break;
1305                 default:
1306                         Sms3gppEventHandler::instance()->handlePushMsgIncoming(pPushHeader, pPushBody, PushBodyLen, app_id, content_type);
1307                         break;
1308                 }
1309         }
1310         storageHandler->releasePushEvent();
1311
1312         MSG_END();
1313 }
1314 #endif
1315
1316 bool Sms3gppWapPushHandler::IsWapPushMsgInWhiteList(int appCode)
1317 {
1318         MSG_BEGIN();
1319
1320         bool isAllowed = false;
1321
1322         switch (appCode) {
1323         case SMS_WAP_APPLICATION_MMS_UA:
1324         case SMS_WAP_APPLICATION_SYNCML_DM_BOOTSTRAP:
1325         case SMS_WAP_APPLICATION_SYNCML_DM_BOOTSTRAP_XML:
1326         case SMS_WAP_APPLICATION_PUSH_PROVISIONING_XML:
1327         case SMS_WAP_APPLICATION_PUSH_PROVISIONING_WBXML:
1328         case SMS_WAP_APPLICATION_PUSH_BROWSER_SETTINGS:
1329         case SMS_WAP_APPLICATION_PUSH_BROWSER_BOOKMARKS:
1330         case SMS_WAP_APPLICATION_SYNCML_DM_NOTIFICATION:
1331         case SMS_WAP_APPLICATION_SYNCML_DS_NOTIFICATION:
1332         case SMS_WAP_APPLICATION_SYNCML_DS_NOTIFICATION_WBXML:
1333                 MSG_INFO("appcode[%d] is allowed even if push receive option is disabled.", appCode);
1334                 isAllowed = true;
1335                 break;
1336         default:
1337                 MSG_DEBUG("appcode[%d] is not allowed when push receive option is disabled.", appCode);
1338                 break;
1339         }
1340
1341         return isAllowed;
1342 }
1343
1344
1345 void Sms3gppWapPushHandler::handleMMSNotification(const char *pPushBody, int PushBodyLen, int simIndex, time_t sent_time)
1346 {
1347         MSG_BEGIN();
1348
1349 #ifdef MSG_FW_FOR_DEBUG
1350         printf("\n\n[handleMMSNotification] Push Body.\n");
1351
1352         for (int i = 0; i < PushBodyLen; i++) {
1353                 printf(" [%02x]", pPushBody[i]);
1354         }
1355         printf("\n\n");
1356 #endif
1357
1358         /* Make MSG_MESSAGE_INFO_S */
1359         MSG_MESSAGE_INFO_S msgInfo;
1360         memset(&msgInfo, 0x00, sizeof(MSG_MESSAGE_INFO_S));
1361
1362         msgInfo.addressList = NULL;
1363         unique_ptr<MSG_ADDRESS_INFO_S*, void(*)(MSG_ADDRESS_INFO_S**)> addressListBuf(&msgInfo.addressList, unique_ptr_deleter);
1364
1365         createMsgInfo(&msgInfo);
1366
1367         /* Convert Type values */
1368         msgInfo.msgType.mainType = MSG_MMS_TYPE;
1369         msgInfo.msgType.subType = MSG_NOTIFICATIONIND_MMS;
1370         msgInfo.msgType.classType = MSG_CLASS_NONE;
1371         msgInfo.storageId = MSG_STORAGE_PHONE;
1372         msgInfo.dataSize = PushBodyLen;
1373         msgInfo.sim_idx = simIndex;
1374
1375         if (sent_time > 0)
1376                 msgInfo.displayTime = sent_time;
1377
1378         if (msgInfo.dataSize > MAX_MSG_TEXT_LEN) {
1379                 msgInfo.bTextSms = false;
1380
1381                 /* Save Message Data into File */
1382                 char fileName[MSG_FILENAME_LEN_MAX+1];
1383                 memset(fileName, 0x00, sizeof(fileName));
1384
1385                 if (MsgCreateFileName(fileName) == false)
1386                         THROW(MsgException::FILE_ERROR, "MsgCreateFileName error");
1387
1388                 if (MsgWriteIpcFile(fileName, pPushBody, msgInfo.dataSize) == false)
1389                         THROW(MsgException::FILE_ERROR, "MsgWriteIpcFile error");
1390
1391                 strncpy(msgInfo.msgData, fileName, MAX_MSG_DATA_LEN);
1392         } else {
1393                 msgInfo.bTextSms = true;
1394
1395                 memcpy(msgInfo.msgText, pPushBody, msgInfo.dataSize);
1396                 msgInfo.msgText[msgInfo.dataSize] = '\0';
1397         }
1398
1399         msg_error_t err = MSG_SUCCESS;
1400
1401         /* Add MMS Noti Msg into DB */
1402         err = SmsPluginStorage::instance()->checkMessage(&msgInfo);
1403
1404         if (err == MSG_SUCCESS) {
1405                 /*  Callback */
1406                 err = Sms3gppEventHandler::instance()->callbackMsgIncoming(&msgInfo);
1407                 if (err != MSG_SUCCESS) {
1408                         MSG_DEBUG("callbackMsgIncoming() Error !! [%d]", err);
1409                 }
1410         } else {
1411                 MSG_DEBUG("checkMessage() Error !! [%d]", err);
1412         }
1413
1414         MSG_END();
1415 }
1416
1417
1418 void XmlFree(xmlChar* tmpXml)
1419 {
1420         if (tmpXml != NULL) {
1421                 xmlFree(tmpXml);
1422                 tmpXml = NULL;
1423         }
1424 }
1425
1426 void Sms3gppWapPushHandler::handleSIMessage(char* pPushBody, int PushBodyLen, bool isText, int simIndex)
1427 {
1428         MSG_BEGIN();
1429
1430         MSG_PUSH_MESSAGE_S pushMsg = {};
1431
1432         xmlDocPtr       xmlDoc = NULL;
1433         xmlNodePtr      topNode = NULL;
1434         xmlNodePtr      indNode = NULL;
1435
1436         xmlChar* tmpXmlChar = NULL;
1437
1438         if (pPushBody == NULL) {
1439                 MSG_DEBUG("pPushBody is NULL");
1440                 return;
1441         }
1442
1443         getXmlDoc(pPushBody, PushBodyLen, &xmlDoc, isText);
1444
1445         if (xmlDoc == NULL) {
1446                 MSG_DEBUG("xmlDoc is NULL");
1447                 return;
1448         }
1449
1450         topNode = xmlDocGetRootElement(xmlDoc);
1451
1452         if (topNode == NULL) {
1453                 MSG_DEBUG("topNode is NULL");
1454                 xmlFreeDoc(xmlDoc);
1455                 return;
1456         }
1457
1458         indNode = topNode->xmlChildrenNode;
1459
1460         while (indNode != NULL) {
1461                 if (!xmlStrcmp(indNode->name, (const xmlChar*) "indication")) {
1462                         MSG_SEC_DEBUG("indNode->name = %s\n", indNode->name);
1463                         break;
1464                 }
1465
1466                 indNode = indNode->next;
1467         }
1468
1469         if (indNode == NULL) {
1470                 MSG_DEBUG("indNode is NULL.");
1471                 return;
1472         }
1473
1474         /**  temporary set to max. */
1475         pushMsg.expires = 0xFFFFFFFF;
1476
1477         /** setting received time */
1478         time_t t = time(NULL);
1479         time_t utfTime = time(&t);
1480
1481         pushMsg.received = (unsigned long)utfTime;
1482
1483         tmpXmlChar = xmlGetProp(indNode, (xmlChar*)SMS_PUSH_XML_HREF_TAG);
1484
1485         if (tmpXmlChar == NULL) {
1486                 MSG_DEBUG("href is NULL.");
1487                 return;
1488         }
1489
1490         if (tmpXmlChar != NULL)
1491                 strncpy(pushMsg.href, (char*)tmpXmlChar, MAX_WAPPUSH_HREF_LEN-1);
1492
1493         XmlFree(tmpXmlChar);
1494
1495         tmpXmlChar = xmlGetProp(indNode, (xmlChar*)SMS_PUSH_XML_SI_ID_TAG);
1496
1497         if (tmpXmlChar != NULL)
1498                 strncpy(pushMsg.id, (char*)tmpXmlChar, MAX_WAPPUSH_ID_LEN-1);
1499
1500         XmlFree(tmpXmlChar);
1501
1502         tmpXmlChar = xmlGetProp(indNode, (xmlChar*)SMS_PUSH_XML_CREATED_TAG);
1503
1504         if (tmpXmlChar != NULL)
1505                 pushMsg.created = convertXmlCharToSec((char*)tmpXmlChar);
1506
1507         if (pushMsg.created == 0)
1508                 pushMsg.created = pushMsg.received;
1509
1510         XmlFree(tmpXmlChar);
1511
1512         tmpXmlChar = xmlGetProp(indNode, (xmlChar*)SMS_PUSH_XML_EXPIRES_TAG);
1513
1514         if (tmpXmlChar != NULL)
1515                 pushMsg.expires = convertXmlCharToSec((char*)tmpXmlChar);
1516
1517         XmlFree(tmpXmlChar);
1518
1519         tmpXmlChar = xmlGetProp(indNode, (xmlChar*)SMS_PUSH_XML_ACTION_TAG);
1520
1521         pushMsg.action = convertSIActionStrToEnum((char*)tmpXmlChar);
1522
1523         XmlFree(tmpXmlChar);
1524
1525         tmpXmlChar = xmlNodeListGetString(xmlDoc, indNode->xmlChildrenNode, 1);
1526
1527         if (tmpXmlChar == NULL) {
1528                 MSG_DEBUG("contents is NULL.");
1529                 return;
1530         }
1531
1532         strncpy(pushMsg.contents, (char*)tmpXmlChar, MAX_WAPPUSH_CONTENTS_LEN-1);
1533
1534         /** Write push Msg to file */
1535         char fileName[MSG_FILENAME_LEN_MAX+1];
1536         memset(fileName, 0x00, sizeof(fileName));
1537
1538         if (MsgCreateFileName(fileName) == false) {
1539                 xmlFree(xmlDoc);
1540                 xmlFree(tmpXmlChar);
1541                 THROW(MsgException::FILE_ERROR, "MsgCreateFileName error");
1542         }
1543
1544         if (MsgWriteIpcFile(fileName, (char*)(&pushMsg), sizeof(pushMsg)) == false) {
1545                 xmlFree(xmlDoc);
1546                 xmlFree(tmpXmlChar);
1547                 THROW(MsgException::FILE_ERROR, "MsgWriteIpcFile error");
1548         }
1549
1550         /* Pack Message Info Structure */
1551         MSG_MESSAGE_INFO_S msgInfo;
1552         memset(&msgInfo, 0, sizeof(MSG_MESSAGE_INFO_S));
1553
1554         msgInfo.addressList = NULL;
1555         unique_ptr<MSG_ADDRESS_INFO_S*, void(*)(MSG_ADDRESS_INFO_S**)> addressListBuf(&msgInfo.addressList, unique_ptr_deleter);
1556
1557         createMsgInfo(&msgInfo);
1558
1559         strncpy(msgInfo.msgData, fileName, MAX_MSG_DATA_LEN);
1560
1561         /* Convert Type values */
1562         msgInfo.msgType.mainType = MSG_SMS_TYPE;
1563         msgInfo.msgType.subType = MSG_WAP_SI_SMS;
1564         msgInfo.sim_idx = simIndex;
1565
1566         msgInfo.dataSize = sizeof(pushMsg);
1567         getDisplayName(msgInfo.msgType.subType, msgInfo.addressList[0].addressVal);
1568         xmlFree(xmlDoc);
1569         xmlFree(tmpXmlChar);
1570
1571         msg_error_t err = MSG_SUCCESS;
1572
1573         /* Add WAP Push Msg into DB */
1574         err = SmsPluginStorage::instance()->checkMessage(&msgInfo);
1575         if (err == MSG_SUCCESS) {
1576                 /* Callback */
1577                 err = Sms3gppEventHandler::instance()->callbackMsgIncoming(&msgInfo);
1578                 if (err != MSG_SUCCESS)
1579                         MSG_DEBUG("callbackMsgIncoming() Error !! [%d]", err);
1580         } else {
1581                 MSG_DEBUG("checkMessage() Error !! [%d]", err);
1582         }
1583
1584         MSG_END();
1585
1586         return;
1587 }
1588
1589
1590 void Sms3gppWapPushHandler::handleSLMessage(char* pPushBody, int PushBodyLen, bool isText, int simIndex)
1591 {
1592         MSG_BEGIN();
1593
1594         MSG_PUSH_MESSAGE_S pushMsg = {};
1595
1596         xmlDocPtr       xmlDoc = NULL;
1597         xmlNodePtr      topNode = NULL;
1598         xmlNodePtr      indNode = NULL;
1599
1600         xmlChar*                tmpXmlChar = NULL;
1601
1602         msg_error_t err = MSG_SUCCESS;
1603
1604         if (pPushBody == NULL) {
1605                 MSG_DEBUG("pPushBody is NULL \n");
1606                 return;
1607         }
1608
1609         getXmlDoc(pPushBody, PushBodyLen, &xmlDoc, isText);
1610
1611         if (xmlDoc == NULL) {
1612                 MSG_DEBUG("xmlDoc is NULL \n");
1613                 return;
1614         }
1615
1616         topNode = xmlDocGetRootElement(xmlDoc);
1617
1618         if (topNode == NULL) {
1619                 MSG_DEBUG("Empty Document.");
1620                 xmlFreeDoc(xmlDoc);
1621                 return;
1622         } else {
1623                 MSG_SEC_DEBUG("Not an empty Document and topNode->name = %s \n", topNode->name);
1624         }
1625
1626         indNode = topNode;
1627
1628         while (indNode != NULL) {
1629                 if (!xmlStrcmp(indNode->name, (const xmlChar*)"sl")) {
1630                         MSG_SEC_DEBUG("indNode->name = %s\n", indNode->name);
1631                         break;
1632                 }
1633
1634                 indNode = indNode->next;
1635         }
1636
1637         /* setting received time setting */
1638         time_t t = time(NULL);
1639         time_t utfTime = time(&t);
1640
1641         pushMsg.received = (unsigned long)utfTime;
1642
1643         tmpXmlChar = xmlGetProp(indNode, (xmlChar*)SMS_PUSH_XML_HREF_TAG);
1644
1645         if (tmpXmlChar == NULL) {
1646                 MSG_DEBUG("href is NULL.");
1647                 return;
1648         }
1649
1650         strncpy(pushMsg.href, (char*)tmpXmlChar, MAX_WAPPUSH_HREF_LEN-1);
1651
1652         XmlFree(tmpXmlChar);
1653
1654         tmpXmlChar = xmlGetProp(indNode, (xmlChar*)SMS_PUSH_XML_ACTION_TAG);
1655         pushMsg.action = convertSLActionStrToEnum((char*)tmpXmlChar);
1656
1657         /* Setting other parameters in default values */
1658         pushMsg.created = pushMsg.received;
1659         /* temporary set to MAX value. */
1660         pushMsg.expires = 0xFFFFFFFF;
1661
1662         MSG_DEBUG("check pushMsg data");
1663         MSG_DEBUG("pushMsg.action : [%d]", pushMsg.action);
1664         MSG_DEBUG("pushMsg.received : [%lu]", pushMsg.received);
1665         MSG_DEBUG("pushMsg.created : [%lu]", pushMsg.created);
1666         MSG_DEBUG("pushMsg.expires : [%lu]", pushMsg.expires);
1667         MSG_SEC_DEBUG("pushMsg.id : [%s]", pushMsg.id);
1668         MSG_DEBUG("pushMsg.href : [%s]", pushMsg.href);
1669         MSG_DEBUG("pushMsg.contents : [%s]", pushMsg.contents);
1670
1671         /* Write push Msg to file */
1672         char fileName[MSG_FILENAME_LEN_MAX+1];
1673         memset(fileName, 0x00, sizeof(fileName));
1674
1675         if (MsgCreateFileName(fileName) == false)
1676                 THROW(MsgException::FILE_ERROR, "MsgCreateFileName error");
1677
1678         if (MsgWriteIpcFile(fileName, (char*)(&pushMsg), sizeof(pushMsg)) == false)
1679                 THROW(MsgException::FILE_ERROR, "MsgWriteIpcFile error");
1680
1681         /* Pack Message Info Structure */
1682         MSG_MESSAGE_INFO_S msgInfo;
1683         memset(&msgInfo, 0, sizeof(MSG_MESSAGE_INFO_S));
1684
1685         msgInfo.addressList = NULL;
1686         unique_ptr<MSG_ADDRESS_INFO_S*, void(*)(MSG_ADDRESS_INFO_S**)> addressListBuf(&msgInfo.addressList, unique_ptr_deleter);
1687
1688         createMsgInfo(&msgInfo);
1689
1690         strncpy(msgInfo.msgData, fileName, MAX_MSG_DATA_LEN);
1691
1692         /* Convert Type values */
1693         msgInfo.msgType.mainType = MSG_SMS_TYPE;
1694         msgInfo.msgType.subType = MSG_WAP_SL_SMS;
1695         msgInfo.sim_idx = simIndex;
1696
1697         /* Update Msg Text */
1698         strncpy(msgInfo.msgText, pushMsg.href, MAX_MSG_TEXT_LEN);
1699
1700         msgInfo.dataSize = sizeof(pushMsg);
1701         getDisplayName(msgInfo.msgType.subType, msgInfo.addressList[0].addressVal);
1702         MSG_DEBUG("dataSize : %zu", msgInfo.dataSize);
1703
1704         /** Add WAP Push Msg into DB */
1705         err = SmsPluginStorage::instance()->checkMessage(&msgInfo);
1706         if (err == MSG_SUCCESS) {
1707                 /* Callback to MSG FW */
1708                 err = Sms3gppEventHandler::instance()->callbackMsgIncoming(&msgInfo);
1709
1710                 if (err != MSG_SUCCESS)
1711                         MSG_DEBUG("callbackMsgIncoming is failed, err=[%d]", err);
1712         } else {
1713                 MSG_DEBUG("checkMessage() Error !! [%d]", err);
1714         }
1715
1716         xmlFree(xmlDoc);
1717         xmlFree(tmpXmlChar);
1718
1719         MSG_END();
1720
1721         return;
1722 }
1723
1724 void Sms3gppWapPushHandler::handleCOMessage(char* pPushBody, int PushBodyLen, bool isText, int simIndex)
1725 {
1726         MSG_PUSH_CACHEOP_S cacheOp;
1727
1728         xmlDocPtr       xmlDoc = NULL;
1729         xmlNodePtr      topNode = NULL;
1730         xmlNodePtr      indNode = NULL;
1731
1732         memset(&cacheOp, 0x00, sizeof(cacheOp));
1733
1734         MSG_DEBUG("Enter handleCOMessage");
1735
1736         if (pPushBody == NULL) {
1737                 MSG_DEBUG("pPushBody is NULL \n");
1738                 return;
1739         }
1740
1741         getXmlDoc(pPushBody, PushBodyLen, &xmlDoc, isText);
1742
1743         if (xmlDoc == NULL) {
1744                 MSG_DEBUG("xmlDoc is NULL \n");
1745                 return;
1746         }
1747
1748         topNode = xmlDocGetRootElement(xmlDoc);
1749         if (topNode == NULL) {
1750                 MSG_DEBUG("Warning:Empty Document\n");
1751                 xmlFreeDoc(xmlDoc);
1752                 return;
1753         }
1754
1755         indNode = topNode->xmlChildrenNode;
1756
1757         while (indNode != NULL) {
1758                 xmlChar* tmpUrl = NULL;
1759                 if (!xmlStrcmp(indNode->name, (const xmlChar*)SMS_PUSH_XML_INVAL_OBJ)) {
1760                         MSG_SEC_DEBUG("indNode->name = %s\n", indNode->name);
1761
1762                         tmpUrl = xmlGetProp(indNode, (xmlChar*)SMS_PUSH_XML_CO_URI);
1763
1764                         if (tmpUrl != NULL) {
1765                                 strncpy(cacheOp.invalObjectUrl[cacheOp.invalObjectCnt++], (char*)tmpUrl, MAX_PUSH_CACHEOP_MAX_URL_LEN-1);
1766
1767                                 MSG_DEBUG("%dth invalObjectUrl is <%s>\n", cacheOp.invalObjectCnt, cacheOp.invalObjectUrl[cacheOp.invalObjectCnt-1]);
1768                         } else {
1769                                 MSG_DEBUG("NO href value from the xmlDoc\n");
1770                         }
1771                 } else if (!xmlStrcmp(indNode->name, (const xmlChar*)SMS_PUSH_XML_INVAL_SVC)) {
1772                         MSG_SEC_DEBUG("indNode->name = %s\n", indNode->name);
1773                         tmpUrl = xmlGetProp(indNode, (xmlChar*)SMS_PUSH_XML_CO_URI);
1774
1775                         if (tmpUrl != NULL) {
1776                                 strncpy(cacheOp.invalServiceUrl[cacheOp.invalServiceCnt++], (char*)tmpUrl, MAX_PUSH_CACHEOP_MAX_URL_LEN-1);
1777                                 MSG_DEBUG("%dth invalServiceUrl is <%s>\n", cacheOp.invalServiceCnt, cacheOp.invalServiceUrl[cacheOp.invalServiceCnt-1]);
1778                         } else {
1779                                 MSG_DEBUG("NO href value from the xmlDoc\n");
1780                         }
1781                 }
1782
1783                 if (tmpUrl != NULL)
1784                         xmlFree(tmpUrl);
1785
1786                 indNode = indNode->next;
1787         }
1788
1789         /* Write push Msg to file */
1790         char fileName[MSG_FILENAME_LEN_MAX+1];
1791         memset(fileName, 0x00, sizeof(fileName));
1792
1793         if (MsgCreateFileName(fileName) == false) {
1794                 xmlFree(xmlDoc);
1795                 THROW(MsgException::FILE_ERROR, "MsgCreateFileName error");
1796         }
1797
1798         if (MsgWriteIpcFile(fileName, (char*)(&cacheOp), sizeof(cacheOp)) == false) {
1799                 xmlFree(xmlDoc);
1800                 THROW(MsgException::FILE_ERROR, "MsgWriteIpcFile error");
1801         }
1802
1803         /* Pack Message Info Structure */
1804         MSG_MESSAGE_INFO_S msgInfo;
1805         memset(&msgInfo, 0, sizeof(MSG_MESSAGE_INFO_S));
1806
1807         msgInfo.addressList = NULL;
1808         unique_ptr<MSG_ADDRESS_INFO_S*, void(*)(MSG_ADDRESS_INFO_S**)> addressListBuf(&msgInfo.addressList, unique_ptr_deleter);
1809
1810         createMsgInfo(&msgInfo);
1811
1812         strncpy(msgInfo.msgData, fileName, MAX_MSG_DATA_LEN);
1813
1814         /* Convert Type values */
1815         msgInfo.msgType.mainType = MSG_SMS_TYPE;
1816         msgInfo.msgType.subType = MSG_WAP_CO_SMS;
1817         msgInfo.sim_idx = simIndex;
1818
1819         msgInfo.dataSize = sizeof(cacheOp);
1820
1821         msg_error_t err = MSG_SUCCESS;
1822
1823         /* Add WAP Push Msg into DB */
1824         err = SmsPluginStorage::instance()->checkMessage(&msgInfo);
1825         if (err == MSG_SUCCESS) {
1826                 /* Callback */
1827                 err = Sms3gppEventHandler::instance()->callbackMsgIncoming(&msgInfo);
1828                 if (err != MSG_SUCCESS)
1829                         MSG_DEBUG("callbackMsgIncoming() Error !! [%d]", err);
1830         } else {
1831                 MSG_DEBUG("checkMessage() Error !! [%d]", err);
1832         }
1833
1834         xmlFree(xmlDoc);
1835
1836         return;
1837 }
1838
1839
1840 void Sms3gppWapPushHandler::handleDrmVer1(char* pPushBody, int PushBodyLen)
1841 {
1842 #if MSG_DRM_SUPPORT
1843         int drmRt = DRM_RETURN_SUCCESS;
1844         char* cid = NULL;
1845         unique_ptr<char*, void(*)(char**)> buf(&cid, unique_ptr_deleter);
1846
1847         MSG_DEBUG("Received DRM RIGHTS OBJECT Type WAP Push Message.");
1848         drm_request_type_e request_type = DRM_REQUEST_TYPE_REGISTER_LICENSE;
1849         drm_register_lic_info_s lic_req_info;
1850         drm_register_lic_resp_s lic_resp_info;
1851
1852         bzero(&lic_req_info, sizeof(drm_register_lic_info_s));
1853         bzero(&lic_resp_info, sizeof(drm_register_lic_resp_s));
1854         bzero(lic_req_info.lic_data, sizeof(lic_req_info.lic_data));
1855
1856         memcpy(lic_req_info.lic_data, pPushBody, PushBodyLen);
1857
1858         lic_req_info.lic_version = DRM_OMA_DRMV1_RIGHTS;
1859         lic_req_info.roap_init_src = DRM_ROAP_INIT_FROM_WAPPUSH;
1860         lic_req_info.operation_callback.callback = NULL;
1861
1862         drmRt = drm_process_request(request_type, &lic_req_info, &lic_resp_info);
1863         if (drmRt == DRM_RETURN_SUCCESS) {
1864                 MSG_DEBUG("DRM successfully registed to drm-service.");
1865         } else {
1866                 MSG_DEBUG("Fail to regist DRM to drm-service.");
1867         }
1868 #endif
1869         return;
1870 }
1871
1872
1873 void Sms3gppWapPushHandler::createMsgInfo(MSG_MESSAGE_INFO_S* pMsgInfo)
1874 {
1875         /* Convert class Type values */
1876         pMsgInfo->msgType.classType = MSG_CLASS_NONE;
1877
1878         /* set folder id (temporary) */
1879         pMsgInfo->folderId = MSG_INBOX_ID;
1880
1881         pMsgInfo->networkStatus = MSG_NETWORK_RECEIVED;
1882         pMsgInfo->bRead = false;
1883         pMsgInfo->bProtected = false;
1884         pMsgInfo->priority = MSG_MESSAGE_PRIORITY_NORMAL;
1885         pMsgInfo->direction = MSG_DIRECTION_TYPE_MT;
1886
1887         time_t rawtime = time(NULL);
1888         /* Comment below lines to save local UTC time..... (it could be used later.) */
1889         /*
1890         if (tmpTimeStamp.format == SMS_3GPP_TIME_ABSOLUTE) {
1891
1892                 MSG_DEBUG("year : %d", tmpTimeStamp.time.absolute.year);
1893                 MSG_DEBUG("month : %d", tmpTimeStamp.time.absolute.month);
1894                 MSG_DEBUG("day : %d", tmpTimeStamp.time.absolute.day);
1895                 MSG_DEBUG("hour : %d", tmpTimeStamp.time.absolute.hour);
1896                 MSG_DEBUG("minute : %d", tmpTimeStamp.time.absolute.minute);
1897                 MSG_DEBUG("second : %d", tmpTimeStamp.time.absolute.second);
1898                 MSG_DEBUG("timezone : %d", tmpTimeStamp.time.absolute.timeZone);
1899
1900                 char displayTime[32];
1901                 struct tm * timeTM;
1902
1903                 struct tm timeinfo;
1904                 memset(&timeinfo, 0x00, sizeof(tm));
1905
1906                 timeinfo.tm_year = (tmpTimeStamp.time.absolute.year + 100);
1907                 timeinfo.tm_mon = (tmpTimeStamp.time.absolute.month - 1);
1908                 timeinfo.tm_mday = tmpTimeStamp.time.absolute.day;
1909                 timeinfo.tm_hour = tmpTimeStamp.time.absolute.hour;
1910                 timeinfo.tm_min = tmpTimeStamp.time.absolute.minute;
1911                 timeinfo.tm_sec = tmpTimeStamp.time.absolute.second;
1912                 timeinfo.tm_isdst = 0;
1913
1914                 rawtime = mktime(&timeinfo);
1915
1916                 MSG_DEBUG("tzname[0] [%s]", tzname[0]);
1917                 MSG_DEBUG("tzname[1] [%s]", tzname[1]);
1918                 MSG_DEBUG("timezone [%d]", timezone);
1919                 MSG_DEBUG("daylight [%d]", daylight);
1920
1921                 memset(displayTime, 0x00, sizeof(displayTime));
1922                 strftime(displayTime, 32, "%Y-%02m-%02d %T %z", &timeinfo);
1923                 MSG_DEBUG("displayTime [%s]", displayTime);
1924
1925                 rawtime -= (tmpTimeStamp.time.absolute.timeZone * (3600/4));
1926
1927                 timeTM = localtime(&rawtime);
1928                 memset(displayTime, 0x00, sizeof(displayTime));
1929                 strftime(displayTime, 32, "%Y-%02m-%02d %T %z", timeTM);
1930                 MSG_DEBUG("displayTime [%s]", displayTime);
1931
1932                 rawtime -= timezone;
1933
1934                 timeTM = localtime(&rawtime);
1935                 memset(displayTime, 0x00, sizeof(displayTime));
1936                 strftime(displayTime, 32, "%Y-%02m-%02d %T %z", timeTM);
1937                 MSG_DEBUG("displayTime [%s]", displayTime);
1938         }
1939         */
1940
1941         pMsgInfo->displayTime = rawtime;
1942
1943         /* Convert Address values */
1944         pMsgInfo->nAddressCnt = 1;
1945
1946         pMsgInfo->addressList = (MSG_ADDRESS_INFO_S *)new char[sizeof(MSG_ADDRESS_INFO_S)];
1947         memset(pMsgInfo->addressList, 0x00, sizeof(MSG_ADDRESS_INFO_S));
1948
1949         pMsgInfo->addressList[0].addressType = MSG_ADDRESS_TYPE_PLMN;
1950         strncpy(pMsgInfo->addressList[0].addressVal, tmpAddress.address, MAX_ADDRESS_VAL_LEN);
1951
1952         pMsgInfo->msgPort.valid = false;
1953         pMsgInfo->msgPort.dstPort = 0;
1954         pMsgInfo->msgPort.srcPort = 0;
1955 }
1956
1957 void Sms3gppWapPushHandler::getXmlDoc(const char* pPushBody, const int PushBodyLen, xmlDocPtr *pXmlDoc, const bool isText)
1958 {
1959         if (pPushBody == NULL) {
1960                 MSG_DEBUG("pPushBody is NULL");
1961                 return;
1962         }
1963
1964         if (isText) {
1965                 *pXmlDoc = xmlParseMemory(pPushBody, AcStrlen(pPushBody));
1966         } else {
1967                 WB_UTINY*       xmldata = NULL;
1968                 WBXMLConvWBXML2XML *conv = NULL;
1969                 WBXMLError ret = WBXML_OK;
1970
1971                 ret = wbxml_conv_wbxml2xml_create(&conv);
1972                 if (ret != WBXML_OK)
1973                         return;
1974
1975                 ret = wbxml_conv_wbxml2xml_run(conv, (WB_UTINY*)pPushBody, (WB_ULONG)PushBodyLen, &xmldata, NULL);
1976
1977                 wbxml_conv_wbxml2xml_destroy(conv);
1978
1979                 if (ret != WBXML_OK ||xmldata == NULL) {
1980                         MSG_DEBUG("xmldata is NULL. Error code is [%d].\n", ret);
1981                         return;
1982                 }
1983
1984                 MSG_DEBUG("xmldata : \n%s\n", xmldata);
1985
1986                 *pXmlDoc = xmlParseMemory((char*)xmldata, AcStrlen((char*)xmldata));
1987         }
1988 }
1989
1990 unsigned long Sms3gppWapPushHandler::convertXmlCharToSec(char* pDate)
1991 {
1992         struct tm timeStruct;
1993         time_t nTimeInSec = 0;
1994         char tmpBuf[10];
1995         int i = 0;
1996         int index = 0;
1997
1998         memset(tmpBuf, 0x00, sizeof(tmpBuf));
1999         memset(&timeStruct, 0x00, sizeof(struct tm));
2000
2001         /* check pDate */
2002         if (AcStrlen(pDate) < 20)
2003                 return 0;
2004
2005         MSG_DEBUG("pDate [%s]", pDate);
2006
2007         /* Year */
2008         for (i = 0; i < 4; i++) {
2009                 tmpBuf[i] = pDate[index++];
2010         }
2011         tmpBuf[i] = '\0';
2012         index++;
2013         timeStruct.tm_year = (atoi(tmpBuf)-1900);
2014
2015         /* Month */
2016         for (i = 0; i < 2; i++) {
2017                 tmpBuf[i] = pDate[index++];
2018         }
2019         tmpBuf[i] = '\0';
2020         index++;
2021         timeStruct.tm_mon = (atoi(tmpBuf) - 1);
2022
2023         /* Date */
2024         for (i = 0; i < 2; i++) {
2025                 tmpBuf[i] = pDate[index++];
2026         }
2027         tmpBuf[i] = '\0';
2028         index++;
2029         timeStruct.tm_mday = atoi(tmpBuf);
2030
2031         /* Hours */
2032         for (i = 0; i < 2; i++) {
2033                 tmpBuf[i] = pDate[index++];
2034         }
2035         tmpBuf[i] = '\0';
2036         index++;
2037         timeStruct.tm_hour = atoi(tmpBuf);
2038
2039         /* Minites */
2040         for (i = 0; i < 2; i++) {
2041                 tmpBuf[i] = pDate[index++];
2042         }
2043         tmpBuf[i] = '\0';
2044         index++;
2045         timeStruct.tm_min = atoi(tmpBuf);
2046
2047         /* Seconds */
2048         for (i = 0; i < 2; i++) {
2049                 tmpBuf[i] = pDate[index++];
2050         }
2051         tmpBuf[i] = '\0';
2052         index++;
2053         timeStruct.tm_sec = atoi(tmpBuf);
2054
2055         nTimeInSec = mktime(&timeStruct);
2056
2057         return nTimeInSec;
2058 }
2059
2060
2061 msg_push_action_t Sms3gppWapPushHandler::convertSIActionStrToEnum(char* pAction)
2062 {
2063         int comp = 0;
2064
2065         if (pAction == NULL) {
2066                 MSG_DEBUG("pAction is NULL. Setting to default action type.");
2067                 return MSG_PUSH_SI_ACTION_SIGNAL_MEDIUM;
2068         }
2069
2070         /* compare  with signal-none. */
2071         comp = g_strcmp0("signal-none", pAction);
2072         if (comp == 0)
2073                 return MSG_PUSH_SI_ACTION_SIGNAL_NONE;
2074
2075         /* compare  with signal-low. */
2076         comp = g_strcmp0("signal-low", pAction);
2077         if (comp == 0)
2078                 return MSG_PUSH_SI_ACTION_SIGNAL_LOW;
2079
2080         /* compare  with signal-medium. */
2081         comp = g_strcmp0("signal-medium", pAction);
2082         if (comp == 0)
2083                 return MSG_PUSH_SI_ACTION_SIGNAL_MEDIUM;
2084
2085         /* compare  with signal-high. */
2086         comp = g_strcmp0("signal-high", pAction);
2087         if (comp == 0)
2088                 return MSG_PUSH_SI_ACTION_SIGNAL_HIGH;
2089
2090         /* compare  with delete. */
2091         comp = g_strcmp0("delete", pAction);
2092         if (comp == 0)
2093                 return MSG_PUSH_SI_ACTION_DELETE;
2094
2095         /* signal-medium is default action value. */
2096         return MSG_PUSH_SI_ACTION_SIGNAL_MEDIUM;
2097 }
2098
2099
2100 msg_push_action_t Sms3gppWapPushHandler::convertSLActionStrToEnum(char* pAction)
2101 {
2102         int comp = 0;
2103
2104         if (pAction == NULL) {
2105                 MSG_DEBUG("MSG_DEBUG is NULL. Setting to default action type.\n");
2106                 return MSG_PUSH_SL_ACTION_EXECUTE_LOW;
2107         }
2108
2109         /* compare pSrcStr with execute-low. */
2110         comp = g_strcmp0("execute-low", pAction);
2111         if (comp == 0)
2112                 return MSG_PUSH_SL_ACTION_EXECUTE_LOW;
2113
2114         /* compare pSrcStr with execute-high. */
2115         comp = g_strcmp0("execute-high", pAction);
2116         if (comp == 0)
2117                 return MSG_PUSH_SL_ACTION_EXECUTE_HIGH;
2118
2119         /* compare pSrcStr with cache. */
2120         comp = g_strcmp0("cache", pAction);
2121         if (comp == 0)
2122                 return MSG_PUSH_SL_ACTION_CACHE;
2123
2124         /* default SL action value is execute-low. */
2125         return MSG_PUSH_SL_ACTION_EXECUTE_LOW;
2126 }
2127
2128
2129 unsigned long Sms3gppWapPushHandler::wspRetriveUintvarDecode(unsigned char* sourceData, unsigned long* currentPointer)
2130 {
2131         unsigned long i = 0;
2132         unsigned long decodedValue;
2133
2134         while (sourceData[*currentPointer + i] >= 0x80)
2135                 i++;
2136
2137         decodedValue = wspDecodeUintvar(i + 1, sourceData + *currentPointer);
2138         *currentPointer = *currentPointer + i + 1;
2139         MSG_DEBUG("wspRetriveUintvarDecode: decodedValue=%lu .\n", decodedValue);
2140         return decodedValue;
2141 }
2142
2143
2144 unsigned long Sms3gppWapPushHandler::wspDecodeUintvar(unsigned long length, unsigned char* userVar)
2145 {
2146         unsigned long i;
2147         unsigned long decodedUintvar = 0;
2148
2149         for (i = 0 ; i < length; i++) {
2150                 decodedUintvar = decodedUintvar +  (wspUintvarDecodeTable[i] * (userVar[length-(i+1)] & 0x7f));
2151         }
2152
2153         return decodedUintvar;
2154 }
2155
2156
2157 void Sms3gppWapPushHandler::wspDecodeHeader(unsigned char* sEncodedHeader, unsigned long encodedHeaderLen, unsigned long contentsLength, bool fContentType, char** pHeader)
2158 {
2159         unsigned long iField = 0;
2160         bool   continueField = FALSE;
2161         unsigned long currentLength;
2162
2163         char* encodedHeader = NULL;
2164         unique_ptr<char*, void(*)(char**)> encodedHeaderbuf(&encodedHeader, unique_ptr_deleter);
2165
2166         char* outTemper = NULL;
2167
2168         char* temper = NULL;
2169         unique_ptr<char*, void(*)(char**)> temperbuf(&temper, unique_ptr_deleter);
2170
2171         unsigned char track;
2172         unsigned long iEncodedHeader = 0;
2173         unsigned char fieldCode = 0xff;
2174
2175         /* outTemper is Decoded Headers.
2176             temper is Single Decoded Header.
2177         */
2178         if (NULL == (outTemper = new char[ WSP_STANDARD_STR_LEN_MAX * 5 ])) {
2179                 MSG_DEBUG("outTemper Memory allocation is failed.\n");
2180                 return;
2181         }
2182         memset(outTemper, 0, (WSP_STANDARD_STR_LEN_MAX * 5));
2183         currentLength = WSP_STANDARD_STR_LEN_MAX;
2184
2185         MSG_DEBUG("wspDecodeHeader: Message header decoding started.\n");
2186
2187         int loop;
2188         char szBuf[64];
2189
2190         szBuf[0] = 0x00;
2191         MSG_DEBUG("wspDecodeHeader: RAW data \n");
2192         for (loop = 0 ; loop < (int)encodedHeaderLen; loop++) {
2193                 char szTempBuf[5];
2194                 szTempBuf[0] = 0x00;
2195                 snprintf(szTempBuf, sizeof(szTempBuf), "%2X ", sEncodedHeader[loop]);
2196
2197                 if (AcStrlen(szBuf) + 7 < 64) {
2198                         strncat(szBuf, szTempBuf, sizeof(szBuf)-AcStrlen(szBuf)-1);
2199                 } else {
2200                         strncat(szBuf, "\n", sizeof(szBuf)-AcStrlen(szBuf)-1);
2201                         MSG_DEBUG("[%s]", szBuf);
2202                         szBuf[0] = 0x00;
2203                         strncat(szBuf, szTempBuf, sizeof(szBuf)-AcStrlen(szBuf)-1);
2204                 }
2205         }
2206         strncat(szBuf, "\n", sizeof(szBuf)-AcStrlen(szBuf)-1);
2207         MSG_DEBUG("[%s]", szBuf);
2208         MSG_DEBUG("fContentType=%d  \n", fContentType);
2209         /* operation for content-type */
2210         /* makes psuedo- content-type fieldcode */
2211         /* content - type is processed with header. But it's come without field code. So existence of fContentType can decide adding content type header field code whether ContentType + general header is or not. */
2212
2213         if (fContentType) {
2214                 encodedHeader = new char[ encodedHeaderLen + 1 ];
2215                 if (encodedHeader == NULL) {
2216                         MSG_DEBUG("encodedHeader Memory allocation is failed.\n");
2217                         return;
2218                 }
2219                 encodedHeader[0] = 0x91;
2220                 memcpy(encodedHeader + 1, sEncodedHeader, (size_t)encodedHeaderLen);
2221         } else {
2222                 encodedHeader = new char[ encodedHeaderLen ];
2223                 if (encodedHeader == NULL) {
2224                         MSG_DEBUG("encodedHeader Memory allocation is failed.\n");
2225                         return;
2226                 }
2227
2228                 memcpy(encodedHeader, sEncodedHeader, (size_t)encodedHeaderLen);
2229         }
2230
2231         /* Is it reacehd end of header? */
2232         while (iEncodedHeader < encodedHeaderLen) {
2233                 /* Get memory for single header */
2234                 if (temper == NULL) {
2235                         temper = new char[ WSP_STANDARD_STR_LEN_MAX * 5 ];
2236
2237                         if (temper == NULL) {
2238                                 MSG_DEBUG("temper Memory allocation is failed.\n");
2239                                 return;
2240                         }
2241                         memset(temper, 0x00, (WSP_STANDARD_STR_LEN_MAX * 5));
2242                 } else {
2243                         memset(temper, 0x00, (WSP_STANDARD_STR_LEN_MAX * 5));
2244                 }
2245
2246                 /* this section presents header code page shifting procedure
2247                    This part can be implemented by future request.
2248                 if (track == 0x 7f)
2249                 */
2250                 track = encodedHeader[iEncodedHeader];
2251
2252                 if (track == 0x00) {
2253                         MSG_DEBUG("WspLDecodeHeader: fieldcode  is 0 \n");
2254                         strncpy((char*) temper, (char*)"", (WSP_STANDARD_STR_LEN_MAX * 5)-1);
2255                         fieldCode = 0xff;
2256                         iEncodedHeader++;
2257                 } else if ((track > 0) && (track < 0x20)) {
2258                         iEncodedHeader++;
2259                 } else if ((track < 0x7f) && (track > 0x1f)) { /* In this case, first byte is normal string. So it's considered to unknown header. */
2260                         unsigned char* fieldName = (unsigned char*)gWapCodeBufferLeft;
2261                         unsigned char* fieldValue = (unsigned char*)gWapCodeBufferRight;
2262
2263                         strncpy((char*)fieldName, (char*)(encodedHeader + iEncodedHeader), WSP_CODE_BUFFER_LEFT_LEN_MAX-1);
2264                         fieldName[WSP_CODE_BUFFER_LEFT_LEN_MAX-1] = '\0';
2265                         iEncodedHeader = iEncodedHeader + AcStrlen((char*)fieldName) + 1;
2266                         strncpy((char*)fieldValue, (char*)(encodedHeader + iEncodedHeader), WSP_CODE_BUFFER_RIGHT_LEN_MAX-1);
2267                         fieldValue[WSP_CODE_BUFFER_RIGHT_LEN_MAX-1] = '\0';
2268                         iEncodedHeader = iEncodedHeader + AcStrlen((char*)fieldValue) + 1;
2269
2270                         strncat((char*)temper, (char*)fieldName, (WSP_STANDARD_STR_LEN_MAX * 5)-AcStrlen((char*)temper)-1);
2271                         strncat((char*)temper, ": ", (WSP_STANDARD_STR_LEN_MAX * 5)-AcStrlen((char*)temper)-1);
2272                         strncat((char*)temper, (char*)fieldValue, (WSP_STANDARD_STR_LEN_MAX * 5)-AcStrlen((char*)temper)-1);
2273                         /* this means 'don't process anymore.' */
2274                         fieldCode = 0xff;
2275
2276                 } else if (track > 0x7f) {
2277                         /* In case of first byte is field code, else case is error. */
2278                         /*if ((track & 0x7f) <= wspHeaderFieldCount) { */
2279                                 unsigned long  fieldValueLen = encodedHeader[iEncodedHeader + 1];
2280                                 unsigned char fieldValue[1275];
2281                                 fieldCode = track & 0x7f;
2282                                 /*
2283                                 if ((fieldValueLen == 0) || (fieldValueLen == 0x80)) {
2284                                         dprint(DNET_WAP,DNET_DBG_HIGH, "%X %X %X %X %X %X\n" , fieldCode, encodedHeader[iEncodedHeader + 1], encodedHeader[iEncodedHeader + 2],encodedHeader[iEncodedHeader + 3],encodedHeader[iEncodedHeader + 4], encodedHeader[iEncodedHeader + 5]);
2285                                 }
2286                                 */
2287                                 memset(fieldValue, 0, 1275);
2288
2289                                 /* add field name */
2290                                 /* This continueField flag show whether previous field code and current field code are same or not. If it's same, there are some sequential display effect by omitting field name addition process. The reason why it should be do that can be found in encoding example of spec. */
2291                                 if (!continueField) {
2292                                         strncat((char*)temper, (char*)wspHeaderFieldName[fieldCode], (WSP_STANDARD_STR_LEN_MAX * 5)-AcStrlen((char*)temper)-1);
2293                                         strncat((char*)temper, ": ", (WSP_STANDARD_STR_LEN_MAX * 5)-AcStrlen((char*)temper)-1);
2294                                         continueField = FALSE;
2295                                 }
2296
2297                                 /* field value is string */
2298                                 /* In this case, it just copy field value. */
2299                                 if ((fieldValueLen > LENGTH_QUOTE) && (fieldValueLen < 0x80)) {
2300                                         /* string field value should be NULL terminated */
2301                                         strncat((char*)temper, (char*)(encodedHeader + iEncodedHeader + 1), (WSP_STANDARD_STR_LEN_MAX * 5)-AcStrlen((char*)temper)-1);
2302                                         strncat((char*)temper, (char*)fieldValue, (WSP_STANDARD_STR_LEN_MAX * 5)-AcStrlen((char*)temper)-1);
2303
2304                                         iEncodedHeader = iEncodedHeader + AcStrlen((char*)encodedHeader + iEncodedHeader + 1) + 2;
2305                                         fieldCode = 0xff;
2306                                 }
2307
2308                                 /* first field value is length */
2309                                 /* If first byte of field value is length value, allocate field value by the length.
2310                                 In field value, data is
2311                                 1D 03 8F 24 24  - Then 8F 24 24 is field value.
2312                                 1D 1F 33.... - Then  allocate 33H for FieldValue.
2313                                 1D 8F - Then 8F
2314                                 1D 'Hi man!' Like 00, if string is come, then process without calculating field value.
2315                                 1D 8F 24 24 - In this case, original data is wrong.
2316                                 If  accept-charset: ISO-10646-ucs-2;Q=0.7 is
2317                                 01 03 03 E8 47
2318                                 01 - field code
2319                                 03 - field value length
2320                                 03 E8 47 - field value
2321                                 it's decoded by above value.
2322                                 */
2323                                 if (fieldValueLen < 0x20) {
2324                                         if (fieldValueLen  == LENGTH_QUOTE) {
2325                                                 /* field length is encoded in UINTVAR */
2326                                                 unsigned long  uintvarLen = 0;
2327                                                 fieldValueLen = wspRetriveUintvarDecode((unsigned char*) encodedHeader + iEncodedHeader + 2, &uintvarLen);
2328                                                 memcpy(fieldValue, encodedHeader + iEncodedHeader + 2 + uintvarLen, (size_t)fieldValueLen);
2329                                                 iEncodedHeader = iEncodedHeader + fieldValueLen + uintvarLen + 2;
2330                                         } else {
2331                                                 if (fieldValueLen == 1) {
2332                                                         /* field value is one byte integer over 0x80 */
2333                                                         /** make it two byte integer */
2334                                                         fieldValue[0] = 0x00;
2335                                                         memcpy(fieldValue + 1, encodedHeader + iEncodedHeader + 2, (size_t)fieldValueLen);
2336                                                         fieldValueLen = 2;
2337                                                         iEncodedHeader = iEncodedHeader + 1 + 2;
2338                                                 } else {
2339                                                         memcpy(fieldValue, encodedHeader + iEncodedHeader + 2, (size_t)fieldValueLen);
2340                                                         fieldValue[fieldValueLen] = 0;
2341                                                         iEncodedHeader = iEncodedHeader + fieldValueLen + 2;
2342                                                         if ((fieldValueLen == 0) || (fieldValueLen == 0x80)) {
2343                                                                 MSG_DEBUG("%X \n",  encodedHeader[iEncodedHeader]);
2344                                                         }
2345                                                 }
2346                                         }
2347                                 }
2348
2349                                 /* field value is single encoded */
2350                                 if (fieldValueLen > 0x7f) {
2351                                         fieldValue[0] = encodedHeader[iEncodedHeader + 1];
2352                                         fieldValueLen = 1;
2353                                         iEncodedHeader = iEncodedHeader + 2;
2354                                 }
2355                                 /* processing normal pre-defined field decoding */
2356
2357                                 MSG_DEBUG("WspLDecodeHeader: FieldCode %X\n", fieldCode);
2358                                 MSG_DEBUG("WspLDecodeHeader: fieldSize %lu\n", fieldValueLen);
2359
2360                                 if ((fieldCode  > wspHeaderFieldCount) && (fieldCode != 0xff)) {
2361                                         MSG_DEBUG("WspLDecodeHeader: unknown fieldcode %X \n", track);
2362                                         strncpy((char*) temper, (char*)"", (WSP_STANDARD_STR_LEN_MAX * 5)-1);
2363                                         fieldCode = 0xff;
2364                                 }
2365
2366                                 switch (fieldCode) {
2367                                         /* accept charset */
2368                                         /* It's normal way of field process. */
2369                                         case 0x01: {
2370                                                 unsigned long  i = 0;
2371                                                 unsigned long  code;
2372
2373                                                 /* Case of length of charset greater than 1 are two thigins.
2374                                                 1. code length of charset is greater than 1.
2375                                                 2. It include parameter.
2376                                                 3. Or both of two
2377                                                 */
2378                                                 if (1 != fieldValueLen) {
2379                                                         code = wspHeaderDecodeInteger(fieldValue);
2380                                                         /* Calculate iField. */
2381                                                         if (fieldValue[0] < 0x80)
2382                                                                 iField = fieldValue[0];
2383                                                         else
2384                                                                 iField = 1;
2385
2386                                                         while (wspCharset[i].charsetCode != code)
2387                                                                 i++;
2388                                                         strncat((char*)temper, (char*)wspCharset[i].charsetName, (WSP_STANDARD_STR_LEN_MAX * 5)-AcStrlen((char*)temper)-1);
2389                                                         /* If parameter exist */
2390                                                         if (iField < fieldValueLen) {
2391                                                                 char* param = NULL;
2392                                                                 unique_ptr<char*, void(*)(char**)> parambuf(&param, unique_ptr_deleter);
2393                                                                 wspHeaderDecodeQValue(fieldValueLen - iField, fieldValue + iField, &param);
2394                                                                 strncat((char*)temper, (char*)param, (WSP_STANDARD_STR_LEN_MAX * 5)-AcStrlen((char*)temper)-1);
2395                                                         }
2396                                                 } else {
2397                                                         code = fieldValue[0] & 0x7f;
2398
2399                                                         while ((wspCharset[i].charsetCode != code) && (wspCharset[i].charsetCode != 0xffff )) i++;
2400                                                         strncat((char*)temper, (char*)wspCharset[i].charsetName, (WSP_STANDARD_STR_LEN_MAX * 5)-AcStrlen((char*)temper)-1);
2401                                                 }
2402                                         }
2403                                         break;
2404
2405                                         /* type encoding */
2406                                         /* Like below routine, Same decoding routine process together. */
2407                                         /* Accept-encoding */
2408                                         case 0x02:
2409                                         /* content-encoding */
2410                                         case 0x0b: {
2411                                                 int integerValue;
2412
2413                                                 integerValue = wspHeaderDecodeIntegerByLength(fieldValue, fieldValueLen);
2414                                                 if (integerValue > 2) {
2415                                                         MSG_DEBUG("WspLDecodeHeader: integerValue is over limit(2).\n");
2416                                                         break;
2417                                                 }
2418                                                 strncat((char*)temper, (char*)wspEncodeMethod[integerValue], (WSP_STANDARD_STR_LEN_MAX * 5)-AcStrlen((char*)temper)-1);
2419                                         }
2420                                         break;
2421                                         /* contents type decoder */
2422                                         /* accept */
2423                                         case 0x00:
2424                                         /* content-type */
2425                                         case 0x11: {
2426                                                 unsigned long  contentsTypeCode;
2427                                                 unsigned long  i = 0;
2428                                                 /* encoded content type length body */
2429                                                 unsigned long  tempLen;
2430                                                 MSG_DEBUG("fieldValueLen: %lu", fieldValueLen);
2431
2432                                                 /* Like HTTP result state 304, it's for processing without Content type. This part doesn't defined. */
2433                                                 if (0 == fieldValueLen) {
2434                                                         strncat((char*)temper, (char*)"None" , (WSP_STANDARD_STR_LEN_MAX * 5)-AcStrlen((char*)temper)-1);
2435                                                         break;
2436                                                 }
2437                                                 /* 01 AE --> 00 AE --> AE*/
2438                                                 if (fieldValueLen == 2  &&  fieldValue[0] == 0) {
2439                                                         memcpy(fieldValue, encodedHeader + iEncodedHeader -1, (size_t)fieldValueLen-1);
2440                                                         MSG_DEBUG("WspLDecodeHeader:For mmO2 problem\r\n");
2441                                                 }
2442
2443                                                 if ((fieldValue[0] < 0x20) || (fieldValue[0] >= 0x80)) {
2444                                                         if (fieldValue[0] >= 0x80) {
2445                                                                 tempLen = 1;
2446                                                         } else if (fieldValueLen == 2 && fieldValue[0] == 0x03 && fieldValue[1] == 0x0A) { /** 06 05 02 03 0A AF 89 */
2447                                                                 fieldValue[3] = fieldValue[2];
2448                                                                 fieldValue[2] = fieldValue[1];
2449                                                                 fieldValue[1] = fieldValue[0];
2450                                                                 fieldValue[0] = 0x02;
2451                                                                 tempLen = 2;
2452                                                                 fieldValueLen = 3;
2453                                                                 MSG_DEBUG("WspLDecodeHeader:For CPE problem\r\n");
2454                                                         } else {
2455                                                                 tempLen = fieldValue[0]; /** 06 06 03 02 03 16 AF 88 */
2456                                                         }
2457
2458                                                         if (tempLen == 1) {
2459                                                                 char* szExtendedContent;
2460
2461                                                                 contentsTypeCode = fieldValue[0] & 0x7f;
2462                                                                 while ((wspContentsType[i].contentsTypeCode != contentsTypeCode) && (i < wspContentsTypeCount)) i++;
2463
2464                                                                 /* If specified content type doesn't exist */
2465                                                                 if (i < wspContentsTypeCount)
2466                                                                         strncat((char*)temper, (char*)wspContentsType[i].contentsTypeName, (WSP_STANDARD_STR_LEN_MAX * 5)-AcStrlen((char*)temper)-1);
2467
2468                                                                 szExtendedContent =  wspExtendedDecodeType((char)contentsTypeCode);
2469
2470                                                                 if (szExtendedContent != NULL) {
2471                                                                         MSG_DEBUG("WspLDecodeHeader: Tele2 server problem \n ");
2472                                                                         strncat((char*)temper, (char*)szExtendedContent, (WSP_STANDARD_STR_LEN_MAX * 5)-AcStrlen((char*)temper)-1);
2473                                                                 }
2474                                                         } else {
2475                                                                 contentsTypeCode = wspHeaderDecodeInteger(fieldValue);
2476
2477                                                                 while ((i < wspUnregisteredContentsTypeCount) && (wspUnregisterContentsType[i].contentsTypeCode != contentsTypeCode)) i++;
2478
2479                                                                 /** If there is a Content-Type assigned, */
2480                                                                 if (i < wspUnregisteredContentsTypeCount)
2481                                                                         strncat((char*)temper, (char*)wspUnregisterContentsType[i].contentsTypeName, (WSP_STANDARD_STR_LEN_MAX * 5)-AcStrlen((char*)temper)-1);
2482
2483                                                                 tempLen +=1;
2484                                                         }
2485                                                 } else {
2486                                                         tempLen = AcStrlen((char*)fieldValue) + 1;
2487
2488                                                         strncat((char*)temper, (char*)fieldValue, (WSP_STANDARD_STR_LEN_MAX * 5)-AcStrlen((char*)temper)-1);
2489                                                         MSG_DEBUG("WspLDecodeHeader: Attention, Decoding Check of Content-Type %lu\n ", tempLen);
2490                                                 }
2491
2492                                                 /* If there is a parameter */
2493                                                 if (tempLen < fieldValueLen) {
2494                                                         char* param = NULL;
2495                                                         unique_ptr<char*, void(*)(char**)> parambuf(&param, unique_ptr_deleter);
2496                                                         wspHeaderDecodeParameter(fieldValue + tempLen, fieldValueLen - tempLen, &param);
2497                                                         if (param != NULL) {
2498                                                                 strncat((char*)temper, "; ", (WSP_STANDARD_STR_LEN_MAX * 5)-AcStrlen((char*)temper)-1);
2499                                                                 strncat((char*)temper, (char*)param, (WSP_STANDARD_STR_LEN_MAX * 5)-AcStrlen((char*)temper)-1);
2500                                                         }
2501                                                 }
2502                                         }
2503                                         break;
2504
2505                                         /* language */
2506                                         /* content-language */
2507                                         case 0x0c:
2508                                         /* accept-language */
2509                                         case 0x03: {
2510                                                 unsigned long i = 0;
2511                                                 unsigned long code;
2512                                                 unsigned long tempLen;
2513                                                 if ((fieldValue[0] < 0x20) || (fieldValue[0] > 0x80)) {
2514                                                         if (fieldValue[0] > 0x80)
2515                                                                 tempLen = 1;
2516                                                         else
2517                                                                 tempLen = fieldValue[0];
2518                                                 } else {
2519                                                         tempLen = AcStrlen((char*)fieldValue) + 1;
2520                                                 }
2521
2522                                                 if (tempLen == 1) {
2523                                                         code = wspHeaderDecodeInteger(fieldValue);
2524                                                         while (wspLanguage[i].languageCode != code) {
2525                                                                 i++;
2526                                                                 if (i == wspLanguageCount)
2527                                                                         break;
2528                                                         }
2529
2530                                                         if (i < wspLanguageCount) {
2531                                                                 strncat((char*)temper, (char*)wspLanguage[i].languageName, (WSP_STANDARD_STR_LEN_MAX * 5)-AcStrlen((char*)temper)-1);
2532                                                         }
2533                                                 } else {
2534                                                         strncat((char*)temper, (char*)fieldValue, (WSP_STANDARD_STR_LEN_MAX * 5)-AcStrlen((char*)temper)-1);
2535                                                 }
2536
2537                                                 if (tempLen < fieldValueLen) {
2538                                                         char* param = NULL;
2539                                                         unique_ptr<char*, void(*)(char**)> parambuf(&param, unique_ptr_deleter);
2540                                                         wspHeaderDecodeQValue(fieldValueLen - tempLen, fieldValue + tempLen, &param);
2541                                                         strncat((char*)temper, (char*)param, (WSP_STANDARD_STR_LEN_MAX * 5)-AcStrlen((char*)temper)-1);
2542                                                 }
2543                                         }
2544                                         break;
2545
2546                                         /* integer */
2547                                         /* Max-forwards */
2548                                         case 0x1e:
2549                                         /* content-length */
2550                                         case 0x0d:
2551                                         /* age */
2552                                         case 0x05:
2553                                         /* Bearer-indication */
2554                                         case 0x33:
2555                                         /* Push-Flag */
2556                                         case 0x34: {
2557                                                 unsigned char temp[16];
2558                                                 /*
2559                                                 if ((fieldValueLen == 2) && (fieldValue[0] > 0x7f))
2560                                                         AcSprintf((char*)temp, "%u", (unsigned int)fieldValue[1]);
2561                                                 else
2562                                                 */
2563                                                 snprintf((char*)temp, sizeof(temp), "%u", (unsigned int)wspHeaderDecodeIntegerByLength(fieldValue, fieldValueLen));
2564                                                 strncat((char*)temper, (char*)temp, (WSP_STANDARD_STR_LEN_MAX * 5)-AcStrlen((char*)temper)-1);
2565                                         }
2566                                         break;
2567                                         /* X-Wap-Application-Id */
2568                                         case 0x2f: {
2569                                                 unsigned char temp[64];
2570                                                 int         integerValue;
2571
2572                                                 if (track == 0xaf) { /* WAP short-integer-encoded */
2573                                                         if (fieldValueLen == 2 &&  fieldValue[0] == 0) {
2574                                                                 memcpy(fieldValue, encodedHeader + iEncodedHeader -1, (size_t)fieldValueLen);
2575                                                         }
2576                                                         integerValue = wspHeaderDecodeIntegerByLength(fieldValue, fieldValueLen);
2577                                                 } else {
2578                                                         if (fieldValueLen == 2 &&  fieldValue[0] == 0) {
2579                                                                 memcpy(fieldValue, encodedHeader + iEncodedHeader -1, (size_t)fieldValueLen-1);
2580                                                                 MSG_DEBUG("WspLDecodeHeader:For mmO2 problem\r\n");
2581                                                                 fieldValueLen = 1;
2582                                                         }
2583                                                         integerValue = wspHeaderDecodeIntegerByLength(fieldValue, fieldValueLen);
2584                                                 }
2585
2586                                                 MSG_DEBUG("integerValue = [%02x]", integerValue);
2587
2588                                                 int count = sizeof(wspHeaderApplId)/sizeof(SMS_3GPP_WSP_HEADER_PARAMETER_S);
2589                                                 for (int i = 0; i < count ; ++i) {
2590                                                         if ((unsigned int)integerValue == wspHeaderApplId[i].parameterCode) {
2591                                                                 snprintf((char*)temp, 64, "%s", wspHeaderApplId[i].parameterToken);
2592                                                                 strncat((char*)temper, (char*)temp, (WSP_STANDARD_STR_LEN_MAX * 5)-AcStrlen(temper)-1);
2593                                                                 break;
2594                                                         }
2595                                                 }
2596                                         }
2597                                         break;
2598                                         /* Accept-Application */
2599                                         case 0x32:
2600                                                 if (0x80 == fieldValue[0]) {
2601                                                         strncat((char*)temper, "*", (WSP_STANDARD_STR_LEN_MAX * 5)-AcStrlen(temper)-1);
2602                                                 } else {
2603                                                         unsigned char temp[16];
2604                                                         /*
2605                                                         if ((fieldValueLen == 2) && (fieldValue[0] == 1))
2606                                                                 AcSprintf((char*)temp, "%u", (unsigned int)fieldValue[0]);
2607                                                         else
2608                                                         */
2609                                                         snprintf((char*)temp, sizeof(temp), "%u", (unsigned int)wspHeaderDecodeIntegerByLength(fieldValue, fieldValueLen));
2610                                                         strncat((char*)temper, (char*)temp, (WSP_STANDARD_STR_LEN_MAX * 5)-AcStrlen(temper)-1);
2611                                                 }
2612                                                 break;
2613
2614
2615                                         /* date */
2616                                         /* last-modified */
2617                                         case 0x1d:
2618                                         /* if-unmodified-since */
2619                                         case 0x1b:
2620                                         /* if-range */
2621                                         case 0x1a:
2622                                         /* if-modified-since */
2623                                         case 0x17:
2624                                         /* expires */
2625                                         case 0x14:
2626                                         /* date */
2627                                         case 0x12: {
2628                                                 char* decodedString = NULL;
2629                                                 unique_ptr<char*, void(*)(char**)> decodedStringbuf(&decodedString, unique_ptr_deleter);
2630                                                 wspHeaderDecodeDateValue(fieldValueLen, fieldValue, &decodedString);
2631                                                 strncat((char*)temper, (char*)decodedString, (WSP_STANDARD_STR_LEN_MAX * 5)-AcStrlen(temper)-1);
2632                                         }
2633                                         break;
2634
2635                                         /* connection */
2636                                         case 0x09:
2637                                                 if (fieldValue[0] == 0x80)
2638                                                         strncat((char*)temper, "Close", (WSP_STANDARD_STR_LEN_MAX * 5)-AcStrlen(temper)-1);
2639                                                 break;
2640                                         /* accept-ranges */
2641                                         case 0x04:
2642                                                 if (fieldValue[0] == 0x80)
2643                                                         strncat((char*)temper, "None", (WSP_STANDARD_STR_LEN_MAX * 5)-AcStrlen(temper)-1);
2644                                                 if (fieldValue[0] == 0x81)
2645                                                         strncat((char*)temper, "Bytes", (WSP_STANDARD_STR_LEN_MAX * 5)-AcStrlen(temper)-1);
2646                                                 break;
2647                                         /* content-md5 */
2648                                         case 0x0f: {
2649                                                 unsigned char temp[1275];
2650                                                 memcpy(temp, fieldValue, (size_t)fieldValueLen);
2651                                                 temp[fieldValueLen] = 0;
2652                                                 wspHeaderCopyDecodedString(temp, &currentLength, &temper);
2653                                         }
2654                                         break;
2655                                         /* Credential */
2656                                         /* authorization */
2657                                         case 0x07:
2658                                         /* proxy - authorization */
2659                                         case 0x21:
2660                                                 if (fieldValue[0] == 0x80) {
2661                                                         char* addString = NULL;
2662                                                         unique_ptr<char*, void(*)(char**)> addStringbuf(&addString, unique_ptr_deleter);
2663                                                         wspHeaderDecodeAuth(fieldValueLen, fieldValue, &addString);
2664                                                         strncat((char*)temper, addString, (WSP_STANDARD_STR_LEN_MAX * 5)-AcStrlen((char*)temper)-1);
2665                                                 } else {
2666                                                         iField = AcStrlen((char*)fieldValue) + 1;
2667
2668                                                         strncat((char*)temper, (char*)fieldValue, (WSP_STANDARD_STR_LEN_MAX * 5)-AcStrlen((char*)temper)-1);
2669                                                         if (iField < fieldValueLen) {
2670                                                                 char* param = NULL;
2671                                                                 unique_ptr<char*, void(*)(char**)> parambuf(&param, unique_ptr_deleter);
2672                                                                 wspHeaderDecodeParameter(fieldValue + 1, fieldValueLen - 1, &param);
2673                                                                 if (param != NULL) {
2674                                                                         strncat((char*)temper, ", ", (WSP_STANDARD_STR_LEN_MAX * 5)-AcStrlen((char*)temper)-1);
2675                                                                         strncat((char*)temper, (char*)param, (WSP_STANDARD_STR_LEN_MAX * 5)-AcStrlen((char*)temper)-1);
2676                                                                 }
2677                                                         }
2678                                                 }
2679                                                 break;
2680
2681
2682                                         /* Challenge */
2683                                         /* www - auth */
2684                                         case 0x2d:
2685                                         /* Proxy-authenticate */
2686                                         case 0x20:
2687                                                 if (0 == fieldValueLen)
2688                                                         break;
2689                                                 if (fieldValue[0] == 0x80) {
2690                                                         char* addString = NULL;
2691                                                         unique_ptr<char*, void(*)(char**)> addStringbuf(&addString, unique_ptr_deleter);
2692                                                         wspHeaderDecodeChallenge(fieldValueLen, fieldValue, &addString);
2693                                                         strncat((char*)temper, addString, (WSP_STANDARD_STR_LEN_MAX * 5)-AcStrlen((char*)temper)-1);
2694                                                 } else {
2695                                                         unsigned char  authScheme[WSP_STANDARD_STR_LEN_MAX + 1];
2696                                                         unsigned char  realmValue[WSP_STANDARD_STR_LEN_MAX];
2697                                                         unsigned char  addedString[WSP_STANDARD_STR_LEN_MAX];
2698
2699                                                         strncpy((char*)authScheme, (char*)fieldValue, WSP_STANDARD_STR_LEN_MAX -1);
2700                                                         iField = AcStrlen((char*)authScheme) + 1;
2701                                                         strncpy((char*)realmValue, (char*)(fieldValue + iField), WSP_STANDARD_STR_LEN_MAX-1);
2702                                                         iField = iField + AcStrlen((char*)realmValue) + 1;
2703                                                         int wrn = snprintf((char*)addedString, sizeof(addedString), "%s %s", authScheme, realmValue);
2704                                                         if(wrn<0)
2705                                                                 MSG_DEBUG("snprintf was failed");
2706                                                         wspHeaderCopyDecodedString(addedString, &currentLength, &temper);
2707
2708                                                         if (iField < fieldValueLen) {
2709                                                                 char* param = NULL;
2710                                                                 unique_ptr<char*, void(*)(char**)> parambuf(&param, unique_ptr_deleter);
2711                                                                 wspHeaderDecodeParameter(fieldValue + iField, fieldValueLen - iField, &param);
2712                                                                 if (param != NULL) {
2713                                                                         strncat((char*)temper, ", ", (WSP_STANDARD_STR_LEN_MAX * 5)-AcStrlen((char*)temper)-1);
2714                                                                         wspHeaderCopyDecodedString((unsigned char*)param, &currentLength, &temper);
2715                                                                 }
2716                                                         }
2717                                                 }
2718                                                 break;
2719
2720                                         /* content -range */
2721                                         case 0x10: {
2722                                                 unsigned long  first, len, last;
2723
2724                                                 unsigned char  temp[16];
2725                                                 iField = 0;
2726
2727                                                 strncat((char*)temper, " bytes ", (WSP_STANDARD_STR_LEN_MAX * 5)-AcStrlen((char*)temper)-1);
2728
2729                                                 first = wspRetriveUintvarDecode(fieldValue, &iField);
2730                                                 len = wspRetriveUintvarDecode(fieldValue, &iField);
2731                                                 /* Originally range of HTTP include entity length. But WSP omit it. Finally to calculate this, it should be get content length from export. If this field is included without content length, then it can get wrong result. This content length can be get by calculating PDU length.
2732                                                 */
2733                                                 last = first + contentsLength - 1;
2734
2735                                                 snprintf((char*)temp, sizeof(temp), "%u-%u/%u", (unsigned int)first, (unsigned int)last, (unsigned int)len);
2736                                                 strncat((char*)temper, (char*)temp, (WSP_STANDARD_STR_LEN_MAX * 5)-AcStrlen((char*)temper)-1);
2737                                         }
2738                                         break;
2739
2740                                         /* cache-control */
2741                                         case 0x08: {
2742                                                 char* cacheString = NULL;
2743                                                 unique_ptr<char*, void(*)(char**)> cacheStringbuf(&cacheString, unique_ptr_deleter);
2744
2745                                                 wspHeaderDecodeCacheControl(fieldValue, fieldValueLen, &cacheString);
2746                                                 strncat((char*)temper, (char*)cacheString, (WSP_STANDARD_STR_LEN_MAX * 5)-AcStrlen((char*)temper)-1);
2747                                         }
2748                                         break;
2749
2750                                         /* pragma */
2751                                         case 0x1f:
2752                                                 if (fieldValue[0] == 0x80) {
2753                                                         strncat((char*)temper, (char*)wspCacheControl[0], (WSP_STANDARD_STR_LEN_MAX * 5)-AcStrlen((char*)temper)-1);
2754                                                 } else {
2755                                                         if (1 < fieldValueLen) {
2756                                                                 char* param = NULL;
2757                                                                 unique_ptr<char*, void(*)(char**)> parambuf(&param, unique_ptr_deleter);
2758                                                                 wspHeaderDecodeParameter(fieldValue, fieldValueLen, &param);
2759
2760                                                                 if (param != NULL) {
2761                                                                         strncat((char*)temper, "; ", (WSP_STANDARD_STR_LEN_MAX * 5)-AcStrlen((char*)temper)-1);
2762                                                                         strncat((char*)temper, (char*)param, (WSP_STANDARD_STR_LEN_MAX * 5)-AcStrlen((char*)temper)-1);
2763                                                                 }
2764                                                         }
2765                                                 }
2766
2767                                                 break;
2768                                         /* public */
2769                                         case 0x22:
2770                                         /* allow */
2771                                         case 0x06: {
2772                                                 unsigned long  i = 0;
2773                                                 while (wspHeaderDecodeIntegerByLength(fieldValue, fieldValueLen) != wspMethodType[i].methodCode) i++;
2774                                                 strncat((char*)temper, (char*)wspMethodType[i].methodName, (WSP_STANDARD_STR_LEN_MAX * 5)-AcStrlen((char*)temper)-1);
2775                                         }
2776                                         break;
2777                                         /* range */
2778                                         case 0x23:
2779                                                 strncat((char*)temper, "bytes=", (WSP_STANDARD_STR_LEN_MAX * 5)-AcStrlen((char*)temper)-1);
2780                                                 if (fieldValue[0] == 0x80) {
2781                                                         unsigned char temp[16];
2782                                                         unsigned long  first, last;
2783                                                         iField = 0;
2784
2785                                                         first = wspRetriveUintvarDecode(fieldValue, &iField);
2786                                                         last = wspRetriveUintvarDecode(fieldValue, &iField);
2787
2788                                                         snprintf((char*)temp, sizeof(temp), "%u-%u", (unsigned int)first, (unsigned int)last);
2789                                                         strncat((char*)temper, (char*)temp, (WSP_STANDARD_STR_LEN_MAX * 5)-AcStrlen((char*)temper)-1);
2790                                                 }
2791                                                 if (fieldValue[0] == 0x81) {
2792                                                         unsigned char temp[16];
2793                                                         unsigned long  suffix;
2794
2795                                                         suffix = wspRetriveUintvarDecode(fieldValue, &iField);
2796
2797                                                         snprintf((char*)temp, sizeof(temp), "-%u", (unsigned int)suffix);
2798                                                 }
2799                                                 break;
2800                                         /* retry-after */
2801                                         case 0x25:
2802                                                 if (fieldValue[0] == 0x80) {
2803                                                         char* temp = NULL;
2804                                                         unique_ptr<char*, void(*)(char**)> tempbuf(&temp, unique_ptr_deleter);
2805
2806                                                         wspHeaderDecodeDateValue(fieldValueLen - 1, fieldValue + 1, &temp);
2807                                                         strncat((char*)temper, (char*)temp, (WSP_STANDARD_STR_LEN_MAX * 5)-AcStrlen((char*)temper)-1);
2808                                                 }
2809
2810                                                 if (fieldValue[0] == 0x81) {
2811                                                         unsigned char temp[16];
2812
2813                                                         snprintf((char*)temp, 16, "%u", (unsigned int)wspHeaderDecodeIntegerByLength(fieldValue, fieldValueLen));
2814                                                         strncat((char*)temper, (char*)temp, (WSP_STANDARD_STR_LEN_MAX * 5)-AcStrlen((char*)temper)-1);
2815                                                 }
2816                                                 break;
2817                                         /* transfer-encoding */
2818                                         case 0x27:
2819                                                 /* No other cases allowed */
2820                                                 if (fieldValue[0] == 0x80)
2821                                                         strncat((char*)temper, "chunked", (WSP_STANDARD_STR_LEN_MAX * 5)-AcStrlen((char*)temper)-1);
2822
2823                                                 break;
2824                                         /* vary */
2825                                         case 0x2a: {
2826                                                 int integerValue = wspHeaderDecodeIntegerByLength(fieldValue, fieldValueLen);
2827                                                 if (integerValue > wspHeaderFieldCount) {
2828                                                         MSG_DEBUG("WspLDecodeHeader: integerValue is over limit(0x%x).\n", wspHeaderFieldCount);
2829                                                         break;
2830                                                 }
2831                                                 strncat((char*)temper, (char*)wspHeaderFieldName[integerValue], (WSP_STANDARD_STR_LEN_MAX * 5)-AcStrlen((char*)temper)-1);
2832                                         }
2833                                         break;
2834                                         /* warning */
2835                                         case 0x2c: {
2836                                                 unsigned char temp[WSP_STANDARD_STR_LEN_MAX];
2837
2838                                                 if (fieldValue[0] < 0x20)
2839                                                         iField = fieldValue[0];
2840                                                 else
2841                                                         iField = 1;
2842
2843                                                 snprintf((char*)temp, sizeof(temp), "%u", (unsigned int)wspHeaderDecodeIntegerByLength(fieldValue, iField));
2844                                                 strncat((char*)temper, (char*)temp, (WSP_STANDARD_STR_LEN_MAX * 5)-AcStrlen((char*)temper)-1);
2845                                                 if (iField < fieldValueLen) {
2846                                                         unsigned char agent[WSP_STANDARD_STR_LEN_MAX];
2847                                                         unsigned char text[WSP_STANDARD_STR_LEN_MAX];
2848                                                         strncpy((char*)agent, (char*)(fieldValue + iField), WSP_STANDARD_STR_LEN_MAX-1);
2849                                                         iField = iField + AcStrlen((char*)agent) + 1;
2850                                                         strncpy((char*)text, (char*)(fieldValue + iField), WSP_STANDARD_STR_LEN_MAX-1);
2851                                                         int wrn = snprintf((char*)temp, sizeof(temp), " %s %s", agent, text);
2852                                                         if(wrn<0)
2853                                                                 MSG_DEBUG("snprintf was failed");
2854                                                         wspHeaderCopyDecodedString(temp, &currentLength, &temper);
2855                                                 }
2856                                         }
2857                                         break;
2858                                         /* content-disposition */
2859                                         case 0x2e:
2860                                                 if (fieldValue[0] == 0x80)
2861                                                         strncat((char*)temper, "form-data", (WSP_STANDARD_STR_LEN_MAX * 5)-AcStrlen((char*)temper)-1);
2862
2863                                                 if (fieldValue[0] == 0x81)
2864                                                         strncat((char*)temper, "attachment", (WSP_STANDARD_STR_LEN_MAX * 5)-AcStrlen((char*)temper)-1);
2865
2866                                                 if (1 < fieldValueLen) {
2867                                                         char* param = NULL;
2868                                                         unique_ptr<char*, void(*)(char**)> parambuf(&param, unique_ptr_deleter);
2869                                                         wspHeaderDecodeParameter(fieldValue + 1, fieldValueLen - 1, &param);
2870
2871                                                         if (param != NULL) {
2872                                                                 strncat((char*)temper, "; ", (WSP_STANDARD_STR_LEN_MAX * 5)-AcStrlen((char*)temper)-1);
2873                                                                 strncat((char*)temper, (char*)param, (WSP_STANDARD_STR_LEN_MAX * 5)-AcStrlen((char*)temper)-1);
2874                                                         }
2875                                                 }
2876                                                 break;
2877                                         /* Profile-diff */
2878                                         case 0x36:
2879                                                 temper[AcStrlen((char*)temper) + fieldValueLen] = '\0';
2880                                                 memcpy(temper, fieldValue, (size_t)fieldValueLen);
2881                                                 break;
2882                                         /* Profile-Warning */
2883                                         case 0x37: {
2884                                                 unsigned char temp[WSP_STANDARD_STR_LEN_MAX];
2885
2886                                                 snprintf((char*)temp, sizeof(temp), "%lX", wspHeaderDecodeInteger(fieldValue));
2887                                                 temp[2] = temp[1];
2888                                                 temp[1] = (unsigned char)0x30;
2889                                                 temp[3] = '\0';
2890                                                 if (fieldValueLen > 1) {
2891                                                         /* copy warn-target - URI */
2892                                                         strncat((char*)temp, (char*)(fieldValue + 1), WSP_STANDARD_STR_LEN_MAX-AcStrlen((char*)temp)-1);
2893                                                         if (fieldValueLen > (AcStrlen((char*)(fieldValue + 1)) + 1)) {
2894                                                                 /* copy warn-date */
2895                                                                 char* decodedString = NULL;
2896                                                                 unique_ptr<char*, void(*)(char**)> decodedStringbuf(&decodedString, unique_ptr_deleter);
2897                                                                 wspHeaderDecodeDateValue(fieldValueLen - (AcStrlen((char*)(fieldValue + 1)) + 2), fieldValue + AcStrlen((char*)(fieldValue + 1)) + 1, &decodedString);
2898                                                                 strncat((char*)temp, (char*)decodedString, WSP_STANDARD_STR_LEN_MAX-AcStrlen((char*)temp)-1);
2899                                                         }
2900                                                 }
2901                                                 strncat((char*)temper, (char*)temp, (WSP_STANDARD_STR_LEN_MAX * 5)-AcStrlen((char*)temper)-1);
2902                                         }
2903                                         break;
2904
2905                                         default:
2906                                                 break;
2907                                 }
2908                         /*}*/
2909                 }
2910                 /* It deosn't finished decoding yet. */
2911                 if ((iEncodedHeader < encodedHeaderLen) && (fieldCode != 0xff)) {
2912                         /* In here, iEncodedHeader already point next field code to be decoded. */
2913                         /* If this code is same, then set continueField else add CRLF. */
2914                         if (fieldCode == (encodedHeader[iEncodedHeader] & 0x7f)) {
2915                                 strncat((char*)temper, ", ", (WSP_STANDARD_STR_LEN_MAX * 5)-AcStrlen((char*)temper)-1);
2916                                 continueField = TRUE;
2917                         } else {
2918                                 strncat((char*)temper, "\r\n", (WSP_STANDARD_STR_LEN_MAX * 5)-AcStrlen((char*)temper)-1);
2919                                 continueField = FALSE;
2920                         }
2921                 } else {
2922                         strncat((char*)temper, "\r\n", (WSP_STANDARD_STR_LEN_MAX * 5)-AcStrlen((char*)temper)-1);
2923                 }
2924
2925                 /* add single header to total headers */
2926                 strncat((char*)outTemper, (char*)temper, (WSP_STANDARD_STR_LEN_MAX * 5)-AcStrlen((char*)outTemper)-1);
2927                 MSG_DEBUG("WspLDecodeHeader: Single Header : %s\r\n", temper);
2928         }
2929
2930         MSG_DEBUG("WspLDecodeHeader: Header decoding ended.\n");
2931
2932         *pHeader = outTemper;
2933
2934         return;
2935 }
2936
2937
2938 unsigned long Sms3gppWapPushHandler::wspHeaderDecodeInteger(unsigned char* data)
2939 {
2940         /* we only can handle max 32bit integer */
2941         unsigned long i;
2942
2943         union {
2944                 unsigned long integer;
2945                 unsigned char seg[4];
2946         } returner;
2947
2948         returner.integer = 0;
2949
2950         if (data[0] < 0x80) {
2951                 unsigned long IntLen = 0;
2952
2953                 IntLen = (data[0] > 0x04) ? 0x04:data[0];
2954
2955                 MSG_DEBUG("WspLHeaderDecodeInteger: input %d , length %lu\n", data[0], IntLen);
2956
2957                 for (i = 0; i < IntLen; i++)
2958                         returner.seg[IntLen-(i+1)] = data[i+1];
2959
2960                 return returner.integer;
2961         }
2962
2963         return data[0] & 0x7f;
2964 }
2965
2966
2967 void Sms3gppWapPushHandler::wspHeaderDecodeQValue(unsigned long length, unsigned char* data, char** pDecodedString)
2968 {
2969         unsigned short qBase = 0;
2970         float  qValue;
2971
2972         *pDecodedString = new char[WSP_STANDARD_STR_LEN_MAX];
2973         if (*pDecodedString == NULL) {
2974                 MSG_DEBUG("WspLHeaderDecodeQValue:MemAlloc failed\n");
2975                 return;
2976         }
2977
2978         memcpy(&qBase, data, (size_t)length);
2979         qValue = (float)qBase;
2980         if (qValue > 100) {
2981                 qValue = qValue - 100;
2982                 qValue = qValue / 1000;
2983                 snprintf((char*)*pDecodedString, sizeof(char)*WSP_STANDARD_STR_LEN_MAX, "; q=%.3f", qValue);
2984         } else {
2985                 /* qValue variable is divided by 100. And it's multiplied by 100.
2986                    It's to resolve problem of changed 0.01 of qValue. */
2987                 unsigned long qValueTemp;
2988                 qValue = qValue - 1;
2989                 qValue = qValue / 100;
2990                 qValueTemp = (unsigned long)(qValue * 100);
2991                 if (0 == (qValueTemp % 10))
2992                         snprintf((char*)*pDecodedString, sizeof(char)*WSP_STANDARD_STR_LEN_MAX, "; q=%.1f", qValue);
2993                 else
2994                         snprintf((char*)*pDecodedString, sizeof(char)*WSP_STANDARD_STR_LEN_MAX, "; q=%.2f", qValue);
2995         }
2996         return;
2997 }
2998
2999
3000 unsigned long Sms3gppWapPushHandler::wspHeaderDecodeIntegerByLength(unsigned char* data, unsigned long length)
3001 {
3002         unsigned long i;
3003
3004         union {
3005                 unsigned long integer;
3006                 unsigned short  seg[4];
3007         } returner;
3008
3009         returner.integer = 0;
3010
3011         if (length == 1)
3012                 return data[0] & 0x7f;
3013
3014         returner.integer = 0;
3015
3016         for (i = 0 ; i < length; i++) {
3017                 returner.integer  =  returner.integer + (data[i]  *  (0x1  << ((length - (i + 1)) * 8)));
3018                 MSG_DEBUG("WspLHeaderDecodeIntegerByLength: %lu \n", returner.integer);
3019         }
3020
3021         return returner.integer;
3022 }
3023
3024
3025 char* Sms3gppWapPushHandler::wspExtendedDecodeType(char contentType)
3026 {
3027         int i = 0;
3028
3029         while (wspExtendedContentsType[i].contentsTypeCode != contentType) {
3030                 if (wspExtendedContentsType[i].contentsTypeCode == 0xff)
3031                         return NULL;
3032                 i++;
3033         }
3034
3035         return (char*)wspExtendedContentsType[i].contentsTypeName;
3036 }
3037
3038
3039 void Sms3gppWapPushHandler::wspHeaderDecodeParameter(unsigned char* data, unsigned long length, char** pParam)
3040 {
3041         char* param = *pParam;
3042
3043         unsigned long SecurityTypeCode;
3044         unsigned long  i = 0;
3045
3046         if (data[0] < 0x80) {
3047                 /* unknown parameter type */
3048                 param = new char[WSP_STANDARD_STR_LEN_MAX];
3049
3050                 if (param == NULL) {
3051                         MSG_DEBUG("WspLHeaderDecodeParameter:MemAlloc failed\n");
3052                         return;
3053                 }
3054
3055                 strncpy((char*)param, (char*)data, WSP_STANDARD_STR_LEN_MAX - 1);
3056
3057                 if (NO_VALUE == data[AcStrlen((char*)param) + 1]) {
3058                         *pParam = param;
3059                         return;
3060                 }
3061
3062                 strncat((char*)param, "=", WSP_STANDARD_STR_LEN_MAX-AcStrlen((char*)param)-1);
3063                 strncat((char*)param, (char*)(data + strlen((char*)param)), WSP_STANDARD_STR_LEN_MAX-strlen((char*)param)-1);
3064
3065                 *pParam = param;
3066
3067                 return;
3068         }
3069
3070         switch (data[0] & 0x7f) {
3071         case 0x00:
3072                 wspHeaderDecodeQValue(length - 1, data + 1, &param);
3073                 break;
3074         case 0x01:
3075                 wspHeaderDecodeCharset(length - 1 , data + 1, &param);
3076                 break;
3077         case 0x02:
3078                 wspHeaderDecodeVersion(length - 1, data + 1, &param);
3079                 break;
3080                 /* integer */
3081         case 0x03:
3082                 /* param = (unsigned char *)malloc((size_t)WSP_STANDARD_STR_LEN_MAX); */
3083                 param = new char[WSP_STANDARD_STR_LEN_MAX];
3084                 if (param == NULL) {
3085                         MSG_DEBUG("WspLHeaderDecodeParameter: 0x03 MemAlloc failed\n");
3086                         return;
3087                 } else {
3088                         snprintf((char*)param, sizeof(char)*WSP_STANDARD_STR_LEN_MAX, "Type=%i", (int)wspHeaderDecodeInteger(data + 1));
3089                 }
3090                 break;
3091         case 0x08:
3092                 param = new char[WSP_STANDARD_STR_LEN_MAX];
3093
3094                 if (param == NULL) {
3095                         MSG_DEBUG("WspLHeaderDecodeParameter:0x08 MemAlloc failed\n");
3096                         return;
3097                 } else {
3098                         snprintf((char*)param, sizeof(char)*WSP_STANDARD_STR_LEN_MAX, "Padding=%i", (int)wspHeaderDecodeInteger(data + 1));
3099                 }
3100                 break;
3101         case 0x05:
3102                 param = new char[WSP_STANDARD_STR_LEN_MAX];
3103
3104                 if (param == NULL) {
3105                         MSG_DEBUG("WspLHeaderDecodeParameter:0x05 MemAlloc failed\n");
3106                         return;
3107                 } else {
3108                         strncpy((char*)param, "Name=", WSP_STANDARD_STR_LEN_MAX-1);
3109                         memcpy(param + 5, data + 1, length - 1);
3110                         param[5 + length - 1] = '\0';
3111                 }
3112                 break;
3113         case 0x06:
3114                 param = new char[WSP_STANDARD_STR_LEN_MAX];
3115
3116                 if (param == NULL) {
3117                         MSG_DEBUG("WspLHeaderDecodeParameter:0x06 MemAlloc failed\n");
3118                         return;
3119                 } else {
3120                         strncpy((char*)param, "Filename=", WSP_STANDARD_STR_LEN_MAX-1);
3121                         memcpy(param + 9, (char*)(data + 1), (size_t)(length - 1));
3122                         param[9 + length - 1] = '\0';
3123                 }
3124                 break;
3125         case 0x07:
3126                 param = NULL;
3127                 /* TBI */
3128                 break;
3129                 /*OMA Provisioning*/
3130         case 0x11:
3131                 param = new char[WSP_STANDARD_STR_LEN_MAX];
3132
3133                 if (param == NULL) {
3134                         MSG_DEBUG("WspLHeaderDecodeParameter:0x11 MemAlloc failed\n");
3135                         return;
3136                 } else {
3137                         strncpy((char*)param, "SEC=", WSP_STANDARD_STR_LEN_MAX-1);
3138                         SecurityTypeCode = data[1] & 0x7f;
3139                         while ((i < wspSecurityTypeCount) && (wspSecurityType[i].SecurityTypeCode != SecurityTypeCode)) i++;
3140
3141                         if (i < wspSecurityTypeCount) {
3142                                 strncat((char*)param, (char*)wspSecurityType[i].SecurityTypeName, WSP_STANDARD_STR_LEN_MAX-AcStrlen((char*)param)-1);
3143                         }
3144
3145                         if (0x12 == (data[2] & 0x7f)) {
3146                                 strncat((char*)param, "; MAC=", WSP_STANDARD_STR_LEN_MAX-AcStrlen((char*)param)-1);
3147                                 memcpy(param+AcStrlen((char*)param), (char*)(data+3), (size_t)length-3);
3148                         }
3149                 }
3150                 break;
3151         default:
3152                 param = NULL;
3153                 break;
3154         }
3155
3156         *pParam = param;
3157         return;
3158 }
3159
3160
3161 void Sms3gppWapPushHandler::wspHeaderDecodeCharset(unsigned long length, unsigned char* data, char**pDecodedString)
3162 {
3163         char* param = NULL;
3164
3165         param = new char[WSP_STANDARD_STR_LEN_MAX];
3166         if (param == NULL) {
3167                 MSG_DEBUG("WspLHeaderDecodeCharset:MemAlloc failed\n");
3168                 *pDecodedString = NULL;
3169                 return;
3170         }
3171
3172         strncpy((char*)param, "charset=", WSP_STANDARD_STR_LEN_MAX-1);
3173
3174         if (data[0] > 0x80) {
3175                 unsigned long code = wspHeaderDecodeInteger(data);
3176                 unsigned long i = 0;
3177                 while (wspCharset[i].charsetCode !=  code) {
3178                         if (wspCharset[i].charsetCode == 0xffff) {
3179                                 *pDecodedString = param;
3180                                 return;
3181                         }
3182                         i++;
3183                 }
3184                 strncat((char*)param, (char*)wspCharset[i].charsetName, WSP_STANDARD_STR_LEN_MAX-AcStrlen((char*)param)-1);
3185         } else {
3186                 unsigned long lastLen = AcStrlen((char*)param);
3187                 if (length + lastLen < WSP_STANDARD_STR_LEN_MAX - 1) {
3188                         memcpy((char*)(param + lastLen), data, (size_t)length);
3189                         param[length + lastLen] = '\0';
3190                 } else {
3191                         memcpy((char*)(param + lastLen), data, WSP_STANDARD_STR_LEN_MAX - lastLen - 1);
3192                         param[WSP_STANDARD_STR_LEN_MAX-1] = '\0';
3193                 }
3194         }
3195
3196         *pDecodedString = param;
3197         return;
3198 }
3199
3200
3201 void Sms3gppWapPushHandler::wspHeaderDecodeVersion(unsigned long length, unsigned char* data, char** pDecodedString)
3202 {
3203         *pDecodedString = new char[WSP_STANDARD_STR_LEN_MAX];
3204
3205         if (*pDecodedString == NULL) {
3206                 MSG_DEBUG("WspLHeaderDecodeVersion:MemAlloc failed\n");
3207                 return;
3208         }
3209
3210         if (length > 1) {
3211                 /* untyped version */
3212                 memcpy(*pDecodedString, data, (size_t)length);
3213         } else {
3214                 /* typed version */
3215                 unsigned char majorVer  = ((data[0] & 0x7f) >> 4);
3216                 unsigned char minorVer = data[0] & 0x0f;
3217                 snprintf((char*)*pDecodedString, sizeof(char)*WSP_STANDARD_STR_LEN_MAX, "level=%u.%u", majorVer, minorVer);
3218         }
3219
3220         return;
3221 }
3222
3223
3224 void Sms3gppWapPushHandler::wspHeaderDecodeDateValue(unsigned long length, unsigned char* data, char** pDecodedString)
3225 {
3226         time_t  lTime;
3227         struct  tm pTMData;
3228
3229         MSG_DEBUG("WspLHeaderDecodeDateValue:   \n");
3230
3231         *pDecodedString = new char[WSP_STANDARD_STR_LEN_MAX];
3232
3233         if (*pDecodedString == NULL) {
3234                 MSG_DEBUG("WspLHeaderDecodeDateValue:MemAlloc failed\n");
3235                 return;
3236         }
3237
3238         lTime = wspHeaderDecodeIntegerByLength(data, length);
3239
3240         (struct tm*)gmtime_r((const time_t*)&lTime, &pTMData);
3241
3242         /* check date value validity */
3243         if ((pTMData.tm_wday > 6) || (pTMData.tm_mon > 11) || (pTMData.tm_mday > 31)) {
3244                 MSG_DEBUG("WspLHeaderDecodeDateValue: Date decode fail %d, %d, %d \n", pTMData.tm_wday, pTMData.tm_mon, pTMData.tm_mday);
3245                 strncpy((char*)*pDecodedString, "Decoding Failed", WSP_STANDARD_STR_LEN_MAX-1);
3246                 return;
3247         }
3248
3249 #ifdef MSG_FW_FOR_DEBUG
3250         /** Date type selection */
3251         switch (wspMachineStatus.dateType) {
3252                         /* UNIX asciitime function */
3253                 case UNIX_DATE_TYPE:
3254                         snprintf((char*)decodedString, sizeof(decodedString), "%s %s %-2u %u:%u:%u %u GMT", wspWeek[pTMData.tm_wday], wspMonth[pTMData.tm_mon],
3255                                            pTMData.tm_mday, pTMData.tm_hour, pTMData.tm_min, pTMData.tm_sec, pTMData.tm_year + 1900);
3256                         break;
3257                 case RFC1123_DATE_TYPE:
3258                         snprintf((char*)decodedString, sizeof(decodedString), "%s, %u %s %u %u:%u:%u GMT", wspWeek[pTMData.tm_wday], pTMData.tm_mday,
3259                                            wspMonth[pTMData.tm_mon], pTMData.tm_year + 1900, pTMData.tm_hour, pTMData.tm_min, pTMData.tm_sec);
3260                         break;
3261                 case RFC850_DATE_TYPE:
3262                         /* Have some Y2K Problems */
3263                         /* In RFC 850, date is represented like 11-May-99. So Y2K problem always can be occured. So remainer (year divided by 100) is used.                     */
3264                         snprintf((char*)decodedString, sizeof(decodedString), "%s, %2u-%s-%2u %u:%u:%u GMT", wspWeekDay[pTMData.tm_wday], pTMData.tm_mday,
3265                                            wspMonth[pTMData.tm_mon], pTMData.tm_year % CENTURY, pTMData.tm_hour, pTMData.tm_min, pTMData.tm_sec);
3266
3267                         break;
3268         }
3269 #endif
3270         /**UNIX_DATE_TYPE : */
3271         snprintf((char*)*pDecodedString, (sizeof(char)*WSP_STANDARD_STR_LEN_MAX), "%s %s %-2u %u:%u:%u %u GMT", wspWeek[pTMData.tm_wday], wspMonth[pTMData.tm_mon],
3272                                                                                         pTMData.tm_mday, pTMData.tm_hour, pTMData.tm_min, pTMData.tm_sec, pTMData.tm_year + 1900);
3273
3274         return;
3275 }
3276
3277 void Sms3gppWapPushHandler::wspHeaderCopyDecodedString(unsigned char* szDecodedString, unsigned long* currentLen, char** pTemper)
3278 {
3279         unsigned long elementLen = AcStrlen((char*)szDecodedString);
3280         char* temper2 = NULL;
3281
3282         /** CR+LF */
3283         *currentLen = *currentLen + elementLen + 2;
3284
3285         if (*currentLen > AcStrlen((char*)* pTemper) + 2) {
3286                 temper2 = new char[(*currentLen + 1)];
3287
3288                 if (temper2 == NULL) {
3289                         MSG_DEBUG("WspLHeaderCopyDecodedString:MemAlloc failed\n");
3290                         return;
3291                 }
3292                 strncpy((char*)temper2, (char*)* pTemper, *currentLen);
3293                 delete[] *pTemper;
3294                 strncpy((char*)temper2, (char*)szDecodedString, *currentLen);
3295         }
3296
3297         *pTemper = temper2;
3298
3299         return;
3300 }
3301
3302
3303 void Sms3gppWapPushHandler::wspHeaderDecodeAuth(unsigned long fieldValueLen, unsigned char* fieldValue, char** pDecodedString)
3304 {
3305         unsigned char  userId[WSP_STANDARD_STR_LEN_MAX];
3306         unsigned char  passWd[WSP_STANDARD_STR_LEN_MAX];
3307         unsigned long iField = 0;
3308         char authStr[256];
3309
3310         *pDecodedString = new char[WSP_STANDARD_STR_LEN_MAX * 2];
3311
3312         if (*pDecodedString == NULL) {
3313                 MSG_DEBUG("WspLHeaderDecodeAuth:MemAlloc failed\n");
3314                 return;
3315         }
3316
3317         /* skip 'basic' code */
3318         iField++;
3319         memset(authStr, 0x00, sizeof(authStr));
3320         snprintf(authStr, sizeof(authStr), "%%%zus", sizeof(userId));
3321         sscanf((char*)(fieldValue + iField), authStr, userId);
3322         iField = iField + AcStrlen((char*)userId) + 1;
3323         memset(authStr, 0x00, sizeof(authStr));
3324         snprintf(authStr, sizeof(authStr), "%%%zus", sizeof(passWd));
3325         sscanf((char*)(fieldValue + iField), authStr, passWd);
3326         iField = iField + AcStrlen((char*)userId) + 1;
3327         int wrn = snprintf((char*)*pDecodedString, (sizeof(char)*WSP_STANDARD_STR_LEN_MAX*2), "basic %s/%s", userId, passWd);
3328         if(wrn<0)
3329                 MSG_DEBUG("snprintf was  failed\n");
3330
3331         return;
3332 }
3333
3334
3335 void Sms3gppWapPushHandler::wspHeaderDecodeChallenge(unsigned long fieldValueLen, unsigned char* fieldValue, char** pDecodedString)
3336 {
3337         unsigned char userId[WSP_STANDARD_STR_LEN_MAX];
3338         unsigned long iField = 0;
3339         char authStr[256];
3340
3341         *pDecodedString = new char[WSP_STANDARD_STR_LEN_MAX];
3342
3343         if (*pDecodedString == NULL) {
3344                 MSG_DEBUG("WspLHeaderDecodeChallenge:MemAlloc failed\n");
3345                 return;
3346         }
3347
3348         /* skip 'basic' code */
3349         iField++;
3350         memset(authStr, 0x00, sizeof(authStr));
3351         snprintf(authStr, sizeof(authStr), "%%%zus", sizeof(userId));
3352         sscanf((char*)(fieldValue + iField), authStr, userId);
3353         iField = iField + AcStrlen((char*)userId) + 1;
3354
3355         int wrn = snprintf((char*)*pDecodedString, (sizeof(char)*WSP_STANDARD_STR_LEN_MAX), "basic realm=\"%s\"", userId);
3356         if(wrn<0)
3357                 MSG_DEBUG("snprintf was  failed\n");
3358
3359         return;
3360 }
3361
3362
3363 void Sms3gppWapPushHandler::wspHeaderDecodeCacheControl(unsigned char* fieldValue, unsigned long fieldValueLen, char** pCacheString)
3364 {
3365         unsigned char  paramString[WSP_STANDARD_STR_LEN_MAX];
3366         unsigned char  cacheCode;
3367         int wrn =0;
3368
3369         *pCacheString = new char[WSP_STANDARD_STR_LEN_MAX];
3370         if (*pCacheString == NULL) {
3371                 MSG_DEBUG("WspLHeaderDecodeCacheControl:MemAlloc failed\n");
3372                 return;
3373         }
3374
3375         if (1 == fieldValueLen) {
3376                 /* only one directive */
3377                 if (fieldValue[0] > 0x8b) {
3378                         return; /* It's error detection. can be omitted. */
3379                 }
3380                 strncpy((char*)*pCacheString, (char*)wspCacheControl[fieldValue[0] & 0x7f], WSP_STANDARD_STR_LEN_MAX-1);
3381                 return;
3382         }
3383
3384         if (fieldValue[0] > 0x7f) {
3385                 /* directive that has parameter */
3386                 cacheCode = fieldValue[0] & 0x7f;
3387                 switch (cacheCode) {
3388                         /* field name */
3389                         /* no-cache */
3390                 case 0x00 :
3391                         /* private */
3392                 case 0x07 :
3393                         if (fieldValue[1] > 0x7f) {
3394                                 /* well known field name */
3395                                 strncpy((char*)paramString, (char*)wspHeaderFieldName[fieldValue[1] & 0x7f], WSP_STANDARD_STR_LEN_MAX-1);
3396                                 paramString[WSP_STANDARD_STR_LEN_MAX-1] = '\0';
3397                         } else {
3398                                 /* unknown field name */
3399                                 strncpy((char*)paramString, (char*)fieldValue + 1 , WSP_STANDARD_STR_LEN_MAX-1);
3400                         }
3401                         break;
3402                         /* secound */
3403                         /* max-age */
3404                 case 0x02 :
3405                         /* max- stale */
3406                 case 0x03 :
3407                         /* min-fresh */
3408                 case 0x04 :
3409                         snprintf((char*)paramString, sizeof(paramString), "%u", (unsigned int)wspHeaderDecodeInteger(fieldValue + 1));
3410                         break;
3411
3412                 default :
3413                         break;
3414                 }
3415                 wrn = snprintf((char*)*pCacheString, (sizeof(char)*WSP_STANDARD_STR_LEN_MAX), "%s=%s", (char*)wspCacheControl[cacheCode], (char*)paramString);
3416                 if(wrn<0)
3417                         MSG_DEBUG("snprintf was failed");
3418         } else {
3419                 /* cache extentions */
3420                 /* In case of come directive of doesn't specified string style */
3421
3422                 unsigned long stringLen;
3423                 char szString[32];
3424                 strncpy((char*)*pCacheString, (char*)fieldValue, WSP_STANDARD_STR_LEN_MAX-1);
3425                 stringLen = AcStrlen((char*)*pCacheString);
3426
3427                 if (stringLen + 1 < fieldValueLen) {
3428                         if (fieldValue[stringLen+ 1] > 0x7f) {
3429                                 int untyped = (int)wspHeaderDecodeIntegerByLength(fieldValue + stringLen + 1, fieldValueLen - (stringLen + 1));
3430
3431                                 snprintf(szString, sizeof(szString), "%d", untyped);
3432                                 strncat((char*)*pCacheString, (char*)"=", WSP_STANDARD_STR_LEN_MAX-AcStrlen((char*)*pCacheString)-1);
3433                                 strncat((char*)*pCacheString, (char*)szString, WSP_STANDARD_STR_LEN_MAX-AcStrlen((char*)*pCacheString)-1);
3434                         } else {
3435                                 if (fieldValue[fieldValueLen] == 0) {
3436                                         strncat((char*)*pCacheString, (char*)"=", WSP_STANDARD_STR_LEN_MAX-AcStrlen((char*)*pCacheString)-1);
3437                                         strncat((char*)*pCacheString, (char*)fieldValue + stringLen + 1 , WSP_STANDARD_STR_LEN_MAX-AcStrlen((char*)*pCacheString)-1);
3438                                 }
3439                         }
3440                 }
3441         }
3442
3443         return;
3444 }
3445
3446 void Sms3gppWapPushHandler::getDisplayName(MSG_SUB_TYPE_T subType, char* displayName)
3447 {
3448         if (subType == MSG_WAP_SL_SMS || subType == MSG_WAP_SI_SMS)
3449                 snprintf(displayName, MAX_ADDRESS_VAL_LEN + 1, "Push message");
3450         return;
3451 }