2019-09-15 13:53:56 +03:00
|
|
|
/**
|
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
|
|
|
* Cliprdr common
|
|
|
|
*
|
|
|
|
* Copyright 2013 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
|
|
|
* Copyright 2015 Thincast Technologies GmbH
|
|
|
|
* Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
|
|
|
|
* Copyright 2019 Kobi Mizrachi <kmizrachi18@gmail.com>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <winpr/crt.h>
|
|
|
|
#include <winpr/stream.h>
|
|
|
|
#include <freerdp/channels/log.h>
|
|
|
|
|
|
|
|
#define TAG CHANNELS_TAG("cliprdr.common")
|
|
|
|
|
|
|
|
#include "cliprdr_common.h"
|
|
|
|
|
|
|
|
static BOOL cliprdr_validate_file_contents_request(const CLIPRDR_FILE_CONTENTS_REQUEST* request)
|
|
|
|
{
|
2019-09-15 14:48:12 +03:00
|
|
|
/*
|
|
|
|
* [MS-RDPECLIP] 2.2.5.3 File Contents Request PDU (CLIPRDR_FILECONTENTS_REQUEST).
|
|
|
|
*
|
2019-09-15 13:53:56 +03:00
|
|
|
* A request for the size of the file identified by the lindex field. The size MUST be
|
|
|
|
* returned as a 64-bit, unsigned integer. The cbRequested field MUST be set to
|
|
|
|
* 0x00000008 and both the nPositionLow and nPositionHigh fields MUST be
|
|
|
|
* set to 0x00000000.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (request->dwFlags & FILECONTENTS_SIZE)
|
|
|
|
{
|
|
|
|
if (request->cbRequested != sizeof(UINT64))
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "[%s]: cbRequested must be %" PRIu32 ", got %" PRIu32 "", __FUNCTION__,
|
2019-09-15 13:53:56 +03:00
|
|
|
sizeof(UINT64), request->cbRequested);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (request->nPositionHigh != 0 || request->nPositionLow != 0)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "[%s]: nPositionHigh and nPositionLow must be set to 0", __FUNCTION__);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-15 14:48:12 +03:00
|
|
|
return TRUE;
|
2019-09-15 13:53:56 +03:00
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
wStream* cliprdr_packet_new(UINT16 msgType, UINT16 msgFlags, UINT32 dataLen)
|
2019-10-17 18:59:07 +03:00
|
|
|
{
|
|
|
|
wStream* s;
|
|
|
|
s = Stream_New(NULL, dataLen + 8);
|
|
|
|
|
|
|
|
if (!s)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Stream_New failed!");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
Stream_Write_UINT16(s, msgType);
|
|
|
|
Stream_Write_UINT16(s, msgFlags);
|
|
|
|
/* Write actual length after the entire packet has been constructed. */
|
|
|
|
Stream_Seek(s, 4);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
static void cliprdr_write_file_contents_request(wStream* s,
|
|
|
|
const CLIPRDR_FILE_CONTENTS_REQUEST* request)
|
2019-09-15 13:53:56 +03:00
|
|
|
{
|
|
|
|
Stream_Write_UINT32(s, request->streamId); /* streamId (4 bytes) */
|
|
|
|
Stream_Write_UINT32(s, request->listIndex); /* listIndex (4 bytes) */
|
|
|
|
Stream_Write_UINT32(s, request->dwFlags); /* dwFlags (4 bytes) */
|
|
|
|
Stream_Write_UINT32(s, request->nPositionLow); /* nPositionLow (4 bytes) */
|
|
|
|
Stream_Write_UINT32(s, request->nPositionHigh); /* nPositionHigh (4 bytes) */
|
|
|
|
Stream_Write_UINT32(s, request->cbRequested); /* cbRequested (4 bytes) */
|
|
|
|
|
|
|
|
if (request->haveClipDataId)
|
|
|
|
Stream_Write_UINT32(s, request->clipDataId); /* clipDataId (4 bytes) */
|
|
|
|
}
|
|
|
|
|
2019-10-17 18:59:07 +03:00
|
|
|
static INLINE void cliprdr_write_lock_unlock_clipdata(wStream* s, UINT32 clipDataId)
|
2019-09-15 13:53:56 +03:00
|
|
|
{
|
2019-09-15 14:48:12 +03:00
|
|
|
Stream_Write_UINT32(s, clipDataId);
|
2019-09-15 13:53:56 +03:00
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
static void cliprdr_write_lock_clipdata(wStream* s,
|
|
|
|
const CLIPRDR_LOCK_CLIPBOARD_DATA* lockClipboardData)
|
2019-09-15 13:53:56 +03:00
|
|
|
{
|
2019-10-17 18:59:07 +03:00
|
|
|
cliprdr_write_lock_unlock_clipdata(s, lockClipboardData->clipDataId);
|
2019-09-15 13:53:56 +03:00
|
|
|
}
|
|
|
|
|
2019-10-17 18:59:07 +03:00
|
|
|
static void cliprdr_write_unlock_clipdata(wStream* s,
|
2019-11-06 17:24:51 +03:00
|
|
|
const CLIPRDR_UNLOCK_CLIPBOARD_DATA* unlockClipboardData)
|
2019-09-15 13:53:56 +03:00
|
|
|
{
|
2019-10-17 18:59:07 +03:00
|
|
|
cliprdr_write_lock_unlock_clipdata(s, unlockClipboardData->clipDataId);
|
2019-09-15 13:53:56 +03:00
|
|
|
}
|
|
|
|
|
2019-10-17 18:59:07 +03:00
|
|
|
static void cliprdr_write_file_contents_response(wStream* s,
|
2019-11-06 17:24:51 +03:00
|
|
|
const CLIPRDR_FILE_CONTENTS_RESPONSE* response)
|
2019-09-15 13:53:56 +03:00
|
|
|
{
|
|
|
|
Stream_Write_UINT32(s, response->streamId); /* streamId (4 bytes) */
|
|
|
|
Stream_Write(s, response->requestedData, response->cbRequested);
|
|
|
|
}
|
|
|
|
|
2019-10-17 18:59:07 +03:00
|
|
|
wStream* cliprdr_packet_lock_clipdata_new(const CLIPRDR_LOCK_CLIPBOARD_DATA* lockClipboardData)
|
|
|
|
{
|
|
|
|
wStream* s;
|
|
|
|
|
|
|
|
if (!lockClipboardData)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
s = cliprdr_packet_new(CB_LOCK_CLIPDATA, 0, 4);
|
|
|
|
|
|
|
|
if (!s)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
cliprdr_write_lock_clipdata(s, lockClipboardData);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
wStream*
|
|
|
|
cliprdr_packet_unlock_clipdata_new(const CLIPRDR_UNLOCK_CLIPBOARD_DATA* unlockClipboardData)
|
2019-10-17 18:59:07 +03:00
|
|
|
{
|
|
|
|
wStream* s;
|
|
|
|
|
|
|
|
if (!unlockClipboardData)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
s = cliprdr_packet_new(CB_LOCK_CLIPDATA, 0, 4);
|
|
|
|
|
|
|
|
if (!s)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
cliprdr_write_unlock_clipdata(s, unlockClipboardData);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
wStream* cliprdr_packet_file_contents_request_new(const CLIPRDR_FILE_CONTENTS_REQUEST* request)
|
|
|
|
{
|
|
|
|
wStream* s;
|
|
|
|
|
|
|
|
if (!request)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
s = cliprdr_packet_new(CB_FILECONTENTS_REQUEST, 0, 28);
|
|
|
|
|
|
|
|
if (!s)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
cliprdr_write_file_contents_request(s, request);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
wStream* cliprdr_packet_file_contents_response_new(const CLIPRDR_FILE_CONTENTS_RESPONSE* response)
|
|
|
|
{
|
|
|
|
wStream* s;
|
|
|
|
|
|
|
|
if (!response)
|
|
|
|
return NULL;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
s = cliprdr_packet_new(CB_FILECONTENTS_RESPONSE, response->msgFlags, 4 + response->cbRequested);
|
2019-10-17 18:59:07 +03:00
|
|
|
|
|
|
|
if (!s)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
cliprdr_write_file_contents_response(s, response);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
wStream* cliprdr_packet_format_list_new(const CLIPRDR_FORMAT_LIST* formatList,
|
|
|
|
BOOL useLongFormatNames)
|
2019-10-17 18:59:07 +03:00
|
|
|
{
|
|
|
|
wStream* s;
|
|
|
|
UINT32 index;
|
|
|
|
int cchWideChar;
|
|
|
|
LPWSTR lpWideCharStr;
|
|
|
|
int formatNameSize;
|
|
|
|
char* szFormatName;
|
|
|
|
WCHAR* wszFormatName;
|
|
|
|
BOOL asciiNames = FALSE;
|
|
|
|
CLIPRDR_FORMAT* format;
|
2020-07-24 19:16:18 +03:00
|
|
|
UINT32 length;
|
2019-10-17 18:59:07 +03:00
|
|
|
|
|
|
|
if (formatList->msgType != CB_FORMAT_LIST)
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_WARN(TAG, "[%s] called with invalid type %08" PRIx32, __FUNCTION__,
|
|
|
|
formatList->msgType);
|
2019-10-17 18:59:07 +03:00
|
|
|
|
|
|
|
if (!useLongFormatNames)
|
|
|
|
{
|
2020-07-24 19:16:18 +03:00
|
|
|
length = formatList->numFormats * 36;
|
2019-10-17 18:59:07 +03:00
|
|
|
s = cliprdr_packet_new(CB_FORMAT_LIST, 0, length);
|
|
|
|
|
|
|
|
if (!s)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "cliprdr_packet_new failed!");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (index = 0; index < formatList->numFormats; index++)
|
|
|
|
{
|
|
|
|
size_t formatNameLength = 0;
|
2019-11-06 17:24:51 +03:00
|
|
|
format = (CLIPRDR_FORMAT*)&(formatList->formats[index]);
|
2019-10-17 18:59:07 +03:00
|
|
|
Stream_Write_UINT32(s, format->formatId); /* formatId (4 bytes) */
|
|
|
|
formatNameSize = 0;
|
|
|
|
|
|
|
|
szFormatName = format->formatName;
|
|
|
|
|
|
|
|
if (asciiNames)
|
|
|
|
{
|
|
|
|
if (szFormatName)
|
2019-10-29 12:18:09 +03:00
|
|
|
formatNameLength = strnlen(szFormatName, 32);
|
2019-10-17 18:59:07 +03:00
|
|
|
|
|
|
|
if (formatNameLength > 31)
|
|
|
|
formatNameLength = 31;
|
|
|
|
|
|
|
|
Stream_Write(s, szFormatName, formatNameLength);
|
|
|
|
Stream_Zero(s, 32 - formatNameLength);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wszFormatName = NULL;
|
|
|
|
|
|
|
|
if (szFormatName)
|
2019-11-06 17:24:51 +03:00
|
|
|
formatNameSize =
|
|
|
|
ConvertToUnicode(CP_UTF8, 0, szFormatName, -1, &wszFormatName, 0);
|
2019-10-17 18:59:07 +03:00
|
|
|
|
|
|
|
if (formatNameSize < 0)
|
2020-07-24 19:16:18 +03:00
|
|
|
{
|
|
|
|
Stream_Free(s, TRUE);
|
2019-10-17 18:59:07 +03:00
|
|
|
return NULL;
|
2020-07-24 19:16:18 +03:00
|
|
|
}
|
2019-10-17 18:59:07 +03:00
|
|
|
|
|
|
|
if (formatNameSize > 15)
|
|
|
|
formatNameSize = 15;
|
|
|
|
|
|
|
|
/* size in bytes instead of wchar */
|
|
|
|
formatNameSize *= 2;
|
|
|
|
|
|
|
|
if (wszFormatName)
|
|
|
|
Stream_Write(s, wszFormatName, (size_t)formatNameSize);
|
|
|
|
|
|
|
|
Stream_Zero(s, (size_t)(32 - formatNameSize));
|
|
|
|
free(wszFormatName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-07-24 19:16:18 +03:00
|
|
|
length = 0;
|
2019-10-17 18:59:07 +03:00
|
|
|
for (index = 0; index < formatList->numFormats; index++)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
format = (CLIPRDR_FORMAT*)&(formatList->formats[index]);
|
2019-10-17 18:59:07 +03:00
|
|
|
length += 4;
|
|
|
|
formatNameSize = 2;
|
|
|
|
|
|
|
|
if (format->formatName)
|
2019-11-06 17:24:51 +03:00
|
|
|
formatNameSize =
|
|
|
|
MultiByteToWideChar(CP_UTF8, 0, format->formatName, -1, NULL, 0) * 2;
|
2019-10-17 18:59:07 +03:00
|
|
|
|
|
|
|
if (formatNameSize < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
length += (UINT32)formatNameSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
s = cliprdr_packet_new(CB_FORMAT_LIST, 0, length);
|
|
|
|
|
|
|
|
if (!s)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "cliprdr_packet_new failed!");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (index = 0; index < formatList->numFormats; index++)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
format = (CLIPRDR_FORMAT*)&(formatList->formats[index]);
|
2019-10-17 18:59:07 +03:00
|
|
|
Stream_Write_UINT32(s, format->formatId); /* formatId (4 bytes) */
|
|
|
|
|
|
|
|
if (format->formatName)
|
|
|
|
{
|
|
|
|
const size_t cap = Stream_Capacity(s);
|
|
|
|
const size_t pos = Stream_GetPosition(s);
|
|
|
|
const size_t rem = cap - pos;
|
|
|
|
if ((cap < pos) || ((rem / 2) > INT_MAX))
|
2020-07-24 19:16:18 +03:00
|
|
|
{
|
|
|
|
Stream_Free(s, TRUE);
|
2019-10-17 18:59:07 +03:00
|
|
|
return NULL;
|
2020-07-24 19:16:18 +03:00
|
|
|
}
|
2019-10-17 18:59:07 +03:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
lpWideCharStr = (LPWSTR)Stream_Pointer(s);
|
2019-10-17 18:59:07 +03:00
|
|
|
cchWideChar = (int)(rem / 2);
|
2019-11-06 17:24:51 +03:00
|
|
|
formatNameSize = MultiByteToWideChar(CP_UTF8, 0, format->formatName, -1,
|
|
|
|
lpWideCharStr, cchWideChar) *
|
|
|
|
2;
|
2019-10-17 18:59:07 +03:00
|
|
|
if (formatNameSize < 0)
|
2020-07-24 19:16:18 +03:00
|
|
|
{
|
|
|
|
Stream_Free(s, TRUE);
|
2019-10-17 18:59:07 +03:00
|
|
|
return NULL;
|
2020-07-24 19:16:18 +03:00
|
|
|
}
|
2019-10-17 18:59:07 +03:00
|
|
|
Stream_Seek(s, (size_t)formatNameSize);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Stream_Write_UINT16(s, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
2019-09-15 13:53:56 +03:00
|
|
|
UINT cliprdr_read_unlock_clipdata(wStream* s, CLIPRDR_UNLOCK_CLIPBOARD_DATA* unlockClipboardData)
|
|
|
|
{
|
2019-09-15 14:48:12 +03:00
|
|
|
if (Stream_GetRemainingLength(s) < 4)
|
2019-09-15 13:53:56 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "not enough remaining data");
|
|
|
|
return ERROR_INVALID_DATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
Stream_Read_UINT32(s, unlockClipboardData->clipDataId); /* clipDataId (4 bytes) */
|
2019-09-15 14:48:12 +03:00
|
|
|
return CHANNEL_RC_OK;
|
2019-09-15 13:53:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
UINT cliprdr_read_format_data_request(wStream* s, CLIPRDR_FORMAT_DATA_REQUEST* request)
|
|
|
|
{
|
|
|
|
if (Stream_GetRemainingLength(s) < 4)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "not enough data in stream!");
|
|
|
|
return ERROR_INVALID_DATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
Stream_Read_UINT32(s, request->requestedFormatId); /* requestedFormatId (4 bytes) */
|
2019-09-15 14:48:12 +03:00
|
|
|
return CHANNEL_RC_OK;
|
2019-09-15 13:53:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
UINT cliprdr_read_format_data_response(wStream* s, CLIPRDR_FORMAT_DATA_RESPONSE* response)
|
|
|
|
{
|
2019-09-15 14:48:12 +03:00
|
|
|
response->requestedFormatData = NULL;
|
2019-09-15 13:53:56 +03:00
|
|
|
|
2019-09-15 14:48:12 +03:00
|
|
|
if (Stream_GetRemainingLength(s) < response->dataLen)
|
2019-09-15 13:53:56 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "not enough data in stream!");
|
|
|
|
return ERROR_INVALID_DATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (response->dataLen)
|
|
|
|
response->requestedFormatData = Stream_Pointer(s);
|
|
|
|
|
2019-09-15 14:48:12 +03:00
|
|
|
return CHANNEL_RC_OK;
|
2019-09-15 13:53:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
UINT cliprdr_read_file_contents_request(wStream* s, CLIPRDR_FILE_CONTENTS_REQUEST* request)
|
|
|
|
{
|
2019-09-15 14:48:12 +03:00
|
|
|
if (Stream_GetRemainingLength(s) < 24)
|
2019-09-15 13:53:56 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "not enough remaining data");
|
|
|
|
return ERROR_INVALID_DATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
request->haveClipDataId = FALSE;
|
|
|
|
Stream_Read_UINT32(s, request->streamId); /* streamId (4 bytes) */
|
|
|
|
Stream_Read_UINT32(s, request->listIndex); /* listIndex (4 bytes) */
|
|
|
|
Stream_Read_UINT32(s, request->dwFlags); /* dwFlags (4 bytes) */
|
|
|
|
Stream_Read_UINT32(s, request->nPositionLow); /* nPositionLow (4 bytes) */
|
|
|
|
Stream_Read_UINT32(s, request->nPositionHigh); /* nPositionHigh (4 bytes) */
|
|
|
|
Stream_Read_UINT32(s, request->cbRequested); /* cbRequested (4 bytes) */
|
|
|
|
|
|
|
|
if (Stream_GetRemainingLength(s) >= 4)
|
|
|
|
{
|
|
|
|
Stream_Read_UINT32(s, request->clipDataId); /* clipDataId (4 bytes) */
|
|
|
|
request->haveClipDataId = TRUE;
|
|
|
|
}
|
|
|
|
|
2019-11-12 14:22:45 +03:00
|
|
|
if (!cliprdr_validate_file_contents_request(request))
|
|
|
|
return ERROR_BAD_ARGUMENTS;
|
|
|
|
|
2019-09-15 13:53:56 +03:00
|
|
|
return CHANNEL_RC_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
UINT cliprdr_read_file_contents_response(wStream* s, CLIPRDR_FILE_CONTENTS_RESPONSE* response)
|
|
|
|
{
|
|
|
|
if (Stream_GetRemainingLength(s) < 4)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "not enough remaining data");
|
|
|
|
return ERROR_INVALID_DATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
Stream_Read_UINT32(s, response->streamId); /* streamId (4 bytes) */
|
|
|
|
response->requestedData = Stream_Pointer(s); /* requestedFileContentsData */
|
|
|
|
response->cbRequested = response->dataLen - 4;
|
|
|
|
return CHANNEL_RC_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
UINT cliprdr_read_format_list(wStream* s, CLIPRDR_FORMAT_LIST* formatList, BOOL useLongFormatNames)
|
|
|
|
{
|
2019-09-15 14:48:12 +03:00
|
|
|
UINT32 index;
|
2019-09-15 13:53:56 +03:00
|
|
|
BOOL asciiNames;
|
|
|
|
int formatNameLength;
|
|
|
|
char* szFormatName;
|
|
|
|
WCHAR* wszFormatName;
|
2020-04-15 17:47:12 +03:00
|
|
|
wStream sub1, sub2;
|
2019-09-15 13:53:56 +03:00
|
|
|
CLIPRDR_FORMAT* formats = NULL;
|
2020-04-20 16:26:33 +03:00
|
|
|
UINT error = ERROR_INTERNAL_ERROR;
|
2019-09-15 13:53:56 +03:00
|
|
|
|
|
|
|
asciiNames = (formatList->msgFlags & CB_ASCII_NAMES) ? TRUE : FALSE;
|
|
|
|
|
|
|
|
index = 0;
|
2020-04-15 17:47:12 +03:00
|
|
|
/* empty format list */
|
|
|
|
formatList->formats = NULL;
|
2019-09-15 13:53:56 +03:00
|
|
|
formatList->numFormats = 0;
|
2020-04-15 17:47:12 +03:00
|
|
|
|
|
|
|
Stream_StaticInit(&sub1, Stream_Pointer(s), formatList->dataLen);
|
|
|
|
if (!Stream_SafeSeek(s, formatList->dataLen))
|
|
|
|
return ERROR_INVALID_DATA;
|
2019-09-15 13:53:56 +03:00
|
|
|
|
|
|
|
if (!formatList->dataLen)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
else if (!useLongFormatNames)
|
|
|
|
{
|
2020-04-15 17:47:12 +03:00
|
|
|
const size_t cap = Stream_Capacity(&sub1);
|
|
|
|
formatList->numFormats = (cap / 36);
|
2019-09-15 13:53:56 +03:00
|
|
|
|
2020-04-15 17:47:12 +03:00
|
|
|
if ((formatList->numFormats * 36) != cap)
|
2019-09-15 13:53:56 +03:00
|
|
|
{
|
2020-04-15 17:47:12 +03:00
|
|
|
WLog_ERR(TAG, "Invalid short format list length: %" PRIuz "", cap);
|
2019-09-15 13:53:56 +03:00
|
|
|
return ERROR_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (formatList->numFormats)
|
2019-09-15 14:48:12 +03:00
|
|
|
formats = (CLIPRDR_FORMAT*)calloc(formatList->numFormats, sizeof(CLIPRDR_FORMAT));
|
2019-09-15 13:53:56 +03:00
|
|
|
|
|
|
|
if (!formats)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "calloc failed!");
|
|
|
|
return CHANNEL_RC_NO_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
formatList->formats = formats;
|
|
|
|
|
2020-04-15 17:47:12 +03:00
|
|
|
while (Stream_GetRemainingLength(&sub1) >= 4)
|
2019-09-15 13:53:56 +03:00
|
|
|
{
|
2020-04-15 17:47:12 +03:00
|
|
|
Stream_Read_UINT32(&sub1, formats[index].formatId); /* formatId (4 bytes) */
|
2019-09-15 13:53:56 +03:00
|
|
|
|
|
|
|
formats[index].formatName = NULL;
|
|
|
|
|
|
|
|
/* According to MS-RDPECLIP 2.2.3.1.1.1 formatName is "a 32-byte block containing
|
|
|
|
* the *null-terminated* name assigned to the Clipboard Format: (32 ASCII 8 characters
|
|
|
|
* or 16 Unicode characters)"
|
|
|
|
* However, both Windows RDSH and mstsc violate this specs as seen in the following
|
|
|
|
* example of a transferred short format name string: [R.i.c.h. .T.e.x.t. .F.o.r.m.a.t.]
|
|
|
|
* These are 16 unicode charaters - *without* terminating null !
|
|
|
|
*/
|
|
|
|
|
2020-04-15 17:47:12 +03:00
|
|
|
szFormatName = (char*)Stream_Pointer(&sub1);
|
|
|
|
wszFormatName = (WCHAR*)Stream_Pointer(&sub1);
|
|
|
|
if (!Stream_SafeSeek(&sub1, 32))
|
|
|
|
goto error_out;
|
2019-09-15 13:53:56 +03:00
|
|
|
if (asciiNames)
|
|
|
|
{
|
|
|
|
if (szFormatName[0])
|
|
|
|
{
|
|
|
|
/* ensure null termination */
|
2019-09-15 14:48:12 +03:00
|
|
|
formats[index].formatName = (char*)malloc(32 + 1);
|
2019-09-15 13:53:56 +03:00
|
|
|
if (!formats[index].formatName)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "malloc failed!");
|
|
|
|
error = CHANNEL_RC_NO_MEMORY;
|
|
|
|
goto error_out;
|
|
|
|
}
|
|
|
|
CopyMemory(formats[index].formatName, szFormatName, 32);
|
|
|
|
formats[index].formatName[32] = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (wszFormatName[0])
|
|
|
|
{
|
|
|
|
/* ConvertFromUnicode always returns a null-terminated
|
|
|
|
* string on success, even if the source string isn't.
|
|
|
|
*/
|
|
|
|
if (ConvertFromUnicode(CP_UTF8, 0, wszFormatName, 16,
|
2019-09-15 14:48:12 +03:00
|
|
|
&(formats[index].formatName), 0, NULL, NULL) < 1)
|
2019-09-15 13:53:56 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "failed to convert short clipboard format name");
|
|
|
|
error = ERROR_INTERNAL_ERROR;
|
|
|
|
goto error_out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-04-15 17:47:12 +03:00
|
|
|
sub2 = sub1;
|
|
|
|
while (Stream_GetRemainingLength(&sub1) > 0)
|
2019-09-15 13:53:56 +03:00
|
|
|
{
|
2020-04-15 17:47:12 +03:00
|
|
|
size_t rest;
|
|
|
|
if (!Stream_SafeSeek(&sub1, 4)) /* formatId (4 bytes) */
|
|
|
|
goto error_out;
|
2019-09-15 13:53:56 +03:00
|
|
|
|
2020-04-15 17:47:12 +03:00
|
|
|
wszFormatName = (WCHAR*)Stream_Pointer(&sub1);
|
|
|
|
rest = Stream_GetRemainingLength(&sub1);
|
|
|
|
formatNameLength = _wcsnlen(wszFormatName, rest / sizeof(WCHAR));
|
2019-09-15 13:53:56 +03:00
|
|
|
|
2020-04-15 17:47:12 +03:00
|
|
|
if (!Stream_SafeSeek(&sub1, (formatNameLength + 1) * sizeof(WCHAR)))
|
|
|
|
goto error_out;
|
2019-09-15 13:53:56 +03:00
|
|
|
formatList->numFormats++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (formatList->numFormats)
|
2019-09-15 14:48:12 +03:00
|
|
|
formats = (CLIPRDR_FORMAT*)calloc(formatList->numFormats, sizeof(CLIPRDR_FORMAT));
|
2019-09-15 13:53:56 +03:00
|
|
|
|
|
|
|
if (!formats)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "calloc failed!");
|
|
|
|
return CHANNEL_RC_NO_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
formatList->formats = formats;
|
|
|
|
|
2020-04-15 17:47:12 +03:00
|
|
|
while (Stream_GetRemainingLength(&sub2) >= 4)
|
2019-09-15 13:53:56 +03:00
|
|
|
{
|
2020-04-15 17:47:12 +03:00
|
|
|
size_t rest;
|
|
|
|
Stream_Read_UINT32(&sub2, formats[index].formatId); /* formatId (4 bytes) */
|
2019-09-15 13:53:56 +03:00
|
|
|
|
|
|
|
formats[index].formatName = NULL;
|
|
|
|
|
2020-04-15 17:47:12 +03:00
|
|
|
wszFormatName = (WCHAR*)Stream_Pointer(&sub2);
|
|
|
|
rest = Stream_GetRemainingLength(&sub2);
|
|
|
|
formatNameLength = _wcsnlen(wszFormatName, rest / sizeof(WCHAR));
|
|
|
|
if (!Stream_SafeSeek(&sub2, (formatNameLength + 1) * sizeof(WCHAR)))
|
|
|
|
goto error_out;
|
2019-09-15 13:53:56 +03:00
|
|
|
|
|
|
|
if (formatNameLength)
|
|
|
|
{
|
2020-04-15 17:47:12 +03:00
|
|
|
if (ConvertFromUnicode(CP_UTF8, 0, wszFormatName, formatNameLength,
|
|
|
|
&(formats[index].formatName), 0, NULL, NULL) < 1)
|
2019-09-15 13:53:56 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "failed to convert long clipboard format name");
|
|
|
|
error = ERROR_INTERNAL_ERROR;
|
|
|
|
goto error_out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-20 16:26:33 +03:00
|
|
|
return CHANNEL_RC_OK;
|
2019-09-15 13:53:56 +03:00
|
|
|
|
|
|
|
error_out:
|
|
|
|
cliprdr_free_format_list(formatList);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cliprdr_free_format_list(CLIPRDR_FORMAT_LIST* formatList)
|
|
|
|
{
|
2019-09-15 14:48:12 +03:00
|
|
|
UINT index = 0;
|
2019-09-15 13:53:56 +03:00
|
|
|
|
2019-09-15 14:48:12 +03:00
|
|
|
if (formatList == NULL)
|
|
|
|
return;
|
2019-09-15 13:53:56 +03:00
|
|
|
|
2019-09-15 14:48:12 +03:00
|
|
|
if (formatList->formats)
|
2019-09-15 13:53:56 +03:00
|
|
|
{
|
|
|
|
for (index = 0; index < formatList->numFormats; index++)
|
|
|
|
{
|
|
|
|
free(formatList->formats[index].formatName);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(formatList->formats);
|
2020-04-15 17:47:12 +03:00
|
|
|
formatList->formats = NULL;
|
|
|
|
formatList->numFormats = 0;
|
2019-09-15 13:53:56 +03:00
|
|
|
}
|
|
|
|
}
|