Added LGPL standard comment, and copyright notices where necessary.
[wine] / dlls / wininet / utility.c
1 /* 
2  * Wininet - Utility functions
3  *
4  * Copyright 1999 Corel Corporation
5  *
6  * Ulrich Czekalla
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include "config.h"
24
25 #include <stdlib.h>
26 #include <string.h>
27 #include <time.h>
28
29 #include "windef.h"
30 #include "winbase.h"
31 #include "wininet.h"
32 #include "winerror.h"
33
34 #include "wine/debug.h"
35 #include "internet.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
38
39 #define TIME_STRING_LEN  30
40
41 time_t ConvertTimeString(LPCSTR asctime)
42 {
43     char tmpChar[TIME_STRING_LEN];
44     char *tmpChar2;
45     struct tm t;
46     int timelen = strlen(asctime);
47
48     if(!asctime || !timelen)
49         return 0;
50
51     strncpy(tmpChar, asctime, TIME_STRING_LEN);
52
53     /* Assert that the string is the expected length */
54     if (tmpChar[TIME_STRING_LEN] != '\0')
55     { 
56         tmpChar[TIME_STRING_LEN] = '\0';
57         FIXME("\n");
58     }
59     
60     /* Convert a time such as 'Mon, 15 Nov 1999 16:09:35 GMT' into a SYSTEMTIME structure
61      * We assume the time is in this format
62      * and divide it into easy to swallow chunks
63      */
64     tmpChar[3]='\0';
65     tmpChar[7]='\0';
66     tmpChar[11]='\0';
67     tmpChar[16]='\0';
68     tmpChar[19]='\0';
69     tmpChar[22]='\0';
70     tmpChar[25]='\0';
71
72     t.tm_year = atoi(tmpChar+12) - 1900;
73     t.tm_mday = atoi(tmpChar+5);
74     t.tm_hour = atoi(tmpChar+17);
75     t.tm_min = atoi(tmpChar+20);
76     t.tm_sec = atoi(tmpChar+23);
77         
78     /* and month */
79     tmpChar2 = tmpChar + 8;
80     switch(tmpChar2[2])
81     {
82         case 'n':
83             if(tmpChar2[1]=='a')
84                 t.tm_mon = 0;
85             else
86                 t.tm_mon = 5;
87             break;
88         case 'b':
89             t.tm_mon = 1;
90             break;
91         case 'r':
92             if(tmpChar2[1]=='a')
93                 t.tm_mon = 2;
94             else
95                 t.tm_mon = 3;
96             break;
97         case 'y':
98             t.tm_mon = 4;
99             break;
100         case 'l':
101             t.tm_mon = 6;
102             break;
103         case 'g':
104             t.tm_mon = 7;
105             break;
106         case 'p':
107             t.tm_mon = 8;
108             break;
109         case 't':
110             t.tm_mon = 9;
111             break;
112         case 'v':
113             t.tm_mon = 10;
114             break;
115         case 'c':
116             t.tm_mon = 11;
117             break;
118         default:
119             FIXME("\n");
120     }
121
122     return mktime(&t);
123 }
124
125
126 BOOL GetAddress(LPCSTR lpszServerName, INTERNET_PORT nServerPort,
127         struct hostent **phe, struct sockaddr_in *psa)
128 {
129     char *found;
130
131     TRACE("%s\n", lpszServerName);
132
133     /* Validate server name first
134      * Check if there is sth. like
135      * pinger.macromedia.com:80
136      * if yes, eliminate the :80....
137      */
138     found = strchr(lpszServerName, ':');
139     if (found)
140     {
141         int len = found - lpszServerName;
142         char *new = HeapAlloc(GetProcessHeap(), 0, len + 1);
143         memcpy( new, lpszServerName, len );
144         new[len] = '\0';
145         TRACE("Found a ':' inside the server name, reparsed name: %s\n", new);
146         *phe = gethostbyname(new);
147         HeapFree( GetProcessHeap(), 0, new );
148     }
149     else *phe = gethostbyname(lpszServerName);
150
151     if (NULL == *phe)
152     {
153         TRACE("Failed to get hostname: (%s)\n", lpszServerName);
154         return FALSE;
155     }
156
157     memset(psa,0,sizeof(struct sockaddr_in));
158     memcpy((char *)&psa->sin_addr, (*phe)->h_addr, (*phe)->h_length);
159     psa->sin_family = (*phe)->h_addrtype;
160     psa->sin_port = htons((u_short)nServerPort);
161
162     return TRUE;
163 }