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