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