Correct size check.
[wine] / tools / winedump / dump.c
1 /*
2  *      File dumping utility
3  *
4  *      Copyright 2001,2005 Eric Pouech
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 <stdlib.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
30 #include <time.h>
31 #ifdef HAVE_SYS_TYPES_H
32 # include <sys/types.h>
33 #endif
34 #ifdef HAVE_SYS_STAT_H
35 # include <sys/stat.h>
36 #endif
37 #ifdef HAVE_SYS_MMAN_H
38 #include <sys/mman.h>
39 #endif
40 #include <fcntl.h>
41
42 #define NONAMELESSUNION
43 #define NONAMELESSSTRUCT
44 #include "windef.h"
45 #include "winbase.h"
46 #include "winedump.h"
47 #include "pe.h"
48
49 static void*                    dump_base;
50 static unsigned long            dump_total_len;
51
52 void dump_data( const unsigned char *ptr, unsigned int size, const char *prefix )
53 {
54     unsigned int i, j;
55
56     printf( "%s%08x: ", prefix, 0 );
57     if (!ptr)
58     {
59         printf("NULL\n");
60         return;
61     }
62     for (i = 0; i < size; i++)
63     {
64         printf( "%02x%c", ptr[i], (i % 16 == 7) ? '-' : ' ' );
65         if ((i % 16) == 15)
66         {
67             printf( " " );
68             for (j = 0; j < 16; j++)
69                 printf( "%c", isprint(ptr[i-15+j]) ? ptr[i-15+j] : '.' );
70             if (i < size-1) printf( "\n%s%08x: ", prefix, i + 1 );
71         }
72     }
73     if (i % 16)
74     {
75         printf( "%*s ", 3 * (16-(i%16)), "" );
76         for (j = 0; j < i % 16; j++)
77             printf( "%c", isprint(ptr[i-(i%16)+j]) ? ptr[i-(i%16)+j] : '.' );
78     }
79     printf( "\n" );
80 }
81
82 const char*     get_time_str(DWORD _t)
83 {
84     time_t      t = (time_t)_t;
85     const char *str = ctime(&t);
86     size_t len = strlen(str);
87     static char buf[128];
88     /* FIXME: I don't get the same values from MS' pedump running under Wine...
89      * I wonder if Wine isn't broken wrt to GMT settings...
90      */
91     if (len && str[len-1] == '\n') len--;
92     if (len >= sizeof(buf)) len = sizeof(buf) - 1;
93     memcpy( buf, str, len );
94     buf[len] = 0;
95     return buf;
96 }
97
98 unsigned int strlenW( const WCHAR *str )
99 {
100     const WCHAR *s = str;
101     while (*s) s++;
102     return s - str;
103 }
104
105 void dump_unicode_str( const WCHAR *str, int len )
106 {
107     if (len == -1) len = strlenW( str );
108     printf( "L\"");
109     while (len-- > 0 && *str)
110     {
111         WCHAR c = *str++;
112         switch (c)
113         {
114         case '\n': printf( "\\n" ); break;
115         case '\r': printf( "\\r" ); break;
116         case '\t': printf( "\\t" ); break;
117         case '"':  printf( "\\\"" ); break;
118         case '\\': printf( "\\\\" ); break;
119         default:
120             if (c >= ' ' && c <= 126) putchar(c);
121             else printf( "\\u%04x",c);
122         }
123     }
124     printf( "\"" );
125 }
126
127 void*   PRD(unsigned long prd, unsigned long len)
128 {
129     return (prd + len > dump_total_len) ? NULL : (char*)dump_base + prd;
130 }
131
132 unsigned long Offset(void* ptr)
133 {
134     if (ptr < dump_base) {printf("<<<<<ptr below\n");return 0;}
135     if ((char *)ptr >= (char*)dump_base + dump_total_len) {printf("<<<<<ptr above\n");return 0;}
136     return (char*)ptr - (char*)dump_base;
137 }
138
139 static  void    do_dump( enum FileSig sig, void* pmt )
140 {
141     if (sig == SIG_NE)
142     {
143         ne_dump( dump_base, dump_total_len );
144         return;
145     }
146
147     if (sig == SIG_LE)
148     {
149         le_dump( dump_base, dump_total_len );
150         return;
151     }
152
153     pe_dump(pmt);
154 }
155
156 static  enum FileSig    check_headers(void** pmt)
157 {
158     WORD*               pw;
159     DWORD*              pdw;
160     IMAGE_DOS_HEADER*   dh;
161     enum FileSig        sig;
162
163     pw = PRD(0, sizeof(WORD));
164     if (!pw) {printf("Can't get main signature, aborting\n"); return 0;}
165
166     *pmt = NULL;
167     switch (*pw)
168     {
169     case IMAGE_DOS_SIGNATURE:
170         sig = SIG_DOS;
171         dh = PRD(0, sizeof(IMAGE_DOS_HEADER));
172         if (dh && dh->e_lfanew >= sizeof(*dh)) /* reasonable DOS header ? */
173         {
174             /* the signature is the first DWORD */
175             pdw = PRD(dh->e_lfanew, sizeof(DWORD));
176             if (pdw)
177             {
178                 if (*pdw == IMAGE_NT_SIGNATURE)
179                 {
180                     *pmt = PRD(dh->e_lfanew, sizeof(DWORD)+sizeof(IMAGE_FILE_HEADER));
181                     sig = SIG_PE;
182                 }
183                 else if (*(WORD *)pdw == IMAGE_OS2_SIGNATURE)
184                 {
185                     sig = SIG_NE;
186                 }
187                 else if (*(WORD *)pdw == IMAGE_VXD_SIGNATURE)
188                 {
189                     sig = SIG_LE;
190                 }
191                 else
192                 {
193                     printf("No PE Signature found\n");
194                 }
195             }
196             else
197             {
198                 printf("Can't get the extented signature, aborting\n");
199             }
200         }
201         break;
202     case 0x4944: /* "DI" */
203         sig = SIG_DBG;
204         break;
205     case 0x444D: /* "MD" */
206         pdw = PRD(0, sizeof(DWORD));
207         if (pdw && *pdw == 0x504D444D) /* "MDMP" */
208             sig = SIG_MDMP;
209         else
210             sig = SIG_UNKNOWN;
211         break;
212     default:
213         printf("No known main signature (%.2s/%x), aborting\n", (char*)pw, *pw);
214         sig = SIG_UNKNOWN;
215     }
216
217     return sig;
218 }
219
220 int dump_analysis(const char* name, void (*fn)(enum FileSig, void*), enum FileSig wanted_sig)
221 {
222     int                 fd;
223     enum FileSig        effective_sig;
224     int                 ret = 1;
225     struct stat         s;
226     void*               pmt;
227
228     setbuf(stdout, NULL);
229
230     fd = open(name, O_RDONLY | O_BINARY);
231     if (fd == -1) fatal("Can't open file");
232
233     if (fstat(fd, &s) < 0) fatal("Can't get size");
234     dump_total_len = s.st_size;
235
236 #ifdef HAVE_MMAP
237     if ((dump_base = mmap(NULL, dump_total_len, PROT_READ, MAP_PRIVATE, fd, 0)) == (void *)-1)
238 #endif
239     {
240         if (!(dump_base = malloc( dump_total_len ))) fatal( "Out of memory" );
241         if ((unsigned long)read( fd, dump_base, dump_total_len ) != dump_total_len) fatal( "Cannot read file" );
242     }
243
244     effective_sig = check_headers(&pmt);
245
246     if (effective_sig == SIG_UNKNOWN)
247     {
248         printf("Can't get a recognized file signature, aborting\n");
249         ret = 0;
250     }
251     else if (wanted_sig == SIG_UNKNOWN || wanted_sig == effective_sig)
252     {
253         switch (effective_sig)
254         {
255         case SIG_UNKNOWN: /* shouldn't happen... */
256             ret = 0; break;
257         case SIG_PE:
258         case SIG_NE:
259         case SIG_LE:
260             printf("Contents of \"%s\": %ld bytes\n\n", name, dump_total_len);
261             (*fn)(effective_sig, pmt);
262             break;
263         case SIG_DBG:
264             dump_separate_dbg();
265             break;
266         case SIG_DOS:
267             ret = 0; break;
268         case SIG_MDMP:
269             mdmp_dump();
270             break;
271         }
272     }
273     else
274     {
275         printf("Can't get a suitable file signature, aborting\n");
276         ret = 0;
277     }
278
279     if (ret) printf("Done dumping %s\n", name);
280 #ifdef HAVE_MMAP
281     if (munmap(dump_base, dump_total_len) == -1)
282 #endif
283     {
284         free( dump_base );
285     }
286     close(fd);
287
288     return ret;
289 }
290
291 void    dump_file(const char* name)
292 {
293     dump_analysis(name, do_dump, SIG_UNKNOWN);
294 }