2323# include "config.h"
2424#endif
2525
26- #ifdef HAVE_JOLIET
2726#ifdef HAVE_STRING_H
2827# include <string.h>
2928#endif
4342#include <stdio.h>
4443#endif
4544
46- // TODO: also remove the need for iconv on MinGW
45+ /* Windows requires some basic UTF-8 support outside of Joliet */
46+ #if defined(_WIN32 )
47+ #include <windows.h>
48+
49+ #define wchar_to_utf8_no_alloc (wsrc , dest , dest_size ) \
50+ WideCharToMultiByte(CP_UTF8, 0, wsrc, -1, dest, dest_size, NULL, NULL)
51+ #define utf8_to_wchar_no_alloc (src , wdest , wdest_size ) \
52+ MultiByteToWideChar(CP_UTF8, 0, src, -1, wdest, wdest_size)
53+
54+ /*
55+ * Converts an UTF-16 string to UTF8 (allocate returned string)
56+ * Returns NULL on error
57+ */
58+ char * cdio_wchar_to_utf8 (const wchar_t * wstr )
59+ {
60+ int size = 0 ;
61+ char * str = NULL ;
62+
63+ /* Find out the size we need to allocate for our converted string */
64+ size = WideCharToMultiByte (CP_UTF8 , 0 , wstr , -1 , NULL , 0 , NULL , NULL );
65+ if (size <= 1 ) /* An empty string would be size 1 */
66+ return NULL ;
67+
68+ if ((str = (char * )calloc (size , 1 )) == NULL )
69+ return NULL ;
70+
71+ if (wchar_to_utf8_no_alloc (wstr , str , size ) != size ) {
72+ free (str );
73+ return NULL ;
74+ }
75+
76+ return str ;
77+ }
78+
79+ /*
80+ * Converts an UTF8 string to UTF-16 (allocate returned string)
81+ * Returns NULL on error
82+ */
83+ wchar_t * cdio_utf8_to_wchar (const char * str )
84+ {
85+ int size = 0 ;
86+ wchar_t * wstr = NULL ;
87+
88+ /* Find out the size we need to allocate for our converted string */
89+ size = MultiByteToWideChar (CP_UTF8 , 0 , str , -1 , NULL , 0 );
90+ if (size <= 1 ) /* An empty string would be size 1 */
91+ return NULL ;
92+
93+ if ((wstr = (wchar_t * )calloc (size , sizeof (wchar_t ))) == NULL )
94+ return NULL ;
95+
96+ if (utf8_to_wchar_no_alloc (str , wstr , size ) != size ) {
97+ free (wstr );
98+ return NULL ;
99+ }
100+ return wstr ;
101+ }
102+ #endif
103+
104+ #ifdef HAVE_JOLIET
47105#ifdef HAVE_ICONV
48106#include <iconv.h>
49107struct cdio_charset_coverter_s
@@ -210,61 +268,6 @@ bool cdio_charset_to_utf8(char *src, size_t src_len, cdio_utf8_t **dst,
210268 return result ;
211269 }
212270#elif defined(_WIN32 )
213- #include <windows.h>
214-
215- #define wchar_to_utf8_no_alloc (wsrc , dest , dest_size ) \
216- WideCharToMultiByte(CP_UTF8, 0, wsrc, -1, dest, dest_size, NULL, NULL)
217- #define utf8_to_wchar_no_alloc (src , wdest , wdest_size ) \
218- MultiByteToWideChar(CP_UTF8, 0, src, -1, wdest, wdest_size)
219-
220- /*
221- * Converts an UTF-16 string to UTF8 (allocate returned string)
222- * Returns NULL on error
223- */
224- static inline char * wchar_to_utf8 (const wchar_t * wstr )
225- {
226- int size = 0 ;
227- char * str = NULL ;
228-
229- /* Find out the size we need to allocate for our converted string */
230- size = WideCharToMultiByte (CP_UTF8 , 0 , wstr , -1 , NULL , 0 , NULL , NULL );
231- if (size <= 1 ) /* An empty string would be size 1 */
232- return NULL ;
233-
234- if ((str = (char * )calloc (size , 1 )) == NULL )
235- return NULL ;
236-
237- if (wchar_to_utf8_no_alloc (wstr , str , size ) != size ) {
238- free (str );
239- return NULL ;
240- }
241-
242- return str ;
243- }
244-
245- /*
246- * Converts an UTF8 string to UTF-16 (allocate returned string)
247- * Returns NULL on error
248- */
249- static inline wchar_t * utf8_to_wchar (const char * str )
250- {
251- int size = 0 ;
252- wchar_t * wstr = NULL ;
253-
254- /* Find out the size we need to allocate for our converted string */
255- size = MultiByteToWideChar (CP_UTF8 , 0 , str , -1 , NULL , 0 );
256- if (size <= 1 ) /* An empty string would be size 1 */
257- return NULL ;
258-
259- if ((wstr = (wchar_t * )calloc (size , sizeof (wchar_t ))) == NULL )
260- return NULL ;
261-
262- if (utf8_to_wchar_no_alloc (str , wstr , size ) != size ) {
263- free (wstr );
264- return NULL ;
265- }
266- return wstr ;
267- }
268271
269272bool cdio_charset_from_utf8 (cdio_utf8_t * src , char * * dst ,
270273 int * dst_len , const char * dst_charset )
@@ -276,7 +279,7 @@ bool cdio_charset_from_utf8(cdio_utf8_t * src, char ** dst,
276279 return false;
277280
278281 /* Eliminate empty strings */
279- le_dst = utf8_to_wchar (src );
282+ le_dst = cdio_utf8_to_wchar (src );
280283 if ((le_dst == NULL ) || (le_dst [0 ] == 0 )) {
281284 free (le_dst );
282285 return false;
@@ -323,7 +326,7 @@ bool cdio_charset_to_utf8(char *src, size_t src_len, cdio_utf8_t **dst,
323326 ((char * )le_src )[2 * i + 1 ] = src [2 * i ];
324327 }
325328 le_src [src_len ] = 0 ;
326- * dst = wchar_to_utf8 (le_src );
329+ * dst = cdio_wchar_to_utf8 (le_src );
327330 free (le_src );
328331
329332 return (* dst != NULL );
0 commit comments