22#include <freerdp/config.h>
25#include <winpr/assert.h>
26#include <winpr/print.h>
27#include <winpr/stream.h>
29#include <freerdp/freerdp.h>
30#include <freerdp/channels/log.h>
31#include "cliprdr_main.h"
32#include "../cliprdr_common.h"
85 WINPR_ASSERT(cliprdr);
87 const size_t pos = Stream_GetPosition(s);
88 if ((pos < 8) || (pos > UINT32_MAX))
94 const UINT32 dataLen = (UINT32)(pos - 8);
95 if (!Stream_SetPosition(s, 4))
97 Stream_Write_UINT32(s, dataLen);
99 WINPR_ASSERT(pos <= UINT32_MAX);
100 status = WTSVirtualChannelWrite(cliprdr->ChannelHandle, Stream_BufferAs(s,
char), (UINT32)pos,
102 rc = status ? CHANNEL_RC_OK : ERROR_INTERNAL_ERROR;
104 Stream_Free(s, TRUE);
113static UINT cliprdr_server_capabilities(CliprdrServerContext* context,
118 WINPR_ASSERT(context);
119 WINPR_ASSERT(capabilities);
123 if (capabilities->common.msgType != CB_CLIP_CAPS)
124 WLog_Print(cliprdr->log, WLOG_WARN,
"called with invalid type %08" PRIx32,
125 capabilities->common.msgType);
127 if (capabilities->cCapabilitiesSets > UINT16_MAX)
129 WLog_Print(cliprdr->log, WLOG_ERROR,
"Invalid number of capability sets in clipboard caps");
130 return ERROR_INVALID_PARAMETER;
133 wStream* s = cliprdr_packet_new(CB_CLIP_CAPS, 0, 4 + CB_CAPSTYPE_GENERAL_LEN);
137 WLog_Print(cliprdr->log, WLOG_ERROR,
"cliprdr_packet_new failed!");
138 return ERROR_INTERNAL_ERROR;
141 Stream_Write_UINT16(s,
142 (UINT16)capabilities->cCapabilitiesSets);
143 Stream_Write_UINT16(s, 0);
144 for (UINT32 x = 0; x < capabilities->cCapabilitiesSets; x++)
148 offset += cap->capabilitySetLength;
150 switch (cap->capabilitySetType)
152 case CB_CAPSTYPE_GENERAL:
157 s, generalCapabilitySet->capabilitySetType);
159 s, generalCapabilitySet->capabilitySetLength);
160 Stream_Write_UINT32(s, generalCapabilitySet->version);
162 s, generalCapabilitySet->generalFlags);
167 WLog_Print(cliprdr->log, WLOG_WARN,
"Unknown capability set type %08" PRIx16,
168 cap->capabilitySetType);
169 if (!Stream_SafeSeek(s, cap->capabilitySetLength))
171 WLog_Print(cliprdr->log, WLOG_ERROR,
"short stream");
172 Stream_Free(s, TRUE);
173 return ERROR_NO_DATA;
178 WLog_Print(cliprdr->log, WLOG_DEBUG,
"ServerCapabilities");
179 return cliprdr_server_packet_send(cliprdr, s);
187static UINT cliprdr_server_monitor_ready(CliprdrServerContext* context,
190 WINPR_ASSERT(context);
191 WINPR_ASSERT(monitorReady);
195 if (monitorReady->common.msgType != CB_MONITOR_READY)
196 WLog_Print(cliprdr->log, WLOG_WARN,
"called with invalid type %08" PRIx32,
197 monitorReady->common.msgType);
199 wStream* s = cliprdr_packet_new(CB_MONITOR_READY, monitorReady->common.msgFlags,
200 monitorReady->common.dataLen);
204 WLog_Print(cliprdr->log, WLOG_ERROR,
"cliprdr_packet_new failed!");
205 return ERROR_INTERNAL_ERROR;
208 WLog_Print(cliprdr->log, WLOG_DEBUG,
"ServerMonitorReady");
209 return cliprdr_server_packet_send(cliprdr, s);
217static UINT cliprdr_server_format_list(CliprdrServerContext* context,
220 WINPR_ASSERT(context);
221 WINPR_ASSERT(formatList);
225 wStream* s = cliprdr_packet_format_list_new(formatList, context->useLongFormatNames, FALSE);
228 WLog_Print(cliprdr->log, WLOG_ERROR,
"cliprdr_packet_format_list_new failed!");
229 return ERROR_INTERNAL_ERROR;
232 WLog_Print(cliprdr->log, WLOG_DEBUG,
"ServerFormatList: numFormats: %" PRIu32
"",
233 formatList->numFormats);
234 return cliprdr_server_packet_send(cliprdr, s);
243cliprdr_server_format_list_response(CliprdrServerContext* context,
246 WINPR_ASSERT(context);
247 WINPR_ASSERT(formatListResponse);
250 if (formatListResponse->common.msgType != CB_FORMAT_LIST_RESPONSE)
251 WLog_Print(cliprdr->log, WLOG_WARN,
"called with invalid type %08" PRIx32,
252 formatListResponse->common.msgType);
254 wStream* s = cliprdr_packet_new(CB_FORMAT_LIST_RESPONSE, formatListResponse->common.msgFlags,
255 formatListResponse->common.dataLen);
259 WLog_Print(cliprdr->log, WLOG_ERROR,
"cliprdr_packet_new failed!");
260 return ERROR_INTERNAL_ERROR;
263 WLog_Print(cliprdr->log, WLOG_DEBUG,
"ServerFormatListResponse");
264 return cliprdr_server_packet_send(cliprdr, s);
272static UINT cliprdr_server_lock_clipboard_data(CliprdrServerContext* context,
275 WINPR_ASSERT(context);
276 WINPR_ASSERT(lockClipboardData);
279 if (lockClipboardData->common.msgType != CB_LOCK_CLIPDATA)
280 WLog_Print(cliprdr->log, WLOG_WARN,
"called with invalid type %08" PRIx32,
281 lockClipboardData->common.msgType);
283 wStream* s = cliprdr_packet_lock_clipdata_new(lockClipboardData);
286 WLog_Print(cliprdr->log, WLOG_ERROR,
"cliprdr_packet_lock_clipdata_new failed!");
287 return ERROR_INTERNAL_ERROR;
290 WLog_Print(cliprdr->log, WLOG_DEBUG,
"ServerLockClipboardData: clipDataId: 0x%08" PRIX32
"",
291 lockClipboardData->clipDataId);
292 return cliprdr_server_packet_send(cliprdr, s);
301cliprdr_server_unlock_clipboard_data(CliprdrServerContext* context,
304 WINPR_ASSERT(context);
305 WINPR_ASSERT(unlockClipboardData);
308 if (unlockClipboardData->common.msgType != CB_UNLOCK_CLIPDATA)
309 WLog_Print(cliprdr->log, WLOG_WARN,
"called with invalid type %08" PRIx32,
310 unlockClipboardData->common.msgType);
312 wStream* s = cliprdr_packet_unlock_clipdata_new(unlockClipboardData);
316 WLog_Print(cliprdr->log, WLOG_ERROR,
"cliprdr_packet_unlock_clipdata_new failed!");
317 return ERROR_INTERNAL_ERROR;
320 WLog_Print(cliprdr->log, WLOG_DEBUG,
"ServerUnlockClipboardData: clipDataId: 0x%08" PRIX32
"",
321 unlockClipboardData->clipDataId);
322 return cliprdr_server_packet_send(cliprdr, s);
330static UINT cliprdr_server_format_data_request(CliprdrServerContext* context,
333 WINPR_ASSERT(context);
334 WINPR_ASSERT(formatDataRequest);
337 if (formatDataRequest->common.msgType != CB_FORMAT_DATA_REQUEST)
338 WLog_Print(cliprdr->log, WLOG_WARN,
"called with invalid type %08" PRIx32,
339 formatDataRequest->common.msgType);
341 wStream* s = cliprdr_packet_new(CB_FORMAT_DATA_REQUEST, formatDataRequest->common.msgFlags,
342 formatDataRequest->common.dataLen);
346 WLog_Print(cliprdr->log, WLOG_ERROR,
"cliprdr_packet_new failed!");
347 return ERROR_INTERNAL_ERROR;
350 Stream_Write_UINT32(s, formatDataRequest->requestedFormatId);
351 WLog_Print(cliprdr->log, WLOG_DEBUG,
"ClientFormatDataRequest");
352 return cliprdr_server_packet_send(cliprdr, s);
361cliprdr_server_format_data_response(CliprdrServerContext* context,
364 WINPR_ASSERT(context);
365 WINPR_ASSERT(formatDataResponse);
369 if (formatDataResponse->common.msgType != CB_FORMAT_DATA_RESPONSE)
370 WLog_Print(cliprdr->log, WLOG_WARN,
"called with invalid type %08" PRIx32,
371 formatDataResponse->common.msgType);
373 wStream* s = cliprdr_packet_new(CB_FORMAT_DATA_RESPONSE, formatDataResponse->common.msgFlags,
374 formatDataResponse->common.dataLen);
378 WLog_Print(cliprdr->log, WLOG_ERROR,
"cliprdr_packet_new failed!");
379 return ERROR_INTERNAL_ERROR;
382 Stream_Write(s, formatDataResponse->requestedFormatData, formatDataResponse->common.dataLen);
383 WLog_Print(cliprdr->log, WLOG_DEBUG,
"ServerFormatDataResponse");
384 return cliprdr_server_packet_send(cliprdr, s);
393cliprdr_server_file_contents_request(CliprdrServerContext* context,
396 WINPR_ASSERT(context);
397 WINPR_ASSERT(fileContentsRequest);
401 if (fileContentsRequest->common.msgType != CB_FILECONTENTS_REQUEST)
402 WLog_Print(cliprdr->log, WLOG_WARN,
"called with invalid type %08" PRIx32,
403 fileContentsRequest->common.msgType);
405 wStream* s = cliprdr_packet_file_contents_request_new(fileContentsRequest);
408 WLog_Print(cliprdr->log, WLOG_ERROR,
"cliprdr_packet_file_contents_request_new failed!");
409 return ERROR_INTERNAL_ERROR;
412 WLog_Print(cliprdr->log, WLOG_DEBUG,
"ServerFileContentsRequest: streamId: 0x%08" PRIX32
"",
413 fileContentsRequest->streamId);
414 return cliprdr_server_packet_send(cliprdr, s);
423cliprdr_server_file_contents_response(CliprdrServerContext* context,
426 WINPR_ASSERT(context);
427 WINPR_ASSERT(fileContentsResponse);
430 WINPR_ASSERT(cliprdr);
432 if (fileContentsResponse->common.msgType != CB_FILECONTENTS_RESPONSE)
433 WLog_Print(cliprdr->log, WLOG_WARN,
"called with invalid type %08" PRIx32,
434 fileContentsResponse->common.msgType);
436 wStream* s = cliprdr_packet_file_contents_response_new(fileContentsResponse);
439 WLog_Print(cliprdr->log, WLOG_ERROR,
"cliprdr_packet_file_contents_response_new failed!");
440 return ERROR_INTERNAL_ERROR;
443 WLog_Print(cliprdr->log, WLOG_DEBUG,
"ServerFileContentsResponse: streamId: 0x%08" PRIX32
"",
444 fileContentsResponse->streamId);
445 return cliprdr_server_packet_send(cliprdr, s);
453static UINT cliprdr_server_receive_general_capability(CliprdrServerContext* context,
wStream* s,
456 WINPR_ASSERT(context);
457 WINPR_ASSERT(cap_set);
460 WINPR_ASSERT(cliprdr);
464 WLog_Print(cliprdr->log, WLOG_ERROR,
465 "invalid capabilitySetLength %" PRIu16
" != %" PRIuz
466 " (capabilitySetType=CB_CAPSTYPE_GENERAL)",
468 return ERROR_INVALID_DATA;
471 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
472 return ERROR_INVALID_DATA;
474 Stream_Read_UINT32(s, cap_set->version);
475 Stream_Read_UINT32(s, cap_set->generalFlags);
477 if (context->useLongFormatNames)
478 context->useLongFormatNames = (cap_set->generalFlags & CB_USE_LONG_FORMAT_NAMES) != 0;
480 if (context->streamFileClipEnabled)
481 context->streamFileClipEnabled = (cap_set->generalFlags & CB_STREAM_FILECLIP_ENABLED) != 0;
483 if (context->fileClipNoFilePaths)
484 context->fileClipNoFilePaths = (cap_set->generalFlags & CB_FILECLIP_NO_FILE_PATHS) != 0;
486 if (context->canLockClipData)
487 context->canLockClipData = (cap_set->generalFlags & CB_CAN_LOCK_CLIPDATA) != 0;
489 if (context->hasHugeFileSupport)
490 context->hasHugeFileSupport = (cap_set->generalFlags & CB_HUGE_FILE_SUPPORT_ENABLED) != 0;
492 return CHANNEL_RC_OK;
496static BOOL cliprdr_capabilities_contain_capset(wLog* log,
const CLIPRDR_CAPABILITIES* capabilities,
497 size_t capabilitiesSize,
500 WINPR_ASSERT(capabilities);
502 const BYTE* offset = (
const BYTE*)capabilities->capabilitySets;
503 size_t byteOffset = 0;
504 for (UINT32 x = 0; x < capabilities->cCapabilitiesSets; x++)
507 byteOffset += cur->capabilitySetLength;
508 if (byteOffset > capabilitiesSize)
511 offset += cur->capabilitySetLength;
513 if (cur->capabilitySetType == capset->capabilitySetType)
515 WLog_Print(log, WLOG_ERROR,
"duplicate CLIPRDR_CAPABILITY_SET %" PRIu16,
516 cur->capabilitySetType);
528static UINT cliprdr_server_receive_capabilities(CliprdrServerContext* context,
wStream* s,
531 UINT error = ERROR_INVALID_DATA;
532 size_t cap_sets_size = 0;
535 WINPR_ASSERT(context);
536 WINPR_UNUSED(header);
539 WINPR_ASSERT(cliprdr);
541 WLog_Print(cliprdr->log, WLOG_DEBUG,
"CliprdrClientCapabilities");
542 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
543 return ERROR_INVALID_DATA;
545 const UINT16 cCapabilitiesSets = Stream_Get_UINT16(s);
546 Stream_Seek_UINT16(s);
548 for (
size_t index = 0; index < cCapabilitiesSets; index++)
550 const size_t cap_set_offset = cap_sets_size;
553 const UINT16 capabilitySetType = Stream_Get_UINT16(s);
554 const UINT16 capabilitySetLength = Stream_Get_UINT16(s);
558 WLog_Print(cliprdr->log, WLOG_ERROR,
559 "invalid capabilitySetLength %" PRIu16
" < %" PRIuz
560 " (capabilitySetType=%" PRIu16
")",
565 if (!Stream_CheckAndLogRequiredLength(
569 cap_sets_size += capabilitySetLength;
574 WLog_Print(cliprdr->log, WLOG_ERROR,
"capabilities.capabilitySets realloc failed!");
575 free(capabilities.capabilitySets);
576 return CHANNEL_RC_NO_MEMORY;
579 capabilities.capabilitySets = tmp;
584 capSet->capabilitySetType = capabilitySetType;
585 capSet->capabilitySetLength = capabilitySetLength;
587 if (cliprdr_capabilities_contain_capset(cliprdr->log, &capabilities, cap_sets_size, capSet))
590 capabilities.cCapabilitiesSets++;
592 switch (capSet->capabilitySetType)
594 case CB_CAPSTYPE_GENERAL:
595 error = cliprdr_server_receive_general_capability(
600 cliprdr->log, WLOG_ERROR,
601 "cliprdr_server_receive_general_capability failed with error %" PRIu32
"",
608 WLog_Print(cliprdr->log, WLOG_ERROR,
"unknown cliprdr capability set: %" PRIu16
"",
609 capSet->capabilitySetType);
614 error = CHANNEL_RC_OK;
615 IFCALLRET(context->ClientCapabilities, error, context, &capabilities);
617 free(capabilities.capabilitySets);
626static UINT cliprdr_server_receive_temporary_directory(CliprdrServerContext* context,
wStream* s,
629 UINT error = CHANNEL_RC_OK;
631 WINPR_ASSERT(context);
632 WINPR_UNUSED(header);
635 WINPR_ASSERT(cliprdr);
637 if (!Stream_CheckAndLogRequiredLength(TAG, s,
638 ARRAYSIZE(cliprdr->temporaryDirectory) *
sizeof(WCHAR)))
639 return CHANNEL_RC_NO_MEMORY;
641 const WCHAR* wszTempDir = Stream_ConstPointer(s);
643 if (wszTempDir[ARRAYSIZE(cliprdr->temporaryDirectory) - 1] != 0)
645 WLog_Print(cliprdr->log, WLOG_ERROR,
"wszTempDir[259] was not 0");
646 return ERROR_INVALID_DATA;
649 if (ConvertWCharNToUtf8(wszTempDir, ARRAYSIZE(cliprdr->temporaryDirectory),
650 cliprdr->temporaryDirectory,
651 ARRAYSIZE(cliprdr->temporaryDirectory)) < 0)
653 WLog_Print(cliprdr->log, WLOG_ERROR,
"failed to convert temporary directory name");
654 return ERROR_INVALID_DATA;
657 size_t length = strnlen(cliprdr->temporaryDirectory, ARRAYSIZE(cliprdr->temporaryDirectory));
659 if (length >= ARRAYSIZE(cliprdr->temporaryDirectory))
660 length = ARRAYSIZE(cliprdr->temporaryDirectory) - 1;
663 CopyMemory(tempDirectory.szTempDir, cliprdr->temporaryDirectory, length);
664 tempDirectory.szTempDir[length] =
'\0';
665 WLog_Print(cliprdr->log, WLOG_DEBUG,
"CliprdrTemporaryDirectory: %s",
666 cliprdr->temporaryDirectory);
667 IFCALLRET(context->TempDirectory, error, context, &tempDirectory);
670 WLog_Print(cliprdr->log, WLOG_ERROR,
"TempDirectory failed with error %" PRIu32
"!", error);
680static UINT cliprdr_server_receive_format_list(CliprdrServerContext* context,
wStream* s,
683 UINT error = CHANNEL_RC_OK;
685 WINPR_ASSERT(context);
686 WINPR_ASSERT(header);
689 .msgFlags = header->msgFlags,
690 .dataLen = header->dataLen },
692 .formats =
nullptr };
693 wLog* log = WLog_Get(TAG);
694 if ((error = cliprdr_read_format_list(log, s, &formatList, context->useLongFormatNames)))
697 WLog_Print(log, WLOG_DEBUG,
"ClientFormatList: numFormats: %" PRIu32
"", formatList.numFormats);
698 IFCALLRET(context->ClientFormatList, error, context, &formatList);
701 WLog_Print(log, WLOG_ERROR,
"ClientFormatList failed with error %" PRIu32
"!", error);
704 cliprdr_free_format_list(&formatList);
713static UINT cliprdr_server_receive_format_list_response(CliprdrServerContext* context,
wStream* s,
716 UINT error = CHANNEL_RC_OK;
718 WINPR_ASSERT(context);
719 WINPR_ASSERT(header);
722 WINPR_ASSERT(cliprdr);
725 WLog_Print(cliprdr->log, WLOG_DEBUG,
"CliprdrClientFormatListResponse");
728 CB_FORMAT_LIST_RESPONSE,
729 .msgFlags = header->msgFlags,
730 .dataLen = header->dataLen } };
731 IFCALLRET(context->ClientFormatListResponse, error, context, &formatListResponse);
734 WLog_Print(cliprdr->log, WLOG_ERROR,
735 "ClientFormatListResponse failed with error %" PRIu32
"!", error);
745static UINT cliprdr_server_receive_lock_clipdata(CliprdrServerContext* context,
wStream* s,
748 UINT error = CHANNEL_RC_OK;
750 WINPR_ASSERT(context);
751 WINPR_ASSERT(header);
754 WINPR_ASSERT(cliprdr);
756 WLog_Print(cliprdr->log, WLOG_DEBUG,
"CliprdrClientLockClipData");
758 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
759 return ERROR_INVALID_DATA;
762 .msgFlags = header->msgFlags,
763 .dataLen = header->dataLen },
766 Stream_Read_UINT32(s, lockClipboardData.clipDataId);
767 IFCALLRET(context->ClientLockClipboardData, error, context, &lockClipboardData);
770 WLog_Print(cliprdr->log, WLOG_ERROR,
771 "ClientLockClipboardData failed with error %" PRIu32
"!", error);
781static UINT cliprdr_server_receive_unlock_clipdata(CliprdrServerContext* context,
wStream* s,
784 WINPR_ASSERT(context);
785 WINPR_ASSERT(header);
788 WINPR_ASSERT(cliprdr);
790 WLog_Print(cliprdr->log, WLOG_DEBUG,
"CliprdrClientUnlockClipData");
793 .msgFlags = header->msgFlags,
794 .dataLen = header->dataLen },
797 UINT error = cliprdr_read_unlock_clipdata(s, &unlockClipboardData);
801 IFCALLRET(context->ClientUnlockClipboardData, error, context, &unlockClipboardData);
804 WLog_Print(cliprdr->log, WLOG_ERROR,
805 "ClientUnlockClipboardData failed with error %" PRIu32
"!", error);
815static UINT cliprdr_server_receive_format_data_request(CliprdrServerContext* context,
wStream* s,
818 WINPR_ASSERT(context);
819 WINPR_ASSERT(header);
822 WINPR_ASSERT(cliprdr);
824 WLog_Print(cliprdr->log, WLOG_DEBUG,
"CliprdrClientFormatDataRequest");
827 .msgFlags = header->msgFlags,
828 .dataLen = header->dataLen },
829 .requestedFormatId = 0 };
831 UINT error = cliprdr_read_format_data_request(s, &formatDataRequest);
835 context->lastRequestedFormatId = formatDataRequest.requestedFormatId;
836 IFCALLRET(context->ClientFormatDataRequest, error, context, &formatDataRequest);
839 WLog_Print(cliprdr->log, WLOG_ERROR,
840 "ClientFormatDataRequest failed with error %" PRIu32
"!", error);
850static UINT cliprdr_server_receive_format_data_response(CliprdrServerContext* context,
wStream* s,
853 WINPR_ASSERT(context);
854 WINPR_ASSERT(header);
857 WINPR_ASSERT(cliprdr);
859 WLog_Print(cliprdr->log, WLOG_DEBUG,
"CliprdrClientFormatDataResponse");
862 CB_FORMAT_DATA_RESPONSE,
863 .msgFlags = header->msgFlags,
864 .dataLen = header->dataLen },
865 .requestedFormatData =
nullptr };
867 UINT error = cliprdr_read_format_data_response(s, &formatDataResponse);
871 IFCALLRET(context->ClientFormatDataResponse, error, context, &formatDataResponse);
874 WLog_Print(cliprdr->log, WLOG_ERROR,
875 "ClientFormatDataResponse failed with error %" PRIu32
"!", error);
885static UINT cliprdr_server_receive_filecontents_request(CliprdrServerContext* context,
wStream* s,
888 WINPR_ASSERT(context);
889 WINPR_ASSERT(header);
892 WINPR_ASSERT(cliprdr);
894 WLog_Print(cliprdr->log, WLOG_DEBUG,
"CliprdrClientFileContentsRequest");
897 .msgFlags = header->msgFlags,
898 .dataLen = header->dataLen },
905 .haveClipDataId = FALSE,
908 UINT error = cliprdr_read_file_contents_request(s, &request);
912 if (!context->hasHugeFileSupport)
914 if (request.nPositionHigh > 0)
915 return ERROR_INVALID_DATA;
916 if ((UINT64)request.nPositionLow + request.cbRequested > UINT32_MAX)
917 return ERROR_INVALID_DATA;
919 IFCALLRET(context->ClientFileContentsRequest, error, context, &request);
922 WLog_Print(cliprdr->log, WLOG_ERROR,
923 "ClientFileContentsRequest failed with error %" PRIu32
"!", error);
933static UINT cliprdr_server_receive_filecontents_response(CliprdrServerContext* context,
wStream* s,
936 UINT error = CHANNEL_RC_OK;
938 WINPR_ASSERT(context);
939 WINPR_ASSERT(header);
942 WINPR_ASSERT(cliprdr);
944 WLog_Print(cliprdr->log, WLOG_DEBUG,
"CliprdrClientFileContentsResponse");
947 .msgFlags = header->msgFlags,
948 .dataLen = header->dataLen },
951 .requestedData =
nullptr };
953 if ((error = cliprdr_read_file_contents_response(s, &response)))
956 IFCALLRET(context->ClientFileContentsResponse, error, context, &response);
959 WLog_Print(cliprdr->log, WLOG_ERROR,
960 "ClientFileContentsResponse failed with error %" PRIu32
"!", error);
970static UINT cliprdr_server_receive_pdu(CliprdrServerContext* context,
wStream* s,
975 WINPR_ASSERT(context);
976 WINPR_ASSERT(header);
979 WINPR_ASSERT(cliprdr);
982 char buffer1[64] = WINPR_C_ARRAY_INIT;
983 char buffer2[64] = WINPR_C_ARRAY_INIT;
984 WLog_Print(cliprdr->log, WLOG_DEBUG,
985 "CliprdrServerReceivePdu: msgType: %s, msgFlags: %s dataLen: %" PRIu32
"",
986 CB_MSG_TYPE_STRING(header->msgType, buffer1,
sizeof(buffer1)),
987 CB_MSG_FLAGS_STRING(header->msgFlags, buffer2,
sizeof(buffer2)),
991 const size_t start = Stream_GetPosition(s);
992 switch (header->msgType)
995 error = cliprdr_server_receive_capabilities(context, s, header);
998 case CB_TEMP_DIRECTORY:
999 error = cliprdr_server_receive_temporary_directory(context, s, header);
1002 case CB_FORMAT_LIST:
1003 error = cliprdr_server_receive_format_list(context, s, header);
1006 case CB_FORMAT_LIST_RESPONSE:
1007 error = cliprdr_server_receive_format_list_response(context, s, header);
1010 case CB_LOCK_CLIPDATA:
1011 error = cliprdr_server_receive_lock_clipdata(context, s, header);
1014 case CB_UNLOCK_CLIPDATA:
1015 error = cliprdr_server_receive_unlock_clipdata(context, s, header);
1018 case CB_FORMAT_DATA_REQUEST:
1019 error = cliprdr_server_receive_format_data_request(context, s, header);
1022 case CB_FORMAT_DATA_RESPONSE:
1023 error = cliprdr_server_receive_format_data_response(context, s, header);
1026 case CB_FILECONTENTS_REQUEST:
1027 error = cliprdr_server_receive_filecontents_request(context, s, header);
1030 case CB_FILECONTENTS_RESPONSE:
1031 error = cliprdr_server_receive_filecontents_response(context, s, header);
1035 error = ERROR_INVALID_DATA;
1036 WLog_Print(cliprdr->log, WLOG_ERROR,
"Unexpected clipboard PDU type: %" PRIu16
"",
1041 const size_t end = Stream_GetPosition(s);
1042 const size_t diff = end - start;
1043 if (diff != header->dataLen)
1045 char buffer1[64] = WINPR_C_ARRAY_INIT;
1047 TAG,
"[%s] Mismatch: header told length is %" PRIu32
", but actually read %" PRIuz,
1048 CB_MSG_TYPE_STRING(header->msgType, buffer1,
sizeof(buffer1)), header->dataLen, diff);
1051 const size_t rem = Stream_GetRemainingLength(s);
1054 char buffer1[64] = WINPR_C_ARRAY_INIT;
1055 char buffer2[64] = WINPR_C_ARRAY_INIT;
1057 TAG,
"msgType: %s, msgFlags: %s dataLen: %" PRIu32
": %" PRIuz
" bytes not parsed",
1058 CB_MSG_TYPE_STRING(header->msgType, buffer1,
sizeof(buffer1)),
1059 CB_MSG_FLAGS_STRING(header->msgFlags, buffer2,
sizeof(buffer2)), header->dataLen, rem);
1069static UINT cliprdr_server_init(CliprdrServerContext* context)
1071 UINT32 generalFlags = 0;
1075 WINPR_ASSERT(context);
1078 WINPR_ASSERT(cliprdr);
1080 monitorReady.common.msgType = CB_MONITOR_READY;
1082 if (context->useLongFormatNames)
1083 generalFlags |= CB_USE_LONG_FORMAT_NAMES;
1085 if (context->streamFileClipEnabled)
1086 generalFlags |= CB_STREAM_FILECLIP_ENABLED;
1088 if (context->fileClipNoFilePaths)
1089 generalFlags |= CB_FILECLIP_NO_FILE_PATHS;
1091 if (context->canLockClipData)
1092 generalFlags |= CB_CAN_LOCK_CLIPDATA;
1094 if (context->hasHugeFileSupport)
1095 generalFlags |= CB_HUGE_FILE_SUPPORT_ENABLED;
1098 CB_CAPSTYPE_GENERAL,
1099 .capabilitySetLength =
1100 CB_CAPSTYPE_GENERAL_LEN,
1101 .version = CB_CAPS_VERSION_2,
1102 .generalFlags = generalFlags };
1106 .dataLen = 4 + CB_CAPSTYPE_GENERAL_LEN },
1107 .cCapabilitiesSets = 1,
1111 if ((error = context->ServerCapabilities(context, &capabilities)))
1113 WLog_Print(cliprdr->log, WLOG_ERROR,
"ServerCapabilities failed with error %" PRIu32
"!",
1118 if ((error = context->MonitorReady(context, &monitorReady)))
1120 WLog_Print(cliprdr->log, WLOG_ERROR,
"MonitorReady failed with error %" PRIu32
"!", error);
1132static UINT cliprdr_server_read(CliprdrServerContext* context)
1134 WINPR_ASSERT(context);
1137 WINPR_ASSERT(cliprdr);
1141 const size_t spos = Stream_GetPosition(s);
1142 WINPR_STATIC_ASSERT(CLIPRDR_HEADER_LENGTH ==
sizeof(
CLIPRDR_HEADER));
1147 if (noHeader || (spos < cliprdr->totalExpectedBytes))
1149 DWORD BytesReturned = 0;
1150 const size_t BytesToRead =
1151 (noHeader ?
sizeof(
CLIPRDR_HEADER) : cliprdr->totalExpectedBytes) - spos;
1152 const DWORD status = WaitForSingleObject(cliprdr->ChannelEvent, 0);
1154 if (status == WAIT_FAILED)
1156 const UINT error = GetLastError();
1157 WLog_Print(cliprdr->log, WLOG_ERROR,
1158 "WaitForSingleObject failed with error %" PRIu32
"", error);
1162 if (status == WAIT_TIMEOUT)
1163 return CHANNEL_RC_OK;
1165 if (!WTSVirtualChannelRead(cliprdr->ChannelHandle, 0, Stream_Pointer(s),
1166 WINPR_ASSERTING_INT_CAST(DWORD, BytesToRead), &BytesReturned))
1168 WLog_Print(cliprdr->log, WLOG_ERROR,
"WTSVirtualChannelRead failed!");
1169 return ERROR_INTERNAL_ERROR;
1172 if (!Stream_SafeSeek(s, BytesReturned))
1173 return ERROR_INTERNAL_ERROR;
1177 const size_t epos = Stream_GetPosition(s);
1178 if ((epos <
sizeof(
CLIPRDR_HEADER)) || (epos < cliprdr->totalExpectedBytes))
1179 return CHANNEL_RC_OK;
1182 if (cliprdr->totalExpectedBytes == 0)
1184 const size_t position = Stream_GetPosition(s);
1185 Stream_ResetPosition(s);
1186 Stream_Read_UINT16(s, cliprdr->header.msgType);
1187 Stream_Read_UINT16(s, cliprdr->header.msgFlags);
1188 Stream_Read_UINT32(s, cliprdr->header.dataLen);
1190 if (!Stream_EnsureRemainingCapacity(s, cliprdr->header.dataLen))
1192 WLog_Print(cliprdr->log, WLOG_ERROR,
"Stream_EnsureCapacity failed!");
1193 return CHANNEL_RC_NO_MEMORY;
1196 if (!Stream_SetPosition(s, position))
1197 return ERROR_INVALID_DATA;
1199 cliprdr->totalExpectedBytes = cliprdr->header.dataLen + CLIPRDR_HEADER_LENGTH;
1204 if (epos > cliprdr->totalExpectedBytes)
1206 char buffer1[128] = WINPR_C_ARRAY_INIT;
1207 char buffer2[128] = WINPR_C_ARRAY_INIT;
1208 WLog_Print(cliprdr->log, WLOG_WARN,
1209 "Received excess bytes for [%s|%s]: Expected %" PRIuz
", but got %" PRIuz,
1210 CB_MSG_TYPE_STRING(cliprdr->header.msgType, buffer1,
sizeof(buffer1)),
1211 CB_MSG_FLAGS_STRING(cliprdr->header.msgFlags, buffer2,
sizeof(buffer2)),
1212 cliprdr->totalExpectedBytes, epos);
1214 Stream_SealLength(s);
1215 if (!Stream_SetPosition(s, CLIPRDR_HEADER_LENGTH))
1216 return ERROR_INVALID_DATA;
1218 const UINT error = cliprdr_server_receive_pdu(context, s, &cliprdr->header);
1219 cliprdr->totalExpectedBytes = 0;
1222 cliprdr->header = empty;
1225 char buffer1[128] = WINPR_C_ARRAY_INIT;
1226 char buffer2[128] = WINPR_C_ARRAY_INIT;
1227 WLog_Print(cliprdr->log, WLOG_ERROR,
1228 "cliprdr_server_receive_pdu [%s|%s] failed with error code %" PRIu32
"!",
1229 CB_MSG_TYPE_STRING(cliprdr->header.msgType, buffer1,
sizeof(buffer1)),
1230 CB_MSG_FLAGS_STRING(cliprdr->header.msgFlags, buffer2,
sizeof(buffer2)),
1236 return CHANNEL_RC_OK;
1239static DWORD WINAPI cliprdr_server_thread(LPVOID arg)
1243 HANDLE events[MAXIMUM_WAIT_OBJECTS] = WINPR_C_ARRAY_INIT;
1244 CliprdrServerContext* context = (CliprdrServerContext*)arg;
1245 UINT error = CHANNEL_RC_OK;
1247 WINPR_ASSERT(context);
1250 WINPR_ASSERT(cliprdr);
1252 HANDLE ChannelEvent = context->GetEventHandle(context);
1254 events[nCount++] = cliprdr->StopEvent;
1255 events[nCount++] = ChannelEvent;
1257 if (context->autoInitializationSequence)
1259 if ((error = cliprdr_server_init(context)))
1261 WLog_Print(cliprdr->log, WLOG_ERROR,
1262 "cliprdr_server_init failed with error %" PRIu32
"!", error);
1269 status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE);
1271 if (status == WAIT_FAILED)
1273 error = GetLastError();
1274 WLog_Print(cliprdr->log, WLOG_ERROR,
1275 "WaitForMultipleObjects failed with error %" PRIu32
"", error);
1279 status = WaitForSingleObject(cliprdr->StopEvent, 0);
1281 if (status == WAIT_FAILED)
1283 error = GetLastError();
1284 WLog_Print(cliprdr->log, WLOG_ERROR,
1285 "WaitForSingleObject failed with error %" PRIu32
"", error);
1289 if (status == WAIT_OBJECT_0)
1292 status = WaitForSingleObject(ChannelEvent, 0);
1294 if (status == WAIT_FAILED)
1296 error = GetLastError();
1297 WLog_Print(cliprdr->log, WLOG_ERROR,
1298 "WaitForSingleObject failed with error %" PRIu32
"", error);
1302 if (status == WAIT_OBJECT_0)
1304 if ((error = context->CheckEventHandle(context)))
1306 WLog_Print(cliprdr->log, WLOG_ERROR,
1307 "CheckEventHandle failed with error %" PRIu32
"!", error);
1315 if (error && context->rdpcontext)
1316 setChannelError(context->rdpcontext, error,
"cliprdr_server_thread reported an error");
1327static UINT cliprdr_server_open(CliprdrServerContext* context)
1329 void* buffer =
nullptr;
1330 DWORD BytesReturned = 0;
1332 WINPR_ASSERT(context);
1335 WINPR_ASSERT(cliprdr);
1337 cliprdr->ChannelHandle =
1338 WTSVirtualChannelOpen(cliprdr->vcm, WTS_CURRENT_SESSION, CLIPRDR_SVC_CHANNEL_NAME);
1340 if (!cliprdr->ChannelHandle)
1342 WLog_Print(cliprdr->log, WLOG_ERROR,
"WTSVirtualChannelOpen for cliprdr failed!");
1343 return ERROR_INTERNAL_ERROR;
1346 cliprdr->ChannelEvent =
nullptr;
1348 if (WTSVirtualChannelQuery(cliprdr->ChannelHandle, WTSVirtualEventHandle, &buffer,
1351 if (BytesReturned !=
sizeof(HANDLE))
1353 WLog_Print(cliprdr->log, WLOG_ERROR,
"BytesReturned has not size of HANDLE!");
1354 return ERROR_INTERNAL_ERROR;
1357 cliprdr->ChannelEvent = *(HANDLE*)buffer;
1358 WTSFreeMemory(buffer);
1361 if (!cliprdr->ChannelEvent)
1363 WLog_Print(cliprdr->log, WLOG_ERROR,
"WTSVirtualChannelQuery for cliprdr failed!");
1364 return ERROR_INTERNAL_ERROR;
1367 return CHANNEL_RC_OK;
1375static UINT cliprdr_server_close(CliprdrServerContext* context)
1377 WINPR_ASSERT(context);
1380 WINPR_ASSERT(cliprdr);
1382 if (cliprdr->ChannelHandle)
1384 (void)WTSVirtualChannelClose(cliprdr->ChannelHandle);
1385 cliprdr->ChannelHandle =
nullptr;
1388 return CHANNEL_RC_OK;
1396static UINT cliprdr_server_start(CliprdrServerContext* context)
1400 WINPR_ASSERT(context);
1403 WINPR_ASSERT(cliprdr);
1405 if (!cliprdr->ChannelHandle)
1407 error = context->Open(context);
1410 WLog_Print(cliprdr->log, WLOG_ERROR,
"Open failed!");
1415 cliprdr->StopEvent = CreateEvent(
nullptr, TRUE, FALSE,
nullptr);
1416 if (!cliprdr->StopEvent)
1418 WLog_Print(cliprdr->log, WLOG_ERROR,
"CreateEvent failed!");
1419 return ERROR_INTERNAL_ERROR;
1422 cliprdr->Thread = CreateThread(
nullptr, 0, cliprdr_server_thread, (
void*)context, 0,
nullptr);
1423 if (!cliprdr->Thread)
1425 WLog_Print(cliprdr->log, WLOG_ERROR,
"CreateThread failed!");
1426 (void)CloseHandle(cliprdr->StopEvent);
1427 cliprdr->StopEvent =
nullptr;
1428 return ERROR_INTERNAL_ERROR;
1431 return CHANNEL_RC_OK;
1439static UINT cliprdr_server_stop(CliprdrServerContext* context)
1441 UINT error = CHANNEL_RC_OK;
1443 WINPR_ASSERT(context);
1446 WINPR_ASSERT(cliprdr);
1448 if (cliprdr->StopEvent)
1450 (void)SetEvent(cliprdr->StopEvent);
1452 if (WaitForSingleObject(cliprdr->Thread, INFINITE) == WAIT_FAILED)
1454 error = GetLastError();
1455 WLog_Print(cliprdr->log, WLOG_ERROR,
1456 "WaitForSingleObject failed with error %" PRIu32
"", error);
1460 (void)CloseHandle(cliprdr->Thread);
1461 (void)CloseHandle(cliprdr->StopEvent);
1464 if (cliprdr->ChannelHandle)
1465 return context->Close(context);
1470static HANDLE cliprdr_server_get_event_handle(CliprdrServerContext* context)
1472 WINPR_ASSERT(context);
1475 WINPR_ASSERT(cliprdr);
1476 return cliprdr->ChannelEvent;
1484static UINT cliprdr_server_check_event_handle(CliprdrServerContext* context)
1486 return cliprdr_server_read(context);
1489CliprdrServerContext* cliprdr_server_context_new(HANDLE vcm)
1491 CliprdrServerContext* context = (CliprdrServerContext*)calloc(1,
sizeof(CliprdrServerContext));
1496 context->autoInitializationSequence = TRUE;
1497 context->Open = cliprdr_server_open;
1498 context->Close = cliprdr_server_close;
1499 context->Start = cliprdr_server_start;
1500 context->Stop = cliprdr_server_stop;
1501 context->GetEventHandle = cliprdr_server_get_event_handle;
1502 context->CheckEventHandle = cliprdr_server_check_event_handle;
1503 context->ServerCapabilities = cliprdr_server_capabilities;
1504 context->MonitorReady = cliprdr_server_monitor_ready;
1505 context->ServerFormatList = cliprdr_server_format_list;
1506 context->ServerFormatListResponse = cliprdr_server_format_list_response;
1507 context->ServerLockClipboardData = cliprdr_server_lock_clipboard_data;
1508 context->ServerUnlockClipboardData = cliprdr_server_unlock_clipboard_data;
1509 context->ServerFormatDataRequest = cliprdr_server_format_data_request;
1510 context->ServerFormatDataResponse = cliprdr_server_format_data_response;
1511 context->ServerFileContentsRequest = cliprdr_server_file_contents_request;
1512 context->ServerFileContentsResponse = cliprdr_server_file_contents_response;
1519 cliprdr->log = WLog_Get(TAG);
1521 cliprdr->s = Stream_New(
nullptr, 4096);
1529 cliprdr_server_context_free(context);
1533void cliprdr_server_context_free(CliprdrServerContext* context)
1541 Stream_Free(cliprdr->s, TRUE);
1543 free(context->handle);