home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / selfchec / blockade / demo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-19  |  5.8 KB  |  185 lines

  1.  
  2. /* -------------------------------------------------------------- */
  3. /*            Blockade(tm) demonstration program.                 */
  4. /*                                                                */
  5. /*  Demonstrates the use of the blockade self checking routines.  */
  6. /*                                                                */
  7. /*   1.03 - automatically clear patch area if /O specified.       */
  8. /*   1.02 - added more comments and cleaned up a little.          */
  9. /*   1.01 - added patch overide /O function to accomodate MSC     */
  10. /*          which places a value in the exe header chksum.        */
  11. /*                                                                */
  12. /* -------------------------------------------------------------- */
  13.  
  14. #include "allstds.h"        /* Standard stuff  */
  15. #ifdef __TURBOC__           /* defined in BC++ */
  16.  #pragma hdrstop
  17. #endif
  18.  
  19. #include "blkproto.h"       /* function prototypes for the library */
  20. #include "demosig.h"        /* holds the signature string          */
  21.  
  22. /* ------------------------------------------- */
  23.  
  24. int main( int argc,char * *argv );
  25. void do_args( int, char **);
  26. int patch_checksum( char *, unsigned );
  27.  
  28. /* ------------------------------------------- */
  29.  
  30. int fix_mode = 0;              /* 1 = patch to fix...        */
  31. int corrupt_mode = 0;          /* 1 = Corrupt to force error */
  32. unsigned fix_value;            /* value to use to patch      */
  33.  
  34. #ifdef __TURBOC__
  35. int patch_overide = 0;         /* There shouldn't be any value.     */
  36. #else
  37. int patch_overide = 1;         /* But MSC uses the checksum area ?! */
  38. #endif
  39.  
  40. /* ------------------------------------------- */
  41.  
  42. main(int argc,char **argv)
  43. {
  44.  int blkstat, usrsize;
  45.  char *sp;
  46.  
  47.  printf("\n");
  48.  printf("BLOCKADE (tm) demo program (%s). Ver. 2.0 \n", argv[0]);
  49.  printf("   Copyright (C) 1991-92 Indusoft Corp. \n\n");
  50.  printf(" Switches:\n");
  51.  printf("    /C  - corrupt myself so self check fails.\n");
  52.  printf("    /F  - fix myself so self check passes.\n\n");
  53.  
  54.  do_args( argc, argv);
  55.  
  56.  if (fix_mode || corrupt_mode)
  57.         { patch_checksum( argv[0], fix_value);  }
  58.  
  59.  printf("Checking myself for changes..\n");
  60.  
  61.  blkstat = blockade( argv[0], 780);       /* the heart of the demo.. */
  62.  
  63.  printf ("Results of file self check = %d\n", blkstat);
  64.  
  65.  sp = get_blk_err_msg( blkstat);
  66.  printf("Status message from file self check was \"%s\". \n", sp);
  67.  if (blkstat == 7)
  68.    {
  69.     printf("Run \"Brand DEMO  /us=HI! \" "
  70.            "or \"Brand DEMO /uf=USERDATA.DAT\"\n");
  71.     printf("to initialize this file and retry..\n");
  72.    }
  73.  
  74. /*   Show how the user function works if anything is there       */
  75. /*   While the user data does not have to be a string (it could  */
  76. /*   be binary from a file), don't print it if it is not.        */
  77.  
  78.  sp = (char *) blk_user_pointer(&usrsize);
  79.  if (strlen(sp) && (sp != NULL) && (blkstat == 0))
  80.     {
  81.         printf("User data : %s \n",sp);
  82.     }
  83.  
  84.  return(0);
  85.  }
  86.  
  87. /*-------------------------------------------------------------------*/
  88. /*  Gathers up the command line switches.                            */
  89. /*                                                                   */
  90. /*-------------------------------------------------------------------*/
  91.  
  92. void do_args( int argc, char **argv)
  93.  {
  94.    char buf[8], *sp;
  95.    int j;
  96.  
  97.   for (j = 1; j < argc; j++)
  98.     {
  99.       sp = buf;
  100.       strncpy( buf, argv[j], sizeof(buf)-1);
  101.       strupr( buf );
  102.       if (*sp =='-') *sp = '\\';       /* accommodate those unix guys..*/
  103.       if (*sp =='/') sp++;             /* look to next char...         */
  104.       else           continue;         /* nothing I know how to handle */
  105.  
  106.       if (*sp == 'F')
  107.            {  fix_mode = 1;     fix_value = 0;  }
  108.       if (*sp == 'C')
  109.            {  corrupt_mode = 1; fix_value = 1;  }
  110.       if (*sp == 'O')
  111.            {  patch_overide = 1;
  112.               fix_mode = 1;     fix_value = 0;  }
  113.    }
  114.  }
  115.  
  116. /* -------------------------------------------------------------- */
  117.  
  118. #pragma pack(1)                /* MSC only, ignored by TC/BC */
  119.  
  120. typedef struct exeheadr {       /* Structure of .EXE file header */
  121.        unsigned char sig[2];
  122.        unsigned mod512;
  123.        unsigned num_pages;
  124.        unsigned reloc_cnt;
  125.        unsigned hdr_size;
  126.        unsigned min_para;
  127.        unsigned max_para;
  128.        unsigned stack_seg;
  129.        unsigned start_sp;
  130.        unsigned checksum;        /* here's where we patch... */
  131.        unsigned entry_ip;
  132.        unsigned code_seg;
  133.        unsigned relo_offset;
  134.        unsigned short    ovl_num;
  135.        unsigned short    relo_stuff[80];
  136.        } EXEHDR;
  137.  
  138.  EXEHDR   exehdr;
  139.  
  140. /* --------------------------------------------------------------- */
  141.  
  142. int patch_checksum( char *infile, unsigned value)
  143. {
  144.  size_t stat, check, ok_to_patch = 1;
  145.  FILE *fd;
  146.  
  147.  if (fix_mode)      printf("Zeroing DEMO file patch area...\n");
  148.  if (corrupt_mode)  printf("Corrupting DEMO file...\n");
  149.  
  150.  if ((fd = fopen(infile,"r+b")) == NULL)
  151.   {
  152.      fprintf(stderr,"Unable to open %s .\n",infile);
  153.      return(2);
  154.   }
  155.  
  156.   stat = fread( (void *)&exehdr, 1, sizeof(exehdr), fd);
  157.   if (stat != sizeof(exehdr))
  158.       {  printf ("Error reading exe header of demo file\n");     }
  159.  
  160.   if ((exehdr.sig[0] != 'M') || (exehdr.sig[1] != 'Z'))
  161.       {
  162.          printf ("Error, not a valid EXE file signature.\n");
  163.       }
  164.  
  165.   check = exehdr.checksum;
  166.   if (check != 0 && check != 1)
  167.     {
  168.       printf("Warning unexpected value (%u) found in Demo patch area.\n",check);
  169.       if (!patch_overide) ok_to_patch = 0;
  170.     }
  171.  
  172.   if (ok_to_patch)
  173.            exehdr.checksum = value;
  174.  
  175.   rewind(fd);
  176.   stat = fwrite( (void *)&exehdr, 1, sizeof(exehdr), fd);
  177.   if (stat != sizeof(exehdr))
  178.       {  printf ("Error writing to demo file. \n");     }
  179.  
  180.   fclose(fd);
  181.   return(0);
  182. }
  183.  
  184. /* ------------------------------------------------------ */
  185.