FreeRDP
Loading...
Searching...
No Matches
server/cliprdr_main.c
1
22#include <freerdp/config.h>
23
24#include <winpr/crt.h>
25#include <winpr/assert.h>
26#include <winpr/print.h>
27#include <winpr/stream.h>
28
29#include <freerdp/freerdp.h>
30#include <freerdp/channels/log.h>
31#include "cliprdr_main.h"
32#include "../cliprdr_common.h"
33
79static UINT cliprdr_server_packet_send(CliprdrServerPrivate* cliprdr, wStream* s)
80{
81 UINT rc = 0;
82 size_t pos = 0;
83 BOOL status = 0;
84 UINT32 dataLen = 0;
85 ULONG written = 0;
86
87 WINPR_ASSERT(cliprdr);
88
89 pos = Stream_GetPosition(s);
90 if ((pos < 8) || (pos > UINT32_MAX))
91 {
92 rc = ERROR_NO_DATA;
93 goto fail;
94 }
95
96 dataLen = (UINT32)(pos - 8);
97 Stream_SetPosition(s, 4);
98 Stream_Write_UINT32(s, dataLen);
99
100 WINPR_ASSERT(pos <= UINT32_MAX);
101 status = WTSVirtualChannelWrite(cliprdr->ChannelHandle, Stream_BufferAs(s, char), (UINT32)pos,
102 &written);
103 rc = status ? CHANNEL_RC_OK : ERROR_INTERNAL_ERROR;
104fail:
105 Stream_Free(s, TRUE);
106 return rc;
107}
108
114static UINT cliprdr_server_capabilities(CliprdrServerContext* context,
115 const CLIPRDR_CAPABILITIES* capabilities)
116{
117 size_t offset = 0;
118
119 WINPR_ASSERT(context);
120 WINPR_ASSERT(capabilities);
121
122 CliprdrServerPrivate* cliprdr = (CliprdrServerPrivate*)context->handle;
123
124 if (capabilities->common.msgType != CB_CLIP_CAPS)
125 WLog_WARN(TAG, "called with invalid type %08" PRIx32, capabilities->common.msgType);
126
127 if (capabilities->cCapabilitiesSets > UINT16_MAX)
128 {
129 WLog_ERR(TAG, "Invalid number of capability sets in clipboard caps");
130 return ERROR_INVALID_PARAMETER;
131 }
132
133 wStream* s = cliprdr_packet_new(CB_CLIP_CAPS, 0, 4 + CB_CAPSTYPE_GENERAL_LEN);
134
135 if (!s)
136 {
137 WLog_ERR(TAG, "cliprdr_packet_new failed!");
138 return ERROR_INTERNAL_ERROR;
139 }
140
141 Stream_Write_UINT16(s,
142 (UINT16)capabilities->cCapabilitiesSets); /* cCapabilitiesSets (2 bytes) */
143 Stream_Write_UINT16(s, 0); /* pad1 (2 bytes) */
144 for (UINT32 x = 0; x < capabilities->cCapabilitiesSets; x++)
145 {
146 const CLIPRDR_CAPABILITY_SET* cap =
147 (const CLIPRDR_CAPABILITY_SET*)(((const BYTE*)capabilities->capabilitySets) + offset);
148 offset += cap->capabilitySetLength;
149
150 switch (cap->capabilitySetType)
151 {
152 case CB_CAPSTYPE_GENERAL:
153 {
154 const CLIPRDR_GENERAL_CAPABILITY_SET* generalCapabilitySet =
156 Stream_Write_UINT16(
157 s, generalCapabilitySet->capabilitySetType); /* capabilitySetType (2 bytes) */
158 Stream_Write_UINT16(
159 s, generalCapabilitySet->capabilitySetLength); /* lengthCapability (2 bytes) */
160 Stream_Write_UINT32(s, generalCapabilitySet->version); /* version (4 bytes) */
161 Stream_Write_UINT32(
162 s, generalCapabilitySet->generalFlags); /* generalFlags (4 bytes) */
163 }
164 break;
165
166 default:
167 WLog_WARN(TAG, "Unknown capability set type %08" PRIx16, cap->capabilitySetType);
168 if (!Stream_SafeSeek(s, cap->capabilitySetLength))
169 {
170 WLog_ERR(TAG, "short stream");
171 Stream_Free(s, TRUE);
172 return ERROR_NO_DATA;
173 }
174 break;
175 }
176 }
177 WLog_DBG(TAG, "ServerCapabilities");
178 return cliprdr_server_packet_send(cliprdr, s);
179}
180
186static UINT cliprdr_server_monitor_ready(CliprdrServerContext* context,
187 const CLIPRDR_MONITOR_READY* monitorReady)
188{
189 wStream* s = NULL;
190 CliprdrServerPrivate* cliprdr = NULL;
191
192 WINPR_ASSERT(context);
193 WINPR_ASSERT(monitorReady);
194
195 cliprdr = (CliprdrServerPrivate*)context->handle;
196
197 if (monitorReady->common.msgType != CB_MONITOR_READY)
198 WLog_WARN(TAG, "called with invalid type %08" PRIx32, monitorReady->common.msgType);
199
200 s = cliprdr_packet_new(CB_MONITOR_READY, monitorReady->common.msgFlags,
201 monitorReady->common.dataLen);
202
203 if (!s)
204 {
205 WLog_ERR(TAG, "cliprdr_packet_new failed!");
206 return ERROR_INTERNAL_ERROR;
207 }
208
209 WLog_DBG(TAG, "ServerMonitorReady");
210 return cliprdr_server_packet_send(cliprdr, s);
211}
212
218static UINT cliprdr_server_format_list(CliprdrServerContext* context,
219 const CLIPRDR_FORMAT_LIST* formatList)
220{
221 wStream* s = NULL;
222 CliprdrServerPrivate* cliprdr = NULL;
223
224 WINPR_ASSERT(context);
225 WINPR_ASSERT(formatList);
226
227 cliprdr = (CliprdrServerPrivate*)context->handle;
228
229 s = cliprdr_packet_format_list_new(formatList, context->useLongFormatNames, FALSE);
230 if (!s)
231 {
232 WLog_ERR(TAG, "cliprdr_packet_format_list_new failed!");
233 return ERROR_INTERNAL_ERROR;
234 }
235
236 WLog_DBG(TAG, "ServerFormatList: numFormats: %" PRIu32 "", formatList->numFormats);
237 return cliprdr_server_packet_send(cliprdr, s);
238}
239
245static UINT
246cliprdr_server_format_list_response(CliprdrServerContext* context,
247 const CLIPRDR_FORMAT_LIST_RESPONSE* formatListResponse)
248{
249 wStream* s = NULL;
250 CliprdrServerPrivate* cliprdr = NULL;
251
252 WINPR_ASSERT(context);
253 WINPR_ASSERT(formatListResponse);
254
255 cliprdr = (CliprdrServerPrivate*)context->handle;
256 if (formatListResponse->common.msgType != CB_FORMAT_LIST_RESPONSE)
257 WLog_WARN(TAG, "called with invalid type %08" PRIx32, formatListResponse->common.msgType);
258
259 s = cliprdr_packet_new(CB_FORMAT_LIST_RESPONSE, formatListResponse->common.msgFlags,
260 formatListResponse->common.dataLen);
261
262 if (!s)
263 {
264 WLog_ERR(TAG, "cliprdr_packet_new failed!");
265 return ERROR_INTERNAL_ERROR;
266 }
267
268 WLog_DBG(TAG, "ServerFormatListResponse");
269 return cliprdr_server_packet_send(cliprdr, s);
270}
271
277static UINT cliprdr_server_lock_clipboard_data(CliprdrServerContext* context,
278 const CLIPRDR_LOCK_CLIPBOARD_DATA* lockClipboardData)
279{
280 wStream* s = NULL;
281 CliprdrServerPrivate* cliprdr = NULL;
282
283 WINPR_ASSERT(context);
284 WINPR_ASSERT(lockClipboardData);
285
286 cliprdr = (CliprdrServerPrivate*)context->handle;
287 if (lockClipboardData->common.msgType != CB_LOCK_CLIPDATA)
288 WLog_WARN(TAG, "called with invalid type %08" PRIx32, lockClipboardData->common.msgType);
289
290 s = cliprdr_packet_lock_clipdata_new(lockClipboardData);
291 if (!s)
292 {
293 WLog_ERR(TAG, "cliprdr_packet_lock_clipdata_new failed!");
294 return ERROR_INTERNAL_ERROR;
295 }
296
297 WLog_DBG(TAG, "ServerLockClipboardData: clipDataId: 0x%08" PRIX32 "",
298 lockClipboardData->clipDataId);
299 return cliprdr_server_packet_send(cliprdr, s);
300}
301
307static UINT
308cliprdr_server_unlock_clipboard_data(CliprdrServerContext* context,
309 const CLIPRDR_UNLOCK_CLIPBOARD_DATA* unlockClipboardData)
310{
311 wStream* s = NULL;
312 CliprdrServerPrivate* cliprdr = NULL;
313
314 WINPR_ASSERT(context);
315 WINPR_ASSERT(unlockClipboardData);
316
317 cliprdr = (CliprdrServerPrivate*)context->handle;
318 if (unlockClipboardData->common.msgType != CB_UNLOCK_CLIPDATA)
319 WLog_WARN(TAG, "called with invalid type %08" PRIx32, unlockClipboardData->common.msgType);
320
321 s = cliprdr_packet_unlock_clipdata_new(unlockClipboardData);
322
323 if (!s)
324 {
325 WLog_ERR(TAG, "cliprdr_packet_unlock_clipdata_new failed!");
326 return ERROR_INTERNAL_ERROR;
327 }
328
329 WLog_DBG(TAG, "ServerUnlockClipboardData: clipDataId: 0x%08" PRIX32 "",
330 unlockClipboardData->clipDataId);
331 return cliprdr_server_packet_send(cliprdr, s);
332}
333
339static UINT cliprdr_server_format_data_request(CliprdrServerContext* context,
340 const CLIPRDR_FORMAT_DATA_REQUEST* formatDataRequest)
341{
342 wStream* s = NULL;
343 CliprdrServerPrivate* cliprdr = NULL;
344
345 WINPR_ASSERT(context);
346 WINPR_ASSERT(formatDataRequest);
347
348 cliprdr = (CliprdrServerPrivate*)context->handle;
349 if (formatDataRequest->common.msgType != CB_FORMAT_DATA_REQUEST)
350 WLog_WARN(TAG, "called with invalid type %08" PRIx32, formatDataRequest->common.msgType);
351
352 s = cliprdr_packet_new(CB_FORMAT_DATA_REQUEST, formatDataRequest->common.msgFlags,
353 formatDataRequest->common.dataLen);
354
355 if (!s)
356 {
357 WLog_ERR(TAG, "cliprdr_packet_new failed!");
358 return ERROR_INTERNAL_ERROR;
359 }
360
361 Stream_Write_UINT32(s, formatDataRequest->requestedFormatId); /* requestedFormatId (4 bytes) */
362 WLog_DBG(TAG, "ClientFormatDataRequest");
363 return cliprdr_server_packet_send(cliprdr, s);
364}
365
371static UINT
372cliprdr_server_format_data_response(CliprdrServerContext* context,
373 const CLIPRDR_FORMAT_DATA_RESPONSE* formatDataResponse)
374{
375 wStream* s = NULL;
376 CliprdrServerPrivate* cliprdr = NULL;
377
378 WINPR_ASSERT(context);
379 WINPR_ASSERT(formatDataResponse);
380
381 cliprdr = (CliprdrServerPrivate*)context->handle;
382
383 if (formatDataResponse->common.msgType != CB_FORMAT_DATA_RESPONSE)
384 WLog_WARN(TAG, "called with invalid type %08" PRIx32, formatDataResponse->common.msgType);
385
386 s = cliprdr_packet_new(CB_FORMAT_DATA_RESPONSE, formatDataResponse->common.msgFlags,
387 formatDataResponse->common.dataLen);
388
389 if (!s)
390 {
391 WLog_ERR(TAG, "cliprdr_packet_new failed!");
392 return ERROR_INTERNAL_ERROR;
393 }
394
395 Stream_Write(s, formatDataResponse->requestedFormatData, formatDataResponse->common.dataLen);
396 WLog_DBG(TAG, "ServerFormatDataResponse");
397 return cliprdr_server_packet_send(cliprdr, s);
398}
399
405static UINT
406cliprdr_server_file_contents_request(CliprdrServerContext* context,
407 const CLIPRDR_FILE_CONTENTS_REQUEST* fileContentsRequest)
408{
409 wStream* s = NULL;
410 CliprdrServerPrivate* cliprdr = NULL;
411
412 WINPR_ASSERT(context);
413 WINPR_ASSERT(fileContentsRequest);
414
415 cliprdr = (CliprdrServerPrivate*)context->handle;
416
417 if (fileContentsRequest->common.msgType != CB_FILECONTENTS_REQUEST)
418 WLog_WARN(TAG, "called with invalid type %08" PRIx32, fileContentsRequest->common.msgType);
419
420 s = cliprdr_packet_file_contents_request_new(fileContentsRequest);
421 if (!s)
422 {
423 WLog_ERR(TAG, "cliprdr_packet_file_contents_request_new failed!");
424 return ERROR_INTERNAL_ERROR;
425 }
426
427 WLog_DBG(TAG, "ServerFileContentsRequest: streamId: 0x%08" PRIX32 "",
428 fileContentsRequest->streamId);
429 return cliprdr_server_packet_send(cliprdr, s);
430}
431
437static UINT
438cliprdr_server_file_contents_response(CliprdrServerContext* context,
439 const CLIPRDR_FILE_CONTENTS_RESPONSE* fileContentsResponse)
440{
441 wStream* s = NULL;
442 CliprdrServerPrivate* cliprdr = NULL;
443
444 WINPR_ASSERT(context);
445 WINPR_ASSERT(fileContentsResponse);
446
447 cliprdr = (CliprdrServerPrivate*)context->handle;
448
449 if (fileContentsResponse->common.msgType != CB_FILECONTENTS_RESPONSE)
450 WLog_WARN(TAG, "called with invalid type %08" PRIx32, fileContentsResponse->common.msgType);
451
452 s = cliprdr_packet_file_contents_response_new(fileContentsResponse);
453 if (!s)
454 {
455 WLog_ERR(TAG, "cliprdr_packet_file_contents_response_new failed!");
456 return ERROR_INTERNAL_ERROR;
457 }
458
459 WLog_DBG(TAG, "ServerFileContentsResponse: streamId: 0x%08" PRIX32 "",
460 fileContentsResponse->streamId);
461 return cliprdr_server_packet_send(cliprdr, s);
462}
463
469static UINT cliprdr_server_receive_general_capability(CliprdrServerContext* context, wStream* s,
471{
472 WINPR_ASSERT(context);
473 WINPR_ASSERT(cap_set);
474
475 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
476 return ERROR_INVALID_DATA;
477
478 Stream_Read_UINT32(s, cap_set->version); /* version (4 bytes) */
479 Stream_Read_UINT32(s, cap_set->generalFlags); /* generalFlags (4 bytes) */
480
481 if (context->useLongFormatNames)
482 context->useLongFormatNames =
483 (cap_set->generalFlags & CB_USE_LONG_FORMAT_NAMES) ? TRUE : FALSE;
484
485 if (context->streamFileClipEnabled)
486 context->streamFileClipEnabled =
487 (cap_set->generalFlags & CB_STREAM_FILECLIP_ENABLED) ? TRUE : FALSE;
488
489 if (context->fileClipNoFilePaths)
490 context->fileClipNoFilePaths =
491 (cap_set->generalFlags & CB_FILECLIP_NO_FILE_PATHS) ? TRUE : FALSE;
492
493 if (context->canLockClipData)
494 context->canLockClipData = (cap_set->generalFlags & CB_CAN_LOCK_CLIPDATA) ? TRUE : FALSE;
495
496 if (context->hasHugeFileSupport)
497 context->hasHugeFileSupport =
498 (cap_set->generalFlags & CB_HUGE_FILE_SUPPORT_ENABLED) ? TRUE : FALSE;
499
500 return CHANNEL_RC_OK;
501}
502
508static UINT cliprdr_server_receive_capabilities(CliprdrServerContext* context, wStream* s,
509 const CLIPRDR_HEADER* header)
510{
511 UINT16 capabilitySetType = 0;
512 UINT16 capabilitySetLength = 0;
513 UINT error = ERROR_INVALID_DATA;
514 size_t cap_sets_size = 0;
515 CLIPRDR_CAPABILITIES capabilities = { 0 };
516 CLIPRDR_CAPABILITY_SET* capSet = NULL;
517
518 WINPR_ASSERT(context);
519 WINPR_UNUSED(header);
520
521 WLog_DBG(TAG, "CliprdrClientCapabilities");
522 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
523 return ERROR_INVALID_DATA;
524
525 Stream_Read_UINT16(s, capabilities.cCapabilitiesSets); /* cCapabilitiesSets (2 bytes) */
526 Stream_Seek_UINT16(s); /* pad1 (2 bytes) */
527
528 for (size_t index = 0; index < capabilities.cCapabilitiesSets; index++)
529 {
530 void* tmp = NULL;
531 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
532 goto out;
533 Stream_Read_UINT16(s, capabilitySetType); /* capabilitySetType (2 bytes) */
534 Stream_Read_UINT16(s, capabilitySetLength); /* capabilitySetLength (2 bytes) */
535
536 cap_sets_size += capabilitySetLength;
537
538 if (cap_sets_size > 0)
539 tmp = realloc(capabilities.capabilitySets, cap_sets_size);
540 if (tmp == NULL)
541 {
542 WLog_ERR(TAG, "capabilities.capabilitySets realloc failed!");
543 free(capabilities.capabilitySets);
544 return CHANNEL_RC_NO_MEMORY;
545 }
546
547 capabilities.capabilitySets = (CLIPRDR_CAPABILITY_SET*)tmp;
548
549 capSet = &(capabilities.capabilitySets[index]);
550
551 capSet->capabilitySetType = capabilitySetType;
552 capSet->capabilitySetLength = capabilitySetLength;
553
554 switch (capSet->capabilitySetType)
555 {
556 case CB_CAPSTYPE_GENERAL:
557 error = cliprdr_server_receive_general_capability(
558 context, s, (CLIPRDR_GENERAL_CAPABILITY_SET*)capSet);
559 if (error)
560 {
561 WLog_ERR(TAG,
562 "cliprdr_server_receive_general_capability failed with error %" PRIu32
563 "",
564 error);
565 goto out;
566 }
567 break;
568
569 default:
570 WLog_ERR(TAG, "unknown cliprdr capability set: %" PRIu16 "",
571 capSet->capabilitySetType);
572 goto out;
573 }
574 }
575
576 error = CHANNEL_RC_OK;
577 IFCALLRET(context->ClientCapabilities, error, context, &capabilities);
578out:
579 free(capabilities.capabilitySets);
580 return error;
581}
582
588static UINT cliprdr_server_receive_temporary_directory(CliprdrServerContext* context, wStream* s,
589 const CLIPRDR_HEADER* header)
590{
591 size_t length = 0;
592 CLIPRDR_TEMP_DIRECTORY tempDirectory = { 0 };
593 CliprdrServerPrivate* cliprdr = NULL;
594 UINT error = CHANNEL_RC_OK;
595
596 WINPR_ASSERT(context);
597 WINPR_UNUSED(header);
598
599 cliprdr = (CliprdrServerPrivate*)context->handle;
600 WINPR_ASSERT(cliprdr);
601
602 if (!Stream_CheckAndLogRequiredLength(TAG, s,
603 ARRAYSIZE(cliprdr->temporaryDirectory) * sizeof(WCHAR)))
604 return CHANNEL_RC_NO_MEMORY;
605
606 const WCHAR* wszTempDir = Stream_ConstPointer(s);
607
608 if (wszTempDir[ARRAYSIZE(cliprdr->temporaryDirectory) - 1] != 0)
609 {
610 WLog_ERR(TAG, "wszTempDir[259] was not 0");
611 return ERROR_INVALID_DATA;
612 }
613
614 if (ConvertWCharNToUtf8(wszTempDir, ARRAYSIZE(cliprdr->temporaryDirectory),
615 cliprdr->temporaryDirectory,
616 ARRAYSIZE(cliprdr->temporaryDirectory)) < 0)
617 {
618 WLog_ERR(TAG, "failed to convert temporary directory name");
619 return ERROR_INVALID_DATA;
620 }
621
622 length = strnlen(cliprdr->temporaryDirectory, ARRAYSIZE(cliprdr->temporaryDirectory));
623
624 if (length >= ARRAYSIZE(cliprdr->temporaryDirectory))
625 length = ARRAYSIZE(cliprdr->temporaryDirectory) - 1;
626
627 CopyMemory(tempDirectory.szTempDir, cliprdr->temporaryDirectory, length);
628 tempDirectory.szTempDir[length] = '\0';
629 WLog_DBG(TAG, "CliprdrTemporaryDirectory: %s", cliprdr->temporaryDirectory);
630 IFCALLRET(context->TempDirectory, error, context, &tempDirectory);
631
632 if (error)
633 WLog_ERR(TAG, "TempDirectory failed with error %" PRIu32 "!", error);
634
635 return error;
636}
637
643static UINT cliprdr_server_receive_format_list(CliprdrServerContext* context, wStream* s,
644 const CLIPRDR_HEADER* header)
645{
646 CLIPRDR_FORMAT_LIST formatList = { 0 };
647 UINT error = CHANNEL_RC_OK;
648
649 WINPR_ASSERT(context);
650 WINPR_ASSERT(header);
651
652 formatList.common.msgType = CB_FORMAT_LIST;
653 formatList.common.msgFlags = header->msgFlags;
654 formatList.common.dataLen = header->dataLen;
655
656 wLog* log = WLog_Get(TAG);
657 if ((error = cliprdr_read_format_list(log, s, &formatList, context->useLongFormatNames)))
658 goto out;
659
660 WLog_Print(log, WLOG_DEBUG, "ClientFormatList: numFormats: %" PRIu32 "", formatList.numFormats);
661 IFCALLRET(context->ClientFormatList, error, context, &formatList);
662
663 if (error)
664 WLog_Print(log, WLOG_ERROR, "ClientFormatList failed with error %" PRIu32 "!", error);
665
666out:
667 cliprdr_free_format_list(&formatList);
668 return error;
669}
670
676static UINT cliprdr_server_receive_format_list_response(CliprdrServerContext* context, wStream* s,
677 const CLIPRDR_HEADER* header)
678{
679 CLIPRDR_FORMAT_LIST_RESPONSE formatListResponse = { 0 };
680 UINT error = CHANNEL_RC_OK;
681
682 WINPR_ASSERT(context);
683 WINPR_ASSERT(header);
684
685 WINPR_UNUSED(s);
686 WLog_DBG(TAG, "CliprdrClientFormatListResponse");
687 formatListResponse.common.msgType = CB_FORMAT_LIST_RESPONSE;
688 formatListResponse.common.msgFlags = header->msgFlags;
689 formatListResponse.common.dataLen = header->dataLen;
690 IFCALLRET(context->ClientFormatListResponse, error, context, &formatListResponse);
691
692 if (error)
693 WLog_ERR(TAG, "ClientFormatListResponse failed with error %" PRIu32 "!", error);
694
695 return error;
696}
697
703static UINT cliprdr_server_receive_lock_clipdata(CliprdrServerContext* context, wStream* s,
704 const CLIPRDR_HEADER* header)
705{
706 CLIPRDR_LOCK_CLIPBOARD_DATA lockClipboardData = { 0 };
707 UINT error = CHANNEL_RC_OK;
708
709 WINPR_ASSERT(context);
710 WINPR_ASSERT(header);
711
712 WLog_DBG(TAG, "CliprdrClientLockClipData");
713
714 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
715 return ERROR_INVALID_DATA;
716
717 lockClipboardData.common.msgType = CB_LOCK_CLIPDATA;
718 lockClipboardData.common.msgFlags = header->msgFlags;
719 lockClipboardData.common.dataLen = header->dataLen;
720 Stream_Read_UINT32(s, lockClipboardData.clipDataId); /* clipDataId (4 bytes) */
721 IFCALLRET(context->ClientLockClipboardData, error, context, &lockClipboardData);
722
723 if (error)
724 WLog_ERR(TAG, "ClientLockClipboardData failed with error %" PRIu32 "!", error);
725
726 return error;
727}
728
734static UINT cliprdr_server_receive_unlock_clipdata(CliprdrServerContext* context, wStream* s,
735 const CLIPRDR_HEADER* header)
736{
737 CLIPRDR_UNLOCK_CLIPBOARD_DATA unlockClipboardData = { 0 };
738 UINT error = CHANNEL_RC_OK;
739
740 WINPR_ASSERT(context);
741 WINPR_ASSERT(header);
742
743 WLog_DBG(TAG, "CliprdrClientUnlockClipData");
744
745 unlockClipboardData.common.msgType = CB_UNLOCK_CLIPDATA;
746 unlockClipboardData.common.msgFlags = header->msgFlags;
747 unlockClipboardData.common.dataLen = header->dataLen;
748
749 if ((error = cliprdr_read_unlock_clipdata(s, &unlockClipboardData)))
750 return error;
751
752 IFCALLRET(context->ClientUnlockClipboardData, error, context, &unlockClipboardData);
753
754 if (error)
755 WLog_ERR(TAG, "ClientUnlockClipboardData failed with error %" PRIu32 "!", error);
756
757 return error;
758}
759
765static UINT cliprdr_server_receive_format_data_request(CliprdrServerContext* context, wStream* s,
766 const CLIPRDR_HEADER* header)
767{
768 CLIPRDR_FORMAT_DATA_REQUEST formatDataRequest = { 0 };
769 UINT error = CHANNEL_RC_OK;
770
771 WINPR_ASSERT(context);
772 WINPR_ASSERT(header);
773
774 WLog_DBG(TAG, "CliprdrClientFormatDataRequest");
775 formatDataRequest.common.msgType = CB_FORMAT_DATA_REQUEST;
776 formatDataRequest.common.msgFlags = header->msgFlags;
777 formatDataRequest.common.dataLen = header->dataLen;
778
779 if ((error = cliprdr_read_format_data_request(s, &formatDataRequest)))
780 return error;
781
782 context->lastRequestedFormatId = formatDataRequest.requestedFormatId;
783 IFCALLRET(context->ClientFormatDataRequest, error, context, &formatDataRequest);
784
785 if (error)
786 WLog_ERR(TAG, "ClientFormatDataRequest failed with error %" PRIu32 "!", error);
787
788 return error;
789}
790
796static UINT cliprdr_server_receive_format_data_response(CliprdrServerContext* context, wStream* s,
797 const CLIPRDR_HEADER* header)
798{
799 CLIPRDR_FORMAT_DATA_RESPONSE formatDataResponse = { 0 };
800 UINT error = CHANNEL_RC_OK;
801
802 WINPR_ASSERT(context);
803 WINPR_ASSERT(header);
804
805 WLog_DBG(TAG, "CliprdrClientFormatDataResponse");
806 formatDataResponse.common.msgType = CB_FORMAT_DATA_RESPONSE;
807 formatDataResponse.common.msgFlags = header->msgFlags;
808 formatDataResponse.common.dataLen = header->dataLen;
809
810 if ((error = cliprdr_read_format_data_response(s, &formatDataResponse)))
811 return error;
812
813 IFCALLRET(context->ClientFormatDataResponse, error, context, &formatDataResponse);
814
815 if (error)
816 WLog_ERR(TAG, "ClientFormatDataResponse failed with error %" PRIu32 "!", error);
817
818 return error;
819}
820
826static UINT cliprdr_server_receive_filecontents_request(CliprdrServerContext* context, wStream* s,
827 const CLIPRDR_HEADER* header)
828{
829 CLIPRDR_FILE_CONTENTS_REQUEST request = { 0 };
830 UINT error = CHANNEL_RC_OK;
831
832 WINPR_ASSERT(context);
833 WINPR_ASSERT(header);
834
835 WLog_DBG(TAG, "CliprdrClientFileContentsRequest");
836 request.common.msgType = CB_FILECONTENTS_REQUEST;
837 request.common.msgFlags = header->msgFlags;
838 request.common.dataLen = header->dataLen;
839
840 if ((error = cliprdr_read_file_contents_request(s, &request)))
841 return error;
842
843 if (!context->hasHugeFileSupport)
844 {
845 if (request.nPositionHigh > 0)
846 return ERROR_INVALID_DATA;
847 if ((UINT64)request.nPositionLow + request.cbRequested > UINT32_MAX)
848 return ERROR_INVALID_DATA;
849 }
850 IFCALLRET(context->ClientFileContentsRequest, error, context, &request);
851
852 if (error)
853 WLog_ERR(TAG, "ClientFileContentsRequest failed with error %" PRIu32 "!", error);
854
855 return error;
856}
857
863static UINT cliprdr_server_receive_filecontents_response(CliprdrServerContext* context, wStream* s,
864 const CLIPRDR_HEADER* header)
865{
866 CLIPRDR_FILE_CONTENTS_RESPONSE response = { 0 };
867 UINT error = CHANNEL_RC_OK;
868
869 WINPR_ASSERT(context);
870 WINPR_ASSERT(header);
871
872 WLog_DBG(TAG, "CliprdrClientFileContentsResponse");
873
874 response.common.msgType = CB_FILECONTENTS_RESPONSE;
875 response.common.msgFlags = header->msgFlags;
876 response.common.dataLen = header->dataLen;
877
878 if ((error = cliprdr_read_file_contents_response(s, &response)))
879 return error;
880
881 IFCALLRET(context->ClientFileContentsResponse, error, context, &response);
882
883 if (error)
884 WLog_ERR(TAG, "ClientFileContentsResponse failed with error %" PRIu32 "!", error);
885
886 return error;
887}
888
894static UINT cliprdr_server_receive_pdu(CliprdrServerContext* context, wStream* s,
895 const CLIPRDR_HEADER* header)
896{
897 UINT error = 0;
898
899 WINPR_ASSERT(context);
900 WINPR_ASSERT(header);
901
902 char buffer1[64] = { 0 };
903 char buffer2[64] = { 0 };
904 WLog_DBG(TAG, "CliprdrServerReceivePdu: msgType: %s, msgFlags: %s dataLen: %" PRIu32 "",
905 CB_MSG_TYPE_STRING(header->msgType, buffer1, sizeof(buffer1)),
906 CB_MSG_FLAGS_STRING(header->msgFlags, buffer2, sizeof(buffer2)), header->dataLen);
907
908 switch (header->msgType)
909 {
910 case CB_CLIP_CAPS:
911 if ((error = cliprdr_server_receive_capabilities(context, s, header)))
912 WLog_ERR(TAG, "cliprdr_server_receive_capabilities failed with error %" PRIu32 "!",
913 error);
914
915 break;
916
917 case CB_TEMP_DIRECTORY:
918 if ((error = cliprdr_server_receive_temporary_directory(context, s, header)))
919 WLog_ERR(TAG,
920 "cliprdr_server_receive_temporary_directory failed with error %" PRIu32
921 "!",
922 error);
923
924 break;
925
926 case CB_FORMAT_LIST:
927 if ((error = cliprdr_server_receive_format_list(context, s, header)))
928 WLog_ERR(TAG, "cliprdr_server_receive_format_list failed with error %" PRIu32 "!",
929 error);
930
931 break;
932
933 case CB_FORMAT_LIST_RESPONSE:
934 if ((error = cliprdr_server_receive_format_list_response(context, s, header)))
935 WLog_ERR(TAG,
936 "cliprdr_server_receive_format_list_response failed with error %" PRIu32
937 "!",
938 error);
939
940 break;
941
942 case CB_LOCK_CLIPDATA:
943 if ((error = cliprdr_server_receive_lock_clipdata(context, s, header)))
944 WLog_ERR(TAG, "cliprdr_server_receive_lock_clipdata failed with error %" PRIu32 "!",
945 error);
946
947 break;
948
949 case CB_UNLOCK_CLIPDATA:
950 if ((error = cliprdr_server_receive_unlock_clipdata(context, s, header)))
951 WLog_ERR(TAG,
952 "cliprdr_server_receive_unlock_clipdata failed with error %" PRIu32 "!",
953 error);
954
955 break;
956
957 case CB_FORMAT_DATA_REQUEST:
958 if ((error = cliprdr_server_receive_format_data_request(context, s, header)))
959 WLog_ERR(TAG,
960 "cliprdr_server_receive_format_data_request failed with error %" PRIu32
961 "!",
962 error);
963
964 break;
965
966 case CB_FORMAT_DATA_RESPONSE:
967 if ((error = cliprdr_server_receive_format_data_response(context, s, header)))
968 WLog_ERR(TAG,
969 "cliprdr_server_receive_format_data_response failed with error %" PRIu32
970 "!",
971 error);
972
973 break;
974
975 case CB_FILECONTENTS_REQUEST:
976 if ((error = cliprdr_server_receive_filecontents_request(context, s, header)))
977 WLog_ERR(TAG,
978 "cliprdr_server_receive_filecontents_request failed with error %" PRIu32
979 "!",
980 error);
981
982 break;
983
984 case CB_FILECONTENTS_RESPONSE:
985 if ((error = cliprdr_server_receive_filecontents_response(context, s, header)))
986 WLog_ERR(TAG,
987 "cliprdr_server_receive_filecontents_response failed with error %" PRIu32
988 "!",
989 error);
990
991 break;
992
993 default:
994 error = ERROR_INVALID_DATA;
995 WLog_ERR(TAG, "Unexpected clipboard PDU type: %" PRIu16 "", header->msgType);
996 break;
997 }
998
999 return error;
1000}
1001
1007static UINT cliprdr_server_init(CliprdrServerContext* context)
1008{
1009 UINT32 generalFlags = 0;
1010 CLIPRDR_GENERAL_CAPABILITY_SET generalCapabilitySet = { 0 };
1011 UINT error = 0;
1012 CLIPRDR_MONITOR_READY monitorReady = { 0 };
1013 CLIPRDR_CAPABILITIES capabilities = { 0 };
1014
1015 WINPR_ASSERT(context);
1016
1017 monitorReady.common.msgType = CB_MONITOR_READY;
1018 capabilities.common.msgType = CB_CLIP_CAPS;
1019
1020 if (context->useLongFormatNames)
1021 generalFlags |= CB_USE_LONG_FORMAT_NAMES;
1022
1023 if (context->streamFileClipEnabled)
1024 generalFlags |= CB_STREAM_FILECLIP_ENABLED;
1025
1026 if (context->fileClipNoFilePaths)
1027 generalFlags |= CB_FILECLIP_NO_FILE_PATHS;
1028
1029 if (context->canLockClipData)
1030 generalFlags |= CB_CAN_LOCK_CLIPDATA;
1031
1032 if (context->hasHugeFileSupport)
1033 generalFlags |= CB_HUGE_FILE_SUPPORT_ENABLED;
1034
1035 capabilities.common.msgType = CB_CLIP_CAPS;
1036 capabilities.common.msgFlags = 0;
1037 capabilities.common.dataLen = 4 + CB_CAPSTYPE_GENERAL_LEN;
1038 capabilities.cCapabilitiesSets = 1;
1039 capabilities.capabilitySets = (CLIPRDR_CAPABILITY_SET*)&generalCapabilitySet;
1040 generalCapabilitySet.capabilitySetType = CB_CAPSTYPE_GENERAL;
1041 generalCapabilitySet.capabilitySetLength = CB_CAPSTYPE_GENERAL_LEN;
1042 generalCapabilitySet.version = CB_CAPS_VERSION_2;
1043 generalCapabilitySet.generalFlags = generalFlags;
1044
1045 if ((error = context->ServerCapabilities(context, &capabilities)))
1046 {
1047 WLog_ERR(TAG, "ServerCapabilities failed with error %" PRIu32 "!", error);
1048 return error;
1049 }
1050
1051 if ((error = context->MonitorReady(context, &monitorReady)))
1052 {
1053 WLog_ERR(TAG, "MonitorReady failed with error %" PRIu32 "!", error);
1054 return error;
1055 }
1056
1057 return error;
1058}
1059
1065static UINT cliprdr_server_read(CliprdrServerContext* context)
1066{
1067 wStream* s = NULL;
1068 size_t position = 0;
1069 DWORD BytesToRead = 0;
1070 DWORD BytesReturned = 0;
1071 CLIPRDR_HEADER header = { 0 };
1072 CliprdrServerPrivate* cliprdr = NULL;
1073 UINT error = 0;
1074 DWORD status = 0;
1075
1076 WINPR_ASSERT(context);
1077
1078 cliprdr = (CliprdrServerPrivate*)context->handle;
1079 WINPR_ASSERT(cliprdr);
1080
1081 s = cliprdr->s;
1082
1083 if (Stream_GetPosition(s) < CLIPRDR_HEADER_LENGTH)
1084 {
1085 BytesReturned = 0;
1086 BytesToRead = (UINT32)(CLIPRDR_HEADER_LENGTH - Stream_GetPosition(s));
1087 status = WaitForSingleObject(cliprdr->ChannelEvent, 0);
1088
1089 if (status == WAIT_FAILED)
1090 {
1091 error = GetLastError();
1092 WLog_ERR(TAG, "WaitForSingleObject failed with error %" PRIu32 "", error);
1093 return error;
1094 }
1095
1096 if (status == WAIT_TIMEOUT)
1097 return CHANNEL_RC_OK;
1098
1099 if (!WTSVirtualChannelRead(cliprdr->ChannelHandle, 0, Stream_Pointer(s), BytesToRead,
1100 &BytesReturned))
1101 {
1102 WLog_ERR(TAG, "WTSVirtualChannelRead failed!");
1103 return ERROR_INTERNAL_ERROR;
1104 }
1105
1106 Stream_Seek(s, BytesReturned);
1107 }
1108
1109 if (Stream_GetPosition(s) >= CLIPRDR_HEADER_LENGTH)
1110 {
1111 position = Stream_GetPosition(s);
1112 Stream_SetPosition(s, 0);
1113 Stream_Read_UINT16(s, header.msgType); /* msgType (2 bytes) */
1114 Stream_Read_UINT16(s, header.msgFlags); /* msgFlags (2 bytes) */
1115 Stream_Read_UINT32(s, header.dataLen); /* dataLen (4 bytes) */
1116
1117 if (!Stream_EnsureRemainingCapacity(s, header.dataLen))
1118 {
1119 WLog_ERR(TAG, "Stream_EnsureCapacity failed!");
1120 return CHANNEL_RC_NO_MEMORY;
1121 }
1122
1123 Stream_SetPosition(s, position);
1124
1125 if (Stream_GetPosition(s) < (header.dataLen + CLIPRDR_HEADER_LENGTH))
1126 {
1127 BytesReturned = 0;
1128 BytesToRead =
1129 (UINT32)((header.dataLen + CLIPRDR_HEADER_LENGTH) - Stream_GetPosition(s));
1130 status = WaitForSingleObject(cliprdr->ChannelEvent, 0);
1131
1132 if (status == WAIT_FAILED)
1133 {
1134 error = GetLastError();
1135 WLog_ERR(TAG, "WaitForSingleObject failed with error %" PRIu32 "", error);
1136 return error;
1137 }
1138
1139 if (status == WAIT_TIMEOUT)
1140 return CHANNEL_RC_OK;
1141
1142 if (!WTSVirtualChannelRead(cliprdr->ChannelHandle, 0, Stream_Pointer(s), BytesToRead,
1143 &BytesReturned))
1144 {
1145 WLog_ERR(TAG, "WTSVirtualChannelRead failed!");
1146 return ERROR_INTERNAL_ERROR;
1147 }
1148
1149 Stream_Seek(s, BytesReturned);
1150 }
1151
1152 if (Stream_GetPosition(s) >= (header.dataLen + CLIPRDR_HEADER_LENGTH))
1153 {
1154 Stream_SetPosition(s, (header.dataLen + CLIPRDR_HEADER_LENGTH));
1155 Stream_SealLength(s);
1156 Stream_SetPosition(s, CLIPRDR_HEADER_LENGTH);
1157
1158 if ((error = cliprdr_server_receive_pdu(context, s, &header)))
1159 {
1160 WLog_ERR(TAG, "cliprdr_server_receive_pdu failed with error code %" PRIu32 "!",
1161 error);
1162 return error;
1163 }
1164
1165 Stream_SetPosition(s, 0);
1166 /* check for trailing zero bytes */
1167 status = WaitForSingleObject(cliprdr->ChannelEvent, 0);
1168
1169 if (status == WAIT_FAILED)
1170 {
1171 error = GetLastError();
1172 WLog_ERR(TAG, "WaitForSingleObject failed with error %" PRIu32 "", error);
1173 return error;
1174 }
1175
1176 if (status == WAIT_TIMEOUT)
1177 return CHANNEL_RC_OK;
1178
1179 BytesReturned = 0;
1180 BytesToRead = 4;
1181
1182 if (!WTSVirtualChannelRead(cliprdr->ChannelHandle, 0, Stream_Pointer(s), BytesToRead,
1183 &BytesReturned))
1184 {
1185 WLog_ERR(TAG, "WTSVirtualChannelRead failed!");
1186 return ERROR_INTERNAL_ERROR;
1187 }
1188
1189 if (BytesReturned == 4)
1190 {
1191 Stream_Read_UINT16(s, header.msgType); /* msgType (2 bytes) */
1192 Stream_Read_UINT16(s, header.msgFlags); /* msgFlags (2 bytes) */
1193
1194 if (!header.msgType)
1195 {
1196 /* ignore trailing bytes */
1197 Stream_SetPosition(s, 0);
1198 }
1199 }
1200 else
1201 {
1202 Stream_Seek(s, BytesReturned);
1203 }
1204 }
1205 }
1206
1207 return CHANNEL_RC_OK;
1208}
1209
1210static DWORD WINAPI cliprdr_server_thread(LPVOID arg)
1211{
1212 DWORD status = 0;
1213 DWORD nCount = 0;
1214 HANDLE events[MAXIMUM_WAIT_OBJECTS] = { 0 };
1215 HANDLE ChannelEvent = NULL;
1216 CliprdrServerContext* context = (CliprdrServerContext*)arg;
1217 CliprdrServerPrivate* cliprdr = NULL;
1218 UINT error = CHANNEL_RC_OK;
1219
1220 WINPR_ASSERT(context);
1221
1222 cliprdr = (CliprdrServerPrivate*)context->handle;
1223 WINPR_ASSERT(cliprdr);
1224
1225 ChannelEvent = context->GetEventHandle(context);
1226
1227 events[nCount++] = cliprdr->StopEvent;
1228 events[nCount++] = ChannelEvent;
1229
1230 if (context->autoInitializationSequence)
1231 {
1232 if ((error = cliprdr_server_init(context)))
1233 {
1234 WLog_ERR(TAG, "cliprdr_server_init failed with error %" PRIu32 "!", error);
1235 goto out;
1236 }
1237 }
1238
1239 while (1)
1240 {
1241 status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE);
1242
1243 if (status == WAIT_FAILED)
1244 {
1245 error = GetLastError();
1246 WLog_ERR(TAG, "WaitForMultipleObjects failed with error %" PRIu32 "", error);
1247 goto out;
1248 }
1249
1250 status = WaitForSingleObject(cliprdr->StopEvent, 0);
1251
1252 if (status == WAIT_FAILED)
1253 {
1254 error = GetLastError();
1255 WLog_ERR(TAG, "WaitForSingleObject failed with error %" PRIu32 "", error);
1256 goto out;
1257 }
1258
1259 if (status == WAIT_OBJECT_0)
1260 break;
1261
1262 status = WaitForSingleObject(ChannelEvent, 0);
1263
1264 if (status == WAIT_FAILED)
1265 {
1266 error = GetLastError();
1267 WLog_ERR(TAG, "WaitForSingleObject failed with error %" PRIu32 "", error);
1268 goto out;
1269 }
1270
1271 if (status == WAIT_OBJECT_0)
1272 {
1273 if ((error = context->CheckEventHandle(context)))
1274 {
1275 WLog_ERR(TAG, "CheckEventHandle failed with error %" PRIu32 "!", error);
1276 break;
1277 }
1278 }
1279 }
1280
1281out:
1282
1283 if (error && context->rdpcontext)
1284 setChannelError(context->rdpcontext, error, "cliprdr_server_thread reported an error");
1285
1286 ExitThread(error);
1287 return error;
1288}
1289
1295static UINT cliprdr_server_open(CliprdrServerContext* context)
1296{
1297 void* buffer = NULL;
1298 DWORD BytesReturned = 0;
1299 CliprdrServerPrivate* cliprdr = NULL;
1300
1301 WINPR_ASSERT(context);
1302
1303 cliprdr = (CliprdrServerPrivate*)context->handle;
1304 WINPR_ASSERT(cliprdr);
1305
1306 cliprdr->ChannelHandle =
1307 WTSVirtualChannelOpen(cliprdr->vcm, WTS_CURRENT_SESSION, CLIPRDR_SVC_CHANNEL_NAME);
1308
1309 if (!cliprdr->ChannelHandle)
1310 {
1311 WLog_ERR(TAG, "WTSVirtualChannelOpen for cliprdr failed!");
1312 return ERROR_INTERNAL_ERROR;
1313 }
1314
1315 cliprdr->ChannelEvent = NULL;
1316
1317 if (WTSVirtualChannelQuery(cliprdr->ChannelHandle, WTSVirtualEventHandle, &buffer,
1318 &BytesReturned))
1319 {
1320 if (BytesReturned != sizeof(HANDLE))
1321 {
1322 WLog_ERR(TAG, "BytesReturned has not size of HANDLE!");
1323 return ERROR_INTERNAL_ERROR;
1324 }
1325
1326 cliprdr->ChannelEvent = *(HANDLE*)buffer;
1327 WTSFreeMemory(buffer);
1328 }
1329
1330 if (!cliprdr->ChannelEvent)
1331 {
1332 WLog_ERR(TAG, "WTSVirtualChannelQuery for cliprdr failed!");
1333 return ERROR_INTERNAL_ERROR;
1334 }
1335
1336 return CHANNEL_RC_OK;
1337}
1338
1344static UINT cliprdr_server_close(CliprdrServerContext* context)
1345{
1346 CliprdrServerPrivate* cliprdr = NULL;
1347
1348 WINPR_ASSERT(context);
1349
1350 cliprdr = (CliprdrServerPrivate*)context->handle;
1351 WINPR_ASSERT(cliprdr);
1352
1353 if (cliprdr->ChannelHandle)
1354 {
1355 (void)WTSVirtualChannelClose(cliprdr->ChannelHandle);
1356 cliprdr->ChannelHandle = NULL;
1357 }
1358
1359 return CHANNEL_RC_OK;
1360}
1361
1367static UINT cliprdr_server_start(CliprdrServerContext* context)
1368{
1369 UINT error = 0;
1370 CliprdrServerPrivate* cliprdr = NULL;
1371
1372 WINPR_ASSERT(context);
1373
1374 cliprdr = (CliprdrServerPrivate*)context->handle;
1375 WINPR_ASSERT(cliprdr);
1376
1377 if (!cliprdr->ChannelHandle)
1378 {
1379 if ((error = context->Open(context)))
1380 {
1381 WLog_ERR(TAG, "Open failed!");
1382 return error;
1383 }
1384 }
1385
1386 if (!(cliprdr->StopEvent = CreateEvent(NULL, TRUE, FALSE, NULL)))
1387 {
1388 WLog_ERR(TAG, "CreateEvent failed!");
1389 return ERROR_INTERNAL_ERROR;
1390 }
1391
1392 if (!(cliprdr->Thread = CreateThread(NULL, 0, cliprdr_server_thread, (void*)context, 0, NULL)))
1393 {
1394 WLog_ERR(TAG, "CreateThread failed!");
1395 (void)CloseHandle(cliprdr->StopEvent);
1396 cliprdr->StopEvent = NULL;
1397 return ERROR_INTERNAL_ERROR;
1398 }
1399
1400 return CHANNEL_RC_OK;
1401}
1402
1408static UINT cliprdr_server_stop(CliprdrServerContext* context)
1409{
1410 UINT error = CHANNEL_RC_OK;
1411 CliprdrServerPrivate* cliprdr = NULL;
1412
1413 WINPR_ASSERT(context);
1414
1415 cliprdr = (CliprdrServerPrivate*)context->handle;
1416 WINPR_ASSERT(cliprdr);
1417
1418 if (cliprdr->StopEvent)
1419 {
1420 (void)SetEvent(cliprdr->StopEvent);
1421
1422 if (WaitForSingleObject(cliprdr->Thread, INFINITE) == WAIT_FAILED)
1423 {
1424 error = GetLastError();
1425 WLog_ERR(TAG, "WaitForSingleObject failed with error %" PRIu32 "", error);
1426 return error;
1427 }
1428
1429 (void)CloseHandle(cliprdr->Thread);
1430 (void)CloseHandle(cliprdr->StopEvent);
1431 }
1432
1433 if (cliprdr->ChannelHandle)
1434 return context->Close(context);
1435
1436 return error;
1437}
1438
1439static HANDLE cliprdr_server_get_event_handle(CliprdrServerContext* context)
1440{
1441 CliprdrServerPrivate* cliprdr = NULL;
1442
1443 WINPR_ASSERT(context);
1444
1445 cliprdr = (CliprdrServerPrivate*)context->handle;
1446 WINPR_ASSERT(cliprdr);
1447 return cliprdr->ChannelEvent;
1448}
1449
1455static UINT cliprdr_server_check_event_handle(CliprdrServerContext* context)
1456{
1457 return cliprdr_server_read(context);
1458}
1459
1460CliprdrServerContext* cliprdr_server_context_new(HANDLE vcm)
1461{
1462 CliprdrServerPrivate* cliprdr = NULL;
1463 CliprdrServerContext* context = (CliprdrServerContext*)calloc(1, sizeof(CliprdrServerContext));
1464
1465 if (context)
1466 {
1467 context->autoInitializationSequence = TRUE;
1468 context->Open = cliprdr_server_open;
1469 context->Close = cliprdr_server_close;
1470 context->Start = cliprdr_server_start;
1471 context->Stop = cliprdr_server_stop;
1472 context->GetEventHandle = cliprdr_server_get_event_handle;
1473 context->CheckEventHandle = cliprdr_server_check_event_handle;
1474 context->ServerCapabilities = cliprdr_server_capabilities;
1475 context->MonitorReady = cliprdr_server_monitor_ready;
1476 context->ServerFormatList = cliprdr_server_format_list;
1477 context->ServerFormatListResponse = cliprdr_server_format_list_response;
1478 context->ServerLockClipboardData = cliprdr_server_lock_clipboard_data;
1479 context->ServerUnlockClipboardData = cliprdr_server_unlock_clipboard_data;
1480 context->ServerFormatDataRequest = cliprdr_server_format_data_request;
1481 context->ServerFormatDataResponse = cliprdr_server_format_data_response;
1482 context->ServerFileContentsRequest = cliprdr_server_file_contents_request;
1483 context->ServerFileContentsResponse = cliprdr_server_file_contents_response;
1484 cliprdr = context->handle = (CliprdrServerPrivate*)calloc(1, sizeof(CliprdrServerPrivate));
1485
1486 if (cliprdr)
1487 {
1488 cliprdr->vcm = vcm;
1489 cliprdr->s = Stream_New(NULL, 4096);
1490
1491 if (!cliprdr->s)
1492 {
1493 WLog_ERR(TAG, "Stream_New failed!");
1494 free(context->handle);
1495 free(context);
1496 return NULL;
1497 }
1498 }
1499 else
1500 {
1501 WLog_ERR(TAG, "calloc failed!");
1502 free(context);
1503 return NULL;
1504 }
1505 }
1506
1507 return context;
1508}
1509
1510void cliprdr_server_context_free(CliprdrServerContext* context)
1511{
1512 CliprdrServerPrivate* cliprdr = NULL;
1513
1514 if (!context)
1515 return;
1516
1517 cliprdr = (CliprdrServerPrivate*)context->handle;
1518
1519 if (cliprdr)
1520 {
1521 Stream_Free(cliprdr->s, TRUE);
1522 }
1523
1524 free(context->handle);
1525 free(context);
1526}