<commit_before>/**
 * FreeRDS: FreeRDP Remote Desktop Services (RDS)
 *
 * Copyright 2013 <NAME> <<EMAIL>>
 *
 * 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.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <winpr/crt.h>

#include <freerdp/freerdp.h>
#include <freerdp/listener.h>
#include <freerdp/codec/rfx.h>
#include <freerdp/codec/bitmap.h>

#include "core.h"

/**
 * Custom helpers
 */

int freerds_set_bounds_rect(rdsConnection* connection, rdsRect* rect)
{
	rdpUpdate* update = connection->client->update;

	if (rect)
	{
		rdpBounds bounds;

		bounds.left = rect->left;
		bounds.top = rect->top;
		bounds.right = rect->right - 1;
		bounds.bottom = rect->bottom - 1;

		update->SetBounds((rdpContext*) connection, &bounds);
	}
	else
	{
		update->SetBounds((rdpContext*) connection, NULL);
	}

	return 0;
}

int freerds_send_palette(rdsConnection* connection, int* palette)
{
	//printf("%s\n", __FUNCTION__);
	return 0;
}

int freerds_send_bell(rdsConnection* connection)
{
	//printf("%s\n", __FUNCTION__);
	return 0;
}

int freerds_send_bitmap_update(rdsConnection* connection, int bpp, RDS_MSG_PAINT_RECT* msg)
{
	int nXSrc;
	int nYSrc;
	int nWidth;
	int nHeight;
	BYTE* data;
	BYTE* buffer;
	int yIdx, xIdx, k;
	int rows, cols;
	int nSrcStep;
	BYTE* pSrcData;
	UINT32 DstSize;
	UINT32 SrcFormat;
	BITMAP_DATA* bitmap;
	rdpUpdate* update;
	rdsEncoder* encoder;
	rdpContext* context;
	rdpSettings* settings;
	UINT32 maxUpdateSize;
	UINT32 totalBitmapSize;
	UINT32 updateSizeEstimate;
	BITMAP_DATA* bitmapData;
	BITMAP_UPDATE bitmapUpdate;

	nXSrc = msg->nLeftRect;
	nYSrc = msg->nTopRect;
	nWidth = msg->nWidth;
	nHeight = msg->nHeight;

	context = (rdpContext*) connection;
	update = context->update;

	settings = connection->settings;
	encoder = connection->encoder;

	maxUpdateSize = settings->MultifragMaxRequestSize;

	if (settings->ColorDepth < 32)
		freerds_encoder_prepare(encoder, FREERDP_CODEC_INTERLEAVED);
	else
		freerds_encoder_prepare(encoder, FREERDP_CODEC_PLANAR);

	pSrcData = msg->framebuffer->fbSharedMemory;
	nSrcStep = msg->framebuffer->fbScanline;
	SrcFormat = PIXEL_FORMAT_RGB32;

	if ((nXSrc % 4) != 0)
	{
		nWidth += (nXSrc % 4);
		nXSrc -= (nXSrc % 4);
	}

	if ((nYSrc % 4) != 0)
	{
		nHeight += (nYSrc % 4);
		nYSrc -= (nYSrc % 4);
	}

	rows = (nHeight / 64) + ((nHeight % 64) ? 1 : 0);<commit_msg>Added checks to make sure that the rectangle for the source bitmap is fully contained within the frame buffer.<commit_after>/**
 * FreeRDS: FreeRDP Remote Desktop Services (RDS)
 *
 * Copyright 2013 <NAME> <<EMAIL>>
 *
 * 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.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <winpr/crt.h>

#include <freerdp/freerdp.h>
#include <freerdp/listener.h>
#include <freerdp/codec/rfx.h>
#include <freerdp/codec/bitmap.h>

#include "core.h"

/**
 * Custom helpers
 */

int freerds_set_bounds_rect(rdsConnection* connection, rdsRect* rect)
{
	rdpUpdate* update = connection->client->update;

	if (rect)
	{
		rdpBounds bounds;

		bounds.left = rect->left;
		bounds.top = rect->top;
		bounds.right = rect->right - 1;
		bounds.bottom = rect->bottom - 1;

		update->SetBounds((rdpContext*) connection, &bounds);
	}
	else
	{
		update->SetBounds((rdpContext*) connection, NULL);
	}

	return 0;
}

int freerds_send_palette(rdsConnection* connection, int* palette)
{
	//printf("%s\n", __FUNCTION__);
	return 0;
}

int freerds_send_bell(rdsConnection* connection)
{
	//printf("%s\n", __FUNCTION__);
	return 0;
}

int freerds_send_bitmap_update(rdsConnection* connection, int bpp, RDS_MSG_PAINT_RECT* msg)
{
	int nXSrc;
	int nYSrc;
	int nWidth;
	int nHeight;
	BYTE* data;
	BYTE* buffer;
	int yIdx, xIdx, k;
	int rows, cols, extra;
	int nSrcStep;
	BYTE* pSrcData;
	UINT32 DstSize;
	UINT32 SrcFormat;
	BITMAP_DATA* bitmap;
	rdpUpdate* update;
	rdsEncoder* encoder;
	rdpContext* context;
	rdpSettings* settings;
	UINT32 maxUpdateSize;
	UINT32 totalBitmapSize;
	UINT32 updateSizeEstimate;
	BITMAP_DATA* bitmapData;
	BITMAP_UPDATE bitmapUpdate;

	nXSrc = msg->nLeftRect;
	nYSrc = msg->nTopRect;
	nWidth = msg->nWidth;
	nHeight = msg->nHeight;

	/* Clip the rectangle to the confines of the frame buffer. */
	if (nXSrc < 0)
	{
		nWidth += nXSrc;
		nXSrc = 0;
	}

	if (nYSrc < 0)
	{
		nHeight += nYSrc;
		nYSrc = 0;
	}

	extra = (nXSrc + nWidth) - msg->framebuffer->fbWidth;
	if (extra > 0)
	{
		nWidth -= extra;
	}

	extra = (nYSrc + nHeight) - msg->framebuffer->fbHeight;
	if (extra > 0)
	{
		nHeight -= extra;
	} 

	if ((nWidth <= 0) || (nHeight <= 0))
	{
		/* Nothing to do. */
		return 1;
	}

	context = (rdpContext*) connection;
	update = context->update;

	settings = connection->settings;
	encoder = connection->encoder;

	maxUpdateSize = settings->MultifragMaxRequestSize;

	if (settings->ColorDepth < 32)
		freerds_encoder_prepare(encoder, FREERDP_CODEC_INTERLEAVED);
	else
		freerds_encoder_prepare(encoder, FREERDP_CODEC_PLANAR);

	pSrcData = msg->framebuffer->fbSharedMemory;
	nSrcStep = msg->framebuffer->fbScanline;
	SrcFormat = PIXEL_FORMAT_RGB32;

	if ((nXSrc % 4) != 0)
	{
		nWidth += (nXSrc % 4);
		nXSrc -= (nXSrc % 4);
	}

	if ((nYSrc % 4) != 0)
	{
		nHeight += (nYSrc % 4);
		nYSrc -= (nYSrc % 4);
	}

	rows = (nHeight / 64) + ((nHeight % 64) ? 1 : 0);
{'fraction_non_alphanumeric': 0.12691570881226052, 'fraction_numerical': 0.014527458492975735, 'mean_word_length': 4.12684124386252, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3055619', 'n_tokens_mistral': 2432, 'n_tokens_neox': 2294, 'n_words': 608}