wined3d: Use ARB_internalformat_query2 for the other texture format caps too.
[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 #if defined(__MINGW32__) || defined (_MSC_VER)
29 #include <ws2tcpip.h>
30 #endif
31
32 #include <stdarg.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <time.h>
36
37 #include "windef.h"
38 #include "winbase.h"
39 #include "wininet.h"
40 #include "winnls.h"
41
42 #include "wine/debug.h"
43 #include "internet.h"
44
45 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
46
47 #ifndef HAVE_GETADDRINFO
48
49 /* critical section to protect non-reentrant gethostbyname() */
50 static CRITICAL_SECTION cs_gethostbyname;
51 static CRITICAL_SECTION_DEBUG critsect_debug =
52 {
53     0, 0, &cs_gethostbyname,
54     { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
55       0, 0, { (DWORD_PTR)(__FILE__ ": cs_gethostbyname") }
56 };
57 static CRITICAL_SECTION cs_gethostbyname = { &critsect_debug, -1, 0, 0, 0, 0 };
58
59 #endif
60
61 #define TIME_STRING_LEN  30
62
63 time_t ConvertTimeString(LPCWSTR asctime)
64 {
65     WCHAR tmpChar[TIME_STRING_LEN];
66     WCHAR *tmpChar2;
67     struct tm t;
68     int timelen = strlenW(asctime);
69
70     if(!timelen)
71         return 0;
72
73     /* FIXME: the atoiWs below rely on that tmpChar is \0 padded */
74     memset( tmpChar, 0, sizeof(tmpChar) );
75     lstrcpynW(tmpChar, asctime, TIME_STRING_LEN);
76
77     /* Assert that the string is the expected length */
78     if (strlenW(asctime) >= TIME_STRING_LEN) FIXME("\n");
79
80     /* Convert a time such as 'Mon, 15 Nov 1999 16:09:35 GMT' into a SYSTEMTIME structure
81      * We assume the time is in this format
82      * and divide it into easy to swallow chunks
83      */
84     tmpChar[3]='\0';
85     tmpChar[7]='\0';
86     tmpChar[11]='\0';
87     tmpChar[16]='\0';
88     tmpChar[19]='\0';
89     tmpChar[22]='\0';
90     tmpChar[25]='\0';
91
92     memset( &t, 0, sizeof(t) );
93     t.tm_year = atoiW(tmpChar+12) - 1900;
94     t.tm_mday = atoiW(tmpChar+5);
95     t.tm_hour = atoiW(tmpChar+17);
96     t.tm_min = atoiW(tmpChar+20);
97     t.tm_sec = atoiW(tmpChar+23);
98
99     /* and month */
100     tmpChar2 = tmpChar + 8;
101     switch(tmpChar2[2])
102     {
103         case 'n':
104             if(tmpChar2[1]=='a')
105                 t.tm_mon = 0;
106             else
107                 t.tm_mon = 5;
108             break;
109         case 'b':
110             t.tm_mon = 1;
111             break;
112         case 'r':
113             if(tmpChar2[1]=='a')
114                 t.tm_mon = 2;
115             else
116                 t.tm_mon = 3;
117             break;
118         case 'y':
119             t.tm_mon = 4;
120             break;
121         case 'l':
122             t.tm_mon = 6;
123             break;
124         case 'g':
125             t.tm_mon = 7;
126             break;
127         case 'p':
128             t.tm_mon = 8;
129             break;
130         case 't':
131             t.tm_mon = 9;
132             break;
133         case 'v':
134             t.tm_mon = 10;
135             break;
136         case 'c':
137             t.tm_mon = 11;
138             break;
139         default:
140             FIXME("\n");
141     }
142
143     return mktime(&t);
144 }
145
146
147 BOOL GetAddress(LPCWSTR lpszServerName, INTERNET_PORT nServerPort,
148         struct sockaddr *psa, socklen_t *sa_len)
149 {
150     WCHAR *found;
151     char *name;
152     int len, sz;
153 #ifdef HAVE_GETADDRINFO
154     struct addrinfo *res, hints;
155     int ret;
156 #else
157     struct hostent *phe;
158     struct sockaddr_in *sin = (struct sockaddr_in *)psa;
159 #endif
160
161     TRACE("%s\n", debugstr_w(lpszServerName));
162
163     /* Validate server name first
164      * Check if there is sth. like
165      * pinger.macromedia.com:80
166      * if yes, eliminate the :80....
167      */
168     found = strchrW(lpszServerName, ':');
169     if (found)
170         len = found - lpszServerName;
171     else
172         len = strlenW(lpszServerName);
173
174     sz = WideCharToMultiByte( CP_UNIXCP, 0, lpszServerName, len, NULL, 0, NULL, NULL );
175     if (!(name = heap_alloc(sz + 1))) return FALSE;
176     WideCharToMultiByte( CP_UNIXCP, 0, lpszServerName, len, name, sz, NULL, NULL );
177     name[sz] = 0;
178
179 #ifdef HAVE_GETADDRINFO
180     memset( &hints, 0, sizeof(struct addrinfo) );
181     /* Prefer IPv4 to IPv6 addresses, since some servers do not listen on
182      * their IPv6 addresses even though they have IPv6 addresses in the DNS.
183      */
184     hints.ai_family = AF_INET;
185
186     ret = getaddrinfo( name, NULL, &hints, &res );
187     if (ret != 0)
188     {
189         TRACE("failed to get IPv4 address of %s (%s), retrying with IPv6\n", debugstr_w(lpszServerName), gai_strerror(ret));
190         hints.ai_family = AF_INET6;
191         ret = getaddrinfo( name, NULL, &hints, &res );
192     }
193     heap_free( name );
194     if (ret != 0)
195     {
196         TRACE("failed to get address of %s (%s)\n", debugstr_w(lpszServerName), gai_strerror(ret));
197         return FALSE;
198     }
199     if (*sa_len < res->ai_addrlen)
200     {
201         WARN("address too small\n");
202         freeaddrinfo( res );
203         return FALSE;
204     }
205     *sa_len = res->ai_addrlen;
206     memcpy( psa, res->ai_addr, res->ai_addrlen );
207     /* Copy port */
208     switch (res->ai_family)
209     {
210     case AF_INET:
211         ((struct sockaddr_in *)psa)->sin_port = htons(nServerPort);
212         break;
213     case AF_INET6:
214         ((struct sockaddr_in6 *)psa)->sin6_port = htons(nServerPort);
215         break;
216     }
217
218     freeaddrinfo( res );
219 #else
220     EnterCriticalSection( &cs_gethostbyname );
221     phe = gethostbyname(name);
222     heap_free( name );
223
224     if (NULL == phe)
225     {
226         TRACE("failed to get address of %s (%d)\n", debugstr_w(lpszServerName), h_errno);
227         LeaveCriticalSection( &cs_gethostbyname );
228         return FALSE;
229     }
230     if (*sa_len < sizeof(struct sockaddr_in))
231     {
232         WARN("address too small\n");
233         LeaveCriticalSection( &cs_gethostbyname );
234         return FALSE;
235     }
236     *sa_len = sizeof(struct sockaddr_in);
237     memset(sin,0,sizeof(struct sockaddr_in));
238     memcpy((char *)&sin->sin_addr, phe->h_addr, phe->h_length);
239     sin->sin_family = phe->h_addrtype;
240     sin->sin_port = htons(nServerPort);
241
242     LeaveCriticalSection( &cs_gethostbyname );
243 #endif
244     return TRUE;
245 }
246
247 /*
248  * Helper function for sending async Callbacks
249  */
250
251 static const char *get_callback_name(DWORD dwInternetStatus) {
252     static const wininet_flag_info internet_status[] = {
253 #define FE(x) { x, #x }
254         FE(INTERNET_STATUS_RESOLVING_NAME),
255         FE(INTERNET_STATUS_NAME_RESOLVED),
256         FE(INTERNET_STATUS_CONNECTING_TO_SERVER),
257         FE(INTERNET_STATUS_CONNECTED_TO_SERVER),
258         FE(INTERNET_STATUS_SENDING_REQUEST),
259         FE(INTERNET_STATUS_REQUEST_SENT),
260         FE(INTERNET_STATUS_RECEIVING_RESPONSE),
261         FE(INTERNET_STATUS_RESPONSE_RECEIVED),
262         FE(INTERNET_STATUS_CTL_RESPONSE_RECEIVED),
263         FE(INTERNET_STATUS_PREFETCH),
264         FE(INTERNET_STATUS_CLOSING_CONNECTION),
265         FE(INTERNET_STATUS_CONNECTION_CLOSED),
266         FE(INTERNET_STATUS_HANDLE_CREATED),
267         FE(INTERNET_STATUS_HANDLE_CLOSING),
268         FE(INTERNET_STATUS_REQUEST_COMPLETE),
269         FE(INTERNET_STATUS_REDIRECT),
270         FE(INTERNET_STATUS_INTERMEDIATE_RESPONSE),
271         FE(INTERNET_STATUS_USER_INPUT_REQUIRED),
272         FE(INTERNET_STATUS_STATE_CHANGE),
273         FE(INTERNET_STATUS_COOKIE_SENT),
274         FE(INTERNET_STATUS_COOKIE_RECEIVED),
275         FE(INTERNET_STATUS_PRIVACY_IMPACTED),
276         FE(INTERNET_STATUS_P3P_HEADER),
277         FE(INTERNET_STATUS_P3P_POLICYREF),
278         FE(INTERNET_STATUS_COOKIE_HISTORY)
279 #undef FE
280     };
281     DWORD i;
282
283     for (i = 0; i < (sizeof(internet_status) / sizeof(internet_status[0])); i++) {
284         if (internet_status[i].val == dwInternetStatus) return internet_status[i].name;
285     }
286     return "Unknown";
287 }
288
289 static const char *debugstr_status_info(DWORD status, void *info)
290 {
291     switch(status) {
292     case INTERNET_STATUS_REQUEST_COMPLETE: {
293         INTERNET_ASYNC_RESULT *iar = info;
294         return wine_dbg_sprintf("{%s, %d}", wine_dbgstr_longlong(iar->dwResult), iar->dwError);
295     }
296     default:
297         return wine_dbg_sprintf("%p", info);
298     }
299 }
300
301 void INTERNET_SendCallback(object_header_t *hdr, DWORD_PTR context, DWORD status, void *info, DWORD info_len)
302 {
303     void *new_info = info;
304
305     if( !hdr->lpfnStatusCB )
306         return;
307
308     /* the IE5 version of wininet does not
309        send callbacks if dwContext is zero */
310     if(!context)
311         return;
312
313     switch(status) {
314     case INTERNET_STATUS_NAME_RESOLVED:
315     case INTERNET_STATUS_CONNECTING_TO_SERVER:
316     case INTERNET_STATUS_CONNECTED_TO_SERVER:
317         new_info = heap_alloc(info_len);
318         if(new_info)
319             memcpy(new_info, info, info_len);
320         break;
321     case INTERNET_STATUS_RESOLVING_NAME:
322     case INTERNET_STATUS_REDIRECT:
323         if(hdr->dwInternalFlags & INET_CALLBACKW) {
324             new_info = heap_strdupW(info);
325             break;
326         }else {
327             new_info = heap_strdupWtoA(info);
328             info_len = strlen(new_info)+1;
329             break;
330         }
331     }
332     
333     TRACE(" callback(%p) (%p (%p), %08lx, %d (%s), %s, %d)\n",
334           hdr->lpfnStatusCB, hdr->hInternet, hdr, context, status, get_callback_name(status),
335           debugstr_status_info(status, new_info), info_len);
336     
337     hdr->lpfnStatusCB(hdr->hInternet, context, status, new_info, info_len);
338
339     TRACE(" end callback().\n");
340
341     if(new_info != info)
342         heap_free(new_info);
343 }
344
345 typedef struct {
346     task_header_t hdr;
347     DWORD_PTR context;
348     DWORD     status;
349     LPVOID    status_info;
350     DWORD     status_info_len;
351 } send_callback_task_t;
352
353 static void SendAsyncCallbackProc(task_header_t *hdr)
354 {
355     send_callback_task_t *task = (send_callback_task_t*)hdr;
356
357     TRACE("%p\n", task->hdr.hdr);
358
359     INTERNET_SendCallback(task->hdr.hdr, task->context, task->status, task->status_info, task->status_info_len);
360
361     /* And frees the copy of the status info */
362     heap_free(task->status_info);
363 }
364
365 void SendAsyncCallback(object_header_t *hdr, DWORD_PTR dwContext,
366                        DWORD dwInternetStatus, LPVOID lpvStatusInfo,
367                        DWORD dwStatusInfoLength)
368 {
369     TRACE("(%p, %08lx, %d (%s), %p, %d): %sasync call with callback %p\n",
370           hdr, dwContext, dwInternetStatus, get_callback_name(dwInternetStatus),
371           lpvStatusInfo, dwStatusInfoLength,
372           hdr->dwFlags & INTERNET_FLAG_ASYNC ? "" : "non ",
373           hdr->lpfnStatusCB);
374     
375     if (!(hdr->lpfnStatusCB))
376         return;
377     
378     if (hdr->dwFlags & INTERNET_FLAG_ASYNC)
379     {
380         send_callback_task_t *task;
381         void *lpvStatusInfo_copy = lpvStatusInfo;
382
383         if (lpvStatusInfo)
384         {
385             lpvStatusInfo_copy = heap_alloc(dwStatusInfoLength);
386             memcpy(lpvStatusInfo_copy, lpvStatusInfo, dwStatusInfoLength);
387         }
388
389         task = alloc_async_task(hdr, SendAsyncCallbackProc, sizeof(*task));
390         task->context = dwContext;
391         task->status = dwInternetStatus;
392         task->status_info = lpvStatusInfo_copy;
393         task->status_info_len = dwStatusInfoLength;
394         
395         INTERNET_AsyncCall(&task->hdr);
396     }
397     else
398         INTERNET_SendCallback(hdr, dwContext, dwInternetStatus,
399                               lpvStatusInfo, dwStatusInfoLength);
400 }