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


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

#include "trousers/tss.h"
#include "trousers/trousers.h"
#include "tcs_tsp.h"
#include "trousers_types.h"
#include "spi_utils.h"
#include "capabilities.h"
#include "tsplog.h"
#include "obj.h"


TSS_RESULT
obj_context_add(TSS_HOBJECT *phObject)
{
	TSS_RESULT result;
	struct tr_context_obj *context = calloc(1, sizeof(struct tr_context_obj));
	unsigned len = strlen(TSS_LOCALHOST_STRING) + 1;

	if (context == NULL) {
		LogError("malloc of %zd bytes failed.", sizeof(struct tr_context_obj));
		return TSPERR(TSS_E_OUTOFMEMORY);
	}

#ifndef TSS_NO_GUI
	context->silentMode = TSS_TSPATTRIB_CONTEXT_NOT_SILENT;
#else
	context->silentMode = TSS_TSPATTRIB_CONTEXT_SILENT;
#endif
	if ((context->machineName = calloc(1, len)) == NULL) {
		LogError("malloc of %u bytes failed", len);
		free(context);
		return TSPERR(TSS_E_OUTOFMEMORY);
	}
	memcpy(context->machineName, TSS_LOCALHOST_STRING, len);
	context->machineNameLength = len;

	context->hashMode = TSS_TSPATTRIB_HASH_MODE_NOT_NULL;
	context->connection_policy = TSS_TSPATTRIB_CONTEXT_VERSION_V1_1;

	if ((result = obj_list_add(&context_list, NULL_HCONTEXT, 0, context, phObject))) {
		free(context);
		return result;
	}

	/* Add the default policy */
	if ((result = obj_policy_add(*phObject, TSS_POLICY_USAGE, &context->policy))) {
		obj_list_remove(&context_list, &context_free, *phObject, *phObject);
		return result;
	}

	context->tcs_api = &tcs_normal_api;

	return TSS_SUCCESS;
}

struct tcs_api_table *
obj_context_get_tcs_api(TSS_HCONTEXT tspContext)
{
	struct tsp_object *obj;
	struct tr_context_obj *context;
	struct tcs_api_table *t;

	if ((obj = obj_list_get_obj(&context_list, tspContext)) == NULL)
		return NULL;

	context = (struct tr_context_obj *)obj->data;

	t = context->tcs_api;

	obj_list_put(&context_list);

	return t;
}

void
context_free(void *data)
{
	struct tr_context_obj *context = (struct tr_context_obj *)data;

	free(context->machineName);
	free(context);
}

TSS_BOOL
obj_is_context(TSS_HOBJECT hObject)
{
	TSS_BOOL answer = FALSE;

	if ((obj_list_get_obj(&context_list, hObject))) {
		answer = TRUE;
		obj_list_put(&context_list);
	}

	return answer;
}

TSS_RESULT
obj_context_get_policy(TSS_HCONTEXT tspContext, UINT32 policyType, TSS_HPOLICY *phPolicy)
{
	struct tsp_object *obj;
	struct tr_context_obj *context;
	TSS_RESULT result = TSS_SUCCESS;

	if ((obj = obj_list_get_obj(&context_list, tspContext)) == NULL)
		return TSPERR(TSS_E_INVALID_HANDLE);

	context = (struct tr_context_obj *)obj->data;

	switch (policyType) {
		case TSS_POLICY_USAGE:
			*phPolicy = context->policy;
			break;
		default:
			result = TSPERR(TSS_E_BAD_PARAMETER);
	}

	obj_list_put(&context_list);

	return result;
}

TSS_RESULT
obj_context_get_machine_name(TSS_HCONTEXT tspContext, UINT32 *size, BYTE **data)<commit_msg>add obj_context_close defn<commit_after>
/*
 * Licensed Materials - Property of IBM
 *
 * trousers - An open source TCG Software Stack
 *
 * (C) Copyright International Business Machines Corp. 2005, 2007
 *
 */


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

#include "trousers/tss.h"
#include "trousers/trousers.h"
#include "tcs_tsp.h"
#include "trousers_types.h"
#include "spi_utils.h"
#include "capabilities.h"
#include "tsplog.h"
#include "obj.h"


TSS_RESULT
obj_context_add(TSS_HOBJECT *phObject)
{
	TSS_RESULT result;
	struct tr_context_obj *context = calloc(1, sizeof(struct tr_context_obj));
	unsigned len = strlen(TSS_LOCALHOST_STRING) + 1;

	if (context == NULL) {
		LogError("malloc of %zd bytes failed.", sizeof(struct tr_context_obj));
		return TSPERR(TSS_E_OUTOFMEMORY);
	}

#ifndef TSS_NO_GUI
	context->silentMode = TSS_TSPATTRIB_CONTEXT_NOT_SILENT;
#else
	context->silentMode = TSS_TSPATTRIB_CONTEXT_SILENT;
#endif
	if ((context->machineName = calloc(1, len)) == NULL) {
		LogError("malloc of %u bytes failed", len);
		free(context);
		return TSPERR(TSS_E_OUTOFMEMORY);
	}
	memcpy(context->machineName, TSS_LOCALHOST_STRING, len);
	context->machineNameLength = len;

	context->hashMode = TSS_TSPATTRIB_HASH_MODE_NOT_NULL;
	context->connection_policy = TSS_TSPATTRIB_CONTEXT_VERSION_V1_1;

	if ((result = obj_list_add(&context_list, NULL_HCONTEXT, 0, context, phObject))) {
		free(context);
		return result;
	}

	/* Add the default policy */
	if ((result = obj_policy_add(*phObject, TSS_POLICY_USAGE, &context->policy))) {
		obj_list_remove(&context_list, &context_free, *phObject, *phObject);
		return result;
	}

	context->tcs_api = &tcs_normal_api;

	return TSS_SUCCESS;
}

struct tcs_api_table *
obj_context_get_tcs_api(TSS_HCONTEXT tspContext)
{
	struct tsp_object *obj;
	struct tr_context_obj *context;
	struct tcs_api_table *t;

	if ((obj = obj_list_get_obj(&context_list, tspContext)) == NULL)
		return NULL;

	context = (struct tr_context_obj *)obj->data;

	t = context->tcs_api;

	obj_list_put(&context_list);

	return t;
}

void
context_free(void *data)
{
	struct tr_context_obj *context = (struct tr_context_obj *)data;

	free(context->machineName);
	free(context);
}

TSS_BOOL
obj_is_context(TSS_HOBJECT hObject)
{
	TSS_BOOL answer = FALSE;

	if ((obj_list_get_obj(&context_list, hObject))) {
		answer = TRUE;
		obj_list_put(&context_list);
	}

	return answer;
}

/* Clean up transport session if necessary. */
void
obj_context_close(TSS_HCONTEXT tspContext)
{
	struct tsp_object *obj;
	struct tr_context_obj *context;

	if ((obj = obj_list_get_obj(&context_list, tspContext)) == NULL)
		return;

	context = (struct tr_context_obj *)obj->data;

	if (context->transAuth.AuthHandle) {
		RPC_FlushSpecific(tspContext, context->transAuth.AuthHandle, TPM_RT_TRANS);

		memset(&context->transPub, 0, sizeof(TPM_TRANSPORT_PUBLIC));
		memset(&context->transMod, 0, sizeof(TPM_MODIFIER_INDICATOR));
		memset(&context->transSecret, 0, sizeof(TPM_TRANSPORT_AUTH));
		memset(&context->transAuth, 0, sizeof(TPM_AUTH));
		memset(&context->transLogIn, 0, sizeof(TPM_TRANSPORT_LOG_IN));
		memset(&context->transLogOut, 0, sizeof(TPM_TRANSPORT_LOG_OUT));
		memset(&context->transLogDigest, 0, sizeof(TPM_DIGEST));
	}

	obj_list_put(&context_list);
}

TSS_RESULT
obj_context_get_policy(TSS_HCONTEXT tspContext, UINT32 policyType, TSS_HPOLICY *phPolicy)
{
	struct tsp_object *obj;
	struct tr_context_obj *context;
	TSS_RESULT result = TSS_SUCCESS;

	if ((obj = obj_list_get_obj(&context_list, tspContext)) == NULL)
		return TSPERR(TSS_E_INVALID_HANDLE);

	context = (struct tr_context_obj *)obj->data;

	switch (policyType) {
		case TSS_POLICY_USAGE:
			*phPolicy = context->policy;
			break;
		default:
			result = TSPERR(TSS_E_BAD_PARAMETER);
	}

	obj_list_put(&context_list);

	return result;
}

TSS_RESULT
obj_context_get_machine_name(TSS_HCONTEXT tspContext, UINT32 *size, BYTE **data)
{'fraction_non_alphanumeric': 0.12117328776875978, 'fraction_numerical': 0.006122739569984337, 'mean_word_length': 5.913385826771654, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 51, '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': '318688', 'n_tokens_mistral': 2802, 'n_tokens_neox': 2670, 'n_words': 523}