2011-07-12 19:06:39 +04:00
|
|
|
/**
|
2012-10-09 07:02:04 +04:00
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
2011-07-12 19:06:39 +04:00
|
|
|
* Clipboard Virtual Channel
|
|
|
|
*
|
|
|
|
* Copyright 2009-2011 Jay Sorg
|
|
|
|
* Copyright 2010-2011 Vic Lee
|
2015-06-02 14:05:10 +03:00
|
|
|
* Copyright 2015 Thincast Technologies GmbH
|
|
|
|
* Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
|
2011-07-12 19:06:39 +04:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2012-08-15 01:09:01 +04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2012-10-09 07:42:01 +04:00
|
|
|
#include <winpr/crt.h>
|
2012-12-17 19:20:25 +04:00
|
|
|
#include <winpr/print.h>
|
2012-10-09 07:42:01 +04:00
|
|
|
|
2011-07-12 19:06:39 +04:00
|
|
|
#include <freerdp/types.h>
|
2011-11-01 09:09:38 +04:00
|
|
|
#include <freerdp/constants.h>
|
2012-10-09 04:33:58 +04:00
|
|
|
#include <freerdp/client/cliprdr.h>
|
2011-07-12 19:06:39 +04:00
|
|
|
|
|
|
|
#include "cliprdr_main.h"
|
|
|
|
#include "cliprdr_format.h"
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
UINT cliprdr_process_format_list(cliprdrPlugin* cliprdr, wStream* s, UINT32 dataLen, UINT16 msgFlags)
|
2011-11-01 09:09:38 +04:00
|
|
|
{
|
2014-11-20 22:28:05 +03:00
|
|
|
UINT32 index;
|
|
|
|
UINT32 position;
|
|
|
|
BOOL asciiNames;
|
|
|
|
int formatNameLength;
|
|
|
|
char* szFormatName;
|
|
|
|
WCHAR* wszFormatName;
|
|
|
|
CLIPRDR_FORMAT* formats = NULL;
|
|
|
|
CLIPRDR_FORMAT_LIST formatList;
|
|
|
|
CliprdrClientContext* context = cliprdr_get_client_interface(cliprdr);
|
2015-08-27 15:25:09 +03:00
|
|
|
UINT error = CHANNEL_RC_OK;
|
2011-11-01 09:09:38 +04:00
|
|
|
|
2014-11-20 22:28:05 +03:00
|
|
|
if (!context->custom)
|
2015-06-02 14:05:10 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "context->custom not set!");
|
|
|
|
return ERROR_INTERNAL_ERROR;
|
|
|
|
}
|
2015-03-23 17:58:16 +03:00
|
|
|
|
2014-11-20 22:28:05 +03:00
|
|
|
asciiNames = (msgFlags & CB_ASCII_NAMES) ? TRUE : FALSE;
|
2015-03-23 17:58:16 +03:00
|
|
|
|
2014-11-20 22:28:05 +03:00
|
|
|
formatList.msgType = CB_FORMAT_LIST;
|
|
|
|
formatList.msgFlags = msgFlags;
|
|
|
|
formatList.dataLen = dataLen;
|
2011-11-01 09:09:38 +04:00
|
|
|
|
2014-11-20 22:28:05 +03:00
|
|
|
index = 0;
|
|
|
|
formatList.numFormats = 0;
|
|
|
|
position = Stream_GetPosition(s);
|
2015-03-23 17:58:16 +03:00
|
|
|
|
2014-12-20 21:07:30 +03:00
|
|
|
if (!formatList.dataLen)
|
|
|
|
{
|
|
|
|
/* empty format list */
|
|
|
|
formatList.formats = NULL;
|
|
|
|
formatList.numFormats = 0;
|
|
|
|
}
|
|
|
|
else if (!cliprdr->useLongFormatNames)
|
2011-11-01 09:09:38 +04:00
|
|
|
{
|
2014-11-20 22:28:05 +03:00
|
|
|
formatList.numFormats = (dataLen / 36);
|
2015-03-23 17:58:16 +03:00
|
|
|
|
2014-11-20 22:28:05 +03:00
|
|
|
if ((formatList.numFormats * 36) != dataLen)
|
2011-12-22 02:02:53 +04:00
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
WLog_ERR(TAG, "Invalid short format list length: %"PRIu32"", dataLen);
|
2015-06-02 14:05:10 +03:00
|
|
|
return ERROR_INTERNAL_ERROR;
|
2011-12-22 02:02:53 +04:00
|
|
|
}
|
2015-03-23 17:58:16 +03:00
|
|
|
|
2014-11-20 22:28:05 +03:00
|
|
|
if (formatList.numFormats)
|
|
|
|
formats = (CLIPRDR_FORMAT*) calloc(formatList.numFormats, sizeof(CLIPRDR_FORMAT));
|
2015-03-23 17:58:16 +03:00
|
|
|
|
2014-11-20 22:28:05 +03:00
|
|
|
if (!formats)
|
2015-06-02 14:05:10 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "calloc failed!");
|
|
|
|
return CHANNEL_RC_NO_MEMORY;
|
|
|
|
}
|
2015-03-23 17:58:16 +03:00
|
|
|
|
2014-11-20 22:28:05 +03:00
|
|
|
formatList.formats = formats;
|
2015-03-23 17:58:16 +03:00
|
|
|
|
2014-11-20 22:28:05 +03:00
|
|
|
while (dataLen)
|
2011-12-22 02:02:53 +04:00
|
|
|
{
|
2014-11-20 22:28:05 +03:00
|
|
|
Stream_Read_UINT32(s, formats[index].formatId); /* formatId (4 bytes) */
|
|
|
|
dataLen -= 4;
|
2015-03-23 17:58:16 +03:00
|
|
|
|
2014-11-20 22:28:05 +03:00
|
|
|
formats[index].formatName = NULL;
|
2015-03-23 17:58:16 +03:00
|
|
|
|
2016-03-03 18:21:12 +03:00
|
|
|
/* 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 !
|
|
|
|
*/
|
|
|
|
|
2014-11-20 22:28:05 +03:00
|
|
|
if (asciiNames)
|
|
|
|
{
|
|
|
|
szFormatName = (char*) Stream_Pointer(s);
|
2015-03-23 17:58:16 +03:00
|
|
|
|
2014-11-21 00:05:13 +03:00
|
|
|
if (szFormatName[0])
|
2014-11-20 22:28:05 +03:00
|
|
|
{
|
2016-03-03 18:21:12 +03:00
|
|
|
/* ensure null termination */
|
2014-11-21 00:05:13 +03:00
|
|
|
formats[index].formatName = (char*) malloc(32 + 1);
|
2015-06-02 14:05:10 +03:00
|
|
|
if (!formats[index].formatName)
|
|
|
|
{
|
2016-03-03 18:21:12 +03:00
|
|
|
WLog_ERR(TAG, "malloc failed!");
|
2015-06-02 14:05:10 +03:00
|
|
|
error = CHANNEL_RC_NO_MEMORY;
|
|
|
|
goto error_out;
|
|
|
|
}
|
2014-11-21 00:05:13 +03:00
|
|
|
CopyMemory(formats[index].formatName, szFormatName, 32);
|
|
|
|
formats[index].formatName[32] = '\0';
|
2014-11-20 22:28:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wszFormatName = (WCHAR*) Stream_Pointer(s);
|
2015-03-23 17:58:16 +03:00
|
|
|
|
2014-11-21 00:05:13 +03:00
|
|
|
if (wszFormatName[0])
|
2014-11-20 22:28:05 +03:00
|
|
|
{
|
2016-03-03 18:21:12 +03:00
|
|
|
/* ConvertFromUnicode always returns a null-terminated
|
|
|
|
* string on success, even if the source string isn't.
|
|
|
|
*/
|
|
|
|
if (ConvertFromUnicode(CP_UTF8, 0, wszFormatName, 16,
|
|
|
|
&(formats[index].formatName), 0, NULL, NULL) < 1)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "failed to convert short clipboard format name");
|
|
|
|
error = ERROR_INTERNAL_ERROR;
|
|
|
|
goto error_out;
|
|
|
|
}
|
2014-11-20 22:28:05 +03:00
|
|
|
}
|
|
|
|
}
|
2015-03-23 17:58:16 +03:00
|
|
|
|
2014-11-20 22:28:05 +03:00
|
|
|
Stream_Seek(s, 32);
|
|
|
|
dataLen -= 32;
|
|
|
|
index++;
|
2011-12-22 02:02:53 +04:00
|
|
|
}
|
2011-11-01 09:09:38 +04:00
|
|
|
}
|
2014-11-20 22:28:05 +03:00
|
|
|
else
|
2011-09-23 07:37:17 +04:00
|
|
|
{
|
2013-11-24 06:35:44 +04:00
|
|
|
while (dataLen)
|
|
|
|
{
|
2014-11-20 22:28:05 +03:00
|
|
|
Stream_Seek(s, 4); /* formatId (4 bytes) */
|
2013-11-24 06:35:44 +04:00
|
|
|
dataLen -= 4;
|
2015-03-23 17:58:16 +03:00
|
|
|
|
2014-11-20 22:28:05 +03:00
|
|
|
wszFormatName = (WCHAR*) Stream_Pointer(s);
|
2015-03-23 17:58:16 +03:00
|
|
|
|
2014-11-20 22:28:05 +03:00
|
|
|
if (!wszFormatName[0])
|
|
|
|
formatNameLength = 0;
|
|
|
|
else
|
|
|
|
formatNameLength = _wcslen(wszFormatName);
|
2015-03-23 17:58:16 +03:00
|
|
|
|
2013-11-24 06:35:44 +04:00
|
|
|
Stream_Seek(s, (formatNameLength + 1) * 2);
|
|
|
|
dataLen -= ((formatNameLength + 1) * 2);
|
2015-03-23 17:58:16 +03:00
|
|
|
|
2014-10-17 05:45:47 +04:00
|
|
|
formatList.numFormats++;
|
2013-11-24 06:35:44 +04:00
|
|
|
}
|
2011-11-18 04:51:30 +04:00
|
|
|
|
2013-11-24 06:35:44 +04:00
|
|
|
dataLen = formatList.dataLen;
|
2014-11-20 22:28:05 +03:00
|
|
|
Stream_SetPosition(s, position);
|
2011-09-23 07:37:17 +04:00
|
|
|
|
2014-11-17 01:44:10 +03:00
|
|
|
if (formatList.numFormats)
|
|
|
|
formats = (CLIPRDR_FORMAT*) calloc(formatList.numFormats, sizeof(CLIPRDR_FORMAT));
|
2015-03-23 17:58:16 +03:00
|
|
|
|
2014-11-16 18:01:08 +03:00
|
|
|
if (!formats)
|
2015-06-02 14:05:10 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "calloc failed!");
|
|
|
|
return CHANNEL_RC_NO_MEMORY;
|
|
|
|
}
|
2014-11-16 18:01:08 +03:00
|
|
|
|
2013-11-24 06:35:44 +04:00
|
|
|
formatList.formats = formats;
|
|
|
|
|
|
|
|
while (dataLen)
|
|
|
|
{
|
2014-11-20 22:28:05 +03:00
|
|
|
Stream_Read_UINT32(s, formats[index].formatId); /* formatId (4 bytes) */
|
2013-11-24 06:35:44 +04:00
|
|
|
dataLen -= 4;
|
|
|
|
|
|
|
|
formats[index].formatName = NULL;
|
2015-03-23 17:58:16 +03:00
|
|
|
|
2014-11-20 22:28:05 +03:00
|
|
|
wszFormatName = (WCHAR*) Stream_Pointer(s);
|
2015-03-23 17:58:16 +03:00
|
|
|
|
2014-11-20 22:28:05 +03:00
|
|
|
if (!wszFormatName[0])
|
|
|
|
formatNameLength = 0;
|
|
|
|
else
|
|
|
|
formatNameLength = _wcslen(wszFormatName);
|
2013-11-24 06:35:44 +04:00
|
|
|
|
|
|
|
if (formatNameLength)
|
|
|
|
{
|
2016-03-03 18:21:12 +03:00
|
|
|
if (ConvertFromUnicode(CP_UTF8, 0, wszFormatName, -1,
|
|
|
|
&(formats[index].formatName), 0, NULL, NULL) < 1)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "failed to convert long clipboard format name");
|
|
|
|
error = ERROR_INTERNAL_ERROR;
|
|
|
|
goto error_out;
|
|
|
|
}
|
2013-11-24 06:35:44 +04:00
|
|
|
}
|
2015-03-23 17:58:16 +03:00
|
|
|
|
2014-11-20 22:28:05 +03:00
|
|
|
Stream_Seek(s, (formatNameLength + 1) * 2);
|
|
|
|
dataLen -= ((formatNameLength + 1) * 2);
|
2013-11-24 06:35:44 +04:00
|
|
|
|
|
|
|
index++;
|
|
|
|
}
|
2014-11-20 22:28:05 +03:00
|
|
|
}
|
2013-11-24 06:35:44 +04:00
|
|
|
|
2016-12-14 00:47:08 +03:00
|
|
|
WLog_Print(cliprdr->log, WLOG_DEBUG, "ServerFormatList: numFormats: %"PRIu32"",
|
2014-11-20 22:28:05 +03:00
|
|
|
formatList.numFormats);
|
2014-10-17 05:45:47 +04:00
|
|
|
|
2014-11-20 22:28:05 +03:00
|
|
|
if (context->ServerFormatList)
|
2015-06-02 14:05:10 +03:00
|
|
|
{
|
|
|
|
if ((error = context->ServerFormatList(context, &formatList)))
|
2016-12-14 00:47:08 +03:00
|
|
|
WLog_ERR(TAG, "ServerFormatList failed with error %"PRIu32"", error);
|
2015-06-02 14:05:10 +03:00
|
|
|
}
|
2013-11-24 06:35:44 +04:00
|
|
|
|
2015-06-02 14:05:10 +03:00
|
|
|
error_out:
|
2015-03-23 17:58:16 +03:00
|
|
|
if (formats)
|
2014-11-20 22:28:05 +03:00
|
|
|
{
|
2015-03-23 17:58:16 +03:00
|
|
|
for (index = 0; index < formatList.numFormats; index++)
|
|
|
|
{
|
2014-11-20 22:28:05 +03:00
|
|
|
free(formats[index].formatName);
|
2015-03-23 17:58:16 +03:00
|
|
|
}
|
2014-11-20 22:28:05 +03:00
|
|
|
|
2015-03-23 17:58:16 +03:00
|
|
|
free(formats);
|
|
|
|
}
|
2015-06-02 14:05:10 +03:00
|
|
|
return error;
|
2011-07-12 19:53:54 +04:00
|
|
|
}
|
2011-07-12 20:30:25 +04:00
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
UINT cliprdr_process_format_list_response(cliprdrPlugin* cliprdr, wStream* s, UINT32 dataLen, UINT16 msgFlags)
|
2011-09-23 07:37:17 +04:00
|
|
|
{
|
2014-11-20 22:28:05 +03:00
|
|
|
CLIPRDR_FORMAT_LIST_RESPONSE formatListResponse;
|
2013-11-24 06:35:44 +04:00
|
|
|
CliprdrClientContext* context = cliprdr_get_client_interface(cliprdr);
|
2015-08-27 15:25:09 +03:00
|
|
|
UINT error = CHANNEL_RC_OK;
|
2013-11-24 06:35:44 +04:00
|
|
|
|
2014-10-16 06:48:18 +04:00
|
|
|
WLog_Print(cliprdr->log, WLOG_DEBUG, "ServerFormatListResponse");
|
|
|
|
|
2014-11-20 22:28:05 +03:00
|
|
|
if (!context->custom)
|
2015-06-02 14:05:10 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "context->custom not set!");
|
|
|
|
return ERROR_INTERNAL_ERROR;
|
|
|
|
}
|
2013-11-24 06:35:44 +04:00
|
|
|
|
2014-11-20 22:28:05 +03:00
|
|
|
formatListResponse.msgType = CB_FORMAT_LIST_RESPONSE;
|
|
|
|
formatListResponse.msgFlags = msgFlags;
|
|
|
|
formatListResponse.dataLen = dataLen;
|
2013-11-24 06:35:44 +04:00
|
|
|
|
2015-06-02 14:05:10 +03:00
|
|
|
IFCALLRET(context->ServerFormatListResponse, error, context, &formatListResponse);
|
|
|
|
if (error)
|
2016-12-14 00:47:08 +03:00
|
|
|
WLog_ERR(TAG, "ServerFormatListResponse failed with error %"PRIu32"!", error);
|
2015-06-02 14:05:10 +03:00
|
|
|
|
|
|
|
return error;
|
2011-09-23 07:37:17 +04:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
UINT cliprdr_process_format_data_request(cliprdrPlugin* cliprdr, wStream* s, UINT32 dataLen, UINT16 msgFlags)
|
2011-07-12 20:30:25 +04:00
|
|
|
{
|
2014-11-20 22:28:05 +03:00
|
|
|
CLIPRDR_FORMAT_DATA_REQUEST formatDataRequest;
|
2013-11-24 08:45:31 +04:00
|
|
|
CliprdrClientContext* context = cliprdr_get_client_interface(cliprdr);
|
2015-08-27 15:25:09 +03:00
|
|
|
UINT error = CHANNEL_RC_OK;
|
2013-11-24 08:45:31 +04:00
|
|
|
|
2014-10-16 06:48:18 +04:00
|
|
|
WLog_Print(cliprdr->log, WLOG_DEBUG, "ServerFormatDataRequest");
|
|
|
|
|
2014-11-20 22:28:05 +03:00
|
|
|
if (!context->custom)
|
2015-06-02 14:05:10 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "context->custom not set!");
|
|
|
|
return ERROR_INTERNAL_ERROR;
|
|
|
|
}
|
2013-11-24 08:45:31 +04:00
|
|
|
|
2014-11-20 22:28:05 +03:00
|
|
|
formatDataRequest.msgType = CB_FORMAT_DATA_REQUEST;
|
|
|
|
formatDataRequest.msgFlags = msgFlags;
|
|
|
|
formatDataRequest.dataLen = dataLen;
|
2013-11-24 08:45:31 +04:00
|
|
|
|
2014-11-20 22:28:05 +03:00
|
|
|
Stream_Read_UINT32(s, formatDataRequest.requestedFormatId); /* requestedFormatId (4 bytes) */
|
2011-07-12 20:30:25 +04:00
|
|
|
|
2015-06-02 14:05:10 +03:00
|
|
|
|
|
|
|
IFCALLRET(context->ServerFormatDataRequest, error, context, &formatDataRequest);
|
|
|
|
if (error)
|
2016-12-14 00:47:08 +03:00
|
|
|
WLog_ERR(TAG, "ServerFormatDataRequest failed with error %"PRIu32"!", error);
|
2015-06-02 14:05:10 +03:00
|
|
|
|
|
|
|
return error;
|
2011-07-12 20:50:00 +04:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
UINT cliprdr_process_format_data_response(cliprdrPlugin* cliprdr, wStream* s, UINT32 dataLen, UINT16 msgFlags)
|
2011-07-12 20:50:00 +04:00
|
|
|
{
|
2014-11-20 22:28:05 +03:00
|
|
|
CLIPRDR_FORMAT_DATA_RESPONSE formatDataResponse;
|
2013-11-25 05:46:56 +04:00
|
|
|
CliprdrClientContext* context = cliprdr_get_client_interface(cliprdr);
|
2015-08-27 15:25:09 +03:00
|
|
|
UINT error = CHANNEL_RC_OK;
|
2015-03-23 17:58:16 +03:00
|
|
|
|
2014-10-16 06:48:18 +04:00
|
|
|
WLog_Print(cliprdr->log, WLOG_DEBUG, "ServerFormatDataResponse");
|
|
|
|
|
2014-11-20 22:28:05 +03:00
|
|
|
if (!context->custom)
|
2015-06-02 14:05:10 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "context->custom not set!");
|
|
|
|
return ERROR_INTERNAL_ERROR;
|
|
|
|
}
|
2015-03-23 17:58:16 +03:00
|
|
|
|
2014-11-20 22:28:05 +03:00
|
|
|
formatDataResponse.msgType = CB_FORMAT_DATA_RESPONSE;
|
|
|
|
formatDataResponse.msgFlags = msgFlags;
|
|
|
|
formatDataResponse.dataLen = dataLen;
|
|
|
|
formatDataResponse.requestedFormatData = NULL;
|
2015-03-23 17:58:16 +03:00
|
|
|
|
2014-11-20 22:28:05 +03:00
|
|
|
if (dataLen)
|
2015-10-02 10:29:00 +03:00
|
|
|
formatDataResponse.requestedFormatData = (BYTE*) Stream_Pointer(s);
|
2015-03-23 17:58:16 +03:00
|
|
|
|
2015-06-02 14:05:10 +03:00
|
|
|
IFCALLRET(context->ServerFormatDataResponse, error, context, &formatDataResponse);
|
|
|
|
if (error)
|
2016-12-14 00:47:08 +03:00
|
|
|
WLog_ERR(TAG, "ServerFormatDataResponse failed with error %"PRIu32"!", error);
|
2015-03-23 17:58:16 +03:00
|
|
|
|
2015-06-02 14:05:10 +03:00
|
|
|
return error;
|
2011-07-12 20:50:00 +04:00
|
|
|
}
|
2017-04-09 02:29:50 +03:00
|
|
|
|
|
|
|
static UINT64 filetime_to_uint64(FILETIME value)
|
|
|
|
{
|
|
|
|
UINT64 converted = 0;
|
|
|
|
converted |= (UINT32) value.dwHighDateTime;
|
|
|
|
converted <<= 32;
|
|
|
|
converted |= (UINT32) value.dwLowDateTime;
|
|
|
|
return converted;
|
|
|
|
}
|
|
|
|
|
|
|
|
static FILETIME uint64_to_filetime(UINT64 value)
|
|
|
|
{
|
|
|
|
FILETIME converted;
|
|
|
|
converted.dwLowDateTime = (UINT32) (value >> 0);
|
|
|
|
converted.dwHighDateTime = (UINT32) (value >> 32);
|
|
|
|
return converted;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define CLIPRDR_FILEDESCRIPTOR_SIZE (4 + 32 + 4 + 16 + 8 + 8 + 520)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse a packed file list.
|
|
|
|
*
|
|
|
|
* The resulting array must be freed with the `free()` function.
|
|
|
|
*
|
|
|
|
* @param [in] format_data packed `CLIPRDR_FILELIST` to parse.
|
|
|
|
* @param [in] format_data_length length of `format_data` in bytes.
|
|
|
|
* @param [out] file_descriptor_array parsed array of `FILEDESCRIPTOR` structs.
|
|
|
|
* @param [out] file_descriptor_count number of elements in `file_descriptor_array`.
|
|
|
|
*
|
|
|
|
* @returns 0 on success, otherwise a Win32 error code.
|
|
|
|
*/
|
|
|
|
UINT cliprdr_parse_file_list(const BYTE* format_data, UINT32 format_data_length,
|
|
|
|
FILEDESCRIPTOR** file_descriptor_array, UINT32* file_descriptor_count)
|
|
|
|
{
|
|
|
|
UINT result = NO_ERROR;
|
|
|
|
UINT32 i;
|
|
|
|
UINT32 count = 0;
|
|
|
|
wStream* s = NULL;
|
|
|
|
|
|
|
|
if (!format_data || !file_descriptor_array || !file_descriptor_count)
|
|
|
|
return ERROR_BAD_ARGUMENTS;
|
|
|
|
|
|
|
|
s = Stream_New((BYTE*) format_data, format_data_length);
|
|
|
|
if (!s)
|
|
|
|
return ERROR_NOT_ENOUGH_MEMORY;
|
|
|
|
|
|
|
|
if (Stream_GetRemainingLength(s) < 4)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "invalid packed file list");
|
|
|
|
|
|
|
|
result = ERROR_INCORRECT_SIZE;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
Stream_Read_UINT32(s, count); /* cItems (4 bytes) */
|
|
|
|
|
|
|
|
if (Stream_GetRemainingLength(s) < count * CLIPRDR_FILEDESCRIPTOR_SIZE)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "packed file list is too short: expected %"PRIu32", have %"PRIuz,
|
|
|
|
count * CLIPRDR_FILEDESCRIPTOR_SIZE,
|
|
|
|
Stream_GetRemainingLength(s));
|
|
|
|
|
|
|
|
result = ERROR_INCORRECT_SIZE;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
*file_descriptor_count = count;
|
|
|
|
*file_descriptor_array = calloc(count, sizeof(FILEDESCRIPTOR));
|
|
|
|
if (!*file_descriptor_array)
|
|
|
|
{
|
|
|
|
result = ERROR_NOT_ENOUGH_MEMORY;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
UINT64 lastWriteTime;
|
|
|
|
FILEDESCRIPTOR* file = &((*file_descriptor_array)[i]);
|
|
|
|
|
|
|
|
Stream_Read_UINT32(s, file->dwFlags); /* flags (4 bytes) */
|
|
|
|
Stream_Seek(s, 32); /* reserved1 (32 bytes) */
|
|
|
|
Stream_Read_UINT32(s, file->dwFileAttributes); /* fileAttributes (4 bytes) */
|
|
|
|
Stream_Seek(s, 16); /* reserved2 (16 bytes) */
|
|
|
|
Stream_Read_UINT64(s, lastWriteTime); /* lastWriteTime (8 bytes) */
|
|
|
|
file->ftLastWriteTime = uint64_to_filetime(lastWriteTime);
|
|
|
|
Stream_Read_UINT32(s, file->nFileSizeHigh); /* fileSizeHigh (4 bytes) */
|
|
|
|
Stream_Read_UINT32(s, file->nFileSizeLow); /* fileSizeLow (4 bytes) */
|
|
|
|
for (c = 0; c < 260; c++) /* fileName (520 bytes) */
|
|
|
|
Stream_Read_UINT16(s, file->fileName[c]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Stream_GetRemainingLength(s) > 0)
|
|
|
|
WLog_WARN(TAG, "packed file list has %"PRIuz" excess bytes",
|
|
|
|
Stream_GetRemainingLength(s));
|
|
|
|
out:
|
|
|
|
Stream_Free(s, FALSE);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Serialize a packed file list.
|
|
|
|
*
|
|
|
|
* The resulting format data must be freed with the `free()` function.
|
|
|
|
*
|
|
|
|
* @param [in] file_descriptor_array array of `FILEDESCRIPTOR` structs to serialize.
|
|
|
|
* @param [in] file_descriptor_count number of elements in `file_descriptor_array`.
|
|
|
|
* @param [out] format_data serialized CLIPRDR_FILELIST.
|
|
|
|
* @param [out] format_data_length length of `format_data` in bytes.
|
|
|
|
*
|
|
|
|
* @returns 0 on success, otherwise a Win32 error code.
|
|
|
|
*/
|
|
|
|
UINT cliprdr_serialize_file_list(const FILEDESCRIPTOR* file_descriptor_array,
|
|
|
|
UINT32 file_descriptor_count, BYTE** format_data, UINT32* format_data_length)
|
|
|
|
{
|
|
|
|
UINT result = NO_ERROR;
|
|
|
|
UINT32 i;
|
|
|
|
wStream* s = NULL;
|
|
|
|
|
|
|
|
if (!file_descriptor_array || !format_data || !format_data_length)
|
|
|
|
return ERROR_BAD_ARGUMENTS;
|
|
|
|
|
|
|
|
s = Stream_New(NULL, 4 + file_descriptor_count * CLIPRDR_FILEDESCRIPTOR_SIZE);
|
|
|
|
if (!s)
|
|
|
|
return ERROR_NOT_ENOUGH_MEMORY;
|
|
|
|
|
|
|
|
Stream_Write_UINT32(s, file_descriptor_count); /* cItems (4 bytes) */
|
|
|
|
|
|
|
|
for (i = 0; i < file_descriptor_count; i++)
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
UINT64 lastWriteTime;
|
|
|
|
const FILEDESCRIPTOR* file = &file_descriptor_array[i];
|
|
|
|
|
|
|
|
Stream_Write_UINT32(s, file->dwFlags); /* flags (4 bytes) */
|
|
|
|
Stream_Zero(s, 32); /* reserved1 (32 bytes) */
|
|
|
|
Stream_Write_UINT32(s, file->dwFileAttributes); /* fileAttributes (4 bytes) */
|
|
|
|
Stream_Zero(s, 16); /* reserved2 (16 bytes) */
|
|
|
|
lastWriteTime = filetime_to_uint64(file->ftLastWriteTime);
|
|
|
|
Stream_Write_UINT64(s, lastWriteTime); /* lastWriteTime (8 bytes) */
|
|
|
|
Stream_Write_UINT32(s, file->nFileSizeHigh); /* fileSizeHigh (4 bytes) */
|
|
|
|
Stream_Write_UINT32(s, file->nFileSizeLow); /* fileSizeLow (4 bytes) */
|
|
|
|
for (c = 0; c < 260; c++) /* fileName (520 bytes) */
|
|
|
|
Stream_Write_UINT16(s, file->fileName[c]);
|
|
|
|
}
|
|
|
|
|
|
|
|
Stream_SealLength(s);
|
|
|
|
|
|
|
|
Stream_GetBuffer(s, *format_data);
|
|
|
|
Stream_GetLength(s, *format_data_length);
|
|
|
|
|
|
|
|
Stream_Free(s, FALSE);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|