/src/FreeRDP/winpr/libwinpr/clipboard/synthetic_file.c
Line | Count | Source |
1 | | /** |
2 | | * WinPR: Windows Portable Runtime |
3 | | * Clipboard Functions: POSIX file handling |
4 | | * |
5 | | * Copyright 2017 Alexei Lozovsky <a.lozovsky@gmail.com> |
6 | | * |
7 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
8 | | * you may not use this file except in compliance with the License. |
9 | | * You may obtain a copy of the License at |
10 | | * |
11 | | * http://www.apache.org/licenses/LICENSE-2.0 |
12 | | * |
13 | | * Unless required by applicable law or agreed to in writing, software |
14 | | * distributed under the License is distributed on an "AS IS" BASIS, |
15 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | | * See the License for the specific language governing permissions and |
17 | | * limitations under the License. |
18 | | */ |
19 | | |
20 | | #include <winpr/config.h> |
21 | | #include <winpr/platform.h> |
22 | | |
23 | | WINPR_PRAGMA_DIAG_PUSH |
24 | | WINPR_PRAGMA_DIAG_IGNORED_RESERVED_ID_MACRO |
25 | | WINPR_PRAGMA_DIAG_IGNORED_UNUSED_MACRO |
26 | | |
27 | | #define _FILE_OFFSET_BITS 64 // NOLINT(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) |
28 | | |
29 | | WINPR_PRAGMA_DIAG_POP |
30 | | |
31 | | #include <errno.h> |
32 | | |
33 | | #include <winpr/wtypes.h> |
34 | | |
35 | | #include <winpr/crt.h> |
36 | | #include <winpr/clipboard.h> |
37 | | #include <winpr/collections.h> |
38 | | #include <winpr/file.h> |
39 | | #include <winpr/shell.h> |
40 | | #include <winpr/string.h> |
41 | | #include <winpr/wlog.h> |
42 | | #include <winpr/path.h> |
43 | | #include <winpr/print.h> |
44 | | |
45 | | #include "clipboard.h" |
46 | | #include "synthetic_file.h" |
47 | | |
48 | | #include "../log.h" |
49 | | #define TAG WINPR_TAG("clipboard.synthetic.file") |
50 | | |
51 | | static const char* mime_uri_list = "text/uri-list"; |
52 | | static const char* mime_FileGroupDescriptorW = "FileGroupDescriptorW"; |
53 | | static const char* mime_gnome_copied_files = "x-special/gnome-copied-files"; |
54 | | static const char* mime_mate_copied_files = "x-special/mate-copied-files"; |
55 | | |
56 | | struct synthetic_file |
57 | | { |
58 | | WCHAR* local_name; |
59 | | WCHAR* remote_name; |
60 | | |
61 | | HANDLE fd; |
62 | | INT64 offset; |
63 | | |
64 | | DWORD dwFileAttributes; |
65 | | FILETIME ftCreationTime; |
66 | | FILETIME ftLastAccessTime; |
67 | | FILETIME ftLastWriteTime; |
68 | | DWORD nFileSizeHigh; |
69 | | DWORD nFileSizeLow; |
70 | | }; |
71 | | |
72 | | void free_synthetic_file(struct synthetic_file* file); |
73 | | |
74 | | static struct synthetic_file* make_synthetic_file(const WCHAR* local_name, const WCHAR* remote_name) |
75 | 0 | { |
76 | 0 | struct synthetic_file* file = NULL; |
77 | 0 | WIN32_FIND_DATAW fd = { 0 }; |
78 | 0 | HANDLE hFind = NULL; |
79 | |
|
80 | 0 | WINPR_ASSERT(local_name); |
81 | 0 | WINPR_ASSERT(remote_name); |
82 | | |
83 | 0 | hFind = FindFirstFileW(local_name, &fd); |
84 | 0 | if (INVALID_HANDLE_VALUE == hFind) |
85 | 0 | { |
86 | 0 | WLog_ERR(TAG, "FindFirstFile failed (%" PRIu32 ")", GetLastError()); |
87 | 0 | return NULL; |
88 | 0 | } |
89 | 0 | FindClose(hFind); |
90 | |
|
91 | 0 | file = calloc(1, sizeof(*file)); |
92 | 0 | if (!file) |
93 | 0 | return NULL; |
94 | | |
95 | 0 | file->fd = INVALID_HANDLE_VALUE; |
96 | 0 | file->offset = 0; |
97 | 0 | file->local_name = _wcsdup(local_name); |
98 | 0 | if (!file->local_name) |
99 | 0 | goto fail; |
100 | | |
101 | 0 | file->remote_name = _wcsdup(remote_name); |
102 | 0 | if (!file->remote_name) |
103 | 0 | goto fail; |
104 | | |
105 | 0 | { |
106 | 0 | const size_t len = _wcslen(file->remote_name); |
107 | 0 | PathCchConvertStyleW(file->remote_name, len, PATH_STYLE_WINDOWS); |
108 | 0 | } |
109 | |
|
110 | 0 | file->dwFileAttributes = fd.dwFileAttributes; |
111 | 0 | file->ftCreationTime = fd.ftCreationTime; |
112 | 0 | file->ftLastWriteTime = fd.ftLastWriteTime; |
113 | 0 | file->ftLastAccessTime = fd.ftLastAccessTime; |
114 | 0 | file->nFileSizeHigh = fd.nFileSizeHigh; |
115 | 0 | file->nFileSizeLow = fd.nFileSizeLow; |
116 | |
|
117 | 0 | return file; |
118 | 0 | fail: |
119 | 0 | free_synthetic_file(file); |
120 | 0 | return NULL; |
121 | 0 | } |
122 | | |
123 | | static UINT synthetic_file_read_close(struct synthetic_file* file, BOOL force); |
124 | | |
125 | | void free_synthetic_file(struct synthetic_file* file) |
126 | 0 | { |
127 | 0 | if (!file) |
128 | 0 | return; |
129 | | |
130 | 0 | synthetic_file_read_close(file, TRUE); |
131 | |
|
132 | 0 | free(file->local_name); |
133 | 0 | free(file->remote_name); |
134 | 0 | free(file); |
135 | 0 | } |
136 | | |
137 | | /* |
138 | | * Note that the function converts a single file name component, |
139 | | * it does not take care of component separators. |
140 | | */ |
141 | | static WCHAR* convert_local_name_component_to_remote(wClipboard* clipboard, const WCHAR* local_name) |
142 | 0 | { |
143 | 0 | wClipboardDelegate* delegate = ClipboardGetDelegate(clipboard); |
144 | 0 | WCHAR* remote_name = NULL; |
145 | |
|
146 | 0 | WINPR_ASSERT(delegate); |
147 | | |
148 | 0 | remote_name = _wcsdup(local_name); |
149 | | |
150 | | /* |
151 | | * Some file names are not valid on Windows. Check for these now |
152 | | * so that we won't get ourselves into a trouble later as such names |
153 | | * are known to crash some Windows shells when pasted via clipboard. |
154 | | * |
155 | | * The IsFileNameComponentValid callback can be overridden by the API |
156 | | * user, if it is known, that the connected peer is not on the |
157 | | * Windows platform. |
158 | | */ |
159 | 0 | if (!delegate->IsFileNameComponentValid(remote_name)) |
160 | 0 | { |
161 | 0 | WLog_ERR(TAG, "invalid file name component: %s", local_name); |
162 | 0 | goto error; |
163 | 0 | } |
164 | | |
165 | 0 | return remote_name; |
166 | 0 | error: |
167 | 0 | free(remote_name); |
168 | 0 | return NULL; |
169 | 0 | } |
170 | | |
171 | | static WCHAR* concat_file_name(const WCHAR* dir, const WCHAR* file) |
172 | 0 | { |
173 | 0 | size_t len_dir = 0; |
174 | 0 | size_t len_file = 0; |
175 | 0 | const WCHAR slash = '/'; |
176 | 0 | WCHAR* buffer = NULL; |
177 | |
|
178 | 0 | WINPR_ASSERT(dir); |
179 | 0 | WINPR_ASSERT(file); |
180 | | |
181 | 0 | len_dir = _wcslen(dir); |
182 | 0 | len_file = _wcslen(file); |
183 | 0 | buffer = calloc(len_dir + 1 + len_file + 2, sizeof(WCHAR)); |
184 | |
|
185 | 0 | if (!buffer) |
186 | 0 | return NULL; |
187 | | |
188 | 0 | memcpy(buffer, dir, len_dir * sizeof(WCHAR)); |
189 | 0 | buffer[len_dir] = slash; |
190 | 0 | memcpy(buffer + len_dir + 1, file, len_file * sizeof(WCHAR)); |
191 | 0 | return buffer; |
192 | 0 | } |
193 | | |
194 | | static BOOL add_file_to_list(wClipboard* clipboard, const WCHAR* local_name, |
195 | | const WCHAR* remote_name, wArrayList* files); |
196 | | |
197 | | static BOOL add_directory_entry_to_list(wClipboard* clipboard, const WCHAR* local_dir_name, |
198 | | const WCHAR* remote_dir_name, |
199 | | const LPWIN32_FIND_DATAW pFileData, wArrayList* files) |
200 | 0 | { |
201 | 0 | BOOL result = FALSE; |
202 | 0 | WCHAR* local_name = NULL; |
203 | 0 | WCHAR* remote_name = NULL; |
204 | 0 | WCHAR* remote_base_name = NULL; |
205 | |
|
206 | 0 | WCHAR dotbuffer[6] = { 0 }; |
207 | 0 | WCHAR dotdotbuffer[6] = { 0 }; |
208 | 0 | const WCHAR* dot = InitializeConstWCharFromUtf8(".", dotbuffer, ARRAYSIZE(dotbuffer)); |
209 | 0 | const WCHAR* dotdot = InitializeConstWCharFromUtf8("..", dotdotbuffer, ARRAYSIZE(dotdotbuffer)); |
210 | |
|
211 | 0 | WINPR_ASSERT(clipboard); |
212 | 0 | WINPR_ASSERT(local_dir_name); |
213 | 0 | WINPR_ASSERT(remote_dir_name); |
214 | 0 | WINPR_ASSERT(pFileData); |
215 | 0 | WINPR_ASSERT(files); |
216 | | |
217 | | /* Skip special directory entries. */ |
218 | | |
219 | 0 | if ((_wcscmp(pFileData->cFileName, dot) == 0) || (_wcscmp(pFileData->cFileName, dotdot) == 0)) |
220 | 0 | return TRUE; |
221 | | |
222 | 0 | remote_base_name = convert_local_name_component_to_remote(clipboard, pFileData->cFileName); |
223 | |
|
224 | 0 | if (!remote_base_name) |
225 | 0 | return FALSE; |
226 | | |
227 | 0 | local_name = concat_file_name(local_dir_name, pFileData->cFileName); |
228 | 0 | remote_name = concat_file_name(remote_dir_name, remote_base_name); |
229 | |
|
230 | 0 | if (local_name && remote_name) |
231 | 0 | result = add_file_to_list(clipboard, local_name, remote_name, files); |
232 | |
|
233 | 0 | free(remote_base_name); |
234 | 0 | free(remote_name); |
235 | 0 | free(local_name); |
236 | 0 | return result; |
237 | 0 | } |
238 | | |
239 | | static BOOL do_add_directory_contents_to_list(wClipboard* clipboard, const WCHAR* local_name, |
240 | | const WCHAR* remote_name, WCHAR* namebuf, |
241 | | wArrayList* files) |
242 | 0 | { |
243 | 0 | WINPR_ASSERT(clipboard); |
244 | 0 | WINPR_ASSERT(local_name); |
245 | 0 | WINPR_ASSERT(remote_name); |
246 | 0 | WINPR_ASSERT(files); |
247 | 0 | WINPR_ASSERT(namebuf); |
248 | | |
249 | 0 | WIN32_FIND_DATAW FindData = { 0 }; |
250 | 0 | HANDLE hFind = FindFirstFileW(namebuf, &FindData); |
251 | 0 | if (INVALID_HANDLE_VALUE == hFind) |
252 | 0 | { |
253 | 0 | WLog_ERR(TAG, "FindFirstFile failed (%" PRIu32 ")", GetLastError()); |
254 | 0 | return FALSE; |
255 | 0 | } |
256 | 0 | while (TRUE) |
257 | 0 | { |
258 | 0 | if (!add_directory_entry_to_list(clipboard, local_name, remote_name, &FindData, files)) |
259 | 0 | { |
260 | 0 | FindClose(hFind); |
261 | 0 | return FALSE; |
262 | 0 | } |
263 | | |
264 | 0 | BOOL bRet = FindNextFileW(hFind, &FindData); |
265 | 0 | if (!bRet) |
266 | 0 | { |
267 | 0 | FindClose(hFind); |
268 | 0 | if (ERROR_NO_MORE_FILES == GetLastError()) |
269 | 0 | return TRUE; |
270 | 0 | WLog_WARN(TAG, "FindNextFile failed (%" PRIu32 ")", GetLastError()); |
271 | 0 | return FALSE; |
272 | 0 | } |
273 | 0 | } |
274 | | |
275 | 0 | return TRUE; |
276 | 0 | } |
277 | | |
278 | | static BOOL add_directory_contents_to_list(wClipboard* clipboard, const WCHAR* local_name, |
279 | | const WCHAR* remote_name, wArrayList* files) |
280 | 0 | { |
281 | 0 | BOOL result = FALSE; |
282 | 0 | union |
283 | 0 | { |
284 | 0 | const char* c; |
285 | 0 | const WCHAR* w; |
286 | 0 | } wildcard; |
287 | 0 | const char buffer[6] = { '/', '\0', '*', '\0', '\0', '\0' }; |
288 | 0 | wildcard.c = buffer; |
289 | 0 | const size_t wildcardLen = ARRAYSIZE(buffer) / sizeof(WCHAR); |
290 | |
|
291 | 0 | WINPR_ASSERT(clipboard); |
292 | 0 | WINPR_ASSERT(local_name); |
293 | 0 | WINPR_ASSERT(remote_name); |
294 | 0 | WINPR_ASSERT(files); |
295 | | |
296 | 0 | size_t len = _wcslen(local_name); |
297 | 0 | WCHAR* namebuf = calloc(len + wildcardLen, sizeof(WCHAR)); |
298 | 0 | if (!namebuf) |
299 | 0 | return FALSE; |
300 | | |
301 | 0 | _wcsncat(namebuf, local_name, len); |
302 | 0 | _wcsncat(namebuf, wildcard.w, wildcardLen); |
303 | |
|
304 | 0 | result = do_add_directory_contents_to_list(clipboard, local_name, remote_name, namebuf, files); |
305 | |
|
306 | 0 | free(namebuf); |
307 | 0 | return result; |
308 | 0 | } |
309 | | |
310 | | static BOOL add_file_to_list(wClipboard* clipboard, const WCHAR* local_name, |
311 | | const WCHAR* remote_name, wArrayList* files) |
312 | 0 | { |
313 | 0 | struct synthetic_file* file = NULL; |
314 | |
|
315 | 0 | WINPR_ASSERT(clipboard); |
316 | 0 | WINPR_ASSERT(local_name); |
317 | 0 | WINPR_ASSERT(remote_name); |
318 | 0 | WINPR_ASSERT(files); |
319 | | |
320 | 0 | file = make_synthetic_file(local_name, remote_name); |
321 | |
|
322 | 0 | if (!file) |
323 | 0 | return FALSE; |
324 | | |
325 | 0 | if (!ArrayList_Append(files, file)) |
326 | 0 | { |
327 | 0 | free_synthetic_file(file); |
328 | 0 | return FALSE; |
329 | 0 | } |
330 | | |
331 | 0 | if (file->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) |
332 | 0 | { |
333 | | /* |
334 | | * This is effectively a recursive call, but we do not track |
335 | | * recursion depth, thus filesystem loops can cause a crash. |
336 | | */ |
337 | 0 | if (!add_directory_contents_to_list(clipboard, local_name, remote_name, files)) |
338 | 0 | return FALSE; |
339 | 0 | } |
340 | | |
341 | 0 | return TRUE; |
342 | 0 | } |
343 | | |
344 | | static const WCHAR* get_basename(const WCHAR* name) |
345 | 0 | { |
346 | 0 | const WCHAR* c = name; |
347 | 0 | const WCHAR* last_name = name; |
348 | 0 | const WCHAR slash = '/'; |
349 | |
|
350 | 0 | WINPR_ASSERT(name); |
351 | | |
352 | 0 | while (*c++) |
353 | 0 | { |
354 | 0 | if (*c == slash) |
355 | 0 | last_name = c + 1; |
356 | 0 | } |
357 | |
|
358 | 0 | return last_name; |
359 | 0 | } |
360 | | |
361 | | static BOOL process_file_name(wClipboard* clipboard, const WCHAR* local_name, wArrayList* files) |
362 | 0 | { |
363 | 0 | BOOL result = FALSE; |
364 | 0 | const WCHAR* base_name = NULL; |
365 | 0 | WCHAR* remote_name = NULL; |
366 | |
|
367 | 0 | WINPR_ASSERT(clipboard); |
368 | 0 | WINPR_ASSERT(local_name); |
369 | 0 | WINPR_ASSERT(files); |
370 | | |
371 | | /* |
372 | | * Start with the base name of the file. text/uri-list contains the |
373 | | * exact files selected by the user, and we want the remote files |
374 | | * to have names relative to that selection. |
375 | | */ |
376 | 0 | base_name = get_basename(local_name); |
377 | 0 | remote_name = convert_local_name_component_to_remote(clipboard, base_name); |
378 | |
|
379 | 0 | if (!remote_name) |
380 | 0 | return FALSE; |
381 | | |
382 | 0 | result = add_file_to_list(clipboard, local_name, remote_name, files); |
383 | 0 | free(remote_name); |
384 | 0 | return result; |
385 | 0 | } |
386 | | |
387 | | static BOOL process_uri(wClipboard* clipboard, const char* uri, size_t uri_len) |
388 | 0 | { |
389 | | // URI is specified by RFC 8089: https://datatracker.ietf.org/doc/html/rfc8089 |
390 | 0 | BOOL result = FALSE; |
391 | 0 | char* name = NULL; |
392 | |
|
393 | 0 | WINPR_ASSERT(clipboard); |
394 | | |
395 | 0 | name = parse_uri_to_local_file(uri, uri_len); |
396 | 0 | if (name) |
397 | 0 | { |
398 | 0 | WCHAR* wname = NULL; |
399 | | /* |
400 | | * Note that local file names are not actually guaranteed to be |
401 | | * encoded in UTF-8. Filesystems and users can use whatever they |
402 | | * want. The OS does not care, aside from special treatment of |
403 | | * '\0' and '/' bytes. But we need to make some decision here. |
404 | | * Assuming UTF-8 is currently the most sane thing. |
405 | | */ |
406 | 0 | wname = ConvertUtf8ToWCharAlloc(name, NULL); |
407 | 0 | if (wname) |
408 | 0 | result = process_file_name(clipboard, wname, clipboard->localFiles); |
409 | |
|
410 | 0 | free(name); |
411 | 0 | free(wname); |
412 | 0 | } |
413 | |
|
414 | 0 | return result; |
415 | 0 | } |
416 | | |
417 | | static BOOL process_uri_list(wClipboard* clipboard, const char* data, size_t length) |
418 | 0 | { |
419 | 0 | const char* cur = data; |
420 | 0 | const char* lim = data + length; |
421 | |
|
422 | 0 | WINPR_ASSERT(clipboard); |
423 | 0 | WINPR_ASSERT(data); |
424 | | |
425 | 0 | WLog_VRB(TAG, "processing URI list:\n%.*s", length, data); |
426 | 0 | ArrayList_Clear(clipboard->localFiles); |
427 | | |
428 | | /* |
429 | | * The "text/uri-list" Internet Media Type is specified by RFC 2483. |
430 | | * |
431 | | * While the RFCs 2046 and 2483 require the lines of text/... formats |
432 | | * to be terminated by CRLF sequence, be prepared for those who don't |
433 | | * read the spec, use plain LFs, and don't leave the trailing CRLF. |
434 | | */ |
435 | |
|
436 | 0 | while (cur < lim) |
437 | 0 | { |
438 | 0 | BOOL comment = (*cur == '#'); |
439 | 0 | const char* start = cur; |
440 | 0 | const char* stop = cur; |
441 | |
|
442 | 0 | for (; stop < lim; stop++) |
443 | 0 | { |
444 | 0 | if (*stop == '\r') |
445 | 0 | { |
446 | 0 | if ((stop + 1 < lim) && (*(stop + 1) == '\n')) |
447 | 0 | cur = stop + 2; |
448 | 0 | else |
449 | 0 | cur = stop + 1; |
450 | |
|
451 | 0 | break; |
452 | 0 | } |
453 | | |
454 | 0 | if (*stop == '\n') |
455 | 0 | { |
456 | 0 | cur = stop + 1; |
457 | 0 | break; |
458 | 0 | } |
459 | 0 | } |
460 | |
|
461 | 0 | if (stop == lim) |
462 | 0 | { |
463 | 0 | if (strnlen(start, WINPR_ASSERTING_INT_CAST(size_t, stop - start)) < 1) |
464 | 0 | return TRUE; |
465 | 0 | cur = lim; |
466 | 0 | } |
467 | | |
468 | 0 | if (comment) |
469 | 0 | continue; |
470 | | |
471 | 0 | if (!process_uri(clipboard, start, WINPR_ASSERTING_INT_CAST(size_t, stop - start))) |
472 | 0 | return FALSE; |
473 | 0 | } |
474 | | |
475 | 0 | return TRUE; |
476 | 0 | } |
477 | | |
478 | | static BOOL convert_local_file_to_filedescriptor(const struct synthetic_file* file, |
479 | | FILEDESCRIPTORW* descriptor) |
480 | 0 | { |
481 | 0 | size_t remote_len = 0; |
482 | |
|
483 | 0 | WINPR_ASSERT(file); |
484 | 0 | WINPR_ASSERT(descriptor); |
485 | | |
486 | 0 | descriptor->dwFlags = FD_ATTRIBUTES | FD_FILESIZE | FD_WRITESTIME | FD_PROGRESSUI; |
487 | |
|
488 | 0 | if (file->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) |
489 | 0 | { |
490 | 0 | descriptor->dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY; |
491 | 0 | descriptor->nFileSizeLow = 0; |
492 | 0 | descriptor->nFileSizeHigh = 0; |
493 | 0 | } |
494 | 0 | else |
495 | 0 | { |
496 | 0 | descriptor->dwFileAttributes = FILE_ATTRIBUTE_NORMAL; |
497 | 0 | descriptor->nFileSizeLow = file->nFileSizeLow; |
498 | 0 | descriptor->nFileSizeHigh = file->nFileSizeHigh; |
499 | 0 | } |
500 | |
|
501 | 0 | descriptor->ftLastWriteTime = file->ftLastWriteTime; |
502 | |
|
503 | 0 | remote_len = _wcsnlen(file->remote_name, ARRAYSIZE(descriptor->cFileName)); |
504 | |
|
505 | 0 | if (remote_len >= ARRAYSIZE(descriptor->cFileName)) |
506 | 0 | { |
507 | 0 | WLog_ERR(TAG, "file name too long (%" PRIuz " characters)", remote_len); |
508 | 0 | return FALSE; |
509 | 0 | } |
510 | | |
511 | 0 | memcpy(descriptor->cFileName, file->remote_name, remote_len * sizeof(WCHAR)); |
512 | 0 | return TRUE; |
513 | 0 | } |
514 | | |
515 | | static FILEDESCRIPTORW* convert_local_file_list_to_filedescriptors(wArrayList* files) |
516 | 0 | { |
517 | 0 | size_t count = 0; |
518 | 0 | FILEDESCRIPTORW* descriptors = NULL; |
519 | |
|
520 | 0 | count = ArrayList_Count(files); |
521 | |
|
522 | 0 | descriptors = calloc(count, sizeof(FILEDESCRIPTORW)); |
523 | |
|
524 | 0 | if (!descriptors) |
525 | 0 | goto error; |
526 | | |
527 | 0 | for (size_t i = 0; i < count; i++) |
528 | 0 | { |
529 | 0 | const struct synthetic_file* file = ArrayList_GetItem(files, i); |
530 | |
|
531 | 0 | if (!convert_local_file_to_filedescriptor(file, &descriptors[i])) |
532 | 0 | goto error; |
533 | 0 | } |
534 | | |
535 | 0 | return descriptors; |
536 | 0 | error: |
537 | 0 | free(descriptors); |
538 | 0 | return NULL; |
539 | 0 | } |
540 | | |
541 | | static void* convert_any_uri_list_to_filedescriptors(wClipboard* clipboard, |
542 | | WINPR_ATTR_UNUSED UINT32 formatId, |
543 | | UINT32* pSize) |
544 | 0 | { |
545 | 0 | FILEDESCRIPTORW* descriptors = NULL; |
546 | |
|
547 | 0 | WINPR_ASSERT(clipboard); |
548 | 0 | WINPR_ASSERT(pSize); |
549 | | |
550 | 0 | descriptors = convert_local_file_list_to_filedescriptors(clipboard->localFiles); |
551 | 0 | *pSize = 0; |
552 | 0 | if (!descriptors) |
553 | 0 | return NULL; |
554 | | |
555 | 0 | *pSize = (UINT32)ArrayList_Count(clipboard->localFiles) * sizeof(FILEDESCRIPTORW); |
556 | 0 | clipboard->fileListSequenceNumber = clipboard->sequenceNumber; |
557 | 0 | return descriptors; |
558 | 0 | } |
559 | | |
560 | | static void* convert_uri_list_to_filedescriptors(wClipboard* clipboard, UINT32 formatId, |
561 | | const void* data, UINT32* pSize) |
562 | 0 | { |
563 | 0 | const UINT32 expected = ClipboardGetFormatId(clipboard, mime_uri_list); |
564 | 0 | if (formatId != expected) |
565 | 0 | return NULL; |
566 | 0 | if (!process_uri_list(clipboard, (const char*)data, *pSize)) |
567 | 0 | return NULL; |
568 | 0 | return convert_any_uri_list_to_filedescriptors(clipboard, formatId, pSize); |
569 | 0 | } |
570 | | |
571 | | static BOOL process_files(wClipboard* clipboard, const char* data, UINT32 pSize, const char* prefix) |
572 | 0 | { |
573 | 0 | WINPR_ASSERT(prefix); |
574 | | |
575 | 0 | const size_t prefix_len = strlen(prefix); |
576 | |
|
577 | 0 | WINPR_ASSERT(clipboard); |
578 | | |
579 | 0 | ArrayList_Clear(clipboard->localFiles); |
580 | |
|
581 | 0 | if (!data || (pSize < prefix_len)) |
582 | 0 | return FALSE; |
583 | 0 | if (strncmp(data, prefix, prefix_len) != 0) |
584 | 0 | return FALSE; |
585 | 0 | data += prefix_len; |
586 | 0 | if (pSize < prefix_len) |
587 | 0 | return FALSE; |
588 | 0 | pSize -= WINPR_ASSERTING_INT_CAST(uint32_t, prefix_len); |
589 | |
|
590 | 0 | BOOL rc = FALSE; |
591 | 0 | char* copy = strndup(data, pSize); |
592 | 0 | if (!copy) |
593 | 0 | goto fail; |
594 | | |
595 | 0 | { |
596 | 0 | char* endptr = NULL; |
597 | 0 | char* tok = strtok_s(copy, "\n", &endptr); |
598 | 0 | while (tok) |
599 | 0 | { |
600 | 0 | const size_t tok_len = strnlen(tok, pSize); |
601 | 0 | if (!process_uri(clipboard, tok, tok_len)) |
602 | 0 | goto fail; |
603 | 0 | if (pSize < tok_len) |
604 | 0 | goto fail; |
605 | 0 | pSize -= WINPR_ASSERTING_INT_CAST(uint32_t, tok_len); |
606 | 0 | tok = strtok_s(NULL, "\n", &endptr); |
607 | 0 | } |
608 | 0 | } |
609 | | |
610 | 0 | rc = TRUE; |
611 | |
|
612 | 0 | fail: |
613 | 0 | free(copy); |
614 | 0 | return rc; |
615 | 0 | } |
616 | | |
617 | | static BOOL process_gnome_copied_files(wClipboard* clipboard, const char* data, UINT32 pSize) |
618 | 0 | { |
619 | 0 | return process_files(clipboard, data, pSize, "copy\n"); |
620 | 0 | } |
621 | | |
622 | | static BOOL process_mate_copied_files(wClipboard* clipboard, const char* data, UINT32 pSize) |
623 | 0 | { |
624 | 0 | return process_files(clipboard, data, pSize, "copy\n"); |
625 | 0 | } |
626 | | |
627 | | static void* convert_gnome_copied_files_to_filedescriptors(wClipboard* clipboard, UINT32 formatId, |
628 | | const void* data, UINT32* pSize) |
629 | 0 | { |
630 | 0 | const UINT32 expected = ClipboardGetFormatId(clipboard, mime_gnome_copied_files); |
631 | 0 | if (formatId != expected) |
632 | 0 | return NULL; |
633 | 0 | if (!process_gnome_copied_files(clipboard, (const char*)data, *pSize)) |
634 | 0 | return NULL; |
635 | 0 | return convert_any_uri_list_to_filedescriptors(clipboard, formatId, pSize); |
636 | 0 | } |
637 | | |
638 | | static void* convert_mate_copied_files_to_filedescriptors(wClipboard* clipboard, UINT32 formatId, |
639 | | const void* data, UINT32* pSize) |
640 | 0 | { |
641 | 0 | const UINT32 expected = ClipboardGetFormatId(clipboard, mime_mate_copied_files); |
642 | 0 | if (formatId != expected) |
643 | 0 | return NULL; |
644 | | |
645 | 0 | if (!process_mate_copied_files(clipboard, (const char*)data, *pSize)) |
646 | 0 | return NULL; |
647 | | |
648 | 0 | return convert_any_uri_list_to_filedescriptors(clipboard, formatId, pSize); |
649 | 0 | } |
650 | | |
651 | | static size_t count_special_chars(const WCHAR* str) |
652 | 0 | { |
653 | 0 | size_t count = 0; |
654 | 0 | const WCHAR* start = str; |
655 | |
|
656 | 0 | WINPR_ASSERT(str); |
657 | 0 | while (*start) |
658 | 0 | { |
659 | 0 | const WCHAR sharp = '#'; |
660 | 0 | const WCHAR questionmark = '?'; |
661 | 0 | const WCHAR star = '*'; |
662 | 0 | const WCHAR exclamationmark = '!'; |
663 | 0 | const WCHAR percent = '%'; |
664 | |
|
665 | 0 | if ((*start == sharp) || (*start == questionmark) || (*start == star) || |
666 | 0 | (*start == exclamationmark) || (*start == percent)) |
667 | 0 | { |
668 | 0 | count++; |
669 | 0 | } |
670 | 0 | start++; |
671 | 0 | } |
672 | 0 | return count; |
673 | 0 | } |
674 | | |
675 | | static const char* stop_at_special_chars(const char* str) |
676 | 0 | { |
677 | 0 | const char* start = str; |
678 | 0 | WINPR_ASSERT(str); |
679 | | |
680 | 0 | while (*start) |
681 | 0 | { |
682 | 0 | if (*start == '#' || *start == '?' || *start == '*' || *start == '!' || *start == '%') |
683 | 0 | { |
684 | 0 | return start; |
685 | 0 | } |
686 | 0 | start++; |
687 | 0 | } |
688 | 0 | return NULL; |
689 | 0 | } |
690 | | |
691 | | /* The universal converter from filedescriptors to different file lists */ |
692 | | static void* convert_filedescriptors_to_file_list(wClipboard* clipboard, UINT32 formatId, |
693 | | const void* data, UINT32* pSize, |
694 | | const char* header, const char* lineprefix, |
695 | | const char* lineending, BOOL skip_last_lineending) |
696 | 0 | { |
697 | 0 | union |
698 | 0 | { |
699 | 0 | char c[2]; |
700 | 0 | WCHAR w; |
701 | 0 | } backslash; |
702 | 0 | backslash.c[0] = '\\'; |
703 | 0 | backslash.c[1] = '\0'; |
704 | |
|
705 | 0 | const FILEDESCRIPTORW* descriptors = NULL; |
706 | 0 | UINT32 nrDescriptors = 0; |
707 | 0 | size_t count = 0; |
708 | 0 | size_t alloc = 0; |
709 | 0 | size_t pos = 0; |
710 | 0 | size_t baseLength = 0; |
711 | 0 | char* dst = NULL; |
712 | 0 | size_t header_len = strlen(header); |
713 | 0 | size_t lineprefix_len = strlen(lineprefix); |
714 | 0 | size_t lineending_len = strlen(lineending); |
715 | 0 | size_t decoration_len = 0; |
716 | |
|
717 | 0 | if (!clipboard || !data || !pSize) |
718 | 0 | return NULL; |
719 | | |
720 | 0 | if (*pSize < sizeof(UINT32)) |
721 | 0 | return NULL; |
722 | | |
723 | 0 | if (clipboard->delegate.basePath) |
724 | 0 | baseLength = strnlen(clipboard->delegate.basePath, MAX_PATH); |
725 | |
|
726 | 0 | if (baseLength < 1) |
727 | 0 | return NULL; |
728 | | |
729 | 0 | wStream sbuffer = { 0 }; |
730 | 0 | wStream* s = Stream_StaticConstInit(&sbuffer, data, *pSize); |
731 | 0 | if (!Stream_CheckAndLogRequiredLength(TAG, s, 4)) |
732 | 0 | return NULL; |
733 | | |
734 | 0 | Stream_Read_UINT32(s, nrDescriptors); |
735 | |
|
736 | 0 | count = (*pSize - 4) / sizeof(FILEDESCRIPTORW); |
737 | |
|
738 | 0 | if ((count < 1) || (count != nrDescriptors)) |
739 | 0 | return NULL; |
740 | | |
741 | 0 | descriptors = Stream_ConstPointer(s); |
742 | |
|
743 | 0 | if (formatId != ClipboardGetFormatId(clipboard, mime_FileGroupDescriptorW)) |
744 | 0 | return NULL; |
745 | | |
746 | | /* Plus 1 for '/' between basepath and filename*/ |
747 | 0 | decoration_len = lineprefix_len + lineending_len + baseLength + 1; |
748 | 0 | alloc = header_len; |
749 | | |
750 | | /* Get total size of file/folder names under first level folder only */ |
751 | 0 | for (size_t x = 0; x < count; x++) |
752 | 0 | { |
753 | 0 | const FILEDESCRIPTORW* dsc = &descriptors[x]; |
754 | |
|
755 | 0 | if (_wcschr(dsc->cFileName, backslash.w) == NULL) |
756 | 0 | { |
757 | 0 | alloc += ARRAYSIZE(dsc->cFileName) * |
758 | 0 | 8; /* Overallocate, just take the biggest value the result path can have */ |
759 | | /* # (1 char) -> %23 (3 chars) , the first char is replaced inplace */ |
760 | 0 | alloc += count_special_chars(dsc->cFileName) * 2; |
761 | 0 | alloc += decoration_len; |
762 | 0 | } |
763 | 0 | } |
764 | | |
765 | | /* Append a prefix file:// and postfix \n for each file */ |
766 | | /* We need to keep last \n since snprintf is null terminated!! */ |
767 | 0 | alloc++; |
768 | 0 | dst = calloc(alloc, sizeof(char)); |
769 | |
|
770 | 0 | if (!dst) |
771 | 0 | return NULL; |
772 | | |
773 | 0 | (void)_snprintf(&dst[0], alloc, "%s", header); |
774 | |
|
775 | 0 | pos = header_len; |
776 | |
|
777 | 0 | for (size_t x = 0; x < count; x++) |
778 | 0 | { |
779 | 0 | const FILEDESCRIPTORW* dsc = &descriptors[x]; |
780 | 0 | BOOL fail = TRUE; |
781 | 0 | if (_wcschr(dsc->cFileName, backslash.w) != NULL) |
782 | 0 | { |
783 | 0 | continue; |
784 | 0 | } |
785 | 0 | int rc = -1; |
786 | 0 | char curName[520] = { 0 }; |
787 | 0 | const char* stop_at = NULL; |
788 | 0 | const char* previous_at = NULL; |
789 | |
|
790 | 0 | if (ConvertWCharNToUtf8(dsc->cFileName, ARRAYSIZE(dsc->cFileName), curName, |
791 | 0 | ARRAYSIZE(curName)) < 0) |
792 | 0 | goto loop_fail; |
793 | | |
794 | 0 | rc = _snprintf(&dst[pos], alloc - pos, "%s%s/", lineprefix, clipboard->delegate.basePath); |
795 | |
|
796 | 0 | if (rc < 0) |
797 | 0 | goto loop_fail; |
798 | | |
799 | 0 | pos += (size_t)rc; |
800 | |
|
801 | 0 | previous_at = curName; |
802 | 0 | while ((stop_at = stop_at_special_chars(previous_at)) != NULL) |
803 | 0 | { |
804 | 0 | const intptr_t diff = stop_at - previous_at; |
805 | 0 | if (diff < 0) |
806 | 0 | goto loop_fail; |
807 | 0 | char* tmp = strndup(previous_at, WINPR_ASSERTING_INT_CAST(size_t, diff)); |
808 | 0 | if (!tmp) |
809 | 0 | goto loop_fail; |
810 | | |
811 | 0 | rc = _snprintf(&dst[pos], WINPR_ASSERTING_INT_CAST(size_t, diff + 1), "%s", tmp); |
812 | 0 | free(tmp); |
813 | 0 | if (rc < 0) |
814 | 0 | goto loop_fail; |
815 | | |
816 | 0 | pos += (size_t)rc; |
817 | 0 | rc = _snprintf(&dst[pos], 4, "%%%x", *stop_at); |
818 | 0 | if (rc < 0) |
819 | 0 | goto loop_fail; |
820 | | |
821 | 0 | pos += (size_t)rc; |
822 | 0 | previous_at = stop_at + 1; |
823 | 0 | } |
824 | | |
825 | 0 | rc = _snprintf(&dst[pos], alloc - pos, "%s%s", previous_at, lineending); |
826 | |
|
827 | 0 | fail = FALSE; |
828 | 0 | loop_fail: |
829 | 0 | if ((rc < 0) || fail) |
830 | 0 | { |
831 | 0 | free(dst); |
832 | 0 | return NULL; |
833 | 0 | } |
834 | | |
835 | 0 | pos += (size_t)rc; |
836 | 0 | } |
837 | | |
838 | 0 | if (skip_last_lineending) |
839 | 0 | { |
840 | 0 | const size_t endlen = strlen(lineending); |
841 | 0 | if (alloc > endlen) |
842 | 0 | { |
843 | 0 | const size_t len = strnlen(dst, alloc); |
844 | 0 | if (len < endlen) |
845 | 0 | { |
846 | 0 | free(dst); |
847 | 0 | return NULL; |
848 | 0 | } |
849 | | |
850 | 0 | if (memcmp(&dst[len - endlen], lineending, endlen) == 0) |
851 | 0 | { |
852 | 0 | memset(&dst[len - endlen], 0, endlen); |
853 | 0 | alloc -= endlen; |
854 | 0 | } |
855 | 0 | } |
856 | 0 | } |
857 | | |
858 | 0 | alloc = strnlen(dst, alloc) + 1; |
859 | 0 | *pSize = (UINT32)alloc; |
860 | 0 | clipboard->fileListSequenceNumber = clipboard->sequenceNumber; |
861 | 0 | return dst; |
862 | 0 | } |
863 | | |
864 | | /* Prepend header of kde dolphin format to file list |
865 | | * See: |
866 | | * GTK: https://docs.gtk.org/glib/struct.Uri.html |
867 | | * uri syntax: https://www.rfc-editor.org/rfc/rfc3986#section-3 |
868 | | * uri-lists format: https://www.rfc-editor.org/rfc/rfc2483#section-5 |
869 | | */ |
870 | | static void* convert_filedescriptors_to_uri_list(wClipboard* clipboard, UINT32 formatId, |
871 | | const void* data, UINT32* pSize) |
872 | 0 | { |
873 | 0 | return convert_filedescriptors_to_file_list(clipboard, formatId, data, pSize, "", "file://", |
874 | 0 | "\r\n", FALSE); |
875 | 0 | } |
876 | | |
877 | | /* Prepend header of common gnome format to file list*/ |
878 | | static void* convert_filedescriptors_to_gnome_copied_files(wClipboard* clipboard, UINT32 formatId, |
879 | | const void* data, UINT32* pSize) |
880 | 0 | { |
881 | 0 | return convert_filedescriptors_to_file_list(clipboard, formatId, data, pSize, "copy\n", |
882 | 0 | "file://", "\n", TRUE); |
883 | 0 | } |
884 | | |
885 | | static void* convert_filedescriptors_to_mate_copied_files(wClipboard* clipboard, UINT32 formatId, |
886 | | const void* data, UINT32* pSize) |
887 | 0 | { |
888 | |
|
889 | 0 | char* pDstData = convert_filedescriptors_to_file_list(clipboard, formatId, data, pSize, |
890 | 0 | "copy\n", "file://", "\n", TRUE); |
891 | 0 | if (!pDstData) |
892 | 0 | { |
893 | 0 | return pDstData; |
894 | 0 | } |
895 | | /* Replace last \n with \0 |
896 | | see |
897 | | mate-desktop/caja/libcaja-private/caja-clipboard.c:caja_clipboard_get_uri_list_from_selection_data |
898 | | */ |
899 | | |
900 | 0 | pDstData[*pSize - 1] = '\0'; |
901 | 0 | *pSize = *pSize - 1; |
902 | 0 | return pDstData; |
903 | 0 | } |
904 | | |
905 | | static void array_free_synthetic_file(void* the_file) |
906 | 0 | { |
907 | 0 | struct synthetic_file* file = the_file; |
908 | 0 | free_synthetic_file(file); |
909 | 0 | } |
910 | | |
911 | | static BOOL register_file_formats_and_synthesizers(wClipboard* clipboard) |
912 | 0 | { |
913 | 0 | wObject* obj = NULL; |
914 | | |
915 | | /* |
916 | | 1. Gnome Nautilus based file manager (Nautilus only with version >= 3.30 AND < 40): |
917 | | TARGET: UTF8_STRING |
918 | | format: x-special/nautilus-clipboard\copy\n\file://path\n\0 |
919 | | 2. Kde Dolpin and Qt: |
920 | | TARGET: text/uri-list |
921 | | format: file:path\r\n\0 |
922 | | See: |
923 | | GTK: https://docs.gtk.org/glib/struct.Uri.html |
924 | | uri syntax: https://www.rfc-editor.org/rfc/rfc3986#section-3 |
925 | | uri-lists format: https://www.rfc-editor.org/rfc/rfc2483#section-5 |
926 | | 3. Gnome and others (Unity/XFCE/Nautilus < 3.30/Nautilus >= 40): |
927 | | TARGET: x-special/gnome-copied-files |
928 | | format: copy\nfile://path\n\0 |
929 | | 4. Mate Caja: |
930 | | TARGET: x-special/mate-copied-files |
931 | | format: copy\nfile://path\n |
932 | | |
933 | | TODO: other file managers do not use previous targets and formats. |
934 | | */ |
935 | |
|
936 | 0 | const UINT32 local_gnome_file_format_id = |
937 | 0 | ClipboardRegisterFormat(clipboard, mime_gnome_copied_files); |
938 | 0 | const UINT32 local_mate_file_format_id = |
939 | 0 | ClipboardRegisterFormat(clipboard, mime_mate_copied_files); |
940 | 0 | const UINT32 file_group_format_id = |
941 | 0 | ClipboardRegisterFormat(clipboard, mime_FileGroupDescriptorW); |
942 | 0 | const UINT32 local_file_format_id = ClipboardRegisterFormat(clipboard, mime_uri_list); |
943 | |
|
944 | 0 | if (!file_group_format_id || !local_file_format_id || !local_gnome_file_format_id || |
945 | 0 | !local_mate_file_format_id) |
946 | 0 | goto error; |
947 | | |
948 | 0 | clipboard->localFiles = ArrayList_New(FALSE); |
949 | |
|
950 | 0 | if (!clipboard->localFiles) |
951 | 0 | goto error; |
952 | | |
953 | 0 | obj = ArrayList_Object(clipboard->localFiles); |
954 | 0 | obj->fnObjectFree = array_free_synthetic_file; |
955 | |
|
956 | 0 | if (!ClipboardRegisterSynthesizer(clipboard, local_file_format_id, file_group_format_id, |
957 | 0 | convert_uri_list_to_filedescriptors)) |
958 | 0 | goto error_free_local_files; |
959 | | |
960 | 0 | if (!ClipboardRegisterSynthesizer(clipboard, file_group_format_id, local_file_format_id, |
961 | 0 | convert_filedescriptors_to_uri_list)) |
962 | 0 | goto error_free_local_files; |
963 | | |
964 | 0 | if (!ClipboardRegisterSynthesizer(clipboard, local_gnome_file_format_id, file_group_format_id, |
965 | 0 | convert_gnome_copied_files_to_filedescriptors)) |
966 | 0 | goto error_free_local_files; |
967 | | |
968 | 0 | if (!ClipboardRegisterSynthesizer(clipboard, file_group_format_id, local_gnome_file_format_id, |
969 | 0 | convert_filedescriptors_to_gnome_copied_files)) |
970 | 0 | goto error_free_local_files; |
971 | | |
972 | 0 | if (!ClipboardRegisterSynthesizer(clipboard, local_mate_file_format_id, file_group_format_id, |
973 | 0 | convert_mate_copied_files_to_filedescriptors)) |
974 | 0 | goto error_free_local_files; |
975 | | |
976 | 0 | if (!ClipboardRegisterSynthesizer(clipboard, file_group_format_id, local_mate_file_format_id, |
977 | 0 | convert_filedescriptors_to_mate_copied_files)) |
978 | 0 | goto error_free_local_files; |
979 | | |
980 | 0 | return TRUE; |
981 | 0 | error_free_local_files: |
982 | 0 | ArrayList_Free(clipboard->localFiles); |
983 | 0 | clipboard->localFiles = NULL; |
984 | 0 | error: |
985 | 0 | return FALSE; |
986 | 0 | } |
987 | | |
988 | | static int32_t file_get_size(const struct synthetic_file* file, UINT64* size) |
989 | 0 | { |
990 | 0 | UINT64 s = 0; |
991 | |
|
992 | 0 | if (!file || !size) |
993 | 0 | return E_INVALIDARG; |
994 | | |
995 | 0 | s = file->nFileSizeHigh; |
996 | 0 | s <<= 32; |
997 | 0 | s |= file->nFileSizeLow; |
998 | 0 | *size = s; |
999 | 0 | return NO_ERROR; |
1000 | 0 | } |
1001 | | |
1002 | | static UINT delegate_file_request_size(wClipboardDelegate* delegate, |
1003 | | const wClipboardFileSizeRequest* request) |
1004 | 0 | { |
1005 | 0 | UINT64 size = 0; |
1006 | |
|
1007 | 0 | if (!delegate || !delegate->clipboard || !request) |
1008 | 0 | return ERROR_BAD_ARGUMENTS; |
1009 | | |
1010 | 0 | if (delegate->clipboard->sequenceNumber != delegate->clipboard->fileListSequenceNumber) |
1011 | 0 | return ERROR_INVALID_STATE; |
1012 | | |
1013 | 0 | struct synthetic_file* file = |
1014 | 0 | ArrayList_GetItem(delegate->clipboard->localFiles, request->listIndex); |
1015 | |
|
1016 | 0 | if (!file) |
1017 | 0 | return ERROR_INDEX_ABSENT; |
1018 | | |
1019 | 0 | const int32_t s = file_get_size(file, &size); |
1020 | 0 | uint32_t error = 0; |
1021 | 0 | if (error) |
1022 | 0 | error = delegate->ClipboardFileSizeFailure(delegate, request, (UINT)s); |
1023 | 0 | else |
1024 | 0 | error = delegate->ClipboardFileSizeSuccess(delegate, request, size); |
1025 | |
|
1026 | 0 | if (error) |
1027 | 0 | WLog_WARN(TAG, "failed to report file size result: 0x%08X", error); |
1028 | |
|
1029 | 0 | return NO_ERROR; |
1030 | 0 | } |
1031 | | |
1032 | | UINT synthetic_file_read_close(struct synthetic_file* file, BOOL force) |
1033 | 0 | { |
1034 | 0 | if (!file || INVALID_HANDLE_VALUE == file->fd) |
1035 | 0 | return NO_ERROR; |
1036 | | |
1037 | | /* Always force close the file. Clipboard might open hundreds of files |
1038 | | * so avoid caching to prevent running out of available file descriptors */ |
1039 | 0 | UINT64 size = 0; |
1040 | 0 | file_get_size(file, &size); |
1041 | 0 | if ((file->offset < 0) || ((UINT64)file->offset >= size) || force) |
1042 | 0 | { |
1043 | 0 | WLog_VRB(TAG, "close file %d", file->fd); |
1044 | 0 | if (!CloseHandle(file->fd)) |
1045 | 0 | { |
1046 | 0 | WLog_WARN(TAG, "failed to close fd %d: %" PRIu32, file->fd, GetLastError()); |
1047 | 0 | } |
1048 | |
|
1049 | 0 | file->fd = INVALID_HANDLE_VALUE; |
1050 | 0 | } |
1051 | |
|
1052 | 0 | return NO_ERROR; |
1053 | 0 | } |
1054 | | |
1055 | | static UINT file_get_range(struct synthetic_file* file, UINT64 offset, UINT32 size, |
1056 | | BYTE** actual_data, UINT32* actual_size) |
1057 | 0 | { |
1058 | 0 | UINT error = NO_ERROR; |
1059 | 0 | DWORD dwLow = 0; |
1060 | 0 | DWORD dwHigh = 0; |
1061 | |
|
1062 | 0 | WINPR_ASSERT(file); |
1063 | 0 | WINPR_ASSERT(actual_data); |
1064 | 0 | WINPR_ASSERT(actual_size); |
1065 | | |
1066 | 0 | if (INVALID_HANDLE_VALUE == file->fd) |
1067 | 0 | { |
1068 | 0 | BY_HANDLE_FILE_INFORMATION FileInfo = { 0 }; |
1069 | |
|
1070 | 0 | file->fd = CreateFileW(file->local_name, GENERIC_READ, 0, NULL, OPEN_EXISTING, |
1071 | 0 | FILE_ATTRIBUTE_NORMAL, NULL); |
1072 | 0 | if (INVALID_HANDLE_VALUE == file->fd) |
1073 | 0 | { |
1074 | 0 | error = GetLastError(); |
1075 | 0 | WLog_ERR(TAG, "failed to open file %s: 0x%08" PRIx32, file->local_name, error); |
1076 | 0 | return error; |
1077 | 0 | } |
1078 | | |
1079 | 0 | if (!GetFileInformationByHandle(file->fd, &FileInfo)) |
1080 | 0 | { |
1081 | 0 | (void)CloseHandle(file->fd); |
1082 | 0 | file->fd = INVALID_HANDLE_VALUE; |
1083 | 0 | error = GetLastError(); |
1084 | 0 | WLog_ERR(TAG, "Get file [%s] information fail: 0x%08" PRIx32, file->local_name, error); |
1085 | 0 | return error; |
1086 | 0 | } |
1087 | | |
1088 | 0 | file->offset = 0; |
1089 | 0 | file->nFileSizeHigh = FileInfo.nFileSizeHigh; |
1090 | 0 | file->nFileSizeLow = FileInfo.nFileSizeLow; |
1091 | | |
1092 | | /* |
1093 | | { |
1094 | | UINT64 s = 0; |
1095 | | file_get_size(file, &s); |
1096 | | WLog_DBG(TAG, "open file %d -> %s", file->fd, file->local_name); |
1097 | | WLog_DBG(TAG, "file %d size: %" PRIu64 " bytes", file->fd, s); |
1098 | | } //*/ |
1099 | 0 | } |
1100 | | |
1101 | 0 | do |
1102 | 0 | { |
1103 | | /* |
1104 | | * We should avoid seeking when possible as some filesystems (e.g., |
1105 | | * an FTP server mapped via FUSE) may not support seeking. We keep |
1106 | | * an accurate account of the current file offset and do not call |
1107 | | * lseek() if the client requests file content sequentially. |
1108 | | */ |
1109 | 0 | if (offset > INT64_MAX) |
1110 | 0 | { |
1111 | 0 | WLog_ERR(TAG, "offset [%" PRIu64 "] > INT64_MAX", offset); |
1112 | 0 | error = ERROR_SEEK; |
1113 | 0 | break; |
1114 | 0 | } |
1115 | | |
1116 | 0 | if (file->offset != (INT64)offset) |
1117 | 0 | { |
1118 | 0 | WLog_DBG(TAG, "file %d force seeking to %" PRIu64 ", current %" PRIu64, file->fd, |
1119 | 0 | offset, file->offset); |
1120 | |
|
1121 | 0 | dwHigh = offset >> 32; |
1122 | 0 | dwLow = offset & 0xFFFFFFFF; |
1123 | 0 | if (INVALID_SET_FILE_POINTER == SetFilePointer(file->fd, |
1124 | 0 | WINPR_ASSERTING_INT_CAST(LONG, dwLow), |
1125 | 0 | (PLONG)&dwHigh, FILE_BEGIN)) |
1126 | 0 | { |
1127 | 0 | error = GetLastError(); |
1128 | 0 | break; |
1129 | 0 | } |
1130 | 0 | } |
1131 | | |
1132 | 0 | BYTE* buffer = malloc(size); |
1133 | 0 | if (!buffer) |
1134 | 0 | { |
1135 | 0 | error = ERROR_NOT_ENOUGH_MEMORY; |
1136 | 0 | break; |
1137 | 0 | } |
1138 | 0 | if (!ReadFile(file->fd, buffer, size, (LPDWORD)actual_size, NULL)) |
1139 | 0 | { |
1140 | 0 | free(buffer); |
1141 | 0 | error = GetLastError(); |
1142 | 0 | break; |
1143 | 0 | } |
1144 | | |
1145 | 0 | *actual_data = buffer; |
1146 | 0 | file->offset += *actual_size; |
1147 | 0 | WLog_VRB(TAG, "file %d actual read %" PRIu32 " bytes (offset %" PRIu64 ")", file->fd, |
1148 | 0 | *actual_size, file->offset); |
1149 | 0 | } while (0); |
1150 | |
|
1151 | 0 | synthetic_file_read_close(file, TRUE /* (error != NO_ERROR) && (size > 0) */); |
1152 | 0 | return error; |
1153 | 0 | } |
1154 | | |
1155 | | static UINT delegate_file_request_range(wClipboardDelegate* delegate, |
1156 | | const wClipboardFileRangeRequest* request) |
1157 | 0 | { |
1158 | 0 | UINT error = 0; |
1159 | 0 | BYTE* data = NULL; |
1160 | 0 | UINT32 size = 0; |
1161 | 0 | UINT64 offset = 0; |
1162 | 0 | struct synthetic_file* file = NULL; |
1163 | |
|
1164 | 0 | if (!delegate || !delegate->clipboard || !request) |
1165 | 0 | return ERROR_BAD_ARGUMENTS; |
1166 | | |
1167 | 0 | if (delegate->clipboard->sequenceNumber != delegate->clipboard->fileListSequenceNumber) |
1168 | 0 | return ERROR_INVALID_STATE; |
1169 | | |
1170 | 0 | file = ArrayList_GetItem(delegate->clipboard->localFiles, request->listIndex); |
1171 | |
|
1172 | 0 | if (!file) |
1173 | 0 | return ERROR_INDEX_ABSENT; |
1174 | | |
1175 | 0 | offset = (((UINT64)request->nPositionHigh) << 32) | ((UINT64)request->nPositionLow); |
1176 | 0 | error = file_get_range(file, offset, request->cbRequested, &data, &size); |
1177 | |
|
1178 | 0 | if (error) |
1179 | 0 | error = delegate->ClipboardFileRangeFailure(delegate, request, error); |
1180 | 0 | else |
1181 | 0 | error = delegate->ClipboardFileRangeSuccess(delegate, request, data, size); |
1182 | |
|
1183 | 0 | if (error) |
1184 | 0 | WLog_WARN(TAG, "failed to report file range result: 0x%08X", error); |
1185 | |
|
1186 | 0 | free(data); |
1187 | 0 | return NO_ERROR; |
1188 | 0 | } |
1189 | | |
1190 | | static UINT dummy_file_size_success(WINPR_ATTR_UNUSED wClipboardDelegate* delegate, |
1191 | | WINPR_ATTR_UNUSED const wClipboardFileSizeRequest* request, |
1192 | | WINPR_ATTR_UNUSED UINT64 fileSize) |
1193 | 0 | { |
1194 | 0 | return ERROR_NOT_SUPPORTED; |
1195 | 0 | } |
1196 | | |
1197 | | static UINT dummy_file_size_failure(WINPR_ATTR_UNUSED wClipboardDelegate* delegate, |
1198 | | WINPR_ATTR_UNUSED const wClipboardFileSizeRequest* request, |
1199 | | WINPR_ATTR_UNUSED UINT errorCode) |
1200 | 0 | { |
1201 | 0 | return ERROR_NOT_SUPPORTED; |
1202 | 0 | } |
1203 | | |
1204 | | static UINT dummy_file_range_success(WINPR_ATTR_UNUSED wClipboardDelegate* delegate, |
1205 | | WINPR_ATTR_UNUSED const wClipboardFileRangeRequest* request, |
1206 | | WINPR_ATTR_UNUSED const BYTE* data, |
1207 | | WINPR_ATTR_UNUSED UINT32 size) |
1208 | 0 | { |
1209 | 0 | return ERROR_NOT_SUPPORTED; |
1210 | 0 | } |
1211 | | |
1212 | | static UINT dummy_file_range_failure(WINPR_ATTR_UNUSED wClipboardDelegate* delegate, |
1213 | | WINPR_ATTR_UNUSED const wClipboardFileRangeRequest* request, |
1214 | | WINPR_ATTR_UNUSED UINT errorCode) |
1215 | 0 | { |
1216 | 0 | return ERROR_NOT_SUPPORTED; |
1217 | 0 | } |
1218 | | |
1219 | | static void setup_delegate(wClipboardDelegate* delegate) |
1220 | 0 | { |
1221 | 0 | WINPR_ASSERT(delegate); |
1222 | | |
1223 | 0 | delegate->ClientRequestFileSize = delegate_file_request_size; |
1224 | 0 | delegate->ClipboardFileSizeSuccess = dummy_file_size_success; |
1225 | 0 | delegate->ClipboardFileSizeFailure = dummy_file_size_failure; |
1226 | 0 | delegate->ClientRequestFileRange = delegate_file_request_range; |
1227 | 0 | delegate->ClipboardFileRangeSuccess = dummy_file_range_success; |
1228 | 0 | delegate->ClipboardFileRangeFailure = dummy_file_range_failure; |
1229 | 0 | delegate->IsFileNameComponentValid = ValidFileNameComponent; |
1230 | 0 | } |
1231 | | |
1232 | | BOOL ClipboardInitSyntheticFileSubsystem(wClipboard* clipboard) |
1233 | 0 | { |
1234 | 0 | if (!clipboard) |
1235 | 0 | return FALSE; |
1236 | | |
1237 | 0 | if (!register_file_formats_and_synthesizers(clipboard)) |
1238 | 0 | return FALSE; |
1239 | | |
1240 | 0 | setup_delegate(&clipboard->delegate); |
1241 | | return TRUE; |
1242 | 0 | } |