home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / TROJAN_P / CRCSET13.ZIP / VALIDCRC.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-13  |  3.2 KB  |  126 lines

  1. /*
  2. VALIDCRC.C
  3.  
  4. Kevin Dean
  5. Fairview Mall P.O. Box 55074
  6. 1800 Sheppard Avenue East
  7. Willowdale, Ontario
  8. CANADA    M2J 5B9
  9. CompuServe ID: 76336,3114
  10.  
  11. March 24, 1991
  12.  
  13.     This module validates the CRC of the program in which it is linked.
  14. The code was designed as an anti-virus algorithm.  The CRC is a very effective
  15. method of detecting viruses; any virus that attaches itself to the program
  16. changes the CRC of the program.  The response to an invalid CRC is entirely up
  17. to the programmer.
  18.  
  19.     This code is public domain.
  20. */
  21.  
  22.  
  23. #include <dos.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27.  
  28. #include "viruscrc.h"
  29.  
  30.  
  31. /* Macros to extract low and high bytes of a word. */
  32. #define lowb(x)  (*(unsigned char *)&(x))
  33. #define hib(x)   (*((unsigned char *)&(x) + 1))
  34.  
  35. /* Macros to extract low and high words of a dword. */
  36. #define loww(x)  (*(unsigned short *)&(x))
  37. #define hiw(x)   (*((unsigned short *)&(x) + 1))
  38.  
  39.  
  40. void *bufalloc(size_t *size, size_t minsize);
  41.  
  42.  
  43. #if defined(__TURBOC__)
  44.  
  45. #include <dir.h>
  46. #define findprog(progname, pn)  strcpy(pn, searchpath(progname))
  47. #define argv0  _argv[0]
  48.  
  49. #elif defined(_MSC_VER) || defined(_QC)
  50.  
  51. #define findprog(progname, pn)  _searchenv(progname, getenv("PATH"), pn)
  52. extern char **__argv;
  53. #define argv0  __argv[0]
  54.  
  55. #else
  56. #error Unknown compiler.
  57. #endif
  58.  
  59.  
  60. /***/
  61. /* Calculate CRC of active program and compare it to CRC in _viruscrc. */
  62. int validatecrc(const char *progname)
  63. {
  64. int retcode;            /* Function return code. */
  65.  
  66. if (_viruscrc.x.polynomial != 0)
  67.   {
  68.   unsigned char *buffer;    /* Buffer for program image. */
  69.   size_t bufsize;        /* Buffer size. */
  70.  
  71.   /* Allocate 8k buffer if possible, but get at least 512 bytes. */
  72.   bufsize = 8192;
  73.   buffer = bufalloc(&bufsize, 512);
  74.  
  75.   if (buffer)
  76.     {
  77.     char pn[80];        /* Fully qualified program name. */
  78.     FILE *progfile;        /* Program file. */
  79.  
  80.     if (_osmajor < 3)
  81.       /* Search PATH for program specified in progname. */
  82.       findprog(progname, pn);
  83.     else
  84.       /* Under DOS versions 3 and above, the program name is in argv[0] (passed as argv0). */
  85.       strcpy(pn, argv0);
  86.  
  87.     if ((progfile = fopen(pn, "rb")) != NULL)
  88.       {
  89.       crc32_t table[256];    /* CRC table. */
  90.       register crc32_t *halfi;    /* Pointer to CRC of i / 2. */
  91.       crc32_t crc;        /* Current CRC. */
  92.  
  93.       register size_t i;    /* Byte counter. */
  94.       unsigned char *bufptr;    /* Pointer to walk through buffer. */
  95.  
  96.       /* Generate a CRC lookup table for faster calculation. */
  97.       for (i = 0, halfi = table, table[0] = 0; i < 256; i += 2, halfi++)
  98.     if (hib(hiw(*halfi)) & 0x80)
  99.       table[i] = (table[i + 1] = *halfi << 1) ^ _viruscrc.x.polynomial;
  100.     else
  101.       table[i + 1] = (table[i] = *halfi << 1) ^ _viruscrc.x.polynomial;
  102.  
  103.       crc = 0;
  104.       while ((i = fread(buffer, 1, bufsize, progfile)) != 0)
  105.     for (bufptr = buffer; i--; bufptr++)
  106.       crc = (crc << 8) ^ table[hib(hiw(crc)) ^ *bufptr];
  107.  
  108.       fclose(progfile);
  109.  
  110.       retcode = crc == _viruscrc.x.crc ? CRC_VALID : CRC_INVALID;
  111.       }
  112.     else
  113.       retcode = CRC_FILEERR;
  114.  
  115.     free(buffer);
  116.     }
  117.   else
  118.     retcode = CRC_NOMEM;
  119.   }
  120. else
  121.   /* CRC polynomial must be something other than 0. */
  122.   retcode = CRC_ISZERO;
  123.  
  124. return (retcode);
  125. }
  126.