]> git.sesse.net Git - ffmpeg/blob - libavformat/os_support.c
flacdec: be less strict when parsing attached pictures.
[ffmpeg] / libavformat / os_support.c
1 /*
2  * various OS-feature replacement utilities
3  * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4  * copyright (c) 2002 Francois Revol
5  *
6  * This file is part of Libav.
7  *
8  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 /* needed by inet_aton() */
24 #define _SVID_SOURCE
25
26 #include "config.h"
27 #include "avformat.h"
28 #include "os_support.h"
29
30 #if defined(_WIN32) && !defined(__MINGW32CE__)
31 #undef open
32 #include <fcntl.h>
33 #include <io.h>
34 #include <windows.h>
35
36 int ff_win32_open(const char *filename_utf8, int oflag, int pmode)
37 {
38     int fd;
39     int num_chars;
40     wchar_t *filename_w;
41
42     /* convert UTF-8 to wide chars */
43     num_chars = MultiByteToWideChar(CP_UTF8, 0, filename_utf8, -1, NULL, 0);
44     if (num_chars <= 0)
45         return -1;
46     filename_w = av_mallocz(sizeof(wchar_t) * num_chars);
47     MultiByteToWideChar(CP_UTF8, 0, filename_utf8, -1, filename_w, num_chars);
48
49     fd = _wopen(filename_w, oflag, pmode);
50     av_freep(&filename_w);
51
52     /* filename maybe be in CP_ACP */
53     if (fd == -1 && !(oflag & O_CREAT))
54         return open(filename_utf8, oflag, pmode);
55
56     return fd;
57 }
58 #endif
59
60 #if CONFIG_NETWORK
61 #include <fcntl.h>
62 #if !HAVE_POLL_H
63 #if HAVE_SYS_TIME_H
64 #include <sys/time.h>
65 #endif
66 #if HAVE_WINSOCK2_H
67 #include <winsock2.h>
68 #elif HAVE_SYS_SELECT_H
69 #include <sys/select.h>
70 #endif
71 #endif
72
73 #include "network.h"
74
75 #if !HAVE_INET_ATON
76 #include <stdlib.h>
77
78 int ff_inet_aton (const char * str, struct in_addr * add)
79 {
80     unsigned int add1 = 0, add2 = 0, add3 = 0, add4 = 0;
81
82     if (sscanf(str, "%d.%d.%d.%d", &add1, &add2, &add3, &add4) != 4)
83         return 0;
84
85     if (!add1 || (add1|add2|add3|add4) > 255) return 0;
86
87     add->s_addr = htonl((add1 << 24) + (add2 << 16) + (add3 << 8) + add4);
88
89     return 1;
90 }
91 #else
92 int ff_inet_aton (const char * str, struct in_addr * add)
93 {
94     return inet_aton(str, add);
95 }
96 #endif /* !HAVE_INET_ATON */
97
98 #if !HAVE_GETADDRINFO
99 int ff_getaddrinfo(const char *node, const char *service,
100                 const struct addrinfo *hints, struct addrinfo **res)
101 {
102     struct hostent *h = NULL;
103     struct addrinfo *ai;
104     struct sockaddr_in *sin;
105
106 #if HAVE_WINSOCK2_H
107     int (WSAAPI *win_getaddrinfo)(const char *node, const char *service,
108                                   const struct addrinfo *hints,
109                                   struct addrinfo **res);
110     HMODULE ws2mod = GetModuleHandle("ws2_32.dll");
111     win_getaddrinfo = GetProcAddress(ws2mod, "getaddrinfo");
112     if (win_getaddrinfo)
113         return win_getaddrinfo(node, service, hints, res);
114 #endif
115
116     *res = NULL;
117     sin = av_mallocz(sizeof(struct sockaddr_in));
118     if (!sin)
119         return EAI_FAIL;
120     sin->sin_family = AF_INET;
121
122     if (node) {
123         if (!ff_inet_aton(node, &sin->sin_addr)) {
124             if (hints && (hints->ai_flags & AI_NUMERICHOST)) {
125                 av_free(sin);
126                 return EAI_FAIL;
127             }
128             h = gethostbyname(node);
129             if (!h) {
130                 av_free(sin);
131                 return EAI_FAIL;
132             }
133             memcpy(&sin->sin_addr, h->h_addr_list[0], sizeof(struct in_addr));
134         }
135     } else {
136         if (hints && (hints->ai_flags & AI_PASSIVE)) {
137             sin->sin_addr.s_addr = INADDR_ANY;
138         } else
139             sin->sin_addr.s_addr = INADDR_LOOPBACK;
140     }
141
142     /* Note: getaddrinfo allows service to be a string, which
143      * should be looked up using getservbyname. */
144     if (service)
145         sin->sin_port = htons(atoi(service));
146
147     ai = av_mallocz(sizeof(struct addrinfo));
148     if (!ai) {
149         av_free(sin);
150         return EAI_FAIL;
151     }
152
153     *res = ai;
154     ai->ai_family = AF_INET;
155     ai->ai_socktype = hints ? hints->ai_socktype : 0;
156     switch (ai->ai_socktype) {
157     case SOCK_STREAM: ai->ai_protocol = IPPROTO_TCP; break;
158     case SOCK_DGRAM:  ai->ai_protocol = IPPROTO_UDP; break;
159     default:          ai->ai_protocol = 0;           break;
160     }
161
162     ai->ai_addr = (struct sockaddr *)sin;
163     ai->ai_addrlen = sizeof(struct sockaddr_in);
164     if (hints && (hints->ai_flags & AI_CANONNAME))
165         ai->ai_canonname = h ? av_strdup(h->h_name) : NULL;
166
167     ai->ai_next = NULL;
168     return 0;
169 }
170
171 void ff_freeaddrinfo(struct addrinfo *res)
172 {
173 #if HAVE_WINSOCK2_H
174     void (WSAAPI *win_freeaddrinfo)(struct addrinfo *res);
175     HMODULE ws2mod = GetModuleHandle("ws2_32.dll");
176     win_freeaddrinfo = (void (WSAAPI *)(struct addrinfo *res))
177                        GetProcAddress(ws2mod, "freeaddrinfo");
178     if (win_freeaddrinfo) {
179         win_freeaddrinfo(res);
180         return;
181     }
182 #endif
183
184     av_free(res->ai_canonname);
185     av_free(res->ai_addr);
186     av_free(res);
187 }
188
189 int ff_getnameinfo(const struct sockaddr *sa, int salen,
190                    char *host, int hostlen,
191                    char *serv, int servlen, int flags)
192 {
193     const struct sockaddr_in *sin = (const struct sockaddr_in *)sa;
194
195 #if HAVE_WINSOCK2_H
196     int (WSAAPI *win_getnameinfo)(const struct sockaddr *sa, socklen_t salen,
197                                   char *host, DWORD hostlen,
198                                   char *serv, DWORD servlen, int flags);
199     HMODULE ws2mod = GetModuleHandle("ws2_32.dll");
200     win_getnameinfo = GetProcAddress(ws2mod, "getnameinfo");
201     if (win_getnameinfo)
202         return win_getnameinfo(sa, salen, host, hostlen, serv, servlen, flags);
203 #endif
204
205     if (sa->sa_family != AF_INET)
206         return EAI_FAMILY;
207     if (!host && !serv)
208         return EAI_NONAME;
209
210     if (host && hostlen > 0) {
211         struct hostent *ent = NULL;
212         uint32_t a;
213         if (!(flags & NI_NUMERICHOST))
214             ent = gethostbyaddr((const char *)&sin->sin_addr,
215                                 sizeof(sin->sin_addr), AF_INET);
216
217         if (ent) {
218             snprintf(host, hostlen, "%s", ent->h_name);
219         } else if (flags & NI_NAMERQD) {
220             return EAI_NONAME;
221         } else {
222             a = ntohl(sin->sin_addr.s_addr);
223             snprintf(host, hostlen, "%d.%d.%d.%d",
224                      ((a >> 24) & 0xff), ((a >> 16) & 0xff),
225                      ((a >>  8) & 0xff), ( a        & 0xff));
226         }
227     }
228
229     if (serv && servlen > 0) {
230         struct servent *ent = NULL;
231         if (!(flags & NI_NUMERICSERV))
232             ent = getservbyport(sin->sin_port, flags & NI_DGRAM ? "udp" : "tcp");
233
234         if (ent) {
235             snprintf(serv, servlen, "%s", ent->s_name);
236         } else
237             snprintf(serv, servlen, "%d", ntohs(sin->sin_port));
238     }
239
240     return 0;
241 }
242
243 const char *ff_gai_strerror(int ecode)
244 {
245     switch(ecode) {
246     case EAI_FAIL   : return "A non-recoverable error occurred";
247     case EAI_FAMILY : return "The address family was not recognized or the address length was invalid for the specified family";
248     case EAI_NONAME : return "The name does not resolve for the supplied parameters";
249     }
250
251     return "Unknown error";
252 }
253 #endif
254
255 int ff_socket_nonblock(int socket, int enable)
256 {
257 #if HAVE_WINSOCK2_H
258    u_long param = enable;
259    return ioctlsocket(socket, FIONBIO, &param);
260 #else
261    if (enable)
262       return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) | O_NONBLOCK);
263    else
264       return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) & ~O_NONBLOCK);
265 #endif
266 }
267
268 #if !HAVE_POLL_H
269 int ff_poll(struct pollfd *fds, nfds_t numfds, int timeout)
270 {
271     fd_set read_set;
272     fd_set write_set;
273     fd_set exception_set;
274     nfds_t i;
275     int n;
276     int rc;
277
278 #if HAVE_WINSOCK2_H
279     if (numfds >= FD_SETSIZE) {
280         errno = EINVAL;
281         return -1;
282     }
283 #endif
284
285     FD_ZERO(&read_set);
286     FD_ZERO(&write_set);
287     FD_ZERO(&exception_set);
288
289     n = 0;
290     for(i = 0; i < numfds; i++) {
291         if (fds[i].fd < 0)
292             continue;
293 #if !HAVE_WINSOCK2_H
294         if (fds[i].fd >= FD_SETSIZE) {
295             errno = EINVAL;
296             return -1;
297         }
298 #endif
299
300         if (fds[i].events & POLLIN)  FD_SET(fds[i].fd, &read_set);
301         if (fds[i].events & POLLOUT) FD_SET(fds[i].fd, &write_set);
302         if (fds[i].events & POLLERR) FD_SET(fds[i].fd, &exception_set);
303
304         if (fds[i].fd >= n)
305             n = fds[i].fd + 1;
306     };
307
308     if (n == 0)
309         /* Hey!? Nothing to poll, in fact!!! */
310         return 0;
311
312     if (timeout < 0)
313         rc = select(n, &read_set, &write_set, &exception_set, NULL);
314     else {
315         struct timeval    tv;
316
317         tv.tv_sec = timeout / 1000;
318         tv.tv_usec = 1000 * (timeout % 1000);
319         rc = select(n, &read_set, &write_set, &exception_set, &tv);
320     };
321
322     if (rc < 0)
323         return rc;
324
325     for(i = 0; i < numfds; i++) {
326         fds[i].revents = 0;
327
328         if (FD_ISSET(fds[i].fd, &read_set))      fds[i].revents |= POLLIN;
329         if (FD_ISSET(fds[i].fd, &write_set))     fds[i].revents |= POLLOUT;
330         if (FD_ISSET(fds[i].fd, &exception_set)) fds[i].revents |= POLLERR;
331     };
332
333     return rc;
334 }
335 #endif /* HAVE_POLL_H */
336 #endif /* CONFIG_NETWORK */