home *** CD-ROM | disk | FTP | other *** search
/ Programming Win32 Under the API / ProgrammingWin32UnderTheApiPatVillani.iso / dsasmsrc.zip / pedump.c < prev    next >
Text File  |  1999-08-13  |  163KB  |  4,885 lines

  1. //
  2. //
  3. // This program was written by Sang Cho, associate professor at 
  4. //                                       the department of 
  5. //                                       computer science and engineering
  6. //                                       chongju university
  7. // this program is based on the program pefile.c
  8. // which is written by Randy Kath(Microsoft Developmer Network Technology Group)
  9. // in june 12, 1993.
  10. // I have investigated P.E. file format as thoroughly as possible,
  11. // but I cannot claim that I am an expert yet, so some of its information  
  12. // may give you wrong results.
  13. //
  14. //
  15. //
  16. // language used: djgpp
  17. // date of creation: September 28, 1997
  18. //
  19. // date of first release: October 15, 1997
  20. //
  21. // date of second release: August 30, 1998 (alpha version)
  22. //
  23. //
  24. //      you can contact me: e-mail address: sangcho@alpha94.chongju.ac.kr
  25. //                            hitel id: chokhas
  26. //                        phone number: (0431) 229-8491    +82-431-229-8491
  27. //
  28. //            real address: Sang Cho
  29. //                      Computer and Information Engineering
  30. //                      ChongJu University
  31. //                      NaeDok-Dong 36 
  32. //                      ChongJu 360-764
  33. //                      South Korea
  34. //
  35. //   Copyright (C) 1997, 1998                            by Sang Cho.
  36. //
  37. //   Permission is granted to make and distribute verbatim copies of this
  38. // program provided the copyright notice and this permission notice are
  39. // preserved on all copies.
  40. //
  41. //
  42. // File: pedump.c ( I included header file into source file. )
  43.  
  44. # include "disasm.h"
  45.  
  46. #define VOID                void
  47. #define BOOLEAN             boolean
  48. #define FALSE               0
  49. #define TRUE                1
  50. #define CONST               const
  51. #define LOWORD(l)           ((WORD)(l))
  52. #define WINAPI
  53.  
  54. //
  55. // Image Format
  56. //
  57.  
  58. #define IMAGE_DOS_SIGNATURE                 0x5A4D      // MZ
  59. #define IMAGE_OS2_SIGNATURE                 0x454E      // NE
  60. #define IMAGE_OS2_SIGNATURE_LE              0x454C      // LE
  61. #define IMAGE_VXD_SIGNATURE                 0x454C      // LE
  62. #define IMAGE_NT_SIGNATURE                  0x00004550  // PE00
  63.  
  64. typedef struct _IMAGE_DOS_HEADER {      // DOS .EXE header
  65.     WORD   e_magic;                     // Magic number
  66.     WORD   e_cblp;                      // Bytes on last page of file
  67.     WORD   e_cp;                        // Pages in file
  68.     WORD   e_crlc;                      // Relocations
  69.     WORD   e_cparhdr;                   // Size of header in paragraphs
  70.     WORD   e_minalloc;                  // Minimum extra paragraphs needed
  71.     WORD   e_maxalloc;                  // Maximum extra paragraphs needed
  72.     WORD   e_ss;                        // Initial (relative) SS value
  73.     WORD   e_sp;                        // Initial SP value
  74.     WORD   e_csum;                      // Checksum
  75.     WORD   e_ip;                        // Initial IP value
  76.     WORD   e_cs;                        // Initial (relative) CS value
  77.     WORD   e_lfarlc;                    // File address of relocation table
  78.     WORD   e_ovno;                      // Overlay number
  79.     WORD   e_res[4];                    // Reserved words
  80.     WORD   e_oemid;                     // OEM identifier (for e_oeminfo)
  81.     WORD   e_oeminfo;                   // OEM information; e_oemid specific
  82.     WORD   e_res2[10];                  // Reserved words
  83.     LONG   e_lfanew;                    // File address of new exe header
  84.   } IMAGE_DOS_HEADER, *PIMAGE_DOS_HEADER;
  85.  
  86. //
  87. // File header format.
  88. //
  89.  
  90.  
  91.  
  92. typedef struct _IMAGE_FILE_HEADER {
  93.     WORD    Machine;
  94.     WORD    NumberOfSections;
  95.     DWORD   TimeDateStamp;
  96.     DWORD   PointerToSymbolTable;
  97.     DWORD   NumberOfSymbols;
  98.     WORD    SizeOfOptionalHeader;
  99.     WORD    Characteristics;
  100. } IMAGE_FILE_HEADER, *PIMAGE_FILE_HEADER;
  101.  
  102. #define IMAGE_SIZEOF_FILE_HEADER             20
  103.  
  104. #define IMAGE_FILE_RELOCS_STRIPPED           0x0001  // Relocation info stripped from file.
  105. #define IMAGE_FILE_EXECUTABLE_IMAGE          0x0002  // File is executable  (i.e. no unresolved externel references).
  106. #define IMAGE_FILE_LINE_NUMS_STRIPPED        0x0004  // Line nunbers stripped from file.
  107. #define IMAGE_FILE_LOCAL_SYMS_STRIPPED       0x0008  // Local symbols stripped from file.
  108. #define IMAGE_FILE_BYTES_REVERSED_LO         0x0080  // Bytes of machine word are reversed.
  109. #define IMAGE_FILE_32BIT_MACHINE             0x0100  // 32 bit word machine.
  110. #define IMAGE_FILE_DEBUG_STRIPPED            0x0200  // Debugging info stripped from file in .DBG file
  111. #define IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP   0x0400  // If Image is on removable media, copy and run from the swap file.
  112. #define IMAGE_FILE_NET_RUN_FROM_SWAP         0x0800  // If Image is on Net, copy and run from the swap file.
  113. #define IMAGE_FILE_SYSTEM                    0x1000  // System File.
  114. #define IMAGE_FILE_DLL                       0x2000  // File is a DLL.
  115. #define IMAGE_FILE_UP_SYSTEM_ONLY            0x4000  // File should only be run on a UP machine
  116. #define IMAGE_FILE_BYTES_REVERSED_HI         0x8000  // Bytes of machine word are reversed.
  117.  
  118. #define IMAGE_FILE_MACHINE_UNKNOWN           0
  119. #define IMAGE_FILE_MACHINE_I386              0x14c   // Intel 386.
  120. #define IMAGE_FILE_MACHINE_R3000             0x162   // MIPS little-endian, 0x160 big-endian
  121. #define IMAGE_FILE_MACHINE_R4000             0x166   // MIPS little-endian
  122. #define IMAGE_FILE_MACHINE_R10000            0x168   // MIPS little-endian
  123. #define IMAGE_FILE_MACHINE_ALPHA             0x184   // Alpha_AXP
  124. #define IMAGE_FILE_MACHINE_POWERPC           0x1F0   // IBM PowerPC Little-Endian
  125.  
  126.  
  127.  
  128. //
  129. // Directory format.
  130. //
  131.  
  132. typedef struct _IMAGE_DATA_DIRECTORY {
  133.     DWORD   VirtualAddress;
  134.     DWORD   Size;
  135. } IMAGE_DATA_DIRECTORY, *PIMAGE_DATA_DIRECTORY;
  136.  
  137. #define IMAGE_NUMBEROF_DIRECTORY_ENTRIES    16
  138.  
  139. //
  140. // Optional header format.
  141. //
  142.  
  143. typedef struct _IMAGE_OPTIONAL_HEADER {
  144.     //
  145.     // Standard fields.
  146.     //
  147.  
  148.     WORD    Magic;
  149.     BYTE    MajorLinkerVersion;
  150.     BYTE    MinorLinkerVersion;
  151.     DWORD   SizeOfCode;
  152.     DWORD   SizeOfInitializedData;
  153.     DWORD   SizeOfUninitializedData;
  154.     DWORD   AddressOfEntryPoint;
  155.     DWORD   BaseOfCode;
  156.     DWORD   BaseOfData;
  157.  
  158.     //
  159.     // NT additional fields.
  160.     //
  161.  
  162.     DWORD   ImageBase;
  163.     DWORD   SectionAlignment;
  164.     DWORD   FileAlignment;
  165.     WORD    MajorOperatingSystemVersion;
  166.     WORD    MinorOperatingSystemVersion;
  167.     WORD    MajorImageVersion;
  168.     WORD    MinorImageVersion;
  169.     WORD    MajorSubsystemVersion;
  170.     WORD    MinorSubsystemVersion;
  171.     DWORD   Win32VersionValue;
  172.     DWORD   SizeOfImage;
  173.     DWORD   SizeOfHeaders;
  174.     DWORD   CheckSum;
  175.     WORD    Subsystem;
  176.     WORD    DllCharacteristics;
  177.     DWORD   SizeOfStackReserve;
  178.     DWORD   SizeOfStackCommit;
  179.     DWORD   SizeOfHeapReserve;
  180.     DWORD   SizeOfHeapCommit;
  181.     DWORD   LoaderFlags;
  182.     DWORD   NumberOfRvaAndSizes;
  183.     IMAGE_DATA_DIRECTORY DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES];
  184. } IMAGE_OPTIONAL_HEADER, *PIMAGE_OPTIONAL_HEADER;
  185.  
  186.  
  187. typedef struct _IMAGE_NT_HEADERS {
  188.     DWORD Signature;
  189.     IMAGE_FILE_HEADER FileHeader;
  190.     IMAGE_OPTIONAL_HEADER OptionalHeader;
  191. } IMAGE_NT_HEADERS, *PIMAGE_NT_HEADERS;
  192.  
  193.  
  194. // Directory Entries
  195.  
  196. #define IMAGE_DIRECTORY_ENTRY_EXPORT         0   // Export Directory
  197. #define IMAGE_DIRECTORY_ENTRY_IMPORT         1   // Import Directory
  198. #define IMAGE_DIRECTORY_ENTRY_RESOURCE       2   // Resource Directory
  199. #define IMAGE_DIRECTORY_ENTRY_EXCEPTION      3   // Exception Directory
  200. #define IMAGE_DIRECTORY_ENTRY_SECURITY       4   // Security Directory
  201. #define IMAGE_DIRECTORY_ENTRY_BASERELOC      5   // Base Relocation Table
  202. #define IMAGE_DIRECTORY_ENTRY_DEBUG          6   // Debug Directory
  203. #define IMAGE_DIRECTORY_ENTRY_COPYRIGHT      7   // Description String
  204. #define IMAGE_DIRECTORY_ENTRY_GLOBALPTR      8   // Machine Value (MIPS GP)
  205. #define IMAGE_DIRECTORY_ENTRY_TLS            9   // TLS Directory
  206. #define IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG   10   // Load Configuration Directory
  207. #define IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT  11   // Bound Import Directory in headers
  208. #define IMAGE_DIRECTORY_ENTRY_IAT           12   // Import Address Table
  209.  
  210. //
  211. // Section header format.
  212. //
  213.  
  214. /*
  215. #define IMAGE_SIZEOF_SHORT_NAME              8
  216.  
  217. typedef struct _IMAGE_SECTION_HEADER {
  218.     BYTE    Name[IMAGE_SIZEOF_SHORT_NAME];
  219.     union {
  220.         DWORD   PhysicalAddress;
  221.         DWORD   VirtualSize;
  222.     } Misc;
  223.     DWORD   VirtualAddress;
  224.     DWORD   SizeOfRawData;
  225.     DWORD   PointerToRawData;
  226.     DWORD   PointerToRelocations;
  227.     DWORD   PointerToLinenumbers;
  228.     WORD    NumberOfRelocations;
  229.     WORD    NumberOfLinenumbers;
  230.     DWORD   Characteristics;
  231. } IMAGE_SECTION_HEADER, *PIMAGE_SECTION_HEADER;
  232.  */
  233.  
  234. #define IMAGE_SIZEOF_SECTION_HEADER          40
  235.  
  236.  
  237. //
  238. // Export Format
  239. //
  240.  
  241. typedef struct _IMAGE_EXPORT_DIRECTORY {
  242.     DWORD   Characteristics;
  243.     DWORD   TimeDateStamp;
  244.     WORD    MajorVersion;
  245.     WORD    MinorVersion;
  246.     DWORD   Name;
  247.     DWORD   Base;
  248.     DWORD   NumberOfFunctions;
  249.     DWORD   NumberOfNames;
  250.     PDWORD  *AddressOfFunctions;
  251.     PDWORD  *AddressOfNames;
  252.     PWORD   *AddressOfNameOrdinals;
  253. } IMAGE_EXPORT_DIRECTORY, *PIMAGE_EXPORT_DIRECTORY;
  254.  
  255. //
  256. // Import Format
  257. //
  258.  
  259. typedef struct _IMAGE_IMPORT_BY_NAME {
  260.     WORD    Hint;
  261.     BYTE    Name[1];
  262. } IMAGE_IMPORT_BY_NAME, *PIMAGE_IMPORT_BY_NAME;
  263.  
  264. #define IMAGE_ORDINAL_FLAG 0x80000000
  265. #define IMAGE_ORDINAL(Ordinal) (Ordinal & 0xffff)
  266.  
  267.  
  268. //
  269. // Resource Format.
  270. //
  271.  
  272. //
  273. // Resource directory consists of two counts, following by a variable length
  274. // array of directory entries.  The first count is the number of entries at
  275. // beginning of the array that have actual names associated with each entry.
  276. // The entries are in ascending order, case insensitive strings.  The second
  277. // count is the number of entries that immediately follow the named entries.
  278. // This second count identifies the number of entries that have 16-bit integer
  279. // Ids as their name.  These entries are also sorted in ascending order.
  280. //
  281. // This structure allows fast lookup by either name or number, but for any
  282. // given resource entry only one form of lookup is supported, not both.
  283. // This is consistant with the syntax of the .RC file and the .RES file.
  284. //
  285.  
  286. // Predefined resource types ... there may be some more, but I don't have
  287. //                               the information yet.  .....sang cho.....
  288.  
  289. #define    RT_NEWRESOURCE   0x2000
  290. #define    RT_ERROR         0x7fff
  291. #define    RT_CURSOR        1
  292. #define    RT_BITMAP        2
  293. #define    RT_ICON          3
  294. #define    RT_MENU          4
  295. #define    RT_DIALOG        5
  296. #define    RT_STRING        6
  297. #define    RT_FONTDIR       7
  298. #define    RT_FONT          8
  299. #define    RT_ACCELERATORS  9
  300. #define    RT_RCDATA        10
  301. #define    RT_MESSAGETABLE  11
  302. #define    RT_GROUP_CURSOR  12
  303. #define    RT_GROUP_ICON    14
  304. #define    RT_VERSION       16
  305. #define    NEWBITMAP        (RT_BITMAP|RT_NEWRESOURCE)
  306. #define    NEWMENU          (RT_MENU|RT_NEWRESOURCE)
  307. #define    NEWDIALOG        (RT_DIALOG|RT_NEWRESOURCE)
  308.  
  309.  
  310. typedef struct _IMAGE_RESOURCE_DIRECTORY {
  311.     DWORD   Characteristics;
  312.     DWORD   TimeDateStamp;
  313.     WORD    MajorVersion;
  314.     WORD    MinorVersion;
  315.     WORD    NumberOfNamedEntries;
  316.     WORD    NumberOfIdEntries;
  317. //  IMAGE_RESOURCE_DIRECTORY_ENTRY DirectoryEntries[1];
  318. } IMAGE_RESOURCE_DIRECTORY, *PIMAGE_RESOURCE_DIRECTORY;
  319.  
  320. #define IMAGE_RESOURCE_NAME_IS_STRING        0x80000000
  321. #define IMAGE_RESOURCE_DATA_IS_DIRECTORY     0x80000000
  322.  
  323. //
  324. // Each directory contains the 32-bit Name of the entry and an offset,
  325. // relative to the beginning of the resource directory of the data associated
  326. // with this directory entry.  If the name of the entry is an actual text
  327. // string instead of an integer Id, then the high order bit of the name field
  328. // is set to one and the low order 31-bits are an offset, relative to the
  329. // beginning of the resource directory of the string, which is of type
  330. // IMAGE_RESOURCE_DIRECTORY_STRING.  Otherwise the high bit is clear and the
  331. // low-order 16-bits are the integer Id that identify this resource directory
  332. // entry. If the directory entry is yet another resource directory (i.e. a
  333. // subdirectory), then the high order bit of the offset field will be
  334. // set to indicate this.  Otherwise the high bit is clear and the offset
  335. // field points to a resource data entry.
  336. //
  337.  
  338. typedef struct _IMAGE_RESOURCE_DIRECTORY_ENTRY {
  339.     DWORD    Name;
  340.     DWORD    OffsetToData;
  341. } IMAGE_RESOURCE_DIRECTORY_ENTRY, *PIMAGE_RESOURCE_DIRECTORY_ENTRY;
  342.  
  343. //
  344. // For resource directory entries that have actual string names, the Name
  345. // field of the directory entry points to an object of the following type.
  346. // All of these string objects are stored together after the last resource
  347. // directory entry and before the first resource data object.  This minimizes
  348. // the impact of these variable length objects on the alignment of the fixed
  349. // size directory entry objects.
  350. //
  351.  
  352. typedef struct _IMAGE_RESOURCE_DIRECTORY_STRING {
  353.     WORD    Length;
  354.     CHAR    NameString[ 1 ];
  355. } IMAGE_RESOURCE_DIRECTORY_STRING, *PIMAGE_RESOURCE_DIRECTORY_STRING;
  356.  
  357.  
  358. typedef struct _IMAGE_RESOURCE_DIR_STRING_U {
  359.     WORD    Length;
  360.     WCHAR   NameString[ 1 ];
  361. } IMAGE_RESOURCE_DIR_STRING_U, *PIMAGE_RESOURCE_DIR_STRING_U;
  362.  
  363.  
  364. //
  365. // Each resource data entry describes a leaf node in the resource directory
  366. // tree.  It contains an offset, relative to the beginning of the resource
  367. // directory of the data for the resource, a size field that gives the number
  368. // of bytes of data at that offset, a CodePage that should be used when
  369. // decoding code point values within the resource data.  Typically for new
  370. // applications the code page would be the unicode code page.
  371. //
  372.  
  373. typedef struct _IMAGE_RESOURCE_DATA_ENTRY {
  374.     DWORD   OffsetToData;
  375.     DWORD   Size;
  376.     DWORD   CodePage;
  377.     DWORD   Reserved;
  378. } IMAGE_RESOURCE_DATA_ENTRY, *PIMAGE_RESOURCE_DATA_ENTRY;
  379.  
  380. //                                       
  381. // BitmapInfoHeader used in DIB Header (Icons, Cursors, Group ...s)
  382. //
  383.  
  384. typedef struct tagBITMAPINFOHEADER {    /* bmih */
  385.     DWORD   biSize;
  386.     LONG    biWidth;
  387.     LONG    biHeight;
  388.     WORD    biPlanes;
  389.     WORD    biBitCount;
  390.     DWORD   biCompression;
  391.     DWORD   biSizeImage;
  392.     LONG    biXPelsPerMeter;
  393.     LONG    biYPelsPerMeter;
  394.     DWORD   biClrUsed;
  395.     DWORD   biClrImportant;
  396. } BITMAPINFOHEADER, *PBITMAPINFOHEADER;
  397.  
  398. typedef struct tagRGBQUAD {     /* rgbq */
  399.     BYTE    rgbBlue;
  400.     BYTE    rgbGreen;
  401.     BYTE    rgbRed;
  402.     BYTE    rgbReserved;
  403. } RGBQUAD, *PRGBQUAD;
  404.  
  405. // Icon Resources       ... addes by Sang Cho
  406.  
  407. typedef struct ICONDIR {
  408.     WORD          idReserved;
  409.     WORD          idType;
  410.     WORD          idCount;
  411. //ICONDIRENTRY idEntries[1];
  412. } ICONHEADER, *PICONHEADER;
  413.  
  414. struct IconDirectoryEntry {
  415.     BYTE  bWidth;
  416.     BYTE  bHeight;
  417.     BYTE  bColorCount;
  418.     BYTE  bReserved;
  419.     WORD  wPlanes;
  420.     WORD  wBitCount;
  421.     DWORD dwBytesInRes;
  422.     DWORD dwImageOffset;
  423. } ICONDIRENTRY, *PICONDIRENTRY;
  424.  
  425.  
  426. //  Menu Resources       ... added by .....sang cho....
  427.  
  428. // Menu resources are composed of a menu header followed by a sequential list
  429. // of menu items. There are two types of menu items: pop-ups and normal menu
  430. // itmes. The MENUITEM SEPARATOR is a special case of a normal menu item with
  431. // an empty name, zero ID, and zero flags.
  432.  
  433. typedef struct _IMAGE_MENU_HEADER{
  434.     WORD   wVersion;      // Currently zero
  435.     WORD   cbHeaderSize;  // Also zero
  436. } IMAGE_MENU_HEADER, *PIMAGE_MENU_HEADER;
  437.  
  438. typedef struct _IMAGE_POPUP_MENU_ITEM{
  439.     WORD   fItemFlags;  
  440.     WCHAR  szItemText[1];
  441. } IMAGE_POPUP_MENU_ITEM, *PIMAGE_POPUP_MENU_ITEM;
  442.  
  443. typedef struct _IMAGE_NORMAL_MENU_ITEM{
  444.     WORD   fItemFlags;  
  445.     WORD   wMenuID;
  446.     WCHAR  szItemText[1];
  447. } IMAGE_NORMAL_MENU_ITEM, *PIMAGE_NORMAL_MENU_ITEM;
  448.  
  449. #define GRAYED       0x0001 // GRAYED keyword
  450. #define INACTIVE     0x0002 // INACTIVE keyword
  451. #define BITMAP       0x0004 // BITMAP keyword
  452. #define OWNERDRAW    0x0100 // OWNERDRAW keyword
  453. #define CHECKED      0x0008 // CHECKED keyword
  454. #define POPUP        0x0010 // used internally
  455. #define MENUBARBREAK 0x0020 // MENUBARBREAK keyword
  456. #define MENUBREAK    0x0040 // MENUBREAK keyword
  457. #define ENDMENU      0x0080 // used internally
  458.  
  459.  
  460. // Dialog Box Resources .................. added by sang cho.
  461.  
  462. // A dialog box is contained in a single resource and has a header and 
  463. // a portion repeated for each control in the dialog box.
  464. // The item DWORD IStyle is a standard window style composed of flags found
  465. // in WINDOWS.H.
  466. // The default style for a dialog box is:
  467. // WS_POPUP | WS_BORDER | WS_SYSMENU
  468. // 
  469. // The itme marked "Name or Ordinal" are :
  470. // If the first word is an 0xffff, the next two bytes contain an ordinal ID.
  471. // Otherwise, the first one or more WORDS contain a double-null-terminated string.
  472. // An empty string is represented by a single WORD zero in the first location.
  473. // 
  474. // The WORD wPointSize and WCHAR szFontName entries are present if the FONT
  475. // statement was included for the dialog box. This can be detected by checking
  476. // the entry IStyle. If IStyle & DS_SETFONT ( which is 0x40), then these
  477. // entries will be present.
  478.  
  479. typedef struct _IMAGE_DIALOG_BOX_HEADER1{
  480.     DWORD  IStyle;
  481.     DWORD  IExtendedStyle;    // New for Windows NT
  482.     WORD   nControls;         // Number of Controls
  483.     WORD   x;
  484.     WORD   y;
  485.     WORD   cx;
  486.     WORD   cy;
  487. //      N_OR_O MenuName;         // Name or Ordinal ID
  488. //      N_OR_O ClassName;                // Name or Ordinal ID
  489. //      WCHAR  szCaption[];
  490. //      WORD   wPointSize;       // Only here if FONT set for dialog
  491. //      WCHAR  szFontName[];     // This too
  492. } IMAGE_DIALOG_HEADER, *PIMAGE_DIALOG_HEADER;
  493.  
  494. typedef union _NAME_OR_ORDINAL{    // Name or Ordinal ID
  495.     struct _ORD_ID{
  496.         WORD   flgId;
  497.     WORD   Id;
  498.     } ORD_ID;
  499.     WCHAR  szName[1];      
  500. } NAME_OR_ORDINAL, *PNAME_OR_ORDINAL;
  501.  
  502. // The data for each control starts on a DWORD boundary (which may require
  503. // some padding from the previous control), and its format is as follows:
  504.  
  505. typedef struct _IMAGE_CONTROL_DATA{
  506.     DWORD   IStyle;
  507.     DWORD   IExtendedStyle;
  508.     WORD    x;
  509.     WORD    y;
  510.     WORD    cx;
  511.     WORD    cy;
  512.     WORD    wId;
  513. //  N_OR_O  ClassId;
  514. //  N_OR_O  Text;
  515. //  WORD    nExtraStuff;
  516. } IMAGE_CONTROL_DATA, *PIMAGE_CONTROL_DATA;
  517.  
  518. #define BUTTON       0x80
  519. #define EDIT         0x81
  520. #define STATIC       0x82
  521. #define LISTBOX      0x83
  522. #define SCROLLBAR    0x84
  523. #define COMBOBOX     0x85
  524.  
  525. // The various statements used in a dialog script are all mapped to these
  526. // classes along with certain modifying styles. The values for these styles
  527. // can be found in WINDOWS.H. All dialog controls have the default styles
  528. // of WS_CHILD and WS_VISIBLE. A list of the default styles used follows:
  529. //
  530. // Statement           Default Class         Default Styles
  531. // CONTROL             None                  WS_CHILD|WS_VISIBLE
  532. // LTEXT               STATIC                ES_LEFT
  533. // RTEXT               STATIC                ES_RIGHT
  534. // CTEXT               STATIC                ES_CENTER
  535. // LISTBOX             LISTBOX               WS_BORDER|LBS_NOTIFY
  536. // CHECKBOX            BUTTON                BS_CHECKBOX|WS_TABSTOP
  537. // PUSHBUTTON          BUTTON                BS_PUSHBUTTON|WS_TABSTOP
  538. // GROUPBOX            BUTTON                BS_GROUPBOX
  539. // DEFPUSHBUTTON       BUTTON                BS_DFPUSHBUTTON|WS_TABSTOP
  540. // RADIOBUTTON         BUTTON                BS_RADIOBUTTON
  541. // AUTOCHECKBOX        BUTTON                BS_AUTOCHECKBOX
  542. // AUTO3STATE          BUTTON                BS_AUTO3STATE
  543. // AUTORADIOBUTTON     BUTTON                BS_AUTORADIOBUTTON
  544. // PUSHBOX             BUTTON                BS_PUSHBOX
  545. // STATE3              BUTTON                BS_3STATE
  546. // EDITTEXT            EDIT                  ES_LEFT|WS_BORDER|WS_TABSTOP
  547. // COMBOBOX            COMBOBOX              None
  548. // ICON                STATIC                SS_ICON
  549. // SCROLLBAR           SCROLLBAR             None
  550. ///
  551.  
  552. #define WS_OVERLAPPED   0x00000000L
  553. #define WS_POPUP        0x80000000L
  554. #define WS_CHILD        0x40000000L
  555. #define WS_CLIPSIBLINGS 0x04000000L
  556. #define WS_CLIPCHILDREN 0x02000000L
  557. #define WS_VISIBLE      0x10000000L
  558. #define WS_DISABLED     0x08000000L
  559. #define WS_MINIMIZE     0x20000000L
  560. #define WS_MAXIMIZE     0x01000000L
  561. #define WS_CAPTION      0x00C00000L
  562. #define WS_BORDER       0x00800000L
  563. #define WS_DLGFRAME     0x00400000L
  564. #define WS_VSCROLL      0x00200000L
  565. #define WS_HSCROLL      0x00100000L
  566. #define WS_SYSMENU      0x00080000L
  567. #define WS_THICKFRAME   0x00040000L
  568. #define WS_MINIMIZEBOX  0x00020000L
  569. #define WS_MAXIMIZEBOX  0x00010000L
  570. #define WS_GROUP        0x00020000L
  571. #define WS_TABSTOP      0x00010000L
  572.  
  573. // other aliases
  574. #define WS_OVERLAPPEDWINDOW (WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX)
  575. #define WS_POPUPWINDOW  (WS_POPUP | WS_BORDER | WS_SYSMENU)
  576. #define WS_CHILDWINDOW  (WS_CHILD)
  577. #define WS_TILED        WS_OVERLAPPED
  578. #define WS_ICONIC       WS_MINIMIZE
  579. #define WS_SIZEBOX      WS_THICKFRAME
  580. #define WS_TILEDWINDOW  WS_OVERLAPPEDWINDOW
  581.  
  582. #define WS_EX_DLGMODALFRAME     0x00000001L
  583. #define WS_EX_NOPARENTNOTIFY    0x00000004L
  584. #define WS_EX_TOPMOST           0x00000008L
  585. #define WS_EX_ACCEPTFILES       0x00000010L
  586. #define WS_EX_TRANSPARENT       0x00000020L
  587.  
  588. #define BS_PUSHBUTTON           0x00000000L
  589. #define BS_DEFPUSHBUTTON        0x00000001L
  590. #define BS_CHECKBOX             0x00000002L
  591. #define BS_AUTOCHECKBOX         0x00000003L
  592. #define BS_RADIOBUTTON          0x00000004L
  593. #define BS_3STATE               0x00000005L
  594. #define BS_AUTO3STATE           0x00000006L
  595. #define BS_GROUPBOX             0x00000007L
  596. #define BS_USERBUTTON           0x00000008L
  597. #define BS_AUTORADIOBUTTON      0x00000009L
  598. #define BS_OWNERDRAW            0x0000000BL
  599. #define BS_LEFTTEXT             0x00000020L
  600.  
  601. #define ES_LEFT         0x00000000L
  602. #define ES_CENTER       0x00000001L
  603. #define ES_RIGHT        0x00000002L
  604. #define ES_MULTILINE    0x00000004L
  605. #define ES_UPPERCASE    0x00000008L
  606. #define ES_LOWERCASE    0x00000010L
  607. #define ES_PASSWORD     0x00000020L
  608. #define ES_AUTOVSCROLL  0x00000040L
  609. #define ES_AUTOHSCROLL  0x00000080L
  610. #define ES_NOHIDESEL    0x00000100L
  611. #define ES_OEMCONVERT   0x00000400L
  612. #define ES_READONLY     0x00000800L
  613. #define ES_WANTRETURN   0x00001000L
  614.  
  615. #define LBS_NOTIFY            0x0001L
  616. #define LBS_SORT              0x0002L
  617. #define LBS_NOREDRAW          0x0004L
  618. #define LBS_MULTIPLESEL       0x0008L
  619. #define LBS_OWNERDRAWFIXED    0x0010L
  620. #define LBS_OWNERDRAWVARIABLE 0x0020L
  621. #define LBS_HASSTRINGS        0x0040L
  622. #define LBS_USETABSTOPS       0x0080L
  623. #define LBS_NOINTEGRALHEIGHT  0x0100L
  624. #define LBS_MULTICOLUMN       0x0200L
  625. #define LBS_WANTKEYBOARDINPUT 0x0400L
  626. #define LBS_EXTENDEDSEL       0x0800L
  627. #define LBS_DISABLENOSCROLL   0x1000L
  628.  
  629. #define SS_LEFT             0x00000000L
  630. #define SS_CENTER           0x00000001L
  631. #define SS_RIGHT            0x00000002L
  632. #define SS_ICON             0x00000003L
  633. #define SS_BLACKRECT        0x00000004L
  634. #define SS_GRAYRECT         0x00000005L
  635. #define SS_WHITERECT        0x00000006L
  636. #define SS_BLACKFRAME       0x00000007L
  637. #define SS_GRAYFRAME        0x00000008L
  638. #define SS_WHITEFRAME       0x00000009L
  639. #define SS_SIMPLE           0x0000000BL
  640. #define SS_LEFTNOWORDWRAP   0x0000000CL
  641. #define SS_BITMAP           0x0000000EL
  642.  
  643. //
  644. // Debug Format
  645. //
  646.  
  647. typedef struct _IMAGE_DEBUG_DIRECTORY {
  648.     DWORD   Characteristics;
  649.     DWORD   TimeDateStamp;
  650.     WORD    MajorVersion;
  651.     WORD    MinorVersion;
  652.     DWORD   Type;
  653.     DWORD   SizeOfData;
  654.     DWORD   AddressOfRawData;
  655.     DWORD   PointerToRawData;
  656. } IMAGE_DEBUG_DIRECTORY, *PIMAGE_DEBUG_DIRECTORY;
  657.  
  658. #define IMAGE_DEBUG_TYPE_UNKNOWN          0
  659. #define IMAGE_DEBUG_TYPE_COFF             1
  660. #define IMAGE_DEBUG_TYPE_CODEVIEW         2
  661. #define IMAGE_DEBUG_TYPE_FPO              3
  662. #define IMAGE_DEBUG_TYPE_MISC             4
  663. #define IMAGE_DEBUG_TYPE_EXCEPTION        5
  664. #define IMAGE_DEBUG_TYPE_FIXUP            6
  665. #define IMAGE_DEBUG_TYPE_OMAP_TO_SRC      7
  666. #define IMAGE_DEBUG_TYPE_OMAP_FROM_SRC    8
  667.  
  668.  
  669. typedef struct _IMAGE_DEBUG_MISC {
  670.     DWORD       DataType;               // type of misc data, see defines
  671.     DWORD       Length;                 // total length of record, rounded to four
  672.                     // byte multiple.
  673.     BOOLEAN     Unicode;                // TRUE if data is unicode string
  674.     BYTE        Reserved[ 3 ];
  675.     BYTE        Data[ 1 ];              // Actual data
  676. } IMAGE_DEBUG_MISC, *PIMAGE_DEBUG_MISC;
  677.  
  678.  
  679. //
  680. // Debugging information can be stripped from an image file and placed
  681. // in a separate .DBG file, whose file name part is the same as the
  682. // image file name part (e.g. symbols for CMD.EXE could be stripped
  683. // and placed in CMD.DBG).  This is indicated by the IMAGE_FILE_DEBUG_STRIPPED
  684. // flag in the Characteristics field of the file header.  The beginning of
  685. // the .DBG file contains the following structure which captures certain
  686. // information from the image file.  This allows a debug to proceed even if
  687. // the original image file is not accessable.  This header is followed by
  688. // zero of more IMAGE_SECTION_HEADER structures, followed by zero or more
  689. // IMAGE_DEBUG_DIRECTORY structures.  The latter structures and those in
  690. // the image file contain file offsets relative to the beginning of the
  691. // .DBG file.
  692. //
  693. // If symbols have been stripped from an image, the IMAGE_DEBUG_MISC structure
  694. // is left in the image file, but not mapped.  This allows a debugger to
  695. // compute the name of the .DBG file, from the name of the image in the
  696. // IMAGE_DEBUG_MISC structure.
  697. //
  698.  
  699. typedef struct _IMAGE_SEPARATE_DEBUG_HEADER {
  700.     WORD        Signature;
  701.     WORD        Flags;
  702.     WORD        Machine;
  703.     WORD        Characteristics;
  704.     DWORD       TimeDateStamp;
  705.     DWORD       CheckSum;
  706.     DWORD       ImageBase;
  707.     DWORD       SizeOfImage;
  708.     DWORD       NumberOfSections;
  709.     DWORD       ExportedNamesSize;
  710.     DWORD       DebugDirectorySize;
  711.     DWORD       SectionAlignment;
  712.     DWORD       Reserved[2];
  713. } IMAGE_SEPARATE_DEBUG_HEADER, *PIMAGE_SEPARATE_DEBUG_HEADER;
  714.  
  715. #define IMAGE_SEPARATE_DEBUG_SIGNATURE  0x4944
  716.  
  717. #define IMAGE_SEPARATE_DEBUG_FLAGS_MASK 0x8000
  718. #define IMAGE_SEPARATE_DEBUG_MISMATCH   0x8000  // when DBG was updated, the
  719.                         // old checksum didn't match.
  720.  
  721.  
  722. //
  723. // End Image Format
  724. //
  725.  
  726.  
  727. #define SIZE_OF_NT_SIGNATURE    sizeof (DWORD)
  728. #define MAXRESOURCENAME         13
  729.  
  730. /* global macros to define header offsets into file */
  731. /* offset to PE file signature                      */
  732. #define NTSIGNATURE(a) ((LPVOID)((BYTE *)a       +  \
  733.              ((PIMAGE_DOS_HEADER)a)->e_lfanew))
  734.  
  735. /* DOS header identifies the NT PEFile signature dword
  736.    the PEFILE header exists just after that dword   */
  737. #define PEFHDROFFSET(a) ((LPVOID)((BYTE *)a      +  \
  738.              ((PIMAGE_DOS_HEADER)a)->e_lfanew    +  \
  739.              SIZE_OF_NT_SIGNATURE))
  740.  
  741. /* PE optional header is immediately after PEFile header */
  742. #define OPTHDROFFSET(a) ((LPVOID)((BYTE *)a      +  \
  743.              ((PIMAGE_DOS_HEADER)a)->e_lfanew    +  \
  744.              SIZE_OF_NT_SIGNATURE                +  \
  745.              sizeof (IMAGE_FILE_HEADER)))
  746.  
  747. /* section headers are immediately after PE optional header */
  748. #define SECHDROFFSET(a) ((LPVOID)((BYTE *)a      +  \
  749.              ((PIMAGE_DOS_HEADER)a)->e_lfanew    +  \
  750.              SIZE_OF_NT_SIGNATURE                +  \
  751.              sizeof (IMAGE_FILE_HEADER)          +  \
  752.              sizeof (IMAGE_OPTIONAL_HEADER)))
  753.  
  754.  
  755. typedef struct tagImportDirectory
  756.     {
  757.     DWORD    dwRVAFunctionNameList;
  758.     DWORD    dwUseless1;
  759.     DWORD    dwUseless2;
  760.     DWORD    dwRVAModuleName;
  761.     DWORD    dwRVAFunctionAddressList;
  762.     }IMAGE_IMPORT_MODULE_DIRECTORY, * PIMAGE_IMPORT_MODULE_DIRECTORY;
  763.  
  764.  
  765. /* global prototypes for functions in pefile.c */
  766. /* PE file header info */
  767. BOOL    WINAPI GetDosHeader (LPVOID, PIMAGE_DOS_HEADER);
  768. DWORD   WINAPI ImageFileType (LPVOID);
  769. BOOL    WINAPI GetPEFileHeader (LPVOID, PIMAGE_FILE_HEADER);
  770.  
  771. /* PE optional header info */
  772. BOOL    WINAPI GetPEOptionalHeader (LPVOID, PIMAGE_OPTIONAL_HEADER);
  773. LPVOID  WINAPI GetModuleEntryPoint (LPVOID);
  774. int     WINAPI NumOfSections (LPVOID);
  775. LPVOID  WINAPI GetImageBase (LPVOID);
  776. LPVOID  WINAPI ImageDirectoryOffset (LPVOID, DWORD);
  777. LPVOID  WINAPI ImageDirectorySection (LPVOID, DWORD);
  778.  
  779. /* PE section header info */
  780. int     WINAPI GetSectionNames (LPVOID, char **);
  781. BOOL    WINAPI GetSectionHdrByName (LPVOID, PIMAGE_SECTION_HEADER, char *);
  782.  
  783. //
  784. // structur to store string tokens
  785. //
  786. typedef struct _Str_P {
  787.     char    flag;                 // string_flag '@' or '%' or '#'
  788.     char    *pos;                 // starting postion of string
  789.     int     length;       // length of string
  790.     BOOL    wasString;    // if it were stringMode or not
  791. } Str_P;
  792.  
  793. /* import section info */
  794. int    WINAPI GetImportModuleNames (LPVOID, char  **);
  795. int    WINAPI GetImportFunctionNamesByModule (LPVOID, char *, char  **);
  796.  
  797. // import function name reporting
  798. int    WINAPI GetStringLength (char *);
  799. int    WINAPI GetPreviousParamString (char *, char *);
  800. int    WINAPI TranslateParameters (char **, char **, char **);
  801. BOOL   WINAPI StringExpands (char **, char **, char **, Str_P *);
  802. LPVOID WINAPI TranslateFunctionName (char *);
  803.  
  804. /* export section info */
  805. int     WINAPI GetExportFunctionNames (LPVOID, char **);
  806.  
  807. /* resource section info */
  808. int    WINAPI GetNumberOfResources (LPVOID);
  809. int    WINAPI GetListOfResourceTypes (LPVOID, char **);
  810. int    WINAPI MenuScan (int *, WORD **);
  811. int    WINAPI MenuFill (char **, WORD **);
  812. void   WINAPI StrangeMenuFill (char **, WORD **, int);
  813. int    WINAPI GetContentsOfMenu (LPVOID, char **);
  814. int    WINAPI PrintMenu (int, char **);
  815. int    WINAPI PrintStrangeMenu (char **);
  816. int    WINAPI dumpMenu (char **,int);
  817.  
  818. /* debug section info */
  819. BOOL   WINAPI IsDebugInfoStripped (LPVOID);
  820. int    WINAPI RetrieveModuleName (LPVOID, char **);
  821. BOOL   WINAPI IsDebugFile (LPVOID);
  822. BOOL   WINAPI GetSeparateDebugHeader (LPVOID, PIMAGE_SEPARATE_DEBUG_HEADER);
  823.  
  824.  
  825. /* copy dos header information to structure */
  826. BOOL  WINAPI GetDosHeader (
  827.     LPVOID               lpFile,
  828.     PIMAGE_DOS_HEADER    pHeader)
  829. {
  830.     /* dos header rpresents first structure of bytes in file */
  831.     if (*(USHORT *)lpFile == IMAGE_DOS_SIGNATURE)
  832.     memcpy((LPVOID)pHeader, lpFile, sizeof (IMAGE_DOS_HEADER));
  833.     else
  834.     return FALSE;
  835.  
  836.     return TRUE;
  837. }
  838.  
  839. /* return file signature */
  840. DWORD  WINAPI ImageFileType (
  841.     LPVOID    lpFile)
  842. {
  843.     /* dos file signature comes first */
  844.     if (*(USHORT *)lpFile == IMAGE_DOS_SIGNATURE)
  845.     {
  846.     /* determine location of PE File header from dos header */
  847.     if (LOWORD (*(DWORD *)NTSIGNATURE (lpFile)) == IMAGE_OS2_SIGNATURE ||
  848.         LOWORD (*(DWORD *)NTSIGNATURE (lpFile)) == IMAGE_OS2_SIGNATURE_LE)
  849.         return (DWORD)LOWORD(*(DWORD *)NTSIGNATURE (lpFile));
  850.  
  851.     else if (*(DWORD *)NTSIGNATURE (lpFile) == IMAGE_NT_SIGNATURE)
  852.         return IMAGE_NT_SIGNATURE;
  853.  
  854.     else
  855.         return IMAGE_DOS_SIGNATURE;
  856.     }
  857.  
  858.     else
  859.     /* unknown file type */
  860.     return 0;
  861. }
  862.  
  863. /* copy file header information to structure */
  864. BOOL  WINAPI GetPEFileHeader (
  865.     LPVOID                lpFile,
  866.     PIMAGE_FILE_HEADER    pHeader)
  867. {
  868.     /* file header follows dos header */
  869.     if (ImageFileType (lpFile) == IMAGE_NT_SIGNATURE)
  870.     memcpy((LPVOID)pHeader,  PEFHDROFFSET (lpFile), sizeof (IMAGE_FILE_HEADER));
  871.     else
  872.     return FALSE;
  873.  
  874.     return TRUE;
  875. }
  876.  
  877. /* copy optional header info to structure */
  878. BOOL WINAPI GetPEOptionalHeader (
  879.     LPVOID                    lpFile,
  880.     PIMAGE_OPTIONAL_HEADER    pHeader)
  881. {
  882.     /* optional header follows file header and dos header */
  883.     if (ImageFileType (lpFile) == IMAGE_NT_SIGNATURE)
  884.     memcpy ((LPVOID)pHeader,  OPTHDROFFSET (lpFile), sizeof (IMAGE_OPTIONAL_HEADER));
  885.     else
  886.     return FALSE;
  887.  
  888.     return TRUE;
  889. }
  890.  
  891. /* function returns the entry point for an exe module lpFile must
  892.    be a memory mapped file pointer to the beginning of the image file */
  893. LPVOID  WINAPI GetModuleEntryPoint (
  894.     LPVOID    lpFile)
  895. {
  896.     PIMAGE_OPTIONAL_HEADER   poh = (PIMAGE_OPTIONAL_HEADER)OPTHDROFFSET (lpFile);
  897.  
  898.     if (poh != NULL)
  899.     return (LPVOID)(poh->AddressOfEntryPoint);
  900.     else
  901.     return NULL;
  902. }
  903.  
  904. /* return the total number of sections in the module */
  905. int   WINAPI NumOfSections (
  906.     LPVOID    lpFile)
  907. {
  908.     /* number os sections is indicated in file header */
  909.     return ((int)((PIMAGE_FILE_HEADER)PEFHDROFFSET (lpFile))->NumberOfSections);
  910. }
  911.  
  912. /* retrieve entry point */
  913. LPVOID  WINAPI GetImageBase (
  914.     LPVOID    lpFile)
  915. {
  916.     PIMAGE_OPTIONAL_HEADER   poh = (PIMAGE_OPTIONAL_HEADER)OPTHDROFFSET (lpFile);
  917.  
  918.     if (poh != NULL)
  919.     return (LPVOID)(poh->ImageBase);
  920.     else
  921.     return NULL;
  922. }
  923.  
  924. //
  925. // This function is written by sang cho
  926. //                                                 .. october 5, 1997
  927. //
  928. /* function returns the actual address of given RVA,      lpFile must
  929.    be a memory mapped file pointer to the beginning of the image file */
  930. LPVOID  WINAPI GetActualAddress (
  931.     LPVOID    lpFile,
  932.     DWORD     dwRVA)
  933. {
  934.     //PIMAGE_OPTIONAL_HEADER   poh = (PIMAGE_OPTIONAL_HEADER)OPTHDROFFSET (lpFile);
  935.     PIMAGE_SECTION_HEADER    psh = (PIMAGE_SECTION_HEADER)SECHDROFFSET (lpFile);
  936.     int                      nSections = NumOfSections (lpFile);
  937.     int                      i = 0;
  938.  
  939.     if (dwRVA == 0) return 0;
  940.  
  941.     /* locate section containing image directory */
  942.     while (i++<nSections)
  943.     {
  944.         if (psh->VirtualAddress <= (DWORD)dwRVA &&
  945.         psh->VirtualAddress + psh->SizeOfRawData > (DWORD)dwRVA)
  946.         break;
  947.         psh++;
  948.     }
  949.  
  950.     if (i > nSections)
  951.     return 0;
  952.  
  953.     /* return image import directory offset */
  954.     return (LPVOID)(((int)lpFile + (int)dwRVA - psh->VirtualAddress) +
  955.                    (int)psh->PointerToRawData);
  956. }
  957.  
  958. //
  959. // This function is modified by sang cho
  960. //
  961. //
  962. /* return offset to specified IMAGE_DIRECTORY entry */
  963. LPVOID  WINAPI ImageDirectoryOffset (
  964.     LPVOID    lpFile,
  965.     DWORD     dwIMAGE_DIRECTORY)
  966. {
  967.     PIMAGE_OPTIONAL_HEADER   poh = (PIMAGE_OPTIONAL_HEADER)OPTHDROFFSET (lpFile);
  968.     PIMAGE_SECTION_HEADER    psh = (PIMAGE_SECTION_HEADER)SECHDROFFSET (lpFile);
  969.     int                      nSections = NumOfSections (lpFile);
  970.     int                      i = 0;
  971.     LPVOID                   VAImageDir;
  972.  
  973.     /* must be 0 thru (NumberOfRvaAndSizes-1) */
  974.     if (dwIMAGE_DIRECTORY >= poh->NumberOfRvaAndSizes)
  975.     return NULL;
  976.  
  977.     /* locate specific image directory's relative virtual address */
  978.     VAImageDir = (LPVOID)poh->DataDirectory[dwIMAGE_DIRECTORY].VirtualAddress;
  979.  
  980.     if (VAImageDir == NULL) return NULL;
  981.     /* locate section containing image directory */
  982.     while (i++<nSections)
  983.     {
  984.         if (psh->VirtualAddress <= (DWORD)VAImageDir &&
  985.         psh->VirtualAddress + psh->SizeOfRawData > (DWORD)VAImageDir)
  986.         break;
  987.         psh++;
  988.     }
  989.  
  990.     if (i > nSections)
  991.     return NULL;
  992.  
  993.     /* return image import directory offset */
  994.     return (LPVOID)(((int)lpFile + (int)VAImageDir - psh->VirtualAddress) +
  995.                    (int)psh->PointerToRawData);
  996. }
  997.  
  998. /* function retrieve names of all the sections in the file */
  999. int WINAPI GetSectionNames (
  1000.     LPVOID    lpFile,
  1001.     char      **pszSections)
  1002. {
  1003.     int                      nSections = NumOfSections (lpFile);
  1004.     int                      i, nCnt = 0;
  1005.     PIMAGE_SECTION_HEADER    psh;
  1006.     char                     *ps;
  1007.  
  1008.  
  1009.     if (ImageFileType (lpFile) != IMAGE_NT_SIGNATURE ||
  1010.     (psh = (PIMAGE_SECTION_HEADER)SECHDROFFSET (lpFile)) == NULL)
  1011.     return 0;
  1012.  
  1013.     /* count the number of chars used in the section names */
  1014.     for (i=0; i<nSections; i++)
  1015.     nCnt += strlen (psh[i].Name) + 1;
  1016.  
  1017.     /* allocate space for all section names from heap */
  1018.     ps = *pszSections = (char *)calloc (nCnt, 1);
  1019.  
  1020.  
  1021.     for (i=0; i<nSections; i++)
  1022.     {
  1023.         strcpy (ps, psh[i].Name);
  1024.         ps += strlen (psh[i].Name) + 1;
  1025.     }
  1026.  
  1027.     return nCnt;
  1028. }
  1029.  
  1030. /* function gets the function header for a section identified by name */
  1031. BOOL    WINAPI GetSectionHdrByName (
  1032.     LPVOID                   lpFile,
  1033.     IMAGE_SECTION_HEADER     *sh,
  1034.     char                     *szSection)
  1035. {
  1036.     PIMAGE_SECTION_HEADER    psh;
  1037.     int                      nSections = NumOfSections (lpFile);
  1038.     int                      i;
  1039.  
  1040.  
  1041.     if ((psh = (PIMAGE_SECTION_HEADER)SECHDROFFSET (lpFile)) != NULL)
  1042.     {
  1043.     /* find the section by name */
  1044.         for (i=0; i<nSections; i++)
  1045.         {
  1046.         if (!strcmp (psh->Name, szSection))
  1047.             {
  1048.             /* copy data to header */
  1049.             memcpy ((LPVOID)sh, (LPVOID)psh, sizeof (IMAGE_SECTION_HEADER));
  1050.             return TRUE;
  1051.             }
  1052.         else psh++;
  1053.         }
  1054.     }
  1055.     return FALSE;
  1056. }
  1057.  
  1058. //
  1059. // This function is modified by sang cho
  1060. //
  1061. //
  1062. /* get import modules names separated by null terminators, return module count */
  1063. int  WINAPI GetImportModuleNames (
  1064.     LPVOID    lpFile,
  1065.     char      **pszModules)
  1066. {
  1067.     PIMAGE_IMPORT_MODULE_DIRECTORY  pid = (PIMAGE_IMPORT_MODULE_DIRECTORY)
  1068.     ImageDirectoryOffset (lpFile, IMAGE_DIRECTORY_ENTRY_IMPORT);
  1069.     //
  1070.     // sometimes there may be no section for idata or edata
  1071.     // instead rdata or data section may contain these sections ..
  1072.     // or even module names or function names are in different section.
  1073.     // so that's why we need to get actual address of RVAs each time.
  1074.     //         ...................sang cho..................
  1075.     //
  1076.     // PIMAGE_SECTION_HEADER     psh = (PIMAGE_SECTION_HEADER)
  1077.     // ImageDirectorySection (lpFile, IMAGE_DIRECTORY_ENTRY_IMPORT);
  1078.     // BYTE                  *pData = (BYTE *)pid;
  1079.     // DWORD            *pdw = (DWORD *)pid;
  1080.     int               nCnt = 0, nSize = 0, i;
  1081.     char             *pModule[1024];  /* hardcoded maximum number of modules?? */
  1082.     int               pidTab[1024];
  1083.     char                 *psz;
  1084.  
  1085.     if (pid == NULL) return 0;
  1086.  
  1087.     // pData = (BYTE *)((int)lpFile + psh->PointerToRawData - psh->VirtualAddress);
  1088.  
  1089.     /* extract all import modules */
  1090.     while (pid->dwRVAModuleName)
  1091.     {
  1092.     /* allocate temporary buffer for absolute string offsets */
  1093.         //pModule[nCnt] = (char *)(pData + pid->dwRVAModuleName);
  1094.         pModule[nCnt] = (char *)GetActualAddress (lpFile, pid->dwRVAModuleName);
  1095.         pidTab[nCnt] = (int)pid;
  1096.         nSize += strlen (pModule[nCnt]) + 1 + 4;
  1097.  
  1098.     /* increment to the next import directory entry */
  1099.         pid++;
  1100.         nCnt++;
  1101.     }
  1102.  
  1103.     /* copy all strings to one chunk of memory */
  1104.     *pszModules = (char *)calloc(nSize, 1);
  1105.     piNameBuffSize = nSize;
  1106.     psz = *pszModules;
  1107.     for (i=0; i<nCnt; i++)
  1108.     {
  1109.         *(int *)psz = pidTab[i]; 
  1110.         strcpy (psz+4, pModule[i]);
  1111.         psz += strlen (psz+4) + 1 + 4;
  1112.     }
  1113.     return nCnt;
  1114. }
  1115.  
  1116. //
  1117. // This function is rewritten by sang cho
  1118. //
  1119. //
  1120. /* get import module function names separated by null terminators, return function count */
  1121. int  WINAPI GetImportFunctionNamesByModule (
  1122.     LPVOID      lpFile,
  1123.     char       *pszModule,
  1124.     char      **pszFunctions)
  1125. {
  1126.     PIMAGE_IMPORT_MODULE_DIRECTORY  pid;
  1127.     
  1128.     //
  1129.     // sometimes there may be no section for idata or edata
  1130.     // instead rdata or data section may contain these sections ..
  1131.     // or even module names or function names are in different section.
  1132.     // so that's why we need to get actual address each time.
  1133.     //         ...................sang cho..................
  1134.     //
  1135.     
  1136.     int              nCnt = 0, nSize = 0;
  1137.     int              nnid = 0;
  1138.     int              mnlength, i;
  1139.     DWORD            dwFunctionName;
  1140.     DWORD            dwFunctionAddress;
  1141.     char             name[128];
  1142.     char             buff[256];             // enough for any string ??
  1143.     char            *psz;
  1144.     DWORD           *pdw;
  1145.     int              r,rr;
  1146.     _key_            k;
  1147.  
  1148.  
  1149.     pid = (PIMAGE_IMPORT_MODULE_DIRECTORY)(*(DWORD *)pszModule);
  1150.  
  1151.     /* exit if the module is not found */
  1152.     if (!pid->dwRVAModuleName)
  1153.     return 0;
  1154.  
  1155.     // I am doing this to get rid of .dll from module name
  1156.     strcpy (name, pszModule+4);
  1157.     mnlength = strlen (pszModule+4);
  1158.     for (i=0; i<mnlength; i++) if (name[i] == '.') break;
  1159.     name[i] = 0;
  1160.     mnlength = i;
  1161.  
  1162.     /* count number of function names and length of strings */
  1163.     dwFunctionName = pid->dwRVAFunctionNameList;
  1164.     
  1165.     // IMAGE_IMPORT_BY_NAME OR IMAGE_THUNK_DATA
  1166.     // modified by Sang Cho
  1167.     
  1168.     //fprintf(stderr,"pid = %08X dwFunctionName = %08X name = %s", 
  1169.     //(int)pid-(int)lpFile, dwFunctionName,name),getch();
  1170.  
  1171.     // modified by sang cho 1998.1.24
  1172.  
  1173.     if (dwFunctionName==0) dwFunctionName = pid->dwRVAFunctionAddressList;
  1174.  
  1175.     while (dwFunctionName &&
  1176.        *(pdw=(DWORD *)GetActualAddress (lpFile, dwFunctionName)) )      
  1177.     {
  1178.         if ((*pdw) & 0x80000000 )   nSize += mnlength + 11 + 1 + 6;
  1179.         else nSize += strlen ((char *)GetActualAddress (lpFile, *pdw+2)) + 1+6;
  1180.         dwFunctionName += 4;
  1181.         nCnt++;
  1182.     }
  1183.     
  1184.     /* allocate memory  for function names */
  1185.     *pszFunctions = (char *)calloc (nSize, 1);
  1186.     psz = *pszFunctions;
  1187.  
  1188.     //
  1189.     // I modified this part to store function address (4 bytes),
  1190.     //                               ord number (2 bytes),
  1191.     //                                                      and      name strings (which was there originally)
  1192.     // so that's why there are 6 more bytes...... +6,  or +4 and +2 etc.
  1193.     // these informations are used where they are needed.
  1194.     //                      ...........sang cho..................
  1195.     //
  1196.     /* copy function names to mempry pointer */
  1197.     dwFunctionName = pid->dwRVAFunctionNameList;
  1198.     // modified by sang cho 1998.1.24
  1199.     if (dwFunctionName==0) dwFunctionName = pid->dwRVAFunctionAddressList;
  1200.     dwFunctionAddress = pid->dwRVAFunctionAddressList;
  1201.     while (dwFunctionName                          &&
  1202.        *(pdw=(DWORD *)GetActualAddress (lpFile, dwFunctionName)) )
  1203.     {
  1204.         if ((*pdw) & 0x80000000)
  1205.         {
  1206.         r=*(int *)psz=(int)(*(DWORD *)GetActualAddress (lpFile, dwFunctionAddress));
  1207.             psz += 4;
  1208.         *(short *)psz=*(short *)pdw;
  1209.         psz += 2;        rr=(int)pdw;
  1210.         sprintf(buff, "%s:NoName%04d", name, nnid++);
  1211.         strcpy (psz, buff);     psz += strlen (buff) + 1;
  1212.             // this one is needed to link import function names to codes..
  1213.             k.class=992; k.c_ref= r; k.c_pos=-rr;
  1214.             MyBtreeInsertX(&k);
  1215.             k.class=0; k.c_ref=-rr; k.c_pos=(int)pszModule+4;
  1216.             MyBtreeInsertX(&k);
  1217.         }
  1218.         else
  1219.         {
  1220.         r=*(int *)psz=(int)(*(DWORD *)GetActualAddress (lpFile, dwFunctionAddress));
  1221.             psz += 4;
  1222.         *(short *)psz=(*(short *)GetActualAddress(lpFile, *pdw));
  1223.         psz += 2;        rr=(int)GetActualAddress(lpFile, *pdw + 2);
  1224.         strcpy (psz, (char *)rr);
  1225.         psz += strlen ((char *)GetActualAddress(lpFile, *pdw + 2)) + 1;
  1226.         
  1227.             // this one is needed to link import function names to codes..
  1228.             k.class=991; k.c_ref= r; k.c_pos=rr;
  1229.             MyBtreeInsertX(&k);
  1230.             k.class=0; k.c_ref=rr; k.c_pos=(int)pszModule+4;
  1231.             MyBtreeInsertX(&k);
  1232.         }
  1233.         dwFunctionName += 4;
  1234.         dwFunctionAddress += 4;
  1235.     }
  1236.  
  1237.     return nCnt;
  1238. }
  1239.  
  1240. //
  1241. // This function is written by sang cho
  1242. //                                                         October 6, 1997
  1243. //
  1244. /* get numerically expressed string length */
  1245. int WINAPI GetStringLength (
  1246.     char      *psz)
  1247. {
  1248.     if (!isdigit (*psz)) return 0; 
  1249.     if (isdigit (*(psz+1))) return (*psz - '0')*10 + *(psz+1) - '0';
  1250.     else return *psz - '0';
  1251. }
  1252.  
  1253. //
  1254. // This function is written by sang cho
  1255. //                                                         October 12, 1997
  1256. //
  1257.  
  1258. /* translate parameter part of condensed name */
  1259. int   WINAPI GetPreviousParamString ( 
  1260.     char       *xpin,                     // read-only source
  1261.     char       *xpout)                            // translated result
  1262. {
  1263.     int         n=0;
  1264.     char       *pin, *pout;           
  1265.  
  1266.     pin  = xpin;
  1267.     pout = xpout;
  1268.  
  1269.     pin--;
  1270.     if (*pin == ',') pin--;
  1271.     else { //printf ("\n **error PreviousParamString1 char = %02X %s", *pin, pin); 
  1272.           return (0); }
  1273.  
  1274.     while (*pin)
  1275.     {
  1276.          if (*pin == '>') n++;
  1277.         else if (*pin == '<') n--;
  1278.         else if (*pin == ')') n++;
  1279.         
  1280.         if (n > 0) 
  1281.         {
  1282.             if (*pin == '(') n--;
  1283.         }
  1284.         else if (strchr (",(", *pin)) break;
  1285.         pin--;
  1286.     }
  1287.  
  1288.     //printf("\n ----- %s", pin);
  1289.     if (strchr (",(", *pin)) {pin++;} // printf("\n %s", pin); }
  1290.     else { printf ("\n **error PreviousParamString2"); return (0); }
  1291.  
  1292.     n = xpin - pin - 1;
  1293.     strncpy (pout, pin, n);
  1294.     *(pout + n) = 0;
  1295.     return 1;
  1296. }
  1297.  
  1298. //
  1299. // This function is written by sang cho
  1300. //                                                         October 10, 1997
  1301. //
  1302.  
  1303. /* translate parameter part of condensed name */
  1304. int   WINAPI TranslateParameters ( 
  1305.     char      **ppin,                     // read-only source
  1306.     char      **ppout,                            // translated result
  1307.     char      **pps)                                          // parameter stack
  1308. {
  1309.     int         i, n;
  1310.     char        c;
  1311.     char        name[128];
  1312.     char        *pin, *pout, *ps;           
  1313.  
  1314.     //printf(" %c ", **in);
  1315.     pin  = *ppin;
  1316.     pout = *ppout;
  1317.     ps   = *pps;
  1318.     c = *pin;
  1319.     switch (c)
  1320.     {
  1321.         // types processing
  1322.         case 'b': strcpy (pout, "byte");       pout +=  4; pin++;  break;
  1323.         case 'c': strcpy (pout, "char");       pout +=  4; pin++;  break; 
  1324.         case 'd': strcpy (pout, "double");     pout +=  6; pin++;  break;
  1325.         case 'f': strcpy (pout, "float");      pout +=  5; pin++;  break;
  1326.         case 'g': strcpy (pout, "long double");pout += 11; pin++;  break;
  1327.         case 'i': strcpy (pout, "int");        pout +=  3; pin++;  break; 
  1328.         case 'l': strcpy (pout, "long");       pout +=  4; pin++;  break;
  1329.         case 's': strcpy (pout, "short");      pout +=  5; pin++;  break; 
  1330.         case 'v': strcpy (pout, "void");       pout +=  4; pin++;  break;
  1331.         // postfix processing
  1332.         case 'M':
  1333.         case 'p': 
  1334.             if (*(pin+1) == 'p') { *ps++ = 'p'; pin += 2; }
  1335.             else { *ps++ = '*'; pin++; }
  1336.             *ppin = pin; *ppout = pout; *pps = ps;
  1337.             return 1;
  1338.         case 'q':
  1339.             *pout++ = '('; pin++;
  1340.             *ps++ = 'q';
  1341.             *ppin = pin; *ppout = pout; *pps = ps;
  1342.             return 1;
  1343.         case 'r':
  1344.             if (*(pin+1) == 'p') { *ps++ = 'r'; pin += 2; }
  1345.             else { *ps++ = '&'; pin++; }
  1346.             *ppin = pin; *ppout = pout; *pps = ps;
  1347.             return 1;
  1348.         // repeat processing
  1349.         case 't':
  1350.             if (isdigit(*(pin+1)))
  1351.             { 
  1352.                 n = *(pin+1) - '0'; pin++; pin++;
  1353.                 if (GetPreviousParamString (pout, name))
  1354.                 {
  1355.                     strcpy (pout, name); pout += strlen (name);
  1356.                     for (i=1; i<n; i++)
  1357.                     {
  1358.                         *pout++ = ',';
  1359.                         strcpy (pout, name); pout += strlen (name);
  1360.                     }
  1361.                 }
  1362.                 else return 0;
  1363.             }
  1364.             else pin++;
  1365.             break;
  1366.         // prefix processing
  1367.         case 'u':
  1368.             strcpy (pout, "u");        pout +=  1; pin++;  
  1369.             *ppin = pin; *ppout = pout; *pps = ps;
  1370.             return 1;
  1371.         case 'x':
  1372.             strcpy (pout, "const ");   pout +=  6; pin++;  
  1373.             *ppin = pin; *ppout = pout; *pps = ps;
  1374.             return 1;
  1375.         case 'z':
  1376.             strcpy (pout, "static ");  pout +=  7; pin++;  
  1377.             *ppin = pin; *ppout = pout; *pps = ps;
  1378.             return 1;
  1379.         default:  strcpy (pout, "!1!");pout +=  3; *pout++=*pin++;
  1380.             *ppin = pin; *ppout = pout; *pps = ps;
  1381.             return 1;
  1382.     }
  1383.     // need to process postfix finally
  1384.     c = *(ps-1);
  1385.     if (strchr ("tqx", c))
  1386.     { if (*(pin)&& !strchr( "@$%", *(pin))) *pout++ = ','; 
  1387.       *ppin = pin; *ppout = pout; *pps = ps; return 1; }
  1388.     switch (c)
  1389.     {
  1390.         case 'r': strcpy (pout, "*&");  pout += 2;  ps--; break;
  1391.         case 'p': strcpy (pout, "**");  pout += 2;  ps--; break;
  1392.         case '&': strcpy (pout, "&");   pout += 1;  ps--; break;
  1393.         case '*': strcpy (pout, "*");   pout += 1;  ps--; break;
  1394.         default:  strcpy (pout, "!2!"); pout += 3;  ps--; break;
  1395.     }
  1396.     if (*(pin) && !strchr( "@$%", *(pin))) *pout++ = ',';
  1397.     *ppin = pin; *ppout = pout; *pps = ps;
  1398.     return 1;
  1399. }
  1400.  
  1401. //
  1402. // This function is written by sang cho
  1403. //                                                         October 11, 1997
  1404. //
  1405.  
  1406. /* translate parameter part of condensed name */
  1407. BOOL   WINAPI StringExpands ( 
  1408.     char      **ppin,                     // read-only source
  1409.     char      **ppout,                            // translated result
  1410.     char      **pps,                                          // parameter stack
  1411.     Str_P      *pcstr)                    // currently stored string
  1412. {
  1413.     // int         n;
  1414.     // char        c;
  1415.     char        *pin, *pout, *ps;  
  1416.     Str_P       c_str;
  1417.     BOOL        stringMode = TRUE;
  1418.  
  1419.     pin  = *ppin;
  1420.     pout = *ppout;
  1421.     ps   = *pps;
  1422.     c_str = *pcstr;
  1423.  
  1424.          if (strncmp (pin, "bctr", 4) == 0)
  1425.     {  strncpy (pout, c_str.pos, c_str.length); 
  1426.        pout += c_str.length; pin += 4; }
  1427.     else if (strncmp (pin, "bdtr", 4) == 0)
  1428.     {  *pout++ = '~'; 
  1429.        strncpy (pout, c_str.pos, c_str.length);     
  1430.        pout += c_str.length; pin += 4; }
  1431.     else if (*pin == 'o')    
  1432.     {  strcpy(pout, "const ");             pout +=  6;  pin++;
  1433.        stringMode = FALSE;
  1434.     }
  1435.     else if (*pin == 'q')    
  1436.     {  *pout++ = '(';  pin++;
  1437.        *ps++ = 'q';    stringMode = FALSE;
  1438.     }
  1439.     else if (*pin == 't')
  1440.     {
  1441.        //if (*(ps-1) == 't') { *pout++ = ','; pin++; }       // this also got me...
  1442.        //else                                                                                          october 12  .. sang
  1443.        {  *pout++ = '<';  pin++;
  1444.           *ps++ = 't';        
  1445.        }
  1446.        stringMode = FALSE;
  1447.     }
  1448.     else if (strncmp (pin, "xq", 2) == 0)
  1449.     {  *pout++ = '('; pin += 2;
  1450.        *ps++ = 'x'; *ps++ = 'q';
  1451.        stringMode = FALSE;
  1452.     }
  1453.     else if (strncmp (pin, "bcall", 5) == 0)
  1454.     {  strcpy (pout, "operator ()");       pout += 11; pin += 5; }
  1455.     else if (strncmp (pin, "bsubs", 5) == 0)
  1456.     {  strcpy (pout, "operator []");       pout += 11; pin += 5; }
  1457.     else if (strncmp (pin, "bnwa", 4) == 0) 
  1458.     {  strcpy (pout, "operator new[]");    pout += 14; pin += 4; }
  1459.     else if (strncmp (pin, "bdla", 4) == 0) 
  1460.     {  strcpy (pout, "operator delete[]"); pout += 17; pin += 4; }
  1461.     else if (strncmp (pin, "bnew", 4) == 0)
  1462.     {  strcpy (pout, "operator new");      pout += 12; pin += 4; }
  1463.     else if (strncmp (pin, "bdele", 5) == 0)
  1464.     {  strcpy (pout, "operator delete");   pout += 15; pin += 5; }
  1465.     else if (strncmp (pin, "blsh", 4) == 0)
  1466.     {  strcpy (pout, "operator <<");       pout += 11; pin += 4; }
  1467.     else if (strncmp (pin, "brsh", 4) == 0)
  1468.     {  strcpy (pout, "operator >>");       pout += 11; pin += 4; }
  1469.     else if (strncmp (pin, "binc", 4) == 0)
  1470.     {  strcpy (pout, "operator ++");       pout += 11; pin += 4; }
  1471.     else if (strncmp (pin, "bdec", 4) == 0)
  1472.     {  strcpy (pout, "operator --");       pout += 11; pin += 4; }
  1473.     else if (strncmp (pin, "badd", 4) == 0)
  1474.     {  strcpy (pout, "operator +");        pout += 10; pin += 4; }
  1475.     else if (strncmp (pin, "brplu", 5) == 0)
  1476.     {  strcpy (pout, "operator +=");       pout += 11; pin += 5; }
  1477.     else if (strncmp (pin, "bdiv", 4) == 0)
  1478.     {  strcpy (pout, "operator /");        pout += 10; pin += 4; }
  1479.     else if (strncmp (pin, "brdiv", 5) == 0)
  1480.     {  strcpy (pout, "operator /=");       pout += 11; pin += 5; }
  1481.     else if (strncmp (pin, "bmul", 4) == 0)
  1482.     {  strcpy (pout, "operator *");        pout += 10; pin += 4; }
  1483.     else if (strncmp (pin, "brmul", 5) == 0)
  1484.     {  strcpy (pout, "operator *=");       pout += 11; pin += 5; }
  1485.     else if (strncmp (pin, "basg", 4) == 0)
  1486.     {  strcpy (pout, "operator =");        pout += 10; pin += 4; }
  1487.     else if (strncmp (pin, "beql", 4) == 0)
  1488.     {  strcpy (pout, "operator ==");       pout += 11; pin += 4; }
  1489.     else if (strncmp (pin, "bneq", 4) == 0)
  1490.     {  strcpy (pout, "operator !=");       pout += 11; pin += 4; }
  1491.     else if (strncmp (pin, "bor", 3) == 0)
  1492.     {  strcpy (pout, "operator |");        pout += 10; pin += 3; }
  1493.     else if (strncmp (pin, "bror", 4) == 0)
  1494.     {  strcpy (pout, "operator |=");       pout += 11; pin += 4; }
  1495.     else if (strncmp (pin, "bcmp", 4) == 0)
  1496.     {  strcpy (pout, "operator ~");        pout += 10; pin += 4; }
  1497.     else if (strncmp (pin, "bnot", 4) == 0)
  1498.     {  strcpy (pout, "operator !");        pout += 10; pin += 4; }
  1499.     else if (strncmp (pin, "band", 4) == 0)
  1500.     {  strcpy (pout, "operator &");        pout += 10; pin += 4; }
  1501.     else if (strncmp (pin, "brand", 5) == 0)
  1502.     {  strcpy (pout, "operator &=");       pout += 11; pin += 5; }
  1503.     else if (strncmp (pin, "bxor", 4) == 0)
  1504.     {  strcpy (pout, "operator ^");        pout += 10; pin += 4; }
  1505.     else if (strncmp (pin, "brxor", 5) == 0)
  1506.     {  strcpy (pout, "operator ^=");       pout += 11; pin += 5; }
  1507.     else     
  1508.     {  
  1509.        strcpy (pout, "!$$$!"); pout += 5; 
  1510.     }
  1511.     *ppin = pin; *ppout = pout; *pps = ps;
  1512.     return stringMode;
  1513. }   // end of '$' processing
  1514.  
  1515. //----------------------------------------------------------------------
  1516. // structure to store string tokens
  1517. //----------------------------------------------------------------------
  1518. //typedef struct _Str_P {
  1519. //    char    flag;               // string_flag '@' or '%' or '#'
  1520. //    char    *pos;               // starting postion of string
  1521. //    int     length;     // length of string
  1522. //    BOOL    wasString;    // if it were stringMode or not
  1523. //} Str_P;
  1524. //----------------------------------------------------------------------
  1525. //
  1526. // I think I knocked it down finally. But who knows? 
  1527. //                            october 12, 1997 ... sang
  1528. //
  1529. // well I have to rewrite whole part of TranslateFunctionName..
  1530. // this time I am a little bit more experienced than 5 days ago.
  1531. // or am i??? anyway i use stacks instead of recurcive calls
  1532. // and i hope this will take care of every symptoms i have experienced..
  1533. //                                                        october 10, 1997 .... sang
  1534. // It took a lot of time for me to figure out what is all about....
  1535. // but still some prefixes like z (static) 
  1536. //     -- or some types like b (byte) ,g (long double) ,s (short) --
  1537. //         -- or postfix  like M ( * )
  1538. //     -- or $or ( & ) which is pretty wierd.         .. added.. october 12
  1539. //     -- also $t business is quite tricky too. (templates) 
  1540. //             there may be a lot of things undiscovered yet....
  1541. // I am not so sure my interpretation is correct or not
  1542. // If I am wrong please let me know.
  1543. //                             october 8, 1997 .... sang
  1544. //
  1545.  
  1546. //
  1547. // This function is written by sang cho
  1548. //                                                         October 5, 1997
  1549. //
  1550.  
  1551. /* translate condesed import function name */
  1552. LPVOID WINAPI TranslateFunctionName (
  1553.     char      *psz)
  1554. {
  1555.     
  1556.     
  1557.     int                                 i, n;
  1558.     char                    c, cc;
  1559.  
  1560.     static char             buff[512];      // result of translation
  1561.  
  1562.     int                     is=0;
  1563.     char                    pStack[32]; // parameter processing stack
  1564.     Str_P                   sStack[32]; // String processing stack
  1565.     Str_P                   tok;        // String token
  1566.     Str_P                   c_str;      // current string 
  1567.  
  1568.     int                     iend=0;
  1569.     char                    *endTab[8];  // end of string position check
  1570.  
  1571.     char                   *ps;
  1572.     char                           *pin, *pout;
  1573.     BOOL                    stringMode=TRUE;
  1574.  
  1575.     if (*psz != '@') return psz;
  1576.     pin  = psz;
  1577.     pout = buff;
  1578.     ps   = pStack;
  1579.     
  1580.     //................................................................
  1581.     // serious users may need to run the following code.
  1582.     // so I may need to include some flag options...
  1583.     // If you want to know about how translation is done,
  1584.     // you can just revive following line and you can see it.
  1585.     //                                                 october 6, 1997 ... sang cho
  1586.     //printf ("\n................................... %s", psz); // for debugging...
  1587.     
  1588.     //pa = pb = pout;
  1589.     pin++;                                             
  1590.     tok.flag = 'A'; tok.pos = pout; tok.length = 0;     tok.wasString = stringMode;
  1591.     sStack[is++] = tok;       // initialize sStack with dummy marker
  1592.     
  1593.     while (*pin)
  1594.     {
  1595.         while (*pin)
  1596.         {
  1597.         c = *pin;
  1598.  
  1599.             //---------------------------------------------
  1600.             // check for the end of number specified string
  1601.             //---------------------------------------------
  1602.             
  1603.             if (iend>0)
  1604.             {
  1605.                 for (i=0;i<iend;i++) if (pin == endTab[i]) break;
  1606.                 if (i<iend) 
  1607.                 { 
  1608.                     // move the end of endTab to ith position
  1609.                     endTab[i] = endTab[iend-1]; iend--;
  1610.  
  1611.                     // get top of the string stack
  1612.                     tok = sStack[is-1];
  1613.  
  1614.                     // I am expecting '#' token from stack
  1615.                     if (tok.flag != '#') 
  1616.  
  1617.                     { printf("\n**some serious error1** %c is = %d char = %c", 
  1618.                       tok.flag, is, *pin); 
  1619.                       exit(0);}
  1620.  
  1621.                     // pop '#' token  I am happy now.
  1622.                     else
  1623.                     {       //if (c)
  1624.                         //printf("\n pop # token ... current char = %c", c);
  1625.                         //else printf("\n pop percent token..next char = NULL");
  1626.                         is--;       
  1627.                     }
  1628.  
  1629.                     stringMode = tok.wasString;
  1630.  
  1631.                     if (!stringMode) 
  1632.                     {
  1633.                         // need to process postfix finally
  1634.                         cc = *(ps-1);
  1635.                         if (strchr ("qtx", cc))
  1636.                         {    if (!strchr ("@$%", c)) *pout++ = ',';
  1637.                         }
  1638.                         else
  1639.                         {
  1640.                 switch (cc)
  1641.                 {
  1642.         case 'r': strcpy (pout, "*&");  pout += 2;  ps--; break;
  1643.         case 'p': strcpy (pout, "**");  pout += 2;  ps--; break;
  1644.         case '&': strcpy (pout, "&");   pout += 1;  ps--; break;
  1645.         case '*': strcpy (pout, "*");   pout += 1;  ps--; break;
  1646.         default:  strcpy (pout, "!3!"); pout += 3;  ps--; break;
  1647.                 }
  1648.                             if (!strchr ("@$%", c)) *pout++ = ',';
  1649.                         }
  1650.                     }
  1651.                     // string mode restored...
  1652.                     else;
  1653.                 }
  1654.                 else ; // do nothing.. 
  1655.             }
  1656.  
  1657.             //------------------------------------------------
  1658.             // special control symbol processing:
  1659.             //------------------------------------------------
  1660.  
  1661.             if (strchr ("@$%", c))  break;
  1662.  
  1663.             //---------------------------------------------------------------
  1664.             // string part processing : no '$' met yet 
  1665.             //                       or inside of '%' block
  1666.             //                       or inside of '#' block (numbered string)
  1667.             //---------------------------------------------------------------
  1668.  
  1669.             else if (stringMode)     *pout++ = *pin++;
  1670.             //else if (is > 1)         *pout++ = *pin++;
  1671.  
  1672.             //------------------------------------------------ 
  1673.             // parameter part processing: '$' met
  1674.             //------------------------------------------------
  1675.  
  1676.             else                 // parameter processing
  1677.             {
  1678.                 if (!isdigit (c)) 
  1679.                 {
  1680.                     if(!TranslateParameters (&pin, &pout, &ps)) return psz;
  1681.                 }
  1682.                 else         // number specified string processing
  1683.                 {
  1684.                     n = GetStringLength (pin);
  1685.                     if (n<10) pin++; else pin += 2;
  1686.  
  1687.                     // push '#' token
  1688.                     //if (*pin)
  1689.                     //printf("\n push # token .. char = %c", *pin);
  1690.                     //else printf("\n push percent token..next char = NULL");
  1691.                     tok.flag = '#'; tok.pos = pout; 
  1692.                     tok.length = 0; tok.wasString = stringMode;
  1693.                     sStack[is++] = tok;
  1694.  
  1695.                     // mark end of input string
  1696.                     endTab[iend++] = pin + n; 
  1697.                     stringMode = TRUE;
  1698.                 }
  1699.             }       
  1700.         }   // end of inner while loop
  1701.         //
  1702.         // beginning of new string or end of string ( quotation mark )
  1703.         //
  1704.         if (c == '%')
  1705.         {
  1706.             pin++;               // anyway we have to proceed...
  1707.         tok = sStack[is-1];  // get top of the sStack
  1708.             if (tok.flag == '%') 
  1709.             {                                       
  1710.                 // pop '%' token and set c_str 
  1711.                 //if (*pin)
  1712.                 //printf("\n pop percent token..next char = %c", *pin);
  1713.                 //else printf("\n pop percent token..next char = NULL");
  1714.                 is--;
  1715.                 c_str = tok; c_str.length = pout - c_str.pos; 
  1716.                 if (*(ps-1) == 't') 
  1717.                 { 
  1718.                     *pout++ = '>'; ps--;  
  1719.                     stringMode = tok.wasString;
  1720.                 }
  1721.                 else { printf("\n**some string error3** stack = %c", *(ps-1)); 
  1722.                 exit(0); }
  1723.             }
  1724.             else if (tok.flag == 'A' || tok.flag == '#')
  1725.             {
  1726.                 // push '%' token
  1727.                 //if (*pin)
  1728.                 //printf("\n push percent token..next char = %c", *pin);
  1729.                 //else printf("\n push percent token..next char = NULL");
  1730.                 tok.flag = '%'; tok.pos = pout; tok.length = 0;
  1731.                 tok.wasString = stringMode;
  1732.                 sStack[is++] = tok;      
  1733.             }
  1734.             else  { printf("\n**some string error5**"); exit(0); }
  1735.         }
  1736.         //
  1737.         // sometimes we need string to use as constructor name or destructor name
  1738.         //
  1739.         else if (c == '@') // get string from previous marker  upto here. 
  1740.         { 
  1741.             pin++;
  1742.             tok = sStack[is-1];
  1743.             c_str.flag = 'S'; 
  1744.             c_str.pos = tok.pos;
  1745.             c_str.length = pout - tok.pos;
  1746.             c_str.wasString = stringMode;
  1747.             *pout++ = ':'; *pout++ = ':';
  1748.         }
  1749.         //
  1750.         // we need to take care of parameter control sequence
  1751.         //
  1752.         else if (c == '$') // need to precess template or parameter part
  1753.         {
  1754.             pin++;
  1755.             if (stringMode) 
  1756.                 stringMode = StringExpands (&pin, &pout, &ps, &c_str);
  1757.             else
  1758.             {       // template parameter mode I guess  "$t"
  1759.                 if (is>1) 
  1760.                 {  
  1761.                     if (*pin == 't') pin++;
  1762.                     else { printf("\nMYGOODNESS1 %c", *pin); exit(0);}
  1763.                     //ps--;
  1764.                     //if (*ps == 't') *pout++ = '>';
  1765.                     //else { printf("\nMYGOODNESS2"); exit(0);}
  1766.                     *pout++ = ','; //pin++; ..this almost blowed me....
  1767.                 }
  1768.                 // real parameter mode I guess
  1769.                 // unexpected case is found ... humm what can I do...
  1770.                 else
  1771.                 {  
  1772.                     // this is newly found twist.. it really hurts.
  1773.                     if (ps <= pStack)
  1774.                     {  if (*pin == 'q') { *ps++ = 'q'; *pout++ = '('; pin++; }
  1775.                        else {printf("\n** I GIVEUP ***"); exit(0);}
  1776.                        continue;
  1777.                     }
  1778.                     ps--;
  1779.                     while (*ps != 'q') 
  1780.                     {       if (*ps == '*') *pout++ = '*';
  1781.                        else if (*ps == '&') *pout++ = '&';
  1782.                        else if (*ps == 'p'){*pout++ = '*'; *pout++ = '*'; }
  1783.                        else if (*ps == 'r'){*pout++ = '*'; *pout++ = '&'; }
  1784.                        else {printf("\n*** SOMETHING IS WRONG1*** char= %c",*pin); 
  1785.                        exit(0);}
  1786.                        ps--;
  1787.                     }
  1788.                 *pout++ = ')'; 
  1789.                     ps--;
  1790.                     while (*ps != 'q') 
  1791.                     {       if (*ps == '*') *pout++ = '*';
  1792.                        else if (*ps == '&') *pout++ = '&';
  1793.                        else if (*ps == 'p'){*pout++ = '*'; *pout++ = '*'; }
  1794.                        else if (*ps == 'r'){*pout++ = '*'; *pout++ = '&'; }
  1795.                        else {printf("\n*** SOMETHING IS WRONG2***"); exit(0);}
  1796.                        ps--;
  1797.                     }
  1798.                 ps++; *pout++ = ',';
  1799.                 }
  1800.             }
  1801.         }   // end of '$' processing
  1802.     }       // end of outer while loop
  1803.     //
  1804.     // need to process remaining parameter stack
  1805.     //
  1806.     while (ps>pStack)
  1807.     {
  1808.         ps--;
  1809.         switch(*ps)
  1810.         {
  1811.             case 't': *pout++ = '>';                      break;
  1812.         case 'q': *pout++ = ')';                      break;
  1813.         case 'x': strcpy (pout, " const"); pout += 6; break;
  1814.         case 'r': strcpy (pout, "*&");     pout += 2; break;
  1815.             case 'p': strcpy (pout, "**");     pout += 2; break;
  1816.             case '&': *pout++ = '&';                      break;
  1817.             case '*': *pout++ = '*';                      break;
  1818.             default:  strcpy (pout, "!4!");    pout += 3; *pout++ = *ps;
  1819.         }
  1820.     }
  1821.     *pout = 0;
  1822.     return buff;
  1823. }
  1824.  
  1825. //
  1826. // This function is written by sang cho
  1827. //
  1828. //
  1829.  
  1830. /* get exported function names separated by null terminators, return count of functions */
  1831. int  WINAPI GetExportFunctionNames (
  1832.     LPVOID    lpFile,
  1833.     char      **pszFunctions)
  1834. {
  1835.     //PIMAGE_SECTION_HEADER      psh;
  1836.     PIMAGE_EXPORT_DIRECTORY    ped;
  1837.     //DWORD                      dwBase;
  1838.     DWORD                      imageBase;                 //===========================
  1839.     char                          *pfns[8192]={NULL,}; // maximum number of functions
  1840.                                                     //=============================  
  1841.     char                       buff[256];        // enough for any string ??
  1842.     char                      *psz;            //===============================
  1843.     DWORD                     *pdwAddress;
  1844.     DWORD                     *pdw1;
  1845.     DWORD                     *pdwNames;
  1846.     WORD                      *pwOrd;
  1847.     int                                i, nCnt=0, ntmp=0;
  1848.     int                        enid=0, ordBase=1; // usally ordBase is 1....
  1849.     int                        enames=0;
  1850.  
  1851.     /* get section header and pointer to data directory for .edata section */
  1852.     ped = (PIMAGE_EXPORT_DIRECTORY)
  1853.     ImageDirectoryOffset(lpFile, IMAGE_DIRECTORY_ENTRY_EXPORT);
  1854.  
  1855.     if (ped == NULL) return 0;
  1856.  
  1857.     //
  1858.     // sometimes there may be no section for idata or edata
  1859.     // instead rdata or data section may contain these sections ..
  1860.     // or even module names or function names are in different section.
  1861.     // so that's why we need to get actual address each time.
  1862.     //         ...................sang cho..................
  1863.     //
  1864.     //psh = (PIMAGE_SECTION_HEADER)
  1865.     //ImageDirectorySection(lpFile, IMAGE_DIRECTORY_ENTRY_EXPORT);
  1866.  
  1867.     //if (psh == NULL) return 0;
  1868.  
  1869.     //dwBase = (DWORD)((int)lpFile + psh->PointerToRawData - psh->VirtualAddress);
  1870.  
  1871.  
  1872.     /* determine the offset of the export function names */
  1873.  
  1874.     pdwAddress = (DWORD *)GetActualAddress (lpFile, (DWORD)ped->AddressOfFunctions);
  1875.  
  1876.     imageBase = (DWORD)GetImageBase (lpFile);
  1877.     
  1878.     ordBase = ped->Base;
  1879.  
  1880.     if (ped->NumberOfNames > 0)
  1881.     {
  1882.     pdwNames = (DWORD *)
  1883.                GetActualAddress (lpFile, (DWORD)ped->AddressOfNames);
  1884.         pwOrd = (WORD *)
  1885.             GetActualAddress (lpFile, (DWORD)ped->AddressOfNameOrdinals);
  1886.         pdw1 = pdwAddress;
  1887.  
  1888.     /* figure out how much memory to allocate for all strings */
  1889.         for (i=0; i < (int)ped->NumberOfNames; i++)
  1890.         {
  1891.             nCnt += strlen ((char *)
  1892.                     GetActualAddress (lpFile, *(DWORD *)pdwNames)) + 1 + 6;
  1893.             pdwNames++;
  1894.         }
  1895.         // get the number of unnamed functions
  1896.         for (i=0; i < (int)ped->NumberOfFunctions; i++)
  1897.             if (*pdw1++) ntmp++;
  1898.         // add memory required to show unnamed functions.
  1899.         if (ntmp > (int)ped->NumberOfNames)
  1900.             nCnt += 18*(ntmp - (int)ped->NumberOfNames);
  1901.  
  1902.     /* allocate memory  for function names */
  1903.         
  1904.         *pszFunctions = (char *)calloc (nCnt, 1);
  1905.         peNameBuffSize=nCnt;
  1906.         pdwNames = (DWORD *)GetActualAddress (lpFile, (DWORD)ped->AddressOfNames);
  1907.  
  1908.     /* copy string pointer to buffer */
  1909.         
  1910.         for (i=0; i < (int)ped->NumberOfNames; i++)
  1911.         {
  1912.             pfns[(int)(*pwOrd)+ordBase] = 
  1913.             (char *)GetActualAddress (lpFile, *(DWORD *)pdwNames);
  1914.             pdwNames++;
  1915.             pwOrd++;
  1916.         }
  1917.  
  1918.         psz = *pszFunctions;
  1919.     }       
  1920.     if (nCnt==0)
  1921.     {
  1922.     // get the number of unnamed functions
  1923.          nCnt += 18*(int)ped->NumberOfFunctions;
  1924.     /* allocate memory  for function names */
  1925.         *pszFunctions = (char *)calloc (nCnt, 1);
  1926.          peNameBuffSize=nCnt;
  1927.          psz=*pszFunctions;
  1928.     }
  1929.     for (i=ordBase; i < (int)ped->NumberOfFunctions + ordBase; i++)
  1930.     {
  1931.         if (*pdwAddress > 0)
  1932.         {
  1933.             *(DWORD *)psz = imageBase + *pdwAddress;
  1934.         psz += 4;
  1935.         *(WORD *)psz = (WORD)(i);
  1936.         psz += 2;
  1937.         if (pfns[i])
  1938.             {
  1939.                 strcpy (psz, pfns[i]);
  1940.                 psz += strlen(psz) + 1;
  1941.             }
  1942.         else
  1943.             {
  1944.                 sprintf (buff, "ExpFn%04d()", enid++);
  1945.                 strcpy (psz, buff);
  1946.                 psz += 12;
  1947.             }
  1948.             enames++;
  1949.         }
  1950.         pdwAddress++;
  1951.     }
  1952.     return enames;
  1953. }
  1954.  
  1955.  
  1956. /* determine the total number of resources in the section */
  1957. int     WINAPI GetNumberOfResources (
  1958.     LPVOID    lpFile)
  1959. {
  1960.     PIMAGE_RESOURCE_DIRECTORY          prdRoot, prdType;
  1961.     PIMAGE_RESOURCE_DIRECTORY_ENTRY    prde;
  1962.     int                                nCnt=0, i;
  1963.  
  1964.  
  1965.     /* get root directory of resource tree */
  1966.     if ((prdRoot = (PIMAGE_RESOURCE_DIRECTORY)ImageDirectoryOffset
  1967.             (lpFile, IMAGE_DIRECTORY_ENTRY_RESOURCE)) == NULL)
  1968.     return 0;
  1969.  
  1970.     /* set pointer to first resource type entry */
  1971.     prde = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)((DWORD)prdRoot + sizeof (IMAGE_RESOURCE_DIRECTORY));
  1972.  
  1973.     /* loop through all resource directory entry types */
  1974.     for (i=0; i<prdRoot->NumberOfIdEntries; i++)
  1975.     {
  1976.     /* locate directory or each resource type */
  1977.     prdType = (PIMAGE_RESOURCE_DIRECTORY)((int)prdRoot + (int)prde->OffsetToData);
  1978.  
  1979.     /* mask off most significant bit of the data offset */
  1980.     prdType = (PIMAGE_RESOURCE_DIRECTORY)((DWORD)prdType ^ 0x80000000);
  1981.  
  1982.     /* increment count of name'd and ID'd resources in directory */
  1983.     nCnt += prdType->NumberOfNamedEntries + prdType->NumberOfIdEntries;
  1984.  
  1985.     /* increment to next entry */
  1986.     prde++;
  1987.     }
  1988.  
  1989.     return nCnt;
  1990. }
  1991.  
  1992. //
  1993. // This function is rewritten by sang cho
  1994. //
  1995. //
  1996.  
  1997. /* name each type of resource in the section */
  1998. int     WINAPI GetListOfResourceTypes (
  1999.     LPVOID    lpFile,
  2000.     char      **pszResTypes)
  2001. {
  2002.     PIMAGE_RESOURCE_DIRECTORY          prdRoot;
  2003.     PIMAGE_RESOURCE_DIRECTORY_ENTRY    prde;
  2004.     PBYTE                              pMem;
  2005.     PWORD                              pw;
  2006.     char                    buff[32];
  2007.     int                                nCnt, i, j, n;
  2008.     DWORD                  prdeName;
  2009.  
  2010.  
  2011.     /* get root directory of resource tree */
  2012.     if ((prdRoot = (PIMAGE_RESOURCE_DIRECTORY)ImageDirectoryOffset
  2013.             (lpFile, IMAGE_DIRECTORY_ENTRY_RESOURCE)) == NULL)
  2014.     return 0;
  2015.  
  2016.     /* allocate enuff space  to cover all types */
  2017.     nCnt = prdRoot->NumberOfNamedEntries * 256 + prdRoot->NumberOfIdEntries * (32);
  2018.     *pszResTypes = (char *)calloc (nCnt, 1);
  2019.     if ((pMem = *pszResTypes) == NULL)
  2020.     return 0;
  2021.  
  2022.     /* set pointer to first resource type entry */
  2023.     prde = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)((DWORD)prdRoot + sizeof (IMAGE_RESOURCE_DIRECTORY));
  2024.  
  2025.     /* loop through all resource directory entry types */
  2026.     for (i=0; i<prdRoot->NumberOfNamedEntries; i++)
  2027.     {
  2028.         pw = (PWORD)((DWORD)prdRoot + (prde->Name ^ 0x80000000));
  2029.  
  2030.         if (!isalpha(*(PBYTE)pw))
  2031.         {
  2032.             n=(int)(*(PBYTE)pw++); 
  2033.             for(j=0;j<n;j++)*pMem++ = *(PBYTE)pw++; 
  2034.             *pMem=0; pMem++;    
  2035.         }
  2036.         else
  2037.         {
  2038.             while(*pw) *pMem++ = *(PBYTE)pw++; 
  2039.             *pMem=0; pMem++;
  2040.         }
  2041.         prde++;
  2042.     }
  2043.  
  2044.     /* loop through all resource directory entry types */
  2045.     for (i=0; i<prdRoot->NumberOfIdEntries; i++)
  2046.     {
  2047.         prdeName=prde->Name;
  2048.  
  2049.     //if (LoadString (hDll, prde->Name, pMem, MAXRESOURCENAME))
  2050.     //    pMem += strlen (pMem) + 1;
  2051.     //
  2052.     // modified by ...................................Sang Cho..
  2053.     // I can't user M/S provied funcitons here so I have to figure out
  2054.     // how to do above functions. But I can settle down with the following
  2055.     // code, which works pretty good for me.
  2056.     //
  2057.         if      (prdeName== 1){strcpy(pMem, "RT_CURSOR");       pMem+=10;}
  2058.         else if (prdeName== 2){strcpy(pMem, "RT_BITMAP");       pMem+=10;}
  2059.         else if (prdeName== 3){strcpy(pMem, "RT_ICON  ");       pMem+=10;}
  2060.         else if (prdeName== 4){strcpy(pMem, "RT_MENU  ");       pMem+=10;}
  2061.         else if (prdeName== 5){strcpy(pMem, "RT_DIALOG");       pMem+=10;}
  2062.         else if (prdeName== 6){strcpy(pMem, "RT_STRING");       pMem+=10;}
  2063.         else if (prdeName== 7){strcpy(pMem, "RT_FONTDIR");      pMem+=11;}
  2064.         else if (prdeName== 8){strcpy(pMem, "RT_FONT  ");       pMem+=10;}
  2065.         else if (prdeName== 9){strcpy(pMem, "RT_ACCELERATORS"); pMem+=16;}
  2066.         else if (prdeName==10){strcpy(pMem, "RT_RCDATA");       pMem+=10;}
  2067.         else if (prdeName==11){strcpy(pMem, "RT_MESSAGETABLE"); pMem+=16;}
  2068.         else if (prdeName==12){strcpy(pMem, "RT_GROUP_CURSOR"); pMem+=16;}
  2069.         else if (prdeName==14){strcpy(pMem, "RT_GROUP_ICON  "); pMem+=16;}
  2070.         else if (prdeName==16){strcpy(pMem, "RT_VERSION");      pMem+=11;}
  2071.         else if (prdeName==17){strcpy(pMem, "RT_DLGINCLUDE  "); pMem+=16;}
  2072.         else if (prdeName==19){strcpy(pMem, "RT_PLUGPLAY    "); pMem+=16;}
  2073.         else if (prdeName==20){strcpy(pMem, "RT_VXD   ");       pMem+=10;}
  2074.         else if (prdeName==21){strcpy(pMem, "RT_ANICURSOR   "); pMem+=16;}
  2075.         else if (prdeName==22){strcpy(pMem, "RT_ANIICON");      pMem+=11;}
  2076.         else if (prdeName== 0x2002)
  2077.                 {strcpy(pMem, "RT_NEWBITMAP");    pMem+=13;}
  2078.         else if (prdeName== 0x2004)
  2079.                 {strcpy(pMem, "RT_NEWMENU");      pMem+=11;}
  2080.         else if (prdeName== 0x2005)
  2081.                 {strcpy(pMem, "RT_NEWDIALOG");    pMem+=13;}
  2082.         else if (prdeName== 0x7fff)
  2083.                 {strcpy(pMem, "RT_ERROR ");       pMem+=10;}
  2084.         else    {sprintf(buff, "RT_UNKNOWN:%08X", (int)prdeName);
  2085.                  strcpy(pMem, buff);              pMem+=20;}
  2086.         prde++;
  2087.     }
  2088.  
  2089.     return prdRoot->NumberOfNamedEntries+prdRoot->NumberOfIdEntries;
  2090. }
  2091.  
  2092. //
  2093. // This function is written by sang cho
  2094. //                                                         October 12, 1997
  2095. //
  2096.  
  2097. /* copy menu information */
  2098. void  WINAPI StrangeMenuFill (
  2099.     char      **psz,              // results
  2100.     WORD      **pMenu,            // read-only
  2101.     int         size)
  2102. {
  2103.     WORD      *pwd;
  2104.     WORD      *ptr, *pmax;
  2105.  
  2106.     pwd = *pMenu;
  2107.     pmax = (WORD *)((DWORD)pwd + size);
  2108.     ptr = (WORD *)(*psz);
  2109.     
  2110.     while (pwd < pmax)
  2111.     {
  2112.         *ptr++=*pwd++;
  2113.     }
  2114.     *psz = (char *)ptr;
  2115.     *pMenu = pwd;
  2116. }
  2117.  
  2118. //
  2119. // This function is written by sang cho
  2120. //                                                         October 1, 1997
  2121. //
  2122.  
  2123. /* obtain menu information */
  2124. int     WINAPI MenuScan (
  2125.     int        *len,
  2126.     WORD      **pMenu)
  2127. {
  2128.     WORD      *pwd;
  2129.     WORD       flag, flag1;
  2130.     WORD       id, ispopup;
  2131.  
  2132.     pwd = *pMenu;
  2133.     
  2134.     flag = *pwd;  // so difficult to correctly code this so let's try this
  2135.     pwd++;
  2136.     (*len) += 2;                      // flag store
  2137.     if ((flag & 0x0010) == 0)
  2138.     {
  2139.         ispopup = flag;
  2140.         id = *pwd;
  2141.         pwd++;
  2142.         (*len) += 2;                  // id store
  2143.         
  2144.         while (*pwd) {(*len)++; pwd++;}
  2145.         (*len)++;                             // name and null character
  2146.         pwd++;                               // skip double null
  2147.         
  2148.         *pMenu = pwd;
  2149.         return (int)flag;
  2150.     }
  2151.     else 
  2152.     {
  2153.         ispopup = flag;
  2154.  
  2155.         while (*pwd) {(*len)++; pwd++;}
  2156.         (*len)++;                             // name and null character
  2157.         pwd++;                               // skip double null
  2158.                        // popup node: need to go on...
  2159.         while (1)
  2160.         {       
  2161.             *pMenu = pwd;
  2162.             flag1 = (WORD)MenuScan (len, pMenu);
  2163.             pwd = *pMenu;
  2164.             if (flag1 & 0x0080) break; 
  2165.         }
  2166.         *pMenu = pwd;
  2167.         return flag;
  2168.     }
  2169. }
  2170.  
  2171. //
  2172. // This function is written by sang cho
  2173. //                                                         October 2, 1997
  2174.  
  2175. /* copy menu information */
  2176. int     WINAPI MenuFill (
  2177.     char      **psz,
  2178.     WORD      **pMenu)
  2179. {
  2180.     char      *ptr;
  2181.     WORD      *pwd;
  2182.     WORD       flag, flag1;
  2183.     WORD       id;
  2184.  
  2185.     ptr = *psz;
  2186.     pwd = *pMenu;
  2187.    
  2188.     flag = *pwd;  // so difficult to correctly code this so let's try this
  2189.     pwd++;  
  2190.     if ((flag & 0x0010) == 0)
  2191.     {
  2192.         *(WORD *)ptr = flag;             // flag store
  2193.         ptr += 2;
  2194.         *(WORD *)ptr = id = *pwd;          // id store
  2195.         ptr += 2;
  2196.         pwd++;
  2197.  
  2198.         while (*pwd)                                             // name extract
  2199.         {
  2200.             *ptr = *(char *)pwd;
  2201.             if(!isprint(*ptr)) *ptr='.';
  2202.             ptr++; pwd++;
  2203.         } //name and null character
  2204.         *ptr=0;
  2205.         ptr++;
  2206.         pwd++;                           // skip double null
  2207.         
  2208.         *pMenu = pwd;
  2209.         *psz = ptr;
  2210.         return (int)flag;
  2211.     }
  2212.     else 
  2213.     {
  2214.         *(WORD *)ptr = flag;             // flag store
  2215.         ptr += 2;
  2216.    
  2217.         while (*pwd)                     // name extract
  2218.         {
  2219.             *ptr = *(char *)pwd;
  2220.             if(!isprint(*ptr)) *ptr='.';
  2221.             ptr++; pwd++;
  2222.         } //name and null character
  2223.         *ptr=0;
  2224.         ptr++;
  2225.         pwd++;                          // skip double null
  2226.    
  2227.                      // popup node: need to go on...
  2228.         while (1)
  2229.         {       
  2230.             //num++;
  2231.             *pMenu = pwd;
  2232.             *psz = ptr;
  2233.             flag1 = (WORD)MenuFill (psz, pMenu);
  2234.             pwd = *pMenu;
  2235.             ptr = *psz;
  2236.             if (flag1 & 0x0080) break; 
  2237.         }
  2238.     
  2239.         *pMenu = pwd;
  2240.         *psz = ptr;
  2241.         return flag;
  2242.     }
  2243. }
  2244.  
  2245.  
  2246. //
  2247. //==============================================================================
  2248. // The following program is based on preorder-tree-traversal.
  2249. // once you understand how to traverse..... 
  2250. // the rest is pretty straight forward.
  2251. // still we need to scan it first and fill it next time.
  2252. // and finally we can print it.
  2253. //
  2254. // This function is written by sang cho
  2255. //                                                         September 29, 1997
  2256. //                                                         revised october 2, 1997
  2257. //                             revised october 12, 1997
  2258. // ..............................................................................
  2259. // ------------------------------------------------------------------------------
  2260. // I use same structure - which is used in P.E. programs - for my reporting.
  2261. // So, my structure is as follows:
  2262. //        # of menu name is stored else where ( in directory I suppose )
  2263. //     supermenuname                    null terminated string, only ascii is considered.
  2264. //         flag                 tells : node is a leaf or a internal node.
  2265. //         popupname                    null terminated string
  2266. //              
  2267. //              flag                normal menu flag (leaf node)
  2268. //                      id                                  normal menu id
  2269. //              name                    normal menu name
  2270. //         or                            or
  2271. //              flag                        popup menu flag (internal node)
  2272. //              popupname                   popup menu name 
  2273. //             
  2274. //                 flag                             it may folows
  2275. //                         id                                   normal menu id
  2276. //                 name                                 normal menu name
  2277. //             or                                 or
  2278. //                 flag                                 popup menu
  2279. //                 popupname                    popup menu name
  2280. //                                 .........
  2281. //                                it goes on like this,
  2282. //                                 but usually, it only goes a few steps,...
  2283. // ------------------------------------------------------------------------------
  2284.  
  2285. /* scan menu and copy menu */
  2286. int     WINAPI GetContentsOfMenu (
  2287.     LPVOID    lpFile,
  2288.     char      **pszResTypes)
  2289. {
  2290.     PIMAGE_RESOURCE_DIRECTORY          prdType, prdName, prdLanguage;
  2291.     PIMAGE_RESOURCE_DIRECTORY_ENTRY    prde, prde1;
  2292.     PIMAGE_RESOURCE_DIR_STRING_U       pMenuName;
  2293.     PIMAGE_RESOURCE_DATA_ENTRY         prData;
  2294.     PIMAGE_MENU_HEADER                 pMenuHeader;
  2295.     PIMAGE_POPUP_MENU_ITEM             pPopup;
  2296.     char                    buff[256];
  2297.     int                     i,j; 
  2298.     int                     size;
  2299.     int                     sLength, nMenus;
  2300.     WORD                    flag;
  2301.     WORD                   *pwd;
  2302.     char                   *pMem;
  2303.  
  2304.  
  2305.     /* get root directory of resource tree */
  2306.     if ((prdType = (PIMAGE_RESOURCE_DIRECTORY)ImageDirectoryOffset
  2307.             (lpFile, IMAGE_DIRECTORY_ENTRY_RESOURCE)) == NULL)
  2308.     return 0;
  2309.  
  2310.     /* set pointer to first resource type entry */
  2311.     prde = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)
  2312.            ((DWORD)prdType + sizeof (IMAGE_RESOURCE_DIRECTORY) 
  2313.            + prdType->NumberOfNamedEntries*8);
  2314.  
  2315.     for (i=0; i<prdType->NumberOfIdEntries; i++)
  2316.     {
  2317.     if (prde->Name == RT_MENU) break;
  2318.     prde++;
  2319.     }
  2320.     if (prde->Name != RT_MENU) return 0; 
  2321.  
  2322.     prdName = (PIMAGE_RESOURCE_DIRECTORY)
  2323.           ((DWORD)prdType + (prde->OffsetToData ^ 0x80000000) ); 
  2324.     if (prdName == NULL) return 0;
  2325.  
  2326.     prde = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)
  2327.            ((DWORD)prdName + sizeof (IMAGE_RESOURCE_DIRECTORY));
  2328.  
  2329.     // sometimes previous code tells you lots of things hidden underneath
  2330.     // I wish I could save all the revisions I made ... but again .... sigh.
  2331.     //                                  october 12, 1997    sang
  2332.     //dwBase = (DWORD)((int)lpFile + psh->PointerToRawData - psh->VirtualAddress);
  2333.  
  2334.     nMenus=prdName->NumberOfNamedEntries + prdName->NumberOfIdEntries;
  2335.     sLength=0;
  2336.     
  2337.     for (i=0; i<prdName->NumberOfNamedEntries; i++)
  2338.     {
  2339.         pMenuName = (PIMAGE_RESOURCE_DIR_STRING_U) 
  2340.             ((DWORD)prdType + (prde->Name ^ 0x80000000));
  2341.         sLength += pMenuName->Length + 1;
  2342.         
  2343.         prdLanguage = (PIMAGE_RESOURCE_DIRECTORY)
  2344.               ((DWORD)prdType + (prde->OffsetToData ^ 0x80000000));
  2345.         if (prdLanguage == NULL) continue;
  2346.         
  2347.         prde1 = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)
  2348.             ((DWORD)prdLanguage + sizeof (IMAGE_RESOURCE_DIRECTORY)
  2349.             +prdLanguage->NumberOfNamedEntries*8);
  2350.         
  2351.         prData = (PIMAGE_RESOURCE_DATA_ENTRY)
  2352.              ((DWORD)prdType + prde1->OffsetToData);
  2353.         if (prData == NULL) continue;
  2354.         
  2355.         pMenuHeader =  (PIMAGE_MENU_HEADER)
  2356.                    GetActualAddress (lpFile, prData->OffsetToData);
  2357.         
  2358.         //
  2359.         // normally wVersion and cbHeaderSize should be zero
  2360.         // but if it is not then nothing is known to us...
  2361.         // so let's do our best ... namely guessing .... and trying ....
  2362.         //                      ... and suffering   ... 
  2363.         // it gave me many sleepless (not exactly but I like to say this) nights.
  2364.         //
  2365.  
  2366.         // strange case
  2367.         if (pMenuHeader->wVersion | pMenuHeader->cbHeaderSize)
  2368.         {
  2369.             //isStrange = TRUE;
  2370.             pwd = (WORD *)((DWORD)pMenuHeader + 16);
  2371.             size = prData->Size;
  2372.             // expect to return the length needed to report.
  2373.             // sixteen more bytes to do something
  2374.             sLength += 16+size;
  2375.             //StrangeMenuScan (&sLength, &pwd, size);   
  2376.         }
  2377.         // normal case
  2378.         else
  2379.         {
  2380.             pPopup = (PIMAGE_POPUP_MENU_ITEM)
  2381.                  ((DWORD)pMenuHeader + sizeof (IMAGE_MENU_HEADER));
  2382.             while (1)
  2383.             {       
  2384.                 flag = (WORD)MenuScan (&sLength, (WORD **)(&pPopup) );
  2385.                 if (flag & 0x0080) break;
  2386.             }
  2387.         }
  2388.         prde++;
  2389.     }
  2390.     for (i=0; i<prdName->NumberOfIdEntries; i++)
  2391.     {
  2392.         sLength += 12;
  2393.         
  2394.         prdLanguage = (PIMAGE_RESOURCE_DIRECTORY)
  2395.               ((DWORD)prdType + (prde->OffsetToData ^ 0x80000000));
  2396.         if (prdLanguage == NULL) continue;
  2397.         
  2398.         prde1 = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)
  2399.             ((DWORD)prdLanguage + sizeof (IMAGE_RESOURCE_DIRECTORY)
  2400.             +prdLanguage->NumberOfNamedEntries*8);
  2401.         
  2402.         prData = (PIMAGE_RESOURCE_DATA_ENTRY)
  2403.              ((DWORD)prdType + prde1->OffsetToData);
  2404.         if (prData == NULL) continue;
  2405.         
  2406.         pMenuHeader =  (PIMAGE_MENU_HEADER)
  2407.                    GetActualAddress (lpFile, prData->OffsetToData);
  2408.         // strange case
  2409.         if (pMenuHeader->wVersion | pMenuHeader->cbHeaderSize)
  2410.         {
  2411.             pwd = (WORD *)((DWORD)pMenuHeader + 16);
  2412.             size = prData->Size;
  2413.             // expect to return the length needed to report.
  2414.             // sixteen more bytes to do something
  2415.             sLength += 16+size;
  2416.             //StrangeMenuScan (&sLength, &pwd, size);
  2417.         }
  2418.         // normal case
  2419.         else
  2420.         {
  2421.             pPopup = (PIMAGE_POPUP_MENU_ITEM)
  2422.                  ((DWORD)pMenuHeader + sizeof (IMAGE_MENU_HEADER));
  2423.             while (1)
  2424.             {       
  2425.                 flag = (WORD)MenuScan (&sLength, (WORD **)(&pPopup) );
  2426.                 if (flag & 0x0080) break; 
  2427.             }
  2428.         }
  2429.         prde++;
  2430.     }
  2431.     //
  2432.     // allocate memory for menu names
  2433.     //
  2434.     *pszResTypes = (char *)calloc (sLength, 1);
  2435.  
  2436.     pMem = *pszResTypes;
  2437.     //
  2438.     // and start all over again
  2439.     //
  2440.     prde = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)
  2441.            ((DWORD)prdName + sizeof (IMAGE_RESOURCE_DIRECTORY));
  2442.  
  2443.     for (i=0; i<prdName->NumberOfNamedEntries; i++)
  2444.     {
  2445.         pMenuName = (PIMAGE_RESOURCE_DIR_STRING_U) 
  2446.             ((DWORD)prdType + (prde->Name ^ 0x80000000));
  2447.         
  2448.         
  2449.         for (j=0; j<pMenuName->Length; j++)
  2450.             *pMem++ = (char)(pMenuName->NameString[j]);
  2451.         *pMem = 0;
  2452.         pMem++;
  2453.         
  2454.  
  2455.         prdLanguage = (PIMAGE_RESOURCE_DIRECTORY)
  2456.               ((DWORD)prdType + (prde->OffsetToData ^ 0x80000000));
  2457.         if (prdLanguage == NULL) continue;
  2458.         
  2459.         prde1 = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)
  2460.             ((DWORD)prdLanguage + sizeof (IMAGE_RESOURCE_DIRECTORY)
  2461.             +prdLanguage->NumberOfNamedEntries*8);
  2462.         
  2463.         prData = (PIMAGE_RESOURCE_DATA_ENTRY)
  2464.              ((DWORD)prdType + prde1->OffsetToData);
  2465.         if (prData == NULL) continue;
  2466.         
  2467.         pMenuHeader =  (PIMAGE_MENU_HEADER)
  2468.                    GetActualAddress (lpFile, prData->OffsetToData);
  2469.         // strange case
  2470.         if (pMenuHeader->wVersion | pMenuHeader->cbHeaderSize)
  2471.         {
  2472.             pwd = (WORD *)((DWORD)pMenuHeader);
  2473.             size = prData->Size;
  2474.             strcpy (pMem, ":::::::::::"); pMem +=12;
  2475.             *(int *)pMem = size;          pMem += 4;
  2476.             StrangeMenuFill (&pMem, &pwd, size);
  2477.         }
  2478.         // normal case
  2479.         else
  2480.         {
  2481.             pPopup = (PIMAGE_POPUP_MENU_ITEM)
  2482.                  ((DWORD)pMenuHeader + sizeof (IMAGE_MENU_HEADER));
  2483.             while (1)
  2484.         {       
  2485.             flag = (WORD)MenuFill (&pMem, (WORD **)(&pPopup) );
  2486.         if (flag & 0x0080) break; 
  2487.         }
  2488.         }
  2489.     prde++;
  2490.     }
  2491.     for (i=0; i<prdName->NumberOfIdEntries; i++)
  2492.     {
  2493.         
  2494.         sprintf (buff, "MenuId_%04X", (int)(prde->Name));
  2495.         strcpy (pMem, buff);
  2496.         pMem += strlen (buff) + 1;
  2497.  
  2498.         prdLanguage = (PIMAGE_RESOURCE_DIRECTORY)
  2499.               ((DWORD)prdType + (prde->OffsetToData ^ 0x80000000));
  2500.         if (prdLanguage == NULL) continue;
  2501.         
  2502.         prde1 = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)
  2503.             ((DWORD)prdLanguage + sizeof (IMAGE_RESOURCE_DIRECTORY)
  2504.             +prdLanguage->NumberOfNamedEntries*8);
  2505.         
  2506.         prData = (PIMAGE_RESOURCE_DATA_ENTRY)
  2507.              ((DWORD)prdType + prde1->OffsetToData);
  2508.         if (prData == NULL) continue;
  2509.         
  2510.         pMenuHeader =  (PIMAGE_MENU_HEADER)
  2511.                    GetActualAddress (lpFile, prData->OffsetToData);
  2512.         // strange case
  2513.         if (pMenuHeader->wVersion | pMenuHeader->cbHeaderSize)
  2514.         {
  2515.             pwd = (WORD *)((DWORD)pMenuHeader);
  2516.             size = prData->Size;
  2517.             strcpy (pMem, ":::::::::::"); pMem +=12;
  2518.             *(int *)pMem = size;          pMem += 4;
  2519.             StrangeMenuFill (&pMem, &pwd, size);
  2520.         }
  2521.         // normal case
  2522.         else
  2523.         {
  2524.             pPopup = (PIMAGE_POPUP_MENU_ITEM)
  2525.                  ((DWORD)pMenuHeader + sizeof (IMAGE_MENU_HEADER));
  2526.             while (1)
  2527.             {
  2528.                 flag = (WORD)MenuFill (&pMem, (WORD **)(&pPopup) );
  2529.                 if (flag & 0x0080) break; 
  2530.             }
  2531.         }
  2532.         prde++;
  2533.     }
  2534.  
  2535.     return nMenus;
  2536. }
  2537.  
  2538.  
  2539. //
  2540. // This function is written by sang cho
  2541. //                                                         October 12, 1997
  2542.  
  2543. /* print contents of menu */
  2544. int     WINAPI PrintStrangeMenu (
  2545.     char      **psz)
  2546. {
  2547.     
  2548.     //int                     i, j, l;
  2549.     int                     num;
  2550.     //WORD                    flag1, flag2;
  2551.     //char                    buff[128];
  2552.     char                   *ptr, *pmax;
  2553.  
  2554.     //return dumpMenu (psz, size);
  2555.  
  2556.     ptr  = *psz;
  2557.  
  2558.     if(strncmp (ptr, ":::::::::::", 11) != 0) 
  2559.     {
  2560.         printf ("\n#### I don't know why!!!");
  2561.         dumpMenu (psz, 1024);
  2562.         exit (0);
  2563.     }
  2564.  
  2565.     ptr += 12;
  2566.     num = *(int *)ptr;
  2567.     ptr += 4;
  2568.     pmax = ptr+num;
  2569.  
  2570.     *psz = ptr;
  2571.     return dumpMenu (psz, num);
  2572.  
  2573.     // I will write some code later...
  2574.  
  2575. }
  2576.  
  2577.  
  2578. //
  2579. // This function is written by sang cho
  2580. //                                                         October 2, 1997
  2581.  
  2582. /* print contents of menu */
  2583. int     WINAPI PrintMenu1 (
  2584.     int         indent,
  2585.     char      **psz)
  2586. {
  2587.     
  2588.     int                     j, k, l;
  2589.     WORD                    id; //, num;
  2590.     WORD                    flag, flag1;
  2591.     char                    buff[128];
  2592.     char                   *ptr;
  2593.  
  2594.     ptr = *psz;
  2595.     flag = *(WORD *)ptr;
  2596.     ptr += 2;
  2597.     if ((flag&0x0010)==0x0000)
  2598.     {
  2599.         printf ("\n");
  2600.         for (j=0; j<indent; j++) printf (" ");
  2601.         id = *(WORD *)ptr;
  2602.         ptr += 2;
  2603.         strcpy (buff, ptr);
  2604.         l = strlen (ptr);
  2605.         ptr += l+1;
  2606.         if (strchr (buff, 0x09) != NULL)
  2607.         {
  2608.             for (k=0; k<l; k++) if (buff[k] == 0x09) break;
  2609.             for (j=0; j<l-k; j++) buff[31-j]=buff[l-j];
  2610.             for (j=k; j<32+k-l; j++) buff[j]=32;
  2611.         }
  2612.         if (strchr (buff, 0x08) != NULL)
  2613.         {
  2614.             for (k=0; k<l; k++) if (buff[k] == 0x08) break;
  2615.             for (j=0; j<l-k; j++) buff[31-j]=buff[l-j];
  2616.             for (j=k; j<32+k-l; j++) buff[j]=32;
  2617.         }
  2618.         printf ("%s", buff);
  2619.         l = strlen (buff);
  2620.         for (j=l; j<32; j++) printf(" ");
  2621.         printf ("[ID=%04Xh]", id);
  2622.         *psz = ptr;
  2623.         return (int)flag;
  2624.     }
  2625.     else
  2626.     {
  2627.         printf ("\n");
  2628.         printf ("%s  {Popup}",ptr);
  2629.         ptr += strlen (ptr) + 1;
  2630.         *psz = ptr;
  2631.     
  2632.         while (1)
  2633.         {       
  2634.             *psz = ptr;
  2635.             flag1=(WORD)PrintMenu1 (indent+5, psz);
  2636.             ptr = *psz;
  2637.             if (flag1 & 0x0080) break; 
  2638.         }
  2639.         *psz = ptr;
  2640.         return (int)flag;
  2641.     }
  2642. }
  2643.  
  2644. //
  2645. // This function is written by sang cho
  2646. //                                                         September 4, 1998
  2647.  
  2648. /* print contents of menu */
  2649. int     WINAPI PrintMenu (
  2650.     int         indent,
  2651.     char      **psz)
  2652. {
  2653.     WORD                    flag;
  2654.     
  2655.     while (1)
  2656.     {       
  2657.         flag=(WORD)PrintMenu1 (indent, psz);
  2658.         if (flag & 0x0080) break; 
  2659.     }
  2660.     return (int)flag;
  2661. }
  2662.  
  2663.  
  2664. //
  2665. // This function is written by sang cho
  2666. //                                                         October 2, 1997
  2667.  
  2668. /* the format of menu is not known so I'll do my best */
  2669. int     WINAPI dumpMenu (
  2670.     char      **psz,
  2671.     int         size)
  2672. {
  2673.     
  2674.     int                                 i, j, k, n, l,c;
  2675.     char                    buff[32];
  2676.     char                           *ptr, *pmax;
  2677.  
  2678.     ptr  = *psz;
  2679.     pmax = ptr+size;
  2680.     for (i=0; i<(size/16)+1; i++)
  2681.     {
  2682.         n = 0;
  2683.         for (j=0; j<16; j++)
  2684.         {
  2685.             c = (int)(*ptr);
  2686.             if (c<0) c+=256;
  2687.             buff[j] = c;
  2688.             printf ("%02X",c);
  2689.             ptr++; 
  2690.             if (ptr >= pmax) break;
  2691.             n++;
  2692.             if (n%4 == 0) printf (" "); 
  2693.         }
  2694.         n++; if (n%4 == 0) printf (" ");
  2695.         l = j;
  2696.         j++;
  2697.         for (; j<16; j++) 
  2698.         { n++; if (n%4 == 0) printf ("   "); else printf ("  "); }
  2699.         printf ("   ");
  2700.         for (k=0; k<l; k++)
  2701.             if (isprint(c=buff[k])) printf("%c", c); else printf(".");
  2702.         printf ("\n");
  2703.         if (ptr >= pmax) break;
  2704.     }
  2705.  
  2706.     *psz = ptr;
  2707.     return 1;
  2708. }
  2709.  
  2710. //
  2711. // This function is written by sang cho
  2712. //                                                         October 13, 1997
  2713.  
  2714. /* scan dialog box and copy dialog box */
  2715. int     WINAPI GetContentsOfDialog (
  2716.     LPVOID    lpFile,
  2717.     char      **pszResTypes)
  2718. {
  2719.     PIMAGE_RESOURCE_DIRECTORY          prdType, prdName, prdLanguage;
  2720.     PIMAGE_RESOURCE_DIRECTORY_ENTRY    prde, prde1;
  2721.     PIMAGE_RESOURCE_DIR_STRING_U       pDialogName;
  2722.     PIMAGE_RESOURCE_DATA_ENTRY         prData;
  2723.     PIMAGE_DIALOG_HEADER               pDialogHeader;
  2724.     char                    buff[32];
  2725.     int                     i,j; 
  2726.     int                     size;
  2727.     int                     sLength, nDialogs;
  2728.     //WORD                    flag;
  2729.     WORD                   *pwd;
  2730.     char                   *pMem; 
  2731.  
  2732.  
  2733.     /* get root directory of resource tree */
  2734.     if ((prdType = (PIMAGE_RESOURCE_DIRECTORY)ImageDirectoryOffset
  2735.             (lpFile, IMAGE_DIRECTORY_ENTRY_RESOURCE)) == NULL)
  2736.     return 0;
  2737.  
  2738.     /* set pointer to first resource type entry */
  2739.     prde = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)
  2740.            ((DWORD)prdType + sizeof (IMAGE_RESOURCE_DIRECTORY)
  2741.            + prdType->NumberOfNamedEntries*8);
  2742.  
  2743.     for (i=0; i<prdType->NumberOfIdEntries; i++)
  2744.     {
  2745.     if (prde->Name == RT_DIALOG) break;
  2746.     prde++;
  2747.     }
  2748.     if (prde->Name != RT_DIALOG) return 0; 
  2749.  
  2750.     prdName = (PIMAGE_RESOURCE_DIRECTORY)
  2751.           ((DWORD)prdType + (prde->OffsetToData ^ 0x80000000) ); 
  2752.     if (prdName == NULL) return 0;
  2753.  
  2754.     prde = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)
  2755.            ((DWORD)prdName + sizeof (IMAGE_RESOURCE_DIRECTORY));
  2756.  
  2757.  
  2758.     nDialogs=prdName->NumberOfNamedEntries + prdName->NumberOfIdEntries;
  2759.     sLength=0;
  2760.     
  2761.     for (i=0; i<prdName->NumberOfNamedEntries; i++)
  2762.     {
  2763.         pDialogName = (PIMAGE_RESOURCE_DIR_STRING_U) 
  2764.               ((DWORD)prdType + (prde->Name ^ 0x80000000));
  2765.         sLength += pDialogName->Length + 1;
  2766.         
  2767.         prdLanguage = (PIMAGE_RESOURCE_DIRECTORY)
  2768.               ((DWORD)prdType + (prde->OffsetToData ^ 0x80000000));
  2769.         if (prdLanguage == NULL) continue;
  2770.         
  2771.         prde1 = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)
  2772.             ((DWORD)prdLanguage + sizeof (IMAGE_RESOURCE_DIRECTORY)
  2773.             +prdLanguage->NumberOfNamedEntries*8);
  2774.         
  2775.         prData = (PIMAGE_RESOURCE_DATA_ENTRY)
  2776.              ((DWORD)prdType + prde1->OffsetToData);
  2777.         if (prData == NULL) continue;
  2778.         
  2779.         size = prData->Size; 
  2780.         sLength += 4+size;       
  2781.         prde++;
  2782.     }
  2783.     for (i=0; i<prdName->NumberOfIdEntries; i++)
  2784.     {
  2785.         sLength += 14;
  2786.         
  2787.         prdLanguage = (PIMAGE_RESOURCE_DIRECTORY)
  2788.               ((DWORD)prdType + (prde->OffsetToData ^ 0x80000000));
  2789.         if (prdLanguage == NULL) continue;
  2790.         
  2791.         prde1 = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)
  2792.             ((DWORD)prdLanguage + sizeof (IMAGE_RESOURCE_DIRECTORY)
  2793.             +prdLanguage->NumberOfNamedEntries*8);
  2794.         
  2795.         prData = (PIMAGE_RESOURCE_DATA_ENTRY)
  2796.              ((DWORD)prdType + prde1->OffsetToData);
  2797.         if (prData == NULL) continue;
  2798.         
  2799.         size = prData->Size; 
  2800.         sLength += 4+size;       
  2801.         prde++;
  2802.     }
  2803.     //
  2804.     // allocate memory for menu names
  2805.     //
  2806.     *pszResTypes = (char *)calloc (sLength, 1);
  2807.  
  2808.     pMem = *pszResTypes;
  2809.     //
  2810.     // and start all over again
  2811.     //
  2812.     prde = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)
  2813.            ((DWORD)prdName + sizeof (IMAGE_RESOURCE_DIRECTORY));
  2814.  
  2815.     for (i=0; i<prdName->NumberOfNamedEntries; i++)
  2816.     {
  2817.         pDialogName = (PIMAGE_RESOURCE_DIR_STRING_U) 
  2818.               ((DWORD)prdType + (prde->Name ^ 0x80000000));
  2819.         
  2820.         
  2821.         for (j=0; j<pDialogName->Length; j++)
  2822.             *pMem++ = (char)(pDialogName->NameString[j]);
  2823.         *pMem = 0;
  2824.         pMem++;
  2825.         
  2826.  
  2827.         prdLanguage = (PIMAGE_RESOURCE_DIRECTORY)
  2828.               ((DWORD)prdType + (prde->OffsetToData ^ 0x80000000));
  2829.         if (prdLanguage == NULL) continue;
  2830.         
  2831.         prde1 = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)
  2832.             ((DWORD)prdLanguage + sizeof (IMAGE_RESOURCE_DIRECTORY)
  2833.             +prdLanguage->NumberOfNamedEntries*8);
  2834.         
  2835.         prData = (PIMAGE_RESOURCE_DATA_ENTRY)
  2836.              ((DWORD)prdType + prde1->OffsetToData);
  2837.         if (prData == NULL) continue;
  2838.         
  2839.         pDialogHeader =  (PIMAGE_DIALOG_HEADER)
  2840.                  GetActualAddress (lpFile, prData->OffsetToData);
  2841.      
  2842.         
  2843.         
  2844.         pwd = (WORD *)((DWORD)pDialogHeader);
  2845.         size = prData->Size;
  2846.         *(int *)pMem = size;          pMem += 4;
  2847.         StrangeMenuFill (&pMem, &pwd, size);
  2848.         
  2849.     prde++;
  2850.     }
  2851.     for (i=0; i<prdName->NumberOfIdEntries; i++)
  2852.     {
  2853.         
  2854.         sprintf (buff, "DialogId_%04X", (int)(prde->Name));
  2855.         strcpy (pMem, buff);
  2856.         pMem += strlen (buff) + 1;
  2857.  
  2858.         prdLanguage = (PIMAGE_RESOURCE_DIRECTORY)
  2859.               ((DWORD)prdType + (prde->OffsetToData ^ 0x80000000));
  2860.         if (prdLanguage == NULL) 
  2861.         {   printf ("\nprdLanguage = NULL"); exit (0); }
  2862.         
  2863.         prde1 = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)
  2864.             ((DWORD)prdLanguage + sizeof (IMAGE_RESOURCE_DIRECTORY)
  2865.             +prdLanguage->NumberOfNamedEntries*8);
  2866.         
  2867.         prData = (PIMAGE_RESOURCE_DATA_ENTRY)
  2868.              ((DWORD)prdType + prde1->OffsetToData);
  2869.         if (prData == NULL) 
  2870.         {   printf ("\nprData = NULL"); exit (0); }
  2871.  
  2872.         pDialogHeader =  (PIMAGE_DIALOG_HEADER)
  2873.                  GetActualAddress (lpFile, prData->OffsetToData);
  2874.         
  2875.         
  2876.         pwd = (WORD *)((DWORD)pDialogHeader);
  2877.         size = prData->Size;
  2878.         *(int *)pMem = size;          pMem += 4;
  2879.         StrangeMenuFill (&pMem, &pwd, size);
  2880.         
  2881.         prde++;
  2882.     }
  2883.  
  2884.     return nDialogs;
  2885. }
  2886.  
  2887. //
  2888. // This function is written by sang cho
  2889. //                                                         October 14, 1997
  2890.  
  2891. /* print contents of dialog */
  2892. int     WINAPI PrintNameOrOrdinal (
  2893.     char      **psz)
  2894. {    
  2895.     char                           *ptr;
  2896.  
  2897.     ptr = *psz;
  2898.     if (*(WORD *)ptr == 0xFFFF) 
  2899.     {       ptr += 2; 
  2900.         printf ("%04X", *(WORD *)ptr);
  2901.         ptr += 2;
  2902.     }
  2903.     else 
  2904.     { 
  2905.         printf ("%c", '"');
  2906.         while (*(WORD *)ptr) 
  2907.         { if(isprint(*ptr))printf("%c", *ptr); else printf("."); ptr+= 2; } 
  2908.         ptr += 2;
  2909.         printf ("%c", '"');
  2910.     }
  2911.     *psz = ptr;
  2912.     return 1;
  2913. }
  2914.  
  2915. //
  2916. // This function is written by sang cho
  2917. //                                                         October 14, 1997
  2918.  
  2919. /* print contents of dialog */
  2920. int     WINAPI PrintDialog (
  2921.     char      **psz)
  2922. {    
  2923.     int                     i; 
  2924.     int                     num, size;
  2925.     DWORD                   flag;
  2926.     WORD                    class;
  2927.     char                    *ptr, *pmax;
  2928.     BOOL                    isStrange=FALSE;
  2929.  
  2930.     ptr  = *psz;
  2931.     size = *(int *)ptr;
  2932.     ptr += 4;
  2933.     pmax = ptr+size;
  2934.     
  2935.     // IStype of Dialog Header
  2936.     flag = *(DWORD *)ptr;
  2937.     //
  2938.     // check if flag is right or not
  2939.     // it has been observed that some dialog information is strange
  2940.     // and extra work is needed to fix that ... so let's try something
  2941.     //
  2942.     
  2943.     if ((flag & 0xFFFF0000) == 0xFFFF0000)
  2944.     {
  2945.         flag = *(DWORD *)(ptr+12);  
  2946.         num = *(short *)(ptr+16); 
  2947.         isStrange = TRUE;
  2948.         ptr += 26;
  2949.     }
  2950.     else 
  2951.     {
  2952.         num  = *(short *)(ptr+8);
  2953.         ptr += 18;
  2954.     }
  2955.     printf (", # of Controls=%03d, Caption:%c", num, '"');
  2956.     
  2957.     // Menu name
  2958.          if (*(WORD *)ptr == 0xFFFF) ptr += 4;                // ordinal
  2959.     else { while (*(WORD *)ptr) ptr += 2; ptr += 2; } // name
  2960.     
  2961.     // Class name
  2962.          if (*(WORD *)ptr == 0xFFFF) ptr += 4;                // ordinal
  2963.     else { while (*(WORD *)ptr) ptr += 2; ptr += 2; } // name
  2964.  
  2965.     // Caption
  2966.     while (*(WORD *)ptr) { printf("%c", *ptr); ptr+= 2; }
  2967.     ptr += 2;
  2968.     printf ("%c", '"');
  2969.     
  2970.     // FONT present
  2971.     if (flag & 0x00000040)
  2972.     {
  2973.         if (isStrange) ptr += 6; else ptr += 2;      // FONT size
  2974.         while (*(WORD *)ptr)  ptr += 2;                          // WCHARs
  2975.         ptr += 2;                                    // double null  
  2976.     }
  2977.  
  2978.     // strange case adjust
  2979.     if (isStrange) ptr += 8;
  2980.  
  2981.     // DWORD padding
  2982.     if ((ptr-*psz) % 4) ptr += 4 - ((ptr-*psz) % 4);
  2983.  
  2984.     // start reporting .. finally
  2985.     for (i=0; i<num; i++)
  2986.     {
  2987.         flag = *(DWORD *)ptr;
  2988.         if (isStrange) ptr += 14; else ptr += 16;
  2989.         printf ("\n     Control::%03d - ID:", i+1);
  2990.         
  2991.         // Control ID
  2992.         printf ("%04X, Class:", *(WORD *)ptr);
  2993.         ptr += 2;
  2994.        
  2995.         // Control Class
  2996.         if (*(WORD *)ptr == 0xFFFF) 
  2997.         {   
  2998.             ptr += 2;  class = *(WORD *)ptr;   ptr += 2;
  2999.             switch (class)
  3000.             {
  3001.                 case 0x80: printf ("BUTTON   ");        break;       
  3002.                 case 0x81: printf ("EDIT     ");        break;    
  3003.                 case 0x82: printf ("STATIC   ");        break;    
  3004.                 case 0x83: printf ("LISTBOX  ");        break;    
  3005.                 case 0x84: printf ("SCROLLBAR");        break;    
  3006.                 case 0x85: printf ("COMBOBOX ");        break;    
  3007.                 default:   printf ("%04X     ", class); break;
  3008.             }
  3009.         }
  3010.         else PrintNameOrOrdinal (&ptr);
  3011.  
  3012.         printf (" Text:");
  3013.  
  3014.         // Text
  3015.         PrintNameOrOrdinal (&ptr);
  3016.  
  3017.         // nExtraStuff
  3018.         ptr += 2;
  3019.         
  3020.         // strange case adjust
  3021.         if (isStrange) ptr += 8;
  3022.  
  3023.         // DWORD padding
  3024.         if ((ptr-*psz) % 4) ptr += 4 - ((ptr-*psz) % 4);
  3025.     }
  3026.  
  3027.     *psz = pmax;
  3028.     return 1;
  3029. }
  3030.  
  3031. //
  3032. // This function is written by sang cho
  3033. //                                                         September 5, 1998
  3034.  
  3035. /* scan string and copy string */
  3036. int     WINAPI GetContentsOfString (
  3037.     LPVOID    lpFile,
  3038.     char      **pszResTypes)
  3039. {
  3040.     PIMAGE_RESOURCE_DIRECTORY          prdType, prdName, prdLanguage;
  3041.     PIMAGE_RESOURCE_DIRECTORY_ENTRY    prde, prde1;
  3042.     PIMAGE_RESOURCE_DIR_STRING_U       pStringName;
  3043.     PIMAGE_RESOURCE_DATA_ENTRY         prData;
  3044.     PWORD                              pStringHeader;
  3045.     char                    buff[256];
  3046.     int                     i,j; 
  3047.     int                     size;
  3048.     int                     sLength, nStrings;
  3049.     WORD                   *pwd;
  3050.     char                   *pMem; 
  3051.  
  3052.     /* get root directory of resource tree */
  3053.     if ((prdType = (PIMAGE_RESOURCE_DIRECTORY)ImageDirectoryOffset
  3054.             (lpFile, IMAGE_DIRECTORY_ENTRY_RESOURCE)) == NULL)
  3055.     return 0;
  3056.  
  3057.     /* set pointer to first resource type entry */
  3058.     prde = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)
  3059.            ((DWORD)prdType + sizeof (IMAGE_RESOURCE_DIRECTORY)
  3060.            + prdType->NumberOfNamedEntries*8);
  3061.  
  3062.     for (i=0; i<prdType->NumberOfIdEntries; i++)
  3063.     {
  3064.     if (prde->Name == RT_STRING) break;
  3065.     prde++;
  3066.     }
  3067.     if (prde->Name != RT_STRING) return 0; 
  3068.  
  3069.     prdName = (PIMAGE_RESOURCE_DIRECTORY)
  3070.           ((DWORD)prdType + (prde->OffsetToData ^ 0x80000000) ); 
  3071.     if (prdName == NULL) return 0;
  3072.  
  3073.     prde = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)
  3074.            ((DWORD)prdName + sizeof (IMAGE_RESOURCE_DIRECTORY));
  3075.  
  3076.  
  3077.     nStrings=prdName->NumberOfNamedEntries + prdName->NumberOfIdEntries;
  3078.     sLength=0;
  3079.     
  3080.     for (i=0; i<prdName->NumberOfNamedEntries; i++)
  3081.     {
  3082.         pStringName = (PIMAGE_RESOURCE_DIR_STRING_U) 
  3083.               ((DWORD)prdType + (prde->Name ^ 0x80000000));
  3084.         sLength += pStringName->Length + 1;
  3085.         
  3086.         prdLanguage = (PIMAGE_RESOURCE_DIRECTORY)
  3087.               ((DWORD)prdType + (prde->OffsetToData ^ 0x80000000));
  3088.         if (prdLanguage == NULL) continue;
  3089.         
  3090.         prde1 = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)
  3091.             ((DWORD)prdLanguage + sizeof (IMAGE_RESOURCE_DIRECTORY)
  3092.             +prdLanguage->NumberOfNamedEntries*8);
  3093.         
  3094.         prData = (PIMAGE_RESOURCE_DATA_ENTRY)
  3095.              ((DWORD)prdType + prde1->OffsetToData);
  3096.         if (prData == NULL) continue;
  3097.         
  3098.         size = prData->Size; 
  3099.         sLength += 4+size;       
  3100.         prde++;
  3101.     }
  3102.     for (i=0; i<prdName->NumberOfIdEntries; i++)
  3103.     {
  3104.         sLength += 14;
  3105.         
  3106.         prdLanguage = (PIMAGE_RESOURCE_DIRECTORY)
  3107.               ((DWORD)prdType + (prde->OffsetToData ^ 0x80000000));
  3108.         if (prdLanguage == NULL) continue;
  3109.         
  3110.         prde1 = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)
  3111.             ((DWORD)prdLanguage + sizeof (IMAGE_RESOURCE_DIRECTORY)
  3112.             +prdLanguage->NumberOfNamedEntries*8);
  3113.         
  3114.         prData = (PIMAGE_RESOURCE_DATA_ENTRY)
  3115.              ((DWORD)prdType + prde1->OffsetToData);
  3116.         if (prData == NULL) continue;
  3117.         
  3118.         size = prData->Size; 
  3119.         sLength += 4+size;       
  3120.         prde++;
  3121.     }
  3122.     //
  3123.     // allocate memory for menu names
  3124.     //
  3125.     *pszResTypes = (char *)calloc (sLength, 1);
  3126.  
  3127.     pMem = *pszResTypes;
  3128.     //
  3129.     // and start all over again
  3130.     //
  3131.     prde = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)
  3132.            ((DWORD)prdName + sizeof (IMAGE_RESOURCE_DIRECTORY));
  3133.  
  3134.     for (i=0; i<prdName->NumberOfNamedEntries; i++)
  3135.     {
  3136.         pStringName = (PIMAGE_RESOURCE_DIR_STRING_U) 
  3137.               ((DWORD)prdType + (prde->Name ^ 0x80000000));
  3138.         
  3139.         
  3140.         for (j=0; j<pStringName->Length; j++)
  3141.             *pMem++ = (char)(pStringName->NameString[j]);
  3142.         *pMem = 0;
  3143.         pMem++;
  3144.         
  3145.  
  3146.         prdLanguage = (PIMAGE_RESOURCE_DIRECTORY)
  3147.               ((DWORD)prdType + (prde->OffsetToData ^ 0x80000000));
  3148.         if (prdLanguage == NULL) continue;
  3149.         
  3150.         prde1 = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)
  3151.             ((DWORD)prdLanguage + sizeof (IMAGE_RESOURCE_DIRECTORY)
  3152.             +prdLanguage->NumberOfNamedEntries*8);
  3153.         
  3154.         prData = (PIMAGE_RESOURCE_DATA_ENTRY)
  3155.              ((DWORD)prdType + prde1->OffsetToData);
  3156.         if (prData == NULL) continue;
  3157.         
  3158.         pStringHeader =  (PWORD)
  3159.                  GetActualAddress (lpFile, prData->OffsetToData);
  3160.      
  3161.         
  3162.         
  3163.         pwd = pStringHeader;
  3164.         size = prData->Size;
  3165.         *(int *)pMem = size;          pMem += 4;
  3166.         StrangeMenuFill (&pMem, &pwd, size);
  3167.         
  3168.         prde++;
  3169.     }
  3170.     for (i=0; i<prdName->NumberOfIdEntries; i++)
  3171.     {
  3172.         
  3173.         sprintf (buff, "StringId_%04X", (int)(prde->Name));
  3174.         strcpy (pMem, buff);
  3175.         pMem += strlen (buff) + 1;
  3176.  
  3177.         prdLanguage = (PIMAGE_RESOURCE_DIRECTORY)
  3178.               ((DWORD)prdType + (prde->OffsetToData ^ 0x80000000));
  3179.         if (prdLanguage == NULL) 
  3180.         {   printf ("\nprdLanguage = NULL"); exit (0); }
  3181.         
  3182.         prde1 = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)
  3183.             ((DWORD)prdLanguage + sizeof (IMAGE_RESOURCE_DIRECTORY)
  3184.             +prdLanguage->NumberOfNamedEntries*8);
  3185.         
  3186.         prData = (PIMAGE_RESOURCE_DATA_ENTRY)
  3187.              ((DWORD)prdType + prde1->OffsetToData);
  3188.         if (prData == NULL) 
  3189.         {   printf ("\nprData = NULL"); exit (0); }
  3190.  
  3191.         pStringHeader =  (PWORD)
  3192.                  GetActualAddress (lpFile, prData->OffsetToData);
  3193.         
  3194.         
  3195.         pwd = pStringHeader;
  3196.         size = prData->Size;
  3197.         *(int *)pMem = size;          pMem += 4;
  3198.         StrangeMenuFill (&pMem, &pwd, size);
  3199.         
  3200.         prde++;
  3201.     }
  3202.  
  3203.     return nStrings;
  3204. }
  3205.  
  3206. //
  3207. // This function is written by sang cho
  3208. //                                                         September 5, 1998
  3209.  
  3210. /* print contents of string */
  3211. int     WINAPI PrintString (
  3212.     char      **psz)
  3213. {    
  3214.     int                     i; 
  3215.     int                     num, size;
  3216.     char                    *ptr, *pmax;
  3217.  
  3218.     ptr  = *psz;
  3219.     size = *(int *)ptr;
  3220.     ptr += 4;
  3221.     pmax = ptr+size;
  3222.     
  3223.        while (ptr<pmax)
  3224.     {
  3225.         num=*ptr;
  3226.         ptr+=2;
  3227.        
  3228.         i=0;   if(num<0) num+=256;
  3229.         if (num>0) {printf ("\n    ");  printf ("%c",'"');}
  3230.         while (i<num) 
  3231.         { 
  3232.             if  (*ptr==0x09) printf("<t>");
  3233.             else if    (*ptr==0x0D && *(ptr+2)==0x0A) {printf("<nl>"); ptr+=2; i++;}
  3234.             else if (isprint(*ptr)) printf("%c", *ptr);
  3235.             else printf(".");
  3236.             ptr+= 2; i++;
  3237.         }
  3238.         if (num>0) printf ("%c", '"');
  3239.     }
  3240.     
  3241.     *psz = pmax;
  3242.     return 1;
  3243. }
  3244.  
  3245. //
  3246. // This function is written by sang cho
  3247. //                                                         September 6, 1998
  3248.  
  3249. /* scan icon and copy icon */
  3250. int     WINAPI GetContentsOfIcon (
  3251.     LPVOID    lpFile,
  3252.     char      **pszResTypes)
  3253. {
  3254.     PIMAGE_RESOURCE_DIRECTORY          prdType, prdName, prdLanguage;
  3255.     PIMAGE_RESOURCE_DIRECTORY_ENTRY    prde, prde1;
  3256.     PIMAGE_RESOURCE_DIR_STRING_U       pIconName;
  3257.     PIMAGE_RESOURCE_DATA_ENTRY         prData;
  3258.     PWORD                              pIconHeader;
  3259.     char                    buff[256];
  3260.     int                     i,j; 
  3261.     int                     size;
  3262.     int                     sLength, nIcons;
  3263.     WORD                   *pwd;
  3264.     char                   *pMem; 
  3265.  
  3266.     /* get root directory of resource tree */
  3267.     if ((prdType = (PIMAGE_RESOURCE_DIRECTORY)ImageDirectoryOffset
  3268.             (lpFile, IMAGE_DIRECTORY_ENTRY_RESOURCE)) == NULL)
  3269.     return 0;
  3270.  
  3271.     /* set pointer to first resource type entry */
  3272.     prde = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)
  3273.            ((DWORD)prdType + sizeof (IMAGE_RESOURCE_DIRECTORY)
  3274.            +prdType->NumberOfNamedEntries*8);
  3275.  
  3276.     for (i=0; i<prdType->NumberOfIdEntries; i++)
  3277.     {
  3278.     if (prde->Name == RT_ICON) break;
  3279.     prde++;
  3280.     }
  3281.     if (prde->Name != RT_ICON) return 0; 
  3282.  
  3283.     prdName = (PIMAGE_RESOURCE_DIRECTORY)
  3284.           ((DWORD)prdType + (prde->OffsetToData ^ 0x80000000) ); 
  3285.     if (prdName == NULL) return 0;
  3286.  
  3287.     prde = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)
  3288.            ((DWORD)prdName + sizeof (IMAGE_RESOURCE_DIRECTORY));
  3289.  
  3290.  
  3291.     nIcons=prdName->NumberOfNamedEntries + prdName->NumberOfIdEntries;
  3292.     sLength=0;
  3293.     
  3294.     for (i=0; i<prdName->NumberOfNamedEntries; i++)
  3295.     {
  3296.         pIconName = (PIMAGE_RESOURCE_DIR_STRING_U) 
  3297.               ((DWORD)prdType + (prde->Name ^ 0x80000000));
  3298.         sLength += pIconName->Length + 1;
  3299.         
  3300.         prdLanguage = (PIMAGE_RESOURCE_DIRECTORY)
  3301.               ((DWORD)prdType + (prde->OffsetToData ^ 0x80000000));
  3302.         if (prdLanguage == NULL) continue;
  3303.         
  3304.         prde1 = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)
  3305.             ((DWORD)prdLanguage + sizeof (IMAGE_RESOURCE_DIRECTORY)
  3306.             +prdLanguage->NumberOfNamedEntries*8);
  3307.         
  3308.         prData = (PIMAGE_RESOURCE_DATA_ENTRY)
  3309.              ((DWORD)prdType + prde1->OffsetToData);
  3310.         if (prData == NULL) continue;
  3311.         
  3312.         size = prData->Size; 
  3313.         sLength += 4+size;       
  3314.         prde++;
  3315.     }
  3316.     for (i=0; i<prdName->NumberOfIdEntries; i++)
  3317.     {
  3318.         sLength += 14;
  3319.         
  3320.         prdLanguage = (PIMAGE_RESOURCE_DIRECTORY)
  3321.               ((DWORD)prdType + (prde->OffsetToData ^ 0x80000000));
  3322.         if (prdLanguage == NULL) continue;
  3323.         
  3324.         prde1 = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)
  3325.             ((DWORD)prdLanguage + sizeof (IMAGE_RESOURCE_DIRECTORY)
  3326.             +prdLanguage->NumberOfNamedEntries*8);
  3327.         
  3328.         prData = (PIMAGE_RESOURCE_DATA_ENTRY)
  3329.              ((DWORD)prdType + prde1->OffsetToData);
  3330.         if (prData == NULL) continue;
  3331.         
  3332.         size = prData->Size; 
  3333.         sLength += 4+size;       
  3334.         prde++;
  3335.     }
  3336.     //
  3337.     // allocate memory for icon names
  3338.     //
  3339.     *pszResTypes = (char *)calloc (sLength, 1);
  3340.  
  3341.     pMem = *pszResTypes;
  3342.     //
  3343.     // and start all over again
  3344.     //
  3345.     prde = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)
  3346.            ((DWORD)prdName + sizeof (IMAGE_RESOURCE_DIRECTORY));
  3347.  
  3348.     for (i=0; i<prdName->NumberOfNamedEntries; i++)
  3349.     {
  3350.         pIconName = (PIMAGE_RESOURCE_DIR_STRING_U) 
  3351.               ((DWORD)prdType + (prde->Name ^ 0x80000000));
  3352.         
  3353.         
  3354.         for (j=0; j<pIconName->Length; j++)
  3355.             *pMem++ = (char)(pIconName->NameString[j]);
  3356.         *pMem = 0;
  3357.         pMem++;
  3358.         
  3359.  
  3360.         prdLanguage = (PIMAGE_RESOURCE_DIRECTORY)
  3361.               ((DWORD)prdType + (prde->OffsetToData ^ 0x80000000));
  3362.         if (prdLanguage == NULL) continue;
  3363.         
  3364.         prde1 = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)
  3365.             ((DWORD)prdLanguage + sizeof (IMAGE_RESOURCE_DIRECTORY)
  3366.             +prdLanguage->NumberOfNamedEntries*8);
  3367.         
  3368.         prData = (PIMAGE_RESOURCE_DATA_ENTRY)
  3369.              ((DWORD)prdType + prde1->OffsetToData);
  3370.         if (prData == NULL) continue;
  3371.         
  3372.         pIconHeader =  (PWORD)
  3373.                  GetActualAddress (lpFile, prData->OffsetToData);
  3374.      
  3375.         
  3376.         
  3377.         pwd = pIconHeader;
  3378.         size = prData->Size;
  3379.         *(int *)pMem = size;          pMem += 4;
  3380.         StrangeMenuFill (&pMem, &pwd, size);
  3381.         
  3382.         prde++;
  3383.     }
  3384.     for (i=0; i<prdName->NumberOfIdEntries; i++)
  3385.     {
  3386.         
  3387.         sprintf (buff, "IconId_%04X", (int)(prde->Name));
  3388.         strcpy (pMem, buff);
  3389.         pMem += strlen (buff) + 1;
  3390.  
  3391.         prdLanguage = (PIMAGE_RESOURCE_DIRECTORY)
  3392.               ((DWORD)prdType + (prde->OffsetToData ^ 0x80000000));
  3393.         if (prdLanguage == NULL) 
  3394.         {   printf ("\nprdLanguage = NULL"); exit (0); }
  3395.         
  3396.         prde1 = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)
  3397.             ((DWORD)prdLanguage + sizeof (IMAGE_RESOURCE_DIRECTORY)
  3398.             +prdLanguage->NumberOfNamedEntries*8);
  3399.         
  3400.         prData = (PIMAGE_RESOURCE_DATA_ENTRY)
  3401.              ((DWORD)prdType + prde1->OffsetToData);
  3402.         if (prData == NULL) 
  3403.         {   printf ("\nprData = NULL"); exit (0); }
  3404.  
  3405.         pIconHeader =  (PWORD)
  3406.                  GetActualAddress (lpFile, prData->OffsetToData);
  3407.         
  3408.         
  3409.         pwd = pIconHeader;
  3410.         size = prData->Size;
  3411.         *(int *)pMem = size;          pMem += 4;
  3412.         StrangeMenuFill (&pMem, &pwd, size);
  3413.         
  3414.         prde++;
  3415.     }
  3416.  
  3417.     return nIcons;
  3418. }
  3419.  
  3420. //
  3421. // This function is written by sang cho
  3422. //                                                         September 6, 1998
  3423.  
  3424. /* print contents of icon */
  3425. int     WINAPI PrintIcon (
  3426.     char      **psz)
  3427. {    
  3428.     int                     i; 
  3429.     int                     j,k,l,n,c;
  3430.     int                     h,w,nc,ww;
  3431.     //char                    buff[256];
  3432.     int                     a[256][256];
  3433.     int                     m[256][256];
  3434.     int                     color[256];
  3435.     char *                  show[]=
  3436.     {"  ",". "," .","+,",", ",",+","'+","aa","^^","+!","!!","#$","!/","@$","$$","##"};
  3437.     int                     size; //num
  3438.     char                    *ptr, *pmax;
  3439.     unsigned char           r,g,b;
  3440.  
  3441.     ptr  = *psz;
  3442.     size = *(int *)ptr;
  3443.     pmax = ptr+size+4;
  3444.  
  3445.     n    = *(int *)(ptr+4);
  3446.     h    = *(int *)(ptr+12); h/=2;
  3447.     w    = *(int *)(ptr+ 8); 
  3448.     l    = *(short *)(ptr + 18);
  3449.     nc   = 1; for(i=0;i<l;i++)nc*=2;
  3450.     ww   = (w+(8/l)-1)/(8/l); ww=(ww+3)/4; ww=ww*4;
  3451.  
  3452.     //fprintf(stderr,"\nprintIcon h=%d w=%d nc=%d ",h,w,nc);getch();
  3453.  
  3454.     if(h>0 && h<256)
  3455.     {
  3456.     
  3457.         ptr += (n+4);
  3458.         for (i=0;i<nc;i++)
  3459.         {
  3460.             r=*ptr++;g=*ptr++;b=*ptr++;ptr++;
  3461.             c=r+g+b;
  3462.                  if (c==0) color[i]=15;
  3463.             else if (c<=128)
  3464.             {
  3465.                 if(r>g&&r>b) color[i]=11;else
  3466.                 if(g>r&&g>b) color[i]=13;else color[i]=14;
  3467.             }
  3468.             else if (c==255)
  3469.             {
  3470.                 if(r>g&&r>b) color[i]=3;else
  3471.                 if(g>r&&g>b) color[i]=5;else color[i]=6;
  3472.             }
  3473.             else if (c==256)
  3474.             {
  3475.                 if(r<g&&r<b) color[i]=12;else
  3476.                 if(g<r&&g<b) color[i]=10;else color[i]=9;
  3477.             }
  3478.             else if (c<=384) color[i]=7;
  3479.             else if (c<=510)
  3480.             {    
  3481.                 if(r<g&&r<b) color[i]=4;else
  3482.                 if(g<r&&g<b) color[i]=2;else color[i]=1;
  3483.             }
  3484.             else if (c<=576) color[i]=8;
  3485.             else             color[i]=0;
  3486.         }
  3487.         for (i=0;i<h;i++)
  3488.         {
  3489.             for (j=0;j<ww;j++)
  3490.             {
  3491.                 b=*ptr++;
  3492.                 if (l==8) {
  3493.                 a[h-1-i][j]  = b;
  3494.                           }
  3495.                 else if (l==4) {
  3496.                 a[h-1-i][j+j]  = b / 16;
  3497.                 a[h-1-i][j+j+1]= b % 16;
  3498.                           }
  3499.                 else if(l==2)
  3500.                           {
  3501.                 for (k=0;k<4;k++) 
  3502.                 {
  3503.                     r=(b>>(6-2*k))&0x03;
  3504.                     if      (r==0x03) a[h-1-i][4*j+k]=15; 
  3505.                     else if (r==0x02) a[h-1-i][4*j+k]=10;
  3506.                     else if (r==0x01) a[h-1-i][4*j+k]=5;
  3507.                     else              a[h-1-i][4*j+k]=0;
  3508.                 }
  3509.                           }
  3510.                 else if(l==1)
  3511.                           {
  3512.                 for (k=0;k<8;k++) 
  3513.                 {
  3514.                     if ((b>>(7-k))&0x01)
  3515.                     a[h-1-i][8*j+k]=15; else a[h-1-i][8*j+k]=0;
  3516.                 }
  3517.                           }
  3518.                 else a[i][j]=0;
  3519.             }
  3520.         }
  3521.         for (i=0;i<h;i++)
  3522.         {
  3523.             for (j=0;j<w/8;j++)
  3524.             {
  3525.                 b=*ptr++;
  3526.                 for (k=0;k<8;k++) 
  3527.                 {
  3528.                     if ((b>>(7-k))&0x01)
  3529.                     m[h-1-i][8*j+k]=1; else m[h-1-i][8*j+k]=0;
  3530.                 }
  3531.             }
  3532.         }
  3533.     }
  3534.  
  3535.     printf("   height=%3d  width=%3d  # of bits=%3d ", h, w, l);
  3536.     if (moreprint)
  3537.     {
  3538.         printf("\n\n");
  3539.         for (i=0;i<h;i++)
  3540.         {
  3541.             for (j=0;j<w;j++)
  3542.             {
  3543.                 if (color[a[i][j]]<15) printf("%s",show[color[a[i][j]]]);
  3544.                 else if (m[i][j])      printf("#~"); else printf("##");
  3545.             }
  3546.             printf("\n");
  3547.         }
  3548.     }
  3549.  
  3550.     *psz=pmax;
  3551.     return 1;
  3552. }
  3553.  
  3554. //
  3555. // This function is written by sang cho
  3556. //                                                         September 10, 1998
  3557.  
  3558. /* scan bitmap and copy bitmap */
  3559. int     WINAPI GetContentsOfBitmap (
  3560.     LPVOID    lpFile,
  3561.     char      **pszResTypes)
  3562. {
  3563.     PIMAGE_RESOURCE_DIRECTORY          prdType, prdName, prdLanguage;
  3564.     PIMAGE_RESOURCE_DIRECTORY_ENTRY    prde, prde1;
  3565.     PIMAGE_RESOURCE_DIR_STRING_U       pBitmapName;
  3566.     PIMAGE_RESOURCE_DATA_ENTRY         prData;
  3567.     PWORD                              pBitmapHeader;
  3568.     char                    buff[256];
  3569.     int                     i,j; 
  3570.     int                     size;
  3571.     int                     sLength, nBitmaps;
  3572.     WORD                   *pwd;
  3573.     char                   *pMem; 
  3574.  
  3575.     /* get root directory of resource tree */
  3576.     if ((prdType = (PIMAGE_RESOURCE_DIRECTORY)ImageDirectoryOffset
  3577.             (lpFile, IMAGE_DIRECTORY_ENTRY_RESOURCE)) == NULL)
  3578.     return 0;
  3579.  
  3580.     /* set pointer to first resource type entry */
  3581.     prde = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)
  3582.            ((DWORD)prdType + sizeof (IMAGE_RESOURCE_DIRECTORY)
  3583.            +prdType->NumberOfNamedEntries*8);
  3584.  
  3585.     for (i=0; i<prdType->NumberOfIdEntries; i++)
  3586.     {
  3587.     if (prde->Name == RT_BITMAP) break;
  3588.     prde++;
  3589.     }
  3590.     if (prde->Name != RT_BITMAP) return 0; 
  3591.  
  3592.     prdName = (PIMAGE_RESOURCE_DIRECTORY)
  3593.           ((DWORD)prdType + (prde->OffsetToData ^ 0x80000000) ); 
  3594.     if (prdName == NULL) return 0;
  3595.  
  3596.     prde = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)
  3597.            ((DWORD)prdName + sizeof (IMAGE_RESOURCE_DIRECTORY));
  3598.  
  3599.  
  3600.     nBitmaps=prdName->NumberOfNamedEntries + prdName->NumberOfIdEntries;
  3601.     sLength=0;
  3602.     
  3603.     for (i=0; i<prdName->NumberOfNamedEntries; i++)
  3604.     {
  3605.         pBitmapName = (PIMAGE_RESOURCE_DIR_STRING_U) 
  3606.               ((DWORD)prdType + (prde->Name ^ 0x80000000));
  3607.         sLength += pBitmapName->Length + 1;
  3608.         
  3609.         prdLanguage = (PIMAGE_RESOURCE_DIRECTORY)
  3610.               ((DWORD)prdType + (prde->OffsetToData ^ 0x80000000));
  3611.         if (prdLanguage == NULL) continue;
  3612.         
  3613.         prde1 = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)
  3614.             ((DWORD)prdLanguage + sizeof (IMAGE_RESOURCE_DIRECTORY)
  3615.             +prdLanguage->NumberOfNamedEntries*8);
  3616.         
  3617.         prData = (PIMAGE_RESOURCE_DATA_ENTRY)
  3618.              ((DWORD)prdType + prde1->OffsetToData);
  3619.         if (prData == NULL) continue;
  3620.         
  3621.         size = prData->Size; 
  3622.         sLength += 4+size;       
  3623.         prde++;
  3624.     }
  3625.     for (i=0; i<prdName->NumberOfIdEntries; i++)
  3626.     {
  3627.         sLength += 14;
  3628.         
  3629.         prdLanguage = (PIMAGE_RESOURCE_DIRECTORY)
  3630.               ((DWORD)prdType + (prde->OffsetToData ^ 0x80000000));
  3631.         if (prdLanguage == NULL) continue;
  3632.         
  3633.         prde1 = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)
  3634.             ((DWORD)prdLanguage + sizeof (IMAGE_RESOURCE_DIRECTORY)
  3635.             +prdLanguage->NumberOfNamedEntries*8);
  3636.         
  3637.         prData = (PIMAGE_RESOURCE_DATA_ENTRY)
  3638.              ((DWORD)prdType + prde1->OffsetToData);
  3639.         if (prData == NULL) continue;
  3640.         
  3641.         size = prData->Size; 
  3642.         sLength += 4+size;       
  3643.         prde++;
  3644.     }
  3645.     //
  3646.     // allocate memory for bitmap names
  3647.     //
  3648.     *pszResTypes = (char *)calloc (sLength, 1);
  3649.  
  3650.     pMem = *pszResTypes;
  3651.     //
  3652.     // and start all over again
  3653.     //
  3654.     prde = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)
  3655.            ((DWORD)prdName + sizeof (IMAGE_RESOURCE_DIRECTORY));
  3656.  
  3657.     for (i=0; i<prdName->NumberOfNamedEntries; i++)
  3658.     {
  3659.         pBitmapName = (PIMAGE_RESOURCE_DIR_STRING_U) 
  3660.               ((DWORD)prdType + (prde->Name ^ 0x80000000));
  3661.         
  3662.         
  3663.         for (j=0; j<pBitmapName->Length; j++)
  3664.             *pMem++ = (char)(pBitmapName->NameString[j]);
  3665.         *pMem = 0;
  3666.         pMem++;
  3667.         
  3668.  
  3669.         prdLanguage = (PIMAGE_RESOURCE_DIRECTORY)
  3670.               ((DWORD)prdType + (prde->OffsetToData ^ 0x80000000));
  3671.         if (prdLanguage == NULL) continue;
  3672.         
  3673.         prde1 = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)
  3674.             ((DWORD)prdLanguage + sizeof (IMAGE_RESOURCE_DIRECTORY)
  3675.             +prdLanguage->NumberOfNamedEntries*8);
  3676.         
  3677.         prData = (PIMAGE_RESOURCE_DATA_ENTRY)
  3678.              ((DWORD)prdType + prde1->OffsetToData);
  3679.         if (prData == NULL) continue;
  3680.         
  3681.         pBitmapHeader =  (PWORD)
  3682.                  GetActualAddress (lpFile, prData->OffsetToData);
  3683.      
  3684.         
  3685.         
  3686.         pwd = pBitmapHeader;
  3687.         size = prData->Size;
  3688.         *(int *)pMem = size;          pMem += 4;
  3689.         StrangeMenuFill (&pMem, &pwd, size);
  3690.         
  3691.         prde++;
  3692.     }
  3693.     for (i=0; i<prdName->NumberOfIdEntries; i++)
  3694.     {
  3695.         
  3696.         sprintf (buff, "BitmapId_%04X", (int)(prde->Name));
  3697.         strcpy (pMem, buff);
  3698.         pMem += strlen (buff) + 1;
  3699.  
  3700.         prdLanguage = (PIMAGE_RESOURCE_DIRECTORY)
  3701.               ((DWORD)prdType + (prde->OffsetToData ^ 0x80000000));
  3702.         if (prdLanguage == NULL) 
  3703.         {   printf ("\nprdLanguage = NULL"); exit (0); }
  3704.         
  3705.         prde1 = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)
  3706.             ((DWORD)prdLanguage + sizeof (IMAGE_RESOURCE_DIRECTORY)
  3707.             +prdLanguage->NumberOfNamedEntries*8);
  3708.         
  3709.         prData = (PIMAGE_RESOURCE_DATA_ENTRY)
  3710.              ((DWORD)prdType + prde1->OffsetToData);
  3711.         if (prData == NULL) 
  3712.         {   printf ("\nprData = NULL"); exit (0); }
  3713.  
  3714.         pBitmapHeader =  (PWORD)
  3715.                  GetActualAddress (lpFile, prData->OffsetToData);
  3716.         
  3717.         
  3718.         pwd = pBitmapHeader;
  3719.         size = prData->Size;
  3720.         *(int *)pMem = size;          pMem += 4;
  3721.         StrangeMenuFill (&pMem, &pwd, size);
  3722.         
  3723.         prde++;
  3724.     }
  3725.  
  3726.     return nBitmaps;
  3727. }
  3728.  
  3729. //
  3730. // This function is written by sang cho
  3731. //                                                         September 10, 1998
  3732.  
  3733. /* print contents of bitmap */
  3734. int     WINAPI PrintBitmap (
  3735.     char      **psz)
  3736. {    
  3737.     int                     i;
  3738.     int                     noprint=0;
  3739.     int                     j,k,l,n,c,e;
  3740.     int                     h,w,nc,ww;
  3741.     //char                    buff[256];
  3742.     int                     a[512][512];
  3743.     int                     color[256];
  3744.     char *                  show[]=
  3745.     {"  ",". "," .","+,",", ",",+","'+","aa","^^","+!","!!","#$","!/","@$","$$","##"};
  3746.     int                     size; //num
  3747.     char                    *ptr, *pmax;
  3748.     unsigned char           r,g,b;
  3749.  
  3750.     ptr  = *psz;
  3751.     size = *(int *)ptr;
  3752.     pmax = ptr+size+4;
  3753.  
  3754.     n    = *(int *)(ptr+4);
  3755.     if (n==12) // strange form
  3756.     {
  3757.         h=*(short*)(ptr+10);
  3758.         w=*(short*)(ptr+8);
  3759.         l=*(short*)(ptr+12);
  3760.         nc=1; for(i=0;i<l;i++)nc*=2;
  3761.     }
  3762.     else
  3763.     {
  3764.         h    = *(int *)(ptr+12); 
  3765.         w    = *(int *)(ptr+ 8); 
  3766.         l    = *(short *)(ptr + 18);
  3767.         e    = *(int *)(ptr+20);
  3768.         nc   = 1; for(i=0;i<l;i++)nc*=2;
  3769.     }
  3770.     if (0<l && l<=8) ww   = (w+(8/l)-1)/(8/l); else ww=ww*4;
  3771.     ww=(ww+3)/4; ww=ww*4;
  3772.  
  3773.     //fprintf(stderr,"\nprintBitmap h=%d w=%d nc=%d size=%d",h,w,nc,size);getch();
  3774.  
  3775.     if(h>0 && h<512 && l<=8 && 12<n)
  3776.     {
  3777.     
  3778.         ptr += (n+4);
  3779.         for (i=0;i<nc;i++)
  3780.         {
  3781.             r=*ptr++;g=*ptr++;b=*ptr++;ptr++;
  3782.             c=r+g+b;
  3783.                  if (c==0) color[i]=15;
  3784.             else if (c<=128)
  3785.             {
  3786.                 if(r>g&&r>b) color[i]=11;else
  3787.                 if(g>r&&g>b) color[i]=13;else color[i]=14;
  3788.             }
  3789.             else if (c==255)
  3790.             {
  3791.                 if(r>g&&r>b) color[i]=3;else
  3792.                 if(g>r&&g>b) color[i]=5;else color[i]=6;
  3793.             }
  3794.             else if (c==256)
  3795.             {
  3796.                 if(r<g&&r<b) color[i]=12;else
  3797.                 if(g<r&&g<b) color[i]=10;else color[i]=9;
  3798.             }
  3799.             else if (c<=384) color[i]=7;
  3800.             else if (c<=510)
  3801.             {    
  3802.                 if(r<g&&r<b) color[i]=4;else
  3803.                 if(g<r&&g<b) color[i]=2;else color[i]=1;
  3804.             }
  3805.             else if (c<=576) color[i]=8;
  3806.             else             color[i]=0;
  3807.         }
  3808.         if (e==0)
  3809.         {
  3810.             for (i=0;i<h;i++)
  3811.             {
  3812.                 for (j=0;j<ww;j++)
  3813.                 {
  3814.                     b=*ptr++;
  3815.                     if (l==8) 
  3816.                     {
  3817.                         a[h-1-i][j]  = b;
  3818.                     }
  3819.                     else if (l==4) 
  3820.                     {
  3821.                         a[h-1-i][j+j]  = b / 16;
  3822.                         a[h-1-i][j+j+1]= b % 16;
  3823.                     }
  3824.                     else if(l==2)
  3825.                     {
  3826.                         for (k=0;k<4;k++) 
  3827.                         {
  3828.                             r=(b>>(6-2*k))&0x03;
  3829.                             if      (r==0x03) a[h-1-i][4*j+k]=15; 
  3830.                             else if (r==0x02) a[h-1-i][4*j+k]=10;
  3831.                             else if (r==0x01) a[h-1-i][4*j+k]=5;
  3832.                             else              a[h-1-i][4*j+k]=0;
  3833.                         }
  3834.                     }
  3835.                     else if(l==1)
  3836.                     {
  3837.                         for (k=0;k<8;k++) 
  3838.                         {
  3839.                             if ((b>>(7-k))&0x01)
  3840.                             a[h-1-i][8*j+k]=15; else a[h-1-i][8*j+k]=0;
  3841.                         }
  3842.                     }
  3843.                     else a[h-1-i][j]=0;
  3844.                 }
  3845.             }
  3846.         }
  3847.         else
  3848.         {
  3849.             if (l==8)
  3850.             {
  3851.                 i=h-1;j=0;
  3852.                 while(ptr<pmax)
  3853.                 {
  3854.                     r=*ptr++; g=*ptr++;
  3855.                     if(r>0)                       // repeat mode (normal)
  3856.                     {
  3857.                         for(k=0;k<r;k++) 
  3858.                         {
  3859.                             a[i][j++]=g; 
  3860.                         }
  3861.                     }
  3862.                     else
  3863.                     {
  3864.                         if(g==0) {i--;j=0;}        // end of line
  3865.                         else if (g==1) break;    // end of bitmap
  3866.                         else if (g==2)             // delta
  3867.                         {
  3868.                             r=*ptr++; g=*ptr++;
  3869.                             j+=r; i-=g;
  3870.                         }
  3871.                         else                    // absolute mode
  3872.                         {
  3873.                             for(k=0;k<g;k++) 
  3874.                             {
  3875.                                 r=*ptr++; b=*ptr++;
  3876.                                 a[i][j++]=r; 
  3877.                                 k++; if (k>=g) break;
  3878.                                 a[i][j++]=b;
  3879.                             }
  3880.                         }
  3881.                     }
  3882.                 }
  3883.             }
  3884.             else if (l==4)
  3885.             {
  3886.                 i=h-1;j=0;
  3887.                 while(ptr<pmax)
  3888.                 {
  3889.                     r=*ptr++; g=*ptr++;
  3890.                     if(r>0)                       // repeat mode (normal)
  3891.                     {
  3892.                         for(k=0;k<r;k++) 
  3893.                         {
  3894.                             a[i][j++]=g/16; 
  3895.                             k++; if (k>=r) break;
  3896.                             a[i][j++]=g%16;    
  3897.                         }
  3898.                     }
  3899.                     else
  3900.                     {
  3901.                         if(g==0) {i--;j=0;}        // end of line
  3902.                         else if (g==1) break;    // end of bitmap
  3903.                         else if (g==2)             // delta
  3904.                         {
  3905.                             r=*ptr++; g=*ptr++;
  3906.                             j+=r; i-=g;
  3907.                         }
  3908.                         else                    // absolute mode
  3909.                         {
  3910.                             for(k=0;k<g;k++) 
  3911.                             {
  3912.                                 r=*ptr++; b=*ptr++;
  3913.                                 a[i][j++]=r/16; 
  3914.                                 k++; if (k>=g) break;
  3915.                                 a[i][j++]=r%16;
  3916.                                 k++; if (k>=g) break;
  3917.                                 a[i][j++]=b/16; 
  3918.                                 k++; if (k>=g) break;
  3919.                                 a[i][j++]=b%16;
  3920.                             }
  3921.                         }
  3922.                     }
  3923.                 }
  3924.             }
  3925.             else noprint=1;
  3926.         }    
  3927.     }
  3928.     else noprint=1;
  3929.     
  3930.     printf("   height=%3d  width=%3d  # of bits=%3d ", h, w, l);
  3931.     if (moreprint && !noprint)
  3932.     {
  3933.         printf("\n\n");
  3934.         for (i=0;i<h;i++)
  3935.         {
  3936.             for (j=0;j<w;j++)
  3937.             {
  3938.                 printf("%s",show[color[a[i][j]]]);
  3939.             }
  3940.             printf("\n");
  3941.         }
  3942.     }
  3943.  
  3944.     *psz=pmax;
  3945.     return 1;
  3946. }
  3947.  
  3948.  
  3949. //
  3950. // This function is written by sang cho
  3951. //                                                         September 9, 1998
  3952.  
  3953. /* scan cursor and copy cursor */
  3954. int     WINAPI GetContentsOfCursor (
  3955.     LPVOID    lpFile,
  3956.     char      **pszResTypes)
  3957. {
  3958.     PIMAGE_RESOURCE_DIRECTORY          prdType, prdName, prdLanguage;
  3959.     PIMAGE_RESOURCE_DIRECTORY_ENTRY    prde, prde1;
  3960.     PIMAGE_RESOURCE_DIR_STRING_U       pCursorName;
  3961.     PIMAGE_RESOURCE_DATA_ENTRY         prData;
  3962.     PWORD                              pCursorHeader;
  3963.     char                    buff[256];
  3964.     int                     i,j; 
  3965.     int                     size;
  3966.     int                     sLength, nCursors;
  3967.     WORD                   *pwd;
  3968.     char                   *pMem; 
  3969.  
  3970.     /* get root directory of resource tree */
  3971.     if ((prdType = (PIMAGE_RESOURCE_DIRECTORY)ImageDirectoryOffset
  3972.             (lpFile, IMAGE_DIRECTORY_ENTRY_RESOURCE)) == NULL)
  3973.     return 0;
  3974.  
  3975.     /* set pointer to first resource type entry */
  3976.     prde = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)
  3977.            ((DWORD)prdType + sizeof (IMAGE_RESOURCE_DIRECTORY)
  3978.            +prdType->NumberOfNamedEntries*8);
  3979.  
  3980.     for (i=0; i<prdType->NumberOfIdEntries; i++)
  3981.     {
  3982.         if (prde->Name == RT_CURSOR) break;
  3983.         prde++;
  3984.     }
  3985.     if (prde->Name != RT_CURSOR) return 0; 
  3986.  
  3987.     prdName = (PIMAGE_RESOURCE_DIRECTORY)
  3988.           ((DWORD)prdType + (prde->OffsetToData ^ 0x80000000) ); 
  3989.     if (prdName == NULL) return 0;
  3990.  
  3991.     prde = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)
  3992.            ((DWORD)prdName + sizeof (IMAGE_RESOURCE_DIRECTORY)
  3993.            +prdName->NumberOfNamedEntries*8);
  3994.  
  3995.  
  3996.     nCursors=prdName->NumberOfNamedEntries + prdName->NumberOfIdEntries;
  3997.     sLength=0;
  3998.     
  3999.     for (i=0; i<prdName->NumberOfNamedEntries; i++)
  4000.     {
  4001.         pCursorName = (PIMAGE_RESOURCE_DIR_STRING_U) 
  4002.               ((DWORD)prdType + (prde->Name ^ 0x80000000));
  4003.         sLength += pCursorName->Length + 1;
  4004.         
  4005.         prdLanguage = (PIMAGE_RESOURCE_DIRECTORY)
  4006.               ((DWORD)prdType + (prde->OffsetToData ^ 0x80000000));
  4007.         if (prdLanguage == NULL) continue;
  4008.         
  4009.         prde1 = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)
  4010.             ((DWORD)prdLanguage + sizeof (IMAGE_RESOURCE_DIRECTORY)
  4011.             +prdLanguage->NumberOfNamedEntries*8);
  4012.         
  4013.         prData = (PIMAGE_RESOURCE_DATA_ENTRY)
  4014.              ((DWORD)prdType + prde1->OffsetToData);
  4015.         if (prData == NULL) continue;
  4016.         
  4017.         size = prData->Size; 
  4018.         sLength += 4+size;       
  4019.         prde++;
  4020.     }
  4021.     
  4022.     for (i=0; i<prdName->NumberOfIdEntries; i++)
  4023.     {
  4024.         sLength += 14;
  4025.         
  4026.         prdLanguage = (PIMAGE_RESOURCE_DIRECTORY)
  4027.               ((DWORD)prdType + (prde->OffsetToData ^ 0x80000000));
  4028.         if (prdLanguage == NULL) continue;
  4029.         
  4030.         prde1 = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)
  4031.             ((DWORD)prdLanguage + sizeof (IMAGE_RESOURCE_DIRECTORY)
  4032.             +prdLanguage->NumberOfNamedEntries*8);
  4033.         
  4034.         prData = (PIMAGE_RESOURCE_DATA_ENTRY)
  4035.              ((DWORD)prdType + prde1->OffsetToData);
  4036.         if (prData == NULL) continue;
  4037.         
  4038.         size = prData->Size; 
  4039.         sLength += 4+size;       
  4040.         prde++;
  4041.     }
  4042.     
  4043.     //
  4044.     // allocate memory for cursor names
  4045.     //
  4046.     *pszResTypes = (char *)calloc (sLength, 1);
  4047.  
  4048.     pMem = *pszResTypes;
  4049.     //
  4050.     // and start all over again
  4051.     //
  4052.     prde = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)
  4053.            ((DWORD)prdName + sizeof (IMAGE_RESOURCE_DIRECTORY)
  4054.            +prdName->NumberOfNamedEntries*8);
  4055.  
  4056.     for (i=0; i<prdName->NumberOfNamedEntries; i++)
  4057.     {
  4058.         pCursorName = (PIMAGE_RESOURCE_DIR_STRING_U) 
  4059.               ((DWORD)prdType + (prde->Name ^ 0x80000000));
  4060.         
  4061.         
  4062.         for (j=0; j<pCursorName->Length; j++)
  4063.             *pMem++ = (char)(pCursorName->NameString[j]);
  4064.         *pMem = 0;
  4065.         pMem++;
  4066.         
  4067.  
  4068.         prdLanguage = (PIMAGE_RESOURCE_DIRECTORY)
  4069.               ((DWORD)prdType + (prde->OffsetToData ^ 0x80000000));
  4070.         if (prdLanguage == NULL) continue;
  4071.         
  4072.         prde1 = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)
  4073.             ((DWORD)prdLanguage + sizeof (IMAGE_RESOURCE_DIRECTORY)
  4074.             +prdLanguage->NumberOfNamedEntries*8);
  4075.         
  4076.         prData = (PIMAGE_RESOURCE_DATA_ENTRY)
  4077.              ((DWORD)prdType + prde1->OffsetToData);
  4078.         if (prData == NULL) continue;
  4079.         
  4080.         pCursorHeader =  (PWORD)
  4081.                  GetActualAddress (lpFile, prData->OffsetToData);
  4082.      
  4083.         
  4084.         
  4085.         pwd = pCursorHeader;
  4086.         size = prData->Size;
  4087.         *(int *)pMem = size;          pMem += 4;
  4088.         StrangeMenuFill (&pMem, &pwd, size);
  4089.         
  4090.         prde++;
  4091.     }
  4092.     
  4093.     for (i=0; i<prdName->NumberOfIdEntries; i++)
  4094.     {
  4095.         
  4096.         sprintf (buff, "CursorId_%04X", (int)(prde->Name));
  4097.         strcpy (pMem, buff);
  4098.         pMem += strlen (buff) + 1;
  4099.  
  4100.         prdLanguage = (PIMAGE_RESOURCE_DIRECTORY)
  4101.               ((DWORD)prdType + (prde->OffsetToData ^ 0x80000000));
  4102.         if (prdLanguage == NULL) 
  4103.         {   printf ("\nprdLanguage = NULL"); exit (0); }
  4104.         
  4105.         prde1 = (PIMAGE_RESOURCE_DIRECTORY_ENTRY)
  4106.             ((DWORD)prdLanguage + sizeof (IMAGE_RESOURCE_DIRECTORY)
  4107.             +prdLanguage->NumberOfNamedEntries*8);
  4108.         
  4109.         prData = (PIMAGE_RESOURCE_DATA_ENTRY)
  4110.              ((DWORD)prdType + prde1->OffsetToData);
  4111.         if (prData == NULL) 
  4112.         {   printf ("\nprData = NULL"); exit (0); }
  4113.  
  4114.         pCursorHeader =  (PWORD)
  4115.                  GetActualAddress (lpFile, prData->OffsetToData);
  4116.         
  4117.         
  4118.         pwd = pCursorHeader;
  4119.         size = prData->Size;
  4120.         *(int *)pMem = size;          pMem += 4;
  4121.         StrangeMenuFill (&pMem, &pwd, size);
  4122.         
  4123.         prde++;
  4124.     }
  4125.     return nCursors;
  4126. }
  4127.  
  4128. //
  4129. // This function is written by sang cho
  4130. //                                                         September 9, 1998
  4131.  
  4132. /* print contents of cursor */
  4133. int     WINAPI PrintCursor (
  4134.     char      **psz)
  4135. {    
  4136.     int                     i; 
  4137.     int                     j,k,l,n,c;
  4138.     int                     h,w,nc,ww;
  4139.     int                     x,y;
  4140.     //char                    buff[256];
  4141.     int                     a[256][256];
  4142.     int                     m[256][256];
  4143.     int                     color[256];
  4144.     char *                  show[]=
  4145.     {"  ",". "," .","+,",", ",",+","'+","aa","^^","+!","!!","#$","!/","@$","$$","##"};
  4146.     int                     size; //num
  4147.     char                    *ptr, *pmax;
  4148.     unsigned char           r,g,b;
  4149.  
  4150.     /*
  4151.     ptr = *psz;
  4152.     printf("\n");
  4153.     size = *(int *)ptr;
  4154.     pmax = ptr+size+4;
  4155.  
  4156.     for (i=0; i<(size/16)+1; i++)
  4157.     {
  4158.         n = 0;
  4159.         for (j=0; j<16; j++)
  4160.         {
  4161.             c = (int)(*ptr);
  4162.             if (c<0) c+=256;
  4163.             buff[j] = c;
  4164.             printf ("%02X",c);
  4165.             ptr++; 
  4166.             if (ptr >= pmax) break;
  4167.             n++;
  4168.             if (n%4 == 0) printf (" "); 
  4169.         }
  4170.         n++; if (n%4 == 0) printf (" ");
  4171.         l = j;
  4172.         j++;
  4173.         for (; j<16; j++) 
  4174.         { n++; if (n%4 == 0) printf ("   "); else printf ("  "); }
  4175.         printf ("   ");
  4176.         for (k=0; k<l; k++)
  4177.             if (isprint(c=buff[k])) printf("%c", c); else printf(".");
  4178.         printf ("\n");
  4179.         if (ptr >= pmax) break;
  4180.     }
  4181.     *psz=pmax;
  4182.     return 1;*/
  4183.     
  4184.     ptr  = *psz;
  4185.     size = *(int *)ptr;
  4186.     pmax = ptr+size+4;
  4187.  
  4188.     x    = *(short*)(ptr+4);
  4189.     y    = *(short*)(ptr+6);ptr+=4;
  4190.     n    = *(int *)(ptr+4);
  4191.     h    = *(int *)(ptr+12); h/=2;
  4192.     w    = *(int *)(ptr+ 8); 
  4193.     l    = *(short *)(ptr + 18);
  4194.     nc   = 1; for(i=0;i<l;i++)nc*=2;
  4195.     ww   = (w+(8/l)-1)/(8/l); ww=(ww+3)/4; ww=ww*4;
  4196.  
  4197.     //fprintf(stderr,"\nprintCursor h=%d w=%d nc=%d ",h,w,nc);getch();
  4198.  
  4199.     if(h>0 && h<256)
  4200.     {
  4201.     
  4202.         ptr += (n+4);
  4203.         for (i=0;i<nc;i++)
  4204.         {
  4205.             r=*ptr++;g=*ptr++;b=*ptr++;ptr++;
  4206.             c=r+g+b;
  4207.                  if (c==0) color[i]=15;
  4208.             else if (c<=128)
  4209.             {
  4210.                 if(r>g&&r>b) color[i]=11;else
  4211.                 if(g>r&&g>b) color[i]=13;else color[i]=14;
  4212.             }
  4213.             else if (c==255)
  4214.             {
  4215.                 if(r>g&&r>b) color[i]=3;else
  4216.                 if(g>r&&g>b) color[i]=5;else color[i]=6;
  4217.             }
  4218.             else if (c==256)
  4219.             {
  4220.                 if(r<g&&r<b) color[i]=12;else
  4221.                 if(g<r&&g<b) color[i]=10;else color[i]=9;
  4222.             }
  4223.             else if (c<=384) color[i]=7;
  4224.             else if (c<=510)
  4225.             {    
  4226.                 if(r<g&&r<b) color[i]=4;else
  4227.                 if(g<r&&g<b) color[i]=2;else color[i]=1;
  4228.             }
  4229.             else if (c<=576) color[i]=8;
  4230.             else             color[i]=0;
  4231.         }
  4232.         for (i=0;i<h;i++)
  4233.         {
  4234.             for (j=0;j<ww;j++)
  4235.             {
  4236.                 b=*ptr++;
  4237.                 if (l==8) {
  4238.                 a[h-1-i][j]  = b;
  4239.                           }
  4240.                 else if (l==4) 
  4241.                           {
  4242.                 a[h-1-i][j+j]  = b / 16;
  4243.                 a[h-1-i][j+j+1]= b % 16;
  4244.                           }
  4245.                 else if(l==2)
  4246.                           {
  4247.                 for (k=0;k<4;k++) 
  4248.                 {
  4249.                     r=(b>>(6-2*k))&0x03;
  4250.                     if      (r==0x03) a[h-1-i][4*j+k]=15; 
  4251.                     else if (r==0x02) a[h-1-i][4*j+k]=10;
  4252.                     else if (r==0x01) a[h-1-i][4*j+k]=5;
  4253.                     else              a[h-1-i][4*j+k]=0;
  4254.                 }
  4255.                           }
  4256.                 else if(l==1)
  4257.                           {
  4258.                 for (k=0;k<8;k++) 
  4259.                 {
  4260.                     if ((b>>(7-k))&0x01)
  4261.                     a[h-1-i][8*j+k]=15; else a[h-1-i][8*j+k]=0;
  4262.                 }
  4263.                           }
  4264.                 else a[i][j]=0;
  4265.             }
  4266.         }
  4267.         for (i=0;i<h;i++)
  4268.         {
  4269.             for (j=0;j<w/8;j++)
  4270.             {
  4271.                 b=*ptr++;
  4272.                 for (k=0;k<8;k++) 
  4273.                 {
  4274.                     if ((b>>(7-k))&0x01)
  4275.                     m[h-1-i][8*j+k]=1; else m[h-1-i][8*j+k]=0;
  4276.                 }
  4277.             }
  4278.         }
  4279.     }
  4280.     printf("   height=%3d  width=%3d  # of bits=%3d (hotx=%d,hoty=%d)", 
  4281.                h, w, l, x, y);
  4282.     if (moreprint)
  4283.     {
  4284.         printf("\n\n");
  4285.         for (i=0;i<h;i++)
  4286.         {
  4287.             for (j=0;j<w;j++)
  4288.             {
  4289.                 if (color[a[i][j]]<15) printf("%s",show[color[a[i][j]]]);
  4290.                 else if (m[i][j])      printf(".."); else printf("##");
  4291.             }
  4292.             printf("\n");
  4293.         }
  4294.     }
  4295.     
  4296.     *psz=pmax;
  4297.     return 1;
  4298. }
  4299.  
  4300.  
  4301.  
  4302. /* function indicates whether debug  info has been stripped from file */
  4303. BOOL    WINAPI IsDebugInfoStripped (
  4304.     LPVOID    lpFile)
  4305. {
  4306.     PIMAGE_FILE_HEADER    pfh;
  4307.  
  4308.     pfh = (PIMAGE_FILE_HEADER)PEFHDROFFSET (lpFile);
  4309.  
  4310.     return (pfh->Characteristics & IMAGE_FILE_DEBUG_STRIPPED);
  4311. }
  4312.  
  4313. /* retrieve the module name from the debug misc. structure */
  4314. int    WINAPI RetrieveModuleName (
  4315.     LPVOID    lpFile,
  4316.     char      **pszModule)
  4317. {
  4318.  
  4319.     PIMAGE_DEBUG_DIRECTORY    pdd;
  4320.     PIMAGE_DEBUG_MISC         pdm = NULL;
  4321.     int                       nCnt;
  4322.  
  4323.     if (!(pdd = (PIMAGE_DEBUG_DIRECTORY)ImageDirectoryOffset (lpFile, IMAGE_DIRECTORY_ENTRY_DEBUG)))
  4324.     return 0;
  4325.  
  4326.     while (pdd->SizeOfData)
  4327.     {
  4328.     if (pdd->Type == IMAGE_DEBUG_TYPE_MISC)
  4329.         {
  4330.         pdm = (PIMAGE_DEBUG_MISC)((DWORD)pdd->PointerToRawData + (DWORD)lpFile);
  4331.         *pszModule = (char *)calloc ((nCnt = (strlen (pdm->Data)))+1, 1);
  4332.         // may need some unicode business here...above
  4333.         memcpy (*pszModule, pdm->Data, nCnt);
  4334.  
  4335.         break;
  4336.         }
  4337.  
  4338.     pdd ++;
  4339.     }
  4340.  
  4341.     if (pdm != NULL)
  4342.     return nCnt;
  4343.     else
  4344.     return 0;
  4345. }
  4346.  
  4347. /* determine if this is a valid debug file */
  4348. BOOL    WINAPI IsDebugFile (
  4349.     LPVOID    lpFile)
  4350. {
  4351.     PIMAGE_SEPARATE_DEBUG_HEADER    psdh;
  4352.  
  4353.     psdh = (PIMAGE_SEPARATE_DEBUG_HEADER)lpFile;
  4354.  
  4355.     return (psdh->Signature == IMAGE_SEPARATE_DEBUG_SIGNATURE);
  4356. }
  4357.  
  4358. /* copy separate debug header structure from debug file */
  4359. BOOL    WINAPI GetSeparateDebugHeader (
  4360.     LPVOID                          lpFile,
  4361.     PIMAGE_SEPARATE_DEBUG_HEADER    psdh)
  4362. {
  4363.     PIMAGE_SEPARATE_DEBUG_HEADER    pdh;
  4364.  
  4365.     pdh = (PIMAGE_SEPARATE_DEBUG_HEADER)lpFile;
  4366.  
  4367.     if (pdh->Signature == IMAGE_SEPARATE_DEBUG_SIGNATURE)
  4368.     {
  4369.     memcpy ((LPVOID)psdh, (LPVOID)pdh, sizeof (IMAGE_SEPARATE_DEBUG_HEADER));
  4370.     return TRUE;
  4371.     }
  4372.  
  4373.     return FALSE;
  4374. }
  4375.  
  4376. void printTimeDateStamp(DWORD tds)
  4377. {
  4378. long long tem;
  4379. int      i, iyear, iweek, idate, ihour, isecond;
  4380. int      monthDays[] ={31,28,31,30,31,30,31,31,30,31,30,31};
  4381. char    *monString[] ={"Jan","Feb","Mar","Apr","May","Jun",  
  4382.                        "Jul","Aug","Sep","Oct","Nov","Dec"};
  4383. char    *weekString[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
  4384.  
  4385.     // I don't have documentation about timedatestamp
  4386.     // so my best guess is to run some kind of reference software
  4387.     // Ida and dumppe gives different results 
  4388.     // I don't know which is right but I guess dumppe is right so 
  4389.     // I changed output accordingly.
  4390.     // If you think Ida is right then the following 8 should be 5.
  4391.     // 1998.2.27 sangcho
  4392.     tem  = (long long)tds;
  4393.     ihour=(int)(tem/3600)-8; isecond=(int)(tem%3600);
  4394.     idate=ihour/24; ihour=ihour%24;     iweek=(idate+4)%7;
  4395.     iyear=idate/365; idate=idate-iyear*365;
  4396.     if (idate<(iyear+1)/4) {iyear=iyear-1; idate=idate+365;}
  4397.     idate -= (iyear+1)/4;  
  4398.     if ((iyear+2)%4==0) monthDays[1]+=1;
  4399.     for (i=0;i<11;i++)monthDays[i+1]=monthDays[i]+monthDays[i+1];
  4400.     for (i=0;i<12;i++)if (idate<monthDays[i]) break;
  4401.     if (i>0) idate-=monthDays[i-1];
  4402.     // summer time adjustment i hope this is right.      1998.2.26 sangcho
  4403.     // daylight saving time is from the first sunday 2:00 Am of April to
  4404.     // the last sunday 2:00 Am of October
  4405.          if ((3<i)&&(i<9)) ihour+=1; 
  4406.     else if (i==3)
  4407.     {
  4408.               if (idate>6) ihour+=1;
  4409.          else if (iweek==0){if (ihour>1) ihour+=1;}
  4410.          else if (idate>=iweek) ihour+=1;
  4411.     }
  4412.     else if (i==9)
  4413.     {
  4414.               if (idate<24) ihour+=1;
  4415.          else if (iweek==0){if (ihour<2) ihour+=1;}
  4416.          else if (idate<24+iweek) ihour+=1;
  4417.     }
  4418.     if (ihour>24) {ihour-=24; idate+=1;}
  4419.     if (idate+1>monthDays[i]) {idate-=monthDays[i];i+=1;}
  4420.     printf("T.DateStamp = %08X: ",(int)tds);
  4421.     printf("%s %s %02d %02d:%02d:%02d %4d\n", weekString[iweek], monString[i], 
  4422.             idate+1, ihour, isecond/60, isecond%60, 1970+iyear);
  4423.  
  4424.  
  4425. }
  4426.  
  4427. /* I need to place these data here to keep integrity of codes */
  4428.  
  4429. LPVOID          lpFile;        /* pointer to the contents of the input file */
  4430. LPVOID          lpMap;         /* pointer to the map of codes processed */
  4431. LPVOID          lpMap1;        /* pointer to the map of codes processed */ 
  4432. int             nSections;              // number of sections
  4433. int             nResources;             // number of resources
  4434. int             nMenus;                 // number of menus
  4435. int             nDialogs;               // number of dialogs
  4436. int             nStrings;               // number of strings
  4437. int             nIcons;                 // number of icons
  4438. int             nCursors;               // number of cursors
  4439. int             nBitmaps;               // number of bitmaps
  4440. int             nAccelerators;          // number of accelerators
  4441. int             nImportedModules;       // number of imported modules
  4442. int             nFunctions;                     // number of functions in the imported module
  4443. int             nExportedFunctions;     // number of exported funcions
  4444. DWORD           imageBase;                      // image base of the file
  4445. DWORD           entryPoint;                     // entry point of the file
  4446. DWORD           imagebaseRVA;  /* imagebase + RVA of the code */
  4447. int             CodeOffset;    /* starting point of code   */
  4448. int             CodeSize;      /* size of code             */
  4449. int             vCodeOffset;    /* starting point of code   */
  4450. int             vCodeSize;      /* size of code             */
  4451. int             MapSize;       /* size of code map         */
  4452. DWORD           maxRVA;        /* the largest RVA of sections */
  4453. int             maxRVAsize;    /* size of that section */
  4454. int             moreprint=0;   /* need to print some more */
  4455.  
  4456. char           *piNameBuff;       // import module name buffer
  4457. char           *pfNameBuff;       // import functions in the module name buffer
  4458. char           *peNameBuff;       // export function name buffer
  4459. char           *pmNameBuff;       // menu name buffer
  4460. char           *pdNameBuff;       // dialog name buffer   
  4461. char           *psNameBuff;       // string name buffer
  4462. char           *pcNameBuff;       // cursor name buffer
  4463. char           *pbNameBuff;       // bitmap name buffer
  4464. char           *pnNameBuff;       // icon   name buffer
  4465. char           *paNameBuff;       // accelerator name buffer
  4466. int             piNameBuffSize;   // import module name buffer
  4467. int             pfNameBuffSize;   // import functions in the module name buffer
  4468. int             peNameBuffSize;   // export function name buffer
  4469. int             pmNameBuffSize;   // menu name buffer
  4470. int             pdNameBuffSize;   // dialog name buffer
  4471.  
  4472. //
  4473. // I tried to immitate the output of w32dasm disassembler.
  4474. // which is a pretty good program.
  4475. // but I am disappointed with this program and I myself 
  4476. // am writting a disassembler.
  4477. // This PEdump program is a byproduct of that project.
  4478. // so enjoy this program and I hope we will have a little more
  4479. // knowledge on windows programming world.
  4480. //                                                        .... sang cho
  4481.  
  4482. #define  MAXSECTIONNUMBER 32
  4483. #define  MAXNAMESTRNUMBER 40
  4484.  
  4485.     IMAGE_SECTION_HEADER            shdr [MAXSECTIONNUMBER];
  4486.  
  4487. int pedump (int argc,char **argv)
  4488. {
  4489.     DWORD                           fileType;
  4490.     
  4491.     IMAGE_DOS_HEADER                dosHdr;
  4492.     PIMAGE_FILE_HEADER              pfh;
  4493.     PIMAGE_OPTIONAL_HEADER          poh;
  4494.     PIMAGE_SECTION_HEADER           psh;
  4495.     //IMAGE_SECTION_HEADER            idsh;
  4496.     extern     IMAGE_SECTION_HEADER    shdr[];
  4497.     //PIMAGE_IMPORT_MODULE_DIRECTORY  pid;
  4498.  
  4499.     int         i, j, n;
  4500.     int         CodeSize1;
  4501.     int         c, nexes;
  4502.     DWORD       tds;
  4503.     DWORD       baseofcode,baseofdata;
  4504.  
  4505.     char     *pnstr;
  4506.     char     *pst;
  4507.  
  4508.     //unsigned char          *p, *q;
  4509.     _key_                   k;
  4510.     //PKEY                    pk;
  4511.  
  4512.     GetDosHeader (lpFile, &dosHdr);
  4513.  
  4514.     if (dosHdr.e_magic == IMAGE_DOS_SIGNATURE)
  4515.     {
  4516.         if ((dosHdr.e_lfanew > 4096) || (dosHdr.e_lfanew < 64))
  4517.         {
  4518.     printf ("This file is not PE format ... sorry, it looks like DOS format\n");
  4519.     exit (0);
  4520.         }
  4521.     }
  4522.     else 
  4523.     {
  4524.     printf ("This doesn't look like executable file .. sorry, ...\n");
  4525.     exit (0);
  4526.     }
  4527.  
  4528.     fileType = ImageFileType (lpFile);
  4529.  
  4530.     if (fileType != IMAGE_NT_SIGNATURE) 
  4531.     {
  4532.         printf ("This file is not PE format ... sorry,\n");
  4533.         exit (0);
  4534.     }
  4535.     
  4536.     //=====================================
  4537.     // now we can really start processing
  4538.     //=====================================
  4539.  
  4540.     pfh = (PIMAGE_FILE_HEADER) PEFHDROFFSET (lpFile);
  4541.  
  4542.     poh = (PIMAGE_OPTIONAL_HEADER) OPTHDROFFSET (lpFile);
  4543.  
  4544.     psh = (PIMAGE_SECTION_HEADER) SECHDROFFSET (lpFile);
  4545.  
  4546.     nSections = pfh->NumberOfSections;
  4547.  
  4548.     imageBase = poh->ImageBase;
  4549.  
  4550.     entryPoint = poh->AddressOfEntryPoint;
  4551.  
  4552.     CodeSize = poh->SizeOfCode;
  4553.  
  4554.     tds = pfh->TimeDateStamp;
  4555.     
  4556.     printTimeDateStamp(tds);
  4557.  
  4558.     if (psh == NULL) return 0;
  4559.  
  4560.     /* store section headers */
  4561.     
  4562.     nexes=0;
  4563.     for (i=0; i < nSections; i++)
  4564.     {       
  4565.         shdr[i] = *psh++;
  4566.          c=(int)shdr[i].Characteristics;
  4567.         if ((c&0x60000020)==0x60000020) nexes++; 
  4568.     }
  4569.  
  4570.     if (CodeSize==0) CodeSize=shdr[0].SizeOfRawData;
  4571.  
  4572.     // get Code offset and size, Data offset and size
  4573.     maxRVA = 0;
  4574.  
  4575.     baseofcode=poh->BaseOfCode;
  4576.     baseofdata=poh->BaseOfData;
  4577.     if (baseofcode>=baseofdata) baseofcode=0;
  4578.     //fprintf(stderr,"\npoh->BaseOfCode=%08X",poh->BaseOfCode);getch();
  4579.  
  4580.     for (i=0; i < nSections; i++)
  4581.     {       
  4582.         if (baseofcode == shdr[i].VirtualAddress 
  4583.          || (baseofcode == 0 && i == 0))
  4584.         {
  4585.             imagebaseRVA = imageBase + shdr[i].VirtualAddress;
  4586.             CodeOffset = shdr[i].PointerToRawData;
  4587.             CodeSize1 = shdr[i].SizeOfRawData;
  4588.             if (nexes==1 && CodeSize!=CodeSize1) CodeSize=CodeSize1;
  4589.             printf("Code Offset = %08X, Code Size = %08X \n", 
  4590.             (int)(shdr[i].PointerToRawData), CodeSize);
  4591.         }
  4592.         if (shdr[i].VirtualAddress>maxRVA) 
  4593.         {
  4594.             maxRVA = shdr[i].VirtualAddress;
  4595.             maxRVAsize = shdr[i].SizeOfRawData;
  4596.         }
  4597.         if (((shdr[i].Characteristics) & 0xC0000040) == 0xC0000040)
  4598.         //if (poh->BaseOfData == shdr[i].VirtualAddress)
  4599.         {
  4600.         printf ("Data Offset = %08X, Data Size = %08X \n",
  4601.             (int)(shdr[i].PointerToRawData), (int)(shdr[i].SizeOfRawData));
  4602.             break;
  4603.         }
  4604.     }
  4605.     for (   ; i < nSections; i++)
  4606.     {       
  4607.         if (shdr[i].VirtualAddress>maxRVA) 
  4608.         {
  4609.             maxRVA = shdr[i].VirtualAddress;
  4610.             maxRVAsize = shdr[i].SizeOfRawData;
  4611.         }
  4612.     }
  4613.  
  4614.     printf ("\n");
  4615.     
  4616.     printf ("Number of Objects = %04d (dec), Imagebase = %08Xh \n",
  4617.         nSections, (int)imageBase);
  4618.  
  4619.     // object name alignment
  4620.     for (i=0; i < nSections; i++)
  4621.     {
  4622.         for (j=0;j<7;j++) if (shdr[i].Name[j]==0) shdr[i].Name[j]=32;
  4623.         shdr[i].Name[7]=0;
  4624.     }
  4625.     for (i=0; i < nSections; i++)
  4626.         printf ("\n   Object%02d: %8s RVA: %08X Offset: %08X Size: %08X Flags: %08X ",
  4627.             i+1, shdr[i].Name, (int)(shdr[i].VirtualAddress), 
  4628.             (int)(shdr[i].PointerToRawData),
  4629.             (int)(shdr[i].SizeOfRawData), (int)(shdr[i].Characteristics));
  4630.     // Get List of Resources
  4631.     nResources = GetListOfResourceTypes (lpFile, &pnstr);
  4632.     pst = pnstr;
  4633.     printf ("\n");
  4634.     printf ("\n+++++++++++++++++++ RESOURCE INFORMATION +++++++++++++++++++");
  4635.     printf ("\n");
  4636.     if (nResources==0)
  4637.     printf ("\n        There are no Resources in This Application.\n");
  4638.     else
  4639.     {
  4640.         printf ("\nNumber of Resource Types = %4d (decimal)\n", nResources);
  4641.         for (i=0; i < nResources; i++)
  4642.         {
  4643.         printf ("\n   Resource Type %03d: %s",i+1, pst);
  4644.         pst += strlen ((char *)(pst)) + 1;
  4645.         }
  4646.         free ((void *)pnstr);
  4647.         
  4648.         nCursors = GetContentsOfCursor (lpFile, &pcNameBuff);
  4649.         
  4650.         if (nCursors > 0)
  4651.         {
  4652.             printf ("\n");
  4653.             printf ("\n+++++++++++++++++ CURSOR INFORMATION +++++++++++++++++++");
  4654.             printf ("\n");
  4655.             
  4656.             pst = pcNameBuff;
  4657.             printf ("\nNumber of Cursors = %4d (decimal)", nCursors);
  4658.  
  4659.             printf ("\n");
  4660.  
  4661.             for (i=0; i < nCursors; i++)
  4662.             {
  4663.                 // Cursor ID print
  4664.                 printf ("\nName: %s", pst);
  4665.                 pst += (n=strlen (pst)) + 1;
  4666.                 for(j=n;j<20;j++) printf(" ");
  4667.                 PrintCursor (&pst);
  4668.             }                                                   
  4669.             free ((void *)pcNameBuff); 
  4670.         }
  4671.  
  4672.         nBitmaps = GetContentsOfBitmap (lpFile, &pbNameBuff);
  4673.         
  4674.         if (nBitmaps > 0)
  4675.         {
  4676.             printf ("\n");
  4677.             printf ("\n+++++++++++++++++ BITMAP INFORMATION +++++++++++++++++++");
  4678.             printf ("\n");
  4679.             
  4680.             pst = pbNameBuff;
  4681.             printf ("\nNumber of Bitmaps = %4d (decimal)", nBitmaps);
  4682.  
  4683.             printf ("\n");
  4684.  
  4685.             for (i=0; i < nBitmaps; i++)
  4686.             {
  4687.                 // Bitmap ID print
  4688.                 printf ("\nName: %s", pst);
  4689.                 pst += (n=strlen (pst)) + 1;
  4690.                 for(j=n;j<20;j++) printf(" ");
  4691.                 PrintBitmap (&pst);
  4692.             }                                                   
  4693.             free ((void *)pbNameBuff); 
  4694.         }
  4695.  
  4696.         nIcons = GetContentsOfIcon (lpFile, &pnNameBuff);
  4697.         
  4698.         if (nIcons > 0)
  4699.         {
  4700.             printf ("\n");
  4701.             printf ("\n+++++++++++++++++ ICON INFORMATION +++++++++++++++++++");
  4702.             printf ("\n");
  4703.             
  4704.             pst = pnNameBuff;
  4705.             printf ("\nNumber of Icons = %4d (decimal)", nIcons);
  4706.  
  4707.             printf ("\n");
  4708.  
  4709.             for (i=0; i < nIcons; i++)
  4710.             {
  4711.                 // Dialog ID print
  4712.                 printf ("\nName: %s", pst);
  4713.                 pst += (n=strlen (pst)) + 1;
  4714.                 for(j=n;j<20;j++) printf(" ");
  4715.                 PrintIcon (&pst);
  4716.             }                                                   
  4717.             free ((void *)pnNameBuff); 
  4718.         }
  4719.  
  4720.  
  4721.         nMenus = GetContentsOfMenu (lpFile, &pmNameBuff);
  4722.         
  4723.         if (nMenus > 0)
  4724.         {
  4725.             printf ("\n");
  4726.             printf ("\n+++++++++++++++++++ MENU INFORMATION +++++++++++++++++++");
  4727.             printf ("\n");
  4728.             
  4729.             pst = pmNameBuff;
  4730.             printf ("\nNumber of Menus = %4d (decimal)", nMenus);
  4731.  
  4732.             //dumpMenu(&pst, 8096); 
  4733.             for (i=0; i < nMenus; i++)
  4734.             {
  4735.                 // menu ID print
  4736.                 printf ("\n\n%s", pst);
  4737.                 pst += strlen (pst) + 1;
  4738.                 printf ("\n-------------");
  4739.                 if (strncmp (pst, ":::::::::::", 11) == 0)
  4740.                 {    
  4741.                     printf("\n");
  4742.                     PrintStrangeMenu (&pst);
  4743.                 }
  4744.                 else 
  4745.                 {
  4746.                     PrintMenu (6, &pst);
  4747.                 }
  4748.                 //else PrintStrangeMenu(&pst);
  4749.             }                                               
  4750.             free ((void *)pmNameBuff); 
  4751.             printf ("\n");
  4752.         }
  4753.  
  4754.         nDialogs = GetContentsOfDialog (lpFile, &pdNameBuff);
  4755.         
  4756.         if (nDialogs > 0)
  4757.         {
  4758.             printf ("\n");
  4759.             printf ("\n+++++++++++++++++ DIALOG INFORMATION +++++++++++++++++++");
  4760.             printf ("\n");
  4761.             
  4762.             pst = pdNameBuff;
  4763.             printf ("\nNumber of Dialogs = %4d (decimal)", nDialogs);
  4764.  
  4765.             printf ("\n");
  4766.  
  4767.             for (i=0; i < nDialogs; i++)
  4768.             {
  4769.                 // Dialog ID print
  4770.                 printf ("\nName: %s", pst);
  4771.                 pst += strlen (pst) + 1;
  4772.                 PrintDialog (&pst);
  4773.             }                                                   
  4774.             free ((void *)pdNameBuff); 
  4775.             printf ("\n");
  4776.         }
  4777.  
  4778.         nStrings = GetContentsOfString (lpFile, &psNameBuff);
  4779.        
  4780.         if (nStrings > 0)
  4781.         {
  4782.             printf ("\n");
  4783.             printf ("\n+++++++++++++++++ STRING INFORMATION +++++++++++++++++++");
  4784.             printf ("\n");
  4785.             
  4786.             pst = psNameBuff;
  4787.             printf ("\nNumber of Strings = %4d (decimal)", nStrings);
  4788.  
  4789.             printf ("\n");
  4790.  
  4791.             for (i=0; i < nStrings; i++)
  4792.         {
  4793.                 // String ID print
  4794.                 printf ("\nName: %s", pst);
  4795.                 pst += strlen (pst) + 1;
  4796.                 PrintString (&pst);
  4797.         }                                                   
  4798.         }
  4799.         free ((void *)psNameBuff); 
  4800.         printf ("\n");
  4801.     }
  4802.     
  4803.     printf ("\n+++++++++++++++++++ IMPORTED FUNCTIONS +++++++++++++++++++\n");
  4804.  
  4805.     nImportedModules = GetImportModuleNames (lpFile, &piNameBuff);
  4806.     if (nImportedModules == 0)
  4807.     {
  4808.         printf("\n        There are no imported Functions in This Application.\n");
  4809.     }
  4810.     else
  4811.     {
  4812.         pnstr = piNameBuff;
  4813.         printf ("\nNumber of Imported Modules = %4d (decimal)\n", nImportedModules);
  4814.         for (i=0; i < nImportedModules; i++)
  4815.         {
  4816.         printf ("\n   Import Module %03d: %s",i+1, pnstr + 4);
  4817.         pnstr += strlen ((char *)(pnstr+4)) + 1 + 4;
  4818.         }
  4819.         
  4820.         printf("\n");
  4821.         printf("\n+++++++++++++++++++ IMPORT MODULE DETAILS +++++++++++++++++");
  4822.         pnstr = piNameBuff;
  4823.         for (i=0; i < nImportedModules; i++)
  4824.         {
  4825.             printf ("\n\n   Import Module %03d: %s \n",i+1, pnstr + 4);
  4826.             nFunctions = GetImportFunctionNamesByModule (lpFile, pnstr, &pfNameBuff);
  4827.             pnstr += strlen ((char *)(pnstr+4)) + 1 + 4;
  4828.             pst = pfNameBuff;
  4829.             for (j=0;j < nFunctions;j++)
  4830.             {
  4831.             printf ("\nAddr:%08X hint(%04X) Name: %s",
  4832.                     (*(int *)pst),(*(short *)(pst+4)), 
  4833.                         //(pst+6));
  4834.                         (char*)TranslateFunctionName(pst+6));
  4835.                 pst += strlen ((char *)(pst+6)) + 1 + 6;
  4836.             }
  4837.             free ((void *)pfNameBuff);
  4838.         }
  4839.         //free ((void *)piNameBuff);
  4840.     }
  4841.     
  4842.     printf("\n");
  4843.     printf("\n+++++++++++++++++++ EXPORTED FUNCTIONS +++++++++++++++++++\n");
  4844.  
  4845.     nExportedFunctions = GetExportFunctionNames (lpFile, &peNameBuff);
  4846.     printf ("\nNumber of Exported Functions = %4d (decimal)\n", nExportedFunctions);
  4847.     
  4848.  
  4849.     MapSize = CodeSize + CodeOffset;
  4850.     lpMap = (void *) calloc (MapSize, 1);
  4851.     lpMap1 = (void *)calloc (MapSize, 1);
  4852.     if (lpMap==NULL || lpMap1==NULL) 
  4853.     {
  4854.         fprintf(stderr,"cannot allocate memory.");exit(0);
  4855.     }
  4856.  
  4857.     if (nExportedFunctions > 0)
  4858.     {
  4859.         pst = peNameBuff;
  4860.       
  4861.         for (i=0; i < nExportedFunctions; i++)
  4862.         {
  4863.             printf ("\nAddr:%08X Ord:%4d (%04Xh) Name: %s",
  4864.                    (*(int *)pst), (*(WORD *)(pst+4)), (*(WORD *)(pst+4)), 
  4865.                    //(pst+6));
  4866.                    (char*)TranslateFunctionName(pst+6));
  4867.             // this one is needed to link export function names to codes..
  4868.             k.class=2048; k.c_ref= *(int *)pst; k.c_pos=0;
  4869.             if (AddressCheck(k.c_ref))
  4870.             {
  4871.                 MyBtreeInsertEx(&k);
  4872.                 if(isGoodAddress(k.c_ref))
  4873.                     addLabels(k.c_ref,512);
  4874.                 orMap(k.c_ref, 0x40);
  4875.                 k.class=992; k.c_pos=(int)(pst+6);
  4876.                 MyBtreeInsertX(&k);
  4877.             }
  4878.             pst += strlen ((char *)(pst+6)) + 6+1;
  4879.         }
  4880.         //free ((void *)peNameBuff);
  4881.     }       
  4882.     // free ((void *)lpFile);
  4883.     return 1;
  4884. }       
  4885.