home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / k95source / 1114fix.c < prev    next >
C/C++ Source or Header  |  2020-01-01  |  8KB  |  283 lines

  1. #include <windows.h>
  2.  
  3. #define _PROTOTYP( func, parms ) func parms
  4. #define FILECOUNT 2
  5.  
  6. CHAR * FileName[FILECOUNT] ;
  7. HANDLE hFile[FILECOUNT] ;
  8. HANDLE hFileMapping[FILECOUNT] ;
  9. CHAR * pView[FILECOUNT] ;
  10. FILETIME ftLastAccess[FILECOUNT], ftCreation[FILECOUNT], ftLastWrite[FILECOUNT] ;
  11.  
  12. #ifdef _M_IX86
  13. #define K95_SIZE            1431552    
  14. #define K95_OFFSET          0x14D4C0
  15. #define K95_PREBYTE         ((CHAR)0x0)
  16. #define K95_POSTBYTE        ((CHAR)0x88)
  17. #define K95DIAL_SIZE        739840
  18. #define K95DIAL_OFFSET      0xA0FA8
  19. #define K95DIAL_PREBYTE     ((CHAR)0x0)
  20. #define K95DIAL_POSTBYTE    ((CHAR)0x57)
  21. #else 
  22. #ifdef _M_MPPC
  23. #define K95_SIZE            2049536
  24. #define K95_OFFSET          0x1C2CAB
  25. #define K95_PREBYTE         ((CHAR)0x0)
  26. #define K95_POSTBYTE        ((CHAR)0x0)
  27. #define K95DIAL_SIZE        1196544
  28. #define K95DIAL_OFFSET      0xFE280
  29. #define K95DIAL_PREBYTE     ((CHAR)0x0)
  30. #define K95DIAL_POSTBYTE    ((CHAR)0x0)
  31. #else
  32. #ifdef _M_ALPHA
  33. #define K95_SIZE            2371584
  34. #define K95_OFFSET          0x192C68
  35. #define K95_PREBYTE         ((CHAR)0x46)
  36. #define K95_POSTBYTE        ((CHAR)0x88)
  37. #define K95DIAL_SIZE        897024
  38. #define K95DIAL_OFFSET      0xB3CC8
  39. #define K95DIAL_PREBYTE     ((CHAR)0x0)
  40. #define K95DIAL_POSTBYTE    ((CHAR)0x25)
  41. #else
  42. PLATFORM NOT SUPPORTED
  43. #endif
  44. #endif
  45. #endif
  46.  
  47. int FixIt = 0;
  48.  
  49. char *zinptr=NULL;
  50. int zincnt=0, zoutcnt=0;
  51. char *zoutptr=NULL;
  52. void zdstuff(char c) {};
  53.  
  54.  
  55. int
  56. ckOpenFile(int i)
  57. {
  58.    hFile[i] = CreateFile(
  59.                    FileName[i],    // address of name of the file 
  60.                    GENERIC_READ | GENERIC_WRITE,
  61.                    0,     // share mode 
  62.                    NULL,// address of security descriptor 
  63.                    OPEN_EXISTING,    // how to create 
  64.                    FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,    // file attributes 
  65.                    NULL     // handle of file with attributes to copy  
  66.    );
  67.  
  68.    if ( !hFile[i] )
  69.       return 0;
  70.     
  71.    GetFileTime( hFile[i], &ftCreation[i], &ftLastAccess[i], &ftLastWrite[i] ) ;
  72.    hFileMapping[i] = CreateFileMapping( hFile[i],
  73.                                             NULL,
  74.                                             PAGE_READWRITE,
  75.                                             0,
  76.                                             0,
  77.                                             NULL );
  78.  
  79.    pView[i] = (CHAR *) MapViewOfFile( hFileMapping[i],
  80.                                  FILE_MAP_WRITE,
  81.                                  0,0,0);
  82.  
  83.    if ( !pView[i] )
  84.       return 0;
  85.    return 1;
  86. }
  87.  
  88. int 
  89. ckCloseFile( int i ) 
  90. {
  91.    if (pView[i])
  92.       UnmapViewOfFile( pView[i] ) ;
  93.    if (hFileMapping[i])
  94.       CloseHandle( hFileMapping[i] ) ;
  95.    if (hFile[i])
  96.    {
  97.       SetFileTime( hFile[i], &ftCreation[i], &ftLastAccess[i], &ftLastWrite[i] ) ;
  98.       CloseHandle( hFile[i] ) ;
  99.    }
  100.    return 1;
  101. }
  102.  
  103. int 
  104. SearchForRegBlock( int i ) 
  105. {
  106.    CHAR * SearchString =   "====758914360207174068160528073" ;
  107.    int    SearchStringLength = 136 ;
  108.    int SearchLength = strlen(SearchString) ;
  109.    BY_HANDLE_FILE_INFORMATION fileInfo ;
  110.    CHAR * start = NULL, * prebug = NULL, * postbug = NULL ;
  111.    ULONG prebugpos=0, postbugpos=0;
  112.    CHAR * p = NULL ;
  113.    int found = 0 ;
  114.    ULONG k = 0 ;
  115.  
  116.    GetFileInformationByHandle( hFile[i], &fileInfo ) ;
  117.  
  118.    //  We only search for the first null terminated portion of the search string 
  119.    //  because we want to be able to find a pre-registered copy
  120.  
  121.    if ( !pView[i] )
  122.       return 0 ;
  123.  
  124.     /* are the files the correct length? */
  125.     switch ( i ) {
  126.     case 0:
  127.         if ( fileInfo.nFileSizeLow != K95_SIZE ) {
  128.             printf("ERROR: Incorrect version of K95.EXE.\n");
  129.             return(0);
  130.         }
  131.         break;
  132.     case 1:
  133.         if ( fileInfo.nFileSizeLow != K95DIAL_SIZE ) {
  134.             printf("ERROR: Incorrect version of K95DIAL.EXE.\n");
  135.             return(0);
  136.         }
  137.         break;
  138.     }
  139.  
  140.    start = pView[i] ;
  141.    p = pView[i] ;
  142.    found = 0 ;
  143.  
  144.    while ( !found && k < fileInfo.nFileSizeLow )
  145.    {
  146.        LONG j = 0 ;
  147.        if ( p[k] == SearchString[0] )
  148.        { 
  149.            start = &p[k] ;
  150.            for ( j = 0 ; j < SearchLength ; j++ )
  151.            {
  152.                if ( start[j] != SearchString[j] )
  153.                    break;
  154.            }
  155.            if ( j == SearchLength )
  156.                found = 1 ;
  157.        } 
  158.        k++ ;
  159.    }
  160.  
  161.    if ( found )
  162.    {
  163.        prebug = start - 1;
  164.        prebugpos = start - pView[i] - 1;
  165.        postbug = start + SearchStringLength;
  166.        postbugpos = start - pView[i] + SearchStringLength;
  167.  
  168.        printf("\nRegistration Block of %d bytes starts at %d (0x%X)\n", 
  169.                SearchStringLength, 
  170.                start - pView[i],
  171.                start - pView[i]) ;
  172.        printf(" Pre-Bug position: %d (0x%X)  value: 0x%X\n",
  173.                prebugpos,prebugpos,(CHAR)*prebug);
  174.        printf("Post-Bug position: %d (0x%X)  value: 0x%X\n",
  175.                postbugpos,postbugpos,(CHAR)*postbug);
  176.  
  177.        switch ( i ) {
  178.        case 0:  /* K95 */
  179.            if ( (prebugpos != K95_OFFSET - 1) ||
  180.                 (postbugpos != K95_OFFSET + SearchStringLength)) {
  181.                printf("ERROR: Incorrect version of K95.EXE.\n");
  182.            }
  183.            else {
  184.                if (*prebug == K95_PREBYTE && *postbug == K95_POSTBYTE)
  185.                {
  186.                    printf("This version of K95.EXE does not contain the Patch bug.\n");
  187.                }
  188.                else {
  189.                    printf("This version of K95.EXE contains the Patch bug.");
  190.                    if ( FixIt ) {
  191.                        *prebug = K95_PREBYTE;
  192.                        *postbug = K95_POSTBYTE;
  193.                        printf("  REPAIRED");
  194.                    }
  195.                    else {
  196.                        printf("\nRun again with the /F switch to repair the bug.");
  197.                    }
  198.                    printf("\n");
  199.                }
  200.            }
  201.            break;
  202.        case 1:  /* K95DIAL */
  203.            if ( (prebugpos != K95DIAL_OFFSET - 1) ||
  204.                 (postbugpos != K95DIAL_OFFSET + SearchStringLength)) {
  205.                printf("ERROR: Incorrect version of K95DIAL.EXE.\n");
  206.            }
  207.            else {
  208.                if (*prebug == K95DIAL_PREBYTE && *postbug == K95DIAL_POSTBYTE)
  209.                {
  210.                    printf("This version of K95DIAL.EXE does not contain the Patch bug.\n");
  211.                }
  212.                else {
  213.                    printf("This version of K95DIAL.EXE contains the Patch bug.");
  214.                    if ( FixIt ) {
  215.                        *prebug = K95DIAL_PREBYTE;
  216.                        *postbug = K95DIAL_POSTBYTE;
  217.                        printf("  REPAIRED");
  218.                    }
  219.                    else {
  220.                        printf("\nRun again with the /F switch to repair the bug.");
  221.                    }
  222.                    printf("\n");
  223.                }
  224.            }
  225.            break;
  226.        }
  227.    }
  228.    return found;
  229. }
  230.  
  231.  
  232. int
  233. main( int argc, char ** argv ) 
  234. {
  235.     int dsn = 0 ;
  236.     int i ;
  237.  
  238.     FileName[0] = "k95.exe" ;
  239.     FileName[1] = "k95dial.exe" ;
  240.  
  241.     printf("This program corrects a bug in Kermit-95 Version 1.1.14\n");
  242.     printf("introduced during the patch procedure from a previous release\n");
  243.  
  244.     if ( argc > 2 || 
  245.          (argc == 2 && ((argv[1][0] != '-' && argv[1][0] != '/') ||
  246.            (argv[1][1] != 'f' && argv[1][1] != 'F')))) {
  247.         printf("ERROR: Usage %s [/F]\n", argv[0]);
  248.         return(2);
  249.     }
  250.     else if ( argc == 2 ) {
  251.         printf("Fix it mode turned on.\n\n");
  252.         FixIt = 1 ;
  253.     }
  254.  
  255.     for ( i=0 ; i < FILECOUNT ; i++ )
  256.     {
  257.         printf("Correcting %s if necessary ...\n",FileName[i]);
  258.  
  259.         hFile[i]        = NULL ;
  260.         hFileMapping[i] = NULL ;
  261.         pView[i]        = NULL ;
  262.  
  263.         if ( !ckOpenFile(i) )
  264.         {
  265.             printf("ERROR: Unable to open %s\n\n", FileName[i]) ;
  266.             continue ;
  267.         }
  268.  
  269.         if ( !SearchForRegBlock(i) )
  270.         {
  271.             // Registration Block not found.
  272.             printf( "ERROR: %s skipped.\n", FileName[i] ) ;
  273.         }
  274.         else {
  275.             printf("\n");
  276.         }
  277.         ckCloseFile(i) ;
  278.  
  279.     }
  280.     return(0);
  281. }
  282.  
  283.