home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / DEF.ZIP / DEF1.C < prev    next >
C/C++ Source or Header  |  1989-11-24  |  8KB  |  243 lines

  1. /*****************************************************************************
  2.  
  3.    NAME:          DEF1.C
  4.  
  5.    DESCRIPTION:   This module contains the logic to create and perform
  6.                   lookups into the resident and non-resident name tables
  7.                   for the files mentioned in a given exe's imported
  8.                   name table (excluding DOSCALLS).
  9.  
  10. *****************************************************************************/
  11.  
  12. /*****************************************************************************
  13.                       I N C L U D E   F I L E S
  14. *****************************************************************************/
  15.  
  16. #define  INCL_DOS
  17. #define  INCL_WIN
  18. #define  INCL_ERRORS
  19.  
  20. #include <os2.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include "def.h"
  24.  
  25. /*****************************************************************************
  26.                         D E F I N I T I O N S
  27. *****************************************************************************/
  28.  
  29. /*****************************************************************************
  30.                         G L O B A L   D A T A
  31. *****************************************************************************/
  32.  
  33. SEL            selInfo;                /* selector for info table */
  34. PINFO_TBL      pInfo;                  /* our table of resources */
  35. USHORT         num_entries;            /* entries in info table */
  36.  
  37. static CHAR    *file=__FILE__;         /* for use with error_exit() */
  38.  
  39. extern USHORT  imptab_size;            /* size of imported names table */
  40. extern CHAR    *imptab;                /* imported names table ptr */
  41. extern HATOMTBL hAtomTbl;
  42.  
  43. /*****************************************************************************
  44.                       BuildImportedInfoTable()
  45. *****************************************************************************/
  46.  
  47. VOID BuildImportedInfoTable()
  48. {
  49.    USHORT   i, len, j, rc;
  50.    CHAR     szTmp[256];
  51.  
  52.    /***** First determine the number of strings in imptab *****/
  53.  
  54.    for (i=0, num_entries = 0; i<imptab_size; )
  55.       {
  56.       len = imptab[i];
  57.       if (len)
  58.          num_entries++;
  59.       i += len + 1;
  60.       }
  61.  
  62.    /***** Allocate our information table storage *****/
  63.  
  64.    if (rc=DosAllocSeg(num_entries * sizeof(INFO_TBL), &selInfo, 0))
  65.          error_exit(rc, "DosAllocSeg", __LINE__, file);
  66.  
  67.    pInfo = MAKEP(selInfo, 0);
  68.  
  69.    /***** Step down the imported names table *****/
  70.  
  71.    for (i=0; i<imptab_size; )
  72.       {
  73.       if (len = imptab[i])
  74.          {
  75.          for (j=0; j<len; j++)
  76.             szTmp[j] = imptab[i+j+1];
  77.          szTmp[j] = '\0';
  78.          pInfo->selRestab = 0;
  79.          pInfo->selNRestab = 0;
  80.          if (!(pInfo->atomName = WinAddAtom(hAtomTbl, szTmp)))
  81.             error_exit(0, "WinAddAtom", __LINE__, file);
  82.          if (strcmp(szTmp, "DOSCALLS"))
  83.             GetDLLTables(pInfo, szTmp);
  84.          pInfo++;
  85.          }
  86.       i += len + 1;
  87.       }
  88.    return;
  89. }
  90.  
  91. /*****************************************************************************
  92.                             GetDLLTables()
  93. *****************************************************************************/
  94.  
  95. VOID GetDLLTables(PINFO_TBL pInfo, CHAR *szTmp)
  96. {
  97.    CHAR        szName[256];
  98.    HMODULE     hMod;
  99.    HFILE       hf;
  100.    NEW_EXE     new_exe;                /* new exe header */
  101.    OLD_EXE     old_exe;                /* old exe header */
  102.    USHORT      cbBytes;
  103.    ULONG       ulFilePtr;
  104.    USHORT      usAction;               /* UNUSED, required for DosOpen */
  105.    USHORT      rc;
  106.  
  107.    if (rc=DosLoadModule(szName, sizeof(szName), szTmp, &hMod))
  108.       /***** Not sure here, it may be an imported function *****/
  109.       return;
  110. //      error_exit(rc, "DosLoadModule", __LINE__, file);
  111.  
  112.    if (rc=DosGetModName(hMod, sizeof(szName), szName))
  113.       error_exit(rc, "DosGetModName", __LINE__, file);
  114.  
  115.    if (rc=DosFreeModule(hMod))
  116.       {
  117.       /***** Another Kludge, E.EXE brought us here *****/
  118.  
  119.       if (rc != ERROR_INVALID_ACCESS)
  120.          error_exit(rc, "DosFreeModule", __LINE__, file);
  121.       }
  122.  
  123.    if (rc=DosOpen(szName, &hf, &usAction, 0L, FILE_READONLY, FILE_OPEN,
  124.          OPEN_SHARE_DENYWRITE, 0L))
  125.       error_exit(rc, "DosOpen", __LINE__, file);
  126.  
  127.    /***** Read in the old exe header information *****/
  128.  
  129.    if (rc=DosRead(hf, &old_exe, sizeof(old_exe), &cbBytes))
  130.       error_exit(rc, "DosRead", __LINE__, file);
  131.  
  132.    /***** Isolate the new exe header information *****/
  133.  
  134.    if (rc=DosChgFilePtr(hf, old_exe.lfanew, FILE_BEGIN, &ulFilePtr))
  135.       error_exit(rc, "DosChgFilePtr", __LINE__, file);
  136.  
  137.    /***** Read in the new exe header *****/
  138.  
  139.    if (rc=DosRead(hf, &new_exe, sizeof(new_exe), &cbBytes))
  140.       error_exit(rc, "DosRead", __LINE__, file);
  141.  
  142.    /***** Allocate and read in the Resident Name Table *****/
  143.  
  144.    CreateTable(hf, pInfo->restab_size = new_exe.modtab - new_exe.restab,
  145.                old_exe.lfanew + (long)new_exe.restab,
  146.                &(pInfo->restab), &(pInfo->selRestab));
  147.  
  148.    /***** Allocate and read in the Non-Resident Names Table *****/
  149.  
  150.    CreateTable(hf, pInfo->nrestab_size = new_exe.cbnrestab,
  151.                new_exe.nrestab,
  152.                &(pInfo->nrestab), &(pInfo->selNRestab));
  153.  
  154.    if (rc=DosClose(hf))
  155.       error_exit(rc, "DosClose", __LINE__, file);
  156.  
  157.    return; 
  158. }
  159.    
  160. /*****************************************************************************
  161.                       FreeImportedInfoTable()
  162. *****************************************************************************/
  163.  
  164. VOID FreeImportedInfoTable()
  165. {
  166.    USHORT   i;
  167.    USHORT   rc;
  168.  
  169.    pInfo = MAKEP(selInfo, 0);
  170.    for (i=0; i < num_entries; i++, pInfo++)
  171.       {
  172.       if (pInfo->selRestab)
  173.          if (rc=DosFreeSeg(pInfo->selRestab))
  174.             error_exit(rc, "DosFreeSeg", __LINE__, file);
  175.  
  176.       if (pInfo->selNRestab)
  177.          if (rc=DosFreeSeg(pInfo->selNRestab))
  178.             error_exit(rc, "DosFreeSeg", __LINE__, file);
  179.       }
  180.  
  181.    if (rc=DosFreeSeg(selInfo))
  182.       error_exit(rc, "DosFreeSeg", __LINE__, file);
  183. }
  184.  
  185. /*****************************************************************************
  186.                             TableLookup()
  187. *****************************************************************************/
  188.  
  189. CHAR *TableLookup(CHAR *szName, USHORT ordinal)
  190. {
  191.    ATOM           atom;
  192.    CHAR           *p;
  193.    USHORT         *iptr;
  194.    USHORT         more = TRUE;
  195.    USHORT         i;
  196.    static CHAR    szBuf[256];
  197.    static CHAR    *ptr = szBuf;
  198.  
  199.    pInfo = MAKEP(selInfo, 0);
  200.    if (!(atom=WinFindAtom(hAtomTbl, szName)))
  201.       error_exit(0, "WinFindAtom", __LINE__, file);
  202.  
  203.    for (i=0; i < num_entries; i++, pInfo++)
  204.       if (pInfo->atomName == atom)
  205.          break;
  206.    
  207.    if (i == num_entries)
  208.       error_exit(0, "TableLookup", __LINE__, file);
  209.  
  210.    /***** First let's search the Resident Names Table *****/
  211.  
  212.    p = pInfo->restab;
  213.    p += *p + 3;                        /* bypass the first entry */
  214.    while (p < pInfo->restab + pInfo->restab_size - 1 && more)
  215.       {
  216.       getstring(p, 0, szBuf);          /* grab entry name */
  217.       p += *p + 1;                     /* bypass the length */
  218.       iptr = (USHORT *)p;              /* grab the ordinal */
  219.       if (*iptr == ordinal)
  220.          more = FALSE;
  221.       p += 2;                          /* move on to next table entry */
  222.       }
  223.  
  224.    if (!more)
  225.       return(ptr);
  226.  
  227.    /***** Now the Non-Resident Names Table *****/
  228.  
  229.    p = pInfo->nrestab;
  230.    p += *p + 3;                        /* bypass the first entry */
  231.    while (p < pInfo->nrestab + pInfo->nrestab_size - 1 && more)
  232.       {
  233.       getstring(p, 0, szBuf);          /* grab entry name */
  234.       p += *p + 1;                     /* bypass the length */
  235.       iptr = (USHORT *)p;              /* grab the ordinal */
  236.       if (*iptr == ordinal)
  237.          more = FALSE;
  238.       p += 2;                          /* move on to next table entry */
  239.       }
  240.  
  241.    return(ptr);      
  242. }
  243.