home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / WHATFN.ZIP / ENUMPROC.C next >
C/C++ Source or Header  |  1989-01-22  |  4KB  |  156 lines

  1. /* enumproc.c */
  2. /* enumerates all exported procs from DLL file */
  3. /* Andrew Schulman, 32 Andrews St., Cambridge MA 02139, (617) 876-2102 */
  4. /* 28-May-1988 */
  5. /* revised 12-Oct-1988:  looking at non-resident table too, now */
  6. /* revised 15-Oct-88:  checking to see if it's really a DLL */
  7. /* revised 22-Jan-1989:
  8.     reset nres_state=LOOK_FOR_NONRES (Charles Petzold found bug)
  9.     rewrote using low-level IO instead of stream IO */
  10.  
  11. #include <stdio.h>
  12. #include <io.h>
  13. #include <fcntl.h>
  14.  
  15. #define LOOK_FOR_NONRES     0
  16. #define SAW_NONRES          1
  17. #define SAW_DESCRIPTION     2
  18.  
  19. /* lifted from MSoft newexe.h */
  20. #define ENEWHDR     0x003CL         /* offset of new EXE header */
  21. #define EMAGIC      0x5A4D          /* old EXE magic id:  'MZ'  */
  22. #define NEMAGIC     0x454E          /* new EXE magic id:  'NE'  */
  23.  
  24. /* 
  25. the following are considered to be dynamic link libraries:
  26.     OS/2 DLL (e.g., PMWIN.DLL)
  27.     Windows dynamic link library (e.g., USER.EXE)
  28.     font file (e.g., COURA.FON)
  29.     new device driver (e.g., POINTDD.SYS)
  30.     Windows device driver (e.g., EPSON.DRV)
  31. */
  32. #define DLL_FLAG    0x8000
  33.  
  34. typedef struct
  35. {
  36.     unsigned    ne_magic;
  37.     char        ne_ver;
  38.     char        ne_rev;
  39.     unsigned    ne_enttab;
  40.     unsigned    ne_cbenttab;
  41.     long        ne_crc;
  42.     unsigned    ne_flags;
  43.     unsigned    ne_autodata;
  44.     unsigned    ne_heap;
  45.     unsigned    ne_stack;
  46.     long        ne_csip;
  47.     long        ne_sssp;
  48.     unsigned    ne_cseg;
  49.     unsigned    ne_cmod;
  50.     unsigned    ne_cbnrestab;           /* size of nonres name table */
  51.     unsigned    ne_segtab;
  52.     unsigned    ne_rsrctab;             /* offset of resource table */
  53.     unsigned    ne_restab;              /* offset of resident name table */
  54.     unsigned    ne_modref;
  55.     unsigned    ne_impname;
  56.     long        ne_nonrestab;           /* offset from begin of nres table */
  57. } NEWEXE;
  58.  
  59. char *dll_description(const char *dll, char *buf)
  60. {
  61.     NEWEXE hdr;
  62.     int f;
  63.     unsigned w;
  64.     unsigned char len;
  65.  
  66.     *buf = '\0';
  67.     if ((f = open(dll, O_RDONLY | O_BINARY)) == -1)
  68.         goto closeit;
  69.     read(f, &w, 2);
  70.     if (w != EMAGIC)
  71.         goto closeit;
  72.     lseek(f, ENEWHDR, SEEK_SET);
  73.     read(f, &w, 2);
  74.     lseek(f, /*start_newexe*/ (long) w, SEEK_SET);
  75.     read(f, &hdr, sizeof(NEWEXE));
  76.     if (hdr.ne_magic != NEMAGIC)
  77.         goto closeit;
  78.     lseek(f, hdr.ne_nonrestab, SEEK_SET);
  79.     read(f, &len, 1);
  80.     if (len)
  81.     {
  82.         read(f, buf, len);
  83.         buf[len] = '\0';
  84.     }
  85.     else
  86.         buf[0] = '\0';
  87.     
  88. closeit:    
  89.     if (f != -1) close(f);
  90.     return buf;
  91. }   
  92.  
  93. char *enumproc(const char *dll, char *buf, unsigned *ord)
  94. {
  95.     static NEWEXE hdr;
  96.     static int f = -1;
  97.     static int nres_state = LOOK_FOR_NONRES;
  98.     unsigned w;
  99.     unsigned char len;
  100.     
  101.     if (dll == (char *)-1)
  102.         goto closeit;
  103.     else if (dll)
  104.     {
  105.         unsigned long start_newexe;
  106.         
  107.         if (f) close(f);
  108.         if ((f = open(dll, O_RDONLY | O_BINARY)) == -1)
  109.             goto closeit;
  110.         read(f, &w, 2);
  111.         if (w != EMAGIC)
  112.             goto closeit;
  113.         lseek(f, ENEWHDR, SEEK_SET);
  114.         read(f, &w, 2);
  115.         start_newexe = (long) w;
  116.         lseek(f, start_newexe, SEEK_SET);
  117.         read(f, &hdr, sizeof(NEWEXE));
  118.         if (hdr.ne_magic != NEMAGIC || !(hdr.ne_flags & DLL_FLAG))
  119.             goto closeit;
  120.         lseek(f, start_newexe, SEEK_SET);
  121.         lseek(f, (long) hdr.ne_restab, SEEK_CUR);
  122.     }
  123.  
  124. getone:
  125.     read(f, &len, 1);
  126.     if (len)
  127.     {
  128.         read(f, buf, len);
  129.         buf[len] = '\0';
  130.         read(f, ord, 2);
  131.         if (nres_state == SAW_NONRES)
  132.         {
  133.             /* skip past the module description string */
  134.             nres_state = SAW_DESCRIPTION;
  135.             goto getone;
  136.         }
  137.         return buf;
  138.     }
  139.     else if (nres_state == LOOK_FOR_NONRES)
  140.     {
  141.         lseek(f, hdr.ne_nonrestab, SEEK_SET);   /* from beginning */
  142.         nres_state = SAW_NONRES;
  143.         goto getone;
  144.     }
  145.     else
  146.     {
  147. closeit:
  148.         close(f);
  149.         f = -1;
  150.         nres_state = LOOK_FOR_NONRES;       /* thanks, Charles P.! */
  151.         return NULL;
  152.     }
  153.     /*NOTREACHED*/
  154. }
  155.  
  156.