home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_03_03 / 3n03018a < prev    next >
Text File  |  1992-01-20  |  3KB  |  105 lines

  1.  
  2. -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=Begin Listing1-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  3. /*****************************************************/
  4. /* zapuser.c                                         */
  5. /* -- A program to patch the USER dll with the       */
  6. /*    signed version of the shift right instruction. */
  7. /* -- This patch removes the bug with MLE's created  */
  8. /*    with the ES_CENTER style.                      */
  9. /*****************************************************/
  10.  
  11. #include <fcntl.h>
  12. #include <sys\types.h>
  13. #include <sys\stat.h>
  14. #include <io.h>
  15. #include <process.h>
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18.  
  19. /* Offset of SHR AX,1 instruction in the dll file. */
  20. #define ibDebugA    0x2b940
  21. #define ibRetailA   0x2b660
  22.  
  23. #define wSHR        0xe8d1  /* SHR AX,1 in hex. */
  24. #define wSAR        0xf8d1  /* SAR AX,1 in hex. */
  25.  
  26. int
  27. main(int csz, char * rgsz[])
  28. /*****************************************************/
  29. /* -- Entry point.                                   */
  30. /* -- csz   : Argument count.                        */
  31. /* -- rgsz  : Array of argument strings.             */
  32. /*****************************************************/
  33.     {
  34.     int         nfd;            /* File descriptor. */
  35.     unsigned    wInstruction;   /* Opcode from file. */
  36.     long        ib;             /* Offset of opcode. */
  37.  
  38.     /* Test command line for validity.  Must have 3 */
  39.     /* arguments: */
  40.     /* -- program name, */
  41.     /* -- name of USER dll. */
  42.     /* -- windows version, r: retail, d: debug. */
  43.     if (csz != 3 ||
  44.       (rgsz[2][0] != 'r' && rgsz[2][0] != 'd'))
  45.         {
  46.         fprintf(stderr,
  47.           "Usage: rgsz[0] filename {r|d}\n");
  48.         exit(0);
  49.         }
  50.  
  51.     /* Get file offset based on version. */
  52.     ib = rgsz[2][0] == 'd' ? ibDebugA : ibRetailA;
  53.  
  54.     /* Open file. */
  55.     if ((nfd = open(rgsz[1], O_RDWR)) == -1)
  56.         {
  57.         fprintf(stderr, "Unable to open file %s\n",
  58.           rgsz[1]);
  59.         exit(0);
  60.         }
  61.  
  62.     /* Make sure SHR AX,1 is at the known file */
  63.     /* offset.  Seek to the offset and read the code */
  64.     /* found there.*/
  65.     if (lseek(nfd, ib, SEEK_SET) == -1 ||
  66.       read(nfd, &wInstruction, sizeof wInstruction) !=
  67.       sizeof wInstruction)
  68.         {
  69.         fprintf(stderr, "Premature EOF on file %s\n",
  70.           rgsz[1]);
  71.         goto MainError;
  72.         }
  73.  
  74.     switch (wInstruction)
  75.         {
  76.     default:
  77.         fprintf(stderr, "Unknown version of %s\n",
  78.           rgsz[1]);
  79.         goto MainError;
  80.  
  81.     case wSAR:
  82.         fprintf(stderr,
  83.           "%s has already been patched\n", rgsz[1]);
  84.         goto MainError;
  85.  
  86.     case wSHR:
  87.         break;
  88.         }   /* End switch wInstruction. */
  89.  
  90.     /* Seek back to offset and replace with */
  91.     /* SAR AX,1. */
  92.     lseek(nfd, ib, SEEK_SET);
  93.     wInstruction = wSAR;
  94.     if (write(nfd, &wInstruction, sizeof wInstruction) !=
  95.       sizeof wInstruction)
  96.         fprintf(stderr, "Error writing file %s\n",
  97.           rgsz[1]);
  98.  
  99. MainError:  /* Jump point to save a bunch of calls */
  100.             /* to close() in case of error. */
  101.     close(nfd);
  102.     return 0;
  103.     }
  104. -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-End Listing1=
  105.