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