Avoid excessive heap memory reallocation when generating EMF
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23  */
24
25 #include "config.h"
26
27 #include <stdarg.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <time.h>
31
32 #include "windef.h"
33 #include "winbase.h"
34 #include "wininet.h"
35 #include "winerror.h"
36
37 #include "wine/debug.h"
38 #include "internet.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
41
42 #define TIME_STRING_LEN  30
43
44 time_t ConvertTimeString(LPCSTR asctime)
45 {
46     char tmpChar[TIME_STRING_LEN];
47     char *tmpChar2;
48     struct tm t;
49     int timelen = strlen(asctime);
50
51     if(!asctime || !timelen)
52         return 0;
53
54     strncpy(tmpChar, asctime, TIME_STRING_LEN);
55
56     /* Assert that the string is the expected length */
57     if (tmpChar[TIME_STRING_LEN] != '\0')
58     {
59         tmpChar[TIME_STRING_LEN] = '\0';
60         FIXME("\n");
61     }
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 = atoi(tmpChar+12) - 1900;
76     t.tm_mday = atoi(tmpChar+5);
77     t.tm_hour = atoi(tmpChar+17);
78     t.tm_min = atoi(tmpChar+20);
79     t.tm_sec = atoi(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(LPCSTR lpszServerName, INTERNET_PORT nServerPort,
130         struct hostent **phe, struct sockaddr_in *psa)
131 {
132     char *found;
133
134     TRACE("%s\n", lpszServerName);
135
136     /* Validate server name first
137      * Check if there is sth. like
138      * pinger.macromedia.com:80
139      * if yes, eliminate the :80....
140      */
141     found = strchr(lpszServerName, ':');
142     if (found)
143     {
144         int len = found - lpszServerName;
145         char *new = HeapAlloc(GetProcessHeap(), 0, len + 1);
146         memcpy( new, lpszServerName, len );
147         new[len] = '\0';
148         TRACE("Found a ':' inside the server name, reparsed name: %s\n", new);
149         *phe = gethostbyname(new);
150         HeapFree( GetProcessHeap(), 0, new );
151     }
152     else *phe = gethostbyname(lpszServerName);
153
154     if (NULL == *phe)
155     {
156         TRACE("Failed to get hostname: (%s)\n", lpszServerName);
157         return FALSE;
158     }
159
160     memset(psa,0,sizeof(struct sockaddr_in));
161     memcpy((char *)&psa->sin_addr, (*phe)->h_addr, (*phe)->h_length);
162     psa->sin_family = (*phe)->h_addrtype;
163     psa->sin_port = htons((u_short)nServerPort);
164
165     return TRUE;
166 }
167
168 /*
169  * Helper function for sending async Callbacks
170  */
171
172 VOID SendAsyncCallbackInt(LPWININETAPPINFOA hIC, HINTERNET hHttpSession,
173                              DWORD dwContext, DWORD dwInternetStatus, LPVOID
174                              lpvStatusInfo, DWORD dwStatusInfoLength)
175 {
176         if (! (hIC->lpfnStatusCB))
177             return;
178
179         /* the IE5 version of wininet does not
180            send callbacks if dwContext is zero */
181         if( !dwContext )
182             return;
183
184         TRACE("--> Callback %ld\n",dwInternetStatus);
185
186         hIC->lpfnStatusCB(hHttpSession, dwContext, dwInternetStatus,
187                           lpvStatusInfo, dwStatusInfoLength);
188
189         TRACE("<-- Callback %ld\n",dwInternetStatus);
190 }
191
192
193
194 VOID SendAsyncCallback(LPWININETAPPINFOA hIC, HINTERNET hHttpSession,
195                              DWORD dwContext, DWORD dwInternetStatus, LPVOID
196                              lpvStatusInfo,  DWORD dwStatusInfoLength)
197 {
198         TRACE("Send Callback %ld\n",dwInternetStatus);
199
200         if (! (hIC->lpfnStatusCB))
201             return;
202         if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
203         {
204             WORKREQUEST workRequest;
205             struct WORKREQ_SENDCALLBACK *req;
206
207             workRequest.asyncall = SENDCALLBACK;
208             workRequest.handle = hIC;
209             req = &workRequest.u.SendCallback;
210             req->hHttpSession = hHttpSession;
211             req->dwContext = dwContext;
212             req->dwInternetStatus = dwInternetStatus;
213             req->lpvStatusInfo = lpvStatusInfo;
214             req->dwStatusInfoLength = dwStatusInfoLength;
215
216             INTERNET_AsyncCall(&workRequest);
217         }
218         else
219             SendAsyncCallbackInt(hIC, hHttpSession, dwContext, dwInternetStatus,
220                                   lpvStatusInfo, dwStatusInfoLength);
221 }