/*****************************************************************************************
*  Copyright (c) 2004, Beijing Senselock Data Security Centre
*  All rights reserved.
*
*  Filename£ºsample_24_win32.c
*          
*  Briefs£º  The purpose of this snippet is to demonstrate how to use double-precision float
*            in SenseLock EL EXF.
*            
*  History:        
*  --------------------------------------------------------        
*            10/28/2004 v1.0.0.0 Lihua created
*            
*            
*******************************************************************************************/

#include <stdio.h>
#include <conio.h>
#include <assert.h>

#include "..\..\inc\win32\sense4.h"
#include "..\..\inc\win32\crypt2.h"
#include "..\..\inc\win32\ses_err_msg.h"/* for debug purpose only */
#include "..\..\common\common.h"

#ifdef WIN32
#define  DOUBLE_T double
#endif

#include "..\inc\sample_24.h"


/* global variables definition */
SENSE4_CONTEXT stS4Ctx = {0};
unsigned long dwResult = 0;
unsigned long dwBytesReturned = 0;

IO_PACKAGE stDataPkgIn = {0};
IO_PACKAGE stDataPkgOut = {0};

#define S4_EXE_FILE_ID "ef24"

/* for debug purpose only : handle results returned by stDataPkgOut.tag */
#ifdef _DEBUG
void HandleError(unsigned char bErrCode, unsigned char *pbErrInfo)
{
	switch(bErrCode)
	{
	case ERR_SUCCESS:
		printf("Success!\n");
		break;
	case ERR_SES:
		printf("Error during SES Execution :\n%s\n", SesErrMsgs[*pbErrInfo]);
		break;
	case ERR_INVALID_PARAMETER:
		printf("Invalid input,pls check your input data\n");
		break;
	default:
		printf("Undefined error code!\n");
		break;
	}
	return;
}
#else
void HandleError(unsigned char bErrCode, unsigned char *pbErrInfo)
{
	return;
}	
#endif

/* check result for S4Execute(...) */
unsigned long CheckS4ExecutionResult(unsigned long dwResult,unsigned long dwBytesReturned,unsigned char tag)
{
	
	if (dwResult != S4_SUCCESS) 
	{
		printf("S4 Execution failed! <error code: 0x%08x>\n", dwResult);
		return 0;
	}
	
	if (dwBytesReturned < 2)
	{
		printf("Invalid data package returned!\n");
		return 0;
	}
	
	if (tag != ERR_SUCCESS)
	{
		HandleError(stDataPkgOut.tag, stDataPkgOut.buff);
		return 0;
	}
	return 1;
}
int main(int argc, char* argv[])
{
	unsigned char lpUserPin[8] = "12345678";
    DATA_BLOCK stDataBlk = {0};
	if (!(dwResult = OpenS4ByIndex(FIRST_S4_INDEX,&stS4Ctx)))
	{
		return 1;
	}   
	
	dwResult = S4ChangeDir(&stS4Ctx, "\\");
	if (dwResult != S4_SUCCESS) 
	{
		printf("Change directory failed! <error code: 0x%08x>\n",dwResult);
		S4Close(&stS4Ctx);
		return dwResult;
	}
	
	// Call S4VerifyPin(...) to verify User PIN so as to get the privilege to execute the program in SenseLock EL.
	dwResult = S4VerifyPin(&stS4Ctx, lpUserPin, sizeof(lpUserPin), S4_USER_PIN);
	if (dwResult != S4_SUCCESS) 
	{
		printf("Verify Pin failed! <error code: 0x%08x>\n",dwResult);
		S4Close(&stS4Ctx);
		return dwResult;
	}	
     
     stDataBlk.cmd = CMD_DBL_ADD;
	 stDataBlk.op1 = 1.7976e+50;
	 stDataBlk.op2 = 3.4028e+50;
	 stDataPkgIn.tag = 0; /* no meaning */
	 stDataPkgIn.len = sizeof(DATA_BLOCK);
	 memcpy(stDataPkgIn.buff, &stDataBlk, stDataPkgIn.len);

	 printf("Running SenseLock EL EXF...\n");
	 dwResult = S4Execute(&stS4Ctx, S4_EXE_FILE_ID, &stDataPkgIn, 
		 IO_PACKAGE_HEADER_SIZE+stDataPkgIn.len, 
		 &stDataPkgOut, sizeof(IO_PACKAGE), &dwBytesReturned);
	 if (!CheckS4ExecutionResult(dwResult, dwBytesReturned, stDataPkgOut.tag))
	 {
		 ResetAndCloseS4(&stS4Ctx);
		 return 1;
	 }
	 printf("Success!\n");

	 printf("The result:%e\n", *((double *)stDataPkgOut.buff));
	/* for better security,use the following instead of using S4close() directly */
 	ResetAndCloseS4(&stS4Ctx);
	getch();

	return dwResult;
}





















