home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / IMPLIST.ZIP / IMPFILT.C next >
C/C++ Source or Header  |  1991-07-28  |  6KB  |  237 lines

  1. /* 
  2.  *
  3.  * SYNOPSIS        ( OS/2 )
  4.  *
  5.  *    EXEHDR /V  exe-path | IMPFILT implist-path > IMPLIST.LST" ;
  6.  *
  7.  *
  8.  *
  9.  * DESCRIPTION
  10.  *
  11.  *     IMPFILT  Will lookup imported function names from corresponding 
  12.  *    cardinal numbers on the output from the MICROSOFT EXEHDR utility.
  13.  *    The implist-path is a file created by the IMPLIST program included
  14.  *    in this package.  This can be helpful when reverse engineering a 
  15.  * software component such as PSTAT.EXE.
  16.  *
  17.  *
  18.  * COMPILATION
  19.  *
  20.  *    See impfilt.mak for a simple makefile to be used with NMAKE. 
  21.  *
  22.  * AUTHOR
  23.  *
  24.  *     Stephen L. Reed, Stephen Reed Software.  
  25.  *
  26.  *
  27.  *         NET:        70401.3342@compuserve.com
  28.  *         BIX:        sreed
  29.  *         COMPUSERVE: 70401,3342
  30.  *
  31.  *
  32.  *         Your comments are appreciated!
  33.  *
  34.  *
  35.  * COPYRIGHT
  36.  *
  37.  *    Copyright (c) 1990-91  Stephen L. Reed.  All rights reserved.
  38.  *
  39.  *    Redistribution and use in source and binary forms are permitted
  40.  *    provided that the above copyright notice and this paragraph are
  41.  *    duplicated in all such forms and that any documentation,
  42.  *    advertising materials, and other materials related to such
  43.  *    distribution and use acknowledge that the software was developed
  44.  *    by Stephen L. Reed.
  45.  *
  46.  *    THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  47.  *    IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  48.  *    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  49.  *
  50.  */
  51.  
  52.  
  53. #include    <stdio.h>             /* Library I/O routines.    */
  54. #include    <stdlib.h>            /* Library common routines. */
  55. #include    <string.h>            /* Library string routines. */
  56.  
  57. #define     INCL_BASE
  58. #include    <os2.h>
  59.  
  60. #include    "SYMTYPES.H"
  61.  
  62. CHAR              szImpList [ 20 ]     = "" ;
  63. FILE *            hfImpList ;
  64.  
  65. PCHAR             ErrTxt1              =
  66.    "Usage: EXEHDR /V  exe-path | IMPFILT implist-path > IMPLIST.LST" ;
  67.  
  68. USHORT            cExternalNameLen     = 0 ;
  69. UCHAR             szExternalName [ 50 ] ;
  70. PUCHAR            pszExternalName      = szExternalName ;
  71.  
  72. USHORT            cDllFunctionLen      = 0 ;
  73. UCHAR             szDllFunction [ 50 ] ;
  74. PUCHAR            pszDllFunction       = szDllFunction ;
  75.  
  76. USHORT            usStatus             = 0 ;
  77.  
  78. CHAR              szText [ 200 ]       = "" ;
  79. CHAR *            pszText ;
  80. USHORT            cTextLen ;
  81. CHAR              szLookupText [ 200 ] = "" ;
  82. CHAR *            pszLookupText ;
  83.  
  84.  
  85. PUCHAR            LookupExternalName ( PUCHAR szDllFunction ) ;
  86.  
  87.  
  88. /*------------------------------------------------------------*/
  89. /* Main function.                                             */
  90. /*------------------------------------------------------------*/
  91.  
  92. main ( USHORT argc, PCHAR argv [] )
  93.    {
  94.  
  95.    if ( argc < 2 )
  96.       {
  97.       printf ( "%s\n", ErrTxt1 ) ;
  98.       exit ( 1 ) ;
  99.       }
  100.  
  101.    if ( strlen ( argv [ 1 ] ) < 2 )
  102.       {
  103.       printf ( "%s\n", ErrTxt1 ) ;
  104.       exit ( 1 ) ;
  105.       }
  106.  
  107.    strcpy ( szImpList, argv [ 1 ] ) ;
  108.  
  109.  
  110.                /*---------------------------------------------*/
  111.                /* Copy the file.                              */
  112.                /*---------------------------------------------*/
  113.  
  114.  
  115.    pszText  = gets ( szText ) ;
  116.  
  117.    while ( pszText )
  118.       {
  119.       cTextLen = strlen ( szText ) ;
  120.  
  121.                /*---------------------------------------------*/
  122.                /* For imported functions, lookup the DLL      */
  123.                /* Ordinal and append the function external    */
  124.                /* name.                                       */
  125.                /*---------------------------------------------*/
  126.  
  127.       if (( cTextLen > 25 ) &&
  128.           ( strncmp ( pszText + 16, "  imp ", 6 ) == 0 ))
  129.          {
  130.          USHORT   cSpacesToInsert   = 0 ;
  131.  
  132.          pszText += 22 ;
  133.          pszDllFunction = szDllFunction ;
  134.  
  135.          while ( *pszText )
  136.             *pszDllFunction++ = *pszText++ ;
  137.  
  138.          *pszDllFunction++    = '\0' ;
  139.  
  140.          if ( cTextLen < 40 )
  141.             cSpacesToInsert   = 40 - cTextLen ;
  142.  
  143.          strncat (
  144.             szText,
  145.             "                    ",
  146.             cSpacesToInsert ) ;
  147.  
  148.          strcat (
  149.             szText,
  150.             LookupExternalName ( szDllFunction )) ;
  151.  
  152.          }
  153.  
  154.       puts ( szText ) ;
  155.  
  156.  
  157.       pszText  = gets ( szText ) ;
  158.  
  159.       }
  160.  
  161.    exit ( 0 ) ;
  162.  
  163.                /* Suppress warning message.                   */
  164.    return ( 0 ) ;
  165.  
  166.    }           /* End of main function.                       */
  167.  
  168.  
  169.  
  170. /*------------------------------------------------------------*/
  171. /* Lookup External Name.                                      */
  172. /*------------------------------------------------------------*/
  173.  
  174. PUCHAR            LookupExternalName ( PUCHAR szDllFunction )
  175.    {
  176.    strcpy ( szExternalName, "" ) ;
  177.    hfImpList                        = fopen ( szImpList, "r" ) ;
  178.  
  179.    if ( ! hfImpList )
  180.       {
  181.       printf ( "\nError - Cannot open %s file\n\n", szImpList ) ;
  182.  
  183.       exit ( 1 ) ;
  184.       }
  185.  
  186.    for ( ; ; )
  187.       {
  188.  
  189.       pszLookupText  = fgets ( szLookupText, 200, hfImpList ) ;
  190.  
  191.                /*---------------------------------------------*/
  192.                /* Not found.                                  */
  193.                /*---------------------------------------------*/
  194.  
  195.       if ( ! pszLookupText )
  196.          break ;
  197.  
  198.       cDllFunctionLen   = strlen ( szDllFunction ) ;
  199.  
  200.       if ( cDllFunctionLen + 34 == strlen ( szLookupText ))
  201.          {
  202.  
  203.          if ( strncmp ( pszLookupText + 33, szDllFunction, cDllFunctionLen )
  204.               == 0 )
  205.             {
  206.                /*---------------------------------------------*/
  207.                /* Found.                                      */
  208.                /*---------------------------------------------*/
  209.  
  210.             pszExternalName   = szExternalName ;
  211.  
  212.             while ( *pszLookupText != ' ' )
  213.                *pszExternalName++  = *pszLookupText++ ;
  214.  
  215.             *pszExternalName       = '\0' ;
  216.  
  217.             break ;
  218.             }
  219.          }
  220.       }
  221.  
  222.    fclose ( hfImpList ) ;
  223.  
  224.    return ( szExternalName ) ;
  225.  
  226.    }           /* End of LookupExternalName function.         */
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.