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