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