Blame channels/rail/rail_common.c

Packit 1fb8d4
/**
Packit 1fb8d4
 * FreeRDP: A Remote Desktop Protocol Implementation
Packit 1fb8d4
 * RAIL common functions
Packit 1fb8d4
 *
Packit 1fb8d4
 * Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
Packit 1fb8d4
 * Copyright 2011 Roman Barabanov <romanbarabanov@gmail.com>
Packit 1fb8d4
 * Copyright 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
#include "rail_common.h"
Packit 1fb8d4
Packit 1fb8d4
#include <winpr/crt.h>
Packit 1fb8d4
Packit 1fb8d4
const char* const RAIL_ORDER_TYPE_STRINGS[] =
Packit 1fb8d4
{
Packit 1fb8d4
	"",
Packit 1fb8d4
	"Execute",
Packit 1fb8d4
	"Activate",
Packit 1fb8d4
	"System Parameters Update",
Packit 1fb8d4
	"System Command",
Packit 1fb8d4
	"Handshake",
Packit 1fb8d4
	"Notify Event",
Packit 1fb8d4
	"",
Packit 1fb8d4
	"Window Move",
Packit 1fb8d4
	"Local Move/Size",
Packit 1fb8d4
	"Min Max Info",
Packit 1fb8d4
	"Client Status",
Packit 1fb8d4
	"System Menu",
Packit 1fb8d4
	"Language Bar Info",
Packit 1fb8d4
	"Get Application ID Request",
Packit 1fb8d4
	"Get Application ID Response",
Packit 1fb8d4
	"Execute Result",
Packit 1fb8d4
	"",
Packit 1fb8d4
	"",
Packit 1fb8d4
	"",
Packit 1fb8d4
	"",
Packit 1fb8d4
	"",
Packit 1fb8d4
	""
Packit 1fb8d4
};
Packit 1fb8d4
Packit 1fb8d4
BOOL rail_string_to_unicode_string(const char* string, RAIL_UNICODE_STRING* unicode_string)
Packit 1fb8d4
{
Packit 1fb8d4
	WCHAR* buffer = NULL;
Packit 1fb8d4
	int length = 0;
Packit 1fb8d4
	free(unicode_string->string);
Packit 1fb8d4
	unicode_string->string = NULL;
Packit 1fb8d4
	unicode_string->length = 0;
Packit 1fb8d4
Packit 1fb8d4
	if (!string || strlen(string) < 1)
Packit 1fb8d4
		return TRUE;
Packit 1fb8d4
Packit 1fb8d4
	length = ConvertToUnicode(CP_UTF8, 0, string, -1, &buffer, 0);
Packit 1fb8d4
Packit 1fb8d4
	if ((length < 0) || ((size_t)length * sizeof(WCHAR) > UINT16_MAX))
Packit 1fb8d4
	{
Packit 1fb8d4
		free(buffer);
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	unicode_string->string = (BYTE*) buffer;
Packit 1fb8d4
	unicode_string->length = (UINT16) length * sizeof(WCHAR);
Packit 1fb8d4
	return TRUE;
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
UINT rail_read_pdu_header(wStream* s, UINT16* orderType, UINT16* orderLength)
Packit 1fb8d4
{
Packit 1fb8d4
	if (!s || !orderType || !orderLength)
Packit 1fb8d4
		return ERROR_INVALID_PARAMETER;
Packit 1fb8d4
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) < 4)
Packit 1fb8d4
		return ERROR_INVALID_DATA;
Packit 1fb8d4
Packit 1fb8d4
	Stream_Read_UINT16(s, *orderType); /* orderType (2 bytes) */
Packit 1fb8d4
	Stream_Read_UINT16(s, *orderLength); /* orderLength (2 bytes) */
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
void rail_write_pdu_header(wStream* s, UINT16 orderType, UINT16 orderLength)
Packit 1fb8d4
{
Packit 1fb8d4
	Stream_Write_UINT16(s, orderType); /* orderType (2 bytes) */
Packit 1fb8d4
	Stream_Write_UINT16(s, orderLength); /* orderLength (2 bytes) */
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
wStream* rail_pdu_init(size_t length)
Packit 1fb8d4
{
Packit 1fb8d4
	wStream* s;
Packit 1fb8d4
	s = Stream_New(NULL, length + RAIL_PDU_HEADER_LENGTH);
Packit 1fb8d4
Packit 1fb8d4
	if (!s)
Packit 1fb8d4
		return NULL;
Packit 1fb8d4
Packit 1fb8d4
	Stream_Seek(s, RAIL_PDU_HEADER_LENGTH);
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
UINT rail_read_handshake_order(wStream* s, RAIL_HANDSHAKE_ORDER* handshake)
Packit 1fb8d4
{
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) < 4)
Packit 1fb8d4
		return ERROR_INVALID_DATA;
Packit 1fb8d4
Packit 1fb8d4
	Stream_Read_UINT32(s, handshake->buildNumber); /* buildNumber (4 bytes) */
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
void rail_write_handshake_order(wStream* s, const RAIL_HANDSHAKE_ORDER* handshake)
Packit 1fb8d4
{
Packit 1fb8d4
	Stream_Write_UINT32(s, handshake->buildNumber); /* buildNumber (4 bytes) */
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
UINT rail_read_handshake_ex_order(wStream* s, RAIL_HANDSHAKE_EX_ORDER* handshakeEx)
Packit 1fb8d4
{
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) < 8)
Packit 1fb8d4
		return ERROR_INVALID_DATA;
Packit 1fb8d4
Packit 1fb8d4
	Stream_Read_UINT32(s, handshakeEx->buildNumber); /* buildNumber (4 bytes) */
Packit 1fb8d4
	Stream_Read_UINT32(s, handshakeEx->railHandshakeFlags); /* railHandshakeFlags (4 bytes) */
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
void rail_write_handshake_ex_order(wStream* s, const RAIL_HANDSHAKE_EX_ORDER* handshakeEx)
Packit 1fb8d4
{
Packit 1fb8d4
	Stream_Write_UINT32(s, handshakeEx->buildNumber); /* buildNumber (4 bytes) */
Packit 1fb8d4
	Stream_Write_UINT32(s, handshakeEx->railHandshakeFlags); /* railHandshakeFlags (4 bytes) */
Packit 1fb8d4
}