<commit_before>
/*
 * Licensed Materials - Property of IBM
 *
 * trousers - An open source TCG Software Stack
 *
 * (C) Copyright International Business Machines Corp. 2004
 *
 */


#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "tss/tss.h"
#include "spi_internal_types.h"
#include "spi_utils.h"
#include "capabilities.h"
#include "tsplog.h"
#include "tspps.h"
#include "hosttable.h"
#include "tcsd_wrap.h"
#include "tcsd.h"
#include "obj.h"
#include "tss/trousers.h"

char *tss_layers[] = { "tpm", "tddl", "tcs", "tsp" };

TSS_RESULT
internal_GetMachineName(UNICODE *name, int nameSize)
{
	mbstate_t ps;
	const char *s = "localhost";
	size_t ret;

	memset(&ps, 0, sizeof(mbstate_t));

	ret = mbsrtowcs(name, &s, nameSize, &ps);
	if (ret == (size_t)(-1)) {
		LogError("Error converting string %s to UNICODE", s);
		return TSS_E_INTERNAL_ERROR;
	}

	return TSS_SUCCESS;

}

TSS_RESULT
Tspi_Context_Create(TSS_HCONTEXT * phContext)	/*  out */
{
	TSS_HOBJECT hObject;
	UINT32 objectSize = sizeof(TCPA_CONTEXT_OBJECT);
	TCPA_CONTEXT_OBJECT *object;
	AnObject *anObject;
	TSS_RESULT result;

	LogDebug1("Tspi_Context_Create");

	if (phContext == NULL)
		return TSS_E_BAD_PARAMETER;

	/* Create a new context object with default settings */
	hObject = addObject(0, TSS_OBJECT_TYPE_CONTEXT);
	if (hObject == NULL_HCONTEXT)
		return TSS_E_INTERNAL_ERROR;

	object = calloc(1, objectSize);
	if (object == NULL) {
		LogError("malloc of %d bytes failed.", objectSize);
		return TSS_E_INTERNAL_ERROR;
	}
	object->tcsHandle = 0;
	object->policy = 0;
	object->silentMode = TSS_TSPATTRIB_CONTEXT_NOT_SILENT;

	if ((result = setObject(hObject, object, objectSize)))
		return TSS_E_INTERNAL_ERROR;

	/* sanity checking */
	anObject = getAnObjectByHandle(hObject);
	if (anObject == NULL) {
		LogError("No object found with handle matching 0x%x", hObject);
		return TSS_E_INTERNAL_ERROR;
	}

	free(object);

	/* Return the new objectHandle to the context */
	*phContext = hObject;

	LogDebug1("Leaving Context Create");
	return TSS_SUCCESS;
}

TSS_RESULT
Tspi_Context_Close(TSS_HCONTEXT tspContext)	/*  in */
{
	TCS_CONTEXT_HANDLE tcsContext;
	TSS_RESULT result;

	LogDebug1("Tspi_Context_Close");
	if ((result = obj_checkType_1(tspContext, TSS_OBJECT_TYPE_CONTEXT)))
		return result;

	/* free all context related memory */
	free_tspi(tspContext, NULL);

	/* Get the TCS context, if we're connected */
	result = obj_isConnected_1(tspContext, &tcsContext);
	if (result == TSS_SUCCESS) {
		LogDebug1("Leaving Context Close");
		/* ---  Have the TCS do its thing */
		TCS_CloseContext(tcsContext);
	} else {
		/* XXX Assume 1 TSPi context per process. Is there a reason to open more than 1? */
		destroy_ps();
	}

	/* ---  Destroy all objects */
	obj_closeContext(tspContext);

	/* We're not a connected context, so just exit */
	return TSS_SUCCESS;
}

TSS_RESULT
ConnectGuts(TSS_HCONTEXT tspContext, UNICODE *wszDestination, TCS_CONTEXT_HANDLE tcsHandle)
{<commit_msg>free context related memory after we're done using it<commit_after>
/*
 * Licensed Materials - Property of IBM
 *
 * trousers - An open source TCG Software Stack
 *
 * (C) Copyright International Business Machines Corp. 2004
 *
 */


#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "tss/tss.h"
#include "spi_internal_types.h"
#include "spi_utils.h"
#include "capabilities.h"
#include "tsplog.h"
#include "tspps.h"
#include "hosttable.h"
#include "tcsd_wrap.h"
#include "tcsd.h"
#include "obj.h"
#include "tss/trousers.h"

char *tss_layers[] = { "tpm", "tddl", "tcs", "tsp" };

TSS_RESULT
internal_GetMachineName(UNICODE *name, int nameSize)
{
	mbstate_t ps;
	const char *s = "localhost";
	size_t ret;

	memset(&ps, 0, sizeof(mbstate_t));

	ret = mbsrtowcs(name, &s, nameSize, &ps);
	if (ret == (size_t)(-1)) {
		LogError("Error converting string %s to UNICODE", s);
		return TSS_E_INTERNAL_ERROR;
	}

	return TSS_SUCCESS;

}

TSS_RESULT
Tspi_Context_Create(TSS_HCONTEXT * phContext)	/*  out */
{
	TSS_HOBJECT hObject;
	UINT32 objectSize = sizeof(TCPA_CONTEXT_OBJECT);
	TCPA_CONTEXT_OBJECT *object;
	AnObject *anObject;
	TSS_RESULT result;

	LogDebug1("Tspi_Context_Create");

	if (phContext == NULL)
		return TSS_E_BAD_PARAMETER;

	/* Create a new context object with default settings */
	hObject = addObject(0, TSS_OBJECT_TYPE_CONTEXT);
	if (hObject == NULL_HCONTEXT)
		return TSS_E_INTERNAL_ERROR;

	object = calloc(1, objectSize);
	if (object == NULL) {
		LogError("malloc of %d bytes failed.", objectSize);
		return TSS_E_INTERNAL_ERROR;
	}
	object->tcsHandle = 0;
	object->policy = 0;
	object->silentMode = TSS_TSPATTRIB_CONTEXT_NOT_SILENT;

	if ((result = setObject(hObject, object, objectSize)))
		return TSS_E_INTERNAL_ERROR;

	/* sanity checking */
	anObject = getAnObjectByHandle(hObject);
	if (anObject == NULL) {
		LogError("No object found with handle matching 0x%x", hObject);
		return TSS_E_INTERNAL_ERROR;
	}

	free(object);

	/* Return the new objectHandle to the context */
	*phContext = hObject;

	LogDebug1("Leaving Context Create");
	return TSS_SUCCESS;
}

TSS_RESULT
Tspi_Context_Close(TSS_HCONTEXT tspContext)	/*  in */
{
	TCS_CONTEXT_HANDLE tcsContext;
	TSS_RESULT result;

	LogDebug1("Tspi_Context_Close");
	if ((result = obj_checkType_1(tspContext, TSS_OBJECT_TYPE_CONTEXT)))
		return result;

	/* Get the TCS context, if we're connected */
	result = obj_isConnected_1(tspContext, &tcsContext);
	if (result == TSS_SUCCESS) {
		LogDebug1("Leaving Context Close");
		/* ---  Have the TCS do its thing */
		TCS_CloseContext(tcsContext);
	} else {
		/* XXX Assume 1 TSPi context per process. Is there a reason to open more than 1? */
		destroy_ps();
	}

	/* ---  Destroy all objects */
	obj_closeContext(tspContext);

	/* free all context related memory */
	free_tspi(tspContext, NULL);

	/* We're not a connected context, so just exit */
	return TSS_SUCCESS;
}

TSS_RESULT
ConnectGuts(TSS_HCONTEXT tspContext, UNICODE *wszDestination, TCS_CONTEXT_HANDLE tcsHandle)
{
{'fraction_non_alphanumeric': 0.1225752508361204, 'fraction_numerical': 0.0070234113712374585, 'mean_word_length': 4.778743961352657, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4855911', 'n_tokens_mistral': 2319, 'n_tokens_neox': 2225, 'n_words': 546}