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