home *** CD-ROM | disk | FTP | other *** search
/ Power CD-ROM!! 7 / POWERCD7.ISO / prgmming / exeval_c / exestamp.c < prev    next >
C/C++ Source or Header  |  1994-07-16  |  2KB  |  72 lines

  1. /* Source code for .EXE file stamping program. Opens an existing .EXE file, */
  2. /* calculated and writes a hash stamp to the end of the file. If you have   */
  3. /* modified the .EXE file validation system in exevalid.c, you will want to */
  4. /* also recompile this program. To do so, create a "makefile" or "project   */
  5. /* file", and add the exestamp.c and exevalid.c files. Then rebuild the     */
  6. /* exestamp.exe program.                                                    */
  7.  
  8. /* Standard header files. */
  9. #include <stdio.h>
  10.  
  11. /* Our header file. */
  12. #include "exevalid.h"
  13.  
  14. /* Program execution begins here. */
  15. int main(int argc, char *argv[])
  16. {
  17.    FILE *pEXEFile;
  18.    tHash nFileHash;
  19.    long lnFileLength;
  20.  
  21.    /* Check that correct number of parameters have been provided. */
  22.    if(argc != 2)
  23.    {
  24.       printf("Format: EXEVALID <Program's .EXE filename>\n");
  25.       return(1);
  26.    }
  27.  
  28.    /* Attempt to open .EXE for read and write in binary mode. */
  29.    pEXEFile = fopen(argv[1], "r+b");
  30.  
  31.    /* If open failed, return with failure. */
  32.    if(pEXEFile == NULL)
  33.    {
  34.       printf("Unable to access file %s.\n", argv[1]);
  35.       return(1);
  36.    }
  37.   
  38.    /* Determine length of .EXE file. */
  39.    fseek(pEXEFile, 0, SEEK_END);
  40.    lnFileLength = ftell(pEXEFile);
  41.    fseek(pEXEFile, 0, SEEK_SET);
  42.  
  43.    /* Determine hash code of file. */
  44.    if(!GetFileHash(pEXEFile, lnFileLength, &nFileHash))
  45.    {
  46.       printf("Unable to calculate hash code of file.\n");
  47.       fclose(pEXEFile);
  48.       return(1);
  49.    }
  50.  
  51.    /* Move to position to write hash code. */
  52.    fseek(pEXEFile, lnFileLength, SEEK_SET);
  53.  
  54.    /* Write hash code to file. */
  55.    if(fwrite(&nFileHash, sizeof(tHash), 1, pEXEFile) != 1)
  56.    {
  57.       printf("Unable to add hash stamp to file.\n");
  58.       fclose(pEXEFile);
  59.       return(1);
  60.    }
  61.  
  62.    /* Close .EXE file. */
  63.    fclose(pEXEFile);
  64.  
  65.    /* Finish with success. */
  66.    printf("Hash stamp has been added to file.\n");
  67.    
  68.    return(0);
  69. }
  70.  
  71.  
  72.