]> git.sesse.net Git - vlc/blob - src/extras/libc.c
Remove useless <dirent.h> check
[vlc] / src / extras / libc.c
1 /*****************************************************************************
2  * libc.c: Extra libc function for some systems.
3  *****************************************************************************
4  * Copyright (C) 2002-2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
8  *          Samuel Hocevar <sam@zoy.org>
9  *          Gildas Bazin <gbazin@videolan.org>
10  *          Derk-Jan Hartman <hartman at videolan dot org>
11  *          Christophe Massiot <massiot@via.ecp.fr>
12  *          Rémi Denis-Courmont <rem à videolan.org>
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
27  *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33
34 #include <errno.h>
35 #include <dirent.h>
36
37 #undef iconv_t
38 #undef iconv_open
39 #undef iconv
40 #undef iconv_close
41
42 #if defined(HAVE_ICONV)
43 #   include <iconv.h>
44 #endif
45
46 #ifdef HAVE_FORK
47 #   include <signal.h>
48 #   include <unistd.h>
49 #   include <sys/wait.h>
50 #   include <sys/socket.h>
51 #   include <sys/poll.h>
52 #   ifndef PF_LOCAL
53 #       define PF_LOCAL PF_UNIX
54 #   endif
55 #endif
56
57 #if defined(WIN32) || defined(UNDER_CE)
58 #   undef _wopendir
59 #   undef _wreaddir
60 #   undef _wclosedir
61 #   undef rewinddir
62 #   define WIN32_LEAN_AND_MEAN
63 #   include <windows.h>
64 #endif
65
66 /*****************************************************************************
67  * vlc_*dir_wrapper: wrapper under Windows to return the list of drive letters
68  * when called with an empty argument or just '\'
69  *****************************************************************************/
70 #if defined(WIN32)
71 #   include <assert.h>
72
73 typedef struct vlc_DIR
74 {
75     _WDIR *p_real_dir;
76     int i_drives;
77     struct _wdirent dd_dir;
78     bool b_insert_back;
79 } vlc_DIR;
80
81 void *vlc_wopendir( const wchar_t *wpath )
82 {
83     vlc_DIR *p_dir = NULL;
84     _WDIR *p_real_dir = NULL;
85
86     if ( wpath == NULL || wpath[0] == '\0'
87           || (wcscmp (wpath, L"\\") == 0) )
88     {
89         /* Special mode to list drive letters */
90         p_dir = malloc( sizeof(vlc_DIR) );
91         if( !p_dir )
92             return NULL;
93         p_dir->p_real_dir = NULL;
94 #if defined(UNDER_CE)
95         p_dir->i_drives = NULL;
96 #else
97         p_dir->i_drives = GetLogicalDrives();
98 #endif
99         return (void *)p_dir;
100     }
101
102     p_real_dir = _wopendir( wpath );
103     if ( p_real_dir == NULL )
104         return NULL;
105
106     p_dir = malloc( sizeof(vlc_DIR) );
107     if( !p_dir )
108     {
109         _wclosedir( p_real_dir );
110         return NULL;
111     }
112     p_dir->p_real_dir = p_real_dir;
113
114     assert (wpath[0]); // wpath[1] is defined
115     p_dir->b_insert_back = !wcscmp (wpath + 1, L":\\");
116     return (void *)p_dir;
117 }
118
119 struct _wdirent *vlc_wreaddir( void *_p_dir )
120 {
121     vlc_DIR *p_dir = (vlc_DIR *)_p_dir;
122     DWORD i_drives;
123
124     if ( p_dir->p_real_dir != NULL )
125     {
126         if ( p_dir->b_insert_back )
127         {
128             /* Adds "..", gruik! */
129             p_dir->dd_dir.d_ino = 0;
130             p_dir->dd_dir.d_reclen = 0;
131             p_dir->dd_dir.d_namlen = 2;
132             wcscpy( p_dir->dd_dir.d_name, L".." );
133             p_dir->b_insert_back = false;
134             return &p_dir->dd_dir;
135         }
136
137         return _wreaddir( p_dir->p_real_dir );
138     }
139
140     /* Drive letters mode */
141     i_drives = p_dir->i_drives;
142 #ifdef UNDER_CE
143     swprintf( p_dir->dd_dir.d_name, L"\\");
144     p_dir->dd_dir.d_namlen = wcslen(p_dir->dd_dir.d_name);
145 #else
146     unsigned int i;
147     if ( !i_drives )
148         return NULL; /* end */
149
150     for ( i = 0; i < sizeof(DWORD)*8; i++, i_drives >>= 1 )
151         if ( i_drives & 1 ) break;
152
153     if ( i >= 26 )
154         return NULL; /* this should not happen */
155
156     swprintf( p_dir->dd_dir.d_name, L"%c:\\", 'A' + i );
157     p_dir->dd_dir.d_namlen = wcslen(p_dir->dd_dir.d_name);
158     p_dir->i_drives &= ~(1UL << i);
159 #endif
160     return &p_dir->dd_dir;
161 }
162
163 void vlc_rewinddir( void *_p_dir )
164 {
165     vlc_DIR *p_dir = (vlc_DIR *)_p_dir;
166
167     if ( p_dir->p_real_dir != NULL )
168         _wrewinddir( p_dir->p_real_dir );
169 }
170 #endif
171
172 /* This one is in the libvlccore exported symbol list */
173 int vlc_wclosedir( void *_p_dir )
174 {
175 #if defined(WIN32)
176     vlc_DIR *p_dir = (vlc_DIR *)_p_dir;
177     int i_ret = 0;
178
179     if ( p_dir->p_real_dir != NULL )
180         i_ret = _wclosedir( p_dir->p_real_dir );
181
182     free( p_dir );
183     return i_ret;
184 #else
185     return closedir( _p_dir );
186 #endif
187 }
188
189 #ifdef ENABLE_NLS
190 # include <libintl.h>
191 #endif
192
193 /**
194  * In-tree plugins share their gettext domain with LibVLC.
195  */
196 char *vlc_gettext( const char *msgid )
197 {
198 #ifdef ENABLE_NLS
199     return dgettext( PACKAGE_NAME, msgid );
200 #else
201     return (char *)msgid;
202 #endif
203 }
204
205 /*****************************************************************************
206  * Local conversion routine from ISO_6937 to UTF-8 charset. Support for this
207  * is still missing in libiconv, hence multiple operating systems lack it.
208  * The conversion table adds Euro sign (0xA4) as per ETSI EN 300 468 Annex A
209  *****************************************************************************/
210 #ifndef __linux__
211 static const uint16_t to_ucs4[128] =
212 {
213   /* 0x80 */ 0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087,
214   /* 0x88 */ 0x0088, 0x0089, 0x008a, 0x008b, 0x008c, 0x008d, 0x008e, 0x008f,
215   /* 0x90 */ 0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097,
216   /* 0x98 */ 0x0098, 0x0099, 0x009a, 0x009b, 0x009c, 0x009d, 0x009e, 0x009f,
217   /* 0xa0 */ 0x00a0, 0x00a1, 0x00a2, 0x00a3, 0x20ac, 0x00a5, 0x0000, 0x00a7,
218   /* 0xa8 */ 0x00a4, 0x2018, 0x201c, 0x00ab, 0x2190, 0x2191, 0x2192, 0x2193,
219   /* 0xb0 */ 0x00b0, 0x00b1, 0x00b2, 0x00b3, 0x00d7, 0x00b5, 0x00b6, 0x00b7,
220   /* 0xb8 */ 0x00f7, 0x2019, 0x201d, 0x00bb, 0x00bc, 0x00bd, 0x00be, 0x00bf,
221   /* 0xc0 */ 0x0000, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
222   /* 0xc8 */ 0xffff, 0x0000, 0xffff, 0xffff, 0x0000, 0xffff, 0xffff, 0xffff,
223   /* 0xd0 */ 0x2014, 0x00b9, 0x00ae, 0x00a9, 0x2122, 0x266a, 0x00ac, 0x00a6,
224   /* 0xd8 */ 0x0000, 0x0000, 0x0000, 0x0000, 0x215b, 0x215c, 0x215d, 0x215e,
225   /* 0xe0 */ 0x2126, 0x00c6, 0x00d0, 0x00aa, 0x0126, 0x0000, 0x0132, 0x013f,
226   /* 0xe8 */ 0x0141, 0x00d8, 0x0152, 0x00ba, 0x00de, 0x0166, 0x014a, 0x0149,
227   /* 0xf0 */ 0x0138, 0x00e6, 0x0111, 0x00f0, 0x0127, 0x0131, 0x0133, 0x0140,
228   /* 0xf8 */ 0x0142, 0x00f8, 0x0153, 0x00df, 0x00fe, 0x0167, 0x014b, 0x00ad
229 };
230
231 /* The outer array range runs from 0xc1 to 0xcf, the inner range from 0x40
232    to 0x7f.  */
233 static const uint16_t to_ucs4_comb[15][64] =
234 {
235   /* 0xc1 */
236   {
237     /* 0x40 */ 0x0000, 0x00c0, 0x0000, 0x0000, 0x0000, 0x00c8, 0x0000, 0x0000,
238     /* 0x48 */ 0x0000, 0x00cc, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x00d2,
239     /* 0x50 */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x00d9, 0x0000, 0x0000,
240     /* 0x58 */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
241     /* 0x60 */ 0x0000, 0x00e0, 0x0000, 0x0000, 0x0000, 0x00e8, 0x0000, 0x0000,
242     /* 0x68 */ 0x0000, 0x00ec, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x00f2,
243     /* 0x70 */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x00f9, 0x0000, 0x0000,
244     /* 0x78 */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000
245   },
246   /* 0xc2 */
247   {
248     /* 0x40 */ 0x0000, 0x00c1, 0x0000, 0x0106, 0x0000, 0x00c9, 0x0000, 0x0000,
249     /* 0x48 */ 0x0000, 0x00cd, 0x0000, 0x0000, 0x0139, 0x0000, 0x0143, 0x00d3,
250     /* 0x50 */ 0x0000, 0x0000, 0x0154, 0x015a, 0x0000, 0x00da, 0x0000, 0x0000,
251     /* 0x58 */ 0x0000, 0x00dd, 0x0179, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
252     /* 0x60 */ 0x0000, 0x00e1, 0x0000, 0x0107, 0x0000, 0x00e9, 0x0000, 0x0000,
253     /* 0x68 */ 0x0000, 0x00ed, 0x0000, 0x0000, 0x013a, 0x0000, 0x0144, 0x00f3,
254     /* 0x70 */ 0x0000, 0x0000, 0x0155, 0x015b, 0x0000, 0x00fa, 0x0000, 0x0000,
255     /* 0x78 */ 0x0000, 0x00fd, 0x017a, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000
256   },
257   /* 0xc3 */
258   {
259     /* 0x40 */ 0x0000, 0x00c2, 0x0000, 0x0108, 0x0000, 0x00ca, 0x0000, 0x011c,
260     /* 0x48 */ 0x0124, 0x00ce, 0x0134, 0x0000, 0x0000, 0x0000, 0x0000, 0x00d4,
261     /* 0x50 */ 0x0000, 0x0000, 0x0000, 0x015c, 0x0000, 0x00db, 0x0000, 0x0174,
262     /* 0x58 */ 0x0000, 0x0176, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
263     /* 0x60 */ 0x0000, 0x00e2, 0x0000, 0x0109, 0x0000, 0x00ea, 0x0000, 0x011d,
264     /* 0x68 */ 0x0125, 0x00ee, 0x0135, 0x0000, 0x0000, 0x0000, 0x0000, 0x00f4,
265     /* 0x70 */ 0x0000, 0x0000, 0x0000, 0x015d, 0x0000, 0x00fb, 0x0000, 0x0175,
266     /* 0x78 */ 0x0000, 0x0177, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000
267   },
268   /* 0xc4 */
269   {
270     /* 0x40 */ 0x0000, 0x00c3, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
271     /* 0x48 */ 0x0000, 0x0128, 0x0000, 0x0000, 0x0000, 0x0000, 0x00d1, 0x00d5,
272     /* 0x50 */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0168, 0x0000, 0x0000,
273     /* 0x58 */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
274     /* 0x60 */ 0x0000, 0x00e3, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
275     /* 0x68 */ 0x0000, 0x0129, 0x0000, 0x0000, 0x0000, 0x0000, 0x00f1, 0x00f5,
276     /* 0x70 */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0169, 0x0000, 0x0000,
277     /* 0x78 */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000
278   },
279   /* 0xc5 */
280   {
281     /* 0x40 */ 0x0000, 0x0100, 0x0000, 0x0000, 0x0000, 0x0112, 0x0000, 0x0000,
282     /* 0x48 */ 0x0000, 0x012a, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x014c,
283     /* 0x50 */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x016a, 0x0000, 0x0000,
284     /* 0x58 */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
285     /* 0x60 */ 0x0000, 0x0101, 0x0000, 0x0000, 0x0000, 0x0113, 0x0000, 0x0000,
286     /* 0x68 */ 0x0000, 0x012b, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x014d,
287     /* 0x70 */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x016b, 0x0000, 0x0000,
288     /* 0x78 */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000
289   },
290   /* 0xc6 */
291   {
292     /* 0x40 */ 0x0000, 0x0102, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x011e,
293     /* 0x48 */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
294     /* 0x50 */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x016c, 0x0000, 0x0000,
295     /* 0x58 */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
296     /* 0x60 */ 0x0000, 0x0103, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x011f,
297     /* 0x68 */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
298     /* 0x70 */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x016d, 0x0000, 0x0000,
299     /* 0x78 */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000
300   },
301   /* 0xc7 */
302   {
303     /* 0x40 */ 0x0000, 0x0000, 0x0000, 0x010a, 0x0000, 0x0116, 0x0000, 0x0120,
304     /* 0x48 */ 0x0000, 0x0130, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
305     /* 0x50 */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
306     /* 0x58 */ 0x0000, 0x0000, 0x017b, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
307     /* 0x60 */ 0x0000, 0x0000, 0x0000, 0x010b, 0x0000, 0x0117, 0x0000, 0x0121,
308     /* 0x68 */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
309     /* 0x70 */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
310     /* 0x78 */ 0x0000, 0x0000, 0x017c, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000
311   },
312   /* 0xc8 */
313   {
314     /* 0x40 */ 0x0000, 0x00c4, 0x0000, 0x0000, 0x0000, 0x00cb, 0x0000, 0x0000,
315     /* 0x48 */ 0x0000, 0x00cf, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x00d6,
316     /* 0x50 */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x00dc, 0x0000, 0x0000,
317     /* 0x58 */ 0x0000, 0x0178, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
318     /* 0x60 */ 0x0000, 0x00e4, 0x0000, 0x0000, 0x0000, 0x00eb, 0x0000, 0x0000,
319     /* 0x68 */ 0x0000, 0x00ef, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x00f6,
320     /* 0x70 */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x00fc, 0x0000, 0x0000,
321     /* 0x78 */ 0x0000, 0x00ff, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000
322   },
323   /* 0xc9 */
324   {
325     0x0000,
326   },
327   /* 0xca */
328   {
329     /* 0x40 */ 0x0000, 0x00c5, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
330     /* 0x48 */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
331     /* 0x50 */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x016e, 0x0000, 0x0000,
332     /* 0x58 */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
333     /* 0x60 */ 0x0000, 0x00e5, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
334     /* 0x68 */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
335     /* 0x70 */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x016f, 0x0000, 0x0000,
336     /* 0x78 */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000
337   },
338   /* 0xcb */
339   {
340     /* 0x40 */ 0x0000, 0x0000, 0x0000, 0x00c7, 0x0000, 0x0000, 0x0000, 0x0122,
341     /* 0x48 */ 0x0000, 0x0000, 0x0000, 0x0136, 0x013b, 0x0000, 0x0145, 0x0000,
342     /* 0x50 */ 0x0000, 0x0000, 0x0156, 0x015e, 0x0162, 0x0000, 0x0000, 0x0000,
343     /* 0x58 */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
344     /* 0x60 */ 0x0000, 0x0000, 0x0000, 0x00e7, 0x0000, 0x0000, 0x0000, 0x0123,
345     /* 0x68 */ 0x0000, 0x0000, 0x0000, 0x0137, 0x013c, 0x0000, 0x0146, 0x0000,
346     /* 0x70 */ 0x0000, 0x0000, 0x0157, 0x015f, 0x0163, 0x0000, 0x0000, 0x0000,
347     /* 0x78 */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000
348   },
349   /* 0xcc */
350   {
351     0x0000,
352   },
353   /* 0xcd */
354   {
355     /* 0x40 */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
356     /* 0x48 */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0150,
357     /* 0x50 */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0170, 0x0000, 0x0000,
358     /* 0x58 */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
359     /* 0x60 */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
360     /* 0x68 */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0151,
361     /* 0x70 */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0171, 0x0000, 0x0000,
362     /* 0x78 */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000
363   },
364   /* 0xce */
365   {
366     /* 0x40 */ 0x0000, 0x0104, 0x0000, 0x0000, 0x0000, 0x0118, 0x0000, 0x0000,
367     /* 0x48 */ 0x0000, 0x012e, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
368     /* 0x50 */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0172, 0x0000, 0x0000,
369     /* 0x58 */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
370     /* 0x60 */ 0x0000, 0x0105, 0x0000, 0x0000, 0x0000, 0x0119, 0x0000, 0x0000,
371     /* 0x68 */ 0x0000, 0x012f, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
372     /* 0x70 */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0173, 0x0000, 0x0000,
373     /* 0x78 */ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000
374   },
375   /* 0xcf */
376   {
377     /* 0x40 */ 0x0000, 0x0000, 0x0000, 0x010c, 0x010e, 0x011a, 0x0000, 0x0000,
378     /* 0x48 */ 0x0000, 0x0000, 0x0000, 0x0000, 0x013d, 0x0000, 0x0147, 0x0000,
379     /* 0x50 */ 0x0000, 0x0000, 0x0158, 0x0160, 0x0164, 0x0000, 0x0000, 0x0000,
380     /* 0x58 */ 0x0000, 0x0000, 0x017d, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
381     /* 0x60 */ 0x0000, 0x0000, 0x0000, 0x010d, 0x010f, 0x011b, 0x0000, 0x0000,
382     /* 0x68 */ 0x0000, 0x0000, 0x0000, 0x0000, 0x013e, 0x0000, 0x0148, 0x0000,
383     /* 0x70 */ 0x0000, 0x0000, 0x0159, 0x0161, 0x0165, 0x0000, 0x0000, 0x0000,
384     /* 0x78 */ 0x0000, 0x0000, 0x017e, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000
385   }
386 };
387
388 static size_t ISO6937toUTF8( const unsigned char **inbuf, size_t *inbytesleft,
389                              unsigned char **outbuf, size_t *outbytesleft )
390
391
392 {
393     if( !inbuf || !(*inbuf) )
394         return (size_t)(0);    /* Reset state requested */
395
396     const unsigned char *iptr = *inbuf;
397     const unsigned char *iend = iptr + *inbytesleft;
398     unsigned char *optr = *outbuf;
399     unsigned char *oend = optr + *outbytesleft;
400     uint16_t ch;
401     int err = 0;
402
403     while ( iptr < iend )
404     {
405         if( *iptr < 0x80 )
406         {
407             if( optr >= oend )
408             {
409                 err = E2BIG;
410                 break;    /* No space in outbuf */
411             }
412             *optr++ = *iptr++;
413             continue;
414         }
415
416
417         if ( optr + 2 >= oend )
418         {
419             err = E2BIG;
420             break;        /* No space in outbuf for multibyte char */
421         }
422
423         ch = to_ucs4[*iptr - 0x80];
424
425         if( ch == 0xffff )
426         {
427             /* Composed character */
428             if ( iptr + 1 >= iend )
429             {
430                 err = EINVAL;
431                 break;    /* No next character */
432             }
433             if ( iptr[1] < 0x40 || iptr[1] >= 0x80 ||
434                  !(ch = to_ucs4_comb[iptr[0] - 0xc1][iptr[1] - 0x40]) )
435             {
436                 err = EILSEQ;
437                 break;   /* Illegal combination */
438             }
439             iptr += 2;
440
441         }
442         else
443         {
444             if ( !ch )
445             {
446                 err = EILSEQ;
447                 break;
448             }
449             iptr++;
450         }
451
452         if ( ch < 0x800 )
453         {
454             optr[1] = 0x80 | (ch & 0x3f);
455             optr[0] = 0xc0 | (ch >> 6);
456             optr +=2;
457         }
458         else
459         {
460             optr[2] = 0x80 | (ch & 0x3f);
461             ch >>= 6;
462             optr[1] = 0x80 | (ch & 0x3f);
463             optr[0] = 0xe0 | (ch >> 6);
464             optr += 3;
465         }
466
467     }
468     *inbuf = iptr;
469     *outbuf = optr;
470     *inbytesleft = iend - iptr;
471     *outbytesleft = oend - optr;
472
473     if( err )
474     {
475         errno = err;
476         return (size_t)(-1);
477     }
478
479     return (size_t)(0);
480
481 }
482 #endif
483
484 /*****************************************************************************
485  * iconv wrapper
486  *****************************************************************************/
487 vlc_iconv_t vlc_iconv_open( const char *tocode, const char *fromcode )
488 {
489 #ifndef __linux__
490     if( !strcasecmp(tocode, "UTF-8") && !strcasecmp(fromcode, "ISO_6937") )
491         return (vlc_iconv_t)(-2);
492 #endif
493 #if defined(HAVE_ICONV)
494     return iconv_open( tocode, fromcode );
495 #else
496     return (vlc_iconv_t)(-1);
497 #endif
498 }
499
500 size_t vlc_iconv( vlc_iconv_t cd, const char **inbuf, size_t *inbytesleft,
501                   char **outbuf, size_t *outbytesleft )
502 {
503 #ifndef __linux__
504     if ( cd == (vlc_iconv_t)(-2) )
505         return ISO6937toUTF8( (const unsigned char **)inbuf, inbytesleft,
506                               (unsigned char **)outbuf, outbytesleft );
507 #endif
508 #if defined(HAVE_ICONV)
509     return iconv( cd, (ICONV_CONST char **)inbuf, inbytesleft,
510                   outbuf, outbytesleft );
511 #else
512     abort ();
513 #endif
514 }
515
516 int vlc_iconv_close( vlc_iconv_t cd )
517 {
518 #ifndef __linux__
519     if ( cd == (vlc_iconv_t)(-2) )
520         return 0;
521 #endif
522 #if defined(HAVE_ICONV)
523     return iconv_close( cd );
524 #else
525     abort ();
526 #endif
527 }
528
529 /*****************************************************************************
530  * reduce a fraction
531  *   (adapted from libavcodec, author Michael Niedermayer <michaelni@gmx.at>)
532  *****************************************************************************/
533 bool vlc_ureduce( unsigned *pi_dst_nom, unsigned *pi_dst_den,
534                         uint64_t i_nom, uint64_t i_den, uint64_t i_max )
535 {
536     bool b_exact = 1;
537     uint64_t i_gcd;
538
539     if( i_den == 0 )
540     {
541         *pi_dst_nom = 0;
542         *pi_dst_den = 1;
543         return 1;
544     }
545
546     i_gcd = GCD( i_nom, i_den );
547     i_nom /= i_gcd;
548     i_den /= i_gcd;
549
550     if( i_max == 0 ) i_max = INT64_C(0xFFFFFFFF);
551
552     if( i_nom > i_max || i_den > i_max )
553     {
554         uint64_t i_a0_num = 0, i_a0_den = 1, i_a1_num = 1, i_a1_den = 0;
555         b_exact = 0;
556
557         for( ; ; )
558         {
559             uint64_t i_x = i_nom / i_den;
560             uint64_t i_a2n = i_x * i_a1_num + i_a0_num;
561             uint64_t i_a2d = i_x * i_a1_den + i_a0_den;
562
563             if( i_a2n > i_max || i_a2d > i_max ) break;
564
565             i_nom %= i_den;
566
567             i_a0_num = i_a1_num; i_a0_den = i_a1_den;
568             i_a1_num = i_a2n; i_a1_den = i_a2d;
569             if( i_nom == 0 ) break;
570             i_x = i_nom; i_nom = i_den; i_den = i_x;
571         }
572         i_nom = i_a1_num;
573         i_den = i_a1_den;
574     }
575
576     *pi_dst_nom = i_nom;
577     *pi_dst_den = i_den;
578
579     return b_exact;
580 }
581
582 #undef vlc_execve
583 /*************************************************************************
584  * vlc_execve: Execute an external program with a given environment,
585  * wait until it finishes and return its standard output
586  *************************************************************************/
587 int vlc_execve( vlc_object_t *p_object, int i_argc, char *const *ppsz_argv,
588                 char *const *ppsz_env, const char *psz_cwd,
589                 const char *p_in, size_t i_in,
590                 char **pp_data, size_t *pi_data )
591 {
592     (void)i_argc; // <-- hmph
593 #ifdef HAVE_FORK
594 # define BUFSIZE 1024
595     int fds[2], i_status;
596
597     if (socketpair (PF_LOCAL, SOCK_STREAM, 0, fds))
598         return -1;
599
600     pid_t pid = -1;
601     if ((fds[0] > 2) && (fds[1] > 2))
602         pid = fork ();
603
604     switch (pid)
605     {
606         case -1:
607             msg_Err (p_object, "unable to fork (%m)");
608             close (fds[0]);
609             close (fds[1]);
610             return -1;
611
612         case 0:
613         {
614             sigset_t set;
615             sigemptyset (&set);
616             pthread_sigmask (SIG_SETMASK, &set, NULL);
617
618             /* NOTE:
619              * Like it or not, close can fail (and not only with EBADF)
620              */
621             if ((dup2 (fds[1], 0) == 0) && (dup2 (fds[1], 1) == 1)
622              && ((psz_cwd == NULL) || (chdir (psz_cwd) == 0)))
623                 execve (ppsz_argv[0], ppsz_argv, ppsz_env);
624
625             _exit (EXIT_FAILURE);
626         }
627     }
628
629     close (fds[1]);
630
631     *pi_data = 0;
632     if (*pp_data)
633         free (*pp_data);
634     *pp_data = NULL;
635
636     if (i_in == 0)
637         shutdown (fds[0], SHUT_WR);
638
639     while (!p_object->b_die)
640     {
641         struct pollfd ufd[1];
642         memset (ufd, 0, sizeof (ufd));
643         ufd[0].fd = fds[0];
644         ufd[0].events = POLLIN;
645
646         if (i_in > 0)
647             ufd[0].events |= POLLOUT;
648
649         if (poll (ufd, 1, 10) <= 0)
650             continue;
651
652         if (ufd[0].revents & ~POLLOUT)
653         {
654             char *ptr = realloc (*pp_data, *pi_data + BUFSIZE + 1);
655             if (ptr == NULL)
656                 break; /* safely abort */
657
658             *pp_data = ptr;
659
660             ssize_t val = read (fds[0], ptr + *pi_data, BUFSIZE);
661             switch (val)
662             {
663                 case -1:
664                 case 0:
665                     shutdown (fds[0], SHUT_RD);
666                     break;
667
668                 default:
669                     *pi_data += val;
670             }
671         }
672
673         if (ufd[0].revents & POLLOUT)
674         {
675             ssize_t val = write (fds[0], p_in, i_in);
676             switch (val)
677             {
678                 case -1:
679                 case 0:
680                     i_in = 0;
681                     shutdown (fds[0], SHUT_WR);
682                     break;
683
684                 default:
685                     i_in -= val;
686                     p_in += val;
687             }
688         }
689     }
690
691     close (fds[0]);
692
693     while (waitpid (pid, &i_status, 0) == -1);
694
695     if (WIFEXITED (i_status))
696     {
697         i_status = WEXITSTATUS (i_status);
698         if (i_status)
699             msg_Warn (p_object,  "child %s (PID %d) exited with error code %d",
700                       ppsz_argv[0], (int)pid, i_status);
701     }
702     else
703     if (WIFSIGNALED (i_status)) // <-- this should be redumdant a check
704     {
705         i_status = WTERMSIG (i_status);
706         msg_Warn (p_object, "child %s (PID %d) exited on signal %d (%s)",
707                   ppsz_argv[0], (int)pid, i_status, strsignal (i_status));
708     }
709
710 #elif defined( WIN32 ) && !defined( UNDER_CE )
711     SECURITY_ATTRIBUTES saAttr;
712     PROCESS_INFORMATION piProcInfo;
713     STARTUPINFO siStartInfo;
714     BOOL bFuncRetn = FALSE;
715     HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr;
716     DWORD i_status;
717     char *psz_cmd = NULL, *p_env = NULL, *p = NULL;
718     char **ppsz_parser;
719     int i_size;
720
721     /* Set the bInheritHandle flag so pipe handles are inherited. */
722     saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
723     saAttr.bInheritHandle = TRUE;
724     saAttr.lpSecurityDescriptor = NULL;
725
726     /* Create a pipe for the child process's STDOUT. */
727     if ( !CreatePipe( &hChildStdoutRd, &hChildStdoutWr, &saAttr, 0 ) )
728     {
729         msg_Err( p_object, "stdout pipe creation failed" );
730         return -1;
731     }
732
733     /* Ensure the read handle to the pipe for STDOUT is not inherited. */
734     SetHandleInformation( hChildStdoutRd, HANDLE_FLAG_INHERIT, 0 );
735
736     /* Create a pipe for the child process's STDIN. */
737     if ( !CreatePipe( &hChildStdinRd, &hChildStdinWr, &saAttr, 0 ) )
738     {
739         msg_Err( p_object, "stdin pipe creation failed" );
740         return -1;
741     }
742
743     /* Ensure the write handle to the pipe for STDIN is not inherited. */
744     SetHandleInformation( hChildStdinWr, HANDLE_FLAG_INHERIT, 0 );
745
746     /* Set up members of the PROCESS_INFORMATION structure. */
747     ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION) );
748
749     /* Set up members of the STARTUPINFO structure. */
750     ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) );
751     siStartInfo.cb = sizeof(STARTUPINFO);
752     siStartInfo.hStdError = hChildStdoutWr;
753     siStartInfo.hStdOutput = hChildStdoutWr;
754     siStartInfo.hStdInput = hChildStdinRd;
755     siStartInfo.wShowWindow = SW_HIDE;
756     siStartInfo.dwFlags |= STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
757
758     /* Set up the command line. */
759     psz_cmd = malloc(32768);
760     if( !psz_cmd )
761         return -1;
762     psz_cmd[0] = '\0';
763     i_size = 32768;
764     ppsz_parser = &ppsz_argv[0];
765     while ( ppsz_parser[0] != NULL && i_size > 0 )
766     {
767         /* Protect the last argument with quotes ; the other arguments
768          * are supposed to be already protected because they have been
769          * passed as a command-line option. */
770         if ( ppsz_parser[1] == NULL )
771         {
772             strncat( psz_cmd, "\"", i_size );
773             i_size--;
774         }
775         strncat( psz_cmd, *ppsz_parser, i_size );
776         i_size -= strlen( *ppsz_parser );
777         if ( ppsz_parser[1] == NULL )
778         {
779             strncat( psz_cmd, "\"", i_size );
780             i_size--;
781         }
782         strncat( psz_cmd, " ", i_size );
783         i_size--;
784         ppsz_parser++;
785     }
786
787     /* Set up the environment. */
788     p = p_env = malloc(32768);
789     if( !p )
790     {
791         free( psz_cmd );
792         return -1;
793     }
794
795     i_size = 32768;
796     ppsz_parser = &ppsz_env[0];
797     while ( *ppsz_parser != NULL && i_size > 0 )
798     {
799         memcpy( p, *ppsz_parser,
800                 __MIN((int)(strlen(*ppsz_parser) + 1), i_size) );
801         p += strlen(*ppsz_parser) + 1;
802         i_size -= strlen(*ppsz_parser) + 1;
803         ppsz_parser++;
804     }
805     *p = '\0';
806
807     /* Create the child process. */
808     bFuncRetn = CreateProcess( NULL,
809           psz_cmd,       // command line
810           NULL,          // process security attributes
811           NULL,          // primary thread security attributes
812           TRUE,          // handles are inherited
813           0,             // creation flags
814           p_env,
815           psz_cwd,
816           &siStartInfo,  // STARTUPINFO pointer
817           &piProcInfo ); // receives PROCESS_INFORMATION
818
819     free( psz_cmd );
820     free( p_env );
821
822     if ( bFuncRetn == 0 )
823     {
824         msg_Err( p_object, "child creation failed" );
825         return -1;
826     }
827
828     /* Read from a file and write its contents to a pipe. */
829     while ( i_in > 0 && !p_object->b_die )
830     {
831         DWORD i_written;
832         if ( !WriteFile( hChildStdinWr, p_in, i_in, &i_written, NULL ) )
833             break;
834         i_in -= i_written;
835         p_in += i_written;
836     }
837
838     /* Close the pipe handle so the child process stops reading. */
839     CloseHandle(hChildStdinWr);
840
841     /* Close the write end of the pipe before reading from the
842      * read end of the pipe. */
843     CloseHandle(hChildStdoutWr);
844
845     /* Read output from the child process. */
846     *pi_data = 0;
847     if( *pp_data )
848         free( pp_data );
849     *pp_data = NULL;
850     *pp_data = malloc( 1025 );  /* +1 for \0 */
851
852     while ( !p_object->b_die )
853     {
854         DWORD i_read;
855         if ( !ReadFile( hChildStdoutRd, &(*pp_data)[*pi_data], 1024, &i_read,
856                         NULL )
857               || i_read == 0 )
858             break;
859         *pi_data += i_read;
860         *pp_data = xrealloc( *pp_data, *pi_data + 1025 );
861     }
862
863     while ( !p_object->b_die
864              && !GetExitCodeProcess( piProcInfo.hProcess, &i_status )
865              && i_status != STILL_ACTIVE )
866         msleep( 10000 );
867
868     CloseHandle(piProcInfo.hProcess);
869     CloseHandle(piProcInfo.hThread);
870
871     if ( i_status )
872         msg_Warn( p_object,
873                   "child %s returned with error code %ld",
874                   ppsz_argv[0], i_status );
875
876 #else
877     msg_Err( p_object, "vlc_execve called but no implementation is available" );
878     return -1;
879
880 #endif
881
882     if (*pp_data == NULL)
883         return -1;
884
885     (*pp_data)[*pi_data] = '\0';
886     return 0;
887 }