/*****************************************************************************************
*  Copyright (c) 2004, Beijing Senselock Data Security Centre
*  All rights reserved.
*
*  Filename£ºsample_19_win32.c
*          
*  Briefs£º  The purpose of sample19 is to demonstrate how to build a static library 
*            for 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"

#include "..\inc\sample_19.h"


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

#define S4_EXE_FILE_ID "ef19"

#define S4_XA_EXE_FILE_ID "af19"

int main(int argc, char* argv[])
{
	unsigned char lpUserPin[8] = "12345678";
	unsigned char test[8] = {3,1,8,2,5,4,7,6};

    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;
	}

    printf("Data to be bubble-ordered:");
	hexprint(stdout, test, sizeof(test));
	
	printf("Execute program in SenseLock EL...\n");
	dwResult = S4Execute(&stS4Ctx, S4_EXE_FILE_ID, test, sizeof(test), test, sizeof(test), &dwBytesReturned);
	if (dwResult != S4_SUCCESS) 
	{
		printf("Execute program in SenseLock EL failed! <error code: 0x%08x>\n",dwResult);
		ResetAndCloseS4(&stS4Ctx);
		return dwResult;
	}
	printf("Success!\n");
    printf("Data after being bubble-ordered:");
	hexprint(stdout, test, sizeof(test));


	/* for better security,use the following instead of using S4close() directly */
	ResetAndCloseS4(&stS4Ctx);
	getch();

	return dwResult;
}
