/* This is the 'C' EVCHK3.OBJ sample code */

// Last updated 28/May/96 SSS 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sb.h"

/* Assign a pointer to SB structure */
SB sb;

/* EVCHK3 Prototype */
short far pascal EVCHK3(SB far *);

/* Function Prototypes */
short do_check(char *);
void  do_output(void);
void  show_usrdata(void);
void  show_access_flgs(void);

short main(argc, argv)
short argc;
char *argv[];
{
   char fpath[63];               /* variable to get load path of program */
   short comp_status;            /* variable to hold completion status of EVCHK3 */

   if(argc < 2)
      strcpy(fpath, argv[0]);    /* get the full load path for this program */
   else
      strcpy(fpath, argv[1]);

   memset(&sb, 0, sizeof(struct SecureBlock)); /* Zero the Structure */
   
   comp_status = do_check(fpath);/* call function to initialize structure and */
                                 /* call EVCHK3 function */
   if(comp_status == 0)          /* If EVCHK3 operated, check status code */
   {
     if(sb.stat1 == 0)           /* If authorization check passed */
        do_output();             /* print information */
     else                        /* otherwise print error and exit */
        printf("\n\nEVCHK3 Authorization Error = %04X-%04X-%04X",
              sb.stat1, sb.stat2, sb.stat3);
   }

   return comp_status;           /* return completion status */
}

/* Initialize fields for EVCHK3 call then call EVCHK3 function */
short do_check(char *fpath)
{
   short comp_status;            /* variable used for return from EVCHK3 */

   strncpy(sb.id, "AZSB", 4);    /* Signature Literal */
   sb.stat1 = 0x7240;            /* Initialize to invalid status code.  */
   sb.func = 1;                  /* Function 0, 1, 2, or 3  */
   strcpy(sb.source, fpath);     /* Full Path and name of Protected Pgm. */
   strcpy(strrchr(sb.source, '\\') + 1, "PCHECK.EXE");
   sb.pin = 1804054920;          /* Product ID Number */
   sb.snp = 0;                   /* Set Product Serial number to 0 */

   comp_status = EVCHK3(&sb);     /* Call the EVCHK3 function */

   return comp_status;           /* return the EVCHK3 return code */
}

/* Output from the EVCHK3 call */
void do_output(void)
{
   /** Print out the Status Codes **/
   printf("\n            Return Code: %04X-%04X-%04X", 
                        sb.stat1, sb.stat2, sb.stat3);

   /** Print out the Function Code used **/
   printf("\n          Function Code: %d", sb.func);

   /** Print out the Drive, Path and Program Name checked **/
   printf("\n Drive and Path Checked: %s", sb.source);

   /** Print out the Protection Type returned **/
   if(sb.ptype == 'P')
      printf("\n        Protection Type:  Original Product");
   else if(sb.ptype == 'C')
      printf("\n        Protection Type:  CPU-Lock Product");
   else if(sb.ptype == 'E')
      printf("\n        Protection Type:  Encrypt Only Product");
   else
      printf("\n        Protection Type:  Installed Copy");

   /** Print Product Name **/
   printf("\n           Product Name: %s", sb.prod);

   /** Print out the Product Serial Number **/
   printf("\n        Product Serial#: %lu",sb.snp);
   
   /** Print out the NUC Handle **/
   printf("\n       Net-Users Handle:  %u", sb.hnuc);

   /** Print out the Authorization Version **/
   printf("\n  Authorization Version:  %u", sb.avers);

   /** Print out the Exec-Counts found **/
   if(sb.ecl == 0xFFFF)
   {
      printf("\n       Exec-Count Limit: ");
      printf("Unlimited");
   }
   else
   {
      printf("\n     Current Exec-Count: ");
      printf("%d", sb.ecl);
   }

   /** Print out the Expire-Days found **/
   if(sb.edl == 0xFFFF)
   {
      printf("\n      Expire-Days Limit: ");
      printf("Unlimited");
   }
   else
   {
      printf("\n      Current Days Left: ");
      printf("%d", sb.edl);
      printf("\n            Expire Date: %s", sb.ed);
   }

   /** Print out the Current Net-Users Count and Limit **/
   if(sb.nul == 0xFFFF)
      printf("\n        Net-Users Limit: Unlimited");
   else
   {
      printf("\nCurrent Net-Users Count: %d",sb.nuc);
      printf("\n        Net-Users Limit: %d",sb.nul);
   }

   /** Print out the Install Counts Available if Original Product **/
   if(sb.ptype == 'P')
      printf("\n         Install Counts: %d",sb.icr);

   /** Print out User-Data **/
   show_usrdata();

   /** Print out the Access Flags **/
   show_access_flgs();

   /** Print out the Registration Info **/
   if(sb.rtype == 'N')
   {
      printf("\n     Registered to Name: %s", sb.rname);
      printf("\n  Registered to Company: %s", sb.rcomp);
      printf("\n Registered to Location: %s", sb.rloc);
      printf("\n        Date Registered: %s", sb.rdate);
   }

   /** Print out the Current Date **/
   printf("\n           Today's Date: %s", sb.cdate);

   /** Print out the Secure String **/
   printf("\n          Secure String: ");
   printf("\n------------------------ ");
   printf("\n%s",sb.secstr);
}

void show_usrdata(void)
{
   char buffer[72];
   char temp[4];
   short i;

   for(i=0; i<72; i++)
      buffer[i] = '\0';

   strcpy(buffer, "              User-Data: ");
                 
   for(i=0; i<8; i++)
   {
      sprintf(temp, "%02X ", sb.usrdat[i]);
      strcat(buffer, temp);
   }

   printf("\n%s", buffer);
}

void show_access_flgs(void)
{
   unsigned char flags[65];
   unsigned char temp[76];
   unsigned char buffer[55];
   unsigned char buf[4];
   short i, j, k;
   unsigned char flg, tmpflg;
   unsigned char *p;

   k=0;

   /* 
      Setup flags[] array to be a list of turned on flags
   */
   for(i=0; i<64; i++)
   {
      if(sb.accflg[i >> 3] & (1 << (i & 7)))
         flags[k++] = i+1;
   }
   flags[k] = '\0';
   
   strcpy(temp, "\n       Access Flags Set: ");
   buffer[0] = '\0';
   p = flags;
   if(*p != '\0')
   {
      for(i=0; i<64; i++)
      {
         if(*p != '\0')
         {
            sprintf(buf, "%2d ", *p);
            p++;
            strcat(buffer, buf);
         }
         else
            break;

         if(strlen(buffer) == 52)
         {
            strcat(temp, buffer);
            printf("%s", temp);
            strcpy(temp, "\n                         ");
            buffer[0] = '\0';
         }
      }

      if(strlen(buffer))
      {
         strcat(temp, buffer);
         printf("%s", temp);
      }
   }
   else
   {
      strcpy(buffer, "None");
      strcat(temp, buffer);
      printf("%s", temp);
   }
}
