- Fix the return key handling and clean up the code.
[wine] / tools / winedump / ne.c
1 /*
2  * Dumping of NE binaries
3  *
4  * Copyright 2002 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <fcntl.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #ifdef HAVE_UNISTD_H
28 #include <unistd.h>
29 #endif
30
31 #include "windef.h"
32 #include "winbase.h"
33 #include "wine/winbase16.h"
34 #include "winedump.h"
35
36 static void dump_ne_header( const IMAGE_OS2_HEADER *ne )
37 {
38     printf( "File header:\n" );
39     printf( "Linker version:      %d.%d\n", ne->ne_ver, ne->ne_rev );
40     printf( "Entry table:         %x len %d\n", ne->ne_enttab, ne->ne_cbenttab );
41     printf( "Checksum:            %08lx\n", ne->ne_crc );
42     printf( "Flags:               %04x\n", ne->ne_flags );
43     printf( "Auto data segment:   %x\n", ne->ne_autodata );
44     printf( "Heap size:           %d bytes\n", ne->ne_heap );
45     printf( "Stack size:          %d bytes\n", ne->ne_stack );
46     printf( "Stack pointer:       %x:%04x\n", SELECTOROF(ne->ne_sssp), OFFSETOF(ne->ne_sssp) );
47     printf( "Entry point:         %x:%04x\n", SELECTOROF(ne->ne_csip), OFFSETOF(ne->ne_csip) );
48     printf( "Number of segments:  %d\n", ne->ne_cseg );
49     printf( "Number of modrefs:   %d\n", ne->ne_cmod );
50     printf( "Segment table:       %x\n", ne->ne_segtab );
51     printf( "Resource table:      %x\n", ne->ne_rsrctab );
52     printf( "Resident name table: %x\n", ne->ne_restab );
53     printf( "Module table:        %x\n", ne->ne_modtab );
54     printf( "Import table:        %x\n", ne->ne_imptab );
55     printf( "Non-resident table:  %lx\n", ne->ne_nrestab );
56     printf( "Exe type:            %x\n", ne->ne_exetyp );
57     printf( "Other flags:         %x\n", ne->ne_flagsothers );
58     printf( "Fast load area:      %x-%x\n", ne->ne_pretthunks << ne->ne_align,
59             (ne->ne_pretthunks+ne->ne_psegrefbytes) << ne->ne_align );
60     printf( "Expected version:    %d.%d\n", HIBYTE(ne->ne_expver), LOBYTE(ne->ne_expver) );
61 }
62
63 static void dump_ne_names( const void *base, const IMAGE_OS2_HEADER *ne )
64 {
65     char *pstr = (char *)ne + ne->ne_restab;
66
67     printf( "\nResident-name table:\n" );
68     while (*pstr)
69     {
70         printf( "%*.*s: %d\n", *pstr, *pstr, pstr + 1, *(WORD *)(pstr + *pstr + 1) );
71         pstr += *pstr + 1 + sizeof(WORD);
72     }
73 }
74
75 static const char *get_resource_type( WORD id )
76 {
77     static char buffer[5];
78     switch(id)
79     {
80     case NE_RSCTYPE_CURSOR: return "CURSOR";
81     case NE_RSCTYPE_BITMAP: return "BITMAP";
82     case NE_RSCTYPE_ICON: return "ICON";
83     case NE_RSCTYPE_MENU: return "MENU";
84     case NE_RSCTYPE_DIALOG: return "DIALOG";
85     case NE_RSCTYPE_STRING: return "STRING";
86     case NE_RSCTYPE_FONTDIR: return "FONTDIR";
87     case NE_RSCTYPE_FONT: return "FONT";
88     case NE_RSCTYPE_ACCELERATOR: return "ACCELERATOR";
89     case NE_RSCTYPE_RCDATA: return "RCDATA";
90     case NE_RSCTYPE_GROUP_CURSOR: return "CURSOR_GROUP";
91     case NE_RSCTYPE_GROUP_ICON: return "ICON_GROUP";
92     default:
93         sprintf( buffer, "%04x", id );
94         return buffer;
95     }
96 }
97
98 static void dump_ne_resources( const void *base, const IMAGE_OS2_HEADER *ne )
99 {
100     NE_NAMEINFO *name;
101     const void *res_ptr = (char *)ne + ne->ne_rsrctab;
102     WORD size_shift = *(WORD *)res_ptr;
103     NE_TYPEINFO *info = (NE_TYPEINFO *)((WORD *)res_ptr + 1);
104     int count;
105
106     printf( "\nResources:\n" );
107     while (info->type_id != 0 && (char *)info < (char *)ne + ne->ne_restab)
108     {
109         name = (NE_NAMEINFO *)(info + 1);
110         for (count = info->count; count > 0; count--, name++)
111         {
112             if (name->id & 0x8000) printf( "  %d", (name->id & ~0x8000) );
113             else printf( "  %.*s", *((unsigned char *)res_ptr + name->id),
114                          (char *)res_ptr + name->id + 1 );
115             if (info->type_id & 0x8000) printf( " %s\n", get_resource_type(info->type_id) );
116             else printf( " %.*s\n", *((unsigned char *)res_ptr + info->type_id),
117                          (char *)res_ptr + info->type_id + 1 );
118             dump_data( (unsigned char *)base + (name->offset << size_shift),
119                        name->length << size_shift, "    " );
120         }
121         info = (NE_TYPEINFO *)name;
122     }
123 }
124
125 void ne_dump( const void *exe, size_t exe_size )
126 {
127     const IMAGE_DOS_HEADER *dos = exe;
128     const IMAGE_OS2_HEADER *ne = (IMAGE_OS2_HEADER *)((char *)dos + dos->e_lfanew);
129
130     dump_ne_header( ne );
131     dump_ne_names( exe, ne );
132     dump_ne_resources( exe, ne );
133 }