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