2 wpathconv: static functions for windows file path conversions
4 This file is intended to be included in libcompat sources for internal use.
5 It is separated out to be able to split off the dlopen functions into a
6 separate libcompat. It is just a code fragment.
8 copyright 2007-2019 by the mpg123 project - free software under the terms of the LGPL 2.1
9 see COPYING and AUTHORS files in distribution or http://mpg123.org
10 initially written by JonY
15 #ifdef WANT_WIN32_UNICODE
17 /* Convert unix UTF-8 (or ASCII) paths to Windows wide character paths. */
18 static wchar_t* u2wpath(const char *upath
)
21 if(!upath
|| INT123_win32_utf8_wide(upath
, &wpath
, NULL
) < 1)
30 /* Convert Windows wide character paths to unix UTF-8. */
31 static char* w2upath(const wchar_t *wpath
)
34 if(!wpath
|| INT123_win32_wide_utf8(wpath
, &upath
, NULL
) < 1)
43 /* An absolute path that is too long and not already marked with
44 \\?\ can be marked as a long one and still work. */
45 static int wpath_need_elongation(wchar_t *wpath
)
47 if( wpath
&& !PathIsRelativeW(wpath
)
48 && wcslen(wpath
) > MAX_PATH
-1
49 && wcsncmp(L
"\\\\?\\", wpath
, 4) )
55 /* Take any wide windows path and turn it into a path that is allowed
56 to be longer than MAX_PATH, if it is not already. */
57 static wchar_t* wlongpath(wchar_t *wpath
)
60 const wchar_t *prefix
= L
"";
61 wchar_t *wlpath
= NULL
;
65 /* Absolute paths that do not start with \\?\ get that prepended
66 to allow them being long. */
67 if(!PathIsRelativeW(wpath
) && wcsncmp(L
"\\\\?\\", wpath
, 4))
69 if(wcslen(wpath
) >= 2 && PathIsUNCW(wpath
))
71 /* \\server\path -> \\?\UNC\server\path */
72 prefix
= L
"\\\\?\\UNC";
73 ++wpath
; /* Skip the first \. */
75 else /* c:\some/path -> \\?\c:\some\path */
78 plen
= wcslen(prefix
);
79 len
= plen
+ wcslen(wpath
);
80 wlpath
= malloc(len
+1*sizeof(wchar_t));
83 /* Brute force memory copying, swprintf is too dandy. */
84 memcpy(wlpath
, prefix
, sizeof(wchar_t)*plen
);
85 memcpy(wlpath
+plen
, wpath
, sizeof(wchar_t)*(len
-plen
));
91 /* Convert unix path to wide windows path, optionally marking
92 it as long path if necessary. */
93 static wchar_t* u2wlongpath(const char *upath
)
95 wchar_t *wpath
= NULL
;
96 wchar_t *wlpath
= NULL
;
97 wpath
= u2wpath(upath
);
98 if(wpath_need_elongation(wpath
))
100 wlpath
= wlongpath(wpath
);
111 static wchar_t* u2wlongpath(const char *upath
)
114 if (!upath
|| INT123_win32_utf8_wide(upath
, &wpath
, NULL
) < 1)
116 for (p
= wpath
; *p
; ++p
)