/* This is the 'C' SIB sample code for EVERLOCK */

/* Last Updated 23/Oct/96 SSS */

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

/* Only first 512 bytes of SB structure is used by SIB */
typedef SB far *SIBFPTR;         /* type far ptr to SB */

void show_access_flgs(char *);

short main(void)
{
   unsigned char flags[65];
   unsigned char flg, tmpflg;
   short i, j, k;
   /* unsigned short far *hnuc; */  /* Far pointer to hnuc value in PSP */ 
   
   SIBFPTR far *sibptrptr;          /* Far pointer to far pointer to SB */
   SIBFPTR sibptr;                  /* Far pointer to SB */
   
   /* If all you need is the NUC Handle that is available directly from */
   /* the PSP.                                                          */
   /* hnuc = (unsigned short far *)((unsigned long) _psp << 0x10 | 0x00FA); */

   sibptrptr = (SIBFPTR far *)((unsigned long)_psp << 0x10 | 0x00FC);
   sibptr = *sibptrptr;

/* The following printf statements are included as a possible use of
   the SB information  */

   /* printf("\nThe HNUC value in the PSP is %u\n", *hnuc); */

   if(_fstrncmp(sibptr->id, "AZSB", 4) == 0)
   {
      printf("\n\n      Signature Literal:  %.4Fs", sibptr->id);
      printf("\n               Drive ID:  %c", sibptr->drv);
      printf("\n       Drive Authorized:  %c", sibptr->auth);
      printf("\n        Protection Type:  %c", sibptr->ptype);
      printf("\n                Product:  %Fs", sibptr->prod);
      printf("\n        Product Serial#:  %lu", sibptr->snp);
      printf("\n       Net-Users Handle:  %u", sibptr->hnuc);
      printf("\n  Authorization Version:  %u", sibptr->avers);

      if(sibptr->ecl == 0xFFFF)
         printf("\n       Exec-Count Limit:  Unlimited");
      else
         printf("\n     Current Exec-Count:  %d", sibptr->ecl);

      if(sibptr->edl == 0xFFFF)
         printf("\n      Expire-Days Limit:  Unlimited");
      else
      {
         printf("\n         Days Remaining:  %d", sibptr->edl);
         printf("\n            Expire Date:  %Fs",sibptr->ed);
      }

      if(sibptr->nul == 0xFFFF)
         printf("\n        Net-Users Limit:  Unlimited");
      else
      {
         printf("\n        Net-Users Count:  %d", sibptr->nuc);
         printf("\n        Net-Users Limit:  %d", sibptr->nul);
      }

      if(sibptr->ptype == 'P')
         printf("\n         Install Counts:  %d", sibptr->icr);

      printf(   "\n              User-Data:  ");
      for(i=0; i<8; i++)
         printf("%02X ", sibptr->usrdat[i]);   

      /*** Get the Access Flags ***/
      k=0;

      /* 
         Setup flags[] array to be a list of turned on flags
      */
      for(i=0; i<64; i++)
      {
         if(sibptr->accflg[i >> 3] & (1 << (i & 7)))
            flags[k++] = i+1;
      }
      flags[k] = '\0';
      show_access_flgs(flags);

      if(sibptr->rtype == 'N')
      {
         printf("\n       Register to Name:  %Fs", sibptr->rname);
         printf("\n    Register to Company:  %Fs", sibptr->rcomp);
         printf("\n   Register to Location:  %Fs", sibptr->rloc);
         printf("\n        Date Registered:  %Fs", sibptr->rdate);
      }

         printf("\n           Current Date:  %Fs", sibptr->cdate);

      printf("\n\n          Secure String: ");
      printf("\n------------------------");
      printf("\n%Fs",sibptr->secstr);
   }
   else
      printf("\nInvalid SIB information.");

   return 0;
}

void show_access_flgs(char *flags)
{
   unsigned char temp[76];
   unsigned char buffer[55];
   unsigned char buf[4];
   short i;
   unsigned char *p;

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