home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast.iso / pcmag / vol8n14.zip / PATCH.C < prev    next >
Text File  |  1989-06-25  |  3KB  |  132 lines

  1.  
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <malloc.h>
  6.  
  7. #define TRUE 1
  8. #define FALSE 0
  9. #define BLOCKS 20
  10.  
  11. struct avirus {
  12.     char code[8];
  13.     int bytes;
  14.     unsigned long lowcrc;
  15.     int crcs[BLOCKS];
  16. } antivir = { "CAPRIO\xDF\0" };
  17.  
  18. main (int argc, char *argv[])
  19. {
  20.     int crc;
  21.     char fname[25];
  22.     char *buff, *buffp;        /* Pointers to data buffer */
  23.     int j1, j2 = 0, j3;
  24.     int found = FALSE;        /* TRUE if patch code found */
  25.     int bsize;                /* Size of CRC block */
  26.     int rsize;                /* Amount of data read into buffer */
  27.     int sector = 0, offset;        /* Position of patch code */
  28.     long fl;                /* File length */
  29.     FILE *fn;
  30.  
  31.     /* Open executable file for reading and writing */
  32.     if (argc == 2)
  33.         strcpy(fname, argv[1]);
  34.     else {
  35.         printf("Filename: ");
  36.         gets(fname);
  37.     }
  38.  
  39.     if ((fn = fopen(fname, "rb+")) == NULL) {
  40.         printf("File not found\n" );
  41.         exit(1);
  42.     }
  43.  
  44.     /* Query user for size of CRC block */
  45.     fl = filelength(fileno(fn));
  46.     do {
  47.         printf ("\nFile size is %ld, max blocks is %d\n", fl, BLOCKS);
  48.         printf ("Lower limit on bytes per block is %d\n", fl/BLOCKS+1);
  49.         printf ("\nEnter number of bytes per block: ");
  50.         scanf ("%d", &bsize);
  51.     } while ((bsize < fl/BLOCKS+1) || (bsize > fl));
  52.  
  53.     /* Find the string that precedes the patch area */
  54.     buff = calloc(1024, sizeof(char));
  55.     rsize = fread(buff, sizeof(char), 512, fn);
  56.  
  57.     while (!eof(fileno(fn)) && !found) {
  58.         sector++;
  59.         rsize = fread(&buff[512], sizeof(char), 512, fn);
  60.         for (offset=0; offset < 512; offset++)
  61.             if (strncmp(&buff[offset], antivir.code, 
  62.                 strlen(antivir.code)) == 0) {
  63.                    found = TRUE;
  64.                    break;
  65.             }
  66.         memcpy(buff, &buff[512], 512);
  67.     }
  68.  
  69.     /* Search the last sector read if patch area not yet found */
  70.     if (!found) {
  71.         sector++;
  72.         for (offset=0; offset < rsize - strlen(antivir.code); offset++)
  73.             if (strncmp(&buff[offset+512], antivir.code, 
  74.                         strlen(antivir.code)) == 0) {
  75.                 found = TRUE;
  76.                 break;
  77.             }
  78.     }
  79.  
  80.     if (found)
  81.         printf("Inserting antivirus data...\n\n");
  82.     else {
  83.         printf("\nPatch area not found in file\n");
  84.         printf("No antivirus data inserted\n");
  85.         exit(1);
  86.     }
  87.  
  88.     /* Calculate CRC values and write them into the executable */
  89.     free(buff);
  90.     buff = calloc(bsize, sizeof(char));
  91.     rewind(fn);
  92.  
  93.     do {
  94.         rsize = fread (buff, sizeof(char), bsize, fn);
  95.         buffp = buff;
  96.         crc = 0;
  97.         for (j1 = 0; j1 < rsize; j1++)
  98.             crc = crc_update (crc, *(buffp++));
  99.         crc = crc_finish(crc);
  100.         antivir.crcs[j2++] = crc;
  101.         printf ("CRC for block %d was %04x\n", j2, crc);
  102.     } while (rsize == bsize);
  103.  
  104.     antivir.bytes = bsize;
  105.     antivir.lowcrc = (sector-1) * 512 + offset;
  106.     fseek (fn, antivir.lowcrc, SEEK_SET);
  107.     fwrite(&antivir, sizeof(struct avirus), 1, fn);
  108.     fclose(fn);
  109. }
  110.  
  111. crc_update(int crcval, char crc_char)
  112. {
  113.     long tmp;
  114.     int  j1;
  115.  
  116.     tmp = ((long)crcval << 8) + crc_char;
  117.     for (j1 = 0; j1 < 8; j1++) {
  118.         tmp = tmp << 1;
  119.         if(tmp & 0x01000000)
  120.             tmp = tmp ^ 0x01800500;
  121.     }
  122.     return((tmp & 0x00ffff00) >> 8);
  123. }
  124.  
  125. crc_finish(int crc)
  126. {
  127.     return(crc_update(crc_update(crc,'\0'), '\0'));
  128. }
  129.  
  130.  
  131.  
  132.