Debian Codegraph @

src/compat/wpathconv.h

raw
1 /*
2 wpathconv: static functions for windows file path conversions
3
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.
7
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
11 */
12
13 #ifndef WINDOWS_UWP
14
15 #ifdef WANT_WIN32_UNICODE
16
17 /* Convert unix UTF-8 (or ASCII) paths to Windows wide character paths. */
18 static wchar_t* u2wpath(const char *upath)
19 {
20 wchar_t* wpath, *p;
21 if(!upath || INT123_win32_utf8_wide(upath, &wpath, NULL) < 1)
22 return NULL;
23 for(p=wpath; *p; ++p)
24 if(*p == L'/')
25 *p = L'\\';
26 return wpath;
27 }
28
29 #ifndef HIDE_w2upath
30 /* Convert Windows wide character paths to unix UTF-8. */
31 static char* w2upath(const wchar_t *wpath)
32 {
33 char* upath, *p;
34 if(!wpath || INT123_win32_wide_utf8(wpath, &upath, NULL) < 1)
35 return NULL;
36 for(p=upath; *p; ++p)
37 if(*p == '\\')
38 *p = '/';
39 return upath;
40 }
41 #endif
42
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)
46 {
47 if( wpath && !PathIsRelativeW(wpath)
48 && wcslen(wpath) > MAX_PATH-1
49 && wcsncmp(L"\\\\?\\", wpath, 4) )
50 return 1;
51 else
52 return 0;
53 }
54
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)
58 {
59 size_t len, plen;
60 const wchar_t *prefix = L"";
61 wchar_t *wlpath = NULL;
62 if(!wpath)
63 return NULL;
64
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))
68 {
69 if(wcslen(wpath) >= 2 && PathIsUNCW(wpath))
70 {
71 /* \\server\path -> \\?\UNC\server\path */
72 prefix = L"\\\\?\\UNC";
73 ++wpath; /* Skip the first \. */
74 }
75 else /* c:\some/path -> \\?\c:\some\path */
76 prefix = L"\\\\?\\";
77 }
78 plen = wcslen(prefix);
79 len = plen + wcslen(wpath);
80 wlpath = malloc(len+1*sizeof(wchar_t));
81 if(wlpath)
82 {
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));
86 wlpath[len] = 0;
87 }
88 return wlpath;
89 }
90
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)
94 {
95 wchar_t *wpath = NULL;
96 wchar_t *wlpath = NULL;
97 wpath = u2wpath(upath);
98 if(wpath_need_elongation(wpath))
99 {
100 wlpath = wlongpath(wpath);
101 free(wpath);
102 wpath = wlpath;
103 }
104 return wpath;
105 }
106
107 #endif
108
109 #else
110
111 static wchar_t* u2wlongpath(const char *upath)
112 {
113 wchar_t* wpath, *p;
114 if (!upath || INT123_win32_utf8_wide(upath, &wpath, NULL) < 1)
115 return NULL;
116 for (p = wpath; *p; ++p)
117 if (*p == L'/')
118 *p = L'\\';
119 return wpath;
120 }
121
122 #endif