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