wined3d: Always use np2 repacking if no native np2 support is available.
[wine] / dlls / wininet / utility.c
1 /*
2  * Wininet - Utility functions
3  *
4  * Copyright 1999 Corel Corporation
5  * Copyright 2002 CodeWeavers Inc.
6  *
7  * Ulrich Czekalla
8  * Aric Stewart
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24
25 #include "config.h"
26 #include "wine/port.h"
27
28 #include <stdarg.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <time.h>
32
33 #include "windef.h"
34 #include "winbase.h"
35 #include "wininet.h"
36 #include "winerror.h"
37 #include "winnls.h"
38
39 #include "wine/debug.h"
40 #include "internet.h"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
43
44 #define TIME_STRING_LEN  30
45
46 time_t ConvertTimeString(LPCWSTR asctime)
47 {
48     WCHAR tmpChar[TIME_STRING_LEN];
49     WCHAR *tmpChar2;
50     struct tm t;
51     int timelen = strlenW(asctime);
52
53     if(!timelen)
54         return 0;
55
56     /* FIXME: the atoiWs below rely on that tmpChar is \0 padded */
57     memset( tmpChar, 0, sizeof(tmpChar) );
58     lstrcpynW(tmpChar, asctime, TIME_STRING_LEN);
59
60     /* Assert that the string is the expected length */
61     if (strlenW(asctime) >= TIME_STRING_LEN) FIXME("\n");
62
63     /* Convert a time such as 'Mon, 15 Nov 1999 16:09:35 GMT' into a SYSTEMTIME structure
64      * We assume the time is in this format
65      * and divide it into easy to swallow chunks
66      */
67     tmpChar[3]='\0';
68     tmpChar[7]='\0';
69     tmpChar[11]='\0';
70     tmpChar[16]='\0';
71     tmpChar[19]='\0';
72     tmpChar[22]='\0';
73     tmpChar[25]='\0';
74
75     t.tm_year = atoiW(tmpChar+12) - 1900;
76     t.tm_mday = atoiW(tmpChar+5);
77     t.tm_hour = atoiW(tmpChar+17);
78     t.tm_min = atoiW(tmpChar+20);
79     t.tm_sec = atoiW(tmpChar+23);
80
81     /* and month */
82     tmpChar2 = tmpChar + 8;
83     switch(tmpChar2[2])
84     {
85         case 'n':
86             if(tmpChar2[1]=='a')
87                 t.tm_mon = 0;
88             else
89                 t.tm_mon = 5;
90             break;
91         case 'b':
92             t.tm_mon = 1;
93             break;
94         case 'r':
95             if(tmpChar2[1]=='a')
96                 t.tm_mon = 2;
97             else
98                 t.tm_mon = 3;
99             break;
100         case 'y':
101             t.tm_mon = 4;
102             break;
103         case 'l':
104             t.tm_mon = 6;
105             break;
106         case 'g':
107             t.tm_mon = 7;
108             break;
109         case 'p':
110             t.tm_mon = 8;
111             break;
112         case 't':
113             t.tm_mon = 9;
114             break;
115         case 'v':
116             t.tm_mon = 10;
117             break;
118         case 'c':
119             t.tm_mon = 11;
120             break;
121         default:
122             FIXME("\n");
123     }
124
125     return mktime(&t);
126 }
127
128
129 BOOL GetAddress(LPCWSTR lpszServerName, INTERNET_PORT nServerPort,
130         struct sockaddr_in *psa)
131 {
132     WCHAR *found;
133     char *name;
134     int len, sz;
135     struct hostent *phe;
136
137     TRACE("%s\n", debugstr_w(lpszServerName));
138
139     /* Validate server name first
140      * Check if there is sth. like
141      * pinger.macromedia.com:80
142      * if yes, eliminate the :80....
143      */
144     found = strchrW(lpszServerName, ':');
145     if (found)
146         len = found - lpszServerName;
147     else
148         len = strlenW(lpszServerName);
149
150     sz = WideCharToMultiByte( CP_UNIXCP, 0, lpszServerName, len, NULL, 0, NULL, NULL );
151     name = HeapAlloc(GetProcessHeap(), 0, sz+1);
152     WideCharToMultiByte( CP_UNIXCP, 0, lpszServerName, len, name, sz, NULL, NULL );
153     name[sz] = 0;
154     phe = gethostbyname(name);
155     HeapFree( GetProcessHeap(), 0, name );
156
157     if (NULL == phe)
158     {
159         TRACE("Failed to get hostname: (%s)\n", debugstr_w(lpszServerName) );
160         return FALSE;
161     }
162
163     memset(psa,0,sizeof(struct sockaddr_in));
164     memcpy((char *)&psa->sin_addr, phe->h_addr, phe->h_length);
165     psa->sin_family = phe->h_addrtype;
166     psa->sin_port = htons((u_short)nServerPort);
167
168     return TRUE;
169 }
170
171 /*
172  * Helper function for sending async Callbacks
173  */
174
175 static const char *get_callback_name(DWORD dwInternetStatus) {
176     static const wininet_flag_info internet_status[] = {
177 #define FE(x) { x, #x }
178         FE(INTERNET_STATUS_RESOLVING_NAME),
179         FE(INTERNET_STATUS_NAME_RESOLVED),
180         FE(INTERNET_STATUS_CONNECTING_TO_SERVER),
181         FE(INTERNET_STATUS_CONNECTED_TO_SERVER),
182         FE(INTERNET_STATUS_SENDING_REQUEST),
183         FE(INTERNET_STATUS_REQUEST_SENT),
184         FE(INTERNET_STATUS_RECEIVING_RESPONSE),
185         FE(INTERNET_STATUS_RESPONSE_RECEIVED),
186         FE(INTERNET_STATUS_CTL_RESPONSE_RECEIVED),
187         FE(INTERNET_STATUS_PREFETCH),
188         FE(INTERNET_STATUS_CLOSING_CONNECTION),
189         FE(INTERNET_STATUS_CONNECTION_CLOSED),
190         FE(INTERNET_STATUS_HANDLE_CREATED),
191         FE(INTERNET_STATUS_HANDLE_CLOSING),
192         FE(INTERNET_STATUS_REQUEST_COMPLETE),
193         FE(INTERNET_STATUS_REDIRECT),
194         FE(INTERNET_STATUS_INTERMEDIATE_RESPONSE),
195         FE(INTERNET_STATUS_USER_INPUT_REQUIRED),
196         FE(INTERNET_STATUS_STATE_CHANGE),
197         FE(INTERNET_STATUS_COOKIE_SENT),
198         FE(INTERNET_STATUS_COOKIE_RECEIVED),
199         FE(INTERNET_STATUS_PRIVACY_IMPACTED),
200         FE(INTERNET_STATUS_P3P_HEADER),
201         FE(INTERNET_STATUS_P3P_POLICYREF),
202         FE(INTERNET_STATUS_COOKIE_HISTORY)
203 #undef FE
204     };
205     DWORD i;
206
207     for (i = 0; i < (sizeof(internet_status) / sizeof(internet_status[0])); i++) {
208         if (internet_status[i].val == dwInternetStatus) return internet_status[i].name;
209     }
210     return "Unknown";
211 }
212
213 VOID INTERNET_SendCallback(LPWININETHANDLEHEADER hdr, DWORD dwContext,
214                            DWORD dwInternetStatus, LPVOID lpvStatusInfo,
215                            DWORD dwStatusInfoLength)
216 {
217     LPVOID lpvNewInfo = NULL;
218
219     if( !hdr->lpfnStatusCB )
220         return;
221
222     /* the IE5 version of wininet does not
223        send callbacks if dwContext is zero */
224     if( !dwContext )
225         return;
226
227     lpvNewInfo = lpvStatusInfo;
228     if(hdr->dwInternalFlags & INET_CALLBACKW) {
229         switch(dwInternetStatus) {
230         case INTERNET_STATUS_NAME_RESOLVED:
231         case INTERNET_STATUS_CONNECTING_TO_SERVER:
232         case INTERNET_STATUS_CONNECTED_TO_SERVER:
233             lpvNewInfo = WININET_strdup_AtoW(lpvStatusInfo);
234         }
235     }else {
236         switch(dwInternetStatus)
237         {
238         case INTERNET_STATUS_RESOLVING_NAME:
239         case INTERNET_STATUS_REDIRECT:
240             lpvNewInfo = WININET_strdup_WtoA(lpvStatusInfo);
241         }
242     }
243     
244     TRACE(" callback(%p) (%p (%p), %08x, %d (%s), %p, %d)\n",
245           hdr->lpfnStatusCB, hdr->hInternet, hdr, dwContext, dwInternetStatus, get_callback_name(dwInternetStatus),
246           lpvNewInfo, dwStatusInfoLength);
247     
248     hdr->lpfnStatusCB(hdr->hInternet, dwContext, dwInternetStatus,
249                       lpvNewInfo, dwStatusInfoLength);
250
251     TRACE(" end callback().\n");
252
253     if(lpvNewInfo != lpvStatusInfo)
254         HeapFree(GetProcessHeap(), 0, lpvNewInfo);
255 }
256
257 static void SendAsyncCallbackProc(WORKREQUEST *workRequest)
258 {
259     struct WORKREQ_SENDCALLBACK const *req = &workRequest->u.SendCallback;
260
261     TRACE("%p\n", workRequest->hdr);
262
263     INTERNET_SendCallback(workRequest->hdr,
264                           req->dwContext, req->dwInternetStatus, req->lpvStatusInfo,
265                           req->dwStatusInfoLength);
266
267     /* And frees the copy of the status info */
268     HeapFree(GetProcessHeap(), 0, req->lpvStatusInfo);
269 }
270
271 VOID SendAsyncCallback(LPWININETHANDLEHEADER hdr, DWORD dwContext,
272                        DWORD dwInternetStatus, LPVOID lpvStatusInfo,
273                        DWORD dwStatusInfoLength)
274 {
275     TRACE("(%p, %08x, %d (%s), %p, %d): %sasync call with callback %p\n",
276           hdr, dwContext, dwInternetStatus, get_callback_name(dwInternetStatus),
277           lpvStatusInfo, dwStatusInfoLength,
278           hdr->dwFlags & INTERNET_FLAG_ASYNC ? "" : "non ",
279           hdr->lpfnStatusCB);
280     
281     if (!(hdr->lpfnStatusCB))
282         return;
283     
284     if (hdr->dwFlags & INTERNET_FLAG_ASYNC)
285     {
286         WORKREQUEST workRequest;
287         struct WORKREQ_SENDCALLBACK *req;
288         void *lpvStatusInfo_copy = lpvStatusInfo;
289
290         if (lpvStatusInfo)
291         {
292             lpvStatusInfo_copy = HeapAlloc(GetProcessHeap(), 0, dwStatusInfoLength);
293             memcpy(lpvStatusInfo_copy, lpvStatusInfo, dwStatusInfoLength);
294         }
295
296         workRequest.asyncproc = SendAsyncCallbackProc;
297         workRequest.hdr = WININET_AddRef( hdr );
298         req = &workRequest.u.SendCallback;
299         req->dwContext = dwContext;
300         req->dwInternetStatus = dwInternetStatus;
301         req->lpvStatusInfo = lpvStatusInfo_copy;
302         req->dwStatusInfoLength = dwStatusInfoLength;
303         
304         INTERNET_AsyncCall(&workRequest);
305     }
306     else
307         INTERNET_SendCallback(hdr, dwContext, dwInternetStatus,
308                               lpvStatusInfo, dwStatusInfoLength);
309 }