//
// Copyright (c) 2014 ANSCENTER. All rights reserved.
//

#include "precomp.h"

#include "base32.h"
#include "base64.h"
#include "sha1.h"
#include "except.h"
#include "bitstream.h"
#include "template.h"
#include "generator.h"
#include "validator.h"
#include "license.h"
#include "licensevalidationargs.h"
#include "licensevalidationresult.h"
#include "licensingclient.h"
#include "certificate.h"
#include "uniconv.h"

void * KeyGenerator_Create()
{
	return new(std::nothrow) KeyGeneratorImpl();
}

void KeyGenerator_Destroy(void * generator)
{
	delete static_cast<KeyGeneratorImpl *>(generator);
}

int KeyGenerator_SetKeyTemplate(void * generator, const void * tmpl)
{
	try {
		static_cast<KeyGeneratorImpl *>(generator)->SetKeyTemplate(static_cast<LicenseTemplateImpl *>(const_cast<void *>(tmpl)));
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int KeyGenerator_SetKeyDataA(void * generator, const char * fieldName, const void * data, int len)
{
	

	try {
		static_cast<KeyGeneratorImpl *>(generator)->SetKeyData(fieldName, data, len);
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int KeyGenerator_SetKeyDataW(void * generator, const wchar_t * fieldName, const void * data, int len)
{
	try {
		static_cast<KeyGeneratorImpl *>(generator)->SetKeyData(w2s(fieldName).c_str(), data, len);
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int KeyGenerator_SetIntKeyDataA(void * generator, const char * fieldName, int data)
{
	try {
		static_cast<KeyGeneratorImpl *>(generator)->SetKeyData(fieldName, data);
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int KeyGenerator_SetIntKeyDataW(void * generator, const wchar_t * fieldName, int data)
{
	

	try {
		static_cast<KeyGeneratorImpl *>(generator)->SetKeyData(w2s(fieldName).c_str(), data);
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int KeyGenerator_SetDateKeyDataA(void * generator, const char * fieldName, int year, int month, int day)
{
	

	try {
		static_cast<KeyGeneratorImpl *>(generator)->SetKeyData(fieldName, year, month, day);
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int KeyGenerator_SetDateKeyDataW(void * generator, const wchar_t * fieldName, int year, int month, int day)
{
	

	try {
		static_cast<KeyGeneratorImpl *>(generator)->SetKeyData(w2s(fieldName).c_str(), year, month, day);
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int KeyGenerator_SetStringKeyDataA(void * generator, const char * fieldName, const char * data)
{
	

	try {
		static_cast<KeyGeneratorImpl *>(generator)->SetKeyData(fieldName, data);
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int KeyGenerator_SetStringKeyDataW(void * generator, const wchar_t * fieldName, const wchar_t * data)
{
	

	try {
		static_cast<KeyGeneratorImpl *>(generator)->SetKeyData(w2s(fieldName).c_str(), w2s(data).c_str());
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int KeyGenerator_SetValidationDataA(void * generator, const char * fieldName, const void * buf, int len)
{
	

	try {
		static_cast<KeyGeneratorImpl *>(generator)->SetValidationData(fieldName, buf, len);
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int KeyGenerator_SetValidationDataW(void * generator, const wchar_t * fieldName, const void * buf, int len)
{
	

	try {
		static_cast<KeyGeneratorImpl *>(generator)->SetValidationData(w2s(fieldName).c_str(), buf, len);
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int KeyGenerator_SetIntValidationDataA(void * generator, const char * fieldName, int data)
{
	

	try {
		static_cast<KeyGeneratorImpl *>(generator)->SetValidationData(fieldName, data);
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int KeyGenerator_SetIntValidationDataW(void * generator, const wchar_t * fieldName, int data)
{
	

	try {
		static_cast<KeyGeneratorImpl *>(generator)->SetValidationData(w2s(fieldName).c_str(), data);
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int KeyGenerator_SetStringValidationDataA(void * generator, const char * fieldName, const char * data)
{
	try {
		static_cast<KeyGeneratorImpl *>(generator)->SetValidationData(fieldName, data);
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int KeyGenerator_SetStringValidationDataW(void * generator, const wchar_t * fieldName, const wchar_t * data)
{
	try {
		static_cast<KeyGeneratorImpl *>(generator)->SetValidationData(w2s(fieldName).c_str(), w2s(data).c_str());
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int KeyGenerator_GenerateKeyA(void * generator, const char **key)
{
	try {
		*key = static_cast<KeyGeneratorImpl *>(generator)->GenerateKey();
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int KeyGenerator_GenerateKeyW(void * generator, const wchar_t **key)
{
	try {	
		static wstring wstr1;
		wstr1 = s2w(static_cast<KeyGeneratorImpl *>(generator)->GenerateKey());
		*key = wstr1.c_str();
	}
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

void * KeyValidator_Create()
{
	return new(std::nothrow) KeyValidatorImpl();
}

void KeyValidator_Destroy(void * validator)
{
	delete static_cast<KeyValidatorImpl *>(validator);
}

int KeyValidator_SetKeyTemplate(void * validator, const void * tmpl)
{
	try {
		static_cast<KeyValidatorImpl *>(validator)->SetKeyTemplate(static_cast<LicenseTemplateImpl *>(const_cast<void *>(tmpl)));
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int KeyValidator_SetKeyA(void * validator, const char * key)
{
	
	try {
		static_cast<KeyValidatorImpl *>(validator)->SetKey(key);
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int KeyValidator_SetKeyW(void * validator, const wchar_t * key)
{
	

	try {
		static_cast<KeyValidatorImpl *>(validator)->SetKey(w2s(key).c_str());
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int KeyValidator_SetValidationDataA(void * validator, const char * fieldName, const void * buf, int len)
{
	
	try {
		static_cast<KeyValidatorImpl *>(validator)->SetValidationData(fieldName, buf, len);
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int KeyValidator_SetValidationDataW(void * validator, const wchar_t * fieldName, const void * buf, int len)
{
	

	try {
		static_cast<KeyValidatorImpl *>(validator)->SetValidationData(w2s(fieldName).c_str(), buf, len);
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int KeyValidator_SetIntValidationDataA(void * validator, const char * fieldName, int data)
{
	

	try {
		static_cast<KeyValidatorImpl *>(validator)->SetValidationData(fieldName, data); 
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int KeyValidator_SetIntValidationDataW(void * validator, const wchar_t * fieldName, int data)
{
	

	try {
		static_cast<KeyValidatorImpl *>(validator)->SetValidationData(w2s(fieldName).c_str(), data); 
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int KeyValidator_SetStringValidationDataA(void * validator, const char * fieldName, const char * data)
{
	

	try {
		static_cast<KeyValidatorImpl *>(validator)->SetValidationData(fieldName, data);
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int KeyValidator_SetStringValidationDataW(void * validator, const wchar_t * fieldName, const wchar_t * data)
{
	

	try {
		static_cast<KeyValidatorImpl *>(validator)->SetValidationData(w2s(fieldName).c_str(), w2s(data).c_str());
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int KeyValidator_IsKeyValid(void * validator)
{
	int status = STATUS_SUCCESS;

	try {
		if (!static_cast<KeyValidatorImpl *>(validator)->IsKeyValid())
			status = STATUS_INVALID_LICENSE_KEY;
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return status;
}

int KeyValidator_QueryKeyDataA(void * validator, const char * dataField, void * buf, int * len)
{
	

	try {
		static_cast<KeyValidatorImpl *>(validator)->QueryKeyData(dataField, buf, len);
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int KeyValidator_QueryKeyDataW(void * validator, const wchar_t * dataField, void * buf, int * len)
{
	

	try {
		static_cast<KeyValidatorImpl *>(validator)->QueryKeyData(w2s(dataField).c_str(), buf, len);
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int KeyValidator_QueryIntKeyDataA(void * validator, const char * dataField, int * data)
{
	

	try {
		*data = static_cast<KeyValidatorImpl *>(validator)->QueryIntKeyData(dataField);
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int KeyValidator_QueryIntKeyDataW(void * validator, const wchar_t * dataField, int * data)
{
	

	try {
		*data = static_cast<KeyValidatorImpl *>(validator)->QueryIntKeyData(w2s(dataField).c_str());
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int KeyValidator_QueryDateKeyDataA(void * validator, const char * dataField, int * year, int * month, int * day)
{
	

	try {
		static_cast<KeyValidatorImpl *>(validator)->QueryDateKeyData(dataField, year, month, day);
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int KeyValidator_QueryDateKeyDataW(void * validator, const wchar_t * dataField, int * year, int * month, int * day)
{
	

	try {
		static_cast<KeyValidatorImpl *>(validator)->QueryDateKeyData(w2s(dataField).c_str(), year, month, day);
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int KeyValidator_QueryValidationDataA(void * validator, const char * dataField, void * buf, int * len)
{
	

	try {
		static_cast<KeyValidatorImpl *>(validator)->QueryValidationData(dataField, buf, len);
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int KeyValidator_QueryValidationDataW(void * validator, const wchar_t * dataField, void * buf, int * len)
{
	

	try {
		static_cast<KeyValidatorImpl *>(validator)->QueryValidationData(w2s(dataField).c_str(), buf, len);
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

void * LicenseTemplate_Create()
{
	return new(std::nothrow) LicenseTemplateImpl();
}

void LicenseTemplate_Destroy(void * tmpl)
{
	delete static_cast<LicenseTemplateImpl *>(tmpl);
}

int LicenseTemplate_SetVersion(void * tmpl, int version)
{
	try {
		static_cast<LicenseTemplateImpl *>(tmpl)->SetVersion(version);
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int LicenseTemplate_GetVersion(void * tmpl, int * version)
{
	try {
		static_cast<LicenseTemplateImpl *>(tmpl)->GetVersion();
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int LicenseTemplate_SetNumberOfGroups(void * tmpl, int numGroups)
{
	try {
		static_cast<LicenseTemplateImpl *>(tmpl)->SetNumberOfGroups(numGroups);
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int LicenseTemplate_GetNumberOfGroups(void * tmpl, int * numGroups)
{
	try {
		*numGroups = static_cast<LicenseTemplateImpl *>(tmpl)->GetNumberOfGroups();
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int LicenseTemplate_SetCharactersPerGroup(void * tmpl, int charsPerGroup)
{
	try {
		static_cast<LicenseTemplateImpl *>(tmpl)->SetCharactersPerGroup(charsPerGroup);
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int LicenseTemplate_GetCharactersPerGroup(void * tmpl, int * charsPerGroup)
{
	try {
		*charsPerGroup = static_cast<LicenseTemplateImpl *>(tmpl)->GetCharactersPerGroup();
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int LicenseTemplate_SetGroupSeparatorA(void * tmpl, const char * groupSep)
{
	

	try {
		static_cast<LicenseTemplateImpl *>(tmpl)->SetGroupSeparator(groupSep);
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int LicenseTemplate_SetGroupSeparatorW(void * tmpl, const wchar_t * groupSep)
{
	

	try {
		static_cast<LicenseTemplateImpl *>(tmpl)->SetGroupSeparator(w2s(groupSep).c_str());
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int LicenseTemplate_GetGroupSeparatorA(void * tmpl, const char **groupSep)
{
	try {
		*groupSep = static_cast<LicenseTemplateImpl *>(tmpl)->GetGroupSeparator();
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int LicenseTemplate_GetGroupSeparatorW(void * tmpl, const wchar_t **groupSep)
{
	try {
		 static wstring wstr1;
		 
		 wstr1 = s2w(static_cast<LicenseTemplateImpl *>(tmpl)->GetGroupSeparator());
		 *groupSep = wstr1.c_str();
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int LicenseTemplate_SetEncoding(void * tmpl, int encoding)
{
	try {
		static_cast<LicenseTemplateImpl *>(tmpl)->SetEncoding(encoding);
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int LicenseTemplate_GetEncoding(void * tmpl, int * encoding)
{
	try {
		*encoding = static_cast<LicenseTemplateImpl *>(tmpl)->GetEncoding();
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int LicenseTemplate_SetHeaderA(void * tmpl, const char * header)
{
	

	try {
		static_cast<LicenseTemplateImpl *>(tmpl)->SetHeader(header);
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int LicenseTemplate_SetHeaderW(void * tmpl, const wchar_t * header)
{
	try {
		static_cast<LicenseTemplateImpl *>(tmpl)->SetHeader(w2s(header).c_str());
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int LicenseTemplate_GetHeaderA(void * tmpl, const char ** header)
{
	try {
		*header = static_cast<LicenseTemplateImpl *>(tmpl)->GetHeader();
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int LicenseTemplate_GetHeaderW(void * tmpl, const wchar_t ** header)
{
	try {
		static wstring wstr1;
		
		wstr1 = s2w(static_cast<LicenseTemplateImpl *>(tmpl)->GetHeader());
		*header = wstr1.c_str();
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int LicenseTemplate_SetFooterA(void * tmpl, const char * footer)
{
	

	try {
		static_cast<LicenseTemplateImpl *>(tmpl)->SetFooter(footer);
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int LicenseTemplate_SetFooterW(void * tmpl, const wchar_t * footer)
{
	

	try {
		static_cast<LicenseTemplateImpl *>(tmpl)->SetFooter(w2s(footer).c_str());
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int LicenseTemplate_GetFooterA(void * tmpl, const char **footer)
{
	try {
		*footer = static_cast<LicenseTemplateImpl *>(tmpl)->GetFooter();
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int LicenseTemplate_GetFooterW(void * tmpl, const wchar_t **footer)
{
	try {
		static wstring wstr1;
		
		wstr1 = s2w(static_cast<LicenseTemplateImpl *>(tmpl)->GetFooter());
		*footer = wstr1.c_str();
	}
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int LicenseTemplate_SetDataSize(void * tmpl, int sizeInBits)
{
	try {
		static_cast<LicenseTemplateImpl *>(tmpl)->SetDataSize(sizeInBits);
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int LicenseTemplate_GetDataSize(void * tmpl, int * sizeInBits)
{
	try {
		*sizeInBits = static_cast<LicenseTemplateImpl *>(tmpl)->GetDataSize();
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int LicenseTemplate_AddDataFieldA(void * tmpl, const char * fieldName, int fieldType, int fieldBitSize, int offset)
{
	
	try {
		static_cast<LicenseTemplateImpl *>(tmpl)->AddDataField(fieldName, fieldType, fieldBitSize, offset);
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int LicenseTemplate_AddDataFieldW(void * tmpl, const wchar_t * fieldName, int fieldType, int fieldBitSize, int fieldOffset)
{
	
	try {
		static_cast<LicenseTemplateImpl *>(tmpl)->AddDataField(w2s(fieldName).c_str(), fieldType, fieldBitSize, fieldOffset);
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}


int LicenseTemplate_GetDataFieldA(void *tmpl, const char *fieldName, int * fieldType, int *fieldSize, int *fieldOffset)
{
	

	if (static_cast<LicenseTemplateImpl *>(tmpl)->GetDataField(fieldName, fieldType, fieldSize, fieldOffset))
		return STATUS_SUCCESS;

	return STATUS_GENERIC_ERROR;
}

int LicenseTemplate_GetDataFieldW(void *tmpl, const wchar_t *fieldName, int * fieldType, int *fieldSize, int *fieldOffset)
{
	

	if (static_cast<LicenseTemplateImpl *>(tmpl)->GetDataField(w2s(fieldName).c_str(), fieldType, fieldSize, fieldOffset))
		return STATUS_SUCCESS;

	return STATUS_GENERIC_ERROR;
}

int LicenseTemplate_EnumDataFieldsA(void * tmpl, void **enumHandle, const char **fieldName, int * fieldType, int * fieldSize, int * fieldOffset)
{
	int status = STATUS_SUCCESS;

	try {
		if (!static_cast<LicenseTemplateImpl *>(tmpl)->EnumDataFields(enumHandle, fieldName, fieldType, fieldSize, fieldOffset))
			status = STATUS_GENERIC_ERROR;
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return status;
}

int LicenseTemplate_EnumDataFieldsW(void * tmpl, void **enumHandle, const wchar_t **fieldName, int * fieldType, int * fieldSize, int * fieldOffset)
{
	int status = STATUS_SUCCESS;

	try {
		const char* str1;
		
		if (static_cast<LicenseTemplateImpl*>(tmpl)->EnumDataFields(enumHandle, &str1, fieldType, fieldSize, fieldOffset))
		{
			static wstring wstr1;

			wstr1 = s2w(str1);
			*fieldName = wstr1.c_str();
		} else
			status = STATUS_GENERIC_ERROR;
	}
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return status;
}

int LicenseTemplate_SetValidationDataSize(void * tmpl, int sizeInBits)
{
	try {
		static_cast<LicenseTemplateImpl *>(tmpl)->SetValidationDataSize(sizeInBits);
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int LicenseTemplate_GetValidationDataSize(void * tmpl, int * sizeInBits)
{
	try {
		*sizeInBits = static_cast<LicenseTemplateImpl *>(tmpl)->GetValidationDataSize();
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int LicenseTemplate_AddValidationFieldA(void * tmpl, const char * fieldName, int fieldType, int fieldBitSize, int offset)
{
	
	try {
		static_cast<LicenseTemplateImpl *>(tmpl)->AddValidationField(fieldName, fieldType, fieldBitSize, offset);
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int LicenseTemplate_AddValidationFieldW(void * tmpl, const wchar_t * fieldName, int fieldType, int fieldBitSize, int offset)
{
	
	try {
		static_cast<LicenseTemplateImpl *>(tmpl)->AddValidationField(w2s(fieldName).c_str(), fieldType, fieldBitSize, offset);
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int LicenseTemplate_GetValidationFieldA(void *tmpl, const char *fieldName, int * fieldType, int *fieldSize, int *fieldOffset)
{
	

	if (static_cast<LicenseTemplateImpl *>(tmpl)->GetValidationField(fieldName, fieldType, fieldSize, fieldOffset))
		return STATUS_SUCCESS;

	return STATUS_GENERIC_ERROR;
}

int LicenseTemplate_GetValidationFieldW(void *tmpl, const wchar_t *fieldName, int * fieldType, int *fieldSize, int *fieldOffset)
{
	

	if (static_cast<LicenseTemplateImpl *>(tmpl)->GetValidationField(w2s(fieldName).c_str(), fieldType, fieldSize, fieldOffset))
		return STATUS_SUCCESS;

	return STATUS_GENERIC_ERROR;
}

int LicenseTemplate_EnumValidationFieldsA(void * tmpl, void **enumHandle, const char **fieldName, int * fieldType, int * fieldSize, int * fieldOffset)
{
	int status = STATUS_SUCCESS;

	try {
		if (!static_cast<LicenseTemplateImpl *>(tmpl)->EnumValidationFields(enumHandle, fieldName, fieldType, fieldSize, fieldOffset))
			status = STATUS_GENERIC_ERROR;
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return status;
}

int LicenseTemplate_EnumValidationFieldsW(void * tmpl, void **enumHandle, const wchar_t **fieldName, int * fieldType, int * fieldSize, int * fieldOffset)
{
	int status = STATUS_SUCCESS;

	try {
		const char* str1;

		if (static_cast<LicenseTemplateImpl *>(tmpl)->EnumValidationFields(enumHandle, &str1, fieldType, fieldSize, fieldOffset))
		{
			static wstring wstr1;
			
			wstr1 = s2w(str1);
			*fieldName = wstr1.c_str();
		} else
			status = STATUS_GENERIC_ERROR;
	}
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return status;
}

int LicenseTemplate_SetSignatureSize(void * tmpl, int signatureSize)
{
	try {
		static_cast<LicenseTemplateImpl *>(tmpl)->SetSignatureSize(signatureSize);
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int LicenseTemplate_GetSignatureSize(void * tmpl, int * signatureSize)
{
	try {
		*signatureSize = static_cast<LicenseTemplateImpl *>(tmpl)->GetSignatureSize();
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int LicenseTemplate_LoadXml(void * tmpl, const char * xmlTemplate)
{
	try {
		static_cast<LicenseTemplateImpl *>(tmpl)->LoadXml(xmlTemplate);
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int LicenseTemplate_SaveXml(void * tmpl, int savePrivateKey, const char **xmlTemplate)
{
	try {
		*xmlTemplate = static_cast<LicenseTemplateImpl *>(tmpl)->SaveXml((savePrivateKey) ? true : false);
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int LicenseTemplate_LoadJson(void * tmpl, const char * jsonTemplate)
{
	try {
		static_cast<LicenseTemplateImpl *>(tmpl)->LoadJson(jsonTemplate);
	}
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int LicenseTemplate_SaveJson(void * tmpl, int savePrivateKey, const char **jsonTemplate)
{
	try {
		*jsonTemplate = static_cast<LicenseTemplateImpl *>(tmpl)->SaveJson((savePrivateKey) ? true : false);
	}
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int LicenseTemplate_SetLicensingServiceUrlA(void * tmpl, const char * url)
{
	try {
		static_cast<LicenseTemplateImpl *>(tmpl)->SetLicensingServiceUrl(url);
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int LicenseTemplate_SetLicensingServiceUrlW(void * tmpl, const wchar_t * url)
{
	try {
		static_cast<LicenseTemplateImpl *>(tmpl)->SetLicensingServiceUrl(w2s(url).c_str());
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int LicenseTemplate_GetLicensingServiceUrlA(void * tmpl, const char **url)
{
	try {
		*url = static_cast<LicenseTemplateImpl *>(tmpl)->GetLicensingServiceUrl();
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int LicenseTemplate_GetLicensingServiceUrlW(void * tmpl, const wchar_t **url)
{
	try {
		static wstring wstr1;
		
		wstr1 = s2w(static_cast<LicenseTemplateImpl*>(tmpl)->GetLicensingServiceUrl());
		*url = wstr1.c_str();
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int LicenseTemplate_SetTemplateIdA(void * tmpl, const char * templateId)
{
	try {
		static_cast<LicenseTemplateImpl *>(tmpl)->SetTemplateId(templateId);
	}
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int LicenseTemplate_SetTemplateIdW(void * tmpl, const wchar_t * templateId)
{
	

	try {
		static_cast<LicenseTemplateImpl *>(tmpl)->SetTemplateId(w2s(templateId).c_str());
	}
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int LicenseTemplate_GetTemplateIdA(void * tmpl, const char **templateId)
{
	try {
		*templateId = static_cast<LicenseTemplateImpl *>(tmpl)->GetTemplateId();
	}
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int LicenseTemplate_GetTemplateIdW(void * tmpl, const wchar_t **templateId)
{
	try {
		static wstring wstr1;
		
		wstr1 = s2w(static_cast<LicenseTemplateImpl*>(tmpl)->GetTemplateId());
		*templateId = wstr1.c_str();
	}
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

/*
int LicenseTemplate_SetProductId(void * tmpl, int productId)
{
	try {
		static_cast<LicenseTemplateImpl *>(tmpl)->SetProductId(productId);
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int LicenseTemplate_GetProductId(void * tmpl, int * productId)
{
	try {
		*productId = static_cast<LicenseTemplateImpl *>(tmpl)->GetProductId();
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

*/

int KeyHelper_GetCurrentHardwareIdW(const wchar_t **hwid)
{
	try {
		*hwid = KeyHelperT<wchar_t>::GetCurrentHardwareId();
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int KeyHelper_GetCurrentHardwareIdA(const char **hwid)
{
	try {
		*hwid = KeyHelperT<char>::GetCurrentHardwareId();
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int KeyHelper_MatchCurrentHardwareIdW(const wchar_t *hwid)
{
	try {
		if (!KeyHelperT<wchar_t>::MatchCurrentHardwareId(hwid))
			return STATUS_INVALID_LICENSE_KEY;
	}
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int KeyHelper_MatchCurrentHardwareIdA(const char *hwid)
{
	try {
		if (!KeyHelperT<char>::MatchCurrentHardwareId(hwid))
			return STATUS_INVALID_LICENSE_KEY;
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int SDKRegistration_SetLicenseKeyA(const char * key)
{
	try {
		SDKRegistrationT<char>::SetLicenseKey(key);
	}
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int SDKRegistration_SetLicenseKeyW(const wchar_t * key)
{
	try {
		SDKRegistrationT<wchar_t>::SetLicenseKey(key);
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int LicenseTemplate_GetPublicKeyCertificateA(void * tmpl, const char **base64Certificate)
{
	try {
		*base64Certificate = static_cast<LicenseTemplateImpl *>(tmpl)->GetPublicKeyCertificate();
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int LicenseTemplate_GetPublicKeyCertificateW(void * tmpl, const wchar_t **base64Certificate)
{
	try {
		static wstring wstr1;
		
		wstr1 = s2w(static_cast<LicenseTemplateImpl *>(tmpl)->GetPublicKeyCertificate());
		*base64Certificate = wstr1.c_str();
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int LicenseTemplate_SetPublicKeyCertificateA(void * tmpl, const char * base64Certificate)
{
	

	try {
		static_cast<LicenseTemplateImpl *>(tmpl)->SetPublicKeyCertificate(base64Certificate);
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int LicenseTemplate_SetPublicKeyCertificateW(void * tmpl, const wchar_t * base64Certificate)
{
	try {
		static_cast<LicenseTemplateImpl *>(tmpl)->SetPublicKeyCertificate(w2s(base64Certificate).c_str());
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int LicenseTemplate_GetPrivateKeyA(void * tmpl, const char **base64Key)
{
	try {
		*base64Key = static_cast<LicenseTemplateImpl *>(tmpl)->GetPrivateKey();
	}
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int LicenseTemplate_GetPrivateKeyW(void * tmpl, const wchar_t **base64Key)
{
	try 
	{
		static wstring wstr1;
		wstr1 = s2w(static_cast<LicenseTemplateImpl *>(tmpl)->GetPrivateKey());
		*base64Key = wstr1.c_str();
	}
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int LicenseTemplate_SetPrivateKeyA(void * tmpl, const char * base64Key)
{
	try {
		static_cast<LicenseTemplateImpl *>(tmpl)->SetPrivateKey(base64Key);
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int LicenseTemplate_SetPrivateKeyW(void * tmpl, const wchar_t * base64Key)
{
	try {
		static_cast<LicenseTemplateImpl *>(tmpl)->SetPrivateKey(w2s(base64Key).c_str());
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int LicenseTemplate_GenerateSigningKeyPair(void * tmpl)
{
	try {
		static_cast<LicenseTemplateImpl *>(tmpl)->GenerateSigningKeyPair();
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int LicenseTemplate_SetPropertyA(void * tmpl, const char * path, const char * name, const char * value)
{	
	try {
		static_cast<LicenseTemplateImpl *>(tmpl)->SetProperty(path, name, value);
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int LicenseTemplate_SetPropertyW(void * tmpl, const wchar_t * path, const wchar_t * name, const wchar_t * value)
{
	try {
		static_cast<LicenseTemplateImpl *>(tmpl)->SetProperty(w2s(path).c_str(), w2s(name).c_str(), w2s(value).c_str());
	}
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int LicenseTemplate_GetPropertyA(void * tmpl, const char * path, const char * name, const char **value)
{
	try {
		*value = static_cast<LicenseTemplateImpl *>(tmpl)->GetProperty(path, name);
	} 
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int LicenseTemplate_GetPropertyW(void *tmpl, const wchar_t * path, const wchar_t * name, const wchar_t **value)
{
	try 
	{
		static wstring wstr1;
		wstr1 = s2w(static_cast<LicenseTemplateImpl *>(tmpl)->GetProperty(w2s(path).c_str(), w2s(name).c_str()));
		*value = wstr1.c_str();
	}
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int Certificate_SignA(const char * csr, const char * privateKey, const char * signingPrivateKey, const char * signingCertificate, const char * certificateAuthorityUrl, int expYear, int expMonth, int expDay, const char **signedCert)
{
	try {
		static std::string staticSignedCert;
		
		staticSignedCert = Certificate::Sign(
			csr,
			privateKey,
			signingPrivateKey,
			signingCertificate,
			certificateAuthorityUrl,
			expYear, expMonth, expDay);

		*signedCert = staticSignedCert.c_str();
	}
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();

		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

void * License_Create()
{
	return new(std::nothrow) LicenseImpl();
}

void License_Destroy(void * license)
{
	delete (LicenseImpl *)license;
}

int License_LoadXml(void * license, const char * xml)
{
	try {
		static_cast<LicenseImpl *>(license)->LoadXml(xml);
	}
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int License_SaveXml(void * license, const char ** xmlLicense)
{
	try {
		*xmlLicense = static_cast<LicenseImpl *>(license)->SaveXml();
	}
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int License_LoadJson(void * license, const char * json)
{
	try {
		static_cast<LicenseImpl *>(license)->LoadJson(json);
	}
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int License_SaveJson(void * license, const char ** jsonLicense)
{
	try {
		*jsonLicense = static_cast<LicenseImpl *>(license)->SaveJson();
	}
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

void * LicenseValidationArgs_CreateA(void * license, char * licenseKey)
{
	
	LicenseValidationArgsImpl * args = new(std::nothrow) LicenseValidationArgsImpl();

	if (license) args->SetLicense((LicenseImpl *)license);

	if (licenseKey)
	{
		args->SetLicenseKey(licenseKey);
	}

	return args;
}

void * LicenseValidationArgs_CreateW(void * license, wchar_t * licenseKey)
{
	
	LicenseValidationArgsImpl * args = new(std::nothrow) LicenseValidationArgsImpl();

	if (license) args->SetLicense((LicenseImpl *)license);

	if (licenseKey)
	{
		args->SetLicenseKey(w2s(licenseKey).c_str());
	}

	return args;
}

void LicenseValidationArgs_Destroy(void* obj)
{
	delete (LicenseValidationArgsImpl*)obj;
}

int LicenseValidationArgs_SetIntLicenseKeyValidationDataA(void * args, const char * fieldName, int fieldValue)
{
	

	try {
		static_cast<LicenseValidationArgsImpl *>(args)->SetLicenseKeyValidationData(fieldName, fieldValue);
	}
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int LicenseValidationArgs_SetIntLicenseKeyValidationDataW(void * args, const wchar_t * fieldName, int fieldValue)
{
	

	try {
		static_cast<LicenseValidationArgsImpl *>(args)->SetLicenseKeyValidationData(w2s(fieldName).c_str(), fieldValue);
	}
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int LicenseValidationArgs_SetStringLicenseKeyValidationDataA(void * args, const char * fieldName, const char * fieldValue)
{
	try {
		static_cast<LicenseValidationArgsImpl *>(args)->SetLicenseKeyValidationData(fieldName, fieldValue);
	}
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int LicenseValidationArgs_SetStringLicenseKeyValidationDataW(void * args, const wchar_t * fieldName, const wchar_t * fieldValue)
{
	try {
		static_cast<LicenseValidationArgsImpl *>(args)->SetLicenseKeyValidationData(w2s(fieldName).c_str(), w2s(fieldValue).c_str());
	}
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int LicenseValidationArgs_SetDateLicenseKeyValidationDataA(void * args, const char * fieldName, int year, int month, int day)
{
	

	try {
		static_cast<LicenseValidationArgsImpl *>(args)->SetLicenseKeyValidationData(fieldName, year, month, day);
	}
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int LicenseValidationArgs_SetDateLicenseKeyValidationDataW(void * args, const wchar_t * fieldName, int year, int month, int day)
{
	

	try {
		static_cast<LicenseValidationArgsImpl *>(args)->SetLicenseKeyValidationData(w2s(fieldName).c_str(), year, month, day);
	}
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int LicenseValidationArgs_SetLicenseKeyValidationDataA(void * args, const char * fieldName, void * data, int len)
{
	

	try {
		static_cast<LicenseValidationArgsImpl *>(args)->SetLicenseKeyValidationData(fieldName, data, len);
	}
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

int LicenseValidationArgs_SetLicenseKeyValidationDataW(void * args, const wchar_t * fieldName, void * data, int len)
{
	

	try {
		static_cast<LicenseValidationArgsImpl *>(args)->SetLicenseKeyValidationData(w2s(fieldName).c_str(), data, len);
	}
	catch (Exception * ex)
	{
		int status = ex->GetCode();
		ex->Destroy();
		return status;
	}
	catch (...)
	{
		return STATUS_GENERIC_ERROR;
	}

	return STATUS_SUCCESS;
}

void * LicensingClient_Create(void * licenseTemplate)
{
	LicensingClientImpl * licensingClient = new(std::nothrow) LicensingClientImpl(NULL, false);

	if (licensingClient)
	{
		licensingClient->SetLicenseTemplate((LicenseTemplateImpl *)licenseTemplate);
	}

	return licensingClient;
}

void LicensingClient_Destroy(void * client)
{
	delete (LicensingClientImpl *)client;
}

int LicensingClient_SetLicenseTemplate(void * client, void * tmpl)
{
	int result = STATUS_SUCCESS;

	try {
		((LicensingClientImpl *)client)->SetLicenseTemplate((LicenseTemplateImpl *)tmpl);
	}
	catch (LicensingException * ex)
	{
		result = ex->GetCode();
		ex->Destroy();
	}
	catch (...)
	{
		result = STATUS_GENERIC_ERROR;
	}

	return result;
}

int LicensingClient_ValidateLicense(void* client, void* licenseValidationArgs, void** licenseValidationResult)
{
	int result = STATUS_SUCCESS;

	try {
		*((LicenseValidationResultImpl **)licenseValidationResult) = &((LicensingClientImpl *)client)->ValidateLicense((LicenseValidationArgsImpl *)licenseValidationArgs);
	}
	catch (LicensingException * ex)
	{
		result = ex->GetCode();
		ex->Destroy();
	}
	catch (...)
	{
		result = STATUS_GENERIC_ERROR;
	}

	return result;
}

int LicenseValidationResult_GetLicense(void * resultObj, void **license)
{
	int result = STATUS_SUCCESS;

	try {
		*((LicenseImpl **)license) = ((LicenseValidationResultImpl *)resultObj)->GetLicense();
	}
	catch (LicensingException * ex)
	{
		result = ex->GetCode();
		ex->Destroy();
	}
	catch (...)
	{
		result = STATUS_GENERIC_ERROR;
	}

	return result;
}

int LicenseValidationResult_IsLicenseExpired(void * result)
{
	return 0;
}

int LicenseValidationResult_IsPaymentRequired(void * result)
{
	int status = STATUS_SUCCESS;

	try {
		((LicenseValidationResultImpl *)result)->IsPaymentRequired();
	}
	catch (LicensingException * ex)
	{
		status = ex->GetCode();
		ex->Destroy();
	}
	catch (...)
	{
		status = STATUS_GENERIC_ERROR;
	}

	return status;
}

int LicenseValidationResult_GetLicenseValidityDays(void * result, int * days)
{
	int status = STATUS_SUCCESS;

	try {
		*days = ((LicenseValidationResultImpl *)result)->GetLicenseValidityDays();
	}
	catch (LicensingException * ex)
	{
		status = ex->GetCode();
		ex->Destroy();
	}
	catch (...)
	{
		status = STATUS_GENERIC_ERROR;
	}

	return status;
}

int LicenseValidationResult_GetLicenseExpirationDate(void * result, int * year, int * month, int * day)
{
	int status = STATUS_SUCCESS;

	try {
		((LicenseValidationResultImpl *)result)->GetLicenseExpirationDate(year, month,day);
	}
	catch (LicensingException * ex)
	{
		status = ex->GetCode();
		ex->Destroy();
	}
	catch (...)
	{
		status = STATUS_GENERIC_ERROR;
	}

	return status;
}
