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