/*@P************************************************************************
 * File           : TSTCHK.C
 * Project        : EVERLOCK EVCHKDX.DLL BCEXT16 Test Application
 * Compiler       : Borland C++ v4.52
 * Description    :
 *
 * Copyright (c) 1994-97 Az-Tech Software, Inc.
 * ALL RIGHTS RESERVED
 ************************************************************************P@*/

#define STRICT
#include <windows.h>
#include <windowsx.h>
#include <stdio.h>
#include <string.h>
#include <memory.h>
#include <dos.h>
#include "sb.h"

// Get state of access flag x in byte array a.
#define ISFLAGSET(a,x) ((a)[((x)-1)>>3]&(1<<(((x)-1)&7)))

/***************************************************************************
 * Function Prototypes.
 ***************************************************************************/
int __far __pascal DoEvchk3( SB __far *lpUserSB );
int NormalizeStatus( int status );

/***************************************************************************
 * Global Variables.
 ***************************************************************************/
SB sb;

//@PB
#pragma argsused
int main( int argc, char **argv )
{
   int comp_status;

   // announce our presence to the user
   printf( "\nEVERLOCK EVCHKDX.DLL BCEXT16 Test Application\n\n" );

   // zero out the contents of the SB structure
   memset( &sb, 0, sizeof( SB ) );

   // initialize the contents of the SB buffer
   strncpy( sb.id, "AZSB", 4 );
   sb.snp = 0;
   sb.pin = 388002617;         // update with PIN from EV3 file

   // get the filename for this application
   strncpy( sb.source, argv[0], 63 );

   // change from our application name to the dummy application name
   strcpy( strrchr( sb.source, '\\' ) + 1, "PCHECK.EXE" );

   // perform the protection check using EVCHKDX.DLL
   sb.func = 1;
   comp_status = DoEvchk3( &sb );

   // display the results of the check
   if ( comp_status )
   {
      // display the error message
      printf( "EVCHKDX.DLL Error %04X!\n", comp_status );

      // return to the OS
      return NormalizeStatus( comp_status );

   } else if ( sb.stat1 ) {

      // display the error message
      printf( "Security Error %04X-%04X-%04X!\n", sb.stat1, sb.stat2,
              sb.stat3 );

      // return to the OS
      return NormalizeStatus( sb.stat1 );
   }

   // display the status of the access flag
   printf( "Access Flag #1 is %s!\n",
           ISFLAGSET( sb.accflg, 1 ) ? "ON" : "off" );

   // display the success message
   printf( "Protection Check Successfull!\n" );

   // return to the OS
   return 0;
}

//@PB
int NormalizeStatus( int status )
{
   int temp;

   // get the hundreds digit from the error code
   temp = ( ( status & 0x0F00 ) >> 8 ) * 100;

   // add in the tens digit from the error code
   temp += ( ( status & 0x00F0 ) >> 4 ) * 10;

   // finally, add in the ones digit
   temp += status & 0x000F;

   // return the normalized status value to our caller
   return temp;
}
