Repository navigation
Expand file tree
/
Copy pathcunicode.cpp
More file actions
346 lines (318 loc) · 11.2 KB
/
Copy pathcunicode.cpp
File metadata and controls
346 lines (318 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#include <windows.h>
#include "fsplugin.h"
#include "cunicode.h"
extern tProgressProc ProgressProc;
extern tLogProc LogProc;
extern tRequestProc RequestProc;
extern tProgressProcW ProgressProcW;
extern tLogProcW LogProcW;
extern tRequestProcW RequestProcW;
BOOL usys()
{
return TRUE; // Win9x is gone; always use the Unicode NT APIs
}
void copyfinddatawa(WIN32_FIND_DATA *lpFindFileDataA,WIN32_FIND_DATAW *lpFindFileDataW)
{
if (!lpFindFileDataA || !lpFindFileDataW)
return;
lpFindFileDataA->dwFileAttributes=lpFindFileDataW->dwFileAttributes;
lpFindFileDataA->ftCreationTime=lpFindFileDataW->ftCreationTime;
lpFindFileDataA->ftLastAccessTime=lpFindFileDataW->ftLastAccessTime;
lpFindFileDataA->ftLastWriteTime=lpFindFileDataW->ftLastWriteTime;
lpFindFileDataA->nFileSizeHigh=lpFindFileDataW->nFileSizeHigh;
lpFindFileDataA->nFileSizeLow=lpFindFileDataW->nFileSizeLow;
lpFindFileDataA->dwReserved0=lpFindFileDataW->dwReserved0;
lpFindFileDataA->dwReserved1=lpFindFileDataW->dwReserved1;
walcopy(lpFindFileDataA->cFileName,lpFindFileDataW->cFileName,countof(lpFindFileDataA->cFileName)-1);
walcopy(lpFindFileDataA->cAlternateFileName,lpFindFileDataW->cAlternateFileName,countof(lpFindFileDataA->cAlternateFileName)-1);
}
void copyfinddataaw(WIN32_FIND_DATAW *lpFindFileDataW,WIN32_FIND_DATA *lpFindFileDataA)
{
if (!lpFindFileDataA || !lpFindFileDataW)
return;
lpFindFileDataW->dwFileAttributes=lpFindFileDataA->dwFileAttributes;
lpFindFileDataW->ftCreationTime=lpFindFileDataA->ftCreationTime;
lpFindFileDataW->ftLastAccessTime=lpFindFileDataA->ftLastAccessTime;
lpFindFileDataW->ftLastWriteTime=lpFindFileDataA->ftLastWriteTime;
lpFindFileDataW->nFileSizeHigh=lpFindFileDataA->nFileSizeHigh;
lpFindFileDataW->nFileSizeLow=lpFindFileDataA->nFileSizeLow;
lpFindFileDataW->dwReserved0=lpFindFileDataA->dwReserved0;
lpFindFileDataW->dwReserved1=lpFindFileDataA->dwReserved1;
awlcopy(lpFindFileDataW->cFileName,lpFindFileDataA->cFileName,countof(lpFindFileDataW->cFileName)-1);
awlcopy(lpFindFileDataW->cAlternateFileName,lpFindFileDataA->cAlternateFileName,countof(lpFindFileDataW->cAlternateFileName)-1);
}
char* walcopy(char* outname,WCHAR* inname,int maxlen)
{
if (inname) {
WideCharToMultiByte(CP_ACP,0,inname,-1,outname,maxlen,NULL,NULL);
outname[maxlen]=0;
return outname;
} else
return NULL;
}
WCHAR* awlcopy(WCHAR* outname,char* inname,int maxlen)
{
if (inname) {
MultiByteToWideChar(CP_ACP,0,inname,-1,outname,maxlen);
outname[maxlen]=0;
return outname;
} else
return NULL;
}
WCHAR* wcslcpy(WCHAR *str1,const WCHAR *str2,int imaxlen)
{
if ((int)wcslen(str2)>=imaxlen-1) {
wcsncpy_s(str1,imaxlen,str2,imaxlen-1);
str1[imaxlen-1]=0;
} else
wcscpy_s(str1,imaxlen,str2);
return str1;
}
WCHAR* wcslcat(wchar_t *str1,const WCHAR *str2,int imaxlen)
{
int l1=(int)wcslen(str1);
if ((int)wcslen(str2)+l1>=imaxlen-1) {
wcsncpy_s(str1+l1,imaxlen,str2,imaxlen-1-l1);
str1[imaxlen-1]=0;
} else
wcscat_s(str1,imaxlen,str2);
return str1;
}
// return true if name wasn't cut
BOOL MakeExtraLongNameW(WCHAR* outbuf,const WCHAR* inbuf,int maxlen)
{
if (wcslen(inbuf)>259) {
if (inbuf[0]=='\\' && inbuf[1]=='\\') { // UNC-Path! Use \\?\UNC\server\share\subdir\name.ext
wcslcpy(outbuf,L"\\\\?\\UNC",maxlen);
wcslcat(outbuf,inbuf+1,maxlen);
} else {
wcslcpy(outbuf,L"\\\\?\\",maxlen);
wcslcat(outbuf,inbuf,maxlen);
}
} else
wcslcpy(outbuf,inbuf,maxlen);
return (int)wcslen(inbuf)+4<=maxlen;
}
/***********************************************************************************************/
/***********************************************************************************************/
int ProgressProcT(int PluginNr,WCHAR* SourceName,WCHAR* TargetName,int PercentDone)
{
if (ProgressProcW) {
return ProgressProcW(PluginNr,SourceName,TargetName,PercentDone);
} else if (ProgressProc) {
char buf1[MAX_PATH],buf2[MAX_PATH];
return ProgressProc(PluginNr,wafilenamecopy(buf1,SourceName),wafilenamecopy(buf2,TargetName),PercentDone);
} else
return 0;
}
void LogProcT(int PluginNr,int MsgType,WCHAR* LogString)
{
if (LogProcW) {
LogProcW(PluginNr,MsgType,LogString);
} else if (LogProc) {
char buf[1024];
LogProc(PluginNr,MsgType,walcopy(buf,LogString,sizeof(buf)-1));
}
}
BOOL RequestProcT(int PluginNr,int RequestType,WCHAR* CustomTitle,
WCHAR* CustomText,WCHAR* ReturnedText,int maxlen)
{
if (RequestProcW) {
return RequestProcW(PluginNr,RequestType,CustomTitle,
CustomText,ReturnedText,maxlen);
} else if (LogProc) {
char buf1[MAX_PATH],buf2[MAX_PATH],buf3[MAX_PATH];
char* preturn=wafilenamecopy(buf3,ReturnedText);
BOOL retval=RequestProc(PluginNr,RequestType,wafilenamecopy(buf1,CustomTitle),
wafilenamecopy(buf2,CustomText),preturn,maxlen);
if (retval && preturn)
awlcopy(ReturnedText,preturn,maxlen);
return retval;
} else
return false;
}
BOOL CopyFileT(WCHAR* lpExistingFileName,WCHAR* lpNewFileName,BOOL bFailIfExists)
{
if (usys()) {
WCHAR wbuf1[wdirtypemax+longnameprefixmax],wbuf2[wdirtypemax+longnameprefixmax];
if (MakeExtraLongNameW(wbuf1,lpExistingFileName,wdirtypemax-1+longnameprefixmax) &&
MakeExtraLongNameW(wbuf2,lpNewFileName,wdirtypemax-1+longnameprefixmax))
return CopyFileW(wbuf1,wbuf2,bFailIfExists);
else
return false;
} else {
char buf1[MAX_PATH],buf2[MAX_PATH];
return CopyFile(wafilenamecopy(buf1,lpExistingFileName),wafilenamecopy(buf2,lpNewFileName),bFailIfExists);
}
}
BOOL CreateDirectoryT(WCHAR* lpPathName,LPSECURITY_ATTRIBUTES lpSecurityAttributes)
{
if (usys()) {
WCHAR wbuf[wdirtypemax+longnameprefixmax];
if (MakeExtraLongNameW(wbuf,lpPathName,wdirtypemax-1+longnameprefixmax))
return CreateDirectoryW(wbuf,lpSecurityAttributes);
else
return false;
} else {
char buf[MAX_PATH];
return CreateDirectory(wafilenamecopy(buf,lpPathName),lpSecurityAttributes);
}
}
BOOL RemoveDirectoryT(WCHAR* lpPathName)
{
if (usys()) {
WCHAR wbuf[wdirtypemax+longnameprefixmax];
if (MakeExtraLongNameW(wbuf,lpPathName,wdirtypemax-1+longnameprefixmax))
return RemoveDirectoryW(wbuf);
else
return false;
} else {
char buf[MAX_PATH];
return RemoveDirectory(wafilenamecopy(buf,lpPathName));
}
}
BOOL DeleteFileT(WCHAR* lpFileName)
{
if (usys()) {
WCHAR wbuf[wdirtypemax+longnameprefixmax];
if (MakeExtraLongNameW(wbuf,lpFileName,wdirtypemax-1+longnameprefixmax))
return DeleteFileW(wbuf);
else
return false;
} else {
char buf[MAX_PATH];
return DeleteFile(wafilenamecopy(buf,lpFileName));
}
}
BOOL MoveFileT(WCHAR* lpExistingFileName,WCHAR* lpNewFileName)
{
if (usys()) {
WCHAR wbuf1[wdirtypemax+longnameprefixmax],wbuf2[wdirtypemax+longnameprefixmax];
if (MakeExtraLongNameW(wbuf1,lpExistingFileName,wdirtypemax-1+longnameprefixmax) &&
MakeExtraLongNameW(wbuf2,lpNewFileName,wdirtypemax-1+longnameprefixmax))
return MoveFileW(wbuf1,wbuf2);
else
return false;
} else {
char buf1[MAX_PATH],buf2[MAX_PATH];
return MoveFile(wafilenamecopy(buf1,lpExistingFileName),wafilenamecopy(buf2,lpNewFileName));
}
}
BOOL SetFileAttributesT(WCHAR* lpFileName,DWORD dwFileAttributes)
{
if (usys()) {
WCHAR wbuf[wdirtypemax+longnameprefixmax];
if (MakeExtraLongNameW(wbuf,lpFileName,wdirtypemax-1+longnameprefixmax))
return SetFileAttributesW(wbuf,dwFileAttributes);
else
return false;
} else {
char buf[MAX_PATH];
return SetFileAttributes(wafilenamecopy(buf,lpFileName),dwFileAttributes);
}
}
HANDLE CreateFileT(WCHAR* lpFileName,DWORD dwDesiredAccess,DWORD dwShareMode,
LPSECURITY_ATTRIBUTES lpSecurityAttributes,DWORD dwCreationDisposition,
DWORD dwFlagsAndAttributes,HANDLE hTemplateFile)
{
if (usys()) {
WCHAR wbuf[wdirtypemax+longnameprefixmax];
if (MakeExtraLongNameW(wbuf,lpFileName,wdirtypemax-1+longnameprefixmax))
return CreateFileW(wbuf,dwDesiredAccess,dwShareMode,
lpSecurityAttributes,dwCreationDisposition,
dwFlagsAndAttributes,hTemplateFile);
else
return INVALID_HANDLE_VALUE;
} else {
char buf[MAX_PATH];
return CreateFile(wafilenamecopy(buf,lpFileName),dwDesiredAccess,dwShareMode,
lpSecurityAttributes,dwCreationDisposition,
dwFlagsAndAttributes,hTemplateFile);
}
}
UINT ExtractIconExT(WCHAR* lpszFile,int nIconIndex,HICON *phiconLarge,HICON *phiconSmall,UINT nIcons)
{
if (usys()) { // Unfortunately this function cannot handle names longer than 259 characters
return ExtractIconExW(lpszFile,nIconIndex,phiconLarge,phiconSmall,nIcons);
} else {
char buf[MAX_PATH];
return ExtractIconEx(wafilenamecopy(buf,lpszFile),nIconIndex,phiconLarge,phiconSmall,nIcons);
}
}
HANDLE FindFirstFileT(WCHAR* lpFileName,LPWIN32_FIND_DATAW lpFindFileData)
{
if (usys()) {
WCHAR wbuf[wdirtypemax+longnameprefixmax];
if (MakeExtraLongNameW(wbuf,lpFileName,wdirtypemax-1+longnameprefixmax))
return FindFirstFileW(wbuf,lpFindFileData);
else
return INVALID_HANDLE_VALUE;
} else {
char buf[MAX_PATH];
WIN32_FIND_DATA FindFileDataA;
HANDLE retval=FindFirstFile(wafilenamecopy(buf,lpFileName),&FindFileDataA);
if (retval!=INVALID_HANDLE_VALUE) {
awlcopy(lpFindFileData->cAlternateFileName,FindFileDataA.cAlternateFileName,countof(lpFindFileData->cAlternateFileName)-1);
awlcopy(lpFindFileData->cFileName,FindFileDataA.cFileName,countof(lpFindFileData->cFileName)-1);
lpFindFileData->dwFileAttributes=FindFileDataA.dwFileAttributes;
lpFindFileData->dwReserved0=FindFileDataA.dwReserved0;
lpFindFileData->dwReserved1=FindFileDataA.dwReserved1;
lpFindFileData->ftCreationTime=FindFileDataA.ftCreationTime;
lpFindFileData->ftLastAccessTime=FindFileDataA.ftLastAccessTime;
lpFindFileData->ftLastWriteTime=FindFileDataA.ftLastWriteTime;
lpFindFileData->nFileSizeHigh=FindFileDataA.nFileSizeHigh;
lpFindFileData->nFileSizeLow=FindFileDataA.nFileSizeLow;
}
return retval;
}
}
BOOL FindNextFileT(HANDLE hFindFile,LPWIN32_FIND_DATAW lpFindFileData)
{
if (usys()) {
return FindNextFileW(hFindFile,lpFindFileData);
} else {
WIN32_FIND_DATA FindFileDataA;
memset(&FindFileDataA,0,sizeof(FindFileDataA));
BOOL retval=FindNextFile(hFindFile,&FindFileDataA);
if (retval) {
awlcopy(lpFindFileData->cAlternateFileName,FindFileDataA.cAlternateFileName,countof(lpFindFileData->cAlternateFileName)-1);
awlcopy(lpFindFileData->cFileName,FindFileDataA.cFileName,countof(lpFindFileData->cFileName)-1);
lpFindFileData->dwFileAttributes=FindFileDataA.dwFileAttributes;
lpFindFileData->dwReserved0=FindFileDataA.dwReserved0;
lpFindFileData->dwReserved1=FindFileDataA.dwReserved1;
lpFindFileData->ftCreationTime=FindFileDataA.ftCreationTime;
lpFindFileData->ftLastAccessTime=FindFileDataA.ftLastAccessTime;
lpFindFileData->ftLastWriteTime=FindFileDataA.ftLastWriteTime;
lpFindFileData->nFileSizeHigh=FindFileDataA.nFileSizeHigh;
lpFindFileData->nFileSizeLow=FindFileDataA.nFileSizeLow;
}
return retval;
}
}
WCHAR* wcslcatbackslash(WCHAR* thedir,int maxlen)
{
if (thedir[0] && wcslen(thedir)<(DWORD)maxlen)
if (thedir[wcslen(thedir)-1]!='\\')
wcscat_s(thedir,maxlen,L"\\");
return thedir;
}
void wcutlastbackslash(WCHAR* thedir)
{
if (thedir[0]) {
WCHAR* p=thedir+wcslen(thedir)-1;
if (p[0]=='\\')
p[0]=0;
}
}
LPWSTR wstrnew(LPWSTR str)
{
LPWSTR retval=NULL;
int len=0;
if (str) {
len=(int)wcslen(str)+1;
retval=(LPWSTR)CoTaskMemRealloc(NULL,len*sizeof(WCHAR));
}
if (retval)
wcscpy_s(retval,len,str);
return retval;
}