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* 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", clipboard, pSize);
442 return NULL;
443 }
444
445 *pSize = 0;
446 format = ClipboardFindFormat(clipboard, clipboard->formatId, NULL);
447
448 if (!format)
449 {
450 WLog_ERR(TAG, "Format [0x%08" PRIx32 "] not found", clipboard->formatId);
451 return NULL;
452 }
453
454 SrcSize = clipboard->size;
455 pSrcData = clipboard->data;
456
457 if (formatId == format->formatId)
458 {
459 DstSize = SrcSize;
460 pDstData = malloc(DstSize);
461
462 if (!pDstData)
463 return NULL;
464
465 CopyMemory(pDstData, pSrcData, SrcSize);
466 *pSize = DstSize;
467 }
468 else
469 {
470 synthesizer = ClipboardFindSynthesizer(format, formatId);
471
472 if (!synthesizer || !synthesizer->pfnSynthesize)
473 {
474 WLog_ERR(TAG, "No synthesizer for format %s [0x%08" PRIx32 "] --> %s [0x%08" PRIx32 "]",
475 ClipboardGetFormatName(clipboard, clipboard->formatId), clipboard->formatId,
476 ClipboardGetFormatName(clipboard, formatId), formatId);
477 return NULL;
478 }
479
480 DstSize = SrcSize;
481 pDstData = synthesizer->pfnSynthesize(clipboard, format->formatId, pSrcData, &DstSize);
482 if (pDstData)
483 *pSize = DstSize;
484 }
485
486 WLog_DBG(TAG, "getting formatId=%s [0x%08" PRIx32 "] data=%p, size=%" PRIu32,
487 ClipboardGetFormatName(clipboard, formatId), formatId, pDstData, *pSize);
488 return pDstData;
489}
490
491BOOL ClipboardSetData(wClipboard* clipboard, UINT32 formatId, const void* data, UINT32 size)
492{
493 wClipboardFormat* format = NULL;
494
495 WLog_DBG(TAG, "setting formatId=%s [0x%08" PRIx32 "], size=%" PRIu32,
496 ClipboardGetFormatName(clipboard, formatId), formatId, size);
497 if (!clipboard)
498 return FALSE;
499
500 format = ClipboardFindFormat(clipboard, formatId, NULL);
501
502 if (!format)
503 return FALSE;
504
505 free(clipboard->data);
506
507 clipboard->data = calloc(size + sizeof(WCHAR), sizeof(char));
508
509 if (!clipboard->data)
510 return FALSE;
511
512 memcpy(clipboard->data, data, size);
513
514 /* For string values we don“t know if they are '\0' terminated.
515 * so set the size to the full length in bytes (e.g. string length + 1)
516 */
517 switch (formatId)
518 {
519 case CF_TEXT:
520 case CF_OEMTEXT:
521 clipboard->size = (UINT32)(strnlen(clipboard->data, size) + 1UL);
522 break;
523 case CF_UNICODETEXT:
524 clipboard->size =
525 (UINT32)((_wcsnlen(clipboard->data, size / sizeof(WCHAR)) + 1UL) * sizeof(WCHAR));
526 break;
527 default:
528 clipboard->size = size;
529 break;
530 }
531
532 clipboard->formatId = formatId;
533 clipboard->sequenceNumber++;
534 return TRUE;
535}
536
537UINT64 ClipboardGetOwner(wClipboard* clipboard)
538{
539 if (!clipboard)
540 return 0;
541
542 return clipboard->ownerId;
543}
544
545void ClipboardSetOwner(wClipboard* clipboard, UINT64 ownerId)
546{
547 if (!clipboard)
548 return;
549
550 clipboard->ownerId = ownerId;
551}
552
553wClipboardDelegate* ClipboardGetDelegate(wClipboard* clipboard)
554{
555 if (!clipboard)
556 return NULL;
557
558 return &clipboard->delegate;
559}
560
561static void ClipboardInitLocalFileSubsystem(wClipboard* clipboard)
562{
563 /*
564 * There can be only one local file subsystem active.
565 * Return as soon as initialization succeeds.
566 */
567 if (ClipboardInitSyntheticFileSubsystem(clipboard))
568 {
569 WLog_DBG(TAG, "initialized synthetic local file subsystem");
570 return;
571 }
572 else
573 {
574 WLog_WARN(TAG, "failed to initialize synthetic local file subsystem");
575 }
576
577 WLog_INFO(TAG, "failed to initialize local file subsystem, file transfer not available");
578}
579
580wClipboard* ClipboardCreate(void)
581{
582 wClipboard* clipboard = (wClipboard*)calloc(1, sizeof(wClipboard));
583
584 if (!clipboard)
585 return NULL;
586
587 clipboard->nextFormatId = 0xC000;
588 clipboard->sequenceNumber = 0;
589
590 if (!InitializeCriticalSectionAndSpinCount(&(clipboard->lock), 4000))
591 goto fail;
592
593 clipboard->numFormats = 0;
594 clipboard->maxFormats = 64;
595 clipboard->formats = (wClipboardFormat*)calloc(clipboard->maxFormats, sizeof(wClipboardFormat));
596
597 if (!clipboard->formats)
598 goto fail;
599
600 if (!ClipboardInitFormats(clipboard))
601 goto fail;
602
603 clipboard->delegate.clipboard = clipboard;
604 ClipboardInitLocalFileSubsystem(clipboard);
605 return clipboard;
606fail:
607 ClipboardDestroy(clipboard);
608 return NULL;
609}
610
611void ClipboardDestroy(wClipboard* clipboard)
612{
613 if (!clipboard)
614 return;
615
616 ArrayList_Free(clipboard->localFiles);
617 clipboard->localFiles = NULL;
618
619 ClipboardUninitFormats(clipboard);
620
621 free(clipboard->data);
622 clipboard->data = NULL;
623 clipboard->size = 0;
624 clipboard->numFormats = 0;
625 free(clipboard->formats);
626 DeleteCriticalSection(&(clipboard->lock));
627 free(clipboard);
628}
629
630static BOOL is_dos_drive(const char* path, size_t len)
631{
632 if (len < 2)
633 return FALSE;
634
635 WINPR_ASSERT(path);
636 if (path[1] == ':' || path[1] == '|')
637 {
638 if (((path[0] >= 'A') && (path[0] <= 'Z')) || ((path[0] >= 'a') && (path[0] <= 'z')))
639 return TRUE;
640 }
641 return FALSE;
642}
643
644char* parse_uri_to_local_file(const char* uri, size_t uri_len)
645{
646 // URI is specified by RFC 8089: https://datatracker.ietf.org/doc/html/rfc8089
647 const char prefix[] = "file:";
648 const char prefixTraditional[] = "file://";
649 const char* localName = NULL;
650 size_t localLen = 0;
651 char* buffer = NULL;
652 const size_t prefixLen = strnlen(prefix, sizeof(prefix));
653 const size_t prefixTraditionalLen = strnlen(prefixTraditional, sizeof(prefixTraditional));
654
655 WINPR_ASSERT(uri || (uri_len == 0));
656
657 WLog_VRB(TAG, "processing URI: %.*s", uri_len, uri);
658
659 if ((uri_len <= prefixLen) || strncmp(uri, prefix, prefixLen) != 0)
660 {
661 WLog_ERR(TAG, "non-'file:' URI schemes are not supported");
662 return NULL;
663 }
664
665 do
666 {
667 /* https://datatracker.ietf.org/doc/html/rfc8089#appendix-F
668 * - The minimal representation of a local file in a DOS- or Windows-
669 * based environment with no authority field and an absolute path
670 * that begins with a drive letter.
671 *
672 * "file:c:/path/to/file"
673 *
674 * - Regular DOS or Windows file URIs with vertical line characters in
675 * the drive letter construct.
676 *
677 * "file:c|/path/to/file"
678 *
679 */
680 if (uri[prefixLen] != '/')
681 {
682
683 if (is_dos_drive(&uri[prefixLen], uri_len - prefixLen))
684 {
685 // Dos and Windows file URI
686 localName = &uri[prefixLen];
687 localLen = uri_len - prefixLen;
688 break;
689 }
690 else
691 {
692 WLog_ERR(TAG, "URI format are not supported: %s", uri);
693 return NULL;
694 }
695 }
696
697 /*
698 * - The minimal representation of a local file with no authority field
699 * and an absolute path that begins with a slash "/". For example:
700 *
701 * "file:/path/to/file"
702 *
703 */
704 else if ((uri_len > prefixLen + 1) && (uri[prefixLen + 1] != '/'))
705 {
706 if (is_dos_drive(&uri[prefixLen + 1], uri_len - prefixLen - 1))
707 {
708 // Dos and Windows file URI
709 localName = (uri + prefixLen + 1);
710 localLen = uri_len - prefixLen - 1;
711 }
712 else
713 {
714 localName = &uri[prefixLen];
715 localLen = uri_len - prefixLen;
716 }
717 break;
718 }
719
720 /*
721 * - A traditional file URI for a local file with an empty authority.
722 *
723 * "file:///path/to/file"
724 */
725 if ((uri_len < prefixTraditionalLen) ||
726 strncmp(uri, prefixTraditional, prefixTraditionalLen) != 0)
727 {
728 WLog_ERR(TAG, "non-'file:' URI schemes are not supported");
729 return NULL;
730 }
731
732 localName = &uri[prefixTraditionalLen];
733 localLen = uri_len - prefixTraditionalLen;
734
735 if (localLen < 1)
736 {
737 WLog_ERR(TAG, "empty 'file:' URI schemes are not supported");
738 return NULL;
739 }
740
741 /*
742 * "file:///c:/path/to/file"
743 * "file:///c|/path/to/file"
744 */
745 if (localName[0] != '/')
746 {
747 WLog_ERR(TAG, "URI format are not supported: %s", uri);
748 return NULL;
749 }
750
751 if (is_dos_drive(&localName[1], localLen - 1))
752 {
753 localName++;
754 localLen--;
755 }
756
757 } while (0);
758
759 buffer = winpr_str_url_decode(localName, localLen);
760 if (buffer)
761 {
762 if (buffer[1] == '|' &&
763 ((buffer[0] >= 'A' && buffer[0] <= 'Z') || (buffer[0] >= 'a' && buffer[0] <= 'z')))
764 buffer[1] = ':';
765 return buffer;
766 }
767
768 return NULL;
769}