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