FreeRDP
Loading...
Searching...
No Matches
clipboard.c
1
20#include <winpr/config.h>
21
22#include <winpr/crt.h>
23#include <winpr/collections.h>
24#include <winpr/wlog.h>
25
26#include <winpr/clipboard.h>
27
28#include "clipboard.h"
29
30#include "synthetic_file.h"
31
32#include "../log.h"
33#define TAG WINPR_TAG("clipboard")
34
35const char* const mime_text_plain = "text/plain";
36
45static const char* CF_STANDARD_STRINGS[] = {
46 "CF_RAW", /* 0 */
47 "CF_TEXT", /* 1 */
48 "CF_BITMAP", /* 2 */
49 "CF_METAFILEPICT", /* 3 */
50 "CF_SYLK", /* 4 */
51 "CF_DIF", /* 5 */
52 "CF_TIFF", /* 6 */
53 "CF_OEMTEXT", /* 7 */
54 "CF_DIB", /* 8 */
55 "CF_PALETTE", /* 9 */
56 "CF_PENDATA", /* 10 */
57 "CF_RIFF", /* 11 */
58 "CF_WAVE", /* 12 */
59 "CF_UNICODETEXT", /* 13 */
60 "CF_ENHMETAFILE", /* 14 */
61 "CF_HDROP", /* 15 */
62 "CF_LOCALE", /* 16 */
63 "CF_DIBV5" /* 17 */
64};
65
66const char* ClipboardGetFormatIdString(UINT32 formatId)
67{
68 if (formatId < ARRAYSIZE(CF_STANDARD_STRINGS))
69 return CF_STANDARD_STRINGS[formatId];
70 return "CF_REGISTERED_FORMAT";
71}
72
73static wClipboardFormat* ClipboardFindFormat(wClipboard* clipboard, UINT32 formatId,
74 const char* name)
75{
76 wClipboardFormat* format = NULL;
77
78 if (!clipboard)
79 return NULL;
80
81 if (formatId)
82 {
83 for (UINT32 index = 0; index < clipboard->numFormats; index++)
84 {
85 wClipboardFormat* cformat = &clipboard->formats[index];
86 if (formatId == cformat->formatId)
87 {
88 format = cformat;
89 break;
90 }
91 }
92 }
93 else if (name)
94 {
95 for (UINT32 index = 0; index < clipboard->numFormats; index++)
96 {
97 wClipboardFormat* cformat = &clipboard->formats[index];
98 if (!cformat->formatName)
99 continue;
100
101 if (strcmp(name, cformat->formatName) == 0)
102 {
103 format = cformat;
104 break;
105 }
106 }
107 }
108 else
109 {
110 /* special "CF_RAW" case */
111 if (clipboard->numFormats > 0)
112 {
113 format = &clipboard->formats[0];
114
115 if (format->formatId)
116 return NULL;
117
118 if (!format->formatName || (strcmp(format->formatName, CF_STANDARD_STRINGS[0]) == 0))
119 return format;
120 }
121 }
122
123 return format;
124}
125
126static wClipboardSynthesizer* ClipboardFindSynthesizer(wClipboardFormat* format, UINT32 formatId)
127{
128 if (!format)
129 return NULL;
130
131 for (UINT32 index = 0; index < format->numSynthesizers; index++)
132 {
133 wClipboardSynthesizer* synthesizer = &(format->synthesizers[index]);
134
135 if (formatId == synthesizer->syntheticId)
136 return synthesizer;
137 }
138
139 return NULL;
140}
141
142void ClipboardLock(wClipboard* clipboard)
143{
144 if (!clipboard)
145 return;
146
147 EnterCriticalSection(&(clipboard->lock));
148}
149
150void ClipboardUnlock(wClipboard* clipboard)
151{
152 if (!clipboard)
153 return;
154
155 LeaveCriticalSection(&(clipboard->lock));
156}
157
158BOOL ClipboardEmpty(wClipboard* clipboard)
159{
160 if (!clipboard)
161 return FALSE;
162
163 if (clipboard->data)
164 {
165 free(clipboard->data);
166 clipboard->data = NULL;
167 }
168
169 clipboard->size = 0;
170 clipboard->formatId = 0;
171 clipboard->sequenceNumber++;
172 return TRUE;
173}
174
175UINT32 ClipboardCountRegisteredFormats(wClipboard* clipboard)
176{
177 if (!clipboard)
178 return 0;
179
180 return clipboard->numFormats;
181}
182
183UINT32 ClipboardGetRegisteredFormatIds(wClipboard* clipboard, UINT32** ppFormatIds)
184{
185 UINT32* pFormatIds = NULL;
186 wClipboardFormat* format = NULL;
187
188 if (!clipboard)
189 return 0;
190
191 if (!ppFormatIds)
192 return 0;
193
194 pFormatIds = *ppFormatIds;
195
196 if (!pFormatIds)
197 {
198 pFormatIds = calloc(clipboard->numFormats, sizeof(UINT32));
199
200 if (!pFormatIds)
201 return 0;
202
203 *ppFormatIds = pFormatIds;
204 }
205
206 for (UINT32 index = 0; index < clipboard->numFormats; index++)
207 {
208 format = &(clipboard->formats[index]);
209 pFormatIds[index] = format->formatId;
210 }
211
212 return clipboard->numFormats;
213}
214
215UINT32 ClipboardRegisterFormat(wClipboard* clipboard, const char* name)
216{
217 wClipboardFormat* format = NULL;
218
219 if (!clipboard)
220 return 0;
221
222 format = ClipboardFindFormat(clipboard, 0, name);
223
224 if (format)
225 return format->formatId;
226
227 if ((clipboard->numFormats + 1) >= clipboard->maxFormats)
228 {
229 UINT32 numFormats = clipboard->maxFormats * 2;
230 wClipboardFormat* tmpFormat = NULL;
231 tmpFormat =
232 (wClipboardFormat*)realloc(clipboard->formats, numFormats * sizeof(wClipboardFormat));
233
234 if (!tmpFormat)
235 return 0;
236
237 clipboard->formats = tmpFormat;
238 clipboard->maxFormats = numFormats;
239 }
240
241 format = &(clipboard->formats[clipboard->numFormats]);
242 ZeroMemory(format, sizeof(wClipboardFormat));
243
244 if (name)
245 {
246 format->formatName = _strdup(name);
247
248 if (!format->formatName)
249 return 0;
250 }
251
252 format->formatId = clipboard->nextFormatId++;
253 clipboard->numFormats++;
254 return format->formatId;
255}
256
257BOOL ClipboardRegisterSynthesizer(wClipboard* clipboard, UINT32 formatId, UINT32 syntheticId,
258 CLIPBOARD_SYNTHESIZE_FN pfnSynthesize)
259{
260 UINT32 index = 0;
261 wClipboardFormat* format = NULL;
262 wClipboardSynthesizer* synthesizer = NULL;
263
264 if (!clipboard)
265 return FALSE;
266
267 format = ClipboardFindFormat(clipboard, formatId, NULL);
268
269 if (!format)
270 return FALSE;
271
272 if (format->formatId == syntheticId)
273 return FALSE;
274
275 synthesizer = ClipboardFindSynthesizer(format, formatId);
276
277 if (!synthesizer)
278 {
279 wClipboardSynthesizer* tmpSynthesizer = NULL;
280 UINT32 numSynthesizers = format->numSynthesizers + 1;
281 tmpSynthesizer = (wClipboardSynthesizer*)realloc(
282 format->synthesizers, numSynthesizers * sizeof(wClipboardSynthesizer));
283
284 if (!tmpSynthesizer)
285 return FALSE;
286
287 format->synthesizers = tmpSynthesizer;
288 format->numSynthesizers = numSynthesizers;
289 index = numSynthesizers - 1;
290 synthesizer = &(format->synthesizers[index]);
291 }
292
293 synthesizer->syntheticId = syntheticId;
294 synthesizer->pfnSynthesize = pfnSynthesize;
295 return TRUE;
296}
297
298UINT32 ClipboardCountFormats(wClipboard* clipboard)
299{
300 UINT32 count = 0;
301 wClipboardFormat* format = NULL;
302
303 if (!clipboard)
304 return 0;
305
306 format = ClipboardFindFormat(clipboard, clipboard->formatId, NULL);
307
308 if (!format)
309 return 0;
310
311 count = 1 + format->numSynthesizers;
312 return count;
313}
314
315UINT32 ClipboardGetFormatIds(wClipboard* clipboard, UINT32** ppFormatIds)
316{
317 UINT32 count = 0;
318 UINT32* pFormatIds = NULL;
319 wClipboardFormat* format = NULL;
320 wClipboardSynthesizer* synthesizer = NULL;
321
322 if (!clipboard)
323 return 0;
324
325 format = ClipboardFindFormat(clipboard, clipboard->formatId, NULL);
326
327 if (!format)
328 return 0;
329
330 count = 1 + format->numSynthesizers;
331
332 if (!ppFormatIds)
333 return 0;
334
335 pFormatIds = *ppFormatIds;
336
337 if (!pFormatIds)
338 {
339 pFormatIds = calloc(count, sizeof(UINT32));
340
341 if (!pFormatIds)
342 return 0;
343
344 *ppFormatIds = pFormatIds;
345 }
346
347 pFormatIds[0] = format->formatId;
348
349 for (UINT32 index = 1; index < count; index++)
350 {
351 synthesizer = &(format->synthesizers[index - 1]);
352 pFormatIds[index] = synthesizer->syntheticId;
353 }
354
355 return count;
356}
357
358static void ClipboardUninitFormats(wClipboard* clipboard)
359{
360 WINPR_ASSERT(clipboard);
361 for (UINT32 formatId = 0; formatId < clipboard->numFormats; formatId++)
362 {
363 wClipboardFormat* format = &clipboard->formats[formatId];
364 free(format->formatName);
365 free(format->synthesizers);
366 format->formatName = NULL;
367 format->synthesizers = NULL;
368 }
369}
370
371static BOOL ClipboardInitFormats(wClipboard* clipboard)
372{
373 UINT32 formatId = 0;
374 wClipboardFormat* format = NULL;
375
376 if (!clipboard)
377 return FALSE;
378
379 for (formatId = 0; formatId < CF_MAX; formatId++, clipboard->numFormats++)
380 {
381 format = &(clipboard->formats[clipboard->numFormats]);
382 ZeroMemory(format, sizeof(wClipboardFormat));
383 format->formatId = formatId;
384 format->formatName = _strdup(CF_STANDARD_STRINGS[formatId]);
385
386 if (!format->formatName)
387 goto error;
388 }
389
390 if (!ClipboardInitSynthesizers(clipboard))
391 goto error;
392
393 return TRUE;
394error:
395
396 ClipboardUninitFormats(clipboard);
397 return FALSE;
398}
399
400UINT32 ClipboardGetFormatId(wClipboard* clipboard, const char* name)
401{
402 wClipboardFormat* format = NULL;
403
404 if (!clipboard)
405 return 0;
406
407 format = ClipboardFindFormat(clipboard, 0, name);
408
409 if (!format)
410 return 0;
411
412 return format->formatId;
413}
414
415const char* ClipboardGetFormatName(wClipboard* clipboard, UINT32 formatId)
416{
417 wClipboardFormat* format = NULL;
418
419 if (!clipboard)
420 return NULL;
421
422 format = ClipboardFindFormat(clipboard, formatId, NULL);
423
424 if (!format)
425 return NULL;
426
427 return format->formatName;
428}
429
430void* ClipboardGetData(wClipboard* clipboard, UINT32 formatId, UINT32* pSize)
431{
432 UINT32 SrcSize = 0;
433 UINT32 DstSize = 0;
434 void* pSrcData = NULL;
435 void* pDstData = NULL;
436 wClipboardFormat* format = NULL;
437 wClipboardSynthesizer* synthesizer = NULL;
438
439 if (!clipboard || !pSize)
440 {
441 WLog_ERR(TAG, "Invalid parameters clipboard=%p, pSize=%p",
442 WINPR_CXX_COMPAT_CAST(const void*, clipboard),
443 WINPR_CXX_COMPAT_CAST(const void*, pSize));
444 return NULL;
445 }
446
447 *pSize = 0;
448 format = ClipboardFindFormat(clipboard, clipboard->formatId, NULL);
449
450 if (!format)
451 {
452 WLog_ERR(TAG, "Format [0x%08" PRIx32 "] not found", clipboard->formatId);
453 return NULL;
454 }
455
456 SrcSize = clipboard->size;
457 pSrcData = clipboard->data;
458
459 if (formatId == format->formatId)
460 {
461 DstSize = SrcSize;
462 pDstData = malloc(DstSize);
463
464 if (!pDstData)
465 return NULL;
466
467 CopyMemory(pDstData, pSrcData, SrcSize);
468 *pSize = DstSize;
469 }
470 else
471 {
472 synthesizer = ClipboardFindSynthesizer(format, formatId);
473
474 if (!synthesizer || !synthesizer->pfnSynthesize)
475 {
476 WLog_ERR(TAG, "No synthesizer for format %s [0x%08" PRIx32 "] --> %s [0x%08" PRIx32 "]",
477 ClipboardGetFormatName(clipboard, clipboard->formatId), clipboard->formatId,
478 ClipboardGetFormatName(clipboard, formatId), formatId);
479 return NULL;
480 }
481
482 DstSize = SrcSize;
483 pDstData = synthesizer->pfnSynthesize(clipboard, format->formatId, pSrcData, &DstSize);
484 if (pDstData)
485 *pSize = DstSize;
486 }
487
488 WLog_DBG(TAG, "getting formatId=%s [0x%08" PRIx32 "] data=%p, size=%" PRIu32,
489 ClipboardGetFormatName(clipboard, formatId), formatId, pDstData, *pSize);
490 return pDstData;
491}
492
493BOOL ClipboardSetData(wClipboard* clipboard, UINT32 formatId, const void* data, UINT32 size)
494{
495 wClipboardFormat* format = NULL;
496
497 WLog_DBG(TAG, "setting formatId=%s [0x%08" PRIx32 "], size=%" PRIu32,
498 ClipboardGetFormatName(clipboard, formatId), formatId, size);
499 if (!clipboard)
500 return FALSE;
501
502 format = ClipboardFindFormat(clipboard, formatId, NULL);
503
504 if (!format)
505 return FALSE;
506
507 free(clipboard->data);
508
509 clipboard->data = calloc(size + sizeof(WCHAR), sizeof(char));
510
511 if (!clipboard->data)
512 return FALSE;
513
514 memcpy(clipboard->data, data, size);
515
516 /* For string values we don“t know if they are '\0' terminated.
517 * so set the size to the full length in bytes (e.g. string length + 1)
518 */
519 switch (formatId)
520 {
521 case CF_TEXT:
522 case CF_OEMTEXT:
523 clipboard->size = (UINT32)(strnlen(clipboard->data, size) + 1UL);
524 break;
525 case CF_UNICODETEXT:
526 clipboard->size =
527 (UINT32)((_wcsnlen(clipboard->data, size / sizeof(WCHAR)) + 1UL) * sizeof(WCHAR));
528 break;
529 default:
530 clipboard->size = size;
531 break;
532 }
533
534 clipboard->formatId = formatId;
535 clipboard->sequenceNumber++;
536 return TRUE;
537}
538
539UINT64 ClipboardGetOwner(wClipboard* clipboard)
540{
541 if (!clipboard)
542 return 0;
543
544 return clipboard->ownerId;
545}
546
547void ClipboardSetOwner(wClipboard* clipboard, UINT64 ownerId)
548{
549 if (!clipboard)
550 return;
551
552 clipboard->ownerId = ownerId;
553}
554
555wClipboardDelegate* ClipboardGetDelegate(wClipboard* clipboard)
556{
557 if (!clipboard)
558 return NULL;
559
560 return &clipboard->delegate;
561}
562
563static void ClipboardInitLocalFileSubsystem(wClipboard* clipboard)
564{
565 /*
566 * There can be only one local file subsystem active.
567 * Return as soon as initialization succeeds.
568 */
569 if (ClipboardInitSyntheticFileSubsystem(clipboard))
570 {
571 WLog_DBG(TAG, "initialized synthetic local file subsystem");
572 return;
573 }
574 else
575 {
576 WLog_WARN(TAG, "failed to initialize synthetic local file subsystem");
577 }
578
579 WLog_INFO(TAG, "failed to initialize local file subsystem, file transfer not available");
580}
581
582wClipboard* ClipboardCreate(void)
583{
584 wClipboard* clipboard = (wClipboard*)calloc(1, sizeof(wClipboard));
585
586 if (!clipboard)
587 return NULL;
588
589 clipboard->nextFormatId = 0xC000;
590 clipboard->sequenceNumber = 0;
591
592 if (!InitializeCriticalSectionAndSpinCount(&(clipboard->lock), 4000))
593 goto fail;
594
595 clipboard->numFormats = 0;
596 clipboard->maxFormats = 64;
597 clipboard->formats = (wClipboardFormat*)calloc(clipboard->maxFormats, sizeof(wClipboardFormat));
598
599 if (!clipboard->formats)
600 goto fail;
601
602 if (!ClipboardInitFormats(clipboard))
603 goto fail;
604
605 clipboard->delegate.clipboard = clipboard;
606 ClipboardInitLocalFileSubsystem(clipboard);
607 return clipboard;
608fail:
609 ClipboardDestroy(clipboard);
610 return NULL;
611}
612
613void ClipboardDestroy(wClipboard* clipboard)
614{
615 if (!clipboard)
616 return;
617
618 ArrayList_Free(clipboard->localFiles);
619 clipboard->localFiles = NULL;
620
621 ClipboardUninitFormats(clipboard);
622
623 free(clipboard->data);
624 clipboard->data = NULL;
625 clipboard->size = 0;
626 clipboard->numFormats = 0;
627 free(clipboard->formats);
628 DeleteCriticalSection(&(clipboard->lock));
629 free(clipboard);
630}
631
632static BOOL is_dos_drive(const char* path, size_t len)
633{
634 if (len < 2)
635 return FALSE;
636
637 WINPR_ASSERT(path);
638 if (path[1] == ':' || path[1] == '|')
639 {
640 if (((path[0] >= 'A') && (path[0] <= 'Z')) || ((path[0] >= 'a') && (path[0] <= 'z')))
641 return TRUE;
642 }
643 return FALSE;
644}
645
646char* parse_uri_to_local_file(const char* uri, size_t uri_len)
647{
648 // URI is specified by RFC 8089: https://datatracker.ietf.org/doc/html/rfc8089
649 const char prefix[] = "file:";
650 const char prefixTraditional[] = "file://";
651 const char* localName = NULL;
652 size_t localLen = 0;
653 char* buffer = NULL;
654 const size_t prefixLen = strnlen(prefix, sizeof(prefix));
655 const size_t prefixTraditionalLen = strnlen(prefixTraditional, sizeof(prefixTraditional));
656
657 WINPR_ASSERT(uri || (uri_len == 0));
658
659 WLog_VRB(TAG, "processing URI: %.*s", WINPR_ASSERTING_INT_CAST(int, uri_len), uri);
660
661 if ((uri_len <= prefixLen) || strncmp(uri, prefix, prefixLen) != 0)
662 {
663 WLog_ERR(TAG, "non-'file:' URI schemes are not supported");
664 return NULL;
665 }
666
667 do
668 {
669 /* https://datatracker.ietf.org/doc/html/rfc8089#appendix-F
670 * - The minimal representation of a local file in a DOS- or Windows-
671 * based environment with no authority field and an absolute path
672 * that begins with a drive letter.
673 *
674 * "file:c:/path/to/file"
675 *
676 * - Regular DOS or Windows file URIs with vertical line characters in
677 * the drive letter construct.
678 *
679 * "file:c|/path/to/file"
680 *
681 */
682 if (uri[prefixLen] != '/')
683 {
684
685 if (is_dos_drive(&uri[prefixLen], uri_len - prefixLen))
686 {
687 // Dos and Windows file URI
688 localName = &uri[prefixLen];
689 localLen = uri_len - prefixLen;
690 break;
691 }
692 else
693 {
694 WLog_ERR(TAG, "URI format are not supported: %s", uri);
695 return NULL;
696 }
697 }
698
699 /*
700 * - The minimal representation of a local file with no authority field
701 * and an absolute path that begins with a slash "/". For example:
702 *
703 * "file:/path/to/file"
704 *
705 */
706 else if ((uri_len > prefixLen + 1) && (uri[prefixLen + 1] != '/'))
707 {
708 if (is_dos_drive(&uri[prefixLen + 1], uri_len - prefixLen - 1))
709 {
710 // Dos and Windows file URI
711 localName = (uri + prefixLen + 1);
712 localLen = uri_len - prefixLen - 1;
713 }
714 else
715 {
716 localName = &uri[prefixLen];
717 localLen = uri_len - prefixLen;
718 }
719 break;
720 }
721
722 /*
723 * - A traditional file URI for a local file with an empty authority.
724 *
725 * "file:///path/to/file"
726 */
727 if ((uri_len < prefixTraditionalLen) ||
728 strncmp(uri, prefixTraditional, prefixTraditionalLen) != 0)
729 {
730 WLog_ERR(TAG, "non-'file:' URI schemes are not supported");
731 return NULL;
732 }
733
734 localName = &uri[prefixTraditionalLen];
735 localLen = uri_len - prefixTraditionalLen;
736
737 if (localLen < 1)
738 {
739 WLog_ERR(TAG, "empty 'file:' URI schemes are not supported");
740 return NULL;
741 }
742
743 /*
744 * "file:///c:/path/to/file"
745 * "file:///c|/path/to/file"
746 */
747 if (localName[0] != '/')
748 {
749 WLog_ERR(TAG, "URI format are not supported: %s", uri);
750 return NULL;
751 }
752
753 if (is_dos_drive(&localName[1], localLen - 1))
754 {
755 localName++;
756 localLen--;
757 }
758
759 } while (0);
760
761 buffer = winpr_str_url_decode(localName, localLen);
762 if (buffer)
763 {
764 if (buffer[1] == '|' &&
765 ((buffer[0] >= 'A' && buffer[0] <= 'Z') || (buffer[0] >= 'a' && buffer[0] <= 'z')))
766 buffer[1] = ':';
767 return buffer;
768 }
769
770 return NULL;
771}