24#include <winpr/stream.h>
25#include <freerdp/channels/log.h>
27#define TAG CHANNELS_TAG("cliprdr.common")
29#include "cliprdr_common.h"
31static const char* CB_MSG_TYPE_STR(UINT32 type)
36 return "CB_TYPE_NONE";
37 case CB_MONITOR_READY:
38 return "CB_MONITOR_READY";
40 return "CB_FORMAT_LIST";
41 case CB_FORMAT_LIST_RESPONSE:
42 return "CB_FORMAT_LIST_RESPONSE";
43 case CB_FORMAT_DATA_REQUEST:
44 return "CB_FORMAT_DATA_REQUEST";
45 case CB_FORMAT_DATA_RESPONSE:
46 return "CB_FORMAT_DATA_RESPONSE";
47 case CB_TEMP_DIRECTORY:
48 return "CB_TEMP_DIRECTORY";
50 return "CB_CLIP_CAPS";
51 case CB_FILECONTENTS_REQUEST:
52 return "CB_FILECONTENTS_REQUEST";
53 case CB_FILECONTENTS_RESPONSE:
54 return "CB_FILECONTENTS_RESPONSE";
55 case CB_LOCK_CLIPDATA:
56 return "CB_LOCK_CLIPDATA";
57 case CB_UNLOCK_CLIPDATA:
58 return "CB_UNLOCK_CLIPDATA";
64const char* CB_MSG_TYPE_STRING(UINT16 type,
char* buffer,
size_t size)
66 (void)_snprintf(buffer, size,
"%s [0x%04" PRIx16
"]", CB_MSG_TYPE_STR(type), type);
70const char* CB_MSG_FLAGS_STRING(UINT16 msgFlags,
char* buffer,
size_t size)
72 if ((msgFlags & CB_RESPONSE_OK) != 0)
73 winpr_str_append(
"CB_RESPONSE_OK", buffer, size,
"|");
74 if ((msgFlags & CB_RESPONSE_FAIL) != 0)
75 winpr_str_append(
"CB_RESPONSE_FAIL", buffer, size,
"|");
76 if ((msgFlags & CB_ASCII_NAMES) != 0)
77 winpr_str_append(
"CB_ASCII_NAMES", buffer, size,
"|");
79 const size_t len = strnlen(buffer, size);
81 winpr_str_append(
"NONE", buffer, size,
"");
83 char val[32] = WINPR_C_ARRAY_INIT;
84 (void)_snprintf(val,
sizeof(val),
"[0x%04" PRIx16
"]", msgFlags);
85 winpr_str_append(val, buffer, size,
"|");
100 if (request->dwFlags & FILECONTENTS_SIZE)
102 if (request->cbRequested !=
sizeof(UINT64))
104 WLog_ERR(TAG,
"cbRequested must be %" PRIuz
", got %" PRIu32
"",
sizeof(UINT64),
105 request->cbRequested);
109 if (request->nPositionHigh != 0 || request->nPositionLow != 0)
111 WLog_ERR(TAG,
"nPositionHigh and nPositionLow must be set to 0");
119wStream* cliprdr_packet_new(UINT16 msgType, UINT16 msgFlags,
size_t dataLen)
121 WINPR_ASSERT(dataLen < UINT32_MAX);
122 wStream* s = Stream_New(
nullptr, dataLen + 8ULL);
126 WLog_ERR(TAG,
"Stream_New failed!");
130 Stream_Write_UINT16(s, msgType);
131 Stream_Write_UINT16(s, msgFlags);
133 Stream_Write_UINT32(s, 0);
137static void cliprdr_write_file_contents_request(
wStream* s,
140 Stream_Write_UINT32(s, request->streamId);
141 Stream_Write_UINT32(s, request->listIndex);
142 Stream_Write_UINT32(s, request->dwFlags);
143 Stream_Write_UINT32(s, request->nPositionLow);
144 Stream_Write_UINT32(s, request->nPositionHigh);
145 Stream_Write_UINT32(s, request->cbRequested);
147 if (request->haveClipDataId)
148 Stream_Write_UINT32(s, request->clipDataId);
151static inline void cliprdr_write_lock_unlock_clipdata(
wStream* s, UINT32 clipDataId)
153 Stream_Write_UINT32(s, clipDataId);
156static void cliprdr_write_lock_clipdata(
wStream* s,
159 cliprdr_write_lock_unlock_clipdata(s, lockClipboardData->clipDataId);
162static void cliprdr_write_unlock_clipdata(
wStream* s,
165 cliprdr_write_lock_unlock_clipdata(s, unlockClipboardData->clipDataId);
168static void cliprdr_write_file_contents_response(
wStream* s,
171 Stream_Write_UINT32(s, response->streamId);
172 Stream_Write(s, response->requestedData, response->cbRequested);
179 if (!lockClipboardData)
182 s = cliprdr_packet_new(CB_LOCK_CLIPDATA, 0, 4);
187 cliprdr_write_lock_clipdata(s, lockClipboardData);
196 if (!unlockClipboardData)
199 s = cliprdr_packet_new(CB_UNLOCK_CLIPDATA, 0, 4);
204 cliprdr_write_unlock_clipdata(s, unlockClipboardData);
215 s = cliprdr_packet_new(CB_FILECONTENTS_REQUEST, 0, 28);
220 cliprdr_write_file_contents_request(s, request);
231 s = cliprdr_packet_new(CB_FILECONTENTS_RESPONSE, response->common.msgFlags,
232 4 + response->cbRequested);
237 cliprdr_write_file_contents_response(s, response);
242 BOOL useLongFormatNames, BOOL useAsciiNames)
244 WINPR_ASSERT(formatList);
246 if (formatList->common.msgType != CB_FORMAT_LIST)
247 WLog_WARN(TAG,
"called with invalid type %08" PRIx32, formatList->common.msgType);
249 if (useLongFormatNames && useAsciiNames)
250 WLog_WARN(TAG,
"called with invalid arguments useLongFormatNames=true && "
251 "useAsciiNames=true. useAsciiNames requires "
252 "useLongFormatNames=false, ignoring argument.");
254 const UINT32 length = formatList->numFormats * 36;
255 const size_t formatNameCharSize =
256 (useLongFormatNames || !useAsciiNames) ?
sizeof(WCHAR) :
sizeof(CHAR);
258 wStream* s = cliprdr_packet_new(CB_FORMAT_LIST, 0, length);
261 WLog_ERR(TAG,
"cliprdr_packet_new failed!");
265 for (UINT32 index = 0; index < formatList->numFormats; index++)
269 const char* szFormatName = format->formatName;
270 size_t formatNameLength = 0;
271 size_t formatNameStrLength = 0;
274 formatNameStrLength = strlen(szFormatName);
275 const SSIZE_T wlen = ConvertUtf8ToWChar(szFormatName,
nullptr, 0);
278 formatNameLength = WINPR_ASSERTING_INT_CAST(
size_t, wlen);
281 size_t formatNameMaxLength = formatNameLength + 1;
282 if (!Stream_EnsureRemainingCapacity(s,
283 4 + MAX(32, formatNameMaxLength * formatNameCharSize)))
286 Stream_Write_UINT32(s, format->formatId);
288 if (!useLongFormatNames)
290 formatNameMaxLength = useAsciiNames ? 32 : 16;
291 formatNameLength = MIN(formatNameMaxLength - 1, formatNameLength);
294 if (szFormatName && (formatNameLength > 0))
298 Stream_Write(s, szFormatName, formatNameLength);
299 Stream_Zero(s, formatNameMaxLength - formatNameLength);
303 if (Stream_Write_UTF16_String_From_UTF8(s, formatNameMaxLength, szFormatName,
304 formatNameStrLength, TRUE) < 0)
309 Stream_Zero(s, formatNameMaxLength * formatNameCharSize);
315 Stream_Free(s, TRUE);
321 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
322 return ERROR_INVALID_DATA;
324 Stream_Read_UINT32(s, unlockClipboardData->clipDataId);
325 return CHANNEL_RC_OK;
330 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
331 return ERROR_INVALID_DATA;
333 Stream_Read_UINT32(s, request->requestedFormatId);
334 return CHANNEL_RC_OK;
339 response->requestedFormatData =
nullptr;
341 if (!Stream_CheckAndLogRequiredLength(TAG, s, response->common.dataLen))
342 return ERROR_INVALID_DATA;
344 if (response->common.dataLen > 0)
346 response->requestedFormatData = Stream_ConstPointer(s);
347 if (!Stream_SafeSeek(s, response->common.dataLen))
348 return ERROR_INVALID_DATA;
350 return CHANNEL_RC_OK;
355 if (!Stream_CheckAndLogRequiredLength(TAG, s, 24))
356 return ERROR_INVALID_DATA;
358 request->haveClipDataId = FALSE;
359 Stream_Read_UINT32(s, request->streamId);
360 Stream_Read_UINT32(s, request->listIndex);
361 Stream_Read_UINT32(s, request->dwFlags);
362 Stream_Read_UINT32(s, request->nPositionLow);
363 Stream_Read_UINT32(s, request->nPositionHigh);
364 Stream_Read_UINT32(s, request->cbRequested);
366 if (Stream_GetRemainingLength(s) >= 4)
368 Stream_Read_UINT32(s, request->clipDataId);
369 request->haveClipDataId = TRUE;
372 if (!cliprdr_validate_file_contents_request(request))
373 return ERROR_BAD_ARGUMENTS;
375 return CHANNEL_RC_OK;
380 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
381 return ERROR_INVALID_DATA;
383 Stream_Read_UINT32(s, response->streamId);
384 response->requestedData = Stream_ConstPointer(s);
386 if (response->common.dataLen < 4)
388 WLog_WARN(TAG,
"dataLen=%" PRIu32
" but expected >= 4", response->common.dataLen);
389 return ERROR_INVALID_DATA;
392 response->cbRequested = response->common.dataLen - 4;
393 if (!Stream_CheckAndLogRequiredLength(TAG, s, response->cbRequested))
394 return ERROR_INVALID_DATA;
395 Stream_Seek(s, response->cbRequested);
396 return CHANNEL_RC_OK;
400 BOOL useLongFormatNames)
403 size_t formatNameLength = 0;
404 const char* szFormatName =
nullptr;
405 const WCHAR* wszFormatName =
nullptr;
406 wStream sub1buffer = WINPR_C_ARRAY_INIT;
408 UINT error = ERROR_INTERNAL_ERROR;
410 const BOOL asciiNames = (formatList->common.msgFlags & CB_ASCII_NAMES) != 0;
414 formatList->formats =
nullptr;
415 formatList->numFormats = 0;
418 Stream_StaticConstInit(&sub1buffer, Stream_ConstPointer(s), formatList->common.dataLen);
419 if (!Stream_SafeSeek(s, formatList->common.dataLen))
420 return ERROR_INVALID_DATA;
422 if (!formatList->common.dataLen)
425 else if (!useLongFormatNames)
427 const size_t cap = Stream_Capacity(sub1) / 36ULL;
428 if (cap > UINT32_MAX)
430 WLog_Print(log, WLOG_ERROR,
"Invalid short format list length: %" PRIuz
"", cap);
431 return ERROR_INTERNAL_ERROR;
433 formatList->numFormats = (UINT32)cap;
435 if (formatList->numFormats)
440 WLog_Print(log, WLOG_ERROR,
"calloc failed!");
441 return CHANNEL_RC_NO_MEMORY;
444 formatList->formats = formats;
446 while (Stream_GetRemainingLength(sub1) >= 4)
448 if (index >= formatList->numFormats)
453 Stream_Read_UINT32(sub1, format->formatId);
463 szFormatName = Stream_ConstPointer(sub1);
464 wszFormatName = Stream_ConstPointer(sub1);
465 if (!Stream_SafeSeek(sub1, 32))
468 free(format->formatName);
469 format->formatName =
nullptr;
476 format->formatName = strndup(szFormatName, 31);
477 if (!format->formatName)
479 WLog_Print(log, WLOG_ERROR,
"malloc failed!");
480 error = CHANNEL_RC_NO_MEMORY;
487 if (wszFormatName[0])
489 format->formatName = ConvertWCharNToUtf8Alloc(wszFormatName, 16,
nullptr);
490 if (!format->formatName)
500 wStream sub2buffer = sub1buffer;
503 while (Stream_GetRemainingLength(sub1) > 0)
506 if (!Stream_SafeSeek(sub1, 4))
509 wszFormatName = Stream_ConstPointer(sub1);
510 rest = Stream_GetRemainingLength(sub1);
511 formatNameLength = _wcsnlen(wszFormatName, rest /
sizeof(WCHAR));
513 if (!Stream_SafeSeek(sub1, (formatNameLength + 1) *
sizeof(WCHAR)))
515 formatList->numFormats++;
518 if (formatList->numFormats)
523 WLog_Print(log, WLOG_ERROR,
"calloc failed!");
524 return CHANNEL_RC_NO_MEMORY;
527 formatList->formats = formats;
529 while (Stream_GetRemainingLength(sub2) >= 4)
531 if (index >= formatList->numFormats)
537 Stream_Read_UINT32(sub2, format->formatId);
539 free(format->formatName);
540 format->formatName =
nullptr;
542 wszFormatName = Stream_ConstPointer(sub2);
543 rest = Stream_GetRemainingLength(sub2);
544 formatNameLength = _wcsnlen(wszFormatName, rest /
sizeof(WCHAR));
545 if (!Stream_SafeSeek(sub2, (formatNameLength + 1) *
sizeof(WCHAR)))
548 if (formatNameLength)
551 ConvertWCharNToUtf8Alloc(wszFormatName, formatNameLength,
nullptr);
552 if (!format->formatName)
560 return CHANNEL_RC_OK;
563 cliprdr_free_format_list(formatList);
569 if (formatList ==
nullptr)
572 if (formatList->formats)
574 for (UINT32 index = 0; index < formatList->numFormats; index++)
576 free(formatList->formats[index].formatName);
579 free(formatList->formats);
580 formatList->formats =
nullptr;
581 formatList->numFormats = 0;