<commit_before>/**
 * FreeRDP: A Remote Desktop Protocol Implementation
 * Smartcard Device Service Virtual Channel
 *
 * Copyright 2011 O.S. Systems Software Ltda.
 * Copyright 2011 <NAME> <<EMAIL>>
 * Copyright 2011 <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 <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <winpr/crt.h>

#include <freerdp/utils/list.h>
#include <freerdp/utils/debug.h>
#include <freerdp/utils/svc_plugin.h>

#include <freerdp/channels/rdpdr.h>

#include "smartcard_main.h"

static void smartcard_free(DEVICE* dev)
{
	IRP* irp;
	COMPLETIONIDINFO* CompletionIdInfo;
	SMARTCARD_DEVICE* smartcard = (SMARTCARD_DEVICE*) dev;

	SetEvent(smartcard->stopEvent);
	WaitForSingleObject(smartcard->thread, INFINITE);

	while ((irp = (IRP*) InterlockedPopEntrySList(smartcard->pIrpList)) != NULL)
		irp->Discard(irp);

	_aligned_free(smartcard->pIrpList);

	/* Begin TS Client defect workaround. */

	while ((CompletionIdInfo = (COMPLETIONIDINFO*) list_dequeue(smartcard->CompletionIds)) != NULL)
		free(CompletionIdInfo);

	CloseHandle(smartcard->thread);
	CloseHandle(smartcard->irpEvent);
	CloseHandle(smartcard->stopEvent);
	CloseHandle(smartcard->CompletionIdsMutex);

	Stream_Free(smartcard->device.data, TRUE);
	list_free(smartcard->CompletionIds);

	/* End TS Client defect workaround. */

	free(dev);
}

static void smartcard_process_irp(SMARTCARD_DEVICE* smartcard, IRP* irp)
{
	switch (irp->MajorFunction)
	{
		case IRP_MJ_DEVICE_CONTROL:
			smartcard_device_control(smartcard, irp);
			break;

		default:
			fprintf(stderr, "MajorFunction 0x%X unexpected for smartcards.", irp->MajorFunction);
			DEBUG_WARN("Smartcard MajorFunction 0x%X not supported.", irp->MajorFunction);
			irp->IoStatus = STATUS_NOT_SUPPORTED;
			irp->Complete(irp);
			break;
	}
}

static void smartcard_process_irp_list(SMARTCARD_DEVICE* smartcard)
{
	IRP* irp;

	while (1)
	{
		if (WaitForSingleObject(smartcard->stopEvent, 0) == WAIT_OBJECT_0)
			break;

		irp = (IRP*) InterlockedPopEntrySList(smartcard->pIrpList);

		if (irp == NULL)
			break;

		smartcard_process_irp(smartcard, irp);
	}
}

struct _SMARTCARD_IRP_WORKER
{
	SMARTCARD_DEVICE* smartcard;
	IRP* irp;
	HANDLE thread;
};
typedef struct _SMARTCARD_IRP_WORKER SMARTCARD_IRP_WORKER;
 
static void smartcard_process_irp_thread_func(SMARTCARD_IRP_WORKER* irpWorker)
{
	smartcard_process_irp(irpWorker->smartcard, irpWorker->irp);

	CloseHandle(irpWorker->thread);

	free(irpWorker);
}

static void* smartcard_thread_func(void* arg)
{
	SMARTCARD_DEVICE* smartcard = (SMARTCARD_DEVICE*) arg;
	HANDLE ev[] = {smartcard->irpEvent, smartcard->stopEvent};

	while (1)
	{
		DWORD status = WaitForSingleObject(2, ev, FALSE, INFINITE);

		if (status == WAIT_OBJECT_0 + 1)
			break;
		else if(status != WAIT_OBJECT_0)
			continue;

		ResetEvent(smartcard->irpEvent);
		smartcard_process_irp_list(smartcard);
	}

	return NULL;
}

/* Begin TS Client defect workaround. */
static COMPLETIONIDINFO* smartcard_mark_duplicate_id(SMARTCARD_DEVICE* smartcard, UINT32 CompletionId)
{
	LIST_ITEM* item;
	COMPLETIONIDINFO* CompletionIdInfo;

	/*
	 * Search from the beginning of the LIST for one outstanding "CompletionID"
	 * that matches the one passed in.  If there is one, mark it as a duplicate<commit_msg>Fixed compilation error, now using WaitForMultipleObjects.<commit_after>/**
 * FreeRDP: A Remote Desktop Protocol Implementation
 * Smartcard Device Service Virtual Channel
 *
 * Copyright 2011 O.S. Systems Software Ltda.
 * Copyright 2011 <NAME> <<EMAIL>>
 * Copyright 2011 <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 <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <winpr/crt.h>

#include <freerdp/utils/list.h>
#include <freerdp/utils/debug.h>
#include <freerdp/utils/svc_plugin.h>

#include <freerdp/channels/rdpdr.h>

#include "smartcard_main.h"

static void smartcard_free(DEVICE* dev)
{
	IRP* irp;
	COMPLETIONIDINFO* CompletionIdInfo;
	SMARTCARD_DEVICE* smartcard = (SMARTCARD_DEVICE*) dev;

	SetEvent(smartcard->stopEvent);
	WaitForSingleObject(smartcard->thread, INFINITE);

	while ((irp = (IRP*) InterlockedPopEntrySList(smartcard->pIrpList)) != NULL)
		irp->Discard(irp);

	_aligned_free(smartcard->pIrpList);

	/* Begin TS Client defect workaround. */

	while ((CompletionIdInfo = (COMPLETIONIDINFO*) list_dequeue(smartcard->CompletionIds)) != NULL)
		free(CompletionIdInfo);

	CloseHandle(smartcard->thread);
	CloseHandle(smartcard->irpEvent);
	CloseHandle(smartcard->stopEvent);
	CloseHandle(smartcard->CompletionIdsMutex);

	Stream_Free(smartcard->device.data, TRUE);
	list_free(smartcard->CompletionIds);

	/* End TS Client defect workaround. */

	free(dev);
}

static void smartcard_process_irp(SMARTCARD_DEVICE* smartcard, IRP* irp)
{
	switch (irp->MajorFunction)
	{
		case IRP_MJ_DEVICE_CONTROL:
			smartcard_device_control(smartcard, irp);
			break;

		default:
			fprintf(stderr, "MajorFunction 0x%X unexpected for smartcards.", irp->MajorFunction);
			DEBUG_WARN("Smartcard MajorFunction 0x%X not supported.", irp->MajorFunction);
			irp->IoStatus = STATUS_NOT_SUPPORTED;
			irp->Complete(irp);
			break;
	}
}

static void smartcard_process_irp_list(SMARTCARD_DEVICE* smartcard)
{
	IRP* irp;

	while (1)
	{
		if (WaitForSingleObject(smartcard->stopEvent, 0) == WAIT_OBJECT_0)
			break;

		irp = (IRP*) InterlockedPopEntrySList(smartcard->pIrpList);

		if (irp == NULL)
			break;

		smartcard_process_irp(smartcard, irp);
	}
}

struct _SMARTCARD_IRP_WORKER
{
	SMARTCARD_DEVICE* smartcard;
	IRP* irp;
	HANDLE thread;
};
typedef struct _SMARTCARD_IRP_WORKER SMARTCARD_IRP_WORKER;
 
static void smartcard_process_irp_thread_func(SMARTCARD_IRP_WORKER* irpWorker)
{
	smartcard_process_irp(irpWorker->smartcard, irpWorker->irp);

	CloseHandle(irpWorker->thread);

	free(irpWorker);
}

static void* smartcard_thread_func(void* arg)
{
	SMARTCARD_DEVICE* smartcard = (SMARTCARD_DEVICE*) arg;
	HANDLE ev[] = {smartcard->irpEvent, smartcard->stopEvent};

	while (1)
	{
		DWORD status = WaitForMultipleObjects(2, ev, FALSE, INFINITE);

		if (status == WAIT_OBJECT_0 + 1)
			break;
		else if(status != WAIT_OBJECT_0)
			continue;

		ResetEvent(smartcard->irpEvent);
		smartcard_process_irp_list(smartcard);
	}

	return NULL;
}

/* Begin TS Client defect workaround. */
static COMPLETIONIDINFO* smartcard_mark_duplicate_id(SMARTCARD_DEVICE* smartcard, UINT32 CompletionId)
{
	LIST_ITEM* item;
	COMPLETIONIDINFO* CompletionIdInfo;

	/*
	 * Search from the beginning of the LIST for one outstanding "CompletionID"
	 * that matches the one passed in.  If there is one, mark it as a duplicate
{'fraction_non_alphanumeric': 0.1101367036368326, 'fraction_numerical': 0.007222078927005417, 'mean_word_length': 5.248992747784045, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 81, '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': '5313556', 'n_tokens_mistral': 2832, 'n_tokens_neox': 2653, 'n_words': 644}