forked from IcebergThings/weston
/
rdpclip.c
1823 lines (1573 loc) · 66.4 KB
/
rdpclip.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright © 2020 Microsoft
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice (including the
* next paragraph) shall be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "config.h"
#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <linux/input.h>
#include <stdio.h>
#include "rdp.h"
#include "libweston-internal.h"
/* From MSDN, RegisterClipboardFormat API.
Registered clipboard formats are identified by values in the range 0xC000 through 0xFFFF. */
#define CF_PRIVATE_RTF 49309 /* fake format ID for "Rich Text Format". */
#define CF_PRIVATE_HTML 49405 /* fake format ID for "HTML Format".*/
/* 1 2 3 4 5 6 7 8 */
/*01234567890 1 2345678901234 5 67890123456 7 89012345678901234567890 1 234567890123456789012 3 4*/
static const char rdp_clipboard_html_header[] = "Version:0.9\r\nStartHTML:-1\r\nEndHTML:-1\r\nStartFragment:00000000\r\nEndFragment:00000000\r\n";
#define RDP_CLIPBOARD_FRAGMENT_START_OFFSET (53) //---------------------------------------------------------+ |
#define RDP_CLIPBOARD_FRAGMENT_END_OFFSET (75) //-----------------------------------------------------------------------------------+
/*
* https://docs.microsoft.com/en-us/windows/win32/dataxchg/html-clipboard-format
*
* The fragment should be preceded and followed by the HTML comments and
* (no space allowed between the !-- and the text) to conveniently
* indicate where the fragment starts and ends.
*/
static const char rdp_clipboard_html_fragment_start[] = "<!--StartFragment-->\r\n";
static const char rdp_clipboard_html_fragment_end[] = "<!--EndFragment-->\r\n";
struct rdp_clipboard_data_source;
typedef bool (*pfn_process_data)(struct rdp_clipboard_data_source *source, bool is_send);
struct rdp_clipboard_supported_format {
uint32_t format_id;
char *format_name;
char *mime_type;
pfn_process_data pfn;
};
static bool
clipboard_process_text_utf8(struct rdp_clipboard_data_source *source, bool is_send);
static bool
clipboard_process_text_raw(struct rdp_clipboard_data_source *source, bool is_send);
static bool
clipboard_process_bmp(struct rdp_clipboard_data_source *source , bool is_send);
static bool
clipboard_process_html(struct rdp_clipboard_data_source *source, bool is_send);
/* TODO: need to support to 1:n or m:n format conversion.
* For example, CF_UNICODETEXT to "UTF8_STRING" as well as "text/plain;charset=utf-8".
*/
struct rdp_clipboard_supported_format clipboard_supported_formats[] = {
{ CF_UNICODETEXT, NULL, "text/plain;charset=utf-8", clipboard_process_text_utf8 },
{ CF_TEXT, NULL, "STRING", clipboard_process_text_raw },
{ CF_DIB, NULL, "image/bmp", clipboard_process_bmp },
{ CF_PRIVATE_RTF, "Rich Text Format", "text/rtf", clipboard_process_text_raw },
{ CF_PRIVATE_HTML, "HTML Format", "text/html", clipboard_process_html },
};
#define RDP_NUM_CLIPBOARD_FORMATS ARRAY_LENGTH(clipboard_supported_formats)
enum rdp_clipboard_data_source_state {
RDP_CLIPBOARD_SOURCE_ALLOCATED = 0,
RDP_CLIPBOARD_SOURCE_FORMATLIST_READY, /* format list obtained from provider */
RDP_CLIPBOARD_SOURCE_PUBLISHED, /* availablity of some or no clipboard data notified to consumer */
RDP_CLIPBOARD_SOURCE_REQUEST_DATA, /* data request sent to provider */
RDP_CLIPBOARD_SOURCE_RECEIVED_DATA, /* data was received from provider, waiting data to be dispatched to consumer */
RDP_CLIPBOARD_SOURCE_TRANSFERING, /* transfering data to consumer */
RDP_CLIPBOARD_SOURCE_TRANSFERRED, /* completed transfering data to consumer */
RDP_CLIPBOARD_SOURCE_CANCEL_PENDING, /* data transfer cancel requested */
RDP_CLIPBOARD_SOURCE_CANCELED, /* data transfer canceled */
RDP_CLIPBOARD_SOURCE_RETRY, /* retry later */
RDP_CLIPBOARD_SOURCE_FAILED, /* failure occured */
};
struct rdp_clipboard_data_source {
struct weston_data_source base;
struct rdp_loop_task task_base;
struct wl_event_source *transfer_event_source; /* used for read/write with pipe */
struct wl_array data_contents;
void *context;
int refcount;
int data_source_fd;
int format_index;
enum rdp_clipboard_data_source_state state;
uint32_t data_response_fail_count;
uint32_t inflight_write_count;
void *inflight_data_to_write;
size_t inflight_data_size;
bool is_data_processed;
void *processed_data_start;
uint32_t processed_data_size;
bool processed_data_is_send;
bool is_canceled;
uint32_t client_format_id_table[RDP_NUM_CLIPBOARD_FORMATS];
};
struct rdp_clipboard_data_request {
struct rdp_loop_task task_base;
RdpPeerContext *ctx;
uint32_t requested_format_index;
};
static char *
clipboard_data_source_state_to_string(struct rdp_clipboard_data_source *source)
{
if (!source)
return "null";
switch (source->state) {
case RDP_CLIPBOARD_SOURCE_ALLOCATED:
return "allocated";
case RDP_CLIPBOARD_SOURCE_FORMATLIST_READY:
return "format list ready";
case RDP_CLIPBOARD_SOURCE_PUBLISHED:
return "published";
case RDP_CLIPBOARD_SOURCE_REQUEST_DATA:
return "request data";
case RDP_CLIPBOARD_SOURCE_RECEIVED_DATA:
return "received data";
case RDP_CLIPBOARD_SOURCE_TRANSFERING:
return "transferring";
case RDP_CLIPBOARD_SOURCE_TRANSFERRED:
return "transferred";
case RDP_CLIPBOARD_SOURCE_CANCEL_PENDING:
return "cancel pending";
case RDP_CLIPBOARD_SOURCE_CANCELED:
return "canceled";
case RDP_CLIPBOARD_SOURCE_RETRY:
return "retry";
case RDP_CLIPBOARD_SOURCE_FAILED:
return "failed";
}
assert(false);
return "unknown";
}
static bool
clipboard_process_text_utf8(struct rdp_clipboard_data_source *source, bool is_send)
{
freerdp_peer *client = (freerdp_peer *)source->context;
RdpPeerContext *ctx = (RdpPeerContext *)client->context;
struct rdp_backend *b = ctx->rdpBackend;
struct wl_array data_contents;
wl_array_init(&data_contents);
assert(!source->is_data_processed);
if (is_send) {
char *data = source->data_contents.data;
size_t data_size, data_size_in_char;
/* Linux to Windows (convert utf-8 to UNICODE) */
/* Include terminating NULL in size */
assert((source->data_contents.size + 1) <= source->data_contents.alloc);
data[source->data_contents.size] = '\0';
source->data_contents.size++;
/* obtain size in UNICODE */
data_size = MultiByteToWideChar(CP_UTF8, 0,
data,
source->data_contents.size,
NULL, 0);
if (data_size < 1)
goto error_return;
data_size *= 2; /* convert to size in bytes. */
if (!wl_array_add(&data_contents, data_size))
goto error_return;
/* convert to UNICODE */
data_size_in_char = MultiByteToWideChar(CP_UTF8, 0,
data,
source->data_contents.size,
data_contents.data,
data_size);
assert(data_contents.size == (data_size_in_char * 2));
} else {
/* Windows to Linux (UNICODE to utf-8) */
size_t data_size;
LPWSTR data = source->data_contents.data;
size_t data_size_in_char = source->data_contents.size / 2;
/* Windows's data has trailing chars, which Linux doesn't expect. */
while (data_size_in_char &&
((data[data_size_in_char-1] == L'\0') || (data[data_size_in_char-1] == L'\n')))
data_size_in_char -= 1;
if (!data_size_in_char)
goto error_return;
/* obtain size in utf-8 */
data_size = WideCharToMultiByte(CP_UTF8, 0,
source->data_contents.data,
data_size_in_char,
NULL, 0,
NULL, NULL);
if (data_size < 1)
goto error_return;
if (!wl_array_add(&data_contents, data_size))
goto error_return;
/* convert to utf-8 */
data_size = WideCharToMultiByte(CP_UTF8, 0,
source->data_contents.data,
data_size_in_char,
data_contents.data,
data_size,
NULL, NULL);
assert(data_contents.size == data_size);
}
/* swap the data_contents with new one */
wl_array_release(&source->data_contents);
source->data_contents = data_contents;
source->is_data_processed = true;
source->processed_data_start = source->data_contents.data;
source->processed_data_size = source->data_contents.size;
rdp_debug_clipboard_verbose(b, "RDP %s (%p:%s): %s (%u bytes)\n",
__func__, source,
clipboard_data_source_state_to_string(source),
is_send ? "send" : "receive",
(uint32_t)source->data_contents.size);
return true;
error_return:
source->state = RDP_CLIPBOARD_SOURCE_FAILED;
weston_log("RDP %s FAILED (%p:%s): %s (%u bytes)\n",
__func__, source,
clipboard_data_source_state_to_string(source),
is_send ? "send" : "receive",
(uint32_t)source->data_contents.size);
//rdp_debug_clipboard_verbose(b, "RDP %s FAILED (%p): %s \n\"%s\"\n (%d bytes)\n",
// __func__, source, is_send ? "send" : "receive",
// (char *)source->data_contents.data,
// (UINT32)source->data_contents.size);
wl_array_release(&data_contents);
return false;
}
static bool
clipboard_process_text_raw(struct rdp_clipboard_data_source *source, bool is_send)
{
freerdp_peer *client = (freerdp_peer *)source->context;
RdpPeerContext *ctx = (RdpPeerContext *)client->context;
struct rdp_backend *b = ctx->rdpBackend;
char *data = source->data_contents.data;
size_t data_size = source->data_contents.size;
assert(!source->is_data_processed);
if (is_send) {
/* Linux to Windows */
/* Include terminating NULL in size */
assert(data_size + 1 <= source->data_contents.alloc);
data[data_size] = '\0';
source->data_contents.size++;
} else {
/* Windows to Linux */
/* Windows's data has trailing chars, which Linux doesn't expect. */
while (data_size && ((data[data_size-1] == '\0') || (data[data_size-1] == '\n')))
data_size -= 1;
source->data_contents.size = data_size;
}
source->is_data_processed = true;
source->processed_data_start = source->data_contents.data;
source->processed_data_size = source->data_contents.size;
rdp_debug_clipboard_verbose(b, "RDP %s (%p): %s (%u bytes)\n",
__func__, source,
is_send ? "send" : "receive",
(uint32_t)source->data_contents.size);
//rdp_debug_clipboard_verbose(b, "RDP %s (%p): %s \n\"%s\"\n (%d bytes)\n",
// __func__, source, is_send ? "send" : "receive",
// (char *)source->data_contents.data,
// (UINT32)source->data_contents.size);
return true;
}
/* based off sample code at https://docs.microsoft.com/en-us/troubleshoot/cpp/add-html-code-clipboard
But this missing a lot of corner cases, it must be rewritten with use of proper HTML parser */
/* TODO: This doesn't work for converting HTML from Firefox in Wayland mode to Windows in certain cases,
because Firefox sends "<meta http-equiv="content-type" content="text/html; charset=utf-8">...", thus
this needs to property strip meta header and convert to the Windows clipboard style HTML. */
static bool
clipboard_process_html(struct rdp_clipboard_data_source *source, bool is_send)
{
freerdp_peer *client = (freerdp_peer *)source->context;
RdpPeerContext *ctx = (RdpPeerContext *)client->context;
struct rdp_backend *b = ctx->rdpBackend;
struct wl_array data_contents;
char *cur = source->data_contents.data;
assert(!source->is_data_processed);
/* We're treating the contents as a string for now, so null
* terminate it so strstr can't run off the end. However, we
* don't increase data_contents.size because we don't want
* to affect the content. */
assert(source->data_contents.size + 1 <= source->data_contents.alloc);
((char *)(source->data_contents.data))[source->data_contents.size] = '\0';
wl_array_init(&data_contents);
cur = strstr(cur, "<html");
if (!cur)
goto error_return;
if (!is_send) {
/* Windows to Linux */
size_t data_size = source->data_contents.size -
(cur - (char *)source->data_contents.data);
/* Windows's data has trailing chars, which Linux doesn't expect. */
while (data_size && ((cur[data_size-1] == '\0') || (cur[data_size-1] == '\n')))
data_size -= 1;
if (!data_size)
goto error_return;
if (!wl_array_add(&data_contents, data_size+1)) /* +1 for null */
goto error_return;
memcpy(data_contents.data, cur, data_size);
((char *)(data_contents.data))[data_size] = '\0';
data_contents.size = data_size;
} else {
/* Linux to Windows */
char *last, *buf;
uint32_t fragment_start, fragment_end;
if (!wl_array_add(&data_contents, source->data_contents.size+200))
goto error_return;
buf = data_contents.data;
strcpy(buf, rdp_clipboard_html_header);
last = cur;
cur = strstr(cur, "<body");
if (!cur)
goto error_return;
cur += 5;
while (*cur != '>' && *cur != '\0')
cur++;
if (*cur == '\0')
goto error_return;
cur++; /* include '>' */
strncat(buf, last, cur-last);
last = cur;
fragment_start = strlen(buf);
strcat(buf, rdp_clipboard_html_fragment_start);
cur = strstr(cur, "</body");
if (!cur)
goto error_return;
strncat(buf, last, cur-last);
fragment_end = strlen(buf);
strcat(buf, rdp_clipboard_html_fragment_end);
strcat(buf, cur);
cur = buf + RDP_CLIPBOARD_FRAGMENT_START_OFFSET;
sprintf(cur, "%08u", fragment_start);
*(cur+8) = '\r';
cur = buf + RDP_CLIPBOARD_FRAGMENT_END_OFFSET;
sprintf(cur, "%08u", fragment_end);
*(cur+8) = '\r';
data_contents.size = strlen(buf) + 1; /* +1 to null terminate. */
}
/* swap the data_contents with new one */
wl_array_release(&source->data_contents);
source->data_contents = data_contents;
//rdp_debug_clipboard_verbose(b, "RDP %s (%p): %s \n\"%s\"\n (%d bytes)\n",
// __func__, source, is_send ? "send" : "receive",
// (char *)source->data_contents.data,
// (UINT32)source->data_contents.size);
source->is_data_processed = true;
source->processed_data_start = source->data_contents.data;
source->processed_data_size = source->data_contents.size;
rdp_debug_clipboard_verbose(b, "RDP %s (%p:%s): %s (%u bytes)\n",
__func__, source, clipboard_data_source_state_to_string(source),
is_send ? "send" : "receive", (uint32_t)source->data_contents.size);
return true;
error_return:
source->state = RDP_CLIPBOARD_SOURCE_FAILED;
weston_log("RDP %s FAILED (%p:%s): %s (%u bytes)\n",
__func__, source, clipboard_data_source_state_to_string(source),
is_send ? "send" : "receive", (uint32_t)source->data_contents.size);
//rdp_debug_clipboard_verbose(b, "RDP %s FAILED (%p): %s \n\"%s\"\n (%d bytes)\n",
// __func__, source, is_send ? "send" : "receive",
// (char *)source->data_contents.data,
// (UINT32)source->data_contents.size);
wl_array_release(&data_contents);
return false;
}
#define DIB_HEADER_MARKER ((WORD) ('M' << 8) | 'B')
#define DIB_WIDTH_BYTES(bits) ((((bits) + 31) & ~31) >> 3)
static bool
clipboard_process_bmp(struct rdp_clipboard_data_source *source, bool is_send)
{
freerdp_peer *client = (freerdp_peer *)source->context;
RdpPeerContext *ctx = (RdpPeerContext *)client->context;
struct rdp_backend *b = ctx->rdpBackend;
BITMAPFILEHEADER *bmfh = NULL;
BITMAPINFOHEADER *bmih = NULL;
uint32_t color_table_size = 0;
struct wl_array data_contents;
assert(!source->is_data_processed);
wl_array_init(&data_contents);
if (is_send) {
/* Linux to Windows (remove BITMAPFILEHEADER) */
if (source->data_contents.size <= sizeof(*bmfh))
goto error_return;
bmfh = source->data_contents.data;
bmih = (BITMAPINFOHEADER *)(bmfh + 1);
source->is_data_processed = true;
source->processed_data_start = bmih;
source->processed_data_size = source->data_contents.size - sizeof(*bmfh);
} else {
/* Windows to Linux (insert BITMAPFILEHEADER) */
BITMAPFILEHEADER _bmfh = {};
if (source->data_contents.size <= sizeof(*bmih))
goto error_return;
bmih = source->data_contents.data;
bmfh = &_bmfh;
if (bmih->biCompression == BI_BITFIELDS)
color_table_size = sizeof(RGBQUAD) * 3;
else
color_table_size = sizeof(RGBQUAD) * bmih->biClrUsed;
bmfh->bfType = DIB_HEADER_MARKER;
bmfh->bfOffBits = sizeof(*bmfh) + bmih->biSize + color_table_size;
if (bmih->biSizeImage)
bmfh->bfSize = bmfh->bfOffBits + bmih->biSizeImage;
else if (bmih->biCompression == BI_BITFIELDS || bmih->biCompression == BI_RGB)
bmfh->bfSize = bmfh->bfOffBits +
(DIB_WIDTH_BYTES(bmih->biWidth * bmih->biBitCount) * abs(bmih->biHeight));
else
goto error_return;
/* source data must have enough size as described in its own bitmap header */
if (source->data_contents.size < (bmfh->bfSize - sizeof(*bmfh)))
goto error_return;
if (!wl_array_add(&data_contents, bmfh->bfSize))
goto error_return;
assert(data_contents.size == bmfh->bfSize);
/* copy generated BITMAPFILEHEADER */
memcpy(data_contents.data, bmfh, sizeof(*bmfh));
/* copy rest of bitmap data from source */
memcpy((char *)data_contents.data + sizeof(*bmfh),
source->data_contents.data, bmfh->bfSize - sizeof(*bmfh));
/* swap the data_contents with new one */
wl_array_release(&source->data_contents);
source->data_contents = data_contents;
source->is_data_processed = true;
source->processed_data_start = source->data_contents.data;
source->processed_data_size = source->data_contents.size;
}
rdp_debug_clipboard_verbose(b, "RDP %s (%p:%s): %s (%d bytes)\n",
__func__, source,
clipboard_data_source_state_to_string(source),
is_send ? "send" : "receive",
(uint32_t)source->data_contents.size);
/*
rdp_debug_clipboard_verbose_continue(b, " BITMAPFILEHEADER.bfType:0x%x\n", bmfh->bfType);
rdp_debug_clipboard_verbose_continue(b, " BITMAPFILEHEADER.bfSize:%d\n", bmfh->bfSize);
rdp_debug_clipboard_verbose_continue(b, " BITMAPFILEHEADER.bfOffBits:%d\n", bmfh->bfOffBits);
rdp_debug_clipboard_verbose_continue(b, " BITMAPINFOHEADER.biSize:%d\n", bmih->biSize);
rdp_debug_clipboard_verbose_continue(b, " BITMAPINFOHEADER.biWidth:%d\n", bmih->biWidth);
rdp_debug_clipboard_verbose_continue(b, " BITMAPINFOHEADER.biHeight:%d, y-Up:%s\n", abs(bmih->biHeight), bmih->biHeight < 0 ? "yes" : "no");
rdp_debug_clipboard_verbose_continue(b, " BITMAPINFOHEADER.biPlanes:%d\n", bmih->biPlanes);
rdp_debug_clipboard_verbose_continue(b, " BITMAPINFOHEADER.biBitCount:%d\n", bmih->biBitCount);
rdp_debug_clipboard_verbose_continue(b, " BITMAPINFOHEADER.biCompression:%d\n", bmih->biCompression);
rdp_debug_clipboard_verbose_continue(b, " BITMAPINFOHEADER.biSizeImage:%d\n", bmih->biSizeImage);
rdp_debug_clipboard_verbose_continue(b, " BITMAPINFOHEADER.biXPelsPerMeter:%d\n", bmih->biXPelsPerMeter);
rdp_debug_clipboard_verbose_continue(b, " BITMAPINFOHEADER.biYPelsPerMeter:%d\n", bmih->biYPelsPerMeter);
rdp_debug_clipboard_verbose_continue(b, " BITMAPINFOHEADER.biClrUsed:%d\n", bmih->biClrUsed);
rdp_debug_clipboard_verbose_continue(b, " BITMAPINFOHEADER.biClrImportant:%d\n", bmih->biClrImportant);
BITMAPINFO *bmi = (BITMAPINFO *)bmih;
if (bmih->biCompression == BI_BITFIELDS)
color_table_size = sizeof(RGBQUAD) * 3;
else
color_table_size = sizeof(RGBQUAD) * bmih->biClrUsed;
for (UINT32 i = 0; i < color_table_size / sizeof(RGBQUAD); i++) {
rdp_debug_clipboard_verbose_continue(b, " BITMAPINFO.bmiColors[%d]:%02x:%02x:%02x:%02x\n",
i,
(UINT32)bmi->bmiColors[i].rgbReserved,
(UINT32)bmi->bmiColors[i].rgbRed,
(UINT32)bmi->bmiColors[i].rgbGreen,
(UINT32)bmi->bmiColors[i].rgbBlue);
}
if (bmih->biBitCount == 32) {
DWORD *bits = (DWORD*)((char*)bmfh + bmfh->bfOffBits);
assert(bits == (DWORD*)(&bmi->bmiColors[color_table_size/sizeof(RGBQUAD)]));
//for (UINT32 i = 0; i < 4; i++) {
// rdp_debug_clipboard_verbose_continue(b, " %08x %08x %08x %08x %08x %08x %08x %08x\n",
// bits[0],bits[1],bits[2],bits[3],bits[4],bits[5],bits[6],bits[7]);
// bits += 8;
//}
} else if (bmih->biBitCount == 24) {
BYTE *bits = (BYTE*)bmfh + bmfh->bfOffBits;
assert(bits == (BYTE*)(&bmi->bmiColors[color_table_size/sizeof(RGBQUAD)]));
//for (UINT32 i = 0; i < 4; i++) {
// rdp_debug_clipboard_verbose_continue(b, " %02x%02x%02x %02x%02x%02x %02x%02x%02x %02x%02x%02x %02x%02x%02x %02x%02x%02x %02x%02x%02x %02x%02x%02x\n",
// bits[ 0],bits[ 1],bits[ 2], bits[ 3],bits[ 4],bits[ 5], bits[ 6],bits[ 7],bits[ 8], bits[ 9],bits[10],bits[11],
// bits[12],bits[13],bits[14], bits[15],bits[16],bits[17], bits[18],bits[19],bits[20], bits[21],bits[22],bits[23]);
// bits += 24;
//}
}
rdp_debug_clipboard_verbose_continue(b, " sizeof(BITMAPFILEHEADER):%d\n", (UINT32) sizeof(BITMAPFILEHEADER));
rdp_debug_clipboard_verbose_continue(b, " sizeof(BITMAPINFOHEADER):%d\n", (UINT32) sizeof(BITMAPINFOHEADER));
rdp_debug_clipboard_verbose_continue(b, " original_data_size:%d\n", (UINT32) original_data_size);
rdp_debug_clipboard_verbose_continue(b, " new_data_size:%d\n", (UINT32) source->data_contents.size);
rdp_debug_clipboard_verbose_continue(b, " data_processed:%d -> %d\n", was_data_processed, source->is_data_processed);
*/
return true;
error_return:
source->state = RDP_CLIPBOARD_SOURCE_FAILED;
weston_log("RDP %s FAILED (%p:%s): %s (%d bytes)\n",
__func__, source, clipboard_data_source_state_to_string(source),
is_send ? "send" : "receive", (uint32_t)source->data_contents.size);
wl_array_release(&data_contents);
return false;
}
static char *
clipboard_format_id_to_string(UINT32 formatId, bool is_server_format_id)
{
switch (formatId) {
case CF_RAW:
return "CF_RAW";
case CF_TEXT:
return "CF_TEXT";
case CF_BITMAP:
return "CF_BITMAP";
case CF_METAFILEPICT:
return "CF_METAFILEPICT";
case CF_SYLK:
return "CF_SYLK";
case CF_DIF:
return "CF_DIF";
case CF_TIFF:
return "CF_TIFF";
case CF_OEMTEXT:
return "CF_OEMTEXT";
case CF_DIB:
return "CF_DIB";
case CF_PALETTE:
return "CF_PALETTE";
case CF_PENDATA:
return "CF_PENDATA";
case CF_RIFF:
return "CF_RIFF";
case CF_WAVE:
return "CF_WAVE";
case CF_UNICODETEXT:
return "CF_UNICODETEXT";
case CF_ENHMETAFILE:
return "CF_ENHMETAFILE";
case CF_HDROP:
return "CF_HDROP";
case CF_LOCALE:
return "CF_LOCALE";
case CF_DIBV5:
return "CF_DIBV5";
case CF_OWNERDISPLAY:
return "CF_OWNERDISPLAY";
case CF_DSPTEXT:
return "CF_DSPTEXT";
case CF_DSPBITMAP:
return "CF_DSPBITMAP";
case CF_DSPMETAFILEPICT:
return "CF_DSPMETAFILEPICT";
case CF_DSPENHMETAFILE:
return "CF_DSPENHMETAFILE";
}
if (formatId >= CF_PRIVATEFIRST && formatId <= CF_PRIVATELAST)
return "CF_PRIVATE";
if (formatId >= CF_GDIOBJFIRST && formatId <= CF_GDIOBJLAST)
return "CF_GDIOBJ";
if (is_server_format_id) {
if (formatId == CF_PRIVATE_HTML)
return "CF_PRIVATE_HTML";
if (formatId == CF_PRIVATE_RTF)
return "CF_PRIVATE_RTF";
} else {
/* From MSDN, RegisterClipboardFormat API.
Registered clipboard formats are identified by values in the range 0xC000 through 0xFFFF. */
if (formatId >= 0xC000 && formatId <= 0xFFFF)
return "Client side Registered Clipboard Format";
}
return "Unknown format";
}
/* find supported index in supported format table by format id from client */
static int
clipboard_find_supported_format_by_format_id(UINT32 format_id)
{
unsigned int i;
for (i = 0; i < RDP_NUM_CLIPBOARD_FORMATS; i++) {
struct rdp_clipboard_supported_format *format = &clipboard_supported_formats[i];
if (format_id == format->format_id)
return i;
}
return -1;
}
/* find supported index in supported format table by format id and name from client */
static int
clipboard_find_supported_format_by_format_id_and_name(UINT32 format_id, const char *format_name)
{
unsigned int i;
for (i = 0; i < RDP_NUM_CLIPBOARD_FORMATS; i++) {
struct rdp_clipboard_supported_format *format = &clipboard_supported_formats[i];
/* when our supported format table has format name, only format name must match,
format id provided from client is ignored (but it may be saved by caller for future use.
When our supported format table doesn't have format name, only format id must match,
format name (if provided from client) is ignored */
if ((format->format_name == NULL && format_id == format->format_id) ||
(format->format_name && format_name && strcmp(format_name, format->format_name) == 0))
return i;
}
return -1;
}
/* find supported index in supported format table by mime */
static int
clipboard_find_supported_format_by_mime_type(const char *mime_type)
{
unsigned int i;
for (i = 0; i < RDP_NUM_CLIPBOARD_FORMATS; i++) {
struct rdp_clipboard_supported_format *format = &clipboard_supported_formats[i];
if (strcmp(mime_type, format->mime_type) == 0)
return i;
}
return -1;
}
static bool
clipboard_process_source(struct rdp_clipboard_data_source *source, bool is_send)
{
if (source->is_data_processed) {
assert(source->processed_data_is_send == is_send);
return true;
}
source->processed_data_start = NULL;
source->processed_data_size = 0;
if (clipboard_supported_formats[source->format_index].pfn)
return clipboard_supported_formats[source->format_index].pfn(source, is_send);
/* No processor, so just set up pointer and length for raw data */
source->is_data_processed = true;
source->processed_data_start = source->data_contents.data;
source->processed_data_size = source->data_contents.size;
source->processed_data_is_send = is_send;
return true;
}
static void
clipboard_data_source_unref(struct rdp_clipboard_data_source *source)
{
freerdp_peer *client = (freerdp_peer *)source->context;
RdpPeerContext *ctx = (RdpPeerContext *)client->context;
struct rdp_backend *b = ctx->rdpBackend;
char **p;
assert_compositor_thread(b);
assert(source->refcount);
source->refcount--;
rdp_debug_clipboard(b, "RDP %s (%p:%s): refcount:%d\n",
__func__, source,
clipboard_data_source_state_to_string(source),
source->refcount);
if (source->refcount > 0)
return;
if (source->transfer_event_source)
wl_event_source_remove(source->transfer_event_source);
if (source->data_source_fd != -1)
close(source->data_source_fd);
if (!wl_list_empty(&source->base.destroy_signal.listener_list))
wl_signal_emit(&source->base.destroy_signal,
&source->base);
wl_array_release(&source->data_contents);
wl_array_for_each(p, &source->base.mime_types)
free(*p);
wl_array_release(&source->base.mime_types);
free(source);
}
/******************************************\
* FreeRDP format data response functions *
\******************************************/
/* Inform client data request is succeeded with data */
static void
clipboard_client_send_format_data_response(RdpPeerContext *ctx, struct rdp_clipboard_data_source *source)
{
struct rdp_backend *b = ctx->rdpBackend;
CLIPRDR_FORMAT_DATA_RESPONSE formatDataResponse = {};
assert(source->is_data_processed);
rdp_debug_clipboard(b, "Client: %s (%p:%s) format_index:%d %s (%d bytes)\n",
__func__, source,
clipboard_data_source_state_to_string(source),
source->format_index,
clipboard_supported_formats[source->format_index].mime_type,
source->processed_data_size);
formatDataResponse.msgType = CB_FORMAT_DATA_RESPONSE;
formatDataResponse.msgFlags = CB_RESPONSE_OK;
formatDataResponse.dataLen = source->processed_data_size;
formatDataResponse.requestedFormatData = source->processed_data_start;
ctx->clipboard_server_context->ServerFormatDataResponse(ctx->clipboard_server_context, &formatDataResponse);
/* if here failed to send response, what can we do ? */
}
/* Inform client data request has failed */
static void
clipboard_client_send_format_data_response_fail(RdpPeerContext *ctx, struct rdp_clipboard_data_source *source)
{
struct rdp_backend *b = ctx->rdpBackend;
CLIPRDR_FORMAT_DATA_RESPONSE formatDataResponse = {};
rdp_debug_clipboard(b, "Client: %s (%p:%s)\n",
__func__, source,
clipboard_data_source_state_to_string(source));
if (source) {
source->state = RDP_CLIPBOARD_SOURCE_FAILED;
source->data_response_fail_count++;
}
formatDataResponse.msgType = CB_FORMAT_DATA_RESPONSE;
formatDataResponse.msgFlags = CB_RESPONSE_FAIL;
formatDataResponse.dataLen = 0;
formatDataResponse.requestedFormatData = NULL;
ctx->clipboard_server_context->ServerFormatDataResponse(ctx->clipboard_server_context, &formatDataResponse);
/* if here failed to send response, what can we do ? */
}
/***************************************\
* Compositor file descritor callbacks *
\***************************************/
/* Send server clipboard data to client when server side application sent them via pipe. */
static int
clipboard_data_source_read(int fd, uint32_t mask, void *arg)
{
struct rdp_clipboard_data_source *source = (struct rdp_clipboard_data_source *)arg;
freerdp_peer *client = (freerdp_peer *)source->context;
RdpPeerContext *ctx = (RdpPeerContext *)client->context;
struct rdp_backend *b = ctx->rdpBackend;
int len;
bool failed = true;
rdp_debug_clipboard_verbose(b, "RDP %s (%p:%s) fd:%d\n",
__func__, source,
clipboard_data_source_state_to_string(source), fd);
assert_compositor_thread(b);
assert(source->data_source_fd == fd);
assert(source->refcount == 1);
/* event source is not removed here, but it will be removed when read is completed,
until it's completed this function will be called whenever next chunk of data is
available for read in pipe. */
assert(source->transfer_event_source);
source->state = RDP_CLIPBOARD_SOURCE_TRANSFERING;
len = rdp_wl_array_read_fd(&source->data_contents, fd);
if (len < 0) {
source->state = RDP_CLIPBOARD_SOURCE_FAILED;
weston_log("RDP %s (%p:%s) read failed (%s)\n",
__func__, source,
clipboard_data_source_state_to_string(source),
strerror(errno));
goto error_exit;
}
if (len > 0) {
rdp_debug_clipboard_verbose(b, "RDP %s (%p:%s) read (%zu bytes)\n",
__func__, source,
clipboard_data_source_state_to_string(source),
source->data_contents.size);
/* continue to read next batch */
return 0;
}
/* len == 0, all data from source is read, so completed. */
source->state = RDP_CLIPBOARD_SOURCE_TRANSFERRED;
rdp_debug_clipboard(b, "RDP %s (%p:%s): read completed (%ld bytes)\n",
__func__, source,
clipboard_data_source_state_to_string(source),
source->data_contents.size);
if (!source->data_contents.size)
goto error_exit;
/* process data before sending to client */
if (!clipboard_process_source(source, true))
goto error_exit;
clipboard_client_send_format_data_response(ctx, source);
failed = false;
error_exit:
if (failed)
clipboard_client_send_format_data_response_fail(ctx, source);
/* make sure this is the last reference, so event source is removed at unref */
assert(source->refcount == 1);
clipboard_data_source_unref(source);
return 0;
}
/* client's reply with error for data request, clean up */
static int
clipboard_data_source_fail(int fd, uint32_t mask, void *arg)
{
struct rdp_clipboard_data_source *source = (struct rdp_clipboard_data_source *)arg;
freerdp_peer *client = (freerdp_peer *)source->context;
RdpPeerContext *ctx = (RdpPeerContext *)client->context;
struct rdp_backend *b = ctx->rdpBackend;
rdp_debug_clipboard_verbose(b, "RDP %s (%p:%s) fd:%d\n", __func__,
source, clipboard_data_source_state_to_string(source), fd);
assert_compositor_thread(b);
assert(source->data_source_fd == fd);
/* this data source must be tracked as inflight */
assert(source == ctx->clipboard_inflight_client_data_source);
wl_event_source_remove(source->transfer_event_source);
source->transfer_event_source = NULL;
/* if data was received, but failed for another reason then keep data
* and format index for future request, otherwise data is purged at
* last reference release. */
if (!source->data_contents.size) {
/* data has been never received, thus must be empty. */
assert(source->data_contents.size == 0);
assert(source->data_contents.alloc == 0);
assert(source->data_contents.data == NULL);
/* clear previous requested format so it can be requested later again. */
source->format_index = -1;
}
/* data has never been sent to write(), thus must be no inflight write. */
assert(source->inflight_write_count == 0);
assert(source->inflight_data_to_write == NULL);
assert(source->inflight_data_size == 0);
/* data never has been sent to write(), so must not be processed. */
assert(source->is_data_processed == FALSE);
/* close fd to server clipboard stop pulling data. */
close(source->data_source_fd);
source->data_source_fd = -1;
/* clear inflight data source from client to server. */
ctx->clipboard_inflight_client_data_source = NULL;
clipboard_data_source_unref(source);
return 0;
}
/* Send client's clipboard data to the requesting application at server side */
static int
clipboard_data_source_write(int fd, uint32_t mask, void *arg)
{
struct rdp_clipboard_data_source *source = (struct rdp_clipboard_data_source *)arg;
freerdp_peer *client = (freerdp_peer *)source->context;
RdpPeerContext *ctx = (RdpPeerContext *)client->context;
struct rdp_backend *b = ctx->rdpBackend;
void *data_to_write;
size_t data_size;
ssize_t size;
rdp_debug_clipboard_verbose(b, "RDP %s (%p:%s) fd:%d\n", __func__,
source,
clipboard_data_source_state_to_string(source),
fd);
assert_compositor_thread(b);
assert(source->data_source_fd == fd);
/* this data source must be tracked as inflight */
assert(source == ctx->clipboard_inflight_client_data_source);
if (source->is_canceled) {
/* if source is being canceled, this must be the last reference */
assert(source->refcount == 1);
source->state = RDP_CLIPBOARD_SOURCE_CANCELED;
rdp_debug_clipboard_verbose(b, "RDP %s (%p:%s) canceled\n",
__func__, source,
clipboard_data_source_state_to_string(source));
goto fail;
}
if (!source->data_contents.data || !source->data_contents.size) {
assert(source->refcount > 1);
weston_log("RDP %s (%p:%s) no data received from client\n",
__func__, source,
clipboard_data_source_state_to_string(source));
goto fail;
}
assert(source->refcount > 1);
if (source->inflight_data_to_write) {
assert(source->inflight_data_size);
rdp_debug_clipboard_verbose(b, "RDP %s (%p:%s) transfer in chunck, count:%d\n",
__func__, source,
clipboard_data_source_state_to_string(source),
source->inflight_write_count);
data_to_write = source->inflight_data_to_write;
data_size = source->inflight_data_size;
} else {
fcntl(source->data_source_fd, F_SETFL, O_WRONLY | O_NONBLOCK);