Blame channels/cliprdr/client/cliprdr_main.c

Packit 1fb8d4
/**
Packit 1fb8d4
 * FreeRDP: A Remote Desktop Protocol Implementation
Packit 1fb8d4
 * Clipboard Virtual Channel
Packit 1fb8d4
 *
Packit 1fb8d4
 * Copyright 2009-2011 Jay Sorg
Packit 1fb8d4
 * Copyright 2010-2011 Vic Lee
Packit 1fb8d4
 * Copyright 2015 Thincast Technologies GmbH
Packit 1fb8d4
 * Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
Packit 1fb8d4
 *
Packit 1fb8d4
 * Licensed under the Apache License, Version 2.0 (the "License");
Packit 1fb8d4
 * you may not use this file except in compliance with the License.
Packit 1fb8d4
 * You may obtain a copy of the License at
Packit 1fb8d4
 *
Packit 1fb8d4
 *     http://www.apache.org/licenses/LICENSE-2.0
Packit 1fb8d4
 *
Packit 1fb8d4
 * Unless required by applicable law or agreed to in writing, software
Packit 1fb8d4
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit 1fb8d4
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit 1fb8d4
 * See the License for the specific language governing permissions and
Packit 1fb8d4
 * limitations under the License.
Packit 1fb8d4
 */
Packit 1fb8d4
Packit 1fb8d4
#ifdef HAVE_CONFIG_H
Packit 1fb8d4
#include "config.h"
Packit 1fb8d4
#endif
Packit 1fb8d4
Packit 1fb8d4
#include <winpr/crt.h>
Packit 1fb8d4
#include <winpr/print.h>
Packit 1fb8d4
Packit 1fb8d4
#include <freerdp/types.h>
Packit 1fb8d4
#include <freerdp/constants.h>
Packit 1fb8d4
#include <freerdp/client/cliprdr.h>
Packit 1fb8d4
Packit 1fb8d4
#include "cliprdr_main.h"
Packit 1fb8d4
#include "cliprdr_format.h"
Packit 1fb8d4
Packit 1fb8d4
#ifdef WITH_DEBUG_CLIPRDR
Packit 1fb8d4
static const char* const CB_MSG_TYPE_STRINGS[] =
Packit 1fb8d4
{
Packit 1fb8d4
	"",
Packit 1fb8d4
	"CB_MONITOR_READY",
Packit 1fb8d4
	"CB_FORMAT_LIST",
Packit 1fb8d4
	"CB_FORMAT_LIST_RESPONSE",
Packit 1fb8d4
	"CB_FORMAT_DATA_REQUEST",
Packit 1fb8d4
	"CB_FORMAT_DATA_RESPONSE",
Packit 1fb8d4
	"CB_TEMP_DIRECTORY",
Packit 1fb8d4
	"CB_CLIP_CAPS",
Packit 1fb8d4
	"CB_FILECONTENTS_REQUEST",
Packit 1fb8d4
	"CB_FILECONTENTS_RESPONSE",
Packit 1fb8d4
	"CB_LOCK_CLIPDATA",
Packit 1fb8d4
	"CB_UNLOCK_CLIPDATA"
Packit 1fb8d4
};
Packit 1fb8d4
#endif
Packit 1fb8d4
Packit 1fb8d4
CliprdrClientContext* cliprdr_get_client_interface(cliprdrPlugin* cliprdr)
Packit 1fb8d4
{
Packit 1fb8d4
	CliprdrClientContext* pInterface;
Packit 1fb8d4
Packit 1fb8d4
	if (!cliprdr)
Packit 1fb8d4
		return NULL;
Packit 1fb8d4
Packit 1fb8d4
	pInterface = (CliprdrClientContext*) cliprdr->channelEntryPoints.pInterface;
Packit 1fb8d4
	return pInterface;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static wStream* cliprdr_packet_new(UINT16 msgType, UINT16 msgFlags,
Packit 1fb8d4
                                   UINT32 dataLen)
Packit 1fb8d4
{
Packit 1fb8d4
	wStream* s;
Packit 1fb8d4
	s = Stream_New(NULL, dataLen + 8);
Packit 1fb8d4
Packit 1fb8d4
	if (!s)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "Stream_New failed!");
Packit 1fb8d4
		return NULL;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	Stream_Write_UINT16(s, msgType);
Packit 1fb8d4
	Stream_Write_UINT16(s, msgFlags);
Packit 1fb8d4
	/* Write actual length after the entire packet has been constructed. */
Packit 1fb8d4
	Stream_Seek(s, 4);
Packit 1fb8d4
	return s;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT cliprdr_packet_send(cliprdrPlugin* cliprdr, wStream* s)
Packit 1fb8d4
{
Packit 1fb8d4
	size_t pos;
Packit 1fb8d4
	UINT32 dataLen;
Packit 1fb8d4
	UINT status = CHANNEL_RC_OK;
Packit 1fb8d4
	pos = Stream_GetPosition(s);
Packit 1fb8d4
	dataLen = pos - 8;
Packit 1fb8d4
	Stream_SetPosition(s, 4);
Packit 1fb8d4
	Stream_Write_UINT32(s, dataLen);
Packit 1fb8d4
	Stream_SetPosition(s, pos);
Packit 1fb8d4
#ifdef WITH_DEBUG_CLIPRDR
Packit 1fb8d4
	WLog_DBG(TAG, "Cliprdr Sending (%"PRIu32" bytes)", dataLen + 8);
Packit 1fb8d4
	winpr_HexDump(TAG, WLOG_DEBUG, Stream_Buffer(s), dataLen + 8);
Packit 1fb8d4
#endif
Packit 1fb8d4
Packit 1fb8d4
	if (!cliprdr)
Packit 1fb8d4
	{
Packit 1fb8d4
		status = CHANNEL_RC_BAD_INIT_HANDLE;
Packit 1fb8d4
	}
Packit 1fb8d4
	else
Packit 1fb8d4
	{
Packit 1fb8d4
		status = cliprdr->channelEntryPoints.pVirtualChannelWriteEx(cliprdr->InitHandle,
Packit 1fb8d4
		         cliprdr->OpenHandle,
Packit 1fb8d4
		         Stream_Buffer(s), (UINT32) Stream_GetPosition(s), s);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (status != CHANNEL_RC_OK)
Packit 1fb8d4
		WLog_ERR(TAG, "VirtualChannelWrite failed with %s [%08"PRIX32"]",
Packit 1fb8d4
		         WTSErrorToString(status), status);
Packit 1fb8d4
Packit 1fb8d4
	return status;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
#ifdef WITH_DEBUG_CLIPRDR
Packit 1fb8d4
static void cliprdr_print_general_capability_flags(UINT32 flags)
Packit 1fb8d4
{
Packit 1fb8d4
	WLog_INFO(TAG,  "generalFlags (0x%08"PRIX32") {", flags);
Packit 1fb8d4
Packit 1fb8d4
	if (flags & CB_USE_LONG_FORMAT_NAMES)
Packit 1fb8d4
		WLog_INFO(TAG,  "\tCB_USE_LONG_FORMAT_NAMES");
Packit 1fb8d4
Packit 1fb8d4
	if (flags & CB_STREAM_FILECLIP_ENABLED)
Packit 1fb8d4
		WLog_INFO(TAG,  "\tCB_STREAM_FILECLIP_ENABLED");
Packit 1fb8d4
Packit 1fb8d4
	if (flags & CB_FILECLIP_NO_FILE_PATHS)
Packit 1fb8d4
		WLog_INFO(TAG,  "\tCB_FILECLIP_NO_FILE_PATHS");
Packit 1fb8d4
Packit 1fb8d4
	if (flags & CB_CAN_LOCK_CLIPDATA)
Packit 1fb8d4
		WLog_INFO(TAG,  "\tCB_CAN_LOCK_CLIPDATA");
Packit 1fb8d4
Packit 1fb8d4
	WLog_INFO(TAG,  "}");
Packit 1fb8d4
}
Packit 1fb8d4
#endif
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT cliprdr_process_general_capability(cliprdrPlugin* cliprdr,
Packit 1fb8d4
        wStream* s)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT32 version;
Packit 1fb8d4
	UINT32 generalFlags;
Packit 1fb8d4
	CLIPRDR_CAPABILITIES capabilities;
Packit 1fb8d4
	CLIPRDR_GENERAL_CAPABILITY_SET generalCapabilitySet;
Packit 1fb8d4
	CliprdrClientContext* context = cliprdr_get_client_interface(cliprdr);
Packit 1fb8d4
	UINT error = CHANNEL_RC_OK;
Packit 1fb8d4
Packit 1fb8d4
	if (!context)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "cliprdr_get_client_interface failed!");
Packit 1fb8d4
		return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) < 8)
Packit 1fb8d4
		return ERROR_INVALID_DATA;
Packit 1fb8d4
Packit 1fb8d4
	Stream_Read_UINT32(s, version); /* version (4 bytes) */
Packit 1fb8d4
	Stream_Read_UINT32(s, generalFlags); /* generalFlags (4 bytes) */
Packit 1fb8d4
	DEBUG_CLIPRDR("Version: %"PRIu32"", version);
Packit 1fb8d4
#ifdef WITH_DEBUG_CLIPRDR
Packit 1fb8d4
	cliprdr_print_general_capability_flags(generalFlags);
Packit 1fb8d4
#endif
Packit 1fb8d4
Packit 1fb8d4
	if (cliprdr->useLongFormatNames)
Packit 1fb8d4
		cliprdr->useLongFormatNames = (generalFlags & CB_USE_LONG_FORMAT_NAMES) ? TRUE :
Packit 1fb8d4
		                              FALSE;
Packit 1fb8d4
Packit 1fb8d4
	if (cliprdr->streamFileClipEnabled)
Packit 1fb8d4
		cliprdr->streamFileClipEnabled = (generalFlags & CB_STREAM_FILECLIP_ENABLED) ?
Packit 1fb8d4
		                                 TRUE : FALSE;
Packit 1fb8d4
Packit 1fb8d4
	if (cliprdr->fileClipNoFilePaths)
Packit 1fb8d4
		cliprdr->fileClipNoFilePaths = (generalFlags & CB_FILECLIP_NO_FILE_PATHS) ?
Packit 1fb8d4
		                               TRUE : FALSE;
Packit 1fb8d4
Packit 1fb8d4
	if (cliprdr->canLockClipData)
Packit 1fb8d4
		cliprdr->canLockClipData = (generalFlags & CB_CAN_LOCK_CLIPDATA) ? TRUE : FALSE;
Packit 1fb8d4
Packit 1fb8d4
	cliprdr->capabilitiesReceived = TRUE;
Packit 1fb8d4
Packit 1fb8d4
	if (!context->custom)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "context->custom not set!");
Packit 1fb8d4
		return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	capabilities.cCapabilitiesSets = 1;
Packit 1fb8d4
	capabilities.capabilitySets = (CLIPRDR_CAPABILITY_SET*) &
Packit 1fb8d4
	                              (generalCapabilitySet);
Packit 1fb8d4
	generalCapabilitySet.capabilitySetType = CB_CAPSTYPE_GENERAL;
Packit 1fb8d4
	generalCapabilitySet.capabilitySetLength = 12;
Packit 1fb8d4
	generalCapabilitySet.version = version;
Packit 1fb8d4
	generalCapabilitySet.generalFlags = generalFlags;
Packit 1fb8d4
	IFCALLRET(context->ServerCapabilities, error, context, &capabilities);
Packit 1fb8d4
Packit 1fb8d4
	if (error)
Packit 1fb8d4
		WLog_ERR(TAG, "ServerCapabilities failed with error %"PRIu32"!", error);
Packit 1fb8d4
Packit 1fb8d4
	return error;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT cliprdr_process_clip_caps(cliprdrPlugin* cliprdr, wStream* s,
Packit 1fb8d4
                                      UINT16 length, UINT16 flags)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT16 index;
Packit 1fb8d4
	UINT16 lengthCapability;
Packit 1fb8d4
	UINT16 cCapabilitiesSets;
Packit 1fb8d4
	UINT16 capabilitySetType;
Packit 1fb8d4
	UINT error = CHANNEL_RC_OK;
Packit 1fb8d4
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) < 4)
Packit 1fb8d4
		return ERROR_INVALID_DATA;
Packit 1fb8d4
Packit 1fb8d4
	Stream_Read_UINT16(s, cCapabilitiesSets); /* cCapabilitiesSets (2 bytes) */
Packit 1fb8d4
	Stream_Seek_UINT16(s); /* pad1 (2 bytes) */
Packit 1fb8d4
	WLog_Print(cliprdr->log, WLOG_DEBUG, "ServerCapabilities");
Packit 1fb8d4
Packit 1fb8d4
	for (index = 0; index < cCapabilitiesSets; index++)
Packit 1fb8d4
	{
Packit 1fb8d4
		if (Stream_GetRemainingLength(s) < 4)
Packit 1fb8d4
			return ERROR_INVALID_DATA;
Packit 1fb8d4
Packit 1fb8d4
		Stream_Read_UINT16(s, capabilitySetType); /* capabilitySetType (2 bytes) */
Packit 1fb8d4
		Stream_Read_UINT16(s, lengthCapability); /* lengthCapability (2 bytes) */
Packit 1fb8d4
Packit 1fb8d4
		if (lengthCapability < 4 || Stream_GetRemainingLength(s) < lengthCapability - 4)
Packit 1fb8d4
			return ERROR_INVALID_DATA;
Packit 1fb8d4
Packit 1fb8d4
		switch (capabilitySetType)
Packit 1fb8d4
		{
Packit 1fb8d4
			case CB_CAPSTYPE_GENERAL:
Packit 1fb8d4
				if ((error = cliprdr_process_general_capability(cliprdr, s)))
Packit 1fb8d4
				{
Packit 1fb8d4
					WLog_ERR(TAG, "cliprdr_process_general_capability failed with error %"PRIu32"!",
Packit 1fb8d4
					         error);
Packit 1fb8d4
					return error;
Packit 1fb8d4
				}
Packit 1fb8d4
Packit 1fb8d4
				break;
Packit 1fb8d4
Packit 1fb8d4
			default:
Packit 1fb8d4
				WLog_ERR(TAG, "unknown cliprdr capability set: %"PRIu16"", capabilitySetType);
Packit 1fb8d4
				return CHANNEL_RC_BAD_PROC;
Packit 1fb8d4
				break;
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return error;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT cliprdr_process_monitor_ready(cliprdrPlugin* cliprdr, wStream* s,
Packit 1fb8d4
        UINT16 length, UINT16 flags)
Packit 1fb8d4
{
Packit 1fb8d4
	CLIPRDR_MONITOR_READY monitorReady;
Packit 1fb8d4
	CliprdrClientContext* context = cliprdr_get_client_interface(cliprdr);
Packit 1fb8d4
	UINT error = CHANNEL_RC_OK;
Packit 1fb8d4
	WLog_Print(cliprdr->log, WLOG_DEBUG, "MonitorReady");
Packit 1fb8d4
Packit 1fb8d4
	if (!context->custom)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "context->custom not set!");
Packit 1fb8d4
		return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (!cliprdr->capabilitiesReceived)
Packit 1fb8d4
	{
Packit 1fb8d4
		/**
Packit 1fb8d4
		  * The clipboard capabilities pdu from server to client is optional,
Packit 1fb8d4
		  * but a server using it must send it before sending the monitor ready pdu.
Packit 1fb8d4
		  * When the server capabilities pdu is not used, default capabilities
Packit 1fb8d4
		  * corresponding to a generalFlags field set to zero are assumed.
Packit 1fb8d4
		  */
Packit 1fb8d4
		cliprdr->useLongFormatNames = FALSE;
Packit 1fb8d4
		cliprdr->streamFileClipEnabled = FALSE;
Packit 1fb8d4
		cliprdr->fileClipNoFilePaths = TRUE;
Packit 1fb8d4
		cliprdr->canLockClipData = FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	monitorReady.msgType = CB_MONITOR_READY;
Packit 1fb8d4
	monitorReady.msgFlags = flags;
Packit 1fb8d4
	monitorReady.dataLen = length;
Packit 1fb8d4
	IFCALLRET(context->MonitorReady, error, context, &monitorReady);
Packit 1fb8d4
Packit 1fb8d4
	if (error)
Packit 1fb8d4
		WLog_ERR(TAG, "MonitorReady failed with error %"PRIu32"!", error);
Packit 1fb8d4
Packit 1fb8d4
	return error;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT cliprdr_process_filecontents_request(cliprdrPlugin* cliprdr,
Packit 1fb8d4
        wStream* s, UINT32 length, UINT16 flags)
Packit 1fb8d4
{
Packit 1fb8d4
	CLIPRDR_FILE_CONTENTS_REQUEST request;
Packit 1fb8d4
	CliprdrClientContext* context = cliprdr_get_client_interface(cliprdr);
Packit 1fb8d4
	UINT error = CHANNEL_RC_OK;
Packit 1fb8d4
	WLog_Print(cliprdr->log, WLOG_DEBUG, "FileContentsRequest");
Packit 1fb8d4
Packit 1fb8d4
	if (!context->custom)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "context->custom not set!");
Packit 1fb8d4
		return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) < 24)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "not enough remaining data");
Packit 1fb8d4
		return ERROR_INVALID_DATA;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	request.msgType = CB_FILECONTENTS_REQUEST;
Packit 1fb8d4
	request.msgFlags = flags;
Packit 1fb8d4
	request.dataLen = length;
Packit 1fb8d4
	request.haveClipDataId = FALSE;
Packit 1fb8d4
	Stream_Read_UINT32(s, request.streamId); /* streamId (4 bytes) */
Packit 1fb8d4
	Stream_Read_UINT32(s, request.listIndex); /* listIndex (4 bytes) */
Packit 1fb8d4
	Stream_Read_UINT32(s, request.dwFlags); /* dwFlags (4 bytes) */
Packit 1fb8d4
	Stream_Read_UINT32(s, request.nPositionLow); /* nPositionLow (4 bytes) */
Packit 1fb8d4
	Stream_Read_UINT32(s, request.nPositionHigh); /* nPositionHigh (4 bytes) */
Packit 1fb8d4
	Stream_Read_UINT32(s, request.cbRequested); /* cbRequested (4 bytes) */
Packit 1fb8d4
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) >= 4)
Packit 1fb8d4
	{
Packit 1fb8d4
		Stream_Read_UINT32(s, request.clipDataId); /* clipDataId (4 bytes) */
Packit 1fb8d4
		request.haveClipDataId = TRUE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	IFCALLRET(context->ServerFileContentsRequest, error, context, &request);
Packit 1fb8d4
Packit 1fb8d4
	if (error)
Packit 1fb8d4
		WLog_ERR(TAG, "ServerFileContentsRequest failed with error %"PRIu32"!", error);
Packit 1fb8d4
Packit 1fb8d4
	return error;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT cliprdr_process_filecontents_response(cliprdrPlugin* cliprdr,
Packit 1fb8d4
        wStream* s, UINT32 length, UINT16 flags)
Packit 1fb8d4
{
Packit 1fb8d4
	CLIPRDR_FILE_CONTENTS_RESPONSE response;
Packit 1fb8d4
	CliprdrClientContext* context = cliprdr_get_client_interface(cliprdr);
Packit 1fb8d4
	UINT error = CHANNEL_RC_OK;
Packit 1fb8d4
	WLog_Print(cliprdr->log, WLOG_DEBUG, "FileContentsResponse");
Packit 1fb8d4
Packit 1fb8d4
	if (!context->custom)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "context->custom not set!");
Packit 1fb8d4
		return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) < 4)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "not enough remaining data");
Packit 1fb8d4
		return ERROR_INVALID_DATA;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	response.msgType = CB_FILECONTENTS_RESPONSE;
Packit 1fb8d4
	response.msgFlags = flags;
Packit 1fb8d4
	response.dataLen = length;
Packit 1fb8d4
	Stream_Read_UINT32(s, response.streamId); /* streamId (4 bytes) */
Packit 1fb8d4
	response.cbRequested = length - 4;
Packit 1fb8d4
	response.requestedData = Stream_Pointer(s); /* requestedFileContentsData */
Packit 1fb8d4
	IFCALLRET(context->ServerFileContentsResponse, error, context, &response);
Packit 1fb8d4
Packit 1fb8d4
	if (error)
Packit 1fb8d4
		WLog_ERR(TAG, "ServerFileContentsResponse failed with error %"PRIu32"!", error);
Packit 1fb8d4
Packit 1fb8d4
	return error;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT cliprdr_process_lock_clipdata(cliprdrPlugin* cliprdr, wStream* s,
Packit 1fb8d4
        UINT32 length, UINT16 flags)
Packit 1fb8d4
{
Packit 1fb8d4
	CLIPRDR_LOCK_CLIPBOARD_DATA lockClipboardData;
Packit 1fb8d4
	CliprdrClientContext* context = cliprdr_get_client_interface(cliprdr);
Packit 1fb8d4
	UINT error = CHANNEL_RC_OK;
Packit 1fb8d4
	WLog_Print(cliprdr->log, WLOG_DEBUG, "LockClipData");
Packit 1fb8d4
Packit 1fb8d4
	if (!context->custom)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "context->custom not set!");
Packit 1fb8d4
		return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) < 4)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "not enough remaining data");
Packit 1fb8d4
		return ERROR_INVALID_DATA;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	lockClipboardData.msgType = CB_LOCK_CLIPDATA;
Packit 1fb8d4
	lockClipboardData.msgFlags = flags;
Packit 1fb8d4
	lockClipboardData.dataLen = length;
Packit 1fb8d4
	Stream_Read_UINT32(s, lockClipboardData.clipDataId); /* clipDataId (4 bytes) */
Packit 1fb8d4
	IFCALLRET(context->ServerLockClipboardData, error, context, &lockClipboardData);
Packit 1fb8d4
Packit 1fb8d4
	if (error)
Packit 1fb8d4
		WLog_ERR(TAG, "ServerLockClipboardData failed with error %"PRIu32"!", error);
Packit 1fb8d4
Packit 1fb8d4
	return error;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT cliprdr_process_unlock_clipdata(cliprdrPlugin* cliprdr, wStream* s,
Packit 1fb8d4
        UINT32 length, UINT16 flags)
Packit 1fb8d4
{
Packit 1fb8d4
	CLIPRDR_UNLOCK_CLIPBOARD_DATA unlockClipboardData;
Packit 1fb8d4
	CliprdrClientContext* context = cliprdr_get_client_interface(cliprdr);
Packit 1fb8d4
	UINT error = CHANNEL_RC_OK;
Packit 1fb8d4
	WLog_Print(cliprdr->log, WLOG_DEBUG, "UnlockClipData");
Packit 1fb8d4
Packit 1fb8d4
	if (!context->custom)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "context->custom not set!");
Packit 1fb8d4
		return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) < 4)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "not enough remaining data");
Packit 1fb8d4
		return ERROR_INVALID_DATA;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	unlockClipboardData.msgType = CB_UNLOCK_CLIPDATA;
Packit 1fb8d4
	unlockClipboardData.msgFlags = flags;
Packit 1fb8d4
	unlockClipboardData.dataLen = length;
Packit 1fb8d4
	Stream_Read_UINT32(s, unlockClipboardData.clipDataId); /* clipDataId (4 bytes) */
Packit 1fb8d4
	IFCALLRET(context->ServerUnlockClipboardData, error, context,
Packit 1fb8d4
	          &unlockClipboardData);
Packit 1fb8d4
Packit 1fb8d4
	if (error)
Packit 1fb8d4
		WLog_ERR(TAG, "ServerUnlockClipboardData failed with error %"PRIu32"!", error);
Packit 1fb8d4
Packit 1fb8d4
	return error;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT cliprdr_order_recv(cliprdrPlugin* cliprdr, wStream* s)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT16 msgType;
Packit 1fb8d4
	UINT16 msgFlags;
Packit 1fb8d4
	UINT32 dataLen;
Packit 1fb8d4
	UINT error;
Packit 1fb8d4
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) < 8)
Packit 1fb8d4
		return ERROR_INVALID_DATA;
Packit 1fb8d4
Packit 1fb8d4
	Stream_Read_UINT16(s, msgType); /* msgType (2 bytes) */
Packit 1fb8d4
	Stream_Read_UINT16(s, msgFlags); /* msgFlags (2 bytes) */
Packit 1fb8d4
	Stream_Read_UINT32(s, dataLen); /* dataLen (4 bytes) */
Packit 1fb8d4
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) < dataLen)
Packit 1fb8d4
		return ERROR_INVALID_DATA;
Packit 1fb8d4
Packit 1fb8d4
#ifdef WITH_DEBUG_CLIPRDR
Packit 1fb8d4
	WLog_DBG(TAG, "msgType: %s (%"PRIu16"), msgFlags: %"PRIu16" dataLen: %"PRIu32"",
Packit 1fb8d4
	         CB_MSG_TYPE_STRINGS[msgType], msgType, msgFlags, dataLen);
Packit 1fb8d4
	winpr_HexDump(TAG, WLOG_DEBUG, Stream_Buffer(s), dataLen + 8);
Packit 1fb8d4
#endif
Packit 1fb8d4
Packit 1fb8d4
	switch (msgType)
Packit 1fb8d4
	{
Packit 1fb8d4
		case CB_CLIP_CAPS:
Packit 1fb8d4
			if ((error = cliprdr_process_clip_caps(cliprdr, s, dataLen, msgFlags)))
Packit 1fb8d4
				WLog_ERR(TAG, "cliprdr_process_clip_caps failed with error %"PRIu32"!", error);
Packit 1fb8d4
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case CB_MONITOR_READY:
Packit 1fb8d4
			if ((error = cliprdr_process_monitor_ready(cliprdr, s, dataLen, msgFlags)))
Packit 1fb8d4
				WLog_ERR(TAG, "cliprdr_process_monitor_ready failed with error %"PRIu32"!", error);
Packit 1fb8d4
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case CB_FORMAT_LIST:
Packit 1fb8d4
			if ((error = cliprdr_process_format_list(cliprdr, s, dataLen, msgFlags)))
Packit 1fb8d4
				WLog_ERR(TAG, "cliprdr_process_format_list failed with error %"PRIu32"!", error);
Packit 1fb8d4
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case CB_FORMAT_LIST_RESPONSE:
Packit 1fb8d4
			if ((error = cliprdr_process_format_list_response(cliprdr, s, dataLen, msgFlags)))
Packit 1fb8d4
				WLog_ERR(TAG, "cliprdr_process_format_list_response failed with error %"PRIu32"!",
Packit 1fb8d4
				         error);
Packit 1fb8d4
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case CB_FORMAT_DATA_REQUEST:
Packit 1fb8d4
			if ((error = cliprdr_process_format_data_request(cliprdr, s, dataLen, msgFlags)))
Packit 1fb8d4
				WLog_ERR(TAG, "cliprdr_process_format_data_request failed with error %"PRIu32"!",
Packit 1fb8d4
				         error);
Packit 1fb8d4
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case CB_FORMAT_DATA_RESPONSE:
Packit 1fb8d4
			if ((error = cliprdr_process_format_data_response(cliprdr, s, dataLen, msgFlags)))
Packit 1fb8d4
				WLog_ERR(TAG, "cliprdr_process_format_data_response failed with error %"PRIu32"!",
Packit 1fb8d4
				         error);
Packit 1fb8d4
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case CB_FILECONTENTS_REQUEST:
Packit 1fb8d4
			if ((error = cliprdr_process_filecontents_request(cliprdr, s, dataLen, msgFlags)))
Packit 1fb8d4
				WLog_ERR(TAG, "cliprdr_process_filecontents_request failed with error %"PRIu32"!",
Packit 1fb8d4
				         error);
Packit 1fb8d4
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case CB_FILECONTENTS_RESPONSE:
Packit 1fb8d4
			if ((error = cliprdr_process_filecontents_response(cliprdr, s, dataLen, msgFlags)))
Packit 1fb8d4
				WLog_ERR(TAG, "cliprdr_process_filecontents_response failed with error %"PRIu32"!",
Packit 1fb8d4
				         error);
Packit 1fb8d4
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case CB_LOCK_CLIPDATA:
Packit 1fb8d4
			if ((error = cliprdr_process_lock_clipdata(cliprdr, s, dataLen, msgFlags)))
Packit 1fb8d4
				WLog_ERR(TAG, "cliprdr_process_lock_clipdata failed with error %"PRIu32"!", error);
Packit 1fb8d4
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case CB_UNLOCK_CLIPDATA:
Packit 1fb8d4
			if ((error = cliprdr_process_unlock_clipdata(cliprdr, s, dataLen, msgFlags)))
Packit 1fb8d4
				WLog_ERR(TAG, "cliprdr_process_lock_clipdata failed with error %"PRIu32"!", error);
Packit 1fb8d4
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		default:
Packit 1fb8d4
			error = CHANNEL_RC_BAD_PROC;
Packit 1fb8d4
			WLog_ERR(TAG, "unknown msgType %"PRIu16"", msgType);
Packit 1fb8d4
			break;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	Stream_Free(s, TRUE);
Packit 1fb8d4
	return error;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Callback Interface
Packit 1fb8d4
 */
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT cliprdr_client_capabilities(CliprdrClientContext* context,
Packit 1fb8d4
                                        CLIPRDR_CAPABILITIES* capabilities)
Packit 1fb8d4
{
Packit 1fb8d4
	wStream* s;
Packit 1fb8d4
	CLIPRDR_GENERAL_CAPABILITY_SET* generalCapabilitySet;
Packit 1fb8d4
	cliprdrPlugin* cliprdr = (cliprdrPlugin*) context->handle;
Packit 1fb8d4
	s = cliprdr_packet_new(CB_CLIP_CAPS, 0, 4 + CB_CAPSTYPE_GENERAL_LEN);
Packit 1fb8d4
Packit 1fb8d4
	if (!s)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "cliprdr_packet_new failed!");
Packit 1fb8d4
		return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	Stream_Write_UINT16(s, 1); /* cCapabilitiesSets */
Packit 1fb8d4
	Stream_Write_UINT16(s, 0); /* pad1 */
Packit 1fb8d4
	generalCapabilitySet = (CLIPRDR_GENERAL_CAPABILITY_SET*)capabilities->capabilitySets;
Packit 1fb8d4
	Stream_Write_UINT16(s, generalCapabilitySet->capabilitySetType); /* capabilitySetType */
Packit 1fb8d4
	Stream_Write_UINT16(s, generalCapabilitySet->capabilitySetLength); /* lengthCapability */
Packit 1fb8d4
	Stream_Write_UINT32(s, generalCapabilitySet->version); /* version */
Packit 1fb8d4
	Stream_Write_UINT32(s, generalCapabilitySet->generalFlags); /* generalFlags */
Packit 1fb8d4
	WLog_Print(cliprdr->log, WLOG_DEBUG, "ClientCapabilities");
Packit 1fb8d4
	return cliprdr_packet_send(cliprdr, s);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT cliprdr_temp_directory(CliprdrClientContext* context,
Packit 1fb8d4
                                   CLIPRDR_TEMP_DIRECTORY* tempDirectory)
Packit 1fb8d4
{
Packit 1fb8d4
	int length;
Packit 1fb8d4
	wStream* s;
Packit 1fb8d4
	WCHAR* wszTempDir = NULL;
Packit 1fb8d4
	cliprdrPlugin* cliprdr = (cliprdrPlugin*) context->handle;
Packit 1fb8d4
	s = cliprdr_packet_new(CB_TEMP_DIRECTORY, 0, 520 * 2);
Packit 1fb8d4
Packit 1fb8d4
	if (!s)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "cliprdr_packet_new failed!");
Packit 1fb8d4
		return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	length = ConvertToUnicode(CP_UTF8, 0, tempDirectory->szTempDir, -1, &wszTempDir, 0);
Packit 1fb8d4
Packit 1fb8d4
	if (length < 0)
Packit 1fb8d4
		return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
Packit 1fb8d4
	if (length > 520)
Packit 1fb8d4
		length = 520;
Packit 1fb8d4
Packit 1fb8d4
	Stream_Write(s, wszTempDir, length * 2);
Packit 1fb8d4
	Stream_Zero(s, (520 - length) * 2);
Packit 1fb8d4
	free(wszTempDir);
Packit 1fb8d4
	WLog_Print(cliprdr->log, WLOG_DEBUG, "TempDirectory: %s",
Packit 1fb8d4
	           tempDirectory->szTempDir);
Packit 1fb8d4
	return cliprdr_packet_send(cliprdr, s);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT cliprdr_client_format_list(CliprdrClientContext* context,
Packit 1fb8d4
                                       CLIPRDR_FORMAT_LIST* formatList)
Packit 1fb8d4
{
Packit 1fb8d4
	wStream* s;
Packit 1fb8d4
	UINT32 index;
Packit 1fb8d4
	int length = 0;
Packit 1fb8d4
	int cchWideChar;
Packit 1fb8d4
	LPWSTR lpWideCharStr;
Packit 1fb8d4
	int formatNameSize;
Packit 1fb8d4
	int formatNameLength;
Packit 1fb8d4
	char* szFormatName;
Packit 1fb8d4
	WCHAR* wszFormatName;
Packit 1fb8d4
	BOOL asciiNames = FALSE;
Packit 1fb8d4
	CLIPRDR_FORMAT* format;
Packit 1fb8d4
	cliprdrPlugin* cliprdr = (cliprdrPlugin*) context->handle;
Packit 1fb8d4
Packit 1fb8d4
	if (!cliprdr->useLongFormatNames)
Packit 1fb8d4
	{
Packit 1fb8d4
		length = formatList->numFormats * 36;
Packit 1fb8d4
		s = cliprdr_packet_new(CB_FORMAT_LIST, 0, length);
Packit 1fb8d4
Packit 1fb8d4
		if (!s)
Packit 1fb8d4
		{
Packit 1fb8d4
			WLog_ERR(TAG, "cliprdr_packet_new failed!");
Packit 1fb8d4
			return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		for (index = 0; index < formatList->numFormats; index++)
Packit 1fb8d4
		{
Packit 1fb8d4
			format = (CLIPRDR_FORMAT*) & (formatList->formats[index]);
Packit 1fb8d4
			Stream_Write_UINT32(s, format->formatId); /* formatId (4 bytes) */
Packit 1fb8d4
			formatNameSize = 0;
Packit 1fb8d4
			formatNameLength = 0;
Packit 1fb8d4
			szFormatName = format->formatName;
Packit 1fb8d4
Packit 1fb8d4
			if (asciiNames)
Packit 1fb8d4
			{
Packit 1fb8d4
				if (szFormatName)
Packit 1fb8d4
					formatNameLength = strlen(szFormatName);
Packit 1fb8d4
Packit 1fb8d4
				if (formatNameLength > 31)
Packit 1fb8d4
					formatNameLength = 31;
Packit 1fb8d4
Packit 1fb8d4
				Stream_Write(s, szFormatName, formatNameLength);
Packit 1fb8d4
				Stream_Zero(s, 32 - formatNameLength);
Packit 1fb8d4
			}
Packit 1fb8d4
			else
Packit 1fb8d4
			{
Packit 1fb8d4
				wszFormatName = NULL;
Packit 1fb8d4
Packit 1fb8d4
				if (szFormatName)
Packit 1fb8d4
					formatNameSize = ConvertToUnicode(CP_UTF8, 0, szFormatName, -1, &wszFormatName,
Packit 1fb8d4
					                                  0);
Packit 1fb8d4
Packit 1fb8d4
				if (formatNameSize > 15)
Packit 1fb8d4
					formatNameSize = 15;
Packit 1fb8d4
Packit 1fb8d4
				if (wszFormatName)
Packit 1fb8d4
					Stream_Write(s, wszFormatName, formatNameSize * 2);
Packit 1fb8d4
Packit 1fb8d4
				Stream_Zero(s, 32 - (formatNameSize * 2));
Packit 1fb8d4
				free(wszFormatName);
Packit 1fb8d4
			}
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
	else
Packit 1fb8d4
	{
Packit 1fb8d4
		for (index = 0; index < formatList->numFormats; index++)
Packit 1fb8d4
		{
Packit 1fb8d4
			format = (CLIPRDR_FORMAT*) & (formatList->formats[index]);
Packit 1fb8d4
			length += 4;
Packit 1fb8d4
			formatNameSize = 2;
Packit 1fb8d4
Packit 1fb8d4
			if (format->formatName)
Packit 1fb8d4
				formatNameSize = MultiByteToWideChar(CP_UTF8, 0, format->formatName, -1, NULL,
Packit 1fb8d4
				                                     0) * 2;
Packit 1fb8d4
Packit 1fb8d4
			length += formatNameSize;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		s = cliprdr_packet_new(CB_FORMAT_LIST, 0, length);
Packit 1fb8d4
Packit 1fb8d4
		if (!s)
Packit 1fb8d4
		{
Packit 1fb8d4
			WLog_ERR(TAG, "cliprdr_packet_new failed!");
Packit 1fb8d4
			return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		for (index = 0; index < formatList->numFormats; index++)
Packit 1fb8d4
		{
Packit 1fb8d4
			format = (CLIPRDR_FORMAT*) & (formatList->formats[index]);
Packit 1fb8d4
			Stream_Write_UINT32(s, format->formatId); /* formatId (4 bytes) */
Packit 1fb8d4
Packit 1fb8d4
			if (format->formatName)
Packit 1fb8d4
			{
Packit 1fb8d4
				lpWideCharStr = (LPWSTR) Stream_Pointer(s);
Packit 1fb8d4
				cchWideChar = (Stream_Capacity(s) - Stream_GetPosition(s)) / 2;
Packit 1fb8d4
				formatNameSize = MultiByteToWideChar(CP_UTF8, 0,
Packit 1fb8d4
				                                     format->formatName, -1, lpWideCharStr, cchWideChar) * 2;
Packit 1fb8d4
				Stream_Seek(s, formatNameSize);
Packit 1fb8d4
			}
Packit 1fb8d4
			else
Packit 1fb8d4
			{
Packit 1fb8d4
				Stream_Write_UINT16(s, 0);
Packit 1fb8d4
			}
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	WLog_Print(cliprdr->log, WLOG_DEBUG, "ClientFormatList: numFormats: %"PRIu32"",
Packit 1fb8d4
	           formatList->numFormats);
Packit 1fb8d4
	return cliprdr_packet_send(cliprdr, s);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT cliprdr_client_format_list_response(CliprdrClientContext* context,
Packit 1fb8d4
        CLIPRDR_FORMAT_LIST_RESPONSE* formatListResponse)
Packit 1fb8d4
{
Packit 1fb8d4
	wStream* s;
Packit 1fb8d4
	cliprdrPlugin* cliprdr = (cliprdrPlugin*) context->handle;
Packit 1fb8d4
	formatListResponse->msgType = CB_FORMAT_LIST_RESPONSE;
Packit 1fb8d4
	formatListResponse->dataLen = 0;
Packit 1fb8d4
	s = cliprdr_packet_new(formatListResponse->msgType,
Packit 1fb8d4
	                       formatListResponse->msgFlags, formatListResponse->dataLen);
Packit 1fb8d4
Packit 1fb8d4
	if (!s)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "cliprdr_packet_new failed!");
Packit 1fb8d4
		return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	WLog_Print(cliprdr->log, WLOG_DEBUG, "ClientFormatListResponse");
Packit 1fb8d4
	return cliprdr_packet_send(cliprdr, s);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT cliprdr_client_lock_clipboard_data(CliprdrClientContext* context,
Packit 1fb8d4
        CLIPRDR_LOCK_CLIPBOARD_DATA* lockClipboardData)
Packit 1fb8d4
{
Packit 1fb8d4
	wStream* s;
Packit 1fb8d4
	cliprdrPlugin* cliprdr = (cliprdrPlugin*) context->handle;
Packit 1fb8d4
	s = cliprdr_packet_new(CB_LOCK_CLIPDATA, 0, 4);
Packit 1fb8d4
Packit 1fb8d4
	if (!s)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "cliprdr_packet_new failed!");
Packit 1fb8d4
		return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	Stream_Write_UINT32(s, lockClipboardData->clipDataId); /* clipDataId (4 bytes) */
Packit 1fb8d4
	WLog_Print(cliprdr->log, WLOG_DEBUG,
Packit 1fb8d4
	           "ClientLockClipboardData: clipDataId: 0x%08"PRIX32"",
Packit 1fb8d4
	           lockClipboardData->clipDataId);
Packit 1fb8d4
	return cliprdr_packet_send(cliprdr, s);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT cliprdr_client_unlock_clipboard_data(CliprdrClientContext* context,
Packit 1fb8d4
        CLIPRDR_UNLOCK_CLIPBOARD_DATA* unlockClipboardData)
Packit 1fb8d4
{
Packit 1fb8d4
	wStream* s;
Packit 1fb8d4
	cliprdrPlugin* cliprdr = (cliprdrPlugin*) context->handle;
Packit 1fb8d4
	s = cliprdr_packet_new(CB_UNLOCK_CLIPDATA, 0, 4);
Packit 1fb8d4
Packit 1fb8d4
	if (!s)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "cliprdr_packet_new failed!");
Packit 1fb8d4
		return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	Stream_Write_UINT32(s, unlockClipboardData->clipDataId); /* clipDataId (4 bytes) */
Packit 1fb8d4
	WLog_Print(cliprdr->log, WLOG_DEBUG,
Packit 1fb8d4
	           "ClientUnlockClipboardData: clipDataId: 0x%08"PRIX32"",
Packit 1fb8d4
	           unlockClipboardData->clipDataId);
Packit 1fb8d4
	return cliprdr_packet_send(cliprdr, s);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT cliprdr_client_format_data_request(CliprdrClientContext* context,
Packit 1fb8d4
        CLIPRDR_FORMAT_DATA_REQUEST* formatDataRequest)
Packit 1fb8d4
{
Packit 1fb8d4
	wStream* s;
Packit 1fb8d4
	cliprdrPlugin* cliprdr = (cliprdrPlugin*) context->handle;
Packit 1fb8d4
	formatDataRequest->msgType = CB_FORMAT_DATA_REQUEST;
Packit 1fb8d4
	formatDataRequest->msgFlags = 0;
Packit 1fb8d4
	formatDataRequest->dataLen = 4;
Packit 1fb8d4
	s = cliprdr_packet_new(formatDataRequest->msgType, formatDataRequest->msgFlags,
Packit 1fb8d4
	                       formatDataRequest->dataLen);
Packit 1fb8d4
Packit 1fb8d4
	if (!s)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "cliprdr_packet_new failed!");
Packit 1fb8d4
		return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	Stream_Write_UINT32(s, formatDataRequest->requestedFormatId); /* requestedFormatId (4 bytes) */
Packit 1fb8d4
	WLog_Print(cliprdr->log, WLOG_DEBUG, "ClientFormatDataRequest");
Packit 1fb8d4
	return cliprdr_packet_send(cliprdr, s);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT cliprdr_client_format_data_response(CliprdrClientContext* context,
Packit 1fb8d4
        CLIPRDR_FORMAT_DATA_RESPONSE* formatDataResponse)
Packit 1fb8d4
{
Packit 1fb8d4
	wStream* s;
Packit 1fb8d4
	cliprdrPlugin* cliprdr = (cliprdrPlugin*) context->handle;
Packit 1fb8d4
	formatDataResponse->msgType = CB_FORMAT_DATA_RESPONSE;
Packit 1fb8d4
	s = cliprdr_packet_new(formatDataResponse->msgType,
Packit 1fb8d4
	                       formatDataResponse->msgFlags, formatDataResponse->dataLen);
Packit 1fb8d4
Packit 1fb8d4
	if (!s)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "cliprdr_packet_new failed!");
Packit 1fb8d4
		return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	Stream_Write(s, formatDataResponse->requestedFormatData,
Packit 1fb8d4
	             formatDataResponse->dataLen);
Packit 1fb8d4
	WLog_Print(cliprdr->log, WLOG_DEBUG, "ClientFormatDataResponse");
Packit 1fb8d4
	return cliprdr_packet_send(cliprdr, s);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT cliprdr_client_file_contents_request(CliprdrClientContext* context,
Packit 1fb8d4
        CLIPRDR_FILE_CONTENTS_REQUEST* fileContentsRequest)
Packit 1fb8d4
{
Packit 1fb8d4
	wStream* s;
Packit 1fb8d4
	cliprdrPlugin* cliprdr = (cliprdrPlugin*) context->handle;
Packit 1fb8d4
	s = cliprdr_packet_new(CB_FILECONTENTS_REQUEST, 0, 28);
Packit 1fb8d4
Packit 1fb8d4
	if (!s)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "cliprdr_packet_new failed!");
Packit 1fb8d4
		return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	Stream_Write_UINT32(s, fileContentsRequest->streamId); /* streamId (4 bytes) */
Packit 1fb8d4
	Stream_Write_UINT32(s, fileContentsRequest->listIndex); /* listIndex (4 bytes) */
Packit 1fb8d4
	Stream_Write_UINT32(s, fileContentsRequest->dwFlags); /* dwFlags (4 bytes) */
Packit 1fb8d4
	Stream_Write_UINT32(s, fileContentsRequest->nPositionLow); /* nPositionLow (4 bytes) */
Packit 1fb8d4
	Stream_Write_UINT32(s, fileContentsRequest->nPositionHigh); /* nPositionHigh (4 bytes) */
Packit 1fb8d4
	Stream_Write_UINT32(s, fileContentsRequest->cbRequested); /* cbRequested (4 bytes) */
Packit 1fb8d4
Packit 1fb8d4
	if (fileContentsRequest->haveClipDataId)
Packit 1fb8d4
		Stream_Write_UINT32(s, fileContentsRequest->clipDataId); /* clipDataId (4 bytes) */
Packit 1fb8d4
Packit 1fb8d4
	WLog_Print(cliprdr->log, WLOG_DEBUG,
Packit 1fb8d4
	           "ClientFileContentsRequest: streamId: 0x%08"PRIX32"",
Packit 1fb8d4
	           fileContentsRequest->streamId);
Packit 1fb8d4
	return cliprdr_packet_send(cliprdr, s);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT cliprdr_client_file_contents_response(CliprdrClientContext* context,
Packit 1fb8d4
        CLIPRDR_FILE_CONTENTS_RESPONSE* fileContentsResponse)
Packit 1fb8d4
{
Packit 1fb8d4
	wStream* s;
Packit 1fb8d4
	cliprdrPlugin* cliprdr = (cliprdrPlugin*) context->handle;
Packit 1fb8d4
	s = cliprdr_packet_new(CB_FILECONTENTS_RESPONSE, fileContentsResponse->msgFlags,
Packit 1fb8d4
	                       4 + fileContentsResponse->cbRequested);
Packit 1fb8d4
Packit 1fb8d4
	if (!s)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "cliprdr_packet_new failed!");
Packit 1fb8d4
		return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	Stream_Write_UINT32(s, fileContentsResponse->streamId); /* streamId (4 bytes) */
Packit 1fb8d4
	/**
Packit 1fb8d4
	 * requestedFileContentsData:
Packit 1fb8d4
	 * FILECONTENTS_SIZE: file size as UINT64
Packit 1fb8d4
	 * FILECONTENTS_RANGE: file data from requested range
Packit 1fb8d4
	 */
Packit 1fb8d4
	Stream_Write(s, fileContentsResponse->requestedData,
Packit 1fb8d4
	             fileContentsResponse->cbRequested);
Packit 1fb8d4
	WLog_Print(cliprdr->log, WLOG_DEBUG,
Packit 1fb8d4
	           "ClientFileContentsResponse: streamId: 0x%08"PRIX32"",
Packit 1fb8d4
	           fileContentsResponse->streamId);
Packit 1fb8d4
	return cliprdr_packet_send(cliprdr, s);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT cliprdr_virtual_channel_event_data_received(cliprdrPlugin* cliprdr,
Packit 1fb8d4
        void* pData, UINT32 dataLength, UINT32 totalLength, UINT32 dataFlags)
Packit 1fb8d4
{
Packit 1fb8d4
	wStream* data_in;
Packit 1fb8d4
Packit 1fb8d4
	if ((dataFlags & CHANNEL_FLAG_SUSPEND) || (dataFlags & CHANNEL_FLAG_RESUME))
Packit 1fb8d4
	{
Packit 1fb8d4
		return CHANNEL_RC_OK;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (dataFlags & CHANNEL_FLAG_FIRST)
Packit 1fb8d4
	{
Packit 1fb8d4
		if (cliprdr->data_in)
Packit 1fb8d4
			Stream_Free(cliprdr->data_in, TRUE);
Packit 1fb8d4
Packit 1fb8d4
		cliprdr->data_in = Stream_New(NULL, totalLength);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (!(data_in = cliprdr->data_in))
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "Stream_New failed!");
Packit 1fb8d4
		return CHANNEL_RC_NO_MEMORY;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (!Stream_EnsureRemainingCapacity(data_in, (int) dataLength))
Packit 1fb8d4
	{
Packit 1fb8d4
		Stream_Free(cliprdr->data_in, TRUE);
Packit 1fb8d4
		cliprdr->data_in = NULL;
Packit 1fb8d4
		return CHANNEL_RC_NO_MEMORY;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	Stream_Write(data_in, pData, dataLength);
Packit 1fb8d4
Packit 1fb8d4
	if (dataFlags & CHANNEL_FLAG_LAST)
Packit 1fb8d4
	{
Packit 1fb8d4
		if (Stream_Capacity(data_in) != Stream_GetPosition(data_in))
Packit 1fb8d4
		{
Packit 1fb8d4
			WLog_ERR(TAG, "cliprdr_plugin_process_received: read error");
Packit 1fb8d4
			return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		cliprdr->data_in = NULL;
Packit 1fb8d4
		Stream_SealLength(data_in);
Packit 1fb8d4
		Stream_SetPosition(data_in, 0);
Packit 1fb8d4
Packit 1fb8d4
		if (!MessageQueue_Post(cliprdr->queue, NULL, 0, (void*) data_in, NULL))
Packit 1fb8d4
		{
Packit 1fb8d4
			WLog_ERR(TAG, "MessageQueue_Post failed!");
Packit 1fb8d4
			return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static VOID VCAPITYPE cliprdr_virtual_channel_open_event_ex(LPVOID lpUserParam, DWORD openHandle,
Packit 1fb8d4
        UINT event,
Packit 1fb8d4
        LPVOID pData, UINT32 dataLength, UINT32 totalLength, UINT32 dataFlags)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT error = CHANNEL_RC_OK;
Packit 1fb8d4
	cliprdrPlugin* cliprdr = (cliprdrPlugin*) lpUserParam;
Packit 1fb8d4
Packit 1fb8d4
	if (!cliprdr || (cliprdr->OpenHandle != openHandle))
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "error no match");
Packit 1fb8d4
		return;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	switch (event)
Packit 1fb8d4
	{
Packit 1fb8d4
		case CHANNEL_EVENT_DATA_RECEIVED:
Packit 1fb8d4
			if ((error = cliprdr_virtual_channel_event_data_received(cliprdr, pData, dataLength,
Packit 1fb8d4
			             totalLength, dataFlags)))
Packit 1fb8d4
				WLog_ERR(TAG, "failed with error %"PRIu32"", error);
Packit 1fb8d4
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case CHANNEL_EVENT_WRITE_COMPLETE:
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case CHANNEL_EVENT_USER:
Packit 1fb8d4
			break;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (error && cliprdr->context->rdpcontext)
Packit 1fb8d4
		setChannelError(cliprdr->context->rdpcontext, error,
Packit 1fb8d4
		                "cliprdr_virtual_channel_open_event_ex reported an error");
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static DWORD WINAPI cliprdr_virtual_channel_client_thread(LPVOID arg)
Packit 1fb8d4
{
Packit 1fb8d4
	wStream* data;
Packit 1fb8d4
	wMessage message;
Packit 1fb8d4
	cliprdrPlugin* cliprdr = (cliprdrPlugin*) arg;
Packit 1fb8d4
	UINT error = CHANNEL_RC_OK;
Packit 1fb8d4
Packit 1fb8d4
	while (1)
Packit 1fb8d4
	{
Packit 1fb8d4
		if (!MessageQueue_Wait(cliprdr->queue))
Packit 1fb8d4
		{
Packit 1fb8d4
			WLog_ERR(TAG, "MessageQueue_Wait failed!");
Packit 1fb8d4
			error = ERROR_INTERNAL_ERROR;
Packit 1fb8d4
			break;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		if (!MessageQueue_Peek(cliprdr->queue, &message, TRUE))
Packit 1fb8d4
		{
Packit 1fb8d4
			WLog_ERR(TAG, "MessageQueue_Peek failed!");
Packit 1fb8d4
			error = ERROR_INTERNAL_ERROR;
Packit 1fb8d4
			break;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		if (message.id == WMQ_QUIT)
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		if (message.id == 0)
Packit 1fb8d4
		{
Packit 1fb8d4
			data = (wStream*) message.wParam;
Packit 1fb8d4
Packit 1fb8d4
			if ((error = cliprdr_order_recv(cliprdr, data)))
Packit 1fb8d4
			{
Packit 1fb8d4
				WLog_ERR(TAG, "cliprdr_order_recv failed with error %"PRIu32"!", error);
Packit 1fb8d4
				break;
Packit 1fb8d4
			}
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (error && cliprdr->context->rdpcontext)
Packit 1fb8d4
		setChannelError(cliprdr->context->rdpcontext, error,
Packit 1fb8d4
		                "cliprdr_virtual_channel_client_thread reported an error");
Packit 1fb8d4
Packit 1fb8d4
	ExitThread(error);
Packit 1fb8d4
	return error;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT cliprdr_virtual_channel_event_connected(cliprdrPlugin* cliprdr,
Packit 1fb8d4
        LPVOID pData, UINT32 dataLength)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT32 status;
Packit 1fb8d4
	status = cliprdr->channelEntryPoints.pVirtualChannelOpenEx(cliprdr->InitHandle,
Packit 1fb8d4
	         &cliprdr->OpenHandle, cliprdr->channelDef.name,
Packit 1fb8d4
	         cliprdr_virtual_channel_open_event_ex);
Packit 1fb8d4
Packit 1fb8d4
	if (status != CHANNEL_RC_OK)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "pVirtualChannelOpen failed with %s [%08"PRIX32"]",
Packit 1fb8d4
		         WTSErrorToString(status), status);
Packit 1fb8d4
		return status;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	cliprdr->queue = MessageQueue_New(NULL);
Packit 1fb8d4
Packit 1fb8d4
	if (!cliprdr->queue)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "MessageQueue_New failed!");
Packit 1fb8d4
		return ERROR_NOT_ENOUGH_MEMORY;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (!(cliprdr->thread = CreateThread(NULL, 0, cliprdr_virtual_channel_client_thread,
Packit 1fb8d4
	                                     (void*) cliprdr,
Packit 1fb8d4
	                                     0, NULL)))
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "CreateThread failed!");
Packit 1fb8d4
		MessageQueue_Free(cliprdr->queue);
Packit 1fb8d4
		cliprdr->queue = NULL;
Packit 1fb8d4
		return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT cliprdr_virtual_channel_event_disconnected(cliprdrPlugin* cliprdr)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT rc;
Packit 1fb8d4
Packit 1fb8d4
	if (cliprdr->OpenHandle == 0)
Packit 1fb8d4
		return CHANNEL_RC_OK;
Packit 1fb8d4
Packit 1fb8d4
	if (MessageQueue_PostQuit(cliprdr->queue, 0)
Packit 1fb8d4
	    && (WaitForSingleObject(cliprdr->thread, INFINITE) == WAIT_FAILED))
Packit 1fb8d4
	{
Packit 1fb8d4
		rc = GetLastError();
Packit 1fb8d4
		WLog_ERR(TAG, "WaitForSingleObject failed with error %"PRIu32"", rc);
Packit 1fb8d4
		return rc;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	MessageQueue_Free(cliprdr->queue);
Packit 1fb8d4
	CloseHandle(cliprdr->thread);
Packit 1fb8d4
	rc = cliprdr->channelEntryPoints.pVirtualChannelCloseEx(cliprdr->InitHandle, cliprdr->OpenHandle);
Packit 1fb8d4
Packit 1fb8d4
	if (CHANNEL_RC_OK != rc)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "pVirtualChannelClose failed with %s [%08"PRIX32"]",
Packit 1fb8d4
		         WTSErrorToString(rc), rc);
Packit 1fb8d4
		return rc;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	cliprdr->OpenHandle = 0;
Packit 1fb8d4
Packit 1fb8d4
	if (cliprdr->data_in)
Packit 1fb8d4
	{
Packit 1fb8d4
		Stream_Free(cliprdr->data_in, TRUE);
Packit 1fb8d4
		cliprdr->data_in = NULL;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT cliprdr_virtual_channel_event_terminated(cliprdrPlugin* cliprdr)
Packit 1fb8d4
{
Packit 1fb8d4
	cliprdr->InitHandle = 0;
Packit 1fb8d4
	free(cliprdr->context);
Packit 1fb8d4
	free(cliprdr);
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static VOID VCAPITYPE cliprdr_virtual_channel_init_event_ex(LPVOID lpUserParam, LPVOID pInitHandle,
Packit 1fb8d4
        UINT event, LPVOID pData, UINT dataLength)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT error = CHANNEL_RC_OK;
Packit 1fb8d4
	cliprdrPlugin* cliprdr = (cliprdrPlugin*) lpUserParam;
Packit 1fb8d4
Packit 1fb8d4
	if (!cliprdr || (cliprdr->InitHandle != pInitHandle))
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "error no match");
Packit 1fb8d4
		return;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	switch (event)
Packit 1fb8d4
	{
Packit 1fb8d4
		case CHANNEL_EVENT_CONNECTED:
Packit 1fb8d4
			if ((error = cliprdr_virtual_channel_event_connected(cliprdr, pData, dataLength)))
Packit 1fb8d4
				WLog_ERR(TAG, "cliprdr_virtual_channel_event_connected failed with error %"PRIu32"!",
Packit 1fb8d4
				         error);
Packit 1fb8d4
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case CHANNEL_EVENT_DISCONNECTED:
Packit 1fb8d4
			if ((error = cliprdr_virtual_channel_event_disconnected(cliprdr)))
Packit 1fb8d4
				WLog_ERR(TAG,
Packit 1fb8d4
				         "cliprdr_virtual_channel_event_disconnected failed with error %"PRIu32"!", error);
Packit 1fb8d4
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case CHANNEL_EVENT_TERMINATED:
Packit 1fb8d4
			if ((error = cliprdr_virtual_channel_event_terminated(cliprdr)))
Packit 1fb8d4
				WLog_ERR(TAG, "cliprdr_virtual_channel_event_terminated failed with error %"PRIu32"!",
Packit 1fb8d4
				         error);
Packit 1fb8d4
Packit 1fb8d4
			break;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (error && cliprdr->context->rdpcontext)
Packit 1fb8d4
		setChannelError(cliprdr->context->rdpcontext, error,
Packit 1fb8d4
		                "cliprdr_virtual_channel_init_event reported an error");
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/* cliprdr is always built-in */
Packit 1fb8d4
#define VirtualChannelEntryEx	cliprdr_VirtualChannelEntryEx
Packit 1fb8d4
Packit 1fb8d4
BOOL VCAPITYPE VirtualChannelEntryEx(PCHANNEL_ENTRY_POINTS pEntryPoints, PVOID pInitHandle)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT rc;
Packit 1fb8d4
	cliprdrPlugin* cliprdr;
Packit 1fb8d4
	CliprdrClientContext* context = NULL;
Packit 1fb8d4
	CHANNEL_ENTRY_POINTS_FREERDP_EX* pEntryPointsEx;
Packit 1fb8d4
	cliprdr = (cliprdrPlugin*) calloc(1, sizeof(cliprdrPlugin));
Packit 1fb8d4
Packit 1fb8d4
	if (!cliprdr)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "calloc failed!");
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	cliprdr->channelDef.options =
Packit 1fb8d4
	    CHANNEL_OPTION_INITIALIZED |
Packit 1fb8d4
	    CHANNEL_OPTION_ENCRYPT_RDP |
Packit 1fb8d4
	    CHANNEL_OPTION_COMPRESS_RDP |
Packit 1fb8d4
	    CHANNEL_OPTION_SHOW_PROTOCOL;
Packit 1fb8d4
	sprintf_s(cliprdr->channelDef.name, ARRAYSIZE(cliprdr->channelDef.name), "cliprdr");
Packit 1fb8d4
	pEntryPointsEx = (CHANNEL_ENTRY_POINTS_FREERDP_EX*) pEntryPoints;
Packit 1fb8d4
Packit 1fb8d4
	if ((pEntryPointsEx->cbSize >= sizeof(CHANNEL_ENTRY_POINTS_FREERDP_EX)) &&
Packit 1fb8d4
	    (pEntryPointsEx->MagicNumber == FREERDP_CHANNEL_MAGIC_NUMBER))
Packit 1fb8d4
	{
Packit 1fb8d4
		context = (CliprdrClientContext*) calloc(1, sizeof(CliprdrClientContext));
Packit 1fb8d4
Packit 1fb8d4
		if (!context)
Packit 1fb8d4
		{
Packit 1fb8d4
			free(cliprdr);
Packit 1fb8d4
			WLog_ERR(TAG, "calloc failed!");
Packit 1fb8d4
			return FALSE;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		context->handle = (void*) cliprdr;
Packit 1fb8d4
		context->custom = NULL;
Packit 1fb8d4
		context->ClientCapabilities = cliprdr_client_capabilities;
Packit 1fb8d4
		context->TempDirectory = cliprdr_temp_directory;
Packit 1fb8d4
		context->ClientFormatList = cliprdr_client_format_list;
Packit 1fb8d4
		context->ClientFormatListResponse = cliprdr_client_format_list_response;
Packit 1fb8d4
		context->ClientLockClipboardData = cliprdr_client_lock_clipboard_data;
Packit 1fb8d4
		context->ClientUnlockClipboardData = cliprdr_client_unlock_clipboard_data;
Packit 1fb8d4
		context->ClientFormatDataRequest = cliprdr_client_format_data_request;
Packit 1fb8d4
		context->ClientFormatDataResponse = cliprdr_client_format_data_response;
Packit 1fb8d4
		context->ClientFileContentsRequest = cliprdr_client_file_contents_request;
Packit 1fb8d4
		context->ClientFileContentsResponse = cliprdr_client_file_contents_response;
Packit 1fb8d4
		cliprdr->context = context;
Packit 1fb8d4
		context->rdpcontext = pEntryPointsEx->context;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	cliprdr->log = WLog_Get("com.freerdp.channels.cliprdr.client");
Packit 1fb8d4
	cliprdr->useLongFormatNames = TRUE;
Packit 1fb8d4
	cliprdr->streamFileClipEnabled = FALSE;
Packit 1fb8d4
	cliprdr->fileClipNoFilePaths = TRUE;
Packit 1fb8d4
	cliprdr->canLockClipData = FALSE;
Packit 1fb8d4
	WLog_Print(cliprdr->log, WLOG_DEBUG, "VirtualChannelEntryEx");
Packit 1fb8d4
	CopyMemory(&(cliprdr->channelEntryPoints), pEntryPoints,
Packit 1fb8d4
	           sizeof(CHANNEL_ENTRY_POINTS_FREERDP_EX));
Packit 1fb8d4
	cliprdr->InitHandle = pInitHandle;
Packit 1fb8d4
	rc = cliprdr->channelEntryPoints.pVirtualChannelInitEx(cliprdr, context, pInitHandle,
Packit 1fb8d4
	        &cliprdr->channelDef, 1, VIRTUAL_CHANNEL_VERSION_WIN2000,
Packit 1fb8d4
	        cliprdr_virtual_channel_init_event_ex);
Packit 1fb8d4
Packit 1fb8d4
	if (CHANNEL_RC_OK != rc)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "pVirtualChannelInit failed with %s [%08"PRIX32"]",
Packit 1fb8d4
		         WTSErrorToString(rc), rc);
Packit 1fb8d4
		free(cliprdr->context);
Packit 1fb8d4
		free(cliprdr);
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	cliprdr->channelEntryPoints.pInterface = context;
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
}