//
//	FAKELOCK.C
//
//
//	This function simulates the type of objects that come with
//	hardware locks.  A real lock object module would make a call to
//	an external device attached to the printer port (or sometime the
//	RS232 port).  The lock object module passes an input parameter to
//	the external device and reads the return value created by the
//	external device.  The lock object module then returns a value
//	to the calling program.
//
//	FAKELOCK's input parameters are a pointer to a string of char
//	and an int, telling which lock to use.  The second parameter is
//	present to simulate two locks.
//

#include <alloc.h>
#include <string.h>
#include "fakelock.h"

int 	FAKELOCK( char* incode, int key )
{
  int   leng;			// length of input string, must be >=5
  int   merg;			// place where incode is merged with itself
  char* strincode;		// pointer to tempory storeage area
  int*  strlong;		// points to place in string;
  int  i,j;

  if ( (leng = strlen( incode )) < 5 )	// find leng of string parameter
    return ( 0 );			// if not above minimun exit

  if ( (strincode = malloc( leng+4 )) == NULL )	// allocate tempory string area
    return ( 0 );			// if tempory area not available exit

  strcpy( strincode, incode );		// copy input parameter to work area

  j = leng % 4;				// determine length to get even 4 bytes

  if ( j != 0 )				// if not even 4 bytes long
  {
    j = 4 - j;				// determine fill length
    for ( i = 1; i <= j; i++ )
    {
      strincode[leng] = NULL;           // fill with NULL
      leng++;				// increment length
    }
  }

  j = leng / 4;				// determine number of loops in merge
  strlong = ((int *)strincode);		// get first four characters

  merg = *strlong;               	// move first part of string to merge

  for ( i = 1; i < j; i++ )
  {
    strlong++;				// increment to next part of in string
    merg &= (long)(*strlong);		// bit wise and
  }

  switch ( key )
  {
    case 1 : merg ^= 11239; break;	// special code for lock 1
    case 2 : merg ^= 15096; break;	// special code for lock 2
    default: merg  = 1;			// code when no key given
  }

  free( strincode );

  return ( merg );
}
